nsrepair2.c 26 KB

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