rsparser.c 24.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * pnpacpi -- PnP ACPI driver
 *
 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
B
Bob Moore 已提交
6
 *
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <linux/kernel.h>
#include <linux/acpi.h>
#include <linux/pci.h>
#include "pnpacpi.h"

#ifdef CONFIG_IA64
#define valid_IRQ(i) (1)
#else
#define valid_IRQ(i) (((i) != 0) && ((i) != 2))
#endif

/*
 * Allocated Resources
 */
B
Bob Moore 已提交
35
static int irq_flags(int triggering, int polarity)
L
Linus Torvalds 已提交
36 37
{
	int flag;
B
Bob Moore 已提交
38
	if (triggering == ACPI_LEVEL_SENSITIVE) {
B
Bjorn Helgaas 已提交
39
		if (polarity == ACPI_ACTIVE_LOW)
L
Linus Torvalds 已提交
40 41 42 43 44
			flag = IORESOURCE_IRQ_LOWLEVEL;
		else
			flag = IORESOURCE_IRQ_HIGHLEVEL;
	}
	else {
B
Bjorn Helgaas 已提交
45
		if (polarity == ACPI_ACTIVE_LOW)
L
Linus Torvalds 已提交
46 47 48 49 50 51 52
			flag = IORESOURCE_IRQ_LOWEDGE;
		else
			flag = IORESOURCE_IRQ_HIGHEDGE;
	}
	return flag;
}

B
Bob Moore 已提交
53
static void decode_irq_flags(int flag, int *triggering, int *polarity)
L
Linus Torvalds 已提交
54 55 56
{
	switch (flag) {
	case IORESOURCE_IRQ_LOWLEVEL:
B
Bob Moore 已提交
57 58
		*triggering = ACPI_LEVEL_SENSITIVE;
		*polarity = ACPI_ACTIVE_LOW;
L
Linus Torvalds 已提交
59
		break;
B
Bjorn Helgaas 已提交
60
	case IORESOURCE_IRQ_HIGHLEVEL:
B
Bob Moore 已提交
61 62
		*triggering = ACPI_LEVEL_SENSITIVE;
		*polarity = ACPI_ACTIVE_HIGH;
L
Linus Torvalds 已提交
63 64
		break;
	case IORESOURCE_IRQ_LOWEDGE:
B
Bob Moore 已提交
65 66
		*triggering = ACPI_EDGE_SENSITIVE;
		*polarity = ACPI_ACTIVE_LOW;
L
Linus Torvalds 已提交
67 68
		break;
	case IORESOURCE_IRQ_HIGHEDGE:
B
Bob Moore 已提交
69 70
		*triggering = ACPI_EDGE_SENSITIVE;
		*polarity = ACPI_ACTIVE_HIGH;
L
Linus Torvalds 已提交
71 72 73 74 75
		break;
	}
}

static void
B
Bjorn Helgaas 已提交
76
pnpacpi_parse_allocated_irqresource(struct pnp_resource_table *res, u32 gsi,
77
	int triggering, int polarity, int shareable)
L
Linus Torvalds 已提交
78 79
{
	int i = 0;
80 81 82 83 84
	int irq;

	if (!valid_IRQ(gsi))
		return;

L
Linus Torvalds 已提交
85 86 87
	while (!(res->irq_resource[i].flags & IORESOURCE_UNSET) &&
			i < PNP_MAX_IRQ)
		i++;
88 89 90 91
	if (i >= PNP_MAX_IRQ)
		return;

	res->irq_resource[i].flags = IORESOURCE_IRQ;  // Also clears _UNSET flag
B
Bob Moore 已提交
92
	irq = acpi_register_gsi(gsi, triggering, polarity);
93 94 95
	if (irq < 0) {
		res->irq_resource[i].flags |= IORESOURCE_DISABLED;
		return;
L
Linus Torvalds 已提交
96
	}
97

98 99 100
	if (shareable)
		res->irq_resource[i].flags |= IORESOURCE_IRQ_SHAREABLE;

101 102 103
	res->irq_resource[i].start = irq;
	res->irq_resource[i].end = irq;
	pcibios_penalize_isa_irq(irq, 1);
L
Linus Torvalds 已提交
104 105 106
}

static void
B
Bjorn Helgaas 已提交
107
pnpacpi_parse_allocated_dmaresource(struct pnp_resource_table *res, u32 dma)
L
Linus Torvalds 已提交
108 109
{
	int i = 0;
110 111
	while (i < PNP_MAX_DMA &&
			!(res->dma_resource[i].flags & IORESOURCE_UNSET))
L
Linus Torvalds 已提交
112 113 114 115 116 117 118
		i++;
	if (i < PNP_MAX_DMA) {
		res->dma_resource[i].flags = IORESOURCE_DMA;  // Also clears _UNSET flag
		if (dma == -1) {
			res->dma_resource[i].flags |= IORESOURCE_DISABLED;
			return;
		}
119 120
		res->dma_resource[i].start = dma;
		res->dma_resource[i].end = dma;
L
Linus Torvalds 已提交
121 122 123 124
	}
}

