blob: 3e7d9f6eb875f24c062131325aaa1d290a0aef9e [file] [log] [blame]
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +01001/*
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>
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +010029#include <linux/slab.h>
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010030
31#ifdef CONFIG_X86
32#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
33#else
34#define valid_IRQ(i) (true)
35#endif
36
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080037static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010038{
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080039 u64 reslen = end - start + 1;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010040
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080041 /*
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
55static void acpi_dev_memresource_flags(struct resource *res, u64 len,
56 u8 write_protect, bool window)
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;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010062
63 if (write_protect == ACPI_READ_WRITE_MEMORY)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080064 res->flags |= IORESOURCE_MEM_WRITEABLE;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010065
66 if (window)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080067 res->flags |= IORESOURCE_WINDOW;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010068}
69
70static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
71 u8 write_protect)
72{
73 res->start = start;
74 res->end = start + len - 1;
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080075 acpi_dev_memresource_flags(res, len, write_protect, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010076}
77
78/**
79 * acpi_dev_resource_memory - Extract ACPI memory resource information.
80 * @ares: Input ACPI resource object.
81 * @res: Output generic resource object.
82 *
83 * Check if the given ACPI resource object represents a memory resource and
84 * if that's the case, use the information in it to populate the generic
85 * resource object pointed to by @res.
86 */
87bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
88{
89 struct acpi_resource_memory24 *memory24;
90 struct acpi_resource_memory32 *memory32;
91 struct acpi_resource_fixed_memory32 *fixed_memory32;
92
93 switch (ares->type) {
94 case ACPI_RESOURCE_TYPE_MEMORY24:
95 memory24 = &ares->data.memory24;
96 acpi_dev_get_memresource(res, memory24->minimum,
97 memory24->address_length,
98 memory24->write_protect);
99 break;
100 case ACPI_RESOURCE_TYPE_MEMORY32:
101 memory32 = &ares->data.memory32;
102 acpi_dev_get_memresource(res, memory32->minimum,
103 memory32->address_length,
104 memory32->write_protect);
105 break;
106 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
107 fixed_memory32 = &ares->data.fixed_memory32;
108 acpi_dev_get_memresource(res, fixed_memory32->address,
109 fixed_memory32->address_length,
110 fixed_memory32->write_protect);
111 break;
112 default:
113 return false;
114 }
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800115
116 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100117}
118EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
119
120static unsigned int acpi_dev_ioresource_flags(u64 start, u64 end, u8 io_decode,
121 bool window)
122{
123 int flags = IORESOURCE_IO;
124
125 if (io_decode == ACPI_DECODE_16)
126 flags |= IORESOURCE_IO_16BIT_ADDR;
127
128 if (start > end || end >= 0x10003)
129 flags |= IORESOURCE_DISABLED;
130
131 if (window)
132 flags |= IORESOURCE_WINDOW;
133
134 return flags;
135}
136
137static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
138 u8 io_decode)
139{
140 u64 end = start + len - 1;
141
142 res->start = start;
143 res->end = end;
144 res->flags = acpi_dev_ioresource_flags(start, end, io_decode, false);
145}
146
147/**
148 * acpi_dev_resource_io - Extract ACPI I/O resource information.
149 * @ares: Input ACPI resource object.
150 * @res: Output generic resource object.
151 *
152 * Check if the given ACPI resource object represents an I/O resource and
153 * if that's the case, use the information in it to populate the generic
154 * resource object pointed to by @res.
155 */
156bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
157{
158 struct acpi_resource_io *io;
159 struct acpi_resource_fixed_io *fixed_io;
160
161 switch (ares->type) {
162 case ACPI_RESOURCE_TYPE_IO:
163 io = &ares->data.io;
Andy Whitcroft867f9d42014-06-19 11:19:16 +0100164 if (!io->minimum && !io->address_length)
Zhang Ruib355cee2014-02-27 11:37:15 +0800165 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100166 acpi_dev_get_ioresource(res, io->minimum,
167 io->address_length,
168 io->io_decode);
169 break;
170 case ACPI_RESOURCE_TYPE_FIXED_IO:
171 fixed_io = &ares->data.fixed_io;
Andy Whitcroft867f9d42014-06-19 11:19:16 +0100172 if (!fixed_io->address && !fixed_io->address_length)
Zhang Ruib355cee2014-02-27 11:37:15 +0800173 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100174 acpi_dev_get_ioresource(res, fixed_io->address,
175 fixed_io->address_length,
176 ACPI_DECODE_10);
177 break;
178 default:
179 return false;
180 }
181 return true;
182}
183EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
184
185/**
186 * acpi_dev_resource_address_space - Extract ACPI address space information.
187 * @ares: Input ACPI resource object.
188 * @res: Output generic resource object.
189 *
190 * Check if the given ACPI resource object represents an address space resource
191 * and if that's the case, use the information in it to populate the generic
192 * resource object pointed to by @res.
193 */
194bool acpi_dev_resource_address_space(struct acpi_resource *ares,
195 struct resource *res)
196{
197 acpi_status status;
198 struct acpi_resource_address64 addr;
199 bool window;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100200 u8 io_decode;
201
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100202 status = acpi_resource_to_address64(ares, &addr);
203 if (ACPI_FAILURE(status))
Jiang Liu6658c732014-10-27 13:21:35 +0800204 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100205
Lv Zhenga45de932015-01-26 16:58:56 +0800206 res->start = addr.address.minimum;
207 res->end = addr.address.maximum;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100208 window = addr.producer_consumer == ACPI_PRODUCER;
209
210 switch(addr.resource_type) {
211 case ACPI_MEMORY_RANGE:
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800212 acpi_dev_memresource_flags(res, addr.address.address_length,
213 addr.info.mem.write_protect,
214 window);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100215 break;
216 case ACPI_IO_RANGE:
Lv Zhenga45de932015-01-26 16:58:56 +0800217 io_decode = addr.address.granularity == 0xfff ?
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100218 ACPI_DECODE_10 : ACPI_DECODE_16;
Lv Zhenga45de932015-01-26 16:58:56 +0800219 res->flags = acpi_dev_ioresource_flags(addr.address.minimum,
220 addr.address.maximum,
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100221 io_decode, window);
222 break;
223 case ACPI_BUS_NUMBER_RANGE:
224 res->flags = IORESOURCE_BUS;
225 break;
226 default:
227 res->flags = 0;
228 }
229
230 return true;
231}
232EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
233
234/**
235 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
236 * @ares: Input ACPI resource object.
237 * @res: Output generic resource object.
238 *
239 * Check if the given ACPI resource object represents an extended address space
240 * resource and if that's the case, use the information in it to populate the
241 * generic resource object pointed to by @res.
242 */
243bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
244 struct resource *res)
245{
246 struct acpi_resource_extended_address64 *ext_addr;
247 bool window;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100248 u8 io_decode;
249
250 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
251 return false;
252
253 ext_addr = &ares->data.ext_address64;
254
Lv Zhenga45de932015-01-26 16:58:56 +0800255 res->start = ext_addr->address.minimum;
256 res->end = ext_addr->address.maximum;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100257 window = ext_addr->producer_consumer == ACPI_PRODUCER;
258
259 switch(ext_addr->resource_type) {
260 case ACPI_MEMORY_RANGE:
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800261 acpi_dev_memresource_flags(res,
262 ext_addr->address.address_length,
263 ext_addr->info.mem.write_protect,
264 window);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100265 break;
266 case ACPI_IO_RANGE:
Lv Zhenga45de932015-01-26 16:58:56 +0800267 io_decode = ext_addr->address.granularity == 0xfff ?
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100268 ACPI_DECODE_10 : ACPI_DECODE_16;
Lv Zhenga45de932015-01-26 16:58:56 +0800269 res->flags = acpi_dev_ioresource_flags(ext_addr->address.minimum,
270 ext_addr->address.maximum,
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100271 io_decode, window);
272 break;
273 case ACPI_BUS_NUMBER_RANGE:
274 res->flags = IORESOURCE_BUS;
275 break;
276 default:
277 res->flags = 0;
278 }
279
280 return true;
281}
282EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
283
284/**
285 * acpi_dev_irq_flags - Determine IRQ resource flags.
286 * @triggering: Triggering type as provided by ACPI.
287 * @polarity: Interrupt polarity as provided by ACPI.
288 * @shareable: Whether or not the interrupt is shareable.
289 */
290unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
291{
292 unsigned long flags;
293
294 if (triggering == ACPI_LEVEL_SENSITIVE)
295 flags = polarity == ACPI_ACTIVE_LOW ?
296 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
297 else
298 flags = polarity == ACPI_ACTIVE_LOW ?
299 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
300
301 if (shareable == ACPI_SHARED)
302 flags |= IORESOURCE_IRQ_SHAREABLE;
303
304 return flags | IORESOURCE_IRQ;
305}
306EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
307
308static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
309{
310 res->start = gsi;
311 res->end = gsi;
312 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED;
313}
314
315static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000316 u8 triggering, u8 polarity, u8 shareable,
317 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100318{
319 int irq, p, t;
320
321 if (!valid_IRQ(gsi)) {
322 acpi_dev_irqresource_disabled(res, gsi);
323 return;
324 }
325
326 /*
327 * In IO-APIC mode, use overrided attribute. Two reasons:
328 * 1. BIOS bug in DSDT
329 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000330 *
331 * We do this only if we are dealing with IRQ() or IRQNoFlags()
332 * resource (the legacy ISA resources). With modern ACPI 5 devices
333 * using extended IRQ descriptors we take the IRQ configuration
334 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100335 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000336 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100337 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
338 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
339
340 if (triggering != trig || polarity != pol) {
341 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000342 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100343 triggering = trig;
344 polarity = pol;
345 }
346 }
347
348 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
349 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
350 if (irq >= 0) {
351 res->start = irq;
352 res->end = irq;
353 } else {
354 acpi_dev_irqresource_disabled(res, gsi);
355 }
356}
357
358/**
359 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
360 * @ares: Input ACPI resource object.
361 * @index: Index into the array of GSIs represented by the resource.
362 * @res: Output generic resource object.
363 *
364 * Check if the given ACPI resource object represents an interrupt resource
365 * and @index does not exceed the resource's interrupt count (true is returned
366 * in that case regardless of the results of the other checks)). If that's the
367 * case, register the GSI corresponding to @index from the array of interrupts
368 * represented by the resource and populate the generic resource object pointed
369 * to by @res accordingly. If the registration of the GSI is not successful,
370 * IORESOURCE_DISABLED will be set it that object's flags.
371 */
372bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
373 struct resource *res)
374{
375 struct acpi_resource_irq *irq;
376 struct acpi_resource_extended_irq *ext_irq;
377
378 switch (ares->type) {
379 case ACPI_RESOURCE_TYPE_IRQ:
380 /*
381 * Per spec, only one interrupt per descriptor is allowed in
382 * _CRS, but some firmware violates this, so parse them all.
383 */
384 irq = &ares->data.irq;
385 if (index >= irq->interrupt_count) {
386 acpi_dev_irqresource_disabled(res, 0);
387 return false;
388 }
389 acpi_dev_get_irqresource(res, irq->interrupts[index],
390 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000391 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100392 break;
393 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
394 ext_irq = &ares->data.extended_irq;
395 if (index >= ext_irq->interrupt_count) {
396 acpi_dev_irqresource_disabled(res, 0);
397 return false;
398 }
399 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
400 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000401 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100402 break;
403 default:
404 return false;
405 }
406
407 return true;
408}
409EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100410
411/**
412 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
413 * @list: The head of the resource list to free.
414 */
415void acpi_dev_free_resource_list(struct list_head *list)
416{
417 struct resource_list_entry *rentry, *re;
418
419 list_for_each_entry_safe(rentry, re, list, node) {
420 list_del(&rentry->node);
421 kfree(rentry);
422 }
423}
424EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
425
426struct res_proc_context {
427 struct list_head *list;
428 int (*preproc)(struct acpi_resource *, void *);
429 void *preproc_data;
430 int count;
431 int error;
432};
433
434static acpi_status acpi_dev_new_resource_entry(struct resource *r,
435 struct res_proc_context *c)
436{
437 struct resource_list_entry *rentry;
438
439 rentry = kmalloc(sizeof(*rentry), GFP_KERNEL);
440 if (!rentry) {
441 c->error = -ENOMEM;
442 return AE_NO_MEMORY;
443 }
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100444 rentry->res = *r;
445 list_add_tail(&rentry->node, c->list);
446 c->count++;
447 return AE_OK;
448}
449
450static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
451 void *context)
452{
453 struct res_proc_context *c = context;
454 struct resource r;
455 int i;
456
457 if (c->preproc) {
458 int ret;
459
460 ret = c->preproc(ares, c->preproc_data);
461 if (ret < 0) {
462 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100463 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100464 } else if (ret > 0) {
465 return AE_OK;
466 }
467 }
468
469 memset(&r, 0, sizeof(r));
470
471 if (acpi_dev_resource_memory(ares, &r)
472 || acpi_dev_resource_io(ares, &r)
473 || acpi_dev_resource_address_space(ares, &r)
474 || acpi_dev_resource_ext_address_space(ares, &r))
475 return acpi_dev_new_resource_entry(&r, c);
476
477 for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) {
478 acpi_status status;
479
480 status = acpi_dev_new_resource_entry(&r, c);
481 if (ACPI_FAILURE(status))
482 return status;
483 }
484
485 return AE_OK;
486}
487
488/**
489 * acpi_dev_get_resources - Get current resources of a device.
490 * @adev: ACPI device node to get the resources for.
491 * @list: Head of the resultant list of resources (must be empty).
492 * @preproc: The caller's preprocessing routine.
493 * @preproc_data: Pointer passed to the caller's preprocessing routine.
494 *
495 * Evaluate the _CRS method for the given device node and process its output by
496 * (1) executing the @preproc() rountine provided by the caller, passing the
497 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
498 * returned and (2) converting all of the returned ACPI resources into struct
499 * resource objects if possible. If the return value of @preproc() in step (1)
500 * is different from 0, step (2) is not applied to the given ACPI resource and
501 * if that value is negative, the whole processing is aborted and that value is
502 * returned as the final error code.
503 *
504 * The resultant struct resource objects are put on the list pointed to by
505 * @list, that must be empty initially, as members of struct resource_list_entry
506 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
507 * free that list.
508 *
509 * The number of resources in the output list is returned on success, an error
510 * code reflecting the error condition is returned otherwise.
511 */
512int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
513 int (*preproc)(struct acpi_resource *, void *),
514 void *preproc_data)
515{
516 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100517 acpi_status status;
518
519 if (!adev || !adev->handle || !list_empty(list))
520 return -EINVAL;
521
Jiang Liu952c63e2013-06-29 00:24:38 +0800522 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100523 return 0;
524
525 c.list = list;
526 c.preproc = preproc;
527 c.preproc_data = preproc_data;
528 c.count = 0;
529 c.error = 0;
530 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
531 acpi_dev_process_resource, &c);
532 if (ACPI_FAILURE(status)) {
533 acpi_dev_free_resource_list(list);
534 return c.error ? c.error : -EIO;
535 }
536
537 return c.count;
538}
539EXPORT_SYMBOL_GPL(acpi_dev_get_resources);