exconfig.c 15 KB

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