Merge branch 'drm-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[pandora-kernel.git] / drivers / acpi / acpica / evgpeinit.c
1 /******************************************************************************
2  *
3  * Module Name: evgpeinit - System GPE initialization and update
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 #include "acinterp.h"
49
50 #define _COMPONENT          ACPI_EVENTS
51 ACPI_MODULE_NAME("evgpeinit")
52
53 /*******************************************************************************
54  *
55  * FUNCTION:    acpi_ev_gpe_initialize
56  *
57  * PARAMETERS:  None
58  *
59  * RETURN:      Status
60  *
61  * DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks
62  *
63  ******************************************************************************/
64 acpi_status acpi_ev_gpe_initialize(void)
65 {
66         u32 register_count0 = 0;
67         u32 register_count1 = 0;
68         u32 gpe_number_max = 0;
69         acpi_status status;
70
71         ACPI_FUNCTION_TRACE(ev_gpe_initialize);
72
73         status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
74         if (ACPI_FAILURE(status)) {
75                 return_ACPI_STATUS(status);
76         }
77
78         /*
79          * Initialize the GPE Block(s) defined in the FADT
80          *
81          * Why the GPE register block lengths are divided by 2:  From the ACPI
82          * Spec, section "General-Purpose Event Registers", we have:
83          *
84          * "Each register block contains two registers of equal length
85          *  GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
86          *  GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
87          *  The length of the GPE1_STS and GPE1_EN registers is equal to
88          *  half the GPE1_LEN. If a generic register block is not supported
89          *  then its respective block pointer and block length values in the
90          *  FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
91          *  to be the same size."
92          */
93
94         /*
95          * Determine the maximum GPE number for this machine.
96          *
97          * Note: both GPE0 and GPE1 are optional, and either can exist without
98          * the other.
99          *
100          * If EITHER the register length OR the block address are zero, then that
101          * particular block is not supported.
102          */
103         if (acpi_gbl_FADT.gpe0_block_length &&
104             acpi_gbl_FADT.xgpe0_block.address) {
105
106                 /* GPE block 0 exists (has both length and address > 0) */
107
108                 register_count0 = (u16)(acpi_gbl_FADT.gpe0_block_length / 2);
109
110                 gpe_number_max =
111                     (register_count0 * ACPI_GPE_REGISTER_WIDTH) - 1;
112
113                 /* Install GPE Block 0 */
114
115                 status = acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
116                                                   &acpi_gbl_FADT.xgpe0_block,
117                                                   register_count0, 0,
118                                                   acpi_gbl_FADT.sci_interrupt,
119                                                   &acpi_gbl_gpe_fadt_blocks[0]);
120
121                 if (ACPI_FAILURE(status)) {
122                         ACPI_EXCEPTION((AE_INFO, status,
123                                         "Could not create GPE Block 0"));
124                 }
125         }
126
127         if (acpi_gbl_FADT.gpe1_block_length &&
128             acpi_gbl_FADT.xgpe1_block.address) {
129
130                 /* GPE block 1 exists (has both length and address > 0) */
131
132                 register_count1 = (u16)(acpi_gbl_FADT.gpe1_block_length / 2);
133
134                 /* Check for GPE0/GPE1 overlap (if both banks exist) */
135
136                 if ((register_count0) &&
137                     (gpe_number_max >= acpi_gbl_FADT.gpe1_base)) {
138                         ACPI_ERROR((AE_INFO,
139                                     "GPE0 block (GPE 0 to %u) overlaps the GPE1 block "
140                                     "(GPE %u to %u) - Ignoring GPE1",
141                                     gpe_number_max, acpi_gbl_FADT.gpe1_base,
142                                     acpi_gbl_FADT.gpe1_base +
143                                     ((register_count1 *
144                                       ACPI_GPE_REGISTER_WIDTH) - 1)));
145
146                         /* Ignore GPE1 block by setting the register count to zero */
147
148                         register_count1 = 0;
149                 } else {
150                         /* Install GPE Block 1 */
151
152                         status =
153                             acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
154                                                      &acpi_gbl_FADT.xgpe1_block,
155                                                      register_count1,
156                                                      acpi_gbl_FADT.gpe1_base,
157                                                      acpi_gbl_FADT.
158                                                      sci_interrupt,
159                                                      &acpi_gbl_gpe_fadt_blocks
160                                                      [1]);
161
162                         if (ACPI_FAILURE(status)) {
163                                 ACPI_EXCEPTION((AE_INFO, status,
164                                                 "Could not create GPE Block 1"));
165                         }
166
167                         /*
168                          * GPE0 and GPE1 do not have to be contiguous in the GPE number
169                          * space. However, GPE0 always starts at GPE number zero.
170                          */
171                         gpe_number_max = acpi_gbl_FADT.gpe1_base +
172                             ((register_count1 * ACPI_GPE_REGISTER_WIDTH) - 1);
173                 }
174         }
175
176         /* Exit if there are no GPE registers */
177
178         if ((register_count0 + register_count1) == 0) {
179
180                 /* GPEs are not required by ACPI, this is OK */
181
182                 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
183                                   "There are no GPE blocks defined in the FADT\n"));
184                 status = AE_OK;
185                 goto cleanup;
186         }
187
188         /* Check for Max GPE number out-of-range */
189
190         if (gpe_number_max > ACPI_GPE_MAX) {
191                 ACPI_ERROR((AE_INFO,
192                             "Maximum GPE number from FADT is too large: 0x%X",
193                             gpe_number_max));
194                 status = AE_BAD_VALUE;
195                 goto cleanup;
196         }
197
198       cleanup:
199         (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
200         return_ACPI_STATUS(AE_OK);
201 }
202
203 /*******************************************************************************
204  *
205  * FUNCTION:    acpi_ev_update_gpes
206  *
207  * PARAMETERS:  table_owner_id      - ID of the newly-loaded ACPI table
208  *
209  * RETURN:      None
210  *
211  * DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a
212  *              result of a Load() or load_table() operation. If new GPE
213  *              methods have been installed, register the new methods and
214  *              enable and runtime GPEs that are associated with them.
215  *
216  ******************************************************************************/
217
218 void acpi_ev_update_gpes(acpi_owner_id table_owner_id)
219 {
220         struct acpi_gpe_xrupt_info *gpe_xrupt_info;
221         struct acpi_gpe_block_info *gpe_block;
222         struct acpi_gpe_walk_info walk_info;
223         acpi_status status = AE_OK;
224
225         /*
226          * 2) Find any _Lxx/_Exx GPE methods that have just been loaded.
227          *
228          * Any GPEs that correspond to new _Lxx/_Exx methods are immediately
229          * enabled.
230          *
231          * Examine the namespace underneath each gpe_device within the
232          * gpe_block lists.
233          */
234         status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
235         if (ACPI_FAILURE(status)) {
236                 return;
237         }
238
239         walk_info.owner_id = table_owner_id;
240         walk_info.execute_by_owner_id = TRUE;
241         walk_info.count = 0;
242         walk_info.enable_this_gpe = TRUE;
243
244         /* Walk the interrupt level descriptor list */
245
246         gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
247         while (gpe_xrupt_info) {
248
249                 /* Walk all Gpe Blocks attached to this interrupt level */
250
251                 gpe_block = gpe_xrupt_info->gpe_block_list_head;
252                 while (gpe_block) {
253                         walk_info.gpe_block = gpe_block;
254                         walk_info.gpe_device = gpe_block->node;
255
256                         status = acpi_ns_walk_namespace(ACPI_TYPE_METHOD,
257                                                         walk_info.gpe_device,
258                                                         ACPI_UINT32_MAX,
259                                                         ACPI_NS_WALK_NO_UNLOCK,
260                                                         acpi_ev_match_gpe_method,
261                                                         NULL, &walk_info, NULL);
262                         if (ACPI_FAILURE(status)) {
263                                 ACPI_EXCEPTION((AE_INFO, status,
264                                                 "While decoding _Lxx/_Exx methods"));
265                         }
266
267                         gpe_block = gpe_block->next;
268                 }
269
270                 gpe_xrupt_info = gpe_xrupt_info->next;
271         }
272
273         if (walk_info.count) {
274                 ACPI_INFO((AE_INFO, "Enabled %u new GPEs", walk_info.count));
275         }
276
277         (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
278         return;
279 }
280
281 /*******************************************************************************
282  *
283  * FUNCTION:    acpi_ev_match_gpe_method
284  *
285  * PARAMETERS:  Callback from walk_namespace
286  *
287  * RETURN:      Status
288  *
289  * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
290  *              control method under the _GPE portion of the namespace.
291  *              Extract the name and GPE type from the object, saving this
292  *              information for quick lookup during GPE dispatch. Allows a
293  *              per-owner_id evaluation if execute_by_owner_id is TRUE in the
294  *              walk_info parameter block.
295  *
296  *              The name of each GPE control method is of the form:
297  *              "_Lxx" or "_Exx", where:
298  *                  L      - means that the GPE is level triggered
299  *                  E      - means that the GPE is edge triggered
300  *                  xx     - is the GPE number [in HEX]
301  *
302  * If walk_info->execute_by_owner_id is TRUE, we only execute examine GPE methods
303  *    with that owner.
304  * If walk_info->enable_this_gpe is TRUE, the GPE that is referred to by a GPE
305  *    method is immediately enabled (Used for Load/load_table operators)
306  *
307  ******************************************************************************/
308
309 acpi_status
310 acpi_ev_match_gpe_method(acpi_handle obj_handle,
311                          u32 level, void *context, void **return_value)
312 {
313         struct acpi_namespace_node *method_node =
314             ACPI_CAST_PTR(struct acpi_namespace_node, obj_handle);
315         struct acpi_gpe_walk_info *walk_info =
316             ACPI_CAST_PTR(struct acpi_gpe_walk_info, context);
317         struct acpi_gpe_event_info *gpe_event_info;
318         struct acpi_namespace_node *gpe_device;
319         acpi_status status;
320         u32 gpe_number;
321         char name[ACPI_NAME_SIZE + 1];
322         u8 type;
323
324         ACPI_FUNCTION_TRACE(ev_match_gpe_method);
325
326         /* Check if requested owner_id matches this owner_id */
327
328         if ((walk_info->execute_by_owner_id) &&
329             (method_node->owner_id != walk_info->owner_id)) {
330                 return_ACPI_STATUS(AE_OK);
331         }
332
333         /*
334          * Match and decode the _Lxx and _Exx GPE method names
335          *
336          * 1) Extract the method name and null terminate it
337          */
338         ACPI_MOVE_32_TO_32(name, &method_node->name.integer);
339         name[ACPI_NAME_SIZE] = 0;
340
341         /* 2) Name must begin with an underscore */
342
343         if (name[0] != '_') {
344                 return_ACPI_STATUS(AE_OK);      /* Ignore this method */
345         }
346
347         /*
348          * 3) Edge/Level determination is based on the 2nd character
349          *    of the method name
350          */
351         switch (name[1]) {
352         case 'L':
353                 type = ACPI_GPE_LEVEL_TRIGGERED;
354                 break;
355
356         case 'E':
357                 type = ACPI_GPE_EDGE_TRIGGERED;
358                 break;
359
360         default:
361                 /* Unknown method type, just ignore it */
362
363                 ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
364                                   "Ignoring unknown GPE method type: %s "
365                                   "(name not of form _Lxx or _Exx)", name));
366                 return_ACPI_STATUS(AE_OK);
367         }
368
369         /* 4) The last two characters of the name are the hex GPE Number */
370
371         gpe_number = ACPI_STRTOUL(&name[2], NULL, 16);
372         if (gpe_number == ACPI_UINT32_MAX) {
373
374                 /* Conversion failed; invalid method, just ignore it */
375
376                 ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
377                                   "Could not extract GPE number from name: %s "
378                                   "(name is not of form _Lxx or _Exx)", name));
379                 return_ACPI_STATUS(AE_OK);
380         }
381
382         /* Ensure that we have a valid GPE number for this GPE block */
383
384         gpe_event_info =
385             acpi_ev_low_get_gpe_info(gpe_number, walk_info->gpe_block);
386         if (!gpe_event_info) {
387                 /*
388                  * This gpe_number is not valid for this GPE block, just ignore it.
389                  * However, it may be valid for a different GPE block, since GPE0
390                  * and GPE1 methods both appear under \_GPE.
391                  */
392                 return_ACPI_STATUS(AE_OK);
393         }
394
395         if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
396             ACPI_GPE_DISPATCH_HANDLER) {
397
398                 /* If there is already a handler, ignore this GPE method */
399
400                 return_ACPI_STATUS(AE_OK);
401         }
402
403         if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
404             ACPI_GPE_DISPATCH_METHOD) {
405                 /*
406                  * If there is already a method, ignore this method. But check
407                  * for a type mismatch (if both the _Lxx AND _Exx exist)
408                  */
409                 if (type != (gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK)) {
410                         ACPI_ERROR((AE_INFO,
411                                     "For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",
412                                     gpe_number, gpe_number, gpe_number));
413                 }
414                 return_ACPI_STATUS(AE_OK);
415         }
416
417         /*
418          * Add the GPE information from above to the gpe_event_info block for
419          * use during dispatch of this GPE.
420          */
421         gpe_event_info->flags |= (u8)(type | ACPI_GPE_DISPATCH_METHOD);
422         gpe_event_info->dispatch.method_node = method_node;
423
424         /*
425          * Enable this GPE if requested. This only happens when during the
426          * execution of a Load or load_table operator. We have found a new
427          * GPE method and want to immediately enable the GPE if it is a
428          * runtime GPE.
429          */
430         if (walk_info->enable_this_gpe) {
431
432                 walk_info->count++;
433                 gpe_device = walk_info->gpe_device;
434
435                 if (gpe_device == acpi_gbl_fadt_gpe_device) {
436                         gpe_device = NULL;
437                 }
438
439                 status = acpi_enable_gpe(gpe_device, gpe_number);
440                 if (ACPI_FAILURE(status)) {
441                         ACPI_EXCEPTION((AE_INFO, status,
442                                         "Could not enable GPE 0x%02X",
443                                         gpe_number));
444                 }
445         }
446
447         ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
448                           "Registered GPE method %s as GPE number 0x%.2X\n",
449                           name, gpe_number));
450         return_ACPI_STATUS(AE_OK);
451 }