]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/acpi/acpica/evxfevnt.c
467fde961aefe164c748d2e37d48c474571391cb
[linux-beck.git] / drivers / acpi / acpica / evxfevnt.c
1 /******************************************************************************
2  *
3  * Module Name: evxfevnt - External Interfaces, ACPI event disable/enable
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2010, Intel Corp.
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>
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acnamesp.h"
48 #include "actables.h"
49
50 #define _COMPONENT          ACPI_EVENTS
51 ACPI_MODULE_NAME("evxfevnt")
52
53 /* Local prototypes */
54 static acpi_status
55 acpi_ev_get_gpe_device(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
56                        struct acpi_gpe_block_info *gpe_block, void *context);
57
58 /*******************************************************************************
59  *
60  * FUNCTION:    acpi_enable
61  *
62  * PARAMETERS:  None
63  *
64  * RETURN:      Status
65  *
66  * DESCRIPTION: Transfers the system into ACPI mode.
67  *
68  ******************************************************************************/
69
70 acpi_status acpi_enable(void)
71 {
72         acpi_status status;
73
74         ACPI_FUNCTION_TRACE(acpi_enable);
75
76         /* ACPI tables must be present */
77
78         if (!acpi_tb_tables_loaded()) {
79                 return_ACPI_STATUS(AE_NO_ACPI_TABLES);
80         }
81
82         /* Check current mode */
83
84         if (acpi_hw_get_mode() == ACPI_SYS_MODE_ACPI) {
85                 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
86                                   "System is already in ACPI mode\n"));
87                 return_ACPI_STATUS(AE_OK);
88         }
89
90         /* Transition to ACPI mode */
91
92         status = acpi_hw_set_mode(ACPI_SYS_MODE_ACPI);
93         if (ACPI_FAILURE(status)) {
94                 ACPI_ERROR((AE_INFO,
95                             "Could not transition to ACPI mode"));
96                 return_ACPI_STATUS(status);
97         }
98
99         /* Sanity check that transition succeeded */
100
101         if (acpi_hw_get_mode() != ACPI_SYS_MODE_ACPI) {
102                 ACPI_ERROR((AE_INFO,
103                             "Hardware did not enter ACPI mode"));
104                 return_ACPI_STATUS(AE_NO_HARDWARE_RESPONSE);
105         }
106
107         ACPI_DEBUG_PRINT((ACPI_DB_INIT,
108                           "Transition to ACPI mode successful\n"));
109
110         return_ACPI_STATUS(AE_OK);
111 }
112
113 ACPI_EXPORT_SYMBOL(acpi_enable)
114
115 /*******************************************************************************
116  *
117  * FUNCTION:    acpi_disable
118  *
119  * PARAMETERS:  None
120  *
121  * RETURN:      Status
122  *
123  * DESCRIPTION: Transfers the system into LEGACY (non-ACPI) mode.
124  *
125  ******************************************************************************/
126 acpi_status acpi_disable(void)
127 {
128         acpi_status status = AE_OK;
129
130         ACPI_FUNCTION_TRACE(acpi_disable);
131
132         if (acpi_hw_get_mode() == ACPI_SYS_MODE_LEGACY) {
133                 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
134                                   "System is already in legacy (non-ACPI) mode\n"));
135         } else {
136                 /* Transition to LEGACY mode */
137
138                 status = acpi_hw_set_mode(ACPI_SYS_MODE_LEGACY);
139
140                 if (ACPI_FAILURE(status)) {
141                         ACPI_ERROR((AE_INFO,
142                                     "Could not exit ACPI mode to legacy mode"));
143                         return_ACPI_STATUS(status);
144                 }
145
146                 ACPI_DEBUG_PRINT((ACPI_DB_INIT, "ACPI mode disabled\n"));
147         }
148
149         return_ACPI_STATUS(status);
150 }
151
152 ACPI_EXPORT_SYMBOL(acpi_disable)
153
154 /*******************************************************************************
155  *
156  * FUNCTION:    acpi_enable_event
157  *
158  * PARAMETERS:  Event           - The fixed eventto be enabled
159  *              Flags           - Reserved
160  *
161  * RETURN:      Status
162  *
163  * DESCRIPTION: Enable an ACPI event (fixed)
164  *
165  ******************************************************************************/
166 acpi_status acpi_enable_event(u32 event, u32 flags)
167 {
168         acpi_status status = AE_OK;
169         u32 value;
170
171         ACPI_FUNCTION_TRACE(acpi_enable_event);
172
173         /* Decode the Fixed Event */
174
175         if (event > ACPI_EVENT_MAX) {
176                 return_ACPI_STATUS(AE_BAD_PARAMETER);
177         }
178
179         /*
180          * Enable the requested fixed event (by writing a one to the enable
181          * register bit)
182          */
183         status =
184             acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
185                                     enable_register_id, ACPI_ENABLE_EVENT);
186         if (ACPI_FAILURE(status)) {
187                 return_ACPI_STATUS(status);
188         }
189
190         /* Make sure that the hardware responded */
191
192         status =
193             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
194                                    enable_register_id, &value);
195         if (ACPI_FAILURE(status)) {
196                 return_ACPI_STATUS(status);
197         }
198
199         if (value != 1) {
200                 ACPI_ERROR((AE_INFO,
201                             "Could not enable %s event",
202                             acpi_ut_get_event_name(event)));
203                 return_ACPI_STATUS(AE_NO_HARDWARE_RESPONSE);
204         }
205
206         return_ACPI_STATUS(status);
207 }
208
209 ACPI_EXPORT_SYMBOL(acpi_enable_event)
210
211 /*******************************************************************************
212  *
213  * FUNCTION:    acpi_clear_and_enable_gpe
214  *
215  * PARAMETERS:  gpe_event_info  - GPE to enable
216  *
217  * RETURN:      Status
218  *
219  * DESCRIPTION: Clear the given GPE from stale events and enable it.
220  *
221  ******************************************************************************/
222 static acpi_status
223 acpi_clear_and_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
224 {
225         acpi_status status;
226
227         /*
228          * We will only allow a GPE to be enabled if it has either an
229          * associated method (_Lxx/_Exx) or a handler. Otherwise, the
230          * GPE will be immediately disabled by acpi_ev_gpe_dispatch the
231          * first time it fires.
232          */
233         if (!(gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)) {
234                 return_ACPI_STATUS(AE_NO_HANDLER);
235         }
236
237         /* Clear the GPE (of stale events) */
238         status = acpi_hw_clear_gpe(gpe_event_info);
239         if (ACPI_FAILURE(status)) {
240                 return_ACPI_STATUS(status);
241         }
242
243         /* Enable the requested GPE */
244         status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE);
245
246         return_ACPI_STATUS(status);
247 }
248
249 /*******************************************************************************
250  *
251  * FUNCTION:    acpi_set_gpe
252  *
253  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
254  *              gpe_number      - GPE level within the GPE block
255  *              action          - ACPI_GPE_ENABLE or ACPI_GPE_DISABLE
256  *
257  * RETURN:      Status
258  *
259  * DESCRIPTION: Enable or disable an individual GPE. This function bypasses
260  *              the reference count mechanism used in the acpi_enable_gpe and
261  *              acpi_disable_gpe interfaces -- and should be used with care.
262  *
263  * Note: Typically used to disable a runtime GPE for short period of time,
264  * then re-enable it, without disturbing the existing reference counts. This
265  * is useful, for example, in the Embedded Controller (EC) driver.
266  *
267  ******************************************************************************/
268 acpi_status acpi_set_gpe(acpi_handle gpe_device, u32 gpe_number, u8 action)
269 {
270         struct acpi_gpe_event_info *gpe_event_info;
271         acpi_status status;
272         acpi_cpu_flags flags;
273
274         ACPI_FUNCTION_TRACE(acpi_set_gpe);
275
276         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
277
278         /* Ensure that we have a valid GPE number */
279
280         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
281         if (!gpe_event_info) {
282                 status = AE_BAD_PARAMETER;
283                 goto unlock_and_exit;
284         }
285
286         /* Perform the action */
287
288         switch (action) {
289         case ACPI_GPE_ENABLE:
290                 status = acpi_clear_and_enable_gpe(gpe_event_info);
291                 break;
292
293         case ACPI_GPE_DISABLE:
294                 status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
295                 break;
296
297         default:
298                 status = AE_BAD_PARAMETER;
299                 break;
300         }
301
302       unlock_and_exit:
303         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
304         return_ACPI_STATUS(status);
305 }
306
307 ACPI_EXPORT_SYMBOL(acpi_set_gpe)
308
309 /*******************************************************************************
310  *
311  * FUNCTION:    acpi_gpe_wakeup
312  *
313  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
314  *              gpe_number      - GPE level within the GPE block
315  *              Action          - Enable or Disable
316  *
317  * RETURN:      Status
318  *
319  * DESCRIPTION: Set or clear the GPE's wakeup enable mask bit.
320  *
321  ******************************************************************************/
322 acpi_status acpi_gpe_wakeup(acpi_handle gpe_device, u32 gpe_number, u8 action)
323 {
324         acpi_status status = AE_OK;
325         struct acpi_gpe_event_info *gpe_event_info;
326         struct acpi_gpe_register_info *gpe_register_info;
327         acpi_cpu_flags flags;
328         u32 register_bit;
329
330         ACPI_FUNCTION_TRACE(acpi_gpe_wakeup);
331
332         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
333
334         /* Ensure that we have a valid GPE number */
335
336         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
337         if (!gpe_event_info) {
338                 status = AE_BAD_PARAMETER;
339                 goto unlock_and_exit;
340         }
341
342         gpe_register_info = gpe_event_info->register_info;
343         if (!gpe_register_info) {
344                 status = AE_NOT_EXIST;
345                 goto unlock_and_exit;
346         }
347
348         register_bit =
349             acpi_hw_get_gpe_register_bit(gpe_event_info, gpe_register_info);
350
351         /* Perform the action */
352
353         switch (action) {
354         case ACPI_GPE_ENABLE:
355                 ACPI_SET_BIT(gpe_register_info->enable_for_wake,
356                              (u8)register_bit);
357                 break;
358
359         case ACPI_GPE_DISABLE:
360                 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
361                                (u8)register_bit);
362                 break;
363
364         default:
365                 ACPI_ERROR((AE_INFO, "%u, Invalid action", action));
366                 status = AE_BAD_PARAMETER;
367                 break;
368         }
369
370 unlock_and_exit:
371         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
372         return_ACPI_STATUS(status);
373 }
374
375 ACPI_EXPORT_SYMBOL(acpi_gpe_wakeup)
376
377 /*******************************************************************************
378  *
379  * FUNCTION:    acpi_enable_gpe
380  *
381  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
382  *              gpe_number      - GPE level within the GPE block
383  *
384  * RETURN:      Status
385  *
386  * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
387  *              hardware-enabled.
388  *
389  ******************************************************************************/
390 acpi_status acpi_enable_gpe(acpi_handle gpe_device, u32 gpe_number)
391 {
392         acpi_status status = AE_OK;
393         struct acpi_gpe_event_info *gpe_event_info;
394         acpi_cpu_flags flags;
395
396         ACPI_FUNCTION_TRACE(acpi_enable_gpe);
397
398         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
399
400         /* Ensure that we have a valid GPE number */
401
402         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
403         if (!gpe_event_info) {
404                 status = AE_BAD_PARAMETER;
405                 goto unlock_and_exit;
406         }
407
408         if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
409                 status = AE_LIMIT;      /* Too many references */
410                 goto unlock_and_exit;
411         }
412
413         gpe_event_info->runtime_count++;
414         if (gpe_event_info->runtime_count == 1) {
415                 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
416                 if (ACPI_SUCCESS(status)) {
417                         status = acpi_clear_and_enable_gpe(gpe_event_info);
418                 }
419                 if (ACPI_FAILURE(status)) {
420                         gpe_event_info->runtime_count--;
421                 }
422         }
423
424 unlock_and_exit:
425         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
426         return_ACPI_STATUS(status);
427 }
428 ACPI_EXPORT_SYMBOL(acpi_enable_gpe)
429
430 /*******************************************************************************
431  *
432  * FUNCTION:    acpi_disable_gpe
433  *
434  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
435  *              gpe_number      - GPE level within the GPE block
436  *
437  * RETURN:      Status
438  *
439  * DESCRIPTION: Remove a reference to a GPE. When the last reference is
440  *              removed, only then is the GPE disabled (for runtime GPEs), or
441  *              the GPE mask bit disabled (for wake GPEs)
442  *
443  ******************************************************************************/
444 acpi_status acpi_disable_gpe(acpi_handle gpe_device, u32 gpe_number)
445 {
446         acpi_status status = AE_OK;
447         struct acpi_gpe_event_info *gpe_event_info;
448         acpi_cpu_flags flags;
449
450         ACPI_FUNCTION_TRACE(acpi_disable_gpe);
451
452         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
453
454         /* Ensure that we have a valid GPE number */
455
456         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
457         if (!gpe_event_info) {
458                 status = AE_BAD_PARAMETER;
459                 goto unlock_and_exit;
460         }
461
462         /* Hardware-disable a runtime GPE on removal of the last reference */
463
464         if (!gpe_event_info->runtime_count) {
465                 status = AE_LIMIT;      /* There are no references to remove */
466                 goto unlock_and_exit;
467         }
468
469         gpe_event_info->runtime_count--;
470         if (!gpe_event_info->runtime_count) {
471                 status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
472                 if (ACPI_SUCCESS(status)) {
473                         status =
474                             acpi_hw_low_set_gpe(gpe_event_info,
475                                                 ACPI_GPE_DISABLE);
476                 }
477                 if (ACPI_FAILURE(status)) {
478                         gpe_event_info->runtime_count++;
479                 }
480         }
481
482 unlock_and_exit:
483         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
484         return_ACPI_STATUS(status);
485 }
486 ACPI_EXPORT_SYMBOL(acpi_disable_gpe)
487
488 /*******************************************************************************
489  *
490  * FUNCTION:    acpi_disable_event
491  *
492  * PARAMETERS:  Event           - The fixed eventto be enabled
493  *              Flags           - Reserved
494  *
495  * RETURN:      Status
496  *
497  * DESCRIPTION: Disable an ACPI event (fixed)
498  *
499  ******************************************************************************/
500 acpi_status acpi_disable_event(u32 event, u32 flags)
501 {
502         acpi_status status = AE_OK;
503         u32 value;
504
505         ACPI_FUNCTION_TRACE(acpi_disable_event);
506
507         /* Decode the Fixed Event */
508
509         if (event > ACPI_EVENT_MAX) {
510                 return_ACPI_STATUS(AE_BAD_PARAMETER);
511         }
512
513         /*
514          * Disable the requested fixed event (by writing a zero to the enable
515          * register bit)
516          */
517         status =
518             acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
519                                     enable_register_id, ACPI_DISABLE_EVENT);
520         if (ACPI_FAILURE(status)) {
521                 return_ACPI_STATUS(status);
522         }
523
524         status =
525             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
526                                    enable_register_id, &value);
527         if (ACPI_FAILURE(status)) {
528                 return_ACPI_STATUS(status);
529         }
530
531         if (value != 0) {
532                 ACPI_ERROR((AE_INFO,
533                             "Could not disable %s events",
534                             acpi_ut_get_event_name(event)));
535                 return_ACPI_STATUS(AE_NO_HARDWARE_RESPONSE);
536         }
537
538         return_ACPI_STATUS(status);
539 }
540
541 ACPI_EXPORT_SYMBOL(acpi_disable_event)
542
543 /*******************************************************************************
544  *
545  * FUNCTION:    acpi_clear_event
546  *
547  * PARAMETERS:  Event           - The fixed event to be cleared
548  *
549  * RETURN:      Status
550  *
551  * DESCRIPTION: Clear an ACPI event (fixed)
552  *
553  ******************************************************************************/
554 acpi_status acpi_clear_event(u32 event)
555 {
556         acpi_status status = AE_OK;
557
558         ACPI_FUNCTION_TRACE(acpi_clear_event);
559
560         /* Decode the Fixed Event */
561
562         if (event > ACPI_EVENT_MAX) {
563                 return_ACPI_STATUS(AE_BAD_PARAMETER);
564         }
565
566         /*
567          * Clear the requested fixed event (By writing a one to the status
568          * register bit)
569          */
570         status =
571             acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
572                                     status_register_id, ACPI_CLEAR_STATUS);
573
574         return_ACPI_STATUS(status);
575 }
576
577 ACPI_EXPORT_SYMBOL(acpi_clear_event)
578
579 /*******************************************************************************
580  *
581  * FUNCTION:    acpi_clear_gpe
582  *
583  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
584  *              gpe_number      - GPE level within the GPE block
585  *
586  * RETURN:      Status
587  *
588  * DESCRIPTION: Clear an ACPI event (general purpose)
589  *
590  ******************************************************************************/
591 acpi_status acpi_clear_gpe(acpi_handle gpe_device, u32 gpe_number)
592 {
593         acpi_status status = AE_OK;
594         struct acpi_gpe_event_info *gpe_event_info;
595         acpi_cpu_flags flags;
596
597         ACPI_FUNCTION_TRACE(acpi_clear_gpe);
598
599         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
600
601         /* Ensure that we have a valid GPE number */
602
603         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
604         if (!gpe_event_info) {
605                 status = AE_BAD_PARAMETER;
606                 goto unlock_and_exit;
607         }
608
609         status = acpi_hw_clear_gpe(gpe_event_info);
610
611       unlock_and_exit:
612         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
613         return_ACPI_STATUS(status);
614 }
615
616 ACPI_EXPORT_SYMBOL(acpi_clear_gpe)
617 /*******************************************************************************
618  *
619  * FUNCTION:    acpi_get_event_status
620  *
621  * PARAMETERS:  Event           - The fixed event
622  *              event_status    - Where the current status of the event will
623  *                                be returned
624  *
625  * RETURN:      Status
626  *
627  * DESCRIPTION: Obtains and returns the current status of the event
628  *
629  ******************************************************************************/
630 acpi_status acpi_get_event_status(u32 event, acpi_event_status * event_status)
631 {
632         acpi_status status = AE_OK;
633         u32 value;
634
635         ACPI_FUNCTION_TRACE(acpi_get_event_status);
636
637         if (!event_status) {
638                 return_ACPI_STATUS(AE_BAD_PARAMETER);
639         }
640
641         /* Decode the Fixed Event */
642
643         if (event > ACPI_EVENT_MAX) {
644                 return_ACPI_STATUS(AE_BAD_PARAMETER);
645         }
646
647         /* Get the status of the requested fixed event */
648
649         status =
650             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
651                               enable_register_id, &value);
652         if (ACPI_FAILURE(status))
653                 return_ACPI_STATUS(status);
654
655         *event_status = value;
656
657         status =
658             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
659                               status_register_id, &value);
660         if (ACPI_FAILURE(status))
661                 return_ACPI_STATUS(status);
662
663         if (value)
664                 *event_status |= ACPI_EVENT_FLAG_SET;
665
666         if (acpi_gbl_fixed_event_handlers[event].handler)
667                 *event_status |= ACPI_EVENT_FLAG_HANDLE;
668
669         return_ACPI_STATUS(status);
670 }
671
672 ACPI_EXPORT_SYMBOL(acpi_get_event_status)
673
674 /*******************************************************************************
675  *
676  * FUNCTION:    acpi_get_gpe_status
677  *
678  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
679  *              gpe_number      - GPE level within the GPE block
680  *              event_status    - Where the current status of the event will
681  *                                be returned
682  *
683  * RETURN:      Status
684  *
685  * DESCRIPTION: Get status of an event (general purpose)
686  *
687  ******************************************************************************/
688 acpi_status
689 acpi_get_gpe_status(acpi_handle gpe_device,
690                     u32 gpe_number, acpi_event_status *event_status)
691 {
692         acpi_status status = AE_OK;
693         struct acpi_gpe_event_info *gpe_event_info;
694         acpi_cpu_flags flags;
695
696         ACPI_FUNCTION_TRACE(acpi_get_gpe_status);
697
698         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
699
700         /* Ensure that we have a valid GPE number */
701
702         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
703         if (!gpe_event_info) {
704                 status = AE_BAD_PARAMETER;
705                 goto unlock_and_exit;
706         }
707
708         /* Obtain status on the requested GPE number */
709
710         status = acpi_hw_get_gpe_status(gpe_event_info, event_status);
711
712         if (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)
713                 *event_status |= ACPI_EVENT_FLAG_HANDLE;
714
715       unlock_and_exit:
716         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
717         return_ACPI_STATUS(status);
718 }
719
720 ACPI_EXPORT_SYMBOL(acpi_get_gpe_status)
721 /*******************************************************************************
722  *
723  * FUNCTION:    acpi_install_gpe_block
724  *
725  * PARAMETERS:  gpe_device          - Handle to the parent GPE Block Device
726  *              gpe_block_address   - Address and space_iD
727  *              register_count      - Number of GPE register pairs in the block
728  *              interrupt_number    - H/W interrupt for the block
729  *
730  * RETURN:      Status
731  *
732  * DESCRIPTION: Create and Install a block of GPE registers
733  *
734  ******************************************************************************/
735 acpi_status
736 acpi_install_gpe_block(acpi_handle gpe_device,
737                        struct acpi_generic_address *gpe_block_address,
738                        u32 register_count, u32 interrupt_number)
739 {
740         acpi_status status;
741         union acpi_operand_object *obj_desc;
742         struct acpi_namespace_node *node;
743         struct acpi_gpe_block_info *gpe_block;
744
745         ACPI_FUNCTION_TRACE(acpi_install_gpe_block);
746
747         if ((!gpe_device) || (!gpe_block_address) || (!register_count)) {
748                 return_ACPI_STATUS(AE_BAD_PARAMETER);
749         }
750
751         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
752         if (ACPI_FAILURE(status)) {
753                 return (status);
754         }
755
756         node = acpi_ns_validate_handle(gpe_device);
757         if (!node) {
758                 status = AE_BAD_PARAMETER;
759                 goto unlock_and_exit;
760         }
761
762         /*
763          * For user-installed GPE Block Devices, the gpe_block_base_number
764          * is always zero
765          */
766         status =
767             acpi_ev_create_gpe_block(node, gpe_block_address, register_count, 0,
768                                      interrupt_number, &gpe_block);
769         if (ACPI_FAILURE(status)) {
770                 goto unlock_and_exit;
771         }
772
773         /* Install block in the device_object attached to the node */
774
775         obj_desc = acpi_ns_get_attached_object(node);
776         if (!obj_desc) {
777
778                 /*
779                  * No object, create a new one (Device nodes do not always have
780                  * an attached object)
781                  */
782                 obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_DEVICE);
783                 if (!obj_desc) {
784                         status = AE_NO_MEMORY;
785                         goto unlock_and_exit;
786                 }
787
788                 status =
789                     acpi_ns_attach_object(node, obj_desc, ACPI_TYPE_DEVICE);
790
791                 /* Remove local reference to the object */
792
793                 acpi_ut_remove_reference(obj_desc);
794
795                 if (ACPI_FAILURE(status)) {
796                         goto unlock_and_exit;
797                 }
798         }
799
800         /* Now install the GPE block in the device_object */
801
802         obj_desc->device.gpe_block = gpe_block;
803
804         /* Run the _PRW methods and enable the runtime GPEs in the new block */
805
806         status = acpi_ev_initialize_gpe_block(node, gpe_block);
807
808       unlock_and_exit:
809         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
810         return_ACPI_STATUS(status);
811 }
812
813 ACPI_EXPORT_SYMBOL(acpi_install_gpe_block)
814
815 /*******************************************************************************
816  *
817  * FUNCTION:    acpi_remove_gpe_block
818  *
819  * PARAMETERS:  gpe_device          - Handle to the parent GPE Block Device
820  *
821  * RETURN:      Status
822  *
823  * DESCRIPTION: Remove a previously installed block of GPE registers
824  *
825  ******************************************************************************/
826 acpi_status acpi_remove_gpe_block(acpi_handle gpe_device)
827 {
828         union acpi_operand_object *obj_desc;
829         acpi_status status;
830         struct acpi_namespace_node *node;
831
832         ACPI_FUNCTION_TRACE(acpi_remove_gpe_block);
833
834         if (!gpe_device) {
835                 return_ACPI_STATUS(AE_BAD_PARAMETER);
836         }
837
838         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
839         if (ACPI_FAILURE(status)) {
840                 return (status);
841         }
842
843         node = acpi_ns_validate_handle(gpe_device);
844         if (!node) {
845                 status = AE_BAD_PARAMETER;
846                 goto unlock_and_exit;
847         }
848
849         /* Get the device_object attached to the node */
850
851         obj_desc = acpi_ns_get_attached_object(node);
852         if (!obj_desc || !obj_desc->device.gpe_block) {
853                 return_ACPI_STATUS(AE_NULL_OBJECT);
854         }
855
856         /* Delete the GPE block (but not the device_object) */
857
858         status = acpi_ev_delete_gpe_block(obj_desc->device.gpe_block);
859         if (ACPI_SUCCESS(status)) {
860                 obj_desc->device.gpe_block = NULL;
861         }
862
863       unlock_and_exit:
864         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
865         return_ACPI_STATUS(status);
866 }
867
868 ACPI_EXPORT_SYMBOL(acpi_remove_gpe_block)
869
870 /*******************************************************************************
871  *
872  * FUNCTION:    acpi_get_gpe_device
873  *
874  * PARAMETERS:  Index               - System GPE index (0-current_gpe_count)
875  *              gpe_device          - Where the parent GPE Device is returned
876  *
877  * RETURN:      Status
878  *
879  * DESCRIPTION: Obtain the GPE device associated with the input index. A NULL
880  *              gpe device indicates that the gpe number is contained in one of
881  *              the FADT-defined gpe blocks. Otherwise, the GPE block device.
882  *
883  ******************************************************************************/
884 acpi_status
885 acpi_get_gpe_device(u32 index, acpi_handle *gpe_device)
886 {
887         struct acpi_gpe_device_info info;
888         acpi_status status;
889
890         ACPI_FUNCTION_TRACE(acpi_get_gpe_device);
891
892         if (!gpe_device) {
893                 return_ACPI_STATUS(AE_BAD_PARAMETER);
894         }
895
896         if (index >= acpi_current_gpe_count) {
897                 return_ACPI_STATUS(AE_NOT_EXIST);
898         }
899
900         /* Setup and walk the GPE list */
901
902         info.index = index;
903         info.status = AE_NOT_EXIST;
904         info.gpe_device = NULL;
905         info.next_block_base_index = 0;
906
907         status = acpi_ev_walk_gpe_list(acpi_ev_get_gpe_device, &info);
908         if (ACPI_FAILURE(status)) {
909                 return_ACPI_STATUS(status);
910         }
911
912         *gpe_device = info.gpe_device;
913         return_ACPI_STATUS(info.status);
914 }
915
916 ACPI_EXPORT_SYMBOL(acpi_get_gpe_device)
917
918 /*******************************************************************************
919  *
920  * FUNCTION:    acpi_ev_get_gpe_device
921  *
922  * PARAMETERS:  GPE_WALK_CALLBACK
923  *
924  * RETURN:      Status
925  *
926  * DESCRIPTION: Matches the input GPE index (0-current_gpe_count) with a GPE
927  *              block device. NULL if the GPE is one of the FADT-defined GPEs.
928  *
929  ******************************************************************************/
930 static acpi_status
931 acpi_ev_get_gpe_device(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
932                        struct acpi_gpe_block_info *gpe_block, void *context)
933 {
934         struct acpi_gpe_device_info *info = context;
935
936         /* Increment Index by the number of GPEs in this block */
937
938         info->next_block_base_index += gpe_block->gpe_count;
939
940         if (info->index < info->next_block_base_index) {
941                 /*
942                  * The GPE index is within this block, get the node. Leave the node
943                  * NULL for the FADT-defined GPEs
944                  */
945                 if ((gpe_block->node)->type == ACPI_TYPE_DEVICE) {
946                         info->gpe_device = gpe_block->node;
947                 }
948
949                 info->status = AE_OK;
950                 return (AE_CTRL_END);
951         }
952
953         return (AE_OK);
954 }
955
956 /******************************************************************************
957  *
958  * FUNCTION:    acpi_disable_all_gpes
959  *
960  * PARAMETERS:  None
961  *
962  * RETURN:      Status
963  *
964  * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
965  *
966  ******************************************************************************/
967
968 acpi_status acpi_disable_all_gpes(void)
969 {
970         acpi_status status;
971
972         ACPI_FUNCTION_TRACE(acpi_disable_all_gpes);
973
974         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
975         if (ACPI_FAILURE(status)) {
976                 return_ACPI_STATUS(status);
977         }
978
979         status = acpi_hw_disable_all_gpes();
980         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
981
982         return_ACPI_STATUS(status);
983 }
984
985 /******************************************************************************
986  *
987  * FUNCTION:    acpi_enable_all_runtime_gpes
988  *
989  * PARAMETERS:  None
990  *
991  * RETURN:      Status
992  *
993  * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
994  *
995  ******************************************************************************/
996
997 acpi_status acpi_enable_all_runtime_gpes(void)
998 {
999         acpi_status status;
1000
1001         ACPI_FUNCTION_TRACE(acpi_enable_all_runtime_gpes);
1002
1003         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
1004         if (ACPI_FAILURE(status)) {
1005                 return_ACPI_STATUS(status);
1006         }
1007
1008         status = acpi_hw_enable_all_runtime_gpes();
1009         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
1010
1011         return_ACPI_STATUS(status);
1012 }