ACPICA: Clarify ACPI_FREE_BUFFER usage.
[deliverable/linux.git] / drivers / acpi / acpica / evgpeblk.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
3 * Module Name: evgpeblk - GPE block creation and initialization.
4 *
5 *****************************************************************************/
6
7/*
25f044e6 8 * Copyright (C) 2000 - 2013, Intel Corp.
1da177e4
LT
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>
e2f7a777
LB
45#include "accommon.h"
46#include "acevents.h"
47#include "acnamesp.h"
1da177e4
LT
48
49#define _COMPONENT ACPI_EVENTS
4be44fcd 50ACPI_MODULE_NAME("evgpeblk")
33620c54 51#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
44f6c012 52/* Local prototypes */
44f6c012 53static acpi_status
4be44fcd
LB
54acpi_ev_install_gpe_block(struct acpi_gpe_block_info *gpe_block,
55 u32 interrupt_number);
44f6c012
RM
56
57static acpi_status
4be44fcd 58acpi_ev_create_gpe_info_blocks(struct acpi_gpe_block_info *gpe_block);
1da177e4 59
1da177e4
LT
60/*******************************************************************************
61 *
62 * FUNCTION: acpi_ev_install_gpe_block
63 *
9f15fc66
BM
64 * PARAMETERS: gpe_block - New GPE block
65 * interrupt_number - Xrupt to be associated with this
66 * GPE block
1da177e4
LT
67 *
68 * RETURN: Status
69 *
70 * DESCRIPTION: Install new GPE block with mutex support
71 *
72 ******************************************************************************/
73
74static acpi_status
4be44fcd
LB
75acpi_ev_install_gpe_block(struct acpi_gpe_block_info *gpe_block,
76 u32 interrupt_number)
1da177e4 77{
4be44fcd
LB
78 struct acpi_gpe_block_info *next_gpe_block;
79 struct acpi_gpe_xrupt_info *gpe_xrupt_block;
80 acpi_status status;
b8e4d893 81 acpi_cpu_flags flags;
1da177e4 82
b229cf92 83 ACPI_FUNCTION_TRACE(ev_install_gpe_block);
1da177e4 84
4be44fcd
LB
85 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
86 if (ACPI_FAILURE(status)) {
87 return_ACPI_STATUS(status);
1da177e4
LT
88 }
89
4be44fcd 90 gpe_xrupt_block = acpi_ev_get_gpe_xrupt_block(interrupt_number);
1da177e4
LT
91 if (!gpe_xrupt_block) {
92 status = AE_NO_MEMORY;
93 goto unlock_and_exit;
94 }
95
44f6c012 96 /* Install the new block at the end of the list with lock */
1da177e4 97
4be44fcd 98 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
1da177e4
LT
99 if (gpe_xrupt_block->gpe_block_list_head) {
100 next_gpe_block = gpe_xrupt_block->gpe_block_list_head;
101 while (next_gpe_block->next) {
102 next_gpe_block = next_gpe_block->next;
103 }
104
105 next_gpe_block->next = gpe_block;
106 gpe_block->previous = next_gpe_block;
4be44fcd 107 } else {
1da177e4
LT
108 gpe_xrupt_block->gpe_block_list_head = gpe_block;
109 }
110
111 gpe_block->xrupt_block = gpe_xrupt_block;
4be44fcd 112 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
1da177e4 113
4be44fcd
LB
114 unlock_and_exit:
115 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
116 return_ACPI_STATUS(status);
1da177e4
LT
117}
118
1da177e4
LT
119/*******************************************************************************
120 *
121 * FUNCTION: acpi_ev_delete_gpe_block
122 *
9f15fc66 123 * PARAMETERS: gpe_block - Existing GPE block
1da177e4
LT
124 *
125 * RETURN: Status
126 *
127 * DESCRIPTION: Remove a GPE block
128 *
129 ******************************************************************************/
130
4be44fcd 131acpi_status acpi_ev_delete_gpe_block(struct acpi_gpe_block_info *gpe_block)
1da177e4 132{
4be44fcd 133 acpi_status status;
b8e4d893 134 acpi_cpu_flags flags;
1da177e4 135
b229cf92 136 ACPI_FUNCTION_TRACE(ev_install_gpe_block);
1da177e4 137
4be44fcd
LB
138 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
139 if (ACPI_FAILURE(status)) {
140 return_ACPI_STATUS(status);
1da177e4
LT
141 }
142
143 /* Disable all GPEs in this block */
144
e97d6bf1
BM
145 status =
146 acpi_hw_disable_gpe_block(gpe_block->xrupt_block, gpe_block, NULL);
1da177e4
LT
147
148 if (!gpe_block->previous && !gpe_block->next) {
52fc0b02 149
1da177e4
LT
150 /* This is the last gpe_block on this interrupt */
151
4be44fcd
LB
152 status = acpi_ev_delete_gpe_xrupt(gpe_block->xrupt_block);
153 if (ACPI_FAILURE(status)) {
1da177e4
LT
154 goto unlock_and_exit;
155 }
4be44fcd 156 } else {
1da177e4
LT
157 /* Remove the block on this interrupt with lock */
158
4be44fcd 159 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
1da177e4
LT
160 if (gpe_block->previous) {
161 gpe_block->previous->next = gpe_block->next;
4be44fcd
LB
162 } else {
163 gpe_block->xrupt_block->gpe_block_list_head =
164 gpe_block->next;
1da177e4
LT
165 }
166
167 if (gpe_block->next) {
168 gpe_block->next->previous = gpe_block->previous;
169 }
4be44fcd 170 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
1da177e4
LT
171 }
172
0f849d2c 173 acpi_current_gpe_count -= gpe_block->gpe_count;
e97d6bf1 174
1da177e4
LT
175 /* Free the gpe_block */
176
8313524a
BM
177 ACPI_FREE(gpe_block->register_info);
178 ACPI_FREE(gpe_block->event_info);
179 ACPI_FREE(gpe_block);
1da177e4 180
4be44fcd
LB
181 unlock_and_exit:
182 status = acpi_ut_release_mutex(ACPI_MTX_EVENTS);
183 return_ACPI_STATUS(status);
1da177e4
LT
184}
185
1da177e4
LT
186/*******************************************************************************
187 *
188 * FUNCTION: acpi_ev_create_gpe_info_blocks
189 *
190 * PARAMETERS: gpe_block - New GPE block
191 *
192 * RETURN: Status
193 *
194 * DESCRIPTION: Create the register_info and event_info blocks for this GPE block
195 *
196 ******************************************************************************/
197
198static acpi_status
4be44fcd 199acpi_ev_create_gpe_info_blocks(struct acpi_gpe_block_info *gpe_block)
1da177e4 200{
4be44fcd
LB
201 struct acpi_gpe_register_info *gpe_register_info = NULL;
202 struct acpi_gpe_event_info *gpe_event_info = NULL;
203 struct acpi_gpe_event_info *this_event;
204 struct acpi_gpe_register_info *this_register;
67a119f9
BM
205 u32 i;
206 u32 j;
4be44fcd 207 acpi_status status;
1da177e4 208
b229cf92 209 ACPI_FUNCTION_TRACE(ev_create_gpe_info_blocks);
1da177e4
LT
210
211 /* Allocate the GPE register information block */
212
8313524a
BM
213 gpe_register_info = ACPI_ALLOCATE_ZEROED((acpi_size) gpe_block->
214 register_count *
215 sizeof(struct
216 acpi_gpe_register_info));
1da177e4 217 if (!gpe_register_info) {
b8e4d893 218 ACPI_ERROR((AE_INFO,
b229cf92 219 "Could not allocate the GpeRegisterInfo table"));
4be44fcd 220 return_ACPI_STATUS(AE_NO_MEMORY);
1da177e4
LT
221 }
222
223 /*
224 * Allocate the GPE event_info block. There are eight distinct GPEs
96db255c 225 * per register. Initialization to zeros is sufficient.
1da177e4 226 */
0f849d2c 227 gpe_event_info = ACPI_ALLOCATE_ZEROED((acpi_size) gpe_block->gpe_count *
8313524a
BM
228 sizeof(struct
229 acpi_gpe_event_info));
1da177e4 230 if (!gpe_event_info) {
b8e4d893 231 ACPI_ERROR((AE_INFO,
b229cf92 232 "Could not allocate the GpeEventInfo table"));
1da177e4
LT
233 status = AE_NO_MEMORY;
234 goto error_exit;
235 }
236
237 /* Save the new Info arrays in the GPE block */
238
239 gpe_block->register_info = gpe_register_info;
4be44fcd 240 gpe_block->event_info = gpe_event_info;
1da177e4
LT
241
242 /*
96db255c 243 * Initialize the GPE Register and Event structures. A goal of these
9f15fc66
BM
244 * tables is to hide the fact that there are two separate GPE register
245 * sets in a given GPE hardware block, the status registers occupy the
246 * first half, and the enable registers occupy the second half.
1da177e4
LT
247 */
248 this_register = gpe_register_info;
4be44fcd 249 this_event = gpe_event_info;
1da177e4
LT
250
251 for (i = 0; i < gpe_block->register_count; i++) {
52fc0b02 252
1da177e4
LT
253 /* Init the register_info for this GPE register (8 GPEs) */
254
4be44fcd
LB
255 this_register->base_gpe_number =
256 (u8) (gpe_block->block_base_number +
257 (i * ACPI_GPE_REGISTER_WIDTH));
258
59fa8505
BM
259 this_register->status_address.address =
260 gpe_block->block_address.address + i;
4be44fcd 261
59fa8505
BM
262 this_register->enable_address.address =
263 gpe_block->block_address.address + i +
264 gpe_block->register_count;
4be44fcd 265
f3d2e786
BM
266 this_register->status_address.space_id =
267 gpe_block->block_address.space_id;
268 this_register->enable_address.space_id =
269 gpe_block->block_address.space_id;
270 this_register->status_address.bit_width =
4be44fcd 271 ACPI_GPE_REGISTER_WIDTH;
f3d2e786 272 this_register->enable_address.bit_width =
4be44fcd 273 ACPI_GPE_REGISTER_WIDTH;
ecfbbc7b
BM
274 this_register->status_address.bit_offset = 0;
275 this_register->enable_address.bit_offset = 0;
1da177e4
LT
276
277 /* Init the event_info for each GPE within this register */
278
279 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
69874165
AS
280 this_event->gpe_number =
281 (u8) (this_register->base_gpe_number + j);
1da177e4
LT
282 this_event->register_info = this_register;
283 this_event++;
284 }
285
96db255c
BM
286 /* Disable all GPEs within this register */
287
c6b5774c 288 status = acpi_hw_write(0x00, &this_register->enable_address);
4be44fcd 289 if (ACPI_FAILURE(status)) {
1da177e4
LT
290 goto error_exit;
291 }
292
96db255c
BM
293 /* Clear any pending GPE events within this register */
294
c6b5774c 295 status = acpi_hw_write(0xFF, &this_register->status_address);
4be44fcd 296 if (ACPI_FAILURE(status)) {
1da177e4
LT
297 goto error_exit;
298 }
299
300 this_register++;
301 }
302
4be44fcd 303 return_ACPI_STATUS(AE_OK);
1da177e4 304
4be44fcd 305 error_exit:
1da177e4 306 if (gpe_register_info) {
8313524a 307 ACPI_FREE(gpe_register_info);
1da177e4
LT
308 }
309 if (gpe_event_info) {
8313524a 310 ACPI_FREE(gpe_event_info);
1da177e4
LT
311 }
312
4be44fcd 313 return_ACPI_STATUS(status);
1da177e4
LT
314}
315
1da177e4
LT
316/*******************************************************************************
317 *
318 * FUNCTION: acpi_ev_create_gpe_block
319 *
320 * PARAMETERS: gpe_device - Handle to the parent GPE block
ba494bee 321 * gpe_block_address - Address and space_ID
1da177e4
LT
322 * register_count - Number of GPE register pairs in the block
323 * gpe_block_base_number - Starting GPE number for the block
6f42ccf2 324 * interrupt_number - H/W interrupt for the block
1da177e4
LT
325 * return_gpe_block - Where the new block descriptor is returned
326 *
327 * RETURN: Status
328 *
96db255c
BM
329 * DESCRIPTION: Create and Install a block of GPE registers. All GPEs within
330 * the block are disabled at exit.
331 * Note: Assumes namespace is locked.
1da177e4
LT
332 *
333 ******************************************************************************/
334
335acpi_status
4be44fcd
LB
336acpi_ev_create_gpe_block(struct acpi_namespace_node *gpe_device,
337 struct acpi_generic_address *gpe_block_address,
338 u32 register_count,
339 u8 gpe_block_base_number,
340 u32 interrupt_number,
341 struct acpi_gpe_block_info **return_gpe_block)
1da177e4 342{
4be44fcd 343 acpi_status status;
96db255c 344 struct acpi_gpe_block_info *gpe_block;
186c307f 345 struct acpi_gpe_walk_info walk_info;
1da177e4 346
b229cf92 347 ACPI_FUNCTION_TRACE(ev_create_gpe_block);
1da177e4
LT
348
349 if (!register_count) {
4be44fcd 350 return_ACPI_STATUS(AE_OK);
1da177e4
LT
351 }
352
353 /* Allocate a new GPE block */
354
8313524a 355 gpe_block = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_gpe_block_info));
1da177e4 356 if (!gpe_block) {
4be44fcd 357 return_ACPI_STATUS(AE_NO_MEMORY);
1da177e4
LT
358 }
359
360 /* Initialize the new GPE block */
361
96db255c 362 gpe_block->node = gpe_device;
0f849d2c 363 gpe_block->gpe_count = (u16)(register_count * ACPI_GPE_REGISTER_WIDTH);
da503373 364 gpe_block->initialized = FALSE;
1da177e4
LT
365 gpe_block->register_count = register_count;
366 gpe_block->block_base_number = gpe_block_base_number;
1da177e4 367
4be44fcd
LB
368 ACPI_MEMCPY(&gpe_block->block_address, gpe_block_address,
369 sizeof(struct acpi_generic_address));
1da177e4 370
96db255c
BM
371 /*
372 * Create the register_info and event_info sub-structures
373 * Note: disables and clears all GPEs in the block
374 */
4be44fcd
LB
375 status = acpi_ev_create_gpe_info_blocks(gpe_block);
376 if (ACPI_FAILURE(status)) {
8313524a 377 ACPI_FREE(gpe_block);
4be44fcd 378 return_ACPI_STATUS(status);
1da177e4
LT
379 }
380
96db255c 381 /* Install the new block in the global lists */
1da177e4 382
4be44fcd
LB
383 status = acpi_ev_install_gpe_block(gpe_block, interrupt_number);
384 if (ACPI_FAILURE(status)) {
b739f106
TN
385 ACPI_FREE(gpe_block->register_info);
386 ACPI_FREE(gpe_block->event_info);
8313524a 387 ACPI_FREE(gpe_block);
4be44fcd 388 return_ACPI_STATUS(status);
1da177e4
LT
389 }
390
3a37898d 391 acpi_gbl_all_gpes_initialized = FALSE;
a2100801 392
186c307f
BM
393 /* Find all GPE methods (_Lxx or_Exx) for this block */
394
395 walk_info.gpe_block = gpe_block;
396 walk_info.gpe_device = gpe_device;
186c307f 397 walk_info.execute_by_owner_id = FALSE;
1da177e4 398
4be44fcd
LB
399 status = acpi_ns_walk_namespace(ACPI_TYPE_METHOD, gpe_device,
400 ACPI_UINT32_MAX, ACPI_NS_WALK_NO_UNLOCK,
0f849d2c 401 acpi_ev_match_gpe_method, NULL,
186c307f 402 &walk_info, NULL);
1da177e4 403
96db255c
BM
404 /* Return the new block */
405
406 if (return_gpe_block) {
407 (*return_gpe_block) = gpe_block;
408 }
409
3e5621a7
BM
410 ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
411 " Initialized GPE %02X to %02X [%4.4s] %u regs on interrupt 0x%X\n",
412 (u32)gpe_block->block_base_number,
413 (u32)(gpe_block->block_base_number +
414 (gpe_block->gpe_count - 1)),
415 gpe_device->name.ascii, gpe_block->register_count,
416 interrupt_number));
96db255c 417
e97d6bf1
BM
418 /* Update global count of currently available GPEs */
419
0f849d2c 420 acpi_current_gpe_count += gpe_block->gpe_count;
96db255c
BM
421 return_ACPI_STATUS(AE_OK);
422}
423
424/*******************************************************************************
425 *
426 * FUNCTION: acpi_ev_initialize_gpe_block
427 *
da503373 428 * PARAMETERS: acpi_gpe_callback
96db255c
BM
429 *
430 * RETURN: Status
431 *
da503373
LM
432 * DESCRIPTION: Initialize and enable a GPE block. Enable GPEs that have
433 * associated methods.
96db255c
BM
434 * Note: Assumes namespace is locked.
435 *
436 ******************************************************************************/
437
438acpi_status
a2100801
RW
439acpi_ev_initialize_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
440 struct acpi_gpe_block_info *gpe_block,
441 void *ignored)
96db255c 442{
0f849d2c 443 acpi_status status;
96db255c 444 struct acpi_gpe_event_info *gpe_event_info;
96db255c 445 u32 gpe_enabled_count;
0f849d2c 446 u32 gpe_index;
67a119f9
BM
447 u32 i;
448 u32 j;
96db255c 449
b229cf92 450 ACPI_FUNCTION_TRACE(ev_initialize_gpe_block);
96db255c 451
a2100801 452 /*
da503373
LM
453 * Ignore a null GPE block (e.g., if no GPE block 1 exists), and
454 * any GPE blocks that have been initialized already.
a2100801
RW
455 */
456 if (!gpe_block || gpe_block->initialized) {
96db255c
BM
457 return_ACPI_STATUS(AE_OK);
458 }
459
1da177e4 460 /*
a2100801 461 * Enable all GPEs that have a corresponding method and have the
da503373
LM
462 * ACPI_GPE_CAN_WAKE flag unset. Any other GPEs within this block
463 * must be enabled via the acpi_enable_gpe() interface.
1da177e4 464 */
1da177e4 465 gpe_enabled_count = 0;
0f849d2c 466
1da177e4 467 for (i = 0; i < gpe_block->register_count; i++) {
9630bdd9 468 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
52fc0b02 469
1da177e4 470 /* Get the info block for this particular GPE */
0f849d2c
LM
471
472 gpe_index = (i * ACPI_GPE_REGISTER_WIDTH) + j;
9630bdd9 473 gpe_event_info = &gpe_block->event_info[gpe_index];
ce43ace0 474
bba63a29
LM
475 /*
476 * Ignore GPEs that have no corresponding _Lxx/_Exx method
477 * and GPEs that are used to wake the system
478 */
479 if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
480 ACPI_GPE_DISPATCH_NONE)
481 || ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK)
482 == ACPI_GPE_DISPATCH_HANDLER)
a2100801 483 || (gpe_event_info->flags & ACPI_GPE_CAN_WAKE)) {
9630bdd9 484 continue;
0f849d2c
LM
485 }
486
3a37898d 487 status = acpi_ev_add_gpe_reference(gpe_event_info);
0f849d2c
LM
488 if (ACPI_FAILURE(status)) {
489 ACPI_EXCEPTION((AE_INFO, status,
a2100801 490 "Could not enable GPE 0x%02X",
1f86e8c1
LZ
491 gpe_index +
492 gpe_block->block_base_number));
0f849d2c
LM
493 continue;
494 }
495
496 gpe_enabled_count++;
1da177e4
LT
497 }
498 }
499
9874647b 500 if (gpe_enabled_count) {
3e5621a7
BM
501 ACPI_INFO((AE_INFO,
502 "Enabled %u GPEs in block %02X to %02X",
503 gpe_enabled_count, (u32)gpe_block->block_base_number,
504 (u32)(gpe_block->block_base_number +
505 (gpe_block->gpe_count - 1))));
186c307f 506 }
1da177e4 507
a2100801
RW
508 gpe_block->initialized = TRUE;
509
9630bdd9 510 return_ACPI_STATUS(AE_OK);
1da177e4 511}
33620c54
BM
512
513#endif /* !ACPI_REDUCED_HARDWARE */
This page took 0.935389 seconds and 5 git commands to generate.