blob: 7ce00a63f6957610f1f059edabf19d79ef0c56f6 [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,
Thomas Gleixner72e26b02015-02-02 10:42:52 +080056 u8 write_protect)
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +080057{
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
67static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
68 u8 write_protect)
69{
70 res->start = start;
71 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +080072 acpi_dev_memresource_flags(res, len, write_protect);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +010073}
74
75/**
76 * acpi_dev_resource_memory - Extract ACPI memory resource information.
77 * @ares: Input ACPI resource object.
78 * @res: Output generic resource object.
79 *
80 * Check if the given ACPI resource object represents a memory resource and
81 * if that's the case, use the information in it to populate the generic
82 * resource object pointed to by @res.
83 */
84bool acpi_dev_resource_memory(struct acpi_resource *ares, struct resource *res)
85{
86 struct acpi_resource_memory24 *memory24;
87 struct acpi_resource_memory32 *memory32;
88 struct acpi_resource_fixed_memory32 *fixed_memory32;
89
90 switch (ares->type) {
91 case ACPI_RESOURCE_TYPE_MEMORY24:
92 memory24 = &ares->data.memory24;
93 acpi_dev_get_memresource(res, memory24->minimum,
94 memory24->address_length,
95 memory24->write_protect);
96 break;
97 case ACPI_RESOURCE_TYPE_MEMORY32:
98 memory32 = &ares->data.memory32;
99 acpi_dev_get_memresource(res, memory32->minimum,
100 memory32->address_length,
101 memory32->write_protect);
102 break;
103 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
104 fixed_memory32 = &ares->data.fixed_memory32;
105 acpi_dev_get_memresource(res, fixed_memory32->address,
106 fixed_memory32->address_length,
107 fixed_memory32->write_protect);
108 break;
109 default:
110 return false;
111 }
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800112
113 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100114}
115EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
116
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800117static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800118 u8 io_decode)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100119{
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800120 res->flags = IORESOURCE_IO;
121
122 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
123 res->flags |= IORESOURCE_DISABLED;
124
125 if (res->end >= 0x10003)
126 res->flags |= IORESOURCE_DISABLED;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100127
128 if (io_decode == ACPI_DECODE_16)
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800129 res->flags |= IORESOURCE_IO_16BIT_ADDR;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100130}
131
132static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
133 u8 io_decode)
134{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100135 res->start = start;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800136 res->end = start + len - 1;
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800137 acpi_dev_ioresource_flags(res, len, io_decode);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100138}
139
140/**
141 * acpi_dev_resource_io - Extract ACPI I/O resource information.
142 * @ares: Input ACPI resource object.
143 * @res: Output generic resource object.
144 *
145 * Check if the given ACPI resource object represents an I/O resource and
146 * if that's the case, use the information in it to populate the generic
147 * resource object pointed to by @res.
148 */
149bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
150{
151 struct acpi_resource_io *io;
152 struct acpi_resource_fixed_io *fixed_io;
153
154 switch (ares->type) {
155 case ACPI_RESOURCE_TYPE_IO:
156 io = &ares->data.io;
157 acpi_dev_get_ioresource(res, io->minimum,
158 io->address_length,
159 io->io_decode);
160 break;
161 case ACPI_RESOURCE_TYPE_FIXED_IO:
162 fixed_io = &ares->data.fixed_io;
163 acpi_dev_get_ioresource(res, fixed_io->address,
164 fixed_io->address_length,
165 ACPI_DECODE_10);
166 break;
167 default:
168 return false;
169 }
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800170
171 return !(res->flags & IORESOURCE_DISABLED);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100172}
173EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
174
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800175static bool acpi_decode_space(struct resource *res,
176 struct acpi_resource_address *addr,
177 struct acpi_address64_attribute *attr)
178{
179 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800180 bool wp = addr->info.mem.write_protect;
181 u64 len = attr->address_length;
182
183 res->start = attr->minimum;
184 res->end = attr->maximum;
185
186 switch (addr->resource_type) {
187 case ACPI_MEMORY_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800188 acpi_dev_memresource_flags(res, len, wp);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800189 break;
190 case ACPI_IO_RANGE:
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800191 acpi_dev_ioresource_flags(res, len, iodec);
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800192 break;
193 case ACPI_BUS_NUMBER_RANGE:
194 res->flags = IORESOURCE_BUS;
195 break;
196 default:
197 return false;
198 }
199
Thomas Gleixner72e26b02015-02-02 10:42:52 +0800200 if (addr->producer_consumer == ACPI_PRODUCER)
201 res->flags |= IORESOURCE_WINDOW;
202
Thomas Gleixnerfcb29bb2015-02-02 10:42:53 +0800203 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
204 res->flags |= IORESOURCE_PREFETCH;
205
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800206 return !(res->flags & IORESOURCE_DISABLED);
207}
208
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100209/**
210 * acpi_dev_resource_address_space - Extract ACPI address space information.
211 * @ares: Input ACPI resource object.
212 * @res: Output generic resource object.
213 *
214 * Check if the given ACPI resource object represents an address space resource
215 * and if that's the case, use the information in it to populate the generic
216 * resource object pointed to by @res.
217 */
218bool acpi_dev_resource_address_space(struct acpi_resource *ares,
219 struct resource *res)
220{
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100221 struct acpi_resource_address64 addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100222
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800223 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
Jiang Liu6658c732014-10-27 13:21:35 +0800224 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100225
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800226 return acpi_decode_space(res, (struct acpi_resource_address *)&addr,
227 &addr.address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100228}
229EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
230
231/**
232 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
233 * @ares: Input ACPI resource object.
234 * @res: Output generic resource object.
235 *
236 * Check if the given ACPI resource object represents an extended address space
237 * resource and if that's the case, use the information in it to populate the
238 * generic resource object pointed to by @res.
239 */
240bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
241 struct resource *res)
242{
243 struct acpi_resource_extended_address64 *ext_addr;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100244
245 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
246 return false;
247
248 ext_addr = &ares->data.ext_address64;
249
Thomas Gleixnereb76d552015-02-02 10:42:51 +0800250 return acpi_decode_space(res, (struct acpi_resource_address *)ext_addr,
251 &ext_addr->address);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100252}
253EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
254
255/**
256 * acpi_dev_irq_flags - Determine IRQ resource flags.
257 * @triggering: Triggering type as provided by ACPI.
258 * @polarity: Interrupt polarity as provided by ACPI.
259 * @shareable: Whether or not the interrupt is shareable.
260 */
261unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
262{
263 unsigned long flags;
264
265 if (triggering == ACPI_LEVEL_SENSITIVE)
266 flags = polarity == ACPI_ACTIVE_LOW ?
267 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
268 else
269 flags = polarity == ACPI_ACTIVE_LOW ?
270 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
271
272 if (shareable == ACPI_SHARED)
273 flags |= IORESOURCE_IRQ_SHAREABLE;
274
275 return flags | IORESOURCE_IRQ;
276}
277EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
278
279static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
280{
281 res->start = gsi;
282 res->end = gsi;
283 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED;
284}
285
286static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000287 u8 triggering, u8 polarity, u8 shareable,
288 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100289{
290 int irq, p, t;
291
292 if (!valid_IRQ(gsi)) {
293 acpi_dev_irqresource_disabled(res, gsi);
294 return;
295 }
296
297 /*
298 * In IO-APIC mode, use overrided attribute. Two reasons:
299 * 1. BIOS bug in DSDT
300 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000301 *
302 * We do this only if we are dealing with IRQ() or IRQNoFlags()
303 * resource (the legacy ISA resources). With modern ACPI 5 devices
304 * using extended IRQ descriptors we take the IRQ configuration
305 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100306 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000307 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100308 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
309 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
310
311 if (triggering != trig || polarity != pol) {
312 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000313 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100314 triggering = trig;
315 polarity = pol;
316 }
317 }
318
319 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
320 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
321 if (irq >= 0) {
322 res->start = irq;
323 res->end = irq;
324 } else {
325 acpi_dev_irqresource_disabled(res, gsi);
326 }
327}
328
329/**
330 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
331 * @ares: Input ACPI resource object.
332 * @index: Index into the array of GSIs represented by the resource.
333 * @res: Output generic resource object.
334 *
335 * Check if the given ACPI resource object represents an interrupt resource
336 * and @index does not exceed the resource's interrupt count (true is returned
337 * in that case regardless of the results of the other checks)). If that's the
338 * case, register the GSI corresponding to @index from the array of interrupts
339 * represented by the resource and populate the generic resource object pointed
340 * to by @res accordingly. If the registration of the GSI is not successful,
341 * IORESOURCE_DISABLED will be set it that object's flags.
342 */
343bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
344 struct resource *res)
345{
346 struct acpi_resource_irq *irq;
347 struct acpi_resource_extended_irq *ext_irq;
348
349 switch (ares->type) {
350 case ACPI_RESOURCE_TYPE_IRQ:
351 /*
352 * Per spec, only one interrupt per descriptor is allowed in
353 * _CRS, but some firmware violates this, so parse them all.
354 */
355 irq = &ares->data.irq;
356 if (index >= irq->interrupt_count) {
357 acpi_dev_irqresource_disabled(res, 0);
358 return false;
359 }
360 acpi_dev_get_irqresource(res, irq->interrupts[index],
361 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000362 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100363 break;
364 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
365 ext_irq = &ares->data.extended_irq;
366 if (index >= ext_irq->interrupt_count) {
367 acpi_dev_irqresource_disabled(res, 0);
368 return false;
369 }
370 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
371 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000372 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100373 break;
374 default:
375 return false;
376 }
377
378 return true;
379}
380EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100381
382/**
383 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
384 * @list: The head of the resource list to free.
385 */
386void acpi_dev_free_resource_list(struct list_head *list)
387{
388 struct resource_list_entry *rentry, *re;
389
390 list_for_each_entry_safe(rentry, re, list, node) {
391 list_del(&rentry->node);
392 kfree(rentry);
393 }
394}
395EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
396
397struct res_proc_context {
398 struct list_head *list;
399 int (*preproc)(struct acpi_resource *, void *);
400 void *preproc_data;
401 int count;
402 int error;
403};
404
405static acpi_status acpi_dev_new_resource_entry(struct resource *r,
406 struct res_proc_context *c)
407{
408 struct resource_list_entry *rentry;
409
410 rentry = kmalloc(sizeof(*rentry), GFP_KERNEL);
411 if (!rentry) {
412 c->error = -ENOMEM;
413 return AE_NO_MEMORY;
414 }
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100415 rentry->res = *r;
416 list_add_tail(&rentry->node, c->list);
417 c->count++;
418 return AE_OK;
419}
420
421static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
422 void *context)
423{
424 struct res_proc_context *c = context;
425 struct resource r;
426 int i;
427
428 if (c->preproc) {
429 int ret;
430
431 ret = c->preproc(ares, c->preproc_data);
432 if (ret < 0) {
433 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100434 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100435 } else if (ret > 0) {
436 return AE_OK;
437 }
438 }
439
440 memset(&r, 0, sizeof(r));
441
442 if (acpi_dev_resource_memory(ares, &r)
443 || acpi_dev_resource_io(ares, &r)
444 || acpi_dev_resource_address_space(ares, &r)
445 || acpi_dev_resource_ext_address_space(ares, &r))
446 return acpi_dev_new_resource_entry(&r, c);
447
448 for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) {
449 acpi_status status;
450
451 status = acpi_dev_new_resource_entry(&r, c);
452 if (ACPI_FAILURE(status))
453 return status;
454 }
455
456 return AE_OK;
457}
458
459/**
460 * acpi_dev_get_resources - Get current resources of a device.
461 * @adev: ACPI device node to get the resources for.
462 * @list: Head of the resultant list of resources (must be empty).
463 * @preproc: The caller's preprocessing routine.
464 * @preproc_data: Pointer passed to the caller's preprocessing routine.
465 *
466 * Evaluate the _CRS method for the given device node and process its output by
467 * (1) executing the @preproc() rountine provided by the caller, passing the
468 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
469 * returned and (2) converting all of the returned ACPI resources into struct
470 * resource objects if possible. If the return value of @preproc() in step (1)
471 * is different from 0, step (2) is not applied to the given ACPI resource and
472 * if that value is negative, the whole processing is aborted and that value is
473 * returned as the final error code.
474 *
475 * The resultant struct resource objects are put on the list pointed to by
476 * @list, that must be empty initially, as members of struct resource_list_entry
477 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
478 * free that list.
479 *
480 * The number of resources in the output list is returned on success, an error
481 * code reflecting the error condition is returned otherwise.
482 */
483int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
484 int (*preproc)(struct acpi_resource *, void *),
485 void *preproc_data)
486{
487 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100488 acpi_status status;
489
490 if (!adev || !adev->handle || !list_empty(list))
491 return -EINVAL;
492
Jiang Liu952c63e2013-06-29 00:24:38 +0800493 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100494 return 0;
495
496 c.list = list;
497 c.preproc = preproc;
498 c.preproc_data = preproc_data;
499 c.count = 0;
500 c.error = 0;
501 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
502 acpi_dev_process_resource, &c);
503 if (ACPI_FAILURE(status)) {
504 acpi_dev_free_resource_list(list);
505 return c.error ? c.error : -EIO;
506 }
507
508 return c.count;
509}
510EXPORT_SYMBOL_GPL(acpi_dev_get_resources);