utmisc.c 11.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*******************************************************************************
 *
 * Module Name: utmisc - common utility procedures
 *
 ******************************************************************************/

/*
8
 * Copyright (C) 2000 - 2015, Intel Corp.
L
Linus Torvalds 已提交
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>
L
Len Brown 已提交
45 46
#include "accommon.h"
#include "acnamesp.h"
L
Linus Torvalds 已提交
47 48

#define _COMPONENT          ACPI_UTILITIES
L
Len Brown 已提交
49
ACPI_MODULE_NAME("utmisc")
R
Robert Moore 已提交
50

51 52 53 54
/*******************************************************************************
 *
 * FUNCTION:    acpi_ut_is_pci_root_bridge
 *
55
 * PARAMETERS:  id              - The HID/CID in string format
56 57 58 59 60 61 62 63 64 65 66 67 68
 *
 * RETURN:      TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
 *
 * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
 *
 ******************************************************************************/
u8 acpi_ut_is_pci_root_bridge(char *id)
{

	/*
	 * Check if this is a PCI root bridge.
	 * ACPI 3.0+: check for a PCI Express root also.
	 */
69 70 71
	if (!(strcmp(id,
		     PCI_ROOT_HID_STRING)) ||
	    !(strcmp(id, PCI_EXPRESS_ROOT_HID_STRING))) {
72 73 74 75 76 77
		return (TRUE);
	}

	return (FALSE);
}

78
#if (defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP || defined ACPI_NAMES_APP)
B
Bob Moore 已提交
79 80 81 82
/*******************************************************************************
 *
 * FUNCTION:    acpi_ut_is_aml_table
 *
83
 * PARAMETERS:  table               - An ACPI table
B
Bob Moore 已提交
84 85 86 87 88 89 90 91
 *
 * RETURN:      TRUE if table contains executable AML; FALSE otherwise
 *
 * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
 *              Currently, these are DSDT,SSDT,PSDT. All other table types are
 *              data tables that do not contain AML code.
 *
 ******************************************************************************/
92

B
Bob Moore 已提交
93 94 95
u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
{

B
Bob Moore 已提交
96
	/* These are the only tables that contain executable AML */
B
Bob Moore 已提交
97

98 99
	if (ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) ||
	    ACPI_COMPARE_NAME(table->signature, ACPI_SIG_PSDT) ||
100 101
	    ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT) ||
	    ACPI_COMPARE_NAME(table->signature, ACPI_SIG_OSDT)) {
B
Bob Moore 已提交
102 103 104 105 106
		return (TRUE);
	}

	return (FALSE);
}
107
#endif
B
Bob Moore 已提交
108

L
Linus Torvalds 已提交
109 110 111 112
/*******************************************************************************
 *
 * FUNCTION:    acpi_ut_dword_byte_swap
 *
113
 * PARAMETERS:  value           - Value to be converted
L
Linus Torvalds 已提交
114
 *
R
Robert Moore 已提交
115 116
 * RETURN:      u32 integer with bytes swapped
 *
L
Linus Torvalds 已提交
117 118 119 120
 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
 *
 ******************************************************************************/

