Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: hwxface - Public ACPICA hardware interfaces
0005  *
0006  * Copyright (C) 2000 - 2022, Intel Corp.
0007  *
0008  *****************************************************************************/
0009 
0010 #define EXPORT_ACPI_INTERFACES
0011 
0012 #include <acpi/acpi.h>
0013 #include "accommon.h"
0014 #include "acnamesp.h"
0015 
0016 #define _COMPONENT          ACPI_HARDWARE
0017 ACPI_MODULE_NAME("hwxface")
0018 
0019 /******************************************************************************
0020  *
0021  * FUNCTION:    acpi_reset
0022  *
0023  * PARAMETERS:  None
0024  *
0025  * RETURN:      Status
0026  *
0027  * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
0028  *              support reset register in PCI config space, this must be
0029  *              handled separately.
0030  *
0031  ******************************************************************************/
0032 acpi_status acpi_reset(void)
0033 {
0034     struct acpi_generic_address *reset_reg;
0035     acpi_status status;
0036 
0037     ACPI_FUNCTION_TRACE(acpi_reset);
0038 
0039     reset_reg = &acpi_gbl_FADT.reset_register;
0040 
0041     /* Check if the reset register is supported */
0042 
0043     if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) ||
0044         !reset_reg->address) {
0045         return_ACPI_STATUS(AE_NOT_EXIST);
0046     }
0047 
0048     if (reset_reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
0049         /*
0050          * For I/O space, write directly to the OSL. This bypasses the port
0051          * validation mechanism, which may block a valid write to the reset
0052          * register.
0053          *
0054          * NOTE:
0055          * The ACPI spec requires the reset register width to be 8, so we
0056          * hardcode it here and ignore the FADT value. This maintains
0057          * compatibility with other ACPI implementations that have allowed
0058          * BIOS code with bad register width values to go unnoticed.
0059          */
0060         status = acpi_os_write_port((acpi_io_address)reset_reg->address,
0061                         acpi_gbl_FADT.reset_value,
0062                         ACPI_RESET_REGISTER_WIDTH);
0063     } else {
0064         /* Write the reset value to the reset register */
0065 
0066         status = acpi_hw_write(acpi_gbl_FADT.reset_value, reset_reg);
0067     }
0068 
0069     return_ACPI_STATUS(status);
0070 }
0071 
0072 ACPI_EXPORT_SYMBOL(acpi_reset)
0073 
0074 /******************************************************************************
0075  *
0076  * FUNCTION:    acpi_read
0077  *
0078  * PARAMETERS:  value               - Where the value is returned
0079  *              reg                 - GAS register structure
0080  *
0081  * RETURN:      Status
0082  *
0083  * DESCRIPTION: Read from either memory or IO space.
0084  *
0085  * LIMITATIONS: <These limitations also apply to acpi_write>
0086  *      bit_width must be exactly 8, 16, 32, or 64.
0087  *      space_ID must be system_memory or system_IO.
0088  *      bit_offset and access_width are currently ignored, as there has
0089  *          not been a need to implement these.
0090  *
0091  ******************************************************************************/
0092 acpi_status acpi_read(u64 *return_value, struct acpi_generic_address *reg)
0093 {
0094     acpi_status status;
0095 
0096     ACPI_FUNCTION_NAME(acpi_read);
0097 
0098     status = acpi_hw_read(return_value, reg);
0099     return (status);
0100 }
0101 
0102 ACPI_EXPORT_SYMBOL(acpi_read)
0103 
0104 /******************************************************************************
0105  *
0106  * FUNCTION:    acpi_write
0107  *
0108  * PARAMETERS:  value               - Value to be written
0109  *              reg                 - GAS register structure
0110  *
0111  * RETURN:      Status
0112  *
0113  * DESCRIPTION: Write to either memory or IO space.
0114  *
0115  ******************************************************************************/
0116 acpi_status acpi_write(u64 value, struct acpi_generic_address *reg)
0117 {
0118     acpi_status status;
0119 
0120     ACPI_FUNCTION_NAME(acpi_write);
0121 
0122     status = acpi_hw_write(value, reg);
0123     return (status);
0124 }
0125 
0126 ACPI_EXPORT_SYMBOL(acpi_write)
0127 
0128 #if (!ACPI_REDUCED_HARDWARE)
0129 /*******************************************************************************
0130  *
0131  * FUNCTION:    acpi_read_bit_register
0132  *
0133  * PARAMETERS:  register_id     - ID of ACPI Bit Register to access
0134  *              return_value    - Value that was read from the register,
0135  *                                normalized to bit position zero.
0136  *
0137  * RETURN:      Status and the value read from the specified Register. Value
0138  *              returned is normalized to bit0 (is shifted all the way right)
0139  *
0140  * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
0141  *
0142  * SUPPORTS:    Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
0143  *              PM2 Control.
0144  *
0145  * Note: The hardware lock is not required when reading the ACPI bit registers
0146  *       since almost all of them are single bit and it does not matter that
0147  *       the parent hardware register can be split across two physical
0148  *       registers. The only multi-bit field is SLP_TYP in the PM1 control
0149  *       register, but this field does not cross an 8-bit boundary (nor does
0150  *       it make much sense to actually read this field.)
0151  *
0152  ******************************************************************************/
0153 acpi_status acpi_read_bit_register(u32 register_id, u32 *return_value)
0154 {
0155     struct acpi_bit_register_info *bit_reg_info;
0156     u32 register_value;
0157     u32 value;
0158     acpi_status status;
0159 
0160     ACPI_FUNCTION_TRACE_U32(acpi_read_bit_register, register_id);
0161 
0162     /* Get the info structure corresponding to the requested ACPI Register */
0163 
0164     bit_reg_info = acpi_hw_get_bit_register_info(register_id);
0165     if (!bit_reg_info) {
0166         return_ACPI_STATUS(AE_BAD_PARAMETER);
0167     }
0168 
0169     /* Read the entire parent register */
0170 
0171     status = acpi_hw_register_read(bit_reg_info->parent_register,
0172                        &register_value);
0173     if (ACPI_FAILURE(status)) {
0174         return_ACPI_STATUS(status);
0175     }
0176 
0177     /* Normalize the value that was read, mask off other bits */
0178 
0179     value = ((register_value & bit_reg_info->access_bit_mask)
0180          >> bit_reg_info->bit_position);
0181 
0182     ACPI_DEBUG_PRINT((ACPI_DB_IO,
0183               "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
0184               register_id, bit_reg_info->parent_register,
0185               register_value, value));
0186 
0187     *return_value = value;
0188     return_ACPI_STATUS(AE_OK);
0189 }
0190 
0191 ACPI_EXPORT_SYMBOL(acpi_read_bit_register)
0192 
0193 /*******************************************************************************
0194  *
0195  * FUNCTION:    acpi_write_bit_register
0196  *
0197  * PARAMETERS:  register_id     - ID of ACPI Bit Register to access
0198  *              value           - Value to write to the register, in bit
0199  *                                position zero. The bit is automatically
0200  *                                shifted to the correct position.
0201  *
0202  * RETURN:      Status
0203  *
0204  * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
0205  *              since most operations require a read/modify/write sequence.
0206  *
0207  * SUPPORTS:    Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
0208  *              PM2 Control.
0209  *
0210  * Note that at this level, the fact that there may be actually two
0211  * hardware registers (A and B - and B may not exist) is abstracted.
0212  *
0213  ******************************************************************************/
0214 acpi_status acpi_write_bit_register(u32 register_id, u32 value)
0215 {
0216     struct acpi_bit_register_info *bit_reg_info;
0217     acpi_cpu_flags lock_flags;
0218     u32 register_value;
0219     acpi_status status = AE_OK;
0220 
0221     ACPI_FUNCTION_TRACE_U32(acpi_write_bit_register, register_id);
0222 
0223     /* Get the info structure corresponding to the requested ACPI Register */
0224 
0225     bit_reg_info = acpi_hw_get_bit_register_info(register_id);
0226     if (!bit_reg_info) {
0227         return_ACPI_STATUS(AE_BAD_PARAMETER);
0228     }
0229 
0230     lock_flags = acpi_os_acquire_raw_lock(acpi_gbl_hardware_lock);
0231 
0232     /*
0233      * At this point, we know that the parent register is one of the
0234      * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
0235      */
0236     if (bit_reg_info->parent_register != ACPI_REGISTER_PM1_STATUS) {
0237         /*
0238          * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
0239          *
0240          * Perform a register read to preserve the bits that we are not
0241          * interested in
0242          */
0243         status = acpi_hw_register_read(bit_reg_info->parent_register,
0244                            &register_value);
0245         if (ACPI_FAILURE(status)) {
0246             goto unlock_and_exit;
0247         }
0248 
0249         /*
0250          * Insert the input bit into the value that was just read
0251          * and write the register
0252          */
0253         ACPI_REGISTER_INSERT_VALUE(register_value,
0254                        bit_reg_info->bit_position,
0255                        bit_reg_info->access_bit_mask,
0256                        value);
0257 
0258         status = acpi_hw_register_write(bit_reg_info->parent_register,
0259                         register_value);
0260     } else {
0261         /*
0262          * 2) Case for PM1 Status
0263          *
0264          * The Status register is different from the rest. Clear an event
0265          * by writing 1, writing 0 has no effect. So, the only relevant
0266          * information is the single bit we're interested in, all others
0267          * should be written as 0 so they will be left unchanged.
0268          */
0269         register_value = ACPI_REGISTER_PREPARE_BITS(value,
0270                                 bit_reg_info->
0271                                 bit_position,
0272                                 bit_reg_info->
0273                                 access_bit_mask);
0274 
0275         /* No need to write the register if value is all zeros */
0276 
0277         if (register_value) {
0278             status =
0279                 acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
0280                            register_value);
0281         }
0282     }
0283 
0284     ACPI_DEBUG_PRINT((ACPI_DB_IO,
0285               "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
0286               register_id, bit_reg_info->parent_register, value,
0287               register_value));
0288 
0289 unlock_and_exit:
0290 
0291     acpi_os_release_raw_lock(acpi_gbl_hardware_lock, lock_flags);
0292     return_ACPI_STATUS(status);
0293 }
0294 
0295 ACPI_EXPORT_SYMBOL(acpi_write_bit_register)
0296 #endif              /* !ACPI_REDUCED_HARDWARE */
0297 /*******************************************************************************
0298  *
0299  * FUNCTION:    acpi_get_sleep_type_data
0300  *
0301  * PARAMETERS:  sleep_state         - Numeric sleep state
0302  *              *sleep_type_a        - Where SLP_TYPa is returned
0303  *              *sleep_type_b        - Where SLP_TYPb is returned
0304  *
0305  * RETURN:      Status
0306  *
0307  * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested
0308  *              sleep state via the appropriate \_Sx object.
0309  *
0310  *  The sleep state package returned from the corresponding \_Sx_ object
0311  *  must contain at least one integer.
0312  *
0313  *  March 2005:
0314  *  Added support for a package that contains two integers. This
0315  *  goes against the ACPI specification which defines this object as a
0316  *  package with one encoded DWORD integer. However, existing practice
0317  *  by many BIOS vendors is to return a package with 2 or more integer
0318  *  elements, at least one per sleep type (A/B).
0319  *
0320  *  January 2013:
0321  *  Therefore, we must be prepared to accept a package with either a
0322  *  single integer or multiple integers.
0323  *
0324  *  The single integer DWORD format is as follows:
0325  *      BYTE 0 - Value for the PM1A SLP_TYP register
0326  *      BYTE 1 - Value for the PM1B SLP_TYP register
0327  *      BYTE 2-3 - Reserved
0328  *
0329  *  The dual integer format is as follows:
0330  *      Integer 0 - Value for the PM1A SLP_TYP register
0331  *      Integer 1 - Value for the PM1A SLP_TYP register
0332  *
0333  ******************************************************************************/
0334 acpi_status
0335 acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
0336 {
0337     acpi_status status;
0338     struct acpi_evaluate_info *info;
0339     union acpi_operand_object **elements;
0340 
0341     ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
0342 
0343     /* Validate parameters */
0344 
0345     if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
0346         return_ACPI_STATUS(AE_BAD_PARAMETER);
0347     }
0348 
0349     /* Allocate the evaluation information block */
0350 
0351     info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
0352     if (!info) {
0353         return_ACPI_STATUS(AE_NO_MEMORY);
0354     }
0355 
0356     /*
0357      * Evaluate the \_Sx namespace object containing the register values
0358      * for this state
0359      */
0360     info->relative_pathname = acpi_gbl_sleep_state_names[sleep_state];
0361 
0362     status = acpi_ns_evaluate(info);
0363     if (ACPI_FAILURE(status)) {
0364         if (status == AE_NOT_FOUND) {
0365 
0366             /* The _Sx states are optional, ignore NOT_FOUND */
0367 
0368             goto final_cleanup;
0369         }
0370 
0371         goto warning_cleanup;
0372     }
0373 
0374     /* Must have a return object */
0375 
0376     if (!info->return_object) {
0377         ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
0378                 info->relative_pathname));
0379         status = AE_AML_NO_RETURN_VALUE;
0380         goto warning_cleanup;
0381     }
0382 
0383     /* Return object must be of type Package */
0384 
0385     if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
0386         ACPI_ERROR((AE_INFO,
0387                 "Sleep State return object is not a Package"));
0388         status = AE_AML_OPERAND_TYPE;
0389         goto return_value_cleanup;
0390     }
0391 
0392     /*
0393      * Any warnings about the package length or the object types have
0394      * already been issued by the predefined name module -- there is no
0395      * need to repeat them here.
0396      */
0397     elements = info->return_object->package.elements;
0398     switch (info->return_object->package.count) {
0399     case 0:
0400 
0401         status = AE_AML_PACKAGE_LIMIT;
0402         break;
0403 
0404     case 1:
0405 
0406         if (elements[0]->common.type != ACPI_TYPE_INTEGER) {
0407             status = AE_AML_OPERAND_TYPE;
0408             break;
0409         }
0410 
0411         /* A valid _Sx_ package with one integer */
0412 
0413         *sleep_type_a = (u8)elements[0]->integer.value;
0414         *sleep_type_b = (u8)(elements[0]->integer.value >> 8);
0415         break;
0416 
0417     case 2:
0418     default:
0419 
0420         if ((elements[0]->common.type != ACPI_TYPE_INTEGER) ||
0421             (elements[1]->common.type != ACPI_TYPE_INTEGER)) {
0422             status = AE_AML_OPERAND_TYPE;
0423             break;
0424         }
0425 
0426         /* A valid _Sx_ package with two integers */
0427 
0428         *sleep_type_a = (u8)elements[0]->integer.value;
0429         *sleep_type_b = (u8)elements[1]->integer.value;
0430         break;
0431     }
0432 
0433 return_value_cleanup:
0434     acpi_ut_remove_reference(info->return_object);
0435 
0436 warning_cleanup:
0437     if (ACPI_FAILURE(status)) {
0438         ACPI_EXCEPTION((AE_INFO, status,
0439                 "While evaluating Sleep State [%s]",
0440                 info->relative_pathname));
0441     }
0442 
0443 final_cleanup:
0444     ACPI_FREE(info);
0445     return_ACPI_STATUS(status);
0446 }
0447 
0448 ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)