- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / acpi / events / evevent.c
blob4ae76eb599331ff06f826d37a633e05337ba116f
1 /******************************************************************************
3 * Module Name: evevent - Fixed and General Purpose Acpi_event
4 * handling and dispatch
5 * $Revision: 30 $
7 *****************************************************************************/
9 /*
10 * Copyright (C) 2000 R. Byron Moore
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "acpi.h"
28 #include "achware.h"
29 #include "acevents.h"
30 #include "acnamesp.h"
31 #include "accommon.h"
33 #define _COMPONENT EVENT_HANDLING
34 MODULE_NAME ("evevent")
37 /**************************************************************************
39 * FUNCTION: Acpi_ev_initialize
41 * PARAMETERS: None
43 * RETURN: Status
45 * DESCRIPTION: Ensures that the system control interrupt (SCI) is properly
46 * configured, disables SCI event sources, installs the SCI
47 * handler
49 *************************************************************************/
51 ACPI_STATUS
52 acpi_ev_initialize (
53 void)
55 ACPI_STATUS status;
58 /* Make sure we've got ACPI tables */
60 if (!acpi_gbl_DSDT) {
61 return (AE_NO_ACPI_TABLES);
65 /* Make sure the BIOS supports ACPI mode */
67 if (SYS_MODE_LEGACY == acpi_hw_get_mode_capabilities()) {
68 return (AE_ERROR);
72 acpi_gbl_original_mode = acpi_hw_get_mode();
75 * Initialize the Fixed and General Purpose Acpi_events prior. This is
76 * done prior to enabling SCIs to prevent interrupts from occuring
77 * before handers are installed.
80 status = acpi_ev_fixed_event_initialize ();
81 if (ACPI_FAILURE (status)) {
82 return (status);
85 status = acpi_ev_gpe_initialize ();
86 if (ACPI_FAILURE (status)) {
87 return (status);
90 /* Install the SCI handler */
92 status = acpi_ev_install_sci_handler ();
93 if (ACPI_FAILURE (status)) {
94 return (status);
98 /* Install handlers for control method GPE handlers (_Lxx, _Exx) */
100 status = acpi_ev_init_gpe_control_methods ();
101 if (ACPI_FAILURE (status)) {
102 return (status);
105 /* Install the handler for the Global Lock */
107 status = acpi_ev_init_global_lock_handler ();
108 if (ACPI_FAILURE (status)) {
109 return (status);
113 return (status);
117 /******************************************************************************
119 * FUNCTION: Acpi_ev_fixed_event_initialize
121 * PARAMETERS: None
123 * RETURN: Status
125 * DESCRIPTION: Initialize the Fixed Acpi_event data structures
127 ******************************************************************************/
129 ACPI_STATUS
130 acpi_ev_fixed_event_initialize(void)
132 int i = 0;
134 /* Initialize the structure that keeps track of fixed event handlers */
136 for (i = 0; i < NUM_FIXED_EVENTS; i++) {
137 acpi_gbl_fixed_event_handlers[i].handler = NULL;
138 acpi_gbl_fixed_event_handlers[i].context = NULL;
141 acpi_hw_register_bit_access (ACPI_WRITE, ACPI_MTX_LOCK, TMR_EN, 0);
142 acpi_hw_register_bit_access (ACPI_WRITE, ACPI_MTX_LOCK, GBL_EN, 0);
143 acpi_hw_register_bit_access (ACPI_WRITE, ACPI_MTX_LOCK, PWRBTN_EN, 0);
144 acpi_hw_register_bit_access (ACPI_WRITE, ACPI_MTX_LOCK, SLPBTN_EN, 0);
145 acpi_hw_register_bit_access (ACPI_WRITE, ACPI_MTX_LOCK, RTC_EN, 0);
147 return (AE_OK);
151 /******************************************************************************
153 * FUNCTION: Acpi_ev_fixed_event_detect
155 * PARAMETERS: None
157 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
159 * DESCRIPTION: Checks the PM status register for fixed events
161 ******************************************************************************/
164 acpi_ev_fixed_event_detect(void)
166 u32 int_status = INTERRUPT_NOT_HANDLED;
167 u32 status_register;
168 u32 enable_register;
171 * Read the fixed feature status and enable registers, as all the cases
172 * depend on their values.
175 status_register = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK, PM1_STS);
176 enable_register = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK, PM1_EN);
179 /* power management timer roll over */
181 if ((status_register & ACPI_STATUS_PMTIMER) &&
182 (enable_register & ACPI_ENABLE_PMTIMER))
184 int_status |= acpi_ev_fixed_event_dispatch (ACPI_EVENT_PMTIMER);
187 /* global event (BIOS want's the global lock) */
189 if ((status_register & ACPI_STATUS_GLOBAL) &&
190 (enable_register & ACPI_ENABLE_GLOBAL))
192 int_status |= acpi_ev_fixed_event_dispatch (ACPI_EVENT_GLOBAL);
195 /* power button event */
197 if ((status_register & ACPI_STATUS_POWER_BUTTON) &&
198 (enable_register & ACPI_ENABLE_POWER_BUTTON))
200 int_status |= acpi_ev_fixed_event_dispatch (ACPI_EVENT_POWER_BUTTON);
203 /* sleep button event */
205 if ((status_register & ACPI_STATUS_SLEEP_BUTTON) &&
206 (enable_register & ACPI_ENABLE_SLEEP_BUTTON))
208 int_status |= acpi_ev_fixed_event_dispatch (ACPI_EVENT_SLEEP_BUTTON);
211 return (int_status);
215 /******************************************************************************
217 * FUNCTION: Acpi_ev_fixed_event_dispatch
219 * PARAMETERS: Event - Event type
221 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
223 * DESCRIPTION: Clears the status bit for the requested event, calls the
224 * handler that previously registered for the event.
226 ******************************************************************************/
229 acpi_ev_fixed_event_dispatch (
230 u32 event)
232 u32 register_id;
234 /* Clear the status bit */
236 switch (event)
238 case ACPI_EVENT_PMTIMER:
239 register_id = TMR_STS;
240 break;
242 case ACPI_EVENT_GLOBAL:
243 register_id = GBL_STS;
244 break;
246 case ACPI_EVENT_POWER_BUTTON:
247 register_id = PWRBTN_STS;
248 break;
250 case ACPI_EVENT_SLEEP_BUTTON:
251 register_id = SLPBTN_STS;
252 break;
254 case ACPI_EVENT_RTC:
255 register_id = RTC_STS;
256 break;
258 default:
259 return 0;
260 break;
263 acpi_hw_register_bit_access (ACPI_WRITE, ACPI_MTX_DO_NOT_LOCK, register_id, 1);
266 * Make sure we've got a handler. If not, report an error.
267 * The event is disabled to prevent further interrupts.
269 if (NULL == acpi_gbl_fixed_event_handlers[event].handler) {
270 register_id = (PM1_EN | REGISTER_BIT_ID(register_id));
272 acpi_hw_register_bit_access (ACPI_WRITE, ACPI_MTX_DO_NOT_LOCK,
273 register_id, 0);
275 REPORT_ERROR (
276 ("Ev_gpe_dispatch: No installed handler for fixed event [%08X]\n",
277 event));
279 return (INTERRUPT_NOT_HANDLED);
282 /* Invoke the handler */
284 return ((acpi_gbl_fixed_event_handlers[event].handler)(
285 acpi_gbl_fixed_event_handlers[event].context));
289 /******************************************************************************
291 * FUNCTION: Acpi_ev_gpe_initialize
293 * PARAMETERS: None
295 * RETURN: Status
297 * DESCRIPTION: Initialize the GPE data structures
299 ******************************************************************************/
301 ACPI_STATUS
302 acpi_ev_gpe_initialize (void)
304 u32 i;
305 u32 j;
306 u32 register_index;
307 u32 gpe_number;
308 u16 gpe0register_count;
309 u16 gpe1_register_count;
313 * Set up various GPE counts
315 * You may ask,why are the GPE register block lengths divided by 2?
316 * From the ACPI 2.0 Spec, section, 4.7.1.6 General-Purpose Event
317 * Registers, we have,
319 * "Each register block contains two registers of equal length
320 * GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
321 * GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
322 * The length of the GPE1_STS and GPE1_EN registers is equal to
323 * half the GPE1_LEN. If a generic register block is not supported
324 * then its respective block pointer and block length values in the
325 * FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
326 * to be the same size."
329 gpe0register_count = (u16) DIV_2 (acpi_gbl_FADT->gpe0blk_len);
330 gpe1_register_count = (u16) DIV_2 (acpi_gbl_FADT->gpe1_blk_len);
331 acpi_gbl_gpe_register_count = gpe0register_count + gpe1_register_count;
333 if (!acpi_gbl_gpe_register_count) {
334 REPORT_WARNING (("Zero GPEs are defined in the FADT\n"));
335 return (AE_OK);
339 * Allocate the Gpe information block
342 acpi_gbl_gpe_registers = acpi_cm_callocate (acpi_gbl_gpe_register_count *
343 sizeof (ACPI_GPE_REGISTERS));
344 if (!acpi_gbl_gpe_registers) {
345 return (AE_NO_MEMORY);
349 * Allocate the Gpe dispatch handler block
350 * There are eight distinct GP events per register.
351 * Initialization to zeros is sufficient
354 acpi_gbl_gpe_info = acpi_cm_callocate (MUL_8 (acpi_gbl_gpe_register_count) *
355 sizeof (ACPI_GPE_LEVEL_INFO));
356 if (!acpi_gbl_gpe_info) {
357 acpi_cm_free (acpi_gbl_gpe_registers);
358 return (AE_NO_MEMORY);
361 /* Set the Gpe validation table to GPE_INVALID */
363 MEMSET (acpi_gbl_gpe_valid, (int) ACPI_GPE_INVALID, NUM_GPE);
366 * Initialize the Gpe information and validation blocks. A goal of these
367 * blocks is to hide the fact that there are two separate GPE register sets
368 * In a given block, the status registers occupy the first half, and
369 * the enable registers occupy the second half.
372 /* GPE Block 0 */
374 register_index = 0;
376 for (i = 0; i < gpe0register_count; i++) {
377 acpi_gbl_gpe_registers[register_index].status_addr =
378 (u16) (acpi_gbl_FADT->Xgpe0blk.address + i);
380 acpi_gbl_gpe_registers[register_index].enable_addr =
381 (u16) (acpi_gbl_FADT->Xgpe0blk.address + i + gpe0register_count);
383 acpi_gbl_gpe_registers[register_index].gpe_base = (u8) MUL_8 (i);
385 for (j = 0; j < 8; j++) {
386 gpe_number = acpi_gbl_gpe_registers[register_index].gpe_base + j;
387 acpi_gbl_gpe_valid[gpe_number] = (u8) register_index;
391 * Clear the status/enable registers. Note that status registers
392 * are cleared by writing a '1', while enable registers are cleared
393 * by writing a '0'.
395 acpi_os_out8 (acpi_gbl_gpe_registers[register_index].enable_addr, 0x00);
396 acpi_os_out8 (acpi_gbl_gpe_registers[register_index].status_addr, 0xFF);
398 register_index++;
401 /* GPE Block 1 */
403 for (i = 0; i < gpe1_register_count; i++) {
404 acpi_gbl_gpe_registers[register_index].status_addr =
405 (u16) (acpi_gbl_FADT->Xgpe1_blk.address + i);
407 acpi_gbl_gpe_registers[register_index].enable_addr =
408 (u16) (acpi_gbl_FADT->Xgpe1_blk.address + i + gpe1_register_count);
410 acpi_gbl_gpe_registers[register_index].gpe_base =
411 (u8) (acpi_gbl_FADT->gpe1_base + MUL_8 (i));
413 for (j = 0; j < 8; j++) {
414 gpe_number = acpi_gbl_gpe_registers[register_index].gpe_base + j;
415 acpi_gbl_gpe_valid[gpe_number] = (u8) register_index;
419 * Clear the status/enable registers. Note that status registers
420 * are cleared by writing a '1', while enable registers are cleared
421 * by writing a '0'.
423 acpi_os_out8 (acpi_gbl_gpe_registers[register_index].enable_addr, 0x00);
424 acpi_os_out8 (acpi_gbl_gpe_registers[register_index].status_addr, 0xFF);
426 register_index++;
429 return (AE_OK);
433 /******************************************************************************
435 * FUNCTION: Acpi_ev_save_method_info
437 * PARAMETERS: None
439 * RETURN: None
441 * DESCRIPTION: Called from Acpi_walk_namespace. Expects each object to be a
442 * control method under the _GPE portion of the namespace.
443 * Extract the name and GPE type from the object, saving this
444 * information for quick lookup during GPE dispatch
446 * The name of each GPE control method is of the form:
447 * "_Lnn" or "_Enn"
448 * Where:
449 * L - means that the GPE is level triggered
450 * E - means that the GPE is edge triggered
451 * nn - is the GPE number
453 ******************************************************************************/
455 static ACPI_STATUS
456 acpi_ev_save_method_info (
457 ACPI_HANDLE obj_handle,
458 u32 level,
459 void *obj_desc,
460 void **return_value)
462 u32 gpe_number;
463 NATIVE_CHAR name[ACPI_NAME_SIZE + 1];
464 u8 type;
467 /* Extract the name from the object and convert to a string */
469 MOVE_UNALIGNED32_TO_32 (name, &((ACPI_NAMESPACE_NODE *) obj_handle)->name);
470 name[ACPI_NAME_SIZE] = 0;
473 * Edge/Level determination is based on the 2nd s8 of the method name
475 if (name[1] == 'L') {
476 type = ACPI_EVENT_LEVEL_TRIGGERED;
478 else if (name[1] == 'E') {
479 type = ACPI_EVENT_EDGE_TRIGGERED;
481 else {
482 /* Unknown method type, just ignore it! */
484 return (AE_OK);
487 /* Convert the last two characters of the name to the Gpe Number */
489 gpe_number = STRTOUL (&name[2], NULL, 16);
490 if (gpe_number == ACPI_UINT32_MAX) {
491 /* Conversion failed; invalid method, just ignore it */
493 return (AE_OK);
496 /* Ensure that we have a valid GPE number */
498 if (acpi_gbl_gpe_valid[gpe_number] == ACPI_GPE_INVALID) {
499 /* Not valid, all we can do here is ignore it */
501 return (AE_OK);
505 * Now we can add this information to the Gpe_info block
506 * for use during dispatch of this GPE.
509 acpi_gbl_gpe_info [gpe_number].type = type;
510 acpi_gbl_gpe_info [gpe_number].method_handle = obj_handle;
514 * Enable the GPE (SCIs should be disabled at this point)
517 acpi_hw_enable_gpe (gpe_number);
519 return (AE_OK);
523 /******************************************************************************
525 * FUNCTION: Acpi_ev_init_gpe_control_methods
527 * PARAMETERS: None
529 * RETURN: None
531 * DESCRIPTION: Obtain the control methods associated with the GPEs.
533 * NOTE: Must be called AFTER namespace initialization!
535 ******************************************************************************/
537 ACPI_STATUS
538 acpi_ev_init_gpe_control_methods (void)
540 ACPI_STATUS status;
543 /* Get a permanent handle to the _GPE object */
545 status = acpi_get_handle (NULL, "\\_GPE", &acpi_gbl_gpe_obj_handle);
546 if (ACPI_FAILURE (status)) {
547 return (status);
550 /* Traverse the namespace under \_GPE to find all methods there */
552 status = acpi_walk_namespace (ACPI_TYPE_METHOD, acpi_gbl_gpe_obj_handle,
553 ACPI_UINT32_MAX, acpi_ev_save_method_info,
554 NULL, NULL);
556 return (status);
560 /******************************************************************************
562 * FUNCTION: Acpi_ev_gpe_detect
564 * PARAMETERS: None
566 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
568 * DESCRIPTION: Detect if any GP events have occurred
570 ******************************************************************************/
573 acpi_ev_gpe_detect (void)
575 u32 int_status = INTERRUPT_NOT_HANDLED;
576 u32 i;
577 u32 j;
578 u8 enabled_status_byte;
579 u8 bit_mask;
583 * Read all of the 8-bit GPE status and enable registers
584 * in both of the register blocks, saving all of it.
585 * Find all currently active GP events.
588 for (i = 0; i < acpi_gbl_gpe_register_count; i++) {
589 acpi_gbl_gpe_registers[i].status =
590 acpi_os_in8 (acpi_gbl_gpe_registers[i].status_addr);
592 acpi_gbl_gpe_registers[i].enable =
593 acpi_os_in8 (acpi_gbl_gpe_registers[i].enable_addr);
595 /* First check if there is anything active at all in this register */
597 enabled_status_byte = (u8) (acpi_gbl_gpe_registers[i].status &
598 acpi_gbl_gpe_registers[i].enable);
600 if (!enabled_status_byte) {
601 /* No active GPEs in this register, move on */
603 continue;
606 /* Now look at the individual GPEs in this byte register */
608 for (j = 0, bit_mask = 1; j < 8; j++, bit_mask <<= 1) {
609 /* Examine one GPE bit */
611 if (enabled_status_byte & bit_mask) {
613 * Found an active GPE. Dispatch the event to a handler
614 * or method.
616 int_status |=
617 acpi_ev_gpe_dispatch (acpi_gbl_gpe_registers[i].gpe_base + j);
622 return (int_status);
626 /******************************************************************************
628 * FUNCTION: Acpi_ev_asynch_execute_gpe_method
630 * PARAMETERS: Gpe_number - The 0-based Gpe number
632 * RETURN: None
634 * DESCRIPTION: Perform the actual execution of a GPE control method. This
635 * function is called from an invocation of Acpi_os_queue_for_execution
636 * (and therefore does NOT execute at interrupt level) so that
637 * the control method itself is not executed in the context of
638 * the SCI interrupt handler.
640 ******************************************************************************/
642 static void
643 acpi_ev_asynch_execute_gpe_method (
644 void *context)
646 u32 gpe_number = (u32) context;
647 ACPI_GPE_LEVEL_INFO gpe_info;
651 * Take a snapshot of the GPE info for this level
653 acpi_cm_acquire_mutex (ACPI_MTX_EVENTS);
654 gpe_info = acpi_gbl_gpe_info [gpe_number];
655 acpi_cm_release_mutex (ACPI_MTX_EVENTS);
658 * Method Handler (_Lxx, _Exx):
659 * ----------------------------
660 * Evaluate the _Lxx/_Exx control method that corresponds to this GPE.
662 if (gpe_info.method_handle) {
663 acpi_ns_evaluate_by_handle (gpe_info.method_handle, NULL, NULL);
667 * Level-Triggered?
668 * ----------------
669 * If level-triggered we clear the GPE status bit after handling the event.
671 if (gpe_info.type & ACPI_EVENT_LEVEL_TRIGGERED) {
672 acpi_hw_clear_gpe (gpe_number);
676 * Enable the GPE.
678 acpi_hw_enable_gpe (gpe_number);
680 return;
684 /******************************************************************************
686 * FUNCTION: Acpi_ev_gpe_dispatch
688 * PARAMETERS: Gpe_number - The 0-based Gpe number
690 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
692 * DESCRIPTION: Handle and dispatch a General Purpose Acpi_event.
693 * Clears the status bit for the requested event.
695 * TBD: [Investigate] is this still valid or necessary:
696 * The Gpe handler differs from the fixed events in that it clears the enable
697 * bit rather than the status bit to clear the interrupt. This allows
698 * software outside of interrupt context to determine what caused the SCI and
699 * dispatch the correct AML.
701 ******************************************************************************/
704 acpi_ev_gpe_dispatch (
705 u32 gpe_number)
707 ACPI_GPE_LEVEL_INFO gpe_info;
709 /*DEBUG_INCREMENT_EVENT_COUNT (EVENT_GENERAL);*/
712 * Valid GPE number?
714 if (acpi_gbl_gpe_valid[gpe_number] == ACPI_GPE_INVALID) {
715 return (INTERRUPT_NOT_HANDLED);
719 * Disable the GPE.
721 acpi_hw_disable_gpe (gpe_number);
723 gpe_info = acpi_gbl_gpe_info [gpe_number];
726 * Edge-Triggered?
727 * ---------------
728 * If edge-triggered, clear the GPE status bit now. Note that
729 * level-triggered events are cleared after the GPE is serviced.
731 if (gpe_info.type & ACPI_EVENT_EDGE_TRIGGERED) {
732 acpi_hw_clear_gpe (gpe_number);
736 * Function Handler (e.g. EC)?
738 if (gpe_info.handler) {
739 /* Invoke function handler (at interrupt level). */
740 gpe_info.handler (gpe_info.context);
742 /* Level-Triggered? */
743 if (gpe_info.type & ACPI_EVENT_LEVEL_TRIGGERED) {
744 acpi_hw_clear_gpe (gpe_number);
747 /* Enable GPE */
748 acpi_hw_enable_gpe (gpe_number);
751 * Method Handler (e.g. _Exx/_Lxx)?
753 else if (gpe_info.method_handle) {
754 if (ACPI_FAILURE(acpi_os_queue_for_execution (OSD_PRIORITY_GPE,
755 acpi_ev_asynch_execute_gpe_method, (void*)(NATIVE_UINT)gpe_number)))
758 * Shoudn't occur, but if it does report an error. Note that
759 * the GPE will remain disabled until the ACPI Core Subsystem
760 * is restarted, or the handler is removed/reinstalled.
762 REPORT_ERROR (("Acpi_ev_gpe_dispatch: Unable to queue handler for GPE bit [%X]\n", gpe_number));
766 * No Handler? Report an error and leave the GPE disabled.
768 else {
769 REPORT_ERROR (("Acpi_ev_gpe_dispatch: No installed handler for GPE [%X]\n", gpe_number));
771 /* Level-Triggered? */
772 if (gpe_info.type & ACPI_EVENT_LEVEL_TRIGGERED) {
773 acpi_hw_clear_gpe (gpe_number);
777 return (INTERRUPT_HANDLED);