nsrepair2.c 18 KB

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