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