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
,
122 u8 io_decode
, u8 translation_type
)
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
;
134 if (translation_type
== ACPI_SPARSE_TRANSLATION
)
135 res
->flags
|= IORESOURCE_IO_SPARSE
;
138 static void acpi_dev_get_ioresource(struct resource
*res
, u64 start
, u64 len
,
142 res
->end
= start
+ len
- 1;
143 acpi_dev_ioresource_flags(res
, len
, io_decode
, 0);
147 * acpi_dev_resource_io - Extract ACPI I/O resource information.
148 * @ares: Input ACPI resource object.
149 * @res: Output generic resource object.
151 * Check if the given ACPI resource object represents an I/O resource and
152 * if that's the case, use the information in it to populate the generic
153 * resource object pointed to by @res.
156 * 1) false with res->flags setting to zero: not the expected resource type
157 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
158 * 3) true: valid assigned resource
160 bool acpi_dev_resource_io(struct acpi_resource
*ares
, struct resource
*res
)
162 struct acpi_resource_io
*io
;
163 struct acpi_resource_fixed_io
*fixed_io
;
165 switch (ares
->type
) {
166 case ACPI_RESOURCE_TYPE_IO
:
168 acpi_dev_get_ioresource(res
, io
->minimum
,
172 case ACPI_RESOURCE_TYPE_FIXED_IO
:
173 fixed_io
= &ares
->data
.fixed_io
;
174 acpi_dev_get_ioresource(res
, fixed_io
->address
,
175 fixed_io
->address_length
,
183 return !(res
->flags
& IORESOURCE_DISABLED
);
185 EXPORT_SYMBOL_GPL(acpi_dev_resource_io
);
187 static bool acpi_decode_space(struct resource_win
*win
,
188 struct acpi_resource_address
*addr
,
189 struct acpi_address64_attribute
*attr
)
191 u8 iodec
= attr
->granularity
== 0xfff ? ACPI_DECODE_10
: ACPI_DECODE_16
;
192 bool wp
= addr
->info
.mem
.write_protect
;
193 u64 len
= attr
->address_length
;
194 u64 start
, end
, offset
= 0;
195 struct resource
*res
= &win
->res
;
198 * Filter out invalid descriptor according to ACPI Spec 5.0, section
199 * 6.4.3.5 Address Space Resource Descriptors.
201 if ((addr
->min_address_fixed
!= addr
->max_address_fixed
&& len
) ||
202 (addr
->min_address_fixed
&& addr
->max_address_fixed
&& !len
))
203 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
204 addr
->min_address_fixed
, addr
->max_address_fixed
, len
);
207 * For bridges that translate addresses across the bridge,
208 * translation_offset is the offset that must be added to the
209 * address on the secondary side to obtain the address on the
210 * primary side. Non-bridge devices must list 0 for all Address
211 * Translation offset bits.
213 if (addr
->producer_consumer
== ACPI_PRODUCER
)
214 offset
= attr
->translation_offset
;
215 else if (attr
->translation_offset
)
216 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
217 attr
->translation_offset
);
218 start
= attr
->minimum
+ offset
;
219 end
= attr
->maximum
+ offset
;
221 win
->offset
= offset
;
224 if (sizeof(resource_size_t
) < sizeof(u64
) &&
225 (offset
!= win
->offset
|| start
!= res
->start
|| end
!= res
->end
)) {
226 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
227 attr
->minimum
, attr
->maximum
);
231 switch (addr
->resource_type
) {
232 case ACPI_MEMORY_RANGE
:
233 acpi_dev_memresource_flags(res
, len
, wp
);
236 acpi_dev_ioresource_flags(res
, len
, iodec
,
237 addr
->info
.io
.translation_type
);
239 case ACPI_BUS_NUMBER_RANGE
:
240 res
->flags
= IORESOURCE_BUS
;
246 if (addr
->producer_consumer
== ACPI_PRODUCER
)
247 res
->flags
|= IORESOURCE_WINDOW
;
249 if (addr
->info
.mem
.caching
== ACPI_PREFETCHABLE_MEMORY
)
250 res
->flags
|= IORESOURCE_PREFETCH
;
252 return !(res
->flags
& IORESOURCE_DISABLED
);
256 * acpi_dev_resource_address_space - Extract ACPI address space information.
257 * @ares: Input ACPI resource object.
258 * @win: Output generic resource object.
260 * Check if the given ACPI resource object represents an address space resource
261 * and if that's the case, use the information in it to populate the generic
262 * resource object pointed to by @win.
265 * 1) false with win->res.flags setting to zero: not the expected resource type
266 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
268 * 3) true: valid assigned resource
270 bool acpi_dev_resource_address_space(struct acpi_resource
*ares
,
271 struct resource_win
*win
)
273 struct acpi_resource_address64 addr
;
276 if (ACPI_FAILURE(acpi_resource_to_address64(ares
, &addr
)))
279 return acpi_decode_space(win
, (struct acpi_resource_address
*)&addr
,
282 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space
);
285 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
286 * @ares: Input ACPI resource object.
287 * @win: Output generic resource object.
289 * Check if the given ACPI resource object represents an extended address space
290 * resource and if that's the case, use the information in it to populate the
291 * generic resource object pointed to by @win.
294 * 1) false with win->res.flags setting to zero: not the expected resource type
295 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
297 * 3) true: valid assigned resource
299 bool acpi_dev_resource_ext_address_space(struct acpi_resource
*ares
,
300 struct resource_win
*win
)
302 struct acpi_resource_extended_address64
*ext_addr
;
305 if (ares
->type
!= ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64
)
308 ext_addr
= &ares
->data
.ext_address64
;
310 return acpi_decode_space(win
, (struct acpi_resource_address
*)ext_addr
,
313 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space
);
316 * acpi_dev_irq_flags - Determine IRQ resource flags.
317 * @triggering: Triggering type as provided by ACPI.
318 * @polarity: Interrupt polarity as provided by ACPI.
319 * @shareable: Whether or not the interrupt is shareable.
321 unsigned long acpi_dev_irq_flags(u8 triggering
, u8 polarity
, u8 shareable
)
325 if (triggering
== ACPI_LEVEL_SENSITIVE
)
326 flags
= polarity
== ACPI_ACTIVE_LOW
?
327 IORESOURCE_IRQ_LOWLEVEL
: IORESOURCE_IRQ_HIGHLEVEL
;
329 flags
= polarity
== ACPI_ACTIVE_LOW
?
330 IORESOURCE_IRQ_LOWEDGE
: IORESOURCE_IRQ_HIGHEDGE
;
332 if (shareable
== ACPI_SHARED
)
333 flags
|= IORESOURCE_IRQ_SHAREABLE
;
335 return flags
| IORESOURCE_IRQ
;
337 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags
);
339 static void acpi_dev_irqresource_disabled(struct resource
*res
, u32 gsi
)
343 res
->flags
= IORESOURCE_IRQ
| IORESOURCE_DISABLED
| IORESOURCE_UNSET
;
346 static void acpi_dev_get_irqresource(struct resource
*res
, u32 gsi
,
347 u8 triggering
, u8 polarity
, u8 shareable
,
352 if (!valid_IRQ(gsi
)) {
353 acpi_dev_irqresource_disabled(res
, gsi
);
358 * In IO-APIC mode, use overrided attribute. Two reasons:
359 * 1. BIOS bug in DSDT
360 * 2. BIOS uses IO-APIC mode Interrupt Source Override
362 * We do this only if we are dealing with IRQ() or IRQNoFlags()
363 * resource (the legacy ISA resources). With modern ACPI 5 devices
364 * using extended IRQ descriptors we take the IRQ configuration
365 * from _CRS directly.
367 if (legacy
&& !acpi_get_override_irq(gsi
, &t
, &p
)) {
368 u8 trig
= t
? ACPI_LEVEL_SENSITIVE
: ACPI_EDGE_SENSITIVE
;
369 u8 pol
= p
? ACPI_ACTIVE_LOW
: ACPI_ACTIVE_HIGH
;
371 if (triggering
!= trig
|| polarity
!= pol
) {
372 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi
,
373 t
? "level" : "edge", p
? "low" : "high");
379 res
->flags
= acpi_dev_irq_flags(triggering
, polarity
, shareable
);
380 irq
= acpi_register_gsi(NULL
, gsi
, triggering
, polarity
);
385 acpi_dev_irqresource_disabled(res
, gsi
);
390 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
391 * @ares: Input ACPI resource object.
392 * @index: Index into the array of GSIs represented by the resource.
393 * @res: Output generic resource object.
395 * Check if the given ACPI resource object represents an interrupt resource
396 * and @index does not exceed the resource's interrupt count (true is returned
397 * in that case regardless of the results of the other checks)). If that's the
398 * case, register the GSI corresponding to @index from the array of interrupts
399 * represented by the resource and populate the generic resource object pointed
400 * to by @res accordingly. If the registration of the GSI is not successful,
401 * IORESOURCE_DISABLED will be set it that object's flags.
404 * 1) false with res->flags setting to zero: not the expected resource type
405 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
406 * 3) true: valid assigned resource
408 bool acpi_dev_resource_interrupt(struct acpi_resource
*ares
, int index
,
409 struct resource
*res
)
411 struct acpi_resource_irq
*irq
;
412 struct acpi_resource_extended_irq
*ext_irq
;
414 switch (ares
->type
) {
415 case ACPI_RESOURCE_TYPE_IRQ
:
417 * Per spec, only one interrupt per descriptor is allowed in
418 * _CRS, but some firmware violates this, so parse them all.
420 irq
= &ares
->data
.irq
;
421 if (index
>= irq
->interrupt_count
) {
422 acpi_dev_irqresource_disabled(res
, 0);
425 acpi_dev_get_irqresource(res
, irq
->interrupts
[index
],
426 irq
->triggering
, irq
->polarity
,
427 irq
->sharable
, true);
429 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ
:
430 ext_irq
= &ares
->data
.extended_irq
;
431 if (index
>= ext_irq
->interrupt_count
) {
432 acpi_dev_irqresource_disabled(res
, 0);
435 acpi_dev_get_irqresource(res
, ext_irq
->interrupts
[index
],
436 ext_irq
->triggering
, ext_irq
->polarity
,
437 ext_irq
->sharable
, false);
446 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt
);
449 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
450 * @list: The head of the resource list to free.
452 void acpi_dev_free_resource_list(struct list_head
*list
)
454 resource_list_free(list
);
456 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list
);
458 struct res_proc_context
{
459 struct list_head
*list
;
460 int (*preproc
)(struct acpi_resource
*, void *);
466 static acpi_status
acpi_dev_new_resource_entry(struct resource_win
*win
,
467 struct res_proc_context
*c
)
469 struct resource_entry
*rentry
;
471 rentry
= resource_list_create_entry(NULL
, 0);
476 *rentry
->res
= win
->res
;
477 rentry
->offset
= win
->offset
;
478 resource_list_add_tail(rentry
, c
->list
);
483 static acpi_status
acpi_dev_process_resource(struct acpi_resource
*ares
,
486 struct res_proc_context
*c
= context
;
487 struct resource_win win
;
488 struct resource
*res
= &win
.res
;
494 ret
= c
->preproc(ares
, c
->preproc_data
);
497 return AE_CTRL_TERMINATE
;
498 } else if (ret
> 0) {
503 memset(&win
, 0, sizeof(win
));
505 if (acpi_dev_resource_memory(ares
, res
)
506 || acpi_dev_resource_io(ares
, res
)
507 || acpi_dev_resource_address_space(ares
, &win
)
508 || acpi_dev_resource_ext_address_space(ares
, &win
))
509 return acpi_dev_new_resource_entry(&win
, c
);
511 for (i
= 0; acpi_dev_resource_interrupt(ares
, i
, res
); i
++) {
514 status
= acpi_dev_new_resource_entry(&win
, c
);
515 if (ACPI_FAILURE(status
))
523 * acpi_dev_get_resources - Get current resources of a device.
524 * @adev: ACPI device node to get the resources for.
525 * @list: Head of the resultant list of resources (must be empty).
526 * @preproc: The caller's preprocessing routine.
527 * @preproc_data: Pointer passed to the caller's preprocessing routine.
529 * Evaluate the _CRS method for the given device node and process its output by
530 * (1) executing the @preproc() rountine provided by the caller, passing the
531 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
532 * returned and (2) converting all of the returned ACPI resources into struct
533 * resource objects if possible. If the return value of @preproc() in step (1)
534 * is different from 0, step (2) is not applied to the given ACPI resource and
535 * if that value is negative, the whole processing is aborted and that value is
536 * returned as the final error code.
538 * The resultant struct resource objects are put on the list pointed to by
539 * @list, that must be empty initially, as members of struct resource_entry
540 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
543 * The number of resources in the output list is returned on success, an error
544 * code reflecting the error condition is returned otherwise.
546 int acpi_dev_get_resources(struct acpi_device
*adev
, struct list_head
*list
,
547 int (*preproc
)(struct acpi_resource
*, void *),
550 struct res_proc_context c
;
553 if (!adev
|| !adev
->handle
|| !list_empty(list
))
556 if (!acpi_has_method(adev
->handle
, METHOD_NAME__CRS
))
561 c
.preproc_data
= preproc_data
;
564 status
= acpi_walk_resources(adev
->handle
, METHOD_NAME__CRS
,
565 acpi_dev_process_resource
, &c
);
566 if (ACPI_FAILURE(status
)) {
567 acpi_dev_free_resource_list(list
);
568 return c
.error
? c
.error
: -EIO
;
573 EXPORT_SYMBOL_GPL(acpi_dev_get_resources
);
576 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
578 * @ares: Input ACPI resource object.
579 * @types: Valid resource types of IORESOURCE_XXX
581 * This is a helper function to support acpi_dev_get_resources(), which filters
582 * ACPI resource objects according to resource types.
584 int acpi_dev_filter_resource_type(struct acpi_resource
*ares
,
587 unsigned long type
= 0;
589 switch (ares
->type
) {
590 case ACPI_RESOURCE_TYPE_MEMORY24
:
591 case ACPI_RESOURCE_TYPE_MEMORY32
:
592 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32
:
593 type
= IORESOURCE_MEM
;
595 case ACPI_RESOURCE_TYPE_IO
:
596 case ACPI_RESOURCE_TYPE_FIXED_IO
:
597 type
= IORESOURCE_IO
;
599 case ACPI_RESOURCE_TYPE_IRQ
:
600 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ
:
601 type
= IORESOURCE_IRQ
;
603 case ACPI_RESOURCE_TYPE_DMA
:
604 case ACPI_RESOURCE_TYPE_FIXED_DMA
:
605 type
= IORESOURCE_DMA
;
607 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER
:
608 type
= IORESOURCE_REG
;
610 case ACPI_RESOURCE_TYPE_ADDRESS16
:
611 case ACPI_RESOURCE_TYPE_ADDRESS32
:
612 case ACPI_RESOURCE_TYPE_ADDRESS64
:
613 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64
:
614 if (ares
->data
.address
.resource_type
== ACPI_MEMORY_RANGE
)
615 type
= IORESOURCE_MEM
;
616 else if (ares
->data
.address
.resource_type
== ACPI_IO_RANGE
)
617 type
= IORESOURCE_IO
;
618 else if (ares
->data
.address
.resource_type
==
619 ACPI_BUS_NUMBER_RANGE
)
620 type
= IORESOURCE_BUS
;
626 return (type
& types
) ? 0 : 1;
628 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type
);