dswstate.c 21 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7
/******************************************************************************
 *
 * Module Name: dswstate - Dispatcher parse tree walk management routines
 *
 *****************************************************************************/

/*
8
 * Copyright (C) 2000 - 2016, Intel Corp.
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
Len Brown's avatar
Len Brown committed
45 46 47 48
#include "accommon.h"
#include "acparser.h"
#include "acdispat.h"
#include "acnamesp.h"
Linus Torvalds's avatar
Linus Torvalds committed
49 50

#define _COMPONENT          ACPI_DISPATCHER
Len Brown's avatar
Len Brown committed
51
ACPI_MODULE_NAME("dswstate")
Linus Torvalds's avatar
Linus Torvalds committed
52

53
  /* Local prototypes */
54 55 56
static acpi_status
acpi_ds_result_stack_push(struct acpi_walk_state *walk_state);
static acpi_status acpi_ds_result_stack_pop(struct acpi_walk_state *walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59 60 61

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_result_pop
 *
62
 * PARAMETERS:  object              - Where to return the popped object
Linus Torvalds's avatar
Linus Torvalds committed
63 64 65 66
 *              walk_state          - Current Walk state
 *
 * RETURN:      Status
 *
67
 * DESCRIPTION: Pop an object off the top of this walk's result stack
Linus Torvalds's avatar
Linus Torvalds committed
68 69 70 71
 *
 ******************************************************************************/

acpi_status
72 73
acpi_ds_result_pop(union acpi_operand_object **object,
		   struct acpi_walk_state *walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
74
{
75
	u32 index;
Len Brown's avatar
Len Brown committed
76
	union acpi_generic_state *state;
77
	acpi_status status;
Linus Torvalds's avatar
Linus Torvalds committed
78

Bob Moore's avatar
Bob Moore committed
79
	ACPI_FUNCTION_NAME(ds_result_pop);
Linus Torvalds's avatar
Linus Torvalds committed
80 81 82

	state = walk_state->results;

83
	/* Incorrect state of result stack */
Linus Torvalds's avatar
Linus Torvalds committed
84

85 86 87
	if (state && !walk_state->result_count) {
		ACPI_ERROR((AE_INFO, "No results on result stack"));
		return (AE_AML_INTERNAL);
Linus Torvalds's avatar
Linus Torvalds committed
88 89
	}

90 91 92 93
	if (!state && walk_state->result_count) {
		ACPI_ERROR((AE_INFO, "No result state for result stack"));
		return (AE_AML_INTERNAL);
	}
Linus Torvalds's avatar
Linus Torvalds committed
94

95
	/* Empty result stack */
Linus Torvalds's avatar
Linus Torvalds committed
96 97

	if (!state) {
98
		ACPI_ERROR((AE_INFO, "Result stack is empty! State=%p",
Bob Moore's avatar
Bob Moore committed
99
			    walk_state));
Linus Torvalds's avatar
Linus Torvalds committed
100 101 102
		return (AE_AML_NO_RETURN_VALUE);
	}

103
	/* Return object of the top element and clean that top element result stack */
Linus Torvalds's avatar
Linus Torvalds committed
104

105
	walk_state->result_count--;
106
	index = (u32)walk_state->result_count % ACPI_RESULTS_FRAME_OBJ_NUM;
Linus Torvalds's avatar
Linus Torvalds committed
107

108
	*object = state->results.obj_desc[index];
Linus Torvalds's avatar
Linus Torvalds committed
109
	if (!*object) {
Bob Moore's avatar
Bob Moore committed
110
		ACPI_ERROR((AE_INFO,
111 112
			    "No result objects on result stack, State=%p",
			    walk_state));
Linus Torvalds's avatar
Linus Torvalds committed
113 114 115
		return (AE_AML_NO_RETURN_VALUE);
	}

116 117 118 119 120 121 122 123 124 125 126
	state->results.obj_desc[index] = NULL;
	if (index == 0) {
		status = acpi_ds_result_stack_pop(walk_state);
		if (ACPI_FAILURE(status)) {
			return (status);
		}
	}

	ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
			  "Obj=%p [%s] Index=%X State=%p Num=%X\n", *object,
			  acpi_ut_get_object_type_name(*object),
127
			  index, walk_state, walk_state->result_count));
Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131 132 133 134 135

	return (AE_OK);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_result_push
 *
136
 * PARAMETERS:  object              - Where to return the popped object
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141 142 143 144 145
 *              walk_state          - Current Walk state
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Push an object onto the current result stack
 *
 ******************************************************************************/

acpi_status
Len Brown's avatar
Len Brown committed
146 147
acpi_ds_result_push(union acpi_operand_object * object,
		    struct acpi_walk_state * walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
148
{
Len Brown's avatar
Len Brown committed
149
	union acpi_generic_state *state;
150
	acpi_status status;
151
	u32 index;
Linus Torvalds's avatar
Linus Torvalds committed
152

Bob Moore's avatar
Bob Moore committed
153
	ACPI_FUNCTION_NAME(ds_result_push);
Linus Torvalds's avatar
Linus Torvalds committed
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	if (walk_state->result_count > walk_state->result_size) {
		ACPI_ERROR((AE_INFO, "Result stack is full"));
		return (AE_AML_INTERNAL);
	} else if (walk_state->result_count == walk_state->result_size) {

		/* Extend the result stack */

		status = acpi_ds_result_stack_push(walk_state);
		if (ACPI_FAILURE(status)) {
			ACPI_ERROR((AE_INFO,
				    "Failed to extend the result stack"));
			return (status);
		}
	}

	if (!(walk_state->result_count < walk_state->result_size)) {
		ACPI_ERROR((AE_INFO, "No free elements in result stack"));
		return (AE_AML_INTERNAL);
	}

Linus Torvalds's avatar
Linus Torvalds committed
175 176
	state = walk_state->results;
	if (!state) {
Bob Moore's avatar
Bob Moore committed
177
		ACPI_ERROR((AE_INFO, "No result stack frame during push"));
Linus Torvalds's avatar
Linus Torvalds committed
178 179 180 181
		return (AE_AML_INTERNAL);
	}

	if (!object) {
Bob Moore's avatar
Bob Moore committed
182
		ACPI_ERROR((AE_INFO,
183
			    "Null Object! Obj=%p State=%p Num=%u",
184
			    object, walk_state, walk_state->result_count));
Linus Torvalds's avatar
Linus Torvalds committed
185 186 187
		return (AE_BAD_PARAMETER);
	}

188 189
	/* Assign the address of object to the top free element of result stack */

190
	index = (u32)walk_state->result_count % ACPI_RESULTS_FRAME_OBJ_NUM;
191 192
	state->results.obj_desc[index] = object;
	walk_state->result_count++;
Linus Torvalds's avatar
Linus Torvalds committed
193

Len Brown's avatar
Len Brown committed
194 195 196 197
	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Obj=%p [%s] State=%p Num=%X Cur=%X\n",
			  object,
			  acpi_ut_get_object_type_name((union
							acpi_operand_object *)
198 199
						       object), walk_state,
			  walk_state->result_count,
Len Brown's avatar
Len Brown committed
200
			  walk_state->current_result));
Linus Torvalds's avatar
Linus Torvalds committed
201 202 203 204 205 206 207 208 209 210 211 212

	return (AE_OK);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_result_stack_push
 *
 * PARAMETERS:  walk_state          - Current Walk state
 *
 * RETURN:      Status
 *
213
 * DESCRIPTION: Push an object onto the walk_state result stack
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216
 *
 ******************************************************************************/

217
static acpi_status acpi_ds_result_stack_push(struct acpi_walk_state *walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
218
{
Len Brown's avatar
Len Brown committed
219
	union acpi_generic_state *state;
Linus Torvalds's avatar
Linus Torvalds committed
220

Bob Moore's avatar
Bob Moore committed
221
	ACPI_FUNCTION_NAME(ds_result_stack_push);
Linus Torvalds's avatar
Linus Torvalds committed
222

223 224
	/* Check for stack overflow */

225
	if (((u32) walk_state->result_size + ACPI_RESULTS_FRAME_OBJ_NUM) >
226
	    ACPI_RESULTS_OBJ_NUM_MAX) {
227
		ACPI_ERROR((AE_INFO, "Result stack overflow: State=%p Num=%u",
228 229 230 231
			    walk_state, walk_state->result_size));
		return (AE_STACK_OVERFLOW);
	}

Len Brown's avatar
Len Brown committed
232
	state = acpi_ut_create_generic_state();
Linus Torvalds's avatar
Linus Torvalds committed
233 234 235 236
	if (!state) {
		return (AE_NO_MEMORY);
	}

Bob Moore's avatar
Bob Moore committed
237
	state->common.descriptor_type = ACPI_DESC_TYPE_STATE_RESULT;
Len Brown's avatar
Len Brown committed
238
	acpi_ut_push_generic_state(&walk_state->results, state);
Linus Torvalds's avatar
Linus Torvalds committed
239

240 241 242 243
	/* Increase the length of the result stack by the length of frame */

	walk_state->result_size += ACPI_RESULTS_FRAME_OBJ_NUM;

Len Brown's avatar
Len Brown committed
244 245
	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Results=%p State=%p\n",
			  state, walk_state));
Linus Torvalds's avatar
Linus Torvalds committed
246 247 248 249 250 251 252 253 254 255 256 257

	return (AE_OK);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_result_stack_pop
 *
 * PARAMETERS:  walk_state          - Current Walk state
 *
 * RETURN:      Status
 *
258
 * DESCRIPTION: Pop an object off of the walk_state result stack
Linus Torvalds's avatar
Linus Torvalds committed
259 260 261
 *
 ******************************************************************************/

262
static acpi_status acpi_ds_result_stack_pop(struct acpi_walk_state *walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
263
{
Len Brown's avatar
Len Brown committed
264
	union acpi_generic_state *state;
Linus Torvalds's avatar
Linus Torvalds committed
265

Bob Moore's avatar
Bob Moore committed
266
	ACPI_FUNCTION_NAME(ds_result_stack_pop);
Linus Torvalds's avatar
Linus Torvalds committed
267 268 269 270

	/* Check for stack underflow */

	if (walk_state->results == NULL) {
271 272
		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
				  "Result stack underflow - State=%p\n",
Len Brown's avatar
Len Brown committed
273
				  walk_state));
Linus Torvalds's avatar
Linus Torvalds committed
274 275 276
		return (AE_AML_NO_OPERAND);
	}

277 278 279 280 281
	if (walk_state->result_size < ACPI_RESULTS_FRAME_OBJ_NUM) {
		ACPI_ERROR((AE_INFO, "Insufficient result stack size"));
		return (AE_AML_INTERNAL);
	}

Len Brown's avatar
Len Brown committed
282
	state = acpi_ut_pop_generic_state(&walk_state->results);
283 284 285 286 287
	acpi_ut_delete_generic_state(state);

	/* Decrease the length of result stack by the length of frame */

	walk_state->result_size -= ACPI_RESULTS_FRAME_OBJ_NUM;
Linus Torvalds's avatar
Linus Torvalds committed
288

Len Brown's avatar
Len Brown committed
289
	ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
Bob Moore's avatar
Bob Moore committed
290
			  "Result=%p RemainingResults=%X State=%p\n",
291
			  state, walk_state->result_count, walk_state));
Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297 298 299

	return (AE_OK);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_obj_stack_push
 *
