1 /******************************************************************************
3 * Module Name: evgpeinit - System GPE initialization and update
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2010, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
50 #define _COMPONENT ACPI_EVENTS
51 ACPI_MODULE_NAME("evgpeinit")
53 /*******************************************************************************
55 * FUNCTION: acpi_ev_gpe_initialize
61 * DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks
63 ******************************************************************************/
64 acpi_status
acpi_ev_gpe_initialize(void)
66 u32 register_count0
= 0;
67 u32 register_count1
= 0;
68 u32 gpe_number_max
= 0;
71 ACPI_FUNCTION_TRACE(ev_gpe_initialize
);
73 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
74 if (ACPI_FAILURE(status
)) {
75 return_ACPI_STATUS(status
);
79 * Initialize the GPE Block(s) defined in the FADT
81 * Why the GPE register block lengths are divided by 2: From the ACPI
82 * Spec, section "General-Purpose Event Registers", we have:
84 * "Each register block contains two registers of equal length
85 * GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
86 * GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
87 * The length of the GPE1_STS and GPE1_EN registers is equal to
88 * half the GPE1_LEN. If a generic register block is not supported
89 * then its respective block pointer and block length values in the
90 * FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
91 * to be the same size."
95 * Determine the maximum GPE number for this machine.
97 * Note: both GPE0 and GPE1 are optional, and either can exist without
100 * If EITHER the register length OR the block address are zero, then that
101 * particular block is not supported.
103 if (acpi_gbl_FADT
.gpe0_block_length
&&
104 acpi_gbl_FADT
.xgpe0_block
.address
) {
106 /* GPE block 0 exists (has both length and address > 0) */
108 register_count0
= (u16
)(acpi_gbl_FADT
.gpe0_block_length
/ 2);
111 (register_count0
* ACPI_GPE_REGISTER_WIDTH
) - 1;
113 /* Install GPE Block 0 */
115 status
= acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device
,
116 &acpi_gbl_FADT
.xgpe0_block
,
118 acpi_gbl_FADT
.sci_interrupt
,
119 &acpi_gbl_gpe_fadt_blocks
[0]);
121 if (ACPI_FAILURE(status
)) {
122 ACPI_EXCEPTION((AE_INFO
, status
,
123 "Could not create GPE Block 0"));
127 if (acpi_gbl_FADT
.gpe1_block_length
&&
128 acpi_gbl_FADT
.xgpe1_block
.address
) {
130 /* GPE block 1 exists (has both length and address > 0) */
132 register_count1
= (u16
)(acpi_gbl_FADT
.gpe1_block_length
/ 2);
134 /* Check for GPE0/GPE1 overlap (if both banks exist) */
136 if ((register_count0
) &&
137 (gpe_number_max
>= acpi_gbl_FADT
.gpe1_base
)) {
139 "GPE0 block (GPE 0 to %u) overlaps the GPE1 block "
140 "(GPE %u to %u) - Ignoring GPE1",
141 gpe_number_max
, acpi_gbl_FADT
.gpe1_base
,
142 acpi_gbl_FADT
.gpe1_base
+
144 ACPI_GPE_REGISTER_WIDTH
) - 1)));
146 /* Ignore GPE1 block by setting the register count to zero */
150 /* Install GPE Block 1 */
153 acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device
,
154 &acpi_gbl_FADT
.xgpe1_block
,
156 acpi_gbl_FADT
.gpe1_base
,
159 &acpi_gbl_gpe_fadt_blocks
162 if (ACPI_FAILURE(status
)) {
163 ACPI_EXCEPTION((AE_INFO
, status
,
164 "Could not create GPE Block 1"));
168 * GPE0 and GPE1 do not have to be contiguous in the GPE number
169 * space. However, GPE0 always starts at GPE number zero.
171 gpe_number_max
= acpi_gbl_FADT
.gpe1_base
+
172 ((register_count1
* ACPI_GPE_REGISTER_WIDTH
) - 1);
176 /* Exit if there are no GPE registers */
178 if ((register_count0
+ register_count1
) == 0) {
180 /* GPEs are not required by ACPI, this is OK */
182 ACPI_DEBUG_PRINT((ACPI_DB_INIT
,
183 "There are no GPE blocks defined in the FADT\n"));
188 /* Check for Max GPE number out-of-range */
190 if (gpe_number_max
> ACPI_GPE_MAX
) {
192 "Maximum GPE number from FADT is too large: 0x%X",
194 status
= AE_BAD_VALUE
;
199 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
200 return_ACPI_STATUS(AE_OK
);
203 /*******************************************************************************
205 * FUNCTION: acpi_ev_update_gpes
207 * PARAMETERS: table_owner_id - ID of the newly-loaded ACPI table
211 * DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a
212 * result of a Load() or load_table() operation. If new GPE
213 * methods have been installed, register the new methods and
214 * enable and runtime GPEs that are associated with them. Also,
215 * run any newly loaded _PRW methods in order to discover any
218 ******************************************************************************/
220 void acpi_ev_update_gpes(acpi_owner_id table_owner_id
)
222 struct acpi_gpe_xrupt_info
*gpe_xrupt_info
;
223 struct acpi_gpe_block_info
*gpe_block
;
224 struct acpi_gpe_walk_info walk_info
;
225 acpi_status status
= AE_OK
;
226 u32 new_wake_gpe_count
= 0;
228 /* We will examine only _PRW/_Lxx/_Exx methods owned by this table */
230 walk_info
.owner_id
= table_owner_id
;
231 walk_info
.execute_by_owner_id
= TRUE
;
234 if (acpi_gbl_leave_wake_gpes_disabled
) {
236 * 1) Run any newly-loaded _PRW methods to find any GPEs that
237 * can now be marked as CAN_WAKE GPEs. Note: We must run the
238 * _PRW methods before we process the _Lxx/_Exx methods because
239 * we will enable all runtime GPEs associated with the new
240 * _Lxx/_Exx methods at the time we process those methods.
242 * Unlock interpreter so that we can run the _PRW methods.
244 walk_info
.gpe_block
= NULL
;
245 walk_info
.gpe_device
= NULL
;
247 acpi_ex_exit_interpreter();
250 acpi_ns_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
252 ACPI_NS_WALK_NO_UNLOCK
,
253 acpi_ev_match_prw_and_gpe
, NULL
,
255 if (ACPI_FAILURE(status
)) {
256 ACPI_EXCEPTION((AE_INFO
, status
,
257 "While executing _PRW methods"));
260 acpi_ex_enter_interpreter();
261 new_wake_gpe_count
= walk_info
.count
;
265 * 2) Find any _Lxx/_Exx GPE methods that have just been loaded.
267 * Any GPEs that correspond to new _Lxx/_Exx methods and are not
268 * marked as CAN_WAKE are immediately enabled.
270 * Examine the namespace underneath each gpe_device within the
273 status
= acpi_ut_acquire_mutex(ACPI_MTX_EVENTS
);
274 if (ACPI_FAILURE(status
)) {
279 walk_info
.enable_this_gpe
= TRUE
;
281 /* Walk the interrupt level descriptor list */
283 gpe_xrupt_info
= acpi_gbl_gpe_xrupt_list_head
;
284 while (gpe_xrupt_info
) {
286 /* Walk all Gpe Blocks attached to this interrupt level */
288 gpe_block
= gpe_xrupt_info
->gpe_block_list_head
;
290 walk_info
.gpe_block
= gpe_block
;
291 walk_info
.gpe_device
= gpe_block
->node
;
293 status
= acpi_ns_walk_namespace(ACPI_TYPE_METHOD
,
294 walk_info
.gpe_device
,
296 ACPI_NS_WALK_NO_UNLOCK
,
297 acpi_ev_match_gpe_method
,
298 NULL
, &walk_info
, NULL
);
299 if (ACPI_FAILURE(status
)) {
300 ACPI_EXCEPTION((AE_INFO
, status
,
301 "While decoding _Lxx/_Exx methods"));
304 gpe_block
= gpe_block
->next
;
307 gpe_xrupt_info
= gpe_xrupt_info
->next
;
310 if (walk_info
.count
|| new_wake_gpe_count
) {
312 "Enabled %u new runtime GPEs, added %u new wakeup GPEs",
313 walk_info
.count
, new_wake_gpe_count
));
316 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS
);
320 /*******************************************************************************
322 * FUNCTION: acpi_ev_match_gpe_method
324 * PARAMETERS: Callback from walk_namespace
328 * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
329 * control method under the _GPE portion of the namespace.
330 * Extract the name and GPE type from the object, saving this
331 * information for quick lookup during GPE dispatch. Allows a
332 * per-owner_id evaluation if execute_by_owner_id is TRUE in the
333 * walk_info parameter block.
335 * The name of each GPE control method is of the form:
336 * "_Lxx" or "_Exx", where:
337 * L - means that the GPE is level triggered
338 * E - means that the GPE is edge triggered
339 * xx - is the GPE number [in HEX]
341 * If walk_info->execute_by_owner_id is TRUE, we only execute examine GPE methods
343 * If walk_info->enable_this_gpe is TRUE, the GPE that is referred to by a GPE
344 * method is immediately enabled (Used for Load/load_table operators)
346 ******************************************************************************/
349 acpi_ev_match_gpe_method(acpi_handle obj_handle
,
350 u32 level
, void *context
, void **return_value
)
352 struct acpi_namespace_node
*method_node
=
353 ACPI_CAST_PTR(struct acpi_namespace_node
, obj_handle
);
354 struct acpi_gpe_walk_info
*walk_info
=
355 ACPI_CAST_PTR(struct acpi_gpe_walk_info
, context
);
356 struct acpi_gpe_event_info
*gpe_event_info
;
357 struct acpi_namespace_node
*gpe_device
;
360 char name
[ACPI_NAME_SIZE
+ 1];
363 ACPI_FUNCTION_TRACE(ev_match_gpe_method
);
365 /* Check if requested owner_id matches this owner_id */
367 if ((walk_info
->execute_by_owner_id
) &&
368 (method_node
->owner_id
!= walk_info
->owner_id
)) {
369 return_ACPI_STATUS(AE_OK
);
373 * Match and decode the _Lxx and _Exx GPE method names
375 * 1) Extract the method name and null terminate it
377 ACPI_MOVE_32_TO_32(name
, &method_node
->name
.integer
);
378 name
[ACPI_NAME_SIZE
] = 0;
380 /* 2) Name must begin with an underscore */
382 if (name
[0] != '_') {
383 return_ACPI_STATUS(AE_OK
); /* Ignore this method */
387 * 3) Edge/Level determination is based on the 2nd character
390 * NOTE: Default GPE type is RUNTIME only. Later, if a _PRW object is
391 * found that points to this GPE, the ACPI_GPE_CAN_WAKE flag is set.
395 type
= ACPI_GPE_LEVEL_TRIGGERED
;
399 type
= ACPI_GPE_EDGE_TRIGGERED
;
403 /* Unknown method type, just ignore it */
405 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
406 "Ignoring unknown GPE method type: %s "
407 "(name not of form _Lxx or _Exx)", name
));
408 return_ACPI_STATUS(AE_OK
);
411 /* 4) The last two characters of the name are the hex GPE Number */
413 gpe_number
= ACPI_STRTOUL(&name
[2], NULL
, 16);
414 if (gpe_number
== ACPI_UINT32_MAX
) {
416 /* Conversion failed; invalid method, just ignore it */
418 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
419 "Could not extract GPE number from name: %s "
420 "(name is not of form _Lxx or _Exx)", name
));
421 return_ACPI_STATUS(AE_OK
);
424 /* Ensure that we have a valid GPE number for this GPE block */
427 acpi_ev_low_get_gpe_info(gpe_number
, walk_info
->gpe_block
);
428 if (!gpe_event_info
) {
430 * This gpe_number is not valid for this GPE block, just ignore it.
431 * However, it may be valid for a different GPE block, since GPE0
432 * and GPE1 methods both appear under \_GPE.
434 return_ACPI_STATUS(AE_OK
);
437 if ((gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) ==
438 ACPI_GPE_DISPATCH_HANDLER
) {
440 /* If there is already a handler, ignore this GPE method */
442 return_ACPI_STATUS(AE_OK
);
445 if ((gpe_event_info
->flags
& ACPI_GPE_DISPATCH_MASK
) ==
446 ACPI_GPE_DISPATCH_METHOD
) {
448 * If there is already a method, ignore this method. But check
449 * for a type mismatch (if both the _Lxx AND _Exx exist)
451 if (type
!= (gpe_event_info
->flags
& ACPI_GPE_XRUPT_TYPE_MASK
)) {
453 "For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",
454 gpe_number
, gpe_number
, gpe_number
));
456 return_ACPI_STATUS(AE_OK
);
460 * Add the GPE information from above to the gpe_event_info block for
461 * use during dispatch of this GPE.
463 gpe_event_info
->flags
|= (u8
)(type
| ACPI_GPE_DISPATCH_METHOD
);
464 gpe_event_info
->dispatch
.method_node
= method_node
;
467 * Enable this GPE if requested. This only happens when during the
468 * execution of a Load or load_table operator. We have found a new
469 * GPE method and want to immediately enable the GPE if it is a
472 if (walk_info
->enable_this_gpe
) {
474 /* Ignore GPEs that can wake the system */
476 if (!(gpe_event_info
->flags
& ACPI_GPE_CAN_WAKE
) ||
477 !acpi_gbl_leave_wake_gpes_disabled
) {
479 gpe_device
= walk_info
->gpe_device
;
481 if (gpe_device
== acpi_gbl_fadt_gpe_device
) {
485 status
= acpi_enable_gpe(gpe_device
, gpe_number
);
486 if (ACPI_FAILURE(status
)) {
487 ACPI_EXCEPTION((AE_INFO
, status
,
488 "Could not enable GPE 0x%02X",
494 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
495 "Registered GPE method %s as GPE number 0x%.2X\n",
497 return_ACPI_STATUS(AE_OK
);
500 /*******************************************************************************
502 * FUNCTION: acpi_ev_match_prw_and_gpe
504 * PARAMETERS: Callback from walk_namespace
506 * RETURN: Status. NOTE: We ignore errors so that the _PRW walk is
507 * not aborted on a single _PRW failure.
509 * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
510 * Device. Run the _PRW method. If present, extract the GPE
511 * number and mark the GPE as a CAN_WAKE GPE. Allows a
512 * per-owner_id execution if execute_by_owner_id is TRUE in the
513 * walk_info parameter block.
515 * If walk_info->execute_by_owner_id is TRUE, we only execute _PRWs with that
517 * If walk_info->gpe_device is NULL, we execute every _PRW found. Otherwise,
518 * we only execute _PRWs that refer to the input gpe_device.
520 ******************************************************************************/
523 acpi_ev_match_prw_and_gpe(acpi_handle obj_handle
,
524 u32 level
, void *context
, void **return_value
)
526 struct acpi_gpe_walk_info
*walk_info
=
527 ACPI_CAST_PTR(struct acpi_gpe_walk_info
, context
);
528 struct acpi_namespace_node
*gpe_device
;
529 struct acpi_gpe_block_info
*gpe_block
;
530 struct acpi_namespace_node
*target_gpe_device
;
531 struct acpi_namespace_node
*prw_node
;
532 struct acpi_gpe_event_info
*gpe_event_info
;
533 union acpi_operand_object
*pkg_desc
;
534 union acpi_operand_object
*obj_desc
;
538 ACPI_FUNCTION_TRACE(ev_match_prw_and_gpe
);
540 /* Check for a _PRW method under this device */
542 status
= acpi_ns_get_node(obj_handle
, METHOD_NAME__PRW
,
543 ACPI_NS_NO_UPSEARCH
, &prw_node
);
544 if (ACPI_FAILURE(status
)) {
545 return_ACPI_STATUS(AE_OK
);
548 /* Check if requested owner_id matches this owner_id */
550 if ((walk_info
->execute_by_owner_id
) &&
551 (prw_node
->owner_id
!= walk_info
->owner_id
)) {
552 return_ACPI_STATUS(AE_OK
);
555 /* Execute the _PRW */
557 status
= acpi_ut_evaluate_object(prw_node
, NULL
,
558 ACPI_BTYPE_PACKAGE
, &pkg_desc
);
559 if (ACPI_FAILURE(status
)) {
560 return_ACPI_STATUS(AE_OK
);
563 /* The returned _PRW package must have at least two elements */
565 if (pkg_desc
->package
.count
< 2) {
569 /* Extract pointers from the input context */
571 gpe_device
= walk_info
->gpe_device
;
572 gpe_block
= walk_info
->gpe_block
;
575 * The _PRW object must return a package, we are only interested
576 * in the first element
578 obj_desc
= pkg_desc
->package
.elements
[0];
580 if (obj_desc
->common
.type
== ACPI_TYPE_INTEGER
) {
582 /* Use FADT-defined GPE device (from definition of _PRW) */
584 target_gpe_device
= NULL
;
586 target_gpe_device
= acpi_gbl_fadt_gpe_device
;
589 /* Integer is the GPE number in the FADT described GPE blocks */
591 gpe_number
= (u32
)obj_desc
->integer
.value
;
592 } else if (obj_desc
->common
.type
== ACPI_TYPE_PACKAGE
) {
594 /* Package contains a GPE reference and GPE number within a GPE block */
596 if ((obj_desc
->package
.count
< 2) ||
597 ((obj_desc
->package
.elements
[0])->common
.type
!=
598 ACPI_TYPE_LOCAL_REFERENCE
) ||
599 ((obj_desc
->package
.elements
[1])->common
.type
!=
600 ACPI_TYPE_INTEGER
)) {
604 /* Get GPE block reference and decode */
607 obj_desc
->package
.elements
[0]->reference
.node
;
608 gpe_number
= (u32
)obj_desc
->package
.elements
[1]->integer
.value
;
610 /* Unknown type, just ignore it */
615 /* Get the gpe_event_info for this GPE */
619 * Is this GPE within this block?
621 * TRUE if and only if these conditions are true:
622 * 1) The GPE devices match.
623 * 2) The GPE index(number) is within the range of the Gpe Block
624 * associated with the GPE device.
626 if (gpe_device
!= target_gpe_device
) {
631 acpi_ev_low_get_gpe_info(gpe_number
, gpe_block
);
633 /* gpe_device is NULL, just match the target_device and gpe_number */
636 acpi_ev_get_gpe_event_info(target_gpe_device
, gpe_number
);
639 if (gpe_event_info
) {
640 if (!(gpe_event_info
->flags
& ACPI_GPE_CAN_WAKE
)) {
642 /* This GPE can wake the system */
644 gpe_event_info
->flags
|= ACPI_GPE_CAN_WAKE
;
650 acpi_ut_remove_reference(pkg_desc
);
651 return_ACPI_STATUS(AE_OK
);