static void
B
Bjorn Helgaas 已提交
125
pnpacpi_parse_allocated_ioresource(struct pnp_resource_table *res,
126
	u64 io, u64 len)
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135 136 137
{
	int i = 0;
	while (!(res->port_resource[i].flags & IORESOURCE_UNSET) &&
			i < PNP_MAX_PORT)
		i++;
	if (i < PNP_MAX_PORT) {
		res->port_resource[i].flags = IORESOURCE_IO;  // Also clears _UNSET flag
		if (len <= 0 || (io + len -1) >= 0x10003) {
			res->port_resource[i].flags |= IORESOURCE_DISABLED;
			return;
		}
138 139
		res->port_resource[i].start = io;
		res->port_resource[i].end = io + len - 1;
L
Linus Torvalds 已提交
140 141 142 143
	}
}

static void
B
Bjorn Helgaas 已提交
144
pnpacpi_parse_allocated_memresource(struct pnp_resource_table *res,
145
	u64 mem, u64 len)
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156
{
	int i = 0;
	while (!(res->mem_resource[i].flags & IORESOURCE_UNSET) &&
			(i < PNP_MAX_MEM))
		i++;
	if (i < PNP_MAX_MEM) {
		res->mem_resource[i].flags = IORESOURCE_MEM;  // Also clears _UNSET flag
		if (len <= 0) {
			res->mem_resource[i].flags |= IORESOURCE_DISABLED;
			return;
		}
157 158
		res->mem_resource[i].start = mem;
		res->mem_resource[i].end = mem + len - 1;
L
Linus Torvalds 已提交
159 160 161
	}
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175
static void
pnpacpi_parse_allocated_address_space(struct pnp_resource_table *res_table,
	struct acpi_resource *res)
{
	struct acpi_resource_address64 addr, *p = &addr;
	acpi_status status;

	status = acpi_resource_to_address64(res, p);
	if (!ACPI_SUCCESS(status)) {
		pnp_warn("PnPACPI: failed to convert resource type %d",
			res->type);
		return;
	}

176 177 178
	if (p->producer_consumer == ACPI_PRODUCER)
		return;

179 180 181 182 183 184 185
	if (p->resource_type == ACPI_MEMORY_RANGE)
		pnpacpi_parse_allocated_memresource(res_table,
				p->minimum, p->address_length);
	else if (p->resource_type == ACPI_IO_RANGE)
		pnpacpi_parse_allocated_ioresource(res_table,
				p->minimum, p->address_length);
}
L
Linus Torvalds 已提交
186 187 188 189

static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
	void *data)
{
B
Bjorn Helgaas 已提交
190
	struct pnp_resource_table *res_table = (struct pnp_resource_table *)data;
191
	int i;
L
Linus Torvalds 已提交
192

193
	switch (res->type) {
B
Bob Moore 已提交
194
	case ACPI_RESOURCE_TYPE_IRQ:
195 196 197 198
		/*
		 * Per spec, only one interrupt per descriptor is allowed in
		 * _CRS, but some firmware violates this, so parse them all.
		 */
B
Bob Moore 已提交
199
		for (i = 0; i < res->data.irq.interrupt_count; i++) {
200 201
			pnpacpi_parse_allocated_irqresource(res_table,
				res->data.irq.interrupts[i],
B
Bob Moore 已提交
202
				res->data.irq.triggering,
203 204
				res->data.irq.polarity,
				res->data.irq.sharable);
L
Linus Torvalds 已提交
205 206 207
		}
		break;

B
Bob Moore 已提交
208 209 210
	case ACPI_RESOURCE_TYPE_DMA:
		if (res->data.dma.channel_count > 0)
			pnpacpi_parse_allocated_dmaresource(res_table,
L
Linus Torvalds 已提交
211 212
					res->data.dma.channels[0]);
		break;
213

B
Bob Moore 已提交
214 215 216 217
	case ACPI_RESOURCE_TYPE_IO:
		pnpacpi_parse_allocated_ioresource(res_table,
				res->data.io.minimum,
				res->data.io.address_length);
L
Linus Torvalds 已提交
218
		break;
219 220 221 222 223

	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
		break;

B
Bob Moore 已提交
224 225 226 227
	case ACPI_RESOURCE_TYPE_FIXED_IO:
		pnpacpi_parse_allocated_ioresource(res_table,
				res->data.fixed_io.address,
				res->data.fixed_io.address_length);
L
Linus Torvalds 已提交
228
		break;
229 230 231 232 233 234 235

	case ACPI_RESOURCE_TYPE_VENDOR:
		break;

	case ACPI_RESOURCE_TYPE_END_TAG:
		break;

B
Bob Moore 已提交
236 237 238 239
	case ACPI_RESOURCE_TYPE_MEMORY24:
		pnpacpi_parse_allocated_memresource(res_table,
				res->data.memory24.minimum,
				res->data.memory24.address_length);
L
Linus Torvalds 已提交
240
		break;
B
Bob Moore 已提交
241 242 243 244
	case ACPI_RESOURCE_TYPE_MEMORY32:
		pnpacpi_parse_allocated_memresource(res_table,
				res->data.memory32.minimum,
				res->data.memory32.address_length);
L
Linus Torvalds 已提交
245
		break;
B
Bob Moore 已提交
246 247 248 249
	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
		pnpacpi_parse_allocated_memresource(res_table,
				res->data.fixed_memory32.address,
				res->data.fixed_memory32.address_length);
L
Linus Torvalds 已提交
250
		break;
B
Bob Moore 已提交
251 252 253
	case ACPI_RESOURCE_TYPE_ADDRESS16:
	case ACPI_RESOURCE_TYPE_ADDRESS32:
	case ACPI_RESOURCE_TYPE_ADDRESS64:
254
		pnpacpi_parse_allocated_address_space(res_table, res);
L
Linus Torvalds 已提交
255
		break;
256 257

	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
258 259
		if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
			return AE_OK;
260 261 262
		break;

	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
263 264 265
		if (res->data.extended_irq.producer_consumer == ACPI_PRODUCER)
			return AE_OK;

266 267 268 269
		for (i = 0; i < res->data.extended_irq.interrupt_count; i++) {
			pnpacpi_parse_allocated_irqresource(res_table,
				res->data.extended_irq.interrupts[i],
				res->data.extended_irq.triggering,
270 271
				res->data.extended_irq.polarity,
				res->data.extended_irq.sharable);
272 273 274 275
		}
		break;

	case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
L
Linus Torvalds 已提交
276
		break;
277

L
Linus Torvalds 已提交
278
	default:
279
		pnp_warn("PnPACPI: unknown resource type %d", res->type);
L
Linus Torvalds 已提交
280 281
		return AE_ERROR;
	}
B
Bjorn Helgaas 已提交
282

L
Linus Torvalds 已提交
283 284 285
	return AE_OK;
}

