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;
194 case ACPI_RESOURCE_TYPE_END_TAG:
195 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "EndTag\n"));
197 case ACPI_RESOURCE_TYPE_FIXED_IO:
198 if (res->Data.FixedIo.AddressLength <= 0)
200 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedIo 0x%x/%d\n",
201 res->Data.FixedIo.Address, res->Data.FixedIo.AddressLength));
202 set->set_ioport(dev, arc->context, res->Data.FixedIo.Address,
203 res->Data.FixedIo.AddressLength);
205 case ACPI_RESOURCE_TYPE_IO:
206 if (res->Data.Io.AddressLength <= 0)
208 if (res->Data.Io.Minimum == res->Data.Io.Maximum) {
209 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x/%d\n",
210 res->Data.Io.Minimum, res->Data.Io.AddressLength));
211 set->set_ioport(dev, arc->context, res->Data.Io.Minimum,
212 res->Data.Io.AddressLength);
214 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x-0x%x/%d\n",
215 res->Data.Io.Minimum, res->Data.Io.Maximum,
216 res->Data.Io.AddressLength));
217 set->set_iorange(dev, arc->context, res->Data.Io.Minimum,
218 res->Data.Io.Maximum, res->Data.Io.AddressLength,
219 res->Data.Io.Alignment);
222 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
223 if (res->Data.FixedMemory32.AddressLength <= 0)
225 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedMemory32 0x%x/%d\n",
226 res->Data.FixedMemory32.Address,
227 res->Data.FixedMemory32.AddressLength));
228 set->set_memory(dev, arc->context, res->Data.FixedMemory32.Address,
229 res->Data.FixedMemory32.AddressLength);
231 case ACPI_RESOURCE_TYPE_MEMORY32:
232 if (res->Data.Memory32.AddressLength <= 0)
234 if (res->Data.Memory32.Minimum == res->Data.Memory32.Maximum) {
235 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x/%d\n",
236 res->Data.Memory32.Minimum, res->Data.Memory32.AddressLength));
237 set->set_memory(dev, arc->context, res->Data.Memory32.Minimum,
238 res->Data.Memory32.AddressLength);
240 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x-0x%x/%d\n",
241 res->Data.Memory32.Minimum, res->Data.Memory32.Maximum,
242 res->Data.Memory32.AddressLength));
243 set->set_memoryrange(dev, arc->context, res->Data.Memory32.Minimum,
244 res->Data.Memory32.Maximum, res->Data.Memory32.AddressLength,
245 res->Data.Memory32.Alignment);
248 case ACPI_RESOURCE_TYPE_MEMORY24:
249 if (res->Data.Memory24.AddressLength <= 0)
251 if (res->Data.Memory24.Minimum == res->Data.Memory24.Maximum) {
252 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x/%d\n",
253 res->Data.Memory24.Minimum, res->Data.Memory24.AddressLength));
254 set->set_memory(dev, arc->context, res->Data.Memory24.Minimum,
255 res->Data.Memory24.AddressLength);
257 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x-0x%x/%d\n",
258 res->Data.Memory24.Minimum, res->Data.Memory24.Maximum,
259 res->Data.Memory24.AddressLength));
260 set->set_memoryrange(dev, arc->context, res->Data.Memory24.Minimum,
261 res->Data.Memory24.Maximum, res->Data.Memory24.AddressLength,
262 res->Data.Memory24.Alignment);
265 case ACPI_RESOURCE_TYPE_IRQ:
268 * "This structure is repeated for each separate interrupt
271 set->set_irq(dev, arc->context, res->Data.Irq.Interrupts,
272 res->Data.Irq.InterruptCount, res->Data.Irq.Triggering,
273 res->Data.Irq.Polarity);
275 case ACPI_RESOURCE_TYPE_DMA:
278 * "This structure is repeated for each separate DMA channel
281 set->set_drq(dev, arc->context, res->Data.Dma.Channels,
282 res->Data.Dma.ChannelCount);
284 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
285 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "start dependent functions\n"));
286 set->set_start_dependent(dev, arc->context,
287 res->Data.StartDpf.CompatibilityPriority);
289 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
290 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "end dependent functions\n"));
291 set->set_end_dependent(dev, arc->context);
293 case ACPI_RESOURCE_TYPE_ADDRESS16:
294 case ACPI_RESOURCE_TYPE_ADDRESS32:
295 case ACPI_RESOURCE_TYPE_ADDRESS64:
296 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
298 case ACPI_RESOURCE_TYPE_ADDRESS16:
299 gran = res->Data.Address16.Address.Granularity;
300 min = res->Data.Address16.Address.Minimum;
301 max = res->Data.Address16.Address.Maximum;
302 length = res->Data.Address16.Address.AddressLength;
305 case ACPI_RESOURCE_TYPE_ADDRESS32:
306 gran = res->Data.Address32.Address.Granularity;
307 min = res->Data.Address32.Address.Minimum;
308 max = res->Data.Address32.Address.Maximum;
309 length = res->Data.Address32.Address.AddressLength;
312 case ACPI_RESOURCE_TYPE_ADDRESS64:
313 gran = res->Data.Address64.Address.Granularity;
314 min = res->Data.Address64.Address.Minimum;
315 max = res->Data.Address64.Address.Maximum;
316 length = res->Data.Address64.Address.AddressLength;
320 KASSERT(res->Type == ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
321 ("should never happen"));
322 gran = res->Data.ExtAddress64.Address.Granularity;
323 min = res->Data.ExtAddress64.Address.Minimum;
324 max = res->Data.ExtAddress64.Address.Maximum;
325 length = res->Data.ExtAddress64.Address.AddressLength;
326 name = "ExtAddress64";
331 if (res->Data.Address.ProducerConsumer != ACPI_CONSUMER) {
332 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
333 "ignored %s %s producer\n", name,
334 acpi_address_range_name(res->Data.Address.ResourceType)));
337 if (res->Data.Address.ResourceType != ACPI_MEMORY_RANGE &&
338 res->Data.Address.ResourceType != ACPI_IO_RANGE) {
339 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
340 "ignored %s for non-memory, non-I/O\n", name));
345 if (min > ULONG_MAX || (res->Data.Address.MaxAddressFixed && max >
347 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored %s above 4G\n",
354 if (res->Data.Address.MinAddressFixed == ACPI_ADDRESS_FIXED &&
355 res->Data.Address.MaxAddressFixed == ACPI_ADDRESS_FIXED) {
356 if (res->Data.Address.ResourceType == ACPI_MEMORY_RANGE) {
357 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/Memory 0x%jx/%ju\n",
358 name, (uintmax_t)min, (uintmax_t)length));
359 set->set_memory(dev, arc->context, min, length);
361 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx/%ju\n", name,
362 (uintmax_t)min, (uintmax_t)length));
363 set->set_ioport(dev, arc->context, min, length);
366 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
367 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
368 "%s/Memory 0x%jx-0x%jx/%ju\n", name, (uintmax_t)min,
369 (uintmax_t)max, (uintmax_t)length));
370 set->set_memoryrange(dev, arc->context, min, max, length, gran);
372 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s/IO 0x%jx-0x%jx/%ju\n",
373 name, (uintmax_t)min, (uintmax_t)max, (uintmax_t)length));
374 set->set_iorange(dev, arc->context, min, max, length, gran);
378 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
379 if (res->Data.ExtendedIrq.ProducerConsumer != ACPI_CONSUMER) {
380 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "ignored ExtIRQ producer\n"));
383 set->set_ext_irq(dev, arc->context, res->Data.ExtendedIrq.Interrupts,
384 res->Data.ExtendedIrq.InterruptCount,
385 res->Data.ExtendedIrq.Triggering, res->Data.ExtendedIrq.Polarity);
387 case ACPI_RESOURCE_TYPE_VENDOR:
388 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
389 "unimplemented VendorSpecific resource\n"));
398 * Fetch a device's resources and associate them with the device.
400 * Note that it might be nice to also locate ACPI-specific resource items, such
403 * We really need to split the resource-fetching code out from the
404 * resource-parsing code, since we may want to use the parsing
405 * code for _PRS someday.
408 acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
409 struct acpi_parse_resource_set *set, void *arg)
411 struct acpi_resource_context arc;
414 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
416 set->set_init(dev, arg, &arc.context);
419 status = AcpiWalkResources(handle, "_CRS", acpi_parse_resource, &arc);
420 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
421 printf("can't fetch resources for %s - %s\n",
422 acpi_name(handle), AcpiFormatException(status));
423 return_ACPI_STATUS (status);
425 set->set_done(dev, arc.context);
426 return_ACPI_STATUS (AE_OK);
430 * Resource-set vectors used to attach _CRS-derived resources
433 static void acpi_res_set_init(device_t dev, void *arg, void **context);
434 static void acpi_res_set_done(device_t dev, void *context);
435 static void acpi_res_set_ioport(device_t dev, void *context,
436 uint64_t base, uint64_t length);
437 static void acpi_res_set_iorange(device_t dev, void *context,
438 uint64_t low, uint64_t high,
439 uint64_t length, uint64_t align);
440 static void acpi_res_set_memory(device_t dev, void *context,
441 uint64_t base, uint64_t length);
442 static void acpi_res_set_memoryrange(device_t dev, void *context,
443 uint64_t low, uint64_t high,
444 uint64_t length, uint64_t align);
445 static void acpi_res_set_irq(device_t dev, void *context, uint8_t *irq,
446 int count, int trig, int pol);
447 static void acpi_res_set_ext_irq(device_t dev, void *context,
448 uint32_t *irq, int count, int trig, int pol);
449 static void acpi_res_set_drq(device_t dev, void *context, uint8_t *drq,
451 static void acpi_res_set_start_dependent(device_t dev, void *context,
453 static void acpi_res_set_end_dependent(device_t dev, void *context);
455 struct acpi_parse_resource_set acpi_res_parse_set = {
459 acpi_res_set_iorange,
461 acpi_res_set_memoryrange,
463 acpi_res_set_ext_irq,
465 acpi_res_set_start_dependent,
466 acpi_res_set_end_dependent
469 struct acpi_res_context {
478 acpi_res_set_init(device_t dev, void *arg, void **context)
480 struct acpi_res_context *cp;
482 if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) {
483 bzero(cp, sizeof(*cp));
490 acpi_res_set_done(device_t dev, void *context)
492 struct acpi_res_context *cp = (struct acpi_res_context *)context;
500 acpi_res_set_ioport(device_t dev, void *context, uint64_t base,
503 struct acpi_res_context *cp = (struct acpi_res_context *)context;
507 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length);
511 acpi_res_set_iorange(device_t dev, void *context, uint64_t low,
512 uint64_t high, uint64_t length, uint64_t align)
514 struct acpi_res_context *cp = (struct acpi_res_context *)context;
518 device_printf(dev, "I/O range not supported\n");
522 acpi_res_set_memory(device_t dev, void *context, uint64_t base,
525 struct acpi_res_context *cp = (struct acpi_res_context *)context;
530 bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length);
534 acpi_res_set_memoryrange(device_t dev, void *context, uint64_t low,
535 uint64_t high, uint64_t length, uint64_t align)
537 struct acpi_res_context *cp = (struct acpi_res_context *)context;
541 device_printf(dev, "memory range not supported\n");
545 acpi_res_set_irq(device_t dev, void *context, uint8_t *irq, int count,
548 struct acpi_res_context *cp = (struct acpi_res_context *)context;
550 if (cp == NULL || irq == NULL)
553 /* This implements no resource relocation. */
557 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1);
561 acpi_res_set_ext_irq(device_t dev, void *context, uint32_t *irq, int count,
564 struct acpi_res_context *cp = (struct acpi_res_context *)context;
566 if (cp == NULL || irq == NULL)
569 /* This implements no resource relocation. */
573 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1);
577 acpi_res_set_drq(device_t dev, void *context, uint8_t *drq, int count)
579 struct acpi_res_context *cp = (struct acpi_res_context *)context;
581 if (cp == NULL || drq == NULL)
584 /* This implements no resource relocation. */
588 bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1);
592 acpi_res_set_start_dependent(device_t dev, void *context, int preference)
594 struct acpi_res_context *cp = (struct acpi_res_context *)context;
598 device_printf(dev, "dependent functions not supported\n");
602 acpi_res_set_end_dependent(device_t dev, void *context)
604 struct acpi_res_context *cp = (struct acpi_res_context *)context;
608 device_printf(dev, "dependent functions not supported\n");
612 * Resource-owning placeholders for IO and memory pseudo-devices.
614 * This code allocates system resources that will be used by ACPI
615 * child devices. The acpi parent manages these resources through a
619 static int acpi_sysres_rid = 100;
621 static int acpi_sysres_probe(device_t dev);
622 static int acpi_sysres_attach(device_t dev);
624 static device_method_t acpi_sysres_methods[] = {
625 /* Device interface */
626 DEVMETHOD(device_probe, acpi_sysres_probe),
627 DEVMETHOD(device_attach, acpi_sysres_attach),
632 static driver_t acpi_sysres_driver = {
638 static devclass_t acpi_sysres_devclass;
639 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysres_driver, acpi_sysres_devclass,
641 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
644 acpi_sysres_probe(device_t dev)
646 static char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL };
648 if (acpi_disabled("sysresource") ||
649 ACPI_ID_PROBE(device_get_parent(dev), dev, sysres_ids) == NULL)
652 device_set_desc(dev, "System Resource");
654 return (BUS_PROBE_DEFAULT);
658 acpi_sysres_attach(device_t dev)
661 struct resource_list_entry *bus_rle, *dev_rle;
662 struct resource_list *bus_rl, *dev_rl;
664 u_long start, end, count;
667 * Loop through all current resources to see if the new one overlaps
668 * any existing ones. If so, grow the old one up and/or down
669 * accordingly. Discard any that are wholly contained in the old. If
670 * the resource is unique, add it to the parent. It will later go into
673 bus = device_get_parent(dev);
674 dev_rl = BUS_GET_RESOURCE_LIST(bus, dev);
675 bus_rl = BUS_GET_RESOURCE_LIST(device_get_parent(bus), bus);
676 STAILQ_FOREACH(dev_rle, dev_rl, link) {
677 if (dev_rle->type != SYS_RES_IOPORT && dev_rle->type != SYS_RES_MEMORY)
680 start = dev_rle->start;
682 count = dev_rle->count;
683 type = dev_rle->type;
686 STAILQ_FOREACH(bus_rle, bus_rl, link) {
687 if (bus_rle->type != type)
690 /* New resource wholly contained in old, discard. */
691 if (start >= bus_rle->start && end <= bus_rle->end)
694 /* New tail overlaps old head, grow existing resource downward. */
695 if (start < bus_rle->start && end >= bus_rle->start) {
696 bus_rle->count += bus_rle->start - start;
697 bus_rle->start = start;
701 /* New head overlaps old tail, grow existing resource upward. */
702 if (start <= bus_rle->end && end > bus_rle->end) {
703 bus_rle->count += end - bus_rle->end;
708 /* If we adjusted the old resource, we're finished. */
713 /* If we didn't merge with anything, add this resource. */
715 bus_set_resource(bus, type, acpi_sysres_rid++, start, count);
718 /* After merging/moving resources to the parent, free the list. */
719 resource_list_free(dev_rl);