exfldio.c 28.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/******************************************************************************
 *
 * Module Name: exfldio - Aml Field I/O
 *
 *****************************************************************************/

/*
8
 * Copyright (C) 2000 - 2010, Intel Corp.
L
Linus Torvalds 已提交
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
 * 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>
L
Len Brown 已提交
45 46 47 48 49
#include "accommon.h"
#include "acinterp.h"
#include "amlcode.h"
#include "acevents.h"
#include "acdispat.h"
L
Linus Torvalds 已提交
50 51

#define _COMPONENT          ACPI_EXECUTER
L
Len Brown 已提交
52
ACPI_MODULE_NAME("exfldio")
L
Linus Torvalds 已提交
53

R
Robert Moore 已提交
54 55
/* Local prototypes */
static acpi_status
L
Len Brown 已提交
56 57
acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
		       u32 field_datum_byte_offset,
58
		       u64 *value, u32 read_write);
R
Robert Moore 已提交
59 60

static u8
61
acpi_ex_register_overflow(union acpi_operand_object *obj_desc, u64 value);
R
Robert Moore 已提交
62 63

static acpi_status
L
Len Brown 已提交
64 65
acpi_ex_setup_region(union acpi_operand_object *obj_desc,
		     u32 field_datum_byte_offset);
L
Linus Torvalds 已提交
66 67 68 69 70

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_setup_region
 *
R
Robert Moore 已提交
71
 * PARAMETERS:  obj_desc                - Field to be read or written
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81 82
 *              field_datum_byte_offset - Byte offset of this datum within the
 *                                        parent field
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Common processing for acpi_ex_extract_from_field and
 *              acpi_ex_insert_into_field. Initialize the Region if necessary and
 *              validate the request.
 *
 ******************************************************************************/

R
Robert Moore 已提交
83
static acpi_status
L
Len Brown 已提交
84 85
acpi_ex_setup_region(union acpi_operand_object *obj_desc,
		     u32 field_datum_byte_offset)
L
Linus Torvalds 已提交
86
{
L
Len Brown 已提交
87 88
	acpi_status status = AE_OK;
	union acpi_operand_object *rgn_desc;
L
Linus Torvalds 已提交
89

B
Bob Moore 已提交
90
	ACPI_FUNCTION_TRACE_U32(ex_setup_region, field_datum_byte_offset);
L
Linus Torvalds 已提交
91 92 93 94 95

	rgn_desc = obj_desc->common_field.region_obj;

	/* We must have a valid region */

96
	if (rgn_desc->common.type != ACPI_TYPE_REGION) {
97
		ACPI_ERROR((AE_INFO, "Needed Region, found type 0x%X (%s)",
98
			    rgn_desc->common.type,
B
Bob Moore 已提交
99
			    acpi_ut_get_object_type_name(rgn_desc)));
L
Linus Torvalds 已提交
100

L
Len Brown 已提交
101
		return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
L
Linus Torvalds 已提交
102 103 104 105 106 107 108
	}

	/*
	 * If the Region Address and Length have not been previously evaluated,
	 * evaluate them now and save the results.
	 */
	if (!(rgn_desc->common.flags & AOPOBJ_DATA_VALID)) {
L
Len Brown 已提交
109 110 111
		status = acpi_ds_get_region_arguments(rgn_desc);
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
112 113 114
		}
	}

115 116 117 118 119 120
	/* Exit if Address/Length have been disallowed by the host OS */

	if (rgn_desc->common.flags & AOPOBJ_INVALID) {
		return_ACPI_STATUS(AE_AML_ILLEGAL_ADDRESS);
	}

B
Bob Moore 已提交
121
	/*
122
	 * Exit now for SMBus or IPMI address space, it has a non-linear address space
B
Bob Moore 已提交
123 124
	 * and the request cannot be directly validated
	 */
125 126
	if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS ||
	    rgn_desc->region.space_id == ACPI_ADR_SPACE_IPMI) {
B
Bob Moore 已提交
127

128
		/* SMBus or IPMI has a non-linear address space */
L
Linus Torvalds 已提交
129

L
Len Brown 已提交
130
		return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
131 132 133 134 135 136 137
	}
#ifdef ACPI_UNDER_DEVELOPMENT
	/*
	 * If the Field access is any_acc, we can now compute the optimal
	 * access (because we know know the length of the parent region)
	 */
	if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
