rsparser.c 29.8 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);
B
Bob Moore 已提交
126
	irq = acpi_register_gsi(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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static int dma_flags(int type, int bus_master, int transfer)
{
	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;
		pnp_err("Invalid DMA type");
	}
	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;
		pnp_err("Invalid DMA transfer type");
	}

	return flags;
}

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

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

	pnp_add_io_resource(dev, start, end, flags);
L
Linus Torvalds 已提交
190 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
/*
 * 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);
	}
}

247
static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
248
						u64 start, u64 len,
B
Bjorn Helgaas 已提交
249
						int write_protect)
L
Linus Torvalds 已提交
250
{
251 252 253 254 255 256 257 258 259
	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 已提交
260 261
}

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

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

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

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

static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
B
Bjorn Helgaas 已提交
290
					      void *data)
L
Linus Torvalds 已提交
291
{
292
	struct pnp_dev *dev = data;
293 294 295 296
	struct acpi_resource_irq *irq;
	struct acpi_resource_dma *dma;
	struct acpi_resource_io *io;
	struct acpi_resource_fixed_io *fixed_io;
297
	struct acpi_resource_vendor_typed *vendor_typed;
298 299 300 301
	struct acpi_resource_memory24 *memory24;
	struct acpi_resource_memory32 *memory32;
	struct acpi_resource_fixed_memory32 *fixed_memory32;
	struct acpi_resource_extended_irq *extended_irq;
302
	int i, flags;
L
Linus Torvalds 已提交
303

304
	switch (res->type) {
B
Bob Moore 已提交
305
	case ACPI_RESOURCE_TYPE_IRQ:
306 307 308 309
		/*
		 * Per spec, only one interrupt per descriptor is allowed in
		 * _CRS, but some firmware violates this, so parse them all.
		 */
310
		irq = &res->data.irq;
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
		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 已提交
333 334 335
		}
		break;

B
Bob Moore 已提交
336
	case ACPI_RESOURCE_TYPE_DMA:
337
		dma = &res->data.dma;
338
		if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
339 340
			flags = dma_flags(dma->type, dma->bus_master,
					  dma->transfer);
341 342 343
		else
			flags = IORESOURCE_DISABLED;
		pnp_add_dma_resource(dev, dma->channels[0], flags);
L
Linus Torvalds 已提交
344
		break;
345

B
Bob Moore 已提交
346
	case ACPI_RESOURCE_TYPE_IO:
347
		io = &res->data.io;
348
		pnpacpi_parse_allocated_ioresource(dev,
349 350 351
			io->minimum,
			io->address_length,
			io->io_decode);
L
Linus Torvalds 已提交
352
		break;
353 354 355 356 357

	case ACPI_RESOURCE_TYPE_START_DEPENDENT:
	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
		break;

B
Bob Moore 已提交
358
	case ACPI_RESOURCE_TYPE_FIXED_IO:
359
		fixed_io = &res->data.fixed_io;
360
		pnpacpi_parse_allocated_ioresource(dev,
361 362
			fixed_io->address,
			fixed_io->address_length,
B
Bjorn Helgaas 已提交
363
			ACPI_DECODE_10);
L
Linus Torvalds 已提交
364
		break;
365 366

	case ACPI_RESOURCE_TYPE_VENDOR:
367 368
		vendor_typed = &res->data.vendor_typed;
		pnpacpi_parse_allocated_vendor(dev, vendor_typed);
369 370 371 372 373
		break;

	case ACPI_RESOURCE_TYPE_END_TAG:
		break;

B
Bob Moore 已提交
374
	case ACPI_RESOURCE_TYPE_MEMORY24:
375
		memory24 = &res->data.memory24;
376
		pnpacpi_parse_allocated_memresource(dev,
377 378 379
			memory24->minimum,
			memory24->address_length,
			memory24->write_protect);
L
Linus Torvalds 已提交
380
		break;
B
Bob Moore 已提交
381
	case ACPI_RESOURCE_TYPE_MEMORY32:
382
		memory32 = &res->data.memory32;
