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;
409 if (extended_irq->interrupt_count == 0)
410 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
412 for (i = 0; i < extended_irq->interrupt_count; i++) {
413 pnpacpi_parse_allocated_irqresource(dev,
414 extended_irq->interrupts[i],
415 extended_irq->triggering,
416 extended_irq->polarity,
417 extended_irq->sharable);
421 * The IRQ encoder puts a single interrupt in each
422 * descriptor, so if a _CRS descriptor has more than
423 * one interrupt, we won't be able to re-encode it.
425 if (pnp_can_write(dev) &&
426 extended_irq->interrupt_count > 1) {
427 dev_warn(&dev->dev, "multiple interrupts in "
428 "_CRS descriptor; configuration can't "
430 dev->capabilities &= ~PNP_WRITE;
435 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
439 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
447 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
449 acpi_handle handle = dev->data;
452 dev_dbg(&dev->dev, "parse allocated resources\n");
454 pnp_init_resources(dev);
456 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
457 pnpacpi_allocated_resource, dev);
459 if (ACPI_FAILURE(status)) {
460 if (status != AE_NOT_FOUND)
461 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
467 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
468 unsigned int option_flags,
469 struct acpi_resource_dma *p)
472 unsigned char map = 0, flags;
474 if (p->channel_count == 0)
477 for (i = 0; i < p->channel_count; i++)
478 map |= 1 << p->channels[i];
480 flags = dma_flags(p->type, p->bus_master, p->transfer);
481 pnp_register_dma_resource(dev, option_flags, map, flags);
484 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
485 unsigned int option_flags,
486 struct acpi_resource_irq *p)
492 if (p->interrupt_count == 0)
495 bitmap_zero(map.bits, PNP_IRQ_NR);
496 for (i = 0; i < p->interrupt_count; i++)
497 if (p->interrupts[i])
498 __set_bit(p->interrupts[i], map.bits);
500 flags = irq_flags(p->triggering, p->polarity, p->sharable);
501 pnp_register_irq_resource(dev, option_flags, &map, flags);
504 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
505 unsigned int option_flags,
506 struct acpi_resource_extended_irq *p)
512 if (p->interrupt_count == 0)
515 bitmap_zero(map.bits, PNP_IRQ_NR);
516 for (i = 0; i < p->interrupt_count; i++) {
517 if (p->interrupts[i]) {
518 if (p->interrupts[i] < PNP_IRQ_NR)
519 __set_bit(p->interrupts[i], map.bits);
521 dev_err(&dev->dev, "ignoring IRQ %d option "
522 "(too large for %d entry bitmap)\n",
523 p->interrupts[i], PNP_IRQ_NR);
527 flags = irq_flags(p->triggering, p->polarity, p->sharable);
528 pnp_register_irq_resource(dev, option_flags, &map, flags);
531 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
532 unsigned int option_flags,
533 struct acpi_resource_io *io)
535 unsigned char flags = 0;
537 if (io->address_length == 0)
540 if (io->io_decode == ACPI_DECODE_16)
541 flags = IORESOURCE_IO_16BIT_ADDR;
542 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
543 io->alignment, io->address_length, flags);
546 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
547 unsigned int option_flags,
548 struct acpi_resource_fixed_io *io)
550 if (io->address_length == 0)
553 pnp_register_port_resource(dev, option_flags, io->address, io->address,
554 0, io->address_length, IORESOURCE_IO_FIXED);
557 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
558 unsigned int option_flags,
559 struct acpi_resource_memory24 *p)
561 unsigned char flags = 0;
563 if (p->address_length == 0)
566 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
567 flags = IORESOURCE_MEM_WRITEABLE;
568 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
569 p->alignment, p->address_length, flags);
572 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
573 unsigned int option_flags,
574 struct acpi_resource_memory32 *p)
576 unsigned char flags = 0;
578 if (p->address_length == 0)
581 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
582 flags = IORESOURCE_MEM_WRITEABLE;
583 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
584 p->alignment, p->address_length, flags);
587 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
588 unsigned int option_flags,
589 struct acpi_resource_fixed_memory32 *p)
591 unsigned char flags = 0;
593 if (p->address_length == 0)
596 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
597 flags = IORESOURCE_MEM_WRITEABLE;
598 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
599 0, p->address_length, flags);
602 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
603 unsigned int option_flags,
604 struct acpi_resource *r)
606 struct acpi_resource_address64 addr, *p = &addr;
608 unsigned char flags = 0;
610 status = acpi_resource_to_address64(r, p);
611 if (!ACPI_SUCCESS(status)) {
612 pnp_warn("PnPACPI: failed to convert resource type %d",
617 if (p->address_length == 0)
620 if (p->resource_type == ACPI_MEMORY_RANGE) {
621 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
622 flags = IORESOURCE_MEM_WRITEABLE;
623 pnp_register_mem_resource(dev, option_flags, p->minimum,
624 p->minimum, 0, p->address_length,
626 } else if (p->resource_type == ACPI_IO_RANGE)
627 pnp_register_port_resource(dev, option_flags, p->minimum,
628 p->minimum, 0, p->address_length,
629 IORESOURCE_IO_FIXED);
632 struct acpipnp_parse_option_s {
634 unsigned int option_flags;
637 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
641 struct acpipnp_parse_option_s *parse_data = data;
642 struct pnp_dev *dev = parse_data->dev;
643 unsigned int option_flags = parse_data->option_flags;
646 case ACPI_RESOURCE_TYPE_IRQ:
647 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
650 case ACPI_RESOURCE_TYPE_DMA:
651 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
654 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
655 switch (res->data.start_dpf.compatibility_priority) {
656 case ACPI_GOOD_CONFIGURATION:
657 priority = PNP_RES_PRIORITY_PREFERRED;
660 case ACPI_ACCEPTABLE_CONFIGURATION:
661 priority = PNP_RES_PRIORITY_ACCEPTABLE;
664 case ACPI_SUB_OPTIMAL_CONFIGURATION:
665 priority = PNP_RES_PRIORITY_FUNCTIONAL;
668 priority = PNP_RES_PRIORITY_INVALID;
671 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
674 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
675 parse_data->option_flags = 0;
678 case ACPI_RESOURCE_TYPE_IO:
679 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
682 case ACPI_RESOURCE_TYPE_FIXED_IO:
683 pnpacpi_parse_fixed_port_option(dev, option_flags,
684 &res->data.fixed_io);
687 case ACPI_RESOURCE_TYPE_VENDOR:
688 case ACPI_RESOURCE_TYPE_END_TAG:
691 case ACPI_RESOURCE_TYPE_MEMORY24:
692 pnpacpi_parse_mem24_option(dev, option_flags,
693 &res->data.memory24);
696 case ACPI_RESOURCE_TYPE_MEMORY32:
697 pnpacpi_parse_mem32_option(dev, option_flags,
698 &res->data.memory32);
701 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
702 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
703 &res->data.fixed_memory32);
706 case ACPI_RESOURCE_TYPE_ADDRESS16:
707 case ACPI_RESOURCE_TYPE_ADDRESS32:
708 case ACPI_RESOURCE_TYPE_ADDRESS64:
709 pnpacpi_parse_address_option(dev, option_flags, res);
712 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
715 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
716 pnpacpi_parse_ext_irq_option(dev, option_flags,
717 &res->data.extended_irq);
720 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
724 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
732 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
734 acpi_handle handle = dev->data;
736 struct acpipnp_parse_option_s parse_data;
738 dev_dbg(&dev->dev, "parse resource options\n");
740 parse_data.dev = dev;
741 parse_data.option_flags = 0;
743 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
744 pnpacpi_option_resource, &parse_data);
746 if (ACPI_FAILURE(status)) {
747 if (status != AE_NOT_FOUND)
748 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
754 static int pnpacpi_supported_resource(struct acpi_resource *res)
757 case ACPI_RESOURCE_TYPE_IRQ:
758 case ACPI_RESOURCE_TYPE_DMA:
759 case ACPI_RESOURCE_TYPE_IO:
760 case ACPI_RESOURCE_TYPE_FIXED_IO:
761 case ACPI_RESOURCE_TYPE_MEMORY24:
762 case ACPI_RESOURCE_TYPE_MEMORY32:
763 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
764 case ACPI_RESOURCE_TYPE_ADDRESS16:
765 case ACPI_RESOURCE_TYPE_ADDRESS32:
766 case ACPI_RESOURCE_TYPE_ADDRESS64:
767 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
776 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
781 if (pnpacpi_supported_resource(res))
786 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
788 struct acpi_resource **resource = data;
790 if (pnpacpi_supported_resource(res)) {
791 (*resource)->type = res->type;
792 (*resource)->length = sizeof(struct acpi_resource);
793 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
794 (*resource)->data.irq.descriptor_length =
795 res->data.irq.descriptor_length;
802 int pnpacpi_build_resource_template(struct pnp_dev *dev,
803 struct acpi_buffer *buffer)
805 acpi_handle handle = dev->data;
806 struct acpi_resource *resource;
810 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
811 pnpacpi_count_resources, &res_cnt);
812 if (ACPI_FAILURE(status)) {
813 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
818 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
819 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
820 if (!buffer->pointer)
823 resource = (struct acpi_resource *)buffer->pointer;
824 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
825 pnpacpi_type_resources, &resource);
826 if (ACPI_FAILURE(status)) {
827 kfree(buffer->pointer);
828 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
831 /* resource will pointer the end resource now */
832 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
837 static void pnpacpi_encode_irq(struct pnp_dev *dev,
838 struct acpi_resource *resource,
841 struct acpi_resource_irq *irq = &resource->data.irq;
842 int triggering, polarity, shareable;
844 if (!pnp_resource_enabled(p)) {
845 irq->interrupt_count = 0;
846 dev_dbg(&dev->dev, " encode irq (%s)\n",
847 p ? "disabled" : "missing");
851 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
852 irq->triggering = triggering;
853 irq->polarity = polarity;
854 irq->sharable = shareable;
855 irq->interrupt_count = 1;
856 irq->interrupts[0] = p->start;
858 dev_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
860 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
861 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
862 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
863 irq->descriptor_length);
866 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
867 struct acpi_resource *resource,
870 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
871 int triggering, polarity, shareable;
873 if (!pnp_resource_enabled(p)) {
874 extended_irq->interrupt_count = 0;
875 dev_dbg(&dev->dev, " encode extended irq (%s)\n",
876 p ? "disabled" : "missing");
880 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
881 extended_irq->producer_consumer = ACPI_CONSUMER;
882 extended_irq->triggering = triggering;
883 extended_irq->polarity = polarity;
884 extended_irq->sharable = shareable;
885 extended_irq->interrupt_count = 1;
886 extended_irq->interrupts[0] = p->start;
888 dev_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
889 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
890 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
891 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
894 static void pnpacpi_encode_dma(struct pnp_dev *dev,
895 struct acpi_resource *resource,
898 struct acpi_resource_dma *dma = &resource->data.dma;
900 if (!pnp_resource_enabled(p)) {
901 dma->channel_count = 0;
902 dev_dbg(&dev->dev, " encode dma (%s)\n",
903 p ? "disabled" : "missing");
907 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
908 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
909 case IORESOURCE_DMA_TYPEA:
910 dma->type = ACPI_TYPE_A;
912 case IORESOURCE_DMA_TYPEB:
913 dma->type = ACPI_TYPE_B;
915 case IORESOURCE_DMA_TYPEF:
916 dma->type = ACPI_TYPE_F;
919 dma->type = ACPI_COMPATIBILITY;
922 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
923 case IORESOURCE_DMA_8BIT:
924 dma->transfer = ACPI_TRANSFER_8;
926 case IORESOURCE_DMA_8AND16BIT:
927 dma->transfer = ACPI_TRANSFER_8_16;
930 dma->transfer = ACPI_TRANSFER_16;
933 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
934 dma->channel_count = 1;
935 dma->channels[0] = p->start;
937 dev_dbg(&dev->dev, " encode dma %d "
938 "type %#x transfer %#x master %d\n",
939 (int) p->start, dma->type, dma->transfer, dma->bus_master);
942 static void pnpacpi_encode_io(struct pnp_dev *dev,
943 struct acpi_resource *resource,
946 struct acpi_resource_io *io = &resource->data.io;
948 if (pnp_resource_enabled(p)) {
949 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
950 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
951 ACPI_DECODE_16 : ACPI_DECODE_10;
952 io->minimum = p->start;
953 io->maximum = p->end;
954 io->alignment = 0; /* Correct? */
955 io->address_length = p->end - p->start + 1;
958 io->address_length = 0;
961 dev_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
962 io->minimum + io->address_length - 1, io->io_decode);
965 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
966 struct acpi_resource *resource,
969 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
971 if (pnp_resource_enabled(p)) {
972 fixed_io->address = p->start;
973 fixed_io->address_length = p->end - p->start + 1;
975 fixed_io->address = 0;
976 fixed_io->address_length = 0;
979 dev_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
980 fixed_io->address + fixed_io->address_length - 1);
983 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
984 struct acpi_resource *resource,
987 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
989 if (pnp_resource_enabled(p)) {
990 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
991 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
992 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
993 memory24->minimum = p->start;
994 memory24->maximum = p->end;
995 memory24->alignment = 0;
996 memory24->address_length = p->end - p->start + 1;
998 memory24->minimum = 0;
999 memory24->address_length = 0;
1002 dev_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
1004 memory24->minimum + memory24->address_length - 1,
1005 memory24->write_protect);
1008 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1009 struct acpi_resource *resource,
1012 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1014 if (pnp_resource_enabled(p)) {
1015 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1016 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1017 memory32->minimum = p->start;
1018 memory32->maximum = p->end;
1019 memory32->alignment = 0;
1020 memory32->address_length = p->end - p->start + 1;
1022 memory32->minimum = 0;
1023 memory32->alignment = 0;
1026 dev_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
1028 memory32->minimum + memory32->address_length - 1,
1029 memory32->write_protect);
1032 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1033 struct acpi_resource *resource,
1036 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1038 if (pnp_resource_enabled(p)) {
1039 fixed_memory32->write_protect =
1040 p->flags & IORESOURCE_MEM_WRITEABLE ?
1041 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1042 fixed_memory32->address = p->start;
1043 fixed_memory32->address_length = p->end - p->start + 1;
1045 fixed_memory32->address = 0;
1046 fixed_memory32->address_length = 0;
1049 dev_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
1050 fixed_memory32->address,
1051 fixed_memory32->address + fixed_memory32->address_length - 1,
1052 fixed_memory32->write_protect);
1055 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1058 /* pnpacpi_build_resource_template allocates extra mem */
1059 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1060 struct acpi_resource *resource = buffer->pointer;
1061 int port = 0, irq = 0, dma = 0, mem = 0;
1063 dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1064 while (i < res_cnt) {
1065 switch (resource->type) {
1066 case ACPI_RESOURCE_TYPE_IRQ:
1067 pnpacpi_encode_irq(dev, resource,
1068 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1072 case ACPI_RESOURCE_TYPE_DMA:
1073 pnpacpi_encode_dma(dev, resource,
1074 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1077 case ACPI_RESOURCE_TYPE_IO:
1078 pnpacpi_encode_io(dev, resource,
1079 pnp_get_resource(dev, IORESOURCE_IO, port));
1082 case ACPI_RESOURCE_TYPE_FIXED_IO:
1083 pnpacpi_encode_fixed_io(dev, resource,
1084 pnp_get_resource(dev, IORESOURCE_IO, port));
1087 case ACPI_RESOURCE_TYPE_MEMORY24:
1088 pnpacpi_encode_mem24(dev, resource,
1089 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1092 case ACPI_RESOURCE_TYPE_MEMORY32:
1093 pnpacpi_encode_mem32(dev, resource,
1094 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1097 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1098 pnpacpi_encode_fixed_mem32(dev, resource,
1099 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1102 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1103 pnpacpi_encode_ext_irq(dev, resource,
1104 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1107 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1108 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1109 case ACPI_RESOURCE_TYPE_VENDOR:
1110 case ACPI_RESOURCE_TYPE_END_TAG:
1111 case ACPI_RESOURCE_TYPE_ADDRESS16:
1112 case ACPI_RESOURCE_TYPE_ADDRESS32:
1113 case ACPI_RESOURCE_TYPE_ADDRESS64:
1114 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1115 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1116 default: /* other type */
1117 dev_warn(&dev->dev, "can't encode unknown resource "
1118 "type %d\n", resource->type);