L
Len Brown 已提交
138 139
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148
		}
	}
#endif

	/*
	 * Validate the request.  The entire request from the byte offset for a
	 * length of one field datum (access width) must fit within the region.
	 * (Region length is specified in bytes)
	 */
B
Bob Moore 已提交
149 150 151 152
	if (rgn_desc->region.length <
	    (obj_desc->common_field.base_byte_offset +
	     field_datum_byte_offset +
	     obj_desc->common_field.access_byte_width)) {
L
Linus Torvalds 已提交
153 154 155 156
		if (acpi_gbl_enable_interpreter_slack) {
			/*
			 * Slack mode only:  We will go ahead and allow access to this
			 * field if it is within the region length rounded up to the next
157
			 * access width boundary. acpi_size cast for 64-bit compile.
L
Linus Torvalds 已提交
158
			 */
L
Len Brown 已提交
159 160 161
			if (ACPI_ROUND_UP(rgn_desc->region.length,
					  obj_desc->common_field.
					  access_byte_width) >=
162 163 164 165
			    ((acpi_size) obj_desc->common_field.
			     base_byte_offset +
			     obj_desc->common_field.access_byte_width +
			     field_datum_byte_offset)) {
L
Len Brown 已提交
166
				return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
167 168 169
			}
		}

L
Len Brown 已提交
170 171
		if (rgn_desc->region.length <
		    obj_desc->common_field.access_byte_width) {
L
Linus Torvalds 已提交
172 173 174 175 176
			/*
			 * This is the case where the access_type (acc_word, etc.) is wider
			 * than the region itself.  For example, a region of length one
			 * byte, and a field with Dword access specified.
			 */
B
Bob Moore 已提交
177
			ACPI_ERROR((AE_INFO,
178
				    "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
B
Bob Moore 已提交
179 180 181 182 183 184
				    acpi_ut_get_node_name(obj_desc->
							  common_field.node),
				    obj_desc->common_field.access_byte_width,
				    acpi_ut_get_node_name(rgn_desc->region.
							  node),
				    rgn_desc->region.length));
L
Linus Torvalds 已提交
185 186 187 188 189 190
		}

		/*
		 * Offset rounded up to next multiple of field width
		 * exceeds region length, indicate an error
		 */
B
Bob Moore 已提交
191
		ACPI_ERROR((AE_INFO,
192
			    "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
B
Bob Moore 已提交
193 194 195 196 197 198
			    acpi_ut_get_node_name(obj_desc->common_field.node),
			    obj_desc->common_field.base_byte_offset,
			    field_datum_byte_offset,
			    obj_desc->common_field.access_byte_width,
			    acpi_ut_get_node_name(rgn_desc->region.node),
			    rgn_desc->region.length));
L
Len Brown 已提交
199 200

		return_ACPI_STATUS(AE_AML_REGION_LIMIT);
L
Linus Torvalds 已提交
201 202
	}

L
Len Brown 已提交
203
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
204 205 206 207 208 209
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_access_region
 *
R
Robert Moore 已提交
210
 * PARAMETERS:  obj_desc                - Field to be read
L
Linus Torvalds 已提交
211 212
 *              field_datum_byte_offset - Byte offset of this datum within the
 *                                        parent field
R
Robert Moore 已提交
213
 *              Value                   - Where to store value (must at least
214
 *                                        64 bits)
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223 224
 *              Function                - Read or Write flag plus other region-
 *                                        dependent flags
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
225
acpi_ex_access_region(union acpi_operand_object *obj_desc,
226
		      u32 field_datum_byte_offset, u64 *value, u32 function)
L
Linus Torvalds 已提交
227
{
L
Len Brown 已提交
228 229
	acpi_status status;
	union acpi_operand_object *rgn_desc;
230
	u32 region_offset;
L
Linus Torvalds 已提交
231

B
Bob Moore 已提交
232
	ACPI_FUNCTION_TRACE(ex_access_region);
L
Linus Torvalds 已提交
233 234 235 236 237

	/*
	 * Ensure that the region operands are fully evaluated and verify
	 * the validity of the request
	 */
L
Len Brown 已提交
238 239 240
	status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248 249 250
	}

	/*
	 * The physical address of this field datum is:
	 *
	 * 1) The base of the region, plus
	 * 2) The base offset of the field, plus
	 * 3) The current offset into the field
	 */
	rgn_desc = obj_desc->common_field.region_obj;
