blob: c723668e3e277def6f8d6309fe1af21b989951fb [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
Rafael J. Wysocki6a239af2015-02-18 08:05:43 +010049 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080050 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
Jiang Liua49170b2015-02-02 10:42:58 +0800187static bool acpi_decode_space(struct resource_win *win,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800188 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;
Jiang Liua49170b2015-02-02 10:42:58 +0800194 struct resource *res = &win->res;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800195
Jiang Liua2740192015-02-02 10:42:57 +0800196 /*
197 * Filter out invalid descriptor according to ACPI Spec 5.0, section
198 * 6.4.3.5 Address Space Resource Descriptors.
199 */
200 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
201 (addr->min_address_fixed && addr->max_address_fixed && !len))
202 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
203 addr->min_address_fixed, addr->max_address_fixed, len);
204
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800205 res->start = attr->minimum;
206 res->end = attr->maximum;
207
Jiang Liu2ea3d262015-02-02 10:42:59 +0800208 /*
209 * For bridges that translate addresses across the bridge,
210 * translation_offset is the offset that must be added to the
211 * address on the secondary side to obtain the address on the
212 * primary side. Non-bridge devices must list 0 for all Address
213 * Translation offset bits.
214 */
215 if (addr->producer_consumer == ACPI_PRODUCER) {
216 res->start += attr->translation_offset;
217 res->end += attr->translation_offset;
218 } else if (attr->translation_offset) {
219 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
220 attr->translation_offset);
221 }
222
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800223 switch (addr->resource_type) {
224 case ACPI_MEMORY_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800225 acpi_dev_memresource_flags(res, len, wp);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800226 break;
227 case ACPI_IO_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800228 acpi_dev_ioresource_flags(res, len, iodec);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800229 break;
230 case ACPI_BUS_NUMBER_RANGE:
231 res->flags = IORESOURCE_BUS;
232 break;
233 default:
234 return false;
235 }
236
Jiang Liua49170b2015-02-02 10:42:58 +0800237 win->offset = attr->translation_offset;
238
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800239 if (addr->producer_consumer == ACPI_PRODUCER)
240 res->flags |= IORESOURCE_WINDOW;
241
Thomas Gleixnerfcb29bb2015-02-02 10:42:53 +0800242 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
243 res->flags |= IORESOURCE_PREFETCH;
244
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800245 return !(res->flags & IORESOURCE_DISABLED);
246}
247
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100248/**
249 * acpi_dev_resource_address_space - Extract ACPI address space information.
250 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800251 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100252 *
253 * Check if the given ACPI resource object represents an address space resource
254 * and if that's the case, use the information in it to populate the generic
Jiang Liua49170b2015-02-02 10:42:58 +0800255 * resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800256 *
257 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800258 * 1) false with win->res.flags setting to zero: not the expected resource type
259 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
260 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800261 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100262 */
263bool acpi_dev_resource_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800264 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100265{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100266 struct acpi_resource_address64 addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100267
Jiang Liua49170b2015-02-02 10:42:58 +0800268 win->res.flags = 0;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800269 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
Jiang Liu6658c732014-10-27 13:21:35 +0800270 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100271
Jiang Liua49170b2015-02-02 10:42:58 +0800272 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800273 &addr.address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100274}
275EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
276
277/**
278 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
279 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800280 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100281 *
282 * Check if the given ACPI resource object represents an extended address space
283 * resource and if that's the case, use the information in it to populate the
Jiang Liua49170b2015-02-02 10:42:58 +0800284 * generic resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800285 *
286 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800287 * 1) false with win->res.flags setting to zero: not the expected resource type
288 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
289 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800290 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100291 */
292bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800293 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100294{
295 struct acpi_resource_extended_address64 *ext_addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100296
Jiang Liua49170b2015-02-02 10:42:58 +0800297 win->res.flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100298 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
299 return false;
300
301 ext_addr = &ares->data.ext_address64;
302
Jiang Liua49170b2015-02-02 10:42:58 +0800303 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800304 &ext_addr->address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100305}
306EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
307
308/**
309 * acpi_dev_irq_flags - Determine IRQ resource flags.
310 * @triggering: Triggering type as provided by ACPI.
311 * @polarity: Interrupt polarity as provided by ACPI.
312 * @shareable: Whether or not the interrupt is shareable.
313 */
314unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
315{
316 unsigned long flags;
317
318 if (triggering == ACPI_LEVEL_SENSITIVE)
319 flags = polarity == ACPI_ACTIVE_LOW ?
320 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
321 else
322 flags = polarity == ACPI_ACTIVE_LOW ?
323 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
324
325 if (shareable == ACPI_SHARED)
326 flags |= IORESOURCE_IRQ_SHAREABLE;
327
328 return flags | IORESOURCE_IRQ;
329}
330EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
331
332static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
333{
334 res->start = gsi;
335 res->end = gsi;
Jiang Liuc78b6882015-02-02 10:42:56 +0800336 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100337}
338
339static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000340 u8 triggering, u8 polarity, u8 shareable,
341 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100342{
343 int irq, p, t;
344
345 if (!valid_IRQ(gsi)) {
346 acpi_dev_irqresource_disabled(res, gsi);
347 return;
348 }
349
350 /*
351 * In IO-APIC mode, use overrided attribute. Two reasons:
352 * 1. BIOS bug in DSDT
353 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000354 *
355 * We do this only if we are dealing with IRQ() or IRQNoFlags()
356 * resource (the legacy ISA resources). With modern ACPI 5 devices
357 * using extended IRQ descriptors we take the IRQ configuration
358 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100359 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000360 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100361 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
362 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
363
364 if (triggering != trig || polarity != pol) {
365 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000366 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100367 triggering = trig;
368 polarity = pol;
369 }
370 }
371
372 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
373 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
374 if (irq >= 0) {
375 res->start = irq;
376 res->end = irq;
377 } else {
378 acpi_dev_irqresource_disabled(res, gsi);
379 }
380}
381
382/**
383 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
384 * @ares: Input ACPI resource object.
385 * @index: Index into the array of GSIs represented by the resource.
386 * @res: Output generic resource object.
387 *
388 * Check if the given ACPI resource object represents an interrupt resource
389 * and @index does not exceed the resource's interrupt count (true is returned
390 * in that case regardless of the results of the other checks)). If that's the
391 * case, register the GSI corresponding to @index from the array of interrupts
392 * represented by the resource and populate the generic resource object pointed
393 * to by @res accordingly. If the registration of the GSI is not successful,
394 * IORESOURCE_DISABLED will be set it that object's flags.
Jiang Liu581c19f2015-02-02 10:42:55 +0800395 *
396 * Return:
397 * 1) false with res->flags setting to zero: not the expected resource type
398 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
399 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100400 */
401bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
402 struct resource *res)
403{
404 struct acpi_resource_irq *irq;
405 struct acpi_resource_extended_irq *ext_irq;
406
407 switch (ares->type) {
408 case ACPI_RESOURCE_TYPE_IRQ:
409 /*
410 * Per spec, only one interrupt per descriptor is allowed in
411 * _CRS, but some firmware violates this, so parse them all.
412 */
413 irq = &ares->data.irq;
414 if (index >= irq->interrupt_count) {
415 acpi_dev_irqresource_disabled(res, 0);
416 return false;
417 }
418 acpi_dev_get_irqresource(res, irq->interrupts[index],
419 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000420 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100421 break;
422 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
423 ext_irq = &ares->data.extended_irq;
424 if (index >= ext_irq->interrupt_count) {
425 acpi_dev_irqresource_disabled(res, 0);
426 return false;
427 }
428 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
429 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000430 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100431 break;
432 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800433 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100434 return false;
435 }
436
437 return true;
438}
439EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100440
441/**
442 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
443 * @list: The head of the resource list to free.
444 */
445void acpi_dev_free_resource_list(struct list_head *list)
446{
Jiang Liu90e97822015-02-05 13:44:43 +0800447 resource_list_free(list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100448}
449EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
450
451struct res_proc_context {
452 struct list_head *list;
453 int (*preproc)(struct acpi_resource *, void *);
454 void *preproc_data;
455 int count;
456 int error;
457};
458
Jiang Liua49170b2015-02-02 10:42:58 +0800459static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100460 struct res_proc_context *c)
461{
Jiang Liu90e97822015-02-05 13:44:43 +0800462 struct resource_entry *rentry;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100463
Jiang Liu90e97822015-02-05 13:44:43 +0800464 rentry = resource_list_create_entry(NULL, 0);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100465 if (!rentry) {
466 c->error = -ENOMEM;
467 return AE_NO_MEMORY;
468 }
Jiang Liu90e97822015-02-05 13:44:43 +0800469 *rentry->res = win->res;
Jiang Liu93286f42015-02-02 10:43:00 +0800470 rentry->offset = win->offset;
Jiang Liu90e97822015-02-05 13:44:43 +0800471 resource_list_add_tail(rentry, c->list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100472 c->count++;
473 return AE_OK;
474}
475
476static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
477 void *context)
478{
479 struct res_proc_context *c = context;
Jiang Liua49170b2015-02-02 10:42:58 +0800480 struct resource_win win;
481 struct resource *res = &win.res;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100482 int i;
483
484 if (c->preproc) {
485 int ret;
486
487 ret = c->preproc(ares, c->preproc_data);
488 if (ret < 0) {
489 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100490 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100491 } else if (ret > 0) {
492 return AE_OK;
493 }
494 }
495
Jiang Liua49170b2015-02-02 10:42:58 +0800496 memset(&win, 0, sizeof(win));
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100497
Jiang Liua49170b2015-02-02 10:42:58 +0800498 if (acpi_dev_resource_memory(ares, res)
499 || acpi_dev_resource_io(ares, res)
500 || acpi_dev_resource_address_space(ares, &win)
501 || acpi_dev_resource_ext_address_space(ares, &win))
502 return acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100503
Jiang Liua49170b2015-02-02 10:42:58 +0800504 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100505 acpi_status status;
506
Jiang Liua49170b2015-02-02 10:42:58 +0800507 status = acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100508 if (ACPI_FAILURE(status))
509 return status;
510 }
511
512 return AE_OK;
513}
514
515/**
516 * acpi_dev_get_resources - Get current resources of a device.
517 * @adev: ACPI device node to get the resources for.
518 * @list: Head of the resultant list of resources (must be empty).
519 * @preproc: The caller's preprocessing routine.
520 * @preproc_data: Pointer passed to the caller's preprocessing routine.
521 *
522 * Evaluate the _CRS method for the given device node and process its output by
523 * (1) executing the @preproc() rountine provided by the caller, passing the
524 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
525 * returned and (2) converting all of the returned ACPI resources into struct
526 * resource objects if possible. If the return value of @preproc() in step (1)
527 * is different from 0, step (2) is not applied to the given ACPI resource and
528 * if that value is negative, the whole processing is aborted and that value is
529 * returned as the final error code.
530 *
531 * The resultant struct resource objects are put on the list pointed to by
Jiang Liu90e97822015-02-05 13:44:43 +0800532 * @list, that must be empty initially, as members of struct resource_entry
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100533 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
534 * free that list.
535 *
536 * The number of resources in the output list is returned on success, an error
537 * code reflecting the error condition is returned otherwise.
538 */
539int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
540 int (*preproc)(struct acpi_resource *, void *),
541 void *preproc_data)
542{
543 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100544 acpi_status status;
545
546 if (!adev || !adev->handle || !list_empty(list))
547 return -EINVAL;
548
Jiang Liu952c63e2013-06-29 00:24:38 +0800549 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100550 return 0;
551
552 c.list = list;
553 c.preproc = preproc;
554 c.preproc_data = preproc_data;
555 c.count = 0;
556 c.error = 0;
557 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
558 acpi_dev_process_resource, &c);
559 if (ACPI_FAILURE(status)) {
560 acpi_dev_free_resource_list(list);
561 return c.error ? c.error : -EIO;
562 }
563
564 return c.count;
565}
566EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
Jiang Liu62d11412015-02-02 10:43:01 +0800567
568/**
569 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
570 * types
571 * @ares: Input ACPI resource object.
572 * @types: Valid resource types of IORESOURCE_XXX
573 *
574 * This is a hepler function to support acpi_dev_get_resources(), which filters
575 * ACPI resource objects according to resource types.
576 */
577int acpi_dev_filter_resource_type(struct acpi_resource *ares,
578 unsigned long types)
579{
580 unsigned long type = 0;
581
582 switch (ares->type) {
583 case ACPI_RESOURCE_TYPE_MEMORY24:
584 case ACPI_RESOURCE_TYPE_MEMORY32:
585 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
586 type = IORESOURCE_MEM;
587 break;
588 case ACPI_RESOURCE_TYPE_IO:
589 case ACPI_RESOURCE_TYPE_FIXED_IO:
590 type = IORESOURCE_IO;
591 break;
592 case ACPI_RESOURCE_TYPE_IRQ:
593 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
594 type = IORESOURCE_IRQ;
595 break;
596 case ACPI_RESOURCE_TYPE_DMA:
597 case ACPI_RESOURCE_TYPE_FIXED_DMA:
598 type = IORESOURCE_DMA;
599 break;
600 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
601 type = IORESOURCE_REG;
602 break;
603 case ACPI_RESOURCE_TYPE_ADDRESS16:
604 case ACPI_RESOURCE_TYPE_ADDRESS32:
605 case ACPI_RESOURCE_TYPE_ADDRESS64:
606 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
607 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
608 type = IORESOURCE_MEM;
609 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
610 type = IORESOURCE_IO;
611 else if (ares->data.address.resource_type ==
612 ACPI_BUS_NUMBER_RANGE)
613 type = IORESOURCE_BUS;
614 break;
615 default:
616 break;
617 }
618
619 return (type & types) ? 0 : 1;
620}
621EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);