tbxface.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /******************************************************************************
  2. *
  3. * Module Name: tbxface - ACPI table-oriented external interfaces
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, 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. #define EXPORT_ACPI_INTERFACES
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "actables.h"
  46. #define _COMPONENT ACPI_TABLES
  47. ACPI_MODULE_NAME("tbxface")
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_allocate_root_table
  51. *
  52. * PARAMETERS: initial_table_count - Size of initial_table_array, in number of
  53. * struct acpi_table_desc structures
  54. *
  55. * RETURN: Status
  56. *
  57. * DESCRIPTION: Allocate a root table array. Used by iASL compiler and
  58. * acpi_initialize_tables.
  59. *
  60. ******************************************************************************/
  61. acpi_status acpi_allocate_root_table(u32 initial_table_count)
  62. {
  63. acpi_gbl_root_table_list.max_table_count = initial_table_count;
  64. acpi_gbl_root_table_list.flags = ACPI_ROOT_ALLOW_RESIZE;
  65. return (acpi_tb_resize_root_table_list());
  66. }
  67. /*******************************************************************************
  68. *
  69. * FUNCTION: acpi_initialize_tables
  70. *
  71. * PARAMETERS: initial_table_array - Pointer to an array of pre-allocated
  72. * struct acpi_table_desc structures. If NULL, the
  73. * array is dynamically allocated.
  74. * initial_table_count - Size of initial_table_array, in number of
  75. * struct acpi_table_desc structures
  76. * allow_resize - Flag to tell Table Manager if resize of
  77. * pre-allocated array is allowed. Ignored
  78. * if initial_table_array is NULL.
  79. *
  80. * RETURN: Status
  81. *
  82. * DESCRIPTION: Initialize the table manager, get the RSDP and RSDT/XSDT.
  83. *
  84. * NOTE: Allows static allocation of the initial table array in order
  85. * to avoid the use of dynamic memory in confined environments
  86. * such as the kernel boot sequence where it may not be available.
  87. *
  88. * If the host OS memory managers are initialized, use NULL for
  89. * initial_table_array, and the table will be dynamically allocated.
  90. *
  91. ******************************************************************************/
  92. acpi_status ACPI_INIT_FUNCTION
  93. acpi_initialize_tables(struct acpi_table_desc *initial_table_array,
  94. u32 initial_table_count, u8 allow_resize)
  95. {
  96. acpi_physical_address rsdp_address;
  97. acpi_status status;
  98. ACPI_FUNCTION_TRACE(acpi_initialize_tables);
  99. /*
  100. * Setup the Root Table Array and allocate the table array
  101. * if requested
  102. */
  103. if (!initial_table_array) {
  104. status = acpi_allocate_root_table(initial_table_count);
  105. if (ACPI_FAILURE(status)) {
  106. return_ACPI_STATUS(status);
  107. }
  108. } else {
  109. /* Root Table Array has been statically allocated by the host */
  110. memset(initial_table_array, 0,
  111. (acpi_size)initial_table_count *
  112. sizeof(struct acpi_table_desc));
  113. acpi_gbl_root_table_list.tables = initial_table_array;
  114. acpi_gbl_root_table_list.max_table_count = initial_table_count;
  115. acpi_gbl_root_table_list.flags = ACPI_ROOT_ORIGIN_UNKNOWN;
  116. if (allow_resize) {
  117. acpi_gbl_root_table_list.flags |=
  118. ACPI_ROOT_ALLOW_RESIZE;
  119. }
  120. }
  121. /* Get the address of the RSDP */
  122. rsdp_address = acpi_os_get_root_pointer();
  123. if (!rsdp_address) {
  124. return_ACPI_STATUS(AE_NOT_FOUND);
  125. }
  126. /*
  127. * Get the root table (RSDT or XSDT) and extract all entries to the local
  128. * Root Table Array. This array contains the information of the RSDT/XSDT
  129. * in a common, more useable format.
  130. */
  131. status = acpi_tb_parse_root_table(rsdp_address);
  132. return_ACPI_STATUS(status);
  133. }
  134. ACPI_EXPORT_SYMBOL_INIT(acpi_initialize_tables)
  135. /*******************************************************************************
  136. *
  137. * FUNCTION: acpi_reallocate_root_table
  138. *
  139. * PARAMETERS: None
  140. *
  141. * RETURN: Status
  142. *
  143. * DESCRIPTION: Reallocate Root Table List into dynamic memory. Copies the
  144. * root list from the previously provided scratch area. Should
  145. * be called once dynamic memory allocation is available in the
  146. * kernel.
  147. *
  148. ******************************************************************************/
  149. acpi_status ACPI_INIT_FUNCTION acpi_reallocate_root_table(void)
  150. {
  151. acpi_status status;
  152. struct acpi_table_desc *table_desc;
  153. u32 i, j;
  154. ACPI_FUNCTION_TRACE(acpi_reallocate_root_table);
  155. /*
  156. * Only reallocate the root table if the host provided a static buffer
  157. * for the table array in the call to acpi_initialize_tables.
  158. */
  159. if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
  160. return_ACPI_STATUS(AE_SUPPORT);
  161. }
  162. (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
  163. /*
  164. * Ensure OS early boot logic, which is required by some hosts. If the
  165. * table state is reported to be wrong, developers should fix the
  166. * issue by invoking acpi_put_table() for the reported table during the
  167. * early stage.
  168. */
  169. for (i = 0; i < acpi_gbl_root_table_list.current_table_count; ++i) {
  170. table_desc = &acpi_gbl_root_table_list.tables[i];
  171. if (table_desc->pointer) {
  172. ACPI_ERROR((AE_INFO,
  173. "Table [%4.4s] is not invalidated during early boot stage",
  174. table_desc->signature.ascii));
  175. }
  176. }
  177. if (!acpi_gbl_enable_table_validation) {
  178. /*
  179. * Now it's safe to do full table validation. We can do deferred
  180. * table initilization here once the flag is set.
  181. */
  182. acpi_gbl_enable_table_validation = TRUE;
  183. for (i = 0; i < acpi_gbl_root_table_list.current_table_count;
  184. ++i) {
  185. table_desc = &acpi_gbl_root_table_list.tables[i];
  186. if (!(table_desc->flags & ACPI_TABLE_IS_VERIFIED)) {
  187. status =
  188. acpi_tb_verify_temp_table(table_desc, NULL,
  189. &j);
  190. if (ACPI_FAILURE(status)) {
  191. acpi_tb_uninstall_table(table_desc);
  192. }
  193. }
  194. }
  195. }
  196. acpi_gbl_root_table_list.flags |= ACPI_ROOT_ALLOW_RESIZE;
  197. status = acpi_tb_resize_root_table_list();
  198. acpi_gbl_root_table_list.flags |= ACPI_ROOT_ORIGIN_ALLOCATED;
  199. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  200. return_ACPI_STATUS(status);
  201. }
  202. ACPI_EXPORT_SYMBOL_INIT(acpi_reallocate_root_table)
  203. /*******************************************************************************
  204. *
  205. * FUNCTION: acpi_get_table_header
  206. *
  207. * PARAMETERS: signature - ACPI signature of needed table
  208. * instance - Which instance (for SSDTs)
  209. * out_table_header - The pointer to the table header to fill
  210. *
  211. * RETURN: Status and pointer to mapped table header
  212. *
  213. * DESCRIPTION: Finds an ACPI table header.
  214. *
  215. * NOTE: Caller is responsible in unmapping the header with
  216. * acpi_os_unmap_memory
  217. *
  218. ******************************************************************************/
  219. acpi_status
  220. acpi_get_table_header(char *signature,
  221. u32 instance, struct acpi_table_header *out_table_header)
  222. {
  223. u32 i;
  224. u32 j;
  225. struct acpi_table_header *header;
  226. /* Parameter validation */
  227. if (!signature || !out_table_header) {
  228. return (AE_BAD_PARAMETER);
  229. }
  230. /* Walk the root table list */
  231. for (i = 0, j = 0; i < acpi_gbl_root_table_list.current_table_count;
  232. i++) {
  233. if (!ACPI_COMPARE_NAME
  234. (&(acpi_gbl_root_table_list.tables[i].signature),
  235. signature)) {
  236. continue;
  237. }
  238. if (++j < instance) {
  239. continue;
  240. }
  241. if (!acpi_gbl_root_table_list.tables[i].pointer) {
  242. if ((acpi_gbl_root_table_list.tables[i].flags &
  243. ACPI_TABLE_ORIGIN_MASK) ==
  244. ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL) {
  245. header =
  246. acpi_os_map_memory(acpi_gbl_root_table_list.
  247. tables[i].address,
  248. sizeof(struct
  249. acpi_table_header));
  250. if (!header) {
  251. return (AE_NO_MEMORY);
  252. }
  253. memcpy(out_table_header, header,
  254. sizeof(struct acpi_table_header));
  255. acpi_os_unmap_memory(header,
  256. sizeof(struct
  257. acpi_table_header));
  258. } else {
  259. return (AE_NOT_FOUND);
  260. }
  261. } else {
  262. memcpy(out_table_header,
  263. acpi_gbl_root_table_list.tables[i].pointer,
  264. sizeof(struct acpi_table_header));
  265. }
  266. return (AE_OK);
  267. }
  268. return (AE_NOT_FOUND);
  269. }
  270. ACPI_EXPORT_SYMBOL(acpi_get_table_header)
  271. /*******************************************************************************
  272. *
  273. * FUNCTION: acpi_get_table
  274. *
  275. * PARAMETERS: signature - ACPI signature of needed table
  276. * instance - Which instance (for SSDTs)
  277. * out_table - Where the pointer to the table is returned
  278. *
  279. * RETURN: Status and pointer to the requested table
  280. *
  281. * DESCRIPTION: Finds and verifies an ACPI table. Table must be in the
  282. * RSDT/XSDT.
  283. * Note that an early stage acpi_get_table() call must be paired
  284. * with an early stage acpi_put_table() call. otherwise the table
  285. * pointer mapped by the early stage mapping implementation may be
  286. * erroneously unmapped by the late stage unmapping implementation
  287. * in an acpi_put_table() invoked during the late stage.
  288. *
  289. ******************************************************************************/
  290. acpi_status
  291. acpi_get_table(char *signature,
  292. u32 instance, struct acpi_table_header ** out_table)
  293. {
  294. u32 i;
  295. u32 j;
  296. acpi_status status = AE_NOT_FOUND;
  297. struct acpi_table_desc *table_desc;
  298. /* Parameter validation */
  299. if (!signature || !out_table) {
  300. return (AE_BAD_PARAMETER);
  301. }
  302. /*
  303. * Note that the following line is required by some OSPMs, they only
  304. * check if the returned table is NULL instead of the returned status
  305. * to determined if this function is succeeded.
  306. */
  307. *out_table = NULL;
  308. (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
  309. /* Walk the root table list */
  310. for (i = 0, j = 0; i < acpi_gbl_root_table_list.current_table_count;
  311. i++) {
  312. table_desc = &acpi_gbl_root_table_list.tables[i];
  313. if (!ACPI_COMPARE_NAME(&table_desc->signature, signature)) {
  314. continue;
  315. }
  316. if (++j < instance) {
  317. continue;
  318. }
  319. status = acpi_tb_get_table(table_desc, out_table);
  320. break;
  321. }
  322. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  323. return (status);
  324. }
  325. ACPI_EXPORT_SYMBOL(acpi_get_table)
  326. /*******************************************************************************
  327. *
  328. * FUNCTION: acpi_put_table
  329. *
  330. * PARAMETERS: table - The pointer to the table
  331. *
  332. * RETURN: None
  333. *
  334. * DESCRIPTION: Release a table returned by acpi_get_table() and its clones.
  335. * Note that it is not safe if this function was invoked after an
  336. * uninstallation happened to the original table descriptor.
  337. * Currently there is no OSPMs' requirement to handle such
  338. * situations.
  339. *
  340. ******************************************************************************/
  341. void acpi_put_table(struct acpi_table_header *table)
  342. {
  343. u32 i;
  344. struct acpi_table_desc *table_desc;
  345. ACPI_FUNCTION_TRACE(acpi_put_table);
  346. if (!table) {
  347. return_VOID;
  348. }
  349. (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
  350. /* Walk the root table list */
  351. for (i = 0; i < acpi_gbl_root_table_list.current_table_count; i++) {
  352. table_desc = &acpi_gbl_root_table_list.tables[i];
  353. if (table_desc->pointer != table) {
  354. continue;
  355. }
  356. acpi_tb_put_table(table_desc);
  357. break;
  358. }
  359. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  360. return_VOID;
  361. }
  362. ACPI_EXPORT_SYMBOL(acpi_put_table)
  363. /*******************************************************************************
  364. *
  365. * FUNCTION: acpi_get_table_by_index
  366. *
  367. * PARAMETERS: table_index - Table index
  368. * out_table - Where the pointer to the table is returned
  369. *
  370. * RETURN: Status and pointer to the requested table
  371. *
  372. * DESCRIPTION: Obtain a table by an index into the global table list. Used
  373. * internally also.
  374. *
  375. ******************************************************************************/
  376. acpi_status
  377. acpi_get_table_by_index(u32 table_index, struct acpi_table_header **out_table)
  378. {
  379. acpi_status status;
  380. ACPI_FUNCTION_TRACE(acpi_get_table_by_index);
  381. /* Parameter validation */
  382. if (!out_table) {
  383. return_ACPI_STATUS(AE_BAD_PARAMETER);
  384. }
  385. /*
  386. * Note that the following line is required by some OSPMs, they only
  387. * check if the returned table is NULL instead of the returned status
  388. * to determined if this function is succeeded.
  389. */
  390. *out_table = NULL;
  391. (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
  392. /* Validate index */
  393. if (table_index >= acpi_gbl_root_table_list.current_table_count) {
  394. status = AE_BAD_PARAMETER;
  395. goto unlock_and_exit;
  396. }
  397. status =
  398. acpi_tb_get_table(&acpi_gbl_root_table_list.tables[table_index],
  399. out_table);
  400. unlock_and_exit:
  401. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  402. return_ACPI_STATUS(status);
  403. }
  404. ACPI_EXPORT_SYMBOL(acpi_get_table_by_index)
  405. /*******************************************************************************
  406. *
  407. * FUNCTION: acpi_install_table_handler
  408. *
  409. * PARAMETERS: handler - Table event handler
  410. * context - Value passed to the handler on each event
  411. *
  412. * RETURN: Status
  413. *
  414. * DESCRIPTION: Install a global table event handler.
  415. *
  416. ******************************************************************************/
  417. acpi_status
  418. acpi_install_table_handler(acpi_table_handler handler, void *context)
  419. {
  420. acpi_status status;
  421. ACPI_FUNCTION_TRACE(acpi_install_table_handler);
  422. if (!handler) {
  423. return_ACPI_STATUS(AE_BAD_PARAMETER);
  424. }
  425. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  426. if (ACPI_FAILURE(status)) {
  427. return_ACPI_STATUS(status);
  428. }
  429. /* Don't allow more than one handler */
  430. if (acpi_gbl_table_handler) {
  431. status = AE_ALREADY_EXISTS;
  432. goto cleanup;
  433. }
  434. /* Install the handler */
  435. acpi_gbl_table_handler = handler;
  436. acpi_gbl_table_handler_context = context;
  437. cleanup:
  438. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  439. return_ACPI_STATUS(status);
  440. }
  441. ACPI_EXPORT_SYMBOL(acpi_install_table_handler)
  442. /*******************************************************************************
  443. *
  444. * FUNCTION: acpi_remove_table_handler
  445. *
  446. * PARAMETERS: handler - Table event handler that was installed
  447. * previously.
  448. *
  449. * RETURN: Status
  450. *
  451. * DESCRIPTION: Remove a table event handler
  452. *
  453. ******************************************************************************/
  454. acpi_status acpi_remove_table_handler(acpi_table_handler handler)
  455. {
  456. acpi_status status;
  457. ACPI_FUNCTION_TRACE(acpi_remove_table_handler);
  458. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  459. if (ACPI_FAILURE(status)) {
  460. return_ACPI_STATUS(status);
  461. }
  462. /* Make sure that the installed handler is the same */
  463. if (!handler || handler != acpi_gbl_table_handler) {
  464. status = AE_BAD_PARAMETER;
  465. goto cleanup;
  466. }
  467. /* Remove the handler */
  468. acpi_gbl_table_handler = NULL;
  469. cleanup:
  470. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  471. return_ACPI_STATUS(status);
  472. }
  473. ACPI_EXPORT_SYMBOL(acpi_remove_table_handler)