Linux 2.4.0-test9pre1
[davej-history.git] / drivers / acpi / events / evevent.c
blobb3e57cd79edabbb5d7de2e8d5385b7fb31e5febc
1 /******************************************************************************
3 * Module Name: evevent - Fixed and General Purpose Acpi_event
4 * handling and dispatch
5 * $Revision: 13 $
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_fixed_event_initialize
41 * PARAMETERS: None
43 * RETURN: Status
45 * DESCRIPTION: Initialize the Fixed Acpi_event data structures
47 ******************************************************************************/
49 ACPI_STATUS
50 acpi_ev_fixed_event_initialize(void)
52 int i = 0;
54 /* Initialize the structure that keeps track of fixed event handlers */
56 for (i = 0; i < NUM_FIXED_EVENTS; i++) {
57 acpi_gbl_fixed_event_handlers[i].handler = NULL;
58 acpi_gbl_fixed_event_handlers[i].context = NULL;
61 acpi_hw_register_access (ACPI_WRITE, ACPI_MTX_LOCK, ACPI_EVENT_PMTIMER +
62 TMR_EN, 0);
63 acpi_hw_register_access (ACPI_WRITE, ACPI_MTX_LOCK, ACPI_EVENT_GLOBAL +
64 TMR_EN, 0);
65 acpi_hw_register_access (ACPI_WRITE, ACPI_MTX_LOCK, ACPI_EVENT_POWER_BUTTON +
66 TMR_EN, 0);
67 acpi_hw_register_access (ACPI_WRITE, ACPI_MTX_LOCK, ACPI_EVENT_SLEEP_BUTTON +
68 TMR_EN, 0);
69 acpi_hw_register_access (ACPI_WRITE, ACPI_MTX_LOCK, ACPI_EVENT_RTC +
70 TMR_EN, 0);
72 return (AE_OK);
76 /******************************************************************************
78 * FUNCTION: Acpi_ev_fixed_event_detect
80 * PARAMETERS: None
82 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
84 * DESCRIPTION: Checks the PM status register for fixed events
86 ******************************************************************************/
88 u32
89 acpi_ev_fixed_event_detect(void)
91 u32 int_status = INTERRUPT_NOT_HANDLED;
92 u32 status_register = 0;
93 u32 enable_register = 0;
96 * Read the fixed feature status and enable registers, as all the cases
97 * depend on their values.
100 status_register = (u32) acpi_os_in16 (acpi_gbl_FACP->pm1a_evt_blk);
101 if (acpi_gbl_FACP->pm1b_evt_blk) {
102 status_register |= (u32) acpi_os_in16 (acpi_gbl_FACP->pm1b_evt_blk);
105 enable_register = (u32) acpi_os_in16 (acpi_gbl_FACP->pm1a_evt_blk +
106 DIV_2 (acpi_gbl_FACP->pm1_evt_len));
107 if (acpi_gbl_FACP->pm1b_evt_blk) {
108 enable_register |= (u32) acpi_os_in16 (acpi_gbl_FACP->pm1b_evt_blk +
109 DIV_2 (acpi_gbl_FACP->pm1_evt_len));
112 /* power management timer roll over */
114 if ((status_register & ACPI_STATUS_PMTIMER) &&
115 (enable_register & ACPI_ENABLE_PMTIMER))
117 int_status |= acpi_ev_fixed_event_dispatch (ACPI_EVENT_PMTIMER);
120 /* global event (BIOS want's the global lock) */
122 if ((status_register & ACPI_STATUS_GLOBAL) &&
123 (enable_register & ACPI_ENABLE_GLOBAL))
125 int_status |= acpi_ev_fixed_event_dispatch (ACPI_EVENT_GLOBAL);
128 /* power button event */
130 if ((status_register & ACPI_STATUS_POWER_BUTTON) &&
131 (enable_register & ACPI_ENABLE_POWER_BUTTON))
133 int_status |= acpi_ev_fixed_event_dispatch (ACPI_EVENT_POWER_BUTTON);
136 /* sleep button event */
138 if ((status_register & ACPI_STATUS_SLEEP_BUTTON) &&
139 (enable_register & ACPI_ENABLE_SLEEP_BUTTON))
141 int_status |= acpi_ev_fixed_event_dispatch (ACPI_EVENT_SLEEP_BUTTON);
144 return (int_status);
148 /******************************************************************************
150 * FUNCTION: Acpi_ev_fixed_event_dispatch
152 * PARAMETERS: Event - Event type
154 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
156 * DESCRIPTION: Clears the status bit for the requested event, calls the
157 * handler that previously registered for the event.
159 ******************************************************************************/
162 acpi_ev_fixed_event_dispatch (
163 u32 event)
165 /* Clear the status bit */
167 acpi_hw_register_access (ACPI_WRITE, ACPI_MTX_DO_NOT_LOCK, TMR_STS +
168 event, 1);
171 * Make sure we've got a handler. If not, report an error.
172 * The event is disabled to prevent further interrupts.
174 if (NULL == acpi_gbl_fixed_event_handlers[event].handler) {
175 acpi_hw_register_access (ACPI_WRITE, ACPI_MTX_DO_NOT_LOCK,
176 TMR_EN + event, 0);
178 REPORT_ERROR("No installed handler for fixed event.");
179 return (INTERRUPT_NOT_HANDLED);
182 /* Invoke the handler */
184 return ((acpi_gbl_fixed_event_handlers[event].handler)(
185 acpi_gbl_fixed_event_handlers[event].context));
189 /******************************************************************************
191 * FUNCTION: Acpi_ev_gpe_initialize
193 * PARAMETERS: None
195 * RETURN: Status
197 * DESCRIPTION: Initialize the GPE data structures
199 ******************************************************************************/
201 ACPI_STATUS
202 acpi_ev_gpe_initialize (void)
204 u32 i;
205 u32 j;
206 u32 register_index;
207 u32 gpe_number;
208 u16 gpe0register_count;
209 u16 gpe1_register_count;
213 * Setup various GPE counts
216 gpe0register_count = (u16) DIV_2 (acpi_gbl_FACP->gpe0blk_len);
217 gpe1_register_count = (u16) DIV_2 (acpi_gbl_FACP->gpe1_blk_len);
218 acpi_gbl_gpe_register_count = gpe0register_count + gpe1_register_count;
220 if (!acpi_gbl_gpe_register_count) {
221 REPORT_WARNING ("No GPEs defined in the FACP");
222 return (AE_OK);
226 * Allocate the Gpe information block
229 acpi_gbl_gpe_registers = acpi_cm_callocate (acpi_gbl_gpe_register_count *
230 sizeof (ACPI_GPE_REGISTERS));
231 if (!acpi_gbl_gpe_registers) {
232 return (AE_NO_MEMORY);
236 * Allocate the Gpe dispatch handler block
237 * There are eight distinct GP events per register.
238 * Initialization to zeros is sufficient
241 acpi_gbl_gpe_info = acpi_cm_callocate (MUL_8 (acpi_gbl_gpe_register_count) *
242 sizeof (ACPI_GPE_LEVEL_INFO));
243 if (!acpi_gbl_gpe_info) {
244 acpi_cm_free (acpi_gbl_gpe_registers);
245 return (AE_NO_MEMORY);
248 /* Set the Gpe validation table to GPE_INVALID */
250 MEMSET (acpi_gbl_gpe_valid, (int) ACPI_GPE_INVALID, NUM_GPE);
253 * Initialize the Gpe information and validation blocks. A goal of these
254 * blocks is to hide the fact that there are two separate GPE register sets
255 * In a given block, the status registers occupy the first half, and
256 * the enable registers occupy the second half.
259 /* GPE Block 0 */
261 register_index = 0;
263 for (i = 0; i < gpe0register_count; i++) {
264 acpi_gbl_gpe_registers[register_index].status_addr =
265 (u16) (acpi_gbl_FACP->gpe0blk + i);
267 acpi_gbl_gpe_registers[register_index].enable_addr =
268 (u16) (acpi_gbl_FACP->gpe0blk + i + gpe0register_count);
270 acpi_gbl_gpe_registers[register_index].gpe_base = (u8) MUL_8 (i);
272 for (j = 0; j < 8; j++) {
273 gpe_number = acpi_gbl_gpe_registers[register_index].gpe_base + j;
274 acpi_gbl_gpe_valid[gpe_number] = (u8) register_index;
278 * Clear the status/enable registers. Note that status registers
279 * are cleared by writing a '1', while enable registers are cleared
280 * by writing a '0'.
282 acpi_os_out8 (acpi_gbl_gpe_registers[register_index].enable_addr, 0x00);
283 acpi_os_out8 (acpi_gbl_gpe_registers[register_index].status_addr, 0xFF);
285 register_index++;
288 /* GPE Block 1 */
290 for (i = 0; i < gpe1_register_count; i++) {
291 acpi_gbl_gpe_registers[register_index].status_addr =
292 (u16) (acpi_gbl_FACP->gpe1_blk + i);
294 acpi_gbl_gpe_registers[register_index].enable_addr =
295 (u16) (acpi_gbl_FACP->gpe1_blk + i + gpe1_register_count);
297 acpi_gbl_gpe_registers[register_index].gpe_base =
298 (u8) (acpi_gbl_FACP->gpe1_base + MUL_8 (i));
300 for (j = 0; j < 8; j++) {
301 gpe_number = acpi_gbl_gpe_registers[register_index].gpe_base + j;
302 acpi_gbl_gpe_valid[gpe_number] = (u8) register_index;
306 * Clear the status/enable registers. Note that status registers
307 * are cleared by writing a '1', while enable registers are cleared
308 * by writing a '0'.
310 acpi_os_out8 (acpi_gbl_gpe_registers[register_index].enable_addr, 0x00);
311 acpi_os_out8 (acpi_gbl_gpe_registers[register_index].status_addr, 0xFF);
313 register_index++;
316 return (AE_OK);
320 /******************************************************************************
322 * FUNCTION: Acpi_ev_save_method_info
324 * PARAMETERS: None
326 * RETURN: None
328 * DESCRIPTION: Called from Acpi_walk_namespace. Expects each object to be a
329 * control method under the _GPE portion of the namespace.
330 * Extract the name and GPE type from the object, saving this
331 * information for quick lookup during GPE dispatch
333 * The name of each GPE control method is of the form:
334 * "_Lnn" or "_Enn"
335 * Where:
336 * L - means that the GPE is level triggered
337 * E - means that the GPE is edge triggered
338 * nn - is the GPE number
340 ******************************************************************************/
342 ACPI_STATUS
343 acpi_ev_save_method_info (
344 ACPI_HANDLE obj_handle,
345 u32 level,
346 void *obj_desc,
347 void **return_value)
349 u32 gpe_number;
350 NATIVE_CHAR name[ACPI_NAME_SIZE + 1];
351 u8 type;
354 /* Extract the name from the object and convert to a string */
356 MOVE_UNALIGNED32_TO_32 (name, &((ACPI_NAMESPACE_NODE *) obj_handle)->name);
357 name[ACPI_NAME_SIZE] = 0;
360 * Edge/Level determination is based on the 2nd s8 of the method name
362 if (name[1] == 'L') {
363 type = ACPI_EVENT_LEVEL_TRIGGERED;
365 else if (name[1] == 'E') {
366 type = ACPI_EVENT_EDGE_TRIGGERED;
368 else {
369 /* Unknown method type, just ignore it! */
371 return (AE_OK);
374 /* Convert the last two characters of the name to the Gpe Number */
376 gpe_number = STRTOUL (&name[2], NULL, 16);
377 if (gpe_number == ACPI_UINT32_MAX) {
378 /* Conversion failed; invalid method, just ignore it */
380 return (AE_OK);
383 /* Ensure that we have a valid GPE number */
385 if (acpi_gbl_gpe_valid[gpe_number] == ACPI_GPE_INVALID) {
386 /* Not valid, all we can do here is ignore it */
388 return (AE_OK);
392 * Now we can add this information to the Gpe_info block
393 * for use during dispatch of this GPE.
396 acpi_gbl_gpe_info [gpe_number].type = type;
397 acpi_gbl_gpe_info [gpe_number].method_handle = obj_handle;
401 * Enable the GPE (SCIs should be disabled at this point)
404 acpi_hw_enable_gpe (gpe_number);
406 return (AE_OK);
410 /******************************************************************************
412 * FUNCTION: Acpi_ev_init_gpe_control_methods
414 * PARAMETERS: None
416 * RETURN: None
418 * DESCRIPTION: Obtain the control methods associated with the GPEs.
420 * NOTE: Must be called AFTER namespace initialization!
422 ******************************************************************************/
424 ACPI_STATUS
425 acpi_ev_init_gpe_control_methods (void)
427 ACPI_STATUS status;
430 /* Get a permanent handle to the _GPE object */
432 status = acpi_get_handle (NULL, "\\_GPE", &acpi_gbl_gpe_obj_handle);
433 if (ACPI_FAILURE (status)) {
434 return (status);
437 /* Traverse the namespace under \_GPE to find all methods there */
439 status = acpi_walk_namespace (ACPI_TYPE_METHOD, acpi_gbl_gpe_obj_handle,
440 ACPI_UINT32_MAX, acpi_ev_save_method_info,
441 NULL, NULL);
443 return (status);
447 /******************************************************************************
449 * FUNCTION: Acpi_ev_gpe_cleanup
451 * PARAMETERS: None
453 * RETURN: None
455 * DESCRIPTION: Cleanup in preparation for unload.
457 ******************************************************************************/
459 void
460 acpi_ev_gpe_cleanup (void)
463 acpi_cm_free (acpi_gbl_gpe_registers);
464 acpi_cm_free (acpi_gbl_gpe_info);
466 return;
470 /******************************************************************************
472 * FUNCTION: Acpi_ev_gpe_detect
474 * PARAMETERS: None
476 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
478 * DESCRIPTION: Detect if any GP events have occurred
480 ******************************************************************************/
483 acpi_ev_gpe_detect (void)
485 u32 int_status = INTERRUPT_NOT_HANDLED;
486 u32 i;
487 u32 j;
488 u8 enabled_status_byte;
489 u8 bit_mask;
493 * Read all of the 8-bit GPE status and enable registers
494 * in both of the register blocks, saving all of it.
495 * Find all currently active GP events.
498 for (i = 0; i < acpi_gbl_gpe_register_count; i++) {
499 acpi_gbl_gpe_registers[i].status =
500 acpi_os_in8 (acpi_gbl_gpe_registers[i].status_addr);
502 acpi_gbl_gpe_registers[i].enable =
503 acpi_os_in8 (acpi_gbl_gpe_registers[i].enable_addr);
505 /* First check if there is anything active at all in this register */
507 enabled_status_byte = (u8) (acpi_gbl_gpe_registers[i].status &
508 acpi_gbl_gpe_registers[i].enable);
510 if (!enabled_status_byte) {
511 /* No active GPEs in this register, move on */
513 continue;
516 /* Now look at the individual GPEs in this byte register */
518 for (j = 0, bit_mask = 1; j < 8; j++, bit_mask <<= 1) {
519 /* Examine one GPE bit */
521 if (enabled_status_byte & bit_mask) {
523 * Found an active GPE. Dispatch the event to a handler
524 * or method.
526 int_status |=
527 acpi_ev_gpe_dispatch (acpi_gbl_gpe_registers[i].gpe_base + j);
532 return (int_status);
536 /******************************************************************************
538 * FUNCTION: Acpi_ev_asynch_execute_gpe_method
540 * PARAMETERS: Gpe_number - The 0-based Gpe number
542 * RETURN: None
544 * DESCRIPTION: Perform the actual execution of a GPE control method. This
545 * function is called from an invocation of Acpi_os_queue_for_execution
546 * (and therefore does NOT execute at interrupt level) so that
547 * the control method itself is not executed in the context of
548 * the SCI interrupt handler.
550 ******************************************************************************/
552 void
553 acpi_ev_asynch_execute_gpe_method (
554 void *context)
556 u32 gpe_number = (u32) context;
557 ACPI_GPE_LEVEL_INFO gpe_info;
560 /* Take a snapshot of the GPE info for this level */
562 acpi_cm_acquire_mutex (ACPI_MTX_EVENTS);
563 gpe_info = acpi_gbl_gpe_info [gpe_number];
564 acpi_cm_release_mutex (ACPI_MTX_EVENTS);
567 * Function Handler (e.g. EC):
568 * ---------------------------
569 * Execute the installed function handler to handle this event.
571 if (gpe_info.handler) {
572 gpe_info.handler (gpe_info.context);
576 * Method Handler (_Lxx, _Exx):
577 * ----------------------------
578 * Acpi_evaluate the _Lxx/_Exx control method that corresponds to this GPE.
580 else if (gpe_info.method_handle) {
581 acpi_ns_evaluate_by_handle (gpe_info.method_handle, NULL, NULL);
585 * Level-Triggered?
586 * ----------------
587 * If level-triggered, clear the GPE status bit after execution. Note
588 * that edge-triggered events are cleared prior to calling (via DPC)
589 * this function.
591 if (gpe_info.type | ACPI_EVENT_LEVEL_TRIGGERED) {
592 acpi_hw_clear_gpe (gpe_number);
596 * Enable the GPE.
598 acpi_hw_enable_gpe (gpe_number);
600 return;
604 /******************************************************************************
606 * FUNCTION: Acpi_ev_gpe_dispatch
608 * PARAMETERS: Gpe_number - The 0-based Gpe number
610 * RETURN: INTERRUPT_HANDLED or INTERRUPT_NOT_HANDLED
612 * DESCRIPTION: Handle and dispatch a General Purpose Acpi_event.
613 * Clears the status bit for the requested event.
615 * TBD: [Investigate] is this still valid or necessary:
616 * The Gpe handler differs from the fixed events in that it clears the enable
617 * bit rather than the status bit to clear the interrupt. This allows
618 * software outside of interrupt context to determine what caused the SCI and
619 * dispatch the correct AML.
621 ******************************************************************************/
624 acpi_ev_gpe_dispatch (
625 u32 gpe_number)
628 /*DEBUG_INCREMENT_EVENT_COUNT (EVENT_GENERAL);*/
630 /* Ensure that we have a valid GPE number */
632 if (acpi_gbl_gpe_valid[gpe_number] == ACPI_GPE_INVALID) {
633 return (INTERRUPT_NOT_HANDLED);
637 * Disable the GPE.
639 acpi_hw_disable_gpe (gpe_number);
642 * Edge-Triggered?
643 * ---------------
644 * If edge-triggered, clear the GPE status bit now. Note that
645 * level-triggered events are cleared after the GPE is serviced
646 * (see Acpi_ev_asynch_execute_gpe_method).
648 if (acpi_gbl_gpe_info [gpe_number].type | ACPI_EVENT_EDGE_TRIGGERED) {
649 acpi_hw_clear_gpe (gpe_number);
653 * Queue-up the Handler:
654 * ---------------------
655 * Queue the handler, which is either an installable function handler
656 * (e.g. EC) or a control method (e.g. _Lxx/_Exx) for later execution.
658 if (acpi_gbl_gpe_info [gpe_number].handler ||
659 acpi_gbl_gpe_info [gpe_number].method_handle)
661 if (ACPI_FAILURE (acpi_os_queue_for_execution (OSD_PRIORITY_GPE,
662 acpi_ev_asynch_execute_gpe_method,
663 (void*)(NATIVE_UINT)gpe_number)))
666 * Shoudn't occur, but if it does report an error. Note that
667 * the GPE will remain disabled until the ACPI Core Subsystem
668 * is restarted, or the handler is removed/reinstalled.
670 REPORT_ERROR ("Unable to queue-up handler for GPE.");
675 * Non Handled GPEs:
676 * -----------------
677 * GPEs without handlers are disabled and kept that way until a handler
678 * is registered for them.
680 else {
681 REPORT_ERROR ("No installed handler for GPE.");
684 return (INTERRUPT_HANDLED);