nsrepair2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /******************************************************************************
  2. *
  3. * Module Name: nsrepair2 - Repair for objects returned by specific
  4. * predefined methods
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2009, 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_predefined_data *data,
  54. union acpi_operand_object **return_object_ptr);
  55. typedef struct acpi_repair_info {
  56. char name[ACPI_NAME_SIZE];
  57. acpi_repair_function repair_function;
  58. } acpi_repair_info;
  59. /* Local prototypes */
  60. static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
  61. acpi_namespace_node
  62. *node);
  63. static acpi_status
  64. acpi_ns_repair_ALR(struct acpi_predefined_data *data,
  65. union acpi_operand_object **return_object_ptr);
  66. static acpi_status
  67. acpi_ns_repair_PSS(struct acpi_predefined_data *data,
  68. union acpi_operand_object **return_object_ptr);
  69. static acpi_status
  70. acpi_ns_repair_TSS(struct acpi_predefined_data *data,
  71. union acpi_operand_object **return_object_ptr);
  72. static acpi_status
  73. acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
  74. union acpi_operand_object *return_object,
  75. u32 expected_count,
  76. u32 sort_index,
  77. u8 sort_direction, char *sort_key_name);
  78. static acpi_status
  79. acpi_ns_remove_null_elements(union acpi_operand_object *package);
  80. static acpi_status
  81. acpi_ns_sort_list(union acpi_operand_object **elements,
  82. u32 count, u32 index, u8 sort_direction);
  83. /* Values for sort_direction above */
  84. #define ACPI_SORT_ASCENDING 0
  85. #define ACPI_SORT_DESCENDING 1
  86. /*
  87. * This table contains the names of the predefined methods for which we can
  88. * perform more complex repairs.
  89. *
  90. * _ALR: Sort the list ascending by ambient_illuminance if necessary
  91. * _PSS: Sort the list descending by Power if necessary
  92. * _TSS: Sort the list descending by Power if necessary
  93. */
  94. static const struct acpi_repair_info acpi_ns_repairable_names[] = {
  95. {"_ALR", acpi_ns_repair_ALR},
  96. {"_PSS", acpi_ns_repair_PSS},
  97. {"_TSS", acpi_ns_repair_TSS},
  98. {{0, 0, 0, 0}, NULL} /* Table terminator */
  99. };
  100. /******************************************************************************
  101. *
  102. * FUNCTION: acpi_ns_complex_repairs
  103. *
  104. * PARAMETERS: Data - Pointer to validation data structure
  105. * Node - Namespace node for the method/object
  106. * validate_status - Original status of earlier validation
  107. * return_object_ptr - Pointer to the object returned from the
  108. * evaluation of a method or object
  109. *
  110. * RETURN: Status. AE_OK if repair was successful. If name is not
  111. * matched, validate_status is returned.
  112. *
  113. * DESCRIPTION: Attempt to repair/convert a return object of a type that was
  114. * not expected.
  115. *
  116. *****************************************************************************/
  117. acpi_status
  118. acpi_ns_complex_repairs(struct acpi_predefined_data *data,
  119. struct acpi_namespace_node *node,
  120. acpi_status validate_status,
  121. union acpi_operand_object **return_object_ptr)
  122. {
  123. const struct acpi_repair_info *predefined;
  124. acpi_status status;
  125. /* Check if this name is in the list of repairable names */
  126. predefined = acpi_ns_match_repairable_name(node);
  127. if (!predefined) {
  128. return (validate_status);
  129. }
  130. status = predefined->repair_function(data, return_object_ptr);
  131. return (status);
  132. }
  133. /******************************************************************************
  134. *
  135. * FUNCTION: acpi_ns_match_repairable_name
  136. *
  137. * PARAMETERS: Node - Namespace node for the method/object
  138. *
  139. * RETURN: Pointer to entry in repair table. NULL indicates not found.
  140. *
  141. * DESCRIPTION: Check an object name against the repairable object list.
  142. *
  143. *****************************************************************************/
  144. static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
  145. acpi_namespace_node
  146. *node)
  147. {
  148. const struct acpi_repair_info *this_name;
  149. /* Search info table for a repairable predefined method/object name */
  150. this_name = acpi_ns_repairable_names;
  151. while (this_name->repair_function) {
  152. if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
  153. return (this_name);
  154. }
  155. this_name++;
  156. }
  157. return (NULL); /* Not found */
  158. }
  159. /******************************************************************************
  160. *
  161. * FUNCTION: acpi_ns_repair_ALR
  162. *
  163. * PARAMETERS: Data - Pointer to validation data structure
  164. * return_object_ptr - Pointer to the object returned from the
  165. * evaluation of a method or object
  166. *
  167. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  168. *
  169. * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
  170. * ascending by the ambient illuminance values.
  171. *
  172. *****************************************************************************/
  173. static acpi_status
  174. acpi_ns_repair_ALR(struct acpi_predefined_data *data,
  175. union acpi_operand_object **return_object_ptr)
  176. {
  177. union acpi_operand_object *return_object = *return_object_ptr;
  178. acpi_status status;
  179. status = acpi_ns_check_sorted_list(data, return_object, 2, 1,
  180. ACPI_SORT_ASCENDING,
  181. "AmbientIlluminance");
  182. return (status);
  183. }
  184. /******************************************************************************
  185. *
  186. * FUNCTION: acpi_ns_repair_TSS
  187. *
  188. * PARAMETERS: Data - Pointer to validation data structure
  189. * return_object_ptr - Pointer to the object returned from the
  190. * evaluation of a method or object
  191. *
  192. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  193. *
  194. * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
  195. * descending by the power dissipation values.
  196. *
  197. *****************************************************************************/
  198. static acpi_status
  199. acpi_ns_repair_TSS(struct acpi_predefined_data *data,
  200. union acpi_operand_object **return_object_ptr)
  201. {
  202. union acpi_operand_object *return_object = *return_object_ptr;
  203. acpi_status status;
  204. status = acpi_ns_check_sorted_list(data, return_object, 5, 1,
  205. ACPI_SORT_DESCENDING,
  206. "PowerDissipation");
  207. return (status);
  208. }
  209. /******************************************************************************
  210. *
  211. * FUNCTION: acpi_ns_repair_PSS
  212. *
  213. * PARAMETERS: Data - Pointer to validation data structure
  214. * return_object_ptr - Pointer to the object returned from the
  215. * evaluation of a method or object
  216. *
  217. * RETURN: Status. AE_OK if object is OK or was repaired successfully
  218. *
  219. * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
  220. * by the CPU frequencies. Check that the power dissipation values
  221. * are all proportional to CPU frequency (i.e., sorting by
  222. * frequency should be the same as sorting by power.)
  223. *
  224. *****************************************************************************/
  225. static acpi_status
  226. acpi_ns_repair_PSS(struct acpi_predefined_data *data,
  227. union acpi_operand_object **return_object_ptr)
  228. {
  229. union acpi_operand_object *return_object = *return_object_ptr;
  230. union acpi_operand_object **outer_elements;
  231. u32 outer_element_count;
  232. union acpi_operand_object **elements;
  233. union acpi_operand_object *obj_desc;
  234. u32 previous_value;
  235. acpi_status status;
  236. u32 i;
  237. /*
  238. * Entries (sub-packages) in the _PSS Package must be sorted by power
  239. * dissipation, in descending order. If it appears that the list is
  240. * incorrectly sorted, sort it. We sort by cpu_frequency, since this
  241. * should be proportional to the power.
  242. */
  243. status = acpi_ns_check_sorted_list(data, return_object, 6, 0,
  244. ACPI_SORT_DESCENDING,
  245. "CpuFrequency");
  246. if (ACPI_FAILURE(status)) {
  247. return (status);
  248. }
  249. /*
  250. * We now know the list is correctly sorted by CPU frequency. Check if
  251. * the power dissipation values are proportional.
  252. */
  253. previous_value = ACPI_UINT32_MAX;
  254. outer_elements = return_object->package.elements;
  255. outer_element_count = return_object->package.count;
  256. for (i = 0; i < outer_element_count; i++) {
  257. elements = (*outer_elements)->package.elements;
  258. obj_desc = elements[1]; /* Index1 = power_dissipation */
  259. if ((u32) obj_desc->integer.value > previous_value) {
  260. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
  261. data->node_flags,
  262. "SubPackage[%u,%u] - suspicious power dissipation values",
  263. i - 1, i));
  264. }
  265. previous_value = (u32) obj_desc->integer.value;
  266. outer_elements++;
  267. }
  268. return (AE_OK);
  269. }
  270. /******************************************************************************
  271. *
  272. * FUNCTION: acpi_ns_check_sorted_list
  273. *
  274. * PARAMETERS: Data - Pointer to validation data structure
  275. * return_object - Pointer to the top-level returned object
  276. * expected_count - Minimum length of each sub-package
  277. * sort_index - Sub-package entry to sort on
  278. * sort_direction - Ascending or descending
  279. * sort_key_name - Name of the sort_index field
  280. *
  281. * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
  282. * has been repaired by sorting the list.
  283. *
  284. * DESCRIPTION: Check if the package list is valid and sorted correctly by the
  285. * sort_index. If not, then sort the list.
  286. *
  287. *****************************************************************************/
  288. static acpi_status
  289. acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
  290. union acpi_operand_object *return_object,
  291. u32 expected_count,
  292. u32 sort_index,
  293. u8 sort_direction, char *sort_key_name)
  294. {
  295. u32 outer_element_count;
  296. union acpi_operand_object **outer_elements;
  297. union acpi_operand_object **elements;
  298. union acpi_operand_object *obj_desc;
  299. u32 i;
  300. u32 previous_value;
  301. acpi_status status;
  302. /* The top-level object must be a package */
  303. if (return_object->common.type != ACPI_TYPE_PACKAGE) {
  304. return (AE_AML_OPERAND_TYPE);
  305. }
  306. /*
  307. * Detect any NULL package elements and remove them from the
  308. * package.
  309. *
  310. * TBD: We may want to do this for all predefined names that
  311. * return a variable-length package of packages.
  312. */
  313. status = acpi_ns_remove_null_elements(return_object);
  314. if (status == AE_NULL_ENTRY) {
  315. ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  316. "NULL elements removed from package"));
  317. /* Exit if package is now zero length */
  318. if (!return_object->package.count) {
  319. return (AE_NULL_ENTRY);
  320. }
  321. }
  322. outer_elements = return_object->package.elements;
  323. outer_element_count = return_object->package.count;
  324. if (!outer_element_count) {
  325. return (AE_AML_PACKAGE_LIMIT);
  326. }
  327. previous_value = 0;
  328. if (sort_direction == ACPI_SORT_DESCENDING) {
  329. previous_value = ACPI_UINT32_MAX;
  330. }
  331. /* Examine each subpackage */
  332. for (i = 0; i < outer_element_count; i++) {
  333. /* Each element of the top-level package must also be a package */
  334. if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
  335. return (AE_AML_OPERAND_TYPE);
  336. }
  337. /* Each sub-package must have the minimum length */
  338. if ((*outer_elements)->package.count < expected_count) {
  339. return (AE_AML_PACKAGE_LIMIT);
  340. }
  341. elements = (*outer_elements)->package.elements;
  342. obj_desc = elements[sort_index];
  343. if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
  344. return (AE_AML_OPERAND_TYPE);
  345. }
  346. /*
  347. * The list must be sorted in the specified order. If we detect a
  348. * discrepancy, issue a warning and sort the entire list
  349. */
  350. if (((sort_direction == ACPI_SORT_ASCENDING) &&
  351. (obj_desc->integer.value < previous_value)) ||
  352. ((sort_direction == ACPI_SORT_DESCENDING) &&
  353. (obj_desc->integer.value > previous_value))) {
  354. status =
  355. acpi_ns_sort_list(return_object->package.elements,
  356. outer_element_count, sort_index,
  357. sort_direction);
  358. if (ACPI_FAILURE(status)) {
  359. return (status);
  360. }
  361. data->flags |= ACPI_OBJECT_REPAIRED;
  362. ACPI_INFO_PREDEFINED((AE_INFO, data->pathname,
  363. data->node_flags,
  364. "Repaired unsorted list - now sorted by %s",
  365. sort_key_name));
  366. return (AE_OK);
  367. }
  368. previous_value = (u32) obj_desc->integer.value;
  369. outer_elements++;
  370. }
  371. return (AE_OK);
  372. }
  373. /******************************************************************************
  374. *
  375. * FUNCTION: acpi_ns_remove_null_elements
  376. *
  377. * PARAMETERS: obj_desc - A Package object
  378. *
  379. * RETURN: Status. AE_NULL_ENTRY means that one or more elements were
  380. * removed.
  381. *
  382. * DESCRIPTION: Remove all NULL package elements and update the package count.
  383. *
  384. *****************************************************************************/
  385. static acpi_status
  386. acpi_ns_remove_null_elements(union acpi_operand_object *obj_desc)
  387. {
  388. union acpi_operand_object **source;
  389. union acpi_operand_object **dest;
  390. acpi_status status = AE_OK;
  391. u32 count;
  392. u32 new_count;
  393. u32 i;
  394. count = obj_desc->package.count;
  395. new_count = count;
  396. source = obj_desc->package.elements;
  397. dest = source;
  398. /* Examine all elements of the package object */
  399. for (i = 0; i < count; i++) {
  400. if (!*source) {
  401. status = AE_NULL_ENTRY;
  402. new_count--;
  403. } else {
  404. *dest = *source;
  405. dest++;
  406. }
  407. source++;
  408. }
  409. if (status == AE_NULL_ENTRY) {
  410. /* NULL terminate list and update the package count */
  411. *dest = NULL;
  412. obj_desc->package.count = new_count;
  413. }
  414. return (status);
  415. }
  416. /******************************************************************************
  417. *
  418. * FUNCTION: acpi_ns_sort_list
  419. *
  420. * PARAMETERS: Elements - Package object element list
  421. * Count - Element count for above
  422. * Index - Sort by which package element
  423. * sort_direction - Ascending or Descending sort
  424. *
  425. * RETURN: Status
  426. *
  427. * DESCRIPTION: Sort the objects that are in a package element list.
  428. *
  429. * NOTE: Assumes that all NULL elements have been removed from the package.
  430. *
  431. *****************************************************************************/
  432. static acpi_status
  433. acpi_ns_sort_list(union acpi_operand_object **elements,
  434. u32 count, u32 index, u8 sort_direction)
  435. {
  436. union acpi_operand_object *obj_desc1;
  437. union acpi_operand_object *obj_desc2;
  438. union acpi_operand_object *temp_obj;
  439. u32 i;
  440. u32 j;
  441. /* Simple bubble sort */
  442. for (i = 1; i < count; i++) {
  443. for (j = (count - 1); j >= i; j--) {
  444. obj_desc1 = elements[j - 1]->package.elements[index];
  445. obj_desc2 = elements[j]->package.elements[index];
  446. if (((sort_direction == ACPI_SORT_ASCENDING) &&
  447. (obj_desc1->integer.value >
  448. obj_desc2->integer.value))
  449. || ((sort_direction == ACPI_SORT_DESCENDING)
  450. && (obj_desc1->integer.value <
  451. obj_desc2->integer.value))) {
  452. temp_obj = elements[j - 1];
  453. elements[j - 1] = elements[j];
  454. elements[j] = temp_obj;
  455. }
  456. }
  457. }
  458. return (AE_OK);
  459. }