1 /****************************************************************************** 2 * 3 * Module Name: exutils - interpreter/scanner utilities 4 * 5 *****************************************************************************/ 6 7 /* 8 * Copyright (C) 2000 - 2012, Intel Corp. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions, and the following disclaimer, 16 * without modification. 17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18 * substantially similar to the "NO WARRANTY" disclaimer below 19 * ("Disclaimer") and any redistribution must be conditioned upon 20 * including a substantially similar Disclaimer requirement for further 21 * binary redistribution. 22 * 3. Neither the names of the above-listed copyright holders nor the names 23 * of any contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * Alternatively, this software may be distributed under the terms of the 27 * GNU General Public License ("GPL") version 2 as published by the Free 28 * Software Foundation. 29 * 30 * NO WARRANTY 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41 * POSSIBILITY OF SUCH DAMAGES. 42 */ 43 44 #define __EXUTILS_C__ 45 46 /* 47 * DEFINE_AML_GLOBALS is tested in amlcode.h 48 * to determine whether certain global names should be "defined" or only 49 * "declared" in the current compilation. This enhances maintainability 50 * by enabling a single header file to embody all knowledge of the names 51 * in question. 52 * 53 * Exactly one module of any executable should #define DEFINE_GLOBALS 54 * before #including the header files which use this convention. The 55 * names in question will be defined and initialized in that module, 56 * and declared as extern in all other modules which #include those 57 * header files. 58 */ 59 60 #define DEFINE_AML_GLOBALS 61 62 #include <contrib/dev/acpica/include/acpi.h> 63 #include <contrib/dev/acpica/include/accommon.h> 64 #include <contrib/dev/acpica/include/acinterp.h> 65 #include <contrib/dev/acpica/include/amlcode.h> 66 67 #define _COMPONENT ACPI_EXECUTER 68 ACPI_MODULE_NAME ("exutils") 69 70 /* Local prototypes */ 71 72 static UINT32 73 AcpiExDigitsNeeded ( 74 UINT64 Value, 75 UINT32 Base); 76 77 78 #ifndef ACPI_NO_METHOD_EXECUTION 79 /******************************************************************************* 80 * 81 * FUNCTION: AcpiExEnterInterpreter 82 * 83 * PARAMETERS: None 84 * 85 * RETURN: None 86 * 87 * DESCRIPTION: Enter the interpreter execution region. Failure to enter 88 * the interpreter region is a fatal system error. Used in 89 * conjunction with ExitInterpreter. 90 * 91 ******************************************************************************/ 92 93 void 94 AcpiExEnterInterpreter ( 95 void) 96 { 97 ACPI_STATUS Status; 98 99 100 ACPI_FUNCTION_TRACE (ExEnterInterpreter); 101 102 103 Status = AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER); 104 if (ACPI_FAILURE (Status)) 105 { 106 ACPI_ERROR ((AE_INFO, "Could not acquire AML Interpreter mutex")); 107 } 108 109 return_VOID; 110 } 111 112 113 /******************************************************************************* 114 * 115 * FUNCTION: AcpiExReacquireInterpreter 116 * 117 * PARAMETERS: None 118 * 119 * RETURN: None 120 * 121 * DESCRIPTION: Reacquire the interpreter execution region from within the 122 * interpreter code. Failure to enter the interpreter region is a 123 * fatal system error. Used in conjunction with 124 * RelinquishInterpreter 125 * 126 ******************************************************************************/ 127 128 void 129 AcpiExReacquireInterpreter ( 130 void) 131 { 132 ACPI_FUNCTION_TRACE (ExReacquireInterpreter); 133 134 135 /* 136 * If the global serialized flag is set, do not release the interpreter, 137 * since it was not actually released by AcpiExRelinquishInterpreter. 138 * This forces the interpreter to be single threaded. 139 */ 140 if (!AcpiGbl_AllMethodsSerialized) 141 { 142 AcpiExEnterInterpreter (); 143 } 144 145 return_VOID; 146 } 147 148 149 /******************************************************************************* 150 * 151 * FUNCTION: AcpiExExitInterpreter 152 * 153 * PARAMETERS: None 154 * 155 * RETURN: None 156 * 157 * DESCRIPTION: Exit the interpreter execution region. This is the top level 158 * routine used to exit the interpreter when all processing has 159 * been completed. 160 * 161 ******************************************************************************/ 162 163 void 164 AcpiExExitInterpreter ( 165 void) 166 { 167 ACPI_STATUS Status; 168 169 170 ACPI_FUNCTION_TRACE (ExExitInterpreter); 171 172 173 Status = AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER); 174 if (ACPI_FAILURE (Status)) 175 { 176 ACPI_ERROR ((AE_INFO, "Could not release AML Interpreter mutex")); 177 } 178 179 return_VOID; 180 } 181 182 183 /******************************************************************************* 184 * 185 * FUNCTION: AcpiExRelinquishInterpreter 186 * 187 * PARAMETERS: None 188 * 189 * RETURN: None 190 * 191 * DESCRIPTION: Exit the interpreter execution region, from within the 192 * interpreter - before attempting an operation that will possibly 193 * block the running thread. 194 * 195 * Cases where the interpreter is unlocked internally 196 * 1) Method to be blocked on a Sleep() AML opcode 197 * 2) Method to be blocked on an Acquire() AML opcode 198 * 3) Method to be blocked on a Wait() AML opcode 199 * 4) Method to be blocked to acquire the global lock 200 * 5) Method to be blocked waiting to execute a serialized control method 201 * that is currently executing 202 * 6) About to invoke a user-installed opregion handler 203 * 204 ******************************************************************************/ 205 206 void 207 AcpiExRelinquishInterpreter ( 208 void) 209 { 210 ACPI_FUNCTION_TRACE (ExRelinquishInterpreter); 211 212 213 /* 214 * If the global serialized flag is set, do not release the interpreter. 215 * This forces the interpreter to be single threaded. 216 */ 217 if (!AcpiGbl_AllMethodsSerialized) 218 { 219 AcpiExExitInterpreter (); 220 } 221 222 return_VOID; 223 } 224 225 226 /******************************************************************************* 227 * 228 * FUNCTION: AcpiExTruncateFor32bitTable 229 * 230 * PARAMETERS: ObjDesc - Object to be truncated 231 * 232 * RETURN: none 233 * 234 * DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is 235 * 32-bit, as determined by the revision of the DSDT. 236 * 237 ******************************************************************************/ 238 239 void 240 AcpiExTruncateFor32bitTable ( 241 ACPI_OPERAND_OBJECT *ObjDesc) 242 { 243 244 ACPI_FUNCTION_ENTRY (); 245 246 247 /* 248 * Object must be a valid number and we must be executing 249 * a control method. NS node could be there for AML_INT_NAMEPATH_OP. 250 */ 251 if ((!ObjDesc) || 252 (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) != ACPI_DESC_TYPE_OPERAND) || 253 (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)) 254 { 255 return; 256 } 257 258 if (AcpiGbl_IntegerByteWidth == 4) 259 { 260 /* 261 * We are running a method that exists in a 32-bit ACPI table. 262 * Truncate the value to 32 bits by zeroing out the upper 32-bit field 263 */ 264 ObjDesc->Integer.Value &= (UINT64) ACPI_UINT32_MAX; 265 } 266 } 267 268 269 /******************************************************************************* 270 * 271 * FUNCTION: AcpiExAcquireGlobalLock 272 * 273 * PARAMETERS: FieldFlags - Flags with Lock rule: 274 * AlwaysLock or NeverLock 275 * 276 * RETURN: None 277 * 278 * DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field 279 * flags specifiy that it is to be obtained before field access. 280 * 281 ******************************************************************************/ 282 283 void 284 AcpiExAcquireGlobalLock ( 285 UINT32 FieldFlags) 286 { 287 ACPI_STATUS Status; 288 289 290 ACPI_FUNCTION_TRACE (ExAcquireGlobalLock); 291 292 293 /* Only use the lock if the AlwaysLock bit is set */ 294 295 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK)) 296 { 297 return_VOID; 298 } 299 300 /* Attempt to get the global lock, wait forever */ 301 302 Status = AcpiExAcquireMutexObject (ACPI_WAIT_FOREVER, 303 AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ()); 304 305 if (ACPI_FAILURE (Status)) 306 { 307 ACPI_EXCEPTION ((AE_INFO, Status, 308 "Could not acquire Global Lock")); 309 } 310 311 return_VOID; 312 } 313 314 315 /******************************************************************************* 316 * 317 * FUNCTION: AcpiExReleaseGlobalLock 318 * 319 * PARAMETERS: FieldFlags - Flags with Lock rule: 320 * AlwaysLock or NeverLock 321 * 322 * RETURN: None 323 * 324 * DESCRIPTION: Release the ACPI hardware Global Lock 325 * 326 ******************************************************************************/ 327 328 void 329 AcpiExReleaseGlobalLock ( 330 UINT32 FieldFlags) 331 { 332 ACPI_STATUS Status; 333 334 335 ACPI_FUNCTION_TRACE (ExReleaseGlobalLock); 336 337 338 /* Only use the lock if the AlwaysLock bit is set */ 339 340 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK)) 341 { 342 return_VOID; 343 } 344 345 /* Release the global lock */ 346 347 Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex); 348 if (ACPI_FAILURE (Status)) 349 { 350 /* Report the error, but there isn't much else we can do */ 351 352 ACPI_EXCEPTION ((AE_INFO, Status, 353 "Could not release Global Lock")); 354 } 355 356 return_VOID; 357 } 358 359 360 /******************************************************************************* 361 * 362 * FUNCTION: AcpiExDigitsNeeded 363 * 364 * PARAMETERS: Value - Value to be represented 365 * Base - Base of representation 366 * 367 * RETURN: The number of digits. 368 * 369 * DESCRIPTION: Calculate the number of digits needed to represent the Value 370 * in the given Base (Radix) 371 * 372 ******************************************************************************/ 373 374 static UINT32 375 AcpiExDigitsNeeded ( 376 UINT64 Value, 377 UINT32 Base) 378 { 379 UINT32 NumDigits; 380 UINT64 CurrentValue; 381 382 383 ACPI_FUNCTION_TRACE (ExDigitsNeeded); 384 385 386 /* UINT64 is unsigned, so we don't worry about a '-' prefix */ 387 388 if (Value == 0) 389 { 390 return_UINT32 (1); 391 } 392 393 CurrentValue = Value; 394 NumDigits = 0; 395 396 /* Count the digits in the requested base */ 397 398 while (CurrentValue) 399 { 400 (void) AcpiUtShortDivide (CurrentValue, Base, &CurrentValue, NULL); 401 NumDigits++; 402 } 403 404 return_UINT32 (NumDigits); 405 } 406 407 408 /******************************************************************************* 409 * 410 * FUNCTION: AcpiExEisaIdToString 411 * 412 * PARAMETERS: CompressedId - EISAID to be converted 413 * OutString - Where to put the converted string (8 bytes) 414 * 415 * RETURN: None 416 * 417 * DESCRIPTION: Convert a numeric EISAID to string representation. Return 418 * buffer must be large enough to hold the string. The string 419 * returned is always exactly of length ACPI_EISAID_STRING_SIZE 420 * (includes null terminator). The EISAID is always 32 bits. 421 * 422 ******************************************************************************/ 423 424 void 425 AcpiExEisaIdToString ( 426 char *OutString, 427 UINT64 CompressedId) 428 { 429 UINT32 SwappedId; 430 431 432 ACPI_FUNCTION_ENTRY (); 433 434 435 /* The EISAID should be a 32-bit integer */ 436 437 if (CompressedId > ACPI_UINT32_MAX) 438 { 439 ACPI_WARNING ((AE_INFO, 440 "Expected EISAID is larger than 32 bits: 0x%8.8X%8.8X, truncating", 441 ACPI_FORMAT_UINT64 (CompressedId))); 442 } 443 444 /* Swap ID to big-endian to get contiguous bits */ 445 446 SwappedId = AcpiUtDwordByteSwap ((UINT32) CompressedId); 447 448 /* First 3 bytes are uppercase letters. Next 4 bytes are hexadecimal */ 449 450 OutString[0] = (char) (0x40 + (((unsigned long) SwappedId >> 26) & 0x1F)); 451 OutString[1] = (char) (0x40 + ((SwappedId >> 21) & 0x1F)); 452 OutString[2] = (char) (0x40 + ((SwappedId >> 16) & 0x1F)); 453 OutString[3] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 12); 454 OutString[4] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 8); 455 OutString[5] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 4); 456 OutString[6] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 0); 457 OutString[7] = 0; 458 } 459 460 461 /******************************************************************************* 462 * 463 * FUNCTION: AcpiExIntegerToString 464 * 465 * PARAMETERS: OutString - Where to put the converted string. At least 466 * 21 bytes are needed to hold the largest 467 * possible 64-bit integer. 468 * Value - Value to be converted 469 * 470 * RETURN: None, string 471 * 472 * DESCRIPTION: Convert a 64-bit integer to decimal string representation. 473 * Assumes string buffer is large enough to hold the string. The 474 * largest string is (ACPI_MAX64_DECIMAL_DIGITS + 1). 475 * 476 ******************************************************************************/ 477 478 void 479 AcpiExIntegerToString ( 480 char *OutString, 481 UINT64 Value) 482 { 483 UINT32 Count; 484 UINT32 DigitsNeeded; 485 UINT32 Remainder; 486 487 488 ACPI_FUNCTION_ENTRY (); 489 490 491 DigitsNeeded = AcpiExDigitsNeeded (Value, 10); 492 OutString[DigitsNeeded] = 0; 493 494 for (Count = DigitsNeeded; Count > 0; Count--) 495 { 496 (void) AcpiUtShortDivide (Value, 10, &Value, &Remainder); 497 OutString[Count-1] = (char) ('0' + Remainder);\ 498 } 499 } 500 501 502 /******************************************************************************* 503 * 504 * FUNCTION: AcpiIsValidSpaceId 505 * 506 * PARAMETERS: SpaceId - ID to be validated 507 * 508 * RETURN: TRUE if valid/supported ID. 509 * 510 * DESCRIPTION: Validate an operation region SpaceID. 511 * 512 ******************************************************************************/ 513 514 BOOLEAN 515 AcpiIsValidSpaceId ( 516 UINT8 SpaceId) 517 { 518 519 if ((SpaceId >= ACPI_NUM_PREDEFINED_REGIONS) && 520 (SpaceId < ACPI_USER_REGION_BEGIN) && 521 (SpaceId != ACPI_ADR_SPACE_DATA_TABLE) && 522 (SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE)) 523 { 524 return (FALSE); 525 } 526 527 return (TRUE); 528 } 529 530 531 #endif 532