B
Bjorn Helgaas 已提交
286
acpi_status pnpacpi_parse_allocated_resource(acpi_handle handle, struct pnp_resource_table *res)
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298
{
	/* Blank the resource table values */
	pnp_init_resource_table(res);

	return acpi_walk_resources(handle, METHOD_NAME__CRS, pnpacpi_allocated_resource, res);
}

static void pnpacpi_parse_dma_option(struct pnp_option *option, struct acpi_resource_dma *p)
{
	int i;
	struct pnp_dma * dma;

B
Bob Moore 已提交
299
	if (p->channel_count == 0)
L
Linus Torvalds 已提交
300
		return;
301
	dma = kcalloc(1, sizeof(struct pnp_dma), GFP_KERNEL);
L
Linus Torvalds 已提交
302 303 304
	if (!dma)
		return;

B
Bob Moore 已提交
305
	for(i = 0; i < p->channel_count; i++)
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
		dma->map |= 1 << p->channels[i];
	dma->flags = 0;
	if (p->bus_master)
		dma->flags |= IORESOURCE_DMA_MASTER;
	switch (p->type) {
	case ACPI_COMPATIBILITY:
		dma->flags |= IORESOURCE_DMA_COMPATIBLE;
		break;
	case ACPI_TYPE_A:
		dma->flags |= IORESOURCE_DMA_TYPEA;
		break;
	case ACPI_TYPE_B:
		dma->flags |= IORESOURCE_DMA_TYPEB;
		break;
	case ACPI_TYPE_F:
		dma->flags |= IORESOURCE_DMA_TYPEF;
		break;
	default:
		/* Set a default value ? */
		dma->flags |= IORESOURCE_DMA_COMPATIBLE;
		pnp_err("Invalid DMA type");
	}
	switch (p->transfer) {
	case ACPI_TRANSFER_8:
		dma->flags |= IORESOURCE_DMA_8BIT;
		break;
	case ACPI_TRANSFER_8_16:
		dma->flags |= IORESOURCE_DMA_8AND16BIT;
		break;
	case ACPI_TRANSFER_16:
		dma->flags |= IORESOURCE_DMA_16BIT;
		break;
	default:
		/* Set a default value ? */
		dma->flags |= IORESOURCE_DMA_8AND16BIT;
		pnp_err("Invalid DMA transfer type");
	}

B
Bjorn Helgaas 已提交
344
	pnp_register_dma_resource(option, dma);
L
Linus Torvalds 已提交
345 346 347
	return;
}

B
Bjorn Helgaas 已提交
348

L
Linus Torvalds 已提交
349 350 351 352
static void pnpacpi_parse_irq_option(struct pnp_option *option,
	struct acpi_resource_irq *p)
{
	int i;
B
Bjorn Helgaas 已提交
353 354
	struct pnp_irq *irq;

B
Bob Moore 已提交
355
	if (p->interrupt_count == 0)
L
Linus Torvalds 已提交
356
		return;
357
	irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
L
Linus Torvalds 已提交
358 359 360
	if (!irq)
		return;

B
Bob Moore 已提交
361
	for(i = 0; i < p->interrupt_count; i++)
L
Linus Torvalds 已提交
362 363
		if (p->interrupts[i])
			__set_bit(p->interrupts[i], irq->map);
B
Bob Moore 已提交
364
	irq->flags = irq_flags(p->triggering, p->polarity);
L
Linus Torvalds 已提交
365 366 367 368 369 370

	pnp_register_irq_resource(option, irq);
	return;
}

static void pnpacpi_parse_ext_irq_option(struct pnp_option *option,
B
Bob Moore 已提交
371
	struct acpi_resource_extended_irq *p)
L
Linus Torvalds 已提交
372 373
{
	int i;
B
Bjorn Helgaas 已提交
374
	struct pnp_irq *irq;
L
Linus Torvalds 已提交
375

B
Bob Moore 已提交
376
	if (p->interrupt_count == 0)
L
Linus Torvalds 已提交
377
		return;
378
	irq = kcalloc(1, sizeof(struct pnp_irq), GFP_KERNEL);
L
Linus Torvalds 已提交
379 380 381
	if (!irq)
		return;

B
Bob Moore 已提交
382
	for(i = 0; i < p->interrupt_count; i++)
L
Linus Torvalds 已提交
383 384
		if (p->interrupts[i])
			__set_bit(p->interrupts[i], irq->map);
B
Bob Moore 已提交
385
	irq->flags = irq_flags(p->triggering, p->polarity);
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394

	pnp_register_irq_resource(option, irq);
	return;
}

