nsrepair2.c 18 KB

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