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>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kernel.h>
22 #include <linux/acpi.h>
23 #include <linux/pci.h>
24 #include <linux/pnp.h>
29 #define valid_IRQ(i) (1)
31 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
37 static int irq_flags(int triggering, int polarity, int shareable)
41 if (triggering == ACPI_LEVEL_SENSITIVE) {
42 if (polarity == ACPI_ACTIVE_LOW)
43 flags = IORESOURCE_IRQ_LOWLEVEL;
45 flags = IORESOURCE_IRQ_HIGHLEVEL;
47 if (polarity == ACPI_ACTIVE_LOW)
48 flags = IORESOURCE_IRQ_LOWEDGE;
50 flags = IORESOURCE_IRQ_HIGHEDGE;
53 if (shareable == ACPI_SHARED)
54 flags |= IORESOURCE_IRQ_SHAREABLE;
59 static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
60 int *polarity, int *shareable)
62 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
63 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
64 case IORESOURCE_IRQ_LOWLEVEL:
65 *triggering = ACPI_LEVEL_SENSITIVE;
66 *polarity = ACPI_ACTIVE_LOW;
68 case IORESOURCE_IRQ_HIGHLEVEL:
69 *triggering = ACPI_LEVEL_SENSITIVE;
70 *polarity = ACPI_ACTIVE_HIGH;
72 case IORESOURCE_IRQ_LOWEDGE:
73 *triggering = ACPI_EDGE_SENSITIVE;
74 *polarity = ACPI_ACTIVE_LOW;
76 case IORESOURCE_IRQ_HIGHEDGE:
77 *triggering = ACPI_EDGE_SENSITIVE;
78 *polarity = ACPI_ACTIVE_HIGH;
81 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
83 *triggering = ACPI_EDGE_SENSITIVE;
84 *polarity = ACPI_ACTIVE_HIGH;
88 if (flags & IORESOURCE_IRQ_SHAREABLE)
89 *shareable = ACPI_SHARED;
91 *shareable = ACPI_EXCLUSIVE;
94 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
95 u32 gsi, int triggering,
96 int polarity, int shareable)
101 if (!valid_IRQ(gsi)) {
102 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
107 * in IO-APIC mode, use overrided attribute. Two reasons:
108 * 1. BIOS bug in DSDT
109 * 2. BIOS uses IO-APIC mode Interrupt Source Override
111 if (!acpi_get_override_irq(gsi, &t, &p)) {
112 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
113 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
115 if (triggering != t || polarity != p) {
116 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
117 gsi, t ? "edge":"level", p ? "low":"high");
123 flags = irq_flags(triggering, polarity, shareable);
124 irq = acpi_register_gsi(gsi, triggering, polarity);
126 pcibios_penalize_isa_irq(irq, 1);
128 flags |= IORESOURCE_DISABLED;
130 pnp_add_irq_resource(dev, irq, flags);
133 static int dma_flags(int type, int bus_master, int transfer)
138 flags |= IORESOURCE_DMA_MASTER;
140 case ACPI_COMPATIBILITY:
141 flags |= IORESOURCE_DMA_COMPATIBLE;
144 flags |= IORESOURCE_DMA_TYPEA;
147 flags |= IORESOURCE_DMA_TYPEB;
150 flags |= IORESOURCE_DMA_TYPEF;
153 /* Set a default value ? */
154 flags |= IORESOURCE_DMA_COMPATIBLE;
155 pnp_err("Invalid DMA type");
158 case ACPI_TRANSFER_8:
159 flags |= IORESOURCE_DMA_8BIT;
161 case ACPI_TRANSFER_8_16:
162 flags |= IORESOURCE_DMA_8AND16BIT;
164 case ACPI_TRANSFER_16:
165 flags |= IORESOURCE_DMA_16BIT;
168 /* Set a default value ? */
169 flags |= IORESOURCE_DMA_8AND16BIT;
170 pnp_err("Invalid DMA transfer type");
176 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
177 u64 len, int io_decode)
180 u64 end = start + len - 1;
182 if (io_decode == ACPI_DECODE_16)
183 flags |= IORESOURCE_IO_16BIT_ADDR;
184 if (len == 0 || end >= 0x10003)
185 flags |= IORESOURCE_DISABLED;
187 pnp_add_io_resource(dev, start, end, flags);
190 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
195 u64 end = start + len - 1;
198 flags |= IORESOURCE_DISABLED;
199 if (write_protect == ACPI_READ_WRITE_MEMORY)
200 flags |= IORESOURCE_MEM_WRITEABLE;
202 pnp_add_mem_resource(dev, start, end, flags);
205 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
206 struct acpi_resource *res)
208 struct acpi_resource_address64 addr, *p = &addr;
211 status = acpi_resource_to_address64(res, p);
212 if (!ACPI_SUCCESS(status)) {
213 dev_warn(&dev->dev, "failed to convert resource type %d\n",
218 if (p->producer_consumer == ACPI_PRODUCER)
221 if (p->resource_type == ACPI_MEMORY_RANGE)
222 pnpacpi_parse_allocated_memresource(dev,
223 p->minimum, p->address_length,
224 p->info.mem.write_protect);
225 else if (p->resource_type == ACPI_IO_RANGE)
226 pnpacpi_parse_allocated_ioresource(dev,
227 p->minimum, p->address_length,
228 p->granularity == 0xfff ? ACPI_DECODE_10 :
232 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
235 struct pnp_dev *dev = data;
236 struct acpi_resource_irq *irq;
237 struct acpi_resource_dma *dma;
238 struct acpi_resource_io *io;
239 struct acpi_resource_fixed_io *fixed_io;
240 struct acpi_resource_memory24 *memory24;
241 struct acpi_resource_memory32 *memory32;
242 struct acpi_resource_fixed_memory32 *fixed_memory32;
243 struct acpi_resource_extended_irq *extended_irq;
247 case ACPI_RESOURCE_TYPE_IRQ:
249 * Per spec, only one interrupt per descriptor is allowed in
250 * _CRS, but some firmware violates this, so parse them all.
252 irq = &res->data.irq;
253 if (irq->interrupt_count == 0)
254 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
256 for (i = 0; i < irq->interrupt_count; i++) {
257 pnpacpi_parse_allocated_irqresource(dev,
265 * The IRQ encoder puts a single interrupt in each
266 * descriptor, so if a _CRS descriptor has more than
267 * one interrupt, we won't be able to re-encode it.
269 if (pnp_can_write(dev) && irq->interrupt_count > 1) {
270 dev_warn(&dev->dev, "multiple interrupts in "
271 "_CRS descriptor; configuration can't "
273 dev->capabilities &= ~PNP_WRITE;
278 case ACPI_RESOURCE_TYPE_DMA:
279 dma = &res->data.dma;
280 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
281 flags = dma_flags(dma->type, dma->bus_master,
284 flags = IORESOURCE_DISABLED;
285 pnp_add_dma_resource(dev, dma->channels[0], flags);
288 case ACPI_RESOURCE_TYPE_IO:
290 pnpacpi_parse_allocated_ioresource(dev,
296 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
297 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
300 case ACPI_RESOURCE_TYPE_FIXED_IO:
301 fixed_io = &res->data.fixed_io;
302 pnpacpi_parse_allocated_ioresource(dev,
304 fixed_io->address_length,
308 case ACPI_RESOURCE_TYPE_VENDOR:
311 case ACPI_RESOURCE_TYPE_END_TAG:
314 case ACPI_RESOURCE_TYPE_MEMORY24:
315 memory24 = &res->data.memory24;
316 pnpacpi_parse_allocated_memresource(dev,
318 memory24->address_length,
319 memory24->write_protect);
321 case ACPI_RESOURCE_TYPE_MEMORY32:
322 memory32 = &res->data.memory32;
323 pnpacpi_parse_allocated_memresource(dev,
325 memory32->address_length,
326 memory32->write_protect);
328 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
329 fixed_memory32 = &res->data.fixed_memory32;
330 pnpacpi_parse_allocated_memresource(dev,
331 fixed_memory32->address,
332 fixed_memory32->address_length,
333 fixed_memory32->write_protect);
335 case ACPI_RESOURCE_TYPE_ADDRESS16:
336 case ACPI_RESOURCE_TYPE_ADDRESS32:
337 case ACPI_RESOURCE_TYPE_ADDRESS64:
338 pnpacpi_parse_allocated_address_space(dev, res);
341 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
342 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
346 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
347 extended_irq = &res->data.extended_irq;
348 if (extended_irq->producer_consumer == ACPI_PRODUCER)
351 if (extended_irq->interrupt_count == 0)
352 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
354 for (i = 0; i < extended_irq->interrupt_count; i++) {
355 pnpacpi_parse_allocated_irqresource(dev,
356 extended_irq->interrupts[i],
357 extended_irq->triggering,
358 extended_irq->polarity,
359 extended_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) &&
368 extended_irq->interrupt_count > 1) {
369 dev_warn(&dev->dev, "multiple interrupts in "
370 "_CRS descriptor; configuration can't "
372 dev->capabilities &= ~PNP_WRITE;
377 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
381 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
389 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
391 acpi_handle handle = dev->data;
394 dev_dbg(&dev->dev, "parse allocated resources\n");
396 pnp_init_resources(dev);
398 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
399 pnpacpi_allocated_resource, dev);
401 if (ACPI_FAILURE(status)) {
402 if (status != AE_NOT_FOUND)
403 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
409 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
410 struct pnp_option *option,
411 struct acpi_resource_dma *p)
416 if (p->channel_count == 0)
418 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
422 for (i = 0; i < p->channel_count; i++)
423 dma->map |= 1 << p->channels[i];
425 dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
427 pnp_register_dma_resource(dev, option, dma);
430 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
431 struct pnp_option *option,
432 struct acpi_resource_irq *p)
437 if (p->interrupt_count == 0)
439 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
443 for (i = 0; i < p->interrupt_count; i++)
444 if (p->interrupts[i])
445 __set_bit(p->interrupts[i], irq->map.bits);
446 irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
448 pnp_register_irq_resource(dev, option, irq);
451 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
452 struct pnp_option *option,
453 struct acpi_resource_extended_irq *p)
458 if (p->interrupt_count == 0)
460 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
464 for (i = 0; i < p->interrupt_count; i++)
465 if (p->interrupts[i])
466 __set_bit(p->interrupts[i], irq->map.bits);
467 irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
469 pnp_register_irq_resource(dev, option, irq);
472 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
473 struct pnp_option *option,
474 struct acpi_resource_io *io)
476 struct pnp_port *port;
478 if (io->address_length == 0)
480 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
483 port->min = io->minimum;
484 port->max = io->maximum;
485 port->align = io->alignment;
486 port->size = io->address_length;
487 port->flags = ACPI_DECODE_16 == io->io_decode ?
488 IORESOURCE_IO_16BIT_ADDR : 0;
489 pnp_register_port_resource(dev, option, port);
492 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
493 struct pnp_option *option,
494 struct acpi_resource_fixed_io *io)
496 struct pnp_port *port;
498 if (io->address_length == 0)
500 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
503 port->min = port->max = io->address;
504 port->size = io->address_length;
506 port->flags = IORESOURCE_IO_FIXED;
507 pnp_register_port_resource(dev, option, port);
510 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
511 struct pnp_option *option,
512 struct acpi_resource_memory24 *p)
516 if (p->address_length == 0)
518 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
521 mem->min = p->minimum;
522 mem->max = p->maximum;
523 mem->align = p->alignment;
524 mem->size = p->address_length;
526 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
527 IORESOURCE_MEM_WRITEABLE : 0;
529 pnp_register_mem_resource(dev, option, mem);
532 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
533 struct pnp_option *option,
534 struct acpi_resource_memory32 *p)
538 if (p->address_length == 0)
540 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
543 mem->min = p->minimum;
544 mem->max = p->maximum;
545 mem->align = p->alignment;
546 mem->size = p->address_length;
548 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
549 IORESOURCE_MEM_WRITEABLE : 0;
551 pnp_register_mem_resource(dev, option, mem);
554 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
555 struct pnp_option *option,
556 struct acpi_resource_fixed_memory32 *p)
560 if (p->address_length == 0)
562 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
565 mem->min = mem->max = p->address;
566 mem->size = p->address_length;
569 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
570 IORESOURCE_MEM_WRITEABLE : 0;
572 pnp_register_mem_resource(dev, option, mem);
575 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
576 struct pnp_option *option,
577 struct acpi_resource *r)
579 struct acpi_resource_address64 addr, *p = &addr;
582 struct pnp_port *port;
584 status = acpi_resource_to_address64(r, p);
585 if (!ACPI_SUCCESS(status)) {
586 pnp_warn("PnPACPI: failed to convert resource type %d",
591 if (p->address_length == 0)
594 if (p->resource_type == ACPI_MEMORY_RANGE) {
595 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
598 mem->min = mem->max = p->minimum;
599 mem->size = p->address_length;
601 mem->flags = (p->info.mem.write_protect ==
602 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE
604 pnp_register_mem_resource(dev, option, mem);
605 } else if (p->resource_type == ACPI_IO_RANGE) {
606 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
609 port->min = port->max = p->minimum;
610 port->size = p->address_length;
612 port->flags = IORESOURCE_IO_FIXED;
613 pnp_register_port_resource(dev, option, port);
617 struct acpipnp_parse_option_s {
618 struct pnp_option *option;
619 struct pnp_option *option_independent;
623 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
627 struct acpipnp_parse_option_s *parse_data = data;
628 struct pnp_dev *dev = parse_data->dev;
629 struct pnp_option *option = parse_data->option;
632 case ACPI_RESOURCE_TYPE_IRQ:
633 pnpacpi_parse_irq_option(dev, option, &res->data.irq);
636 case ACPI_RESOURCE_TYPE_DMA:
637 pnpacpi_parse_dma_option(dev, option, &res->data.dma);
640 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
641 switch (res->data.start_dpf.compatibility_priority) {
642 case ACPI_GOOD_CONFIGURATION:
643 priority = PNP_RES_PRIORITY_PREFERRED;
646 case ACPI_ACCEPTABLE_CONFIGURATION:
647 priority = PNP_RES_PRIORITY_ACCEPTABLE;
650 case ACPI_SUB_OPTIMAL_CONFIGURATION:
651 priority = PNP_RES_PRIORITY_FUNCTIONAL;
654 priority = PNP_RES_PRIORITY_INVALID;
657 /* TBD: Consider performance/robustness bits */
658 option = pnp_register_dependent_option(dev, priority);
661 parse_data->option = option;
664 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
665 /*only one EndDependentFn is allowed */
666 if (!parse_data->option_independent) {
667 dev_warn(&dev->dev, "more than one EndDependentFn "
671 parse_data->option = parse_data->option_independent;
672 parse_data->option_independent = NULL;
673 dev_dbg(&dev->dev, "end dependent options\n");
676 case ACPI_RESOURCE_TYPE_IO:
677 pnpacpi_parse_port_option(dev, option, &res->data.io);
680 case ACPI_RESOURCE_TYPE_FIXED_IO:
681 pnpacpi_parse_fixed_port_option(dev, option,
682 &res->data.fixed_io);
685 case ACPI_RESOURCE_TYPE_VENDOR:
686 case ACPI_RESOURCE_TYPE_END_TAG:
689 case ACPI_RESOURCE_TYPE_MEMORY24:
690 pnpacpi_parse_mem24_option(dev, option, &res->data.memory24);
693 case ACPI_RESOURCE_TYPE_MEMORY32:
694 pnpacpi_parse_mem32_option(dev, option, &res->data.memory32);
697 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
698 pnpacpi_parse_fixed_mem32_option(dev, option,
699 &res->data.fixed_memory32);
702 case ACPI_RESOURCE_TYPE_ADDRESS16:
703 case ACPI_RESOURCE_TYPE_ADDRESS32:
704 case ACPI_RESOURCE_TYPE_ADDRESS64:
705 pnpacpi_parse_address_option(dev, option, res);
708 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
711 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
712 pnpacpi_parse_ext_irq_option(dev, option,
713 &res->data.extended_irq);
716 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
720 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
728 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
730 acpi_handle handle = dev->data;
732 struct acpipnp_parse_option_s parse_data;
734 dev_dbg(&dev->dev, "parse resource options\n");
736 parse_data.option = pnp_register_independent_option(dev);
737 if (!parse_data.option)
740 parse_data.option_independent = parse_data.option;
741 parse_data.dev = dev;
742 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
743 pnpacpi_option_resource, &parse_data);
745 if (ACPI_FAILURE(status)) {
746 if (status != AE_NOT_FOUND)
747 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
753 static int pnpacpi_supported_resource(struct acpi_resource *res)
756 case ACPI_RESOURCE_TYPE_IRQ:
757 case ACPI_RESOURCE_TYPE_DMA:
758 case ACPI_RESOURCE_TYPE_IO:
759 case ACPI_RESOURCE_TYPE_FIXED_IO:
760 case ACPI_RESOURCE_TYPE_MEMORY24:
761 case ACPI_RESOURCE_TYPE_MEMORY32:
762 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
763 case ACPI_RESOURCE_TYPE_ADDRESS16:
764 case ACPI_RESOURCE_TYPE_ADDRESS32:
765 case ACPI_RESOURCE_TYPE_ADDRESS64:
766 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
775 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
780 if (pnpacpi_supported_resource(res))
785 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
787 struct acpi_resource **resource = data;
789 if (pnpacpi_supported_resource(res)) {
790 (*resource)->type = res->type;
791 (*resource)->length = sizeof(struct acpi_resource);
792 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
793 (*resource)->data.irq.descriptor_length =
794 res->data.irq.descriptor_length;
801 int pnpacpi_build_resource_template(struct pnp_dev *dev,
802 struct acpi_buffer *buffer)
804 acpi_handle handle = dev->data;
805 struct acpi_resource *resource;
809 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
810 pnpacpi_count_resources, &res_cnt);
811 if (ACPI_FAILURE(status)) {
812 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
817 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
818 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
819 if (!buffer->pointer)
822 resource = (struct acpi_resource *)buffer->pointer;
823 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
824 pnpacpi_type_resources, &resource);
825 if (ACPI_FAILURE(status)) {
826 kfree(buffer->pointer);
827 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
830 /* resource will pointer the end resource now */
831 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
836 static void pnpacpi_encode_irq(struct pnp_dev *dev,
837 struct acpi_resource *resource,
840 struct acpi_resource_irq *irq = &resource->data.irq;
841 int triggering, polarity, shareable;
843 if (!pnp_resource_enabled(p)) {
844 irq->interrupt_count = 0;
845 dev_dbg(&dev->dev, " encode irq (%s)\n",
846 p ? "disabled" : "missing");
850 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
851 irq->triggering = triggering;
852 irq->polarity = polarity;
853 irq->sharable = shareable;
854 irq->interrupt_count = 1;
855 irq->interrupts[0] = p->start;
857 dev_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
859 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
860 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
861 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
862 irq->descriptor_length);
865 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
866 struct acpi_resource *resource,
869 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
870 int triggering, polarity, shareable;
872 if (!pnp_resource_enabled(p)) {
873 extended_irq->interrupt_count = 0;
874 dev_dbg(&dev->dev, " encode extended irq (%s)\n",
875 p ? "disabled" : "missing");
879 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
880 extended_irq->producer_consumer = ACPI_CONSUMER;
881 extended_irq->triggering = triggering;
882 extended_irq->polarity = polarity;
883 extended_irq->sharable = shareable;
884 extended_irq->interrupt_count = 1;
885 extended_irq->interrupts[0] = p->start;
887 dev_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
888 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
889 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
890 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
893 static void pnpacpi_encode_dma(struct pnp_dev *dev,
894 struct acpi_resource *resource,
897 struct acpi_resource_dma *dma = &resource->data.dma;
899 if (!pnp_resource_enabled(p)) {
900 dma->channel_count = 0;
901 dev_dbg(&dev->dev, " encode dma (%s)\n",
902 p ? "disabled" : "missing");
906 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
907 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
908 case IORESOURCE_DMA_TYPEA:
909 dma->type = ACPI_TYPE_A;
911 case IORESOURCE_DMA_TYPEB:
912 dma->type = ACPI_TYPE_B;
914 case IORESOURCE_DMA_TYPEF:
915 dma->type = ACPI_TYPE_F;
918 dma->type = ACPI_COMPATIBILITY;
921 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
922 case IORESOURCE_DMA_8BIT:
923 dma->transfer = ACPI_TRANSFER_8;
925 case IORESOURCE_DMA_8AND16BIT:
926 dma->transfer = ACPI_TRANSFER_8_16;
929 dma->transfer = ACPI_TRANSFER_16;
932 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
933 dma->channel_count = 1;
934 dma->channels[0] = p->start;
936 dev_dbg(&dev->dev, " encode dma %d "
937 "type %#x transfer %#x master %d\n",
938 (int) p->start, dma->type, dma->transfer, dma->bus_master);
941 static void pnpacpi_encode_io(struct pnp_dev *dev,
942 struct acpi_resource *resource,
945 struct acpi_resource_io *io = &resource->data.io;
947 if (pnp_resource_enabled(p)) {
948 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
949 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
950 ACPI_DECODE_16 : ACPI_DECODE_10;
951 io->minimum = p->start;
952 io->maximum = p->end;
953 io->alignment = 0; /* Correct? */
954 io->address_length = p->end - p->start + 1;
957 io->address_length = 0;
960 dev_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
961 io->minimum + io->address_length - 1, io->io_decode);
964 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
965 struct acpi_resource *resource,
968 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
970 if (pnp_resource_enabled(p)) {
971 fixed_io->address = p->start;
972 fixed_io->address_length = p->end - p->start + 1;
974 fixed_io->address = 0;
975 fixed_io->address_length = 0;
978 dev_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
979 fixed_io->address + fixed_io->address_length - 1);
982 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
983 struct acpi_resource *resource,
986 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
988 if (pnp_resource_enabled(p)) {
989 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
990 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
991 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
992 memory24->minimum = p->start;
993 memory24->maximum = p->end;
994 memory24->alignment = 0;
995 memory24->address_length = p->end - p->start + 1;
997 memory24->minimum = 0;
998 memory24->address_length = 0;
1001 dev_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
1003 memory24->minimum + memory24->address_length - 1,
1004 memory24->write_protect);
1007 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1008 struct acpi_resource *resource,
1011 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1013 if (pnp_resource_enabled(p)) {
1014 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1015 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1016 memory32->minimum = p->start;
1017 memory32->maximum = p->end;
1018 memory32->alignment = 0;
1019 memory32->address_length = p->end - p->start + 1;
1021 memory32->minimum = 0;
1022 memory32->alignment = 0;
1025 dev_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
1027 memory32->minimum + memory32->address_length - 1,
1028 memory32->write_protect);
1031 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1032 struct acpi_resource *resource,
1035 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1037 if (pnp_resource_enabled(p)) {
1038 fixed_memory32->write_protect =
1039 p->flags & IORESOURCE_MEM_WRITEABLE ?
1040 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1041 fixed_memory32->address = p->start;
1042 fixed_memory32->address_length = p->end - p->start + 1;
1044 fixed_memory32->address = 0;
1045 fixed_memory32->address_length = 0;
1048 dev_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
1049 fixed_memory32->address,
1050 fixed_memory32->address + fixed_memory32->address_length - 1,
1051 fixed_memory32->write_protect);
1054 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1057 /* pnpacpi_build_resource_template allocates extra mem */
1058 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1059 struct acpi_resource *resource = buffer->pointer;
1060 int port = 0, irq = 0, dma = 0, mem = 0;
1062 dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1063 while (i < res_cnt) {
1064 switch (resource->type) {
1065 case ACPI_RESOURCE_TYPE_IRQ:
1066 pnpacpi_encode_irq(dev, resource,
1067 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1071 case ACPI_RESOURCE_TYPE_DMA:
1072 pnpacpi_encode_dma(dev, resource,
1073 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1076 case ACPI_RESOURCE_TYPE_IO:
1077 pnpacpi_encode_io(dev, resource,
1078 pnp_get_resource(dev, IORESOURCE_IO, port));
1081 case ACPI_RESOURCE_TYPE_FIXED_IO:
1082 pnpacpi_encode_fixed_io(dev, resource,
1083 pnp_get_resource(dev, IORESOURCE_IO, port));
1086 case ACPI_RESOURCE_TYPE_MEMORY24:
1087 pnpacpi_encode_mem24(dev, resource,
1088 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1091 case ACPI_RESOURCE_TYPE_MEMORY32:
1092 pnpacpi_encode_mem32(dev, resource,
1093 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1096 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1097 pnpacpi_encode_fixed_mem32(dev, resource,
1098 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1101 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1102 pnpacpi_encode_ext_irq(dev, resource,
1103 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1106 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1107 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1108 case ACPI_RESOURCE_TYPE_VENDOR:
1109 case ACPI_RESOURCE_TYPE_END_TAG:
1110 case ACPI_RESOURCE_TYPE_ADDRESS16:
1111 case ACPI_RESOURCE_TYPE_ADDRESS32:
1112 case ACPI_RESOURCE_TYPE_ADDRESS64:
1113 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1114 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1115 default: /* other type */
1116 dev_warn(&dev->dev, "can't encode unknown resource "
1117 "type %d\n", resource->type);