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