xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision bf847ea31ae25953d115a57c77ff556e9641cd26)
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 #include "opt_acpi.h"
30 #include "opt_pci.h"
31 
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     bus_dma_tag_t	ap_dma_tag;
62     int			ap_flags;
63     uint32_t		ap_osc_ctl;
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     struct pcib_host_resources ap_host_res;
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 static int		acpi_pcib_acpi_adjust_resource(device_t dev,
97 			    device_t child, struct resource *r,
98 			    rman_res_t start, rman_res_t end);
99 static int		acpi_pcib_acpi_release_resource(device_t dev,
100 			    device_t child, struct resource *r);
101 static int		acpi_pcib_acpi_activate_resource(device_t dev,
102 			    device_t child, struct resource *r);
103 static int		acpi_pcib_acpi_deactivate_resource(device_t dev,
104 			    device_t child, struct resource *r);
105 static int		acpi_pcib_request_feature(device_t pcib, device_t dev,
106 			    enum pci_feature feature);
107 static bus_dma_tag_t	acpi_pcib_get_dma_tag(device_t bus, device_t child);
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     DEVMETHOD(bus_adjust_resource,	acpi_pcib_acpi_adjust_resource),
122     DEVMETHOD(bus_release_resource,	acpi_pcib_acpi_release_resource),
123     DEVMETHOD(bus_activate_resource,	acpi_pcib_acpi_activate_resource),
124     DEVMETHOD(bus_deactivate_resource,	acpi_pcib_acpi_deactivate_resource),
125     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
126     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
127     DEVMETHOD(bus_get_cpus,		acpi_pcib_get_cpus),
128     DEVMETHOD(bus_get_dma_tag,		acpi_pcib_get_dma_tag),
129 
130     /* pcib interface */
131     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
132     DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
133     DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
134     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
135     DEVMETHOD(pcib_alloc_msi,		acpi_pcib_alloc_msi),
136     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
137     DEVMETHOD(pcib_alloc_msix,		acpi_pcib_alloc_msix),
138     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
139     DEVMETHOD(pcib_map_msi,		acpi_pcib_map_msi),
140     DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
141     DEVMETHOD(pcib_request_feature,	acpi_pcib_request_feature),
142 
143     DEVMETHOD_END
144 };
145 
146 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
147     sizeof(struct acpi_hpcib_softc));
148 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, 0, 0);
149 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
150 
151 static int
152 acpi_pcib_acpi_probe(device_t dev)
153 {
154     ACPI_DEVICE_INFO	*devinfo;
155     ACPI_HANDLE		h;
156     int			root;
157 
158     if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
159 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
160 	return (ENXIO);
161     root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
162     AcpiOsFree(devinfo);
163     if (!root || pci_cfgregopen() == 0)
164 	return (ENXIO);
165 
166     device_set_desc(dev, "ACPI Host-PCI bridge");
167     return (0);
168 }
169 
170 static ACPI_STATUS
171 acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
172 {
173 	struct acpi_hpcib_softc *sc;
174 	UINT64 length, min, max;
175 	u_int flags;
176 	int error, type;
177 
178 	sc = context;
179 	switch (res->Type) {
180 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
181 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
182 		panic("host bridge has depenedent resources");
183 	case ACPI_RESOURCE_TYPE_ADDRESS16:
184 	case ACPI_RESOURCE_TYPE_ADDRESS32:
185 	case ACPI_RESOURCE_TYPE_ADDRESS64:
186 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
187 		if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
188 			break;
189 		switch (res->Type) {
190 		case ACPI_RESOURCE_TYPE_ADDRESS16:
191 			min = res->Data.Address16.Address.Minimum;
192 			max = res->Data.Address16.Address.Maximum;
193 			length = res->Data.Address16.Address.AddressLength;
194 			break;
195 		case ACPI_RESOURCE_TYPE_ADDRESS32:
196 			min = res->Data.Address32.Address.Minimum;
197 			max = res->Data.Address32.Address.Maximum;
198 			length = res->Data.Address32.Address.AddressLength;
199 			break;
200 		case ACPI_RESOURCE_TYPE_ADDRESS64:
201 			min = res->Data.Address64.Address.Minimum;
202 			max = res->Data.Address64.Address.Maximum;
203 			length = res->Data.Address64.Address.AddressLength;
204 			break;
205 		default:
206 			KASSERT(res->Type ==
207 			    ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
208 			    ("should never happen"));
209 			min = res->Data.ExtAddress64.Address.Minimum;
210 			max = res->Data.ExtAddress64.Address.Maximum;
211 			length = res->Data.ExtAddress64.Address.AddressLength;
212 			break;
213 		}
214 		if (length == 0)
215 			break;
216 		if (min + length - 1 != max &&
217 		    (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
218 		    res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
219 			break;
220 		flags = 0;
221 		switch (res->Data.Address.ResourceType) {
222 		case ACPI_MEMORY_RANGE:
223 			type = SYS_RES_MEMORY;
224 			if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
225 				if (res->Data.Address.Info.Mem.Caching ==
226 				    ACPI_PREFETCHABLE_MEMORY)
227 					flags |= RF_PREFETCHABLE;
228 			} else {
229 				/*
230 				 * XXX: Parse prefetch flag out of
231 				 * TypeSpecific.
232 				 */
233 			}
234 			break;
235 		case ACPI_IO_RANGE:
236 			type = SYS_RES_IOPORT;
237 			break;
238 		case ACPI_BUS_NUMBER_RANGE:
239 			type = PCI_RES_BUS;
240 			break;
241 		default:
242 			return (AE_OK);
243 		}
244 
245 		if (min + length - 1 != max)
246 			device_printf(sc->ap_dev,
247 			    "Length mismatch for %d range: %jx vs %jx\n", type,
248 			    (uintmax_t)(max - min + 1), (uintmax_t)length);
249 #ifdef __i386__
250 		if (min > ULONG_MAX) {
251 			device_printf(sc->ap_dev,
252 			    "Ignoring %d range above 4GB (%#jx-%#jx)\n",
253 			    type, (uintmax_t)min, (uintmax_t)max);
254 			break;
255 		}
256 		if (max > ULONG_MAX) {
257 			device_printf(sc->ap_dev,
258        		    "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
259 			    type, (uintmax_t)min, (uintmax_t)max);
260 			max = ULONG_MAX;
261 		}
262 #endif
263 		error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
264 		    flags);
265 		if (error)
266 			panic("Failed to manage %d range (%#jx-%#jx): %d",
267 			    type, (uintmax_t)min, (uintmax_t)max, error);
268 		break;
269 	default:
270 		break;
271 	}
272 	return (AE_OK);
273 }
274 
275 static bool
276 get_decoded_bus_range(struct acpi_hpcib_softc *sc, rman_res_t *startp,
277     rman_res_t *endp)
278 {
279 	struct resource_list_entry *rle;
280 
281 	rle = resource_list_find(&sc->ap_host_res.hr_rl, PCI_RES_BUS, 0);
282 	if (rle == NULL)
283 		return (false);
284 	*startp = rle->start;
285 	*endp = rle->end;
286 	return (true);
287 }
288 
289 static int
290 acpi_pcib_osc(struct acpi_hpcib_softc *sc, uint32_t osc_ctl)
291 {
292 	ACPI_STATUS status;
293 	uint32_t cap_set[3];
294 
295 	static uint8_t pci_host_bridge_uuid[ACPI_UUID_LENGTH] = {
296 		0x5b, 0x4d, 0xdb, 0x33, 0xf7, 0x1f, 0x1c, 0x40,
297 		0x96, 0x57, 0x74, 0x41, 0xc0, 0x3d, 0xd7, 0x66
298 	};
299 
300 	/*
301 	 * Don't invoke _OSC if a control is already granted.
302 	 * However, always invoke _OSC during attach when 0 is passed.
303 	 */
304 	if (osc_ctl != 0 && (sc->ap_osc_ctl & osc_ctl) == osc_ctl)
305 		return (0);
306 
307 	/* Support Field: Extended PCI Config Space, PCI Segment Groups, MSI */
308 	cap_set[PCI_OSC_SUPPORT] = PCIM_OSC_SUPPORT_EXT_PCI_CONF |
309 	    PCIM_OSC_SUPPORT_SEG_GROUP | PCIM_OSC_SUPPORT_MSI;
310 	/* Active State Power Management, Clock Power Management Capability */
311 	if (pci_enable_aspm)
312 		cap_set[PCI_OSC_SUPPORT] |= PCIM_OSC_SUPPORT_ASPM |
313 		    PCIM_OSC_SUPPORT_CPMC;
314 
315 	/* Control Field */
316 	cap_set[PCI_OSC_CTL] = sc->ap_osc_ctl | osc_ctl;
317 
318 	status = acpi_EvaluateOSC(sc->ap_handle, pci_host_bridge_uuid, 1,
319 	    nitems(cap_set), cap_set, cap_set, false);
320 	if (ACPI_FAILURE(status)) {
321 		if (status == AE_NOT_FOUND) {
322 			sc->ap_osc_ctl |= osc_ctl;
323 			return (0);
324 		}
325 		device_printf(sc->ap_dev, "_OSC failed: %s\n",
326 		    AcpiFormatException(status));
327 		return (EIO);
328 	}
329 
330 	/*
331 	 * _OSC may return an error in the status word, but will
332 	 * update the control mask always.  _OSC should not revoke
333 	 * previously-granted controls.
334 	 */
335 	if ((cap_set[PCI_OSC_CTL] & sc->ap_osc_ctl) != sc->ap_osc_ctl)
336 		device_printf(sc->ap_dev, "_OSC revoked %#x\n",
337 		    (cap_set[PCI_OSC_CTL] & sc->ap_osc_ctl) ^ sc->ap_osc_ctl);
338 	sc->ap_osc_ctl = cap_set[PCI_OSC_CTL];
339 	if ((sc->ap_osc_ctl & osc_ctl) != osc_ctl)
340 		return (EIO);
341 
342 	return (0);
343 }
344 
345 static int
346 acpi_pcib_acpi_attach(device_t dev)
347 {
348     struct acpi_hpcib_softc	*sc;
349     ACPI_STATUS			status;
350     static int bus0_seen = 0;
351     u_int slot, func, busok;
352     struct resource *bus_res;
353     rman_res_t end, start;
354     int rid;
355     int error, domain;
356     uint8_t busno;
357 
358     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
359 
360     sc = device_get_softc(dev);
361     sc->ap_dev = dev;
362     sc->ap_handle = acpi_get_handle(dev);
363 
364     /*
365      * Don't attach if we're not really there.
366      */
367     if (!acpi_DeviceIsPresent(dev))
368 	return (ENXIO);
369 
370     acpi_pcib_osc(sc, 0);
371 
372     /*
373      * Get our segment number by evaluating _SEG.
374      * It's OK for this to not exist.
375      */
376     status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
377     if (ACPI_FAILURE(status)) {
378 	if (status != AE_NOT_FOUND) {
379 	    device_printf(dev, "could not evaluate _SEG - %s\n",
380 		AcpiFormatException(status));
381 	    return_VALUE (ENXIO);
382 	}
383 	/* If it's not found, assume 0. */
384 	sc->ap_segment = 0;
385     }
386 
387     /*
388      * Get the address (device and function) of the associated
389      * PCI-Host bridge device from _ADR.  Assume we don't have one if
390      * it doesn't exist.
391      */
392     status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
393     if (ACPI_FAILURE(status)) {
394 	if (status != AE_NOT_FOUND)
395 	    device_printf(dev, "could not evaluate _ADR - %s\n",
396 		AcpiFormatException(status));
397 	sc->ap_addr = -1;
398     }
399 
400     /*
401      * Determine which address ranges this bridge decodes and setup
402      * resource managers for those ranges.
403      */
404     if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
405 	    panic("failed to init hostb resources");
406     if (!acpi_disabled("hostres")) {
407 	status = AcpiWalkResources(sc->ap_handle, "_CRS",
408 	    acpi_pcib_producer_handler, sc);
409 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
410 	    device_printf(sc->ap_dev, "failed to parse resources: %s\n",
411 		AcpiFormatException(status));
412     }
413 
414     /*
415      * Get our base bus number by evaluating _BBN.
416      * If this doesn't work, we assume we're bus number 0.
417      *
418      * XXX note that it may also not exist in the case where we are
419      *     meant to use a private configuration space mechanism for this bus,
420      *     so we should dig out our resources and check to see if we have
421      *     anything like that.  How do we do this?
422      * XXX If we have the requisite information, and if we don't think the
423      *     default PCI configuration space handlers can deal with this bus,
424      *     we should attach our own handler.
425      * XXX invoke _REG on this for the PCI config space address space?
426      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
427      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
428      *     if _BBN is zero and PCI bus 0 already exists, we try to read our
429      *     bus number from the configuration registers at address _ADR.
430      *     We only do this for domain/segment 0 in the hopes that this is
431      *     only needed for old single-domain machines.
432      */
433     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
434     if (ACPI_FAILURE(status)) {
435 	if (status != AE_NOT_FOUND) {
436 	    device_printf(dev, "could not evaluate _BBN - %s\n",
437 		AcpiFormatException(status));
438 	    return (ENXIO);
439 	} else {
440 	    /* If it's not found, assume 0. */
441 	    sc->ap_bus = 0;
442 	}
443     }
444 
445     /*
446      * If this is segment 0, the bus is zero, and PCI bus 0 already
447      * exists, read the bus number via PCI config space.
448      */
449     busok = 1;
450     if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
451 	busok = 0;
452 	if (sc->ap_addr != -1) {
453 	    /* XXX: We assume bus 0. */
454 	    slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
455 	    func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
456 	    if (bootverbose)
457 		device_printf(dev, "reading config registers from 0:%d:%d\n",
458 		    slot, func);
459 	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
460 		device_printf(dev, "couldn't read bus number from cfg space\n");
461 	    else {
462 		sc->ap_bus = busno;
463 		busok = 1;
464 	    }
465 	}
466     }
467 
468     /*
469      * If nothing else worked, hope that ACPI at least lays out the
470      * Host-PCI bridges in order and that as a result the next free
471      * bus number is our bus number.
472      */
473     if (busok == 0) {
474 	    /*
475 	     * If we have a region of bus numbers, use the first
476 	     * number for our bus.
477 	     */
478 	    if (get_decoded_bus_range(sc, &start, &end))
479 		    sc->ap_bus = start;
480 	    else {
481 		    rid = 0;
482 		    bus_res = pci_domain_alloc_bus(sc->ap_segment, dev, &rid, 0,
483 			PCI_BUSMAX, 1, 0);
484 		    if (bus_res == NULL) {
485 			    device_printf(dev,
486 				"could not allocate bus number\n");
487 			    pcib_host_res_free(dev, &sc->ap_host_res);
488 			    return (ENXIO);
489 		    }
490 		    sc->ap_bus = rman_get_start(bus_res);
491 		    pci_domain_release_bus(sc->ap_segment, dev, bus_res);
492 	    }
493     } else {
494 	    /*
495 	     * If there is a decoded bus range, assume the bus number is
496 	     * the first value in the range.  Warn if _BBN doesn't match.
497 	     */
498 	    if (get_decoded_bus_range(sc, &start, &end)) {
499 		    if (sc->ap_bus != start) {
500 			    device_printf(dev,
501 				"WARNING: BIOS configured bus number (%d) is "
502 				"not within decoded bus number range "
503 				"(%ju - %ju).\n",
504 				sc->ap_bus, (uintmax_t)start, (uintmax_t)end);
505 			    device_printf(dev,
506 				"Using range start (%ju) as bus number.\n",
507 				(uintmax_t)start);
508 			    sc->ap_bus = start;
509 		    }
510 	    }
511     }
512 
513     /* If this is bus 0 on segment 0, note that it has been seen already. */
514     if (sc->ap_segment == 0 && sc->ap_bus == 0)
515 	    bus0_seen = 1;
516 
517     acpi_pcib_fetch_prt(dev, &sc->ap_prt);
518 
519     error = bus_dma_tag_create(bus_get_dma_tag(dev), 1,
520 	0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
521 	NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
522 	BUS_SPACE_MAXSIZE, 0, NULL, NULL, &sc->ap_dma_tag);
523     if (error != 0)
524 	goto errout;
525     error = bus_get_domain(dev, &domain);
526     if (error == 0)
527 	error = bus_dma_tag_set_domain(sc->ap_dma_tag, domain);
528     /* Don't fail to attach if the domain can't be queried or set. */
529     error = 0;
530 
531     bus_identify_children(dev);
532     if (device_add_child(dev, "pci", -1) == NULL) {
533 	bus_dma_tag_destroy(sc->ap_dma_tag);
534 	sc->ap_dma_tag = NULL;
535 	error = ENXIO;
536 	goto errout;
537     }
538     bus_attach_children(dev);
539     return (0);
540 
541 errout:
542     device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
543     pcib_host_res_free(dev, &sc->ap_host_res);
544     return (error);
545 }
546 
547 /*
548  * Support for standard PCI bridge ivars.
549  */
550 static int
551 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
552 {
553     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
554 
555     switch (which) {
556     case PCIB_IVAR_DOMAIN:
557 	*result = sc->ap_segment;
558 	return (0);
559     case PCIB_IVAR_BUS:
560 	*result = sc->ap_bus;
561 	return (0);
562     case ACPI_IVAR_HANDLE:
563 	*result = (uintptr_t)sc->ap_handle;
564 	return (0);
565     case ACPI_IVAR_FLAGS:
566 	*result = (uintptr_t)sc->ap_flags;
567 	return (0);
568     }
569     return (ENOENT);
570 }
571 
572 static int
573 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
574 {
575     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
576 
577     switch (which) {
578     case PCIB_IVAR_DOMAIN:
579 	return (EINVAL);
580     case PCIB_IVAR_BUS:
581 	sc->ap_bus = value;
582 	return (0);
583     case ACPI_IVAR_HANDLE:
584 	sc->ap_handle = (ACPI_HANDLE)value;
585 	return (0);
586     case ACPI_IVAR_FLAGS:
587 	sc->ap_flags = (int)value;
588 	return (0);
589     }
590     return (ENOENT);
591 }
592 
593 static uint32_t
594 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
595     u_int reg, int bytes)
596 {
597     struct acpi_hpcib_softc *sc = device_get_softc(dev);
598 
599     return (pci_cfgregread(sc->ap_segment, bus, slot, func, reg, bytes));
600 }
601 
602 static void
603 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
604     u_int reg, uint32_t data, int bytes)
605 {
606     struct acpi_hpcib_softc *sc = device_get_softc(dev);
607 
608     pci_cfgregwrite(sc->ap_segment, bus, slot, func, reg, data, bytes);
609 }
610 
611 static int
612 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
613 {
614     struct acpi_hpcib_softc *sc = device_get_softc(pcib);
615 
616     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
617 }
618 
619 static int
620 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
621     int *irqs)
622 {
623 	device_t bus;
624 
625 	bus = device_get_parent(pcib);
626 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
627 	    irqs));
628 }
629 
630 static int
631 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
632 {
633 	device_t bus;
634 
635 	bus = device_get_parent(pcib);
636 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
637 }
638 
639 static int
640 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
641     uint32_t *data)
642 {
643 	struct acpi_hpcib_softc *sc;
644 	device_t bus, hostb;
645 	int error;
646 
647 	bus = device_get_parent(pcib);
648 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
649 	if (error)
650 		return (error);
651 
652 	sc = device_get_softc(pcib);
653 	if (sc->ap_addr == -1)
654 		return (0);
655 	/* XXX: Assumes all bridges are on bus 0. */
656 	hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
657 	    ACPI_ADR_PCI_FUNC(sc->ap_addr));
658 	if (hostb != NULL)
659 		pci_ht_map_msi(hostb, *addr);
660 	return (0);
661 }
662 
663 struct resource *
664 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
665     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
666 {
667     struct acpi_hpcib_softc *sc;
668     struct resource *res;
669 
670 #if defined(__i386__) || defined(__amd64__)
671     start = hostb_alloc_start(type, start, end, count);
672 #endif
673 
674     sc = device_get_softc(dev);
675     if (type == PCI_RES_BUS)
676 	return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
677 	    count, flags));
678     res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
679 	count, flags);
680 
681     /*
682      * XXX: If this is a request for a specific range, assume it is
683      * correct and pass it up to the parent.  What we probably want to
684      * do long-term is explicitly trust any firmware-configured
685      * resources during the initial bus scan on boot and then disable
686      * this after that.
687      */
688     if (res == NULL && start + count - 1 == end)
689 	res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
690 	    count, flags);
691     return (res);
692 }
693 
694 int
695 acpi_pcib_acpi_adjust_resource(device_t dev, device_t child,
696     struct resource *r, rman_res_t start, rman_res_t end)
697 {
698 	struct acpi_hpcib_softc *sc;
699 
700 	sc = device_get_softc(dev);
701 	if (rman_get_type(r) == PCI_RES_BUS)
702 		return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
703 		    end));
704 	return (pcib_host_res_adjust(&sc->ap_host_res, child, r, start, end));
705 }
706 
707 int
708 acpi_pcib_acpi_release_resource(device_t dev, device_t child,
709     struct resource *r)
710 {
711 	struct acpi_hpcib_softc *sc;
712 
713 	sc = device_get_softc(dev);
714 	if (rman_get_type(r) == PCI_RES_BUS)
715 		return (pci_domain_release_bus(sc->ap_segment, child, r));
716 	return (bus_generic_release_resource(dev, child, r));
717 }
718 
719 int
720 acpi_pcib_acpi_activate_resource(device_t dev, device_t child,
721     struct resource *r)
722 {
723 	struct acpi_hpcib_softc *sc;
724 
725 	sc = device_get_softc(dev);
726 	if (rman_get_type(r) == PCI_RES_BUS)
727 		return (pci_domain_activate_bus(sc->ap_segment, child, r));
728 	return (bus_generic_activate_resource(dev, child, r));
729 }
730 
731 int
732 acpi_pcib_acpi_deactivate_resource(device_t dev, device_t child,
733     struct resource *r)
734 {
735 	struct acpi_hpcib_softc *sc;
736 
737 	sc = device_get_softc(dev);
738 	if (rman_get_type(r) == PCI_RES_BUS)
739 		return (pci_domain_deactivate_bus(sc->ap_segment, child, r));
740 	return (bus_generic_deactivate_resource(dev, child, r));
741 }
742 
743 static int
744 acpi_pcib_request_feature(device_t pcib, device_t dev, enum pci_feature feature)
745 {
746 	uint32_t osc_ctl;
747 	struct acpi_hpcib_softc *sc;
748 
749 	sc = device_get_softc(pcib);
750 
751 	switch (feature) {
752 	case PCI_FEATURE_HP:
753 		osc_ctl = PCIM_OSC_CTL_PCIE_HP;
754 		break;
755 	case PCI_FEATURE_AER:
756 		osc_ctl = PCIM_OSC_CTL_PCIE_AER;
757 		break;
758 	default:
759 		return (EINVAL);
760 	}
761 
762 	return (acpi_pcib_osc(sc, osc_ctl));
763 }
764 
765 static bus_dma_tag_t
766 acpi_pcib_get_dma_tag(device_t bus, device_t child)
767 {
768 	struct acpi_hpcib_softc *sc;
769 
770 	sc = device_get_softc(bus);
771 
772 	return (sc->ap_dma_tag);
773 }
774