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