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