]> git.ipfire.org Git - people/ms/linux.git/blob - drivers/acpi/resource.c
drm/i915: add MISSING_CASE to a few port/aux power domain helpers
[people/ms/linux.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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 */
20
21 #include <linux/acpi.h>
22 #include <linux/device.h>
23 #include <linux/export.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26
27 #ifdef CONFIG_X86
28 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
29 #else
30 #define valid_IRQ(i) (true)
31 #endif
32
33 static bool acpi_dev_resource_len_valid(u64 start, u64 end, u64 len, bool io)
34 {
35 u64 reslen = end - start + 1;
36
37 /*
38 * CHECKME: len might be required to check versus a minimum
39 * length as well. 1 for io is fine, but for memory it does
40 * not make any sense at all.
41 * Note: some BIOSes report incorrect length for ACPI address space
42 * descriptor, so remove check of 'reslen == len' to avoid regression.
43 */
44 if (len && reslen && start <= end)
45 return true;
46
47 pr_debug("ACPI: invalid or unassigned resource %s [%016llx - %016llx] length [%016llx]\n",
48 io ? "io" : "mem", start, end, len);
49
50 return false;
51 }
52
53 static void acpi_dev_memresource_flags(struct resource *res, u64 len,
54 u8 write_protect)
55 {
56 res->flags = IORESOURCE_MEM;
57
58 if (!acpi_dev_resource_len_valid(res->start, res->end, len, false))
59 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
60
61 if (write_protect == ACPI_READ_WRITE_MEMORY)
62 res->flags |= IORESOURCE_MEM_WRITEABLE;
63 }
64
65 static void acpi_dev_get_memresource(struct resource *res, u64 start, u64 len,
66 u8 write_protect)
67 {
68 res->start = start;
69 res->end = start + len - 1;
70 acpi_dev_memresource_flags(res, len, write_protect);
71 }
72
73 /**
74 * acpi_dev_resource_memory - Extract ACPI memory resource information.
75 * @ares: Input ACPI resource object.
76 * @res: Output generic resource object.
77 *
78 * Check if the given ACPI resource object represents a memory resource and
79 * if that's the case, use the information in it to populate the generic
80 * resource object pointed to by @res.
81 *
82 * Return:
83 * 1) false with res->flags setting to zero: not the expected resource type
84 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
85 * 3) true: valid assigned resource
86 */
87 bool 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 << 8,
97 memory24->address_length << 8,
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 res->flags = 0;
114 return false;
115 }
116
117 return !(res->flags & IORESOURCE_DISABLED);
118 }
119 EXPORT_SYMBOL_GPL(acpi_dev_resource_memory);
120
121 static void acpi_dev_ioresource_flags(struct resource *res, u64 len,
122 u8 io_decode, u8 translation_type)
123 {
124 res->flags = IORESOURCE_IO;
125
126 if (!acpi_dev_resource_len_valid(res->start, res->end, len, true))
127 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
128
129 if (res->end >= 0x10003)
130 res->flags |= IORESOURCE_DISABLED | IORESOURCE_UNSET;
131
132 if (io_decode == ACPI_DECODE_16)
133 res->flags |= IORESOURCE_IO_16BIT_ADDR;
134 if (translation_type == ACPI_SPARSE_TRANSLATION)
135 res->flags |= IORESOURCE_IO_SPARSE;
136 }
137
138 static void acpi_dev_get_ioresource(struct resource *res, u64 start, u64 len,
139 u8 io_decode)
140 {
141 res->start = start;
142 res->end = start + len - 1;
143 acpi_dev_ioresource_flags(res, len, io_decode, 0);
144 }
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 *
155 * Return:
156 * 1) false with res->flags setting to zero: not the expected resource type
157 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
158 * 3) true: valid assigned resource
159 */
160 bool acpi_dev_resource_io(struct acpi_resource *ares, struct resource *res)
161 {
162 struct acpi_resource_io *io;
163 struct acpi_resource_fixed_io *fixed_io;
164
165 switch (ares->type) {
166 case ACPI_RESOURCE_TYPE_IO:
167 io = &ares->data.io;
168 acpi_dev_get_ioresource(res, io->minimum,
169 io->address_length,
170 io->io_decode);
171 break;
172 case ACPI_RESOURCE_TYPE_FIXED_IO:
173 fixed_io = &ares->data.fixed_io;
174 acpi_dev_get_ioresource(res, fixed_io->address,
175 fixed_io->address_length,
176 ACPI_DECODE_10);
177 break;
178 default:
179 res->flags = 0;
180 return false;
181 }
182
183 return !(res->flags & IORESOURCE_DISABLED);
184 }
185 EXPORT_SYMBOL_GPL(acpi_dev_resource_io);
186
187 static bool acpi_decode_space(struct resource_win *win,
188 struct acpi_resource_address *addr,
189 struct acpi_address64_attribute *attr)
190 {
191 u8 iodec = attr->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16;
192 bool wp = addr->info.mem.write_protect;
193 u64 len = attr->address_length;
194 u64 start, end, offset = 0;
195 struct resource *res = &win->res;
196
197 /*
198 * Filter out invalid descriptor according to ACPI Spec 5.0, section
199 * 6.4.3.5 Address Space Resource Descriptors.
200 */
201 if ((addr->min_address_fixed != addr->max_address_fixed && len) ||
202 (addr->min_address_fixed && addr->max_address_fixed && !len))
203 pr_debug("ACPI: Invalid address space min_addr_fix %d, max_addr_fix %d, len %llx\n",
204 addr->min_address_fixed, addr->max_address_fixed, len);
205
206 /*
207 * For bridges that translate addresses across the bridge,
208 * translation_offset is the offset that must be added to the
209 * address on the secondary side to obtain the address on the
210 * primary side. Non-bridge devices must list 0 for all Address
211 * Translation offset bits.
212 */
213 if (addr->producer_consumer == ACPI_PRODUCER)
214 offset = attr->translation_offset;
215 else if (attr->translation_offset)
216 pr_debug("ACPI: translation_offset(%lld) is invalid for non-bridge device.\n",
217 attr->translation_offset);
218 start = attr->minimum + offset;
219 end = attr->maximum + offset;
220
221 win->offset = offset;
222 res->start = start;
223 res->end = end;
224 if (sizeof(resource_size_t) < sizeof(u64) &&
225 (offset != win->offset || start != res->start || end != res->end)) {
226 pr_warn("acpi resource window ([%#llx-%#llx] ignored, not CPU addressable)\n",
227 attr->minimum, attr->maximum);
228 return false;
229 }
230
231 switch (addr->resource_type) {
232 case ACPI_MEMORY_RANGE:
233 acpi_dev_memresource_flags(res, len, wp);
234 break;
235 case ACPI_IO_RANGE:
236 acpi_dev_ioresource_flags(res, len, iodec,
237 addr->info.io.translation_type);
238 break;
239 case ACPI_BUS_NUMBER_RANGE:
240 res->flags = IORESOURCE_BUS;
241 break;
242 default:
243 return false;
244 }
245
246 if (addr->producer_consumer == ACPI_PRODUCER)
247 res->flags |= IORESOURCE_WINDOW;
248
249 if (addr->info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
250 res->flags |= IORESOURCE_PREFETCH;
251
252 return !(res->flags & IORESOURCE_DISABLED);
253 }
254
255 /**
256 * acpi_dev_resource_address_space - Extract ACPI address space information.
257 * @ares: Input ACPI resource object.
258 * @win: Output generic resource object.
259 *
260 * Check if the given ACPI resource object represents an address space resource
261 * and if that's the case, use the information in it to populate the generic
262 * resource object pointed to by @win.
263 *
264 * Return:
265 * 1) false with win->res.flags setting to zero: not the expected resource type
266 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
267 * resource
268 * 3) true: valid assigned resource
269 */
270 bool acpi_dev_resource_address_space(struct acpi_resource *ares,
271 struct resource_win *win)
272 {
273 struct acpi_resource_address64 addr;
274
275 win->res.flags = 0;
276 if (ACPI_FAILURE(acpi_resource_to_address64(ares, &addr)))
277 return false;
278
279 return acpi_decode_space(win, (struct acpi_resource_address *)&addr,
280 &addr.address);
281 }
282 EXPORT_SYMBOL_GPL(acpi_dev_resource_address_space);
283
284 /**
285 * acpi_dev_resource_ext_address_space - Extract ACPI address space information.
286 * @ares: Input ACPI resource object.
287 * @win: Output generic resource object.
288 *
289 * Check if the given ACPI resource object represents an extended address space
290 * resource and if that's the case, use the information in it to populate the
291 * generic resource object pointed to by @win.
292 *
293 * Return:
294 * 1) false with win->res.flags setting to zero: not the expected resource type
295 * 2) false with IORESOURCE_DISABLED in win->res.flags: valid unassigned
296 * resource
297 * 3) true: valid assigned resource
298 */
299 bool acpi_dev_resource_ext_address_space(struct acpi_resource *ares,
300 struct resource_win *win)
301 {
302 struct acpi_resource_extended_address64 *ext_addr;
303
304 win->res.flags = 0;
305 if (ares->type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64)
306 return false;
307
308 ext_addr = &ares->data.ext_address64;
309
310 return acpi_decode_space(win, (struct acpi_resource_address *)ext_addr,
311 &ext_addr->address);
312 }
313 EXPORT_SYMBOL_GPL(acpi_dev_resource_ext_address_space);
314
315 /**
316 * acpi_dev_irq_flags - Determine IRQ resource flags.
317 * @triggering: Triggering type as provided by ACPI.
318 * @polarity: Interrupt polarity as provided by ACPI.
319 * @shareable: Whether or not the interrupt is shareable.
320 */
321 unsigned long acpi_dev_irq_flags(u8 triggering, u8 polarity, u8 shareable)
322 {
323 unsigned long flags;
324
325 if (triggering == ACPI_LEVEL_SENSITIVE)
326 flags = polarity == ACPI_ACTIVE_LOW ?
327 IORESOURCE_IRQ_LOWLEVEL : IORESOURCE_IRQ_HIGHLEVEL;
328 else
329 flags = polarity == ACPI_ACTIVE_LOW ?
330 IORESOURCE_IRQ_LOWEDGE : IORESOURCE_IRQ_HIGHEDGE;
331
332 if (shareable == ACPI_SHARED)
333 flags |= IORESOURCE_IRQ_SHAREABLE;
334
335 return flags | IORESOURCE_IRQ;
336 }
337 EXPORT_SYMBOL_GPL(acpi_dev_irq_flags);
338
339 static void acpi_dev_irqresource_disabled(struct resource *res, u32 gsi)
340 {
341 res->start = gsi;
342 res->end = gsi;
343 res->flags = IORESOURCE_IRQ | IORESOURCE_DISABLED | IORESOURCE_UNSET;
344 }
345
346 static void acpi_dev_get_irqresource(struct resource *res, u32 gsi,
347 u8 triggering, u8 polarity, u8 shareable,
348 bool legacy)
349 {
350 int irq, p, t;
351
352 if (!valid_IRQ(gsi)) {
353 acpi_dev_irqresource_disabled(res, gsi);
354 return;
355 }
356
357 /*
358 * In IO-APIC mode, use overrided attribute. Two reasons:
359 * 1. BIOS bug in DSDT
360 * 2. BIOS uses IO-APIC mode Interrupt Source Override
361 *
362 * We do this only if we are dealing with IRQ() or IRQNoFlags()
363 * resource (the legacy ISA resources). With modern ACPI 5 devices
364 * using extended IRQ descriptors we take the IRQ configuration
365 * from _CRS directly.
366 */
367 if (legacy && !acpi_get_override_irq(gsi, &t, &p)) {
368 u8 trig = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
369 u8 pol = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
370
371 if (triggering != trig || polarity != pol) {
372 pr_warning("ACPI: IRQ %d override to %s, %s\n", gsi,
373 t ? "level" : "edge", p ? "low" : "high");
374 triggering = trig;
375 polarity = pol;
376 }
377 }
378
379 res->flags = acpi_dev_irq_flags(triggering, polarity, shareable);
380 irq = acpi_register_gsi(NULL, gsi, triggering, polarity);
381 if (irq >= 0) {
382 res->start = irq;
383 res->end = irq;
384 } else {
385 acpi_dev_irqresource_disabled(res, gsi);
386 }
387 }
388
389 /**
390 * acpi_dev_resource_interrupt - Extract ACPI interrupt resource information.
391 * @ares: Input ACPI resource object.
392 * @index: Index into the array of GSIs represented by the resource.
393 * @res: Output generic resource object.
394 *
395 * Check if the given ACPI resource object represents an interrupt resource
396 * and @index does not exceed the resource's interrupt count (true is returned
397 * in that case regardless of the results of the other checks)). If that's the
398 * case, register the GSI corresponding to @index from the array of interrupts
399 * represented by the resource and populate the generic resource object pointed
400 * to by @res accordingly. If the registration of the GSI is not successful,
401 * IORESOURCE_DISABLED will be set it that object's flags.
402 *
403 * Return:
404 * 1) false with res->flags setting to zero: not the expected resource type
405 * 2) false with IORESOURCE_DISABLED in res->flags: valid unassigned resource
406 * 3) true: valid assigned resource
407 */
408 bool acpi_dev_resource_interrupt(struct acpi_resource *ares, int index,
409 struct resource *res)
410 {
411 struct acpi_resource_irq *irq;
412 struct acpi_resource_extended_irq *ext_irq;
413
414 switch (ares->type) {
415 case ACPI_RESOURCE_TYPE_IRQ:
416 /*
417 * Per spec, only one interrupt per descriptor is allowed in
418 * _CRS, but some firmware violates this, so parse them all.
419 */
420 irq = &ares->data.irq;
421 if (index >= irq->interrupt_count) {
422 acpi_dev_irqresource_disabled(res, 0);
423 return false;
424 }
425 acpi_dev_get_irqresource(res, irq->interrupts[index],
426 irq->triggering, irq->polarity,
427 irq->sharable, true);
428 break;
429 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
430 ext_irq = &ares->data.extended_irq;
431 if (index >= ext_irq->interrupt_count) {
432 acpi_dev_irqresource_disabled(res, 0);
433 return false;
434 }
435 acpi_dev_get_irqresource(res, ext_irq->interrupts[index],
436 ext_irq->triggering, ext_irq->polarity,
437 ext_irq->sharable, false);
438 break;
439 default:
440 res->flags = 0;
441 return false;
442 }
443
444 return true;
445 }
446 EXPORT_SYMBOL_GPL(acpi_dev_resource_interrupt);
447
448 /**
449 * acpi_dev_free_resource_list - Free resource from %acpi_dev_get_resources().
450 * @list: The head of the resource list to free.
451 */
452 void acpi_dev_free_resource_list(struct list_head *list)
453 {
454 resource_list_free(list);
455 }
456 EXPORT_SYMBOL_GPL(acpi_dev_free_resource_list);
457
458 struct res_proc_context {
459 struct list_head *list;
460 int (*preproc)(struct acpi_resource *, void *);
461 void *preproc_data;
462 int count;
463 int error;
464 };
465
466 static acpi_status acpi_dev_new_resource_entry(struct resource_win *win,
467 struct res_proc_context *c)
468 {
469 struct resource_entry *rentry;
470
471 rentry = resource_list_create_entry(NULL, 0);
472 if (!rentry) {
473 c->error = -ENOMEM;
474 return AE_NO_MEMORY;
475 }
476 *rentry->res = win->res;
477 rentry->offset = win->offset;
478 resource_list_add_tail(rentry, c->list);
479 c->count++;
480 return AE_OK;
481 }
482
483 static acpi_status acpi_dev_process_resource(struct acpi_resource *ares,
484 void *context)
485 {
486 struct res_proc_context *c = context;
487 struct resource_win win;
488 struct resource *res = &win.res;
489 int i;
490
491 if (c->preproc) {
492 int ret;
493
494 ret = c->preproc(ares, c->preproc_data);
495 if (ret < 0) {
496 c->error = ret;
497 return AE_CTRL_TERMINATE;
498 } else if (ret > 0) {
499 return AE_OK;
500 }
501 }
502
503 memset(&win, 0, sizeof(win));
504
505 if (acpi_dev_resource_memory(ares, res)
506 || acpi_dev_resource_io(ares, res)
507 || acpi_dev_resource_address_space(ares, &win)
508 || acpi_dev_resource_ext_address_space(ares, &win))
509 return acpi_dev_new_resource_entry(&win, c);
510
511 for (i = 0; acpi_dev_resource_interrupt(ares, i, res); i++) {
512 acpi_status status;
513
514 status = acpi_dev_new_resource_entry(&win, c);
515 if (ACPI_FAILURE(status))
516 return status;
517 }
518
519 return AE_OK;
520 }
521
522 /**
523 * acpi_dev_get_resources - Get current resources of a device.
524 * @adev: ACPI device node to get the resources for.
525 * @list: Head of the resultant list of resources (must be empty).
526 * @preproc: The caller's preprocessing routine.
527 * @preproc_data: Pointer passed to the caller's preprocessing routine.
528 *
529 * Evaluate the _CRS method for the given device node and process its output by
530 * (1) executing the @preproc() rountine provided by the caller, passing the
531 * resource pointer and @preproc_data to it as arguments, for each ACPI resource
532 * returned and (2) converting all of the returned ACPI resources into struct
533 * resource objects if possible. If the return value of @preproc() in step (1)
534 * is different from 0, step (2) is not applied to the given ACPI resource and
535 * if that value is negative, the whole processing is aborted and that value is
536 * returned as the final error code.
537 *
538 * The resultant struct resource objects are put on the list pointed to by
539 * @list, that must be empty initially, as members of struct resource_entry
540 * objects. Callers of this routine should use %acpi_dev_free_resource_list() to
541 * free that list.
542 *
543 * The number of resources in the output list is returned on success, an error
544 * code reflecting the error condition is returned otherwise.
545 */
546 int acpi_dev_get_resources(struct acpi_device *adev, struct list_head *list,
547 int (*preproc)(struct acpi_resource *, void *),
548 void *preproc_data)
549 {
550 struct res_proc_context c;
551 acpi_status status;
552
553 if (!adev || !adev->handle || !list_empty(list))
554 return -EINVAL;
555
556 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS))
557 return 0;
558
559 c.list = list;
560 c.preproc = preproc;
561 c.preproc_data = preproc_data;
562 c.count = 0;
563 c.error = 0;
564 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
565 acpi_dev_process_resource, &c);
566 if (ACPI_FAILURE(status)) {
567 acpi_dev_free_resource_list(list);
568 return c.error ? c.error : -EIO;
569 }
570
571 return c.count;
572 }
573 EXPORT_SYMBOL_GPL(acpi_dev_get_resources);
574
575 /**
576 * acpi_dev_filter_resource_type - Filter ACPI resource according to resource
577 * types
578 * @ares: Input ACPI resource object.
579 * @types: Valid resource types of IORESOURCE_XXX
580 *
581 * This is a helper function to support acpi_dev_get_resources(), which filters
582 * ACPI resource objects according to resource types.
583 */
584 int acpi_dev_filter_resource_type(struct acpi_resource *ares,
585 unsigned long types)
586 {
587 unsigned long type = 0;
588
589 switch (ares->type) {
590 case ACPI_RESOURCE_TYPE_MEMORY24:
591 case ACPI_RESOURCE_TYPE_MEMORY32:
592 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
593 type = IORESOURCE_MEM;
594 break;
595 case ACPI_RESOURCE_TYPE_IO:
596 case ACPI_RESOURCE_TYPE_FIXED_IO:
597 type = IORESOURCE_IO;
598 break;
599 case ACPI_RESOURCE_TYPE_IRQ:
600 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
601 type = IORESOURCE_IRQ;
602 break;
603 case ACPI_RESOURCE_TYPE_DMA:
604 case ACPI_RESOURCE_TYPE_FIXED_DMA:
605 type = IORESOURCE_DMA;
606 break;
607 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
608 type = IORESOURCE_REG;
609 break;
610 case ACPI_RESOURCE_TYPE_ADDRESS16:
611 case ACPI_RESOURCE_TYPE_ADDRESS32:
612 case ACPI_RESOURCE_TYPE_ADDRESS64:
613 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
614 if (ares->data.address.resource_type == ACPI_MEMORY_RANGE)
615 type = IORESOURCE_MEM;
616 else if (ares->data.address.resource_type == ACPI_IO_RANGE)
617 type = IORESOURCE_IO;
618 else if (ares->data.address.resource_type ==
619 ACPI_BUS_NUMBER_RANGE)
620 type = IORESOURCE_BUS;
621 break;
622 default:
623 break;
624 }
625
626 return (type & types) ? 0 : 1;
627 }
628 EXPORT_SYMBOL_GPL(acpi_dev_filter_resource_type);