606e9a3a3f4ba509d4c656fe95c54deecead78c6
[dragonfly.git] / sys / bus / pci / i386 / pci_bus.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  * __FBSDID("$FreeBSD: src/sys/i386/pci/pci_bus.c,v 1.128.8.1 2009/04/15 03:14:26 kensmith Exp $");
26  */
27
28 #include <sys/cdefs.h>
29
30 #include "opt_pci.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39
40 #include <bus/pci/pcivar.h>
41 #include <bus/pci/pcireg.h>
42 #include <bus/pci/pcib_private.h>
43 #include <bus/isa/isavar.h>
44 #include <machine/md_var.h>
45
46 #include "legacyvar.h"
47 #include "pci_cfgreg.h"
48
49 #include "pcib_if.h"
50
51 static int      pcibios_pcib_route_interrupt(device_t pcib, device_t dev,
52     int pin);
53
54 int
55 legacy_pcib_maxslots(device_t dev)
56 {
57         return 31;
58 }
59
60 /* read configuration space register */
61
62 u_int32_t
63 legacy_pcib_read_config(device_t dev, int bus, int slot, int func,
64                         int reg, int bytes)
65 {
66         return(pci_cfgregread(bus, slot, func, reg, bytes));
67 }
68
69 /* write configuration space register */
70
71 void
72 legacy_pcib_write_config(device_t dev, int bus, int slot, int func,
73                          int reg, u_int32_t data, int bytes)
74 {
75         pci_cfgregwrite(bus, slot, func, reg, data, bytes);
76 }
77
78 /* Pass MSI requests up to the nexus. */
79
80 static int
81 legacy_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
82     int *irqs)
83 {
84         device_t bus;
85
86         bus = device_get_parent(pcib);
87         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
88             irqs));
89 }
90
91 static int
92 legacy_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
93 {
94         device_t bus;
95
96         bus = device_get_parent(pcib);
97         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
98 }
99
100 static int
101 legacy_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
102     uint32_t *data)
103 {
104         device_t bus;
105
106         bus = device_get_parent(pcib);
107         return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
108 }
109
110 static const char *
111 legacy_pcib_is_host_bridge(int bus, int slot, int func,
112                           uint32_t id, uint8_t class, uint8_t subclass,
113                           uint8_t *busnum)
114 {
115         const char *s = NULL;
116         static uint8_t pxb[4];  /* hack for 450nx */
117
118         *busnum = 0;
119
120         switch (id) {
121         case 0x12258086:
122                 s = "Intel 824?? host to PCI bridge";
123                 /* XXX This is a guess */
124                 /* *busnum = legacy_pcib_read_config(0, bus, slot, func, 0x41, 1); */
125                 *busnum = bus;
126                 break;
127         case 0x71208086:
128                 s = "Intel 82810 (i810 GMCH) Host To Hub bridge";
129                 break;
130         case 0x71228086:
131                 s = "Intel 82810-DC100 (i810-DC100 GMCH) Host To Hub bridge";
132                 break;
133         case 0x71248086:
134                 s = "Intel 82810E (i810E GMCH) Host To Hub bridge";
135                 break;
136         case 0x11308086:
137                 s = "Intel 82815 (i815 GMCH) Host To Hub bridge";
138                 break;
139         case 0x71808086:
140                 s = "Intel 82443LX (440 LX) host to PCI bridge";
141                 break;
142         case 0x71908086:
143                 s = "Intel 82443BX (440 BX) host to PCI bridge";
144                 break;
145         case 0x71928086:
146                 s = "Intel 82443BX host to PCI bridge (AGP disabled)";
147                 break;
148         case 0x71948086:
149                 s = "Intel 82443MX host to PCI bridge";
150                 break;
151         case 0x71a08086:
152                 s = "Intel 82443GX host to PCI bridge";
153                 break;
154         case 0x71a18086:
155                 s = "Intel 82443GX host to AGP bridge";
156                 break;
157         case 0x71a28086:
158                 s = "Intel 82443GX host to PCI bridge (AGP disabled)";
159                 break;
160         case 0x84c48086:
161                 s = "Intel 82454KX/GX (Orion) host to PCI bridge";
162                 *busnum = legacy_pcib_read_config(0, bus, slot, func, 0x4a, 1);
163                 break;
164         case 0x84ca8086:
165                 /*
166                  * For the 450nx chipset, there is a whole bundle of
167                  * things pretending to be host bridges. The MIOC will
168                  * be seen first and isn't really a pci bridge (the
169                  * actual busses are attached to the PXB's). We need to
170                  * read the registers of the MIOC to figure out the
171                  * bus numbers for the PXB channels.
172                  *
173                  * Since the MIOC doesn't have a pci bus attached, we
174                  * pretend it wasn't there.
175                  */
176                 pxb[0] = legacy_pcib_read_config(0, bus, slot, func,
177                                                 0xd0, 1); /* BUSNO[0] */
178                 pxb[1] = legacy_pcib_read_config(0, bus, slot, func,
179                                                 0xd1, 1) + 1;   /* SUBA[0]+1 */
180                 pxb[2] = legacy_pcib_read_config(0, bus, slot, func,
181                                                 0xd3, 1); /* BUSNO[1] */
182                 pxb[3] = legacy_pcib_read_config(0, bus, slot, func,
183                                                 0xd4, 1) + 1;   /* SUBA[1]+1 */
184                 return NULL;
185         case 0x84cb8086:
186                 switch (slot) {
187                 case 0x12:
188                         s = "Intel 82454NX PXB#0, Bus#A";
189                         *busnum = pxb[0];
190                         break;
191                 case 0x13:
192                         s = "Intel 82454NX PXB#0, Bus#B";
193                         *busnum = pxb[1];
194                         break;
195                 case 0x14:
196                         s = "Intel 82454NX PXB#1, Bus#A";
197                         *busnum = pxb[2];
198                         break;
199                 case 0x15:
200                         s = "Intel 82454NX PXB#1, Bus#B";
201                         *busnum = pxb[3];
202                         break;
203                 }
204                 break;
205
206                 /* AMD -- vendor 0x1022 */
207         case 0x30001022:
208                 s = "AMD Elan SC520 host to PCI bridge";
209 #ifdef CPU_ELAN
210                 init_AMD_Elan_sc520();
211 #else
212                 kprintf(
213 "*** WARNING: missing CPU_ELAN -- timekeeping may be wrong\n");
214 #endif
215                 break;
216         case 0x70061022:
217                 s = "AMD-751 host to PCI bridge";
218                 break;
219         case 0x700e1022:
220                 s = "AMD-761 host to PCI bridge";
221                 break;
222
223                 /* SiS -- vendor 0x1039 */
224         case 0x04961039:
225                 s = "SiS 85c496";
226                 break;
227         case 0x04061039:
228                 s = "SiS 85c501";
229                 break;
230         case 0x06011039:
231                 s = "SiS 85c601";
232                 break;
233         case 0x55911039:
234                 s = "SiS 5591 host to PCI bridge";
235                 break;
236         case 0x00011039:
237                 s = "SiS 5591 host to AGP bridge";
238                 break;
239
240                 /* VLSI -- vendor 0x1004 */
241         case 0x00051004:
242                 s = "VLSI 82C592 Host to PCI bridge";
243                 break;
244
245                 /* XXX Here is MVP3, I got the datasheet but NO M/B to test it  */
246                 /* totally. Please let me know if anything wrong.            -F */
247                 /* XXX need info on the MVP3 -- any takers? */
248         case 0x05981106:
249                 s = "VIA 82C598MVP (Apollo MVP3) host bridge";
250                 break;
251
252                 /* AcerLabs -- vendor 0x10b9 */
253                 /* Funny : The datasheet told me vendor id is "10b8",sub-vendor */
254                 /* id is '10b9" but the register always shows "10b9". -Foxfair  */
255         case 0x154110b9:
256                 s = "AcerLabs M1541 (Aladdin-V) PCI host bridge";
257                 break;
258
259                 /* OPTi -- vendor 0x1045 */
260         case 0xc7011045:
261                 s = "OPTi 82C700 host to PCI bridge";
262                 break;
263         case 0xc8221045:
264                 s = "OPTi 82C822 host to PCI Bridge";
265                 break;
266
267                 /* ServerWorks -- vendor 0x1166 */
268         case 0x00051166:
269                 s = "ServerWorks NB6536 2.0HE host to PCI bridge";
270                 *busnum = legacy_pcib_read_config(0, bus, slot, func, 0x44, 1);
271                 break;
272
273         case 0x00061166:
274                 /* FALLTHROUGH */
275         case 0x00081166:
276                 /* FALLTHROUGH */
277         case 0x02011166:
278                 /* FALLTHROUGH */
279         case 0x010f1014: /* IBM re-badged ServerWorks chipset */
280                 s = "ServerWorks host to PCI bridge";
281                 *busnum = legacy_pcib_read_config(0, bus, slot, func, 0x44, 1);
282                 break;
283
284         case 0x00091166:
285                 s = "ServerWorks NB6635 3.0LE host to PCI bridge";
286                 *busnum = legacy_pcib_read_config(0, bus, slot, func, 0x44, 1);
287                 break;
288
289         case 0x00101166:
290                 s = "ServerWorks CIOB30 host to PCI bridge";
291                 *busnum = legacy_pcib_read_config(0, bus, slot, func, 0x44, 1);
292                 break;
293
294         case 0x00111166:
295                 /* FALLTHROUGH */
296         case 0x03021014: /* IBM re-badged ServerWorks chipset */
297                 s = "ServerWorks CMIC-HE host to PCI-X bridge";
298                 *busnum = legacy_pcib_read_config(0, bus, slot, func, 0x44, 1);
299                 break;
300
301                 /* XXX unknown chipset, but working */
302         case 0x00171166:
303                 /* FALLTHROUGH */
304         case 0x01011166:
305                 s = "ServerWorks host to PCI bridge(unknown chipset)";
306                 *busnum = legacy_pcib_read_config(0, bus, slot, func, 0x44, 1);
307                 break;
308
309                 /* Compaq/HP -- vendor 0x0e11 */
310         case 0x60100e11:
311                 s = "Compaq/HP Model 6010 HotPlug PCI Bridge";
312                 *busnum = legacy_pcib_read_config(0, bus, slot, func, 0xc8, 1);
313                 break;
314
315                 /* Integrated Micro Solutions -- vendor 0x10e0 */
316         case 0x884910e0:
317                 s = "Integrated Micro Solutions VL Bridge";
318                 break;
319
320         default:
321                 if (class == PCIC_BRIDGE && subclass == PCIS_BRIDGE_HOST)
322                         s = "Host to PCI bridge";
323                 break;
324         }
325
326         return s;
327 }
328
329 /*
330  * Scan the first pci bus for host-pci bridges and add pcib instances
331  * to the nexus for each bridge.
332  */
333 static int
334 legacy_pcib_identify(driver_t *driver, device_t parent)
335 {
336         int bus, slot, func;
337         u_int8_t  hdrtype;
338         int found = 0;
339         int pcifunchigh;
340         int found824xx = 0;
341         int found_orion = 0;
342         device_t child;
343         devclass_t pci_devclass;
344
345         if (pci_cfgregopen() == 0)
346                 return ENXIO;
347         /*
348          * Check to see if we haven't already had a PCI bus added
349          * via some other means.  If we have, bail since otherwise
350          * we're going to end up duplicating it.
351          */
352         if ((pci_devclass = devclass_find("pci")) &&
353             devclass_get_device(pci_devclass, 0))
354                 return ENXIO;
355
356         bus = 0;
357 retry:
358         for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
359                 func = 0;
360                 hdrtype = legacy_pcib_read_config(0, bus, slot, func,
361                                                  PCIR_HDRTYPE, 1);
362                 /*
363                  * When enumerating bus devices, the standard says that
364                  * one should check the header type and ignore the slots whose
365                  * header types that the software doesn't know about.  We use
366                  * this to filter out devices.
367                  */
368                 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
369                         continue;
370                 if ((hdrtype & PCIM_MFDEV) &&
371                     (!found_orion || hdrtype != 0xff))
372                         pcifunchigh = PCI_FUNCMAX;
373                 else
374                         pcifunchigh = 0;
375                 for (func = 0; func <= pcifunchigh; func++) {
376                         /*
377                          * Read the IDs and class from the device.
378                          */
379                         u_int32_t id;
380                         u_int8_t class, subclass, busnum;
381                         const char *s;
382                         device_t *devs;
383                         int ndevs, i;
384
385                         id = legacy_pcib_read_config(0, bus, slot, func,
386                                                     PCIR_DEVVENDOR, 4);
387                         if (id == -1)
388                                 continue;
389                         class = legacy_pcib_read_config(0, bus, slot, func,
390                                                        PCIR_CLASS, 1);
391                         subclass = legacy_pcib_read_config(0, bus, slot, func,
392                                                           PCIR_SUBCLASS, 1);
393
394                         s = legacy_pcib_is_host_bridge(bus, slot, func,
395                                                       id, class, subclass,
396                                                       &busnum);
397                         if (s == NULL)
398                                 continue;
399
400                         /*
401                          * Check to see if the physical bus has already
402                          * been seen.  Eg: hybrid 32 and 64 bit host
403                          * bridges to the same logical bus.
404                          */
405                         if (device_get_children(parent, &devs, &ndevs) == 0) {
406                                 for (i = 0; s != NULL && i < ndevs; i++) {
407                                         if (strcmp(device_get_name(devs[i]),
408                                             "pcib") != 0)
409                                                 continue;
410                                         if (legacy_get_pcibus(devs[i]) == busnum)
411                                                 s = NULL;
412                                 }
413                                 kfree(devs, M_TEMP);
414                         }
415
416                         if (s == NULL)
417                                 continue;
418
419                         /*
420                          * Add at priority 100 to make sure we
421                          * go after any motherboard resources
422                          */
423                         child = BUS_ADD_CHILD(parent, parent, 100 + busnum,
424                                               "pcib", busnum);
425                         device_set_desc(child, s);
426                         legacy_set_pcibus(child, busnum);
427
428                         found = 1;
429                         if (id == 0x12258086)
430                                 found824xx = 1;
431                         if (id == 0x84c48086)
432                                 found_orion = 1;
433                 }
434         }
435         if (found824xx && bus == 0) {
436                 bus++;
437                 goto retry;
438         }
439
440         /*
441          * Make sure we add at least one bridge since some old
442          * hardware doesn't actually have a host-pci bridge device.
443          * Note that pci_cfgregopen() thinks we have PCI devices..
444          */
445         if (!found) {
446                 if (bootverbose)
447                         kprintf(
448         "legacy_pcib_identify: no bridge found, adding pcib0 anyway\n");
449                 child = BUS_ADD_CHILD(parent, parent, 100, "pcib", 0);
450                 legacy_set_pcibus(child, 0);
451         }
452         return 0;
453 }
454
455 static int
456 legacy_pcib_probe(device_t dev)
457 {
458         if (pci_cfgregopen() == 0)
459                 return ENXIO;
460         return -100;
461 }
462
463 static int
464 legacy_pcib_attach(device_t dev)
465 {
466         device_t pir;
467         int bus;
468
469         /*
470          * Look for a PCI BIOS interrupt routing table as that will be
471          * our method of routing interrupts if we have one.
472          */
473         bus = pcib_get_bus(dev);
474         if (pci_pir_probe(bus, 0)) {
475                 pir = BUS_ADD_CHILD(device_get_parent(dev), device_get_parent(dev), 0, "pir", 0);
476                 if (pir != NULL)
477                         device_probe_and_attach(pir);
478         }
479         device_add_child(dev, "pci", bus);
480         return bus_generic_attach(dev);
481 }
482
483 int
484 legacy_pcib_read_ivar(device_t dev, device_t child, int which,
485     uintptr_t *result)
486 {
487
488         switch (which) {
489         case  PCIB_IVAR_DOMAIN:
490                 *result = 0;
491                 return 0;
492         case  PCIB_IVAR_BUS:
493                 *result = legacy_get_pcibus(dev);
494                 return 0;
495         }
496         return ENOENT;
497 }
498
499 int
500 legacy_pcib_write_ivar(device_t dev, device_t child, int which,
501     uintptr_t value)
502 {
503
504         switch (which) {
505         case  PCIB_IVAR_DOMAIN:
506                 return EINVAL;
507         case  PCIB_IVAR_BUS:
508                 legacy_set_pcibus(dev, value);
509                 return 0;
510         }
511         return ENOENT;
512 }
513
514 SYSCTL_DECL(_hw_pci);
515
516 static unsigned long legacy_host_mem_start = 0x80000000;
517 /* XXX need TUNABLE_ULONG */
518 TUNABLE_INT("hw.pci.host_mem_start", (int *)&legacy_host_mem_start);
519 SYSCTL_ULONG(_hw_pci, OID_AUTO, host_mem_start, CTLFLAG_RD,
520     &legacy_host_mem_start, 0x80000000,
521     "Limit the host bridge memory to being above this address.  Must be\n\
522 set at boot via a tunable.");
523
524 struct resource *
525 legacy_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
526     u_long start, u_long end, u_long count, u_int flags)
527 {
528     /*
529      * If no memory preference is given, use upper 32MB slot most
530      * bioses use for their memory window.  Typically other bridges
531      * before us get in the way to assert their preferences on memory.
532      * Hardcoding like this sucks, so a more MD/MI way needs to be
533      * found to do it.  This is typically only used on older laptops
534      * that don't have pci busses behind pci bridge, so assuming > 32MB
535      * is liekly OK.
536      *
537      * However, this can cause problems for other chipsets, so we make
538      * this tunable by hw.pci.host_mem_start.
539      */
540     if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
541         start = legacy_host_mem_start;
542     if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL)
543         start = 0x1000;
544     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
545         count, flags));
546 }
547
548 static device_method_t legacy_pcib_methods[] = {
549         /* Device interface */
550         DEVMETHOD(device_identify,      legacy_pcib_identify),
551         DEVMETHOD(device_probe,         legacy_pcib_probe),
552         DEVMETHOD(device_attach,        legacy_pcib_attach),
553         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
554         DEVMETHOD(device_suspend,       bus_generic_suspend),
555         DEVMETHOD(device_resume,        bus_generic_resume),
556
557         /* Bus interface */
558         DEVMETHOD(bus_print_child,      bus_generic_print_child),
559         DEVMETHOD(bus_read_ivar,        legacy_pcib_read_ivar),
560         DEVMETHOD(bus_write_ivar,       legacy_pcib_write_ivar),
561         DEVMETHOD(bus_alloc_resource,   legacy_pcib_alloc_resource),
562         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
563         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
564         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
565         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
566         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
567
568         /* pcib interface */
569         DEVMETHOD(pcib_maxslots,        legacy_pcib_maxslots),
570         DEVMETHOD(pcib_read_config,     legacy_pcib_read_config),
571         DEVMETHOD(pcib_write_config,    legacy_pcib_write_config),
572         DEVMETHOD(pcib_route_interrupt, pcibios_pcib_route_interrupt),
573         DEVMETHOD(pcib_alloc_msi,       legacy_pcib_alloc_msi),
574         DEVMETHOD(pcib_release_msi,     pcib_release_msi),
575         DEVMETHOD(pcib_alloc_msix,      legacy_pcib_alloc_msix),
576         DEVMETHOD(pcib_release_msix,    pcib_release_msix),
577         DEVMETHOD(pcib_map_msi,         legacy_pcib_map_msi),
578         { 0, 0 }
579 };
580
581 static devclass_t hostb_devclass;
582
583 DEFINE_CLASS_0(pcib, legacy_pcib_driver, legacy_pcib_methods, 1);
584 DRIVER_MODULE(pcib, legacy, legacy_pcib_driver, hostb_devclass, 0, 0);
585
586
587 /*
588  * Install placeholder to claim the resources owned by the
589  * PCI bus interface.  This could be used to extract the
590  * config space registers in the extreme case where the PnP
591  * ID is available and the PCI BIOS isn't, but for now we just
592  * eat the PnP ID and do nothing else.
593  *
594  * XXX we should silence this probe, as it will generally confuse
595  * people.
596  */
597 static struct isa_pnp_id pcibus_pnp_ids[] = {
598         { 0x030ad041 /* PNP0A03 */, "PCI Bus" },
599         { 0 }
600 };
601
602 static int
603 pcibus_pnp_probe(device_t dev)
604 {
605         int result;
606
607         if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, pcibus_pnp_ids)) <= 0)
608                 device_quiet(dev);
609         return(result);
610 }
611
612 static int
613 pcibus_pnp_attach(device_t dev)
614 {
615         return(0);
616 }
617
618 static device_method_t pcibus_pnp_methods[] = {
619         /* Device interface */
620         DEVMETHOD(device_probe,         pcibus_pnp_probe),
621         DEVMETHOD(device_attach,        pcibus_pnp_attach),
622         DEVMETHOD(device_detach,        bus_generic_detach),
623         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
624         DEVMETHOD(device_suspend,       bus_generic_suspend),
625         DEVMETHOD(device_resume,        bus_generic_resume),
626         { 0, 0 }
627 };
628
629 static devclass_t pcibus_pnp_devclass;
630
631 DEFINE_CLASS_0(pcibus_pnp, pcibus_pnp_driver, pcibus_pnp_methods, 1);
632 DRIVER_MODULE(pcibus_pnp, isa, pcibus_pnp_driver, pcibus_pnp_devclass, 0, 0);
633
634
635 /*
636  * Provide a PCI-PCI bridge driver for PCI busses behind PCI-PCI bridges
637  * that appear in the PCIBIOS Interrupt Routing Table to use the routing
638  * table for interrupt routing when possible.
639  */
640 static int      pcibios_pcib_probe(device_t bus);
641
642 static device_method_t pcibios_pcib_pci_methods[] = {
643         /* Device interface */
644         DEVMETHOD(device_probe,         pcibios_pcib_probe),
645         DEVMETHOD(device_attach,        pcib_attach),
646         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
647         DEVMETHOD(device_suspend,       bus_generic_suspend),
648         DEVMETHOD(device_resume,        bus_generic_resume),
649
650         /* Bus interface */
651         DEVMETHOD(bus_print_child,      bus_generic_print_child),
652         DEVMETHOD(bus_read_ivar,        pcib_read_ivar),
653         DEVMETHOD(bus_write_ivar,       pcib_write_ivar),
654         DEVMETHOD(bus_alloc_resource,   pcib_alloc_resource),
655         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
656         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
657         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
658         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
659         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
660
661         /* pcib interface */
662         DEVMETHOD(pcib_maxslots,        pcib_maxslots),
663         DEVMETHOD(pcib_read_config,     pcib_read_config),
664         DEVMETHOD(pcib_write_config,    pcib_write_config),
665         DEVMETHOD(pcib_route_interrupt, pcibios_pcib_route_interrupt),
666         DEVMETHOD(pcib_alloc_msi,       pcib_alloc_msi),
667         DEVMETHOD(pcib_release_msi,     pcib_release_msi),
668         DEVMETHOD(pcib_alloc_msix,      pcib_alloc_msix),
669         DEVMETHOD(pcib_release_msix,    pcib_release_msix),
670         DEVMETHOD(pcib_map_msi,         pcib_map_msi),
671
672         {0, 0}
673 };
674
675 static devclass_t pcib_devclass;
676
677 DEFINE_CLASS_0(pcib, pcibios_pcib_driver, pcibios_pcib_pci_methods,
678     sizeof(struct pcib_softc));
679 DRIVER_MODULE(pcibios_pcib, pci, pcibios_pcib_driver, pcib_devclass, 0, 0);
680
681 static int
682 pcibios_pcib_probe(device_t dev)
683 {
684         int bus;
685
686         if ((pci_get_class(dev) != PCIC_BRIDGE) ||
687             (pci_get_subclass(dev) != PCIS_BRIDGE_PCI))
688                 return (ENXIO);
689         bus = pci_read_config(dev, PCIR_SECBUS_1, 1);
690         if (bus == 0)
691                 return (ENXIO);
692         if (!pci_pir_probe(bus, 1))
693                 return (ENXIO);
694         device_set_desc(dev, "PCIBIOS PCI-PCI bridge");
695         return (-2000);
696 }
697
698 static int
699 pcibios_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
700 {
701         return (pci_pir_route_interrupt(pci_get_bus(dev), pci_get_slot(dev),
702                 pci_get_function(dev), pin));
703 }