300
 * PARAMETERS:  object              - Object to push
Linus Torvalds's avatar
Linus Torvalds committed
301 302 303 304 305 306 307 308 309
 *              walk_state          - Current Walk state
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Push an object onto this walk's object/operand stack
 *
 ******************************************************************************/

acpi_status
Len Brown's avatar
Len Brown committed
310
acpi_ds_obj_stack_push(void *object, struct acpi_walk_state * walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
311
{
Bob Moore's avatar
Bob Moore committed
312
	ACPI_FUNCTION_NAME(ds_obj_stack_push);
Linus Torvalds's avatar
Linus Torvalds committed
313 314 315 316

	/* Check for stack overflow */

	if (walk_state->num_operands >= ACPI_OBJ_NUM_OPERANDS) {
Bob Moore's avatar
Bob Moore committed
317
		ACPI_ERROR((AE_INFO,
318
			    "Object stack overflow! Obj=%p State=%p #Ops=%u",
Bob Moore's avatar
Bob Moore committed
319
			    object, walk_state, walk_state->num_operands));
Linus Torvalds's avatar
Linus Torvalds committed
320 321 322 323 324
		return (AE_STACK_OVERFLOW);
	}

	/* Put the object onto the stack */

325
	walk_state->operands[walk_state->operand_index] = object;
Linus Torvalds's avatar
Linus Torvalds committed
326 327
	walk_state->num_operands++;

328 329 330 331
	/* For the usual order of filling the operand stack */

	walk_state->operand_index++;

Len Brown's avatar
Len Brown committed
332 333 334 335 336 337
	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Obj=%p [%s] State=%p #Ops=%X\n",
			  object,
			  acpi_ut_get_object_type_name((union
							acpi_operand_object *)
						       object), walk_state,
			  walk_state->num_operands));
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340 341 342 343 344 345 346 347 348 349 350

	return (AE_OK);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_obj_stack_pop
 *
 * PARAMETERS:  pop_count           - Number of objects/entries to pop
 *              walk_state          - Current Walk state
 *
 * RETURN:      Status
 *
