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