nsrepair2.c 20 KB

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