blob: 99903d196024acfe2b7b177136107bea4d54b306 [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
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800120static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
121 u8 io_decode, bool window)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100122{
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800123 res->flags = IORESOURCE_IO;
124
125 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
126 res->flags |= IORESOURCE_DISABLED;
127
128 if (res->end >= 0x10003)
129 res->flags |= IORESOURCE_DISABLED;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100130
131 if (io_decode == ACPI_DECODE_16)
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800132 res->flags |= IORESOURCE_IO_16BIT_ADDR;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100133
134 if (window)
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800135 res->flags |= IORESOURCE_WINDOW;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100136}
137
138static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
139 u8 io_decode)
140{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100141 res->start = start;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800142 res->end = start + len - 1;
143 acpi_dev_ioresource_flags(res, len, io_decode, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100144}
145
146/**
147 * acpi_dev_resource_io - Extract ACPI I/O resource information.
148 * @ares: Input ACPI resource object.
149 * @res: Output generic resource object.
150 *
151 * Check if the given ACPI resource object represents an I/O resource and
152 * if that's the case, use the information in it to populate the generic
153 * resource object pointed to by @res.
154 */
155bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
156{
157 struct acpi_resource_io *io;
158 struct acpi_resource_fixed_io *fixed_io;
159
160 switch (ares->type) {
161 case ACPI_RESOURCE_TYPE_IO:
162 io = &ares->data.io;
163 acpi_dev_get_ioresource(res, io->minimum,
164 io->address_length,
165 io->io_decode);
166 break;
167 case ACPI_RESOURCE_TYPE_FIXED_IO:
168 fixed_io = &ares->data.fixed_io;
169 acpi_dev_get_ioresource(res, fixed_io->address,
170 fixed_io->address_length,
171 ACPI_DECODE_10);
172 break;
173 default:
174 return false;
175 }
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800176
177 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100178}
179EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
180
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800181static bool acpi_decode_space(struct resource *res,
182 struct acpi_resource_address *addr,
183 struct acpi_address64_attribute *attr)
184{
185 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
186 bool window = addr->producer_consumer == ACPI_PRODUCER;
187 bool wp = addr->info.mem.write_protect;
188 u64 len = attr->address_length;
189
190 res->start = attr->minimum;
191 res->end = attr->maximum;
192
193 switch (addr->resource_type) {
194 case ACPI_MEMORY_RANGE:
195 acpi_dev_memresource_flags(res, len, wp, window);
196 break;
197 case ACPI_IO_RANGE:
198 acpi_dev_ioresource_flags(res, len, iodec, window);
199 break;
200 case ACPI_BUS_NUMBER_RANGE:
201 res->flags = IORESOURCE_BUS;
202 break;
203 default:
204 return false;
205 }
206
207 return !(res->flags & IORESOURCE_DISABLED);
208}
209
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100210/**
211 * acpi_dev_resource_address_space - Extract ACPI address space information.
212 * @ares: Input ACPI resource object.
213 * @res: Output generic resource object.
214 *
215 * Check if the given ACPI resource object represents an address space resource
216 * and if that's the case, use the information in it to populate the generic
217 * resource object pointed to by @res.
218 */
219bool acpi_dev_resource_address_space(struct acpi_resource *ares,
220 struct resource *res)
221{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100222 struct acpi_resource_address64 addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100223
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800224 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
Jiang Liu6658c732014-10-27 13:21:35 +0800225 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100226
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800227 return acpi_decode_space(res, (struct acpi_resource_address *)&addr,
228 &addr.address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100229}
230EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
231
232/**
233 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
234 * @ares: Input ACPI resource object.
235 * @res: Output generic resource object.
236 *
237 * Check if the given ACPI resource object represents an extended address space
238 * resource and if that's the case, use the information in it to populate the
239 * generic resource object pointed to by @res.
240 */
241bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
242 struct resource *res)
243{
244 struct acpi_resource_extended_address64 *ext_addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100245
246 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
247 return false;
248
249 ext_addr = &ares->data.ext_address64;
250
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800251 return acpi_decode_space(res, (struct acpi_resource_address *)ext_addr,
252 &ext_addr->address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100253}
254EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
255
256/**
257 * acpi_dev_irq_flags - Determine IRQ resource flags.
258 * @triggering: Triggering type as provided by ACPI.
259 * @polarity: Interrupt polarity as provided by ACPI.
260 * @shareable: Whether or not the interrupt is shareable.
261 */
262unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
263{
264 unsigned long flags;
265
266 if (triggering == ACPI_LEVEL_SENSITIVE)
267 flags = polarity == ACPI_ACTIVE_LOW ?
268 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
269 else
270 flags = polarity == ACPI_ACTIVE_LOW ?
271 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
272
273 if (shareable == ACPI_SHARED)
274 flags |= IORESOURCE_IRQ_SHAREABLE;
275
276 return flags | IORESOURCE_IRQ;
277}
278EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
279
280static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
281{
282 res->start = gsi;
283 res->end = gsi;
284 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED;
285}
286
287static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000288 u8 triggering, u8 polarity, u8 shareable,
289 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100290{
291 int irq, p, t;
292
293 if (!valid_IRQ(gsi)) {
294 acpi_dev_irqresource_disabled(res, gsi);
295 return;
296 }
297
298 /*
299 * In IO-APIC mode, use overrided attribute. Two reasons:
300 * 1. BIOS bug in DSDT
301 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000302 *
303 * We do this only if we are dealing with IRQ() or IRQNoFlags()
304 * resource (the legacy ISA resources). With modern ACPI 5 devices
305 * using extended IRQ descriptors we take the IRQ configuration
306 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100307 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000308 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100309 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
310 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
311
312 if (triggering != trig || polarity != pol) {
313 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000314 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100315 triggering = trig;
316 polarity = pol;
317 }
318 }
319
320 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
321 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
322 if (irq >= 0) {
323 res->start = irq;
324 res->end = irq;
325 } else {
326 acpi_dev_irqresource_disabled(res, gsi);
327 }
328}
329
330/**
331 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
332 * @ares: Input ACPI resource object.
333 * @index: Index into the array of GSIs represented by the resource.
334 * @res: Output generic resource object.
335 *
336 * Check if the given ACPI resource object represents an interrupt resource
337 * and @index does not exceed the resource's interrupt count (true is returned
338 * in that case regardless of the results of the other checks)). If that's the
339 * case, register the GSI corresponding to @index from the array of interrupts
340 * represented by the resource and populate the generic resource object pointed
341 * to by @res accordingly. If the registration of the GSI is not successful,
342 * IORESOURCE_DISABLED will be set it that object's flags.
343 */
344bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
345 struct resource *res)
346{
347 struct acpi_resource_irq *irq;
348 struct acpi_resource_extended_irq *ext_irq;
349
350 switch (ares->type) {
351 case ACPI_RESOURCE_TYPE_IRQ:
352 /*
353 * Per spec, only one interrupt per descriptor is allowed in
354 * _CRS, but some firmware violates this, so parse them all.
355 */
356 irq = &ares->data.irq;
357 if (index >= irq->interrupt_count) {
358 acpi_dev_irqresource_disabled(res, 0);
359 return false;
360 }
361 acpi_dev_get_irqresource(res, irq->interrupts[index],
362 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000363 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100364 break;
365 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
366 ext_irq = &ares->data.extended_irq;
367 if (index >= ext_irq->interrupt_count) {
368 acpi_dev_irqresource_disabled(res, 0);
369 return false;
370 }
371 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
372 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000373 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100374 break;
375 default:
376 return false;
377 }
378
379 return true;
380}
381EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100382
383/**
384 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
385 * @list: The head of the resource list to free.
386 */
387void acpi_dev_free_resource_list(struct list_head *list)
388{
389 struct resource_list_entry *rentry, *re;
390
391 list_for_each_entry_safe(rentry, re, list, node) {
392 list_del(&rentry->node);
393 kfree(rentry);
394 }
395}
396EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
397
398struct res_proc_context {
399 struct list_head *list;
400 int (*preproc)(struct acpi_resource *, void *);
401 void *preproc_data;
402 int count;
403 int error;
404};
405
406static acpi_status acpi_dev_new_resource_entry(struct resource *r,
407 struct res_proc_context *c)
408{
409 struct resource_list_entry *rentry;
410
411 rentry = kmalloc(sizeof(*rentry), GFP_KERNEL);
412 if (!rentry) {
413 c->error = -ENOMEM;
414 return AE_NO_MEMORY;
415 }
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100416 rentry->res = *r;
417 list_add_tail(&rentry->node, c->list);
418 c->count++;
419 return AE_OK;
420}
421
422static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
423 void *context)
424{
425 struct res_proc_context *c = context;
426 struct resource r;
427 int i;
428
429 if (c->preproc) {
430 int ret;
431
432 ret = c->preproc(ares, c->preproc_data);
433 if (ret < 0) {
434 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100435 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100436 } else if (ret > 0) {
437 return AE_OK;
438 }
439 }
440
441 memset(&r, 0, sizeof(r));
442
443 if (acpi_dev_resource_memory(ares, &r)
444 || acpi_dev_resource_io(ares, &r)
445 || acpi_dev_resource_address_space(ares, &r)
446 || acpi_dev_resource_ext_address_space(ares, &r))
447 return acpi_dev_new_resource_entry(&r, c);
448
449 for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) {
450 acpi_status status;
451
452 status = acpi_dev_new_resource_entry(&r, c);
453 if (ACPI_FAILURE(status))
454 return status;
455 }
456
457 return AE_OK;
458}
459
460/**
461 * acpi_dev_get_resources - Get current resources of a device.
462 * @adev: ACPI device node to get the resources for.
463 * @list: Head of the resultant list of resources (must be empty).
464 * @preproc: The caller's preprocessing routine.
465 * @preproc_data: Pointer passed to the caller's preprocessing routine.
466 *
467 * Evaluate the _CRS method for the given device node and process its output by
468 * (1) executing the @preproc() rountine provided by the caller, passing the
469 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
470 * returned and (2) converting all of the returned ACPI resources into struct
471 * resource objects if possible. If the return value of @preproc() in step (1)
472 * is different from 0, step (2) is not applied to the given ACPI resource and
473 * if that value is negative, the whole processing is aborted and that value is
474 * returned as the final error code.
475 *
476 * The resultant struct resource objects are put on the list pointed to by
477 * @list, that must be empty initially, as members of struct resource_list_entry
478 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
479 * free that list.
480 *
481 * The number of resources in the output list is returned on success, an error
482 * code reflecting the error condition is returned otherwise.
483 */
484int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
485 int (*preproc)(struct acpi_resource *, void *),
486 void *preproc_data)
487{
488 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100489 acpi_status status;
490
491 if (!adev || !adev->handle || !list_empty(list))
492 return -EINVAL;
493
Jiang Liu952c63e2013-06-29 00:24:38 +0800494 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100495 return 0;
496
497 c.list = list;
498 c.preproc = preproc;
499 c.preproc_data = preproc_data;
500 c.count = 0;
501 c.error = 0;
502 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
503 acpi_dev_process_resource, &c);
504 if (ACPI_FAILURE(status)) {
505 acpi_dev_free_resource_list(list);
506 return c.error ? c.error : -EIO;
507 }
508
509 return c.count;
510}
511EXPORT_SYMBOL_GPL(acpi_dev_get_resources);