Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[linux-btrfs-devel.git] / drivers / pnp / pnpacpi / rsparser.c
blobbbf3edd85beb0c404b4e5b7791cfd1b79908dc3a
1 /*
2 * pnpacpi -- PnP ACPI driver
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>
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.
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.
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
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"
31 #ifdef CONFIG_IA64
32 #define valid_IRQ(i) (1)
33 #else
34 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
35 #endif
38 * Allocated Resources
40 static int irq_flags(int triggering, int polarity, int shareable)
42 int flags;
44 if (triggering == ACPI_LEVEL_SENSITIVE) {
45 if (polarity == ACPI_ACTIVE_LOW)
46 flags = IORESOURCE_IRQ_LOWLEVEL;
47 else
48 flags = IORESOURCE_IRQ_HIGHLEVEL;
49 } else {
50 if (polarity == ACPI_ACTIVE_LOW)
51 flags = IORESOURCE_IRQ_LOWEDGE;
52 else
53 flags = IORESOURCE_IRQ_HIGHEDGE;
56 if (shareable == ACPI_SHARED)
57 flags |= IORESOURCE_IRQ_SHAREABLE;
59 return flags;
62 static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
63 int *polarity, int *shareable)
65 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
66 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
67 case IORESOURCE_IRQ_LOWLEVEL:
68 *triggering = ACPI_LEVEL_SENSITIVE;
69 *polarity = ACPI_ACTIVE_LOW;
70 break;
71 case IORESOURCE_IRQ_HIGHLEVEL:
72 *triggering = ACPI_LEVEL_SENSITIVE;
73 *polarity = ACPI_ACTIVE_HIGH;
74 break;
75 case IORESOURCE_IRQ_LOWEDGE:
76 *triggering = ACPI_EDGE_SENSITIVE;
77 *polarity = ACPI_ACTIVE_LOW;
78 break;
79 case IORESOURCE_IRQ_HIGHEDGE:
80 *triggering = ACPI_EDGE_SENSITIVE;
81 *polarity = ACPI_ACTIVE_HIGH;
82 break;
83 default:
84 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
85 flags);
86 *triggering = ACPI_EDGE_SENSITIVE;
87 *polarity = ACPI_ACTIVE_HIGH;
88 break;
91 if (flags & IORESOURCE_IRQ_SHAREABLE)
92 *shareable = ACPI_SHARED;
93 else
94 *shareable = ACPI_EXCLUSIVE;
97 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
98 u32 gsi, int triggering,
99 int polarity, int shareable)
101 int irq, flags;
102 int p, t;
104 if (!valid_IRQ(gsi)) {
105 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
106 return;
110 * in IO-APIC mode, use overrided attribute. Two reasons:
111 * 1. BIOS bug in DSDT
112 * 2. BIOS uses IO-APIC mode Interrupt Source Override
114 if (!acpi_get_override_irq(gsi, &t, &p)) {
115 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
116 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
118 if (triggering != t || polarity != p) {
119 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
120 gsi, t ? "edge":"level", p ? "low":"high");
121 triggering = t;
122 polarity = p;
126 flags = irq_flags(triggering, polarity, shareable);
127 irq = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
128 if (irq >= 0)
129 pcibios_penalize_isa_irq(irq, 1);
130 else
131 flags |= IORESOURCE_DISABLED;
133 pnp_add_irq_resource(dev, irq, flags);
136 static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
137 int transfer)
139 int flags = 0;
141 if (bus_master)
142 flags |= IORESOURCE_DMA_MASTER;
143 switch (type) {
144 case ACPI_COMPATIBILITY:
145 flags |= IORESOURCE_DMA_COMPATIBLE;
146 break;
147 case ACPI_TYPE_A:
148 flags |= IORESOURCE_DMA_TYPEA;
149 break;
150 case ACPI_TYPE_B:
151 flags |= IORESOURCE_DMA_TYPEB;
152 break;
153 case ACPI_TYPE_F:
154 flags |= IORESOURCE_DMA_TYPEF;
155 break;
156 default:
157 /* Set a default value ? */
158 flags |= IORESOURCE_DMA_COMPATIBLE;
159 dev_err(&dev->dev, "invalid DMA type %d\n", type);
161 switch (transfer) {
162 case ACPI_TRANSFER_8:
163 flags |= IORESOURCE_DMA_8BIT;
164 break;
165 case ACPI_TRANSFER_8_16:
166 flags |= IORESOURCE_DMA_8AND16BIT;
167 break;
168 case ACPI_TRANSFER_16:
169 flags |= IORESOURCE_DMA_16BIT;
170 break;
171 default:
172 /* Set a default value ? */
173 flags |= IORESOURCE_DMA_8AND16BIT;
174 dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
177 return flags;
180 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
181 u64 len, int io_decode,
182 int window)
184 int flags = 0;
185 u64 end = start + len - 1;
187 if (io_decode == ACPI_DECODE_16)
188 flags |= IORESOURCE_IO_16BIT_ADDR;
189 if (len == 0 || end >= 0x10003)
190 flags |= IORESOURCE_DISABLED;
191 if (window)
192 flags |= IORESOURCE_WINDOW;
194 pnp_add_io_resource(dev, start, end, flags);
198 * Device CSRs that do not appear in PCI config space should be described
199 * via ACPI. This would normally be done with Address Space Descriptors
200 * marked as "consumer-only," but old versions of Windows and Linux ignore
201 * the producer/consumer flag, so HP invented a vendor-defined resource to
202 * describe the location and size of CSR space.
204 static struct acpi_vendor_uuid hp_ccsr_uuid = {
205 .subtype = 2,
206 .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
207 0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
210 static int vendor_resource_matches(struct pnp_dev *dev,
211 struct acpi_resource_vendor_typed *vendor,
212 struct acpi_vendor_uuid *match,
213 int expected_len)
215 int uuid_len = sizeof(vendor->uuid);
216 u8 uuid_subtype = vendor->uuid_subtype;
217 u8 *uuid = vendor->uuid;
218 int actual_len;
220 /* byte_length includes uuid_subtype and uuid */
221 actual_len = vendor->byte_length - uuid_len - 1;
223 if (uuid_subtype == match->subtype &&
224 uuid_len == sizeof(match->data) &&
225 memcmp(uuid, match->data, uuid_len) == 0) {
226 if (expected_len && expected_len != actual_len) {
227 dev_err(&dev->dev, "wrong vendor descriptor size; "
228 "expected %d, found %d bytes\n",
229 expected_len, actual_len);
230 return 0;
233 return 1;
236 return 0;
239 static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
240 struct acpi_resource_vendor_typed *vendor)
242 if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
243 u64 start, length;
245 memcpy(&start, vendor->byte_data, sizeof(start));
246 memcpy(&length, vendor->byte_data + 8, sizeof(length));
248 pnp_add_mem_resource(dev, start, start + length - 1, 0);
252 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
253 u64 start, u64 len,
254 int write_protect, int window)
256 int flags = 0;
257 u64 end = start + len - 1;
259 if (len == 0)
260 flags |= IORESOURCE_DISABLED;
261 if (write_protect == ACPI_READ_WRITE_MEMORY)
262 flags |= IORESOURCE_MEM_WRITEABLE;
263 if (window)
264 flags |= IORESOURCE_WINDOW;
266 pnp_add_mem_resource(dev, start, end, flags);
269 static void pnpacpi_parse_allocated_busresource(struct pnp_dev *dev,
270 u64 start, u64 len)
272 u64 end = start + len - 1;
274 pnp_add_bus_resource(dev, start, end);
277 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
278 struct acpi_resource *res)
280 struct acpi_resource_address64 addr, *p = &addr;
281 acpi_status status;
282 int window;
283 u64 len;
285 status = acpi_resource_to_address64(res, p);
286 if (!ACPI_SUCCESS(status)) {
287 dev_warn(&dev->dev, "failed to convert resource type %d\n",
288 res->type);
289 return;
292 /* Windows apparently computes length rather than using _LEN */
293 len = p->maximum - p->minimum + 1;
294 window = (p->producer_consumer == ACPI_PRODUCER) ? 1 : 0;
296 if (p->resource_type == ACPI_MEMORY_RANGE)
297 pnpacpi_parse_allocated_memresource(dev, p->minimum, len,
298 p->info.mem.write_protect, window);
299 else if (p->resource_type == ACPI_IO_RANGE)
300 pnpacpi_parse_allocated_ioresource(dev, p->minimum, len,
301 p->granularity == 0xfff ? ACPI_DECODE_10 :
302 ACPI_DECODE_16, window);
303 else if (p->resource_type == ACPI_BUS_NUMBER_RANGE)
304 pnpacpi_parse_allocated_busresource(dev, p->minimum, len);
307 static void pnpacpi_parse_allocated_ext_address_space(struct pnp_dev *dev,
308 struct acpi_resource *res)
310 struct acpi_resource_extended_address64 *p = &res->data.ext_address64;
311 int window;
312 u64 len;
314 /* Windows apparently computes length rather than using _LEN */
315 len = p->maximum - p->minimum + 1;
316 window = (p->producer_consumer == ACPI_PRODUCER) ? 1 : 0;
318 if (p->resource_type == ACPI_MEMORY_RANGE)
319 pnpacpi_parse_allocated_memresource(dev, p->minimum, len,
320 p->info.mem.write_protect, window);
321 else if (p->resource_type == ACPI_IO_RANGE)
322 pnpacpi_parse_allocated_ioresource(dev, p->minimum, len,
323 p->granularity == 0xfff ? ACPI_DECODE_10 :
324 ACPI_DECODE_16, window);
325 else if (p->resource_type == ACPI_BUS_NUMBER_RANGE)
326 pnpacpi_parse_allocated_busresource(dev, p->minimum, len);
329 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
330 void *data)
332 struct pnp_dev *dev = data;
333 struct acpi_resource_irq *irq;
334 struct acpi_resource_dma *dma;
335 struct acpi_resource_io *io;
336 struct acpi_resource_fixed_io *fixed_io;
337 struct acpi_resource_vendor_typed *vendor_typed;
338 struct acpi_resource_memory24 *memory24;
339 struct acpi_resource_memory32 *memory32;
340 struct acpi_resource_fixed_memory32 *fixed_memory32;
341 struct acpi_resource_extended_irq *extended_irq;
342 int i, flags;
344 switch (res->type) {
345 case ACPI_RESOURCE_TYPE_IRQ:
347 * Per spec, only one interrupt per descriptor is allowed in
348 * _CRS, but some firmware violates this, so parse them all.
350 irq = &res->data.irq;
351 if (irq->interrupt_count == 0)
352 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
353 else {
354 for (i = 0; i < irq->interrupt_count; i++) {
355 pnpacpi_parse_allocated_irqresource(dev,
356 irq->interrupts[i],
357 irq->triggering,
358 irq->polarity,
359 irq->sharable);
363 * The IRQ encoder puts a single interrupt in each
364 * descriptor, so if a _CRS descriptor has more than
365 * one interrupt, we won't be able to re-encode it.
367 if (pnp_can_write(dev) && irq->interrupt_count > 1) {
368 dev_warn(&dev->dev, "multiple interrupts in "
369 "_CRS descriptor; configuration can't "
370 "be changed\n");
371 dev->capabilities &= ~PNP_WRITE;
374 break;
376 case ACPI_RESOURCE_TYPE_DMA:
377 dma = &res->data.dma;
378 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
379 flags = dma_flags(dev, dma->type, dma->bus_master,
380 dma->transfer);
381 else
382 flags = IORESOURCE_DISABLED;
383 pnp_add_dma_resource(dev, dma->channels[0], flags);
384 break;
386 case ACPI_RESOURCE_TYPE_IO:
387 io = &res->data.io;
388 pnpacpi_parse_allocated_ioresource(dev,
389 io->minimum,
390 io->address_length,
391 io->io_decode, 0);
392 break;
394 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
395 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
396 break;
398 case ACPI_RESOURCE_TYPE_FIXED_IO:
399 fixed_io = &res->data.fixed_io;
400 pnpacpi_parse_allocated_ioresource(dev,
401 fixed_io->address,
402 fixed_io->address_length,
403 ACPI_DECODE_10, 0);
404 break;
406 case ACPI_RESOURCE_TYPE_VENDOR:
407 vendor_typed = &res->data.vendor_typed;
408 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
409 break;
411 case ACPI_RESOURCE_TYPE_END_TAG:
412 break;
414 case ACPI_RESOURCE_TYPE_MEMORY24:
415 memory24 = &res->data.memory24;
416 pnpacpi_parse_allocated_memresource(dev,
417 memory24->minimum,
418 memory24->address_length,
419 memory24->write_protect, 0);
420 break;
421 case ACPI_RESOURCE_TYPE_MEMORY32:
422 memory32 = &res->data.memory32;
423 pnpacpi_parse_allocated_memresource(dev,
424 memory32->minimum,
425 memory32->address_length,
426 memory32->write_protect, 0);
427 break;
428 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
429 fixed_memory32 = &res->data.fixed_memory32;
430 pnpacpi_parse_allocated_memresource(dev,
431 fixed_memory32->address,
432 fixed_memory32->address_length,
433 fixed_memory32->write_protect, 0);
434 break;
435 case ACPI_RESOURCE_TYPE_ADDRESS16:
436 case ACPI_RESOURCE_TYPE_ADDRESS32:
437 case ACPI_RESOURCE_TYPE_ADDRESS64:
438 pnpacpi_parse_allocated_address_space(dev, res);
439 break;
441 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
442 pnpacpi_parse_allocated_ext_address_space(dev, res);
443 break;
445 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
446 extended_irq = &res->data.extended_irq;
448 if (extended_irq->interrupt_count == 0)
449 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
450 else {
451 for (i = 0; i < extended_irq->interrupt_count; i++) {
452 pnpacpi_parse_allocated_irqresource(dev,
453 extended_irq->interrupts[i],
454 extended_irq->triggering,
455 extended_irq->polarity,
456 extended_irq->sharable);
460 * The IRQ encoder puts a single interrupt in each
461 * descriptor, so if a _CRS descriptor has more than
462 * one interrupt, we won't be able to re-encode it.
464 if (pnp_can_write(dev) &&
465 extended_irq->interrupt_count > 1) {
466 dev_warn(&dev->dev, "multiple interrupts in "
467 "_CRS descriptor; configuration can't "
468 "be changed\n");
469 dev->capabilities &= ~PNP_WRITE;
472 break;
474 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
475 break;
477 default:
478 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
479 res->type);
480 return AE_ERROR;
483 return AE_OK;
486 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
488 struct acpi_device *acpi_dev = dev->data;
489 acpi_handle handle = acpi_dev->handle;
490 acpi_status status;
492 pnp_dbg(&dev->dev, "parse allocated resources\n");
494 pnp_init_resources(dev);
496 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
497 pnpacpi_allocated_resource, dev);
499 if (ACPI_FAILURE(status)) {
500 if (status != AE_NOT_FOUND)
501 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
502 return -EPERM;
504 return 0;
507 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
508 unsigned int option_flags,
509 struct acpi_resource_dma *p)
511 int i;
512 unsigned char map = 0, flags = 0;
514 if (p->channel_count == 0)
515 flags |= IORESOURCE_DISABLED;
517 for (i = 0; i < p->channel_count; i++)
518 map |= 1 << p->channels[i];
520 flags |= dma_flags(dev, p->type, p->bus_master, p->transfer);
521 pnp_register_dma_resource(dev, option_flags, map, flags);
524 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
525 unsigned int option_flags,
526 struct acpi_resource_irq *p)
528 int i;
529 pnp_irq_mask_t map;
530 unsigned char flags = 0;
532 if (p->interrupt_count == 0)
533 flags |= IORESOURCE_DISABLED;
535 bitmap_zero(map.bits, PNP_IRQ_NR);
536 for (i = 0; i < p->interrupt_count; i++)
537 if (p->interrupts[i])
538 __set_bit(p->interrupts[i], map.bits);
540 flags |= irq_flags(p->triggering, p->polarity, p->sharable);
541 pnp_register_irq_resource(dev, option_flags, &map, flags);
544 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
545 unsigned int option_flags,
546 struct acpi_resource_extended_irq *p)
548 int i;
549 pnp_irq_mask_t map;
550 unsigned char flags = 0;
552 if (p->interrupt_count == 0)
553 flags |= IORESOURCE_DISABLED;
555 bitmap_zero(map.bits, PNP_IRQ_NR);
556 for (i = 0; i < p->interrupt_count; i++) {
557 if (p->interrupts[i]) {
558 if (p->interrupts[i] < PNP_IRQ_NR)
559 __set_bit(p->interrupts[i], map.bits);
560 else
561 dev_err(&dev->dev, "ignoring IRQ %d option "
562 "(too large for %d entry bitmap)\n",
563 p->interrupts[i], PNP_IRQ_NR);
567 flags |= irq_flags(p->triggering, p->polarity, p->sharable);
568 pnp_register_irq_resource(dev, option_flags, &map, flags);
571 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
572 unsigned int option_flags,
573 struct acpi_resource_io *io)
575 unsigned char flags = 0;
577 if (io->address_length == 0)
578 flags |= IORESOURCE_DISABLED;
580 if (io->io_decode == ACPI_DECODE_16)
581 flags |= IORESOURCE_IO_16BIT_ADDR;
582 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
583 io->alignment, io->address_length, flags);
586 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
587 unsigned int option_flags,
588 struct acpi_resource_fixed_io *io)
590 unsigned char flags = 0;
592 if (io->address_length == 0)
593 flags |= IORESOURCE_DISABLED;
595 pnp_register_port_resource(dev, option_flags, io->address, io->address,
596 0, io->address_length, flags | IORESOURCE_IO_FIXED);
599 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
600 unsigned int option_flags,
601 struct acpi_resource_memory24 *p)
603 unsigned char flags = 0;
605 if (p->address_length == 0)
606 flags |= IORESOURCE_DISABLED;
608 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
609 flags |= IORESOURCE_MEM_WRITEABLE;
610 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
611 p->alignment, p->address_length, flags);
614 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
615 unsigned int option_flags,
616 struct acpi_resource_memory32 *p)
618 unsigned char flags = 0;
620 if (p->address_length == 0)
621 flags |= IORESOURCE_DISABLED;
623 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
624 flags |= IORESOURCE_MEM_WRITEABLE;
625 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
626 p->alignment, p->address_length, flags);
629 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
630 unsigned int option_flags,
631 struct acpi_resource_fixed_memory32 *p)
633 unsigned char flags = 0;
635 if (p->address_length == 0)
636 flags |= IORESOURCE_DISABLED;
638 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
639 flags |= IORESOURCE_MEM_WRITEABLE;
640 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
641 0, p->address_length, flags);
644 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
645 unsigned int option_flags,
646 struct acpi_resource *r)
648 struct acpi_resource_address64 addr, *p = &addr;
649 acpi_status status;
650 unsigned char flags = 0;
652 status = acpi_resource_to_address64(r, p);
653 if (ACPI_FAILURE(status)) {
654 dev_warn(&dev->dev, "can't convert resource type %d\n",
655 r->type);
656 return;
659 if (p->address_length == 0)
660 flags |= IORESOURCE_DISABLED;
662 if (p->resource_type == ACPI_MEMORY_RANGE) {
663 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
664 flags |= IORESOURCE_MEM_WRITEABLE;
665 pnp_register_mem_resource(dev, option_flags, p->minimum,
666 p->minimum, 0, p->address_length,
667 flags);
668 } else if (p->resource_type == ACPI_IO_RANGE)
669 pnp_register_port_resource(dev, option_flags, p->minimum,
670 p->minimum, 0, p->address_length,
671 flags | IORESOURCE_IO_FIXED);
674 static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
675 unsigned int option_flags,
676 struct acpi_resource *r)
678 struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
679 unsigned char flags = 0;
681 if (p->address_length == 0)
682 flags |= IORESOURCE_DISABLED;
684 if (p->resource_type == ACPI_MEMORY_RANGE) {
685 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
686 flags |= IORESOURCE_MEM_WRITEABLE;
687 pnp_register_mem_resource(dev, option_flags, p->minimum,
688 p->minimum, 0, p->address_length,
689 flags);
690 } else if (p->resource_type == ACPI_IO_RANGE)
691 pnp_register_port_resource(dev, option_flags, p->minimum,
692 p->minimum, 0, p->address_length,
693 flags | IORESOURCE_IO_FIXED);
696 struct acpipnp_parse_option_s {
697 struct pnp_dev *dev;
698 unsigned int option_flags;
701 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
702 void *data)
704 int priority;
705 struct acpipnp_parse_option_s *parse_data = data;
706 struct pnp_dev *dev = parse_data->dev;
707 unsigned int option_flags = parse_data->option_flags;
709 switch (res->type) {
710 case ACPI_RESOURCE_TYPE_IRQ:
711 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
712 break;
714 case ACPI_RESOURCE_TYPE_DMA:
715 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
716 break;
718 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
719 switch (res->data.start_dpf.compatibility_priority) {
720 case ACPI_GOOD_CONFIGURATION:
721 priority = PNP_RES_PRIORITY_PREFERRED;
722 break;
724 case ACPI_ACCEPTABLE_CONFIGURATION:
725 priority = PNP_RES_PRIORITY_ACCEPTABLE;
726 break;
728 case ACPI_SUB_OPTIMAL_CONFIGURATION:
729 priority = PNP_RES_PRIORITY_FUNCTIONAL;
730 break;
731 default:
732 priority = PNP_RES_PRIORITY_INVALID;
733 break;
735 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
736 break;
738 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
739 parse_data->option_flags = 0;
740 break;
742 case ACPI_RESOURCE_TYPE_IO:
743 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
744 break;
746 case ACPI_RESOURCE_TYPE_FIXED_IO:
747 pnpacpi_parse_fixed_port_option(dev, option_flags,
748 &res->data.fixed_io);
749 break;
751 case ACPI_RESOURCE_TYPE_VENDOR:
752 case ACPI_RESOURCE_TYPE_END_TAG:
753 break;
755 case ACPI_RESOURCE_TYPE_MEMORY24:
756 pnpacpi_parse_mem24_option(dev, option_flags,
757 &res->data.memory24);
758 break;
760 case ACPI_RESOURCE_TYPE_MEMORY32:
761 pnpacpi_parse_mem32_option(dev, option_flags,
762 &res->data.memory32);
763 break;
765 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
766 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
767 &res->data.fixed_memory32);
768 break;
770 case ACPI_RESOURCE_TYPE_ADDRESS16:
771 case ACPI_RESOURCE_TYPE_ADDRESS32:
772 case ACPI_RESOURCE_TYPE_ADDRESS64:
773 pnpacpi_parse_address_option(dev, option_flags, res);
774 break;
776 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
777 pnpacpi_parse_ext_address_option(dev, option_flags, res);
778 break;
780 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
781 pnpacpi_parse_ext_irq_option(dev, option_flags,
782 &res->data.extended_irq);
783 break;
785 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
786 break;
788 default:
789 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
790 res->type);
791 return AE_ERROR;
794 return AE_OK;
797 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
799 struct acpi_device *acpi_dev = dev->data;
800 acpi_handle handle = acpi_dev->handle;
801 acpi_status status;
802 struct acpipnp_parse_option_s parse_data;
804 pnp_dbg(&dev->dev, "parse resource options\n");
806 parse_data.dev = dev;
807 parse_data.option_flags = 0;
809 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
810 pnpacpi_option_resource, &parse_data);
812 if (ACPI_FAILURE(status)) {
813 if (status != AE_NOT_FOUND)
814 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
815 return -EPERM;
817 return 0;
820 static int pnpacpi_supported_resource(struct acpi_resource *res)
822 switch (res->type) {
823 case ACPI_RESOURCE_TYPE_IRQ:
824 case ACPI_RESOURCE_TYPE_DMA:
825 case ACPI_RESOURCE_TYPE_IO:
826 case ACPI_RESOURCE_TYPE_FIXED_IO:
827 case ACPI_RESOURCE_TYPE_MEMORY24:
828 case ACPI_RESOURCE_TYPE_MEMORY32:
829 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
830 case ACPI_RESOURCE_TYPE_ADDRESS16:
831 case ACPI_RESOURCE_TYPE_ADDRESS32:
832 case ACPI_RESOURCE_TYPE_ADDRESS64:
833 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
834 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
835 return 1;
837 return 0;
841 * Set resource
843 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
844 void *data)
846 int *res_cnt = data;
848 if (pnpacpi_supported_resource(res))
849 (*res_cnt)++;
850 return AE_OK;
853 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
855 struct acpi_resource **resource = data;
857 if (pnpacpi_supported_resource(res)) {
858 (*resource)->type = res->type;
859 (*resource)->length = sizeof(struct acpi_resource);
860 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
861 (*resource)->data.irq.descriptor_length =
862 res->data.irq.descriptor_length;
863 (*resource)++;
866 return AE_OK;
869 int pnpacpi_build_resource_template(struct pnp_dev *dev,
870 struct acpi_buffer *buffer)
872 struct acpi_device *acpi_dev = dev->data;
873 acpi_handle handle = acpi_dev->handle;
874 struct acpi_resource *resource;
875 int res_cnt = 0;
876 acpi_status status;
878 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
879 pnpacpi_count_resources, &res_cnt);
880 if (ACPI_FAILURE(status)) {
881 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
882 return -EINVAL;
884 if (!res_cnt)
885 return -EINVAL;
886 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
887 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
888 if (!buffer->pointer)
889 return -ENOMEM;
891 resource = (struct acpi_resource *)buffer->pointer;
892 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
893 pnpacpi_type_resources, &resource);
894 if (ACPI_FAILURE(status)) {
895 kfree(buffer->pointer);
896 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
897 return -EINVAL;
899 /* resource will pointer the end resource now */
900 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
902 return 0;
905 static void pnpacpi_encode_irq(struct pnp_dev *dev,
906 struct acpi_resource *resource,
907 struct resource *p)
909 struct acpi_resource_irq *irq = &resource->data.irq;
910 int triggering, polarity, shareable;
912 if (!pnp_resource_enabled(p)) {
913 irq->interrupt_count = 0;
914 pnp_dbg(&dev->dev, " encode irq (%s)\n",
915 p ? "disabled" : "missing");
916 return;
919 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
920 irq->triggering = triggering;
921 irq->polarity = polarity;
922 irq->sharable = shareable;
923 irq->interrupt_count = 1;
924 irq->interrupts[0] = p->start;
926 pnp_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
927 (int) p->start,
928 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
929 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
930 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
931 irq->descriptor_length);
934 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
935 struct acpi_resource *resource,
936 struct resource *p)
938 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
939 int triggering, polarity, shareable;
941 if (!pnp_resource_enabled(p)) {
942 extended_irq->interrupt_count = 0;
943 pnp_dbg(&dev->dev, " encode extended irq (%s)\n",
944 p ? "disabled" : "missing");
945 return;
948 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
949 extended_irq->producer_consumer = ACPI_CONSUMER;
950 extended_irq->triggering = triggering;
951 extended_irq->polarity = polarity;
952 extended_irq->sharable = shareable;
953 extended_irq->interrupt_count = 1;
954 extended_irq->interrupts[0] = p->start;
956 pnp_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
957 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
958 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
959 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
962 static void pnpacpi_encode_dma(struct pnp_dev *dev,
963 struct acpi_resource *resource,
964 struct resource *p)
966 struct acpi_resource_dma *dma = &resource->data.dma;
968 if (!pnp_resource_enabled(p)) {
969 dma->channel_count = 0;
970 pnp_dbg(&dev->dev, " encode dma (%s)\n",
971 p ? "disabled" : "missing");
972 return;
975 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
976 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
977 case IORESOURCE_DMA_TYPEA:
978 dma->type = ACPI_TYPE_A;
979 break;
980 case IORESOURCE_DMA_TYPEB:
981 dma->type = ACPI_TYPE_B;
982 break;
983 case IORESOURCE_DMA_TYPEF:
984 dma->type = ACPI_TYPE_F;
985 break;
986 default:
987 dma->type = ACPI_COMPATIBILITY;
990 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
991 case IORESOURCE_DMA_8BIT:
992 dma->transfer = ACPI_TRANSFER_8;
993 break;
994 case IORESOURCE_DMA_8AND16BIT:
995 dma->transfer = ACPI_TRANSFER_8_16;
996 break;
997 default:
998 dma->transfer = ACPI_TRANSFER_16;
1001 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
1002 dma->channel_count = 1;
1003 dma->channels[0] = p->start;
1005 pnp_dbg(&dev->dev, " encode dma %d "
1006 "type %#x transfer %#x master %d\n",
1007 (int) p->start, dma->type, dma->transfer, dma->bus_master);
1010 static void pnpacpi_encode_io(struct pnp_dev *dev,
1011 struct acpi_resource *resource,
1012 struct resource *p)
1014 struct acpi_resource_io *io = &resource->data.io;
1016 if (pnp_resource_enabled(p)) {
1017 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
1018 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
1019 ACPI_DECODE_16 : ACPI_DECODE_10;
1020 io->minimum = p->start;
1021 io->maximum = p->end;
1022 io->alignment = 0; /* Correct? */
1023 io->address_length = resource_size(p);
1024 } else {
1025 io->minimum = 0;
1026 io->address_length = 0;
1029 pnp_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
1030 io->minimum + io->address_length - 1, io->io_decode);
1033 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
1034 struct acpi_resource *resource,
1035 struct resource *p)
1037 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
1039 if (pnp_resource_enabled(p)) {
1040 fixed_io->address = p->start;
1041 fixed_io->address_length = resource_size(p);
1042 } else {
1043 fixed_io->address = 0;
1044 fixed_io->address_length = 0;
1047 pnp_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
1048 fixed_io->address + fixed_io->address_length - 1);
1051 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
1052 struct acpi_resource *resource,
1053 struct resource *p)
1055 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
1057 if (pnp_resource_enabled(p)) {
1058 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
1059 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1060 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1061 memory24->minimum = p->start;
1062 memory24->maximum = p->end;
1063 memory24->alignment = 0;
1064 memory24->address_length = resource_size(p);
1065 } else {
1066 memory24->minimum = 0;
1067 memory24->address_length = 0;
1070 pnp_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
1071 memory24->minimum,
1072 memory24->minimum + memory24->address_length - 1,
1073 memory24->write_protect);
1076 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1077 struct acpi_resource *resource,
1078 struct resource *p)
1080 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1082 if (pnp_resource_enabled(p)) {
1083 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1084 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1085 memory32->minimum = p->start;
1086 memory32->maximum = p->end;
1087 memory32->alignment = 0;
1088 memory32->address_length = resource_size(p);
1089 } else {
1090 memory32->minimum = 0;
1091 memory32->alignment = 0;
1094 pnp_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
1095 memory32->minimum,
1096 memory32->minimum + memory32->address_length - 1,
1097 memory32->write_protect);
1100 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1101 struct acpi_resource *resource,
1102 struct resource *p)
1104 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1106 if (pnp_resource_enabled(p)) {
1107 fixed_memory32->write_protect =
1108 p->flags & IORESOURCE_MEM_WRITEABLE ?
1109 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1110 fixed_memory32->address = p->start;
1111 fixed_memory32->address_length = resource_size(p);
1112 } else {
1113 fixed_memory32->address = 0;
1114 fixed_memory32->address_length = 0;
1117 pnp_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
1118 fixed_memory32->address,
1119 fixed_memory32->address + fixed_memory32->address_length - 1,
1120 fixed_memory32->write_protect);
1123 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1125 int i = 0;
1126 /* pnpacpi_build_resource_template allocates extra mem */
1127 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1128 struct acpi_resource *resource = buffer->pointer;
1129 int port = 0, irq = 0, dma = 0, mem = 0;
1131 pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1132 while (i < res_cnt) {
1133 switch (resource->type) {
1134 case ACPI_RESOURCE_TYPE_IRQ:
1135 pnpacpi_encode_irq(dev, resource,
1136 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1137 irq++;
1138 break;
1140 case ACPI_RESOURCE_TYPE_DMA:
1141 pnpacpi_encode_dma(dev, resource,
1142 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1143 dma++;
1144 break;
1145 case ACPI_RESOURCE_TYPE_IO:
1146 pnpacpi_encode_io(dev, resource,
1147 pnp_get_resource(dev, IORESOURCE_IO, port));
1148 port++;
1149 break;
1150 case ACPI_RESOURCE_TYPE_FIXED_IO:
1151 pnpacpi_encode_fixed_io(dev, resource,
1152 pnp_get_resource(dev, IORESOURCE_IO, port));
1153 port++;
1154 break;
1155 case ACPI_RESOURCE_TYPE_MEMORY24:
1156 pnpacpi_encode_mem24(dev, resource,
1157 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1158 mem++;
1159 break;
1160 case ACPI_RESOURCE_TYPE_MEMORY32:
1161 pnpacpi_encode_mem32(dev, resource,
1162 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1163 mem++;
1164 break;
1165 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1166 pnpacpi_encode_fixed_mem32(dev, resource,
1167 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1168 mem++;
1169 break;
1170 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1171 pnpacpi_encode_ext_irq(dev, resource,
1172 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1173 irq++;
1174 break;
1175 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1176 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1177 case ACPI_RESOURCE_TYPE_VENDOR:
1178 case ACPI_RESOURCE_TYPE_END_TAG:
1179 case ACPI_RESOURCE_TYPE_ADDRESS16:
1180 case ACPI_RESOURCE_TYPE_ADDRESS32:
1181 case ACPI_RESOURCE_TYPE_ADDRESS64:
1182 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1183 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1184 default: /* other type */
1185 dev_warn(&dev->dev, "can't encode unknown resource "
1186 "type %d\n", resource->type);
1187 return -EINVAL;
1189 resource++;
1190 i++;
1192 return 0;