251
	region_offset =
L
Len Brown 已提交
252
	    obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
L
Linus Torvalds 已提交
253 254

	if ((function & ACPI_IO_MASK) == ACPI_READ) {
L
Len Brown 已提交
255 256 257
		ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
	} else {
		ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
L
Linus Torvalds 已提交
258 259
	}

L
Len Brown 已提交
260
	ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
261
			      " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
L
Len Brown 已提交
262 263 264 265 266
			      acpi_ut_get_region_name(rgn_desc->region.
						      space_id),
			      rgn_desc->region.space_id,
			      obj_desc->common_field.access_byte_width,
			      obj_desc->common_field.base_byte_offset,
267
			      field_datum_byte_offset, ACPI_CAST_PTR(void,
268 269 270 271
								     (rgn_desc->
								      region.
								      address +
								      region_offset))));
L
Linus Torvalds 已提交
272 273 274

	/* Invoke the appropriate address_space/op_region handler */

275 276 277 278 279
	status =
	    acpi_ev_address_space_dispatch(rgn_desc, function, region_offset,
					   ACPI_MUL_8(obj_desc->common_field.
						      access_byte_width),
					   value);
L
Linus Torvalds 已提交
280

L
Len Brown 已提交
281
	if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
282
		if (status == AE_NOT_IMPLEMENTED) {
B
Bob Moore 已提交
283
			ACPI_ERROR((AE_INFO,
284
				    "Region %s(0x%X) not implemented",
B
Bob Moore 已提交
285 286 287
				    acpi_ut_get_region_name(rgn_desc->region.
							    space_id),
				    rgn_desc->region.space_id));
L
Len Brown 已提交
288
		} else if (status == AE_NOT_EXIST) {
B
Bob Moore 已提交
289
			ACPI_ERROR((AE_INFO,
290
				    "Region %s(0x%X) has no handler",
B
Bob Moore 已提交
291 292 293
				    acpi_ut_get_region_name(rgn_desc->region.
							    space_id),
				    rgn_desc->region.space_id));
L
Linus Torvalds 已提交
294 295 296
		}
	}

L
Len Brown 已提交
297
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
298 299 300 301 302 303
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_register_overflow
 *
R
Robert Moore 已提交
304
 * PARAMETERS:  obj_desc                - Register(Field) to be written
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312 313 314 315 316
 *              Value                   - Value to be stored
 *
 * RETURN:      TRUE if value overflows the field, FALSE otherwise
 *
 * DESCRIPTION: Check if a value is out of range of the field being written.
 *              Used to check if the values written to Index and Bank registers
 *              are out of range.  Normally, the value is simply truncated
 *              to fit the field, but this case is most likely a serious
 *              coding error in the ASL.
 *
 ******************************************************************************/

R
Robert Moore 已提交
317
static u8
318
acpi_ex_register_overflow(union acpi_operand_object *obj_desc, u64 value)
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328
{

	if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
		/*
		 * The field is large enough to hold the maximum integer, so we can
		 * never overflow it.
		 */
		return (FALSE);
	}

329
	if (value >= ((u64) 1 << obj_desc->common_field.bit_length)) {
L
Linus Torvalds 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
		/*
		 * The Value is larger than the maximum value that can fit into
		 * the register.
		 */
		return (TRUE);
	}

	/* The Value will fit into the field with no truncation */

	return (FALSE);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_field_datum_io
 *
R
Robert Moore 已提交
346
 * PARAMETERS:  obj_desc                - Field to be read
L
Linus Torvalds 已提交
347 348
 *              field_datum_byte_offset - Byte offset of this datum within the
 *                                        parent field
R
Robert Moore 已提交
349
 *              Value                   - Where to store value (must be 64 bits)
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359
 *              read_write              - Read or Write flag
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Read or Write a single datum of a field.  The field_type is
 *              demultiplexed here to handle the different types of fields
 *              (buffer_field, region_field, index_field, bank_field)
 *
 ******************************************************************************/

R
Robert Moore 已提交
360
static acpi_status
L
Len Brown 已提交
361
acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
362
		       u32 field_datum_byte_offset, u64 *value, u32 read_write)
