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