2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
26 * $FreeBSD: src/sys/pci/pci.c,v 1.141.2.15 2002/04/30 17:48:18 tmm Exp $
27 * $DragonFly: src/sys/bus/pci/pci.c,v 1.3 2003/06/23 17:55:49 dillon Exp $
34 #include "opt_simos.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/fcntl.h>
42 #include <sys/kernel.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
49 #include <vm/vm_extern.h>
52 #include <machine/bus.h>
54 #include <machine/resource.h>
55 #include <machine/md_var.h> /* For the Alpha */
57 #include <machine/pci_cfgreg.h>
60 #include <pci/pcireg.h>
61 #include <pci/pcivar.h>
62 #include <sys/pciio.h>
65 #include <machine/rpb.h>
69 #include <machine/smp.h>
72 static void pci_read_extcap(pcicfgregs *cfg);
75 u_int32_t devid; /* Vendor/device of the card */
77 #define PCI_QUIRK_MAP_REG 1 /* PCI map register in wierd place */
82 struct pci_quirk pci_quirks[] = {
84 * The Intel 82371AB and 82443MX has a map register at offset 0x90.
86 { 0x71138086, PCI_QUIRK_MAP_REG, 0x90, 0 },
87 { 0x719b8086, PCI_QUIRK_MAP_REG, 0x90, 0 },
92 /* map register information */
93 #define PCI_MAPMEM 0x01 /* memory map */
94 #define PCI_MAPMEMP 0x02 /* prefetchable memory map */
95 #define PCI_MAPPORT 0x04 /* port map */
98 STAILQ_ENTRY(pci_devinfo) pci_links;
99 struct resource_list resources;
101 struct pci_conf conf;
104 static STAILQ_HEAD(devlist, pci_devinfo) pci_devq;
105 u_int32_t pci_numdevs = 0;
106 static u_int32_t pci_generation = 0;
109 pci_find_bsf (u_int8_t bus, u_int8_t slot, u_int8_t func)
111 struct pci_devinfo *dinfo;
113 STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
114 if ((dinfo->cfg.bus == bus) &&
115 (dinfo->cfg.slot == slot) &&
116 (dinfo->cfg.func == func)) {
117 return (dinfo->cfg.dev);
125 pci_find_device (u_int16_t vendor, u_int16_t device)
127 struct pci_devinfo *dinfo;
129 STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
130 if ((dinfo->cfg.vendor == vendor) &&
131 (dinfo->cfg.device == device)) {
132 return (dinfo->cfg.dev);
139 /* return base address of memory or port map */
142 pci_mapbase(unsigned mapreg)
145 if ((mapreg & 0x01) == 0)
147 return (mapreg & ~mask);
150 /* return map type of memory or port map */
153 pci_maptype(unsigned mapreg)
155 static u_int8_t maptype[0x10] = {
156 PCI_MAPMEM, PCI_MAPPORT,
158 PCI_MAPMEM, PCI_MAPPORT,
160 PCI_MAPMEM|PCI_MAPMEMP, PCI_MAPPORT,
161 PCI_MAPMEM|PCI_MAPMEMP, 0,
162 PCI_MAPMEM|PCI_MAPMEMP, PCI_MAPPORT,
166 return maptype[mapreg & 0x0f];
169 /* return log2 of map size decoded for memory or port map */
172 pci_mapsize(unsigned testval)
176 testval = pci_mapbase(testval);
179 while ((testval & 1) == 0)
188 /* return log2 of address range supported by map register */
191 pci_maprange(unsigned mapreg)
194 switch (mapreg & 0x07) {
210 /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */
213 pci_fixancient(pcicfgregs *cfg)
215 if (cfg->hdrtype != 0)
218 /* PCI to PCI bridges use header type 1 */
219 if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI)
223 /* read config data specific to header type 1 device (PCI to PCI bridge) */
226 pci_readppb(pcicfgregs *cfg)
230 p = malloc(sizeof (pcih1cfgregs), M_DEVBUF, M_WAITOK);
236 p->secstat = pci_cfgread(cfg, PCIR_SECSTAT_1, 2);
237 p->bridgectl = pci_cfgread(cfg, PCIR_BRIDGECTL_1, 2);
239 p->seclat = pci_cfgread(cfg, PCIR_SECLAT_1, 1);
241 p->iobase = PCI_PPBIOBASE (pci_cfgread(cfg, PCIR_IOBASEH_1, 2),
242 pci_cfgread(cfg, PCIR_IOBASEL_1, 1));
243 p->iolimit = PCI_PPBIOLIMIT (pci_cfgread(cfg, PCIR_IOLIMITH_1, 2),
244 pci_cfgread(cfg, PCIR_IOLIMITL_1, 1));
246 p->membase = PCI_PPBMEMBASE (0,
247 pci_cfgread(cfg, PCIR_MEMBASE_1, 2));
248 p->memlimit = PCI_PPBMEMLIMIT (0,
249 pci_cfgread(cfg, PCIR_MEMLIMIT_1, 2));
251 p->pmembase = PCI_PPBMEMBASE (
252 (pci_addr_t)pci_cfgread(cfg, PCIR_PMBASEH_1, 4),
253 pci_cfgread(cfg, PCIR_PMBASEL_1, 2));
255 p->pmemlimit = PCI_PPBMEMLIMIT (
256 (pci_addr_t)pci_cfgread(cfg, PCIR_PMLIMITH_1, 4),
257 pci_cfgread(cfg, PCIR_PMLIMITL_1, 2));
261 /* read config data specific to header type 2 device (PCI to CardBus bridge) */
264 pci_readpcb(pcicfgregs *cfg)
268 p = malloc(sizeof (pcih2cfgregs), M_DEVBUF, M_WAITOK);
274 p->secstat = pci_cfgread(cfg, PCIR_SECSTAT_2, 2);
275 p->bridgectl = pci_cfgread(cfg, PCIR_BRIDGECTL_2, 2);
277 p->seclat = pci_cfgread(cfg, PCIR_SECLAT_2, 1);
279 p->membase0 = pci_cfgread(cfg, PCIR_MEMBASE0_2, 4);
280 p->memlimit0 = pci_cfgread(cfg, PCIR_MEMLIMIT0_2, 4);
281 p->membase1 = pci_cfgread(cfg, PCIR_MEMBASE1_2, 4);
282 p->memlimit1 = pci_cfgread(cfg, PCIR_MEMLIMIT1_2, 4);
284 p->iobase0 = pci_cfgread(cfg, PCIR_IOBASE0_2, 4);
285 p->iolimit0 = pci_cfgread(cfg, PCIR_IOLIMIT0_2, 4);
286 p->iobase1 = pci_cfgread(cfg, PCIR_IOBASE1_2, 4);
287 p->iolimit1 = pci_cfgread(cfg, PCIR_IOLIMIT1_2, 4);
289 p->pccardif = pci_cfgread(cfg, PCIR_PCCARDIF_2, 4);
293 /* extract header type specific config data */
296 pci_hdrtypedata(pcicfgregs *cfg)
298 switch (cfg->hdrtype) {
300 cfg->subvendor = pci_cfgread(cfg, PCIR_SUBVEND_0, 2);
301 cfg->subdevice = pci_cfgread(cfg, PCIR_SUBDEV_0, 2);
302 cfg->nummaps = PCI_MAXMAPS_0;
305 cfg->subvendor = pci_cfgread(cfg, PCIR_SUBVEND_1, 2);
306 cfg->subdevice = pci_cfgread(cfg, PCIR_SUBDEV_1, 2);
307 cfg->secondarybus = pci_cfgread(cfg, PCIR_SECBUS_1, 1);
308 cfg->subordinatebus = pci_cfgread(cfg, PCIR_SUBBUS_1, 1);
309 cfg->nummaps = PCI_MAXMAPS_1;
310 cfg->hdrspec = pci_readppb(cfg);
313 cfg->subvendor = pci_cfgread(cfg, PCIR_SUBVEND_2, 2);
314 cfg->subdevice = pci_cfgread(cfg, PCIR_SUBDEV_2, 2);
315 cfg->secondarybus = pci_cfgread(cfg, PCIR_SECBUS_2, 1);
316 cfg->subordinatebus = pci_cfgread(cfg, PCIR_SUBBUS_2, 1);
317 cfg->nummaps = PCI_MAXMAPS_2;
318 cfg->hdrspec = pci_readpcb(cfg);
323 /* read configuration header into pcicfgrect structure */
325 static struct pci_devinfo *
326 pci_readcfg(pcicfgregs *probe)
328 #define REG(n, w) pci_cfgread(probe, n, w)
330 pcicfgregs *cfg = NULL;
331 struct pci_devinfo *devlist_entry;
332 struct devlist *devlist_head;
334 devlist_head = &pci_devq;
336 devlist_entry = NULL;
338 if (pci_cfgread(probe, PCIR_DEVVENDOR, 4) != -1) {
340 devlist_entry = malloc(sizeof(struct pci_devinfo),
342 if (devlist_entry == NULL)
344 bzero(devlist_entry, sizeof *devlist_entry);
346 cfg = &devlist_entry->cfg;
348 cfg->hose = probe->hose;
349 cfg->bus = probe->bus;
350 cfg->slot = probe->slot;
351 cfg->func = probe->func;
352 cfg->vendor = pci_cfgread(cfg, PCIR_VENDOR, 2);
353 cfg->device = pci_cfgread(cfg, PCIR_DEVICE, 2);
354 cfg->cmdreg = pci_cfgread(cfg, PCIR_COMMAND, 2);
355 cfg->statreg = pci_cfgread(cfg, PCIR_STATUS, 2);
356 cfg->baseclass = pci_cfgread(cfg, PCIR_CLASS, 1);
357 cfg->subclass = pci_cfgread(cfg, PCIR_SUBCLASS, 1);
358 cfg->progif = pci_cfgread(cfg, PCIR_PROGIF, 1);
359 cfg->revid = pci_cfgread(cfg, PCIR_REVID, 1);
360 cfg->hdrtype = pci_cfgread(cfg, PCIR_HEADERTYPE, 1);
361 cfg->cachelnsz = pci_cfgread(cfg, PCIR_CACHELNSZ, 1);
362 cfg->lattimer = pci_cfgread(cfg, PCIR_LATTIMER, 1);
363 cfg->intpin = pci_cfgread(cfg, PCIR_INTPIN, 1);
364 cfg->intline = pci_cfgread(cfg, PCIR_INTLINE, 1);
366 alpha_platform_assign_pciintr(cfg);
370 if (cfg->intpin != 0) {
373 airq = pci_apic_irq(cfg->bus, cfg->slot, cfg->intpin);
375 /* PCI specific entry found in MP table */
376 if (airq != cfg->intline) {
377 undirect_pci_irq(cfg->intline);
382 * PCI interrupts might be redirected to the
383 * ISA bus according to some MP tables. Use the
384 * same methods as used by the ISA devices
385 * devices to find the proper IOAPIC int pin.
387 airq = isa_apic_irq(cfg->intline);
388 if ((airq >= 0) && (airq != cfg->intline)) {
389 /* XXX: undirect_pci_irq() ? */
390 undirect_isa_irq(cfg->intline);
397 cfg->mingnt = pci_cfgread(cfg, PCIR_MINGNT, 1);
398 cfg->maxlat = pci_cfgread(cfg, PCIR_MAXLAT, 1);
400 cfg->mfdev = (cfg->hdrtype & PCIM_MFDEV) != 0;
401 cfg->hdrtype &= ~PCIM_MFDEV;
404 pci_hdrtypedata(cfg);
406 if (REG(PCIR_STATUS, 2) & PCIM_STATUS_CAPPRESENT)
407 pci_read_extcap(cfg);
409 STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links);
411 devlist_entry->conf.pc_sel.pc_bus = cfg->bus;
412 devlist_entry->conf.pc_sel.pc_dev = cfg->slot;
413 devlist_entry->conf.pc_sel.pc_func = cfg->func;
414 devlist_entry->conf.pc_hdr = cfg->hdrtype;
416 devlist_entry->conf.pc_subvendor = cfg->subvendor;
417 devlist_entry->conf.pc_subdevice = cfg->subdevice;
418 devlist_entry->conf.pc_vendor = cfg->vendor;
419 devlist_entry->conf.pc_device = cfg->device;
421 devlist_entry->conf.pc_class = cfg->baseclass;
422 devlist_entry->conf.pc_subclass = cfg->subclass;
423 devlist_entry->conf.pc_progif = cfg->progif;
424 devlist_entry->conf.pc_revid = cfg->revid;
429 return (devlist_entry);
434 pci_read_extcap(pcicfgregs *cfg)
436 #define REG(n, w) pci_cfgread(cfg, n, w)
437 int ptr, nextptr, ptrptr;
439 switch (cfg->hdrtype) {
447 return; /* no extended capabilities support */
449 nextptr = REG(ptrptr, 1); /* sanity check? */
452 * Read capability entries.
454 while (nextptr != 0) {
457 printf("illegal PCI extended capability offset %d\n",
461 /* Find the next entry */
463 nextptr = REG(ptr + 1, 1);
465 /* Process this entry */
466 switch (REG(ptr, 1)) {
467 case 0x01: /* PCI power management */
468 if (cfg->pp_cap == 0) {
469 cfg->pp_cap = REG(ptr + PCIR_POWER_CAP, 2);
470 cfg->pp_status = ptr + PCIR_POWER_STATUS;
471 cfg->pp_pmcsr = ptr + PCIR_POWER_PMCSR;
472 if ((nextptr - ptr) > PCIR_POWER_DATA)
473 cfg->pp_data = ptr + PCIR_POWER_DATA;
484 /* free pcicfgregs structure and all depending data structures */
487 pci_freecfg(struct pci_devinfo *dinfo)
489 struct devlist *devlist_head;
491 devlist_head = &pci_devq;
493 if (dinfo->cfg.hdrspec != NULL)
494 free(dinfo->cfg.hdrspec, M_DEVBUF);
495 if (dinfo->cfg.map != NULL)
496 free(dinfo->cfg.map, M_DEVBUF);
497 /* XXX this hasn't been tested */
498 STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links);
499 free(dinfo, M_DEVBUF);
501 /* increment the generation count */
504 /* we're losing one device */
512 * PCI power manangement
515 pci_set_powerstate_method(device_t dev, device_t child, int state)
517 struct pci_devinfo *dinfo = device_get_ivars(child);
518 pcicfgregs *cfg = &dinfo->cfg;
522 if (cfg->pp_cap != 0) {
523 status = PCI_READ_CONFIG(dev, child, cfg->pp_status, 2) & ~PCIM_PSTAT_DMASK;
526 case PCI_POWERSTATE_D0:
527 status |= PCIM_PSTAT_D0;
529 case PCI_POWERSTATE_D1:
530 if (cfg->pp_cap & PCIM_PCAP_D1SUPP) {
531 status |= PCIM_PSTAT_D1;
536 case PCI_POWERSTATE_D2:
537 if (cfg->pp_cap & PCIM_PCAP_D2SUPP) {
538 status |= PCIM_PSTAT_D2;
543 case PCI_POWERSTATE_D3:
544 status |= PCIM_PSTAT_D3;
550 PCI_WRITE_CONFIG(dev, child, cfg->pp_status, status, 2);
558 pci_get_powerstate_method(device_t dev, device_t child)
560 struct pci_devinfo *dinfo = device_get_ivars(child);
561 pcicfgregs *cfg = &dinfo->cfg;
565 if (cfg->pp_cap != 0) {
566 status = PCI_READ_CONFIG(dev, child, cfg->pp_status, 2);
567 switch (status & PCIM_PSTAT_DMASK) {
569 result = PCI_POWERSTATE_D0;
572 result = PCI_POWERSTATE_D1;
575 result = PCI_POWERSTATE_D2;
578 result = PCI_POWERSTATE_D3;
581 result = PCI_POWERSTATE_UNKNOWN;
585 /* No support, device is always at D0 */
586 result = PCI_POWERSTATE_D0;
592 * Some convenience functions for PCI device drivers.
596 pci_set_command_bit(device_t dev, device_t child, u_int16_t bit)
600 command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
602 PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
606 pci_clear_command_bit(device_t dev, device_t child, u_int16_t bit)
610 command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
612 PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
616 pci_enable_busmaster_method(device_t dev, device_t child)
618 pci_set_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
622 pci_disable_busmaster_method(device_t dev, device_t child)
624 pci_clear_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
628 pci_enable_io_method(device_t dev, device_t child, int space)
632 pci_set_command_bit(dev, child, PCIM_CMD_PORTEN);
635 pci_set_command_bit(dev, child, PCIM_CMD_MEMEN);
641 pci_disable_io_method(device_t dev, device_t child, int space)
645 pci_clear_command_bit(dev, child, PCIM_CMD_PORTEN);
648 pci_clear_command_bit(dev, child, PCIM_CMD_MEMEN);
654 * This is the user interface to PCI configuration space.
658 pci_open(dev_t dev, int oflags, int devtype, struct thread *td)
660 if ((oflags & FWRITE) && securelevel > 0) {
667 pci_close(dev_t dev, int flag, int devtype, struct thread *td)
673 * Match a single pci_conf structure against an array of pci_match_conf
674 * structures. The first argument, 'matches', is an array of num_matches
675 * pci_match_conf structures. match_buf is a pointer to the pci_conf
676 * structure that will be compared to every entry in the matches array.
677 * This function returns 1 on failure, 0 on success.
680 pci_conf_match(struct pci_match_conf *matches, int num_matches,
681 struct pci_conf *match_buf)
685 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
688 for (i = 0; i < num_matches; i++) {
690 * I'm not sure why someone would do this...but...
692 if (matches[i].flags == PCI_GETCONF_NO_MATCH)
696 * Look at each of the match flags. If it's set, do the
697 * comparison. If the comparison fails, we don't have a
698 * match, go on to the next item if there is one.
700 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
701 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
704 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
705 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
708 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
709 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
712 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
713 && (match_buf->pc_vendor != matches[i].pc_vendor))
716 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
717 && (match_buf->pc_device != matches[i].pc_device))
720 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
721 && (match_buf->pc_class != matches[i].pc_class))
724 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
725 && (match_buf->pd_unit != matches[i].pd_unit))
728 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
729 && (strncmp(matches[i].pd_name, match_buf->pd_name,
730 sizeof(match_buf->pd_name)) != 0))
740 * Locate the parent of a PCI device by scanning the PCI devlist
741 * and return the entry for the parent.
742 * For devices on PCI Bus 0 (the host bus), this is the PCI Host.
743 * For devices on secondary PCI busses, this is that bus' PCI-PCI Bridge.
747 pci_devlist_get_parent(pcicfgregs *cfg)
749 struct devlist *devlist_head;
750 struct pci_devinfo *dinfo;
751 pcicfgregs *bridge_cfg;
754 dinfo = STAILQ_FIRST(devlist_head = &pci_devq);
756 /* If the device is on PCI bus 0, look for the host */
758 for (i = 0; (dinfo != NULL) && (i < pci_numdevs);
759 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
760 bridge_cfg = &dinfo->cfg;
761 if (bridge_cfg->baseclass == PCIC_BRIDGE
762 && bridge_cfg->subclass == PCIS_BRIDGE_HOST
763 && bridge_cfg->bus == cfg->bus) {
769 /* If the device is not on PCI bus 0, look for the PCI-PCI bridge */
771 for (i = 0; (dinfo != NULL) && (i < pci_numdevs);
772 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
773 bridge_cfg = &dinfo->cfg;
774 if (bridge_cfg->baseclass == PCIC_BRIDGE
775 && bridge_cfg->subclass == PCIS_BRIDGE_PCI
776 && bridge_cfg->secondarybus == cfg->bus) {
786 pci_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
792 if (!(flag & FWRITE))
799 struct pci_devinfo *dinfo;
800 struct pci_conf_io *cio;
801 struct devlist *devlist_head;
802 struct pci_match_conf *pattern_buf;
807 cio = (struct pci_conf_io *)data;
813 * Hopefully the user won't pass in a null pointer, but it
814 * can't hurt to check.
822 * If the user specified an offset into the device list,
823 * but the list has changed since they last called this
824 * ioctl, tell them that the list has changed. They will
825 * have to get the list from the beginning.
827 if ((cio->offset != 0)
828 && (cio->generation != pci_generation)){
829 cio->num_matches = 0;
830 cio->status = PCI_GETCONF_LIST_CHANGED;
836 * Check to see whether the user has asked for an offset
837 * past the end of our list.
839 if (cio->offset >= pci_numdevs) {
840 cio->num_matches = 0;
841 cio->status = PCI_GETCONF_LAST_DEVICE;
846 /* get the head of the device queue */
847 devlist_head = &pci_devq;
850 * Determine how much room we have for pci_conf structures.
851 * Round the user's buffer size down to the nearest
852 * multiple of sizeof(struct pci_conf) in case the user
853 * didn't specify a multiple of that size.
855 iolen = min(cio->match_buf_len -
856 (cio->match_buf_len % sizeof(struct pci_conf)),
857 pci_numdevs * sizeof(struct pci_conf));
860 * Since we know that iolen is a multiple of the size of
861 * the pciconf union, it's okay to do this.
863 ionum = iolen / sizeof(struct pci_conf);
866 * If this test is true, the user wants the pci_conf
867 * structures returned to match the supplied entries.
869 if ((cio->num_patterns > 0)
870 && (cio->pat_buf_len > 0)) {
872 * pat_buf_len needs to be:
873 * num_patterns * sizeof(struct pci_match_conf)
874 * While it is certainly possible the user just
875 * allocated a large buffer, but set the number of
876 * matches correctly, it is far more likely that
877 * their kernel doesn't match the userland utility
878 * they're using. It's also possible that the user
879 * forgot to initialize some variables. Yes, this
880 * may be overly picky, but I hazard to guess that
881 * it's far more likely to just catch folks that
882 * updated their kernel but not their userland.
884 if ((cio->num_patterns *
885 sizeof(struct pci_match_conf)) != cio->pat_buf_len){
886 /* The user made a mistake, return an error*/
887 cio->status = PCI_GETCONF_ERROR;
888 printf("pci_ioctl: pat_buf_len %d != "
889 "num_patterns (%d) * sizeof(struct "
890 "pci_match_conf) (%d)\npci_ioctl: "
891 "pat_buf_len should be = %d\n",
892 cio->pat_buf_len, cio->num_patterns,
893 (int)sizeof(struct pci_match_conf),
894 (int)sizeof(struct pci_match_conf) *
896 printf("pci_ioctl: do your headers match your "
898 cio->num_matches = 0;
904 * Check the user's buffer to make sure it's readable.
906 if (!useracc((caddr_t)cio->patterns,
907 cio->pat_buf_len, VM_PROT_READ)) {
908 printf("pci_ioctl: pattern buffer %p, "
909 "length %u isn't user accessible for"
910 " READ\n", cio->patterns,
916 * Allocate a buffer to hold the patterns.
918 pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
920 error = copyin(cio->patterns, pattern_buf,
924 num_patterns = cio->num_patterns;
926 } else if ((cio->num_patterns > 0)
927 || (cio->pat_buf_len > 0)) {
929 * The user made a mistake, spit out an error.
931 cio->status = PCI_GETCONF_ERROR;
932 cio->num_matches = 0;
933 printf("pci_ioctl: invalid GETCONF arguments\n");
940 * Make sure we can write to the match buffer.
942 if (!useracc((caddr_t)cio->matches,
943 cio->match_buf_len, VM_PROT_WRITE)) {
944 printf("pci_ioctl: match buffer %p, length %u "
945 "isn't user accessible for WRITE\n",
946 cio->matches, cio->match_buf_len);
952 * Go through the list of devices and copy out the devices
953 * that match the user's criteria.
955 for (cio->num_matches = 0, error = 0, i = 0,
956 dinfo = STAILQ_FIRST(devlist_head);
957 (dinfo != NULL) && (cio->num_matches < ionum)
958 && (error == 0) && (i < pci_numdevs);
959 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
964 /* Populate pd_name and pd_unit */
966 if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0')
967 name = device_get_name(dinfo->cfg.dev);
969 strncpy(dinfo->conf.pd_name, name,
970 sizeof(dinfo->conf.pd_name));
971 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
972 dinfo->conf.pd_unit =
973 device_get_unit(dinfo->cfg.dev);
976 if ((pattern_buf == NULL) ||
977 (pci_conf_match(pattern_buf, num_patterns,
978 &dinfo->conf) == 0)) {
981 * If we've filled up the user's buffer,
982 * break out at this point. Since we've
983 * got a match here, we'll pick right back
984 * up at the matching entry. We can also
985 * tell the user that there are more matches
988 if (cio->num_matches >= ionum)
991 error = copyout(&dinfo->conf,
992 &cio->matches[cio->num_matches],
993 sizeof(struct pci_conf));
999 * Set the pointer into the list, so if the user is getting
1000 * n records at a time, where n < pci_numdevs,
1005 * Set the generation, the user will need this if they make
1006 * another ioctl call with offset != 0.
1008 cio->generation = pci_generation;
1011 * If this is the last device, inform the user so he won't
1012 * bother asking for more devices. If dinfo isn't NULL, we
1013 * know that there are more matches in the list because of
1014 * the way the traversal is done.
1017 cio->status = PCI_GETCONF_LAST_DEVICE;
1019 cio->status = PCI_GETCONF_MORE_DEVS;
1021 if (pattern_buf != NULL)
1022 free(pattern_buf, M_TEMP);
1027 io = (struct pci_io *)data;
1028 switch(io->pi_width) {
1034 probe.bus = io->pi_sel.pc_bus;
1035 probe.slot = io->pi_sel.pc_dev;
1036 probe.func = io->pi_sel.pc_func;
1037 io->pi_data = pci_cfgread(&probe,
1038 io->pi_reg, io->pi_width);
1048 io = (struct pci_io *)data;
1049 switch(io->pi_width) {
1055 probe.bus = io->pi_sel.pc_bus;
1056 probe.slot = io->pi_sel.pc_dev;
1057 probe.func = io->pi_sel.pc_func;
1058 pci_cfgwrite(&probe,
1059 io->pi_reg, io->pi_data, io->pi_width);
1078 static struct cdevsw pcicdev = {
1079 /* open */ pci_open,
1080 /* close */ pci_close,
1082 /* write */ nowrite,
1083 /* ioctl */ pci_ioctl,
1086 /* strategy */ nostrategy,
1090 /* psize */ nopsize,
1098 * A simple driver to wrap the old pci driver mechanism for back-compat.
1102 pci_compat_probe(device_t dev)
1104 struct pci_device *dvp;
1105 struct pci_devinfo *dinfo;
1110 dinfo = device_get_ivars(dev);
1112 dvp = device_get_driver(dev)->priv;
1115 * Do the wrapped probe.
1118 if (dvp && dvp->pd_probe) {
1119 name = dvp->pd_probe(cfg, (cfg->device << 16) + cfg->vendor);
1121 device_set_desc_copy(dev, name);
1122 /* Allow newbus drivers to match "better" */
1131 pci_compat_attach(device_t dev)
1133 struct pci_device *dvp;
1134 struct pci_devinfo *dinfo;
1138 dinfo = device_get_ivars(dev);
1140 dvp = device_get_driver(dev)->priv;
1142 unit = device_get_unit(dev);
1143 if (unit > *dvp->pd_count)
1144 *dvp->pd_count = unit;
1146 dvp->pd_attach(cfg, unit);
1147 device_printf(dev, "driver is using old-style compatibility shims\n");
1151 static device_method_t pci_compat_methods[] = {
1152 /* Device interface */
1153 DEVMETHOD(device_probe, pci_compat_probe),
1154 DEVMETHOD(device_attach, pci_compat_attach),
1159 static devclass_t pci_devclass;
1162 * Create a new style driver around each old pci driver.
1165 compat_pci_handler(module_t mod, int type, void *data)
1167 struct pci_device *dvp = (struct pci_device *)data;
1172 driver = malloc(sizeof(driver_t), M_DEVBUF, M_NOWAIT);
1175 bzero(driver, sizeof(driver_t));
1176 driver->name = dvp->pd_name;
1177 driver->methods = pci_compat_methods;
1178 driver->softc = sizeof(struct pci_devinfo *);
1180 devclass_add_driver(pci_devclass, driver);
1183 printf("%s: module unload not supported!\n", dvp->pd_name);
1192 * New style pci driver. Parent device is either a pci-host-bridge or a
1193 * pci-pci-bridge. Both kinds are represented by instances of pcib.
1197 pci_print_verbose(struct pci_devinfo *dinfo)
1200 pcicfgregs *cfg = &dinfo->cfg;
1202 printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n",
1203 cfg->vendor, cfg->device, cfg->revid);
1204 printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n",
1205 cfg->baseclass, cfg->subclass, cfg->progif,
1206 cfg->hdrtype, cfg->mfdev);
1207 printf("\tsubordinatebus=%x \tsecondarybus=%x\n",
1208 cfg->subordinatebus, cfg->secondarybus);
1210 printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n",
1211 cfg->cmdreg, cfg->statreg, cfg->cachelnsz);
1212 printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n",
1213 cfg->lattimer, cfg->lattimer * 30,
1214 cfg->mingnt, cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250);
1215 #endif /* PCI_DEBUG */
1216 if (cfg->intpin > 0)
1217 printf("\tintpin=%c, irq=%d\n", cfg->intpin +'a' -1, cfg->intline);
1222 pci_porten(pcicfgregs *cfg)
1224 return ((cfg->cmdreg & PCIM_CMD_PORTEN) != 0);
1228 pci_memen(pcicfgregs *cfg)
1230 return ((cfg->cmdreg & PCIM_CMD_MEMEN) != 0);
1234 * Add a resource based on a pci map register. Return 1 if the map
1235 * register is a 32bit map register or 2 if it is a 64bit register.
1238 pci_add_map(device_t dev, pcicfgregs* cfg, int reg)
1240 struct pci_devinfo *dinfo = device_get_ivars(dev);
1241 struct resource_list *rl = &dinfo->resources;
1250 map = pci_cfgread(cfg, reg, 4);
1252 if (map == 0 || map == 0xffffffff)
1253 return 1; /* skip invalid entry */
1255 pci_cfgwrite(cfg, reg, 0xffffffff, 4);
1256 testval = pci_cfgread(cfg, reg, 4);
1257 pci_cfgwrite(cfg, reg, map, 4);
1259 base = pci_mapbase(map);
1260 if (pci_maptype(map) & PCI_MAPMEM)
1261 type = SYS_RES_MEMORY;
1263 type = SYS_RES_IOPORT;
1264 ln2size = pci_mapsize(testval);
1265 ln2range = pci_maprange(testval);
1266 if (ln2range == 64) {
1267 /* Read the other half of a 64bit map register */
1268 base |= (u_int64_t) pci_cfgread(cfg, reg + 4, 4) << 32;
1273 * XXX: encode hose number in the base addr,
1274 * This will go away once the bus_space functions
1275 * can deal with multiple hoses
1279 u_int32_t mask, shift, maxh;
1281 switch (hwrpb->rpb_type) {
1286 maxh = 4; /* not a hose. MCPCIA instance # */
1305 printf("base addr = 0x%llx\n", (long long) base);
1306 printf("mask addr = 0x%lx\n", (long) mask);
1307 printf("hacked addr = 0x%llx\n", (long long)
1308 (base | ((u_int64_t)cfg->hose << shift)));
1309 panic("hose encoding hack would clobber base addr");
1312 if (cfg->hose >= maxh) {
1313 panic("Hose %d - can only encode %d hose(s)",
1317 base |= ((u_int64_t)cfg->hose << shift);
1322 * This code theoretically does the right thing, but has
1323 * undesirable side effects in some cases where
1324 * peripherals respond oddly to having these bits
1325 * enabled. Leave them alone by default.
1327 #ifdef PCI_ENABLE_IO_MODES
1328 if (type == SYS_RES_IOPORT && !pci_porten(cfg)) {
1329 cfg->cmdreg |= PCIM_CMD_PORTEN;
1330 pci_cfgwrite(cfg, PCIR_COMMAND, cfg->cmdreg, 2);
1332 if (type == SYS_RES_MEMORY && !pci_memen(cfg)) {
1333 cfg->cmdreg |= PCIM_CMD_MEMEN;
1334 pci_cfgwrite(cfg, PCIR_COMMAND, cfg->cmdreg, 2);
1337 if (type == SYS_RES_IOPORT && !pci_porten(cfg))
1339 if (type == SYS_RES_MEMORY && !pci_memen(cfg))
1343 resource_list_add(rl, type, reg,
1344 base, base + (1 << ln2size) - 1,
1348 printf("\tmap[%02x]: type %x, range %2d, base %08x, size %2d\n",
1349 reg, pci_maptype(base), ln2range,
1350 (unsigned int) base, ln2size);
1353 return (ln2range == 64) ? 2 : 1;
1357 pci_add_resources(device_t dev, pcicfgregs* cfg)
1359 struct pci_devinfo *dinfo = device_get_ivars(dev);
1360 struct resource_list *rl = &dinfo->resources;
1361 struct pci_quirk *q;
1364 for (i = 0; i < cfg->nummaps;) {
1365 i += pci_add_map(dev, cfg, PCIR_MAPS + i*4);
1368 for (q = &pci_quirks[0]; q->devid; q++) {
1369 if (q->devid == ((cfg->device << 16) | cfg->vendor)
1370 && q->type == PCI_QUIRK_MAP_REG)
1371 pci_add_map(dev, cfg, q->arg1);
1374 if (cfg->intpin > 0 && cfg->intline != 255)
1375 resource_list_add(rl, SYS_RES_IRQ, 0,
1376 cfg->intline, cfg->intline, 1);
1380 pci_add_children(device_t dev, int busno)
1386 #define PCI_SLOTMAX 0
1389 bzero(&probe, sizeof probe);
1391 probe.hose = pcib_get_hose(dev);
1398 for (probe.slot = 0; probe.slot <= PCI_SLOTMAX; probe.slot++) {
1399 int pcifunchigh = 0;
1400 for (probe.func = 0; probe.func <= pcifunchigh; probe.func++) {
1401 struct pci_devinfo *dinfo = pci_readcfg(&probe);
1402 if (dinfo != NULL) {
1403 if (dinfo->cfg.mfdev)
1406 pci_print_verbose(dinfo);
1407 dinfo->cfg.dev = device_add_child(dev, NULL, -1);
1408 device_set_ivars(dinfo->cfg.dev, dinfo);
1409 pci_add_resources(dinfo->cfg.dev, &dinfo->cfg);
1416 pci_new_probe(device_t dev)
1420 device_set_desc(dev, "PCI bus");
1421 pci_add_children(dev, device_get_unit(dev));
1423 make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644, "pci");
1431 pci_print_resources(struct resource_list *rl, const char *name, int type,
1434 struct resource_list_entry *rle;
1435 int printed, retval;
1439 /* Yes, this is kinda cheating */
1440 SLIST_FOREACH(rle, rl, link) {
1441 if (rle->type == type) {
1443 retval += printf(" %s ", name);
1444 else if (printed > 0)
1445 retval += printf(",");
1447 retval += printf(format, rle->start);
1448 if (rle->count > 1) {
1449 retval += printf("-");
1450 retval += printf(format, rle->start +
1459 pci_print_child(device_t dev, device_t child)
1461 struct pci_devinfo *dinfo;
1462 struct resource_list *rl;
1466 dinfo = device_get_ivars(child);
1468 rl = &dinfo->resources;
1470 retval += bus_print_child_header(dev, child);
1472 retval += pci_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
1473 retval += pci_print_resources(rl, "mem", SYS_RES_MEMORY, "%#lx");
1474 retval += pci_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
1475 if (device_get_flags(dev))
1476 retval += printf(" flags %#x", device_get_flags(dev));
1478 retval += printf(" at device %d.%d", pci_get_slot(child),
1479 pci_get_function(child));
1481 retval += bus_print_child_footer(dev, child);
1487 pci_probe_nomatch(device_t dev, device_t child)
1489 struct pci_devinfo *dinfo;
1495 dinfo = device_get_ivars(child);
1497 desc = pci_ata_match(child);
1498 if (!desc) desc = pci_usb_match(child);
1499 if (!desc) desc = pci_vga_match(child);
1501 desc = "unknown card";
1504 device_printf(dev, "<%s>", desc);
1505 if (bootverbose || unknown) {
1506 printf(" (vendor=0x%04x, dev=0x%04x)",
1511 pci_get_slot(child),
1512 pci_get_function(child));
1513 if (cfg->intpin > 0 && cfg->intline != 255) {
1514 printf(" irq %d", cfg->intline);
1522 pci_read_ivar(device_t dev, device_t child, int which, u_long *result)
1524 struct pci_devinfo *dinfo;
1527 dinfo = device_get_ivars(child);
1531 case PCI_IVAR_SUBVENDOR:
1532 *result = cfg->subvendor;
1534 case PCI_IVAR_SUBDEVICE:
1535 *result = cfg->subdevice;
1537 case PCI_IVAR_VENDOR:
1538 *result = cfg->vendor;
1540 case PCI_IVAR_DEVICE:
1541 *result = cfg->device;
1543 case PCI_IVAR_DEVID:
1544 *result = (cfg->device << 16) | cfg->vendor;
1546 case PCI_IVAR_CLASS:
1547 *result = cfg->baseclass;
1549 case PCI_IVAR_SUBCLASS:
1550 *result = cfg->subclass;
1552 case PCI_IVAR_PROGIF:
1553 *result = cfg->progif;
1555 case PCI_IVAR_REVID:
1556 *result = cfg->revid;
1558 case PCI_IVAR_INTPIN:
1559 *result = cfg->intpin;
1562 *result = cfg->intline;
1568 *result = cfg->slot;
1570 case PCI_IVAR_FUNCTION:
1571 *result = cfg->func;
1573 case PCI_IVAR_SECONDARYBUS:
1574 *result = cfg->secondarybus;
1576 case PCI_IVAR_SUBORDINATEBUS:
1577 *result = cfg->subordinatebus;
1581 * Pass up to parent bridge.
1583 *result = pcib_get_hose(dev);
1592 pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1594 struct pci_devinfo *dinfo;
1597 dinfo = device_get_ivars(child);
1601 case PCI_IVAR_SUBVENDOR:
1602 case PCI_IVAR_SUBDEVICE:
1603 case PCI_IVAR_VENDOR:
1604 case PCI_IVAR_DEVICE:
1605 case PCI_IVAR_DEVID:
1606 case PCI_IVAR_CLASS:
1607 case PCI_IVAR_SUBCLASS:
1608 case PCI_IVAR_PROGIF:
1609 case PCI_IVAR_REVID:
1610 case PCI_IVAR_INTPIN:
1614 case PCI_IVAR_FUNCTION:
1615 return EINVAL; /* disallow for now */
1617 case PCI_IVAR_SECONDARYBUS:
1618 cfg->secondarybus = value;
1620 case PCI_IVAR_SUBORDINATEBUS:
1621 cfg->subordinatebus = value;
1629 static struct resource *
1630 pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
1631 u_long start, u_long end, u_long count, u_int flags)
1633 struct pci_devinfo *dinfo = device_get_ivars(child);
1634 struct resource_list *rl = &dinfo->resources;
1636 #ifdef __i386__ /* Only supported on x86 in stable */
1637 pcicfgregs *cfg = &dinfo->cfg;
1639 * Perform lazy resource allocation
1641 * XXX add support here for SYS_RES_IOPORT and SYS_RES_MEMORY
1643 if (device_get_parent(child) == dev) {
1645 * If device doesn't have an interrupt routed, and is
1646 * deserving of an interrupt, try to assign it one.
1648 if ((type == SYS_RES_IRQ) &&
1649 (cfg->intline == 255 || cfg->intline == 0) &&
1650 (cfg->intpin != 0) && (start == 0) && (end == ~0UL)) {
1651 cfg->intline = pci_cfgintr(pci_get_bus(child),
1652 pci_get_slot(child), cfg->intpin);
1653 if (cfg->intline != 255) {
1654 pci_write_config(child, PCIR_INTLINE,
1656 resource_list_add(rl, SYS_RES_IRQ, 0,
1657 cfg->intline, cfg->intline, 1);
1662 return resource_list_alloc(rl, dev, child, type, rid,
1663 start, end, count, flags);
1667 pci_release_resource(device_t dev, device_t child, int type, int rid,
1670 struct pci_devinfo *dinfo = device_get_ivars(child);
1671 struct resource_list *rl = &dinfo->resources;
1673 return resource_list_release(rl, dev, child, type, rid, r);
1677 pci_set_resource(device_t dev, device_t child, int type, int rid,
1678 u_long start, u_long count)
1680 struct pci_devinfo *dinfo = device_get_ivars(child);
1681 struct resource_list *rl = &dinfo->resources;
1683 resource_list_add(rl, type, rid, start, start + count - 1, count);
1688 pci_get_resource(device_t dev, device_t child, int type, int rid,
1689 u_long *startp, u_long *countp)
1691 struct pci_devinfo *dinfo = device_get_ivars(child);
1692 struct resource_list *rl = &dinfo->resources;
1693 struct resource_list_entry *rle;
1695 rle = resource_list_find(rl, type, rid);
1700 *startp = rle->start;
1702 *countp = rle->count;
1708 pci_delete_resource(device_t dev, device_t child, int type, int rid)
1710 printf("pci_delete_resource: PCI resources can not be deleted\n");
1714 pci_read_config_method(device_t dev, device_t child, int reg, int width)
1716 struct pci_devinfo *dinfo = device_get_ivars(child);
1717 pcicfgregs *cfg = &dinfo->cfg;
1718 return pci_cfgread(cfg, reg, width);
1722 pci_write_config_method(device_t dev, device_t child, int reg,
1723 u_int32_t val, int width)
1725 struct pci_devinfo *dinfo = device_get_ivars(child);
1726 pcicfgregs *cfg = &dinfo->cfg;
1727 pci_cfgwrite(cfg, reg, val, width);
1731 pci_modevent(module_t mod, int what, void *arg)
1735 STAILQ_INIT(&pci_devq);
1745 static device_method_t pci_methods[] = {
1746 /* Device interface */
1747 DEVMETHOD(device_probe, pci_new_probe),
1748 DEVMETHOD(device_attach, bus_generic_attach),
1749 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1750 DEVMETHOD(device_suspend, bus_generic_suspend),
1751 DEVMETHOD(device_resume, bus_generic_resume),
1754 DEVMETHOD(bus_print_child, pci_print_child),
1755 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch),
1756 DEVMETHOD(bus_read_ivar, pci_read_ivar),
1757 DEVMETHOD(bus_write_ivar, pci_write_ivar),
1758 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1759 DEVMETHOD(bus_alloc_resource, pci_alloc_resource),
1760 DEVMETHOD(bus_release_resource, pci_release_resource),
1761 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1762 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1763 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
1764 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
1765 DEVMETHOD(bus_set_resource, pci_set_resource),
1766 DEVMETHOD(bus_get_resource, pci_get_resource),
1767 DEVMETHOD(bus_delete_resource, pci_delete_resource),
1770 DEVMETHOD(pci_read_config, pci_read_config_method),
1771 DEVMETHOD(pci_write_config, pci_write_config_method),
1772 DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method),
1773 DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
1774 DEVMETHOD(pci_enable_io, pci_enable_io_method),
1775 DEVMETHOD(pci_disable_io, pci_disable_io_method),
1776 DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method),
1777 DEVMETHOD(pci_set_powerstate, pci_set_powerstate_method),
1782 static driver_t pci_driver = {
1788 DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, 0);