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
27 * $FreeBSD: head/sys/dev/acpica/acpi_resource.c 263975 2014-03-31 19:37:39Z imp $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/resource.h>
38 #include <sys/machintr.h>
43 #include <dev/acpica/acpivar.h>
45 /* Hooks for the ACPICA debugging infrastructure */
46 #define _COMPONENT ACPI_BUS
47 ACPI_MODULE_NAME("RESOURCE")
49 struct lookup_irq_request {
50 ACPI_RESOURCE *acpi_res;
58 acpi_lookup_irq_handler(ACPI_RESOURCE *res, void *context)
60 struct lookup_irq_request *req;
66 case ACPI_RESOURCE_TYPE_IRQ:
67 irqnum = res->Data.Irq.InterruptCount;
68 irq = res->Data.Irq.Interrupts[0];
69 len = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
71 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
72 irqnum = res->Data.ExtendedIrq.InterruptCount;
73 irq = res->Data.ExtendedIrq.Interrupts[0];
74 len = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
81 req = (struct lookup_irq_request *)context;
82 if (req->counter != req->rid) {
87 KASSERT(irq == rman_get_start(req->res),
88 ("IRQ resources do not match"));
89 bcopy(res, req->acpi_res, len);
90 return (AE_CTRL_TERMINATE);
94 acpi_lookup_irq_resource(device_t dev, int rid, struct resource *res,
95 ACPI_RESOURCE *acpi_res)
97 struct lookup_irq_request req;
100 req.acpi_res = acpi_res;
105 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
106 acpi_lookup_irq_handler, &req);
107 if (ACPI_SUCCESS(status) && req.found == 0)
108 status = AE_NOT_FOUND;
113 acpi_config_intr(device_t dev, ACPI_RESOURCE *res)
117 enum intr_trigger trigger;
118 enum intr_polarity polarity;
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(__x86_64__) || 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;
150 if (trig == ACPI_EDGE_SENSITIVE)
151 trigger = INTR_TRIGGER_EDGE;
153 trigger = INTR_TRIGGER_LEVEL;
155 if (pol == ACPI_ACTIVE_HIGH)
156 polarity = INTR_POLARITY_HIGH;
158 polarity = INTR_POLARITY_LOW;
160 if (machintr_legacy_intr_find(irq, trigger, polarity) < 0)
161 kprintf("%s: Skip irq %d config\n", __func__, irq);
163 BUS_CONFIG_INTR(dev, dev, irq, trigger, polarity);
166 struct acpi_resource_context {
167 struct acpi_parse_resource_set *set;
172 #ifdef ACPI_DEBUG_OUTPUT
174 acpi_address_range_name(UINT8 ResourceType)
178 switch (ResourceType) {
179 case ACPI_MEMORY_RANGE:
183 case ACPI_BUS_NUMBER_RANGE:
184 return ("Bus Number");
186 ksnprintf(buf, sizeof(buf), "type %u", ResourceType);
193 acpi_parse_resource(ACPI_RESOURCE *res, void *context)
195 struct acpi_parse_resource_set *set;
196 struct acpi_resource_context *arc;
197 UINT64 min, max, length, gran;
208 case ACPI_RESOURCE_TYPE_END_TAG:
209 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "EndTag\n"));
211 case ACPI_RESOURCE_TYPE_FIXED_IO:
212 if (res->Data.FixedIo.AddressLength <= 0)
214 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedIo 0x%x/%d\n",
215 res->Data.FixedIo.Address, res->Data.FixedIo.AddressLength));
216 set->set_ioport(dev, arc->context, res->Data.FixedIo.Address,
217 res->Data.FixedIo.AddressLength);
219 case ACPI_RESOURCE_TYPE_IO:
220 if (res->Data.Io.AddressLength <= 0)
222 if (res->Data.Io.Minimum == res->Data.Io.Maximum) {
223 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x/%d\n",
224 res->Data.Io.Minimum, res->Data.Io.AddressLength));
225 set->set_ioport(dev, arc->context, res->Data.Io.Minimum,
226 res->Data.Io.AddressLength);
228 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x-0x%x/%d\n",
229 res->Data.Io.Minimum, res->Data.Io.Maximum,
230 res->Data.Io.AddressLength));
231 set->set_iorange(dev, arc->context, res->Data.Io.Minimum,
232 res->Data.Io.Maximum, res->Data.Io.AddressLength,
233 res->Data.Io.Alignment);
236 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
237 if (res->Data.FixedMemory32.AddressLength <= 0)
239 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedMemory32 0x%x/%d\n",
240 res->Data.FixedMemory32.Address,
241 res->Data.FixedMemory32.AddressLength));
242 set->set_memory(dev, arc->context, res->Data.FixedMemory32.Address,
243 res->Data.FixedMemory32.AddressLength);
245 case ACPI_RESOURCE_TYPE_MEMORY32:
246 if (res->Data.Memory32.AddressLength <= 0)
248 if (res->Data.Memory32.Minimum == res->Data.Memory32.Maximum) {
249 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x/%d\n",
250 res->Data.Memory32.Minimum, res->Data.Memory32.AddressLength));
251 set->set_memory(dev, arc->context, res->Data.Memory32.Minimum,
252 res->Data.Memory32.AddressLength);
254 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x-0x%x/%d\n",
255 res->Data.Memory32.Minimum, res->Data.Memory32.Maximum,
256 res->Data.Memory32.AddressLength));
257 set->set_memoryrange(dev, arc->context, res->Data.Memory32.Minimum,
258 res->Data.Memory32.Maximum, res->Data.Memory32.AddressLength,
259 res->Data.Memory32.Alignment);
262 case ACPI_RESOURCE_TYPE_MEMORY24:
263 if (res->Data.Memory24.AddressLength <= 0)
265 if (res->Data.Memory24.Minimum == res->Data.Memory24.Maximum) {
266 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x/%d\n",
267 res->Data.Memory24.Minimum, res->Data.Memory24.AddressLength));
268 set->set_memory(dev, arc->context, res->Data.Memory24.Minimum,
269 res->Data.Memory24.AddressLength);
271 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x-0x%x/%d\n",
272 res->Data.Memory24.Minimum, res->Data.Memory24.Maximum,
273 res->Data.Memory24.AddressLength));
274 set->set_memoryrange(dev, arc->context, res->Data.Memory24.Minimum,
275 res->Data.Memory24.Maximum, res->Data.Memory24.AddressLength,
276 res->Data.Memory24.Alignment);
279 case ACPI_RESOURCE_TYPE_IRQ:
282 * "This structure is repeated for each separate interrupt
285 set->set_irq(dev, arc->context, res->Data.Irq.Interrupts,
286 res->Data.Irq.InterruptCount, res->Data.Irq.Triggering,
287 res->Data.Irq.Polarity);
289 case ACPI_RESOURCE_TYPE_DMA:
292 * "This structure is repeated for each separate DMA channel
295 set->set_drq(dev, arc->context, res->Data.Dma.Channels,
296 res->Data.Dma.ChannelCount);
298 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
299 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "start dependent functions\n"));
300 set->set_start_dependent(dev, arc->context,
301 res->Data.StartDpf.CompatibilityPriority);
303 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
304 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "end dependent functions\n"));
305 set->set_end_dependent(dev, arc->context);
307 case ACPI_RESOURCE_TYPE_ADDRESS16:
308 case ACPI_RESOURCE_TYPE_ADDRESS32:
309 case ACPI_RESOURCE_TYPE_ADDRESS64:
310 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
312 case ACPI_RESOURCE_TYPE_ADDRESS16:
313 gran = res->Data.Address16.Granularity;
314 min = res->Data.Address16.Minimum;
315 max = res->Data.Address16.Maximum;
316 length = res->Data.Address16.AddressLength;
321 case ACPI_RESOURCE_TYPE_ADDRESS32:
322 gran = res->Data.Address32.Granularity;
323 min = res->Data.Address32.Minimum;
324 max = res->Data.Address32.Maximum;
325 length = res->Data.Address32.AddressLength;
330 case ACPI_RESOURCE_TYPE_ADDRESS64:
331 gran = res->Data.Address64.Granularity;
332 min = res->Data.Address64.Minimum;
333 max = res->Data.Address64.Maximum;
334 length = res->Data.Address64.AddressLength;
340 KASSERT(res->Type == ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
341 ("should never happen"));
342 gran = res->Data.ExtAddress64.Granularity;
343 min = res->Data.ExtAddress64.Minimum;
344 max = res->Data.ExtAddress64.Maximum;
345 length = res->Data.ExtAddress64.AddressLength;
347 name = "ExtAddress64";
353 if (res->Data.Address.ProducerConsumer != ACPI_CONSUMER) {
354 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
355 "ignored %s %s producer\n", name,
356 acpi_address_range_name(res->Data.Address.ResourceType)));
359 if (res->Data.Address.ResourceType != ACPI_MEMORY_RANGE &&
360 res->Data.Address.ResourceType != ACPI_IO_RANGE) {
361 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
362 "ignored %s for non-memory, non-I/O\n", name));
367 if (min > ULONG_MAX || (res->Data.Address.MaxAddressFixed && max >
369 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored %s above 4G\n",
376 if (res->Data.Address.MinAddressFixed == ACPI_ADDRESS_FIXED &&
377 res->Data.Address.MaxAddressFixed == ACPI_ADDRESS_FIXED) {
378 if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE) {
379 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/Memory 0x%jx/%ju\n",
380 name, (uintmax_t)min, (uintmax_t)length));
381 set->set_memory(dev, arc->context, min, length);
383 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx/%ju\n", name,
384 (uintmax_t)min, (uintmax_t)length));
385 set->set_ioport(dev, arc->context, min, length);
388 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
389 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
390 "%s/Memory 0x%jx-0x%jx/%ju\n", name, (uintmax_t)min,
391 (uintmax_t)max, (uintmax_t)length));
392 set->set_memoryrange(dev, arc->context, min, max, length, gran);
394 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx-0x%jx/%ju\n",
395 name, (uintmax_t)min, (uintmax_t)max, (uintmax_t)length));
396 set->set_iorange(dev, arc->context, min, max, length, gran);
400 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
401 if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER) {
402 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored ExtIRQ producer\n"));
405 set->set_ext_irq(dev, arc->context, res->Data.ExtendedIrq.Interrupts,
406 res->Data.ExtendedIrq.InterruptCount,
407 res->Data.ExtendedIrq.Triggering, res->Data.ExtendedIrq.Polarity);
409 case ACPI_RESOURCE_TYPE_VENDOR:
410 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
411 "unimplemented VendorSpecific resource\n"));
420 * Fetch a device's resources and associate them with the device.
422 * Note that it might be nice to also locate ACPI-specific resource items, such
425 * We really need to split the resource-fetching code out from the
426 * resource-parsing code, since we may want to use the parsing
427 * code for _PRS someday.
430 acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
431 struct acpi_parse_resource_set *set, void *arg)
433 struct acpi_resource_context arc;
436 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
438 set->set_init(dev, arg, &arc.context);
441 status = AcpiWalkResources(handle, "_CRS", acpi_parse_resource, &arc);
442 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
443 kprintf("can't fetch resources for %s - %s\n",
444 acpi_name(handle), AcpiFormatException(status));
445 return_ACPI_STATUS (status);
447 set->set_done(dev, arc.context);
448 return_ACPI_STATUS (AE_OK);
452 * Resource-set vectors used to attach _CRS-derived resources
455 static void acpi_res_set_init(device_t dev, void *arg, void **context);
456 static void acpi_res_set_done(device_t dev, void *context);
457 static void acpi_res_set_ioport(device_t dev, void *context,
458 uint64_t base, uint64_t length);
459 static void acpi_res_set_iorange(device_t dev, void *context,
460 uint64_t low, uint64_t high,
461 uint64_t length, uint64_t align);
462 static void acpi_res_set_memory(device_t dev, void *context,
463 uint64_t base, uint64_t length);
464 static void acpi_res_set_memoryrange(device_t dev, void *context,
465 uint64_t low, uint64_t high,
466 uint64_t length, uint64_t align);
467 static void acpi_res_set_irq(device_t dev, void *context, uint8_t *irq,
468 int count, int trig, int pol);
469 static void acpi_res_set_ext_irq(device_t dev, void *context,
470 uint32_t *irq, int count, int trig, int pol);
471 static void acpi_res_set_drq(device_t dev, void *context, uint8_t *drq,
473 static void acpi_res_set_start_dependent(device_t dev, void *context,
475 static void acpi_res_set_end_dependent(device_t dev, void *context);
477 struct acpi_parse_resource_set acpi_res_parse_set = {
481 acpi_res_set_iorange,
483 acpi_res_set_memoryrange,
485 acpi_res_set_ext_irq,
487 acpi_res_set_start_dependent,
488 acpi_res_set_end_dependent
491 struct acpi_res_context {
500 acpi_res_set_init(device_t dev, void *arg, void **context)
502 struct acpi_res_context *cp;
504 if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) {
505 bzero(cp, sizeof(*cp));
512 acpi_res_set_done(device_t dev, void *context)
514 struct acpi_res_context *cp = (struct acpi_res_context *)context;
522 acpi_res_set_ioport(device_t dev, void *context, uint64_t base,
525 struct acpi_res_context *cp = (struct acpi_res_context *)context;
529 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length, -1);
533 acpi_res_set_iorange(device_t dev, void *context, uint64_t low,
534 uint64_t high, uint64_t length, uint64_t align)
536 struct acpi_res_context *cp = (struct acpi_res_context *)context;
540 device_printf(dev, "I/O range not supported\n");
544 acpi_res_set_memory(device_t dev, void *context, uint64_t base,
547 struct acpi_res_context *cp = (struct acpi_res_context *)context;
552 bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length, -1);
556 acpi_res_set_memoryrange(device_t dev, void *context, uint64_t low,
557 uint64_t high, uint64_t length, uint64_t align)
559 struct acpi_res_context *cp = (struct acpi_res_context *)context;
563 device_printf(dev, "memory range not supported\n");
567 acpi_res_set_irq(device_t dev, void *context, uint8_t *irq, int count,
570 struct acpi_res_context *cp = (struct acpi_res_context *)context;
572 if (cp == NULL || irq == NULL)
575 /* This implements no resource relocation. */
579 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1,
580 machintr_legacy_intr_cpuid(*irq));
584 acpi_res_set_ext_irq(device_t dev, void *context, uint32_t *irq, int count,
587 struct acpi_res_context *cp = (struct acpi_res_context *)context;
589 if (cp == NULL || irq == NULL)
592 /* This implements no resource relocation. */
596 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1,
597 machintr_legacy_intr_cpuid(*irq));
601 acpi_res_set_drq(device_t dev, void *context, uint8_t *drq, int count)
603 struct acpi_res_context *cp = (struct acpi_res_context *)context;
605 if (cp == NULL || drq == NULL)
608 /* This implements no resource relocation. */
612 bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1, -1);
616 acpi_res_set_start_dependent(device_t dev, void *context, int preference)
618 struct acpi_res_context *cp = (struct acpi_res_context *)context;
622 device_printf(dev, "dependent functions not supported\n");
626 acpi_res_set_end_dependent(device_t dev, void *context)
628 struct acpi_res_context *cp = (struct acpi_res_context *)context;
632 device_printf(dev, "dependent functions not supported\n");
636 * Resource-owning placeholders for IO and memory pseudo-devices.
638 * This code allocates system resources that will be used by ACPI
639 * child devices. The acpi parent manages these resources through a
643 static int acpi_sysres_rid = 100;
645 static int acpi_sysres_probe(device_t dev);
646 static int acpi_sysres_attach(device_t dev);
648 static device_method_t acpi_sysres_methods[] = {
649 /* Device interface */
650 DEVMETHOD(device_probe, acpi_sysres_probe),
651 DEVMETHOD(device_attach, acpi_sysres_attach),
656 static driver_t acpi_sysres_driver = {
662 static devclass_t acpi_sysres_devclass;
663 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass,
665 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
668 acpi_sysres_probe(device_t dev)
670 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
672 if (acpi_disabled("sysresource") ||
673 ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids) == NULL)
676 device_set_desc(dev, "System Resource");
678 return (BUS_PROBE_DEFAULT);
682 acpi_sysres_attach(device_t dev)
685 struct resource_list_entry *bus_rle, *dev_rle;
686 struct resource_list *bus_rl, *dev_rl;
688 u_long start, end, count;
691 * Loop through all current resources to see if the new one overlaps
692 * any existing ones. If so, grow the old one up and/or down
693 * accordingly. Discard any that are wholly contained in the old. If
694 * the resource is unique, add it to the parent. It will later go into
697 bus = device_get_parent(dev);
698 dev_rl = BUS_GET_RESOURCE_LIST(bus, dev);
699 bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus);
701 kprintf("busrl is not null!\n");
702 SLIST_FOREACH(dev_rle, dev_rl, link) {
703 if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY)
706 start = dev_rle->start;
708 count = dev_rle->count;
709 type = dev_rle->type;
712 SLIST_FOREACH(bus_rle, bus_rl, link) {
713 if (bus_rle->type != type)
716 /* New resource wholly contained in old, discard. */
717 if (start >= bus_rle->start && end <= bus_rle->end)
720 /* New tail overlaps old head, grow existing resource downward. */
721 if (start < bus_rle->start && end >= bus_rle->start) {
722 bus_rle->count += bus_rle->start - start;
723 bus_rle->start = start;
727 /* New head overlaps old tail, grow existing resource upward. */
728 if (start <= bus_rle->end && end > bus_rle->end) {
729 bus_rle->count += end - bus_rle->end;
734 /* If we adjusted the old resource, we're finished. */
741 /* If we didn't merge with anything, add this resource. */
743 bus_set_resource(bus, type, acpi_sysres_rid++, start, count, -1);
746 /* After merging/moving resources to the parent, free the list. */
747 resource_list_free(dev_rl);