2 * drivers/acpi/resource.c - ACPI device resources interpretation.
4 * Copyright (C) 2012, Intel Corp.
5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 #include <linux/acpi.h>
22 #include <linux/device.h>
23 #include <linux/export.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
28 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
29 static inline bool acpi_iospace_resource_valid(struct resource *res)
31 /* On X86 IO space is limited to the [0 - 64K] IO port range */
32 return res->end < 0x10003;
35 #define valid_IRQ(i) (true)
37 * ACPI IO descriptors on arches other than X86 contain MMIO CPU physical
38 * addresses mapping IO space in CPU physical address space, IO space
39 * resources can be placed anywhere in the 64-bit physical address space.
42 acpi_iospace_resource_valid(struct resource *res) { return true; }
45 static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
47 u64 reslen = end - start + 1;
50 * CHECKME: len might be required to check versus a minimum
51 * length as well. 1 for io is fine, but for memory it does
52 * not make any sense at all.
53 * Note: some BIOSes report incorrect length for ACPI address space
54 * descriptor, so remove check of 'reslen == len' to avoid regression.
56 if (len && reslen && start <= end)
59 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
60 io ? "io" : "mem", start, end, len);
65 static void acpi_dev_memresource_flags(struct resource *res, u64 len,
68 res->flags = IORESOURCE_MEM;
70 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
71 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
73 if (write_protect == ACPI_READ_WRITE_MEMORY)
74 res->flags |= IORESOURCE_MEM_WRITEABLE;
77 static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
81 res->end = start + len - 1;
82 acpi_dev_memresource_flags(res, len, write_protect);
86 * acpi_dev_resource_memory - Extract ACPI memory resource information.
87 * @ares: Input ACPI resource object.
88 * @res: Output generic resource object.
90 * Check if the given ACPI resource object represents a memory resource and
91 * if that's the case, use the information in it to populate the generic
92 * resource object pointed to by @res.
95 * 1) false with res->flags setting to zero: not the expected resource type
96 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
97 * 3) true: valid assigned resource
99 bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
101 struct acpi_resource_memory24 *memory24;
102 struct acpi_resource_memory32 *memory32;
103 struct acpi_resource_fixed_memory32 *fixed_memory32;
105 switch (ares->type) {
106 case ACPI_RESOURCE_TYPE_MEMORY24:
107 memory24 = &ares->data.memory24;
108 acpi_dev_get_memresource(res, memory24->minimum << 8,
109 memory24->address_length << 8,
110 memory24->write_protect);
112 case ACPI_RESOURCE_TYPE_MEMORY32:
113 memory32 = &ares->data.memory32;
114 acpi_dev_get_memresource(res, memory32->minimum,
115 memory32->address_length,
116 memory32->write_protect);
118 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
119 fixed_memory32 = &ares->data.fixed_memory32;
120 acpi_dev_get_memresource(res, fixed_memory32->address,
121 fixed_memory32->address_length,
122 fixed_memory32->write_protect);
129 return !(res->flags & IORESOURCE_DISABLED);
131 EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
133 static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
134 u8 io_decode, u8 translation_type)
136 res->flags = IORESOURCE_IO;
138 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
139 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
141 if (!acpi_iospace_resource_valid(res))
142 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
144 if (io_decode == ACPI_DECODE_16)
145 res->flags |= IORESOURCE_IO_16BIT_ADDR;
146 if (translation_type == ACPI_SPARSE_TRANSLATION)
147 res->flags |= IORESOURCE_IO_SPARSE;
150 static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
154 res->end = start + len - 1;
155 acpi_dev_ioresource_flags(res, len, io_decode, 0);
159 * acpi_dev_resource_io - Extract ACPI I/O resource information.
160 * @ares: Input ACPI resource object.
161 * @res: Output generic resource object.
163 * Check if the given ACPI resource object represents an I/O resource and
164 * if that's the case, use the information in it to populate the generic
165 * resource object pointed to by @res.
168 * 1) false with res->flags setting to zero: not the expected resource type
169 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
170 * 3) true: valid assigned resource
172 bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
174 struct acpi_resource_io *io;
175 struct acpi_resource_fixed_io *fixed_io;
177 switch (ares->type) {
178 case ACPI_RESOURCE_TYPE_IO:
180 acpi_dev_get_ioresource(res, io->minimum,
184 case ACPI_RESOURCE_TYPE_FIXED_IO:
185 fixed_io = &ares->data.fixed_io;
186 acpi_dev_get_ioresource(res, fixed_io->address,
187 fixed_io->address_length,
195 return !(res->flags & IORESOURCE_DISABLED);
197 EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
199 static bool acpi_decode_space(struct resource_win *win,
200 struct acpi_resource_address *addr,
201 struct acpi_address64_attribute *attr)
203 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
204 bool wp = addr->info.mem.write_protect;
205 u64 len = attr->address_length;
206 u64 start, end, offset = 0;
207 struct resource *res = &win->res;
210 * Filter out invalid descriptor according to ACPI Spec 5.0, section
211 * 6.4.3.5 Address Space Resource Descriptors.
213 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
214 (addr->min_address_fixed && addr->max_address_fixed && !len))
215 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
216 addr->min_address_fixed, addr->max_address_fixed, len);
219 * For bridges that translate addresses across the bridge,
220 * translation_offset is the offset that must be added to the
221 * address on the secondary side to obtain the address on the
222 * primary side. Non-bridge devices must list 0 for all Address
223 * Translation offset bits.
225 if (addr->producer_consumer == ACPI_PRODUCER)
226 offset = attr->translation_offset;
227 else if (attr->translation_offset)
228 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
229 attr->translation_offset);
230 start = attr->minimum + offset;
231 end = attr->maximum + offset;
233 win->offset = offset;
236 if (sizeof(resource_size_t) < sizeof(u64) &&
237 (offset != win->offset || start != res->start || end != res->end)) {
238 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
239 attr->minimum, attr->maximum);
243 switch (addr->resource_type) {
244 case ACPI_MEMORY_RANGE:
245 acpi_dev_memresource_flags(res, len, wp);
248 acpi_dev_ioresource_flags(res, len, iodec,
249 addr->info.io.translation_type);
251 case ACPI_BUS_NUMBER_RANGE:
252 res->flags = IORESOURCE_BUS;
258 if (addr->producer_consumer == ACPI_PRODUCER)
259 res->flags |= IORESOURCE_WINDOW;
261 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
262 res->flags |= IORESOURCE_PREFETCH;
264 return !(res->flags & IORESOURCE_DISABLED);
268 * acpi_dev_resource_address_space - Extract ACPI address space information.
269 * @ares: Input ACPI resource object.
270 * @win: Output generic resource object.
272 * Check if the given ACPI resource object represents an address space resource
273 * and if that's the case, use the information in it to populate the generic
274 * resource object pointed to by @win.
277 * 1) false with win->res.flags setting to zero: not the expected resource type
278 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
280 * 3) true: valid assigned resource
282 bool acpi_dev_resource_address_space(struct acpi_resource *ares,
283 struct resource_win *win)
285 struct acpi_resource_address64 addr;
288 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
291 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
294 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
297 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
298 * @ares: Input ACPI resource object.
299 * @win: Output generic resource object.
301 * Check if the given ACPI resource object represents an extended address space
302 * resource and if that's the case, use the information in it to populate the
303 * generic resource object pointed to by @win.
306 * 1) false with win->res.flags setting to zero: not the expected resource type
307 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
309 * 3) true: valid assigned resource
311 bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
312 struct resource_win *win)
314 struct acpi_resource_extended_address64 *ext_addr;
317 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
320 ext_addr = &ares->data.ext_address64;
322 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
325 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
328 * acpi_dev_irq_flags - Determine IRQ resource flags.
329 * @triggering: Triggering type as provided by ACPI.
330 * @polarity: Interrupt polarity as provided by ACPI.
331 * @shareable: Whether or not the interrupt is shareable.
333 unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
337 if (triggering == ACPI_LEVEL_SENSITIVE)
338 flags = polarity == ACPI_ACTIVE_LOW ?
339 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
341 flags = polarity == ACPI_ACTIVE_LOW ?
342 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
344 if (shareable == ACPI_SHARED)
345 flags |= IORESOURCE_IRQ_SHAREABLE;
347 return flags | IORESOURCE_IRQ;
349 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
351 static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
355 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
358 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
359 u8 triggering, u8 polarity, u8 shareable,
364 if (!valid_IRQ(gsi)) {
365 acpi_dev_irqresource_disabled(res, gsi);
370 * In IO-APIC mode, use overrided attribute. Two reasons:
371 * 1. BIOS bug in DSDT
372 * 2. BIOS uses IO-APIC mode Interrupt Source Override
374 * We do this only if we are dealing with IRQ() or IRQNoFlags()
375 * resource (the legacy ISA resources). With modern ACPI 5 devices
376 * using extended IRQ descriptors we take the IRQ configuration
377 * from _CRS directly.
379 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
380 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
381 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
383 if (triggering != trig || polarity != pol) {
384 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
385 t ? "level" : "edge", p ? "low" : "high");
391 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
392 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
397 acpi_dev_irqresource_disabled(res, gsi);
402 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
403 * @ares: Input ACPI resource object.
404 * @index: Index into the array of GSIs represented by the resource.
405 * @res: Output generic resource object.
407 * Check if the given ACPI resource object represents an interrupt resource
408 * and @index does not exceed the resource's interrupt count (true is returned
409 * in that case regardless of the results of the other checks)). If that's the
410 * case, register the GSI corresponding to @index from the array of interrupts
411 * represented by the resource and populate the generic resource object pointed
412 * to by @res accordingly. If the registration of the GSI is not successful,
413 * IORESOURCE_DISABLED will be set it that object's flags.
416 * 1) false with res->flags setting to zero: not the expected resource type
417 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
418 * 3) true: valid assigned resource
420 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
421 struct resource *res)
423 struct acpi_resource_irq *irq;
424 struct acpi_resource_extended_irq *ext_irq;
426 switch (ares->type) {
427 case ACPI_RESOURCE_TYPE_IRQ:
429 * Per spec, only one interrupt per descriptor is allowed in
430 * _CRS, but some firmware violates this, so parse them all.
432 irq = &ares->data.irq;
433 if (index >= irq->interrupt_count) {
434 acpi_dev_irqresource_disabled(res, 0);
437 acpi_dev_get_irqresource(res, irq->interrupts[index],
438 irq->triggering, irq->polarity,
439 irq->sharable, true);
441 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
442 ext_irq = &ares->data.extended_irq;
443 if (index >= ext_irq->interrupt_count) {
444 acpi_dev_irqresource_disabled(res, 0);
447 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
448 ext_irq->triggering, ext_irq->polarity,
449 ext_irq->sharable, false);
458 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
461 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
462 * @list: The head of the resource list to free.
464 void acpi_dev_free_resource_list(struct list_head *list)
466 resource_list_free(list);
468 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
470 struct res_proc_context {
471 struct list_head *list;
472 int (*preproc)(struct acpi_resource *, void *);
478 static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
479 struct res_proc_context *c)
481 struct resource_entry *rentry;
483 rentry = resource_list_create_entry(NULL, 0);
488 *rentry->res = win->res;
489 rentry->offset = win->offset;
490 resource_list_add_tail(rentry, c->list);
495 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
498 struct res_proc_context *c = context;
499 struct resource_win win;
500 struct resource *res = &win.res;
506 ret = c->preproc(ares, c->preproc_data);
509 return AE_ABORT_METHOD;
510 } else if (ret > 0) {
515 memset(&win, 0, sizeof(win));
517 if (acpi_dev_resource_memory(ares, res)
518 || acpi_dev_resource_io(ares, res)
519 || acpi_dev_resource_address_space(ares, &win)
520 || acpi_dev_resource_ext_address_space(ares, &win))
521 return acpi_dev_new_resource_entry(&win, c);
523 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
526 status = acpi_dev_new_resource_entry(&win, c);
527 if (ACPI_FAILURE(status))
535 * acpi_dev_get_resources - Get current resources of a device.
536 * @adev: ACPI device node to get the resources for.
537 * @list: Head of the resultant list of resources (must be empty).
538 * @preproc: The caller's preprocessing routine.
539 * @preproc_data: Pointer passed to the caller's preprocessing routine.
541 * Evaluate the _CRS method for the given device node and process its output by
542 * (1) executing the @preproc() rountine provided by the caller, passing the
543 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
544 * returned and (2) converting all of the returned ACPI resources into struct
545 * resource objects if possible. If the return value of @preproc() in step (1)
546 * is different from 0, step (2) is not applied to the given ACPI resource and
547 * if that value is negative, the whole processing is aborted and that value is
548 * returned as the final error code.
550 * The resultant struct resource objects are put on the list pointed to by
551 * @list, that must be empty initially, as members of struct resource_entry
552 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
555 * The number of resources in the output list is returned on success, an error
556 * code reflecting the error condition is returned otherwise.
558 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
559 int (*preproc)(struct acpi_resource *, void *),
562 struct res_proc_context c;
565 if (!adev || !adev->handle || !list_empty(list))
568 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
573 c.preproc_data = preproc_data;
576 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
577 acpi_dev_process_resource, &c);
578 if (ACPI_FAILURE(status)) {
579 acpi_dev_free_resource_list(list);
580 return c.error ? c.error : -EIO;
585 EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
588 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
590 * @ares: Input ACPI resource object.
591 * @types: Valid resource types of IORESOURCE_XXX
593 * This is a helper function to support acpi_dev_get_resources(), which filters
594 * ACPI resource objects according to resource types.
596 int acpi_dev_filter_resource_type(struct acpi_resource *ares,
599 unsigned long type = 0;
601 switch (ares->type) {
602 case ACPI_RESOURCE_TYPE_MEMORY24:
603 case ACPI_RESOURCE_TYPE_MEMORY32:
604 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
605 type = IORESOURCE_MEM;
607 case ACPI_RESOURCE_TYPE_IO:
608 case ACPI_RESOURCE_TYPE_FIXED_IO:
609 type = IORESOURCE_IO;
611 case ACPI_RESOURCE_TYPE_IRQ:
612 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
613 type = IORESOURCE_IRQ;
615 case ACPI_RESOURCE_TYPE_DMA:
616 case ACPI_RESOURCE_TYPE_FIXED_DMA:
617 type = IORESOURCE_DMA;
619 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
620 type = IORESOURCE_REG;
622 case ACPI_RESOURCE_TYPE_ADDRESS16:
623 case ACPI_RESOURCE_TYPE_ADDRESS32:
624 case ACPI_RESOURCE_TYPE_ADDRESS64:
625 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
626 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
627 type = IORESOURCE_MEM;
628 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
629 type = IORESOURCE_IO;
630 else if (ares->data.address.resource_type ==
631 ACPI_BUS_NUMBER_RANGE)
632 type = IORESOURCE_BUS;
638 return (type & types) ? 0 : 1;
640 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);