OSDN Git Service

Fix build on NetBSD-4.
[android-x86/external-libpciaccess.git] / src / netbsd_pci.c
1 /*
2  * Copyright (c) 2008 Juan Romero Pardines
3  * Copyright (c) 2008 Mark Kettenis
4  * Copyright (c) 2009 Michael Lorenz
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include <sys/param.h>
20 #include <sys/ioctl.h>
21 #include <sys/mman.h>
22 #include <sys/types.h>
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #ifdef HAVE_MTRR
29 #include <machine/sysarch.h>
30 #include <machine/mtrr.h>
31 #ifdef _X86_SYSARCH_L
32 /* NetBSD 5.x and newer */
33 #define netbsd_set_mtrr(mr, num)        _X86_SYSARCH_L(set_mtrr)(mr, num)
34 #else
35 /* NetBSD 4.x and older */
36 #ifdef __i386__
37 #define netbsd_set_mtrr(mr, num)        i386_set_mtrr((mr), (num))
38 #endif
39 #ifdef __amd64__
40 #define netbsd_set_mtrr(mr, num)        x86_64_set_mtrr((mr), (num))
41 #endif
42 #endif
43 #endif
44
45 #include <dev/pci/pcidevs.h>
46 #include <dev/pci/pciio.h>
47 #include <dev/pci/pcireg.h>
48
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56
57 #include <pci.h>
58 #include <dev/wscons/wsconsio.h>
59
60 #include "pciaccess.h"
61 #include "pciaccess_private.h"
62
63 typedef struct _pcibus {
64         int fd;         /* /dev/pci* */
65         int num;        /* bus number */
66         int maxdevs;    /* maximum number of devices */
67 } PciBus;
68
69 static PciBus buses[32];        /* indexed by pci_device.domain */
70 static int nbuses = 0;          /* number of buses found */
71
72 /*
73  * NetBSD's userland has a /dev/pci* entry for each bus but userland has no way
74  * to tell if a bus is a subordinate of another one or if it's on a different
75  * host bridge. On some architectures ( macppc for example ) all root buses have
76  * bus number 0 but on sparc64 for example the two roots in an Ultra60 have
77  * different bus numbers - one is 0 and the other 128.
78  * With each /dev/pci* we can map everything on the same root and we can also
79  * see all devices on the same root, trying to do that causes problems though:
80  * - since we can't tell which /dev/pci* is a subordinate we would find some
81  *   devices more than once
82  * - we would have to guess subordinate bus numbers which is a waste of time
83  *   since we can ask each /dev/pci* for its bus number so we can scan only the
84  *   buses we know exist, not all 256 which may exist in each domain.
85  * - some bus_space_mmap() methods may limit mappings to address ranges which
86  *   belong to known devices on that bus only.
87  * Each host bridge may or may not have its own IO range, to avoid guesswork
88  * here each /dev/pci* will let userland map its appropriate IO range at
89  * PCI_MAGIC_IO_RANGE if defined in <machine/param.h>
90  * With all this we should be able to use any PCI graphics device on any PCI
91  * bus on any architecture as long as Xorg has a driver, without allowing
92  * arbitrary mappings via /dev/mem and without userland having to know or care
93  * about translating bus addresses to physical addresses or the other way
94  * around.
95  */
96
97 static int
98 pci_read(int domain, int bus, int dev, int func, uint32_t reg, uint32_t *val)
99 {
100         uint32_t rval;
101
102         if ((domain < 0) || (domain > nbuses))
103                 return -1;
104
105         if (pcibus_conf_read(buses[domain].fd, (unsigned int)bus,
106             (unsigned int)dev, (unsigned int)func, reg, &rval) == -1)
107                 return (-1);
108
109         *val = rval;
110
111         return 0;
112 }
113
114 static int
115 pci_write(int domain, int bus, int dev, int func, uint32_t reg, uint32_t val)
116 {
117
118         if ((domain < 0) || (domain > nbuses))
119                 return -1;
120
121         return pcibus_conf_write(buses[domain].fd, (unsigned int)bus,
122             (unsigned int)dev, (unsigned int)func, reg, val);
123 }
124
125 static int
126 pci_nfuncs(int domain, int bus, int dev)
127 {
128         uint32_t hdr;
129
130         if ((domain < 0) || (domain > nbuses))
131                 return -1;
132
133         if (pci_read(domain, bus, dev, 0, PCI_BHLC_REG, &hdr) != 0)
134                 return -1;
135
136         return (PCI_HDRTYPE_MULTIFN(hdr) ? 8 : 1);
137 }
138
139 /*ARGSUSED*/
140 static int
141 pci_device_netbsd_map_range(struct pci_device *dev,
142     struct pci_device_mapping *map)
143 {
144 #ifdef HAVE_MTRR
145         struct mtrr m;
146         int n = 1;
147 #endif
148         int prot, ret = 0;
149
150         prot = PROT_READ;
151
152         if (map->flags & PCI_DEV_MAP_FLAG_WRITABLE)
153                 prot |= PROT_WRITE;
154         map->memory = mmap(NULL, (size_t)map->size, prot, MAP_SHARED,
155             buses[dev->domain].fd, (off_t)map->base);
156         if (map->memory == MAP_FAILED)
157                 return errno;
158
159 #ifdef HAVE_MTRR
160         memset(&m, 0, sizeof(m));
161
162         /* No need to set an MTRR if it's the default mode. */
163         if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
164             (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
165                 m.base = map->base;
166                 m.flags = MTRR_VALID | MTRR_PRIVATE;
167                 m.len = map->size;
168                 m.owner = getpid();
169                 if (map->flags & PCI_DEV_MAP_FLAG_CACHABLE)
170                         m.type = MTRR_TYPE_WB;
171                 if (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)
172                         m.type = MTRR_TYPE_WC;
173
174                 if ((netbsd_set_mtrr(&m, &n)) == -1) {
175                         fprintf(stderr, "mtrr set failed: %s\n",
176                             strerror(errno));
177                 }
178         }
179 #endif
180
181         return ret;
182 }
183
184 static int
185 pci_device_netbsd_unmap_range(struct pci_device *dev,
186     struct pci_device_mapping *map)
187 {
188 #ifdef HAVE_MTRR
189         struct mtrr m;
190         int n = 1;
191
192         memset(&m, 0, sizeof(m));
193
194         if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
195             (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
196                 m.base = map->base;
197                 m.flags = 0;
198                 m.len = map->size;
199                 m.type = MTRR_TYPE_UC;
200                 (void)netbsd_set_mtrr(&m, &n);
201         }
202 #endif
203
204         return pci_device_generic_unmap_range(dev, map);
205 }
206
207 static int
208 pci_device_netbsd_read(struct pci_device *dev, void *data,
209     pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read)
210 {
211         u_int reg, rval;
212
213         *bytes_read = 0;
214         while (size > 0) {
215                 size_t toread = MIN(size, 4 - (offset & 0x3));
216
217                 reg = (u_int)(offset & ~0x3);
218
219                 if ((pcibus_conf_read(buses[dev->domain].fd,
220                     (unsigned int)dev->bus, (unsigned int)dev->dev,
221                     (unsigned int)dev->func, reg, &rval)) == -1)
222                         return errno;
223
224                 rval = htole32(rval);
225                 rval >>= ((offset & 0x3) * 8);
226
227                 memcpy(data, &rval, toread);
228
229                 offset += toread;
230                 data = (char *)data + toread;
231                 size -= toread;
232                 *bytes_read += toread;
233         }
234
235         return 0;
236 }
237
238 static int
239 pci_device_netbsd_write(struct pci_device *dev, const void *data,
240     pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written)
241 {
242         u_int reg, val;
243
244         if ((offset % 4) != 0 || (size % 4) != 0)
245                 return EINVAL;
246
247         *bytes_written = 0;
248         while (size > 0) {
249                 reg = (u_int)offset;
250                 memcpy(&val, data, 4);
251
252                 if ((pcibus_conf_write(buses[dev->domain].fd,
253                     (unsigned int)dev->bus, (unsigned int)dev->dev,
254                     (unsigned int)dev->func, reg, val)) == -1)
255                         return errno;
256
257                 offset += 4;
258                 data = (const char *)data + 4;
259                 size -= 4;
260                 *bytes_written += 4;
261         }
262
263         return 0;
264 }
265
266 #if defined(WSDISPLAYIO_GET_BUSID)
267 static int
268 pci_device_netbsd_boot_vga(struct pci_device *dev)
269 {
270         int ret;
271         struct wsdisplayio_bus_id busid;
272         int fd;
273
274         fd = open("/dev/ttyE0", O_RDONLY);
275         if (fd == -1) {
276                 fprintf(stderr, "failed to open /dev/ttyE0: %s\n",
277                     strerror(errno));
278                 return 0;
279         }
280
281         ret = ioctl(fd, WSDISPLAYIO_GET_BUSID, &busid);
282         close(fd);
283         if (ret == -1) {
284                 fprintf(stderr, "ioctl WSDISPLAYIO_GET_BUSID failed: %s\n",
285                     strerror(errno));
286                 return 0;
287         }
288
289         if (busid.bus_type != WSDISPLAYIO_BUS_PCI)
290                 return 0;
291
292         if (busid.ubus.pci.domain != dev->domain)
293                 return 0;
294         if (busid.ubus.pci.bus != dev->bus)
295                 return 0;
296         if (busid.ubus.pci.device != dev->dev)
297                 return 0;
298         if (busid.ubus.pci.function != dev->func)
299                 return 0;
300
301         return 1;
302 }
303 #endif
304
305 static void
306 pci_system_netbsd_destroy(void)
307 {
308         int i;
309
310         for (i = 0; i < nbuses; i++) {
311                 close(buses[i].fd);
312         }
313         free(pci_sys);
314         pci_sys = NULL;
315 }
316
317 static int
318 pci_device_netbsd_probe(struct pci_device *device)
319 {
320         struct pci_device_private *priv =
321             (struct pci_device_private *)(void *)device;
322         struct pci_mem_region *region;
323         uint64_t reg64, size64;
324         uint32_t bar, reg, size;
325         int bus, dev, func, err, domain;
326
327         domain = device->domain;
328         bus = device->bus;
329         dev = device->dev;
330         func = device->func;
331
332         /* Enable the device if necessary */
333         err = pci_read(domain, bus, dev, func, PCI_COMMAND_STATUS_REG, &reg);
334         if (err)
335                 return err;
336         if ((reg & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) !=
337             (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
338                 reg |= PCI_COMMAND_IO_ENABLE |
339                        PCI_COMMAND_MEM_ENABLE |
340                        PCI_COMMAND_MASTER_ENABLE;
341                 err = pci_write(domain, bus, dev, func, PCI_COMMAND_STATUS_REG,
342                                 reg);
343                 if (err)
344                         return err;
345         }
346
347         err = pci_read(domain, bus, dev, func, PCI_BHLC_REG, &reg);
348         if (err)
349                 return err;
350
351         priv->header_type = PCI_HDRTYPE_TYPE(reg);
352         if (priv->header_type != 0)
353                 return 0;
354
355         region = device->regions;
356         for (bar = PCI_MAPREG_START; bar < PCI_MAPREG_END;
357              bar += sizeof(uint32_t), region++) {
358                 err = pci_read(domain, bus, dev, func, bar, &reg);
359                 if (err)
360                         return err;
361
362                 /* Probe the size of the region. */
363                 err = pci_write(domain, bus, dev, func, bar, (unsigned int)~0);
364                 if (err)
365                         return err;
366                 pci_read(domain, bus, dev, func, bar, &size);
367                 pci_write(domain, bus, dev, func, bar, reg);
368
369                 if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) {
370                         region->is_IO = 1;
371                         region->base_addr = PCI_MAPREG_IO_ADDR(reg);
372                         region->size = PCI_MAPREG_IO_SIZE(size);
373                 } else {
374                         if (PCI_MAPREG_MEM_PREFETCHABLE(reg))
375                                 region->is_prefetchable = 1;
376                         switch(PCI_MAPREG_MEM_TYPE(reg)) {
377                         case PCI_MAPREG_MEM_TYPE_32BIT:
378                         case PCI_MAPREG_MEM_TYPE_32BIT_1M:
379                                 region->base_addr = PCI_MAPREG_MEM_ADDR(reg);
380                                 region->size = PCI_MAPREG_MEM_SIZE(size);
381                                 break;
382                         case PCI_MAPREG_MEM_TYPE_64BIT:
383                                 region->is_64 = 1;
384
385                                 reg64 = reg;
386                                 size64 = size;
387
388                                 bar += sizeof(uint32_t);
389
390                                 err = pci_read(domain, bus, dev, func, bar, &reg);
391                                 if (err)
392                                         return err;
393                                 reg64 |= (uint64_t)reg << 32;
394
395                                 err = pci_write(domain, bus, dev, func, bar,
396                                     (unsigned int)~0);
397                                 if (err)
398                                         return err;
399                                 pci_read(domain, bus, dev, func, bar, &size);
400                                 pci_write(domain, bus, dev, func, bar,
401                                     (unsigned int)(reg64 >> 32));
402                                 size64 |= (uint64_t)size << 32;
403
404                                 region->base_addr =
405                                     (unsigned long)PCI_MAPREG_MEM64_ADDR(reg64);
406                                 region->size =
407                                     (unsigned long)PCI_MAPREG_MEM64_SIZE(size64);
408                                 region++;
409                                 break;
410                         }
411                 }
412         }
413
414         /* Probe expansion ROM if present */
415         err = pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &reg);
416         if (err)
417                 return err;
418         if (reg != 0) {
419                 err = pci_write(domain, bus, dev, func, PCI_MAPREG_ROM,
420                     (uint32_t)(~PCI_MAPREG_ROM_ENABLE));
421                 if (err)
422                         return err;
423                 pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &size);
424                 pci_write(domain, bus, dev, func, PCI_MAPREG_ROM, reg);
425                 if ((reg & PCI_MAPREG_MEM_ADDR_MASK) != 0) {
426                         priv->rom_base = reg & PCI_MAPREG_MEM_ADDR_MASK;
427                         device->rom_size = -(size & PCI_MAPREG_MEM_ADDR_MASK);
428                 }
429         }
430
431         return 0;
432 }
433
434 /**
435  * Read a VGA rom using the 0xc0000 mapping.
436  *
437  * This function should be extended to handle access through PCI resources,
438  * which should be more reliable when available.
439  */
440 static int
441 pci_device_netbsd_read_rom(struct pci_device *dev, void *buffer)
442 {
443     struct pci_device_private *priv = (struct pci_device_private *)(void *)dev;
444     void *bios;
445     pciaddr_t rom_base;
446     size_t rom_size;
447     uint32_t bios_val, command_val;
448     int pci_rom;
449
450     if (((priv->base.device_class >> 16) & 0xff) != PCI_CLASS_DISPLAY ||
451         ((priv->base.device_class >> 8) & 0xff) != PCI_SUBCLASS_DISPLAY_VGA)
452         return ENOSYS;
453
454     if (priv->rom_base == 0) {
455 #if defined(__amd64__) || defined(__i386__)
456         /*
457          * We need a way to detect when this isn't the console and reject
458          * this request outright.
459          */
460         rom_base = 0xc0000;
461         rom_size = 0x10000;
462         pci_rom = 0;
463 #else
464         return ENOSYS;
465 #endif
466     } else {
467         rom_base = priv->rom_base;
468         rom_size = dev->rom_size;
469         pci_rom = 1;
470         if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus,
471             (unsigned int)dev->dev, (unsigned int)dev->func,
472             PCI_COMMAND_STATUS_REG, &command_val)) == -1)
473             return errno;
474         if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) {
475             if ((pcibus_conf_write(buses[dev->domain].fd,
476                 (unsigned int)dev->bus, (unsigned int)dev->dev,
477                 (unsigned int)dev->func, PCI_COMMAND_STATUS_REG,
478                 command_val | PCI_COMMAND_MEM_ENABLE)) == -1)
479                 return errno;
480         }
481         if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus,
482             (unsigned int)dev->dev, (unsigned int)dev->func,
483             PCI_MAPREG_ROM, &bios_val)) == -1)
484             return errno;
485         if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) {
486             if ((pcibus_conf_write(buses[dev->domain].fd,
487                 (unsigned int)dev->bus,
488                 (unsigned int)dev->dev, (unsigned int)dev->func,
489                 PCI_MAPREG_ROM, bios_val | PCI_MAPREG_ROM_ENABLE)) == -1)
490                 return errno;
491         }
492     }
493
494     fprintf(stderr, "Using rom_base = 0x%lx 0x%lx (pci_rom=%d)\n",
495         (long)rom_base, (long)rom_size, pci_rom);
496
497     bios = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, buses[dev->domain].fd,
498         (off_t)rom_base);
499     if (bios == MAP_FAILED) {
500         int serrno = errno;
501         return serrno;
502     }
503
504     memcpy(buffer, bios, rom_size);
505
506     munmap(bios, rom_size);
507
508     if (pci_rom) {
509         if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) {
510             if ((pcibus_conf_write(buses[dev->domain].fd,
511                 (unsigned int)dev->bus,
512                 (unsigned int)dev->dev, (unsigned int)dev->func,
513                 PCI_COMMAND_STATUS_REG, command_val)) == -1)
514                 return errno;
515         }
516         if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) {
517             if ((pcibus_conf_write(buses[dev->domain].fd,
518                 (unsigned int)dev->bus,
519                 (unsigned int)dev->dev, (unsigned int)dev->func,
520                 PCI_MAPREG_ROM, bios_val)) == -1)
521                 return errno;
522         }
523     }
524
525     return 0;
526 }
527
528 static const struct pci_system_methods netbsd_pci_methods = {
529         .destroy = pci_system_netbsd_destroy,
530         .destroy_device = NULL,
531         .read_rom = pci_device_netbsd_read_rom,
532         .probe = pci_device_netbsd_probe,
533         .map_range = pci_device_netbsd_map_range,
534         .unmap_range = pci_device_netbsd_unmap_range,
535         .read = pci_device_netbsd_read,
536         .write = pci_device_netbsd_write,
537         .fill_capabilities = pci_fill_capabilities_generic,
538 #if defined(WSDISPLAYIO_GET_BUSID)
539         .boot_vga = pci_device_netbsd_boot_vga,
540 #else
541         .boot_vga = NULL,
542 #endif
543 };
544
545 int
546 pci_system_netbsd_create(void)
547 {
548         struct pci_device_private *device;
549         int bus, dev, func, ndevs, nfuncs, domain, pcifd;
550         uint32_t reg;
551         char netbsd_devname[32];
552         struct pciio_businfo businfo;
553
554         pci_sys = calloc(1, sizeof(struct pci_system));
555
556         pci_sys->methods = &netbsd_pci_methods;
557
558         ndevs = 0;
559         nbuses = 0;
560         snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses);
561         pcifd = open(netbsd_devname, O_RDWR | O_CLOEXEC);
562         while (pcifd > 0) {
563                 ioctl(pcifd, PCI_IOC_BUSINFO, &businfo);
564                 buses[nbuses].fd = pcifd;
565                 buses[nbuses].num = bus = businfo.busno;
566                 buses[nbuses].maxdevs = businfo.maxdevs;
567                 domain = nbuses;
568                 nbuses++;
569                 for (dev = 0; dev < businfo.maxdevs; dev++) {
570                         nfuncs = pci_nfuncs(domain, bus, dev);
571                         for (func = 0; func < nfuncs; func++) {
572                                 if (pci_read(domain, bus, dev, func, PCI_ID_REG,
573                                     &reg) != 0)
574                                         continue;
575                                 if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
576                                     PCI_VENDOR(reg) == 0)
577                                         continue;
578
579                                 ndevs++;
580                         }
581                 }
582                 snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses);
583                 pcifd = open(netbsd_devname, O_RDWR);
584         }
585
586         pci_sys->num_devices = ndevs;
587         pci_sys->devices = calloc(ndevs, sizeof(struct pci_device_private));
588         if (pci_sys->devices == NULL) {
589                 int i;
590
591                 for (i = 0; i < nbuses; i++)
592                         close(buses[i].fd);
593                 free(pci_sys);
594                 return ENOMEM;
595         }
596
597         device = pci_sys->devices;
598         for (domain = 0; domain < nbuses; domain++) {
599                 bus = buses[domain].num;
600                 for (dev = 0; dev < buses[domain].maxdevs; dev++) {
601                         nfuncs = pci_nfuncs(domain, bus, dev);
602                         for (func = 0; func < nfuncs; func++) {
603                                 if (pci_read(domain, bus, dev, func,
604                                     PCI_ID_REG, &reg) != 0)
605                                         continue;
606                                 if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
607                                     PCI_VENDOR(reg) == 0)
608                                         continue;
609
610                                 device->base.domain = domain;
611                                 device->base.bus = bus;
612                                 device->base.dev = dev;
613                                 device->base.func = func;
614                                 device->base.vendor_id = PCI_VENDOR(reg);
615                                 device->base.device_id = PCI_PRODUCT(reg);
616
617                                 if (pci_read(domain, bus, dev, func,
618                                     PCI_CLASS_REG, &reg) != 0)
619                                         continue;
620
621                                 device->base.device_class =
622                                     PCI_INTERFACE(reg) | PCI_CLASS(reg) << 16 |
623                                     PCI_SUBCLASS(reg) << 8;
624                                 device->base.revision = PCI_REVISION(reg);
625
626                                 if (pci_read(domain, bus, dev, func,
627                                     PCI_SUBSYS_ID_REG, &reg) != 0)
628                                         continue;
629
630                                 device->base.subvendor_id = PCI_VENDOR(reg);
631                                 device->base.subdevice_id = PCI_PRODUCT(reg);
632
633                                 device++;
634                         }
635                 }
636         }
637
638         return 0;
639 }