383
		pnpacpi_parse_allocated_memresource(dev,
384 385 386
			memory32->minimum,
			memory32->address_length,
			memory32->write_protect);
L
Linus Torvalds 已提交
387
		break;
B
Bob Moore 已提交
388
	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
389
		fixed_memory32 = &res->data.fixed_memory32;
390
		pnpacpi_parse_allocated_memresource(dev,
391 392 393
			fixed_memory32->address,
			fixed_memory32->address_length,
			fixed_memory32->write_protect);
L
Linus Torvalds 已提交
394
		break;
B
Bob Moore 已提交
395 396 397
	case ACPI_RESOURCE_TYPE_ADDRESS16:
	case ACPI_RESOURCE_TYPE_ADDRESS32:
	case ACPI_RESOURCE_TYPE_ADDRESS64:
398
		pnpacpi_parse_allocated_address_space(dev, res);
L
Linus Torvalds 已提交
399
		break;
400 401

	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
402 403
		if (res->data.ext_address64.producer_consumer == ACPI_PRODUCER)
			return AE_OK;
404 405 406
		break;

	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
407
		extended_irq = &res->data.extended_irq;
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
		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;
			}
432 433 434 435
		}
		break;

	case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
L
Linus Torvalds 已提交
436
		break;
437

L
Linus Torvalds 已提交
438
	default:
439 440
		dev_warn(&dev->dev, "unknown resource type %d in _CRS\n",
			 res->type);
L
Linus Torvalds 已提交
441 442
		return AE_ERROR;
	}
B
Bjorn Helgaas 已提交
443

L
Linus Torvalds 已提交
444 445 446
	return AE_OK;
}

447
int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
L
Linus Torvalds 已提交
448
{
449
	acpi_handle handle = dev->data;
450
	acpi_status status;
451

452 453
	dev_dbg(&dev->dev, "parse allocated resources\n");

454
	pnp_init_resources(dev);
L
Linus Torvalds 已提交
455

456 457 458 459 460 461 462 463 464
	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 已提交
465 466
}

467
static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
468
					    unsigned int option_flags,
469
					    struct acpi_resource_dma *p)
L
Linus Torvalds 已提交
470 471
{
	int i;
472
	unsigned char map = 0, flags;
L
Linus Torvalds 已提交
473

B
Bob Moore 已提交
474
	if (p->channel_count == 0)
L
Linus Torvalds 已提交
475 476
		return;

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

480
	flags = dma_flags(p->type, p->bus_master, p->transfer);
481
	pnp_register_dma_resource(dev, option_flags, map, flags);
L
Linus Torvalds 已提交
482 483
}

484
static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
485
					    unsigned int option_flags,
486
					    struct acpi_resource_irq *p)
L
Linus Torvalds 已提交
487 488
{
	int i;
489 490
	pnp_irq_mask_t map;
	unsigned char flags;
B
Bjorn Helgaas 已提交
491

B
Bob Moore 已提交
492
	if (p->interrupt_count == 0)
L
Linus Torvalds 已提交
493 494
		return;

495
	bitmap_zero(map.bits, PNP_IRQ_NR);
B
Bjorn Helgaas 已提交
496
	for (i = 0; i < p->interrupt_count; i++)
L
Linus Torvalds 已提交
497
		if (p->interrupts[i])
498
			__set_bit(p->interrupts[i], map.bits);
L
Linus Torvalds 已提交
499

500
	flags = irq_flags(p->triggering, p->polarity, p->sharable);
501
	pnp_register_irq_resource(dev, option_flags, &map, flags);
L
Linus Torvalds 已提交
502 503
}

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

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

515
	bitmap_zero(map.bits, PNP_IRQ_NR);
516 517 518 519 520 521 522 523 524 525
	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 已提交
526

527
	flags = irq_flags(p->triggering, p->polarity, p->sharable);
528
	pnp_register_irq_resource(dev, option_flags, &map, flags);
L
Linus Torvalds 已提交
529 530
}

531
static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
532
					     unsigned int option_flags,
533
					     struct acpi_resource_io *io)
