exconfig.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exconfig - Namespace reconfiguration (Load/Unload opcodes)
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acinterp.h"
  12. #include "acnamesp.h"
  13. #include "actables.h"
  14. #include "acdispat.h"
  15. #include "acevents.h"
  16. #include "amlcode.h"
  17. #define _COMPONENT ACPI_EXECUTER
  18. ACPI_MODULE_NAME("exconfig")
  19. /* Local prototypes */
  20. static acpi_status
  21. acpi_ex_add_table(u32 table_index, union acpi_operand_object **ddb_handle);
  22. static acpi_status
  23. acpi_ex_region_read(union acpi_operand_object *obj_desc,
  24. u32 length, u8 *buffer);
  25. /*******************************************************************************
  26. *
  27. * FUNCTION: acpi_ex_add_table
  28. *
  29. * PARAMETERS: table - Pointer to raw table
  30. * parent_node - Where to load the table (scope)
  31. * ddb_handle - Where to return the table handle.
  32. *
  33. * RETURN: Status
  34. *
  35. * DESCRIPTION: Common function to Install and Load an ACPI table with a
  36. * returned table handle.
  37. *
  38. ******************************************************************************/
  39. static acpi_status
  40. acpi_ex_add_table(u32 table_index, union acpi_operand_object **ddb_handle)
  41. {
  42. union acpi_operand_object *obj_desc;
  43. ACPI_FUNCTION_TRACE(ex_add_table);
  44. /* Create an object to be the table handle */
  45. obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
  46. if (!obj_desc) {
  47. return_ACPI_STATUS(AE_NO_MEMORY);
  48. }
  49. /* Init the table handle */
  50. obj_desc->common.flags |= AOPOBJ_DATA_VALID;
  51. obj_desc->reference.class = ACPI_REFCLASS_TABLE;
  52. obj_desc->reference.value = table_index;
  53. *ddb_handle = obj_desc;
  54. return_ACPI_STATUS(AE_OK);
  55. }
  56. /*******************************************************************************
  57. *
  58. * FUNCTION: acpi_ex_load_table_op
  59. *
  60. * PARAMETERS: walk_state - Current state with operands
  61. * return_desc - Where to store the return object
  62. *
  63. * RETURN: Status
  64. *
  65. * DESCRIPTION: Load an ACPI table from the RSDT/XSDT
  66. *
  67. ******************************************************************************/
  68. acpi_status
  69. acpi_ex_load_table_op(struct acpi_walk_state *walk_state,
  70. union acpi_operand_object **return_desc)
  71. {
  72. acpi_status status;
  73. union acpi_operand_object **operand = &walk_state->operands[0];
  74. struct acpi_namespace_node *parent_node;
  75. struct acpi_namespace_node *start_node;
  76. struct acpi_namespace_node *parameter_node = NULL;
  77. union acpi_operand_object *ddb_handle;
  78. u32 table_index;
  79. ACPI_FUNCTION_TRACE(ex_load_table_op);
  80. /* Find the ACPI table in the RSDT/XSDT */
  81. acpi_ex_exit_interpreter();
  82. status = acpi_tb_find_table(operand[0]->string.pointer,
  83. operand[1]->string.pointer,
  84. operand[2]->string.pointer, &table_index);
  85. acpi_ex_enter_interpreter();
  86. if (ACPI_FAILURE(status)) {
  87. if (status != AE_NOT_FOUND) {
  88. return_ACPI_STATUS(status);
  89. }
  90. /* Table not found, return an Integer=0 and AE_OK */
  91. ddb_handle = acpi_ut_create_integer_object((u64) 0);
  92. if (!ddb_handle) {
  93. return_ACPI_STATUS(AE_NO_MEMORY);
  94. }
  95. *return_desc = ddb_handle;
  96. return_ACPI_STATUS(AE_OK);
  97. }
  98. /* Default nodes */
  99. start_node = walk_state->scope_info->scope.node;
  100. parent_node = acpi_gbl_root_node;
  101. /* root_path (optional parameter) */
  102. if (operand[3]->string.length > 0) {
  103. /*
  104. * Find the node referenced by the root_path_string. This is the
  105. * location within the namespace where the table will be loaded.
  106. */
  107. status = acpi_ns_get_node_unlocked(start_node,
  108. operand[3]->string.pointer,
  109. ACPI_NS_SEARCH_PARENT,
  110. &parent_node);
  111. if (ACPI_FAILURE(status)) {
  112. return_ACPI_STATUS(status);
  113. }
  114. }
  115. /* parameter_path (optional parameter) */
  116. if (operand[4]->string.length > 0) {
  117. if ((operand[4]->string.pointer[0] != AML_ROOT_PREFIX) &&
  118. (operand[4]->string.pointer[0] != AML_PARENT_PREFIX)) {
  119. /*
  120. * Path is not absolute, so it will be relative to the node
  121. * referenced by the root_path_string (or the NS root if omitted)
  122. */
  123. start_node = parent_node;
  124. }
  125. /* Find the node referenced by the parameter_path_string */
  126. status = acpi_ns_get_node_unlocked(start_node,
  127. operand[4]->string.pointer,
  128. ACPI_NS_SEARCH_PARENT,
  129. &parameter_node);
  130. if (ACPI_FAILURE(status)) {
  131. return_ACPI_STATUS(status);
  132. }
  133. }
  134. /* Load the table into the namespace */
  135. ACPI_INFO(("Dynamic OEM Table Load:"));
  136. acpi_ex_exit_interpreter();
  137. status = acpi_tb_load_table(table_index, parent_node);
  138. acpi_ex_enter_interpreter();
  139. if (ACPI_FAILURE(status)) {
  140. return_ACPI_STATUS(status);
  141. }
  142. status = acpi_ex_add_table(table_index, &ddb_handle);
  143. if (ACPI_FAILURE(status)) {
  144. return_ACPI_STATUS(status);
  145. }
  146. /* Parameter Data (optional) */
  147. if (parameter_node) {
  148. /* Store the parameter data into the optional parameter object */
  149. status = acpi_ex_store(operand[5],
  150. ACPI_CAST_PTR(union acpi_operand_object,
  151. parameter_node),
  152. walk_state);
  153. if (ACPI_FAILURE(status)) {
  154. (void)acpi_ex_unload_table(ddb_handle);
  155. acpi_ut_remove_reference(ddb_handle);
  156. return_ACPI_STATUS(status);
  157. }
  158. }
  159. *return_desc = ddb_handle;
  160. return_ACPI_STATUS(status);
  161. }
  162. /*******************************************************************************
  163. *
  164. * FUNCTION: acpi_ex_region_read
  165. *
  166. * PARAMETERS: obj_desc - Region descriptor
  167. * length - Number of bytes to read
  168. * buffer - Pointer to where to put the data
  169. *
  170. * RETURN: Status
  171. *
  172. * DESCRIPTION: Read data from an operation region. The read starts from the
  173. * beginning of the region.
  174. *
  175. ******************************************************************************/
  176. static acpi_status
  177. acpi_ex_region_read(union acpi_operand_object *obj_desc, u32 length, u8 *buffer)
  178. {
  179. acpi_status status;
  180. u64 value;
  181. u32 region_offset = 0;
  182. u32 i;
  183. /* Bytewise reads */
  184. for (i = 0; i < length; i++) {
  185. status =
  186. acpi_ev_address_space_dispatch(obj_desc, NULL, ACPI_READ,
  187. region_offset, 8, &value);
  188. if (ACPI_FAILURE(status)) {
  189. return (status);
  190. }
  191. *buffer = (u8)value;
  192. buffer++;
  193. region_offset++;
  194. }
  195. return (AE_OK);
  196. }
  197. /*******************************************************************************
  198. *
  199. * FUNCTION: acpi_ex_load_op
  200. *
  201. * PARAMETERS: obj_desc - Region or Buffer/Field where the table will be
  202. * obtained
  203. * target - Where a handle to the table will be stored
  204. * walk_state - Current state
  205. *
  206. * RETURN: Status
  207. *
  208. * DESCRIPTION: Load an ACPI table from a field or operation region
  209. *
  210. * NOTE: Region Fields (Field, bank_field, index_fields) are resolved to buffer
  211. * objects before this code is reached.
  212. *
  213. * If source is an operation region, it must refer to system_memory, as
  214. * per the ACPI specification.
  215. *
  216. ******************************************************************************/
  217. acpi_status
  218. acpi_ex_load_op(union acpi_operand_object *obj_desc,
  219. union acpi_operand_object *target,
  220. struct acpi_walk_state *walk_state)
  221. {
  222. union acpi_operand_object *ddb_handle;
  223. struct acpi_table_header *table_header;
  224. struct acpi_table_header *table;
  225. u32 table_index;
  226. acpi_status status;
  227. u32 length;
  228. ACPI_FUNCTION_TRACE(ex_load_op);
  229. /* Source Object can be either an op_region or a Buffer/Field */
  230. switch (obj_desc->common.type) {
  231. case ACPI_TYPE_REGION:
  232. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  233. "Load table from Region %p\n", obj_desc));
  234. /* Region must be system_memory (from ACPI spec) */
  235. if (obj_desc->region.space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  236. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  237. }
  238. /*
  239. * If the Region Address and Length have not been previously
  240. * evaluated, evaluate them now and save the results.
  241. */
  242. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  243. status = acpi_ds_get_region_arguments(obj_desc);
  244. if (ACPI_FAILURE(status)) {
  245. return_ACPI_STATUS(status);
  246. }
  247. }
  248. /* Get the table header first so we can get the table length */
  249. table_header = ACPI_ALLOCATE(sizeof(struct acpi_table_header));
  250. if (!table_header) {
  251. return_ACPI_STATUS(AE_NO_MEMORY);
  252. }
  253. status =
  254. acpi_ex_region_read(obj_desc,
  255. sizeof(struct acpi_table_header),
  256. ACPI_CAST_PTR(u8, table_header));
  257. length = table_header->length;
  258. ACPI_FREE(table_header);
  259. if (ACPI_FAILURE(status)) {
  260. return_ACPI_STATUS(status);
  261. }
  262. /* Must have at least an ACPI table header */
  263. if (length < sizeof(struct acpi_table_header)) {
  264. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  265. }
  266. /*
  267. * The original implementation simply mapped the table, with no copy.
  268. * However, the memory region is not guaranteed to remain stable and
  269. * we must copy the table to a local buffer. For example, the memory
  270. * region is corrupted after suspend on some machines. Dynamically
  271. * loaded tables are usually small, so this overhead is minimal.
  272. *
  273. * The latest implementation (5/2009) does not use a mapping at all.
  274. * We use the low-level operation region interface to read the table
  275. * instead of the obvious optimization of using a direct mapping.
  276. * This maintains a consistent use of operation regions across the
  277. * entire subsystem. This is important if additional processing must
  278. * be performed in the (possibly user-installed) operation region
  279. * handler. For example, acpi_exec and ASLTS depend on this.
  280. */
  281. /* Allocate a buffer for the table */
  282. table = ACPI_ALLOCATE(length);
  283. if (!table) {
  284. return_ACPI_STATUS(AE_NO_MEMORY);
  285. }
  286. /* Read the entire table */
  287. status = acpi_ex_region_read(obj_desc, length,
  288. ACPI_CAST_PTR(u8, table));
  289. if (ACPI_FAILURE(status)) {
  290. ACPI_FREE(table);
  291. return_ACPI_STATUS(status);
  292. }
  293. break;
  294. case ACPI_TYPE_BUFFER: /* Buffer or resolved region_field */
  295. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  296. "Load table from Buffer or Field %p\n",
  297. obj_desc));
  298. /* Must have at least an ACPI table header */
  299. if (obj_desc->buffer.length < sizeof(struct acpi_table_header)) {
  300. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  301. }
  302. /* Get the actual table length from the table header */
  303. table_header =
  304. ACPI_CAST_PTR(struct acpi_table_header,
  305. obj_desc->buffer.pointer);
  306. length = table_header->length;
  307. /* Table cannot extend beyond the buffer */
  308. if (length > obj_desc->buffer.length) {
  309. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  310. }
  311. if (length < sizeof(struct acpi_table_header)) {
  312. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  313. }
  314. /*
  315. * Copy the table from the buffer because the buffer could be
  316. * modified or even deleted in the future
  317. */
  318. table = ACPI_ALLOCATE(length);
  319. if (!table) {
  320. return_ACPI_STATUS(AE_NO_MEMORY);
  321. }
  322. memcpy(table, table_header, length);
  323. break;
  324. default:
  325. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  326. }
  327. /* Install the new table into the local data structures */
  328. ACPI_INFO(("Dynamic OEM Table Load:"));
  329. acpi_ex_exit_interpreter();
  330. status = acpi_tb_install_and_load_table(ACPI_PTR_TO_PHYSADDR(table),
  331. ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
  332. TRUE, &table_index);
  333. acpi_ex_enter_interpreter();
  334. if (ACPI_FAILURE(status)) {
  335. /* Delete allocated table buffer */
  336. ACPI_FREE(table);
  337. return_ACPI_STATUS(status);
  338. }
  339. /*
  340. * Add the table to the namespace.
  341. *
  342. * Note: Load the table objects relative to the root of the namespace.
  343. * This appears to go against the ACPI specification, but we do it for
  344. * compatibility with other ACPI implementations.
  345. */
  346. status = acpi_ex_add_table(table_index, &ddb_handle);
  347. if (ACPI_FAILURE(status)) {
  348. /* On error, table_ptr was deallocated above */
  349. return_ACPI_STATUS(status);
  350. }
  351. /* Store the ddb_handle into the Target operand */
  352. status = acpi_ex_store(ddb_handle, target, walk_state);
  353. if (ACPI_FAILURE(status)) {
  354. (void)acpi_ex_unload_table(ddb_handle);
  355. /* table_ptr was deallocated above */
  356. acpi_ut_remove_reference(ddb_handle);
  357. return_ACPI_STATUS(status);
  358. }
  359. /* Remove the reference by added by acpi_ex_store above */
  360. acpi_ut_remove_reference(ddb_handle);
  361. return_ACPI_STATUS(status);
  362. }
  363. /*******************************************************************************
  364. *
  365. * FUNCTION: acpi_ex_unload_table
  366. *
  367. * PARAMETERS: ddb_handle - Handle to a previously loaded table
  368. *
  369. * RETURN: Status
  370. *
  371. * DESCRIPTION: Unload an ACPI table
  372. *
  373. ******************************************************************************/
  374. acpi_status acpi_ex_unload_table(union acpi_operand_object *ddb_handle)
  375. {
  376. acpi_status status = AE_OK;
  377. union acpi_operand_object *table_desc = ddb_handle;
  378. u32 table_index;
  379. ACPI_FUNCTION_TRACE(ex_unload_table);
  380. /*
  381. * Temporarily emit a warning so that the ASL for the machine can be
  382. * hopefully obtained. This is to say that the Unload() operator is
  383. * extremely rare if not completely unused.
  384. */
  385. ACPI_WARNING((AE_INFO, "Received request to unload an ACPI table"));
  386. /*
  387. * Validate the handle
  388. * Although the handle is partially validated in acpi_ex_reconfiguration()
  389. * when it calls acpi_ex_resolve_operands(), the handle is more completely
  390. * validated here.
  391. *
  392. * Handle must be a valid operand object of type reference. Also, the
  393. * ddb_handle must still be marked valid (table has not been previously
  394. * unloaded)
  395. */
  396. if ((!ddb_handle) ||
  397. (ACPI_GET_DESCRIPTOR_TYPE(ddb_handle) != ACPI_DESC_TYPE_OPERAND) ||
  398. (ddb_handle->common.type != ACPI_TYPE_LOCAL_REFERENCE) ||
  399. (!(ddb_handle->common.flags & AOPOBJ_DATA_VALID))) {
  400. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  401. }
  402. /* Get the table index from the ddb_handle */
  403. table_index = table_desc->reference.value;
  404. /*
  405. * Release the interpreter lock so that the table lock won't have
  406. * strict order requirement against it.
  407. */
  408. acpi_ex_exit_interpreter();
  409. status = acpi_tb_unload_table(table_index);
  410. acpi_ex_enter_interpreter();
  411. /*
  412. * Invalidate the handle. We do this because the handle may be stored
  413. * in a named object and may not be actually deleted until much later.
  414. */
  415. if (ACPI_SUCCESS(status)) {
  416. ddb_handle->common.flags &= ~AOPOBJ_DATA_VALID;
  417. }
  418. return_ACPI_STATUS(status);
  419. }