static void
pnpacpi_parse_port_option(struct pnp_option *option,
	struct acpi_resource_io *io)
{
B
Bjorn Helgaas 已提交
395
	struct pnp_port *port;
L
Linus Torvalds 已提交
396

B
Bob Moore 已提交
397
	if (io->address_length == 0)
L
Linus Torvalds 已提交
398
		return;
399
	port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
L
Linus Torvalds 已提交
400 401
	if (!port)
		return;
B
Bob Moore 已提交
402 403
	port->min = io->minimum;
	port->max = io->maximum;
L
Linus Torvalds 已提交
404
	port->align = io->alignment;
B
Bob Moore 已提交
405 406
	port->size = io->address_length;
	port->flags = ACPI_DECODE_16 == io->io_decode ?
L
Linus Torvalds 已提交
407
		PNP_PORT_FLAG_16BITADDR : 0;
B
Bjorn Helgaas 已提交
408
	pnp_register_port_resource(option, port);
L
Linus Torvalds 已提交
409 410 411 412 413 414 415
	return;
}

static void
pnpacpi_parse_fixed_port_option(struct pnp_option *option,
	struct acpi_resource_fixed_io *io)
{
B
Bjorn Helgaas 已提交
416
	struct pnp_port *port;
L
Linus Torvalds 已提交
417

B
Bob Moore 已提交
418
	if (io->address_length == 0)
L
Linus Torvalds 已提交
419
		return;
420
	port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
L
Linus Torvalds 已提交
421 422
	if (!port)
		return;
B
Bob Moore 已提交
423 424
	port->min = port->max = io->address;
	port->size = io->address_length;
L
Linus Torvalds 已提交
425 426
	port->align = 0;
	port->flags = PNP_PORT_FLAG_FIXED;
B
Bjorn Helgaas 已提交
427
	pnp_register_port_resource(option, port);
L
Linus Torvalds 已提交
428 429 430 431 432
	return;
}

static void
pnpacpi_parse_mem24_option(struct pnp_option *option,
B
Bob Moore 已提交
433
	struct acpi_resource_memory24 *p)
L
Linus Torvalds 已提交
434
{
B
Bjorn Helgaas 已提交
435
	struct pnp_mem *mem;
L
Linus Torvalds 已提交
436

B
Bob Moore 已提交
437
	if (p->address_length == 0)
L
Linus Torvalds 已提交
438
		return;
439
	mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
L
Linus Torvalds 已提交
440 441
	if (!mem)
		return;
B
Bob Moore 已提交
442 443
	mem->min = p->minimum;
	mem->max = p->maximum;
L
Linus Torvalds 已提交
444
	mem->align = p->alignment;
B
Bob Moore 已提交
445
	mem->size = p->address_length;
L
Linus Torvalds 已提交
446

B
Bob Moore 已提交
447
	mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
L
Linus Torvalds 已提交
448 449
			IORESOURCE_MEM_WRITEABLE : 0;

B
Bjorn Helgaas 已提交
450
	pnp_register_mem_resource(option, mem);
L
Linus Torvalds 已提交
451 452 453 454 455
	return;
}

static void
pnpacpi_parse_mem32_option(struct pnp_option *option,
B
Bob Moore 已提交
456
	struct acpi_resource_memory32 *p)
L
Linus Torvalds 已提交
457
{
B
Bjorn Helgaas 已提交
458
	struct pnp_mem *mem;
L
Linus Torvalds 已提交
459

B
Bob Moore 已提交
460
	if (p->address_length == 0)
L
Linus Torvalds 已提交
461
		return;
462
	mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
L
Linus Torvalds 已提交
463 464
	if (!mem)
		return;
B
Bob Moore 已提交
465 466
	mem->min = p->minimum;
	mem->max = p->maximum;
L
Linus Torvalds 已提交
467
	mem->align = p->alignment;
B
Bob Moore 已提交
468
	mem->size = p->address_length;
L
Linus Torvalds 已提交
469

B
Bob Moore 已提交
470
	mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
L
Linus Torvalds 已提交
471 472
			IORESOURCE_MEM_WRITEABLE : 0;

B
Bjorn Helgaas 已提交
473
	pnp_register_mem_resource(option, mem);
L
Linus Torvalds 已提交
474 475 476 477 478
	return;
}

static void
pnpacpi_parse_fixed_mem32_option(struct pnp_option *option,
B
Bob Moore 已提交
479
	struct acpi_resource_fixed_memory32 *p)
L
Linus Torvalds 已提交
480
{
B
Bjorn Helgaas 已提交
481
	struct pnp_mem *mem;
L
Linus Torvalds 已提交
482

B
Bob Moore 已提交
483
	if (p->address_length == 0)
L
Linus Torvalds 已提交
484
		return;
485
	mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
L
Linus Torvalds 已提交
486 487
	if (!mem)
		return;
B
Bob Moore 已提交
488 489
	mem->min = mem->max = p->address;
	mem->size = p->address_length;
L
Linus Torvalds 已提交
490 491
	mem->align = 0;

B
Bob Moore 已提交
492
	mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
L
Linus Torvalds 已提交
493 494
			IORESOURCE_MEM_WRITEABLE : 0;

B
Bjorn Helgaas 已提交
495
	pnp_register_mem_resource(option, mem);
L
Linus Torvalds 已提交
496 497 498
	return;
}

