xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision 7f9dff23d3092aa33ad45b2b63e52469b3c13a6e)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_acpi.h"
32 #include "opt_pci.h"
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/rman.h>
41 #include <sys/sysctl.h>
42 
43 #include <contrib/dev/acpica/include/acpi.h>
44 #include <contrib/dev/acpica/include/accommon.h>
45 
46 #include <dev/acpica/acpivar.h>
47 
48 #include <machine/pci_cfgreg.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcib_private.h>
52 #include "pcib_if.h"
53 
54 #include <dev/acpica/acpi_pcibvar.h>
55 
56 /* Hooks for the ACPI CA debugging infrastructure. */
57 #define _COMPONENT	ACPI_BUS
58 ACPI_MODULE_NAME("PCI_ACPI")
59 
60 struct acpi_hpcib_softc {
61     device_t		ap_dev;
62     ACPI_HANDLE		ap_handle;
63     int			ap_flags;
64 
65     int			ap_segment;	/* PCI domain */
66     int			ap_bus;		/* bios-assigned bus number */
67     int			ap_addr;	/* device/func of PCI-Host bridge */
68 
69     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
70 #ifdef NEW_PCIB
71     struct pcib_host_resources ap_host_res;
72 #endif
73 };
74 
75 static int		acpi_pcib_acpi_probe(device_t bus);
76 static int		acpi_pcib_acpi_attach(device_t bus);
77 static int		acpi_pcib_read_ivar(device_t dev, device_t child,
78 			    int which, uintptr_t *result);
79 static int		acpi_pcib_write_ivar(device_t dev, device_t child,
80 			    int which, uintptr_t value);
81 static uint32_t		acpi_pcib_read_config(device_t dev, u_int bus,
82 			    u_int slot, u_int func, u_int reg, int bytes);
83 static void		acpi_pcib_write_config(device_t dev, u_int bus,
84 			    u_int slot, u_int func, u_int reg, uint32_t data,
85 			    int bytes);
86 static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
87 			    device_t dev, int pin);
88 static int		acpi_pcib_alloc_msi(device_t pcib, device_t dev,
89 			    int count, int maxcount, int *irqs);
90 static int		acpi_pcib_map_msi(device_t pcib, device_t dev,
91 			    int irq, uint64_t *addr, uint32_t *data);
92 static int		acpi_pcib_alloc_msix(device_t pcib, device_t dev,
93 			    int *irq);
94 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
95 			    device_t child, int type, int *rid,
96 			    rman_res_t start, rman_res_t end, rman_res_t count,
97 			    u_int flags);
98 #ifdef NEW_PCIB
99 static int		acpi_pcib_acpi_adjust_resource(device_t dev,
100 			    device_t child, int type, struct resource *r,
101 			    rman_res_t start, rman_res_t end);
102 #ifdef PCI_RES_BUS
103 static int		acpi_pcib_acpi_release_resource(device_t dev,
104 			    device_t child, int type, int rid,
105 			    struct resource *r);
106 #endif
107 #endif
108 
109 static device_method_t acpi_pcib_acpi_methods[] = {
110     /* Device interface */
111     DEVMETHOD(device_probe,		acpi_pcib_acpi_probe),
112     DEVMETHOD(device_attach,		acpi_pcib_acpi_attach),
113     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
114     DEVMETHOD(device_suspend,		bus_generic_suspend),
115     DEVMETHOD(device_resume,		bus_generic_resume),
116 
117     /* Bus interface */
118     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
119     DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
120     DEVMETHOD(bus_alloc_resource,	acpi_pcib_acpi_alloc_resource),
121 #ifdef NEW_PCIB
122     DEVMETHOD(bus_adjust_resource,	acpi_pcib_acpi_adjust_resource),
123 #else
124     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
125 #endif
126 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
127     DEVMETHOD(bus_release_resource,	acpi_pcib_acpi_release_resource),
128 #else
129     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
130 #endif
131     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
132     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
133     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
134     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
135     DEVMETHOD(bus_get_cpus,		acpi_pcib_get_cpus),
136 
137     /* pcib interface */
138     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
139     DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
140     DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
141     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
142     DEVMETHOD(pcib_alloc_msi,		acpi_pcib_alloc_msi),
143     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
144     DEVMETHOD(pcib_alloc_msix,		acpi_pcib_alloc_msix),
145     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
146     DEVMETHOD(pcib_map_msi,		acpi_pcib_map_msi),
147     DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
148 
149     DEVMETHOD_END
150 };
151 
152 static devclass_t pcib_devclass;
153 
154 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
155     sizeof(struct acpi_hpcib_softc));
156 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
157 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
158 
159 static int
160 acpi_pcib_acpi_probe(device_t dev)
161 {
162     ACPI_DEVICE_INFO	*devinfo;
163     ACPI_HANDLE		h;
164     int			root;
165 
166     if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
167 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
168 	return (ENXIO);
169     root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
170     AcpiOsFree(devinfo);
171     if (!root || pci_cfgregopen() == 0)
172 	return (ENXIO);
173 
174     device_set_desc(dev, "ACPI Host-PCI bridge");
175     return (0);
176 }
177 
178 #ifdef NEW_PCIB
179 static ACPI_STATUS
180 acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
181 {
182 	struct acpi_hpcib_softc *sc;
183 	UINT64 length, min, max;
184 	u_int flags;
185 	int error, type;
186 
187 	sc = context;
188 	switch (res->Type) {
189 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
190 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
191 		panic("host bridge has depenedent resources");
192 	case ACPI_RESOURCE_TYPE_ADDRESS16:
193 	case ACPI_RESOURCE_TYPE_ADDRESS32:
194 	case ACPI_RESOURCE_TYPE_ADDRESS64:
195 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
196 		if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
197 			break;
198 		switch (res->Type) {
199 		case ACPI_RESOURCE_TYPE_ADDRESS16:
200 			min = res->Data.Address16.Address.Minimum;
201 			max = res->Data.Address16.Address.Maximum;
202 			length = res->Data.Address16.Address.AddressLength;
203 			break;
204 		case ACPI_RESOURCE_TYPE_ADDRESS32:
205 			min = res->Data.Address32.Address.Minimum;
206 			max = res->Data.Address32.Address.Maximum;
207 			length = res->Data.Address32.Address.AddressLength;
208 			break;
209 		case ACPI_RESOURCE_TYPE_ADDRESS64:
210 			min = res->Data.Address64.Address.Minimum;
211 			max = res->Data.Address64.Address.Maximum;
212 			length = res->Data.Address64.Address.AddressLength;
213 			break;
214 		default:
215 			KASSERT(res->Type ==
216 			    ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
217 			    ("should never happen"));
218 			min = res->Data.ExtAddress64.Address.Minimum;
219 			max = res->Data.ExtAddress64.Address.Maximum;
220 			length = res->Data.ExtAddress64.Address.AddressLength;
221 			break;
222 		}
223 		if (length == 0)
224 			break;
225 		if (min + length - 1 != max &&
226 		    (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
227 		    res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
228 			break;
229 		flags = 0;
230 		switch (res->Data.Address.ResourceType) {
231 		case ACPI_MEMORY_RANGE:
232 			type = SYS_RES_MEMORY;
233 			if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
234 				if (res->Data.Address.Info.Mem.Caching ==
235 				    ACPI_PREFETCHABLE_MEMORY)
236 					flags |= RF_PREFETCHABLE;
237 			} else {
238 				/*
239 				 * XXX: Parse prefetch flag out of
240 				 * TypeSpecific.
241 				 */
242 			}
243 			break;
244 		case ACPI_IO_RANGE:
245 			type = SYS_RES_IOPORT;
246 			break;
247 #ifdef PCI_RES_BUS
248 		case ACPI_BUS_NUMBER_RANGE:
249 			type = PCI_RES_BUS;
250 			break;
251 #endif
252 		default:
253 			return (AE_OK);
254 		}
255 
256 		if (min + length - 1 != max)
257 			device_printf(sc->ap_dev,
258 			    "Length mismatch for %d range: %jx vs %jx\n", type,
259 			    (uintmax_t)(max - min + 1), (uintmax_t)length);
260 #ifdef __i386__
261 		if (min > ULONG_MAX) {
262 			device_printf(sc->ap_dev,
263 			    "Ignoring %d range above 4GB (%#jx-%#jx)\n",
264 			    type, (uintmax_t)min, (uintmax_t)max);
265 			break;
266 		}
267 		if (max > ULONG_MAX) {
268 			device_printf(sc->ap_dev,
269        		    "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
270 			    type, (uintmax_t)min, (uintmax_t)max);
271 			max = ULONG_MAX;
272 		}
273 #endif
274 		error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
275 		    flags);
276 		if (error)
277 			panic("Failed to manage %d range (%#jx-%#jx): %d",
278 			    type, (uintmax_t)min, (uintmax_t)max, error);
279 		break;
280 	default:
281 		break;
282 	}
283 	return (AE_OK);
284 }
285 #endif
286 
287 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
288 static int
289 first_decoded_bus(struct acpi_hpcib_softc *sc, rman_res_t *startp)
290 {
291 	struct resource_list_entry *rle;
292 
293 	rle = resource_list_find(&sc->ap_host_res.hr_rl, PCI_RES_BUS, 0);
294 	if (rle == NULL)
295 		return (ENXIO);
296 	*startp = rle->start;
297 	return (0);
298 }
299 #endif
300 
301 static void
302 acpi_pcib_osc(struct acpi_hpcib_softc *sc)
303 {
304 	ACPI_STATUS status;
305 	uint32_t cap_set[3];
306 
307 	static uint8_t pci_host_bridge_uuid[ACPI_UUID_LENGTH] = {
308 		0x5b, 0x4d, 0xdb, 0x33, 0xf7, 0x1f, 0x1c, 0x40,
309 		0x96, 0x57, 0x74, 0x41, 0xc0, 0x3d, 0xd7, 0x66
310 	};
311 
312 	/* Support Field: Extended PCI Config Space, MSI */
313 	cap_set[1] = 0x11;
314 
315 	/* Control Field */
316 	cap_set[2] = 0;
317 
318 #ifdef PCI_HP
319 	/* Control Field: PCI Express Native Hot Plug */
320 	cap_set[2] |= 0x1;
321 #endif
322 
323 	status = acpi_EvaluateOSC(sc->ap_handle, pci_host_bridge_uuid, 1,
324 	    nitems(cap_set), cap_set, cap_set, false);
325 	if (ACPI_FAILURE(status)) {
326 		if (status == AE_NOT_FOUND)
327 			return;
328 		device_printf(sc->ap_dev, "_OSC failed: %s\n",
329 		    AcpiFormatException(status));
330 		return;
331 	}
332 
333 	if (cap_set[0] != 0) {
334 		device_printf(sc->ap_dev, "_OSC returned error %#x\n",
335 		    cap_set[0]);
336 	}
337 }
338 
339 static int
340 acpi_pcib_acpi_attach(device_t dev)
341 {
342     struct acpi_hpcib_softc	*sc;
343     ACPI_STATUS			status;
344     static int bus0_seen = 0;
345     u_int slot, func, busok;
346 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
347     struct resource *bus_res;
348     rman_res_t start;
349     int rid;
350 #endif
351     uint8_t busno;
352 
353     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
354 
355     sc = device_get_softc(dev);
356     sc->ap_dev = dev;
357     sc->ap_handle = acpi_get_handle(dev);
358 
359     /*
360      * Don't attach if we're not really there.
361      */
362     if (!acpi_DeviceIsPresent(dev))
363 	return (ENXIO);
364 
365     acpi_pcib_osc(sc);
366 
367     /*
368      * Get our segment number by evaluating _SEG.
369      * It's OK for this to not exist.
370      */
371     status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
372     if (ACPI_FAILURE(status)) {
373 	if (status != AE_NOT_FOUND) {
374 	    device_printf(dev, "could not evaluate _SEG - %s\n",
375 		AcpiFormatException(status));
376 	    return_VALUE (ENXIO);
377 	}
378 	/* If it's not found, assume 0. */
379 	sc->ap_segment = 0;
380     }
381 
382     /*
383      * Get the address (device and function) of the associated
384      * PCI-Host bridge device from _ADR.  Assume we don't have one if
385      * it doesn't exist.
386      */
387     status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
388     if (ACPI_FAILURE(status)) {
389 	device_printf(dev, "could not evaluate _ADR - %s\n",
390 	    AcpiFormatException(status));
391 	sc->ap_addr = -1;
392     }
393 
394 #ifdef NEW_PCIB
395     /*
396      * Determine which address ranges this bridge decodes and setup
397      * resource managers for those ranges.
398      */
399     if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
400 	    panic("failed to init hostb resources");
401     if (!acpi_disabled("hostres")) {
402 	status = AcpiWalkResources(sc->ap_handle, "_CRS",
403 	    acpi_pcib_producer_handler, sc);
404 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
405 	    device_printf(sc->ap_dev, "failed to parse resources: %s\n",
406 		AcpiFormatException(status));
407     }
408 #endif
409 
410     /*
411      * Get our base bus number by evaluating _BBN.
412      * If this doesn't work, we assume we're bus number 0.
413      *
414      * XXX note that it may also not exist in the case where we are
415      *     meant to use a private configuration space mechanism for this bus,
416      *     so we should dig out our resources and check to see if we have
417      *     anything like that.  How do we do this?
418      * XXX If we have the requisite information, and if we don't think the
419      *     default PCI configuration space handlers can deal with this bus,
420      *     we should attach our own handler.
421      * XXX invoke _REG on this for the PCI config space address space?
422      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
423      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
424      *     if _BBN is zero and PCI bus 0 already exists, we try to read our
425      *     bus number from the configuration registers at address _ADR.
426      *     We only do this for domain/segment 0 in the hopes that this is
427      *     only needed for old single-domain machines.
428      */
429     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
430     if (ACPI_FAILURE(status)) {
431 	if (status != AE_NOT_FOUND) {
432 	    device_printf(dev, "could not evaluate _BBN - %s\n",
433 		AcpiFormatException(status));
434 	    return (ENXIO);
435 	} else {
436 	    /* If it's not found, assume 0. */
437 	    sc->ap_bus = 0;
438 	}
439     }
440 
441     /*
442      * If this is segment 0, the bus is zero, and PCI bus 0 already
443      * exists, read the bus number via PCI config space.
444      */
445     busok = 1;
446     if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
447 	busok = 0;
448 	if (sc->ap_addr != -1) {
449 	    /* XXX: We assume bus 0. */
450 	    slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
451 	    func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
452 	    if (bootverbose)
453 		device_printf(dev, "reading config registers from 0:%d:%d\n",
454 		    slot, func);
455 	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
456 		device_printf(dev, "couldn't read bus number from cfg space\n");
457 	    else {
458 		sc->ap_bus = busno;
459 		busok = 1;
460 	    }
461 	}
462     }
463 
464 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
465     /*
466      * If nothing else worked, hope that ACPI at least lays out the
467      * Host-PCI bridges in order and that as a result the next free
468      * bus number is our bus number.
469      */
470     if (busok == 0) {
471 	    /*
472 	     * If we have a region of bus numbers, use the first
473 	     * number for our bus.
474 	     */
475 	    if (first_decoded_bus(sc, &start) == 0)
476 		    sc->ap_bus = start;
477 	    else {
478 		    rid = 0;
479 		    bus_res = pci_domain_alloc_bus(sc->ap_segment, dev, &rid, 0,
480 			PCI_BUSMAX, 1, 0);
481 		    if (bus_res == NULL) {
482 			    device_printf(dev,
483 				"could not allocate bus number\n");
484 			    pcib_host_res_free(dev, &sc->ap_host_res);
485 			    return (ENXIO);
486 		    }
487 		    sc->ap_bus = rman_get_start(bus_res);
488 		    pci_domain_release_bus(sc->ap_segment, dev, rid, bus_res);
489 	    }
490     } else {
491 	    /*
492 	     * Require the bus number from _BBN to match the start of any
493 	     * decoded range.
494 	     */
495 	    if (first_decoded_bus(sc, &start) == 0 && sc->ap_bus != start) {
496 		    device_printf(dev,
497 		"bus number %d does not match start of decoded range %ju\n",
498 			sc->ap_bus, (uintmax_t)start);
499 		    pcib_host_res_free(dev, &sc->ap_host_res);
500 		    return (ENXIO);
501 	    }
502     }
503 #else
504     /*
505      * If nothing else worked, hope that ACPI at least lays out the
506      * host-PCI bridges in order and that as a result our unit number
507      * is actually our bus number.  There are several reasons this
508      * might not be true.
509      */
510     if (busok == 0) {
511 	sc->ap_bus = device_get_unit(dev);
512 	device_printf(dev, "trying bus number %d\n", sc->ap_bus);
513     }
514 #endif
515 
516     /* If this is bus 0 on segment 0, note that it has been seen already. */
517     if (sc->ap_segment == 0 && sc->ap_bus == 0)
518 	    bus0_seen = 1;
519 
520     acpi_pcib_fetch_prt(dev, &sc->ap_prt);
521 
522     if (device_add_child(dev, "pci", -1) == NULL) {
523 	device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
524 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
525 	pcib_host_res_free(dev, &sc->ap_host_res);
526 #endif
527 	return (ENXIO);
528     }
529     return (bus_generic_attach(dev));
530 }
531 
532 /*
533  * Support for standard PCI bridge ivars.
534  */
535 static int
536 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
537 {
538     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
539 
540     switch (which) {
541     case PCIB_IVAR_DOMAIN:
542 	*result = sc->ap_segment;
543 	return (0);
544     case PCIB_IVAR_BUS:
545 	*result = sc->ap_bus;
546 	return (0);
547     case ACPI_IVAR_HANDLE:
548 	*result = (uintptr_t)sc->ap_handle;
549 	return (0);
550     case ACPI_IVAR_FLAGS:
551 	*result = (uintptr_t)sc->ap_flags;
552 	return (0);
553     }
554     return (ENOENT);
555 }
556 
557 static int
558 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
559 {
560     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
561 
562     switch (which) {
563     case PCIB_IVAR_DOMAIN:
564 	return (EINVAL);
565     case PCIB_IVAR_BUS:
566 	sc->ap_bus = value;
567 	return (0);
568     case ACPI_IVAR_HANDLE:
569 	sc->ap_handle = (ACPI_HANDLE)value;
570 	return (0);
571     case ACPI_IVAR_FLAGS:
572 	sc->ap_flags = (int)value;
573 	return (0);
574     }
575     return (ENOENT);
576 }
577 
578 static uint32_t
579 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
580     u_int reg, int bytes)
581 {
582     return (pci_cfgregread(bus, slot, func, reg, bytes));
583 }
584 
585 static void
586 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
587     u_int reg, uint32_t data, int bytes)
588 {
589     pci_cfgregwrite(bus, slot, func, reg, data, bytes);
590 }
591 
592 static int
593 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
594 {
595     struct acpi_hpcib_softc *sc = device_get_softc(pcib);
596 
597     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
598 }
599 
600 static int
601 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
602     int *irqs)
603 {
604 	device_t bus;
605 
606 	bus = device_get_parent(pcib);
607 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
608 	    irqs));
609 }
610 
611 static int
612 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
613 {
614 	device_t bus;
615 
616 	bus = device_get_parent(pcib);
617 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
618 }
619 
620 static int
621 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
622     uint32_t *data)
623 {
624 	struct acpi_hpcib_softc *sc;
625 	device_t bus, hostb;
626 	int error;
627 
628 	bus = device_get_parent(pcib);
629 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
630 	if (error)
631 		return (error);
632 
633 	sc = device_get_softc(pcib);
634 	if (sc->ap_addr == -1)
635 		return (0);
636 	/* XXX: Assumes all bridges are on bus 0. */
637 	hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
638 	    ACPI_ADR_PCI_FUNC(sc->ap_addr));
639 	if (hostb != NULL)
640 		pci_ht_map_msi(hostb, *addr);
641 	return (0);
642 }
643 
644 struct resource *
645 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
646     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
647 {
648 #ifdef NEW_PCIB
649     struct acpi_hpcib_softc *sc;
650     struct resource *res;
651 #endif
652 
653 #if defined(__i386__) || defined(__amd64__)
654     start = hostb_alloc_start(type, start, end, count);
655 #endif
656 
657 #ifdef NEW_PCIB
658     sc = device_get_softc(dev);
659 #ifdef PCI_RES_BUS
660     if (type == PCI_RES_BUS)
661 	return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
662 	    count, flags));
663 #endif
664     res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
665 	count, flags);
666 
667     /*
668      * XXX: If this is a request for a specific range, assume it is
669      * correct and pass it up to the parent.  What we probably want to
670      * do long-term is explicitly trust any firmware-configured
671      * resources during the initial bus scan on boot and then disable
672      * this after that.
673      */
674     if (res == NULL && start + count - 1 == end)
675 	res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
676 	    count, flags);
677     return (res);
678 #else
679     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
680 	count, flags));
681 #endif
682 }
683 
684 #ifdef NEW_PCIB
685 int
686 acpi_pcib_acpi_adjust_resource(device_t dev, device_t child, int type,
687     struct resource *r, rman_res_t start, rman_res_t end)
688 {
689 	struct acpi_hpcib_softc *sc;
690 
691 	sc = device_get_softc(dev);
692 #ifdef PCI_RES_BUS
693 	if (type == PCI_RES_BUS)
694 		return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
695 		    end));
696 #endif
697 	return (pcib_host_res_adjust(&sc->ap_host_res, child, type, r, start,
698 	    end));
699 }
700 
701 #ifdef PCI_RES_BUS
702 int
703 acpi_pcib_acpi_release_resource(device_t dev, device_t child, int type, int rid,
704     struct resource *r)
705 {
706 	struct acpi_hpcib_softc *sc;
707 
708 	sc = device_get_softc(dev);
709 	if (type == PCI_RES_BUS)
710 		return (pci_domain_release_bus(sc->ap_segment, child, rid, r));
711 	return (bus_generic_release_resource(dev, child, type, rid, r));
712 }
713 #endif
714 #endif
715