tbinstal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /******************************************************************************
  2. *
  3. * Module Name: tbinstal - ACPI table installation and removal
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  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 <acpi/actables.h>
  44. #define _COMPONENT ACPI_TABLES
  45. ACPI_MODULE_NAME("tbinstal")
  46. /* Local prototypes */
  47. static acpi_status
  48. acpi_tb_match_signature(char *signature,
  49. struct acpi_table_desc *table_info, u8 search_type);
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_tb_match_signature
  53. *
  54. * PARAMETERS: Signature - Table signature to match
  55. * table_info - Return data
  56. * search_type - Table type to match (primary/secondary)
  57. *
  58. * RETURN: Status
  59. *
  60. * DESCRIPTION: Compare signature against the list of "ACPI-subsystem-owned"
  61. * tables (DSDT/FADT/SSDT, etc.) Returns the table_type_iD on match.
  62. *
  63. ******************************************************************************/
  64. static acpi_status
  65. acpi_tb_match_signature(char *signature,
  66. struct acpi_table_desc *table_info, u8 search_type)
  67. {
  68. acpi_native_uint i;
  69. ACPI_FUNCTION_TRACE("tb_match_signature");
  70. /* Search for a signature match among the known table types */
  71. for (i = 0; i < NUM_ACPI_TABLE_TYPES; i++) {
  72. if (!(acpi_gbl_table_data[i].flags & search_type)) {
  73. continue;
  74. }
  75. if (!ACPI_STRNCMP(signature, acpi_gbl_table_data[i].signature,
  76. acpi_gbl_table_data[i].sig_length)) {
  77. /* Found a signature match, return index if requested */
  78. if (table_info) {
  79. table_info->type = (u8) i;
  80. }
  81. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  82. "Table [%4.4s] is an ACPI table consumed by the core subsystem\n",
  83. (char *)acpi_gbl_table_data[i].
  84. signature));
  85. return_ACPI_STATUS(AE_OK);
  86. }
  87. }
  88. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  89. "Table [%4.4s] is not an ACPI table consumed by the core subsystem - ignored\n",
  90. (char *)signature));
  91. return_ACPI_STATUS(AE_TABLE_NOT_SUPPORTED);
  92. }
  93. /*******************************************************************************
  94. *
  95. * FUNCTION: acpi_tb_install_table
  96. *
  97. * PARAMETERS: table_info - Return value from acpi_tb_get_table_body
  98. *
  99. * RETURN: Status
  100. *
  101. * DESCRIPTION: Install the table into the global data structures.
  102. *
  103. ******************************************************************************/
  104. acpi_status acpi_tb_install_table(struct acpi_table_desc *table_info)
  105. {
  106. acpi_status status;
  107. ACPI_FUNCTION_TRACE("tb_install_table");
  108. /* Lock tables while installing */
  109. status = acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
  110. if (ACPI_FAILURE(status)) {
  111. ACPI_EXCEPTION((AE_INFO, status,
  112. "Could not acquire table mutex"));
  113. return_ACPI_STATUS(status);
  114. }
  115. /*
  116. * Ignore a table that is already installed. For example, some BIOS
  117. * ASL code will repeatedly attempt to load the same SSDT.
  118. */
  119. status = acpi_tb_is_table_installed(table_info);
  120. if (ACPI_FAILURE(status)) {
  121. goto unlock_and_exit;
  122. }
  123. /* Install the table into the global data structure */
  124. status = acpi_tb_init_table_descriptor(table_info->type, table_info);
  125. if (ACPI_FAILURE(status)) {
  126. ACPI_EXCEPTION((AE_INFO, status,
  127. "Could not install table [%4.4s]",
  128. table_info->pointer->signature));
  129. }
  130. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s located at %p\n",
  131. acpi_gbl_table_data[table_info->type].name,
  132. table_info->pointer));
  133. unlock_and_exit:
  134. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  135. return_ACPI_STATUS(status);
  136. }
  137. /*******************************************************************************
  138. *
  139. * FUNCTION: acpi_tb_recognize_table
  140. *
  141. * PARAMETERS: table_info - Return value from acpi_tb_get_table_body
  142. * search_type - Table type to match (primary/secondary)
  143. *
  144. * RETURN: Status
  145. *
  146. * DESCRIPTION: Check a table signature for a match against known table types
  147. *
  148. * NOTE: All table pointers are validated as follows:
  149. * 1) Table pointer must point to valid physical memory
  150. * 2) Signature must be 4 ASCII chars, even if we don't recognize the
  151. * name
  152. * 3) Table must be readable for length specified in the header
  153. * 4) Table checksum must be valid (with the exception of the FACS
  154. * which has no checksum for some odd reason)
  155. *
  156. ******************************************************************************/
  157. acpi_status
  158. acpi_tb_recognize_table(struct acpi_table_desc *table_info, u8 search_type)
  159. {
  160. struct acpi_table_header *table_header;
  161. acpi_status status;
  162. ACPI_FUNCTION_TRACE("tb_recognize_table");
  163. /* Ensure that we have a valid table pointer */
  164. table_header = (struct acpi_table_header *)table_info->pointer;
  165. if (!table_header) {
  166. return_ACPI_STATUS(AE_BAD_PARAMETER);
  167. }
  168. /*
  169. * We only "recognize" a limited number of ACPI tables -- namely, the
  170. * ones that are used by the subsystem (DSDT, FADT, etc.)
  171. *
  172. * An AE_TABLE_NOT_SUPPORTED means that the table was not recognized.
  173. * This can be any one of many valid ACPI tables, it just isn't one of
  174. * the tables that is consumed by the core subsystem
  175. */
  176. status = acpi_tb_match_signature(table_header->signature,
  177. table_info, search_type);
  178. if (ACPI_FAILURE(status)) {
  179. return_ACPI_STATUS(status);
  180. }
  181. status = acpi_tb_validate_table_header(table_header);
  182. if (ACPI_FAILURE(status)) {
  183. return_ACPI_STATUS(status);
  184. }
  185. /* Return the table type and length via the info struct */
  186. table_info->length = (acpi_size) table_header->length;
  187. return_ACPI_STATUS(status);
  188. }
  189. /*******************************************************************************
  190. *
  191. * FUNCTION: acpi_tb_init_table_descriptor
  192. *
  193. * PARAMETERS: table_type - The type of the table
  194. * table_info - A table info struct
  195. *
  196. * RETURN: None.
  197. *
  198. * DESCRIPTION: Install a table into the global data structs.
  199. *
  200. ******************************************************************************/
  201. acpi_status
  202. acpi_tb_init_table_descriptor(acpi_table_type table_type,
  203. struct acpi_table_desc *table_info)
  204. {
  205. struct acpi_table_list *list_head;
  206. struct acpi_table_desc *table_desc;
  207. acpi_status status;
  208. ACPI_FUNCTION_TRACE_U32("tb_init_table_descriptor", table_type);
  209. /* Allocate a descriptor for this table */
  210. table_desc = ACPI_MEM_CALLOCATE(sizeof(struct acpi_table_desc));
  211. if (!table_desc) {
  212. return_ACPI_STATUS(AE_NO_MEMORY);
  213. }
  214. /* Get a new owner ID for the table */
  215. status = acpi_ut_allocate_owner_id(&table_desc->owner_id);
  216. if (ACPI_FAILURE(status)) {
  217. return_ACPI_STATUS(status);
  218. }
  219. /* Install the table into the global data structure */
  220. list_head = &acpi_gbl_table_lists[table_type];
  221. /*
  222. * Two major types of tables: 1) Only one instance is allowed. This
  223. * includes most ACPI tables such as the DSDT. 2) Multiple instances of
  224. * the table are allowed. This includes SSDT and PSDTs.
  225. */
  226. if (ACPI_IS_SINGLE_TABLE(acpi_gbl_table_data[table_type].flags)) {
  227. /*
  228. * Only one table allowed, and a table has alread been installed
  229. * at this location, so return an error.
  230. */
  231. if (list_head->next) {
  232. ACPI_MEM_FREE(table_desc);
  233. return_ACPI_STATUS(AE_ALREADY_EXISTS);
  234. }
  235. table_desc->next = list_head->next;
  236. list_head->next = table_desc;
  237. if (table_desc->next) {
  238. table_desc->next->prev = table_desc;
  239. }
  240. list_head->count++;
  241. } else {
  242. /*
  243. * Link the new table in to the list of tables of this type.
  244. * Insert at the end of the list, order IS IMPORTANT.
  245. *
  246. * table_desc->Prev & Next are already NULL from calloc()
  247. */
  248. list_head->count++;
  249. if (!list_head->next) {
  250. list_head->next = table_desc;
  251. } else {
  252. table_desc->next = list_head->next;
  253. while (table_desc->next->next) {
  254. table_desc->next = table_desc->next->next;
  255. }
  256. table_desc->next->next = table_desc;
  257. table_desc->prev = table_desc->next;
  258. table_desc->next = NULL;
  259. }
  260. }
  261. /* Finish initialization of the table descriptor */
  262. table_desc->type = (u8) table_type;
  263. table_desc->pointer = table_info->pointer;
  264. table_desc->length = table_info->length;
  265. table_desc->allocation = table_info->allocation;
  266. table_desc->aml_start = (u8 *) (table_desc->pointer + 1),
  267. table_desc->aml_length = (u32) (table_desc->length -
  268. (u32) sizeof(struct
  269. acpi_table_header));
  270. table_desc->loaded_into_namespace = FALSE;
  271. /*
  272. * Set the appropriate global pointer (if there is one) to point to the
  273. * newly installed table
  274. */
  275. if (acpi_gbl_table_data[table_type].global_ptr) {
  276. *(acpi_gbl_table_data[table_type].global_ptr) =
  277. table_info->pointer;
  278. }
  279. /* Return Data */
  280. table_info->owner_id = table_desc->owner_id;
  281. table_info->installed_desc = table_desc;
  282. return_ACPI_STATUS(AE_OK);
  283. }
  284. /*******************************************************************************
  285. *
  286. * FUNCTION: acpi_tb_delete_all_tables
  287. *
  288. * PARAMETERS: None.
  289. *
  290. * RETURN: None.
  291. *
  292. * DESCRIPTION: Delete all internal ACPI tables
  293. *
  294. ******************************************************************************/
  295. void acpi_tb_delete_all_tables(void)
  296. {
  297. acpi_table_type type;
  298. /*
  299. * Free memory allocated for ACPI tables
  300. * Memory can either be mapped or allocated
  301. */
  302. for (type = 0; type < NUM_ACPI_TABLE_TYPES; type++) {
  303. acpi_tb_delete_tables_by_type(type);
  304. }
  305. }
  306. /*******************************************************************************
  307. *
  308. * FUNCTION: acpi_tb_delete_tables_by_type
  309. *
  310. * PARAMETERS: Type - The table type to be deleted
  311. *
  312. * RETURN: None.
  313. *
  314. * DESCRIPTION: Delete an internal ACPI table
  315. * Locks the ACPI table mutex
  316. *
  317. ******************************************************************************/
  318. void acpi_tb_delete_tables_by_type(acpi_table_type type)
  319. {
  320. struct acpi_table_desc *table_desc;
  321. u32 count;
  322. u32 i;
  323. ACPI_FUNCTION_TRACE_U32("tb_delete_tables_by_type", type);
  324. if (type > ACPI_TABLE_MAX) {
  325. return_VOID;
  326. }
  327. if (ACPI_FAILURE(acpi_ut_acquire_mutex(ACPI_MTX_TABLES))) {
  328. return;
  329. }
  330. /* Clear the appropriate "typed" global table pointer */
  331. switch (type) {
  332. case ACPI_TABLE_RSDP:
  333. acpi_gbl_RSDP = NULL;
  334. break;
  335. case ACPI_TABLE_DSDT:
  336. acpi_gbl_DSDT = NULL;
  337. break;
  338. case ACPI_TABLE_FADT:
  339. acpi_gbl_FADT = NULL;
  340. break;
  341. case ACPI_TABLE_FACS:
  342. acpi_gbl_FACS = NULL;
  343. break;
  344. case ACPI_TABLE_XSDT:
  345. acpi_gbl_XSDT = NULL;
  346. break;
  347. case ACPI_TABLE_SSDT:
  348. case ACPI_TABLE_PSDT:
  349. default:
  350. break;
  351. }
  352. /*
  353. * Free the table
  354. * 1) Get the head of the list
  355. */
  356. table_desc = acpi_gbl_table_lists[type].next;
  357. count = acpi_gbl_table_lists[type].count;
  358. /*
  359. * 2) Walk the entire list, deleting both the allocated tables
  360. * and the table descriptors
  361. */
  362. for (i = 0; i < count; i++) {
  363. table_desc = acpi_tb_uninstall_table(table_desc);
  364. }
  365. (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
  366. return_VOID;
  367. }
  368. /*******************************************************************************
  369. *
  370. * FUNCTION: acpi_tb_delete_single_table
  371. *
  372. * PARAMETERS: table_info - A table info struct
  373. *
  374. * RETURN: None.
  375. *
  376. * DESCRIPTION: Low-level free for a single ACPI table. Handles cases where
  377. * the table was allocated a buffer or was mapped.
  378. *
  379. ******************************************************************************/
  380. void acpi_tb_delete_single_table(struct acpi_table_desc *table_desc)
  381. {
  382. /* Must have a valid table descriptor and pointer */
  383. if ((!table_desc) || (!table_desc->pointer)) {
  384. return;
  385. }
  386. /* Valid table, determine type of memory allocation */
  387. switch (table_desc->allocation) {
  388. case ACPI_MEM_NOT_ALLOCATED:
  389. break;
  390. case ACPI_MEM_ALLOCATED:
  391. ACPI_MEM_FREE(table_desc->pointer);
  392. break;
  393. case ACPI_MEM_MAPPED:
  394. acpi_os_unmap_memory(table_desc->pointer, table_desc->length);
  395. break;
  396. default:
  397. break;
  398. }
  399. }
  400. /*******************************************************************************
  401. *
  402. * FUNCTION: acpi_tb_uninstall_table
  403. *
  404. * PARAMETERS: table_info - A table info struct
  405. *
  406. * RETURN: Pointer to the next table in the list (of same type)
  407. *
  408. * DESCRIPTION: Free the memory associated with an internal ACPI table that
  409. * is either installed or has never been installed.
  410. * Table mutex should be locked.
  411. *
  412. ******************************************************************************/
  413. struct acpi_table_desc *acpi_tb_uninstall_table(struct acpi_table_desc
  414. *table_desc)
  415. {
  416. struct acpi_table_desc *next_desc;
  417. ACPI_FUNCTION_TRACE_PTR("tb_uninstall_table", table_desc);
  418. if (!table_desc) {
  419. return_PTR(NULL);
  420. }
  421. /* Unlink the descriptor from the doubly linked list */
  422. if (table_desc->prev) {
  423. table_desc->prev->next = table_desc->next;
  424. } else {
  425. /* Is first on list, update list head */
  426. acpi_gbl_table_lists[table_desc->type].next = table_desc->next;
  427. }
  428. if (table_desc->next) {
  429. table_desc->next->prev = table_desc->prev;
  430. }
  431. /* Free the memory allocated for the table itself */
  432. acpi_tb_delete_single_table(table_desc);
  433. /* Free the table descriptor */
  434. next_desc = table_desc->next;
  435. ACPI_MEM_FREE(table_desc);
  436. /* Return pointer to the next descriptor */
  437. return_PTR(next_desc);
  438. }