1/*
2 * Copyright 1997 through 2004 by Marc Aurele La France (TSI @ UQV), tsi@xfree86.org
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of Marc Aurele La France not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission.  Marc Aurele La France makes no representations
11 * about the suitability of this software for any purpose.  It is provided
12 * "as-is" without express or implied warranty.
13 *
14 * MARC AURELE LA FRANCE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO
16 * EVENT SHALL MARC AURELE LA FRANCE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22
23/*************************************************************************/
24
25/*
26 * Author:  Marc Aurele La France (TSI @ UQV), tsi@xfree86.org
27 *
28 * This is the ATI driver for XFree86.
29 *
30 * John Donne once said "No man is an island", and I am most certainly not an
31 * exception.  Contributions, intentional or not, to this and previous versions
32 * of this driver by the following are hereby acknowledged:
33 *
34 * Thomas Roell, Per Lindqvist, Doug Evans, Rik Faith, Arthur Tateishi,
35 * Alain Hebert, Ton van Rosmalen, David Chambers, William Shubert,
36 * ATI Technologies Incorporated, Robert Wolff, David Dawes, Mark Weaver,
37 * Hans Nasten, Kevin Martin, Frederic Rienthaler, Marc Bolduc, Reuben Sumner,
38 * Benjamin T. Yang, James Fast Kane, Randall Hopper, W. Marcus Miller,
39 * Henrik Harmsen, Christian Lupien, Precision Insight Incorporated,
40 * Mark Vojkovich, Huw D M Davies, Andrew C Aitchison, Ani Joshi,
41 * Kostas Gewrgiou, Jakub Jelinek, David S. Miller, A E Lawrence,
42 * Linus Torvalds, William Blew, Ignacio Garcia Etxebarria, Patrick Chase,
43 * Vladimir Dergachev, Egbert Eich, Mike A. Harris
44 *
45 * ... and, many, many others from around the world.
46 *
47 * In addition, this work would not have been possible without the active
48 * support, both moral and otherwise, of the staff and management of Computing
49 * and Network Services at the University of Alberta, in Edmonton, Alberta,
50 * Canada.
51 *
52 * The driver is intended to support all ATI adapters since their VGA Wonder
53 * V3, including OEM counterparts.
54 */
55
56#ifdef HAVE_CONFIG_H
57#include "config.h"
58#endif
59
60#ifdef XSERVER_LIBPCIACCESS
61#include <pciaccess.h>
62#endif
63#include "atipcirename.h"
64
65#include "ati.h"
66#include "atipciids.h"
67#include "ativersion.h"
68
69/* names duplicated from version headers */
70#define MACH64_DRIVER_NAME  "mach64"
71#define R128_DRIVER_NAME    "r128"
72#define RADEON_DRIVER_NAME  "radeon"
73
74enum
75{
76    ATI_CHIP_FAMILY_NONE = 0,
77    ATI_CHIP_FAMILY_Mach64,
78    ATI_CHIP_FAMILY_Rage128,
79    ATI_CHIP_FAMILY_Radeon
80};
81
82static int ATIChipID(const uint16_t);
83
84#ifdef XSERVER_LIBPCIACCESS
85
86/* domain defines (stolen from xserver) */
87#if (defined(__alpha__) || defined(__ia64__)) && defined (linux)
88# define PCI_DOM_MASK 0x01fful
89#else
90# define PCI_DOM_MASK 0x0ffu
91#endif
92
93#define PCI_DOM_FROM_BUS(bus)  (((bus) >> 8) & (PCI_DOM_MASK))
94#define PCI_BUS_NO_DOMAIN(bus) ((bus) & 0xffu)
95
96static struct pci_device*
97ati_device_get_from_busid(int bus, int dev, int func)
98{
99    return pci_device_find_by_slot(PCI_DOM_FROM_BUS(bus),
100                                   PCI_BUS_NO_DOMAIN(bus),
101                                   dev,
102                                   func);
103}
104
105static struct pci_device*
106ati_device_get_primary(void)
107{
108    struct pci_device *device = NULL;
109    struct pci_device_iterator *device_iter;
110
111    device_iter = pci_slot_match_iterator_create(NULL);
112
113    while ((device = pci_device_next(device_iter)) != NULL) {
114        if (xf86IsPrimaryPci(device))
115            break;
116    }
117
118    pci_iterator_destroy(device_iter);
119
120    return device;
121}
122
123#else /* XSERVER_LIBPCIACCESS */
124
125static pciVideoPtr
126ati_device_get_from_busid(int bus, int dev, int func)
127{
128    pciVideoPtr  pVideo = NULL;
129    pciVideoPtr *xf86PciVideoInfo;
130
131    xf86PciVideoInfo = xf86GetPciVideoInfo();
132
133    if (xf86PciVideoInfo == NULL)
134        return NULL;
135
136    while ((pVideo = *xf86PciVideoInfo++) != NULL)
137    {
138        if ((pVideo->bus == bus) && (pVideo->device == dev) &&
139            (pVideo->func == func))
140            break;
141    }
142
143    return pVideo;
144}
145
146static pciVideoPtr
147ati_device_get_primary()
148{
149    pciVideoPtr  pVideo = NULL;
150    pciVideoPtr *xf86PciVideoInfo;
151
152    xf86PciVideoInfo = xf86GetPciVideoInfo();
153
154    if (xf86PciVideoInfo == NULL)
155        return NULL;
156
157    while ((pVideo = *xf86PciVideoInfo++) != NULL)
158    {
159        if (xf86IsPrimaryPci(pVideo))
160            break;
161    }
162
163    return pVideo;
164}
165
166#endif /* XSERVER_LIBPCIACCESS */
167
168void
169ati_gdev_subdriver(pointer options)
170{
171    int      nATIGDev, nMach64GDev, nR128GDev, nRadeonGDev;
172    GDevPtr *ATIGDevs;
173    Bool     load_mach64 = FALSE, load_r128 = FALSE, load_radeon = FALSE;
174    int      i;
175
176    /* let the subdrivers configure for themselves */
177    if (xf86ServerIsOnlyDetecting())
178        return;
179
180    /* get Device sections with Driver "ati" */
181    nATIGDev = xf86MatchDevice(ATI_DRIVER_NAME, &ATIGDevs);
182    nMach64GDev = xf86MatchDevice(MACH64_DRIVER_NAME, NULL);
183    nR128GDev = xf86MatchDevice(R128_DRIVER_NAME, NULL);
184    nRadeonGDev = xf86MatchDevice(RADEON_DRIVER_NAME, NULL);
185
186    for (i = 0; i < nATIGDev; i++) {
187        GDevPtr     ati_gdev = ATIGDevs[i];
188        pciVideoPtr device = NULL;
189        int         chip_family;
190
191        /* get pci device for the Device section */
192        if (ati_gdev->busID) {
193            int bus, dev, func;
194
195            if (!xf86ParsePciBusString(ati_gdev->busID, &bus, &dev, &func))
196                continue;
197
198            device = ati_device_get_from_busid(bus, dev, func);
199        }
200        else {
201            device = ati_device_get_primary();
202        }
203
204        if (!device)
205            continue;
206
207        /* check for non-ati devices and prehistoric mach32 */
208        if ((PCI_DEV_VENDOR_ID(device) != PCI_VENDOR_ATI) ||
209            (PCI_DEV_DEVICE_ID(device) == PCI_CHIP_MACH32))
210            continue;
211
212        /* replace Driver line in the Device section */
213        chip_family = ATIChipID(PCI_DEV_DEVICE_ID(device));
214
215        if (chip_family == ATI_CHIP_FAMILY_Mach64) {
216            ati_gdev->driver = MACH64_DRIVER_NAME;
217            load_mach64 = TRUE;
218        }
219
220        if (chip_family == ATI_CHIP_FAMILY_Rage128) {
221            ati_gdev->driver = R128_DRIVER_NAME;
222            load_r128 = TRUE;
223        }
224
225        if (chip_family == ATI_CHIP_FAMILY_Radeon) {
226            ati_gdev->driver = RADEON_DRIVER_NAME;
227            load_radeon = TRUE;
228        }
229    }
230
231    free(ATIGDevs);
232
233    /* load subdrivers as primary modules and only if they do not get loaded
234     * from other device sections
235     */
236
237    if (load_mach64 && (nMach64GDev == 0))
238         xf86LoadOneModule(MACH64_DRIVER_NAME, options);
239
240    if (load_r128 && (nR128GDev == 0))
241         xf86LoadOneModule(R128_DRIVER_NAME, options);
242
243    if (load_radeon && (nRadeonGDev == 0))
244         xf86LoadOneModule(RADEON_DRIVER_NAME, options);
245}
246
247/*
248 * ATIChipID --
249 *
250 * This returns the ATI_CHIP_FAMILY_* value associated with a particular ChipID.
251 */
252static int
253ATIChipID(const uint16_t ChipID)
254{
255    switch (ChipID)
256    {
257        case PCI_CHIP_MACH64GX:
258        case PCI_CHIP_MACH64CX:
259        case PCI_CHIP_MACH64CT:
260        case PCI_CHIP_MACH64ET:
261        case PCI_CHIP_MACH64VT:
262        case PCI_CHIP_MACH64GT:
263        case PCI_CHIP_MACH64VU:
264        case PCI_CHIP_MACH64GU:
265        case PCI_CHIP_MACH64LG:
266        case PCI_CHIP_MACH64VV:
267        case PCI_CHIP_MACH64GV:
268        case PCI_CHIP_MACH64GW:
269        case PCI_CHIP_MACH64GY:
270        case PCI_CHIP_MACH64GZ:
271        case PCI_CHIP_MACH64GB:
272        case PCI_CHIP_MACH64GD:
273        case PCI_CHIP_MACH64GI:
274        case PCI_CHIP_MACH64GP:
275        case PCI_CHIP_MACH64GQ:
276        case PCI_CHIP_MACH64LB:
277        case PCI_CHIP_MACH64LD:
278        case PCI_CHIP_MACH64LI:
279        case PCI_CHIP_MACH64LP:
280        case PCI_CHIP_MACH64LQ:
281        case PCI_CHIP_MACH64GL:
282        case PCI_CHIP_MACH64GM:
283        case PCI_CHIP_MACH64GN:
284        case PCI_CHIP_MACH64GO:
285        case PCI_CHIP_MACH64GR:
286        case PCI_CHIP_MACH64GS:
287        case PCI_CHIP_MACH64LM:
288        case PCI_CHIP_MACH64LN:
289        case PCI_CHIP_MACH64LR:
290        case PCI_CHIP_MACH64LS:
291            return ATI_CHIP_FAMILY_Mach64;
292
293        case PCI_CHIP_RAGE128RE:
294        case PCI_CHIP_RAGE128RF:
295        case PCI_CHIP_RAGE128RG:
296        case PCI_CHIP_RAGE128SK:
297        case PCI_CHIP_RAGE128SL:
298        case PCI_CHIP_RAGE128SM:
299        case PCI_CHIP_RAGE128SN:
300        case PCI_CHIP_RAGE128RK:
301        case PCI_CHIP_RAGE128RL:
302        case PCI_CHIP_RAGE128SE:
303        case PCI_CHIP_RAGE128SF:
304        case PCI_CHIP_RAGE128SG:
305        case PCI_CHIP_RAGE128SH:
306        case PCI_CHIP_RAGE128PA:
307        case PCI_CHIP_RAGE128PB:
308        case PCI_CHIP_RAGE128PC:
309        case PCI_CHIP_RAGE128PD:
310        case PCI_CHIP_RAGE128PE:
311        case PCI_CHIP_RAGE128PF:
312        case PCI_CHIP_RAGE128PG:
313        case PCI_CHIP_RAGE128PH:
314        case PCI_CHIP_RAGE128PI:
315        case PCI_CHIP_RAGE128PJ:
316        case PCI_CHIP_RAGE128PK:
317        case PCI_CHIP_RAGE128PL:
318        case PCI_CHIP_RAGE128PM:
319        case PCI_CHIP_RAGE128PN:
320        case PCI_CHIP_RAGE128PO:
321        case PCI_CHIP_RAGE128PP:
322        case PCI_CHIP_RAGE128PQ:
323        case PCI_CHIP_RAGE128PR:
324        case PCI_CHIP_RAGE128PS:
325        case PCI_CHIP_RAGE128PT:
326        case PCI_CHIP_RAGE128PU:
327        case PCI_CHIP_RAGE128PV:
328        case PCI_CHIP_RAGE128PW:
329        case PCI_CHIP_RAGE128PX:
330        case PCI_CHIP_RAGE128TF:
331        case PCI_CHIP_RAGE128TL:
332        case PCI_CHIP_RAGE128TR:
333        case PCI_CHIP_RAGE128TS:
334        case PCI_CHIP_RAGE128TT:
335        case PCI_CHIP_RAGE128TU:
336        case PCI_CHIP_RAGE128LE:
337        case PCI_CHIP_RAGE128LF:
338#if 0
339        case PCI_CHIP_RAGE128LK:
340        case PCI_CHIP_RAGE128LL:
341#endif
342        case PCI_CHIP_RAGE128MF:
343        case PCI_CHIP_RAGE128ML:
344            return ATI_CHIP_FAMILY_Rage128;
345
346        default:
347            return ATI_CHIP_FAMILY_Radeon;
348    }
349}
350