Merge branch 'next' of git://git.infradead.org/users/vkoul/slave-dma
[linux/fpc-iii.git] / drivers / acpi / resource.c
blob5589a6e2a02346e3b2ce48656b3facea1abfc621
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 struct resource *res = &win->res;
199 * Filter out invalid descriptor according to ACPI Spec 5.0, section
200 * 6.4.3.5 Address Space Resource Descriptors.
202 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
203 (addr->min_address_fixed && addr->max_address_fixed && !len))
204 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
205 addr->min_address_fixed, addr->max_address_fixed, len);
207 res->start = attr->minimum;
208 res->end = attr->maximum;
211 * For bridges that translate addresses across the bridge,
212 * translation_offset is the offset that must be added to the
213 * address on the secondary side to obtain the address on the
214 * primary side. Non-bridge devices must list 0 for all Address
215 * Translation offset bits.
217 if (addr->producer_consumer == ACPI_PRODUCER) {
218 res->start += attr->translation_offset;
219 res->end += attr->translation_offset;
220 } else if (attr->translation_offset) {
221 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
222 attr->translation_offset);
225 switch (addr->resource_type) {
226 case ACPI_MEMORY_RANGE:
227 acpi_dev_memresource_flags(res, len, wp);
228 break;
229 case ACPI_IO_RANGE:
230 acpi_dev_ioresource_flags(res, len, iodec);
231 break;
232 case ACPI_BUS_NUMBER_RANGE:
233 res->flags = IORESOURCE_BUS;
234 break;
235 default:
236 return false;
239 win->offset = attr->translation_offset;
241 if (addr->producer_consumer == ACPI_PRODUCER)
242 res->flags |= IORESOURCE_WINDOW;
244 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
245 res->flags |= IORESOURCE_PREFETCH;
247 return !(res->flags & IORESOURCE_DISABLED);
251 * acpi_dev_resource_address_space - Extract ACPI address space information.
252 * @ares: Input ACPI resource object.
253 * @win: Output generic resource object.
255 * Check if the given ACPI resource object represents an address space resource
256 * and if that's the case, use the information in it to populate the generic
257 * resource object pointed to by @win.
259 * Return:
260 * 1) false with win->res.flags setting to zero: not the expected resource type
261 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
262 * resource
263 * 3) true: valid assigned resource
265 bool acpi_dev_resource_address_space(struct acpi_resource *ares,
266 struct resource_win *win)
268 struct acpi_resource_address64 addr;
270 win->res.flags = 0;
271 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
272 return false;
274 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
275 &addr.address);
277 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
280 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
281 * @ares: Input ACPI resource object.
282 * @win: Output generic resource object.
284 * Check if the given ACPI resource object represents an extended address space
285 * resource and if that's the case, use the information in it to populate the
286 * generic resource object pointed to by @win.
288 * Return:
289 * 1) false with win->res.flags setting to zero: not the expected resource type
290 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
291 * resource
292 * 3) true: valid assigned resource
294 bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
295 struct resource_win *win)
297 struct acpi_resource_extended_address64 *ext_addr;
299 win->res.flags = 0;
300 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
301 return false;
303 ext_addr = &ares->data.ext_address64;
305 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
306 &ext_addr->address);
308 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
311 * acpi_dev_irq_flags - Determine IRQ resource flags.
312 * @triggering: Triggering type as provided by ACPI.
313 * @polarity: Interrupt polarity as provided by ACPI.
314 * @shareable: Whether or not the interrupt is shareable.
316 unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
318 unsigned long flags;
320 if (triggering == ACPI_LEVEL_SENSITIVE)
321 flags = polarity == ACPI_ACTIVE_LOW ?
322 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
323 else
324 flags = polarity == ACPI_ACTIVE_LOW ?
325 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
327 if (shareable == ACPI_SHARED)
328 flags |= IORESOURCE_IRQ_SHAREABLE;
330 return flags | IORESOURCE_IRQ;
332 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
334 static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
336 res->start = gsi;
337 res->end = gsi;
338 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
341 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
342 u8 triggering, u8 polarity, u8 shareable,
343 bool legacy)
345 int irq, p, t;
347 if (!valid_IRQ(gsi)) {
348 acpi_dev_irqresource_disabled(res, gsi);
349 return;
353 * In IO-APIC mode, use overrided attribute. Two reasons:
354 * 1. BIOS bug in DSDT
355 * 2. BIOS uses IO-APIC mode Interrupt Source Override
357 * We do this only if we are dealing with IRQ() or IRQNoFlags()
358 * resource (the legacy ISA resources). With modern ACPI 5 devices
359 * using extended IRQ descriptors we take the IRQ configuration
360 * from _CRS directly.
362 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
363 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
364 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
366 if (triggering != trig || polarity != pol) {
367 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
368 t ? "level" : "edge", p ? "low" : "high");
369 triggering = trig;
370 polarity = pol;
374 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
375 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
376 if (irq >= 0) {
377 res->start = irq;
378 res->end = irq;
379 } else {
380 acpi_dev_irqresource_disabled(res, gsi);
385 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
386 * @ares: Input ACPI resource object.
387 * @index: Index into the array of GSIs represented by the resource.
388 * @res: Output generic resource object.
390 * Check if the given ACPI resource object represents an interrupt resource
391 * and @index does not exceed the resource's interrupt count (true is returned
392 * in that case regardless of the results of the other checks)). If that's the
393 * case, register the GSI corresponding to @index from the array of interrupts
394 * represented by the resource and populate the generic resource object pointed
395 * to by @res accordingly. If the registration of the GSI is not successful,
396 * IORESOURCE_DISABLED will be set it that object's flags.
398 * Return:
399 * 1) false with res->flags setting to zero: not the expected resource type
400 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
401 * 3) true: valid assigned resource
403 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
404 struct resource *res)
406 struct acpi_resource_irq *irq;
407 struct acpi_resource_extended_irq *ext_irq;
409 switch (ares->type) {
410 case ACPI_RESOURCE_TYPE_IRQ:
412 * Per spec, only one interrupt per descriptor is allowed in
413 * _CRS, but some firmware violates this, so parse them all.
415 irq = &ares->data.irq;
416 if (index >= irq->interrupt_count) {
417 acpi_dev_irqresource_disabled(res, 0);
418 return false;
420 acpi_dev_get_irqresource(res, irq->interrupts[index],
421 irq->triggering, irq->polarity,
422 irq->sharable, true);
423 break;
424 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
425 ext_irq = &ares->data.extended_irq;
426 if (index >= ext_irq->interrupt_count) {
427 acpi_dev_irqresource_disabled(res, 0);
428 return false;
430 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
431 ext_irq->triggering, ext_irq->polarity,
432 ext_irq->sharable, false);
433 break;
434 default:
435 res->flags = 0;
436 return false;
439 return true;
441 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
444 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
445 * @list: The head of the resource list to free.
447 void acpi_dev_free_resource_list(struct list_head *list)
449 resource_list_free(list);
451 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
453 struct res_proc_context {
454 struct list_head *list;
455 int (*preproc)(struct acpi_resource *, void *);
456 void *preproc_data;
457 int count;
458 int error;
461 static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
462 struct res_proc_context *c)
464 struct resource_entry *rentry;
466 rentry = resource_list_create_entry(NULL, 0);
467 if (!rentry) {
468 c->error = -ENOMEM;
469 return AE_NO_MEMORY;
471 *rentry->res = win->res;
472 rentry->offset = win->offset;
473 resource_list_add_tail(rentry, c->list);
474 c->count++;
475 return AE_OK;
478 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
479 void *context)
481 struct res_proc_context *c = context;
482 struct resource_win win;
483 struct resource *res = &win.res;
484 int i;
486 if (c->preproc) {
487 int ret;
489 ret = c->preproc(ares, c->preproc_data);
490 if (ret < 0) {
491 c->error = ret;
492 return AE_CTRL_TERMINATE;
493 } else if (ret > 0) {
494 return AE_OK;
498 memset(&win, 0, sizeof(win));
500 if (acpi_dev_resource_memory(ares, res)
501 || acpi_dev_resource_io(ares, res)
502 || acpi_dev_resource_address_space(ares, &win)
503 || acpi_dev_resource_ext_address_space(ares, &win))
504 return acpi_dev_new_resource_entry(&win, c);
506 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
507 acpi_status status;
509 status = acpi_dev_new_resource_entry(&win, c);
510 if (ACPI_FAILURE(status))
511 return status;
514 return AE_OK;
518 * acpi_dev_get_resources - Get current resources of a device.
519 * @adev: ACPI device node to get the resources for.
520 * @list: Head of the resultant list of resources (must be empty).
521 * @preproc: The caller's preprocessing routine.
522 * @preproc_data: Pointer passed to the caller's preprocessing routine.
524 * Evaluate the _CRS method for the given device node and process its output by
525 * (1) executing the @preproc() rountine provided by the caller, passing the
526 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
527 * returned and (2) converting all of the returned ACPI resources into struct
528 * resource objects if possible. If the return value of @preproc() in step (1)
529 * is different from 0, step (2) is not applied to the given ACPI resource and
530 * if that value is negative, the whole processing is aborted and that value is
531 * returned as the final error code.
533 * The resultant struct resource objects are put on the list pointed to by
534 * @list, that must be empty initially, as members of struct resource_entry
535 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
536 * free that list.
538 * The number of resources in the output list is returned on success, an error
539 * code reflecting the error condition is returned otherwise.
541 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
542 int (*preproc)(struct acpi_resource *, void *),
543 void *preproc_data)
545 struct res_proc_context c;
546 acpi_status status;
548 if (!adev || !adev->handle || !list_empty(list))
549 return -EINVAL;
551 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
552 return 0;
554 c.list = list;
555 c.preproc = preproc;
556 c.preproc_data = preproc_data;
557 c.count = 0;
558 c.error = 0;
559 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
560 acpi_dev_process_resource, &c);
561 if (ACPI_FAILURE(status)) {
562 acpi_dev_free_resource_list(list);
563 return c.error ? c.error : -EIO;
566 return c.count;
568 EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
571 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
572 * types
573 * @ares: Input ACPI resource object.
574 * @types: Valid resource types of IORESOURCE_XXX
576 * This is a hepler function to support acpi_dev_get_resources(), which filters
577 * ACPI resource objects according to resource types.
579 int acpi_dev_filter_resource_type(struct acpi_resource *ares,
580 unsigned long types)
582 unsigned long type = 0;
584 switch (ares->type) {
585 case ACPI_RESOURCE_TYPE_MEMORY24:
586 case ACPI_RESOURCE_TYPE_MEMORY32:
587 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
588 type = IORESOURCE_MEM;
589 break;
590 case ACPI_RESOURCE_TYPE_IO:
591 case ACPI_RESOURCE_TYPE_FIXED_IO:
592 type = IORESOURCE_IO;
593 break;
594 case ACPI_RESOURCE_TYPE_IRQ:
595 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
596 type = IORESOURCE_IRQ;
597 break;
598 case ACPI_RESOURCE_TYPE_DMA:
599 case ACPI_RESOURCE_TYPE_FIXED_DMA:
600 type = IORESOURCE_DMA;
601 break;
602 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
603 type = IORESOURCE_REG;
604 break;
605 case ACPI_RESOURCE_TYPE_ADDRESS16:
606 case ACPI_RESOURCE_TYPE_ADDRESS32:
607 case ACPI_RESOURCE_TYPE_ADDRESS64:
608 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
609 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
610 type = IORESOURCE_MEM;
611 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
612 type = IORESOURCE_IO;
613 else if (ares->data.address.resource_type ==
614 ACPI_BUS_NUMBER_RANGE)
615 type = IORESOURCE_BUS;
616 break;
617 default:
618 break;
621 return (type & types) ? 0 : 1;
623 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);