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; | |
93 | acpi_dev_get_memresource(res, memory24->minimum, | |
94 | memory24->address_length, | |
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; | |
202 | ||
eb76d55e TG |
203 | return !(res->flags & IORESOURCE_DISABLED); |
204 | } | |
205 | ||
046d9ce6 RW |
206 | /** |
207 | * acpi_dev_resource_address_space - Extract ACPI address space information. | |
208 | * @ares: Input ACPI resource object. | |
209 | * @res: Output generic resource object. | |
210 | * | |
211 | * Check if the given ACPI resource object represents an address space resource | |
212 | * and if that's the case, use the information in it to populate the generic | |
213 | * resource object pointed to by @res. | |
214 | */ | |
215 | bool acpi_dev_resource_address_space(struct acpi_resource *ares, | |
216 | struct resource *res) | |
217 | { | |
046d9ce6 | 218 | struct acpi_resource_address64 addr; |
046d9ce6 | 219 | |
eb76d55e | 220 | if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr))) |
6658c739 | 221 | return false; |
046d9ce6 | 222 | |
eb76d55e TG |
223 | return acpi_decode_space(res, (struct acpi_resource_address *)&addr, |
224 | &addr.address); | |
046d9ce6 RW |
225 | } |
226 | EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space); | |
227 | ||
228 | /** | |
229 | * acpi_dev_resource_ext_address_space - Extract ACPI address space information. | |
230 | * @ares: Input ACPI resource object. | |
231 | * @res: Output generic resource object. | |
232 | * | |
233 | * Check if the given ACPI resource object represents an extended address space | |
234 | * resource and if that's the case, use the information in it to populate the | |
235 | * generic resource object pointed to by @res. | |
236 | */ | |
237 | bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares, | |
238 | struct resource *res) | |
239 | { | |
240 | struct acpi_resource_extended_address64 *ext_addr; | |
046d9ce6 RW |
241 | |
242 | if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) | |
243 | return false; | |
244 | ||
245 | ext_addr = &ares->data.ext_address64; | |
246 | ||
eb76d55e TG |
247 | return acpi_decode_space(res, (struct acpi_resource_address *)ext_addr, |
248 | &ext_addr->address); | |
046d9ce6 RW |
249 | } |
250 | EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space); | |
251 | ||
252 | /** | |
253 | * acpi_dev_irq_flags - Determine IRQ resource flags. | |
254 | * @triggering: Triggering type as provided by ACPI. | |
255 | * @polarity: Interrupt polarity as provided by ACPI. | |
256 | * @shareable: Whether or not the interrupt is shareable. | |
257 | */ | |
258 | unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable) | |
259 | { | |
260 | unsigned long flags; | |
261 | ||
262 | if (triggering == ACPI_LEVEL_SENSITIVE) | |
263 | flags = polarity == ACPI_ACTIVE_LOW ? | |
264 | IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL; | |
265 | else | |
266 | flags = polarity == ACPI_ACTIVE_LOW ? | |
267 | IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE; | |
268 | ||
269 | if (shareable == ACPI_SHARED) | |
270 | flags |= IORESOURCE_IRQ_SHAREABLE; | |
271 | ||
272 | return flags | IORESOURCE_IRQ; | |
273 | } | |
274 | EXPORT_SYMBOL_GPL(acpi_dev_irq_flags); | |
275 | ||
276 | static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi) | |
277 | { | |
278 | res->start = gsi; | |
279 | res->end = gsi; | |
280 | res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED; | |
281 | } | |
282 | ||
283 | static void acpi_dev_get_irqresource(struct resource *res, u32 gsi, | |
204ebc0a MW |
284 | u8 triggering, u8 polarity, u8 shareable, |
285 | bool legacy) | |
046d9ce6 RW |
286 | { |
287 | int irq, p, t; | |
288 | ||
289 | if (!valid_IRQ(gsi)) { | |
290 | acpi_dev_irqresource_disabled(res, gsi); | |
291 | return; | |
292 | } | |
293 | ||
294 | /* | |
295 | * In IO-APIC mode, use overrided attribute. Two reasons: | |
296 | * 1. BIOS bug in DSDT | |
297 | * 2. BIOS uses IO-APIC mode Interrupt Source Override | |
204ebc0a MW |
298 | * |
299 | * We do this only if we are dealing with IRQ() or IRQNoFlags() | |
300 | * resource (the legacy ISA resources). With modern ACPI 5 devices | |
301 | * using extended IRQ descriptors we take the IRQ configuration | |
302 | * from _CRS directly. | |
046d9ce6 | 303 | */ |
204ebc0a | 304 | if (legacy && !acpi_get_override_irq(gsi, &t, &p)) { |
046d9ce6 RW |
305 | u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; |
306 | u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; | |
307 | ||
308 | if (triggering != trig || polarity != pol) { | |
309 | pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi, | |
204ebc0a | 310 | t ? "level" : "edge", p ? "low" : "high"); |
046d9ce6 RW |
311 | triggering = trig; |
312 | polarity = pol; | |
313 | } | |
314 | } | |
315 | ||
316 | res->flags = acpi_dev_irq_flags(triggering, polarity, shareable); | |
317 | irq = acpi_register_gsi(NULL, gsi, triggering, polarity); | |
318 | if (irq >= 0) { | |
319 | res->start = irq; | |
320 | res->end = irq; | |
321 | } else { | |
322 | acpi_dev_irqresource_disabled(res, gsi); | |
323 | } | |
324 | } | |
325 | ||
326 | /** | |
327 | * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information. | |
328 | * @ares: Input ACPI resource object. | |
329 | * @index: Index into the array of GSIs represented by the resource. | |
330 | * @res: Output generic resource object. | |
331 | * | |
332 | * Check if the given ACPI resource object represents an interrupt resource | |
333 | * and @index does not exceed the resource's interrupt count (true is returned | |
334 | * in that case regardless of the results of the other checks)). If that's the | |
335 | * case, register the GSI corresponding to @index from the array of interrupts | |
336 | * represented by the resource and populate the generic resource object pointed | |
337 | * to by @res accordingly. If the registration of the GSI is not successful, | |
338 | * IORESOURCE_DISABLED will be set it that object's flags. | |
339 | */ | |
340 | bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index, | |
341 | struct resource *res) | |
342 | { | |
343 | struct acpi_resource_irq *irq; | |
344 | struct acpi_resource_extended_irq *ext_irq; | |
345 | ||
346 | switch (ares->type) { | |
347 | case ACPI_RESOURCE_TYPE_IRQ: | |
348 | /* | |
349 | * Per spec, only one interrupt per descriptor is allowed in | |
350 | * _CRS, but some firmware violates this, so parse them all. | |
351 | */ | |
352 | irq = &ares->data.irq; | |
353 | if (index >= irq->interrupt_count) { | |
354 | acpi_dev_irqresource_disabled(res, 0); | |
355 | return false; | |
356 | } | |
357 | acpi_dev_get_irqresource(res, irq->interrupts[index], | |
358 | irq->triggering, irq->polarity, | |
204ebc0a | 359 | irq->sharable, true); |
046d9ce6 RW |
360 | break; |
361 | case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: | |
362 | ext_irq = &ares->data.extended_irq; | |
363 | if (index >= ext_irq->interrupt_count) { | |
364 | acpi_dev_irqresource_disabled(res, 0); | |
365 | return false; | |
366 | } | |
367 | acpi_dev_get_irqresource(res, ext_irq->interrupts[index], | |
368 | ext_irq->triggering, ext_irq->polarity, | |
204ebc0a | 369 | ext_irq->sharable, false); |
046d9ce6 RW |
370 | break; |
371 | default: | |
372 | return false; | |
373 | } | |
374 | ||
375 | return true; | |
376 | } | |
377 | EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt); | |
8e345c99 RW |
378 | |
379 | /** | |
380 | * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources(). | |
381 | * @list: The head of the resource list to free. | |
382 | */ | |
383 | void acpi_dev_free_resource_list(struct list_head *list) | |
384 | { | |
385 | struct resource_list_entry *rentry, *re; | |
386 | ||
387 | list_for_each_entry_safe(rentry, re, list, node) { | |
388 | list_del(&rentry->node); | |
389 | kfree(rentry); | |
390 | } | |
391 | } | |
392 | EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list); | |
393 | ||
394 | struct res_proc_context { | |
395 | struct list_head *list; | |
396 | int (*preproc)(struct acpi_resource *, void *); | |
397 | void *preproc_data; | |
398 | int count; | |
399 | int error; | |
400 | }; | |
401 | ||
402 | static acpi_status acpi_dev_new_resource_entry(struct resource *r, | |
403 | struct res_proc_context *c) | |
404 | { | |
405 | struct resource_list_entry *rentry; | |
406 | ||
407 | rentry = kmalloc(sizeof(*rentry), GFP_KERNEL); | |
408 | if (!rentry) { | |
409 | c->error = -ENOMEM; | |
410 | return AE_NO_MEMORY; | |
411 | } | |
8e345c99 RW |
412 | rentry->res = *r; |
413 | list_add_tail(&rentry->node, c->list); | |
414 | c->count++; | |
415 | return AE_OK; | |
416 | } | |
417 | ||
418 | static acpi_status acpi_dev_process_resource(struct acpi_resource *ares, | |
419 | void *context) | |
420 | { | |
421 | struct res_proc_context *c = context; | |
422 | struct resource r; | |
423 | int i; | |
424 | ||
425 | if (c->preproc) { | |
426 | int ret; | |
427 | ||
428 | ret = c->preproc(ares, c->preproc_data); | |
429 | if (ret < 0) { | |
430 | c->error = ret; | |
8a66790b | 431 | return AE_CTRL_TERMINATE; |
8e345c99 RW |
432 | } else if (ret > 0) { |
433 | return AE_OK; | |
434 | } | |
435 | } | |
436 | ||
437 | memset(&r, 0, sizeof(r)); | |
438 | ||
439 | if (acpi_dev_resource_memory(ares, &r) | |
440 | || acpi_dev_resource_io(ares, &r) | |
441 | || acpi_dev_resource_address_space(ares, &r) | |
442 | || acpi_dev_resource_ext_address_space(ares, &r)) | |
443 | return acpi_dev_new_resource_entry(&r, c); | |
444 | ||
445 | for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) { | |
446 | acpi_status status; | |
447 | ||
448 | status = acpi_dev_new_resource_entry(&r, c); | |
449 | if (ACPI_FAILURE(status)) | |
450 | return status; | |
451 | } | |
452 | ||
453 | return AE_OK; | |
454 | } | |
455 | ||
456 | /** | |
457 | * acpi_dev_get_resources - Get current resources of a device. | |
458 | * @adev: ACPI device node to get the resources for. | |
459 | * @list: Head of the resultant list of resources (must be empty). | |
460 | * @preproc: The caller's preprocessing routine. | |
461 | * @preproc_data: Pointer passed to the caller's preprocessing routine. | |
462 | * | |
463 | * Evaluate the _CRS method for the given device node and process its output by | |
464 | * (1) executing the @preproc() rountine provided by the caller, passing the | |
465 | * resource pointer and @preproc_data to it as arguments, for each ACPI resource | |
466 | * returned and (2) converting all of the returned ACPI resources into struct | |
467 | * resource objects if possible. If the return value of @preproc() in step (1) | |
468 | * is different from 0, step (2) is not applied to the given ACPI resource and | |
469 | * if that value is negative, the whole processing is aborted and that value is | |
470 | * returned as the final error code. | |
471 | * | |
472 | * The resultant struct resource objects are put on the list pointed to by | |
473 | * @list, that must be empty initially, as members of struct resource_list_entry | |
474 | * objects. Callers of this routine should use %acpi_dev_free_resource_list() to | |
475 | * free that list. | |
476 | * | |
477 | * The number of resources in the output list is returned on success, an error | |
478 | * code reflecting the error condition is returned otherwise. | |
479 | */ | |
480 | int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list, | |
481 | int (*preproc)(struct acpi_resource *, void *), | |
482 | void *preproc_data) | |
483 | { | |
484 | struct res_proc_context c; | |
8e345c99 RW |
485 | acpi_status status; |
486 | ||
487 | if (!adev || !adev->handle || !list_empty(list)) | |
488 | return -EINVAL; | |
489 | ||
952c63e9 | 490 | if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) |
8e345c99 RW |
491 | return 0; |
492 | ||
493 | c.list = list; | |
494 | c.preproc = preproc; | |
495 | c.preproc_data = preproc_data; | |
496 | c.count = 0; | |
497 | c.error = 0; | |
498 | status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS, | |
499 | acpi_dev_process_resource, &c); | |
500 | if (ACPI_FAILURE(status)) { | |
501 | acpi_dev_free_resource_list(list); | |
502 | return c.error ? c.error : -EIO; | |
503 | } | |
504 | ||
505 | return c.count; | |
506 | } | |
507 | EXPORT_SYMBOL_GPL(acpi_dev_get_resources); |