]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/pci/pci_pci.c
MFC 233708,234154:
[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 int
588 pcib_attach(device_t dev)
589 {
590     struct pcib_softc   *sc;
591     device_t            child;
592
593     pcib_attach_common(dev);
594     sc = device_get_softc(dev);
595     if (sc->secbus != 0) {
596         child = device_add_child(dev, "pci", sc->secbus);
597         if (child != NULL)
598             return(bus_generic_attach(dev));
599     }
600
601     /* no secondary bus; we should have fixed this */
602     return(0);
603 }
604
605 int
606 pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
607 {
608     struct pcib_softc   *sc = device_get_softc(dev);
609     
610     switch (which) {
611     case PCIB_IVAR_DOMAIN:
612         *result = sc->domain;
613         return(0);
614     case PCIB_IVAR_BUS:
615         *result = sc->secbus;
616         return(0);
617     }
618     return(ENOENT);
619 }
620
621 int
622 pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
623 {
624     struct pcib_softc   *sc = device_get_softc(dev);
625
626     switch (which) {
627     case PCIB_IVAR_DOMAIN:
628         return(EINVAL);
629     case PCIB_IVAR_BUS:
630         sc->secbus = value;
631         return(0);
632     }
633     return(ENOENT);
634 }
635
636 #ifdef NEW_PCIB
637 static const char *
638 pcib_child_name(device_t child)
639 {
640         static char buf[64];
641
642         if (device_get_nameunit(child) != NULL)
643                 return (device_get_nameunit(child));
644         snprintf(buf, sizeof(buf), "pci%d:%d:%d:%d", pci_get_domain(child),
645             pci_get_bus(child), pci_get_slot(child), pci_get_function(child));
646         return (buf);
647 }
648
649 /*
650  * Attempt to allocate a resource from the existing resources assigned
651  * to a window.
652  */
653 static struct resource *
654 pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
655     device_t child, int type, int *rid, u_long start, u_long end, u_long count,
656     u_int flags)
657 {
658         struct resource *res;
659
660         if (!pcib_is_window_open(w))
661                 return (NULL);
662
663         res = rman_reserve_resource(&w->rman, start, end, count,
664             flags & ~RF_ACTIVE, child);
665         if (res == NULL)
666                 return (NULL);
667
668         if (bootverbose)
669                 device_printf(sc->dev,
670                     "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
671                     w->name, rman_get_start(res), rman_get_end(res), *rid,
672                     pcib_child_name(child));
673         rman_set_rid(res, *rid);
674
675         /*
676          * If the resource should be active, pass that request up the
677          * tree.  This assumes the parent drivers can handle
678          * activating sub-allocated resources.
679          */
680         if (flags & RF_ACTIVE) {
681                 if (bus_activate_resource(child, type, *rid, res) != 0) {
682                         rman_release_resource(res);
683                         return (NULL);
684                 }
685         }
686
687         return (res);
688 }
689
690 /*
691  * Attempt to grow a window to make room for a given resource request.
692  * The 'step' parameter is log_2 of the desired I/O window's alignment.
693  */
694 static int
695 pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
696     u_long start, u_long end, u_long count, u_int flags)
697 {
698         u_long align, start_free, end_free, front, back;
699         int error, rid;
700
701         /*
702          * Clamp the desired resource range to the maximum address
703          * this window supports.  Reject impossible requests.
704          */
705         if (!w->valid)
706                 return (EINVAL);
707         if (end > w->rman.rm_end)
708                 end = w->rman.rm_end;
709         if (start + count - 1 > end || start + count < start)
710                 return (EINVAL);
711
712         /*
713          * If there is no resource at all, just try to allocate enough
714          * aligned space for this resource.
715          */
716         if (w->res == NULL) {
717                 if (RF_ALIGNMENT(flags) < w->step) {
718                         flags &= ~RF_ALIGNMENT_MASK;
719                         flags |= RF_ALIGNMENT_LOG2(w->step);
720                 }
721                 start &= ~((1ul << w->step) - 1);
722                 end |= ((1ul << w->step) - 1);
723                 count = roundup2(count, 1ul << w->step);
724                 rid = w->reg;
725                 w->res = bus_alloc_resource(sc->dev, type, &rid, start, end,
726                     count, flags & ~RF_ACTIVE);
727                 if (w->res == NULL) {
728                         if (bootverbose)
729                                 device_printf(sc->dev,
730                     "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
731                                     w->name, start, end, count);
732                         return (ENXIO);
733                 }
734                 if (bootverbose)
735                         device_printf(sc->dev,
736                             "allocated initial %s window of %#lx-%#lx\n",
737                             w->name, rman_get_start(w->res),
738                             rman_get_end(w->res));
739                 error = rman_manage_region(&w->rman, rman_get_start(w->res),
740                     rman_get_end(w->res));
741                 if (error) {
742                         if (bootverbose)
743                                 device_printf(sc->dev,
744                                     "failed to add initial %s window to rman\n",
745                                     w->name);
746                         bus_release_resource(sc->dev, type, w->reg, w->res);
747                         w->res = NULL;
748                         return (error);
749                 }
750                 pcib_activate_window(sc, type);
751                 goto updatewin;
752         }
753
754         /*
755          * See if growing the window would help.  Compute the minimum
756          * amount of address space needed on both the front and back
757          * ends of the existing window to satisfy the allocation.
758          *
759          * For each end, build a candidate region adjusting for the
760          * required alignment, etc.  If there is a free region at the
761          * edge of the window, grow from the inner edge of the free
762          * region.  Otherwise grow from the window boundary.
763          *
764          * XXX: Special case: if w->res is completely empty and the
765          * request size is larger than w->res, we should find the
766          * optimal aligned buffer containing w->res and allocate that.
767          */
768         if (bootverbose)
769                 device_printf(sc->dev,
770                     "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
771                     w->name, start, end, count);
772         align = 1ul << RF_ALIGNMENT(flags);
773         if (start < rman_get_start(w->res)) {
774                 if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
775                     0 || start_free != rman_get_start(w->res))
776                         end_free = rman_get_start(w->res) - 1;
777                 if (end_free > end)
778                         end_free = end;
779
780                 /* Move end_free down until it is properly aligned. */
781                 end_free &= ~(align - 1);
782                 end_free--;
783                 front = end_free - (count - 1);
784
785                 /*
786                  * The resource would now be allocated at (front,
787                  * end_free).  Ensure that fits in the (start, end)
788                  * bounds.  end_free is checked above.  If 'front' is
789                  * ok, ensure it is properly aligned for this window.
790                  * Also check for underflow.
791                  */
792                 if (front >= start && front <= end_free) {
793                         if (bootverbose)
794                                 printf("\tfront candidate range: %#lx-%#lx\n",
795                                     front, end_free);
796                         front &= (1ul << w->step) - 1;
797                         front = rman_get_start(w->res) - front;
798                 } else
799                         front = 0;
800         } else
801                 front = 0;
802         if (end > rman_get_end(w->res)) {
803                 if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
804                     0 || end_free != rman_get_end(w->res))
805                         start_free = rman_get_end(w->res) + 1;
806                 if (start_free < start)
807                         start_free = start;
808
809                 /* Move start_free up until it is properly aligned. */
810                 start_free = roundup2(start_free, align);
811                 back = start_free + count - 1;
812
813                 /*
814                  * The resource would now be allocated at (start_free,
815                  * back).  Ensure that fits in the (start, end)
816                  * bounds.  start_free is checked above.  If 'back' is
817                  * ok, ensure it is properly aligned for this window.
818                  * Also check for overflow.
819                  */
820                 if (back <= end && start_free <= back) {
821                         if (bootverbose)
822                                 printf("\tback candidate range: %#lx-%#lx\n",
823                                     start_free, back);
824                         back = roundup2(back + 1, 1ul << w->step) - 1;
825                         back -= rman_get_end(w->res);
826                 } else
827                         back = 0;
828         } else
829                 back = 0;
830
831         /*
832          * Try to allocate the smallest needed region first.
833          * If that fails, fall back to the other region.
834          */
835         error = ENOSPC;
836         while (front != 0 || back != 0) {
837                 if (front != 0 && (front <= back || back == 0)) {
838                         error = bus_adjust_resource(sc->dev, type, w->res,
839                             rman_get_start(w->res) - front,
840                             rman_get_end(w->res));
841                         if (error == 0)
842                                 break;
843                         front = 0;
844                 } else {
845                         error = bus_adjust_resource(sc->dev, type, w->res,
846                             rman_get_start(w->res),
847                             rman_get_end(w->res) + back);
848                         if (error == 0)
849                                 break;
850                         back = 0;
851                 }
852         }
853
854         if (error)
855                 return (error);
856         if (bootverbose)
857                 device_printf(sc->dev, "grew %s window to %#lx-%#lx\n",
858                     w->name, rman_get_start(w->res), rman_get_end(w->res));
859
860         /* Add the newly allocated region to the resource manager. */
861         if (w->base != rman_get_start(w->res)) {
862                 KASSERT(w->limit == rman_get_end(w->res), ("both ends moved"));
863                 error = rman_manage_region(&w->rman, rman_get_start(w->res),
864                     w->base - 1);
865         } else {
866                 KASSERT(w->limit != rman_get_end(w->res),
867                     ("neither end moved"));
868                 error = rman_manage_region(&w->rman, w->limit + 1,
869                     rman_get_end(w->res));
870         }
871         if (error) {
872                 if (bootverbose)
873                         device_printf(sc->dev,
874                             "failed to expand %s resource manager\n", w->name);
875                 bus_adjust_resource(sc->dev, type, w->res, w->base, w->limit);
876                 return (error);
877         }
878
879 updatewin:
880         /* Save the new window. */
881         w->base = rman_get_start(w->res);
882         w->limit = rman_get_end(w->res);
883         KASSERT((w->base & ((1ul << w->step) - 1)) == 0,
884             ("start address is not aligned"));
885         KASSERT((w->limit & ((1ul << w->step) - 1)) == (1ul << w->step) - 1,
886             ("end address is not aligned"));
887         pcib_write_windows(sc, w->mask);
888         return (0);
889 }
890
891 /*
892  * We have to trap resource allocation requests and ensure that the bridge
893  * is set up to, or capable of handling them.
894  */
895 struct resource *
896 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
897     u_long start, u_long end, u_long count, u_int flags)
898 {
899         struct pcib_softc *sc;
900         struct resource *r;
901
902         sc = device_get_softc(dev);
903
904         /*
905          * VGA resources are decoded iff the VGA enable bit is set in
906          * the bridge control register.  VGA resources do not fall into
907          * the resource windows and are passed up to the parent.
908          */
909         if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
910             (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
911                 if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
912                         return (bus_generic_alloc_resource(dev, child, type,
913                             rid, start, end, count, flags));
914                 else
915                         return (NULL);
916         }
917
918         switch (type) {
919         case SYS_RES_IOPORT:
920                 r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
921                     end, count, flags);
922                 if (r != NULL)
923                         break;
924                 if (pcib_grow_window(sc, &sc->io, type, start, end, count,
925                     flags) == 0)
926                         r = pcib_suballoc_resource(sc, &sc->io, child, type,
927                             rid, start, end, count, flags);
928                 break;
929         case SYS_RES_MEMORY:
930                 /*
931                  * For prefetchable resources, prefer the prefetchable
932                  * memory window, but fall back to the regular memory
933                  * window if that fails.  Try both windows before
934                  * attempting to grow a window in case the firmware
935                  * has used a range in the regular memory window to
936                  * map a prefetchable BAR.
937                  */
938                 if (flags & RF_PREFETCHABLE) {
939                         r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
940                             rid, start, end, count, flags);
941                         if (r != NULL)
942                                 break;
943                 }
944                 r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
945                     start, end, count, flags);
946                 if (r != NULL)
947                         break;
948                 if (flags & RF_PREFETCHABLE) {
949                         if (pcib_grow_window(sc, &sc->pmem, type, start, end,
950                             count, flags) == 0) {
951                                 r = pcib_suballoc_resource(sc, &sc->pmem, child,
952                                     type, rid, start, end, count, flags);
953                                 if (r != NULL)
954                                         break;
955                         }
956                 }
957                 if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
958                     flags & ~RF_PREFETCHABLE) == 0)
959                         r = pcib_suballoc_resource(sc, &sc->mem, child, type,
960                             rid, start, end, count, flags);
961                 break;
962         default:
963                 return (bus_generic_alloc_resource(dev, child, type, rid,
964                     start, end, count, flags));
965         }
966
967         /*
968          * If attempts to suballocate from the window fail but this is a
969          * subtractive bridge, pass the request up the tree.
970          */
971         if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
972                 return (bus_generic_alloc_resource(dev, child, type, rid,
973                     start, end, count, flags));
974         return (r);
975 }
976
977 int
978 pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
979     u_long start, u_long end)
980 {
981         struct pcib_softc *sc;
982
983         sc = device_get_softc(bus);
984         if (pcib_is_resource_managed(sc, type, r))
985                 return (rman_adjust_resource(r, start, end));
986         return (bus_generic_adjust_resource(bus, child, type, r, start, end));
987 }
988
989 int
990 pcib_release_resource(device_t dev, device_t child, int type, int rid,
991     struct resource *r)
992 {
993         struct pcib_softc *sc;
994         int error;
995
996         sc = device_get_softc(dev);
997         if (pcib_is_resource_managed(sc, type, r)) {
998                 if (rman_get_flags(r) & RF_ACTIVE) {
999                         error = bus_deactivate_resource(child, type, rid, r);
1000                         if (error)
1001                                 return (error);
1002                 }
1003                 return (rman_release_resource(r));
1004         }
1005         return (bus_generic_release_resource(dev, child, type, rid, r));
1006 }
1007 #else
1008 /*
1009  * We have to trap resource allocation requests and ensure that the bridge
1010  * is set up to, or capable of handling them.
1011  */
1012 struct resource *
1013 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 
1014     u_long start, u_long end, u_long count, u_int flags)
1015 {
1016         struct pcib_softc       *sc = device_get_softc(dev);
1017         const char *name, *suffix;
1018         int ok;
1019
1020         /*
1021          * Fail the allocation for this range if it's not supported.
1022          */
1023         name = device_get_nameunit(child);
1024         if (name == NULL) {
1025                 name = "";
1026                 suffix = "";
1027         } else
1028                 suffix = " ";
1029         switch (type) {
1030         case SYS_RES_IOPORT:
1031                 ok = 0;
1032                 if (!pcib_is_io_open(sc))
1033                         break;
1034                 ok = (start >= sc->iobase && end <= sc->iolimit);
1035
1036                 /*
1037                  * Make sure we allow access to VGA I/O addresses when the
1038                  * bridge has the "VGA Enable" bit set.
1039                  */
1040                 if (!ok && pci_is_vga_ioport_range(start, end))
1041                         ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1042
1043                 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1044                         if (!ok) {
1045                                 if (start < sc->iobase)
1046                                         start = sc->iobase;
1047                                 if (end > sc->iolimit)
1048                                         end = sc->iolimit;
1049                                 if (start < end)
1050                                         ok = 1;
1051                         }
1052                 } else {
1053                         ok = 1;
1054 #if 0
1055                         /*
1056                          * If we overlap with the subtractive range, then
1057                          * pick the upper range to use.
1058                          */
1059                         if (start < sc->iolimit && end > sc->iobase)
1060                                 start = sc->iolimit + 1;
1061 #endif
1062                 }
1063                 if (end < start) {
1064                         device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
1065                             end, start);
1066                         start = 0;
1067                         end = 0;
1068                         ok = 0;
1069                 }
1070                 if (!ok) {
1071                         device_printf(dev, "%s%srequested unsupported I/O "
1072                             "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
1073                             name, suffix, start, end, sc->iobase, sc->iolimit);
1074                         return (NULL);
1075                 }
1076                 if (bootverbose)
1077                         device_printf(dev,
1078                             "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
1079                             name, suffix, start, end);
1080                 break;
1081
1082         case SYS_RES_MEMORY:
1083                 ok = 0;
1084                 if (pcib_is_nonprefetch_open(sc))
1085                         ok = ok || (start >= sc->membase && end <= sc->memlimit);
1086                 if (pcib_is_prefetch_open(sc))
1087                         ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1088
1089                 /*
1090                  * Make sure we allow access to VGA memory addresses when the
1091                  * bridge has the "VGA Enable" bit set.
1092                  */
1093                 if (!ok && pci_is_vga_memory_range(start, end))
1094                         ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1095
1096                 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1097                         if (!ok) {
1098                                 ok = 1;
1099                                 if (flags & RF_PREFETCHABLE) {
1100                                         if (pcib_is_prefetch_open(sc)) {
1101                                                 if (start < sc->pmembase)
1102                                                         start = sc->pmembase;
1103                                                 if (end > sc->pmemlimit)
1104                                                         end = sc->pmemlimit;
1105                                         } else {
1106                                                 ok = 0;
1107                                         }
1108                                 } else {        /* non-prefetchable */
1109                                         if (pcib_is_nonprefetch_open(sc)) {
1110                                                 if (start < sc->membase)
1111                                                         start = sc->membase;
1112                                                 if (end > sc->memlimit)
1113                                                         end = sc->memlimit;
1114                                         } else {
1115                                                 ok = 0;
1116                                         }
1117                                 }
1118                         }
1119                 } else if (!ok) {
1120                         ok = 1; /* subtractive bridge: always ok */
1121 #if 0
1122                         if (pcib_is_nonprefetch_open(sc)) {
1123                                 if (start < sc->memlimit && end > sc->membase)
1124                                         start = sc->memlimit + 1;
1125                         }
1126                         if (pcib_is_prefetch_open(sc)) {
1127                                 if (start < sc->pmemlimit && end > sc->pmembase)
1128                                         start = sc->pmemlimit + 1;
1129                         }
1130 #endif
1131                 }
1132                 if (end < start) {
1133                         device_printf(dev, "memory: end (%lx) < start (%lx)\n",
1134                             end, start);
1135                         start = 0;
1136                         end = 0;
1137                         ok = 0;
1138                 }
1139                 if (!ok && bootverbose)
1140                         device_printf(dev,
1141                             "%s%srequested unsupported memory range %#lx-%#lx "
1142                             "(decoding %#jx-%#jx, %#jx-%#jx)\n",
1143                             name, suffix, start, end,
1144                             (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1145                             (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1146                 if (!ok)
1147                         return (NULL);
1148                 if (bootverbose)
1149                         device_printf(dev,"%s%srequested memory range "
1150                             "0x%lx-0x%lx: good\n",
1151                             name, suffix, start, end);
1152                 break;
1153
1154         default:
1155                 break;
1156         }
1157         /*
1158          * Bridge is OK decoding this resource, so pass it up.
1159          */
1160         return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
1161             count, flags));
1162 }
1163 #endif
1164
1165 /*
1166  * PCIB interface.
1167  */
1168 int
1169 pcib_maxslots(device_t dev)
1170 {
1171     return(PCI_SLOTMAX);
1172 }
1173
1174 /*
1175  * Since we are a child of a PCI bus, its parent must support the pcib interface.
1176  */
1177 uint32_t
1178 pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
1179 {
1180     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
1181 }
1182
1183 void
1184 pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
1185 {
1186     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
1187 }
1188
1189 /*
1190  * Route an interrupt across a PCI bridge.
1191  */
1192 int
1193 pcib_route_interrupt(device_t pcib, device_t dev, int pin)
1194 {
1195     device_t    bus;
1196     int         parent_intpin;
1197     int         intnum;
1198
1199     /*  
1200      *
1201      * The PCI standard defines a swizzle of the child-side device/intpin to
1202      * the parent-side intpin as follows.
1203      *
1204      * device = device on child bus
1205      * child_intpin = intpin on child bus slot (0-3)
1206      * parent_intpin = intpin on parent bus slot (0-3)
1207      *
1208      * parent_intpin = (device + child_intpin) % 4
1209      */
1210     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
1211
1212     /*
1213      * Our parent is a PCI bus.  Its parent must export the pcib interface
1214      * which includes the ability to route interrupts.
1215      */
1216     bus = device_get_parent(pcib);
1217     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
1218     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1219         device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1220             pci_get_slot(dev), 'A' + pin - 1, intnum);
1221     }
1222     return(intnum);
1223 }
1224
1225 /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
1226 int
1227 pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
1228 {
1229         struct pcib_softc *sc = device_get_softc(pcib);
1230         device_t bus;
1231
1232         if (sc->flags & PCIB_DISABLE_MSI)
1233                 return (ENXIO);
1234         bus = device_get_parent(pcib);
1235         return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
1236             irqs));
1237 }
1238
1239 /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
1240 int
1241 pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
1242 {
1243         device_t bus;
1244
1245         bus = device_get_parent(pcib);
1246         return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
1247 }
1248
1249 /* Pass request to alloc an MSI-X message up to the parent bridge. */
1250 int
1251 pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
1252 {
1253         struct pcib_softc *sc = device_get_softc(pcib);
1254         device_t bus;
1255
1256         if (sc->flags & PCIB_DISABLE_MSI)
1257                 return (ENXIO);
1258         bus = device_get_parent(pcib);
1259         return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
1260 }
1261
1262 /* Pass request to release an MSI-X message up to the parent bridge. */
1263 int
1264 pcib_release_msix(device_t pcib, device_t dev, int irq)
1265 {
1266         device_t bus;
1267
1268         bus = device_get_parent(pcib);
1269         return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
1270 }
1271
1272 /* Pass request to map MSI/MSI-X message up to parent bridge. */
1273 int
1274 pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
1275     uint32_t *data)
1276 {
1277         device_t bus;
1278         int error;
1279
1280         bus = device_get_parent(pcib);
1281         error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
1282         if (error)
1283                 return (error);
1284
1285         pci_ht_map_msi(pcib, *addr);
1286         return (0);
1287 }
1288
1289 /*
1290  * Try to read the bus number of a host-PCI bridge using appropriate config
1291  * registers.
1292  */
1293 int
1294 host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
1295     uint8_t *busnum)
1296 {
1297         uint32_t id;
1298
1299         id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
1300         if (id == 0xffffffff)
1301                 return (0);
1302
1303         switch (id) {
1304         case 0x12258086:
1305                 /* Intel 824?? */
1306                 /* XXX This is a guess */
1307                 /* *busnum = read_config(bus, slot, func, 0x41, 1); */
1308                 *busnum = bus;
1309                 break;
1310         case 0x84c48086:
1311                 /* Intel 82454KX/GX (Orion) */
1312                 *busnum = read_config(bus, slot, func, 0x4a, 1);
1313                 break;
1314         case 0x84ca8086:
1315                 /*
1316                  * For the 450nx chipset, there is a whole bundle of
1317                  * things pretending to be host bridges. The MIOC will 
1318                  * be seen first and isn't really a pci bridge (the
1319                  * actual busses are attached to the PXB's). We need to 
1320                  * read the registers of the MIOC to figure out the
1321                  * bus numbers for the PXB channels.
1322                  *
1323                  * Since the MIOC doesn't have a pci bus attached, we
1324                  * pretend it wasn't there.
1325                  */
1326                 return (0);
1327         case 0x84cb8086:
1328                 switch (slot) {
1329                 case 0x12:
1330                         /* Intel 82454NX PXB#0, Bus#A */
1331                         *busnum = read_config(bus, 0x10, func, 0xd0, 1);
1332                         break;
1333                 case 0x13:
1334                         /* Intel 82454NX PXB#0, Bus#B */
1335                         *busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
1336                         break;
1337                 case 0x14:
1338                         /* Intel 82454NX PXB#1, Bus#A */
1339                         *busnum = read_config(bus, 0x10, func, 0xd3, 1);
1340                         break;
1341                 case 0x15:
1342                         /* Intel 82454NX PXB#1, Bus#B */
1343                         *busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
1344                         break;
1345                 }
1346                 break;
1347
1348                 /* ServerWorks -- vendor 0x1166 */
1349         case 0x00051166:
1350         case 0x00061166:
1351         case 0x00081166:
1352         case 0x00091166:
1353         case 0x00101166:
1354         case 0x00111166:
1355         case 0x00171166:
1356         case 0x01011166:
1357         case 0x010f1014:
1358         case 0x01101166:
1359         case 0x02011166:
1360         case 0x02251166:
1361         case 0x03021014:
1362                 *busnum = read_config(bus, slot, func, 0x44, 1);
1363                 break;
1364
1365                 /* Compaq/HP -- vendor 0x0e11 */
1366         case 0x60100e11:
1367                 *busnum = read_config(bus, slot, func, 0xc8, 1);
1368                 break;
1369         default:
1370                 /* Don't know how to read bus number. */
1371                 return 0;
1372         }
1373
1374         return 1;
1375 }