acpi_status acpi_hw_validate_register(struct acpi_generic_address *reg, u8 max_bit_width, u64 *address) { u8 bit_width; u8 access_width; /* Must have a valid pointer to a GAS structure */ if (!reg) { return (AE_BAD_PARAMETER); } /* * Copy the target address. This handles possible alignment issues. * Address must not be null. A null address also indicates an optional * ACPI register that is not supported, so no error message. */ ACPI_MOVE_64_TO_64(address, ®->address); if (!(*address)) { return (AE_BAD_ADDRESS); } /* Validate the space_ID */ if ((reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) && (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO)) { ACPI_ERROR((AE_INFO, "Unsupported address space: 0x%X", reg->space_id)); return (AE_SUPPORT); } /* Validate the access_width */ if (reg->access_width > 4) { ACPI_ERROR((AE_INFO, "Unsupported register access width: 0x%X", reg->access_width)); return (AE_SUPPORT); } /* Validate the bit_width, convert access_width into number of bits */ access_width = acpi_hw_get_access_bit_width(*address, reg, max_bit_width); bit_width = ACPI_ROUND_UP(reg->bit_offset + reg->bit_width, access_width); if (max_bit_width < bit_width) { ACPI_WARNING((AE_INFO, "Requested bit width 0x%X is smaller than register bit width 0x%X", max_bit_width, bit_width)); return (AE_SUPPORT); } return (AE_OK); }
static void XfCheckFieldRange ( ACPI_PARSE_OBJECT *Op, UINT32 RegionBitLength, UINT32 FieldBitOffset, UINT32 FieldBitLength, UINT32 AccessBitWidth) { UINT32 FieldEndBitOffset; /* * Check each field unit against the region size. The entire * field unit (start offset plus length) must fit within the * region. */ FieldEndBitOffset = FieldBitOffset + FieldBitLength; if (FieldEndBitOffset > RegionBitLength) { /* Field definition itself is beyond the end-of-region */ AslError (ASL_ERROR, ASL_MSG_FIELD_UNIT_OFFSET, Op, NULL); return; } /* * Now check that the field plus AccessWidth doesn't go beyond * the end-of-region. Assumes AccessBitWidth is a power of 2 */ FieldEndBitOffset = ACPI_ROUND_UP (FieldEndBitOffset, AccessBitWidth); if (FieldEndBitOffset > RegionBitLength) { /* Field definition combined with the access is beyond EOR */ AslError (ASL_ERROR, ASL_MSG_FIELD_UNIT_ACCESS_WIDTH, Op, NULL); } }
static u32 acpi_ex_generate_access(u32 field_bit_offset, u32 field_bit_length, u32 region_length) { u32 field_byte_length; u32 field_byte_offset; u32 field_byte_end_offset; u32 access_byte_width; u32 field_start_offset; u32 field_end_offset; u32 minimum_access_width = 0xFFFFFFFF; u32 minimum_accesses = 0xFFFFFFFF; u32 accesses; ACPI_FUNCTION_TRACE(ex_generate_access); /* Round Field start offset and length to "minimal" byte boundaries */ field_byte_offset = ACPI_DIV_8(ACPI_ROUND_DOWN(field_bit_offset, 8)); field_byte_end_offset = ACPI_DIV_8(ACPI_ROUND_UP(field_bit_length + field_bit_offset, 8)); field_byte_length = field_byte_end_offset - field_byte_offset; ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Bit length %u, Bit offset %u\n", field_bit_length, field_bit_offset)); ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Byte Length %u, Byte Offset %u, End Offset %u\n", field_byte_length, field_byte_offset, field_byte_end_offset)); /* * Iterative search for the maximum access width that is both aligned * and does not go beyond the end of the region * * Start at byte_acc and work upwards to qword_acc max. (1,2,4,8 bytes) */ for (access_byte_width = 1; access_byte_width <= 8; access_byte_width <<= 1) { /* * 1) Round end offset up to next access boundary and make sure that * this does not go beyond the end of the parent region. * 2) When the Access width is greater than the field_byte_length, we * are done. (This does not optimize for the perfectly aligned * case yet). */ if (ACPI_ROUND_UP(field_byte_end_offset, access_byte_width) <= region_length) { field_start_offset = ACPI_ROUND_DOWN(field_byte_offset, access_byte_width) / access_byte_width; field_end_offset = ACPI_ROUND_UP((field_byte_length + field_byte_offset), access_byte_width) / access_byte_width; accesses = field_end_offset - field_start_offset; ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "AccessWidth %u end is within region\n", access_byte_width)); ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Field Start %u, Field End %u -- requires %u accesses\n", field_start_offset, field_end_offset, accesses)); /* Single access is optimal */ if (accesses <= 1) { ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Entire field can be accessed with one operation of size %u\n", access_byte_width)); return_VALUE(access_byte_width); } /* * Fits in the region, but requires more than one read/write. * try the next wider access on next iteration */ if (accesses < minimum_accesses) { minimum_accesses = accesses; minimum_access_width = access_byte_width; } } else { ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "AccessWidth %u end is NOT within region\n", access_byte_width)); if (access_byte_width == 1) { ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Field goes beyond end-of-region!\n")); /* Field does not fit in the region at all */ return_VALUE(0); } /* * This width goes beyond the end-of-region, back off to * previous access */ ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Backing off to previous optimal access width of %u\n", minimum_access_width)); return_VALUE(minimum_access_width); } } /* * Could not read/write field with one operation, * just use max access width */ ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Cannot access field in one operation, using width 8\n")); return_VALUE(8); }
static ACPI_STATUS AcpiExSetupRegion ( ACPI_OPERAND_OBJECT *ObjDesc, UINT32 FieldDatumByteOffset) { ACPI_STATUS Status = AE_OK; ACPI_OPERAND_OBJECT *RgnDesc; UINT8 SpaceId; ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset); RgnDesc = ObjDesc->CommonField.RegionObj; /* We must have a valid region */ if (RgnDesc->Common.Type != ACPI_TYPE_REGION) { ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)", RgnDesc->Common.Type, AcpiUtGetObjectTypeName (RgnDesc))); return_ACPI_STATUS (AE_AML_OPERAND_TYPE); } SpaceId = RgnDesc->Region.SpaceId; /* Validate the Space ID */ if (!AcpiIsValidSpaceId (SpaceId)) { ACPI_ERROR ((AE_INFO, "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId)); return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID); } /* * If the Region Address and Length have not been previously evaluated, * evaluate them now and save the results. */ if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID)) { Status = AcpiDsGetRegionArguments (RgnDesc); if (ACPI_FAILURE (Status)) { return_ACPI_STATUS (Status); } } /* * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear * address space and the request cannot be directly validated */ if (SpaceId == ACPI_ADR_SPACE_SMBUS || SpaceId == ACPI_ADR_SPACE_GSBUS || SpaceId == ACPI_ADR_SPACE_IPMI) { /* SMBus or IPMI has a non-linear address space */ return_ACPI_STATUS (AE_OK); } #ifdef ACPI_UNDER_DEVELOPMENT /* * If the Field access is AnyAcc, we can now compute the optimal * access (because we know know the length of the parent region) */ if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID)) { if (ACPI_FAILURE (Status)) { return_ACPI_STATUS (Status); } } #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) */ if (RgnDesc->Region.Length < (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset + ObjDesc->CommonField.AccessByteWidth)) { if (AcpiGbl_EnableInterpreterSlack) { /* * 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 * access width boundary. ACPI_SIZE cast for 64-bit compile. */ if (ACPI_ROUND_UP (RgnDesc->Region.Length, ObjDesc->CommonField.AccessByteWidth) >= ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset + ObjDesc->CommonField.AccessByteWidth + FieldDatumByteOffset)) { return_ACPI_STATUS (AE_OK); } } if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth) { /* * This is the case where the AccessType (AccWord, etc.) is wider * than the region itself. For example, a region of length one * byte, and a field with Dword access specified. */ ACPI_ERROR ((AE_INFO, "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)", AcpiUtGetNodeName (ObjDesc->CommonField.Node), ObjDesc->CommonField.AccessByteWidth, AcpiUtGetNodeName (RgnDesc->Region.Node), RgnDesc->Region.Length)); } /* * Offset rounded up to next multiple of field width * exceeds region length, indicate an error */ ACPI_ERROR ((AE_INFO, "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)", AcpiUtGetNodeName (ObjDesc->CommonField.Node), ObjDesc->CommonField.BaseByteOffset, FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth, AcpiUtGetNodeName (RgnDesc->Region.Node), RgnDesc->Region.Length)); return_ACPI_STATUS (AE_AML_REGION_LIMIT); } return_ACPI_STATUS (AE_OK); }
static u32 acpi_ex_generate_access(u32 field_bit_offset, u32 field_bit_length, u32 region_length) { u32 field_byte_length; u32 field_byte_offset; u32 field_byte_end_offset; u32 access_byte_width; u32 field_start_offset; u32 field_end_offset; u32 minimum_access_width = 0xFFFFFFFF; u32 minimum_accesses = 0xFFFFFFFF; u32 accesses; ACPI_FUNCTION_TRACE(ex_generate_access); /* */ field_byte_offset = ACPI_DIV_8(ACPI_ROUND_DOWN(field_bit_offset, 8)); field_byte_end_offset = ACPI_DIV_8(ACPI_ROUND_UP(field_bit_length + field_bit_offset, 8)); field_byte_length = field_byte_end_offset - field_byte_offset; ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Bit length %u, Bit offset %u\n", field_bit_length, field_bit_offset)); ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Byte Length %u, Byte Offset %u, End Offset %u\n", field_byte_length, field_byte_offset, field_byte_end_offset)); /* */ for (access_byte_width = 1; access_byte_width <= 8; access_byte_width <<= 1) { /* */ if (ACPI_ROUND_UP(field_byte_end_offset, access_byte_width) <= region_length) { field_start_offset = ACPI_ROUND_DOWN(field_byte_offset, access_byte_width) / access_byte_width; field_end_offset = ACPI_ROUND_UP((field_byte_length + field_byte_offset), access_byte_width) / access_byte_width; accesses = field_end_offset - field_start_offset; ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "AccessWidth %u end is within region\n", access_byte_width)); ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Field Start %u, Field End %u -- requires %u accesses\n", field_start_offset, field_end_offset, accesses)); /* */ if (accesses <= 1) { ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Entire field can be accessed with one operation of size %u\n", access_byte_width)); return_VALUE(access_byte_width); } /* */ if (accesses < minimum_accesses) { minimum_accesses = accesses; minimum_access_width = access_byte_width; } } else { ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "AccessWidth %u end is NOT within region\n", access_byte_width)); if (access_byte_width == 1) { ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Field goes beyond end-of-region!\n")); /* */ return_VALUE(0); } /* */ ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Backing off to previous optimal access width of %u\n", minimum_access_width)); return_VALUE(minimum_access_width); } } /* */ ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "Cannot access field in one operation, using width 8\n")); return_VALUE(8); }
static acpi_status acpi_ex_setup_region(union acpi_operand_object *obj_desc, u32 field_datum_byte_offset) { acpi_status status = AE_OK; union acpi_operand_object *rgn_desc; u8 space_id; ACPI_FUNCTION_TRACE_U32(ex_setup_region, field_datum_byte_offset); rgn_desc = obj_desc->common_field.region_obj; /* We must have a valid region */ if (rgn_desc->common.type != ACPI_TYPE_REGION) { ACPI_ERROR((AE_INFO, "Needed Region, found type 0x%X (%s)", rgn_desc->common.type, acpi_ut_get_object_type_name(rgn_desc))); return_ACPI_STATUS(AE_AML_OPERAND_TYPE); } space_id = rgn_desc->region.space_id; /* Validate the Space ID */ if (!acpi_is_valid_space_id(space_id)) { ACPI_ERROR((AE_INFO, "Invalid/unknown Address Space ID: 0x%2.2X", space_id)); return_ACPI_STATUS(AE_AML_INVALID_SPACE_ID); } /* * 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)) { status = acpi_ds_get_region_arguments(rgn_desc); if (ACPI_FAILURE(status)) { return_ACPI_STATUS(status); } } /* 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); } /* * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear * address space and the request cannot be directly validated */ if (space_id == ACPI_ADR_SPACE_SMBUS || space_id == ACPI_ADR_SPACE_GSBUS || space_id == ACPI_ADR_SPACE_IPMI) { /* SMBus or IPMI has a non-linear address space */ return_ACPI_STATUS(AE_OK); } #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)) { if (ACPI_FAILURE(status)) { return_ACPI_STATUS(status); } } #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) */ if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset + field_datum_byte_offset + obj_desc->common_field.access_byte_width)) { 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 * access width boundary. acpi_size cast for 64-bit compile. */ if (ACPI_ROUND_UP(rgn_desc->region.length, obj_desc->common_field. access_byte_width) >= ((acpi_size) obj_desc->common_field. base_byte_offset + obj_desc->common_field.access_byte_width + field_datum_byte_offset)) { return_ACPI_STATUS(AE_OK); } } if (rgn_desc->region.length < obj_desc->common_field.access_byte_width) { /* * 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. */ ACPI_ERROR((AE_INFO, "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)", 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)); } /* * Offset rounded up to next multiple of field width * exceeds region length, indicate an error */ ACPI_ERROR((AE_INFO, "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)", 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)); return_ACPI_STATUS(AE_AML_REGION_LIMIT); } return_ACPI_STATUS(AE_OK); }
static UINT32 AcpiExGenerateAccess ( UINT32 FieldBitOffset, UINT32 FieldBitLength, UINT32 RegionLength) { UINT32 FieldByteLength; UINT32 FieldByteOffset; UINT32 FieldByteEndOffset; UINT32 AccessByteWidth; UINT32 FieldStartOffset; UINT32 FieldEndOffset; UINT32 MinimumAccessWidth = 0xFFFFFFFF; UINT32 MinimumAccesses = 0xFFFFFFFF; UINT32 Accesses; ACPI_FUNCTION_TRACE (ExGenerateAccess); /* Round Field start offset and length to "minimal" byte boundaries */ FieldByteOffset = ACPI_DIV_8 (ACPI_ROUND_DOWN (FieldBitOffset, 8)); FieldByteEndOffset = ACPI_DIV_8 (ACPI_ROUND_UP (FieldBitLength + FieldBitOffset, 8)); FieldByteLength = FieldByteEndOffset - FieldByteOffset; ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Bit length %u, Bit offset %u\n", FieldBitLength, FieldBitOffset)); ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Byte Length %u, Byte Offset %u, End Offset %u\n", FieldByteLength, FieldByteOffset, FieldByteEndOffset)); /* * Iterative search for the maximum access width that is both aligned * and does not go beyond the end of the region * * Start at ByteAcc and work upwards to QwordAcc max. (1,2,4,8 bytes) */ for (AccessByteWidth = 1; AccessByteWidth <= 8; AccessByteWidth <<= 1) { /* * 1) Round end offset up to next access boundary and make sure that * this does not go beyond the end of the parent region. * 2) When the Access width is greater than the FieldByteLength, we * are done. (This does not optimize for the perfectly aligned * case yet). */ if (ACPI_ROUND_UP (FieldByteEndOffset, AccessByteWidth) <= RegionLength) { FieldStartOffset = ACPI_ROUND_DOWN (FieldByteOffset, AccessByteWidth) / AccessByteWidth; FieldEndOffset = ACPI_ROUND_UP ((FieldByteLength + FieldByteOffset), AccessByteWidth) / AccessByteWidth; Accesses = FieldEndOffset - FieldStartOffset; ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "AccessWidth %u end is within region\n", AccessByteWidth)); ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Field Start %u, Field End %u -- requires %u accesses\n", FieldStartOffset, FieldEndOffset, Accesses)); /* Single access is optimal */ if (Accesses <= 1) { ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Entire field can be accessed with one operation of size %u\n", AccessByteWidth)); return_VALUE (AccessByteWidth); } /* * Fits in the region, but requires more than one read/write. * try the next wider access on next iteration */ if (Accesses < MinimumAccesses) { MinimumAccesses = Accesses; MinimumAccessWidth = AccessByteWidth; } } else { ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "AccessWidth %u end is NOT within region\n", AccessByteWidth)); if (AccessByteWidth == 1) { ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Field goes beyond end-of-region!\n")); /* Field does not fit in the region at all */ return_VALUE (0); } /* * This width goes beyond the end-of-region, back off to * previous access */ ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Backing off to previous optimal access width of %u\n", MinimumAccessWidth)); return_VALUE (MinimumAccessWidth); } } /* * Could not read/write field with one operation, * just use max access width */ ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Cannot access field in one operation, using width 8\n")); return_VALUE (8); }
/******************************************************************************* * * FUNCTION: acpi_ex_system_memory_space_handler * * PARAMETERS: Function - Read or Write operation * Address - Where in the space to read or write * bit_width - Field width in bits (8, 16, or 32) * Value - Pointer to in or out value * handler_context - Pointer to Handler's context * region_context - Pointer to context specific to the * accessed region * * RETURN: Status * * DESCRIPTION: Handler for the System Memory address space (Op Region) * ******************************************************************************/ acpi_status acpi_ex_system_memory_space_handler(u32 function, acpi_physical_address address, u32 bit_width, u64 *value, void *handler_context, void *region_context) { acpi_status status = AE_OK; void *logical_addr_ptr = NULL; struct acpi_mem_space_context *mem_info = region_context; u32 length; acpi_size map_length; acpi_size page_boundary_map_length; #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED u32 remainder; #endif ACPI_FUNCTION_TRACE(ex_system_memory_space_handler); /* Validate and translate the bit width */ switch (bit_width) { case 8: length = 1; break; case 16: length = 2; break; case 32: length = 4; break; case 64: length = 8; break; default: ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %u", bit_width)); return_ACPI_STATUS(AE_AML_OPERAND_VALUE); } #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED /* * Hardware does not support non-aligned data transfers, we must verify * the request. */ (void)acpi_ut_short_divide((u64) address, length, NULL, &remainder); if (remainder != 0) { return_ACPI_STATUS(AE_AML_ALIGNMENT); } #endif /* * Does the request fit into the cached memory mapping? * Is 1) Address below the current mapping? OR * 2) Address beyond the current mapping? */ if ((address < mem_info->mapped_physical_address) || (((u64) address + length) > ((u64) mem_info->mapped_physical_address + mem_info->mapped_length))) { /* * The request cannot be resolved by the current memory mapping; * Delete the existing mapping and create a new one. */ if (mem_info->mapped_length) { /* Valid mapping, delete it */ acpi_os_unmap_memory(mem_info->mapped_logical_address, mem_info->mapped_length); } /* * Attempt to map from the requested address to the end of the region. * However, we will never map more than one page, nor will we cross * a page boundary. */ map_length = (acpi_size) ((mem_info->address + mem_info->length) - address); /* * If mapping the entire remaining portion of the region will cross * a page boundary, just map up to the page boundary, do not cross. * On some systems, crossing a page boundary while mapping regions * can cause warnings if the pages have different attributes * due to resource management */ page_boundary_map_length = ACPI_ROUND_UP(address, ACPI_DEFAULT_PAGE_SIZE) - address; if (!page_boundary_map_length) { page_boundary_map_length = ACPI_DEFAULT_PAGE_SIZE; } if (map_length > page_boundary_map_length) { map_length = page_boundary_map_length; } /* Create a new mapping starting at the address given */ mem_info->mapped_logical_address = acpi_os_map_memory((acpi_physical_address) address, map_length); if (!mem_info->mapped_logical_address) { ACPI_ERROR((AE_INFO, "Could not map memory at 0x%8.8X%8.8X, size %u", ACPI_FORMAT_NATIVE_UINT(address), (u32) map_length)); mem_info->mapped_length = 0; return_ACPI_STATUS(AE_NO_MEMORY); } /* Save the physical address and mapping size */ mem_info->mapped_physical_address = address; mem_info->mapped_length = map_length; } /* * Generate a logical pointer corresponding to the address we want to * access */ logical_addr_ptr = mem_info->mapped_logical_address + ((u64) address - (u64) mem_info->mapped_physical_address); ACPI_DEBUG_PRINT((ACPI_DB_INFO, "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n", bit_width, function, ACPI_FORMAT_NATIVE_UINT(address))); /* * Perform the memory read or write * * Note: For machines that do not support non-aligned transfers, the target * address was checked for alignment above. We do not attempt to break the * transfer up into smaller (byte-size) chunks because the AML specifically * asked for a transfer width that the hardware may require. */ switch (function) { case ACPI_READ: *value = 0; switch (bit_width) { case 8: *value = (u64) ACPI_GET8(logical_addr_ptr); break; case 16: *value = (u64) ACPI_GET16(logical_addr_ptr); break; case 32: *value = (u64) ACPI_GET32(logical_addr_ptr); break; case 64: *value = (u64) ACPI_GET64(logical_addr_ptr); break; default: /* bit_width was already validated */ break; } break; case ACPI_WRITE: switch (bit_width) { case 8: ACPI_SET8(logical_addr_ptr) = (u8) * value; break; case 16: ACPI_SET16(logical_addr_ptr) = (u16) * value; break; case 32: ACPI_SET32(logical_addr_ptr) = (u32) * value; break; case 64: ACPI_SET64(logical_addr_ptr) = (u64) * value; break; default: /* bit_width was already validated */ break; } break; default: status = AE_BAD_PARAMETER; break; } return_ACPI_STATUS(status); }
ACPI_STATUS AcpiExSystemMemorySpaceHandler ( UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 BitWidth, UINT64 *Value, void *HandlerContext, void *RegionContext) { ACPI_STATUS Status = AE_OK; void *LogicalAddrPtr = NULL; ACPI_MEM_SPACE_CONTEXT *MemInfo = RegionContext; UINT32 Length; ACPI_SIZE MapLength; ACPI_SIZE PageBoundaryMapLength; #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED UINT32 Remainder; #endif ACPI_FUNCTION_TRACE (ExSystemMemorySpaceHandler); /* Validate and translate the bit width */ switch (BitWidth) { case 8: Length = 1; break; case 16: Length = 2; break; case 32: Length = 4; break; case 64: Length = 8; break; default: ACPI_ERROR ((AE_INFO, "Invalid SystemMemory width %u", BitWidth)); return_ACPI_STATUS (AE_AML_OPERAND_VALUE); } #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED /* * Hardware does not support non-aligned data transfers, we must verify * the request. */ (void) AcpiUtShortDivide ((UINT64) Address, Length, NULL, &Remainder); if (Remainder != 0) { return_ACPI_STATUS (AE_AML_ALIGNMENT); } #endif /* * Does the request fit into the cached memory mapping? * Is 1) Address below the current mapping? OR * 2) Address beyond the current mapping? */ if ((Address < MemInfo->MappedPhysicalAddress) || (((UINT64) Address + Length) > ((UINT64) MemInfo->MappedPhysicalAddress + MemInfo->MappedLength))) { /* * The request cannot be resolved by the current memory mapping; * Delete the existing mapping and create a new one. */ if (MemInfo->MappedLength) { /* Valid mapping, delete it */ AcpiOsUnmapMemory (MemInfo->MappedLogicalAddress, MemInfo->MappedLength); } /* * October 2009: Attempt to map from the requested address to the * end of the region. However, we will never map more than one * page, nor will we cross a page boundary. */ MapLength = (ACPI_SIZE) ((MemInfo->Address + MemInfo->Length) - Address); /* * If mapping the entire remaining portion of the region will cross * a page boundary, just map up to the page boundary, do not cross. * On some systems, crossing a page boundary while mapping regions * can cause warnings if the pages have different attributes * due to resource management. * * This has the added benefit of constraining a single mapping to * one page, which is similar to the original code that used a 4k * maximum window. */ PageBoundaryMapLength = (ACPI_SIZE) (ACPI_ROUND_UP (Address, ACPI_DEFAULT_PAGE_SIZE) - Address); if (PageBoundaryMapLength == 0) { PageBoundaryMapLength = ACPI_DEFAULT_PAGE_SIZE; } if (MapLength > PageBoundaryMapLength) { MapLength = PageBoundaryMapLength; } /* Create a new mapping starting at the address given */ MemInfo->MappedLogicalAddress = AcpiOsMapMemory (Address, MapLength); if (!MemInfo->MappedLogicalAddress) { ACPI_ERROR ((AE_INFO, "Could not map memory at 0x%8.8X%8.8X, size %u", ACPI_FORMAT_UINT64 (Address), (UINT32) MapLength)); MemInfo->MappedLength = 0; return_ACPI_STATUS (AE_NO_MEMORY); } /* Save the physical address and mapping size */ MemInfo->MappedPhysicalAddress = Address; MemInfo->MappedLength = MapLength; } /* * Generate a logical pointer corresponding to the address we want to * access */ LogicalAddrPtr = MemInfo->MappedLogicalAddress + ((UINT64) Address - (UINT64) MemInfo->MappedPhysicalAddress); ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n", BitWidth, Function, ACPI_FORMAT_UINT64 (Address))); /* * Perform the memory read or write * * Note: For machines that do not support non-aligned transfers, the target * address was checked for alignment above. We do not attempt to break the * transfer up into smaller (byte-size) chunks because the AML specifically * asked for a transfer width that the hardware may require. */ switch (Function) { case ACPI_READ: *Value = 0; switch (BitWidth) { case 8: *Value = (UINT64) ACPI_GET8 (LogicalAddrPtr); break; case 16: *Value = (UINT64) ACPI_GET16 (LogicalAddrPtr); break; case 32: *Value = (UINT64) ACPI_GET32 (LogicalAddrPtr); break; case 64: *Value = (UINT64) ACPI_GET64 (LogicalAddrPtr); break; default: /* BitWidth was already validated */ break; } break; case ACPI_WRITE: switch (BitWidth) { case 8: ACPI_SET8 (LogicalAddrPtr, *Value); break; case 16: ACPI_SET16 (LogicalAddrPtr, *Value); break; case 32: ACPI_SET32 (LogicalAddrPtr, *Value); break; case 64: ACPI_SET64 (LogicalAddrPtr, *Value); break; default: /* BitWidth was already validated */ break; } break; default: Status = AE_BAD_PARAMETER; break; } return_ACPI_STATUS (Status); }
acpi_status acpi_ex_setup_region ( union acpi_operand_object *obj_desc, u32 field_datum_byte_offset) { acpi_status status = AE_OK; union acpi_operand_object *rgn_desc; ACPI_FUNCTION_TRACE_U32 ("ex_setup_region", field_datum_byte_offset); rgn_desc = obj_desc->common_field.region_obj; /* We must have a valid region */ if (ACPI_GET_OBJECT_TYPE (rgn_desc) != ACPI_TYPE_REGION) { ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %X (%s)\n", ACPI_GET_OBJECT_TYPE (rgn_desc), acpi_ut_get_object_type_name (rgn_desc))); return_ACPI_STATUS (AE_AML_OPERAND_TYPE); } /* * 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)) { status = acpi_ds_get_region_arguments (rgn_desc); if (ACPI_FAILURE (status)) { return_ACPI_STATUS (status); } } if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS) { /* SMBus has a non-linear address space */ return_ACPI_STATUS (AE_OK); } #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)) { if (ACPI_FAILURE (status)) { return_ACPI_STATUS (status); } } #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) */ if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset + field_datum_byte_offset + obj_desc->common_field.access_byte_width)) { if (rgn_desc->region.length < obj_desc->common_field.access_byte_width) { /* * 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. */ ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n", obj_desc->common_field.node->name.ascii, obj_desc->common_field.access_byte_width, rgn_desc->region.node->name.ascii, rgn_desc->region.length)); } /* * Offset rounded up to next multiple of field width * exceeds region length, indicate an error */ ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n", obj_desc->common_field.node->name.ascii, obj_desc->common_field.base_byte_offset, field_datum_byte_offset, obj_desc->common_field.access_byte_width, rgn_desc->region.node->name.ascii, rgn_desc->region.length)); #ifdef CONFIG_ACPI_RELAXED_AML { /* * Allow access to the field if it is within the region size * rounded up to a multiple of the access byte width. This * overcomes "off-by-one" programming errors in the AML often * found in Toshiba laptops. These errors were allowed by * the Microsoft ASL compiler. */ u32 rounded_length = ACPI_ROUND_UP(rgn_desc->region.length, obj_desc->common_field.access_byte_width); if (rounded_length < (obj_desc->common_field.base_byte_offset + field_datum_byte_offset + obj_desc->common_field.access_byte_width)) { return_ACPI_STATUS (AE_AML_REGION_LIMIT); } else { static int warn_once = 1; if (warn_once) { // Could also associate a flag with each field, and // warn once for each field. ACPI_REPORT_WARNING(( "The ACPI AML in your computer contains errors, " "please nag the manufacturer to correct it.\n")); ACPI_REPORT_WARNING(( "Allowing relaxed access to fields; " "turn on CONFIG_ACPI_DEBUG for details.\n")); warn_once = 0; } return_ACPI_STATUS (AE_OK); } } #else return_ACPI_STATUS (AE_AML_REGION_LIMIT); #endif } return_ACPI_STATUS (AE_OK); }
ACPI_STATUS AcpiHwWrite ( UINT32 Value, ACPI_GENERIC_ADDRESS *Reg) { UINT64 Address; UINT8 AccessWidth; UINT32 BitWidth; UINT8 BitOffset; UINT64 Value64; UINT32 NewValue32, OldValue32; UINT8 Index; ACPI_STATUS Status; ACPI_FUNCTION_NAME (HwWrite); /* Validate contents of the GAS register */ Status = AcpiHwValidateRegister (Reg, 32, &Address); if (ACPI_FAILURE (Status)) { return (Status); } /* Convert AccessWidth into number of bits based */ AccessWidth = Reg->AccessWidth ? Reg->AccessWidth : 1; AccessWidth = 1 << (AccessWidth + 2); BitWidth = ACPI_ROUND_UP (Reg->BitOffset + Reg->BitWidth, AccessWidth); BitOffset = Reg->BitOffset; /* * Two address spaces supported: Memory or IO. PCI_Config is * not supported here because the GAS structure is insufficient */ Index = 0; while (BitWidth) { NewValue32 = ACPI_GET_BITS (&Value, (Index * AccessWidth), ((1 << AccessWidth) - 1)); if (BitOffset > AccessWidth) { BitOffset -= AccessWidth; } else { if (BitOffset) { NewValue32 &= ACPI_MASK_BITS_BELOW (BitOffset); } if (BitWidth < AccessWidth) { NewValue32 &= ACPI_MASK_BITS_ABOVE (BitWidth); } if (Reg->SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY) { if (BitOffset || BitWidth < AccessWidth) { /* * Read old values in order not to modify the bits that * are beyond the register BitWidth/BitOffset setting. */ Status = AcpiOsReadMemory ((ACPI_PHYSICAL_ADDRESS) Address + Index * ACPI_DIV_8 (AccessWidth), &Value64, AccessWidth); OldValue32 = (UINT32) Value64; if (BitOffset) { OldValue32 &= ACPI_MASK_BITS_ABOVE (BitOffset + 1); BitOffset = 0; } if (BitWidth < AccessWidth) { OldValue32 &= ACPI_MASK_BITS_BELOW (BitWidth - 1); } NewValue32 |= OldValue32; } Value64 = (UINT64) NewValue32; Status = AcpiOsWriteMemory ((ACPI_PHYSICAL_ADDRESS) Address + Index * ACPI_DIV_8 (AccessWidth), Value64, AccessWidth); } else /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */ { if (BitOffset || BitWidth < AccessWidth) { /* * Read old values in order not to modify the bits that * are beyond the register BitWidth/BitOffset setting. */ Status = AcpiHwReadPort ((ACPI_IO_ADDRESS) Address + Index * ACPI_DIV_8 (AccessWidth), &OldValue32, AccessWidth); if (BitOffset) { OldValue32 &= ACPI_MASK_BITS_ABOVE (BitOffset + 1); BitOffset = 0; } if (BitWidth < AccessWidth) { OldValue32 &= ACPI_MASK_BITS_BELOW (BitWidth - 1); } NewValue32 |= OldValue32; } Status = AcpiHwWritePort ((ACPI_IO_ADDRESS) Address + Index * ACPI_DIV_8 (AccessWidth), NewValue32, AccessWidth); } } BitWidth -= BitWidth > AccessWidth ? AccessWidth : BitWidth; Index++; } ACPI_DEBUG_PRINT ((ACPI_DB_IO, "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n", Value, AccessWidth, ACPI_FORMAT_UINT64 (Address), AcpiUtGetRegionName (Reg->SpaceId))); return (Status); }
ACPI_STATUS AcpiHwRead ( UINT32 *Value, ACPI_GENERIC_ADDRESS *Reg) { UINT64 Address; UINT8 AccessWidth; UINT32 BitWidth; UINT8 BitOffset; UINT64 Value64; UINT32 Value32; UINT8 Index; ACPI_STATUS Status; ACPI_FUNCTION_NAME (HwRead); /* Validate contents of the GAS register */ Status = AcpiHwValidateRegister (Reg, 32, &Address); if (ACPI_FAILURE (Status)) { return (Status); } /* * Initialize entire 32-bit return value to zero, convert AccessWidth * into number of bits based */ *Value = 0; AccessWidth = Reg->AccessWidth ? Reg->AccessWidth : 1; AccessWidth = 1 << (AccessWidth + 2); BitWidth = ACPI_ROUND_UP (Reg->BitOffset + Reg->BitWidth, AccessWidth); BitOffset = Reg->BitOffset; /* * Two address spaces supported: Memory or IO. PCI_Config is * not supported here because the GAS structure is insufficient */ Index = 0; while (BitWidth) { if (BitOffset > AccessWidth) { Value32 = 0; BitOffset -= AccessWidth; } else { if (Reg->SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY) { Status = AcpiOsReadMemory ((ACPI_PHYSICAL_ADDRESS) Address + Index * ACPI_DIV_8 (AccessWidth), &Value64, AccessWidth); Value32 = (UINT32) Value64; } else /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */ { Status = AcpiHwReadPort ((ACPI_IO_ADDRESS) Address + Index * ACPI_DIV_8 (AccessWidth), &Value32, AccessWidth); } if (BitOffset) { Value32 &= ACPI_MASK_BITS_BELOW (BitOffset); BitOffset = 0; } if (BitWidth < AccessWidth) { Value32 &= ACPI_MASK_BITS_ABOVE (BitWidth); } } ACPI_SET_BITS (Value, Index * AccessWidth, ((1 << AccessWidth) - 1), Value32); BitWidth -= BitWidth > AccessWidth ? AccessWidth : BitWidth; Index++; } ACPI_DEBUG_PRINT ((ACPI_DB_IO, "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n", *Value, AccessWidth, ACPI_FORMAT_UINT64 (Address), AcpiUtGetRegionName (Reg->SpaceId))); return (Status); }
ACPI_STATUS AcpiHwValidateRegister ( ACPI_GENERIC_ADDRESS *Reg, UINT8 MaxBitWidth, UINT64 *Address) { UINT8 BitWidth; UINT8 AccessWidth; /* Must have a valid pointer to a GAS structure */ if (!Reg) { return (AE_BAD_PARAMETER); } /* * Copy the target address. This handles possible alignment issues. * Address must not be null. A null address also indicates an optional * ACPI register that is not supported, so no error message. */ ACPI_MOVE_64_TO_64 (Address, &Reg->Address); if (!(*Address)) { return (AE_BAD_ADDRESS); } /* Validate the SpaceID */ if ((Reg->SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) && (Reg->SpaceId != ACPI_ADR_SPACE_SYSTEM_IO)) { ACPI_ERROR ((AE_INFO, "Unsupported address space: 0x%X", Reg->SpaceId)); return (AE_SUPPORT); } /* Validate the AccessWidth */ if (Reg->AccessWidth > 4) { ACPI_ERROR ((AE_INFO, "Unsupported register access width: 0x%X", Reg->AccessWidth)); return (AE_SUPPORT); } /* Validate the BitWidth, convert AccessWidth into number of bits */ AccessWidth = Reg->AccessWidth ? Reg->AccessWidth : 1; AccessWidth = 1 << (AccessWidth + 2); BitWidth = ACPI_ROUND_UP (Reg->BitOffset + Reg->BitWidth, AccessWidth); if (MaxBitWidth < BitWidth) { ACPI_WARNING ((AE_INFO, "Requested bit width 0x%X is smaller than register bit width 0x%X", MaxBitWidth, BitWidth)); return (AE_SUPPORT); } return (AE_OK); }
acpi_status acpi_ex_setup_region ( union acpi_operand_object *obj_desc, u32 field_datum_byte_offset) { acpi_status status = AE_OK; union acpi_operand_object *rgn_desc; ACPI_FUNCTION_TRACE_U32 ("ex_setup_region", field_datum_byte_offset); rgn_desc = obj_desc->common_field.region_obj; /* We must have a valid region */ if (ACPI_GET_OBJECT_TYPE (rgn_desc) != ACPI_TYPE_REGION) { ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %X (%s)\n", ACPI_GET_OBJECT_TYPE (rgn_desc), acpi_ut_get_object_type_name (rgn_desc))); return_ACPI_STATUS (AE_AML_OPERAND_TYPE); } /* * 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)) { status = acpi_ds_get_region_arguments (rgn_desc); if (ACPI_FAILURE (status)) { return_ACPI_STATUS (status); } } if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS) { /* SMBus has a non-linear address space */ return_ACPI_STATUS (AE_OK); } #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)) { if (ACPI_FAILURE (status)) { return_ACPI_STATUS (status); } } #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) */ if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset + field_datum_byte_offset + obj_desc->common_field.access_byte_width)) { 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 * access width boundary. */ if (ACPI_ROUND_UP (rgn_desc->region.length, obj_desc->common_field.access_byte_width) >= (obj_desc->common_field.base_byte_offset + (acpi_native_uint) obj_desc->common_field.access_byte_width + field_datum_byte_offset)) { return_ACPI_STATUS (AE_OK); } } if (rgn_desc->region.length < obj_desc->common_field.access_byte_width) { /* * 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. */ ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n", 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)); } /* * Offset rounded up to next multiple of field width * exceeds region length, indicate an error */ ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n", 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)); return_ACPI_STATUS (AE_AML_REGION_LIMIT); } return_ACPI_STATUS (AE_OK); }