GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / acpi / acpica / tbdata.c
1 /******************************************************************************
2  *
3  * Module Name: tbdata - Table manager data structure functions
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2017, Intel Corp.
9  * All rights reserved.
10  *
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.
25  *
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.
29  *
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.
42  */
43
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "actables.h"
48 #include "acevents.h"
49
50 #define _COMPONENT          ACPI_TABLES
51 ACPI_MODULE_NAME("tbdata")
52
53 /* Local prototypes */
54 static acpi_status
55 acpi_tb_check_duplication(struct acpi_table_desc *table_desc, u32 *table_index);
56
57 static u8
58 acpi_tb_compare_tables(struct acpi_table_desc *table_desc, u32 table_index);
59
60 /*******************************************************************************
61  *
62  * FUNCTION:    acpi_tb_compare_tables
63  *
64  * PARAMETERS:  table_desc          - Table 1 descriptor to be compared
65  *              table_index         - Index of table 2 to be compared
66  *
67  * RETURN:      TRUE if both tables are identical.
68  *
69  * DESCRIPTION: This function compares a table with another table that has
70  *              already been installed in the root table list.
71  *
72  ******************************************************************************/
73
74 static u8
75 acpi_tb_compare_tables(struct acpi_table_desc *table_desc, u32 table_index)
76 {
77         acpi_status status = AE_OK;
78         u8 is_identical;
79         struct acpi_table_header *table;
80         u32 table_length;
81         u8 table_flags;
82
83         status =
84             acpi_tb_acquire_table(&acpi_gbl_root_table_list.tables[table_index],
85                                   &table, &table_length, &table_flags);
86         if (ACPI_FAILURE(status)) {
87                 return (FALSE);
88         }
89
90         /*
91          * Check for a table match on the entire table length,
92          * not just the header.
93          */
94         is_identical = (u8)((table_desc->length != table_length ||
95                              memcmp(table_desc->pointer, table, table_length)) ?
96                             FALSE : TRUE);
97
98         /* Release the acquired table */
99
100         acpi_tb_release_table(table, table_length, table_flags);
101         return (is_identical);
102 }
103
104 /*******************************************************************************
105  *
106  * FUNCTION:    acpi_tb_init_table_descriptor
107  *
108  * PARAMETERS:  table_desc              - Table descriptor
109  *              address                 - Physical address of the table
110  *              flags                   - Allocation flags of the table
111  *              table                   - Pointer to the table
112  *
113  * RETURN:      None
114  *
115  * DESCRIPTION: Initialize a new table descriptor
116  *
117  ******************************************************************************/
118
119 void
120 acpi_tb_init_table_descriptor(struct acpi_table_desc *table_desc,
121                               acpi_physical_address address,
122                               u8 flags, struct acpi_table_header *table)
123 {
124
125         /*
126          * Initialize the table descriptor. Set the pointer to NULL, since the
127          * table is not fully mapped at this time.
128          */
129         memset(table_desc, 0, sizeof(struct acpi_table_desc));
130         table_desc->address = address;
131         table_desc->length = table->length;
132         table_desc->flags = flags;
133         ACPI_MOVE_32_TO_32(table_desc->signature.ascii, table->signature);
134 }
135
136 /*******************************************************************************
137  *
138  * FUNCTION:    acpi_tb_acquire_table
139  *
140  * PARAMETERS:  table_desc          - Table descriptor
141  *              table_ptr           - Where table is returned
142  *              table_length        - Where table length is returned
143  *              table_flags         - Where table allocation flags are returned
144  *
145  * RETURN:      Status
146  *
147  * DESCRIPTION: Acquire an ACPI table. It can be used for tables not
148  *              maintained in the acpi_gbl_root_table_list.
149  *
150  ******************************************************************************/
151
152 acpi_status
153 acpi_tb_acquire_table(struct acpi_table_desc *table_desc,
154                       struct acpi_table_header **table_ptr,
155                       u32 *table_length, u8 *table_flags)
156 {
157         struct acpi_table_header *table = NULL;
158
159         switch (table_desc->flags & ACPI_TABLE_ORIGIN_MASK) {
160         case ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL:
161
162                 table =
163                     acpi_os_map_memory(table_desc->address, table_desc->length);
164                 break;
165
166         case ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL:
167         case ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL:
168
169                 table = ACPI_CAST_PTR(struct acpi_table_header,
170                                       ACPI_PHYSADDR_TO_PTR(table_desc->
171                                                            address));
172                 break;
173
174         default:
175
176                 break;
177         }
178
179         /* Table is not valid yet */
180
181         if (!table) {
182                 return (AE_NO_MEMORY);
183         }
184
185         /* Fill the return values */
186
187         *table_ptr = table;
188         *table_length = table_desc->length;
189         *table_flags = table_desc->flags;
190         return (AE_OK);
191 }
192
193 /*******************************************************************************
194  *
195  * FUNCTION:    acpi_tb_release_table
196  *
197  * PARAMETERS:  table               - Pointer for the table
198  *              table_length        - Length for the table
199  *              table_flags         - Allocation flags for the table
200  *
201  * RETURN:      None
202  *
203  * DESCRIPTION: Release a table. The inverse of acpi_tb_acquire_table().
204  *
205  ******************************************************************************/
206
207 void
208 acpi_tb_release_table(struct acpi_table_header *table,
209                       u32 table_length, u8 table_flags)
210 {
211
212         switch (table_flags & ACPI_TABLE_ORIGIN_MASK) {
213         case ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL:
214
215                 acpi_os_unmap_memory(table, table_length);
216                 break;
217
218         case ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL:
219         case ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL:
220         default:
221
222                 break;
223         }
224 }
225
226 /*******************************************************************************
227  *
228  * FUNCTION:    acpi_tb_acquire_temp_table
229  *
230  * PARAMETERS:  table_desc          - Table descriptor to be acquired
231  *              address             - Address of the table
232  *              flags               - Allocation flags of the table
233  *
234  * RETURN:      Status
235  *
236  * DESCRIPTION: This function validates the table header to obtain the length
237  *              of a table and fills the table descriptor to make its state as
238  *              "INSTALLED". Such a table descriptor is only used for verified
239  *              installation.
240  *
241  ******************************************************************************/
242
243 acpi_status
244 acpi_tb_acquire_temp_table(struct acpi_table_desc *table_desc,
245                            acpi_physical_address address, u8 flags)
246 {
247         struct acpi_table_header *table_header;
248
249         switch (flags & ACPI_TABLE_ORIGIN_MASK) {
250         case ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL:
251
252                 /* Get the length of the full table from the header */
253
254                 table_header =
255                     acpi_os_map_memory(address,
256                                        sizeof(struct acpi_table_header));
257                 if (!table_header) {
258                         return (AE_NO_MEMORY);
259                 }
260
261                 acpi_tb_init_table_descriptor(table_desc, address, flags,
262                                               table_header);
263                 acpi_os_unmap_memory(table_header,
264                                      sizeof(struct acpi_table_header));
265                 return (AE_OK);
266
267         case ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL:
268         case ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL:
269
270                 table_header = ACPI_CAST_PTR(struct acpi_table_header,
271                                              ACPI_PHYSADDR_TO_PTR(address));
272                 if (!table_header) {
273                         return (AE_NO_MEMORY);
274                 }
275
276                 acpi_tb_init_table_descriptor(table_desc, address, flags,
277                                               table_header);
278                 return (AE_OK);
279
280         default:
281
282                 break;
283         }
284
285         /* Table is not valid yet */
286
287         return (AE_NO_MEMORY);
288 }
289
290 /*******************************************************************************
291  *
292  * FUNCTION:    acpi_tb_release_temp_table
293  *
294  * PARAMETERS:  table_desc          - Table descriptor to be released
295  *
296  * RETURN:      Status
297  *
298  * DESCRIPTION: The inverse of acpi_tb_acquire_temp_table().
299  *
300  *****************************************************************************/
301
302 void acpi_tb_release_temp_table(struct acpi_table_desc *table_desc)
303 {
304
305         /*
306          * Note that the .Address is maintained by the callers of
307          * acpi_tb_acquire_temp_table(), thus do not invoke acpi_tb_uninstall_table()
308          * where .Address will be freed.
309          */
310         acpi_tb_invalidate_table(table_desc);
311 }
312
313 /******************************************************************************
314  *
315  * FUNCTION:    acpi_tb_validate_table
316  *
317  * PARAMETERS:  table_desc          - Table descriptor
318  *
319  * RETURN:      Status
320  *
321  * DESCRIPTION: This function is called to validate the table, the returned
322  *              table descriptor is in "VALIDATED" state.
323  *
324  *****************************************************************************/
325
326 acpi_status acpi_tb_validate_table(struct acpi_table_desc *table_desc)
327 {
328         acpi_status status = AE_OK;
329
330         ACPI_FUNCTION_TRACE(tb_validate_table);
331
332         /* Validate the table if necessary */
333
334         if (!table_desc->pointer) {
335                 status = acpi_tb_acquire_table(table_desc, &table_desc->pointer,
336                                                &table_desc->length,
337                                                &table_desc->flags);
338                 if (!table_desc->pointer) {
339                         status = AE_NO_MEMORY;
340                 }
341         }
342
343         return_ACPI_STATUS(status);
344 }
345
346 /*******************************************************************************
347  *
348  * FUNCTION:    acpi_tb_invalidate_table
349  *
350  * PARAMETERS:  table_desc          - Table descriptor
351  *
352  * RETURN:      None
353  *
354  * DESCRIPTION: Invalidate one internal ACPI table, this is the inverse of
355  *              acpi_tb_validate_table().
356  *
357  ******************************************************************************/
358
359 void acpi_tb_invalidate_table(struct acpi_table_desc *table_desc)
360 {
361
362         ACPI_FUNCTION_TRACE(tb_invalidate_table);
363
364         /* Table must be validated */
365
366         if (!table_desc->pointer) {
367                 return_VOID;
368         }
369
370         acpi_tb_release_table(table_desc->pointer, table_desc->length,
371                               table_desc->flags);
372         table_desc->pointer = NULL;
373
374         return_VOID;
375 }
376
377 /******************************************************************************
378  *
379  * FUNCTION:    acpi_tb_validate_temp_table
380  *
381  * PARAMETERS:  table_desc          - Table descriptor
382  *
383  * RETURN:      Status
384  *
385  * DESCRIPTION: This function is called to validate the table, the returned
386  *              table descriptor is in "VALIDATED" state.
387  *
388  *****************************************************************************/
389
390 acpi_status acpi_tb_validate_temp_table(struct acpi_table_desc *table_desc)
391 {
392
393         if (!table_desc->pointer && !acpi_gbl_enable_table_validation) {
394                 /*
395                  * Only validates the header of the table.
396                  * Note that Length contains the size of the mapping after invoking
397                  * this work around, this value is required by
398                  * acpi_tb_release_temp_table().
399                  * We can do this because in acpi_init_table_descriptor(), the Length
400                  * field of the installed descriptor is filled with the actual
401                  * table length obtaining from the table header.
402                  */
403                 table_desc->length = sizeof(struct acpi_table_header);
404         }
405
406         return (acpi_tb_validate_table(table_desc));
407 }
408
409 /*******************************************************************************
410  *
411  * FUNCTION:    acpi_tb_check_duplication
412  *
413  * PARAMETERS:  table_desc          - Table descriptor
414  *              table_index         - Where the table index is returned
415  *
416  * RETURN:      Status
417  *
418  * DESCRIPTION: Avoid installing duplicated tables. However table override and
419  *              user aided dynamic table load is allowed, thus comparing the
420  *              address of the table is not sufficient, and checking the entire
421  *              table content is required.
422  *
423  ******************************************************************************/
424
425 static acpi_status
426 acpi_tb_check_duplication(struct acpi_table_desc *table_desc, u32 *table_index)
427 {
428         u32 i;
429
430         ACPI_FUNCTION_TRACE(tb_check_duplication);
431
432         /* Check if table is already registered */
433
434         for (i = 0; i < acpi_gbl_root_table_list.current_table_count; ++i) {
435
436                 /* Do not compare with unverified tables */
437
438                 if (!
439                     (acpi_gbl_root_table_list.tables[i].
440                      flags & ACPI_TABLE_IS_VERIFIED)) {
441                         continue;
442                 }
443
444                 /*
445                  * Check for a table match on the entire table length,
446                  * not just the header.
447                  */
448                 if (!acpi_tb_compare_tables(table_desc, i)) {
449                         continue;
450                 }
451
452                 /*
453                  * Note: the current mechanism does not unregister a table if it is
454                  * dynamically unloaded. The related namespace entries are deleted,
455                  * but the table remains in the root table list.
456                  *
457                  * The assumption here is that the number of different tables that
458                  * will be loaded is actually small, and there is minimal overhead
459                  * in just keeping the table in case it is needed again.
460                  *
461                  * If this assumption changes in the future (perhaps on large
462                  * machines with many table load/unload operations), tables will
463                  * need to be unregistered when they are unloaded, and slots in the
464                  * root table list should be reused when empty.
465                  */
466                 if (acpi_gbl_root_table_list.tables[i].flags &
467                     ACPI_TABLE_IS_LOADED) {
468
469                         /* Table is still loaded, this is an error */
470
471                         return_ACPI_STATUS(AE_ALREADY_EXISTS);
472                 } else {
473                         *table_index = i;
474                         return_ACPI_STATUS(AE_CTRL_TERMINATE);
475                 }
476         }
477
478         /* Indicate no duplication to the caller */
479
480         return_ACPI_STATUS(AE_OK);
481 }
482
483 /******************************************************************************
484  *
485  * FUNCTION:    acpi_tb_verify_temp_table
486  *
487  * PARAMETERS:  table_desc          - Table descriptor
488  *              signature           - Table signature to verify
489  *              table_index         - Where the table index is returned
490  *
491  * RETURN:      Status
492  *
493  * DESCRIPTION: This function is called to validate and verify the table, the
494  *              returned table descriptor is in "VALIDATED" state.
495  *              Note that 'TableIndex' is required to be set to !NULL to
496  *              enable duplication check.
497  *
498  *****************************************************************************/
499
500 acpi_status
501 acpi_tb_verify_temp_table(struct acpi_table_desc *table_desc,
502                           char *signature, u32 *table_index)
503 {
504         acpi_status status = AE_OK;
505
506         ACPI_FUNCTION_TRACE(tb_verify_temp_table);
507
508         /* Validate the table */
509
510         status = acpi_tb_validate_temp_table(table_desc);
511         if (ACPI_FAILURE(status)) {
512                 return_ACPI_STATUS(AE_NO_MEMORY);
513         }
514
515         /* If a particular signature is expected (DSDT/FACS), it must match */
516
517         if (signature && !ACPI_COMPARE_NAME(&table_desc->signature, signature)) {
518                 ACPI_BIOS_ERROR((AE_INFO,
519                                  "Invalid signature 0x%X for ACPI table, expected [%s]",
520                                  table_desc->signature.integer, signature));
521                 status = AE_BAD_SIGNATURE;
522                 goto invalidate_and_exit;
523         }
524
525         if (acpi_gbl_enable_table_validation) {
526
527                 /* Verify the checksum */
528
529                 status =
530                     acpi_tb_verify_checksum(table_desc->pointer,
531                                             table_desc->length);
532                 if (ACPI_FAILURE(status)) {
533                         ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY,
534                                         "%4.4s 0x%8.8X%8.8X"
535                                         " Attempted table install failed",
536                                         acpi_ut_valid_nameseg(table_desc->
537                                                               signature.
538                                                               ascii) ?
539                                         table_desc->signature.ascii : "????",
540                                         ACPI_FORMAT_UINT64(table_desc->
541                                                            address)));
542
543                         goto invalidate_and_exit;
544                 }
545
546                 /* Avoid duplications */
547
548                 if (table_index) {
549                         status =
550                             acpi_tb_check_duplication(table_desc, table_index);
551                         if (ACPI_FAILURE(status)) {
552                                 if (status != AE_CTRL_TERMINATE) {
553                                         ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY,
554                                                         "%4.4s 0x%8.8X%8.8X"
555                                                         " Table is duplicated",
556                                                         acpi_ut_valid_nameseg
557                                                         (table_desc->signature.
558                                                          ascii) ? table_desc->
559                                                         signature.
560                                                         ascii : "????",
561                                                         ACPI_FORMAT_UINT64
562                                                         (table_desc->address)));
563                                 }
564
565                                 goto invalidate_and_exit;
566                         }
567                 }
568
569                 table_desc->flags |= ACPI_TABLE_IS_VERIFIED;
570         }
571
572         return_ACPI_STATUS(status);
573
574 invalidate_and_exit:
575         acpi_tb_invalidate_table(table_desc);
576         return_ACPI_STATUS(status);
577 }
578
579 /*******************************************************************************
580  *
581  * FUNCTION:    acpi_tb_resize_root_table_list
582  *
583  * PARAMETERS:  None
584  *
585  * RETURN:      Status
586  *
587  * DESCRIPTION: Expand the size of global table array
588  *
589  ******************************************************************************/
590
591 acpi_status acpi_tb_resize_root_table_list(void)
592 {
593         struct acpi_table_desc *tables;
594         u32 table_count;
595         u32 current_table_count, max_table_count;
596         u32 i;
597
598         ACPI_FUNCTION_TRACE(tb_resize_root_table_list);
599
600         /* allow_resize flag is a parameter to acpi_initialize_tables */
601
602         if (!(acpi_gbl_root_table_list.flags & ACPI_ROOT_ALLOW_RESIZE)) {
603                 ACPI_ERROR((AE_INFO,
604                             "Resize of Root Table Array is not allowed"));
605                 return_ACPI_STATUS(AE_SUPPORT);
606         }
607
608         /* Increase the Table Array size */
609
610         if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
611                 table_count = acpi_gbl_root_table_list.max_table_count;
612         } else {
613                 table_count = acpi_gbl_root_table_list.current_table_count;
614         }
615
616         max_table_count = table_count + ACPI_ROOT_TABLE_SIZE_INCREMENT;
617         tables = ACPI_ALLOCATE_ZEROED(((acpi_size)max_table_count) *
618                                       sizeof(struct acpi_table_desc));
619         if (!tables) {
620                 ACPI_ERROR((AE_INFO,
621                             "Could not allocate new root table array"));
622                 return_ACPI_STATUS(AE_NO_MEMORY);
623         }
624
625         /* Copy and free the previous table array */
626
627         current_table_count = 0;
628         if (acpi_gbl_root_table_list.tables) {
629                 for (i = 0; i < table_count; i++) {
630                         if (acpi_gbl_root_table_list.tables[i].address) {
631                                 memcpy(tables + current_table_count,
632                                        acpi_gbl_root_table_list.tables + i,
633                                        sizeof(struct acpi_table_desc));
634                                 current_table_count++;
635                         }
636                 }
637
638                 if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
639                         ACPI_FREE(acpi_gbl_root_table_list.tables);
640                 }
641         }
642
643         acpi_gbl_root_table_list.tables = tables;
644         acpi_gbl_root_table_list.max_table_count = max_table_count;
645         acpi_gbl_root_table_list.current_table_count = current_table_count;
646         acpi_gbl_root_table_list.flags |= ACPI_ROOT_ORIGIN_ALLOCATED;
647
648         return_ACPI_STATUS(AE_OK);
649 }
650
651 /*******************************************************************************
652  *
653  * FUNCTION:    acpi_tb_get_next_table_descriptor
654  *
655  * PARAMETERS:  table_index         - Where table index is returned
656  *              table_desc          - Where table descriptor is returned
657  *
658  * RETURN:      Status and table index/descriptor.
659  *
660  * DESCRIPTION: Allocate a new ACPI table entry to the global table list
661  *
662  ******************************************************************************/
663
664 acpi_status
665 acpi_tb_get_next_table_descriptor(u32 *table_index,
666                                   struct acpi_table_desc **table_desc)
667 {
668         acpi_status status;
669         u32 i;
670
671         /* Ensure that there is room for the table in the Root Table List */
672
673         if (acpi_gbl_root_table_list.current_table_count >=
674             acpi_gbl_root_table_list.max_table_count) {
675                 status = acpi_tb_resize_root_table_list();
676                 if (ACPI_FAILURE(status)) {
677                         return (status);
678                 }
679         }
680
681         i = acpi_gbl_root_table_list.current_table_count;
682         acpi_gbl_root_table_list.current_table_count++;
683
684         if (table_index) {
685                 *table_index = i;
686         }
687         if (table_desc) {
688                 *table_desc = &acpi_gbl_root_table_list.tables[i];
689         }
690
691         return (AE_OK);
692 }
693
694 /*******************************************************************************
695  *
696  * FUNCTION:    acpi_tb_terminate
697  *
698  * PARAMETERS:  None
699  *
700  * RETURN:      None
701  *
702  * DESCRIPTION: Delete all internal ACPI tables
703  *
704  ******************************************************************************/
705
706 void acpi_tb_terminate(void)
707 {
708         u32 i;
709
710         ACPI_FUNCTION_TRACE(tb_terminate);
711
712         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
713
714         /* Delete the individual tables */
715
716         for (i = 0; i < acpi_gbl_root_table_list.current_table_count; i++) {
717                 acpi_tb_uninstall_table(&acpi_gbl_root_table_list.tables[i]);
718         }
719
720         /*
721          * Delete the root table array if allocated locally. Array cannot be
722          * mapped, so we don't need to check for that flag.
723          */
724         if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
725                 ACPI_FREE(acpi_gbl_root_table_list.tables);
726         }
727
728         acpi_gbl_root_table_list.tables = NULL;
729         acpi_gbl_root_table_list.flags = 0;
730         acpi_gbl_root_table_list.current_table_count = 0;
731
732         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "ACPI Tables freed\n"));
733
734         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
735         return_VOID;
736 }
737
738 /*******************************************************************************
739  *
740  * FUNCTION:    acpi_tb_delete_namespace_by_owner
741  *
742  * PARAMETERS:  table_index         - Table index
743  *
744  * RETURN:      Status
745  *
746  * DESCRIPTION: Delete all namespace objects created when this table was loaded.
747  *
748  ******************************************************************************/
749
750 acpi_status acpi_tb_delete_namespace_by_owner(u32 table_index)
751 {
752         acpi_owner_id owner_id;
753         acpi_status status;
754
755         ACPI_FUNCTION_TRACE(tb_delete_namespace_by_owner);
756
757         status = acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
758         if (ACPI_FAILURE(status)) {
759                 return_ACPI_STATUS(status);
760         }
761
762         if (table_index >= acpi_gbl_root_table_list.current_table_count) {
763
764                 /* The table index does not exist */
765
766                 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
767                 return_ACPI_STATUS(AE_NOT_EXIST);
768         }
769
770         /* Get the owner ID for this table, used to delete namespace nodes */
771
772         owner_id = acpi_gbl_root_table_list.tables[table_index].owner_id;
773         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
774
775         /*
776          * Need to acquire the namespace writer lock to prevent interference
777          * with any concurrent namespace walks. The interpreter must be
778          * released during the deletion since the acquisition of the deletion
779          * lock may block, and also since the execution of a namespace walk
780          * must be allowed to use the interpreter.
781          */
782         status = acpi_ut_acquire_write_lock(&acpi_gbl_namespace_rw_lock);
783         if (ACPI_FAILURE(status)) {
784                 return_ACPI_STATUS(status);
785         }
786         acpi_ns_delete_namespace_by_owner(owner_id);
787         acpi_ut_release_write_lock(&acpi_gbl_namespace_rw_lock);
788         return_ACPI_STATUS(status);
789 }
790
791 /*******************************************************************************
792  *
793  * FUNCTION:    acpi_tb_allocate_owner_id
794  *
795  * PARAMETERS:  table_index         - Table index
796  *
797  * RETURN:      Status
798  *
799  * DESCRIPTION: Allocates owner_id in table_desc
800  *
801  ******************************************************************************/
802
803 acpi_status acpi_tb_allocate_owner_id(u32 table_index)
804 {
805         acpi_status status = AE_BAD_PARAMETER;
806
807         ACPI_FUNCTION_TRACE(tb_allocate_owner_id);
808
809         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
810         if (table_index < acpi_gbl_root_table_list.current_table_count) {
811                 status =
812                     acpi_ut_allocate_owner_id(&
813                                               (acpi_gbl_root_table_list.
814                                                tables[table_index].owner_id));
815         }
816
817         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
818         return_ACPI_STATUS(status);
819 }
820
821 /*******************************************************************************
822  *
823  * FUNCTION:    acpi_tb_release_owner_id
824  *
825  * PARAMETERS:  table_index         - Table index
826  *
827  * RETURN:      Status
828  *
829  * DESCRIPTION: Releases owner_id in table_desc
830  *
831  ******************************************************************************/
832
833 acpi_status acpi_tb_release_owner_id(u32 table_index)
834 {
835         acpi_status status = AE_BAD_PARAMETER;
836
837         ACPI_FUNCTION_TRACE(tb_release_owner_id);
838
839         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
840         if (table_index < acpi_gbl_root_table_list.current_table_count) {
841                 acpi_ut_release_owner_id(&
842                                          (acpi_gbl_root_table_list.
843                                           tables[table_index].owner_id));
844                 status = AE_OK;
845         }
846
847         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
848         return_ACPI_STATUS(status);
849 }
850
851 /*******************************************************************************
852  *
853  * FUNCTION:    acpi_tb_get_owner_id
854  *
855  * PARAMETERS:  table_index         - Table index
856  *              owner_id            - Where the table owner_id is returned
857  *
858  * RETURN:      Status
859  *
860  * DESCRIPTION: returns owner_id for the ACPI table
861  *
862  ******************************************************************************/
863
864 acpi_status acpi_tb_get_owner_id(u32 table_index, acpi_owner_id *owner_id)
865 {
866         acpi_status status = AE_BAD_PARAMETER;
867
868         ACPI_FUNCTION_TRACE(tb_get_owner_id);
869
870         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
871         if (table_index < acpi_gbl_root_table_list.current_table_count) {
872                 *owner_id =
873                     acpi_gbl_root_table_list.tables[table_index].owner_id;
874                 status = AE_OK;
875         }
876
877         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
878         return_ACPI_STATUS(status);
879 }
880
881 /*******************************************************************************
882  *
883  * FUNCTION:    acpi_tb_is_table_loaded
884  *
885  * PARAMETERS:  table_index         - Index into the root table
886  *
887  * RETURN:      Table Loaded Flag
888  *
889  ******************************************************************************/
890
891 u8 acpi_tb_is_table_loaded(u32 table_index)
892 {
893         u8 is_loaded = FALSE;
894
895         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
896         if (table_index < acpi_gbl_root_table_list.current_table_count) {
897                 is_loaded = (u8)
898                     (acpi_gbl_root_table_list.tables[table_index].flags &
899                      ACPI_TABLE_IS_LOADED);
900         }
901
902         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
903         return (is_loaded);
904 }
905
906 /*******************************************************************************
907  *
908  * FUNCTION:    acpi_tb_set_table_loaded_flag
909  *
910  * PARAMETERS:  table_index         - Table index
911  *              is_loaded           - TRUE if table is loaded, FALSE otherwise
912  *
913  * RETURN:      None
914  *
915  * DESCRIPTION: Sets the table loaded flag to either TRUE or FALSE.
916  *
917  ******************************************************************************/
918
919 void acpi_tb_set_table_loaded_flag(u32 table_index, u8 is_loaded)
920 {
921
922         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
923         if (table_index < acpi_gbl_root_table_list.current_table_count) {
924                 if (is_loaded) {
925                         acpi_gbl_root_table_list.tables[table_index].flags |=
926                             ACPI_TABLE_IS_LOADED;
927                 } else {
928                         acpi_gbl_root_table_list.tables[table_index].flags &=
929                             ~ACPI_TABLE_IS_LOADED;
930                 }
931         }
932
933         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
934 }
935
936 /*******************************************************************************
937  *
938  * FUNCTION:    acpi_tb_load_table
939  *
940  * PARAMETERS:  table_index             - Table index
941  *              parent_node             - Where table index is returned
942  *
943  * RETURN:      Status
944  *
945  * DESCRIPTION: Load an ACPI table
946  *
947  ******************************************************************************/
948
949 acpi_status
950 acpi_tb_load_table(u32 table_index, struct acpi_namespace_node *parent_node)
951 {
952         struct acpi_table_header *table;
953         acpi_status status;
954         acpi_owner_id owner_id;
955
956         ACPI_FUNCTION_TRACE(tb_load_table);
957
958         /*
959          * Note: Now table is "INSTALLED", it must be validated before
960          * using.
961          */
962         status = acpi_get_table_by_index(table_index, &table);
963         if (ACPI_FAILURE(status)) {
964                 return_ACPI_STATUS(status);
965         }
966
967         status = acpi_ns_load_table(table_index, parent_node);
968
969         /* Execute any module-level code that was found in the table */
970
971         if (!acpi_gbl_parse_table_as_term_list
972             && acpi_gbl_group_module_level_code) {
973                 acpi_ns_exec_module_code_list();
974         }
975
976         /*
977          * Update GPEs for any new _Lxx/_Exx methods. Ignore errors. The host is
978          * responsible for discovering any new wake GPEs by running _PRW methods
979          * that may have been loaded by this table.
980          */
981         status = acpi_tb_get_owner_id(table_index, &owner_id);
982         if (ACPI_SUCCESS(status)) {
983                 acpi_ev_update_gpes(owner_id);
984         }
985
986         /* Invoke table handler */
987
988         acpi_tb_notify_table(ACPI_TABLE_EVENT_LOAD, table);
989         return_ACPI_STATUS(status);
990 }
991
992 /*******************************************************************************
993  *
994  * FUNCTION:    acpi_tb_install_and_load_table
995  *
996  * PARAMETERS:  address                 - Physical address of the table
997  *              flags                   - Allocation flags of the table
998  *              override                - Whether override should be performed
999  *              table_index             - Where table index is returned
1000  *
1001  * RETURN:      Status
1002  *
1003  * DESCRIPTION: Install and load an ACPI table
1004  *
1005  ******************************************************************************/
1006
1007 acpi_status
1008 acpi_tb_install_and_load_table(acpi_physical_address address,
1009                                u8 flags, u8 override, u32 *table_index)
1010 {
1011         acpi_status status;
1012         u32 i;
1013
1014         ACPI_FUNCTION_TRACE(tb_install_and_load_table);
1015
1016         /* Install the table and load it into the namespace */
1017
1018         status = acpi_tb_install_standard_table(address, flags, TRUE,
1019                                                 override, &i);
1020         if (ACPI_FAILURE(status)) {
1021                 goto exit;
1022         }
1023
1024         status = acpi_tb_load_table(i, acpi_gbl_root_node);
1025
1026 exit:
1027         *table_index = i;
1028         return_ACPI_STATUS(status);
1029 }
1030
1031 ACPI_EXPORT_SYMBOL(acpi_tb_install_and_load_table)
1032
1033 /*******************************************************************************
1034  *
1035  * FUNCTION:    acpi_tb_unload_table
1036  *
1037  * PARAMETERS:  table_index             - Table index
1038  *
1039  * RETURN:      Status
1040  *
1041  * DESCRIPTION: Unload an ACPI table
1042  *
1043  ******************************************************************************/
1044
1045 acpi_status acpi_tb_unload_table(u32 table_index)
1046 {
1047         acpi_status status = AE_OK;
1048         struct acpi_table_header *table;
1049
1050         ACPI_FUNCTION_TRACE(tb_unload_table);
1051
1052         /* Ensure the table is still loaded */
1053
1054         if (!acpi_tb_is_table_loaded(table_index)) {
1055                 return_ACPI_STATUS(AE_NOT_EXIST);
1056         }
1057
1058         /* Invoke table handler */
1059
1060         status = acpi_get_table_by_index(table_index, &table);
1061         if (ACPI_SUCCESS(status)) {
1062                 acpi_tb_notify_table(ACPI_TABLE_EVENT_UNLOAD, table);
1063         }
1064
1065         /* Delete the portion of the namespace owned by this table */
1066
1067         status = acpi_tb_delete_namespace_by_owner(table_index);
1068         if (ACPI_FAILURE(status)) {
1069                 return_ACPI_STATUS(status);
1070         }
1071
1072         (void)acpi_tb_release_owner_id(table_index);
1073         acpi_tb_set_table_loaded_flag(table_index, FALSE);
1074         return_ACPI_STATUS(status);
1075 }
1076
1077 ACPI_EXPORT_SYMBOL(acpi_tb_unload_table)
1078
1079 /*******************************************************************************
1080  *
1081  * FUNCTION:    acpi_tb_notify_table
1082  *
1083  * PARAMETERS:  event               - Table event
1084  *              table               - Validated table pointer
1085  *
1086  * RETURN:      None
1087  *
1088  * DESCRIPTION: Notify a table event to the users.
1089  *
1090  ******************************************************************************/
1091
1092 void acpi_tb_notify_table(u32 event, void *table)
1093 {
1094         /* Invoke table handler if present */
1095
1096         if (acpi_gbl_table_handler) {
1097                 (void)acpi_gbl_table_handler(event, table,
1098                                              acpi_gbl_table_handler_context);
1099         }
1100 }