rsmisc.c 14.1 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 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 35 36 37 38 39 40 41 42 43 44 45 46 47
/*******************************************************************************
 *
 * Module Name: rsmisc - Miscellaneous resource descriptors
 *
 ******************************************************************************/

/*
 * Copyright (C) 2000 - 2005, R. Byron Moore
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
#include <acpi/acresrc.h>

#define _COMPONENT          ACPI_RESOURCES
Len Brown's avatar
Len Brown committed
48
ACPI_MODULE_NAME("rsmisc")
Linus Torvalds's avatar
Linus Torvalds committed
49

Bob Moore's avatar
Bob Moore committed
50 51 52 53 54 55
#define INIT_RESOURCE_TYPE(i)       i->resource_offset
#define INIT_RESOURCE_LENGTH(i)     i->aml_offset
#define INIT_TABLE_LENGTH(i)        i->value
#define COMPARE_OPCODE(i)           i->resource_offset
#define COMPARE_TARGET(i)           i->aml_offset
#define COMPARE_VALUE(i)            i->value
Robert Moore's avatar
Robert Moore committed
56 57
/*******************************************************************************
 *
Bob Moore's avatar
Bob Moore committed
58
 * FUNCTION:    acpi_rs_convert_aml_to_resource
Robert Moore's avatar
Robert Moore committed
59
 *
Bob Moore's avatar
Bob Moore committed
60 61
 * PARAMETERS:  Resource            - Pointer to the resource descriptor
 *              Aml                 - Where the AML descriptor is returned
Bob Moore's avatar
Bob Moore committed
62
 *              Info                - Pointer to appropriate conversion table
Robert Moore's avatar
Robert Moore committed
63 64 65
 *
 * RETURN:      Status
 *
Bob Moore's avatar
Bob Moore committed
66 67
 * DESCRIPTION: Convert an external AML resource descriptor to the corresponding
 *              internal resource descriptor
Linus Torvalds's avatar
Linus Torvalds committed
68 69 70
 *
 ******************************************************************************/
