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