blob: 5589a6e2a02346e3b2ce48656b3facea1abfc621 [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.
Jiang Liuaa714d22015-03-04 16:47:12 +080045 * Note: some BIOSes report incorrect length for ACPI address space
46 * descriptor, so remove check of 'reslen == len' to avoid regression.
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080047 */
Jiang Liuaa714d22015-03-04 16:47:12 +080048 if (len && reslen && start <= end)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080049 return true;
50
Rafael J. Wysocki6a239af2015-02-18 08:05:43 +010051 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080052 io ? "io" : "mem", start, end, len);
53
54 return false;
55}
56
57static void acpi_dev_memresource_flags(struct resource *res, u64 len,
Thomas Gleixner72e26b02015-02-02 10:42:52 +080058 u8 write_protect)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080059{
60 res->flags = IORESOURCE_MEM;
61
62 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
Jiang Liuc78b6882015-02-02 10:42:56 +080063 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010064
65 if (write_protect == ACPI_READ_WRITE_MEMORY)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080066 res->flags |= IORESOURCE_MEM_WRITEABLE;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010067}
68
69static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
70 u8 write_protect)
71{
72 res->start = start;
73 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +080074 acpi_dev_memresource_flags(res, len, write_protect);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010075}
76
77/**
78 * acpi_dev_resource_memory - Extract ACPI memory resource information.
79 * @ares: Input ACPI resource object.
80 * @res: Output generic resource object.
81 *
82 * Check if the given ACPI resource object represents a memory resource and
83 * if that's the case, use the information in it to populate the generic
84 * resource object pointed to by @res.
Jiang Liu581c19f2015-02-02 10:42:55 +080085 *
86 * Return:
87 * 1) false with res->flags setting to zero: not the expected resource type
88 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
89 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010090 */
91bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
92{
93 struct acpi_resource_memory24 *memory24;
94 struct acpi_resource_memory32 *memory32;
95 struct acpi_resource_fixed_memory32 *fixed_memory32;
96
97 switch (ares->type) {
98 case ACPI_RESOURCE_TYPE_MEMORY24:
99 memory24 = &ares->data.memory24;
Jiang Liu8515f932015-02-02 10:42:54 +0800100 acpi_dev_get_memresource(res, memory24->minimum << 8,
101 memory24->address_length << 8,
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100102 memory24->write_protect);
103 break;
104 case ACPI_RESOURCE_TYPE_MEMORY32:
105 memory32 = &ares->data.memory32;
106 acpi_dev_get_memresource(res, memory32->minimum,
107 memory32->address_length,
108 memory32->write_protect);
109 break;
110 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
111 fixed_memory32 = &ares->data.fixed_memory32;
112 acpi_dev_get_memresource(res, fixed_memory32->address,
113 fixed_memory32->address_length,
114 fixed_memory32->write_protect);
115 break;
116 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800117 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100118 return false;
119 }
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800120
121 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100122}
123EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
124
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800125static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800126 u8 io_decode)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100127{
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800128 res->flags = IORESOURCE_IO;
129
130 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
Jiang Liuc78b6882015-02-02 10:42:56 +0800131 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800132
133 if (res->end >= 0x10003)
Jiang Liuc78b6882015-02-02 10:42:56 +0800134 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100135
136 if (io_decode == ACPI_DECODE_16)
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800137 res->flags |= IORESOURCE_IO_16BIT_ADDR;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100138}
139
140static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
141 u8 io_decode)
142{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100143 res->start = start;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800144 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800145 acpi_dev_ioresource_flags(res, len, io_decode);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100146}
147
148/**
149 * acpi_dev_resource_io - Extract ACPI I/O resource information.
150 * @ares: Input ACPI resource object.
151 * @res: Output generic resource object.
152 *
153 * Check if the given ACPI resource object represents an I/O resource and
154 * if that's the case, use the information in it to populate the generic
155 * resource object pointed to by @res.
Jiang Liu581c19f2015-02-02 10:42:55 +0800156 *
157 * Return:
158 * 1) false with res->flags setting to zero: not the expected resource type
159 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
160 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100161 */
162bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
163{
164 struct acpi_resource_io *io;
165 struct acpi_resource_fixed_io *fixed_io;
166
167 switch (ares->type) {
168 case ACPI_RESOURCE_TYPE_IO:
169 io = &ares->data.io;
170 acpi_dev_get_ioresource(res, io->minimum,
171 io->address_length,
172 io->io_decode);
173 break;
174 case ACPI_RESOURCE_TYPE_FIXED_IO:
175 fixed_io = &ares->data.fixed_io;
176 acpi_dev_get_ioresource(res, fixed_io->address,
177 fixed_io->address_length,
178 ACPI_DECODE_10);
179 break;
180 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800181 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100182 return false;
183 }
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800184
185 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100186}
187EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
188
Jiang Liua49170b2015-02-02 10:42:58 +0800189static bool acpi_decode_space(struct resource_win *win,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800190 struct acpi_resource_address *addr,
191 struct acpi_address64_attribute *attr)
192{
193 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800194 bool wp = addr->info.mem.write_protect;
195 u64 len = attr->address_length;
Jiang Liua49170b2015-02-02 10:42:58 +0800196 struct resource *res = &win->res;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800197
Jiang Liua2740192015-02-02 10:42:57 +0800198 /*
199 * Filter out invalid descriptor according to ACPI Spec 5.0, section
200 * 6.4.3.5 Address Space Resource Descriptors.
201 */
202 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
203 (addr->min_address_fixed && addr->max_address_fixed && !len))
204 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
205 addr->min_address_fixed, addr->max_address_fixed, len);
206
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800207 res->start = attr->minimum;
208 res->end = attr->maximum;
209
Jiang Liu2ea3d262015-02-02 10:42:59 +0800210 /*
211 * For bridges that translate addresses across the bridge,
212 * translation_offset is the offset that must be added to the
213 * address on the secondary side to obtain the address on the
214 * primary side. Non-bridge devices must list 0 for all Address
215 * Translation offset bits.
216 */
217 if (addr->producer_consumer == ACPI_PRODUCER) {
218 res->start += attr->translation_offset;
219 res->end += attr->translation_offset;
220 } else if (attr->translation_offset) {
221 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
222 attr->translation_offset);
223 }
224
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800225 switch (addr->resource_type) {
226 case ACPI_MEMORY_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800227 acpi_dev_memresource_flags(res, len, wp);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800228 break;
229 case ACPI_IO_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800230 acpi_dev_ioresource_flags(res, len, iodec);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800231 break;
232 case ACPI_BUS_NUMBER_RANGE:
233 res->flags = IORESOURCE_BUS;
234 break;
235 default:
236 return false;
237 }
238
Jiang Liua49170b2015-02-02 10:42:58 +0800239 win->offset = attr->translation_offset;
240
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800241 if (addr->producer_consumer == ACPI_PRODUCER)
242 res->flags |= IORESOURCE_WINDOW;
243
Thomas Gleixnerfcb29bb2015-02-02 10:42:53 +0800244 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
245 res->flags |= IORESOURCE_PREFETCH;
246
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800247 return !(res->flags & IORESOURCE_DISABLED);
248}
249
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100250/**
251 * acpi_dev_resource_address_space - Extract ACPI address space information.
252 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800253 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100254 *
255 * Check if the given ACPI resource object represents an address space resource
256 * and if that's the case, use the information in it to populate the generic
Jiang Liua49170b2015-02-02 10:42:58 +0800257 * resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800258 *
259 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800260 * 1) false with win->res.flags setting to zero: not the expected resource type
261 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
262 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800263 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100264 */
265bool acpi_dev_resource_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800266 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100267{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100268 struct acpi_resource_address64 addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100269
Jiang Liua49170b2015-02-02 10:42:58 +0800270 win->res.flags = 0;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800271 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
Jiang Liu6658c732014-10-27 13:21:35 +0800272 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100273
Jiang Liua49170b2015-02-02 10:42:58 +0800274 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800275 &addr.address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100276}
277EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
278
279/**
280 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
281 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800282 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100283 *
284 * Check if the given ACPI resource object represents an extended address space
285 * resource and if that's the case, use the information in it to populate the
Jiang Liua49170b2015-02-02 10:42:58 +0800286 * generic resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800287 *
288 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800289 * 1) false with win->res.flags setting to zero: not the expected resource type
290 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
291 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800292 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100293 */
294bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800295 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100296{
297 struct acpi_resource_extended_address64 *ext_addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100298
Jiang Liua49170b2015-02-02 10:42:58 +0800299 win->res.flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100300 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
301 return false;
302
303 ext_addr = &ares->data.ext_address64;
304
Jiang Liua49170b2015-02-02 10:42:58 +0800305 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800306 &ext_addr->address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100307}
308EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
309
310/**
311 * acpi_dev_irq_flags - Determine IRQ resource flags.
312 * @triggering: Triggering type as provided by ACPI.
313 * @polarity: Interrupt polarity as provided by ACPI.
314 * @shareable: Whether or not the interrupt is shareable.
315 */
316unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
317{
318 unsigned long flags;
319
320 if (triggering == ACPI_LEVEL_SENSITIVE)
321 flags = polarity == ACPI_ACTIVE_LOW ?
322 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
323 else
324 flags = polarity == ACPI_ACTIVE_LOW ?
325 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
326
327 if (shareable == ACPI_SHARED)
328 flags |= IORESOURCE_IRQ_SHAREABLE;
329
330 return flags | IORESOURCE_IRQ;
331}
332EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
333
334static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
335{
336 res->start = gsi;
337 res->end = gsi;
Jiang Liuc78b6882015-02-02 10:42:56 +0800338 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100339}
340
341static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000342 u8 triggering, u8 polarity, u8 shareable,
343 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100344{
345 int irq, p, t;
346
347 if (!valid_IRQ(gsi)) {
348 acpi_dev_irqresource_disabled(res, gsi);
349 return;
350 }
351
352 /*
353 * In IO-APIC mode, use overrided attribute. Two reasons:
354 * 1. BIOS bug in DSDT
355 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000356 *
357 * We do this only if we are dealing with IRQ() or IRQNoFlags()
358 * resource (the legacy ISA resources). With modern ACPI 5 devices
359 * using extended IRQ descriptors we take the IRQ configuration
360 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100361 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000362 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100363 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
364 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
365
366 if (triggering != trig || polarity != pol) {
367 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000368 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100369 triggering = trig;
370 polarity = pol;
371 }
372 }
373
374 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
375 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
376 if (irq >= 0) {
377 res->start = irq;
378 res->end = irq;
379 } else {
380 acpi_dev_irqresource_disabled(res, gsi);
381 }
382}
383
384/**
385 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
386 * @ares: Input ACPI resource object.
387 * @index: Index into the array of GSIs represented by the resource.
388 * @res: Output generic resource object.
389 *
390 * Check if the given ACPI resource object represents an interrupt resource
391 * and @index does not exceed the resource's interrupt count (true is returned
392 * in that case regardless of the results of the other checks)). If that's the
393 * case, register the GSI corresponding to @index from the array of interrupts
394 * represented by the resource and populate the generic resource object pointed
395 * to by @res accordingly. If the registration of the GSI is not successful,
396 * IORESOURCE_DISABLED will be set it that object's flags.
Jiang Liu581c19f2015-02-02 10:42:55 +0800397 *
398 * Return:
399 * 1) false with res->flags setting to zero: not the expected resource type
400 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
401 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100402 */
403bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
404 struct resource *res)
405{
406 struct acpi_resource_irq *irq;
407 struct acpi_resource_extended_irq *ext_irq;
408
409 switch (ares->type) {
410 case ACPI_RESOURCE_TYPE_IRQ:
411 /*
412 * Per spec, only one interrupt per descriptor is allowed in
413 * _CRS, but some firmware violates this, so parse them all.
414 */
415 irq = &ares->data.irq;
416 if (index >= irq->interrupt_count) {
417 acpi_dev_irqresource_disabled(res, 0);
418 return false;
419 }
420 acpi_dev_get_irqresource(res, irq->interrupts[index],
421 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000422 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100423 break;
424 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
425 ext_irq = &ares->data.extended_irq;
426 if (index >= ext_irq->interrupt_count) {
427 acpi_dev_irqresource_disabled(res, 0);
428 return false;
429 }
430 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
431 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000432 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100433 break;
434 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800435 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100436 return false;
437 }
438
439 return true;
440}
441EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100442
443/**
444 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
445 * @list: The head of the resource list to free.
446 */
447void acpi_dev_free_resource_list(struct list_head *list)
448{
Jiang Liu90e97822015-02-05 13:44:43 +0800449 resource_list_free(list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100450}
451EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
452
453struct res_proc_context {
454 struct list_head *list;
455 int (*preproc)(struct acpi_resource *, void *);
456 void *preproc_data;
457 int count;
458 int error;
459};
460
Jiang Liua49170b2015-02-02 10:42:58 +0800461static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100462 struct res_proc_context *c)
463{
Jiang Liu90e97822015-02-05 13:44:43 +0800464 struct resource_entry *rentry;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100465
Jiang Liu90e97822015-02-05 13:44:43 +0800466 rentry = resource_list_create_entry(NULL, 0);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100467 if (!rentry) {
468 c->error = -ENOMEM;
469 return AE_NO_MEMORY;
470 }
Jiang Liu90e97822015-02-05 13:44:43 +0800471 *rentry->res = win->res;
Jiang Liu93286f42015-02-02 10:43:00 +0800472 rentry->offset = win->offset;
Jiang Liu90e97822015-02-05 13:44:43 +0800473 resource_list_add_tail(rentry, c->list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100474 c->count++;
475 return AE_OK;
476}
477
478static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
479 void *context)
480{
481 struct res_proc_context *c = context;
Jiang Liua49170b2015-02-02 10:42:58 +0800482 struct resource_win win;
483 struct resource *res = &win.res;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100484 int i;
485
486 if (c->preproc) {
487 int ret;
488
489 ret = c->preproc(ares, c->preproc_data);
490 if (ret < 0) {
491 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100492 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100493 } else if (ret > 0) {
494 return AE_OK;
495 }
496 }
497
Jiang Liua49170b2015-02-02 10:42:58 +0800498 memset(&win, 0, sizeof(win));
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100499
Jiang Liua49170b2015-02-02 10:42:58 +0800500 if (acpi_dev_resource_memory(ares, res)
501 || acpi_dev_resource_io(ares, res)
502 || acpi_dev_resource_address_space(ares, &win)
503 || acpi_dev_resource_ext_address_space(ares, &win))
504 return acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100505
Jiang Liua49170b2015-02-02 10:42:58 +0800506 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100507 acpi_status status;
508
Jiang Liua49170b2015-02-02 10:42:58 +0800509 status = acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100510 if (ACPI_FAILURE(status))
511 return status;
512 }
513
514 return AE_OK;
515}
516
517/**
518 * acpi_dev_get_resources - Get current resources of a device.
519 * @adev: ACPI device node to get the resources for.
520 * @list: Head of the resultant list of resources (must be empty).
521 * @preproc: The caller's preprocessing routine.
522 * @preproc_data: Pointer passed to the caller's preprocessing routine.
523 *
524 * Evaluate the _CRS method for the given device node and process its output by
525 * (1) executing the @preproc() rountine provided by the caller, passing the
526 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
527 * returned and (2) converting all of the returned ACPI resources into struct
528 * resource objects if possible. If the return value of @preproc() in step (1)
529 * is different from 0, step (2) is not applied to the given ACPI resource and
530 * if that value is negative, the whole processing is aborted and that value is
531 * returned as the final error code.
532 *
533 * The resultant struct resource objects are put on the list pointed to by
Jiang Liu90e97822015-02-05 13:44:43 +0800534 * @list, that must be empty initially, as members of struct resource_entry
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100535 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
536 * free that list.
537 *
538 * The number of resources in the output list is returned on success, an error
539 * code reflecting the error condition is returned otherwise.
540 */
541int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
542 int (*preproc)(struct acpi_resource *, void *),
543 void *preproc_data)
544{
545 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100546 acpi_status status;
547
548 if (!adev || !adev->handle || !list_empty(list))
549 return -EINVAL;
550
Jiang Liu952c63e2013-06-29 00:24:38 +0800551 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100552 return 0;
553
554 c.list = list;
555 c.preproc = preproc;
556 c.preproc_data = preproc_data;
557 c.count = 0;
558 c.error = 0;
559 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
560 acpi_dev_process_resource, &c);
561 if (ACPI_FAILURE(status)) {
562 acpi_dev_free_resource_list(list);
563 return c.error ? c.error : -EIO;
564 }
565
566 return c.count;
567}
568EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
Jiang Liu62d11412015-02-02 10:43:01 +0800569
570/**
571 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
572 * types
573 * @ares: Input ACPI resource object.
574 * @types: Valid resource types of IORESOURCE_XXX
575 *
576 * This is a hepler function to support acpi_dev_get_resources(), which filters
577 * ACPI resource objects according to resource types.
578 */
579int acpi_dev_filter_resource_type(struct acpi_resource *ares,
580 unsigned long types)
581{
582 unsigned long type = 0;
583
584 switch (ares->type) {
585 case ACPI_RESOURCE_TYPE_MEMORY24:
586 case ACPI_RESOURCE_TYPE_MEMORY32:
587 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
588 type = IORESOURCE_MEM;
589 break;
590 case ACPI_RESOURCE_TYPE_IO:
591 case ACPI_RESOURCE_TYPE_FIXED_IO:
592 type = IORESOURCE_IO;
593 break;
594 case ACPI_RESOURCE_TYPE_IRQ:
595 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
596 type = IORESOURCE_IRQ;
597 break;
598 case ACPI_RESOURCE_TYPE_DMA:
599 case ACPI_RESOURCE_TYPE_FIXED_DMA:
600 type = IORESOURCE_DMA;
601 break;
602 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
603 type = IORESOURCE_REG;
604 break;
605 case ACPI_RESOURCE_TYPE_ADDRESS16:
606 case ACPI_RESOURCE_TYPE_ADDRESS32:
607 case ACPI_RESOURCE_TYPE_ADDRESS64:
608 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
609 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
610 type = IORESOURCE_MEM;
611 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
612 type = IORESOURCE_IO;
613 else if (ares->data.address.resource_type ==
614 ACPI_BUS_NUMBER_RANGE)
615 type = IORESOURCE_BUS;
616 break;
617 default:
618 break;
619 }
620
621 return (type & types) ? 0 : 1;
622}
623EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);