exconfig.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /******************************************************************************
  2. *
  3. * Module Name: exconfig - Namespace reconfiguration (Load/Unload opcodes)
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acinterp.h"
  45. #include "acnamesp.h"
  46. #include "actables.h"
  47. #include "acdispat.h"
  48. #include "acevents.h"
  49. #include "amlcode.h"
  50. #define _COMPONENT ACPI_EXECUTER
  51. ACPI_MODULE_NAME("exconfig")
  52. /* Local prototypes */
  53. static acpi_status
  54. acpi_ex_add_table(u32 table_index,
  55. struct acpi_namespace_node *parent_node,
  56. union acpi_operand_object **ddb_handle);
  57. static acpi_status
  58. acpi_ex_region_read(union acpi_operand_object *obj_desc,
  59. u32 length, u8 *buffer);
  60. /*******************************************************************************
  61. *
  62. * FUNCTION: acpi_ex_add_table
  63. *
  64. * PARAMETERS: table - Pointer to raw table
  65. * parent_node - Where to load the table (scope)
  66. * ddb_handle - Where to return the table handle.
  67. *
  68. * RETURN: Status
  69. *
  70. * DESCRIPTION: Common function to Install and Load an ACPI table with a
  71. * returned table handle.
  72. *
  73. ******************************************************************************/
  74. static acpi_status
  75. acpi_ex_add_table(u32 table_index,
  76. struct acpi_namespace_node *parent_node,
  77. union acpi_operand_object **ddb_handle)
  78. {
  79. union acpi_operand_object *obj_desc;
  80. acpi_status status;
  81. acpi_owner_id owner_id;
  82. ACPI_FUNCTION_TRACE(ex_add_table);
  83. /* Create an object to be the table handle */
  84. obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
  85. if (!obj_desc) {
  86. return_ACPI_STATUS(AE_NO_MEMORY);
  87. }
  88. /* Init the table handle */
  89. obj_desc->common.flags |= AOPOBJ_DATA_VALID;
  90. obj_desc->reference.class = ACPI_REFCLASS_TABLE;
  91. *ddb_handle = obj_desc;
  92. /* Install the new table into the local data structures */
  93. obj_desc->reference.value = table_index;
  94. /* Add the table to the namespace */
  95. status = acpi_ns_load_table(table_index, parent_node);
  96. if (ACPI_FAILURE(status)) {
  97. acpi_ut_remove_reference(obj_desc);
  98. *ddb_handle = NULL;
  99. return_ACPI_STATUS(status);
  100. }
  101. /* Execute any module-level code that was found in the table */
  102. acpi_ex_exit_interpreter();
  103. if (acpi_gbl_group_module_level_code) {
  104. acpi_ns_exec_module_code_list();
  105. }
  106. acpi_ex_enter_interpreter();
  107. /*
  108. * Update GPEs for any new _Lxx/_Exx methods. Ignore errors. The host is
  109. * responsible for discovering any new wake GPEs by running _PRW methods
  110. * that may have been loaded by this table.
  111. */
  112. status = acpi_tb_get_owner_id(table_index, &owner_id);
  113. if (ACPI_SUCCESS(status)) {
  114. acpi_ev_update_gpes(owner_id);
  115. }
  116. return_ACPI_STATUS(AE_OK);
  117. }
  118. /*******************************************************************************
  119. *
  120. * FUNCTION: acpi_ex_load_table_op
  121. *
  122. * PARAMETERS: walk_state - Current state with operands
  123. * return_desc - Where to store the return object
  124. *
  125. * RETURN: Status
  126. *
  127. * DESCRIPTION: Load an ACPI table from the RSDT/XSDT
  128. *
  129. ******************************************************************************/
  130. acpi_status
  131. acpi_ex_load_table_op(struct acpi_walk_state *walk_state,
  132. union acpi_operand_object **return_desc)
  133. {
  134. acpi_status status;
  135. union acpi_operand_object **operand = &walk_state->operands[0];
  136. struct acpi_namespace_node *parent_node;
  137. struct acpi_namespace_node *start_node;
  138. struct acpi_namespace_node *parameter_node = NULL;
  139. union acpi_operand_object *ddb_handle;
  140. struct acpi_table_header *table;
  141. u32 table_index;
  142. ACPI_FUNCTION_TRACE(ex_load_table_op);
  143. /* Find the ACPI table in the RSDT/XSDT */
  144. status = acpi_tb_find_table(operand[0]->string.pointer,
  145. operand[1]->string.pointer,
  146. operand[2]->string.pointer, &table_index);
  147. if (ACPI_FAILURE(status)) {
  148. if (status != AE_NOT_FOUND) {
  149. return_ACPI_STATUS(status);
  150. }
  151. /* Table not found, return an Integer=0 and AE_OK */
  152. ddb_handle = acpi_ut_create_integer_object((u64) 0);
  153. if (!ddb_handle) {
  154. return_ACPI_STATUS(AE_NO_MEMORY);
  155. }
  156. *return_desc = ddb_handle;
  157. return_ACPI_STATUS(AE_OK);
  158. }
  159. /* Default nodes */
  160. start_node = walk_state->scope_info->scope.node;
  161. parent_node = acpi_gbl_root_node;
  162. /* root_path (optional parameter) */
  163. if (operand[3]->string.length > 0) {
  164. /*
  165. * Find the node referenced by the root_path_string. This is the
  166. * location within the namespace where the table will be loaded.
  167. */
  168. status =
  169. acpi_ns_get_node(start_node, operand[3]->string.pointer,
  170. ACPI_NS_SEARCH_PARENT, &parent_node);
  171. if (ACPI_FAILURE(status)) {
  172. return_ACPI_STATUS(status);
  173. }
  174. }
  175. /* parameter_path (optional parameter) */
  176. if (operand[4]->string.length > 0) {
  177. if ((operand[4]->string.pointer[0] != AML_ROOT_PREFIX) &&
  178. (operand[4]->string.pointer[0] != AML_PARENT_PREFIX)) {
  179. /*
  180. * Path is not absolute, so it will be relative to the node
  181. * referenced by the root_path_string (or the NS root if omitted)
  182. */
  183. start_node = parent_node;
  184. }
  185. /* Find the node referenced by the parameter_path_string */
  186. status =
  187. acpi_ns_get_node(start_node, operand[4]->string.pointer,
  188. ACPI_NS_SEARCH_PARENT, &parameter_node);
  189. if (ACPI_FAILURE(status)) {
  190. return_ACPI_STATUS(status);
  191. }
  192. }
  193. /* Load the table into the namespace */
  194. status = acpi_ex_add_table(table_index, parent_node, &ddb_handle);
  195. if (ACPI_FAILURE(status)) {
  196. return_ACPI_STATUS(status);
  197. }
  198. /* Parameter Data (optional) */
  199. if (parameter_node) {
  200. /* Store the parameter data into the optional parameter object */
  201. status = acpi_ex_store(operand[5],
  202. ACPI_CAST_PTR(union acpi_operand_object,
  203. parameter_node),
  204. walk_state);
  205. if (ACPI_FAILURE(status)) {
  206. (void)acpi_ex_unload_table(ddb_handle);
  207. acpi_ut_remove_reference(ddb_handle);
  208. return_ACPI_STATUS(status);
  209. }
  210. }
  211. status = acpi_get_table_by_index(table_index, &table);
  212. if (ACPI_SUCCESS(status)) {
  213. ACPI_INFO(("Dynamic OEM Table Load:"));
  214. acpi_tb_print_table_header(0, table);
  215. }
  216. /* Invoke table handler if present */
  217. if (acpi_gbl_table_handler) {
  218. (void)acpi_gbl_table_handler(ACPI_TABLE_EVENT_LOAD, table,
  219. acpi_gbl_table_handler_context);
  220. }
  221. *return_desc = ddb_handle;
  222. return_ACPI_STATUS(status);
  223. }
  224. /*******************************************************************************
  225. *
  226. * FUNCTION: acpi_ex_region_read
  227. *
  228. * PARAMETERS: obj_desc - Region descriptor
  229. * length - Number of bytes to read
  230. * buffer - Pointer to where to put the data
  231. *
  232. * RETURN: Status
  233. *
  234. * DESCRIPTION: Read data from an operation region. The read starts from the
  235. * beginning of the region.
  236. *
  237. ******************************************************************************/
  238. static acpi_status
  239. acpi_ex_region_read(union acpi_operand_object *obj_desc, u32 length, u8 *buffer)
  240. {
  241. acpi_status status;
  242. u64 value;
  243. u32 region_offset = 0;
  244. u32 i;
  245. /* Bytewise reads */
  246. for (i = 0; i < length; i++) {
  247. status =
  248. acpi_ev_address_space_dispatch(obj_desc, NULL, ACPI_READ,
  249. region_offset, 8, &value);
  250. if (ACPI_FAILURE(status)) {
  251. return (status);
  252. }
  253. *buffer = (u8)value;
  254. buffer++;
  255. region_offset++;
  256. }
  257. return (AE_OK);
  258. }
  259. /*******************************************************************************
  260. *
  261. * FUNCTION: acpi_ex_load_op
  262. *
  263. * PARAMETERS: obj_desc - Region or Buffer/Field where the table will be
  264. * obtained
  265. * target - Where a handle to the table will be stored
  266. * walk_state - Current state
  267. *
  268. * RETURN: Status
  269. *
  270. * DESCRIPTION: Load an ACPI table from a field or operation region
  271. *
  272. * NOTE: Region Fields (Field, bank_field, index_fields) are resolved to buffer
  273. * objects before this code is reached.
  274. *
  275. * If source is an operation region, it must refer to system_memory, as
  276. * per the ACPI specification.
  277. *
  278. ******************************************************************************/
  279. acpi_status
  280. acpi_ex_load_op(union acpi_operand_object *obj_desc,
  281. union acpi_operand_object *target,
  282. struct acpi_walk_state *walk_state)
  283. {
  284. union acpi_operand_object *ddb_handle;
  285. struct acpi_table_header *table_header;
  286. struct acpi_table_header *table;
  287. u32 table_index;
  288. acpi_status status;
  289. u32 length;
  290. ACPI_FUNCTION_TRACE(ex_load_op);
  291. /* Source Object can be either an op_region or a Buffer/Field */
  292. switch (obj_desc->common.type) {
  293. case ACPI_TYPE_REGION:
  294. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  295. "Load table from Region %p\n", obj_desc));
  296. /* Region must be system_memory (from ACPI spec) */
  297. if (obj_desc->region.space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  298. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  299. }
  300. /*
  301. * If the Region Address and Length have not been previously
  302. * evaluated, evaluate them now and save the results.
  303. */
  304. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  305. status = acpi_ds_get_region_arguments(obj_desc);
  306. if (ACPI_FAILURE(status)) {
  307. return_ACPI_STATUS(status);
  308. }
  309. }
  310. /* Get the table header first so we can get the table length */
  311. table_header = ACPI_ALLOCATE(sizeof(struct acpi_table_header));
  312. if (!table_header) {
  313. return_ACPI_STATUS(AE_NO_MEMORY);
  314. }
  315. status =
  316. acpi_ex_region_read(obj_desc,
  317. sizeof(struct acpi_table_header),
  318. ACPI_CAST_PTR(u8, table_header));
  319. length = table_header->length;
  320. ACPI_FREE(table_header);
  321. if (ACPI_FAILURE(status)) {
  322. return_ACPI_STATUS(status);
  323. }
  324. /* Must have at least an ACPI table header */
  325. if (length < sizeof(struct acpi_table_header)) {
  326. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  327. }
  328. /*
  329. * The original implementation simply mapped the table, with no copy.
  330. * However, the memory region is not guaranteed to remain stable and
  331. * we must copy the table to a local buffer. For example, the memory
  332. * region is corrupted after suspend on some machines. Dynamically
  333. * loaded tables are usually small, so this overhead is minimal.
  334. *
  335. * The latest implementation (5/2009) does not use a mapping at all.
  336. * We use the low-level operation region interface to read the table
  337. * instead of the obvious optimization of using a direct mapping.
  338. * This maintains a consistent use of operation regions across the
  339. * entire subsystem. This is important if additional processing must
  340. * be performed in the (possibly user-installed) operation region
  341. * handler. For example, acpi_exec and ASLTS depend on this.
  342. */
  343. /* Allocate a buffer for the table */
  344. table = ACPI_ALLOCATE(length);
  345. if (!table) {
  346. return_ACPI_STATUS(AE_NO_MEMORY);
  347. }
  348. /* Read the entire table */
  349. status = acpi_ex_region_read(obj_desc, length,
  350. ACPI_CAST_PTR(u8, table));
  351. if (ACPI_FAILURE(status)) {
  352. ACPI_FREE(table);
  353. return_ACPI_STATUS(status);
  354. }
  355. break;
  356. case ACPI_TYPE_BUFFER: /* Buffer or resolved region_field */
  357. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  358. "Load table from Buffer or Field %p\n",
  359. obj_desc));
  360. /* Must have at least an ACPI table header */
  361. if (obj_desc->buffer.length < sizeof(struct acpi_table_header)) {
  362. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  363. }
  364. /* Get the actual table length from the table header */
  365. table_header =
  366. ACPI_CAST_PTR(struct acpi_table_header,
  367. obj_desc->buffer.pointer);
  368. length = table_header->length;
  369. /* Table cannot extend beyond the buffer */
  370. if (length > obj_desc->buffer.length) {
  371. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  372. }
  373. if (length < sizeof(struct acpi_table_header)) {
  374. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  375. }
  376. /*
  377. * Copy the table from the buffer because the buffer could be
  378. * modified or even deleted in the future
  379. */
  380. table = ACPI_ALLOCATE(length);
  381. if (!table) {
  382. return_ACPI_STATUS(AE_NO_MEMORY);
  383. }
  384. memcpy(table, table_header, length);
  385. break;
  386. default:
  387. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  388. }
  389. /* Install the new table into the local data structures */
  390. ACPI_INFO(("Dynamic OEM Table Load:"));
  391. (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
  392. status = acpi_tb_install_standard_table(ACPI_PTR_TO_PHYSADDR(table),
  393. ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
  394. TRUE, TRUE, &table_index);
  395. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  396. if (ACPI_FAILURE(status)) {
  397. /* Delete allocated table buffer */
  398. ACPI_FREE(table);
  399. return_ACPI_STATUS(status);
  400. }
  401. /*
  402. * Note: Now table is "INSTALLED", it must be validated before
  403. * loading.
  404. */
  405. status =
  406. acpi_tb_validate_table(&acpi_gbl_root_table_list.
  407. tables[table_index]);
  408. if (ACPI_FAILURE(status)) {
  409. return_ACPI_STATUS(status);
  410. }
  411. /*
  412. * Add the table to the namespace.
  413. *
  414. * Note: Load the table objects relative to the root of the namespace.
  415. * This appears to go against the ACPI specification, but we do it for
  416. * compatibility with other ACPI implementations.
  417. */
  418. status =
  419. acpi_ex_add_table(table_index, acpi_gbl_root_node, &ddb_handle);
  420. if (ACPI_FAILURE(status)) {
  421. /* On error, table_ptr was deallocated above */
  422. return_ACPI_STATUS(status);
  423. }
  424. /* Store the ddb_handle into the Target operand */
  425. status = acpi_ex_store(ddb_handle, target, walk_state);
  426. if (ACPI_FAILURE(status)) {
  427. (void)acpi_ex_unload_table(ddb_handle);
  428. /* table_ptr was deallocated above */
  429. acpi_ut_remove_reference(ddb_handle);
  430. return_ACPI_STATUS(status);
  431. }
  432. /* Remove the reference by added by acpi_ex_store above */
  433. acpi_ut_remove_reference(ddb_handle);
  434. /* Invoke table handler if present */
  435. if (acpi_gbl_table_handler) {
  436. (void)acpi_gbl_table_handler(ACPI_TABLE_EVENT_LOAD, table,
  437. acpi_gbl_table_handler_context);
  438. }
  439. return_ACPI_STATUS(status);
  440. }
  441. /*******************************************************************************
  442. *
  443. * FUNCTION: acpi_ex_unload_table
  444. *
  445. * PARAMETERS: ddb_handle - Handle to a previously loaded table
  446. *
  447. * RETURN: Status
  448. *
  449. * DESCRIPTION: Unload an ACPI table
  450. *
  451. ******************************************************************************/
  452. acpi_status acpi_ex_unload_table(union acpi_operand_object *ddb_handle)
  453. {
  454. acpi_status status = AE_OK;
  455. union acpi_operand_object *table_desc = ddb_handle;
  456. u32 table_index;
  457. struct acpi_table_header *table;
  458. ACPI_FUNCTION_TRACE(ex_unload_table);
  459. /*
  460. * Temporarily emit a warning so that the ASL for the machine can be
  461. * hopefully obtained. This is to say that the Unload() operator is
  462. * extremely rare if not completely unused.
  463. */
  464. ACPI_WARNING((AE_INFO, "Received request to unload an ACPI table"));
  465. /*
  466. * Validate the handle
  467. * Although the handle is partially validated in acpi_ex_reconfiguration()
  468. * when it calls acpi_ex_resolve_operands(), the handle is more completely
  469. * validated here.
  470. *
  471. * Handle must be a valid operand object of type reference. Also, the
  472. * ddb_handle must still be marked valid (table has not been previously
  473. * unloaded)
  474. */
  475. if ((!ddb_handle) ||
  476. (ACPI_GET_DESCRIPTOR_TYPE(ddb_handle) != ACPI_DESC_TYPE_OPERAND) ||
  477. (ddb_handle->common.type != ACPI_TYPE_LOCAL_REFERENCE) ||
  478. (!(ddb_handle->common.flags & AOPOBJ_DATA_VALID))) {
  479. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  480. }
  481. /* Get the table index from the ddb_handle */
  482. table_index = table_desc->reference.value;
  483. /* Ensure the table is still loaded */
  484. if (!acpi_tb_is_table_loaded(table_index)) {
  485. return_ACPI_STATUS(AE_NOT_EXIST);
  486. }
  487. /* Invoke table handler if present */
  488. if (acpi_gbl_table_handler) {
  489. status = acpi_get_table_by_index(table_index, &table);
  490. if (ACPI_SUCCESS(status)) {
  491. (void)acpi_gbl_table_handler(ACPI_TABLE_EVENT_UNLOAD,
  492. table,
  493. acpi_gbl_table_handler_context);
  494. }
  495. }
  496. /* Delete the portion of the namespace owned by this table */
  497. status = acpi_tb_delete_namespace_by_owner(table_index);
  498. if (ACPI_FAILURE(status)) {
  499. return_ACPI_STATUS(status);
  500. }
  501. (void)acpi_tb_release_owner_id(table_index);
  502. acpi_tb_set_table_loaded_flag(table_index, FALSE);
  503. /*
  504. * Invalidate the handle. We do this because the handle may be stored
  505. * in a named object and may not be actually deleted until much later.
  506. */
  507. ddb_handle->common.flags &= ~AOPOBJ_DATA_VALID;
  508. return_ACPI_STATUS(AE_OK);
  509. }