xref: /freebsd/sys/dev/acpica/acpi_pcib_acpi.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39 #include <sys/sysctl.h>
40 
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
43 
44 #include <dev/acpica/acpivar.h>
45 
46 #include <machine/pci_cfgreg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcib_private.h>
49 #include "pcib_if.h"
50 
51 #include <dev/acpica/acpi_pcibvar.h>
52 
53 /* Hooks for the ACPI CA debugging infrastructure. */
54 #define _COMPONENT	ACPI_BUS
55 ACPI_MODULE_NAME("PCI_ACPI")
56 
57 struct acpi_hpcib_softc {
58     device_t		ap_dev;
59     ACPI_HANDLE		ap_handle;
60     int			ap_flags;
61 
62     int			ap_segment;	/* PCI domain */
63     int			ap_bus;		/* bios-assigned bus number */
64     int			ap_addr;	/* device/func of PCI-Host bridge */
65 
66     ACPI_BUFFER		ap_prt;		/* interrupt routing table */
67 #ifdef NEW_PCIB
68     struct pcib_host_resources ap_host_res;
69 #endif
70 };
71 
72 static int		acpi_pcib_acpi_probe(device_t bus);
73 static int		acpi_pcib_acpi_attach(device_t bus);
74 static int		acpi_pcib_read_ivar(device_t dev, device_t child,
75 			    int which, uintptr_t *result);
76 static int		acpi_pcib_write_ivar(device_t dev, device_t child,
77 			    int which, uintptr_t value);
78 static uint32_t		acpi_pcib_read_config(device_t dev, u_int bus,
79 			    u_int slot, u_int func, u_int reg, int bytes);
80 static void		acpi_pcib_write_config(device_t dev, u_int bus,
81 			    u_int slot, u_int func, u_int reg, uint32_t data,
82 			    int bytes);
83 static int		acpi_pcib_acpi_route_interrupt(device_t pcib,
84 			    device_t dev, int pin);
85 static int		acpi_pcib_alloc_msi(device_t pcib, device_t dev,
86 			    int count, int maxcount, int *irqs);
87 static int		acpi_pcib_map_msi(device_t pcib, device_t dev,
88 			    int irq, uint64_t *addr, uint32_t *data);
89 static int		acpi_pcib_alloc_msix(device_t pcib, device_t dev,
90 			    int *irq);
91 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
92 			    device_t child, int type, int *rid,
93 			    u_long start, u_long end, u_long count,
94 			    u_int flags);
95 #ifdef NEW_PCIB
96 static int		acpi_pcib_acpi_adjust_resource(device_t dev,
97 			    device_t child, int type, struct resource *r,
98 			    u_long start, u_long end);
99 #endif
100 
101 static device_method_t acpi_pcib_acpi_methods[] = {
102     /* Device interface */
103     DEVMETHOD(device_probe,		acpi_pcib_acpi_probe),
104     DEVMETHOD(device_attach,		acpi_pcib_acpi_attach),
105     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
106     DEVMETHOD(device_suspend,		bus_generic_suspend),
107     DEVMETHOD(device_resume,		bus_generic_resume),
108 
109     /* Bus interface */
110     DEVMETHOD(bus_read_ivar,		acpi_pcib_read_ivar),
111     DEVMETHOD(bus_write_ivar,		acpi_pcib_write_ivar),
112     DEVMETHOD(bus_alloc_resource,	acpi_pcib_acpi_alloc_resource),
113 #ifdef NEW_PCIB
114     DEVMETHOD(bus_adjust_resource,	acpi_pcib_acpi_adjust_resource),
115 #else
116     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
117 #endif
118     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
119     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
120     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
121     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
122     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
123 
124     /* pcib interface */
125     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
126     DEVMETHOD(pcib_read_config,		acpi_pcib_read_config),
127     DEVMETHOD(pcib_write_config,	acpi_pcib_write_config),
128     DEVMETHOD(pcib_route_interrupt,	acpi_pcib_acpi_route_interrupt),
129     DEVMETHOD(pcib_alloc_msi,		acpi_pcib_alloc_msi),
130     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
131     DEVMETHOD(pcib_alloc_msix,		acpi_pcib_alloc_msix),
132     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
133     DEVMETHOD(pcib_map_msi,		acpi_pcib_map_msi),
134     DEVMETHOD(pcib_power_for_sleep,	acpi_pcib_power_for_sleep),
135 
136     DEVMETHOD_END
137 };
138 
139 static devclass_t pcib_devclass;
140 
141 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
142     sizeof(struct acpi_hpcib_softc));
143 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, pcib_devclass, 0, 0);
144 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
145 
146 static int
147 acpi_pcib_acpi_probe(device_t dev)
148 {
149     ACPI_DEVICE_INFO	*devinfo;
150     ACPI_HANDLE		h;
151     int			root;
152 
153     if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
154 	ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
155 	return (ENXIO);
156     root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
157     AcpiOsFree(devinfo);
158     if (!root || pci_cfgregopen() == 0)
159 	return (ENXIO);
160 
161     device_set_desc(dev, "ACPI Host-PCI bridge");
162     return (0);
163 }
164 
165 #ifdef NEW_PCIB
166 static ACPI_STATUS
167 acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
168 {
169 	struct acpi_hpcib_softc *sc;
170 	UINT64 length, min, max;
171 	u_int flags;
172 	int error, type;
173 
174 	sc = context;
175 	switch (res->Type) {
176 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
177 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
178 		panic("host bridge has depenedent resources");
179 	case ACPI_RESOURCE_TYPE_ADDRESS16:
180 	case ACPI_RESOURCE_TYPE_ADDRESS32:
181 	case ACPI_RESOURCE_TYPE_ADDRESS64:
182 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
183 		if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
184 			break;
185 		switch (res->Type) {
186 		case ACPI_RESOURCE_TYPE_ADDRESS16:
187 			min = res->Data.Address16.Minimum;
188 			max = res->Data.Address16.Maximum;
189 			length = res->Data.Address16.AddressLength;
190 			break;
191 		case ACPI_RESOURCE_TYPE_ADDRESS32:
192 			min = res->Data.Address32.Minimum;
193 			max = res->Data.Address32.Maximum;
194 			length = res->Data.Address32.AddressLength;
195 			break;
196 		case ACPI_RESOURCE_TYPE_ADDRESS64:
197 			min = res->Data.Address64.Minimum;
198 			max = res->Data.Address64.Maximum;
199 			length = res->Data.Address64.AddressLength;
200 			break;
201 		default:
202 			KASSERT(res->Type ==
203 			    ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
204 			    ("should never happen"));
205 			min = res->Data.ExtAddress64.Minimum;
206 			max = res->Data.ExtAddress64.Maximum;
207 			length = res->Data.ExtAddress64.AddressLength;
208 			break;
209 		}
210 		if (length == 0)
211 			break;
212 		if (min + length - 1 != max &&
213 		    (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
214 		    res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
215 			break;
216 		flags = 0;
217 		switch (res->Data.Address.ResourceType) {
218 		case ACPI_MEMORY_RANGE:
219 			type = SYS_RES_MEMORY;
220 			if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
221 				if (res->Data.Address.Info.Mem.Caching ==
222 				    ACPI_PREFETCHABLE_MEMORY)
223 					flags |= RF_PREFETCHABLE;
224 			} else {
225 				/*
226 				 * XXX: Parse prefetch flag out of
227 				 * TypeSpecific.
228 				 */
229 			}
230 			break;
231 		case ACPI_IO_RANGE:
232 			type = SYS_RES_IOPORT;
233 			break;
234 #ifdef PCI_RES_BUS
235 		case ACPI_BUS_NUMBER_RANGE:
236 			type = PCI_RES_BUS;
237 			break;
238 #endif
239 		default:
240 			return (AE_OK);
241 		}
242 
243 		if (min + length - 1 != max)
244 			device_printf(sc->ap_dev,
245 			    "Length mismatch for %d range: %jx vs %jx\n", type,
246 			    (uintmax_t)(max - min + 1), (uintmax_t)length);
247 #ifdef __i386__
248 		if (min > ULONG_MAX) {
249 			device_printf(sc->ap_dev,
250 			    "Ignoring %d range above 4GB (%#jx-%#jx)\n",
251 			    type, (uintmax_t)min, (uintmax_t)max);
252 			break;
253 		}
254 		if (max > ULONG_MAX) {
255 			device_printf(sc->ap_dev,
256        		    "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
257 			    type, (uintmax_t)min, (uintmax_t)max);
258 			max = ULONG_MAX;
259 		}
260 #endif
261 		error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
262 		    flags);
263 		if (error)
264 			panic("Failed to manage %d range (%#jx-%#jx): %d",
265 			    type, (uintmax_t)min, (uintmax_t)max, error);
266 		break;
267 	default:
268 		break;
269 	}
270 	return (AE_OK);
271 }
272 #endif
273 
274 static int
275 acpi_pcib_acpi_attach(device_t dev)
276 {
277     struct acpi_hpcib_softc	*sc;
278     ACPI_STATUS			status;
279     static int bus0_seen = 0;
280     u_int slot, func, busok;
281     uint8_t busno;
282 
283     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
284 
285     sc = device_get_softc(dev);
286     sc->ap_dev = dev;
287     sc->ap_handle = acpi_get_handle(dev);
288 
289     /*
290      * Don't attach if we're not really there.
291      */
292     if (!acpi_DeviceIsPresent(dev))
293 	return (ENXIO);
294 
295     /*
296      * Get our segment number by evaluating _SEG.
297      * It's OK for this to not exist.
298      */
299     status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
300     if (ACPI_FAILURE(status)) {
301 	if (status != AE_NOT_FOUND) {
302 	    device_printf(dev, "could not evaluate _SEG - %s\n",
303 		AcpiFormatException(status));
304 	    return_VALUE (ENXIO);
305 	}
306 	/* If it's not found, assume 0. */
307 	sc->ap_segment = 0;
308     }
309 
310     /*
311      * Get the address (device and function) of the associated
312      * PCI-Host bridge device from _ADR.  Assume we don't have one if
313      * it doesn't exist.
314      */
315     status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
316     if (ACPI_FAILURE(status)) {
317 	device_printf(dev, "could not evaluate _ADR - %s\n",
318 	    AcpiFormatException(status));
319 	sc->ap_addr = -1;
320     }
321 
322 #ifdef NEW_PCIB
323     /*
324      * Determine which address ranges this bridge decodes and setup
325      * resource managers for those ranges.
326      */
327     if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
328 	    panic("failed to init hostb resources");
329     if (!acpi_disabled("hostres")) {
330 	status = AcpiWalkResources(sc->ap_handle, "_CRS",
331 	    acpi_pcib_producer_handler, sc);
332 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
333 	    device_printf(sc->ap_dev, "failed to parse resources: %s\n",
334 		AcpiFormatException(status));
335     }
336 #endif
337 
338     /*
339      * Get our base bus number by evaluating _BBN.
340      * If this doesn't work, we assume we're bus number 0.
341      *
342      * XXX note that it may also not exist in the case where we are
343      *     meant to use a private configuration space mechanism for this bus,
344      *     so we should dig out our resources and check to see if we have
345      *     anything like that.  How do we do this?
346      * XXX If we have the requisite information, and if we don't think the
347      *     default PCI configuration space handlers can deal with this bus,
348      *     we should attach our own handler.
349      * XXX invoke _REG on this for the PCI config space address space?
350      * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
351      *     _BBN correctly.  They set _BBN to zero for all bridges.  Thus,
352      *     if _BBN is zero and PCI bus 0 already exists, we try to read our
353      *     bus number from the configuration registers at address _ADR.
354      *     We only do this for domain/segment 0 in the hopes that this is
355      *     only needed for old single-domain machines.
356      */
357     status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
358     if (ACPI_FAILURE(status)) {
359 	if (status != AE_NOT_FOUND) {
360 	    device_printf(dev, "could not evaluate _BBN - %s\n",
361 		AcpiFormatException(status));
362 	    return (ENXIO);
363 	} else {
364 	    /* If it's not found, assume 0. */
365 	    sc->ap_bus = 0;
366 	}
367     }
368 
369     /*
370      * If this is segment 0, the bus is zero, and PCI bus 0 already
371      * exists, read the bus number via PCI config space.
372      */
373     busok = 1;
374     if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
375 	busok = 0;
376 	if (sc->ap_addr != -1) {
377 	    /* XXX: We assume bus 0. */
378 	    slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
379 	    func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
380 	    if (bootverbose)
381 		device_printf(dev, "reading config registers from 0:%d:%d\n",
382 		    slot, func);
383 	    if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
384 		device_printf(dev, "couldn't read bus number from cfg space\n");
385 	    else {
386 		sc->ap_bus = busno;
387 		busok = 1;
388 	    }
389 	}
390     }
391 
392     /*
393      * If nothing else worked, hope that ACPI at least lays out the
394      * host-PCI bridges in order and that as a result our unit number
395      * is actually our bus number.  There are several reasons this
396      * might not be true.
397      */
398     if (busok == 0) {
399 	sc->ap_bus = device_get_unit(dev);
400 	device_printf(dev, "trying bus number %d\n", sc->ap_bus);
401     }
402 
403     /* If this is bus 0 on segment 0, note that it has been seen already. */
404     if (sc->ap_segment == 0 && sc->ap_bus == 0)
405 	    bus0_seen = 1;
406 
407     return (acpi_pcib_attach(dev, &sc->ap_prt, sc->ap_bus));
408 }
409 
410 /*
411  * Support for standard PCI bridge ivars.
412  */
413 static int
414 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
415 {
416     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
417 
418     switch (which) {
419     case PCIB_IVAR_DOMAIN:
420 	*result = sc->ap_segment;
421 	return (0);
422     case PCIB_IVAR_BUS:
423 	*result = sc->ap_bus;
424 	return (0);
425     case ACPI_IVAR_HANDLE:
426 	*result = (uintptr_t)sc->ap_handle;
427 	return (0);
428     case ACPI_IVAR_FLAGS:
429 	*result = (uintptr_t)sc->ap_flags;
430 	return (0);
431     }
432     return (ENOENT);
433 }
434 
435 static int
436 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
437 {
438     struct acpi_hpcib_softc	*sc = device_get_softc(dev);
439 
440     switch (which) {
441     case PCIB_IVAR_DOMAIN:
442 	return (EINVAL);
443     case PCIB_IVAR_BUS:
444 	sc->ap_bus = value;
445 	return (0);
446     case ACPI_IVAR_HANDLE:
447 	sc->ap_handle = (ACPI_HANDLE)value;
448 	return (0);
449     case ACPI_IVAR_FLAGS:
450 	sc->ap_flags = (int)value;
451 	return (0);
452     }
453     return (ENOENT);
454 }
455 
456 static uint32_t
457 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
458     u_int reg, int bytes)
459 {
460     return (pci_cfgregread(bus, slot, func, reg, bytes));
461 }
462 
463 static void
464 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
465     u_int reg, uint32_t data, int bytes)
466 {
467     pci_cfgregwrite(bus, slot, func, reg, data, bytes);
468 }
469 
470 static int
471 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
472 {
473     struct acpi_hpcib_softc *sc = device_get_softc(pcib);
474 
475     return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
476 }
477 
478 static int
479 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
480     int *irqs)
481 {
482 	device_t bus;
483 
484 	bus = device_get_parent(pcib);
485 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
486 	    irqs));
487 }
488 
489 static int
490 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
491 {
492 	device_t bus;
493 
494 	bus = device_get_parent(pcib);
495 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
496 }
497 
498 static int
499 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
500     uint32_t *data)
501 {
502 	struct acpi_hpcib_softc *sc;
503 	device_t bus, hostb;
504 	int error;
505 
506 	bus = device_get_parent(pcib);
507 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
508 	if (error)
509 		return (error);
510 
511 	sc = device_get_softc(pcib);
512 	if (sc->ap_addr == -1)
513 		return (0);
514 	/* XXX: Assumes all bridges are on bus 0. */
515 	hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
516 	    ACPI_ADR_PCI_FUNC(sc->ap_addr));
517 	if (hostb != NULL)
518 		pci_ht_map_msi(hostb, *addr);
519 	return (0);
520 }
521 
522 struct resource *
523 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
524     u_long start, u_long end, u_long count, u_int flags)
525 {
526 #ifdef NEW_PCIB
527     struct acpi_hpcib_softc *sc;
528     struct resource *res;
529 #endif
530 
531 #if defined(__i386__) || defined(__amd64__)
532     start = hostb_alloc_start(type, start, end, count);
533 #endif
534 
535 #ifdef NEW_PCIB
536     sc = device_get_softc(dev);
537     res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
538 	count, flags);
539 
540     /*
541      * XXX: If this is a request for a specific range, assume it is
542      * correct and pass it up to the parent.  What we probably want to
543      * do long-term is explicitly trust any firmware-configured
544      * resources during the initial bus scan on boot and then disable
545      * this after that.
546      */
547     if (res == NULL && start + count - 1 == end)
548 	res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
549 	    count, flags);
550     return (res);
551 #else
552     return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
553 	count, flags));
554 #endif
555 }
556 
557 #ifdef NEW_PCIB
558 int
559 acpi_pcib_acpi_adjust_resource(device_t dev, device_t child, int type,
560     struct resource *r, u_long start, u_long end)
561 {
562 	struct acpi_hpcib_softc *sc;
563 
564 	sc = device_get_softc(dev);
565 	return (pcib_host_res_adjust(&sc->ap_host_res, child, type, r, start,
566 	    end));
567 }
568 #endif
569