blob: ef3a2cd3a7a0ac80a188cfb5cacbda03373cf3e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -06006 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
Bob Moore50eca3e2005-09-30 19:03:00 -04008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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>
Bjorn Helgaas02d83b52008-04-28 16:34:28 -060026#include <linux/pnp.h>
27#include "../base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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 */
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060039static int irq_flags(int triggering, int polarity, int shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060041 int flags;
42
Bob Moore50eca3e2005-09-30 19:03:00 -040043 if (triggering == ACPI_LEVEL_SENSITIVE) {
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050044 if (polarity == ACPI_ACTIVE_LOW)
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060045 flags = IORESOURCE_IRQ_LOWLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 else
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060047 flags = IORESOURCE_IRQ_HIGHLEVEL;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070048 } else {
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050049 if (polarity == ACPI_ACTIVE_LOW)
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060050 flags = IORESOURCE_IRQ_LOWEDGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 else
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060052 flags = IORESOURCE_IRQ_HIGHEDGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060054
Bjorn Helgaasa9932732008-06-09 16:52:05 -070055 if (shareable == ACPI_SHARED)
Bjorn Helgaascd7ec922008-04-28 16:34:02 -060056 flags |= IORESOURCE_IRQ_SHAREABLE;
57
58 return flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070061static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
Bjorn Helgaasa9932732008-06-09 16:52:05 -070062 int *polarity, int *shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070064 switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
65 IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 case IORESOURCE_IRQ_LOWLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040067 *triggering = ACPI_LEVEL_SENSITIVE;
68 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 break;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050070 case IORESOURCE_IRQ_HIGHLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040071 *triggering = ACPI_LEVEL_SENSITIVE;
72 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 break;
74 case IORESOURCE_IRQ_LOWEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040075 *triggering = ACPI_EDGE_SENSITIVE;
76 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 break;
78 case IORESOURCE_IRQ_HIGHEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040079 *triggering = ACPI_EDGE_SENSITIVE;
80 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 break;
Bjorn Helgaase9fe9e12008-06-09 16:52:04 -070082 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
Bjorn Helgaasa9932732008-06-09 16:52:05 -070089
90 if (flags & IORESOURCE_IRQ_SHAREABLE)
91 *shareable = ACPI_SHARED;
92 else
93 *shareable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -060096static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070097 u32 gsi, int triggering,
98 int polarity, int shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600100 int irq, flags;
Shaohua Li61fd47e2007-11-17 01:05:28 -0500101 int p, t;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400102
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600103 if (!valid_IRQ(gsi)) {
104 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400105 return;
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600106 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400107
Shaohua Li61fd47e2007-11-17 01:05:28 -0500108 /*
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) {
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600118 dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
Shaohua Li61fd47e2007-11-17 01:05:28 -0500119 gsi, t ? "edge":"level", p ? "low":"high");
120 triggering = t;
121 polarity = p;
122 }
123 }
124
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600125 flags = irq_flags(triggering, polarity, shareable);
Yinghai Lua2f809b2009-04-27 18:01:20 -0700126 irq = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600127 if (irq >= 0)
128 pcibios_penalize_isa_irq(irq, 1);
129 else
130 flags |= IORESOURCE_DISABLED;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400131
Bjorn Helgaasdbddd032008-04-28 16:34:34 -0600132 pnp_add_irq_resource(dev, irq, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600135static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
136 int transfer)
Michael Karcher362ea082007-03-08 23:29:29 -0500137{
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;
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600158 dev_err(&dev->dev, "invalid DMA type %d\n", type);
Michael Karcher362ea082007-03-08 23:29:29 -0500159 }
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;
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600173 dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
Michael Karcher362ea082007-03-08 23:29:29 -0500174 }
175
176 return flags;
177}
178
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600179static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
180 u64 len, int io_decode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600182 int flags = 0;
183 u64 end = start + len - 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700184
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600185 if (io_decode == ACPI_DECODE_16)
Bjorn Helgaas08c9f262008-06-27 16:57:03 -0600186 flags |= IORESOURCE_IO_16BIT_ADDR;
Bjorn Helgaascc8c2e32008-04-28 16:34:36 -0600187 if (len == 0 || end >= 0x10003)
188 flags |= IORESOURCE_DISABLED;
189
190 pnp_add_io_resource(dev, start, end, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600193/*
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 */
200static 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
206static 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
235static 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
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600248static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600249 u64 start, u64 len,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700250 int write_protect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600252 int flags = 0;
253 u64 end = start + len - 1;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700254
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600255 if (len == 0)
256 flags |= IORESOURCE_DISABLED;
257 if (write_protect == ACPI_READ_WRITE_MEMORY)
258 flags |= IORESOURCE_MEM_WRITEABLE;
Michael Karcher362ea082007-03-08 23:29:29 -0500259
Bjorn Helgaasd6180f32008-04-28 16:34:37 -0600260 pnp_add_mem_resource(dev, start, end, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600263static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700264 struct acpi_resource *res)
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500265{
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)) {
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600271 dev_warn(&dev->dev, "failed to convert resource type %d\n",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700272 res->type);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500273 return;
274 }
275
matthieu castet2b8de5f2006-08-05 12:15:12 -0700276 if (p->producer_consumer == ACPI_PRODUCER)
277 return;
278
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500279 if (p->resource_type == ACPI_MEMORY_RANGE)
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600280 pnpacpi_parse_allocated_memresource(dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700281 p->minimum, p->address_length,
282 p->info.mem.write_protect);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500283 else if (p->resource_type == ACPI_IO_RANGE)
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600284 pnpacpi_parse_allocated_ioresource(dev,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700285 p->minimum, p->address_length,
286 p->granularity == 0xfff ? ACPI_DECODE_10 :
287 ACPI_DECODE_16);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600290static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700310 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600312 struct pnp_dev *dev = data;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600313 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;
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600317 struct acpi_resource_vendor_typed *vendor_typed;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600318 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;
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600322 int i, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Len Browneca008c2005-09-22 00:25:18 -0400324 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400325 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400326 /*
327 * Per spec, only one interrupt per descriptor is allowed in
328 * _CRS, but some firmware violates this, so parse them all.
329 */
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600330 irq = &res->data.irq;
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600331 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 break;
355
Bob Moore50eca3e2005-09-30 19:03:00 -0400356 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600357 dma = &res->data.dma;
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600358 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600359 flags = dma_flags(dev, dma->type, dma->bus_master,
Bjorn Helgaasdc16f5f2008-04-28 16:34:35 -0600360 dma->transfer);
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600361 else
362 flags = IORESOURCE_DISABLED;
363 pnp_add_dma_resource(dev, dma->channels[0], flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
Len Brown0af58532006-01-20 01:11:37 -0500365
Bob Moore50eca3e2005-09-30 19:03:00 -0400366 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600367 io = &res->data.io;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600368 pnpacpi_parse_allocated_ioresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600369 io->minimum,
370 io->address_length,
371 io->io_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 break;
Len Brown0af58532006-01-20 01:11:37 -0500373
374 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
375 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
376 break;
377
Bob Moore50eca3e2005-09-30 19:03:00 -0400378 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600379 fixed_io = &res->data.fixed_io;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600380 pnpacpi_parse_allocated_ioresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600381 fixed_io->address,
382 fixed_io->address_length,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700383 ACPI_DECODE_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
Len Brown0af58532006-01-20 01:11:37 -0500385
386 case ACPI_RESOURCE_TYPE_VENDOR:
Bjorn Helgaas40ab4f42008-06-27 16:57:19 -0600387 vendor_typed = &res->data.vendor_typed;
388 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
Len Brown0af58532006-01-20 01:11:37 -0500389 break;
390
391 case ACPI_RESOURCE_TYPE_END_TAG:
392 break;
393
Bob Moore50eca3e2005-09-30 19:03:00 -0400394 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600395 memory24 = &res->data.memory24;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600396 pnpacpi_parse_allocated_memresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600397 memory24->minimum,
398 memory24->address_length,
399 memory24->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400401 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600402 memory32 = &res->data.memory32;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600403 pnpacpi_parse_allocated_memresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600404 memory32->minimum,
405 memory32->address_length,
406 memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400408 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600409 fixed_memory32 = &res->data.fixed_memory32;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600410 pnpacpi_parse_allocated_memresource(dev,
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600411 fixed_memory32->address,
412 fixed_memory32->address_length,
413 fixed_memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400415 case ACPI_RESOURCE_TYPE_ADDRESS16:
Bob Moore50eca3e2005-09-30 19:03:00 -0400416 case ACPI_RESOURCE_TYPE_ADDRESS32:
Bob Moore50eca3e2005-09-30 19:03:00 -0400417 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600418 pnpacpi_parse_allocated_address_space(dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
Len Brown0af58532006-01-20 01:11:37 -0500420
421 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600422 pnpacpi_parse_allocated_ext_address_space(dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 break;
Len Brown0af58532006-01-20 01:11:37 -0500424
425 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600426 extended_irq = &res->data.extended_irq;
matthieu castet2b8de5f2006-08-05 12:15:12 -0700427
Bjorn Helgaas5acf9142008-06-27 16:56:58 -0600428 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 }
Len Brown0af58532006-01-20 01:11:37 -0500451 }
452 break;
453
454 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
455 break;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 default:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600458 dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
459 res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return AE_ERROR;
461 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return AE_OK;
464}
465
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600466int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600468 acpi_handle handle = dev->data;
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600469 acpi_status status;
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600470
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600471 pnp_dbg(&dev->dev, "parse allocated resources\n");
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600472
Bjorn Helgaasf4490002008-04-28 16:34:09 -0600473 pnp_init_resources(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600475 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
476 pnpacpi_allocated_resource, dev);
477
478 if (ACPI_FAILURE(status)) {
479 if (status != AE_NOT_FOUND)
480 dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
481 return -EPERM;
482 }
483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600486static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600487 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800488 struct acpi_resource_dma *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600491 unsigned char map = 0, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Bob Moore50eca3e2005-09-30 19:03:00 -0400493 if (p->channel_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700496 for (i = 0; i < p->channel_count; i++)
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600497 map |= 1 << p->channels[i];
Michael Karcher362ea082007-03-08 23:29:29 -0500498
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600499 flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600500 pnp_register_dma_resource(dev, option_flags, map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600503static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600504 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800505 struct acpi_resource_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600508 pnp_irq_mask_t map;
509 unsigned char flags;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500510
Bob Moore50eca3e2005-09-30 19:03:00 -0400511 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600514 bitmap_zero(map.bits, PNP_IRQ_NR);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700515 for (i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (p->interrupts[i])
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600517 __set_bit(p->interrupts[i], map.bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600519 flags = irq_flags(p->triggering, p->polarity, p->sharable);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600520 pnp_register_irq_resource(dev, option_flags, &map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600523static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600524 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800525 struct acpi_resource_extended_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 int i;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600528 pnp_irq_mask_t map;
529 unsigned char flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Bob Moore50eca3e2005-09-30 19:03:00 -0400531 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600534 bitmap_zero(map.bits, PNP_IRQ_NR);
Bjorn Helgaasfe2cf592008-06-27 16:57:12 -0600535 for (i = 0; i < p->interrupt_count; i++) {
536 if (p->interrupts[i]) {
537 if (p->interrupts[i] < PNP_IRQ_NR)
538 __set_bit(p->interrupts[i], map.bits);
539 else
540 dev_err(&dev->dev, "ignoring IRQ %d option "
541 "(too large for %d entry bitmap)\n",
542 p->interrupts[i], PNP_IRQ_NR);
543 }
544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600546 flags = irq_flags(p->triggering, p->polarity, p->sharable);
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600547 pnp_register_irq_resource(dev, option_flags, &map, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600550static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600551 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800552 struct acpi_resource_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600554 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Bob Moore50eca3e2005-09-30 19:03:00 -0400556 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600558
559 if (io->io_decode == ACPI_DECODE_16)
560 flags = IORESOURCE_IO_16BIT_ADDR;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600561 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600562 io->alignment, io->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600565static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600566 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800567 struct acpi_resource_fixed_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Bob Moore50eca3e2005-09-30 19:03:00 -0400569 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600571
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600572 pnp_register_port_resource(dev, option_flags, io->address, io->address,
573 0, io->address_length, IORESOURCE_IO_FIXED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600576static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600577 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800578 struct acpi_resource_memory24 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600580 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Bob Moore50eca3e2005-09-30 19:03:00 -0400582 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600585 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
586 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600587 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600588 p->alignment, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600591static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600592 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800593 struct acpi_resource_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600595 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Bob Moore50eca3e2005-09-30 19:03:00 -0400597 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600600 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
601 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600602 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600603 p->alignment, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600606static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600607 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800608 struct acpi_resource_fixed_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600610 unsigned char flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Bob Moore50eca3e2005-09-30 19:03:00 -0400612 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600615 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
616 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600617 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600618 0, p->address_length, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600621static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600622 unsigned int option_flags,
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800623 struct acpi_resource *r)
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400624{
625 struct acpi_resource_address64 addr, *p = &addr;
626 acpi_status status;
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600627 unsigned char flags = 0;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400628
629 status = acpi_resource_to_address64(r, p);
Bjorn Helgaas958a1fd2008-08-19 16:53:21 -0600630 if (ACPI_FAILURE(status)) {
631 dev_warn(&dev->dev, "can't convert resource type %d\n",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700632 r->type);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400633 return;
634 }
635
636 if (p->address_length == 0)
637 return;
638
639 if (p->resource_type == ACPI_MEMORY_RANGE) {
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600640 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
641 flags = IORESOURCE_MEM_WRITEABLE;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600642 pnp_register_mem_resource(dev, option_flags, p->minimum,
643 p->minimum, 0, p->address_length,
644 flags);
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600645 } else if (p->resource_type == ACPI_IO_RANGE)
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600646 pnp_register_port_resource(dev, option_flags, p->minimum,
647 p->minimum, 0, p->address_length,
Bjorn Helgaasc2275362008-06-27 16:57:11 -0600648 IORESOURCE_IO_FIXED);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400649}
650
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600651static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
652 unsigned int option_flags,
653 struct acpi_resource *r)
654{
655 struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
656 unsigned char flags = 0;
657
658 if (p->address_length == 0)
659 return;
660
661 if (p->resource_type == ACPI_MEMORY_RANGE) {
662 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
663 flags = IORESOURCE_MEM_WRITEABLE;
664 pnp_register_mem_resource(dev, option_flags, p->minimum,
665 p->minimum, 0, p->address_length,
666 flags);
667 } else if (p->resource_type == ACPI_IO_RANGE)
668 pnp_register_port_resource(dev, option_flags, p->minimum,
669 p->minimum, 0, p->address_length,
670 IORESOURCE_IO_FIXED);
671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673struct acpipnp_parse_option_s {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct pnp_dev *dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600675 unsigned int option_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676};
677
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800678static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
679 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Bjorn Helgaase2a1a6f2008-06-27 16:57:15 -0600681 int priority;
Bjorn Helgaas4721a4c2007-08-15 10:32:10 -0600682 struct acpipnp_parse_option_s *parse_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct pnp_dev *dev = parse_data->dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600684 unsigned int option_flags = parse_data->option_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Len Browneca008c2005-09-22 00:25:18 -0400686 switch (res->type) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700687 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600688 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700689 break;
690
691 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600692 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700693 break;
694
695 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
696 switch (res->data.start_dpf.compatibility_priority) {
697 case ACPI_GOOD_CONFIGURATION:
698 priority = PNP_RES_PRIORITY_PREFERRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 break;
Len Brown0af58532006-01-20 01:11:37 -0500700
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700701 case ACPI_ACCEPTABLE_CONFIGURATION:
702 priority = PNP_RES_PRIORITY_ACCEPTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
Len Brown0af58532006-01-20 01:11:37 -0500704
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700705 case ACPI_SUB_OPTIMAL_CONFIGURATION:
706 priority = PNP_RES_PRIORITY_FUNCTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 default:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700709 priority = PNP_RES_PRIORITY_INVALID;
710 break;
711 }
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600712 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700713 break;
714
715 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600716 parse_data->option_flags = 0;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700717 break;
718
719 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600720 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700721 break;
722
723 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600724 pnpacpi_parse_fixed_port_option(dev, option_flags,
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600725 &res->data.fixed_io);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700726 break;
727
728 case ACPI_RESOURCE_TYPE_VENDOR:
729 case ACPI_RESOURCE_TYPE_END_TAG:
730 break;
731
732 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600733 pnpacpi_parse_mem24_option(dev, option_flags,
734 &res->data.memory24);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700735 break;
736
737 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600738 pnpacpi_parse_mem32_option(dev, option_flags,
739 &res->data.memory32);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700740 break;
741
742 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600743 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700744 &res->data.fixed_memory32);
745 break;
746
747 case ACPI_RESOURCE_TYPE_ADDRESS16:
748 case ACPI_RESOURCE_TYPE_ADDRESS32:
749 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600750 pnpacpi_parse_address_option(dev, option_flags, res);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700751 break;
752
753 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600754 pnpacpi_parse_ext_address_option(dev, option_flags, res);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700755 break;
756
757 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600758 pnpacpi_parse_ext_irq_option(dev, option_flags,
Bjorn Helgaasc1caf062008-04-28 16:34:04 -0600759 &res->data.extended_irq);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700760 break;
761
762 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
763 break;
764
765 default:
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -0600766 dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
767 res->type);
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700768 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return AE_OK;
772}
773
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600774int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -0600776 acpi_handle handle = dev->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 acpi_status status;
778 struct acpipnp_parse_option_s parse_data;
779
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600780 pnp_dbg(&dev->dev, "parse resource options\n");
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 parse_data.dev = dev;
Bjorn Helgaas1f32ca32008-06-27 16:57:17 -0600783 parse_data.option_flags = 0;
784
Bob Moore50eca3e2005-09-30 19:03:00 -0400785 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700786 pnpacpi_option_resource, &parse_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600788 if (ACPI_FAILURE(status)) {
789 if (status != AE_NOT_FOUND)
790 dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
791 return -EPERM;
792 }
793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500796static int pnpacpi_supported_resource(struct acpi_resource *res)
797{
798 switch (res->type) {
799 case ACPI_RESOURCE_TYPE_IRQ:
800 case ACPI_RESOURCE_TYPE_DMA:
801 case ACPI_RESOURCE_TYPE_IO:
802 case ACPI_RESOURCE_TYPE_FIXED_IO:
803 case ACPI_RESOURCE_TYPE_MEMORY24:
804 case ACPI_RESOURCE_TYPE_MEMORY32:
805 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
806 case ACPI_RESOURCE_TYPE_ADDRESS16:
807 case ACPI_RESOURCE_TYPE_ADDRESS32:
808 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas8cb24c82009-05-21 15:49:59 -0600809 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500810 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
811 return 1;
812 }
813 return 0;
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816/*
817 * Set resource
818 */
819static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700820 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Bjorn Helgaas4721a4c2007-08-15 10:32:10 -0600822 int *res_cnt = data;
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500823
824 if (pnpacpi_supported_resource(res))
825 (*res_cnt)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return AE_OK;
827}
828
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500829static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Bjorn Helgaas4721a4c2007-08-15 10:32:10 -0600831 struct acpi_resource **resource = data;
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500832
833 if (pnpacpi_supported_resource(res)) {
Len Browneca008c2005-09-22 00:25:18 -0400834 (*resource)->type = res->type;
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500835 (*resource)->length = sizeof(struct acpi_resource);
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700836 if (res->type == ACPI_RESOURCE_TYPE_IRQ)
837 (*resource)->data.irq.descriptor_length =
838 res->data.irq.descriptor_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 (*resource)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 return AE_OK;
843}
844
Bjorn Helgaascdef6252008-04-28 16:34:03 -0600845int pnpacpi_build_resource_template(struct pnp_dev *dev,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700846 struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Bjorn Helgaascdef6252008-04-28 16:34:03 -0600848 acpi_handle handle = dev->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct acpi_resource *resource;
850 int res_cnt = 0;
851 acpi_status status;
852
Bob Moore50eca3e2005-09-30 19:03:00 -0400853 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700854 pnpacpi_count_resources, &res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (ACPI_FAILURE(status)) {
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600856 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -EINVAL;
858 }
859 if (!res_cnt)
860 return -EINVAL;
861 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800862 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!buffer->pointer)
864 return -ENOMEM;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 resource = (struct acpi_resource *)buffer->pointer;
Bob Moore50eca3e2005-09-30 19:03:00 -0400867 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700868 pnpacpi_type_resources, &resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (ACPI_FAILURE(status)) {
870 kfree(buffer->pointer);
Bjorn Helgaasd152cf52008-04-28 16:34:39 -0600871 dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return -EINVAL;
873 }
874 /* resource will pointer the end resource now */
Bob Moore50eca3e2005-09-30 19:03:00 -0400875 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 return 0;
878}
879
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600880static void pnpacpi_encode_irq(struct pnp_dev *dev,
881 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700882 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600884 struct acpi_resource_irq *irq = &resource->data.irq;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700885 int triggering, polarity, shareable;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500886
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600887 if (!pnp_resource_enabled(p)) {
888 irq->interrupt_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600889 pnp_dbg(&dev->dev, " encode irq (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600890 p ? "disabled" : "missing");
891 return;
892 }
893
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700894 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600895 irq->triggering = triggering;
896 irq->polarity = polarity;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700897 irq->sharable = shareable;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600898 irq->interrupt_count = 1;
899 irq->interrupts[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600900
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600901 pnp_dbg(&dev->dev, " encode irq %d %s %s %s (%d-byte descriptor)\n",
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700902 (int) p->start,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600903 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
904 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
Bjorn Helgaas36d872a2008-06-09 16:52:06 -0700905 irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
906 irq->descriptor_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600909static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
910 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700911 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600913 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700914 int triggering, polarity, shareable;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500915
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600916 if (!pnp_resource_enabled(p)) {
917 extended_irq->interrupt_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600918 pnp_dbg(&dev->dev, " encode extended irq (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600919 p ? "disabled" : "missing");
920 return;
921 }
922
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700923 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600924 extended_irq->producer_consumer = ACPI_CONSUMER;
925 extended_irq->triggering = triggering;
926 extended_irq->polarity = polarity;
Bjorn Helgaasa9932732008-06-09 16:52:05 -0700927 extended_irq->sharable = shareable;
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600928 extended_irq->interrupt_count = 1;
929 extended_irq->interrupts[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600930
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600931 pnp_dbg(&dev->dev, " encode irq %d %s %s %s\n", (int) p->start,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600932 triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
933 polarity == ACPI_ACTIVE_LOW ? "low" : "high",
934 extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600937static void pnpacpi_encode_dma(struct pnp_dev *dev,
938 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700939 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600941 struct acpi_resource_dma *dma = &resource->data.dma;
942
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600943 if (!pnp_resource_enabled(p)) {
944 dma->channel_count = 0;
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600945 pnp_dbg(&dev->dev, " encode dma (%s)\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600946 p ? "disabled" : "missing");
947 return;
948 }
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200951 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700952 case IORESOURCE_DMA_TYPEA:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600953 dma->type = ACPI_TYPE_A;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700954 break;
955 case IORESOURCE_DMA_TYPEB:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600956 dma->type = ACPI_TYPE_B;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700957 break;
958 case IORESOURCE_DMA_TYPEF:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600959 dma->type = ACPI_TYPE_F;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700960 break;
961 default:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600962 dma->type = ACPI_COMPATIBILITY;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200963 }
964
965 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700966 case IORESOURCE_DMA_8BIT:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600967 dma->transfer = ACPI_TRANSFER_8;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700968 break;
969 case IORESOURCE_DMA_8AND16BIT:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600970 dma->transfer = ACPI_TRANSFER_8_16;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700971 break;
972 default:
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600973 dma->transfer = ACPI_TRANSFER_16;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200974 }
975
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600976 dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
977 dma->channel_count = 1;
978 dma->channels[0] = p->start;
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600979
Bjorn Helgaas2f534322008-08-19 16:53:47 -0600980 pnp_dbg(&dev->dev, " encode dma %d "
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600981 "type %#x transfer %#x master %d\n",
982 (int) p->start, dma->type, dma->transfer, dma->bus_master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Bjorn Helgaas72dcc882008-04-28 16:34:07 -0600985static void pnpacpi_encode_io(struct pnp_dev *dev,
986 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700987 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Bjorn Helgaas9570a202008-04-28 16:34:00 -0600989 struct acpi_resource_io *io = &resource->data.io;
990
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600991 if (pnp_resource_enabled(p)) {
992 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
Bjorn Helgaas08c9f262008-06-27 16:57:03 -0600993 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -0600994 ACPI_DECODE_16 : ACPI_DECODE_10;
995 io->minimum = p->start;
996 io->maximum = p->end;
997 io->alignment = 0; /* Correct? */
998 io->address_length = p->end - p->start + 1;
999 } else {
1000 io->minimum = 0;
1001 io->address_length = 0;
1002 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001003
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001004 pnp_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001005 io->minimum + io->address_length - 1, io->io_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001008static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
1009 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001010 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001012 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
1013
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001014 if (pnp_resource_enabled(p)) {
1015 fixed_io->address = p->start;
1016 fixed_io->address_length = p->end - p->start + 1;
1017 } else {
1018 fixed_io->address = 0;
1019 fixed_io->address_length = 0;
1020 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001021
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001022 pnp_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001023 fixed_io->address + fixed_io->address_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001026static void pnpacpi_encode_mem24(struct pnp_dev *dev,
1027 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001028 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001030 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
1031
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001032 if (pnp_resource_enabled(p)) {
1033 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
1034 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1035 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1036 memory24->minimum = p->start;
1037 memory24->maximum = p->end;
1038 memory24->alignment = 0;
1039 memory24->address_length = p->end - p->start + 1;
1040 } else {
1041 memory24->minimum = 0;
1042 memory24->address_length = 0;
1043 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001044
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001045 pnp_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001046 memory24->minimum,
1047 memory24->minimum + memory24->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001048 memory24->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001051static void pnpacpi_encode_mem32(struct pnp_dev *dev,
1052 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001053 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001055 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
1056
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001057 if (pnp_resource_enabled(p)) {
1058 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
1059 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1060 memory32->minimum = p->start;
1061 memory32->maximum = p->end;
1062 memory32->alignment = 0;
1063 memory32->address_length = p->end - p->start + 1;
1064 } else {
1065 memory32->minimum = 0;
1066 memory32->alignment = 0;
1067 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001068
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001069 pnp_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001070 memory32->minimum,
1071 memory32->minimum + memory32->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001072 memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001075static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
1076 struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001077 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Bjorn Helgaas9570a202008-04-28 16:34:00 -06001079 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
1080
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001081 if (pnp_resource_enabled(p)) {
1082 fixed_memory32->write_protect =
1083 p->flags & IORESOURCE_MEM_WRITEABLE ?
1084 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
1085 fixed_memory32->address = p->start;
1086 fixed_memory32->address_length = p->end - p->start + 1;
1087 } else {
1088 fixed_memory32->address = 0;
1089 fixed_memory32->address_length = 0;
1090 }
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001091
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001092 pnp_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
Bjorn Helgaasaee3ad82008-06-27 16:56:57 -06001093 fixed_memory32->address,
1094 fixed_memory32->address + fixed_memory32->address_length - 1,
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001095 fixed_memory32->write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Bjorn Helgaas4ab55d82008-04-28 16:34:06 -06001098int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 int i = 0;
1101 /* pnpacpi_build_resource_template allocates extra mem */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001102 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
Bjorn Helgaas4721a4c2007-08-15 10:32:10 -06001103 struct acpi_resource *resource = buffer->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int port = 0, irq = 0, dma = 0, mem = 0;
1105
Bjorn Helgaas2f534322008-08-19 16:53:47 -06001106 pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 while (i < res_cnt) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001108 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -04001109 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001110 pnpacpi_encode_irq(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001111 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 irq++;
1113 break;
1114
Bob Moore50eca3e2005-09-30 19:03:00 -04001115 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001116 pnpacpi_encode_dma(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001117 pnp_get_resource(dev, IORESOURCE_DMA, dma));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001118 dma++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001120 case ACPI_RESOURCE_TYPE_IO:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001121 pnpacpi_encode_io(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001122 pnp_get_resource(dev, IORESOURCE_IO, port));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001123 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001125 case ACPI_RESOURCE_TYPE_FIXED_IO:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001126 pnpacpi_encode_fixed_io(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001127 pnp_get_resource(dev, IORESOURCE_IO, port));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001128 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001130 case ACPI_RESOURCE_TYPE_MEMORY24:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001131 pnpacpi_encode_mem24(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001132 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001133 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001135 case ACPI_RESOURCE_TYPE_MEMORY32:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001136 pnpacpi_encode_mem32(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001137 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001138 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
Bob Moore50eca3e2005-09-30 19:03:00 -04001140 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001141 pnpacpi_encode_fixed_mem32(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001142 pnp_get_resource(dev, IORESOURCE_MEM, mem));
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001143 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
Len Brown0af58532006-01-20 01:11:37 -05001145 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Bjorn Helgaas72dcc882008-04-28 16:34:07 -06001146 pnpacpi_encode_ext_irq(dev, resource,
Bjorn Helgaas7e2cf312008-04-28 16:34:24 -06001147 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
Len Brown0af58532006-01-20 01:11:37 -05001148 irq++;
1149 break;
1150 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1151 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
1152 case ACPI_RESOURCE_TYPE_VENDOR:
1153 case ACPI_RESOURCE_TYPE_END_TAG:
1154 case ACPI_RESOURCE_TYPE_ADDRESS16:
1155 case ACPI_RESOURCE_TYPE_ADDRESS32:
1156 case ACPI_RESOURCE_TYPE_ADDRESS64:
1157 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
1158 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -07001159 default: /* other type */
Bjorn Helgaasaf11cb22008-04-28 16:34:12 -06001160 dev_warn(&dev->dev, "can't encode unknown resource "
1161 "type %d\n", resource->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -EINVAL;
1163 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -05001164 resource++;
1165 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 return 0;
1168}