Automatic merge with /usr/src/ntfs-2.6.git.
[linux-2.6] / drivers / acpi / events / evgpeblk.c
1 /******************************************************************************
2  *
3  * Module Name: evgpeblk - GPE block creation and initialization.
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
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 <acpi/acevents.h>
46 #include <acpi/acnamesp.h>
47
48 #define _COMPONENT          ACPI_EVENTS
49          ACPI_MODULE_NAME    ("evgpeblk")
50
51
52 /*******************************************************************************
53  *
54  * FUNCTION:    acpi_ev_valid_gpe_event
55  *
56  * PARAMETERS:  gpe_event_info              - Info for this GPE
57  *
58  * RETURN:      TRUE if the gpe_event is valid
59  *
60  * DESCRIPTION: Validate a GPE event.  DO NOT CALL FROM INTERRUPT LEVEL.
61  *              Should be called only when the GPE lists are semaphore locked
62  *              and not subject to change.
63  *
64  ******************************************************************************/
65
66 u8
67 acpi_ev_valid_gpe_event (
68         struct acpi_gpe_event_info      *gpe_event_info)
69 {
70         struct acpi_gpe_xrupt_info      *gpe_xrupt_block;
71         struct acpi_gpe_block_info      *gpe_block;
72
73
74         ACPI_FUNCTION_ENTRY ();
75
76
77         /* No need for spin lock since we are not changing any list elements */
78
79         /* Walk the GPE interrupt levels */
80
81         gpe_xrupt_block = acpi_gbl_gpe_xrupt_list_head;
82         while (gpe_xrupt_block) {
83                 gpe_block = gpe_xrupt_block->gpe_block_list_head;
84
85                 /* Walk the GPE blocks on this interrupt level */
86
87                 while (gpe_block) {
88                         if ((&gpe_block->event_info[0] <= gpe_event_info) &&
89                                 (&gpe_block->event_info[((acpi_size) gpe_block->register_count) * 8] > gpe_event_info)) {
90                                 return (TRUE);
91                         }
92
93                         gpe_block = gpe_block->next;
94                 }
95
96                 gpe_xrupt_block = gpe_xrupt_block->next;
97         }
98
99         return (FALSE);
100 }
101
102
103 /*******************************************************************************
104  *
105  * FUNCTION:    acpi_ev_walk_gpe_list
106  *
107  * PARAMETERS:  gpe_walk_callback   - Routine called for each GPE block
108  *              Flags               - ACPI_NOT_ISR or ACPI_ISR
109  *
110  * RETURN:      Status
111  *
112  * DESCRIPTION: Walk the GPE lists.
113  *
114  ******************************************************************************/
115
116 acpi_status
117 acpi_ev_walk_gpe_list (
118         ACPI_GPE_CALLBACK       gpe_walk_callback,
119         u32                             flags)
120 {
121         struct acpi_gpe_block_info      *gpe_block;
122         struct acpi_gpe_xrupt_info      *gpe_xrupt_info;
123         acpi_status                     status = AE_OK;
124
125
126         ACPI_FUNCTION_TRACE ("ev_walk_gpe_list");
127
128
129         acpi_os_acquire_lock (acpi_gbl_gpe_lock, flags);
130
131         /* Walk the interrupt level descriptor list */
132
133         gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
134         while (gpe_xrupt_info) {
135                 /* Walk all Gpe Blocks attached to this interrupt level */
136
137                 gpe_block = gpe_xrupt_info->gpe_block_list_head;
138                 while (gpe_block) {
139                         /* One callback per GPE block */
140
141                         status = gpe_walk_callback (gpe_xrupt_info, gpe_block);
142                         if (ACPI_FAILURE (status)) {
143                                 goto unlock_and_exit;
144                         }
145
146                         gpe_block = gpe_block->next;
147                 }
148
149                 gpe_xrupt_info = gpe_xrupt_info->next;
150         }
151
152 unlock_and_exit:
153         acpi_os_release_lock (acpi_gbl_gpe_lock, flags);
154         return_ACPI_STATUS (status);
155 }
156
157
158 /******************************************************************************
159  *
160  * FUNCTION:    acpi_ev_delete_gpe_handlers
161  *
162  * PARAMETERS:  gpe_xrupt_info      - GPE Interrupt info
163  *              gpe_block           - Gpe Block info
164  *
165  * RETURN:      Status
166  *
167  * DESCRIPTION: Delete all Handler objects found in the GPE data structs.
168  *              Used only prior to termination.
169  *
170  ******************************************************************************/
171
172 acpi_status
173 acpi_ev_delete_gpe_handlers (
174         struct acpi_gpe_xrupt_info      *gpe_xrupt_info,
175         struct acpi_gpe_block_info      *gpe_block)
176 {
177         struct acpi_gpe_event_info      *gpe_event_info;
178         acpi_native_uint                i;
179         acpi_native_uint                j;
180
181
182         ACPI_FUNCTION_TRACE ("ev_delete_gpe_handlers");
183
184
185         /* Examine each GPE Register within the block */
186
187         for (i = 0; i < gpe_block->register_count; i++) {
188                 /* Now look at the individual GPEs in this byte register */
189
190                 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
191                         gpe_event_info = &gpe_block->event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j];
192
193                         if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_HANDLER) {
194                                 ACPI_MEM_FREE (gpe_event_info->dispatch.handler);
195                                 gpe_event_info->dispatch.handler = NULL;
196                                 gpe_event_info->flags &= ~ACPI_GPE_DISPATCH_MASK;
197                         }
198                 }
199         }
200
201         return_ACPI_STATUS (AE_OK);
202 }
203
204
205 /*******************************************************************************
206  *
207  * FUNCTION:    acpi_ev_save_method_info
208  *
209  * PARAMETERS:  Callback from walk_namespace
210  *
211  * RETURN:      Status
212  *
213  * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
214  *              control method under the _GPE portion of the namespace.
215  *              Extract the name and GPE type from the object, saving this
216  *              information for quick lookup during GPE dispatch
217  *
218  *              The name of each GPE control method is of the form:
219  *              "_Lxx" or "_Exx"
220  *              Where:
221  *                  L      - means that the GPE is level triggered
222  *                  E      - means that the GPE is edge triggered
223  *                  xx     - is the GPE number [in HEX]
224  *
225  ******************************************************************************/
226
227 static acpi_status
228 acpi_ev_save_method_info (
229         acpi_handle                     obj_handle,
230         u32                             level,
231         void                            *obj_desc,
232         void                            **return_value)
233 {
234         struct acpi_gpe_block_info      *gpe_block = (void *) obj_desc;
235         struct acpi_gpe_event_info      *gpe_event_info;
236         u32                             gpe_number;
237         char                            name[ACPI_NAME_SIZE + 1];
238         u8                              type;
239         acpi_status                     status;
240
241
242         ACPI_FUNCTION_TRACE ("ev_save_method_info");
243
244
245         /*
246          * _Lxx and _Exx GPE method support
247          *
248          * 1) Extract the name from the object and convert to a string
249          */
250         ACPI_MOVE_32_TO_32 (name,
251                            &((struct acpi_namespace_node *) obj_handle)->name.integer);
252         name[ACPI_NAME_SIZE] = 0;
253
254         /*
255          * 2) Edge/Level determination is based on the 2nd character
256          *    of the method name
257          *
258          * NOTE: Default GPE type is RUNTIME.  May be changed later to WAKE
259          * if a _PRW object is found that points to this GPE.
260          */
261         switch (name[1]) {
262         case 'L':
263                 type = ACPI_GPE_LEVEL_TRIGGERED;
264                 break;
265
266         case 'E':
267                 type = ACPI_GPE_EDGE_TRIGGERED;
268                 break;
269
270         default:
271                 /* Unknown method type, just ignore it! */
272
273                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
274                         "Unknown GPE method type: %s (name not of form _Lxx or _Exx)\n",
275                         name));
276                 return_ACPI_STATUS (AE_OK);
277         }
278
279         /* Convert the last two characters of the name to the GPE Number */
280
281         gpe_number = ACPI_STRTOUL (&name[2], NULL, 16);
282         if (gpe_number == ACPI_UINT32_MAX) {
283                 /* Conversion failed; invalid method, just ignore it */
284
285                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
286                         "Could not extract GPE number from name: %s (name is not of form _Lxx or _Exx)\n",
287                         name));
288                 return_ACPI_STATUS (AE_OK);
289         }
290
291         /* Ensure that we have a valid GPE number for this GPE block */
292
293         if ((gpe_number < gpe_block->block_base_number) ||
294                 (gpe_number >= (gpe_block->block_base_number + (gpe_block->register_count * 8)))) {
295                 /*
296                  * Not valid for this GPE block, just ignore it
297                  * However, it may be valid for a different GPE block, since GPE0 and GPE1
298                  * methods both appear under \_GPE.
299                  */
300                 return_ACPI_STATUS (AE_OK);
301         }
302
303         /*
304          * Now we can add this information to the gpe_event_info block
305          * for use during dispatch of this GPE.  Default type is RUNTIME, although
306          * this may change when the _PRW methods are executed later.
307          */
308         gpe_event_info = &gpe_block->event_info[gpe_number - gpe_block->block_base_number];
309
310         gpe_event_info->flags = (u8) (type | ACPI_GPE_DISPATCH_METHOD |
311                            ACPI_GPE_TYPE_RUNTIME);
312
313         gpe_event_info->dispatch.method_node = (struct acpi_namespace_node *) obj_handle;
314
315         /* Update enable mask, but don't enable the HW GPE as of yet */
316
317         status = acpi_ev_enable_gpe (gpe_event_info, FALSE);
318
319         ACPI_DEBUG_PRINT ((ACPI_DB_LOAD,
320                 "Registered GPE method %s as GPE number 0x%.2X\n",
321                 name, gpe_number));
322         return_ACPI_STATUS (status);
323 }
324
325
326 /*******************************************************************************
327  *
328  * FUNCTION:    acpi_ev_match_prw_and_gpe
329  *
330  * PARAMETERS:  Callback from walk_namespace
331  *
332  * RETURN:      Status.  NOTE: We ignore errors so that the _PRW walk is
333  *              not aborted on a single _PRW failure.
334  *
335  * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
336  *              Device.  Run the _PRW method.  If present, extract the GPE
337  *              number and mark the GPE as a WAKE GPE.
338  *
339  ******************************************************************************/
340
341 static acpi_status
342 acpi_ev_match_prw_and_gpe (
343         acpi_handle                     obj_handle,
344         u32                             level,
345         void                            *info,
346         void                            **return_value)
347 {
348         struct acpi_gpe_walk_info       *gpe_info = (void *) info;
349         struct acpi_namespace_node      *gpe_device;
350         struct acpi_gpe_block_info      *gpe_block;
351         struct acpi_namespace_node      *target_gpe_device;
352         struct acpi_gpe_event_info      *gpe_event_info;
353         union acpi_operand_object       *pkg_desc;
354         union acpi_operand_object       *obj_desc;
355         u32                             gpe_number;
356         acpi_status                     status;
357
358
359         ACPI_FUNCTION_TRACE ("ev_match_prw_and_gpe");
360
361
362         /* Check for a _PRW method under this device */
363
364         status = acpi_ut_evaluate_object (obj_handle, METHOD_NAME__PRW,
365                          ACPI_BTYPE_PACKAGE, &pkg_desc);
366         if (ACPI_FAILURE (status)) {
367                 /* Ignore all errors from _PRW, we don't want to abort the subsystem */
368
369                 return_ACPI_STATUS (AE_OK);
370         }
371
372         /* The returned _PRW package must have at least two elements */
373
374         if (pkg_desc->package.count < 2) {
375                 goto cleanup;
376         }
377
378         /* Extract pointers from the input context */
379
380         gpe_device = gpe_info->gpe_device;
381         gpe_block = gpe_info->gpe_block;
382
383         /*
384          * The _PRW object must return a package, we are only interested
385          * in the first element
386          */
387         obj_desc = pkg_desc->package.elements[0];
388
389         if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_INTEGER) {
390                 /* Use FADT-defined GPE device (from definition of _PRW) */
391
392                 target_gpe_device = acpi_gbl_fadt_gpe_device;
393
394                 /* Integer is the GPE number in the FADT described GPE blocks */
395
396                 gpe_number = (u32) obj_desc->integer.value;
397         }
398         else if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_PACKAGE) {
399                 /* Package contains a GPE reference and GPE number within a GPE block */
400
401                 if ((obj_desc->package.count < 2) ||
402                         (ACPI_GET_OBJECT_TYPE (obj_desc->package.elements[0]) != ACPI_TYPE_LOCAL_REFERENCE) ||
403                         (ACPI_GET_OBJECT_TYPE (obj_desc->package.elements[1]) != ACPI_TYPE_INTEGER)) {
404                         goto cleanup;
405                 }
406
407                 /* Get GPE block reference and decode */
408
409                 target_gpe_device = obj_desc->package.elements[0]->reference.node;
410                 gpe_number = (u32) obj_desc->package.elements[1]->integer.value;
411         }
412         else {
413                 /* Unknown type, just ignore it */
414
415                 goto cleanup;
416         }
417
418         /*
419          * Is this GPE within this block?
420          *
421          * TRUE iff these conditions are true:
422          *     1) The GPE devices match.
423          *     2) The GPE index(number) is within the range of the Gpe Block
424          *          associated with the GPE device.
425          */
426         if ((gpe_device == target_gpe_device) &&
427                 (gpe_number >= gpe_block->block_base_number) &&
428                 (gpe_number < gpe_block->block_base_number + (gpe_block->register_count * 8))) {
429                 gpe_event_info = &gpe_block->event_info[gpe_number - gpe_block->block_base_number];
430
431                 /* Mark GPE for WAKE-ONLY but WAKE_DISABLED */
432
433                 gpe_event_info->flags &= ~(ACPI_GPE_WAKE_ENABLED | ACPI_GPE_RUN_ENABLED);
434                 status = acpi_ev_set_gpe_type (gpe_event_info, ACPI_GPE_TYPE_WAKE);
435                 if (ACPI_FAILURE (status)) {
436                         goto cleanup;
437                 }
438                 status = acpi_ev_update_gpe_enable_masks (gpe_event_info, ACPI_GPE_DISABLE);
439         }
440
441 cleanup:
442         acpi_ut_remove_reference (pkg_desc);
443         return_ACPI_STATUS (AE_OK);
444 }
445
446
447 /*******************************************************************************
448  *
449  * FUNCTION:    acpi_ev_get_gpe_xrupt_block
450  *
451  * PARAMETERS:  interrupt_level     - Interrupt for a GPE block
452  *
453  * RETURN:      A GPE interrupt block
454  *
455  * DESCRIPTION: Get or Create a GPE interrupt block.  There is one interrupt
456  *              block per unique interrupt level used for GPEs.
457  *              Should be called only when the GPE lists are semaphore locked
458  *              and not subject to change.
459  *
460  ******************************************************************************/
461
462 static struct acpi_gpe_xrupt_info *
463 acpi_ev_get_gpe_xrupt_block (
464         u32                             interrupt_level)
465 {
466         struct acpi_gpe_xrupt_info      *next_gpe_xrupt;
467         struct acpi_gpe_xrupt_info      *gpe_xrupt;
468         acpi_status                     status;
469
470
471         ACPI_FUNCTION_TRACE ("ev_get_gpe_xrupt_block");
472
473
474         /* No need for spin lock since we are not changing any list elements here */
475
476         next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
477         while (next_gpe_xrupt) {
478                 if (next_gpe_xrupt->interrupt_level == interrupt_level) {
479                         return_PTR (next_gpe_xrupt);
480                 }
481
482                 next_gpe_xrupt = next_gpe_xrupt->next;
483         }
484
485         /* Not found, must allocate a new xrupt descriptor */
486
487         gpe_xrupt = ACPI_MEM_CALLOCATE (sizeof (struct acpi_gpe_xrupt_info));
488         if (!gpe_xrupt) {
489                 return_PTR (NULL);
490         }
491
492         gpe_xrupt->interrupt_level = interrupt_level;
493
494         /* Install new interrupt descriptor with spin lock */
495
496         acpi_os_acquire_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
497         if (acpi_gbl_gpe_xrupt_list_head) {
498                 next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
499                 while (next_gpe_xrupt->next) {
500                         next_gpe_xrupt = next_gpe_xrupt->next;
501                 }
502
503                 next_gpe_xrupt->next = gpe_xrupt;
504                 gpe_xrupt->previous = next_gpe_xrupt;
505         }
506         else {
507                 acpi_gbl_gpe_xrupt_list_head = gpe_xrupt;
508         }
509         acpi_os_release_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
510
511         /* Install new interrupt handler if not SCI_INT */
512
513         if (interrupt_level != acpi_gbl_FADT->sci_int) {
514                 status = acpi_os_install_interrupt_handler (interrupt_level,
515                                  acpi_ev_gpe_xrupt_handler, gpe_xrupt);
516                 if (ACPI_FAILURE (status)) {
517                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
518                                 "Could not install GPE interrupt handler at level 0x%X\n",
519                                 interrupt_level));
520                         return_PTR (NULL);
521                 }
522         }
523
524         return_PTR (gpe_xrupt);
525 }
526
527
528 /*******************************************************************************
529  *
530  * FUNCTION:    acpi_ev_delete_gpe_xrupt
531  *
532  * PARAMETERS:  gpe_xrupt       - A GPE interrupt info block
533  *
534  * RETURN:      Status
535  *
536  * DESCRIPTION: Remove and free a gpe_xrupt block. Remove an associated
537  *              interrupt handler if not the SCI interrupt.
538  *
539  ******************************************************************************/
540
541 static acpi_status
542 acpi_ev_delete_gpe_xrupt (
543         struct acpi_gpe_xrupt_info      *gpe_xrupt)
544 {
545         acpi_status                     status;
546
547
548         ACPI_FUNCTION_TRACE ("ev_delete_gpe_xrupt");
549
550
551         /* We never want to remove the SCI interrupt handler */
552
553         if (gpe_xrupt->interrupt_level == acpi_gbl_FADT->sci_int) {
554                 gpe_xrupt->gpe_block_list_head = NULL;
555                 return_ACPI_STATUS (AE_OK);
556         }
557
558         /* Disable this interrupt */
559
560         status = acpi_os_remove_interrupt_handler (gpe_xrupt->interrupt_level,
561                            acpi_ev_gpe_xrupt_handler);
562         if (ACPI_FAILURE (status)) {
563                 return_ACPI_STATUS (status);
564         }
565
566         /* Unlink the interrupt block with lock */
567
568         acpi_os_acquire_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
569         if (gpe_xrupt->previous) {
570                 gpe_xrupt->previous->next = gpe_xrupt->next;
571         }
572
573         if (gpe_xrupt->next) {
574                 gpe_xrupt->next->previous = gpe_xrupt->previous;
575         }
576         acpi_os_release_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
577
578         /* Free the block */
579
580         ACPI_MEM_FREE (gpe_xrupt);
581         return_ACPI_STATUS (AE_OK);
582 }
583
584
585 /*******************************************************************************
586  *
587  * FUNCTION:    acpi_ev_install_gpe_block
588  *
589  * PARAMETERS:  gpe_block       - New GPE block
590  *              interrupt_level - Level to be associated with this GPE block
591  *
592  * RETURN:      Status
593  *
594  * DESCRIPTION: Install new GPE block with mutex support
595  *
596  ******************************************************************************/
597
598 static acpi_status
599 acpi_ev_install_gpe_block (
600         struct acpi_gpe_block_info      *gpe_block,
601         u32                             interrupt_level)
602 {
603         struct acpi_gpe_block_info      *next_gpe_block;
604         struct acpi_gpe_xrupt_info      *gpe_xrupt_block;
605         acpi_status                     status;
606
607
608         ACPI_FUNCTION_TRACE ("ev_install_gpe_block");
609
610
611         status = acpi_ut_acquire_mutex (ACPI_MTX_EVENTS);
612         if (ACPI_FAILURE (status)) {
613                 return_ACPI_STATUS (status);
614         }
615
616         gpe_xrupt_block = acpi_ev_get_gpe_xrupt_block (interrupt_level);
617         if (!gpe_xrupt_block) {
618                 status = AE_NO_MEMORY;
619                 goto unlock_and_exit;
620         }
621
622         /* Install the new block at the end of the list for this interrupt with lock */
623
624         acpi_os_acquire_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
625         if (gpe_xrupt_block->gpe_block_list_head) {
626                 next_gpe_block = gpe_xrupt_block->gpe_block_list_head;
627                 while (next_gpe_block->next) {
628                         next_gpe_block = next_gpe_block->next;
629                 }
630
631                 next_gpe_block->next = gpe_block;
632                 gpe_block->previous = next_gpe_block;
633         }
634         else {
635                 gpe_xrupt_block->gpe_block_list_head = gpe_block;
636         }
637
638         gpe_block->xrupt_block = gpe_xrupt_block;
639         acpi_os_release_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
640
641 unlock_and_exit:
642         status = acpi_ut_release_mutex (ACPI_MTX_EVENTS);
643         return_ACPI_STATUS (status);
644 }
645
646
647 /*******************************************************************************
648  *
649  * FUNCTION:    acpi_ev_delete_gpe_block
650  *
651  * PARAMETERS:  gpe_block       - Existing GPE block
652  *
653  * RETURN:      Status
654  *
655  * DESCRIPTION: Remove a GPE block
656  *
657  ******************************************************************************/
658
659 acpi_status
660 acpi_ev_delete_gpe_block (
661         struct acpi_gpe_block_info      *gpe_block)
662 {
663         acpi_status                     status;
664
665
666         ACPI_FUNCTION_TRACE ("ev_install_gpe_block");
667
668
669         status = acpi_ut_acquire_mutex (ACPI_MTX_EVENTS);
670         if (ACPI_FAILURE (status)) {
671                 return_ACPI_STATUS (status);
672         }
673
674         /* Disable all GPEs in this block */
675
676         status = acpi_hw_disable_gpe_block (gpe_block->xrupt_block, gpe_block);
677
678         if (!gpe_block->previous && !gpe_block->next) {
679                 /* This is the last gpe_block on this interrupt */
680
681                 status = acpi_ev_delete_gpe_xrupt (gpe_block->xrupt_block);
682                 if (ACPI_FAILURE (status)) {
683                         goto unlock_and_exit;
684                 }
685         }
686         else {
687                 /* Remove the block on this interrupt with lock */
688
689                 acpi_os_acquire_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
690                 if (gpe_block->previous) {
691                         gpe_block->previous->next = gpe_block->next;
692                 }
693                 else {
694                         gpe_block->xrupt_block->gpe_block_list_head = gpe_block->next;
695                 }
696
697                 if (gpe_block->next) {
698                         gpe_block->next->previous = gpe_block->previous;
699                 }
700                 acpi_os_release_lock (acpi_gbl_gpe_lock, ACPI_NOT_ISR);
701         }
702
703         /* Free the gpe_block */
704
705         ACPI_MEM_FREE (gpe_block->register_info);
706         ACPI_MEM_FREE (gpe_block->event_info);
707         ACPI_MEM_FREE (gpe_block);
708
709 unlock_and_exit:
710         status = acpi_ut_release_mutex (ACPI_MTX_EVENTS);
711         return_ACPI_STATUS (status);
712 }
713
714
715 /*******************************************************************************
716  *
717  * FUNCTION:    acpi_ev_create_gpe_info_blocks
718  *
719  * PARAMETERS:  gpe_block   - New GPE block
720  *
721  * RETURN:      Status
722  *
723  * DESCRIPTION: Create the register_info and event_info blocks for this GPE block
724  *
725  ******************************************************************************/
726
727 static acpi_status
728 acpi_ev_create_gpe_info_blocks (
729         struct acpi_gpe_block_info      *gpe_block)
730 {
731         struct acpi_gpe_register_info   *gpe_register_info = NULL;
732         struct acpi_gpe_event_info      *gpe_event_info = NULL;
733         struct acpi_gpe_event_info      *this_event;
734         struct acpi_gpe_register_info   *this_register;
735         acpi_native_uint                i;
736         acpi_native_uint                j;
737         acpi_status                     status;
738
739
740         ACPI_FUNCTION_TRACE ("ev_create_gpe_info_blocks");
741
742
743         /* Allocate the GPE register information block */
744
745         gpe_register_info = ACPI_MEM_CALLOCATE (
746                           (acpi_size) gpe_block->register_count *
747                           sizeof (struct acpi_gpe_register_info));
748         if (!gpe_register_info) {
749                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
750                         "Could not allocate the gpe_register_info table\n"));
751                 return_ACPI_STATUS (AE_NO_MEMORY);
752         }
753
754         /*
755          * Allocate the GPE event_info block. There are eight distinct GPEs
756          * per register.  Initialization to zeros is sufficient.
757          */
758         gpe_event_info = ACPI_MEM_CALLOCATE (
759                            ((acpi_size) gpe_block->register_count * ACPI_GPE_REGISTER_WIDTH) *
760                            sizeof (struct acpi_gpe_event_info));
761         if (!gpe_event_info) {
762                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not allocate the gpe_event_info table\n"));
763                 status = AE_NO_MEMORY;
764                 goto error_exit;
765         }
766
767         /* Save the new Info arrays in the GPE block */
768
769         gpe_block->register_info = gpe_register_info;
770         gpe_block->event_info  = gpe_event_info;
771
772         /*
773          * Initialize the GPE Register and Event structures.  A goal of these
774          * tables is to hide the fact that there are two separate GPE register sets
775          * in a given gpe hardware block, the status registers occupy the first half,
776          * and the enable registers occupy the second half.
777          */
778         this_register = gpe_register_info;
779         this_event   = gpe_event_info;
780
781         for (i = 0; i < gpe_block->register_count; i++) {
782                 /* Init the register_info for this GPE register (8 GPEs) */
783
784                 this_register->base_gpe_number = (u8) (gpe_block->block_base_number +
785                                    (i * ACPI_GPE_REGISTER_WIDTH));
786
787                 ACPI_STORE_ADDRESS (this_register->status_address.address,
788                                  (gpe_block->block_address.address
789                                  + i));
790
791                 ACPI_STORE_ADDRESS (this_register->enable_address.address,
792                                  (gpe_block->block_address.address
793                                  + i
794                                  + gpe_block->register_count));
795
796                 this_register->status_address.address_space_id = gpe_block->block_address.address_space_id;
797                 this_register->enable_address.address_space_id = gpe_block->block_address.address_space_id;
798                 this_register->status_address.register_bit_width = ACPI_GPE_REGISTER_WIDTH;
799                 this_register->enable_address.register_bit_width = ACPI_GPE_REGISTER_WIDTH;
800                 this_register->status_address.register_bit_offset = ACPI_GPE_REGISTER_WIDTH;
801                 this_register->enable_address.register_bit_offset = ACPI_GPE_REGISTER_WIDTH;
802
803                 /* Init the event_info for each GPE within this register */
804
805                 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
806                         this_event->register_bit = acpi_gbl_decode_to8bit[j];
807                         this_event->register_info = this_register;
808                         this_event++;
809                 }
810
811                 /*
812                  * Clear the status/enable registers.  Note that status registers
813                  * are cleared by writing a '1', while enable registers are cleared
814                  * by writing a '0'.
815                  */
816                 status = acpi_hw_low_level_write (ACPI_GPE_REGISTER_WIDTH, 0x00,
817                                  &this_register->enable_address);
818                 if (ACPI_FAILURE (status)) {
819                         goto error_exit;
820                 }
821
822                 status = acpi_hw_low_level_write (ACPI_GPE_REGISTER_WIDTH, 0xFF,
823                                  &this_register->status_address);
824                 if (ACPI_FAILURE (status)) {
825                         goto error_exit;
826                 }
827
828                 this_register++;
829         }
830
831         return_ACPI_STATUS (AE_OK);
832
833
834 error_exit:
835         if (gpe_register_info) {
836                 ACPI_MEM_FREE (gpe_register_info);
837         }
838         if (gpe_event_info) {
839                 ACPI_MEM_FREE (gpe_event_info);
840         }
841
842         return_ACPI_STATUS (status);
843 }
844
845
846 /*******************************************************************************
847  *
848  * FUNCTION:    acpi_ev_create_gpe_block
849  *
850  * PARAMETERS:  gpe_device          - Handle to the parent GPE block
851  *              gpe_block_address   - Address and space_iD
852  *              register_count      - Number of GPE register pairs in the block
853  *              gpe_block_base_number - Starting GPE number for the block
854  *              interrupt_level     - H/W interrupt for the block
855  *              return_gpe_block    - Where the new block descriptor is returned
856  *
857  * RETURN:      Status
858  *
859  * DESCRIPTION: Create and Install a block of GPE registers
860  *
861  ******************************************************************************/
862
863 acpi_status
864 acpi_ev_create_gpe_block (
865         struct acpi_namespace_node      *gpe_device,
866         struct acpi_generic_address     *gpe_block_address,
867         u32                             register_count,
868         u8                              gpe_block_base_number,
869         u32                             interrupt_level,
870         struct acpi_gpe_block_info      **return_gpe_block)
871 {
872         struct acpi_gpe_block_info      *gpe_block;
873         struct acpi_gpe_event_info      *gpe_event_info;
874         acpi_native_uint                i;
875         acpi_native_uint                j;
876         u32                             wake_gpe_count;
877         u32                             gpe_enabled_count;
878         acpi_status                     status;
879         struct acpi_gpe_walk_info       gpe_info;
880
881
882         ACPI_FUNCTION_TRACE ("ev_create_gpe_block");
883
884
885         if (!register_count) {
886                 return_ACPI_STATUS (AE_OK);
887         }
888
889         /* Allocate a new GPE block */
890
891         gpe_block = ACPI_MEM_CALLOCATE (sizeof (struct acpi_gpe_block_info));
892         if (!gpe_block) {
893                 return_ACPI_STATUS (AE_NO_MEMORY);
894         }
895
896         /* Initialize the new GPE block */
897
898         gpe_block->register_count = register_count;
899         gpe_block->block_base_number = gpe_block_base_number;
900         gpe_block->node           = gpe_device;
901
902         ACPI_MEMCPY (&gpe_block->block_address, gpe_block_address, sizeof (struct acpi_generic_address));
903
904         /* Create the register_info and event_info sub-structures */
905
906         status = acpi_ev_create_gpe_info_blocks (gpe_block);
907         if (ACPI_FAILURE (status)) {
908                 ACPI_MEM_FREE (gpe_block);
909                 return_ACPI_STATUS (status);
910         }
911
912         /* Install the new block in the global list(s) */
913
914         status = acpi_ev_install_gpe_block (gpe_block, interrupt_level);
915         if (ACPI_FAILURE (status)) {
916                 ACPI_MEM_FREE (gpe_block);
917                 return_ACPI_STATUS (status);
918         }
919
920         /* Find all GPE methods (_Lxx, _Exx) for this block */
921
922         status = acpi_ns_walk_namespace (ACPI_TYPE_METHOD, gpe_device,
923                           ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK, acpi_ev_save_method_info,
924                           gpe_block, NULL);
925
926         /*
927          * Runtime option: Should Wake GPEs be enabled at runtime?  The default
928          * is No, they should only be enabled just as the machine goes to sleep.
929          */
930         if (acpi_gbl_leave_wake_gpes_disabled) {
931                 /*
932                  * Differentiate RUNTIME vs WAKE GPEs, via the _PRW control methods.
933                  * (Each GPE that has one or more _PRWs that reference it is by
934                  * definition a WAKE GPE and will not be enabled while the machine
935                  * is running.)
936                  */
937                 gpe_info.gpe_block = gpe_block;
938                 gpe_info.gpe_device = gpe_device;
939
940                 status = acpi_ns_walk_namespace (ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
941                                   ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK, acpi_ev_match_prw_and_gpe,
942                                   &gpe_info, NULL);
943         }
944
945         /*
946          * Enable all GPEs in this block that are 1) "runtime" or "run/wake" GPEs,
947          * and 2) have a corresponding _Lxx or _Exx method.  All other GPEs must
948          * be enabled via the acpi_enable_gpe() external interface.
949          */
950         wake_gpe_count = 0;
951         gpe_enabled_count = 0;
952
953         for (i = 0; i < gpe_block->register_count; i++) {
954                 for (j = 0; j < 8; j++) {
955                         /* Get the info block for this particular GPE */
956
957                         gpe_event_info = &gpe_block->event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j];
958
959                         if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) == ACPI_GPE_DISPATCH_METHOD) &&
960                                  (gpe_event_info->flags & ACPI_GPE_TYPE_RUNTIME)) {
961                                 gpe_enabled_count++;
962                         }
963
964                         if (gpe_event_info->flags & ACPI_GPE_TYPE_WAKE) {
965                                 wake_gpe_count++;
966                         }
967                 }
968         }
969
970         /* Dump info about this GPE block */
971
972         ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
973                 "GPE %02X to %02X [%4.4s] %u regs on int 0x%X\n",
974                 (u32) gpe_block->block_base_number,
975                 (u32) (gpe_block->block_base_number +
976                                 ((gpe_block->register_count * ACPI_GPE_REGISTER_WIDTH) -1)),
977                 gpe_device->name.ascii,
978                 gpe_block->register_count,
979                 interrupt_level));
980
981         /* Enable all valid GPEs found above */
982
983         status = acpi_hw_enable_runtime_gpe_block (NULL, gpe_block);
984
985         ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
986                         "Found %u Wake, Enabled %u Runtime GPEs in this block\n",
987                         wake_gpe_count, gpe_enabled_count));
988
989         /* Return the new block */
990
991         if (return_gpe_block) {
992                 (*return_gpe_block) = gpe_block;
993         }
994
995         return_ACPI_STATUS (AE_OK);
996 }
997
998
999 /*******************************************************************************
1000  *
1001  * FUNCTION:    acpi_ev_gpe_initialize
1002  *
1003  * PARAMETERS:  None
1004  *
1005  * RETURN:      Status
1006  *
1007  * DESCRIPTION: Initialize the GPE data structures
1008  *
1009  ******************************************************************************/
1010
1011 acpi_status
1012 acpi_ev_gpe_initialize (
1013         void)
1014 {
1015         u32                             register_count0 = 0;
1016         u32                             register_count1 = 0;
1017         u32                             gpe_number_max = 0;
1018         acpi_status                     status;
1019
1020
1021         ACPI_FUNCTION_TRACE ("ev_gpe_initialize");
1022
1023
1024         status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
1025         if (ACPI_FAILURE (status)) {
1026                 return_ACPI_STATUS (status);
1027         }
1028
1029         /*
1030          * Initialize the GPE Block(s) defined in the FADT
1031          *
1032          * Why the GPE register block lengths are divided by 2:  From the ACPI Spec,
1033          * section "General-Purpose Event Registers", we have:
1034          *
1035          * "Each register block contains two registers of equal length
1036          *  GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
1037          *  GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
1038          *  The length of the GPE1_STS and GPE1_EN registers is equal to
1039          *  half the GPE1_LEN. If a generic register block is not supported
1040          *  then its respective block pointer and block length values in the
1041          *  FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
1042          *  to be the same size."
1043          */
1044
1045         /*
1046          * Determine the maximum GPE number for this machine.
1047          *
1048          * Note: both GPE0 and GPE1 are optional, and either can exist without
1049          * the other.
1050          *
1051          * If EITHER the register length OR the block address are zero, then that
1052          * particular block is not supported.
1053          */
1054         if (acpi_gbl_FADT->gpe0_blk_len &&
1055                 acpi_gbl_FADT->xgpe0_blk.address) {
1056                 /* GPE block 0 exists (has both length and address > 0) */
1057
1058                 register_count0 = (u16) (acpi_gbl_FADT->gpe0_blk_len / 2);
1059
1060                 gpe_number_max = (register_count0 * ACPI_GPE_REGISTER_WIDTH) - 1;
1061
1062                 /* Install GPE Block 0 */
1063
1064                 status = acpi_ev_create_gpe_block (acpi_gbl_fadt_gpe_device, &acpi_gbl_FADT->xgpe0_blk,
1065                                  register_count0, 0, acpi_gbl_FADT->sci_int, &acpi_gbl_gpe_fadt_blocks[0]);
1066
1067                 if (ACPI_FAILURE (status)) {
1068                         ACPI_REPORT_ERROR ((
1069                                 "Could not create GPE Block 0, %s\n",
1070                                 acpi_format_exception (status)));
1071                 }
1072         }
1073
1074         if (acpi_gbl_FADT->gpe1_blk_len &&
1075                 acpi_gbl_FADT->xgpe1_blk.address) {
1076                 /* GPE block 1 exists (has both length and address > 0) */
1077
1078                 register_count1 = (u16) (acpi_gbl_FADT->gpe1_blk_len / 2);
1079
1080                 /* Check for GPE0/GPE1 overlap (if both banks exist) */
1081
1082                 if ((register_count0) &&
1083                         (gpe_number_max >= acpi_gbl_FADT->gpe1_base)) {
1084                         ACPI_REPORT_ERROR ((
1085                                 "GPE0 block (GPE 0 to %d) overlaps the GPE1 block (GPE %d to %d) - Ignoring GPE1\n",
1086                                 gpe_number_max, acpi_gbl_FADT->gpe1_base,
1087                                 acpi_gbl_FADT->gpe1_base +
1088                                 ((register_count1 * ACPI_GPE_REGISTER_WIDTH) - 1)));
1089
1090                         /* Ignore GPE1 block by setting the register count to zero */
1091
1092                         register_count1 = 0;
1093                 }
1094                 else {
1095                         /* Install GPE Block 1 */
1096
1097                         status = acpi_ev_create_gpe_block (acpi_gbl_fadt_gpe_device, &acpi_gbl_FADT->xgpe1_blk,
1098                                          register_count1, acpi_gbl_FADT->gpe1_base,
1099                                          acpi_gbl_FADT->sci_int, &acpi_gbl_gpe_fadt_blocks[1]);
1100
1101                         if (ACPI_FAILURE (status)) {
1102                                 ACPI_REPORT_ERROR ((
1103                                         "Could not create GPE Block 1, %s\n",
1104                                         acpi_format_exception (status)));
1105                         }
1106
1107                         /*
1108                          * GPE0 and GPE1 do not have to be contiguous in the GPE number
1109                          * space. However, GPE0 always starts at GPE number zero.
1110                          */
1111                         gpe_number_max = acpi_gbl_FADT->gpe1_base +
1112                                            ((register_count1 * ACPI_GPE_REGISTER_WIDTH) - 1);
1113                 }
1114         }
1115
1116         /* Exit if there are no GPE registers */
1117
1118         if ((register_count0 + register_count1) == 0) {
1119                 /* GPEs are not required by ACPI, this is OK */
1120
1121                 ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
1122                                 "There are no GPE blocks defined in the FADT\n"));
1123                 status = AE_OK;
1124                 goto cleanup;
1125         }
1126
1127         /* Check for Max GPE number out-of-range */
1128
1129         if (gpe_number_max > ACPI_GPE_MAX) {
1130                 ACPI_REPORT_ERROR (("Maximum GPE number from FADT is too large: 0x%X\n",
1131                         gpe_number_max));
1132                 status = AE_BAD_VALUE;
1133                 goto cleanup;
1134         }
1135
1136 cleanup:
1137         (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
1138         return_ACPI_STATUS (AE_OK);
1139 }
1140
1141