Linux 4.1.16
[linux/fpc-iii.git] / drivers / acpi / resource.c
blobf1c966e05078426455227d8f582a7ddd0f211cb2
1 /*
2 * drivers/acpi/resource.c - ACPI device resources interpretation.
4 * Copyright (C) 2012, Intel Corp.
5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/acpi.h>
26 #include <linux/device.h>
27 #include <linux/export.h>
28 #include <linux/ioport.h>
29 #include <linux/slab.h>
31 #ifdef CONFIG_X86
32 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
33 #else
34 #define valid_IRQ(i) (true)
35 #endif
37 static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
39 u64 reslen = end - start + 1;
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 * Note: some BIOSes report incorrect length for ACPI address space
46 * descriptor, so remove check of 'reslen == len' to avoid regression.
48 if (len && reslen && start <= end)
49 return true;
51 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
52 io ? "io" : "mem", start, end, len);
54 return false;
57 static void acpi_dev_memresource_flags(struct resource *res, u64 len,
58 u8 write_protect)
60 res->flags = IORESOURCE_MEM;
62 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
63 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
65 if (write_protect == ACPI_READ_WRITE_MEMORY)
66 res->flags |= IORESOURCE_MEM_WRITEABLE;
69 static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
70 u8 write_protect)
72 res->start = start;
73 res->end = start + len - 1;
74 acpi_dev_memresource_flags(res, len, write_protect);
77 /**
78 * acpi_dev_resource_memory - Extract ACPI memory resource information.
79 * @ares: Input ACPI resource object.
80 * @res: Output generic resource object.
82 * Check if the given ACPI resource object represents a memory resource and
83 * if that's the case, use the information in it to populate the generic
84 * resource object pointed to by @res.
86 * Return:
87 * 1) false with res->flags setting to zero: not the expected resource type
88 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
89 * 3) true: valid assigned resource
91 bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
93 struct acpi_resource_memory24 *memory24;
94 struct acpi_resource_memory32 *memory32;
95 struct acpi_resource_fixed_memory32 *fixed_memory32;
97 switch (ares->type) {
98 case ACPI_RESOURCE_TYPE_MEMORY24:
99 memory24 = &ares->data.memory24;
100 acpi_dev_get_memresource(res, memory24->minimum << 8,
101 memory24->address_length << 8,
102 memory24->write_protect);
103 break;
104 case ACPI_RESOURCE_TYPE_MEMORY32:
105 memory32 = &ares->data.memory32;
106 acpi_dev_get_memresource(res, memory32->minimum,
107 memory32->address_length,
108 memory32->write_protect);
109 break;
110 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
111 fixed_memory32 = &ares->data.fixed_memory32;
112 acpi_dev_get_memresource(res, fixed_memory32->address,
113 fixed_memory32->address_length,
114 fixed_memory32->write_protect);
115 break;
116 default:
117 res->flags = 0;
118 return false;
121 return !(res->flags & IORESOURCE_DISABLED);
123 EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
125 static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
126 u8 io_decode)
128 res->flags = IORESOURCE_IO;
130 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
131 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
133 if (res->end >= 0x10003)
134 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
136 if (io_decode == ACPI_DECODE_16)
137 res->flags |= IORESOURCE_IO_16BIT_ADDR;
140 static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
141 u8 io_decode)
143 res->start = start;
144 res->end = start + len - 1;
145 acpi_dev_ioresource_flags(res, len, io_decode);
149 * acpi_dev_resource_io - Extract ACPI I/O resource information.
150 * @ares: Input ACPI resource object.
151 * @res: Output generic resource object.
153 * Check if the given ACPI resource object represents an I/O resource and
154 * if that's the case, use the information in it to populate the generic
155 * resource object pointed to by @res.
157 * Return:
158 * 1) false with res->flags setting to zero: not the expected resource type
159 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
160 * 3) true: valid assigned resource
162 bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
164 struct acpi_resource_io *io;
165 struct acpi_resource_fixed_io *fixed_io;
167 switch (ares->type) {
168 case ACPI_RESOURCE_TYPE_IO:
169 io = &ares->data.io;
170 acpi_dev_get_ioresource(res, io->minimum,
171 io->address_length,
172 io->io_decode);
173 break;
174 case ACPI_RESOURCE_TYPE_FIXED_IO:
175 fixed_io = &ares->data.fixed_io;
176 acpi_dev_get_ioresource(res, fixed_io->address,
177 fixed_io->address_length,
178 ACPI_DECODE_10);
179 break;
180 default:
181 res->flags = 0;
182 return false;
185 return !(res->flags & IORESOURCE_DISABLED);
187 EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
189 static bool acpi_decode_space(struct resource_win *win,
190 struct acpi_resource_address *addr,
191 struct acpi_address64_attribute *attr)
193 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
194 bool wp = addr->info.mem.write_protect;
195 u64 len = attr->address_length;
196 u64 start, end, offset = 0;
197 struct resource *res = &win->res;
200 * Filter out invalid descriptor according to ACPI Spec 5.0, section
201 * 6.4.3.5 Address Space Resource Descriptors.
203 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
204 (addr->min_address_fixed && addr->max_address_fixed && !len))
205 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
206 addr->min_address_fixed, addr->max_address_fixed, len);
209 * For bridges that translate addresses across the bridge,
210 * translation_offset is the offset that must be added to the
211 * address on the secondary side to obtain the address on the
212 * primary side. Non-bridge devices must list 0 for all Address
213 * Translation offset bits.
215 if (addr->producer_consumer == ACPI_PRODUCER)
216 offset = attr->translation_offset;
217 else if (attr->translation_offset)
218 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
219 attr->translation_offset);
220 start = attr->minimum + offset;
221 end = attr->maximum + offset;
223 win->offset = offset;
224 res->start = start;
225 res->end = end;
226 if (sizeof(resource_size_t) < sizeof(u64) &&
227 (offset != win->offset || start != res->start || end != res->end)) {
228 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
229 attr->minimum, attr->maximum);
230 return false;
233 switch (addr->resource_type) {
234 case ACPI_MEMORY_RANGE:
235 acpi_dev_memresource_flags(res, len, wp);
236 break;
237 case ACPI_IO_RANGE:
238 acpi_dev_ioresource_flags(res, len, iodec);
239 break;
240 case ACPI_BUS_NUMBER_RANGE:
241 res->flags = IORESOURCE_BUS;
242 break;
243 default:
244 return false;
247 if (addr->producer_consumer == ACPI_PRODUCER)
248 res->flags |= IORESOURCE_WINDOW;
250 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
251 res->flags |= IORESOURCE_PREFETCH;
253 return !(res->flags & IORESOURCE_DISABLED);
257 * acpi_dev_resource_address_space - Extract ACPI address space information.
258 * @ares: Input ACPI resource object.
259 * @win: Output generic resource object.
261 * Check if the given ACPI resource object represents an address space resource
262 * and if that's the case, use the information in it to populate the generic
263 * resource object pointed to by @win.
265 * Return:
266 * 1) false with win->res.flags setting to zero: not the expected resource type
267 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
268 * resource
269 * 3) true: valid assigned resource
271 bool acpi_dev_resource_address_space(struct acpi_resource *ares,
272 struct resource_win *win)
274 struct acpi_resource_address64 addr;
276 win->res.flags = 0;
277 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
278 return false;
280 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
281 &addr.address);
283 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
286 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
287 * @ares: Input ACPI resource object.
288 * @win: Output generic resource object.
290 * Check if the given ACPI resource object represents an extended address space
291 * resource and if that's the case, use the information in it to populate the
292 * generic resource object pointed to by @win.
294 * Return:
295 * 1) false with win->res.flags setting to zero: not the expected resource type
296 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
297 * resource
298 * 3) true: valid assigned resource
300 bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
301 struct resource_win *win)
303 struct acpi_resource_extended_address64 *ext_addr;
305 win->res.flags = 0;
306 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
307 return false;
309 ext_addr = &ares->data.ext_address64;
311 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
312 &ext_addr->address);
314 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
317 * acpi_dev_irq_flags - Determine IRQ resource flags.
318 * @triggering: Triggering type as provided by ACPI.
319 * @polarity: Interrupt polarity as provided by ACPI.
320 * @shareable: Whether or not the interrupt is shareable.
322 unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
324 unsigned long flags;
326 if (triggering == ACPI_LEVEL_SENSITIVE)
327 flags = polarity == ACPI_ACTIVE_LOW ?
328 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
329 else
330 flags = polarity == ACPI_ACTIVE_LOW ?
331 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
333 if (shareable == ACPI_SHARED)
334 flags |= IORESOURCE_IRQ_SHAREABLE;
336 return flags | IORESOURCE_IRQ;
338 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
340 static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
342 res->start = gsi;
343 res->end = gsi;
344 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
347 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
348 u8 triggering, u8 polarity, u8 shareable,
349 bool legacy)
351 int irq, p, t;
353 if (!valid_IRQ(gsi)) {
354 acpi_dev_irqresource_disabled(res, gsi);
355 return;
359 * In IO-APIC mode, use overrided attribute. Two reasons:
360 * 1. BIOS bug in DSDT
361 * 2. BIOS uses IO-APIC mode Interrupt Source Override
363 * We do this only if we are dealing with IRQ() or IRQNoFlags()
364 * resource (the legacy ISA resources). With modern ACPI 5 devices
365 * using extended IRQ descriptors we take the IRQ configuration
366 * from _CRS directly.
368 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
369 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
370 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
372 if (triggering != trig || polarity != pol) {
373 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
374 t ? "level" : "edge", p ? "low" : "high");
375 triggering = trig;
376 polarity = pol;
380 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
381 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
382 if (irq >= 0) {
383 res->start = irq;
384 res->end = irq;
385 } else {
386 acpi_dev_irqresource_disabled(res, gsi);
391 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
392 * @ares: Input ACPI resource object.
393 * @index: Index into the array of GSIs represented by the resource.
394 * @res: Output generic resource object.
396 * Check if the given ACPI resource object represents an interrupt resource
397 * and @index does not exceed the resource's interrupt count (true is returned
398 * in that case regardless of the results of the other checks)). If that's the
399 * case, register the GSI corresponding to @index from the array of interrupts
400 * represented by the resource and populate the generic resource object pointed
401 * to by @res accordingly. If the registration of the GSI is not successful,
402 * IORESOURCE_DISABLED will be set it that object's flags.
404 * Return:
405 * 1) false with res->flags setting to zero: not the expected resource type
406 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
407 * 3) true: valid assigned resource
409 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
410 struct resource *res)
412 struct acpi_resource_irq *irq;
413 struct acpi_resource_extended_irq *ext_irq;
415 switch (ares->type) {
416 case ACPI_RESOURCE_TYPE_IRQ:
418 * Per spec, only one interrupt per descriptor is allowed in
419 * _CRS, but some firmware violates this, so parse them all.
421 irq = &ares->data.irq;
422 if (index >= irq->interrupt_count) {
423 acpi_dev_irqresource_disabled(res, 0);
424 return false;
426 acpi_dev_get_irqresource(res, irq->interrupts[index],
427 irq->triggering, irq->polarity,
428 irq->sharable, true);
429 break;
430 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
431 ext_irq = &ares->data.extended_irq;
432 if (index >= ext_irq->interrupt_count) {
433 acpi_dev_irqresource_disabled(res, 0);
434 return false;
436 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
437 ext_irq->triggering, ext_irq->polarity,
438 ext_irq->sharable, false);
439 break;
440 default:
441 res->flags = 0;
442 return false;
445 return true;
447 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
450 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
451 * @list: The head of the resource list to free.
453 void acpi_dev_free_resource_list(struct list_head *list)
455 resource_list_free(list);
457 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
459 struct res_proc_context {
460 struct list_head *list;
461 int (*preproc)(struct acpi_resource *, void *);
462 void *preproc_data;
463 int count;
464 int error;
467 static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
468 struct res_proc_context *c)
470 struct resource_entry *rentry;
472 rentry = resource_list_create_entry(NULL, 0);
473 if (!rentry) {
474 c->error = -ENOMEM;
475 return AE_NO_MEMORY;
477 *rentry->res = win->res;
478 rentry->offset = win->offset;
479 resource_list_add_tail(rentry, c->list);
480 c->count++;
481 return AE_OK;
484 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
485 void *context)
487 struct res_proc_context *c = context;
488 struct resource_win win;
489 struct resource *res = &win.res;
490 int i;
492 if (c->preproc) {
493 int ret;
495 ret = c->preproc(ares, c->preproc_data);
496 if (ret < 0) {
497 c->error = ret;
498 return AE_CTRL_TERMINATE;
499 } else if (ret > 0) {
500 return AE_OK;
504 memset(&win, 0, sizeof(win));
506 if (acpi_dev_resource_memory(ares, res)
507 || acpi_dev_resource_io(ares, res)
508 || acpi_dev_resource_address_space(ares, &win)
509 || acpi_dev_resource_ext_address_space(ares, &win))
510 return acpi_dev_new_resource_entry(&win, c);
512 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
513 acpi_status status;
515 status = acpi_dev_new_resource_entry(&win, c);
516 if (ACPI_FAILURE(status))
517 return status;
520 return AE_OK;
524 * acpi_dev_get_resources - Get current resources of a device.
525 * @adev: ACPI device node to get the resources for.
526 * @list: Head of the resultant list of resources (must be empty).
527 * @preproc: The caller's preprocessing routine.
528 * @preproc_data: Pointer passed to the caller's preprocessing routine.
530 * Evaluate the _CRS method for the given device node and process its output by
531 * (1) executing the @preproc() rountine provided by the caller, passing the
532 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
533 * returned and (2) converting all of the returned ACPI resources into struct
534 * resource objects if possible. If the return value of @preproc() in step (1)
535 * is different from 0, step (2) is not applied to the given ACPI resource and
536 * if that value is negative, the whole processing is aborted and that value is
537 * returned as the final error code.
539 * The resultant struct resource objects are put on the list pointed to by
540 * @list, that must be empty initially, as members of struct resource_entry
541 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
542 * free that list.
544 * The number of resources in the output list is returned on success, an error
545 * code reflecting the error condition is returned otherwise.
547 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
548 int (*preproc)(struct acpi_resource *, void *),
549 void *preproc_data)
551 struct res_proc_context c;
552 acpi_status status;
554 if (!adev || !adev->handle || !list_empty(list))
555 return -EINVAL;
557 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
558 return 0;
560 c.list = list;
561 c.preproc = preproc;
562 c.preproc_data = preproc_data;
563 c.count = 0;
564 c.error = 0;
565 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
566 acpi_dev_process_resource, &c);
567 if (ACPI_FAILURE(status)) {
568 acpi_dev_free_resource_list(list);
569 return c.error ? c.error : -EIO;
572 return c.count;
574 EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
577 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
578 * types
579 * @ares: Input ACPI resource object.
580 * @types: Valid resource types of IORESOURCE_XXX
582 * This is a helper function to support acpi_dev_get_resources(), which filters
583 * ACPI resource objects according to resource types.
585 int acpi_dev_filter_resource_type(struct acpi_resource *ares,
586 unsigned long types)
588 unsigned long type = 0;
590 switch (ares->type) {
591 case ACPI_RESOURCE_TYPE_MEMORY24:
592 case ACPI_RESOURCE_TYPE_MEMORY32:
593 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
594 type = IORESOURCE_MEM;
595 break;
596 case ACPI_RESOURCE_TYPE_IO:
597 case ACPI_RESOURCE_TYPE_FIXED_IO:
598 type = IORESOURCE_IO;
599 break;
600 case ACPI_RESOURCE_TYPE_IRQ:
601 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
602 type = IORESOURCE_IRQ;
603 break;
604 case ACPI_RESOURCE_TYPE_DMA:
605 case ACPI_RESOURCE_TYPE_FIXED_DMA:
606 type = IORESOURCE_DMA;
607 break;
608 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
609 type = IORESOURCE_REG;
610 break;
611 case ACPI_RESOURCE_TYPE_ADDRESS16:
612 case ACPI_RESOURCE_TYPE_ADDRESS32:
613 case ACPI_RESOURCE_TYPE_ADDRESS64:
614 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
615 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
616 type = IORESOURCE_MEM;
617 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
618 type = IORESOURCE_IO;
619 else if (ares->data.address.resource_type ==
620 ACPI_BUS_NUMBER_RANGE)
621 type = IORESOURCE_BUS;
622 break;
623 default:
624 break;
627 return (type & types) ? 0 : 1;
629 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);