5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
12 #include <sys/types.h>
18 #include <pciaccess.h>
20 #include <xorg/xf86.h>
21 #include <xorg/xf86Module.h>
22 #include <xorg/xf86str.h>
25 #define TRACE(f, ...) \
26 fprintf(stderr, ". %s " f, __FUNCTION__, ##__VA_ARGS__)
28 fprintf(stderr, "NI: %s\n", __FUNCTION__)
35 #define _X_EXPORT __attribute__((visibility("default")))
41 _X_EXPORT ClientPtr clients[MAXCLIENTS];
42 _X_EXPORT ScreenInfo screenInfo;
43 _X_EXPORT ClientPtr serverClient;
45 #define MAXCLIENTS 256
46 _X_EXPORT void *clients[MAXCLIENTS];
47 _X_EXPORT long screenInfo[16]; // ScreenInfo
48 _X_EXPORT void *serverClient;
51 _X_EXPORT ScrnInfoPtr *xf86Screens;
52 _X_EXPORT confDRIRec xf86ConfigDRI;
54 _X_EXPORT void *xf86Screens;
55 _X_EXPORT long xf86ConfigDRI[4]; // confDRIRec
57 _X_EXPORT int PanoramiXNumScreens;
58 _X_EXPORT long inputInfo[16]; // InputInfo
59 _X_EXPORT unsigned long globalSerialNumber = 0;
60 _X_EXPORT unsigned long serverGeneration = 0;
61 _X_EXPORT int noPanoramiXExtension;
62 _X_EXPORT int noRRExtension;
63 _X_EXPORT int noCompositeExtension;
64 _X_EXPORT int noRenderExtension;
65 _X_EXPORT char dispatchException;
66 _X_EXPORT void *xf86DriverList;
67 _X_EXPORT int xf86NumDrivers;
68 _X_EXPORT void *xf86configptr;
69 _X_EXPORT int xf86DefaultModes[1024]; // DisplayModeRec[]
70 _X_EXPORT int xf86Info[64]; // xf86InfoRec
71 _X_EXPORT int xf86Initialising;
72 _X_EXPORT int XRT_WINDOW;
73 _X_EXPORT int monitorResolution;
74 _X_EXPORT int DPMSEnabled;
75 #define SCREEN_SAVER_OFF 1
76 _X_EXPORT int screenIsSaved = SCREEN_SAVER_OFF;
79 _X_EXPORT void xf86DrvMsg(int scrnIndex, MessageType type, const char *format, ...);
80 _X_EXPORT Bool xf86LoaderCheckSymbol(const char *name);
81 _X_EXPORT void xf86AddDriver(DriverPtr driver, pointer module, int flags);
83 _X_EXPORT void GetSpritePosition(struct _DeviceIntRec*, int *, int *);
84 _X_EXPORT int xf86LoadKernelModule(const char *pathname);
86 // don't delete my globals..
87 extern void ddmg(void *g);
89 static void __attribute__((constructor)) ddmg_hack(void)
92 ddmg(&PanoramiXNumScreens);
95 ddmg(&globalSerialNumber);
96 ddmg(&serverGeneration);
97 ddmg(&noPanoramiXExtension);
99 ddmg(&noCompositeExtension);
100 ddmg(&noRenderExtension);
101 ddmg(&dispatchException);
103 ddmg(&xf86DriverList);
104 ddmg(&xf86NumDrivers);
105 ddmg(&xf86configptr);
106 ddmg(&xf86DefaultModes);
108 ddmg(&xf86ConfigDRI);
109 ddmg(&xf86Initialising);
112 ddmg(&monitorResolution);
114 ddmg(&screenIsSaved);
116 ddmg(&LoadSubModule);
118 ddmg(&xf86LoaderCheckSymbol);
120 ddmg(&xf86VDrvMsgVerb);
123 ddmg(&xf86AddDriver);
124 ddmg(&GetSpritePosition);
128 ddmg(&xf86MatchDevice);
129 ddmg(&xf86LoadKernelModule);
130 ddmg(&xf86MatchPciInstances);
131 ddmg(&xf86CheckPciSlot);
132 ddmg(&xf86ConfigPciEntity);
133 ddmg(&xf86ComparePciBusString);
134 ddmg(&xf86ClaimPciSlot);
135 ddmg(&xf86GetPciInfoForEntity);
136 ddmg(&xf86GetEntityInfo);
137 ddmg(&xf86AllocateEntityPrivateIndex);
138 ddmg(&xf86GetEntityPrivate);
139 ddmg(&xf86SetEntitySharable);
140 ddmg(&xf86GetNumEntityInstances);
141 ddmg(&xf86GetDevFromEntity);
142 ddmg(&xf86AddBusDeviceToConfigure);
143 ddmg(&GetTimeInMillis);
144 ddmg(&xf86GetOptValBool);
147 #define MAKE_FUNC(name) \
148 int _X_EXPORT name() \
153 static void __attribute__((constructor)) c_##name(void) \
158 MAKE_FUNC(xf86SetDGAMode)
159 MAKE_FUNC(GetSpriteCursor)
160 MAKE_FUNC(StandardMinorOpcode)
163 GetSpritePosition(struct _DeviceIntRec *pDev, int *px, int *py)
169 LoadSubModule(pointer _parent, const char *module,
170 const char **subdirlist, const char **patternlist,
171 pointer options, const XF86ModReqInfo * modreq,
172 int *errmaj, int *errmin)
176 printf("LoadSubModule '%s'\n", module);
178 if (module && strstr(module, "fglrxdrm"))
179 module = "/usr/lib/xorg/modules/linux/libfglrxdrm.so";
180 ret = dlopen(module, RTLD_LAZY | RTLD_GLOBAL);
182 fprintf(stderr, "LoadSubModule dlopen: %s\n", dlerror());
188 LoaderSymbol(const char *name)
190 void *ret = dlsym(NULL, name);
191 TRACE("'%s' %p\n", name, ret);
196 xf86LoaderCheckSymbol(const char *name)
200 ret = (dlsym(NULL, name) != NULL);
201 TRACE("'%s' %d\n", name, ret);
206 xf86MatchDevice(const char *drivername, GDevPtr **sectlist)
210 TRACE("'%s' %p\n", drivername, sectlist);
212 if (sectlist == NULL)
216 list = malloc(2 * sizeof(list[0]));
228 xf86MatchPciInstances(const char *driverName, int vendorID,
229 SymTabPtr chipsets, PciChipsets *PCIchipsets,
230 GDevPtr *devList, int numDevs, DriverPtr drvp,
233 TRACE("'%s' %04x %p %p %p %d %p %p\n",
234 driverName, vendorID, chipsets, PCIchipsets, devList, numDevs,
235 drvp, foundEntities);
237 *foundEntities = malloc(sizeof(int));
238 if (*foundEntities == NULL)
241 (*foundEntities)[0] = 0;
246 xf86AddBusDeviceToConfigure(const char *driver, BusType bus, void *busData, int chipset)
248 struct pci_device *pci = busData;
250 TRACE("'%s' %d %p %d\n", driver, bus, busData, chipset);
252 drec.driver = strdup(driver);
253 drec.identifier = strdup("fake_dev");
254 drec.busID = malloc(64);
255 snprintf(drec.busID, 64, "PCI:%d:%d:%d", pci->bus, pci->dev, pci->func);
256 drec.chipID = pci->device_id;
257 drec.chipRev = pci->revision;
260 drec.myScreenSection = calloc(1, sizeof(*drec.myScreenSection));
262 TRACE("ret busID '%s'\n", drec.busID);
267 xf86CheckPciSlot(const struct pci_device *d)
274 xf86ConfigPciEntity(ScrnInfoPtr pScrn, int scrnFlag, int entityIndex,
275 PciChipsets *p_chip, void *dummy, EntityProc init,
276 EntityProc enter, EntityProc leave, pointer private)
280 TRACE("%p %x %d %p %p %p %p %p %p\n",
281 pScrn, scrnFlag, entityIndex, p_chip, dummy, init,
282 enter, leave, private);
284 screen = calloc(1, sizeof(*screen));
285 screen->entityList = calloc(1, sizeof(screen->entityList[0]));
286 screen->numEntities = 1;
287 // used by atiddxIsCrossDisplay_3
288 screen->confScreen = calloc(1, sizeof(*screen->confScreen));
290 xf86Screens = calloc(2, sizeof(xf86Screens[0]));
291 return (xf86Screens[0] = screen);
295 xf86ComparePciBusString(const char *busID, int bus, int device, int func)
297 TRACE("'%s' %d:%d:%d\n", busID, bus, device, func);
301 static struct pci_device *cur_pci_dev;
304 xf86ClaimPciSlot(struct pci_device *d, DriverPtr drvp,
305 int chipset, GDevPtr dev, Bool active)
307 TRACE("%p %p %d %p %d\n", d, drvp, chipset, dev, active);
314 xf86GetPciInfoForEntity(int entityIndex)
316 TRACE("%d\n", entityIndex);
322 xf86GetEntityInfo(int entityIndex)
326 TRACE("%d\n", entityIndex);
328 ret = calloc(1, sizeof(*ret));
330 ret->location.id.pci = cur_pci_dev;
336 static DevUnion *privs;
339 xf86AllocateEntityPrivateIndex(void)
344 TRACE("numprivs=%d\n", numprivs);
346 privs = realloc(privs, numprivs * sizeof(DevUnion));
351 xf86GetEntityPrivate(int entityIndex, int privIndex)
353 DevUnion *ret = NULL;
355 if (entityIndex != 0 || (unsigned int)privIndex > (unsigned int)numprivs)
358 ret = &privs[privIndex];
360 TRACE("%d %d; ret=%p\n", entityIndex, privIndex, ret);
365 xf86SetEntitySharable(int entityIndex)
367 TRACE("%d\n", entityIndex);
371 xf86GetNumEntityInstances(int entityIndex)
373 TRACE("%d\n", entityIndex);
378 xf86GetDevFromEntity(int entityIndex, int instance)
380 TRACE("%d %d\n", entityIndex, instance);
385 xf86LoadKernelModule(const char *modName)
387 TRACE("'%s'\n", modName);
392 XNFalloc(unsigned long n)
398 XNFcalloc(unsigned long n)
404 XNFrealloc(void *ptr, unsigned long amount)
406 return realloc(ptr, amount);
410 xf86DrvMsg(int scrnIndex, MessageType type, const char *format, ...)
414 printf("xf86DrvMsg: ");
415 va_start(ap, format);
422 xf86VDrvMsgVerb(int scrnIndex, MessageType type, int verb, const char *format,
425 printf("xf86VDrvMsgVerb: ");
426 vprintf(format, args);
430 void xf86Msg(MessageType type, const char *format, ...)
435 va_start(ap, format);
441 void xf86MsgVerb(MessageType type, int verb, const char *format, ...)
445 printf("xf86MsgVerb: ");
446 va_start(ap, format);
453 GetTimeInMillis(void)
456 clock_gettime(CLOCK_MONOTONIC, &tp);
457 return tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
461 xf86GetOptValBool(const OptionInfoRec *table, int token, Bool *value)
463 TRACE("%p/%s %d %p\n", table, table ? table->name : NULL, token, value);
467 static DriverPtr g_driver;
470 xf86AddDriver(DriverPtr driver, pointer module, int flags)
472 TRACE("%p %p %x\n", driver, module, flags);
476 static void scan_pci(void)
478 static struct pci_slot_match match = {
479 PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, 0
481 struct pci_device_iterator *iter;
482 struct pci_device *info;
485 ret = pci_system_init();
487 fprintf(stderr, "pci_system_init failed\n");
491 iter = pci_slot_match_iterator_create(&match);
492 while ((info = pci_device_next(iter)) != NULL) {
493 if ((info->device_class & 0xff0000) != 0x30000)
497 pci_device_probe(info);
498 if (pci_device_is_boot_vga(info))
501 pci_iterator_destroy(iter);
504 fprintf(stderr, "no display devices?\n");
508 if (info->vendor_id != 0x1002) {
509 fprintf(stderr, "not AMD display? (%04x)\n",
517 static int module_desc[64];
519 int main(int argc, char *argv[])
521 XF86ModuleData *mod_data;
522 ScrnInfoPtr screen = NULL;
523 pthread_mutex_t mutex;
524 int errmaj = 0, errmin = 0;
531 pthread_mutex_init(&mutex, NULL);
534 // preload libpciaccess as it's not in fglrx's deps
535 mod = dlopen("libpciaccess.so", RTLD_LAZY | RTLD_GLOBAL);
537 fprintf(stderr, "preload pciaccess: %s\n", dlerror());
539 // fglrx relies on pci_system_init() being called too
542 mod = dlopen("/usr/lib/xorg/modules/drivers/fglrx_drv.so",
543 RTLD_LAZY | RTLD_GLOBAL);
545 fprintf(stderr, "open module: %s\n", dlerror());
549 mod_data = dlsym(mod, "fglrxModuleData");
550 if (mod_data == NULL) {
551 fprintf(stderr, "dlsym fglrxModuleData: %s\n", dlerror());
555 if (mod_data->vers == NULL) {
556 fprintf(stderr, "mod_data->vers is NULL\n");
560 printf("name: %s\n", mod_data->vers->modname);
561 printf("vendor: %s\n", mod_data->vers->vendor);
562 printf("abiclass: %s\n", mod_data->vers->abiclass);
563 printf("moduleclass: %s\n", mod_data->vers->moduleclass);
564 printf("version: %u.%u.%u\n", mod_data->vers->majorversion,
565 mod_data->vers->minorversion, mod_data->vers->patchlevel);
568 // fglrx assumes descriptor 0 to be Xorg.log
569 fd = open("/var/log/Xorg.0.log", O_RDONLY);
571 perror("open Xorg.0.log");
580 if (mod_data->setup == NULL) {
581 fprintf(stderr, "mod_data->setup is NULL\n");
585 setup_ret = mod_data->setup(module_desc, NULL, &errmaj, &errmin);
586 printf("setup: %p, errmaj %u, errmin %u\n", setup_ret, errmaj, errmin);
587 if (setup_ret == NULL) {
588 fprintf(stderr, "setup() returned NULL\n");
592 if (g_driver == NULL) {
593 fprintf(stderr, "setup() did not provide driver ptr\n");
597 printf("driverName: %s\n", g_driver->driverName);
598 if (g_driver->Probe == NULL) {
599 fprintf(stderr, "Probe is NULL\n");
603 // #0 in xdl_xs111_atiddxPxEarlyCheck ()
604 // #1 in xdl_xs111_atiddxProbe ()
605 // #2 in atiddxProbe ()
606 ret = g_driver->Probe(g_driver, PROBE_DETECT);
607 printf("Probe(PROBE_DETECT): %d\n", ret);
611 ret = g_driver->Probe(g_driver, PROBE_DEFAULT);
612 printf("Probe(PROBE_DEFAULT): %d\n", ret);
617 if (xf86Screens == NULL || xf86Screens[0] == NULL) {
618 fprintf(stderr, "xf86Screens[0] did not get set\n");
621 screen = xf86Screens[0];
622 if (screen->PreInit == NULL) {
623 fprintf(stderr, "screen->PreInit did not get set\n");
626 ret = screen->PreInit(screen, 0);
627 printf("PreInit: %d\n", ret);