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