xref: /openbmc/linux/drivers/acpi/acpica/evgpe.c (revision c819e2cf)
1 /******************************************************************************
2  *
3  * Module Name: evgpe - General Purpose Event handling and dispatch
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 #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 #if (!ACPI_REDUCED_HARDWARE)	/* Entire module */
52 /* Local prototypes */
53 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context);
54 
55 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context);
56 
57 /*******************************************************************************
58  *
59  * FUNCTION:    acpi_ev_update_gpe_enable_mask
60  *
61  * PARAMETERS:  gpe_event_info          - GPE to update
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Updates GPE register enable mask based upon whether there are
66  *              runtime references to this GPE
67  *
68  ******************************************************************************/
69 
70 acpi_status
71 acpi_ev_update_gpe_enable_mask(struct acpi_gpe_event_info *gpe_event_info)
72 {
73 	struct acpi_gpe_register_info *gpe_register_info;
74 	u32 register_bit;
75 
76 	ACPI_FUNCTION_TRACE(ev_update_gpe_enable_mask);
77 
78 	gpe_register_info = gpe_event_info->register_info;
79 	if (!gpe_register_info) {
80 		return_ACPI_STATUS(AE_NOT_EXIST);
81 	}
82 
83 	register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
84 
85 	/* Clear the run bit up front */
86 
87 	ACPI_CLEAR_BIT(gpe_register_info->enable_for_run, register_bit);
88 
89 	/* Set the mask bit only if there are references to this GPE */
90 
91 	if (gpe_event_info->runtime_count) {
92 		ACPI_SET_BIT(gpe_register_info->enable_for_run,
93 			     (u8)register_bit);
94 	}
95 
96 	return_ACPI_STATUS(AE_OK);
97 }
98 
99 /*******************************************************************************
100  *
101  * FUNCTION:    acpi_ev_enable_gpe
102  *
103  * PARAMETERS:  gpe_event_info          - GPE to enable
104  *
105  * RETURN:      Status
106  *
107  * DESCRIPTION: Clear a GPE of stale events and enable it.
108  *
109  ******************************************************************************/
110 
111 acpi_status acpi_ev_enable_gpe(struct acpi_gpe_event_info *gpe_event_info)
112 {
113 	acpi_status status;
114 
115 	ACPI_FUNCTION_TRACE(ev_enable_gpe);
116 
117 	/*
118 	 * We will only allow a GPE to be enabled if it has either an associated
119 	 * method (_Lxx/_Exx) or a handler, or is using the implicit notify
120 	 * feature. Otherwise, the GPE will be immediately disabled by
121 	 * acpi_ev_gpe_dispatch the first time it fires.
122 	 */
123 	if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
124 	    ACPI_GPE_DISPATCH_NONE) {
125 		return_ACPI_STATUS(AE_NO_HANDLER);
126 	}
127 
128 	/* Clear the GPE (of stale events) */
129 
130 	status = acpi_hw_clear_gpe(gpe_event_info);
131 	if (ACPI_FAILURE(status)) {
132 		return_ACPI_STATUS(status);
133 	}
134 
135 	/* Enable the requested GPE */
136 
137 	status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_ENABLE_SAVE);
138 	return_ACPI_STATUS(status);
139 }
140 
141 /*******************************************************************************
142  *
143  * FUNCTION:    acpi_ev_add_gpe_reference
144  *
145  * PARAMETERS:  gpe_event_info          - Add a reference to this GPE
146  *
147  * RETURN:      Status
148  *
149  * DESCRIPTION: Add a reference to a GPE. On the first reference, the GPE is
150  *              hardware-enabled.
151  *
152  ******************************************************************************/
153 
154 acpi_status
155 acpi_ev_add_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
156 {
157 	acpi_status status = AE_OK;
158 
159 	ACPI_FUNCTION_TRACE(ev_add_gpe_reference);
160 
161 	if (gpe_event_info->runtime_count == ACPI_UINT8_MAX) {
162 		return_ACPI_STATUS(AE_LIMIT);
163 	}
164 
165 	gpe_event_info->runtime_count++;
166 	if (gpe_event_info->runtime_count == 1) {
167 
168 		/* Enable on first reference */
169 
170 		status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
171 		if (ACPI_SUCCESS(status)) {
172 			status = acpi_ev_enable_gpe(gpe_event_info);
173 		}
174 
175 		if (ACPI_FAILURE(status)) {
176 			gpe_event_info->runtime_count--;
177 		}
178 	}
179 
180 	return_ACPI_STATUS(status);
181 }
182 
183 /*******************************************************************************
184  *
185  * FUNCTION:    acpi_ev_remove_gpe_reference
186  *
187  * PARAMETERS:  gpe_event_info          - Remove a reference to this GPE
188  *
189  * RETURN:      Status
190  *
191  * DESCRIPTION: Remove a reference to a GPE. When the last reference is
192  *              removed, the GPE is hardware-disabled.
193  *
194  ******************************************************************************/
195 
196 acpi_status
197 acpi_ev_remove_gpe_reference(struct acpi_gpe_event_info *gpe_event_info)
198 {
199 	acpi_status status = AE_OK;
200 
201 	ACPI_FUNCTION_TRACE(ev_remove_gpe_reference);
202 
203 	if (!gpe_event_info->runtime_count) {
204 		return_ACPI_STATUS(AE_LIMIT);
205 	}
206 
207 	gpe_event_info->runtime_count--;
208 	if (!gpe_event_info->runtime_count) {
209 
210 		/* Disable on last reference */
211 
212 		status = acpi_ev_update_gpe_enable_mask(gpe_event_info);
213 		if (ACPI_SUCCESS(status)) {
214 			status =
215 			    acpi_hw_low_set_gpe(gpe_event_info,
216 						ACPI_GPE_DISABLE_SAVE);
217 		}
218 
219 		if (ACPI_FAILURE(status)) {
220 			gpe_event_info->runtime_count++;
221 		}
222 	}
223 
224 	return_ACPI_STATUS(status);
225 }
226 
227 /*******************************************************************************
228  *
229  * FUNCTION:    acpi_ev_low_get_gpe_info
230  *
231  * PARAMETERS:  gpe_number          - Raw GPE number
232  *              gpe_block           - A GPE info block
233  *
234  * RETURN:      A GPE event_info struct. NULL if not a valid GPE (The gpe_number
235  *              is not within the specified GPE block)
236  *
237  * DESCRIPTION: Returns the event_info struct associated with this GPE. This is
238  *              the low-level implementation of ev_get_gpe_event_info.
239  *
240  ******************************************************************************/
241 
242 struct acpi_gpe_event_info *acpi_ev_low_get_gpe_info(u32 gpe_number,
243 						     struct acpi_gpe_block_info
244 						     *gpe_block)
245 {
246 	u32 gpe_index;
247 
248 	/*
249 	 * Validate that the gpe_number is within the specified gpe_block.
250 	 * (Two steps)
251 	 */
252 	if (!gpe_block || (gpe_number < gpe_block->block_base_number)) {
253 		return (NULL);
254 	}
255 
256 	gpe_index = gpe_number - gpe_block->block_base_number;
257 	if (gpe_index >= gpe_block->gpe_count) {
258 		return (NULL);
259 	}
260 
261 	return (&gpe_block->event_info[gpe_index]);
262 }
263 
264 
265 /*******************************************************************************
266  *
267  * FUNCTION:    acpi_ev_get_gpe_event_info
268  *
269  * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
270  *              gpe_number          - Raw GPE number
271  *
272  * RETURN:      A GPE event_info struct. NULL if not a valid GPE
273  *
274  * DESCRIPTION: Returns the event_info struct associated with this GPE.
275  *              Validates the gpe_block and the gpe_number
276  *
277  *              Should be called only when the GPE lists are semaphore locked
278  *              and not subject to change.
279  *
280  ******************************************************************************/
281 
282 struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
283 						       u32 gpe_number)
284 {
285 	union acpi_operand_object *obj_desc;
286 	struct acpi_gpe_event_info *gpe_info;
287 	u32 i;
288 
289 	ACPI_FUNCTION_ENTRY();
290 
291 	/* A NULL gpe_device means use the FADT-defined GPE block(s) */
292 
293 	if (!gpe_device) {
294 
295 		/* Examine GPE Block 0 and 1 (These blocks are permanent) */
296 
297 		for (i = 0; i < ACPI_MAX_GPE_BLOCKS; i++) {
298 			gpe_info = acpi_ev_low_get_gpe_info(gpe_number,
299 							    acpi_gbl_gpe_fadt_blocks
300 							    [i]);
301 			if (gpe_info) {
302 				return (gpe_info);
303 			}
304 		}
305 
306 		/* The gpe_number was not in the range of either FADT GPE block */
307 
308 		return (NULL);
309 	}
310 
311 	/* A Non-NULL gpe_device means this is a GPE Block Device */
312 
313 	obj_desc =
314 	    acpi_ns_get_attached_object((struct acpi_namespace_node *)
315 					       gpe_device);
316 	if (!obj_desc || !obj_desc->device.gpe_block) {
317 		return (NULL);
318 	}
319 
320 	return (acpi_ev_low_get_gpe_info
321 		(gpe_number, obj_desc->device.gpe_block));
322 }
323 
324 /*******************************************************************************
325  *
326  * FUNCTION:    acpi_ev_gpe_detect
327  *
328  * PARAMETERS:  gpe_xrupt_list      - Interrupt block for this interrupt.
329  *                                    Can have multiple GPE blocks attached.
330  *
331  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
332  *
333  * DESCRIPTION: Detect if any GP events have occurred. This function is
334  *              executed at interrupt level.
335  *
336  ******************************************************************************/
337 
338 u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info *gpe_xrupt_list)
339 {
340 	acpi_status status;
341 	struct acpi_gpe_block_info *gpe_block;
342 	struct acpi_gpe_register_info *gpe_register_info;
343 	u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
344 	u8 enabled_status_byte;
345 	u32 status_reg;
346 	u32 enable_reg;
347 	acpi_cpu_flags flags;
348 	u32 i;
349 	u32 j;
350 
351 	ACPI_FUNCTION_NAME(ev_gpe_detect);
352 
353 	/* Check for the case where there are no GPEs */
354 
355 	if (!gpe_xrupt_list) {
356 		return (int_status);
357 	}
358 
359 	/*
360 	 * We need to obtain the GPE lock for both the data structs and registers
361 	 * Note: Not necessary to obtain the hardware lock, since the GPE
362 	 * registers are owned by the gpe_lock.
363 	 */
364 	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
365 
366 	/* Examine all GPE blocks attached to this interrupt level */
367 
368 	gpe_block = gpe_xrupt_list->gpe_block_list_head;
369 	while (gpe_block) {
370 		/*
371 		 * Read all of the 8-bit GPE status and enable registers in this GPE
372 		 * block, saving all of them. Find all currently active GP events.
373 		 */
374 		for (i = 0; i < gpe_block->register_count; i++) {
375 
376 			/* Get the next status/enable pair */
377 
378 			gpe_register_info = &gpe_block->register_info[i];
379 
380 			/*
381 			 * Optimization: If there are no GPEs enabled within this
382 			 * register, we can safely ignore the entire register.
383 			 */
384 			if (!(gpe_register_info->enable_for_run |
385 			      gpe_register_info->enable_for_wake)) {
386 				ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
387 						  "Ignore disabled registers for GPE %02X-%02X: "
388 						  "RunEnable=%02X, WakeEnable=%02X\n",
389 						  gpe_register_info->
390 						  base_gpe_number,
391 						  gpe_register_info->
392 						  base_gpe_number +
393 						  (ACPI_GPE_REGISTER_WIDTH - 1),
394 						  gpe_register_info->
395 						  enable_for_run,
396 						  gpe_register_info->
397 						  enable_for_wake));
398 				continue;
399 			}
400 
401 			/* Read the Status Register */
402 
403 			status =
404 			    acpi_hw_read(&status_reg,
405 					 &gpe_register_info->status_address);
406 			if (ACPI_FAILURE(status)) {
407 				goto unlock_and_exit;
408 			}
409 
410 			/* Read the Enable Register */
411 
412 			status =
413 			    acpi_hw_read(&enable_reg,
414 					 &gpe_register_info->enable_address);
415 			if (ACPI_FAILURE(status)) {
416 				goto unlock_and_exit;
417 			}
418 
419 			ACPI_DEBUG_PRINT((ACPI_DB_INTERRUPTS,
420 					  "Read registers for GPE %02X-%02X: Status=%02X, Enable=%02X, "
421 					  "RunEnable=%02X, WakeEnable=%02X\n",
422 					  gpe_register_info->base_gpe_number,
423 					  gpe_register_info->base_gpe_number +
424 					  (ACPI_GPE_REGISTER_WIDTH - 1),
425 					  status_reg, enable_reg,
426 					  gpe_register_info->enable_for_run,
427 					  gpe_register_info->enable_for_wake));
428 
429 			/* Check if there is anything active at all in this register */
430 
431 			enabled_status_byte = (u8)(status_reg & enable_reg);
432 			if (!enabled_status_byte) {
433 
434 				/* No active GPEs in this register, move on */
435 
436 				continue;
437 			}
438 
439 			/* Now look at the individual GPEs in this byte register */
440 
441 			for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
442 
443 				/* Examine one GPE bit */
444 
445 				if (enabled_status_byte & (1 << j)) {
446 					/*
447 					 * Found an active GPE. Dispatch the event to a handler
448 					 * or method.
449 					 */
450 					int_status |=
451 					    acpi_ev_gpe_dispatch(gpe_block->
452 								 node,
453 								 &gpe_block->
454 								 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
455 				}
456 			}
457 		}
458 
459 		gpe_block = gpe_block->next;
460 	}
461 
462 unlock_and_exit:
463 
464 	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
465 	return (int_status);
466 }
467 
468 /*******************************************************************************
469  *
470  * FUNCTION:    acpi_ev_asynch_execute_gpe_method
471  *
472  * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
473  *
474  * RETURN:      None
475  *
476  * DESCRIPTION: Perform the actual execution of a GPE control method. This
477  *              function is called from an invocation of acpi_os_execute and
478  *              therefore does NOT execute at interrupt level - so that
479  *              the control method itself is not executed in the context of
480  *              an interrupt handler.
481  *
482  ******************************************************************************/
483 
484 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
485 {
486 	struct acpi_gpe_event_info *gpe_event_info = context;
487 	acpi_status status;
488 	struct acpi_gpe_event_info *local_gpe_event_info;
489 	struct acpi_evaluate_info *info;
490 	struct acpi_gpe_notify_info *notify;
491 
492 	ACPI_FUNCTION_TRACE(ev_asynch_execute_gpe_method);
493 
494 	/* Allocate a local GPE block */
495 
496 	local_gpe_event_info =
497 	    ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_event_info));
498 	if (!local_gpe_event_info) {
499 		ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "while handling a GPE"));
500 		return_VOID;
501 	}
502 
503 	status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
504 	if (ACPI_FAILURE(status)) {
505 		ACPI_FREE(local_gpe_event_info);
506 		return_VOID;
507 	}
508 
509 	/* Must revalidate the gpe_number/gpe_block */
510 
511 	if (!acpi_ev_valid_gpe_event(gpe_event_info)) {
512 		status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
513 		ACPI_FREE(local_gpe_event_info);
514 		return_VOID;
515 	}
516 
517 	/*
518 	 * Take a snapshot of the GPE info for this level - we copy the info to
519 	 * prevent a race condition with remove_handler/remove_block.
520 	 */
521 	ACPI_MEMCPY(local_gpe_event_info, gpe_event_info,
522 		    sizeof(struct acpi_gpe_event_info));
523 
524 	status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
525 	if (ACPI_FAILURE(status)) {
526 		ACPI_FREE(local_gpe_event_info);
527 		return_VOID;
528 	}
529 
530 	/* Do the correct dispatch - normal method or implicit notify */
531 
532 	switch (local_gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
533 	case ACPI_GPE_DISPATCH_NOTIFY:
534 		/*
535 		 * Implicit notify.
536 		 * Dispatch a DEVICE_WAKE notify to the appropriate handler.
537 		 * NOTE: the request is queued for execution after this method
538 		 * completes. The notify handlers are NOT invoked synchronously
539 		 * from this thread -- because handlers may in turn run other
540 		 * control methods.
541 		 *
542 		 * June 2012: Expand implicit notify mechanism to support
543 		 * notifies on multiple device objects.
544 		 */
545 		notify = local_gpe_event_info->dispatch.notify_list;
546 		while (ACPI_SUCCESS(status) && notify) {
547 			status =
548 			    acpi_ev_queue_notify_request(notify->device_node,
549 							 ACPI_NOTIFY_DEVICE_WAKE);
550 
551 			notify = notify->next;
552 		}
553 
554 		break;
555 
556 	case ACPI_GPE_DISPATCH_METHOD:
557 
558 		/* Allocate the evaluation information block */
559 
560 		info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
561 		if (!info) {
562 			status = AE_NO_MEMORY;
563 		} else {
564 			/*
565 			 * Invoke the GPE Method (_Lxx, _Exx) i.e., evaluate the
566 			 * _Lxx/_Exx control method that corresponds to this GPE
567 			 */
568 			info->prefix_node =
569 			    local_gpe_event_info->dispatch.method_node;
570 			info->flags = ACPI_IGNORE_RETURN_VALUE;
571 
572 			status = acpi_ns_evaluate(info);
573 			ACPI_FREE(info);
574 		}
575 
576 		if (ACPI_FAILURE(status)) {
577 			ACPI_EXCEPTION((AE_INFO, status,
578 					"while evaluating GPE method [%4.4s]",
579 					acpi_ut_get_node_name
580 					(local_gpe_event_info->dispatch.
581 					 method_node)));
582 		}
583 		break;
584 
585 	default:
586 
587 		return_VOID;	/* Should never happen */
588 	}
589 
590 	/* Defer enabling of GPE until all notify handlers are done */
591 
592 	status = acpi_os_execute(OSL_NOTIFY_HANDLER,
593 				 acpi_ev_asynch_enable_gpe,
594 				 local_gpe_event_info);
595 	if (ACPI_FAILURE(status)) {
596 		ACPI_FREE(local_gpe_event_info);
597 	}
598 	return_VOID;
599 }
600 
601 
602 /*******************************************************************************
603  *
604  * FUNCTION:    acpi_ev_asynch_enable_gpe
605  *
606  * PARAMETERS:  Context (gpe_event_info) - Info for this GPE
607  *              Callback from acpi_os_execute
608  *
609  * RETURN:      None
610  *
611  * DESCRIPTION: Asynchronous clear/enable for GPE. This allows the GPE to
612  *              complete (i.e., finish execution of Notify)
613  *
614  ******************************************************************************/
615 
616 static void ACPI_SYSTEM_XFACE acpi_ev_asynch_enable_gpe(void *context)
617 {
618 	struct acpi_gpe_event_info *gpe_event_info = context;
619 	acpi_cpu_flags flags;
620 
621 	flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
622 	(void)acpi_ev_finish_gpe(gpe_event_info);
623 	acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
624 
625 	ACPI_FREE(gpe_event_info);
626 	return;
627 }
628 
629 
630 /*******************************************************************************
631  *
632  * FUNCTION:    acpi_ev_finish_gpe
633  *
634  * PARAMETERS:  gpe_event_info      - Info for this GPE
635  *
636  * RETURN:      Status
637  *
638  * DESCRIPTION: Clear/Enable a GPE. Common code that is used after execution
639  *              of a GPE method or a synchronous or asynchronous GPE handler.
640  *
641  ******************************************************************************/
642 
643 acpi_status acpi_ev_finish_gpe(struct acpi_gpe_event_info * gpe_event_info)
644 {
645 	acpi_status status;
646 
647 	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
648 	    ACPI_GPE_LEVEL_TRIGGERED) {
649 		/*
650 		 * GPE is level-triggered, we clear the GPE status bit after
651 		 * handling the event.
652 		 */
653 		status = acpi_hw_clear_gpe(gpe_event_info);
654 		if (ACPI_FAILURE(status)) {
655 			return (status);
656 		}
657 	}
658 
659 	/*
660 	 * Enable this GPE, conditionally. This means that the GPE will
661 	 * only be physically enabled if the enable_mask bit is set
662 	 * in the event_info.
663 	 */
664 	(void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_CONDITIONAL_ENABLE);
665 	return (AE_OK);
666 }
667 
668 
669 /*******************************************************************************
670  *
671  * FUNCTION:    acpi_ev_gpe_dispatch
672  *
673  * PARAMETERS:  gpe_device          - Device node. NULL for GPE0/GPE1
674  *              gpe_event_info      - Info for this GPE
675  *              gpe_number          - Number relative to the parent GPE block
676  *
677  * RETURN:      INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
678  *
679  * DESCRIPTION: Dispatch a General Purpose Event to either a function (e.g. EC)
680  *              or method (e.g. _Lxx/_Exx) handler.
681  *
682  *              This function executes at interrupt level.
683  *
684  ******************************************************************************/
685 
686 u32
687 acpi_ev_gpe_dispatch(struct acpi_namespace_node *gpe_device,
688 		     struct acpi_gpe_event_info *gpe_event_info, u32 gpe_number)
689 {
690 	acpi_status status;
691 	u32 return_value;
692 
693 	ACPI_FUNCTION_TRACE(ev_gpe_dispatch);
694 
695 	/* Invoke global event handler if present */
696 
697 	acpi_gpe_count++;
698 	if (acpi_gbl_global_event_handler) {
699 		acpi_gbl_global_event_handler(ACPI_EVENT_TYPE_GPE, gpe_device,
700 					      gpe_number,
701 					      acpi_gbl_global_event_handler_context);
702 	}
703 
704 	/*
705 	 * Always disable the GPE so that it does not keep firing before
706 	 * any asynchronous activity completes (either from the execution
707 	 * of a GPE method or an asynchronous GPE handler.)
708 	 *
709 	 * If there is no handler or method to run, just disable the
710 	 * GPE and leave it disabled permanently to prevent further such
711 	 * pointless events from firing.
712 	 */
713 	status = acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
714 	if (ACPI_FAILURE(status)) {
715 		ACPI_EXCEPTION((AE_INFO, status,
716 				"Unable to disable GPE %02X", gpe_number));
717 		return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
718 	}
719 
720 	/*
721 	 * If edge-triggered, clear the GPE status bit now. Note that
722 	 * level-triggered events are cleared after the GPE is serviced.
723 	 */
724 	if ((gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK) ==
725 	    ACPI_GPE_EDGE_TRIGGERED) {
726 		status = acpi_hw_clear_gpe(gpe_event_info);
727 		if (ACPI_FAILURE(status)) {
728 			ACPI_EXCEPTION((AE_INFO, status,
729 					"Unable to clear GPE %02X",
730 					gpe_number));
731 			(void)acpi_hw_low_set_gpe(gpe_event_info,
732 						  ACPI_GPE_CONDITIONAL_ENABLE);
733 			return_UINT32(ACPI_INTERRUPT_NOT_HANDLED);
734 		}
735 	}
736 
737 	/*
738 	 * Dispatch the GPE to either an installed handler or the control
739 	 * method associated with this GPE (_Lxx or _Exx). If a handler
740 	 * exists, we invoke it and do not attempt to run the method.
741 	 * If there is neither a handler nor a method, leave the GPE
742 	 * disabled.
743 	 */
744 	switch (gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) {
745 	case ACPI_GPE_DISPATCH_HANDLER:
746 
747 		/* Invoke the installed handler (at interrupt level) */
748 
749 		return_value =
750 		    gpe_event_info->dispatch.handler->address(gpe_device,
751 							      gpe_number,
752 							      gpe_event_info->
753 							      dispatch.handler->
754 							      context);
755 
756 		/* If requested, clear (if level-triggered) and reenable the GPE */
757 
758 		if (return_value & ACPI_REENABLE_GPE) {
759 			(void)acpi_ev_finish_gpe(gpe_event_info);
760 		}
761 		break;
762 
763 	case ACPI_GPE_DISPATCH_METHOD:
764 	case ACPI_GPE_DISPATCH_NOTIFY:
765 		/*
766 		 * Execute the method associated with the GPE
767 		 * NOTE: Level-triggered GPEs are cleared after the method completes.
768 		 */
769 		status = acpi_os_execute(OSL_GPE_HANDLER,
770 					 acpi_ev_asynch_execute_gpe_method,
771 					 gpe_event_info);
772 		if (ACPI_FAILURE(status)) {
773 			ACPI_EXCEPTION((AE_INFO, status,
774 					"Unable to queue handler for GPE %02X - event disabled",
775 					gpe_number));
776 		}
777 		break;
778 
779 	default:
780 		/*
781 		 * No handler or method to run!
782 		 * 03/2010: This case should no longer be possible. We will not allow
783 		 * a GPE to be enabled if it has no handler or method.
784 		 */
785 		ACPI_ERROR((AE_INFO,
786 			    "No handler or method for GPE %02X, disabling event",
787 			    gpe_number));
788 
789 		break;
790 	}
791 
792 	return_UINT32(ACPI_INTERRUPT_HANDLED);
793 }
794 
795 #endif				/* !ACPI_REDUCED_HARDWARE */
796