]>
Commit | Line | Data |
---|---|---|
046d9ce6 RW |
1 | /* |
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 | * | |
046d9ce6 RW |
18 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
19 | */ | |
20 | ||
21 | #include <linux/acpi.h> | |
22 | #include <linux/device.h> | |
23 | #include <linux/export.h> | |
24 | #include <linux/ioport.h> | |
8e345c99 | 25 | #include <linux/slab.h> |
55a93417 | 26 | #include <linux/irq.h> |
046d9ce6 RW |
27 | |
28 | #ifdef CONFIG_X86 | |
29 | #define valid_IRQ(i) (((i) != 0) && ((i) != 2)) | |
4a2e7aab LP |
30 | static inline bool acpi_iospace_resource_valid(struct resource *res) |
31 | { | |
32 | /* On X86 IO space is limited to the [0 - 64K] IO port range */ | |
33 | return res->end < 0x10003; | |
34 | } | |
046d9ce6 RW |
35 | #else |
36 | #define valid_IRQ(i) (true) | |
4a2e7aab LP |
37 | /* |
38 | * ACPI IO descriptors on arches other than X86 contain MMIO CPU physical | |
39 | * addresses mapping IO space in CPU physical address space, IO space | |
40 | * resources can be placed anywhere in the 64-bit physical address space. | |
41 | */ | |
42 | static inline bool | |
43 | acpi_iospace_resource_valid(struct resource *res) { return true; } | |
046d9ce6 RW |
44 | #endif |
45 | ||
fa20b176 AVF |
46 | #if IS_ENABLED(CONFIG_ACPI_GENERIC_GSI) |
47 | static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq) | |
48 | { | |
49 | return ext_irq->resource_source.string_length == 0 && | |
50 | ext_irq->producer_consumer == ACPI_CONSUMER; | |
51 | } | |
52 | #else | |
53 | static inline bool is_gsi(struct acpi_resource_extended_irq *ext_irq) | |
54 | { | |
55 | return true; | |
56 | } | |
57 | #endif | |
58 | ||
c420dbd1 | 59 | static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io) |
046d9ce6 | 60 | { |
c420dbd1 | 61 | u64 reslen = end - start + 1; |
046d9ce6 | 62 | |
c420dbd1 TG |
63 | /* |
64 | * CHECKME: len might be required to check versus a minimum | |
65 | * length as well. 1 for io is fine, but for memory it does | |
66 | * not make any sense at all. | |
aa714d28 JL |
67 | * Note: some BIOSes report incorrect length for ACPI address space |
68 | * descriptor, so remove check of 'reslen == len' to avoid regression. | |
c420dbd1 | 69 | */ |
aa714d28 | 70 | if (len && reslen && start <= end) |
c420dbd1 TG |
71 | return true; |
72 | ||
6a239af2 | 73 | pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n", |
c420dbd1 TG |
74 | io ? "io" : "mem", start, end, len); |
75 | ||
76 | return false; | |
77 | } | |
78 | ||
79 | static void acpi_dev_memresource_flags(struct resource *res, u64 len, | |
72e26b0d | 80 | u8 write_protect) |
c420dbd1 TG |
81 | { |
82 | res->flags = IORESOURCE_MEM; | |
83 | ||
84 | if (!acpi_dev_resource_len_valid(res->start, res->end, len, false)) | |
c78b6885 | 85 | res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; |
046d9ce6 RW |
86 | |
87 | if (write_protect == ACPI_READ_WRITE_MEMORY) | |
c420dbd1 | 88 | res->flags |= IORESOURCE_MEM_WRITEABLE; |
046d9ce6 RW |
89 | } |
90 | ||
91 | static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len, | |
92 | u8 write_protect) | |
93 | { | |
94 | res->start = start; | |
95 | res->end = start + len - 1; | |
72e26b0d | 96 | acpi_dev_memresource_flags(res, len, write_protect); |
046d9ce6 RW |
97 | } |
98 | ||
99 | /** | |
100 | * acpi_dev_resource_memory - Extract ACPI memory resource information. | |
101 | * @ares: Input ACPI resource object. | |
102 | * @res: Output generic resource object. | |
103 | * | |
104 | * Check if the given ACPI resource object represents a memory resource and | |
105 | * if that's the case, use the information in it to populate the generic | |
106 | * resource object pointed to by @res. | |
581c19f3 JL |
107 | * |
108 | * Return: | |
109 | * 1) false with res->flags setting to zero: not the expected resource type | |
110 | * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource | |
111 | * 3) true: valid assigned resource | |
046d9ce6 RW |
112 | */ |
113 | bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res) | |
114 | { | |
115 | struct acpi_resource_memory24 *memory24; | |
116 | struct acpi_resource_memory32 *memory32; | |
117 | struct acpi_resource_fixed_memory32 *fixed_memory32; | |
118 | ||
119 | switch (ares->type) { | |
120 | case ACPI_RESOURCE_TYPE_MEMORY24: | |
121 | memory24 = &ares->data.memory24; | |
8515f936 JL |
122 | acpi_dev_get_memresource(res, memory24->minimum << 8, |
123 | memory24->address_length << 8, | |
046d9ce6 RW |
124 | memory24->write_protect); |
125 | break; | |
126 | case ACPI_RESOURCE_TYPE_MEMORY32: | |
127 | memory32 = &ares->data.memory32; | |
128 | acpi_dev_get_memresource(res, memory32->minimum, | |
129 | memory32->address_length, | |
130 | memory32->write_protect); | |
131 | break; | |
132 | case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: | |
133 | fixed_memory32 = &ares->data.fixed_memory32; | |
134 | acpi_dev_get_memresource(res, fixed_memory32->address, | |
135 | fixed_memory32->address_length, | |
136 | fixed_memory32->write_protect); | |
137 | break; | |
138 | default: | |
581c19f3 | 139 | res->flags = 0; |
046d9ce6 RW |
140 | return false; |
141 | } | |
c420dbd1 TG |
142 | |
143 | return !(res->flags & IORESOURCE_DISABLED); | |
046d9ce6 RW |
144 | } |
145 | EXPORT_SYMBOL_GPL(acpi_dev_resource_memory); | |
146 | ||
1d0420f1 | 147 | static void acpi_dev_ioresource_flags(struct resource *res, u64 len, |
91236ecc | 148 | u8 io_decode, u8 translation_type) |
046d9ce6 | 149 | { |
1d0420f1 | 150 | res->flags = IORESOURCE_IO; |
046d9ce6 | 151 | |
1d0420f1 | 152 | if (!acpi_dev_resource_len_valid(res->start, res->end, len, true)) |
c78b6885 | 153 | res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; |
1d0420f1 | 154 | |
4a2e7aab | 155 | if (!acpi_iospace_resource_valid(res)) |
c78b6885 | 156 | res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET; |
046d9ce6 | 157 | |
1d0420f1 TG |
158 | if (io_decode == ACPI_DECODE_16) |
159 | res->flags |= IORESOURCE_IO_16BIT_ADDR; | |
91236ecc JL |
160 | if (translation_type == ACPI_SPARSE_TRANSLATION) |
161 | res->flags |= IORESOURCE_IO_SPARSE; | |
046d9ce6 RW |
162 | } |
163 | ||
164 | static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len, | |
165 | u8 io_decode) | |
166 | { | |
046d9ce6 | 167 | res->start = start; |
1d0420f1 | 168 | res->end = start + len - 1; |
91236ecc | 169 | acpi_dev_ioresource_flags(res, len, io_decode, 0); |
046d9ce6 RW |
170 | } |
171 | ||
172 | /** | |
173 | * acpi_dev_resource_io - Extract ACPI I/O resource information. | |
174 | * @ares: Input ACPI resource object. | |
175 | * @res: Output generic resource object. | |
176 | * | |
177 | * Check if the given ACPI resource object represents an I/O resource and | |
178 | * if that's the case, use the information in it to populate the generic | |
179 | * resource object pointed to by @res. | |
581c19f3 JL |
180 | * |
181 | * Return: | |
182 | * 1) false with res->flags setting to zero: not the expected resource type | |
183 | * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource | |
184 | * 3) true: valid assigned resource | |
046d9ce6 RW |
185 | */ |
186 | bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res) | |
187 | { | |
188 | struct acpi_resource_io *io; | |
189 | struct acpi_resource_fixed_io *fixed_io; | |
190 | ||
191 | switch (ares->type) { | |
192 | case ACPI_RESOURCE_TYPE_IO: | |
193 | io = &ares->data.io; | |
194 | acpi_dev_get_ioresource(res, io->minimum, | |
195 | io->address_length, | |
196 | io->io_decode); | |
197 | break; | |
198 | case ACPI_RESOURCE_TYPE_FIXED_IO: | |
199 | fixed_io = &ares->data.fixed_io; | |
200 | acpi_dev_get_ioresource(res, fixed_io->address, | |
201 | fixed_io->address_length, | |
202 | ACPI_DECODE_10); | |
203 | break; | |
204 | default: | |
581c19f3 | 205 | res->flags = 0; |
046d9ce6 RW |
206 | return false; |
207 | } | |
1d0420f1 TG |
208 | |
209 | return !(res->flags & IORESOURCE_DISABLED); | |
046d9ce6 RW |
210 | } |
211 | EXPORT_SYMBOL_GPL(acpi_dev_resource_io); | |
212 | ||
a49170b5 | 213 | static bool acpi_decode_space(struct resource_win *win, |
eb76d55e TG |
214 | struct acpi_resource_address *addr, |
215 | struct acpi_address64_attribute *attr) | |
216 | { | |
217 | u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16; | |
eb76d55e TG |
218 | bool wp = addr->info.mem.write_protect; |
219 | u64 len = attr->address_length; | |
1fb01ca9 | 220 | u64 start, end, offset = 0; |
a49170b5 | 221 | struct resource *res = &win->res; |
eb76d55e | 222 | |
a274019f JL |
223 | /* |
224 | * Filter out invalid descriptor according to ACPI Spec 5.0, section | |
225 | * 6.4.3.5 Address Space Resource Descriptors. | |
226 | */ | |
227 | if ((addr->min_address_fixed != addr->max_address_fixed && len) || | |
228 | (addr->min_address_fixed && addr->max_address_fixed && !len)) | |
229 | pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n", | |
230 | addr->min_address_fixed, addr->max_address_fixed, len); | |
231 | ||
2ea3d266 JL |
232 | /* |
233 | * For bridges that translate addresses across the bridge, | |
234 | * translation_offset is the offset that must be added to the | |
235 | * address on the secondary side to obtain the address on the | |
236 | * primary side. Non-bridge devices must list 0 for all Address | |
237 | * Translation offset bits. | |
238 | */ | |
1fb01ca9 JL |
239 | if (addr->producer_consumer == ACPI_PRODUCER) |
240 | offset = attr->translation_offset; | |
241 | else if (attr->translation_offset) | |
2ea3d266 JL |
242 | pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n", |
243 | attr->translation_offset); | |
1fb01ca9 JL |
244 | start = attr->minimum + offset; |
245 | end = attr->maximum + offset; | |
246 | ||
247 | win->offset = offset; | |
248 | res->start = start; | |
249 | res->end = end; | |
250 | if (sizeof(resource_size_t) < sizeof(u64) && | |
251 | (offset != win->offset || start != res->start || end != res->end)) { | |
252 | pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n", | |
253 | attr->minimum, attr->maximum); | |
254 | return false; | |
2ea3d266 JL |
255 | } |
256 | ||
eb76d55e TG |
257 | switch (addr->resource_type) { |
258 | case ACPI_MEMORY_RANGE: | |
72e26b0d | 259 | acpi_dev_memresource_flags(res, len, wp); |
eb76d55e TG |
260 | break; |
261 | case ACPI_IO_RANGE: | |
91236ecc JL |
262 | acpi_dev_ioresource_flags(res, len, iodec, |
263 | addr->info.io.translation_type); | |
eb76d55e TG |
264 | break; |
265 | case ACPI_BUS_NUMBER_RANGE: | |
266 | res->flags = IORESOURCE_BUS; | |
267 | break; | |
268 | default: | |
269 | return false; | |
270 | } | |
271 | ||
72e26b0d TG |
272 | if (addr->producer_consumer == ACPI_PRODUCER) |
273 | res->flags |= IORESOURCE_WINDOW; | |
fcb29bbc TG |
274 | |
275 | if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY) | |
276 | res->flags |= IORESOURCE_PREFETCH; | |
72e26b0d | 277 | |
eb76d55e TG |
278 | return !(res->flags & IORESOURCE_DISABLED); |
279 | } | |
280 | ||
046d9ce6 RW |
281 | /** |
282 | * acpi_dev_resource_address_space - Extract ACPI address space information. | |
283 | * @ares: Input ACPI resource object. | |
a49170b5 | 284 | * @win: Output generic resource object. |
046d9ce6 RW |
285 | * |
286 | * Check if the given ACPI resource object represents an address space resource | |
287 | * and if that's the case, use the information in it to populate the generic | |
a49170b5 | 288 | * resource object pointed to by @win. |
581c19f3 JL |
289 | * |
290 | * Return: | |
a49170b5 JL |
291 | * 1) false with win->res.flags setting to zero: not the expected resource type |
292 | * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned | |
293 | * resource | |
581c19f3 | 294 | * 3) true: valid assigned resource |
046d9ce6 RW |
295 | */ |
296 | bool acpi_dev_resource_address_space(struct acpi_resource *ares, | |
a49170b5 | 297 | struct resource_win *win) |
046d9ce6 | 298 | { |
046d9ce6 | 299 | struct acpi_resource_address64 addr; |
046d9ce6 | 300 | |
a49170b5 | 301 | win->res.flags = 0; |
eb76d55e | 302 | if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr))) |
6658c739 | 303 | return false; |
046d9ce6 | 304 | |
a49170b5 | 305 | return acpi_decode_space(win, (struct acpi_resource_address *)&addr, |
eb76d55e | 306 | &addr.address); |
046d9ce6 RW |
307 | } |
308 | EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space); | |
309 | ||
310 | /** | |
311 | * acpi_dev_resource_ext_address_space - Extract ACPI address space information. | |
312 | * @ares: Input ACPI resource object. | |
a49170b5 | 313 | * @win: Output generic resource object. |
046d9ce6 RW |
314 | * |
315 | * Check if the given ACPI resource object represents an extended address space | |
316 | * resource and if that's the case, use the information in it to populate the | |
a49170b5 | 317 | * generic resource object pointed to by @win. |
581c19f3 JL |
318 | * |
319 | * Return: | |
a49170b5 JL |
320 | * 1) false with win->res.flags setting to zero: not the expected resource type |
321 | * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned | |
322 | * resource | |
581c19f3 | 323 | * 3) true: valid assigned resource |
046d9ce6 RW |
324 | */ |
325 | bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares, | |
a49170b5 | 326 | struct resource_win *win) |
046d9ce6 RW |
327 | { |
328 | struct acpi_resource_extended_address64 *ext_addr; | |
046d9ce6 | 329 | |
a49170b5 | 330 | win->res.flags = 0; |
046d9ce6 RW |
331 | if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) |
332 | return false; | |
333 | ||
334 | ext_addr = &ares->data.ext_address64; | |
335 | ||
a49170b5 | 336 | return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr, |
eb76d55e | 337 | &ext_addr->address); |
046d9ce6 RW |
338 | } |
339 | EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space); | |
340 | ||
341 | /** | |
342 | * acpi_dev_irq_flags - Determine IRQ resource flags. | |
343 | * @triggering: Triggering type as provided by ACPI. | |
344 | * @polarity: Interrupt polarity as provided by ACPI. | |
345 | * @shareable: Whether or not the interrupt is shareable. | |
346 | */ | |
347 | unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable) | |
348 | { | |
349 | unsigned long flags; | |
350 | ||
351 | if (triggering == ACPI_LEVEL_SENSITIVE) | |
352 | flags = polarity == ACPI_ACTIVE_LOW ? | |
353 | IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL; | |
354 | else | |
355 | flags = polarity == ACPI_ACTIVE_LOW ? | |
356 | IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE; | |
357 | ||
358 | if (shareable == ACPI_SHARED) | |
359 | flags |= IORESOURCE_IRQ_SHAREABLE; | |
360 | ||
361 | return flags | IORESOURCE_IRQ; | |
362 | } | |
363 | EXPORT_SYMBOL_GPL(acpi_dev_irq_flags); | |
364 | ||
55a93417 CR |
365 | /** |
366 | * acpi_dev_get_irq_type - Determine irq type. | |
367 | * @triggering: Triggering type as provided by ACPI. | |
368 | * @polarity: Interrupt polarity as provided by ACPI. | |
369 | */ | |
370 | unsigned int acpi_dev_get_irq_type(int triggering, int polarity) | |
371 | { | |
372 | switch (polarity) { | |
373 | case ACPI_ACTIVE_LOW: | |
374 | return triggering == ACPI_EDGE_SENSITIVE ? | |
375 | IRQ_TYPE_EDGE_FALLING : | |
376 | IRQ_TYPE_LEVEL_LOW; | |
377 | case ACPI_ACTIVE_HIGH: | |
378 | return triggering == ACPI_EDGE_SENSITIVE ? | |
379 | IRQ_TYPE_EDGE_RISING : | |
380 | IRQ_TYPE_LEVEL_HIGH; | |
381 | case ACPI_ACTIVE_BOTH: | |
382 | if (triggering == ACPI_EDGE_SENSITIVE) | |
383 | return IRQ_TYPE_EDGE_BOTH; | |
384 | default: | |
385 | return IRQ_TYPE_NONE; | |
386 | } | |
387 | } | |
388 | EXPORT_SYMBOL_GPL(acpi_dev_get_irq_type); | |
389 | ||
046d9ce6 RW |
390 | static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi) |
391 | { | |
392 | res->start = gsi; | |
393 | res->end = gsi; | |
c78b6885 | 394 | res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET; |
046d9ce6 RW |
395 | } |
396 | ||
397 | static void acpi_dev_get_irqresource(struct resource *res, u32 gsi, | |
204ebc0a MW |
398 | u8 triggering, u8 polarity, u8 shareable, |
399 | bool legacy) | |
046d9ce6 RW |
400 | { |
401 | int irq, p, t; | |
402 | ||
403 | if (!valid_IRQ(gsi)) { | |
404 | acpi_dev_irqresource_disabled(res, gsi); | |
405 | return; | |
406 | } | |
407 | ||
408 | /* | |
03671057 | 409 | * In IO-APIC mode, use overridden attribute. Two reasons: |
046d9ce6 RW |
410 | * 1. BIOS bug in DSDT |
411 | * 2. BIOS uses IO-APIC mode Interrupt Source Override | |
204ebc0a MW |
412 | * |
413 | * We do this only if we are dealing with IRQ() or IRQNoFlags() | |
414 | * resource (the legacy ISA resources). With modern ACPI 5 devices | |
415 | * using extended IRQ descriptors we take the IRQ configuration | |
416 | * from _CRS directly. | |
046d9ce6 | 417 | */ |
204ebc0a | 418 | if (legacy && !acpi_get_override_irq(gsi, &t, &p)) { |
046d9ce6 RW |
419 | u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; |
420 | u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; | |
421 | ||
422 | if (triggering != trig || polarity != pol) { | |
423 | pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi, | |
204ebc0a | 424 | t ? "level" : "edge", p ? "low" : "high"); |
046d9ce6 RW |
425 | triggering = trig; |
426 | polarity = pol; | |
427 | } | |
428 | } | |
429 | ||
430 | res->flags = acpi_dev_irq_flags(triggering, polarity, shareable); | |
431 | irq = acpi_register_gsi(NULL, gsi, triggering, polarity); | |
432 | if (irq >= 0) { | |
433 | res->start = irq; | |
434 | res->end = irq; | |
435 | } else { | |
436 | acpi_dev_irqresource_disabled(res, gsi); | |
437 | } | |
438 | } | |
439 | ||
440 | /** | |
441 | * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information. | |
442 | * @ares: Input ACPI resource object. | |
443 | * @index: Index into the array of GSIs represented by the resource. | |
444 | * @res: Output generic resource object. | |
445 | * | |
446 | * Check if the given ACPI resource object represents an interrupt resource | |
447 | * and @index does not exceed the resource's interrupt count (true is returned | |
448 | * in that case regardless of the results of the other checks)). If that's the | |
449 | * case, register the GSI corresponding to @index from the array of interrupts | |
450 | * represented by the resource and populate the generic resource object pointed | |
451 | * to by @res accordingly. If the registration of the GSI is not successful, | |
452 | * IORESOURCE_DISABLED will be set it that object's flags. | |
581c19f3 JL |
453 | * |
454 | * Return: | |
455 | * 1) false with res->flags setting to zero: not the expected resource type | |
456 | * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource | |
457 | * 3) true: valid assigned resource | |
046d9ce6 RW |
458 | */ |
459 | bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index, | |
460 | struct resource *res) | |
461 | { | |
462 | struct acpi_resource_irq *irq; | |
463 | struct acpi_resource_extended_irq *ext_irq; | |
464 | ||
465 | switch (ares->type) { | |
466 | case ACPI_RESOURCE_TYPE_IRQ: | |
467 | /* | |
468 | * Per spec, only one interrupt per descriptor is allowed in | |
469 | * _CRS, but some firmware violates this, so parse them all. | |
470 | */ | |
471 | irq = &ares->data.irq; | |
472 | if (index >= irq->interrupt_count) { | |
473 | acpi_dev_irqresource_disabled(res, 0); | |
474 | return false; | |
475 | } | |
476 | acpi_dev_get_irqresource(res, irq->interrupts[index], | |
477 | irq->triggering, irq->polarity, | |
204ebc0a | 478 | irq->sharable, true); |
046d9ce6 RW |
479 | break; |
480 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: | |
481 | ext_irq = &ares->data.extended_irq; | |
482 | if (index >= ext_irq->interrupt_count) { | |
483 | acpi_dev_irqresource_disabled(res, 0); | |
484 | return false; | |
485 | } | |
fa20b176 AVF |
486 | if (is_gsi(ext_irq)) |
487 | acpi_dev_get_irqresource(res, ext_irq->interrupts[index], | |
046d9ce6 | 488 | ext_irq->triggering, ext_irq->polarity, |
204ebc0a | 489 | ext_irq->sharable, false); |
fa20b176 AVF |
490 | else |
491 | acpi_dev_irqresource_disabled(res, 0); | |
046d9ce6 RW |
492 | break; |
493 | default: | |
581c19f3 | 494 | res->flags = 0; |
046d9ce6 RW |
495 | return false; |
496 | } | |
497 | ||
498 | return true; | |
499 | } | |
500 | EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt); | |
8e345c99 RW |
501 | |
502 | /** | |
503 | * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources(). | |
504 | * @list: The head of the resource list to free. | |
505 | */ | |
506 | void acpi_dev_free_resource_list(struct list_head *list) | |
507 | { | |
90e97820 | 508 | resource_list_free(list); |
8e345c99 RW |
509 | } |
510 | EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list); | |
511 | ||
512 | struct res_proc_context { | |
513 | struct list_head *list; | |
514 | int (*preproc)(struct acpi_resource *, void *); | |
515 | void *preproc_data; | |
516 | int count; | |
517 | int error; | |
518 | }; | |
519 | ||
a49170b5 | 520 | static acpi_status acpi_dev_new_resource_entry(struct resource_win *win, |
8e345c99 RW |
521 | struct res_proc_context *c) |
522 | { | |
90e97820 | 523 | struct resource_entry *rentry; |
8e345c99 | 524 | |
90e97820 | 525 | rentry = resource_list_create_entry(NULL, 0); |
8e345c99 RW |
526 | if (!rentry) { |
527 | c->error = -ENOMEM; | |
528 | return AE_NO_MEMORY; | |
529 | } | |
90e97820 | 530 | *rentry->res = win->res; |
93286f47 | 531 | rentry->offset = win->offset; |
90e97820 | 532 | resource_list_add_tail(rentry, c->list); |
8e345c99 RW |
533 | c->count++; |
534 | return AE_OK; | |
535 | } | |
536 | ||
537 | static acpi_status acpi_dev_process_resource(struct acpi_resource *ares, | |
538 | void *context) | |
539 | { | |
540 | struct res_proc_context *c = context; | |
a49170b5 JL |
541 | struct resource_win win; |
542 | struct resource *res = &win.res; | |
8e345c99 RW |
543 | int i; |
544 | ||
545 | if (c->preproc) { | |
546 | int ret; | |
547 | ||
548 | ret = c->preproc(ares, c->preproc_data); | |
549 | if (ret < 0) { | |
550 | c->error = ret; | |
8a66790b | 551 | return AE_CTRL_TERMINATE; |
8e345c99 RW |
552 | } else if (ret > 0) { |
553 | return AE_OK; | |
554 | } | |
555 | } | |
556 | ||
a49170b5 | 557 | memset(&win, 0, sizeof(win)); |
8e345c99 | 558 | |
a49170b5 JL |
559 | if (acpi_dev_resource_memory(ares, res) |
560 | || acpi_dev_resource_io(ares, res) | |
561 | || acpi_dev_resource_address_space(ares, &win) | |
562 | || acpi_dev_resource_ext_address_space(ares, &win)) | |
563 | return acpi_dev_new_resource_entry(&win, c); | |
8e345c99 | 564 | |
a49170b5 | 565 | for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) { |
8e345c99 RW |
566 | acpi_status status; |
567 | ||
a49170b5 | 568 | status = acpi_dev_new_resource_entry(&win, c); |
8e345c99 RW |
569 | if (ACPI_FAILURE(status)) |
570 | return status; | |
571 | } | |
572 | ||
573 | return AE_OK; | |
574 | } | |
575 | ||
4f0450af LP |
576 | static int __acpi_dev_get_resources(struct acpi_device *adev, |
577 | struct list_head *list, | |
578 | int (*preproc)(struct acpi_resource *, void *), | |
579 | void *preproc_data, char *method) | |
580 | { | |
581 | struct res_proc_context c; | |
582 | acpi_status status; | |
583 | ||
584 | if (!adev || !adev->handle || !list_empty(list)) | |
585 | return -EINVAL; | |
586 | ||
587 | if (!acpi_has_method(adev->handle, method)) | |
588 | return 0; | |
589 | ||
590 | c.list = list; | |
591 | c.preproc = preproc; | |
592 | c.preproc_data = preproc_data; | |
593 | c.count = 0; | |
594 | c.error = 0; | |
595 | status = acpi_walk_resources(adev->handle, method, | |
596 | acpi_dev_process_resource, &c); | |
597 | if (ACPI_FAILURE(status)) { | |
598 | acpi_dev_free_resource_list(list); | |
599 | return c.error ? c.error : -EIO; | |
600 | } | |
601 | ||
602 | return c.count; | |
603 | } | |
604 | ||
8e345c99 RW |
605 | /** |
606 | * acpi_dev_get_resources - Get current resources of a device. | |
607 | * @adev: ACPI device node to get the resources for. | |
608 | * @list: Head of the resultant list of resources (must be empty). | |
609 | * @preproc: The caller's preprocessing routine. | |
610 | * @preproc_data: Pointer passed to the caller's preprocessing routine. | |
611 | * | |
612 | * Evaluate the _CRS method for the given device node and process its output by | |
613 | * (1) executing the @preproc() rountine provided by the caller, passing the | |
614 | * resource pointer and @preproc_data to it as arguments, for each ACPI resource | |
615 | * returned and (2) converting all of the returned ACPI resources into struct | |
616 | * resource objects if possible. If the return value of @preproc() in step (1) | |
617 | * is different from 0, step (2) is not applied to the given ACPI resource and | |
618 | * if that value is negative, the whole processing is aborted and that value is | |
619 | * returned as the final error code. | |
620 | * | |
621 | * The resultant struct resource objects are put on the list pointed to by | |
90e97820 | 622 | * @list, that must be empty initially, as members of struct resource_entry |
8e345c99 RW |
623 | * objects. Callers of this routine should use %acpi_dev_free_resource_list() to |
624 | * free that list. | |
625 | * | |
626 | * The number of resources in the output list is returned on success, an error | |
627 | * code reflecting the error condition is returned otherwise. | |
628 | */ | |
629 | int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list, | |
630 | int (*preproc)(struct acpi_resource *, void *), | |
631 | void *preproc_data) | |
632 | { | |
4f0450af LP |
633 | return __acpi_dev_get_resources(adev, list, preproc, preproc_data, |
634 | METHOD_NAME__CRS); | |
8e345c99 RW |
635 | } |
636 | EXPORT_SYMBOL_GPL(acpi_dev_get_resources); | |
62d1141f | 637 | |
c04ac679 LP |
638 | static int is_memory(struct acpi_resource *ares, void *not_used) |
639 | { | |
640 | struct resource_win win; | |
641 | struct resource *res = &win.res; | |
642 | ||
643 | memset(&win, 0, sizeof(win)); | |
644 | ||
645 | return !(acpi_dev_resource_memory(ares, res) | |
646 | || acpi_dev_resource_address_space(ares, &win) | |
647 | || acpi_dev_resource_ext_address_space(ares, &win)); | |
648 | } | |
649 | ||
650 | /** | |
651 | * acpi_dev_get_dma_resources - Get current DMA resources of a device. | |
652 | * @adev: ACPI device node to get the resources for. | |
653 | * @list: Head of the resultant list of resources (must be empty). | |
654 | * | |
655 | * Evaluate the _DMA method for the given device node and process its | |
656 | * output. | |
657 | * | |
658 | * The resultant struct resource objects are put on the list pointed to | |
659 | * by @list, that must be empty initially, as members of struct | |
660 | * resource_entry objects. Callers of this routine should use | |
661 | * %acpi_dev_free_resource_list() to free that list. | |
662 | * | |
663 | * The number of resources in the output list is returned on success, | |
664 | * an error code reflecting the error condition is returned otherwise. | |
665 | */ | |
666 | int acpi_dev_get_dma_resources(struct acpi_device *adev, struct list_head *list) | |
667 | { | |
668 | return __acpi_dev_get_resources(adev, list, is_memory, NULL, | |
669 | METHOD_NAME__DMA); | |
670 | } | |
671 | EXPORT_SYMBOL_GPL(acpi_dev_get_dma_resources); | |
672 | ||
62d1141f JL |
673 | /** |
674 | * acpi_dev_filter_resource_type - Filter ACPI resource according to resource | |
675 | * types | |
676 | * @ares: Input ACPI resource object. | |
677 | * @types: Valid resource types of IORESOURCE_XXX | |
678 | * | |
2c62e849 | 679 | * This is a helper function to support acpi_dev_get_resources(), which filters |
62d1141f JL |
680 | * ACPI resource objects according to resource types. |
681 | */ | |
682 | int acpi_dev_filter_resource_type(struct acpi_resource *ares, | |
683 | unsigned long types) | |
684 | { | |
685 | unsigned long type = 0; | |
686 | ||
687 | switch (ares->type) { | |
688 | case ACPI_RESOURCE_TYPE_MEMORY24: | |
689 | case ACPI_RESOURCE_TYPE_MEMORY32: | |
690 | case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: | |
691 | type = IORESOURCE_MEM; | |
692 | break; | |
693 | case ACPI_RESOURCE_TYPE_IO: | |
694 | case ACPI_RESOURCE_TYPE_FIXED_IO: | |
695 | type = IORESOURCE_IO; | |
696 | break; | |
697 | case ACPI_RESOURCE_TYPE_IRQ: | |
698 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: | |
699 | type = IORESOURCE_IRQ; | |
700 | break; | |
701 | case ACPI_RESOURCE_TYPE_DMA: | |
702 | case ACPI_RESOURCE_TYPE_FIXED_DMA: | |
703 | type = IORESOURCE_DMA; | |
704 | break; | |
705 | case ACPI_RESOURCE_TYPE_GENERIC_REGISTER: | |
706 | type = IORESOURCE_REG; | |
707 | break; | |
708 | case ACPI_RESOURCE_TYPE_ADDRESS16: | |
709 | case ACPI_RESOURCE_TYPE_ADDRESS32: | |
710 | case ACPI_RESOURCE_TYPE_ADDRESS64: | |
711 | case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64: | |
712 | if (ares->data.address.resource_type == ACPI_MEMORY_RANGE) | |
713 | type = IORESOURCE_MEM; | |
714 | else if (ares->data.address.resource_type == ACPI_IO_RANGE) | |
715 | type = IORESOURCE_IO; | |
716 | else if (ares->data.address.resource_type == | |
717 | ACPI_BUS_NUMBER_RANGE) | |
718 | type = IORESOURCE_BUS; | |
719 | break; | |
720 | default: | |
721 | break; | |
722 | } | |
723 | ||
724 | return (type & types) ? 0 : 1; | |
725 | } | |
726 | EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type); | |
00710984 BH |
727 | |
728 | static int acpi_dev_consumes_res(struct acpi_device *adev, struct resource *res) | |
729 | { | |
730 | struct list_head resource_list; | |
731 | struct resource_entry *rentry; | |
732 | int ret, found = 0; | |
733 | ||
734 | INIT_LIST_HEAD(&resource_list); | |
735 | ret = acpi_dev_get_resources(adev, &resource_list, NULL, NULL); | |
736 | if (ret < 0) | |
737 | return 0; | |
738 | ||
739 | list_for_each_entry(rentry, &resource_list, node) { | |
740 | if (resource_contains(rentry->res, res)) { | |
741 | found = 1; | |
742 | break; | |
743 | } | |
744 | ||
745 | } | |
746 | ||
747 | acpi_dev_free_resource_list(&resource_list); | |
748 | return found; | |
749 | } | |
750 | ||
751 | static acpi_status acpi_res_consumer_cb(acpi_handle handle, u32 depth, | |
752 | void *context, void **ret) | |
753 | { | |
754 | struct resource *res = context; | |
755 | struct acpi_device **consumer = (struct acpi_device **) ret; | |
756 | struct acpi_device *adev; | |
757 | ||
758 | if (acpi_bus_get_device(handle, &adev)) | |
759 | return AE_OK; | |
760 | ||
761 | if (acpi_dev_consumes_res(adev, res)) { | |
762 | *consumer = adev; | |
763 | return AE_CTRL_TERMINATE; | |
764 | } | |
765 | ||
766 | return AE_OK; | |
767 | } | |
768 | ||
769 | /** | |
770 | * acpi_resource_consumer - Find the ACPI device that consumes @res. | |
771 | * @res: Resource to search for. | |
772 | * | |
773 | * Search the current resource settings (_CRS) of every ACPI device node | |
774 | * for @res. If we find an ACPI device whose _CRS includes @res, return | |
775 | * it. Otherwise, return NULL. | |
776 | */ | |
777 | struct acpi_device *acpi_resource_consumer(struct resource *res) | |
778 | { | |
779 | struct acpi_device *consumer = NULL; | |
780 | ||
781 | acpi_get_devices(NULL, acpi_res_consumer_cb, res, (void **) &consumer); | |
782 | return consumer; | |
783 | } |