]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/acpi/acpica/evxfevnt.c
ACPI / ACPICA: Fix reference counting problems with GPE handlers
[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_gpe_wakeup
214  *
215  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
216  *              gpe_number      - GPE level within the GPE block
217  *              Action          - Enable or Disable
218  *
219  * RETURN:      Status
220  *
221  * DESCRIPTION: Set or clear the GPE's wakeup enable mask bit.
222  *
223  ******************************************************************************/
224 acpi_status acpi_gpe_wakeup(acpi_handle gpe_device, u32 gpe_number, u8 action)
225 {
226         acpi_status status = AE_OK;
227         struct acpi_gpe_event_info *gpe_event_info;
228         struct acpi_gpe_register_info *gpe_register_info;
229         acpi_cpu_flags flags;
230         u32 register_bit;
231
232         ACPI_FUNCTION_TRACE(acpi_gpe_wakeup);
233
234         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
235
236         /* Ensure that we have a valid GPE number */
237
238         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
239         if (!gpe_event_info || !(gpe_event_info->flags & ACPI_GPE_CAN_WAKE)) {
240                 status = AE_BAD_PARAMETER;
241                 goto unlock_and_exit;
242         }
243
244         gpe_register_info = gpe_event_info->register_info;
245         if (!gpe_register_info) {
246                 status = AE_NOT_EXIST;
247                 goto unlock_and_exit;
248         }
249
250         register_bit =
251             acpi_hw_get_gpe_register_bit(gpe_event_info, gpe_register_info);
252
253         /* Perform the action */
254
255         switch (action) {
256         case ACPI_GPE_ENABLE:
257                 ACPI_SET_BIT(gpe_register_info->enable_for_wake,
258                              (u8)register_bit);
259                 break;
260
261         case ACPI_GPE_DISABLE:
262                 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
263                                (u8)register_bit);
264                 break;
265
266         default:
267                 ACPI_ERROR((AE_INFO, "%u, Invalid action", action));
268                 status = AE_BAD_PARAMETER;
269                 break;
270         }
271
272 unlock_and_exit:
273         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
274         return_ACPI_STATUS(status);
275 }
276
277 ACPI_EXPORT_SYMBOL(acpi_gpe_wakeup)
278
279 /*******************************************************************************
280  *
281  * FUNCTION:    acpi_enable_gpe
282  *
283  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
284  *              gpe_number      - GPE level within the GPE block
285  *
286  * RETURN:      Status
287  *
288  * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
289  *              hardware-enabled.
290  *
291  ******************************************************************************/
292 acpi_status acpi_enable_gpe(acpi_handle gpe_device, u32 gpe_number)
293 {
294         acpi_status status = AE_BAD_PARAMETER;
295         struct acpi_gpe_event_info *gpe_event_info;
296         acpi_cpu_flags flags;
297
298         ACPI_FUNCTION_TRACE(acpi_enable_gpe);
299
300         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
301
302         /* Ensure that we have a valid GPE number */
303
304         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
305         if (gpe_event_info) {
306                 status = acpi_raw_enable_gpe(gpe_event_info);
307         }
308
309         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
310         return_ACPI_STATUS(status);
311 }
312 ACPI_EXPORT_SYMBOL(acpi_enable_gpe)
313
314 /*******************************************************************************
315  *
316  * FUNCTION:    acpi_disable_gpe
317  *
318  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
319  *              gpe_number      - GPE level within the GPE block
320  *
321  * RETURN:      Status
322  *
323  * DESCRIPTION: Remove a reference to a GPE. When the last reference is
324  *              removed, only then is the GPE disabled (for runtime GPEs), or
325  *              the GPE mask bit disabled (for wake GPEs)
326  *
327  ******************************************************************************/
328 acpi_status acpi_disable_gpe(acpi_handle gpe_device, u32 gpe_number)
329 {
330         acpi_status status = AE_BAD_PARAMETER;
331         struct acpi_gpe_event_info *gpe_event_info;
332         acpi_cpu_flags flags;
333
334         ACPI_FUNCTION_TRACE(acpi_disable_gpe);
335
336         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
337
338         /* Ensure that we have a valid GPE number */
339
340         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
341         if (gpe_event_info) {
342                 status = acpi_raw_disable_gpe(gpe_event_info) ;
343         }
344
345         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
346         return_ACPI_STATUS(status);
347 }
348 ACPI_EXPORT_SYMBOL(acpi_disable_gpe)
349
350 /*******************************************************************************
351  *
352  * FUNCTION:    acpi_gpe_can_wake
353  *
354  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
355  *              gpe_number      - GPE level within the GPE block
356  *
357  * RETURN:      Status
358  *
359  * DESCRIPTION: Set the ACPI_GPE_CAN_WAKE flag for the given GPE.  If the GPE
360  *              has a corresponding method and is currently enabled, disable it
361  *              (GPEs with corresponding methods are enabled unconditionally
362  *              during initialization, but GPEs that can wake up are expected
363  *              to be initially disabled).
364  *
365  ******************************************************************************/
366 acpi_status acpi_gpe_can_wake(acpi_handle gpe_device, u32 gpe_number)
367 {
368         acpi_status status = AE_OK;
369         struct acpi_gpe_event_info *gpe_event_info;
370         acpi_cpu_flags flags;
371
372         ACPI_FUNCTION_TRACE(acpi_gpe_can_wake);
373
374         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
375
376         /* Ensure that we have a valid GPE number */
377
378         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
379         if (!gpe_event_info) {
380                 status = AE_BAD_PARAMETER;
381                 goto unlock_and_exit;
382         }
383
384         if (gpe_event_info->flags & ACPI_GPE_CAN_WAKE) {
385                 goto unlock_and_exit;
386         }
387
388         gpe_event_info->flags |= ACPI_GPE_CAN_WAKE;
389         if (gpe_event_info->flags & ACPI_GPE_DISPATCH_METHOD) {
390                 (void)acpi_raw_disable_gpe(gpe_event_info);
391         }
392
393 unlock_and_exit:
394         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
395         return_ACPI_STATUS(status);
396 }
397 ACPI_EXPORT_SYMBOL(acpi_gpe_can_wake)
398
399 /*******************************************************************************
400  *
401  * FUNCTION:    acpi_disable_event
402  *
403  * PARAMETERS:  Event           - The fixed eventto be enabled
404  *              Flags           - Reserved
405  *
406  * RETURN:      Status
407  *
408  * DESCRIPTION: Disable an ACPI event (fixed)
409  *
410  ******************************************************************************/
411 acpi_status acpi_disable_event(u32 event, u32 flags)
412 {
413         acpi_status status = AE_OK;
414         u32 value;
415
416         ACPI_FUNCTION_TRACE(acpi_disable_event);
417
418         /* Decode the Fixed Event */
419
420         if (event > ACPI_EVENT_MAX) {
421                 return_ACPI_STATUS(AE_BAD_PARAMETER);
422         }
423
424         /*
425          * Disable the requested fixed event (by writing a zero to the enable
426          * register bit)
427          */
428         status =
429             acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
430                                     enable_register_id, ACPI_DISABLE_EVENT);
431         if (ACPI_FAILURE(status)) {
432                 return_ACPI_STATUS(status);
433         }
434
435         status =
436             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
437                                    enable_register_id, &value);
438         if (ACPI_FAILURE(status)) {
439                 return_ACPI_STATUS(status);
440         }
441
442         if (value != 0) {
443                 ACPI_ERROR((AE_INFO,
444                             "Could not disable %s events",
445                             acpi_ut_get_event_name(event)));
446                 return_ACPI_STATUS(AE_NO_HARDWARE_RESPONSE);
447         }
448
449         return_ACPI_STATUS(status);
450 }
451
452 ACPI_EXPORT_SYMBOL(acpi_disable_event)
453
454 /*******************************************************************************
455  *
456  * FUNCTION:    acpi_clear_event
457  *
458  * PARAMETERS:  Event           - The fixed event to be cleared
459  *
460  * RETURN:      Status
461  *
462  * DESCRIPTION: Clear an ACPI event (fixed)
463  *
464  ******************************************************************************/
465 acpi_status acpi_clear_event(u32 event)
466 {
467         acpi_status status = AE_OK;
468
469         ACPI_FUNCTION_TRACE(acpi_clear_event);
470
471         /* Decode the Fixed Event */
472
473         if (event > ACPI_EVENT_MAX) {
474                 return_ACPI_STATUS(AE_BAD_PARAMETER);
475         }
476
477         /*
478          * Clear the requested fixed event (By writing a one to the status
479          * register bit)
480          */
481         status =
482             acpi_write_bit_register(acpi_gbl_fixed_event_info[event].
483                                     status_register_id, ACPI_CLEAR_STATUS);
484
485         return_ACPI_STATUS(status);
486 }
487
488 ACPI_EXPORT_SYMBOL(acpi_clear_event)
489
490 /*******************************************************************************
491  *
492  * FUNCTION:    acpi_clear_gpe
493  *
494  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
495  *              gpe_number      - GPE level within the GPE block
496  *
497  * RETURN:      Status
498  *
499  * DESCRIPTION: Clear an ACPI event (general purpose)
500  *
501  ******************************************************************************/
502 acpi_status acpi_clear_gpe(acpi_handle gpe_device, u32 gpe_number)
503 {
504         acpi_status status = AE_OK;
505         struct acpi_gpe_event_info *gpe_event_info;
506         acpi_cpu_flags flags;
507
508         ACPI_FUNCTION_TRACE(acpi_clear_gpe);
509
510         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
511
512         /* Ensure that we have a valid GPE number */
513
514         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
515         if (!gpe_event_info) {
516                 status = AE_BAD_PARAMETER;
517                 goto unlock_and_exit;
518         }
519
520         status = acpi_hw_clear_gpe(gpe_event_info);
521
522       unlock_and_exit:
523         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
524         return_ACPI_STATUS(status);
525 }
526
527 ACPI_EXPORT_SYMBOL(acpi_clear_gpe)
528 /*******************************************************************************
529  *
530  * FUNCTION:    acpi_get_event_status
531  *
532  * PARAMETERS:  Event           - The fixed event
533  *              event_status    - Where the current status of the event will
534  *                                be returned
535  *
536  * RETURN:      Status
537  *
538  * DESCRIPTION: Obtains and returns the current status of the event
539  *
540  ******************************************************************************/
541 acpi_status acpi_get_event_status(u32 event, acpi_event_status * event_status)
542 {
543         acpi_status status = AE_OK;
544         u32 value;
545
546         ACPI_FUNCTION_TRACE(acpi_get_event_status);
547
548         if (!event_status) {
549                 return_ACPI_STATUS(AE_BAD_PARAMETER);
550         }
551
552         /* Decode the Fixed Event */
553
554         if (event > ACPI_EVENT_MAX) {
555                 return_ACPI_STATUS(AE_BAD_PARAMETER);
556         }
557
558         /* Get the status of the requested fixed event */
559
560         status =
561             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
562                               enable_register_id, &value);
563         if (ACPI_FAILURE(status))
564                 return_ACPI_STATUS(status);
565
566         *event_status = value;
567
568         status =
569             acpi_read_bit_register(acpi_gbl_fixed_event_info[event].
570                               status_register_id, &value);
571         if (ACPI_FAILURE(status))
572                 return_ACPI_STATUS(status);
573
574         if (value)
575                 *event_status |= ACPI_EVENT_FLAG_SET;
576
577         if (acpi_gbl_fixed_event_handlers[event].handler)
578                 *event_status |= ACPI_EVENT_FLAG_HANDLE;
579
580         return_ACPI_STATUS(status);
581 }
582
583 ACPI_EXPORT_SYMBOL(acpi_get_event_status)
584
585 /*******************************************************************************
586  *
587  * FUNCTION:    acpi_get_gpe_status
588  *
589  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
590  *              gpe_number      - GPE level within the GPE block
591  *              event_status    - Where the current status of the event will
592  *                                be returned
593  *
594  * RETURN:      Status
595  *
596  * DESCRIPTION: Get status of an event (general purpose)
597  *
598  ******************************************************************************/
599 acpi_status
600 acpi_get_gpe_status(acpi_handle gpe_device,
601                     u32 gpe_number, acpi_event_status *event_status)
602 {
603         acpi_status status = AE_OK;
604         struct acpi_gpe_event_info *gpe_event_info;
605         acpi_cpu_flags flags;
606
607         ACPI_FUNCTION_TRACE(acpi_get_gpe_status);
608
609         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
610
611         /* Ensure that we have a valid GPE number */
612
613         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
614         if (!gpe_event_info) {
615                 status = AE_BAD_PARAMETER;
616                 goto unlock_and_exit;
617         }
618
619         /* Obtain status on the requested GPE number */
620
621         status = acpi_hw_get_gpe_status(gpe_event_info, event_status);
622
623         if (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)
624                 *event_status |= ACPI_EVENT_FLAG_HANDLE;
625
626       unlock_and_exit:
627         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
628         return_ACPI_STATUS(status);
629 }
630
631 ACPI_EXPORT_SYMBOL(acpi_get_gpe_status)
632 /*******************************************************************************
633  *
634  * FUNCTION:    acpi_install_gpe_block
635  *
636  * PARAMETERS:  gpe_device          - Handle to the parent GPE Block Device
637  *              gpe_block_address   - Address and space_iD
638  *              register_count      - Number of GPE register pairs in the block
639  *              interrupt_number    - H/W interrupt for the block
640  *
641  * RETURN:      Status
642  *
643  * DESCRIPTION: Create and Install a block of GPE registers
644  *
645  ******************************************************************************/
646 acpi_status
647 acpi_install_gpe_block(acpi_handle gpe_device,
648                        struct acpi_generic_address *gpe_block_address,
649                        u32 register_count, u32 interrupt_number)
650 {
651         acpi_status status;
652         union acpi_operand_object *obj_desc;
653         struct acpi_namespace_node *node;
654         struct acpi_gpe_block_info *gpe_block;
655
656         ACPI_FUNCTION_TRACE(acpi_install_gpe_block);
657
658         if ((!gpe_device) || (!gpe_block_address) || (!register_count)) {
659                 return_ACPI_STATUS(AE_BAD_PARAMETER);
660         }
661
662         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
663         if (ACPI_FAILURE(status)) {
664                 return (status);
665         }
666
667         node = acpi_ns_validate_handle(gpe_device);
668         if (!node) {
669                 status = AE_BAD_PARAMETER;
670                 goto unlock_and_exit;
671         }
672
673         /*
674          * For user-installed GPE Block Devices, the gpe_block_base_number
675          * is always zero
676          */
677         status =
678             acpi_ev_create_gpe_block(node, gpe_block_address, register_count, 0,
679                                      interrupt_number, &gpe_block);
680         if (ACPI_FAILURE(status)) {
681                 goto unlock_and_exit;
682         }
683
684         /* Install block in the device_object attached to the node */
685
686         obj_desc = acpi_ns_get_attached_object(node);
687         if (!obj_desc) {
688
689                 /*
690                  * No object, create a new one (Device nodes do not always have
691                  * an attached object)
692                  */
693                 obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_DEVICE);
694                 if (!obj_desc) {
695                         status = AE_NO_MEMORY;
696                         goto unlock_and_exit;
697                 }
698
699                 status =
700                     acpi_ns_attach_object(node, obj_desc, ACPI_TYPE_DEVICE);
701
702                 /* Remove local reference to the object */
703
704                 acpi_ut_remove_reference(obj_desc);
705
706                 if (ACPI_FAILURE(status)) {
707                         goto unlock_and_exit;
708                 }
709         }
710
711         /* Now install the GPE block in the device_object */
712
713         obj_desc->device.gpe_block = gpe_block;
714
715         /* Enable the runtime GPEs in the new block */
716
717         status = acpi_ev_initialize_gpe_block(node, gpe_block);
718
719       unlock_and_exit:
720         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
721         return_ACPI_STATUS(status);
722 }
723
724 ACPI_EXPORT_SYMBOL(acpi_install_gpe_block)
725
726 /*******************************************************************************
727  *
728  * FUNCTION:    acpi_remove_gpe_block
729  *
730  * PARAMETERS:  gpe_device          - Handle to the parent GPE Block Device
731  *
732  * RETURN:      Status
733  *
734  * DESCRIPTION: Remove a previously installed block of GPE registers
735  *
736  ******************************************************************************/
737 acpi_status acpi_remove_gpe_block(acpi_handle gpe_device)
738 {
739         union acpi_operand_object *obj_desc;
740         acpi_status status;
741         struct acpi_namespace_node *node;
742
743         ACPI_FUNCTION_TRACE(acpi_remove_gpe_block);
744
745         if (!gpe_device) {
746                 return_ACPI_STATUS(AE_BAD_PARAMETER);
747         }
748
749         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
750         if (ACPI_FAILURE(status)) {
751                 return (status);
752         }
753
754         node = acpi_ns_validate_handle(gpe_device);
755         if (!node) {
756                 status = AE_BAD_PARAMETER;
757                 goto unlock_and_exit;
758         }
759
760         /* Get the device_object attached to the node */
761
762         obj_desc = acpi_ns_get_attached_object(node);
763         if (!obj_desc || !obj_desc->device.gpe_block) {
764                 return_ACPI_STATUS(AE_NULL_OBJECT);
765         }
766
767         /* Delete the GPE block (but not the device_object) */
768
769         status = acpi_ev_delete_gpe_block(obj_desc->device.gpe_block);
770         if (ACPI_SUCCESS(status)) {
771                 obj_desc->device.gpe_block = NULL;
772         }
773
774       unlock_and_exit:
775         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
776         return_ACPI_STATUS(status);
777 }
778
779 ACPI_EXPORT_SYMBOL(acpi_remove_gpe_block)
780
781 /*******************************************************************************
782  *
783  * FUNCTION:    acpi_get_gpe_device
784  *
785  * PARAMETERS:  Index               - System GPE index (0-current_gpe_count)
786  *              gpe_device          - Where the parent GPE Device is returned
787  *
788  * RETURN:      Status
789  *
790  * DESCRIPTION: Obtain the GPE device associated with the input index. A NULL
791  *              gpe device indicates that the gpe number is contained in one of
792  *              the FADT-defined gpe blocks. Otherwise, the GPE block device.
793  *
794  ******************************************************************************/
795 acpi_status
796 acpi_get_gpe_device(u32 index, acpi_handle *gpe_device)
797 {
798         struct acpi_gpe_device_info info;
799         acpi_status status;
800
801         ACPI_FUNCTION_TRACE(acpi_get_gpe_device);
802
803         if (!gpe_device) {
804                 return_ACPI_STATUS(AE_BAD_PARAMETER);
805         }
806
807         if (index >= acpi_current_gpe_count) {
808                 return_ACPI_STATUS(AE_NOT_EXIST);
809         }
810
811         /* Setup and walk the GPE list */
812
813         info.index = index;
814         info.status = AE_NOT_EXIST;
815         info.gpe_device = NULL;
816         info.next_block_base_index = 0;
817
818         status = acpi_ev_walk_gpe_list(acpi_ev_get_gpe_device, &info);
819         if (ACPI_FAILURE(status)) {
820                 return_ACPI_STATUS(status);
821         }
822
823         *gpe_device = info.gpe_device;
824         return_ACPI_STATUS(info.status);
825 }
826
827 ACPI_EXPORT_SYMBOL(acpi_get_gpe_device)
828
829 /*******************************************************************************
830  *
831  * FUNCTION:    acpi_ev_get_gpe_device
832  *
833  * PARAMETERS:  GPE_WALK_CALLBACK
834  *
835  * RETURN:      Status
836  *
837  * DESCRIPTION: Matches the input GPE index (0-current_gpe_count) with a GPE
838  *              block device. NULL if the GPE is one of the FADT-defined GPEs.
839  *
840  ******************************************************************************/
841 static acpi_status
842 acpi_ev_get_gpe_device(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
843                        struct acpi_gpe_block_info *gpe_block, void *context)
844 {
845         struct acpi_gpe_device_info *info = context;
846
847         /* Increment Index by the number of GPEs in this block */
848
849         info->next_block_base_index += gpe_block->gpe_count;
850
851         if (info->index < info->next_block_base_index) {
852                 /*
853                  * The GPE index is within this block, get the node. Leave the node
854                  * NULL for the FADT-defined GPEs
855                  */
856                 if ((gpe_block->node)->type == ACPI_TYPE_DEVICE) {
857                         info->gpe_device = gpe_block->node;
858                 }
859
860                 info->status = AE_OK;
861                 return (AE_CTRL_END);
862         }
863
864         return (AE_OK);
865 }
866
867 /******************************************************************************
868  *
869  * FUNCTION:    acpi_disable_all_gpes
870  *
871  * PARAMETERS:  None
872  *
873  * RETURN:      Status
874  *
875  * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
876  *
877  ******************************************************************************/
878
879 acpi_status acpi_disable_all_gpes(void)
880 {
881         acpi_status status;
882
883         ACPI_FUNCTION_TRACE(acpi_disable_all_gpes);
884
885         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
886         if (ACPI_FAILURE(status)) {
887                 return_ACPI_STATUS(status);
888         }
889
890         status = acpi_hw_disable_all_gpes();
891         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
892
893         return_ACPI_STATUS(status);
894 }
895
896 /******************************************************************************
897  *
898  * FUNCTION:    acpi_enable_all_runtime_gpes
899  *
900  * PARAMETERS:  None
901  *
902  * RETURN:      Status
903  *
904  * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
905  *
906  ******************************************************************************/
907
908 acpi_status acpi_enable_all_runtime_gpes(void)
909 {
910         acpi_status status;
911
912         ACPI_FUNCTION_TRACE(acpi_enable_all_runtime_gpes);
913
914         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
915         if (ACPI_FAILURE(status)) {
916                 return_ACPI_STATUS(status);
917         }
918
919         status = acpi_hw_enable_all_runtime_gpes();
920         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
921
922         return_ACPI_STATUS(status);
923 }