L
Linus Torvalds 已提交
363
{
L
Len Brown 已提交
364
	acpi_status status;
365
	u64 local_value;
L
Linus Torvalds 已提交
366

B
Bob Moore 已提交
367
	ACPI_FUNCTION_TRACE_U32(ex_field_datum_io, field_datum_byte_offset);
L
Linus Torvalds 已提交
368 369 370 371

	if (read_write == ACPI_READ) {
		if (!value) {
			local_value = 0;
R
Robert Moore 已提交
372 373 374

			/* To support reads without saving return value */
			value = &local_value;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385 386
		}

		/* Clear the entire return buffer first, [Very Important!] */

		*value = 0;
	}

	/*
	 * The four types of fields are:
	 *
	 * buffer_field - Read/write from/to a Buffer
	 * region_field - Read/write from/to a Operation Region.
R
Robert Moore 已提交
387 388 389 390
	 * bank_field  - Write to a Bank Register, then read/write from/to an
	 *               operation_region
	 * index_field - Write to an Index Register, then read/write from/to a
	 *               Data Register
L
Linus Torvalds 已提交
391
	 */
392
	switch (obj_desc->common.type) {
L
Linus Torvalds 已提交
393 394 395 396 397 398
	case ACPI_TYPE_BUFFER_FIELD:
		/*
		 * If the buffer_field arguments have not been previously evaluated,
		 * evaluate them now and save the results.
		 */
		if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
L
Len Brown 已提交
399 400 401
			status = acpi_ds_get_buffer_field_arguments(obj_desc);
			if (ACPI_FAILURE(status)) {
				return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409
			}
		}

		if (read_write == ACPI_READ) {
			/*
			 * Copy the data from the source buffer.
			 * Length is the field width in bytes.
			 */
L
Len Brown 已提交
410 411 412 413 414 415 416
			ACPI_MEMCPY(value,
				    (obj_desc->buffer_field.buffer_obj)->buffer.
				    pointer +
				    obj_desc->buffer_field.base_byte_offset +
				    field_datum_byte_offset,
				    obj_desc->common_field.access_byte_width);
		} else {
L
Linus Torvalds 已提交
417 418 419 420
			/*
			 * Copy the data to the target buffer.
			 * Length is the field width in bytes.
			 */
L
Len Brown 已提交
421 422 423 424 425
			ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer.
				    pointer +
				    obj_desc->buffer_field.base_byte_offset +
				    field_datum_byte_offset, value,
				    obj_desc->common_field.access_byte_width);
L
Linus Torvalds 已提交
426 427 428 429 430 431 432
		}

		status = AE_OK;
		break;

	case ACPI_TYPE_LOCAL_BANK_FIELD:

R
Robert Moore 已提交
433 434 435 436
		/*
		 * Ensure that the bank_value is not beyond the capacity of
		 * the register
		 */
L
Len Brown 已提交
437
		if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj,
438 439
					      (u64) obj_desc->bank_field.
					      value)) {
L
Len Brown 已提交
440
			return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
L
Linus Torvalds 已提交
441 442 443 444 445 446
		}

		/*
		 * For bank_fields, we must write the bank_value to the bank_register
		 * (itself a region_field) before we can access the data.
		 */
L
Len Brown 已提交
447 448 449 450 451 452 453
		status =
		    acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj,
					      &obj_desc->bank_field.value,
					      sizeof(obj_desc->bank_field.
						     value));
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467
		}

		/*
		 * Now that the Bank has been selected, fall through to the
		 * region_field case and write the datum to the Operation Region
		 */

		/*lint -fallthrough */

	case ACPI_TYPE_LOCAL_REGION_FIELD:
		/*
		 * For simple region_fields, we just directly access the owning
		 * Operation Region.
		 */
L
Len Brown 已提交
468 469 470
		status =
		    acpi_ex_access_region(obj_desc, field_datum_byte_offset,
					  value, read_write);
L
Linus Torvalds 已提交
471 472 473 474
		break;

	case ACPI_TYPE_LOCAL_INDEX_FIELD:

R
Robert Moore 已提交
475 476 477 478
		/*
		 * Ensure that the index_value is not beyond the capacity of
		 * the register
		 */
L
Len Brown 已提交
479
		if (acpi_ex_register_overflow(obj_desc->index_field.index_obj,
480 481
					      (u64) obj_desc->index_field.
					      value)) {
L
Len Brown 已提交
482
			return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
L
Linus Torvalds 已提交
483 484 485 486 487 488
		}

		/* Write the index value to the index_register (itself a region_field) */

		field_datum_byte_offset += obj_desc->index_field.value;

