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