xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision 53bb5613a8a15363718b6e6de8d965bf9a2c5469)
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_generic_probe(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     return (bus_generic_attach(dev));
539 
540 errout:
541     device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
542     pcib_host_res_free(dev, &sc->ap_host_res);
543     return (error);
544 }
545 
546 /*
547  * Support for standard PCI bridge ivars.
548  */
549 static int
550 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
551 {
552     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
553 
554     switch (which) {
555     case PCIB_IVAR_DOMAIN:
556 	*result = sc->ap_segment;
557 	return (0);
558     case PCIB_IVAR_BUS:
559 	*result = sc->ap_bus;
560 	return (0);
561     case ACPI_IVAR_HANDLE:
562 	*result = (uintptr_t)sc->ap_handle;
563 	return (0);
564     case ACPI_IVAR_FLAGS:
565 	*result = (uintptr_t)sc->ap_flags;
566 	return (0);
567     }
568     return (ENOENT);
569 }
570 
571 static int
572 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
573 {
574     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
575 
576     switch (which) {
577     case PCIB_IVAR_DOMAIN:
578 	return (EINVAL);
579     case PCIB_IVAR_BUS:
580 	sc->ap_bus = value;
581 	return (0);
582     case ACPI_IVAR_HANDLE:
583 	sc->ap_handle = (ACPI_HANDLE)value;
584 	return (0);
585     case ACPI_IVAR_FLAGS:
586 	sc->ap_flags = (int)value;
587 	return (0);
588     }
589     return (ENOENT);
590 }
591 
592 static uint32_t
593 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
594     u_int reg, int bytes)
595 {
596     struct acpi_hpcib_softc *sc = device_get_softc(dev);
597 
598     return (pci_cfgregread(sc->ap_segment, bus, slot, func, reg, bytes));
599 }
600 
601 static void
602 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
603     u_int reg, uint32_t data, int bytes)
604 {
605     struct acpi_hpcib_softc *sc = device_get_softc(dev);
606 
607     pci_cfgregwrite(sc->ap_segment, bus, slot, func, reg, data, bytes);
608 }
609 
610 static int
611 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
612 {
613     struct acpi_hpcib_softc *sc = device_get_softc(pcib);
614 
615     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
616 }
617 
618 static int
619 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
620     int *irqs)
621 {
622 	device_t bus;
623 
624 	bus = device_get_parent(pcib);
625 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
626 	    irqs));
627 }
628 
629 static int
630 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
631 {
632 	device_t bus;
633 
634 	bus = device_get_parent(pcib);
635 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
636 }
637 
638 static int
639 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
640     uint32_t *data)
641 {
642 	struct acpi_hpcib_softc *sc;
643 	device_t bus, hostb;
644 	int error;
645 
646 	bus = device_get_parent(pcib);
647 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
648 	if (error)
649 		return (error);
650 
651 	sc = device_get_softc(pcib);
652 	if (sc->ap_addr == -1)
653 		return (0);
654 	/* XXX: Assumes all bridges are on bus 0. */
655 	hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
656 	    ACPI_ADR_PCI_FUNC(sc->ap_addr));
657 	if (hostb != NULL)
658 		pci_ht_map_msi(hostb, *addr);
659 	return (0);
660 }
661 
662 struct resource *
663 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
664     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
665 {
666     struct acpi_hpcib_softc *sc;
667     struct resource *res;
668 
669 #if defined(__i386__) || defined(__amd64__)
670     start = hostb_alloc_start(type, start, end, count);
671 #endif
672 
673     sc = device_get_softc(dev);
674     if (type == PCI_RES_BUS)
675 	return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
676 	    count, flags));
677     res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
678 	count, flags);
679 
680     /*
681      * XXX: If this is a request for a specific range, assume it is
682      * correct and pass it up to the parent.  What we probably want to
683      * do long-term is explicitly trust any firmware-configured
684      * resources during the initial bus scan on boot and then disable
685      * this after that.
686      */
687     if (res == NULL && start + count - 1 == end)
688 	res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
689 	    count, flags);
690     return (res);
691 }
692 
693 int
694 acpi_pcib_acpi_adjust_resource(device_t dev, device_t child,
695     struct resource *r, rman_res_t start, rman_res_t end)
696 {
697 	struct acpi_hpcib_softc *sc;
698 
699 	sc = device_get_softc(dev);
700 	if (rman_get_type(r) == PCI_RES_BUS)
701 		return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
702 		    end));
703 	return (pcib_host_res_adjust(&sc->ap_host_res, child, r, start, end));
704 }
705 
706 int
707 acpi_pcib_acpi_release_resource(device_t dev, device_t child,
708     struct resource *r)
709 {
710 	struct acpi_hpcib_softc *sc;
711 
712 	sc = device_get_softc(dev);
713 	if (rman_get_type(r) == PCI_RES_BUS)
714 		return (pci_domain_release_bus(sc->ap_segment, child, r));
715 	return (bus_generic_release_resource(dev, child, r));
716 }
717 
718 int
719 acpi_pcib_acpi_activate_resource(device_t dev, device_t child,
720     struct resource *r)
721 {
722 	struct acpi_hpcib_softc *sc;
723 
724 	sc = device_get_softc(dev);
725 	if (rman_get_type(r) == PCI_RES_BUS)
726 		return (pci_domain_activate_bus(sc->ap_segment, child, r));
727 	return (bus_generic_activate_resource(dev, child, r));
728 }
729 
730 int
731 acpi_pcib_acpi_deactivate_resource(device_t dev, device_t child,
732     struct resource *r)
733 {
734 	struct acpi_hpcib_softc *sc;
735 
736 	sc = device_get_softc(dev);
737 	if (rman_get_type(r) == PCI_RES_BUS)
738 		return (pci_domain_deactivate_bus(sc->ap_segment, child, r));
739 	return (bus_generic_deactivate_resource(dev, child, r));
740 }
741 
742 static int
743 acpi_pcib_request_feature(device_t pcib, device_t dev, enum pci_feature feature)
744 {
745 	uint32_t osc_ctl;
746 	struct acpi_hpcib_softc *sc;
747 
748 	sc = device_get_softc(pcib);
749 
750 	switch (feature) {
751 	case PCI_FEATURE_HP:
752 		osc_ctl = PCIM_OSC_CTL_PCIE_HP;
753 		break;
754 	case PCI_FEATURE_AER:
755 		osc_ctl = PCIM_OSC_CTL_PCIE_AER;
756 		break;
757 	default:
758 		return (EINVAL);
759 	}
760 
761 	return (acpi_pcib_osc(sc, osc_ctl));
762 }
763 
764 static bus_dma_tag_t
765 acpi_pcib_get_dma_tag(device_t bus, device_t child)
766 {
767 	struct acpi_hpcib_softc *sc;
768 
769 	sc = device_get_softc(bus);
770 
771 	return (sc->ap_dma_tag);
772 }
773