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(int type, int bus_master, int transfer)
140 flags |= IORESOURCE_DMA_MASTER;
142 case ACPI_COMPATIBILITY:
143 flags |= IORESOURCE_DMA_COMPATIBLE;
146 flags |= IORESOURCE_DMA_TYPEA;
149 flags |= IORESOURCE_DMA_TYPEB;
152 flags |= IORESOURCE_DMA_TYPEF;
155 /* Set a default value ? */
156 flags |= IORESOURCE_DMA_COMPATIBLE;
157 pnp_err("Invalid DMA type");
160 case ACPI_TRANSFER_8:
161 flags |= IORESOURCE_DMA_8BIT;
163 case ACPI_TRANSFER_8_16:
164 flags |= IORESOURCE_DMA_8AND16BIT;
166 case ACPI_TRANSFER_16:
167 flags |= IORESOURCE_DMA_16BIT;
170 /* Set a default value ? */
171 flags |= IORESOURCE_DMA_8AND16BIT;
172 pnp_err("Invalid DMA transfer type");
178 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
179 u64 len, int io_decode)
182 u64 end = start + len - 1;
184 if (io_decode == ACPI_DECODE_16)
185 flags |= IORESOURCE_IO_16BIT_ADDR;
186 if (len == 0 || end >= 0x10003)
187 flags |= IORESOURCE_DISABLED;
189 pnp_add_io_resource(dev, start, end, flags);
193 * Device CSRs that do not appear in PCI config space should be described
194 * via ACPI. This would normally be done with Address Space Descriptors
195 * marked as "consumer-only," but old versions of Windows and Linux ignore
196 * the producer/consumer flag, so HP invented a vendor-defined resource to
197 * describe the location and size of CSR space.
199 static struct acpi_vendor_uuid hp_ccsr_uuid = {
201 .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
202 0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
205 static int vendor_resource_matches(struct pnp_dev *dev,
206 struct acpi_resource_vendor_typed *vendor,
207 struct acpi_vendor_uuid *match,
210 int uuid_len = sizeof(vendor->uuid);
211 u8 uuid_subtype = vendor->uuid_subtype;
212 u8 *uuid = vendor->uuid;
215 /* byte_length includes uuid_subtype and uuid */
216 actual_len = vendor->byte_length - uuid_len - 1;
218 if (uuid_subtype == match->subtype &&
219 uuid_len == sizeof(match->data) &&
220 memcmp(uuid, match->data, uuid_len) == 0) {
221 if (expected_len && expected_len != actual_len) {
222 dev_err(&dev->dev, "wrong vendor descriptor size; "
223 "expected %d, found %d bytes\n",
224 expected_len, actual_len);
234 static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
235 struct acpi_resource_vendor_typed *vendor)
237 if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
240 memcpy(&start, vendor->byte_data, sizeof(start));
241 memcpy(&length, vendor->byte_data + 8, sizeof(length));
243 pnp_add_mem_resource(dev, start, start + length - 1, 0);
247 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
252 u64 end = start + len - 1;
255 flags |= IORESOURCE_DISABLED;
256 if (write_protect == ACPI_READ_WRITE_MEMORY)
257 flags |= IORESOURCE_MEM_WRITEABLE;
259 pnp_add_mem_resource(dev, start, end, flags);
262 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
263 struct acpi_resource *res)
265 struct acpi_resource_address64 addr, *p = &addr;
268 status = acpi_resource_to_address64(res, p);
269 if (!ACPI_SUCCESS(status)) {
270 dev_warn(&dev->dev, "failed to convert resource type %d\n",
275 if (p->producer_consumer == ACPI_PRODUCER)
278 if (p->resource_type == ACPI_MEMORY_RANGE)
279 pnpacpi_parse_allocated_memresource(dev,
280 p->minimum, p->address_length,
281 p->info.mem.write_protect);
282 else if (p->resource_type == ACPI_IO_RANGE)
283 pnpacpi_parse_allocated_ioresource(dev,
284 p->minimum, p->address_length,
285 p->granularity == 0xfff ? ACPI_DECODE_10 :
289 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
292 struct pnp_dev *dev = data;
293 struct acpi_resource_irq *irq;
294 struct acpi_resource_dma *dma;
295 struct acpi_resource_io *io;
296 struct acpi_resource_fixed_io *fixed_io;
297 struct acpi_resource_vendor_typed *vendor_typed;
298 struct acpi_resource_memory24 *memory24;
299 struct acpi_resource_memory32 *memory32;
300 struct acpi_resource_fixed_memory32 *fixed_memory32;
301 struct acpi_resource_extended_irq *extended_irq;
305 case ACPI_RESOURCE_TYPE_IRQ:
307 * Per spec, only one interrupt per descriptor is allowed in
308 * _CRS, but some firmware violates this, so parse them all.
310 irq = &res->data.irq;
311 if (irq->interrupt_count == 0)
312 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
314 for (i = 0; i < irq->interrupt_count; i++) {
315 pnpacpi_parse_allocated_irqresource(dev,
323 * The IRQ encoder puts a single interrupt in each
324 * descriptor, so if a _CRS descriptor has more than
325 * one interrupt, we won't be able to re-encode it.
327 if (pnp_can_write(dev) && irq->interrupt_count > 1) {
328 dev_warn(&dev->dev, "multiple interrupts in "
329 "_CRS descriptor; configuration can't "
331 dev->capabilities &= ~PNP_WRITE;
336 case ACPI_RESOURCE_TYPE_DMA:
337 dma = &res->data.dma;
338 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
339 flags = dma_flags(dma->type, dma->bus_master,
342 flags = IORESOURCE_DISABLED;
343 pnp_add_dma_resource(dev, dma->channels[0], flags);
346 case ACPI_RESOURCE_TYPE_IO:
348 pnpacpi_parse_allocated_ioresource(dev,
354 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
355 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
358 case ACPI_RESOURCE_TYPE_FIXED_IO:
359 fixed_io = &res->data.fixed_io;
360 pnpacpi_parse_allocated_ioresource(dev,
362 fixed_io->address_length,
366 case ACPI_RESOURCE_TYPE_VENDOR:
367 vendor_typed = &res->data.vendor_typed;
368 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
371 case ACPI_RESOURCE_TYPE_END_TAG:
374 case ACPI_RESOURCE_TYPE_MEMORY24:
375 memory24 = &res->data.memory24;
376 pnpacpi_parse_allocated_memresource(dev,
378 memory24->address_length,
379 memory24->write_protect);
381 case ACPI_RESOURCE_TYPE_MEMORY32:
382 memory32 = &res->data.memory32;
383 pnpacpi_parse_allocated_memresource(dev,
385 memory32->address_length,
386 memory32->write_protect);
388 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
389 fixed_memory32 = &res->data.fixed_memory32;
390 pnpacpi_parse_allocated_memresource(dev,
391 fixed_memory32->address,
392 fixed_memory32->address_length,
393 fixed_memory32->write_protect);
395 case ACPI_RESOURCE_TYPE_ADDRESS16:
396 case ACPI_RESOURCE_TYPE_ADDRESS32:
397 case ACPI_RESOURCE_TYPE_ADDRESS64:
398 pnpacpi_parse_allocated_address_space(dev, res);
401 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
402 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
406 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
407 extended_irq = &res->data.extended_irq;
408 if (extended_irq->producer_consumer == ACPI_PRODUCER)
411 if (extended_irq->interrupt_count == 0)
412 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
414 for (i = 0; i < extended_irq->interrupt_count; i++) {
415 pnpacpi_parse_allocated_irqresource(dev,
416 extended_irq->interrupts[i],
417 extended_irq->triggering,
418 extended_irq->polarity,
419 extended_irq->sharable);
423 * The IRQ encoder puts a single interrupt in each
424 * descriptor, so if a _CRS descriptor has more than
425 * one interrupt, we won't be able to re-encode it.
427 if (pnp_can_write(dev) &&
428 extended_irq->interrupt_count > 1) {
429 dev_warn(&dev->dev, "multiple interrupts in "
430 "_CRS descriptor; configuration can't "
432 dev->capabilities &= ~PNP_WRITE;
437 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
441 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
449 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
451 acpi_handle handle = dev->data;
454 dev_dbg(&dev->dev, "parse allocated resources\n");
456 pnp_init_resources(dev);
458 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
459 pnpacpi_allocated_resource, dev);
461 if (ACPI_FAILURE(status)) {
462 if (status != AE_NOT_FOUND)
463 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
469 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
470 unsigned int option_flags,
471 struct acpi_resource_dma *p)
474 unsigned char map = 0, flags;
476 if (p->channel_count == 0)
479 for (i = 0; i < p->channel_count; i++)
480 map |= 1 << p->channels[i];
482 flags = dma_flags(p->type, p->bus_master, p->transfer);
483 pnp_register_dma_resource(dev, option_flags, map, flags);
486 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
487 unsigned int option_flags,
488 struct acpi_resource_irq *p)
494 if (p->interrupt_count == 0)
497 bitmap_zero(map.bits, PNP_IRQ_NR);
498 for (i = 0; i < p->interrupt_count; i++)
499 if (p->interrupts[i])
500 __set_bit(p->interrupts[i], map.bits);
502 flags = irq_flags(p->triggering, p->polarity, p->sharable);
503 pnp_register_irq_resource(dev, option_flags, &map, flags);
506 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
507 unsigned int option_flags,
508 struct acpi_resource_extended_irq *p)
514 if (p->interrupt_count == 0)
517 bitmap_zero(map.bits, PNP_IRQ_NR);
518 for (i = 0; i < p->interrupt_count; i++) {
519 if (p->interrupts[i]) {
520 if (p->interrupts[i] < PNP_IRQ_NR)
521 __set_bit(p->interrupts[i], map.bits);
523 dev_err(&dev->dev, "ignoring IRQ %d option "
524 "(too large for %d entry bitmap)\n",
525 p->interrupts[i], PNP_IRQ_NR);
529 flags = irq_flags(p->triggering, p->polarity, p->sharable);
530 pnp_register_irq_resource(dev, option_flags, &map, flags);
533 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
534 unsigned int option_flags,
535 struct acpi_resource_io *io)
537 unsigned char flags = 0;
539 if (io->address_length == 0)
542 if (io->io_decode == ACPI_DECODE_16)
543 flags = IORESOURCE_IO_16BIT_ADDR;
544 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
545 io->alignment, io->address_length, flags);
548 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
549 unsigned int option_flags,
550 struct acpi_resource_fixed_io *io)
552 if (io->address_length == 0)
555 pnp_register_port_resource(dev, option_flags, io->address, io->address,
556 0, io->address_length, IORESOURCE_IO_FIXED);
559 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
560 unsigned int option_flags,
561 struct acpi_resource_memory24 *p)
563 unsigned char flags = 0;
565 if (p->address_length == 0)
568 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
569 flags = IORESOURCE_MEM_WRITEABLE;
570 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
571 p->alignment, p->address_length, flags);
574 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
575 unsigned int option_flags,
576 struct acpi_resource_memory32 *p)
578 unsigned char flags = 0;
580 if (p->address_length == 0)
583 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
584 flags = IORESOURCE_MEM_WRITEABLE;
585 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
586 p->alignment, p->address_length, flags);
589 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
590 unsigned int option_flags,
591 struct acpi_resource_fixed_memory32 *p)
593 unsigned char flags = 0;
595 if (p->address_length == 0)
598 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
599 flags = IORESOURCE_MEM_WRITEABLE;
600 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
601 0, p->address_length, flags);
604 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
605 unsigned int option_flags,
606 struct acpi_resource *r)
608 struct acpi_resource_address64 addr, *p = &addr;
610 unsigned char flags = 0;
612 status = acpi_resource_to_address64(r, p);
613 if (!ACPI_SUCCESS(status)) {
614 pnp_warn("PnPACPI: failed to convert resource type %d",
619 if (p->address_length == 0)
622 if (p->resource_type == ACPI_MEMORY_RANGE) {
623 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
624 flags = IORESOURCE_MEM_WRITEABLE;
625 pnp_register_mem_resource(dev, option_flags, p->minimum,
626 p->minimum, 0, p->address_length,
628 } else if (p->resource_type == ACPI_IO_RANGE)
629 pnp_register_port_resource(dev, option_flags, p->minimum,
630 p->minimum, 0, p->address_length,
631 IORESOURCE_IO_FIXED);
634 struct acpipnp_parse_option_s {
636 unsigned int option_flags;
639 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
643 struct acpipnp_parse_option_s *parse_data = data;
644 struct pnp_dev *dev = parse_data->dev;
645 unsigned int option_flags = parse_data->option_flags;
648 case ACPI_RESOURCE_TYPE_IRQ:
649 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
652 case ACPI_RESOURCE_TYPE_DMA:
653 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
656 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
657 switch (res->data.start_dpf.compatibility_priority) {
658 case ACPI_GOOD_CONFIGURATION:
659 priority = PNP_RES_PRIORITY_PREFERRED;
662 case ACPI_ACCEPTABLE_CONFIGURATION:
663 priority = PNP_RES_PRIORITY_ACCEPTABLE;
666 case ACPI_SUB_OPTIMAL_CONFIGURATION:
667 priority = PNP_RES_PRIORITY_FUNCTIONAL;
670 priority = PNP_RES_PRIORITY_INVALID;
673 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
676 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
677 parse_data->option_flags = 0;
680 case ACPI_RESOURCE_TYPE_IO:
681 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
684 case ACPI_RESOURCE_TYPE_FIXED_IO:
685 pnpacpi_parse_fixed_port_option(dev, option_flags,
686 &res->data.fixed_io);
689 case ACPI_RESOURCE_TYPE_VENDOR:
690 case ACPI_RESOURCE_TYPE_END_TAG:
693 case ACPI_RESOURCE_TYPE_MEMORY24:
694 pnpacpi_parse_mem24_option(dev, option_flags,
695 &res->data.memory24);
698 case ACPI_RESOURCE_TYPE_MEMORY32:
699 pnpacpi_parse_mem32_option(dev, option_flags,
700 &res->data.memory32);
703 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
704 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
705 &res->data.fixed_memory32);
708 case ACPI_RESOURCE_TYPE_ADDRESS16:
709 case ACPI_RESOURCE_TYPE_ADDRESS32:
710 case ACPI_RESOURCE_TYPE_ADDRESS64:
711 pnpacpi_parse_address_option(dev, option_flags, res);
714 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
717 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
718 pnpacpi_parse_ext_irq_option(dev, option_flags,
719 &res->data.extended_irq);
722 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
726 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
734 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
736 acpi_handle handle = dev->data;
738 struct acpipnp_parse_option_s parse_data;
740 dev_dbg(&dev->dev, "parse resource options\n");
742 parse_data.dev = dev;
743 parse_data.option_flags = 0;
745 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
746 pnpacpi_option_resource, &parse_data);
748 if (ACPI_FAILURE(status)) {
749 if (status != AE_NOT_FOUND)
750 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
756 static int pnpacpi_supported_resource(struct acpi_resource *res)
759 case ACPI_RESOURCE_TYPE_IRQ:
760 case ACPI_RESOURCE_TYPE_DMA:
761 case ACPI_RESOURCE_TYPE_IO:
762 case ACPI_RESOURCE_TYPE_FIXED_IO:
763 case ACPI_RESOURCE_TYPE_MEMORY24:
764 case ACPI_RESOURCE_TYPE_MEMORY32:
765 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
766 case ACPI_RESOURCE_TYPE_ADDRESS16:
767 case ACPI_RESOURCE_TYPE_ADDRESS32:
768 case ACPI_RESOURCE_TYPE_ADDRESS64:
769 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
778 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
783 if (pnpacpi_supported_resource(res))
788 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
790 struct acpi_resource **resource = data;
792 if (pnpacpi_supported_resource(res)) {
793 (*resource)->type = res->type;
794 (*resource)->length = sizeof(struct acpi_resource);
795 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
796 (*resource)->data.irq.descriptor_length =
797 res->data.irq.descriptor_length;
804 int pnpacpi_build_resource_template(struct pnp_dev *dev,
805 struct acpi_buffer *buffer)
807 acpi_handle handle = dev->data;
808 struct acpi_resource *resource;
812 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
813 pnpacpi_count_resources, &res_cnt);
814 if (ACPI_FAILURE(status)) {
815 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
820 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
821 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
822 if (!buffer->pointer)
825 resource = (struct acpi_resource *)buffer->pointer;
826 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
827 pnpacpi_type_resources, &resource);
828 if (ACPI_FAILURE(status)) {
829 kfree(buffer->pointer);
830 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
833 /* resource will pointer the end resource now */
834 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
839 static void pnpacpi_encode_irq(struct pnp_dev *dev,
840 struct acpi_resource *resource,
843 struct acpi_resource_irq *irq = &resource->data.irq;
844 int triggering, polarity, shareable;
846 if (!pnp_resource_enabled(p)) {
847 irq->interrupt_count = 0;
848 dev_dbg(&dev->dev, " encode irq (%s)\n",
849 p ? "disabled" : "missing");
853 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
854 irq->triggering = triggering;
855 irq->polarity = polarity;
856 irq->sharable = shareable;
857 irq->interrupt_count = 1;
858 irq->interrupts[0] = p->start;
860 dev_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
862 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
863 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
864 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
865 irq->descriptor_length);
868 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
869 struct acpi_resource *resource,
872 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
873 int triggering, polarity, shareable;
875 if (!pnp_resource_enabled(p)) {
876 extended_irq->interrupt_count = 0;
877 dev_dbg(&dev->dev, " encode extended irq (%s)\n",
878 p ? "disabled" : "missing");
882 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
883 extended_irq->producer_consumer = ACPI_CONSUMER;
884 extended_irq->triggering = triggering;
885 extended_irq->polarity = polarity;
886 extended_irq->sharable = shareable;
887 extended_irq->interrupt_count = 1;
888 extended_irq->interrupts[0] = p->start;
890 dev_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
891 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
892 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
893 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
896 static void pnpacpi_encode_dma(struct pnp_dev *dev,
897 struct acpi_resource *resource,
900 struct acpi_resource_dma *dma = &resource->data.dma;
902 if (!pnp_resource_enabled(p)) {
903 dma->channel_count = 0;
904 dev_dbg(&dev->dev, " encode dma (%s)\n",
905 p ? "disabled" : "missing");
909 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
910 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
911 case IORESOURCE_DMA_TYPEA:
912 dma->type = ACPI_TYPE_A;
914 case IORESOURCE_DMA_TYPEB:
915 dma->type = ACPI_TYPE_B;
917 case IORESOURCE_DMA_TYPEF:
918 dma->type = ACPI_TYPE_F;
921 dma->type = ACPI_COMPATIBILITY;
924 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
925 case IORESOURCE_DMA_8BIT:
926 dma->transfer = ACPI_TRANSFER_8;
928 case IORESOURCE_DMA_8AND16BIT:
929 dma->transfer = ACPI_TRANSFER_8_16;
932 dma->transfer = ACPI_TRANSFER_16;
935 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
936 dma->channel_count = 1;
937 dma->channels[0] = p->start;
939 dev_dbg(&dev->dev, " encode dma %d "
940 "type %#x transfer %#x master %d\n",
941 (int) p->start, dma->type, dma->transfer, dma->bus_master);
944 static void pnpacpi_encode_io(struct pnp_dev *dev,
945 struct acpi_resource *resource,
948 struct acpi_resource_io *io = &resource->data.io;
950 if (pnp_resource_enabled(p)) {
951 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
952 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
953 ACPI_DECODE_16 : ACPI_DECODE_10;
954 io->minimum = p->start;
955 io->maximum = p->end;
956 io->alignment = 0; /* Correct? */
957 io->address_length = p->end - p->start + 1;
960 io->address_length = 0;
963 dev_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
964 io->minimum + io->address_length - 1, io->io_decode);
967 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
968 struct acpi_resource *resource,
971 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
973 if (pnp_resource_enabled(p)) {
974 fixed_io->address = p->start;
975 fixed_io->address_length = p->end - p->start + 1;
977 fixed_io->address = 0;
978 fixed_io->address_length = 0;
981 dev_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
982 fixed_io->address + fixed_io->address_length - 1);
985 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
986 struct acpi_resource *resource,
989 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
991 if (pnp_resource_enabled(p)) {
992 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
993 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
994 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
995 memory24->minimum = p->start;
996 memory24->maximum = p->end;
997 memory24->alignment = 0;
998 memory24->address_length = p->end - p->start + 1;
1000 memory24->minimum = 0;
1001 memory24->address_length = 0;
1004 dev_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
1006 memory24->minimum + memory24->address_length - 1,
1007 memory24->write_protect);
1010 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1011 struct acpi_resource *resource,
1014 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1016 if (pnp_resource_enabled(p)) {
1017 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1018 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1019 memory32->minimum = p->start;
1020 memory32->maximum = p->end;
1021 memory32->alignment = 0;
1022 memory32->address_length = p->end - p->start + 1;
1024 memory32->minimum = 0;
1025 memory32->alignment = 0;
1028 dev_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
1030 memory32->minimum + memory32->address_length - 1,
1031 memory32->write_protect);
1034 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1035 struct acpi_resource *resource,
1038 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1040 if (pnp_resource_enabled(p)) {
1041 fixed_memory32->write_protect =
1042 p->flags & IORESOURCE_MEM_WRITEABLE ?
1043 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1044 fixed_memory32->address = p->start;
1045 fixed_memory32->address_length = p->end - p->start + 1;
1047 fixed_memory32->address = 0;
1048 fixed_memory32->address_length = 0;
1051 dev_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
1052 fixed_memory32->address,
1053 fixed_memory32->address + fixed_memory32->address_length - 1,
1054 fixed_memory32->write_protect);
1057 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1060 /* pnpacpi_build_resource_template allocates extra mem */
1061 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1062 struct acpi_resource *resource = buffer->pointer;
1063 int port = 0, irq = 0, dma = 0, mem = 0;
1065 dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1066 while (i < res_cnt) {
1067 switch (resource->type) {
1068 case ACPI_RESOURCE_TYPE_IRQ:
1069 pnpacpi_encode_irq(dev, resource,
1070 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1074 case ACPI_RESOURCE_TYPE_DMA:
1075 pnpacpi_encode_dma(dev, resource,
1076 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1079 case ACPI_RESOURCE_TYPE_IO:
1080 pnpacpi_encode_io(dev, resource,
1081 pnp_get_resource(dev, IORESOURCE_IO, port));
1084 case ACPI_RESOURCE_TYPE_FIXED_IO:
1085 pnpacpi_encode_fixed_io(dev, resource,
1086 pnp_get_resource(dev, IORESOURCE_IO, port));
1089 case ACPI_RESOURCE_TYPE_MEMORY24:
1090 pnpacpi_encode_mem24(dev, resource,
1091 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1094 case ACPI_RESOURCE_TYPE_MEMORY32:
1095 pnpacpi_encode_mem32(dev, resource,
1096 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1099 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1100 pnpacpi_encode_fixed_mem32(dev, resource,
1101 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1104 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1105 pnpacpi_encode_ext_irq(dev, resource,
1106 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1109 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1110 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1111 case ACPI_RESOURCE_TYPE_VENDOR:
1112 case ACPI_RESOURCE_TYPE_END_TAG:
1113 case ACPI_RESOURCE_TYPE_ADDRESS16:
1114 case ACPI_RESOURCE_TYPE_ADDRESS32:
1115 case ACPI_RESOURCE_TYPE_ADDRESS64:
1116 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1117 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1118 default: /* other type */
1119 dev_warn(&dev->dev, "can't encode unknown resource "
1120 "type %d\n", resource->type);