2a7711fb00841b4328e39f877f5308835b51f92c
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / resource.c
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>
29 #include <linux/list.h>
30 #include <linux/slab.h>
31
32 #ifdef CONFIG_X86
33 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
34 #else
35 #define valid_IRQ(i) (true)
36 #endif
37
38 static unsigned long acpi_dev_memresource_flags(u64 len, u8 write_protect,
39 bool window)
40 {
41 unsigned long flags = IORESOURCE_MEM;
42
43 if (len == 0)
44 flags |= IORESOURCE_DISABLED;
45
46 if (write_protect == ACPI_READ_WRITE_MEMORY)
47 flags |= IORESOURCE_MEM_WRITEABLE;
48
49 if (window)
50 flags |= IORESOURCE_WINDOW;
51
52 return flags;
53 }
54
55 static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
56 u8 write_protect)
57 {
58 res->start = start;
59 res->end = start + len - 1;
60 res->flags = acpi_dev_memresource_flags(len, write_protect, false);
61 }
62
63 /**
64 * acpi_dev_resource_memory - Extract ACPI memory resource information.
65 * @ares: Input ACPI resource object.
66 * @res: Output generic resource object.
67 *
68 * Check if the given ACPI resource object represents a memory resource and
69 * if that's the case, use the information in it to populate the generic
70 * resource object pointed to by @res.
71 */
72 bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
73 {
74 struct acpi_resource_memory24 *memory24;
75 struct acpi_resource_memory32 *memory32;
76 struct acpi_resource_fixed_memory32 *fixed_memory32;
77
78 switch (ares->type) {
79 case ACPI_RESOURCE_TYPE_MEMORY24:
80 memory24 = &ares->data.memory24;
81 if (!memory24->minimum && !memory24->address_length)
82 return false;
83 acpi_dev_get_memresource(res, memory24->minimum,
84 memory24->address_length,
85 memory24->write_protect);
86 break;
87 case ACPI_RESOURCE_TYPE_MEMORY32:
88 memory32 = &ares->data.memory32;
89 if (!memory32->minimum && !memory32->address_length)
90 return false;
91 acpi_dev_get_memresource(res, memory32->minimum,
92 memory32->address_length,
93 memory32->write_protect);
94 break;
95 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
96 fixed_memory32 = &ares->data.fixed_memory32;
97 if (!fixed_memory32->address && !fixed_memory32->address_length)
98 return false;
99 acpi_dev_get_memresource(res, fixed_memory32->address,
100 fixed_memory32->address_length,
101 fixed_memory32->write_protect);
102 break;
103 default:
104 return false;
105 }
106 return true;
107 }
108 EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
109
110 static unsigned int acpi_dev_ioresource_flags(u64 start, u64 end, u8 io_decode,
111 bool window)
112 {
113 int flags = IORESOURCE_IO;
114
115 if (io_decode == ACPI_DECODE_16)
116 flags |= IORESOURCE_IO_16BIT_ADDR;
117
118 if (start > end || end >= 0x10003)
119 flags |= IORESOURCE_DISABLED;
120
121 if (window)
122 flags |= IORESOURCE_WINDOW;
123
124 return flags;
125 }
126
127 static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
128 u8 io_decode)
129 {
130 u64 end = start + len - 1;
131
132 res->start = start;
133 res->end = end;
134 res->flags = acpi_dev_ioresource_flags(start, end, io_decode, false);
135 }
136
137 /**
138 * acpi_dev_resource_io - Extract ACPI I/O resource information.
139 * @ares: Input ACPI resource object.
140 * @res: Output generic resource object.
141 *
142 * Check if the given ACPI resource object represents an I/O resource and
143 * if that's the case, use the information in it to populate the generic
144 * resource object pointed to by @res.
145 */
146 bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
147 {
148 struct acpi_resource_io *io;
149 struct acpi_resource_fixed_io *fixed_io;
150
151 switch (ares->type) {
152 case ACPI_RESOURCE_TYPE_IO:
153 io = &ares->data.io;
154 if (!io->minimum && !io->address_length)
155 return false;
156 acpi_dev_get_ioresource(res, io->minimum,
157 io->address_length,
158 io->io_decode);
159 break;
160 case ACPI_RESOURCE_TYPE_FIXED_IO:
161 fixed_io = &ares->data.fixed_io;
162 if (!fixed_io->address && !fixed_io->address_length)
163 return false;
164 acpi_dev_get_ioresource(res, fixed_io->address,
165 fixed_io->address_length,
166 ACPI_DECODE_10);
167 break;
168 default:
169 return false;
170 }
171 return true;
172 }
173 EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
174
175 /**
176 * acpi_dev_resource_address_space - Extract ACPI address space information.
177 * @ares: Input ACPI resource object.
178 * @res: Output generic resource object.
179 *
180 * Check if the given ACPI resource object represents an address space resource
181 * and if that's the case, use the information in it to populate the generic
182 * resource object pointed to by @res.
183 */
184 bool acpi_dev_resource_address_space(struct acpi_resource *ares,
185 struct resource *res)
186 {
187 acpi_status status;
188 struct acpi_resource_address64 addr;
189 bool window;
190 u64 len;
191 u8 io_decode;
192
193 switch (ares->type) {
194 case ACPI_RESOURCE_TYPE_ADDRESS16:
195 case ACPI_RESOURCE_TYPE_ADDRESS32:
196 case ACPI_RESOURCE_TYPE_ADDRESS64:
197 break;
198 default:
199 return false;
200 }
201
202 status = acpi_resource_to_address64(ares, &addr);
203 if (ACPI_FAILURE(status))
204 return true;
205
206 res->start = addr.minimum;
207 res->end = addr.maximum;
208 window = addr.producer_consumer == ACPI_PRODUCER;
209
210 switch(addr.resource_type) {
211 case ACPI_MEMORY_RANGE:
212 len = addr.maximum - addr.minimum + 1;
213 res->flags = acpi_dev_memresource_flags(len,
214 addr.info.mem.write_protect,
215 window);
216 break;
217 case ACPI_IO_RANGE:
218 io_decode = addr.granularity == 0xfff ?
219 ACPI_DECODE_10 : ACPI_DECODE_16;
220 res->flags = acpi_dev_ioresource_flags(addr.minimum,
221 addr.maximum,
222 io_decode, window);
223 break;
224 case ACPI_BUS_NUMBER_RANGE:
225 res->flags = IORESOURCE_BUS;
226 break;
227 default:
228 res->flags = 0;
229 }
230
231 return true;
232 }
233 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
234
235 /**
236 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
237 * @ares: Input ACPI resource object.
238 * @res: Output generic resource object.
239 *
240 * Check if the given ACPI resource object represents an extended address space
241 * resource and if that's the case, use the information in it to populate the
242 * generic resource object pointed to by @res.
243 */
244 bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
245 struct resource *res)
246 {
247 struct acpi_resource_extended_address64 *ext_addr;
248 bool window;
249 u64 len;
250 u8 io_decode;
251
252 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
253 return false;
254
255 ext_addr = &ares->data.ext_address64;
256
257 res->start = ext_addr->minimum;
258 res->end = ext_addr->maximum;
259 window = ext_addr->producer_consumer == ACPI_PRODUCER;
260
261 switch(ext_addr->resource_type) {
262 case ACPI_MEMORY_RANGE:
263 len = ext_addr->maximum - ext_addr->minimum + 1;
264 res->flags = acpi_dev_memresource_flags(len,
265 ext_addr->info.mem.write_protect,
266 window);
267 break;
268 case ACPI_IO_RANGE:
269 io_decode = ext_addr->granularity == 0xfff ?
270 ACPI_DECODE_10 : ACPI_DECODE_16;
271 res->flags = acpi_dev_ioresource_flags(ext_addr->minimum,
272 ext_addr->maximum,
273 io_decode, window);
274 break;
275 case ACPI_BUS_NUMBER_RANGE:
276 res->flags = IORESOURCE_BUS;
277 break;
278 default:
279 res->flags = 0;
280 }
281
282 return true;
283 }
284 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
285
286 /**
287 * acpi_dev_irq_flags - Determine IRQ resource flags.
288 * @triggering: Triggering type as provided by ACPI.
289 * @polarity: Interrupt polarity as provided by ACPI.
290 * @shareable: Whether or not the interrupt is shareable.
291 */
292 unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
293 {
294 unsigned long flags;
295
296 if (triggering == ACPI_LEVEL_SENSITIVE)
297 flags = polarity == ACPI_ACTIVE_LOW ?
298 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
299 else
300 flags = polarity == ACPI_ACTIVE_LOW ?
301 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
302
303 if (shareable == ACPI_SHARED)
304 flags |= IORESOURCE_IRQ_SHAREABLE;
305
306 return flags | IORESOURCE_IRQ;
307 }
308 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
309
310 static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
311 {
312 res->start = gsi;
313 res->end = gsi;
314 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED;
315 }
316
317 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
318 u8 triggering, u8 polarity, u8 shareable,
319 bool legacy)
320 {
321 int irq, p, t;
322
323 if (!valid_IRQ(gsi)) {
324 acpi_dev_irqresource_disabled(res, gsi);
325 return;
326 }
327
328 /*
329 * In IO-APIC mode, use overrided attribute. Two reasons:
330 * 1. BIOS bug in DSDT
331 * 2. BIOS uses IO-APIC mode Interrupt Source Override
332 *
333 * We do this only if we are dealing with IRQ() or IRQNoFlags()
334 * resource (the legacy ISA resources). With modern ACPI 5 devices
335 * using extended IRQ descriptors we take the IRQ configuration
336 * from _CRS directly.
337 */
338 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
339 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
340 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
341
342 if (triggering != trig || polarity != pol) {
343 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
344 t ? "level" : "edge", p ? "low" : "high");
345 triggering = trig;
346 polarity = pol;
347 }
348 }
349
350 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
351 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
352 if (irq >= 0) {
353 res->start = irq;
354 res->end = irq;
355 } else {
356 acpi_dev_irqresource_disabled(res, gsi);
357 }
358 }
359
360 /**
361 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
362 * @ares: Input ACPI resource object.
363 * @index: Index into the array of GSIs represented by the resource.
364 * @res: Output generic resource object.
365 *
366 * Check if the given ACPI resource object represents an interrupt resource
367 * and @index does not exceed the resource's interrupt count (true is returned
368 * in that case regardless of the results of the other checks)). If that's the
369 * case, register the GSI corresponding to @index from the array of interrupts
370 * represented by the resource and populate the generic resource object pointed
371 * to by @res accordingly. If the registration of the GSI is not successful,
372 * IORESOURCE_DISABLED will be set it that object's flags.
373 */
374 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
375 struct resource *res)
376 {
377 struct acpi_resource_irq *irq;
378 struct acpi_resource_extended_irq *ext_irq;
379
380 switch (ares->type) {
381 case ACPI_RESOURCE_TYPE_IRQ:
382 /*
383 * Per spec, only one interrupt per descriptor is allowed in
384 * _CRS, but some firmware violates this, so parse them all.
385 */
386 irq = &ares->data.irq;
387 if (index >= irq->interrupt_count) {
388 acpi_dev_irqresource_disabled(res, 0);
389 return false;
390 }
391 acpi_dev_get_irqresource(res, irq->interrupts[index],
392 irq->triggering, irq->polarity,
393 irq->sharable, true);
394 break;
395 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
396 ext_irq = &ares->data.extended_irq;
397 if (index >= ext_irq->interrupt_count) {
398 acpi_dev_irqresource_disabled(res, 0);
399 return false;
400 }
401 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
402 ext_irq->triggering, ext_irq->polarity,
403 ext_irq->sharable, false);
404 break;
405 default:
406 return false;
407 }
408
409 return true;
410 }
411 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
412
413 /**
414 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
415 * @list: The head of the resource list to free.
416 */
417 void acpi_dev_free_resource_list(struct list_head *list)
418 {
419 struct resource_list_entry *rentry, *re;
420
421 list_for_each_entry_safe(rentry, re, list, node) {
422 list_del(&rentry->node);
423 kfree(rentry);
424 }
425 }
426 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
427
428 struct res_proc_context {
429 struct list_head *list;
430 int (*preproc)(struct acpi_resource *, void *);
431 void *preproc_data;
432 int count;
433 int error;
434 };
435
436 static acpi_status acpi_dev_new_resource_entry(struct resource *r,
437 struct res_proc_context *c)
438 {
439 struct resource_list_entry *rentry;
440
441 rentry = kmalloc(sizeof(*rentry), GFP_KERNEL);
442 if (!rentry) {
443 c->error = -ENOMEM;
444 return AE_NO_MEMORY;
445 }
446 rentry->res = *r;
447 list_add_tail(&rentry->node, c->list);
448 c->count++;
449 return AE_OK;
450 }
451
452 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
453 void *context)
454 {
455 struct res_proc_context *c = context;
456 struct resource r;
457 int i;
458
459 if (c->preproc) {
460 int ret;
461
462 ret = c->preproc(ares, c->preproc_data);
463 if (ret < 0) {
464 c->error = ret;
465 return AE_CTRL_TERMINATE;
466 } else if (ret > 0) {
467 return AE_OK;
468 }
469 }
470
471 memset(&r, 0, sizeof(r));
472
473 if (acpi_dev_resource_memory(ares, &r)
474 || acpi_dev_resource_io(ares, &r)
475 || acpi_dev_resource_address_space(ares, &r)
476 || acpi_dev_resource_ext_address_space(ares, &r))
477 return acpi_dev_new_resource_entry(&r, c);
478
479 for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) {
480 acpi_status status;
481
482 status = acpi_dev_new_resource_entry(&r, c);
483 if (ACPI_FAILURE(status))
484 return status;
485 }
486
487 return AE_OK;
488 }
489
490 /**
491 * acpi_dev_get_resources - Get current resources of a device.
492 * @adev: ACPI device node to get the resources for.
493 * @list: Head of the resultant list of resources (must be empty).
494 * @preproc: The caller's preprocessing routine.
495 * @preproc_data: Pointer passed to the caller's preprocessing routine.
496 *
497 * Evaluate the _CRS method for the given device node and process its output by
498 * (1) executing the @preproc() rountine provided by the caller, passing the
499 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
500 * returned and (2) converting all of the returned ACPI resources into struct
501 * resource objects if possible. If the return value of @preproc() in step (1)
502 * is different from 0, step (2) is not applied to the given ACPI resource and
503 * if that value is negative, the whole processing is aborted and that value is
504 * returned as the final error code.
505 *
506 * The resultant struct resource objects are put on the list pointed to by
507 * @list, that must be empty initially, as members of struct resource_list_entry
508 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
509 * free that list.
510 *
511 * The number of resources in the output list is returned on success, an error
512 * code reflecting the error condition is returned otherwise.
513 */
514 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
515 int (*preproc)(struct acpi_resource *, void *),
516 void *preproc_data)
517 {
518 struct res_proc_context c;
519 acpi_handle not_used;
520 acpi_status status;
521
522 if (!adev || !adev->handle || !list_empty(list))
523 return -EINVAL;
524
525 status = acpi_get_handle(adev->handle, METHOD_NAME__CRS, &not_used);
526 if (ACPI_FAILURE(status))
527 return 0;
528
529 c.list = list;
530 c.preproc = preproc;
531 c.preproc_data = preproc_data;
532 c.count = 0;
533 c.error = 0;
534 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
535 acpi_dev_process_resource, &c);
536 if (ACPI_FAILURE(status)) {
537 acpi_dev_free_resource_list(list);
538 return c.error ? c.error : -EIO;
539 }
540
541 return c.count;
542 }
543 EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
544
545 struct reserved_region {
546 struct list_head node;
547 u64 start;
548 u64 end;
549 };
550
551 static LIST_HEAD(reserved_io_regions);
552 static LIST_HEAD(reserved_mem_regions);
553
554 static int request_range(u64 start, u64 end, u8 space_id, unsigned long flags,
555 char *desc)
556 {
557 unsigned int length = end - start + 1;
558 struct resource *res;
559
560 res = space_id == ACPI_ADR_SPACE_SYSTEM_IO ?
561 request_region(start, length, desc) :
562 request_mem_region(start, length, desc);
563 if (!res)
564 return -EIO;
565
566 res->flags &= ~flags;
567 return 0;
568 }
569
570 static int add_region_before(u64 start, u64 end, u8 space_id,
571 unsigned long flags, char *desc,
572 struct list_head *head)
573 {
574 struct reserved_region *reg;
575 int error;
576
577 reg = kmalloc(sizeof(*reg), GFP_KERNEL);
578 if (!reg)
579 return -ENOMEM;
580
581 error = request_range(start, end, space_id, flags, desc);
582 if (error) {
583 kfree(reg);
584 return error;
585 }
586
587 reg->start = start;
588 reg->end = end;
589 list_add_tail(&reg->node, head);
590 return 0;
591 }
592
593 /**
594 * acpi_reserve_region - Reserve an I/O or memory region as a system resource.
595 * @start: Starting address of the region.
596 * @length: Length of the region.
597 * @space_id: Identifier of address space to reserve the region from.
598 * @flags: Resource flags to clear for the region after requesting it.
599 * @desc: Region description (for messages).
600 *
601 * Reserve an I/O or memory region as a system resource to prevent others from
602 * using it. If the new region overlaps with one of the regions (in the given
603 * address space) already reserved by this routine, only the non-overlapping
604 * parts of it will be reserved.
605 *
606 * Returned is either 0 (success) or a negative error code indicating a resource
607 * reservation problem. It is the code of the first encountered error, but the
608 * routine doesn't abort until it has attempted to request all of the parts of
609 * the new region that don't overlap with other regions reserved previously.
610 *
611 * The resources requested by this routine are never released.
612 */
613 int acpi_reserve_region(u64 start, unsigned int length, u8 space_id,
614 unsigned long flags, char *desc)
615 {
616 struct list_head *regions;
617 struct reserved_region *reg;
618 u64 end = start + length - 1;
619 int ret = 0, error = 0;
620
621 if (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
622 regions = &reserved_io_regions;
623 else if (space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
624 regions = &reserved_mem_regions;
625 else
626 return -EINVAL;
627
628 if (list_empty(regions))
629 return add_region_before(start, end, space_id, flags, desc, regions);
630
631 list_for_each_entry(reg, regions, node)
632 if (reg->start == end + 1) {
633 /* The new region can be prepended to this one. */
634 ret = request_range(start, end, space_id, flags, desc);
635 if (!ret)
636 reg->start = start;
637
638 return ret;
639 } else if (reg->start > end) {
640 /* No overlap. Add the new region here and get out. */
641 return add_region_before(start, end, space_id, flags,
642 desc, &reg->node);
643 } else if (reg->end == start - 1) {
644 goto combine;
645 } else if (reg->end >= start) {
646 goto overlap;
647 }
648
649 /* The new region goes after the last existing one. */
650 return add_region_before(start, end, space_id, flags, desc, regions);
651
652 overlap:
653 /*
654 * The new region overlaps an existing one.
655 *
656 * The head part of the new region immediately preceding the existing
657 * overlapping one can be combined with it right away.
658 */
659 if (reg->start > start) {
660 error = request_range(start, reg->start - 1, space_id, flags, desc);
661 if (error)
662 ret = error;
663 else
664 reg->start = start;
665 }
666
667 combine:
668 /*
669 * The new region is adjacent to an existing one. If it extends beyond
670 * that region all the way to the next one, it is possible to combine
671 * all three of them.
672 */
673 while (reg->end < end) {
674 struct reserved_region *next = NULL;
675 u64 a = reg->end + 1, b = end;
676
677 if (!list_is_last(&reg->node, regions)) {
678 next = list_next_entry(reg, node);
679 if (next->start <= end)
680 b = next->start - 1;
681 }
682 error = request_range(a, b, space_id, flags, desc);
683 if (!error) {
684 if (next && next->start == b + 1) {
685 reg->end = next->end;
686 list_del(&next->node);
687 kfree(next);
688 } else {
689 reg->end = end;
690 break;
691 }
692 } else if (next) {
693 if (!ret)
694 ret = error;
695
696 reg = next;
697 } else {
698 break;
699 }
700 }
701
702 return ret ? ret : error;
703 }
704 EXPORT_SYMBOL_GPL(acpi_reserve_region);