blob: e8d281739cbc32582259ec8a34e2e5edcd38b29c [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. Wysocki0f1b4142015-06-18 18:32:02 +020029#include <linux/list.h>
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010030#include <linux/slab.h>
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010031
32#ifdef CONFIG_X86
33#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
34#else
35#define valid_IRQ(i) (true)
36#endif
37
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080038static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010039{
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080040 u64 reslen = end - start + 1;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010041
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080042 /*
43 * CHECKME: len might be required to check versus a minimum
44 * length as well. 1 for io is fine, but for memory it does
45 * not make any sense at all.
Jiang Liuaa714d22015-03-04 16:47:12 +080046 * Note: some BIOSes report incorrect length for ACPI address space
47 * descriptor, so remove check of 'reslen == len' to avoid regression.
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080048 */
Jiang Liuaa714d22015-03-04 16:47:12 +080049 if (len && reslen && start <= end)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080050 return true;
51
Rafael J. Wysocki6a239af2015-02-18 08:05:43 +010052 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080053 io ? "io" : "mem", start, end, len);
54
55 return false;
56}
57
58static void acpi_dev_memresource_flags(struct resource *res, u64 len,
Thomas Gleixner72e26b02015-02-02 10:42:52 +080059 u8 write_protect)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080060{
61 res->flags = IORESOURCE_MEM;
62
63 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
Jiang Liuc78b6882015-02-02 10:42:56 +080064 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010065
66 if (write_protect == ACPI_READ_WRITE_MEMORY)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080067 res->flags |= IORESOURCE_MEM_WRITEABLE;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010068}
69
70static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
71 u8 write_protect)
72{
73 res->start = start;
74 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +080075 acpi_dev_memresource_flags(res, len, write_protect);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010076}
77
78/**
79 * acpi_dev_resource_memory - Extract ACPI memory resource information.
80 * @ares: Input ACPI resource object.
81 * @res: Output generic resource object.
82 *
83 * Check if the given ACPI resource object represents a memory resource and
84 * if that's the case, use the information in it to populate the generic
85 * resource object pointed to by @res.
Jiang Liu581c19f2015-02-02 10:42:55 +080086 *
87 * Return:
88 * 1) false with res->flags setting to zero: not the expected resource type
89 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
90 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010091 */
92bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
93{
94 struct acpi_resource_memory24 *memory24;
95 struct acpi_resource_memory32 *memory32;
96 struct acpi_resource_fixed_memory32 *fixed_memory32;
97
98 switch (ares->type) {
99 case ACPI_RESOURCE_TYPE_MEMORY24:
100 memory24 = &ares->data.memory24;
Jiang Liu8515f932015-02-02 10:42:54 +0800101 acpi_dev_get_memresource(res, memory24->minimum << 8,
102 memory24->address_length << 8,
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100103 memory24->write_protect);
104 break;
105 case ACPI_RESOURCE_TYPE_MEMORY32:
106 memory32 = &ares->data.memory32;
107 acpi_dev_get_memresource(res, memory32->minimum,
108 memory32->address_length,
109 memory32->write_protect);
110 break;
111 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
112 fixed_memory32 = &ares->data.fixed_memory32;
113 acpi_dev_get_memresource(res, fixed_memory32->address,
114 fixed_memory32->address_length,
115 fixed_memory32->write_protect);
116 break;
117 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800118 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100119 return false;
120 }
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800121
122 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100123}
124EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
125
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800126static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800127 u8 io_decode)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100128{
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800129 res->flags = IORESOURCE_IO;
130
131 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
Jiang Liuc78b6882015-02-02 10:42:56 +0800132 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800133
134 if (res->end >= 0x10003)
Jiang Liuc78b6882015-02-02 10:42:56 +0800135 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100136
137 if (io_decode == ACPI_DECODE_16)
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800138 res->flags |= IORESOURCE_IO_16BIT_ADDR;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100139}
140
141static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
142 u8 io_decode)
143{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100144 res->start = start;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800145 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800146 acpi_dev_ioresource_flags(res, len, io_decode);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100147}
148
149/**
150 * acpi_dev_resource_io - Extract ACPI I/O resource information.
151 * @ares: Input ACPI resource object.
152 * @res: Output generic resource object.
153 *
154 * Check if the given ACPI resource object represents an I/O resource and
155 * if that's the case, use the information in it to populate the generic
156 * resource object pointed to by @res.
Jiang Liu581c19f2015-02-02 10:42:55 +0800157 *
158 * Return:
159 * 1) false with res->flags setting to zero: not the expected resource type
160 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
161 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100162 */
163bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
164{
165 struct acpi_resource_io *io;
166 struct acpi_resource_fixed_io *fixed_io;
167
168 switch (ares->type) {
169 case ACPI_RESOURCE_TYPE_IO:
170 io = &ares->data.io;
171 acpi_dev_get_ioresource(res, io->minimum,
172 io->address_length,
173 io->io_decode);
174 break;
175 case ACPI_RESOURCE_TYPE_FIXED_IO:
176 fixed_io = &ares->data.fixed_io;
177 acpi_dev_get_ioresource(res, fixed_io->address,
178 fixed_io->address_length,
179 ACPI_DECODE_10);
180 break;
181 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800182 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100183 return false;
184 }
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800185
186 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100187}
188EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
189
Jiang Liua49170b2015-02-02 10:42:58 +0800190static bool acpi_decode_space(struct resource_win *win,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800191 struct acpi_resource_address *addr,
192 struct acpi_address64_attribute *attr)
193{
194 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800195 bool wp = addr->info.mem.write_protect;
196 u64 len = attr->address_length;
Jiang Liu1fb01ca2015-07-08 15:26:39 +0800197 u64 start, end, offset = 0;
Jiang Liua49170b2015-02-02 10:42:58 +0800198 struct resource *res = &win->res;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800199
Jiang Liua2740192015-02-02 10:42:57 +0800200 /*
201 * Filter out invalid descriptor according to ACPI Spec 5.0, section
202 * 6.4.3.5 Address Space Resource Descriptors.
203 */
204 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
205 (addr->min_address_fixed && addr->max_address_fixed && !len))
206 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
207 addr->min_address_fixed, addr->max_address_fixed, len);
208
Jiang Liu2ea3d262015-02-02 10:42:59 +0800209 /*
210 * For bridges that translate addresses across the bridge,
211 * translation_offset is the offset that must be added to the
212 * address on the secondary side to obtain the address on the
213 * primary side. Non-bridge devices must list 0 for all Address
214 * Translation offset bits.
215 */
Jiang Liu1fb01ca2015-07-08 15:26:39 +0800216 if (addr->producer_consumer == ACPI_PRODUCER)
217 offset = attr->translation_offset;
218 else if (attr->translation_offset)
Jiang Liu2ea3d262015-02-02 10:42:59 +0800219 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
220 attr->translation_offset);
Jiang Liu1fb01ca2015-07-08 15:26:39 +0800221 start = attr->minimum + offset;
222 end = attr->maximum + offset;
223
224 win->offset = offset;
225 res->start = start;
226 res->end = end;
227 if (sizeof(resource_size_t) < sizeof(u64) &&
228 (offset != win->offset || start != res->start || end != res->end)) {
229 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
230 attr->minimum, attr->maximum);
231 return false;
Jiang Liu2ea3d262015-02-02 10:42:59 +0800232 }
233
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800234 switch (addr->resource_type) {
235 case ACPI_MEMORY_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800236 acpi_dev_memresource_flags(res, len, wp);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800237 break;
238 case ACPI_IO_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800239 acpi_dev_ioresource_flags(res, len, iodec);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800240 break;
241 case ACPI_BUS_NUMBER_RANGE:
242 res->flags = IORESOURCE_BUS;
243 break;
244 default:
245 return false;
246 }
247
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800248 if (addr->producer_consumer == ACPI_PRODUCER)
249 res->flags |= IORESOURCE_WINDOW;
250
Thomas Gleixnerfcb29bb2015-02-02 10:42:53 +0800251 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
252 res->flags |= IORESOURCE_PREFETCH;
253
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800254 return !(res->flags & IORESOURCE_DISABLED);
255}
256
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100257/**
258 * acpi_dev_resource_address_space - Extract ACPI address space information.
259 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800260 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100261 *
262 * Check if the given ACPI resource object represents an address space resource
263 * and if that's the case, use the information in it to populate the generic
Jiang Liua49170b2015-02-02 10:42:58 +0800264 * resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800265 *
266 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800267 * 1) false with win->res.flags setting to zero: not the expected resource type
268 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
269 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800270 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100271 */
272bool acpi_dev_resource_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800273 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100274{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100275 struct acpi_resource_address64 addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100276
Jiang Liua49170b2015-02-02 10:42:58 +0800277 win->res.flags = 0;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800278 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
Jiang Liu6658c732014-10-27 13:21:35 +0800279 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100280
Jiang Liua49170b2015-02-02 10:42:58 +0800281 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800282 &addr.address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100283}
284EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
285
286/**
287 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
288 * @ares: Input ACPI resource object.
Jiang Liua49170b2015-02-02 10:42:58 +0800289 * @win: Output generic resource object.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100290 *
291 * Check if the given ACPI resource object represents an extended address space
292 * resource and if that's the case, use the information in it to populate the
Jiang Liua49170b2015-02-02 10:42:58 +0800293 * generic resource object pointed to by @win.
Jiang Liu581c19f2015-02-02 10:42:55 +0800294 *
295 * Return:
Jiang Liua49170b2015-02-02 10:42:58 +0800296 * 1) false with win->res.flags setting to zero: not the expected resource type
297 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
298 * resource
Jiang Liu581c19f2015-02-02 10:42:55 +0800299 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100300 */
301bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
Jiang Liua49170b2015-02-02 10:42:58 +0800302 struct resource_win *win)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100303{
304 struct acpi_resource_extended_address64 *ext_addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100305
Jiang Liua49170b2015-02-02 10:42:58 +0800306 win->res.flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100307 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
308 return false;
309
310 ext_addr = &ares->data.ext_address64;
311
Jiang Liua49170b2015-02-02 10:42:58 +0800312 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800313 &ext_addr->address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100314}
315EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
316
317/**
318 * acpi_dev_irq_flags - Determine IRQ resource flags.
319 * @triggering: Triggering type as provided by ACPI.
320 * @polarity: Interrupt polarity as provided by ACPI.
321 * @shareable: Whether or not the interrupt is shareable.
322 */
323unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
324{
325 unsigned long flags;
326
327 if (triggering == ACPI_LEVEL_SENSITIVE)
328 flags = polarity == ACPI_ACTIVE_LOW ?
329 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
330 else
331 flags = polarity == ACPI_ACTIVE_LOW ?
332 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
333
334 if (shareable == ACPI_SHARED)
335 flags |= IORESOURCE_IRQ_SHAREABLE;
336
337 return flags | IORESOURCE_IRQ;
338}
339EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
340
341static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
342{
343 res->start = gsi;
344 res->end = gsi;
Jiang Liuc78b6882015-02-02 10:42:56 +0800345 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100346}
347
348static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000349 u8 triggering, u8 polarity, u8 shareable,
350 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100351{
352 int irq, p, t;
353
354 if (!valid_IRQ(gsi)) {
355 acpi_dev_irqresource_disabled(res, gsi);
356 return;
357 }
358
359 /*
360 * In IO-APIC mode, use overrided attribute. Two reasons:
361 * 1. BIOS bug in DSDT
362 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000363 *
364 * We do this only if we are dealing with IRQ() or IRQNoFlags()
365 * resource (the legacy ISA resources). With modern ACPI 5 devices
366 * using extended IRQ descriptors we take the IRQ configuration
367 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100368 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000369 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100370 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
371 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
372
373 if (triggering != trig || polarity != pol) {
374 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000375 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100376 triggering = trig;
377 polarity = pol;
378 }
379 }
380
381 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
382 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
383 if (irq >= 0) {
384 res->start = irq;
385 res->end = irq;
386 } else {
387 acpi_dev_irqresource_disabled(res, gsi);
388 }
389}
390
391/**
392 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
393 * @ares: Input ACPI resource object.
394 * @index: Index into the array of GSIs represented by the resource.
395 * @res: Output generic resource object.
396 *
397 * Check if the given ACPI resource object represents an interrupt resource
398 * and @index does not exceed the resource's interrupt count (true is returned
399 * in that case regardless of the results of the other checks)). If that's the
400 * case, register the GSI corresponding to @index from the array of interrupts
401 * represented by the resource and populate the generic resource object pointed
402 * to by @res accordingly. If the registration of the GSI is not successful,
403 * IORESOURCE_DISABLED will be set it that object's flags.
Jiang Liu581c19f2015-02-02 10:42:55 +0800404 *
405 * Return:
406 * 1) false with res->flags setting to zero: not the expected resource type
407 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
408 * 3) true: valid assigned resource
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100409 */
410bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
411 struct resource *res)
412{
413 struct acpi_resource_irq *irq;
414 struct acpi_resource_extended_irq *ext_irq;
415
416 switch (ares->type) {
417 case ACPI_RESOURCE_TYPE_IRQ:
418 /*
419 * Per spec, only one interrupt per descriptor is allowed in
420 * _CRS, but some firmware violates this, so parse them all.
421 */
422 irq = &ares->data.irq;
423 if (index >= irq->interrupt_count) {
424 acpi_dev_irqresource_disabled(res, 0);
425 return false;
426 }
427 acpi_dev_get_irqresource(res, irq->interrupts[index],
428 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000429 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100430 break;
431 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
432 ext_irq = &ares->data.extended_irq;
433 if (index >= ext_irq->interrupt_count) {
434 acpi_dev_irqresource_disabled(res, 0);
435 return false;
436 }
437 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
438 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000439 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100440 break;
441 default:
Jiang Liu581c19f2015-02-02 10:42:55 +0800442 res->flags = 0;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100443 return false;
444 }
445
446 return true;
447}
448EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100449
450/**
451 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
452 * @list: The head of the resource list to free.
453 */
454void acpi_dev_free_resource_list(struct list_head *list)
455{
Jiang Liu90e97822015-02-05 13:44:43 +0800456 resource_list_free(list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100457}
458EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
459
460struct res_proc_context {
461 struct list_head *list;
462 int (*preproc)(struct acpi_resource *, void *);
463 void *preproc_data;
464 int count;
465 int error;
466};
467
Jiang Liua49170b2015-02-02 10:42:58 +0800468static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100469 struct res_proc_context *c)
470{
Jiang Liu90e97822015-02-05 13:44:43 +0800471 struct resource_entry *rentry;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100472
Jiang Liu90e97822015-02-05 13:44:43 +0800473 rentry = resource_list_create_entry(NULL, 0);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100474 if (!rentry) {
475 c->error = -ENOMEM;
476 return AE_NO_MEMORY;
477 }
Jiang Liu90e97822015-02-05 13:44:43 +0800478 *rentry->res = win->res;
Jiang Liu93286f42015-02-02 10:43:00 +0800479 rentry->offset = win->offset;
Jiang Liu90e97822015-02-05 13:44:43 +0800480 resource_list_add_tail(rentry, c->list);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100481 c->count++;
482 return AE_OK;
483}
484
485static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
486 void *context)
487{
488 struct res_proc_context *c = context;
Jiang Liua49170b2015-02-02 10:42:58 +0800489 struct resource_win win;
490 struct resource *res = &win.res;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100491 int i;
492
493 if (c->preproc) {
494 int ret;
495
496 ret = c->preproc(ares, c->preproc_data);
497 if (ret < 0) {
498 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100499 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100500 } else if (ret > 0) {
501 return AE_OK;
502 }
503 }
504
Jiang Liua49170b2015-02-02 10:42:58 +0800505 memset(&win, 0, sizeof(win));
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100506
Jiang Liua49170b2015-02-02 10:42:58 +0800507 if (acpi_dev_resource_memory(ares, res)
508 || acpi_dev_resource_io(ares, res)
509 || acpi_dev_resource_address_space(ares, &win)
510 || acpi_dev_resource_ext_address_space(ares, &win))
511 return acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100512
Jiang Liua49170b2015-02-02 10:42:58 +0800513 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100514 acpi_status status;
515
Jiang Liua49170b2015-02-02 10:42:58 +0800516 status = acpi_dev_new_resource_entry(&win, c);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100517 if (ACPI_FAILURE(status))
518 return status;
519 }
520
521 return AE_OK;
522}
523
524/**
525 * acpi_dev_get_resources - Get current resources of a device.
526 * @adev: ACPI device node to get the resources for.
527 * @list: Head of the resultant list of resources (must be empty).
528 * @preproc: The caller's preprocessing routine.
529 * @preproc_data: Pointer passed to the caller's preprocessing routine.
530 *
531 * Evaluate the _CRS method for the given device node and process its output by
532 * (1) executing the @preproc() rountine provided by the caller, passing the
533 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
534 * returned and (2) converting all of the returned ACPI resources into struct
535 * resource objects if possible. If the return value of @preproc() in step (1)
536 * is different from 0, step (2) is not applied to the given ACPI resource and
537 * if that value is negative, the whole processing is aborted and that value is
538 * returned as the final error code.
539 *
540 * The resultant struct resource objects are put on the list pointed to by
Jiang Liu90e97822015-02-05 13:44:43 +0800541 * @list, that must be empty initially, as members of struct resource_entry
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100542 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
543 * free that list.
544 *
545 * The number of resources in the output list is returned on success, an error
546 * code reflecting the error condition is returned otherwise.
547 */
548int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
549 int (*preproc)(struct acpi_resource *, void *),
550 void *preproc_data)
551{
552 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100553 acpi_status status;
554
555 if (!adev || !adev->handle || !list_empty(list))
556 return -EINVAL;
557
Jiang Liu952c63e2013-06-29 00:24:38 +0800558 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100559 return 0;
560
561 c.list = list;
562 c.preproc = preproc;
563 c.preproc_data = preproc_data;
564 c.count = 0;
565 c.error = 0;
566 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
567 acpi_dev_process_resource, &c);
568 if (ACPI_FAILURE(status)) {
569 acpi_dev_free_resource_list(list);
570 return c.error ? c.error : -EIO;
571 }
572
573 return c.count;
574}
575EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
Jiang Liu62d11412015-02-02 10:43:01 +0800576
577/**
578 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
579 * types
580 * @ares: Input ACPI resource object.
581 * @types: Valid resource types of IORESOURCE_XXX
582 *
Jiang Liu2c62e842015-04-30 12:41:28 +0800583 * This is a helper function to support acpi_dev_get_resources(), which filters
Jiang Liu62d11412015-02-02 10:43:01 +0800584 * ACPI resource objects according to resource types.
585 */
586int acpi_dev_filter_resource_type(struct acpi_resource *ares,
587 unsigned long types)
588{
589 unsigned long type = 0;
590
591 switch (ares->type) {
592 case ACPI_RESOURCE_TYPE_MEMORY24:
593 case ACPI_RESOURCE_TYPE_MEMORY32:
594 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
595 type = IORESOURCE_MEM;
596 break;
597 case ACPI_RESOURCE_TYPE_IO:
598 case ACPI_RESOURCE_TYPE_FIXED_IO:
599 type = IORESOURCE_IO;
600 break;
601 case ACPI_RESOURCE_TYPE_IRQ:
602 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
603 type = IORESOURCE_IRQ;
604 break;
605 case ACPI_RESOURCE_TYPE_DMA:
606 case ACPI_RESOURCE_TYPE_FIXED_DMA:
607 type = IORESOURCE_DMA;
608 break;
609 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
610 type = IORESOURCE_REG;
611 break;
612 case ACPI_RESOURCE_TYPE_ADDRESS16:
613 case ACPI_RESOURCE_TYPE_ADDRESS32:
614 case ACPI_RESOURCE_TYPE_ADDRESS64:
615 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
616 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
617 type = IORESOURCE_MEM;
618 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
619 type = IORESOURCE_IO;
620 else if (ares->data.address.resource_type ==
621 ACPI_BUS_NUMBER_RANGE)
622 type = IORESOURCE_BUS;
623 break;
624 default:
625 break;
626 }
627
628 return (type & types) ? 0 : 1;
629}
630EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);
Rafael J. Wysocki0f1b4142015-06-18 18:32:02 +0200631
632struct reserved_region {
633 struct list_head node;
634 u64 start;
635 u64 end;
636};
637
638static LIST_HEAD(reserved_io_regions);
639static LIST_HEAD(reserved_mem_regions);
640
641static int request_range(u64 start, u64 end, u8 space_id, unsigned long flags,
642 char *desc)
643{
644 unsigned int length = end - start + 1;
645 struct resource *res;
646
647 res = space_id == ACPI_ADR_SPACE_SYSTEM_IO ?
648 request_region(start, length, desc) :
649 request_mem_region(start, length, desc);
650 if (!res)
651 return -EIO;
652
653 res->flags &= ~flags;
654 return 0;
655}
656
657static int add_region_before(u64 start, u64 end, u8 space_id,
658 unsigned long flags, char *desc,
659 struct list_head *head)
660{
661 struct reserved_region *reg;
662 int error;
663
664 reg = kmalloc(sizeof(*reg), GFP_KERNEL);
665 if (!reg)
666 return -ENOMEM;
667
668 error = request_range(start, end, space_id, flags, desc);
Dan Carpenter7bc10382015-06-24 17:30:15 +0300669 if (error) {
670 kfree(reg);
Rafael J. Wysocki0f1b4142015-06-18 18:32:02 +0200671 return error;
Dan Carpenter7bc10382015-06-24 17:30:15 +0300672 }
Rafael J. Wysocki0f1b4142015-06-18 18:32:02 +0200673
674 reg->start = start;
675 reg->end = end;
676 list_add_tail(&reg->node, head);
677 return 0;
678}
679
680/**
681 * acpi_reserve_region - Reserve an I/O or memory region as a system resource.
682 * @start: Starting address of the region.
683 * @length: Length of the region.
684 * @space_id: Identifier of address space to reserve the region from.
685 * @flags: Resource flags to clear for the region after requesting it.
686 * @desc: Region description (for messages).
687 *
688 * Reserve an I/O or memory region as a system resource to prevent others from
689 * using it. If the new region overlaps with one of the regions (in the given
690 * address space) already reserved by this routine, only the non-overlapping
691 * parts of it will be reserved.
692 *
693 * Returned is either 0 (success) or a negative error code indicating a resource
694 * reservation problem. It is the code of the first encountered error, but the
695 * routine doesn't abort until it has attempted to request all of the parts of
696 * the new region that don't overlap with other regions reserved previously.
697 *
698 * The resources requested by this routine are never released.
699 */
700int acpi_reserve_region(u64 start, unsigned int length, u8 space_id,
701 unsigned long flags, char *desc)
702{
703 struct list_head *regions;
704 struct reserved_region *reg;
705 u64 end = start + length - 1;
706 int ret = 0, error = 0;
707
708 if (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
709 regions = &reserved_io_regions;
710 else if (space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
711 regions = &reserved_mem_regions;
712 else
713 return -EINVAL;
714
715 if (list_empty(regions))
716 return add_region_before(start, end, space_id, flags, desc, regions);
717
718 list_for_each_entry(reg, regions, node)
719 if (reg->start == end + 1) {
720 /* The new region can be prepended to this one. */
721 ret = request_range(start, end, space_id, flags, desc);
722 if (!ret)
723 reg->start = start;
724
725 return ret;
726 } else if (reg->start > end) {
727 /* No overlap. Add the new region here and get out. */
728 return add_region_before(start, end, space_id, flags,
729 desc, &reg->node);
730 } else if (reg->end == start - 1) {
731 goto combine;
732 } else if (reg->end >= start) {
733 goto overlap;
734 }
735
736 /* The new region goes after the last existing one. */
737 return add_region_before(start, end, space_id, flags, desc, regions);
738
739 overlap:
740 /*
741 * The new region overlaps an existing one.
742 *
743 * The head part of the new region immediately preceding the existing
744 * overlapping one can be combined with it right away.
745 */
746 if (reg->start > start) {
747 error = request_range(start, reg->start - 1, space_id, flags, desc);
748 if (error)
749 ret = error;
750 else
751 reg->start = start;
752 }
753
754 combine:
755 /*
756 * The new region is adjacent to an existing one. If it extends beyond
757 * that region all the way to the next one, it is possible to combine
758 * all three of them.
759 */
760 while (reg->end < end) {
761 struct reserved_region *next = NULL;
762 u64 a = reg->end + 1, b = end;
763
764 if (!list_is_last(&reg->node, regions)) {
765 next = list_next_entry(reg, node);
766 if (next->start <= end)
767 b = next->start - 1;
768 }
769 error = request_range(a, b, space_id, flags, desc);
770 if (!error) {
771 if (next && next->start == b + 1) {
772 reg->end = next->end;
773 list_del(&next->node);
774 kfree(next);
775 } else {
776 reg->end = end;
777 break;
778 }
779 } else if (next) {
780 if (!ret)
781 ret = error;
782
783 reg = next;
784 } else {
785 break;
786 }
787 }
788
789 return ret ? ret : error;
790}
791EXPORT_SYMBOL_GPL(acpi_reserve_region);