Merge branches 'acerhdf', 'acpi-pci-bind', 'bjorn-pci-root', 'bugzilla-12904', 'bugzi...
[linux-2.6.git] / drivers / pnp / pnpacpi / rsparser.c
1 /*
2  * pnpacpi -- PnP ACPI driver
3  *
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>
8  *
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
12  * later version.
13  *
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.
18  *
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
22  */
23 #include <linux/kernel.h>
24 #include <linux/acpi.h>
25 #include <linux/pci.h>
26 #include <linux/pnp.h>
27 #include "../base.h"
28 #include "pnpacpi.h"
29
30 #ifdef CONFIG_IA64
31 #define valid_IRQ(i) (1)
32 #else
33 #define valid_IRQ(i) (((i) != 0) && ((i) != 2))
34 #endif
35
36 /*
37  * Allocated Resources
38  */
39 static int irq_flags(int triggering, int polarity, int shareable)
40 {
41         int flags;
42
43         if (triggering == ACPI_LEVEL_SENSITIVE) {
44                 if (polarity == ACPI_ACTIVE_LOW)
45                         flags = IORESOURCE_IRQ_LOWLEVEL;
46                 else
47                         flags = IORESOURCE_IRQ_HIGHLEVEL;
48         } else {
49                 if (polarity == ACPI_ACTIVE_LOW)
50                         flags = IORESOURCE_IRQ_LOWEDGE;
51                 else
52                         flags = IORESOURCE_IRQ_HIGHEDGE;
53         }
54
55         if (shareable == ACPI_SHARED)
56                 flags |= IORESOURCE_IRQ_SHAREABLE;
57
58         return flags;
59 }
60
61 static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
62                              int *polarity, int *shareable)
63 {
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;
69                 break;
70         case IORESOURCE_IRQ_HIGHLEVEL:
71                 *triggering = ACPI_LEVEL_SENSITIVE;
72                 *polarity = ACPI_ACTIVE_HIGH;
73                 break;
74         case IORESOURCE_IRQ_LOWEDGE:
75                 *triggering = ACPI_EDGE_SENSITIVE;
76                 *polarity = ACPI_ACTIVE_LOW;
77                 break;
78         case IORESOURCE_IRQ_HIGHEDGE:
79                 *triggering = ACPI_EDGE_SENSITIVE;
80                 *polarity = ACPI_ACTIVE_HIGH;
81                 break;
82         default:
83                 dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
84                         flags);
85                 *triggering = ACPI_EDGE_SENSITIVE;
86                 *polarity = ACPI_ACTIVE_HIGH;
87                 break;
88         }
89
90         if (flags & IORESOURCE_IRQ_SHAREABLE)
91                 *shareable = ACPI_SHARED;
92         else
93                 *shareable = ACPI_EXCLUSIVE;
94 }
95
96 static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
97                                                 u32 gsi, int triggering,
98                                                 int polarity, int shareable)
99 {
100         int irq, flags;
101         int p, t;
102
103         if (!valid_IRQ(gsi)) {
104                 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
105                 return;
106         }
107
108         /*
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
112          */
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;
116
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");
120                         triggering = t;
121                         polarity = p;
122                 }
123         }
124
125         flags = irq_flags(triggering, polarity, shareable);
126         irq = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
127         if (irq >= 0)
128                 pcibios_penalize_isa_irq(irq, 1);
129         else
130                 flags |= IORESOURCE_DISABLED;
131
132         pnp_add_irq_resource(dev, irq, flags);
133 }
134
135 static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
136                      int transfer)
137 {
138         int flags = 0;
139
140         if (bus_master)
141                 flags |= IORESOURCE_DMA_MASTER;
142         switch (type) {
143         case ACPI_COMPATIBILITY:
144                 flags |= IORESOURCE_DMA_COMPATIBLE;
145                 break;
146         case ACPI_TYPE_A:
147                 flags |= IORESOURCE_DMA_TYPEA;
148                 break;
149         case ACPI_TYPE_B:
150                 flags |= IORESOURCE_DMA_TYPEB;
151                 break;
152         case ACPI_TYPE_F:
153                 flags |= IORESOURCE_DMA_TYPEF;
154                 break;
155         default:
156                 /* Set a default value ? */
157                 flags |= IORESOURCE_DMA_COMPATIBLE;
158                 dev_err(&dev->dev, "invalid DMA type %d\n", type);
159         }
160         switch (transfer) {
161         case ACPI_TRANSFER_8:
162                 flags |= IORESOURCE_DMA_8BIT;
163                 break;
164         case ACPI_TRANSFER_8_16:
165                 flags |= IORESOURCE_DMA_8AND16BIT;
166                 break;
167         case ACPI_TRANSFER_16:
168                 flags |= IORESOURCE_DMA_16BIT;
169                 break;
170         default:
171                 /* Set a default value ? */
172                 flags |= IORESOURCE_DMA_8AND16BIT;
173                 dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
174         }
175
176         return flags;
177 }
178
179 static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
180                                                u64 len, int io_decode)
181 {
182         int flags = 0;
183         u64 end = start + len - 1;
184
185         if (io_decode == ACPI_DECODE_16)
186                 flags |= IORESOURCE_IO_16BIT_ADDR;
187         if (len == 0 || end >= 0x10003)
188                 flags |= IORESOURCE_DISABLED;
189
190         pnp_add_io_resource(dev, start, end, flags);
191 }
192
193 /*
194  * Device CSRs that do not appear in PCI config space should be described
195  * via ACPI.  This would normally be done with Address Space Descriptors
196  * marked as "consumer-only," but old versions of Windows and Linux ignore
197  * the producer/consumer flag, so HP invented a vendor-defined resource to
198  * describe the location and size of CSR space.
199  */
200 static struct acpi_vendor_uuid hp_ccsr_uuid = {
201         .subtype = 2,
202         .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
203             0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
204 };
205
206 static int vendor_resource_matches(struct pnp_dev *dev,
207                                    struct acpi_resource_vendor_typed *vendor,
208                                    struct acpi_vendor_uuid *match,
209                                    int expected_len)
210 {
211         int uuid_len = sizeof(vendor->uuid);
212         u8 uuid_subtype = vendor->uuid_subtype;
213         u8 *uuid = vendor->uuid;
214         int actual_len;
215
216         /* byte_length includes uuid_subtype and uuid */
217         actual_len = vendor->byte_length - uuid_len - 1;
218
219         if (uuid_subtype == match->subtype &&
220             uuid_len == sizeof(match->data) &&
221             memcmp(uuid, match->data, uuid_len) == 0) {
222                 if (expected_len && expected_len != actual_len) {
223                         dev_err(&dev->dev, "wrong vendor descriptor size; "
224                                 "expected %d, found %d bytes\n",
225                                 expected_len, actual_len);
226                         return 0;
227                 }
228
229                 return 1;
230         }
231
232         return 0;
233 }
234
235 static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
236                                     struct acpi_resource_vendor_typed *vendor)
237 {
238         if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
239                 u64 start, length;
240
241                 memcpy(&start, vendor->byte_data, sizeof(start));
242                 memcpy(&length, vendor->byte_data + 8, sizeof(length));
243
244                 pnp_add_mem_resource(dev, start, start + length - 1, 0);
245         }
246 }
247
248 static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
249                                                 u64 start, u64 len,
250                                                 int write_protect)
251 {
252         int flags = 0;
253         u64 end = start + len - 1;
254
255         if (len == 0)
256                 flags |= IORESOURCE_DISABLED;
257         if (write_protect == ACPI_READ_WRITE_MEMORY)
258                 flags |= IORESOURCE_MEM_WRITEABLE;
259
260         pnp_add_mem_resource(dev, start, end, flags);
261 }
262
263 static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
264                                                   struct acpi_resource *res)
265 {
266         struct acpi_resource_address64 addr, *p = &addr;
267         acpi_status status;
268
269         status = acpi_resource_to_address64(res, p);
270         if (!ACPI_SUCCESS(status)) {
271                 dev_warn(&dev->dev, "failed to convert resource type %d\n",
272                          res->type);
273                 return;
274         }
275
276         if (p->producer_consumer == ACPI_PRODUCER)
277                 return;
278
279         if (p->resource_type == ACPI_MEMORY_RANGE)
280                 pnpacpi_parse_allocated_memresource(dev,
281                         p->minimum, p->address_length,
282                         p->info.mem.write_protect);
283         else if (p->resource_type == ACPI_IO_RANGE)
284                 pnpacpi_parse_allocated_ioresource(dev,
285                         p->minimum, p->address_length,
286                         p->granularity == 0xfff ? ACPI_DECODE_10 :
287                                 ACPI_DECODE_16);
288 }
289
290 static void pnpacpi_parse_allocated_ext_address_space(struct pnp_dev *dev,
291                                                       struct acpi_resource *res)
292 {
293         struct acpi_resource_extended_address64 *p = &res->data.ext_address64;
294
295         if (p->producer_consumer == ACPI_PRODUCER)
296                 return;
297
298         if (p->resource_type == ACPI_MEMORY_RANGE)
299                 pnpacpi_parse_allocated_memresource(dev,
300                         p->minimum, p->address_length,
301                         p->info.mem.write_protect);
302         else if (p->resource_type == ACPI_IO_RANGE)
303                 pnpacpi_parse_allocated_ioresource(dev,
304                         p->minimum, p->address_length,
305                         p->granularity == 0xfff ? ACPI_DECODE_10 :
306                                 ACPI_DECODE_16);
307 }
308
309 static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
310                                               void *data)
311 {
312         struct pnp_dev *dev = data;
313         struct acpi_resource_irq *irq;
314         struct acpi_resource_dma *dma;
315         struct acpi_resource_io *io;
316         struct acpi_resource_fixed_io *fixed_io;
317         struct acpi_resource_vendor_typed *vendor_typed;
318         struct acpi_resource_memory24 *memory24;
319         struct acpi_resource_memory32 *memory32;
320         struct acpi_resource_fixed_memory32 *fixed_memory32;
321         struct acpi_resource_extended_irq *extended_irq;
322         int i, flags;
323
324         switch (res->type) {
325         case ACPI_RESOURCE_TYPE_IRQ:
326                 /*
327                  * Per spec, only one interrupt per descriptor is allowed in
328                  * _CRS, but some firmware violates this, so parse them all.
329                  */
330                 irq = &res->data.irq;
331                 if (irq->interrupt_count == 0)
332                         pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
333                 else {
334                         for (i = 0; i < irq->interrupt_count; i++) {
335                                 pnpacpi_parse_allocated_irqresource(dev,
336                                         irq->interrupts[i],
337                                         irq->triggering,
338                                         irq->polarity,
339                                     irq->sharable);
340                         }
341
342                         /*
343                          * The IRQ encoder puts a single interrupt in each
344                          * descriptor, so if a _CRS descriptor has more than
345                          * one interrupt, we won't be able to re-encode it.
346                          */
347                         if (pnp_can_write(dev) && irq->interrupt_count > 1) {
348                                 dev_warn(&dev->dev, "multiple interrupts in "
349                                          "_CRS descriptor; configuration can't "
350                                          "be changed\n");
351                                 dev->capabilities &= ~PNP_WRITE;
352                         }
353                 }
354                 break;
355
356         case ACPI_RESOURCE_TYPE_DMA:
357                 dma = &res->data.dma;
358                 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
359                         flags = dma_flags(dev, dma->type, dma->bus_master,
360                                           dma->transfer);
361                 else
362                         flags = IORESOURCE_DISABLED;
363                 pnp_add_dma_resource(dev, dma->channels[0], flags);
364                 break;
365
366         case ACPI_RESOURCE_TYPE_IO:
367                 io = &res->data.io;
368                 pnpacpi_parse_allocated_ioresource(dev,
369                         io->minimum,
370                         io->address_length,
371                         io->io_decode);
372                 break;
373
374         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
375         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
376                 break;
377
378         case ACPI_RESOURCE_TYPE_FIXED_IO:
379                 fixed_io = &res->data.fixed_io;
380                 pnpacpi_parse_allocated_ioresource(dev,
381                         fixed_io->address,
382                         fixed_io->address_length,
383                         ACPI_DECODE_10);
384                 break;
385
386         case ACPI_RESOURCE_TYPE_VENDOR:
387                 vendor_typed = &res->data.vendor_typed;
388                 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
389                 break;
390
391         case ACPI_RESOURCE_TYPE_END_TAG:
392                 break;
393
394         case ACPI_RESOURCE_TYPE_MEMORY24:
395                 memory24 = &res->data.memory24;
396                 pnpacpi_parse_allocated_memresource(dev,
397                         memory24->minimum,
398                         memory24->address_length,
399                         memory24->write_protect);
400                 break;
401         case ACPI_RESOURCE_TYPE_MEMORY32:
402                 memory32 = &res->data.memory32;
403                 pnpacpi_parse_allocated_memresource(dev,
404                         memory32->minimum,
405                         memory32->address_length,
406                         memory32->write_protect);
407                 break;
408         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
409                 fixed_memory32 = &res->data.fixed_memory32;
410                 pnpacpi_parse_allocated_memresource(dev,
411                         fixed_memory32->address,
412                         fixed_memory32->address_length,
413                         fixed_memory32->write_protect);
414                 break;
415         case ACPI_RESOURCE_TYPE_ADDRESS16:
416         case ACPI_RESOURCE_TYPE_ADDRESS32:
417         case ACPI_RESOURCE_TYPE_ADDRESS64:
418                 pnpacpi_parse_allocated_address_space(dev, res);
419                 break;
420
421         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
422                 pnpacpi_parse_allocated_ext_address_space(dev, res);
423                 break;
424
425         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
426                 extended_irq = &res->data.extended_irq;
427
428                 if (extended_irq->interrupt_count == 0)
429                         pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
430                 else {
431                         for (i = 0; i < extended_irq->interrupt_count; i++) {
432                                 pnpacpi_parse_allocated_irqresource(dev,
433                                         extended_irq->interrupts[i],
434                                         extended_irq->triggering,
435                                         extended_irq->polarity,
436                                         extended_irq->sharable);
437                         }
438
439                         /*
440                          * The IRQ encoder puts a single interrupt in each
441                          * descriptor, so if a _CRS descriptor has more than
442                          * one interrupt, we won't be able to re-encode it.
443                          */
444                         if (pnp_can_write(dev) &&
445                             extended_irq->interrupt_count > 1) {
446                                 dev_warn(&dev->dev, "multiple interrupts in "
447                                          "_CRS descriptor; configuration can't "
448                                          "be changed\n");
449                                 dev->capabilities &= ~PNP_WRITE;
450                         }
451                 }
452                 break;
453
454         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
455                 break;
456
457         default:
458                 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
459                          res->type);
460                 return AE_ERROR;
461         }
462
463         return AE_OK;
464 }
465
466 int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
467 {
468         acpi_handle handle = dev->data;
469         acpi_status status;
470
471         pnp_dbg(&dev->dev, "parse allocated resources\n");
472
473         pnp_init_resources(dev);
474
475         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
476                                      pnpacpi_allocated_resource, dev);
477
478         if (ACPI_FAILURE(status)) {
479                 if (status != AE_NOT_FOUND)
480                         dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
481                 return -EPERM;
482         }
483         return 0;
484 }
485
486 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
487                                             unsigned int option_flags,
488                                             struct acpi_resource_dma *p)
489 {
490         int i;
491         unsigned char map = 0, flags;
492
493         if (p->channel_count == 0)
494                 return;
495
496         for (i = 0; i < p->channel_count; i++)
497                 map |= 1 << p->channels[i];
498
499         flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
500         pnp_register_dma_resource(dev, option_flags, map, flags);
501 }
502
503 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
504                                             unsigned int option_flags,
505                                             struct acpi_resource_irq *p)
506 {
507         int i;
508         pnp_irq_mask_t map;
509         unsigned char flags;
510
511         if (p->interrupt_count == 0)
512                 return;
513
514         bitmap_zero(map.bits, PNP_IRQ_NR);
515         for (i = 0; i < p->interrupt_count; i++)
516                 if (p->interrupts[i])
517                         __set_bit(p->interrupts[i], map.bits);
518
519         flags = irq_flags(p->triggering, p->polarity, p->sharable);
520         pnp_register_irq_resource(dev, option_flags, &map, flags);
521 }
522
523 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
524                                         unsigned int option_flags,
525                                         struct acpi_resource_extended_irq *p)
526 {
527         int i;
528         pnp_irq_mask_t map;
529         unsigned char flags;
530
531         if (p->interrupt_count == 0)
532                 return;
533
534         bitmap_zero(map.bits, PNP_IRQ_NR);
535         for (i = 0; i < p->interrupt_count; i++) {
536                 if (p->interrupts[i]) {
537                         if (p->interrupts[i] < PNP_IRQ_NR)
538                                 __set_bit(p->interrupts[i], map.bits);
539                         else
540                                 dev_err(&dev->dev, "ignoring IRQ %d option "
541                                         "(too large for %d entry bitmap)\n",
542                                         p->interrupts[i], PNP_IRQ_NR);
543                 }
544         }
545
546         flags = irq_flags(p->triggering, p->polarity, p->sharable);
547         pnp_register_irq_resource(dev, option_flags, &map, flags);
548 }
549
550 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
551                                              unsigned int option_flags,
552                                              struct acpi_resource_io *io)
553 {
554         unsigned char flags = 0;
555
556         if (io->address_length == 0)
557                 return;
558
559         if (io->io_decode == ACPI_DECODE_16)
560                 flags = IORESOURCE_IO_16BIT_ADDR;
561         pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
562                                    io->alignment, io->address_length, flags);
563 }
564
565 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
566                                         unsigned int option_flags,
567                                         struct acpi_resource_fixed_io *io)
568 {
569         if (io->address_length == 0)
570                 return;
571
572         pnp_register_port_resource(dev, option_flags, io->address, io->address,
573                                    0, io->address_length, IORESOURCE_IO_FIXED);
574 }
575
576 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
577                                               unsigned int option_flags,
578                                               struct acpi_resource_memory24 *p)
579 {
580         unsigned char flags = 0;
581
582         if (p->address_length == 0)
583                 return;
584
585         if (p->write_protect == ACPI_READ_WRITE_MEMORY)
586                 flags = IORESOURCE_MEM_WRITEABLE;
587         pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
588                                   p->alignment, p->address_length, flags);
589 }
590
591 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
592                                               unsigned int option_flags,
593                                               struct acpi_resource_memory32 *p)
594 {
595         unsigned char flags = 0;
596
597         if (p->address_length == 0)
598                 return;
599
600         if (p->write_protect == ACPI_READ_WRITE_MEMORY)
601                 flags = IORESOURCE_MEM_WRITEABLE;
602         pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
603                                   p->alignment, p->address_length, flags);
604 }
605
606 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
607                                         unsigned int option_flags,
608                                         struct acpi_resource_fixed_memory32 *p)
609 {
610         unsigned char flags = 0;
611
612         if (p->address_length == 0)
613                 return;
614
615         if (p->write_protect == ACPI_READ_WRITE_MEMORY)
616                 flags = IORESOURCE_MEM_WRITEABLE;
617         pnp_register_mem_resource(dev, option_flags, p->address, p->address,
618                                   0, p->address_length, flags);
619 }
620
621 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
622                                                 unsigned int option_flags,
623                                                 struct acpi_resource *r)
624 {
625         struct acpi_resource_address64 addr, *p = &addr;
626         acpi_status status;
627         unsigned char flags = 0;
628
629         status = acpi_resource_to_address64(r, p);
630         if (ACPI_FAILURE(status)) {
631                 dev_warn(&dev->dev, "can't convert resource type %d\n",
632                          r->type);
633                 return;
634         }
635
636         if (p->address_length == 0)
637                 return;
638
639         if (p->resource_type == ACPI_MEMORY_RANGE) {
640                 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
641                         flags = IORESOURCE_MEM_WRITEABLE;
642                 pnp_register_mem_resource(dev, option_flags, p->minimum,
643                                           p->minimum, 0, p->address_length,
644                                           flags);
645         } else if (p->resource_type == ACPI_IO_RANGE)
646                 pnp_register_port_resource(dev, option_flags, p->minimum,
647                                            p->minimum, 0, p->address_length,
648                                            IORESOURCE_IO_FIXED);
649 }
650
651 static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
652                                                     unsigned int option_flags,
653                                                     struct acpi_resource *r)
654 {
655         struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
656         unsigned char flags = 0;
657
658         if (p->address_length == 0)
659                 return;
660
661         if (p->resource_type == ACPI_MEMORY_RANGE) {
662                 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
663                         flags = IORESOURCE_MEM_WRITEABLE;
664                 pnp_register_mem_resource(dev, option_flags, p->minimum,
665                                           p->minimum, 0, p->address_length,
666                                           flags);
667         } else if (p->resource_type == ACPI_IO_RANGE)
668                 pnp_register_port_resource(dev, option_flags, p->minimum,
669                                            p->minimum, 0, p->address_length,
670                                            IORESOURCE_IO_FIXED);
671 }
672
673 struct acpipnp_parse_option_s {
674         struct pnp_dev *dev;
675         unsigned int option_flags;
676 };
677
678 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
679                                                   void *data)
680 {
681         int priority;
682         struct acpipnp_parse_option_s *parse_data = data;
683         struct pnp_dev *dev = parse_data->dev;
684         unsigned int option_flags = parse_data->option_flags;
685
686         switch (res->type) {
687         case ACPI_RESOURCE_TYPE_IRQ:
688                 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
689                 break;
690
691         case ACPI_RESOURCE_TYPE_DMA:
692                 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
693                 break;
694
695         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
696                 switch (res->data.start_dpf.compatibility_priority) {
697                 case ACPI_GOOD_CONFIGURATION:
698                         priority = PNP_RES_PRIORITY_PREFERRED;
699                         break;
700
701                 case ACPI_ACCEPTABLE_CONFIGURATION:
702                         priority = PNP_RES_PRIORITY_ACCEPTABLE;
703                         break;
704
705                 case ACPI_SUB_OPTIMAL_CONFIGURATION:
706                         priority = PNP_RES_PRIORITY_FUNCTIONAL;
707                         break;
708                 default:
709                         priority = PNP_RES_PRIORITY_INVALID;
710                         break;
711                 }
712                 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
713                 break;
714
715         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
716                 parse_data->option_flags = 0;
717                 break;
718
719         case ACPI_RESOURCE_TYPE_IO:
720                 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
721                 break;
722
723         case ACPI_RESOURCE_TYPE_FIXED_IO:
724                 pnpacpi_parse_fixed_port_option(dev, option_flags,
725                                                 &res->data.fixed_io);
726                 break;
727
728         case ACPI_RESOURCE_TYPE_VENDOR:
729         case ACPI_RESOURCE_TYPE_END_TAG:
730                 break;
731
732         case ACPI_RESOURCE_TYPE_MEMORY24:
733                 pnpacpi_parse_mem24_option(dev, option_flags,
734                                            &res->data.memory24);
735                 break;
736
737         case ACPI_RESOURCE_TYPE_MEMORY32:
738                 pnpacpi_parse_mem32_option(dev, option_flags,
739                                            &res->data.memory32);
740                 break;
741
742         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
743                 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
744                                                  &res->data.fixed_memory32);
745                 break;
746
747         case ACPI_RESOURCE_TYPE_ADDRESS16:
748         case ACPI_RESOURCE_TYPE_ADDRESS32:
749         case ACPI_RESOURCE_TYPE_ADDRESS64:
750                 pnpacpi_parse_address_option(dev, option_flags, res);
751                 break;
752
753         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
754                 pnpacpi_parse_ext_address_option(dev, option_flags, res);
755                 break;
756
757         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
758                 pnpacpi_parse_ext_irq_option(dev, option_flags,
759                                              &res->data.extended_irq);
760                 break;
761
762         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
763                 break;
764
765         default:
766                 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
767                          res->type);
768                 return AE_ERROR;
769         }
770
771         return AE_OK;
772 }
773
774 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
775 {
776         acpi_handle handle = dev->data;
777         acpi_status status;
778         struct acpipnp_parse_option_s parse_data;
779
780         pnp_dbg(&dev->dev, "parse resource options\n");
781
782         parse_data.dev = dev;
783         parse_data.option_flags = 0;
784
785         status = acpi_walk_resources(handle, METHOD_NAME__PRS,
786                                      pnpacpi_option_resource, &parse_data);
787
788         if (ACPI_FAILURE(status)) {
789                 if (status != AE_NOT_FOUND)
790                         dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
791                 return -EPERM;
792         }
793         return 0;
794 }
795
796 static int pnpacpi_supported_resource(struct acpi_resource *res)
797 {
798         switch (res->type) {
799         case ACPI_RESOURCE_TYPE_IRQ:
800         case ACPI_RESOURCE_TYPE_DMA:
801         case ACPI_RESOURCE_TYPE_IO:
802         case ACPI_RESOURCE_TYPE_FIXED_IO:
803         case ACPI_RESOURCE_TYPE_MEMORY24:
804         case ACPI_RESOURCE_TYPE_MEMORY32:
805         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
806         case ACPI_RESOURCE_TYPE_ADDRESS16:
807         case ACPI_RESOURCE_TYPE_ADDRESS32:
808         case ACPI_RESOURCE_TYPE_ADDRESS64:
809         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
810         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
811                 return 1;
812         }
813         return 0;
814 }
815
816 /*
817  * Set resource
818  */
819 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
820                                            void *data)
821 {
822         int *res_cnt = data;
823
824         if (pnpacpi_supported_resource(res))
825                 (*res_cnt)++;
826         return AE_OK;
827 }
828
829 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
830 {
831         struct acpi_resource **resource = data;
832
833         if (pnpacpi_supported_resource(res)) {
834                 (*resource)->type = res->type;
835                 (*resource)->length = sizeof(struct acpi_resource);
836                 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
837                         (*resource)->data.irq.descriptor_length =
838                                         res->data.irq.descriptor_length;
839                 (*resource)++;
840         }
841
842         return AE_OK;
843 }
844
845 int pnpacpi_build_resource_template(struct pnp_dev *dev,
846                                     struct acpi_buffer *buffer)
847 {
848         acpi_handle handle = dev->data;
849         struct acpi_resource *resource;
850         int res_cnt = 0;
851         acpi_status status;
852
853         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
854                                      pnpacpi_count_resources, &res_cnt);
855         if (ACPI_FAILURE(status)) {
856                 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
857                 return -EINVAL;
858         }
859         if (!res_cnt)
860                 return -EINVAL;
861         buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
862         buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
863         if (!buffer->pointer)
864                 return -ENOMEM;
865
866         resource = (struct acpi_resource *)buffer->pointer;
867         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
868                                      pnpacpi_type_resources, &resource);
869         if (ACPI_FAILURE(status)) {
870                 kfree(buffer->pointer);
871                 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
872                 return -EINVAL;
873         }
874         /* resource will pointer the end resource now */
875         resource->type = ACPI_RESOURCE_TYPE_END_TAG;
876
877         return 0;
878 }
879
880 static void pnpacpi_encode_irq(struct pnp_dev *dev,
881                                struct acpi_resource *resource,
882                                struct resource *p)
883 {
884         struct acpi_resource_irq *irq = &resource->data.irq;
885         int triggering, polarity, shareable;
886
887         if (!pnp_resource_enabled(p)) {
888                 irq->interrupt_count = 0;
889                 pnp_dbg(&dev->dev, "  encode irq (%s)\n",
890                         p ? "disabled" : "missing");
891                 return;
892         }
893
894         decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
895         irq->triggering = triggering;
896         irq->polarity = polarity;
897         irq->sharable = shareable;
898         irq->interrupt_count = 1;
899         irq->interrupts[0] = p->start;
900
901         pnp_dbg(&dev->dev, "  encode irq %d %s %s %s (%d-byte descriptor)\n",
902                 (int) p->start,
903                 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
904                 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
905                 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
906                 irq->descriptor_length);
907 }
908
909 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
910                                    struct acpi_resource *resource,
911                                    struct resource *p)
912 {
913         struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
914         int triggering, polarity, shareable;
915
916         if (!pnp_resource_enabled(p)) {
917                 extended_irq->interrupt_count = 0;
918                 pnp_dbg(&dev->dev, "  encode extended irq (%s)\n",
919                         p ? "disabled" : "missing");
920                 return;
921         }
922
923         decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
924         extended_irq->producer_consumer = ACPI_CONSUMER;
925         extended_irq->triggering = triggering;
926         extended_irq->polarity = polarity;
927         extended_irq->sharable = shareable;
928         extended_irq->interrupt_count = 1;
929         extended_irq->interrupts[0] = p->start;
930
931         pnp_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
932                 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
933                 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
934                 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
935 }
936
937 static void pnpacpi_encode_dma(struct pnp_dev *dev,
938                                struct acpi_resource *resource,
939                                struct resource *p)
940 {
941         struct acpi_resource_dma *dma = &resource->data.dma;
942
943         if (!pnp_resource_enabled(p)) {
944                 dma->channel_count = 0;
945                 pnp_dbg(&dev->dev, "  encode dma (%s)\n",
946                         p ? "disabled" : "missing");
947                 return;
948         }
949
950         /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
951         switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
952         case IORESOURCE_DMA_TYPEA:
953                 dma->type = ACPI_TYPE_A;
954                 break;
955         case IORESOURCE_DMA_TYPEB:
956                 dma->type = ACPI_TYPE_B;
957                 break;
958         case IORESOURCE_DMA_TYPEF:
959                 dma->type = ACPI_TYPE_F;
960                 break;
961         default:
962                 dma->type = ACPI_COMPATIBILITY;
963         }
964
965         switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
966         case IORESOURCE_DMA_8BIT:
967                 dma->transfer = ACPI_TRANSFER_8;
968                 break;
969         case IORESOURCE_DMA_8AND16BIT:
970                 dma->transfer = ACPI_TRANSFER_8_16;
971                 break;
972         default:
973                 dma->transfer = ACPI_TRANSFER_16;
974         }
975
976         dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
977         dma->channel_count = 1;
978         dma->channels[0] = p->start;
979
980         pnp_dbg(&dev->dev, "  encode dma %d "
981                 "type %#x transfer %#x master %d\n",
982                 (int) p->start, dma->type, dma->transfer, dma->bus_master);
983 }
984
985 static void pnpacpi_encode_io(struct pnp_dev *dev,
986                               struct acpi_resource *resource,
987                               struct resource *p)
988 {
989         struct acpi_resource_io *io = &resource->data.io;
990
991         if (pnp_resource_enabled(p)) {
992                 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
993                 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
994                     ACPI_DECODE_16 : ACPI_DECODE_10;
995                 io->minimum = p->start;
996                 io->maximum = p->end;
997                 io->alignment = 0;      /* Correct? */
998                 io->address_length = p->end - p->start + 1;
999         } else {
1000                 io->minimum = 0;
1001                 io->address_length = 0;
1002         }
1003
1004         pnp_dbg(&dev->dev, "  encode io %#x-%#x decode %#x\n", io->minimum,
1005                 io->minimum + io->address_length - 1, io->io_decode);
1006 }
1007
1008 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
1009                                     struct acpi_resource *resource,
1010                                     struct resource *p)
1011 {
1012         struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
1013
1014         if (pnp_resource_enabled(p)) {
1015                 fixed_io->address = p->start;
1016                 fixed_io->address_length = p->end - p->start + 1;
1017         } else {
1018                 fixed_io->address = 0;
1019                 fixed_io->address_length = 0;
1020         }
1021
1022         pnp_dbg(&dev->dev, "  encode fixed_io %#x-%#x\n", fixed_io->address,
1023                 fixed_io->address + fixed_io->address_length - 1);
1024 }
1025
1026 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
1027                                  struct acpi_resource *resource,
1028                                  struct resource *p)
1029 {
1030         struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
1031
1032         if (pnp_resource_enabled(p)) {
1033                 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
1034                 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1035                     ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1036                 memory24->minimum = p->start;
1037                 memory24->maximum = p->end;
1038                 memory24->alignment = 0;
1039                 memory24->address_length = p->end - p->start + 1;
1040         } else {
1041                 memory24->minimum = 0;
1042                 memory24->address_length = 0;
1043         }
1044
1045         pnp_dbg(&dev->dev, "  encode mem24 %#x-%#x write_protect %#x\n",
1046                 memory24->minimum,
1047                 memory24->minimum + memory24->address_length - 1,
1048                 memory24->write_protect);
1049 }
1050
1051 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1052                                  struct acpi_resource *resource,
1053                                  struct resource *p)
1054 {
1055         struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1056
1057         if (pnp_resource_enabled(p)) {
1058                 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1059                     ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1060                 memory32->minimum = p->start;
1061                 memory32->maximum = p->end;
1062                 memory32->alignment = 0;
1063                 memory32->address_length = p->end - p->start + 1;
1064         } else {
1065                 memory32->minimum = 0;
1066                 memory32->alignment = 0;
1067         }
1068
1069         pnp_dbg(&dev->dev, "  encode mem32 %#x-%#x write_protect %#x\n",
1070                 memory32->minimum,
1071                 memory32->minimum + memory32->address_length - 1,
1072                 memory32->write_protect);
1073 }
1074
1075 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1076                                        struct acpi_resource *resource,
1077                                        struct resource *p)
1078 {
1079         struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1080
1081         if (pnp_resource_enabled(p)) {
1082                 fixed_memory32->write_protect =
1083                     p->flags & IORESOURCE_MEM_WRITEABLE ?
1084                     ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1085                 fixed_memory32->address = p->start;
1086                 fixed_memory32->address_length = p->end - p->start + 1;
1087         } else {
1088                 fixed_memory32->address = 0;
1089                 fixed_memory32->address_length = 0;
1090         }
1091
1092         pnp_dbg(&dev->dev, "  encode fixed_mem32 %#x-%#x write_protect %#x\n",
1093                 fixed_memory32->address,
1094                 fixed_memory32->address + fixed_memory32->address_length - 1,
1095                 fixed_memory32->write_protect);
1096 }
1097
1098 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1099 {
1100         int i = 0;
1101         /* pnpacpi_build_resource_template allocates extra mem */
1102         int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1103         struct acpi_resource *resource = buffer->pointer;
1104         int port = 0, irq = 0, dma = 0, mem = 0;
1105
1106         pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1107         while (i < res_cnt) {
1108                 switch (resource->type) {
1109                 case ACPI_RESOURCE_TYPE_IRQ:
1110                         pnpacpi_encode_irq(dev, resource,
1111                                pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1112                         irq++;
1113                         break;
1114
1115                 case ACPI_RESOURCE_TYPE_DMA:
1116                         pnpacpi_encode_dma(dev, resource,
1117                                 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1118                         dma++;
1119                         break;
1120                 case ACPI_RESOURCE_TYPE_IO:
1121                         pnpacpi_encode_io(dev, resource,
1122                                 pnp_get_resource(dev, IORESOURCE_IO, port));
1123                         port++;
1124                         break;
1125                 case ACPI_RESOURCE_TYPE_FIXED_IO:
1126                         pnpacpi_encode_fixed_io(dev, resource,
1127                                 pnp_get_resource(dev, IORESOURCE_IO, port));
1128                         port++;
1129                         break;
1130                 case ACPI_RESOURCE_TYPE_MEMORY24:
1131                         pnpacpi_encode_mem24(dev, resource,
1132                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1133                         mem++;
1134                         break;
1135                 case ACPI_RESOURCE_TYPE_MEMORY32:
1136                         pnpacpi_encode_mem32(dev, resource,
1137                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1138                         mem++;
1139                         break;
1140                 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1141                         pnpacpi_encode_fixed_mem32(dev, resource,
1142                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1143                         mem++;
1144                         break;
1145                 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1146                         pnpacpi_encode_ext_irq(dev, resource,
1147                                 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1148                         irq++;
1149                         break;
1150                 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1151                 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1152                 case ACPI_RESOURCE_TYPE_VENDOR:
1153                 case ACPI_RESOURCE_TYPE_END_TAG:
1154                 case ACPI_RESOURCE_TYPE_ADDRESS16:
1155                 case ACPI_RESOURCE_TYPE_ADDRESS32:
1156                 case ACPI_RESOURCE_TYPE_ADDRESS64:
1157                 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1158                 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1159                 default:        /* other type */
1160                         dev_warn(&dev->dev, "can't encode unknown resource "
1161                                  "type %d\n", resource->type);
1162                         return -EINVAL;
1163                 }
1164                 resource++;
1165                 i++;
1166         }
1167         return 0;
1168 }