499 500 501 502 503
static void
pnpacpi_parse_address_option(struct pnp_option *option, struct acpi_resource *r)
{
	struct acpi_resource_address64 addr, *p = &addr;
	acpi_status status;
B
Bjorn Helgaas 已提交
504 505
	struct pnp_mem *mem;
	struct pnp_port *port;
506 507 508

	status = acpi_resource_to_address64(r, p);
	if (!ACPI_SUCCESS(status)) {
L
Len Brown 已提交
509
		pnp_warn("PnPACPI: failed to convert resource type %d", r->type);
510 511 512 513 514 515 516 517 518 519
		return;
	}

	if (p->address_length == 0)
		return;

	if (p->resource_type == ACPI_MEMORY_RANGE) {
		mem = kcalloc(1, sizeof(struct pnp_mem), GFP_KERNEL);
		if (!mem)
			return;
L
Len Brown 已提交
520
		mem->min = mem->max = p->minimum;
521 522
		mem->size = p->address_length;
		mem->align = 0;
L
Len Brown 已提交
523
		mem->flags = (p->info.mem.write_protect ==
524
		    ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE : 0;
B
Bjorn Helgaas 已提交
525
		pnp_register_mem_resource(option, mem);
526 527 528 529
	} else if (p->resource_type == ACPI_IO_RANGE) {
		port = kcalloc(1, sizeof(struct pnp_port), GFP_KERNEL);
		if (!port)
			return;
L
Len Brown 已提交
530
		port->min = port->max = p->minimum;
531 532 533
		port->size = p->address_length;
		port->align = 0;
		port->flags = PNP_PORT_FLAG_FIXED;
B
Bjorn Helgaas 已提交
534
		pnp_register_port_resource(option, port);
535 536 537
	}
}

L
Linus Torvalds 已提交
538 539
struct acpipnp_parse_option_s {
	struct pnp_option *option;
M
Matthieu Castet 已提交
540
	struct pnp_option *option_independent;
L
Linus Torvalds 已提交
541 542 543
	struct pnp_dev *dev;
};

B
Bob Moore 已提交
544
static acpi_status pnpacpi_option_resource(struct acpi_resource *res,
L
Linus Torvalds 已提交
545 546 547 548 549 550 551
	void *data)
{
	int priority = 0;
	struct acpipnp_parse_option_s *parse_data = (struct acpipnp_parse_option_s *)data;
	struct pnp_dev *dev = parse_data->dev;
	struct pnp_option *option = parse_data->option;

552
	switch (res->type) {
B
Bob Moore 已提交
553
		case ACPI_RESOURCE_TYPE_IRQ:
L
Linus Torvalds 已提交
554 555
			pnpacpi_parse_irq_option(option, &res->data.irq);
			break;
556

B
Bob Moore 已提交
557
		case ACPI_RESOURCE_TYPE_DMA:
B
Bjorn Helgaas 已提交
558
			pnpacpi_parse_dma_option(option, &res->data.dma);
L
Linus Torvalds 已提交
559
			break;
560

B
Bob Moore 已提交
561
		case ACPI_RESOURCE_TYPE_START_DEPENDENT:
L
Linus Torvalds 已提交
562 563 564 565
			switch (res->data.start_dpf.compatibility_priority) {
				case ACPI_GOOD_CONFIGURATION:
					priority = PNP_RES_PRIORITY_PREFERRED;
					break;
B
Bjorn Helgaas 已提交
566

L
Linus Torvalds 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
				case ACPI_ACCEPTABLE_CONFIGURATION:
					priority = PNP_RES_PRIORITY_ACCEPTABLE;
					break;

				case ACPI_SUB_OPTIMAL_CONFIGURATION:
					priority = PNP_RES_PRIORITY_FUNCTIONAL;
					break;
				default:
					priority = PNP_RES_PRIORITY_INVALID;
					break;
			}
			/* TBD: Considering performace/robustness bits */
			option = pnp_register_dependent_option(dev, priority);
			if (!option)
				return AE_ERROR;
B
Bjorn Helgaas 已提交
582
			parse_data->option = option;
L
Linus Torvalds 已提交
583
			break;
584

B
Bob Moore 已提交
585
		case ACPI_RESOURCE_TYPE_END_DEPENDENT:
M
Matthieu Castet 已提交
586 587 588 589 590 591 592 593
			/*only one EndDependentFn is allowed*/
			if (!parse_data->option_independent) {
				pnp_warn("PnPACPI: more than one EndDependentFn");
				return AE_ERROR;
			}
			parse_data->option = parse_data->option_independent;
			parse_data->option_independent = NULL;
			break;
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

		case ACPI_RESOURCE_TYPE_IO:
			pnpacpi_parse_port_option(option, &res->data.io);
			break;

		case ACPI_RESOURCE_TYPE_FIXED_IO:
			pnpacpi_parse_fixed_port_option(option,
				&res->data.fixed_io);
			break;

		case ACPI_RESOURCE_TYPE_VENDOR:
		case ACPI_RESOURCE_TYPE_END_TAG:
			break;

		case ACPI_RESOURCE_TYPE_MEMORY24:
			pnpacpi_parse_mem24_option(option, &res->data.memory24);
			break;

		case ACPI_RESOURCE_TYPE_MEMORY32:
			pnpacpi_parse_mem32_option(option, &res->data.memory32);
			break;

		case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
			pnpacpi_parse_fixed_mem32_option(option,
				&res->data.fixed_memory32);
			break;

		case ACPI_RESOURCE_TYPE_ADDRESS16:
		case ACPI_RESOURCE_TYPE_ADDRESS32:
		case ACPI_RESOURCE_TYPE_ADDRESS64:
			pnpacpi_parse_address_option(option, res);
			break;

		case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
			break;

		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
			pnpacpi_parse_ext_irq_option(option,
				&res->data.extended_irq);
			break;

		case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
			break;

L
Linus Torvalds 已提交
638
		default:
639
			pnp_warn("PnPACPI: unknown resource type %d", res->type);
L
Linus Torvalds 已提交
640 641
			return AE_ERROR;
	}
B
Bjorn Helgaas 已提交
642

L
Linus Torvalds 已提交
643 644 645
	return AE_OK;
}

B
Bob Moore 已提交
646
acpi_status pnpacpi_parse_resource_option_data(acpi_handle handle,
L
Linus Torvalds 已提交
647 648 649 650 651 652 653 654
	struct pnp_dev *dev)
{
	acpi_status status;
	struct acpipnp_parse_option_s parse_data;

	parse_data.option = pnp_register_independent_option(dev);
	if (!parse_data.option)
		return AE_ERROR;
M
Matthieu Castet 已提交
655
	parse_data.option_independent = parse_data.option;
L
Linus Torvalds 已提交
656
	parse_data.dev = dev;
B
Bob Moore 已提交
657
	status = acpi_walk_resources(handle, METHOD_NAME__PRS,
L
Linus Torvalds 已提交
658 659 660 661 662
		pnpacpi_option_resource, &parse_data);

	return status;
}

663
static int pnpacpi_supported_resource(struct acpi_resource *res)
L
Linus Torvalds 已提交
664
{
665
	switch (res->type) {
B
Bob Moore 已提交
666 667 668 669 670 671 672 673 674 675
	case ACPI_RESOURCE_TYPE_IRQ:
	case ACPI_RESOURCE_TYPE_DMA:
	case ACPI_RESOURCE_TYPE_IO:
	case ACPI_RESOURCE_TYPE_FIXED_IO:
	case ACPI_RESOURCE_TYPE_MEMORY24:
	case ACPI_RESOURCE_TYPE_MEMORY32:
	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
	case ACPI_RESOURCE_TYPE_ADDRESS16:
	case ACPI_RESOURCE_TYPE_ADDRESS32:
	case ACPI_RESOURCE_TYPE_ADDRESS64:
676
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
677
		return 1;
L
Linus Torvalds 已提交
678
	}
679 680 681 682 683 684 685 686 687 688 689 690 691
	return 0;
}

/*
 * Set resource
 */
static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
	void *data)
{
	int *res_cnt = (int *)data;

	if (pnpacpi_supported_resource(res))
		(*res_cnt)++;
L
Linus Torvalds 已提交
692 693 694
	return AE_OK;
}

