blob: 3c5eb374adf8bbcc0a1d0a7dbb85ab6f55587a7d [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>
Bob Moore50eca3e2005-09-30 19:03:00 -04006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/kernel.h>
22#include <linux/acpi.h>
23#include <linux/pci.h>
24#include "pnpacpi.h"
25
26#ifdef CONFIG_IA64
27#define valid_IRQ(i) (1)
28#else
29#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
30#endif
31
32/*
33 * Allocated Resources
34 */
Bob Moore50eca3e2005-09-30 19:03:00 -040035static int irq_flags(int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Bob Moore50eca3e2005-09-30 19:03:00 -040037 if (triggering == ACPI_LEVEL_SENSITIVE) {
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050038 if (polarity == ACPI_ACTIVE_LOW)
Bjorn Helgaas4cec0862007-08-15 10:32:09 -060039 return IORESOURCE_IRQ_LOWLEVEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 else
Bjorn Helgaas4cec0862007-08-15 10:32:09 -060041 return IORESOURCE_IRQ_HIGHLEVEL;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070042 } else {
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050043 if (polarity == ACPI_ACTIVE_LOW)
Bjorn Helgaas4cec0862007-08-15 10:32:09 -060044 return IORESOURCE_IRQ_LOWEDGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 else
Bjorn Helgaas4cec0862007-08-15 10:32:09 -060046 return IORESOURCE_IRQ_HIGHEDGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Bob Moore50eca3e2005-09-30 19:03:00 -040050static void decode_irq_flags(int flag, int *triggering, int *polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 switch (flag) {
53 case IORESOURCE_IRQ_LOWLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040054 *triggering = ACPI_LEVEL_SENSITIVE;
55 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 break;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050057 case IORESOURCE_IRQ_HIGHLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040058 *triggering = ACPI_LEVEL_SENSITIVE;
59 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 break;
61 case IORESOURCE_IRQ_LOWEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040062 *triggering = ACPI_EDGE_SENSITIVE;
63 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 break;
65 case IORESOURCE_IRQ_HIGHEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040066 *triggering = ACPI_EDGE_SENSITIVE;
67 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
69 }
70}
71
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -070072static void pnpacpi_parse_allocated_irqresource(struct pnp_resource_table *res,
73 u32 gsi, int triggering,
74 int polarity, int shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 int i = 0;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040077 int irq;
Shaohua Li61fd47e2007-11-17 01:05:28 -050078 int p, t;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040079
80 if (!valid_IRQ(gsi))
81 return;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070084 i < PNP_MAX_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 i++;
Zhao Yakuia7839e92007-11-28 16:21:21 -080086 if (i >= PNP_MAX_IRQ) {
87 printk(KERN_ERR "pnpacpi: exceeded the max number of IRQ "
88 "resources: %d \n", PNP_MAX_IRQ);
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040089 return;
Zhao Yakuia7839e92007-11-28 16:21:21 -080090 }
Shaohua Li61fd47e2007-11-17 01:05:28 -050091 /*
92 * in IO-APIC mode, use overrided attribute. Two reasons:
93 * 1. BIOS bug in DSDT
94 * 2. BIOS uses IO-APIC mode Interrupt Source Override
95 */
96 if (!acpi_get_override_irq(gsi, &t, &p)) {
97 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
98 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
99
100 if (triggering != t || polarity != p) {
101 pnp_warn("IRQ %d override to %s, %s",
102 gsi, t ? "edge":"level", p ? "low":"high");
103 triggering = t;
104 polarity = p;
105 }
106 }
107
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700108 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
Michael Karcher362ea082007-03-08 23:29:29 -0500109 res->irq_resource[i].flags |= irq_flags(triggering, polarity);
Bob Moore50eca3e2005-09-30 19:03:00 -0400110 irq = acpi_register_gsi(gsi, triggering, polarity);
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400111 if (irq < 0) {
112 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
113 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400115
Bjorn Helgaasc32928c2006-07-03 00:24:10 -0700116 if (shareable)
117 res->irq_resource[i].flags |= IORESOURCE_IRQ_SHAREABLE;
118
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400119 res->irq_resource[i].start = irq;
120 res->irq_resource[i].end = irq;
121 pcibios_penalize_isa_irq(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Michael Karcher362ea082007-03-08 23:29:29 -0500124static int dma_flags(int type, int bus_master, int transfer)
125{
126 int flags = 0;
127
128 if (bus_master)
129 flags |= IORESOURCE_DMA_MASTER;
130 switch (type) {
131 case ACPI_COMPATIBILITY:
132 flags |= IORESOURCE_DMA_COMPATIBLE;
133 break;
134 case ACPI_TYPE_A:
135 flags |= IORESOURCE_DMA_TYPEA;
136 break;
137 case ACPI_TYPE_B:
138 flags |= IORESOURCE_DMA_TYPEB;
139 break;
140 case ACPI_TYPE_F:
141 flags |= IORESOURCE_DMA_TYPEF;
142 break;
143 default:
144 /* Set a default value ? */
145 flags |= IORESOURCE_DMA_COMPATIBLE;
146 pnp_err("Invalid DMA type");
147 }
148 switch (transfer) {
149 case ACPI_TRANSFER_8:
150 flags |= IORESOURCE_DMA_8BIT;
151 break;
152 case ACPI_TRANSFER_8_16:
153 flags |= IORESOURCE_DMA_8AND16BIT;
154 break;
155 case ACPI_TRANSFER_16:
156 flags |= IORESOURCE_DMA_16BIT;
157 break;
158 default:
159 /* Set a default value ? */
160 flags |= IORESOURCE_DMA_8AND16BIT;
161 pnp_err("Invalid DMA transfer type");
162 }
163
164 return flags;
165}
166
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700167static void pnpacpi_parse_allocated_dmaresource(struct pnp_resource_table *res,
168 u32 dma, int type,
169 int bus_master, int transfer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 int i = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700172
Adrian Bunk0dec63b2005-05-01 08:59:30 -0700173 while (i < PNP_MAX_DMA &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700174 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 i++;
176 if (i < PNP_MAX_DMA) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700177 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
178 res->dma_resource[i].flags |=
179 dma_flags(type, bus_master, transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (dma == -1) {
181 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
182 return;
183 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400184 res->dma_resource[i].start = dma;
185 res->dma_resource[i].end = dma;
Zhao Yakuia7839e92007-11-28 16:21:21 -0800186 } else {
187 printk(KERN_ERR "pnpacpi: exceeded the max number of DMA "
188 "resources: %d \n", PNP_MAX_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190}
191
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700192static void pnpacpi_parse_allocated_ioresource(struct pnp_resource_table *res,
193 u64 io, u64 len, int io_decode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 int i = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700198 i < PNP_MAX_PORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 i++;
200 if (i < PNP_MAX_PORT) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700201 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
Michael Karcher362ea082007-03-08 23:29:29 -0500202 if (io_decode == ACPI_DECODE_16)
203 res->port_resource[i].flags |= PNP_PORT_FLAG_16BITADDR;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700204 if (len <= 0 || (io + len - 1) >= 0x10003) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 res->port_resource[i].flags |= IORESOURCE_DISABLED;
206 return;
207 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400208 res->port_resource[i].start = io;
209 res->port_resource[i].end = io + len - 1;
Zhao Yakuia7839e92007-11-28 16:21:21 -0800210 } else {
211 printk(KERN_ERR "pnpacpi: exceeded the max number of IO "
212 "resources: %d \n", PNP_MAX_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214}
215
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700216static void pnpacpi_parse_allocated_memresource(struct pnp_resource_table *res,
217 u64 mem, u64 len,
218 int write_protect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int i = 0;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700223 (i < PNP_MAX_MEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 i++;
225 if (i < PNP_MAX_MEM) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700226 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (len <= 0) {
228 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
229 return;
230 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700231 if (write_protect == ACPI_READ_WRITE_MEMORY)
Michael Karcher362ea082007-03-08 23:29:29 -0500232 res->mem_resource[i].flags |= IORESOURCE_MEM_WRITEABLE;
233
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400234 res->mem_resource[i].start = mem;
235 res->mem_resource[i].end = mem + len - 1;
Zhao Yakuia7839e92007-11-28 16:21:21 -0800236 } else {
237 printk(KERN_ERR "pnpacpi: exceeded the max number of mem "
238 "resources: %d\n", PNP_MAX_MEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240}
241
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700242static void pnpacpi_parse_allocated_address_space(struct pnp_resource_table *res_table,
243 struct acpi_resource *res)
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500244{
245 struct acpi_resource_address64 addr, *p = &addr;
246 acpi_status status;
247
248 status = acpi_resource_to_address64(res, p);
249 if (!ACPI_SUCCESS(status)) {
250 pnp_warn("PnPACPI: failed to convert resource type %d",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700251 res->type);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500252 return;
253 }
254
matthieu castet2b8de5f2006-08-05 12:15:12 -0700255 if (p->producer_consumer == ACPI_PRODUCER)
256 return;
257
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500258 if (p->resource_type == ACPI_MEMORY_RANGE)
259 pnpacpi_parse_allocated_memresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700260 p->minimum, p->address_length,
261 p->info.mem.write_protect);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500262 else if (p->resource_type == ACPI_IO_RANGE)
263 pnpacpi_parse_allocated_ioresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700264 p->minimum, p->address_length,
265 p->granularity == 0xfff ? ACPI_DECODE_10 :
266 ACPI_DECODE_16);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700270 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600272 struct pnp_resource_table *res_table = data;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400273 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Len Browneca008c2005-09-22 00:25:18 -0400275 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400276 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400277 /*
278 * Per spec, only one interrupt per descriptor is allowed in
279 * _CRS, but some firmware violates this, so parse them all.
280 */
Bob Moore50eca3e2005-09-30 19:03:00 -0400281 for (i = 0; i < res->data.irq.interrupt_count; i++) {
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400282 pnpacpi_parse_allocated_irqresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700283 res->data.irq.interrupts[i],
284 res->data.irq.triggering,
285 res->data.irq.polarity,
286 res->data.irq.sharable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 break;
289
Bob Moore50eca3e2005-09-30 19:03:00 -0400290 case ACPI_RESOURCE_TYPE_DMA:
291 if (res->data.dma.channel_count > 0)
292 pnpacpi_parse_allocated_dmaresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700293 res->data.dma.channels[0],
294 res->data.dma.type,
295 res->data.dma.bus_master,
296 res->data.dma.transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
Len Brown0af58532006-01-20 01:11:37 -0500298
Bob Moore50eca3e2005-09-30 19:03:00 -0400299 case ACPI_RESOURCE_TYPE_IO:
300 pnpacpi_parse_allocated_ioresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700301 res->data.io.minimum,
302 res->data.io.address_length,
303 res->data.io.io_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
Len Brown0af58532006-01-20 01:11:37 -0500305
306 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
307 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
308 break;
309
Bob Moore50eca3e2005-09-30 19:03:00 -0400310 case ACPI_RESOURCE_TYPE_FIXED_IO:
311 pnpacpi_parse_allocated_ioresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700312 res->data.fixed_io.address,
313 res->data.fixed_io.address_length,
314 ACPI_DECODE_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
Len Brown0af58532006-01-20 01:11:37 -0500316
317 case ACPI_RESOURCE_TYPE_VENDOR:
318 break;
319
320 case ACPI_RESOURCE_TYPE_END_TAG:
321 break;
322
Bob Moore50eca3e2005-09-30 19:03:00 -0400323 case ACPI_RESOURCE_TYPE_MEMORY24:
324 pnpacpi_parse_allocated_memresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700325 res->data.memory24.minimum,
326 res->data.memory24.address_length,
327 res->data.memory24.write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400329 case ACPI_RESOURCE_TYPE_MEMORY32:
330 pnpacpi_parse_allocated_memresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700331 res->data.memory32.minimum,
332 res->data.memory32.address_length,
333 res->data.memory32.write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400335 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
336 pnpacpi_parse_allocated_memresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700337 res->data.fixed_memory32.address,
338 res->data.fixed_memory32.address_length,
339 res->data.fixed_memory32.write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400341 case ACPI_RESOURCE_TYPE_ADDRESS16:
Bob Moore50eca3e2005-09-30 19:03:00 -0400342 case ACPI_RESOURCE_TYPE_ADDRESS32:
Bob Moore50eca3e2005-09-30 19:03:00 -0400343 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500344 pnpacpi_parse_allocated_address_space(res_table, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 break;
Len Brown0af58532006-01-20 01:11:37 -0500346
347 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
matthieu castet2b8de5f2006-08-05 12:15:12 -0700348 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
349 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
Len Brown0af58532006-01-20 01:11:37 -0500351
352 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
matthieu castet2b8de5f2006-08-05 12:15:12 -0700353 if (res->data.extended_irq.producer_consumer == ACPI_PRODUCER)
354 return AE_OK;
355
Len Brown0af58532006-01-20 01:11:37 -0500356 for (i = 0; i < res->data.extended_irq.interrupt_count; i++) {
357 pnpacpi_parse_allocated_irqresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700358 res->data.extended_irq.interrupts[i],
359 res->data.extended_irq.triggering,
360 res->data.extended_irq.polarity,
361 res->data.extended_irq.sharable);
Len Brown0af58532006-01-20 01:11:37 -0500362 }
363 break;
364
365 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
366 break;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 default:
Len Browneca008c2005-09-22 00:25:18 -0400369 pnp_warn("PnPACPI: unknown resource type %d", res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return AE_ERROR;
371 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return AE_OK;
374}
375
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700376acpi_status pnpacpi_parse_allocated_resource(acpi_handle handle,
377 struct pnp_resource_table * res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 /* Blank the resource table values */
380 pnp_init_resource_table(res);
381
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700382 return acpi_walk_resources(handle, METHOD_NAME__CRS,
383 pnpacpi_allocated_resource, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700386static void pnpacpi_parse_dma_option(struct pnp_option *option,
387 struct acpi_resource_dma *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700390 struct pnp_dma *dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Bob Moore50eca3e2005-09-30 19:03:00 -0400392 if (p->channel_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800394 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!dma)
396 return;
397
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700398 for (i = 0; i < p->channel_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 dma->map |= 1 << p->channels[i];
Michael Karcher362ea082007-03-08 23:29:29 -0500400
401 dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500403 pnp_register_dma_resource(option, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static void pnpacpi_parse_irq_option(struct pnp_option *option,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700407 struct acpi_resource_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 int i;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500410 struct pnp_irq *irq;
411
Bob Moore50eca3e2005-09-30 19:03:00 -0400412 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800414 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!irq)
416 return;
417
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700418 for (i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (p->interrupts[i])
420 __set_bit(p->interrupts[i], irq->map);
Bob Moore50eca3e2005-09-30 19:03:00 -0400421 irq->flags = irq_flags(p->triggering, p->polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 pnp_register_irq_resource(option, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426static void pnpacpi_parse_ext_irq_option(struct pnp_option *option,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700427 struct acpi_resource_extended_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 int i;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500430 struct pnp_irq *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Bob Moore50eca3e2005-09-30 19:03:00 -0400432 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800434 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!irq)
436 return;
437
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700438 for (i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (p->interrupts[i])
440 __set_bit(p->interrupts[i], irq->map);
Bob Moore50eca3e2005-09-30 19:03:00 -0400441 irq->flags = irq_flags(p->triggering, p->polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 pnp_register_irq_resource(option, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700446static void pnpacpi_parse_port_option(struct pnp_option *option,
447 struct acpi_resource_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500449 struct pnp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Bob Moore50eca3e2005-09-30 19:03:00 -0400451 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800453 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!port)
455 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400456 port->min = io->minimum;
457 port->max = io->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 port->align = io->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400459 port->size = io->address_length;
460 port->flags = ACPI_DECODE_16 == io->io_decode ?
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700461 PNP_PORT_FLAG_16BITADDR : 0;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500462 pnp_register_port_resource(option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700465static void pnpacpi_parse_fixed_port_option(struct pnp_option *option,
466 struct acpi_resource_fixed_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500468 struct pnp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Bob Moore50eca3e2005-09-30 19:03:00 -0400470 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800472 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!port)
474 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400475 port->min = port->max = io->address;
476 port->size = io->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 port->align = 0;
478 port->flags = PNP_PORT_FLAG_FIXED;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500479 pnp_register_port_resource(option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700482static void pnpacpi_parse_mem24_option(struct pnp_option *option,
483 struct acpi_resource_memory24 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500485 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Bob Moore50eca3e2005-09-30 19:03:00 -0400487 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800489 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (!mem)
491 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400492 mem->min = p->minimum;
493 mem->max = p->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 mem->align = p->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400495 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Bob Moore08978312005-10-21 00:00:00 -0400497 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700498 IORESOURCE_MEM_WRITEABLE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500500 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700503static void pnpacpi_parse_mem32_option(struct pnp_option *option,
504 struct acpi_resource_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500506 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Bob Moore50eca3e2005-09-30 19:03:00 -0400508 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800510 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!mem)
512 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400513 mem->min = p->minimum;
514 mem->max = p->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 mem->align = p->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400516 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Bob Moore08978312005-10-21 00:00:00 -0400518 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700519 IORESOURCE_MEM_WRITEABLE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500521 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700524static void pnpacpi_parse_fixed_mem32_option(struct pnp_option *option,
525 struct acpi_resource_fixed_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500527 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Bob Moore50eca3e2005-09-30 19:03:00 -0400529 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800531 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!mem)
533 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400534 mem->min = mem->max = p->address;
535 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 mem->align = 0;
537
Bob Moore08978312005-10-21 00:00:00 -0400538 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700539 IORESOURCE_MEM_WRITEABLE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500541 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700544static void pnpacpi_parse_address_option(struct pnp_option *option,
545 struct acpi_resource *r)
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400546{
547 struct acpi_resource_address64 addr, *p = &addr;
548 acpi_status status;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500549 struct pnp_mem *mem;
550 struct pnp_port *port;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400551
552 status = acpi_resource_to_address64(r, p);
553 if (!ACPI_SUCCESS(status)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700554 pnp_warn("PnPACPI: failed to convert resource type %d",
555 r->type);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400556 return;
557 }
558
559 if (p->address_length == 0)
560 return;
561
562 if (p->resource_type == ACPI_MEMORY_RANGE) {
Robert P. J. Daycd861282006-12-13 00:34:52 -0800563 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400564 if (!mem)
565 return;
Len Browned03f432006-01-07 03:50:18 -0500566 mem->min = mem->max = p->minimum;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400567 mem->size = p->address_length;
568 mem->align = 0;
Len Browned03f432006-01-07 03:50:18 -0500569 mem->flags = (p->info.mem.write_protect ==
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700570 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE
571 : 0;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500572 pnp_register_mem_resource(option, mem);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400573 } else if (p->resource_type == ACPI_IO_RANGE) {
Robert P. J. Daycd861282006-12-13 00:34:52 -0800574 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400575 if (!port)
576 return;
Len Browned03f432006-01-07 03:50:18 -0500577 port->min = port->max = p->minimum;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400578 port->size = p->address_length;
579 port->align = 0;
580 port->flags = PNP_PORT_FLAG_FIXED;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500581 pnp_register_port_resource(option, port);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400582 }
583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585struct acpipnp_parse_option_s {
586 struct pnp_option *option;
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500587 struct pnp_option *option_independent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct pnp_dev *dev;
589};
590
Bob Moore50eca3e2005-09-30 19:03:00 -0400591static acpi_status pnpacpi_option_resource(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700592 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 int priority = 0;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600595 struct acpipnp_parse_option_s *parse_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct pnp_dev *dev = parse_data->dev;
597 struct pnp_option *option = parse_data->option;
598
Len Browneca008c2005-09-22 00:25:18 -0400599 switch (res->type) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700600 case ACPI_RESOURCE_TYPE_IRQ:
601 pnpacpi_parse_irq_option(option, &res->data.irq);
602 break;
603
604 case ACPI_RESOURCE_TYPE_DMA:
605 pnpacpi_parse_dma_option(option, &res->data.dma);
606 break;
607
608 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
609 switch (res->data.start_dpf.compatibility_priority) {
610 case ACPI_GOOD_CONFIGURATION:
611 priority = PNP_RES_PRIORITY_PREFERRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
Len Brown0af58532006-01-20 01:11:37 -0500613
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700614 case ACPI_ACCEPTABLE_CONFIGURATION:
615 priority = PNP_RES_PRIORITY_ACCEPTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
Len Brown0af58532006-01-20 01:11:37 -0500617
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700618 case ACPI_SUB_OPTIMAL_CONFIGURATION:
619 priority = PNP_RES_PRIORITY_FUNCTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 default:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700622 priority = PNP_RES_PRIORITY_INVALID;
623 break;
624 }
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700625 /* TBD: Consider performance/robustness bits */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700626 option = pnp_register_dependent_option(dev, priority);
627 if (!option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return AE_ERROR;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700629 parse_data->option = option;
630 break;
631
632 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
633 /*only one EndDependentFn is allowed */
634 if (!parse_data->option_independent) {
635 pnp_warn("PnPACPI: more than one EndDependentFn");
636 return AE_ERROR;
637 }
638 parse_data->option = parse_data->option_independent;
639 parse_data->option_independent = NULL;
640 break;
641
642 case ACPI_RESOURCE_TYPE_IO:
643 pnpacpi_parse_port_option(option, &res->data.io);
644 break;
645
646 case ACPI_RESOURCE_TYPE_FIXED_IO:
647 pnpacpi_parse_fixed_port_option(option, &res->data.fixed_io);
648 break;
649
650 case ACPI_RESOURCE_TYPE_VENDOR:
651 case ACPI_RESOURCE_TYPE_END_TAG:
652 break;
653
654 case ACPI_RESOURCE_TYPE_MEMORY24:
655 pnpacpi_parse_mem24_option(option, &res->data.memory24);
656 break;
657
658 case ACPI_RESOURCE_TYPE_MEMORY32:
659 pnpacpi_parse_mem32_option(option, &res->data.memory32);
660 break;
661
662 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
663 pnpacpi_parse_fixed_mem32_option(option,
664 &res->data.fixed_memory32);
665 break;
666
667 case ACPI_RESOURCE_TYPE_ADDRESS16:
668 case ACPI_RESOURCE_TYPE_ADDRESS32:
669 case ACPI_RESOURCE_TYPE_ADDRESS64:
670 pnpacpi_parse_address_option(option, res);
671 break;
672
673 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
674 break;
675
676 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
677 pnpacpi_parse_ext_irq_option(option, &res->data.extended_irq);
678 break;
679
680 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
681 break;
682
683 default:
684 pnp_warn("PnPACPI: unknown resource type %d", res->type);
685 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return AE_OK;
689}
690
Bob Moore50eca3e2005-09-30 19:03:00 -0400691acpi_status pnpacpi_parse_resource_option_data(acpi_handle handle,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700692 struct pnp_dev * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 acpi_status status;
695 struct acpipnp_parse_option_s parse_data;
696
697 parse_data.option = pnp_register_independent_option(dev);
698 if (!parse_data.option)
699 return AE_ERROR;
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500700 parse_data.option_independent = parse_data.option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 parse_data.dev = dev;
Bob Moore50eca3e2005-09-30 19:03:00 -0400702 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700703 pnpacpi_option_resource, &parse_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 return status;
706}
707
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500708static int pnpacpi_supported_resource(struct acpi_resource *res)
709{
710 switch (res->type) {
711 case ACPI_RESOURCE_TYPE_IRQ:
712 case ACPI_RESOURCE_TYPE_DMA:
713 case ACPI_RESOURCE_TYPE_IO:
714 case ACPI_RESOURCE_TYPE_FIXED_IO:
715 case ACPI_RESOURCE_TYPE_MEMORY24:
716 case ACPI_RESOURCE_TYPE_MEMORY32:
717 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
718 case ACPI_RESOURCE_TYPE_ADDRESS16:
719 case ACPI_RESOURCE_TYPE_ADDRESS32:
720 case ACPI_RESOURCE_TYPE_ADDRESS64:
721 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
722 return 1;
723 }
724 return 0;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/*
728 * Set resource
729 */
730static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700731 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600733 int *res_cnt = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500734
735 if (pnpacpi_supported_resource(res))
736 (*res_cnt)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return AE_OK;
738}
739
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500740static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600742 struct acpi_resource **resource = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500743
744 if (pnpacpi_supported_resource(res)) {
Len Browneca008c2005-09-22 00:25:18 -0400745 (*resource)->type = res->type;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500746 (*resource)->length = sizeof(struct acpi_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 (*resource)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 return AE_OK;
751}
752
Bob Moore50eca3e2005-09-30 19:03:00 -0400753int pnpacpi_build_resource_template(acpi_handle handle,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700754 struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct acpi_resource *resource;
757 int res_cnt = 0;
758 acpi_status status;
759
Bob Moore50eca3e2005-09-30 19:03:00 -0400760 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700761 pnpacpi_count_resources, &res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (ACPI_FAILURE(status)) {
763 pnp_err("Evaluate _CRS failed");
764 return -EINVAL;
765 }
766 if (!res_cnt)
767 return -EINVAL;
768 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800769 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (!buffer->pointer)
771 return -ENOMEM;
772 pnp_dbg("Res cnt %d", res_cnt);
773 resource = (struct acpi_resource *)buffer->pointer;
Bob Moore50eca3e2005-09-30 19:03:00 -0400774 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700775 pnpacpi_type_resources, &resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (ACPI_FAILURE(status)) {
777 kfree(buffer->pointer);
778 pnp_err("Evaluate _CRS failed");
779 return -EINVAL;
780 }
781 /* resource will pointer the end resource now */
Bob Moore50eca3e2005-09-30 19:03:00 -0400782 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 return 0;
785}
786
Bob Moore50eca3e2005-09-30 19:03:00 -0400787static void pnpacpi_encode_irq(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700788 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Bob Moore50eca3e2005-09-30 19:03:00 -0400790 int triggering, polarity;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500791
792 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
Bob Moore50eca3e2005-09-30 19:03:00 -0400793 resource->data.irq.triggering = triggering;
794 resource->data.irq.polarity = polarity;
795 if (triggering == ACPI_EDGE_SENSITIVE)
796 resource->data.irq.sharable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400798 resource->data.irq.sharable = ACPI_SHARED;
799 resource->data.irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 resource->data.irq.interrupts[0] = p->start;
801}
802
803static void pnpacpi_encode_ext_irq(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700804 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Bob Moore50eca3e2005-09-30 19:03:00 -0400806 int triggering, polarity;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500807
808 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 resource->data.extended_irq.producer_consumer = ACPI_CONSUMER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400810 resource->data.extended_irq.triggering = triggering;
811 resource->data.extended_irq.polarity = polarity;
812 if (triggering == ACPI_EDGE_SENSITIVE)
813 resource->data.irq.sharable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400815 resource->data.irq.sharable = ACPI_SHARED;
816 resource->data.extended_irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 resource->data.extended_irq.interrupts[0] = p->start;
818}
819
820static void pnpacpi_encode_dma(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700821 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200824 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700825 case IORESOURCE_DMA_TYPEA:
826 resource->data.dma.type = ACPI_TYPE_A;
827 break;
828 case IORESOURCE_DMA_TYPEB:
829 resource->data.dma.type = ACPI_TYPE_B;
830 break;
831 case IORESOURCE_DMA_TYPEF:
832 resource->data.dma.type = ACPI_TYPE_F;
833 break;
834 default:
835 resource->data.dma.type = ACPI_COMPATIBILITY;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200836 }
837
838 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700839 case IORESOURCE_DMA_8BIT:
840 resource->data.dma.transfer = ACPI_TRANSFER_8;
841 break;
842 case IORESOURCE_DMA_8AND16BIT:
843 resource->data.dma.transfer = ACPI_TRANSFER_8_16;
844 break;
845 default:
846 resource->data.dma.transfer = ACPI_TRANSFER_16;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200847 }
848
849 resource->data.dma.bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
Bob Moore50eca3e2005-09-30 19:03:00 -0400850 resource->data.dma.channel_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 resource->data.dma.channels[0] = p->start;
852}
853
854static void pnpacpi_encode_io(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700855 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700858 resource->data.io.io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR) ?
859 ACPI_DECODE_16 : ACPI_DECODE_10;
Bob Moore50eca3e2005-09-30 19:03:00 -0400860 resource->data.io.minimum = p->start;
861 resource->data.io.maximum = p->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700862 resource->data.io.alignment = 0; /* Correct? */
Bob Moore50eca3e2005-09-30 19:03:00 -0400863 resource->data.io.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866static void pnpacpi_encode_fixed_io(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700867 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Bob Moore50eca3e2005-09-30 19:03:00 -0400869 resource->data.fixed_io.address = p->start;
870 resource->data.fixed_io.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873static void pnpacpi_encode_mem24(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700874 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
Bob Moore08978312005-10-21 00:00:00 -0400877 resource->data.memory24.write_protect =
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700878 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
879 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400880 resource->data.memory24.minimum = p->start;
881 resource->data.memory24.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 resource->data.memory24.alignment = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400883 resource->data.memory24.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static void pnpacpi_encode_mem32(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700887 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Bob Moore08978312005-10-21 00:00:00 -0400889 resource->data.memory32.write_protect =
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700890 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
891 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400892 resource->data.memory32.minimum = p->start;
893 resource->data.memory32.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 resource->data.memory32.alignment = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400895 resource->data.memory32.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898static void pnpacpi_encode_fixed_mem32(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700899 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Bob Moore08978312005-10-21 00:00:00 -0400901 resource->data.fixed_memory32.write_protect =
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700902 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
903 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400904 resource->data.fixed_memory32.address = p->start;
905 resource->data.fixed_memory32.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Bob Moore50eca3e2005-09-30 19:03:00 -0400908int pnpacpi_encode_resources(struct pnp_resource_table *res_table,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700909 struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 int i = 0;
912 /* pnpacpi_build_resource_template allocates extra mem */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700913 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600914 struct acpi_resource *resource = buffer->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int port = 0, irq = 0, dma = 0, mem = 0;
916
917 pnp_dbg("res cnt %d", res_cnt);
918 while (i < res_cnt) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700919 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400920 case ACPI_RESOURCE_TYPE_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 pnp_dbg("Encode irq");
Bob Moore50eca3e2005-09-30 19:03:00 -0400922 pnpacpi_encode_irq(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700923 &res_table->irq_resource[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 irq++;
925 break;
926
Bob Moore50eca3e2005-09-30 19:03:00 -0400927 case ACPI_RESOURCE_TYPE_DMA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 pnp_dbg("Encode dma");
Bob Moore50eca3e2005-09-30 19:03:00 -0400929 pnpacpi_encode_dma(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700930 &res_table->dma_resource[dma]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500931 dma++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400933 case ACPI_RESOURCE_TYPE_IO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 pnp_dbg("Encode io");
Bob Moore50eca3e2005-09-30 19:03:00 -0400935 pnpacpi_encode_io(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700936 &res_table->port_resource[port]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500937 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400939 case ACPI_RESOURCE_TYPE_FIXED_IO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 pnp_dbg("Encode fixed io");
941 pnpacpi_encode_fixed_io(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700942 &res_table->
943 port_resource[port]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500944 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400946 case ACPI_RESOURCE_TYPE_MEMORY24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 pnp_dbg("Encode mem24");
948 pnpacpi_encode_mem24(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700949 &res_table->mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500950 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400952 case ACPI_RESOURCE_TYPE_MEMORY32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 pnp_dbg("Encode mem32");
954 pnpacpi_encode_mem32(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700955 &res_table->mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500956 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400958 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 pnp_dbg("Encode fixed mem32");
960 pnpacpi_encode_fixed_mem32(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700961 &res_table->
962 mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500963 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 break;
Len Brown0af58532006-01-20 01:11:37 -0500965 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
966 pnp_dbg("Encode ext irq");
967 pnpacpi_encode_ext_irq(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700968 &res_table->irq_resource[irq]);
Len Brown0af58532006-01-20 01:11:37 -0500969 irq++;
970 break;
971 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
972 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
973 case ACPI_RESOURCE_TYPE_VENDOR:
974 case ACPI_RESOURCE_TYPE_END_TAG:
975 case ACPI_RESOURCE_TYPE_ADDRESS16:
976 case ACPI_RESOURCE_TYPE_ADDRESS32:
977 case ACPI_RESOURCE_TYPE_ADDRESS64:
978 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
979 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700980 default: /* other type */
Len Browneca008c2005-09-22 00:25:18 -0400981 pnp_warn("unknown resource type %d", resource->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return -EINVAL;
983 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500984 resource++;
985 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 return 0;
988}