]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/acpi/acpica/evgpe.c
ACPI: Use GPE reference counting to support shared GPEs
[net-next-2.6.git] / drivers / acpi / acpica / evgpe.c
1 /******************************************************************************
2  *
3  * Module Name: evgpe - General Purpose Event handling and dispatch
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2008, 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
49 #define _COMPONENT          ACPI_EVENTS
50 ACPI_MODULE_NAME("evgpe")
51
52 /* Local prototypes */
53 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_ev_update_gpe_enable_masks
58  *
59  * PARAMETERS:  gpe_event_info          - GPE to update
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Updates GPE register enable masks based on the GPE type
64  *
65  ******************************************************************************/
66
67 acpi_status
68 acpi_ev_update_gpe_enable_masks(struct acpi_gpe_event_info *gpe_event_info)
69 {
70         struct acpi_gpe_register_info *gpe_register_info;
71         u8 register_bit;
72
73         ACPI_FUNCTION_TRACE(ev_update_gpe_enable_masks);
74
75         gpe_register_info = gpe_event_info->register_info;
76         if (!gpe_register_info) {
77                 return_ACPI_STATUS(AE_NOT_EXIST);
78         }
79
80         register_bit = (u8)
81             (1 <<
82              (gpe_event_info->gpe_number - gpe_register_info->base_gpe_number));
83
84         ACPI_CLEAR_BIT(gpe_register_info->enable_for_wake, register_bit);
85         ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
86
87         if (gpe_event_info->runtime_count)
88                 ACPI_SET_BIT(gpe_register_info->enable_for_run, register_bit);
89
90         if (gpe_event_info->wakeup_count)
91                 ACPI_SET_BIT(gpe_register_info->enable_for_wake, register_bit);
92
93         return_ACPI_STATUS(AE_OK);
94 }
95
96 /*******************************************************************************
97  *
98  * FUNCTION:    acpi_ev_enable_gpe
99  *
100  * PARAMETERS:  gpe_event_info          - GPE to enable
101  *              write_to_hardware       - Enable now, or just mark data structs
102  *                                        (WAKE GPEs should be deferred)
103  *
104  * RETURN:      Status
105  *
106  * DESCRIPTION: Enable a GPE based on the GPE type
107  *
108  ******************************************************************************/
109
110 acpi_status
111 acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info,
112                    u8 write_to_hardware)
113 {
114         acpi_status status;
115
116         ACPI_FUNCTION_TRACE(ev_enable_gpe);
117
118         /* Make sure HW enable masks are updated */
119
120         status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
121         if (ACPI_FAILURE(status))
122                 return_ACPI_STATUS(status);
123
124         /* Mark wake-enabled or HW enable, or both */
125
126         if (gpe_event_info->runtime_count && write_to_hardware) {
127                 /* Clear the GPE (of stale events), then enable it */
128                 status = acpi_hw_clear_gpe(gpe_event_info);
129                 if (ACPI_FAILURE(status))
130                         return_ACPI_STATUS(status);
131
132                 /* Enable the requested runtime GPE */
133                 status = acpi_hw_write_gpe_enable_reg(gpe_event_info);
134         }
135
136         return_ACPI_STATUS(AE_OK);
137 }
138
139 /*******************************************************************************
140  *
141  * FUNCTION:    acpi_ev_disable_gpe
142  *
143  * PARAMETERS:  gpe_event_info          - GPE to disable
144  *
145  * RETURN:      Status
146  *
147  * DESCRIPTION: Disable a GPE based on the GPE type
148  *
149  ******************************************************************************/
150
151 acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
152 {
153         acpi_status status;
154
155         ACPI_FUNCTION_TRACE(ev_disable_gpe);
156
157         /* Make sure HW enable masks are updated */
158
159         status = acpi_ev_update_gpe_enable_masks(gpe_event_info);
160         if (ACPI_FAILURE(status))
161                 return_ACPI_STATUS(status);
162
163         /*
164          * Even if we don't know the GPE type, make sure that we always
165          * disable it. low_disable_gpe will just clear the enable bit for this
166          * GPE and write it. It will not write out the current GPE enable mask,
167          * since this may inadvertently enable GPEs too early, if a rogue GPE has
168          * come in during ACPICA initialization - possibly as a result of AML or
169          * other code that has enabled the GPE.
170          */
171         status = acpi_hw_low_disable_gpe(gpe_event_info);
172         return_ACPI_STATUS(status);
173 }
174
175 /*******************************************************************************
176  *
177  * FUNCTION:    acpi_ev_get_gpe_event_info
178  *
179  * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
180  *              gpe_number          - Raw GPE number
181  *
182  * RETURN:      A GPE event_info struct. NULL if not a valid GPE
183  *
184  * DESCRIPTION: Returns the event_info struct associated with this GPE.
185  *              Validates the gpe_block and the gpe_number
186  *
187  *              Should be called only when the GPE lists are semaphore locked
188  *              and not subject to change.
189  *
190  ******************************************************************************/
191
192 struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
193                                                        u32 gpe_number)
194 {
195         union acpi_operand_object *obj_desc;
196         struct acpi_gpe_block_info *gpe_block;
197         u32 i;
198
199         ACPI_FUNCTION_ENTRY();
200
201         /* A NULL gpe_block means use the FADT-defined GPE block(s) */
202
203         if (!gpe_device) {
204
205                 /* Examine GPE Block 0 and 1 (These blocks are permanent) */
206
207                 for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
208                         gpe_block = acpi_gbl_gpe_fadt_blocks[i];
209                         if (gpe_block) {
210                                 if ((gpe_number >= gpe_block->block_base_number)
211                                     && (gpe_number <
212                                         gpe_block->block_base_number +
213                                         (gpe_block->register_count * 8))) {
214                                         return (&gpe_block->
215                                                 event_info[gpe_number -
216                                                            gpe_block->
217                                                            block_base_number]);
218                                 }
219                         }
220                 }
221
222                 /* The gpe_number was not in the range of either FADT GPE block */
223
224                 return (NULL);
225         }
226
227         /* A Non-NULL gpe_device means this is a GPE Block Device */
228
229         obj_desc = acpi_ns_get_attached_object((struct acpi_namespace_node *)
230                                                gpe_device);
231         if (!obj_desc || !obj_desc->device.gpe_block) {
232                 return (NULL);
233         }
234
235         gpe_block = obj_desc->device.gpe_block;
236
237         if ((gpe_number >= gpe_block->block_base_number) &&
238             (gpe_number <
239              gpe_block->block_base_number + (gpe_block->register_count * 8))) {
240                 return (&gpe_block->
241                         event_info[gpe_number - gpe_block->block_base_number]);
242         }
243
244         return (NULL);
245 }
246
247 /*******************************************************************************
248  *
249  * FUNCTION:    acpi_ev_gpe_detect
250  *
251  * PARAMETERS:  gpe_xrupt_list      - Interrupt block for this interrupt.
252  *                                    Can have multiple GPE blocks attached.
253  *
254  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
255  *
256  * DESCRIPTION: Detect if any GP events have occurred. This function is
257  *              executed at interrupt level.
258  *
259  ******************************************************************************/
260
261 u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
262 {
263         acpi_status status;
264         struct acpi_gpe_block_info *gpe_block;
265         struct acpi_gpe_register_info *gpe_register_info;
266         u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
267         u8 enabled_status_byte;
268         u32 status_reg;
269         u32 enable_reg;
270         acpi_cpu_flags flags;
271         u32 i;
272         u32 j;
273
274         ACPI_FUNCTION_NAME(ev_gpe_detect);
275
276         /* Check for the case where there are no GPEs */
277
278         if (!gpe_xrupt_list) {
279                 return (int_status);
280         }
281
282         /*
283          * We need to obtain the GPE lock for both the data structs and registers
284          * Note: Not necessary to obtain the hardware lock, since the GPE
285          * registers are owned by the gpe_lock.
286          */
287         flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
288
289         /* Examine all GPE blocks attached to this interrupt level */
290
291         gpe_block = gpe_xrupt_list->gpe_block_list_head;
292         while (gpe_block) {
293                 /*
294                  * Read all of the 8-bit GPE status and enable registers in this GPE
295                  * block, saving all of them. Find all currently active GP events.
296                  */
297                 for (i = 0; i < gpe_block->register_count; i++) {
298
299                         /* Get the next status/enable pair */
300
301                         gpe_register_info = &gpe_block->register_info[i];
302
303                         /* Read the Status Register */
304
305                         status =
306                             acpi_hw_read(&status_reg,
307                                          &gpe_register_info->status_address);
308                         if (ACPI_FAILURE(status)) {
309                                 goto unlock_and_exit;
310                         }
311
312                         /* Read the Enable Register */
313
314                         status =
315                             acpi_hw_read(&enable_reg,
316                                          &gpe_register_info->enable_address);
317                         if (ACPI_FAILURE(status)) {
318                                 goto unlock_and_exit;
319                         }
320
321                         ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
322                                           "Read GPE Register at GPE%X: Status=%02X, Enable=%02X\n",
323                                           gpe_register_info->base_gpe_number,
324                                           status_reg, enable_reg));
325
326                         /* Check if there is anything active at all in this register */
327
328                         enabled_status_byte = (u8) (status_reg & enable_reg);
329                         if (!enabled_status_byte) {
330
331                                 /* No active GPEs in this register, move on */
332
333                                 continue;
334                         }
335
336                         /* Now look at the individual GPEs in this byte register */
337
338                         for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
339
340                                 /* Examine one GPE bit */
341
342                                 if (enabled_status_byte & (1 << j)) {
343                                         /*
344                                          * Found an active GPE. Dispatch the event to a handler
345                                          * or method.
346                                          */
347                                         int_status |=
348                                             acpi_ev_gpe_dispatch(&gpe_block->
349                                                 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
350                                 }
351                         }
352                 }
353
354                 gpe_block = gpe_block->next;
355         }
356
357       unlock_and_exit:
358
359         acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
360         return (int_status);
361 }
362
363 /*******************************************************************************
364  *
365  * FUNCTION:    acpi_ev_asynch_execute_gpe_method
366  *
367  * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
368  *
369  * RETURN:      None
370  *
371  * DESCRIPTION: Perform the actual execution of a GPE control method. This
372  *              function is called from an invocation of acpi_os_execute and
373  *              therefore does NOT execute at interrupt level - so that
374  *              the control method itself is not executed in the context of
375  *              an interrupt handler.
376  *
377  ******************************************************************************/
378 static void acpi_ev_asynch_enable_gpe(void *context);
379
380 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
381 {
382         struct acpi_gpe_event_info *gpe_event_info = (void *)context;
383         acpi_status status;
384         struct acpi_gpe_event_info local_gpe_event_info;
385         struct acpi_evaluate_info *info;
386
387         ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
388
389         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
390         if (ACPI_FAILURE(status)) {
391                 return_VOID;
392         }
393
394         /* Must revalidate the gpe_number/gpe_block */
395
396         if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
397                 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
398                 return_VOID;
399         }
400
401         /* Set the GPE flags for return to enabled state */
402
403         (void)acpi_ev_enable_gpe(gpe_event_info, FALSE);
404
405         /*
406          * Take a snapshot of the GPE info for this level - we copy the info to
407          * prevent a race condition with remove_handler/remove_block.
408          */
409         ACPI_MEMCPY(&local_gpe_event_info, gpe_event_info,
410                     sizeof(struct acpi_gpe_event_info));
411
412         status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
413         if (ACPI_FAILURE(status)) {
414                 return_VOID;
415         }
416
417         /*
418          * Must check for control method type dispatch one more time to avoid a
419          * race with ev_gpe_install_handler
420          */
421         if ((local_gpe_event_info.flags & ACPI_GPE_DISPATCH_MASK) ==
422             ACPI_GPE_DISPATCH_METHOD) {
423
424                 /* Allocate the evaluation information block */
425
426                 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
427                 if (!info) {
428                         status = AE_NO_MEMORY;
429                 } else {
430                         /*
431                          * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the _Lxx/_Exx
432                          * control method that corresponds to this GPE
433                          */
434                         info->prefix_node =
435                             local_gpe_event_info.dispatch.method_node;
436                         info->flags = ACPI_IGNORE_RETURN_VALUE;
437
438                         status = acpi_ns_evaluate(info);
439                         ACPI_FREE(info);
440                 }
441
442                 if (ACPI_FAILURE(status)) {
443                         ACPI_EXCEPTION((AE_INFO, status,
444                                         "while evaluating GPE method [%4.4s]",
445                                         acpi_ut_get_node_name
446                                         (local_gpe_event_info.dispatch.
447                                          method_node)));
448                 }
449         }
450         /* Defer enabling of GPE until all notify handlers are done */
451         acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_ev_asynch_enable_gpe,
452                                 gpe_event_info);
453         return_VOID;
454 }
455
456 static void acpi_ev_asynch_enable_gpe(void *context)
457 {
458         struct acpi_gpe_event_info *gpe_event_info = context;
459         acpi_status status;
460         if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
461             ACPI_GPE_LEVEL_TRIGGERED) {
462                 /*
463                  * GPE is level-triggered, we clear the GPE status bit after handling
464                  * the event.
465                  */
466                 status = acpi_hw_clear_gpe(gpe_event_info);
467                 if (ACPI_FAILURE(status)) {
468                         return_VOID;
469                 }
470         }
471
472         /* Enable this GPE */
473         (void)acpi_hw_write_gpe_enable_reg(gpe_event_info);
474         return_VOID;
475 }
476
477 /*******************************************************************************
478  *
479  * FUNCTION:    acpi_ev_gpe_dispatch
480  *
481  * PARAMETERS:  gpe_event_info  - Info for this GPE
482  *              gpe_number      - Number relative to the parent GPE block
483  *
484  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
485  *
486  * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
487  *              or method (e.g. _Lxx/_Exx) handler.
488  *
489  *              This function executes at interrupt level.
490  *
491  ******************************************************************************/
492
493 u32
494 acpi_ev_gpe_dispatch(struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
495 {
496         acpi_status status;
497
498         ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
499
500         acpi_os_gpe_count(gpe_number);
501
502         /*
503          * If edge-triggered, clear the GPE status bit now. Note that
504          * level-triggered events are cleared after the GPE is serviced.
505          */
506         if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
507             ACPI_GPE_EDGE_TRIGGERED) {
508                 status = acpi_hw_clear_gpe(gpe_event_info);
509                 if (ACPI_FAILURE(status)) {
510                         ACPI_EXCEPTION((AE_INFO, status,
511                                         "Unable to clear GPE[%2X]",
512                                         gpe_number));
513                         return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
514                 }
515         }
516
517         /*
518          * Dispatch the GPE to either an installed handler, or the control method
519          * associated with this GPE (_Lxx or _Exx). If a handler exists, we invoke
520          * it and do not attempt to run the method. If there is neither a handler
521          * nor a method, we disable this GPE to prevent further such pointless
522          * events from firing.
523          */
524         switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
525         case ACPI_GPE_DISPATCH_HANDLER:
526
527                 /*
528                  * Invoke the installed handler (at interrupt level)
529                  * Ignore return status for now.
530                  * TBD: leave GPE disabled on error?
531                  */
532                 (void)gpe_event_info->dispatch.handler->address(gpe_event_info->
533                                                                 dispatch.
534                                                                 handler->
535                                                                 context);
536
537                 /* It is now safe to clear level-triggered events. */
538
539                 if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
540                     ACPI_GPE_LEVEL_TRIGGERED) {
541                         status = acpi_hw_clear_gpe(gpe_event_info);
542                         if (ACPI_FAILURE(status)) {
543                                 ACPI_EXCEPTION((AE_INFO, status,
544                                                 "Unable to clear GPE[%2X]",
545                                                 gpe_number));
546                                 return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
547                         }
548                 }
549                 break;
550
551         case ACPI_GPE_DISPATCH_METHOD:
552
553                 /*
554                  * Disable the GPE, so it doesn't keep firing before the method has a
555                  * chance to run (it runs asynchronously with interrupts enabled).
556                  */
557                 status = acpi_ev_disable_gpe(gpe_event_info);
558                 if (ACPI_FAILURE(status)) {
559                         ACPI_EXCEPTION((AE_INFO, status,
560                                         "Unable to disable GPE[%2X]",
561                                         gpe_number));
562                         return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
563                 }
564
565                 /*
566                  * Execute the method associated with the GPE
567                  * NOTE: Level-triggered GPEs are cleared after the method completes.
568                  */
569                 status = acpi_os_execute(OSL_GPE_HANDLER,
570                                          acpi_ev_asynch_execute_gpe_method,
571                                          gpe_event_info);
572                 if (ACPI_FAILURE(status)) {
573                         ACPI_EXCEPTION((AE_INFO, status,
574                                         "Unable to queue handler for GPE[%2X] - event disabled",
575                                         gpe_number));
576                 }
577                 break;
578
579         default:
580
581                 /* No handler or method to run! */
582
583                 ACPI_ERROR((AE_INFO,
584                             "No handler or method for GPE[%2X], disabling event",
585                             gpe_number));
586
587                 /*
588                  * Disable the GPE. The GPE will remain disabled until the ACPICA
589                  * Core Subsystem is restarted, or a handler is installed.
590                  */
591                 status = acpi_ev_disable_gpe(gpe_event_info);
592                 if (ACPI_FAILURE(status)) {
593                         ACPI_EXCEPTION((AE_INFO, status,
594                                         "Unable to disable GPE[%2X]",
595                                         gpe_number));
596                         return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
597                 }
598                 break;
599         }
600
601         return_UINT32(ACPI_INTERRUPT_HANDLED);
602 }