xref: /openbmc/linux/drivers/acpi/acpica/tbinstal.c (revision c8cefe307d79c57b32ca1d4c9ebff528f8dd914c)
195b482a8SLen Brown /******************************************************************************
295b482a8SLen Brown  *
395b482a8SLen Brown  * Module Name: tbinstal - ACPI table installation and removal
495b482a8SLen Brown  *
595b482a8SLen Brown  *****************************************************************************/
695b482a8SLen Brown 
795b482a8SLen Brown /*
8b4e104eaSBob Moore  * Copyright (C) 2000 - 2011, Intel Corp.
995b482a8SLen Brown  * All rights reserved.
1095b482a8SLen Brown  *
1195b482a8SLen Brown  * Redistribution and use in source and binary forms, with or without
1295b482a8SLen Brown  * modification, are permitted provided that the following conditions
1395b482a8SLen Brown  * are met:
1495b482a8SLen Brown  * 1. Redistributions of source code must retain the above copyright
1595b482a8SLen Brown  *    notice, this list of conditions, and the following disclaimer,
1695b482a8SLen Brown  *    without modification.
1795b482a8SLen Brown  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1895b482a8SLen Brown  *    substantially similar to the "NO WARRANTY" disclaimer below
1995b482a8SLen Brown  *    ("Disclaimer") and any redistribution must be conditioned upon
2095b482a8SLen Brown  *    including a substantially similar Disclaimer requirement for further
2195b482a8SLen Brown  *    binary redistribution.
2295b482a8SLen Brown  * 3. Neither the names of the above-listed copyright holders nor the names
2395b482a8SLen Brown  *    of any contributors may be used to endorse or promote products derived
2495b482a8SLen Brown  *    from this software without specific prior written permission.
2595b482a8SLen Brown  *
2695b482a8SLen Brown  * Alternatively, this software may be distributed under the terms of the
2795b482a8SLen Brown  * GNU General Public License ("GPL") version 2 as published by the Free
2895b482a8SLen Brown  * Software Foundation.
2995b482a8SLen Brown  *
3095b482a8SLen Brown  * NO WARRANTY
3195b482a8SLen Brown  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3295b482a8SLen Brown  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3395b482a8SLen Brown  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3495b482a8SLen Brown  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3595b482a8SLen Brown  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3695b482a8SLen Brown  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3795b482a8SLen Brown  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3895b482a8SLen Brown  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3995b482a8SLen Brown  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4095b482a8SLen Brown  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4195b482a8SLen Brown  * POSSIBILITY OF SUCH DAMAGES.
4295b482a8SLen Brown  */
4395b482a8SLen Brown 
4495b482a8SLen Brown #include <acpi/acpi.h>
45e2f7a777SLen Brown #include "accommon.h"
46e2f7a777SLen Brown #include "acnamesp.h"
47e2f7a777SLen Brown #include "actables.h"
4895b482a8SLen Brown 
4995b482a8SLen Brown #define _COMPONENT          ACPI_TABLES
5095b482a8SLen Brown ACPI_MODULE_NAME("tbinstal")
5195b482a8SLen Brown 
5295b482a8SLen Brown /******************************************************************************
5395b482a8SLen Brown  *
5495b482a8SLen Brown  * FUNCTION:    acpi_tb_verify_table
5595b482a8SLen Brown  *
5695b482a8SLen Brown  * PARAMETERS:  table_desc          - table
5795b482a8SLen Brown  *
5895b482a8SLen Brown  * RETURN:      Status
5995b482a8SLen Brown  *
6095b482a8SLen Brown  * DESCRIPTION: this function is called to verify and map table
6195b482a8SLen Brown  *
6295b482a8SLen Brown  *****************************************************************************/
6395b482a8SLen Brown acpi_status acpi_tb_verify_table(struct acpi_table_desc *table_desc)
6495b482a8SLen Brown {
6595b482a8SLen Brown 	acpi_status status = AE_OK;
6695b482a8SLen Brown 
6795b482a8SLen Brown 	ACPI_FUNCTION_TRACE(tb_verify_table);
6895b482a8SLen Brown 
6995b482a8SLen Brown 	/* Map the table if necessary */
7095b482a8SLen Brown 
7195b482a8SLen Brown 	if (!table_desc->pointer) {
7295b482a8SLen Brown 		if ((table_desc->flags & ACPI_TABLE_ORIGIN_MASK) ==
7395b482a8SLen Brown 		    ACPI_TABLE_ORIGIN_MAPPED) {
7495b482a8SLen Brown 			table_desc->pointer =
7595b482a8SLen Brown 			    acpi_os_map_memory(table_desc->address,
7695b482a8SLen Brown 					       table_desc->length);
7795b482a8SLen Brown 		}
7895b482a8SLen Brown 		if (!table_desc->pointer) {
7995b482a8SLen Brown 			return_ACPI_STATUS(AE_NO_MEMORY);
8095b482a8SLen Brown 		}
8195b482a8SLen Brown 	}
8295b482a8SLen Brown 
8395b482a8SLen Brown 	/* FACS is the odd table, has no standard ACPI header and no checksum */
8495b482a8SLen Brown 
8595b482a8SLen Brown 	if (!ACPI_COMPARE_NAME(&table_desc->signature, ACPI_SIG_FACS)) {
8695b482a8SLen Brown 
8795b482a8SLen Brown 		/* Always calculate checksum, ignore bad checksum if requested */
8895b482a8SLen Brown 
8995b482a8SLen Brown 		status =
9095b482a8SLen Brown 		    acpi_tb_verify_checksum(table_desc->pointer,
9195b482a8SLen Brown 					    table_desc->length);
9295b482a8SLen Brown 	}
9395b482a8SLen Brown 
9495b482a8SLen Brown 	return_ACPI_STATUS(status);
9595b482a8SLen Brown }
9695b482a8SLen Brown 
9795b482a8SLen Brown /*******************************************************************************
9895b482a8SLen Brown  *
9995b482a8SLen Brown  * FUNCTION:    acpi_tb_add_table
10095b482a8SLen Brown  *
10195b482a8SLen Brown  * PARAMETERS:  table_desc          - Table descriptor
10295b482a8SLen Brown  *              table_index         - Where the table index is returned
10395b482a8SLen Brown  *
10495b482a8SLen Brown  * RETURN:      Status
10595b482a8SLen Brown  *
106d3ccaff8SBob Moore  * DESCRIPTION: This function is called to add an ACPI table. It is used to
107d3ccaff8SBob Moore  *              dynamically load tables via the Load and load_table AML
108d3ccaff8SBob Moore  *              operators.
10995b482a8SLen Brown  *
11095b482a8SLen Brown  ******************************************************************************/
11195b482a8SLen Brown 
11295b482a8SLen Brown acpi_status
11395b482a8SLen Brown acpi_tb_add_table(struct acpi_table_desc *table_desc, u32 *table_index)
11495b482a8SLen Brown {
11595b482a8SLen Brown 	u32 i;
11695b482a8SLen Brown 	acpi_status status = AE_OK;
117d3ccaff8SBob Moore 	struct acpi_table_header *override_table = NULL;
11895b482a8SLen Brown 
11995b482a8SLen Brown 	ACPI_FUNCTION_TRACE(tb_add_table);
12095b482a8SLen Brown 
12195b482a8SLen Brown 	if (!table_desc->pointer) {
12295b482a8SLen Brown 		status = acpi_tb_verify_table(table_desc);
12395b482a8SLen Brown 		if (ACPI_FAILURE(status) || !table_desc->pointer) {
12495b482a8SLen Brown 			return_ACPI_STATUS(status);
12595b482a8SLen Brown 		}
12695b482a8SLen Brown 	}
12795b482a8SLen Brown 
12895b482a8SLen Brown 	/*
129*c8cefe30SBob Moore 	 * Validate the incoming table signature.
130*c8cefe30SBob Moore 	 *
131*c8cefe30SBob Moore 	 * 1) Originally, we checked the table signature for "SSDT" or "PSDT".
132*c8cefe30SBob Moore 	 * 2) We added support for OEMx tables, signature "OEM".
133*c8cefe30SBob Moore 	 * 3) Valid tables were encountered with a null signature, so we just
134*c8cefe30SBob Moore 	 *    gave up on validating the signature, (05/2008).
135*c8cefe30SBob Moore 	 * 4) We encountered non-AML tables such as the MADT, which caused
136*c8cefe30SBob Moore 	 *    interpreter errors and kernel faults. So now, we once again allow
137*c8cefe30SBob Moore 	 *    only "SSDT", "OEMx", and now, also a null signature. (05/2011).
13895b482a8SLen Brown 	 */
139*c8cefe30SBob Moore 	if ((table_desc->pointer->signature[0] != 0x00) &&
140*c8cefe30SBob Moore 	    (!ACPI_COMPARE_NAME(table_desc->pointer->signature, ACPI_SIG_SSDT))
141*c8cefe30SBob Moore 	    && (ACPI_STRNCMP(table_desc->pointer->signature, "OEM", 3))) {
142*c8cefe30SBob Moore 		ACPI_ERROR((AE_INFO,
143*c8cefe30SBob Moore 			    "Table has invalid signature [%4.4s] (0x%8.8X), must be SSDT or OEMx",
144*c8cefe30SBob Moore 			    acpi_ut_valid_acpi_name(*(u32 *)table_desc->
145*c8cefe30SBob Moore 						    pointer->
146*c8cefe30SBob Moore 						    signature) ? table_desc->
147*c8cefe30SBob Moore 			    pointer->signature : "????",
148*c8cefe30SBob Moore 			    *(u32 *)table_desc->pointer->signature));
149*c8cefe30SBob Moore 
150*c8cefe30SBob Moore 		return_ACPI_STATUS(AE_BAD_SIGNATURE);
151*c8cefe30SBob Moore 	}
15295b482a8SLen Brown 
15395b482a8SLen Brown 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
15495b482a8SLen Brown 
15595b482a8SLen Brown 	/* Check if table is already registered */
15695b482a8SLen Brown 
157b9ee2043SBob Moore 	for (i = 0; i < acpi_gbl_root_table_list.current_table_count; ++i) {
15895b482a8SLen Brown 		if (!acpi_gbl_root_table_list.tables[i].pointer) {
15995b482a8SLen Brown 			status =
16095b482a8SLen Brown 			    acpi_tb_verify_table(&acpi_gbl_root_table_list.
16195b482a8SLen Brown 						 tables[i]);
16295b482a8SLen Brown 			if (ACPI_FAILURE(status)
16395b482a8SLen Brown 			    || !acpi_gbl_root_table_list.tables[i].pointer) {
16495b482a8SLen Brown 				continue;
16595b482a8SLen Brown 			}
16695b482a8SLen Brown 		}
16795b482a8SLen Brown 
16895b482a8SLen Brown 		/*
16995b482a8SLen Brown 		 * Check for a table match on the entire table length,
17095b482a8SLen Brown 		 * not just the header.
17195b482a8SLen Brown 		 */
17295b482a8SLen Brown 		if (table_desc->length !=
17395b482a8SLen Brown 		    acpi_gbl_root_table_list.tables[i].length) {
17495b482a8SLen Brown 			continue;
17595b482a8SLen Brown 		}
17695b482a8SLen Brown 
17795b482a8SLen Brown 		if (ACPI_MEMCMP(table_desc->pointer,
17895b482a8SLen Brown 				acpi_gbl_root_table_list.tables[i].pointer,
17995b482a8SLen Brown 				acpi_gbl_root_table_list.tables[i].length)) {
18095b482a8SLen Brown 			continue;
18195b482a8SLen Brown 		}
18295b482a8SLen Brown 
18395b482a8SLen Brown 		/*
18495b482a8SLen Brown 		 * Note: the current mechanism does not unregister a table if it is
18595b482a8SLen Brown 		 * dynamically unloaded. The related namespace entries are deleted,
18695b482a8SLen Brown 		 * but the table remains in the root table list.
18795b482a8SLen Brown 		 *
18895b482a8SLen Brown 		 * The assumption here is that the number of different tables that
18995b482a8SLen Brown 		 * will be loaded is actually small, and there is minimal overhead
19095b482a8SLen Brown 		 * in just keeping the table in case it is needed again.
19195b482a8SLen Brown 		 *
19295b482a8SLen Brown 		 * If this assumption changes in the future (perhaps on large
19395b482a8SLen Brown 		 * machines with many table load/unload operations), tables will
19495b482a8SLen Brown 		 * need to be unregistered when they are unloaded, and slots in the
19595b482a8SLen Brown 		 * root table list should be reused when empty.
19695b482a8SLen Brown 		 */
19795b482a8SLen Brown 
19895b482a8SLen Brown 		/*
19995b482a8SLen Brown 		 * Table is already registered.
20095b482a8SLen Brown 		 * We can delete the table that was passed as a parameter.
20195b482a8SLen Brown 		 */
20295b482a8SLen Brown 		acpi_tb_delete_table(table_desc);
20395b482a8SLen Brown 		*table_index = i;
20495b482a8SLen Brown 
20595b482a8SLen Brown 		if (acpi_gbl_root_table_list.tables[i].
20695b482a8SLen Brown 		    flags & ACPI_TABLE_IS_LOADED) {
20795b482a8SLen Brown 
20895b482a8SLen Brown 			/* Table is still loaded, this is an error */
20995b482a8SLen Brown 
21095b482a8SLen Brown 			status = AE_ALREADY_EXISTS;
21195b482a8SLen Brown 			goto release;
21295b482a8SLen Brown 		} else {
21395b482a8SLen Brown 			/* Table was unloaded, allow it to be reloaded */
21495b482a8SLen Brown 
21595b482a8SLen Brown 			table_desc->pointer =
21695b482a8SLen Brown 			    acpi_gbl_root_table_list.tables[i].pointer;
21795b482a8SLen Brown 			table_desc->address =
21895b482a8SLen Brown 			    acpi_gbl_root_table_list.tables[i].address;
21995b482a8SLen Brown 			status = AE_OK;
22095b482a8SLen Brown 			goto print_header;
22195b482a8SLen Brown 		}
22295b482a8SLen Brown 	}
22395b482a8SLen Brown 
224d3ccaff8SBob Moore 	/*
225d3ccaff8SBob Moore 	 * ACPI Table Override:
226d3ccaff8SBob Moore 	 * Allow the host to override dynamically loaded tables.
227d3ccaff8SBob Moore 	 */
228d3ccaff8SBob Moore 	status = acpi_os_table_override(table_desc->pointer, &override_table);
229d3ccaff8SBob Moore 	if (ACPI_SUCCESS(status) && override_table) {
230d3ccaff8SBob Moore 		ACPI_INFO((AE_INFO,
231d3ccaff8SBob Moore 			   "%4.4s @ 0x%p Table override, replaced with:",
232d3ccaff8SBob Moore 			   table_desc->pointer->signature,
233d3ccaff8SBob Moore 			   ACPI_CAST_PTR(void, table_desc->address)));
234d3ccaff8SBob Moore 
235d3ccaff8SBob Moore 		/* We can delete the table that was passed as a parameter */
236d3ccaff8SBob Moore 
237d3ccaff8SBob Moore 		acpi_tb_delete_table(table_desc);
238d3ccaff8SBob Moore 
239d3ccaff8SBob Moore 		/* Setup descriptor for the new table */
240d3ccaff8SBob Moore 
241d3ccaff8SBob Moore 		table_desc->address = ACPI_PTR_TO_PHYSADDR(override_table);
242d3ccaff8SBob Moore 		table_desc->pointer = override_table;
243d3ccaff8SBob Moore 		table_desc->length = override_table->length;
244d3ccaff8SBob Moore 		table_desc->flags = ACPI_TABLE_ORIGIN_OVERRIDE;
245d3ccaff8SBob Moore 	}
246d3ccaff8SBob Moore 
24795b482a8SLen Brown 	/* Add the table to the global root table list */
24895b482a8SLen Brown 
24995b482a8SLen Brown 	status = acpi_tb_store_table(table_desc->address, table_desc->pointer,
25095b482a8SLen Brown 				     table_desc->length, table_desc->flags,
25195b482a8SLen Brown 				     table_index);
25295b482a8SLen Brown 	if (ACPI_FAILURE(status)) {
25395b482a8SLen Brown 		goto release;
25495b482a8SLen Brown 	}
25595b482a8SLen Brown 
25695b482a8SLen Brown       print_header:
25795b482a8SLen Brown 	acpi_tb_print_table_header(table_desc->address, table_desc->pointer);
25895b482a8SLen Brown 
25995b482a8SLen Brown       release:
26095b482a8SLen Brown 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
26195b482a8SLen Brown 	return_ACPI_STATUS(status);
26295b482a8SLen Brown }
26395b482a8SLen Brown 
26495b482a8SLen Brown /*******************************************************************************
26595b482a8SLen Brown  *
26695b482a8SLen Brown  * FUNCTION:    acpi_tb_resize_root_table_list
26795b482a8SLen Brown  *
26895b482a8SLen Brown  * PARAMETERS:  None
26995b482a8SLen Brown  *
27095b482a8SLen Brown  * RETURN:      Status
27195b482a8SLen Brown  *
27295b482a8SLen Brown  * DESCRIPTION: Expand the size of global table array
27395b482a8SLen Brown  *
27495b482a8SLen Brown  ******************************************************************************/
27595b482a8SLen Brown 
27695b482a8SLen Brown acpi_status acpi_tb_resize_root_table_list(void)
27795b482a8SLen Brown {
27895b482a8SLen Brown 	struct acpi_table_desc *tables;
27995b482a8SLen Brown 
28095b482a8SLen Brown 	ACPI_FUNCTION_TRACE(tb_resize_root_table_list);
28195b482a8SLen Brown 
28295b482a8SLen Brown 	/* allow_resize flag is a parameter to acpi_initialize_tables */
28395b482a8SLen Brown 
28495b482a8SLen Brown 	if (!(acpi_gbl_root_table_list.flags & ACPI_ROOT_ALLOW_RESIZE)) {
28595b482a8SLen Brown 		ACPI_ERROR((AE_INFO,
28695b482a8SLen Brown 			    "Resize of Root Table Array is not allowed"));
28795b482a8SLen Brown 		return_ACPI_STATUS(AE_SUPPORT);
28895b482a8SLen Brown 	}
28995b482a8SLen Brown 
29095b482a8SLen Brown 	/* Increase the Table Array size */
29195b482a8SLen Brown 
29295b482a8SLen Brown 	tables = ACPI_ALLOCATE_ZEROED(((acpi_size) acpi_gbl_root_table_list.
293b9ee2043SBob Moore 				       max_table_count +
294ec41f193SBob Moore 				       ACPI_ROOT_TABLE_SIZE_INCREMENT) *
295ec41f193SBob Moore 				      sizeof(struct acpi_table_desc));
29695b482a8SLen Brown 	if (!tables) {
29795b482a8SLen Brown 		ACPI_ERROR((AE_INFO,
29895b482a8SLen Brown 			    "Could not allocate new root table array"));
29995b482a8SLen Brown 		return_ACPI_STATUS(AE_NO_MEMORY);
30095b482a8SLen Brown 	}
30195b482a8SLen Brown 
30295b482a8SLen Brown 	/* Copy and free the previous table array */
30395b482a8SLen Brown 
30495b482a8SLen Brown 	if (acpi_gbl_root_table_list.tables) {
30595b482a8SLen Brown 		ACPI_MEMCPY(tables, acpi_gbl_root_table_list.tables,
306b9ee2043SBob Moore 			    (acpi_size) acpi_gbl_root_table_list.
307b9ee2043SBob Moore 			    max_table_count * sizeof(struct acpi_table_desc));
30895b482a8SLen Brown 
30995b482a8SLen Brown 		if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
31095b482a8SLen Brown 			ACPI_FREE(acpi_gbl_root_table_list.tables);
31195b482a8SLen Brown 		}
31295b482a8SLen Brown 	}
31395b482a8SLen Brown 
31495b482a8SLen Brown 	acpi_gbl_root_table_list.tables = tables;
315b9ee2043SBob Moore 	acpi_gbl_root_table_list.max_table_count +=
316b9ee2043SBob Moore 	    ACPI_ROOT_TABLE_SIZE_INCREMENT;
31795b482a8SLen Brown 	acpi_gbl_root_table_list.flags |= (u8)ACPI_ROOT_ORIGIN_ALLOCATED;
31895b482a8SLen Brown 
31995b482a8SLen Brown 	return_ACPI_STATUS(AE_OK);
32095b482a8SLen Brown }
32195b482a8SLen Brown 
32295b482a8SLen Brown /*******************************************************************************
32395b482a8SLen Brown  *
32495b482a8SLen Brown  * FUNCTION:    acpi_tb_store_table
32595b482a8SLen Brown  *
32695b482a8SLen Brown  * PARAMETERS:  Address             - Table address
32795b482a8SLen Brown  *              Table               - Table header
32895b482a8SLen Brown  *              Length              - Table length
32995b482a8SLen Brown  *              Flags               - flags
33095b482a8SLen Brown  *
33195b482a8SLen Brown  * RETURN:      Status and table index.
33295b482a8SLen Brown  *
33395b482a8SLen Brown  * DESCRIPTION: Add an ACPI table to the global table list
33495b482a8SLen Brown  *
33595b482a8SLen Brown  ******************************************************************************/
33695b482a8SLen Brown 
33795b482a8SLen Brown acpi_status
33895b482a8SLen Brown acpi_tb_store_table(acpi_physical_address address,
33995b482a8SLen Brown 		    struct acpi_table_header *table,
34095b482a8SLen Brown 		    u32 length, u8 flags, u32 *table_index)
34195b482a8SLen Brown {
342b9ee2043SBob Moore 	acpi_status status;
343b9ee2043SBob Moore 	struct acpi_table_desc *new_table;
34495b482a8SLen Brown 
34595b482a8SLen Brown 	/* Ensure that there is room for the table in the Root Table List */
34695b482a8SLen Brown 
347b9ee2043SBob Moore 	if (acpi_gbl_root_table_list.current_table_count >=
348b9ee2043SBob Moore 	    acpi_gbl_root_table_list.max_table_count) {
34995b482a8SLen Brown 		status = acpi_tb_resize_root_table_list();
35095b482a8SLen Brown 		if (ACPI_FAILURE(status)) {
35195b482a8SLen Brown 			return (status);
35295b482a8SLen Brown 		}
35395b482a8SLen Brown 	}
35495b482a8SLen Brown 
355b9ee2043SBob Moore 	new_table =
356b9ee2043SBob Moore 	    &acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.
357b9ee2043SBob Moore 					     current_table_count];
358b9ee2043SBob Moore 
35995b482a8SLen Brown 	/* Initialize added table */
36095b482a8SLen Brown 
361b9ee2043SBob Moore 	new_table->address = address;
362b9ee2043SBob Moore 	new_table->pointer = table;
363b9ee2043SBob Moore 	new_table->length = length;
364b9ee2043SBob Moore 	new_table->owner_id = 0;
365b9ee2043SBob Moore 	new_table->flags = flags;
36695b482a8SLen Brown 
367b9ee2043SBob Moore 	ACPI_MOVE_32_TO_32(&new_table->signature, table->signature);
36895b482a8SLen Brown 
369b9ee2043SBob Moore 	*table_index = acpi_gbl_root_table_list.current_table_count;
370b9ee2043SBob Moore 	acpi_gbl_root_table_list.current_table_count++;
371b9ee2043SBob Moore 	return (AE_OK);
37295b482a8SLen Brown }
37395b482a8SLen Brown 
37495b482a8SLen Brown /*******************************************************************************
37595b482a8SLen Brown  *
37695b482a8SLen Brown  * FUNCTION:    acpi_tb_delete_table
37795b482a8SLen Brown  *
37895b482a8SLen Brown  * PARAMETERS:  table_index         - Table index
37995b482a8SLen Brown  *
38095b482a8SLen Brown  * RETURN:      None
38195b482a8SLen Brown  *
38295b482a8SLen Brown  * DESCRIPTION: Delete one internal ACPI table
38395b482a8SLen Brown  *
38495b482a8SLen Brown  ******************************************************************************/
38595b482a8SLen Brown 
38695b482a8SLen Brown void acpi_tb_delete_table(struct acpi_table_desc *table_desc)
38795b482a8SLen Brown {
38895b482a8SLen Brown 	/* Table must be mapped or allocated */
38995b482a8SLen Brown 	if (!table_desc->pointer) {
39095b482a8SLen Brown 		return;
39195b482a8SLen Brown 	}
39295b482a8SLen Brown 	switch (table_desc->flags & ACPI_TABLE_ORIGIN_MASK) {
39395b482a8SLen Brown 	case ACPI_TABLE_ORIGIN_MAPPED:
39495b482a8SLen Brown 		acpi_os_unmap_memory(table_desc->pointer, table_desc->length);
39595b482a8SLen Brown 		break;
39695b482a8SLen Brown 	case ACPI_TABLE_ORIGIN_ALLOCATED:
39795b482a8SLen Brown 		ACPI_FREE(table_desc->pointer);
39895b482a8SLen Brown 		break;
39995b482a8SLen Brown 	default:;
40095b482a8SLen Brown 	}
40195b482a8SLen Brown 
40295b482a8SLen Brown 	table_desc->pointer = NULL;
40395b482a8SLen Brown }
40495b482a8SLen Brown 
40595b482a8SLen Brown /*******************************************************************************
40695b482a8SLen Brown  *
40795b482a8SLen Brown  * FUNCTION:    acpi_tb_terminate
40895b482a8SLen Brown  *
40995b482a8SLen Brown  * PARAMETERS:  None
41095b482a8SLen Brown  *
41195b482a8SLen Brown  * RETURN:      None
41295b482a8SLen Brown  *
41395b482a8SLen Brown  * DESCRIPTION: Delete all internal ACPI tables
41495b482a8SLen Brown  *
41595b482a8SLen Brown  ******************************************************************************/
41695b482a8SLen Brown 
41795b482a8SLen Brown void acpi_tb_terminate(void)
41895b482a8SLen Brown {
41995b482a8SLen Brown 	u32 i;
42095b482a8SLen Brown 
42195b482a8SLen Brown 	ACPI_FUNCTION_TRACE(tb_terminate);
42295b482a8SLen Brown 
42395b482a8SLen Brown 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
42495b482a8SLen Brown 
42595b482a8SLen Brown 	/* Delete the individual tables */
42695b482a8SLen Brown 
427b9ee2043SBob Moore 	for (i = 0; i < acpi_gbl_root_table_list.current_table_count; i++) {
42895b482a8SLen Brown 		acpi_tb_delete_table(&acpi_gbl_root_table_list.tables[i]);
42995b482a8SLen Brown 	}
43095b482a8SLen Brown 
43195b482a8SLen Brown 	/*
43295b482a8SLen Brown 	 * Delete the root table array if allocated locally. Array cannot be
43395b482a8SLen Brown 	 * mapped, so we don't need to check for that flag.
43495b482a8SLen Brown 	 */
43595b482a8SLen Brown 	if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
43695b482a8SLen Brown 		ACPI_FREE(acpi_gbl_root_table_list.tables);
43795b482a8SLen Brown 	}
43895b482a8SLen Brown 
43995b482a8SLen Brown 	acpi_gbl_root_table_list.tables = NULL;
44095b482a8SLen Brown 	acpi_gbl_root_table_list.flags = 0;
441b9ee2043SBob Moore 	acpi_gbl_root_table_list.current_table_count = 0;
44295b482a8SLen Brown 
44395b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "ACPI Tables freed\n"));
44495b482a8SLen Brown 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
44595b482a8SLen Brown }
44695b482a8SLen Brown 
44795b482a8SLen Brown /*******************************************************************************
44895b482a8SLen Brown  *
44995b482a8SLen Brown  * FUNCTION:    acpi_tb_delete_namespace_by_owner
45095b482a8SLen Brown  *
45195b482a8SLen Brown  * PARAMETERS:  table_index         - Table index
45295b482a8SLen Brown  *
4538a335a23SBob Moore  * RETURN:      Status
45495b482a8SLen Brown  *
45595b482a8SLen Brown  * DESCRIPTION: Delete all namespace objects created when this table was loaded.
45695b482a8SLen Brown  *
45795b482a8SLen Brown  ******************************************************************************/
45895b482a8SLen Brown 
4598a335a23SBob Moore acpi_status acpi_tb_delete_namespace_by_owner(u32 table_index)
46095b482a8SLen Brown {
46195b482a8SLen Brown 	acpi_owner_id owner_id;
4628a335a23SBob Moore 	acpi_status status;
46395b482a8SLen Brown 
4648a335a23SBob Moore 	ACPI_FUNCTION_TRACE(tb_delete_namespace_by_owner);
4658a335a23SBob Moore 
4668a335a23SBob Moore 	status = acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
4678a335a23SBob Moore 	if (ACPI_FAILURE(status)) {
4688a335a23SBob Moore 		return_ACPI_STATUS(status);
46995b482a8SLen Brown 	}
47095b482a8SLen Brown 
471b9ee2043SBob Moore 	if (table_index >= acpi_gbl_root_table_list.current_table_count) {
4728a335a23SBob Moore 
4738a335a23SBob Moore 		/* The table index does not exist */
4748a335a23SBob Moore 
47595b482a8SLen Brown 		(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
4768a335a23SBob Moore 		return_ACPI_STATUS(AE_NOT_EXIST);
4778a335a23SBob Moore 	}
4788a335a23SBob Moore 
4798a335a23SBob Moore 	/* Get the owner ID for this table, used to delete namespace nodes */
4808a335a23SBob Moore 
4818a335a23SBob Moore 	owner_id = acpi_gbl_root_table_list.tables[table_index].owner_id;
4828a335a23SBob Moore 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
4838a335a23SBob Moore 
4848a335a23SBob Moore 	/*
4858a335a23SBob Moore 	 * Need to acquire the namespace writer lock to prevent interference
4868a335a23SBob Moore 	 * with any concurrent namespace walks. The interpreter must be
4878a335a23SBob Moore 	 * released during the deletion since the acquisition of the deletion
4888a335a23SBob Moore 	 * lock may block, and also since the execution of a namespace walk
4898a335a23SBob Moore 	 * must be allowed to use the interpreter.
4908a335a23SBob Moore 	 */
491e4c1ebfcSBob Moore 	(void)acpi_ut_release_mutex(ACPI_MTX_INTERPRETER);
4928a335a23SBob Moore 	status = acpi_ut_acquire_write_lock(&acpi_gbl_namespace_rw_lock);
4938a335a23SBob Moore 
49495b482a8SLen Brown 	acpi_ns_delete_namespace_by_owner(owner_id);
4958a335a23SBob Moore 	if (ACPI_FAILURE(status)) {
4968a335a23SBob Moore 		return_ACPI_STATUS(status);
4978a335a23SBob Moore 	}
4988a335a23SBob Moore 
4998a335a23SBob Moore 	acpi_ut_release_write_lock(&acpi_gbl_namespace_rw_lock);
5008a335a23SBob Moore 
5018a335a23SBob Moore 	status = acpi_ut_acquire_mutex(ACPI_MTX_INTERPRETER);
5028a335a23SBob Moore 	return_ACPI_STATUS(status);
50395b482a8SLen Brown }
50495b482a8SLen Brown 
50595b482a8SLen Brown /*******************************************************************************
50695b482a8SLen Brown  *
50795b482a8SLen Brown  * FUNCTION:    acpi_tb_allocate_owner_id
50895b482a8SLen Brown  *
50995b482a8SLen Brown  * PARAMETERS:  table_index         - Table index
51095b482a8SLen Brown  *
51195b482a8SLen Brown  * RETURN:      Status
51295b482a8SLen Brown  *
51395b482a8SLen Brown  * DESCRIPTION: Allocates owner_id in table_desc
51495b482a8SLen Brown  *
51595b482a8SLen Brown  ******************************************************************************/
51695b482a8SLen Brown 
51795b482a8SLen Brown acpi_status acpi_tb_allocate_owner_id(u32 table_index)
51895b482a8SLen Brown {
51995b482a8SLen Brown 	acpi_status status = AE_BAD_PARAMETER;
52095b482a8SLen Brown 
52195b482a8SLen Brown 	ACPI_FUNCTION_TRACE(tb_allocate_owner_id);
52295b482a8SLen Brown 
52395b482a8SLen Brown 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
524b9ee2043SBob Moore 	if (table_index < acpi_gbl_root_table_list.current_table_count) {
52595b482a8SLen Brown 		status = acpi_ut_allocate_owner_id
52695b482a8SLen Brown 		    (&(acpi_gbl_root_table_list.tables[table_index].owner_id));
52795b482a8SLen Brown 	}
52895b482a8SLen Brown 
52995b482a8SLen Brown 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
53095b482a8SLen Brown 	return_ACPI_STATUS(status);
53195b482a8SLen Brown }
53295b482a8SLen Brown 
53395b482a8SLen Brown /*******************************************************************************
53495b482a8SLen Brown  *
53595b482a8SLen Brown  * FUNCTION:    acpi_tb_release_owner_id
53695b482a8SLen Brown  *
53795b482a8SLen Brown  * PARAMETERS:  table_index         - Table index
53895b482a8SLen Brown  *
53995b482a8SLen Brown  * RETURN:      Status
54095b482a8SLen Brown  *
54195b482a8SLen Brown  * DESCRIPTION: Releases owner_id in table_desc
54295b482a8SLen Brown  *
54395b482a8SLen Brown  ******************************************************************************/
54495b482a8SLen Brown 
54595b482a8SLen Brown acpi_status acpi_tb_release_owner_id(u32 table_index)
54695b482a8SLen Brown {
54795b482a8SLen Brown 	acpi_status status = AE_BAD_PARAMETER;
54895b482a8SLen Brown 
54995b482a8SLen Brown 	ACPI_FUNCTION_TRACE(tb_release_owner_id);
55095b482a8SLen Brown 
55195b482a8SLen Brown 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
552b9ee2043SBob Moore 	if (table_index < acpi_gbl_root_table_list.current_table_count) {
55395b482a8SLen Brown 		acpi_ut_release_owner_id(&
55495b482a8SLen Brown 					 (acpi_gbl_root_table_list.
55595b482a8SLen Brown 					  tables[table_index].owner_id));
55695b482a8SLen Brown 		status = AE_OK;
55795b482a8SLen Brown 	}
55895b482a8SLen Brown 
55995b482a8SLen Brown 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
56095b482a8SLen Brown 	return_ACPI_STATUS(status);
56195b482a8SLen Brown }
56295b482a8SLen Brown 
56395b482a8SLen Brown /*******************************************************************************
56495b482a8SLen Brown  *
56595b482a8SLen Brown  * FUNCTION:    acpi_tb_get_owner_id
56695b482a8SLen Brown  *
56795b482a8SLen Brown  * PARAMETERS:  table_index         - Table index
56895b482a8SLen Brown  *              owner_id            - Where the table owner_id is returned
56995b482a8SLen Brown  *
57095b482a8SLen Brown  * RETURN:      Status
57195b482a8SLen Brown  *
57295b482a8SLen Brown  * DESCRIPTION: returns owner_id for the ACPI table
57395b482a8SLen Brown  *
57495b482a8SLen Brown  ******************************************************************************/
57595b482a8SLen Brown 
57695b482a8SLen Brown acpi_status acpi_tb_get_owner_id(u32 table_index, acpi_owner_id *owner_id)
57795b482a8SLen Brown {
57895b482a8SLen Brown 	acpi_status status = AE_BAD_PARAMETER;
57995b482a8SLen Brown 
58095b482a8SLen Brown 	ACPI_FUNCTION_TRACE(tb_get_owner_id);
58195b482a8SLen Brown 
58295b482a8SLen Brown 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
583b9ee2043SBob Moore 	if (table_index < acpi_gbl_root_table_list.current_table_count) {
58495b482a8SLen Brown 		*owner_id =
58595b482a8SLen Brown 		    acpi_gbl_root_table_list.tables[table_index].owner_id;
58695b482a8SLen Brown 		status = AE_OK;
58795b482a8SLen Brown 	}
58895b482a8SLen Brown 
58995b482a8SLen Brown 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
59095b482a8SLen Brown 	return_ACPI_STATUS(status);
59195b482a8SLen Brown }
59295b482a8SLen Brown 
59395b482a8SLen Brown /*******************************************************************************
59495b482a8SLen Brown  *
59595b482a8SLen Brown  * FUNCTION:    acpi_tb_is_table_loaded
59695b482a8SLen Brown  *
59795b482a8SLen Brown  * PARAMETERS:  table_index         - Table index
59895b482a8SLen Brown  *
59995b482a8SLen Brown  * RETURN:      Table Loaded Flag
60095b482a8SLen Brown  *
60195b482a8SLen Brown  ******************************************************************************/
60295b482a8SLen Brown 
60395b482a8SLen Brown u8 acpi_tb_is_table_loaded(u32 table_index)
60495b482a8SLen Brown {
60595b482a8SLen Brown 	u8 is_loaded = FALSE;
60695b482a8SLen Brown 
60795b482a8SLen Brown 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
608b9ee2043SBob Moore 	if (table_index < acpi_gbl_root_table_list.current_table_count) {
60995b482a8SLen Brown 		is_loaded = (u8)
610ec41f193SBob Moore 		    (acpi_gbl_root_table_list.tables[table_index].flags &
611ec41f193SBob Moore 		     ACPI_TABLE_IS_LOADED);
61295b482a8SLen Brown 	}
61395b482a8SLen Brown 
61495b482a8SLen Brown 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
61595b482a8SLen Brown 	return (is_loaded);
61695b482a8SLen Brown }
61795b482a8SLen Brown 
61895b482a8SLen Brown /*******************************************************************************
61995b482a8SLen Brown  *
62095b482a8SLen Brown  * FUNCTION:    acpi_tb_set_table_loaded_flag
62195b482a8SLen Brown  *
62295b482a8SLen Brown  * PARAMETERS:  table_index         - Table index
62395b482a8SLen Brown  *              is_loaded           - TRUE if table is loaded, FALSE otherwise
62495b482a8SLen Brown  *
62595b482a8SLen Brown  * RETURN:      None
62695b482a8SLen Brown  *
62795b482a8SLen Brown  * DESCRIPTION: Sets the table loaded flag to either TRUE or FALSE.
62895b482a8SLen Brown  *
62995b482a8SLen Brown  ******************************************************************************/
63095b482a8SLen Brown 
63195b482a8SLen Brown void acpi_tb_set_table_loaded_flag(u32 table_index, u8 is_loaded)
63295b482a8SLen Brown {
63395b482a8SLen Brown 
63495b482a8SLen Brown 	(void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
635b9ee2043SBob Moore 	if (table_index < acpi_gbl_root_table_list.current_table_count) {
63695b482a8SLen Brown 		if (is_loaded) {
63795b482a8SLen Brown 			acpi_gbl_root_table_list.tables[table_index].flags |=
63895b482a8SLen Brown 			    ACPI_TABLE_IS_LOADED;
63995b482a8SLen Brown 		} else {
64095b482a8SLen Brown 			acpi_gbl_root_table_list.tables[table_index].flags &=
64195b482a8SLen Brown 			    ~ACPI_TABLE_IS_LOADED;
64295b482a8SLen Brown 		}
64395b482a8SLen Brown 	}
64495b482a8SLen Brown 
64595b482a8SLen Brown 	(void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
64695b482a8SLen Brown }
647