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