B
Bjorn Helgaas 已提交
695
static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
L
Linus Torvalds 已提交
696
{
B
Bjorn Helgaas 已提交
697
	struct acpi_resource **resource = (struct acpi_resource **)data;
698 699

	if (pnpacpi_supported_resource(res)) {
700
		(*resource)->type = res->type;
701
		(*resource)->length = sizeof(struct acpi_resource);
L
Linus Torvalds 已提交
702 703 704 705 706 707
		(*resource)++;
	}

	return AE_OK;
}

B
Bob Moore 已提交
708
int pnpacpi_build_resource_template(acpi_handle handle,
L
Linus Torvalds 已提交
709 710 711 712 713 714
	struct acpi_buffer *buffer)
{
	struct acpi_resource *resource;
	int res_cnt = 0;
	acpi_status status;

B
Bob Moore 已提交
715
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
L
Linus Torvalds 已提交
716 717 718 719 720 721 722 723
		pnpacpi_count_resources, &res_cnt);
	if (ACPI_FAILURE(status)) {
		pnp_err("Evaluate _CRS failed");
		return -EINVAL;
	}
	if (!res_cnt)
		return -EINVAL;
	buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
724
	buffer->pointer = kcalloc(1, buffer->length - 1, GFP_KERNEL);
L
Linus Torvalds 已提交
725 726 727 728
	if (!buffer->pointer)
		return -ENOMEM;
	pnp_dbg("Res cnt %d", res_cnt);
	resource = (struct acpi_resource *)buffer->pointer;
B
Bob Moore 已提交
729
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
L
Linus Torvalds 已提交
730 731 732 733 734 735 736
		pnpacpi_type_resources, &resource);
	if (ACPI_FAILURE(status)) {
		kfree(buffer->pointer);
		pnp_err("Evaluate _CRS failed");
		return -EINVAL;
	}
	/* resource will pointer the end resource now */
B
Bob Moore 已提交
737
	resource->type = ACPI_RESOURCE_TYPE_END_TAG;
L
Linus Torvalds 已提交
738 739 740 741

	return 0;
}

