]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/pci/pci_pci.c
MFC r286886: Fixing typo as well as improving readability of a few comments.
[FreeBSD/stable/8.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/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/libkern.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcib_private.h>
54
55 #include "pcib_if.h"
56
57 static int              pcib_probe(device_t dev);
58
59 static device_method_t pcib_methods[] = {
60     /* Device interface */
61     DEVMETHOD(device_probe,             pcib_probe),
62     DEVMETHOD(device_attach,            pcib_attach),
63     DEVMETHOD(device_detach,            bus_generic_detach),
64     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
65     DEVMETHOD(device_suspend,           bus_generic_suspend),
66     DEVMETHOD(device_resume,            bus_generic_resume),
67
68     /* Bus interface */
69     DEVMETHOD(bus_read_ivar,            pcib_read_ivar),
70     DEVMETHOD(bus_write_ivar,           pcib_write_ivar),
71     DEVMETHOD(bus_alloc_resource,       pcib_alloc_resource),
72 #ifdef NEW_PCIB
73     DEVMETHOD(bus_adjust_resource,      pcib_adjust_resource),
74     DEVMETHOD(bus_release_resource,     pcib_release_resource),
75 #else
76     DEVMETHOD(bus_adjust_resource,      bus_generic_adjust_resource),
77     DEVMETHOD(bus_release_resource,     bus_generic_release_resource),
78 #endif
79     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
80     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
81     DEVMETHOD(bus_setup_intr,           bus_generic_setup_intr),
82     DEVMETHOD(bus_teardown_intr,        bus_generic_teardown_intr),
83
84     /* pcib interface */
85     DEVMETHOD(pcib_maxslots,            pcib_maxslots),
86     DEVMETHOD(pcib_read_config,         pcib_read_config),
87     DEVMETHOD(pcib_write_config,        pcib_write_config),
88     DEVMETHOD(pcib_route_interrupt,     pcib_route_interrupt),
89     DEVMETHOD(pcib_alloc_msi,           pcib_alloc_msi),
90     DEVMETHOD(pcib_release_msi,         pcib_release_msi),
91     DEVMETHOD(pcib_alloc_msix,          pcib_alloc_msix),
92     DEVMETHOD(pcib_release_msix,        pcib_release_msix),
93     DEVMETHOD(pcib_map_msi,             pcib_map_msi),
94
95     DEVMETHOD_END
96 };
97
98 static devclass_t pcib_devclass;
99
100 DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
101 DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
102
103 #ifdef NEW_PCIB
104 /*
105  * XXX Todo:
106  * - properly handle the ISA enable bit.  If it is set, we should change
107  *   the behavior of the I/O window resource and rman to not allocate the
108  *   blocked ranges (upper 768 bytes of each 1K in the first 64k of the
109  *   I/O port address space).
110  */
111
112 /*
113  * Is a resource from a child device sub-allocated from one of our
114  * resource managers?
115  */
116 static int
117 pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
118 {
119
120         switch (type) {
121         case SYS_RES_IOPORT:
122                 return (rman_is_region_manager(r, &sc->io.rman));
123         case SYS_RES_MEMORY:
124                 /* Prefetchable resources may live in either memory rman. */
125                 if (rman_get_flags(r) & RF_PREFETCHABLE &&
126                     rman_is_region_manager(r, &sc->pmem.rman))
127                         return (1);
128                 return (rman_is_region_manager(r, &sc->mem.rman));
129         }
130         return (0);
131 }
132
133 static int
134 pcib_is_window_open(struct pcib_window *pw)
135 {
136
137         return (pw->valid && pw->base < pw->limit);
138 }
139
140 /*
141  * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
142  * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
143  * when allocating the resource windows and rely on the PCI bus driver
144  * to do this for us.
145  */
146 static void
147 pcib_activate_window(struct pcib_softc *sc, int type)
148 {
149
150         PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
151 }
152
153 static void
154 pcib_write_windows(struct pcib_softc *sc, int mask)
155 {
156         device_t dev;
157         uint32_t val;
158
159         dev = sc->dev;
160         if (sc->io.valid && mask & WIN_IO) {
161                 val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
162                 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
163                         pci_write_config(dev, PCIR_IOBASEH_1,
164                             sc->io.base >> 16, 2);
165                         pci_write_config(dev, PCIR_IOLIMITH_1,
166                             sc->io.limit >> 16, 2);
167                 }
168                 pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
169                 pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
170         }
171
172         if (mask & WIN_MEM) {
173                 pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
174                 pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
175         }
176
177         if (sc->pmem.valid && mask & WIN_PMEM) {
178                 val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
179                 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
180                         pci_write_config(dev, PCIR_PMBASEH_1,
181                             sc->pmem.base >> 32, 4);
182                         pci_write_config(dev, PCIR_PMLIMITH_1,
183                             sc->pmem.limit >> 32, 4);
184                 }
185                 pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
186                 pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
187         }
188 }
189
190 static void
191 pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
192     int flags, pci_addr_t max_address)
193 {
194         char buf[64];
195         int error, rid;
196
197         if (max_address != (u_long)max_address)
198                 max_address = ~0ul;
199         w->rman.rm_start = 0;
200         w->rman.rm_end = max_address;
201         w->rman.rm_type = RMAN_ARRAY;
202         snprintf(buf, sizeof(buf), "%s %s window",
203             device_get_nameunit(sc->dev), w->name);
204         w->rman.rm_descr = strdup(buf, M_DEVBUF);
205         error = rman_init(&w->rman);
206         if (error)
207                 panic("Failed to initialize %s %s rman",
208                     device_get_nameunit(sc->dev), w->name);
209
210         if (!pcib_is_window_open(w))
211                 return;
212
213         if (w->base > max_address || w->limit > max_address) {
214                 device_printf(sc->dev,
215                     "initial %s window has too many bits, ignoring\n", w->name);
216                 return;
217         }
218         rid = w->reg;
219         w->res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
220             w->limit - w->base + 1, flags);
221         if (w->res == NULL) {
222                 device_printf(sc->dev,
223                     "failed to allocate initial %s window: %#jx-%#jx\n",
224                     w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
225                 w->base = max_address;
226                 w->limit = 0;
227                 pcib_write_windows(sc, w->mask);
228                 return;
229         }
230         pcib_activate_window(sc, type);
231
232         error = rman_manage_region(&w->rman, rman_get_start(w->res),
233             rman_get_end(w->res));
234         if (error)
235                 panic("Failed to initialize rman with resource");
236 }
237
238 /*
239  * Initialize I/O windows.
240  */
241 static void
242 pcib_probe_windows(struct pcib_softc *sc)
243 {
244         pci_addr_t max;
245         device_t dev;
246         uint32_t val;
247
248         dev = sc->dev;
249
250         /* Determine if the I/O port window is implemented. */
251         val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
252         if (val == 0) {
253                 /*
254                  * If 'val' is zero, then only 16-bits of I/O space
255                  * are supported.
256                  */
257                 pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
258                 if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
259                         sc->io.valid = 1;
260                         pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
261                 }
262         } else
263                 sc->io.valid = 1;
264
265         /* Read the existing I/O port window. */
266         if (sc->io.valid) {
267                 sc->io.reg = PCIR_IOBASEL_1;
268                 sc->io.step = 12;
269                 sc->io.mask = WIN_IO;
270                 sc->io.name = "I/O port";
271                 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
272                         sc->io.base = PCI_PPBIOBASE(
273                             pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
274                         sc->io.limit = PCI_PPBIOLIMIT(
275                             pci_read_config(dev, PCIR_IOLIMITH_1, 2),
276                             pci_read_config(dev, PCIR_IOLIMITL_1, 1));
277                         max = 0xffffffff;
278                 } else {
279                         sc->io.base = PCI_PPBIOBASE(0, val);
280                         sc->io.limit = PCI_PPBIOLIMIT(0,
281                             pci_read_config(dev, PCIR_IOLIMITL_1, 1));
282                         max = 0xffff;
283                 }
284                 pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
285         }
286
287         /* Read the existing memory window. */
288         sc->mem.valid = 1;
289         sc->mem.reg = PCIR_MEMBASE_1;
290         sc->mem.step = 20;
291         sc->mem.mask = WIN_MEM;
292         sc->mem.name = "memory";
293         sc->mem.base = PCI_PPBMEMBASE(0,
294             pci_read_config(dev, PCIR_MEMBASE_1, 2));
295         sc->mem.limit = PCI_PPBMEMLIMIT(0,
296             pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
297         pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
298
299         /* Determine if the prefetchable memory window is implemented. */
300         val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
301         if (val == 0) {
302                 /*
303                  * If 'val' is zero, then only 32-bits of memory space
304                  * are supported.
305                  */
306                 pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
307                 if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
308                         sc->pmem.valid = 1;
309                         pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
310                 }
311         } else
312                 sc->pmem.valid = 1;
313
314         /* Read the existing prefetchable memory window. */
315         if (sc->pmem.valid) {
316                 sc->pmem.reg = PCIR_PMBASEL_1;
317                 sc->pmem.step = 20;
318                 sc->pmem.mask = WIN_PMEM;
319                 sc->pmem.name = "prefetch";
320                 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
321                         sc->pmem.base = PCI_PPBMEMBASE(
322                             pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
323                         sc->pmem.limit = PCI_PPBMEMLIMIT(
324                             pci_read_config(dev, PCIR_PMLIMITH_1, 4),
325                             pci_read_config(dev, PCIR_PMLIMITL_1, 2));
326                         max = 0xffffffffffffffff;
327                 } else {
328                         sc->pmem.base = PCI_PPBMEMBASE(0, val);
329                         sc->pmem.limit = PCI_PPBMEMLIMIT(0,
330                             pci_read_config(dev, PCIR_PMLIMITL_1, 2));
331                         max = 0xffffffff;
332                 }
333                 pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
334                     RF_PREFETCHABLE, max);
335         }
336 }
337
338 #else
339
340 /*
341  * Is the prefetch window open (eg, can we allocate memory in it?)
342  */
343 static int
344 pcib_is_prefetch_open(struct pcib_softc *sc)
345 {
346         return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
347 }
348
349 /*
350  * Is the nonprefetch window open (eg, can we allocate memory in it?)
351  */
352 static int
353 pcib_is_nonprefetch_open(struct pcib_softc *sc)
354 {
355         return (sc->membase > 0 && sc->membase < sc->memlimit);
356 }
357
358 /*
359  * Is the io window open (eg, can we allocate ports in it?)
360  */
361 static int
362 pcib_is_io_open(struct pcib_softc *sc)
363 {
364         return (sc->iobase > 0 && sc->iobase < sc->iolimit);
365 }
366 #endif
367
368 /*
369  * Generic device interface
370  */
371 static int
372 pcib_probe(device_t dev)
373 {
374     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
375         (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
376         device_set_desc(dev, "PCI-PCI bridge");
377         return(-10000);
378     }
379     return(ENXIO);
380 }
381
382 void
383 pcib_attach_common(device_t dev)
384 {
385     struct pcib_softc   *sc;
386 #ifndef NEW_PCIB
387     uint8_t             iolow;
388 #endif
389     struct sysctl_ctx_list *sctx;
390     struct sysctl_oid   *soid;
391
392     sc = device_get_softc(dev);
393     sc->dev = dev;
394
395     /*
396      * Get current bridge configuration.
397      */
398     sc->command   = pci_read_config(dev, PCIR_COMMAND, 1);
399     sc->domain    = pci_get_domain(dev);
400     sc->pribus    = pci_read_config(dev, PCIR_PRIBUS_1, 1);
401     sc->secbus    = pci_read_config(dev, PCIR_SECBUS_1, 1);
402     sc->subbus    = pci_read_config(dev, PCIR_SUBBUS_1, 1);
403     sc->secstat   = pci_read_config(dev, PCIR_SECSTAT_1, 2);
404     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
405     sc->seclat    = pci_read_config(dev, PCIR_SECLAT_1, 1);
406
407     /*
408      * Setup sysctl reporting nodes
409      */
410     sctx = device_get_sysctl_ctx(dev);
411     soid = device_get_sysctl_tree(dev);
412     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
413       CTLFLAG_RD, &sc->domain, 0, "Domain number");
414     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
415       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
416     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
417       CTLFLAG_RD, &sc->secbus, 0, "Secondary bus number");
418     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
419       CTLFLAG_RD, &sc->subbus, 0, "Subordinate bus number");
420
421 #ifndef NEW_PCIB
422     /*
423      * Determine current I/O decode.
424      */
425     if (sc->command & PCIM_CMD_PORTEN) {
426         iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
427         if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
428             sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2),
429                                        pci_read_config(dev, PCIR_IOBASEL_1, 1));
430         } else {
431             sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1));
432         }
433
434         iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
435         if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
436             sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2),
437                                          pci_read_config(dev, PCIR_IOLIMITL_1, 1));
438         } else {
439             sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1));
440         }
441     }
442
443     /*
444      * Determine current memory decode.
445      */
446     if (sc->command & PCIM_CMD_MEMEN) {
447         sc->membase   = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2));
448         sc->memlimit  = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
449         iolow = pci_read_config(dev, PCIR_PMBASEL_1, 1);
450         if ((iolow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
451             sc->pmembase = PCI_PPBMEMBASE(
452                 pci_read_config(dev, PCIR_PMBASEH_1, 4),
453                 pci_read_config(dev, PCIR_PMBASEL_1, 2));
454         else
455             sc->pmembase = PCI_PPBMEMBASE(0,
456                 pci_read_config(dev, PCIR_PMBASEL_1, 2));
457         iolow = pci_read_config(dev, PCIR_PMLIMITL_1, 1);
458         if ((iolow & PCIM_BRPM_MASK) == PCIM_BRPM_64)   
459             sc->pmemlimit = PCI_PPBMEMLIMIT(
460                 pci_read_config(dev, PCIR_PMLIMITH_1, 4),
461                 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
462         else
463             sc->pmemlimit = PCI_PPBMEMLIMIT(0,
464                 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
465     }
466 #endif
467
468     /*
469      * Quirk handling.
470      */
471     switch (pci_get_devid(dev)) {
472     case 0x12258086:            /* Intel 82454KX/GX (Orion) */
473         {
474             uint8_t     supbus;
475
476             supbus = pci_read_config(dev, 0x41, 1);
477             if (supbus != 0xff) {
478                 sc->secbus = supbus + 1;
479                 sc->subbus = supbus + 1;
480             }
481             break;
482         }
483
484     /*
485      * The i82380FB mobile docking controller is a PCI-PCI bridge,
486      * and it is a subtractive bridge.  However, the ProgIf is wrong
487      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
488      * happen.  There's also a Toshiba bridge that behaves this
489      * way.
490      */
491     case 0x124b8086:            /* Intel 82380FB Mobile */
492     case 0x060513d7:            /* Toshiba ???? */
493         sc->flags |= PCIB_SUBTRACTIVE;
494         break;
495
496     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
497     case 0x00dd10de:
498         {
499             char *cp;
500
501             if ((cp = getenv("smbios.planar.maker")) == NULL)
502                 break;
503             if (strncmp(cp, "Compal", 6) != 0) {
504                 freeenv(cp);
505                 break;
506             }
507             freeenv(cp);
508             if ((cp = getenv("smbios.planar.product")) == NULL)
509                 break;
510             if (strncmp(cp, "08A0", 4) != 0) {
511                 freeenv(cp);
512                 break;
513             }
514             freeenv(cp);
515             if (sc->subbus < 0xa) {
516                 pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
517                 sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
518             }
519             break;
520         }
521     }
522
523     if (pci_msi_device_blacklisted(dev))
524         sc->flags |= PCIB_DISABLE_MSI;
525
526     /*
527      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
528      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
529      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
530      * This means they act as if they were subtractively decoding
531      * bridges and pass all transactions.  Mark them and real ProgIf 1
532      * parts as subtractive.
533      */
534     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
535       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
536         sc->flags |= PCIB_SUBTRACTIVE;
537
538 #ifdef NEW_PCIB
539     pcib_probe_windows(sc);
540 #endif
541     if (bootverbose) {
542         device_printf(dev, "  domain            %d\n", sc->domain);
543         device_printf(dev, "  secondary bus     %d\n", sc->secbus);
544         device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
545 #ifdef NEW_PCIB
546         if (pcib_is_window_open(&sc->io))
547             device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
548               (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
549         if (pcib_is_window_open(&sc->mem))
550             device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
551               (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
552         if (pcib_is_window_open(&sc->pmem))
553             device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
554               (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
555 #else
556         if (pcib_is_io_open(sc))
557             device_printf(dev, "  I/O decode        0x%x-0x%x\n",
558               sc->iobase, sc->iolimit);
559         if (pcib_is_nonprefetch_open(sc))
560             device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
561               (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
562         if (pcib_is_prefetch_open(sc))
563             device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
564               (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
565 #endif
566         else
567             device_printf(dev, "  no prefetched decode\n");
568         if (sc->flags & PCIB_SUBTRACTIVE)
569             device_printf(dev, "  Subtractively decoded bridge.\n");
570     }
571
572     /*
573      * XXX If the secondary bus number is zero, we should assign a bus number
574      *     since the BIOS hasn't, then initialise the bridge.  A simple
575      *     bus_alloc_resource with the a couple of busses seems like the right
576      *     approach, but we don't know what busses the BIOS might have already
577      *     assigned to other bridges on this bus that probe later than we do.
578      *
579      *     If the subordinate bus number is less than the secondary bus number,
580      *     we should pick a better value.  One sensible alternative would be to
581      *     pick 255; the only tradeoff here is that configuration transactions
582      *     would be more widely routed than absolutely necessary.  We could
583      *     then do a walk of the tree later and fix it.
584      */
585
586     /*
587      * Always enable busmastering on bridges so that transactions
588      * initiated on the secondary bus are passed through to the
589      * primary bus.
590      */
591     pci_enable_busmaster(dev);
592 }
593
594 int
595 pcib_attach(device_t dev)
596 {
597     struct pcib_softc   *sc;
598     device_t            child;
599
600     pcib_attach_common(dev);
601     sc = device_get_softc(dev);
602     if (sc->secbus != 0) {
603         child = device_add_child(dev, "pci", sc->secbus);
604         if (child != NULL)
605             return(bus_generic_attach(dev));
606     }
607
608     /* no secondary bus; we should have fixed this */
609     return(0);
610 }
611
612 int
613 pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
614 {
615     struct pcib_softc   *sc = device_get_softc(dev);
616     
617     switch (which) {
618     case PCIB_IVAR_DOMAIN:
619         *result = sc->domain;
620         return(0);
621     case PCIB_IVAR_BUS:
622         *result = sc->secbus;
623         return(0);
624     }
625     return(ENOENT);
626 }
627
628 int
629 pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
630 {
631     struct pcib_softc   *sc = device_get_softc(dev);
632
633     switch (which) {
634     case PCIB_IVAR_DOMAIN:
635         return(EINVAL);
636     case PCIB_IVAR_BUS:
637         sc->secbus = value;
638         return(0);
639     }
640     return(ENOENT);
641 }
642
643 #ifdef NEW_PCIB
644 static const char *
645 pcib_child_name(device_t child)
646 {
647         static char buf[64];
648
649         if (device_get_nameunit(child) != NULL)
650                 return (device_get_nameunit(child));
651         snprintf(buf, sizeof(buf), "pci%d:%d:%d:%d", pci_get_domain(child),
652             pci_get_bus(child), pci_get_slot(child), pci_get_function(child));
653         return (buf);
654 }
655
656 /*
657  * Attempt to allocate a resource from the existing resources assigned
658  * to a window.
659  */
660 static struct resource *
661 pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
662     device_t child, int type, int *rid, u_long start, u_long end, u_long count,
663     u_int flags)
664 {
665         struct resource *res;
666
667         if (!pcib_is_window_open(w))
668                 return (NULL);
669
670         res = rman_reserve_resource(&w->rman, start, end, count,
671             flags & ~RF_ACTIVE, child);
672         if (res == NULL)
673                 return (NULL);
674
675         if (bootverbose)
676                 device_printf(sc->dev,
677                     "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
678                     w->name, rman_get_start(res), rman_get_end(res), *rid,
679                     pcib_child_name(child));
680         rman_set_rid(res, *rid);
681
682         /*
683          * If the resource should be active, pass that request up the
684          * tree.  This assumes the parent drivers can handle
685          * activating sub-allocated resources.
686          */
687         if (flags & RF_ACTIVE) {
688                 if (bus_activate_resource(child, type, *rid, res) != 0) {
689                         rman_release_resource(res);
690                         return (NULL);
691                 }
692         }
693
694         return (res);
695 }
696
697 /*
698  * Attempt to grow a window to make room for a given resource request.
699  * The 'step' parameter is log_2 of the desired I/O window's alignment.
700  */
701 static int
702 pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
703     u_long start, u_long end, u_long count, u_int flags)
704 {
705         u_long align, start_free, end_free, front, back, wmask;
706         int error, rid;
707
708         /*
709          * Clamp the desired resource range to the maximum address
710          * this window supports.  Reject impossible requests.
711          */
712         if (!w->valid)
713                 return (EINVAL);
714         if (end > w->rman.rm_end)
715                 end = w->rman.rm_end;
716         if (start + count - 1 > end || start + count < start)
717                 return (EINVAL);
718         wmask = (1ul << w->step) - 1;
719
720         /*
721          * If there is no resource at all, just try to allocate enough
722          * aligned space for this resource.
723          */
724         if (w->res == NULL) {
725                 if (RF_ALIGNMENT(flags) < w->step) {
726                         flags &= ~RF_ALIGNMENT_MASK;
727                         flags |= RF_ALIGNMENT_LOG2(w->step);
728                 }
729                 start &= ~wmask;
730                 end |= wmask;
731                 count = roundup2(count, 1ul << w->step);
732                 rid = w->reg;
733                 w->res = bus_alloc_resource(sc->dev, type, &rid, start, end,
734                     count, flags & ~RF_ACTIVE);
735                 if (w->res == NULL) {
736                         if (bootverbose)
737                                 device_printf(sc->dev,
738                     "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
739                                     w->name, start, end, count);
740                         return (ENXIO);
741                 }
742                 if (bootverbose)
743                         device_printf(sc->dev,
744                             "allocated initial %s window of %#lx-%#lx\n",
745                             w->name, rman_get_start(w->res),
746                             rman_get_end(w->res));
747                 error = rman_manage_region(&w->rman, rman_get_start(w->res),
748                     rman_get_end(w->res));
749                 if (error) {
750                         if (bootverbose)
751                                 device_printf(sc->dev,
752                                     "failed to add initial %s window to rman\n",
753                                     w->name);
754                         bus_release_resource(sc->dev, type, w->reg, w->res);
755                         w->res = NULL;
756                         return (error);
757                 }
758                 pcib_activate_window(sc, type);
759                 goto updatewin;
760         }
761
762         /*
763          * See if growing the window would help.  Compute the minimum
764          * amount of address space needed on both the front and back
765          * ends of the existing window to satisfy the allocation.
766          *
767          * For each end, build a candidate region adjusting for the
768          * required alignment, etc.  If there is a free region at the
769          * edge of the window, grow from the inner edge of the free
770          * region.  Otherwise grow from the window boundary.
771          *
772          * XXX: Special case: if w->res is completely empty and the
773          * request size is larger than w->res, we should find the
774          * optimal aligned buffer containing w->res and allocate that.
775          */
776         if (bootverbose)
777                 device_printf(sc->dev,
778                     "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
779                     w->name, start, end, count);
780         align = 1ul << RF_ALIGNMENT(flags);
781         if (start < rman_get_start(w->res)) {
782                 if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
783                     0 || start_free != rman_get_start(w->res))
784                         end_free = rman_get_start(w->res);
785                 if (end_free > end)
786                         end_free = end + 1;
787
788                 /* Move end_free down until it is properly aligned. */
789                 end_free &= ~(align - 1);
790                 end_free--;
791                 front = end_free - (count - 1);
792
793                 /*
794                  * The resource would now be allocated at (front,
795                  * end_free).  Ensure that fits in the (start, end)
796                  * bounds.  end_free is checked above.  If 'front' is
797                  * ok, ensure it is properly aligned for this window.
798                  * Also check for underflow.
799                  */
800                 if (front >= start && front <= end_free) {
801                         if (bootverbose)
802                                 printf("\tfront candidate range: %#lx-%#lx\n",
803                                     front, end_free);
804                         front &= ~wmask;
805                         front = rman_get_start(w->res) - front;
806                 } else
807                         front = 0;
808         } else
809                 front = 0;
810         if (end > rman_get_end(w->res)) {
811                 if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
812                     0 || end_free != rman_get_end(w->res))
813                         start_free = rman_get_end(w->res) + 1;
814                 if (start_free < start)
815                         start_free = start;
816
817                 /* Move start_free up until it is properly aligned. */
818                 start_free = roundup2(start_free, align);
819                 back = start_free + count - 1;
820
821                 /*
822                  * The resource would now be allocated at (start_free,
823                  * back).  Ensure that fits in the (start, end)
824                  * bounds.  start_free is checked above.  If 'back' is
825                  * ok, ensure it is properly aligned for this window.
826                  * Also check for overflow.
827                  */
828                 if (back <= end && start_free <= back) {
829                         if (bootverbose)
830                                 printf("\tback candidate range: %#lx-%#lx\n",
831                                     start_free, back);
832                         back |= wmask;
833                         back -= rman_get_end(w->res);
834                 } else
835                         back = 0;
836         } else
837                 back = 0;
838
839         /*
840          * Try to allocate the smallest needed region first.
841          * If that fails, fall back to the other region.
842          */
843         error = ENOSPC;
844         while (front != 0 || back != 0) {
845                 if (front != 0 && (front <= back || back == 0)) {
846                         error = bus_adjust_resource(sc->dev, type, w->res,
847                             rman_get_start(w->res) - front,
848                             rman_get_end(w->res));
849                         if (error == 0)
850                                 break;
851                         front = 0;
852                 } else {
853                         error = bus_adjust_resource(sc->dev, type, w->res,
854                             rman_get_start(w->res),
855                             rman_get_end(w->res) + back);
856                         if (error == 0)
857                                 break;
858                         back = 0;
859                 }
860         }
861
862         if (error)
863                 return (error);
864         if (bootverbose)
865                 device_printf(sc->dev, "grew %s window to %#lx-%#lx\n",
866                     w->name, rman_get_start(w->res), rman_get_end(w->res));
867
868         /* Add the newly allocated region to the resource manager. */
869         if (w->base != rman_get_start(w->res)) {
870                 KASSERT(w->limit == rman_get_end(w->res), ("both ends moved"));
871                 error = rman_manage_region(&w->rman, rman_get_start(w->res),
872                     w->base - 1);
873         } else {
874                 KASSERT(w->limit != rman_get_end(w->res),
875                     ("neither end moved"));
876                 error = rman_manage_region(&w->rman, w->limit + 1,
877                     rman_get_end(w->res));
878         }
879         if (error) {
880                 if (bootverbose)
881                         device_printf(sc->dev,
882                             "failed to expand %s resource manager\n", w->name);
883                 bus_adjust_resource(sc->dev, type, w->res, w->base, w->limit);
884                 return (error);
885         }
886
887 updatewin:
888         /* Save the new window. */
889         w->base = rman_get_start(w->res);
890         w->limit = rman_get_end(w->res);
891         KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
892         KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
893         pcib_write_windows(sc, w->mask);
894         return (0);
895 }
896
897 /*
898  * We have to trap resource allocation requests and ensure that the bridge
899  * is set up to, or capable of handling them.
900  */
901 struct resource *
902 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
903     u_long start, u_long end, u_long count, u_int flags)
904 {
905         struct pcib_softc *sc;
906         struct resource *r;
907
908         sc = device_get_softc(dev);
909
910         /*
911          * VGA resources are decoded iff the VGA enable bit is set in
912          * the bridge control register.  VGA resources do not fall into
913          * the resource windows and are passed up to the parent.
914          */
915         if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
916             (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
917                 if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
918                         return (bus_generic_alloc_resource(dev, child, type,
919                             rid, start, end, count, flags));
920                 else
921                         return (NULL);
922         }
923
924         switch (type) {
925         case SYS_RES_IOPORT:
926                 r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
927                     end, count, flags);
928                 if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
929                         break;
930                 if (pcib_grow_window(sc, &sc->io, type, start, end, count,
931                     flags) == 0)
932                         r = pcib_suballoc_resource(sc, &sc->io, child, type,
933                             rid, start, end, count, flags);
934                 break;
935         case SYS_RES_MEMORY:
936                 /*
937                  * For prefetchable resources, prefer the prefetchable
938                  * memory window, but fall back to the regular memory
939                  * window if that fails.  Try both windows before
940                  * attempting to grow a window in case the firmware
941                  * has used a range in the regular memory window to
942                  * map a prefetchable BAR.
943                  */
944                 if (flags & RF_PREFETCHABLE) {
945                         r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
946                             rid, start, end, count, flags);
947                         if (r != NULL)
948                                 break;
949                 }
950                 r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
951                     start, end, count, flags);
952                 if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
953                         break;
954                 if (flags & RF_PREFETCHABLE) {
955                         if (pcib_grow_window(sc, &sc->pmem, type, start, end,
956                             count, flags) == 0) {
957                                 r = pcib_suballoc_resource(sc, &sc->pmem, child,
958                                     type, rid, start, end, count, flags);
959                                 if (r != NULL)
960                                         break;
961                         }
962                 }
963                 if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
964                     flags & ~RF_PREFETCHABLE) == 0)
965                         r = pcib_suballoc_resource(sc, &sc->mem, child, type,
966                             rid, start, end, count, flags);
967                 break;
968         default:
969                 return (bus_generic_alloc_resource(dev, child, type, rid,
970                     start, end, count, flags));
971         }
972
973         /*
974          * If attempts to suballocate from the window fail but this is a
975          * subtractive bridge, pass the request up the tree.
976          */
977         if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
978                 return (bus_generic_alloc_resource(dev, child, type, rid,
979                     start, end, count, flags));
980         return (r);
981 }
982
983 int
984 pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
985     u_long start, u_long end)
986 {
987         struct pcib_softc *sc;
988
989         sc = device_get_softc(bus);
990         if (pcib_is_resource_managed(sc, type, r))
991                 return (rman_adjust_resource(r, start, end));
992         return (bus_generic_adjust_resource(bus, child, type, r, start, end));
993 }
994
995 int
996 pcib_release_resource(device_t dev, device_t child, int type, int rid,
997     struct resource *r)
998 {
999         struct pcib_softc *sc;
1000         int error;
1001
1002         sc = device_get_softc(dev);
1003         if (pcib_is_resource_managed(sc, type, r)) {
1004                 if (rman_get_flags(r) & RF_ACTIVE) {
1005                         error = bus_deactivate_resource(child, type, rid, r);
1006                         if (error)
1007                                 return (error);
1008                 }
1009                 return (rman_release_resource(r));
1010         }
1011         return (bus_generic_release_resource(dev, child, type, rid, r));
1012 }
1013 #else
1014 /*
1015  * We have to trap resource allocation requests and ensure that the bridge
1016  * is set up to, or capable of handling them.
1017  */
1018 struct resource *
1019 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 
1020     u_long start, u_long end, u_long count, u_int flags)
1021 {
1022         struct pcib_softc       *sc = device_get_softc(dev);
1023         const char *name, *suffix;
1024         int ok;
1025
1026         /*
1027          * Fail the allocation for this range if it's not supported.
1028          */
1029         name = device_get_nameunit(child);
1030         if (name == NULL) {
1031                 name = "";
1032                 suffix = "";
1033         } else
1034                 suffix = " ";
1035         switch (type) {
1036         case SYS_RES_IOPORT:
1037                 ok = 0;
1038                 if (!pcib_is_io_open(sc))
1039                         break;
1040                 ok = (start >= sc->iobase && end <= sc->iolimit);
1041
1042                 /*
1043                  * Make sure we allow access to VGA I/O addresses when the
1044                  * bridge has the "VGA Enable" bit set.
1045                  */
1046                 if (!ok && pci_is_vga_ioport_range(start, end))
1047                         ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1048
1049                 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1050                         if (!ok) {
1051                                 if (start < sc->iobase)
1052                                         start = sc->iobase;
1053                                 if (end > sc->iolimit)
1054                                         end = sc->iolimit;
1055                                 if (start < end)
1056                                         ok = 1;
1057                         }
1058                 } else {
1059                         ok = 1;
1060 #if 0
1061                         /*
1062                          * If we overlap with the subtractive range, then
1063                          * pick the upper range to use.
1064                          */
1065                         if (start < sc->iolimit && end > sc->iobase)
1066                                 start = sc->iolimit + 1;
1067 #endif
1068                 }
1069                 if (end < start) {
1070                         device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
1071                             end, start);
1072                         start = 0;
1073                         end = 0;
1074                         ok = 0;
1075                 }
1076                 if (!ok) {
1077                         device_printf(dev, "%s%srequested unsupported I/O "
1078                             "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
1079                             name, suffix, start, end, sc->iobase, sc->iolimit);
1080                         return (NULL);
1081                 }
1082                 if (bootverbose)
1083                         device_printf(dev,
1084                             "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
1085                             name, suffix, start, end);
1086                 break;
1087
1088         case SYS_RES_MEMORY:
1089                 ok = 0;
1090                 if (pcib_is_nonprefetch_open(sc))
1091                         ok = ok || (start >= sc->membase && end <= sc->memlimit);
1092                 if (pcib_is_prefetch_open(sc))
1093                         ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1094
1095                 /*
1096                  * Make sure we allow access to VGA memory addresses when the
1097                  * bridge has the "VGA Enable" bit set.
1098                  */
1099                 if (!ok && pci_is_vga_memory_range(start, end))
1100                         ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1101
1102                 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1103                         if (!ok) {
1104                                 ok = 1;
1105                                 if (flags & RF_PREFETCHABLE) {
1106                                         if (pcib_is_prefetch_open(sc)) {
1107                                                 if (start < sc->pmembase)
1108                                                         start = sc->pmembase;
1109                                                 if (end > sc->pmemlimit)
1110                                                         end = sc->pmemlimit;
1111                                         } else {
1112                                                 ok = 0;
1113                                         }
1114                                 } else {        /* non-prefetchable */
1115                                         if (pcib_is_nonprefetch_open(sc)) {
1116                                                 if (start < sc->membase)
1117                                                         start = sc->membase;
1118                                                 if (end > sc->memlimit)
1119                                                         end = sc->memlimit;
1120                                         } else {
1121                                                 ok = 0;
1122                                         }
1123                                 }
1124                         }
1125                 } else if (!ok) {
1126                         ok = 1; /* subtractive bridge: always ok */
1127 #if 0
1128                         if (pcib_is_nonprefetch_open(sc)) {
1129                                 if (start < sc->memlimit && end > sc->membase)
1130                                         start = sc->memlimit + 1;
1131                         }
1132                         if (pcib_is_prefetch_open(sc)) {
1133                                 if (start < sc->pmemlimit && end > sc->pmembase)
1134                                         start = sc->pmemlimit + 1;
1135                         }
1136 #endif
1137                 }
1138                 if (end < start) {
1139                         device_printf(dev, "memory: end (%lx) < start (%lx)\n",
1140                             end, start);
1141                         start = 0;
1142                         end = 0;
1143                         ok = 0;
1144                 }
1145                 if (!ok && bootverbose)
1146                         device_printf(dev,
1147                             "%s%srequested unsupported memory range %#lx-%#lx "
1148                             "(decoding %#jx-%#jx, %#jx-%#jx)\n",
1149                             name, suffix, start, end,
1150                             (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1151                             (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1152                 if (!ok)
1153                         return (NULL);
1154                 if (bootverbose)
1155                         device_printf(dev,"%s%srequested memory range "
1156                             "0x%lx-0x%lx: good\n",
1157                             name, suffix, start, end);
1158                 break;
1159
1160         default:
1161                 break;
1162         }
1163         /*
1164          * Bridge is OK decoding this resource, so pass it up.
1165          */
1166         return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
1167             count, flags));
1168 }
1169 #endif
1170
1171 /*
1172  * PCIB interface.
1173  */
1174 int
1175 pcib_maxslots(device_t dev)
1176 {
1177     return(PCI_SLOTMAX);
1178 }
1179
1180 /*
1181  * Since we are a child of a PCI bus, its parent must support the pcib interface.
1182  */
1183 uint32_t
1184 pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
1185 {
1186     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
1187 }
1188
1189 void
1190 pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
1191 {
1192     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
1193 }
1194
1195 /*
1196  * Route an interrupt across a PCI bridge.
1197  */
1198 int
1199 pcib_route_interrupt(device_t pcib, device_t dev, int pin)
1200 {
1201     device_t    bus;
1202     int         parent_intpin;
1203     int         intnum;
1204
1205     /*  
1206      *
1207      * The PCI standard defines a swizzle of the child-side device/intpin to
1208      * the parent-side intpin as follows.
1209      *
1210      * device = device on child bus
1211      * child_intpin = intpin on child bus slot (0-3)
1212      * parent_intpin = intpin on parent bus slot (0-3)
1213      *
1214      * parent_intpin = (device + child_intpin) % 4
1215      */
1216     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
1217
1218     /*
1219      * Our parent is a PCI bus.  Its parent must export the pcib interface
1220      * which includes the ability to route interrupts.
1221      */
1222     bus = device_get_parent(pcib);
1223     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
1224     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1225         device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1226             pci_get_slot(dev), 'A' + pin - 1, intnum);
1227     }
1228     return(intnum);
1229 }
1230
1231 /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
1232 int
1233 pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
1234 {
1235         struct pcib_softc *sc = device_get_softc(pcib);
1236         device_t bus;
1237
1238         if (sc->flags & PCIB_DISABLE_MSI)
1239                 return (ENXIO);
1240         bus = device_get_parent(pcib);
1241         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
1242             irqs));
1243 }
1244
1245 /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
1246 int
1247 pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
1248 {
1249         device_t bus;
1250
1251         bus = device_get_parent(pcib);
1252         return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
1253 }
1254
1255 /* Pass request to alloc an MSI-X message up to the parent bridge. */
1256 int
1257 pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
1258 {
1259         struct pcib_softc *sc = device_get_softc(pcib);
1260         device_t bus;
1261
1262         if (sc->flags & PCIB_DISABLE_MSI)
1263                 return (ENXIO);
1264         bus = device_get_parent(pcib);
1265         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
1266 }
1267
1268 /* Pass request to release an MSI-X message up to the parent bridge. */
1269 int
1270 pcib_release_msix(device_t pcib, device_t dev, int irq)
1271 {
1272         device_t bus;
1273
1274         bus = device_get_parent(pcib);
1275         return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
1276 }
1277
1278 /* Pass request to map MSI/MSI-X message up to parent bridge. */
1279 int
1280 pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
1281     uint32_t *data)
1282 {
1283         device_t bus;
1284         int error;
1285
1286         bus = device_get_parent(pcib);
1287         error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
1288         if (error)
1289                 return (error);
1290
1291         pci_ht_map_msi(pcib, *addr);
1292         return (0);
1293 }
1294
1295 /*
1296  * Try to read the bus number of a host-PCI bridge using appropriate config
1297  * registers.
1298  */
1299 int
1300 host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
1301     uint8_t *busnum)
1302 {
1303         uint32_t id;
1304
1305         id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
1306         if (id == 0xffffffff)
1307                 return (0);
1308
1309         switch (id) {
1310         case 0x12258086:
1311                 /* Intel 824?? */
1312                 /* XXX This is a guess */
1313                 /* *busnum = read_config(bus, slot, func, 0x41, 1); */
1314                 *busnum = bus;
1315                 break;
1316         case 0x84c48086:
1317                 /* Intel 82454KX/GX (Orion) */
1318                 *busnum = read_config(bus, slot, func, 0x4a, 1);
1319                 break;
1320         case 0x84ca8086:
1321                 /*
1322                  * For the 450nx chipset, there is a whole bundle of
1323                  * things pretending to be host bridges. The MIOC will 
1324                  * be seen first and isn't really a pci bridge (the
1325                  * actual busses are attached to the PXB's). We need to 
1326                  * read the registers of the MIOC to figure out the
1327                  * bus numbers for the PXB channels.
1328                  *
1329                  * Since the MIOC doesn't have a pci bus attached, we
1330                  * pretend it wasn't there.
1331                  */
1332                 return (0);
1333         case 0x84cb8086:
1334                 switch (slot) {
1335                 case 0x12:
1336                         /* Intel 82454NX PXB#0, Bus#A */
1337                         *busnum = read_config(bus, 0x10, func, 0xd0, 1);
1338                         break;
1339                 case 0x13:
1340                         /* Intel 82454NX PXB#0, Bus#B */
1341                         *busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
1342                         break;
1343                 case 0x14:
1344                         /* Intel 82454NX PXB#1, Bus#A */
1345                         *busnum = read_config(bus, 0x10, func, 0xd3, 1);
1346                         break;
1347                 case 0x15:
1348                         /* Intel 82454NX PXB#1, Bus#B */
1349                         *busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
1350                         break;
1351                 }
1352                 break;
1353
1354                 /* ServerWorks -- vendor 0x1166 */
1355         case 0x00051166:
1356         case 0x00061166:
1357         case 0x00081166:
1358         case 0x00091166:
1359         case 0x00101166:
1360         case 0x00111166:
1361         case 0x00171166:
1362         case 0x01011166:
1363         case 0x010f1014:
1364         case 0x01101166:
1365         case 0x02011166:
1366         case 0x02251166:
1367         case 0x03021014:
1368                 *busnum = read_config(bus, slot, func, 0x44, 1);
1369                 break;
1370
1371                 /* Compaq/HP -- vendor 0x0e11 */
1372         case 0x60100e11:
1373                 *busnum = read_config(bus, slot, func, 0xc8, 1);
1374                 break;
1375         default:
1376                 /* Don't know how to read bus number. */
1377                 return 0;
1378         }
1379
1380         return 1;
1381 }