1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun * all copies or substantial portions of the Software.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17*4882a593Smuzhiyun * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*4882a593Smuzhiyun * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*4882a593Smuzhiyun * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*4882a593Smuzhiyun * OTHER DEALINGS IN THE SOFTWARE.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Except as contained in this notice, the name of the copyright holder(s)
23*4882a593Smuzhiyun * and author(s) shall not be used in advertising or otherwise to promote
24*4882a593Smuzhiyun * the sale, use or other dealings in this Software without prior written
25*4882a593Smuzhiyun * authorization from the copyright holder(s) and author(s).
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun * This file contains the interfaces to the bus-specific code
30*4882a593Smuzhiyun */
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #ifdef HAVE_XORG_CONFIG_H
33*4882a593Smuzhiyun #include <xorg-config.h>
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #include <ctype.h>
37*4882a593Smuzhiyun #include <stdlib.h>
38*4882a593Smuzhiyun #include <unistd.h>
39*4882a593Smuzhiyun #include <X11/X.h>
40*4882a593Smuzhiyun #include "os.h"
41*4882a593Smuzhiyun #include "xf86.h"
42*4882a593Smuzhiyun #include "xf86Priv.h"
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /* Bus-specific headers */
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #include "xf86Bus.h"
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #define XF86_OS_PRIVS
49*4882a593Smuzhiyun #include "xf86_OSproc.h"
50*4882a593Smuzhiyun #ifdef XSERVER_LIBPCIACCESS
51*4882a593Smuzhiyun #include "xf86VGAarbiter.h"
52*4882a593Smuzhiyun #endif
53*4882a593Smuzhiyun /* Entity data */
54*4882a593Smuzhiyun EntityPtr *xf86Entities = NULL; /* Bus slots claimed by drivers */
55*4882a593Smuzhiyun int xf86NumEntities = 0;
56*4882a593Smuzhiyun static int xf86EntityPrivateCount = 0;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun BusRec primaryBus = { BUS_NONE, {0} };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /**
61*4882a593Smuzhiyun * Call the driver's correct probe function.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * If the driver implements the \c DriverRec::PciProbe entry-point and an
64*4882a593Smuzhiyun * appropriate PCI device (with matching Device section in the xorg.conf file)
65*4882a593Smuzhiyun * is found, it is called. If \c DriverRec::PciProbe or no devices can be
66*4882a593Smuzhiyun * successfully probed with it (e.g., only non-PCI devices are available),
67*4882a593Smuzhiyun * the driver's \c DriverRec::Probe function is called.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * \param drv Driver to probe
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * \return
72*4882a593Smuzhiyun * If a device can be successfully probed by the driver, \c TRUE is
73*4882a593Smuzhiyun * returned. Otherwise, \c FALSE is returned.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun Bool
xf86CallDriverProbe(DriverPtr drv,Bool detect_only)76*4882a593Smuzhiyun xf86CallDriverProbe(DriverPtr drv, Bool detect_only)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun Bool foundScreen = FALSE;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun #ifdef XSERVER_PLATFORM_BUS
81*4882a593Smuzhiyun /* xf86platformBus.c does not support Xorg -configure */
82*4882a593Smuzhiyun if (!xf86DoConfigure && drv->platformProbe != NULL) {
83*4882a593Smuzhiyun foundScreen = xf86platformProbeDev(drv);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun #endif
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun #ifdef XSERVER_LIBPCIACCESS
88*4882a593Smuzhiyun if (!foundScreen && (drv->PciProbe != NULL)) {
89*4882a593Smuzhiyun if (xf86DoConfigure && xf86DoConfigurePass1) {
90*4882a593Smuzhiyun assert(detect_only);
91*4882a593Smuzhiyun foundScreen = xf86PciAddMatchingDev(drv);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun else {
94*4882a593Smuzhiyun assert(!detect_only);
95*4882a593Smuzhiyun foundScreen = xf86PciProbeDev(drv);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun #endif
99*4882a593Smuzhiyun if (!foundScreen && (drv->Probe != NULL)) {
100*4882a593Smuzhiyun xf86Msg(X_WARNING, "Falling back to old probe method for %s\n",
101*4882a593Smuzhiyun drv->driverName);
102*4882a593Smuzhiyun foundScreen = (*drv->Probe) (drv, (detect_only) ? PROBE_DETECT
103*4882a593Smuzhiyun : PROBE_DEFAULT);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun return foundScreen;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /**
110*4882a593Smuzhiyun * @return TRUE if all buses are configured and set up correctly and FALSE
111*4882a593Smuzhiyun * otherwise.
112*4882a593Smuzhiyun */
113*4882a593Smuzhiyun Bool
xf86BusConfig(void)114*4882a593Smuzhiyun xf86BusConfig(void)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun screenLayoutPtr layout;
117*4882a593Smuzhiyun int i, j;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * 3 step probe to (hopefully) ensure that we always find at least 1
121*4882a593Smuzhiyun * (non GPU) screen:
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun * 1. Call each drivers probe function normally,
124*4882a593Smuzhiyun * Each successful probe will result in an extra entry added to the
125*4882a593Smuzhiyun * xf86Screens[] list for each instance of the hardware found.
126*4882a593Smuzhiyun */
127*4882a593Smuzhiyun for (i = 0; i < xf86NumDrivers; i++) {
128*4882a593Smuzhiyun xf86CallDriverProbe(xf86DriverList[i], FALSE);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun * 2. If no Screens were found, call each drivers probe function with
133*4882a593Smuzhiyun * ignorePrimary = TRUE, to ensure that we do actually get a
134*4882a593Smuzhiyun * Screen if there is atleast one supported video card.
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun if (xf86NumScreens == 0) {
137*4882a593Smuzhiyun xf86ProbeIgnorePrimary = TRUE;
138*4882a593Smuzhiyun for (i = 0; i < xf86NumDrivers && xf86NumScreens == 0; i++) {
139*4882a593Smuzhiyun xf86CallDriverProbe(xf86DriverList[i], FALSE);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun xf86ProbeIgnorePrimary = FALSE;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun * 3. Call xf86platformAddGPUDevices() to add any additional video cards as
146*4882a593Smuzhiyun * GPUScreens (GPUScreens are only supported by platformBus drivers).
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun for (i = 0; i < xf86NumDrivers; i++) {
149*4882a593Smuzhiyun xf86platformAddGPUDevices(xf86DriverList[i]);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* If nothing was detected, return now */
153*4882a593Smuzhiyun if (xf86NumScreens == 0) {
154*4882a593Smuzhiyun xf86Msg(X_ERROR, "No devices detected.\n");
155*4882a593Smuzhiyun return FALSE;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun xf86VGAarbiterInit();
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun * Match up the screens found by the probes against those specified
162*4882a593Smuzhiyun * in the config file. Remove the ones that won't be used. Sort
163*4882a593Smuzhiyun * them in the order specified.
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * What is the best way to do this?
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * For now, go through the screens allocated by the probes, and
168*4882a593Smuzhiyun * look for screen config entry which refers to the same device
169*4882a593Smuzhiyun * section as picked out by the probe.
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun */
172*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
173*4882a593Smuzhiyun for (layout = xf86ConfigLayout.screens; layout->screen != NULL;
174*4882a593Smuzhiyun layout++) {
175*4882a593Smuzhiyun Bool found = FALSE;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun for (j = 0; j < xf86Screens[i]->numEntities; j++) {
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun GDevPtr dev =
180*4882a593Smuzhiyun xf86GetDevFromEntity(xf86Screens[i]->entityList[j],
181*4882a593Smuzhiyun xf86Screens[i]->entityInstanceList[j]);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (dev == layout->screen->device) {
184*4882a593Smuzhiyun /* A match has been found */
185*4882a593Smuzhiyun xf86Screens[i]->confScreen = layout->screen;
186*4882a593Smuzhiyun found = TRUE;
187*4882a593Smuzhiyun break;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun if (found)
191*4882a593Smuzhiyun break;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun if (layout->screen == NULL) {
194*4882a593Smuzhiyun /* No match found */
195*4882a593Smuzhiyun xf86Msg(X_ERROR,
196*4882a593Smuzhiyun "Screen %d deleted because of no matching config section.\n",
197*4882a593Smuzhiyun i);
198*4882a593Smuzhiyun xf86DeleteScreen(xf86Screens[i--]);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /* bind GPU conf screen to protocol screen 0 */
203*4882a593Smuzhiyun for (i = 0; i < xf86NumGPUScreens; i++)
204*4882a593Smuzhiyun xf86GPUScreens[i]->confScreen = xf86Screens[0]->confScreen;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* If no screens left, return now. */
207*4882a593Smuzhiyun if (xf86NumScreens == 0) {
208*4882a593Smuzhiyun xf86Msg(X_ERROR,
209*4882a593Smuzhiyun "Device(s) detected, but none match those in the config file.\n");
210*4882a593Smuzhiyun return FALSE;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun return TRUE;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * Call the bus probes relevant to the architecture.
218*4882a593Smuzhiyun *
219*4882a593Smuzhiyun * The only one available so far is for PCI and SBUS.
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun void
xf86BusProbe(void)223*4882a593Smuzhiyun xf86BusProbe(void)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun #ifdef XSERVER_PLATFORM_BUS
226*4882a593Smuzhiyun xf86platformProbe();
227*4882a593Smuzhiyun if (ServerIsNotSeat0() && xf86_num_platform_devices > 0)
228*4882a593Smuzhiyun return;
229*4882a593Smuzhiyun #endif
230*4882a593Smuzhiyun #ifdef XSERVER_LIBPCIACCESS
231*4882a593Smuzhiyun xf86PciProbe();
232*4882a593Smuzhiyun #endif
233*4882a593Smuzhiyun #if (defined(__sparc__) || defined(__sparc)) && !defined(__OpenBSD__)
234*4882a593Smuzhiyun xf86SbusProbe();
235*4882a593Smuzhiyun #endif
236*4882a593Smuzhiyun #ifdef XSERVER_PLATFORM_BUS
237*4882a593Smuzhiyun xf86platformPrimary();
238*4882a593Smuzhiyun #endif
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun * Determine what bus type the busID string represents. The start of the
243*4882a593Smuzhiyun * bus-dependent part of the string is returned as retID.
244*4882a593Smuzhiyun */
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun BusType
StringToBusType(const char * busID,const char ** retID)247*4882a593Smuzhiyun StringToBusType(const char *busID, const char **retID)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun char *p, *s;
250*4882a593Smuzhiyun BusType ret = BUS_NONE;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun /* If no type field, Default to PCI */
253*4882a593Smuzhiyun if (isdigit(busID[0])) {
254*4882a593Smuzhiyun if (retID)
255*4882a593Smuzhiyun *retID = busID;
256*4882a593Smuzhiyun return BUS_PCI;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun s = xstrdup(busID);
260*4882a593Smuzhiyun p = strtok(s, ":");
261*4882a593Smuzhiyun if (p == NULL || *p == 0) {
262*4882a593Smuzhiyun free(s);
263*4882a593Smuzhiyun return BUS_NONE;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun if (!xf86NameCmp(p, "pci") || !xf86NameCmp(p, "agp"))
266*4882a593Smuzhiyun ret = BUS_PCI;
267*4882a593Smuzhiyun if (!xf86NameCmp(p, "sbus"))
268*4882a593Smuzhiyun ret = BUS_SBUS;
269*4882a593Smuzhiyun if (!xf86NameCmp(p, "platform"))
270*4882a593Smuzhiyun ret = BUS_PLATFORM;
271*4882a593Smuzhiyun if (ret != BUS_NONE)
272*4882a593Smuzhiyun if (retID)
273*4882a593Smuzhiyun *retID = busID + strlen(p) + 1;
274*4882a593Smuzhiyun free(s);
275*4882a593Smuzhiyun return ret;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun int
xf86AllocateEntity(void)279*4882a593Smuzhiyun xf86AllocateEntity(void)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun xf86NumEntities++;
282*4882a593Smuzhiyun xf86Entities = xnfreallocarray(xf86Entities,
283*4882a593Smuzhiyun xf86NumEntities, sizeof(EntityPtr));
284*4882a593Smuzhiyun xf86Entities[xf86NumEntities - 1] = xnfcalloc(1, sizeof(EntityRec));
285*4882a593Smuzhiyun xf86Entities[xf86NumEntities - 1]->entityPrivates =
286*4882a593Smuzhiyun xnfcalloc(xf86EntityPrivateCount, sizeof(DevUnion));
287*4882a593Smuzhiyun return xf86NumEntities - 1;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun Bool
xf86IsEntityPrimary(int entityIndex)291*4882a593Smuzhiyun xf86IsEntityPrimary(int entityIndex)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun EntityPtr pEnt = xf86Entities[entityIndex];
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun #ifdef XSERVER_LIBPCIACCESS
296*4882a593Smuzhiyun if (primaryBus.type == BUS_PLATFORM && pEnt->bus.type == BUS_PCI)
297*4882a593Smuzhiyun return MATCH_PCI_DEVICES(pEnt->bus.id.pci, primaryBus.id.plat->pdev);
298*4882a593Smuzhiyun #endif
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun if (primaryBus.type != pEnt->bus.type)
301*4882a593Smuzhiyun return FALSE;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun switch (pEnt->bus.type) {
304*4882a593Smuzhiyun case BUS_PCI:
305*4882a593Smuzhiyun return pEnt->bus.id.pci == primaryBus.id.pci;
306*4882a593Smuzhiyun case BUS_SBUS:
307*4882a593Smuzhiyun return pEnt->bus.id.sbus.fbNum == primaryBus.id.sbus.fbNum;
308*4882a593Smuzhiyun case BUS_PLATFORM:
309*4882a593Smuzhiyun return pEnt->bus.id.plat == primaryBus.id.plat;
310*4882a593Smuzhiyun default:
311*4882a593Smuzhiyun return FALSE;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun Bool
xf86DriverHasEntities(DriverPtr drvp)316*4882a593Smuzhiyun xf86DriverHasEntities(DriverPtr drvp)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun int i;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun for (i = 0; i < xf86NumEntities; i++) {
321*4882a593Smuzhiyun if (xf86Entities[i]->driver == drvp)
322*4882a593Smuzhiyun return TRUE;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun return FALSE;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun void
xf86AddEntityToScreen(ScrnInfoPtr pScrn,int entityIndex)328*4882a593Smuzhiyun xf86AddEntityToScreen(ScrnInfoPtr pScrn, int entityIndex)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun if (entityIndex == -1)
331*4882a593Smuzhiyun return;
332*4882a593Smuzhiyun if (xf86Entities[entityIndex]->inUse &&
333*4882a593Smuzhiyun !(xf86Entities[entityIndex]->entityProp & IS_SHARED_ACCEL)) {
334*4882a593Smuzhiyun ErrorF("Requested Entity already in use!\n");
335*4882a593Smuzhiyun return;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun pScrn->numEntities++;
339*4882a593Smuzhiyun pScrn->entityList = xnfreallocarray(pScrn->entityList,
340*4882a593Smuzhiyun pScrn->numEntities, sizeof(int));
341*4882a593Smuzhiyun pScrn->entityList[pScrn->numEntities - 1] = entityIndex;
342*4882a593Smuzhiyun xf86Entities[entityIndex]->inUse = TRUE;
343*4882a593Smuzhiyun pScrn->entityInstanceList = xnfreallocarray(pScrn->entityInstanceList,
344*4882a593Smuzhiyun pScrn->numEntities,
345*4882a593Smuzhiyun sizeof(int));
346*4882a593Smuzhiyun pScrn->entityInstanceList[pScrn->numEntities - 1] = 0;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun void
xf86SetEntityInstanceForScreen(ScrnInfoPtr pScrn,int entityIndex,int instance)350*4882a593Smuzhiyun xf86SetEntityInstanceForScreen(ScrnInfoPtr pScrn, int entityIndex, int instance)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun int i;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun if (entityIndex == -1 || entityIndex >= xf86NumEntities)
355*4882a593Smuzhiyun return;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun for (i = 0; i < pScrn->numEntities; i++) {
358*4882a593Smuzhiyun if (pScrn->entityList[i] == entityIndex) {
359*4882a593Smuzhiyun pScrn->entityInstanceList[i] = instance;
360*4882a593Smuzhiyun break;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /*
366*4882a593Smuzhiyun * XXX This needs to be updated for the case where a single entity may have
367*4882a593Smuzhiyun * instances associated with more than one screen.
368*4882a593Smuzhiyun */
369*4882a593Smuzhiyun ScrnInfoPtr
xf86FindScreenForEntity(int entityIndex)370*4882a593Smuzhiyun xf86FindScreenForEntity(int entityIndex)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun int i, j;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun if (entityIndex == -1)
375*4882a593Smuzhiyun return NULL;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun if (xf86Screens) {
378*4882a593Smuzhiyun for (i = 0; i < xf86NumScreens; i++) {
379*4882a593Smuzhiyun for (j = 0; j < xf86Screens[i]->numEntities; j++) {
380*4882a593Smuzhiyun if (xf86Screens[i]->entityList[j] == entityIndex)
381*4882a593Smuzhiyun return xf86Screens[i];
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun return NULL;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun void
xf86RemoveEntityFromScreen(ScrnInfoPtr pScrn,int entityIndex)389*4882a593Smuzhiyun xf86RemoveEntityFromScreen(ScrnInfoPtr pScrn, int entityIndex)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun int i;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun for (i = 0; i < pScrn->numEntities; i++) {
394*4882a593Smuzhiyun if (pScrn->entityList[i] == entityIndex) {
395*4882a593Smuzhiyun for (i++; i < pScrn->numEntities; i++)
396*4882a593Smuzhiyun pScrn->entityList[i - 1] = pScrn->entityList[i];
397*4882a593Smuzhiyun pScrn->numEntities--;
398*4882a593Smuzhiyun xf86Entities[entityIndex]->inUse = FALSE;
399*4882a593Smuzhiyun break;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /*
405*4882a593Smuzhiyun * xf86ClearEntityListForScreen() - called when a screen is deleted
406*4882a593Smuzhiyun * to mark it's entities unused. Called by xf86DeleteScreen().
407*4882a593Smuzhiyun */
408*4882a593Smuzhiyun void
xf86ClearEntityListForScreen(ScrnInfoPtr pScrn)409*4882a593Smuzhiyun xf86ClearEntityListForScreen(ScrnInfoPtr pScrn)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun int i, entityIndex;
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun if (pScrn->entityList == NULL || pScrn->numEntities == 0)
414*4882a593Smuzhiyun return;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun for (i = 0; i < pScrn->numEntities; i++) {
417*4882a593Smuzhiyun entityIndex = pScrn->entityList[i];
418*4882a593Smuzhiyun xf86Entities[entityIndex]->inUse = FALSE;
419*4882a593Smuzhiyun /* disable resource: call the disable function */
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun free(pScrn->entityList);
422*4882a593Smuzhiyun free(pScrn->entityInstanceList);
423*4882a593Smuzhiyun pScrn->entityList = NULL;
424*4882a593Smuzhiyun pScrn->entityInstanceList = NULL;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /*
428*4882a593Smuzhiyun * Add an extra device section (GDevPtr) to an entity.
429*4882a593Smuzhiyun */
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun void
xf86AddDevToEntity(int entityIndex,GDevPtr dev)432*4882a593Smuzhiyun xf86AddDevToEntity(int entityIndex, GDevPtr dev)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun EntityPtr pEnt;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun if (entityIndex >= xf86NumEntities)
437*4882a593Smuzhiyun return;
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun pEnt = xf86Entities[entityIndex];
440*4882a593Smuzhiyun pEnt->numInstances++;
441*4882a593Smuzhiyun pEnt->devices = xnfreallocarray(pEnt->devices,
442*4882a593Smuzhiyun pEnt->numInstances, sizeof(GDevPtr));
443*4882a593Smuzhiyun pEnt->devices[pEnt->numInstances - 1] = dev;
444*4882a593Smuzhiyun dev->claimed = TRUE;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun void
xf86RemoveDevFromEntity(int entityIndex,GDevPtr dev)449*4882a593Smuzhiyun xf86RemoveDevFromEntity(int entityIndex, GDevPtr dev)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun EntityPtr pEnt;
452*4882a593Smuzhiyun int i, j;
453*4882a593Smuzhiyun if (entityIndex >= xf86NumEntities)
454*4882a593Smuzhiyun return;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun pEnt = xf86Entities[entityIndex];
457*4882a593Smuzhiyun for (i = 0; i < pEnt->numInstances; i++) {
458*4882a593Smuzhiyun if (pEnt->devices[i] == dev) {
459*4882a593Smuzhiyun for (j = i; j < pEnt->numInstances - 1; j++)
460*4882a593Smuzhiyun pEnt->devices[j] = pEnt->devices[j + 1];
461*4882a593Smuzhiyun break;
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun pEnt->numInstances--;
465*4882a593Smuzhiyun dev->claimed = FALSE;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun /*
468*4882a593Smuzhiyun * xf86GetEntityInfo() -- This function hands information from the
469*4882a593Smuzhiyun * EntityRec struct to the drivers. The EntityRec structure itself
470*4882a593Smuzhiyun * remains invisible to the driver.
471*4882a593Smuzhiyun */
472*4882a593Smuzhiyun EntityInfoPtr
xf86GetEntityInfo(int entityIndex)473*4882a593Smuzhiyun xf86GetEntityInfo(int entityIndex)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun EntityInfoPtr pEnt;
476*4882a593Smuzhiyun int i;
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun if (entityIndex == -1)
479*4882a593Smuzhiyun return NULL;
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun if (entityIndex >= xf86NumEntities)
482*4882a593Smuzhiyun return NULL;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun pEnt = xnfcalloc(1, sizeof(EntityInfoRec));
485*4882a593Smuzhiyun pEnt->index = entityIndex;
486*4882a593Smuzhiyun pEnt->location = xf86Entities[entityIndex]->bus;
487*4882a593Smuzhiyun pEnt->active = xf86Entities[entityIndex]->active;
488*4882a593Smuzhiyun pEnt->chipset = xf86Entities[entityIndex]->chipset;
489*4882a593Smuzhiyun pEnt->driver = xf86Entities[entityIndex]->driver;
490*4882a593Smuzhiyun if ((xf86Entities[entityIndex]->devices) &&
491*4882a593Smuzhiyun (xf86Entities[entityIndex]->devices[0])) {
492*4882a593Smuzhiyun for (i = 0; i < xf86Entities[entityIndex]->numInstances; i++)
493*4882a593Smuzhiyun if (xf86Entities[entityIndex]->devices[i]->screen == 0)
494*4882a593Smuzhiyun break;
495*4882a593Smuzhiyun pEnt->device = xf86Entities[entityIndex]->devices[i];
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun else
498*4882a593Smuzhiyun pEnt->device = NULL;
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun return pEnt;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun int
xf86GetNumEntityInstances(int entityIndex)504*4882a593Smuzhiyun xf86GetNumEntityInstances(int entityIndex)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun if (entityIndex >= xf86NumEntities)
507*4882a593Smuzhiyun return -1;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun return xf86Entities[entityIndex]->numInstances;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun GDevPtr
xf86GetDevFromEntity(int entityIndex,int instance)513*4882a593Smuzhiyun xf86GetDevFromEntity(int entityIndex, int instance)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun int i;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun /* We might not use AddDevtoEntity */
518*4882a593Smuzhiyun if ((!xf86Entities[entityIndex]->devices) ||
519*4882a593Smuzhiyun (!xf86Entities[entityIndex]->devices[0]))
520*4882a593Smuzhiyun return NULL;
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun if (entityIndex >= xf86NumEntities ||
523*4882a593Smuzhiyun instance >= xf86Entities[entityIndex]->numInstances)
524*4882a593Smuzhiyun return NULL;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun for (i = 0; i < xf86Entities[entityIndex]->numInstances; i++)
527*4882a593Smuzhiyun if (xf86Entities[entityIndex]->devices[i]->screen == instance)
528*4882a593Smuzhiyun break;
529*4882a593Smuzhiyun return xf86Entities[entityIndex]->devices[i];
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun /*
533*4882a593Smuzhiyun * xf86PostProbe() -- Allocate all non conflicting resources
534*4882a593Smuzhiyun * This function gets called by xf86Init().
535*4882a593Smuzhiyun */
536*4882a593Smuzhiyun void
xf86PostProbe(void)537*4882a593Smuzhiyun xf86PostProbe(void)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun if (fbSlotClaimed && (
540*4882a593Smuzhiyun #if (defined(__sparc__) || defined(__sparc)) && !defined(__OpenBSD__)
541*4882a593Smuzhiyun sbusSlotClaimed ||
542*4882a593Smuzhiyun #endif
543*4882a593Smuzhiyun #ifdef XSERVER_PLATFORM_BUS
544*4882a593Smuzhiyun platformSlotClaimed ||
545*4882a593Smuzhiyun #endif
546*4882a593Smuzhiyun #ifdef XSERVER_LIBPCIACCESS
547*4882a593Smuzhiyun pciSlotClaimed
548*4882a593Smuzhiyun #else
549*4882a593Smuzhiyun TRUE
550*4882a593Smuzhiyun #endif
551*4882a593Smuzhiyun ))
552*4882a593Smuzhiyun FatalError("Cannot run in framebuffer mode. Please specify busIDs "
553*4882a593Smuzhiyun " for all framebuffer devices\n");
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun int
xf86GetLastScrnFlag(int entityIndex)557*4882a593Smuzhiyun xf86GetLastScrnFlag(int entityIndex)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
560*4882a593Smuzhiyun return xf86Entities[entityIndex]->lastScrnFlag;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun else {
563*4882a593Smuzhiyun return -1;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun void
xf86SetLastScrnFlag(int entityIndex,int scrnIndex)568*4882a593Smuzhiyun xf86SetLastScrnFlag(int entityIndex, int scrnIndex)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
571*4882a593Smuzhiyun xf86Entities[entityIndex]->lastScrnFlag = scrnIndex;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun Bool
xf86IsEntityShared(int entityIndex)576*4882a593Smuzhiyun xf86IsEntityShared(int entityIndex)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
579*4882a593Smuzhiyun if (xf86Entities[entityIndex]->entityProp & IS_SHARED_ACCEL) {
580*4882a593Smuzhiyun return TRUE;
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun return FALSE;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun void
xf86SetEntityShared(int entityIndex)587*4882a593Smuzhiyun xf86SetEntityShared(int entityIndex)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
590*4882a593Smuzhiyun xf86Entities[entityIndex]->entityProp |= IS_SHARED_ACCEL;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun Bool
xf86IsEntitySharable(int entityIndex)595*4882a593Smuzhiyun xf86IsEntitySharable(int entityIndex)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
598*4882a593Smuzhiyun if (xf86Entities[entityIndex]->entityProp & ACCEL_IS_SHARABLE) {
599*4882a593Smuzhiyun return TRUE;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun return FALSE;
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun void
xf86SetEntitySharable(int entityIndex)606*4882a593Smuzhiyun xf86SetEntitySharable(int entityIndex)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
609*4882a593Smuzhiyun xf86Entities[entityIndex]->entityProp |= ACCEL_IS_SHARABLE;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun Bool
xf86IsPrimInitDone(int entityIndex)614*4882a593Smuzhiyun xf86IsPrimInitDone(int entityIndex)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
617*4882a593Smuzhiyun if (xf86Entities[entityIndex]->entityProp & SA_PRIM_INIT_DONE) {
618*4882a593Smuzhiyun return TRUE;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun return FALSE;
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun void
xf86SetPrimInitDone(int entityIndex)625*4882a593Smuzhiyun xf86SetPrimInitDone(int entityIndex)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
628*4882a593Smuzhiyun xf86Entities[entityIndex]->entityProp |= SA_PRIM_INIT_DONE;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun void
xf86ClearPrimInitDone(int entityIndex)633*4882a593Smuzhiyun xf86ClearPrimInitDone(int entityIndex)
634*4882a593Smuzhiyun {
635*4882a593Smuzhiyun if (entityIndex < xf86NumEntities) {
636*4882a593Smuzhiyun xf86Entities[entityIndex]->entityProp &= ~SA_PRIM_INIT_DONE;
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun /*
641*4882a593Smuzhiyun * Allocate a private in the entities.
642*4882a593Smuzhiyun */
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun int
xf86AllocateEntityPrivateIndex(void)645*4882a593Smuzhiyun xf86AllocateEntityPrivateIndex(void)
646*4882a593Smuzhiyun {
647*4882a593Smuzhiyun int idx, i;
648*4882a593Smuzhiyun EntityPtr pEnt;
649*4882a593Smuzhiyun DevUnion *nprivs;
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun idx = xf86EntityPrivateCount++;
652*4882a593Smuzhiyun for (i = 0; i < xf86NumEntities; i++) {
653*4882a593Smuzhiyun pEnt = xf86Entities[i];
654*4882a593Smuzhiyun nprivs = xnfreallocarray(pEnt->entityPrivates,
655*4882a593Smuzhiyun xf86EntityPrivateCount, sizeof(DevUnion));
656*4882a593Smuzhiyun /* Zero the new private */
657*4882a593Smuzhiyun memset(&nprivs[idx], 0, sizeof(DevUnion));
658*4882a593Smuzhiyun pEnt->entityPrivates = nprivs;
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun return idx;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun DevUnion *
xf86GetEntityPrivate(int entityIndex,int privIndex)664*4882a593Smuzhiyun xf86GetEntityPrivate(int entityIndex, int privIndex)
665*4882a593Smuzhiyun {
666*4882a593Smuzhiyun if (entityIndex >= xf86NumEntities || privIndex >= xf86EntityPrivateCount)
667*4882a593Smuzhiyun return NULL;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun return &(xf86Entities[entityIndex]->entityPrivates[privIndex]);
670*4882a593Smuzhiyun }
671