blob: 5bf73a9f26d9fb92b4b3229cd2c932e86cbd5f87 [file] [log] [blame]
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +01001/*
2 * drivers/acpi/resource.c - ACPI device resources interpretation.
3 *
4 * Copyright (C) 2012, Intel Corp.
5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
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.
12 *
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.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24
25#include <linux/acpi.h>
26#include <linux/device.h>
27#include <linux/export.h>
28#include <linux/ioport.h>
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010029#include <linux/slab.h>
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010030
31#ifdef CONFIG_X86
32#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
33#else
34#define valid_IRQ(i) (true)
35#endif
36
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080037static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010038{
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080039 u64 reslen = end - start + 1;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010040
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080041 /*
42 * CHECKME: len might be required to check versus a minimum
43 * length as well. 1 for io is fine, but for memory it does
44 * not make any sense at all.
45 */
46 if (len && reslen && reslen == len && start <= end)
47 return true;
48
49 pr_info("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
50 io ? "io" : "mem", start, end, len);
51
52 return false;
53}
54
55static void acpi_dev_memresource_flags(struct resource *res, u64 len,
Thomas Gleixner72e26b02015-02-02 10:42:52 +080056 u8 write_protect)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080057{
58 res->flags = IORESOURCE_MEM;
59
60 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
Jiang Liuc78b6882015-02-02 10:42:56 +080061 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010062
63 if (write_protect == ACPI_READ_WRITE_MEMORY)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080064 res->flags |= IORESOURCE_MEM_WRITEABLE;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010065}
66
67static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
68 u8 write_protect)
69{
70 res->start = start;
71 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +080072 acpi_dev_memresource_flags(res, len, write_protect);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010073}
74
75/**
76 * acpi_dev_resource_memory - Extract ACPI memory resource information.
77 * @ares: Input ACPI resource object.
78 * @res: Output generic resource object.
79 *
80 * Check if the given ACPI resource object represents a memory resource and
81 * if that's the case, use the information in it to populate the generic
82 * resource object pointed to by @res.
Jiang Liu581c19f2015-02-02 10:42:55 +080083 *
84 * Return:
85 * 1) false with res->flags setting to zero: not the expected resource type
86 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
87 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010088 */
89bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
90{
91 struct acpi_resource_memory24 *memory24;
92 struct acpi_resource_memory32 *memory32;
93 struct acpi_resource_fixed_memory32 *fixed_memory32;
94
95 switch (ares->type) {
96 case ACPI_RESOURCE_TYPE_MEMORY24:
97 memory24 = &ares->data.memory24;
Jiang Liu8515f932015-02-02 10:42:54 +080098 acpi_dev_get_memresource(res, memory24->minimum << 8,
99 memory24->address_length << 8,
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100100 memory24->write_protect);
101 break;
102 case ACPI_RESOURCE_TYPE_MEMORY32:
103 memory32 = &ares->data.memory32;
104 acpi_dev_get_memresource(res, memory32->minimum,
105 memory32->address_length,
106 memory32->write_protect);
107 break;
108 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
109 fixed_memory32 = &ares->data.fixed_memory32;
110 acpi_dev_get_memresource(res, fixed_memory32->address,
111 fixed_memory32->address_length,
112 fixed_memory32->write_protect);
113 break;
114 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800115 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100116 return false;
117 }
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800118
119 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100120}
121EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
122
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800123static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800124 u8 io_decode)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100125{
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800126 res->flags = IORESOURCE_IO;
127
128 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
Jiang Liuc78b6882015-02-02 10:42:56 +0800129 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800130
131 if (res->end >= 0x10003)
Jiang Liuc78b6882015-02-02 10:42:56 +0800132 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100133
134 if (io_decode == ACPI_DECODE_16)
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800135 res->flags |= IORESOURCE_IO_16BIT_ADDR;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100136}
137
138static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
139 u8 io_decode)
140{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100141 res->start = start;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800142 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800143 acpi_dev_ioresource_flags(res, len, io_decode);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100144}
145
146/**
147 * acpi_dev_resource_io - Extract ACPI I/O resource information.
148 * @ares: Input ACPI resource object.
149 * @res: Output generic resource object.
150 *
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.
Jiang Liu581c19f2015-02-02 10:42:55 +0800154 *
155 * Return:
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
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100159 */
160bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
161{
162 struct acpi_resource_io *io;
163 struct acpi_resource_fixed_io *fixed_io;
164
165 switch (ares->type) {
166 case ACPI_RESOURCE_TYPE_IO:
167 io = &ares->data.io;
168 acpi_dev_get_ioresource(res, io->minimum,
169 io->address_length,
170 io->io_decode);
171 break;
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,
176 ACPI_DECODE_10);
177 break;
178 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800179 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100180 return false;
181 }
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800182
183 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100184}
185EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
186
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800187static bool acpi_decode_space(struct resource *res,
188 struct acpi_resource_address *addr,
189 struct acpi_address64_attribute *attr)
190{
191 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800192 bool wp = addr->info.mem.write_protect;
193 u64 len = attr->address_length;
194
195 res->start = attr->minimum;
196 res->end = attr->maximum;
197
198 switch (addr->resource_type) {
199 case ACPI_MEMORY_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800200 acpi_dev_memresource_flags(res, len, wp);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800201 break;
202 case ACPI_IO_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800203 acpi_dev_ioresource_flags(res, len, iodec);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800204 break;
205 case ACPI_BUS_NUMBER_RANGE:
206 res->flags = IORESOURCE_BUS;
207 break;
208 default:
209 return false;
210 }
211
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800212 if (addr->producer_consumer == ACPI_PRODUCER)
213 res->flags |= IORESOURCE_WINDOW;
214
Thomas Gleixnerfcb29bb2015-02-02 10:42:53 +0800215 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
216 res->flags |= IORESOURCE_PREFETCH;
217
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800218 return !(res->flags & IORESOURCE_DISABLED);
219}
220
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100221/**
222 * acpi_dev_resource_address_space - Extract ACPI address space information.
223 * @ares: Input ACPI resource object.
224 * @res: Output generic resource object.
225 *
226 * Check if the given ACPI resource object represents an address space resource
227 * and if that's the case, use the information in it to populate the generic
228 * resource object pointed to by @res.
Jiang Liu581c19f2015-02-02 10:42:55 +0800229 *
230 * Return:
231 * 1) false with res->flags setting to zero: not the expected resource type
232 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
233 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100234 */
235bool acpi_dev_resource_address_space(struct acpi_resource *ares,
236 struct resource *res)
237{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100238 struct acpi_resource_address64 addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100239
Jiang Liu581c19f2015-02-02 10:42:55 +0800240 res->flags = 0;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800241 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
Jiang Liu6658c732014-10-27 13:21:35 +0800242 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100243
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800244 return acpi_decode_space(res, (struct acpi_resource_address *)&addr,
245 &addr.address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100246}
247EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
248
249/**
250 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
251 * @ares: Input ACPI resource object.
252 * @res: Output generic resource object.
253 *
254 * Check if the given ACPI resource object represents an extended address space
255 * resource and if that's the case, use the information in it to populate the
256 * generic resource object pointed to by @res.
Jiang Liu581c19f2015-02-02 10:42:55 +0800257 *
258 * Return:
259 * 1) false with res->flags setting to zero: not the expected resource type
260 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
261 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100262 */
263bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
264 struct resource *res)
265{
266 struct acpi_resource_extended_address64 *ext_addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100267
Jiang Liu581c19f2015-02-02 10:42:55 +0800268 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100269 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
270 return false;
271
272 ext_addr = &ares->data.ext_address64;
273
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800274 return acpi_decode_space(res, (struct acpi_resource_address *)ext_addr,
275 &ext_addr->address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100276}
277EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
278
279/**
280 * acpi_dev_irq_flags - Determine IRQ resource flags.
281 * @triggering: Triggering type as provided by ACPI.
282 * @polarity: Interrupt polarity as provided by ACPI.
283 * @shareable: Whether or not the interrupt is shareable.
284 */
285unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
286{
287 unsigned long flags;
288
289 if (triggering == ACPI_LEVEL_SENSITIVE)
290 flags = polarity == ACPI_ACTIVE_LOW ?
291 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
292 else
293 flags = polarity == ACPI_ACTIVE_LOW ?
294 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
295
296 if (shareable == ACPI_SHARED)
297 flags |= IORESOURCE_IRQ_SHAREABLE;
298
299 return flags | IORESOURCE_IRQ;
300}
301EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
302
303static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
304{
305 res->start = gsi;
306 res->end = gsi;
Jiang Liuc78b6882015-02-02 10:42:56 +0800307 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100308}
309
310static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000311 u8 triggering, u8 polarity, u8 shareable,
312 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100313{
314 int irq, p, t;
315
316 if (!valid_IRQ(gsi)) {
317 acpi_dev_irqresource_disabled(res, gsi);
318 return;
319 }
320
321 /*
322 * In IO-APIC mode, use overrided attribute. Two reasons:
323 * 1. BIOS bug in DSDT
324 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000325 *
326 * We do this only if we are dealing with IRQ() or IRQNoFlags()
327 * resource (the legacy ISA resources). With modern ACPI 5 devices
328 * using extended IRQ descriptors we take the IRQ configuration
329 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100330 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000331 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100332 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
333 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
334
335 if (triggering != trig || polarity != pol) {
336 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000337 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100338 triggering = trig;
339 polarity = pol;
340 }
341 }
342
343 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
344 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
345 if (irq >= 0) {
346 res->start = irq;
347 res->end = irq;
348 } else {
349 acpi_dev_irqresource_disabled(res, gsi);
350 }
351}
352
353/**
354 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
355 * @ares: Input ACPI resource object.
356 * @index: Index into the array of GSIs represented by the resource.
357 * @res: Output generic resource object.
358 *
359 * Check if the given ACPI resource object represents an interrupt resource
360 * and @index does not exceed the resource's interrupt count (true is returned
361 * in that case regardless of the results of the other checks)). If that's the
362 * case, register the GSI corresponding to @index from the array of interrupts
363 * represented by the resource and populate the generic resource object pointed
364 * to by @res accordingly. If the registration of the GSI is not successful,
365 * IORESOURCE_DISABLED will be set it that object's flags.
Jiang Liu581c19f2015-02-02 10:42:55 +0800366 *
367 * Return:
368 * 1) false with res->flags setting to zero: not the expected resource type
369 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
370 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100371 */
372bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
373 struct resource *res)
374{
375 struct acpi_resource_irq *irq;
376 struct acpi_resource_extended_irq *ext_irq;
377
378 switch (ares->type) {
379 case ACPI_RESOURCE_TYPE_IRQ:
380 /*
381 * Per spec, only one interrupt per descriptor is allowed in
382 * _CRS, but some firmware violates this, so parse them all.
383 */
384 irq = &ares->data.irq;
385 if (index >= irq->interrupt_count) {
386 acpi_dev_irqresource_disabled(res, 0);
387 return false;
388 }
389 acpi_dev_get_irqresource(res, irq->interrupts[index],
390 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000391 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100392 break;
393 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
394 ext_irq = &ares->data.extended_irq;
395 if (index >= ext_irq->interrupt_count) {
396 acpi_dev_irqresource_disabled(res, 0);
397 return false;
398 }
399 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
400 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000401 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100402 break;
403 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800404 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100405 return false;
406 }
407
408 return true;
409}
410EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100411
412/**
413 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
414 * @list: The head of the resource list to free.
415 */
416void acpi_dev_free_resource_list(struct list_head *list)
417{
418 struct resource_list_entry *rentry, *re;
419
420 list_for_each_entry_safe(rentry, re, list, node) {
421 list_del(&rentry->node);
422 kfree(rentry);
423 }
424}
425EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
426
427struct res_proc_context {
428 struct list_head *list;
429 int (*preproc)(struct acpi_resource *, void *);
430 void *preproc_data;
431 int count;
432 int error;
433};
434
435static acpi_status acpi_dev_new_resource_entry(struct resource *r,
436 struct res_proc_context *c)
437{
438 struct resource_list_entry *rentry;
439
440 rentry = kmalloc(sizeof(*rentry), GFP_KERNEL);
441 if (!rentry) {
442 c->error = -ENOMEM;
443 return AE_NO_MEMORY;
444 }
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100445 rentry->res = *r;
446 list_add_tail(&rentry->node, c->list);
447 c->count++;
448 return AE_OK;
449}
450
451static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
452 void *context)
453{
454 struct res_proc_context *c = context;
455 struct resource r;
456 int i;
457
458 if (c->preproc) {
459 int ret;
460
461 ret = c->preproc(ares, c->preproc_data);
462 if (ret < 0) {
463 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100464 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100465 } else if (ret > 0) {
466 return AE_OK;
467 }
468 }
469
470 memset(&r, 0, sizeof(r));
471
472 if (acpi_dev_resource_memory(ares, &r)
473 || acpi_dev_resource_io(ares, &r)
474 || acpi_dev_resource_address_space(ares, &r)
475 || acpi_dev_resource_ext_address_space(ares, &r))
476 return acpi_dev_new_resource_entry(&r, c);
477
478 for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) {
479 acpi_status status;
480
481 status = acpi_dev_new_resource_entry(&r, c);
482 if (ACPI_FAILURE(status))
483 return status;
484 }
485
486 return AE_OK;
487}
488
489/**
490 * acpi_dev_get_resources - Get current resources of a device.
491 * @adev: ACPI device node to get the resources for.
492 * @list: Head of the resultant list of resources (must be empty).
493 * @preproc: The caller's preprocessing routine.
494 * @preproc_data: Pointer passed to the caller's preprocessing routine.
495 *
496 * Evaluate the _CRS method for the given device node and process its output by
497 * (1) executing the @preproc() rountine provided by the caller, passing the
498 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
499 * returned and (2) converting all of the returned ACPI resources into struct
500 * resource objects if possible. If the return value of @preproc() in step (1)
501 * is different from 0, step (2) is not applied to the given ACPI resource and
502 * if that value is negative, the whole processing is aborted and that value is
503 * returned as the final error code.
504 *
505 * The resultant struct resource objects are put on the list pointed to by
506 * @list, that must be empty initially, as members of struct resource_list_entry
507 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
508 * free that list.
509 *
510 * The number of resources in the output list is returned on success, an error
511 * code reflecting the error condition is returned otherwise.
512 */
513int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
514 int (*preproc)(struct acpi_resource *, void *),
515 void *preproc_data)
516{
517 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100518 acpi_status status;
519
520 if (!adev || !adev->handle || !list_empty(list))
521 return -EINVAL;
522
Jiang Liu952c63e2013-06-29 00:24:38 +0800523 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100524 return 0;
525
526 c.list = list;
527 c.preproc = preproc;
528 c.preproc_data = preproc_data;
529 c.count = 0;
530 c.error = 0;
531 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
532 acpi_dev_process_resource, &c);
533 if (ACPI_FAILURE(status)) {
534 acpi_dev_free_resource_list(list);
535 return c.error ? c.error : -EIO;
536 }
537
538 return c.count;
539}
540EXPORT_SYMBOL_GPL(acpi_dev_get_resources);