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