]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/pci/pci.c
- Add constants for HT PCI capability registers including the various
[FreeBSD/FreeBSD.git] / sys / dev / pci / pci.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000, BSDi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_bus.h"
33
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>
40 #include <sys/conf.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/endian.h>
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <vm/vm_extern.h>
49
50 #include <sys/bus.h>
51 #include <machine/bus.h>
52 #include <sys/rman.h>
53 #include <machine/resource.h>
54
55 #if defined(__i386__) || defined(__amd64__)
56 #include <machine/intr_machdep.h>
57 #endif
58
59 #include <sys/pciio.h>
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/pci_private.h>
63
64 #include "pcib_if.h"
65 #include "pci_if.h"
66
67 #ifdef __HAVE_ACPI
68 #include <contrib/dev/acpica/acpi.h>
69 #include "acpi_if.h"
70 #else
71 #define ACPI_PWR_FOR_SLEEP(x, y, z)
72 #endif
73
74 static uint32_t         pci_mapbase(unsigned mapreg);
75 static int              pci_maptype(unsigned mapreg);
76 static int              pci_mapsize(unsigned testval);
77 static int              pci_maprange(unsigned mapreg);
78 static void             pci_fixancient(pcicfgregs *cfg);
79
80 static int              pci_porten(device_t pcib, int b, int s, int f);
81 static int              pci_memen(device_t pcib, int b, int s, int f);
82 static void             pci_assign_interrupt(device_t bus, device_t dev,
83                             int force_route);
84 static int              pci_add_map(device_t pcib, device_t bus, device_t dev,
85                             int b, int s, int f, int reg,
86                             struct resource_list *rl, int force, int prefetch);
87 static int              pci_probe(device_t dev);
88 static int              pci_attach(device_t dev);
89 static void             pci_load_vendor_data(void);
90 static int              pci_describe_parse_line(char **ptr, int *vendor,
91                             int *device, char **desc);
92 static char             *pci_describe_device(device_t dev);
93 static int              pci_modevent(module_t mod, int what, void *arg);
94 static void             pci_hdrtypedata(device_t pcib, int b, int s, int f,
95                             pcicfgregs *cfg);
96 static void             pci_read_extcap(device_t pcib, pcicfgregs *cfg);
97 static uint32_t         pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg,
98                             int reg);
99 #if 0
100 static void             pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg,
101                             int reg, uint32_t data);
102 #endif
103 static void             pci_read_vpd(device_t pcib, pcicfgregs *cfg);
104
105 static device_method_t pci_methods[] = {
106         /* Device interface */
107         DEVMETHOD(device_probe,         pci_probe),
108         DEVMETHOD(device_attach,        pci_attach),
109         DEVMETHOD(device_detach,        bus_generic_detach),
110         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
111         DEVMETHOD(device_suspend,       pci_suspend),
112         DEVMETHOD(device_resume,        pci_resume),
113
114         /* Bus interface */
115         DEVMETHOD(bus_print_child,      pci_print_child),
116         DEVMETHOD(bus_probe_nomatch,    pci_probe_nomatch),
117         DEVMETHOD(bus_read_ivar,        pci_read_ivar),
118         DEVMETHOD(bus_write_ivar,       pci_write_ivar),
119         DEVMETHOD(bus_driver_added,     pci_driver_added),
120         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
121         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
122
123         DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
124         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
125         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
126         DEVMETHOD(bus_delete_resource,  pci_delete_resource),
127         DEVMETHOD(bus_alloc_resource,   pci_alloc_resource),
128         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
129         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
130         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
131         DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
132         DEVMETHOD(bus_child_location_str, pci_child_location_str_method),
133
134         /* PCI interface */
135         DEVMETHOD(pci_read_config,      pci_read_config_method),
136         DEVMETHOD(pci_write_config,     pci_write_config_method),
137         DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method),
138         DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
139         DEVMETHOD(pci_enable_io,        pci_enable_io_method),
140         DEVMETHOD(pci_disable_io,       pci_disable_io_method),
141         DEVMETHOD(pci_get_vpd_ident,    pci_get_vpd_ident_method),
142         DEVMETHOD(pci_get_vpd_readonly, pci_get_vpd_readonly_method),
143         DEVMETHOD(pci_get_powerstate,   pci_get_powerstate_method),
144         DEVMETHOD(pci_set_powerstate,   pci_set_powerstate_method),
145         DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method),
146         DEVMETHOD(pci_find_extcap,      pci_find_extcap_method),
147         DEVMETHOD(pci_alloc_msi,        pci_alloc_msi_method),
148         DEVMETHOD(pci_release_msi,      pci_release_msi_method),
149         DEVMETHOD(pci_msi_count,        pci_msi_count_method),
150
151         { 0, 0 }
152 };
153
154 DEFINE_CLASS_0(pci, pci_driver, pci_methods, 0);
155
156 static devclass_t pci_devclass;
157 DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, 0);
158 MODULE_VERSION(pci, 1);
159
160 static char     *pci_vendordata;
161 static size_t   pci_vendordata_size;
162
163
164 struct pci_quirk {
165         uint32_t devid; /* Vendor/device of the card */
166         int     type;
167 #define PCI_QUIRK_MAP_REG       1 /* PCI map register in weird place */
168         int     arg1;
169         int     arg2;
170 };
171
172 struct pci_quirk pci_quirks[] = {
173         /* The Intel 82371AB and 82443MX has a map register at offset 0x90. */
174         { 0x71138086, PCI_QUIRK_MAP_REG,        0x90,    0 },
175         { 0x719b8086, PCI_QUIRK_MAP_REG,        0x90,    0 },
176         /* As does the Serverworks OSB4 (the SMBus mapping register) */
177         { 0x02001166, PCI_QUIRK_MAP_REG,        0x90,    0 },
178
179         { 0 }
180 };
181
182 /* map register information */
183 #define PCI_MAPMEM      0x01    /* memory map */
184 #define PCI_MAPMEMP     0x02    /* prefetchable memory map */
185 #define PCI_MAPPORT     0x04    /* port map */
186
187 struct devlist pci_devq;
188 uint32_t pci_generation;
189 uint32_t pci_numdevs = 0;
190
191 /* sysctl vars */
192 SYSCTL_NODE(_hw, OID_AUTO, pci, CTLFLAG_RD, 0, "PCI bus tuning parameters");
193
194 static int pci_enable_io_modes = 1;
195 TUNABLE_INT("hw.pci.enable_io_modes", &pci_enable_io_modes);
196 SYSCTL_INT(_hw_pci, OID_AUTO, enable_io_modes, CTLFLAG_RW,
197     &pci_enable_io_modes, 1,
198     "Enable I/O and memory bits in the config register.  Some BIOSes do not\n\
199 enable these bits correctly.  We'd like to do this all the time, but there\n\
200 are some peripherals that this causes problems with.");
201
202 static int pci_do_power_nodriver = 0;
203 TUNABLE_INT("hw.pci.do_power_nodriver", &pci_do_power_nodriver);
204 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_nodriver, CTLFLAG_RW,
205     &pci_do_power_nodriver, 0,
206   "Place a function into D3 state when no driver attaches to it.  0 means\n\
207 disable.  1 means conservatively place devices into D3 state.  2 means\n\
208 agressively place devices into D3 state.  3 means put absolutely everything\n\
209 in D3 state.");
210
211 static int pci_do_power_resume = 1;
212 TUNABLE_INT("hw.pci.do_power_resume", &pci_do_power_resume);
213 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_resume, CTLFLAG_RW,
214     &pci_do_power_resume, 1,
215   "Transition from D3 -> D0 on resume.");
216
217 static int pci_do_msi = 1;
218 TUNABLE_INT("hw.pci.enable_msi", &pci_do_msi);
219 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msi, CTLFLAG_RW, &pci_do_msi, 1,
220     "Enable support for MSI interrupts");
221
222 static int pci_do_msix = 1;
223 TUNABLE_INT("hw.pci.enable_msix", &pci_do_msix);
224 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msix, CTLFLAG_RW, &pci_do_msix, 1,
225     "Enable support for MSI-X interrupts");
226
227 /* Find a device_t by bus/slot/function */
228
229 device_t
230 pci_find_bsf(uint8_t bus, uint8_t slot, uint8_t func)
231 {
232         struct pci_devinfo *dinfo;
233
234         STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
235                 if ((dinfo->cfg.bus == bus) &&
236                     (dinfo->cfg.slot == slot) &&
237                     (dinfo->cfg.func == func)) {
238                         return (dinfo->cfg.dev);
239                 }
240         }
241
242         return (NULL);
243 }
244
245 /* Find a device_t by vendor/device ID */
246
247 device_t
248 pci_find_device(uint16_t vendor, uint16_t device)
249 {
250         struct pci_devinfo *dinfo;
251
252         STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
253                 if ((dinfo->cfg.vendor == vendor) &&
254                     (dinfo->cfg.device == device)) {
255                         return (dinfo->cfg.dev);
256                 }
257         }
258
259         return (NULL);
260 }
261
262 /* return base address of memory or port map */
263
264 static uint32_t
265 pci_mapbase(uint32_t mapreg)
266 {
267         int mask = 0x03;
268         if ((mapreg & 0x01) == 0)
269                 mask = 0x0f;
270         return (mapreg & ~mask);
271 }
272
273 /* return map type of memory or port map */
274
275 static int
276 pci_maptype(unsigned mapreg)
277 {
278         static uint8_t maptype[0x10] = {
279                 PCI_MAPMEM,             PCI_MAPPORT,
280                 PCI_MAPMEM,             0,
281                 PCI_MAPMEM,             PCI_MAPPORT,
282                 0,                      0,
283                 PCI_MAPMEM|PCI_MAPMEMP, PCI_MAPPORT,
284                 PCI_MAPMEM|PCI_MAPMEMP, 0,
285                 PCI_MAPMEM|PCI_MAPMEMP, PCI_MAPPORT,
286                 0,                      0,
287         };
288
289         return maptype[mapreg & 0x0f];
290 }
291
292 /* return log2 of map size decoded for memory or port map */
293
294 static int
295 pci_mapsize(uint32_t testval)
296 {
297         int ln2size;
298
299         testval = pci_mapbase(testval);
300         ln2size = 0;
301         if (testval != 0) {
302                 while ((testval & 1) == 0)
303                 {
304                         ln2size++;
305                         testval >>= 1;
306                 }
307         }
308         return (ln2size);
309 }
310
311 /* return log2 of address range supported by map register */
312
313 static int
314 pci_maprange(unsigned mapreg)
315 {
316         int ln2range = 0;
317         switch (mapreg & 0x07) {
318         case 0x00:
319         case 0x01:
320         case 0x05:
321                 ln2range = 32;
322                 break;
323         case 0x02:
324                 ln2range = 20;
325                 break;
326         case 0x04:
327                 ln2range = 64;
328                 break;
329         }
330         return (ln2range);
331 }
332
333 /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */
334
335 static void
336 pci_fixancient(pcicfgregs *cfg)
337 {
338         if (cfg->hdrtype != 0)
339                 return;
340
341         /* PCI to PCI bridges use header type 1 */
342         if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI)
343                 cfg->hdrtype = 1;
344 }
345
346 /* extract header type specific config data */
347
348 static void
349 pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg)
350 {
351 #define REG(n, w)       PCIB_READ_CONFIG(pcib, b, s, f, n, w)
352         switch (cfg->hdrtype) {
353         case 0:
354                 cfg->subvendor      = REG(PCIR_SUBVEND_0, 2);
355                 cfg->subdevice      = REG(PCIR_SUBDEV_0, 2);
356                 cfg->nummaps        = PCI_MAXMAPS_0;
357                 break;
358         case 1:
359                 cfg->subvendor      = REG(PCIR_SUBVEND_1, 2);
360                 cfg->subdevice      = REG(PCIR_SUBDEV_1, 2);
361                 cfg->nummaps        = PCI_MAXMAPS_1;
362                 break;
363         case 2:
364                 cfg->subvendor      = REG(PCIR_SUBVEND_2, 2);
365                 cfg->subdevice      = REG(PCIR_SUBDEV_2, 2);
366                 cfg->nummaps        = PCI_MAXMAPS_2;
367                 break;
368         }
369 #undef REG
370 }
371
372 /* read configuration header into pcicfgregs structure */
373 struct pci_devinfo *
374 pci_read_device(device_t pcib, int b, int s, int f, size_t size)
375 {
376 #define REG(n, w)       PCIB_READ_CONFIG(pcib, b, s, f, n, w)
377         pcicfgregs *cfg = NULL;
378         struct pci_devinfo *devlist_entry;
379         struct devlist *devlist_head;
380
381         devlist_head = &pci_devq;
382
383         devlist_entry = NULL;
384
385         if (REG(PCIR_DEVVENDOR, 4) != -1) {
386                 devlist_entry = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
387                 if (devlist_entry == NULL)
388                         return (NULL);
389
390                 cfg = &devlist_entry->cfg;
391
392                 cfg->bus                = b;
393                 cfg->slot               = s;
394                 cfg->func               = f;
395                 cfg->vendor             = REG(PCIR_VENDOR, 2);
396                 cfg->device             = REG(PCIR_DEVICE, 2);
397                 cfg->cmdreg             = REG(PCIR_COMMAND, 2);
398                 cfg->statreg            = REG(PCIR_STATUS, 2);
399                 cfg->baseclass          = REG(PCIR_CLASS, 1);
400                 cfg->subclass           = REG(PCIR_SUBCLASS, 1);
401                 cfg->progif             = REG(PCIR_PROGIF, 1);
402                 cfg->revid              = REG(PCIR_REVID, 1);
403                 cfg->hdrtype            = REG(PCIR_HDRTYPE, 1);
404                 cfg->cachelnsz          = REG(PCIR_CACHELNSZ, 1);
405                 cfg->lattimer           = REG(PCIR_LATTIMER, 1);
406                 cfg->intpin             = REG(PCIR_INTPIN, 1);
407                 cfg->intline            = REG(PCIR_INTLINE, 1);
408
409                 cfg->mingnt             = REG(PCIR_MINGNT, 1);
410                 cfg->maxlat             = REG(PCIR_MAXLAT, 1);
411
412                 cfg->mfdev              = (cfg->hdrtype & PCIM_MFDEV) != 0;
413                 cfg->hdrtype            &= ~PCIM_MFDEV;
414
415                 pci_fixancient(cfg);
416                 pci_hdrtypedata(pcib, b, s, f, cfg);
417
418                 if (REG(PCIR_STATUS, 2) & PCIM_STATUS_CAPPRESENT)
419                         pci_read_extcap(pcib, cfg);
420
421                 STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links);
422
423                 devlist_entry->conf.pc_sel.pc_bus = cfg->bus;
424                 devlist_entry->conf.pc_sel.pc_dev = cfg->slot;
425                 devlist_entry->conf.pc_sel.pc_func = cfg->func;
426                 devlist_entry->conf.pc_hdr = cfg->hdrtype;
427
428                 devlist_entry->conf.pc_subvendor = cfg->subvendor;
429                 devlist_entry->conf.pc_subdevice = cfg->subdevice;
430                 devlist_entry->conf.pc_vendor = cfg->vendor;
431                 devlist_entry->conf.pc_device = cfg->device;
432
433                 devlist_entry->conf.pc_class = cfg->baseclass;
434                 devlist_entry->conf.pc_subclass = cfg->subclass;
435                 devlist_entry->conf.pc_progif = cfg->progif;
436                 devlist_entry->conf.pc_revid = cfg->revid;
437
438                 pci_numdevs++;
439                 pci_generation++;
440         }
441         return (devlist_entry);
442 #undef REG
443 }
444
445 static void
446 pci_read_extcap(device_t pcib, pcicfgregs *cfg)
447 {
448 #define REG(n, w)       PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
449 #define WREG(n, v, w)   PCIB_WRITE_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, v, w)
450 #if defined(__i386__) || defined(__amd64__)
451         uint64_t addr;
452 #endif
453         uint32_t val;
454         int     ptr, nextptr, ptrptr;
455
456         switch (cfg->hdrtype & PCIM_HDRTYPE) {
457         case 0:
458         case 1:
459                 ptrptr = PCIR_CAP_PTR;
460                 break;
461         case 2:
462                 ptrptr = PCIR_CAP_PTR_2;        /* cardbus capabilities ptr */
463                 break;
464         default:
465                 return;         /* no extended capabilities support */
466         }
467         nextptr = REG(ptrptr, 1);       /* sanity check? */
468
469         /*
470          * Read capability entries.
471          */
472         while (nextptr != 0) {
473                 /* Sanity check */
474                 if (nextptr > 255) {
475                         printf("illegal PCI extended capability offset %d\n",
476                             nextptr);
477                         return;
478                 }
479                 /* Find the next entry */
480                 ptr = nextptr;
481                 nextptr = REG(ptr + PCICAP_NEXTPTR, 1);
482
483                 /* Process this entry */
484                 switch (REG(ptr + PCICAP_ID, 1)) {
485                 case PCIY_PMG:          /* PCI power management */
486                         if (cfg->pp.pp_cap == 0) {
487                                 cfg->pp.pp_cap = REG(ptr + PCIR_POWER_CAP, 2);
488                                 cfg->pp.pp_status = ptr + PCIR_POWER_STATUS;
489                                 cfg->pp.pp_pmcsr = ptr + PCIR_POWER_PMCSR;
490                                 if ((nextptr - ptr) > PCIR_POWER_DATA)
491                                         cfg->pp.pp_data = ptr + PCIR_POWER_DATA;
492                         }
493                         break;
494 #if defined(__i386__) || defined(__amd64__)
495                 case PCIY_HT:           /* HyperTransport */
496                         /* Determine HT-specific capability type. */
497                         val = REG(ptr + PCIR_HT_COMMAND, 2);
498                         switch (val & PCIM_HTCMD_CAP_MASK) {
499                         case PCIM_HTCAP_MSI_MAPPING:
500                                 /* Sanity check the mapping window. */
501                                 addr = REG(ptr + PCIR_HTMSI_ADDRESS_HI, 4);
502                                 addr <<= 32;
503                                 addr = REG(ptr + PCIR_HTMSI_ADDRESS_LO, 4);
504                                 if (addr != MSI_INTEL_ADDR_BASE)
505                                         device_printf(pcib,
506                     "HT Bridge at %d:%d:%d has non-default MSI window 0x%llx\n",
507                                             cfg->bus, cfg->slot, cfg->func,
508                                             (long long)addr);
509
510                                 /* Enable MSI -> HT mapping. */
511                                 val |= PCIM_HTCMD_MSI_ENABLE;
512                                 WREG(ptr + PCIR_HT_COMMAND, val, 2);
513                                 break;
514                         }
515                         break;
516 #endif
517                 case PCIY_MSI:          /* PCI MSI */
518                         cfg->msi.msi_location = ptr;
519                         cfg->msi.msi_ctrl = REG(ptr + PCIR_MSI_CTRL, 2);
520                         cfg->msi.msi_msgnum = 1 << ((cfg->msi.msi_ctrl &
521                                                      PCIM_MSICTRL_MMC_MASK)>>1);
522                         break;
523                 case PCIY_MSIX:         /* PCI MSI-X */
524                         cfg->msix.msix_location = ptr;
525                         cfg->msix.msix_ctrl = REG(ptr + PCIR_MSIX_CTRL, 2);
526                         cfg->msix.msix_msgnum = (cfg->msix.msix_ctrl &
527                             PCIM_MSIXCTRL_TABLE_SIZE) + 1;
528                         val = REG(ptr + PCIR_MSIX_TABLE, 4);
529                         cfg->msix.msix_table_bar = PCIR_BAR(val &
530                             PCIM_MSIX_BIR_MASK);
531                         cfg->msix.msix_table_offset = val & ~PCIM_MSIX_BIR_MASK;
532                         val = REG(ptr + PCIR_MSIX_PBA, 4);
533                         cfg->msix.msix_pba_bar = PCIR_BAR(val &
534                             PCIM_MSIX_BIR_MASK);
535                         cfg->msix.msix_pba_offset = val & ~PCIM_MSIX_BIR_MASK;
536                         break;
537                 case PCIY_VPD:          /* PCI Vital Product Data */
538                         cfg->vpd.vpd_reg = ptr;
539                         pci_read_vpd(pcib, cfg);
540                         break;
541                 default:
542                         break;
543                 }
544         }
545 /* REG and WREG use carry through to next functions */
546 }
547
548 /*
549  * PCI Vital Product Data
550  */
551 static uint32_t
552 pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg)
553 {
554
555         KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
556
557         WREG(cfg->vpd.vpd_reg + 2, reg, 2);
558         while ((REG(cfg->vpd.vpd_reg + 2, 2) & 0x8000) != 0x8000)
559                 DELAY(1);       /* limit looping */
560
561         return REG(cfg->vpd.vpd_reg + 4, 4);
562 }
563
564 #if 0
565 static void
566 pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t data)
567 {
568         KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
569
570         WREG(cfg->vpd.vpd_reg + 4, data, 4);
571         WREG(cfg->vpd.vpd_reg + 2, reg | 0x8000, 2);
572         while ((REG(cfg->vpd.vpd_reg + 2, 2) & 0x8000) == 0x8000)
573                 DELAY(1);       /* limit looping */
574
575         return;
576 }
577 #endif
578
579 struct vpd_readstate {
580         device_t        pcib;
581         pcicfgregs      *cfg;
582         uint32_t        val;
583         int             bytesinval;
584         int             off;
585         uint8_t         cksum;
586 };
587
588 static uint8_t
589 vpd_nextbyte(struct vpd_readstate *vrs)
590 {
591         uint8_t byte;
592
593         if (vrs->bytesinval == 0) {
594                 vrs->val = le32toh(pci_read_vpd_reg(vrs->pcib, vrs->cfg,
595                     vrs->off));
596                 vrs->off += 4;
597                 byte = vrs->val & 0xff;
598                 vrs->bytesinval = 3;
599         } else {
600                 vrs->val = vrs->val >> 8;
601                 byte = vrs->val & 0xff;
602                 vrs->bytesinval--;
603         }
604
605         vrs->cksum += byte;
606         return byte;
607 }
608
609 static void
610 pci_read_vpd(device_t pcib, pcicfgregs *cfg)
611 {
612         struct vpd_readstate vrs;
613         int state;
614         int name;
615         int remain;
616         int end;
617         int i;
618         uint8_t byte;
619         int alloc, off;         /* alloc/off for RO/W arrays */
620         int cksumvalid;
621         int dflen;
622
623         /* init vpd reader */
624         vrs.bytesinval = 0;
625         vrs.off = 0;
626         vrs.pcib = pcib;
627         vrs.cfg = cfg;
628         vrs.cksum = 0;
629
630         state = 0;
631         name = remain = i = 0;  /* shut up stupid gcc */
632         alloc = off = 0;        /* shut up stupid gcc */
633         dflen = 0;              /* shut up stupid gcc */
634         end = 0;
635         cksumvalid = -1;
636         for (; !end;) {
637                 byte = vpd_nextbyte(&vrs);
638 #if 0
639                 printf("vpd: val: %#x, off: %d, bytesinval: %d, byte: %#hhx, " \
640                     "state: %d, remain: %d, name: %#x, i: %d\n", vrs.val,
641                     vrs.off, vrs.bytesinval, byte, state, remain, name, i);
642 #endif
643                 switch (state) {
644                 case 0:         /* item name */
645                         if (byte & 0x80) {
646                                 remain = vpd_nextbyte(&vrs);
647                                 remain |= vpd_nextbyte(&vrs) << 8;
648                                 if (remain > (0x7f*4 - vrs.off)) {
649                                         end = 1;
650                                         printf(
651                             "pci%d:%d:%d: invalid vpd data, remain %#x\n",
652                                             cfg->bus, cfg->slot, cfg->func,
653                                             remain);
654                                 }
655                                 name = byte & 0x7f;
656                         } else {
657                                 remain = byte & 0x7;
658                                 name = (byte >> 3) & 0xf;
659                         }
660                         switch (name) {
661                         case 0x2:       /* String */
662                                 cfg->vpd.vpd_ident = malloc(remain + 1,
663                                     M_DEVBUF, M_WAITOK);
664                                 i = 0;
665                                 state = 1;
666                                 break;
667                         case 0xf:       /* End */
668                                 end = 1;
669                                 state = -1;
670                                 break;
671                         case 0x10:      /* VPD-R */
672                                 alloc = 8;
673                                 off = 0;
674                                 cfg->vpd.vpd_ros = malloc(alloc *
675                                     sizeof *cfg->vpd.vpd_ros, M_DEVBUF,
676                                     M_WAITOK);
677                                 state = 2;
678                                 break;
679                         case 0x11:      /* VPD-W */
680                                 alloc = 8;
681                                 off = 0;
682                                 cfg->vpd.vpd_w = malloc(alloc *
683                                     sizeof *cfg->vpd.vpd_w, M_DEVBUF,
684                                     M_WAITOK);
685                                 state = 5;
686                                 break;
687                         default:        /* Invalid data, abort */
688                                 end = 1;
689                                 continue;
690                         }
691                         break;
692
693                 case 1: /* Identifier String */
694                         cfg->vpd.vpd_ident[i++] = byte;
695                         remain--;
696                         if (remain == 0)  {
697                                 cfg->vpd.vpd_ident[i] = '\0';
698                                 state = 0;
699                         }
700                         break;
701
702                 case 2: /* VPD-R Keyword Header */
703                         if (off == alloc) {
704                                 cfg->vpd.vpd_ros = reallocf(cfg->vpd.vpd_ros,
705                                     (alloc *= 2) * sizeof *cfg->vpd.vpd_ros,
706                                     M_DEVBUF, M_WAITOK);
707                         }
708                         cfg->vpd.vpd_ros[off].keyword[0] = byte;
709                         cfg->vpd.vpd_ros[off].keyword[1] = vpd_nextbyte(&vrs);
710                         dflen = vpd_nextbyte(&vrs);
711                         if (dflen == 0 &&
712                             strncmp(cfg->vpd.vpd_ros[off].keyword, "RV",
713                             2) == 0) {
714                                 /*
715                                  * if this happens, we can't trust the rest
716                                  * of the VPD.
717                                  */
718                                 printf("pci%d:%d:%d: bad keyword length: %d\n",
719                                     cfg->bus, cfg->slot, cfg->func, dflen);
720                                 cksumvalid = 0;
721                                 end = 1;
722                                 break;
723                         } else if (dflen == 0) {
724                                 cfg->vpd.vpd_ros[off].value = malloc(1 *
725                                     sizeof *cfg->vpd.vpd_ros[off].value,
726                                     M_DEVBUF, M_WAITOK);
727                                 cfg->vpd.vpd_ros[off].value[0] = '\x00';
728                         } else
729                                 cfg->vpd.vpd_ros[off].value = malloc(
730                                     (dflen + 1) *
731                                     sizeof *cfg->vpd.vpd_ros[off].value,
732                                     M_DEVBUF, M_WAITOK);
733                         remain -= 3;
734                         i = 0;
735                         /* keep in sync w/ state 3's transistions */
736                         if (dflen == 0 && remain == 0)
737                                 state = 0;
738                         else if (dflen == 0)
739                                 state = 2;
740                         else
741                                 state = 3;
742                         break;
743
744                 case 3: /* VPD-R Keyword Value */
745                         cfg->vpd.vpd_ros[off].value[i++] = byte;
746                         if (strncmp(cfg->vpd.vpd_ros[off].keyword,
747                             "RV", 2) == 0 && cksumvalid == -1) {
748                                 if (vrs.cksum == 0)
749                                         cksumvalid = 1;
750                                 else {
751                                         printf(
752                                     "pci%d:%d:%d: bad VPD cksum, remain %hhu\n",
753                                             cfg->bus, cfg->slot, cfg->func,
754                                             vrs.cksum);
755                                         cksumvalid = 0;
756                                         end = 1;
757                                         break;
758                                 }
759                         }
760                         dflen--;
761                         remain--;
762                         /* keep in sync w/ state 2's transistions */
763                         if (dflen == 0)
764                                 cfg->vpd.vpd_ros[off++].value[i++] = '\0';
765                         if (dflen == 0 && remain == 0) {
766                                 cfg->vpd.vpd_rocnt = off;
767                                 cfg->vpd.vpd_ros = reallocf(cfg->vpd.vpd_ros,
768                                     off * sizeof *cfg->vpd.vpd_ros,
769                                     M_DEVBUF, M_WAITOK);
770                                 state = 0;
771                         } else if (dflen == 0)
772                                 state = 2;
773                         break;
774
775                 case 4:
776                         remain--;
777                         if (remain == 0)
778                                 state = 0;
779                         break;
780
781                 case 5: /* VPD-W Keyword Header */
782                         if (off == alloc) {
783                                 cfg->vpd.vpd_w = reallocf(cfg->vpd.vpd_w,
784                                     (alloc *= 2) * sizeof *cfg->vpd.vpd_w,
785                                     M_DEVBUF, M_WAITOK);
786                         }
787                         cfg->vpd.vpd_w[off].keyword[0] = byte;
788                         cfg->vpd.vpd_w[off].keyword[1] = vpd_nextbyte(&vrs);
789                         cfg->vpd.vpd_w[off].len = dflen = vpd_nextbyte(&vrs);
790                         cfg->vpd.vpd_w[off].start = vrs.off - vrs.bytesinval;
791                         cfg->vpd.vpd_w[off].value = malloc((dflen + 1) *
792                             sizeof *cfg->vpd.vpd_w[off].value,
793                             M_DEVBUF, M_WAITOK);
794                         remain -= 3;
795                         i = 0;
796                         /* keep in sync w/ state 6's transistions */
797                         if (dflen == 0 && remain == 0)
798                                 state = 0;
799                         else if (dflen == 0)
800                                 state = 5;
801                         else
802                                 state = 6;
803                         break;
804
805                 case 6: /* VPD-W Keyword Value */
806                         cfg->vpd.vpd_w[off].value[i++] = byte;
807                         dflen--;
808                         remain--;
809                         /* keep in sync w/ state 5's transistions */
810                         if (dflen == 0)
811                                 cfg->vpd.vpd_w[off++].value[i++] = '\0';
812                         if (dflen == 0 && remain == 0) {
813                                 cfg->vpd.vpd_wcnt = off;
814                                 cfg->vpd.vpd_w = reallocf(cfg->vpd.vpd_w,
815                                     off * sizeof *cfg->vpd.vpd_w,
816                                     M_DEVBUF, M_WAITOK);
817                                 state = 0;
818                         } else if (dflen == 0)
819                                 state = 5;
820                         break;
821
822                 default:
823                         printf("pci%d:%d:%d: invalid state: %d\n",
824                             cfg->bus, cfg->slot, cfg->func, state);
825                         end = 1;
826                         break;
827                 }
828         }
829
830         if (cksumvalid == 0) {
831                 /* read-only data bad, clean up */
832                 for (; off; off--)
833                         free(cfg->vpd.vpd_ros[off].value, M_DEVBUF);
834
835                 free(cfg->vpd.vpd_ros, M_DEVBUF);
836                 cfg->vpd.vpd_ros = NULL;
837         }
838 #undef REG
839 #undef WREG
840 }
841
842 int
843 pci_get_vpd_ident_method(device_t dev, device_t child, const char **identptr)
844 {
845         struct pci_devinfo *dinfo = device_get_ivars(child);
846         pcicfgregs *cfg = &dinfo->cfg;
847
848         *identptr = cfg->vpd.vpd_ident;
849
850         if (*identptr == NULL)
851                 return ENXIO;
852
853         return 0;
854 }
855
856 int
857 pci_get_vpd_readonly_method(device_t dev, device_t child, const char *kw,
858         const char **vptr)
859 {
860         struct pci_devinfo *dinfo = device_get_ivars(child);
861         pcicfgregs *cfg = &dinfo->cfg;
862         int i;
863
864         for (i = 0; i < cfg->vpd.vpd_rocnt; i++)
865                 if (memcmp(kw, cfg->vpd.vpd_ros[i].keyword,
866                     sizeof cfg->vpd.vpd_ros[i].keyword) == 0) {
867                         *vptr = cfg->vpd.vpd_ros[i].value;
868                 }
869
870         if (i != cfg->vpd.vpd_rocnt)
871                 return 0;
872
873         *vptr = NULL;
874         return ENXIO;
875 }
876
877 /*
878  * Return the offset in configuration space of the requested extended
879  * capability entry or 0 if the specified capability was not found.
880  */
881 int
882 pci_find_extcap_method(device_t dev, device_t child, int capability,
883     int *capreg)
884 {
885         struct pci_devinfo *dinfo = device_get_ivars(child);
886         pcicfgregs *cfg = &dinfo->cfg;
887         u_int32_t status;
888         u_int8_t ptr;
889
890         /*
891          * Check the CAP_LIST bit of the PCI status register first.
892          */
893         status = pci_read_config(child, PCIR_STATUS, 2);
894         if (!(status & PCIM_STATUS_CAPPRESENT))
895                 return (ENXIO);
896
897         /*
898          * Determine the start pointer of the capabilities list.
899          */
900         switch (cfg->hdrtype & PCIM_HDRTYPE) {
901         case 0:
902         case 1:
903                 ptr = PCIR_CAP_PTR;
904                 break;
905         case 2:
906                 ptr = PCIR_CAP_PTR_2;
907                 break;
908         default:
909                 /* XXX: panic? */
910                 return (ENXIO);         /* no extended capabilities support */
911         }
912         ptr = pci_read_config(child, ptr, 1);
913
914         /*
915          * Traverse the capabilities list.
916          */
917         while (ptr != 0) {
918                 if (pci_read_config(child, ptr + PCICAP_ID, 1) == capability) {
919                         if (capreg != NULL)
920                                 *capreg = ptr;
921                         return (0);
922                 }
923                 ptr = pci_read_config(child, ptr + PCICAP_NEXTPTR, 1);
924         }
925
926         return (ENOENT);
927 }
928
929 /*
930  * Support for MSI-X message interrupts.
931  */
932 void
933 pci_enable_msix(device_t dev, u_int index, uint64_t address, uint32_t data)
934 {
935         struct pci_devinfo *dinfo = device_get_ivars(dev);
936         pcicfgregs *cfg = &dinfo->cfg;
937         uint32_t offset;
938
939         KASSERT(cfg->msix.msix_alloc > index, ("bogus index"));
940         offset = cfg->msix.msix_table_offset + index * 16;
941         bus_write_4(cfg->msix.msix_table_res, offset, address & 0xffffffff);
942         bus_write_4(cfg->msix.msix_table_res, offset + 4, address >> 32);
943         bus_write_4(cfg->msix.msix_table_res, offset + 8, data);
944 }
945
946 void
947 pci_mask_msix(device_t dev, u_int index)
948 {
949         struct pci_devinfo *dinfo = device_get_ivars(dev);
950         pcicfgregs *cfg = &dinfo->cfg;
951         uint32_t offset, val;
952
953         KASSERT(cfg->msix.msix_msgnum > index, ("bogus index"));
954         offset = cfg->msix.msix_table_offset + index * 16 + 12;
955         val = bus_read_4(cfg->msix.msix_table_res, offset);
956         if (!(val & PCIM_MSIX_VCTRL_MASK)) {
957                 val |= PCIM_MSIX_VCTRL_MASK;
958                 bus_write_4(cfg->msix.msix_table_res, offset, val);
959         }
960 }
961
962 void
963 pci_unmask_msix(device_t dev, u_int index)
964 {
965         struct pci_devinfo *dinfo = device_get_ivars(dev);
966         pcicfgregs *cfg = &dinfo->cfg;
967         uint32_t offset, val;
968
969         KASSERT(cfg->msix.msix_alloc > index, ("bogus index"));
970         offset = cfg->msix.msix_table_offset + index * 16 + 12;
971         val = bus_read_4(cfg->msix.msix_table_res, offset);
972         if (val & PCIM_MSIX_VCTRL_MASK) {
973                 val &= ~PCIM_MSIX_VCTRL_MASK;
974                 bus_write_4(cfg->msix.msix_table_res, offset, val);
975         }
976 }
977
978 int
979 pci_pending_msix(device_t dev, u_int index)
980 {
981         struct pci_devinfo *dinfo = device_get_ivars(dev);
982         pcicfgregs *cfg = &dinfo->cfg;
983         uint32_t offset, bit;
984
985         KASSERT(cfg->msix.msix_alloc > index, ("bogus index"));
986         offset = cfg->msix.msix_pba_offset + (index / 4) * 4;
987         bit = 1 << index % 32;
988         return (bus_read_4(cfg->msix.msix_pba_res, offset) & bit);
989 }
990
991 static int
992 pci_alloc_msix(device_t dev, device_t child, int *count)
993 {
994         struct pci_devinfo *dinfo = device_get_ivars(child);
995         pcicfgregs *cfg = &dinfo->cfg;
996         struct resource_list_entry *rle;
997         int actual, error, i, irq, max;
998
999         /* MSI-X capability present? */
1000         if (cfg->msix.msix_location == 0 || !pci_do_msix)
1001                 return (ENODEV);
1002
1003         /* Make sure the appropriate BARs are mapped. */
1004         rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1005             cfg->msix.msix_table_bar);
1006         if (rle == NULL || rle->res == NULL ||
1007             !(rman_get_flags(rle->res) & RF_ACTIVE))
1008                 return (ENXIO);
1009         cfg->msix.msix_table_res = rle->res;
1010         if (cfg->msix.msix_pba_bar != cfg->msix.msix_table_bar) {
1011                 rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1012                     cfg->msix.msix_pba_bar);
1013                 if (rle == NULL || rle->res == NULL ||
1014                     !(rman_get_flags(rle->res) & RF_ACTIVE))
1015                         return (ENXIO);
1016         }
1017         cfg->msix.msix_pba_res = rle->res;
1018
1019         /* Already have allocated messages? */
1020         if (cfg->msix.msix_alloc != 0)
1021                 return (ENXIO);
1022
1023         if (bootverbose)
1024                 device_printf(child,
1025                     "attempting to allocate %d MSI-X vectors (%d supported)\n",
1026                     *count, cfg->msix.msix_msgnum);
1027         max = min(*count, cfg->msix.msix_msgnum);
1028         for (i = 0; i < max; i++) {
1029                 /* Allocate a message. */
1030                 error = PCIB_ALLOC_MSIX(device_get_parent(dev), child, i,
1031                     &irq);
1032                 if (error)
1033                         break;
1034                 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1, irq,
1035                     irq, 1);
1036         }
1037         actual = i;
1038
1039         if (bootverbose) {
1040                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 1);
1041                 if (actual == 1)
1042                         device_printf(child, "using IRQ %lu for MSI-X\n",
1043                             rle->start);
1044                 else {
1045                         int run;
1046
1047                         /*
1048                          * Be fancy and try to print contiguous runs of
1049                          * IRQ values as ranges.  'irq' is the previous IRQ.
1050                          * 'run' is true if we are in a range.
1051                          */
1052                         device_printf(child, "using IRQs %lu", rle->start);
1053                         irq = rle->start;
1054                         run = 0;
1055                         for (i = 1; i < actual; i++) {
1056                                 rle = resource_list_find(&dinfo->resources,
1057                                     SYS_RES_IRQ, i + 1);
1058
1059                                 /* Still in a run? */
1060                                 if (rle->start == irq + 1) {
1061                                         run = 1;
1062                                         irq++;
1063                                         continue;
1064                                 }
1065
1066                                 /* Finish previous range. */
1067                                 if (run) {
1068                                         printf("-%d", irq);
1069                                         run = 0;
1070                                 }
1071
1072                                 /* Start new range. */
1073                                 printf(",%lu", rle->start);
1074                                 irq = rle->start;
1075                         }
1076
1077                         /* Unfinished range? */
1078                         if (run)
1079                                 printf("%d", irq);
1080                         printf(" for MSI-X\n");
1081                 }
1082         }
1083
1084         /* Mask all vectors. */
1085         for (i = 0; i < cfg->msix.msix_msgnum; i++)
1086                 pci_mask_msix(child, i);
1087
1088         /* Update control register to enable MSI-X. */
1089         cfg->msix.msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
1090         pci_write_config(child, cfg->msix.msix_location + PCIR_MSIX_CTRL,
1091             cfg->msix.msix_ctrl, 2);
1092
1093         /* Update counts of alloc'd messages. */
1094         cfg->msix.msix_alloc = actual;
1095         *count = actual;
1096         return (0);
1097 }
1098
1099 static int
1100 pci_release_msix(device_t dev, device_t child)
1101 {
1102         struct pci_devinfo *dinfo = device_get_ivars(child);
1103         pcicfgregs *cfg = &dinfo->cfg;
1104         struct resource_list_entry *rle;
1105         int i;
1106
1107         /* Do we have any messages to release? */
1108         if (cfg->msix.msix_alloc == 0)
1109                 return (ENODEV);
1110
1111         /* Make sure none of the resources are allocated. */
1112         for (i = 0; i < cfg->msix.msix_alloc; i++) {
1113                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1114                 KASSERT(rle != NULL, ("missing MSI resource"));
1115                 if (rle->res != NULL)
1116                         return (EBUSY);
1117         }
1118
1119         /* Update control register with to disable MSI-X. */
1120         cfg->msix.msix_ctrl &= ~PCIM_MSIXCTRL_MSIX_ENABLE;
1121         pci_write_config(child, cfg->msix.msix_location + PCIR_MSIX_CTRL,
1122             cfg->msix.msix_ctrl, 2);
1123
1124         /* Release the messages. */
1125         for (i = 0; i < cfg->msix.msix_alloc; i++) {
1126                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1127                 PCIB_RELEASE_MSIX(device_get_parent(dev), child,
1128                     rle->start);
1129                 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
1130         }
1131
1132         /* Update alloc count. */
1133         cfg->msix.msix_alloc = 0;
1134         return (0);
1135 }
1136
1137 /*
1138  * Support for MSI message signalled interrupts.
1139  */
1140 void
1141 pci_enable_msi(device_t dev, uint64_t address, uint16_t data)
1142 {
1143         struct pci_devinfo *dinfo = device_get_ivars(dev);
1144         pcicfgregs *cfg = &dinfo->cfg;
1145
1146         /* Write data and address values. */
1147         cfg->msi.msi_addr = address;
1148         cfg->msi.msi_data = data;
1149         pci_write_config(dev, cfg->msi.msi_location + PCIR_MSI_ADDR,
1150             address & 0xffffffff, 4);
1151         if (cfg->msi.msi_ctrl & PCIM_MSICTRL_64BIT) {
1152                 pci_write_config(dev, cfg->msi.msi_location +
1153                     PCIR_MSI_ADDR_HIGH, address >> 32, 4);
1154                 pci_write_config(dev, cfg->msi.msi_location +
1155                     PCIR_MSI_DATA_64BIT, data, 2);
1156         } else
1157                 pci_write_config(dev, cfg->msi.msi_location +
1158                     PCIR_MSI_DATA, data, 2);
1159
1160         /* Enable MSI in the control register. */
1161         cfg->msi.msi_ctrl |= PCIM_MSICTRL_MSI_ENABLE;
1162         pci_write_config(dev, cfg->msi.msi_location + PCIR_MSI_CTRL,
1163             cfg->msi.msi_ctrl, 2);
1164 }
1165
1166 /*
1167  * Restore MSI registers during resume.  If MSI is enabled then
1168  * restore the data and address registers in addition to the control
1169  * register.
1170  */
1171 static void
1172 pci_resume_msi(device_t dev)
1173 {
1174         struct pci_devinfo *dinfo = device_get_ivars(dev);
1175         pcicfgregs *cfg = &dinfo->cfg;
1176         uint64_t address;
1177         uint16_t data;
1178
1179         if (cfg->msi.msi_ctrl & PCIM_MSICTRL_MSI_ENABLE) {
1180                 address = cfg->msi.msi_addr;
1181                 data = cfg->msi.msi_data;
1182                 pci_write_config(dev, cfg->msi.msi_location + PCIR_MSI_ADDR,
1183                     address & 0xffffffff, 4);
1184                 if (cfg->msi.msi_ctrl & PCIM_MSICTRL_64BIT) {
1185                         pci_write_config(dev, cfg->msi.msi_location +
1186                             PCIR_MSI_ADDR_HIGH, address >> 32, 4);
1187                         pci_write_config(dev, cfg->msi.msi_location +
1188                             PCIR_MSI_DATA_64BIT, data, 2);
1189                 } else
1190                         pci_write_config(dev, cfg->msi.msi_location +
1191                             PCIR_MSI_DATA, data, 2);
1192         }
1193         pci_write_config(dev, cfg->msi.msi_location + PCIR_MSI_CTRL,
1194             cfg->msi.msi_ctrl, 2);
1195 }
1196
1197 /*
1198  * Attempt to allocate *count MSI messages.  The actual number allocated is
1199  * returned in *count.  After this function returns, each message will be
1200  * available to the driver as SYS_RES_IRQ resources starting at a rid 1.
1201  */
1202 int
1203 pci_alloc_msi_method(device_t dev, device_t child, int *count)
1204 {
1205         struct pci_devinfo *dinfo = device_get_ivars(child);
1206         pcicfgregs *cfg = &dinfo->cfg;
1207         struct resource_list_entry *rle;
1208         int actual, error, i, irqs[32];
1209         uint16_t ctrl;
1210
1211         /* Don't let count == 0 get us into trouble. */
1212         if (*count == 0)
1213                 return (EINVAL);
1214
1215         /* If rid 0 is allocated, then fail. */
1216         rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
1217         if (rle != NULL && rle->res != NULL)
1218                 return (ENXIO);
1219
1220         /* Try MSI-X first. */
1221         error = pci_alloc_msix(dev, child, count);
1222         if (error != ENODEV)
1223                 return (error);
1224
1225         /* MSI capability present? */
1226         if (cfg->msi.msi_location == 0 || !pci_do_msi)
1227                 return (ENODEV);
1228
1229         /* Already have allocated messages? */
1230         if (cfg->msi.msi_alloc != 0)
1231                 return (ENXIO);
1232
1233         if (bootverbose)
1234                 device_printf(child,
1235                     "attempting to allocate %d MSI vectors (%d supported)\n",
1236                     *count, cfg->msi.msi_msgnum);
1237
1238         /* Don't ask for more than the device supports. */
1239         actual = min(*count, cfg->msi.msi_msgnum);
1240
1241         /* Don't ask for more than 32 messages. */
1242         actual = min(actual, 32);
1243
1244         /* MSI requires power of 2 number of messages. */
1245         if (!powerof2(actual))
1246                 return (EINVAL);
1247
1248         for (;;) {
1249                 /* Try to allocate N messages. */
1250                 error = PCIB_ALLOC_MSI(device_get_parent(dev), child, actual,
1251                     cfg->msi.msi_msgnum, irqs);
1252                 if (error == 0)
1253                         break;
1254                 if (actual == 1)
1255                         return (error);
1256
1257                 /* Try N / 2. */
1258                 actual >>= 1;
1259         }
1260
1261         /*
1262          * We now have N actual messages mapped onto SYS_RES_IRQ
1263          * resources in the irqs[] array, so add new resources
1264          * starting at rid 1.
1265          */
1266         for (i = 0; i < actual; i++)
1267                 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1,
1268                     irqs[i], irqs[i], 1);
1269
1270         if (bootverbose) {
1271                 if (actual == 1)
1272                         device_printf(child, "using IRQ %d for MSI\n", irqs[0]);
1273                 else {
1274                         int run;
1275
1276                         /*
1277                          * Be fancy and try to print contiguous runs
1278                          * of IRQ values as ranges.  'run' is true if
1279                          * we are in a range.
1280                          */
1281                         device_printf(child, "using IRQs %d", irqs[0]);
1282                         run = 0;
1283                         for (i = 1; i < actual; i++) {
1284
1285                                 /* Still in a run? */
1286                                 if (irqs[i] == irqs[i - 1] + 1) {
1287                                         run = 1;
1288                                         continue;
1289                                 }
1290
1291                                 /* Finish previous range. */
1292                                 if (run) {
1293                                         printf("-%d", irqs[i - 1]);
1294                                         run = 0;
1295                                 }
1296
1297                                 /* Start new range. */
1298                                 printf(",%d", irqs[i]);
1299                         }
1300
1301                         /* Unfinished range? */
1302                         if (run)
1303                                 printf("%d", irqs[actual - 1]);
1304                         printf(" for MSI\n");
1305                 }
1306         }
1307
1308         /* Update control register with actual count and enable MSI. */
1309         ctrl = cfg->msi.msi_ctrl;
1310         ctrl &= ~PCIM_MSICTRL_MME_MASK;
1311         ctrl |= (ffs(actual) - 1) << 4;
1312         cfg->msi.msi_ctrl = ctrl;
1313         pci_write_config(child, cfg->msi.msi_location + PCIR_MSI_CTRL, ctrl, 2);
1314
1315         /* Update counts of alloc'd messages. */
1316         cfg->msi.msi_alloc = actual;
1317         *count = actual;
1318         return (0);
1319 }
1320
1321 /* Release the MSI messages associated with this device. */
1322 int
1323 pci_release_msi_method(device_t dev, device_t child)
1324 {
1325         struct pci_devinfo *dinfo = device_get_ivars(child);
1326         pcicfgregs *cfg = &dinfo->cfg;
1327         struct resource_list_entry *rle;
1328         int error, i, irqs[32];
1329
1330         /* Try MSI-X first. */
1331         error = pci_release_msix(dev, child);
1332         if (error != ENODEV)
1333                 return (error);
1334
1335         /* Do we have any messages to release? */
1336         if (cfg->msi.msi_alloc == 0)
1337                 return (ENODEV);
1338         KASSERT(cfg->msi.msi_alloc <= 32, ("more than 32 alloc'd messages"));
1339
1340         /* Make sure none of the resources are allocated. */
1341         for (i = 0; i < cfg->msi.msi_alloc; i++) {
1342                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1343                 KASSERT(rle != NULL, ("missing MSI resource"));
1344                 if (rle->res != NULL)
1345                         return (EBUSY);
1346                 irqs[i] = rle->start;
1347         }
1348
1349         /* Update control register with 0 count and disable MSI. */
1350         cfg->msi.msi_ctrl &= ~(PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE);
1351         pci_write_config(child, cfg->msi.msi_location + PCIR_MSI_CTRL,
1352             cfg->msi.msi_ctrl, 2);
1353
1354         /* Release the messages. */
1355         PCIB_RELEASE_MSI(device_get_parent(dev), child, cfg->msi.msi_alloc,
1356             irqs);
1357         for (i = 0; i < cfg->msi.msi_alloc; i++)
1358                 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
1359
1360         /* Update alloc count. */
1361         cfg->msi.msi_alloc = 0;
1362         return (0);
1363 }
1364
1365 /*
1366  * Return the max supported MSI or MSI-X messages this device supports.
1367  * Basically, assuming the MD code can alloc messages, this function
1368  * should return the maximum value that pci_alloc_msi() can return.  Thus,
1369  * it is subject to the tunables, etc.
1370  */
1371 int
1372 pci_msi_count_method(device_t dev, device_t child)
1373 {
1374         struct pci_devinfo *dinfo = device_get_ivars(child);
1375         pcicfgregs *cfg = &dinfo->cfg;
1376
1377         if (pci_do_msix && cfg->msix.msix_location != 0)
1378                 return (cfg->msix.msix_msgnum);
1379         if (pci_do_msi && cfg->msi.msi_location != 0)
1380                 return (cfg->msi.msi_msgnum);
1381         return (0);
1382 }
1383
1384 /* free pcicfgregs structure and all depending data structures */
1385
1386 int
1387 pci_freecfg(struct pci_devinfo *dinfo)
1388 {
1389         struct devlist *devlist_head;
1390         int i;
1391
1392         devlist_head = &pci_devq;
1393
1394         if (dinfo->cfg.vpd.vpd_reg) {
1395                 free(dinfo->cfg.vpd.vpd_ident, M_DEVBUF);
1396                 for (i = 0; i < dinfo->cfg.vpd.vpd_rocnt; i++)
1397                         free(dinfo->cfg.vpd.vpd_ros[i].value, M_DEVBUF);
1398                 free(dinfo->cfg.vpd.vpd_ros, M_DEVBUF);
1399                 for (i = 0; i < dinfo->cfg.vpd.vpd_wcnt; i++)
1400                         free(dinfo->cfg.vpd.vpd_w[i].value, M_DEVBUF);
1401                 free(dinfo->cfg.vpd.vpd_w, M_DEVBUF);
1402         }
1403         STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links);
1404         free(dinfo, M_DEVBUF);
1405
1406         /* increment the generation count */
1407         pci_generation++;
1408
1409         /* we're losing one device */
1410         pci_numdevs--;
1411         return (0);
1412 }
1413
1414 /*
1415  * PCI power manangement
1416  */
1417 int
1418 pci_set_powerstate_method(device_t dev, device_t child, int state)
1419 {
1420         struct pci_devinfo *dinfo = device_get_ivars(child);
1421         pcicfgregs *cfg = &dinfo->cfg;
1422         uint16_t status;
1423         int result, oldstate, highest, delay;
1424
1425         if (cfg->pp.pp_cap == 0)
1426                 return (EOPNOTSUPP);
1427
1428         /*
1429          * Optimize a no state change request away.  While it would be OK to
1430          * write to the hardware in theory, some devices have shown odd
1431          * behavior when going from D3 -> D3.
1432          */
1433         oldstate = pci_get_powerstate(child);
1434         if (oldstate == state)
1435                 return (0);
1436
1437         /*
1438          * The PCI power management specification states that after a state
1439          * transition between PCI power states, system software must
1440          * guarantee a minimal delay before the function accesses the device.
1441          * Compute the worst case delay that we need to guarantee before we
1442          * access the device.  Many devices will be responsive much more
1443          * quickly than this delay, but there are some that don't respond
1444          * instantly to state changes.  Transitions to/from D3 state require
1445          * 10ms, while D2 requires 200us, and D0/1 require none.  The delay
1446          * is done below with DELAY rather than a sleeper function because
1447          * this function can be called from contexts where we cannot sleep.
1448          */
1449         highest = (oldstate > state) ? oldstate : state;
1450         if (highest == PCI_POWERSTATE_D3)
1451             delay = 10000;
1452         else if (highest == PCI_POWERSTATE_D2)
1453             delay = 200;
1454         else
1455             delay = 0;
1456         status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2)
1457             & ~PCIM_PSTAT_DMASK;
1458         result = 0;
1459         switch (state) {
1460         case PCI_POWERSTATE_D0:
1461                 status |= PCIM_PSTAT_D0;
1462                 break;
1463         case PCI_POWERSTATE_D1:
1464                 if ((cfg->pp.pp_cap & PCIM_PCAP_D1SUPP) == 0)
1465                         return (EOPNOTSUPP);
1466                 status |= PCIM_PSTAT_D1;
1467                 break;
1468         case PCI_POWERSTATE_D2:
1469                 if ((cfg->pp.pp_cap & PCIM_PCAP_D2SUPP) == 0)
1470                         return (EOPNOTSUPP);
1471                 status |= PCIM_PSTAT_D2;
1472                 break;
1473         case PCI_POWERSTATE_D3:
1474                 status |= PCIM_PSTAT_D3;
1475                 break;
1476         default:
1477                 return (EINVAL);
1478         }
1479
1480         if (bootverbose)
1481                 printf(
1482                     "pci%d:%d:%d: Transition from D%d to D%d\n",
1483                     dinfo->cfg.bus, dinfo->cfg.slot, dinfo->cfg.func,
1484                     oldstate, state);
1485
1486         PCI_WRITE_CONFIG(dev, child, cfg->pp.pp_status, status, 2);
1487         if (delay)
1488                 DELAY(delay);
1489         return (0);
1490 }
1491
1492 int
1493 pci_get_powerstate_method(device_t dev, device_t child)
1494 {
1495         struct pci_devinfo *dinfo = device_get_ivars(child);
1496         pcicfgregs *cfg = &dinfo->cfg;
1497         uint16_t status;
1498         int result;
1499
1500         if (cfg->pp.pp_cap != 0) {
1501                 status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2);
1502                 switch (status & PCIM_PSTAT_DMASK) {
1503                 case PCIM_PSTAT_D0:
1504                         result = PCI_POWERSTATE_D0;
1505                         break;
1506                 case PCIM_PSTAT_D1:
1507                         result = PCI_POWERSTATE_D1;
1508                         break;
1509                 case PCIM_PSTAT_D2:
1510                         result = PCI_POWERSTATE_D2;
1511                         break;
1512                 case PCIM_PSTAT_D3:
1513                         result = PCI_POWERSTATE_D3;
1514                         break;
1515                 default:
1516                         result = PCI_POWERSTATE_UNKNOWN;
1517                         break;
1518                 }
1519         } else {
1520                 /* No support, device is always at D0 */
1521                 result = PCI_POWERSTATE_D0;
1522         }
1523         return (result);
1524 }
1525
1526 /*
1527  * Some convenience functions for PCI device drivers.
1528  */
1529
1530 static __inline void
1531 pci_set_command_bit(device_t dev, device_t child, uint16_t bit)
1532 {
1533         uint16_t        command;
1534
1535         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
1536         command |= bit;
1537         PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
1538 }
1539
1540 static __inline void
1541 pci_clear_command_bit(device_t dev, device_t child, uint16_t bit)
1542 {
1543         uint16_t        command;
1544
1545         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
1546         command &= ~bit;
1547         PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
1548 }
1549
1550 int
1551 pci_enable_busmaster_method(device_t dev, device_t child)
1552 {
1553         pci_set_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
1554         return (0);
1555 }
1556
1557 int
1558 pci_disable_busmaster_method(device_t dev, device_t child)
1559 {
1560         pci_clear_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
1561         return (0);
1562 }
1563
1564 int
1565 pci_enable_io_method(device_t dev, device_t child, int space)
1566 {
1567         uint16_t command;
1568         uint16_t bit;
1569         char *error;
1570
1571         bit = 0;
1572         error = NULL;
1573
1574         switch(space) {
1575         case SYS_RES_IOPORT:
1576                 bit = PCIM_CMD_PORTEN;
1577                 error = "port";
1578                 break;
1579         case SYS_RES_MEMORY:
1580                 bit = PCIM_CMD_MEMEN;
1581                 error = "memory";
1582                 break;
1583         default:
1584                 return (EINVAL);
1585         }
1586         pci_set_command_bit(dev, child, bit);
1587         /* Some devices seem to need a brief stall here, what do to? */
1588         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
1589         if (command & bit)
1590                 return (0);
1591         device_printf(child, "failed to enable %s mapping!\n", error);
1592         return (ENXIO);
1593 }
1594
1595 int
1596 pci_disable_io_method(device_t dev, device_t child, int space)
1597 {
1598         uint16_t command;
1599         uint16_t bit;
1600         char *error;
1601
1602         bit = 0;
1603         error = NULL;
1604
1605         switch(space) {
1606         case SYS_RES_IOPORT:
1607                 bit = PCIM_CMD_PORTEN;
1608                 error = "port";
1609                 break;
1610         case SYS_RES_MEMORY:
1611                 bit = PCIM_CMD_MEMEN;
1612                 error = "memory";
1613                 break;
1614         default:
1615                 return (EINVAL);
1616         }
1617         pci_clear_command_bit(dev, child, bit);
1618         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
1619         if (command & bit) {
1620                 device_printf(child, "failed to disable %s mapping!\n", error);
1621                 return (ENXIO);
1622         }
1623         return (0);
1624 }
1625
1626 /*
1627  * New style pci driver.  Parent device is either a pci-host-bridge or a
1628  * pci-pci-bridge.  Both kinds are represented by instances of pcib.
1629  */
1630
1631 void
1632 pci_print_verbose(struct pci_devinfo *dinfo)
1633 {
1634         int i;
1635
1636         if (bootverbose) {
1637                 pcicfgregs *cfg = &dinfo->cfg;
1638
1639                 printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n",
1640                     cfg->vendor, cfg->device, cfg->revid);
1641                 printf("\tbus=%d, slot=%d, func=%d\n",
1642                     cfg->bus, cfg->slot, cfg->func);
1643                 printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n",
1644                     cfg->baseclass, cfg->subclass, cfg->progif, cfg->hdrtype,
1645                     cfg->mfdev);
1646                 printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n",
1647                     cfg->cmdreg, cfg->statreg, cfg->cachelnsz);
1648                 printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n",
1649                     cfg->lattimer, cfg->lattimer * 30, cfg->mingnt,
1650                     cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250);
1651                 if (cfg->intpin > 0)
1652                         printf("\tintpin=%c, irq=%d\n",
1653                             cfg->intpin +'a' -1, cfg->intline);
1654                 if (cfg->pp.pp_cap) {
1655                         uint16_t status;
1656
1657                         status = pci_read_config(cfg->dev, cfg->pp.pp_status, 2);
1658                         printf("\tpowerspec %d  supports D0%s%s D3  current D%d\n",
1659                             cfg->pp.pp_cap & PCIM_PCAP_SPEC,
1660                             cfg->pp.pp_cap & PCIM_PCAP_D1SUPP ? " D1" : "",
1661                             cfg->pp.pp_cap & PCIM_PCAP_D2SUPP ? " D2" : "",
1662                             status & PCIM_PSTAT_DMASK);
1663                 }
1664                 if (cfg->vpd.vpd_reg) {
1665                         printf("\tVPD Ident: %s\n", cfg->vpd.vpd_ident);
1666                         for (i = 0; i < cfg->vpd.vpd_rocnt; i++) {
1667                                 struct vpd_readonly *vrop;
1668                                 vrop = &cfg->vpd.vpd_ros[i];
1669                                 if (strncmp("CP", vrop->keyword, 2) == 0)
1670                                         printf("\tCP: id %d, BAR%d, off %#x\n",
1671                                             vrop->value[0], vrop->value[1],
1672                                             le16toh(
1673                                               *(uint16_t *)&vrop->value[2]));
1674                                 else if (strncmp("RV", vrop->keyword, 2) == 0)
1675                                         printf("\tRV: %#hhx\n", vrop->value[0]);
1676                                 else
1677                                         printf("\t%.2s: %s\n", vrop->keyword,
1678                                             vrop->value);
1679                         }
1680                         for (i = 0; i < cfg->vpd.vpd_wcnt; i++) {
1681                                 struct vpd_write *vwp;
1682                                 vwp = &cfg->vpd.vpd_w[i];
1683                                 if (strncmp("RW", vwp->keyword, 2) != 0)
1684                                         printf("\t%.2s(%#x-%#x): %s\n",
1685                                             vwp->keyword, vwp->start,
1686                                             vwp->start + vwp->len, vwp->value);
1687                         }
1688                 }
1689                 if (cfg->msi.msi_location) {
1690                         int ctrl;
1691
1692                         ctrl = cfg->msi.msi_ctrl;
1693                         printf("\tMSI supports %d message%s%s%s\n",
1694                             cfg->msi.msi_msgnum,
1695                             (cfg->msi.msi_msgnum == 1) ? "" : "s",
1696                             (ctrl & PCIM_MSICTRL_64BIT) ? ", 64 bit" : "",
1697                             (ctrl & PCIM_MSICTRL_VECTOR) ? ", vector masks":"");
1698                 }
1699                 if (cfg->msix.msix_location) {
1700                         printf("\tMSI-X supports %d message%s ",
1701                             cfg->msix.msix_msgnum,
1702                             (cfg->msix.msix_msgnum == 1) ? "" : "s");
1703                         if (cfg->msix.msix_table_bar == cfg->msix.msix_pba_bar)
1704                                 printf("in map 0x%x\n",
1705                                     cfg->msix.msix_table_bar);
1706                         else
1707                                 printf("in maps 0x%x and 0x%x\n",
1708                                     cfg->msix.msix_table_bar,
1709                                     cfg->msix.msix_pba_bar);
1710                 }
1711         }
1712 }
1713
1714 static int
1715 pci_porten(device_t pcib, int b, int s, int f)
1716 {
1717         return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2)
1718                 & PCIM_CMD_PORTEN) != 0;
1719 }
1720
1721 static int
1722 pci_memen(device_t pcib, int b, int s, int f)
1723 {
1724         return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2)
1725                 & PCIM_CMD_MEMEN) != 0;
1726 }
1727
1728 /*
1729  * Add a resource based on a pci map register. Return 1 if the map
1730  * register is a 32bit map register or 2 if it is a 64bit register.
1731  */
1732 static int
1733 pci_add_map(device_t pcib, device_t bus, device_t dev,
1734     int b, int s, int f, int reg, struct resource_list *rl, int force,
1735     int prefetch)
1736 {
1737         uint32_t map;
1738         pci_addr_t base;
1739         pci_addr_t start, end, count;
1740         uint8_t ln2size;
1741         uint8_t ln2range;
1742         uint32_t testval;
1743         uint16_t cmd;
1744         int type;
1745         int barlen;
1746         struct resource *res;
1747
1748         map = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4);
1749         PCIB_WRITE_CONFIG(pcib, b, s, f, reg, 0xffffffff, 4);
1750         testval = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4);
1751         PCIB_WRITE_CONFIG(pcib, b, s, f, reg, map, 4);
1752
1753         if (pci_maptype(map) & PCI_MAPMEM)
1754                 type = SYS_RES_MEMORY;
1755         else
1756                 type = SYS_RES_IOPORT;
1757         ln2size = pci_mapsize(testval);
1758         ln2range = pci_maprange(testval);
1759         base = pci_mapbase(map);
1760         barlen = ln2range == 64 ? 2 : 1;
1761
1762         /*
1763          * For I/O registers, if bottom bit is set, and the next bit up
1764          * isn't clear, we know we have a BAR that doesn't conform to the
1765          * spec, so ignore it.  Also, sanity check the size of the data
1766          * areas to the type of memory involved.  Memory must be at least
1767          * 16 bytes in size, while I/O ranges must be at least 4.
1768          */
1769         if ((testval & 0x1) == 0x1 &&
1770             (testval & 0x2) != 0)
1771                 return (barlen);
1772         if ((type == SYS_RES_MEMORY && ln2size < 4) ||
1773             (type == SYS_RES_IOPORT && ln2size < 2))
1774                 return (barlen);
1775
1776         if (ln2range == 64)
1777                 /* Read the other half of a 64bit map register */
1778                 base |= (uint64_t) PCIB_READ_CONFIG(pcib, b, s, f, reg + 4, 4) << 32;
1779         if (bootverbose) {
1780                 printf("\tmap[%02x]: type %x, range %2d, base %#jx, size %2d",
1781                     reg, pci_maptype(map), ln2range, (uintmax_t)base, ln2size);
1782                 if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f))
1783                         printf(", port disabled\n");
1784                 else if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f))
1785                         printf(", memory disabled\n");
1786                 else
1787                         printf(", enabled\n");
1788         }
1789
1790         /*
1791          * If base is 0, then we have problems.  It is best to ignore
1792          * such entries for the moment.  These will be allocated later if
1793          * the driver specifically requests them.  However, some
1794          * removable busses look better when all resources are allocated,
1795          * so allow '0' to be overriden.
1796          *
1797          * Similarly treat maps whose values is the same as the test value
1798          * read back.  These maps have had all f's written to them by the
1799          * BIOS in an attempt to disable the resources.
1800          */
1801         if (!force && (base == 0 || map == testval))
1802                 return (barlen);
1803         if ((u_long)base != base) {
1804                 device_printf(bus,
1805                     "pci%d:%d:%d bar %#x too many address bits", b, s, f, reg);
1806                 return (barlen);
1807         }
1808
1809         /*
1810          * This code theoretically does the right thing, but has
1811          * undesirable side effects in some cases where peripherals
1812          * respond oddly to having these bits enabled.  Let the user
1813          * be able to turn them off (since pci_enable_io_modes is 1 by
1814          * default).
1815          */
1816         if (pci_enable_io_modes) {
1817                 /* Turn on resources that have been left off by a lazy BIOS */
1818                 if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f)) {
1819                         cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2);
1820                         cmd |= PCIM_CMD_PORTEN;
1821                         PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2);
1822                 }
1823                 if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f)) {
1824                         cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2);
1825                         cmd |= PCIM_CMD_MEMEN;
1826                         PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2);
1827                 }
1828         } else {
1829                 if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f))
1830                         return (barlen);
1831                 if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f))
1832                         return (barlen);
1833         }
1834
1835         count = 1 << ln2size;
1836         if (base == 0 || base == pci_mapbase(testval)) {
1837                 start = 0;      /* Let the parent deside */
1838                 end = ~0ULL;
1839         } else {
1840                 start = base;
1841                 end = base + (1 << ln2size) - 1;
1842         }
1843         resource_list_add(rl, type, reg, start, end, count);
1844
1845         /*
1846          * Not quite sure what to do on failure of allocating the resource
1847          * since I can postulate several right answers.
1848          */
1849         res = resource_list_alloc(rl, bus, dev, type, &reg, start, end, count,
1850             prefetch ? RF_PREFETCHABLE : 0);
1851         if (res == NULL)
1852                 return (barlen);
1853         start = rman_get_start(res);
1854         if ((u_long)start != start) {
1855                 /* Wait a minute!  this platform can't do this address. */
1856                 device_printf(bus,
1857                     "pci%d.%d.%x bar %#x start %#jx, too many bits.",
1858                     b, s, f, reg, (uintmax_t)start);
1859                 resource_list_release(rl, bus, dev, type, reg, res);
1860                 return (barlen);
1861         }
1862         pci_write_config(dev, reg, start, 4);
1863         if (ln2range == 64)
1864                 pci_write_config(dev, reg + 4, start >> 32, 4);
1865         return (barlen);
1866 }
1867
1868 /*
1869  * For ATA devices we need to decide early what addressing mode to use.
1870  * Legacy demands that the primary and secondary ATA ports sits on the
1871  * same addresses that old ISA hardware did. This dictates that we use
1872  * those addresses and ignore the BAR's if we cannot set PCI native
1873  * addressing mode.
1874  */
1875 static void
1876 pci_ata_maps(device_t pcib, device_t bus, device_t dev, int b,
1877     int s, int f, struct resource_list *rl, int force, uint32_t prefetchmask)
1878 {
1879         int rid, type, progif;
1880 #if 0
1881         /* if this device supports PCI native addressing use it */
1882         progif = pci_read_config(dev, PCIR_PROGIF, 1);
1883         if ((progif & 0x8a) == 0x8a) {
1884                 if (pci_mapbase(pci_read_config(dev, PCIR_BAR(0), 4)) &&
1885                     pci_mapbase(pci_read_config(dev, PCIR_BAR(2), 4))) {
1886                         printf("Trying ATA native PCI addressing mode\n");
1887                         pci_write_config(dev, PCIR_PROGIF, progif | 0x05, 1);
1888                 }
1889         }
1890 #endif
1891         progif = pci_read_config(dev, PCIR_PROGIF, 1);
1892         type = SYS_RES_IOPORT;
1893         if (progif & PCIP_STORAGE_IDE_MODEPRIM) {
1894                 pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(0), rl, force,
1895                     prefetchmask & (1 << 0));
1896                 pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(1), rl, force,
1897                     prefetchmask & (1 << 1));
1898         } else {
1899                 rid = PCIR_BAR(0);
1900                 resource_list_add(rl, type, rid, 0x1f0, 0x1f7, 8);
1901                 resource_list_alloc(rl, bus, dev, type, &rid, 0x1f0, 0x1f7, 8,
1902                     0);
1903                 rid = PCIR_BAR(1);
1904                 resource_list_add(rl, type, rid, 0x3f6, 0x3f6, 1);
1905                 resource_list_alloc(rl, bus, dev, type, &rid, 0x3f6, 0x3f6, 1,
1906                     0);
1907         }
1908         if (progif & PCIP_STORAGE_IDE_MODESEC) {
1909                 pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(2), rl, force,
1910                     prefetchmask & (1 << 2));
1911                 pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(3), rl, force,
1912                     prefetchmask & (1 << 3));
1913         } else {
1914                 rid = PCIR_BAR(2);
1915                 resource_list_add(rl, type, rid, 0x170, 0x177, 8);
1916                 resource_list_alloc(rl, bus, dev, type, &rid, 0x170, 0x177, 8,
1917                     0);
1918                 rid = PCIR_BAR(3);
1919                 resource_list_add(rl, type, rid, 0x376, 0x376, 1);
1920                 resource_list_alloc(rl, bus, dev, type, &rid, 0x376, 0x376, 1,
1921                     0);
1922         }
1923         pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(4), rl, force,
1924             prefetchmask & (1 << 4));
1925         pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(5), rl, force,
1926             prefetchmask & (1 << 5));
1927 }
1928
1929 static void
1930 pci_assign_interrupt(device_t bus, device_t dev, int force_route)
1931 {
1932         struct pci_devinfo *dinfo = device_get_ivars(dev);
1933         pcicfgregs *cfg = &dinfo->cfg;
1934         char tunable_name[64];
1935         int irq;
1936
1937         /* Has to have an intpin to have an interrupt. */
1938         if (cfg->intpin == 0)
1939                 return;
1940
1941         /* Let the user override the IRQ with a tunable. */
1942         irq = PCI_INVALID_IRQ;
1943         snprintf(tunable_name, sizeof(tunable_name), "hw.pci%d.%d.INT%c.irq",
1944             cfg->bus, cfg->slot, cfg->intpin + 'A' - 1);
1945         if (TUNABLE_INT_FETCH(tunable_name, &irq) && (irq >= 255 || irq <= 0))
1946                 irq = PCI_INVALID_IRQ;
1947
1948         /*
1949          * If we didn't get an IRQ via the tunable, then we either use the
1950          * IRQ value in the intline register or we ask the bus to route an
1951          * interrupt for us.  If force_route is true, then we only use the
1952          * value in the intline register if the bus was unable to assign an
1953          * IRQ.
1954          */
1955         if (!PCI_INTERRUPT_VALID(irq)) {
1956                 if (!PCI_INTERRUPT_VALID(cfg->intline) || force_route)
1957                         irq = PCI_ASSIGN_INTERRUPT(bus, dev);
1958                 if (!PCI_INTERRUPT_VALID(irq))
1959                         irq = cfg->intline;
1960         }
1961
1962         /* If after all that we don't have an IRQ, just bail. */
1963         if (!PCI_INTERRUPT_VALID(irq))
1964                 return;
1965
1966         /* Update the config register if it changed. */
1967         if (irq != cfg->intline) {
1968                 cfg->intline = irq;
1969                 pci_write_config(dev, PCIR_INTLINE, irq, 1);
1970         }
1971
1972         /* Add this IRQ as rid 0 interrupt resource. */
1973         resource_list_add(&dinfo->resources, SYS_RES_IRQ, 0, irq, irq, 1);
1974 }
1975
1976 void
1977 pci_add_resources(device_t bus, device_t dev, int force, uint32_t prefetchmask)
1978 {
1979         device_t pcib;
1980         struct pci_devinfo *dinfo = device_get_ivars(dev);
1981         pcicfgregs *cfg = &dinfo->cfg;
1982         struct resource_list *rl = &dinfo->resources;
1983         struct pci_quirk *q;
1984         int b, i, f, s;
1985
1986         pcib = device_get_parent(bus);
1987
1988         b = cfg->bus;
1989         s = cfg->slot;
1990         f = cfg->func;
1991
1992         /* ATA devices needs special map treatment */
1993         if ((pci_get_class(dev) == PCIC_STORAGE) &&
1994             (pci_get_subclass(dev) == PCIS_STORAGE_IDE) &&
1995             (pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV))
1996                 pci_ata_maps(pcib, bus, dev, b, s, f, rl, force, prefetchmask);
1997         else
1998                 for (i = 0; i < cfg->nummaps;)
1999                         i += pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(i),
2000                             rl, force, prefetchmask & (1 << i));
2001
2002         /*
2003          * Add additional, quirked resources.
2004          */
2005         for (q = &pci_quirks[0]; q->devid; q++) {
2006                 if (q->devid == ((cfg->device << 16) | cfg->vendor)
2007                     && q->type == PCI_QUIRK_MAP_REG)
2008                         pci_add_map(pcib, bus, dev, b, s, f, q->arg1, rl,
2009                           force, 0);
2010         }
2011
2012         if (cfg->intpin > 0 && PCI_INTERRUPT_VALID(cfg->intline)) {
2013 #ifdef __PCI_REROUTE_INTERRUPT
2014                 /*
2015                  * Try to re-route interrupts. Sometimes the BIOS or
2016                  * firmware may leave bogus values in these registers.
2017                  * If the re-route fails, then just stick with what we
2018                  * have.
2019                  */
2020                 pci_assign_interrupt(bus, dev, 1);
2021 #else
2022                 pci_assign_interrupt(bus, dev, 0);
2023 #endif
2024         }
2025 }
2026
2027 void
2028 pci_add_children(device_t dev, int busno, size_t dinfo_size)
2029 {
2030 #define REG(n, w)       PCIB_READ_CONFIG(pcib, busno, s, f, n, w)
2031         device_t pcib = device_get_parent(dev);
2032         struct pci_devinfo *dinfo;
2033         int maxslots;
2034         int s, f, pcifunchigh;
2035         uint8_t hdrtype;
2036
2037         KASSERT(dinfo_size >= sizeof(struct pci_devinfo),
2038             ("dinfo_size too small"));
2039         maxslots = PCIB_MAXSLOTS(pcib);
2040         for (s = 0; s <= maxslots; s++) {
2041                 pcifunchigh = 0;
2042                 f = 0;
2043                 DELAY(1);
2044                 hdrtype = REG(PCIR_HDRTYPE, 1);
2045                 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
2046                         continue;
2047                 if (hdrtype & PCIM_MFDEV)
2048                         pcifunchigh = PCI_FUNCMAX;
2049                 for (f = 0; f <= pcifunchigh; f++) {
2050                         dinfo = pci_read_device(pcib, busno, s, f, dinfo_size);
2051                         if (dinfo != NULL) {
2052                                 pci_add_child(dev, dinfo);
2053                         }
2054                 }
2055         }
2056 #undef REG
2057 }
2058
2059 void
2060 pci_add_child(device_t bus, struct pci_devinfo *dinfo)
2061 {
2062         dinfo->cfg.dev = device_add_child(bus, NULL, -1);
2063         device_set_ivars(dinfo->cfg.dev, dinfo);
2064         resource_list_init(&dinfo->resources);
2065         pci_cfg_save(dinfo->cfg.dev, dinfo, 0);
2066         pci_cfg_restore(dinfo->cfg.dev, dinfo);
2067         pci_print_verbose(dinfo);
2068         pci_add_resources(bus, dinfo->cfg.dev, 0, 0);
2069 }
2070
2071 static int
2072 pci_probe(device_t dev)
2073 {
2074
2075         device_set_desc(dev, "PCI bus");
2076
2077         /* Allow other subclasses to override this driver. */
2078         return (-1000);
2079 }
2080
2081 static int
2082 pci_attach(device_t dev)
2083 {
2084         int busno;
2085
2086         /*
2087          * Since there can be multiple independantly numbered PCI
2088          * busses on systems with multiple PCI domains, we can't use
2089          * the unit number to decide which bus we are probing. We ask
2090          * the parent pcib what our bus number is.
2091          */
2092         busno = pcib_get_bus(dev);
2093         if (bootverbose)
2094                 device_printf(dev, "physical bus=%d\n", busno);
2095
2096         pci_add_children(dev, busno, sizeof(struct pci_devinfo));
2097
2098         return (bus_generic_attach(dev));
2099 }
2100
2101 int
2102 pci_suspend(device_t dev)
2103 {
2104         int dstate, error, i, numdevs;
2105         device_t acpi_dev, child, *devlist;
2106         struct pci_devinfo *dinfo;
2107
2108         /*
2109          * Save the PCI configuration space for each child and set the
2110          * device in the appropriate power state for this sleep state.
2111          */
2112         acpi_dev = NULL;
2113         if (pci_do_power_resume)
2114                 acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
2115         device_get_children(dev, &devlist, &numdevs);
2116         for (i = 0; i < numdevs; i++) {
2117                 child = devlist[i];
2118                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2119                 pci_cfg_save(child, dinfo, 0);
2120         }
2121
2122         /* Suspend devices before potentially powering them down. */
2123         error = bus_generic_suspend(dev);
2124         if (error) {
2125                 free(devlist, M_TEMP);
2126                 return (error);
2127         }
2128
2129         /*
2130          * Always set the device to D3.  If ACPI suggests a different
2131          * power state, use it instead.  If ACPI is not present, the
2132          * firmware is responsible for managing device power.  Skip
2133          * children who aren't attached since they are powered down
2134          * separately.  Only manage type 0 devices for now.
2135          */
2136         for (i = 0; acpi_dev && i < numdevs; i++) {
2137                 child = devlist[i];
2138                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2139                 if (device_is_attached(child) && dinfo->cfg.hdrtype == 0) {
2140                         dstate = PCI_POWERSTATE_D3;
2141                         ACPI_PWR_FOR_SLEEP(acpi_dev, child, &dstate);
2142                         pci_set_powerstate(child, dstate);
2143                 }
2144         }
2145         free(devlist, M_TEMP);
2146         return (0);
2147 }
2148
2149 int
2150 pci_resume(device_t dev)
2151 {
2152         int i, numdevs;
2153         device_t acpi_dev, child, *devlist;
2154         struct pci_devinfo *dinfo;
2155
2156         /*
2157          * Set each child to D0 and restore its PCI configuration space.
2158          */
2159         acpi_dev = NULL;
2160         if (pci_do_power_resume)
2161                 acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
2162         device_get_children(dev, &devlist, &numdevs);
2163         for (i = 0; i < numdevs; i++) {
2164                 /*
2165                  * Notify ACPI we're going to D0 but ignore the result.  If
2166                  * ACPI is not present, the firmware is responsible for
2167                  * managing device power.  Only manage type 0 devices for now.
2168                  */
2169                 child = devlist[i];
2170                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2171                 if (acpi_dev && device_is_attached(child) &&
2172                     dinfo->cfg.hdrtype == 0) {
2173                         ACPI_PWR_FOR_SLEEP(acpi_dev, child, NULL);
2174                         pci_set_powerstate(child, PCI_POWERSTATE_D0);
2175                 }
2176
2177                 /* Now the device is powered up, restore its config space. */
2178                 pci_cfg_restore(child, dinfo);
2179         }
2180         free(devlist, M_TEMP);
2181         return (bus_generic_resume(dev));
2182 }
2183
2184 static void
2185 pci_load_vendor_data(void)
2186 {
2187         caddr_t vendordata, info;
2188
2189         if ((vendordata = preload_search_by_type("pci_vendor_data")) != NULL) {
2190                 info = preload_search_info(vendordata, MODINFO_ADDR);
2191                 pci_vendordata = *(char **)info;
2192                 info = preload_search_info(vendordata, MODINFO_SIZE);
2193                 pci_vendordata_size = *(size_t *)info;
2194                 /* terminate the database */
2195                 pci_vendordata[pci_vendordata_size] = '\n';
2196         }
2197 }
2198
2199 void
2200 pci_driver_added(device_t dev, driver_t *driver)
2201 {
2202         int numdevs;
2203         device_t *devlist;
2204         device_t child;
2205         struct pci_devinfo *dinfo;
2206         int i;
2207
2208         if (bootverbose)
2209                 device_printf(dev, "driver added\n");
2210         DEVICE_IDENTIFY(driver, dev);
2211         device_get_children(dev, &devlist, &numdevs);
2212         for (i = 0; i < numdevs; i++) {
2213                 child = devlist[i];
2214                 if (device_get_state(child) != DS_NOTPRESENT)
2215                         continue;
2216                 dinfo = device_get_ivars(child);
2217                 pci_print_verbose(dinfo);
2218                 if (bootverbose)
2219                         printf("pci%d:%d:%d: reprobing on driver added\n",
2220                             dinfo->cfg.bus, dinfo->cfg.slot, dinfo->cfg.func);
2221                 pci_cfg_restore(child, dinfo);
2222                 if (device_probe_and_attach(child) != 0)
2223                         pci_cfg_save(child, dinfo, 1);
2224         }
2225         free(devlist, M_TEMP);
2226 }
2227
2228 int
2229 pci_print_child(device_t dev, device_t child)
2230 {
2231         struct pci_devinfo *dinfo;
2232         struct resource_list *rl;
2233         int retval = 0;
2234
2235         dinfo = device_get_ivars(child);
2236         rl = &dinfo->resources;
2237
2238         retval += bus_print_child_header(dev, child);
2239
2240         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
2241         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
2242         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
2243         if (device_get_flags(dev))
2244                 retval += printf(" flags %#x", device_get_flags(dev));
2245
2246         retval += printf(" at device %d.%d", pci_get_slot(child),
2247             pci_get_function(child));
2248
2249         retval += bus_print_child_footer(dev, child);
2250
2251         return (retval);
2252 }
2253
2254 static struct
2255 {
2256         int     class;
2257         int     subclass;
2258         char    *desc;
2259 } pci_nomatch_tab[] = {
2260         {PCIC_OLD,              -1,                     "old"},
2261         {PCIC_OLD,              PCIS_OLD_NONVGA,        "non-VGA display device"},
2262         {PCIC_OLD,              PCIS_OLD_VGA,           "VGA-compatible display device"},
2263         {PCIC_STORAGE,          -1,                     "mass storage"},
2264         {PCIC_STORAGE,          PCIS_STORAGE_SCSI,      "SCSI"},
2265         {PCIC_STORAGE,          PCIS_STORAGE_IDE,       "ATA"},
2266         {PCIC_STORAGE,          PCIS_STORAGE_FLOPPY,    "floppy disk"},
2267         {PCIC_STORAGE,          PCIS_STORAGE_IPI,       "IPI"},
2268         {PCIC_STORAGE,          PCIS_STORAGE_RAID,      "RAID"},
2269         {PCIC_NETWORK,          -1,                     "network"},
2270         {PCIC_NETWORK,          PCIS_NETWORK_ETHERNET,  "ethernet"},
2271         {PCIC_NETWORK,          PCIS_NETWORK_TOKENRING, "token ring"},
2272         {PCIC_NETWORK,          PCIS_NETWORK_FDDI,      "fddi"},
2273         {PCIC_NETWORK,          PCIS_NETWORK_ATM,       "ATM"},
2274         {PCIC_NETWORK,          PCIS_NETWORK_ISDN,      "ISDN"},
2275         {PCIC_DISPLAY,          -1,                     "display"},
2276         {PCIC_DISPLAY,          PCIS_DISPLAY_VGA,       "VGA"},
2277         {PCIC_DISPLAY,          PCIS_DISPLAY_XGA,       "XGA"},
2278         {PCIC_DISPLAY,          PCIS_DISPLAY_3D,        "3D"},
2279         {PCIC_MULTIMEDIA,       -1,                     "multimedia"},
2280         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_VIDEO,  "video"},
2281         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_AUDIO,  "audio"},
2282         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_TELE,   "telephony"},
2283         {PCIC_MEMORY,           -1,                     "memory"},
2284         {PCIC_MEMORY,           PCIS_MEMORY_RAM,        "RAM"},
2285         {PCIC_MEMORY,           PCIS_MEMORY_FLASH,      "flash"},
2286         {PCIC_BRIDGE,           -1,                     "bridge"},
2287         {PCIC_BRIDGE,           PCIS_BRIDGE_HOST,       "HOST-PCI"},
2288         {PCIC_BRIDGE,           PCIS_BRIDGE_ISA,        "PCI-ISA"},
2289         {PCIC_BRIDGE,           PCIS_BRIDGE_EISA,       "PCI-EISA"},
2290         {PCIC_BRIDGE,           PCIS_BRIDGE_MCA,        "PCI-MCA"},
2291         {PCIC_BRIDGE,           PCIS_BRIDGE_PCI,        "PCI-PCI"},
2292         {PCIC_BRIDGE,           PCIS_BRIDGE_PCMCIA,     "PCI-PCMCIA"},
2293         {PCIC_BRIDGE,           PCIS_BRIDGE_NUBUS,      "PCI-NuBus"},
2294         {PCIC_BRIDGE,           PCIS_BRIDGE_CARDBUS,    "PCI-CardBus"},
2295         {PCIC_BRIDGE,           PCIS_BRIDGE_RACEWAY,    "PCI-RACEway"},
2296         {PCIC_SIMPLECOMM,       -1,                     "simple comms"},
2297         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_UART,   "UART"},        /* could detect 16550 */
2298         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_PAR,    "parallel port"},
2299         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_MULSER, "multiport serial"},
2300         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_MODEM,  "generic modem"},
2301         {PCIC_BASEPERIPH,       -1,                     "base peripheral"},
2302         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_PIC,    "interrupt controller"},
2303         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_DMA,    "DMA controller"},
2304         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_TIMER,  "timer"},
2305         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_RTC,    "realtime clock"},
2306         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
2307         {PCIC_INPUTDEV,         -1,                     "input device"},
2308         {PCIC_INPUTDEV,         PCIS_INPUTDEV_KEYBOARD, "keyboard"},
2309         {PCIC_INPUTDEV,         PCIS_INPUTDEV_DIGITIZER,"digitizer"},
2310         {PCIC_INPUTDEV,         PCIS_INPUTDEV_MOUSE,    "mouse"},
2311         {PCIC_INPUTDEV,         PCIS_INPUTDEV_SCANNER,  "scanner"},
2312         {PCIC_INPUTDEV,         PCIS_INPUTDEV_GAMEPORT, "gameport"},
2313         {PCIC_DOCKING,          -1,                     "docking station"},
2314         {PCIC_PROCESSOR,        -1,                     "processor"},
2315         {PCIC_SERIALBUS,        -1,                     "serial bus"},
2316         {PCIC_SERIALBUS,        PCIS_SERIALBUS_FW,      "FireWire"},
2317         {PCIC_SERIALBUS,        PCIS_SERIALBUS_ACCESS,  "AccessBus"},
2318         {PCIC_SERIALBUS,        PCIS_SERIALBUS_SSA,     "SSA"},
2319         {PCIC_SERIALBUS,        PCIS_SERIALBUS_USB,     "USB"},
2320         {PCIC_SERIALBUS,        PCIS_SERIALBUS_FC,      "Fibre Channel"},
2321         {PCIC_SERIALBUS,        PCIS_SERIALBUS_SMBUS,   "SMBus"},
2322         {PCIC_WIRELESS,         -1,                     "wireless controller"},
2323         {PCIC_WIRELESS,         PCIS_WIRELESS_IRDA,     "iRDA"},
2324         {PCIC_WIRELESS,         PCIS_WIRELESS_IR,       "IR"},
2325         {PCIC_WIRELESS,         PCIS_WIRELESS_RF,       "RF"},
2326         {PCIC_INTELLIIO,        -1,                     "intelligent I/O controller"},
2327         {PCIC_INTELLIIO,        PCIS_INTELLIIO_I2O,     "I2O"},
2328         {PCIC_SATCOM,           -1,                     "satellite communication"},
2329         {PCIC_SATCOM,           PCIS_SATCOM_TV,         "sat TV"},
2330         {PCIC_SATCOM,           PCIS_SATCOM_AUDIO,      "sat audio"},
2331         {PCIC_SATCOM,           PCIS_SATCOM_VOICE,      "sat voice"},
2332         {PCIC_SATCOM,           PCIS_SATCOM_DATA,       "sat data"},
2333         {PCIC_CRYPTO,           -1,                     "encrypt/decrypt"},
2334         {PCIC_CRYPTO,           PCIS_CRYPTO_NETCOMP,    "network/computer crypto"},
2335         {PCIC_CRYPTO,           PCIS_CRYPTO_ENTERTAIN,  "entertainment crypto"},
2336         {PCIC_DASP,             -1,                     "dasp"},
2337         {PCIC_DASP,             PCIS_DASP_DPIO,         "DPIO module"},
2338         {0, 0,          NULL}
2339 };
2340
2341 void
2342 pci_probe_nomatch(device_t dev, device_t child)
2343 {
2344         int     i;
2345         char    *cp, *scp, *device;
2346
2347         /*
2348          * Look for a listing for this device in a loaded device database.
2349          */
2350         if ((device = pci_describe_device(child)) != NULL) {
2351                 device_printf(dev, "<%s>", device);
2352                 free(device, M_DEVBUF);
2353         } else {
2354                 /*
2355                  * Scan the class/subclass descriptions for a general
2356                  * description.
2357                  */
2358                 cp = "unknown";
2359                 scp = NULL;
2360                 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
2361                         if (pci_nomatch_tab[i].class == pci_get_class(child)) {
2362                                 if (pci_nomatch_tab[i].subclass == -1) {
2363                                         cp = pci_nomatch_tab[i].desc;
2364                                 } else if (pci_nomatch_tab[i].subclass ==
2365                                     pci_get_subclass(child)) {
2366                                         scp = pci_nomatch_tab[i].desc;
2367                                 }
2368                         }
2369                 }
2370                 device_printf(dev, "<%s%s%s>",
2371                     cp ? cp : "",
2372                     ((cp != NULL) && (scp != NULL)) ? ", " : "",
2373                     scp ? scp : "");
2374         }
2375         printf(" at device %d.%d (no driver attached)\n",
2376             pci_get_slot(child), pci_get_function(child));
2377         if (pci_do_power_nodriver)
2378                 pci_cfg_save(child,
2379                     (struct pci_devinfo *) device_get_ivars(child), 1);
2380         return;
2381 }
2382
2383 /*
2384  * Parse the PCI device database, if loaded, and return a pointer to a
2385  * description of the device.
2386  *
2387  * The database is flat text formatted as follows:
2388  *
2389  * Any line not in a valid format is ignored.
2390  * Lines are terminated with newline '\n' characters.
2391  *
2392  * A VENDOR line consists of the 4 digit (hex) vendor code, a TAB, then
2393  * the vendor name.
2394  *
2395  * A DEVICE line is entered immediately below the corresponding VENDOR ID.
2396  * - devices cannot be listed without a corresponding VENDOR line.
2397  * A DEVICE line consists of a TAB, the 4 digit (hex) device code,
2398  * another TAB, then the device name.
2399  */
2400
2401 /*
2402  * Assuming (ptr) points to the beginning of a line in the database,
2403  * return the vendor or device and description of the next entry.
2404  * The value of (vendor) or (device) inappropriate for the entry type
2405  * is set to -1.  Returns nonzero at the end of the database.
2406  *
2407  * Note that this is slightly unrobust in the face of corrupt data;
2408  * we attempt to safeguard against this by spamming the end of the
2409  * database with a newline when we initialise.
2410  */
2411 static int
2412 pci_describe_parse_line(char **ptr, int *vendor, int *device, char **desc)
2413 {
2414         char    *cp = *ptr;
2415         int     left;
2416
2417         *device = -1;
2418         *vendor = -1;
2419         **desc = '\0';
2420         for (;;) {
2421                 left = pci_vendordata_size - (cp - pci_vendordata);
2422                 if (left <= 0) {
2423                         *ptr = cp;
2424                         return(1);
2425                 }
2426
2427                 /* vendor entry? */
2428                 if (*cp != '\t' &&
2429                     sscanf(cp, "%x\t%80[^\n]", vendor, *desc) == 2)
2430                         break;
2431                 /* device entry? */
2432                 if (*cp == '\t' &&
2433                     sscanf(cp, "%x\t%80[^\n]", device, *desc) == 2)
2434                         break;
2435
2436                 /* skip to next line */
2437                 while (*cp != '\n' && left > 0) {
2438                         cp++;
2439                         left--;
2440                 }
2441                 if (*cp == '\n') {
2442                         cp++;
2443                         left--;
2444                 }
2445         }
2446         /* skip to next line */
2447         while (*cp != '\n' && left > 0) {
2448                 cp++;
2449                 left--;
2450         }
2451         if (*cp == '\n' && left > 0)
2452                 cp++;
2453         *ptr = cp;
2454         return(0);
2455 }
2456
2457 static char *
2458 pci_describe_device(device_t dev)
2459 {
2460         int     vendor, device;
2461         char    *desc, *vp, *dp, *line;
2462
2463         desc = vp = dp = NULL;
2464
2465         /*
2466          * If we have no vendor data, we can't do anything.
2467          */
2468         if (pci_vendordata == NULL)
2469                 goto out;
2470
2471         /*
2472          * Scan the vendor data looking for this device
2473          */
2474         line = pci_vendordata;
2475         if ((vp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
2476                 goto out;
2477         for (;;) {
2478                 if (pci_describe_parse_line(&line, &vendor, &device, &vp))
2479                         goto out;
2480                 if (vendor == pci_get_vendor(dev))
2481                         break;
2482         }
2483         if ((dp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
2484                 goto out;
2485         for (;;) {
2486                 if (pci_describe_parse_line(&line, &vendor, &device, &dp)) {
2487                         *dp = 0;
2488                         break;
2489                 }
2490                 if (vendor != -1) {
2491                         *dp = 0;
2492                         break;
2493                 }
2494                 if (device == pci_get_device(dev))
2495                         break;
2496         }
2497         if (dp[0] == '\0')
2498                 snprintf(dp, 80, "0x%x", pci_get_device(dev));
2499         if ((desc = malloc(strlen(vp) + strlen(dp) + 3, M_DEVBUF, M_NOWAIT)) !=
2500             NULL)
2501                 sprintf(desc, "%s, %s", vp, dp);
2502  out:
2503         if (vp != NULL)
2504                 free(vp, M_DEVBUF);
2505         if (dp != NULL)
2506                 free(dp, M_DEVBUF);
2507         return(desc);
2508 }
2509
2510 int
2511 pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
2512 {
2513         struct pci_devinfo *dinfo;
2514         pcicfgregs *cfg;
2515
2516         dinfo = device_get_ivars(child);
2517         cfg = &dinfo->cfg;
2518
2519         switch (which) {
2520         case PCI_IVAR_ETHADDR:
2521                 /*
2522                  * The generic accessor doesn't deal with failure, so
2523                  * we set the return value, then return an error.
2524                  */
2525                 *((uint8_t **) result) = NULL;
2526                 return (EINVAL);
2527         case PCI_IVAR_SUBVENDOR:
2528                 *result = cfg->subvendor;
2529                 break;
2530         case PCI_IVAR_SUBDEVICE:
2531                 *result = cfg->subdevice;
2532                 break;
2533         case PCI_IVAR_VENDOR:
2534                 *result = cfg->vendor;
2535                 break;
2536         case PCI_IVAR_DEVICE:
2537                 *result = cfg->device;
2538                 break;
2539         case PCI_IVAR_DEVID:
2540                 *result = (cfg->device << 16) | cfg->vendor;
2541                 break;
2542         case PCI_IVAR_CLASS:
2543                 *result = cfg->baseclass;
2544                 break;
2545         case PCI_IVAR_SUBCLASS:
2546                 *result = cfg->subclass;
2547                 break;
2548         case PCI_IVAR_PROGIF:
2549                 *result = cfg->progif;
2550                 break;
2551         case PCI_IVAR_REVID:
2552                 *result = cfg->revid;
2553                 break;
2554         case PCI_IVAR_INTPIN:
2555                 *result = cfg->intpin;
2556                 break;
2557         case PCI_IVAR_IRQ:
2558                 *result = cfg->intline;
2559                 break;
2560         case PCI_IVAR_BUS:
2561                 *result = cfg->bus;
2562                 break;
2563         case PCI_IVAR_SLOT:
2564                 *result = cfg->slot;
2565                 break;
2566         case PCI_IVAR_FUNCTION:
2567                 *result = cfg->func;
2568                 break;
2569         case PCI_IVAR_CMDREG:
2570                 *result = cfg->cmdreg;
2571                 break;
2572         case PCI_IVAR_CACHELNSZ:
2573                 *result = cfg->cachelnsz;
2574                 break;
2575         case PCI_IVAR_MINGNT:
2576                 *result = cfg->mingnt;
2577                 break;
2578         case PCI_IVAR_MAXLAT:
2579                 *result = cfg->maxlat;
2580                 break;
2581         case PCI_IVAR_LATTIMER:
2582                 *result = cfg->lattimer;
2583                 break;
2584         default:
2585                 return (ENOENT);
2586         }
2587         return (0);
2588 }
2589
2590 int
2591 pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
2592 {
2593         struct pci_devinfo *dinfo;
2594
2595         dinfo = device_get_ivars(child);
2596
2597         switch (which) {
2598         case PCI_IVAR_INTPIN:
2599                 dinfo->cfg.intpin = value;
2600                 return (0);
2601         case PCI_IVAR_ETHADDR:
2602         case PCI_IVAR_SUBVENDOR:
2603         case PCI_IVAR_SUBDEVICE:
2604         case PCI_IVAR_VENDOR:
2605         case PCI_IVAR_DEVICE:
2606         case PCI_IVAR_DEVID:
2607         case PCI_IVAR_CLASS:
2608         case PCI_IVAR_SUBCLASS:
2609         case PCI_IVAR_PROGIF:
2610         case PCI_IVAR_REVID:
2611         case PCI_IVAR_IRQ:
2612         case PCI_IVAR_BUS:
2613         case PCI_IVAR_SLOT:
2614         case PCI_IVAR_FUNCTION:
2615                 return (EINVAL);        /* disallow for now */
2616
2617         default:
2618                 return (ENOENT);
2619         }
2620 }
2621
2622
2623 #include "opt_ddb.h"
2624 #ifdef DDB
2625 #include <ddb/ddb.h>
2626 #include <sys/cons.h>
2627
2628 /*
2629  * List resources based on pci map registers, used for within ddb
2630  */
2631
2632 DB_SHOW_COMMAND(pciregs, db_pci_dump)
2633 {
2634         struct pci_devinfo *dinfo;
2635         struct devlist *devlist_head;
2636         struct pci_conf *p;
2637         const char *name;
2638         int i, error, none_count;
2639
2640         none_count = 0;
2641         /* get the head of the device queue */
2642         devlist_head = &pci_devq;
2643
2644         /*
2645          * Go through the list of devices and print out devices
2646          */
2647         for (error = 0, i = 0,
2648              dinfo = STAILQ_FIRST(devlist_head);
2649              (dinfo != NULL) && (error == 0) && (i < pci_numdevs) && !db_pager_quit;
2650              dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
2651
2652                 /* Populate pd_name and pd_unit */
2653                 name = NULL;
2654                 if (dinfo->cfg.dev)
2655                         name = device_get_name(dinfo->cfg.dev);
2656
2657                 p = &dinfo->conf;
2658                 db_printf("%s%d@pci%d:%d:%d:\tclass=0x%06x card=0x%08x "
2659                         "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
2660                         (name && *name) ? name : "none",
2661                         (name && *name) ? (int)device_get_unit(dinfo->cfg.dev) :
2662                         none_count++,
2663                         p->pc_sel.pc_bus, p->pc_sel.pc_dev,
2664                         p->pc_sel.pc_func, (p->pc_class << 16) |
2665                         (p->pc_subclass << 8) | p->pc_progif,
2666                         (p->pc_subdevice << 16) | p->pc_subvendor,
2667                         (p->pc_device << 16) | p->pc_vendor,
2668                         p->pc_revid, p->pc_hdr);
2669         }
2670 }
2671 #endif /* DDB */
2672
2673 static struct resource *
2674 pci_alloc_map(device_t dev, device_t child, int type, int *rid,
2675     u_long start, u_long end, u_long count, u_int flags)
2676 {
2677         struct pci_devinfo *dinfo = device_get_ivars(child);
2678         struct resource_list *rl = &dinfo->resources;
2679         struct resource_list_entry *rle;
2680         struct resource *res;
2681         pci_addr_t map, testval;
2682         int mapsize;
2683
2684         /*
2685          * Weed out the bogons, and figure out how large the BAR/map
2686          * is.  Bars that read back 0 here are bogus and unimplemented.
2687          * Note: atapci in legacy mode are special and handled elsewhere
2688          * in the code.  If you have a atapci device in legacy mode and
2689          * it fails here, that other code is broken.
2690          */
2691         res = NULL;
2692         map = pci_read_config(child, *rid, 4);
2693         pci_write_config(child, *rid, 0xffffffff, 4);
2694         testval = pci_read_config(child, *rid, 4);
2695         if (pci_maprange(testval) == 64)
2696                 map |= (pci_addr_t)pci_read_config(child, *rid + 4, 4) << 32;
2697         if (pci_mapbase(testval) == 0)
2698                 goto out;
2699         if (pci_maptype(testval) & PCI_MAPMEM) {
2700                 if (type != SYS_RES_MEMORY) {
2701                         if (bootverbose)
2702                                 device_printf(dev,
2703                                     "child %s requested type %d for rid %#x,"
2704                                     " but the BAR says it is an memio\n",
2705                                     device_get_nameunit(child), type, *rid);
2706                         goto out;
2707                 }
2708         } else {
2709                 if (type != SYS_RES_IOPORT) {
2710                         if (bootverbose)
2711                                 device_printf(dev,
2712                                     "child %s requested type %d for rid %#x,"
2713                                     " but the BAR says it is an ioport\n",
2714                                     device_get_nameunit(child), type, *rid);
2715                         goto out;
2716                 }
2717         }
2718         /*
2719          * For real BARs, we need to override the size that
2720          * the driver requests, because that's what the BAR
2721          * actually uses and we would otherwise have a
2722          * situation where we might allocate the excess to
2723          * another driver, which won't work.
2724          */
2725         mapsize = pci_mapsize(testval);
2726         count = 1UL << mapsize;
2727         if (RF_ALIGNMENT(flags) < mapsize)
2728                 flags = (flags & ~RF_ALIGNMENT_MASK) | RF_ALIGNMENT_LOG2(mapsize);
2729
2730         /*
2731          * Allocate enough resource, and then write back the
2732          * appropriate bar for that resource.
2733          */
2734         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, type, rid,
2735             start, end, count, flags);
2736         if (res == NULL) {
2737                 device_printf(child,
2738                     "%#lx bytes of rid %#x res %d failed (%#lx, %#lx).\n",
2739                     count, *rid, type, start, end);
2740                 goto out;
2741         }
2742         resource_list_add(rl, type, *rid, start, end, count);
2743         rle = resource_list_find(rl, type, *rid);
2744         if (rle == NULL)
2745                 panic("pci_alloc_map: unexpectedly can't find resource.");
2746         rle->res = res;
2747         rle->start = rman_get_start(res);
2748         rle->end = rman_get_end(res);
2749         rle->count = count;
2750         if (bootverbose)
2751                 device_printf(child,
2752                     "Lazy allocation of %#lx bytes rid %#x type %d at %#lx\n",
2753                     count, *rid, type, rman_get_start(res));
2754         map = rman_get_start(res);
2755 out:;
2756         pci_write_config(child, *rid, map, 4);
2757         if (pci_maprange(testval) == 64)
2758                 pci_write_config(child, *rid + 4, map >> 32, 4);
2759         return (res);
2760 }
2761
2762
2763 struct resource *
2764 pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
2765                    u_long start, u_long end, u_long count, u_int flags)
2766 {
2767         struct pci_devinfo *dinfo = device_get_ivars(child);
2768         struct resource_list *rl = &dinfo->resources;
2769         struct resource_list_entry *rle;
2770         pcicfgregs *cfg = &dinfo->cfg;
2771
2772         /*
2773          * Perform lazy resource allocation
2774          */
2775         if (device_get_parent(child) == dev) {
2776                 switch (type) {
2777                 case SYS_RES_IRQ:
2778                         /*
2779                          * Can't alloc legacy interrupt once MSI messages
2780                          * have been allocated.
2781                          */
2782                         if (*rid == 0 && (cfg->msi.msi_alloc > 0 ||
2783                             cfg->msix.msix_alloc > 0))
2784                                 return (NULL);
2785                         /*
2786                          * If the child device doesn't have an
2787                          * interrupt routed and is deserving of an
2788                          * interrupt, try to assign it one.
2789                          */
2790                         if (*rid == 0 && !PCI_INTERRUPT_VALID(cfg->intline) &&
2791                             (cfg->intpin != 0))
2792                                 pci_assign_interrupt(dev, child, 0);
2793                         break;
2794                 case SYS_RES_IOPORT:
2795                 case SYS_RES_MEMORY:
2796                         if (*rid < PCIR_BAR(cfg->nummaps)) {
2797                                 /*
2798                                  * Enable the I/O mode.  We should
2799                                  * also be assigning resources too
2800                                  * when none are present.  The
2801                                  * resource_list_alloc kind of sorta does
2802                                  * this...
2803                                  */
2804                                 if (PCI_ENABLE_IO(dev, child, type))
2805                                         return (NULL);
2806                         }
2807                         rle = resource_list_find(rl, type, *rid);
2808                         if (rle == NULL)
2809                                 return (pci_alloc_map(dev, child, type, rid,
2810                                     start, end, count, flags));
2811                         break;
2812                 }
2813                 /*
2814                  * If we've already allocated the resource, then
2815                  * return it now.  But first we may need to activate
2816                  * it, since we don't allocate the resource as active
2817                  * above.  Normally this would be done down in the
2818                  * nexus, but since we short-circuit that path we have
2819                  * to do its job here.  Not sure if we should free the
2820                  * resource if it fails to activate.
2821                  */
2822                 rle = resource_list_find(rl, type, *rid);
2823                 if (rle != NULL && rle->res != NULL) {
2824                         if (bootverbose)
2825                                 device_printf(child,
2826                             "Reserved %#lx bytes for rid %#x type %d at %#lx\n",
2827                                     rman_get_size(rle->res), *rid, type,
2828                                     rman_get_start(rle->res));
2829                         if ((flags & RF_ACTIVE) &&
2830                             bus_generic_activate_resource(dev, child, type,
2831                             *rid, rle->res) != 0)
2832                                 return NULL;
2833                         return (rle->res);
2834                 }
2835         }
2836         return (resource_list_alloc(rl, dev, child, type, rid,
2837             start, end, count, flags));
2838 }
2839
2840 void
2841 pci_delete_resource(device_t dev, device_t child, int type, int rid)
2842 {
2843         struct pci_devinfo *dinfo;
2844         struct resource_list *rl;
2845         struct resource_list_entry *rle;
2846
2847         if (device_get_parent(child) != dev)
2848                 return;
2849
2850         dinfo = device_get_ivars(child);
2851         rl = &dinfo->resources;
2852         rle = resource_list_find(rl, type, rid);
2853         if (rle) {
2854                 if (rle->res) {
2855                         if (rman_get_device(rle->res) != dev ||
2856                             rman_get_flags(rle->res) & RF_ACTIVE) {
2857                                 device_printf(dev, "delete_resource: "
2858                                     "Resource still owned by child, oops. "
2859                                     "(type=%d, rid=%d, addr=%lx)\n",
2860                                     rle->type, rle->rid,
2861                                     rman_get_start(rle->res));
2862                                 return;
2863                         }
2864                         bus_release_resource(dev, type, rid, rle->res);
2865                 }
2866                 resource_list_delete(rl, type, rid);
2867         }
2868         /*
2869          * Why do we turn off the PCI configuration BAR when we delete a
2870          * resource? -- imp
2871          */
2872         pci_write_config(child, rid, 0, 4);
2873         BUS_DELETE_RESOURCE(device_get_parent(dev), child, type, rid);
2874 }
2875
2876 struct resource_list *
2877 pci_get_resource_list (device_t dev, device_t child)
2878 {
2879         struct pci_devinfo *dinfo = device_get_ivars(child);
2880
2881         return (&dinfo->resources);
2882 }
2883
2884 uint32_t
2885 pci_read_config_method(device_t dev, device_t child, int reg, int width)
2886 {
2887         struct pci_devinfo *dinfo = device_get_ivars(child);
2888         pcicfgregs *cfg = &dinfo->cfg;
2889
2890         return (PCIB_READ_CONFIG(device_get_parent(dev),
2891             cfg->bus, cfg->slot, cfg->func, reg, width));
2892 }
2893
2894 void
2895 pci_write_config_method(device_t dev, device_t child, int reg,
2896     uint32_t val, int width)
2897 {
2898         struct pci_devinfo *dinfo = device_get_ivars(child);
2899         pcicfgregs *cfg = &dinfo->cfg;
2900
2901         PCIB_WRITE_CONFIG(device_get_parent(dev),
2902             cfg->bus, cfg->slot, cfg->func, reg, val, width);
2903 }
2904
2905 int
2906 pci_child_location_str_method(device_t dev, device_t child, char *buf,
2907     size_t buflen)
2908 {
2909
2910         snprintf(buf, buflen, "slot=%d function=%d", pci_get_slot(child),
2911             pci_get_function(child));
2912         return (0);
2913 }
2914
2915 int
2916 pci_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
2917     size_t buflen)
2918 {
2919         struct pci_devinfo *dinfo;
2920         pcicfgregs *cfg;
2921
2922         dinfo = device_get_ivars(child);
2923         cfg = &dinfo->cfg;
2924         snprintf(buf, buflen, "vendor=0x%04x device=0x%04x subvendor=0x%04x "
2925             "subdevice=0x%04x class=0x%02x%02x%02x", cfg->vendor, cfg->device,
2926             cfg->subvendor, cfg->subdevice, cfg->baseclass, cfg->subclass,
2927             cfg->progif);
2928         return (0);
2929 }
2930
2931 int
2932 pci_assign_interrupt_method(device_t dev, device_t child)
2933 {
2934         struct pci_devinfo *dinfo = device_get_ivars(child);
2935         pcicfgregs *cfg = &dinfo->cfg;
2936
2937         return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
2938             cfg->intpin));
2939 }
2940
2941 static int
2942 pci_modevent(module_t mod, int what, void *arg)
2943 {
2944         static struct cdev *pci_cdev;
2945
2946         switch (what) {
2947         case MOD_LOAD:
2948                 STAILQ_INIT(&pci_devq);
2949                 pci_generation = 0;
2950                 pci_cdev = make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644,
2951                     "pci");
2952                 pci_load_vendor_data();
2953                 break;
2954
2955         case MOD_UNLOAD:
2956                 destroy_dev(pci_cdev);
2957                 break;
2958         }
2959
2960         return (0);
2961 }
2962
2963 void
2964 pci_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
2965 {
2966         int i;
2967
2968         /*
2969          * Only do header type 0 devices.  Type 1 devices are bridges,
2970          * which we know need special treatment.  Type 2 devices are
2971          * cardbus bridges which also require special treatment.
2972          * Other types are unknown, and we err on the side of safety
2973          * by ignoring them.
2974          */
2975         if (dinfo->cfg.hdrtype != 0)
2976                 return;
2977
2978         /*
2979          * Restore the device to full power mode.  We must do this
2980          * before we restore the registers because moving from D3 to
2981          * D0 will cause the chip's BARs and some other registers to
2982          * be reset to some unknown power on reset values.  Cut down
2983          * the noise on boot by doing nothing if we are already in
2984          * state D0.
2985          */
2986         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
2987                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
2988         }
2989         for (i = 0; i < dinfo->cfg.nummaps; i++)
2990                 pci_write_config(dev, PCIR_BAR(i), dinfo->cfg.bar[i], 4);
2991         pci_write_config(dev, PCIR_BIOS, dinfo->cfg.bios, 4);
2992         pci_write_config(dev, PCIR_COMMAND, dinfo->cfg.cmdreg, 2);
2993         pci_write_config(dev, PCIR_INTLINE, dinfo->cfg.intline, 1);
2994         pci_write_config(dev, PCIR_INTPIN, dinfo->cfg.intpin, 1);
2995         pci_write_config(dev, PCIR_MINGNT, dinfo->cfg.mingnt, 1);
2996         pci_write_config(dev, PCIR_MAXLAT, dinfo->cfg.maxlat, 1);
2997         pci_write_config(dev, PCIR_CACHELNSZ, dinfo->cfg.cachelnsz, 1);
2998         pci_write_config(dev, PCIR_LATTIMER, dinfo->cfg.lattimer, 1);
2999         pci_write_config(dev, PCIR_PROGIF, dinfo->cfg.progif, 1);
3000         pci_write_config(dev, PCIR_REVID, dinfo->cfg.revid, 1);
3001
3002         /*
3003          * Restore MSI configuration if it is present.  If MSI is enabled,
3004          * then restore the data and addr registers.
3005          */
3006         if (dinfo->cfg.msi.msi_location != 0)
3007                 pci_resume_msi(dev);
3008 }
3009
3010 void
3011 pci_cfg_save(device_t dev, struct pci_devinfo *dinfo, int setstate)
3012 {
3013         int i;
3014         uint32_t cls;
3015         int ps;
3016
3017         /*
3018          * Only do header type 0 devices.  Type 1 devices are bridges, which
3019          * we know need special treatment.  Type 2 devices are cardbus bridges
3020          * which also require special treatment.  Other types are unknown, and
3021          * we err on the side of safety by ignoring them.  Powering down
3022          * bridges should not be undertaken lightly.
3023          */
3024         if (dinfo->cfg.hdrtype != 0)
3025                 return;
3026         for (i = 0; i < dinfo->cfg.nummaps; i++)
3027                 dinfo->cfg.bar[i] = pci_read_config(dev, PCIR_BAR(i), 4);
3028         dinfo->cfg.bios = pci_read_config(dev, PCIR_BIOS, 4);
3029
3030         /*
3031          * Some drivers apparently write to these registers w/o updating our
3032          * cached copy.  No harm happens if we update the copy, so do so here
3033          * so we can restore them.  The COMMAND register is modified by the
3034          * bus w/o updating the cache.  This should represent the normally
3035          * writable portion of the 'defined' part of type 0 headers.  In
3036          * theory we also need to save/restore the PCI capability structures
3037          * we know about, but apart from power we don't know any that are
3038          * writable.
3039          */
3040         dinfo->cfg.subvendor = pci_read_config(dev, PCIR_SUBVEND_0, 2);
3041         dinfo->cfg.subdevice = pci_read_config(dev, PCIR_SUBDEV_0, 2);
3042         dinfo->cfg.vendor = pci_read_config(dev, PCIR_VENDOR, 2);
3043         dinfo->cfg.device = pci_read_config(dev, PCIR_DEVICE, 2);
3044         dinfo->cfg.cmdreg = pci_read_config(dev, PCIR_COMMAND, 2);
3045         dinfo->cfg.intline = pci_read_config(dev, PCIR_INTLINE, 1);
3046         dinfo->cfg.intpin = pci_read_config(dev, PCIR_INTPIN, 1);
3047         dinfo->cfg.mingnt = pci_read_config(dev, PCIR_MINGNT, 1);
3048         dinfo->cfg.maxlat = pci_read_config(dev, PCIR_MAXLAT, 1);
3049         dinfo->cfg.cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
3050         dinfo->cfg.lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
3051         dinfo->cfg.baseclass = pci_read_config(dev, PCIR_CLASS, 1);
3052         dinfo->cfg.subclass = pci_read_config(dev, PCIR_SUBCLASS, 1);
3053         dinfo->cfg.progif = pci_read_config(dev, PCIR_PROGIF, 1);
3054         dinfo->cfg.revid = pci_read_config(dev, PCIR_REVID, 1);
3055
3056         /*
3057          * don't set the state for display devices, base peripherals and
3058          * memory devices since bad things happen when they are powered down.
3059          * We should (a) have drivers that can easily detach and (b) use
3060          * generic drivers for these devices so that some device actually
3061          * attaches.  We need to make sure that when we implement (a) we don't
3062          * power the device down on a reattach.
3063          */
3064         cls = pci_get_class(dev);
3065         if (!setstate)
3066                 return;
3067         switch (pci_do_power_nodriver)
3068         {
3069                 case 0:         /* NO powerdown at all */
3070                         return;
3071                 case 1:         /* Conservative about what to power down */
3072                         if (cls == PCIC_STORAGE)
3073                                 return;
3074                         /*FALLTHROUGH*/
3075                 case 2:         /* Agressive about what to power down */
3076                         if (cls == PCIC_DISPLAY || cls == PCIC_MEMORY ||
3077                             cls == PCIC_BASEPERIPH)
3078                                 return;
3079                         /*FALLTHROUGH*/
3080                 case 3:         /* Power down everything */
3081                         break;
3082         }
3083         /*
3084          * PCI spec says we can only go into D3 state from D0 state.
3085          * Transition from D[12] into D0 before going to D3 state.
3086          */
3087         ps = pci_get_powerstate(dev);
3088         if (ps != PCI_POWERSTATE_D0 && ps != PCI_POWERSTATE_D3)
3089                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
3090         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D3)
3091                 pci_set_powerstate(dev, PCI_POWERSTATE_D3);
3092 }