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