L
Linus Torvalds 已提交
534
{
535
	unsigned char flags = 0;
L
Linus Torvalds 已提交
536

B
Bob Moore 已提交
537
	if (io->address_length == 0)
L
Linus Torvalds 已提交
538
		return;
539 540 541

	if (io->io_decode == ACPI_DECODE_16)
		flags = IORESOURCE_IO_16BIT_ADDR;
542
	pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
543
				   io->alignment, io->address_length, flags);
L
Linus Torvalds 已提交
544 545
}

546
static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
547
					unsigned int option_flags,
548
					struct acpi_resource_fixed_io *io)
L
Linus Torvalds 已提交
549
{
B
Bob Moore 已提交
550
	if (io->address_length == 0)
L
Linus Torvalds 已提交
551
		return;
552

553 554
	pnp_register_port_resource(dev, option_flags, io->address, io->address,
				   0, io->address_length, IORESOURCE_IO_FIXED);
L
Linus Torvalds 已提交
555 556
}

557
static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
558
					      unsigned int option_flags,
559
					      struct acpi_resource_memory24 *p)
L
Linus Torvalds 已提交
560
{
561
	unsigned char flags = 0;
L
Linus Torvalds 已提交
562

B
Bob Moore 已提交
563
	if (p->address_length == 0)
L
Linus Torvalds 已提交
564 565
		return;

566 567
	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
		flags = IORESOURCE_MEM_WRITEABLE;
568
	pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
569
				  p->alignment, p->address_length, flags);
L
Linus Torvalds 已提交
570 571
}

572
static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
573
					      unsigned int option_flags,
574
					      struct acpi_resource_memory32 *p)
L
Linus Torvalds 已提交
575
{
576
	unsigned char flags = 0;
L
Linus Torvalds 已提交
577

B
Bob Moore 已提交
578
	if (p->address_length == 0)
L
Linus Torvalds 已提交
579 580
		return;

581 582
	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
		flags = IORESOURCE_MEM_WRITEABLE;
583
	pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
584
				  p->alignment, p->address_length, flags);
L
Linus Torvalds 已提交
585 586
}

587
static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
588
					unsigned int option_flags,
589
					struct acpi_resource_fixed_memory32 *p)
L
Linus Torvalds 已提交
590
{
591
	unsigned char flags = 0;
L
Linus Torvalds 已提交
592

B
Bob Moore 已提交
593
	if (p->address_length == 0)
L
Linus Torvalds 已提交
594 595
		return;

596 597
	if (p->write_protect == ACPI_READ_WRITE_MEMORY)
		flags = IORESOURCE_MEM_WRITEABLE;
598
	pnp_register_mem_resource(dev, option_flags, p->address, p->address,
599
				  0, p->address_length, flags);
L
Linus Torvalds 已提交
600 601
}

602
static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
603
						unsigned int option_flags,
604
						struct acpi_resource *r)
605 606 607
{
	struct acpi_resource_address64 addr, *p = &addr;
	acpi_status status;
608
	unsigned char flags = 0;
609 610 611

	status = acpi_resource_to_address64(r, p);
	if (!ACPI_SUCCESS(status)) {
B
Bjorn Helgaas 已提交
612 613
		pnp_warn("PnPACPI: failed to convert resource type %d",
			 r->type);
614 615 616 617 618 619 620
		return;
	}

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

	if (p->resource_type == ACPI_MEMORY_RANGE) {
621 622
		if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
			flags = IORESOURCE_MEM_WRITEABLE;
623 624 625
		pnp_register_mem_resource(dev, option_flags, p->minimum,
					  p->minimum, 0, p->address_length,
					  flags);
626
	} else if (p->resource_type == ACPI_IO_RANGE)
627 628
		pnp_register_port_resource(dev, option_flags, p->minimum,
					   p->minimum, 0, p->address_length,
629
					   IORESOURCE_IO_FIXED);
630 631
}

L
Linus Torvalds 已提交
632 633
struct acpipnp_parse_option_s {
	struct pnp_dev *dev;
634
	unsigned int option_flags;
L
Linus Torvalds 已提交
635 636
};

