- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.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         struct acpi_device *acpi_dev = dev->data;
469         acpi_handle handle = acpi_dev->handle;
470         acpi_status status;
471
472         pnp_dbg(&dev->dev, "parse allocated resources\n");
473
474         pnp_init_resources(dev);
475
476         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
477                                      pnpacpi_allocated_resource, dev);
478
479         if (ACPI_FAILURE(status)) {
480                 if (status != AE_NOT_FOUND)
481                         dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
482                 return -EPERM;
483         }
484         return 0;
485 }
486
487 static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
488                                             unsigned int option_flags,
489                                             struct acpi_resource_dma *p)
490 {
491         int i;
492         unsigned char map = 0, flags;
493
494         if (p->channel_count == 0)
495                 return;
496
497         for (i = 0; i < p->channel_count; i++)
498                 map |= 1 << p->channels[i];
499
500         flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
501         pnp_register_dma_resource(dev, option_flags, map, flags);
502 }
503
504 static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
505                                             unsigned int option_flags,
506                                             struct acpi_resource_irq *p)
507 {
508         int i;
509         pnp_irq_mask_t map;
510         unsigned char flags;
511
512         if (p->interrupt_count == 0)
513                 return;
514
515         bitmap_zero(map.bits, PNP_IRQ_NR);
516         for (i = 0; i < p->interrupt_count; i++)
517                 if (p->interrupts[i])
518                         __set_bit(p->interrupts[i], map.bits);
519
520         flags = irq_flags(p->triggering, p->polarity, p->sharable);
521         pnp_register_irq_resource(dev, option_flags, &map, flags);
522 }
523
524 static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
525                                         unsigned int option_flags,
526                                         struct acpi_resource_extended_irq *p)
527 {
528         int i;
529         pnp_irq_mask_t map;
530         unsigned char flags;
531
532         if (p->interrupt_count == 0)
533                 return;
534
535         bitmap_zero(map.bits, PNP_IRQ_NR);
536         for (i = 0; i < p->interrupt_count; i++) {
537                 if (p->interrupts[i]) {
538                         if (p->interrupts[i] < PNP_IRQ_NR)
539                                 __set_bit(p->interrupts[i], map.bits);
540                         else
541                                 dev_err(&dev->dev, "ignoring IRQ %d option "
542                                         "(too large for %d entry bitmap)\n",
543                                         p->interrupts[i], PNP_IRQ_NR);
544                 }
545         }
546
547         flags = irq_flags(p->triggering, p->polarity, p->sharable);
548         pnp_register_irq_resource(dev, option_flags, &map, flags);
549 }
550
551 static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
552                                              unsigned int option_flags,
553                                              struct acpi_resource_io *io)
554 {
555         unsigned char flags = 0;
556
557         if (io->address_length == 0)
558                 return;
559
560         if (io->io_decode == ACPI_DECODE_16)
561                 flags = IORESOURCE_IO_16BIT_ADDR;
562         pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
563                                    io->alignment, io->address_length, flags);
564 }
565
566 static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
567                                         unsigned int option_flags,
568                                         struct acpi_resource_fixed_io *io)
569 {
570         if (io->address_length == 0)
571                 return;
572
573         pnp_register_port_resource(dev, option_flags, io->address, io->address,
574                                    0, io->address_length, IORESOURCE_IO_FIXED);
575 }
576
577 static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
578                                               unsigned int option_flags,
579                                               struct acpi_resource_memory24 *p)
580 {
581         unsigned char flags = 0;
582
583         if (p->address_length == 0)
584                 return;
585
586         if (p->write_protect == ACPI_READ_WRITE_MEMORY)
587                 flags = IORESOURCE_MEM_WRITEABLE;
588         pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
589                                   p->alignment, p->address_length, flags);
590 }
591
592 static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
593                                               unsigned int option_flags,
594                                               struct acpi_resource_memory32 *p)
595 {
596         unsigned char flags = 0;
597
598         if (p->address_length == 0)
599                 return;
600
601         if (p->write_protect == ACPI_READ_WRITE_MEMORY)
602                 flags = IORESOURCE_MEM_WRITEABLE;
603         pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
604                                   p->alignment, p->address_length, flags);
605 }
606
607 static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
608                                         unsigned int option_flags,
609                                         struct acpi_resource_fixed_memory32 *p)
610 {
611         unsigned char flags = 0;
612
613         if (p->address_length == 0)
614                 return;
615
616         if (p->write_protect == ACPI_READ_WRITE_MEMORY)
617                 flags = IORESOURCE_MEM_WRITEABLE;
618         pnp_register_mem_resource(dev, option_flags, p->address, p->address,
619                                   0, p->address_length, flags);
620 }
621
622 static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
623                                                 unsigned int option_flags,
624                                                 struct acpi_resource *r)
625 {
626         struct acpi_resource_address64 addr, *p = &addr;
627         acpi_status status;
628         unsigned char flags = 0;
629
630         status = acpi_resource_to_address64(r, p);
631         if (ACPI_FAILURE(status)) {
632                 dev_warn(&dev->dev, "can't convert resource type %d\n",
633                          r->type);
634                 return;
635         }
636
637         if (p->address_length == 0)
638                 return;
639
640         if (p->resource_type == ACPI_MEMORY_RANGE) {
641                 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
642                         flags = IORESOURCE_MEM_WRITEABLE;
643                 pnp_register_mem_resource(dev, option_flags, p->minimum,
644                                           p->minimum, 0, p->address_length,
645                                           flags);
646         } else if (p->resource_type == ACPI_IO_RANGE)
647                 pnp_register_port_resource(dev, option_flags, p->minimum,
648                                            p->minimum, 0, p->address_length,
649                                            IORESOURCE_IO_FIXED);
650 }
651
652 static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
653                                                     unsigned int option_flags,
654                                                     struct acpi_resource *r)
655 {
656         struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
657         unsigned char flags = 0;
658
659         if (p->address_length == 0)
660                 return;
661
662         if (p->resource_type == ACPI_MEMORY_RANGE) {
663                 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
664                         flags = IORESOURCE_MEM_WRITEABLE;
665                 pnp_register_mem_resource(dev, option_flags, p->minimum,
666                                           p->minimum, 0, p->address_length,
667                                           flags);
668         } else if (p->resource_type == ACPI_IO_RANGE)
669                 pnp_register_port_resource(dev, option_flags, p->minimum,
670                                            p->minimum, 0, p->address_length,
671                                            IORESOURCE_IO_FIXED);
672 }
673
674 struct acpipnp_parse_option_s {
675         struct pnp_dev *dev;
676         unsigned int option_flags;
677 };
678
679 static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
680                                                   void *data)
681 {
682         int priority;
683         struct acpipnp_parse_option_s *parse_data = data;
684         struct pnp_dev *dev = parse_data->dev;
685         unsigned int option_flags = parse_data->option_flags;
686
687         switch (res->type) {
688         case ACPI_RESOURCE_TYPE_IRQ:
689                 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
690                 break;
691
692         case ACPI_RESOURCE_TYPE_DMA:
693                 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
694                 break;
695
696         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
697                 switch (res->data.start_dpf.compatibility_priority) {
698                 case ACPI_GOOD_CONFIGURATION:
699                         priority = PNP_RES_PRIORITY_PREFERRED;
700                         break;
701
702                 case ACPI_ACCEPTABLE_CONFIGURATION:
703                         priority = PNP_RES_PRIORITY_ACCEPTABLE;
704                         break;
705
706                 case ACPI_SUB_OPTIMAL_CONFIGURATION:
707                         priority = PNP_RES_PRIORITY_FUNCTIONAL;
708                         break;
709                 default:
710                         priority = PNP_RES_PRIORITY_INVALID;
711                         break;
712                 }
713                 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
714                 break;
715
716         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
717                 parse_data->option_flags = 0;
718                 break;
719
720         case ACPI_RESOURCE_TYPE_IO:
721                 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
722                 break;
723
724         case ACPI_RESOURCE_TYPE_FIXED_IO:
725                 pnpacpi_parse_fixed_port_option(dev, option_flags,
726                                                 &res->data.fixed_io);
727                 break;
728
729         case ACPI_RESOURCE_TYPE_VENDOR:
730         case ACPI_RESOURCE_TYPE_END_TAG:
731                 break;
732
733         case ACPI_RESOURCE_TYPE_MEMORY24:
734                 pnpacpi_parse_mem24_option(dev, option_flags,
735                                            &res->data.memory24);
736                 break;
737
738         case ACPI_RESOURCE_TYPE_MEMORY32:
739                 pnpacpi_parse_mem32_option(dev, option_flags,
740                                            &res->data.memory32);
741                 break;
742
743         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
744                 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
745                                                  &res->data.fixed_memory32);
746                 break;
747
748         case ACPI_RESOURCE_TYPE_ADDRESS16:
749         case ACPI_RESOURCE_TYPE_ADDRESS32:
750         case ACPI_RESOURCE_TYPE_ADDRESS64:
751                 pnpacpi_parse_address_option(dev, option_flags, res);
752                 break;
753
754         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
755                 pnpacpi_parse_ext_address_option(dev, option_flags, res);
756                 break;
757
758         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
759                 pnpacpi_parse_ext_irq_option(dev, option_flags,
760                                              &res->data.extended_irq);
761                 break;
762
763         case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
764                 break;
765
766         default:
767                 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
768                          res->type);
769                 return AE_ERROR;
770         }
771
772         return AE_OK;
773 }
774
775 int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
776 {
777         struct acpi_device *acpi_dev = dev->data;
778         acpi_handle handle = acpi_dev->handle;
779         acpi_status status;
780         struct acpipnp_parse_option_s parse_data;
781
782         pnp_dbg(&dev->dev, "parse resource options\n");
783
784         parse_data.dev = dev;
785         parse_data.option_flags = 0;
786
787         status = acpi_walk_resources(handle, METHOD_NAME__PRS,
788                                      pnpacpi_option_resource, &parse_data);
789
790         if (ACPI_FAILURE(status)) {
791                 if (status != AE_NOT_FOUND)
792                         dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
793                 return -EPERM;
794         }
795         return 0;
796 }
797
798 static int pnpacpi_supported_resource(struct acpi_resource *res)
799 {
800         switch (res->type) {
801         case ACPI_RESOURCE_TYPE_IRQ:
802         case ACPI_RESOURCE_TYPE_DMA:
803         case ACPI_RESOURCE_TYPE_IO:
804         case ACPI_RESOURCE_TYPE_FIXED_IO:
805         case ACPI_RESOURCE_TYPE_MEMORY24:
806         case ACPI_RESOURCE_TYPE_MEMORY32:
807         case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
808         case ACPI_RESOURCE_TYPE_ADDRESS16:
809         case ACPI_RESOURCE_TYPE_ADDRESS32:
810         case ACPI_RESOURCE_TYPE_ADDRESS64:
811         case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
812         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
813                 return 1;
814         }
815         return 0;
816 }
817
818 /*
819  * Set resource
820  */
821 static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
822                                            void *data)
823 {
824         int *res_cnt = data;
825
826         if (pnpacpi_supported_resource(res))
827                 (*res_cnt)++;
828         return AE_OK;
829 }
830
831 static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
832 {
833         struct acpi_resource **resource = data;
834
835         if (pnpacpi_supported_resource(res)) {
836                 (*resource)->type = res->type;
837                 (*resource)->length = sizeof(struct acpi_resource);
838                 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
839                         (*resource)->data.irq.descriptor_length =
840                                         res->data.irq.descriptor_length;
841                 (*resource)++;
842         }
843
844         return AE_OK;
845 }
846
847 int pnpacpi_build_resource_template(struct pnp_dev *dev,
848                                     struct acpi_buffer *buffer)
849 {
850         struct acpi_device *acpi_dev = dev->data;
851         acpi_handle handle = acpi_dev->handle;
852         struct acpi_resource *resource;
853         int res_cnt = 0;
854         acpi_status status;
855
856         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
857                                      pnpacpi_count_resources, &res_cnt);
858         if (ACPI_FAILURE(status)) {
859                 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
860                 return -EINVAL;
861         }
862         if (!res_cnt)
863                 return -EINVAL;
864         buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
865         buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
866         if (!buffer->pointer)
867                 return -ENOMEM;
868
869         resource = (struct acpi_resource *)buffer->pointer;
870         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
871                                      pnpacpi_type_resources, &resource);
872         if (ACPI_FAILURE(status)) {
873                 kfree(buffer->pointer);
874                 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
875                 return -EINVAL;
876         }
877         /* resource will pointer the end resource now */
878         resource->type = ACPI_RESOURCE_TYPE_END_TAG;
879
880         return 0;
881 }
882
883 static void pnpacpi_encode_irq(struct pnp_dev *dev,
884                                struct acpi_resource *resource,
885                                struct resource *p)
886 {
887         struct acpi_resource_irq *irq = &resource->data.irq;
888         int triggering, polarity, shareable;
889
890         if (!pnp_resource_enabled(p)) {
891                 irq->interrupt_count = 0;
892                 pnp_dbg(&dev->dev, "  encode irq (%s)\n",
893                         p ? "disabled" : "missing");
894                 return;
895         }
896
897         decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
898         irq->triggering = triggering;
899         irq->polarity = polarity;
900         irq->sharable = shareable;
901         irq->interrupt_count = 1;
902         irq->interrupts[0] = p->start;
903
904         pnp_dbg(&dev->dev, "  encode irq %d %s %s %s (%d-byte descriptor)\n",
905                 (int) p->start,
906                 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
907                 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
908                 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
909                 irq->descriptor_length);
910 }
911
912 static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
913                                    struct acpi_resource *resource,
914                                    struct resource *p)
915 {
916         struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
917         int triggering, polarity, shareable;
918
919         if (!pnp_resource_enabled(p)) {
920                 extended_irq->interrupt_count = 0;
921                 pnp_dbg(&dev->dev, "  encode extended irq (%s)\n",
922                         p ? "disabled" : "missing");
923                 return;
924         }
925
926         decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
927         extended_irq->producer_consumer = ACPI_CONSUMER;
928         extended_irq->triggering = triggering;
929         extended_irq->polarity = polarity;
930         extended_irq->sharable = shareable;
931         extended_irq->interrupt_count = 1;
932         extended_irq->interrupts[0] = p->start;
933
934         pnp_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
935                 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
936                 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
937                 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
938 }
939
940 static void pnpacpi_encode_dma(struct pnp_dev *dev,
941                                struct acpi_resource *resource,
942                                struct resource *p)
943 {
944         struct acpi_resource_dma *dma = &resource->data.dma;
945
946         if (!pnp_resource_enabled(p)) {
947                 dma->channel_count = 0;
948                 pnp_dbg(&dev->dev, "  encode dma (%s)\n",
949                         p ? "disabled" : "missing");
950                 return;
951         }
952
953         /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
954         switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
955         case IORESOURCE_DMA_TYPEA:
956                 dma->type = ACPI_TYPE_A;
957                 break;
958         case IORESOURCE_DMA_TYPEB:
959                 dma->type = ACPI_TYPE_B;
960                 break;
961         case IORESOURCE_DMA_TYPEF:
962                 dma->type = ACPI_TYPE_F;
963                 break;
964         default:
965                 dma->type = ACPI_COMPATIBILITY;
966         }
967
968         switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
969         case IORESOURCE_DMA_8BIT:
970                 dma->transfer = ACPI_TRANSFER_8;
971                 break;
972         case IORESOURCE_DMA_8AND16BIT:
973                 dma->transfer = ACPI_TRANSFER_8_16;
974                 break;
975         default:
976                 dma->transfer = ACPI_TRANSFER_16;
977         }
978
979         dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
980         dma->channel_count = 1;
981         dma->channels[0] = p->start;
982
983         pnp_dbg(&dev->dev, "  encode dma %d "
984                 "type %#x transfer %#x master %d\n",
985                 (int) p->start, dma->type, dma->transfer, dma->bus_master);
986 }
987
988 static void pnpacpi_encode_io(struct pnp_dev *dev,
989                               struct acpi_resource *resource,
990                               struct resource *p)
991 {
992         struct acpi_resource_io *io = &resource->data.io;
993
994         if (pnp_resource_enabled(p)) {
995                 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
996                 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
997                     ACPI_DECODE_16 : ACPI_DECODE_10;
998                 io->minimum = p->start;
999                 io->maximum = p->end;
1000                 io->alignment = 0;      /* Correct? */
1001                 io->address_length = p->end - p->start + 1;
1002         } else {
1003                 io->minimum = 0;
1004                 io->address_length = 0;
1005         }
1006
1007         pnp_dbg(&dev->dev, "  encode io %#x-%#x decode %#x\n", io->minimum,
1008                 io->minimum + io->address_length - 1, io->io_decode);
1009 }
1010
1011 static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
1012                                     struct acpi_resource *resource,
1013                                     struct resource *p)
1014 {
1015         struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
1016
1017         if (pnp_resource_enabled(p)) {
1018                 fixed_io->address = p->start;
1019                 fixed_io->address_length = p->end - p->start + 1;
1020         } else {
1021                 fixed_io->address = 0;
1022                 fixed_io->address_length = 0;
1023         }
1024
1025         pnp_dbg(&dev->dev, "  encode fixed_io %#x-%#x\n", fixed_io->address,
1026                 fixed_io->address + fixed_io->address_length - 1);
1027 }
1028
1029 static void pnpacpi_encode_mem24(struct pnp_dev *dev,
1030                                  struct acpi_resource *resource,
1031                                  struct resource *p)
1032 {
1033         struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
1034
1035         if (pnp_resource_enabled(p)) {
1036                 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
1037                 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1038                     ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1039                 memory24->minimum = p->start;
1040                 memory24->maximum = p->end;
1041                 memory24->alignment = 0;
1042                 memory24->address_length = p->end - p->start + 1;
1043         } else {
1044                 memory24->minimum = 0;
1045                 memory24->address_length = 0;
1046         }
1047
1048         pnp_dbg(&dev->dev, "  encode mem24 %#x-%#x write_protect %#x\n",
1049                 memory24->minimum,
1050                 memory24->minimum + memory24->address_length - 1,
1051                 memory24->write_protect);
1052 }
1053
1054 static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1055                                  struct acpi_resource *resource,
1056                                  struct resource *p)
1057 {
1058         struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1059
1060         if (pnp_resource_enabled(p)) {
1061                 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1062                     ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1063                 memory32->minimum = p->start;
1064                 memory32->maximum = p->end;
1065                 memory32->alignment = 0;
1066                 memory32->address_length = p->end - p->start + 1;
1067         } else {
1068                 memory32->minimum = 0;
1069                 memory32->alignment = 0;
1070         }
1071
1072         pnp_dbg(&dev->dev, "  encode mem32 %#x-%#x write_protect %#x\n",
1073                 memory32->minimum,
1074                 memory32->minimum + memory32->address_length - 1,
1075                 memory32->write_protect);
1076 }
1077
1078 static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1079                                        struct acpi_resource *resource,
1080                                        struct resource *p)
1081 {
1082         struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1083
1084         if (pnp_resource_enabled(p)) {
1085                 fixed_memory32->write_protect =
1086                     p->flags & IORESOURCE_MEM_WRITEABLE ?
1087                     ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1088                 fixed_memory32->address = p->start;
1089                 fixed_memory32->address_length = p->end - p->start + 1;
1090         } else {
1091                 fixed_memory32->address = 0;
1092                 fixed_memory32->address_length = 0;
1093         }
1094
1095         pnp_dbg(&dev->dev, "  encode fixed_mem32 %#x-%#x write_protect %#x\n",
1096                 fixed_memory32->address,
1097                 fixed_memory32->address + fixed_memory32->address_length - 1,
1098                 fixed_memory32->write_protect);
1099 }
1100
1101 int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
1102 {
1103         int i = 0;
1104         /* pnpacpi_build_resource_template allocates extra mem */
1105         int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1106         struct acpi_resource *resource = buffer->pointer;
1107         int port = 0, irq = 0, dma = 0, mem = 0;
1108
1109         pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
1110         while (i < res_cnt) {
1111                 switch (resource->type) {
1112                 case ACPI_RESOURCE_TYPE_IRQ:
1113                         pnpacpi_encode_irq(dev, resource,
1114                                pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1115                         irq++;
1116                         break;
1117
1118                 case ACPI_RESOURCE_TYPE_DMA:
1119                         pnpacpi_encode_dma(dev, resource,
1120                                 pnp_get_resource(dev, IORESOURCE_DMA, dma));
1121                         dma++;
1122                         break;
1123                 case ACPI_RESOURCE_TYPE_IO:
1124                         pnpacpi_encode_io(dev, resource,
1125                                 pnp_get_resource(dev, IORESOURCE_IO, port));
1126                         port++;
1127                         break;
1128                 case ACPI_RESOURCE_TYPE_FIXED_IO:
1129                         pnpacpi_encode_fixed_io(dev, resource,
1130                                 pnp_get_resource(dev, IORESOURCE_IO, port));
1131                         port++;
1132                         break;
1133                 case ACPI_RESOURCE_TYPE_MEMORY24:
1134                         pnpacpi_encode_mem24(dev, resource,
1135                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1136                         mem++;
1137                         break;
1138                 case ACPI_RESOURCE_TYPE_MEMORY32:
1139                         pnpacpi_encode_mem32(dev, resource,
1140                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1141                         mem++;
1142                         break;
1143                 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1144                         pnpacpi_encode_fixed_mem32(dev, resource,
1145                                 pnp_get_resource(dev, IORESOURCE_MEM, mem));
1146                         mem++;
1147                         break;
1148                 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1149                         pnpacpi_encode_ext_irq(dev, resource,
1150                                 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1151                         irq++;
1152                         break;
1153                 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1154                 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1155                 case ACPI_RESOURCE_TYPE_VENDOR:
1156                 case ACPI_RESOURCE_TYPE_END_TAG:
1157                 case ACPI_RESOURCE_TYPE_ADDRESS16:
1158                 case ACPI_RESOURCE_TYPE_ADDRESS32:
1159                 case ACPI_RESOURCE_TYPE_ADDRESS64:
1160                 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1161                 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1162                 default:        /* other type */
1163                         dev_warn(&dev->dev, "can't encode unknown resource "
1164                                  "type %d\n", resource->type);
1165                         return -EINVAL;
1166                 }
1167                 resource++;
1168                 i++;
1169         }
1170         return 0;
1171 }