351
 * DESCRIPTION: Pop this walk's object stack. Objects on the stack are NOT
Linus Torvalds's avatar
Linus Torvalds committed
352 353 354 355 356
 *              deleted by this routine.
 *
 ******************************************************************************/

acpi_status
Len Brown's avatar
Len Brown committed
357
acpi_ds_obj_stack_pop(u32 pop_count, struct acpi_walk_state * walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
358
{
Len Brown's avatar
Len Brown committed
359
	u32 i;
Linus Torvalds's avatar
Linus Torvalds committed
360

Bob Moore's avatar
Bob Moore committed
361
	ACPI_FUNCTION_NAME(ds_obj_stack_pop);
Linus Torvalds's avatar
Linus Torvalds committed
362 363

	for (i = 0; i < pop_count; i++) {
Bob Moore's avatar
Bob Moore committed
364

Linus Torvalds's avatar
Linus Torvalds committed
365 366 367
		/* Check for stack underflow */

		if (walk_state->num_operands == 0) {
Bob Moore's avatar
Bob Moore committed
368
			ACPI_ERROR((AE_INFO,
369
				    "Object stack underflow! Count=%X State=%p #Ops=%u",
Bob Moore's avatar
Bob Moore committed
370 371
				    pop_count, walk_state,
				    walk_state->num_operands));
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377
			return (AE_STACK_UNDERFLOW);
		}

		/* Just set the stack entry to null */

		walk_state->num_operands--;
Len Brown's avatar
Len Brown committed
378
		walk_state->operands[walk_state->num_operands] = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
379 380
	}

381
	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Count=%X State=%p #Ops=%u\n",
Linus Torvalds's avatar
Linus Torvalds committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
			  pop_count, walk_state, walk_state->num_operands));

	return (AE_OK);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_obj_stack_pop_and_delete
 *
 * PARAMETERS:  pop_count           - Number of objects/entries to pop
 *              walk_state          - Current Walk state
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Pop this walk's object stack and delete each object that is
 *              popped off.
 *
 ******************************************************************************/