L
Len Brown 已提交
489 490 491
		ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
				  "Write to Index Register: Value %8.8X\n",
				  field_datum_byte_offset));
L
Linus Torvalds 已提交
492

L
Len Brown 已提交
493 494 495 496 497 498
		status =
		    acpi_ex_insert_into_field(obj_desc->index_field.index_obj,
					      &field_datum_byte_offset,
					      sizeof(field_datum_byte_offset));
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
499 500 501
		}

		if (read_write == ACPI_READ) {
B
Bob Moore 已提交
502

L
Linus Torvalds 已提交
503 504
			/* Read the datum from the data_register */

505 506 507
			ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
					  "Read from Data Register\n"));

L
Len Brown 已提交
508 509 510
			status =
			    acpi_ex_extract_from_field(obj_desc->index_field.
						       data_obj, value,
511
						       sizeof(u64));
L
Len Brown 已提交
512
		} else {
L
Linus Torvalds 已提交
513 514
			/* Write the datum to the data_register */

515 516 517 518
			ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
					  "Write to Data Register: Value %8.8X%8.8X\n",
					  ACPI_FORMAT_UINT64(*value)));

L
Len Brown 已提交
519 520 521
			status =
			    acpi_ex_insert_into_field(obj_desc->index_field.
						      data_obj, value,
522
						      sizeof(u64));
L
Linus Torvalds 已提交
523 524 525 526 527
		}
		break;

	default:

528
		ACPI_ERROR((AE_INFO, "Wrong object type in field I/O %u",
529
			    obj_desc->common.type));
L
Linus Torvalds 已提交
530 531 532 533
		status = AE_AML_INTERNAL;
		break;
	}

L
Len Brown 已提交
534
	if (ACPI_SUCCESS(status)) {
L
Linus Torvalds 已提交
535
		if (read_write == ACPI_READ) {
L
Len Brown 已提交
536 537 538 539 540 541 542 543 544 545 546
			ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
					  "Value Read %8.8X%8.8X, Width %d\n",
					  ACPI_FORMAT_UINT64(*value),
					  obj_desc->common_field.
					  access_byte_width));
		} else {
			ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
					  "Value Written %8.8X%8.8X, Width %d\n",
					  ACPI_FORMAT_UINT64(*value),
					  obj_desc->common_field.
					  access_byte_width));
L
Linus Torvalds 已提交
547 548 549
		}
	}

L
Len Brown 已提交
550
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
551 552 553 554 555 556
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_write_with_update_rule
 *
R
Robert Moore 已提交
557 558 559 560
 * PARAMETERS:  obj_desc                - Field to be written
 *              Mask                    - bitmask within field datum
 *              field_value             - Value to write
 *              field_datum_byte_offset - Offset of datum within field
L
Linus Torvalds 已提交
561 562 563 564 565 566 567 568
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Apply the field update rule to a field write
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
569
acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
570 571
			       u64 mask,
			       u64 field_value, u32 field_datum_byte_offset)
L
Linus Torvalds 已提交
572
{
L
Len Brown 已提交
573
	acpi_status status = AE_OK;
574 575
	u64 merged_value;
	u64 current_value;
L
Linus Torvalds 已提交
576

B
Bob Moore 已提交
577
	ACPI_FUNCTION_TRACE_U32(ex_write_with_update_rule, mask);
L
Linus Torvalds 已提交
578 579 580 581 582 583 584

	/* Start with the new bits  */

	merged_value = field_value;

	/* If the mask is all ones, we don't need to worry about the update rule */

585
	if (mask != ACPI_UINT64_MAX) {
B
Bob Moore 已提交
586

L
Linus Torvalds 已提交
587 588
		/* Decode the update rule */

L
Len Brown 已提交
589 590
		switch (obj_desc->common_field.
			field_flags & AML_FIELD_UPDATE_RULE_MASK) {
L
Linus Torvalds 已提交
591 592 593 594 595
		case AML_FIELD_UPDATE_PRESERVE:
			/*
			 * Check if update rule needs to be applied (not if mask is all
			 * ones)  The left shift drops the bits we want to ignore.
			 */
L
Len Brown 已提交
596 597 598
			if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
				       ACPI_MUL_8(obj_desc->common_field.
						  access_byte_width))) != 0) {
L
Linus Torvalds 已提交
599 600 601 602
				/*
				 * Read the current contents of the byte/word/dword containing
				 * the field, and merge with the new field value.
				 */
L
Len Brown 已提交
603 604 605 606 607 608 609
				status =
				    acpi_ex_field_datum_io(obj_desc,
							   field_datum_byte_offset,
							   &current_value,
							   ACPI_READ);
				if (ACPI_FAILURE(status)) {
					return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
				}

				merged_value |= (current_value & ~mask);
			}
			break;

		case AML_FIELD_UPDATE_WRITE_AS_ONES:

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

			merged_value |= ~mask;
			break;

		case AML_FIELD_UPDATE_WRITE_AS_ZEROS:

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

			merged_value &= mask;
			break;

		default:

