blob: 2c39ff2a7406900ea593f6d7f886253410444379 [file] [log] [blame]
Erik Schmauss95857632018-03-14 16:13:07 -07001// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
Lin Ming3cfd53d2010-12-13 13:36:02 +08002/******************************************************************************
3 *
4 * Module Name: evxfgpe - External Interfaces for General Purpose Events (GPEs)
5 *
Bob Moore800ba7c2020-01-10 11:31:49 -08006 * Copyright (C) 2000 - 2020, Intel Corp.
Lin Ming3cfd53d2010-12-13 13:36:02 +08007 *
Erik Schmauss95857632018-03-14 16:13:07 -07008 *****************************************************************************/
Lin Ming3cfd53d2010-12-13 13:36:02 +08009
Lv Zheng839e9282013-10-29 09:29:51 +080010#define EXPORT_ACPI_INTERFACES
11
Lin Ming3cfd53d2010-12-13 13:36:02 +080012#include <acpi/acpi.h>
13#include "accommon.h"
14#include "acevents.h"
15#include "acnamesp.h"
16
17#define _COMPONENT ACPI_EVENTS
18ACPI_MODULE_NAME("evxfgpe")
19
Bob Moore33620c52012-02-14 18:14:27 +080020#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
Lv Zheng75c80442012-12-19 05:36:49 +000021/*******************************************************************************
Lin Ming3cfd53d2010-12-13 13:36:02 +080022 *
Lin Ming3a378982010-12-13 13:36:15 +080023 * FUNCTION: acpi_update_all_gpes
Lin Ming3cfd53d2010-12-13 13:36:02 +080024 *
25 * PARAMETERS: None
26 *
Lin Mingda503372010-12-13 13:39:37 +080027 * RETURN: Status
Lin Ming3cfd53d2010-12-13 13:36:02 +080028 *
Lin Mingda503372010-12-13 13:39:37 +080029 * DESCRIPTION: Complete GPE initialization and enable all GPEs that have
30 * associated _Lxx or _Exx methods and are not pointed to by any
31 * device _PRW methods (this indicates that these GPEs are
32 * generally intended for system or device wakeup. Such GPEs
33 * have to be enabled directly when the devices whose _PRW
34 * methods point to them are set up for wakeup signaling.)
35 *
36 * NOTE: Should be called after any GPEs are added to the system. Primarily,
37 * after the system _PRW methods have been run, but also after a GPE Block
38 * Device has been added or if any new GPE methods have been added via a
39 * dynamic table load.
Lin Ming3cfd53d2010-12-13 13:36:02 +080040 *
41 ******************************************************************************/
42
Lin Ming3a378982010-12-13 13:36:15 +080043acpi_status acpi_update_all_gpes(void)
Lin Ming3cfd53d2010-12-13 13:36:02 +080044{
45 acpi_status status;
Erik Schmauss87cd8262018-03-14 16:12:59 -070046 u8 is_polling_needed = FALSE;
Lin Ming3cfd53d2010-12-13 13:36:02 +080047
Lin Ming3a378982010-12-13 13:36:15 +080048 ACPI_FUNCTION_TRACE(acpi_update_all_gpes);
Lin Ming3cfd53d2010-12-13 13:36:02 +080049
50 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
51 if (ACPI_FAILURE(status)) {
52 return_ACPI_STATUS(status);
Lin Ming3a378982010-12-13 13:36:15 +080053 }
54
55 if (acpi_gbl_all_gpes_initialized) {
56 goto unlock_and_exit;
Lin Ming3cfd53d2010-12-13 13:36:02 +080057 }
58
Erik Schmauss87cd8262018-03-14 16:12:59 -070059 status = acpi_ev_walk_gpe_list(acpi_ev_initialize_gpe_block,
60 &is_polling_needed);
Lin Ming3cfd53d2010-12-13 13:36:02 +080061 if (ACPI_SUCCESS(status)) {
Lin Ming3a378982010-12-13 13:36:15 +080062 acpi_gbl_all_gpes_initialized = TRUE;
Lin Ming3cfd53d2010-12-13 13:36:02 +080063 }
64
Lin Ming3a378982010-12-13 13:36:15 +080065unlock_and_exit:
Lin Ming3cfd53d2010-12-13 13:36:02 +080066 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
67
Erik Schmauss87cd8262018-03-14 16:12:59 -070068 if (is_polling_needed && acpi_gbl_all_gpes_initialized) {
69
70 /* Poll GPEs to handle already triggered events */
71
72 acpi_ev_gpe_detect(acpi_gbl_gpe_xrupt_list_head);
73 }
Lin Ming3cfd53d2010-12-13 13:36:02 +080074 return_ACPI_STATUS(status);
75}
76
Lin Ming3a378982010-12-13 13:36:15 +080077ACPI_EXPORT_SYMBOL(acpi_update_all_gpes)
78
Lin Ming3cfd53d2010-12-13 13:36:02 +080079/*******************************************************************************
80 *
81 * FUNCTION: acpi_enable_gpe
82 *
Lv Zhengf19f1a72014-10-10 10:39:39 +080083 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
84 * gpe_number - GPE level within the GPE block
Lin Ming3cfd53d2010-12-13 13:36:02 +080085 *
86 * RETURN: Status
87 *
88 * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
89 * hardware-enabled.
90 *
91 ******************************************************************************/
Lin Ming3cfd53d2010-12-13 13:36:02 +080092acpi_status acpi_enable_gpe(acpi_handle gpe_device, u32 gpe_number)
93{
94 acpi_status status = AE_BAD_PARAMETER;
95 struct acpi_gpe_event_info *gpe_event_info;
96 acpi_cpu_flags flags;
97
98 ACPI_FUNCTION_TRACE(acpi_enable_gpe);
99
100 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
101
Rafael J. Wysockib9ca3d72014-07-23 13:11:59 +0800102 /*
103 * Ensure that we have a valid GPE number and that there is some way
104 * of handling the GPE (handler or a GPE method). In other words, we
105 * won't allow a valid GPE to be enabled if there is no way to handle it.
106 */
Lin Ming3cfd53d2010-12-13 13:36:02 +0800107 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
108 if (gpe_event_info) {
Lv Zheng7c433122015-02-05 15:20:29 +0800109 if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) !=
Rafael J. Wysockib9ca3d72014-07-23 13:11:59 +0800110 ACPI_GPE_DISPATCH_NONE) {
Rafael J. Wysocki44758ba2019-06-17 13:31:45 +0200111 status = acpi_ev_add_gpe_reference(gpe_event_info, TRUE);
Erik Schmauss87cd8262018-03-14 16:12:59 -0700112 if (ACPI_SUCCESS(status) &&
113 ACPI_GPE_IS_POLLING_NEEDED(gpe_event_info)) {
114
115 /* Poll edge-triggered GPEs to handle existing events */
116
117 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
118 (void)acpi_ev_detect_gpe(gpe_device,
119 gpe_event_info,
120 gpe_number);
121 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
122 }
Rafael J. Wysockib9ca3d72014-07-23 13:11:59 +0800123 } else {
124 status = AE_NO_HANDLER;
125 }
Lin Ming3cfd53d2010-12-13 13:36:02 +0800126 }
127
128 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
129 return_ACPI_STATUS(status);
130}
131ACPI_EXPORT_SYMBOL(acpi_enable_gpe)
132
133/*******************************************************************************
134 *
135 * FUNCTION: acpi_disable_gpe
136 *
137 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
138 * gpe_number - GPE level within the GPE block
139 *
140 * RETURN: Status
141 *
142 * DESCRIPTION: Remove a reference to a GPE. When the last reference is
143 * removed, only then is the GPE disabled (for runtime GPEs), or
144 * the GPE mask bit disabled (for wake GPEs)
145 *
146 ******************************************************************************/
147
148acpi_status acpi_disable_gpe(acpi_handle gpe_device, u32 gpe_number)
149{
150 acpi_status status = AE_BAD_PARAMETER;
151 struct acpi_gpe_event_info *gpe_event_info;
152 acpi_cpu_flags flags;
153
154 ACPI_FUNCTION_TRACE(acpi_disable_gpe);
155
156 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
157
158 /* Ensure that we have a valid GPE number */
159
160 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
161 if (gpe_event_info) {
Lin Ming3a378982010-12-13 13:36:15 +0800162 status = acpi_ev_remove_gpe_reference(gpe_event_info) ;
Lin Ming3cfd53d2010-12-13 13:36:02 +0800163 }
164
165 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
166 return_ACPI_STATUS(status);
167}
Lv Zheng3e8214e2012-12-19 05:37:15 +0000168
Lin Ming3cfd53d2010-12-13 13:36:02 +0800169ACPI_EXPORT_SYMBOL(acpi_disable_gpe)
170
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800171/*******************************************************************************
172 *
173 * FUNCTION: acpi_set_gpe
174 *
175 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
176 * gpe_number - GPE level within the GPE block
177 * action - ACPI_GPE_ENABLE or ACPI_GPE_DISABLE
178 *
179 * RETURN: Status
180 *
181 * DESCRIPTION: Enable or disable an individual GPE. This function bypasses
Lv Zheng2eedd3d2015-02-05 16:27:15 +0800182 * the reference count mechanism used in the acpi_enable_gpe(),
183 * acpi_disable_gpe() interfaces.
184 * This API is typically used by the GPE raw handler mode driver
185 * to switch between the polling mode and the interrupt mode after
186 * the driver has enabled the GPE.
187 * The APIs should be invoked in this order:
188 * acpi_enable_gpe() <- Ensure the reference count > 0
189 * acpi_set_gpe(ACPI_GPE_DISABLE) <- Enter polling mode
190 * acpi_set_gpe(ACPI_GPE_ENABLE) <- Leave polling mode
191 * acpi_disable_gpe() <- Decrease the reference count
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800192 *
Lv Zheng2eedd3d2015-02-05 16:27:15 +0800193 * Note: If a GPE is shared by 2 silicon components, then both the drivers
194 * should support GPE polling mode or disabling the GPE for long period
195 * for one driver may break the other. So use it with care since all
196 * firmware _Lxx/_Exx handlers currently rely on the GPE interrupt mode.
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800197 *
198 ******************************************************************************/
199acpi_status acpi_set_gpe(acpi_handle gpe_device, u32 gpe_number, u8 action)
200{
201 struct acpi_gpe_event_info *gpe_event_info;
202 acpi_status status;
203 acpi_cpu_flags flags;
204
205 ACPI_FUNCTION_TRACE(acpi_set_gpe);
206
207 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
208
209 /* Ensure that we have a valid GPE number */
210
211 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
212 if (!gpe_event_info) {
213 status = AE_BAD_PARAMETER;
214 goto unlock_and_exit;
215 }
216
217 /* Perform the action */
218
219 switch (action) {
220 case ACPI_GPE_ENABLE:
221
Lv Zheng2eedd3d2015-02-05 16:27:15 +0800222 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
Lv Zheng2af52c22016-08-04 16:43:39 +0800223 gpe_event_info->disable_for_dispatch = FALSE;
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800224 break;
225
226 case ACPI_GPE_DISABLE:
227
228 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
Lv Zheng2af52c22016-08-04 16:43:39 +0800229 gpe_event_info->disable_for_dispatch = TRUE;
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800230 break;
231
232 default:
233
234 status = AE_BAD_PARAMETER;
235 break;
236 }
237
238unlock_and_exit:
239 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
240 return_ACPI_STATUS(status);
241}
242
243ACPI_EXPORT_SYMBOL(acpi_set_gpe)
Lin Mingbba63a22010-12-13 13:39:17 +0800244
Lin Ming3cfd53d2010-12-13 13:36:02 +0800245/*******************************************************************************
246 *
Lv Zheng2af52c22016-08-04 16:43:39 +0800247 * FUNCTION: acpi_mask_gpe
248 *
249 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
250 * gpe_number - GPE level within the GPE block
251 * is_masked - Whether the GPE is masked or not
252 *
253 * RETURN: Status
254 *
255 * DESCRIPTION: Unconditionally mask/unmask the an individual GPE, ex., to
256 * prevent a GPE flooding.
257 *
258 ******************************************************************************/
259acpi_status acpi_mask_gpe(acpi_handle gpe_device, u32 gpe_number, u8 is_masked)
260{
261 struct acpi_gpe_event_info *gpe_event_info;
262 acpi_status status;
263 acpi_cpu_flags flags;
264
265 ACPI_FUNCTION_TRACE(acpi_mask_gpe);
266
267 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
268
269 /* Ensure that we have a valid GPE number */
270
271 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
272 if (!gpe_event_info) {
273 status = AE_BAD_PARAMETER;
274 goto unlock_and_exit;
275 }
276
277 status = acpi_ev_mask_gpe(gpe_event_info, is_masked);
278
279unlock_and_exit:
280 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
281 return_ACPI_STATUS(status);
282}
283
284ACPI_EXPORT_SYMBOL(acpi_mask_gpe)
285
286/*******************************************************************************
287 *
Rafael J. Wysockic12f07d2014-07-23 13:12:16 +0800288 * FUNCTION: acpi_mark_gpe_for_wake
289 *
290 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
291 * gpe_number - GPE level within the GPE block
292 *
293 * RETURN: Status
294 *
295 * DESCRIPTION: Mark a GPE as having the ability to wake the system. Simply
296 * sets the ACPI_GPE_CAN_WAKE flag.
297 *
298 * Some potential callers of acpi_setup_gpe_for_wake may know in advance that
299 * there won't be any notify handlers installed for device wake notifications
300 * from the given GPE (one example is a button GPE in Linux). For these cases,
301 * acpi_mark_gpe_for_wake should be used instead of acpi_setup_gpe_for_wake.
302 * This will set the ACPI_GPE_CAN_WAKE flag for the GPE without trying to
303 * setup implicit wake notification for it (since there's no handler method).
304 *
305 ******************************************************************************/
306acpi_status acpi_mark_gpe_for_wake(acpi_handle gpe_device, u32 gpe_number)
307{
308 struct acpi_gpe_event_info *gpe_event_info;
309 acpi_status status = AE_BAD_PARAMETER;
310 acpi_cpu_flags flags;
311
312 ACPI_FUNCTION_TRACE(acpi_mark_gpe_for_wake);
313
314 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
315
316 /* Ensure that we have a valid GPE number */
317
318 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
319 if (gpe_event_info) {
320
321 /* Mark the GPE as a possible wake event */
322
323 gpe_event_info->flags |= ACPI_GPE_CAN_WAKE;
324 status = AE_OK;
325 }
326
327 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
328 return_ACPI_STATUS(status);
329}
330
331ACPI_EXPORT_SYMBOL(acpi_mark_gpe_for_wake)
332
333/*******************************************************************************
334 *
Lin Ming3a378982010-12-13 13:36:15 +0800335 * FUNCTION: acpi_setup_gpe_for_wake
Lin Ming3cfd53d2010-12-13 13:36:02 +0800336 *
Lin Mingbba63a22010-12-13 13:39:17 +0800337 * PARAMETERS: wake_device - Device associated with the GPE (via _PRW)
338 * gpe_device - Parent GPE Device. NULL for GPE0/GPE1
339 * gpe_number - GPE level within the GPE block
Lin Ming3cfd53d2010-12-13 13:36:02 +0800340 *
341 * RETURN: Status
342 *
Lin Mingbba63a22010-12-13 13:39:17 +0800343 * DESCRIPTION: Mark a GPE as having the ability to wake the system. This
344 * interface is intended to be used as the host executes the
345 * _PRW methods (Power Resources for Wake) in the system tables.
346 * Each _PRW appears under a Device Object (The wake_device), and
347 * contains the info for the wake GPE associated with the
348 * wake_device.
Lin Ming3cfd53d2010-12-13 13:36:02 +0800349 *
350 ******************************************************************************/
Lin Mingbba63a22010-12-13 13:39:17 +0800351acpi_status
352acpi_setup_gpe_for_wake(acpi_handle wake_device,
353 acpi_handle gpe_device, u32 gpe_number)
Lin Ming3cfd53d2010-12-13 13:36:02 +0800354{
Bob Moore5816b342012-06-29 10:04:17 +0800355 acpi_status status;
Lin Ming3cfd53d2010-12-13 13:36:02 +0800356 struct acpi_gpe_event_info *gpe_event_info;
Lin Mingbba63a22010-12-13 13:39:17 +0800357 struct acpi_namespace_node *device_node;
Bob Moore5816b342012-06-29 10:04:17 +0800358 struct acpi_gpe_notify_info *notify;
359 struct acpi_gpe_notify_info *new_notify;
Lin Ming3cfd53d2010-12-13 13:36:02 +0800360 acpi_cpu_flags flags;
361
Lin Ming3a378982010-12-13 13:36:15 +0800362 ACPI_FUNCTION_TRACE(acpi_setup_gpe_for_wake);
Lin Ming3cfd53d2010-12-13 13:36:02 +0800363
Lin Mingbba63a22010-12-13 13:39:17 +0800364 /* Parameter Validation */
365
366 if (!wake_device) {
367 /*
368 * By forcing wake_device to be valid, we automatically enable the
369 * implicit notify feature on all hosts.
370 */
371 return_ACPI_STATUS(AE_BAD_PARAMETER);
372 }
373
Bob Moore5816b342012-06-29 10:04:17 +0800374 /* Handle root object case */
375
376 if (wake_device == ACPI_ROOT_OBJECT) {
377 device_node = acpi_gbl_root_node;
378 } else {
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000379 device_node =
380 ACPI_CAST_PTR(struct acpi_namespace_node, wake_device);
Bob Moore5816b342012-06-29 10:04:17 +0800381 }
382
Lv Zheng75c80442012-12-19 05:36:49 +0000383 /* Validate wake_device is of type Device */
Bob Moore5816b342012-06-29 10:04:17 +0800384
385 if (device_node->type != ACPI_TYPE_DEVICE) {
386 return_ACPI_STATUS (AE_BAD_PARAMETER);
387 }
388
389 /*
390 * Allocate a new notify object up front, in case it is needed.
391 * Memory allocation while holding a spinlock is a big no-no
392 * on some hosts.
393 */
394 new_notify = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_notify_info));
395 if (!new_notify) {
396 return_ACPI_STATUS(AE_NO_MEMORY);
397 }
398
Lin Ming3cfd53d2010-12-13 13:36:02 +0800399 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
400
401 /* Ensure that we have a valid GPE number */
402
403 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
Rafael J. Wysocki2d559512011-02-12 01:39:15 +0100404 if (!gpe_event_info) {
Bob Moore5816b342012-06-29 10:04:17 +0800405 status = AE_BAD_PARAMETER;
Rafael J. Wysocki2d559512011-02-12 01:39:15 +0100406 goto unlock_and_exit;
Lin Ming3cfd53d2010-12-13 13:36:02 +0800407 }
408
Rafael J. Wysocki2d559512011-02-12 01:39:15 +0100409 /*
410 * If there is no method or handler for this GPE, then the
Bob Moore5816b342012-06-29 10:04:17 +0800411 * wake_device will be notified whenever this GPE fires. This is
412 * known as an "implicit notify". Note: The GPE is assumed to be
Rafael J. Wysocki2d559512011-02-12 01:39:15 +0100413 * level-triggered (for windows compatibility).
414 */
Lv Zheng7c433122015-02-05 15:20:29 +0800415 if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
Bob Moore5816b342012-06-29 10:04:17 +0800416 ACPI_GPE_DISPATCH_NONE) {
417 /*
418 * This is the first device for implicit notify on this GPE.
419 * Just set the flags here, and enter the NOTIFY block below.
420 */
421 gpe_event_info->flags =
422 (ACPI_GPE_DISPATCH_NOTIFY | ACPI_GPE_LEVEL_TRIGGERED);
Rafael J. Wysocki1312b7e2017-08-10 00:31:58 +0200423 } else if (gpe_event_info->flags & ACPI_GPE_AUTO_ENABLED) {
424 /*
425 * A reference to this GPE has been added during the GPE block
426 * initialization, so drop it now to prevent the GPE from being
427 * permanently enabled and clear its ACPI_GPE_AUTO_ENABLED flag.
428 */
429 (void)acpi_ev_remove_gpe_reference(gpe_event_info);
430 gpe_event_info->flags &= ~ACPI_GPE_AUTO_ENABLED;
Rafael J. Wysocki2d559512011-02-12 01:39:15 +0100431 }
432
Bob Moore5816b342012-06-29 10:04:17 +0800433 /*
434 * If we already have an implicit notify on this GPE, add
435 * this device to the notify list.
436 */
Lv Zheng7c433122015-02-05 15:20:29 +0800437 if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
Bob Moore5816b342012-06-29 10:04:17 +0800438 ACPI_GPE_DISPATCH_NOTIFY) {
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100439
Bob Moore5816b342012-06-29 10:04:17 +0800440 /* Ensure that the device is not already in the list */
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100441
Bob Moore5816b342012-06-29 10:04:17 +0800442 notify = gpe_event_info->dispatch.notify_list;
443 while (notify) {
444 if (notify->device_node == device_node) {
445 status = AE_ALREADY_EXISTS;
446 goto unlock_and_exit;
447 }
448 notify = notify->next;
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100449 }
450
Bob Moore5816b342012-06-29 10:04:17 +0800451 /* Add this device to the notify list for this GPE */
452
453 new_notify->device_node = device_node;
454 new_notify->next = gpe_event_info->dispatch.notify_list;
455 gpe_event_info->dispatch.notify_list = new_notify;
456 new_notify = NULL;
Rafael J. Wysocki981858b2011-02-24 19:59:21 +0100457 }
458
Bob Moore5816b342012-06-29 10:04:17 +0800459 /* Mark the GPE as a possible wake event */
460
Rafael J. Wysocki2d559512011-02-12 01:39:15 +0100461 gpe_event_info->flags |= ACPI_GPE_CAN_WAKE;
462 status = AE_OK;
463
Bob Moore5816b342012-06-29 10:04:17 +0800464unlock_and_exit:
Lin Ming3cfd53d2010-12-13 13:36:02 +0800465 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Bob Moore5816b342012-06-29 10:04:17 +0800466
467 /* Delete the notify object if it was not used above */
468
469 if (new_notify) {
470 ACPI_FREE(new_notify);
471 }
Lin Ming3cfd53d2010-12-13 13:36:02 +0800472 return_ACPI_STATUS(status);
473}
Lin Ming3a378982010-12-13 13:36:15 +0800474ACPI_EXPORT_SYMBOL(acpi_setup_gpe_for_wake)
Lin Ming3cfd53d2010-12-13 13:36:02 +0800475
476/*******************************************************************************
477 *
Lin Ming3a378982010-12-13 13:36:15 +0800478 * FUNCTION: acpi_set_gpe_wake_mask
Lin Ming3cfd53d2010-12-13 13:36:02 +0800479 *
480 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
481 * gpe_number - GPE level within the GPE block
Bob Mooreba494be2012-07-12 09:40:10 +0800482 * action - Enable or Disable
Lin Ming3cfd53d2010-12-13 13:36:02 +0800483 *
484 * RETURN: Status
485 *
Lin Mingda503372010-12-13 13:39:37 +0800486 * DESCRIPTION: Set or clear the GPE's wakeup enable mask bit. The GPE must
487 * already be marked as a WAKE GPE.
Lin Ming3cfd53d2010-12-13 13:36:02 +0800488 *
489 ******************************************************************************/
490
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000491acpi_status
492acpi_set_gpe_wake_mask(acpi_handle gpe_device, u32 gpe_number, u8 action)
Lin Ming3cfd53d2010-12-13 13:36:02 +0800493{
494 acpi_status status = AE_OK;
495 struct acpi_gpe_event_info *gpe_event_info;
496 struct acpi_gpe_register_info *gpe_register_info;
497 acpi_cpu_flags flags;
498 u32 register_bit;
499
Lin Ming3a378982010-12-13 13:36:15 +0800500 ACPI_FUNCTION_TRACE(acpi_set_gpe_wake_mask);
Lin Ming3cfd53d2010-12-13 13:36:02 +0800501
502 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
503
Lin Mingda503372010-12-13 13:39:37 +0800504 /*
505 * Ensure that we have a valid GPE number and that this GPE is in
506 * fact a wake GPE
507 */
Lin Ming3cfd53d2010-12-13 13:36:02 +0800508 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
Lin Ming3a378982010-12-13 13:36:15 +0800509 if (!gpe_event_info) {
Lin Ming3cfd53d2010-12-13 13:36:02 +0800510 status = AE_BAD_PARAMETER;
511 goto unlock_and_exit;
512 }
513
Lin Ming3a378982010-12-13 13:36:15 +0800514 if (!(gpe_event_info->flags & ACPI_GPE_CAN_WAKE)) {
515 status = AE_TYPE;
516 goto unlock_and_exit;
517 }
518
Lin Ming3cfd53d2010-12-13 13:36:02 +0800519 gpe_register_info = gpe_event_info->register_info;
520 if (!gpe_register_info) {
521 status = AE_NOT_EXIST;
522 goto unlock_and_exit;
523 }
524
Feng Tang1d94e1e2012-08-17 11:10:02 +0800525 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
Lin Ming3cfd53d2010-12-13 13:36:02 +0800526
527 /* Perform the action */
528
529 switch (action) {
530 case ACPI_GPE_ENABLE:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000531
Lin Ming3cfd53d2010-12-13 13:36:02 +0800532 ACPI_SET_BIT(gpe_register_info->enable_for_wake,
533 (u8)register_bit);
534 break;
535
536 case ACPI_GPE_DISABLE:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000537
Lin Ming3cfd53d2010-12-13 13:36:02 +0800538 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
539 (u8)register_bit);
540 break;
541
542 default:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000543
Lin Ming3cfd53d2010-12-13 13:36:02 +0800544 ACPI_ERROR((AE_INFO, "%u, Invalid action", action));
545 status = AE_BAD_PARAMETER;
546 break;
547 }
548
549unlock_and_exit:
550 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
551 return_ACPI_STATUS(status);
552}
553
Lin Ming3a378982010-12-13 13:36:15 +0800554ACPI_EXPORT_SYMBOL(acpi_set_gpe_wake_mask)
Lin Ming3cfd53d2010-12-13 13:36:02 +0800555
556/*******************************************************************************
557 *
558 * FUNCTION: acpi_clear_gpe
559 *
560 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
561 * gpe_number - GPE level within the GPE block
562 *
563 * RETURN: Status
564 *
565 * DESCRIPTION: Clear an ACPI event (general purpose)
566 *
567 ******************************************************************************/
568acpi_status acpi_clear_gpe(acpi_handle gpe_device, u32 gpe_number)
569{
570 acpi_status status = AE_OK;
571 struct acpi_gpe_event_info *gpe_event_info;
572 acpi_cpu_flags flags;
573
574 ACPI_FUNCTION_TRACE(acpi_clear_gpe);
575
576 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
577
578 /* Ensure that we have a valid GPE number */
579
580 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
581 if (!gpe_event_info) {
582 status = AE_BAD_PARAMETER;
583 goto unlock_and_exit;
584 }
585
586 status = acpi_hw_clear_gpe(gpe_event_info);
587
588 unlock_and_exit:
589 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
590 return_ACPI_STATUS(status);
591}
592
593ACPI_EXPORT_SYMBOL(acpi_clear_gpe)
594
595/*******************************************************************************
596 *
597 * FUNCTION: acpi_get_gpe_status
598 *
Lv Zhengf19f1a72014-10-10 10:39:39 +0800599 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
600 * gpe_number - GPE level within the GPE block
Lv Zheng75c80442012-12-19 05:36:49 +0000601 * event_status - Where the current status of the event
602 * will be returned
Lin Ming3cfd53d2010-12-13 13:36:02 +0800603 *
604 * RETURN: Status
605 *
Lin Mingda503372010-12-13 13:39:37 +0800606 * DESCRIPTION: Get the current status of a GPE (signalled/not_signalled)
Lin Ming3cfd53d2010-12-13 13:36:02 +0800607 *
608 ******************************************************************************/
609acpi_status
610acpi_get_gpe_status(acpi_handle gpe_device,
611 u32 gpe_number, acpi_event_status *event_status)
612{
613 acpi_status status = AE_OK;
614 struct acpi_gpe_event_info *gpe_event_info;
615 acpi_cpu_flags flags;
616
617 ACPI_FUNCTION_TRACE(acpi_get_gpe_status);
618
619 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
620
621 /* Ensure that we have a valid GPE number */
622
623 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
624 if (!gpe_event_info) {
625 status = AE_BAD_PARAMETER;
626 goto unlock_and_exit;
627 }
628
629 /* Obtain status on the requested GPE number */
630
631 status = acpi_hw_get_gpe_status(gpe_event_info, event_status);
632
Lv Zheng10622bf2013-10-29 09:30:02 +0800633unlock_and_exit:
Lin Ming3cfd53d2010-12-13 13:36:02 +0800634 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
635 return_ACPI_STATUS(status);
636}
637
638ACPI_EXPORT_SYMBOL(acpi_get_gpe_status)
639
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800640/*******************************************************************************
641 *
Rafael J. Wysocki74536452018-05-25 10:30:35 +0200642 * FUNCTION: acpi_gispatch_gpe
643 *
644 * PARAMETERS: gpe_device - Parent GPE Device. NULL for GPE0/GPE1
645 * gpe_number - GPE level within the GPE block
646 *
Rafael J. Wysocki6921de82019-07-15 13:03:28 +0200647 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
Rafael J. Wysocki74536452018-05-25 10:30:35 +0200648 *
649 * DESCRIPTION: Detect and dispatch a General Purpose Event to either a function
650 * (e.g. EC) or method (e.g. _Lxx/_Exx) handler.
651 *
652 ******************************************************************************/
Rafael J. Wysocki6921de82019-07-15 13:03:28 +0200653u32 acpi_dispatch_gpe(acpi_handle gpe_device, u32 gpe_number)
Rafael J. Wysocki74536452018-05-25 10:30:35 +0200654{
655 ACPI_FUNCTION_TRACE(acpi_dispatch_gpe);
656
Rafael J. Wysocki6921de82019-07-15 13:03:28 +0200657 return acpi_ev_detect_gpe(gpe_device, NULL, gpe_number);
Rafael J. Wysocki74536452018-05-25 10:30:35 +0200658}
659
660ACPI_EXPORT_SYMBOL(acpi_dispatch_gpe)
661
662/*******************************************************************************
663 *
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800664 * FUNCTION: acpi_finish_gpe
665 *
666 * PARAMETERS: gpe_device - Namespace node for the GPE Block
667 * (NULL for FADT defined GPEs)
668 * gpe_number - GPE level within the GPE block
669 *
670 * RETURN: Status
671 *
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800672 * DESCRIPTION: Clear and conditionally re-enable a GPE. This completes the GPE
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800673 * processing. Intended for use by asynchronous host-installed
Erik Schmaussc163f90c2019-02-15 13:36:19 -0800674 * GPE handlers. The GPE is only re-enabled if the enable_for_run bit
Lv Zhengeb3d80f2015-02-05 16:27:09 +0800675 * is set in the GPE info.
676 *
677 ******************************************************************************/
678acpi_status acpi_finish_gpe(acpi_handle gpe_device, u32 gpe_number)
679{
680 struct acpi_gpe_event_info *gpe_event_info;
681 acpi_status status;
682 acpi_cpu_flags flags;
683
684 ACPI_FUNCTION_TRACE(acpi_finish_gpe);
685
686 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
687
688 /* Ensure that we have a valid GPE number */
689
690 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
691 if (!gpe_event_info) {
692 status = AE_BAD_PARAMETER;
693 goto unlock_and_exit;
694 }
695
696 status = acpi_ev_finish_gpe(gpe_event_info);
697
698unlock_and_exit:
699 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
700 return_ACPI_STATUS(status);
701}
702
703ACPI_EXPORT_SYMBOL(acpi_finish_gpe)
704
Lin Ming3cfd53d2010-12-13 13:36:02 +0800705/******************************************************************************
706 *
707 * FUNCTION: acpi_disable_all_gpes
708 *
709 * PARAMETERS: None
710 *
711 * RETURN: Status
712 *
713 * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
714 *
715 ******************************************************************************/
716
717acpi_status acpi_disable_all_gpes(void)
718{
719 acpi_status status;
720
721 ACPI_FUNCTION_TRACE(acpi_disable_all_gpes);
722
723 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
724 if (ACPI_FAILURE(status)) {
725 return_ACPI_STATUS(status);
726 }
727
728 status = acpi_hw_disable_all_gpes();
729 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
730
731 return_ACPI_STATUS(status);
732}
733
Lin Ming3a378982010-12-13 13:36:15 +0800734ACPI_EXPORT_SYMBOL(acpi_disable_all_gpes)
735
Lin Ming3cfd53d2010-12-13 13:36:02 +0800736/******************************************************************************
737 *
738 * FUNCTION: acpi_enable_all_runtime_gpes
739 *
740 * PARAMETERS: None
741 *
742 * RETURN: Status
743 *
744 * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
745 *
746 ******************************************************************************/
747
748acpi_status acpi_enable_all_runtime_gpes(void)
749{
750 acpi_status status;
751
752 ACPI_FUNCTION_TRACE(acpi_enable_all_runtime_gpes);
753
754 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
755 if (ACPI_FAILURE(status)) {
756 return_ACPI_STATUS(status);
757 }
758
759 status = acpi_hw_enable_all_runtime_gpes();
760 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
761
762 return_ACPI_STATUS(status);
763}
764
Lin Ming3a378982010-12-13 13:36:15 +0800765ACPI_EXPORT_SYMBOL(acpi_enable_all_runtime_gpes)
766
Rafael J. Wysockie0fa9752014-09-30 02:25:42 +0200767/******************************************************************************
768 *
769 * FUNCTION: acpi_enable_all_wakeup_gpes
770 *
771 * PARAMETERS: None
772 *
773 * RETURN: Status
774 *
775 * DESCRIPTION: Enable all "wakeup" GPEs and disable all of the other GPEs, in
776 * all GPE blocks.
777 *
778 ******************************************************************************/
Rafael J. Wysockie0fa9752014-09-30 02:25:42 +0200779acpi_status acpi_enable_all_wakeup_gpes(void)
780{
781 acpi_status status;
782
783 ACPI_FUNCTION_TRACE(acpi_enable_all_wakeup_gpes);
784
785 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
786 if (ACPI_FAILURE(status)) {
787 return_ACPI_STATUS(status);
788 }
789
790 status = acpi_hw_enable_all_wakeup_gpes();
791 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
792
793 return_ACPI_STATUS(status);
794}
795
796ACPI_EXPORT_SYMBOL(acpi_enable_all_wakeup_gpes)
797
Lin Ming3cfd53d2010-12-13 13:36:02 +0800798/*******************************************************************************
799 *
800 * FUNCTION: acpi_install_gpe_block
801 *
802 * PARAMETERS: gpe_device - Handle to the parent GPE Block Device
Bob Mooreba494be2012-07-12 09:40:10 +0800803 * gpe_block_address - Address and space_ID
Lin Ming3cfd53d2010-12-13 13:36:02 +0800804 * register_count - Number of GPE register pairs in the block
805 * interrupt_number - H/W interrupt for the block
806 *
807 * RETURN: Status
808 *
Lin Mingda503372010-12-13 13:39:37 +0800809 * DESCRIPTION: Create and Install a block of GPE registers. The GPEs are not
810 * enabled here.
Lin Ming3cfd53d2010-12-13 13:36:02 +0800811 *
812 ******************************************************************************/
813acpi_status
814acpi_install_gpe_block(acpi_handle gpe_device,
815 struct acpi_generic_address *gpe_block_address,
816 u32 register_count, u32 interrupt_number)
817{
Lin Ming3a378982010-12-13 13:36:15 +0800818 acpi_status status;
Lin Ming3cfd53d2010-12-13 13:36:02 +0800819 union acpi_operand_object *obj_desc;
820 struct acpi_namespace_node *node;
821 struct acpi_gpe_block_info *gpe_block;
822
823 ACPI_FUNCTION_TRACE(acpi_install_gpe_block);
824
825 if ((!gpe_device) || (!gpe_block_address) || (!register_count)) {
826 return_ACPI_STATUS(AE_BAD_PARAMETER);
827 }
828
829 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
830 if (ACPI_FAILURE(status)) {
Bob Moore68aafc32012-10-31 02:26:01 +0000831 return_ACPI_STATUS(status);
Lin Ming3cfd53d2010-12-13 13:36:02 +0800832 }
833
834 node = acpi_ns_validate_handle(gpe_device);
835 if (!node) {
836 status = AE_BAD_PARAMETER;
837 goto unlock_and_exit;
838 }
839
Bob Moore9186fb52014-02-08 09:42:19 +0800840 /* Validate the parent device */
841
842 if (node->type != ACPI_TYPE_DEVICE) {
843 status = AE_TYPE;
844 goto unlock_and_exit;
845 }
846
847 if (node->object) {
848 status = AE_ALREADY_EXISTS;
849 goto unlock_and_exit;
850 }
851
Lin Ming3cfd53d2010-12-13 13:36:02 +0800852 /*
853 * For user-installed GPE Block Devices, the gpe_block_base_number
854 * is always zero
855 */
Bob Moore7505da42014-04-30 10:06:15 +0800856 status = acpi_ev_create_gpe_block(node, gpe_block_address->address,
857 gpe_block_address->space_id,
858 register_count, 0, interrupt_number,
859 &gpe_block);
Lin Ming3cfd53d2010-12-13 13:36:02 +0800860 if (ACPI_FAILURE(status)) {
861 goto unlock_and_exit;
862 }
863
864 /* Install block in the device_object attached to the node */
865
866 obj_desc = acpi_ns_get_attached_object(node);
867 if (!obj_desc) {
868
869 /*
870 * No object, create a new one (Device nodes do not always have
871 * an attached object)
872 */
873 obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_DEVICE);
874 if (!obj_desc) {
875 status = AE_NO_MEMORY;
876 goto unlock_and_exit;
877 }
878
879 status =
880 acpi_ns_attach_object(node, obj_desc, ACPI_TYPE_DEVICE);
881
882 /* Remove local reference to the object */
883
884 acpi_ut_remove_reference(obj_desc);
885
886 if (ACPI_FAILURE(status)) {
887 goto unlock_and_exit;
888 }
889 }
890
891 /* Now install the GPE block in the device_object */
892
893 obj_desc->device.gpe_block = gpe_block;
894
Lv Zheng10622bf2013-10-29 09:30:02 +0800895unlock_and_exit:
Lin Ming3cfd53d2010-12-13 13:36:02 +0800896 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
897 return_ACPI_STATUS(status);
898}
899
900ACPI_EXPORT_SYMBOL(acpi_install_gpe_block)
901
902/*******************************************************************************
903 *
904 * FUNCTION: acpi_remove_gpe_block
905 *
906 * PARAMETERS: gpe_device - Handle to the parent GPE Block Device
907 *
908 * RETURN: Status
909 *
910 * DESCRIPTION: Remove a previously installed block of GPE registers
911 *
912 ******************************************************************************/
913acpi_status acpi_remove_gpe_block(acpi_handle gpe_device)
914{
915 union acpi_operand_object *obj_desc;
916 acpi_status status;
917 struct acpi_namespace_node *node;
918
919 ACPI_FUNCTION_TRACE(acpi_remove_gpe_block);
920
921 if (!gpe_device) {
922 return_ACPI_STATUS(AE_BAD_PARAMETER);
923 }
924
925 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
926 if (ACPI_FAILURE(status)) {
Bob Moore68aafc32012-10-31 02:26:01 +0000927 return_ACPI_STATUS(status);
Lin Ming3cfd53d2010-12-13 13:36:02 +0800928 }
929
930 node = acpi_ns_validate_handle(gpe_device);
931 if (!node) {
932 status = AE_BAD_PARAMETER;
933 goto unlock_and_exit;
934 }
935
Bob Moore9186fb52014-02-08 09:42:19 +0800936 /* Validate the parent device */
937
938 if (node->type != ACPI_TYPE_DEVICE) {
939 status = AE_TYPE;
940 goto unlock_and_exit;
941 }
942
Lin Ming3cfd53d2010-12-13 13:36:02 +0800943 /* Get the device_object attached to the node */
944
945 obj_desc = acpi_ns_get_attached_object(node);
946 if (!obj_desc || !obj_desc->device.gpe_block) {
947 return_ACPI_STATUS(AE_NULL_OBJECT);
948 }
949
950 /* Delete the GPE block (but not the device_object) */
951
952 status = acpi_ev_delete_gpe_block(obj_desc->device.gpe_block);
953 if (ACPI_SUCCESS(status)) {
954 obj_desc->device.gpe_block = NULL;
955 }
956
Lv Zheng10622bf2013-10-29 09:30:02 +0800957unlock_and_exit:
Lin Ming3cfd53d2010-12-13 13:36:02 +0800958 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
959 return_ACPI_STATUS(status);
960}
961
962ACPI_EXPORT_SYMBOL(acpi_remove_gpe_block)
963
964/*******************************************************************************
965 *
966 * FUNCTION: acpi_get_gpe_device
967 *
Bob Mooreba494be2012-07-12 09:40:10 +0800968 * PARAMETERS: index - System GPE index (0-current_gpe_count)
Lin Ming3cfd53d2010-12-13 13:36:02 +0800969 * gpe_device - Where the parent GPE Device is returned
970 *
971 * RETURN: Status
972 *
973 * DESCRIPTION: Obtain the GPE device associated with the input index. A NULL
974 * gpe device indicates that the gpe number is contained in one of
975 * the FADT-defined gpe blocks. Otherwise, the GPE block device.
976 *
977 ******************************************************************************/
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800978acpi_status acpi_get_gpe_device(u32 index, acpi_handle *gpe_device)
Lin Ming3cfd53d2010-12-13 13:36:02 +0800979{
980 struct acpi_gpe_device_info info;
981 acpi_status status;
982
983 ACPI_FUNCTION_TRACE(acpi_get_gpe_device);
984
985 if (!gpe_device) {
986 return_ACPI_STATUS(AE_BAD_PARAMETER);
987 }
988
989 if (index >= acpi_current_gpe_count) {
990 return_ACPI_STATUS(AE_NOT_EXIST);
991 }
992
993 /* Setup and walk the GPE list */
994
995 info.index = index;
996 info.status = AE_NOT_EXIST;
997 info.gpe_device = NULL;
998 info.next_block_base_index = 0;
999
1000 status = acpi_ev_walk_gpe_list(acpi_ev_get_gpe_device, &info);
1001 if (ACPI_FAILURE(status)) {
1002 return_ACPI_STATUS(status);
1003 }
1004
Lin Ming3a378982010-12-13 13:36:15 +08001005 *gpe_device = ACPI_CAST_PTR(acpi_handle, info.gpe_device);
Lin Ming3cfd53d2010-12-13 13:36:02 +08001006 return_ACPI_STATUS(info.status);
1007}
1008
1009ACPI_EXPORT_SYMBOL(acpi_get_gpe_device)
Bob Moore33620c52012-02-14 18:14:27 +08001010#endif /* !ACPI_REDUCED_HARDWARE */