Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: evgpeinit - System GPE initialization and update
0005  *
0006  * Copyright (C) 2000 - 2022, Intel Corp.
0007  *
0008  *****************************************************************************/
0009 
0010 #include <acpi/acpi.h>
0011 #include "accommon.h"
0012 #include "acevents.h"
0013 #include "acnamesp.h"
0014 
0015 #define _COMPONENT          ACPI_EVENTS
0016 ACPI_MODULE_NAME("evgpeinit")
0017 #if (!ACPI_REDUCED_HARDWARE)    /* Entire module */
0018 /*
0019  * Note: History of _PRW support in ACPICA
0020  *
0021  * Originally (2000 - 2010), the GPE initialization code performed a walk of
0022  * the entire namespace to execute the _PRW methods and detect all GPEs
0023  * capable of waking the system.
0024  *
0025  * As of 10/2010, the _PRW method execution has been removed since it is
0026  * actually unnecessary. The host OS must in fact execute all _PRW methods
0027  * in order to identify the device/power-resource dependencies. We now put
0028  * the onus on the host OS to identify the wake GPEs as part of this process
0029  * and to inform ACPICA of these GPEs via the acpi_setup_gpe_for_wake interface. This
0030  * not only reduces the complexity of the ACPICA initialization code, but in
0031  * some cases (on systems with very large namespaces) it should reduce the
0032  * kernel boot time as well.
0033  */
0034 
0035 #ifdef ACPI_GPE_USE_LOGICAL_ADDRESSES
0036 #define ACPI_FADT_GPE_BLOCK_ADDRESS(N)  \
0037     acpi_gbl_FADT.xgpe##N##_block.space_id == \
0038                     ACPI_ADR_SPACE_SYSTEM_MEMORY ? \
0039         (u64)acpi_gbl_xgpe##N##_block_logical_address : \
0040         acpi_gbl_FADT.xgpe##N##_block.address
0041 #else
0042 #define ACPI_FADT_GPE_BLOCK_ADDRESS(N)  acpi_gbl_FADT.xgpe##N##_block.address
0043 #endif      /* ACPI_GPE_USE_LOGICAL_ADDRESSES */
0044 
0045 /*******************************************************************************
0046  *
0047  * FUNCTION:    acpi_ev_gpe_initialize
0048  *
0049  * PARAMETERS:  None
0050  *
0051  * RETURN:      Status
0052  *
0053  * DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks
0054  *
0055  ******************************************************************************/
0056 acpi_status acpi_ev_gpe_initialize(void)
0057 {
0058     u32 register_count0 = 0;
0059     u32 register_count1 = 0;
0060     u32 gpe_number_max = 0;
0061     acpi_status status;
0062     u64 address;
0063 
0064     ACPI_FUNCTION_TRACE(ev_gpe_initialize);
0065 
0066     ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
0067                   "Initializing General Purpose Events (GPEs):\n"));
0068 
0069     status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
0070     if (ACPI_FAILURE(status)) {
0071         return_ACPI_STATUS(status);
0072     }
0073 
0074     /*
0075      * Initialize the GPE Block(s) defined in the FADT
0076      *
0077      * Why the GPE register block lengths are divided by 2:  From the ACPI
0078      * Spec, section "General-Purpose Event Registers", we have:
0079      *
0080      * "Each register block contains two registers of equal length
0081      *  GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
0082      *  GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
0083      *  The length of the GPE1_STS and GPE1_EN registers is equal to
0084      *  half the GPE1_LEN. If a generic register block is not supported
0085      *  then its respective block pointer and block length values in the
0086      *  FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
0087      *  to be the same size."
0088      */
0089 
0090     /*
0091      * Determine the maximum GPE number for this machine.
0092      *
0093      * Note: both GPE0 and GPE1 are optional, and either can exist without
0094      * the other.
0095      *
0096      * If EITHER the register length OR the block address are zero, then that
0097      * particular block is not supported.
0098      */
0099     address = ACPI_FADT_GPE_BLOCK_ADDRESS(0);
0100 
0101     if (acpi_gbl_FADT.gpe0_block_length && address) {
0102 
0103         /* GPE block 0 exists (has both length and address > 0) */
0104 
0105         register_count0 = (u16)(acpi_gbl_FADT.gpe0_block_length / 2);
0106         gpe_number_max =
0107             (register_count0 * ACPI_GPE_REGISTER_WIDTH) - 1;
0108 
0109         /* Install GPE Block 0 */
0110 
0111         status = acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
0112                           address,
0113                           acpi_gbl_FADT.xgpe0_block.
0114                           space_id, register_count0, 0,
0115                           acpi_gbl_FADT.sci_interrupt,
0116                           &acpi_gbl_gpe_fadt_blocks[0]);
0117 
0118         if (ACPI_FAILURE(status)) {
0119             ACPI_EXCEPTION((AE_INFO, status,
0120                     "Could not create GPE Block 0"));
0121         }
0122     }
0123 
0124     address = ACPI_FADT_GPE_BLOCK_ADDRESS(1);
0125 
0126     if (acpi_gbl_FADT.gpe1_block_length && address) {
0127 
0128         /* GPE block 1 exists (has both length and address > 0) */
0129 
0130         register_count1 = (u16)(acpi_gbl_FADT.gpe1_block_length / 2);
0131 
0132         /* Check for GPE0/GPE1 overlap (if both banks exist) */
0133 
0134         if ((register_count0) &&
0135             (gpe_number_max >= acpi_gbl_FADT.gpe1_base)) {
0136             ACPI_ERROR((AE_INFO,
0137                     "GPE0 block (GPE 0 to %u) overlaps the GPE1 block "
0138                     "(GPE %u to %u) - Ignoring GPE1",
0139                     gpe_number_max, acpi_gbl_FADT.gpe1_base,
0140                     acpi_gbl_FADT.gpe1_base +
0141                     ((register_count1 *
0142                       ACPI_GPE_REGISTER_WIDTH) - 1)));
0143 
0144             /* Ignore GPE1 block by setting the register count to zero */
0145 
0146             register_count1 = 0;
0147         } else {
0148             /* Install GPE Block 1 */
0149 
0150             status =
0151                 acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
0152                              address,
0153                              acpi_gbl_FADT.xgpe1_block.
0154                              space_id, register_count1,
0155                              acpi_gbl_FADT.gpe1_base,
0156                              acpi_gbl_FADT.
0157                              sci_interrupt,
0158                              &acpi_gbl_gpe_fadt_blocks
0159                              [1]);
0160 
0161             if (ACPI_FAILURE(status)) {
0162                 ACPI_EXCEPTION((AE_INFO, status,
0163                         "Could not create GPE Block 1"));
0164             }
0165 
0166             /*
0167              * GPE0 and GPE1 do not have to be contiguous in the GPE number
0168              * space. However, GPE0 always starts at GPE number zero.
0169              */
0170         }
0171     }
0172 
0173     /* Exit if there are no GPE registers */
0174 
0175     if ((register_count0 + register_count1) == 0) {
0176 
0177         /* GPEs are not required by ACPI, this is OK */
0178 
0179         ACPI_DEBUG_PRINT((ACPI_DB_INIT,
0180                   "There are no GPE blocks defined in the FADT\n"));
0181         goto cleanup;
0182     }
0183 
0184 cleanup:
0185     (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
0186     return_ACPI_STATUS(AE_OK);
0187 }
0188 
0189 /*******************************************************************************
0190  *
0191  * FUNCTION:    acpi_ev_update_gpes
0192  *
0193  * PARAMETERS:  table_owner_id      - ID of the newly-loaded ACPI table
0194  *
0195  * RETURN:      None
0196  *
0197  * DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a
0198  *              result of a Load() or load_table() operation. If new GPE
0199  *              methods have been installed, register the new methods.
0200  *
0201  ******************************************************************************/
0202 
0203 void acpi_ev_update_gpes(acpi_owner_id table_owner_id)
0204 {
0205     struct acpi_gpe_xrupt_info *gpe_xrupt_info;
0206     struct acpi_gpe_block_info *gpe_block;
0207     struct acpi_gpe_walk_info walk_info;
0208     acpi_status status = AE_OK;
0209 
0210     /*
0211      * Find any _Lxx/_Exx GPE methods that have just been loaded.
0212      *
0213      * Any GPEs that correspond to new _Lxx/_Exx methods are immediately
0214      * enabled.
0215      *
0216      * Examine the namespace underneath each gpe_device within the
0217      * gpe_block lists.
0218      */
0219     status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
0220     if (ACPI_FAILURE(status)) {
0221         return;
0222     }
0223 
0224     walk_info.count = 0;
0225     walk_info.owner_id = table_owner_id;
0226     walk_info.execute_by_owner_id = TRUE;
0227 
0228     /* Walk the interrupt level descriptor list */
0229 
0230     gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
0231     while (gpe_xrupt_info) {
0232 
0233         /* Walk all Gpe Blocks attached to this interrupt level */
0234 
0235         gpe_block = gpe_xrupt_info->gpe_block_list_head;
0236         while (gpe_block) {
0237             walk_info.gpe_block = gpe_block;
0238             walk_info.gpe_device = gpe_block->node;
0239 
0240             status = acpi_ns_walk_namespace(ACPI_TYPE_METHOD,
0241                             walk_info.gpe_device,
0242                             ACPI_UINT32_MAX,
0243                             ACPI_NS_WALK_NO_UNLOCK,
0244                             acpi_ev_match_gpe_method,
0245                             NULL, &walk_info, NULL);
0246             if (ACPI_FAILURE(status)) {
0247                 ACPI_EXCEPTION((AE_INFO, status,
0248                         "While decoding _Lxx/_Exx methods"));
0249             }
0250 
0251             gpe_block = gpe_block->next;
0252         }
0253 
0254         gpe_xrupt_info = gpe_xrupt_info->next;
0255     }
0256 
0257     if (walk_info.count) {
0258         ACPI_INFO(("Enabled %u new GPEs", walk_info.count));
0259     }
0260 
0261     (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
0262     return;
0263 }
0264 
0265 /*******************************************************************************
0266  *
0267  * FUNCTION:    acpi_ev_match_gpe_method
0268  *
0269  * PARAMETERS:  Callback from walk_namespace
0270  *
0271  * RETURN:      Status
0272  *
0273  * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
0274  *              control method under the _GPE portion of the namespace.
0275  *              Extract the name and GPE type from the object, saving this
0276  *              information for quick lookup during GPE dispatch. Allows a
0277  *              per-owner_id evaluation if execute_by_owner_id is TRUE in the
0278  *              walk_info parameter block.
0279  *
0280  *              The name of each GPE control method is of the form:
0281  *              "_Lxx" or "_Exx", where:
0282  *                  L      - means that the GPE is level triggered
0283  *                  E      - means that the GPE is edge triggered
0284  *                  xx     - is the GPE number [in HEX]
0285  *
0286  * If walk_info->execute_by_owner_id is TRUE, we only execute examine GPE methods
0287  * with that owner.
0288  *
0289  ******************************************************************************/
0290 
0291 acpi_status
0292 acpi_ev_match_gpe_method(acpi_handle obj_handle,
0293              u32 level, void *context, void **return_value)
0294 {
0295     struct acpi_namespace_node *method_node =
0296         ACPI_CAST_PTR(struct acpi_namespace_node, obj_handle);
0297     struct acpi_gpe_walk_info *walk_info =
0298         ACPI_CAST_PTR(struct acpi_gpe_walk_info, context);
0299     struct acpi_gpe_event_info *gpe_event_info;
0300     acpi_status status;
0301     u32 gpe_number;
0302     u8 temp_gpe_number;
0303     char name[ACPI_NAMESEG_SIZE + 1];
0304     u8 type;
0305 
0306     ACPI_FUNCTION_TRACE(ev_match_gpe_method);
0307 
0308     /* Check if requested owner_id matches this owner_id */
0309 
0310     if ((walk_info->execute_by_owner_id) &&
0311         (method_node->owner_id != walk_info->owner_id)) {
0312         return_ACPI_STATUS(AE_OK);
0313     }
0314 
0315     /*
0316      * Match and decode the _Lxx and _Exx GPE method names
0317      *
0318      * 1) Extract the method name and null terminate it
0319      */
0320     ACPI_MOVE_32_TO_32(name, &method_node->name.integer);
0321     name[ACPI_NAMESEG_SIZE] = 0;
0322 
0323     /* 2) Name must begin with an underscore */
0324 
0325     if (name[0] != '_') {
0326         return_ACPI_STATUS(AE_OK);  /* Ignore this method */
0327     }
0328 
0329     /*
0330      * 3) Edge/Level determination is based on the 2nd character
0331      *    of the method name
0332      */
0333     switch (name[1]) {
0334     case 'L':
0335 
0336         type = ACPI_GPE_LEVEL_TRIGGERED;
0337         break;
0338 
0339     case 'E':
0340 
0341         type = ACPI_GPE_EDGE_TRIGGERED;
0342         break;
0343 
0344     default:
0345 
0346         /* Unknown method type, just ignore it */
0347 
0348         ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
0349                   "Ignoring unknown GPE method type: %s "
0350                   "(name not of form _Lxx or _Exx)", name));
0351         return_ACPI_STATUS(AE_OK);
0352     }
0353 
0354     /* 4) The last two characters of the name are the hex GPE Number */
0355 
0356     status = acpi_ut_ascii_to_hex_byte(&name[2], &temp_gpe_number);
0357     if (ACPI_FAILURE(status)) {
0358 
0359         /* Conversion failed; invalid method, just ignore it */
0360 
0361         ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
0362                   "Could not extract GPE number from name: %s "
0363                   "(name is not of form _Lxx or _Exx)", name));
0364         return_ACPI_STATUS(AE_OK);
0365     }
0366 
0367     /* Ensure that we have a valid GPE number for this GPE block */
0368 
0369     gpe_number = (u32)temp_gpe_number;
0370     gpe_event_info =
0371         acpi_ev_low_get_gpe_info(gpe_number, walk_info->gpe_block);
0372     if (!gpe_event_info) {
0373         /*
0374          * This gpe_number is not valid for this GPE block, just ignore it.
0375          * However, it may be valid for a different GPE block, since GPE0
0376          * and GPE1 methods both appear under \_GPE.
0377          */
0378         return_ACPI_STATUS(AE_OK);
0379     }
0380 
0381     if ((ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
0382          ACPI_GPE_DISPATCH_HANDLER) ||
0383         (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
0384          ACPI_GPE_DISPATCH_RAW_HANDLER)) {
0385 
0386         /* If there is already a handler, ignore this GPE method */
0387 
0388         return_ACPI_STATUS(AE_OK);
0389     }
0390 
0391     if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
0392         ACPI_GPE_DISPATCH_METHOD) {
0393         /*
0394          * If there is already a method, ignore this method. But check
0395          * for a type mismatch (if both the _Lxx AND _Exx exist)
0396          */
0397         if (type != (gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK)) {
0398             ACPI_ERROR((AE_INFO,
0399                     "For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",
0400                     gpe_number, gpe_number, gpe_number));
0401         }
0402         return_ACPI_STATUS(AE_OK);
0403     }
0404 
0405     /* Disable the GPE in case it's been enabled already. */
0406 
0407     (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
0408 
0409     /*
0410      * Add the GPE information from above to the gpe_event_info block for
0411      * use during dispatch of this GPE.
0412      */
0413     gpe_event_info->flags &= ~(ACPI_GPE_DISPATCH_MASK);
0414     gpe_event_info->flags |= (u8)(type | ACPI_GPE_DISPATCH_METHOD);
0415     gpe_event_info->dispatch.method_node = method_node;
0416 
0417     ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
0418               "Registered GPE method %s as GPE number 0x%.2X\n",
0419               name, gpe_number));
0420     return_ACPI_STATUS(AE_OK);
0421 }
0422 
0423 #endif              /* !ACPI_REDUCED_HARDWARE */