2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/linker.h>
39 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/endian.h>
48 #include <vm/vm_extern.h>
51 #include <machine/bus.h>
53 #include <machine/resource.h>
54 #include <machine/stdarg.h>
56 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
57 #include <machine/intr_machdep.h>
60 #include <sys/pciio.h>
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pci_private.h>
65 #include <dev/usb/controller/ehcireg.h>
66 #include <dev/usb/controller/ohcireg.h>
67 #include <dev/usb/controller/uhcireg.h>
73 #include <contrib/dev/acpica/include/acpi.h>
76 #define ACPI_PWR_FOR_SLEEP(x, y, z)
79 static pci_addr_t pci_mapbase(uint64_t mapreg);
80 static const char *pci_maptype(uint64_t mapreg);
81 static int pci_mapsize(uint64_t testval);
82 static int pci_maprange(uint64_t mapreg);
83 static void pci_fixancient(pcicfgregs *cfg);
84 static int pci_printf(pcicfgregs *cfg, const char *fmt, ...);
86 static int pci_porten(device_t dev);
87 static int pci_memen(device_t dev);
88 static void pci_assign_interrupt(device_t bus, device_t dev,
90 static int pci_add_map(device_t bus, device_t dev, int reg,
91 struct resource_list *rl, int force, int prefetch);
92 static int pci_probe(device_t dev);
93 static int pci_attach(device_t dev);
94 static void pci_load_vendor_data(void);
95 static int pci_describe_parse_line(char **ptr, int *vendor,
96 int *device, char **desc);
97 static char *pci_describe_device(device_t dev);
98 static int pci_modevent(module_t mod, int what, void *arg);
99 static void pci_hdrtypedata(device_t pcib, int b, int s, int f,
101 static void pci_read_extcap(device_t pcib, pcicfgregs *cfg);
102 static int pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg,
103 int reg, uint32_t *data);
105 static int pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg,
106 int reg, uint32_t data);
108 static void pci_read_vpd(device_t pcib, pcicfgregs *cfg);
109 static void pci_disable_msi(device_t dev);
110 static void pci_enable_msi(device_t dev, uint64_t address,
112 static void pci_enable_msix(device_t dev, u_int index,
113 uint64_t address, uint32_t data);
114 static void pci_mask_msix(device_t dev, u_int index);
115 static void pci_unmask_msix(device_t dev, u_int index);
116 static int pci_msi_blacklisted(void);
117 static void pci_resume_msi(device_t dev);
118 static void pci_resume_msix(device_t dev);
119 static int pci_remap_intr_method(device_t bus, device_t dev,
122 static device_method_t pci_methods[] = {
123 /* Device interface */
124 DEVMETHOD(device_probe, pci_probe),
125 DEVMETHOD(device_attach, pci_attach),
126 DEVMETHOD(device_detach, bus_generic_detach),
127 DEVMETHOD(device_shutdown, bus_generic_shutdown),
128 DEVMETHOD(device_suspend, pci_suspend),
129 DEVMETHOD(device_resume, pci_resume),
132 DEVMETHOD(bus_print_child, pci_print_child),
133 DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch),
134 DEVMETHOD(bus_read_ivar, pci_read_ivar),
135 DEVMETHOD(bus_write_ivar, pci_write_ivar),
136 DEVMETHOD(bus_driver_added, pci_driver_added),
137 DEVMETHOD(bus_setup_intr, pci_setup_intr),
138 DEVMETHOD(bus_teardown_intr, pci_teardown_intr),
140 DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
141 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
142 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
143 DEVMETHOD(bus_delete_resource, pci_delete_resource),
144 DEVMETHOD(bus_alloc_resource, pci_alloc_resource),
145 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
146 DEVMETHOD(bus_activate_resource, pci_activate_resource),
147 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
148 DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
149 DEVMETHOD(bus_child_location_str, pci_child_location_str_method),
150 DEVMETHOD(bus_remap_intr, pci_remap_intr_method),
153 DEVMETHOD(pci_read_config, pci_read_config_method),
154 DEVMETHOD(pci_write_config, pci_write_config_method),
155 DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method),
156 DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
157 DEVMETHOD(pci_enable_io, pci_enable_io_method),
158 DEVMETHOD(pci_disable_io, pci_disable_io_method),
159 DEVMETHOD(pci_get_vpd_ident, pci_get_vpd_ident_method),
160 DEVMETHOD(pci_get_vpd_readonly, pci_get_vpd_readonly_method),
161 DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method),
162 DEVMETHOD(pci_set_powerstate, pci_set_powerstate_method),
163 DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method),
164 DEVMETHOD(pci_find_extcap, pci_find_extcap_method),
165 DEVMETHOD(pci_alloc_msi, pci_alloc_msi_method),
166 DEVMETHOD(pci_alloc_msix, pci_alloc_msix_method),
167 DEVMETHOD(pci_remap_msix, pci_remap_msix_method),
168 DEVMETHOD(pci_release_msi, pci_release_msi_method),
169 DEVMETHOD(pci_msi_count, pci_msi_count_method),
170 DEVMETHOD(pci_msix_count, pci_msix_count_method),
175 DEFINE_CLASS_0(pci, pci_driver, pci_methods, 0);
177 static devclass_t pci_devclass;
178 DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, 0);
179 MODULE_VERSION(pci, 1);
181 static char *pci_vendordata;
182 static size_t pci_vendordata_size;
186 uint32_t devid; /* Vendor/device of the card */
188 #define PCI_QUIRK_MAP_REG 1 /* PCI map register in weird place */
189 #define PCI_QUIRK_DISABLE_MSI 2 /* MSI/MSI-X doesn't work */
190 #define PCI_QUIRK_ENABLE_MSI_VM 3 /* Older chipset in VM where MSI works */
195 struct pci_quirk pci_quirks[] = {
196 /* The Intel 82371AB and 82443MX has a map register at offset 0x90. */
197 { 0x71138086, PCI_QUIRK_MAP_REG, 0x90, 0 },
198 { 0x719b8086, PCI_QUIRK_MAP_REG, 0x90, 0 },
199 /* As does the Serverworks OSB4 (the SMBus mapping register) */
200 { 0x02001166, PCI_QUIRK_MAP_REG, 0x90, 0 },
203 * MSI doesn't work with the ServerWorks CNB20-HE Host Bridge
204 * or the CMIC-SL (AKA ServerWorks GC_LE).
206 { 0x00141166, PCI_QUIRK_DISABLE_MSI, 0, 0 },
207 { 0x00171166, PCI_QUIRK_DISABLE_MSI, 0, 0 },
210 * MSI doesn't work on earlier Intel chipsets including
211 * E7500, E7501, E7505, 845, 865, 875/E7210, and 855.
213 { 0x25408086, PCI_QUIRK_DISABLE_MSI, 0, 0 },
214 { 0x254c8086, PCI_QUIRK_DISABLE_MSI, 0, 0 },
215 { 0x25508086, PCI_QUIRK_DISABLE_MSI, 0, 0 },
216 { 0x25608086, PCI_QUIRK_DISABLE_MSI, 0, 0 },
217 { 0x25708086, PCI_QUIRK_DISABLE_MSI, 0, 0 },
218 { 0x25788086, PCI_QUIRK_DISABLE_MSI, 0, 0 },
219 { 0x35808086, PCI_QUIRK_DISABLE_MSI, 0, 0 },
222 * MSI doesn't work with devices behind the AMD 8131 HT-PCIX
225 { 0x74501022, PCI_QUIRK_DISABLE_MSI, 0, 0 },
228 * Some virtualization environments emulate an older chipset
229 * but support MSI just fine. QEMU uses the Intel 82440.
231 { 0x12378086, PCI_QUIRK_ENABLE_MSI_VM, 0, 0 },
236 /* map register information */
237 #define PCI_MAPMEM 0x01 /* memory map */
238 #define PCI_MAPMEMP 0x02 /* prefetchable memory map */
239 #define PCI_MAPPORT 0x04 /* port map */
241 struct devlist pci_devq;
242 uint32_t pci_generation;
243 uint32_t pci_numdevs = 0;
244 static int pcie_chipset, pcix_chipset;
247 SYSCTL_NODE(_hw, OID_AUTO, pci, CTLFLAG_RD, 0, "PCI bus tuning parameters");
249 static int pci_enable_io_modes = 1;
250 TUNABLE_INT("hw.pci.enable_io_modes", &pci_enable_io_modes);
251 SYSCTL_INT(_hw_pci, OID_AUTO, enable_io_modes, CTLFLAG_RW,
252 &pci_enable_io_modes, 1,
253 "Enable I/O and memory bits in the config register. Some BIOSes do not\n\
254 enable these bits correctly. We'd like to do this all the time, but there\n\
255 are some peripherals that this causes problems with.");
257 static int pci_do_power_nodriver = 0;
258 TUNABLE_INT("hw.pci.do_power_nodriver", &pci_do_power_nodriver);
259 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_nodriver, CTLFLAG_RW,
260 &pci_do_power_nodriver, 0,
261 "Place a function into D3 state when no driver attaches to it. 0 means\n\
262 disable. 1 means conservatively place devices into D3 state. 2 means\n\
263 agressively place devices into D3 state. 3 means put absolutely everything\n\
266 static int pci_do_power_resume = 1;
267 TUNABLE_INT("hw.pci.do_power_resume", &pci_do_power_resume);
268 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_resume, CTLFLAG_RW,
269 &pci_do_power_resume, 1,
270 "Transition from D3 -> D0 on resume.");
272 static int pci_do_msi = 1;
273 TUNABLE_INT("hw.pci.enable_msi", &pci_do_msi);
274 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msi, CTLFLAG_RW, &pci_do_msi, 1,
275 "Enable support for MSI interrupts");
277 static int pci_do_msix = 1;
278 TUNABLE_INT("hw.pci.enable_msix", &pci_do_msix);
279 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msix, CTLFLAG_RW, &pci_do_msix, 1,
280 "Enable support for MSI-X interrupts");
282 static int pci_honor_msi_blacklist = 1;
283 TUNABLE_INT("hw.pci.honor_msi_blacklist", &pci_honor_msi_blacklist);
284 SYSCTL_INT(_hw_pci, OID_AUTO, honor_msi_blacklist, CTLFLAG_RD,
285 &pci_honor_msi_blacklist, 1, "Honor chipset blacklist for MSI");
287 #if defined(__i386__) || defined(__amd64__)
288 static int pci_usb_takeover = 1;
290 static int pci_usb_takeover = 0;
292 TUNABLE_INT("hw.pci.usb_early_takeover", &pci_usb_takeover);
293 SYSCTL_INT(_hw_pci, OID_AUTO, usb_early_takeover, CTLFLAG_RD | CTLFLAG_TUN,
294 &pci_usb_takeover, 1, "Enable early takeover of USB controllers.\n\
295 Disable this if you depend on BIOS emulation of USB devices, that is\n\
296 you use USB devices (like keyboard or mouse) but do not load USB drivers");
298 /* Find a device_t by bus/slot/function in domain 0 */
301 pci_find_bsf(uint8_t bus, uint8_t slot, uint8_t func)
304 return (pci_find_dbsf(0, bus, slot, func));
307 /* Find a device_t by domain/bus/slot/function */
310 pci_find_dbsf(uint32_t domain, uint8_t bus, uint8_t slot, uint8_t func)
312 struct pci_devinfo *dinfo;
314 STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
315 if ((dinfo->cfg.domain == domain) &&
316 (dinfo->cfg.bus == bus) &&
317 (dinfo->cfg.slot == slot) &&
318 (dinfo->cfg.func == func)) {
319 return (dinfo->cfg.dev);
326 /* Find a device_t by vendor/device ID */
329 pci_find_device(uint16_t vendor, uint16_t device)
331 struct pci_devinfo *dinfo;
333 STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
334 if ((dinfo->cfg.vendor == vendor) &&
335 (dinfo->cfg.device == device)) {
336 return (dinfo->cfg.dev);
344 pci_printf(pcicfgregs *cfg, const char *fmt, ...)
349 retval = printf("pci%d:%d:%d:%d: ", cfg->domain, cfg->bus, cfg->slot,
352 retval += vprintf(fmt, ap);
357 /* return base address of memory or port map */
360 pci_mapbase(uint64_t mapreg)
363 if (PCI_BAR_MEM(mapreg))
364 return (mapreg & PCIM_BAR_MEM_BASE);
366 return (mapreg & PCIM_BAR_IO_BASE);
369 /* return map type of memory or port map */
372 pci_maptype(uint64_t mapreg)
375 if (PCI_BAR_IO(mapreg))
377 if (mapreg & PCIM_BAR_MEM_PREFETCH)
378 return ("Prefetchable Memory");
382 /* return log2 of map size decoded for memory or port map */
385 pci_mapsize(uint64_t testval)
389 testval = pci_mapbase(testval);
392 while ((testval & 1) == 0)
401 /* return log2 of address range supported by map register */
404 pci_maprange(uint64_t mapreg)
408 if (PCI_BAR_IO(mapreg))
411 switch (mapreg & PCIM_BAR_MEM_TYPE) {
412 case PCIM_BAR_MEM_32:
415 case PCIM_BAR_MEM_1MB:
418 case PCIM_BAR_MEM_64:
425 /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */
428 pci_fixancient(pcicfgregs *cfg)
430 if (cfg->hdrtype != 0)
433 /* PCI to PCI bridges use header type 1 */
434 if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI)
438 /* extract header type specific config data */
441 pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg)
443 #define REG(n, w) PCIB_READ_CONFIG(pcib, b, s, f, n, w)
444 switch (cfg->hdrtype) {
446 cfg->subvendor = REG(PCIR_SUBVEND_0, 2);
447 cfg->subdevice = REG(PCIR_SUBDEV_0, 2);
448 cfg->nummaps = PCI_MAXMAPS_0;
451 cfg->nummaps = PCI_MAXMAPS_1;
454 cfg->subvendor = REG(PCIR_SUBVEND_2, 2);
455 cfg->subdevice = REG(PCIR_SUBDEV_2, 2);
456 cfg->nummaps = PCI_MAXMAPS_2;
462 /* read configuration header into pcicfgregs structure */
464 pci_read_device(device_t pcib, int d, int b, int s, int f, size_t size)
466 #define REG(n, w) PCIB_READ_CONFIG(pcib, b, s, f, n, w)
467 pcicfgregs *cfg = NULL;
468 struct pci_devinfo *devlist_entry;
469 struct devlist *devlist_head;
471 devlist_head = &pci_devq;
473 devlist_entry = NULL;
475 if (REG(PCIR_DEVVENDOR, 4) != 0xfffffffful) {
476 devlist_entry = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
477 if (devlist_entry == NULL)
480 cfg = &devlist_entry->cfg;
486 cfg->vendor = REG(PCIR_VENDOR, 2);
487 cfg->device = REG(PCIR_DEVICE, 2);
488 cfg->cmdreg = REG(PCIR_COMMAND, 2);
489 cfg->statreg = REG(PCIR_STATUS, 2);
490 cfg->baseclass = REG(PCIR_CLASS, 1);
491 cfg->subclass = REG(PCIR_SUBCLASS, 1);
492 cfg->progif = REG(PCIR_PROGIF, 1);
493 cfg->revid = REG(PCIR_REVID, 1);
494 cfg->hdrtype = REG(PCIR_HDRTYPE, 1);
495 cfg->cachelnsz = REG(PCIR_CACHELNSZ, 1);
496 cfg->lattimer = REG(PCIR_LATTIMER, 1);
497 cfg->intpin = REG(PCIR_INTPIN, 1);
498 cfg->intline = REG(PCIR_INTLINE, 1);
500 cfg->mingnt = REG(PCIR_MINGNT, 1);
501 cfg->maxlat = REG(PCIR_MAXLAT, 1);
503 cfg->mfdev = (cfg->hdrtype & PCIM_MFDEV) != 0;
504 cfg->hdrtype &= ~PCIM_MFDEV;
507 pci_hdrtypedata(pcib, b, s, f, cfg);
509 if (REG(PCIR_STATUS, 2) & PCIM_STATUS_CAPPRESENT)
510 pci_read_extcap(pcib, cfg);
512 STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links);
514 devlist_entry->conf.pc_sel.pc_domain = cfg->domain;
515 devlist_entry->conf.pc_sel.pc_bus = cfg->bus;
516 devlist_entry->conf.pc_sel.pc_dev = cfg->slot;
517 devlist_entry->conf.pc_sel.pc_func = cfg->func;
518 devlist_entry->conf.pc_hdr = cfg->hdrtype;
520 devlist_entry->conf.pc_subvendor = cfg->subvendor;
521 devlist_entry->conf.pc_subdevice = cfg->subdevice;
522 devlist_entry->conf.pc_vendor = cfg->vendor;
523 devlist_entry->conf.pc_device = cfg->device;
525 devlist_entry->conf.pc_class = cfg->baseclass;
526 devlist_entry->conf.pc_subclass = cfg->subclass;
527 devlist_entry->conf.pc_progif = cfg->progif;
528 devlist_entry->conf.pc_revid = cfg->revid;
533 return (devlist_entry);
538 pci_read_extcap(device_t pcib, pcicfgregs *cfg)
540 #define REG(n, w) PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
541 #define WREG(n, v, w) PCIB_WRITE_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, v, w)
542 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
546 int ptr, nextptr, ptrptr;
548 switch (cfg->hdrtype & PCIM_HDRTYPE) {
551 ptrptr = PCIR_CAP_PTR;
554 ptrptr = PCIR_CAP_PTR_2; /* cardbus capabilities ptr */
557 return; /* no extended capabilities support */
559 nextptr = REG(ptrptr, 1); /* sanity check? */
562 * Read capability entries.
564 while (nextptr != 0) {
567 printf("illegal PCI extended capability offset %d\n",
571 /* Find the next entry */
573 nextptr = REG(ptr + PCICAP_NEXTPTR, 1);
575 /* Process this entry */
576 switch (REG(ptr + PCICAP_ID, 1)) {
577 case PCIY_PMG: /* PCI power management */
578 if (cfg->pp.pp_cap == 0) {
579 cfg->pp.pp_cap = REG(ptr + PCIR_POWER_CAP, 2);
580 cfg->pp.pp_status = ptr + PCIR_POWER_STATUS;
581 cfg->pp.pp_pmcsr = ptr + PCIR_POWER_PMCSR;
582 if ((nextptr - ptr) > PCIR_POWER_DATA)
583 cfg->pp.pp_data = ptr + PCIR_POWER_DATA;
586 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
587 case PCIY_HT: /* HyperTransport */
588 /* Determine HT-specific capability type. */
589 val = REG(ptr + PCIR_HT_COMMAND, 2);
590 switch (val & PCIM_HTCMD_CAP_MASK) {
591 case PCIM_HTCAP_MSI_MAPPING:
592 if (!(val & PCIM_HTCMD_MSI_FIXED)) {
593 /* Sanity check the mapping window. */
594 addr = REG(ptr + PCIR_HTMSI_ADDRESS_HI,
597 addr |= REG(ptr + PCIR_HTMSI_ADDRESS_LO,
599 if (addr != MSI_INTEL_ADDR_BASE)
601 "HT Bridge at pci%d:%d:%d:%d has non-default MSI window 0x%llx\n",
602 cfg->domain, cfg->bus,
603 cfg->slot, cfg->func,
606 addr = MSI_INTEL_ADDR_BASE;
608 cfg->ht.ht_msimap = ptr;
609 cfg->ht.ht_msictrl = val;
610 cfg->ht.ht_msiaddr = addr;
615 case PCIY_MSI: /* PCI MSI */
616 cfg->msi.msi_location = ptr;
617 cfg->msi.msi_ctrl = REG(ptr + PCIR_MSI_CTRL, 2);
618 cfg->msi.msi_msgnum = 1 << ((cfg->msi.msi_ctrl &
619 PCIM_MSICTRL_MMC_MASK)>>1);
621 case PCIY_MSIX: /* PCI MSI-X */
622 cfg->msix.msix_location = ptr;
623 cfg->msix.msix_ctrl = REG(ptr + PCIR_MSIX_CTRL, 2);
624 cfg->msix.msix_msgnum = (cfg->msix.msix_ctrl &
625 PCIM_MSIXCTRL_TABLE_SIZE) + 1;
626 val = REG(ptr + PCIR_MSIX_TABLE, 4);
627 cfg->msix.msix_table_bar = PCIR_BAR(val &
629 cfg->msix.msix_table_offset = val & ~PCIM_MSIX_BIR_MASK;
630 val = REG(ptr + PCIR_MSIX_PBA, 4);
631 cfg->msix.msix_pba_bar = PCIR_BAR(val &
633 cfg->msix.msix_pba_offset = val & ~PCIM_MSIX_BIR_MASK;
635 case PCIY_VPD: /* PCI Vital Product Data */
636 cfg->vpd.vpd_reg = ptr;
639 /* Should always be true. */
640 if ((cfg->hdrtype & PCIM_HDRTYPE) == 1) {
641 val = REG(ptr + PCIR_SUBVENDCAP_ID, 4);
642 cfg->subvendor = val & 0xffff;
643 cfg->subdevice = val >> 16;
646 case PCIY_PCIX: /* PCI-X */
648 * Assume we have a PCI-X chipset if we have
649 * at least one PCI-PCI bridge with a PCI-X
650 * capability. Note that some systems with
651 * PCI-express or HT chipsets might match on
652 * this check as well.
654 if ((cfg->hdrtype & PCIM_HDRTYPE) == 1)
657 case PCIY_EXPRESS: /* PCI-express */
659 * Assume we have a PCI-express chipset if we have
660 * at least one PCI-express device.
668 /* REG and WREG use carry through to next functions */
672 * PCI Vital Product Data
675 #define PCI_VPD_TIMEOUT 1000000
678 pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t *data)
680 int count = PCI_VPD_TIMEOUT;
682 KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
684 WREG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, reg, 2);
686 while ((REG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, 2) & 0x8000) != 0x8000) {
689 DELAY(1); /* limit looping */
691 *data = (REG(cfg->vpd.vpd_reg + PCIR_VPD_DATA, 4));
698 pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t data)
700 int count = PCI_VPD_TIMEOUT;
702 KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
704 WREG(cfg->vpd.vpd_reg + PCIR_VPD_DATA, data, 4);
705 WREG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, reg | 0x8000, 2);
706 while ((REG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, 2) & 0x8000) == 0x8000) {
709 DELAY(1); /* limit looping */
716 #undef PCI_VPD_TIMEOUT
718 struct vpd_readstate {
728 vpd_nextbyte(struct vpd_readstate *vrs, uint8_t *data)
733 if (vrs->bytesinval == 0) {
734 if (pci_read_vpd_reg(vrs->pcib, vrs->cfg, vrs->off, ®))
736 vrs->val = le32toh(reg);
738 byte = vrs->val & 0xff;
741 vrs->val = vrs->val >> 8;
742 byte = vrs->val & 0xff;
752 pci_read_vpd(device_t pcib, pcicfgregs *cfg)
754 struct vpd_readstate vrs;
759 int alloc, off; /* alloc/off for RO/W arrays */
765 /* init vpd reader */
773 name = remain = i = 0; /* shut up stupid gcc */
774 alloc = off = 0; /* shut up stupid gcc */
775 dflen = 0; /* shut up stupid gcc */
778 if (vpd_nextbyte(&vrs, &byte)) {
783 printf("vpd: val: %#x, off: %d, bytesinval: %d, byte: %#hhx, " \
784 "state: %d, remain: %d, name: %#x, i: %d\n", vrs.val,
785 vrs.off, vrs.bytesinval, byte, state, remain, name, i);
788 case 0: /* item name */
790 if (vpd_nextbyte(&vrs, &byte2)) {
795 if (vpd_nextbyte(&vrs, &byte2)) {
799 remain |= byte2 << 8;
800 if (remain > (0x7f*4 - vrs.off)) {
803 "pci%d:%d:%d:%d: invalid VPD data, remain %#x\n",
804 cfg->domain, cfg->bus, cfg->slot,
810 name = (byte >> 3) & 0xf;
813 case 0x2: /* String */
814 cfg->vpd.vpd_ident = malloc(remain + 1,
822 case 0x10: /* VPD-R */
825 cfg->vpd.vpd_ros = malloc(alloc *
826 sizeof(*cfg->vpd.vpd_ros), M_DEVBUF,
830 case 0x11: /* VPD-W */
833 cfg->vpd.vpd_w = malloc(alloc *
834 sizeof(*cfg->vpd.vpd_w), M_DEVBUF,
838 default: /* Invalid data, abort */
844 case 1: /* Identifier String */
845 cfg->vpd.vpd_ident[i++] = byte;
848 cfg->vpd.vpd_ident[i] = '\0';
853 case 2: /* VPD-R Keyword Header */
855 cfg->vpd.vpd_ros = reallocf(cfg->vpd.vpd_ros,
856 (alloc *= 2) * sizeof(*cfg->vpd.vpd_ros),
857 M_DEVBUF, M_WAITOK | M_ZERO);
859 cfg->vpd.vpd_ros[off].keyword[0] = byte;
860 if (vpd_nextbyte(&vrs, &byte2)) {
864 cfg->vpd.vpd_ros[off].keyword[1] = byte2;
865 if (vpd_nextbyte(&vrs, &byte2)) {
871 strncmp(cfg->vpd.vpd_ros[off].keyword, "RV",
874 * if this happens, we can't trust the rest
878 "pci%d:%d:%d:%d: bad keyword length: %d\n",
879 cfg->domain, cfg->bus, cfg->slot,
884 } else if (dflen == 0) {
885 cfg->vpd.vpd_ros[off].value = malloc(1 *
886 sizeof(*cfg->vpd.vpd_ros[off].value),
888 cfg->vpd.vpd_ros[off].value[0] = '\x00';
890 cfg->vpd.vpd_ros[off].value = malloc(
892 sizeof(*cfg->vpd.vpd_ros[off].value),
896 /* keep in sync w/ state 3's transistions */
897 if (dflen == 0 && remain == 0)
905 case 3: /* VPD-R Keyword Value */
906 cfg->vpd.vpd_ros[off].value[i++] = byte;
907 if (strncmp(cfg->vpd.vpd_ros[off].keyword,
908 "RV", 2) == 0 && cksumvalid == -1) {
914 "pci%d:%d:%d:%d: bad VPD cksum, remain %hhu\n",
915 cfg->domain, cfg->bus,
916 cfg->slot, cfg->func,
925 /* keep in sync w/ state 2's transistions */
927 cfg->vpd.vpd_ros[off++].value[i++] = '\0';
928 if (dflen == 0 && remain == 0) {
929 cfg->vpd.vpd_rocnt = off;
930 cfg->vpd.vpd_ros = reallocf(cfg->vpd.vpd_ros,
931 off * sizeof(*cfg->vpd.vpd_ros),
932 M_DEVBUF, M_WAITOK | M_ZERO);
934 } else if (dflen == 0)
944 case 5: /* VPD-W Keyword Header */
946 cfg->vpd.vpd_w = reallocf(cfg->vpd.vpd_w,
947 (alloc *= 2) * sizeof(*cfg->vpd.vpd_w),
948 M_DEVBUF, M_WAITOK | M_ZERO);
950 cfg->vpd.vpd_w[off].keyword[0] = byte;
951 if (vpd_nextbyte(&vrs, &byte2)) {
955 cfg->vpd.vpd_w[off].keyword[1] = byte2;
956 if (vpd_nextbyte(&vrs, &byte2)) {
960 cfg->vpd.vpd_w[off].len = dflen = byte2;
961 cfg->vpd.vpd_w[off].start = vrs.off - vrs.bytesinval;
962 cfg->vpd.vpd_w[off].value = malloc((dflen + 1) *
963 sizeof(*cfg->vpd.vpd_w[off].value),
967 /* keep in sync w/ state 6's transistions */
968 if (dflen == 0 && remain == 0)
976 case 6: /* VPD-W Keyword Value */
977 cfg->vpd.vpd_w[off].value[i++] = byte;
980 /* keep in sync w/ state 5's transistions */
982 cfg->vpd.vpd_w[off++].value[i++] = '\0';
983 if (dflen == 0 && remain == 0) {
984 cfg->vpd.vpd_wcnt = off;
985 cfg->vpd.vpd_w = reallocf(cfg->vpd.vpd_w,
986 off * sizeof(*cfg->vpd.vpd_w),
987 M_DEVBUF, M_WAITOK | M_ZERO);
989 } else if (dflen == 0)
994 printf("pci%d:%d:%d:%d: invalid state: %d\n",
995 cfg->domain, cfg->bus, cfg->slot, cfg->func,
1002 if (cksumvalid == 0 || state < -1) {
1003 /* read-only data bad, clean up */
1004 if (cfg->vpd.vpd_ros != NULL) {
1005 for (off = 0; cfg->vpd.vpd_ros[off].value; off++)
1006 free(cfg->vpd.vpd_ros[off].value, M_DEVBUF);
1007 free(cfg->vpd.vpd_ros, M_DEVBUF);
1008 cfg->vpd.vpd_ros = NULL;
1012 /* I/O error, clean up */
1013 printf("pci%d:%d:%d:%d: failed to read VPD data.\n",
1014 cfg->domain, cfg->bus, cfg->slot, cfg->func);
1015 if (cfg->vpd.vpd_ident != NULL) {
1016 free(cfg->vpd.vpd_ident, M_DEVBUF);
1017 cfg->vpd.vpd_ident = NULL;
1019 if (cfg->vpd.vpd_w != NULL) {
1020 for (off = 0; cfg->vpd.vpd_w[off].value; off++)
1021 free(cfg->vpd.vpd_w[off].value, M_DEVBUF);
1022 free(cfg->vpd.vpd_w, M_DEVBUF);
1023 cfg->vpd.vpd_w = NULL;
1026 cfg->vpd.vpd_cached = 1;
1032 pci_get_vpd_ident_method(device_t dev, device_t child, const char **identptr)
1034 struct pci_devinfo *dinfo = device_get_ivars(child);
1035 pcicfgregs *cfg = &dinfo->cfg;
1037 if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1038 pci_read_vpd(device_get_parent(dev), cfg);
1040 *identptr = cfg->vpd.vpd_ident;
1042 if (*identptr == NULL)
1049 pci_get_vpd_readonly_method(device_t dev, device_t child, const char *kw,
1052 struct pci_devinfo *dinfo = device_get_ivars(child);
1053 pcicfgregs *cfg = &dinfo->cfg;
1056 if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1057 pci_read_vpd(device_get_parent(dev), cfg);
1059 for (i = 0; i < cfg->vpd.vpd_rocnt; i++)
1060 if (memcmp(kw, cfg->vpd.vpd_ros[i].keyword,
1061 sizeof(cfg->vpd.vpd_ros[i].keyword)) == 0) {
1062 *vptr = cfg->vpd.vpd_ros[i].value;
1065 if (i != cfg->vpd.vpd_rocnt)
1073 * Find the requested extended capability and return the offset in
1074 * configuration space via the pointer provided. The function returns
1075 * 0 on success and error code otherwise.
1078 pci_find_extcap_method(device_t dev, device_t child, int capability,
1081 struct pci_devinfo *dinfo = device_get_ivars(child);
1082 pcicfgregs *cfg = &dinfo->cfg;
1087 * Check the CAP_LIST bit of the PCI status register first.
1089 status = pci_read_config(child, PCIR_STATUS, 2);
1090 if (!(status & PCIM_STATUS_CAPPRESENT))
1094 * Determine the start pointer of the capabilities list.
1096 switch (cfg->hdrtype & PCIM_HDRTYPE) {
1102 ptr = PCIR_CAP_PTR_2;
1106 return (ENXIO); /* no extended capabilities support */
1108 ptr = pci_read_config(child, ptr, 1);
1111 * Traverse the capabilities list.
1114 if (pci_read_config(child, ptr + PCICAP_ID, 1) == capability) {
1119 ptr = pci_read_config(child, ptr + PCICAP_NEXTPTR, 1);
1126 * Support for MSI-X message interrupts.
1129 pci_enable_msix(device_t dev, u_int index, uint64_t address, uint32_t data)
1131 struct pci_devinfo *dinfo = device_get_ivars(dev);
1132 struct pcicfg_msix *msix = &dinfo->cfg.msix;
1135 KASSERT(msix->msix_table_len > index, ("bogus index"));
1136 offset = msix->msix_table_offset + index * 16;
1137 bus_write_4(msix->msix_table_res, offset, address & 0xffffffff);
1138 bus_write_4(msix->msix_table_res, offset + 4, address >> 32);
1139 bus_write_4(msix->msix_table_res, offset + 8, data);
1141 /* Enable MSI -> HT mapping. */
1142 pci_ht_map_msi(dev, address);
1146 pci_mask_msix(device_t dev, u_int index)
1148 struct pci_devinfo *dinfo = device_get_ivars(dev);
1149 struct pcicfg_msix *msix = &dinfo->cfg.msix;
1150 uint32_t offset, val;
1152 KASSERT(msix->msix_msgnum > index, ("bogus index"));
1153 offset = msix->msix_table_offset + index * 16 + 12;
1154 val = bus_read_4(msix->msix_table_res, offset);
1155 if (!(val & PCIM_MSIX_VCTRL_MASK)) {
1156 val |= PCIM_MSIX_VCTRL_MASK;
1157 bus_write_4(msix->msix_table_res, offset, val);
1162 pci_unmask_msix(device_t dev, u_int index)
1164 struct pci_devinfo *dinfo = device_get_ivars(dev);
1165 struct pcicfg_msix *msix = &dinfo->cfg.msix;
1166 uint32_t offset, val;
1168 KASSERT(msix->msix_table_len > index, ("bogus index"));
1169 offset = msix->msix_table_offset + index * 16 + 12;
1170 val = bus_read_4(msix->msix_table_res, offset);
1171 if (val & PCIM_MSIX_VCTRL_MASK) {
1172 val &= ~PCIM_MSIX_VCTRL_MASK;
1173 bus_write_4(msix->msix_table_res, offset, val);
1178 pci_pending_msix(device_t dev, u_int index)
1180 struct pci_devinfo *dinfo = device_get_ivars(dev);
1181 struct pcicfg_msix *msix = &dinfo->cfg.msix;
1182 uint32_t offset, bit;
1184 KASSERT(msix->msix_table_len > index, ("bogus index"));
1185 offset = msix->msix_pba_offset + (index / 32) * 4;
1186 bit = 1 << index % 32;
1187 return (bus_read_4(msix->msix_pba_res, offset) & bit);
1191 * Restore MSI-X registers and table during resume. If MSI-X is
1192 * enabled then walk the virtual table to restore the actual MSI-X
1196 pci_resume_msix(device_t dev)
1198 struct pci_devinfo *dinfo = device_get_ivars(dev);
1199 struct pcicfg_msix *msix = &dinfo->cfg.msix;
1200 struct msix_table_entry *mte;
1201 struct msix_vector *mv;
1204 if (msix->msix_alloc > 0) {
1205 /* First, mask all vectors. */
1206 for (i = 0; i < msix->msix_msgnum; i++)
1207 pci_mask_msix(dev, i);
1209 /* Second, program any messages with at least one handler. */
1210 for (i = 0; i < msix->msix_table_len; i++) {
1211 mte = &msix->msix_table[i];
1212 if (mte->mte_vector == 0 || mte->mte_handlers == 0)
1214 mv = &msix->msix_vectors[mte->mte_vector - 1];
1215 pci_enable_msix(dev, i, mv->mv_address, mv->mv_data);
1216 pci_unmask_msix(dev, i);
1219 pci_write_config(dev, msix->msix_location + PCIR_MSIX_CTRL,
1220 msix->msix_ctrl, 2);
1224 * Attempt to allocate *count MSI-X messages. The actual number allocated is
1225 * returned in *count. After this function returns, each message will be
1226 * available to the driver as SYS_RES_IRQ resources starting at rid 1.
1229 pci_alloc_msix_method(device_t dev, device_t child, int *count)
1231 struct pci_devinfo *dinfo = device_get_ivars(child);
1232 pcicfgregs *cfg = &dinfo->cfg;
1233 struct resource_list_entry *rle;
1234 int actual, error, i, irq, max;
1236 /* Don't let count == 0 get us into trouble. */
1240 /* If rid 0 is allocated, then fail. */
1241 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
1242 if (rle != NULL && rle->res != NULL)
1245 /* Already have allocated messages? */
1246 if (cfg->msi.msi_alloc != 0 || cfg->msix.msix_alloc != 0)
1249 /* If MSI is blacklisted for this system, fail. */
1250 if (pci_msi_blacklisted())
1253 /* MSI-X capability present? */
1254 if (cfg->msix.msix_location == 0 || !pci_do_msix)
1257 /* Make sure the appropriate BARs are mapped. */
1258 rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1259 cfg->msix.msix_table_bar);
1260 if (rle == NULL || rle->res == NULL ||
1261 !(rman_get_flags(rle->res) & RF_ACTIVE))
1263 cfg->msix.msix_table_res = rle->res;
1264 if (cfg->msix.msix_pba_bar != cfg->msix.msix_table_bar) {
1265 rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1266 cfg->msix.msix_pba_bar);
1267 if (rle == NULL || rle->res == NULL ||
1268 !(rman_get_flags(rle->res) & RF_ACTIVE))
1271 cfg->msix.msix_pba_res = rle->res;
1274 device_printf(child,
1275 "attempting to allocate %d MSI-X vectors (%d supported)\n",
1276 *count, cfg->msix.msix_msgnum);
1277 max = min(*count, cfg->msix.msix_msgnum);
1278 for (i = 0; i < max; i++) {
1279 /* Allocate a message. */
1280 error = PCIB_ALLOC_MSIX(device_get_parent(dev), child, &irq);
1283 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1, irq,
1289 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 1);
1291 device_printf(child, "using IRQ %lu for MSI-X\n",
1297 * Be fancy and try to print contiguous runs of
1298 * IRQ values as ranges. 'irq' is the previous IRQ.
1299 * 'run' is true if we are in a range.
1301 device_printf(child, "using IRQs %lu", rle->start);
1304 for (i = 1; i < actual; i++) {
1305 rle = resource_list_find(&dinfo->resources,
1306 SYS_RES_IRQ, i + 1);
1308 /* Still in a run? */
1309 if (rle->start == irq + 1) {
1315 /* Finish previous range. */
1321 /* Start new range. */
1322 printf(",%lu", rle->start);
1326 /* Unfinished range? */
1329 printf(" for MSI-X\n");
1333 /* Mask all vectors. */
1334 for (i = 0; i < cfg->msix.msix_msgnum; i++)
1335 pci_mask_msix(child, i);
1337 /* Allocate and initialize vector data and virtual table. */
1338 cfg->msix.msix_vectors = malloc(sizeof(struct msix_vector) * actual,
1339 M_DEVBUF, M_WAITOK | M_ZERO);
1340 cfg->msix.msix_table = malloc(sizeof(struct msix_table_entry) * actual,
1341 M_DEVBUF, M_WAITOK | M_ZERO);
1342 for (i = 0; i < actual; i++) {
1343 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1344 cfg->msix.msix_vectors[i].mv_irq = rle->start;
1345 cfg->msix.msix_table[i].mte_vector = i + 1;
1348 /* Update control register to enable MSI-X. */
1349 cfg->msix.msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
1350 pci_write_config(child, cfg->msix.msix_location + PCIR_MSIX_CTRL,
1351 cfg->msix.msix_ctrl, 2);
1353 /* Update counts of alloc'd messages. */
1354 cfg->msix.msix_alloc = actual;
1355 cfg->msix.msix_table_len = actual;
1361 * By default, pci_alloc_msix() will assign the allocated IRQ
1362 * resources consecutively to the first N messages in the MSI-X table.
1363 * However, device drivers may want to use different layouts if they
1364 * either receive fewer messages than they asked for, or they wish to
1365 * populate the MSI-X table sparsely. This method allows the driver
1366 * to specify what layout it wants. It must be called after a
1367 * successful pci_alloc_msix() but before any of the associated
1368 * SYS_RES_IRQ resources are allocated via bus_alloc_resource().
1370 * The 'vectors' array contains 'count' message vectors. The array
1371 * maps directly to the MSI-X table in that index 0 in the array
1372 * specifies the vector for the first message in the MSI-X table, etc.
1373 * The vector value in each array index can either be 0 to indicate
1374 * that no vector should be assigned to a message slot, or it can be a
1375 * number from 1 to N (where N is the count returned from a
1376 * succcessful call to pci_alloc_msix()) to indicate which message
1377 * vector (IRQ) to be used for the corresponding message.
1379 * On successful return, each message with a non-zero vector will have
1380 * an associated SYS_RES_IRQ whose rid is equal to the array index +
1381 * 1. Additionally, if any of the IRQs allocated via the previous
1382 * call to pci_alloc_msix() are not used in the mapping, those IRQs
1383 * will be freed back to the system automatically.
1385 * For example, suppose a driver has a MSI-X table with 6 messages and
1386 * asks for 6 messages, but pci_alloc_msix() only returns a count of
1387 * 3. Call the three vectors allocated by pci_alloc_msix() A, B, and
1388 * C. After the call to pci_alloc_msix(), the device will be setup to
1389 * have an MSI-X table of ABC--- (where - means no vector assigned).
1390 * If the driver ten passes a vector array of { 1, 0, 1, 2, 0, 2 },
1391 * then the MSI-X table will look like A-AB-B, and the 'C' vector will
1392 * be freed back to the system. This device will also have valid
1393 * SYS_RES_IRQ rids of 1, 3, 4, and 6.
1395 * In any case, the SYS_RES_IRQ rid X will always map to the message
1396 * at MSI-X table index X - 1 and will only be valid if a vector is
1397 * assigned to that table entry.
1400 pci_remap_msix_method(device_t dev, device_t child, int count,
1401 const u_int *vectors)
1403 struct pci_devinfo *dinfo = device_get_ivars(child);
1404 struct pcicfg_msix *msix = &dinfo->cfg.msix;
1405 struct resource_list_entry *rle;
1406 int i, irq, j, *used;
1409 * Have to have at least one message in the table but the
1410 * table can't be bigger than the actual MSI-X table in the
1413 if (count == 0 || count > msix->msix_msgnum)
1416 /* Sanity check the vectors. */
1417 for (i = 0; i < count; i++)
1418 if (vectors[i] > msix->msix_alloc)
1422 * Make sure there aren't any holes in the vectors to be used.
1423 * It's a big pain to support it, and it doesn't really make
1424 * sense anyway. Also, at least one vector must be used.
1426 used = malloc(sizeof(int) * msix->msix_alloc, M_DEVBUF, M_WAITOK |
1428 for (i = 0; i < count; i++)
1429 if (vectors[i] != 0)
1430 used[vectors[i] - 1] = 1;
1431 for (i = 0; i < msix->msix_alloc - 1; i++)
1432 if (used[i] == 0 && used[i + 1] == 1) {
1433 free(used, M_DEVBUF);
1437 free(used, M_DEVBUF);
1441 /* Make sure none of the resources are allocated. */
1442 for (i = 0; i < msix->msix_table_len; i++) {
1443 if (msix->msix_table[i].mte_vector == 0)
1445 if (msix->msix_table[i].mte_handlers > 0)
1447 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1448 KASSERT(rle != NULL, ("missing resource"));
1449 if (rle->res != NULL)
1453 /* Free the existing resource list entries. */
1454 for (i = 0; i < msix->msix_table_len; i++) {
1455 if (msix->msix_table[i].mte_vector == 0)
1457 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
1461 * Build the new virtual table keeping track of which vectors are
1464 free(msix->msix_table, M_DEVBUF);
1465 msix->msix_table = malloc(sizeof(struct msix_table_entry) * count,
1466 M_DEVBUF, M_WAITOK | M_ZERO);
1467 for (i = 0; i < count; i++)
1468 msix->msix_table[i].mte_vector = vectors[i];
1469 msix->msix_table_len = count;
1471 /* Free any unused IRQs and resize the vectors array if necessary. */
1472 j = msix->msix_alloc - 1;
1474 struct msix_vector *vec;
1476 while (used[j] == 0) {
1477 PCIB_RELEASE_MSIX(device_get_parent(dev), child,
1478 msix->msix_vectors[j].mv_irq);
1481 vec = malloc(sizeof(struct msix_vector) * (j + 1), M_DEVBUF,
1483 bcopy(msix->msix_vectors, vec, sizeof(struct msix_vector) *
1485 free(msix->msix_vectors, M_DEVBUF);
1486 msix->msix_vectors = vec;
1487 msix->msix_alloc = j + 1;
1489 free(used, M_DEVBUF);
1491 /* Map the IRQs onto the rids. */
1492 for (i = 0; i < count; i++) {
1493 if (vectors[i] == 0)
1495 irq = msix->msix_vectors[vectors[i]].mv_irq;
1496 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1, irq,
1501 device_printf(child, "Remapped MSI-X IRQs as: ");
1502 for (i = 0; i < count; i++) {
1505 if (vectors[i] == 0)
1509 msix->msix_vectors[vectors[i]].mv_irq);
1518 pci_release_msix(device_t dev, device_t child)
1520 struct pci_devinfo *dinfo = device_get_ivars(child);
1521 struct pcicfg_msix *msix = &dinfo->cfg.msix;
1522 struct resource_list_entry *rle;
1525 /* Do we have any messages to release? */
1526 if (msix->msix_alloc == 0)
1529 /* Make sure none of the resources are allocated. */
1530 for (i = 0; i < msix->msix_table_len; i++) {
1531 if (msix->msix_table[i].mte_vector == 0)
1533 if (msix->msix_table[i].mte_handlers > 0)
1535 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1536 KASSERT(rle != NULL, ("missing resource"));
1537 if (rle->res != NULL)
1541 /* Update control register to disable MSI-X. */
1542 msix->msix_ctrl &= ~PCIM_MSIXCTRL_MSIX_ENABLE;
1543 pci_write_config(child, msix->msix_location + PCIR_MSIX_CTRL,
1544 msix->msix_ctrl, 2);
1546 /* Free the resource list entries. */
1547 for (i = 0; i < msix->msix_table_len; i++) {
1548 if (msix->msix_table[i].mte_vector == 0)
1550 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
1552 free(msix->msix_table, M_DEVBUF);
1553 msix->msix_table_len = 0;
1555 /* Release the IRQs. */
1556 for (i = 0; i < msix->msix_alloc; i++)
1557 PCIB_RELEASE_MSIX(device_get_parent(dev), child,
1558 msix->msix_vectors[i].mv_irq);
1559 free(msix->msix_vectors, M_DEVBUF);
1560 msix->msix_alloc = 0;
1565 * Return the max supported MSI-X messages this device supports.
1566 * Basically, assuming the MD code can alloc messages, this function
1567 * should return the maximum value that pci_alloc_msix() can return.
1568 * Thus, it is subject to the tunables, etc.
1571 pci_msix_count_method(device_t dev, device_t child)
1573 struct pci_devinfo *dinfo = device_get_ivars(child);
1574 struct pcicfg_msix *msix = &dinfo->cfg.msix;
1576 if (pci_do_msix && msix->msix_location != 0)
1577 return (msix->msix_msgnum);
1582 * HyperTransport MSI mapping control
1585 pci_ht_map_msi(device_t dev, uint64_t addr)
1587 struct pci_devinfo *dinfo = device_get_ivars(dev);
1588 struct pcicfg_ht *ht = &dinfo->cfg.ht;
1593 if (addr && !(ht->ht_msictrl & PCIM_HTCMD_MSI_ENABLE) &&
1594 ht->ht_msiaddr >> 20 == addr >> 20) {
1595 /* Enable MSI -> HT mapping. */
1596 ht->ht_msictrl |= PCIM_HTCMD_MSI_ENABLE;
1597 pci_write_config(dev, ht->ht_msimap + PCIR_HT_COMMAND,
1601 if (!addr && ht->ht_msictrl & PCIM_HTCMD_MSI_ENABLE) {
1602 /* Disable MSI -> HT mapping. */
1603 ht->ht_msictrl &= ~PCIM_HTCMD_MSI_ENABLE;
1604 pci_write_config(dev, ht->ht_msimap + PCIR_HT_COMMAND,
1610 pci_get_max_read_req(device_t dev)
1615 if (pci_find_extcap(dev, PCIY_EXPRESS, &cap) != 0)
1617 val = pci_read_config(dev, cap + PCIR_EXPRESS_DEVICE_CTL, 2);
1618 val &= PCIM_EXP_CTL_MAX_READ_REQUEST;
1620 return (1 << (val + 7));
1624 pci_set_max_read_req(device_t dev, int size)
1629 if (pci_find_extcap(dev, PCIY_EXPRESS, &cap) != 0)
1635 size = (1 << (fls(size) - 1));
1636 val = pci_read_config(dev, cap + PCIR_EXPRESS_DEVICE_CTL, 2);
1637 val &= ~PCIM_EXP_CTL_MAX_READ_REQUEST;
1638 val |= (fls(size) - 8) << 12;
1639 pci_write_config(dev, cap + PCIR_EXPRESS_DEVICE_CTL, val, 2);
1644 * Support for MSI message signalled interrupts.
1647 pci_enable_msi(device_t dev, uint64_t address, uint16_t data)
1649 struct pci_devinfo *dinfo = device_get_ivars(dev);
1650 struct pcicfg_msi *msi = &dinfo->cfg.msi;
1652 /* Write data and address values. */
1653 pci_write_config(dev, msi->msi_location + PCIR_MSI_ADDR,
1654 address & 0xffffffff, 4);
1655 if (msi->msi_ctrl & PCIM_MSICTRL_64BIT) {
1656 pci_write_config(dev, msi->msi_location + PCIR_MSI_ADDR_HIGH,
1658 pci_write_config(dev, msi->msi_location + PCIR_MSI_DATA_64BIT,
1661 pci_write_config(dev, msi->msi_location + PCIR_MSI_DATA, data,
1664 /* Enable MSI in the control register. */
1665 msi->msi_ctrl |= PCIM_MSICTRL_MSI_ENABLE;
1666 pci_write_config(dev, msi->msi_location + PCIR_MSI_CTRL, msi->msi_ctrl,
1669 /* Enable MSI -> HT mapping. */
1670 pci_ht_map_msi(dev, address);
1674 pci_disable_msi(device_t dev)
1676 struct pci_devinfo *dinfo = device_get_ivars(dev);
1677 struct pcicfg_msi *msi = &dinfo->cfg.msi;
1679 /* Disable MSI -> HT mapping. */
1680 pci_ht_map_msi(dev, 0);
1682 /* Disable MSI in the control register. */
1683 msi->msi_ctrl &= ~PCIM_MSICTRL_MSI_ENABLE;
1684 pci_write_config(dev, msi->msi_location + PCIR_MSI_CTRL, msi->msi_ctrl,
1689 * Restore MSI registers during resume. If MSI is enabled then
1690 * restore the data and address registers in addition to the control
1694 pci_resume_msi(device_t dev)
1696 struct pci_devinfo *dinfo = device_get_ivars(dev);
1697 struct pcicfg_msi *msi = &dinfo->cfg.msi;
1701 if (msi->msi_ctrl & PCIM_MSICTRL_MSI_ENABLE) {
1702 address = msi->msi_addr;
1703 data = msi->msi_data;
1704 pci_write_config(dev, msi->msi_location + PCIR_MSI_ADDR,
1705 address & 0xffffffff, 4);
1706 if (msi->msi_ctrl & PCIM_MSICTRL_64BIT) {
1707 pci_write_config(dev, msi->msi_location +
1708 PCIR_MSI_ADDR_HIGH, address >> 32, 4);
1709 pci_write_config(dev, msi->msi_location +
1710 PCIR_MSI_DATA_64BIT, data, 2);
1712 pci_write_config(dev, msi->msi_location + PCIR_MSI_DATA,
1715 pci_write_config(dev, msi->msi_location + PCIR_MSI_CTRL, msi->msi_ctrl,
1720 pci_remap_intr_method(device_t bus, device_t dev, u_int irq)
1722 struct pci_devinfo *dinfo = device_get_ivars(dev);
1723 pcicfgregs *cfg = &dinfo->cfg;
1724 struct resource_list_entry *rle;
1725 struct msix_table_entry *mte;
1726 struct msix_vector *mv;
1732 * Handle MSI first. We try to find this IRQ among our list
1733 * of MSI IRQs. If we find it, we request updated address and
1734 * data registers and apply the results.
1736 if (cfg->msi.msi_alloc > 0) {
1738 /* If we don't have any active handlers, nothing to do. */
1739 if (cfg->msi.msi_handlers == 0)
1741 for (i = 0; i < cfg->msi.msi_alloc; i++) {
1742 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ,
1744 if (rle->start == irq) {
1745 error = PCIB_MAP_MSI(device_get_parent(bus),
1746 dev, irq, &addr, &data);
1749 pci_disable_msi(dev);
1750 dinfo->cfg.msi.msi_addr = addr;
1751 dinfo->cfg.msi.msi_data = data;
1752 pci_enable_msi(dev, addr, data);
1760 * For MSI-X, we check to see if we have this IRQ. If we do,
1761 * we request the updated mapping info. If that works, we go
1762 * through all the slots that use this IRQ and update them.
1764 if (cfg->msix.msix_alloc > 0) {
1765 for (i = 0; i < cfg->msix.msix_alloc; i++) {
1766 mv = &cfg->msix.msix_vectors[i];
1767 if (mv->mv_irq == irq) {
1768 error = PCIB_MAP_MSI(device_get_parent(bus),
1769 dev, irq, &addr, &data);
1772 mv->mv_address = addr;
1774 for (j = 0; j < cfg->msix.msix_table_len; j++) {
1775 mte = &cfg->msix.msix_table[j];
1776 if (mte->mte_vector != i + 1)
1778 if (mte->mte_handlers == 0)
1780 pci_mask_msix(dev, j);
1781 pci_enable_msix(dev, j, addr, data);
1782 pci_unmask_msix(dev, j);
1793 * Returns true if the specified device is blacklisted because MSI
1797 pci_msi_device_blacklisted(device_t dev)
1799 struct pci_quirk *q;
1801 if (!pci_honor_msi_blacklist)
1804 for (q = &pci_quirks[0]; q->devid; q++) {
1805 if (q->devid == pci_get_devid(dev) &&
1806 q->type == PCI_QUIRK_DISABLE_MSI)
1813 * Returns true if a specified chipset supports MSI when it is
1814 * emulated hardware in a virtual machine.
1817 pci_msi_vm_chipset(device_t dev)
1819 struct pci_quirk *q;
1821 for (q = &pci_quirks[0]; q->devid; q++) {
1822 if (q->devid == pci_get_devid(dev) &&
1823 q->type == PCI_QUIRK_ENABLE_MSI_VM)
1830 * Determine if MSI is blacklisted globally on this sytem. Currently,
1831 * we just check for blacklisted chipsets as represented by the
1832 * host-PCI bridge at device 0:0:0. In the future, it may become
1833 * necessary to check other system attributes, such as the kenv values
1834 * that give the motherboard manufacturer and model number.
1837 pci_msi_blacklisted(void)
1841 if (!pci_honor_msi_blacklist)
1844 /* Blacklist all non-PCI-express and non-PCI-X chipsets. */
1845 if (!(pcie_chipset || pcix_chipset)) {
1846 if (vm_guest != VM_GUEST_NO) {
1847 dev = pci_find_bsf(0, 0, 0);
1849 return (pci_msi_vm_chipset(dev) == 0);
1854 dev = pci_find_bsf(0, 0, 0);
1856 return (pci_msi_device_blacklisted(dev));
1861 * Attempt to allocate *count MSI messages. The actual number allocated is
1862 * returned in *count. After this function returns, each message will be
1863 * available to the driver as SYS_RES_IRQ resources starting at a rid 1.
1866 pci_alloc_msi_method(device_t dev, device_t child, int *count)
1868 struct pci_devinfo *dinfo = device_get_ivars(child);
1869 pcicfgregs *cfg = &dinfo->cfg;
1870 struct resource_list_entry *rle;
1871 int actual, error, i, irqs[32];
1874 /* Don't let count == 0 get us into trouble. */
1878 /* If rid 0 is allocated, then fail. */
1879 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
1880 if (rle != NULL && rle->res != NULL)
1883 /* Already have allocated messages? */
1884 if (cfg->msi.msi_alloc != 0 || cfg->msix.msix_alloc != 0)
1887 /* If MSI is blacklisted for this system, fail. */
1888 if (pci_msi_blacklisted())
1891 /* MSI capability present? */
1892 if (cfg->msi.msi_location == 0 || !pci_do_msi)
1896 device_printf(child,
1897 "attempting to allocate %d MSI vectors (%d supported)\n",
1898 *count, cfg->msi.msi_msgnum);
1900 /* Don't ask for more than the device supports. */
1901 actual = min(*count, cfg->msi.msi_msgnum);
1903 /* Don't ask for more than 32 messages. */
1904 actual = min(actual, 32);
1906 /* MSI requires power of 2 number of messages. */
1907 if (!powerof2(actual))
1911 /* Try to allocate N messages. */
1912 error = PCIB_ALLOC_MSI(device_get_parent(dev), child, actual,
1913 cfg->msi.msi_msgnum, irqs);
1924 * We now have N actual messages mapped onto SYS_RES_IRQ
1925 * resources in the irqs[] array, so add new resources
1926 * starting at rid 1.
1928 for (i = 0; i < actual; i++)
1929 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1,
1930 irqs[i], irqs[i], 1);
1934 device_printf(child, "using IRQ %d for MSI\n", irqs[0]);
1939 * Be fancy and try to print contiguous runs
1940 * of IRQ values as ranges. 'run' is true if
1941 * we are in a range.
1943 device_printf(child, "using IRQs %d", irqs[0]);
1945 for (i = 1; i < actual; i++) {
1947 /* Still in a run? */
1948 if (irqs[i] == irqs[i - 1] + 1) {
1953 /* Finish previous range. */
1955 printf("-%d", irqs[i - 1]);
1959 /* Start new range. */
1960 printf(",%d", irqs[i]);
1963 /* Unfinished range? */
1965 printf("-%d", irqs[actual - 1]);
1966 printf(" for MSI\n");
1970 /* Update control register with actual count. */
1971 ctrl = cfg->msi.msi_ctrl;
1972 ctrl &= ~PCIM_MSICTRL_MME_MASK;
1973 ctrl |= (ffs(actual) - 1) << 4;
1974 cfg->msi.msi_ctrl = ctrl;
1975 pci_write_config(child, cfg->msi.msi_location + PCIR_MSI_CTRL, ctrl, 2);
1977 /* Update counts of alloc'd messages. */
1978 cfg->msi.msi_alloc = actual;
1979 cfg->msi.msi_handlers = 0;
1984 /* Release the MSI messages associated with this device. */
1986 pci_release_msi_method(device_t dev, device_t child)
1988 struct pci_devinfo *dinfo = device_get_ivars(child);
1989 struct pcicfg_msi *msi = &dinfo->cfg.msi;
1990 struct resource_list_entry *rle;
1991 int error, i, irqs[32];
1993 /* Try MSI-X first. */
1994 error = pci_release_msix(dev, child);
1995 if (error != ENODEV)
1998 /* Do we have any messages to release? */
1999 if (msi->msi_alloc == 0)
2001 KASSERT(msi->msi_alloc <= 32, ("more than 32 alloc'd messages"));
2003 /* Make sure none of the resources are allocated. */
2004 if (msi->msi_handlers > 0)
2006 for (i = 0; i < msi->msi_alloc; i++) {
2007 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
2008 KASSERT(rle != NULL, ("missing MSI resource"));
2009 if (rle->res != NULL)
2011 irqs[i] = rle->start;
2014 /* Update control register with 0 count. */
2015 KASSERT(!(msi->msi_ctrl & PCIM_MSICTRL_MSI_ENABLE),
2016 ("%s: MSI still enabled", __func__));
2017 msi->msi_ctrl &= ~PCIM_MSICTRL_MME_MASK;
2018 pci_write_config(child, msi->msi_location + PCIR_MSI_CTRL,
2021 /* Release the messages. */
2022 PCIB_RELEASE_MSI(device_get_parent(dev), child, msi->msi_alloc, irqs);
2023 for (i = 0; i < msi->msi_alloc; i++)
2024 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
2026 /* Update alloc count. */
2034 * Return the max supported MSI messages this device supports.
2035 * Basically, assuming the MD code can alloc messages, this function
2036 * should return the maximum value that pci_alloc_msi() can return.
2037 * Thus, it is subject to the tunables, etc.
2040 pci_msi_count_method(device_t dev, device_t child)
2042 struct pci_devinfo *dinfo = device_get_ivars(child);
2043 struct pcicfg_msi *msi = &dinfo->cfg.msi;
2045 if (pci_do_msi && msi->msi_location != 0)
2046 return (msi->msi_msgnum);
2050 /* free pcicfgregs structure and all depending data structures */
2053 pci_freecfg(struct pci_devinfo *dinfo)
2055 struct devlist *devlist_head;
2058 devlist_head = &pci_devq;
2060 if (dinfo->cfg.vpd.vpd_reg) {
2061 free(dinfo->cfg.vpd.vpd_ident, M_DEVBUF);
2062 for (i = 0; i < dinfo->cfg.vpd.vpd_rocnt; i++)
2063 free(dinfo->cfg.vpd.vpd_ros[i].value, M_DEVBUF);
2064 free(dinfo->cfg.vpd.vpd_ros, M_DEVBUF);
2065 for (i = 0; i < dinfo->cfg.vpd.vpd_wcnt; i++)
2066 free(dinfo->cfg.vpd.vpd_w[i].value, M_DEVBUF);
2067 free(dinfo->cfg.vpd.vpd_w, M_DEVBUF);
2069 STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links);
2070 free(dinfo, M_DEVBUF);
2072 /* increment the generation count */
2075 /* we're losing one device */
2081 * PCI power manangement
2084 pci_set_powerstate_method(device_t dev, device_t child, int state)
2086 struct pci_devinfo *dinfo = device_get_ivars(child);
2087 pcicfgregs *cfg = &dinfo->cfg;
2089 int result, oldstate, highest, delay;
2091 if (cfg->pp.pp_cap == 0)
2092 return (EOPNOTSUPP);
2095 * Optimize a no state change request away. While it would be OK to
2096 * write to the hardware in theory, some devices have shown odd
2097 * behavior when going from D3 -> D3.
2099 oldstate = pci_get_powerstate(child);
2100 if (oldstate == state)
2104 * The PCI power management specification states that after a state
2105 * transition between PCI power states, system software must
2106 * guarantee a minimal delay before the function accesses the device.
2107 * Compute the worst case delay that we need to guarantee before we
2108 * access the device. Many devices will be responsive much more
2109 * quickly than this delay, but there are some that don't respond
2110 * instantly to state changes. Transitions to/from D3 state require
2111 * 10ms, while D2 requires 200us, and D0/1 require none. The delay
2112 * is done below with DELAY rather than a sleeper function because
2113 * this function can be called from contexts where we cannot sleep.
2115 highest = (oldstate > state) ? oldstate : state;
2116 if (highest == PCI_POWERSTATE_D3)
2118 else if (highest == PCI_POWERSTATE_D2)
2122 status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2)
2123 & ~PCIM_PSTAT_DMASK;
2126 case PCI_POWERSTATE_D0:
2127 status |= PCIM_PSTAT_D0;
2129 case PCI_POWERSTATE_D1:
2130 if ((cfg->pp.pp_cap & PCIM_PCAP_D1SUPP) == 0)
2131 return (EOPNOTSUPP);
2132 status |= PCIM_PSTAT_D1;
2134 case PCI_POWERSTATE_D2:
2135 if ((cfg->pp.pp_cap & PCIM_PCAP_D2SUPP) == 0)
2136 return (EOPNOTSUPP);
2137 status |= PCIM_PSTAT_D2;
2139 case PCI_POWERSTATE_D3:
2140 status |= PCIM_PSTAT_D3;
2147 pci_printf(cfg, "Transition from D%d to D%d\n", oldstate,
2150 PCI_WRITE_CONFIG(dev, child, cfg->pp.pp_status, status, 2);
2157 pci_get_powerstate_method(device_t dev, device_t child)
2159 struct pci_devinfo *dinfo = device_get_ivars(child);
2160 pcicfgregs *cfg = &dinfo->cfg;
2164 if (cfg->pp.pp_cap != 0) {
2165 status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2);
2166 switch (status & PCIM_PSTAT_DMASK) {
2168 result = PCI_POWERSTATE_D0;
2171 result = PCI_POWERSTATE_D1;
2174 result = PCI_POWERSTATE_D2;
2177 result = PCI_POWERSTATE_D3;
2180 result = PCI_POWERSTATE_UNKNOWN;
2184 /* No support, device is always at D0 */
2185 result = PCI_POWERSTATE_D0;
2191 * Some convenience functions for PCI device drivers.
2194 static __inline void
2195 pci_set_command_bit(device_t dev, device_t child, uint16_t bit)
2199 command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2201 PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
2204 static __inline void
2205 pci_clear_command_bit(device_t dev, device_t child, uint16_t bit)
2209 command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2211 PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
2215 pci_enable_busmaster_method(device_t dev, device_t child)
2217 pci_set_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
2222 pci_disable_busmaster_method(device_t dev, device_t child)
2224 pci_clear_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
2229 pci_enable_io_method(device_t dev, device_t child, int space)
2234 case SYS_RES_IOPORT:
2235 bit = PCIM_CMD_PORTEN;
2237 case SYS_RES_MEMORY:
2238 bit = PCIM_CMD_MEMEN;
2243 pci_set_command_bit(dev, child, bit);
2248 pci_disable_io_method(device_t dev, device_t child, int space)
2253 case SYS_RES_IOPORT:
2254 bit = PCIM_CMD_PORTEN;
2256 case SYS_RES_MEMORY:
2257 bit = PCIM_CMD_MEMEN;
2262 pci_clear_command_bit(dev, child, bit);
2267 * New style pci driver. Parent device is either a pci-host-bridge or a
2268 * pci-pci-bridge. Both kinds are represented by instances of pcib.
2272 pci_print_verbose(struct pci_devinfo *dinfo)
2276 pcicfgregs *cfg = &dinfo->cfg;
2278 printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n",
2279 cfg->vendor, cfg->device, cfg->revid);
2280 printf("\tdomain=%d, bus=%d, slot=%d, func=%d\n",
2281 cfg->domain, cfg->bus, cfg->slot, cfg->func);
2282 printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n",
2283 cfg->baseclass, cfg->subclass, cfg->progif, cfg->hdrtype,
2285 printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n",
2286 cfg->cmdreg, cfg->statreg, cfg->cachelnsz);
2287 printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n",
2288 cfg->lattimer, cfg->lattimer * 30, cfg->mingnt,
2289 cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250);
2290 if (cfg->intpin > 0)
2291 printf("\tintpin=%c, irq=%d\n",
2292 cfg->intpin +'a' -1, cfg->intline);
2293 if (cfg->pp.pp_cap) {
2296 status = pci_read_config(cfg->dev, cfg->pp.pp_status, 2);
2297 printf("\tpowerspec %d supports D0%s%s D3 current D%d\n",
2298 cfg->pp.pp_cap & PCIM_PCAP_SPEC,
2299 cfg->pp.pp_cap & PCIM_PCAP_D1SUPP ? " D1" : "",
2300 cfg->pp.pp_cap & PCIM_PCAP_D2SUPP ? " D2" : "",
2301 status & PCIM_PSTAT_DMASK);
2303 if (cfg->msi.msi_location) {
2306 ctrl = cfg->msi.msi_ctrl;
2307 printf("\tMSI supports %d message%s%s%s\n",
2308 cfg->msi.msi_msgnum,
2309 (cfg->msi.msi_msgnum == 1) ? "" : "s",
2310 (ctrl & PCIM_MSICTRL_64BIT) ? ", 64 bit" : "",
2311 (ctrl & PCIM_MSICTRL_VECTOR) ? ", vector masks":"");
2313 if (cfg->msix.msix_location) {
2314 printf("\tMSI-X supports %d message%s ",
2315 cfg->msix.msix_msgnum,
2316 (cfg->msix.msix_msgnum == 1) ? "" : "s");
2317 if (cfg->msix.msix_table_bar == cfg->msix.msix_pba_bar)
2318 printf("in map 0x%x\n",
2319 cfg->msix.msix_table_bar);
2321 printf("in maps 0x%x and 0x%x\n",
2322 cfg->msix.msix_table_bar,
2323 cfg->msix.msix_pba_bar);
2329 pci_porten(device_t dev)
2331 return (pci_read_config(dev, PCIR_COMMAND, 2) & PCIM_CMD_PORTEN) != 0;
2335 pci_memen(device_t dev)
2337 return (pci_read_config(dev, PCIR_COMMAND, 2) & PCIM_CMD_MEMEN) != 0;
2341 pci_read_bar(device_t dev, int reg, pci_addr_t *mapp, pci_addr_t *testvalp)
2343 pci_addr_t map, testval;
2347 map = pci_read_config(dev, reg, 4);
2348 ln2range = pci_maprange(map);
2350 map |= (pci_addr_t)pci_read_config(dev, reg + 4, 4) << 32;
2353 * Disable decoding via the command register before
2354 * determining the BAR's length since we will be placing it in
2357 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
2358 pci_write_config(dev, PCIR_COMMAND,
2359 cmd & ~(PCI_BAR_MEM(map) ? PCIM_CMD_MEMEN : PCIM_CMD_PORTEN), 2);
2362 * Determine the BAR's length by writing all 1's. The bottom
2363 * log_2(size) bits of the BAR will stick as 0 when we read
2366 pci_write_config(dev, reg, 0xffffffff, 4);
2367 testval = pci_read_config(dev, reg, 4);
2368 if (ln2range == 64) {
2369 pci_write_config(dev, reg + 4, 0xffffffff, 4);
2370 testval |= (pci_addr_t)pci_read_config(dev, reg + 4, 4) << 32;
2374 * Restore the original value of the BAR. We may have reprogrammed
2375 * the BAR of the low-level console device and when booting verbose,
2376 * we need the console device addressable.
2378 pci_write_config(dev, reg, map, 4);
2380 pci_write_config(dev, reg + 4, map >> 32, 4);
2381 pci_write_config(dev, PCIR_COMMAND, cmd, 2);
2384 *testvalp = testval;
2388 pci_write_bar(device_t dev, int reg, pci_addr_t base)
2393 map = pci_read_config(dev, reg, 4);
2394 ln2range = pci_maprange(map);
2395 pci_write_config(dev, reg, base, 4);
2397 pci_write_config(dev, reg + 4, base >> 32, 4);
2401 * Add a resource based on a pci map register. Return 1 if the map
2402 * register is a 32bit map register or 2 if it is a 64bit register.
2405 pci_add_map(device_t bus, device_t dev, int reg, struct resource_list *rl,
2406 int force, int prefetch)
2408 pci_addr_t base, map, testval;
2409 pci_addr_t start, end, count;
2410 int barlen, basezero, maprange, mapsize, type;
2412 struct resource *res;
2414 pci_read_bar(dev, reg, &map, &testval);
2415 if (PCI_BAR_MEM(map)) {
2416 type = SYS_RES_MEMORY;
2417 if (map & PCIM_BAR_MEM_PREFETCH)
2420 type = SYS_RES_IOPORT;
2421 mapsize = pci_mapsize(testval);
2422 base = pci_mapbase(map);
2423 #ifdef __PCI_BAR_ZERO_VALID
2426 basezero = base == 0;
2428 maprange = pci_maprange(map);
2429 barlen = maprange == 64 ? 2 : 1;
2432 * For I/O registers, if bottom bit is set, and the next bit up
2433 * isn't clear, we know we have a BAR that doesn't conform to the
2434 * spec, so ignore it. Also, sanity check the size of the data
2435 * areas to the type of memory involved. Memory must be at least
2436 * 16 bytes in size, while I/O ranges must be at least 4.
2438 if (PCI_BAR_IO(testval) && (testval & PCIM_BAR_IO_RESERVED) != 0)
2440 if ((type == SYS_RES_MEMORY && mapsize < 4) ||
2441 (type == SYS_RES_IOPORT && mapsize < 2))
2445 printf("\tmap[%02x]: type %s, range %2d, base %#jx, size %2d",
2446 reg, pci_maptype(map), maprange, (uintmax_t)base, mapsize);
2447 if (type == SYS_RES_IOPORT && !pci_porten(dev))
2448 printf(", port disabled\n");
2449 else if (type == SYS_RES_MEMORY && !pci_memen(dev))
2450 printf(", memory disabled\n");
2452 printf(", enabled\n");
2456 * If base is 0, then we have problems if this architecture does
2457 * not allow that. It is best to ignore such entries for the
2458 * moment. These will be allocated later if the driver specifically
2459 * requests them. However, some removable busses look better when
2460 * all resources are allocated, so allow '0' to be overriden.
2462 * Similarly treat maps whose values is the same as the test value
2463 * read back. These maps have had all f's written to them by the
2464 * BIOS in an attempt to disable the resources.
2466 if (!force && (basezero || map == testval))
2468 if ((u_long)base != base) {
2470 "pci%d:%d:%d:%d bar %#x too many address bits",
2471 pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev),
2472 pci_get_function(dev), reg);
2477 * This code theoretically does the right thing, but has
2478 * undesirable side effects in some cases where peripherals
2479 * respond oddly to having these bits enabled. Let the user
2480 * be able to turn them off (since pci_enable_io_modes is 1 by
2483 if (pci_enable_io_modes) {
2484 /* Turn on resources that have been left off by a lazy BIOS */
2485 if (type == SYS_RES_IOPORT && !pci_porten(dev)) {
2486 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
2487 cmd |= PCIM_CMD_PORTEN;
2488 pci_write_config(dev, PCIR_COMMAND, cmd, 2);
2490 if (type == SYS_RES_MEMORY && !pci_memen(dev)) {
2491 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
2492 cmd |= PCIM_CMD_MEMEN;
2493 pci_write_config(dev, PCIR_COMMAND, cmd, 2);
2496 if (type == SYS_RES_IOPORT && !pci_porten(dev))
2498 if (type == SYS_RES_MEMORY && !pci_memen(dev))
2502 count = (pci_addr_t)1 << mapsize;
2503 if (basezero || base == pci_mapbase(testval)) {
2504 start = 0; /* Let the parent decide. */
2508 end = base + count - 1;
2510 resource_list_add(rl, type, reg, start, end, count);
2513 * Try to allocate the resource for this BAR from our parent
2514 * so that this resource range is already reserved. The
2515 * driver for this device will later inherit this resource in
2516 * pci_alloc_resource().
2518 res = resource_list_alloc(rl, bus, dev, type, ®, start, end, count,
2519 prefetch ? RF_PREFETCHABLE : 0);
2522 * If the allocation fails, clear the BAR and delete
2523 * the resource list entry to force
2524 * pci_alloc_resource() to allocate resources from the
2527 resource_list_delete(rl, type, reg);
2530 start = rman_get_start(res);
2531 rman_set_device(res, bus);
2533 pci_write_bar(dev, reg, start);
2538 * For ATA devices we need to decide early what addressing mode to use.
2539 * Legacy demands that the primary and secondary ATA ports sits on the
2540 * same addresses that old ISA hardware did. This dictates that we use
2541 * those addresses and ignore the BAR's if we cannot set PCI native
2545 pci_ata_maps(device_t bus, device_t dev, struct resource_list *rl, int force,
2546 uint32_t prefetchmask)
2549 int rid, type, progif;
2551 /* if this device supports PCI native addressing use it */
2552 progif = pci_read_config(dev, PCIR_PROGIF, 1);
2553 if ((progif & 0x8a) == 0x8a) {
2554 if (pci_mapbase(pci_read_config(dev, PCIR_BAR(0), 4)) &&
2555 pci_mapbase(pci_read_config(dev, PCIR_BAR(2), 4))) {
2556 printf("Trying ATA native PCI addressing mode\n");
2557 pci_write_config(dev, PCIR_PROGIF, progif | 0x05, 1);
2561 progif = pci_read_config(dev, PCIR_PROGIF, 1);
2562 type = SYS_RES_IOPORT;
2563 if (progif & PCIP_STORAGE_IDE_MODEPRIM) {
2564 pci_add_map(bus, dev, PCIR_BAR(0), rl, force,
2565 prefetchmask & (1 << 0));
2566 pci_add_map(bus, dev, PCIR_BAR(1), rl, force,
2567 prefetchmask & (1 << 1));
2570 resource_list_add(rl, type, rid, 0x1f0, 0x1f7, 8);
2571 r = resource_list_alloc(rl, bus, dev, type, &rid, 0x1f0, 0x1f7,
2573 rman_set_device(r, bus);
2575 resource_list_add(rl, type, rid, 0x3f6, 0x3f6, 1);
2576 r = resource_list_alloc(rl, bus, dev, type, &rid, 0x3f6, 0x3f6,
2578 rman_set_device(r, bus);
2580 if (progif & PCIP_STORAGE_IDE_MODESEC) {
2581 pci_add_map(bus, dev, PCIR_BAR(2), rl, force,
2582 prefetchmask & (1 << 2));
2583 pci_add_map(bus, dev, PCIR_BAR(3), rl, force,
2584 prefetchmask & (1 << 3));
2587 resource_list_add(rl, type, rid, 0x170, 0x177, 8);
2588 r = resource_list_alloc(rl, bus, dev, type, &rid, 0x170, 0x177,
2590 rman_set_device(r, bus);
2592 resource_list_add(rl, type, rid, 0x376, 0x376, 1);
2593 r = resource_list_alloc(rl, bus, dev, type, &rid, 0x376, 0x376,
2595 rman_set_device(r, bus);
2597 pci_add_map(bus, dev, PCIR_BAR(4), rl, force,
2598 prefetchmask & (1 << 4));
2599 pci_add_map(bus, dev, PCIR_BAR(5), rl, force,
2600 prefetchmask & (1 << 5));
2604 pci_assign_interrupt(device_t bus, device_t dev, int force_route)
2606 struct pci_devinfo *dinfo = device_get_ivars(dev);
2607 pcicfgregs *cfg = &dinfo->cfg;
2608 char tunable_name[64];
2611 /* Has to have an intpin to have an interrupt. */
2612 if (cfg->intpin == 0)
2615 /* Let the user override the IRQ with a tunable. */
2616 irq = PCI_INVALID_IRQ;
2617 snprintf(tunable_name, sizeof(tunable_name),
2618 "hw.pci%d.%d.%d.INT%c.irq",
2619 cfg->domain, cfg->bus, cfg->slot, cfg->intpin + 'A' - 1);
2620 if (TUNABLE_INT_FETCH(tunable_name, &irq) && (irq >= 255 || irq <= 0))
2621 irq = PCI_INVALID_IRQ;
2624 * If we didn't get an IRQ via the tunable, then we either use the
2625 * IRQ value in the intline register or we ask the bus to route an
2626 * interrupt for us. If force_route is true, then we only use the
2627 * value in the intline register if the bus was unable to assign an
2630 if (!PCI_INTERRUPT_VALID(irq)) {
2631 if (!PCI_INTERRUPT_VALID(cfg->intline) || force_route)
2632 irq = PCI_ASSIGN_INTERRUPT(bus, dev);
2633 if (!PCI_INTERRUPT_VALID(irq))
2637 /* If after all that we don't have an IRQ, just bail. */
2638 if (!PCI_INTERRUPT_VALID(irq))
2641 /* Update the config register if it changed. */
2642 if (irq != cfg->intline) {
2644 pci_write_config(dev, PCIR_INTLINE, irq, 1);
2647 /* Add this IRQ as rid 0 interrupt resource. */
2648 resource_list_add(&dinfo->resources, SYS_RES_IRQ, 0, irq, irq, 1);
2651 /* Perform early OHCI takeover from SMM. */
2653 ohci_early_takeover(device_t self)
2655 struct resource *res;
2661 res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
2665 ctl = bus_read_4(res, OHCI_CONTROL);
2666 if (ctl & OHCI_IR) {
2668 printf("ohci early: "
2669 "SMM active, request owner change\n");
2670 bus_write_4(res, OHCI_COMMAND_STATUS, OHCI_OCR);
2671 for (i = 0; (i < 100) && (ctl & OHCI_IR); i++) {
2673 ctl = bus_read_4(res, OHCI_CONTROL);
2675 if (ctl & OHCI_IR) {
2677 printf("ohci early: "
2678 "SMM does not respond, resetting\n");
2679 bus_write_4(res, OHCI_CONTROL, OHCI_HCFS_RESET);
2681 /* Disable interrupts */
2682 bus_write_4(res, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
2685 bus_release_resource(self, SYS_RES_MEMORY, rid, res);
2688 /* Perform early UHCI takeover from SMM. */
2690 uhci_early_takeover(device_t self)
2692 struct resource *res;
2696 * Set the PIRQD enable bit and switch off all the others. We don't
2697 * want legacy support to interfere with us XXX Does this also mean
2698 * that the BIOS won't touch the keyboard anymore if it is connected
2699 * to the ports of the root hub?
2701 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
2703 /* Disable interrupts */
2704 rid = PCI_UHCI_BASE_REG;
2705 res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid, RF_ACTIVE);
2707 bus_write_2(res, UHCI_INTR, 0);
2708 bus_release_resource(self, SYS_RES_IOPORT, rid, res);
2712 /* Perform early EHCI takeover from SMM. */
2714 ehci_early_takeover(device_t self)
2716 struct resource *res;
2726 res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
2730 cparams = bus_read_4(res, EHCI_HCCPARAMS);
2732 /* Synchronise with the BIOS if it owns the controller. */
2733 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
2734 eecp = EHCI_EECP_NEXT(eec)) {
2735 eec = pci_read_config(self, eecp, 4);
2736 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
2739 bios_sem = pci_read_config(self, eecp +
2740 EHCI_LEGSUP_BIOS_SEM, 1);
2741 if (bios_sem == 0) {
2745 printf("ehci early: "
2746 "SMM active, request owner change\n");
2748 pci_write_config(self, eecp + EHCI_LEGSUP_OS_SEM, 1, 1);
2750 for (i = 0; (i < 100) && (bios_sem != 0); i++) {
2752 bios_sem = pci_read_config(self, eecp +
2753 EHCI_LEGSUP_BIOS_SEM, 1);
2756 if (bios_sem != 0) {
2758 printf("ehci early: "
2759 "SMM does not respond\n");
2761 /* Disable interrupts */
2762 offs = EHCI_CAPLENGTH(bus_read_4(res, EHCI_CAPLEN_HCIVERSION));
2763 bus_write_4(res, offs + EHCI_USBINTR, 0);
2765 bus_release_resource(self, SYS_RES_MEMORY, rid, res);
2769 pci_add_resources(device_t bus, device_t dev, int force, uint32_t prefetchmask)
2771 struct pci_devinfo *dinfo = device_get_ivars(dev);
2772 pcicfgregs *cfg = &dinfo->cfg;
2773 struct resource_list *rl = &dinfo->resources;
2774 struct pci_quirk *q;
2777 /* ATA devices needs special map treatment */
2778 if ((pci_get_class(dev) == PCIC_STORAGE) &&
2779 (pci_get_subclass(dev) == PCIS_STORAGE_IDE) &&
2780 ((pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) ||
2781 (!pci_read_config(dev, PCIR_BAR(0), 4) &&
2782 !pci_read_config(dev, PCIR_BAR(2), 4))) )
2783 pci_ata_maps(bus, dev, rl, force, prefetchmask);
2785 for (i = 0; i < cfg->nummaps;)
2786 i += pci_add_map(bus, dev, PCIR_BAR(i), rl, force,
2787 prefetchmask & (1 << i));
2790 * Add additional, quirked resources.
2792 for (q = &pci_quirks[0]; q->devid; q++) {
2793 if (q->devid == ((cfg->device << 16) | cfg->vendor)
2794 && q->type == PCI_QUIRK_MAP_REG)
2795 pci_add_map(bus, dev, q->arg1, rl, force, 0);
2798 if (cfg->intpin > 0 && PCI_INTERRUPT_VALID(cfg->intline)) {
2799 #ifdef __PCI_REROUTE_INTERRUPT
2801 * Try to re-route interrupts. Sometimes the BIOS or
2802 * firmware may leave bogus values in these registers.
2803 * If the re-route fails, then just stick with what we
2806 pci_assign_interrupt(bus, dev, 1);
2808 pci_assign_interrupt(bus, dev, 0);
2812 if (pci_usb_takeover && pci_get_class(dev) == PCIC_SERIALBUS &&
2813 pci_get_subclass(dev) == PCIS_SERIALBUS_USB) {
2814 if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_EHCI)
2815 ehci_early_takeover(dev);
2816 else if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_OHCI)
2817 ohci_early_takeover(dev);
2818 else if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_UHCI)
2819 uhci_early_takeover(dev);
2824 pci_add_children(device_t dev, int domain, int busno, size_t dinfo_size)
2826 #define REG(n, w) PCIB_READ_CONFIG(pcib, busno, s, f, n, w)
2827 device_t pcib = device_get_parent(dev);
2828 struct pci_devinfo *dinfo;
2830 int s, f, pcifunchigh;
2833 KASSERT(dinfo_size >= sizeof(struct pci_devinfo),
2834 ("dinfo_size too small"));
2835 maxslots = PCIB_MAXSLOTS(pcib);
2836 for (s = 0; s <= maxslots; s++) {
2840 hdrtype = REG(PCIR_HDRTYPE, 1);
2841 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
2843 if (hdrtype & PCIM_MFDEV)
2844 pcifunchigh = PCI_FUNCMAX;
2845 for (f = 0; f <= pcifunchigh; f++) {
2846 dinfo = pci_read_device(pcib, domain, busno, s, f,
2848 if (dinfo != NULL) {
2849 pci_add_child(dev, dinfo);
2857 pci_add_child(device_t bus, struct pci_devinfo *dinfo)
2859 dinfo->cfg.dev = device_add_child(bus, NULL, -1);
2860 device_set_ivars(dinfo->cfg.dev, dinfo);
2861 resource_list_init(&dinfo->resources);
2862 pci_cfg_save(dinfo->cfg.dev, dinfo, 0);
2863 pci_cfg_restore(dinfo->cfg.dev, dinfo);
2864 pci_print_verbose(dinfo);
2865 pci_add_resources(bus, dinfo->cfg.dev, 0, 0);
2869 pci_probe(device_t dev)
2872 device_set_desc(dev, "PCI bus");
2874 /* Allow other subclasses to override this driver. */
2875 return (BUS_PROBE_GENERIC);
2879 pci_attach(device_t dev)
2884 * Since there can be multiple independantly numbered PCI
2885 * busses on systems with multiple PCI domains, we can't use
2886 * the unit number to decide which bus we are probing. We ask
2887 * the parent pcib what our domain and bus numbers are.
2889 domain = pcib_get_domain(dev);
2890 busno = pcib_get_bus(dev);
2892 device_printf(dev, "domain=%d, physical bus=%d\n",
2894 pci_add_children(dev, domain, busno, sizeof(struct pci_devinfo));
2895 return (bus_generic_attach(dev));
2899 pci_suspend(device_t dev)
2901 int dstate, error, i, numdevs;
2902 device_t acpi_dev, child, *devlist;
2903 struct pci_devinfo *dinfo;
2906 * Save the PCI configuration space for each child and set the
2907 * device in the appropriate power state for this sleep state.
2910 if (pci_do_power_resume)
2911 acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
2912 if ((error = device_get_children(dev, &devlist, &numdevs)) != 0)
2914 for (i = 0; i < numdevs; i++) {
2916 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2917 pci_cfg_save(child, dinfo, 0);
2920 /* Suspend devices before potentially powering them down. */
2921 error = bus_generic_suspend(dev);
2923 free(devlist, M_TEMP);
2928 * Always set the device to D3. If ACPI suggests a different
2929 * power state, use it instead. If ACPI is not present, the
2930 * firmware is responsible for managing device power. Skip
2931 * children who aren't attached since they are powered down
2932 * separately. Only manage type 0 devices for now.
2934 for (i = 0; acpi_dev && i < numdevs; i++) {
2936 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2937 if (device_is_attached(child) && dinfo->cfg.hdrtype == 0) {
2938 dstate = PCI_POWERSTATE_D3;
2939 ACPI_PWR_FOR_SLEEP(acpi_dev, child, &dstate);
2940 pci_set_powerstate(child, dstate);
2943 free(devlist, M_TEMP);
2948 pci_resume(device_t dev)
2950 int i, numdevs, error;
2951 device_t acpi_dev, child, *devlist;
2952 struct pci_devinfo *dinfo;
2955 * Set each child to D0 and restore its PCI configuration space.
2958 if (pci_do_power_resume)
2959 acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
2960 if ((error = device_get_children(dev, &devlist, &numdevs)) != 0)
2962 for (i = 0; i < numdevs; i++) {
2964 * Notify ACPI we're going to D0 but ignore the result. If
2965 * ACPI is not present, the firmware is responsible for
2966 * managing device power. Only manage type 0 devices for now.
2969 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2970 if (acpi_dev && device_is_attached(child) &&
2971 dinfo->cfg.hdrtype == 0) {
2972 ACPI_PWR_FOR_SLEEP(acpi_dev, child, NULL);
2973 pci_set_powerstate(child, PCI_POWERSTATE_D0);
2976 /* Now the device is powered up, restore its config space. */
2977 pci_cfg_restore(child, dinfo);
2979 free(devlist, M_TEMP);
2980 return (bus_generic_resume(dev));
2984 pci_load_vendor_data(void)
2986 caddr_t vendordata, info;
2988 if ((vendordata = preload_search_by_type("pci_vendor_data")) != NULL) {
2989 info = preload_search_info(vendordata, MODINFO_ADDR);
2990 pci_vendordata = *(char **)info;
2991 info = preload_search_info(vendordata, MODINFO_SIZE);
2992 pci_vendordata_size = *(size_t *)info;
2993 /* terminate the database */
2994 pci_vendordata[pci_vendordata_size] = '\n';
2999 pci_driver_added(device_t dev, driver_t *driver)
3004 struct pci_devinfo *dinfo;
3008 device_printf(dev, "driver added\n");
3009 DEVICE_IDENTIFY(driver, dev);
3010 if (device_get_children(dev, &devlist, &numdevs) != 0)
3012 for (i = 0; i < numdevs; i++) {
3014 if (device_get_state(child) != DS_NOTPRESENT)
3016 dinfo = device_get_ivars(child);
3017 pci_print_verbose(dinfo);
3019 pci_printf(&dinfo->cfg, "reprobing on driver added\n");
3020 pci_cfg_restore(child, dinfo);
3021 if (device_probe_and_attach(child) != 0)
3022 pci_cfg_save(child, dinfo, 1);
3024 free(devlist, M_TEMP);
3028 pci_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
3029 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
3031 struct pci_devinfo *dinfo;
3032 struct msix_table_entry *mte;
3033 struct msix_vector *mv;
3039 error = bus_generic_setup_intr(dev, child, irq, flags, filter, intr,
3044 /* If this is not a direct child, just bail out. */
3045 if (device_get_parent(child) != dev) {
3050 rid = rman_get_rid(irq);
3052 /* Make sure that INTx is enabled */
3053 pci_clear_command_bit(dev, child, PCIM_CMD_INTxDIS);
3056 * Check to see if the interrupt is MSI or MSI-X.
3057 * Ask our parent to map the MSI and give
3058 * us the address and data register values.
3059 * If we fail for some reason, teardown the
3060 * interrupt handler.
3062 dinfo = device_get_ivars(child);
3063 if (dinfo->cfg.msi.msi_alloc > 0) {
3064 if (dinfo->cfg.msi.msi_addr == 0) {
3065 KASSERT(dinfo->cfg.msi.msi_handlers == 0,
3066 ("MSI has handlers, but vectors not mapped"));
3067 error = PCIB_MAP_MSI(device_get_parent(dev),
3068 child, rman_get_start(irq), &addr, &data);
3071 dinfo->cfg.msi.msi_addr = addr;
3072 dinfo->cfg.msi.msi_data = data;
3074 if (dinfo->cfg.msi.msi_handlers == 0)
3075 pci_enable_msi(child, dinfo->cfg.msi.msi_addr,
3076 dinfo->cfg.msi.msi_data);
3077 dinfo->cfg.msi.msi_handlers++;
3079 KASSERT(dinfo->cfg.msix.msix_alloc > 0,
3080 ("No MSI or MSI-X interrupts allocated"));
3081 KASSERT(rid <= dinfo->cfg.msix.msix_table_len,
3082 ("MSI-X index too high"));
3083 mte = &dinfo->cfg.msix.msix_table[rid - 1];
3084 KASSERT(mte->mte_vector != 0, ("no message vector"));
3085 mv = &dinfo->cfg.msix.msix_vectors[mte->mte_vector - 1];
3086 KASSERT(mv->mv_irq == rman_get_start(irq),
3088 if (mv->mv_address == 0) {
3089 KASSERT(mte->mte_handlers == 0,
3090 ("MSI-X table entry has handlers, but vector not mapped"));
3091 error = PCIB_MAP_MSI(device_get_parent(dev),
3092 child, rman_get_start(irq), &addr, &data);
3095 mv->mv_address = addr;
3098 if (mte->mte_handlers == 0) {
3099 pci_enable_msix(child, rid - 1, mv->mv_address,
3101 pci_unmask_msix(child, rid - 1);
3103 mte->mte_handlers++;
3106 /* Make sure that INTx is disabled if we are using MSI/MSIX */
3107 pci_set_command_bit(dev, child, PCIM_CMD_INTxDIS);
3110 (void)bus_generic_teardown_intr(dev, child, irq,
3120 pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
3123 struct msix_table_entry *mte;
3124 struct resource_list_entry *rle;
3125 struct pci_devinfo *dinfo;
3128 if (irq == NULL || !(rman_get_flags(irq) & RF_ACTIVE))
3131 /* If this isn't a direct child, just bail out */
3132 if (device_get_parent(child) != dev)
3133 return(bus_generic_teardown_intr(dev, child, irq, cookie));
3135 rid = rman_get_rid(irq);
3138 pci_set_command_bit(dev, child, PCIM_CMD_INTxDIS);
3141 * Check to see if the interrupt is MSI or MSI-X. If so,
3142 * decrement the appropriate handlers count and mask the
3143 * MSI-X message, or disable MSI messages if the count
3146 dinfo = device_get_ivars(child);
3147 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, rid);
3148 if (rle->res != irq)
3150 if (dinfo->cfg.msi.msi_alloc > 0) {
3151 KASSERT(rid <= dinfo->cfg.msi.msi_alloc,
3152 ("MSI-X index too high"));
3153 if (dinfo->cfg.msi.msi_handlers == 0)
3155 dinfo->cfg.msi.msi_handlers--;
3156 if (dinfo->cfg.msi.msi_handlers == 0)
3157 pci_disable_msi(child);
3159 KASSERT(dinfo->cfg.msix.msix_alloc > 0,
3160 ("No MSI or MSI-X interrupts allocated"));
3161 KASSERT(rid <= dinfo->cfg.msix.msix_table_len,
3162 ("MSI-X index too high"));
3163 mte = &dinfo->cfg.msix.msix_table[rid - 1];
3164 if (mte->mte_handlers == 0)
3166 mte->mte_handlers--;
3167 if (mte->mte_handlers == 0)
3168 pci_mask_msix(child, rid - 1);
3171 error = bus_generic_teardown_intr(dev, child, irq, cookie);
3174 ("%s: generic teardown failed for MSI/MSI-X", __func__));
3179 pci_print_child(device_t dev, device_t child)
3181 struct pci_devinfo *dinfo;
3182 struct resource_list *rl;
3185 dinfo = device_get_ivars(child);
3186 rl = &dinfo->resources;
3188 retval += bus_print_child_header(dev, child);
3190 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
3191 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
3192 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
3193 if (device_get_flags(dev))
3194 retval += printf(" flags %#x", device_get_flags(dev));
3196 retval += printf(" at device %d.%d", pci_get_slot(child),
3197 pci_get_function(child));
3199 retval += bus_print_child_footer(dev, child);
3209 } pci_nomatch_tab[] = {
3210 {PCIC_OLD, -1, "old"},
3211 {PCIC_OLD, PCIS_OLD_NONVGA, "non-VGA display device"},
3212 {PCIC_OLD, PCIS_OLD_VGA, "VGA-compatible display device"},
3213 {PCIC_STORAGE, -1, "mass storage"},
3214 {PCIC_STORAGE, PCIS_STORAGE_SCSI, "SCSI"},
3215 {PCIC_STORAGE, PCIS_STORAGE_IDE, "ATA"},
3216 {PCIC_STORAGE, PCIS_STORAGE_FLOPPY, "floppy disk"},
3217 {PCIC_STORAGE, PCIS_STORAGE_IPI, "IPI"},
3218 {PCIC_STORAGE, PCIS_STORAGE_RAID, "RAID"},
3219 {PCIC_STORAGE, PCIS_STORAGE_ATA_ADMA, "ATA (ADMA)"},
3220 {PCIC_STORAGE, PCIS_STORAGE_SATA, "SATA"},
3221 {PCIC_STORAGE, PCIS_STORAGE_SAS, "SAS"},
3222 {PCIC_NETWORK, -1, "network"},
3223 {PCIC_NETWORK, PCIS_NETWORK_ETHERNET, "ethernet"},
3224 {PCIC_NETWORK, PCIS_NETWORK_TOKENRING, "token ring"},
3225 {PCIC_NETWORK, PCIS_NETWORK_FDDI, "fddi"},
3226 {PCIC_NETWORK, PCIS_NETWORK_ATM, "ATM"},
3227 {PCIC_NETWORK, PCIS_NETWORK_ISDN, "ISDN"},
3228 {PCIC_DISPLAY, -1, "display"},
3229 {PCIC_DISPLAY, PCIS_DISPLAY_VGA, "VGA"},
3230 {PCIC_DISPLAY, PCIS_DISPLAY_XGA, "XGA"},
3231 {PCIC_DISPLAY, PCIS_DISPLAY_3D, "3D"},
3232 {PCIC_MULTIMEDIA, -1, "multimedia"},
3233 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_VIDEO, "video"},
3234 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_AUDIO, "audio"},
3235 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_TELE, "telephony"},
3236 {PCIC_MULTIMEDIA, PCIS_MULTIMEDIA_HDA, "HDA"},
3237 {PCIC_MEMORY, -1, "memory"},
3238 {PCIC_MEMORY, PCIS_MEMORY_RAM, "RAM"},
3239 {PCIC_MEMORY, PCIS_MEMORY_FLASH, "flash"},
3240 {PCIC_BRIDGE, -1, "bridge"},
3241 {PCIC_BRIDGE, PCIS_BRIDGE_HOST, "HOST-PCI"},
3242 {PCIC_BRIDGE, PCIS_BRIDGE_ISA, "PCI-ISA"},
3243 {PCIC_BRIDGE, PCIS_BRIDGE_EISA, "PCI-EISA"},
3244 {PCIC_BRIDGE, PCIS_BRIDGE_MCA, "PCI-MCA"},
3245 {PCIC_BRIDGE, PCIS_BRIDGE_PCI, "PCI-PCI"},
3246 {PCIC_BRIDGE, PCIS_BRIDGE_PCMCIA, "PCI-PCMCIA"},
3247 {PCIC_BRIDGE, PCIS_BRIDGE_NUBUS, "PCI-NuBus"},
3248 {PCIC_BRIDGE, PCIS_BRIDGE_CARDBUS, "PCI-CardBus"},
3249 {PCIC_BRIDGE, PCIS_BRIDGE_RACEWAY, "PCI-RACEway"},
3250 {PCIC_SIMPLECOMM, -1, "simple comms"},
3251 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_UART, "UART"}, /* could detect 16550 */
3252 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_PAR, "parallel port"},
3253 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MULSER, "multiport serial"},
3254 {PCIC_SIMPLECOMM, PCIS_SIMPLECOMM_MODEM, "generic modem"},
3255 {PCIC_BASEPERIPH, -1, "base peripheral"},
3256 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PIC, "interrupt controller"},
3257 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_DMA, "DMA controller"},
3258 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_TIMER, "timer"},
3259 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_RTC, "realtime clock"},
3260 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
3261 {PCIC_BASEPERIPH, PCIS_BASEPERIPH_SDHC, "SD host controller"},
3262 {PCIC_INPUTDEV, -1, "input device"},
3263 {PCIC_INPUTDEV, PCIS_INPUTDEV_KEYBOARD, "keyboard"},
3264 {PCIC_INPUTDEV, PCIS_INPUTDEV_DIGITIZER,"digitizer"},
3265 {PCIC_INPUTDEV, PCIS_INPUTDEV_MOUSE, "mouse"},
3266 {PCIC_INPUTDEV, PCIS_INPUTDEV_SCANNER, "scanner"},
3267 {PCIC_INPUTDEV, PCIS_INPUTDEV_GAMEPORT, "gameport"},
3268 {PCIC_DOCKING, -1, "docking station"},
3269 {PCIC_PROCESSOR, -1, "processor"},
3270 {PCIC_SERIALBUS, -1, "serial bus"},
3271 {PCIC_SERIALBUS, PCIS_SERIALBUS_FW, "FireWire"},
3272 {PCIC_SERIALBUS, PCIS_SERIALBUS_ACCESS, "AccessBus"},
3273 {PCIC_SERIALBUS, PCIS_SERIALBUS_SSA, "SSA"},
3274 {PCIC_SERIALBUS, PCIS_SERIALBUS_USB, "USB"},
3275 {PCIC_SERIALBUS, PCIS_SERIALBUS_FC, "Fibre Channel"},
3276 {PCIC_SERIALBUS, PCIS_SERIALBUS_SMBUS, "SMBus"},
3277 {PCIC_WIRELESS, -1, "wireless controller"},
3278 {PCIC_WIRELESS, PCIS_WIRELESS_IRDA, "iRDA"},
3279 {PCIC_WIRELESS, PCIS_WIRELESS_IR, "IR"},
3280 {PCIC_WIRELESS, PCIS_WIRELESS_RF, "RF"},
3281 {PCIC_INTELLIIO, -1, "intelligent I/O controller"},
3282 {PCIC_INTELLIIO, PCIS_INTELLIIO_I2O, "I2O"},
3283 {PCIC_SATCOM, -1, "satellite communication"},
3284 {PCIC_SATCOM, PCIS_SATCOM_TV, "sat TV"},
3285 {PCIC_SATCOM, PCIS_SATCOM_AUDIO, "sat audio"},
3286 {PCIC_SATCOM, PCIS_SATCOM_VOICE, "sat voice"},
3287 {PCIC_SATCOM, PCIS_SATCOM_DATA, "sat data"},
3288 {PCIC_CRYPTO, -1, "encrypt/decrypt"},
3289 {PCIC_CRYPTO, PCIS_CRYPTO_NETCOMP, "network/computer crypto"},
3290 {PCIC_CRYPTO, PCIS_CRYPTO_ENTERTAIN, "entertainment crypto"},
3291 {PCIC_DASP, -1, "dasp"},
3292 {PCIC_DASP, PCIS_DASP_DPIO, "DPIO module"},
3297 pci_probe_nomatch(device_t dev, device_t child)
3300 char *cp, *scp, *device;
3303 * Look for a listing for this device in a loaded device database.
3305 if ((device = pci_describe_device(child)) != NULL) {
3306 device_printf(dev, "<%s>", device);
3307 free(device, M_DEVBUF);
3310 * Scan the class/subclass descriptions for a general
3315 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
3316 if (pci_nomatch_tab[i].class == pci_get_class(child)) {
3317 if (pci_nomatch_tab[i].subclass == -1) {
3318 cp = pci_nomatch_tab[i].desc;
3319 } else if (pci_nomatch_tab[i].subclass ==
3320 pci_get_subclass(child)) {
3321 scp = pci_nomatch_tab[i].desc;
3325 device_printf(dev, "<%s%s%s>",
3327 ((cp != NULL) && (scp != NULL)) ? ", " : "",
3330 printf(" at device %d.%d (no driver attached)\n",
3331 pci_get_slot(child), pci_get_function(child));
3332 pci_cfg_save(child, (struct pci_devinfo *)device_get_ivars(child), 1);
3337 * Parse the PCI device database, if loaded, and return a pointer to a
3338 * description of the device.
3340 * The database is flat text formatted as follows:
3342 * Any line not in a valid format is ignored.
3343 * Lines are terminated with newline '\n' characters.
3345 * A VENDOR line consists of the 4 digit (hex) vendor code, a TAB, then
3348 * A DEVICE line is entered immediately below the corresponding VENDOR ID.
3349 * - devices cannot be listed without a corresponding VENDOR line.
3350 * A DEVICE line consists of a TAB, the 4 digit (hex) device code,
3351 * another TAB, then the device name.
3355 * Assuming (ptr) points to the beginning of a line in the database,
3356 * return the vendor or device and description of the next entry.
3357 * The value of (vendor) or (device) inappropriate for the entry type
3358 * is set to -1. Returns nonzero at the end of the database.
3360 * Note that this is slightly unrobust in the face of corrupt data;
3361 * we attempt to safeguard against this by spamming the end of the
3362 * database with a newline when we initialise.
3365 pci_describe_parse_line(char **ptr, int *vendor, int *device, char **desc)
3374 left = pci_vendordata_size - (cp - pci_vendordata);
3382 sscanf(cp, "%x\t%80[^\n]", vendor, *desc) == 2)
3386 sscanf(cp, "%x\t%80[^\n]", device, *desc) == 2)
3389 /* skip to next line */
3390 while (*cp != '\n' && left > 0) {
3399 /* skip to next line */
3400 while (*cp != '\n' && left > 0) {
3404 if (*cp == '\n' && left > 0)
3411 pci_describe_device(device_t dev)
3414 char *desc, *vp, *dp, *line;
3416 desc = vp = dp = NULL;
3419 * If we have no vendor data, we can't do anything.
3421 if (pci_vendordata == NULL)
3425 * Scan the vendor data looking for this device
3427 line = pci_vendordata;
3428 if ((vp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
3431 if (pci_describe_parse_line(&line, &vendor, &device, &vp))
3433 if (vendor == pci_get_vendor(dev))
3436 if ((dp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
3439 if (pci_describe_parse_line(&line, &vendor, &device, &dp)) {
3447 if (device == pci_get_device(dev))
3451 snprintf(dp, 80, "0x%x", pci_get_device(dev));
3452 if ((desc = malloc(strlen(vp) + strlen(dp) + 3, M_DEVBUF, M_NOWAIT)) !=
3454 sprintf(desc, "%s, %s", vp, dp);
3464 pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
3466 struct pci_devinfo *dinfo;
3469 dinfo = device_get_ivars(child);
3473 case PCI_IVAR_ETHADDR:
3475 * The generic accessor doesn't deal with failure, so
3476 * we set the return value, then return an error.
3478 *((uint8_t **) result) = NULL;
3480 case PCI_IVAR_SUBVENDOR:
3481 *result = cfg->subvendor;
3483 case PCI_IVAR_SUBDEVICE:
3484 *result = cfg->subdevice;
3486 case PCI_IVAR_VENDOR:
3487 *result = cfg->vendor;
3489 case PCI_IVAR_DEVICE:
3490 *result = cfg->device;
3492 case PCI_IVAR_DEVID:
3493 *result = (cfg->device << 16) | cfg->vendor;
3495 case PCI_IVAR_CLASS:
3496 *result = cfg->baseclass;
3498 case PCI_IVAR_SUBCLASS:
3499 *result = cfg->subclass;
3501 case PCI_IVAR_PROGIF:
3502 *result = cfg->progif;
3504 case PCI_IVAR_REVID:
3505 *result = cfg->revid;
3507 case PCI_IVAR_INTPIN:
3508 *result = cfg->intpin;
3511 *result = cfg->intline;
3513 case PCI_IVAR_DOMAIN:
3514 *result = cfg->domain;
3520 *result = cfg->slot;
3522 case PCI_IVAR_FUNCTION:
3523 *result = cfg->func;
3525 case PCI_IVAR_CMDREG:
3526 *result = cfg->cmdreg;
3528 case PCI_IVAR_CACHELNSZ:
3529 *result = cfg->cachelnsz;
3531 case PCI_IVAR_MINGNT:
3532 *result = cfg->mingnt;
3534 case PCI_IVAR_MAXLAT:
3535 *result = cfg->maxlat;
3537 case PCI_IVAR_LATTIMER:
3538 *result = cfg->lattimer;
3547 pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
3549 struct pci_devinfo *dinfo;
3551 dinfo = device_get_ivars(child);
3554 case PCI_IVAR_INTPIN:
3555 dinfo->cfg.intpin = value;
3557 case PCI_IVAR_ETHADDR:
3558 case PCI_IVAR_SUBVENDOR:
3559 case PCI_IVAR_SUBDEVICE:
3560 case PCI_IVAR_VENDOR:
3561 case PCI_IVAR_DEVICE:
3562 case PCI_IVAR_DEVID:
3563 case PCI_IVAR_CLASS:
3564 case PCI_IVAR_SUBCLASS:
3565 case PCI_IVAR_PROGIF:
3566 case PCI_IVAR_REVID:
3568 case PCI_IVAR_DOMAIN:
3571 case PCI_IVAR_FUNCTION:
3572 return (EINVAL); /* disallow for now */
3580 #include "opt_ddb.h"
3582 #include <ddb/ddb.h>
3583 #include <sys/cons.h>
3586 * List resources based on pci map registers, used for within ddb
3589 DB_SHOW_COMMAND(pciregs, db_pci_dump)
3591 struct pci_devinfo *dinfo;
3592 struct devlist *devlist_head;
3595 int i, error, none_count;
3598 /* get the head of the device queue */
3599 devlist_head = &pci_devq;
3602 * Go through the list of devices and print out devices
3604 for (error = 0, i = 0,
3605 dinfo = STAILQ_FIRST(devlist_head);
3606 (dinfo != NULL) && (error == 0) && (i < pci_numdevs) && !db_pager_quit;
3607 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
3609 /* Populate pd_name and pd_unit */
3612 name = device_get_name(dinfo->cfg.dev);
3615 db_printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
3616 "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
3617 (name && *name) ? name : "none",
3618 (name && *name) ? (int)device_get_unit(dinfo->cfg.dev) :
3620 p->pc_sel.pc_domain, p->pc_sel.pc_bus, p->pc_sel.pc_dev,
3621 p->pc_sel.pc_func, (p->pc_class << 16) |
3622 (p->pc_subclass << 8) | p->pc_progif,
3623 (p->pc_subdevice << 16) | p->pc_subvendor,
3624 (p->pc_device << 16) | p->pc_vendor,
3625 p->pc_revid, p->pc_hdr);
3630 static struct resource *
3631 pci_alloc_map(device_t dev, device_t child, int type, int *rid,
3632 u_long start, u_long end, u_long count, u_int flags)
3634 struct pci_devinfo *dinfo = device_get_ivars(child);
3635 struct resource_list *rl = &dinfo->resources;
3636 struct resource_list_entry *rle;
3637 struct resource *res;
3638 pci_addr_t map, testval;
3642 * Weed out the bogons, and figure out how large the BAR/map
3643 * is. Bars that read back 0 here are bogus and unimplemented.
3644 * Note: atapci in legacy mode are special and handled elsewhere
3645 * in the code. If you have a atapci device in legacy mode and
3646 * it fails here, that other code is broken.
3649 pci_read_bar(child, *rid, &map, &testval);
3651 /* Ignore a BAR with a base of 0. */
3652 if (pci_mapbase(testval) == 0)
3655 if (PCI_BAR_MEM(testval)) {
3656 if (type != SYS_RES_MEMORY) {
3659 "child %s requested type %d for rid %#x,"
3660 " but the BAR says it is an memio\n",
3661 device_get_nameunit(child), type, *rid);
3665 if (type != SYS_RES_IOPORT) {
3668 "child %s requested type %d for rid %#x,"
3669 " but the BAR says it is an ioport\n",
3670 device_get_nameunit(child), type, *rid);
3676 * For real BARs, we need to override the size that
3677 * the driver requests, because that's what the BAR
3678 * actually uses and we would otherwise have a
3679 * situation where we might allocate the excess to
3680 * another driver, which won't work.
3682 mapsize = pci_mapsize(testval);
3683 count = (pci_addr_t)1 << mapsize;
3684 if (RF_ALIGNMENT(flags) < mapsize)
3685 flags = (flags & ~RF_ALIGNMENT_MASK) | RF_ALIGNMENT_LOG2(mapsize);
3686 if (PCI_BAR_MEM(testval) && (testval & PCIM_BAR_MEM_PREFETCH))
3687 flags |= RF_PREFETCHABLE;
3690 * Allocate enough resource, and then write back the
3691 * appropriate bar for that resource.
3693 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, type, rid,
3694 start, end, count, flags & ~RF_ACTIVE);
3696 device_printf(child,
3697 "%#lx bytes of rid %#x res %d failed (%#lx, %#lx).\n",
3698 count, *rid, type, start, end);
3701 rman_set_device(res, dev);
3702 resource_list_add(rl, type, *rid, start, end, count);
3703 rle = resource_list_find(rl, type, *rid);
3705 panic("pci_alloc_map: unexpectedly can't find resource.");
3707 rle->start = rman_get_start(res);
3708 rle->end = rman_get_end(res);
3711 device_printf(child,
3712 "Lazy allocation of %#lx bytes rid %#x type %d at %#lx\n",
3713 count, *rid, type, rman_get_start(res));
3714 map = rman_get_start(res);
3715 pci_write_bar(child, *rid, map);
3722 pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
3723 u_long start, u_long end, u_long count, u_int flags)
3725 struct pci_devinfo *dinfo = device_get_ivars(child);
3726 struct resource_list *rl = &dinfo->resources;
3727 struct resource_list_entry *rle;
3728 struct resource *res;
3729 pcicfgregs *cfg = &dinfo->cfg;
3731 if (device_get_parent(child) != dev)
3732 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
3733 type, rid, start, end, count, flags));
3736 * Perform lazy resource allocation
3741 * Can't alloc legacy interrupt once MSI messages have
3744 if (*rid == 0 && (cfg->msi.msi_alloc > 0 ||
3745 cfg->msix.msix_alloc > 0))
3749 * If the child device doesn't have an interrupt
3750 * routed and is deserving of an interrupt, try to
3753 if (*rid == 0 && !PCI_INTERRUPT_VALID(cfg->intline) &&
3755 pci_assign_interrupt(dev, child, 0);
3757 case SYS_RES_IOPORT:
3758 case SYS_RES_MEMORY:
3759 /* Allocate resources for this BAR if needed. */
3760 rle = resource_list_find(rl, type, *rid);
3762 res = pci_alloc_map(dev, child, type, rid, start, end,
3766 rle = resource_list_find(rl, type, *rid);
3770 * If the resource belongs to the bus, then give it to
3771 * the child. We need to activate it if requested
3772 * since the bus always allocates inactive resources.
3774 if (rle != NULL && rle->res != NULL &&
3775 rman_get_device(rle->res) == dev) {
3777 device_printf(child,
3778 "Reserved %#lx bytes for rid %#x type %d at %#lx\n",
3779 rman_get_size(rle->res), *rid, type,
3780 rman_get_start(rle->res));
3781 rman_set_device(rle->res, child);
3782 if ((flags & RF_ACTIVE) &&
3783 bus_activate_resource(child, type, *rid,
3789 return (resource_list_alloc(rl, dev, child, type, rid,
3790 start, end, count, flags));
3794 pci_release_resource(device_t dev, device_t child, int type, int rid,
3799 if (device_get_parent(child) != dev)
3800 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
3804 * For BARs we don't actually want to release the resource.
3805 * Instead, we deactivate the resource if needed and then give
3806 * ownership of the BAR back to the bus.
3809 case SYS_RES_IOPORT:
3810 case SYS_RES_MEMORY:
3811 if (rman_get_device(r) != child)
3813 if (rman_get_flags(r) & RF_ACTIVE) {
3814 error = bus_deactivate_resource(child, type, rid, r);
3818 rman_set_device(r, dev);
3821 return (bus_generic_rl_release_resource(dev, child, type, rid, r));
3825 pci_activate_resource(device_t dev, device_t child, int type, int rid,
3830 error = bus_generic_activate_resource(dev, child, type, rid, r);
3834 /* Enable decoding in the command register when activating BARs. */
3835 if (device_get_parent(child) == dev) {
3837 case SYS_RES_IOPORT:
3838 case SYS_RES_MEMORY:
3839 error = PCI_ENABLE_IO(dev, child, type);
3847 pci_delete_resource(device_t dev, device_t child, int type, int rid)
3849 struct pci_devinfo *dinfo;
3850 struct resource_list *rl;
3851 struct resource_list_entry *rle;
3853 if (device_get_parent(child) != dev)
3856 dinfo = device_get_ivars(child);
3857 rl = &dinfo->resources;
3858 rle = resource_list_find(rl, type, rid);
3863 if (rman_get_device(rle->res) != dev ||
3864 rman_get_flags(rle->res) & RF_ACTIVE) {
3865 device_printf(dev, "delete_resource: "
3866 "Resource still owned by child, oops. "
3867 "(type=%d, rid=%d, addr=%lx)\n",
3868 rle->type, rle->rid,
3869 rman_get_start(rle->res));
3873 #ifndef __PCI_BAR_ZERO_VALID
3875 * If this is a BAR, clear the BAR so it stops
3876 * decoding before releasing the resource.
3879 case SYS_RES_IOPORT:
3880 case SYS_RES_MEMORY:
3881 pci_write_bar(child, rid, 0);
3885 bus_release_resource(dev, type, rid, rle->res);
3887 resource_list_delete(rl, type, rid);
3890 struct resource_list *
3891 pci_get_resource_list (device_t dev, device_t child)
3893 struct pci_devinfo *dinfo = device_get_ivars(child);
3895 return (&dinfo->resources);
3899 pci_read_config_method(device_t dev, device_t child, int reg, int width)
3901 struct pci_devinfo *dinfo = device_get_ivars(child);
3902 pcicfgregs *cfg = &dinfo->cfg;
3904 return (PCIB_READ_CONFIG(device_get_parent(dev),
3905 cfg->bus, cfg->slot, cfg->func, reg, width));
3909 pci_write_config_method(device_t dev, device_t child, int reg,
3910 uint32_t val, int width)
3912 struct pci_devinfo *dinfo = device_get_ivars(child);
3913 pcicfgregs *cfg = &dinfo->cfg;
3915 PCIB_WRITE_CONFIG(device_get_parent(dev),
3916 cfg->bus, cfg->slot, cfg->func, reg, val, width);
3920 pci_child_location_str_method(device_t dev, device_t child, char *buf,
3924 snprintf(buf, buflen, "slot=%d function=%d", pci_get_slot(child),
3925 pci_get_function(child));
3930 pci_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
3933 struct pci_devinfo *dinfo;
3936 dinfo = device_get_ivars(child);
3938 snprintf(buf, buflen, "vendor=0x%04x device=0x%04x subvendor=0x%04x "
3939 "subdevice=0x%04x class=0x%02x%02x%02x", cfg->vendor, cfg->device,
3940 cfg->subvendor, cfg->subdevice, cfg->baseclass, cfg->subclass,
3946 pci_assign_interrupt_method(device_t dev, device_t child)
3948 struct pci_devinfo *dinfo = device_get_ivars(child);
3949 pcicfgregs *cfg = &dinfo->cfg;
3951 return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
3956 pci_modevent(module_t mod, int what, void *arg)
3958 static struct cdev *pci_cdev;
3962 STAILQ_INIT(&pci_devq);
3964 pci_cdev = make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644,
3966 pci_load_vendor_data();
3970 destroy_dev(pci_cdev);
3978 pci_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
3983 * Only do header type 0 devices. Type 1 devices are bridges,
3984 * which we know need special treatment. Type 2 devices are
3985 * cardbus bridges which also require special treatment.
3986 * Other types are unknown, and we err on the side of safety
3989 if (dinfo->cfg.hdrtype != 0)
3993 * Restore the device to full power mode. We must do this
3994 * before we restore the registers because moving from D3 to
3995 * D0 will cause the chip's BARs and some other registers to
3996 * be reset to some unknown power on reset values. Cut down
3997 * the noise on boot by doing nothing if we are already in
4000 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
4001 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
4003 for (i = 0; i < dinfo->cfg.nummaps; i++)
4004 pci_write_config(dev, PCIR_BAR(i), dinfo->cfg.bar[i], 4);
4005 pci_write_config(dev, PCIR_BIOS, dinfo->cfg.bios, 4);
4006 pci_write_config(dev, PCIR_COMMAND, dinfo->cfg.cmdreg, 2);
4007 pci_write_config(dev, PCIR_INTLINE, dinfo->cfg.intline, 1);
4008 pci_write_config(dev, PCIR_INTPIN, dinfo->cfg.intpin, 1);
4009 pci_write_config(dev, PCIR_MINGNT, dinfo->cfg.mingnt, 1);
4010 pci_write_config(dev, PCIR_MAXLAT, dinfo->cfg.maxlat, 1);
4011 pci_write_config(dev, PCIR_CACHELNSZ, dinfo->cfg.cachelnsz, 1);
4012 pci_write_config(dev, PCIR_LATTIMER, dinfo->cfg.lattimer, 1);
4013 pci_write_config(dev, PCIR_PROGIF, dinfo->cfg.progif, 1);
4014 pci_write_config(dev, PCIR_REVID, dinfo->cfg.revid, 1);
4016 /* Restore MSI and MSI-X configurations if they are present. */
4017 if (dinfo->cfg.msi.msi_location != 0)
4018 pci_resume_msi(dev);
4019 if (dinfo->cfg.msix.msix_location != 0)
4020 pci_resume_msix(dev);
4024 pci_cfg_save(device_t dev, struct pci_devinfo *dinfo, int setstate)
4031 * Only do header type 0 devices. Type 1 devices are bridges, which
4032 * we know need special treatment. Type 2 devices are cardbus bridges
4033 * which also require special treatment. Other types are unknown, and
4034 * we err on the side of safety by ignoring them. Powering down
4035 * bridges should not be undertaken lightly.
4037 if (dinfo->cfg.hdrtype != 0)
4039 for (i = 0; i < dinfo->cfg.nummaps; i++)
4040 dinfo->cfg.bar[i] = pci_read_config(dev, PCIR_BAR(i), 4);
4041 dinfo->cfg.bios = pci_read_config(dev, PCIR_BIOS, 4);
4044 * Some drivers apparently write to these registers w/o updating our
4045 * cached copy. No harm happens if we update the copy, so do so here
4046 * so we can restore them. The COMMAND register is modified by the
4047 * bus w/o updating the cache. This should represent the normally
4048 * writable portion of the 'defined' part of type 0 headers. In
4049 * theory we also need to save/restore the PCI capability structures
4050 * we know about, but apart from power we don't know any that are
4053 dinfo->cfg.subvendor = pci_read_config(dev, PCIR_SUBVEND_0, 2);
4054 dinfo->cfg.subdevice = pci_read_config(dev, PCIR_SUBDEV_0, 2);
4055 dinfo->cfg.vendor = pci_read_config(dev, PCIR_VENDOR, 2);
4056 dinfo->cfg.device = pci_read_config(dev, PCIR_DEVICE, 2);
4057 dinfo->cfg.cmdreg = pci_read_config(dev, PCIR_COMMAND, 2);
4058 dinfo->cfg.intline = pci_read_config(dev, PCIR_INTLINE, 1);
4059 dinfo->cfg.intpin = pci_read_config(dev, PCIR_INTPIN, 1);
4060 dinfo->cfg.mingnt = pci_read_config(dev, PCIR_MINGNT, 1);
4061 dinfo->cfg.maxlat = pci_read_config(dev, PCIR_MAXLAT, 1);
4062 dinfo->cfg.cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
4063 dinfo->cfg.lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
4064 dinfo->cfg.baseclass = pci_read_config(dev, PCIR_CLASS, 1);
4065 dinfo->cfg.subclass = pci_read_config(dev, PCIR_SUBCLASS, 1);
4066 dinfo->cfg.progif = pci_read_config(dev, PCIR_PROGIF, 1);
4067 dinfo->cfg.revid = pci_read_config(dev, PCIR_REVID, 1);
4070 * don't set the state for display devices, base peripherals and
4071 * memory devices since bad things happen when they are powered down.
4072 * We should (a) have drivers that can easily detach and (b) use
4073 * generic drivers for these devices so that some device actually
4074 * attaches. We need to make sure that when we implement (a) we don't
4075 * power the device down on a reattach.
4077 cls = pci_get_class(dev);
4080 switch (pci_do_power_nodriver)
4082 case 0: /* NO powerdown at all */
4084 case 1: /* Conservative about what to power down */
4085 if (cls == PCIC_STORAGE)
4088 case 2: /* Agressive about what to power down */
4089 if (cls == PCIC_DISPLAY || cls == PCIC_MEMORY ||
4090 cls == PCIC_BASEPERIPH)
4093 case 3: /* Power down everything */
4097 * PCI spec says we can only go into D3 state from D0 state.
4098 * Transition from D[12] into D0 before going to D3 state.
4100 ps = pci_get_powerstate(dev);
4101 if (ps != PCI_POWERSTATE_D0 && ps != PCI_POWERSTATE_D3)
4102 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
4103 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D3)
4104 pci_set_powerstate(dev, PCI_POWERSTATE_D3);