]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/pci/pci_pci.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / pci / pci_pci.c
1 /*-
2  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
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, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /*
35  * PCI:PCI bridge support.
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/bus.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46
47 #include <machine/resource.h>
48
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcib_private.h>
52
53 #include "pcib_if.h"
54
55 static int              pcib_probe(device_t dev);
56
57 static device_method_t pcib_methods[] = {
58     /* Device interface */
59     DEVMETHOD(device_probe,             pcib_probe),
60     DEVMETHOD(device_attach,            pcib_attach),
61     DEVMETHOD(device_detach,            bus_generic_detach),
62     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
63     DEVMETHOD(device_suspend,           bus_generic_suspend),
64     DEVMETHOD(device_resume,            bus_generic_resume),
65
66     /* Bus interface */
67     DEVMETHOD(bus_print_child,          bus_generic_print_child),
68     DEVMETHOD(bus_read_ivar,            pcib_read_ivar),
69     DEVMETHOD(bus_write_ivar,           pcib_write_ivar),
70     DEVMETHOD(bus_alloc_resource,       pcib_alloc_resource),
71     DEVMETHOD(bus_release_resource,     bus_generic_release_resource),
72     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
73     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
74     DEVMETHOD(bus_setup_intr,           bus_generic_setup_intr),
75     DEVMETHOD(bus_teardown_intr,        bus_generic_teardown_intr),
76
77     /* pcib interface */
78     DEVMETHOD(pcib_maxslots,            pcib_maxslots),
79     DEVMETHOD(pcib_read_config,         pcib_read_config),
80     DEVMETHOD(pcib_write_config,        pcib_write_config),
81     DEVMETHOD(pcib_route_interrupt,     pcib_route_interrupt),
82     DEVMETHOD(pcib_alloc_msi,           pcib_alloc_msi),
83     DEVMETHOD(pcib_release_msi,         pcib_release_msi),
84     DEVMETHOD(pcib_alloc_msix,          pcib_alloc_msix),
85     DEVMETHOD(pcib_release_msix,        pcib_release_msix),
86     DEVMETHOD(pcib_map_msi,             pcib_map_msi),
87
88     { 0, 0 }
89 };
90
91 static devclass_t pcib_devclass;
92
93 DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
94 DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
95
96 /*
97  * Is the prefetch window open (eg, can we allocate memory in it?)
98  */
99 static int
100 pcib_is_prefetch_open(struct pcib_softc *sc)
101 {
102         return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
103 }
104
105 /*
106  * Is the nonprefetch window open (eg, can we allocate memory in it?)
107  */
108 static int
109 pcib_is_nonprefetch_open(struct pcib_softc *sc)
110 {
111         return (sc->membase > 0 && sc->membase < sc->memlimit);
112 }
113
114 /*
115  * Is the io window open (eg, can we allocate ports in it?)
116  */
117 static int
118 pcib_is_io_open(struct pcib_softc *sc)
119 {
120         return (sc->iobase > 0 && sc->iobase < sc->iolimit);
121 }
122
123 /*
124  * Generic device interface
125  */
126 static int
127 pcib_probe(device_t dev)
128 {
129     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
130         (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
131         device_set_desc(dev, "PCI-PCI bridge");
132         return(-10000);
133     }
134     return(ENXIO);
135 }
136
137 void
138 pcib_attach_common(device_t dev)
139 {
140     struct pcib_softc   *sc;
141     uint8_t             iolow;
142
143     sc = device_get_softc(dev);
144     sc->dev = dev;
145
146     /*
147      * Get current bridge configuration.
148      */
149     sc->command   = pci_read_config(dev, PCIR_COMMAND, 1);
150     sc->domain    = pci_get_domain(dev);
151     sc->secbus    = pci_read_config(dev, PCIR_SECBUS_1, 1);
152     sc->subbus    = pci_read_config(dev, PCIR_SUBBUS_1, 1);
153     sc->secstat   = pci_read_config(dev, PCIR_SECSTAT_1, 2);
154     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
155     sc->seclat    = pci_read_config(dev, PCIR_SECLAT_1, 1);
156
157     /*
158      * Determine current I/O decode.
159      */
160     if (sc->command & PCIM_CMD_PORTEN) {
161         iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
162         if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
163             sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2),
164                                        pci_read_config(dev, PCIR_IOBASEL_1, 1));
165         } else {
166             sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1));
167         }
168
169         iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
170         if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
171             sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2),
172                                          pci_read_config(dev, PCIR_IOLIMITL_1, 1));
173         } else {
174             sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1));
175         }
176     }
177
178     /*
179      * Determine current memory decode.
180      */
181     if (sc->command & PCIM_CMD_MEMEN) {
182         sc->membase   = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2));
183         sc->memlimit  = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
184         iolow = pci_read_config(dev, PCIR_PMBASEL_1, 1);
185         if ((iolow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
186             sc->pmembase = PCI_PPBMEMBASE(
187                 pci_read_config(dev, PCIR_PMBASEH_1, 4),
188                 pci_read_config(dev, PCIR_PMBASEL_1, 2));
189         else
190             sc->pmembase = PCI_PPBMEMBASE(0,
191                 pci_read_config(dev, PCIR_PMBASEL_1, 2));
192         iolow = pci_read_config(dev, PCIR_PMLIMITL_1, 1);
193         if ((iolow & PCIM_BRPM_MASK) == PCIM_BRPM_64)   
194             sc->pmemlimit = PCI_PPBMEMLIMIT(
195                 pci_read_config(dev, PCIR_PMLIMITH_1, 4),
196                 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
197         else
198             sc->pmemlimit = PCI_PPBMEMLIMIT(0,
199                 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
200     }
201
202     /*
203      * Quirk handling.
204      */
205     switch (pci_get_devid(dev)) {
206     case 0x12258086:            /* Intel 82454KX/GX (Orion) */
207         {
208             uint8_t     supbus;
209
210             supbus = pci_read_config(dev, 0x41, 1);
211             if (supbus != 0xff) {
212                 sc->secbus = supbus + 1;
213                 sc->subbus = supbus + 1;
214             }
215             break;
216         }
217
218     /*
219      * The i82380FB mobile docking controller is a PCI-PCI bridge,
220      * and it is a subtractive bridge.  However, the ProgIf is wrong
221      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
222      * happen.  There's also a Toshiba bridge that behaves this
223      * way.
224      */
225     case 0x124b8086:            /* Intel 82380FB Mobile */
226     case 0x060513d7:            /* Toshiba ???? */
227         sc->flags |= PCIB_SUBTRACTIVE;
228         break;
229
230     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
231     case 0x00dd10de:
232         {
233             char *cp;
234
235             if ((cp = getenv("smbios.planar.maker")) == NULL)
236                 break;
237             if (strncmp(cp, "Compal", 6) != 0) {
238                 freeenv(cp);
239                 break;
240             }
241             freeenv(cp);
242             if ((cp = getenv("smbios.planar.product")) == NULL)
243                 break;
244             if (strncmp(cp, "08A0", 4) != 0) {
245                 freeenv(cp);
246                 break;
247             }
248             freeenv(cp);
249             if (sc->subbus < 0xa) {
250                 pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
251                 sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
252             }
253             break;
254         }
255     }
256
257     if (pci_msi_device_blacklisted(dev))
258         sc->flags |= PCIB_DISABLE_MSI;
259
260     /*
261      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
262      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
263      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
264      * This means they act as if they were subtractively decoding
265      * bridges and pass all transactions.  Mark them and real ProgIf 1
266      * parts as subtractive.
267      */
268     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
269       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
270         sc->flags |= PCIB_SUBTRACTIVE;
271         
272     if (bootverbose) {
273         device_printf(dev, "  domain            %d\n", sc->domain);
274         device_printf(dev, "  secondary bus     %d\n", sc->secbus);
275         device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
276         device_printf(dev, "  I/O decode        0x%x-0x%x\n", sc->iobase, sc->iolimit);
277         if (pcib_is_nonprefetch_open(sc))
278             device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
279               (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
280         if (pcib_is_prefetch_open(sc))
281             device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
282               (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
283         else
284             device_printf(dev, "  no prefetched decode\n");
285         if (sc->flags & PCIB_SUBTRACTIVE)
286             device_printf(dev, "  Subtractively decoded bridge.\n");
287     }
288
289     /*
290      * XXX If the secondary bus number is zero, we should assign a bus number
291      *     since the BIOS hasn't, then initialise the bridge.
292      */
293
294     /*
295      * XXX If the subordinate bus number is less than the secondary bus number,
296      *     we should pick a better value.  One sensible alternative would be to
297      *     pick 255; the only tradeoff here is that configuration transactions
298      *     would be more widely routed than absolutely necessary.
299      */
300 }
301
302 int
303 pcib_attach(device_t dev)
304 {
305     struct pcib_softc   *sc;
306     device_t            child;
307
308     pcib_attach_common(dev);
309     sc = device_get_softc(dev);
310     if (sc->secbus != 0) {
311         child = device_add_child(dev, "pci", sc->secbus);
312         if (child != NULL)
313             return(bus_generic_attach(dev));
314     } 
315
316     /* no secondary bus; we should have fixed this */
317     return(0);
318 }
319
320 int
321 pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
322 {
323     struct pcib_softc   *sc = device_get_softc(dev);
324     
325     switch (which) {
326     case PCIB_IVAR_DOMAIN:
327         *result = sc->domain;
328         return(0);
329     case PCIB_IVAR_BUS:
330         *result = sc->secbus;
331         return(0);
332     }
333     return(ENOENT);
334 }
335
336 int
337 pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
338 {
339     struct pcib_softc   *sc = device_get_softc(dev);
340
341     switch (which) {
342     case PCIB_IVAR_DOMAIN:
343         return(EINVAL);
344     case PCIB_IVAR_BUS:
345         sc->secbus = value;
346         return(0);
347     }
348     return(ENOENT);
349 }
350
351 /*
352  * We have to trap resource allocation requests and ensure that the bridge
353  * is set up to, or capable of handling them.
354  */
355 struct resource *
356 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 
357     u_long start, u_long end, u_long count, u_int flags)
358 {
359         struct pcib_softc       *sc = device_get_softc(dev);
360         const char *name, *suffix;
361         int ok;
362
363         /*
364          * Fail the allocation for this range if it's not supported.
365          */
366         name = device_get_nameunit(child);
367         if (name == NULL) {
368                 name = "";
369                 suffix = "";
370         } else
371                 suffix = " ";
372         switch (type) {
373         case SYS_RES_IOPORT:
374                 ok = 0;
375                 if (!pcib_is_io_open(sc))
376                         break;
377                 ok = (start >= sc->iobase && end <= sc->iolimit);
378
379                 /*
380                  * Make sure we allow access to VGA I/O addresses when the
381                  * bridge has the "VGA Enable" bit set.
382                  */
383                 if (!ok && pci_is_vga_ioport_range(start, end))
384                         ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
385
386                 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
387                         if (!ok) {
388                                 if (start < sc->iobase)
389                                         start = sc->iobase;
390                                 if (end > sc->iolimit)
391                                         end = sc->iolimit;
392                                 if (start < end)
393                                         ok = 1;
394                         }
395                 } else {
396                         ok = 1;
397 #if 1
398                         if (start < sc->iobase && end > sc->iolimit) {
399                                 start = sc->iobase;
400                                 end = sc->iolimit;
401                         }
402 #endif                  
403                 }
404                 if (end < start) {
405                         device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
406                             end, start);
407                         start = 0;
408                         end = 0;
409                         ok = 0;
410                 }
411                 if (!ok) {
412                         device_printf(dev, "%s%srequested unsupported I/O "
413                             "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
414                             name, suffix, start, end, sc->iobase, sc->iolimit);
415                         return (NULL);
416                 }
417                 if (bootverbose)
418                         device_printf(dev,
419                             "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
420                             name, suffix, start, end);
421                 break;
422
423         case SYS_RES_MEMORY:
424                 ok = 0;
425                 if (pcib_is_nonprefetch_open(sc))
426                         ok = ok || (start >= sc->membase && end <= sc->memlimit);
427                 if (pcib_is_prefetch_open(sc))
428                         ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
429
430                 /*
431                  * Make sure we allow access to VGA memory addresses when the
432                  * bridge has the "VGA Enable" bit set.
433                  */
434                 if (!ok && pci_is_vga_memory_range(start, end))
435                         ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
436
437                 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
438                         if (!ok) {
439                                 ok = 1;
440                                 if (flags & RF_PREFETCHABLE) {
441                                         if (pcib_is_prefetch_open(sc)) {
442                                                 if (start < sc->pmembase)
443                                                         start = sc->pmembase;
444                                                 if (end > sc->pmemlimit)
445                                                         end = sc->pmemlimit;
446                                         } else {
447                                                 ok = 0;
448                                         }
449                                 } else {        /* non-prefetchable */
450                                         if (pcib_is_nonprefetch_open(sc)) {
451                                                 if (start < sc->membase)
452                                                         start = sc->membase;
453                                                 if (end > sc->memlimit)
454                                                         end = sc->memlimit;
455                                         } else {
456                                                 ok = 0;
457                                         }
458                                 }
459                         }
460                 } else if (!ok) {
461                         ok = 1; /* subtractive bridge: always ok */
462 #if 1
463                         if (pcib_is_nonprefetch_open(sc)) {
464                                 if (start < sc->membase && end > sc->memlimit) {
465                                         start = sc->membase;
466                                         end = sc->memlimit;
467                                 }
468                         }
469                         if (pcib_is_prefetch_open(sc)) {
470                                 if (start < sc->pmembase && end > sc->pmemlimit) {
471                                         start = sc->pmembase;
472                                         end = sc->pmemlimit;
473                                 }
474                         }
475 #endif
476                 }
477                 if (end < start) {
478                         device_printf(dev, "memory: end (%lx) < start (%lx)\n",
479                             end, start);
480                         start = 0;
481                         end = 0;
482                         ok = 0;
483                 }
484                 if (!ok && bootverbose)
485                         device_printf(dev,
486                             "%s%srequested unsupported memory range %#lx-%#lx "
487                             "(decoding %#jx-%#jx, %#jx-%#jx)\n",
488                             name, suffix, start, end,
489                             (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
490                             (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
491                 if (!ok)
492                         return (NULL);
493                 if (bootverbose)
494                         device_printf(dev,"%s%srequested memory range "
495                             "0x%lx-0x%lx: good\n",
496                             name, suffix, start, end);
497                 break;
498
499         default:
500                 break;
501         }
502         /*
503          * Bridge is OK decoding this resource, so pass it up.
504          */
505         return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
506             count, flags));
507 }
508
509 /*
510  * PCIB interface.
511  */
512 int
513 pcib_maxslots(device_t dev)
514 {
515     return(PCI_SLOTMAX);
516 }
517
518 /*
519  * Since we are a child of a PCI bus, its parent must support the pcib interface.
520  */
521 uint32_t
522 pcib_read_config(device_t dev, int b, int s, int f, int reg, int width)
523 {
524     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
525 }
526
527 void
528 pcib_write_config(device_t dev, int b, int s, int f, int reg, uint32_t val, int width)
529 {
530     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
531 }
532
533 /*
534  * Route an interrupt across a PCI bridge.
535  */
536 int
537 pcib_route_interrupt(device_t pcib, device_t dev, int pin)
538 {
539     device_t    bus;
540     int         parent_intpin;
541     int         intnum;
542
543     /*  
544      *
545      * The PCI standard defines a swizzle of the child-side device/intpin to
546      * the parent-side intpin as follows.
547      *
548      * device = device on child bus
549      * child_intpin = intpin on child bus slot (0-3)
550      * parent_intpin = intpin on parent bus slot (0-3)
551      *
552      * parent_intpin = (device + child_intpin) % 4
553      */
554     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
555
556     /*
557      * Our parent is a PCI bus.  Its parent must export the pcib interface
558      * which includes the ability to route interrupts.
559      */
560     bus = device_get_parent(pcib);
561     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
562     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
563         device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
564             pci_get_slot(dev), 'A' + pin - 1, intnum);
565     }
566     return(intnum);
567 }
568
569 /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
570 int
571 pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
572 {
573         struct pcib_softc *sc = device_get_softc(pcib);
574         device_t bus;
575
576         if (sc->flags & PCIB_DISABLE_MSI)
577                 return (ENXIO);
578         bus = device_get_parent(pcib);
579         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
580             irqs));
581 }
582
583 /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
584 int
585 pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
586 {
587         device_t bus;
588
589         bus = device_get_parent(pcib);
590         return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
591 }
592
593 /* Pass request to alloc an MSI-X message up to the parent bridge. */
594 int
595 pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
596 {
597         struct pcib_softc *sc = device_get_softc(pcib);
598         device_t bus;
599
600         if (sc->flags & PCIB_DISABLE_MSI)
601                 return (ENXIO);
602         bus = device_get_parent(pcib);
603         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
604 }
605
606 /* Pass request to release an MSI-X message up to the parent bridge. */
607 int
608 pcib_release_msix(device_t pcib, device_t dev, int irq)
609 {
610         device_t bus;
611
612         bus = device_get_parent(pcib);
613         return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
614 }
615
616 /* Pass request to map MSI/MSI-X message up to parent bridge. */
617 int
618 pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
619     uint32_t *data)
620 {
621         device_t bus;
622         int error;
623
624         bus = device_get_parent(pcib);
625         error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
626         if (error)
627                 return (error);
628
629         pci_ht_map_msi(pcib, *addr);
630         return (0);
631 }
632
633 /*
634  * Try to read the bus number of a host-PCI bridge using appropriate config
635  * registers.
636  */
637 int
638 host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
639     uint8_t *busnum)
640 {
641         uint32_t id;
642
643         id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
644         if (id == 0xffffffff)
645                 return (0);
646
647         switch (id) {
648         case 0x12258086:
649                 /* Intel 824?? */
650                 /* XXX This is a guess */
651                 /* *busnum = read_config(bus, slot, func, 0x41, 1); */
652                 *busnum = bus;
653                 break;
654         case 0x84c48086:
655                 /* Intel 82454KX/GX (Orion) */
656                 *busnum = read_config(bus, slot, func, 0x4a, 1);
657                 break;
658         case 0x84ca8086:
659                 /*
660                  * For the 450nx chipset, there is a whole bundle of
661                  * things pretending to be host bridges. The MIOC will 
662                  * be seen first and isn't really a pci bridge (the
663                  * actual busses are attached to the PXB's). We need to 
664                  * read the registers of the MIOC to figure out the
665                  * bus numbers for the PXB channels.
666                  *
667                  * Since the MIOC doesn't have a pci bus attached, we
668                  * pretend it wasn't there.
669                  */
670                 return (0);
671         case 0x84cb8086:
672                 switch (slot) {
673                 case 0x12:
674                         /* Intel 82454NX PXB#0, Bus#A */
675                         *busnum = read_config(bus, 0x10, func, 0xd0, 1);
676                         break;
677                 case 0x13:
678                         /* Intel 82454NX PXB#0, Bus#B */
679                         *busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
680                         break;
681                 case 0x14:
682                         /* Intel 82454NX PXB#1, Bus#A */
683                         *busnum = read_config(bus, 0x10, func, 0xd3, 1);
684                         break;
685                 case 0x15:
686                         /* Intel 82454NX PXB#1, Bus#B */
687                         *busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
688                         break;
689                 }
690                 break;
691
692                 /* ServerWorks -- vendor 0x1166 */
693         case 0x00051166:
694         case 0x00061166:
695         case 0x00081166:
696         case 0x00091166:
697         case 0x00101166:
698         case 0x00111166:
699         case 0x00171166:
700         case 0x01011166:
701         case 0x010f1014:
702         case 0x02011166:
703         case 0x03021014:
704                 *busnum = read_config(bus, slot, func, 0x44, 1);
705                 break;
706
707                 /* Compaq/HP -- vendor 0x0e11 */
708         case 0x60100e11:
709                 *busnum = read_config(bus, slot, func, 0xc8, 1);
710                 break;
711         default:
712                 /* Don't know how to read bus number. */
713                 return 0;
714         }
715
716         return 1;
717 }