ACPICA: Events: Reduce indent divergences of events files.
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / acpica / evxfgpe.c
1 /******************************************************************************
2  *
3  * Module Name: evxfgpe - External Interfaces for General Purpose Events (GPEs)
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, 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 #define EXPORT_ACPI_INTERFACES
45
46 #include <acpi/acpi.h>
47 #include "accommon.h"
48 #include "acevents.h"
49 #include "acnamesp.h"
50
51 #define _COMPONENT          ACPI_EVENTS
52 ACPI_MODULE_NAME("evxfgpe")
53
54 #if (!ACPI_REDUCED_HARDWARE)    /* Entire module */
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_update_all_gpes
58  *
59  * PARAMETERS:  None
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Complete GPE initialization and enable all GPEs that have
64  *              associated _Lxx or _Exx methods and are not pointed to by any
65  *              device _PRW methods (this indicates that these GPEs are
66  *              generally intended for system or device wakeup. Such GPEs
67  *              have to be enabled directly when the devices whose _PRW
68  *              methods point to them are set up for wakeup signaling.)
69  *
70  * NOTE: Should be called after any GPEs are added to the system. Primarily,
71  * after the system _PRW methods have been run, but also after a GPE Block
72  * Device has been added or if any new GPE methods have been added via a
73  * dynamic table load.
74  *
75  ******************************************************************************/
76
77 acpi_status acpi_update_all_gpes(void)
78 {
79         acpi_status status;
80
81         ACPI_FUNCTION_TRACE(acpi_update_all_gpes);
82
83         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
84         if (ACPI_FAILURE(status)) {
85                 return_ACPI_STATUS(status);
86         }
87
88         if (acpi_gbl_all_gpes_initialized) {
89                 goto unlock_and_exit;
90         }
91
92         status = acpi_ev_walk_gpe_list(acpi_ev_initialize_gpe_block, NULL);
93         if (ACPI_SUCCESS(status)) {
94                 acpi_gbl_all_gpes_initialized = TRUE;
95         }
96
97 unlock_and_exit:
98         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
99
100         return_ACPI_STATUS(status);
101 }
102
103 ACPI_EXPORT_SYMBOL(acpi_update_all_gpes)
104
105 /*******************************************************************************
106  *
107  * FUNCTION:    acpi_enable_gpe
108  *
109  * PARAMETERS:  gpe_device          - Parent GPE Device. NULL for GPE0/GPE1
110  *              gpe_number          - GPE level within the GPE block
111  *
112  * RETURN:      Status
113  *
114  * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
115  *              hardware-enabled.
116  *
117  ******************************************************************************/
118 acpi_status acpi_enable_gpe(acpi_handle gpe_device, u32 gpe_number)
119 {
120         acpi_status status = AE_BAD_PARAMETER;
121         struct acpi_gpe_event_info *gpe_event_info;
122         acpi_cpu_flags flags;
123
124         ACPI_FUNCTION_TRACE(acpi_enable_gpe);
125
126         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
127
128         /*
129          * Ensure that we have a valid GPE number and that there is some way
130          * of handling the GPE (handler or a GPE method). In other words, we
131          * won't allow a valid GPE to be enabled if there is no way to handle it.
132          */
133         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
134         if (gpe_event_info) {
135                 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
136                     ACPI_GPE_DISPATCH_NONE) {
137                         status = acpi_ev_add_gpe_reference(gpe_event_info);
138                 } else {
139                         status = AE_NO_HANDLER;
140                 }
141         }
142
143         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
144         return_ACPI_STATUS(status);
145 }
146 ACPI_EXPORT_SYMBOL(acpi_enable_gpe)
147
148 /*******************************************************************************
149  *
150  * FUNCTION:    acpi_disable_gpe
151  *
152  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
153  *              gpe_number      - GPE level within the GPE block
154  *
155  * RETURN:      Status
156  *
157  * DESCRIPTION: Remove a reference to a GPE. When the last reference is
158  *              removed, only then is the GPE disabled (for runtime GPEs), or
159  *              the GPE mask bit disabled (for wake GPEs)
160  *
161  ******************************************************************************/
162
163 acpi_status acpi_disable_gpe(acpi_handle gpe_device, u32 gpe_number)
164 {
165         acpi_status status = AE_BAD_PARAMETER;
166         struct acpi_gpe_event_info *gpe_event_info;
167         acpi_cpu_flags flags;
168
169         ACPI_FUNCTION_TRACE(acpi_disable_gpe);
170
171         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
172
173         /* Ensure that we have a valid GPE number */
174
175         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
176         if (gpe_event_info) {
177                 status = acpi_ev_remove_gpe_reference(gpe_event_info) ;
178         }
179
180         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
181         return_ACPI_STATUS(status);
182 }
183
184 ACPI_EXPORT_SYMBOL(acpi_disable_gpe)
185
186
187 /*******************************************************************************
188  *
189  * FUNCTION:    acpi_mark_gpe_for_wake
190  *
191  * PARAMETERS:  gpe_device          - Parent GPE Device. NULL for GPE0/GPE1
192  *              gpe_number          - GPE level within the GPE block
193  *
194  * RETURN:      Status
195  *
196  * DESCRIPTION: Mark a GPE as having the ability to wake the system. Simply
197  *              sets the ACPI_GPE_CAN_WAKE flag.
198  *
199  * Some potential callers of acpi_setup_gpe_for_wake may know in advance that
200  * there won't be any notify handlers installed for device wake notifications
201  * from the given GPE (one example is a button GPE in Linux). For these cases,
202  * acpi_mark_gpe_for_wake should be used instead of acpi_setup_gpe_for_wake.
203  * This will set the ACPI_GPE_CAN_WAKE flag for the GPE without trying to
204  * setup implicit wake notification for it (since there's no handler method).
205  *
206  ******************************************************************************/
207 acpi_status acpi_mark_gpe_for_wake(acpi_handle gpe_device, u32 gpe_number)
208 {
209         struct acpi_gpe_event_info *gpe_event_info;
210         acpi_status status = AE_BAD_PARAMETER;
211         acpi_cpu_flags flags;
212
213         ACPI_FUNCTION_TRACE(acpi_mark_gpe_for_wake);
214
215         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
216
217         /* Ensure that we have a valid GPE number */
218
219         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
220         if (gpe_event_info) {
221
222                 /* Mark the GPE as a possible wake event */
223
224                 gpe_event_info->flags |= ACPI_GPE_CAN_WAKE;
225                 status = AE_OK;
226         }
227
228         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
229         return_ACPI_STATUS(status);
230 }
231
232 ACPI_EXPORT_SYMBOL(acpi_mark_gpe_for_wake)
233
234 /*******************************************************************************
235  *
236  * FUNCTION:    acpi_setup_gpe_for_wake
237  *
238  * PARAMETERS:  wake_device         - Device associated with the GPE (via _PRW)
239  *              gpe_device          - Parent GPE Device. NULL for GPE0/GPE1
240  *              gpe_number          - GPE level within the GPE block
241  *
242  * RETURN:      Status
243  *
244  * DESCRIPTION: Mark a GPE as having the ability to wake the system. This
245  *              interface is intended to be used as the host executes the
246  *              _PRW methods (Power Resources for Wake) in the system tables.
247  *              Each _PRW appears under a Device Object (The wake_device), and
248  *              contains the info for the wake GPE associated with the
249  *              wake_device.
250  *
251  ******************************************************************************/
252 acpi_status
253 acpi_setup_gpe_for_wake(acpi_handle wake_device,
254                         acpi_handle gpe_device, u32 gpe_number)
255 {
256         acpi_status status;
257         struct acpi_gpe_event_info *gpe_event_info;
258         struct acpi_namespace_node *device_node;
259         struct acpi_gpe_notify_info *notify;
260         struct acpi_gpe_notify_info *new_notify;
261         acpi_cpu_flags flags;
262
263         ACPI_FUNCTION_TRACE(acpi_setup_gpe_for_wake);
264
265         /* Parameter Validation */
266
267         if (!wake_device) {
268                 /*
269                  * By forcing wake_device to be valid, we automatically enable the
270                  * implicit notify feature on all hosts.
271                  */
272                 return_ACPI_STATUS(AE_BAD_PARAMETER);
273         }
274
275         /* Handle root object case */
276
277         if (wake_device == ACPI_ROOT_OBJECT) {
278                 device_node = acpi_gbl_root_node;
279         } else {
280                 device_node =
281                     ACPI_CAST_PTR(struct acpi_namespace_node, wake_device);
282         }
283
284         /* Validate wake_device is of type Device */
285
286         if (device_node->type != ACPI_TYPE_DEVICE) {
287                 return_ACPI_STATUS (AE_BAD_PARAMETER);
288         }
289
290         /*
291          * Allocate a new notify object up front, in case it is needed.
292          * Memory allocation while holding a spinlock is a big no-no
293          * on some hosts.
294          */
295         new_notify = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_notify_info));
296         if (!new_notify) {
297                 return_ACPI_STATUS(AE_NO_MEMORY);
298         }
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         /*
311          * If there is no method or handler for this GPE, then the
312          * wake_device will be notified whenever this GPE fires. This is
313          * known as an "implicit notify". Note: The GPE is assumed to be
314          * level-triggered (for windows compatibility).
315          */
316         if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
317             ACPI_GPE_DISPATCH_NONE) {
318                 /*
319                  * This is the first device for implicit notify on this GPE.
320                  * Just set the flags here, and enter the NOTIFY block below.
321                  */
322                 gpe_event_info->flags =
323                     (ACPI_GPE_DISPATCH_NOTIFY | ACPI_GPE_LEVEL_TRIGGERED);
324         }
325
326         /*
327          * If we already have an implicit notify on this GPE, add
328          * this device to the notify list.
329          */
330         if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
331             ACPI_GPE_DISPATCH_NOTIFY) {
332
333                 /* Ensure that the device is not already in the list */
334
335                 notify = gpe_event_info->dispatch.notify_list;
336                 while (notify) {
337                         if (notify->device_node == device_node) {
338                                 status = AE_ALREADY_EXISTS;
339                                 goto unlock_and_exit;
340                         }
341                         notify = notify->next;
342                 }
343
344                 /* Add this device to the notify list for this GPE */
345
346                 new_notify->device_node = device_node;
347                 new_notify->next = gpe_event_info->dispatch.notify_list;
348                 gpe_event_info->dispatch.notify_list = new_notify;
349                 new_notify = NULL;
350         }
351
352         /* Mark the GPE as a possible wake event */
353
354         gpe_event_info->flags |= ACPI_GPE_CAN_WAKE;
355         status = AE_OK;
356
357 unlock_and_exit:
358         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
359
360         /* Delete the notify object if it was not used above */
361
362         if (new_notify) {
363                 ACPI_FREE(new_notify);
364         }
365         return_ACPI_STATUS(status);
366 }
367 ACPI_EXPORT_SYMBOL(acpi_setup_gpe_for_wake)
368
369 /*******************************************************************************
370  *
371  * FUNCTION:    acpi_set_gpe_wake_mask
372  *
373  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
374  *              gpe_number      - GPE level within the GPE block
375  *              action              - Enable or Disable
376  *
377  * RETURN:      Status
378  *
379  * DESCRIPTION: Set or clear the GPE's wakeup enable mask bit. The GPE must
380  *              already be marked as a WAKE GPE.
381  *
382  ******************************************************************************/
383
384 acpi_status
385 acpi_set_gpe_wake_mask(acpi_handle gpe_device, u32 gpe_number, u8 action)
386 {
387         acpi_status status = AE_OK;
388         struct acpi_gpe_event_info *gpe_event_info;
389         struct acpi_gpe_register_info *gpe_register_info;
390         acpi_cpu_flags flags;
391         u32 register_bit;
392
393         ACPI_FUNCTION_TRACE(acpi_set_gpe_wake_mask);
394
395         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
396
397         /*
398          * Ensure that we have a valid GPE number and that this GPE is in
399          * fact a wake GPE
400          */
401         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
402         if (!gpe_event_info) {
403                 status = AE_BAD_PARAMETER;
404                 goto unlock_and_exit;
405         }
406
407         if (!(gpe_event_info->flags & ACPI_GPE_CAN_WAKE)) {
408                 status = AE_TYPE;
409                 goto unlock_and_exit;
410         }
411
412         gpe_register_info = gpe_event_info->register_info;
413         if (!gpe_register_info) {
414                 status = AE_NOT_EXIST;
415                 goto unlock_and_exit;
416         }
417
418         register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
419
420         /* Perform the action */
421
422         switch (action) {
423         case ACPI_GPE_ENABLE:
424
425                 ACPI_SET_BIT(gpe_register_info->enable_for_wake,
426                              (u8)register_bit);
427                 break;
428
429         case ACPI_GPE_DISABLE:
430
431                 ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake,
432                                (u8)register_bit);
433                 break;
434
435         default:
436
437                 ACPI_ERROR((AE_INFO, "%u, Invalid action", action));
438                 status = AE_BAD_PARAMETER;
439                 break;
440         }
441
442 unlock_and_exit:
443         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
444         return_ACPI_STATUS(status);
445 }
446
447 ACPI_EXPORT_SYMBOL(acpi_set_gpe_wake_mask)
448
449 /*******************************************************************************
450  *
451  * FUNCTION:    acpi_clear_gpe
452  *
453  * PARAMETERS:  gpe_device      - Parent GPE Device. NULL for GPE0/GPE1
454  *              gpe_number      - GPE level within the GPE block
455  *
456  * RETURN:      Status
457  *
458  * DESCRIPTION: Clear an ACPI event (general purpose)
459  *
460  ******************************************************************************/
461 acpi_status acpi_clear_gpe(acpi_handle gpe_device, u32 gpe_number)
462 {
463         acpi_status status = AE_OK;
464         struct acpi_gpe_event_info *gpe_event_info;
465         acpi_cpu_flags flags;
466
467         ACPI_FUNCTION_TRACE(acpi_clear_gpe);
468
469         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
470
471         /* Ensure that we have a valid GPE number */
472
473         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
474         if (!gpe_event_info) {
475                 status = AE_BAD_PARAMETER;
476                 goto unlock_and_exit;
477         }
478
479         status = acpi_hw_clear_gpe(gpe_event_info);
480
481       unlock_and_exit:
482         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
483         return_ACPI_STATUS(status);
484 }
485
486 ACPI_EXPORT_SYMBOL(acpi_clear_gpe)
487
488 /*******************************************************************************
489  *
490  * FUNCTION:    acpi_get_gpe_status
491  *
492  * PARAMETERS:  gpe_device          - Parent GPE Device. NULL for GPE0/GPE1
493  *              gpe_number          - GPE level within the GPE block
494  *              event_status        - Where the current status of the event
495  *                                    will be returned
496  *
497  * RETURN:      Status
498  *
499  * DESCRIPTION: Get the current status of a GPE (signalled/not_signalled)
500  *
501  ******************************************************************************/
502 acpi_status
503 acpi_get_gpe_status(acpi_handle gpe_device,
504                     u32 gpe_number, acpi_event_status *event_status)
505 {
506         acpi_status status = AE_OK;
507         struct acpi_gpe_event_info *gpe_event_info;
508         acpi_cpu_flags flags;
509
510         ACPI_FUNCTION_TRACE(acpi_get_gpe_status);
511
512         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
513
514         /* Ensure that we have a valid GPE number */
515
516         gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
517         if (!gpe_event_info) {
518                 status = AE_BAD_PARAMETER;
519                 goto unlock_and_exit;
520         }
521
522         /* Obtain status on the requested GPE number */
523
524         status = acpi_hw_get_gpe_status(gpe_event_info, event_status);
525
526         if (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)
527                 *event_status |= ACPI_EVENT_FLAG_HANDLE;
528
529 unlock_and_exit:
530         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
531         return_ACPI_STATUS(status);
532 }
533
534 ACPI_EXPORT_SYMBOL(acpi_get_gpe_status)
535
536 /******************************************************************************
537  *
538  * FUNCTION:    acpi_disable_all_gpes
539  *
540  * PARAMETERS:  None
541  *
542  * RETURN:      Status
543  *
544  * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
545  *
546  ******************************************************************************/
547
548 acpi_status acpi_disable_all_gpes(void)
549 {
550         acpi_status status;
551
552         ACPI_FUNCTION_TRACE(acpi_disable_all_gpes);
553
554         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
555         if (ACPI_FAILURE(status)) {
556                 return_ACPI_STATUS(status);
557         }
558
559         status = acpi_hw_disable_all_gpes();
560         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
561
562         return_ACPI_STATUS(status);
563 }
564
565 ACPI_EXPORT_SYMBOL(acpi_disable_all_gpes)
566
567 /******************************************************************************
568  *
569  * FUNCTION:    acpi_enable_all_runtime_gpes
570  *
571  * PARAMETERS:  None
572  *
573  * RETURN:      Status
574  *
575  * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
576  *
577  ******************************************************************************/
578
579 acpi_status acpi_enable_all_runtime_gpes(void)
580 {
581         acpi_status status;
582
583         ACPI_FUNCTION_TRACE(acpi_enable_all_runtime_gpes);
584
585         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
586         if (ACPI_FAILURE(status)) {
587                 return_ACPI_STATUS(status);
588         }
589
590         status = acpi_hw_enable_all_runtime_gpes();
591         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
592
593         return_ACPI_STATUS(status);
594 }
595
596 ACPI_EXPORT_SYMBOL(acpi_enable_all_runtime_gpes)
597
598 /******************************************************************************
599  *
600  * FUNCTION:    acpi_enable_all_wakeup_gpes
601  *
602  * PARAMETERS:  None
603  *
604  * RETURN:      Status
605  *
606  * DESCRIPTION: Enable all "wakeup" GPEs and disable all of the other GPEs, in
607  *              all GPE blocks.
608  *
609  ******************************************************************************/
610
611 acpi_status acpi_enable_all_wakeup_gpes(void)
612 {
613         acpi_status status;
614
615         ACPI_FUNCTION_TRACE(acpi_enable_all_wakeup_gpes);
616
617         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
618         if (ACPI_FAILURE(status)) {
619                 return_ACPI_STATUS(status);
620         }
621
622         status = acpi_hw_enable_all_wakeup_gpes();
623         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
624
625         return_ACPI_STATUS(status);
626 }
627
628 ACPI_EXPORT_SYMBOL(acpi_enable_all_wakeup_gpes)
629
630 /*******************************************************************************
631  *
632  * FUNCTION:    acpi_install_gpe_block
633  *
634  * PARAMETERS:  gpe_device          - Handle to the parent GPE Block Device
635  *              gpe_block_address   - Address and space_ID
636  *              register_count      - Number of GPE register pairs in the block
637  *              interrupt_number    - H/W interrupt for the block
638  *
639  * RETURN:      Status
640  *
641  * DESCRIPTION: Create and Install a block of GPE registers. The GPEs are not
642  *              enabled here.
643  *
644  ******************************************************************************/
645 acpi_status
646 acpi_install_gpe_block(acpi_handle gpe_device,
647                        struct acpi_generic_address *gpe_block_address,
648                        u32 register_count, u32 interrupt_number)
649 {
650         acpi_status status;
651         union acpi_operand_object *obj_desc;
652         struct acpi_namespace_node *node;
653         struct acpi_gpe_block_info *gpe_block;
654
655         ACPI_FUNCTION_TRACE(acpi_install_gpe_block);
656
657         if ((!gpe_device) || (!gpe_block_address) || (!register_count)) {
658                 return_ACPI_STATUS(AE_BAD_PARAMETER);
659         }
660
661         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
662         if (ACPI_FAILURE(status)) {
663                 return_ACPI_STATUS(status);
664         }
665
666         node = acpi_ns_validate_handle(gpe_device);
667         if (!node) {
668                 status = AE_BAD_PARAMETER;
669                 goto unlock_and_exit;
670         }
671
672         /* Validate the parent device */
673
674         if (node->type != ACPI_TYPE_DEVICE) {
675                 status = AE_TYPE;
676                 goto unlock_and_exit;
677         }
678
679         if (node->object) {
680                 status = AE_ALREADY_EXISTS;
681                 goto unlock_and_exit;
682         }
683
684         /*
685          * For user-installed GPE Block Devices, the gpe_block_base_number
686          * is always zero
687          */
688         status = acpi_ev_create_gpe_block(node, gpe_block_address->address,
689                                           gpe_block_address->space_id,
690                                           register_count, 0, interrupt_number,
691                                           &gpe_block);
692         if (ACPI_FAILURE(status)) {
693                 goto unlock_and_exit;
694         }
695
696         /* Install block in the device_object attached to the node */
697
698         obj_desc = acpi_ns_get_attached_object(node);
699         if (!obj_desc) {
700
701                 /*
702                  * No object, create a new one (Device nodes do not always have
703                  * an attached object)
704                  */
705                 obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_DEVICE);
706                 if (!obj_desc) {
707                         status = AE_NO_MEMORY;
708                         goto unlock_and_exit;
709                 }
710
711                 status =
712                     acpi_ns_attach_object(node, obj_desc, ACPI_TYPE_DEVICE);
713
714                 /* Remove local reference to the object */
715
716                 acpi_ut_remove_reference(obj_desc);
717
718                 if (ACPI_FAILURE(status)) {
719                         goto unlock_and_exit;
720                 }
721         }
722
723         /* Now install the GPE block in the device_object */
724
725         obj_desc->device.gpe_block = gpe_block;
726
727 unlock_and_exit:
728         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
729         return_ACPI_STATUS(status);
730 }
731
732 ACPI_EXPORT_SYMBOL(acpi_install_gpe_block)
733
734 /*******************************************************************************
735  *
736  * FUNCTION:    acpi_remove_gpe_block
737  *
738  * PARAMETERS:  gpe_device          - Handle to the parent GPE Block Device
739  *
740  * RETURN:      Status
741  *
742  * DESCRIPTION: Remove a previously installed block of GPE registers
743  *
744  ******************************************************************************/
745 acpi_status acpi_remove_gpe_block(acpi_handle gpe_device)
746 {
747         union acpi_operand_object *obj_desc;
748         acpi_status status;
749         struct acpi_namespace_node *node;
750
751         ACPI_FUNCTION_TRACE(acpi_remove_gpe_block);
752
753         if (!gpe_device) {
754                 return_ACPI_STATUS(AE_BAD_PARAMETER);
755         }
756
757         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
758         if (ACPI_FAILURE(status)) {
759                 return_ACPI_STATUS(status);
760         }
761
762         node = acpi_ns_validate_handle(gpe_device);
763         if (!node) {
764                 status = AE_BAD_PARAMETER;
765                 goto unlock_and_exit;
766         }
767
768         /* Validate the parent device */
769
770         if (node->type != ACPI_TYPE_DEVICE) {
771                 status = AE_TYPE;
772                 goto unlock_and_exit;
773         }
774
775         /* Get the device_object attached to the node */
776
777         obj_desc = acpi_ns_get_attached_object(node);
778         if (!obj_desc || !obj_desc->device.gpe_block) {
779                 return_ACPI_STATUS(AE_NULL_OBJECT);
780         }
781
782         /* Delete the GPE block (but not the device_object) */
783
784         status = acpi_ev_delete_gpe_block(obj_desc->device.gpe_block);
785         if (ACPI_SUCCESS(status)) {
786                 obj_desc->device.gpe_block = NULL;
787         }
788
789 unlock_and_exit:
790         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
791         return_ACPI_STATUS(status);
792 }
793
794 ACPI_EXPORT_SYMBOL(acpi_remove_gpe_block)
795
796 /*******************************************************************************
797  *
798  * FUNCTION:    acpi_get_gpe_device
799  *
800  * PARAMETERS:  index               - System GPE index (0-current_gpe_count)
801  *              gpe_device          - Where the parent GPE Device is returned
802  *
803  * RETURN:      Status
804  *
805  * DESCRIPTION: Obtain the GPE device associated with the input index. A NULL
806  *              gpe device indicates that the gpe number is contained in one of
807  *              the FADT-defined gpe blocks. Otherwise, the GPE block device.
808  *
809  ******************************************************************************/
810 acpi_status acpi_get_gpe_device(u32 index, acpi_handle * gpe_device)
811 {
812         struct acpi_gpe_device_info info;
813         acpi_status status;
814
815         ACPI_FUNCTION_TRACE(acpi_get_gpe_device);
816
817         if (!gpe_device) {
818                 return_ACPI_STATUS(AE_BAD_PARAMETER);
819         }
820
821         if (index >= acpi_current_gpe_count) {
822                 return_ACPI_STATUS(AE_NOT_EXIST);
823         }
824
825         /* Setup and walk the GPE list */
826
827         info.index = index;
828         info.status = AE_NOT_EXIST;
829         info.gpe_device = NULL;
830         info.next_block_base_index = 0;
831
832         status = acpi_ev_walk_gpe_list(acpi_ev_get_gpe_device, &info);
833         if (ACPI_FAILURE(status)) {
834                 return_ACPI_STATUS(status);
835         }
836
837         *gpe_device = ACPI_CAST_PTR(acpi_handle, info.gpe_device);
838         return_ACPI_STATUS(info.status);
839 }
840
841 ACPI_EXPORT_SYMBOL(acpi_get_gpe_device)
842 #endif                          /* !ACPI_REDUCED_HARDWARE */