summaryrefslogtreecommitdiff
path: root/sysutils/libpciaccess/patches/patch-src_openbsd_pci_c
blob: 31dce07cb933d894bf87ee8d99c8d7f8c98c6066 (plain)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

Index: src/openbsd_pci.c
--- src/openbsd_pci.c.orig
+++ src/openbsd_pci.c
@@ -17,17 +17,23 @@
 #include "config.h"
 #endif
 
-#include <sys/param.h>
 #include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#if defined(__i386__) || defined(__amd64__)
 #include <sys/memrange.h>
+#endif
 #include <sys/mman.h>
 #include <sys/pciio.h>
+#include <sys/sysctl.h>
+#include <machine/cpu.h>
 
 #include <dev/pci/pcireg.h>
 #include <dev/pci/pcidevs.h>
 
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -36,6 +42,8 @@
 #include "pciaccess.h"
 #include "pciaccess_private.h"
 
+#define MIN(a,b) (((a)<(b))?(a):(b))
+
 /*
  * This should allow for 16 domains, which should cover everything
  * except perhaps the really big fridge-sized sparc64 server machines
@@ -190,21 +198,28 @@ static int
 pci_device_openbsd_map_range(struct pci_device *dev,
     struct pci_device_mapping *map)
 {
-	struct mem_range_desc mr;
-	struct mem_range_op mo;
 	int prot = PROT_READ;
+	off_t addr = map->base;
 
 	if (map->flags & PCI_DEV_MAP_FLAG_WRITABLE)
 		prot |= PROT_WRITE;
 
+#ifdef MEMRANGE_WC_RANGE
+	if (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)
+		addr += MEMRANGE_WC_RANGE;
+#endif
+
 	map->memory = mmap(NULL, map->size, prot, MAP_SHARED, aperturefd,
-	    map->base);
+	    addr);
 	if (map->memory == MAP_FAILED)
 		return  errno;
 #if defined(__i386__) || defined(__amd64__)
 	/* No need to set an MTRR if it's the default mode. */
 	if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
 	    (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
+		struct mem_range_desc mr;
+		struct mem_range_op mo;
+
 		mr.mr_base = map->base;
 		mr.mr_len = map->size;
 		mr.mr_flags = 0;
@@ -218,7 +233,8 @@ pci_device_openbsd_map_range(struct pci_device *dev,
 		mo.mo_arg[0] = MEMRANGE_SET_UPDATE;
 
 		if (ioctl(aperturefd, MEMRANGE_SET, &mo))
-			(void)fprintf(stderr, "mtrr set failed: %s\n",
+			(void)fprintf(stderr, "mtrr set %lx %lx failed: %s\n",
+			    (intptr_t)map->base, (intptr_t)map->size,
 			    strerror(errno));
 	}
 #endif
@@ -230,11 +246,11 @@ pci_device_openbsd_unmap_range(struct pci_device *dev,
     struct pci_device_mapping *map)
 {
 #if defined(__i386__) || defined(__amd64__)
-	struct mem_range_desc mr;
-	struct mem_range_op mo;
-
 	if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
 	    (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
+		struct mem_range_desc mr;
+		struct mem_range_op mo;
+
 		mr.mr_base = map->base;
 		mr.mr_len = map->size;
 		mr.mr_flags = MDF_UNCACHEABLE;
@@ -306,7 +322,7 @@ pci_device_openbsd_write(struct pci_device *dev, const
 			return errno;
 
 		offset += 4;
-		data = (char *)data + 4;
+		data = (const char *)data + 4;
 		size -= 4;
 		*bytes_written += 4;
 	}
@@ -416,50 +432,77 @@ pci_device_openbsd_probe(struct pci_device *device)
 	return 0;
 }
 
+static int
+pci_device_openbsd_boot_vga(struct pci_device *dev)
+{
+	struct pci_vga pv;
+	int err;
+
+	if (dev->domain != 0)
+		return 0;
+
+	pv.pv_sel.pc_bus = 0;
+	pv.pv_sel.pc_dev = 0;
+	pv.pv_sel.pc_func = 0;
+	err = ioctl(pcifd[0], PCIOCGETVGA, &pv);
+	if (err)
+		return 0;
+
+	if (dev->bus == pv.pv_sel.pc_bus &&
+	    dev->dev == pv.pv_sel.pc_dev &&
+	    dev->func == pv.pv_sel.pc_func)
+		return 1;
+
+	return 0;
+}
+
 #if defined(__i386__) || defined(__amd64__)
 #include <machine/sysarch.h>
 #include <machine/pio.h>
 #endif
 
+static struct pci_io_handle *legacy_io_handle = NULL;
+
 static struct pci_io_handle *
 pci_device_openbsd_open_legacy_io(struct pci_io_handle *ret,
     struct pci_device *dev, pciaddr_t base, pciaddr_t size)
 {
 #if defined(__i386__)
 	struct i386_iopl_args ia;
+#elif defined(__amd64__)
+	struct amd64_iopl_args ia;
+#endif
 
+	/* With X server privilege separation, i/o access is 
+	   enabled early and never disabled, allow recursive, 
+	   privilege-less calls */
+	if (legacy_io_handle != NULL) {
+		ret->base = legacy_io_handle->base;
+		ret->size = legacy_io_handle->size;
+		ret->memory = legacy_io_handle->memory;
+		ret->is_legacy = 1;
+		return ret;
+	}
+#if defined(__i386__)
 	ia.iopl = 1;
 	if (sysarch(I386_IOPL, &ia))
 		return NULL;
-
-	ret->base = base;
-	ret->size = size;
-	ret->is_legacy = 1;
-	return ret;
 #elif defined(__amd64__)
-	struct amd64_iopl_args ia;
-
 	ia.iopl = 1;
 	if (sysarch(AMD64_IOPL, &ia))
 		return NULL;
-
-	ret->base = base;
-	ret->size = size;
-	ret->is_legacy = 1;
-	return ret;
 #elif defined(PCI_MAGIC_IO_RANGE)
 	ret->memory = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
 	    aperturefd, PCI_MAGIC_IO_RANGE + base);
 	if (ret->memory == MAP_FAILED)
 		return NULL;
-
-	ret->base = base;
-	ret->size = size;
-	ret->is_legacy = 1;
-	return ret;
 #else
 	return NULL;
 #endif
+	ret->base = base;
+	ret->size = size;
+	legacy_io_handle = ret;
+	return ret;
 }
 
 static uint32_t
@@ -565,9 +608,9 @@ static const struct pci_system_methods openbsd_pci_met
 	pci_device_openbsd_write,
 	pci_fill_capabilities_generic,
 	NULL,
+	pci_device_openbsd_boot_vga,
 	NULL,
 	NULL,
-	NULL,
 	pci_device_openbsd_open_legacy_io,
 	NULL,
 	pci_device_openbsd_read32,
@@ -585,33 +628,32 @@ pci_system_openbsd_create(void)
 {
 	struct pci_device_private *device;
 	int domain, bus, dev, func, ndevs, nfuncs;
-	char path[MAXPATHLEN];
+	char path[PATH_MAX];
 	uint32_t reg;
 
 	if (ndomains > 0)
 		return 0;
 
+	pci_sys = calloc(1, sizeof(struct pci_system));
+	if (pci_sys == NULL)
+		return ENOMEM;
+
+	pci_sys->methods = &openbsd_pci_methods;
+
 	for (domain = 0; domain < sizeof(pcifd) / sizeof(pcifd[0]); domain++) {
 		snprintf(path, sizeof(path), "/dev/pci%d", domain);
 	        pcifd[domain] = open(path, O_RDWR | O_CLOEXEC);
-		if (pcifd[domain] == -1)
-			break;
+		if (pcifd[domain] == -1) {
+			pcifd[domain] = open(path, O_RDONLY | O_CLOEXEC);
+			if (pcifd[domain] == -1)
+				break;
+		}
 		ndomains++;
 	}
 
 	if (ndomains == 0)
 		return ENXIO;
 
-	pci_sys = calloc(1, sizeof(struct pci_system));
-	if (pci_sys == NULL) {
-		for (domain = 0; domain < ndomains; domain++)
-			close(pcifd[domain]);
-		ndomains = 0;
-		return ENOMEM;
-	}
-
-	pci_sys->methods = &openbsd_pci_methods;
-
 	ndevs = 0;
 	for (domain = 0; domain < ndomains; domain++) {
 		for (bus = 0; bus < 256; bus++) {
@@ -683,10 +725,6 @@ pci_system_openbsd_create(void)
 					device->base.subvendor_id = PCI_VENDOR(reg);
 					device->base.subdevice_id = PCI_PRODUCT(reg);
 
-					device->base.vgaarb_rsrc =
-					    VGA_ARB_RSRC_LEGACY_IO |
-					    VGA_ARB_RSRC_LEGACY_MEM;
-
 					device++;
 				}
 			}
@@ -715,6 +753,22 @@ pci_device_vgaarb_init(void)
 	struct pci_vga pv;
 	int err;
 
+#ifdef CPU_ALLOWAPERTURE
+	int mib[2];
+	int allowaperture;
+	size_t len;
+
+	mib[0] = CTL_MACHDEP;
+	mib[1] = CPU_ALLOWAPERTURE;
+	len = sizeof(allowaperture);
+	if (sysctl(mib, 2, &allowaperture, &len, NULL, 0) == 0 &&
+	    allowaperture == 0) {
+		/* No direct hardware access allowed. */
+		pci_sys->vga_count = 0;
+		return 0;
+	}
+#endif
+
 	pv.pv_sel.pc_bus = 0;
 	pv.pv_sel.pc_dev = 0;
 	pv.pv_sel.pc_func = 0;
@@ -731,8 +785,18 @@ pci_device_vgaarb_init(void)
 		return -1;
 	pci_sys->vga_count = 0;
 	while ((dev = pci_device_next(iter)) != NULL) {
-		if (dev->domain == 0)
+		if (dev->domain == 0) {
 			pci_sys->vga_count++;
+			pv.pv_sel.pc_bus = dev->bus;
+			pv.pv_sel.pc_dev = dev->dev;
+			pv.pv_sel.pc_func = dev->func;
+			if (ioctl(pcifd[0], PCIOCGETVGA, &pv))
+				continue;
+			if (pv.pv_decode)
+				dev->vgaarb_rsrc = 
+				    VGA_ARB_RSRC_LEGACY_IO |
+				    VGA_ARB_RSRC_LEGACY_MEM;
+		}
 	}
 	pci_iterator_destroy(iter);
 
@@ -774,13 +838,8 @@ pci_device_vgaarb_lock(void)
 	if (dev == NULL)
 		return -1;
 
-#if 0
 	if (dev->vgaarb_rsrc == 0 || pci_sys->vga_count == 1)
 		return 0;
-#else
-	if (pci_sys->vga_count == 1)
-		return 0;
-#endif
 
 	pv.pv_sel.pc_bus = dev->bus;
 	pv.pv_sel.pc_dev = dev->dev;
@@ -798,13 +857,8 @@ pci_device_vgaarb_unlock(void)
 	if (dev == NULL)
 		return -1;
 
-#if 0
 	if (dev->vgaarb_rsrc == 0 || pci_sys->vga_count == 1)
 		return 0;
-#else
-	if (pci_sys->vga_count == 1)
-		return 0;
-#endif
 
 	pv.pv_sel.pc_bus = dev->bus;
 	pv.pv_sel.pc_dev = dev->dev;