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