rsparser.c 31.4 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>
6 7
 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
 *	Bjorn Helgaas <bjorn.helgaas@hp.com>
B
Bob Moore 已提交
8
 *
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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>
26 27
#include <linux/pnp.h>
#include "../base.h"
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37 38
#include "pnpacpi.h"

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

/*
 * Allocated Resources
 */
39
static int irq_flags(int triggering, int polarity, int shareable)
L
Linus Torvalds 已提交
40
{
41 42
	int flags;

B
Bob Moore 已提交
43
	if (triggering == ACPI_LEVEL_SENSITIVE) {
B
Bjorn Helgaas 已提交
44
		if (polarity == ACPI_ACTIVE_LOW)
45
			flags = IORESOURCE_IRQ_LOWLEVEL;
L
Linus Torvalds 已提交
46
		else
47
			flags = IORESOURCE_IRQ_HIGHLEVEL;
B
Bjorn Helgaas 已提交
48
	} else {
B
Bjorn Helgaas 已提交
49
		if (polarity == ACPI_ACTIVE_LOW)
50
			flags = IORESOURCE_IRQ_LOWEDGE;
L
Linus Torvalds 已提交
51
		else
52
			flags = IORESOURCE_IRQ_HIGHEDGE;
L
Linus Torvalds 已提交
53
	}
54

55
	if (shareable == ACPI_SHARED)
56 57 58
		flags |= IORESOURCE_IRQ_SHAREABLE;

	return flags;
L
Linus Torvalds 已提交
59 60
}

B
Bjorn Helgaas 已提交
61
static void decode_irq_flags(struct pnp_dev *dev, int flags, int *triggering,
62
			     int *polarity, int *shareable)
L
Linus Torvalds 已提交
63
{
B
Bjorn Helgaas 已提交
64 65
	switch (flags & (IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL |
			 IORESOURCE_IRQ_LOWEDGE  | IORESOURCE_IRQ_HIGHEDGE)) {
L
Linus Torvalds 已提交
66
	case IORESOURCE_IRQ_LOWLEVEL:
B
Bob Moore 已提交
67 68
		*triggering = ACPI_LEVEL_SENSITIVE;
		*polarity = ACPI_ACTIVE_LOW;
L
Linus Torvalds 已提交
69
		break;
B
Bjorn Helgaas 已提交
70
	case IORESOURCE_IRQ_HIGHLEVEL:
B
Bob Moore 已提交
71 72
		*triggering = ACPI_LEVEL_SENSITIVE;
		*polarity = ACPI_ACTIVE_HIGH;
L
Linus Torvalds 已提交
73 74
		break;
	case IORESOURCE_IRQ_LOWEDGE:
B
Bob Moore 已提交
75 76
		*triggering = ACPI_EDGE_SENSITIVE;
		*polarity = ACPI_ACTIVE_LOW;
L
Linus Torvalds 已提交
77 78
		break;
	case IORESOURCE_IRQ_HIGHEDGE:
B
Bob Moore 已提交
79 80
		*triggering = ACPI_EDGE_SENSITIVE;
		*polarity = ACPI_ACTIVE_HIGH;
L
Linus Torvalds 已提交
81
		break;
B
Bjorn Helgaas 已提交
82 83 84 85 86 87
	default:
		dev_err(&dev->dev, "can't encode invalid IRQ mode %#x\n",
			flags);
		*triggering = ACPI_EDGE_SENSITIVE;
		*polarity = ACPI_ACTIVE_HIGH;
		break;
L
Linus Torvalds 已提交
88
	}
89 90 91 92 93

	if (flags & IORESOURCE_IRQ_SHAREABLE)
		*shareable = ACPI_SHARED;
	else
		*shareable = ACPI_EXCLUSIVE;
L
Linus Torvalds 已提交
94 95
}

96
static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
B
Bjorn Helgaas 已提交
97 98
						u32 gsi, int triggering,
						int polarity, int shareable)
L
Linus Torvalds 已提交
99
{
100
	int irq, flags;
101
	int p, t;
102

103 104
	if (!valid_IRQ(gsi)) {
		pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
105
		return;
106
	}
107

108 109 110 111 112 113 114 115 116 117
	/*
	 * in IO-APIC mode, use overrided attribute. Two reasons:
	 * 1. BIOS bug in DSDT
	 * 2. BIOS uses IO-APIC mode Interrupt Source Override
	 */
	if (!acpi_get_override_irq(gsi, &t, &p)) {
		t = t ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
		p = p ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;

		if (triggering != t || polarity != p) {
118
			dev_warn(&dev->dev, "IRQ %d override to %s, %s\n",
119 120 121 122 123 124
				gsi, t ? "edge":"level", p ? "low":"high");
			triggering = t;
			polarity = p;
		}
	}

125
	flags = irq_flags(triggering, polarity, shareable);
126
	irq = acpi_register_gsi(&dev->dev, gsi, triggering, polarity);
127 128 129 130
	if (irq >= 0)
		pcibios_penalize_isa_irq(irq, 1);
	else
		flags |= IORESOURCE_DISABLED;
131

132
	pnp_add_irq_resource(dev, irq, flags);
L
Linus Torvalds 已提交
133 134
}

135 136
static int dma_flags(struct pnp_dev *dev, int type, int bus_master,
		     int transfer)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
{
	int flags = 0;

	if (bus_master)
		flags |= IORESOURCE_DMA_MASTER;
	switch (type) {
	case ACPI_COMPATIBILITY:
		flags |= IORESOURCE_DMA_COMPATIBLE;
		break;
	case ACPI_TYPE_A:
		flags |= IORESOURCE_DMA_TYPEA;
		break;
	case ACPI_TYPE_B:
		flags |= IORESOURCE_DMA_TYPEB;
		break;
	case ACPI_TYPE_F:
		flags |= IORESOURCE_DMA_TYPEF;
		break;
	default:
		/* Set a default value ? */
		flags |= IORESOURCE_DMA_COMPATIBLE;
158
		dev_err(&dev->dev, "invalid DMA type %d\n", type);
159 160 161 162 163 164 165 166 167 168 169 170 171 172
	}
	switch (transfer) {
	case ACPI_TRANSFER_8:
		flags |= IORESOURCE_DMA_8BIT;
		break;
	case ACPI_TRANSFER_8_16:
		flags |= IORESOURCE_DMA_8AND16BIT;
		break;
	case ACPI_TRANSFER_16:
		flags |= IORESOURCE_DMA_16BIT;
		break;
	default:
		/* Set a default value ? */
		flags |= IORESOURCE_DMA_8AND16BIT;
173
		dev_err(&dev->dev, "invalid DMA transfer type %d\n", transfer);
174 175 176 177 178
	}

	return flags;
}

