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