1 /* 2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 * 28 */ 29 30 #include "opt_bus.h" 31 32 #include "opt_simos.h" 33 #include "opt_compat_oldpci.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/fcntl.h> 40 #include <sys/conf.h> 41 #include <sys/kernel.h> 42 #include <sys/queue.h> 43 #include <sys/types.h> 44 45 #include <vm/vm.h> 46 #include <vm/pmap.h> 47 #include <vm/vm_extern.h> 48 49 #include <sys/bus.h> 50 #include <machine/bus.h> 51 #include <sys/rman.h> 52 #include <machine/resource.h> 53 #include <machine/md_var.h> /* For the Alpha */ 54 55 #include <pci/pcireg.h> 56 #include <pci/pcivar.h> 57 #include <sys/pciio.h> 58 59 #ifdef __alpha__ 60 #include <machine/rpb.h> 61 #endif 62 63 #ifdef APIC_IO 64 #include <machine/smp.h> 65 #endif /* APIC_IO */ 66 67 struct pci_quirk { 68 u_int32_t devid; /* Vendor/device of the card */ 69 int type; 70 #define PCI_QUIRK_MAP_REG 1 /* PCI map register in wierd place */ 71 int arg1; 72 int arg2; 73 }; 74 75 struct pci_quirk pci_quirks[] = { 76 /* 77 * The Intel 82371AB has a map register at offset 0x90. 78 */ 79 { 0x71138086, PCI_QUIRK_MAP_REG, 0x90, 0 }, 80 81 { 0 } 82 }; 83 84 /* map register information */ 85 #define PCI_MAPMEM 0x01 /* memory map */ 86 #define PCI_MAPMEMP 0x02 /* prefetchable memory map */ 87 #define PCI_MAPPORT 0x04 /* port map */ 88 89 struct pci_devinfo { 90 STAILQ_ENTRY(pci_devinfo) pci_links; 91 struct resource_list resources; 92 pcicfgregs cfg; 93 struct pci_conf conf; 94 }; 95 96 static STAILQ_HEAD(devlist, pci_devinfo) pci_devq; 97 u_int32_t pci_numdevs = 0; 98 static u_int32_t pci_generation = 0; 99 100 /* return base address of memory or port map */ 101 102 static u_int32_t 103 pci_mapbase(unsigned mapreg) 104 { 105 int mask = 0x03; 106 if ((mapreg & 0x01) == 0) 107 mask = 0x0f; 108 return (mapreg & ~mask); 109 } 110 111 /* return map type of memory or port map */ 112 113 static int 114 pci_maptype(unsigned mapreg) 115 { 116 static u_int8_t maptype[0x10] = { 117 PCI_MAPMEM, PCI_MAPPORT, 118 PCI_MAPMEM, 0, 119 PCI_MAPMEM, PCI_MAPPORT, 120 0, 0, 121 PCI_MAPMEM|PCI_MAPMEMP, PCI_MAPPORT, 122 PCI_MAPMEM|PCI_MAPMEMP, 0, 123 PCI_MAPMEM|PCI_MAPMEMP, PCI_MAPPORT, 124 0, 0, 125 }; 126 127 return maptype[mapreg & 0x0f]; 128 } 129 130 /* return log2 of map size decoded for memory or port map */ 131 132 static int 133 pci_mapsize(unsigned testval) 134 { 135 int ln2size; 136 137 testval = pci_mapbase(testval); 138 ln2size = 0; 139 if (testval != 0) { 140 while ((testval & 1) == 0) 141 { 142 ln2size++; 143 testval >>= 1; 144 } 145 } 146 return (ln2size); 147 } 148 149 /* return log2 of address range supported by map register */ 150 151 static int 152 pci_maprange(unsigned mapreg) 153 { 154 int ln2range = 0; 155 switch (mapreg & 0x07) { 156 case 0x00: 157 case 0x01: 158 case 0x05: 159 ln2range = 32; 160 break; 161 case 0x02: 162 ln2range = 20; 163 break; 164 case 0x04: 165 ln2range = 64; 166 break; 167 } 168 return (ln2range); 169 } 170 171 /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */ 172 173 static void 174 pci_fixancient(pcicfgregs *cfg) 175 { 176 if (cfg->hdrtype != 0) 177 return; 178 179 /* PCI to PCI bridges use header type 1 */ 180 if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI) 181 cfg->hdrtype = 1; 182 } 183 184 /* read config data specific to header type 1 device (PCI to PCI bridge) */ 185 186 static void * 187 pci_readppb(pcicfgregs *cfg) 188 { 189 pcih1cfgregs *p; 190 191 p = malloc(sizeof (pcih1cfgregs), M_DEVBUF, M_WAITOK); 192 if (p == NULL) 193 return (NULL); 194 195 bzero(p, sizeof *p); 196 197 p->secstat = pci_cfgread(cfg, PCIR_SECSTAT_1, 2); 198 p->bridgectl = pci_cfgread(cfg, PCIR_BRIDGECTL_1, 2); 199 200 p->seclat = pci_cfgread(cfg, PCIR_SECLAT_1, 1); 201 202 p->iobase = PCI_PPBIOBASE (pci_cfgread(cfg, PCIR_IOBASEH_1, 2), 203 pci_cfgread(cfg, PCIR_IOBASEL_1, 1)); 204 p->iolimit = PCI_PPBIOLIMIT (pci_cfgread(cfg, PCIR_IOLIMITH_1, 2), 205 pci_cfgread(cfg, PCIR_IOLIMITL_1, 1)); 206 207 p->membase = PCI_PPBMEMBASE (0, 208 pci_cfgread(cfg, PCIR_MEMBASE_1, 2)); 209 p->memlimit = PCI_PPBMEMLIMIT (0, 210 pci_cfgread(cfg, PCIR_MEMLIMIT_1, 2)); 211 212 p->pmembase = PCI_PPBMEMBASE ( 213 (pci_addr_t)pci_cfgread(cfg, PCIR_PMBASEH_1, 4), 214 pci_cfgread(cfg, PCIR_PMBASEL_1, 2)); 215 216 p->pmemlimit = PCI_PPBMEMLIMIT ( 217 (pci_addr_t)pci_cfgread(cfg, PCIR_PMLIMITH_1, 4), 218 pci_cfgread(cfg, PCIR_PMLIMITL_1, 2)); 219 return (p); 220 } 221 222 /* read config data specific to header type 2 device (PCI to CardBus bridge) */ 223 224 static void * 225 pci_readpcb(pcicfgregs *cfg) 226 { 227 pcih2cfgregs *p; 228 229 p = malloc(sizeof (pcih2cfgregs), M_DEVBUF, M_WAITOK); 230 if (p == NULL) 231 return (NULL); 232 233 bzero(p, sizeof *p); 234 235 p->secstat = pci_cfgread(cfg, PCIR_SECSTAT_2, 2); 236 p->bridgectl = pci_cfgread(cfg, PCIR_BRIDGECTL_2, 2); 237 238 p->seclat = pci_cfgread(cfg, PCIR_SECLAT_2, 1); 239 240 p->membase0 = pci_cfgread(cfg, PCIR_MEMBASE0_2, 4); 241 p->memlimit0 = pci_cfgread(cfg, PCIR_MEMLIMIT0_2, 4); 242 p->membase1 = pci_cfgread(cfg, PCIR_MEMBASE1_2, 4); 243 p->memlimit1 = pci_cfgread(cfg, PCIR_MEMLIMIT1_2, 4); 244 245 p->iobase0 = pci_cfgread(cfg, PCIR_IOBASE0_2, 4); 246 p->iolimit0 = pci_cfgread(cfg, PCIR_IOLIMIT0_2, 4); 247 p->iobase1 = pci_cfgread(cfg, PCIR_IOBASE1_2, 4); 248 p->iolimit1 = pci_cfgread(cfg, PCIR_IOLIMIT1_2, 4); 249 250 p->pccardif = pci_cfgread(cfg, PCIR_PCCARDIF_2, 4); 251 return p; 252 } 253 254 /* extract header type specific config data */ 255 256 static void 257 pci_hdrtypedata(pcicfgregs *cfg) 258 { 259 switch (cfg->hdrtype) { 260 case 0: 261 cfg->subvendor = pci_cfgread(cfg, PCIR_SUBVEND_0, 2); 262 cfg->subdevice = pci_cfgread(cfg, PCIR_SUBDEV_0, 2); 263 cfg->nummaps = PCI_MAXMAPS_0; 264 break; 265 case 1: 266 cfg->subvendor = pci_cfgread(cfg, PCIR_SUBVEND_1, 2); 267 cfg->subdevice = pci_cfgread(cfg, PCIR_SUBDEV_1, 2); 268 cfg->secondarybus = pci_cfgread(cfg, PCIR_SECBUS_1, 1); 269 cfg->subordinatebus = pci_cfgread(cfg, PCIR_SUBBUS_1, 1); 270 cfg->nummaps = PCI_MAXMAPS_1; 271 cfg->hdrspec = pci_readppb(cfg); 272 break; 273 case 2: 274 cfg->subvendor = pci_cfgread(cfg, PCIR_SUBVEND_2, 2); 275 cfg->subdevice = pci_cfgread(cfg, PCIR_SUBDEV_2, 2); 276 cfg->secondarybus = pci_cfgread(cfg, PCIR_SECBUS_2, 1); 277 cfg->subordinatebus = pci_cfgread(cfg, PCIR_SUBBUS_2, 1); 278 cfg->nummaps = PCI_MAXMAPS_2; 279 cfg->hdrspec = pci_readpcb(cfg); 280 break; 281 } 282 } 283 284 /* read configuration header into pcicfgrect structure */ 285 286 static struct pci_devinfo * 287 pci_readcfg(pcicfgregs *probe) 288 { 289 pcicfgregs *cfg = NULL; 290 struct pci_devinfo *devlist_entry; 291 struct devlist *devlist_head; 292 293 devlist_head = &pci_devq; 294 295 devlist_entry = NULL; 296 297 if (pci_cfgread(probe, PCIR_DEVVENDOR, 4) != -1) { 298 299 devlist_entry = malloc(sizeof(struct pci_devinfo), 300 M_DEVBUF, M_WAITOK); 301 if (devlist_entry == NULL) 302 return (NULL); 303 bzero(devlist_entry, sizeof *devlist_entry); 304 305 cfg = &devlist_entry->cfg; 306 307 cfg->hose = probe->hose; 308 cfg->bus = probe->bus; 309 cfg->slot = probe->slot; 310 cfg->func = probe->func; 311 cfg->vendor = pci_cfgread(cfg, PCIR_VENDOR, 2); 312 cfg->device = pci_cfgread(cfg, PCIR_DEVICE, 2); 313 cfg->cmdreg = pci_cfgread(cfg, PCIR_COMMAND, 2); 314 cfg->statreg = pci_cfgread(cfg, PCIR_STATUS, 2); 315 cfg->baseclass = pci_cfgread(cfg, PCIR_CLASS, 1); 316 cfg->subclass = pci_cfgread(cfg, PCIR_SUBCLASS, 1); 317 cfg->progif = pci_cfgread(cfg, PCIR_PROGIF, 1); 318 cfg->revid = pci_cfgread(cfg, PCIR_REVID, 1); 319 cfg->hdrtype = pci_cfgread(cfg, PCIR_HEADERTYPE, 1); 320 cfg->cachelnsz = pci_cfgread(cfg, PCIR_CACHELNSZ, 1); 321 cfg->lattimer = pci_cfgread(cfg, PCIR_LATTIMER, 1); 322 cfg->intpin = pci_cfgread(cfg, PCIR_INTPIN, 1); 323 cfg->intline = pci_cfgread(cfg, PCIR_INTLINE, 1); 324 #ifdef __alpha__ 325 alpha_platform_assign_pciintr(cfg); 326 #endif 327 328 #ifdef APIC_IO 329 if (cfg->intpin != 0) { 330 int airq; 331 332 airq = pci_apic_irq(cfg->bus, cfg->slot, cfg->intpin); 333 if (airq >= 0) { 334 /* PCI specific entry found in MP table */ 335 if (airq != cfg->intline) { 336 undirect_pci_irq(cfg->intline); 337 cfg->intline = airq; 338 } 339 } else { 340 /* 341 * PCI interrupts might be redirected to the 342 * ISA bus according to some MP tables. Use the 343 * same methods as used by the ISA devices 344 * devices to find the proper IOAPIC int pin. 345 */ 346 airq = isa_apic_irq(cfg->intline); 347 if ((airq >= 0) && (airq != cfg->intline)) { 348 /* XXX: undirect_pci_irq() ? */ 349 undirect_isa_irq(cfg->intline); 350 cfg->intline = airq; 351 } 352 } 353 } 354 #endif /* APIC_IO */ 355 356 cfg->mingnt = pci_cfgread(cfg, PCIR_MINGNT, 1); 357 cfg->maxlat = pci_cfgread(cfg, PCIR_MAXLAT, 1); 358 359 cfg->mfdev = (cfg->hdrtype & PCIM_MFDEV) != 0; 360 cfg->hdrtype &= ~PCIM_MFDEV; 361 362 pci_fixancient(cfg); 363 pci_hdrtypedata(cfg); 364 365 STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links); 366 367 devlist_entry->conf.pc_sel.pc_bus = cfg->bus; 368 devlist_entry->conf.pc_sel.pc_dev = cfg->slot; 369 devlist_entry->conf.pc_sel.pc_func = cfg->func; 370 devlist_entry->conf.pc_hdr = cfg->hdrtype; 371 372 devlist_entry->conf.pc_subvendor = cfg->subvendor; 373 devlist_entry->conf.pc_subdevice = cfg->subdevice; 374 devlist_entry->conf.pc_vendor = cfg->vendor; 375 devlist_entry->conf.pc_device = cfg->device; 376 377 devlist_entry->conf.pc_class = cfg->baseclass; 378 devlist_entry->conf.pc_subclass = cfg->subclass; 379 devlist_entry->conf.pc_progif = cfg->progif; 380 devlist_entry->conf.pc_revid = cfg->revid; 381 382 pci_numdevs++; 383 pci_generation++; 384 } 385 return (devlist_entry); 386 } 387 388 #if 0 389 /* free pcicfgregs structure and all depending data structures */ 390 391 static int 392 pci_freecfg(struct pci_devinfo *dinfo) 393 { 394 struct devlist *devlist_head; 395 396 devlist_head = &pci_devq; 397 398 if (dinfo->cfg.hdrspec != NULL) 399 free(dinfo->cfg.hdrspec, M_DEVBUF); 400 if (dinfo->cfg.map != NULL) 401 free(dinfo->cfg.map, M_DEVBUF); 402 /* XXX this hasn't been tested */ 403 STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links); 404 free(dinfo, M_DEVBUF); 405 406 /* increment the generation count */ 407 pci_generation++; 408 409 /* we're losing one device */ 410 pci_numdevs--; 411 return (0); 412 } 413 #endif 414 415 416 /* 417 * This is the user interface to PCI configuration space. 418 */ 419 420 static int 421 pci_open(dev_t dev, int oflags, int devtype, struct proc *p) 422 { 423 if ((oflags & FWRITE) && securelevel > 0) { 424 return EPERM; 425 } 426 return 0; 427 } 428 429 static int 430 pci_close(dev_t dev, int flag, int devtype, struct proc *p) 431 { 432 return 0; 433 } 434 435 /* 436 * Match a single pci_conf structure against an array of pci_match_conf 437 * structures. The first argument, 'matches', is an array of num_matches 438 * pci_match_conf structures. match_buf is a pointer to the pci_conf 439 * structure that will be compared to every entry in the matches array. 440 * This function returns 1 on failure, 0 on success. 441 */ 442 static int 443 pci_conf_match(struct pci_match_conf *matches, int num_matches, 444 struct pci_conf *match_buf) 445 { 446 int i; 447 448 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 449 return(1); 450 451 for (i = 0; i < num_matches; i++) { 452 /* 453 * I'm not sure why someone would do this...but... 454 */ 455 if (matches[i].flags == PCI_GETCONF_NO_MATCH) 456 continue; 457 458 /* 459 * Look at each of the match flags. If it's set, do the 460 * comparison. If the comparison fails, we don't have a 461 * match, go on to the next item if there is one. 462 */ 463 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 464 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 465 continue; 466 467 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 468 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 469 continue; 470 471 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 472 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 473 continue; 474 475 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 476 && (match_buf->pc_vendor != matches[i].pc_vendor)) 477 continue; 478 479 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 480 && (match_buf->pc_device != matches[i].pc_device)) 481 continue; 482 483 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 484 && (match_buf->pc_class != matches[i].pc_class)) 485 continue; 486 487 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 488 && (match_buf->pd_unit != matches[i].pd_unit)) 489 continue; 490 491 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 492 && (strncmp(matches[i].pd_name, match_buf->pd_name, 493 sizeof(match_buf->pd_name)) != 0)) 494 continue; 495 496 return(0); 497 } 498 499 return(1); 500 } 501 502 /* 503 * Locate the parent of a PCI device by scanning the PCI devlist 504 * and return the entry for the parent. 505 * For devices on PCI Bus 0 (the host bus), this is the PCI Host. 506 * For devices on secondary PCI busses, this is that bus' PCI-PCI Bridge. 507 */ 508 509 pcicfgregs * 510 pci_devlist_get_parent(pcicfgregs *cfg) 511 { 512 struct devlist *devlist_head; 513 struct pci_devinfo *dinfo; 514 pcicfgregs *bridge_cfg; 515 int i; 516 517 dinfo = STAILQ_FIRST(devlist_head = &pci_devq); 518 519 /* If the device is on PCI bus 0, look for the host */ 520 if (cfg->bus == 0) { 521 for (i = 0; (dinfo != NULL) && (i < pci_numdevs); 522 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 523 bridge_cfg = &dinfo->cfg; 524 if (bridge_cfg->baseclass == PCIC_BRIDGE 525 && bridge_cfg->subclass == PCIS_BRIDGE_HOST 526 && bridge_cfg->bus == cfg->bus) { 527 return bridge_cfg; 528 } 529 } 530 } 531 532 /* If the device is not on PCI bus 0, look for the PCI-PCI bridge */ 533 if (cfg->bus > 0) { 534 for (i = 0; (dinfo != NULL) && (i < pci_numdevs); 535 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 536 bridge_cfg = &dinfo->cfg; 537 if (bridge_cfg->baseclass == PCIC_BRIDGE 538 && bridge_cfg->subclass == PCIS_BRIDGE_PCI 539 && bridge_cfg->secondarybus == cfg->bus) { 540 return bridge_cfg; 541 } 542 } 543 } 544 545 return NULL; 546 } 547 548 static int 549 pci_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 550 { 551 struct pci_io *io; 552 const char *name; 553 int error; 554 555 if (!(flag & FWRITE)) 556 return EPERM; 557 558 559 switch(cmd) { 560 case PCIOCGETCONF: 561 { 562 struct pci_devinfo *dinfo; 563 struct pci_conf_io *cio; 564 struct devlist *devlist_head; 565 struct pci_match_conf *pattern_buf; 566 int num_patterns; 567 size_t iolen; 568 int ionum, i; 569 570 cio = (struct pci_conf_io *)data; 571 572 num_patterns = 0; 573 dinfo = NULL; 574 575 /* 576 * Hopefully the user won't pass in a null pointer, but it 577 * can't hurt to check. 578 */ 579 if (cio == NULL) { 580 error = EINVAL; 581 break; 582 } 583 584 /* 585 * If the user specified an offset into the device list, 586 * but the list has changed since they last called this 587 * ioctl, tell them that the list has changed. They will 588 * have to get the list from the beginning. 589 */ 590 if ((cio->offset != 0) 591 && (cio->generation != pci_generation)){ 592 cio->num_matches = 0; 593 cio->status = PCI_GETCONF_LIST_CHANGED; 594 error = 0; 595 break; 596 } 597 598 /* 599 * Check to see whether the user has asked for an offset 600 * past the end of our list. 601 */ 602 if (cio->offset >= pci_numdevs) { 603 cio->num_matches = 0; 604 cio->status = PCI_GETCONF_LAST_DEVICE; 605 error = 0; 606 break; 607 } 608 609 /* get the head of the device queue */ 610 devlist_head = &pci_devq; 611 612 /* 613 * Determine how much room we have for pci_conf structures. 614 * Round the user's buffer size down to the nearest 615 * multiple of sizeof(struct pci_conf) in case the user 616 * didn't specify a multiple of that size. 617 */ 618 iolen = min(cio->match_buf_len - 619 (cio->match_buf_len % sizeof(struct pci_conf)), 620 pci_numdevs * sizeof(struct pci_conf)); 621 622 /* 623 * Since we know that iolen is a multiple of the size of 624 * the pciconf union, it's okay to do this. 625 */ 626 ionum = iolen / sizeof(struct pci_conf); 627 628 /* 629 * If this test is true, the user wants the pci_conf 630 * structures returned to match the supplied entries. 631 */ 632 if ((cio->num_patterns > 0) 633 && (cio->pat_buf_len > 0)) { 634 /* 635 * pat_buf_len needs to be: 636 * num_patterns * sizeof(struct pci_match_conf) 637 * While it is certainly possible the user just 638 * allocated a large buffer, but set the number of 639 * matches correctly, it is far more likely that 640 * their kernel doesn't match the userland utility 641 * they're using. It's also possible that the user 642 * forgot to initialize some variables. Yes, this 643 * may be overly picky, but I hazard to guess that 644 * it's far more likely to just catch folks that 645 * updated their kernel but not their userland. 646 */ 647 if ((cio->num_patterns * 648 sizeof(struct pci_match_conf)) != cio->pat_buf_len){ 649 /* The user made a mistake, return an error*/ 650 cio->status = PCI_GETCONF_ERROR; 651 printf("pci_ioctl: pat_buf_len %d != " 652 "num_patterns (%d) * sizeof(struct " 653 "pci_match_conf) (%d)\npci_ioctl: " 654 "pat_buf_len should be = %d\n", 655 cio->pat_buf_len, cio->num_patterns, 656 (int)sizeof(struct pci_match_conf), 657 (int)sizeof(struct pci_match_conf) * 658 cio->num_patterns); 659 printf("pci_ioctl: do your headers match your " 660 "kernel?\n"); 661 cio->num_matches = 0; 662 error = EINVAL; 663 break; 664 } 665 666 /* 667 * Check the user's buffer to make sure it's readable. 668 */ 669 if (!useracc((caddr_t)cio->patterns, 670 cio->pat_buf_len, VM_PROT_READ)) { 671 printf("pci_ioctl: pattern buffer %p, " 672 "length %u isn't user accessible for" 673 " READ\n", cio->patterns, 674 cio->pat_buf_len); 675 error = EACCES; 676 break; 677 } 678 /* 679 * Allocate a buffer to hold the patterns. 680 */ 681 pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 682 M_WAITOK); 683 error = copyin(cio->patterns, pattern_buf, 684 cio->pat_buf_len); 685 if (error != 0) 686 break; 687 num_patterns = cio->num_patterns; 688 689 } else if ((cio->num_patterns > 0) 690 || (cio->pat_buf_len > 0)) { 691 /* 692 * The user made a mistake, spit out an error. 693 */ 694 cio->status = PCI_GETCONF_ERROR; 695 cio->num_matches = 0; 696 printf("pci_ioctl: invalid GETCONF arguments\n"); 697 error = EINVAL; 698 break; 699 } else 700 pattern_buf = NULL; 701 702 /* 703 * Make sure we can write to the match buffer. 704 */ 705 if (!useracc((caddr_t)cio->matches, 706 cio->match_buf_len, VM_PROT_WRITE)) { 707 printf("pci_ioctl: match buffer %p, length %u " 708 "isn't user accessible for WRITE\n", 709 cio->matches, cio->match_buf_len); 710 error = EACCES; 711 break; 712 } 713 714 /* 715 * Go through the list of devices and copy out the devices 716 * that match the user's criteria. 717 */ 718 for (cio->num_matches = 0, error = 0, i = 0, 719 dinfo = STAILQ_FIRST(devlist_head); 720 (dinfo != NULL) && (cio->num_matches < ionum) 721 && (error == 0) && (i < pci_numdevs); 722 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 723 724 if (i < cio->offset) 725 continue; 726 727 /* Populate pd_name and pd_unit */ 728 name = NULL; 729 if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0') 730 name = device_get_name(dinfo->cfg.dev); 731 if (name) { 732 strncpy(dinfo->conf.pd_name, name, 733 sizeof(dinfo->conf.pd_name)); 734 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 735 dinfo->conf.pd_unit = 736 device_get_unit(dinfo->cfg.dev); 737 } 738 739 if ((pattern_buf == NULL) || 740 (pci_conf_match(pattern_buf, num_patterns, 741 &dinfo->conf) == 0)) { 742 743 /* 744 * If we've filled up the user's buffer, 745 * break out at this point. Since we've 746 * got a match here, we'll pick right back 747 * up at the matching entry. We can also 748 * tell the user that there are more matches 749 * left. 750 */ 751 if (cio->num_matches >= ionum) 752 break; 753 754 error = copyout(&dinfo->conf, 755 &cio->matches[cio->num_matches], 756 sizeof(struct pci_conf)); 757 cio->num_matches++; 758 } 759 } 760 761 /* 762 * Set the pointer into the list, so if the user is getting 763 * n records at a time, where n < pci_numdevs, 764 */ 765 cio->offset = i; 766 767 /* 768 * Set the generation, the user will need this if they make 769 * another ioctl call with offset != 0. 770 */ 771 cio->generation = pci_generation; 772 773 /* 774 * If this is the last device, inform the user so he won't 775 * bother asking for more devices. If dinfo isn't NULL, we 776 * know that there are more matches in the list because of 777 * the way the traversal is done. 778 */ 779 if (dinfo == NULL) 780 cio->status = PCI_GETCONF_LAST_DEVICE; 781 else 782 cio->status = PCI_GETCONF_MORE_DEVS; 783 784 if (pattern_buf != NULL) 785 free(pattern_buf, M_TEMP); 786 787 break; 788 } 789 case PCIOCREAD: 790 io = (struct pci_io *)data; 791 switch(io->pi_width) { 792 pcicfgregs probe; 793 case 4: 794 case 2: 795 case 1: 796 probe.hose = -1; 797 probe.bus = io->pi_sel.pc_bus; 798 probe.slot = io->pi_sel.pc_dev; 799 probe.func = io->pi_sel.pc_func; 800 io->pi_data = pci_cfgread(&probe, 801 io->pi_reg, io->pi_width); 802 error = 0; 803 break; 804 default: 805 error = ENODEV; 806 break; 807 } 808 break; 809 810 case PCIOCWRITE: 811 io = (struct pci_io *)data; 812 switch(io->pi_width) { 813 pcicfgregs probe; 814 case 4: 815 case 2: 816 case 1: 817 probe.hose = -1; 818 probe.bus = io->pi_sel.pc_bus; 819 probe.slot = io->pi_sel.pc_dev; 820 probe.func = io->pi_sel.pc_func; 821 pci_cfgwrite(&probe, 822 io->pi_reg, io->pi_data, io->pi_width); 823 error = 0; 824 break; 825 default: 826 error = ENODEV; 827 break; 828 } 829 break; 830 831 default: 832 error = ENOTTY; 833 break; 834 } 835 836 return (error); 837 } 838 839 #define PCI_CDEV 78 840 841 static struct cdevsw pcicdev = { 842 /* open */ pci_open, 843 /* close */ pci_close, 844 /* read */ noread, 845 /* write */ nowrite, 846 /* ioctl */ pci_ioctl, 847 /* poll */ nopoll, 848 /* mmap */ nommap, 849 /* strategy */ nostrategy, 850 /* name */ "pci", 851 /* maj */ PCI_CDEV, 852 /* dump */ nodump, 853 /* psize */ nopsize, 854 /* flags */ 0, 855 /* bmaj */ -1 856 }; 857 858 #include "pci_if.h" 859 860 static devclass_t pci_devclass; 861 862 #ifdef COMPAT_OLDPCI 863 /* 864 * A simple driver to wrap the old pci driver mechanism for back-compat. 865 */ 866 867 static int 868 pci_compat_probe(device_t dev) 869 { 870 struct pci_device *dvp; 871 struct pci_devinfo *dinfo; 872 pcicfgregs *cfg; 873 const char *name; 874 int error; 875 876 dinfo = device_get_ivars(dev); 877 cfg = &dinfo->cfg; 878 dvp = device_get_driver(dev)->priv; 879 880 /* 881 * Do the wrapped probe. 882 */ 883 error = ENXIO; 884 if (dvp && dvp->pd_probe) { 885 name = dvp->pd_probe(cfg, (cfg->device << 16) + cfg->vendor); 886 if (name) { 887 device_set_desc_copy(dev, name); 888 /* Allow newbus drivers to match "better" */ 889 error = -200; 890 } 891 } 892 893 return error; 894 } 895 896 static int 897 pci_compat_attach(device_t dev) 898 { 899 struct pci_device *dvp; 900 struct pci_devinfo *dinfo; 901 pcicfgregs *cfg; 902 int unit; 903 904 dinfo = device_get_ivars(dev); 905 cfg = &dinfo->cfg; 906 dvp = device_get_driver(dev)->priv; 907 908 unit = device_get_unit(dev); 909 if (unit > *dvp->pd_count) 910 *dvp->pd_count = unit; 911 if (dvp->pd_attach) 912 dvp->pd_attach(cfg, unit); 913 device_printf(dev, "driver is using old-style compatability shims\n"); 914 return 0; 915 } 916 917 static device_method_t pci_compat_methods[] = { 918 /* Device interface */ 919 DEVMETHOD(device_probe, pci_compat_probe), 920 DEVMETHOD(device_attach, pci_compat_attach), 921 922 { 0, 0 } 923 }; 924 925 /* 926 * Create a new style driver around each old pci driver. 927 */ 928 int 929 compat_pci_handler(module_t mod, int type, void *data) 930 { 931 struct pci_device *dvp = (struct pci_device *)data; 932 driver_t *driver; 933 934 switch (type) { 935 case MOD_LOAD: 936 driver = malloc(sizeof(driver_t), M_DEVBUF, M_NOWAIT); 937 if (!driver) 938 return ENOMEM; 939 bzero(driver, sizeof(driver_t)); 940 driver->name = dvp->pd_name; 941 driver->methods = pci_compat_methods; 942 driver->size = sizeof(struct pci_devinfo *); 943 driver->priv = dvp; 944 devclass_add_driver(pci_devclass, driver); 945 break; 946 case MOD_UNLOAD: 947 printf("%s: module unload not supported!\n", dvp->pd_name); 948 return EOPNOTSUPP; 949 default: 950 break; 951 } 952 return 0; 953 } 954 #endif 955 956 /* 957 * New style pci driver. Parent device is either a pci-host-bridge or a 958 * pci-pci-bridge. Both kinds are represented by instances of pcib. 959 */ 960 961 static void 962 pci_print_verbose(struct pci_devinfo *dinfo) 963 { 964 if (bootverbose) { 965 pcicfgregs *cfg = &dinfo->cfg; 966 967 printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n", 968 cfg->vendor, cfg->device, cfg->revid); 969 printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n", 970 cfg->baseclass, cfg->subclass, cfg->progif, 971 cfg->hdrtype, cfg->mfdev); 972 printf("\tsubordinatebus=%x \tsecondarybus=%x\n", 973 cfg->subordinatebus, cfg->secondarybus); 974 #ifdef PCI_DEBUG 975 printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n", 976 cfg->cmdreg, cfg->statreg, cfg->cachelnsz); 977 printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n", 978 cfg->lattimer, cfg->lattimer * 30, 979 cfg->mingnt, cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250); 980 #endif /* PCI_DEBUG */ 981 if (cfg->intpin > 0) 982 printf("\tintpin=%c, irq=%d\n", cfg->intpin +'a' -1, cfg->intline); 983 } 984 } 985 986 static int 987 pci_porten(pcicfgregs *cfg) 988 { 989 return ((cfg->cmdreg & PCIM_CMD_PORTEN) != 0); 990 } 991 992 static int 993 pci_memen(pcicfgregs *cfg) 994 { 995 return ((cfg->cmdreg & PCIM_CMD_MEMEN) != 0); 996 } 997 998 /* 999 * Add a resource based on a pci map register. Return 1 if the map 1000 * register is a 32bit map register or 2 if it is a 64bit register. 1001 */ 1002 static int 1003 pci_add_map(device_t dev, pcicfgregs* cfg, int reg) 1004 { 1005 struct pci_devinfo *dinfo = device_get_ivars(dev); 1006 struct resource_list *rl = &dinfo->resources; 1007 u_int32_t map; 1008 u_int64_t base; 1009 u_int8_t ln2size; 1010 u_int8_t ln2range; 1011 u_int32_t testval; 1012 1013 int type; 1014 1015 map = pci_cfgread(cfg, reg, 4); 1016 1017 if (map == 0 || map == 0xffffffff) 1018 return 1; /* skip invalid entry */ 1019 1020 pci_cfgwrite(cfg, reg, 0xffffffff, 4); 1021 testval = pci_cfgread(cfg, reg, 4); 1022 pci_cfgwrite(cfg, reg, map, 4); 1023 1024 base = pci_mapbase(map); 1025 if (pci_maptype(map) & PCI_MAPMEM) 1026 type = SYS_RES_MEMORY; 1027 else 1028 type = SYS_RES_IOPORT; 1029 ln2size = pci_mapsize(testval); 1030 ln2range = pci_maprange(testval); 1031 if (ln2range == 64) { 1032 /* Read the other half of a 64bit map register */ 1033 base |= (u_int64_t) pci_cfgread(cfg, reg + 4, 4) << 32; 1034 } 1035 1036 #ifdef __alpha__ 1037 /* 1038 * XXX: encode hose number in the base addr, 1039 * This will go away once the bus_space functions 1040 * can deal with multiple hoses 1041 */ 1042 1043 if (cfg->hose) { 1044 u_int32_t mask, shift, maxh; 1045 1046 switch (hwrpb->rpb_type) { 1047 case ST_DEC_21000: 1048 case ST_DEC_4100: 1049 mask = 0xf8000000; 1050 shift = 27; 1051 maxh = 32; 1052 break; 1053 case ST_DEC_6600: 1054 mask = 0x80000000; 1055 shift = 31; 1056 maxh = 2; 1057 break; 1058 default: 1059 mask = 0; 1060 shift = 0; 1061 maxh = 0; 1062 break; 1063 } 1064 if (base & mask) { 1065 printf("base addr = 0x%llx\n", (long long) base); 1066 printf("mask addr = 0x%lx\n", (long) mask); 1067 printf("hacked addr = 0x%llx\n", (long long) 1068 (base | ((u_int64_t)cfg->hose << shift))); 1069 panic("hose encoding hack would clobber base addr"); 1070 /* NOTREACHED */ 1071 } 1072 if (cfg->hose >= maxh) { 1073 panic("Hose %d - can only encode %d hose(s)", 1074 cfg->hose, maxh); 1075 /* NOTREACHED */ 1076 } 1077 base |= ((u_int64_t)cfg->hose << shift); 1078 } 1079 #endif 1080 if (bootverbose) { 1081 printf("\tmap[%02x]: type %x, range %2d, base %08x, size %2d", 1082 reg, pci_maptype(base), ln2range, 1083 (unsigned int) base, ln2size); 1084 if (type == SYS_RES_IOPORT && !pci_porten(cfg)) 1085 printf(", port disabled\n"); 1086 else if (type == SYS_RES_MEMORY && !pci_memen(cfg)) 1087 printf(", memory disabled\n"); 1088 else 1089 printf(", enabled\n"); 1090 } 1091 1092 if (type == SYS_RES_IOPORT && !pci_porten(cfg)) 1093 return 1; 1094 if (type == SYS_RES_MEMORY && !pci_memen(cfg)) 1095 return 1; 1096 1097 resource_list_add(rl, type, reg, 1098 base, base + (1 << ln2size) - 1, 1099 (1 << ln2size)); 1100 1101 return (ln2range == 64) ? 2 : 1; 1102 } 1103 1104 static void 1105 pci_add_resources(device_t dev, pcicfgregs* cfg) 1106 { 1107 struct pci_devinfo *dinfo = device_get_ivars(dev); 1108 struct resource_list *rl = &dinfo->resources; 1109 struct pci_quirk *q; 1110 int i; 1111 1112 for (i = 0; i < cfg->nummaps;) { 1113 i += pci_add_map(dev, cfg, PCIR_MAPS + i*4); 1114 } 1115 1116 for (q = &pci_quirks[0]; q->devid; q++) { 1117 if (q->devid == ((cfg->device << 16) | cfg->vendor) 1118 && q->type == PCI_QUIRK_MAP_REG) 1119 pci_add_map(dev, cfg, q->arg1); 1120 } 1121 1122 if (cfg->intpin > 0 && cfg->intline != 255) 1123 resource_list_add(rl, SYS_RES_IRQ, 0, 1124 cfg->intline, cfg->intline, 1); 1125 } 1126 1127 static void 1128 pci_add_children(device_t dev, int busno) 1129 { 1130 pcicfgregs probe; 1131 1132 #ifdef SIMOS 1133 #undef PCI_SLOTMAX 1134 #define PCI_SLOTMAX 0 1135 #endif 1136 1137 bzero(&probe, sizeof probe); 1138 #ifdef __alpha__ 1139 probe.hose = pcib_get_hose(dev); 1140 #endif 1141 #ifdef __i386__ 1142 probe.hose = 0; 1143 #endif 1144 probe.bus = busno; 1145 1146 for (probe.slot = 0; probe.slot <= PCI_SLOTMAX; probe.slot++) { 1147 int pcifunchigh = 0; 1148 for (probe.func = 0; probe.func <= pcifunchigh; probe.func++) { 1149 struct pci_devinfo *dinfo = pci_readcfg(&probe); 1150 if (dinfo != NULL) { 1151 if (dinfo->cfg.mfdev) 1152 pcifunchigh = 7; 1153 1154 pci_print_verbose(dinfo); 1155 dinfo->cfg.dev = device_add_child(dev, NULL, -1); 1156 device_set_ivars(dinfo->cfg.dev, dinfo); 1157 pci_add_resources(dinfo->cfg.dev, &dinfo->cfg); 1158 } 1159 } 1160 } 1161 } 1162 1163 static int 1164 pci_new_probe(device_t dev) 1165 { 1166 static int once; 1167 1168 device_set_desc(dev, "PCI bus"); 1169 pci_add_children(dev, device_get_unit(dev)); 1170 if (!once) { 1171 make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644, "pci"); 1172 once++; 1173 } 1174 1175 return 0; 1176 } 1177 1178 static int 1179 pci_print_resources(struct resource_list *rl, const char *name, int type, 1180 const char *format) 1181 { 1182 struct resource_list_entry *rle; 1183 int printed, retval; 1184 1185 printed = 0; 1186 retval = 0; 1187 /* Yes, this is kinda cheating */ 1188 SLIST_FOREACH(rle, rl, link) { 1189 if (rle->type == type) { 1190 if (printed == 0) 1191 retval += printf(" %s ", name); 1192 else if (printed > 0) 1193 retval += printf(","); 1194 printed++; 1195 retval += printf(format, rle->start); 1196 if (rle->count > 1) { 1197 retval += printf("-"); 1198 retval += printf(format, rle->start + 1199 rle->count - 1); 1200 } 1201 } 1202 } 1203 return retval; 1204 } 1205 1206 static int 1207 pci_print_child(device_t dev, device_t child) 1208 { 1209 struct pci_devinfo *dinfo; 1210 struct resource_list *rl; 1211 pcicfgregs *cfg; 1212 int retval = 0; 1213 1214 dinfo = device_get_ivars(child); 1215 cfg = &dinfo->cfg; 1216 rl = &dinfo->resources; 1217 1218 retval += bus_print_child_header(dev, child); 1219 1220 retval += pci_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx"); 1221 retval += pci_print_resources(rl, "mem", SYS_RES_MEMORY, "%#lx"); 1222 retval += pci_print_resources(rl, "irq", SYS_RES_IRQ, "%ld"); 1223 if (device_get_flags(dev)) 1224 retval += printf(" flags %#x", device_get_flags(dev)); 1225 1226 retval += printf(" at device %d.%d", pci_get_slot(child), 1227 pci_get_function(child)); 1228 1229 retval += bus_print_child_footer(dev, child); 1230 1231 return (retval); 1232 } 1233 1234 static void 1235 pci_probe_nomatch(device_t dev, device_t child) 1236 { 1237 struct pci_devinfo *dinfo; 1238 pcicfgregs *cfg; 1239 const char *desc; 1240 int unknown; 1241 1242 unknown = 0; 1243 dinfo = device_get_ivars(child); 1244 cfg = &dinfo->cfg; 1245 desc = pci_ata_match(child); 1246 if (!desc) desc = pci_usb_match(child); 1247 if (!desc) desc = pci_vga_match(child); 1248 if (!desc) { 1249 desc = "unknown card"; 1250 unknown++; 1251 } 1252 device_printf(dev, "<%s>", desc); 1253 if (bootverbose || unknown) { 1254 printf(" (vendor=0x%04x, dev=0x%04x)", 1255 cfg->vendor, 1256 cfg->device); 1257 } 1258 printf(" at %d.%d", 1259 pci_get_slot(child), 1260 pci_get_function(child)); 1261 if (cfg->intpin > 0 && cfg->intline != 255) { 1262 printf(" irq %d", cfg->intline); 1263 } 1264 printf("\n"); 1265 1266 return; 1267 } 1268 1269 static int 1270 pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1271 { 1272 struct pci_devinfo *dinfo; 1273 pcicfgregs *cfg; 1274 1275 dinfo = device_get_ivars(child); 1276 cfg = &dinfo->cfg; 1277 1278 switch (which) { 1279 case PCI_IVAR_SUBVENDOR: 1280 *result = cfg->subvendor; 1281 break; 1282 case PCI_IVAR_SUBDEVICE: 1283 *result = cfg->subdevice; 1284 break; 1285 case PCI_IVAR_VENDOR: 1286 *result = cfg->vendor; 1287 break; 1288 case PCI_IVAR_DEVICE: 1289 *result = cfg->device; 1290 break; 1291 case PCI_IVAR_DEVID: 1292 *result = (cfg->device << 16) | cfg->vendor; 1293 break; 1294 case PCI_IVAR_CLASS: 1295 *result = cfg->baseclass; 1296 break; 1297 case PCI_IVAR_SUBCLASS: 1298 *result = cfg->subclass; 1299 break; 1300 case PCI_IVAR_PROGIF: 1301 *result = cfg->progif; 1302 break; 1303 case PCI_IVAR_REVID: 1304 *result = cfg->revid; 1305 break; 1306 case PCI_IVAR_INTPIN: 1307 *result = cfg->intpin; 1308 break; 1309 case PCI_IVAR_IRQ: 1310 *result = cfg->intline; 1311 break; 1312 case PCI_IVAR_BUS: 1313 *result = cfg->bus; 1314 break; 1315 case PCI_IVAR_SLOT: 1316 *result = cfg->slot; 1317 break; 1318 case PCI_IVAR_FUNCTION: 1319 *result = cfg->func; 1320 break; 1321 case PCI_IVAR_SECONDARYBUS: 1322 *result = cfg->secondarybus; 1323 break; 1324 case PCI_IVAR_SUBORDINATEBUS: 1325 *result = cfg->subordinatebus; 1326 break; 1327 case PCI_IVAR_HOSE: 1328 /* 1329 * Pass up to parent bridge. 1330 */ 1331 *result = pcib_get_hose(dev); 1332 break; 1333 default: 1334 return ENOENT; 1335 } 1336 return 0; 1337 } 1338 1339 static int 1340 pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1341 { 1342 struct pci_devinfo *dinfo; 1343 pcicfgregs *cfg; 1344 1345 dinfo = device_get_ivars(child); 1346 cfg = &dinfo->cfg; 1347 1348 switch (which) { 1349 case PCI_IVAR_SUBVENDOR: 1350 case PCI_IVAR_SUBDEVICE: 1351 case PCI_IVAR_VENDOR: 1352 case PCI_IVAR_DEVICE: 1353 case PCI_IVAR_DEVID: 1354 case PCI_IVAR_CLASS: 1355 case PCI_IVAR_SUBCLASS: 1356 case PCI_IVAR_PROGIF: 1357 case PCI_IVAR_REVID: 1358 case PCI_IVAR_INTPIN: 1359 case PCI_IVAR_IRQ: 1360 case PCI_IVAR_BUS: 1361 case PCI_IVAR_SLOT: 1362 case PCI_IVAR_FUNCTION: 1363 return EINVAL; /* disallow for now */ 1364 1365 case PCI_IVAR_SECONDARYBUS: 1366 cfg->secondarybus = value; 1367 break; 1368 case PCI_IVAR_SUBORDINATEBUS: 1369 cfg->subordinatebus = value; 1370 break; 1371 default: 1372 return ENOENT; 1373 } 1374 return 0; 1375 } 1376 1377 static struct resource * 1378 pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 1379 u_long start, u_long end, u_long count, u_int flags) 1380 { 1381 struct pci_devinfo *dinfo = device_get_ivars(child); 1382 struct resource_list *rl = &dinfo->resources; 1383 1384 return resource_list_alloc(rl, dev, child, type, rid, 1385 start, end, count, flags); 1386 } 1387 1388 static int 1389 pci_release_resource(device_t dev, device_t child, int type, int rid, 1390 struct resource *r) 1391 { 1392 struct pci_devinfo *dinfo = device_get_ivars(child); 1393 struct resource_list *rl = &dinfo->resources; 1394 1395 return resource_list_release(rl, dev, child, type, rid, r); 1396 } 1397 1398 static int 1399 pci_set_resource(device_t dev, device_t child, int type, int rid, 1400 u_long start, u_long count) 1401 { 1402 struct pci_devinfo *dinfo = device_get_ivars(child); 1403 struct resource_list *rl = &dinfo->resources; 1404 1405 resource_list_add(rl, type, rid, start, start + count - 1, count); 1406 return 0; 1407 } 1408 1409 static int 1410 pci_get_resource(device_t dev, device_t child, int type, int rid, 1411 u_long *startp, u_long *countp) 1412 { 1413 struct pci_devinfo *dinfo = device_get_ivars(child); 1414 struct resource_list *rl = &dinfo->resources; 1415 struct resource_list_entry *rle; 1416 1417 rle = resource_list_find(rl, type, rid); 1418 if (!rle) 1419 return ENOENT; 1420 1421 if (startp) 1422 *startp = rle->start; 1423 if (countp) 1424 *countp = rle->count; 1425 1426 return 0; 1427 } 1428 1429 static void 1430 pci_delete_resource(device_t dev, device_t child, int type, int rid) 1431 { 1432 printf("pci_delete_resource: PCI resources can not be deleted\n"); 1433 } 1434 1435 static u_int32_t 1436 pci_read_config_method(device_t dev, device_t child, int reg, int width) 1437 { 1438 struct pci_devinfo *dinfo = device_get_ivars(child); 1439 pcicfgregs *cfg = &dinfo->cfg; 1440 return pci_cfgread(cfg, reg, width); 1441 } 1442 1443 static void 1444 pci_write_config_method(device_t dev, device_t child, int reg, 1445 u_int32_t val, int width) 1446 { 1447 struct pci_devinfo *dinfo = device_get_ivars(child); 1448 pcicfgregs *cfg = &dinfo->cfg; 1449 pci_cfgwrite(cfg, reg, val, width); 1450 } 1451 1452 static int 1453 pci_modevent(module_t mod, int what, void *arg) 1454 { 1455 switch (what) { 1456 case MOD_LOAD: 1457 STAILQ_INIT(&pci_devq); 1458 break; 1459 1460 case MOD_UNLOAD: 1461 break; 1462 } 1463 1464 return 0; 1465 } 1466 1467 static device_method_t pci_methods[] = { 1468 /* Device interface */ 1469 DEVMETHOD(device_probe, pci_new_probe), 1470 DEVMETHOD(device_attach, bus_generic_attach), 1471 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1472 DEVMETHOD(device_suspend, bus_generic_suspend), 1473 DEVMETHOD(device_resume, bus_generic_resume), 1474 1475 /* Bus interface */ 1476 DEVMETHOD(bus_print_child, pci_print_child), 1477 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch), 1478 DEVMETHOD(bus_read_ivar, pci_read_ivar), 1479 DEVMETHOD(bus_write_ivar, pci_write_ivar), 1480 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 1481 DEVMETHOD(bus_alloc_resource, pci_alloc_resource), 1482 DEVMETHOD(bus_release_resource, pci_release_resource), 1483 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 1484 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 1485 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 1486 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1487 DEVMETHOD(bus_set_resource, pci_set_resource), 1488 DEVMETHOD(bus_get_resource, pci_get_resource), 1489 DEVMETHOD(bus_delete_resource, pci_delete_resource), 1490 1491 /* PCI interface */ 1492 DEVMETHOD(pci_read_config, pci_read_config_method), 1493 DEVMETHOD(pci_write_config, pci_write_config_method), 1494 1495 { 0, 0 } 1496 }; 1497 1498 static driver_t pci_driver = { 1499 "pci", 1500 pci_methods, 1501 1, /* no softc */ 1502 }; 1503 1504 DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, 0); 1505