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