blob: cdc5c2599bebcc1a95ca2f29a525a5e63fab3b0f [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 *
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010018 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 */
20
21#include <linux/acpi.h>
22#include <linux/device.h>
23#include <linux/export.h>
24#include <linux/ioport.h>
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010025#include <linux/slab.h>
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010026
27#ifdef CONFIG_X86
28#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
29#else
30#define valid_IRQ(i) (true)
31#endif
32
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080033static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010034{
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080035 u64 reslen = end - start + 1;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010036
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080037 /*
38 * CHECKME: len might be required to check versus a minimum
39 * length as well. 1 for io is fine, but for memory it does
40 * not make any sense at all.
Jiang Liuaa714d22015-03-04 16:47:12 +080041 * Note: some BIOSes report incorrect length for ACPI address space
42 * descriptor, so remove check of 'reslen == len' to avoid regression.
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080043 */
Jiang Liuaa714d22015-03-04 16:47:12 +080044 if (len && reslen && start <= end)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080045 return true;
46
Rafael J. Wysocki6a239af2015-02-18 08:05:43 +010047 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080048 io ? "io" : "mem", start, end, len);
49
50 return false;
51}
52
53static void acpi_dev_memresource_flags(struct resource *res, u64 len,
Thomas Gleixner72e26b02015-02-02 10:42:52 +080054 u8 write_protect)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080055{
56 res->flags = IORESOURCE_MEM;
57
58 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
Jiang Liuc78b6882015-02-02 10:42:56 +080059 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010060
61 if (write_protect == ACPI_READ_WRITE_MEMORY)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080062 res->flags |= IORESOURCE_MEM_WRITEABLE;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010063}
64
65static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
66 u8 write_protect)
67{
68 res->start = start;
69 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +080070 acpi_dev_memresource_flags(res, len, write_protect);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010071}
72
73/**
74 * acpi_dev_resource_memory - Extract ACPI memory resource information.
75 * @ares: Input ACPI resource object.
76 * @res: Output generic resource object.
77 *
78 * Check if the given ACPI resource object represents a memory resource and
79 * if that's the case, use the information in it to populate the generic
80 * resource object pointed to by @res.
Jiang Liu581c19f2015-02-02 10:42:55 +080081 *
82 * Return:
83 * 1) false with res->flags setting to zero: not the expected resource type
84 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
85 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010086 */
87bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
88{
89 struct acpi_resource_memory24 *memory24;
90 struct acpi_resource_memory32 *memory32;
91 struct acpi_resource_fixed_memory32 *fixed_memory32;
92
93 switch (ares->type) {
94 case ACPI_RESOURCE_TYPE_MEMORY24:
95 memory24 = &ares->data.memory24;
Jiang Liu8515f932015-02-02 10:42:54 +080096 acpi_dev_get_memresource(res, memory24->minimum << 8,
97 memory24->address_length << 8,
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010098 memory24->write_protect);
99 break;
100 case ACPI_RESOURCE_TYPE_MEMORY32:
101 memory32 = &ares->data.memory32;
102 acpi_dev_get_memresource(res, memory32->minimum,
103 memory32->address_length,
104 memory32->write_protect);
105 break;
106 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
107 fixed_memory32 = &ares->data.fixed_memory32;
108 acpi_dev_get_memresource(res, fixed_memory32->address,
109 fixed_memory32->address_length,
110 fixed_memory32->write_protect);
111 break;
112 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800113 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100114 return false;
115 }
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800116
117 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100118}
119EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
120
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800121static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
Jiang Liu91236ec2015-10-14 14:29:36 +0800122 u8 io_decode, u8 translation_type)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100123{
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800124 res->flags = IORESOURCE_IO;
125
126 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
Jiang Liuc78b6882015-02-02 10:42:56 +0800127 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800128
129 if (res->end >= 0x10003)
Jiang Liuc78b6882015-02-02 10:42:56 +0800130 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100131
132 if (io_decode == ACPI_DECODE_16)
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800133 res->flags |= IORESOURCE_IO_16BIT_ADDR;
Jiang Liu91236ec2015-10-14 14:29:36 +0800134 if (translation_type == ACPI_SPARSE_TRANSLATION)
135 res->flags |= IORESOURCE_IO_SPARSE;
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;
Jiang Liu91236ec2015-10-14 14:29:36 +0800143 acpi_dev_ioresource_flags(res, len, io_decode, 0);
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 Liu1fb01ca2015-07-08 15:26:39 +0800194 u64 start, end, offset = 0;
Jiang Liua49170b2015-02-02 10:42:58 +0800195 struct resource *res = &win->res;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800196
Jiang Liua2740192015-02-02 10:42:57 +0800197 /*
198 * Filter out invalid descriptor according to ACPI Spec 5.0, section
199 * 6.4.3.5 Address Space Resource Descriptors.
200 */
201 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
202 (addr->min_address_fixed && addr->max_address_fixed && !len))
203 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
204 addr->min_address_fixed, addr->max_address_fixed, len);
205
Jiang Liu2ea3d262015-02-02 10:42:59 +0800206 /*
207 * For bridges that translate addresses across the bridge,
208 * translation_offset is the offset that must be added to the
209 * address on the secondary side to obtain the address on the
210 * primary side. Non-bridge devices must list 0 for all Address
211 * Translation offset bits.
212 */
Jiang Liu1fb01ca2015-07-08 15:26:39 +0800213 if (addr->producer_consumer == ACPI_PRODUCER)
214 offset = attr->translation_offset;
215 else if (attr->translation_offset)
Jiang Liu2ea3d262015-02-02 10:42:59 +0800216 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
217 attr->translation_offset);
Jiang Liu1fb01ca2015-07-08 15:26:39 +0800218 start = attr->minimum + offset;
219 end = attr->maximum + offset;
220
221 win->offset = offset;
222 res->start = start;
223 res->end = end;
224 if (sizeof(resource_size_t) < sizeof(u64) &&
225 (offset != win->offset || start != res->start || end != res->end)) {
226 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
227 attr->minimum, attr->maximum);
228 return false;
Jiang Liu2ea3d262015-02-02 10:42:59 +0800229 }
230
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800231 switch (addr->resource_type) {
232 case ACPI_MEMORY_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800233 acpi_dev_memresource_flags(res, len, wp);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800234 break;
235 case ACPI_IO_RANGE:
Jiang Liu91236ec2015-10-14 14:29:36 +0800236 acpi_dev_ioresource_flags(res, len, iodec,
237 addr->info.io.translation_type);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800238 break;
239 case ACPI_BUS_NUMBER_RANGE:
240 res->flags = IORESOURCE_BUS;
241 break;
242 default:
243 return false;
244 }
245
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800246 if (addr->producer_consumer == ACPI_PRODUCER)
247 res->flags |= IORESOURCE_WINDOW;
248
Thomas Gleixnerfcb29bb2015-02-02 10:42:53 +0800249 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
250 res->flags |= IORESOURCE_PREFETCH;
251
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800252 return !(res->flags & IORESOURCE_DISABLED);
253}
254
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100255/**
256 * acpi_dev_resource_address_space - Extract ACPI address space information.
257 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800258 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100259 *
260 * Check if the given ACPI resource object represents an address space resource
261 * and if that's the case, use the information in it to populate the generic
Jiang Liua49170b2015-02-02 10:42:58 +0800262 * resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800263 *
264 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800265 * 1) false with win->res.flags setting to zero: not the expected resource type
266 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
267 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800268 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100269 */
270bool acpi_dev_resource_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800271 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100272{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100273 struct acpi_resource_address64 addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100274
Jiang Liua49170b2015-02-02 10:42:58 +0800275 win->res.flags = 0;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800276 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
Jiang Liu6658c732014-10-27 13:21:35 +0800277 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100278
Jiang Liua49170b2015-02-02 10:42:58 +0800279 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800280 &addr.address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100281}
282EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
283
284/**
285 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
286 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800287 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100288 *
289 * Check if the given ACPI resource object represents an extended address space
290 * resource and if that's the case, use the information in it to populate the
Jiang Liua49170b2015-02-02 10:42:58 +0800291 * generic resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800292 *
293 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800294 * 1) false with win->res.flags setting to zero: not the expected resource type
295 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
296 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800297 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100298 */
299bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800300 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100301{
302 struct acpi_resource_extended_address64 *ext_addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100303
Jiang Liua49170b2015-02-02 10:42:58 +0800304 win->res.flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100305 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
306 return false;
307
308 ext_addr = &ares->data.ext_address64;
309
Jiang Liua49170b2015-02-02 10:42:58 +0800310 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800311 &ext_addr->address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100312}
313EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
314
315/**
316 * acpi_dev_irq_flags - Determine IRQ resource flags.
317 * @triggering: Triggering type as provided by ACPI.
318 * @polarity: Interrupt polarity as provided by ACPI.
319 * @shareable: Whether or not the interrupt is shareable.
320 */
321unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
322{
323 unsigned long flags;
324
325 if (triggering == ACPI_LEVEL_SENSITIVE)
326 flags = polarity == ACPI_ACTIVE_LOW ?
327 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
328 else
329 flags = polarity == ACPI_ACTIVE_LOW ?
330 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
331
332 if (shareable == ACPI_SHARED)
333 flags |= IORESOURCE_IRQ_SHAREABLE;
334
335 return flags | IORESOURCE_IRQ;
336}
337EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
338
339static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
340{
341 res->start = gsi;
342 res->end = gsi;
Jiang Liuc78b6882015-02-02 10:42:56 +0800343 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100344}
345
346static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000347 u8 triggering, u8 polarity, u8 shareable,
348 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100349{
350 int irq, p, t;
351
352 if (!valid_IRQ(gsi)) {
353 acpi_dev_irqresource_disabled(res, gsi);
354 return;
355 }
356
357 /*
358 * In IO-APIC mode, use overrided attribute. Two reasons:
359 * 1. BIOS bug in DSDT
360 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000361 *
362 * We do this only if we are dealing with IRQ() or IRQNoFlags()
363 * resource (the legacy ISA resources). With modern ACPI 5 devices
364 * using extended IRQ descriptors we take the IRQ configuration
365 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100366 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000367 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100368 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
369 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
370
371 if (triggering != trig || polarity != pol) {
372 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000373 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100374 triggering = trig;
375 polarity = pol;
376 }
377 }
378
379 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
380 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
381 if (irq >= 0) {
382 res->start = irq;
383 res->end = irq;
384 } else {
385 acpi_dev_irqresource_disabled(res, gsi);
386 }
387}
388
389/**
390 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
391 * @ares: Input ACPI resource object.
392 * @index: Index into the array of GSIs represented by the resource.
393 * @res: Output generic resource object.
394 *
395 * Check if the given ACPI resource object represents an interrupt resource
396 * and @index does not exceed the resource's interrupt count (true is returned
397 * in that case regardless of the results of the other checks)). If that's the
398 * case, register the GSI corresponding to @index from the array of interrupts
399 * represented by the resource and populate the generic resource object pointed
400 * to by @res accordingly. If the registration of the GSI is not successful,
401 * IORESOURCE_DISABLED will be set it that object's flags.
Jiang Liu581c19f2015-02-02 10:42:55 +0800402 *
403 * Return:
404 * 1) false with res->flags setting to zero: not the expected resource type
405 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
406 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100407 */
408bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
409 struct resource *res)
410{
411 struct acpi_resource_irq *irq;
412 struct acpi_resource_extended_irq *ext_irq;
413
414 switch (ares->type) {
415 case ACPI_RESOURCE_TYPE_IRQ:
416 /*
417 * Per spec, only one interrupt per descriptor is allowed in
418 * _CRS, but some firmware violates this, so parse them all.
419 */
420 irq = &ares->data.irq;
421 if (index >= irq->interrupt_count) {
422 acpi_dev_irqresource_disabled(res, 0);
423 return false;
424 }
425 acpi_dev_get_irqresource(res, irq->interrupts[index],
426 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000427 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100428 break;
429 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
430 ext_irq = &ares->data.extended_irq;
431 if (index >= ext_irq->interrupt_count) {
432 acpi_dev_irqresource_disabled(res, 0);
433 return false;
434 }
435 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
436 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000437 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100438 break;
439 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800440 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100441 return false;
442 }
443
444 return true;
445}
446EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100447
448/**
449 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
450 * @list: The head of the resource list to free.
451 */
452void acpi_dev_free_resource_list(struct list_head *list)
453{
Jiang Liu90e97822015-02-05 13:44:43 +0800454 resource_list_free(list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100455}
456EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
457
458struct res_proc_context {
459 struct list_head *list;
460 int (*preproc)(struct acpi_resource *, void *);
461 void *preproc_data;
462 int count;
463 int error;
464};
465
Jiang Liua49170b2015-02-02 10:42:58 +0800466static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100467 struct res_proc_context *c)
468{
Jiang Liu90e97822015-02-05 13:44:43 +0800469 struct resource_entry *rentry;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100470
Jiang Liu90e97822015-02-05 13:44:43 +0800471 rentry = resource_list_create_entry(NULL, 0);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100472 if (!rentry) {
473 c->error = -ENOMEM;
474 return AE_NO_MEMORY;
475 }
Jiang Liu90e97822015-02-05 13:44:43 +0800476 *rentry->res = win->res;
Jiang Liu93286f42015-02-02 10:43:00 +0800477 rentry->offset = win->offset;
Jiang Liu90e97822015-02-05 13:44:43 +0800478 resource_list_add_tail(rentry, c->list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100479 c->count++;
480 return AE_OK;
481}
482
483static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
484 void *context)
485{
486 struct res_proc_context *c = context;
Jiang Liua49170b2015-02-02 10:42:58 +0800487 struct resource_win win;
488 struct resource *res = &win.res;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100489 int i;
490
491 if (c->preproc) {
492 int ret;
493
494 ret = c->preproc(ares, c->preproc_data);
495 if (ret < 0) {
496 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100497 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100498 } else if (ret > 0) {
499 return AE_OK;
500 }
501 }
502
Jiang Liua49170b2015-02-02 10:42:58 +0800503 memset(&win, 0, sizeof(win));
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100504
Jiang Liua49170b2015-02-02 10:42:58 +0800505 if (acpi_dev_resource_memory(ares, res)
506 || acpi_dev_resource_io(ares, res)
507 || acpi_dev_resource_address_space(ares, &win)
508 || acpi_dev_resource_ext_address_space(ares, &win))
509 return acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100510
Jiang Liua49170b2015-02-02 10:42:58 +0800511 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100512 acpi_status status;
513
Jiang Liua49170b2015-02-02 10:42:58 +0800514 status = acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100515 if (ACPI_FAILURE(status))
516 return status;
517 }
518
519 return AE_OK;
520}
521
522/**
523 * acpi_dev_get_resources - Get current resources of a device.
524 * @adev: ACPI device node to get the resources for.
525 * @list: Head of the resultant list of resources (must be empty).
526 * @preproc: The caller's preprocessing routine.
527 * @preproc_data: Pointer passed to the caller's preprocessing routine.
528 *
529 * Evaluate the _CRS method for the given device node and process its output by
530 * (1) executing the @preproc() rountine provided by the caller, passing the
531 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
532 * returned and (2) converting all of the returned ACPI resources into struct
533 * resource objects if possible. If the return value of @preproc() in step (1)
534 * is different from 0, step (2) is not applied to the given ACPI resource and
535 * if that value is negative, the whole processing is aborted and that value is
536 * returned as the final error code.
537 *
538 * The resultant struct resource objects are put on the list pointed to by
Jiang Liu90e97822015-02-05 13:44:43 +0800539 * @list, that must be empty initially, as members of struct resource_entry
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100540 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
541 * free that list.
542 *
543 * The number of resources in the output list is returned on success, an error
544 * code reflecting the error condition is returned otherwise.
545 */
546int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
547 int (*preproc)(struct acpi_resource *, void *),
548 void *preproc_data)
549{
550 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100551 acpi_status status;
552
553 if (!adev || !adev->handle || !list_empty(list))
554 return -EINVAL;
555
Jiang Liu952c63e2013-06-29 00:24:38 +0800556 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100557 return 0;
558
559 c.list = list;
560 c.preproc = preproc;
561 c.preproc_data = preproc_data;
562 c.count = 0;
563 c.error = 0;
564 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
565 acpi_dev_process_resource, &c);
566 if (ACPI_FAILURE(status)) {
567 acpi_dev_free_resource_list(list);
568 return c.error ? c.error : -EIO;
569 }
570
571 return c.count;
572}
573EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
Jiang Liu62d11412015-02-02 10:43:01 +0800574
575/**
576 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
577 * types
578 * @ares: Input ACPI resource object.
579 * @types: Valid resource types of IORESOURCE_XXX
580 *
Jiang Liu2c62e842015-04-30 12:41:28 +0800581 * This is a helper function to support acpi_dev_get_resources(), which filters
Jiang Liu62d11412015-02-02 10:43:01 +0800582 * ACPI resource objects according to resource types.
583 */
584int acpi_dev_filter_resource_type(struct acpi_resource *ares,
585 unsigned long types)
586{
587 unsigned long type = 0;
588
589 switch (ares->type) {
590 case ACPI_RESOURCE_TYPE_MEMORY24:
591 case ACPI_RESOURCE_TYPE_MEMORY32:
592 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
593 type = IORESOURCE_MEM;
594 break;
595 case ACPI_RESOURCE_TYPE_IO:
596 case ACPI_RESOURCE_TYPE_FIXED_IO:
597 type = IORESOURCE_IO;
598 break;
599 case ACPI_RESOURCE_TYPE_IRQ:
600 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
601 type = IORESOURCE_IRQ;
602 break;
603 case ACPI_RESOURCE_TYPE_DMA:
604 case ACPI_RESOURCE_TYPE_FIXED_DMA:
605 type = IORESOURCE_DMA;
606 break;
607 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
608 type = IORESOURCE_REG;
609 break;
610 case ACPI_RESOURCE_TYPE_ADDRESS16:
611 case ACPI_RESOURCE_TYPE_ADDRESS32:
612 case ACPI_RESOURCE_TYPE_ADDRESS64:
613 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
614 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
615 type = IORESOURCE_MEM;
616 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
617 type = IORESOURCE_IO;
618 else if (ares->data.address.resource_type ==
619 ACPI_BUS_NUMBER_RANGE)
620 type = IORESOURCE_BUS;
621 break;
622 default:
623 break;
624 }
625
626 return (type & types) ? 0 : 1;
627}
628EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);