]>
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 | * | |
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> | |
8e345c99 | 29 | #include <linux/slab.h> |
046d9ce6 RW |
30 | |
31 | #ifdef CONFIG_X86 | |
32 | #define valid_IRQ(i) (((i) != 0) && ((i) != 2)) | |
33 | #else | |
34 | #define valid_IRQ(i) (true) | |
35 | #endif | |
36 | ||
c420dbd1 | 37 | static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io) |
046d9ce6 | 38 | { |
c420dbd1 | 39 | u64 reslen = end - start + 1; |
046d9ce6 | 40 | |
c420dbd1 TG |
41 | /* |
42 | * CHECKME: len might be required to check versus a minimum | |
43 | * length as well. 1 for io is fine, but for memory it does | |
44 | * not make any sense at all. | |
45 | */ | |
46 | if (len && reslen && reslen == len && start <= end) | |
47 | return true; | |
48 | ||
49 | pr_info("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n", | |
50 | io ? "io" : "mem", start, end, len); | |
51 | ||
52 | return false; | |
53 | } | |
54 | ||
55 | static void acpi_dev_memresource_flags(struct resource *res, u64 len, | |
72e26b0d | 56 | u8 write_protect) |
c420dbd1 TG |
57 | { |
58 | res->flags = IORESOURCE_MEM; | |
59 | ||
60 | if (!acpi_dev_resource_len_valid(res->start, res->end, len, false)) | |
61 | res->flags |= IORESOURCE_DISABLED; | |
046d9ce6 RW |
62 | |
63 | if (write_protect == ACPI_READ_WRITE_MEMORY) | |
c420dbd1 | 64 | res->flags |= IORESOURCE_MEM_WRITEABLE; |
046d9ce6 RW |
65 | } |
66 | ||
67 | static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len, | |
68 | u8 write_protect) | |
69 | { | |
70 | res->start = start; | |
71 | res->end = start + len - 1; | |
72e26b0d | 72 | acpi_dev_memresource_flags(res, len, write_protect); |
046d9ce6 RW |
73 | } |
74 | ||
75 | /** | |
76 | * acpi_dev_resource_memory - Extract ACPI memory resource information. | |
77 | * @ares: Input ACPI resource object. | |
78 | * @res: Output generic resource object. | |
79 | * | |
80 | * Check if the given ACPI resource object represents a memory resource and | |
81 | * if that's the case, use the information in it to populate the generic | |
82 | * resource object pointed to by @res. | |
83 | */ | |
84 | bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res) | |
85 | { | |
86 | struct acpi_resource_memory24 *memory24; | |
87 | struct acpi_resource_memory32 *memory32; | |
88 | struct acpi_resource_fixed_memory32 *fixed_memory32; | |
89 | ||
90 | switch (ares->type) { | |
91 | case ACPI_RESOURCE_TYPE_MEMORY24: | |
92 | memory24 = &ares->data.memory24; | |
8515f936 JL |
93 | acpi_dev_get_memresource(res, memory24->minimum << 8, |
94 | memory24->address_length << 8, | |
046d9ce6 RW |
95 | memory24->write_protect); |
96 | break; | |
97 | case ACPI_RESOURCE_TYPE_MEMORY32: | |
98 | memory32 = &ares->data.memory32; | |
99 | acpi_dev_get_memresource(res, memory32->minimum, | |
100 | memory32->address_length, | |
101 | memory32->write_protect); | |
102 | break; | |
103 | case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: | |
104 | fixed_memory32 = &ares->data.fixed_memory32; | |
105 | acpi_dev_get_memresource(res, fixed_memory32->address, | |
106 | fixed_memory32->address_length, | |
107 | fixed_memory32->write_protect); | |
108 | break; | |
109 | default: | |
110 | return false; | |
111 | } | |
c420dbd1 TG |
112 | |
113 | return !(res->flags & IORESOURCE_DISABLED); | |
046d9ce6 RW |
114 | } |
115 | EXPORT_SYMBOL_GPL(acpi_dev_resource_memory); | |
116 | ||
1d0420f1 | 117 | static void acpi_dev_ioresource_flags(struct resource *res, u64 len, |
72e26b0d | 118 | u8 io_decode) |
046d9ce6 | 119 | { |
1d0420f1 | 120 | res->flags = IORESOURCE_IO; |
046d9ce6 | 121 | |
1d0420f1 TG |
122 | if (!acpi_dev_resource_len_valid(res->start, res->end, len, true)) |
123 | res->flags |= IORESOURCE_DISABLED; | |
124 | ||
125 | if (res->end >= 0x10003) | |
126 | res->flags |= IORESOURCE_DISABLED; | |
046d9ce6 | 127 | |
1d0420f1 TG |
128 | if (io_decode == ACPI_DECODE_16) |
129 | res->flags |= IORESOURCE_IO_16BIT_ADDR; | |
046d9ce6 RW |
130 | } |
131 | ||
132 | static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len, | |
133 | u8 io_decode) | |
134 | { | |
046d9ce6 | 135 | res->start = start; |
1d0420f1 | 136 | res->end = start + len - 1; |
72e26b0d | 137 | acpi_dev_ioresource_flags(res, len, io_decode); |
046d9ce6 RW |
138 | } |
139 | ||
140 | /** | |
141 | * acpi_dev_resource_io - Extract ACPI I/O resource information. | |
142 | * @ares: Input ACPI resource object. | |
143 | * @res: Output generic resource object. | |
144 | * | |
145 | * Check if the given ACPI resource object represents an I/O resource and | |
146 | * if that's the case, use the information in it to populate the generic | |
147 | * resource object pointed to by @res. | |
148 | */ | |
149 | bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res) | |
150 | { | |
151 | struct acpi_resource_io *io; | |
152 | struct acpi_resource_fixed_io *fixed_io; | |
153 | ||
154 | switch (ares->type) { | |
155 | case ACPI_RESOURCE_TYPE_IO: | |
156 | io = &ares->data.io; | |
157 | acpi_dev_get_ioresource(res, io->minimum, | |
158 | io->address_length, | |
159 | io->io_decode); | |
160 | break; | |
161 | case ACPI_RESOURCE_TYPE_FIXED_IO: | |
162 | fixed_io = &ares->data.fixed_io; | |
163 | acpi_dev_get_ioresource(res, fixed_io->address, | |
164 | fixed_io->address_length, | |
165 | ACPI_DECODE_10); | |
166 | break; | |
167 | default: | |
168 | return false; | |
169 | } | |
1d0420f1 TG |
170 | |
171 | return !(res->flags & IORESOURCE_DISABLED); | |
046d9ce6 RW |
172 | } |
173 | EXPORT_SYMBOL_GPL(acpi_dev_resource_io); | |
174 | ||
eb76d55e TG |
175 | static bool acpi_decode_space(struct resource *res, |
176 | struct acpi_resource_address *addr, | |
177 | struct acpi_address64_attribute *attr) | |
178 | { | |
179 | u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16; | |
eb76d55e TG |
180 | bool wp = addr->info.mem.write_protect; |
181 | u64 len = attr->address_length; | |
182 | ||
183 | res->start = attr->minimum; | |
184 | res->end = attr->maximum; | |
185 | ||
186 | switch (addr->resource_type) { | |
187 | case ACPI_MEMORY_RANGE: | |
72e26b0d | 188 | acpi_dev_memresource_flags(res, len, wp); |
eb76d55e TG |
189 | break; |
190 | case ACPI_IO_RANGE: | |
72e26b0d | 191 | acpi_dev_ioresource_flags(res, len, iodec); |
eb76d55e TG |
192 | break; |
193 | case ACPI_BUS_NUMBER_RANGE: | |
194 | res->flags = IORESOURCE_BUS; | |
195 | break; | |
196 | default: | |
197 | return false; | |
198 | } | |
199 | ||
72e26b0d TG |
200 | if (addr->producer_consumer == ACPI_PRODUCER) |
201 | res->flags |= IORESOURCE_WINDOW; | |
fcb29bbc TG |
202 | |
203 | if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY) | |
204 | res->flags |= IORESOURCE_PREFETCH; | |
72e26b0d | 205 | |
eb76d55e TG |
206 | return !(res->flags & IORESOURCE_DISABLED); |
207 | } | |
208 | ||
046d9ce6 RW |
209 | /** |
210 | * acpi_dev_resource_address_space - Extract ACPI address space information. | |
211 | * @ares: Input ACPI resource object. | |
212 | * @res: Output generic resource object. | |
213 | * | |
214 | * Check if the given ACPI resource object represents an address space resource | |
215 | * and if that's the case, use the information in it to populate the generic | |
216 | * resource object pointed to by @res. | |
217 | */ | |
218 | bool acpi_dev_resource_address_space(struct acpi_resource *ares, | |
219 | struct resource *res) | |
220 | { | |
046d9ce6 | 221 | struct acpi_resource_address64 addr; |
046d9ce6 | 222 | |
eb76d55e | 223 | if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr))) |
6658c739 | 224 | return false; |
046d9ce6 | 225 | |
eb76d55e TG |
226 | return acpi_decode_space(res, (struct acpi_resource_address *)&addr, |
227 | &addr.address); | |
046d9ce6 RW |
228 | } |
229 | EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space); | |
230 | ||
231 | /** | |
232 | * acpi_dev_resource_ext_address_space - Extract ACPI address space information. | |
233 | * @ares: Input ACPI resource object. | |
234 | * @res: Output generic resource object. | |
235 | * | |
236 | * Check if the given ACPI resource object represents an extended address space | |
237 | * resource and if that's the case, use the information in it to populate the | |
238 | * generic resource object pointed to by @res. | |
239 | */ | |
240 | bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares, | |
241 | struct resource *res) | |
242 | { | |
243 | struct acpi_resource_extended_address64 *ext_addr; | |
046d9ce6 RW |
244 | |
245 | if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) | |
246 | return false; | |
247 | ||
248 | ext_addr = &ares->data.ext_address64; | |
249 | ||
eb76d55e TG |
250 | return acpi_decode_space(res, (struct acpi_resource_address *)ext_addr, |
251 | &ext_addr->address); | |
046d9ce6 RW |
252 | } |
253 | EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space); | |
254 | ||
255 | /** | |
256 | * acpi_dev_irq_flags - Determine IRQ resource flags. | |
257 | * @triggering: Triggering type as provided by ACPI. | |
258 | * @polarity: Interrupt polarity as provided by ACPI. | |
259 | * @shareable: Whether or not the interrupt is shareable. | |
260 | */ | |
261 | unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable) | |
262 | { | |
263 | unsigned long flags; | |
264 | ||
265 | if (triggering == ACPI_LEVEL_SENSITIVE) | |
266 | flags = polarity == ACPI_ACTIVE_LOW ? | |
267 | IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL; | |
268 | else | |
269 | flags = polarity == ACPI_ACTIVE_LOW ? | |
270 | IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE; | |
271 | ||
272 | if (shareable == ACPI_SHARED) | |
273 | flags |= IORESOURCE_IRQ_SHAREABLE; | |
274 | ||
275 | return flags | IORESOURCE_IRQ; | |
276 | } | |
277 | EXPORT_SYMBOL_GPL(acpi_dev_irq_flags); | |
278 | ||
279 | static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi) | |
280 | { | |
281 | res->start = gsi; | |
282 | res->end = gsi; | |
283 | res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED; | |
284 | } | |
285 | ||
286 | static void acpi_dev_get_irqresource(struct resource *res, u32 gsi, | |
204ebc0a MW |
287 | u8 triggering, u8 polarity, u8 shareable, |
288 | bool legacy) | |
046d9ce6 RW |
289 | { |
290 | int irq, p, t; | |
291 | ||
292 | if (!valid_IRQ(gsi)) { | |
293 | acpi_dev_irqresource_disabled(res, gsi); | |
294 | return; | |
295 | } | |
296 | ||
297 | /* | |
298 | * In IO-APIC mode, use overrided attribute. Two reasons: | |
299 | * 1. BIOS bug in DSDT | |
300 | * 2. BIOS uses IO-APIC mode Interrupt Source Override | |
204ebc0a MW |
301 | * |
302 | * We do this only if we are dealing with IRQ() or IRQNoFlags() | |
303 | * resource (the legacy ISA resources). With modern ACPI 5 devices | |
304 | * using extended IRQ descriptors we take the IRQ configuration | |
305 | * from _CRS directly. | |
046d9ce6 | 306 | */ |
204ebc0a | 307 | if (legacy && !acpi_get_override_irq(gsi, &t, &p)) { |
046d9ce6 RW |
308 | u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; |
309 | u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; | |
310 | ||
311 | if (triggering != trig || polarity != pol) { | |
312 | pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi, | |
204ebc0a | 313 | t ? "level" : "edge", p ? "low" : "high"); |
046d9ce6 RW |
314 | triggering = trig; |
315 | polarity = pol; | |
316 | } | |
317 | } | |
318 | ||
319 | res->flags = acpi_dev_irq_flags(triggering, polarity, shareable); | |
320 | irq = acpi_register_gsi(NULL, gsi, triggering, polarity); | |
321 | if (irq >= 0) { | |
322 | res->start = irq; | |
323 | res->end = irq; | |
324 | } else { | |
325 | acpi_dev_irqresource_disabled(res, gsi); | |
326 | } | |
327 | } | |
328 | ||
329 | /** | |
330 | * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information. | |
331 | * @ares: Input ACPI resource object. | |
332 | * @index: Index into the array of GSIs represented by the resource. | |
333 | * @res: Output generic resource object. | |
334 | * | |
335 | * Check if the given ACPI resource object represents an interrupt resource | |
336 | * and @index does not exceed the resource's interrupt count (true is returned | |
337 | * in that case regardless of the results of the other checks)). If that's the | |
338 | * case, register the GSI corresponding to @index from the array of interrupts | |
339 | * represented by the resource and populate the generic resource object pointed | |
340 | * to by @res accordingly. If the registration of the GSI is not successful, | |
341 | * IORESOURCE_DISABLED will be set it that object's flags. | |
342 | */ | |
343 | bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index, | |
344 | struct resource *res) | |
345 | { | |
346 | struct acpi_resource_irq *irq; | |
347 | struct acpi_resource_extended_irq *ext_irq; | |
348 | ||
349 | switch (ares->type) { | |
350 | case ACPI_RESOURCE_TYPE_IRQ: | |
351 | /* | |
352 | * Per spec, only one interrupt per descriptor is allowed in | |
353 | * _CRS, but some firmware violates this, so parse them all. | |
354 | */ | |
355 | irq = &ares->data.irq; | |
356 | if (index >= irq->interrupt_count) { | |
357 | acpi_dev_irqresource_disabled(res, 0); | |
358 | return false; | |
359 | } | |
360 | acpi_dev_get_irqresource(res, irq->interrupts[index], | |
361 | irq->triggering, irq->polarity, | |
204ebc0a | 362 | irq->sharable, true); |
046d9ce6 RW |
363 | break; |
364 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: | |
365 | ext_irq = &ares->data.extended_irq; | |
366 | if (index >= ext_irq->interrupt_count) { | |
367 | acpi_dev_irqresource_disabled(res, 0); | |
368 | return false; | |
369 | } | |
370 | acpi_dev_get_irqresource(res, ext_irq->interrupts[index], | |
371 | ext_irq->triggering, ext_irq->polarity, | |
204ebc0a | 372 | ext_irq->sharable, false); |
046d9ce6 RW |
373 | break; |
374 | default: | |
375 | return false; | |
376 | } | |
377 | ||
378 | return true; | |
379 | } | |
380 | EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt); | |
8e345c99 RW |
381 | |
382 | /** | |
383 | * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources(). | |
384 | * @list: The head of the resource list to free. | |
385 | */ | |
386 | void acpi_dev_free_resource_list(struct list_head *list) | |
387 | { | |
388 | struct resource_list_entry *rentry, *re; | |
389 | ||
390 | list_for_each_entry_safe(rentry, re, list, node) { | |
391 | list_del(&rentry->node); | |
392 | kfree(rentry); | |
393 | } | |
394 | } | |
395 | EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list); | |
396 | ||
397 | struct res_proc_context { | |
398 | struct list_head *list; | |
399 | int (*preproc)(struct acpi_resource *, void *); | |
400 | void *preproc_data; | |
401 | int count; | |
402 | int error; | |
403 | }; | |
404 | ||
405 | static acpi_status acpi_dev_new_resource_entry(struct resource *r, | |
406 | struct res_proc_context *c) | |
407 | { | |
408 | struct resource_list_entry *rentry; | |
409 | ||
410 | rentry = kmalloc(sizeof(*rentry), GFP_KERNEL); | |
411 | if (!rentry) { | |
412 | c->error = -ENOMEM; | |
413 | return AE_NO_MEMORY; | |
414 | } | |
8e345c99 RW |
415 | rentry->res = *r; |
416 | list_add_tail(&rentry->node, c->list); | |
417 | c->count++; | |
418 | return AE_OK; | |
419 | } | |
420 | ||
421 | static acpi_status acpi_dev_process_resource(struct acpi_resource *ares, | |
422 | void *context) | |
423 | { | |
424 | struct res_proc_context *c = context; | |
425 | struct resource r; | |
426 | int i; | |
427 | ||
428 | if (c->preproc) { | |
429 | int ret; | |
430 | ||
431 | ret = c->preproc(ares, c->preproc_data); | |
432 | if (ret < 0) { | |
433 | c->error = ret; | |
8a66790b | 434 | return AE_CTRL_TERMINATE; |
8e345c99 RW |
435 | } else if (ret > 0) { |
436 | return AE_OK; | |
437 | } | |
438 | } | |
439 | ||
440 | memset(&r, 0, sizeof(r)); | |
441 | ||
442 | if (acpi_dev_resource_memory(ares, &r) | |
443 | || acpi_dev_resource_io(ares, &r) | |
444 | || acpi_dev_resource_address_space(ares, &r) | |
445 | || acpi_dev_resource_ext_address_space(ares, &r)) | |
446 | return acpi_dev_new_resource_entry(&r, c); | |
447 | ||
448 | for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) { | |
449 | acpi_status status; | |
450 | ||
451 | status = acpi_dev_new_resource_entry(&r, c); | |
452 | if (ACPI_FAILURE(status)) | |
453 | return status; | |
454 | } | |
455 | ||
456 | return AE_OK; | |
457 | } | |
458 | ||
459 | /** | |
460 | * acpi_dev_get_resources - Get current resources of a device. | |
461 | * @adev: ACPI device node to get the resources for. | |
462 | * @list: Head of the resultant list of resources (must be empty). | |
463 | * @preproc: The caller's preprocessing routine. | |
464 | * @preproc_data: Pointer passed to the caller's preprocessing routine. | |
465 | * | |
466 | * Evaluate the _CRS method for the given device node and process its output by | |
467 | * (1) executing the @preproc() rountine provided by the caller, passing the | |
468 | * resource pointer and @preproc_data to it as arguments, for each ACPI resource | |
469 | * returned and (2) converting all of the returned ACPI resources into struct | |
470 | * resource objects if possible. If the return value of @preproc() in step (1) | |
471 | * is different from 0, step (2) is not applied to the given ACPI resource and | |
472 | * if that value is negative, the whole processing is aborted and that value is | |
473 | * returned as the final error code. | |
474 | * | |
475 | * The resultant struct resource objects are put on the list pointed to by | |
476 | * @list, that must be empty initially, as members of struct resource_list_entry | |
477 | * objects. Callers of this routine should use %acpi_dev_free_resource_list() to | |
478 | * free that list. | |
479 | * | |
480 | * The number of resources in the output list is returned on success, an error | |
481 | * code reflecting the error condition is returned otherwise. | |
482 | */ | |
483 | int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list, | |
484 | int (*preproc)(struct acpi_resource *, void *), | |
485 | void *preproc_data) | |
486 | { | |
487 | struct res_proc_context c; | |
8e345c99 RW |
488 | acpi_status status; |
489 | ||
490 | if (!adev || !adev->handle || !list_empty(list)) | |
491 | return -EINVAL; | |
492 | ||
952c63e9 | 493 | if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) |
8e345c99 RW |
494 | return 0; |
495 | ||
496 | c.list = list; | |
497 | c.preproc = preproc; | |
498 | c.preproc_data = preproc_data; | |
499 | c.count = 0; | |
500 | c.error = 0; | |
501 | status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS, | |
502 | acpi_dev_process_resource, &c); | |
503 | if (ACPI_FAILURE(status)) { | |
504 | acpi_dev_free_resource_list(list); | |
505 | return c.error ? c.error : -EIO; | |
506 | } | |
507 | ||
508 | return c.count; | |
509 | } | |
510 | EXPORT_SYMBOL_GPL(acpi_dev_get_resources); |