B
Bob Moore 已提交
742
static void pnpacpi_encode_irq(struct acpi_resource *resource,
L
Linus Torvalds 已提交
743 744
	struct resource *p)
{
B
Bob Moore 已提交
745
	int triggering, polarity;
B
Bjorn Helgaas 已提交
746 747

	decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
B
Bob Moore 已提交
748 749 750 751
	resource->data.irq.triggering = triggering;
	resource->data.irq.polarity = polarity;
	if (triggering == ACPI_EDGE_SENSITIVE)
		resource->data.irq.sharable = ACPI_EXCLUSIVE;
L
Linus Torvalds 已提交
752
	else
B
Bob Moore 已提交
753 754
		resource->data.irq.sharable = ACPI_SHARED;
	resource->data.irq.interrupt_count = 1;
L
Linus Torvalds 已提交
755 756 757 758 759 760
	resource->data.irq.interrupts[0] = p->start;
}

static void pnpacpi_encode_ext_irq(struct acpi_resource *resource,
	struct resource *p)
{
B
Bob Moore 已提交
761
	int triggering, polarity;
B
Bjorn Helgaas 已提交
762 763

	decode_irq_flags(p->flags & IORESOURCE_BITS, &triggering, &polarity);
L
Linus Torvalds 已提交
764
	resource->data.extended_irq.producer_consumer = ACPI_CONSUMER;
B
Bob Moore 已提交
765 766 767 768
	resource->data.extended_irq.triggering = triggering;
	resource->data.extended_irq.polarity = polarity;
	if (triggering == ACPI_EDGE_SENSITIVE)
		resource->data.irq.sharable = ACPI_EXCLUSIVE;
L
Linus Torvalds 已提交
769
	else
B
Bob Moore 已提交
770 771
		resource->data.irq.sharable = ACPI_SHARED;
	resource->data.extended_irq.interrupt_count = 1;
L
Linus Torvalds 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	resource->data.extended_irq.interrupts[0] = p->start;
}

static void pnpacpi_encode_dma(struct acpi_resource *resource,
	struct resource *p)
{
	/* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
	if (p->flags & IORESOURCE_DMA_COMPATIBLE)
		resource->data.dma.type = ACPI_COMPATIBILITY;
	else if (p->flags & IORESOURCE_DMA_TYPEA)
		resource->data.dma.type = ACPI_TYPE_A;
	else if (p->flags & IORESOURCE_DMA_TYPEB)
		resource->data.dma.type = ACPI_TYPE_B;
	else if (p->flags & IORESOURCE_DMA_TYPEF)
		resource->data.dma.type = ACPI_TYPE_F;
	if (p->flags & IORESOURCE_DMA_8BIT)
		resource->data.dma.transfer = ACPI_TRANSFER_8;
	else if (p->flags & IORESOURCE_DMA_8AND16BIT)
		resource->data.dma.transfer = ACPI_TRANSFER_8_16;
	else if (p->flags & IORESOURCE_DMA_16BIT)
		resource->data.dma.transfer = ACPI_TRANSFER_16;
	resource->data.dma.bus_master = p->flags & IORESOURCE_DMA_MASTER;
B
Bob Moore 已提交
794
	resource->data.dma.channel_count = 1;
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802
	resource->data.dma.channels[0] = p->start;
}

static void pnpacpi_encode_io(struct acpi_resource *resource,
	struct resource *p)
{
	/* Note: pnp_assign_port will copy pnp_port->flags into p->flags */
	resource->data.io.io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR)?
B
Bob Moore 已提交
803 804 805
		ACPI_DECODE_16 : ACPI_DECODE_10;
	resource->data.io.minimum = p->start;
	resource->data.io.maximum = p->end;
L
Linus Torvalds 已提交
806
	resource->data.io.alignment = 0; /* Correct? */
B
Bob Moore 已提交
807
	resource->data.io.address_length = p->end - p->start + 1;
L
Linus Torvalds 已提交
808 809 810 811 812
}

static void pnpacpi_encode_fixed_io(struct acpi_resource *resource,
	struct resource *p)
{
B
Bob Moore 已提交
813 814
	resource->data.fixed_io.address = p->start;
	resource->data.fixed_io.address_length = p->end - p->start + 1;
L
Linus Torvalds 已提交
815 816 817 818 819 820
}

static void pnpacpi_encode_mem24(struct acpi_resource *resource,
	struct resource *p)
{
	/* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */
B
Bob Moore 已提交
821
	resource->data.memory24.write_protect =
L
Linus Torvalds 已提交
822 823
		(p->flags & IORESOURCE_MEM_WRITEABLE) ?
		ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
B
Bob Moore 已提交
824 825
	resource->data.memory24.minimum = p->start;
	resource->data.memory24.maximum = p->end;
L
Linus Torvalds 已提交
826
	resource->data.memory24.alignment = 0;
B
Bob Moore 已提交
827
	resource->data.memory24.address_length = p->end - p->start + 1;
L
Linus Torvalds 已提交
828 829 830 831 832
}

static void pnpacpi_encode_mem32(struct acpi_resource *resource,
	struct resource *p)
{
B
Bob Moore 已提交
833
	resource->data.memory32.write_protect =
L
Linus Torvalds 已提交
834 835
		(p->flags & IORESOURCE_MEM_WRITEABLE) ?
		ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
B
Bob Moore 已提交
836 837
	resource->data.memory32.minimum = p->start;
	resource->data.memory32.maximum = p->end;
L
Linus Torvalds 已提交
838
	resource->data.memory32.alignment = 0;
B
Bob Moore 已提交
839
	resource->data.memory32.address_length = p->end - p->start + 1;
L
Linus Torvalds 已提交
840 841 842 843 844
}

