blob: 379048fdf05dccf01575a5727219529ac7d39fac [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{
37 int flag;
Bob Moore50eca3e2005-09-30 19:03:00 -040038 if (triggering == ACPI_LEVEL_SENSITIVE) {
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050039 if (polarity == ACPI_ACTIVE_LOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 flag = IORESOURCE_IRQ_LOWLEVEL;
41 else
42 flag = IORESOURCE_IRQ_HIGHLEVEL;
43 }
44 else {
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050045 if (polarity == ACPI_ACTIVE_LOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 flag = IORESOURCE_IRQ_LOWEDGE;
47 else
48 flag = IORESOURCE_IRQ_HIGHEDGE;
49 }
50 return flag;
51}
52
Bob Moore50eca3e2005-09-30 19:03:00 -040053static void decode_irq_flags(int flag, int *triggering, int *polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 switch (flag) {
56 case IORESOURCE_IRQ_LOWLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040057 *triggering = ACPI_LEVEL_SENSITIVE;
58 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 break;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050060 case IORESOURCE_IRQ_HIGHLEVEL:
Bob Moore50eca3e2005-09-30 19:03:00 -040061 *triggering = ACPI_LEVEL_SENSITIVE;
62 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 break;
64 case IORESOURCE_IRQ_LOWEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040065 *triggering = ACPI_EDGE_SENSITIVE;
66 *polarity = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
68 case IORESOURCE_IRQ_HIGHEDGE:
Bob Moore50eca3e2005-09-30 19:03:00 -040069 *triggering = ACPI_EDGE_SENSITIVE;
70 *polarity = ACPI_ACTIVE_HIGH;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 break;
72 }
73}
74
75static void
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -050076pnpacpi_parse_allocated_irqresource(struct pnp_resource_table *res, u32 gsi,
Bjorn Helgaasc32928c2006-07-03 00:24:10 -070077 int triggering, int polarity, int shareable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 int i = 0;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040080 int irq;
81
82 if (!valid_IRQ(gsi))
83 return;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) &&
86 i < PNP_MAX_IRQ)
87 i++;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040088 if (i >= PNP_MAX_IRQ)
89 return;
90
91 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
Bob Moore50eca3e2005-09-30 19:03:00 -040092 irq = acpi_register_gsi(gsi, triggering, polarity);
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040093 if (irq < 0) {
94 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
95 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -040097
Bjorn Helgaasc32928c2006-07-03 00:24:10 -070098 if (shareable)
99 res->irq_resource[i].flags |= IORESOURCE_IRQ_SHAREABLE;
100
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400101 res->irq_resource[i].start = irq;
102 res->irq_resource[i].end = irq;
103 pcibios_penalize_isa_irq(irq, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static void
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500107pnpacpi_parse_allocated_dmaresource(struct pnp_resource_table *res, u32 dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 int i = 0;
Adrian Bunk0dec63b2005-05-01 08:59:30 -0700110 while (i < PNP_MAX_DMA &&
111 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 i++;
113 if (i < PNP_MAX_DMA) {
114 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
115 if (dma == -1) {
116 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
117 return;
118 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400119 res->dma_resource[i].start = dma;
120 res->dma_resource[i].end = dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122}
123
124static void
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500125pnpacpi_parse_allocated_ioresource(struct pnp_resource_table *res,
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500126 u64 io, u64 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int i = 0;
129 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) &&
130 i < PNP_MAX_PORT)
131 i++;
132 if (i < PNP_MAX_PORT) {
133 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
134 if (len <= 0 || (io + len -1) >= 0x10003) {
135 res->port_resource[i].flags |= IORESOURCE_DISABLED;
136 return;
137 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400138 res->port_resource[i].start = io;
139 res->port_resource[i].end = io + len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141}
142
143static void
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500144pnpacpi_parse_allocated_memresource(struct pnp_resource_table *res,
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400145 u64 mem, u64 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 int i = 0;
148 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) &&
149 (i < PNP_MAX_MEM))
150 i++;
151 if (i < PNP_MAX_MEM) {
152 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
153 if (len <= 0) {
154 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
155 return;
156 }
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400157 res->mem_resource[i].start = mem;
158 res->mem_resource[i].end = mem + len - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160}
161
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500162static void
163pnpacpi_parse_allocated_address_space(struct pnp_resource_table *res_table,
164 struct acpi_resource *res)
165{
166 struct acpi_resource_address64 addr, *p = &addr;
167 acpi_status status;
168
169 status = acpi_resource_to_address64(res, p);
170 if (!ACPI_SUCCESS(status)) {
171 pnp_warn("PnPACPI: failed to convert resource type %d",
172 res->type);
173 return;
174 }
175
matthieu castet2b8de5f2006-08-05 12:15:12 -0700176 if (p->producer_consumer == ACPI_PRODUCER)
177 return;
178
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500179 if (p->resource_type == ACPI_MEMORY_RANGE)
180 pnpacpi_parse_allocated_memresource(res_table,
181 p->minimum, p->address_length);
182 else if (p->resource_type == ACPI_IO_RANGE)
183 pnpacpi_parse_allocated_ioresource(res_table,
184 p->minimum, p->address_length);
185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
188 void *data)
189{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500190 struct pnp_resource_table *res_table = (struct pnp_resource_table *)data;
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400191 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Len Browneca008c2005-09-22 00:25:18 -0400193 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400194 case ACPI_RESOURCE_TYPE_IRQ:
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400195 /*
196 * Per spec, only one interrupt per descriptor is allowed in
197 * _CRS, but some firmware violates this, so parse them all.
198 */
Bob Moore50eca3e2005-09-30 19:03:00 -0400199 for (i = 0; i < res->data.irq.interrupt_count; i++) {
Bjorn Helgaasdbed12d2005-09-03 00:37:56 -0400200 pnpacpi_parse_allocated_irqresource(res_table,
201 res->data.irq.interrupts[i],
Bob Moore50eca3e2005-09-30 19:03:00 -0400202 res->data.irq.triggering,
Bjorn Helgaasc32928c2006-07-03 00:24:10 -0700203 res->data.irq.polarity,
204 res->data.irq.sharable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 break;
207
Bob Moore50eca3e2005-09-30 19:03:00 -0400208 case ACPI_RESOURCE_TYPE_DMA:
209 if (res->data.dma.channel_count > 0)
210 pnpacpi_parse_allocated_dmaresource(res_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 res->data.dma.channels[0]);
212 break;
Len Brown0af58532006-01-20 01:11:37 -0500213
Bob Moore50eca3e2005-09-30 19:03:00 -0400214 case ACPI_RESOURCE_TYPE_IO:
215 pnpacpi_parse_allocated_ioresource(res_table,
216 res->data.io.minimum,
217 res->data.io.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
Len Brown0af58532006-01-20 01:11:37 -0500219
220 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
221 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
222 break;
223
Bob Moore50eca3e2005-09-30 19:03:00 -0400224 case ACPI_RESOURCE_TYPE_FIXED_IO:
225 pnpacpi_parse_allocated_ioresource(res_table,
226 res->data.fixed_io.address,
227 res->data.fixed_io.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 break;
Len Brown0af58532006-01-20 01:11:37 -0500229
230 case ACPI_RESOURCE_TYPE_VENDOR:
231 break;
232
233 case ACPI_RESOURCE_TYPE_END_TAG:
234 break;
235
Bob Moore50eca3e2005-09-30 19:03:00 -0400236 case ACPI_RESOURCE_TYPE_MEMORY24:
237 pnpacpi_parse_allocated_memresource(res_table,
238 res->data.memory24.minimum,
239 res->data.memory24.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400241 case ACPI_RESOURCE_TYPE_MEMORY32:
242 pnpacpi_parse_allocated_memresource(res_table,
243 res->data.memory32.minimum,
244 res->data.memory32.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400246 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
247 pnpacpi_parse_allocated_memresource(res_table,
248 res->data.fixed_memory32.address,
249 res->data.fixed_memory32.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400251 case ACPI_RESOURCE_TYPE_ADDRESS16:
Bob Moore50eca3e2005-09-30 19:03:00 -0400252 case ACPI_RESOURCE_TYPE_ADDRESS32:
Bob Moore50eca3e2005-09-30 19:03:00 -0400253 case ACPI_RESOURCE_TYPE_ADDRESS64:
Bjorn Helgaas1acfb7f2006-03-28 17:03:00 -0500254 pnpacpi_parse_allocated_address_space(res_table, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
Len Brown0af58532006-01-20 01:11:37 -0500256
257 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
matthieu castet2b8de5f2006-08-05 12:15:12 -0700258 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
259 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
Len Brown0af58532006-01-20 01:11:37 -0500261
262 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
matthieu castet2b8de5f2006-08-05 12:15:12 -0700263 if (res->data.extended_irq.producer_consumer == ACPI_PRODUCER)
264 return AE_OK;
265
Len Brown0af58532006-01-20 01:11:37 -0500266 for (i = 0; i < res->data.extended_irq.interrupt_count; i++) {
267 pnpacpi_parse_allocated_irqresource(res_table,
268 res->data.extended_irq.interrupts[i],
269 res->data.extended_irq.triggering,
Bjorn Helgaasc32928c2006-07-03 00:24:10 -0700270 res->data.extended_irq.polarity,
271 res->data.extended_irq.sharable);
Len Brown0af58532006-01-20 01:11:37 -0500272 }
273 break;
274
275 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
276 break;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 default:
Len Browneca008c2005-09-22 00:25:18 -0400279 pnp_warn("PnPACPI: unknown resource type %d", res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return AE_ERROR;
281 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return AE_OK;
284}
285
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500286acpi_status pnpacpi_parse_allocated_resource(acpi_handle handle, struct pnp_resource_table *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 /* Blank the resource table values */
289 pnp_init_resource_table(res);
290
291 return acpi_walk_resources(handle, METHOD_NAME__CRS, pnpacpi_allocated_resource, res);
292}
293
294static void pnpacpi_parse_dma_option(struct pnp_option *option, struct acpi_resource_dma *p)
295{
296 int i;
297 struct pnp_dma * dma;
298
Bob Moore50eca3e2005-09-30 19:03:00 -0400299 if (p->channel_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700301 dma = kcalloc(1, sizeof(struct pnp_dma), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!dma)
303 return;
304
Bob Moore50eca3e2005-09-30 19:03:00 -0400305 for(i = 0; i < p->channel_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 dma->map |= 1 << p->channels[i];
307 dma->flags = 0;
308 if (p->bus_master)
309 dma->flags |= IORESOURCE_DMA_MASTER;
310 switch (p->type) {
311 case ACPI_COMPATIBILITY:
312 dma->flags |= IORESOURCE_DMA_COMPATIBLE;
313 break;
314 case ACPI_TYPE_A:
315 dma->flags |= IORESOURCE_DMA_TYPEA;
316 break;
317 case ACPI_TYPE_B:
318 dma->flags |= IORESOURCE_DMA_TYPEB;
319 break;
320 case ACPI_TYPE_F:
321 dma->flags |= IORESOURCE_DMA_TYPEF;
322 break;
323 default:
324 /* Set a default value ? */
325 dma->flags |= IORESOURCE_DMA_COMPATIBLE;
326 pnp_err("Invalid DMA type");
327 }
328 switch (p->transfer) {
329 case ACPI_TRANSFER_8:
330 dma->flags |= IORESOURCE_DMA_8BIT;
331 break;
332 case ACPI_TRANSFER_8_16:
333 dma->flags |= IORESOURCE_DMA_8AND16BIT;
334 break;
335 case ACPI_TRANSFER_16:
336 dma->flags |= IORESOURCE_DMA_16BIT;
337 break;
338 default:
339 /* Set a default value ? */
340 dma->flags |= IORESOURCE_DMA_8AND16BIT;
341 pnp_err("Invalid DMA transfer type");
342 }
343
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500344 pnp_register_dma_resource(option, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return;
346}
347
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static void pnpacpi_parse_irq_option(struct pnp_option *option,
350 struct acpi_resource_irq *p)
351{
352 int i;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500353 struct pnp_irq *irq;
354
Bob Moore50eca3e2005-09-30 19:03:00 -0400355 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700357 irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!irq)
359 return;
360
Bob Moore50eca3e2005-09-30 19:03:00 -0400361 for(i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (p->interrupts[i])
363 __set_bit(p->interrupts[i], irq->map);
Bob Moore50eca3e2005-09-30 19:03:00 -0400364 irq->flags = irq_flags(p->triggering, p->polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 pnp_register_irq_resource(option, irq);
367 return;
368}
369
370static void pnpacpi_parse_ext_irq_option(struct pnp_option *option,
Bob Moore50eca3e2005-09-30 19:03:00 -0400371 struct acpi_resource_extended_irq *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 int i;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500374 struct pnp_irq *irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Bob Moore50eca3e2005-09-30 19:03:00 -0400376 if (p->interrupt_count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700378 irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!irq)
380 return;
381
Bob Moore50eca3e2005-09-30 19:03:00 -0400382 for(i = 0; i < p->interrupt_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (p->interrupts[i])
384 __set_bit(p->interrupts[i], irq->map);
Bob Moore50eca3e2005-09-30 19:03:00 -0400385 irq->flags = irq_flags(p->triggering, p->polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 pnp_register_irq_resource(option, irq);
388 return;
389}
390
391static void
392pnpacpi_parse_port_option(struct pnp_option *option,
393 struct acpi_resource_io *io)
394{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500395 struct pnp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Bob Moore50eca3e2005-09-30 19:03:00 -0400397 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700399 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!port)
401 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400402 port->min = io->minimum;
403 port->max = io->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 port->align = io->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400405 port->size = io->address_length;
406 port->flags = ACPI_DECODE_16 == io->io_decode ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 PNP_PORT_FLAG_16BITADDR : 0;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500408 pnp_register_port_resource(option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return;
410}
411
412static void
413pnpacpi_parse_fixed_port_option(struct pnp_option *option,
414 struct acpi_resource_fixed_io *io)
415{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500416 struct pnp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Bob Moore50eca3e2005-09-30 19:03:00 -0400418 if (io->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700420 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!port)
422 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400423 port->min = port->max = io->address;
424 port->size = io->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 port->align = 0;
426 port->flags = PNP_PORT_FLAG_FIXED;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500427 pnp_register_port_resource(option, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return;
429}
430
431static void
432pnpacpi_parse_mem24_option(struct pnp_option *option,
Bob Moore50eca3e2005-09-30 19:03:00 -0400433 struct acpi_resource_memory24 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500435 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Bob Moore50eca3e2005-09-30 19:03:00 -0400437 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700439 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!mem)
441 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400442 mem->min = p->minimum;
443 mem->max = p->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 mem->align = p->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400445 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Bob Moore08978312005-10-21 00:00:00 -0400447 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 IORESOURCE_MEM_WRITEABLE : 0;
449
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500450 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return;
452}
453
454static void
455pnpacpi_parse_mem32_option(struct pnp_option *option,
Bob Moore50eca3e2005-09-30 19:03:00 -0400456 struct acpi_resource_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500458 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Bob Moore50eca3e2005-09-30 19:03:00 -0400460 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700462 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!mem)
464 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400465 mem->min = p->minimum;
466 mem->max = p->maximum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 mem->align = p->alignment;
Bob Moore50eca3e2005-09-30 19:03:00 -0400468 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Bob Moore08978312005-10-21 00:00:00 -0400470 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 IORESOURCE_MEM_WRITEABLE : 0;
472
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500473 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return;
475}
476
477static void
478pnpacpi_parse_fixed_mem32_option(struct pnp_option *option,
Bob Moore50eca3e2005-09-30 19:03:00 -0400479 struct acpi_resource_fixed_memory32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500481 struct pnp_mem *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Bob Moore50eca3e2005-09-30 19:03:00 -0400483 if (p->address_length == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700485 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!mem)
487 return;
Bob Moore50eca3e2005-09-30 19:03:00 -0400488 mem->min = mem->max = p->address;
489 mem->size = p->address_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 mem->align = 0;
491
Bob Moore08978312005-10-21 00:00:00 -0400492 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 IORESOURCE_MEM_WRITEABLE : 0;
494
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500495 pnp_register_mem_resource(option, mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return;
497}
498
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400499static void
500pnpacpi_parse_address_option(struct pnp_option *option, struct acpi_resource *r)
501{
502 struct acpi_resource_address64 addr, *p = &addr;
503 acpi_status status;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500504 struct pnp_mem *mem;
505 struct pnp_port *port;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400506
507 status = acpi_resource_to_address64(r, p);
508 if (!ACPI_SUCCESS(status)) {
Len Browned03f432006-01-07 03:50:18 -0500509 pnp_warn("PnPACPI: failed to convert resource type %d", r->type);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400510 return;
511 }
512
513 if (p->address_length == 0)
514 return;
515
516 if (p->resource_type == ACPI_MEMORY_RANGE) {
517 mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
518 if (!mem)
519 return;
Len Browned03f432006-01-07 03:50:18 -0500520 mem->min = mem->max = p->minimum;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400521 mem->size = p->address_length;
522 mem->align = 0;
Len Browned03f432006-01-07 03:50:18 -0500523 mem->flags = (p->info.mem.write_protect ==
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400524 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE : 0;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500525 pnp_register_mem_resource(option, mem);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400526 } else if (p->resource_type == ACPI_IO_RANGE) {
527 port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
528 if (!port)
529 return;
Len Browned03f432006-01-07 03:50:18 -0500530 port->min = port->max = p->minimum;
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400531 port->size = p->address_length;
532 port->align = 0;
533 port->flags = PNP_PORT_FLAG_FIXED;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500534 pnp_register_port_resource(option, port);
Bjorn Helgaas6f957ea2005-09-20 15:26:00 -0400535 }
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538struct acpipnp_parse_option_s {
539 struct pnp_option *option;
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500540 struct pnp_option *option_independent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct pnp_dev *dev;
542};
543
Bob Moore50eca3e2005-09-30 19:03:00 -0400544static acpi_status pnpacpi_option_resource(struct acpi_resource *res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 void *data)
546{
547 int priority = 0;
548 struct acpipnp_parse_option_s *parse_data = (struct acpipnp_parse_option_s *)data;
549 struct pnp_dev *dev = parse_data->dev;
550 struct pnp_option *option = parse_data->option;
551
Len Browneca008c2005-09-22 00:25:18 -0400552 switch (res->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400553 case ACPI_RESOURCE_TYPE_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 pnpacpi_parse_irq_option(option, &res->data.irq);
555 break;
Len Brown0af58532006-01-20 01:11:37 -0500556
Bob Moore50eca3e2005-09-30 19:03:00 -0400557 case ACPI_RESOURCE_TYPE_DMA:
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500558 pnpacpi_parse_dma_option(option, &res->data.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
Len Brown0af58532006-01-20 01:11:37 -0500560
Bob Moore50eca3e2005-09-30 19:03:00 -0400561 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 switch (res->data.start_dpf.compatibility_priority) {
563 case ACPI_GOOD_CONFIGURATION:
564 priority = PNP_RES_PRIORITY_PREFERRED;
565 break;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 case ACPI_ACCEPTABLE_CONFIGURATION:
568 priority = PNP_RES_PRIORITY_ACCEPTABLE;
569 break;
570
571 case ACPI_SUB_OPTIMAL_CONFIGURATION:
572 priority = PNP_RES_PRIORITY_FUNCTIONAL;
573 break;
574 default:
575 priority = PNP_RES_PRIORITY_INVALID;
576 break;
577 }
578 /* TBD: Considering performace/robustness bits */
579 option = pnp_register_dependent_option(dev, priority);
580 if (!option)
581 return AE_ERROR;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500582 parse_data->option = option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
Len Brown0af58532006-01-20 01:11:37 -0500584
Bob Moore50eca3e2005-09-30 19:03:00 -0400585 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500586 /*only one EndDependentFn is allowed*/
587 if (!parse_data->option_independent) {
588 pnp_warn("PnPACPI: more than one EndDependentFn");
589 return AE_ERROR;
590 }
591 parse_data->option = parse_data->option_independent;
592 parse_data->option_independent = NULL;
593 break;
Len Brown0af58532006-01-20 01:11:37 -0500594
595 case ACPI_RESOURCE_TYPE_IO:
596 pnpacpi_parse_port_option(option, &res->data.io);
597 break;
598
599 case ACPI_RESOURCE_TYPE_FIXED_IO:
600 pnpacpi_parse_fixed_port_option(option,
601 &res->data.fixed_io);
602 break;
603
604 case ACPI_RESOURCE_TYPE_VENDOR:
605 case ACPI_RESOURCE_TYPE_END_TAG:
606 break;
607
608 case ACPI_RESOURCE_TYPE_MEMORY24:
609 pnpacpi_parse_mem24_option(option, &res->data.memory24);
610 break;
611
612 case ACPI_RESOURCE_TYPE_MEMORY32:
613 pnpacpi_parse_mem32_option(option, &res->data.memory32);
614 break;
615
616 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
617 pnpacpi_parse_fixed_mem32_option(option,
618 &res->data.fixed_memory32);
619 break;
620
621 case ACPI_RESOURCE_TYPE_ADDRESS16:
622 case ACPI_RESOURCE_TYPE_ADDRESS32:
623 case ACPI_RESOURCE_TYPE_ADDRESS64:
624 pnpacpi_parse_address_option(option, res);
625 break;
626
627 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
628 break;
629
630 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
631 pnpacpi_parse_ext_irq_option(option,
632 &res->data.extended_irq);
633 break;
634
635 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
636 break;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 default:
Len Browneca008c2005-09-22 00:25:18 -0400639 pnp_warn("PnPACPI: unknown resource type %d", res->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return AE_ERROR;
641 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return AE_OK;
644}
645
Bob Moore50eca3e2005-09-30 19:03:00 -0400646acpi_status pnpacpi_parse_resource_option_data(acpi_handle handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct pnp_dev *dev)
648{
649 acpi_status status;
650 struct acpipnp_parse_option_s parse_data;
651
652 parse_data.option = pnp_register_independent_option(dev);
653 if (!parse_data.option)
654 return AE_ERROR;
Matthieu Castetb008b8d2005-03-25 12:03:15 -0500655 parse_data.option_independent = parse_data.option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 parse_data.dev = dev;
Bob Moore50eca3e2005-09-30 19:03:00 -0400657 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 pnpacpi_option_resource, &parse_data);
659
660 return status;
661}
662
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500663static int pnpacpi_supported_resource(struct acpi_resource *res)
664{
665 switch (res->type) {
666 case ACPI_RESOURCE_TYPE_IRQ:
667 case ACPI_RESOURCE_TYPE_DMA:
668 case ACPI_RESOURCE_TYPE_IO:
669 case ACPI_RESOURCE_TYPE_FIXED_IO:
670 case ACPI_RESOURCE_TYPE_MEMORY24:
671 case ACPI_RESOURCE_TYPE_MEMORY32:
672 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
673 case ACPI_RESOURCE_TYPE_ADDRESS16:
674 case ACPI_RESOURCE_TYPE_ADDRESS32:
675 case ACPI_RESOURCE_TYPE_ADDRESS64:
676 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
677 return 1;
678 }
679 return 0;
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/*
683 * Set resource
684 */
685static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
686 void *data)
687{
688 int *res_cnt = (int *)data;
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500689
690 if (pnpacpi_supported_resource(res))
691 (*res_cnt)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return AE_OK;
693}
694
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500695static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500697 struct acpi_resource **resource = (struct acpi_resource **)data;
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500698
699 if (pnpacpi_supported_resource(res)) {
Len Browneca008c2005-09-22 00:25:18 -0400700 (*resource)->type = res->type;
Bjorn Helgaasb5f24902006-03-28 17:04:00 -0500701 (*resource)->length = sizeof(struct acpi_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 (*resource)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 return AE_OK;
706}
707
Bob Moore50eca3e2005-09-30 19:03:00 -0400708int pnpacpi_build_resource_template(acpi_handle handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct acpi_buffer *buffer)
710{
711 struct acpi_resource *resource;
712 int res_cnt = 0;
713 acpi_status status;
714
Bob Moore50eca3e2005-09-30 19:03:00 -0400715 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 pnpacpi_count_resources, &res_cnt);
717 if (ACPI_FAILURE(status)) {
718 pnp_err("Evaluate _CRS failed");
719 return -EINVAL;
720 }
721 if (!res_cnt)
722 return -EINVAL;
723 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
Bjorn Helgaasa2822e72005-09-06 15:16:51 -0700724 buffer->pointer = kcalloc(1, buffer->length - 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!buffer->pointer)
726 return -ENOMEM;
727 pnp_dbg("Res cnt %d", res_cnt);
728 resource = (struct acpi_resource *)buffer->pointer;
Bob Moore50eca3e2005-09-30 19:03:00 -0400729 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 pnpacpi_type_resources, &resource);
731 if (ACPI_FAILURE(status)) {
732 kfree(buffer->pointer);
733 pnp_err("Evaluate _CRS failed");
734 return -EINVAL;
735 }
736 /* resource will pointer the end resource now */
Bob Moore50eca3e2005-09-30 19:03:00 -0400737 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 return 0;
740}
741
Bob Moore50eca3e2005-09-30 19:03:00 -0400742static void pnpacpi_encode_irq(struct acpi_resource *resource,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct resource *p)
744{
Bob Moore50eca3e2005-09-30 19:03:00 -0400745 int triggering, polarity;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500746
747 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
Bob Moore50eca3e2005-09-30 19:03:00 -0400748 resource->data.irq.triggering = triggering;
749 resource->data.irq.polarity = polarity;
750 if (triggering == ACPI_EDGE_SENSITIVE)
751 resource->data.irq.sharable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400753 resource->data.irq.sharable = ACPI_SHARED;
754 resource->data.irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 resource->data.irq.interrupts[0] = p->start;
756}
757
758static void pnpacpi_encode_ext_irq(struct acpi_resource *resource,
759 struct resource *p)
760{
Bob Moore50eca3e2005-09-30 19:03:00 -0400761 int triggering, polarity;
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500762
763 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 resource->data.extended_irq.producer_consumer = ACPI_CONSUMER;
Bob Moore50eca3e2005-09-30 19:03:00 -0400765 resource->data.extended_irq.triggering = triggering;
766 resource->data.extended_irq.polarity = polarity;
767 if (triggering == ACPI_EDGE_SENSITIVE)
768 resource->data.irq.sharable = ACPI_EXCLUSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 else
Bob Moore50eca3e2005-09-30 19:03:00 -0400770 resource->data.irq.sharable = ACPI_SHARED;
771 resource->data.extended_irq.interrupt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 resource->data.extended_irq.interrupts[0] = p->start;
773}
774
775static void pnpacpi_encode_dma(struct acpi_resource *resource,
776 struct resource *p)
777{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
Vojtech Pavlikccc4c7b2006-04-07 20:00:27 +0200779 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
780 case IORESOURCE_DMA_TYPEA:
781 resource->data.dma.type = ACPI_TYPE_A;
782 break;
783 case IORESOURCE_DMA_TYPEB:
784 resource->data.dma.type = ACPI_TYPE_B;
785 break;
786 case IORESOURCE_DMA_TYPEF:
787 resource->data.dma.type = ACPI_TYPE_F;
788 break;
789 default:
790 resource->data.dma.type = ACPI_COMPATIBILITY;
791 }
792
793 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
794 case IORESOURCE_DMA_8BIT:
795 resource->data.dma.transfer = ACPI_TRANSFER_8;
796 break;
797 case IORESOURCE_DMA_8AND16BIT:
798 resource->data.dma.transfer = ACPI_TRANSFER_8_16;
799 break;
800 default:
801 resource->data.dma.transfer = ACPI_TRANSFER_16;
802 }
803
804 resource->data.dma.bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
Bob Moore50eca3e2005-09-30 19:03:00 -0400805 resource->data.dma.channel_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 resource->data.dma.channels[0] = p->start;
807}
808
809static void pnpacpi_encode_io(struct acpi_resource *resource,
810 struct resource *p)
811{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
813 resource->data.io.io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR)?
Bob Moore50eca3e2005-09-30 19:03:00 -0400814 ACPI_DECODE_16 : ACPI_DECODE_10;
815 resource->data.io.minimum = p->start;
816 resource->data.io.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 resource->data.io.alignment = 0; /* Correct? */
Bob Moore50eca3e2005-09-30 19:03:00 -0400818 resource->data.io.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821static void pnpacpi_encode_fixed_io(struct acpi_resource *resource,
822 struct resource *p)
823{
Bob Moore50eca3e2005-09-30 19:03:00 -0400824 resource->data.fixed_io.address = p->start;
825 resource->data.fixed_io.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
828static void pnpacpi_encode_mem24(struct acpi_resource *resource,
829 struct resource *p)
830{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
Bob Moore08978312005-10-21 00:00:00 -0400832 resource->data.memory24.write_protect =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
834 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400835 resource->data.memory24.minimum = p->start;
836 resource->data.memory24.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 resource->data.memory24.alignment = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400838 resource->data.memory24.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841static void pnpacpi_encode_mem32(struct acpi_resource *resource,
842 struct resource *p)
843{
Bob Moore08978312005-10-21 00:00:00 -0400844 resource->data.memory32.write_protect =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
846 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400847 resource->data.memory32.minimum = p->start;
848 resource->data.memory32.maximum = p->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 resource->data.memory32.alignment = 0;
Bob Moore50eca3e2005-09-30 19:03:00 -0400850 resource->data.memory32.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853static void pnpacpi_encode_fixed_mem32(struct acpi_resource *resource,
854 struct resource *p)
855{
Bob Moore08978312005-10-21 00:00:00 -0400856 resource->data.fixed_memory32.write_protect =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
858 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
Bob Moore50eca3e2005-09-30 19:03:00 -0400859 resource->data.fixed_memory32.address = p->start;
860 resource->data.fixed_memory32.address_length = p->end - p->start + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Bob Moore50eca3e2005-09-30 19:03:00 -0400863int pnpacpi_encode_resources(struct pnp_resource_table *res_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct acpi_buffer *buffer)
865{
866 int i = 0;
867 /* pnpacpi_build_resource_template allocates extra mem */
868 int res_cnt = (buffer->length - 1)/sizeof(struct acpi_resource) - 1;
869 struct acpi_resource *resource = (struct acpi_resource*)buffer->pointer;
870 int port = 0, irq = 0, dma = 0, mem = 0;
871
872 pnp_dbg("res cnt %d", res_cnt);
873 while (i < res_cnt) {
Len Browneca008c2005-09-22 00:25:18 -0400874 switch(resource->type) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400875 case ACPI_RESOURCE_TYPE_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 pnp_dbg("Encode irq");
Bob Moore50eca3e2005-09-30 19:03:00 -0400877 pnpacpi_encode_irq(resource,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 &res_table->irq_resource[irq]);
879 irq++;
880 break;
881
Bob Moore50eca3e2005-09-30 19:03:00 -0400882 case ACPI_RESOURCE_TYPE_DMA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 pnp_dbg("Encode dma");
Bob Moore50eca3e2005-09-30 19:03:00 -0400884 pnpacpi_encode_dma(resource,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 &res_table->dma_resource[dma]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500886 dma++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400888 case ACPI_RESOURCE_TYPE_IO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 pnp_dbg("Encode io");
Bob Moore50eca3e2005-09-30 19:03:00 -0400890 pnpacpi_encode_io(resource,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 &res_table->port_resource[port]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500892 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400894 case ACPI_RESOURCE_TYPE_FIXED_IO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 pnp_dbg("Encode fixed io");
896 pnpacpi_encode_fixed_io(resource,
897 &res_table->port_resource[port]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500898 port++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400900 case ACPI_RESOURCE_TYPE_MEMORY24:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 pnp_dbg("Encode mem24");
902 pnpacpi_encode_mem24(resource,
903 &res_table->mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500904 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400906 case ACPI_RESOURCE_TYPE_MEMORY32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 pnp_dbg("Encode mem32");
908 pnpacpi_encode_mem32(resource,
909 &res_table->mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500910 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 break;
Bob Moore50eca3e2005-09-30 19:03:00 -0400912 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 pnp_dbg("Encode fixed mem32");
914 pnpacpi_encode_fixed_mem32(resource,
915 &res_table->mem_resource[mem]);
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500916 mem++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
Len Brown0af58532006-01-20 01:11:37 -0500918 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
919 pnp_dbg("Encode ext irq");
920 pnpacpi_encode_ext_irq(resource,
921 &res_table->irq_resource[irq]);
922 irq++;
923 break;
924 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
925 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
926 case ACPI_RESOURCE_TYPE_VENDOR:
927 case ACPI_RESOURCE_TYPE_END_TAG:
928 case ACPI_RESOURCE_TYPE_ADDRESS16:
929 case ACPI_RESOURCE_TYPE_ADDRESS32:
930 case ACPI_RESOURCE_TYPE_ADDRESS64:
931 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
932 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 default: /* other type */
Len Browneca008c2005-09-22 00:25:18 -0400934 pnp_warn("unknown resource type %d", resource->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -EINVAL;
936 }
Bjorn Helgaas1c6e7d02006-03-28 17:04:00 -0500937 resource++;
938 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940 return 0;
941}