acpi_status
Bob Moore's avatar
Bob Moore committed
71 72 73
acpi_rs_convert_aml_to_resource(struct acpi_resource *resource,
				union aml_resource *aml,
				struct acpi_rsconvert_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
74
{
Bob Moore's avatar
Bob Moore committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	acpi_rs_length aml_resource_length;
	void *source;
	void *destination;
	char *target;
	u8 count;
	u8 flags_mode = FALSE;
	u16 item_count = 0;
	u16 temp16 = 0;

	ACPI_FUNCTION_TRACE("rs_get_resource");

	if (((acpi_native_uint) resource) & 0x3) {
		acpi_os_printf
		    ("**** GET: Misaligned resource pointer: %p Type %2.2X Len %X\n",
		     resource, resource->type, resource->length);
	}
91

Bob Moore's avatar
Bob Moore committed
92
	/* Extract the resource Length field (does not include header length) */
Linus Torvalds's avatar
Linus Torvalds committed
93

Bob Moore's avatar
Bob Moore committed
94
	aml_resource_length = acpi_ut_get_resource_length(aml);
95

Bob Moore's avatar
Bob Moore committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 178 179 180 181 182 183 184 185 186 187 188 189 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	/*
	 * First table entry must be ACPI_RSC_INITxxx and must contain the
	 * table length (# of table entries)
	 */
	count = INIT_TABLE_LENGTH(info);

	while (count) {
		/*
		 * Source is the external AML byte stream buffer,
		 * destination is the internal resource descriptor
		 */
		source = ((u8 *) aml) + info->aml_offset;
		destination = ((u8 *) resource) + info->resource_offset;

		switch (info->opcode) {
		case ACPI_RSC_INITGET:
			/*
			 * Get the resource type and the initial (minimum) length
			 */
			ACPI_MEMSET(resource, 0, INIT_RESOURCE_LENGTH(info));
			resource->type = INIT_RESOURCE_TYPE(info);
			resource->length = INIT_RESOURCE_LENGTH(info);
			break;

		case ACPI_RSC_INITSET:
			break;

		case ACPI_RSC_FLAGINIT:

			flags_mode = TRUE;
			break;

		case ACPI_RSC_1BITFLAG:
			/*
			 * Mask and shift the flag bit
			 */
			*((u8 *) destination) = (u8)
			    ((*((u8 *) source) >> info->value) & 0x01);
			break;

		case ACPI_RSC_2BITFLAG:
			/*
			 * Mask and shift the flag bits
			 */
			*((u8 *) destination) = (u8)
			    ((*((u8 *) source) >> info->value) & 0x03);
			break;

		case ACPI_RSC_COUNT:

			item_count = *((u8 *) source);
			*((u8 *) destination) = (u8) item_count;

			resource->length = resource->length +
			    (info->value * (item_count - 1));
			break;

		case ACPI_RSC_COUNT16:

			item_count = aml_resource_length;
			*((u16 *) destination) = item_count;

			resource->length = resource->length +
			    (info->value * (item_count - 1));
			break;

		case ACPI_RSC_LENGTH:

			resource->length = resource->length + info->value;
			break;

		case ACPI_RSC_MOVE8:
		case ACPI_RSC_MOVE16:
		case ACPI_RSC_MOVE32:
		case ACPI_RSC_MOVE64:
			/*
			 * Raw data move. Use the Info value field unless item_count has
			 * been previously initialized via a COUNT opcode
			 */
			if (info->value) {
				item_count = info->value;
			}
			acpi_rs_move_data(destination, source, item_count,
					  info->opcode);
			break;

		case ACPI_RSC_SET8:

			ACPI_MEMSET(destination, info->aml_offset, info->value);
			break;

		case ACPI_RSC_DATA8:

			target = ((char *)resource) + info->value;
			ACPI_MEMCPY(destination, source,
				    *(ACPI_CAST_PTR(u16, target)));
			break;

		case ACPI_RSC_ADDRESS:
			/*
			 * Common handler for address descriptor flags
			 */
			if (!acpi_rs_get_address_common(resource, aml)) {
				return_ACPI_STATUS
				    (AE_AML_INVALID_RESOURCE_TYPE);
			}
			break;

		case ACPI_RSC_SOURCE:
			/*
			 * Optional resource_source (Index and String)
			 */
			resource->length +=
			    acpi_rs_get_resource_source(aml_resource_length,
							info->value,
							destination, aml, NULL);
			break;

		case ACPI_RSC_SOURCEX:
			/*
			 * Optional resource_source (Index and String). This is the more
			 * complicated case used by the Interrupt() macro
			 */
			target =
			    ((char *)resource) + info->aml_offset +
			    (item_count * 4);

			resource->length +=
			    acpi_rs_get_resource_source(aml_resource_length,
							(acpi_rs_length) (((item_count - 1) * sizeof(u32)) + info->value), destination, aml, target);
			break;

		case ACPI_RSC_BITMASK:
			/*
			 * 8-bit encoded bitmask (DMA macro)
			 */
			item_count =
			    acpi_rs_decode_bitmask(*((u8 *) source),
						   destination);
			if (item_count) {
				resource->length +=
				    resource->length + (item_count - 1);
			}

			target = ((char *)resource) + info->value;
			*((u8 *) target) = (u8) item_count;
			break;

		case ACPI_RSC_BITMASK16:
			/*
			 * 16-bit encoded bitmask (IRQ macro)
			 */
			ACPI_MOVE_16_TO_16(&temp16, source);

			item_count =
			    acpi_rs_decode_bitmask(temp16, destination);
			if (item_count) {
				resource->length =
				    resource->length + (item_count - 1);
			}

			target = ((char *)resource) + info->value;
			*((u8 *) target) = (u8) item_count;
			break;

		case ACPI_RSC_EXIT_NE:
			/*
			 * Control - Exit conversion if not equal
			 */
			switch (info->resource_offset) {
			case ACPI_RSC_COMPARE_AML_LENGTH:
				if (aml_resource_length != info->value) {
					goto exit;
				}
				break;

			case ACPI_RSC_COMPARE_VALUE:
				if (*((u8 *) source) != info->value) {
					goto exit;
				}
				break;

			default:
				acpi_os_printf
				    ("*** Invalid conversion sub-opcode\n");
				return_ACPI_STATUS(AE_BAD_PARAMETER);
			}
			break;

		default:

			acpi_os_printf("*** Invalid conversion opcode\n");
			return_ACPI_STATUS(AE_BAD_PARAMETER);
		}
Linus Torvalds's avatar
Linus Torvalds committed
290

Bob Moore's avatar
Bob Moore committed
291 292
		count--;
		info++;
Bob Moore's avatar
Bob Moore committed
293
	}
294

Bob Moore's avatar
Bob Moore committed
295 296 297
      exit:
	if (!flags_mode) {
		/* Round the resource struct length up to the next 32-bit boundary */
298

Bob Moore's avatar
Bob Moore committed
299 300
		resource->length = ACPI_ROUND_UP_to_32_bITS(resource->length);
	}
Len Brown's avatar
Len Brown committed
301
	return_ACPI_STATUS(AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304 305
}

/*******************************************************************************
 *
Bob Moore's avatar
Bob Moore committed
306
 * FUNCTION:    acpi_rs_convert_resource_to_aml
Linus Torvalds's avatar
Linus Torvalds committed
307
 *
Bob Moore's avatar
Bob Moore committed
308 309
 * PARAMETERS:  Resource            - Pointer to the resource descriptor
 *              Aml                 - Where the AML descriptor is returned
Bob Moore's avatar
Bob Moore committed
310
 *              Info                - Pointer to appropriate conversion table
Linus Torvalds's avatar
Linus Torvalds committed
311 312 313
 *
 * RETURN:      Status
 *
Bob Moore's avatar
Bob Moore committed
314 315
 * DESCRIPTION: Convert an internal resource descriptor to the corresponding
 *              external AML resource descriptor.
Linus Torvalds's avatar
Linus Torvalds committed
316 317 318 319
 *
 ******************************************************************************/

acpi_status
Bob Moore's avatar
Bob Moore committed
320 321 322
acpi_rs_convert_resource_to_aml(struct acpi_resource *resource,
				union aml_resource *aml,
				struct acpi_rsconvert_info *info)
Linus Torvalds's avatar
Linus Torvalds committed
323
{
Bob Moore's avatar
Bob Moore committed
324 325 326 327 328 329
	void *source = NULL;
	void *destination;
	acpi_rsdesc_size aml_length = 0;
	u8 count;
	u16 temp16 = 0;
	u16 item_count = 0;
Linus Torvalds's avatar
Linus Torvalds committed
330

Bob Moore's avatar
Bob Moore committed
331
	ACPI_FUNCTION_TRACE("rs_convert_resource_to_aml");
Bob Moore's avatar
Bob Moore committed
332

Bob Moore's avatar
Bob Moore committed
333
	/* Validate the Resource pointer, must be 32-bit aligned */
Bob Moore's avatar
Bob Moore committed
334

Bob Moore's avatar
Bob Moore committed
335 336 337 338
	if (((acpi_native_uint) resource) & 0x3) {
		acpi_os_printf
		    ("**** SET: Misaligned resource pointer: %p Type %2.2X Len %X\n",
		     resource, resource->type, resource->length);
Bob Moore's avatar
Bob Moore committed
339 340
	}

Bob Moore's avatar
Bob Moore committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	/*
	 * First table entry must be ACPI_RSC_INITxxx and must contain the
	 * table length (# of table entries)
	 */
	count = INIT_TABLE_LENGTH(info);

	while (count) {
		/*
		 * Source is the internal resource descriptor,
		 * destination is the external AML byte stream buffer
		 */
		source = ((u8 *) resource) + info->resource_offset;
		destination = ((u8 *) aml) + info->aml_offset;

		switch (info->opcode) {
		case ACPI_RSC_INITSET:

			ACPI_MEMSET(aml, 0, INIT_RESOURCE_LENGTH(info));
			aml_length = INIT_RESOURCE_LENGTH(info);
			acpi_rs_set_resource_header(INIT_RESOURCE_TYPE(info),
						    aml_length, aml);
			break;

		case ACPI_RSC_INITGET:
			break;

		case ACPI_RSC_FLAGINIT:
			/*
			 * Clear the flag byte
			 */
			*((u8 *) destination) = 0;
			break;

		case ACPI_RSC_1BITFLAG:
			/*
			 * Mask and shift the flag bit
			 */
			*((u8 *) destination) |= (u8)
			    ((*((u8 *) source) & 0x01) << info->value);
			break;

		case ACPI_RSC_2BITFLAG:
			/*
			 * Mask and shift the flag bits
			 */
			*((u8 *) destination) |= (u8)
			    ((*((u8 *) source) & 0x03) << info->value);
			break;

		case ACPI_RSC_COUNT:

			item_count = *((u8 *) source);
			*((u8 *) destination) = (u8) item_count;

			aml_length = (u16) (aml_length +
					    (info->value * (item_count - 1)));
			break;

		case ACPI_RSC_COUNT16:

			item_count = *((u16 *) source);
			aml_length = (u16) (aml_length + item_count);
			acpi_rs_set_resource_length(aml_length, aml);
			break;

		case ACPI_RSC_LENGTH:

			acpi_rs_set_resource_length(info->value, aml);
			break;

		case ACPI_RSC_MOVE8:
		case ACPI_RSC_MOVE16:
		case ACPI_RSC_MOVE32:
		case ACPI_RSC_MOVE64:

			if (info->value) {
				item_count = info->value;
			}
			acpi_rs_move_data(destination, source, item_count,
					  info->opcode);
			break;

		case ACPI_RSC_ADDRESS:

			/* Set the Resource Type, General Flags, and Type-Specific Flags */

			acpi_rs_set_address_common(aml, resource);
			break;

		case ACPI_RSC_SOURCEX:
			/*
			 * Optional resource_source (Index and String)
			 */
			aml_length =
			    acpi_rs_set_resource_source(aml,
							(acpi_rs_length)
							aml_length, source);
			acpi_rs_set_resource_length(aml_length, aml);
			break;

		case ACPI_RSC_SOURCE:
			/*
			 * Optional resource_source (Index and String). This is the more
			 * complicated case used by the Interrupt() macro
			 */
			aml_length =
			    acpi_rs_set_resource_source(aml, info->value,
							source);
			acpi_rs_set_resource_length(aml_length, aml);
			break;

		case ACPI_RSC_BITMASK:
			/*
			 * 8-bit encoded bitmask (DMA macro)
			 */
			*((u8 *) destination) = (u8)
			    acpi_rs_encode_bitmask(source,
						   *(((u8 *) resource) +
						     info->value));
			break;

		case ACPI_RSC_BITMASK16:
			/*
			 * 16-bit encoded bitmask (IRQ macro)
			 */
			temp16 =
			    acpi_rs_encode_bitmask(source,
						   *(((u8 *) resource) +
						     info->value));
			ACPI_MOVE_16_TO_16(destination, &temp16);
			break;

		case ACPI_RSC_EXIT_LE:
			/*
			 * Control - Exit conversion if less than or equal
			 */
			if (item_count <= info->value) {
				goto exit;
			}
			break;

		case ACPI_RSC_EXIT_NE:
			/*
			 * Control - Exit conversion if not equal
			 */
			switch (COMPARE_OPCODE(info)) {
			case ACPI_RSC_COMPARE_VALUE:
				if (*
				    ((u8 *) (((u8 *) resource) +
					     COMPARE_TARGET(info))) !=
				    COMPARE_VALUE(info)) {
					goto exit;
				}
				break;

			default:
				acpi_os_printf
				    ("*** Invalid conversion sub-opcode\n");
				return_ACPI_STATUS(AE_BAD_PARAMETER);
			}
			break;

		default:

			acpi_os_printf("*** Invalid conversion opcode\n");
			return_ACPI_STATUS(AE_BAD_PARAMETER);
Bob Moore's avatar
Bob Moore committed
507
		}
Linus Torvalds's avatar
Linus Torvalds committed
508

Bob Moore's avatar
Bob Moore committed
509 510
		count--;
		info++;
Linus Torvalds's avatar
Linus Torvalds committed
511 512
	}

Bob Moore's avatar
Bob Moore committed
513
      exit:
Len Brown's avatar
Len Brown committed
514
	return_ACPI_STATUS(AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
515 516
}

Bob Moore's avatar
Bob Moore committed
517 518
#if 0
/* Previous resource validations */
519

Bob Moore's avatar
Bob Moore committed
520 521
if (aml->ext_address64.revision_iD != AML_RESOURCE_EXTENDED_ADDRESS_REVISION) {
	return_ACPI_STATUS(AE_SUPPORT);
Linus Torvalds's avatar
Linus Torvalds committed
522 523
}

Bob Moore's avatar
Bob Moore committed
524 525
if (resource->data.start_dpf.performance_robustness >= 3) {
	return_ACPI_STATUS(AE_AML_BAD_RESOURCE_VALUE);
Linus Torvalds's avatar
Linus Torvalds committed
526 527
}

Bob Moore's avatar
Bob Moore committed
528 529 530 531 532 533 534 535 536 537
if (((aml->irq.flags & 0x09) == 0x00) || ((aml->irq.flags & 0x09) == 0x09)) {
	/*
	 * Only [active_high, edge_sensitive] or [active_low, level_sensitive]
	 * polarity/trigger interrupts are allowed (ACPI spec, section
	 * "IRQ Format"), so 0x00 and 0x09 are illegal.
	 */
	ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
			  "Invalid interrupt polarity/trigger in resource list, %X\n",
			  aml->irq.flags));
	return_ACPI_STATUS(AE_BAD_DATA);
Linus Torvalds's avatar
Linus Torvalds committed
538 539
}

Bob Moore's avatar
Bob Moore committed
540 541 542
resource->data.extended_irq.interrupt_count = temp8;
if (temp8 < 1) {
	/* Must have at least one IRQ */
543

Bob Moore's avatar
Bob Moore committed
544
	return_ACPI_STATUS(AE_AML_BAD_RESOURCE_LENGTH);
Linus Torvalds's avatar
Linus Torvalds committed
545 546
}

Bob Moore's avatar
Bob Moore committed
547 548 549 550
if (resource->data.dma.transfer == 0x03) {
	ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
			  "Invalid DMA.Transfer preference (3)\n"));
	return_ACPI_STATUS(AE_BAD_DATA);
Linus Torvalds's avatar
Linus Torvalds committed
551
}
Bob Moore's avatar
Bob Moore committed
552
#endif