blob: 0fd00b5ad650c454776908905cc73484f9bc3383 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: evgpeblk - GPE block creation and initialization.
4 *
5 *****************************************************************************/
6
7/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05008 * Copyright (C) 2000 - 2006, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Len Brown4be44fc2005-08-05 00:44:28 -040049ACPI_MODULE_NAME("evgpeblk")
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Robert Moore44f6c012005-04-18 22:49:35 -040051/* Local prototypes */
Len Brown4be44fc2005-08-05 00:44:28 -040052static acpi_status
53acpi_ev_save_method_info(acpi_handle obj_handle,
54 u32 level, void *obj_desc, void **return_value);
Robert Moore44f6c012005-04-18 22:49:35 -040055
56static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040057acpi_ev_match_prw_and_gpe(acpi_handle obj_handle,
58 u32 level, void *info, void **return_value);
59
60static struct acpi_gpe_xrupt_info *acpi_ev_get_gpe_xrupt_block(u32
61 interrupt_number);
Robert Moore44f6c012005-04-18 22:49:35 -040062
63static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040064acpi_ev_delete_gpe_xrupt(struct acpi_gpe_xrupt_info *gpe_xrupt);
Robert Moore44f6c012005-04-18 22:49:35 -040065
66static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040067acpi_ev_install_gpe_block(struct acpi_gpe_block_info *gpe_block,
68 u32 interrupt_number);
Robert Moore44f6c012005-04-18 22:49:35 -040069
70static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040071acpi_ev_create_gpe_info_blocks(struct acpi_gpe_block_info *gpe_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*******************************************************************************
74 *
75 * FUNCTION: acpi_ev_valid_gpe_event
76 *
77 * PARAMETERS: gpe_event_info - Info for this GPE
78 *
79 * RETURN: TRUE if the gpe_event is valid
80 *
Bob Moore96db2552005-11-02 00:00:00 -050081 * DESCRIPTION: Validate a GPE event. DO NOT CALL FROM INTERRUPT LEVEL.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Should be called only when the GPE lists are semaphore locked
83 * and not subject to change.
84 *
85 ******************************************************************************/
86
Len Brown4be44fc2005-08-05 00:44:28 -040087u8 acpi_ev_valid_gpe_event(struct acpi_gpe_event_info *gpe_event_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Len Brown4be44fc2005-08-05 00:44:28 -040089 struct acpi_gpe_xrupt_info *gpe_xrupt_block;
90 struct acpi_gpe_block_info *gpe_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Len Brown4be44fc2005-08-05 00:44:28 -040092 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* No need for spin lock since we are not changing any list elements */
95
96 /* Walk the GPE interrupt levels */
97
98 gpe_xrupt_block = acpi_gbl_gpe_xrupt_list_head;
99 while (gpe_xrupt_block) {
100 gpe_block = gpe_xrupt_block->gpe_block_list_head;
101
102 /* Walk the GPE blocks on this interrupt level */
103
104 while (gpe_block) {
105 if ((&gpe_block->event_info[0] <= gpe_event_info) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400106 (&gpe_block->
107 event_info[((acpi_size) gpe_block->
108 register_count) * 8] >
109 gpe_event_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return (TRUE);
111 }
112
113 gpe_block = gpe_block->next;
114 }
115
116 gpe_xrupt_block = gpe_xrupt_block->next;
117 }
118
119 return (FALSE);
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*******************************************************************************
123 *
124 * FUNCTION: acpi_ev_walk_gpe_list
125 *
126 * PARAMETERS: gpe_walk_callback - Routine called for each GPE block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
128 * RETURN: Status
129 *
130 * DESCRIPTION: Walk the GPE lists.
131 *
132 ******************************************************************************/
133
Len Brown4be44fc2005-08-05 00:44:28 -0400134acpi_status acpi_ev_walk_gpe_list(ACPI_GPE_CALLBACK gpe_walk_callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Len Brown4be44fc2005-08-05 00:44:28 -0400136 struct acpi_gpe_block_info *gpe_block;
137 struct acpi_gpe_xrupt_info *gpe_xrupt_info;
138 acpi_status status = AE_OK;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500139 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Len Brown4be44fc2005-08-05 00:44:28 -0400141 ACPI_FUNCTION_TRACE("ev_walk_gpe_list");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Len Brown4be44fc2005-08-05 00:44:28 -0400143 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* Walk the interrupt level descriptor list */
146
147 gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
148 while (gpe_xrupt_info) {
149 /* Walk all Gpe Blocks attached to this interrupt level */
150
151 gpe_block = gpe_xrupt_info->gpe_block_list_head;
152 while (gpe_block) {
153 /* One callback per GPE block */
154
Len Brown4be44fc2005-08-05 00:44:28 -0400155 status = gpe_walk_callback(gpe_xrupt_info, gpe_block);
156 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 goto unlock_and_exit;
158 }
159
160 gpe_block = gpe_block->next;
161 }
162
163 gpe_xrupt_info = gpe_xrupt_info->next;
164 }
165
Len Brown4be44fc2005-08-05 00:44:28 -0400166 unlock_and_exit:
167 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
168 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Robert Moore44f6c012005-04-18 22:49:35 -0400171/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 * FUNCTION: acpi_ev_delete_gpe_handlers
174 *
175 * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
176 * gpe_block - Gpe Block info
177 *
178 * RETURN: Status
179 *
180 * DESCRIPTION: Delete all Handler objects found in the GPE data structs.
181 * Used only prior to termination.
182 *
183 ******************************************************************************/
184
185acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400186acpi_ev_delete_gpe_handlers(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
187 struct acpi_gpe_block_info *gpe_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Len Brown4be44fc2005-08-05 00:44:28 -0400189 struct acpi_gpe_event_info *gpe_event_info;
190 acpi_native_uint i;
191 acpi_native_uint j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Len Brown4be44fc2005-08-05 00:44:28 -0400193 ACPI_FUNCTION_TRACE("ev_delete_gpe_handlers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /* Examine each GPE Register within the block */
196
197 for (i = 0; i < gpe_block->register_count; i++) {
198 /* Now look at the individual GPEs in this byte register */
199
200 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 gpe_event_info =
202 &gpe_block->
203 event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Robert Moore44f6c012005-04-18 22:49:35 -0400205 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400206 ACPI_GPE_DISPATCH_HANDLER) {
207 ACPI_MEM_FREE(gpe_event_info->dispatch.handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 gpe_event_info->dispatch.handler = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400209 gpe_event_info->flags &=
210 ~ACPI_GPE_DISPATCH_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 }
213 }
214
Len Brown4be44fc2005-08-05 00:44:28 -0400215 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*******************************************************************************
219 *
220 * FUNCTION: acpi_ev_save_method_info
221 *
222 * PARAMETERS: Callback from walk_namespace
223 *
224 * RETURN: Status
225 *
226 * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
227 * control method under the _GPE portion of the namespace.
228 * Extract the name and GPE type from the object, saving this
229 * information for quick lookup during GPE dispatch
230 *
231 * The name of each GPE control method is of the form:
232 * "_Lxx" or "_Exx"
233 * Where:
234 * L - means that the GPE is level triggered
235 * E - means that the GPE is edge triggered
236 * xx - is the GPE number [in HEX]
237 *
238 ******************************************************************************/
239
240static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400241acpi_ev_save_method_info(acpi_handle obj_handle,
242 u32 level, void *obj_desc, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Len Brown4be44fc2005-08-05 00:44:28 -0400244 struct acpi_gpe_block_info *gpe_block = (void *)obj_desc;
245 struct acpi_gpe_event_info *gpe_event_info;
246 u32 gpe_number;
247 char name[ACPI_NAME_SIZE + 1];
248 u8 type;
249 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Len Brown4be44fc2005-08-05 00:44:28 -0400251 ACPI_FUNCTION_TRACE("ev_save_method_info");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /*
254 * _Lxx and _Exx GPE method support
255 *
256 * 1) Extract the name from the object and convert to a string
257 */
Len Brown4be44fc2005-08-05 00:44:28 -0400258 ACPI_MOVE_32_TO_32(name,
259 &((struct acpi_namespace_node *)obj_handle)->name.
260 integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 name[ACPI_NAME_SIZE] = 0;
262
263 /*
264 * 2) Edge/Level determination is based on the 2nd character
265 * of the method name
266 *
Bob Moore96db2552005-11-02 00:00:00 -0500267 * NOTE: Default GPE type is RUNTIME. May be changed later to WAKE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * if a _PRW object is found that points to this GPE.
269 */
270 switch (name[1]) {
271 case 'L':
272 type = ACPI_GPE_LEVEL_TRIGGERED;
273 break;
274
275 case 'E':
276 type = ACPI_GPE_EDGE_TRIGGERED;
277 break;
278
279 default:
280 /* Unknown method type, just ignore it! */
281
Bob Mooreb8e4d892006-01-27 16:43:00 -0500282 ACPI_ERROR((AE_INFO,
283 "Unknown GPE method type: %s (name not of form _Lxx or _Exx)",
284 name));
Len Brown4be44fc2005-08-05 00:44:28 -0400285 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
288 /* Convert the last two characters of the name to the GPE Number */
289
Len Brown4be44fc2005-08-05 00:44:28 -0400290 gpe_number = ACPI_STRTOUL(&name[2], NULL, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (gpe_number == ACPI_UINT32_MAX) {
292 /* Conversion failed; invalid method, just ignore it */
293
Bob Mooreb8e4d892006-01-27 16:43:00 -0500294 ACPI_ERROR((AE_INFO,
295 "Could not extract GPE number from name: %s (name is not of form _Lxx or _Exx)",
296 name));
Len Brown4be44fc2005-08-05 00:44:28 -0400297 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
300 /* Ensure that we have a valid GPE number for this GPE block */
301
302 if ((gpe_number < gpe_block->block_base_number) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400303 (gpe_number >=
304 (gpe_block->block_base_number +
305 (gpe_block->register_count * 8)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /*
307 * Not valid for this GPE block, just ignore it
308 * However, it may be valid for a different GPE block, since GPE0 and GPE1
309 * methods both appear under \_GPE.
310 */
Len Brown4be44fc2005-08-05 00:44:28 -0400311 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 /*
315 * Now we can add this information to the gpe_event_info block
Bob Moore96db2552005-11-02 00:00:00 -0500316 * for use during dispatch of this GPE. Default type is RUNTIME, although
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 * this may change when the _PRW methods are executed later.
318 */
Len Brown4be44fc2005-08-05 00:44:28 -0400319 gpe_event_info =
320 &gpe_block->event_info[gpe_number - gpe_block->block_base_number];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Bob Moore96db2552005-11-02 00:00:00 -0500322 gpe_event_info->flags = (u8)
323 (type | ACPI_GPE_DISPATCH_METHOD | ACPI_GPE_TYPE_RUNTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Len Brown4be44fc2005-08-05 00:44:28 -0400325 gpe_event_info->dispatch.method_node =
326 (struct acpi_namespace_node *)obj_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Update enable mask, but don't enable the HW GPE as of yet */
329
Len Brown4be44fc2005-08-05 00:44:28 -0400330 status = acpi_ev_enable_gpe(gpe_event_info, FALSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Len Brown4be44fc2005-08-05 00:44:28 -0400332 ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
333 "Registered GPE method %s as GPE number 0x%.2X\n",
334 name, gpe_number));
335 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*******************************************************************************
339 *
340 * FUNCTION: acpi_ev_match_prw_and_gpe
341 *
342 * PARAMETERS: Callback from walk_namespace
343 *
Bob Moore96db2552005-11-02 00:00:00 -0500344 * RETURN: Status. NOTE: We ignore errors so that the _PRW walk is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * not aborted on a single _PRW failure.
346 *
347 * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
Bob Moore96db2552005-11-02 00:00:00 -0500348 * Device. Run the _PRW method. If present, extract the GPE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * number and mark the GPE as a WAKE GPE.
350 *
351 ******************************************************************************/
352
353static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400354acpi_ev_match_prw_and_gpe(acpi_handle obj_handle,
355 u32 level, void *info, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Len Brown4be44fc2005-08-05 00:44:28 -0400357 struct acpi_gpe_walk_info *gpe_info = (void *)info;
358 struct acpi_namespace_node *gpe_device;
359 struct acpi_gpe_block_info *gpe_block;
360 struct acpi_namespace_node *target_gpe_device;
361 struct acpi_gpe_event_info *gpe_event_info;
362 union acpi_operand_object *pkg_desc;
363 union acpi_operand_object *obj_desc;
364 u32 gpe_number;
365 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Len Brown4be44fc2005-08-05 00:44:28 -0400367 ACPI_FUNCTION_TRACE("ev_match_prw_and_gpe");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* Check for a _PRW method under this device */
370
Len Brown4be44fc2005-08-05 00:44:28 -0400371 status = acpi_ut_evaluate_object(obj_handle, METHOD_NAME__PRW,
372 ACPI_BTYPE_PACKAGE, &pkg_desc);
373 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* Ignore all errors from _PRW, we don't want to abort the subsystem */
375
Len Brown4be44fc2005-08-05 00:44:28 -0400376 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
379 /* The returned _PRW package must have at least two elements */
380
381 if (pkg_desc->package.count < 2) {
382 goto cleanup;
383 }
384
385 /* Extract pointers from the input context */
386
387 gpe_device = gpe_info->gpe_device;
388 gpe_block = gpe_info->gpe_block;
389
390 /*
391 * The _PRW object must return a package, we are only interested
392 * in the first element
393 */
394 obj_desc = pkg_desc->package.elements[0];
395
Len Brown4be44fc2005-08-05 00:44:28 -0400396 if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Use FADT-defined GPE device (from definition of _PRW) */
398
399 target_gpe_device = acpi_gbl_fadt_gpe_device;
400
401 /* Integer is the GPE number in the FADT described GPE blocks */
402
403 gpe_number = (u32) obj_desc->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400404 } else if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_PACKAGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* Package contains a GPE reference and GPE number within a GPE block */
406
407 if ((obj_desc->package.count < 2) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400408 (ACPI_GET_OBJECT_TYPE(obj_desc->package.elements[0]) !=
409 ACPI_TYPE_LOCAL_REFERENCE)
410 || (ACPI_GET_OBJECT_TYPE(obj_desc->package.elements[1]) !=
411 ACPI_TYPE_INTEGER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto cleanup;
413 }
414
415 /* Get GPE block reference and decode */
416
Len Brown4be44fc2005-08-05 00:44:28 -0400417 target_gpe_device =
418 obj_desc->package.elements[0]->reference.node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 gpe_number = (u32) obj_desc->package.elements[1]->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400420 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* Unknown type, just ignore it */
422
423 goto cleanup;
424 }
425
426 /*
427 * Is this GPE within this block?
428 *
429 * TRUE iff these conditions are true:
430 * 1) The GPE devices match.
431 * 2) The GPE index(number) is within the range of the Gpe Block
432 * associated with the GPE device.
433 */
434 if ((gpe_device == target_gpe_device) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400435 (gpe_number >= gpe_block->block_base_number) &&
436 (gpe_number <
437 gpe_block->block_base_number + (gpe_block->register_count * 8))) {
438 gpe_event_info =
439 &gpe_block->event_info[gpe_number -
440 gpe_block->block_base_number];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* Mark GPE for WAKE-ONLY but WAKE_DISABLED */
443
Len Brown4be44fc2005-08-05 00:44:28 -0400444 gpe_event_info->flags &=
445 ~(ACPI_GPE_WAKE_ENABLED | ACPI_GPE_RUN_ENABLED);
Bob Moore96db2552005-11-02 00:00:00 -0500446
Len Brown4be44fc2005-08-05 00:44:28 -0400447 status =
448 acpi_ev_set_gpe_type(gpe_event_info, ACPI_GPE_TYPE_WAKE);
449 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto cleanup;
451 }
Len Brown4be44fc2005-08-05 00:44:28 -0400452 status =
453 acpi_ev_update_gpe_enable_masks(gpe_event_info,
454 ACPI_GPE_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
Len Brown4be44fc2005-08-05 00:44:28 -0400457 cleanup:
458 acpi_ut_remove_reference(pkg_desc);
459 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/*******************************************************************************
463 *
464 * FUNCTION: acpi_ev_get_gpe_xrupt_block
465 *
Robert Moore6f42ccf2005-05-13 00:00:00 -0400466 * PARAMETERS: interrupt_number - Interrupt for a GPE block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *
468 * RETURN: A GPE interrupt block
469 *
Bob Moore96db2552005-11-02 00:00:00 -0500470 * DESCRIPTION: Get or Create a GPE interrupt block. There is one interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * block per unique interrupt level used for GPEs.
472 * Should be called only when the GPE lists are semaphore locked
473 * and not subject to change.
474 *
475 ******************************************************************************/
476
Len Brown4be44fc2005-08-05 00:44:28 -0400477static struct acpi_gpe_xrupt_info *acpi_ev_get_gpe_xrupt_block(u32
478 interrupt_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Len Brown4be44fc2005-08-05 00:44:28 -0400480 struct acpi_gpe_xrupt_info *next_gpe_xrupt;
481 struct acpi_gpe_xrupt_info *gpe_xrupt;
482 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500483 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 ACPI_FUNCTION_TRACE("ev_get_gpe_xrupt_block");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Robert Moore44f6c012005-04-18 22:49:35 -0400487 /* No need for lock since we are not changing any list elements here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
490 while (next_gpe_xrupt) {
Robert Moore6f42ccf2005-05-13 00:00:00 -0400491 if (next_gpe_xrupt->interrupt_number == interrupt_number) {
Len Brown4be44fc2005-08-05 00:44:28 -0400492 return_PTR(next_gpe_xrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
495 next_gpe_xrupt = next_gpe_xrupt->next;
496 }
497
498 /* Not found, must allocate a new xrupt descriptor */
499
Len Brown4be44fc2005-08-05 00:44:28 -0400500 gpe_xrupt = ACPI_MEM_CALLOCATE(sizeof(struct acpi_gpe_xrupt_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!gpe_xrupt) {
Len Brown4be44fc2005-08-05 00:44:28 -0400502 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
Robert Moore6f42ccf2005-05-13 00:00:00 -0400505 gpe_xrupt->interrupt_number = interrupt_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /* Install new interrupt descriptor with spin lock */
508
Len Brown4be44fc2005-08-05 00:44:28 -0400509 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (acpi_gbl_gpe_xrupt_list_head) {
511 next_gpe_xrupt = acpi_gbl_gpe_xrupt_list_head;
512 while (next_gpe_xrupt->next) {
513 next_gpe_xrupt = next_gpe_xrupt->next;
514 }
515
516 next_gpe_xrupt->next = gpe_xrupt;
517 gpe_xrupt->previous = next_gpe_xrupt;
Len Brown4be44fc2005-08-05 00:44:28 -0400518 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 acpi_gbl_gpe_xrupt_list_head = gpe_xrupt;
520 }
Len Brown4be44fc2005-08-05 00:44:28 -0400521 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Install new interrupt handler if not SCI_INT */
524
Robert Moore6f42ccf2005-05-13 00:00:00 -0400525 if (interrupt_number != acpi_gbl_FADT->sci_int) {
Len Brown4be44fc2005-08-05 00:44:28 -0400526 status = acpi_os_install_interrupt_handler(interrupt_number,
527 acpi_ev_gpe_xrupt_handler,
528 gpe_xrupt);
529 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500530 ACPI_ERROR((AE_INFO,
531 "Could not install GPE interrupt handler at level 0x%X",
532 interrupt_number));
Len Brown4be44fc2005-08-05 00:44:28 -0400533 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 }
536
Len Brown4be44fc2005-08-05 00:44:28 -0400537 return_PTR(gpe_xrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/*******************************************************************************
541 *
542 * FUNCTION: acpi_ev_delete_gpe_xrupt
543 *
544 * PARAMETERS: gpe_xrupt - A GPE interrupt info block
545 *
546 * RETURN: Status
547 *
548 * DESCRIPTION: Remove and free a gpe_xrupt block. Remove an associated
549 * interrupt handler if not the SCI interrupt.
550 *
551 ******************************************************************************/
552
553static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400554acpi_ev_delete_gpe_xrupt(struct acpi_gpe_xrupt_info *gpe_xrupt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Len Brown4be44fc2005-08-05 00:44:28 -0400556 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500557 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Len Brown4be44fc2005-08-05 00:44:28 -0400559 ACPI_FUNCTION_TRACE("ev_delete_gpe_xrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* We never want to remove the SCI interrupt handler */
562
Robert Moore6f42ccf2005-05-13 00:00:00 -0400563 if (gpe_xrupt->interrupt_number == acpi_gbl_FADT->sci_int) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 gpe_xrupt->gpe_block_list_head = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400565 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 /* Disable this interrupt */
569
Bob Moore96db2552005-11-02 00:00:00 -0500570 status =
571 acpi_os_remove_interrupt_handler(gpe_xrupt->interrupt_number,
572 acpi_ev_gpe_xrupt_handler);
Len Brown4be44fc2005-08-05 00:44:28 -0400573 if (ACPI_FAILURE(status)) {
574 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 /* Unlink the interrupt block with lock */
578
Len Brown4be44fc2005-08-05 00:44:28 -0400579 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (gpe_xrupt->previous) {
581 gpe_xrupt->previous->next = gpe_xrupt->next;
582 }
583
584 if (gpe_xrupt->next) {
585 gpe_xrupt->next->previous = gpe_xrupt->previous;
586 }
Len Brown4be44fc2005-08-05 00:44:28 -0400587 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* Free the block */
590
Len Brown4be44fc2005-08-05 00:44:28 -0400591 ACPI_MEM_FREE(gpe_xrupt);
592 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*******************************************************************************
596 *
597 * FUNCTION: acpi_ev_install_gpe_block
598 *
599 * PARAMETERS: gpe_block - New GPE block
Robert Moore6f42ccf2005-05-13 00:00:00 -0400600 * interrupt_number - Xrupt to be associated with this GPE block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * RETURN: Status
603 *
604 * DESCRIPTION: Install new GPE block with mutex support
605 *
606 ******************************************************************************/
607
608static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400609acpi_ev_install_gpe_block(struct acpi_gpe_block_info *gpe_block,
610 u32 interrupt_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Len Brown4be44fc2005-08-05 00:44:28 -0400612 struct acpi_gpe_block_info *next_gpe_block;
613 struct acpi_gpe_xrupt_info *gpe_xrupt_block;
614 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500615 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Len Brown4be44fc2005-08-05 00:44:28 -0400617 ACPI_FUNCTION_TRACE("ev_install_gpe_block");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Len Brown4be44fc2005-08-05 00:44:28 -0400619 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
620 if (ACPI_FAILURE(status)) {
621 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
Len Brown4be44fc2005-08-05 00:44:28 -0400624 gpe_xrupt_block = acpi_ev_get_gpe_xrupt_block(interrupt_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!gpe_xrupt_block) {
626 status = AE_NO_MEMORY;
627 goto unlock_and_exit;
628 }
629
Robert Moore44f6c012005-04-18 22:49:35 -0400630 /* Install the new block at the end of the list with lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Len Brown4be44fc2005-08-05 00:44:28 -0400632 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (gpe_xrupt_block->gpe_block_list_head) {
634 next_gpe_block = gpe_xrupt_block->gpe_block_list_head;
635 while (next_gpe_block->next) {
636 next_gpe_block = next_gpe_block->next;
637 }
638
639 next_gpe_block->next = gpe_block;
640 gpe_block->previous = next_gpe_block;
Len Brown4be44fc2005-08-05 00:44:28 -0400641 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 gpe_xrupt_block->gpe_block_list_head = gpe_block;
643 }
644
645 gpe_block->xrupt_block = gpe_xrupt_block;
Len Brown4be44fc2005-08-05 00:44:28 -0400646 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Len Brown4be44fc2005-08-05 00:44:28 -0400648 unlock_and_exit:
649 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
650 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*******************************************************************************
654 *
655 * FUNCTION: acpi_ev_delete_gpe_block
656 *
657 * PARAMETERS: gpe_block - Existing GPE block
658 *
659 * RETURN: Status
660 *
661 * DESCRIPTION: Remove a GPE block
662 *
663 ******************************************************************************/
664
Len Brown4be44fc2005-08-05 00:44:28 -0400665acpi_status acpi_ev_delete_gpe_block(struct acpi_gpe_block_info *gpe_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Len Brown4be44fc2005-08-05 00:44:28 -0400667 acpi_status status;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500668 acpi_cpu_flags flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Len Brown4be44fc2005-08-05 00:44:28 -0400670 ACPI_FUNCTION_TRACE("ev_install_gpe_block");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Len Brown4be44fc2005-08-05 00:44:28 -0400672 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
673 if (ACPI_FAILURE(status)) {
674 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 /* Disable all GPEs in this block */
678
Len Brown4be44fc2005-08-05 00:44:28 -0400679 status = acpi_hw_disable_gpe_block(gpe_block->xrupt_block, gpe_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (!gpe_block->previous && !gpe_block->next) {
682 /* This is the last gpe_block on this interrupt */
683
Len Brown4be44fc2005-08-05 00:44:28 -0400684 status = acpi_ev_delete_gpe_xrupt(gpe_block->xrupt_block);
685 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto unlock_and_exit;
687 }
Len Brown4be44fc2005-08-05 00:44:28 -0400688 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Remove the block on this interrupt with lock */
690
Len Brown4be44fc2005-08-05 00:44:28 -0400691 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (gpe_block->previous) {
693 gpe_block->previous->next = gpe_block->next;
Len Brown4be44fc2005-08-05 00:44:28 -0400694 } else {
695 gpe_block->xrupt_block->gpe_block_list_head =
696 gpe_block->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 if (gpe_block->next) {
700 gpe_block->next->previous = gpe_block->previous;
701 }
Len Brown4be44fc2005-08-05 00:44:28 -0400702 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
705 /* Free the gpe_block */
706
Len Brown4be44fc2005-08-05 00:44:28 -0400707 ACPI_MEM_FREE(gpe_block->register_info);
708 ACPI_MEM_FREE(gpe_block->event_info);
709 ACPI_MEM_FREE(gpe_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Len Brown4be44fc2005-08-05 00:44:28 -0400711 unlock_and_exit:
712 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
713 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/*******************************************************************************
717 *
718 * FUNCTION: acpi_ev_create_gpe_info_blocks
719 *
720 * PARAMETERS: gpe_block - New GPE block
721 *
722 * RETURN: Status
723 *
724 * DESCRIPTION: Create the register_info and event_info blocks for this GPE block
725 *
726 ******************************************************************************/
727
728static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400729acpi_ev_create_gpe_info_blocks(struct acpi_gpe_block_info *gpe_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Len Brown4be44fc2005-08-05 00:44:28 -0400731 struct acpi_gpe_register_info *gpe_register_info = NULL;
732 struct acpi_gpe_event_info *gpe_event_info = NULL;
733 struct acpi_gpe_event_info *this_event;
734 struct acpi_gpe_register_info *this_register;
735 acpi_native_uint i;
736 acpi_native_uint j;
737 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Len Brown4be44fc2005-08-05 00:44:28 -0400739 ACPI_FUNCTION_TRACE("ev_create_gpe_info_blocks");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* Allocate the GPE register information block */
742
Len Brown4be44fc2005-08-05 00:44:28 -0400743 gpe_register_info = ACPI_MEM_CALLOCATE((acpi_size) gpe_block->
744 register_count *
745 sizeof(struct
746 acpi_gpe_register_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!gpe_register_info) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500748 ACPI_ERROR((AE_INFO,
749 "Could not allocate the gpe_register_info table"));
Len Brown4be44fc2005-08-05 00:44:28 -0400750 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
753 /*
754 * Allocate the GPE event_info block. There are eight distinct GPEs
Bob Moore96db2552005-11-02 00:00:00 -0500755 * per register. Initialization to zeros is sufficient.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
Len Brown4be44fc2005-08-05 00:44:28 -0400757 gpe_event_info = ACPI_MEM_CALLOCATE(((acpi_size) gpe_block->
758 register_count *
759 ACPI_GPE_REGISTER_WIDTH) *
760 sizeof(struct acpi_gpe_event_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!gpe_event_info) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500762 ACPI_ERROR((AE_INFO,
763 "Could not allocate the gpe_event_info table"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 status = AE_NO_MEMORY;
765 goto error_exit;
766 }
767
768 /* Save the new Info arrays in the GPE block */
769
770 gpe_block->register_info = gpe_register_info;
Len Brown4be44fc2005-08-05 00:44:28 -0400771 gpe_block->event_info = gpe_event_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /*
Bob Moore96db2552005-11-02 00:00:00 -0500774 * Initialize the GPE Register and Event structures. A goal of these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * tables is to hide the fact that there are two separate GPE register sets
Bob Moore96db2552005-11-02 00:00:00 -0500776 * in a given GPE hardware block, the status registers occupy the first half,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * and the enable registers occupy the second half.
778 */
779 this_register = gpe_register_info;
Len Brown4be44fc2005-08-05 00:44:28 -0400780 this_event = gpe_event_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 for (i = 0; i < gpe_block->register_count; i++) {
783 /* Init the register_info for this GPE register (8 GPEs) */
784
Len Brown4be44fc2005-08-05 00:44:28 -0400785 this_register->base_gpe_number =
786 (u8) (gpe_block->block_base_number +
787 (i * ACPI_GPE_REGISTER_WIDTH));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Len Brown4be44fc2005-08-05 00:44:28 -0400789 ACPI_STORE_ADDRESS(this_register->status_address.address,
790 (gpe_block->block_address.address + i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Len Brown4be44fc2005-08-05 00:44:28 -0400792 ACPI_STORE_ADDRESS(this_register->enable_address.address,
793 (gpe_block->block_address.address
794 + i + gpe_block->register_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Len Brown4be44fc2005-08-05 00:44:28 -0400796 this_register->status_address.address_space_id =
797 gpe_block->block_address.address_space_id;
798 this_register->enable_address.address_space_id =
799 gpe_block->block_address.address_space_id;
800 this_register->status_address.register_bit_width =
801 ACPI_GPE_REGISTER_WIDTH;
802 this_register->enable_address.register_bit_width =
803 ACPI_GPE_REGISTER_WIDTH;
804 this_register->status_address.register_bit_offset =
805 ACPI_GPE_REGISTER_WIDTH;
806 this_register->enable_address.register_bit_offset =
807 ACPI_GPE_REGISTER_WIDTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
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->register_bit = acpi_gbl_decode_to8bit[j];
813 this_event->register_info = this_register;
814 this_event++;
815 }
816
Bob Moore96db2552005-11-02 00:00:00 -0500817 /* Disable all GPEs within this register */
818
Len Brown4be44fc2005-08-05 00:44:28 -0400819 status = acpi_hw_low_level_write(ACPI_GPE_REGISTER_WIDTH, 0x00,
820 &this_register->
821 enable_address);
822 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto error_exit;
824 }
825
Bob Moore96db2552005-11-02 00:00:00 -0500826 /* Clear any pending GPE events within this register */
827
Len Brown4be44fc2005-08-05 00:44:28 -0400828 status = acpi_hw_low_level_write(ACPI_GPE_REGISTER_WIDTH, 0xFF,
829 &this_register->
830 status_address);
831 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 goto error_exit;
833 }
834
835 this_register++;
836 }
837
Len Brown4be44fc2005-08-05 00:44:28 -0400838 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Len Brown4be44fc2005-08-05 00:44:28 -0400840 error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (gpe_register_info) {
Len Brown4be44fc2005-08-05 00:44:28 -0400842 ACPI_MEM_FREE(gpe_register_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 if (gpe_event_info) {
Len Brown4be44fc2005-08-05 00:44:28 -0400845 ACPI_MEM_FREE(gpe_event_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
Len Brown4be44fc2005-08-05 00:44:28 -0400848 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/*******************************************************************************
852 *
853 * FUNCTION: acpi_ev_create_gpe_block
854 *
855 * PARAMETERS: gpe_device - Handle to the parent GPE block
856 * gpe_block_address - Address and space_iD
857 * register_count - Number of GPE register pairs in the block
858 * gpe_block_base_number - Starting GPE number for the block
Robert Moore6f42ccf2005-05-13 00:00:00 -0400859 * interrupt_number - H/W interrupt for the block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * return_gpe_block - Where the new block descriptor is returned
861 *
862 * RETURN: Status
863 *
Bob Moore96db2552005-11-02 00:00:00 -0500864 * DESCRIPTION: Create and Install a block of GPE registers. All GPEs within
865 * the block are disabled at exit.
866 * Note: Assumes namespace is locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 *
868 ******************************************************************************/
869
870acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400871acpi_ev_create_gpe_block(struct acpi_namespace_node *gpe_device,
872 struct acpi_generic_address *gpe_block_address,
873 u32 register_count,
874 u8 gpe_block_base_number,
875 u32 interrupt_number,
876 struct acpi_gpe_block_info **return_gpe_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Len Brown4be44fc2005-08-05 00:44:28 -0400878 acpi_status status;
Bob Moore96db2552005-11-02 00:00:00 -0500879 struct acpi_gpe_block_info *gpe_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Len Brown4be44fc2005-08-05 00:44:28 -0400881 ACPI_FUNCTION_TRACE("ev_create_gpe_block");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 if (!register_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400884 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886
887 /* Allocate a new GPE block */
888
Len Brown4be44fc2005-08-05 00:44:28 -0400889 gpe_block = ACPI_MEM_CALLOCATE(sizeof(struct acpi_gpe_block_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (!gpe_block) {
Len Brown4be44fc2005-08-05 00:44:28 -0400891 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
894 /* Initialize the new GPE block */
895
Bob Moore96db2552005-11-02 00:00:00 -0500896 gpe_block->node = gpe_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 gpe_block->register_count = register_count;
898 gpe_block->block_base_number = gpe_block_base_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Len Brown4be44fc2005-08-05 00:44:28 -0400900 ACPI_MEMCPY(&gpe_block->block_address, gpe_block_address,
901 sizeof(struct acpi_generic_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Bob Moore96db2552005-11-02 00:00:00 -0500903 /*
904 * Create the register_info and event_info sub-structures
905 * Note: disables and clears all GPEs in the block
906 */
Len Brown4be44fc2005-08-05 00:44:28 -0400907 status = acpi_ev_create_gpe_info_blocks(gpe_block);
908 if (ACPI_FAILURE(status)) {
909 ACPI_MEM_FREE(gpe_block);
910 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
Bob Moore96db2552005-11-02 00:00:00 -0500913 /* Install the new block in the global lists */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Len Brown4be44fc2005-08-05 00:44:28 -0400915 status = acpi_ev_install_gpe_block(gpe_block, interrupt_number);
916 if (ACPI_FAILURE(status)) {
917 ACPI_MEM_FREE(gpe_block);
918 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
921 /* Find all GPE methods (_Lxx, _Exx) for this block */
922
Len Brown4be44fc2005-08-05 00:44:28 -0400923 status = acpi_ns_walk_namespace(ACPI_TYPE_METHOD, gpe_device,
924 ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK,
925 acpi_ev_save_method_info, gpe_block,
926 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Bob Moore96db2552005-11-02 00:00:00 -0500928 /* Return the new block */
929
930 if (return_gpe_block) {
931 (*return_gpe_block) = gpe_block;
932 }
933
934 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
935 "GPE %02X to %02X [%4.4s] %u regs on int 0x%X\n",
936 (u32) gpe_block->block_base_number,
937 (u32) (gpe_block->block_base_number +
938 ((gpe_block->register_count *
939 ACPI_GPE_REGISTER_WIDTH) - 1)),
940 gpe_device->name.ascii, gpe_block->register_count,
941 interrupt_number));
942
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
962acpi_status
963acpi_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 acpi_native_uint i;
972 acpi_native_uint j;
973
974 ACPI_FUNCTION_TRACE("ev_initialize_gpe_block");
975
976 /* Ignore a null GPE block (e.g., if no GPE block 1 exists) */
977
978 if (!gpe_block) {
979 return_ACPI_STATUS(AE_OK);
980 }
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
Bob Moore96db2552005-11-02 00:00:00 -0500983 * Runtime option: Should wake GPEs be enabled at runtime? The default
984 * is no, they should only be enabled just as the machine goes to sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
986 if (acpi_gbl_leave_wake_gpes_disabled) {
987 /*
Bob Moore96db2552005-11-02 00:00:00 -0500988 * Differentiate runtime vs wake GPEs, via the _PRW control methods.
989 * Each GPE that has one or more _PRWs that reference it is by
990 * definition a wake GPE and will not be enabled while the machine
991 * is running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
993 gpe_info.gpe_block = gpe_block;
994 gpe_info.gpe_device = gpe_device;
995
Len Brown4be44fc2005-08-05 00:44:28 -0400996 status =
997 acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
998 ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
999 acpi_ev_match_prw_and_gpe, &gpe_info,
1000 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002
1003 /*
Bob Moore96db2552005-11-02 00:00:00 -05001004 * Enable all GPEs in this block that have these attributes:
1005 * 1) are "runtime" or "run/wake" GPEs, and
1006 * 2) have a corresponding _Lxx or _Exx method
1007 *
1008 * Any other GPEs within this block must be enabled via the acpi_enable_gpe()
1009 * external interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
1011 wake_gpe_count = 0;
1012 gpe_enabled_count = 0;
1013
1014 for (i = 0; i < gpe_block->register_count; i++) {
1015 for (j = 0; j < 8; j++) {
1016 /* Get the info block for this particular GPE */
1017
Len Brown4be44fc2005-08-05 00:44:28 -04001018 gpe_event_info =
1019 &gpe_block->
1020 event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Len Brown4be44fc2005-08-05 00:44:28 -04001022 if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
1023 ACPI_GPE_DISPATCH_METHOD)
1024 && (gpe_event_info->
1025 flags & ACPI_GPE_TYPE_RUNTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 gpe_enabled_count++;
1027 }
1028
1029 if (gpe_event_info->flags & ACPI_GPE_TYPE_WAKE) {
1030 wake_gpe_count++;
1031 }
1032 }
1033 }
1034
Len Brown4be44fc2005-08-05 00:44:28 -04001035 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
1036 "Found %u Wake, Enabled %u Runtime GPEs in this block\n",
1037 wake_gpe_count, gpe_enabled_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Bob Moore96db2552005-11-02 00:00:00 -05001039 /* Enable all valid runtime GPEs found above */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Bob Moore96db2552005-11-02 00:00:00 -05001041 status = acpi_hw_enable_runtime_gpe_block(NULL, gpe_block);
1042 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -05001043 ACPI_ERROR((AE_INFO, "Could not enable GPEs in gpe_block %p",
1044 gpe_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
Bob Moore96db2552005-11-02 00:00:00 -05001047 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/*******************************************************************************
1051 *
1052 * FUNCTION: acpi_ev_gpe_initialize
1053 *
1054 * PARAMETERS: None
1055 *
1056 * RETURN: Status
1057 *
1058 * DESCRIPTION: Initialize the GPE data structures
1059 *
1060 ******************************************************************************/
1061
Len Brown4be44fc2005-08-05 00:44:28 -04001062acpi_status acpi_ev_gpe_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Len Brown4be44fc2005-08-05 00:44:28 -04001064 u32 register_count0 = 0;
1065 u32 register_count1 = 0;
1066 u32 gpe_number_max = 0;
1067 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Len Brown4be44fc2005-08-05 00:44:28 -04001069 ACPI_FUNCTION_TRACE("ev_gpe_initialize");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Len Brown4be44fc2005-08-05 00:44:28 -04001071 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
1072 if (ACPI_FAILURE(status)) {
1073 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
1076 /*
1077 * Initialize the GPE Block(s) defined in the FADT
1078 *
1079 * Why the GPE register block lengths are divided by 2: From the ACPI Spec,
1080 * section "General-Purpose Event Registers", we have:
1081 *
1082 * "Each register block contains two registers of equal length
1083 * GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
1084 * GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
1085 * The length of the GPE1_STS and GPE1_EN registers is equal to
1086 * half the GPE1_LEN. If a generic register block is not supported
1087 * then its respective block pointer and block length values in the
1088 * FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
1089 * to be the same size."
1090 */
1091
1092 /*
1093 * Determine the maximum GPE number for this machine.
1094 *
1095 * Note: both GPE0 and GPE1 are optional, and either can exist without
1096 * the other.
1097 *
1098 * If EITHER the register length OR the block address are zero, then that
1099 * particular block is not supported.
1100 */
Len Brown4be44fc2005-08-05 00:44:28 -04001101 if (acpi_gbl_FADT->gpe0_blk_len && acpi_gbl_FADT->xgpe0_blk.address) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* GPE block 0 exists (has both length and address > 0) */
1103
1104 register_count0 = (u16) (acpi_gbl_FADT->gpe0_blk_len / 2);
1105
Len Brown4be44fc2005-08-05 00:44:28 -04001106 gpe_number_max =
1107 (register_count0 * ACPI_GPE_REGISTER_WIDTH) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* Install GPE Block 0 */
1110
Len Brown4be44fc2005-08-05 00:44:28 -04001111 status = acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
1112 &acpi_gbl_FADT->xgpe0_blk,
1113 register_count0, 0,
1114 acpi_gbl_FADT->sci_int,
1115 &acpi_gbl_gpe_fadt_blocks[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Len Brown4be44fc2005-08-05 00:44:28 -04001117 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -05001118 ACPI_EXCEPTION((AE_INFO, status,
1119 "Could not create GPE Block 0"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 }
1122
Len Brown4be44fc2005-08-05 00:44:28 -04001123 if (acpi_gbl_FADT->gpe1_blk_len && acpi_gbl_FADT->xgpe1_blk.address) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /* GPE block 1 exists (has both length and address > 0) */
1125
1126 register_count1 = (u16) (acpi_gbl_FADT->gpe1_blk_len / 2);
1127
1128 /* Check for GPE0/GPE1 overlap (if both banks exist) */
1129
1130 if ((register_count0) &&
Len Brown4be44fc2005-08-05 00:44:28 -04001131 (gpe_number_max >= acpi_gbl_FADT->gpe1_base)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -05001132 ACPI_ERROR((AE_INFO,
1133 "GPE0 block (GPE 0 to %d) overlaps the GPE1 block (GPE %d to %d) - Ignoring GPE1",
1134 gpe_number_max, acpi_gbl_FADT->gpe1_base,
1135 acpi_gbl_FADT->gpe1_base +
1136 ((register_count1 *
1137 ACPI_GPE_REGISTER_WIDTH) - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* Ignore GPE1 block by setting the register count to zero */
1140
1141 register_count1 = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001142 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /* Install GPE Block 1 */
1144
Len Brown4be44fc2005-08-05 00:44:28 -04001145 status =
1146 acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
1147 &acpi_gbl_FADT->xgpe1_blk,
1148 register_count1,
1149 acpi_gbl_FADT->gpe1_base,
1150 acpi_gbl_FADT->sci_int,
1151 &acpi_gbl_gpe_fadt_blocks
1152 [1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Len Brown4be44fc2005-08-05 00:44:28 -04001154 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -05001155 ACPI_EXCEPTION((AE_INFO, status,
1156 "Could not create GPE Block 1"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
1159 /*
1160 * GPE0 and GPE1 do not have to be contiguous in the GPE number
1161 * space. However, GPE0 always starts at GPE number zero.
1162 */
1163 gpe_number_max = acpi_gbl_FADT->gpe1_base +
Len Brown4be44fc2005-08-05 00:44:28 -04001164 ((register_count1 * ACPI_GPE_REGISTER_WIDTH) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 }
1167
1168 /* Exit if there are no GPE registers */
1169
1170 if ((register_count0 + register_count1) == 0) {
1171 /* GPEs are not required by ACPI, this is OK */
1172
Len Brown4be44fc2005-08-05 00:44:28 -04001173 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
1174 "There are no GPE blocks defined in the FADT\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 status = AE_OK;
1176 goto cleanup;
1177 }
1178
1179 /* Check for Max GPE number out-of-range */
1180
1181 if (gpe_number_max > ACPI_GPE_MAX) {
Bob Mooreb8e4d892006-01-27 16:43:00 -05001182 ACPI_ERROR((AE_INFO,
1183 "Maximum GPE number from FADT is too large: 0x%X",
1184 gpe_number_max));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 status = AE_BAD_VALUE;
1186 goto cleanup;
1187 }
1188
Len Brown4be44fc2005-08-05 00:44:28 -04001189 cleanup:
1190 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
1191 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}