637 638
static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
						  void *data)
L
Linus Torvalds 已提交
639
{
640
	int priority;
641
	struct acpipnp_parse_option_s *parse_data = data;
L
Linus Torvalds 已提交
642
	struct pnp_dev *dev = parse_data->dev;
643
	unsigned int option_flags = parse_data->option_flags;
L
Linus Torvalds 已提交
644

645
	switch (res->type) {
B
Bjorn Helgaas 已提交
646
	case ACPI_RESOURCE_TYPE_IRQ:
647
		pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
B
Bjorn Helgaas 已提交
648
		break;
649

B
Bjorn Helgaas 已提交
650
	case ACPI_RESOURCE_TYPE_DMA:
651
		pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
B
Bjorn Helgaas 已提交
652
		break;
653

B
Bjorn Helgaas 已提交
654 655 656 657
	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 已提交
658
			break;
659

B
Bjorn Helgaas 已提交
660 661
		case ACPI_ACCEPTABLE_CONFIGURATION:
			priority = PNP_RES_PRIORITY_ACCEPTABLE;
M
Matthieu Castet 已提交
662
			break;
663

B
Bjorn Helgaas 已提交
664 665
		case ACPI_SUB_OPTIMAL_CONFIGURATION:
			priority = PNP_RES_PRIORITY_FUNCTIONAL;
666
			break;
B
Bjorn Helgaas 已提交
667 668
		default:
			priority = PNP_RES_PRIORITY_INVALID;
669
			break;
B
Bjorn Helgaas 已提交
670
		}
671
		parse_data->option_flags = pnp_new_dependent_set(dev, priority);
B
Bjorn Helgaas 已提交
672
		break;
673

B
Bjorn Helgaas 已提交
674
	case ACPI_RESOURCE_TYPE_END_DEPENDENT:
675
		parse_data->option_flags = 0;
B
Bjorn Helgaas 已提交
676
		break;
677

B
Bjorn Helgaas 已提交
678
	case ACPI_RESOURCE_TYPE_IO:
679
		pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
B
Bjorn Helgaas 已提交
680
		break;
681

B
Bjorn Helgaas 已提交
682
	case ACPI_RESOURCE_TYPE_FIXED_IO:
683
		pnpacpi_parse_fixed_port_option(dev, option_flags,
684
					        &res->data.fixed_io);
B
Bjorn Helgaas 已提交
685
		break;
686

B
Bjorn Helgaas 已提交
687 688 689
	case ACPI_RESOURCE_TYPE_VENDOR:
	case ACPI_RESOURCE_TYPE_END_TAG:
		break;
690

B
Bjorn Helgaas 已提交
691
	case ACPI_RESOURCE_TYPE_MEMORY24:
692 693
		pnpacpi_parse_mem24_option(dev, option_flags,
					   &res->data.memory24);
B
Bjorn Helgaas 已提交
694
		break;
695

B
Bjorn Helgaas 已提交
696
	case ACPI_RESOURCE_TYPE_MEMORY32:
697 698
		pnpacpi_parse_mem32_option(dev, option_flags,
					   &res->data.memory32);
B
Bjorn Helgaas 已提交
699
		break;
700

B
Bjorn Helgaas 已提交
701
	case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
702
		pnpacpi_parse_fixed_mem32_option(dev, option_flags,
B
Bjorn Helgaas 已提交
703 704
						 &res->data.fixed_memory32);
		break;
705

B
Bjorn Helgaas 已提交
706 707 708
	case ACPI_RESOURCE_TYPE_ADDRESS16:
	case ACPI_RESOURCE_TYPE_ADDRESS32:
	case ACPI_RESOURCE_TYPE_ADDRESS64:
709
		pnpacpi_parse_address_option(dev, option_flags, res);
B
Bjorn Helgaas 已提交
710
		break;
711

B
Bjorn Helgaas 已提交
712 713 714 715
	case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
		break;

	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
716
		pnpacpi_parse_ext_irq_option(dev, option_flags,
717
					     &res->data.extended_irq);
B
Bjorn Helgaas 已提交
718 719 720 721 722 723
		break;

	case ACPI_RESOURCE_TYPE_GENERIC_REGISTER:
		break;

	default:
724 725
		dev_warn(&dev->dev, "unknown resource type %d in _PRS\n",
			 res->type);
B
Bjorn Helgaas 已提交
726
		return AE_ERROR;
L
Linus Torvalds 已提交
727
	}
