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