blob: f1c966e05078426455227d8f582a7ddd0f211cb2 [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 Liu1fb01ca2015-07-08 15:26:39 +0800196 u64 start, end, offset = 0;
Jiang Liua49170b2015-02-02 10:42:58 +0800197 struct resource *res = &win->res;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800198
Jiang Liua2740192015-02-02 10:42:57 +0800199 /*
200 * Filter out invalid descriptor according to ACPI Spec 5.0, section
201 * 6.4.3.5 Address Space Resource Descriptors.
202 */
203 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
204 (addr->min_address_fixed && addr->max_address_fixed && !len))
205 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
206 addr->min_address_fixed, addr->max_address_fixed, len);
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 */
Jiang Liu1fb01ca2015-07-08 15:26:39 +0800215 if (addr->producer_consumer == ACPI_PRODUCER)
216 offset = attr->translation_offset;
217 else if (attr->translation_offset)
Jiang Liu2ea3d262015-02-02 10:42:59 +0800218 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
219 attr->translation_offset);
Jiang Liu1fb01ca2015-07-08 15:26:39 +0800220 start = attr->minimum + offset;
221 end = attr->maximum + offset;
222
223 win->offset = offset;
224 res->start = start;
225 res->end = end;
226 if (sizeof(resource_size_t) < sizeof(u64) &&
227 (offset != win->offset || start != res->start || end != res->end)) {
228 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
229 attr->minimum, attr->maximum);
230 return false;
Jiang Liu2ea3d262015-02-02 10:42:59 +0800231 }
232
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800233 switch (addr->resource_type) {
234 case ACPI_MEMORY_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800235 acpi_dev_memresource_flags(res, len, wp);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800236 break;
237 case ACPI_IO_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800238 acpi_dev_ioresource_flags(res, len, iodec);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800239 break;
240 case ACPI_BUS_NUMBER_RANGE:
241 res->flags = IORESOURCE_BUS;
242 break;
243 default:
244 return false;
245 }
246
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800247 if (addr->producer_consumer == ACPI_PRODUCER)
248 res->flags |= IORESOURCE_WINDOW;
249
Thomas Gleixnerfcb29bb2015-02-02 10:42:53 +0800250 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
251 res->flags |= IORESOURCE_PREFETCH;
252
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800253 return !(res->flags & IORESOURCE_DISABLED);
254}
255
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100256/**
257 * acpi_dev_resource_address_space - Extract ACPI address space information.
258 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800259 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100260 *
261 * Check if the given ACPI resource object represents an address space resource
262 * and if that's the case, use the information in it to populate the generic
Jiang Liua49170b2015-02-02 10:42:58 +0800263 * resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800264 *
265 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800266 * 1) false with win->res.flags setting to zero: not the expected resource type
267 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
268 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800269 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100270 */
271bool acpi_dev_resource_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800272 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100273{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100274 struct acpi_resource_address64 addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100275
Jiang Liua49170b2015-02-02 10:42:58 +0800276 win->res.flags = 0;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800277 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
Jiang Liu6658c732014-10-27 13:21:35 +0800278 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100279
Jiang Liua49170b2015-02-02 10:42:58 +0800280 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800281 &addr.address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100282}
283EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
284
285/**
286 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
287 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800288 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100289 *
290 * Check if the given ACPI resource object represents an extended address space
291 * resource and if that's the case, use the information in it to populate the
Jiang Liua49170b2015-02-02 10:42:58 +0800292 * generic resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800293 *
294 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800295 * 1) false with win->res.flags setting to zero: not the expected resource type
296 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
297 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800298 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100299 */
300bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800301 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100302{
303 struct acpi_resource_extended_address64 *ext_addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100304
Jiang Liua49170b2015-02-02 10:42:58 +0800305 win->res.flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100306 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
307 return false;
308
309 ext_addr = &ares->data.ext_address64;
310
Jiang Liua49170b2015-02-02 10:42:58 +0800311 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800312 &ext_addr->address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100313}
314EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
315
316/**
317 * acpi_dev_irq_flags - Determine IRQ resource flags.
318 * @triggering: Triggering type as provided by ACPI.
319 * @polarity: Interrupt polarity as provided by ACPI.
320 * @shareable: Whether or not the interrupt is shareable.
321 */
322unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
323{
324 unsigned long flags;
325
326 if (triggering == ACPI_LEVEL_SENSITIVE)
327 flags = polarity == ACPI_ACTIVE_LOW ?
328 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
329 else
330 flags = polarity == ACPI_ACTIVE_LOW ?
331 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
332
333 if (shareable == ACPI_SHARED)
334 flags |= IORESOURCE_IRQ_SHAREABLE;
335
336 return flags | IORESOURCE_IRQ;
337}
338EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
339
340static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
341{
342 res->start = gsi;
343 res->end = gsi;
Jiang Liuc78b6882015-02-02 10:42:56 +0800344 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100345}
346
347static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000348 u8 triggering, u8 polarity, u8 shareable,
349 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100350{
351 int irq, p, t;
352
353 if (!valid_IRQ(gsi)) {
354 acpi_dev_irqresource_disabled(res, gsi);
355 return;
356 }
357
358 /*
359 * In IO-APIC mode, use overrided attribute. Two reasons:
360 * 1. BIOS bug in DSDT
361 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000362 *
363 * We do this only if we are dealing with IRQ() or IRQNoFlags()
364 * resource (the legacy ISA resources). With modern ACPI 5 devices
365 * using extended IRQ descriptors we take the IRQ configuration
366 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100367 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000368 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100369 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
370 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
371
372 if (triggering != trig || polarity != pol) {
373 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000374 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100375 triggering = trig;
376 polarity = pol;
377 }
378 }
379
380 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
381 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
382 if (irq >= 0) {
383 res->start = irq;
384 res->end = irq;
385 } else {
386 acpi_dev_irqresource_disabled(res, gsi);
387 }
388}
389
390/**
391 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
392 * @ares: Input ACPI resource object.
393 * @index: Index into the array of GSIs represented by the resource.
394 * @res: Output generic resource object.
395 *
396 * Check if the given ACPI resource object represents an interrupt resource
397 * and @index does not exceed the resource's interrupt count (true is returned
398 * in that case regardless of the results of the other checks)). If that's the
399 * case, register the GSI corresponding to @index from the array of interrupts
400 * represented by the resource and populate the generic resource object pointed
401 * to by @res accordingly. If the registration of the GSI is not successful,
402 * IORESOURCE_DISABLED will be set it that object's flags.
Jiang Liu581c19f2015-02-02 10:42:55 +0800403 *
404 * Return:
405 * 1) false with res->flags setting to zero: not the expected resource type
406 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
407 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100408 */
409bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
410 struct resource *res)
411{
412 struct acpi_resource_irq *irq;
413 struct acpi_resource_extended_irq *ext_irq;
414
415 switch (ares->type) {
416 case ACPI_RESOURCE_TYPE_IRQ:
417 /*
418 * Per spec, only one interrupt per descriptor is allowed in
419 * _CRS, but some firmware violates this, so parse them all.
420 */
421 irq = &ares->data.irq;
422 if (index >= irq->interrupt_count) {
423 acpi_dev_irqresource_disabled(res, 0);
424 return false;
425 }
426 acpi_dev_get_irqresource(res, irq->interrupts[index],
427 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000428 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100429 break;
430 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
431 ext_irq = &ares->data.extended_irq;
432 if (index >= ext_irq->interrupt_count) {
433 acpi_dev_irqresource_disabled(res, 0);
434 return false;
435 }
436 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
437 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000438 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100439 break;
440 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800441 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100442 return false;
443 }
444
445 return true;
446}
447EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100448
449/**
450 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
451 * @list: The head of the resource list to free.
452 */
453void acpi_dev_free_resource_list(struct list_head *list)
454{
Jiang Liu90e97822015-02-05 13:44:43 +0800455 resource_list_free(list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100456}
457EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
458
459struct res_proc_context {
460 struct list_head *list;
461 int (*preproc)(struct acpi_resource *, void *);
462 void *preproc_data;
463 int count;
464 int error;
465};
466
Jiang Liua49170b2015-02-02 10:42:58 +0800467static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100468 struct res_proc_context *c)
469{
Jiang Liu90e97822015-02-05 13:44:43 +0800470 struct resource_entry *rentry;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100471
Jiang Liu90e97822015-02-05 13:44:43 +0800472 rentry = resource_list_create_entry(NULL, 0);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100473 if (!rentry) {
474 c->error = -ENOMEM;
475 return AE_NO_MEMORY;
476 }
Jiang Liu90e97822015-02-05 13:44:43 +0800477 *rentry->res = win->res;
Jiang Liu93286f42015-02-02 10:43:00 +0800478 rentry->offset = win->offset;
Jiang Liu90e97822015-02-05 13:44:43 +0800479 resource_list_add_tail(rentry, c->list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100480 c->count++;
481 return AE_OK;
482}
483
484static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
485 void *context)
486{
487 struct res_proc_context *c = context;
Jiang Liua49170b2015-02-02 10:42:58 +0800488 struct resource_win win;
489 struct resource *res = &win.res;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100490 int i;
491
492 if (c->preproc) {
493 int ret;
494
495 ret = c->preproc(ares, c->preproc_data);
496 if (ret < 0) {
497 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100498 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100499 } else if (ret > 0) {
500 return AE_OK;
501 }
502 }
503
Jiang Liua49170b2015-02-02 10:42:58 +0800504 memset(&win, 0, sizeof(win));
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100505
Jiang Liua49170b2015-02-02 10:42:58 +0800506 if (acpi_dev_resource_memory(ares, res)
507 || acpi_dev_resource_io(ares, res)
508 || acpi_dev_resource_address_space(ares, &win)
509 || acpi_dev_resource_ext_address_space(ares, &win))
510 return acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100511
Jiang Liua49170b2015-02-02 10:42:58 +0800512 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100513 acpi_status status;
514
Jiang Liua49170b2015-02-02 10:42:58 +0800515 status = acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100516 if (ACPI_FAILURE(status))
517 return status;
518 }
519
520 return AE_OK;
521}
522
523/**
524 * acpi_dev_get_resources - Get current resources of a device.
525 * @adev: ACPI device node to get the resources for.
526 * @list: Head of the resultant list of resources (must be empty).
527 * @preproc: The caller's preprocessing routine.
528 * @preproc_data: Pointer passed to the caller's preprocessing routine.
529 *
530 * Evaluate the _CRS method for the given device node and process its output by
531 * (1) executing the @preproc() rountine provided by the caller, passing the
532 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
533 * returned and (2) converting all of the returned ACPI resources into struct
534 * resource objects if possible. If the return value of @preproc() in step (1)
535 * is different from 0, step (2) is not applied to the given ACPI resource and
536 * if that value is negative, the whole processing is aborted and that value is
537 * returned as the final error code.
538 *
539 * The resultant struct resource objects are put on the list pointed to by
Jiang Liu90e97822015-02-05 13:44:43 +0800540 * @list, that must be empty initially, as members of struct resource_entry
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100541 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
542 * free that list.
543 *
544 * The number of resources in the output list is returned on success, an error
545 * code reflecting the error condition is returned otherwise.
546 */
547int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
548 int (*preproc)(struct acpi_resource *, void *),
549 void *preproc_data)
550{
551 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100552 acpi_status status;
553
554 if (!adev || !adev->handle || !list_empty(list))
555 return -EINVAL;
556
Jiang Liu952c63e2013-06-29 00:24:38 +0800557 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100558 return 0;
559
560 c.list = list;
561 c.preproc = preproc;
562 c.preproc_data = preproc_data;
563 c.count = 0;
564 c.error = 0;
565 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
566 acpi_dev_process_resource, &c);
567 if (ACPI_FAILURE(status)) {
568 acpi_dev_free_resource_list(list);
569 return c.error ? c.error : -EIO;
570 }
571
572 return c.count;
573}
574EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
Jiang Liu62d11412015-02-02 10:43:01 +0800575
576/**
577 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
578 * types
579 * @ares: Input ACPI resource object.
580 * @types: Valid resource types of IORESOURCE_XXX
581 *
Jiang Liu2c62e842015-04-30 12:41:28 +0800582 * This is a helper function to support acpi_dev_get_resources(), which filters
Jiang Liu62d11412015-02-02 10:43:01 +0800583 * ACPI resource objects according to resource types.
584 */
585int acpi_dev_filter_resource_type(struct acpi_resource *ares,
586 unsigned long types)
587{
588 unsigned long type = 0;
589
590 switch (ares->type) {
591 case ACPI_RESOURCE_TYPE_MEMORY24:
592 case ACPI_RESOURCE_TYPE_MEMORY32:
593 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
594 type = IORESOURCE_MEM;
595 break;
596 case ACPI_RESOURCE_TYPE_IO:
597 case ACPI_RESOURCE_TYPE_FIXED_IO:
598 type = IORESOURCE_IO;
599 break;
600 case ACPI_RESOURCE_TYPE_IRQ:
601 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
602 type = IORESOURCE_IRQ;
603 break;
604 case ACPI_RESOURCE_TYPE_DMA:
605 case ACPI_RESOURCE_TYPE_FIXED_DMA:
606 type = IORESOURCE_DMA;
607 break;
608 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
609 type = IORESOURCE_REG;
610 break;
611 case ACPI_RESOURCE_TYPE_ADDRESS16:
612 case ACPI_RESOURCE_TYPE_ADDRESS32:
613 case ACPI_RESOURCE_TYPE_ADDRESS64:
614 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
615 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
616 type = IORESOURCE_MEM;
617 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
618 type = IORESOURCE_IO;
619 else if (ares->data.address.resource_type ==
620 ACPI_BUS_NUMBER_RANGE)
621 type = IORESOURCE_BUS;
622 break;
623 default:
624 break;
625 }
626
627 return (type & types) ? 0 : 1;
628}
629EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);