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))
30 #define valid_IRQ(i) (true)
33 static bool acpi_dev_resource_len_valid(u64 start
, u64 end
, u64 len
, bool io
)
35 u64 reslen
= end
- start
+ 1;
38 * CHECKME: len might be required to check versus a minimum
39 * length as well. 1 for io is fine, but for memory it does
40 * not make any sense at all.
41 * Note: some BIOSes report incorrect length for ACPI address space
42 * descriptor, so remove check of 'reslen == len' to avoid regression.
44 if (len
&& reslen
&& start
<= end
)
47 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
48 io
? "io" : "mem", start
, end
, len
);
53 static void acpi_dev_memresource_flags(struct resource
*res
, u64 len
,
56 res
->flags
= IORESOURCE_MEM
;
58 if (!acpi_dev_resource_len_valid(res
->start
, res
->end
, len
, false))
59 res
->flags
|= IORESOURCE_DISABLED
| IORESOURCE_UNSET
;
61 if (write_protect
== ACPI_READ_WRITE_MEMORY
)
62 res
->flags
|= IORESOURCE_MEM_WRITEABLE
;
65 static void acpi_dev_get_memresource(struct resource
*res
, u64 start
, u64 len
,
69 res
->end
= start
+ len
- 1;
70 acpi_dev_memresource_flags(res
, len
, write_protect
);
74 * acpi_dev_resource_memory - Extract ACPI memory resource information.
75 * @ares: Input ACPI resource object.
76 * @res: Output generic resource object.
78 * Check if the given ACPI resource object represents a memory resource and
79 * if that's the case, use the information in it to populate the generic
80 * resource object pointed to by @res.
83 * 1) false with res->flags setting to zero: not the expected resource type
84 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
85 * 3) true: valid assigned resource
87 bool acpi_dev_resource_memory(struct acpi_resource
*ares
, struct resource
*res
)
89 struct acpi_resource_memory24
*memory24
;
90 struct acpi_resource_memory32
*memory32
;
91 struct acpi_resource_fixed_memory32
*fixed_memory32
;
94 case ACPI_RESOURCE_TYPE_MEMORY24
:
95 memory24
= &ares
->data
.memory24
;
96 acpi_dev_get_memresource(res
, memory24
->minimum
<< 8,
97 memory24
->address_length
<< 8,
98 memory24
->write_protect
);
100 case ACPI_RESOURCE_TYPE_MEMORY32
:
101 memory32
= &ares
->data
.memory32
;
102 acpi_dev_get_memresource(res
, memory32
->minimum
,
103 memory32
->address_length
,
104 memory32
->write_protect
);
106 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32
:
107 fixed_memory32
= &ares
->data
.fixed_memory32
;
108 acpi_dev_get_memresource(res
, fixed_memory32
->address
,
109 fixed_memory32
->address_length
,
110 fixed_memory32
->write_protect
);
117 return !(res
->flags
& IORESOURCE_DISABLED
);
119 EXPORT_SYMBOL_GPL(acpi_dev_resource_memory
);
121 static void acpi_dev_ioresource_flags(struct resource
*res
, u64 len
,
124 res
->flags
= IORESOURCE_IO
;
126 if (!acpi_dev_resource_len_valid(res
->start
, res
->end
, len
, true))
127 res
->flags
|= IORESOURCE_DISABLED
| IORESOURCE_UNSET
;
129 if (res
->end
>= 0x10003)
130 res
->flags
|= IORESOURCE_DISABLED
| IORESOURCE_UNSET
;
132 if (io_decode
== ACPI_DECODE_16
)
133 res
->flags
|= IORESOURCE_IO_16BIT_ADDR
;
136 static void acpi_dev_get_ioresource(struct resource
*res
, u64 start
, u64 len
,
140 res
->end
= start
+ len
- 1;
141 acpi_dev_ioresource_flags(res
, len
, io_decode
);
145 * acpi_dev_resource_io - Extract ACPI I/O resource information.
146 * @ares: Input ACPI resource object.
147 * @res: Output generic resource object.
149 * Check if the given ACPI resource object represents an I/O resource and
150 * if that's the case, use the information in it to populate the generic
151 * resource object pointed to by @res.
154 * 1) false with res->flags setting to zero: not the expected resource type
155 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
156 * 3) true: valid assigned resource
158 bool acpi_dev_resource_io(struct acpi_resource
*ares
, struct resource
*res
)
160 struct acpi_resource_io
*io
;
161 struct acpi_resource_fixed_io
*fixed_io
;
163 switch (ares
->type
) {
164 case ACPI_RESOURCE_TYPE_IO
:
166 acpi_dev_get_ioresource(res
, io
->minimum
,
170 case ACPI_RESOURCE_TYPE_FIXED_IO
:
171 fixed_io
= &ares
->data
.fixed_io
;
172 acpi_dev_get_ioresource(res
, fixed_io
->address
,
173 fixed_io
->address_length
,
181 return !(res
->flags
& IORESOURCE_DISABLED
);
183 EXPORT_SYMBOL_GPL(acpi_dev_resource_io
);
185 static bool acpi_decode_space(struct resource_win
*win
,
186 struct acpi_resource_address
*addr
,
187 struct acpi_address64_attribute
*attr
)
189 u8 iodec
= attr
->granularity
== 0xfff ? ACPI_DECODE_10
: ACPI_DECODE_16
;
190 bool wp
= addr
->info
.mem
.write_protect
;
191 u64 len
= attr
->address_length
;
192 u64 start
, end
, offset
= 0;
193 struct resource
*res
= &win
->res
;
196 * Filter out invalid descriptor according to ACPI Spec 5.0, section
197 * 6.4.3.5 Address Space Resource Descriptors.
199 if ((addr
->min_address_fixed
!= addr
->max_address_fixed
&& len
) ||
200 (addr
->min_address_fixed
&& addr
->max_address_fixed
&& !len
))
201 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
202 addr
->min_address_fixed
, addr
->max_address_fixed
, len
);
205 * For bridges that translate addresses across the bridge,
206 * translation_offset is the offset that must be added to the
207 * address on the secondary side to obtain the address on the
208 * primary side. Non-bridge devices must list 0 for all Address
209 * Translation offset bits.
211 if (addr
->producer_consumer
== ACPI_PRODUCER
)
212 offset
= attr
->translation_offset
;
213 else if (attr
->translation_offset
)
214 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
215 attr
->translation_offset
);
216 start
= attr
->minimum
+ offset
;
217 end
= attr
->maximum
+ offset
;
219 win
->offset
= offset
;
222 if (sizeof(resource_size_t
) < sizeof(u64
) &&
223 (offset
!= win
->offset
|| start
!= res
->start
|| end
!= res
->end
)) {
224 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
225 attr
->minimum
, attr
->maximum
);
229 switch (addr
->resource_type
) {
230 case ACPI_MEMORY_RANGE
:
231 acpi_dev_memresource_flags(res
, len
, wp
);
234 acpi_dev_ioresource_flags(res
, len
, iodec
);
236 case ACPI_BUS_NUMBER_RANGE
:
237 res
->flags
= IORESOURCE_BUS
;
243 if (addr
->producer_consumer
== ACPI_PRODUCER
)
244 res
->flags
|= IORESOURCE_WINDOW
;
246 if (addr
->info
.mem
.caching
== ACPI_PREFETCHABLE_MEMORY
)
247 res
->flags
|= IORESOURCE_PREFETCH
;
249 return !(res
->flags
& IORESOURCE_DISABLED
);
253 * acpi_dev_resource_address_space - Extract ACPI address space information.
254 * @ares: Input ACPI resource object.
255 * @win: Output generic resource object.
257 * Check if the given ACPI resource object represents an address space resource
258 * and if that's the case, use the information in it to populate the generic
259 * resource object pointed to by @win.
262 * 1) false with win->res.flags setting to zero: not the expected resource type
263 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
265 * 3) true: valid assigned resource
267 bool acpi_dev_resource_address_space(struct acpi_resource
*ares
,
268 struct resource_win
*win
)
270 struct acpi_resource_address64 addr
;
273 if (ACPI_FAILURE(acpi_resource_to_address64(ares
, &addr
)))
276 return acpi_decode_space(win
, (struct acpi_resource_address
*)&addr
,
279 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space
);
282 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
283 * @ares: Input ACPI resource object.
284 * @win: Output generic resource object.
286 * Check if the given ACPI resource object represents an extended address space
287 * resource and if that's the case, use the information in it to populate the
288 * generic resource object pointed to by @win.
291 * 1) false with win->res.flags setting to zero: not the expected resource type
292 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
294 * 3) true: valid assigned resource
296 bool acpi_dev_resource_ext_address_space(struct acpi_resource
*ares
,
297 struct resource_win
*win
)
299 struct acpi_resource_extended_address64
*ext_addr
;
302 if (ares
->type
!= ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64
)
305 ext_addr
= &ares
->data
.ext_address64
;
307 return acpi_decode_space(win
, (struct acpi_resource_address
*)ext_addr
,
310 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space
);
313 * acpi_dev_irq_flags - Determine IRQ resource flags.
314 * @triggering: Triggering type as provided by ACPI.
315 * @polarity: Interrupt polarity as provided by ACPI.
316 * @shareable: Whether or not the interrupt is shareable.
318 unsigned long acpi_dev_irq_flags(u8 triggering
, u8 polarity
, u8 shareable
)
322 if (triggering
== ACPI_LEVEL_SENSITIVE
)
323 flags
= polarity
== ACPI_ACTIVE_LOW
?
324 IORESOURCE_IRQ_LOWLEVEL
: IORESOURCE_IRQ_HIGHLEVEL
;
326 flags
= polarity
== ACPI_ACTIVE_LOW
?
327 IORESOURCE_IRQ_LOWEDGE
: IORESOURCE_IRQ_HIGHEDGE
;
329 if (shareable
== ACPI_SHARED
)
330 flags
|= IORESOURCE_IRQ_SHAREABLE
;
332 return flags
| IORESOURCE_IRQ
;
334 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags
);
336 static void acpi_dev_irqresource_disabled(struct resource
*res
, u32 gsi
)
340 res
->flags
= IORESOURCE_IRQ
| IORESOURCE_DISABLED
| IORESOURCE_UNSET
;
343 static void acpi_dev_get_irqresource(struct resource
*res
, u32 gsi
,
344 u8 triggering
, u8 polarity
, u8 shareable
,
349 if (!valid_IRQ(gsi
)) {
350 acpi_dev_irqresource_disabled(res
, gsi
);
355 * In IO-APIC mode, use overrided attribute. Two reasons:
356 * 1. BIOS bug in DSDT
357 * 2. BIOS uses IO-APIC mode Interrupt Source Override
359 * We do this only if we are dealing with IRQ() or IRQNoFlags()
360 * resource (the legacy ISA resources). With modern ACPI 5 devices
361 * using extended IRQ descriptors we take the IRQ configuration
362 * from _CRS directly.
364 if (legacy
&& !acpi_get_override_irq(gsi
, &t
, &p
)) {
365 u8 trig
= t
? ACPI_LEVEL_SENSITIVE
: ACPI_EDGE_SENSITIVE
;
366 u8 pol
= p
? ACPI_ACTIVE_LOW
: ACPI_ACTIVE_HIGH
;
368 if (triggering
!= trig
|| polarity
!= pol
) {
369 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi
,
370 t
? "level" : "edge", p
? "low" : "high");
376 res
->flags
= acpi_dev_irq_flags(triggering
, polarity
, shareable
);
377 irq
= acpi_register_gsi(NULL
, gsi
, triggering
, polarity
);
382 acpi_dev_irqresource_disabled(res
, gsi
);
387 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
388 * @ares: Input ACPI resource object.
389 * @index: Index into the array of GSIs represented by the resource.
390 * @res: Output generic resource object.
392 * Check if the given ACPI resource object represents an interrupt resource
393 * and @index does not exceed the resource's interrupt count (true is returned
394 * in that case regardless of the results of the other checks)). If that's the
395 * case, register the GSI corresponding to @index from the array of interrupts
396 * represented by the resource and populate the generic resource object pointed
397 * to by @res accordingly. If the registration of the GSI is not successful,
398 * IORESOURCE_DISABLED will be set it that object's flags.
401 * 1) false with res->flags setting to zero: not the expected resource type
402 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
403 * 3) true: valid assigned resource
405 bool acpi_dev_resource_interrupt(struct acpi_resource
*ares
, int index
,
406 struct resource
*res
)
408 struct acpi_resource_irq
*irq
;
409 struct acpi_resource_extended_irq
*ext_irq
;
411 switch (ares
->type
) {
412 case ACPI_RESOURCE_TYPE_IRQ
:
414 * Per spec, only one interrupt per descriptor is allowed in
415 * _CRS, but some firmware violates this, so parse them all.
417 irq
= &ares
->data
.irq
;
418 if (index
>= irq
->interrupt_count
) {
419 acpi_dev_irqresource_disabled(res
, 0);
422 acpi_dev_get_irqresource(res
, irq
->interrupts
[index
],
423 irq
->triggering
, irq
->polarity
,
424 irq
->sharable
, true);
426 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ
:
427 ext_irq
= &ares
->data
.extended_irq
;
428 if (index
>= ext_irq
->interrupt_count
) {
429 acpi_dev_irqresource_disabled(res
, 0);
432 acpi_dev_get_irqresource(res
, ext_irq
->interrupts
[index
],
433 ext_irq
->triggering
, ext_irq
->polarity
,
434 ext_irq
->sharable
, false);
443 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt
);
446 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
447 * @list: The head of the resource list to free.
449 void acpi_dev_free_resource_list(struct list_head
*list
)
451 resource_list_free(list
);
453 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list
);
455 struct res_proc_context
{
456 struct list_head
*list
;
457 int (*preproc
)(struct acpi_resource
*, void *);
463 static acpi_status
acpi_dev_new_resource_entry(struct resource_win
*win
,
464 struct res_proc_context
*c
)
466 struct resource_entry
*rentry
;
468 rentry
= resource_list_create_entry(NULL
, 0);
473 *rentry
->res
= win
->res
;
474 rentry
->offset
= win
->offset
;
475 resource_list_add_tail(rentry
, c
->list
);
480 static acpi_status
acpi_dev_process_resource(struct acpi_resource
*ares
,
483 struct res_proc_context
*c
= context
;
484 struct resource_win win
;
485 struct resource
*res
= &win
.res
;
491 ret
= c
->preproc(ares
, c
->preproc_data
);
494 return AE_CTRL_TERMINATE
;
495 } else if (ret
> 0) {
500 memset(&win
, 0, sizeof(win
));
502 if (acpi_dev_resource_memory(ares
, res
)
503 || acpi_dev_resource_io(ares
, res
)
504 || acpi_dev_resource_address_space(ares
, &win
)
505 || acpi_dev_resource_ext_address_space(ares
, &win
))
506 return acpi_dev_new_resource_entry(&win
, c
);
508 for (i
= 0; acpi_dev_resource_interrupt(ares
, i
, res
); i
++) {
511 status
= acpi_dev_new_resource_entry(&win
, c
);
512 if (ACPI_FAILURE(status
))
520 * acpi_dev_get_resources - Get current resources of a device.
521 * @adev: ACPI device node to get the resources for.
522 * @list: Head of the resultant list of resources (must be empty).
523 * @preproc: The caller's preprocessing routine.
524 * @preproc_data: Pointer passed to the caller's preprocessing routine.
526 * Evaluate the _CRS method for the given device node and process its output by
527 * (1) executing the @preproc() rountine provided by the caller, passing the
528 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
529 * returned and (2) converting all of the returned ACPI resources into struct
530 * resource objects if possible. If the return value of @preproc() in step (1)
531 * is different from 0, step (2) is not applied to the given ACPI resource and
532 * if that value is negative, the whole processing is aborted and that value is
533 * returned as the final error code.
535 * The resultant struct resource objects are put on the list pointed to by
536 * @list, that must be empty initially, as members of struct resource_entry
537 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
540 * The number of resources in the output list is returned on success, an error
541 * code reflecting the error condition is returned otherwise.
543 int acpi_dev_get_resources(struct acpi_device
*adev
, struct list_head
*list
,
544 int (*preproc
)(struct acpi_resource
*, void *),
547 struct res_proc_context c
;
550 if (!adev
|| !adev
->handle
|| !list_empty(list
))
553 if (!acpi_has_method(adev
->handle
, METHOD_NAME__CRS
))
558 c
.preproc_data
= preproc_data
;
561 status
= acpi_walk_resources(adev
->handle
, METHOD_NAME__CRS
,
562 acpi_dev_process_resource
, &c
);
563 if (ACPI_FAILURE(status
)) {
564 acpi_dev_free_resource_list(list
);
565 return c
.error
? c
.error
: -EIO
;
570 EXPORT_SYMBOL_GPL(acpi_dev_get_resources
);
573 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
575 * @ares: Input ACPI resource object.
576 * @types: Valid resource types of IORESOURCE_XXX
578 * This is a helper function to support acpi_dev_get_resources(), which filters
579 * ACPI resource objects according to resource types.
581 int acpi_dev_filter_resource_type(struct acpi_resource
*ares
,
584 unsigned long type
= 0;
586 switch (ares
->type
) {
587 case ACPI_RESOURCE_TYPE_MEMORY24
:
588 case ACPI_RESOURCE_TYPE_MEMORY32
:
589 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32
:
590 type
= IORESOURCE_MEM
;
592 case ACPI_RESOURCE_TYPE_IO
:
593 case ACPI_RESOURCE_TYPE_FIXED_IO
:
594 type
= IORESOURCE_IO
;
596 case ACPI_RESOURCE_TYPE_IRQ
:
597 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ
:
598 type
= IORESOURCE_IRQ
;
600 case ACPI_RESOURCE_TYPE_DMA
:
601 case ACPI_RESOURCE_TYPE_FIXED_DMA
:
602 type
= IORESOURCE_DMA
;
604 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER
:
605 type
= IORESOURCE_REG
;
607 case ACPI_RESOURCE_TYPE_ADDRESS16
:
608 case ACPI_RESOURCE_TYPE_ADDRESS32
:
609 case ACPI_RESOURCE_TYPE_ADDRESS64
:
610 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64
:
611 if (ares
->data
.address
.resource_type
== ACPI_MEMORY_RANGE
)
612 type
= IORESOURCE_MEM
;
613 else if (ares
->data
.address
.resource_type
== ACPI_IO_RANGE
)
614 type
= IORESOURCE_IO
;
615 else if (ares
->data
.address
.resource_type
==
616 ACPI_BUS_NUMBER_RANGE
)
617 type
= IORESOURCE_BUS
;
623 return (type
& types
) ? 0 : 1;
625 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type
);