401
void
Len Brown's avatar
Len Brown committed
402
acpi_ds_obj_stack_pop_and_delete(u32 pop_count,
403
				 struct acpi_walk_state *walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
404
{
405
	s32 i;
Len Brown's avatar
Len Brown committed
406
	union acpi_operand_object *obj_desc;
Linus Torvalds's avatar
Linus Torvalds committed
407

Bob Moore's avatar
Bob Moore committed
408
	ACPI_FUNCTION_NAME(ds_obj_stack_pop_and_delete);
Linus Torvalds's avatar
Linus Torvalds committed
409

410 411 412
	if (pop_count == 0) {
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
413

414
	for (i = (s32) pop_count - 1; i >= 0; i--) {
Linus Torvalds's avatar
Linus Torvalds committed
415
		if (walk_state->num_operands == 0) {
416
			return;
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419 420 421
		}

		/* Pop the stack and delete an object if present in this stack entry */

		walk_state->num_operands--;
422
		obj_desc = walk_state->operands[i];
Linus Torvalds's avatar
Linus Torvalds committed
423
		if (obj_desc) {
424 425
			acpi_ut_remove_reference(walk_state->operands[i]);
			walk_state->operands[i] = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
426 427 428
		}
	}

Len Brown's avatar
Len Brown committed
429
	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Count=%X State=%p #Ops=%X\n",
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432 433 434 435 436
			  pop_count, walk_state, walk_state->num_operands));
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_get_current_walk_state
 *
437
 * PARAMETERS:  thread          - Get current active state for this Thread
Linus Torvalds's avatar
Linus Torvalds committed
438 439 440 441 442 443 444 445
 *
 * RETURN:      Pointer to the current walk state
 *
 * DESCRIPTION: Get the walk state that is at the head of the list (the "current"
 *              walk state.)
 *
 ******************************************************************************/

Len Brown's avatar
Len Brown committed
446 447
struct acpi_walk_state *acpi_ds_get_current_walk_state(struct acpi_thread_state
						       *thread)
Linus Torvalds's avatar
Linus Torvalds committed
448
{
Bob Moore's avatar
Bob Moore committed
449
	ACPI_FUNCTION_NAME(ds_get_current_walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
450 451 452 453 454

	if (!thread) {
		return (NULL);
	}

Bob Moore's avatar
Bob Moore committed
455
	ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Current WalkState %p\n",
Len Brown's avatar
Len Brown committed
456
			  thread->walk_state_list));
Linus Torvalds's avatar
Linus Torvalds committed
457 458 459 460 461 462 463 464 465

	return (thread->walk_state_list);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_push_walk_state
 *
 * PARAMETERS:  walk_state      - State to push
466
 *              thread          - Thread state object
Linus Torvalds's avatar
Linus Torvalds committed
467 468 469
 *
 * RETURN:      None
 *
470
 * DESCRIPTION: Place the Thread state at the head of the state list
Linus Torvalds's avatar
Linus Torvalds committed
471 472 473 474
 *
 ******************************************************************************/

void
Len Brown's avatar
Len Brown committed
475 476
acpi_ds_push_walk_state(struct acpi_walk_state *walk_state,
			struct acpi_thread_state *thread)
Linus Torvalds's avatar
Linus Torvalds committed
477
{
Bob Moore's avatar
Bob Moore committed
478
	ACPI_FUNCTION_TRACE(ds_push_walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
479

Len Brown's avatar
Len Brown committed
480
	walk_state->next = thread->walk_state_list;
Linus Torvalds's avatar
Linus Torvalds committed
481 482 483 484 485 486 487 488 489
	thread->walk_state_list = walk_state;

	return_VOID;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_pop_walk_state
 *
490
 * PARAMETERS:  thread      - Current thread state
Linus Torvalds's avatar
Linus Torvalds committed
491
 *
Robert Moore's avatar
Robert Moore committed
492
 * RETURN:      A walk_state object popped from the thread's stack
Linus Torvalds's avatar
Linus Torvalds committed
493 494
 *
 * DESCRIPTION: Remove and return the walkstate object that is at the head of
495
 *              the walk stack for the given walk list. NULL indicates that
Linus Torvalds's avatar
Linus Torvalds committed
496 497 498 499
 *              the list is empty.
 *
 ******************************************************************************/

Len Brown's avatar
Len Brown committed
500
struct acpi_walk_state *acpi_ds_pop_walk_state(struct acpi_thread_state *thread)
Linus Torvalds's avatar
Linus Torvalds committed
501
{
Len Brown's avatar
Len Brown committed
502
	struct acpi_walk_state *walk_state;
Linus Torvalds's avatar
Linus Torvalds committed
503

Bob Moore's avatar
Bob Moore committed
504
	ACPI_FUNCTION_TRACE(ds_pop_walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
505 506 507 508

	walk_state = thread->walk_state_list;

	if (walk_state) {
Bob Moore's avatar
Bob Moore committed
509

Linus Torvalds's avatar
Linus Torvalds committed
510 511 512 513 514 515 516
		/* Next walk state becomes the current walk state */

		thread->walk_state_list = walk_state->next;

		/*
		 * Don't clear the NEXT field, this serves as an indicator
		 * that there is a parent WALK STATE
Robert Moore's avatar
Robert Moore committed
517
		 * Do Not: walk_state->Next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
518 519 520
		 */
	}

Len Brown's avatar
Len Brown committed
521
	return_PTR(walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
522 523 524 525 526 527
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_create_walk_state
 *
Robert Moore's avatar
Robert Moore committed
528
 * PARAMETERS:  owner_id        - ID for object creation
529
 *              origin          - Starting point for this walk
Bob Moore's avatar
Bob Moore committed
530
 *              method_desc     - Method object
531
 *              thread          - Current thread state
Linus Torvalds's avatar
Linus Torvalds committed
532 533 534
 *
 * RETURN:      Pointer to the new walk state.
 *
535
 * DESCRIPTION: Allocate and initialize a new walk state. The current walk
Linus Torvalds's avatar
Linus Torvalds committed
536 537 538 539
 *              state is set to this new state.
 *
 ******************************************************************************/

540 541 542 543 544 545
struct acpi_walk_state *acpi_ds_create_walk_state(acpi_owner_id owner_id,
						  union acpi_parse_object
						  *origin,
						  union acpi_operand_object
						  *method_desc,
						  struct acpi_thread_state
Len Brown's avatar
Len Brown committed
546
						  *thread)
Linus Torvalds's avatar
Linus Torvalds committed
547
{
Len Brown's avatar
Len Brown committed
548
	struct acpi_walk_state *walk_state;
Linus Torvalds's avatar
Linus Torvalds committed
549

Bob Moore's avatar
Bob Moore committed
550
	ACPI_FUNCTION_TRACE(ds_create_walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
551

Bob Moore's avatar
Bob Moore committed
552
	walk_state = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_walk_state));
Linus Torvalds's avatar
Linus Torvalds committed
553
	if (!walk_state) {
Len Brown's avatar
Len Brown committed
554
		return_PTR(NULL);
Linus Torvalds's avatar
Linus Torvalds committed
555 556
	}

Bob Moore's avatar
Bob Moore committed
557 558
	walk_state->descriptor_type = ACPI_DESC_TYPE_WALK;
	walk_state->method_desc = method_desc;
Len Brown's avatar
Len Brown committed
559 560 561
	walk_state->owner_id = owner_id;
	walk_state->origin = origin;
	walk_state->thread = thread;
Linus Torvalds's avatar
Linus Torvalds committed
562 563 564 565 566 567

	walk_state->parser_state.start_op = origin;

	/* Init the method args/local */

#if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
Len Brown's avatar
Len Brown committed
568
	acpi_ds_method_data_init(walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
569 570 571 572 573
#endif

	/* Put the new state at the head of the walk list */

	if (thread) {
Len Brown's avatar
Len Brown committed
574
		acpi_ds_push_walk_state(walk_state, thread);
Linus Torvalds's avatar
Linus Torvalds committed
575 576
	}

Len Brown's avatar
Len Brown committed
577
	return_PTR(walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
578 579 580 581 582 583 584
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_init_aml_walk
 *
 * PARAMETERS:  walk_state      - New state to be initialized
585
 *              op              - Current parse op
Linus Torvalds's avatar
Linus Torvalds committed
586 587 588
 *              method_node     - Control method NS node, if any
 *              aml_start       - Start of AML
 *              aml_length      - Length of AML
589
 *              info            - Method info block (params, etc.)
Linus Torvalds's avatar
Linus Torvalds committed
590 591 592 593 594 595 596 597 598
 *              pass_number     - 1, 2, or 3
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Initialize a walk state for a pass 1 or 2 parse tree walk
 *
 ******************************************************************************/

acpi_status
Len Brown's avatar
Len Brown committed
599 600 601 602 603
acpi_ds_init_aml_walk(struct acpi_walk_state *walk_state,
		      union acpi_parse_object *op,
		      struct acpi_namespace_node *method_node,
		      u8 * aml_start,
		      u32 aml_length,
Bob Moore's avatar
Bob Moore committed
604
		      struct acpi_evaluate_info *info, u8 pass_number)
Linus Torvalds's avatar
Linus Torvalds committed
605
{
Len Brown's avatar
Len Brown committed
606 607 608
	acpi_status status;
	struct acpi_parse_state *parser_state = &walk_state->parser_state;
	union acpi_parse_object *extra_op;
Linus Torvalds's avatar
Linus Torvalds committed
609

Bob Moore's avatar
Bob Moore committed
610
	ACPI_FUNCTION_TRACE(ds_init_aml_walk);
Linus Torvalds's avatar
Linus Torvalds committed
611

Len Brown's avatar
Len Brown committed
612 613
	walk_state->parser_state.aml =
	    walk_state->parser_state.aml_start = aml_start;
Linus Torvalds's avatar
Linus Torvalds committed
614
	walk_state->parser_state.aml_end =
Len Brown's avatar
Len Brown committed
615
	    walk_state->parser_state.pkg_end = aml_start + aml_length;
Linus Torvalds's avatar
Linus Torvalds committed
616 617 618

	/* The next_op of the next_walk will be the beginning of the method */

Robert Moore's avatar
Robert Moore committed
619
	walk_state->next_op = NULL;
620
	walk_state->pass_number = pass_number;
Linus Torvalds's avatar
Linus Torvalds committed
621 622

	if (info) {
623 624
		walk_state->params = info->parameters;
		walk_state->caller_return_desc = &info->return_object;
Linus Torvalds's avatar
Linus Torvalds committed
625 626
	}

Len Brown's avatar
Len Brown committed
627 628 629
	status = acpi_ps_init_scope(&walk_state->parser_state, op);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
630 631 632 633
	}

	if (method_node) {
		walk_state->parser_state.start_node = method_node;
Len Brown's avatar
Len Brown committed
634 635 636 637
		walk_state->walk_type = ACPI_WALK_METHOD;
		walk_state->method_node = method_node;
		walk_state->method_desc =
		    acpi_ns_get_attached_object(method_node);
Linus Torvalds's avatar
Linus Torvalds committed
638 639 640

		/* Push start scope on scope stack and make it current  */

Len Brown's avatar
Len Brown committed
641 642 643 644 645
		status =
		    acpi_ds_scope_stack_push(method_node, ACPI_TYPE_METHOD,
					     walk_state);
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
646 647 648 649
		}

		/* Init the method arguments */

Len Brown's avatar
Len Brown committed
650 651 652 653 654
		status = acpi_ds_method_data_init_args(walk_state->params,
						       ACPI_METHOD_NUM_ARGS,
						       walk_state);
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
655
		}
Len Brown's avatar
Len Brown committed
656
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
657 658 659
		/*
		 * Setup the current scope.
		 * Find a Named Op that has a namespace node associated with it.
660
		 * search upwards from this Op. Current scope is the first
Linus Torvalds's avatar
Linus Torvalds committed
661 662 663 664 665 666 667 668 669
		 * Op with a namespace node.
		 */
		extra_op = parser_state->start_op;
		while (extra_op && !extra_op->common.node) {
			extra_op = extra_op->common.parent;
		}

		if (!extra_op) {
			parser_state->start_node = NULL;
Len Brown's avatar
Len Brown committed
670
		} else {
Linus Torvalds's avatar
Linus Torvalds committed
671 672 673 674
			parser_state->start_node = extra_op->common.node;
		}

		if (parser_state->start_node) {
Bob Moore's avatar
Bob Moore committed
675

Linus Torvalds's avatar
Linus Torvalds committed
676 677
			/* Push start scope on scope stack and make it current  */

Len Brown's avatar
Len Brown committed
678 679 680 681 682 683
			status =
			    acpi_ds_scope_stack_push(parser_state->start_node,
						     parser_state->start_node->
						     type, walk_state);
			if (ACPI_FAILURE(status)) {
				return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
684 685 686 687
			}
		}
	}

Len Brown's avatar
Len Brown committed
688 689
	status = acpi_ds_init_callbacks(walk_state, pass_number);
	return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ds_delete_walk_state
 *
 * PARAMETERS:  walk_state      - State to delete
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Delete a walk state including all internal data structures
 *
 ******************************************************************************/

Len Brown's avatar
Len Brown committed
704
void acpi_ds_delete_walk_state(struct acpi_walk_state *walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
705
{
Len Brown's avatar
Len Brown committed
706
	union acpi_generic_state *state;
Linus Torvalds's avatar
Linus Torvalds committed
707

Bob Moore's avatar
Bob Moore committed
708
	ACPI_FUNCTION_TRACE_PTR(ds_delete_walk_state, walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
709 710

	if (!walk_state) {
711
		return_VOID;
Linus Torvalds's avatar
Linus Torvalds committed
712 713
	}

Bob Moore's avatar
Bob Moore committed
714
	if (walk_state->descriptor_type != ACPI_DESC_TYPE_WALK) {
Bob Moore's avatar
Bob Moore committed
715 716
		ACPI_ERROR((AE_INFO, "%p is not a valid walk state",
			    walk_state));
717
		return_VOID;
Linus Torvalds's avatar
Linus Torvalds committed
718 719
	}

Bob Moore's avatar
Bob Moore committed
720 721
	/* There should not be any open scopes */

Linus Torvalds's avatar
Linus Torvalds committed
722
	if (walk_state->parser_state.scope) {
Bob Moore's avatar
Bob Moore committed
723 724
		ACPI_ERROR((AE_INFO, "%p walk still has a scope list",
			    walk_state));
Bob Moore's avatar
Bob Moore committed
725
		acpi_ps_cleanup_scope(&walk_state->parser_state);
Linus Torvalds's avatar
Linus Torvalds committed
726 727 728 729 730 731 732 733
	}

	/* Always must free any linked control states */

	while (walk_state->control_state) {
		state = walk_state->control_state;
		walk_state->control_state = state->common.next;

Len Brown's avatar
Len Brown committed
734
		acpi_ut_delete_generic_state(state);
Linus Torvalds's avatar
Linus Torvalds committed
735 736 737 738 739 740 741 742
	}

	/* Always must free any linked parse states */

	while (walk_state->scope_info) {
		state = walk_state->scope_info;
		walk_state->scope_info = state->common.next;

Len Brown's avatar
Len Brown committed
743
		acpi_ut_delete_generic_state(state);
Linus Torvalds's avatar
Linus Torvalds committed
744 745 746 747 748 749 750 751
	}

	/* Always must free any stacked result states */

	while (walk_state->results) {
		state = walk_state->results;
		walk_state->results = state->common.next;

Len Brown's avatar
Len Brown committed
752
		acpi_ut_delete_generic_state(state);
Linus Torvalds's avatar
Linus Torvalds committed
753 754
	}

Bob Moore's avatar
Bob Moore committed
755
	ACPI_FREE(walk_state);
Linus Torvalds's avatar
Linus Torvalds committed
756 757
	return_VOID;
}