blob: 16d291c602771cda4492410a5144cd71d79eeaba [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
181/**
182 * acpi_dev_resource_address_space - Extract ACPI address space information.
183 * @ares: Input ACPI resource object.
184 * @res: Output generic resource object.
185 *
186 * Check if the given ACPI resource object represents an address space resource
187 * and if that's the case, use the information in it to populate the generic
188 * resource object pointed to by @res.
189 */
190bool acpi_dev_resource_address_space(struct acpi_resource *ares,
191 struct resource *res)
192{
193 acpi_status status;
194 struct acpi_resource_address64 addr;
195 bool window;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100196 u8 io_decode;
197
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100198 status = acpi_resource_to_address64(ares, &addr);
199 if (ACPI_FAILURE(status))
Jiang Liu6658c732014-10-27 13:21:35 +0800200 return false;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100201
Lv Zhenga45de932015-01-26 16:58:56 +0800202 res->start = addr.address.minimum;
203 res->end = addr.address.maximum;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100204 window = addr.producer_consumer == ACPI_PRODUCER;
205
206 switch(addr.resource_type) {
207 case ACPI_MEMORY_RANGE:
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800208 acpi_dev_memresource_flags(res, addr.address.address_length,
209 addr.info.mem.write_protect,
210 window);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100211 break;
212 case ACPI_IO_RANGE:
Lv Zhenga45de932015-01-26 16:58:56 +0800213 io_decode = addr.address.granularity == 0xfff ?
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100214 ACPI_DECODE_10 : ACPI_DECODE_16;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800215 acpi_dev_ioresource_flags(res, addr.address.address_length,
216 io_decode, window);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100217 break;
218 case ACPI_BUS_NUMBER_RANGE:
219 res->flags = IORESOURCE_BUS;
220 break;
221 default:
222 res->flags = 0;
223 }
224
225 return true;
226}
227EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
228
229/**
230 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
231 * @ares: Input ACPI resource object.
232 * @res: Output generic resource object.
233 *
234 * Check if the given ACPI resource object represents an extended address space
235 * resource and if that's the case, use the information in it to populate the
236 * generic resource object pointed to by @res.
237 */
238bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
239 struct resource *res)
240{
241 struct acpi_resource_extended_address64 *ext_addr;
242 bool window;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100243 u8 io_decode;
244
245 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
246 return false;
247
248 ext_addr = &ares->data.ext_address64;
249
Lv Zhenga45de932015-01-26 16:58:56 +0800250 res->start = ext_addr->address.minimum;
251 res->end = ext_addr->address.maximum;
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100252 window = ext_addr->producer_consumer == ACPI_PRODUCER;
253
254 switch(ext_addr->resource_type) {
255 case ACPI_MEMORY_RANGE:
Thomas Gleixnerc420dbd2015-02-02 10:42:48 +0800256 acpi_dev_memresource_flags(res,
257 ext_addr->address.address_length,
258 ext_addr->info.mem.write_protect,
259 window);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100260 break;
261 case ACPI_IO_RANGE:
Lv Zhenga45de932015-01-26 16:58:56 +0800262 io_decode = ext_addr->address.granularity == 0xfff ?
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100263 ACPI_DECODE_10 : ACPI_DECODE_16;
Thomas Gleixner1d0420f2015-02-02 10:42:49 +0800264 acpi_dev_ioresource_flags(res, ext_addr->address.address_length,
265 io_decode, window);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100266 break;
267 case ACPI_BUS_NUMBER_RANGE:
268 res->flags = IORESOURCE_BUS;
269 break;
270 default:
271 res->flags = 0;
272 }
273
274 return true;
275}
276EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
277
278/**
279 * acpi_dev_irq_flags - Determine IRQ resource flags.
280 * @triggering: Triggering type as provided by ACPI.
281 * @polarity: Interrupt polarity as provided by ACPI.
282 * @shareable: Whether or not the interrupt is shareable.
283 */
284unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
285{
286 unsigned long flags;
287
288 if (triggering == ACPI_LEVEL_SENSITIVE)
289 flags = polarity == ACPI_ACTIVE_LOW ?
290 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
291 else
292 flags = polarity == ACPI_ACTIVE_LOW ?
293 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
294
295 if (shareable == ACPI_SHARED)
296 flags |= IORESOURCE_IRQ_SHAREABLE;
297
298 return flags | IORESOURCE_IRQ;
299}
300EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
301
302static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
303{
304 res->start = gsi;
305 res->end = gsi;
306 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED;
307}
308
309static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000310 u8 triggering, u8 polarity, u8 shareable,
311 bool legacy)
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100312{
313 int irq, p, t;
314
315 if (!valid_IRQ(gsi)) {
316 acpi_dev_irqresource_disabled(res, gsi);
317 return;
318 }
319
320 /*
321 * In IO-APIC mode, use overrided attribute. Two reasons:
322 * 1. BIOS bug in DSDT
323 * 2. BIOS uses IO-APIC mode Interrupt Source Override
Mika Westerberg204ebc02013-05-20 15:41:45 +0000324 *
325 * We do this only if we are dealing with IRQ() or IRQNoFlags()
326 * resource (the legacy ISA resources). With modern ACPI 5 devices
327 * using extended IRQ descriptors we take the IRQ configuration
328 * from _CRS directly.
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100329 */
Mika Westerberg204ebc02013-05-20 15:41:45 +0000330 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100331 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
332 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
333
334 if (triggering != trig || polarity != pol) {
335 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000336 t ? "level" : "edge", p ? "low" : "high");
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100337 triggering = trig;
338 polarity = pol;
339 }
340 }
341
342 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
343 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
344 if (irq >= 0) {
345 res->start = irq;
346 res->end = irq;
347 } else {
348 acpi_dev_irqresource_disabled(res, gsi);
349 }
350}
351
352/**
353 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
354 * @ares: Input ACPI resource object.
355 * @index: Index into the array of GSIs represented by the resource.
356 * @res: Output generic resource object.
357 *
358 * Check if the given ACPI resource object represents an interrupt resource
359 * and @index does not exceed the resource's interrupt count (true is returned
360 * in that case regardless of the results of the other checks)). If that's the
361 * case, register the GSI corresponding to @index from the array of interrupts
362 * represented by the resource and populate the generic resource object pointed
363 * to by @res accordingly. If the registration of the GSI is not successful,
364 * IORESOURCE_DISABLED will be set it that object's flags.
365 */
366bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
367 struct resource *res)
368{
369 struct acpi_resource_irq *irq;
370 struct acpi_resource_extended_irq *ext_irq;
371
372 switch (ares->type) {
373 case ACPI_RESOURCE_TYPE_IRQ:
374 /*
375 * Per spec, only one interrupt per descriptor is allowed in
376 * _CRS, but some firmware violates this, so parse them all.
377 */
378 irq = &ares->data.irq;
379 if (index >= irq->interrupt_count) {
380 acpi_dev_irqresource_disabled(res, 0);
381 return false;
382 }
383 acpi_dev_get_irqresource(res, irq->interrupts[index],
384 irq->triggering, irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000385 irq->sharable, true);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100386 break;
387 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
388 ext_irq = &ares->data.extended_irq;
389 if (index >= ext_irq->interrupt_count) {
390 acpi_dev_irqresource_disabled(res, 0);
391 return false;
392 }
393 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
394 ext_irq->triggering, ext_irq->polarity,
Mika Westerberg204ebc02013-05-20 15:41:45 +0000395 ext_irq->sharable, false);
Rafael J. Wysocki046d9ce2012-11-15 00:30:01 +0100396 break;
397 default:
398 return false;
399 }
400
401 return true;
402}
403EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100404
405/**
406 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
407 * @list: The head of the resource list to free.
408 */
409void acpi_dev_free_resource_list(struct list_head *list)
410{
411 struct resource_list_entry *rentry, *re;
412
413 list_for_each_entry_safe(rentry, re, list, node) {
414 list_del(&rentry->node);
415 kfree(rentry);
416 }
417}
418EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
419
420struct res_proc_context {
421 struct list_head *list;
422 int (*preproc)(struct acpi_resource *, void *);
423 void *preproc_data;
424 int count;
425 int error;
426};
427
428static acpi_status acpi_dev_new_resource_entry(struct resource *r,
429 struct res_proc_context *c)
430{
431 struct resource_list_entry *rentry;
432
433 rentry = kmalloc(sizeof(*rentry), GFP_KERNEL);
434 if (!rentry) {
435 c->error = -ENOMEM;
436 return AE_NO_MEMORY;
437 }
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100438 rentry->res = *r;
439 list_add_tail(&rentry->node, c->list);
440 c->count++;
441 return AE_OK;
442}
443
444static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
445 void *context)
446{
447 struct res_proc_context *c = context;
448 struct resource r;
449 int i;
450
451 if (c->preproc) {
452 int ret;
453
454 ret = c->preproc(ares, c->preproc_data);
455 if (ret < 0) {
456 c->error = ret;
Rafael J. Wysocki8a667902012-11-16 21:55:48 +0100457 return AE_CTRL_TERMINATE;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100458 } else if (ret > 0) {
459 return AE_OK;
460 }
461 }
462
463 memset(&r, 0, sizeof(r));
464
465 if (acpi_dev_resource_memory(ares, &r)
466 || acpi_dev_resource_io(ares, &r)
467 || acpi_dev_resource_address_space(ares, &r)
468 || acpi_dev_resource_ext_address_space(ares, &r))
469 return acpi_dev_new_resource_entry(&r, c);
470
471 for (i = 0; acpi_dev_resource_interrupt(ares, i, &r); i++) {
472 acpi_status status;
473
474 status = acpi_dev_new_resource_entry(&r, c);
475 if (ACPI_FAILURE(status))
476 return status;
477 }
478
479 return AE_OK;
480}
481
482/**
483 * acpi_dev_get_resources - Get current resources of a device.
484 * @adev: ACPI device node to get the resources for.
485 * @list: Head of the resultant list of resources (must be empty).
486 * @preproc: The caller's preprocessing routine.
487 * @preproc_data: Pointer passed to the caller's preprocessing routine.
488 *
489 * Evaluate the _CRS method for the given device node and process its output by
490 * (1) executing the @preproc() rountine provided by the caller, passing the
491 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
492 * returned and (2) converting all of the returned ACPI resources into struct
493 * resource objects if possible. If the return value of @preproc() in step (1)
494 * is different from 0, step (2) is not applied to the given ACPI resource and
495 * if that value is negative, the whole processing is aborted and that value is
496 * returned as the final error code.
497 *
498 * The resultant struct resource objects are put on the list pointed to by
499 * @list, that must be empty initially, as members of struct resource_list_entry
500 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
501 * free that list.
502 *
503 * The number of resources in the output list is returned on success, an error
504 * code reflecting the error condition is returned otherwise.
505 */
506int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
507 int (*preproc)(struct acpi_resource *, void *),
508 void *preproc_data)
509{
510 struct res_proc_context c;
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100511 acpi_status status;
512
513 if (!adev || !adev->handle || !list_empty(list))
514 return -EINVAL;
515
Jiang Liu952c63e2013-06-29 00:24:38 +0800516 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
Rafael J. Wysocki8e345c92012-11-15 00:30:21 +0100517 return 0;
518
519 c.list = list;
520 c.preproc = preproc;
521 c.preproc_data = preproc_data;
522 c.count = 0;
523 c.error = 0;
524 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
525 acpi_dev_process_resource, &c);
526 if (ACPI_FAILURE(status)) {
527 acpi_dev_free_resource_list(list);
528 return c.error ? c.error : -EIO;
529 }
530
531 return c.count;
532}
533EXPORT_SYMBOL_GPL(acpi_dev_get_resources);