tbxface.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /******************************************************************************
  2. *
  3. * Module Name: tbxface - ACPI table oriented external interfaces
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2013, 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 <linux/export.h>
  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_realloc - 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 __init
  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. * Set up the Root Table Array
  101. * Allocate the table array 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. ACPI_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. /*******************************************************************************
  135. *
  136. * FUNCTION: acpi_reallocate_root_table
  137. *
  138. * PARAMETERS: None
  139. *
  140. * RETURN: Status
  141. *
  142. * DESCRIPTION: Reallocate Root Table List into dynamic memory. Copies the
  143. * root list from the previously provided scratch area. Should
  144. * be called once dynamic memory allocation is available in the
  145. * kernel.
  146. *
  147. ******************************************************************************/
  148. acpi_status acpi_reallocate_root_table(void)
  149. {
  150. acpi_status status;
  151. ACPI_FUNCTION_TRACE(acpi_reallocate_root_table);
  152. /*
  153. * Only reallocate the root table if the host provided a static buffer
  154. * for the table array in the call to acpi_initialize_tables.
  155. */
  156. if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
  157. return_ACPI_STATUS(AE_SUPPORT);
  158. }
  159. acpi_gbl_root_table_list.flags |= ACPI_ROOT_ALLOW_RESIZE;
  160. status = acpi_tb_resize_root_table_list();
  161. return_ACPI_STATUS(status);
  162. }
  163. /*******************************************************************************
  164. *
  165. * FUNCTION: acpi_get_table_header
  166. *
  167. * PARAMETERS: signature - ACPI signature of needed table
  168. * instance - Which instance (for SSDTs)
  169. * out_table_header - The pointer to the table header to fill
  170. *
  171. * RETURN: Status and pointer to mapped table header
  172. *
  173. * DESCRIPTION: Finds an ACPI table header.
  174. *
  175. * NOTE: Caller is responsible in unmapping the header with
  176. * acpi_os_unmap_memory
  177. *
  178. ******************************************************************************/
  179. acpi_status
  180. acpi_get_table_header(char *signature,
  181. u32 instance, struct acpi_table_header *out_table_header)
  182. {
  183. u32 i;
  184. u32 j;
  185. struct acpi_table_header *header;
  186. /* Parameter validation */
  187. if (!signature || !out_table_header) {
  188. return (AE_BAD_PARAMETER);
  189. }
  190. /* Walk the root table list */
  191. for (i = 0, j = 0; i < acpi_gbl_root_table_list.current_table_count;
  192. i++) {
  193. if (!ACPI_COMPARE_NAME
  194. (&(acpi_gbl_root_table_list.tables[i].signature),
  195. signature)) {
  196. continue;
  197. }
  198. if (++j < instance) {
  199. continue;
  200. }
  201. if (!acpi_gbl_root_table_list.tables[i].pointer) {
  202. if ((acpi_gbl_root_table_list.tables[i].flags &
  203. ACPI_TABLE_ORIGIN_MASK) ==
  204. ACPI_TABLE_ORIGIN_MAPPED) {
  205. header =
  206. acpi_os_map_memory(acpi_gbl_root_table_list.
  207. tables[i].address,
  208. sizeof(struct
  209. acpi_table_header));
  210. if (!header) {
  211. return (AE_NO_MEMORY);
  212. }
  213. ACPI_MEMCPY(out_table_header, header,
  214. sizeof(struct acpi_table_header));
  215. acpi_os_unmap_memory(header,
  216. sizeof(struct
  217. acpi_table_header));
  218. } else {
  219. return (AE_NOT_FOUND);
  220. }
  221. } else {
  222. ACPI_MEMCPY(out_table_header,
  223. acpi_gbl_root_table_list.tables[i].pointer,
  224. sizeof(struct acpi_table_header));
  225. }
  226. return (AE_OK);
  227. }
  228. return (AE_NOT_FOUND);
  229. }
  230. ACPI_EXPORT_SYMBOL(acpi_get_table_header)
  231. /*******************************************************************************
  232. *
  233. * FUNCTION: acpi_unload_table_id
  234. *
  235. * PARAMETERS: id - Owner ID of the table to be removed.
  236. *
  237. * RETURN: Status
  238. *
  239. * DESCRIPTION: This routine is used to force the unload of a table (by id)
  240. *
  241. ******************************************************************************/
  242. acpi_status acpi_unload_table_id(acpi_owner_id id)
  243. {
  244. int i;
  245. acpi_status status = AE_NOT_EXIST;
  246. ACPI_FUNCTION_TRACE(acpi_unload_table_id);
  247. /* Find table in the global table list */
  248. for (i = 0; i < acpi_gbl_root_table_list.current_table_count; ++i) {
  249. if (id != acpi_gbl_root_table_list.tables[i].owner_id) {
  250. continue;
  251. }
  252. /*
  253. * Delete all namespace objects owned by this table. Note that these
  254. * objects can appear anywhere in the namespace by virtue of the AML
  255. * "Scope" operator. Thus, we need to track ownership by an ID, not
  256. * simply a position within the hierarchy
  257. */
  258. acpi_tb_delete_namespace_by_owner(i);
  259. status = acpi_tb_release_owner_id(i);
  260. acpi_tb_set_table_loaded_flag(i, FALSE);
  261. break;
  262. }
  263. return_ACPI_STATUS(status);
  264. }
  265. ACPI_EXPORT_SYMBOL(acpi_unload_table_id)
  266. /*******************************************************************************
  267. *
  268. * FUNCTION: acpi_get_table_with_size
  269. *
  270. * PARAMETERS: signature - ACPI signature of needed table
  271. * instance - Which instance (for SSDTs)
  272. * out_table - Where the pointer to the table is returned
  273. *
  274. * RETURN: Status and pointer to table
  275. *
  276. * DESCRIPTION: Finds and verifies an ACPI table.
  277. *
  278. ******************************************************************************/
  279. acpi_status
  280. acpi_get_table_with_size(char *signature,
  281. u32 instance, struct acpi_table_header **out_table,
  282. acpi_size *tbl_size)
  283. {
  284. u32 i;
  285. u32 j;
  286. acpi_status status;
  287. /* Parameter validation */
  288. if (!signature || !out_table) {
  289. return (AE_BAD_PARAMETER);
  290. }
  291. /* Walk the root table list */
  292. for (i = 0, j = 0; i < acpi_gbl_root_table_list.current_table_count;
  293. i++) {
  294. if (!ACPI_COMPARE_NAME
  295. (&(acpi_gbl_root_table_list.tables[i].signature),
  296. signature)) {
  297. continue;
  298. }
  299. if (++j < instance) {
  300. continue;
  301. }
  302. status =
  303. acpi_tb_verify_table(&acpi_gbl_root_table_list.tables[i]);
  304. if (ACPI_SUCCESS(status)) {
  305. *out_table = acpi_gbl_root_table_list.tables[i].pointer;
  306. *tbl_size = acpi_gbl_root_table_list.tables[i].length;
  307. }
  308. if (!acpi_gbl_permanent_mmap) {
  309. acpi_gbl_root_table_list.tables[i].pointer = NULL;
  310. }
  311. return (status);
  312. }
  313. return (AE_NOT_FOUND);
  314. }
  315. ACPI_EXPORT_SYMBOL(acpi_get_table_with_size)
  316. acpi_status
  317. acpi_get_table(char *signature,
  318. u32 instance, struct acpi_table_header **out_table)
  319. {
  320. acpi_size tbl_size;
  321. return acpi_get_table_with_size(signature,
  322. instance, out_table, &tbl_size);
  323. }
  324. ACPI_EXPORT_SYMBOL(acpi_get_table)
  325. /*******************************************************************************
  326. *
  327. * FUNCTION: acpi_get_table_by_index
  328. *
  329. * PARAMETERS: table_index - Table index
  330. * table - Where the pointer to the table is returned
  331. *
  332. * RETURN: Status and pointer to the table
  333. *
  334. * DESCRIPTION: Obtain a table by an index into the global table list.
  335. *
  336. ******************************************************************************/
  337. acpi_status
  338. acpi_get_table_by_index(u32 table_index, struct acpi_table_header **table)
  339. {
  340. acpi_status status;
  341. ACPI_FUNCTION_TRACE(acpi_get_table_by_index);
  342. /* Parameter validation */
  343. if (!table) {
  344. return_ACPI_STATUS(AE_BAD_PARAMETER);
  345. }
  346. (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
  347. /* Validate index */
  348. if (table_index >= acpi_gbl_root_table_list.current_table_count) {
  349. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  350. return_ACPI_STATUS(AE_BAD_PARAMETER);
  351. }
  352. if (!acpi_gbl_root_table_list.tables[table_index].pointer) {
  353. /* Table is not mapped, map it */
  354. status =
  355. acpi_tb_verify_table(&acpi_gbl_root_table_list.
  356. tables[table_index]);
  357. if (ACPI_FAILURE(status)) {
  358. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  359. return_ACPI_STATUS(status);
  360. }
  361. }
  362. *table = acpi_gbl_root_table_list.tables[table_index].pointer;
  363. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  364. return_ACPI_STATUS(AE_OK);
  365. }
  366. ACPI_EXPORT_SYMBOL(acpi_get_table_by_index)
  367. /*******************************************************************************
  368. *
  369. * FUNCTION: acpi_install_table_handler
  370. *
  371. * PARAMETERS: handler - Table event handler
  372. * context - Value passed to the handler on each event
  373. *
  374. * RETURN: Status
  375. *
  376. * DESCRIPTION: Install table event handler
  377. *
  378. ******************************************************************************/
  379. acpi_status
  380. acpi_install_table_handler(acpi_table_handler handler, void *context)
  381. {
  382. acpi_status status;
  383. ACPI_FUNCTION_TRACE(acpi_install_table_handler);
  384. if (!handler) {
  385. return_ACPI_STATUS(AE_BAD_PARAMETER);
  386. }
  387. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  388. if (ACPI_FAILURE(status)) {
  389. return_ACPI_STATUS(status);
  390. }
  391. /* Don't allow more than one handler */
  392. if (acpi_gbl_table_handler) {
  393. status = AE_ALREADY_EXISTS;
  394. goto cleanup;
  395. }
  396. /* Install the handler */
  397. acpi_gbl_table_handler = handler;
  398. acpi_gbl_table_handler_context = context;
  399. cleanup:
  400. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  401. return_ACPI_STATUS(status);
  402. }
  403. ACPI_EXPORT_SYMBOL(acpi_install_table_handler)
  404. /*******************************************************************************
  405. *
  406. * FUNCTION: acpi_remove_table_handler
  407. *
  408. * PARAMETERS: handler - Table event handler that was installed
  409. * previously.
  410. *
  411. * RETURN: Status
  412. *
  413. * DESCRIPTION: Remove table event handler
  414. *
  415. ******************************************************************************/
  416. acpi_status acpi_remove_table_handler(acpi_table_handler handler)
  417. {
  418. acpi_status status;
  419. ACPI_FUNCTION_TRACE(acpi_remove_table_handler);
  420. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  421. if (ACPI_FAILURE(status)) {
  422. return_ACPI_STATUS(status);
  423. }
  424. /* Make sure that the installed handler is the same */
  425. if (!handler || handler != acpi_gbl_table_handler) {
  426. status = AE_BAD_PARAMETER;
  427. goto cleanup;
  428. }
  429. /* Remove the handler */
  430. acpi_gbl_table_handler = NULL;
  431. cleanup:
  432. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  433. return_ACPI_STATUS(status);
  434. }
  435. ACPI_EXPORT_SYMBOL(acpi_remove_table_handler)