exconfig.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 - 2022, 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 *return_obj;
  78. union acpi_operand_object *ddb_handle;
  79. u32 table_index;
  80. ACPI_FUNCTION_TRACE(ex_load_table_op);
  81. /* Create the return object */
  82. return_obj = acpi_ut_create_integer_object((u64)0);
  83. if (!return_obj) {
  84. return_ACPI_STATUS(AE_NO_MEMORY);
  85. }
  86. *return_desc = return_obj;
  87. /* Find the ACPI table in the RSDT/XSDT */
  88. acpi_ex_exit_interpreter();
  89. status = acpi_tb_find_table(operand[0]->string.pointer,
  90. operand[1]->string.pointer,
  91. operand[2]->string.pointer, &table_index);
  92. acpi_ex_enter_interpreter();
  93. if (ACPI_FAILURE(status)) {
  94. if (status != AE_NOT_FOUND) {
  95. return_ACPI_STATUS(status);
  96. }
  97. /* Table not found, return an Integer=0 and AE_OK */
  98. return_ACPI_STATUS(AE_OK);
  99. }
  100. /* Default nodes */
  101. start_node = walk_state->scope_info->scope.node;
  102. parent_node = acpi_gbl_root_node;
  103. /* root_path (optional parameter) */
  104. if (operand[3]->string.length > 0) {
  105. /*
  106. * Find the node referenced by the root_path_string. This is the
  107. * location within the namespace where the table will be loaded.
  108. */
  109. status = acpi_ns_get_node_unlocked(start_node,
  110. operand[3]->string.pointer,
  111. ACPI_NS_SEARCH_PARENT,
  112. &parent_node);
  113. if (ACPI_FAILURE(status)) {
  114. return_ACPI_STATUS(status);
  115. }
  116. }
  117. /* parameter_path (optional parameter) */
  118. if (operand[4]->string.length > 0) {
  119. if ((operand[4]->string.pointer[0] != AML_ROOT_PREFIX) &&
  120. (operand[4]->string.pointer[0] != AML_PARENT_PREFIX)) {
  121. /*
  122. * Path is not absolute, so it will be relative to the node
  123. * referenced by the root_path_string (or the NS root if omitted)
  124. */
  125. start_node = parent_node;
  126. }
  127. /* Find the node referenced by the parameter_path_string */
  128. status = acpi_ns_get_node_unlocked(start_node,
  129. operand[4]->string.pointer,
  130. ACPI_NS_SEARCH_PARENT,
  131. &parameter_node);
  132. if (ACPI_FAILURE(status)) {
  133. return_ACPI_STATUS(status);
  134. }
  135. }
  136. /* Load the table into the namespace */
  137. ACPI_INFO(("Dynamic OEM Table Load:"));
  138. acpi_ex_exit_interpreter();
  139. status = acpi_tb_load_table(table_index, parent_node);
  140. acpi_ex_enter_interpreter();
  141. if (ACPI_FAILURE(status)) {
  142. return_ACPI_STATUS(status);
  143. }
  144. status = acpi_ex_add_table(table_index, &ddb_handle);
  145. if (ACPI_FAILURE(status)) {
  146. return_ACPI_STATUS(status);
  147. }
  148. /* Complete the initialization/resolution of new objects */
  149. acpi_ex_exit_interpreter();
  150. acpi_ns_initialize_objects();
  151. acpi_ex_enter_interpreter();
  152. /* Parameter Data (optional) */
  153. if (parameter_node) {
  154. /* Store the parameter data into the optional parameter object */
  155. status = acpi_ex_store(operand[5],
  156. ACPI_CAST_PTR(union acpi_operand_object,
  157. parameter_node),
  158. walk_state);
  159. if (ACPI_FAILURE(status)) {
  160. (void)acpi_ex_unload_table(ddb_handle);
  161. acpi_ut_remove_reference(ddb_handle);
  162. return_ACPI_STATUS(status);
  163. }
  164. }
  165. /* Remove the reference to ddb_handle created by acpi_ex_add_table above */
  166. acpi_ut_remove_reference(ddb_handle);
  167. /* Return -1 (non-zero) indicates success */
  168. return_obj->integer.value = 0xFFFFFFFFFFFFFFFF;
  169. return_ACPI_STATUS(status);
  170. }
  171. /*******************************************************************************
  172. *
  173. * FUNCTION: acpi_ex_region_read
  174. *
  175. * PARAMETERS: obj_desc - Region descriptor
  176. * length - Number of bytes to read
  177. * buffer - Pointer to where to put the data
  178. *
  179. * RETURN: Status
  180. *
  181. * DESCRIPTION: Read data from an operation region. The read starts from the
  182. * beginning of the region.
  183. *
  184. ******************************************************************************/
  185. static acpi_status
  186. acpi_ex_region_read(union acpi_operand_object *obj_desc, u32 length, u8 *buffer)
  187. {
  188. acpi_status status;
  189. u64 value;
  190. u32 region_offset = 0;
  191. u32 i;
  192. /* Bytewise reads */
  193. for (i = 0; i < length; i++) {
  194. status =
  195. acpi_ev_address_space_dispatch(obj_desc, NULL, ACPI_READ,
  196. region_offset, 8, &value);
  197. if (ACPI_FAILURE(status)) {
  198. return (status);
  199. }
  200. *buffer = (u8)value;
  201. buffer++;
  202. region_offset++;
  203. }
  204. return (AE_OK);
  205. }
  206. /*******************************************************************************
  207. *
  208. * FUNCTION: acpi_ex_load_op
  209. *
  210. * PARAMETERS: obj_desc - Region or Buffer/Field where the table will be
  211. * obtained
  212. * target - Where the status of the load will be stored
  213. * walk_state - Current state
  214. *
  215. * RETURN: Status
  216. *
  217. * DESCRIPTION: Load an ACPI table from a field or operation region
  218. *
  219. * NOTE: Region Fields (Field, bank_field, index_fields) are resolved to buffer
  220. * objects before this code is reached.
  221. *
  222. * If source is an operation region, it must refer to system_memory, as
  223. * per the ACPI specification.
  224. *
  225. ******************************************************************************/
  226. acpi_status
  227. acpi_ex_load_op(union acpi_operand_object *obj_desc,
  228. union acpi_operand_object *target,
  229. struct acpi_walk_state *walk_state)
  230. {
  231. union acpi_operand_object *ddb_handle;
  232. struct acpi_table_header *table_header;
  233. struct acpi_table_header *table;
  234. u32 table_index;
  235. acpi_status status;
  236. u32 length;
  237. ACPI_FUNCTION_TRACE(ex_load_op);
  238. if (target->common.descriptor_type == ACPI_DESC_TYPE_NAMED) {
  239. target =
  240. acpi_ns_get_attached_object(ACPI_CAST_PTR
  241. (struct acpi_namespace_node,
  242. target));
  243. }
  244. if (target->common.type != ACPI_TYPE_INTEGER) {
  245. ACPI_EXCEPTION((AE_INFO, AE_TYPE,
  246. "Type not integer: %X\n", target->common.type));
  247. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  248. }
  249. target->integer.value = 0;
  250. /* Source Object can be either an op_region or a Buffer/Field */
  251. switch (obj_desc->common.type) {
  252. case ACPI_TYPE_REGION:
  253. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  254. "Load table from Region %p\n", obj_desc));
  255. /* Region must be system_memory (from ACPI spec) */
  256. if (obj_desc->region.space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  257. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  258. }
  259. /*
  260. * If the Region Address and Length have not been previously
  261. * evaluated, evaluate them now and save the results.
  262. */
  263. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  264. status = acpi_ds_get_region_arguments(obj_desc);
  265. if (ACPI_FAILURE(status)) {
  266. return_ACPI_STATUS(status);
  267. }
  268. }
  269. /* Get the table header first so we can get the table length */
  270. table_header = ACPI_ALLOCATE(sizeof(struct acpi_table_header));
  271. if (!table_header) {
  272. return_ACPI_STATUS(AE_NO_MEMORY);
  273. }
  274. status =
  275. acpi_ex_region_read(obj_desc,
  276. sizeof(struct acpi_table_header),
  277. ACPI_CAST_PTR(u8, table_header));
  278. length = table_header->length;
  279. ACPI_FREE(table_header);
  280. if (ACPI_FAILURE(status)) {
  281. return_ACPI_STATUS(status);
  282. }
  283. /* Must have at least an ACPI table header */
  284. if (length < sizeof(struct acpi_table_header)) {
  285. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  286. }
  287. /*
  288. * The original implementation simply mapped the table, with no copy.
  289. * However, the memory region is not guaranteed to remain stable and
  290. * we must copy the table to a local buffer. For example, the memory
  291. * region is corrupted after suspend on some machines. Dynamically
  292. * loaded tables are usually small, so this overhead is minimal.
  293. *
  294. * The latest implementation (5/2009) does not use a mapping at all.
  295. * We use the low-level operation region interface to read the table
  296. * instead of the obvious optimization of using a direct mapping.
  297. * This maintains a consistent use of operation regions across the
  298. * entire subsystem. This is important if additional processing must
  299. * be performed in the (possibly user-installed) operation region
  300. * handler. For example, acpi_exec and ASLTS depend on this.
  301. */
  302. /* Allocate a buffer for the table */
  303. table = ACPI_ALLOCATE(length);
  304. if (!table) {
  305. return_ACPI_STATUS(AE_NO_MEMORY);
  306. }
  307. /* Read the entire table */
  308. status = acpi_ex_region_read(obj_desc, length,
  309. ACPI_CAST_PTR(u8, table));
  310. if (ACPI_FAILURE(status)) {
  311. ACPI_FREE(table);
  312. return_ACPI_STATUS(status);
  313. }
  314. break;
  315. case ACPI_TYPE_BUFFER: /* Buffer or resolved region_field */
  316. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  317. "Load table from Buffer or Field %p\n",
  318. obj_desc));
  319. /* Must have at least an ACPI table header */
  320. if (obj_desc->buffer.length < sizeof(struct acpi_table_header)) {
  321. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  322. }
  323. /* Get the actual table length from the table header */
  324. table_header =
  325. ACPI_CAST_PTR(struct acpi_table_header,
  326. obj_desc->buffer.pointer);
  327. length = table_header->length;
  328. /* Table cannot extend beyond the buffer */
  329. if (length > obj_desc->buffer.length) {
  330. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  331. }
  332. if (length < sizeof(struct acpi_table_header)) {
  333. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  334. }
  335. /*
  336. * Copy the table from the buffer because the buffer could be
  337. * modified or even deleted in the future
  338. */
  339. table = ACPI_ALLOCATE(length);
  340. if (!table) {
  341. return_ACPI_STATUS(AE_NO_MEMORY);
  342. }
  343. memcpy(table, table_header, length);
  344. break;
  345. default:
  346. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  347. }
  348. /* Install the new table into the local data structures */
  349. ACPI_INFO(("Dynamic OEM Table Load:"));
  350. acpi_ex_exit_interpreter();
  351. status = acpi_tb_install_and_load_table(ACPI_PTR_TO_PHYSADDR(table),
  352. ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
  353. table, TRUE, &table_index);
  354. acpi_ex_enter_interpreter();
  355. if (ACPI_FAILURE(status)) {
  356. /* Delete allocated table buffer */
  357. ACPI_FREE(table);
  358. return_ACPI_STATUS(status);
  359. }
  360. /*
  361. * Add the table to the namespace.
  362. *
  363. * Note: Load the table objects relative to the root of the namespace.
  364. * This appears to go against the ACPI specification, but we do it for
  365. * compatibility with other ACPI implementations.
  366. */
  367. status = acpi_ex_add_table(table_index, &ddb_handle);
  368. if (ACPI_FAILURE(status)) {
  369. return_ACPI_STATUS(status);
  370. }
  371. /* Complete the initialization/resolution of new objects */
  372. acpi_ex_exit_interpreter();
  373. acpi_ns_initialize_objects();
  374. acpi_ex_enter_interpreter();
  375. /* Remove the reference to ddb_handle created by acpi_ex_add_table above */
  376. acpi_ut_remove_reference(ddb_handle);
  377. /* Return -1 (non-zero) indicates success */
  378. target->integer.value = 0xFFFFFFFFFFFFFFFF;
  379. return_ACPI_STATUS(status);
  380. }
  381. /*******************************************************************************
  382. *
  383. * FUNCTION: acpi_ex_unload_table
  384. *
  385. * PARAMETERS: ddb_handle - Handle to a previously loaded table
  386. *
  387. * RETURN: Status
  388. *
  389. * DESCRIPTION: Unload an ACPI table
  390. *
  391. ******************************************************************************/
  392. acpi_status acpi_ex_unload_table(union acpi_operand_object *ddb_handle)
  393. {
  394. acpi_status status = AE_OK;
  395. union acpi_operand_object *table_desc = ddb_handle;
  396. u32 table_index;
  397. ACPI_FUNCTION_TRACE(ex_unload_table);
  398. /*
  399. * Temporarily emit a warning so that the ASL for the machine can be
  400. * hopefully obtained. This is to say that the Unload() operator is
  401. * extremely rare if not completely unused.
  402. */
  403. ACPI_WARNING((AE_INFO, "Received request to unload an ACPI table"));
  404. /*
  405. * May 2018: Unload is no longer supported for the following reasons:
  406. * 1) A correct implementation on some hosts may not be possible.
  407. * 2) Other ACPI implementations do not correctly/fully support it.
  408. * 3) It requires host device driver support which does not exist.
  409. * (To properly support namespace unload out from underneath.)
  410. * 4) This AML operator has never been seen in the field.
  411. */
  412. ACPI_EXCEPTION((AE_INFO, AE_NOT_IMPLEMENTED,
  413. "AML Unload operator is not supported"));
  414. /*
  415. * Validate the handle
  416. * Although the handle is partially validated in acpi_ex_reconfiguration()
  417. * when it calls acpi_ex_resolve_operands(), the handle is more completely
  418. * validated here.
  419. *
  420. * Handle must be a valid operand object of type reference. Also, the
  421. * ddb_handle must still be marked valid (table has not been previously
  422. * unloaded)
  423. */
  424. if ((!ddb_handle) ||
  425. (ACPI_GET_DESCRIPTOR_TYPE(ddb_handle) != ACPI_DESC_TYPE_OPERAND) ||
  426. (ddb_handle->common.type != ACPI_TYPE_LOCAL_REFERENCE) ||
  427. (!(ddb_handle->common.flags & AOPOBJ_DATA_VALID))) {
  428. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  429. }
  430. /* Get the table index from the ddb_handle */
  431. table_index = table_desc->reference.value;
  432. /*
  433. * Release the interpreter lock so that the table lock won't have
  434. * strict order requirement against it.
  435. */
  436. acpi_ex_exit_interpreter();
  437. status = acpi_tb_unload_table(table_index);
  438. acpi_ex_enter_interpreter();
  439. /*
  440. * Invalidate the handle. We do this because the handle may be stored
  441. * in a named object and may not be actually deleted until much later.
  442. */
  443. if (ACPI_SUCCESS(status)) {
  444. ddb_handle->common.flags &= ~AOPOBJ_DATA_VALID;
  445. }
  446. return_ACPI_STATUS(status);
  447. }