tbxface.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /******************************************************************************
  2. *
  3. * Module Name: tbxface - ACPI table oriented external interfaces
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2012, 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 "acnamesp.h"
  46. #include "actables.h"
  47. #define _COMPONENT ACPI_TABLES
  48. ACPI_MODULE_NAME("tbxface")
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_allocate_root_table
  52. *
  53. * PARAMETERS: initial_table_count - Size of initial_table_array, in number of
  54. * struct acpi_table_desc structures
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Allocate a root table array. Used by iASL compiler and
  59. * acpi_initialize_tables.
  60. *
  61. ******************************************************************************/
  62. acpi_status acpi_allocate_root_table(u32 initial_table_count)
  63. {
  64. acpi_gbl_root_table_list.max_table_count = initial_table_count;
  65. acpi_gbl_root_table_list.flags = ACPI_ROOT_ALLOW_RESIZE;
  66. return (acpi_tb_resize_root_table_list());
  67. }
  68. /*******************************************************************************
  69. *
  70. * FUNCTION: acpi_initialize_tables
  71. *
  72. * PARAMETERS: initial_table_array - Pointer to an array of pre-allocated
  73. * struct acpi_table_desc structures. If NULL, the
  74. * array is dynamically allocated.
  75. * initial_table_count - Size of initial_table_array, in number of
  76. * struct acpi_table_desc structures
  77. * allow_realloc - Flag to tell Table Manager if resize of
  78. * pre-allocated array is allowed. Ignored
  79. * if initial_table_array is NULL.
  80. *
  81. * RETURN: Status
  82. *
  83. * DESCRIPTION: Initialize the table manager, get the RSDP and RSDT/XSDT.
  84. *
  85. * NOTE: Allows static allocation of the initial table array in order
  86. * to avoid the use of dynamic memory in confined environments
  87. * such as the kernel boot sequence where it may not be available.
  88. *
  89. * If the host OS memory managers are initialized, use NULL for
  90. * initial_table_array, and the table will be dynamically allocated.
  91. *
  92. ******************************************************************************/
  93. acpi_status __init
  94. acpi_initialize_tables(struct acpi_table_desc * initial_table_array,
  95. u32 initial_table_count, u8 allow_resize)
  96. {
  97. acpi_physical_address rsdp_address;
  98. acpi_status status;
  99. ACPI_FUNCTION_TRACE(acpi_initialize_tables);
  100. /*
  101. * Set up the Root Table Array
  102. * Allocate the table array if requested
  103. */
  104. if (!initial_table_array) {
  105. status = acpi_allocate_root_table(initial_table_count);
  106. if (ACPI_FAILURE(status)) {
  107. return_ACPI_STATUS(status);
  108. }
  109. } else {
  110. /* Root Table Array has been statically allocated by the host */
  111. ACPI_MEMSET(initial_table_array, 0,
  112. (acpi_size) initial_table_count *
  113. sizeof(struct acpi_table_desc));
  114. acpi_gbl_root_table_list.tables = initial_table_array;
  115. acpi_gbl_root_table_list.max_table_count = initial_table_count;
  116. acpi_gbl_root_table_list.flags = ACPI_ROOT_ORIGIN_UNKNOWN;
  117. if (allow_resize) {
  118. acpi_gbl_root_table_list.flags |=
  119. ACPI_ROOT_ALLOW_RESIZE;
  120. }
  121. }
  122. /* Get the address of the RSDP */
  123. rsdp_address = acpi_os_get_root_pointer();
  124. if (!rsdp_address) {
  125. return_ACPI_STATUS(AE_NOT_FOUND);
  126. }
  127. /*
  128. * Get the root table (RSDT or XSDT) and extract all entries to the local
  129. * Root Table Array. This array contains the information of the RSDT/XSDT
  130. * in a common, more useable format.
  131. */
  132. status = acpi_tb_parse_root_table(rsdp_address);
  133. return_ACPI_STATUS(status);
  134. }
  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_reallocate_root_table(void)
  150. {
  151. struct acpi_table_desc *tables;
  152. acpi_size new_size;
  153. acpi_size current_size;
  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. /*
  163. * Get the current size of the root table and add the default
  164. * increment to create the new table size.
  165. */
  166. current_size = (acpi_size)
  167. acpi_gbl_root_table_list.current_table_count *
  168. sizeof(struct acpi_table_desc);
  169. new_size = current_size +
  170. (ACPI_ROOT_TABLE_SIZE_INCREMENT * sizeof(struct acpi_table_desc));
  171. /* Create new array and copy the old array */
  172. tables = ACPI_ALLOCATE_ZEROED(new_size);
  173. if (!tables) {
  174. return_ACPI_STATUS(AE_NO_MEMORY);
  175. }
  176. ACPI_MEMCPY(tables, acpi_gbl_root_table_list.tables, current_size);
  177. /*
  178. * Update the root table descriptor. The new size will be the current
  179. * number of tables plus the increment, independent of the reserved
  180. * size of the original table list.
  181. */
  182. acpi_gbl_root_table_list.tables = tables;
  183. acpi_gbl_root_table_list.max_table_count =
  184. acpi_gbl_root_table_list.current_table_count +
  185. ACPI_ROOT_TABLE_SIZE_INCREMENT;
  186. acpi_gbl_root_table_list.flags =
  187. ACPI_ROOT_ORIGIN_ALLOCATED | ACPI_ROOT_ALLOW_RESIZE;
  188. return_ACPI_STATUS(AE_OK);
  189. }
  190. /*******************************************************************************
  191. *
  192. * FUNCTION: acpi_get_table_header
  193. *
  194. * PARAMETERS: signature - ACPI signature of needed table
  195. * instance - Which instance (for SSDTs)
  196. * out_table_header - The pointer to the table header to fill
  197. *
  198. * RETURN: Status and pointer to mapped table header
  199. *
  200. * DESCRIPTION: Finds an ACPI table header.
  201. *
  202. * NOTE: Caller is responsible in unmapping the header with
  203. * acpi_os_unmap_memory
  204. *
  205. ******************************************************************************/
  206. acpi_status
  207. acpi_get_table_header(char *signature,
  208. u32 instance, struct acpi_table_header *out_table_header)
  209. {
  210. u32 i;
  211. u32 j;
  212. struct acpi_table_header *header;
  213. /* Parameter validation */
  214. if (!signature || !out_table_header) {
  215. return (AE_BAD_PARAMETER);
  216. }
  217. /* Walk the root table list */
  218. for (i = 0, j = 0; i < acpi_gbl_root_table_list.current_table_count;
  219. i++) {
  220. if (!ACPI_COMPARE_NAME
  221. (&(acpi_gbl_root_table_list.tables[i].signature),
  222. signature)) {
  223. continue;
  224. }
  225. if (++j < instance) {
  226. continue;
  227. }
  228. if (!acpi_gbl_root_table_list.tables[i].pointer) {
  229. if ((acpi_gbl_root_table_list.tables[i].flags &
  230. ACPI_TABLE_ORIGIN_MASK) ==
  231. ACPI_TABLE_ORIGIN_MAPPED) {
  232. header =
  233. acpi_os_map_memory(acpi_gbl_root_table_list.
  234. tables[i].address,
  235. sizeof(struct
  236. acpi_table_header));
  237. if (!header) {
  238. return AE_NO_MEMORY;
  239. }
  240. ACPI_MEMCPY(out_table_header, header,
  241. sizeof(struct acpi_table_header));
  242. acpi_os_unmap_memory(header,
  243. sizeof(struct
  244. acpi_table_header));
  245. } else {
  246. return AE_NOT_FOUND;
  247. }
  248. } else {
  249. ACPI_MEMCPY(out_table_header,
  250. acpi_gbl_root_table_list.tables[i].pointer,
  251. sizeof(struct acpi_table_header));
  252. }
  253. return (AE_OK);
  254. }
  255. return (AE_NOT_FOUND);
  256. }
  257. ACPI_EXPORT_SYMBOL(acpi_get_table_header)
  258. /*******************************************************************************
  259. *
  260. * FUNCTION: acpi_unload_table_id
  261. *
  262. * PARAMETERS: id - Owner ID of the table to be removed.
  263. *
  264. * RETURN: Status
  265. *
  266. * DESCRIPTION: This routine is used to force the unload of a table (by id)
  267. *
  268. ******************************************************************************/
  269. acpi_status acpi_unload_table_id(acpi_owner_id id)
  270. {
  271. int i;
  272. acpi_status status = AE_NOT_EXIST;
  273. ACPI_FUNCTION_TRACE(acpi_unload_table_id);
  274. /* Find table in the global table list */
  275. for (i = 0; i < acpi_gbl_root_table_list.current_table_count; ++i) {
  276. if (id != acpi_gbl_root_table_list.tables[i].owner_id) {
  277. continue;
  278. }
  279. /*
  280. * Delete all namespace objects owned by this table. Note that these
  281. * objects can appear anywhere in the namespace by virtue of the AML
  282. * "Scope" operator. Thus, we need to track ownership by an ID, not
  283. * simply a position within the hierarchy
  284. */
  285. acpi_tb_delete_namespace_by_owner(i);
  286. status = acpi_tb_release_owner_id(i);
  287. acpi_tb_set_table_loaded_flag(i, FALSE);
  288. break;
  289. }
  290. return_ACPI_STATUS(status);
  291. }
  292. ACPI_EXPORT_SYMBOL(acpi_unload_table_id)
  293. /*******************************************************************************
  294. *
  295. * FUNCTION: acpi_get_table_with_size
  296. *
  297. * PARAMETERS: signature - ACPI signature of needed table
  298. * instance - Which instance (for SSDTs)
  299. * out_table - Where the pointer to the table is returned
  300. *
  301. * RETURN: Status and pointer to table
  302. *
  303. * DESCRIPTION: Finds and verifies an ACPI table.
  304. *
  305. ******************************************************************************/
  306. acpi_status
  307. acpi_get_table_with_size(char *signature,
  308. u32 instance, struct acpi_table_header **out_table,
  309. acpi_size *tbl_size)
  310. {
  311. u32 i;
  312. u32 j;
  313. acpi_status status;
  314. /* Parameter validation */
  315. if (!signature || !out_table) {
  316. return (AE_BAD_PARAMETER);
  317. }
  318. /* Walk the root table list */
  319. for (i = 0, j = 0; i < acpi_gbl_root_table_list.current_table_count;
  320. i++) {
  321. if (!ACPI_COMPARE_NAME
  322. (&(acpi_gbl_root_table_list.tables[i].signature),
  323. signature)) {
  324. continue;
  325. }
  326. if (++j < instance) {
  327. continue;
  328. }
  329. status =
  330. acpi_tb_verify_table(&acpi_gbl_root_table_list.tables[i]);
  331. if (ACPI_SUCCESS(status)) {
  332. *out_table = acpi_gbl_root_table_list.tables[i].pointer;
  333. *tbl_size = acpi_gbl_root_table_list.tables[i].length;
  334. }
  335. if (!acpi_gbl_permanent_mmap) {
  336. acpi_gbl_root_table_list.tables[i].pointer = NULL;
  337. }
  338. return (status);
  339. }
  340. return (AE_NOT_FOUND);
  341. }
  342. acpi_status
  343. acpi_get_table(char *signature,
  344. u32 instance, struct acpi_table_header **out_table)
  345. {
  346. acpi_size tbl_size;
  347. return acpi_get_table_with_size(signature,
  348. instance, out_table, &tbl_size);
  349. }
  350. ACPI_EXPORT_SYMBOL(acpi_get_table)
  351. /*******************************************************************************
  352. *
  353. * FUNCTION: acpi_get_table_by_index
  354. *
  355. * PARAMETERS: table_index - Table index
  356. * table - Where the pointer to the table is returned
  357. *
  358. * RETURN: Status and pointer to the table
  359. *
  360. * DESCRIPTION: Obtain a table by an index into the global table list.
  361. *
  362. ******************************************************************************/
  363. acpi_status
  364. acpi_get_table_by_index(u32 table_index, struct acpi_table_header **table)
  365. {
  366. acpi_status status;
  367. ACPI_FUNCTION_TRACE(acpi_get_table_by_index);
  368. /* Parameter validation */
  369. if (!table) {
  370. return_ACPI_STATUS(AE_BAD_PARAMETER);
  371. }
  372. (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
  373. /* Validate index */
  374. if (table_index >= acpi_gbl_root_table_list.current_table_count) {
  375. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  376. return_ACPI_STATUS(AE_BAD_PARAMETER);
  377. }
  378. if (!acpi_gbl_root_table_list.tables[table_index].pointer) {
  379. /* Table is not mapped, map it */
  380. status =
  381. acpi_tb_verify_table(&acpi_gbl_root_table_list.
  382. tables[table_index]);
  383. if (ACPI_FAILURE(status)) {
  384. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  385. return_ACPI_STATUS(status);
  386. }
  387. }
  388. *table = acpi_gbl_root_table_list.tables[table_index].pointer;
  389. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  390. return_ACPI_STATUS(AE_OK);
  391. }
  392. ACPI_EXPORT_SYMBOL(acpi_get_table_by_index)
  393. /*******************************************************************************
  394. *
  395. * FUNCTION: acpi_install_table_handler
  396. *
  397. * PARAMETERS: handler - Table event handler
  398. * context - Value passed to the handler on each event
  399. *
  400. * RETURN: Status
  401. *
  402. * DESCRIPTION: Install table event handler
  403. *
  404. ******************************************************************************/
  405. acpi_status
  406. acpi_install_table_handler(acpi_tbl_handler handler, void *context)
  407. {
  408. acpi_status status;
  409. ACPI_FUNCTION_TRACE(acpi_install_table_handler);
  410. if (!handler) {
  411. return_ACPI_STATUS(AE_BAD_PARAMETER);
  412. }
  413. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  414. if (ACPI_FAILURE(status)) {
  415. return_ACPI_STATUS(status);
  416. }
  417. /* Don't allow more than one handler */
  418. if (acpi_gbl_table_handler) {
  419. status = AE_ALREADY_EXISTS;
  420. goto cleanup;
  421. }
  422. /* Install the handler */
  423. acpi_gbl_table_handler = handler;
  424. acpi_gbl_table_handler_context = context;
  425. cleanup:
  426. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  427. return_ACPI_STATUS(status);
  428. }
  429. ACPI_EXPORT_SYMBOL(acpi_install_table_handler)
  430. /*******************************************************************************
  431. *
  432. * FUNCTION: acpi_remove_table_handler
  433. *
  434. * PARAMETERS: handler - Table event handler that was installed
  435. * previously.
  436. *
  437. * RETURN: Status
  438. *
  439. * DESCRIPTION: Remove table event handler
  440. *
  441. ******************************************************************************/
  442. acpi_status acpi_remove_table_handler(acpi_tbl_handler handler)
  443. {
  444. acpi_status status;
  445. ACPI_FUNCTION_TRACE(acpi_remove_table_handler);
  446. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  447. if (ACPI_FAILURE(status)) {
  448. return_ACPI_STATUS(status);
  449. }
  450. /* Make sure that the installed handler is the same */
  451. if (!handler || handler != acpi_gbl_table_handler) {
  452. status = AE_BAD_PARAMETER;
  453. goto cleanup;
  454. }
  455. /* Remove the handler */
  456. acpi_gbl_table_handler = NULL;
  457. cleanup:
  458. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  459. return_ACPI_STATUS(status);
  460. }
  461. ACPI_EXPORT_SYMBOL(acpi_remove_table_handler)