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
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>
31 #define valid_IRQ(i) (1)
33 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
39 static int irq_flags(int triggering, int polarity, int shareable)
43 if (triggering == ACPI_LEVEL_SENSITIVE) {
44 if (polarity == ACPI_ACTIVE_LOW)
45 flags = IORESOURCE_IRQ_LOWLEVEL;
47 flags = IORESOURCE_IRQ_HIGHLEVEL;
49 if (polarity == ACPI_ACTIVE_LOW)
50 flags = IORESOURCE_IRQ_LOWEDGE;
52 flags = IORESOURCE_IRQ_HIGHEDGE;
55 if (shareable == ACPI_SHARED)
56 flags |= IORESOURCE_IRQ_SHAREABLE;
61 static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
62 int *polarity, int *shareable)
64 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
65 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
66 case IORESOURCE_IRQ_LOWLEVEL:
67 *triggering = ACPI_LEVEL_SENSITIVE;
68 *polarity = ACPI_ACTIVE_LOW;
70 case IORESOURCE_IRQ_HIGHLEVEL:
71 *triggering = ACPI_LEVEL_SENSITIVE;
72 *polarity = ACPI_ACTIVE_HIGH;
74 case IORESOURCE_IRQ_LOWEDGE:
75 *triggering = ACPI_EDGE_SENSITIVE;
76 *polarity = ACPI_ACTIVE_LOW;
78 case IORESOURCE_IRQ_HIGHEDGE:
79 *triggering = ACPI_EDGE_SENSITIVE;
80 *polarity = ACPI_ACTIVE_HIGH;
83 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
85 *triggering = ACPI_EDGE_SENSITIVE;
86 *polarity = ACPI_ACTIVE_HIGH;
90 if (flags & IORESOURCE_IRQ_SHAREABLE)
91 *shareable = ACPI_SHARED;
93 *shareable = ACPI_EXCLUSIVE;
96 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
97 u32 gsi, int triggering,
98 int polarity, int shareable)
103 if (!valid_IRQ(gsi)) {
104 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
109 * in IO-APIC mode, use overrided attribute. Two reasons:
110 * 1. BIOS bug in DSDT
111 * 2. BIOS uses IO-APIC mode Interrupt Source Override
113 if (!acpi_get_override_irq(gsi, &t, &p)) {
114 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
115 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
117 if (triggering != t || polarity != p) {
118 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
119 gsi, t ? "edge":"level", p ? "low":"high");
125 flags = irq_flags(triggering, polarity, shareable);
126 irq = acpi_register_gsi(gsi, triggering, polarity);
128 pcibios_penalize_isa_irq(irq, 1);
130 flags |= IORESOURCE_DISABLED;
132 pnp_add_irq_resource(dev, irq, flags);
135 static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
141 flags |= IORESOURCE_DMA_MASTER;
143 case ACPI_COMPATIBILITY:
144 flags |= IORESOURCE_DMA_COMPATIBLE;
147 flags |= IORESOURCE_DMA_TYPEA;
150 flags |= IORESOURCE_DMA_TYPEB;
153 flags |= IORESOURCE_DMA_TYPEF;
156 /* Set a default value ? */
157 flags |= IORESOURCE_DMA_COMPATIBLE;
158 dev_err(&dev->dev, "invalid DMA type %d\n", type);
161 case ACPI_TRANSFER_8:
162 flags |= IORESOURCE_DMA_8BIT;
164 case ACPI_TRANSFER_8_16:
165 flags |= IORESOURCE_DMA_8AND16BIT;
167 case ACPI_TRANSFER_16:
168 flags |= IORESOURCE_DMA_16BIT;
171 /* Set a default value ? */
172 flags |= IORESOURCE_DMA_8AND16BIT;
173 dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
179 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
180 u64 len, int io_decode)
183 u64 end = start + len - 1;
185 if (io_decode == ACPI_DECODE_16)
186 flags |= IORESOURCE_IO_16BIT_ADDR;
187 if (len == 0 || end >= 0x10003)
188 flags |= IORESOURCE_DISABLED;
190 pnp_add_io_resource(dev, start, end, flags);
194 * Device CSRs that do not appear in PCI config space should be described
195 * via ACPI. This would normally be done with Address Space Descriptors
196 * marked as "consumer-only," but old versions of Windows and Linux ignore
197 * the producer/consumer flag, so HP invented a vendor-defined resource to
198 * describe the location and size of CSR space.
200 static struct acpi_vendor_uuid hp_ccsr_uuid = {
202 .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
203 0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
206 static int vendor_resource_matches(struct pnp_dev *dev,
207 struct acpi_resource_vendor_typed *vendor,
208 struct acpi_vendor_uuid *match,
211 int uuid_len = sizeof(vendor->uuid);
212 u8 uuid_subtype = vendor->uuid_subtype;
213 u8 *uuid = vendor->uuid;
216 /* byte_length includes uuid_subtype and uuid */
217 actual_len = vendor->byte_length - uuid_len - 1;
219 if (uuid_subtype == match->subtype &&
220 uuid_len == sizeof(match->data) &&
221 memcmp(uuid, match->data, uuid_len) == 0) {
222 if (expected_len && expected_len != actual_len) {
223 dev_err(&dev->dev, "wrong vendor descriptor size; "
224 "expected %d, found %d bytes\n",
225 expected_len, actual_len);
235 static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
236 struct acpi_resource_vendor_typed *vendor)
238 if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
241 memcpy(&start, vendor->byte_data, sizeof(start));
242 memcpy(&length, vendor->byte_data + 8, sizeof(length));
244 pnp_add_mem_resource(dev, start, start + length - 1, 0);
248 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
253 u64 end = start + len - 1;
256 flags |= IORESOURCE_DISABLED;
257 if (write_protect == ACPI_READ_WRITE_MEMORY)
258 flags |= IORESOURCE_MEM_WRITEABLE;
260 pnp_add_mem_resource(dev, start, end, flags);
263 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
264 struct acpi_resource *res)
266 struct acpi_resource_address64 addr, *p = &addr;
269 status = acpi_resource_to_address64(res, p);
270 if (!ACPI_SUCCESS(status)) {
271 dev_warn(&dev->dev, "failed to convert resource type %d\n",
276 if (p->producer_consumer == ACPI_PRODUCER)
279 if (p->resource_type == ACPI_MEMORY_RANGE)
280 pnpacpi_parse_allocated_memresource(dev,
281 p->minimum, p->address_length,
282 p->info.mem.write_protect);
283 else if (p->resource_type == ACPI_IO_RANGE)
284 pnpacpi_parse_allocated_ioresource(dev,
285 p->minimum, p->address_length,
286 p->granularity == 0xfff ? ACPI_DECODE_10 :
290 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
293 struct pnp_dev *dev = data;
294 struct acpi_resource_irq *irq;
295 struct acpi_resource_dma *dma;
296 struct acpi_resource_io *io;
297 struct acpi_resource_fixed_io *fixed_io;
298 struct acpi_resource_vendor_typed *vendor_typed;
299 struct acpi_resource_memory24 *memory24;
300 struct acpi_resource_memory32 *memory32;
301 struct acpi_resource_fixed_memory32 *fixed_memory32;
302 struct acpi_resource_extended_irq *extended_irq;
306 case ACPI_RESOURCE_TYPE_IRQ:
308 * Per spec, only one interrupt per descriptor is allowed in
309 * _CRS, but some firmware violates this, so parse them all.
311 irq = &res->data.irq;
312 if (irq->interrupt_count == 0)
313 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
315 for (i = 0; i < irq->interrupt_count; i++) {
316 pnpacpi_parse_allocated_irqresource(dev,
324 * The IRQ encoder puts a single interrupt in each
325 * descriptor, so if a _CRS descriptor has more than
326 * one interrupt, we won't be able to re-encode it.
328 if (pnp_can_write(dev) && irq->interrupt_count > 1) {
329 dev_warn(&dev->dev, "multiple interrupts in "
330 "_CRS descriptor; configuration can't "
332 dev->capabilities &= ~PNP_WRITE;
337 case ACPI_RESOURCE_TYPE_DMA:
338 dma = &res->data.dma;
339 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
340 flags = dma_flags(dev, dma->type, dma->bus_master,
343 flags = IORESOURCE_DISABLED;
344 pnp_add_dma_resource(dev, dma->channels[0], flags);
347 case ACPI_RESOURCE_TYPE_IO:
349 pnpacpi_parse_allocated_ioresource(dev,
355 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
356 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
359 case ACPI_RESOURCE_TYPE_FIXED_IO:
360 fixed_io = &res->data.fixed_io;
361 pnpacpi_parse_allocated_ioresource(dev,
363 fixed_io->address_length,
367 case ACPI_RESOURCE_TYPE_VENDOR:
368 vendor_typed = &res->data.vendor_typed;
369 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
372 case ACPI_RESOURCE_TYPE_END_TAG:
375 case ACPI_RESOURCE_TYPE_MEMORY24:
376 memory24 = &res->data.memory24;
377 pnpacpi_parse_allocated_memresource(dev,
379 memory24->address_length,
380 memory24->write_protect);
382 case ACPI_RESOURCE_TYPE_MEMORY32:
383 memory32 = &res->data.memory32;
384 pnpacpi_parse_allocated_memresource(dev,
386 memory32->address_length,
387 memory32->write_protect);
389 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
390 fixed_memory32 = &res->data.fixed_memory32;
391 pnpacpi_parse_allocated_memresource(dev,
392 fixed_memory32->address,
393 fixed_memory32->address_length,
394 fixed_memory32->write_protect);
396 case ACPI_RESOURCE_TYPE_ADDRESS16:
397 case ACPI_RESOURCE_TYPE_ADDRESS32:
398 case ACPI_RESOURCE_TYPE_ADDRESS64:
399 pnpacpi_parse_allocated_address_space(dev, res);
402 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
403 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
407 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
408 extended_irq = &res->data.extended_irq;
410 if (extended_irq->interrupt_count == 0)
411 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
413 for (i = 0; i < extended_irq->interrupt_count; i++) {
414 pnpacpi_parse_allocated_irqresource(dev,
415 extended_irq->interrupts[i],
416 extended_irq->triggering,
417 extended_irq->polarity,
418 extended_irq->sharable);
422 * The IRQ encoder puts a single interrupt in each
423 * descriptor, so if a _CRS descriptor has more than
424 * one interrupt, we won't be able to re-encode it.
426 if (pnp_can_write(dev) &&
427 extended_irq->interrupt_count > 1) {
428 dev_warn(&dev->dev, "multiple interrupts in "
429 "_CRS descriptor; configuration can't "
431 dev->capabilities &= ~PNP_WRITE;
436 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
440 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
448 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
450 acpi_handle handle = dev->data;
453 pnp_dbg(&dev->dev, "parse allocated resources\n");
455 pnp_init_resources(dev);
457 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
458 pnpacpi_allocated_resource, dev);
460 if (ACPI_FAILURE(status)) {
461 if (status != AE_NOT_FOUND)
462 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
468 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
469 unsigned int option_flags,
470 struct acpi_resource_dma *p)
473 unsigned char map = 0, flags;
475 if (p->channel_count == 0)
478 for (i = 0; i < p->channel_count; i++)
479 map |= 1 << p->channels[i];
481 flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
482 pnp_register_dma_resource(dev, option_flags, map, flags);
485 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
486 unsigned int option_flags,
487 struct acpi_resource_irq *p)
493 if (p->interrupt_count == 0)
496 bitmap_zero(map.bits, PNP_IRQ_NR);
497 for (i = 0; i < p->interrupt_count; i++)
498 if (p->interrupts[i])
499 __set_bit(p->interrupts[i], map.bits);
501 flags = irq_flags(p->triggering, p->polarity, p->sharable);
502 pnp_register_irq_resource(dev, option_flags, &map, flags);
505 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
506 unsigned int option_flags,
507 struct acpi_resource_extended_irq *p)
513 if (p->interrupt_count == 0)
516 bitmap_zero(map.bits, PNP_IRQ_NR);
517 for (i = 0; i < p->interrupt_count; i++) {
518 if (p->interrupts[i]) {
519 if (p->interrupts[i] < PNP_IRQ_NR)
520 __set_bit(p->interrupts[i], map.bits);
522 dev_err(&dev->dev, "ignoring IRQ %d option "
523 "(too large for %d entry bitmap)\n",
524 p->interrupts[i], PNP_IRQ_NR);
528 flags = irq_flags(p->triggering, p->polarity, p->sharable);
529 pnp_register_irq_resource(dev, option_flags, &map, flags);
532 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
533 unsigned int option_flags,
534 struct acpi_resource_io *io)
536 unsigned char flags = 0;
538 if (io->address_length == 0)
541 if (io->io_decode == ACPI_DECODE_16)
542 flags = IORESOURCE_IO_16BIT_ADDR;
543 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
544 io->alignment, io->address_length, flags);
547 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
548 unsigned int option_flags,
549 struct acpi_resource_fixed_io *io)
551 if (io->address_length == 0)
554 pnp_register_port_resource(dev, option_flags, io->address, io->address,
555 0, io->address_length, IORESOURCE_IO_FIXED);
558 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
559 unsigned int option_flags,
560 struct acpi_resource_memory24 *p)
562 unsigned char flags = 0;
564 if (p->address_length == 0)
567 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
568 flags = IORESOURCE_MEM_WRITEABLE;
569 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
570 p->alignment, p->address_length, flags);
573 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
574 unsigned int option_flags,
575 struct acpi_resource_memory32 *p)
577 unsigned char flags = 0;
579 if (p->address_length == 0)
582 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
583 flags = IORESOURCE_MEM_WRITEABLE;
584 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
585 p->alignment, p->address_length, flags);
588 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
589 unsigned int option_flags,
590 struct acpi_resource_fixed_memory32 *p)
592 unsigned char flags = 0;
594 if (p->address_length == 0)
597 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
598 flags = IORESOURCE_MEM_WRITEABLE;
599 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
600 0, p->address_length, flags);
603 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
604 unsigned int option_flags,
605 struct acpi_resource *r)
607 struct acpi_resource_address64 addr, *p = &addr;
609 unsigned char flags = 0;
611 status = acpi_resource_to_address64(r, p);
612 if (ACPI_FAILURE(status)) {
613 dev_warn(&dev->dev, "can't convert resource type %d\n",
618 if (p->address_length == 0)
621 if (p->resource_type == ACPI_MEMORY_RANGE) {
622 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
623 flags = IORESOURCE_MEM_WRITEABLE;
624 pnp_register_mem_resource(dev, option_flags, p->minimum,
625 p->minimum, 0, p->address_length,
627 } else if (p->resource_type == ACPI_IO_RANGE)
628 pnp_register_port_resource(dev, option_flags, p->minimum,
629 p->minimum, 0, p->address_length,
630 IORESOURCE_IO_FIXED);
633 struct acpipnp_parse_option_s {
635 unsigned int option_flags;
638 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
642 struct acpipnp_parse_option_s *parse_data = data;
643 struct pnp_dev *dev = parse_data->dev;
644 unsigned int option_flags = parse_data->option_flags;
647 case ACPI_RESOURCE_TYPE_IRQ:
648 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
651 case ACPI_RESOURCE_TYPE_DMA:
652 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
655 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
656 switch (res->data.start_dpf.compatibility_priority) {
657 case ACPI_GOOD_CONFIGURATION:
658 priority = PNP_RES_PRIORITY_PREFERRED;
661 case ACPI_ACCEPTABLE_CONFIGURATION:
662 priority = PNP_RES_PRIORITY_ACCEPTABLE;
665 case ACPI_SUB_OPTIMAL_CONFIGURATION:
666 priority = PNP_RES_PRIORITY_FUNCTIONAL;
669 priority = PNP_RES_PRIORITY_INVALID;
672 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
675 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
676 parse_data->option_flags = 0;
679 case ACPI_RESOURCE_TYPE_IO:
680 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
683 case ACPI_RESOURCE_TYPE_FIXED_IO:
684 pnpacpi_parse_fixed_port_option(dev, option_flags,
685 &res->data.fixed_io);
688 case ACPI_RESOURCE_TYPE_VENDOR:
689 case ACPI_RESOURCE_TYPE_END_TAG:
692 case ACPI_RESOURCE_TYPE_MEMORY24:
693 pnpacpi_parse_mem24_option(dev, option_flags,
694 &res->data.memory24);
697 case ACPI_RESOURCE_TYPE_MEMORY32:
698 pnpacpi_parse_mem32_option(dev, option_flags,
699 &res->data.memory32);
702 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
703 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
704 &res->data.fixed_memory32);
707 case ACPI_RESOURCE_TYPE_ADDRESS16:
708 case ACPI_RESOURCE_TYPE_ADDRESS32:
709 case ACPI_RESOURCE_TYPE_ADDRESS64:
710 pnpacpi_parse_address_option(dev, option_flags, res);
713 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
716 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
717 pnpacpi_parse_ext_irq_option(dev, option_flags,
718 &res->data.extended_irq);
721 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
725 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
733 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
735 acpi_handle handle = dev->data;
737 struct acpipnp_parse_option_s parse_data;
739 pnp_dbg(&dev->dev, "parse resource options\n");
741 parse_data.dev = dev;
742 parse_data.option_flags = 0;
744 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
745 pnpacpi_option_resource, &parse_data);
747 if (ACPI_FAILURE(status)) {
748 if (status != AE_NOT_FOUND)
749 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
755 static int pnpacpi_supported_resource(struct acpi_resource *res)
758 case ACPI_RESOURCE_TYPE_IRQ:
759 case ACPI_RESOURCE_TYPE_DMA:
760 case ACPI_RESOURCE_TYPE_IO:
761 case ACPI_RESOURCE_TYPE_FIXED_IO:
762 case ACPI_RESOURCE_TYPE_MEMORY24:
763 case ACPI_RESOURCE_TYPE_MEMORY32:
764 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
765 case ACPI_RESOURCE_TYPE_ADDRESS16:
766 case ACPI_RESOURCE_TYPE_ADDRESS32:
767 case ACPI_RESOURCE_TYPE_ADDRESS64:
768 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
777 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
782 if (pnpacpi_supported_resource(res))
787 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
789 struct acpi_resource **resource = data;
791 if (pnpacpi_supported_resource(res)) {
792 (*resource)->type = res->type;
793 (*resource)->length = sizeof(struct acpi_resource);
794 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
795 (*resource)->data.irq.descriptor_length =
796 res->data.irq.descriptor_length;
803 int pnpacpi_build_resource_template(struct pnp_dev *dev,
804 struct acpi_buffer *buffer)
806 acpi_handle handle = dev->data;
807 struct acpi_resource *resource;
811 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
812 pnpacpi_count_resources, &res_cnt);
813 if (ACPI_FAILURE(status)) {
814 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
819 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
820 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
821 if (!buffer->pointer)
824 resource = (struct acpi_resource *)buffer->pointer;
825 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
826 pnpacpi_type_resources, &resource);
827 if (ACPI_FAILURE(status)) {
828 kfree(buffer->pointer);
829 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
832 /* resource will pointer the end resource now */
833 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
838 static void pnpacpi_encode_irq(struct pnp_dev *dev,
839 struct acpi_resource *resource,
842 struct acpi_resource_irq *irq = &resource->data.irq;
843 int triggering, polarity, shareable;
845 if (!pnp_resource_enabled(p)) {
846 irq->interrupt_count = 0;
847 pnp_dbg(&dev->dev, " encode irq (%s)\n",
848 p ? "disabled" : "missing");
852 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
853 irq->triggering = triggering;
854 irq->polarity = polarity;
855 irq->sharable = shareable;
856 irq->interrupt_count = 1;
857 irq->interrupts[0] = p->start;
859 pnp_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
861 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
862 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
863 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
864 irq->descriptor_length);
867 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
868 struct acpi_resource *resource,
871 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
872 int triggering, polarity, shareable;
874 if (!pnp_resource_enabled(p)) {
875 extended_irq->interrupt_count = 0;
876 pnp_dbg(&dev->dev, " encode extended irq (%s)\n",
877 p ? "disabled" : "missing");
881 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
882 extended_irq->producer_consumer = ACPI_CONSUMER;
883 extended_irq->triggering = triggering;
884 extended_irq->polarity = polarity;
885 extended_irq->sharable = shareable;
886 extended_irq->interrupt_count = 1;
887 extended_irq->interrupts[0] = p->start;
889 pnp_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
890 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
891 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
892 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
895 static void pnpacpi_encode_dma(struct pnp_dev *dev,
896 struct acpi_resource *resource,
899 struct acpi_resource_dma *dma = &resource->data.dma;
901 if (!pnp_resource_enabled(p)) {
902 dma->channel_count = 0;
903 pnp_dbg(&dev->dev, " encode dma (%s)\n",
904 p ? "disabled" : "missing");
908 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
909 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
910 case IORESOURCE_DMA_TYPEA:
911 dma->type = ACPI_TYPE_A;
913 case IORESOURCE_DMA_TYPEB:
914 dma->type = ACPI_TYPE_B;
916 case IORESOURCE_DMA_TYPEF:
917 dma->type = ACPI_TYPE_F;
920 dma->type = ACPI_COMPATIBILITY;
923 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
924 case IORESOURCE_DMA_8BIT:
925 dma->transfer = ACPI_TRANSFER_8;
927 case IORESOURCE_DMA_8AND16BIT:
928 dma->transfer = ACPI_TRANSFER_8_16;
931 dma->transfer = ACPI_TRANSFER_16;
934 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
935 dma->channel_count = 1;
936 dma->channels[0] = p->start;
938 pnp_dbg(&dev->dev, " encode dma %d "
939 "type %#x transfer %#x master %d\n",
940 (int) p->start, dma->type, dma->transfer, dma->bus_master);
943 static void pnpacpi_encode_io(struct pnp_dev *dev,
944 struct acpi_resource *resource,
947 struct acpi_resource_io *io = &resource->data.io;
949 if (pnp_resource_enabled(p)) {
950 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
951 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
952 ACPI_DECODE_16 : ACPI_DECODE_10;
953 io->minimum = p->start;
954 io->maximum = p->end;
955 io->alignment = 0; /* Correct? */
956 io->address_length = p->end - p->start + 1;
959 io->address_length = 0;
962 pnp_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
963 io->minimum + io->address_length - 1, io->io_decode);
966 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
967 struct acpi_resource *resource,
970 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
972 if (pnp_resource_enabled(p)) {
973 fixed_io->address = p->start;
974 fixed_io->address_length = p->end - p->start + 1;
976 fixed_io->address = 0;
977 fixed_io->address_length = 0;
980 pnp_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
981 fixed_io->address + fixed_io->address_length - 1);
984 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
985 struct acpi_resource *resource,
988 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
990 if (pnp_resource_enabled(p)) {
991 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
992 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
993 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
994 memory24->minimum = p->start;
995 memory24->maximum = p->end;
996 memory24->alignment = 0;
997 memory24->address_length = p->end - p->start + 1;
999 memory24->minimum = 0;
1000 memory24->address_length = 0;
1003 pnp_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
1005 memory24->minimum + memory24->address_length - 1,
1006 memory24->write_protect);
1009 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1010 struct acpi_resource *resource,
1013 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1015 if (pnp_resource_enabled(p)) {
1016 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1017 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1018 memory32->minimum = p->start;
1019 memory32->maximum = p->end;
1020 memory32->alignment = 0;
1021 memory32->address_length = p->end - p->start + 1;
1023 memory32->minimum = 0;
1024 memory32->alignment = 0;
1027 pnp_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
1029 memory32->minimum + memory32->address_length - 1,
1030 memory32->write_protect);
1033 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1034 struct acpi_resource *resource,
1037 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1039 if (pnp_resource_enabled(p)) {
1040 fixed_memory32->write_protect =
1041 p->flags & IORESOURCE_MEM_WRITEABLE ?
1042 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1043 fixed_memory32->address = p->start;
1044 fixed_memory32->address_length = p->end - p->start + 1;
1046 fixed_memory32->address = 0;
1047 fixed_memory32->address_length = 0;
1050 pnp_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
1051 fixed_memory32->address,
1052 fixed_memory32->address + fixed_memory32->address_length - 1,
1053 fixed_memory32->write_protect);
1056 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1059 /* pnpacpi_build_resource_template allocates extra mem */
1060 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1061 struct acpi_resource *resource = buffer->pointer;
1062 int port = 0, irq = 0, dma = 0, mem = 0;
1064 pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1065 while (i < res_cnt) {
1066 switch (resource->type) {
1067 case ACPI_RESOURCE_TYPE_IRQ:
1068 pnpacpi_encode_irq(dev, resource,
1069 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1073 case ACPI_RESOURCE_TYPE_DMA:
1074 pnpacpi_encode_dma(dev, resource,
1075 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1078 case ACPI_RESOURCE_TYPE_IO:
1079 pnpacpi_encode_io(dev, resource,
1080 pnp_get_resource(dev, IORESOURCE_IO, port));
1083 case ACPI_RESOURCE_TYPE_FIXED_IO:
1084 pnpacpi_encode_fixed_io(dev, resource,
1085 pnp_get_resource(dev, IORESOURCE_IO, port));
1088 case ACPI_RESOURCE_TYPE_MEMORY24:
1089 pnpacpi_encode_mem24(dev, resource,
1090 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1093 case ACPI_RESOURCE_TYPE_MEMORY32:
1094 pnpacpi_encode_mem32(dev, resource,
1095 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1098 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1099 pnpacpi_encode_fixed_mem32(dev, resource,
1100 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1103 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1104 pnpacpi_encode_ext_irq(dev, resource,
1105 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1108 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1109 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1110 case ACPI_RESOURCE_TYPE_VENDOR:
1111 case ACPI_RESOURCE_TYPE_END_TAG:
1112 case ACPI_RESOURCE_TYPE_ADDRESS16:
1113 case ACPI_RESOURCE_TYPE_ADDRESS32:
1114 case ACPI_RESOURCE_TYPE_ADDRESS64:
1115 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1116 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1117 default: /* other type */
1118 dev_warn(&dev->dev, "can't encode unknown resource "
1119 "type %d\n", resource->type);