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