]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/resource.c
KVM: arm64: vgic-v3: Log which GICv3 system registers are trapped
[mirror_ubuntu-zesty-kernel.git] / drivers / acpi / resource.c
CommitLineData
046d9ce6
RW
1/*
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 *
046d9ce6
RW
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 */
20
21#include <linux/acpi.h>
22#include <linux/device.h>
23#include <linux/export.h>
24#include <linux/ioport.h>
8e345c99 25#include <linux/slab.h>
55a93417 26#include <linux/irq.h>
046d9ce6
RW
27
28#ifdef CONFIG_X86
29#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
4a2e7aab
LP
30static inline bool acpi_iospace_resource_valid(struct resource *res)
31{
32 /* On X86 IO space is limited to the [0 - 64K] IO port range */
33 return res->end < 0x10003;
34}
046d9ce6
RW
35#else
36#define valid_IRQ(i) (true)
4a2e7aab
LP
37/*
38 * ACPI IO descriptors on arches other than X86 contain MMIO CPU physical
39 * addresses mapping IO space in CPU physical address space, IO space
40 * resources can be placed anywhere in the 64-bit physical address space.
41 */
42static inline bool
43acpi_iospace_resource_valid(struct resource *res) { return true; }
046d9ce6
RW
44#endif
45
0c5a9002
AVF
46#if IS_ENABLED(CONFIG_ACPI_GENERIC_GSI)
47static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
48{
49 return ext_irq->resource_source.string_length == 0 &&
50 ext_irq->producer_consumer == ACPI_CONSUMER;
51}
52#else
53static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq)
54{
55 return true;
56}
57#endif
58
c420dbd1 59static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
046d9ce6 60{
c420dbd1 61 u64 reslen = end - start + 1;
046d9ce6 62
c420dbd1
TG
63 /*
64 * CHECKME: len might be required to check versus a minimum
65 * length as well. 1 for io is fine, but for memory it does
66 * not make any sense at all.
aa714d28
JL
67 * Note: some BIOSes report incorrect length for ACPI address space
68 * descriptor, so remove check of 'reslen == len' to avoid regression.
c420dbd1 69 */
aa714d28 70 if (len && reslen && start <= end)
c420dbd1
TG
71 return true;
72
6a239af2 73 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
c420dbd1
TG
74 io ? "io" : "mem", start, end, len);
75
76 return false;
77}
78
79static void acpi_dev_memresource_flags(struct resource *res, u64 len,
72e26b0d 80 u8 write_protect)
c420dbd1
TG
81{
82 res->flags = IORESOURCE_MEM;
83
84 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
c78b6885 85 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
046d9ce6
RW
86
87 if (write_protect == ACPI_READ_WRITE_MEMORY)
c420dbd1 88 res->flags |= IORESOURCE_MEM_WRITEABLE;
046d9ce6
RW
89}
90
91static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
92 u8 write_protect)
93{
94 res->start = start;
95 res->end = start + len - 1;
72e26b0d 96 acpi_dev_memresource_flags(res, len, write_protect);
046d9ce6
RW
97}
98
99/**
100 * acpi_dev_resource_memory - Extract ACPI memory resource information.
101 * @ares: Input ACPI resource object.
102 * @res: Output generic resource object.
103 *
104 * Check if the given ACPI resource object represents a memory resource and
105 * if that's the case, use the information in it to populate the generic
106 * resource object pointed to by @res.
581c19f3
JL
107 *
108 * Return:
109 * 1) false with res->flags setting to zero: not the expected resource type
110 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
111 * 3) true: valid assigned resource
046d9ce6
RW
112 */
113bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
114{
115 struct acpi_resource_memory24 *memory24;
116 struct acpi_resource_memory32 *memory32;
117 struct acpi_resource_fixed_memory32 *fixed_memory32;
118
119 switch (ares->type) {
120 case ACPI_RESOURCE_TYPE_MEMORY24:
121 memory24 = &ares->data.memory24;
8515f936
JL
122 acpi_dev_get_memresource(res, memory24->minimum << 8,
123 memory24->address_length << 8,
046d9ce6
RW
124 memory24->write_protect);
125 break;
126 case ACPI_RESOURCE_TYPE_MEMORY32:
127 memory32 = &ares->data.memory32;
128 acpi_dev_get_memresource(res, memory32->minimum,
129 memory32->address_length,
130 memory32->write_protect);
131 break;
132 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
133 fixed_memory32 = &ares->data.fixed_memory32;
134 acpi_dev_get_memresource(res, fixed_memory32->address,
135 fixed_memory32->address_length,
136 fixed_memory32->write_protect);
137 break;
138 default:
581c19f3 139 res->flags = 0;
046d9ce6
RW
140 return false;
141 }
c420dbd1
TG
142
143 return !(res->flags & IORESOURCE_DISABLED);
046d9ce6
RW
144}
145EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
146
1d0420f1 147static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
91236ecc 148 u8 io_decode, u8 translation_type)
046d9ce6 149{
1d0420f1 150 res->flags = IORESOURCE_IO;
046d9ce6 151
1d0420f1 152 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
c78b6885 153 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
1d0420f1 154
4a2e7aab 155 if (!acpi_iospace_resource_valid(res))
c78b6885 156 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
046d9ce6 157
1d0420f1
TG
158 if (io_decode == ACPI_DECODE_16)
159 res->flags |= IORESOURCE_IO_16BIT_ADDR;
91236ecc
JL
160 if (translation_type == ACPI_SPARSE_TRANSLATION)
161 res->flags |= IORESOURCE_IO_SPARSE;
046d9ce6
RW
162}
163
164static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
165 u8 io_decode)
166{
046d9ce6 167 res->start = start;
1d0420f1 168 res->end = start + len - 1;
91236ecc 169 acpi_dev_ioresource_flags(res, len, io_decode, 0);
046d9ce6
RW
170}
171
172/**
173 * acpi_dev_resource_io - Extract ACPI I/O resource information.
174 * @ares: Input ACPI resource object.
175 * @res: Output generic resource object.
176 *
177 * Check if the given ACPI resource object represents an I/O resource and
178 * if that's the case, use the information in it to populate the generic
179 * resource object pointed to by @res.
581c19f3
JL
180 *
181 * Return:
182 * 1) false with res->flags setting to zero: not the expected resource type
183 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
184 * 3) true: valid assigned resource
046d9ce6
RW
185 */
186bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
187{
188 struct acpi_resource_io *io;
189 struct acpi_resource_fixed_io *fixed_io;
190
191 switch (ares->type) {
192 case ACPI_RESOURCE_TYPE_IO:
193 io = &ares->data.io;
194 acpi_dev_get_ioresource(res, io->minimum,
195 io->address_length,
196 io->io_decode);
197 break;
198 case ACPI_RESOURCE_TYPE_FIXED_IO:
199 fixed_io = &ares->data.fixed_io;
200 acpi_dev_get_ioresource(res, fixed_io->address,
201 fixed_io->address_length,
202 ACPI_DECODE_10);
203 break;
204 default:
581c19f3 205 res->flags = 0;
046d9ce6
RW
206 return false;
207 }
1d0420f1
TG
208
209 return !(res->flags & IORESOURCE_DISABLED);
046d9ce6
RW
210}
211EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
212
a49170b5 213static bool acpi_decode_space(struct resource_win *win,
eb76d55e
TG
214 struct acpi_resource_address *addr,
215 struct acpi_address64_attribute *attr)
216{
217 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
eb76d55e
TG
218 bool wp = addr->info.mem.write_protect;
219 u64 len = attr->address_length;
1fb01ca9 220 u64 start, end, offset = 0;
a49170b5 221 struct resource *res = &win->res;
eb76d55e 222
a274019f
JL
223 /*
224 * Filter out invalid descriptor according to ACPI Spec 5.0, section
225 * 6.4.3.5 Address Space Resource Descriptors.
226 */
227 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
228 (addr->min_address_fixed && addr->max_address_fixed && !len))
229 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
230 addr->min_address_fixed, addr->max_address_fixed, len);
231
2ea3d266
JL
232 /*
233 * For bridges that translate addresses across the bridge,
234 * translation_offset is the offset that must be added to the
235 * address on the secondary side to obtain the address on the
236 * primary side. Non-bridge devices must list 0 for all Address
237 * Translation offset bits.
238 */
1fb01ca9
JL
239 if (addr->producer_consumer == ACPI_PRODUCER)
240 offset = attr->translation_offset;
241 else if (attr->translation_offset)
2ea3d266
JL
242 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
243 attr->translation_offset);
1fb01ca9
JL
244 start = attr->minimum + offset;
245 end = attr->maximum + offset;
246
247 win->offset = offset;
248 res->start = start;
249 res->end = end;
250 if (sizeof(resource_size_t) < sizeof(u64) &&
251 (offset != win->offset || start != res->start || end != res->end)) {
252 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
253 attr->minimum, attr->maximum);
254 return false;
2ea3d266
JL
255 }
256
eb76d55e
TG
257 switch (addr->resource_type) {
258 case ACPI_MEMORY_RANGE:
72e26b0d 259 acpi_dev_memresource_flags(res, len, wp);
eb76d55e
TG
260 break;
261 case ACPI_IO_RANGE:
91236ecc
JL
262 acpi_dev_ioresource_flags(res, len, iodec,
263 addr->info.io.translation_type);
eb76d55e
TG
264 break;
265 case ACPI_BUS_NUMBER_RANGE:
266 res->flags = IORESOURCE_BUS;
267 break;
268 default:
269 return false;
270 }
271
72e26b0d
TG
272 if (addr->producer_consumer == ACPI_PRODUCER)
273 res->flags |= IORESOURCE_WINDOW;
fcb29bbc
TG
274
275 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
276 res->flags |= IORESOURCE_PREFETCH;
72e26b0d 277
eb76d55e
TG
278 return !(res->flags & IORESOURCE_DISABLED);
279}
280
046d9ce6
RW
281/**
282 * acpi_dev_resource_address_space - Extract ACPI address space information.
283 * @ares: Input ACPI resource object.
a49170b5 284 * @win: Output generic resource object.
046d9ce6
RW
285 *
286 * Check if the given ACPI resource object represents an address space resource
287 * and if that's the case, use the information in it to populate the generic
a49170b5 288 * resource object pointed to by @win.
581c19f3
JL
289 *
290 * Return:
a49170b5
JL
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
293 * resource
581c19f3 294 * 3) true: valid assigned resource
046d9ce6
RW
295 */
296bool acpi_dev_resource_address_space(struct acpi_resource *ares,
a49170b5 297 struct resource_win *win)
046d9ce6 298{
046d9ce6 299 struct acpi_resource_address64 addr;
046d9ce6 300
a49170b5 301 win->res.flags = 0;
eb76d55e 302 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
6658c739 303 return false;
046d9ce6 304
a49170b5 305 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
eb76d55e 306 &addr.address);
046d9ce6
RW
307}
308EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
309
310/**
311 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
312 * @ares: Input ACPI resource object.
a49170b5 313 * @win: Output generic resource object.
046d9ce6
RW
314 *
315 * Check if the given ACPI resource object represents an extended address space
316 * resource and if that's the case, use the information in it to populate the
a49170b5 317 * generic resource object pointed to by @win.
581c19f3
JL
318 *
319 * Return:
a49170b5
JL
320 * 1) false with win->res.flags setting to zero: not the expected resource type
321 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
322 * resource
581c19f3 323 * 3) true: valid assigned resource
046d9ce6
RW
324 */
325bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
a49170b5 326 struct resource_win *win)
046d9ce6
RW
327{
328 struct acpi_resource_extended_address64 *ext_addr;
046d9ce6 329
a49170b5 330 win->res.flags = 0;
046d9ce6
RW
331 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
332 return false;
333
334 ext_addr = &ares->data.ext_address64;
335
a49170b5 336 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
eb76d55e 337 &ext_addr->address);
046d9ce6
RW
338}
339EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
340
341/**
342 * acpi_dev_irq_flags - Determine IRQ resource flags.
343 * @triggering: Triggering type as provided by ACPI.
344 * @polarity: Interrupt polarity as provided by ACPI.
345 * @shareable: Whether or not the interrupt is shareable.
346 */
347unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
348{
349 unsigned long flags;
350
351 if (triggering == ACPI_LEVEL_SENSITIVE)
352 flags = polarity == ACPI_ACTIVE_LOW ?
353 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
354 else
355 flags = polarity == ACPI_ACTIVE_LOW ?
356 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
357
358 if (shareable == ACPI_SHARED)
359 flags |= IORESOURCE_IRQ_SHAREABLE;
360
361 return flags | IORESOURCE_IRQ;
362}
363EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
364
55a93417
CR
365/**
366 * acpi_dev_get_irq_type - Determine irq type.
367 * @triggering: Triggering type as provided by ACPI.
368 * @polarity: Interrupt polarity as provided by ACPI.
369 */
370unsigned int acpi_dev_get_irq_type(int triggering, int polarity)
371{
372 switch (polarity) {
373 case ACPI_ACTIVE_LOW:
374 return triggering == ACPI_EDGE_SENSITIVE ?
375 IRQ_TYPE_EDGE_FALLING :
376 IRQ_TYPE_LEVEL_LOW;
377 case ACPI_ACTIVE_HIGH:
378 return triggering == ACPI_EDGE_SENSITIVE ?
379 IRQ_TYPE_EDGE_RISING :
380 IRQ_TYPE_LEVEL_HIGH;
381 case ACPI_ACTIVE_BOTH:
382 if (triggering == ACPI_EDGE_SENSITIVE)
383 return IRQ_TYPE_EDGE_BOTH;
384 default:
385 return IRQ_TYPE_NONE;
386 }
387}
388EXPORT_SYMBOL_GPL(acpi_dev_get_irq_type);
389
046d9ce6
RW
390static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
391{
392 res->start = gsi;
393 res->end = gsi;
c78b6885 394 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
046d9ce6
RW
395}
396
397static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
204ebc0a
MW
398 u8 triggering, u8 polarity, u8 shareable,
399 bool legacy)
046d9ce6
RW
400{
401 int irq, p, t;
402
403 if (!valid_IRQ(gsi)) {
404 acpi_dev_irqresource_disabled(res, gsi);
405 return;
406 }
407
408 /*
409 * In IO-APIC mode, use overrided attribute. Two reasons:
410 * 1. BIOS bug in DSDT
411 * 2. BIOS uses IO-APIC mode Interrupt Source Override
204ebc0a
MW
412 *
413 * We do this only if we are dealing with IRQ() or IRQNoFlags()
414 * resource (the legacy ISA resources). With modern ACPI 5 devices
415 * using extended IRQ descriptors we take the IRQ configuration
416 * from _CRS directly.
046d9ce6 417 */
204ebc0a 418 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
046d9ce6
RW
419 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
420 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
421
422 if (triggering != trig || polarity != pol) {
423 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
204ebc0a 424 t ? "level" : "edge", p ? "low" : "high");
046d9ce6
RW
425 triggering = trig;
426 polarity = pol;
427 }
428 }
429
430 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
431 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
432 if (irq >= 0) {
433 res->start = irq;
434 res->end = irq;
435 } else {
436 acpi_dev_irqresource_disabled(res, gsi);
437 }
438}
439
440/**
441 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
442 * @ares: Input ACPI resource object.
443 * @index: Index into the array of GSIs represented by the resource.
444 * @res: Output generic resource object.
445 *
446 * Check if the given ACPI resource object represents an interrupt resource
447 * and @index does not exceed the resource's interrupt count (true is returned
448 * in that case regardless of the results of the other checks)). If that's the
449 * case, register the GSI corresponding to @index from the array of interrupts
450 * represented by the resource and populate the generic resource object pointed
451 * to by @res accordingly. If the registration of the GSI is not successful,
452 * IORESOURCE_DISABLED will be set it that object's flags.
581c19f3
JL
453 *
454 * Return:
455 * 1) false with res->flags setting to zero: not the expected resource type
456 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
457 * 3) true: valid assigned resource
046d9ce6
RW
458 */
459bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
460 struct resource *res)
461{
462 struct acpi_resource_irq *irq;
463 struct acpi_resource_extended_irq *ext_irq;
464
465 switch (ares->type) {
466 case ACPI_RESOURCE_TYPE_IRQ:
467 /*
468 * Per spec, only one interrupt per descriptor is allowed in
469 * _CRS, but some firmware violates this, so parse them all.
470 */
471 irq = &ares->data.irq;
472 if (index >= irq->interrupt_count) {
473 acpi_dev_irqresource_disabled(res, 0);
474 return false;
475 }
476 acpi_dev_get_irqresource(res, irq->interrupts[index],
477 irq->triggering, irq->polarity,
204ebc0a 478 irq->sharable, true);
046d9ce6
RW
479 break;
480 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
481 ext_irq = &ares->data.extended_irq;
482 if (index >= ext_irq->interrupt_count) {
483 acpi_dev_irqresource_disabled(res, 0);
484 return false;
485 }
0c5a9002
AVF
486 if (is_gsi(ext_irq))
487 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
046d9ce6 488 ext_irq->triggering, ext_irq->polarity,
204ebc0a 489 ext_irq->sharable, false);
0c5a9002
AVF
490 else
491 acpi_dev_irqresource_disabled(res, 0);
046d9ce6
RW
492 break;
493 default:
581c19f3 494 res->flags = 0;
046d9ce6
RW
495 return false;
496 }
497
498 return true;
499}
500EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
8e345c99
RW
501
502/**
503 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
504 * @list: The head of the resource list to free.
505 */
506void acpi_dev_free_resource_list(struct list_head *list)
507{
90e97820 508 resource_list_free(list);
8e345c99
RW
509}
510EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
511
512struct res_proc_context {
513 struct list_head *list;
514 int (*preproc)(struct acpi_resource *, void *);
515 void *preproc_data;
516 int count;
517 int error;
518};
519
a49170b5 520static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
8e345c99
RW
521 struct res_proc_context *c)
522{
90e97820 523 struct resource_entry *rentry;
8e345c99 524
90e97820 525 rentry = resource_list_create_entry(NULL, 0);
8e345c99
RW
526 if (!rentry) {
527 c->error = -ENOMEM;
528 return AE_NO_MEMORY;
529 }
90e97820 530 *rentry->res = win->res;
93286f47 531 rentry->offset = win->offset;
90e97820 532 resource_list_add_tail(rentry, c->list);
8e345c99
RW
533 c->count++;
534 return AE_OK;
535}
536
537static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
538 void *context)
539{
540 struct res_proc_context *c = context;
a49170b5
JL
541 struct resource_win win;
542 struct resource *res = &win.res;
8e345c99
RW
543 int i;
544
545 if (c->preproc) {
546 int ret;
547
548 ret = c->preproc(ares, c->preproc_data);
549 if (ret < 0) {
550 c->error = ret;
8a66790b 551 return AE_CTRL_TERMINATE;
8e345c99
RW
552 } else if (ret > 0) {
553 return AE_OK;
554 }
555 }
556
a49170b5 557 memset(&win, 0, sizeof(win));
8e345c99 558
a49170b5
JL
559 if (acpi_dev_resource_memory(ares, res)
560 || acpi_dev_resource_io(ares, res)
561 || acpi_dev_resource_address_space(ares, &win)
562 || acpi_dev_resource_ext_address_space(ares, &win))
563 return acpi_dev_new_resource_entry(&win, c);
8e345c99 564
a49170b5 565 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
8e345c99
RW
566 acpi_status status;
567
a49170b5 568 status = acpi_dev_new_resource_entry(&win, c);
8e345c99
RW
569 if (ACPI_FAILURE(status))
570 return status;
571 }
572
573 return AE_OK;
574}
575
576/**
577 * acpi_dev_get_resources - Get current resources of a device.
578 * @adev: ACPI device node to get the resources for.
579 * @list: Head of the resultant list of resources (must be empty).
580 * @preproc: The caller's preprocessing routine.
581 * @preproc_data: Pointer passed to the caller's preprocessing routine.
582 *
583 * Evaluate the _CRS method for the given device node and process its output by
584 * (1) executing the @preproc() rountine provided by the caller, passing the
585 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
586 * returned and (2) converting all of the returned ACPI resources into struct
587 * resource objects if possible. If the return value of @preproc() in step (1)
588 * is different from 0, step (2) is not applied to the given ACPI resource and
589 * if that value is negative, the whole processing is aborted and that value is
590 * returned as the final error code.
591 *
592 * The resultant struct resource objects are put on the list pointed to by
90e97820 593 * @list, that must be empty initially, as members of struct resource_entry
8e345c99
RW
594 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
595 * free that list.
596 *
597 * The number of resources in the output list is returned on success, an error
598 * code reflecting the error condition is returned otherwise.
599 */
600int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
601 int (*preproc)(struct acpi_resource *, void *),
602 void *preproc_data)
603{
604 struct res_proc_context c;
8e345c99
RW
605 acpi_status status;
606
607 if (!adev || !adev->handle || !list_empty(list))
608 return -EINVAL;
609
952c63e9 610 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
8e345c99
RW
611 return 0;
612
613 c.list = list;
614 c.preproc = preproc;
615 c.preproc_data = preproc_data;
616 c.count = 0;
617 c.error = 0;
618 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
619 acpi_dev_process_resource, &c);
620 if (ACPI_FAILURE(status)) {
621 acpi_dev_free_resource_list(list);
622 return c.error ? c.error : -EIO;
623 }
624
625 return c.count;
626}
627EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
62d1141f
JL
628
629/**
630 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
631 * types
632 * @ares: Input ACPI resource object.
633 * @types: Valid resource types of IORESOURCE_XXX
634 *
2c62e849 635 * This is a helper function to support acpi_dev_get_resources(), which filters
62d1141f
JL
636 * ACPI resource objects according to resource types.
637 */
638int acpi_dev_filter_resource_type(struct acpi_resource *ares,
639 unsigned long types)
640{
641 unsigned long type = 0;
642
643 switch (ares->type) {
644 case ACPI_RESOURCE_TYPE_MEMORY24:
645 case ACPI_RESOURCE_TYPE_MEMORY32:
646 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
647 type = IORESOURCE_MEM;
648 break;
649 case ACPI_RESOURCE_TYPE_IO:
650 case ACPI_RESOURCE_TYPE_FIXED_IO:
651 type = IORESOURCE_IO;
652 break;
653 case ACPI_RESOURCE_TYPE_IRQ:
654 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
655 type = IORESOURCE_IRQ;
656 break;
657 case ACPI_RESOURCE_TYPE_DMA:
658 case ACPI_RESOURCE_TYPE_FIXED_DMA:
659 type = IORESOURCE_DMA;
660 break;
661 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
662 type = IORESOURCE_REG;
663 break;
664 case ACPI_RESOURCE_TYPE_ADDRESS16:
665 case ACPI_RESOURCE_TYPE_ADDRESS32:
666 case ACPI_RESOURCE_TYPE_ADDRESS64:
667 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
668 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
669 type = IORESOURCE_MEM;
670 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
671 type = IORESOURCE_IO;
672 else if (ares->data.address.resource_type ==
673 ACPI_BUS_NUMBER_RANGE)
674 type = IORESOURCE_BUS;
675 break;
676 default:
677 break;
678 }
679
680 return (type & types) ? 0 : 1;
681}
682EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);
00710984
BH
683
684static int acpi_dev_consumes_res(struct acpi_device *adev, struct resource *res)
685{
686 struct list_head resource_list;
687 struct resource_entry *rentry;
688 int ret, found = 0;
689
690 INIT_LIST_HEAD(&resource_list);
691 ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
692 if (ret < 0)
693 return 0;
694
695 list_for_each_entry(rentry, &resource_list, node) {
696 if (resource_contains(rentry->res, res)) {
697 found = 1;
698 break;
699 }
700
701 }
702
703 acpi_dev_free_resource_list(&resource_list);
704 return found;
705}
706
707static acpi_status acpi_res_consumer_cb(acpi_handle handle, u32 depth,
708 void *context, void **ret)
709{
710 struct resource *res = context;
711 struct acpi_device **consumer = (struct acpi_device **) ret;
712 struct acpi_device *adev;
713
714 if (acpi_bus_get_device(handle, &adev))
715 return AE_OK;
716
717 if (acpi_dev_consumes_res(adev, res)) {
718 *consumer = adev;
719 return AE_CTRL_TERMINATE;
720 }
721
722 return AE_OK;
723}
724
725/**
726 * acpi_resource_consumer - Find the ACPI device that consumes @res.
727 * @res: Resource to search for.
728 *
729 * Search the current resource settings (_CRS) of every ACPI device node
730 * for @res. If we find an ACPI device whose _CRS includes @res, return
731 * it. Otherwise, return NULL.
732 */
733struct acpi_device *acpi_resource_consumer(struct resource *res)
734{
735 struct acpi_device *consumer = NULL;
736
737 acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer);
738 return consumer;
739}