B
Bob Moore 已提交
632
			ACPI_ERROR((AE_INFO,
633
				    "Unknown UpdateRule value: 0x%X",
B
Bob Moore 已提交
634 635 636
				    (obj_desc->common_field.
				     field_flags &
				     AML_FIELD_UPDATE_RULE_MASK)));
L
Len Brown 已提交
637
			return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
L
Linus Torvalds 已提交
638 639 640
		}
	}

L
Len Brown 已提交
641
	ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
B
Bob Moore 已提交
642
			  "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
L
Len Brown 已提交
643 644 645 646 647
			  ACPI_FORMAT_UINT64(mask),
			  field_datum_byte_offset,
			  obj_desc->common_field.access_byte_width,
			  ACPI_FORMAT_UINT64(field_value),
			  ACPI_FORMAT_UINT64(merged_value)));
L
Linus Torvalds 已提交
648 649 650

	/* Write the merged value */

L
Len Brown 已提交
651 652
	status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
					&merged_value, ACPI_WRITE);
L
Linus Torvalds 已提交
653

L
Len Brown 已提交
654
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_extract_from_field
 *
 * PARAMETERS:  obj_desc            - Field to be read
 *              Buffer              - Where to store the field data
 *              buffer_length       - Length of Buffer
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Retrieve the current value of the given field
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
672 673
acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
			   void *buffer, u32 buffer_length)
L
Linus Torvalds 已提交
674
{
L
Len Brown 已提交
675
	acpi_status status;
676 677
	u64 raw_datum;
	u64 merged_datum;
L
Len Brown 已提交
678 679 680 681 682 683 684
	u32 field_offset = 0;
	u32 buffer_offset = 0;
	u32 buffer_tail_bits;
	u32 datum_count;
	u32 field_datum_count;
	u32 i;

B
Bob Moore 已提交
685
	ACPI_FUNCTION_TRACE(ex_extract_from_field);
L
Linus Torvalds 已提交
686 687 688

	/* Validate target buffer and clear it */

L
Len Brown 已提交
689 690
	if (buffer_length <
	    ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
B
Bob Moore 已提交
691
		ACPI_ERROR((AE_INFO,
692
			    "Field size %u (bits) is too large for buffer (%u)",
B
Bob Moore 已提交
693
			    obj_desc->common_field.bit_length, buffer_length));
L
Linus Torvalds 已提交
694

L
Len Brown 已提交
695
		return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
L
Linus Torvalds 已提交
696
	}
L
Len Brown 已提交
697
	ACPI_MEMSET(buffer, 0, buffer_length);
L
Linus Torvalds 已提交
698 699 700

	/* Compute the number of datums (access width data items) */

L
Len Brown 已提交
701 702 703 704 705 706 707
	datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
				       obj_desc->common_field.access_bit_width);
	field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
					     obj_desc->common_field.
					     start_field_bit_offset,
					     obj_desc->common_field.
					     access_bit_width);
L
Linus Torvalds 已提交
708 709 710

	/* Priming read from the field */

L
Len Brown 已提交
711 712 713 714 715
	status =
	    acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
				   ACPI_READ);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
716
	}
L
Len Brown 已提交
717 718
	merged_datum =
	    raw_datum >> obj_desc->common_field.start_field_bit_offset;
L
Linus Torvalds 已提交
719 720 721 722

	/* Read the rest of the field */

	for (i = 1; i < field_datum_count; i++) {
B
Bob Moore 已提交
723

L
Linus Torvalds 已提交
724 725 726
		/* Get next input datum from the field */

		field_offset += obj_desc->common_field.access_byte_width;
L
Len Brown 已提交
727 728 729 730
		status = acpi_ex_field_datum_io(obj_desc, field_offset,
						&raw_datum, ACPI_READ);
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
731 732
		}