static void pnpacpi_encode_fixed_mem32(struct acpi_resource *resource,
	struct resource *p)
{
B
Bob Moore 已提交
845
	resource->data.fixed_memory32.write_protect =
L
Linus Torvalds 已提交
846 847
		(p->flags & IORESOURCE_MEM_WRITEABLE) ?
		ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
B
Bob Moore 已提交
848 849
	resource->data.fixed_memory32.address = p->start;
	resource->data.fixed_memory32.address_length = p->end - p->start + 1;
L
Linus Torvalds 已提交
850 851
}

B
Bob Moore 已提交
852
int pnpacpi_encode_resources(struct pnp_resource_table *res_table,
L
Linus Torvalds 已提交
853 854 855 856 857 858 859 860 861 862
	struct acpi_buffer *buffer)
{
	int i = 0;
	/* pnpacpi_build_resource_template allocates extra mem */
	int res_cnt = (buffer->length - 1)/sizeof(struct acpi_resource) - 1;
	struct acpi_resource *resource = (struct acpi_resource*)buffer->pointer;
	int port = 0, irq = 0, dma = 0, mem = 0;

	pnp_dbg("res cnt %d", res_cnt);
	while (i < res_cnt) {
863
		switch(resource->type) {
B
Bob Moore 已提交
864
		case ACPI_RESOURCE_TYPE_IRQ:
L
Linus Torvalds 已提交
865
			pnp_dbg("Encode irq");
B
Bob Moore 已提交
866
			pnpacpi_encode_irq(resource,
L
Linus Torvalds 已提交
867 868 869 870
				&res_table->irq_resource[irq]);
			irq++;
			break;

B
Bob Moore 已提交
871
		case ACPI_RESOURCE_TYPE_DMA:
L
Linus Torvalds 已提交
872
			pnp_dbg("Encode dma");
B
Bob Moore 已提交
873
			pnpacpi_encode_dma(resource,
L
Linus Torvalds 已提交
874
				&res_table->dma_resource[dma]);
B
Bjorn Helgaas 已提交
875
			dma++;
L
Linus Torvalds 已提交
876
			break;
B
Bob Moore 已提交
877
		case ACPI_RESOURCE_TYPE_IO:
L
Linus Torvalds 已提交
878
			pnp_dbg("Encode io");
B
Bob Moore 已提交
879
			pnpacpi_encode_io(resource,
L
Linus Torvalds 已提交
880
				&res_table->port_resource[port]);
B
Bjorn Helgaas 已提交
881
			port++;
L
Linus Torvalds 已提交
882
			break;
B
Bob Moore 已提交
883
		case ACPI_RESOURCE_TYPE_FIXED_IO:
L
Linus Torvalds 已提交
884 885 886
			pnp_dbg("Encode fixed io");
			pnpacpi_encode_fixed_io(resource,
				&res_table->port_resource[port]);
B
Bjorn Helgaas 已提交
887
			port++;
L
Linus Torvalds 已提交
888
			break;
B
Bob Moore 已提交
889
		case ACPI_RESOURCE_TYPE_MEMORY24:
L
Linus Torvalds 已提交
890 891 892
			pnp_dbg("Encode mem24");
			pnpacpi_encode_mem24(resource,
				&res_table->mem_resource[mem]);
B
Bjorn Helgaas 已提交
893
			mem++;
L
Linus Torvalds 已提交
894
			break;
B
Bob Moore 已提交
895
		case ACPI_RESOURCE_TYPE_MEMORY32:
L
Linus Torvalds 已提交
896 897 898
			pnp_dbg("Encode mem32");
			pnpacpi_encode_mem32(resource,
				&res_table->mem_resource[mem]);
B
Bjorn Helgaas 已提交
899
			mem++;
L
Linus Torvalds 已提交
900
			break;
B
Bob Moore 已提交
901
		case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
L
Linus Torvalds 已提交
902 903 904
			pnp_dbg("Encode fixed mem32");
			pnpacpi_encode_fixed_mem32(resource,
				&res_table->mem_resource[mem]);
B
Bjorn Helgaas 已提交
905
			mem++;
L
Linus Torvalds 已提交
906
			break;
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
			pnp_dbg("Encode ext irq");
			pnpacpi_encode_ext_irq(resource,
				&res_table->irq_resource[irq]);
			irq++;
			break;
		case ACPI_RESOURCE_TYPE_START_DEPENDENT:
		case ACPI_RESOURCE_TYPE_END_DEPENDENT:
		case ACPI_RESOURCE_TYPE_VENDOR:
		case ACPI_RESOURCE_TYPE_END_TAG:
		case ACPI_RESOURCE_TYPE_ADDRESS16:
		case ACPI_RESOURCE_TYPE_ADDRESS32:
		case ACPI_RESOURCE_TYPE_ADDRESS64:
		case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
		case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
L
Linus Torvalds 已提交
922
		default: /* other type */
923
			pnp_warn("unknown resource type %d", resource->type);
L
Linus Torvalds 已提交
924 925
			return -EINVAL;
		}
B
Bjorn Helgaas 已提交
926 927
		resource++;
		i++;
L
Linus Torvalds 已提交
928 929 930
	}
	return 0;
}