blob: 6aa231ef642d3e366b13984a56e524851d9251fa [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;
Len Brown66a21732008-01-12 17:56:36 -050079 static unsigned char warned;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040080
81 if (!valid_IRQ(gsi))
82 return;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -070085 i < PNP_MAX_IRQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 i++;
Len Brown66a21732008-01-12 17:56:36 -050087 if (i >= PNP_MAX_IRQ && !warned) {
Zhao Yakuia7839e92007-11-28 16:21:21 -080088 printk(KERN_ERR "pnpacpi: exceeded the max number of IRQ "
89 "resources: %d \n", PNP_MAX_IRQ);
Len Brown66a21732008-01-12 17:56:36 -050090 warned = 1;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040091 return;
Zhao Yakuia7839e92007-11-28 16:21:21 -080092 }
Shaohua Li61fd47e2007-11-17 01:05:28 -050093 /*
94 * in IO-APIC mode, use overrided attribute. Two reasons:
95 * 1. BIOS bug in DSDT
96 * 2. BIOS uses IO-APIC mode Interrupt Source Override
97 */
98 if (!acpi_get_override_irq(gsi, &t, &p)) {
99 t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
100 p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
101
102 if (triggering != t || polarity != p) {
103 pnp_warn("IRQ %d override to %s, %s",
104 gsi, t ? "edge":"level", p ? "low":"high");
105 triggering = t;
106 polarity = p;
107 }
108 }
109
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700110 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
Michael Karcher362ea082007-03-08 23:29:29 -0500111 res->irq_resource[i].flags |= irq_flags(triggering, polarity);
Bob Moore50eca3e2005-09-30 19:03:00 -0400112 irq = acpi_register_gsi(gsi, triggering, polarity);
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400113 if (irq < 0) {
114 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400117
Bjorn Helgaasc32928c2006-07-03 00:24:10 -0700118 if (shareable)
119 res->irq_resource[i].flags |= IORESOURCE_IRQ_SHAREABLE;
120
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400121 res->irq_resource[i].start = irq;
122 res->irq_resource[i].end = irq;
123 pcibios_penalize_isa_irq(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Michael Karcher362ea082007-03-08 23:29:29 -0500126static int dma_flags(int type, int bus_master, int transfer)
127{
128 int flags = 0;
129
130 if (bus_master)
131 flags |= IORESOURCE_DMA_MASTER;
132 switch (type) {
133 case ACPI_COMPATIBILITY:
134 flags |= IORESOURCE_DMA_COMPATIBLE;
135 break;
136 case ACPI_TYPE_A:
137 flags |= IORESOURCE_DMA_TYPEA;
138 break;
139 case ACPI_TYPE_B:
140 flags |= IORESOURCE_DMA_TYPEB;
141 break;
142 case ACPI_TYPE_F:
143 flags |= IORESOURCE_DMA_TYPEF;
144 break;
145 default:
146 /* Set a default value ? */
147 flags |= IORESOURCE_DMA_COMPATIBLE;
148 pnp_err("Invalid DMA type");
149 }
150 switch (transfer) {
151 case ACPI_TRANSFER_8:
152 flags |= IORESOURCE_DMA_8BIT;
153 break;
154 case ACPI_TRANSFER_8_16:
155 flags |= IORESOURCE_DMA_8AND16BIT;
156 break;
157 case ACPI_TRANSFER_16:
158 flags |= IORESOURCE_DMA_16BIT;
159 break;
160 default:
161 /* Set a default value ? */
162 flags |= IORESOURCE_DMA_8AND16BIT;
163 pnp_err("Invalid DMA transfer type");
164 }
165
166 return flags;
167}
168
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700169static void pnpacpi_parse_allocated_dmaresource(struct pnp_resource_table *res,
170 u32 dma, int type,
171 int bus_master, int transfer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int i = 0;
Len Brown66a21732008-01-12 17:56:36 -0500174 static unsigned char warned;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700175
Adrian Bunk0dec63b2005-05-01 08:59:30 -0700176 while (i < PNP_MAX_DMA &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700177 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 i++;
179 if (i < PNP_MAX_DMA) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700180 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
181 res->dma_resource[i].flags |=
182 dma_flags(type, bus_master, transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (dma == -1) {
184 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
185 return;
186 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400187 res->dma_resource[i].start = dma;
188 res->dma_resource[i].end = dma;
Len Brown66a21732008-01-12 17:56:36 -0500189 } else if (!warned) {
Zhao Yakuia7839e92007-11-28 16:21:21 -0800190 printk(KERN_ERR "pnpacpi: exceeded the max number of DMA "
191 "resources: %d \n", PNP_MAX_DMA);
Len Brown66a21732008-01-12 17:56:36 -0500192 warned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194}
195
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700196static void pnpacpi_parse_allocated_ioresource(struct pnp_resource_table *res,
197 u64 io, u64 len, int io_decode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int i = 0;
Len Brown66a21732008-01-12 17:56:36 -0500200 static unsigned char warned;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700203 i < PNP_MAX_PORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 i++;
205 if (i < PNP_MAX_PORT) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700206 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
Michael Karcher362ea082007-03-08 23:29:29 -0500207 if (io_decode == ACPI_DECODE_16)
208 res->port_resource[i].flags |= PNP_PORT_FLAG_16BITADDR;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700209 if (len <= 0 || (io + len - 1) >= 0x10003) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 res->port_resource[i].flags |= IORESOURCE_DISABLED;
211 return;
212 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400213 res->port_resource[i].start = io;
214 res->port_resource[i].end = io + len - 1;
Len Brown66a21732008-01-12 17:56:36 -0500215 } else if (!warned) {
Zhao Yakuia7839e92007-11-28 16:21:21 -0800216 printk(KERN_ERR "pnpacpi: exceeded the max number of IO "
217 "resources: %d \n", PNP_MAX_PORT);
Len Brownefe7cf22008-01-17 15:21:10 -0800218 warned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220}
221
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700222static void pnpacpi_parse_allocated_memresource(struct pnp_resource_table *res,
223 u64 mem, u64 len,
224 int write_protect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int i = 0;
Len Brown66a21732008-01-12 17:56:36 -0500227 static unsigned char warned;
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) &&
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700230 (i < PNP_MAX_MEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 i++;
232 if (i < PNP_MAX_MEM) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700233 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (len <= 0) {
235 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
236 return;
237 }
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700238 if (write_protect == ACPI_READ_WRITE_MEMORY)
Michael Karcher362ea082007-03-08 23:29:29 -0500239 res->mem_resource[i].flags |= IORESOURCE_MEM_WRITEABLE;
240
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400241 res->mem_resource[i].start = mem;
242 res->mem_resource[i].end = mem + len - 1;
Len Brown66a21732008-01-12 17:56:36 -0500243 } else if (!warned) {
Zhao Yakuia7839e92007-11-28 16:21:21 -0800244 printk(KERN_ERR "pnpacpi: exceeded the max number of mem "
245 "resources: %d\n", PNP_MAX_MEM);
Len Brownefe7cf22008-01-17 15:21:10 -0800246 warned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248}
249
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700250static void pnpacpi_parse_allocated_address_space(struct pnp_resource_table *res_table,
251 struct acpi_resource *res)
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500252{
253 struct acpi_resource_address64 addr, *p = &addr;
254 acpi_status status;
255
256 status = acpi_resource_to_address64(res, p);
257 if (!ACPI_SUCCESS(status)) {
258 pnp_warn("PnPACPI: failed to convert resource type %d",
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700259 res->type);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500260 return;
261 }
262
matthieu castet2b8de5f2006-08-05 12:15:12 -0700263 if (p->producer_consumer == ACPI_PRODUCER)
264 return;
265
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500266 if (p->resource_type == ACPI_MEMORY_RANGE)
267 pnpacpi_parse_allocated_memresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700268 p->minimum, p->address_length,
269 p->info.mem.write_protect);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500270 else if (p->resource_type == ACPI_IO_RANGE)
271 pnpacpi_parse_allocated_ioresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700272 p->minimum, p->address_length,
273 p->granularity == 0xfff ? ACPI_DECODE_10 :
274 ACPI_DECODE_16);
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700278 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600280 struct pnp_resource_table *res_table = data;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400281 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Len Browneca008c2005-09-22 00:25:18 -0400283 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400284 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400285 /*
286 * Per spec, only one interrupt per descriptor is allowed in
287 * _CRS, but some firmware violates this, so parse them all.
288 */
Bob Moore50eca3e2005-09-30 19:03:00 -0400289 for (i = 0; i < res->data.irq.interrupt_count; i++) {
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400290 pnpacpi_parse_allocated_irqresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700291 res->data.irq.interrupts[i],
292 res->data.irq.triggering,
293 res->data.irq.polarity,
294 res->data.irq.sharable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 break;
297
Bob Moore50eca3e2005-09-30 19:03:00 -0400298 case ACPI_RESOURCE_TYPE_DMA:
299 if (res->data.dma.channel_count > 0)
300 pnpacpi_parse_allocated_dmaresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700301 res->data.dma.channels[0],
302 res->data.dma.type,
303 res->data.dma.bus_master,
304 res->data.dma.transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
Len Brown0af58532006-01-20 01:11:37 -0500306
Bob Moore50eca3e2005-09-30 19:03:00 -0400307 case ACPI_RESOURCE_TYPE_IO:
308 pnpacpi_parse_allocated_ioresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700309 res->data.io.minimum,
310 res->data.io.address_length,
311 res->data.io.io_decode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
Len Brown0af58532006-01-20 01:11:37 -0500313
314 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
315 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
316 break;
317
Bob Moore50eca3e2005-09-30 19:03:00 -0400318 case ACPI_RESOURCE_TYPE_FIXED_IO:
319 pnpacpi_parse_allocated_ioresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700320 res->data.fixed_io.address,
321 res->data.fixed_io.address_length,
322 ACPI_DECODE_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
Len Brown0af58532006-01-20 01:11:37 -0500324
325 case ACPI_RESOURCE_TYPE_VENDOR:
326 break;
327
328 case ACPI_RESOURCE_TYPE_END_TAG:
329 break;
330
Bob Moore50eca3e2005-09-30 19:03:00 -0400331 case ACPI_RESOURCE_TYPE_MEMORY24:
332 pnpacpi_parse_allocated_memresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700333 res->data.memory24.minimum,
334 res->data.memory24.address_length,
335 res->data.memory24.write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400337 case ACPI_RESOURCE_TYPE_MEMORY32:
338 pnpacpi_parse_allocated_memresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700339 res->data.memory32.minimum,
340 res->data.memory32.address_length,
341 res->data.memory32.write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400343 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
344 pnpacpi_parse_allocated_memresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700345 res->data.fixed_memory32.address,
346 res->data.fixed_memory32.address_length,
347 res->data.fixed_memory32.write_protect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400349 case ACPI_RESOURCE_TYPE_ADDRESS16:
Bob Moore50eca3e2005-09-30 19:03:00 -0400350 case ACPI_RESOURCE_TYPE_ADDRESS32:
Bob Moore50eca3e2005-09-30 19:03:00 -0400351 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500352 pnpacpi_parse_allocated_address_space(res_table, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
Len Brown0af58532006-01-20 01:11:37 -0500354
355 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
matthieu castet2b8de5f2006-08-05 12:15:12 -0700356 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
357 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
Len Brown0af58532006-01-20 01:11:37 -0500359
360 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
matthieu castet2b8de5f2006-08-05 12:15:12 -0700361 if (res->data.extended_irq.producer_consumer == ACPI_PRODUCER)
362 return AE_OK;
363
Len Brown0af58532006-01-20 01:11:37 -0500364 for (i = 0; i < res->data.extended_irq.interrupt_count; i++) {
365 pnpacpi_parse_allocated_irqresource(res_table,
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700366 res->data.extended_irq.interrupts[i],
367 res->data.extended_irq.triggering,
368 res->data.extended_irq.polarity,
369 res->data.extended_irq.sharable);
Len Brown0af58532006-01-20 01:11:37 -0500370 }
371 break;
372
373 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
374 break;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 default:
Len Browneca008c2005-09-22 00:25:18 -0400377 pnp_warn("PnPACPI: unknown resource type %d", res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return AE_ERROR;
379 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return AE_OK;
382}
383
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700384acpi_status pnpacpi_parse_allocated_resource(acpi_handle handle,
385 struct pnp_resource_table * res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 /* Blank the resource table values */
388 pnp_init_resource_table(res);
389
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700390 return acpi_walk_resources(handle, METHOD_NAME__CRS,
391 pnpacpi_allocated_resource, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800394static __init void pnpacpi_parse_dma_option(struct pnp_option *option,
395 struct acpi_resource_dma *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 int i;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700398 struct pnp_dma *dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Bob Moore50eca3e2005-09-30 19:03:00 -0400400 if (p->channel_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800402 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!dma)
404 return;
405
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700406 for (i = 0; i < p->channel_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dma->map |= 1 << p->channels[i];
Michael Karcher362ea082007-03-08 23:29:29 -0500408
409 dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500411 pnp_register_dma_resource(option, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800414static __init void pnpacpi_parse_irq_option(struct pnp_option *option,
415 struct acpi_resource_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 int i;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500418 struct pnp_irq *irq;
419
Bob Moore50eca3e2005-09-30 19:03:00 -0400420 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800422 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (!irq)
424 return;
425
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700426 for (i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (p->interrupts[i])
428 __set_bit(p->interrupts[i], irq->map);
Bob Moore50eca3e2005-09-30 19:03:00 -0400429 irq->flags = irq_flags(p->triggering, p->polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 pnp_register_irq_resource(option, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800434static __init void pnpacpi_parse_ext_irq_option(struct pnp_option *option,
435 struct acpi_resource_extended_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 int i;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500438 struct pnp_irq *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Bob Moore50eca3e2005-09-30 19:03:00 -0400440 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800442 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (!irq)
444 return;
445
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700446 for (i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (p->interrupts[i])
448 __set_bit(p->interrupts[i], irq->map);
Bob Moore50eca3e2005-09-30 19:03:00 -0400449 irq->flags = irq_flags(p->triggering, p->polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 pnp_register_irq_resource(option, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800454static __init void pnpacpi_parse_port_option(struct pnp_option *option,
455 struct acpi_resource_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500457 struct pnp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Bob Moore50eca3e2005-09-30 19:03:00 -0400459 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800461 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!port)
463 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400464 port->min = io->minimum;
465 port->max = io->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 port->align = io->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400467 port->size = io->address_length;
468 port->flags = ACPI_DECODE_16 == io->io_decode ?
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700469 PNP_PORT_FLAG_16BITADDR : 0;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500470 pnp_register_port_resource(option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800473static __init void pnpacpi_parse_fixed_port_option(struct pnp_option *option,
474 struct acpi_resource_fixed_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500476 struct pnp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Bob Moore50eca3e2005-09-30 19:03:00 -0400478 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800480 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!port)
482 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400483 port->min = port->max = io->address;
484 port->size = io->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 port->align = 0;
486 port->flags = PNP_PORT_FLAG_FIXED;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500487 pnp_register_port_resource(option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800490static __init void pnpacpi_parse_mem24_option(struct pnp_option *option,
491 struct acpi_resource_memory24 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500493 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Bob Moore50eca3e2005-09-30 19:03:00 -0400495 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800497 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!mem)
499 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400500 mem->min = p->minimum;
501 mem->max = p->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 mem->align = p->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400503 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Bob Moore08978312005-10-21 00:00:00 -0400505 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700506 IORESOURCE_MEM_WRITEABLE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500508 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800511static __init void pnpacpi_parse_mem32_option(struct pnp_option *option,
512 struct acpi_resource_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500514 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Bob Moore50eca3e2005-09-30 19:03:00 -0400516 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800518 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (!mem)
520 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400521 mem->min = p->minimum;
522 mem->max = p->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 mem->align = p->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400524 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Bob Moore08978312005-10-21 00:00:00 -0400526 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700527 IORESOURCE_MEM_WRITEABLE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500529 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800532static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_option *option,
533 struct acpi_resource_fixed_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500535 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Bob Moore50eca3e2005-09-30 19:03:00 -0400537 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800539 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (!mem)
541 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400542 mem->min = mem->max = p->address;
543 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 mem->align = 0;
545
Bob Moore08978312005-10-21 00:00:00 -0400546 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700547 IORESOURCE_MEM_WRITEABLE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500549 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800552static __init void pnpacpi_parse_address_option(struct pnp_option *option,
553 struct acpi_resource *r)
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400554{
555 struct acpi_resource_address64 addr, *p = &addr;
556 acpi_status status;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500557 struct pnp_mem *mem;
558 struct pnp_port *port;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400559
560 status = acpi_resource_to_address64(r, p);
561 if (!ACPI_SUCCESS(status)) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700562 pnp_warn("PnPACPI: failed to convert resource type %d",
563 r->type);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400564 return;
565 }
566
567 if (p->address_length == 0)
568 return;
569
570 if (p->resource_type == ACPI_MEMORY_RANGE) {
Robert P. J. Daycd861282006-12-13 00:34:52 -0800571 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400572 if (!mem)
573 return;
Len Browned03f432006-01-07 03:50:18 -0500574 mem->min = mem->max = p->minimum;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400575 mem->size = p->address_length;
576 mem->align = 0;
Len Browned03f432006-01-07 03:50:18 -0500577 mem->flags = (p->info.mem.write_protect ==
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700578 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE
579 : 0;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500580 pnp_register_mem_resource(option, mem);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400581 } else if (p->resource_type == ACPI_IO_RANGE) {
Robert P. J. Daycd861282006-12-13 00:34:52 -0800582 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400583 if (!port)
584 return;
Len Browned03f432006-01-07 03:50:18 -0500585 port->min = port->max = p->minimum;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400586 port->size = p->address_length;
587 port->align = 0;
588 port->flags = PNP_PORT_FLAG_FIXED;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500589 pnp_register_port_resource(option, port);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400590 }
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593struct acpipnp_parse_option_s {
594 struct pnp_option *option;
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500595 struct pnp_option *option_independent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct pnp_dev *dev;
597};
598
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800599static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
600 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 int priority = 0;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600603 struct acpipnp_parse_option_s *parse_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct pnp_dev *dev = parse_data->dev;
605 struct pnp_option *option = parse_data->option;
606
Len Browneca008c2005-09-22 00:25:18 -0400607 switch (res->type) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700608 case ACPI_RESOURCE_TYPE_IRQ:
609 pnpacpi_parse_irq_option(option, &res->data.irq);
610 break;
611
612 case ACPI_RESOURCE_TYPE_DMA:
613 pnpacpi_parse_dma_option(option, &res->data.dma);
614 break;
615
616 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
617 switch (res->data.start_dpf.compatibility_priority) {
618 case ACPI_GOOD_CONFIGURATION:
619 priority = PNP_RES_PRIORITY_PREFERRED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
Len Brown0af58532006-01-20 01:11:37 -0500621
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700622 case ACPI_ACCEPTABLE_CONFIGURATION:
623 priority = PNP_RES_PRIORITY_ACCEPTABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
Len Brown0af58532006-01-20 01:11:37 -0500625
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700626 case ACPI_SUB_OPTIMAL_CONFIGURATION:
627 priority = PNP_RES_PRIORITY_FUNCTIONAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 default:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700630 priority = PNP_RES_PRIORITY_INVALID;
631 break;
632 }
Bjorn Helgaas07d4e9a2007-07-26 10:41:21 -0700633 /* TBD: Consider performance/robustness bits */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700634 option = pnp_register_dependent_option(dev, priority);
635 if (!option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return AE_ERROR;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700637 parse_data->option = option;
638 break;
639
640 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
641 /*only one EndDependentFn is allowed */
642 if (!parse_data->option_independent) {
643 pnp_warn("PnPACPI: more than one EndDependentFn");
644 return AE_ERROR;
645 }
646 parse_data->option = parse_data->option_independent;
647 parse_data->option_independent = NULL;
648 break;
649
650 case ACPI_RESOURCE_TYPE_IO:
651 pnpacpi_parse_port_option(option, &res->data.io);
652 break;
653
654 case ACPI_RESOURCE_TYPE_FIXED_IO:
655 pnpacpi_parse_fixed_port_option(option, &res->data.fixed_io);
656 break;
657
658 case ACPI_RESOURCE_TYPE_VENDOR:
659 case ACPI_RESOURCE_TYPE_END_TAG:
660 break;
661
662 case ACPI_RESOURCE_TYPE_MEMORY24:
663 pnpacpi_parse_mem24_option(option, &res->data.memory24);
664 break;
665
666 case ACPI_RESOURCE_TYPE_MEMORY32:
667 pnpacpi_parse_mem32_option(option, &res->data.memory32);
668 break;
669
670 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
671 pnpacpi_parse_fixed_mem32_option(option,
672 &res->data.fixed_memory32);
673 break;
674
675 case ACPI_RESOURCE_TYPE_ADDRESS16:
676 case ACPI_RESOURCE_TYPE_ADDRESS32:
677 case ACPI_RESOURCE_TYPE_ADDRESS64:
678 pnpacpi_parse_address_option(option, res);
679 break;
680
681 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
682 break;
683
684 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
685 pnpacpi_parse_ext_irq_option(option, &res->data.extended_irq);
686 break;
687
688 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
689 break;
690
691 default:
692 pnp_warn("PnPACPI: unknown resource type %d", res->type);
693 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return AE_OK;
697}
698
Thomas Renninger2bb9a6b2008-02-06 01:40:03 -0800699acpi_status __init pnpacpi_parse_resource_option_data(acpi_handle handle,
700 struct pnp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 acpi_status status;
703 struct acpipnp_parse_option_s parse_data;
704
705 parse_data.option = pnp_register_independent_option(dev);
706 if (!parse_data.option)
707 return AE_ERROR;
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500708 parse_data.option_independent = parse_data.option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 parse_data.dev = dev;
Bob Moore50eca3e2005-09-30 19:03:00 -0400710 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700711 pnpacpi_option_resource, &parse_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 return status;
714}
715
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500716static int pnpacpi_supported_resource(struct acpi_resource *res)
717{
718 switch (res->type) {
719 case ACPI_RESOURCE_TYPE_IRQ:
720 case ACPI_RESOURCE_TYPE_DMA:
721 case ACPI_RESOURCE_TYPE_IO:
722 case ACPI_RESOURCE_TYPE_FIXED_IO:
723 case ACPI_RESOURCE_TYPE_MEMORY24:
724 case ACPI_RESOURCE_TYPE_MEMORY32:
725 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
726 case ACPI_RESOURCE_TYPE_ADDRESS16:
727 case ACPI_RESOURCE_TYPE_ADDRESS32:
728 case ACPI_RESOURCE_TYPE_ADDRESS64:
729 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
730 return 1;
731 }
732 return 0;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * Set resource
737 */
738static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700739 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600741 int *res_cnt = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500742
743 if (pnpacpi_supported_resource(res))
744 (*res_cnt)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return AE_OK;
746}
747
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500748static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600750 struct acpi_resource **resource = data;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500751
752 if (pnpacpi_supported_resource(res)) {
Len Browneca008c2005-09-22 00:25:18 -0400753 (*resource)->type = res->type;
Bjorn Helgaasb5f2490b2006-03-28 17:04:00 -0500754 (*resource)->length = sizeof(struct acpi_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 (*resource)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
758 return AE_OK;
759}
760
Bob Moore50eca3e2005-09-30 19:03:00 -0400761int pnpacpi_build_resource_template(acpi_handle handle,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700762 struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct acpi_resource *resource;
765 int res_cnt = 0;
766 acpi_status status;
767
Bob Moore50eca3e2005-09-30 19:03:00 -0400768 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700769 pnpacpi_count_resources, &res_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (ACPI_FAILURE(status)) {
771 pnp_err("Evaluate _CRS failed");
772 return -EINVAL;
773 }
774 if (!res_cnt)
775 return -EINVAL;
776 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
Robert P. J. Daycd861282006-12-13 00:34:52 -0800777 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!buffer->pointer)
779 return -ENOMEM;
780 pnp_dbg("Res cnt %d", res_cnt);
781 resource = (struct acpi_resource *)buffer->pointer;
Bob Moore50eca3e2005-09-30 19:03:00 -0400782 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700783 pnpacpi_type_resources, &resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (ACPI_FAILURE(status)) {
785 kfree(buffer->pointer);
786 pnp_err("Evaluate _CRS failed");
787 return -EINVAL;
788 }
789 /* resource will pointer the end resource now */
Bob Moore50eca3e2005-09-30 19:03:00 -0400790 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 return 0;
793}
794
Bob Moore50eca3e2005-09-30 19:03:00 -0400795static void pnpacpi_encode_irq(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700796 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Bob Moore50eca3e2005-09-30 19:03:00 -0400798 int triggering, polarity;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500799
800 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
Bob Moore50eca3e2005-09-30 19:03:00 -0400801 resource->data.irq.triggering = triggering;
802 resource->data.irq.polarity = polarity;
803 if (triggering == ACPI_EDGE_SENSITIVE)
804 resource->data.irq.sharable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400806 resource->data.irq.sharable = ACPI_SHARED;
807 resource->data.irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 resource->data.irq.interrupts[0] = p->start;
809}
810
811static void pnpacpi_encode_ext_irq(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700812 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Bob Moore50eca3e2005-09-30 19:03:00 -0400814 int triggering, polarity;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500815
816 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 resource->data.extended_irq.producer_consumer = ACPI_CONSUMER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400818 resource->data.extended_irq.triggering = triggering;
819 resource->data.extended_irq.polarity = polarity;
820 if (triggering == ACPI_EDGE_SENSITIVE)
821 resource->data.irq.sharable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400823 resource->data.irq.sharable = ACPI_SHARED;
824 resource->data.extended_irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 resource->data.extended_irq.interrupts[0] = p->start;
826}
827
828static void pnpacpi_encode_dma(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700829 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200832 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700833 case IORESOURCE_DMA_TYPEA:
834 resource->data.dma.type = ACPI_TYPE_A;
835 break;
836 case IORESOURCE_DMA_TYPEB:
837 resource->data.dma.type = ACPI_TYPE_B;
838 break;
839 case IORESOURCE_DMA_TYPEF:
840 resource->data.dma.type = ACPI_TYPE_F;
841 break;
842 default:
843 resource->data.dma.type = ACPI_COMPATIBILITY;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200844 }
845
846 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700847 case IORESOURCE_DMA_8BIT:
848 resource->data.dma.transfer = ACPI_TRANSFER_8;
849 break;
850 case IORESOURCE_DMA_8AND16BIT:
851 resource->data.dma.transfer = ACPI_TRANSFER_8_16;
852 break;
853 default:
854 resource->data.dma.transfer = ACPI_TRANSFER_16;
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200855 }
856
857 resource->data.dma.bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
Bob Moore50eca3e2005-09-30 19:03:00 -0400858 resource->data.dma.channel_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 resource->data.dma.channels[0] = p->start;
860}
861
862static void pnpacpi_encode_io(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700863 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700866 resource->data.io.io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR) ?
867 ACPI_DECODE_16 : ACPI_DECODE_10;
Bob Moore50eca3e2005-09-30 19:03:00 -0400868 resource->data.io.minimum = p->start;
869 resource->data.io.maximum = p->end;
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700870 resource->data.io.alignment = 0; /* Correct? */
Bob Moore50eca3e2005-09-30 19:03:00 -0400871 resource->data.io.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874static void pnpacpi_encode_fixed_io(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700875 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Bob Moore50eca3e2005-09-30 19:03:00 -0400877 resource->data.fixed_io.address = p->start;
878 resource->data.fixed_io.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881static void pnpacpi_encode_mem24(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700882 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
Bob Moore08978312005-10-21 00:00:00 -0400885 resource->data.memory24.write_protect =
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700886 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
887 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400888 resource->data.memory24.minimum = p->start;
889 resource->data.memory24.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 resource->data.memory24.alignment = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400891 resource->data.memory24.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894static void pnpacpi_encode_mem32(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700895 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Bob Moore08978312005-10-21 00:00:00 -0400897 resource->data.memory32.write_protect =
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700898 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
899 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400900 resource->data.memory32.minimum = p->start;
901 resource->data.memory32.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 resource->data.memory32.alignment = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400903 resource->data.memory32.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906static void pnpacpi_encode_fixed_mem32(struct acpi_resource *resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700907 struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Bob Moore08978312005-10-21 00:00:00 -0400909 resource->data.fixed_memory32.write_protect =
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700910 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
911 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400912 resource->data.fixed_memory32.address = p->start;
913 resource->data.fixed_memory32.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Bob Moore50eca3e2005-09-30 19:03:00 -0400916int pnpacpi_encode_resources(struct pnp_resource_table *res_table,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700917 struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 int i = 0;
920 /* pnpacpi_build_resource_template allocates extra mem */
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700921 int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
Bjorn Helgaas4721a4cc2007-08-15 10:32:10 -0600922 struct acpi_resource *resource = buffer->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int port = 0, irq = 0, dma = 0, mem = 0;
924
925 pnp_dbg("res cnt %d", res_cnt);
926 while (i < res_cnt) {
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700927 switch (resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400928 case ACPI_RESOURCE_TYPE_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 pnp_dbg("Encode irq");
Bob Moore50eca3e2005-09-30 19:03:00 -0400930 pnpacpi_encode_irq(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700931 &res_table->irq_resource[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 irq++;
933 break;
934
Bob Moore50eca3e2005-09-30 19:03:00 -0400935 case ACPI_RESOURCE_TYPE_DMA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 pnp_dbg("Encode dma");
Bob Moore50eca3e2005-09-30 19:03:00 -0400937 pnpacpi_encode_dma(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700938 &res_table->dma_resource[dma]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500939 dma++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400941 case ACPI_RESOURCE_TYPE_IO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 pnp_dbg("Encode io");
Bob Moore50eca3e2005-09-30 19:03:00 -0400943 pnpacpi_encode_io(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700944 &res_table->port_resource[port]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500945 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400947 case ACPI_RESOURCE_TYPE_FIXED_IO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 pnp_dbg("Encode fixed io");
949 pnpacpi_encode_fixed_io(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700950 &res_table->
951 port_resource[port]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500952 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400954 case ACPI_RESOURCE_TYPE_MEMORY24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 pnp_dbg("Encode mem24");
956 pnpacpi_encode_mem24(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700957 &res_table->mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500958 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400960 case ACPI_RESOURCE_TYPE_MEMORY32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 pnp_dbg("Encode mem32");
962 pnpacpi_encode_mem32(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700963 &res_table->mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500964 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400966 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 pnp_dbg("Encode fixed mem32");
968 pnpacpi_encode_fixed_mem32(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700969 &res_table->
970 mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500971 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
Len Brown0af58532006-01-20 01:11:37 -0500973 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
974 pnp_dbg("Encode ext irq");
975 pnpacpi_encode_ext_irq(resource,
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700976 &res_table->irq_resource[irq]);
Len Brown0af58532006-01-20 01:11:37 -0500977 irq++;
978 break;
979 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
980 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
981 case ACPI_RESOURCE_TYPE_VENDOR:
982 case ACPI_RESOURCE_TYPE_END_TAG:
983 case ACPI_RESOURCE_TYPE_ADDRESS16:
984 case ACPI_RESOURCE_TYPE_ADDRESS32:
985 case ACPI_RESOURCE_TYPE_ADDRESS64:
986 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
987 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Bjorn Helgaas9dd78462007-07-26 10:41:20 -0700988 default: /* other type */
Len Browneca008c2005-09-22 00:25:18 -0400989 pnp_warn("unknown resource type %d", resource->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -EINVAL;
991 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500992 resource++;
993 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 return 0;
996}