nsrepair2.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: nsrepair2 - Repair for objects returned by specific
  5. * predefined methods
  6. *
  7. * Copyright (C) 2000 - 2022, Intel Corp.
  8. *
  9. *****************************************************************************/
  10. #include <acpi/acpi.h>
  11. #include "accommon.h"
  12. #include "acnamesp.h"
  13. #define _COMPONENT ACPI_NAMESPACE
  14. ACPI_MODULE_NAME("nsrepair2")
  15. /*
  16. * Information structure and handler for ACPI predefined names that can
  17. * be repaired on a per-name basis.
  18. */
  19. typedef
  20. acpi_status (*acpi_repair_function) (struct acpi_evaluate_info * info,
  21. union acpi_operand_object **
  22. return_object_ptr);
  23. typedef struct acpi_repair_info {
  24. char name[ACPI_NAMESEG_SIZE];
  25. acpi_repair_function repair_function;
  26. } acpi_repair_info;
  27. /* Local prototypes */
  28. static const struct acpi_repair_info *acpi_ns_match_complex_repair(struct
  29. acpi_namespace_node
  30. *node);
  31. static acpi_status
  32. acpi_ns_repair_ALR(struct acpi_evaluate_info *info,
  33. union acpi_operand_object **return_object_ptr);
  34. static acpi_status
  35. acpi_ns_repair_CID(struct acpi_evaluate_info *info,
  36. union acpi_operand_object **return_object_ptr);
  37. static acpi_status
  38. acpi_ns_repair_CST(struct acpi_evaluate_info *info,
  39. union acpi_operand_object **return_object_ptr);
  40. static acpi_status
  41. acpi_ns_repair_FDE(struct acpi_evaluate_info *info,
  42. union acpi_operand_object **return_object_ptr);
  43. static acpi_status
  44. acpi_ns_repair_HID(struct acpi_evaluate_info *info,
  45. union acpi_operand_object **return_object_ptr);
  46. static acpi_status
  47. acpi_ns_repair_PRT(struct acpi_evaluate_info *info,
  48. union acpi_operand_object **return_object_ptr);
  49. static acpi_status
  50. acpi_ns_repair_PSS(struct acpi_evaluate_info *info,
  51. union acpi_operand_object **return_object_ptr);
  52. static acpi_status
  53. acpi_ns_repair_TSS(struct acpi_evaluate_info *info,
  54. union acpi_operand_object **return_object_ptr);
  55. static acpi_status
  56. acpi_ns_check_sorted_list(struct acpi_evaluate_info *info,
  57. union acpi_operand_object *return_object,
  58. u32 start_index,
  59. u32 expected_count,
  60. u32 sort_index,
  61. u8 sort_direction, char *sort_key_name);
  62. /* Values for sort_direction above */
  63. #define ACPI_SORT_ASCENDING 0
  64. #define ACPI_SORT_DESCENDING 1
  65. static void
  66. acpi_ns_remove_element(union acpi_operand_object *obj_desc, u32 index);
  67. static void
  68. acpi_ns_sort_list(union acpi_operand_object **elements,
  69. u32 count, u32 index, u8 sort_direction);
  70. /*
  71. * This table contains the names of the predefined methods for which we can
  72. * perform more complex repairs.
  73. *
  74. * As necessary:
  75. *
  76. * _ALR: Sort the list ascending by ambient_illuminance
  77. * _CID: Strings: uppercase all, remove any leading asterisk
  78. * _CST: Sort the list ascending by C state type
  79. * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
  80. * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
  81. * _HID: Strings: uppercase all, remove any leading asterisk
  82. * _PRT: Fix reversed source_name and source_index
  83. * _PSS: Sort the list descending by Power
  84. * _TSS: Sort the list descending by Power
  85. *
  86. * Names that must be packages, but cannot be sorted:
  87. *
  88. * _BCL: Values are tied to the Package index where they appear, and cannot
  89. * be moved or sorted. These index values are used for _BQC and _BCM.
  90. * However, we can fix the case where a buffer is returned, by converting
  91. * it to a Package of integers.
  92. */
  93. static const struct acpi_repair_info acpi_ns_repairable_names[] = {
  94. {"_ALR", acpi_ns_repair_ALR},
  95. {"_CID", acpi_ns_repair_CID},
  96. {"_CST", acpi_ns_repair_CST},
  97. {"_FDE", acpi_ns_repair_FDE},
  98. {"_GTM", acpi_ns_repair_FDE}, /* _GTM has same repair as _FDE */
  99. {"_HID", acpi_ns_repair_HID},
  100. {"_PRT", acpi_ns_repair_PRT},
  101. {"_PSS", acpi_ns_repair_PSS},
  102. {"_TSS", acpi_ns_repair_TSS},
  103. {{0, 0, 0, 0}, NULL} /* Table terminator */
  104. };
  105. #define ACPI_FDE_FIELD_COUNT 5
  106. #define ACPI_FDE_BYTE_BUFFER_SIZE 5
  107. #define ACPI_FDE_DWORD_BUFFER_SIZE (ACPI_FDE_FIELD_COUNT * (u32) sizeof (u32))
  108. /******************************************************************************
  109. *
  110. * FUNCTION: acpi_ns_complex_repairs
  111. *
  112. * PARAMETERS: info - Method execution information block
  113. * node - Namespace node for the method/object
  114. * validate_status - Original status of earlier validation
  115. * return_object_ptr - Pointer to the object returned from the
  116. * evaluation of a method or object
  117. *
  118. * RETURN: Status. AE_OK if repair was successful. If name is not
  119. * matched, validate_status is returned.
  120. *
  121. * DESCRIPTION: Attempt to repair/convert a return object of a type that was
  122. * not expected.
  123. *
  124. *****************************************************************************/
  125. acpi_status
  126. acpi_ns_complex_repairs(struct acpi_evaluate_info *info,
  127. struct acpi_namespace_node *node,
  128. acpi_status validate_status,
  129. union acpi_operand_object **return_object_ptr)
  130. {
  131. const struct acpi_repair_info *predefined;
  132. acpi_status status;
  133. ACPI_FUNCTION_TRACE(ns_complex_repairs);
  134. /* Check if this name is in the list of repairable names */
  135. predefined = acpi_ns_match_complex_repair(node);
  136. if (!predefined) {
  137. return_ACPI_STATUS(validate_status);
  138. }
  139. status = predefined->repair_function(info, return_object_ptr);
  140. return_ACPI_STATUS(status);
  141. }
  142. /******************************************************************************
  143. *
  144. * FUNCTION: acpi_ns_match_complex_repair
  145. *
  146. * PARAMETERS: node - Namespace node for the method/object
  147. *
  148. * RETURN: Pointer to entry in repair table. NULL indicates not found.
  149. *
  150. * DESCRIPTION: Check an object name against the repairable object list.
  151. *
  152. *****************************************************************************/
  153. static const struct acpi_repair_info *acpi_ns_match_complex_repair(struct
  154. acpi_namespace_node
  155. *node)
  156. {
  157. const struct acpi_repair_info *this_name;
  158. /* Search info table for a repairable predefined method/object name */
  159. this_name = acpi_ns_repairable_names;
  160. while (this_name->repair_function) {
  161. if (ACPI_COMPARE_NAMESEG(node->name.ascii, this_name->name)) {
  162. return (this_name);
  163. }
  164. this_name++;
  165. }
  166. return (NULL); /* Not found */
  167. }
  168. /******************************************************************************
  169. *
  170. * FUNCTION: acpi_ns_repair_ALR
  171. *
  172. * PARAMETERS: info - Method execution information block
  173. * return_object_ptr - Pointer to the object returned from the
  174. * evaluation of a method or object
  175. *
  176. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  177. *
  178. * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
  179. * ascending by the ambient illuminance values.
  180. *
  181. *****************************************************************************/
  182. static acpi_status
  183. acpi_ns_repair_ALR(struct acpi_evaluate_info *info,
  184. union acpi_operand_object **return_object_ptr)
  185. {
  186. union acpi_operand_object *return_object = *return_object_ptr;
  187. acpi_status status;
  188. status = acpi_ns_check_sorted_list(info, return_object, 0, 2, 1,
  189. ACPI_SORT_ASCENDING,
  190. "AmbientIlluminance");
  191. return (status);
  192. }
  193. /******************************************************************************
  194. *
  195. * FUNCTION: acpi_ns_repair_FDE
  196. *
  197. * PARAMETERS: info - Method execution information block
  198. * return_object_ptr - Pointer to the object returned from the
  199. * evaluation of a method or object
  200. *
  201. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  202. *
  203. * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
  204. * value is a Buffer of 5 DWORDs. This function repairs a common
  205. * problem where the return value is a Buffer of BYTEs, not
  206. * DWORDs.
  207. *
  208. *****************************************************************************/
  209. static acpi_status
  210. acpi_ns_repair_FDE(struct acpi_evaluate_info *info,
  211. union acpi_operand_object **return_object_ptr)
  212. {
  213. union acpi_operand_object *return_object = *return_object_ptr;
  214. union acpi_operand_object *buffer_object;
  215. u8 *byte_buffer;
  216. u32 *dword_buffer;
  217. u32 i;
  218. ACPI_FUNCTION_NAME(ns_repair_FDE);
  219. switch (return_object->common.type) {
  220. case ACPI_TYPE_BUFFER:
  221. /* This is the expected type. Length should be (at least) 5 DWORDs */
  222. if (return_object->buffer.length >= ACPI_FDE_DWORD_BUFFER_SIZE) {
  223. return (AE_OK);
  224. }
  225. /* We can only repair if we have exactly 5 BYTEs */
  226. if (return_object->buffer.length != ACPI_FDE_BYTE_BUFFER_SIZE) {
  227. ACPI_WARN_PREDEFINED((AE_INFO,
  228. info->full_pathname,
  229. info->node_flags,
  230. "Incorrect return buffer length %u, expected %u",
  231. return_object->buffer.length,
  232. ACPI_FDE_DWORD_BUFFER_SIZE));
  233. return (AE_AML_OPERAND_TYPE);
  234. }
  235. /* Create the new (larger) buffer object */
  236. buffer_object =
  237. acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
  238. if (!buffer_object) {
  239. return (AE_NO_MEMORY);
  240. }
  241. /* Expand each byte to a DWORD */
  242. byte_buffer = return_object->buffer.pointer;
  243. dword_buffer = ACPI_CAST_PTR(u32,
  244. buffer_object->buffer.pointer);
  245. for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++) {
  246. *dword_buffer = (u32) *byte_buffer;
  247. dword_buffer++;
  248. byte_buffer++;
  249. }
  250. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  251. "%s Expanded Byte Buffer to expected DWord Buffer\n",
  252. info->full_pathname));
  253. break;
  254. default:
  255. return (AE_AML_OPERAND_TYPE);
  256. }
  257. /* Delete the original return object, return the new buffer object */
  258. acpi_ut_remove_reference(return_object);
  259. *return_object_ptr = buffer_object;
  260. info->return_flags |= ACPI_OBJECT_REPAIRED;
  261. return (AE_OK);
  262. }
  263. /******************************************************************************
  264. *
  265. * FUNCTION: acpi_ns_repair_CID
  266. *
  267. * PARAMETERS: info - Method execution information block
  268. * return_object_ptr - Pointer to the object returned from the
  269. * evaluation of a method or object
  270. *
  271. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  272. *
  273. * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
  274. * letters are uppercase and that there is no leading asterisk.
  275. * If a Package, ensure same for all string elements.
  276. *
  277. *****************************************************************************/
  278. static acpi_status
  279. acpi_ns_repair_CID(struct acpi_evaluate_info *info,
  280. union acpi_operand_object **return_object_ptr)
  281. {
  282. acpi_status status;
  283. union acpi_operand_object *return_object = *return_object_ptr;
  284. union acpi_operand_object **element_ptr;
  285. union acpi_operand_object *original_element;
  286. u16 original_ref_count;
  287. u32 i;
  288. ACPI_FUNCTION_TRACE(ns_repair_CID);
  289. /* Check for _CID as a simple string */
  290. if (return_object->common.type == ACPI_TYPE_STRING) {
  291. status = acpi_ns_repair_HID(info, return_object_ptr);
  292. return_ACPI_STATUS(status);
  293. }
  294. /* Exit if not a Package */
  295. if (return_object->common.type != ACPI_TYPE_PACKAGE) {
  296. return_ACPI_STATUS(AE_OK);
  297. }
  298. /* Examine each element of the _CID package */
  299. element_ptr = return_object->package.elements;
  300. for (i = 0; i < return_object->package.count; i++) {
  301. original_element = *element_ptr;
  302. original_ref_count = original_element->common.reference_count;
  303. status = acpi_ns_repair_HID(info, element_ptr);
  304. if (ACPI_FAILURE(status)) {
  305. return_ACPI_STATUS(status);
  306. }
  307. if (original_element != *element_ptr) {
  308. /* Update reference count of new object */
  309. (*element_ptr)->common.reference_count =
  310. original_ref_count;
  311. }
  312. element_ptr++;
  313. }
  314. return_ACPI_STATUS(AE_OK);
  315. }
  316. /******************************************************************************
  317. *
  318. * FUNCTION: acpi_ns_repair_CST
  319. *
  320. * PARAMETERS: info - Method execution information block
  321. * return_object_ptr - Pointer to the object returned from the
  322. * evaluation of a method or object
  323. *
  324. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  325. *
  326. * DESCRIPTION: Repair for the _CST object:
  327. * 1. Sort the list ascending by C state type
  328. * 2. Ensure type cannot be zero
  329. * 3. A subpackage count of zero means _CST is meaningless
  330. * 4. Count must match the number of C state subpackages
  331. *
  332. *****************************************************************************/
  333. static acpi_status
  334. acpi_ns_repair_CST(struct acpi_evaluate_info *info,
  335. union acpi_operand_object **return_object_ptr)
  336. {
  337. union acpi_operand_object *return_object = *return_object_ptr;
  338. union acpi_operand_object **outer_elements;
  339. u32 outer_element_count;
  340. union acpi_operand_object *obj_desc;
  341. acpi_status status;
  342. u8 removing;
  343. u32 i;
  344. ACPI_FUNCTION_NAME(ns_repair_CST);
  345. /*
  346. * Check if the C-state type values are proportional.
  347. */
  348. outer_element_count = return_object->package.count - 1;
  349. i = 0;
  350. while (i < outer_element_count) {
  351. outer_elements = &return_object->package.elements[i + 1];
  352. removing = FALSE;
  353. if ((*outer_elements)->package.count == 0) {
  354. ACPI_WARN_PREDEFINED((AE_INFO,
  355. info->full_pathname,
  356. info->node_flags,
  357. "SubPackage[%u] - removing entry due to zero count",
  358. i));
  359. removing = TRUE;
  360. goto remove_element;
  361. }
  362. obj_desc = (*outer_elements)->package.elements[1]; /* Index1 = Type */
  363. if ((u32)obj_desc->integer.value == 0) {
  364. ACPI_WARN_PREDEFINED((AE_INFO,
  365. info->full_pathname,
  366. info->node_flags,
  367. "SubPackage[%u] - removing entry due to invalid Type(0)",
  368. i));
  369. removing = TRUE;
  370. }
  371. remove_element:
  372. if (removing) {
  373. acpi_ns_remove_element(return_object, i + 1);
  374. outer_element_count--;
  375. } else {
  376. i++;
  377. }
  378. }
  379. /* Update top-level package count, Type "Integer" checked elsewhere */
  380. obj_desc = return_object->package.elements[0];
  381. obj_desc->integer.value = outer_element_count;
  382. /*
  383. * Entries (subpackages) in the _CST Package must be sorted by the
  384. * C-state type, in ascending order.
  385. */
  386. status = acpi_ns_check_sorted_list(info, return_object, 1, 4, 1,
  387. ACPI_SORT_ASCENDING, "C-State Type");
  388. if (ACPI_FAILURE(status)) {
  389. return (status);
  390. }
  391. return (AE_OK);
  392. }
  393. /******************************************************************************
  394. *
  395. * FUNCTION: acpi_ns_repair_HID
  396. *
  397. * PARAMETERS: info - Method execution information block
  398. * return_object_ptr - Pointer to the object returned from the
  399. * evaluation of a method or object
  400. *
  401. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  402. *
  403. * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
  404. * letters are uppercase and that there is no leading asterisk.
  405. *
  406. *****************************************************************************/
  407. static acpi_status
  408. acpi_ns_repair_HID(struct acpi_evaluate_info *info,
  409. union acpi_operand_object **return_object_ptr)
  410. {
  411. union acpi_operand_object *return_object = *return_object_ptr;
  412. union acpi_operand_object *new_string;
  413. char *source;
  414. char *dest;
  415. ACPI_FUNCTION_NAME(ns_repair_HID);
  416. /* We only care about string _HID objects (not integers) */
  417. if (return_object->common.type != ACPI_TYPE_STRING) {
  418. return_ACPI_STATUS(AE_OK);
  419. }
  420. if (return_object->string.length == 0) {
  421. ACPI_WARN_PREDEFINED((AE_INFO,
  422. info->full_pathname, info->node_flags,
  423. "Invalid zero-length _HID or _CID string"));
  424. /* Return AE_OK anyway, let driver handle it */
  425. info->return_flags |= ACPI_OBJECT_REPAIRED;
  426. return_ACPI_STATUS(AE_OK);
  427. }
  428. /* It is simplest to always create a new string object */
  429. new_string = acpi_ut_create_string_object(return_object->string.length);
  430. if (!new_string) {
  431. return_ACPI_STATUS(AE_NO_MEMORY);
  432. }
  433. /*
  434. * Remove a leading asterisk if present. For some unknown reason, there
  435. * are many machines in the field that contains IDs like this.
  436. *
  437. * Examples: "*PNP0C03", "*ACPI0003"
  438. */
  439. source = return_object->string.pointer;
  440. if (*source == '*') {
  441. source++;
  442. new_string->string.length--;
  443. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  444. "%s: Removed invalid leading asterisk\n",
  445. info->full_pathname));
  446. }
  447. /*
  448. * Copy and uppercase the string. From the ACPI 5.0 specification:
  449. *
  450. * A valid PNP ID must be of the form "AAA####" where A is an uppercase
  451. * letter and # is a hex digit. A valid ACPI ID must be of the form
  452. * "NNNN####" where N is an uppercase letter or decimal digit, and
  453. * # is a hex digit.
  454. */
  455. for (dest = new_string->string.pointer; *source; dest++, source++) {
  456. *dest = (char)toupper((int)*source);
  457. }
  458. acpi_ut_remove_reference(return_object);
  459. *return_object_ptr = new_string;
  460. return_ACPI_STATUS(AE_OK);
  461. }
  462. /******************************************************************************
  463. *
  464. * FUNCTION: acpi_ns_repair_PRT
  465. *
  466. * PARAMETERS: info - Method execution information block
  467. * return_object_ptr - Pointer to the object returned from the
  468. * evaluation of a method or object
  469. *
  470. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  471. *
  472. * DESCRIPTION: Repair for the _PRT object. If necessary, fix reversed
  473. * source_name and source_index field, a common BIOS bug.
  474. *
  475. *****************************************************************************/
  476. static acpi_status
  477. acpi_ns_repair_PRT(struct acpi_evaluate_info *info,
  478. union acpi_operand_object **return_object_ptr)
  479. {
  480. union acpi_operand_object *package_object = *return_object_ptr;
  481. union acpi_operand_object **top_object_list;
  482. union acpi_operand_object **sub_object_list;
  483. union acpi_operand_object *obj_desc;
  484. union acpi_operand_object *sub_package;
  485. u32 element_count;
  486. u32 index;
  487. /* Each element in the _PRT package is a subpackage */
  488. top_object_list = package_object->package.elements;
  489. element_count = package_object->package.count;
  490. /* Examine each subpackage */
  491. for (index = 0; index < element_count; index++, top_object_list++) {
  492. sub_package = *top_object_list;
  493. sub_object_list = sub_package->package.elements;
  494. /* Check for minimum required element count */
  495. if (sub_package->package.count < 4) {
  496. continue;
  497. }
  498. /*
  499. * If the BIOS has erroneously reversed the _PRT source_name (index 2)
  500. * and the source_index (index 3), fix it. _PRT is important enough to
  501. * workaround this BIOS error. This also provides compatibility with
  502. * other ACPI implementations.
  503. */
  504. obj_desc = sub_object_list[3];
  505. if (!obj_desc || (obj_desc->common.type != ACPI_TYPE_INTEGER)) {
  506. sub_object_list[3] = sub_object_list[2];
  507. sub_object_list[2] = obj_desc;
  508. info->return_flags |= ACPI_OBJECT_REPAIRED;
  509. ACPI_WARN_PREDEFINED((AE_INFO,
  510. info->full_pathname,
  511. info->node_flags,
  512. "PRT[%X]: Fixed reversed SourceName and SourceIndex",
  513. index));
  514. }
  515. }
  516. return (AE_OK);
  517. }
  518. /******************************************************************************
  519. *
  520. * FUNCTION: acpi_ns_repair_PSS
  521. *
  522. * PARAMETERS: info - Method execution information block
  523. * return_object_ptr - Pointer to the object returned from the
  524. * evaluation of a method or object
  525. *
  526. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  527. *
  528. * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
  529. * by the CPU frequencies. Check that the power dissipation values
  530. * are all proportional to CPU frequency (i.e., sorting by
  531. * frequency should be the same as sorting by power.)
  532. *
  533. *****************************************************************************/
  534. static acpi_status
  535. acpi_ns_repair_PSS(struct acpi_evaluate_info *info,
  536. union acpi_operand_object **return_object_ptr)
  537. {
  538. union acpi_operand_object *return_object = *return_object_ptr;
  539. union acpi_operand_object **outer_elements;
  540. u32 outer_element_count;
  541. union acpi_operand_object **elements;
  542. union acpi_operand_object *obj_desc;
  543. u32 previous_value;
  544. acpi_status status;
  545. u32 i;
  546. /*
  547. * Entries (subpackages) in the _PSS Package must be sorted by power
  548. * dissipation, in descending order. If it appears that the list is
  549. * incorrectly sorted, sort it. We sort by cpu_frequency, since this
  550. * should be proportional to the power.
  551. */
  552. status = acpi_ns_check_sorted_list(info, return_object, 0, 6, 0,
  553. ACPI_SORT_DESCENDING,
  554. "CpuFrequency");
  555. if (ACPI_FAILURE(status)) {
  556. return (status);
  557. }
  558. /*
  559. * We now know the list is correctly sorted by CPU frequency. Check if
  560. * the power dissipation values are proportional.
  561. */
  562. previous_value = ACPI_UINT32_MAX;
  563. outer_elements = return_object->package.elements;
  564. outer_element_count = return_object->package.count;
  565. for (i = 0; i < outer_element_count; i++) {
  566. elements = (*outer_elements)->package.elements;
  567. obj_desc = elements[1]; /* Index1 = power_dissipation */
  568. if ((u32)obj_desc->integer.value > previous_value) {
  569. ACPI_WARN_PREDEFINED((AE_INFO,
  570. info->full_pathname,
  571. info->node_flags,
  572. "SubPackage[%u,%u] - suspicious power dissipation values",
  573. i - 1, i));
  574. }
  575. previous_value = (u32) obj_desc->integer.value;
  576. outer_elements++;
  577. }
  578. return (AE_OK);
  579. }
  580. /******************************************************************************
  581. *
  582. * FUNCTION: acpi_ns_repair_TSS
  583. *
  584. * PARAMETERS: info - Method execution information block
  585. * return_object_ptr - Pointer to the object returned from the
  586. * evaluation of a method or object
  587. *
  588. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  589. *
  590. * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
  591. * descending by the power dissipation values.
  592. *
  593. *****************************************************************************/
  594. static acpi_status
  595. acpi_ns_repair_TSS(struct acpi_evaluate_info *info,
  596. union acpi_operand_object **return_object_ptr)
  597. {
  598. union acpi_operand_object *return_object = *return_object_ptr;
  599. acpi_status status;
  600. struct acpi_namespace_node *node;
  601. /*
  602. * We can only sort the _TSS return package if there is no _PSS in the
  603. * same scope. This is because if _PSS is present, the ACPI specification
  604. * dictates that the _TSS Power Dissipation field is to be ignored, and
  605. * therefore some BIOSs leave garbage values in the _TSS Power field(s).
  606. * In this case, it is best to just return the _TSS package as-is.
  607. * (May, 2011)
  608. */
  609. status = acpi_ns_get_node(info->node, "^_PSS",
  610. ACPI_NS_NO_UPSEARCH, &node);
  611. if (ACPI_SUCCESS(status)) {
  612. return (AE_OK);
  613. }
  614. status = acpi_ns_check_sorted_list(info, return_object, 0, 5, 1,
  615. ACPI_SORT_DESCENDING,
  616. "PowerDissipation");
  617. return (status);
  618. }
  619. /******************************************************************************
  620. *
  621. * FUNCTION: acpi_ns_check_sorted_list
  622. *
  623. * PARAMETERS: info - Method execution information block
  624. * return_object - Pointer to the top-level returned object
  625. * start_index - Index of the first subpackage
  626. * expected_count - Minimum length of each subpackage
  627. * sort_index - Subpackage entry to sort on
  628. * sort_direction - Ascending or descending
  629. * sort_key_name - Name of the sort_index field
  630. *
  631. * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
  632. * has been repaired by sorting the list.
  633. *
  634. * DESCRIPTION: Check if the package list is valid and sorted correctly by the
  635. * sort_index. If not, then sort the list.
  636. *
  637. *****************************************************************************/
  638. static acpi_status
  639. acpi_ns_check_sorted_list(struct acpi_evaluate_info *info,
  640. union acpi_operand_object *return_object,
  641. u32 start_index,
  642. u32 expected_count,
  643. u32 sort_index,
  644. u8 sort_direction, char *sort_key_name)
  645. {
  646. u32 outer_element_count;
  647. union acpi_operand_object **outer_elements;
  648. union acpi_operand_object **elements;
  649. union acpi_operand_object *obj_desc;
  650. u32 i;
  651. u32 previous_value;
  652. ACPI_FUNCTION_NAME(ns_check_sorted_list);
  653. /* The top-level object must be a package */
  654. if (return_object->common.type != ACPI_TYPE_PACKAGE) {
  655. return (AE_AML_OPERAND_TYPE);
  656. }
  657. /*
  658. * NOTE: assumes list of subpackages contains no NULL elements.
  659. * Any NULL elements should have been removed by earlier call
  660. * to acpi_ns_remove_null_elements.
  661. */
  662. outer_element_count = return_object->package.count;
  663. if (!outer_element_count || start_index >= outer_element_count) {
  664. return (AE_AML_PACKAGE_LIMIT);
  665. }
  666. outer_elements = &return_object->package.elements[start_index];
  667. outer_element_count -= start_index;
  668. previous_value = 0;
  669. if (sort_direction == ACPI_SORT_DESCENDING) {
  670. previous_value = ACPI_UINT32_MAX;
  671. }
  672. /* Examine each subpackage */
  673. for (i = 0; i < outer_element_count; i++) {
  674. /* Each element of the top-level package must also be a package */
  675. if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
  676. return (AE_AML_OPERAND_TYPE);
  677. }
  678. /* Each subpackage must have the minimum length */
  679. if ((*outer_elements)->package.count < expected_count) {
  680. return (AE_AML_PACKAGE_LIMIT);
  681. }
  682. elements = (*outer_elements)->package.elements;
  683. obj_desc = elements[sort_index];
  684. if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
  685. return (AE_AML_OPERAND_TYPE);
  686. }
  687. /*
  688. * The list must be sorted in the specified order. If we detect a
  689. * discrepancy, sort the entire list.
  690. */
  691. if (((sort_direction == ACPI_SORT_ASCENDING) &&
  692. (obj_desc->integer.value < previous_value)) ||
  693. ((sort_direction == ACPI_SORT_DESCENDING) &&
  694. (obj_desc->integer.value > previous_value))) {
  695. acpi_ns_sort_list(&return_object->package.
  696. elements[start_index],
  697. outer_element_count, sort_index,
  698. sort_direction);
  699. info->return_flags |= ACPI_OBJECT_REPAIRED;
  700. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  701. "%s: Repaired unsorted list - now sorted by %s\n",
  702. info->full_pathname, sort_key_name));
  703. return (AE_OK);
  704. }
  705. previous_value = (u32) obj_desc->integer.value;
  706. outer_elements++;
  707. }
  708. return (AE_OK);
  709. }
  710. /******************************************************************************
  711. *
  712. * FUNCTION: acpi_ns_sort_list
  713. *
  714. * PARAMETERS: elements - Package object element list
  715. * count - Element count for above
  716. * index - Sort by which package element
  717. * sort_direction - Ascending or Descending sort
  718. *
  719. * RETURN: None
  720. *
  721. * DESCRIPTION: Sort the objects that are in a package element list.
  722. *
  723. * NOTE: Assumes that all NULL elements have been removed from the package,
  724. * and that all elements have been verified to be of type Integer.
  725. *
  726. *****************************************************************************/
  727. static void
  728. acpi_ns_sort_list(union acpi_operand_object **elements,
  729. u32 count, u32 index, u8 sort_direction)
  730. {
  731. union acpi_operand_object *obj_desc1;
  732. union acpi_operand_object *obj_desc2;
  733. union acpi_operand_object *temp_obj;
  734. u32 i;
  735. u32 j;
  736. /* Simple bubble sort */
  737. for (i = 1; i < count; i++) {
  738. for (j = (count - 1); j >= i; j--) {
  739. obj_desc1 = elements[j - 1]->package.elements[index];
  740. obj_desc2 = elements[j]->package.elements[index];
  741. if (((sort_direction == ACPI_SORT_ASCENDING) &&
  742. (obj_desc1->integer.value >
  743. obj_desc2->integer.value))
  744. || ((sort_direction == ACPI_SORT_DESCENDING)
  745. && (obj_desc1->integer.value <
  746. obj_desc2->integer.value))) {
  747. temp_obj = elements[j - 1];
  748. elements[j - 1] = elements[j];
  749. elements[j] = temp_obj;
  750. }
  751. }
  752. }
  753. }
  754. /******************************************************************************
  755. *
  756. * FUNCTION: acpi_ns_remove_element
  757. *
  758. * PARAMETERS: obj_desc - Package object element list
  759. * index - Index of element to remove
  760. *
  761. * RETURN: None
  762. *
  763. * DESCRIPTION: Remove the requested element of a package and delete it.
  764. *
  765. *****************************************************************************/
  766. static void
  767. acpi_ns_remove_element(union acpi_operand_object *obj_desc, u32 index)
  768. {
  769. union acpi_operand_object **source;
  770. union acpi_operand_object **dest;
  771. u32 count;
  772. u32 new_count;
  773. u32 i;
  774. ACPI_FUNCTION_NAME(ns_remove_element);
  775. count = obj_desc->package.count;
  776. new_count = count - 1;
  777. source = obj_desc->package.elements;
  778. dest = source;
  779. /* Examine all elements of the package object, remove matched index */
  780. for (i = 0; i < count; i++) {
  781. if (i == index) {
  782. acpi_ut_remove_reference(*source); /* Remove one ref for being in pkg */
  783. acpi_ut_remove_reference(*source);
  784. } else {
  785. *dest = *source;
  786. dest++;
  787. }
  788. source++;
  789. }
  790. /* NULL terminate list and update the package count */
  791. *dest = NULL;
  792. obj_desc->package.count = new_count;
  793. }