B
Bjorn Helgaas 已提交
728

L
Linus Torvalds 已提交
729 730 731
	return AE_OK;
}

732
int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
L
Linus Torvalds 已提交
733
{
734
	acpi_handle handle = dev->data;
L
Linus Torvalds 已提交
735 736 737
	acpi_status status;
	struct acpipnp_parse_option_s parse_data;

738 739
	dev_dbg(&dev->dev, "parse resource options\n");

L
Linus Torvalds 已提交
740
	parse_data.dev = dev;
741 742
	parse_data.option_flags = 0;

B
Bob Moore 已提交
743
	status = acpi_walk_resources(handle, METHOD_NAME__PRS,
B
Bjorn Helgaas 已提交
744
				     pnpacpi_option_resource, &parse_data);
L
Linus Torvalds 已提交
745

746 747 748 749 750 751
	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 已提交
752 753
}

754
static int pnpacpi_supported_resource(struct acpi_resource *res)
L
Linus Torvalds 已提交
755
{
756
	switch (res->type) {
B
Bob Moore 已提交
757 758 759 760 761 762 763 764 765 766
	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:
767
	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
768
		return 1;
L
Linus Torvalds 已提交
769
	}
770 771 772 773 774 775 776
	return 0;
}

/*
 * Set resource
 */
static acpi_status pnpacpi_count_resources(struct acpi_resource *res,
B
Bjorn Helgaas 已提交
777
					   void *data)
778
{
779
	int *res_cnt = data;
780 781 782

	if (pnpacpi_supported_resource(res))
		(*res_cnt)++;
L
Linus Torvalds 已提交
783 784 785
	return AE_OK;
}

B
Bjorn Helgaas 已提交
786
static acpi_status pnpacpi_type_resources(struct acpi_resource *res, void *data)
L
Linus Torvalds 已提交
787
{
788
	struct acpi_resource **resource = data;
789 790

	if (pnpacpi_supported_resource(res)) {
791
		(*resource)->type = res->type;
792
		(*resource)->length = sizeof(struct acpi_resource);
793 794 795
		if (res->type == ACPI_RESOURCE_TYPE_IRQ)
			(*resource)->data.irq.descriptor_length =
					res->data.irq.descriptor_length;
L
Linus Torvalds 已提交
796 797 798 799 800 801
		(*resource)++;
	}

	return AE_OK;
}

802
int pnpacpi_build_resource_template(struct pnp_dev *dev,
B
Bjorn Helgaas 已提交
803
				    struct acpi_buffer *buffer)
L
Linus Torvalds 已提交
804
{
805
	acpi_handle handle = dev->data;
L
Linus Torvalds 已提交
806 807 808 809
	struct acpi_resource *resource;
	int res_cnt = 0;
	acpi_status status;

B
Bob Moore 已提交
810
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
B
Bjorn Helgaas 已提交
811
				     pnpacpi_count_resources, &res_cnt);
L
Linus Torvalds 已提交
812
	if (ACPI_FAILURE(status)) {
813
		dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
L
Linus Torvalds 已提交
814 815 816 817 818
		return -EINVAL;
	}
	if (!res_cnt)
		return -EINVAL;
	buffer->length = sizeof(struct acpi_resource) * (res_cnt + 1) + 1;
819
	buffer->pointer = kzalloc(buffer->length - 1, GFP_KERNEL);
L
Linus Torvalds 已提交
820 821
	if (!buffer->pointer)
		return -ENOMEM;
822

L
Linus Torvalds 已提交
823
	resource = (struct acpi_resource *)buffer->pointer;
