xref: /openbmc/linux/drivers/pnp/pnpacpi/rsparser.c (revision 80ecbd24)
1 /*
2  * pnpacpi -- PnP ACPI driver
3  *
4  * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5  * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
6  * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7  *	Bjorn Helgaas <bjorn.helgaas@hp.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2, or (at your option) any
12  * later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 #include <linux/kernel.h>
24 #include <linux/acpi.h>
25 #include <linux/pci.h>
26 #include <linux/pnp.h>
27 #include <linux/slab.h>
28 #include "../base.h"
29 #include "pnpacpi.h"
30 
31 static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
32 			     int *polarity, int *shareable)
33 {
34 	switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
35 			 IORESOURCE_IRQ_LOWEDGE  | IORESOURCE_IRQ_HIGHEDGE)) {
36 	case IORESOURCE_IRQ_LOWLEVEL:
37 		*triggering = ACPI_LEVEL_SENSITIVE;
38 		*polarity = ACPI_ACTIVE_LOW;
39 		break;
40 	case IORESOURCE_IRQ_HIGHLEVEL:
41 		*triggering = ACPI_LEVEL_SENSITIVE;
42 		*polarity = ACPI_ACTIVE_HIGH;
43 		break;
44 	case IORESOURCE_IRQ_LOWEDGE:
45 		*triggering = ACPI_EDGE_SENSITIVE;
46 		*polarity = ACPI_ACTIVE_LOW;
47 		break;
48 	case IORESOURCE_IRQ_HIGHEDGE:
49 		*triggering = ACPI_EDGE_SENSITIVE;
50 		*polarity = ACPI_ACTIVE_HIGH;
51 		break;
52 	default:
53 		dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
54 			flags);
55 		*triggering = ACPI_EDGE_SENSITIVE;
56 		*polarity = ACPI_ACTIVE_HIGH;
57 		break;
58 	}
59 
60 	if (flags & IORESOURCE_IRQ_SHAREABLE)
61 		*shareable = ACPI_SHARED;
62 	else
63 		*shareable = ACPI_EXCLUSIVE;
64 }
65 
66 static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
67 		     int transfer)
68 {
69 	int flags = 0;
70 
71 	if (bus_master)
72 		flags |= IORESOURCE_DMA_MASTER;
73 	switch (type) {
74 	case ACPI_COMPATIBILITY:
75 		flags |= IORESOURCE_DMA_COMPATIBLE;
76 		break;
77 	case ACPI_TYPE_A:
78 		flags |= IORESOURCE_DMA_TYPEA;
79 		break;
80 	case ACPI_TYPE_B:
81 		flags |= IORESOURCE_DMA_TYPEB;
82 		break;
83 	case ACPI_TYPE_F:
84 		flags |= IORESOURCE_DMA_TYPEF;
85 		break;
86 	default:
87 		/* Set a default value ? */
88 		flags |= IORESOURCE_DMA_COMPATIBLE;
89 		dev_err(&dev->dev, "invalid DMA type %d\n", type);
90 	}
91 	switch (transfer) {
92 	case ACPI_TRANSFER_8:
93 		flags |= IORESOURCE_DMA_8BIT;
94 		break;
95 	case ACPI_TRANSFER_8_16:
96 		flags |= IORESOURCE_DMA_8AND16BIT;
97 		break;
98 	case ACPI_TRANSFER_16:
99 		flags |= IORESOURCE_DMA_16BIT;
100 		break;
101 	default:
102 		/* Set a default value ? */
103 		flags |= IORESOURCE_DMA_8AND16BIT;
104 		dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
105 	}
106 
107 	return flags;
108 }
109 
110 /*
111  * Allocated Resources
112  */
113 
114 static void pnpacpi_add_irqresource(struct pnp_dev *dev, struct resource *r)
115 {
116 	if (!(r->flags & IORESOURCE_DISABLED))
117 		pcibios_penalize_isa_irq(r->start, 1);
118 
119 	pnp_add_resource(dev, r);
120 }
121 
122 /*
123  * Device CSRs that do not appear in PCI config space should be described
124  * via ACPI.  This would normally be done with Address Space Descriptors
125  * marked as "consumer-only," but old versions of Windows and Linux ignore
126  * the producer/consumer flag, so HP invented a vendor-defined resource to
127  * describe the location and size of CSR space.
128  */
129 static struct acpi_vendor_uuid hp_ccsr_uuid = {
130 	.subtype = 2,
131 	.data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
132 	    0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
133 };
134 
135 static int vendor_resource_matches(struct pnp_dev *dev,
136 				   struct acpi_resource_vendor_typed *vendor,
137 				   struct acpi_vendor_uuid *match,
138 				   int expected_len)
139 {
140 	int uuid_len = sizeof(vendor->uuid);
141 	u8 uuid_subtype = vendor->uuid_subtype;
142 	u8 *uuid = vendor->uuid;
143 	int actual_len;
144 
145 	/* byte_length includes uuid_subtype and uuid */
146 	actual_len = vendor->byte_length - uuid_len - 1;
147 
148 	if (uuid_subtype == match->subtype &&
149 	    uuid_len == sizeof(match->data) &&
150 	    memcmp(uuid, match->data, uuid_len) == 0) {
151 		if (expected_len && expected_len != actual_len) {
152 			dev_err(&dev->dev, "wrong vendor descriptor size; "
153 				"expected %d, found %d bytes\n",
154 				expected_len, actual_len);
155 			return 0;
156 		}
157 
158 		return 1;
159 	}
160 
161 	return 0;
162 }
163 
164 static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
165 				    struct acpi_resource_vendor_typed *vendor)
166 {
167 	if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
168 		u64 start, length;
169 
170 		memcpy(&start, vendor->byte_data, sizeof(start));
171 		memcpy(&length, vendor->byte_data + 8, sizeof(length));
172 
173 		pnp_add_mem_resource(dev, start, start + length - 1, 0);
174 	}
175 }
176 
177 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
178 					      void *data)
179 {
180 	struct pnp_dev *dev = data;
181 	struct acpi_resource_dma *dma;
182 	struct acpi_resource_vendor_typed *vendor_typed;
183 	struct resource r = {0};
184 	int i, flags;
185 
186 	if (acpi_dev_resource_memory(res, &r)
187 	    || acpi_dev_resource_io(res, &r)
188 	    || acpi_dev_resource_address_space(res, &r)
189 	    || acpi_dev_resource_ext_address_space(res, &r)) {
190 		pnp_add_resource(dev, &r);
191 		return AE_OK;
192 	}
193 
194 	r.flags = 0;
195 	if (acpi_dev_resource_interrupt(res, 0, &r)) {
196 		pnpacpi_add_irqresource(dev, &r);
197 		for (i = 1; acpi_dev_resource_interrupt(res, i, &r); i++)
198 			pnpacpi_add_irqresource(dev, &r);
199 
200 		if (i > 1) {
201 			/*
202 			 * The IRQ encoder puts a single interrupt in each
203 			 * descriptor, so if a _CRS descriptor has more than
204 			 * one interrupt, we won't be able to re-encode it.
205 			 */
206 			if (pnp_can_write(dev)) {
207 				dev_warn(&dev->dev, "multiple interrupts in "
208 					 "_CRS descriptor; configuration can't "
209 					 "be changed\n");
210 				dev->capabilities &= ~PNP_WRITE;
211 			}
212 		}
213 		return AE_OK;
214 	} else if (r.flags & IORESOURCE_DISABLED) {
215 		pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
216 		return AE_OK;
217 	}
218 
219 	switch (res->type) {
220 	case ACPI_RESOURCE_TYPE_DMA:
221 		dma = &res->data.dma;
222 		if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
223 			flags = dma_flags(dev, dma->type, dma->bus_master,
224 					  dma->transfer);
225 		else
226 			flags = IORESOURCE_DISABLED;
227 		pnp_add_dma_resource(dev, dma->channels[0], flags);
228 		break;
229 
230 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
231 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
232 		break;
233 
234 	case ACPI_RESOURCE_TYPE_VENDOR:
235 		vendor_typed = &res->data.vendor_typed;
236 		pnpacpi_parse_allocated_vendor(dev, vendor_typed);
237 		break;
238 
239 	case ACPI_RESOURCE_TYPE_END_TAG:
240 		break;
241 
242 	case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
243 		break;
244 
245 	default:
246 		dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
247 			 res->type);
248 		return AE_ERROR;
249 	}
250 
251 	return AE_OK;
252 }
253 
254 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
255 {
256 	struct acpi_device *acpi_dev = dev->data;
257 	acpi_handle handle = acpi_dev->handle;
258 	acpi_status status;
259 
260 	pnp_dbg(&dev->dev, "parse allocated resources\n");
261 
262 	pnp_init_resources(dev);
263 
264 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
265 				     pnpacpi_allocated_resource, dev);
266 
267 	if (ACPI_FAILURE(status)) {
268 		if (status != AE_NOT_FOUND)
269 			dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
270 		return -EPERM;
271 	}
272 	return 0;
273 }
274 
275 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
276 					    unsigned int option_flags,
277 					    struct acpi_resource_dma *p)
278 {
279 	int i;
280 	unsigned char map = 0, flags;
281 
282 	for (i = 0; i < p->channel_count; i++)
283 		map |= 1 << p->channels[i];
284 
285 	flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
286 	pnp_register_dma_resource(dev, option_flags, map, flags);
287 }
288 
289 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
290 					    unsigned int option_flags,
291 					    struct acpi_resource_irq *p)
292 {
293 	int i;
294 	pnp_irq_mask_t map;
295 	unsigned char flags;
296 
297 	bitmap_zero(map.bits, PNP_IRQ_NR);
298 	for (i = 0; i < p->interrupt_count; i++)
299 		if (p->interrupts[i])
300 			__set_bit(p->interrupts[i], map.bits);
301 
302 	flags = acpi_dev_irq_flags(p->triggering, p->polarity, p->sharable);
303 	pnp_register_irq_resource(dev, option_flags, &map, flags);
304 }
305 
306 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
307 					unsigned int option_flags,
308 					struct acpi_resource_extended_irq *p)
309 {
310 	int i;
311 	pnp_irq_mask_t map;
312 	unsigned char flags;
313 
314 	bitmap_zero(map.bits, PNP_IRQ_NR);
315 	for (i = 0; i < p->interrupt_count; i++) {
316 		if (p->interrupts[i]) {
317 			if (p->interrupts[i] < PNP_IRQ_NR)
318 				__set_bit(p->interrupts[i], map.bits);
319 			else
320 				dev_err(&dev->dev, "ignoring IRQ %d option "
321 					"(too large for %d entry bitmap)\n",
322 					p->interrupts[i], PNP_IRQ_NR);
323 		}
324 	}
325 
326 	flags = acpi_dev_irq_flags(p->triggering, p->polarity, p->sharable);
327 	pnp_register_irq_resource(dev, option_flags, &map, flags);
328 }
329 
330 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
331 					     unsigned int option_flags,
332 					     struct acpi_resource_io *io)
333 {
334 	unsigned char flags = 0;
335 
336 	if (io->io_decode == ACPI_DECODE_16)
337 		flags = IORESOURCE_IO_16BIT_ADDR;
338 	pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
339 				   io->alignment, io->address_length, flags);
340 }
341 
342 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
343 					unsigned int option_flags,
344 					struct acpi_resource_fixed_io *io)
345 {
346 	pnp_register_port_resource(dev, option_flags, io->address, io->address,
347 				   0, io->address_length, IORESOURCE_IO_FIXED);
348 }
349 
350 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
351 					      unsigned int option_flags,
352 					      struct acpi_resource_memory24 *p)
353 {
354 	unsigned char flags = 0;
355 
356 	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
357 		flags = IORESOURCE_MEM_WRITEABLE;
358 	pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
359 				  p->alignment, p->address_length, flags);
360 }
361 
362 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
363 					      unsigned int option_flags,
364 					      struct acpi_resource_memory32 *p)
365 {
366 	unsigned char flags = 0;
367 
368 	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
369 		flags = IORESOURCE_MEM_WRITEABLE;
370 	pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
371 				  p->alignment, p->address_length, flags);
372 }
373 
374 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
375 					unsigned int option_flags,
376 					struct acpi_resource_fixed_memory32 *p)
377 {
378 	unsigned char flags = 0;
379 
380 	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
381 		flags = IORESOURCE_MEM_WRITEABLE;
382 	pnp_register_mem_resource(dev, option_flags, p->address, p->address,
383 				  0, p->address_length, flags);
384 }
385 
386 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
387 						unsigned int option_flags,
388 						struct acpi_resource *r)
389 {
390 	struct acpi_resource_address64 addr, *p = &addr;
391 	acpi_status status;
392 	unsigned char flags = 0;
393 
394 	status = acpi_resource_to_address64(r, p);
395 	if (ACPI_FAILURE(status)) {
396 		dev_warn(&dev->dev, "can't convert resource type %d\n",
397 			 r->type);
398 		return;
399 	}
400 
401 	if (p->resource_type == ACPI_MEMORY_RANGE) {
402 		if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
403 			flags = IORESOURCE_MEM_WRITEABLE;
404 		pnp_register_mem_resource(dev, option_flags, p->minimum,
405 					  p->minimum, 0, p->address_length,
406 					  flags);
407 	} else if (p->resource_type == ACPI_IO_RANGE)
408 		pnp_register_port_resource(dev, option_flags, p->minimum,
409 					   p->minimum, 0, p->address_length,
410 					   IORESOURCE_IO_FIXED);
411 }
412 
413 static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
414 						    unsigned int option_flags,
415 						    struct acpi_resource *r)
416 {
417 	struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
418 	unsigned char flags = 0;
419 
420 	if (p->resource_type == ACPI_MEMORY_RANGE) {
421 		if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
422 			flags = IORESOURCE_MEM_WRITEABLE;
423 		pnp_register_mem_resource(dev, option_flags, p->minimum,
424 					  p->minimum, 0, p->address_length,
425 					  flags);
426 	} else if (p->resource_type == ACPI_IO_RANGE)
427 		pnp_register_port_resource(dev, option_flags, p->minimum,
428 					   p->minimum, 0, p->address_length,
429 					   IORESOURCE_IO_FIXED);
430 }
431 
432 struct acpipnp_parse_option_s {
433 	struct pnp_dev *dev;
434 	unsigned int option_flags;
435 };
436 
437 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
438 						  void *data)
439 {
440 	int priority;
441 	struct acpipnp_parse_option_s *parse_data = data;
442 	struct pnp_dev *dev = parse_data->dev;
443 	unsigned int option_flags = parse_data->option_flags;
444 
445 	switch (res->type) {
446 	case ACPI_RESOURCE_TYPE_IRQ:
447 		pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
448 		break;
449 
450 	case ACPI_RESOURCE_TYPE_DMA:
451 		pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
452 		break;
453 
454 	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
455 		switch (res->data.start_dpf.compatibility_priority) {
456 		case ACPI_GOOD_CONFIGURATION:
457 			priority = PNP_RES_PRIORITY_PREFERRED;
458 			break;
459 
460 		case ACPI_ACCEPTABLE_CONFIGURATION:
461 			priority = PNP_RES_PRIORITY_ACCEPTABLE;
462 			break;
463 
464 		case ACPI_SUB_OPTIMAL_CONFIGURATION:
465 			priority = PNP_RES_PRIORITY_FUNCTIONAL;
466 			break;
467 		default:
468 			priority = PNP_RES_PRIORITY_INVALID;
469 			break;
470 		}
471 		parse_data->option_flags = pnp_new_dependent_set(dev, priority);
472 		break;
473 
474 	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
475 		parse_data->option_flags = 0;
476 		break;
477 
478 	case ACPI_RESOURCE_TYPE_IO:
479 		pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
480 		break;
481 
482 	case ACPI_RESOURCE_TYPE_FIXED_IO:
483 		pnpacpi_parse_fixed_port_option(dev, option_flags,
484 					        &res->data.fixed_io);
485 		break;
486 
487 	case ACPI_RESOURCE_TYPE_VENDOR:
488 	case ACPI_RESOURCE_TYPE_END_TAG:
489 		break;
490 
491 	case ACPI_RESOURCE_TYPE_MEMORY24:
492 		pnpacpi_parse_mem24_option(dev, option_flags,
493 					   &res->data.memory24);
494 		break;
495 
496 	case ACPI_RESOURCE_TYPE_MEMORY32:
497 		pnpacpi_parse_mem32_option(dev, option_flags,
498 					   &res->data.memory32);
499 		break;
500 
501 	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
502 		pnpacpi_parse_fixed_mem32_option(dev, option_flags,
503 						 &res->data.fixed_memory32);
504 		break;
505 
506 	case ACPI_RESOURCE_TYPE_ADDRESS16:
507 	case ACPI_RESOURCE_TYPE_ADDRESS32:
508 	case ACPI_RESOURCE_TYPE_ADDRESS64:
509 		pnpacpi_parse_address_option(dev, option_flags, res);
510 		break;
511 
512 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
513 		pnpacpi_parse_ext_address_option(dev, option_flags, res);
514 		break;
515 
516 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
517 		pnpacpi_parse_ext_irq_option(dev, option_flags,
518 					     &res->data.extended_irq);
519 		break;
520 
521 	case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
522 		break;
523 
524 	default:
525 		dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
526 			 res->type);
527 		return AE_ERROR;
528 	}
529 
530 	return AE_OK;
531 }
532 
533 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
534 {
535 	struct acpi_device *acpi_dev = dev->data;
536 	acpi_handle handle = acpi_dev->handle;
537 	acpi_status status;
538 	struct acpipnp_parse_option_s parse_data;
539 
540 	pnp_dbg(&dev->dev, "parse resource options\n");
541 
542 	parse_data.dev = dev;
543 	parse_data.option_flags = 0;
544 
545 	status = acpi_walk_resources(handle, METHOD_NAME__PRS,
546 				     pnpacpi_option_resource, &parse_data);
547 
548 	if (ACPI_FAILURE(status)) {
549 		if (status != AE_NOT_FOUND)
550 			dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
551 		return -EPERM;
552 	}
553 	return 0;
554 }
555 
556 static int pnpacpi_supported_resource(struct acpi_resource *res)
557 {
558 	switch (res->type) {
559 	case ACPI_RESOURCE_TYPE_IRQ:
560 	case ACPI_RESOURCE_TYPE_DMA:
561 	case ACPI_RESOURCE_TYPE_IO:
562 	case ACPI_RESOURCE_TYPE_FIXED_IO:
563 	case ACPI_RESOURCE_TYPE_MEMORY24:
564 	case ACPI_RESOURCE_TYPE_MEMORY32:
565 	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
566 	case ACPI_RESOURCE_TYPE_ADDRESS16:
567 	case ACPI_RESOURCE_TYPE_ADDRESS32:
568 	case ACPI_RESOURCE_TYPE_ADDRESS64:
569 	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
570 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
571 		return 1;
572 	}
573 	return 0;
574 }
575 
576 /*
577  * Set resource
578  */
579 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
580 					   void *data)
581 {
582 	int *res_cnt = data;
583 
584 	if (pnpacpi_supported_resource(res))
585 		(*res_cnt)++;
586 	return AE_OK;
587 }
588 
589 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
590 {
591 	struct acpi_resource **resource = data;
592 
593 	if (pnpacpi_supported_resource(res)) {
594 		(*resource)->type = res->type;
595 		(*resource)->length = sizeof(struct acpi_resource);
596 		if (res->type == ACPI_RESOURCE_TYPE_IRQ)
597 			(*resource)->data.irq.descriptor_length =
598 					res->data.irq.descriptor_length;
599 		(*resource)++;
600 	}
601 
602 	return AE_OK;
603 }
604 
605 int pnpacpi_build_resource_template(struct pnp_dev *dev,
606 				    struct acpi_buffer *buffer)
607 {
608 	struct acpi_device *acpi_dev = dev->data;
609 	acpi_handle handle = acpi_dev->handle;
610 	struct acpi_resource *resource;
611 	int res_cnt = 0;
612 	acpi_status status;
613 
614 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
615 				     pnpacpi_count_resources, &res_cnt);
616 	if (ACPI_FAILURE(status)) {
617 		dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
618 		return -EINVAL;
619 	}
620 	if (!res_cnt)
621 		return -EINVAL;
622 	buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
623 	buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
624 	if (!buffer->pointer)
625 		return -ENOMEM;
626 
627 	resource = (struct acpi_resource *)buffer->pointer;
628 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
629 				     pnpacpi_type_resources, &resource);
630 	if (ACPI_FAILURE(status)) {
631 		kfree(buffer->pointer);
632 		dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
633 		return -EINVAL;
634 	}
635 	/* resource will pointer the end resource now */
636 	resource->type = ACPI_RESOURCE_TYPE_END_TAG;
637 	resource->length = sizeof(struct acpi_resource);
638 
639 	return 0;
640 }
641 
642 static void pnpacpi_encode_irq(struct pnp_dev *dev,
643 			       struct acpi_resource *resource,
644 			       struct resource *p)
645 {
646 	struct acpi_resource_irq *irq = &resource->data.irq;
647 	int triggering, polarity, shareable;
648 
649 	if (!pnp_resource_enabled(p)) {
650 		irq->interrupt_count = 0;
651 		pnp_dbg(&dev->dev, "  encode irq (%s)\n",
652 			p ? "disabled" : "missing");
653 		return;
654 	}
655 
656 	decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
657 	irq->triggering = triggering;
658 	irq->polarity = polarity;
659 	irq->sharable = shareable;
660 	irq->interrupt_count = 1;
661 	irq->interrupts[0] = p->start;
662 
663 	pnp_dbg(&dev->dev, "  encode irq %d %s %s %s (%d-byte descriptor)\n",
664 		(int) p->start,
665 		triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
666 		polarity == ACPI_ACTIVE_LOW ? "low" : "high",
667 		irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
668 		irq->descriptor_length);
669 }
670 
671 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
672 				   struct acpi_resource *resource,
673 				   struct resource *p)
674 {
675 	struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
676 	int triggering, polarity, shareable;
677 
678 	if (!pnp_resource_enabled(p)) {
679 		extended_irq->interrupt_count = 0;
680 		pnp_dbg(&dev->dev, "  encode extended irq (%s)\n",
681 			p ? "disabled" : "missing");
682 		return;
683 	}
684 
685 	decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
686 	extended_irq->producer_consumer = ACPI_CONSUMER;
687 	extended_irq->triggering = triggering;
688 	extended_irq->polarity = polarity;
689 	extended_irq->sharable = shareable;
690 	extended_irq->interrupt_count = 1;
691 	extended_irq->interrupts[0] = p->start;
692 
693 	pnp_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
694 		triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
695 		polarity == ACPI_ACTIVE_LOW ? "low" : "high",
696 		extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
697 }
698 
699 static void pnpacpi_encode_dma(struct pnp_dev *dev,
700 			       struct acpi_resource *resource,
701 			       struct resource *p)
702 {
703 	struct acpi_resource_dma *dma = &resource->data.dma;
704 
705 	if (!pnp_resource_enabled(p)) {
706 		dma->channel_count = 0;
707 		pnp_dbg(&dev->dev, "  encode dma (%s)\n",
708 			p ? "disabled" : "missing");
709 		return;
710 	}
711 
712 	/* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
713 	switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
714 	case IORESOURCE_DMA_TYPEA:
715 		dma->type = ACPI_TYPE_A;
716 		break;
717 	case IORESOURCE_DMA_TYPEB:
718 		dma->type = ACPI_TYPE_B;
719 		break;
720 	case IORESOURCE_DMA_TYPEF:
721 		dma->type = ACPI_TYPE_F;
722 		break;
723 	default:
724 		dma->type = ACPI_COMPATIBILITY;
725 	}
726 
727 	switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
728 	case IORESOURCE_DMA_8BIT:
729 		dma->transfer = ACPI_TRANSFER_8;
730 		break;
731 	case IORESOURCE_DMA_8AND16BIT:
732 		dma->transfer = ACPI_TRANSFER_8_16;
733 		break;
734 	default:
735 		dma->transfer = ACPI_TRANSFER_16;
736 	}
737 
738 	dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
739 	dma->channel_count = 1;
740 	dma->channels[0] = p->start;
741 
742 	pnp_dbg(&dev->dev, "  encode dma %d "
743 		"type %#x transfer %#x master %d\n",
744 		(int) p->start, dma->type, dma->transfer, dma->bus_master);
745 }
746 
747 static void pnpacpi_encode_io(struct pnp_dev *dev,
748 			      struct acpi_resource *resource,
749 			      struct resource *p)
750 {
751 	struct acpi_resource_io *io = &resource->data.io;
752 
753 	if (pnp_resource_enabled(p)) {
754 		/* Note: pnp_assign_port copies pnp_port->flags into p->flags */
755 		io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
756 		    ACPI_DECODE_16 : ACPI_DECODE_10;
757 		io->minimum = p->start;
758 		io->maximum = p->end;
759 		io->alignment = 0;	/* Correct? */
760 		io->address_length = resource_size(p);
761 	} else {
762 		io->minimum = 0;
763 		io->address_length = 0;
764 	}
765 
766 	pnp_dbg(&dev->dev, "  encode io %#x-%#x decode %#x\n", io->minimum,
767 		io->minimum + io->address_length - 1, io->io_decode);
768 }
769 
770 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
771 				    struct acpi_resource *resource,
772 				    struct resource *p)
773 {
774 	struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
775 
776 	if (pnp_resource_enabled(p)) {
777 		fixed_io->address = p->start;
778 		fixed_io->address_length = resource_size(p);
779 	} else {
780 		fixed_io->address = 0;
781 		fixed_io->address_length = 0;
782 	}
783 
784 	pnp_dbg(&dev->dev, "  encode fixed_io %#x-%#x\n", fixed_io->address,
785 		fixed_io->address + fixed_io->address_length - 1);
786 }
787 
788 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
789 				 struct acpi_resource *resource,
790 				 struct resource *p)
791 {
792 	struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
793 
794 	if (pnp_resource_enabled(p)) {
795 		/* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
796 		memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
797 		    ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
798 		memory24->minimum = p->start;
799 		memory24->maximum = p->end;
800 		memory24->alignment = 0;
801 		memory24->address_length = resource_size(p);
802 	} else {
803 		memory24->minimum = 0;
804 		memory24->address_length = 0;
805 	}
806 
807 	pnp_dbg(&dev->dev, "  encode mem24 %#x-%#x write_protect %#x\n",
808 		memory24->minimum,
809 		memory24->minimum + memory24->address_length - 1,
810 		memory24->write_protect);
811 }
812 
813 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
814 				 struct acpi_resource *resource,
815 				 struct resource *p)
816 {
817 	struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
818 
819 	if (pnp_resource_enabled(p)) {
820 		memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
821 		    ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
822 		memory32->minimum = p->start;
823 		memory32->maximum = p->end;
824 		memory32->alignment = 0;
825 		memory32->address_length = resource_size(p);
826 	} else {
827 		memory32->minimum = 0;
828 		memory32->alignment = 0;
829 	}
830 
831 	pnp_dbg(&dev->dev, "  encode mem32 %#x-%#x write_protect %#x\n",
832 		memory32->minimum,
833 		memory32->minimum + memory32->address_length - 1,
834 		memory32->write_protect);
835 }
836 
837 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
838 				       struct acpi_resource *resource,
839 				       struct resource *p)
840 {
841 	struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
842 
843 	if (pnp_resource_enabled(p)) {
844 		fixed_memory32->write_protect =
845 		    p->flags & IORESOURCE_MEM_WRITEABLE ?
846 		    ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
847 		fixed_memory32->address = p->start;
848 		fixed_memory32->address_length = resource_size(p);
849 	} else {
850 		fixed_memory32->address = 0;
851 		fixed_memory32->address_length = 0;
852 	}
853 
854 	pnp_dbg(&dev->dev, "  encode fixed_mem32 %#x-%#x write_protect %#x\n",
855 		fixed_memory32->address,
856 		fixed_memory32->address + fixed_memory32->address_length - 1,
857 		fixed_memory32->write_protect);
858 }
859 
860 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
861 {
862 	int i = 0;
863 	/* pnpacpi_build_resource_template allocates extra mem */
864 	int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
865 	struct acpi_resource *resource = buffer->pointer;
866 	int port = 0, irq = 0, dma = 0, mem = 0;
867 
868 	pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
869 	while (i < res_cnt) {
870 		switch (resource->type) {
871 		case ACPI_RESOURCE_TYPE_IRQ:
872 			pnpacpi_encode_irq(dev, resource,
873 			       pnp_get_resource(dev, IORESOURCE_IRQ, irq));
874 			irq++;
875 			break;
876 
877 		case ACPI_RESOURCE_TYPE_DMA:
878 			pnpacpi_encode_dma(dev, resource,
879 				pnp_get_resource(dev, IORESOURCE_DMA, dma));
880 			dma++;
881 			break;
882 		case ACPI_RESOURCE_TYPE_IO:
883 			pnpacpi_encode_io(dev, resource,
884 				pnp_get_resource(dev, IORESOURCE_IO, port));
885 			port++;
886 			break;
887 		case ACPI_RESOURCE_TYPE_FIXED_IO:
888 			pnpacpi_encode_fixed_io(dev, resource,
889 				pnp_get_resource(dev, IORESOURCE_IO, port));
890 			port++;
891 			break;
892 		case ACPI_RESOURCE_TYPE_MEMORY24:
893 			pnpacpi_encode_mem24(dev, resource,
894 				pnp_get_resource(dev, IORESOURCE_MEM, mem));
895 			mem++;
896 			break;
897 		case ACPI_RESOURCE_TYPE_MEMORY32:
898 			pnpacpi_encode_mem32(dev, resource,
899 				pnp_get_resource(dev, IORESOURCE_MEM, mem));
900 			mem++;
901 			break;
902 		case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
903 			pnpacpi_encode_fixed_mem32(dev, resource,
904 				pnp_get_resource(dev, IORESOURCE_MEM, mem));
905 			mem++;
906 			break;
907 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
908 			pnpacpi_encode_ext_irq(dev, resource,
909 				pnp_get_resource(dev, IORESOURCE_IRQ, irq));
910 			irq++;
911 			break;
912 		case ACPI_RESOURCE_TYPE_START_DEPENDENT:
913 		case ACPI_RESOURCE_TYPE_END_DEPENDENT:
914 		case ACPI_RESOURCE_TYPE_VENDOR:
915 		case ACPI_RESOURCE_TYPE_END_TAG:
916 		case ACPI_RESOURCE_TYPE_ADDRESS16:
917 		case ACPI_RESOURCE_TYPE_ADDRESS32:
918 		case ACPI_RESOURCE_TYPE_ADDRESS64:
919 		case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
920 		case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
921 		default:	/* other type */
922 			dev_warn(&dev->dev, "can't encode unknown resource "
923 				 "type %d\n", resource->type);
924 			return -EINVAL;
925 		}
926 		resource++;
927 		i++;
928 	}
929 	return 0;
930 }
931