B
Bob Moore 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
		/*
		 * Merge with previous datum if necessary.
		 *
		 * Note: Before the shift, check if the shift value will be larger than
		 * the integer size. If so, there is no need to perform the operation.
		 * This avoids the differences in behavior between different compilers
		 * concerning shift values larger than the target data width.
		 */
		if ((obj_desc->common_field.access_bit_width -
		     obj_desc->common_field.start_field_bit_offset) <
		    ACPI_INTEGER_BIT_SIZE) {
			merged_datum |=
			    raw_datum << (obj_desc->common_field.
					  access_bit_width -
					  obj_desc->common_field.
					  start_field_bit_offset);
		}
L
Linus Torvalds 已提交
750 751 752 753 754 755 756

		if (i == datum_count) {
			break;
		}

		/* Write merged datum to target buffer */

L
Len Brown 已提交
757 758 759
		ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
			    ACPI_MIN(obj_desc->common_field.access_byte_width,
				     buffer_length - buffer_offset));
L
Linus Torvalds 已提交
760 761

		buffer_offset += obj_desc->common_field.access_byte_width;
L
Len Brown 已提交
762 763
		merged_datum =
		    raw_datum >> obj_desc->common_field.start_field_bit_offset;
L
Linus Torvalds 已提交
764 765 766 767
	}

	/* Mask off any extra bits in the last datum */

R
Robert Moore 已提交
768
	buffer_tail_bits = obj_desc->common_field.bit_length %
L
Len Brown 已提交
769
	    obj_desc->common_field.access_bit_width;
L
Linus Torvalds 已提交
770
	if (buffer_tail_bits) {
L
Len Brown 已提交
771
		merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
L
Linus Torvalds 已提交
772 773 774 775
	}

	/* Write the last datum to the buffer */

L
Len Brown 已提交
776 777 778
	ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
		    ACPI_MIN(obj_desc->common_field.access_byte_width,
			     buffer_length - buffer_offset));
L
Linus Torvalds 已提交
779

L
Len Brown 已提交
780
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_insert_into_field
 *
 * PARAMETERS:  obj_desc            - Field to be written
 *              Buffer              - Data to be written
 *              buffer_length       - Length of Buffer
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Store the Buffer contents into the given field
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
798 799
acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
			  void *buffer, u32 buffer_length)
L
Linus Torvalds 已提交
800
{
L
Len Brown 已提交
801
	acpi_status status;
802 803 804 805
	u64 mask;
	u64 width_mask;
	u64 merged_datum;
	u64 raw_datum = 0;
L
Len Brown 已提交
806 807 808 809 810 811
	u32 field_offset = 0;
	u32 buffer_offset = 0;
	u32 buffer_tail_bits;
	u32 datum_count;
	u32 field_datum_count;
	u32 i;
812 813
	u32 required_length;
	void *new_buffer;
L
Len Brown 已提交
814

B
Bob Moore 已提交
815
	ACPI_FUNCTION_TRACE(ex_insert_into_field);
L
Linus Torvalds 已提交
816 817 818

	/* Validate input buffer */

819 820 821 822 823 824 825 826 827 828
	new_buffer = NULL;
	required_length =
	    ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
	/*
	 * We must have a buffer that is at least as long as the field
	 * we are writing to.  This is because individual fields are
	 * indivisible and partial writes are not supported -- as per
	 * the ACPI specification.
	 */
	if (buffer_length < required_length) {
L
Linus Torvalds 已提交
829

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
		/* We need to create a new buffer */

		new_buffer = ACPI_ALLOCATE_ZEROED(required_length);
		if (!new_buffer) {
			return_ACPI_STATUS(AE_NO_MEMORY);
		}

		/*
		 * Copy the original data to the new buffer, starting
		 * at Byte zero.  All unused (upper) bytes of the
		 * buffer will be 0.
		 */
		ACPI_MEMCPY((char *)new_buffer, (char *)buffer, buffer_length);
		buffer = new_buffer;
		buffer_length = required_length;
L
Linus Torvalds 已提交
845 846
	}

B
Bob Moore 已提交
847 848 849 850 851 852
	/*
	 * Create the bitmasks used for bit insertion.
	 * Note: This if/else is used to bypass compiler differences with the
	 * shift operator
	 */
	if (obj_desc->common_field.access_bit_width == ACPI_INTEGER_BIT_SIZE) {
853
		width_mask = ACPI_UINT64_MAX;
B
Bob Moore 已提交
854 855 856 857 858
	} else {
		width_mask =
		    ACPI_MASK_BITS_ABOVE(obj_desc->common_field.
					 access_bit_width);
	}
L
Linus Torvalds 已提交
859

B
Bob Moore 已提交
860 861 862 863
	mask = width_mask &
	    ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);

	/* Compute the number of datums (access width data items) */