B
Bob Moore 已提交
824
	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
B
Bjorn Helgaas 已提交
825
				     pnpacpi_type_resources, &resource);
L
Linus Torvalds 已提交
826 827
	if (ACPI_FAILURE(status)) {
		kfree(buffer->pointer);
828
		dev_err(&dev->dev, "can't evaluate _CRS: %d\n", status);
L
Linus Torvalds 已提交
829 830 831
		return -EINVAL;
	}
	/* resource will pointer the end resource now */
B
Bob Moore 已提交
832
	resource->type = ACPI_RESOURCE_TYPE_END_TAG;
L
Linus Torvalds 已提交
833 834 835 836

	return 0;
}

837 838
static void pnpacpi_encode_irq(struct pnp_dev *dev,
			       struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
839
			       struct resource *p)
L
Linus Torvalds 已提交
840
{
841
	struct acpi_resource_irq *irq = &resource->data.irq;
842
	int triggering, polarity, shareable;
B
Bjorn Helgaas 已提交
843

844 845 846 847 848 849 850
	if (!pnp_resource_enabled(p)) {
		irq->interrupt_count = 0;
		dev_dbg(&dev->dev, "  encode irq (%s)\n",
			p ? "disabled" : "missing");
		return;
	}

851
	decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
852 853
	irq->triggering = triggering;
	irq->polarity = polarity;
854
	irq->sharable = shareable;
855 856
	irq->interrupt_count = 1;
	irq->interrupts[0] = p->start;
857

858 859
	dev_dbg(&dev->dev, "  encode irq %d %s %s %s (%d-byte descriptor)\n",
		(int) p->start,
860 861
		triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
		polarity == ACPI_ACTIVE_LOW ? "low" : "high",
862 863
		irq->sharable == ACPI_SHARED ? "shared" : "exclusive",
		irq->descriptor_length);
L
Linus Torvalds 已提交
864 865
}

866 867
static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
				   struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
868
				   struct resource *p)
L
Linus Torvalds 已提交
869
{
870
	struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
871
	int triggering, polarity, shareable;
B
Bjorn Helgaas 已提交
872

873 874 875 876 877 878 879
	if (!pnp_resource_enabled(p)) {
		extended_irq->interrupt_count = 0;
		dev_dbg(&dev->dev, "  encode extended irq (%s)\n",
			p ? "disabled" : "missing");
		return;
	}

880
	decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
881 882 883
	extended_irq->producer_consumer = ACPI_CONSUMER;
	extended_irq->triggering = triggering;
	extended_irq->polarity = polarity;
884
	extended_irq->sharable = shareable;
885 886
	extended_irq->interrupt_count = 1;
	extended_irq->interrupts[0] = p->start;
887 888 889 890 891

	dev_dbg(&dev->dev, "  encode irq %d %s %s %s\n", (int) p->start,
		triggering == ACPI_LEVEL_SENSITIVE ? "level" : "edge",
		polarity == ACPI_ACTIVE_LOW ? "low" : "high",
		extended_irq->sharable == ACPI_SHARED ? "shared" : "exclusive");
L
Linus Torvalds 已提交
892 893
}

894 895
static void pnpacpi_encode_dma(struct pnp_dev *dev,
			       struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
896
			       struct resource *p)
