pci_pci.c revision 280970
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: stable/10/sys/dev/pci/pci_pci.c 280970 2015-04-01 21:48:54Z jhb $");
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
54static int		pcib_probe(device_t dev);
55static int		pcib_suspend(device_t dev);
56static int		pcib_resume(device_t dev);
57static int		pcib_power_for_sleep(device_t pcib, device_t dev,
58			    int *pstate);
59static uint16_t		pcib_ari_get_rid(device_t pcib, device_t dev);
60static uint32_t		pcib_read_config(device_t dev, u_int b, u_int s,
61    u_int f, u_int reg, int width);
62static void		pcib_write_config(device_t dev, u_int b, u_int s,
63    u_int f, u_int reg, uint32_t val, int width);
64static int		pcib_ari_maxslots(device_t dev);
65static int		pcib_ari_maxfuncs(device_t dev);
66static int		pcib_try_enable_ari(device_t pcib, device_t dev);
67
68static device_method_t pcib_methods[] = {
69    /* Device interface */
70    DEVMETHOD(device_probe,		pcib_probe),
71    DEVMETHOD(device_attach,		pcib_attach),
72    DEVMETHOD(device_detach,		bus_generic_detach),
73    DEVMETHOD(device_shutdown,		bus_generic_shutdown),
74    DEVMETHOD(device_suspend,		pcib_suspend),
75    DEVMETHOD(device_resume,		pcib_resume),
76
77    /* Bus interface */
78    DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
79    DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
80    DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
81#ifdef NEW_PCIB
82    DEVMETHOD(bus_adjust_resource,	pcib_adjust_resource),
83    DEVMETHOD(bus_release_resource,	pcib_release_resource),
84#else
85    DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
86    DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
87#endif
88    DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
89    DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
90    DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
91    DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
92
93    /* pcib interface */
94    DEVMETHOD(pcib_maxslots,		pcib_ari_maxslots),
95    DEVMETHOD(pcib_maxfuncs,		pcib_ari_maxfuncs),
96    DEVMETHOD(pcib_read_config,		pcib_read_config),
97    DEVMETHOD(pcib_write_config,	pcib_write_config),
98    DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
99    DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
100    DEVMETHOD(pcib_release_msi,		pcib_release_msi),
101    DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
102    DEVMETHOD(pcib_release_msix,	pcib_release_msix),
103    DEVMETHOD(pcib_map_msi,		pcib_map_msi),
104    DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
105    DEVMETHOD(pcib_get_rid,		pcib_ari_get_rid),
106    DEVMETHOD(pcib_try_enable_ari,	pcib_try_enable_ari),
107
108    DEVMETHOD_END
109};
110
111static devclass_t pcib_devclass;
112
113DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
114DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL);
115
116#ifdef NEW_PCIB
117SYSCTL_DECL(_hw_pci);
118
119static int pci_clear_pcib;
120TUNABLE_INT("hw.pci.clear_pcib", &pci_clear_pcib);
121SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0,
122    "Clear firmware-assigned resources for PCI-PCI bridge I/O windows.");
123
124/*
125 * Is a resource from a child device sub-allocated from one of our
126 * resource managers?
127 */
128static int
129pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
130{
131
132	switch (type) {
133#ifdef PCI_RES_BUS
134	case PCI_RES_BUS:
135		return (rman_is_region_manager(r, &sc->bus.rman));
136#endif
137	case SYS_RES_IOPORT:
138		return (rman_is_region_manager(r, &sc->io.rman));
139	case SYS_RES_MEMORY:
140		/* Prefetchable resources may live in either memory rman. */
141		if (rman_get_flags(r) & RF_PREFETCHABLE &&
142		    rman_is_region_manager(r, &sc->pmem.rman))
143			return (1);
144		return (rman_is_region_manager(r, &sc->mem.rman));
145	}
146	return (0);
147}
148
149static int
150pcib_is_window_open(struct pcib_window *pw)
151{
152
153	return (pw->valid && pw->base < pw->limit);
154}
155
156/*
157 * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
158 * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
159 * when allocating the resource windows and rely on the PCI bus driver
160 * to do this for us.
161 */
162static void
163pcib_activate_window(struct pcib_softc *sc, int type)
164{
165
166	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
167}
168
169static void
170pcib_write_windows(struct pcib_softc *sc, int mask)
171{
172	device_t dev;
173	uint32_t val;
174
175	dev = sc->dev;
176	if (sc->io.valid && mask & WIN_IO) {
177		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
178		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
179			pci_write_config(dev, PCIR_IOBASEH_1,
180			    sc->io.base >> 16, 2);
181			pci_write_config(dev, PCIR_IOLIMITH_1,
182			    sc->io.limit >> 16, 2);
183		}
184		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
185		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
186	}
187
188	if (mask & WIN_MEM) {
189		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
190		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
191	}
192
193	if (sc->pmem.valid && mask & WIN_PMEM) {
194		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
195		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
196			pci_write_config(dev, PCIR_PMBASEH_1,
197			    sc->pmem.base >> 32, 4);
198			pci_write_config(dev, PCIR_PMLIMITH_1,
199			    sc->pmem.limit >> 32, 4);
200		}
201		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
202		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
203	}
204}
205
206/*
207 * This is used to reject I/O port allocations that conflict with an
208 * ISA alias range.
209 */
210static int
211pcib_is_isa_range(struct pcib_softc *sc, u_long start, u_long end, u_long count)
212{
213	u_long next_alias;
214
215	if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
216		return (0);
217
218	/* Only check fixed ranges for overlap. */
219	if (start + count - 1 != end)
220		return (0);
221
222	/* ISA aliases are only in the lower 64KB of I/O space. */
223	if (start >= 65536)
224		return (0);
225
226	/* Check for overlap with 0x000 - 0x0ff as a special case. */
227	if (start < 0x100)
228		goto alias;
229
230	/*
231	 * If the start address is an alias, the range is an alias.
232	 * Otherwise, compute the start of the next alias range and
233	 * check if it is before the end of the candidate range.
234	 */
235	if ((start & 0x300) != 0)
236		goto alias;
237	next_alias = (start & ~0x3fful) | 0x100;
238	if (next_alias <= end)
239		goto alias;
240	return (0);
241
242alias:
243	if (bootverbose)
244		device_printf(sc->dev,
245		    "I/O range %#lx-%#lx overlaps with an ISA alias\n", start,
246		    end);
247	return (1);
248}
249
250static void
251pcib_add_window_resources(struct pcib_window *w, struct resource **res,
252    int count)
253{
254	struct resource **newarray;
255	int error, i;
256
257	newarray = malloc(sizeof(struct resource *) * (w->count + count),
258	    M_DEVBUF, M_WAITOK);
259	if (w->res != NULL)
260		bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
261	bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
262	free(w->res, M_DEVBUF);
263	w->res = newarray;
264	w->count += count;
265
266	for (i = 0; i < count; i++) {
267		error = rman_manage_region(&w->rman, rman_get_start(res[i]),
268		    rman_get_end(res[i]));
269		if (error)
270			panic("Failed to add resource to rman");
271	}
272}
273
274typedef void (nonisa_callback)(u_long start, u_long end, void *arg);
275
276static void
277pcib_walk_nonisa_ranges(u_long start, u_long end, nonisa_callback *cb,
278    void *arg)
279{
280	u_long next_end;
281
282	/*
283	 * If start is within an ISA alias range, move up to the start
284	 * of the next non-alias range.  As a special case, addresses
285	 * in the range 0x000 - 0x0ff should also be skipped since
286	 * those are used for various system I/O devices in ISA
287	 * systems.
288	 */
289	if (start <= 65535) {
290		if (start < 0x100 || (start & 0x300) != 0) {
291			start &= ~0x3ff;
292			start += 0x400;
293		}
294	}
295
296	/* ISA aliases are only in the lower 64KB of I/O space. */
297	while (start <= MIN(end, 65535)) {
298		next_end = MIN(start | 0xff, end);
299		cb(start, next_end, arg);
300		start += 0x400;
301	}
302
303	if (start <= end)
304		cb(start, end, arg);
305}
306
307static void
308count_ranges(u_long start, u_long end, void *arg)
309{
310	int *countp;
311
312	countp = arg;
313	(*countp)++;
314}
315
316struct alloc_state {
317	struct resource **res;
318	struct pcib_softc *sc;
319	int count, error;
320};
321
322static void
323alloc_ranges(u_long start, u_long end, void *arg)
324{
325	struct alloc_state *as;
326	struct pcib_window *w;
327	int rid;
328
329	as = arg;
330	if (as->error != 0)
331		return;
332
333	w = &as->sc->io;
334	rid = w->reg;
335	if (bootverbose)
336		device_printf(as->sc->dev,
337		    "allocating non-ISA range %#lx-%#lx\n", start, end);
338	as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
339	    &rid, start, end, end - start + 1, 0);
340	if (as->res[as->count] == NULL)
341		as->error = ENXIO;
342	else
343		as->count++;
344}
345
346static int
347pcib_alloc_nonisa_ranges(struct pcib_softc *sc, u_long start, u_long end)
348{
349	struct alloc_state as;
350	int i, new_count;
351
352	/* First, see how many ranges we need. */
353	new_count = 0;
354	pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
355
356	/* Second, allocate the ranges. */
357	as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
358	    M_WAITOK);
359	as.sc = sc;
360	as.count = 0;
361	as.error = 0;
362	pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
363	if (as.error != 0) {
364		for (i = 0; i < as.count; i++)
365			bus_release_resource(sc->dev, SYS_RES_IOPORT,
366			    sc->io.reg, as.res[i]);
367		free(as.res, M_DEVBUF);
368		return (as.error);
369	}
370	KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
371
372	/* Third, add the ranges to the window. */
373	pcib_add_window_resources(&sc->io, as.res, as.count);
374	free(as.res, M_DEVBUF);
375	return (0);
376}
377
378static void
379pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
380    int flags, pci_addr_t max_address)
381{
382	struct resource *res;
383	char buf[64];
384	int error, rid;
385
386	if (max_address != (u_long)max_address)
387		max_address = ~0ul;
388	w->rman.rm_start = 0;
389	w->rman.rm_end = max_address;
390	w->rman.rm_type = RMAN_ARRAY;
391	snprintf(buf, sizeof(buf), "%s %s window",
392	    device_get_nameunit(sc->dev), w->name);
393	w->rman.rm_descr = strdup(buf, M_DEVBUF);
394	error = rman_init(&w->rman);
395	if (error)
396		panic("Failed to initialize %s %s rman",
397		    device_get_nameunit(sc->dev), w->name);
398
399	if (!pcib_is_window_open(w))
400		return;
401
402	if (w->base > max_address || w->limit > max_address) {
403		device_printf(sc->dev,
404		    "initial %s window has too many bits, ignoring\n", w->name);
405		return;
406	}
407	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
408		(void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
409	else {
410		rid = w->reg;
411		res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
412		    w->limit - w->base + 1, flags);
413		if (res != NULL)
414			pcib_add_window_resources(w, &res, 1);
415	}
416	if (w->res == NULL) {
417		device_printf(sc->dev,
418		    "failed to allocate initial %s window: %#jx-%#jx\n",
419		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
420		w->base = max_address;
421		w->limit = 0;
422		pcib_write_windows(sc, w->mask);
423		return;
424	}
425	pcib_activate_window(sc, type);
426}
427
428/*
429 * Initialize I/O windows.
430 */
431static void
432pcib_probe_windows(struct pcib_softc *sc)
433{
434	pci_addr_t max;
435	device_t dev;
436	uint32_t val;
437
438	dev = sc->dev;
439
440	if (pci_clear_pcib) {
441		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
442		pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
443		pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
444		pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
445		pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
446		pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
447		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
448		pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
449		pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
450		pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
451	}
452
453	/* Determine if the I/O port window is implemented. */
454	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
455	if (val == 0) {
456		/*
457		 * If 'val' is zero, then only 16-bits of I/O space
458		 * are supported.
459		 */
460		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
461		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
462			sc->io.valid = 1;
463			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
464		}
465	} else
466		sc->io.valid = 1;
467
468	/* Read the existing I/O port window. */
469	if (sc->io.valid) {
470		sc->io.reg = PCIR_IOBASEL_1;
471		sc->io.step = 12;
472		sc->io.mask = WIN_IO;
473		sc->io.name = "I/O port";
474		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
475			sc->io.base = PCI_PPBIOBASE(
476			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
477			sc->io.limit = PCI_PPBIOLIMIT(
478			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
479			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
480			max = 0xffffffff;
481		} else {
482			sc->io.base = PCI_PPBIOBASE(0, val);
483			sc->io.limit = PCI_PPBIOLIMIT(0,
484			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
485			max = 0xffff;
486		}
487		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
488	}
489
490	/* Read the existing memory window. */
491	sc->mem.valid = 1;
492	sc->mem.reg = PCIR_MEMBASE_1;
493	sc->mem.step = 20;
494	sc->mem.mask = WIN_MEM;
495	sc->mem.name = "memory";
496	sc->mem.base = PCI_PPBMEMBASE(0,
497	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
498	sc->mem.limit = PCI_PPBMEMLIMIT(0,
499	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
500	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
501
502	/* Determine if the prefetchable memory window is implemented. */
503	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
504	if (val == 0) {
505		/*
506		 * If 'val' is zero, then only 32-bits of memory space
507		 * are supported.
508		 */
509		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
510		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
511			sc->pmem.valid = 1;
512			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
513		}
514	} else
515		sc->pmem.valid = 1;
516
517	/* Read the existing prefetchable memory window. */
518	if (sc->pmem.valid) {
519		sc->pmem.reg = PCIR_PMBASEL_1;
520		sc->pmem.step = 20;
521		sc->pmem.mask = WIN_PMEM;
522		sc->pmem.name = "prefetch";
523		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
524			sc->pmem.base = PCI_PPBMEMBASE(
525			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
526			sc->pmem.limit = PCI_PPBMEMLIMIT(
527			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
528			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
529			max = 0xffffffffffffffff;
530		} else {
531			sc->pmem.base = PCI_PPBMEMBASE(0, val);
532			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
533			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
534			max = 0xffffffff;
535		}
536		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
537		    RF_PREFETCHABLE, max);
538	}
539}
540
541#ifdef PCI_RES_BUS
542/*
543 * Allocate a suitable secondary bus for this bridge if needed and
544 * initialize the resource manager for the secondary bus range.  Note
545 * that the minimum count is a desired value and this may allocate a
546 * smaller range.
547 */
548void
549pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
550{
551	char buf[64];
552	int error, rid;
553
554	switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
555	case PCIM_HDRTYPE_BRIDGE:
556		bus->sub_reg = PCIR_SUBBUS_1;
557		break;
558	case PCIM_HDRTYPE_CARDBUS:
559		bus->sub_reg = PCIR_SUBBUS_2;
560		break;
561	default:
562		panic("not a PCI bridge");
563	}
564	bus->dev = dev;
565	bus->rman.rm_start = 0;
566	bus->rman.rm_end = PCI_BUSMAX;
567	bus->rman.rm_type = RMAN_ARRAY;
568	snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
569	bus->rman.rm_descr = strdup(buf, M_DEVBUF);
570	error = rman_init(&bus->rman);
571	if (error)
572		panic("Failed to initialize %s bus number rman",
573		    device_get_nameunit(dev));
574
575	/*
576	 * Allocate a bus range.  This will return an existing bus range
577	 * if one exists, or a new bus range if one does not.
578	 */
579	rid = 0;
580	bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul,
581	    min_count, 0);
582	if (bus->res == NULL) {
583		/*
584		 * Fall back to just allocating a range of a single bus
585		 * number.
586		 */
587		bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul,
588		    1, 0);
589	} else if (rman_get_size(bus->res) < min_count)
590		/*
591		 * Attempt to grow the existing range to satisfy the
592		 * minimum desired count.
593		 */
594		(void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
595		    rman_get_start(bus->res), rman_get_start(bus->res) +
596		    min_count - 1);
597
598	/*
599	 * Add the initial resource to the rman.
600	 */
601	if (bus->res != NULL) {
602		error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
603		    rman_get_end(bus->res));
604		if (error)
605			panic("Failed to add resource to rman");
606		bus->sec = rman_get_start(bus->res);
607		bus->sub = rman_get_end(bus->res);
608	}
609}
610
611static struct resource *
612pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
613    u_long start, u_long end, u_long count, u_int flags)
614{
615	struct resource *res;
616
617	res = rman_reserve_resource(&bus->rman, start, end, count, flags,
618	    child);
619	if (res == NULL)
620		return (NULL);
621
622	if (bootverbose)
623		device_printf(bus->dev,
624		    "allocated bus range (%lu-%lu) for rid %d of %s\n",
625		    rman_get_start(res), rman_get_end(res), *rid,
626		    pcib_child_name(child));
627	rman_set_rid(res, *rid);
628	return (res);
629}
630
631/*
632 * Attempt to grow the secondary bus range.  This is much simpler than
633 * for I/O windows as the range can only be grown by increasing
634 * subbus.
635 */
636static int
637pcib_grow_subbus(struct pcib_secbus *bus, u_long new_end)
638{
639	u_long old_end;
640	int error;
641
642	old_end = rman_get_end(bus->res);
643	KASSERT(new_end > old_end, ("attempt to shrink subbus"));
644	error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
645	    rman_get_start(bus->res), new_end);
646	if (error)
647		return (error);
648	if (bootverbose)
649		device_printf(bus->dev, "grew bus range to %lu-%lu\n",
650		    rman_get_start(bus->res), rman_get_end(bus->res));
651	error = rman_manage_region(&bus->rman, old_end + 1,
652	    rman_get_end(bus->res));
653	if (error)
654		panic("Failed to add resource to rman");
655	bus->sub = rman_get_end(bus->res);
656	pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
657	return (0);
658}
659
660struct resource *
661pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
662    u_long start, u_long end, u_long count, u_int flags)
663{
664	struct resource *res;
665	u_long start_free, end_free, new_end;
666
667	/*
668	 * First, see if the request can be satisified by the existing
669	 * bus range.
670	 */
671	res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
672	if (res != NULL)
673		return (res);
674
675	/*
676	 * Figure out a range to grow the bus range.  First, find the
677	 * first bus number after the last allocated bus in the rman and
678	 * enforce that as a minimum starting point for the range.
679	 */
680	if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
681	    end_free != bus->sub)
682		start_free = bus->sub + 1;
683	if (start_free < start)
684		start_free = start;
685	new_end = start_free + count - 1;
686
687	/*
688	 * See if this new range would satisfy the request if it
689	 * succeeds.
690	 */
691	if (new_end > end)
692		return (NULL);
693
694	/* Finally, attempt to grow the existing resource. */
695	if (bootverbose) {
696		device_printf(bus->dev,
697		    "attempting to grow bus range for %lu buses\n", count);
698		printf("\tback candidate range: %lu-%lu\n", start_free,
699		    new_end);
700	}
701	if (pcib_grow_subbus(bus, new_end) == 0)
702		return (pcib_suballoc_bus(bus, child, rid, start, end, count,
703		    flags));
704	return (NULL);
705}
706#endif
707
708#else
709
710/*
711 * Is the prefetch window open (eg, can we allocate memory in it?)
712 */
713static int
714pcib_is_prefetch_open(struct pcib_softc *sc)
715{
716	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
717}
718
719/*
720 * Is the nonprefetch window open (eg, can we allocate memory in it?)
721 */
722static int
723pcib_is_nonprefetch_open(struct pcib_softc *sc)
724{
725	return (sc->membase > 0 && sc->membase < sc->memlimit);
726}
727
728/*
729 * Is the io window open (eg, can we allocate ports in it?)
730 */
731static int
732pcib_is_io_open(struct pcib_softc *sc)
733{
734	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
735}
736
737/*
738 * Get current I/O decode.
739 */
740static void
741pcib_get_io_decode(struct pcib_softc *sc)
742{
743	device_t	dev;
744	uint32_t	iolow;
745
746	dev = sc->dev;
747
748	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
749	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
750		sc->iobase = PCI_PPBIOBASE(
751		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
752	else
753		sc->iobase = PCI_PPBIOBASE(0, iolow);
754
755	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
756	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
757		sc->iolimit = PCI_PPBIOLIMIT(
758		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
759	else
760		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
761}
762
763/*
764 * Get current memory decode.
765 */
766static void
767pcib_get_mem_decode(struct pcib_softc *sc)
768{
769	device_t	dev;
770	pci_addr_t	pmemlow;
771
772	dev = sc->dev;
773
774	sc->membase = PCI_PPBMEMBASE(0,
775	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
776	sc->memlimit = PCI_PPBMEMLIMIT(0,
777	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
778
779	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
780	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
781		sc->pmembase = PCI_PPBMEMBASE(
782		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
783	else
784		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
785
786	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
787	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
788		sc->pmemlimit = PCI_PPBMEMLIMIT(
789		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
790	else
791		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
792}
793
794/*
795 * Restore previous I/O decode.
796 */
797static void
798pcib_set_io_decode(struct pcib_softc *sc)
799{
800	device_t	dev;
801	uint32_t	iohi;
802
803	dev = sc->dev;
804
805	iohi = sc->iobase >> 16;
806	if (iohi > 0)
807		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
808	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
809
810	iohi = sc->iolimit >> 16;
811	if (iohi > 0)
812		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
813	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
814}
815
816/*
817 * Restore previous memory decode.
818 */
819static void
820pcib_set_mem_decode(struct pcib_softc *sc)
821{
822	device_t	dev;
823	pci_addr_t	pmemhi;
824
825	dev = sc->dev;
826
827	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
828	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
829
830	pmemhi = sc->pmembase >> 32;
831	if (pmemhi > 0)
832		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
833	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
834
835	pmemhi = sc->pmemlimit >> 32;
836	if (pmemhi > 0)
837		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
838	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
839}
840#endif
841
842/*
843 * Get current bridge configuration.
844 */
845static void
846pcib_cfg_save(struct pcib_softc *sc)
847{
848	device_t	dev;
849
850	dev = sc->dev;
851
852	sc->command = pci_read_config(dev, PCIR_COMMAND, 2);
853	sc->pribus = pci_read_config(dev, PCIR_PRIBUS_1, 1);
854	sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1);
855	sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
856	sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
857	sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1);
858#ifndef NEW_PCIB
859	if (sc->command & PCIM_CMD_PORTEN)
860		pcib_get_io_decode(sc);
861	if (sc->command & PCIM_CMD_MEMEN)
862		pcib_get_mem_decode(sc);
863#endif
864}
865
866/*
867 * Restore previous bridge configuration.
868 */
869static void
870pcib_cfg_restore(struct pcib_softc *sc)
871{
872	device_t	dev;
873
874	dev = sc->dev;
875
876	pci_write_config(dev, PCIR_COMMAND, sc->command, 2);
877	pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
878	pci_write_config(dev, PCIR_SECBUS_1, sc->bus.sec, 1);
879	pci_write_config(dev, PCIR_SUBBUS_1, sc->bus.sub, 1);
880	pci_write_config(dev, PCIR_BRIDGECTL_1, sc->bridgectl, 2);
881	pci_write_config(dev, PCIR_SECLAT_1, sc->seclat, 1);
882#ifdef NEW_PCIB
883	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
884#else
885	if (sc->command & PCIM_CMD_PORTEN)
886		pcib_set_io_decode(sc);
887	if (sc->command & PCIM_CMD_MEMEN)
888		pcib_set_mem_decode(sc);
889#endif
890}
891
892/*
893 * Generic device interface
894 */
895static int
896pcib_probe(device_t dev)
897{
898    if ((pci_get_class(dev) == PCIC_BRIDGE) &&
899	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
900	device_set_desc(dev, "PCI-PCI bridge");
901	return(-10000);
902    }
903    return(ENXIO);
904}
905
906void
907pcib_attach_common(device_t dev)
908{
909    struct pcib_softc	*sc;
910    struct sysctl_ctx_list *sctx;
911    struct sysctl_oid	*soid;
912    int comma;
913
914    sc = device_get_softc(dev);
915    sc->dev = dev;
916
917    /*
918     * Get current bridge configuration.
919     */
920    sc->domain = pci_get_domain(dev);
921    sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2);
922    pcib_cfg_save(sc);
923
924    /*
925     * The primary bus register should always be the bus of the
926     * parent.
927     */
928    sc->pribus = pci_get_bus(dev);
929    pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
930
931    /*
932     * Setup sysctl reporting nodes
933     */
934    sctx = device_get_sysctl_ctx(dev);
935    soid = device_get_sysctl_tree(dev);
936    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
937      CTLFLAG_RD, &sc->domain, 0, "Domain number");
938    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
939      CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
940    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
941      CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
942    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
943      CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
944
945    /*
946     * Quirk handling.
947     */
948    switch (pci_get_devid(dev)) {
949#if !defined(NEW_PCIB) && !defined(PCI_RES_BUS)
950    case 0x12258086:		/* Intel 82454KX/GX (Orion) */
951	{
952	    uint8_t	supbus;
953
954	    supbus = pci_read_config(dev, 0x41, 1);
955	    if (supbus != 0xff) {
956		sc->bus.sec = supbus + 1;
957		sc->bus.sub = supbus + 1;
958	    }
959	    break;
960	}
961#endif
962
963    /*
964     * The i82380FB mobile docking controller is a PCI-PCI bridge,
965     * and it is a subtractive bridge.  However, the ProgIf is wrong
966     * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
967     * happen.  There's also a Toshiba bridge that behaves this
968     * way.
969     */
970    case 0x124b8086:		/* Intel 82380FB Mobile */
971    case 0x060513d7:		/* Toshiba ???? */
972	sc->flags |= PCIB_SUBTRACTIVE;
973	break;
974
975#if !defined(NEW_PCIB) && !defined(PCI_RES_BUS)
976    /* Compaq R3000 BIOS sets wrong subordinate bus number. */
977    case 0x00dd10de:
978	{
979	    char *cp;
980
981	    if ((cp = getenv("smbios.planar.maker")) == NULL)
982		break;
983	    if (strncmp(cp, "Compal", 6) != 0) {
984		freeenv(cp);
985		break;
986	    }
987	    freeenv(cp);
988	    if ((cp = getenv("smbios.planar.product")) == NULL)
989		break;
990	    if (strncmp(cp, "08A0", 4) != 0) {
991		freeenv(cp);
992		break;
993	    }
994	    freeenv(cp);
995	    if (sc->bus.sub < 0xa) {
996		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
997		sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
998	    }
999	    break;
1000	}
1001#endif
1002    }
1003
1004    if (pci_msi_device_blacklisted(dev))
1005	sc->flags |= PCIB_DISABLE_MSI;
1006
1007    if (pci_msix_device_blacklisted(dev))
1008	sc->flags |= PCIB_DISABLE_MSIX;
1009
1010    /*
1011     * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1012     * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
1013     * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1014     * This means they act as if they were subtractively decoding
1015     * bridges and pass all transactions.  Mark them and real ProgIf 1
1016     * parts as subtractive.
1017     */
1018    if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1019      pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1020	sc->flags |= PCIB_SUBTRACTIVE;
1021
1022#ifdef NEW_PCIB
1023#ifdef PCI_RES_BUS
1024    pcib_setup_secbus(dev, &sc->bus, 1);
1025#endif
1026    pcib_probe_windows(sc);
1027#endif
1028    if (bootverbose) {
1029	device_printf(dev, "  domain            %d\n", sc->domain);
1030	device_printf(dev, "  secondary bus     %d\n", sc->bus.sec);
1031	device_printf(dev, "  subordinate bus   %d\n", sc->bus.sub);
1032#ifdef NEW_PCIB
1033	if (pcib_is_window_open(&sc->io))
1034	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
1035	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
1036	if (pcib_is_window_open(&sc->mem))
1037	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
1038	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
1039	if (pcib_is_window_open(&sc->pmem))
1040	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
1041	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
1042#else
1043	if (pcib_is_io_open(sc))
1044	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
1045	      sc->iobase, sc->iolimit);
1046	if (pcib_is_nonprefetch_open(sc))
1047	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
1048	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
1049	if (pcib_is_prefetch_open(sc))
1050	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
1051	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1052#endif
1053	if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1054	    sc->flags & PCIB_SUBTRACTIVE) {
1055		device_printf(dev, "  special decode    ");
1056		comma = 0;
1057		if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1058			printf("ISA");
1059			comma = 1;
1060		}
1061		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1062			printf("%sVGA", comma ? ", " : "");
1063			comma = 1;
1064		}
1065		if (sc->flags & PCIB_SUBTRACTIVE)
1066			printf("%ssubtractive", comma ? ", " : "");
1067		printf("\n");
1068	}
1069    }
1070
1071    /*
1072     * Always enable busmastering on bridges so that transactions
1073     * initiated on the secondary bus are passed through to the
1074     * primary bus.
1075     */
1076    pci_enable_busmaster(dev);
1077}
1078
1079int
1080pcib_attach(device_t dev)
1081{
1082    struct pcib_softc	*sc;
1083    device_t		child;
1084
1085    pcib_attach_common(dev);
1086    sc = device_get_softc(dev);
1087    if (sc->bus.sec != 0) {
1088	child = device_add_child(dev, "pci", sc->bus.sec);
1089	if (child != NULL)
1090	    return(bus_generic_attach(dev));
1091    }
1092
1093    /* no secondary bus; we should have fixed this */
1094    return(0);
1095}
1096
1097int
1098pcib_suspend(device_t dev)
1099{
1100	device_t	pcib;
1101	int		dstate, error;
1102
1103	pcib_cfg_save(device_get_softc(dev));
1104	error = bus_generic_suspend(dev);
1105	if (error == 0 && pci_do_power_suspend) {
1106		dstate = PCI_POWERSTATE_D3;
1107		pcib = device_get_parent(device_get_parent(dev));
1108		if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0)
1109			pci_set_powerstate(dev, dstate);
1110	}
1111	return (error);
1112}
1113
1114int
1115pcib_resume(device_t dev)
1116{
1117	device_t	pcib;
1118	int dstate;
1119
1120	if (pci_do_power_resume) {
1121		pcib = device_get_parent(device_get_parent(dev));
1122		dstate = PCI_POWERSTATE_D0;
1123		if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0)
1124			pci_set_powerstate(dev, dstate);
1125	}
1126	pcib_cfg_restore(device_get_softc(dev));
1127	return (bus_generic_resume(dev));
1128}
1129
1130int
1131pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1132{
1133    struct pcib_softc	*sc = device_get_softc(dev);
1134
1135    switch (which) {
1136    case PCIB_IVAR_DOMAIN:
1137	*result = sc->domain;
1138	return(0);
1139    case PCIB_IVAR_BUS:
1140	*result = sc->bus.sec;
1141	return(0);
1142    }
1143    return(ENOENT);
1144}
1145
1146int
1147pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1148{
1149
1150    switch (which) {
1151    case PCIB_IVAR_DOMAIN:
1152	return(EINVAL);
1153    case PCIB_IVAR_BUS:
1154	return(EINVAL);
1155    }
1156    return(ENOENT);
1157}
1158
1159#ifdef NEW_PCIB
1160/*
1161 * Attempt to allocate a resource from the existing resources assigned
1162 * to a window.
1163 */
1164static struct resource *
1165pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
1166    device_t child, int type, int *rid, u_long start, u_long end, u_long count,
1167    u_int flags)
1168{
1169	struct resource *res;
1170
1171	if (!pcib_is_window_open(w))
1172		return (NULL);
1173
1174	res = rman_reserve_resource(&w->rman, start, end, count,
1175	    flags & ~RF_ACTIVE, child);
1176	if (res == NULL)
1177		return (NULL);
1178
1179	if (bootverbose)
1180		device_printf(sc->dev,
1181		    "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
1182		    w->name, rman_get_start(res), rman_get_end(res), *rid,
1183		    pcib_child_name(child));
1184	rman_set_rid(res, *rid);
1185
1186	/*
1187	 * If the resource should be active, pass that request up the
1188	 * tree.  This assumes the parent drivers can handle
1189	 * activating sub-allocated resources.
1190	 */
1191	if (flags & RF_ACTIVE) {
1192		if (bus_activate_resource(child, type, *rid, res) != 0) {
1193			rman_release_resource(res);
1194			return (NULL);
1195		}
1196	}
1197
1198	return (res);
1199}
1200
1201/* Allocate a fresh resource range for an unconfigured window. */
1202static int
1203pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1204    u_long start, u_long end, u_long count, u_int flags)
1205{
1206	struct resource *res;
1207	u_long base, limit, wmask;
1208	int rid;
1209
1210	/*
1211	 * If this is an I/O window on a bridge with ISA enable set
1212	 * and the start address is below 64k, then try to allocate an
1213	 * initial window of 0x1000 bytes long starting at address
1214	 * 0xf000 and walking down.  Note that if the original request
1215	 * was larger than the non-aliased range size of 0x100 our
1216	 * caller would have raised the start address up to 64k
1217	 * already.
1218	 */
1219	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1220	    start < 65536) {
1221		for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1222			limit = base + 0xfff;
1223
1224			/*
1225			 * Skip ranges that wouldn't work for the
1226			 * original request.  Note that the actual
1227			 * window that overlaps are the non-alias
1228			 * ranges within [base, limit], so this isn't
1229			 * quite a simple comparison.
1230			 */
1231			if (start + count > limit - 0x400)
1232				continue;
1233			if (base == 0) {
1234				/*
1235				 * The first open region for the window at
1236				 * 0 is 0x400-0x4ff.
1237				 */
1238				if (end - count + 1 < 0x400)
1239					continue;
1240			} else {
1241				if (end - count + 1 < base)
1242					continue;
1243			}
1244
1245			if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1246				w->base = base;
1247				w->limit = limit;
1248				return (0);
1249			}
1250		}
1251		return (ENOSPC);
1252	}
1253
1254	wmask = (1ul << w->step) - 1;
1255	if (RF_ALIGNMENT(flags) < w->step) {
1256		flags &= ~RF_ALIGNMENT_MASK;
1257		flags |= RF_ALIGNMENT_LOG2(w->step);
1258	}
1259	start &= ~wmask;
1260	end |= wmask;
1261	count = roundup2(count, 1ul << w->step);
1262	rid = w->reg;
1263	res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1264	    flags & ~RF_ACTIVE);
1265	if (res == NULL)
1266		return (ENOSPC);
1267	pcib_add_window_resources(w, &res, 1);
1268	pcib_activate_window(sc, type);
1269	w->base = rman_get_start(res);
1270	w->limit = rman_get_end(res);
1271	return (0);
1272}
1273
1274/* Try to expand an existing window to the requested base and limit. */
1275static int
1276pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1277    u_long base, u_long limit)
1278{
1279	struct resource *res;
1280	int error, i, force_64k_base;
1281
1282	KASSERT(base <= w->base && limit >= w->limit,
1283	    ("attempting to shrink window"));
1284
1285	/*
1286	 * XXX: pcib_grow_window() doesn't try to do this anyway and
1287	 * the error handling for all the edge cases would be tedious.
1288	 */
1289	KASSERT(limit == w->limit || base == w->base,
1290	    ("attempting to grow both ends of a window"));
1291
1292	/*
1293	 * Yet more special handling for requests to expand an I/O
1294	 * window behind an ISA-enabled bridge.  Since I/O windows
1295	 * have to grow in 0x1000 increments and the end of the 0xffff
1296	 * range is an alias, growing a window below 64k will always
1297	 * result in allocating new resources and never adjusting an
1298	 * existing resource.
1299	 */
1300	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1301	    (limit <= 65535 || (base <= 65535 && base != w->base))) {
1302		KASSERT(limit == w->limit || limit <= 65535,
1303		    ("attempting to grow both ends across 64k ISA alias"));
1304
1305		if (base != w->base)
1306			error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
1307		else
1308			error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
1309			    limit);
1310		if (error == 0) {
1311			w->base = base;
1312			w->limit = limit;
1313		}
1314		return (error);
1315	}
1316
1317	/*
1318	 * Find the existing resource to adjust.  Usually there is only one,
1319	 * but for an ISA-enabled bridge we might be growing the I/O window
1320	 * above 64k and need to find the existing resource that maps all
1321	 * of the area above 64k.
1322	 */
1323	for (i = 0; i < w->count; i++) {
1324		if (rman_get_end(w->res[i]) == w->limit)
1325			break;
1326	}
1327	KASSERT(i != w->count, ("did not find existing resource"));
1328	res = w->res[i];
1329
1330	/*
1331	 * Usually the resource we found should match the window's
1332	 * existing range.  The one exception is the ISA-enabled case
1333	 * mentioned above in which case the resource should start at
1334	 * 64k.
1335	 */
1336	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1337	    w->base <= 65535) {
1338		KASSERT(rman_get_start(res) == 65536,
1339		    ("existing resource mismatch"));
1340		force_64k_base = 1;
1341	} else {
1342		KASSERT(w->base == rman_get_start(res),
1343		    ("existing resource mismatch"));
1344		force_64k_base = 0;
1345	}
1346
1347	error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1348	    rman_get_start(res) : base, limit);
1349	if (error)
1350		return (error);
1351
1352	/* Add the newly allocated region to the resource manager. */
1353	if (w->base != base) {
1354		error = rman_manage_region(&w->rman, base, w->base - 1);
1355		w->base = base;
1356	} else {
1357		error = rman_manage_region(&w->rman, w->limit + 1, limit);
1358		w->limit = limit;
1359	}
1360	if (error) {
1361		if (bootverbose)
1362			device_printf(sc->dev,
1363			    "failed to expand %s resource manager\n", w->name);
1364		(void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1365		    rman_get_start(res) : w->base, w->limit);
1366	}
1367	return (error);
1368}
1369
1370/*
1371 * Attempt to grow a window to make room for a given resource request.
1372 */
1373static int
1374pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1375    u_long start, u_long end, u_long count, u_int flags)
1376{
1377	u_long align, start_free, end_free, front, back, wmask;
1378	int error;
1379
1380	/*
1381	 * Clamp the desired resource range to the maximum address
1382	 * this window supports.  Reject impossible requests.
1383	 *
1384	 * For I/O port requests behind a bridge with the ISA enable
1385	 * bit set, force large allocations to start above 64k.
1386	 */
1387	if (!w->valid)
1388		return (EINVAL);
1389	if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
1390	    start < 65536)
1391		start = 65536;
1392	if (end > w->rman.rm_end)
1393		end = w->rman.rm_end;
1394	if (start + count - 1 > end || start + count < start)
1395		return (EINVAL);
1396	wmask = (1ul << w->step) - 1;
1397
1398	/*
1399	 * If there is no resource at all, just try to allocate enough
1400	 * aligned space for this resource.
1401	 */
1402	if (w->res == NULL) {
1403		error = pcib_alloc_new_window(sc, w, type, start, end, count,
1404		    flags);
1405		if (error) {
1406			if (bootverbose)
1407				device_printf(sc->dev,
1408		    "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
1409				    w->name, start, end, count);
1410			return (error);
1411		}
1412		if (bootverbose)
1413			device_printf(sc->dev,
1414			    "allocated initial %s window of %#jx-%#jx\n",
1415			    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
1416		goto updatewin;
1417	}
1418
1419	/*
1420	 * See if growing the window would help.  Compute the minimum
1421	 * amount of address space needed on both the front and back
1422	 * ends of the existing window to satisfy the allocation.
1423	 *
1424	 * For each end, build a candidate region adjusting for the
1425	 * required alignment, etc.  If there is a free region at the
1426	 * edge of the window, grow from the inner edge of the free
1427	 * region.  Otherwise grow from the window boundary.
1428	 *
1429	 * Growing an I/O window below 64k for a bridge with the ISA
1430	 * enable bit doesn't require any special magic as the step
1431	 * size of an I/O window (1k) always includes multiple
1432	 * non-alias ranges when it is grown in either direction.
1433	 *
1434	 * XXX: Special case: if w->res is completely empty and the
1435	 * request size is larger than w->res, we should find the
1436	 * optimal aligned buffer containing w->res and allocate that.
1437	 */
1438	if (bootverbose)
1439		device_printf(sc->dev,
1440		    "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
1441		    w->name, start, end, count);
1442	align = 1ul << RF_ALIGNMENT(flags);
1443	if (start < w->base) {
1444		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
1445		    0 || start_free != w->base)
1446			end_free = w->base;
1447		if (end_free > end)
1448			end_free = end + 1;
1449
1450		/* Move end_free down until it is properly aligned. */
1451		end_free &= ~(align - 1);
1452		end_free--;
1453		front = end_free - (count - 1);
1454
1455		/*
1456		 * The resource would now be allocated at (front,
1457		 * end_free).  Ensure that fits in the (start, end)
1458		 * bounds.  end_free is checked above.  If 'front' is
1459		 * ok, ensure it is properly aligned for this window.
1460		 * Also check for underflow.
1461		 */
1462		if (front >= start && front <= end_free) {
1463			if (bootverbose)
1464				printf("\tfront candidate range: %#lx-%#lx\n",
1465				    front, end_free);
1466			front &= ~wmask;
1467			front = w->base - front;
1468		} else
1469			front = 0;
1470	} else
1471		front = 0;
1472	if (end > w->limit) {
1473		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
1474		    0 || end_free != w->limit)
1475			start_free = w->limit + 1;
1476		if (start_free < start)
1477			start_free = start;
1478
1479		/* Move start_free up until it is properly aligned. */
1480		start_free = roundup2(start_free, align);
1481		back = start_free + count - 1;
1482
1483		/*
1484		 * The resource would now be allocated at (start_free,
1485		 * back).  Ensure that fits in the (start, end)
1486		 * bounds.  start_free is checked above.  If 'back' is
1487		 * ok, ensure it is properly aligned for this window.
1488		 * Also check for overflow.
1489		 */
1490		if (back <= end && start_free <= back) {
1491			if (bootverbose)
1492				printf("\tback candidate range: %#lx-%#lx\n",
1493				    start_free, back);
1494			back |= wmask;
1495			back -= w->limit;
1496		} else
1497			back = 0;
1498	} else
1499		back = 0;
1500
1501	/*
1502	 * Try to allocate the smallest needed region first.
1503	 * If that fails, fall back to the other region.
1504	 */
1505	error = ENOSPC;
1506	while (front != 0 || back != 0) {
1507		if (front != 0 && (front <= back || back == 0)) {
1508			error = pcib_expand_window(sc, w, type, w->base - front,
1509			    w->limit);
1510			if (error == 0)
1511				break;
1512			front = 0;
1513		} else {
1514			error = pcib_expand_window(sc, w, type, w->base,
1515			    w->limit + back);
1516			if (error == 0)
1517				break;
1518			back = 0;
1519		}
1520	}
1521
1522	if (error)
1523		return (error);
1524	if (bootverbose)
1525		device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
1526		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
1527
1528updatewin:
1529	/* Write the new window. */
1530	KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
1531	KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
1532	pcib_write_windows(sc, w->mask);
1533	return (0);
1534}
1535
1536/*
1537 * We have to trap resource allocation requests and ensure that the bridge
1538 * is set up to, or capable of handling them.
1539 */
1540struct resource *
1541pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1542    u_long start, u_long end, u_long count, u_int flags)
1543{
1544	struct pcib_softc *sc;
1545	struct resource *r;
1546
1547	sc = device_get_softc(dev);
1548
1549	/*
1550	 * VGA resources are decoded iff the VGA enable bit is set in
1551	 * the bridge control register.  VGA resources do not fall into
1552	 * the resource windows and are passed up to the parent.
1553	 */
1554	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
1555	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
1556		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
1557			return (bus_generic_alloc_resource(dev, child, type,
1558			    rid, start, end, count, flags));
1559		else
1560			return (NULL);
1561	}
1562
1563	switch (type) {
1564#ifdef PCI_RES_BUS
1565	case PCI_RES_BUS:
1566		return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
1567		    count, flags));
1568#endif
1569	case SYS_RES_IOPORT:
1570		if (pcib_is_isa_range(sc, start, end, count))
1571			return (NULL);
1572		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
1573		    end, count, flags);
1574		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
1575			break;
1576		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
1577		    flags) == 0)
1578			r = pcib_suballoc_resource(sc, &sc->io, child, type,
1579			    rid, start, end, count, flags);
1580		break;
1581	case SYS_RES_MEMORY:
1582		/*
1583		 * For prefetchable resources, prefer the prefetchable
1584		 * memory window, but fall back to the regular memory
1585		 * window if that fails.  Try both windows before
1586		 * attempting to grow a window in case the firmware
1587		 * has used a range in the regular memory window to
1588		 * map a prefetchable BAR.
1589		 */
1590		if (flags & RF_PREFETCHABLE) {
1591			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
1592			    rid, start, end, count, flags);
1593			if (r != NULL)
1594				break;
1595		}
1596		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
1597		    start, end, count, flags);
1598		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
1599			break;
1600		if (flags & RF_PREFETCHABLE) {
1601			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
1602			    count, flags) == 0) {
1603				r = pcib_suballoc_resource(sc, &sc->pmem, child,
1604				    type, rid, start, end, count, flags);
1605				if (r != NULL)
1606					break;
1607			}
1608		}
1609		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
1610		    flags & ~RF_PREFETCHABLE) == 0)
1611			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
1612			    rid, start, end, count, flags);
1613		break;
1614	default:
1615		return (bus_generic_alloc_resource(dev, child, type, rid,
1616		    start, end, count, flags));
1617	}
1618
1619	/*
1620	 * If attempts to suballocate from the window fail but this is a
1621	 * subtractive bridge, pass the request up the tree.
1622	 */
1623	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
1624		return (bus_generic_alloc_resource(dev, child, type, rid,
1625		    start, end, count, flags));
1626	return (r);
1627}
1628
1629int
1630pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
1631    u_long start, u_long end)
1632{
1633	struct pcib_softc *sc;
1634
1635	sc = device_get_softc(bus);
1636	if (pcib_is_resource_managed(sc, type, r))
1637		return (rman_adjust_resource(r, start, end));
1638	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
1639}
1640
1641int
1642pcib_release_resource(device_t dev, device_t child, int type, int rid,
1643    struct resource *r)
1644{
1645	struct pcib_softc *sc;
1646	int error;
1647
1648	sc = device_get_softc(dev);
1649	if (pcib_is_resource_managed(sc, type, r)) {
1650		if (rman_get_flags(r) & RF_ACTIVE) {
1651			error = bus_deactivate_resource(child, type, rid, r);
1652			if (error)
1653				return (error);
1654		}
1655		return (rman_release_resource(r));
1656	}
1657	return (bus_generic_release_resource(dev, child, type, rid, r));
1658}
1659#else
1660/*
1661 * We have to trap resource allocation requests and ensure that the bridge
1662 * is set up to, or capable of handling them.
1663 */
1664struct resource *
1665pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1666    u_long start, u_long end, u_long count, u_int flags)
1667{
1668	struct pcib_softc	*sc = device_get_softc(dev);
1669	const char *name, *suffix;
1670	int ok;
1671
1672	/*
1673	 * Fail the allocation for this range if it's not supported.
1674	 */
1675	name = device_get_nameunit(child);
1676	if (name == NULL) {
1677		name = "";
1678		suffix = "";
1679	} else
1680		suffix = " ";
1681	switch (type) {
1682	case SYS_RES_IOPORT:
1683		ok = 0;
1684		if (!pcib_is_io_open(sc))
1685			break;
1686		ok = (start >= sc->iobase && end <= sc->iolimit);
1687
1688		/*
1689		 * Make sure we allow access to VGA I/O addresses when the
1690		 * bridge has the "VGA Enable" bit set.
1691		 */
1692		if (!ok && pci_is_vga_ioport_range(start, end))
1693			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1694
1695		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1696			if (!ok) {
1697				if (start < sc->iobase)
1698					start = sc->iobase;
1699				if (end > sc->iolimit)
1700					end = sc->iolimit;
1701				if (start < end)
1702					ok = 1;
1703			}
1704		} else {
1705			ok = 1;
1706#if 0
1707			/*
1708			 * If we overlap with the subtractive range, then
1709			 * pick the upper range to use.
1710			 */
1711			if (start < sc->iolimit && end > sc->iobase)
1712				start = sc->iolimit + 1;
1713#endif
1714		}
1715		if (end < start) {
1716			device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
1717			    end, start);
1718			start = 0;
1719			end = 0;
1720			ok = 0;
1721		}
1722		if (!ok) {
1723			device_printf(dev, "%s%srequested unsupported I/O "
1724			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
1725			    name, suffix, start, end, sc->iobase, sc->iolimit);
1726			return (NULL);
1727		}
1728		if (bootverbose)
1729			device_printf(dev,
1730			    "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
1731			    name, suffix, start, end);
1732		break;
1733
1734	case SYS_RES_MEMORY:
1735		ok = 0;
1736		if (pcib_is_nonprefetch_open(sc))
1737			ok = ok || (start >= sc->membase && end <= sc->memlimit);
1738		if (pcib_is_prefetch_open(sc))
1739			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1740
1741		/*
1742		 * Make sure we allow access to VGA memory addresses when the
1743		 * bridge has the "VGA Enable" bit set.
1744		 */
1745		if (!ok && pci_is_vga_memory_range(start, end))
1746			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1747
1748		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1749			if (!ok) {
1750				ok = 1;
1751				if (flags & RF_PREFETCHABLE) {
1752					if (pcib_is_prefetch_open(sc)) {
1753						if (start < sc->pmembase)
1754							start = sc->pmembase;
1755						if (end > sc->pmemlimit)
1756							end = sc->pmemlimit;
1757					} else {
1758						ok = 0;
1759					}
1760				} else {	/* non-prefetchable */
1761					if (pcib_is_nonprefetch_open(sc)) {
1762						if (start < sc->membase)
1763							start = sc->membase;
1764						if (end > sc->memlimit)
1765							end = sc->memlimit;
1766					} else {
1767						ok = 0;
1768					}
1769				}
1770			}
1771		} else if (!ok) {
1772			ok = 1;	/* subtractive bridge: always ok */
1773#if 0
1774			if (pcib_is_nonprefetch_open(sc)) {
1775				if (start < sc->memlimit && end > sc->membase)
1776					start = sc->memlimit + 1;
1777			}
1778			if (pcib_is_prefetch_open(sc)) {
1779				if (start < sc->pmemlimit && end > sc->pmembase)
1780					start = sc->pmemlimit + 1;
1781			}
1782#endif
1783		}
1784		if (end < start) {
1785			device_printf(dev, "memory: end (%lx) < start (%lx)\n",
1786			    end, start);
1787			start = 0;
1788			end = 0;
1789			ok = 0;
1790		}
1791		if (!ok && bootverbose)
1792			device_printf(dev,
1793			    "%s%srequested unsupported memory range %#lx-%#lx "
1794			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
1795			    name, suffix, start, end,
1796			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1797			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1798		if (!ok)
1799			return (NULL);
1800		if (bootverbose)
1801			device_printf(dev,"%s%srequested memory range "
1802			    "0x%lx-0x%lx: good\n",
1803			    name, suffix, start, end);
1804		break;
1805
1806	default:
1807		break;
1808	}
1809	/*
1810	 * Bridge is OK decoding this resource, so pass it up.
1811	 */
1812	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
1813	    count, flags));
1814}
1815#endif
1816
1817/*
1818 * If ARI is enabled on this downstream port, translate the function number
1819 * to the non-ARI slot/function.  The downstream port will convert it back in
1820 * hardware.  If ARI is not enabled slot and func are not modified.
1821 */
1822static __inline void
1823pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func)
1824{
1825	struct pcib_softc *sc;
1826	int ari_func;
1827
1828	sc = device_get_softc(pcib);
1829	ari_func = *func;
1830
1831	if (sc->flags & PCIB_ENABLE_ARI) {
1832		KASSERT(*slot == 0,
1833		    ("Non-zero slot number with ARI enabled!"));
1834		*slot = PCIE_ARI_SLOT(ari_func);
1835		*func = PCIE_ARI_FUNC(ari_func);
1836	}
1837}
1838
1839
1840static void
1841pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos)
1842{
1843	uint32_t ctl2;
1844
1845	ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4);
1846	ctl2 |= PCIEM_CTL2_ARI;
1847	pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4);
1848
1849	sc->flags |= PCIB_ENABLE_ARI;
1850}
1851
1852/*
1853 * PCIB interface.
1854 */
1855int
1856pcib_maxslots(device_t dev)
1857{
1858	return (PCI_SLOTMAX);
1859}
1860
1861static int
1862pcib_ari_maxslots(device_t dev)
1863{
1864	struct pcib_softc *sc;
1865
1866	sc = device_get_softc(dev);
1867
1868	if (sc->flags & PCIB_ENABLE_ARI)
1869		return (PCIE_ARI_SLOTMAX);
1870	else
1871		return (PCI_SLOTMAX);
1872}
1873
1874static int
1875pcib_ari_maxfuncs(device_t dev)
1876{
1877	struct pcib_softc *sc;
1878
1879	sc = device_get_softc(dev);
1880
1881	if (sc->flags & PCIB_ENABLE_ARI)
1882		return (PCIE_ARI_FUNCMAX);
1883	else
1884		return (PCI_FUNCMAX);
1885}
1886
1887/*
1888 * Since we are a child of a PCI bus, its parent must support the pcib interface.
1889 */
1890static uint32_t
1891pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
1892{
1893
1894	pcib_xlate_ari(dev, b, &s, &f);
1895	return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s,
1896	    f, reg, width));
1897}
1898
1899static void
1900pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
1901{
1902
1903	pcib_xlate_ari(dev, b, &s, &f);
1904	PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f,
1905	    reg, val, width);
1906}
1907
1908/*
1909 * Route an interrupt across a PCI bridge.
1910 */
1911int
1912pcib_route_interrupt(device_t pcib, device_t dev, int pin)
1913{
1914    device_t	bus;
1915    int		parent_intpin;
1916    int		intnum;
1917
1918    /*
1919     *
1920     * The PCI standard defines a swizzle of the child-side device/intpin to
1921     * the parent-side intpin as follows.
1922     *
1923     * device = device on child bus
1924     * child_intpin = intpin on child bus slot (0-3)
1925     * parent_intpin = intpin on parent bus slot (0-3)
1926     *
1927     * parent_intpin = (device + child_intpin) % 4
1928     */
1929    parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
1930
1931    /*
1932     * Our parent is a PCI bus.  Its parent must export the pcib interface
1933     * which includes the ability to route interrupts.
1934     */
1935    bus = device_get_parent(pcib);
1936    intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
1937    if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1938	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1939	    pci_get_slot(dev), 'A' + pin - 1, intnum);
1940    }
1941    return(intnum);
1942}
1943
1944/* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
1945int
1946pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
1947{
1948	struct pcib_softc *sc = device_get_softc(pcib);
1949	device_t bus;
1950
1951	if (sc->flags & PCIB_DISABLE_MSI)
1952		return (ENXIO);
1953	bus = device_get_parent(pcib);
1954	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
1955	    irqs));
1956}
1957
1958/* Pass request to release MSI/MSI-X messages up to the parent bridge. */
1959int
1960pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
1961{
1962	device_t bus;
1963
1964	bus = device_get_parent(pcib);
1965	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
1966}
1967
1968/* Pass request to alloc an MSI-X message up to the parent bridge. */
1969int
1970pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
1971{
1972	struct pcib_softc *sc = device_get_softc(pcib);
1973	device_t bus;
1974
1975	if (sc->flags & PCIB_DISABLE_MSIX)
1976		return (ENXIO);
1977	bus = device_get_parent(pcib);
1978	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
1979}
1980
1981/* Pass request to release an MSI-X message up to the parent bridge. */
1982int
1983pcib_release_msix(device_t pcib, device_t dev, int irq)
1984{
1985	device_t bus;
1986
1987	bus = device_get_parent(pcib);
1988	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
1989}
1990
1991/* Pass request to map MSI/MSI-X message up to parent bridge. */
1992int
1993pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
1994    uint32_t *data)
1995{
1996	device_t bus;
1997	int error;
1998
1999	bus = device_get_parent(pcib);
2000	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
2001	if (error)
2002		return (error);
2003
2004	pci_ht_map_msi(pcib, *addr);
2005	return (0);
2006}
2007
2008/* Pass request for device power state up to parent bridge. */
2009int
2010pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
2011{
2012	device_t bus;
2013
2014	bus = device_get_parent(pcib);
2015	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
2016}
2017
2018static uint16_t
2019pcib_ari_get_rid(device_t pcib, device_t dev)
2020{
2021	struct pcib_softc *sc;
2022	uint8_t bus, slot, func;
2023
2024	sc = device_get_softc(pcib);
2025
2026	if (sc->flags & PCIB_ENABLE_ARI) {
2027		bus = pci_get_bus(dev);
2028		func = pci_get_function(dev);
2029
2030		return (PCI_ARI_RID(bus, func));
2031	} else {
2032		bus = pci_get_bus(dev);
2033		slot = pci_get_slot(dev);
2034		func = pci_get_function(dev);
2035
2036		return (PCI_RID(bus, slot, func));
2037	}
2038}
2039
2040/*
2041 * Check that the downstream port (pcib) and the endpoint device (dev) both
2042 * support ARI.  If so, enable it and return 0, otherwise return an error.
2043 */
2044static int
2045pcib_try_enable_ari(device_t pcib, device_t dev)
2046{
2047	struct pcib_softc *sc;
2048	int error;
2049	uint32_t cap2;
2050	int ari_cap_off;
2051	uint32_t ari_ver;
2052	uint32_t pcie_pos;
2053
2054	sc = device_get_softc(pcib);
2055
2056	/*
2057	 * ARI is controlled in a register in the PCIe capability structure.
2058	 * If the downstream port does not have the PCIe capability structure
2059	 * then it does not support ARI.
2060	 */
2061	error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos);
2062	if (error != 0)
2063		return (ENODEV);
2064
2065	/* Check that the PCIe port advertises ARI support. */
2066	cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4);
2067	if (!(cap2 & PCIEM_CAP2_ARI))
2068		return (ENODEV);
2069
2070	/*
2071	 * Check that the endpoint device advertises ARI support via the ARI
2072	 * extended capability structure.
2073	 */
2074	error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off);
2075	if (error != 0)
2076		return (ENODEV);
2077
2078	/*
2079	 * Finally, check that the endpoint device supports the same version
2080	 * of ARI that we do.
2081	 */
2082	ari_ver = pci_read_config(dev, ari_cap_off, 4);
2083	if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) {
2084		if (bootverbose)
2085			device_printf(pcib,
2086			    "Unsupported version of ARI (%d) detected\n",
2087			    PCI_EXTCAP_VER(ari_ver));
2088
2089		return (ENXIO);
2090	}
2091
2092	pcib_enable_ari(sc, pcie_pos);
2093
2094	return (0);
2095}
2096
2097