179 180
static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
					       u64 len, int io_decode)
L
Linus Torvalds 已提交
181
{
182 183
	int flags = 0;
	u64 end = start + len - 1;
B
Bjorn Helgaas 已提交
184

185
	if (io_decode == ACPI_DECODE_16)
186
		flags |= IORESOURCE_IO_16BIT_ADDR;
187 188 189 190
	if (len == 0 || end >= 0x10003)
		flags |= IORESOURCE_DISABLED;

	pnp_add_io_resource(dev, start, end, flags);
L
Linus Torvalds 已提交
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
/*
 * Device CSRs that do not appear in PCI config space should be described
 * via ACPI.  This would normally be done with Address Space Descriptors
 * marked as "consumer-only," but old versions of Windows and Linux ignore
 * the producer/consumer flag, so HP invented a vendor-defined resource to
 * describe the location and size of CSR space.
 */
static struct acpi_vendor_uuid hp_ccsr_uuid = {
	.subtype = 2,
	.data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
	    0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
};

static int vendor_resource_matches(struct pnp_dev *dev,
				   struct acpi_resource_vendor_typed *vendor,
				   struct acpi_vendor_uuid *match,
				   int expected_len)
{
	int uuid_len = sizeof(vendor->uuid);
	u8 uuid_subtype = vendor->uuid_subtype;
	u8 *uuid = vendor->uuid;
	int actual_len;

	/* byte_length includes uuid_subtype and uuid */
	actual_len = vendor->byte_length - uuid_len - 1;

	if (uuid_subtype == match->subtype &&
	    uuid_len == sizeof(match->data) &&
	    memcmp(uuid, match->data, uuid_len) == 0) {
		if (expected_len && expected_len != actual_len) {
			dev_err(&dev->dev, "wrong vendor descriptor size; "
				"expected %d, found %d bytes\n",
				expected_len, actual_len);
			return 0;
		}

		return 1;
	}

	return 0;
}

static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
				    struct acpi_resource_vendor_typed *vendor)
{
	if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
		u64 start, length;

		memcpy(&start, vendor->byte_data, sizeof(start));
		memcpy(&length, vendor->byte_data + 8, sizeof(length));

		pnp_add_mem_resource(dev, start, start + length - 1, 0);
	}
}

248
static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
249
						u64 start, u64 len,
B
Bjorn Helgaas 已提交
250
						int write_protect)
L
Linus Torvalds 已提交
251
{
252 253 254 255 256 257 258 259 260
	int flags = 0;
	u64 end = start + len - 1;

	if (len == 0)
		flags |= IORESOURCE_DISABLED;
	if (write_protect == ACPI_READ_WRITE_MEMORY)
		flags |= IORESOURCE_MEM_WRITEABLE;

	pnp_add_mem_resource(dev, start, end, flags);
L
Linus Torvalds 已提交
261 262
}

263
static void pnpacpi_parse_allocated_address_space(struct pnp_dev *dev,
B
Bjorn Helgaas 已提交
264
						  struct acpi_resource *res)
265 266 267 268 269 270
{
	struct acpi_resource_address64 addr, *p = &addr;
	acpi_status status;

	status = acpi_resource_to_address64(res, p);
	if (!ACPI_SUCCESS(status)) {
271
		dev_warn(&dev->dev, "failed to convert resource type %d\n",
B
Bjorn Helgaas 已提交
272
			 res->type);
273 274 275
		return;
	}

276 277 278
	if (p->producer_consumer == ACPI_PRODUCER)
		return;

279
	if (p->resource_type == ACPI_MEMORY_RANGE)
280
		pnpacpi_parse_allocated_memresource(dev,
B
Bjorn Helgaas 已提交
281 282
			p->minimum, p->address_length,
			p->info.mem.write_protect);
283
	else if (p->resource_type == ACPI_IO_RANGE)
284
		pnpacpi_parse_allocated_ioresource(dev,
B
Bjorn Helgaas 已提交
285 286 287
			p->minimum, p->address_length,
			p->granularity == 0xfff ? ACPI_DECODE_10 :
				ACPI_DECODE_16);
288
}
L
Linus Torvalds 已提交
289

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
static void pnpacpi_parse_allocated_ext_address_space(struct pnp_dev *dev,
						      struct acpi_resource *res)
{
	struct acpi_resource_extended_address64 *p = &res->data.ext_address64;

	if (p->producer_consumer == ACPI_PRODUCER)
		return;

	if (p->resource_type == ACPI_MEMORY_RANGE)
		pnpacpi_parse_allocated_memresource(dev,
			p->minimum, p->address_length,
			p->info.mem.write_protect);
	else if (p->resource_type == ACPI_IO_RANGE)
		pnpacpi_parse_allocated_ioresource(dev,
			p->minimum, p->address_length,
			p->granularity == 0xfff ? ACPI_DECODE_10 :
				ACPI_DECODE_16);
}

L
Linus Torvalds 已提交
309
static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
B
Bjorn Helgaas 已提交
310
					      void *data)
