amfldio.c 17 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/******************************************************************************
 *
 * Module Name: amfldio - Aml Field I/O
Linus Torvalds's avatar
Linus Torvalds committed
4
 *              $Revision: 37 $
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8
 *
 *****************************************************************************/

/*
Linus Torvalds's avatar
Linus Torvalds committed
9
 *  Copyright (C) 2000, 2001 R. Byron Moore
Linus Torvalds's avatar
Linus Torvalds committed
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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
 *
 *  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 of the License, 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 "acpi.h"
#include "acinterp.h"
#include "amlcode.h"
#include "acnamesp.h"
#include "achware.h"
#include "acevents.h"


#define _COMPONENT          INTERPRETER
	 MODULE_NAME         ("amfldio")


/*******************************************************************************
 *
 * FUNCTION:    Acpi_aml_read_field_data
 *
 * PARAMETERS:  *Obj_desc           - Field to be read
 *              *Value              - Where to store value
 *              Field_bit_width     - Field Width in bits (8, 16, or 32)
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Retrieve the value of the given field
 *
 ******************************************************************************/

ACPI_STATUS
acpi_aml_read_field_data (
	ACPI_OPERAND_OBJECT     *obj_desc,
	u32                     field_byte_offset,
	u32                     field_bit_width,
	u32                     *value)
{
	ACPI_STATUS             status;
	ACPI_OPERAND_OBJECT     *rgn_desc = NULL;
	ACPI_PHYSICAL_ADDRESS   address;
	u32                     local_value = 0;
	u32                     field_byte_width;


	/* Obj_desc is validated by callers */

	if (obj_desc) {
		rgn_desc = obj_desc->field.container;
	}


	field_byte_width = DIV_8 (field_bit_width);
	status = acpi_aml_setup_field (obj_desc, rgn_desc, field_bit_width);
	if (ACPI_FAILURE (status)) {
		return (status);
	}

	/* Setup_field validated Rgn_desc and Field_bit_width */

	if (!value) {
		value = &local_value;   /*  support reads without saving value  */
	}


	/*
	 * Set offset to next multiple of field width,
	 *  add region base address and offset within the field
	 */
	address = rgn_desc->region.address +
			  (obj_desc->field.offset * field_byte_width) +
			  field_byte_offset;


	/* Invoke the appropriate Address_space/Op_region handler */

	status = acpi_ev_address_space_dispatch (rgn_desc, ADDRESS_SPACE_READ,
			 address, field_bit_width, value);



	return (status);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_aml_read_field
 *
 * PARAMETERS:  *Obj_desc           - Field to be read
 *              *Value              - Where to store value
 *              Field_bit_width     - Field Width in bits (8, 16, or 32)
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Retrieve the value of the given field
 *
 ******************************************************************************/

ACPI_STATUS
acpi_aml_read_field (
	ACPI_OPERAND_OBJECT     *obj_desc,
	void                    *buffer,
	u32                     buffer_length,
	u32                     byte_length,
	u32                     datum_length,
	u32                     bit_granularity,
	u32                     byte_granularity)
{
	ACPI_STATUS             status;
	u32                     this_field_byte_offset;
	u32                     this_field_datum_offset;
	u32                     previous_raw_datum;
Linus Torvalds's avatar
Linus Torvalds committed
135
	u32                     this_raw_datum = 0;
Linus Torvalds's avatar
Linus Torvalds committed
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
	u32                     valid_field_bits;
	u32                     mask;
	u32                     merged_datum = 0;


	/*
	 * Clear the caller's buffer (the whole buffer length as given)
	 * This is very important, especially in the cases where a byte is read,
	 * but the buffer is really a u32 (4 bytes).
	 */

	MEMSET (buffer, 0, buffer_length);

	/* Read the first raw datum to prime the loop */

	this_field_byte_offset = 0;
	this_field_datum_offset= 0;

	status = acpi_aml_read_field_data (obj_desc, this_field_byte_offset, bit_granularity,
			   &previous_raw_datum);
	if (ACPI_FAILURE (status)) {
		goto cleanup;
	}

	/* We might actually be done if the request fits in one datum */

	if ((datum_length == 1) &&
		((obj_desc->field.bit_offset + obj_desc->field_unit.length) <=
			(u16) bit_granularity))
	{
		merged_datum = previous_raw_datum;

		merged_datum = (merged_datum >> obj_desc->field.bit_offset);

		valid_field_bits = obj_desc->field_unit.length % bit_granularity;
		if (valid_field_bits) {
			mask = (((u32) 1 << valid_field_bits) - (u32) 1);
			merged_datum &= mask;
		}


		/*
		 * Place the Merged_datum into the proper format and return buffer
		 * field
		 */

		switch (byte_granularity)
		{
		case 1:
			((u8 *) buffer) [this_field_datum_offset] = (u8) merged_datum;
			break;

		case 2:
			MOVE_UNALIGNED16_TO_16 (&(((u16 *) buffer)[this_field_datum_offset]), &merged_datum);
			break;

		case 4:
			MOVE_UNALIGNED32_TO_32 (&(((u32 *) buffer)[this_field_datum_offset]), &merged_datum);
			break;
		}

		this_field_byte_offset = 1;
		this_field_datum_offset = 1;
	}

	else {
		/* We need to get more raw data to complete one or more field data */

		while (this_field_datum_offset < datum_length) {
			/*
Linus Torvalds's avatar
Linus Torvalds committed
206 207 208 209 210 211
			 * If the field is aligned on a byte boundary, we don't want
			 * to perform a final read, since this would potentially read
			 * past the end of the region.
			 *
			 * TBD: [Investigate] It may make more sense to just split the aligned
			 * and non-aligned cases since the aligned case is so very simple,
Linus Torvalds's avatar
Linus Torvalds committed
212
			 */
Linus Torvalds's avatar
Linus Torvalds committed
213 214 215
			if ((obj_desc->field.bit_offset != 0) ||
				((obj_desc->field.bit_offset == 0) &&
				(this_field_datum_offset < (datum_length -1))))
Linus Torvalds's avatar
Linus Torvalds committed
216
			{
Linus Torvalds's avatar
Linus Torvalds committed
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
				/*
				 * Get the next raw datum, it contains some or all bits
				 * of the current field datum
				 */

				status = acpi_aml_read_field_data (obj_desc,
						  this_field_byte_offset + byte_granularity,
						  bit_granularity, &this_raw_datum);
				if (ACPI_FAILURE (status)) {
					goto cleanup;
				}

				/* Before merging the data, make sure the unused bits are clear */

				switch (byte_granularity)
				{
				case 1:
					this_raw_datum &= 0x000000FF;
					previous_raw_datum &= 0x000000FF;
					break;

				case 2:
					this_raw_datum &= 0x0000FFFF;
					previous_raw_datum &= 0x0000FFFF;
					break;
				}
Linus Torvalds's avatar
Linus Torvalds committed
243 244
			}

Linus Torvalds's avatar
Linus Torvalds committed
245

Linus Torvalds's avatar
Linus Torvalds committed
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 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
			/*
			 * Put together bits of the two raw data to make a complete
			 * field datum
			 */


			if (obj_desc->field.bit_offset != 0) {
				merged_datum =
					(previous_raw_datum >> obj_desc->field.bit_offset) |
					(this_raw_datum << (bit_granularity - obj_desc->field.bit_offset));
			}

			else {
				merged_datum = previous_raw_datum;
			}

			/*
			 * Prepare the merged datum for storing into the caller's
			 *  buffer.  It is possible to have a 32-bit buffer
			 *  (Byte_granularity == 4), but a Obj_desc->Field.Length
			 *  of 8 or 16, meaning that the upper bytes of merged data
			 *  are undesired.  This section fixes that.
			 */
			switch (obj_desc->field.length)
			{
			case 8:
				merged_datum &= 0x000000FF;
				break;

			case 16:
				merged_datum &= 0x0000FFFF;
				break;
			}

			/*
			 * Now store the datum in the caller's buffer, according to
			 * the data type
			 */
			switch (byte_granularity)
			{
			case 1:
				((u8 *) buffer) [this_field_datum_offset] = (u8) merged_datum;
				break;

			case 2:
				MOVE_UNALIGNED16_TO_16 (&(((u16 *) buffer) [this_field_datum_offset]), &merged_datum);
				break;

			case 4:
				MOVE_UNALIGNED32_TO_32 (&(((u32 *) buffer) [this_field_datum_offset]), &merged_datum);
				break;
			}

			/*
			 * Save the most recent datum since it contains bits of
			 * the *next* field datum
			 */

			previous_raw_datum = this_raw_datum;

			this_field_byte_offset += byte_granularity;
			this_field_datum_offset++;

		}  /* while */
	}

cleanup:

	return (status);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_aml_write_field_data
 *
 * PARAMETERS:  *Obj_desc           - Field to be set
 *              Value               - Value to store
 *              Field_bit_width     - Field Width in bits (8, 16, or 32)
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Store the value into the given field
 *
 ******************************************************************************/

static ACPI_STATUS
acpi_aml_write_field_data (
	ACPI_OPERAND_OBJECT     *obj_desc,
	u32                     field_byte_offset,
	u32                     field_bit_width,
	u32                     value)
{
	ACPI_STATUS             status = AE_OK;
	ACPI_OPERAND_OBJECT     *rgn_desc = NULL;
	ACPI_PHYSICAL_ADDRESS   address;
	u32                     field_byte_width;


	/* Obj_desc is validated by callers */

	if (obj_desc) {
		rgn_desc = obj_desc->field.container;
	}

	field_byte_width = DIV_8 (field_bit_width);
	status = acpi_aml_setup_field (obj_desc, rgn_desc, field_bit_width);
	if (ACPI_FAILURE (status)) {
		return (status);
	}


	/*
	 * Set offset to next multiple of field width,
	 *  add region base address and offset within the field
	 */
	address = rgn_desc->region.address +
			  (obj_desc->field.offset * field_byte_width) +
			  field_byte_offset;

	/* Invoke the appropriate Address_space/Op_region handler */

	status = acpi_ev_address_space_dispatch (rgn_desc, ADDRESS_SPACE_WRITE,
			 address, field_bit_width, &value);



	return (status);
}


/*****************************************************************************
 *
 * FUNCTION:    Acpi_aml_write_field_data_with_update_rule
 *
 * PARAMETERS:  *Obj_desc           - Field to be set
 *              Value               - Value to store
 *              Field_bit_width     - Field Width in bits (8, 16, or 32)
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Apply the field update rule to a field write
 *
 ****************************************************************************/

static ACPI_STATUS
acpi_aml_write_field_data_with_update_rule (
	ACPI_OPERAND_OBJECT     *obj_desc,
	u32                     mask,
	u32                     field_value,
	u32                     this_field_byte_offset,
	u32                     bit_granularity)
{
	ACPI_STATUS             status = AE_OK;
	u32                     merged_value;
	u32                     current_value;


	/* Start with the new bits  */

	merged_value = field_value;


	/* Decode the update rule */

	switch (obj_desc->field.update_rule)
	{

	case UPDATE_PRESERVE:

Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419 420 421 422 423 424 425 426
		/* Check if update rule needs to be applied (not if mask is all ones) */

		if (((1 << bit_granularity) -1) & ~mask) {
			/*
			 * Read the current contents of the byte/word/dword containing
			 * the field, and merge with the new field value.
			 */
			status = acpi_aml_read_field_data (obj_desc, this_field_byte_offset,
					   bit_granularity, &current_value);
			merged_value |= (current_value & ~mask);
		}
Linus Torvalds's avatar
Linus Torvalds committed
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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
		break;


	case UPDATE_WRITE_AS_ONES:

		/* Set positions outside the field to all ones */

		merged_value |= ~mask;
		break;


	case UPDATE_WRITE_AS_ZEROS:

		/* Set positions outside the field to all zeros */

		merged_value &= mask;
		break;


	default:
		status = AE_AML_OPERAND_VALUE;
	}


	/* Write the merged value */

	if (ACPI_SUCCESS (status)) {
		status = acpi_aml_write_field_data (obj_desc, this_field_byte_offset,
				   bit_granularity, merged_value);
	}

	return (status);
}


/*****************************************************************************
 *
 * FUNCTION:    Acpi_aml_write_field
 *
 * PARAMETERS:  *Obj_desc           - Field to be set
 *              Value               - Value to store
 *              Field_bit_width     - Field Width in bits (8, 16, or 32)
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Store the value into the given field
 *
 ****************************************************************************/

ACPI_STATUS
acpi_aml_write_field (
	ACPI_OPERAND_OBJECT     *obj_desc,
	void                    *buffer,
	u32                     buffer_length,
	u32                     byte_length,
	u32                     datum_length,
	u32                     bit_granularity,
	u32                     byte_granularity)
{
	ACPI_STATUS             status;
	u32                     this_field_byte_offset;
	u32                     this_field_datum_offset;
	u32                     mask;
	u32                     merged_datum;
	u32                     previous_raw_datum;
	u32                     this_raw_datum;
	u32                     field_value;
	u32                     valid_field_bits;


	/*
	 * Break the request into up to three parts:
	 * non-aligned part at start, aligned part in middle, non-aligned part
	 * at end --- Just like an I/O request ---
	 */

	this_field_byte_offset = 0;
	this_field_datum_offset= 0;

	/* Get a datum */

	switch (byte_granularity)
	{
	case 1:
		previous_raw_datum = ((u8 *) buffer) [this_field_datum_offset];
		break;

	case 2:
		MOVE_UNALIGNED16_TO_32 (&previous_raw_datum, &(((u16 *) buffer) [this_field_datum_offset]));
		break;

	case 4:
		MOVE_UNALIGNED32_TO_32 (&previous_raw_datum, &(((u32 *) buffer) [this_field_datum_offset]));
		break;

	default:
		status = AE_AML_OPERAND_VALUE;
		goto cleanup;
	}


	/*
	 * Write a partial field datum if field does not begin on a datum boundary
	 *
	 * Construct Mask with 1 bits where the field is, 0 bits elsewhere
	 *
	 * 1) Bits above the field
	 */

	mask = (((u32)(-1)) << (u32)obj_desc->field.bit_offset);

	/* 2) Only the bottom 5 bits are valid for a shift operation. */

	if ((obj_desc->field.bit_offset + obj_desc->field_unit.length) < 32) {
		/* Bits above the field */

		mask &= (~(((u32)(-1)) << ((u32)obj_desc->field.bit_offset +
				   (u32)obj_desc->field_unit.length)));
	}

	/* 3) Shift and mask the value into the field position */

	field_value = (previous_raw_datum << obj_desc->field.bit_offset) & mask;

	status = acpi_aml_write_field_data_with_update_rule (obj_desc, mask, field_value,
			 this_field_byte_offset,
			 bit_granularity);
	if (ACPI_FAILURE (status)) {
		goto cleanup;
	}


	/* If the field fits within one datum, we are done. */

	if ((datum_length == 1) &&
	   ((obj_desc->field.bit_offset + obj_desc->field_unit.length) <=
			(u16) bit_granularity))
	{
		goto cleanup;
	}

	/*
	 * We don't need to worry about the update rule for these data, because
	 * all of the bits are part of the field.
	 *
	 * Can't write the last datum, however, because it might contain bits that
	 * are not part of the field -- the update rule must be applied.
	 */

	while (this_field_datum_offset < (datum_length - 1)) {
		this_field_datum_offset++;

		/* Get the next raw datum, it contains bits of the current field datum... */

		switch (byte_granularity)
		{
		case 1:
			this_raw_datum = ((u8 *) buffer) [this_field_datum_offset];
			break;

		case 2:
			MOVE_UNALIGNED16_TO_32 (&this_raw_datum, &(((u16 *) buffer) [this_field_datum_offset]));
			break;

		case 4:
			MOVE_UNALIGNED32_TO_32 (&this_raw_datum, &(((u32 *) buffer) [this_field_datum_offset]));
			break;

		default:
			status = AE_AML_OPERAND_VALUE;
			goto cleanup;
		}

		/*
		 * Put together bits of the two raw data to make a complete field
		 * datum
		 */

		if (obj_desc->field.bit_offset != 0) {
			merged_datum =
				(previous_raw_datum >> (bit_granularity - obj_desc->field.bit_offset)) |
				(this_raw_datum << obj_desc->field.bit_offset);
		}

		else {
			merged_datum = this_raw_datum;
		}

		/* Now write the completed datum  */


		status = acpi_aml_write_field_data (obj_desc,
				   this_field_byte_offset + byte_granularity,
				   bit_granularity, merged_datum);
		if (ACPI_FAILURE (status)) {
			goto cleanup;
		}


		/*
		 * Save the most recent datum since it contains bits of
		 * the *next* field datum
		 */

		previous_raw_datum = this_raw_datum;

		this_field_byte_offset += byte_granularity;

	}  /* while */


	/* Write a partial field datum if field does not end on a datum boundary */

	if ((obj_desc->field_unit.length + obj_desc->field_unit.bit_offset) %
		bit_granularity)
	{
		switch (byte_granularity)
		{
		case 1:
			this_raw_datum = ((u8 *) buffer) [this_field_datum_offset];
			break;

		case 2:
			MOVE_UNALIGNED16_TO_32 (&this_raw_datum, &(((u16 *) buffer) [this_field_datum_offset]));
			break;

		case 4:
			MOVE_UNALIGNED32_TO_32 (&this_raw_datum, &(((u32 *) buffer) [this_field_datum_offset]));
			break;
		}

		/* Construct Mask with 1 bits where the field is, 0 bits elsewhere */

		valid_field_bits = ((obj_desc->field_unit.length % bit_granularity) +
				   obj_desc->field.bit_offset);

		mask = (((u32) 1 << valid_field_bits) - (u32) 1);

		/* Shift and mask the value into the field position */

		field_value = (previous_raw_datum >>
				  (bit_granularity - obj_desc->field.bit_offset)) & mask;

		status = acpi_aml_write_field_data_with_update_rule (obj_desc, mask, field_value,
				 this_field_byte_offset + byte_granularity,
				 bit_granularity);
		if (ACPI_FAILURE (status)) {
			goto cleanup;
		}
	}


cleanup:

	return (status);
}