x86_64: cleanup tsc.c merge artifact
[deliverable/linux.git] / drivers / pnp / pnpacpi / rsparser.c
CommitLineData
1da177e4
LT
1/*
2 * pnpacpi -- PnP ACPI driver
3 *
4 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
50eca3eb 6 *
1da177e4
LT
7 * 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 */
50eca3eb 35static int irq_flags(int triggering, int polarity)
1da177e4
LT
36{
37 int flag;
50eca3eb 38 if (triggering == ACPI_LEVEL_SENSITIVE) {
1c6e7d0a 39 if (polarity == ACPI_ACTIVE_LOW)
1da177e4
LT
40 flag = IORESOURCE_IRQ_LOWLEVEL;
41 else
42 flag = IORESOURCE_IRQ_HIGHLEVEL;
43 }
44 else {
1c6e7d0a 45 if (polarity == ACPI_ACTIVE_LOW)
1da177e4
LT
46 flag = IORESOURCE_IRQ_LOWEDGE;
47 else
48 flag = IORESOURCE_IRQ_HIGHEDGE;
49 }
50 return flag;
51}
52
50eca3eb 53static void decode_irq_flags(int flag, int *triggering, int *polarity)
1da177e4
LT
54{
55 switch (flag) {
56 case IORESOURCE_IRQ_LOWLEVEL:
50eca3eb
BM
57 *triggering = ACPI_LEVEL_SENSITIVE;
58 *polarity = ACPI_ACTIVE_LOW;
1da177e4 59 break;
1c6e7d0a 60 case IORESOURCE_IRQ_HIGHLEVEL:
50eca3eb
BM
61 *triggering = ACPI_LEVEL_SENSITIVE;
62 *polarity = ACPI_ACTIVE_HIGH;
1da177e4
LT
63 break;
64 case IORESOURCE_IRQ_LOWEDGE:
50eca3eb
BM
65 *triggering = ACPI_EDGE_SENSITIVE;
66 *polarity = ACPI_ACTIVE_LOW;
1da177e4
LT
67 break;
68 case IORESOURCE_IRQ_HIGHEDGE:
50eca3eb
BM
69 *triggering = ACPI_EDGE_SENSITIVE;
70 *polarity = ACPI_ACTIVE_HIGH;
1da177e4
LT
71 break;
72 }
73}
74
75static void
1c6e7d0a 76pnpacpi_parse_allocated_irqresource(struct pnp_resource_table *res, u32 gsi,
c32928c5 77 int triggering, int polarity, int shareable)
1da177e4
LT
78{
79 int i = 0;
dbed12da
BH
80 int irq;
81
82 if (!valid_IRQ(gsi))
83 return;
84
1da177e4
LT
85 while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) &&
86 i < PNP_MAX_IRQ)
87 i++;
dbed12da
BH
88 if (i >= PNP_MAX_IRQ)
89 return;
90
91 res->irq_resource[i].flags = IORESOURCE_IRQ; // Also clears _UNSET flag
362ea087 92 res->irq_resource[i].flags |= irq_flags(triggering, polarity);
50eca3eb 93 irq = acpi_register_gsi(gsi, triggering, polarity);
dbed12da
BH
94 if (irq < 0) {
95 res->irq_resource[i].flags |= IORESOURCE_DISABLED;
96 return;
1da177e4 97 }
dbed12da 98
c32928c5
BH
99 if (shareable)
100 res->irq_resource[i].flags |= IORESOURCE_IRQ_SHAREABLE;
101
dbed12da
BH
102 res->irq_resource[i].start = irq;
103 res->irq_resource[i].end = irq;
104 pcibios_penalize_isa_irq(irq, 1);
1da177e4
LT
105}
106
362ea087
MK
107static int dma_flags(int type, int bus_master, int transfer)
108{
109 int flags = 0;
110
111 if (bus_master)
112 flags |= IORESOURCE_DMA_MASTER;
113 switch (type) {
114 case ACPI_COMPATIBILITY:
115 flags |= IORESOURCE_DMA_COMPATIBLE;
116 break;
117 case ACPI_TYPE_A:
118 flags |= IORESOURCE_DMA_TYPEA;
119 break;
120 case ACPI_TYPE_B:
121 flags |= IORESOURCE_DMA_TYPEB;
122 break;
123 case ACPI_TYPE_F:
124 flags |= IORESOURCE_DMA_TYPEF;
125 break;
126 default:
127 /* Set a default value ? */
128 flags |= IORESOURCE_DMA_COMPATIBLE;
129 pnp_err("Invalid DMA type");
130 }
131 switch (transfer) {
132 case ACPI_TRANSFER_8:
133 flags |= IORESOURCE_DMA_8BIT;
134 break;
135 case ACPI_TRANSFER_8_16:
136 flags |= IORESOURCE_DMA_8AND16BIT;
137 break;
138 case ACPI_TRANSFER_16:
139 flags |= IORESOURCE_DMA_16BIT;
140 break;
141 default:
142 /* Set a default value ? */
143 flags |= IORESOURCE_DMA_8AND16BIT;
144 pnp_err("Invalid DMA transfer type");
145 }
146
147 return flags;
148}
149
1da177e4 150static void
362ea087
MK
151pnpacpi_parse_allocated_dmaresource(struct pnp_resource_table *res, u32 dma,
152 int type, int bus_master, int transfer)
1da177e4
LT
153{
154 int i = 0;
0dec63ba
AB
155 while (i < PNP_MAX_DMA &&
156 !(res->dma_resource[i].flags & IORESOURCE_UNSET))
1da177e4
LT
157 i++;
158 if (i < PNP_MAX_DMA) {
159 res->dma_resource[i].flags = IORESOURCE_DMA; // Also clears _UNSET flag
362ea087 160 res->dma_resource[i].flags |= dma_flags(type, bus_master, transfer);
1da177e4
LT
161 if (dma == -1) {
162 res->dma_resource[i].flags |= IORESOURCE_DISABLED;
163 return;
164 }
dbed12da
BH
165 res->dma_resource[i].start = dma;
166 res->dma_resource[i].end = dma;
1da177e4
LT
167 }
168}
169
170static void
1c6e7d0a 171pnpacpi_parse_allocated_ioresource(struct pnp_resource_table *res,
362ea087 172 u64 io, u64 len, int io_decode)
1da177e4
LT
173{
174 int i = 0;
175 while (!(res->port_resource[i].flags & IORESOURCE_UNSET) &&
176 i < PNP_MAX_PORT)
177 i++;
178 if (i < PNP_MAX_PORT) {
179 res->port_resource[i].flags = IORESOURCE_IO; // Also clears _UNSET flag
362ea087
MK
180 if (io_decode == ACPI_DECODE_16)
181 res->port_resource[i].flags |= PNP_PORT_FLAG_16BITADDR;
1da177e4
LT
182 if (len <= 0 || (io + len -1) >= 0x10003) {
183 res->port_resource[i].flags |= IORESOURCE_DISABLED;
184 return;
185 }
dbed12da
BH
186 res->port_resource[i].start = io;
187 res->port_resource[i].end = io + len - 1;
1da177e4
LT
188 }
189}
190
191static void
1c6e7d0a 192pnpacpi_parse_allocated_memresource(struct pnp_resource_table *res,
362ea087 193 u64 mem, u64 len, int write_protect)
1da177e4
LT
194{
195 int i = 0;
196 while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) &&
197 (i < PNP_MAX_MEM))
198 i++;
199 if (i < PNP_MAX_MEM) {
200 res->mem_resource[i].flags = IORESOURCE_MEM; // Also clears _UNSET flag
201 if (len <= 0) {
202 res->mem_resource[i].flags |= IORESOURCE_DISABLED;
203 return;
204 }
362ea087
MK
205 if(write_protect == ACPI_READ_WRITE_MEMORY)
206 res->mem_resource[i].flags |= IORESOURCE_MEM_WRITEABLE;
207
dbed12da
BH
208 res->mem_resource[i].start = mem;
209 res->mem_resource[i].end = mem + len - 1;
1da177e4
LT
210 }
211}
212
1acfb7f2
BH
213static void
214pnpacpi_parse_allocated_address_space(struct pnp_resource_table *res_table,
215 struct acpi_resource *res)
216{
217 struct acpi_resource_address64 addr, *p = &addr;
218 acpi_status status;
219
220 status = acpi_resource_to_address64(res, p);
221 if (!ACPI_SUCCESS(status)) {
222 pnp_warn("PnPACPI: failed to convert resource type %d",
223 res->type);
224 return;
225 }
226
2b8de5f5 227 if (p->producer_consumer == ACPI_PRODUCER)
228 return;
229
1acfb7f2
BH
230 if (p->resource_type == ACPI_MEMORY_RANGE)
231 pnpacpi_parse_allocated_memresource(res_table,
362ea087 232 p->minimum, p->address_length, p->info.mem.write_protect);
1acfb7f2
BH
233 else if (p->resource_type == ACPI_IO_RANGE)
234 pnpacpi_parse_allocated_ioresource(res_table,
362ea087
MK
235 p->minimum, p->address_length,
236 p->granularity == 0xfff ? ACPI_DECODE_10 : ACPI_DECODE_16);
1acfb7f2 237}
1da177e4
LT
238
239static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
240 void *data)
241{
1c6e7d0a 242 struct pnp_resource_table *res_table = (struct pnp_resource_table *)data;
dbed12da 243 int i;
1da177e4 244
eca008c8 245 switch (res->type) {
50eca3eb 246 case ACPI_RESOURCE_TYPE_IRQ:
dbed12da
BH
247 /*
248 * Per spec, only one interrupt per descriptor is allowed in
249 * _CRS, but some firmware violates this, so parse them all.
250 */
50eca3eb 251 for (i = 0; i < res->data.irq.interrupt_count; i++) {
dbed12da
BH
252 pnpacpi_parse_allocated_irqresource(res_table,
253 res->data.irq.interrupts[i],
50eca3eb 254 res->data.irq.triggering,
c32928c5
BH
255 res->data.irq.polarity,
256 res->data.irq.sharable);
1da177e4
LT
257 }
258 break;
259
50eca3eb
BM
260 case ACPI_RESOURCE_TYPE_DMA:
261 if (res->data.dma.channel_count > 0)
262 pnpacpi_parse_allocated_dmaresource(res_table,
362ea087
MK
263 res->data.dma.channels[0],
264 res->data.dma.type,
265 res->data.dma.bus_master,
266 res->data.dma.transfer);
1da177e4 267 break;
0af5853b 268
50eca3eb
BM
269 case ACPI_RESOURCE_TYPE_IO:
270 pnpacpi_parse_allocated_ioresource(res_table,
271 res->data.io.minimum,
362ea087
MK
272 res->data.io.address_length,
273 res->data.io.io_decode);
1da177e4 274 break;
0af5853b
LB
275
276 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
277 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
278 break;
279
50eca3eb
BM
280 case ACPI_RESOURCE_TYPE_FIXED_IO:
281 pnpacpi_parse_allocated_ioresource(res_table,
282 res->data.fixed_io.address,
362ea087
MK
283 res->data.fixed_io.address_length,
284 ACPI_DECODE_10);
1da177e4 285 break;
0af5853b
LB
286
287 case ACPI_RESOURCE_TYPE_VENDOR:
288 break;
289
290 case ACPI_RESOURCE_TYPE_END_TAG:
291 break;
292
50eca3eb
BM
293 case ACPI_RESOURCE_TYPE_MEMORY24:
294 pnpacpi_parse_allocated_memresource(res_table,
295 res->data.memory24.minimum,
362ea087
MK
296 res->data.memory24.address_length,
297 res->data.memory24.write_protect);
1da177e4 298 break;
50eca3eb
BM
299 case ACPI_RESOURCE_TYPE_MEMORY32:
300 pnpacpi_parse_allocated_memresource(res_table,
301 res->data.memory32.minimum,
362ea087
MK
302 res->data.memory32.address_length,
303 res->data.memory32.write_protect);
1da177e4 304 break;
50eca3eb
BM
305 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
306 pnpacpi_parse_allocated_memresource(res_table,
307 res->data.fixed_memory32.address,
362ea087
MK
308 res->data.fixed_memory32.address_length,
309 res->data.fixed_memory32.write_protect);
1da177e4 310 break;
50eca3eb 311 case ACPI_RESOURCE_TYPE_ADDRESS16:
50eca3eb 312 case ACPI_RESOURCE_TYPE_ADDRESS32:
50eca3eb 313 case ACPI_RESOURCE_TYPE_ADDRESS64:
1acfb7f2 314 pnpacpi_parse_allocated_address_space(res_table, res);
1da177e4 315 break;
0af5853b
LB
316
317 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
2b8de5f5 318 if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
319 return AE_OK;
0af5853b
LB
320 break;
321
322 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2b8de5f5 323 if (res->data.extended_irq.producer_consumer == ACPI_PRODUCER)
324 return AE_OK;
325
0af5853b
LB
326 for (i = 0; i < res->data.extended_irq.interrupt_count; i++) {
327 pnpacpi_parse_allocated_irqresource(res_table,
328 res->data.extended_irq.interrupts[i],
329 res->data.extended_irq.triggering,
c32928c5
BH
330 res->data.extended_irq.polarity,
331 res->data.extended_irq.sharable);
0af5853b
LB
332 }
333 break;
334
335 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1da177e4 336 break;
0af5853b 337
1da177e4 338 default:
eca008c8 339 pnp_warn("PnPACPI: unknown resource type %d", res->type);
1da177e4
LT
340 return AE_ERROR;
341 }
1c6e7d0a 342
1da177e4
LT
343 return AE_OK;
344}
345
1c6e7d0a 346acpi_status pnpacpi_parse_allocated_resource(acpi_handle handle, struct pnp_resource_table *res)
1da177e4
LT
347{
348 /* Blank the resource table values */
349 pnp_init_resource_table(res);
350
351 return acpi_walk_resources(handle, METHOD_NAME__CRS, pnpacpi_allocated_resource, res);
352}
353
354static void pnpacpi_parse_dma_option(struct pnp_option *option, struct acpi_resource_dma *p)
355{
356 int i;
357 struct pnp_dma * dma;
358
50eca3eb 359 if (p->channel_count == 0)
1da177e4 360 return;
cd861280 361 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
1da177e4
LT
362 if (!dma)
363 return;
364
50eca3eb 365 for(i = 0; i < p->channel_count; i++)
1da177e4 366 dma->map |= 1 << p->channels[i];
362ea087
MK
367
368 dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
1da177e4 369
1c6e7d0a 370 pnp_register_dma_resource(option, dma);
1da177e4
LT
371 return;
372}
373
1c6e7d0a 374
1da177e4
LT
375static void pnpacpi_parse_irq_option(struct pnp_option *option,
376 struct acpi_resource_irq *p)
377{
378 int i;
1c6e7d0a
BH
379 struct pnp_irq *irq;
380
50eca3eb 381 if (p->interrupt_count == 0)
1da177e4 382 return;
cd861280 383 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
1da177e4
LT
384 if (!irq)
385 return;
386
50eca3eb 387 for(i = 0; i < p->interrupt_count; i++)
1da177e4
LT
388 if (p->interrupts[i])
389 __set_bit(p->interrupts[i], irq->map);
50eca3eb 390 irq->flags = irq_flags(p->triggering, p->polarity);
1da177e4
LT
391
392 pnp_register_irq_resource(option, irq);
393 return;
394}
395
396static void pnpacpi_parse_ext_irq_option(struct pnp_option *option,
50eca3eb 397 struct acpi_resource_extended_irq *p)
1da177e4
LT
398{
399 int i;
1c6e7d0a 400 struct pnp_irq *irq;
1da177e4 401
50eca3eb 402 if (p->interrupt_count == 0)
1da177e4 403 return;
cd861280 404 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
1da177e4
LT
405 if (!irq)
406 return;
407
50eca3eb 408 for(i = 0; i < p->interrupt_count; i++)
1da177e4
LT
409 if (p->interrupts[i])
410 __set_bit(p->interrupts[i], irq->map);
50eca3eb 411 irq->flags = irq_flags(p->triggering, p->polarity);
1da177e4
LT
412
413 pnp_register_irq_resource(option, irq);
414 return;
415}
416
417static void
418pnpacpi_parse_port_option(struct pnp_option *option,
419 struct acpi_resource_io *io)
420{
1c6e7d0a 421 struct pnp_port *port;
1da177e4 422
50eca3eb 423 if (io->address_length == 0)
1da177e4 424 return;
cd861280 425 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
1da177e4
LT
426 if (!port)
427 return;
50eca3eb
BM
428 port->min = io->minimum;
429 port->max = io->maximum;
1da177e4 430 port->align = io->alignment;
50eca3eb
BM
431 port->size = io->address_length;
432 port->flags = ACPI_DECODE_16 == io->io_decode ?
1da177e4 433 PNP_PORT_FLAG_16BITADDR : 0;
1c6e7d0a 434 pnp_register_port_resource(option, port);
1da177e4
LT
435 return;
436}
437
438static void
439pnpacpi_parse_fixed_port_option(struct pnp_option *option,
440 struct acpi_resource_fixed_io *io)
441{
1c6e7d0a 442 struct pnp_port *port;
1da177e4 443
50eca3eb 444 if (io->address_length == 0)
1da177e4 445 return;
cd861280 446 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
1da177e4
LT
447 if (!port)
448 return;
50eca3eb
BM
449 port->min = port->max = io->address;
450 port->size = io->address_length;
1da177e4
LT
451 port->align = 0;
452 port->flags = PNP_PORT_FLAG_FIXED;
1c6e7d0a 453 pnp_register_port_resource(option, port);
1da177e4
LT
454 return;
455}
456
457static void
458pnpacpi_parse_mem24_option(struct pnp_option *option,
50eca3eb 459 struct acpi_resource_memory24 *p)
1da177e4 460{
1c6e7d0a 461 struct pnp_mem *mem;
1da177e4 462
50eca3eb 463 if (p->address_length == 0)
1da177e4 464 return;
cd861280 465 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
1da177e4
LT
466 if (!mem)
467 return;
50eca3eb
BM
468 mem->min = p->minimum;
469 mem->max = p->maximum;
1da177e4 470 mem->align = p->alignment;
50eca3eb 471 mem->size = p->address_length;
1da177e4 472
0897831b 473 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
1da177e4
LT
474 IORESOURCE_MEM_WRITEABLE : 0;
475
1c6e7d0a 476 pnp_register_mem_resource(option, mem);
1da177e4
LT
477 return;
478}
479
480static void
481pnpacpi_parse_mem32_option(struct pnp_option *option,
50eca3eb 482 struct acpi_resource_memory32 *p)
1da177e4 483{
1c6e7d0a 484 struct pnp_mem *mem;
1da177e4 485
50eca3eb 486 if (p->address_length == 0)
1da177e4 487 return;
cd861280 488 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
1da177e4
LT
489 if (!mem)
490 return;
50eca3eb
BM
491 mem->min = p->minimum;
492 mem->max = p->maximum;
1da177e4 493 mem->align = p->alignment;
50eca3eb 494 mem->size = p->address_length;
1da177e4 495
0897831b 496 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
1da177e4
LT
497 IORESOURCE_MEM_WRITEABLE : 0;
498
1c6e7d0a 499 pnp_register_mem_resource(option, mem);
1da177e4
LT
500 return;
501}
502
503static void
504pnpacpi_parse_fixed_mem32_option(struct pnp_option *option,
50eca3eb 505 struct acpi_resource_fixed_memory32 *p)
1da177e4 506{
1c6e7d0a 507 struct pnp_mem *mem;
1da177e4 508
50eca3eb 509 if (p->address_length == 0)
1da177e4 510 return;
cd861280 511 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
1da177e4
LT
512 if (!mem)
513 return;
50eca3eb
BM
514 mem->min = mem->max = p->address;
515 mem->size = p->address_length;
1da177e4
LT
516 mem->align = 0;
517
0897831b 518 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
1da177e4
LT
519 IORESOURCE_MEM_WRITEABLE : 0;
520
1c6e7d0a 521 pnp_register_mem_resource(option, mem);
1da177e4
LT
522 return;
523}
524
6f957eaf
BH
525static void
526pnpacpi_parse_address_option(struct pnp_option *option, struct acpi_resource *r)
527{
528 struct acpi_resource_address64 addr, *p = &addr;
529 acpi_status status;
1c6e7d0a
BH
530 struct pnp_mem *mem;
531 struct pnp_port *port;
6f957eaf
BH
532
533 status = acpi_resource_to_address64(r, p);
534 if (!ACPI_SUCCESS(status)) {
ed03f430 535 pnp_warn("PnPACPI: failed to convert resource type %d", r->type);
6f957eaf
BH
536 return;
537 }
538
539 if (p->address_length == 0)
540 return;
541
542 if (p->resource_type == ACPI_MEMORY_RANGE) {
cd861280 543 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
6f957eaf
BH
544 if (!mem)
545 return;
ed03f430 546 mem->min = mem->max = p->minimum;
6f957eaf
BH
547 mem->size = p->address_length;
548 mem->align = 0;
ed03f430 549 mem->flags = (p->info.mem.write_protect ==
6f957eaf 550 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE : 0;
1c6e7d0a 551 pnp_register_mem_resource(option, mem);
6f957eaf 552 } else if (p->resource_type == ACPI_IO_RANGE) {
cd861280 553 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
6f957eaf
BH
554 if (!port)
555 return;
ed03f430 556 port->min = port->max = p->minimum;
6f957eaf
BH
557 port->size = p->address_length;
558 port->align = 0;
559 port->flags = PNP_PORT_FLAG_FIXED;
1c6e7d0a 560 pnp_register_port_resource(option, port);
6f957eaf
BH
561 }
562}
563
1da177e4
LT
564struct acpipnp_parse_option_s {
565 struct pnp_option *option;
b008b8d7 566 struct pnp_option *option_independent;
1da177e4
LT
567 struct pnp_dev *dev;
568};
569
50eca3eb 570static acpi_status pnpacpi_option_resource(struct acpi_resource *res,
1da177e4
LT
571 void *data)
572{
573 int priority = 0;
574 struct acpipnp_parse_option_s *parse_data = (struct acpipnp_parse_option_s *)data;
575 struct pnp_dev *dev = parse_data->dev;
576 struct pnp_option *option = parse_data->option;
577
eca008c8 578 switch (res->type) {
50eca3eb 579 case ACPI_RESOURCE_TYPE_IRQ:
1da177e4
LT
580 pnpacpi_parse_irq_option(option, &res->data.irq);
581 break;
0af5853b 582
50eca3eb 583 case ACPI_RESOURCE_TYPE_DMA:
1c6e7d0a 584 pnpacpi_parse_dma_option(option, &res->data.dma);
1da177e4 585 break;
0af5853b 586
50eca3eb 587 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
1da177e4
LT
588 switch (res->data.start_dpf.compatibility_priority) {
589 case ACPI_GOOD_CONFIGURATION:
590 priority = PNP_RES_PRIORITY_PREFERRED;
591 break;
1c6e7d0a 592
1da177e4
LT
593 case ACPI_ACCEPTABLE_CONFIGURATION:
594 priority = PNP_RES_PRIORITY_ACCEPTABLE;
595 break;
596
597 case ACPI_SUB_OPTIMAL_CONFIGURATION:
598 priority = PNP_RES_PRIORITY_FUNCTIONAL;
599 break;
600 default:
601 priority = PNP_RES_PRIORITY_INVALID;
602 break;
603 }
604 /* TBD: Considering performace/robustness bits */
605 option = pnp_register_dependent_option(dev, priority);
606 if (!option)
607 return AE_ERROR;
1c6e7d0a 608 parse_data->option = option;
1da177e4 609 break;
0af5853b 610
50eca3eb 611 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
b008b8d7
MC
612 /*only one EndDependentFn is allowed*/
613 if (!parse_data->option_independent) {
614 pnp_warn("PnPACPI: more than one EndDependentFn");
615 return AE_ERROR;
616 }
617 parse_data->option = parse_data->option_independent;
618 parse_data->option_independent = NULL;
619 break;
0af5853b
LB
620
621 case ACPI_RESOURCE_TYPE_IO:
622 pnpacpi_parse_port_option(option, &res->data.io);
623 break;
624
625 case ACPI_RESOURCE_TYPE_FIXED_IO:
626 pnpacpi_parse_fixed_port_option(option,
627 &res->data.fixed_io);
628 break;
629
630 case ACPI_RESOURCE_TYPE_VENDOR:
631 case ACPI_RESOURCE_TYPE_END_TAG:
632 break;
633
634 case ACPI_RESOURCE_TYPE_MEMORY24:
635 pnpacpi_parse_mem24_option(option, &res->data.memory24);
636 break;
637
638 case ACPI_RESOURCE_TYPE_MEMORY32:
639 pnpacpi_parse_mem32_option(option, &res->data.memory32);
640 break;
641
642 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
643 pnpacpi_parse_fixed_mem32_option(option,
644 &res->data.fixed_memory32);
645 break;
646
647 case ACPI_RESOURCE_TYPE_ADDRESS16:
648 case ACPI_RESOURCE_TYPE_ADDRESS32:
649 case ACPI_RESOURCE_TYPE_ADDRESS64:
650 pnpacpi_parse_address_option(option, res);
651 break;
652
653 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
654 break;
655
656 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
657 pnpacpi_parse_ext_irq_option(option,
658 &res->data.extended_irq);
659 break;
660
661 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
662 break;
663
1da177e4 664 default:
eca008c8 665 pnp_warn("PnPACPI: unknown resource type %d", res->type);
1da177e4
LT
666 return AE_ERROR;
667 }
1c6e7d0a 668
1da177e4
LT
669 return AE_OK;
670}
671
50eca3eb 672acpi_status pnpacpi_parse_resource_option_data(acpi_handle handle,
1da177e4
LT
673 struct pnp_dev *dev)
674{
675 acpi_status status;
676 struct acpipnp_parse_option_s parse_data;
677
678 parse_data.option = pnp_register_independent_option(dev);
679 if (!parse_data.option)
680 return AE_ERROR;
b008b8d7 681 parse_data.option_independent = parse_data.option;
1da177e4 682 parse_data.dev = dev;
50eca3eb 683 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
1da177e4
LT
684 pnpacpi_option_resource, &parse_data);
685
686 return status;
687}
688
b5f2490b 689static int pnpacpi_supported_resource(struct acpi_resource *res)
1da177e4 690{
eca008c8 691 switch (res->type) {
50eca3eb 692 case ACPI_RESOURCE_TYPE_IRQ:
50eca3eb
BM
693 case ACPI_RESOURCE_TYPE_DMA:
694 case ACPI_RESOURCE_TYPE_IO:
695 case ACPI_RESOURCE_TYPE_FIXED_IO:
696 case ACPI_RESOURCE_TYPE_MEMORY24:
697 case ACPI_RESOURCE_TYPE_MEMORY32:
698 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
50eca3eb
BM
699 case ACPI_RESOURCE_TYPE_ADDRESS16:
700 case ACPI_RESOURCE_TYPE_ADDRESS32:
701 case ACPI_RESOURCE_TYPE_ADDRESS64:
0af5853b 702 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
b5f2490b 703 return 1;
1da177e4 704 }
b5f2490b
BH
705 return 0;
706}
707
708/*
709 * Set resource
710 */
711static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
712 void *data)
713{
714 int *res_cnt = (int *)data;
715
716 if (pnpacpi_supported_resource(res))
717 (*res_cnt)++;
1da177e4
LT
718 return AE_OK;
719}
720
1c6e7d0a 721static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
1da177e4 722{
1c6e7d0a 723 struct acpi_resource **resource = (struct acpi_resource **)data;
b5f2490b
BH
724
725 if (pnpacpi_supported_resource(res)) {
eca008c8 726 (*resource)->type = res->type;
b5f2490b 727 (*resource)->length = sizeof(struct acpi_resource);
1da177e4 728 (*resource)++;
1da177e4
LT
729 }
730
731 return AE_OK;
732}
733
50eca3eb 734int pnpacpi_build_resource_template(acpi_handle handle,
1da177e4
LT
735 struct acpi_buffer *buffer)
736{
737 struct acpi_resource *resource;
738 int res_cnt = 0;
739 acpi_status status;
740
50eca3eb 741 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1da177e4
LT
742 pnpacpi_count_resources, &res_cnt);
743 if (ACPI_FAILURE(status)) {
744 pnp_err("Evaluate _CRS failed");
745 return -EINVAL;
746 }
747 if (!res_cnt)
748 return -EINVAL;
749 buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
cd861280 750 buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
1da177e4
LT
751 if (!buffer->pointer)
752 return -ENOMEM;
753 pnp_dbg("Res cnt %d", res_cnt);
754 resource = (struct acpi_resource *)buffer->pointer;
50eca3eb 755 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1da177e4
LT
756 pnpacpi_type_resources, &resource);
757 if (ACPI_FAILURE(status)) {
758 kfree(buffer->pointer);
759 pnp_err("Evaluate _CRS failed");
760 return -EINVAL;
761 }
762 /* resource will pointer the end resource now */
50eca3eb 763 resource->type = ACPI_RESOURCE_TYPE_END_TAG;
1da177e4
LT
764
765 return 0;
766}
767
50eca3eb 768static void pnpacpi_encode_irq(struct acpi_resource *resource,
1da177e4
LT
769 struct resource *p)
770{
50eca3eb 771 int triggering, polarity;
1c6e7d0a
BH
772
773 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
50eca3eb
BM
774 resource->data.irq.triggering = triggering;
775 resource->data.irq.polarity = polarity;
776 if (triggering == ACPI_EDGE_SENSITIVE)
777 resource->data.irq.sharable = ACPI_EXCLUSIVE;
1da177e4 778 else
50eca3eb
BM
779 resource->data.irq.sharable = ACPI_SHARED;
780 resource->data.irq.interrupt_count = 1;
1da177e4
LT
781 resource->data.irq.interrupts[0] = p->start;
782}
783
784static void pnpacpi_encode_ext_irq(struct acpi_resource *resource,
785 struct resource *p)
786{
50eca3eb 787 int triggering, polarity;
1c6e7d0a
BH
788
789 decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
1da177e4 790 resource->data.extended_irq.producer_consumer = ACPI_CONSUMER;
50eca3eb
BM
791 resource->data.extended_irq.triggering = triggering;
792 resource->data.extended_irq.polarity = polarity;
793 if (triggering == ACPI_EDGE_SENSITIVE)
794 resource->data.irq.sharable = ACPI_EXCLUSIVE;
1da177e4 795 else
50eca3eb
BM
796 resource->data.irq.sharable = ACPI_SHARED;
797 resource->data.extended_irq.interrupt_count = 1;
1da177e4
LT
798 resource->data.extended_irq.interrupts[0] = p->start;
799}
800
801static void pnpacpi_encode_dma(struct acpi_resource *resource,
802 struct resource *p)
803{
1da177e4 804 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
ccc4c7bb
VP
805 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
806 case IORESOURCE_DMA_TYPEA:
807 resource->data.dma.type = ACPI_TYPE_A;
808 break;
809 case IORESOURCE_DMA_TYPEB:
810 resource->data.dma.type = ACPI_TYPE_B;
811 break;
812 case IORESOURCE_DMA_TYPEF:
813 resource->data.dma.type = ACPI_TYPE_F;
814 break;
815 default:
816 resource->data.dma.type = ACPI_COMPATIBILITY;
817 }
818
819 switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
820 case IORESOURCE_DMA_8BIT:
821 resource->data.dma.transfer = ACPI_TRANSFER_8;
822 break;
823 case IORESOURCE_DMA_8AND16BIT:
824 resource->data.dma.transfer = ACPI_TRANSFER_8_16;
825 break;
826 default:
827 resource->data.dma.transfer = ACPI_TRANSFER_16;
828 }
829
830 resource->data.dma.bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
50eca3eb 831 resource->data.dma.channel_count = 1;
1da177e4
LT
832 resource->data.dma.channels[0] = p->start;
833}
834
835static void pnpacpi_encode_io(struct acpi_resource *resource,
836 struct resource *p)
837{
1da177e4
LT
838 /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
839 resource->data.io.io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR)?
50eca3eb
BM
840 ACPI_DECODE_16 : ACPI_DECODE_10;
841 resource->data.io.minimum = p->start;
842 resource->data.io.maximum = p->end;
1da177e4 843 resource->data.io.alignment = 0; /* Correct? */
50eca3eb 844 resource->data.io.address_length = p->end - p->start + 1;
1da177e4
LT
845}
846
847static void pnpacpi_encode_fixed_io(struct acpi_resource *resource,
848 struct resource *p)
849{
50eca3eb
BM
850 resource->data.fixed_io.address = p->start;
851 resource->data.fixed_io.address_length = p->end - p->start + 1;
1da177e4
LT
852}
853
854static void pnpacpi_encode_mem24(struct acpi_resource *resource,
855 struct resource *p)
856{
1da177e4 857 /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
0897831b 858 resource->data.memory24.write_protect =
1da177e4
LT
859 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
860 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
50eca3eb
BM
861 resource->data.memory24.minimum = p->start;
862 resource->data.memory24.maximum = p->end;
1da177e4 863 resource->data.memory24.alignment = 0;
50eca3eb 864 resource->data.memory24.address_length = p->end - p->start + 1;
1da177e4
LT
865}
866
867static void pnpacpi_encode_mem32(struct acpi_resource *resource,
868 struct resource *p)
869{
0897831b 870 resource->data.memory32.write_protect =
1da177e4
LT
871 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
872 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
50eca3eb
BM
873 resource->data.memory32.minimum = p->start;
874 resource->data.memory32.maximum = p->end;
1da177e4 875 resource->data.memory32.alignment = 0;
50eca3eb 876 resource->data.memory32.address_length = p->end - p->start + 1;
1da177e4
LT
877}
878
879static void pnpacpi_encode_fixed_mem32(struct acpi_resource *resource,
880 struct resource *p)
881{
0897831b 882 resource->data.fixed_memory32.write_protect =
1da177e4
LT
883 (p->flags & IORESOURCE_MEM_WRITEABLE) ?
884 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
50eca3eb
BM
885 resource->data.fixed_memory32.address = p->start;
886 resource->data.fixed_memory32.address_length = p->end - p->start + 1;
1da177e4
LT
887}
888
50eca3eb 889int pnpacpi_encode_resources(struct pnp_resource_table *res_table,
1da177e4
LT
890 struct acpi_buffer *buffer)
891{
892 int i = 0;
893 /* pnpacpi_build_resource_template allocates extra mem */
894 int res_cnt = (buffer->length - 1)/sizeof(struct acpi_resource) - 1;
895 struct acpi_resource *resource = (struct acpi_resource*)buffer->pointer;
896 int port = 0, irq = 0, dma = 0, mem = 0;
897
898 pnp_dbg("res cnt %d", res_cnt);
899 while (i < res_cnt) {
eca008c8 900 switch(resource->type) {
50eca3eb 901 case ACPI_RESOURCE_TYPE_IRQ:
1da177e4 902 pnp_dbg("Encode irq");
50eca3eb 903 pnpacpi_encode_irq(resource,
1da177e4
LT
904 &res_table->irq_resource[irq]);
905 irq++;
906 break;
907
50eca3eb 908 case ACPI_RESOURCE_TYPE_DMA:
1da177e4 909 pnp_dbg("Encode dma");
50eca3eb 910 pnpacpi_encode_dma(resource,
1da177e4 911 &res_table->dma_resource[dma]);
1c6e7d0a 912 dma++;
1da177e4 913 break;
50eca3eb 914 case ACPI_RESOURCE_TYPE_IO:
1da177e4 915 pnp_dbg("Encode io");
50eca3eb 916 pnpacpi_encode_io(resource,
1da177e4 917 &res_table->port_resource[port]);
1c6e7d0a 918 port++;
1da177e4 919 break;
50eca3eb 920 case ACPI_RESOURCE_TYPE_FIXED_IO:
1da177e4
LT
921 pnp_dbg("Encode fixed io");
922 pnpacpi_encode_fixed_io(resource,
923 &res_table->port_resource[port]);
1c6e7d0a 924 port++;
1da177e4 925 break;
50eca3eb 926 case ACPI_RESOURCE_TYPE_MEMORY24:
1da177e4
LT
927 pnp_dbg("Encode mem24");
928 pnpacpi_encode_mem24(resource,
929 &res_table->mem_resource[mem]);
1c6e7d0a 930 mem++;
1da177e4 931 break;
50eca3eb 932 case ACPI_RESOURCE_TYPE_MEMORY32:
1da177e4
LT
933 pnp_dbg("Encode mem32");
934 pnpacpi_encode_mem32(resource,
935 &res_table->mem_resource[mem]);
1c6e7d0a 936 mem++;
1da177e4 937 break;
50eca3eb 938 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1da177e4
LT
939 pnp_dbg("Encode fixed mem32");
940 pnpacpi_encode_fixed_mem32(resource,
941 &res_table->mem_resource[mem]);
1c6e7d0a 942 mem++;
1da177e4 943 break;
0af5853b
LB
944 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
945 pnp_dbg("Encode ext irq");
946 pnpacpi_encode_ext_irq(resource,
947 &res_table->irq_resource[irq]);
948 irq++;
949 break;
950 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
951 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
952 case ACPI_RESOURCE_TYPE_VENDOR:
953 case ACPI_RESOURCE_TYPE_END_TAG:
954 case ACPI_RESOURCE_TYPE_ADDRESS16:
955 case ACPI_RESOURCE_TYPE_ADDRESS32:
956 case ACPI_RESOURCE_TYPE_ADDRESS64:
957 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
958 case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
1da177e4 959 default: /* other type */
eca008c8 960 pnp_warn("unknown resource type %d", resource->type);
1da177e4
LT
961 return -EINVAL;
962 }
1c6e7d0a
BH
963 resource++;
964 i++;
1da177e4
LT
965 }
966 return 0;
967}
This page took 0.24196 seconds and 5 git commands to generate.