aboutsummaryrefslogtreecommitdiff
path: root/x11-servers/xorg-server/files/patch-freebsdPci.c
blob: 83b034fc9a3e0af03ad0164d786a2e68b9fd6310 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
--- hw/xfree86/os-support/bus/freebsdPci.c.orig	Thu Sep  6 02:48:26 2007
+++ hw/xfree86/os-support/bus/freebsdPci.c	Sat Sep 29 12:22:56 2007
@@ -109,10 +109,6 @@
 #define PCI_CPU(val)	(val)
 #endif
 
-
-#define BUS(tag) (((tag)>>16)&0xff)
-#define DFN(tag) (((tag)>>8)&0xff)
-
 static int pciFd = -1;
 
 void
@@ -133,9 +129,12 @@
 {
 	struct pci_io io;
 	int error;
-	io.pi_sel.pc_bus = BUS(tag);
-	io.pi_sel.pc_dev = DFN(tag) >> 3;
-	io.pi_sel.pc_func = DFN(tag) & 7;
+#ifndef INCLUDE_XF86_NO_DOMAIN
+	io.pi_sel.pc_domain = PCI_DOM_FROM_TAG(tag);
+#endif
+	io.pi_sel.pc_bus = PCI_BUS_NO_DOMAIN(PCI_BUS_FROM_TAG(tag));
+	io.pi_sel.pc_dev = PCI_DEV_FROM_TAG(tag);
+	io.pi_sel.pc_func = PCI_FUNC_FROM_TAG(tag);
 	io.pi_reg = off;
 	io.pi_width = 4;
 	error = ioctl(pciFd, PCIOCREAD, &io);
@@ -148,9 +147,12 @@
 freebsdPciCfgWrite(PCITAG tag, int off, CARD32 val)
 {
 	struct pci_io io;
-	io.pi_sel.pc_bus = BUS(tag);
-	io.pi_sel.pc_dev = DFN(tag) >> 3;
-	io.pi_sel.pc_func = DFN(tag) & 7;
+#ifndef INCLUDE_XF86_NO_DOMAIN
+	io.pi_sel.pc_domain = PCI_DOM_FROM_TAG(tag);
+#endif
+	io.pi_sel.pc_bus = PCI_BUS_NO_DOMAIN(PCI_BUS_FROM_TAG(tag));
+	io.pi_sel.pc_dev = PCI_DEV_FROM_TAG(tag);
+	io.pi_sel.pc_func = PCI_FUNC_FROM_TAG(tag);
 	io.pi_reg = off;
 	io.pi_width = 4;
 	io.pi_data = PCI_CPU(val);
@@ -164,3 +166,260 @@
 	val = (val & ~mask) | (bits & mask);
 	freebsdPciCfgWrite(tag, off, val);
 }
+
+#ifndef INCLUDE_XF86_NO_DOMAIN
+
+/* This probably shouldn't be FreeBSD-specific. */
+static pciConfigPtr
+xf86GetPciHostConfigFromTag(PCITAG Tag)
+{
+	int bus = PCI_BUS_FROM_TAG(Tag);
+	pciBusInfo_t *pBusInfo;
+
+	while ((bus < pciNumBuses) && (pBusInfo = pciBusInfo[bus])) {
+		if (bus == pBusInfo->primary_bus)
+			return pBusInfo->bridge;
+		bus = pBusInfo->primary_bus;
+	}
+
+	return NULL;	/* Bad data */
+}
+
+/*
+ * This is ugly, but until I can extract this information from the kernel,
+ * it'll have to do. The default I/O space size is 64K, and 4G for memory.
+ * Anything else needs to go in this table. (PowerPC folk take note.)
+ *
+ * Please keep this table in ascending vendor/device order.
+ */
+static const struct pciSizes {
+	unsigned short vendor, device;
+	unsigned long io_size, mem_size;
+} pciControllerSizes[] = {
+	{
+		PCI_VENDOR_SUN, PCI_CHIP_PSYCHO,
+		1U << 16, 1U << 31
+	},
+	{
+		PCI_VENDOR_SUN, PCI_CHIP_SCHIZO,
+		1U << 24, 1U << 31	/* ??? */
+	},
+	{
+		PCI_VENDOR_SUN, PCI_CHIP_SABRE,
+		1U << 24, (unsigned long)(1ULL << 32)
+	},
+	{
+		PCI_VENDOR_SUN, PCI_CHIP_HUMMINGBIRD,
+		1U << 24, (unsigned long)(1ULL << 32)
+	}
+};
+#define NUM_SIZES (sizeof(pciControllerSizes) / sizeof(pciControllerSizes[0]))
+
+static const struct pciSizes *
+freebsdGetSizesStruct(PCITAG Tag)
+{
+	static const struct pciSizes default_size = {
+		0, 0, 1U << 16, (unsigned long)(1ULL << 32)
+	};
+	pciConfigPtr pPCI;
+	int i;
+
+	/* Find host bridge */
+	if ((pPCI = xf86GetPciHostConfigFromTag(Tag))) {
+		/* Look up vendor/device */
+		for (i = 0; i < NUM_SIZES; i++) {
+			if ((pPCI->pci_vendor ==
+			    pciControllerSizes[i].vendor) &&
+			    (pPCI->pci_device ==
+			    pciControllerSizes[i].device)) {
+				return & pciControllerSizes[i];
+			}
+		}
+	}
+
+	/* Default to 64KB I/O and 4GB memory. */
+	return & default_size;
+}
+
+static __inline__ unsigned long
+freebsdGetIOSize(PCITAG Tag)
+{
+	const struct pciSizes * const sizes = freebsdGetSizesStruct(Tag);
+	return sizes->io_size;
+}
+
+static __inline__ void
+freebsdGetSizes(PCITAG Tag, unsigned long *io_size, unsigned long *mem_size)
+{
+	const struct pciSizes * const sizes = freebsdGetSizesStruct(Tag);
+
+	*io_size = sizes->io_size;
+	*mem_size = sizes->mem_size;
+}
+
+_X_EXPORT int
+xf86GetPciDomain(PCITAG Tag)
+{
+	return PCI_DOM_FROM_TAG(Tag);
+}
+
+_X_EXPORT pointer
+xf86MapDomainMemory(int ScreenNum, int Flags, PCITAG Tag, ADDRESS Base,
+    unsigned long Size)
+{
+	return xf86MapVidMem(ScreenNum, Flags, Base, Size);
+}
+
+_X_EXPORT IOADDRESS
+xf86MapDomainIO(int ScreenNum, int Flags, PCITAG Tag, IOADDRESS Base,
+    unsigned long Size)
+{
+	return Base;
+}
+
+_X_EXPORT int
+xf86ReadDomainMemory(PCITAG Tag, ADDRESS Base, int Len, unsigned char *Buf)
+{
+	int ret, length, rlength;
+
+	/* Read in 64kB chunks. */
+	ret = 0;
+	while ((length = Len) > 0) {
+		if (length > 0x010000) length = 0x010000;
+		rlength = xf86ReadBIOS(Base, 0, Buf, length);
+		if (rlength < 0) {
+			ret = rlength;
+			break;
+		}
+		ret += rlength;
+		if (rlength < length) break;
+		Base += rlength;
+		Buf += rlength;
+		Len -= rlength;
+	}
+
+	return ret;
+}
+
+resPtr
+xf86BusAccWindowsFromOS(void)
+{
+	pciConfigPtr *ppPCI, pPCI;
+	resPtr pRes = NULL;
+	resRange range;
+	unsigned long io_size, mem_size;
+	int domain;
+
+	if ((ppPCI = xf86scanpci(0))) {
+		for (; (pPCI = *ppPCI); ppPCI++) {
+			if ((pPCI->pci_base_class != PCI_CLASS_BRIDGE) ||
+			    (pPCI->pci_sub_class != PCI_SUBCLASS_BRIDGE_HOST))
+				continue;
+
+			domain = xf86GetPciDomain(pPCI->tag);
+			freebsdGetSizes(pPCI->tag, &io_size, &mem_size);
+
+			RANGE(range, 0, (ADDRESS)(mem_size - 1),
+			    RANGE_TYPE(ResExcMemBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+
+			RANGE(range, 0, (IOADDRESS)(io_size - 1),
+			    RANGE_TYPE(ResExcIoBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+
+			if (domain <= 0)
+				break;
+		}
+	}
+
+	return pRes;
+}
+
+resPtr
+xf86PciBusAccWindowsFromOS(void)
+{
+	pciConfigPtr *ppPCI, pPCI;
+	resPtr pRes = NULL;
+	resRange range;
+	unsigned long io_size, mem_size;
+	int domain;
+
+	if ((ppPCI = xf86scanpci(0))) {
+		for (; (pPCI = *ppPCI); ppPCI++) {
+			if ((pPCI->pci_base_class != PCI_CLASS_BRIDGE) ||
+			    (pPCI->pci_sub_class != PCI_SUBCLASS_BRIDGE_HOST))
+				continue;
+
+			domain = xf86GetPciDomain(pPCI->tag);
+			freebsdGetSizes(pPCI->tag, &io_size, &mem_size);
+
+			RANGE(range, 0, (ADDRESS)(mem_size - 1),
+			    RANGE_TYPE(ResExcMemBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+
+			RANGE(range, 0, (IOADDRESS)(io_size - 1),
+			    RANGE_TYPE(ResExcIoBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+
+			if (domain <= 0)
+				break;
+		}
+	}
+
+	return pRes;
+}
+
+resPtr
+xf86AccResFromOS(resPtr pRes)
+{
+	pciConfigPtr *ppPCI, pPCI;
+	resRange range;
+	unsigned long io_size, mem_size;
+	int domain;
+
+	if ((ppPCI = xf86scanpci(0))) {
+		for (; (pPCI = *ppPCI); ppPCI++) {
+			if ((pPCI->pci_base_class != PCI_CLASS_BRIDGE) ||
+			    (pPCI->pci_sub_class != PCI_SUBCLASS_BRIDGE_HOST))
+				continue;
+
+			domain = xf86GetPciDomain(pPCI->tag);
+			freebsdGetSizes(pPCI->tag, &io_size, &mem_size);
+
+			/*
+			 * At minimum, the top and bottom resources must be
+			 * claimed, so that resources that are (or appear to
+			 * be) unallocated can be relocated.
+			 */
+			RANGE(range, 0x00000000u, 0x0009ffffu,
+			    RANGE_TYPE(ResExcMemBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+			RANGE(range, 0x000c0000u, 0x000effffu,
+			    RANGE_TYPE(ResExcMemBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+			RANGE(range, 0x000f0000u, 0x000fffffu,
+			    RANGE_TYPE(ResExcMemBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+
+			RANGE(range, (ADDRESS)(mem_size - 1),
+			    (ADDRESS)(mem_size - 1),
+			    RANGE_TYPE(ResExcMemBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+
+			RANGE(range, 0x00000000u, 0x00000000u,
+			    RANGE_TYPE(ResExcIoBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+			RANGE(range, (IOADDRESS)(io_size - 1),
+			    (IOADDRESS)(io_size - 1),
+			    RANGE_TYPE(ResExcIoBlock, domain));
+			pRes = xf86AddResToList(pRes, &range, -1);
+
+			if (domain <= 0)
+				break;
+		}
+	}
+
+	return pRes;
+}
+
+#endif /* !INCLUDE_XF86_NO_DOMAIN */