L
Len Brown 已提交
121
u32 acpi_ut_dword_byte_swap(u32 value)
L
Linus Torvalds 已提交
122 123
{
	union {
L
Len Brown 已提交
124 125
		u32 value;
		u8 bytes[4];
L
Linus Torvalds 已提交
126 127
	} out;
	union {
L
Len Brown 已提交
128 129
		u32 value;
		u8 bytes[4];
L
Linus Torvalds 已提交
130 131
	} in;

L
Len Brown 已提交
132
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

	in.value = value;

	out.bytes[0] = in.bytes[3];
	out.bytes[1] = in.bytes[2];
	out.bytes[2] = in.bytes[1];
	out.bytes[3] = in.bytes[0];

	return (out.value);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ut_set_integer_width
 *
 * PARAMETERS:  Revision            From DSDT header
 *
 * RETURN:      None
 *
 * DESCRIPTION: Set the global integer bit width based upon the revision
153 154
 *              of the DSDT. For Revision 1 and 0, Integers are 32 bits.
 *              For Revision 2 and above, Integers are 64 bits. Yes, this
L
Linus Torvalds 已提交
155 156 157 158
 *              makes a difference.
 *
 ******************************************************************************/

L
Len Brown 已提交
159
void acpi_ut_set_integer_width(u8 revision)
L
Linus Torvalds 已提交
160 161
{

162
	if (revision < 2) {
B
Bob Moore 已提交
163 164 165

		/* 32-bit case */

L
Linus Torvalds 已提交
166 167 168
		acpi_gbl_integer_bit_width = 32;
		acpi_gbl_integer_nybble_width = 8;
		acpi_gbl_integer_byte_width = 4;
L
Len Brown 已提交
169
	} else {
B
Bob Moore 已提交
170 171
		/* 64-bit case (ACPI 2.0+) */

L
Linus Torvalds 已提交
172 173 174 175 176 177 178 179 180 181
		acpi_gbl_integer_bit_width = 64;
		acpi_gbl_integer_nybble_width = 16;
		acpi_gbl_integer_byte_width = 8;
	}
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ut_create_update_state_and_push
 *
182 183
 * PARAMETERS:  object          - Object to be added to the new state
 *              action          - Increment/Decrement
L
Linus Torvalds 已提交
184 185
 *              state_list      - List the state will be added to
 *
R
Robert Moore 已提交
186
 * RETURN:      Status
L
Linus Torvalds 已提交
187 188 189 190 191 192
 *
 * DESCRIPTION: Create a new state and push it
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
193 194 195
acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
				     u16 action,
				     union acpi_generic_state **state_list)
L
Linus Torvalds 已提交
196
{
L
Len Brown 已提交
197
	union acpi_generic_state *state;
L
Linus Torvalds 已提交
198

L
Len Brown 已提交
199
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
200 201 202 203 204 205 206

	/* Ignore null objects; these are expected */

	if (!object) {
		return (AE_OK);
	}

L
Len Brown 已提交
207
	state = acpi_ut_create_update_state(object, action);
L
Linus Torvalds 已提交
208 209 210 211
	if (!state) {
		return (AE_NO_MEMORY);
	}

L
Len Brown 已提交
212
	acpi_ut_push_generic_state(state_list, state);
L
Linus Torvalds 已提交
213 214 215 216 217 218 219
	return (AE_OK);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ut_walk_package_tree
 *
R
Robert Moore 已提交
220 221 222
 * PARAMETERS:  source_object       - The package to walk
 *              target_object       - Target object (if package is being copied)
 *              walk_callback       - Called once for each package element
223
 *              context             - Passed to the callback function
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Walk through a package
 *
 ******************************************************************************/

acpi_status
232
acpi_ut_walk_package_tree(union acpi_operand_object *source_object,
L
Len Brown 已提交
233 234
			  void *target_object,
			  acpi_pkg_callback walk_callback, void *context)
L
Linus Torvalds 已提交
235
{
L
Len Brown 已提交
236 237 238 239 240
	acpi_status status = AE_OK;
	union acpi_generic_state *state_list = NULL;
	union acpi_generic_state *state;
	u32 this_index;
	union acpi_operand_object *this_source_obj;
L
Linus Torvalds 已提交
241

B
Bob Moore 已提交
242
	ACPI_FUNCTION_TRACE(ut_walk_package_tree);
L
Linus Torvalds 已提交
243

L
Len Brown 已提交
244
	state = acpi_ut_create_pkg_state(source_object, target_object, 0);
L
Linus Torvalds 已提交
245
	if (!state) {
L
Len Brown 已提交
246
		return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
247 248 249
	}

	while (state) {
B
Bob Moore 已提交
250

L
Linus Torvalds 已提交
251 252
		/* Get one element of the package */

L
Len Brown 已提交
253
		this_index = state->pkg.index;
L
Linus Torvalds 已提交
254
		this_source_obj = (union acpi_operand_object *)
L
Len Brown 已提交
255
		    state->pkg.source_object->package.elements[this_index];
L
Linus Torvalds 已提交
256 257 258

		/*
		 * Check for:
259
		 * 1) An uninitialized package element. It is completely
L
Linus Torvalds 已提交
260 261
		 *    legal to declare a package and leave it uninitialized
		 * 2) Not an internal object - can be a namespace node instead
262
		 * 3) Any type other than a package. Packages are handled in else
L
Linus Torvalds 已提交
263 264 265
		 *    case below.
		 */
		if ((!this_source_obj) ||
L
Len Brown 已提交
266 267
		    (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
		     ACPI_DESC_TYPE_OPERAND)
268
		    || (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
L
Len Brown 已提交
269 270 271 272 273
			status =
			    walk_callback(ACPI_COPY_TYPE_SIMPLE,
					  this_source_obj, state, context);
			if (ACPI_FAILURE(status)) {
				return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
274 275 276
			}

			state->pkg.index++;
L
Len Brown 已提交
277 278
			while (state->pkg.index >=
			       state->pkg.source_object->package.count) {
L
Linus Torvalds 已提交
279 280
				/*
				 * We've handled all of the objects at this level,  This means
281
				 * that we have just completed a package. That package may
L
Linus Torvalds 已提交
282 283 284 285
				 * have contained one or more packages itself.
				 *
				 * Delete this state and pop the previous state (package).
				 */
L
Len Brown 已提交
286 287
				acpi_ut_delete_generic_state(state);
				state = acpi_ut_pop_generic_state(&state_list);
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296

				/* Finished when there are no more states */

				if (!state) {
					/*
					 * We have handled all of the objects in the top level
					 * package just add the length of the package objects
					 * and exit
					 */
L
Len Brown 已提交
297
					return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
298 299 300 301 302 303 304 305
				}

				/*
				 * Go back up a level and move the index past the just
				 * completed package object.
				 */
				state->pkg.index++;
			}
L
Len Brown 已提交
306
		} else {
L
Linus Torvalds 已提交
307 308
			/* This is a subobject of type package */

L
Len Brown 已提交
309 310 311 312 313
			status =
			    walk_callback(ACPI_COPY_TYPE_PACKAGE,
					  this_source_obj, state, context);
			if (ACPI_FAILURE(status)) {
				return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
314 315 316 317 318 319
			}

			/*
			 * Push the current state and create a new one
			 * The callback above returned a new target package object.
			 */
L
Len Brown 已提交
320 321 322 323
			acpi_ut_push_generic_state(&state_list, state);
			state = acpi_ut_create_pkg_state(this_source_obj,
							 state->pkg.
							 this_target_obj, 0);
L
Linus Torvalds 已提交
324
			if (!state) {
325 326 327 328 329 330 331 332 333

				/* Free any stacked Update State objects */

				while (state_list) {
					state =
					    acpi_ut_pop_generic_state
					    (&state_list);
					acpi_ut_delete_generic_state(state);
				}
L
Len Brown 已提交
334
				return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
335 336 337 338 339 340
			}
		}
	}

	/* We should never get here */

L
Len Brown 已提交
341
	return_ACPI_STATUS(AE_AML_INTERNAL);
L
Linus Torvalds 已提交
342
}
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

#ifdef ACPI_DEBUG_OUTPUT
/*******************************************************************************
 *
 * FUNCTION:    acpi_ut_display_init_pathname
 *
 * PARAMETERS:  type                - Object type of the node
 *              obj_handle          - Handle whose pathname will be displayed
 *              path                - Additional path string to be appended.
 *                                      (NULL if no extra path)
 *
 * RETURN:      acpi_status
 *
 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
 *
 ******************************************************************************/

void
acpi_ut_display_init_pathname(u8 type,
			      struct acpi_namespace_node *obj_handle,
			      char *path)
{
	acpi_status status;
	struct acpi_buffer buffer;

	ACPI_FUNCTION_ENTRY();

	/* Only print the path if the appropriate debug level is enabled */

	if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
		return;
	}

	/* Get the full pathname to the node */

	buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
379
	status = acpi_ns_handle_to_pathname(obj_handle, &buffer, FALSE);
380 381 382 383 384 385 386 387
	if (ACPI_FAILURE(status)) {
		return;
	}

	/* Print what we're doing */

	switch (type) {
	case ACPI_TYPE_METHOD:
388

389 390 391 392
		acpi_os_printf("Executing  ");
		break;

	default:
393

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
		acpi_os_printf("Initializing ");
		break;
	}

	/* Print the object type and pathname */

	acpi_os_printf("%-12s %s",
		       acpi_ut_get_type_name(type), (char *)buffer.pointer);

	/* Extra path is used to append names like _STA, _INI, etc. */

	if (path) {
		acpi_os_printf(".%s", path);
	}
	acpi_os_printf("\n");

	ACPI_FREE(buffer.pointer);
}
#endif