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;
54 flags |= IORESOURCE_IRQ_SHAREABLE;
59 static void decode_irq_flags(int flag, int *triggering, int *polarity)
62 case IORESOURCE_IRQ_LOWLEVEL:
63 *triggering = ACPI_LEVEL_SENSITIVE;
64 *polarity = ACPI_ACTIVE_LOW;
66 case IORESOURCE_IRQ_HIGHLEVEL:
67 *triggering = ACPI_LEVEL_SENSITIVE;
68 *polarity = ACPI_ACTIVE_HIGH;
70 case IORESOURCE_IRQ_LOWEDGE:
71 *triggering = ACPI_EDGE_SENSITIVE;
72 *polarity = ACPI_ACTIVE_LOW;
74 case IORESOURCE_IRQ_HIGHEDGE:
75 *triggering = ACPI_EDGE_SENSITIVE;
76 *polarity = ACPI_ACTIVE_HIGH;
81 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
82 u32 gsi, int triggering,
83 int polarity, int shareable)
92 * in IO-APIC mode, use overrided attribute. Two reasons:
94 * 2. BIOS uses IO-APIC mode Interrupt Source Override
96 if (!acpi_get_override_irq(gsi, &t, &p)) {
97 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
98 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
100 if (triggering != t || polarity != p) {
101 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
102 gsi, t ? "edge":"level", p ? "low":"high");
108 flags = irq_flags(triggering, polarity, shareable);
109 irq = acpi_register_gsi(gsi, triggering, polarity);
111 pcibios_penalize_isa_irq(irq, 1);
113 flags |= IORESOURCE_DISABLED;
115 pnp_add_irq_resource(dev, irq, flags);
118 static int dma_flags(int type, int bus_master, int transfer)
123 flags |= IORESOURCE_DMA_MASTER;
125 case ACPI_COMPATIBILITY:
126 flags |= IORESOURCE_DMA_COMPATIBLE;
129 flags |= IORESOURCE_DMA_TYPEA;
132 flags |= IORESOURCE_DMA_TYPEB;
135 flags |= IORESOURCE_DMA_TYPEF;
138 /* Set a default value ? */
139 flags |= IORESOURCE_DMA_COMPATIBLE;
140 pnp_err("Invalid DMA type");
143 case ACPI_TRANSFER_8:
144 flags |= IORESOURCE_DMA_8BIT;
146 case ACPI_TRANSFER_8_16:
147 flags |= IORESOURCE_DMA_8AND16BIT;
149 case ACPI_TRANSFER_16:
150 flags |= IORESOURCE_DMA_16BIT;
153 /* Set a default value ? */
154 flags |= IORESOURCE_DMA_8AND16BIT;
155 pnp_err("Invalid DMA transfer type");
161 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
162 u64 len, int io_decode)
165 u64 end = start + len - 1;
167 if (io_decode == ACPI_DECODE_16)
168 flags |= PNP_PORT_FLAG_16BITADDR;
169 if (len == 0 || end >= 0x10003)
170 flags |= IORESOURCE_DISABLED;
172 pnp_add_io_resource(dev, start, end, flags);
175 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
180 u64 end = start + len - 1;
183 flags |= IORESOURCE_DISABLED;
184 if (write_protect == ACPI_READ_WRITE_MEMORY)
185 flags |= IORESOURCE_MEM_WRITEABLE;
187 pnp_add_mem_resource(dev, start, end, flags);
190 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
191 struct acpi_resource *res)
193 struct acpi_resource_address64 addr, *p = &addr;
196 status = acpi_resource_to_address64(res, p);
197 if (!ACPI_SUCCESS(status)) {
198 dev_warn(&dev->dev, "failed to convert resource type %d\n",
203 if (p->producer_consumer == ACPI_PRODUCER)
206 if (p->resource_type == ACPI_MEMORY_RANGE)
207 pnpacpi_parse_allocated_memresource(dev,
208 p->minimum, p->address_length,
209 p->info.mem.write_protect);
210 else if (p->resource_type == ACPI_IO_RANGE)
211 pnpacpi_parse_allocated_ioresource(dev,
212 p->minimum, p->address_length,
213 p->granularity == 0xfff ? ACPI_DECODE_10 :
217 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
220 struct pnp_dev *dev = data;
221 struct acpi_resource_irq *irq;
222 struct acpi_resource_dma *dma;
223 struct acpi_resource_io *io;
224 struct acpi_resource_fixed_io *fixed_io;
225 struct acpi_resource_memory24 *memory24;
226 struct acpi_resource_memory32 *memory32;
227 struct acpi_resource_fixed_memory32 *fixed_memory32;
228 struct acpi_resource_extended_irq *extended_irq;
232 case ACPI_RESOURCE_TYPE_IRQ:
234 * Per spec, only one interrupt per descriptor is allowed in
235 * _CRS, but some firmware violates this, so parse them all.
237 irq = &res->data.irq;
238 for (i = 0; i < irq->interrupt_count; i++) {
239 pnpacpi_parse_allocated_irqresource(dev,
247 case ACPI_RESOURCE_TYPE_DMA:
248 dma = &res->data.dma;
249 if (dma->channel_count > 0) {
250 flags = dma_flags(dma->type, dma->bus_master,
252 if (dma->channels[0] == (u8) -1)
253 flags |= IORESOURCE_DISABLED;
254 pnp_add_dma_resource(dev, dma->channels[0], flags);
258 case ACPI_RESOURCE_TYPE_IO:
260 pnpacpi_parse_allocated_ioresource(dev,
266 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
267 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
270 case ACPI_RESOURCE_TYPE_FIXED_IO:
271 fixed_io = &res->data.fixed_io;
272 pnpacpi_parse_allocated_ioresource(dev,
274 fixed_io->address_length,
278 case ACPI_RESOURCE_TYPE_VENDOR:
281 case ACPI_RESOURCE_TYPE_END_TAG:
284 case ACPI_RESOURCE_TYPE_MEMORY24:
285 memory24 = &res->data.memory24;
286 pnpacpi_parse_allocated_memresource(dev,
288 memory24->address_length,
289 memory24->write_protect);
291 case ACPI_RESOURCE_TYPE_MEMORY32:
292 memory32 = &res->data.memory32;
293 pnpacpi_parse_allocated_memresource(dev,
295 memory32->address_length,
296 memory32->write_protect);
298 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
299 fixed_memory32 = &res->data.fixed_memory32;
300 pnpacpi_parse_allocated_memresource(dev,
301 fixed_memory32->address,
302 fixed_memory32->address_length,
303 fixed_memory32->write_protect);
305 case ACPI_RESOURCE_TYPE_ADDRESS16:
306 case ACPI_RESOURCE_TYPE_ADDRESS32:
307 case ACPI_RESOURCE_TYPE_ADDRESS64:
308 pnpacpi_parse_allocated_address_space(dev, res);
311 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
312 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
316 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
317 extended_irq = &res->data.extended_irq;
318 if (extended_irq->producer_consumer == ACPI_PRODUCER)
321 for (i = 0; i < extended_irq->interrupt_count; i++) {
322 pnpacpi_parse_allocated_irqresource(dev,
323 extended_irq->interrupts[i],
324 extended_irq->triggering,
325 extended_irq->polarity,
326 extended_irq->sharable);
330 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
334 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
342 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
344 acpi_handle handle = dev->data;
347 dev_dbg(&dev->dev, "parse allocated resources\n");
349 pnp_init_resources(dev);
351 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
352 pnpacpi_allocated_resource, dev);
354 if (ACPI_FAILURE(status)) {
355 if (status != AE_NOT_FOUND)
356 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
362 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
363 struct pnp_option *option,
364 struct acpi_resource_dma *p)
369 if (p->channel_count == 0)
371 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
375 for (i = 0; i < p->channel_count; i++)
376 dma->map |= 1 << p->channels[i];
378 dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
380 pnp_register_dma_resource(dev, option, dma);
383 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
384 struct pnp_option *option,
385 struct acpi_resource_irq *p)
390 if (p->interrupt_count == 0)
392 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
396 for (i = 0; i < p->interrupt_count; i++)
397 if (p->interrupts[i])
398 __set_bit(p->interrupts[i], irq->map);
399 irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
401 pnp_register_irq_resource(dev, option, irq);
404 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
405 struct pnp_option *option,
406 struct acpi_resource_extended_irq *p)
411 if (p->interrupt_count == 0)
413 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
417 for (i = 0; i < p->interrupt_count; i++)
418 if (p->interrupts[i])
419 __set_bit(p->interrupts[i], irq->map);
420 irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
422 pnp_register_irq_resource(dev, option, irq);
425 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
426 struct pnp_option *option,
427 struct acpi_resource_io *io)
429 struct pnp_port *port;
431 if (io->address_length == 0)
433 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
436 port->min = io->minimum;
437 port->max = io->maximum;
438 port->align = io->alignment;
439 port->size = io->address_length;
440 port->flags = ACPI_DECODE_16 == io->io_decode ?
441 PNP_PORT_FLAG_16BITADDR : 0;
442 pnp_register_port_resource(dev, option, port);
445 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
446 struct pnp_option *option,
447 struct acpi_resource_fixed_io *io)
449 struct pnp_port *port;
451 if (io->address_length == 0)
453 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
456 port->min = port->max = io->address;
457 port->size = io->address_length;
459 port->flags = PNP_PORT_FLAG_FIXED;
460 pnp_register_port_resource(dev, option, port);
463 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
464 struct pnp_option *option,
465 struct acpi_resource_memory24 *p)
469 if (p->address_length == 0)
471 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
474 mem->min = p->minimum;
475 mem->max = p->maximum;
476 mem->align = p->alignment;
477 mem->size = p->address_length;
479 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
480 IORESOURCE_MEM_WRITEABLE : 0;
482 pnp_register_mem_resource(dev, option, mem);
485 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
486 struct pnp_option *option,
487 struct acpi_resource_memory32 *p)
491 if (p->address_length == 0)
493 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
496 mem->min = p->minimum;
497 mem->max = p->maximum;
498 mem->align = p->alignment;
499 mem->size = p->address_length;
501 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
502 IORESOURCE_MEM_WRITEABLE : 0;
504 pnp_register_mem_resource(dev, option, mem);
507 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
508 struct pnp_option *option,
509 struct acpi_resource_fixed_memory32 *p)
513 if (p->address_length == 0)
515 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
518 mem->min = mem->max = p->address;
519 mem->size = p->address_length;
522 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
523 IORESOURCE_MEM_WRITEABLE : 0;
525 pnp_register_mem_resource(dev, option, mem);
528 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
529 struct pnp_option *option,
530 struct acpi_resource *r)
532 struct acpi_resource_address64 addr, *p = &addr;
535 struct pnp_port *port;
537 status = acpi_resource_to_address64(r, p);
538 if (!ACPI_SUCCESS(status)) {
539 pnp_warn("PnPACPI: failed to convert resource type %d",
544 if (p->address_length == 0)
547 if (p->resource_type == ACPI_MEMORY_RANGE) {
548 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
551 mem->min = mem->max = p->minimum;
552 mem->size = p->address_length;
554 mem->flags = (p->info.mem.write_protect ==
555 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE
557 pnp_register_mem_resource(dev, option, mem);
558 } else if (p->resource_type == ACPI_IO_RANGE) {
559 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
562 port->min = port->max = p->minimum;
563 port->size = p->address_length;
565 port->flags = PNP_PORT_FLAG_FIXED;
566 pnp_register_port_resource(dev, option, port);
570 struct acpipnp_parse_option_s {
571 struct pnp_option *option;
572 struct pnp_option *option_independent;
576 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
580 struct acpipnp_parse_option_s *parse_data = data;
581 struct pnp_dev *dev = parse_data->dev;
582 struct pnp_option *option = parse_data->option;
585 case ACPI_RESOURCE_TYPE_IRQ:
586 pnpacpi_parse_irq_option(dev, option, &res->data.irq);
589 case ACPI_RESOURCE_TYPE_DMA:
590 pnpacpi_parse_dma_option(dev, option, &res->data.dma);
593 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
594 switch (res->data.start_dpf.compatibility_priority) {
595 case ACPI_GOOD_CONFIGURATION:
596 priority = PNP_RES_PRIORITY_PREFERRED;
599 case ACPI_ACCEPTABLE_CONFIGURATION:
600 priority = PNP_RES_PRIORITY_ACCEPTABLE;
603 case ACPI_SUB_OPTIMAL_CONFIGURATION:
604 priority = PNP_RES_PRIORITY_FUNCTIONAL;
607 priority = PNP_RES_PRIORITY_INVALID;
610 /* TBD: Consider performance/robustness bits */
611 option = pnp_register_dependent_option(dev, priority);
614 parse_data->option = option;
617 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
618 /*only one EndDependentFn is allowed */
619 if (!parse_data->option_independent) {
620 dev_warn(&dev->dev, "more than one EndDependentFn "
624 parse_data->option = parse_data->option_independent;
625 parse_data->option_independent = NULL;
626 dev_dbg(&dev->dev, "end dependent options\n");
629 case ACPI_RESOURCE_TYPE_IO:
630 pnpacpi_parse_port_option(dev, option, &res->data.io);
633 case ACPI_RESOURCE_TYPE_FIXED_IO:
634 pnpacpi_parse_fixed_port_option(dev, option,
635 &res->data.fixed_io);
638 case ACPI_RESOURCE_TYPE_VENDOR:
639 case ACPI_RESOURCE_TYPE_END_TAG:
642 case ACPI_RESOURCE_TYPE_MEMORY24:
643 pnpacpi_parse_mem24_option(dev, option, &res->data.memory24);
646 case ACPI_RESOURCE_TYPE_MEMORY32:
647 pnpacpi_parse_mem32_option(dev, option, &res->data.memory32);
650 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
651 pnpacpi_parse_fixed_mem32_option(dev, option,
652 &res->data.fixed_memory32);
655 case ACPI_RESOURCE_TYPE_ADDRESS16:
656 case ACPI_RESOURCE_TYPE_ADDRESS32:
657 case ACPI_RESOURCE_TYPE_ADDRESS64:
658 pnpacpi_parse_address_option(dev, option, res);
661 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
664 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
665 pnpacpi_parse_ext_irq_option(dev, option,
666 &res->data.extended_irq);
669 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
673 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
681 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
683 acpi_handle handle = dev->data;
685 struct acpipnp_parse_option_s parse_data;
687 dev_dbg(&dev->dev, "parse resource options\n");
689 parse_data.option = pnp_register_independent_option(dev);
690 if (!parse_data.option)
693 parse_data.option_independent = parse_data.option;
694 parse_data.dev = dev;
695 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
696 pnpacpi_option_resource, &parse_data);
698 if (ACPI_FAILURE(status)) {
699 if (status != AE_NOT_FOUND)
700 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
706 static int pnpacpi_supported_resource(struct acpi_resource *res)
709 case ACPI_RESOURCE_TYPE_IRQ:
710 case ACPI_RESOURCE_TYPE_DMA:
711 case ACPI_RESOURCE_TYPE_IO:
712 case ACPI_RESOURCE_TYPE_FIXED_IO:
713 case ACPI_RESOURCE_TYPE_MEMORY24:
714 case ACPI_RESOURCE_TYPE_MEMORY32:
715 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
716 case ACPI_RESOURCE_TYPE_ADDRESS16:
717 case ACPI_RESOURCE_TYPE_ADDRESS32:
718 case ACPI_RESOURCE_TYPE_ADDRESS64:
719 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
728 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
733 if (pnpacpi_supported_resource(res))
738 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
740 struct acpi_resource **resource = data;
742 if (pnpacpi_supported_resource(res)) {
743 (*resource)->type = res->type;
744 (*resource)->length = sizeof(struct acpi_resource);
751 int pnpacpi_build_resource_template(struct pnp_dev *dev,
752 struct acpi_buffer *buffer)
754 acpi_handle handle = dev->data;
755 struct acpi_resource *resource;
759 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
760 pnpacpi_count_resources, &res_cnt);
761 if (ACPI_FAILURE(status)) {
762 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
767 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
768 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
769 if (!buffer->pointer)
772 resource = (struct acpi_resource *)buffer->pointer;
773 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
774 pnpacpi_type_resources, &resource);
775 if (ACPI_FAILURE(status)) {
776 kfree(buffer->pointer);
777 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
780 /* resource will pointer the end resource now */
781 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
786 static void pnpacpi_encode_irq(struct pnp_dev *dev,
787 struct acpi_resource *resource,
790 struct acpi_resource_irq *irq = &resource->data.irq;
791 int triggering, polarity;
793 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
794 irq->triggering = triggering;
795 irq->polarity = polarity;
796 if (triggering == ACPI_EDGE_SENSITIVE)
797 irq->sharable = ACPI_EXCLUSIVE;
799 irq->sharable = ACPI_SHARED;
800 irq->interrupt_count = 1;
801 irq->interrupts[0] = p->start;
803 dev_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
804 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
805 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
806 irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
809 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
810 struct acpi_resource *resource,
813 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
814 int triggering, polarity;
816 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
817 extended_irq->producer_consumer = ACPI_CONSUMER;
818 extended_irq->triggering = triggering;
819 extended_irq->polarity = polarity;
820 if (triggering == ACPI_EDGE_SENSITIVE)
821 extended_irq->sharable = ACPI_EXCLUSIVE;
823 extended_irq->sharable = ACPI_SHARED;
824 extended_irq->interrupt_count = 1;
825 extended_irq->interrupts[0] = p->start;
827 dev_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
828 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
829 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
830 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
833 static void pnpacpi_encode_dma(struct pnp_dev *dev,
834 struct acpi_resource *resource,
837 struct acpi_resource_dma *dma = &resource->data.dma;
839 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
840 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
841 case IORESOURCE_DMA_TYPEA:
842 dma->type = ACPI_TYPE_A;
844 case IORESOURCE_DMA_TYPEB:
845 dma->type = ACPI_TYPE_B;
847 case IORESOURCE_DMA_TYPEF:
848 dma->type = ACPI_TYPE_F;
851 dma->type = ACPI_COMPATIBILITY;
854 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
855 case IORESOURCE_DMA_8BIT:
856 dma->transfer = ACPI_TRANSFER_8;
858 case IORESOURCE_DMA_8AND16BIT:
859 dma->transfer = ACPI_TRANSFER_8_16;
862 dma->transfer = ACPI_TRANSFER_16;
865 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
866 dma->channel_count = 1;
867 dma->channels[0] = p->start;
869 dev_dbg(&dev->dev, " encode dma %d "
870 "type %#x transfer %#x master %d\n",
871 (int) p->start, dma->type, dma->transfer, dma->bus_master);
874 static void pnpacpi_encode_io(struct pnp_dev *dev,
875 struct acpi_resource *resource,
878 struct acpi_resource_io *io = &resource->data.io;
880 /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
881 io->io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR) ?
882 ACPI_DECODE_16 : ACPI_DECODE_10;
883 io->minimum = p->start;
884 io->maximum = p->end;
885 io->alignment = 0; /* Correct? */
886 io->address_length = p->end - p->start + 1;
888 dev_dbg(&dev->dev, " encode io %#llx-%#llx decode %#x\n",
889 (unsigned long long) p->start, (unsigned long long) p->end,
893 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
894 struct acpi_resource *resource,
897 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
899 fixed_io->address = p->start;
900 fixed_io->address_length = p->end - p->start + 1;
902 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n",
903 (unsigned long long) p->start, (unsigned long long) p->end);
906 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
907 struct acpi_resource *resource,
910 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
912 /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
913 memory24->write_protect =
914 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
915 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
916 memory24->minimum = p->start;
917 memory24->maximum = p->end;
918 memory24->alignment = 0;
919 memory24->address_length = p->end - p->start + 1;
921 dev_dbg(&dev->dev, " encode mem24 %#llx-%#llx write_protect %#x\n",
922 (unsigned long long) p->start, (unsigned long long) p->end,
923 memory24->write_protect);
926 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
927 struct acpi_resource *resource,
930 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
932 memory32->write_protect =
933 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
934 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
935 memory32->minimum = p->start;
936 memory32->maximum = p->end;
937 memory32->alignment = 0;
938 memory32->address_length = p->end - p->start + 1;
940 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx write_protect %#x\n",
941 (unsigned long long) p->start, (unsigned long long) p->end,
942 memory32->write_protect);
945 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
946 struct acpi_resource *resource,
949 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
951 fixed_memory32->write_protect =
952 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
953 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
954 fixed_memory32->address = p->start;
955 fixed_memory32->address_length = p->end - p->start + 1;
957 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx "
958 "write_protect %#x\n",
959 (unsigned long long) p->start, (unsigned long long) p->end,
960 fixed_memory32->write_protect);
963 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
966 /* pnpacpi_build_resource_template allocates extra mem */
967 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
968 struct acpi_resource *resource = buffer->pointer;
969 int port = 0, irq = 0, dma = 0, mem = 0;
971 dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
972 while (i < res_cnt) {
973 switch (resource->type) {
974 case ACPI_RESOURCE_TYPE_IRQ:
975 pnpacpi_encode_irq(dev, resource,
976 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
980 case ACPI_RESOURCE_TYPE_DMA:
981 pnpacpi_encode_dma(dev, resource,
982 pnp_get_resource(dev, IORESOURCE_DMA, dma));
985 case ACPI_RESOURCE_TYPE_IO:
986 pnpacpi_encode_io(dev, resource,
987 pnp_get_resource(dev, IORESOURCE_IO, port));
990 case ACPI_RESOURCE_TYPE_FIXED_IO:
991 pnpacpi_encode_fixed_io(dev, resource,
992 pnp_get_resource(dev, IORESOURCE_IO, port));
995 case ACPI_RESOURCE_TYPE_MEMORY24:
996 pnpacpi_encode_mem24(dev, resource,
997 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1000 case ACPI_RESOURCE_TYPE_MEMORY32:
1001 pnpacpi_encode_mem32(dev, resource,
1002 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1005 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1006 pnpacpi_encode_fixed_mem32(dev, resource,
1007 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1010 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1011 pnpacpi_encode_ext_irq(dev, resource,
1012 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1015 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1016 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1017 case ACPI_RESOURCE_TYPE_VENDOR:
1018 case ACPI_RESOURCE_TYPE_END_TAG:
1019 case ACPI_RESOURCE_TYPE_ADDRESS16:
1020 case ACPI_RESOURCE_TYPE_ADDRESS32:
1021 case ACPI_RESOURCE_TYPE_ADDRESS64:
1022 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1023 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1024 default: /* other type */
1025 dev_warn(&dev->dev, "can't encode unknown resource "
1026 "type %d\n", resource->type);