B
Bob Moore 已提交
864 865 866 867 868 869 870 871 872

	datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
				       obj_desc->common_field.access_bit_width);

	field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
					     obj_desc->common_field.
					     start_field_bit_offset,
					     obj_desc->common_field.
					     access_bit_width);
L
Linus Torvalds 已提交
873 874 875

	/* Get initial Datum from the input buffer */

L
Len Brown 已提交
876 877 878
	ACPI_MEMCPY(&raw_datum, buffer,
		    ACPI_MIN(obj_desc->common_field.access_byte_width,
			     buffer_length - buffer_offset));
L
Linus Torvalds 已提交
879

L
Len Brown 已提交
880 881
	merged_datum =
	    raw_datum << obj_desc->common_field.start_field_bit_offset;
L
Linus Torvalds 已提交
882 883 884 885

	/* Write the entire field */

	for (i = 1; i < field_datum_count; i++) {
B
Bob Moore 已提交
886

L
Linus Torvalds 已提交
887 888 889
		/* Write merged datum to the target field */

		merged_datum &= mask;
L
Len Brown 已提交
890 891 892 893
		status = acpi_ex_write_with_update_rule(obj_desc, mask,
							merged_datum,
							field_offset);
		if (ACPI_FAILURE(status)) {
894
			goto exit;
L
Linus Torvalds 已提交
895 896 897
		}

		field_offset += obj_desc->common_field.access_byte_width;
B
Bob Moore 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

		/*
		 * Start new output datum by merging with previous input datum
		 * if necessary.
		 *
		 * Note: Before the shift, check if the shift value will be larger than
		 * the integer size. If so, there is no need to perform the operation.
		 * This avoids the differences in behavior between different compilers
		 * concerning shift values larger than the target data width.
		 */
		if ((obj_desc->common_field.access_bit_width -
		     obj_desc->common_field.start_field_bit_offset) <
		    ACPI_INTEGER_BIT_SIZE) {
			merged_datum =
			    raw_datum >> (obj_desc->common_field.
					  access_bit_width -
					  obj_desc->common_field.
					  start_field_bit_offset);
		} else {
			merged_datum = 0;
		}

B
Bob Moore 已提交
920
		mask = width_mask;
L
Linus Torvalds 已提交
921 922 923 924 925 926 927 928

		if (i == datum_count) {
			break;
		}

		/* Get the next input datum from the buffer */

		buffer_offset += obj_desc->common_field.access_byte_width;
L
Len Brown 已提交
929 930 931 932 933
		ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
			    ACPI_MIN(obj_desc->common_field.access_byte_width,
				     buffer_length - buffer_offset));
		merged_datum |=
		    raw_datum << obj_desc->common_field.start_field_bit_offset;
L
Linus Torvalds 已提交
934 935 936 937 938
	}

	/* Mask off any extra bits in the last datum */

	buffer_tail_bits = (obj_desc->common_field.bit_length +
L
Len Brown 已提交
939 940
			    obj_desc->common_field.start_field_bit_offset) %
	    obj_desc->common_field.access_bit_width;
L
Linus Torvalds 已提交
941
	if (buffer_tail_bits) {
L
Len Brown 已提交
942
		mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
L
Linus Torvalds 已提交
943 944 945 946 947
	}

	/* Write the last datum to the field */

	merged_datum &= mask;
L
Len Brown 已提交
948 949 950
	status = acpi_ex_write_with_update_rule(obj_desc,
						mask, merged_datum,
						field_offset);
L
Linus Torvalds 已提交
951

952 953 954 955 956 957
      exit:
	/* Free temporary buffer if we used one */

	if (new_buffer) {
		ACPI_FREE(new_buffer);
	}
L
Len Brown 已提交
958
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
959
}