2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
43 #include <contrib/dev/acpica/include/acpi.h>
44 #include <contrib/dev/acpica/include/accommon.h>
46 #include <dev/acpica/acpivar.h>
48 /* Hooks for the ACPI CA debugging infrastructure */
49 #define _COMPONENT ACPI_BUS
50 ACPI_MODULE_NAME("RESOURCE")
52 struct lookup_irq_request {
53 ACPI_RESOURCE *acpi_res;
61 acpi_lookup_irq_handler(ACPI_RESOURCE *res, void *context)
63 struct lookup_irq_request *req;
68 case ACPI_RESOURCE_TYPE_IRQ:
69 irqnum = res->Data.Irq.InterruptCount;
70 irq = res->Data.Irq.Interrupts[0];
71 len = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
73 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
74 irqnum = res->Data.ExtendedIrq.InterruptCount;
75 irq = res->Data.ExtendedIrq.Interrupts[0];
76 len = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
83 req = (struct lookup_irq_request *)context;
84 if (req->counter != req->rid) {
89 KASSERT(irq == rman_get_start(req->res),
90 ("IRQ resources do not match"));
91 bcopy(res, req->acpi_res, len);
92 return (AE_CTRL_TERMINATE);
96 acpi_lookup_irq_resource(device_t dev, int rid, struct resource *res,
97 ACPI_RESOURCE *acpi_res)
99 struct lookup_irq_request req;
102 req.acpi_res = acpi_res;
107 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
108 acpi_lookup_irq_handler, &req);
109 if (ACPI_SUCCESS(status) && req.found == 0)
110 status = AE_NOT_FOUND;
115 acpi_config_intr(device_t dev, ACPI_RESOURCE *res)
121 case ACPI_RESOURCE_TYPE_IRQ:
122 KASSERT(res->Data.Irq.InterruptCount == 1,
123 ("%s: multiple interrupts", __func__));
124 irq = res->Data.Irq.Interrupts[0];
125 trig = res->Data.Irq.Triggering;
126 pol = res->Data.Irq.Polarity;
128 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
129 KASSERT(res->Data.ExtendedIrq.InterruptCount == 1,
130 ("%s: multiple interrupts", __func__));
131 irq = res->Data.ExtendedIrq.Interrupts[0];
132 trig = res->Data.ExtendedIrq.Triggering;
133 pol = res->Data.ExtendedIrq.Polarity;
136 panic("%s: bad resource type %u", __func__, res->Type);
139 #if defined(__amd64__) || defined(__i386__)
141 * XXX: Certain BIOSes have buggy AML that specify an IRQ that is
142 * edge-sensitive and active-lo. However, edge-sensitive IRQs
143 * should be active-hi. Force IRQs with an ISA IRQ value to be
146 if (irq < 16 && trig == ACPI_EDGE_SENSITIVE && pol == ACPI_ACTIVE_LOW)
147 pol = ACPI_ACTIVE_HIGH;
149 BUS_CONFIG_INTR(dev, irq, (trig == ACPI_EDGE_SENSITIVE) ?
150 INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL, (pol == ACPI_ACTIVE_HIGH) ?
151 INTR_POLARITY_HIGH : INTR_POLARITY_LOW);
154 struct acpi_resource_context {
155 struct acpi_parse_resource_set *set;
160 #ifdef ACPI_DEBUG_OUTPUT
162 acpi_address_range_name(UINT8 ResourceType)
166 switch (ResourceType) {
167 case ACPI_MEMORY_RANGE:
171 case ACPI_BUS_NUMBER_RANGE:
172 return ("Bus Number");
174 snprintf(buf, sizeof(buf), "type %u", ResourceType);
181 acpi_parse_resource(ACPI_RESOURCE *res, void *context)
183 struct acpi_parse_resource_set *set;
184 struct acpi_resource_context *arc;
185 UINT64 min, max, length, gran;
196 case ACPI_RESOURCE_TYPE_END_TAG:
197 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "EndTag\n"));
199 case ACPI_RESOURCE_TYPE_FIXED_IO:
200 if (res->Data.FixedIo.AddressLength <= 0)
202 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedIo 0x%x/%d\n",
203 res->Data.FixedIo.Address, res->Data.FixedIo.AddressLength));
204 set->set_ioport(dev, arc->context, res->Data.FixedIo.Address,
205 res->Data.FixedIo.AddressLength);
207 case ACPI_RESOURCE_TYPE_IO:
208 if (res->Data.Io.AddressLength <= 0)
210 if (res->Data.Io.Minimum == res->Data.Io.Maximum) {
211 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x/%d\n",
212 res->Data.Io.Minimum, res->Data.Io.AddressLength));
213 set->set_ioport(dev, arc->context, res->Data.Io.Minimum,
214 res->Data.Io.AddressLength);
216 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x-0x%x/%d\n",
217 res->Data.Io.Minimum, res->Data.Io.Maximum,
218 res->Data.Io.AddressLength));
219 set->set_iorange(dev, arc->context, res->Data.Io.Minimum,
220 res->Data.Io.Maximum, res->Data.Io.AddressLength,
221 res->Data.Io.Alignment);
224 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
225 if (res->Data.FixedMemory32.AddressLength <= 0)
227 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedMemory32 0x%x/%d\n",
228 res->Data.FixedMemory32.Address,
229 res->Data.FixedMemory32.AddressLength));
230 set->set_memory(dev, arc->context, res->Data.FixedMemory32.Address,
231 res->Data.FixedMemory32.AddressLength);
233 case ACPI_RESOURCE_TYPE_MEMORY32:
234 if (res->Data.Memory32.AddressLength <= 0)
236 if (res->Data.Memory32.Minimum == res->Data.Memory32.Maximum) {
237 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x/%d\n",
238 res->Data.Memory32.Minimum, res->Data.Memory32.AddressLength));
239 set->set_memory(dev, arc->context, res->Data.Memory32.Minimum,
240 res->Data.Memory32.AddressLength);
242 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x-0x%x/%d\n",
243 res->Data.Memory32.Minimum, res->Data.Memory32.Maximum,
244 res->Data.Memory32.AddressLength));
245 set->set_memoryrange(dev, arc->context, res->Data.Memory32.Minimum,
246 res->Data.Memory32.Maximum, res->Data.Memory32.AddressLength,
247 res->Data.Memory32.Alignment);
250 case ACPI_RESOURCE_TYPE_MEMORY24:
251 if (res->Data.Memory24.AddressLength <= 0)
253 if (res->Data.Memory24.Minimum == res->Data.Memory24.Maximum) {
254 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x/%d\n",
255 res->Data.Memory24.Minimum, res->Data.Memory24.AddressLength));
256 set->set_memory(dev, arc->context, res->Data.Memory24.Minimum,
257 res->Data.Memory24.AddressLength);
259 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x-0x%x/%d\n",
260 res->Data.Memory24.Minimum, res->Data.Memory24.Maximum,
261 res->Data.Memory24.AddressLength));
262 set->set_memoryrange(dev, arc->context, res->Data.Memory24.Minimum,
263 res->Data.Memory24.Maximum, res->Data.Memory24.AddressLength,
264 res->Data.Memory24.Alignment);
267 case ACPI_RESOURCE_TYPE_IRQ:
270 * "This structure is repeated for each separate interrupt
273 set->set_irq(dev, arc->context, res->Data.Irq.Interrupts,
274 res->Data.Irq.InterruptCount, res->Data.Irq.Triggering,
275 res->Data.Irq.Polarity);
277 case ACPI_RESOURCE_TYPE_DMA:
280 * "This structure is repeated for each separate DMA channel
283 set->set_drq(dev, arc->context, res->Data.Dma.Channels,
284 res->Data.Dma.ChannelCount);
286 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
287 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "start dependent functions\n"));
288 set->set_start_dependent(dev, arc->context,
289 res->Data.StartDpf.CompatibilityPriority);
291 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
292 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "end dependent functions\n"));
293 set->set_end_dependent(dev, arc->context);
295 case ACPI_RESOURCE_TYPE_ADDRESS16:
296 case ACPI_RESOURCE_TYPE_ADDRESS32:
297 case ACPI_RESOURCE_TYPE_ADDRESS64:
298 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
300 case ACPI_RESOURCE_TYPE_ADDRESS16:
301 gran = res->Data.Address16.Address.Granularity;
302 min = res->Data.Address16.Address.Minimum;
303 max = res->Data.Address16.Address.Maximum;
304 length = res->Data.Address16.Address.AddressLength;
309 case ACPI_RESOURCE_TYPE_ADDRESS32:
310 gran = res->Data.Address32.Address.Granularity;
311 min = res->Data.Address32.Address.Minimum;
312 max = res->Data.Address32.Address.Maximum;
313 length = res->Data.Address32.Address.AddressLength;
318 case ACPI_RESOURCE_TYPE_ADDRESS64:
319 gran = res->Data.Address64.Address.Granularity;
320 min = res->Data.Address64.Address.Minimum;
321 max = res->Data.Address64.Address.Maximum;
322 length = res->Data.Address64.Address.AddressLength;
328 KASSERT(res->Type == ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
329 ("should never happen"));
330 gran = res->Data.ExtAddress64.Address.Granularity;
331 min = res->Data.ExtAddress64.Address.Minimum;
332 max = res->Data.ExtAddress64.Address.Maximum;
333 length = res->Data.ExtAddress64.Address.AddressLength;
335 name = "ExtAddress64";
341 if (res->Data.Address.ProducerConsumer != ACPI_CONSUMER) {
342 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
343 "ignored %s %s producer\n", name,
344 acpi_address_range_name(res->Data.Address.ResourceType)));
347 if (res->Data.Address.ResourceType != ACPI_MEMORY_RANGE &&
348 res->Data.Address.ResourceType != ACPI_IO_RANGE) {
349 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
350 "ignored %s for non-memory, non-I/O\n", name));
355 if (min > ULONG_MAX || (res->Data.Address.MaxAddressFixed && max >
357 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored %s above 4G\n",
364 if (res->Data.Address.MinAddressFixed == ACPI_ADDRESS_FIXED &&
365 res->Data.Address.MaxAddressFixed == ACPI_ADDRESS_FIXED) {
366 if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE) {
367 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/Memory 0x%jx/%ju\n",
368 name, (uintmax_t)min, (uintmax_t)length));
369 set->set_memory(dev, arc->context, min, length);
371 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx/%ju\n", name,
372 (uintmax_t)min, (uintmax_t)length));
373 set->set_ioport(dev, arc->context, min, length);
376 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
377 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
378 "%s/Memory 0x%jx-0x%jx/%ju\n", name, (uintmax_t)min,
379 (uintmax_t)max, (uintmax_t)length));
380 set->set_memoryrange(dev, arc->context, min, max, length, gran);
382 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx-0x%jx/%ju\n",
383 name, (uintmax_t)min, (uintmax_t)max, (uintmax_t)length));
384 set->set_iorange(dev, arc->context, min, max, length, gran);
388 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
389 if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER) {
390 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored ExtIRQ producer\n"));
393 set->set_ext_irq(dev, arc->context, res->Data.ExtendedIrq.Interrupts,
394 res->Data.ExtendedIrq.InterruptCount,
395 res->Data.ExtendedIrq.Triggering, res->Data.ExtendedIrq.Polarity);
397 case ACPI_RESOURCE_TYPE_VENDOR:
398 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
399 "unimplemented VendorSpecific resource\n"));
408 * Fetch a device's resources and associate them with the device.
410 * Note that it might be nice to also locate ACPI-specific resource items, such
413 * We really need to split the resource-fetching code out from the
414 * resource-parsing code, since we may want to use the parsing
415 * code for _PRS someday.
418 acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
419 struct acpi_parse_resource_set *set, void *arg)
421 struct acpi_resource_context arc;
424 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
426 set->set_init(dev, arg, &arc.context);
429 status = AcpiWalkResources(handle, "_CRS", acpi_parse_resource, &arc);
430 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
431 printf("can't fetch resources for %s - %s\n",
432 acpi_name(handle), AcpiFormatException(status));
433 return_ACPI_STATUS (status);
435 set->set_done(dev, arc.context);
436 return_ACPI_STATUS (AE_OK);
440 * Resource-set vectors used to attach _CRS-derived resources
443 static void acpi_res_set_init(device_t dev, void *arg, void **context);
444 static void acpi_res_set_done(device_t dev, void *context);
445 static void acpi_res_set_ioport(device_t dev, void *context,
446 uint64_t base, uint64_t length);
447 static void acpi_res_set_iorange(device_t dev, void *context,
448 uint64_t low, uint64_t high,
449 uint64_t length, uint64_t align);
450 static void acpi_res_set_memory(device_t dev, void *context,
451 uint64_t base, uint64_t length);
452 static void acpi_res_set_memoryrange(device_t dev, void *context,
453 uint64_t low, uint64_t high,
454 uint64_t length, uint64_t align);
455 static void acpi_res_set_irq(device_t dev, void *context, uint8_t *irq,
456 int count, int trig, int pol);
457 static void acpi_res_set_ext_irq(device_t dev, void *context,
458 uint32_t *irq, int count, int trig, int pol);
459 static void acpi_res_set_drq(device_t dev, void *context, uint8_t *drq,
461 static void acpi_res_set_start_dependent(device_t dev, void *context,
463 static void acpi_res_set_end_dependent(device_t dev, void *context);
465 struct acpi_parse_resource_set acpi_res_parse_set = {
469 acpi_res_set_iorange,
471 acpi_res_set_memoryrange,
473 acpi_res_set_ext_irq,
475 acpi_res_set_start_dependent,
476 acpi_res_set_end_dependent
479 struct acpi_res_context {
488 acpi_res_set_init(device_t dev, void *arg, void **context)
490 struct acpi_res_context *cp;
492 if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) {
493 bzero(cp, sizeof(*cp));
500 acpi_res_set_done(device_t dev, void *context)
502 struct acpi_res_context *cp = (struct acpi_res_context *)context;
510 acpi_res_set_ioport(device_t dev, void *context, uint64_t base,
513 struct acpi_res_context *cp = (struct acpi_res_context *)context;
517 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length);
521 acpi_res_set_iorange(device_t dev, void *context, uint64_t low,
522 uint64_t high, uint64_t length, uint64_t align)
524 struct acpi_res_context *cp = (struct acpi_res_context *)context;
528 device_printf(dev, "I/O range not supported\n");
532 acpi_res_set_memory(device_t dev, void *context, uint64_t base,
535 struct acpi_res_context *cp = (struct acpi_res_context *)context;
540 bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length);
544 acpi_res_set_memoryrange(device_t dev, void *context, uint64_t low,
545 uint64_t high, uint64_t length, uint64_t align)
547 struct acpi_res_context *cp = (struct acpi_res_context *)context;
551 device_printf(dev, "memory range not supported\n");
555 acpi_res_set_irq(device_t dev, void *context, uint8_t *irq, int count,
558 struct acpi_res_context *cp = (struct acpi_res_context *)context;
560 if (cp == NULL || irq == NULL)
563 /* This implements no resource relocation. */
567 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1);
571 acpi_res_set_ext_irq(device_t dev, void *context, uint32_t *irq, int count,
574 struct acpi_res_context *cp = (struct acpi_res_context *)context;
576 if (cp == NULL || irq == NULL)
579 /* This implements no resource relocation. */
583 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1);
587 acpi_res_set_drq(device_t dev, void *context, uint8_t *drq, int count)
589 struct acpi_res_context *cp = (struct acpi_res_context *)context;
591 if (cp == NULL || drq == NULL)
594 /* This implements no resource relocation. */
598 bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1);
602 acpi_res_set_start_dependent(device_t dev, void *context, int preference)
604 struct acpi_res_context *cp = (struct acpi_res_context *)context;
608 device_printf(dev, "dependent functions not supported\n");
612 acpi_res_set_end_dependent(device_t dev, void *context)
614 struct acpi_res_context *cp = (struct acpi_res_context *)context;
618 device_printf(dev, "dependent functions not supported\n");
622 * Resource-owning placeholders for IO and memory pseudo-devices.
624 * This code allocates system resources that will be used by ACPI
625 * child devices. The acpi parent manages these resources through a
629 static int acpi_sysres_rid = 100;
631 static int acpi_sysres_probe(device_t dev);
632 static int acpi_sysres_attach(device_t dev);
634 static device_method_t acpi_sysres_methods[] = {
635 /* Device interface */
636 DEVMETHOD(device_probe, acpi_sysres_probe),
637 DEVMETHOD(device_attach, acpi_sysres_attach),
642 static driver_t acpi_sysres_driver = {
648 static devclass_t acpi_sysres_devclass;
649 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass,
651 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
654 acpi_sysres_probe(device_t dev)
656 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
658 if (acpi_disabled("sysresource") ||
659 ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids) == NULL)
662 device_set_desc(dev, "System Resource");
664 return (BUS_PROBE_DEFAULT);
668 acpi_sysres_attach(device_t dev)
671 struct resource_list_entry *bus_rle, *dev_rle;
672 struct resource_list *bus_rl, *dev_rl;
674 rman_res_t start, end, count;
677 * Loop through all current resources to see if the new one overlaps
678 * any existing ones. If so, grow the old one up and/or down
679 * accordingly. Discard any that are wholly contained in the old. If
680 * the resource is unique, add it to the parent. It will later go into
683 bus = device_get_parent(dev);
684 dev_rl = BUS_GET_RESOURCE_LIST(bus, dev);
685 bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus);
686 STAILQ_FOREACH(dev_rle, dev_rl, link) {
687 if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY)
690 start = dev_rle->start;
692 count = dev_rle->count;
693 type = dev_rle->type;
696 STAILQ_FOREACH(bus_rle, bus_rl, link) {
697 if (bus_rle->type != type)
700 /* New resource wholly contained in old, discard. */
701 if (start >= bus_rle->start && end <= bus_rle->end)
704 /* New tail overlaps old head, grow existing resource downward. */
705 if (start < bus_rle->start && end >= bus_rle->start) {
706 bus_rle->count += bus_rle->start - start;
707 bus_rle->start = start;
711 /* New head overlaps old tail, grow existing resource upward. */
712 if (start <= bus_rle->end && end > bus_rle->end) {
713 bus_rle->count += end - bus_rle->end;
718 /* If we adjusted the old resource, we're finished. */
723 /* If we didn't merge with anything, add this resource. */
725 bus_set_resource(bus, type, acpi_sysres_rid++, start, count);
728 /* After merging/moving resources to the parent, free the list. */
729 resource_list_free(dev_rl);