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