L
Linus Torvalds 已提交
897
{
898 899
	struct acpi_resource_dma *dma = &resource->data.dma;

900 901 902 903 904 905 906
	if (!pnp_resource_enabled(p)) {
		dma->channel_count = 0;
		dev_dbg(&dev->dev, "  encode dma (%s)\n",
			p ? "disabled" : "missing");
		return;
	}

L
Linus Torvalds 已提交
907
	/* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
908
	switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
B
Bjorn Helgaas 已提交
909
	case IORESOURCE_DMA_TYPEA:
910
		dma->type = ACPI_TYPE_A;
B
Bjorn Helgaas 已提交
911 912
		break;
	case IORESOURCE_DMA_TYPEB:
913
		dma->type = ACPI_TYPE_B;
B
Bjorn Helgaas 已提交
914 915
		break;
	case IORESOURCE_DMA_TYPEF:
916
		dma->type = ACPI_TYPE_F;
B
Bjorn Helgaas 已提交
917 918
		break;
	default:
919
		dma->type = ACPI_COMPATIBILITY;
920 921 922
	}

	switch (p->flags & IORESOURCE_DMA_TYPE_MASK) {
B
Bjorn Helgaas 已提交
923
	case IORESOURCE_DMA_8BIT:
924
		dma->transfer = ACPI_TRANSFER_8;
B
Bjorn Helgaas 已提交
925 926
		break;
	case IORESOURCE_DMA_8AND16BIT:
927
		dma->transfer = ACPI_TRANSFER_8_16;
B
Bjorn Helgaas 已提交
928 929
		break;
	default:
930
		dma->transfer = ACPI_TRANSFER_16;
931 932
	}

933 934 935
	dma->bus_master = !!(p->flags & IORESOURCE_DMA_MASTER);
	dma->channel_count = 1;
	dma->channels[0] = p->start;
936 937 938 939

	dev_dbg(&dev->dev, "  encode dma %d "
		"type %#x transfer %#x master %d\n",
		(int) p->start, dma->type, dma->transfer, dma->bus_master);
L
Linus Torvalds 已提交
940 941
}

942 943
static void pnpacpi_encode_io(struct pnp_dev *dev,
			      struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
944
			      struct resource *p)
L
Linus Torvalds 已提交
945
{
946 947
	struct acpi_resource_io *io = &resource->data.io;

948 949
	if (pnp_resource_enabled(p)) {
		/* Note: pnp_assign_port copies pnp_port->flags into p->flags */
950
		io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
951 952 953 954 955 956 957 958 959
		    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;
	}
960

961 962
	dev_dbg(&dev->dev, "  encode io %#x-%#x decode %#x\n", io->minimum,
		io->minimum + io->address_length - 1, io->io_decode);
L
Linus Torvalds 已提交
963 964
}

965 966
static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
				    struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
967
				    struct resource *p)
L
Linus Torvalds 已提交
968
{
969 970
	struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;

971 972 973 974 975 976 977
	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;
	}
978

979 980
	dev_dbg(&dev->dev, "  encode fixed_io %#x-%#x\n", fixed_io->address,
		fixed_io->address + fixed_io->address_length - 1);
L
Linus Torvalds 已提交
981 982
}

983 984
static void pnpacpi_encode_mem24(struct pnp_dev *dev,
				 struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
985
				 struct resource *p)
L
Linus Torvalds 已提交
986
{
987 988
	struct acpi_resource_memory24 *memory24 = &resource->data.memory24;

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
	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;
	}

	dev_dbg(&dev->dev, "  encode mem24 %#x-%#x write_protect %#x\n",
		memory24->minimum,
		memory24->minimum + memory24->address_length - 1,
1005
		memory24->write_protect);
L
Linus Torvalds 已提交
1006 1007
}

1008 1009
static void pnpacpi_encode_mem32(struct pnp_dev *dev,
				 struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
1010
				 struct resource *p)
L
Linus Torvalds 已提交
1011
{
1012 1013
	struct acpi_resource_memory32 *memory32 = &resource->data.memory32;

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	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;
	}
1025

1026 1027 1028
	dev_dbg(&dev->dev, "  encode mem32 %#x-%#x write_protect %#x\n",
		memory32->minimum,
		memory32->minimum + memory32->address_length - 1,
1029
		memory32->write_protect);
L
Linus Torvalds 已提交
1030 1031
}

1032 1033
static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
				       struct acpi_resource *resource,
B
Bjorn Helgaas 已提交
1034
				       struct resource *p)
L
Linus Torvalds 已提交
1035
{
1036 1037
	struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	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;
	}
1048

1049 1050 1051
	dev_dbg(&dev->dev, "  encode fixed_mem32 %#x-%#x write_protect %#x\n",
		fixed_memory32->address,
		fixed_memory32->address + fixed_memory32->address_length - 1,
1052
		fixed_memory32->write_protect);
