Linux 4.14-rc3
[linux-block.git] / drivers / acpi / acpica / hwgpe.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
3 * Module Name: hwgpe - Low level GPE enable/disable/clear functions
4 *
5 *****************************************************************************/
6
7/*
7735ca0e 8 * Copyright (C) 2000 - 2017, Intel Corp.
1da177e4
LT
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
42 */
43
44#include <acpi/acpi.h>
e2f7a777
LB
45#include "accommon.h"
46#include "acevents.h"
1da177e4
LT
47
48#define _COMPONENT ACPI_HARDWARE
4be44fcd 49ACPI_MODULE_NAME("hwgpe")
33620c54 50#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
44f6c012 51/* Local prototypes */
44f6c012 52static acpi_status
4be44fcd 53acpi_hw_enable_wakeup_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
e97d6bf1
BM
54 struct acpi_gpe_block_info *gpe_block,
55 void *context);
1da177e4 56
1c4c81a2
LZ
57static acpi_status
58acpi_hw_gpe_enable_write(u8 enable_mask,
59 struct acpi_gpe_register_info *gpe_register_info);
60
e4e9a735
RW
61/******************************************************************************
62 *
b76df673 63 * FUNCTION: acpi_hw_get_gpe_register_bit
e4e9a735
RW
64 *
65 * PARAMETERS: gpe_event_info - Info block for the GPE
e4e9a735 66 *
da503373 67 * RETURN: Register mask with a one in the GPE bit position
e4e9a735 68 *
da503373
LM
69 * DESCRIPTION: Compute the register mask for this GPE. One bit is set in the
70 * correct position for the input GPE.
e4e9a735
RW
71 *
72 ******************************************************************************/
73
1d94e1e8 74u32 acpi_hw_get_gpe_register_bit(struct acpi_gpe_event_info *gpe_event_info)
e4e9a735 75{
9c0d7939
LZ
76
77 return ((u32)1 <<
78 (gpe_event_info->gpe_number -
79 gpe_event_info->register_info->base_gpe_number));
e4e9a735
RW
80}
81
e38e8a07
BM
82/******************************************************************************
83 *
fd247447 84 * FUNCTION: acpi_hw_low_set_gpe
e38e8a07
BM
85 *
86 * PARAMETERS: gpe_event_info - Info block for the GPE to be disabled
fd247447 87 * action - Enable or disable
e38e8a07
BM
88 *
89 * RETURN: Status
90 *
da503373 91 * DESCRIPTION: Enable or disable a single GPE in the parent enable register.
0ee0d349
RW
92 * The enable_mask field of the involved GPE register must be
93 * updated by the caller if necessary.
e38e8a07
BM
94 *
95 ******************************************************************************/
96
fd247447 97acpi_status
da503373 98acpi_hw_low_set_gpe(struct acpi_gpe_event_info *gpe_event_info, u32 action)
e38e8a07
BM
99{
100 struct acpi_gpe_register_info *gpe_register_info;
2af52c2b 101 acpi_status status = AE_OK;
e38e8a07 102 u32 enable_mask;
e4e9a735 103 u32 register_bit;
e38e8a07 104
fd247447
RW
105 ACPI_FUNCTION_ENTRY();
106
e38e8a07
BM
107 /* Get the info block for the entire GPE register */
108
109 gpe_register_info = gpe_event_info->register_info;
110 if (!gpe_register_info) {
111 return (AE_NOT_EXIST);
112 }
113
114 /* Get current value of the enable register that contains this GPE */
115
c6b5774c 116 status = acpi_hw_read(&enable_mask, &gpe_register_info->enable_address);
e38e8a07
BM
117 if (ACPI_FAILURE(status)) {
118 return (status);
119 }
120
da503373 121 /* Set or clear just the bit that corresponds to this GPE */
e38e8a07 122
1d94e1e8 123 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
0ee0d349 124 switch (action) {
3a37898d 125 case ACPI_GPE_CONDITIONAL_ENABLE:
da503373 126
c50f13c6 127 /* Only enable if the corresponding enable_mask bit is set */
da503373 128
c50f13c6 129 if (!(register_bit & gpe_register_info->enable_mask)) {
c9a8bbb7 130 return (AE_BAD_PARAMETER);
da503373
LM
131 }
132
133 /*lint -fallthrough */
c9a8bbb7 134
fd247447 135 case ACPI_GPE_ENABLE:
1d1ea1b7 136
fd247447
RW
137 ACPI_SET_BIT(enable_mask, register_bit);
138 break;
139
140 case ACPI_GPE_DISABLE:
1d1ea1b7 141
fd247447
RW
142 ACPI_CLEAR_BIT(enable_mask, register_bit);
143 break;
144
145 default:
1d1ea1b7 146
5e30a96e 147 ACPI_ERROR((AE_INFO, "Invalid GPE Action, %u", action));
fd247447
RW
148 return (AE_BAD_PARAMETER);
149 }
e38e8a07 150
2af52c2b 151 if (!(register_bit & gpe_register_info->mask_for_run)) {
e38e8a07 152
2af52c2b
LZ
153 /* Write the updated enable mask */
154
155 status =
156 acpi_hw_write(enable_mask,
157 &gpe_register_info->enable_address);
158 }
e38e8a07
BM
159 return (status);
160}
161
1da177e4
LT
162/******************************************************************************
163 *
164 * FUNCTION: acpi_hw_clear_gpe
165 *
166 * PARAMETERS: gpe_event_info - Info block for the GPE to be cleared
167 *
168 * RETURN: Status
169 *
170 * DESCRIPTION: Clear the status bit for a single GPE.
171 *
172 ******************************************************************************/
173
f5c1e1c5 174acpi_status acpi_hw_clear_gpe(struct acpi_gpe_event_info *gpe_event_info)
1da177e4 175{
e4e9a735 176 struct acpi_gpe_register_info *gpe_register_info;
4be44fcd 177 acpi_status status;
e4e9a735 178 u32 register_bit;
1da177e4 179
4be44fcd 180 ACPI_FUNCTION_ENTRY();
1da177e4 181
e4e9a735
RW
182 /* Get the info block for the entire GPE register */
183
184 gpe_register_info = gpe_event_info->register_info;
185 if (!gpe_register_info) {
186 return (AE_NOT_EXIST);
187 }
188
1da177e4
LT
189 /*
190 * Write a one to the appropriate bit in the status register to
191 * clear this GPE.
192 */
1d94e1e8 193 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
da503373 194
1fad8738
BM
195 status =
196 acpi_hw_write(register_bit, &gpe_register_info->status_address);
1da177e4
LT
197 return (status);
198}
199
1da177e4
LT
200/******************************************************************************
201 *
202 * FUNCTION: acpi_hw_get_gpe_status
203 *
204 * PARAMETERS: gpe_event_info - Info block for the GPE to queried
205 * event_status - Where the GPE status is returned
206 *
207 * RETURN: Status
208 *
209 * DESCRIPTION: Return the status of a single GPE.
210 *
211 ******************************************************************************/
44f6c012 212
1da177e4 213acpi_status
f5c1e1c5 214acpi_hw_get_gpe_status(struct acpi_gpe_event_info *gpe_event_info,
f19f1a7e 215 acpi_event_status *event_status)
1da177e4 216{
4be44fcd 217 u32 in_byte;
e4e9a735 218 u32 register_bit;
4be44fcd 219 struct acpi_gpe_register_info *gpe_register_info;
4be44fcd 220 acpi_event_status local_event_status = 0;
da503373 221 acpi_status status;
1da177e4 222
4be44fcd 223 ACPI_FUNCTION_ENTRY();
1da177e4
LT
224
225 if (!event_status) {
226 return (AE_BAD_PARAMETER);
227 }
228
a08f813e
LZ
229 /* GPE currently handled? */
230
7c43312a 231 if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) !=
a08f813e 232 ACPI_GPE_DISPATCH_NONE) {
2f857234 233 local_event_status |= ACPI_EVENT_FLAG_HAS_HANDLER;
a08f813e
LZ
234 }
235
1da177e4
LT
236 /* Get the info block for the entire GPE register */
237
238 gpe_register_info = gpe_event_info->register_info;
239
240 /* Get the register bitmask for this GPE */
241
1d94e1e8 242 register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
1da177e4
LT
243
244 /* GPE currently enabled? (enabled for runtime?) */
245
246 if (register_bit & gpe_register_info->enable_for_run) {
247 local_event_status |= ACPI_EVENT_FLAG_ENABLED;
248 }
249
2af52c2b
LZ
250 /* GPE currently masked? (masked for runtime?) */
251
252 if (register_bit & gpe_register_info->mask_for_run) {
253 local_event_status |= ACPI_EVENT_FLAG_MASKED;
254 }
255
1da177e4
LT
256 /* GPE enabled for wake? */
257
258 if (register_bit & gpe_register_info->enable_for_wake) {
259 local_event_status |= ACPI_EVENT_FLAG_WAKE_ENABLED;
260 }
261
09af8e82
LZ
262 /* GPE currently enabled (enable bit == 1)? */
263
264 status = acpi_hw_read(&in_byte, &gpe_register_info->enable_address);
265 if (ACPI_FAILURE(status)) {
266 return (status);
267 }
268
269 if (register_bit & in_byte) {
270 local_event_status |= ACPI_EVENT_FLAG_ENABLE_SET;
271 }
272
1da177e4
LT
273 /* GPE currently active (status bit == 1)? */
274
c6b5774c 275 status = acpi_hw_read(&in_byte, &gpe_register_info->status_address);
4be44fcd 276 if (ACPI_FAILURE(status)) {
2147d3f0 277 return (status);
1da177e4
LT
278 }
279
280 if (register_bit & in_byte) {
09af8e82 281 local_event_status |= ACPI_EVENT_FLAG_STATUS_SET;
1da177e4
LT
282 }
283
284 /* Set return value */
285
286 (*event_status) = local_event_status;
2147d3f0 287 return (AE_OK);
1da177e4 288}
1da177e4 289
c50f13c6
RW
290/******************************************************************************
291 *
292 * FUNCTION: acpi_hw_gpe_enable_write
293 *
294 * PARAMETERS: enable_mask - Bit mask to write to the GPE register
295 * gpe_register_info - Gpe Register info
296 *
297 * RETURN: Status
298 *
299 * DESCRIPTION: Write the enable mask byte to the given GPE register.
300 *
301 ******************************************************************************/
302
303static acpi_status
304acpi_hw_gpe_enable_write(u8 enable_mask,
305 struct acpi_gpe_register_info *gpe_register_info)
306{
307 acpi_status status;
308
0ee0d349 309 gpe_register_info->enable_mask = enable_mask;
5431b654 310
1fad8738 311 status = acpi_hw_write(enable_mask, &gpe_register_info->enable_address);
c50f13c6
RW
312 return (status);
313}
314
1da177e4
LT
315/******************************************************************************
316 *
317 * FUNCTION: acpi_hw_disable_gpe_block
318 *
319 * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
320 * gpe_block - Gpe Block info
321 *
322 * RETURN: Status
323 *
44f6c012 324 * DESCRIPTION: Disable all GPEs within a single GPE block
1da177e4
LT
325 *
326 ******************************************************************************/
327
328acpi_status
e97d6bf1
BM
329acpi_hw_disable_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
330 struct acpi_gpe_block_info *gpe_block, void *context)
1da177e4 331{
4be44fcd
LB
332 u32 i;
333 acpi_status status;
1da177e4
LT
334
335 /* Examine each GPE Register within the block */
336
337 for (i = 0; i < gpe_block->register_count; i++) {
52fc0b02 338
1da177e4
LT
339 /* Disable all GPEs in this register */
340
ecfbbc7b 341 status =
c50f13c6
RW
342 acpi_hw_gpe_enable_write(0x00,
343 &gpe_block->register_info[i]);
4be44fcd 344 if (ACPI_FAILURE(status)) {
1da177e4
LT
345 return (status);
346 }
347 }
348
349 return (AE_OK);
350}
351
1da177e4
LT
352/******************************************************************************
353 *
354 * FUNCTION: acpi_hw_clear_gpe_block
355 *
356 * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
357 * gpe_block - Gpe Block info
358 *
359 * RETURN: Status
360 *
44f6c012 361 * DESCRIPTION: Clear status bits for all GPEs within a single GPE block
1da177e4
LT
362 *
363 ******************************************************************************/
364
365acpi_status
e97d6bf1
BM
366acpi_hw_clear_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
367 struct acpi_gpe_block_info *gpe_block, void *context)
1da177e4 368{
4be44fcd
LB
369 u32 i;
370 acpi_status status;
1da177e4
LT
371
372 /* Examine each GPE Register within the block */
373
374 for (i = 0; i < gpe_block->register_count; i++) {
52fc0b02 375
1da177e4
LT
376 /* Clear status on all GPEs in this register */
377
ecfbbc7b 378 status =
c6b5774c
BM
379 acpi_hw_write(0xFF,
380 &gpe_block->register_info[i].status_address);
4be44fcd 381 if (ACPI_FAILURE(status)) {
1da177e4
LT
382 return (status);
383 }
384 }
385
386 return (AE_OK);
387}
388
1da177e4
LT
389/******************************************************************************
390 *
391 * FUNCTION: acpi_hw_enable_runtime_gpe_block
392 *
393 * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
394 * gpe_block - Gpe Block info
395 *
396 * RETURN: Status
397 *
44f6c012
RM
398 * DESCRIPTION: Enable all "runtime" GPEs within a single GPE block. Includes
399 * combination wake/run GPEs.
1da177e4
LT
400 *
401 ******************************************************************************/
402
403acpi_status
e97d6bf1 404acpi_hw_enable_runtime_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
f5c1e1c5 405 struct acpi_gpe_block_info *gpe_block,
1f86e8c1 406 void *context)
1da177e4 407{
4be44fcd
LB
408 u32 i;
409 acpi_status status;
c50f13c6 410 struct acpi_gpe_register_info *gpe_register_info;
2af52c2b 411 u8 enable_mask;
1da177e4
LT
412
413 /* NOTE: assumes that all GPEs are currently disabled */
414
415 /* Examine each GPE Register within the block */
416
417 for (i = 0; i < gpe_block->register_count; i++) {
c50f13c6
RW
418 gpe_register_info = &gpe_block->register_info[i];
419 if (!gpe_register_info->enable_for_run) {
1da177e4
LT
420 continue;
421 }
422
423 /* Enable all "runtime" GPEs in this register */
424
2af52c2b
LZ
425 enable_mask = gpe_register_info->enable_for_run &
426 ~gpe_register_info->mask_for_run;
c6b5774c 427 status =
2af52c2b 428 acpi_hw_gpe_enable_write(enable_mask, gpe_register_info);
4be44fcd 429 if (ACPI_FAILURE(status)) {
1da177e4
LT
430 return (status);
431 }
432 }
433
434 return (AE_OK);
435}
436
1da177e4
LT
437/******************************************************************************
438 *
439 * FUNCTION: acpi_hw_enable_wakeup_gpe_block
440 *
441 * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
442 * gpe_block - Gpe Block info
443 *
444 * RETURN: Status
445 *
44f6c012
RM
446 * DESCRIPTION: Enable all "wake" GPEs within a single GPE block. Includes
447 * combination wake/run GPEs.
1da177e4
LT
448 *
449 ******************************************************************************/
450
44f6c012 451static acpi_status
4be44fcd 452acpi_hw_enable_wakeup_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
e97d6bf1
BM
453 struct acpi_gpe_block_info *gpe_block,
454 void *context)
1da177e4 455{
4be44fcd
LB
456 u32 i;
457 acpi_status status;
c50f13c6 458 struct acpi_gpe_register_info *gpe_register_info;
1da177e4
LT
459
460 /* Examine each GPE Register within the block */
461
462 for (i = 0; i < gpe_block->register_count; i++) {
c50f13c6 463 gpe_register_info = &gpe_block->register_info[i];
1da177e4 464
5a0b8dee
RW
465 /*
466 * Enable all "wake" GPEs in this register and disable the
467 * remaining ones.
468 */
1da177e4 469
c6b5774c 470 status =
c50f13c6
RW
471 acpi_hw_gpe_enable_write(gpe_register_info->enable_for_wake,
472 gpe_register_info);
4be44fcd 473 if (ACPI_FAILURE(status)) {
1da177e4
LT
474 return (status);
475 }
476 }
477
478 return (AE_OK);
479}
480
1da177e4
LT
481/******************************************************************************
482 *
483 * FUNCTION: acpi_hw_disable_all_gpes
484 *
73459f73 485 * PARAMETERS: None
1da177e4
LT
486 *
487 * RETURN: Status
488 *
44f6c012 489 * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
1da177e4
LT
490 *
491 ******************************************************************************/
492
4be44fcd 493acpi_status acpi_hw_disable_all_gpes(void)
1da177e4 494{
4be44fcd 495 acpi_status status;
1da177e4 496
b229cf92 497 ACPI_FUNCTION_TRACE(hw_disable_all_gpes);
1da177e4 498
e97d6bf1
BM
499 status = acpi_ev_walk_gpe_list(acpi_hw_disable_gpe_block, NULL);
500 status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
4be44fcd 501 return_ACPI_STATUS(status);
1da177e4
LT
502}
503
1da177e4
LT
504/******************************************************************************
505 *
506 * FUNCTION: acpi_hw_enable_all_runtime_gpes
507 *
73459f73 508 * PARAMETERS: None
1da177e4
LT
509 *
510 * RETURN: Status
511 *
44f6c012 512 * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
1da177e4
LT
513 *
514 ******************************************************************************/
515
4be44fcd 516acpi_status acpi_hw_enable_all_runtime_gpes(void)
1da177e4 517{
4be44fcd 518 acpi_status status;
1da177e4 519
b229cf92 520 ACPI_FUNCTION_TRACE(hw_enable_all_runtime_gpes);
1da177e4 521
e97d6bf1 522 status = acpi_ev_walk_gpe_list(acpi_hw_enable_runtime_gpe_block, NULL);
4be44fcd 523 return_ACPI_STATUS(status);
1da177e4
LT
524}
525
1da177e4
LT
526/******************************************************************************
527 *
528 * FUNCTION: acpi_hw_enable_all_wakeup_gpes
529 *
73459f73 530 * PARAMETERS: None
1da177e4
LT
531 *
532 * RETURN: Status
533 *
44f6c012 534 * DESCRIPTION: Enable all "wakeup" GPEs, in all GPE blocks
1da177e4
LT
535 *
536 ******************************************************************************/
537
4be44fcd 538acpi_status acpi_hw_enable_all_wakeup_gpes(void)
1da177e4 539{
4be44fcd 540 acpi_status status;
1da177e4 541
b229cf92 542 ACPI_FUNCTION_TRACE(hw_enable_all_wakeup_gpes);
1da177e4 543
e97d6bf1 544 status = acpi_ev_walk_gpe_list(acpi_hw_enable_wakeup_gpe_block, NULL);
4be44fcd 545 return_ACPI_STATUS(status);
1da177e4 546}
33620c54
BM
547
548#endif /* !ACPI_REDUCED_HARDWARE */