ACPICA: Allow same ACPI table to be loaded/unloaded more than once
[linux-2.6/mini2440.git] / drivers / acpi / tables / tbinstal.c
blob905dc38ab23b3528d3fbbcaa4f54408e09125383
1 /******************************************************************************
3 * Module Name: tbinstal - ACPI table installation and removal
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2008, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
45 #include <acpi/acnamesp.h>
46 #include <acpi/actables.h>
48 #define _COMPONENT ACPI_TABLES
49 ACPI_MODULE_NAME("tbinstal")
51 /******************************************************************************
53 * FUNCTION: acpi_tb_verify_table
55 * PARAMETERS: table_desc - table
57 * RETURN: Status
59 * DESCRIPTION: this function is called to verify and map table
61 *****************************************************************************/
62 acpi_status acpi_tb_verify_table(struct acpi_table_desc *table_desc)
64 acpi_status status = AE_OK;
66 ACPI_FUNCTION_TRACE(tb_verify_table);
68 /* Map the table if necessary */
70 if (!table_desc->pointer) {
71 if ((table_desc->flags & ACPI_TABLE_ORIGIN_MASK) ==
72 ACPI_TABLE_ORIGIN_MAPPED) {
73 table_desc->pointer =
74 acpi_os_map_memory(table_desc->address,
75 table_desc->length);
77 if (!table_desc->pointer) {
78 return_ACPI_STATUS(AE_NO_MEMORY);
82 /* FACS is the odd table, has no standard ACPI header and no checksum */
84 if (!ACPI_COMPARE_NAME(&table_desc->signature, ACPI_SIG_FACS)) {
86 /* Always calculate checksum, ignore bad checksum if requested */
88 status =
89 acpi_tb_verify_checksum(table_desc->pointer,
90 table_desc->length);
93 return_ACPI_STATUS(status);
96 /*******************************************************************************
98 * FUNCTION: acpi_tb_add_table
100 * PARAMETERS: table_desc - Table descriptor
101 * table_index - Where the table index is returned
103 * RETURN: Status
105 * DESCRIPTION: This function is called to add the ACPI table
107 ******************************************************************************/
109 acpi_status
110 acpi_tb_add_table(struct acpi_table_desc *table_desc, u32 *table_index)
112 u32 i;
113 u32 length;
114 acpi_status status = AE_OK;
116 ACPI_FUNCTION_TRACE(tb_add_table);
118 if (!table_desc->pointer) {
119 status = acpi_tb_verify_table(table_desc);
120 if (ACPI_FAILURE(status) || !table_desc->pointer) {
121 return_ACPI_STATUS(status);
126 * Originally, we checked the table signature for "SSDT" or "PSDT" here.
127 * Next, we added support for OEMx tables, signature "OEM".
128 * Valid tables were encountered with a null signature, so we've just
129 * given up on validating the signature, since it seems to be a waste
130 * of code. The original code was removed (05/2008).
133 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
135 /* Check if table is already registered */
137 for (i = 0; i < acpi_gbl_root_table_list.count; ++i) {
138 if (!acpi_gbl_root_table_list.tables[i].pointer) {
139 status =
140 acpi_tb_verify_table(&acpi_gbl_root_table_list.
141 tables[i]);
142 if (ACPI_FAILURE(status)
143 || !acpi_gbl_root_table_list.tables[i].pointer) {
144 continue;
148 /* Check for a table match on the entire table length */
150 length = ACPI_MIN(table_desc->length,
151 acpi_gbl_root_table_list.tables[i].length);
152 if (ACPI_MEMCMP(table_desc->pointer,
153 acpi_gbl_root_table_list.tables[i].pointer,
154 length)) {
155 continue;
159 * Note: the current mechanism does not unregister a table if it is
160 * dynamically unloaded. The related namespace entries are deleted,
161 * but the table remains in the root table list.
163 * The assumption here is that the number of different tables that
164 * will be loaded is actually small, and there is minimal overhead
165 * in just keeping the table in case it is needed again.
167 * If this assumption changes in the future (perhaps on large
168 * machines with many table load/unload operations), tables will
169 * need to be unregistered when they are unloaded, and slots in the
170 * root table list should be reused when empty.
174 * Table is already registered.
175 * We can delete the table that was passed as a parameter.
177 acpi_tb_delete_table(table_desc);
178 *table_index = i;
180 if (acpi_gbl_root_table_list.tables[i].
181 flags & ACPI_TABLE_IS_LOADED) {
183 /* Table is still loaded, this is an error */
185 status = AE_ALREADY_EXISTS;
186 goto release;
187 } else {
188 /* Table was unloaded, allow it to be reloaded */
190 table_desc->pointer =
191 acpi_gbl_root_table_list.tables[i].pointer;
192 table_desc->address =
193 acpi_gbl_root_table_list.tables[i].address;
194 status = AE_OK;
195 goto print_header;
199 /* Add the table to the global root table list */
201 status = acpi_tb_store_table(table_desc->address, table_desc->pointer,
202 table_desc->length, table_desc->flags,
203 table_index);
204 if (ACPI_FAILURE(status)) {
205 goto release;
208 print_header:
209 acpi_tb_print_table_header(table_desc->address, table_desc->pointer);
211 release:
212 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
213 return_ACPI_STATUS(status);
216 /*******************************************************************************
218 * FUNCTION: acpi_tb_resize_root_table_list
220 * PARAMETERS: None
222 * RETURN: Status
224 * DESCRIPTION: Expand the size of global table array
226 ******************************************************************************/
228 acpi_status acpi_tb_resize_root_table_list(void)
230 struct acpi_table_desc *tables;
232 ACPI_FUNCTION_TRACE(tb_resize_root_table_list);
234 /* allow_resize flag is a parameter to acpi_initialize_tables */
236 if (!(acpi_gbl_root_table_list.flags & ACPI_ROOT_ALLOW_RESIZE)) {
237 ACPI_ERROR((AE_INFO,
238 "Resize of Root Table Array is not allowed"));
239 return_ACPI_STATUS(AE_SUPPORT);
242 /* Increase the Table Array size */
244 tables = ACPI_ALLOCATE_ZEROED(((acpi_size) acpi_gbl_root_table_list.
245 size + ACPI_ROOT_TABLE_SIZE_INCREMENT)
246 * sizeof(struct acpi_table_desc));
247 if (!tables) {
248 ACPI_ERROR((AE_INFO,
249 "Could not allocate new root table array"));
250 return_ACPI_STATUS(AE_NO_MEMORY);
253 /* Copy and free the previous table array */
255 if (acpi_gbl_root_table_list.tables) {
256 ACPI_MEMCPY(tables, acpi_gbl_root_table_list.tables,
257 (acpi_size) acpi_gbl_root_table_list.size *
258 sizeof(struct acpi_table_desc));
260 if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
261 ACPI_FREE(acpi_gbl_root_table_list.tables);
265 acpi_gbl_root_table_list.tables = tables;
266 acpi_gbl_root_table_list.size += ACPI_ROOT_TABLE_SIZE_INCREMENT;
267 acpi_gbl_root_table_list.flags |= (u8) ACPI_ROOT_ORIGIN_ALLOCATED;
269 return_ACPI_STATUS(AE_OK);
272 /*******************************************************************************
274 * FUNCTION: acpi_tb_store_table
276 * PARAMETERS: Address - Table address
277 * Table - Table header
278 * Length - Table length
279 * Flags - flags
281 * RETURN: Status and table index.
283 * DESCRIPTION: Add an ACPI table to the global table list
285 ******************************************************************************/
287 acpi_status
288 acpi_tb_store_table(acpi_physical_address address,
289 struct acpi_table_header *table,
290 u32 length, u8 flags, u32 *table_index)
292 acpi_status status = AE_OK;
294 /* Ensure that there is room for the table in the Root Table List */
296 if (acpi_gbl_root_table_list.count >= acpi_gbl_root_table_list.size) {
297 status = acpi_tb_resize_root_table_list();
298 if (ACPI_FAILURE(status)) {
299 return (status);
303 /* Initialize added table */
305 acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
306 address = address;
307 acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
308 pointer = table;
309 acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].length =
310 length;
311 acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
312 owner_id = 0;
313 acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].flags =
314 flags;
316 ACPI_MOVE_32_TO_32(&
317 (acpi_gbl_root_table_list.
318 tables[acpi_gbl_root_table_list.count].signature),
319 table->signature);
321 *table_index = acpi_gbl_root_table_list.count;
322 acpi_gbl_root_table_list.count++;
323 return (status);
326 /*******************************************************************************
328 * FUNCTION: acpi_tb_delete_table
330 * PARAMETERS: table_index - Table index
332 * RETURN: None
334 * DESCRIPTION: Delete one internal ACPI table
336 ******************************************************************************/
338 void acpi_tb_delete_table(struct acpi_table_desc *table_desc)
340 /* Table must be mapped or allocated */
341 if (!table_desc->pointer) {
342 return;
344 switch (table_desc->flags & ACPI_TABLE_ORIGIN_MASK) {
345 case ACPI_TABLE_ORIGIN_MAPPED:
346 acpi_os_unmap_memory(table_desc->pointer, table_desc->length);
347 break;
348 case ACPI_TABLE_ORIGIN_ALLOCATED:
349 ACPI_FREE(table_desc->pointer);
350 break;
351 default:;
354 table_desc->pointer = NULL;
357 /*******************************************************************************
359 * FUNCTION: acpi_tb_terminate
361 * PARAMETERS: None
363 * RETURN: None
365 * DESCRIPTION: Delete all internal ACPI tables
367 ******************************************************************************/
369 void acpi_tb_terminate(void)
371 u32 i;
373 ACPI_FUNCTION_TRACE(tb_terminate);
375 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
377 /* Delete the individual tables */
379 for (i = 0; i < acpi_gbl_root_table_list.count; ++i) {
380 acpi_tb_delete_table(&acpi_gbl_root_table_list.tables[i]);
384 * Delete the root table array if allocated locally. Array cannot be
385 * mapped, so we don't need to check for that flag.
387 if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
388 ACPI_FREE(acpi_gbl_root_table_list.tables);
391 acpi_gbl_root_table_list.tables = NULL;
392 acpi_gbl_root_table_list.flags = 0;
393 acpi_gbl_root_table_list.count = 0;
395 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "ACPI Tables freed\n"));
396 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
399 /*******************************************************************************
401 * FUNCTION: acpi_tb_delete_namespace_by_owner
403 * PARAMETERS: table_index - Table index
405 * RETURN: None
407 * DESCRIPTION: Delete all namespace objects created when this table was loaded.
409 ******************************************************************************/
411 void acpi_tb_delete_namespace_by_owner(u32 table_index)
413 acpi_owner_id owner_id;
415 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
416 if (table_index < acpi_gbl_root_table_list.count) {
417 owner_id =
418 acpi_gbl_root_table_list.tables[table_index].owner_id;
419 } else {
420 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
421 return;
424 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
425 acpi_ns_delete_namespace_by_owner(owner_id);
428 /*******************************************************************************
430 * FUNCTION: acpi_tb_allocate_owner_id
432 * PARAMETERS: table_index - Table index
434 * RETURN: Status
436 * DESCRIPTION: Allocates owner_id in table_desc
438 ******************************************************************************/
440 acpi_status acpi_tb_allocate_owner_id(u32 table_index)
442 acpi_status status = AE_BAD_PARAMETER;
444 ACPI_FUNCTION_TRACE(tb_allocate_owner_id);
446 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
447 if (table_index < acpi_gbl_root_table_list.count) {
448 status = acpi_ut_allocate_owner_id
449 (&(acpi_gbl_root_table_list.tables[table_index].owner_id));
452 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
453 return_ACPI_STATUS(status);
456 /*******************************************************************************
458 * FUNCTION: acpi_tb_release_owner_id
460 * PARAMETERS: table_index - Table index
462 * RETURN: Status
464 * DESCRIPTION: Releases owner_id in table_desc
466 ******************************************************************************/
468 acpi_status acpi_tb_release_owner_id(u32 table_index)
470 acpi_status status = AE_BAD_PARAMETER;
472 ACPI_FUNCTION_TRACE(tb_release_owner_id);
474 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
475 if (table_index < acpi_gbl_root_table_list.count) {
476 acpi_ut_release_owner_id(&
477 (acpi_gbl_root_table_list.
478 tables[table_index].owner_id));
479 status = AE_OK;
482 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
483 return_ACPI_STATUS(status);
486 /*******************************************************************************
488 * FUNCTION: acpi_tb_get_owner_id
490 * PARAMETERS: table_index - Table index
491 * owner_id - Where the table owner_id is returned
493 * RETURN: Status
495 * DESCRIPTION: returns owner_id for the ACPI table
497 ******************************************************************************/
499 acpi_status acpi_tb_get_owner_id(u32 table_index, acpi_owner_id *owner_id)
501 acpi_status status = AE_BAD_PARAMETER;
503 ACPI_FUNCTION_TRACE(tb_get_owner_id);
505 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
506 if (table_index < acpi_gbl_root_table_list.count) {
507 *owner_id =
508 acpi_gbl_root_table_list.tables[table_index].owner_id;
509 status = AE_OK;
512 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
513 return_ACPI_STATUS(status);
516 /*******************************************************************************
518 * FUNCTION: acpi_tb_is_table_loaded
520 * PARAMETERS: table_index - Table index
522 * RETURN: Table Loaded Flag
524 ******************************************************************************/
526 u8 acpi_tb_is_table_loaded(u32 table_index)
528 u8 is_loaded = FALSE;
530 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
531 if (table_index < acpi_gbl_root_table_list.count) {
532 is_loaded = (u8)
533 (acpi_gbl_root_table_list.tables[table_index].
534 flags & ACPI_TABLE_IS_LOADED);
537 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
538 return (is_loaded);
541 /*******************************************************************************
543 * FUNCTION: acpi_tb_set_table_loaded_flag
545 * PARAMETERS: table_index - Table index
546 * is_loaded - TRUE if table is loaded, FALSE otherwise
548 * RETURN: None
550 * DESCRIPTION: Sets the table loaded flag to either TRUE or FALSE.
552 ******************************************************************************/
554 void acpi_tb_set_table_loaded_flag(u32 table_index, u8 is_loaded)
557 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
558 if (table_index < acpi_gbl_root_table_list.count) {
559 if (is_loaded) {
560 acpi_gbl_root_table_list.tables[table_index].flags |=
561 ACPI_TABLE_IS_LOADED;
562 } else {
563 acpi_gbl_root_table_list.tables[table_index].flags &=
564 ~ACPI_TABLE_IS_LOADED;
568 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);