L
Linus Torvalds 已提交
1053 1054
}

1055
int pnpacpi_encode_resources(struct pnp_dev *dev, struct acpi_buffer *buffer)
L
Linus Torvalds 已提交
1056 1057 1058
{
	int i = 0;
	/* pnpacpi_build_resource_template allocates extra mem */
B
Bjorn Helgaas 已提交
1059
	int res_cnt = (buffer->length - 1) / sizeof(struct acpi_resource) - 1;
1060
	struct acpi_resource *resource = buffer->pointer;
L
Linus Torvalds 已提交
1061 1062
	int port = 0, irq = 0, dma = 0, mem = 0;

1063
	dev_dbg(&dev->dev, "encode %d resources\n", res_cnt);
L
Linus Torvalds 已提交
1064
	while (i < res_cnt) {
B
Bjorn Helgaas 已提交
1065
		switch (resource->type) {
B
Bob Moore 已提交
1066
		case ACPI_RESOURCE_TYPE_IRQ:
1067
			pnpacpi_encode_irq(dev, resource,
1068
			       pnp_get_resource(dev, IORESOURCE_IRQ, irq));
L
Linus Torvalds 已提交
1069 1070 1071
			irq++;
			break;

B
Bob Moore 已提交
1072
		case ACPI_RESOURCE_TYPE_DMA:
1073
			pnpacpi_encode_dma(dev, resource,
1074
				pnp_get_resource(dev, IORESOURCE_DMA, dma));
B
Bjorn Helgaas 已提交
1075
			dma++;
L
Linus Torvalds 已提交
1076
			break;
B
Bob Moore 已提交
1077
		case ACPI_RESOURCE_TYPE_IO:
1078
			pnpacpi_encode_io(dev, resource,
1079
				pnp_get_resource(dev, IORESOURCE_IO, port));
B
Bjorn Helgaas 已提交
1080
			port++;
L
Linus Torvalds 已提交
1081
			break;
B
Bob Moore 已提交
1082
		case ACPI_RESOURCE_TYPE_FIXED_IO:
1083
			pnpacpi_encode_fixed_io(dev, resource,
1084
				pnp_get_resource(dev, IORESOURCE_IO, port));
B
Bjorn Helgaas 已提交
1085
			port++;
L
Linus Torvalds 已提交
1086
			break;
B
Bob Moore 已提交
1087
		case ACPI_RESOURCE_TYPE_MEMORY24:
1088
			pnpacpi_encode_mem24(dev, resource,
1089
				pnp_get_resource(dev, IORESOURCE_MEM, mem));
B
Bjorn Helgaas 已提交
1090
			mem++;
L
Linus Torvalds 已提交
1091
			break;
B
Bob Moore 已提交
1092
		case ACPI_RESOURCE_TYPE_MEMORY32:
1093
			pnpacpi_encode_mem32(dev, resource,
1094
				pnp_get_resource(dev, IORESOURCE_MEM, mem));
B
Bjorn Helgaas 已提交
1095
			mem++;
L
Linus Torvalds 已提交
1096
			break;
B
Bob Moore 已提交
1097
		case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
1098
			pnpacpi_encode_fixed_mem32(dev, resource,
1099
				pnp_get_resource(dev, IORESOURCE_MEM, mem));
B
Bjorn Helgaas 已提交
1100
			mem++;
L
Linus Torvalds 已提交
1101
			break;
1102
		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1103
			pnpacpi_encode_ext_irq(dev, resource,
1104
				pnp_get_resource(dev, IORESOURCE_IRQ, irq));
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
			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 已提交
1116
		default:	/* other type */
1117 1118
			dev_warn(&dev->dev, "can't encode unknown resource "
				 "type %d\n", resource->type);
L
Linus Torvalds 已提交
1119 1120
			return -EINVAL;
		}
B
Bjorn Helgaas 已提交
1121 1122
		resource++;
		i++;
L
Linus Torvalds 已提交
1123 1124 1125
	}
	return 0;
}