L
Linus Torvalds 已提交
311
{
312
	struct pnp_dev *dev = data;
313 314 315 316
	struct acpi_resource_irq *irq;
	struct acpi_resource_dma *dma;
	struct acpi_resource_io *io;
	struct acpi_resource_fixed_io *fixed_io;
317
	struct acpi_resource_vendor_typed *vendor_typed;
318 319 320 321
	struct acpi_resource_memory24 *memory24;
	struct acpi_resource_memory32 *memory32;
	struct acpi_resource_fixed_memory32 *fixed_memory32;
	struct acpi_resource_extended_irq *extended_irq;
322
	int i, flags;
L
Linus Torvalds 已提交
323

324
	switch (res->type) {
B
Bob Moore 已提交
325
	case ACPI_RESOURCE_TYPE_IRQ:
326 327 328 329
		/*
		 * Per spec, only one interrupt per descriptor is allowed in
		 * _CRS, but some firmware violates this, so parse them all.
		 */
330
		irq = &res->data.irq;
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
		if (irq->interrupt_count == 0)
			pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
		else {
			for (i = 0; i < irq->interrupt_count; i++) {
				pnpacpi_parse_allocated_irqresource(dev,
					irq->interrupts[i],
					irq->triggering,
					irq->polarity,
				    irq->sharable);
			}

			/*
			 * The IRQ encoder puts a single interrupt in each
			 * descriptor, so if a _CRS descriptor has more than
			 * one interrupt, we won't be able to re-encode it.
			 */
			if (pnp_can_write(dev) && irq->interrupt_count > 1) {
				dev_warn(&dev->dev, "multiple interrupts in "
					 "_CRS descriptor; configuration can't "
					 "be changed\n");
				dev->capabilities &= ~PNP_WRITE;
			}
L
Linus Torvalds 已提交
353 354 355
		}
		break;

B
Bob Moore 已提交
356
	case ACPI_RESOURCE_TYPE_DMA:
357
		dma = &res->data.dma;
358
		if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
359
			flags = dma_flags(dev, dma->type, dma->bus_master,
360
					  dma->transfer);
361 362 363
		else
			flags = IORESOURCE_DISABLED;
		pnp_add_dma_resource(dev, dma->channels[0], flags);
L
Linus Torvalds 已提交
364
		break;
365

B
Bob Moore 已提交
366
	case ACPI_RESOURCE_TYPE_IO:
367
		io = &res->data.io;
368
		pnpacpi_parse_allocated_ioresource(dev,
369 370 371
			io->minimum,
			io->address_length,
			io->io_decode);
L
Linus Torvalds 已提交
372
		break;
373 374 375 376 377

	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
		break;

B
Bob Moore 已提交
378
	case ACPI_RESOURCE_TYPE_FIXED_IO:
379
		fixed_io = &res->data.fixed_io;
380
		pnpacpi_parse_allocated_ioresource(dev,
381 382
			fixed_io->address,
			fixed_io->address_length,
B
Bjorn Helgaas 已提交
383
			ACPI_DECODE_10);
L
Linus Torvalds 已提交
384
		break;
385 386

	case ACPI_RESOURCE_TYPE_VENDOR:
387 388
		vendor_typed = &res->data.vendor_typed;
		pnpacpi_parse_allocated_vendor(dev, vendor_typed);
389 390 391 392 393
		break;

	case ACPI_RESOURCE_TYPE_END_TAG:
		break;

B
Bob Moore 已提交
394
	case ACPI_RESOURCE_TYPE_MEMORY24:
395
		memory24 = &res->data.memory24;
396
		pnpacpi_parse_allocated_memresource(dev,
397 398 399
			memory24->minimum,
			memory24->address_length,
			memory24->write_protect);
L
Linus Torvalds 已提交
400
		break;
B
Bob Moore 已提交
401
	case ACPI_RESOURCE_TYPE_MEMORY32:
402
		memory32 = &res->data.memory32;
403
		pnpacpi_parse_allocated_memresource(dev,
404 405 406
			memory32->minimum,
			memory32->address_length,
			memory32->write_protect);
L
Linus Torvalds 已提交
407
		break;
B
Bob Moore 已提交
408
	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
409
		fixed_memory32 = &res->data.fixed_memory32;
410
		pnpacpi_parse_allocated_memresource(dev,
411 412 413
			fixed_memory32->address,
			fixed_memory32->address_length,
			fixed_memory32->write_protect);
L
Linus Torvalds 已提交
414
		break;
B
Bob Moore 已提交
415 416 417
	case ACPI_RESOURCE_TYPE_ADDRESS16:
	case ACPI_RESOURCE_TYPE_ADDRESS32:
	case ACPI_RESOURCE_TYPE_ADDRESS64:
418
		pnpacpi_parse_allocated_address_space(dev, res);
L
Linus Torvalds 已提交
419
		break;
420 421

	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
422
		pnpacpi_parse_allocated_ext_address_space(dev, res);
423 424 425
		break;

	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
426
		extended_irq = &res->data.extended_irq;
427

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		if (extended_irq->interrupt_count == 0)
			pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
		else {
			for (i = 0; i < extended_irq->interrupt_count; i++) {
				pnpacpi_parse_allocated_irqresource(dev,
					extended_irq->interrupts[i],
					extended_irq->triggering,
					extended_irq->polarity,
					extended_irq->sharable);
			}

			/*
			 * The IRQ encoder puts a single interrupt in each
			 * descriptor, so if a _CRS descriptor has more than
			 * one interrupt, we won't be able to re-encode it.
			 */
			if (pnp_can_write(dev) &&
			    extended_irq->interrupt_count > 1) {
				dev_warn(&dev->dev, "multiple interrupts in "
					 "_CRS descriptor; configuration can't "
					 "be changed\n");
				dev->capabilities &= ~PNP_WRITE;
			}
451 452 453 454
		}
		break;

	case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
L
Linus Torvalds 已提交
455
		break;
456

L
Linus Torvalds 已提交
457
	default:
458 459
		dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
			 res->type);
L
Linus Torvalds 已提交
460 461
		return AE_ERROR;
	}
B
Bjorn Helgaas 已提交
462

L
Linus Torvalds 已提交
463 464 465
	return AE_OK;
}

466
int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
L
Linus Torvalds 已提交
467
{
468 469
	struct acpi_device *acpi_dev = dev->data;
	acpi_handle handle = acpi_dev->handle;
470
	acpi_status status;
471

B
Bjorn Helgaas 已提交
472
	pnp_dbg(&dev->dev, "parse allocated resources\n");
473

474
	pnp_init_resources(dev);
L
Linus Torvalds 已提交
475

476 477 478 479 480 481 482 483 484
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
				     pnpacpi_allocated_resource, dev);

	if (ACPI_FAILURE(status)) {
		if (status != AE_NOT_FOUND)
			dev_err(&dev->dev, "can't evaluate _CRS: %d", status);
		return -EPERM;
	}
	return 0;
L
Linus Torvalds 已提交
485 486
}

487
static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
488
					    unsigned int option_flags,
489
					    struct acpi_resource_dma *p)
L
Linus Torvalds 已提交
490 491
{
	int i;
492
	unsigned char map = 0, flags;
L
Linus Torvalds 已提交
493

B
Bob Moore 已提交
494
	if (p->channel_count == 0)
L
Linus Torvalds 已提交
495 496
		return;

B
Bjorn Helgaas 已提交
497
	for (i = 0; i < p->channel_count; i++)
498
		map |= 1 << p->channels[i];
L
Linus Torvalds 已提交
499

500
	flags = dma_flags(dev, p->type, p->bus_master, p->transfer);
501
	pnp_register_dma_resource(dev, option_flags, map, flags);
L
Linus Torvalds 已提交
502 503
}

504
static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
505
					    unsigned int option_flags,
506
					    struct acpi_resource_irq *p)
L
Linus Torvalds 已提交
507 508
{
	int i;
509 510
	pnp_irq_mask_t map;
	unsigned char flags;
B
Bjorn Helgaas 已提交
511

B
Bob Moore 已提交
512
	if (p->interrupt_count == 0)
L
Linus Torvalds 已提交
513 514
		return;

515
	bitmap_zero(map.bits, PNP_IRQ_NR);
B
Bjorn Helgaas 已提交
516
	for (i = 0; i < p->interrupt_count; i++)
L
Linus Torvalds 已提交
517
		if (p->interrupts[i])
518
			__set_bit(p->interrupts[i], map.bits);
L
Linus Torvalds 已提交
519

520
	flags = irq_flags(p->triggering, p->polarity, p->sharable);
521
	pnp_register_irq_resource(dev, option_flags, &map, flags);
L
Linus Torvalds 已提交
522 523
}

524
static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
525
					unsigned int option_flags,
526
					struct acpi_resource_extended_irq *p)
L
Linus Torvalds 已提交
527 528
{
	int i;
529 530
	pnp_irq_mask_t map;
	unsigned char flags;
L
Linus Torvalds 已提交
531

B
Bob Moore 已提交
532
	if (p->interrupt_count == 0)
L
Linus Torvalds 已提交
533 534
		return;

535
	bitmap_zero(map.bits, PNP_IRQ_NR);
536 537 538 539 540 541 542 543 544 545
	for (i = 0; i < p->interrupt_count; i++) {
		if (p->interrupts[i]) {
			if (p->interrupts[i] < PNP_IRQ_NR)
				__set_bit(p->interrupts[i], map.bits);
			else
				dev_err(&dev->dev, "ignoring IRQ %d option "
					"(too large for %d entry bitmap)\n",
					p->interrupts[i], PNP_IRQ_NR);
		}
	}
L
Linus Torvalds 已提交
546

547
	flags = irq_flags(p->triggering, p->polarity, p->sharable);
548
	pnp_register_irq_resource(dev, option_flags, &map, flags);
L
Linus Torvalds 已提交
549 550
}

551
static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
552
					     unsigned int option_flags,
553
					     struct acpi_resource_io *io)
L
Linus Torvalds 已提交
554
{
555
	unsigned char flags = 0;
L
Linus Torvalds 已提交
556

B
Bob Moore 已提交
557
	if (io->address_length == 0)
L
Linus Torvalds 已提交
558
		return;
559 560 561

	if (io->io_decode == ACPI_DECODE_16)
		flags = IORESOURCE_IO_16BIT_ADDR;
562
	pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
563
				   io->alignment, io->address_length, flags);
L
Linus Torvalds 已提交
564 565
}

566
static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
567
					unsigned int option_flags,
568
					struct acpi_resource_fixed_io *io)
L
Linus Torvalds 已提交
569
{
B
Bob Moore 已提交
570
	if (io->address_length == 0)
L
Linus Torvalds 已提交
571
		return;
572

573 574
	pnp_register_port_resource(dev, option_flags, io->address, io->address,
				   0, io->address_length, IORESOURCE_IO_FIXED);
L
Linus Torvalds 已提交
575 576
}

577
static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
578
					      unsigned int option_flags,
579
					      struct acpi_resource_memory24 *p)
L
Linus Torvalds 已提交
580
{
581
	unsigned char flags = 0;
L
Linus Torvalds 已提交
582

B
Bob Moore 已提交
583
	if (p->address_length == 0)
L
Linus Torvalds 已提交
584 585
		return;

586 587
	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
		flags = IORESOURCE_MEM_WRITEABLE;
588
	pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
589
				  p->alignment, p->address_length, flags);
L
Linus Torvalds 已提交
590 591
}

592
static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
593
					      unsigned int option_flags,
594
					      struct acpi_resource_memory32 *p)
L
Linus Torvalds 已提交
595
{
596
	unsigned char flags = 0;
L
Linus Torvalds 已提交
597

B
Bob Moore 已提交
598
	if (p->address_length == 0)
L
Linus Torvalds 已提交
599 600
		return;

601 602
	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
		flags = IORESOURCE_MEM_WRITEABLE;
603
	pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
604
				  p->alignment, p->address_length, flags);
L
Linus Torvalds 已提交
605 606
}

607
static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
608
					unsigned int option_flags,
609
					struct acpi_resource_fixed_memory32 *p)
L
Linus Torvalds 已提交
610
{
611
	unsigned char flags = 0;
L
Linus Torvalds 已提交
612

B
Bob Moore 已提交
613
	if (p->address_length == 0)
L
Linus Torvalds 已提交
614 615
		return;

616 617
	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
		flags = IORESOURCE_MEM_WRITEABLE;
618
	pnp_register_mem_resource(dev, option_flags, p->address, p->address,
619
				  0, p->address_length, flags);
L
Linus Torvalds 已提交
620 621
}

622
static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
623
						unsigned int option_flags,
624
						struct acpi_resource *r)
625 626 627
{
	struct acpi_resource_address64 addr, *p = &addr;
	acpi_status status;
628
	unsigned char flags = 0;
629 630

	status = acpi_resource_to_address64(r, p);
631 632
	if (ACPI_FAILURE(status)) {
		dev_warn(&dev->dev, "can't convert resource type %d\n",
B
Bjorn Helgaas 已提交
633
			 r->type);
634 635 636 637 638 639 640
		return;
	}

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

	if (p->resource_type == ACPI_MEMORY_RANGE) {
641 642
		if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
			flags = IORESOURCE_MEM_WRITEABLE;
643 644 645
		pnp_register_mem_resource(dev, option_flags, p->minimum,
					  p->minimum, 0, p->address_length,
					  flags);
646
	} else if (p->resource_type == ACPI_IO_RANGE)
647 648
		pnp_register_port_resource(dev, option_flags, p->minimum,
					   p->minimum, 0, p->address_length,
649
					   IORESOURCE_IO_FIXED);
650 651
}

652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
static __init void pnpacpi_parse_ext_address_option(struct pnp_dev *dev,
						    unsigned int option_flags,
						    struct acpi_resource *r)
{
	struct acpi_resource_extended_address64 *p = &r->data.ext_address64;
	unsigned char flags = 0;

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

	if (p->resource_type == ACPI_MEMORY_RANGE) {
		if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
			flags = IORESOURCE_MEM_WRITEABLE;
		pnp_register_mem_resource(dev, option_flags, p->minimum,
					  p->minimum, 0, p->address_length,
					  flags);
	} else if (p->resource_type == ACPI_IO_RANGE)
		pnp_register_port_resource(dev, option_flags, p->minimum,
					   p->minimum, 0, p->address_length,
					   IORESOURCE_IO_FIXED);
}

L
Linus Torvalds 已提交
674 675
struct acpipnp_parse_option_s {
	struct pnp_dev *dev;
676
	unsigned int option_flags;
L
Linus Torvalds 已提交
677 678
};

679 680
static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
						  void *data)
L
Linus Torvalds 已提交
681
{
682
	int priority;
683
	struct acpipnp_parse_option_s *parse_data = data;
L
Linus Torvalds 已提交
684
	struct pnp_dev *dev = parse_data->dev;
685
	unsigned int option_flags = parse_data->option_flags;
L
Linus Torvalds 已提交
686

687
	switch (res->type) {
B
Bjorn Helgaas 已提交
688
	case ACPI_RESOURCE_TYPE_IRQ:
689
		pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
B
Bjorn Helgaas 已提交
690
		break;
691

B
Bjorn Helgaas 已提交
692
	case ACPI_RESOURCE_TYPE_DMA:
693
		pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
B
Bjorn Helgaas 已提交
694
		break;
695

B
Bjorn Helgaas 已提交
696 697 698 699
	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
		switch (res->data.start_dpf.compatibility_priority) {
		case ACPI_GOOD_CONFIGURATION:
			priority = PNP_RES_PRIORITY_PREFERRED;
L
Linus Torvalds 已提交
700
			break;
701

B
Bjorn Helgaas 已提交
702 703
		case ACPI_ACCEPTABLE_CONFIGURATION:
			priority = PNP_RES_PRIORITY_ACCEPTABLE;
M
Matthieu Castet 已提交
704
			break;
705

B
Bjorn Helgaas 已提交
706 707
		case ACPI_SUB_OPTIMAL_CONFIGURATION:
			priority = PNP_RES_PRIORITY_FUNCTIONAL;
708
			break;
B
Bjorn Helgaas 已提交
709 710
		default:
			priority = PNP_RES_PRIORITY_INVALID;
711
			break;
B
Bjorn Helgaas 已提交
712
		}
713
		parse_data->option_flags = pnp_new_dependent_set(dev, priority);
B
Bjorn Helgaas 已提交
714
		break;
715

B
Bjorn Helgaas 已提交
716
	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
717
		parse_data->option_flags = 0;
B
Bjorn Helgaas 已提交
718
		break;
719

B
Bjorn Helgaas 已提交
720
	case ACPI_RESOURCE_TYPE_IO:
721
		pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
B
Bjorn Helgaas 已提交
722
		break;
723

B
Bjorn Helgaas 已提交
724
	case ACPI_RESOURCE_TYPE_FIXED_IO:
725
		pnpacpi_parse_fixed_port_option(dev, option_flags,
726
					        &res->data.fixed_io);
B
Bjorn Helgaas 已提交
727
		break;
728

B
Bjorn Helgaas 已提交
729 730 731
	case ACPI_RESOURCE_TYPE_VENDOR:
	case ACPI_RESOURCE_TYPE_END_TAG:
		break;
732

B
Bjorn Helgaas 已提交
733
	case ACPI_RESOURCE_TYPE_MEMORY24:
734 735
		pnpacpi_parse_mem24_option(dev, option_flags,
					   &res->data.memory24);
B
Bjorn Helgaas 已提交
736
		break;
737

B
Bjorn Helgaas 已提交
738
	case ACPI_RESOURCE_TYPE_MEMORY32:
739 740
		pnpacpi_parse_mem32_option(dev, option_flags,
					   &res->data.memory32);
B
Bjorn Helgaas 已提交
741
		break;
742

B
Bjorn Helgaas 已提交
743
	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
744
		pnpacpi_parse_fixed_mem32_option(dev, option_flags,
B
Bjorn Helgaas 已提交
745 746
						 &res->data.fixed_memory32);
		break;
747

B
Bjorn Helgaas 已提交
748 749 750
	case ACPI_RESOURCE_TYPE_ADDRESS16:
	case ACPI_RESOURCE_TYPE_ADDRESS32:
	case ACPI_RESOURCE_TYPE_ADDRESS64:
751
		pnpacpi_parse_address_option(dev, option_flags, res);
B
Bjorn Helgaas 已提交
752
		break;
753

B
Bjorn Helgaas 已提交
754
	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
755
		pnpacpi_parse_ext_address_option(dev, option_flags, res);
B
Bjorn Helgaas 已提交
756 757 758
		break;

	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
759
		pnpacpi_parse_ext_irq_option(dev, option_flags,
760
					     &res->data.extended_irq);
B
Bjorn Helgaas 已提交
761 762 763 764 765 766
		break;

	case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
		break;

	default:
767 768
		dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
			 res->type);
B
Bjorn Helgaas 已提交
769
		return AE_ERROR;
L
Linus Torvalds 已提交
770
	}
B
Bjorn Helgaas 已提交
771

L
Linus Torvalds 已提交
772 773 774
	return AE_OK;
}

775
int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
L
Linus Torvalds 已提交
776
{
777 778
	struct acpi_device *acpi_dev = dev->data;
	acpi_handle handle = acpi_dev->handle;
L
Linus Torvalds 已提交
779 780 781
	acpi_status status;
	struct acpipnp_parse_option_s parse_data;

B
Bjorn Helgaas 已提交
782
	pnp_dbg(&dev->dev, "parse resource options\n");
783

L
Linus Torvalds 已提交
784
	parse_data.dev = dev;
785 786
	parse_data.option_flags = 0;

B
Bob Moore 已提交
787
	status = acpi_walk_resources(handle, METHOD_NAME__PRS,
B
Bjorn Helgaas 已提交
788
				     pnpacpi_option_resource, &parse_data);
L
Linus Torvalds 已提交
789

790 791 792 793 794 795
	if (ACPI_FAILURE(status)) {
		if (status != AE_NOT_FOUND)
			dev_err(&dev->dev, "can't evaluate _PRS: %d", status);
		return -EPERM;
	}
	return 0;
L
Linus Torvalds 已提交
796 797
}

798
static int pnpacpi_supported_resource(struct acpi_resource *res)
L
Linus Torvalds 已提交
799
{
800
	switch (res->type) {
B
Bob Moore 已提交
801 802 803 804 805 806 807 808 809 810
	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:
811
	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
812
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
813
		return 1;
L
Linus Torvalds 已提交
814
	}
815 816 817 818 819 820 821
	return 0;
}

/*
 * Set resource
 */
static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
B
Bjorn Helgaas 已提交
822
					   void *data)
823
{
824
	int *res_cnt = data;
825 826 827

	if (pnpacpi_supported_resource(res))
		(*res_cnt)++;
L
Linus Torvalds 已提交
828 829 830
	return AE_OK;
}

B
Bjorn Helgaas 已提交
831
static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
L
Linus Torvalds 已提交
832
{
833
	struct acpi_resource **resource = data;
834 835

	if (pnpacpi_supported_resource(res)) {
836
		(*resource)->type = res->type;
837
		(*resource)->length = sizeof(struct acpi_resource);
838 839 840
		if (res->type == ACPI_RESOURCE_TYPE_IRQ)
			(*resource)->data.irq.descriptor_length =
					res->data.irq.descriptor_length;
L
Linus Torvalds 已提交
841 842 843 844 845 846
		(*resource)++;
	}

	return AE_OK;
}

847
int pnpacpi_build_resource_template(struct pnp_dev *dev,
B
Bjorn Helgaas 已提交
848
				    struct acpi_buffer *buffer)
L
Linus Torvalds 已提交
849
{
850 851
	struct acpi_device *acpi_dev = dev->data;
	acpi_handle handle = acpi_dev->handle;
L
Linus Torvalds 已提交
852 853 854 855
	struct acpi_resource *resource;
	int res_cnt = 0;
	acpi_status status;

B
Bob Moore 已提交
856
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
B
Bjorn Helgaas 已提交
857
				     pnpacpi_count_resources, &res_cnt);
L
Linus Torvalds 已提交
858
	if (ACPI_FAILURE(status)) {
859
		dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
L
Linus Torvalds 已提交
860 861 862 863 864
		return -EINVAL;
	}
	if (!res_cnt)
		return -EINVAL;
	buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
865
	buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
L
Linus Torvalds 已提交
866 867
	if (!buffer->pointer)
		return -ENOMEM;
868

L
Linus Torvalds 已提交
869
	resource = (struct acpi_resource *)buffer->pointer;
B
Bob Moore 已提交
870
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
B
Bjorn Helgaas 已提交
871
				     pnpacpi_type_resources, &resource);
L
Linus Torvalds 已提交
872 873
	if (ACPI_FAILURE(status)) {
		kfree(buffer->pointer);
874
		dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
L
Linus Torvalds 已提交
875 876 877
		return -EINVAL;
	}
	/* resource will pointer the end resource now */
B
Bob Moore 已提交
878
	resource->type = ACPI_RESOURCE_TYPE_END_TAG;
L
Linus Torvalds 已提交
879 880 881 882

	return 0;
}

883 884
static void pnpacpi_encode_irq(struct pnp_dev *dev,
			       struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
885
			       struct resource *p)
L
Linus Torvalds 已提交
886
{
887
	struct acpi_resource_irq *irq = &resource->data.irq;
888
	int triggering, polarity, shareable;
B
Bjorn Helgaas 已提交
889

890 891
	if (!pnp_resource_enabled(p)) {
		irq->interrupt_count = 0;
B
Bjorn Helgaas 已提交
892
		pnp_dbg(&dev->dev, "  encode irq (%s)\n",
893 894 895 896
			p ? "disabled" : "missing");
		return;
	}

897
	decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
898 899
	irq->triggering = triggering;
	irq->polarity = polarity;
900
	irq->sharable = shareable;
901 902
	irq->interrupt_count = 1;
	irq->interrupts[0] = p->start;
903

B
Bjorn Helgaas 已提交
904
	pnp_dbg(&dev->dev, "  encode irq %d %s %s %s (%d-byte descriptor)\n",
905
		(int) p->start,
906 907
		triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
		polarity == ACPI_ACTIVE_LOW ? "low" : "high",
908 909
		irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
		irq->descriptor_length);
L
Linus Torvalds 已提交
910 911
}

912 913
static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
				   struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
914
				   struct resource *p)
L
Linus Torvalds 已提交
915
{
916
	struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
917
	int triggering, polarity, shareable;
B
Bjorn Helgaas 已提交
918

919 920
	if (!pnp_resource_enabled(p)) {
		extended_irq->interrupt_count = 0;
B
Bjorn Helgaas 已提交
921
		pnp_dbg(&dev->dev, "  encode extended irq (%s)\n",
922 923 924 925
			p ? "disabled" : "missing");
		return;
	}

926
	decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
927 928 929
	extended_irq->producer_consumer = ACPI_CONSUMER;
	extended_irq->triggering = triggering;
	extended_irq->polarity = polarity;
930
	extended_irq->sharable = shareable;
931 932
	extended_irq->interrupt_count = 1;
	extended_irq->interrupts[0] = p->start;
933

B
Bjorn Helgaas 已提交
934
	pnp_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
935 936 937
		triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
		polarity == ACPI_ACTIVE_LOW ? "low" : "high",
		extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
L
Linus Torvalds 已提交
938 939
}

940 941
static void pnpacpi_encode_dma(struct pnp_dev *dev,
			       struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
942
			       struct resource *p)
L
Linus Torvalds 已提交
943
{
944 945
	struct acpi_resource_dma *dma = &resource->data.dma;

946 947
	if (!pnp_resource_enabled(p)) {
		dma->channel_count = 0;
B
Bjorn Helgaas 已提交
948
		pnp_dbg(&dev->dev, "  encode dma (%s)\n",
949 950 951 952
			p ? "disabled" : "missing");
		return;
	}

L
Linus Torvalds 已提交
953
	/* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
954
	switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
B
Bjorn Helgaas 已提交
955
	case IORESOURCE_DMA_TYPEA:
956
		dma->type = ACPI_TYPE_A;
B
Bjorn Helgaas 已提交
957 958
		break;
	case IORESOURCE_DMA_TYPEB:
959
		dma->type = ACPI_TYPE_B;
B
Bjorn Helgaas 已提交
960 961
		break;
	case IORESOURCE_DMA_TYPEF:
962
		dma->type = ACPI_TYPE_F;
B
Bjorn Helgaas 已提交
963 964
		break;
	default:
965
		dma->type = ACPI_COMPATIBILITY;
966 967 968
	}

	switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
B
Bjorn Helgaas 已提交
969
	case IORESOURCE_DMA_8BIT:
970
		dma->transfer = ACPI_TRANSFER_8;
B
Bjorn Helgaas 已提交
971 972
		break;
	case IORESOURCE_DMA_8AND16BIT:
973
		dma->transfer = ACPI_TRANSFER_8_16;
B
Bjorn Helgaas 已提交
974 975
		break;
	default:
976
		dma->transfer = ACPI_TRANSFER_16;
977 978
	}

979 980 981
	dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
	dma->channel_count = 1;
	dma->channels[0] = p->start;
982

B
Bjorn Helgaas 已提交
983
	pnp_dbg(&dev->dev, "  encode dma %d "
984 985
		"type %#x transfer %#x master %d\n",
		(int) p->start, dma->type, dma->transfer, dma->bus_master);
L
Linus Torvalds 已提交
986 987
}

988 989
static void pnpacpi_encode_io(struct pnp_dev *dev,
			      struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
990
			      struct resource *p)
L
Linus Torvalds 已提交
991
{
992 993
	struct acpi_resource_io *io = &resource->data.io;

994 995
	if (pnp_resource_enabled(p)) {
		/* Note: pnp_assign_port copies pnp_port->flags into p->flags */
996
		io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
997 998 999 1000 1001 1002 1003 1004 1005
		    ACPI_DECODE_16 : ACPI_DECODE_10;
		io->minimum = p->start;
		io->maximum = p->end;
		io->alignment = 0;	/* Correct? */
		io->address_length = p->end - p->start + 1;
	} else {
		io->minimum = 0;
		io->address_length = 0;
	}
1006

B
Bjorn Helgaas 已提交
1007
	pnp_dbg(&dev->dev, "  encode io %#x-%#x decode %#x\n", io->minimum,
1008
		io->minimum + io->address_length - 1, io->io_decode);
L
Linus Torvalds 已提交
1009 1010
}

1011 1012
static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
				    struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
1013
				    struct resource *p)
L
Linus Torvalds 已提交
1014
{
1015 1016
	struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;

1017 1018 1019 1020 1021 1022 1023
	if (pnp_resource_enabled(p)) {
		fixed_io->address = p->start;
		fixed_io->address_length = p->end - p->start + 1;
	} else {
		fixed_io->address = 0;
		fixed_io->address_length = 0;
	}
1024

B
Bjorn Helgaas 已提交
1025
	pnp_dbg(&dev->dev, "  encode fixed_io %#x-%#x\n", fixed_io->address,
1026
		fixed_io->address + fixed_io->address_length - 1);
L
Linus Torvalds 已提交
1027 1028
}

1029 1030
static void pnpacpi_encode_mem24(struct pnp_dev *dev,
				 struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
1031
				 struct resource *p)
L
Linus Torvalds 已提交
1032
{
1033 1034
	struct acpi_resource_memory24 *memory24 = &resource->data.memory24;

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	if (pnp_resource_enabled(p)) {
		/* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
		memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
		    ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
		memory24->minimum = p->start;
		memory24->maximum = p->end;
		memory24->alignment = 0;
		memory24->address_length = p->end - p->start + 1;
	} else {
		memory24->minimum = 0;
		memory24->address_length = 0;
	}

B
Bjorn Helgaas 已提交
1048
	pnp_dbg(&dev->dev, "  encode mem24 %#x-%#x write_protect %#x\n",
1049 1050
		memory24->minimum,
		memory24->minimum + memory24->address_length - 1,
1051
		memory24->write_protect);
L
Linus Torvalds 已提交
1052 1053
}

1054 1055
static void pnpacpi_encode_mem32(struct pnp_dev *dev,
				 struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
1056
				 struct resource *p)
L
Linus Torvalds 已提交
1057
{
1058 1059
	struct acpi_resource_memory32 *memory32 = &resource->data.memory32;

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	if (pnp_resource_enabled(p)) {
		memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
		    ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
		memory32->minimum = p->start;
		memory32->maximum = p->end;
		memory32->alignment = 0;
		memory32->address_length = p->end - p->start + 1;
	} else {
		memory32->minimum = 0;
		memory32->alignment = 0;
	}
1071

B
Bjorn Helgaas 已提交
1072
	pnp_dbg(&dev->dev, "  encode mem32 %#x-%#x write_protect %#x\n",
1073 1074
		memory32->minimum,
		memory32->minimum + memory32->address_length - 1,
1075
		memory32->write_protect);
L
Linus Torvalds 已提交
1076 1077
}

1078 1079
static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
				       struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
1080
				       struct resource *p)
L
Linus Torvalds 已提交
1081
{
1082 1083
	struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	if (pnp_resource_enabled(p)) {
		fixed_memory32->write_protect =
		    p->flags & IORESOURCE_MEM_WRITEABLE ?
		    ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
		fixed_memory32->address = p->start;
		fixed_memory32->address_length = p->end - p->start + 1;
	} else {
		fixed_memory32->address = 0;
		fixed_memory32->address_length = 0;
	}
1094

B
Bjorn Helgaas 已提交
1095
	pnp_dbg(&dev->dev, "  encode fixed_mem32 %#x-%#x write_protect %#x\n",
1096 1097
		fixed_memory32->address,
		fixed_memory32->address + fixed_memory32->address_length - 1,
1098
		fixed_memory32->write_protect);
L
Linus Torvalds 已提交
1099 1100
}

1101
int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
L
Linus Torvalds 已提交
1102 1103 1104
{
	int i = 0;
	/* pnpacpi_build_resource_template allocates extra mem */
B
Bjorn Helgaas 已提交
1105
	int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1106
	struct acpi_resource *resource = buffer->pointer;
L
Linus Torvalds 已提交
1107 1108
	int port = 0, irq = 0, dma = 0, mem = 0;

B
Bjorn Helgaas 已提交
1109
	pnp_dbg(&dev->dev, "encode %d resources\n", res_cnt);
L
Linus Torvalds 已提交
1110
	while (i < res_cnt) {
B
Bjorn Helgaas 已提交
1111
		switch (resource->type) {
B
Bob Moore 已提交
1112
		case ACPI_RESOURCE_TYPE_IRQ:
1113
			pnpacpi_encode_irq(dev, resource,
1114
			       pnp_get_resource(dev, IORESOURCE_IRQ, irq));
L
Linus Torvalds 已提交
1115 1116 1117
			irq++;
			break;

B
Bob Moore 已提交
1118
		case ACPI_RESOURCE_TYPE_DMA:
1119
			pnpacpi_encode_dma(dev, resource,
1120
				pnp_get_resource(dev, IORESOURCE_DMA, dma));
B
Bjorn Helgaas 已提交
1121
			dma++;
L
Linus Torvalds 已提交
1122
			break;
B
Bob Moore 已提交
1123
		case ACPI_RESOURCE_TYPE_IO:
1124
			pnpacpi_encode_io(dev, resource,
1125
				pnp_get_resource(dev, IORESOURCE_IO, port));
B
Bjorn Helgaas 已提交
1126
			port++;
L
Linus Torvalds 已提交
1127
			break;
B
Bob Moore 已提交
1128
		case ACPI_RESOURCE_TYPE_FIXED_IO:
1129
			pnpacpi_encode_fixed_io(dev, resource,
1130
				pnp_get_resource(dev, IORESOURCE_IO, port));
B
Bjorn Helgaas 已提交
1131
			port++;
L
Linus Torvalds 已提交
1132
			break;
B
Bob Moore 已提交
1133
		case ACPI_RESOURCE_TYPE_MEMORY24:
1134
			pnpacpi_encode_mem24(dev, resource,
1135
				pnp_get_resource(dev, IORESOURCE_MEM, mem));
B
Bjorn Helgaas 已提交
1136
			mem++;
L
Linus Torvalds 已提交
1137
			break;
B
Bob Moore 已提交
1138
		case ACPI_RESOURCE_TYPE_MEMORY32:
1139
			pnpacpi_encode_mem32(dev, resource,
1140
				pnp_get_resource(dev, IORESOURCE_MEM, mem));
B
Bjorn Helgaas 已提交
1141
			mem++;
L
Linus Torvalds 已提交
1142
			break;
B
Bob Moore 已提交
1143
		case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1144
			pnpacpi_encode_fixed_mem32(dev, resource,
1145
				pnp_get_resource(dev, IORESOURCE_MEM, mem));
B
Bjorn Helgaas 已提交
1146
			mem++;
L
Linus Torvalds 已提交
1147
			break;
1148
		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1149
			pnpacpi_encode_ext_irq(dev, resource,
1150
				pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
			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:
B
Bjorn Helgaas 已提交
1162
		default:	/* other type */
1163 1164
			dev_warn(&dev->dev, "can't encode unknown resource "
				 "type %d\n", resource->type);
L
Linus Torvalds 已提交
1165 1166
			return -EINVAL;
		}
B
Bjorn Helgaas 已提交
1167 1168
		resource++;
		i++;
L
Linus Torvalds 已提交
1169 1170 1171
	}
	return 0;
}