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: src/sys/dev/acpica/acpi_resource.c,v 1.24 2004/05/30 20:08:23 phk Exp $
28 * $DragonFly: src/sys/dev/acpica5/acpi_resource.c,v 1.7 2007/02/23 01:18:00 y0netan1 Exp $
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
39 #include <dev/acpica5/acpivar.h>
41 /* Hooks for the ACPI CA debugging infrastructure */
42 #define _COMPONENT ACPI_BUS
43 ACPI_MODULE_NAME("RESOURCE")
46 * Fetch a device's resources and associate them with the device.
48 * Note that it might be nice to also locate ACPI-specific resource items, such
51 * We really need to split the resource-fetching code out from the
52 * resource-parsing code, since we may want to use the parsing
53 * code for _PRS someday.
56 acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
57 struct acpi_parse_resource_set *set, void *arg)
65 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
68 * Special-case some devices that abuse _PRS/_CRS to mean
69 * something other than "I consume this resource".
71 * XXX do we really need this? It's only relevant once
72 * we start always-allocating these resources, and even
73 * then, the only special-cased device is likely to be
74 * the PCI interrupt link.
77 /* Fetch the device's current resources. */
78 buf.Length = ACPI_ALLOCATE_BUFFER;
79 if (ACPI_FAILURE((status = AcpiGetCurrentResources(handle, &buf)))) {
80 if (status != AE_NOT_FOUND && status != AE_TYPE)
81 kprintf("can't fetch resources for %s - %s\n",
82 acpi_name(handle), AcpiFormatException(status));
83 return_ACPI_STATUS (status);
85 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "%s - got %ld bytes of resources\n",
86 acpi_name(handle), (long)buf.Length));
87 set->set_init(dev, arg, &context);
89 /* Iterate through the resources */
91 last = (char *)buf.Pointer + buf.Length;
93 res = (ACPI_RESOURCE *)curr;
96 /* Handle the individual resource types */
98 case ACPI_RESOURCE_TYPE_END_TAG:
99 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "EndTag\n"));
102 case ACPI_RESOURCE_TYPE_FIXED_IO:
103 if (res->Data.FixedIo.AddressLength <= 0)
105 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedIo 0x%x/%d\n",
106 res->Data.FixedIo.Address,
107 res->Data.FixedIo.AddressLength));
108 set->set_ioport(dev, context,
109 res->Data.FixedIo.Address,
110 res->Data.FixedIo.AddressLength);
112 case ACPI_RESOURCE_TYPE_IO:
113 if (res->Data.Io.AddressLength <= 0)
115 if (res->Data.Io.Minimum == res->Data.Io.Maximum) {
116 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x/%d\n",
117 res->Data.Io.Minimum,
118 res->Data.Io.AddressLength));
119 set->set_ioport(dev, context,
120 res->Data.Io.Minimum,
121 res->Data.Io.AddressLength);
123 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Io 0x%x-0x%x/%d\n",
124 res->Data.Io.Minimum,
125 res->Data.Io.Maximum,
126 res->Data.Io.AddressLength));
127 set->set_iorange(dev, context,
128 res->Data.Io.Minimum,
129 res->Data.Io.Maximum,
130 res->Data.Io.AddressLength,
131 res->Data.Io.Alignment);
134 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
135 if (res->Data.FixedMemory32.AddressLength <= 0)
137 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "FixedMemory32 0x%x/%d\n",
138 res->Data.FixedMemory32.Address,
139 res->Data.FixedMemory32.AddressLength));
140 set->set_memory(dev, context,
141 res->Data.FixedMemory32.Address,
142 res->Data.FixedMemory32.AddressLength);
144 case ACPI_RESOURCE_TYPE_MEMORY32:
145 if (res->Data.Memory32.AddressLength <= 0)
147 if (res->Data.Memory32.Minimum ==
148 res->Data.Memory32.Maximum) {
150 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x/%d\n",
151 res->Data.Memory32.Minimum,
152 res->Data.Memory32.AddressLength));
153 set->set_memory(dev, context,
154 res->Data.Memory32.Minimum,
155 res->Data.Memory32.AddressLength);
157 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory32 0x%x-0x%x/%d\n",
158 res->Data.Memory32.Minimum,
159 res->Data.Memory32.Maximum,
160 res->Data.Memory32.AddressLength));
161 set->set_memoryrange(dev, context,
162 res->Data.Memory32.Minimum,
163 res->Data.Memory32.Maximum,
164 res->Data.Memory32.AddressLength,
165 res->Data.Memory32.Alignment);
168 case ACPI_RESOURCE_TYPE_MEMORY24:
169 if (res->Data.Memory24.AddressLength <= 0)
171 if (res->Data.Memory24.Minimum ==
172 res->Data.Memory24.Maximum) {
174 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x/%d\n",
175 res->Data.Memory24.Minimum,
176 res->Data.Memory24.AddressLength));
177 set->set_memory(dev, context, res->Data.Memory24.Minimum,
178 res->Data.Memory24.AddressLength);
180 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "Memory24 0x%x-0x%x/%d\n",
181 res->Data.Memory24.Minimum,
182 res->Data.Memory24.Maximum,
183 res->Data.Memory24.AddressLength));
184 set->set_memoryrange(dev, context,
185 res->Data.Memory24.Minimum,
186 res->Data.Memory24.Maximum,
187 res->Data.Memory24.AddressLength,
188 res->Data.Memory24.Alignment);
191 case ACPI_RESOURCE_TYPE_IRQ:
194 * "This structure is repeated for each separate interrupt
197 set->set_irq(dev, context, res->Data.Irq.Interrupts,
198 res->Data.Irq.InterruptCount, res->Data.Irq.Triggering,
199 res->Data.Irq.Polarity);
201 case ACPI_RESOURCE_TYPE_DMA:
204 * "This structure is repeated for each separate dma channel
207 set->set_drq(dev, context, res->Data.Dma.Channels,
208 res->Data.Dma.ChannelCount);
210 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
211 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "start dependant functions\n"));
212 set->set_start_dependant(dev, context,
213 res->Data.StartDpf.CompatibilityPriority);
215 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
216 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "end dependant functions\n"));
217 set->set_end_dependant(dev, context);
219 case ACPI_RESOURCE_TYPE_ADDRESS32:
220 if (res->Data.Address32.AddressLength <= 0)
222 if (res->Data.Address32.ProducerConsumer != ACPI_CONSUMER) {
223 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
224 "ignored Address32 %s producer\n",
225 res->Data.Address32.ResourceType == ACPI_IO_RANGE ?
229 if (res->Data.Address32.ResourceType != ACPI_MEMORY_RANGE &&
230 res->Data.Address32.ResourceType != ACPI_IO_RANGE) {
231 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
232 "ignored Address32 for non-memory, non-I/O\n"));
236 if (res->Data.Address32.Minimum == ACPI_ADDRESS_FIXED &&
237 res->Data.Address32.Maximum == ACPI_ADDRESS_FIXED) {
239 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
240 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
241 "Address32/Memory 0x%x/%d\n",
242 res->Data.Address32.Minimum,
243 res->Data.Address32.AddressLength));
244 set->set_memory(dev, context,
245 res->Data.Address32.Minimum,
246 res->Data.Address32.AddressLength);
248 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
249 "Address32/IO 0x%x/%d\n",
250 res->Data.Address32.Minimum,
251 res->Data.Address32.AddressLength));
252 set->set_ioport(dev, context,
253 res->Data.Address32.Minimum,
254 res->Data.Address32.AddressLength);
257 if (res->Data.Address32.ResourceType == ACPI_MEMORY_RANGE) {
258 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
259 "Address32/Memory 0x%x-0x%x/%d\n",
260 res->Data.Address32.Minimum,
261 res->Data.Address32.Maximum,
262 res->Data.Address32.AddressLength));
263 set->set_memoryrange(dev, context,
264 res->Data.Address32.Minimum,
265 res->Data.Address32.Maximum,
266 res->Data.Address32.AddressLength,
267 res->Data.Address32.Granularity);
269 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
270 "Address32/IO 0x%x-0x%x/%d\n",
271 res->Data.Address32.Minimum,
272 res->Data.Address32.Maximum,
273 res->Data.Address32.AddressLength));
274 set->set_iorange(dev, context,
275 res->Data.Address32.Minimum,
276 res->Data.Address32.Maximum,
277 res->Data.Address32.AddressLength,
278 res->Data.Address32.Granularity);
282 case ACPI_RESOURCE_TYPE_ADDRESS16:
283 if (res->Data.Address16.AddressLength <= 0)
285 if (res->Data.Address16.ProducerConsumer != ACPI_CONSUMER) {
286 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
287 "ignored Address16 %s producer\n",
288 res->Data.Address16.ResourceType == ACPI_IO_RANGE ?
292 if (res->Data.Address16.ResourceType != ACPI_MEMORY_RANGE &&
293 res->Data.Address16.ResourceType != ACPI_IO_RANGE) {
294 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
295 "ignored Address16 for non-memory, non-I/O\n"));
299 if (res->Data.Address16.Minimum == ACPI_ADDRESS_FIXED &&
300 res->Data.Address16.Maximum == ACPI_ADDRESS_FIXED) {
302 if (res->Data.Address16.ResourceType == ACPI_MEMORY_RANGE) {
303 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
304 "Address16/Memory 0x%x/%d\n",
305 res->Data.Address16.Minimum,
306 res->Data.Address16.AddressLength));
307 set->set_memory(dev, context,
308 res->Data.Address16.Minimum,
309 res->Data.Address16.AddressLength);
311 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
312 "Address16/IO 0x%x/%d\n",
313 res->Data.Address16.Minimum,
314 res->Data.Address16.AddressLength));
315 set->set_ioport(dev, context,
316 res->Data.Address16.Minimum,
317 res->Data.Address16.AddressLength);
320 if (res->Data.Address16.ResourceType == ACPI_MEMORY_RANGE) {
321 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
322 "Address16/Memory 0x%x-0x%x/%d\n",
323 res->Data.Address16.Minimum,
324 res->Data.Address16.Maximum,
325 res->Data.Address16.AddressLength));
326 set->set_memoryrange(dev, context,
327 res->Data.Address16.Minimum,
328 res->Data.Address16.Maximum,
329 res->Data.Address16.AddressLength,
330 res->Data.Address16.Granularity);
332 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
333 "Address16/IO 0x%x-0x%x/%d\n",
334 res->Data.Address16.Minimum,
335 res->Data.Address16.Maximum,
336 res->Data.Address16.AddressLength));
337 set->set_iorange(dev, context,
338 res->Data.Address16.Minimum,
339 res->Data.Address16.Maximum,
340 res->Data.Address16.AddressLength,
341 res->Data.Address16.Granularity);
345 case ACPI_RESOURCE_TYPE_ADDRESS64:
346 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
347 "unimplemented Address64 resource\n"));
349 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
350 /* XXX special handling? */
351 set->set_ext_irq(dev, context, res->Data.ExtendedIrq.Interrupts,
352 res->Data.ExtendedIrq.InterruptCount,
353 res->Data.ExtendedIrq.Triggering,
354 res->Data.ExtendedIrq.Polarity);
356 case ACPI_RESOURCE_TYPE_VENDOR:
357 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
358 "unimplemented VendorSpecific resource\n"));
365 AcpiOsFree(buf.Pointer);
366 set->set_done(dev, context);
367 return_ACPI_STATUS (AE_OK);
371 * Resource-set vectors used to attach _CRS-derived resources
374 static void acpi_res_set_init(device_t dev, void *arg, void **context);
375 static void acpi_res_set_done(device_t dev, void *context);
376 static void acpi_res_set_ioport(device_t dev, void *context,
377 u_int32_t base, u_int32_t length);
378 static void acpi_res_set_iorange(device_t dev, void *context,
379 u_int32_t low, u_int32_t high,
380 u_int32_t length, u_int32_t align);
381 static void acpi_res_set_memory(device_t dev, void *context,
382 u_int32_t base, u_int32_t length);
383 static void acpi_res_set_memoryrange(device_t dev, void *context,
384 u_int32_t low, u_int32_t high,
385 u_int32_t length, u_int32_t align);
386 static void acpi_res_set_irq(device_t dev, void *context, u_int8_t *irq,
387 int count, int trig, int pol);
388 static void acpi_res_set_ext_irq(device_t dev, void *context,
389 u_int32_t *irq, int count, int trig,
391 static void acpi_res_set_drq(device_t dev, void *context, u_int8_t *drq,
393 static void acpi_res_set_start_dependant(device_t dev, void *context,
395 static void acpi_res_set_end_dependant(device_t dev, void *context);
397 struct acpi_parse_resource_set acpi_res_parse_set = {
401 acpi_res_set_iorange,
403 acpi_res_set_memoryrange,
405 acpi_res_set_ext_irq,
407 acpi_res_set_start_dependant,
408 acpi_res_set_end_dependant
411 struct acpi_res_context {
420 acpi_res_set_init(device_t dev, void *arg, void **context)
422 struct acpi_res_context *cp;
424 if ((cp = AcpiOsAllocate(sizeof(*cp))) != NULL) {
425 bzero(cp, sizeof(*cp));
432 acpi_res_set_done(device_t dev, void *context)
434 struct acpi_res_context *cp = (struct acpi_res_context *)context;
442 acpi_res_set_ioport(device_t dev, void *context, u_int32_t base,
445 struct acpi_res_context *cp = (struct acpi_res_context *)context;
449 bus_set_resource(dev, SYS_RES_IOPORT, cp->ar_nio++, base, length);
453 acpi_res_set_iorange(device_t dev, void *context, u_int32_t low,
454 u_int32_t high, u_int32_t length, u_int32_t align)
456 struct acpi_res_context *cp = (struct acpi_res_context *)context;
460 device_printf(dev, "I/O range not supported\n");
464 acpi_res_set_memory(device_t dev, void *context, u_int32_t base,
467 struct acpi_res_context *cp = (struct acpi_res_context *)context;
472 bus_set_resource(dev, SYS_RES_MEMORY, cp->ar_nmem++, base, length);
476 acpi_res_set_memoryrange(device_t dev, void *context, u_int32_t low,
477 u_int32_t high, u_int32_t length, u_int32_t align)
479 struct acpi_res_context *cp = (struct acpi_res_context *)context;
483 device_printf(dev, "memory range not supported\n");
487 acpi_res_set_irq(device_t dev, void *context, u_int8_t *irq, int count,
490 struct acpi_res_context *cp = (struct acpi_res_context *)context;
492 if (cp == NULL || irq == NULL)
495 /* This implements no resource relocation. */
499 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1);
500 #if 0 /* From FreeBSD-5 XXX */
501 BUS_CONFIG_INTR(dev, *irq, (trig == ACPI_EDGE_SENSITIVE) ?
502 INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL, (pol == ACPI_ACTIVE_HIGH) ?
503 INTR_POLARITY_HIGH : INTR_POLARITY_LOW);
508 acpi_res_set_ext_irq(device_t dev, void *context, u_int32_t *irq, int count,
511 struct acpi_res_context *cp = (struct acpi_res_context *)context;
513 if (cp == NULL || irq == NULL)
516 /* This implements no resource relocation. */
520 bus_set_resource(dev, SYS_RES_IRQ, cp->ar_nirq++, *irq, 1);
521 #if 0 /* From FreeBSD-5 XXX */
522 BUS_CONFIG_INTR(dev, *irq, (trig == ACPI_EDGE_SENSITIVE) ?
523 INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL, (pol == ACPI_ACTIVE_HIGH) ?
524 INTR_POLARITY_HIGH : INTR_POLARITY_LOW);
529 acpi_res_set_drq(device_t dev, void *context, u_int8_t *drq, int count)
531 struct acpi_res_context *cp = (struct acpi_res_context *)context;
533 if (cp == NULL || drq == NULL)
536 /* This implements no resource relocation. */
540 bus_set_resource(dev, SYS_RES_DRQ, cp->ar_ndrq++, *drq, 1);
544 acpi_res_set_start_dependant(device_t dev, void *context, int preference)
546 struct acpi_res_context *cp = (struct acpi_res_context *)context;
550 device_printf(dev, "dependant functions not supported\n");
554 acpi_res_set_end_dependant(device_t dev, void *context)
556 struct acpi_res_context *cp = (struct acpi_res_context *)context;
560 device_printf(dev, "dependant functions not supported\n");
564 * Resource-owning placeholders.
566 * This code "owns" system resource objects that aren't
567 * otherwise useful to devices, and which shouldn't be
570 * Note that some systems claim *all* of the physical address space
571 * with a PNP0C01 device, so we cannot correctly "own" system memory
572 * here (must be done in the SMAP handler on x86 systems, for
576 static int acpi_sysresource_probe(device_t dev);
577 static int acpi_sysresource_attach(device_t dev);
579 static device_method_t acpi_sysresource_methods[] = {
580 /* Device interface */
581 DEVMETHOD(device_probe, acpi_sysresource_probe),
582 DEVMETHOD(device_attach, acpi_sysresource_attach),
587 static driver_t acpi_sysresource_driver = {
589 acpi_sysresource_methods,
593 static devclass_t acpi_sysresource_devclass;
594 DRIVER_MODULE(acpi_sysresource, acpi, acpi_sysresource_driver,
595 acpi_sysresource_devclass, 0, 0);
596 MODULE_DEPEND(acpi_sysresource, acpi, 1, 1, 1);
599 acpi_sysresource_probe(device_t dev)
601 if (!acpi_disabled("sysresource") && acpi_MatchHid(dev, "PNP0C02"))
602 device_set_desc(dev, "System Resource");
611 acpi_sysresource_attach(device_t dev)
613 struct resource *res;
617 * Suck up all the resources that might have been assigned to us.
618 * Note that it's impossible to tell the difference between a
619 * resource that someone else has claimed, and one that doesn't
622 for (i = 0; i < 100; i++) {
624 res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 0);
626 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
628 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE);