ACPICA: Implemented full support for deferred execution for the TermArg string argume...
[linux-2.6] / drivers / acpi / tables / tbinstal.c
1 /******************************************************************************
2  *
3  * Module Name: tbinstal - ACPI table installation and removal
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2007, R. Byron Moore
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 <acpi/acnamesp.h>
46 #include <acpi/actables.h>
47
48 #define _COMPONENT          ACPI_TABLES
49 ACPI_MODULE_NAME("tbinstal")
50
51 /******************************************************************************
52  *
53  * FUNCTION:    acpi_tb_verify_table
54  *
55  * PARAMETERS:  table_desc          - table
56  *
57  * RETURN:      Status
58  *
59  * DESCRIPTION: this function is called to verify and map table
60  *
61  *****************************************************************************/
62 acpi_status acpi_tb_verify_table(struct acpi_table_desc *table_desc)
63 {
64         acpi_status status = AE_OK;
65
66         ACPI_FUNCTION_TRACE(tb_verify_table);
67
68         /* Map the table if necessary */
69
70         if (!table_desc->pointer) {
71                 if ((table_desc->flags & ACPI_TABLE_ORIGIN_MASK) ==
72                     ACPI_TABLE_ORIGIN_MAPPED) {
73                         table_desc->pointer =
74                             acpi_os_map_memory(table_desc->address,
75                                                table_desc->length);
76                 }
77                 if (!table_desc->pointer) {
78                         return_ACPI_STATUS(AE_NO_MEMORY);
79                 }
80         }
81
82         /* FACS is the odd table, has no standard ACPI header and no checksum */
83
84         if (!ACPI_COMPARE_NAME(&table_desc->signature, ACPI_SIG_FACS)) {
85
86                 /* Always calculate checksum, ignore bad checksum if requested */
87
88                 status =
89                     acpi_tb_verify_checksum(table_desc->pointer,
90                                             table_desc->length);
91         }
92
93         return_ACPI_STATUS(status);
94 }
95
96 /*******************************************************************************
97  *
98  * FUNCTION:    acpi_tb_add_table
99  *
100  * PARAMETERS:  table_desc          - Table descriptor
101  *              table_index         - Where the table index is returned
102  *
103  * RETURN:      Status
104  *
105  * DESCRIPTION: This function is called to add the ACPI table
106  *
107  ******************************************************************************/
108
109 acpi_status
110 acpi_tb_add_table(struct acpi_table_desc *table_desc,
111                   acpi_native_uint * table_index)
112 {
113         acpi_native_uint i;
114         acpi_native_uint length;
115         acpi_status status = AE_OK;
116
117         ACPI_FUNCTION_TRACE(tb_add_table);
118
119         if (!table_desc->pointer) {
120                 status = acpi_tb_verify_table(table_desc);
121                 if (ACPI_FAILURE(status) || !table_desc->pointer) {
122                         return_ACPI_STATUS(status);
123                 }
124         }
125
126         /* The table must be either an SSDT or a PSDT or an OEMx */
127
128         if (!ACPI_COMPARE_NAME(table_desc->pointer->signature, ACPI_SIG_PSDT)&&
129             !ACPI_COMPARE_NAME(table_desc->pointer->signature, ACPI_SIG_SSDT)&&
130             strncmp(table_desc->pointer->signature, "OEM", 3)) {
131                 /* Check for a printable name */
132                 if (acpi_ut_valid_acpi_name(
133                         *(u32 *) table_desc->pointer->signature)) {
134                         ACPI_ERROR((AE_INFO, "Table has invalid signature "
135                                         "[%4.4s], must be SSDT or PSDT",
136                                     table_desc->pointer->signature));
137                 } else {
138                         ACPI_ERROR((AE_INFO, "Table has invalid signature "
139                                         "(0x%8.8X), must be SSDT or PSDT",
140                                     *(u32 *) table_desc->pointer->signature));
141                 }
142                 return_ACPI_STATUS(AE_BAD_SIGNATURE);
143         }
144
145         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
146
147         /* Check if table is already registered */
148
149         for (i = 0; i < acpi_gbl_root_table_list.count; ++i) {
150                 if (!acpi_gbl_root_table_list.tables[i].pointer) {
151                         status =
152                             acpi_tb_verify_table(&acpi_gbl_root_table_list.
153                                                  tables[i]);
154                         if (ACPI_FAILURE(status)
155                             || !acpi_gbl_root_table_list.tables[i].pointer) {
156                                 continue;
157                         }
158                 }
159
160                 length = ACPI_MIN(table_desc->length,
161                                   acpi_gbl_root_table_list.tables[i].length);
162                 if (ACPI_MEMCMP(table_desc->pointer,
163                                 acpi_gbl_root_table_list.tables[i].pointer,
164                                 length)) {
165                         continue;
166                 }
167
168                 /* Table is already registered */
169
170                 acpi_tb_delete_table(table_desc);
171                 *table_index = i;
172                 goto release;
173         }
174
175         /*
176          * Add the table to the global table list
177          */
178         status = acpi_tb_store_table(table_desc->address, table_desc->pointer,
179                                      table_desc->length, table_desc->flags,
180                                      table_index);
181         if (ACPI_FAILURE(status)) {
182                 goto release;
183         }
184
185         acpi_tb_print_table_header(table_desc->address, table_desc->pointer);
186
187       release:
188         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
189         return_ACPI_STATUS(status);
190 }
191
192 /*******************************************************************************
193  *
194  * FUNCTION:    acpi_tb_resize_root_table_list
195  *
196  * PARAMETERS:  None
197  *
198  * RETURN:      Status
199  *
200  * DESCRIPTION: Expand the size of global table array
201  *
202  ******************************************************************************/
203
204 acpi_status acpi_tb_resize_root_table_list(void)
205 {
206         struct acpi_table_desc *tables;
207
208         ACPI_FUNCTION_TRACE(tb_resize_root_table_list);
209
210         /* allow_resize flag is a parameter to acpi_initialize_tables */
211
212         if (!(acpi_gbl_root_table_list.flags & ACPI_ROOT_ALLOW_RESIZE)) {
213                 ACPI_ERROR((AE_INFO,
214                             "Resize of Root Table Array is not allowed"));
215                 return_ACPI_STATUS(AE_SUPPORT);
216         }
217
218         /* Increase the Table Array size */
219
220         tables = ACPI_ALLOCATE_ZEROED((acpi_gbl_root_table_list.size +
221                                        ACPI_ROOT_TABLE_SIZE_INCREMENT)
222                                       * sizeof(struct acpi_table_desc));
223         if (!tables) {
224                 ACPI_ERROR((AE_INFO,
225                             "Could not allocate new root table array"));
226                 return_ACPI_STATUS(AE_NO_MEMORY);
227         }
228
229         /* Copy and free the previous table array */
230
231         if (acpi_gbl_root_table_list.tables) {
232                 ACPI_MEMCPY(tables, acpi_gbl_root_table_list.tables,
233                             acpi_gbl_root_table_list.size *
234                             sizeof(struct acpi_table_desc));
235
236                 if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
237                         ACPI_FREE(acpi_gbl_root_table_list.tables);
238                 }
239         }
240
241         acpi_gbl_root_table_list.tables = tables;
242         acpi_gbl_root_table_list.size += ACPI_ROOT_TABLE_SIZE_INCREMENT;
243         acpi_gbl_root_table_list.flags |= (u8) ACPI_ROOT_ORIGIN_ALLOCATED;
244
245         return_ACPI_STATUS(AE_OK);
246 }
247
248 /*******************************************************************************
249  *
250  * FUNCTION:    acpi_tb_store_table
251  *
252  * PARAMETERS:  Address             - Table address
253  *              Table               - Table header
254  *              Length              - Table length
255  *              Flags               - flags
256  *
257  * RETURN:      Status and table index.
258  *
259  * DESCRIPTION: Add an ACPI table to the global table list
260  *
261  ******************************************************************************/
262
263 acpi_status
264 acpi_tb_store_table(acpi_physical_address address,
265                     struct acpi_table_header *table,
266                     u32 length, u8 flags, acpi_native_uint * table_index)
267 {
268         acpi_status status = AE_OK;
269
270         /* Ensure that there is room for the table in the Root Table List */
271
272         if (acpi_gbl_root_table_list.count >= acpi_gbl_root_table_list.size) {
273                 status = acpi_tb_resize_root_table_list();
274                 if (ACPI_FAILURE(status)) {
275                         return (status);
276                 }
277         }
278
279         /* Initialize added table */
280
281         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
282             address = address;
283         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
284             pointer = table;
285         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].length =
286             length;
287         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
288             owner_id = 0;
289         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].flags =
290             flags;
291
292         ACPI_MOVE_32_TO_32(&
293                            (acpi_gbl_root_table_list.
294                             tables[acpi_gbl_root_table_list.count].signature),
295                            table->signature);
296
297         *table_index = acpi_gbl_root_table_list.count;
298         acpi_gbl_root_table_list.count++;
299         return (status);
300 }
301
302 /*******************************************************************************
303  *
304  * FUNCTION:    acpi_tb_delete_table
305  *
306  * PARAMETERS:  table_index         - Table index
307  *
308  * RETURN:      None
309  *
310  * DESCRIPTION: Delete one internal ACPI table
311  *
312  ******************************************************************************/
313
314 void acpi_tb_delete_table(struct acpi_table_desc *table_desc)
315 {
316         /* Table must be mapped or allocated */
317         if (!table_desc->pointer) {
318                 return;
319         }
320         switch (table_desc->flags & ACPI_TABLE_ORIGIN_MASK) {
321         case ACPI_TABLE_ORIGIN_MAPPED:
322                 acpi_os_unmap_memory(table_desc->pointer, table_desc->length);
323                 break;
324         case ACPI_TABLE_ORIGIN_ALLOCATED:
325                 ACPI_FREE(table_desc->pointer);
326                 break;
327         default:;
328         }
329
330         table_desc->pointer = NULL;
331 }
332
333 /*******************************************************************************
334  *
335  * FUNCTION:    acpi_tb_terminate
336  *
337  * PARAMETERS:  None
338  *
339  * RETURN:      None
340  *
341  * DESCRIPTION: Delete all internal ACPI tables
342  *
343  ******************************************************************************/
344
345 void acpi_tb_terminate(void)
346 {
347         acpi_native_uint i;
348
349         ACPI_FUNCTION_TRACE(tb_terminate);
350
351         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
352
353         /* Delete the individual tables */
354
355         for (i = 0; i < acpi_gbl_root_table_list.count; ++i) {
356                 acpi_tb_delete_table(&acpi_gbl_root_table_list.tables[i]);
357         }
358
359         /*
360          * Delete the root table array if allocated locally. Array cannot be
361          * mapped, so we don't need to check for that flag.
362          */
363         if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
364                 ACPI_FREE(acpi_gbl_root_table_list.tables);
365         }
366
367         acpi_gbl_root_table_list.tables = NULL;
368         acpi_gbl_root_table_list.flags = 0;
369         acpi_gbl_root_table_list.count = 0;
370
371         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "ACPI Tables freed\n"));
372         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
373 }
374
375 /*******************************************************************************
376  *
377  * FUNCTION:    acpi_tb_delete_namespace_by_owner
378  *
379  * PARAMETERS:  table_index         - Table index
380  *
381  * RETURN:      None
382  *
383  * DESCRIPTION: Delete all namespace objects created when this table was loaded.
384  *
385  ******************************************************************************/
386
387 void acpi_tb_delete_namespace_by_owner(acpi_native_uint table_index)
388 {
389         acpi_owner_id owner_id;
390
391         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
392         if (table_index < acpi_gbl_root_table_list.count) {
393                 owner_id =
394                     acpi_gbl_root_table_list.tables[table_index].owner_id;
395         } else {
396                 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
397                 return;
398         }
399
400         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
401         acpi_ns_delete_namespace_by_owner(owner_id);
402 }
403
404 /*******************************************************************************
405  *
406  * FUNCTION:    acpi_tb_allocate_owner_id
407  *
408  * PARAMETERS:  table_index         - Table index
409  *
410  * RETURN:      Status
411  *
412  * DESCRIPTION: Allocates owner_id in table_desc
413  *
414  ******************************************************************************/
415
416 acpi_status acpi_tb_allocate_owner_id(acpi_native_uint table_index)
417 {
418         acpi_status status = AE_BAD_PARAMETER;
419
420         ACPI_FUNCTION_TRACE(tb_allocate_owner_id);
421
422         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
423         if (table_index < acpi_gbl_root_table_list.count) {
424                 status = acpi_ut_allocate_owner_id
425                     (&(acpi_gbl_root_table_list.tables[table_index].owner_id));
426         }
427
428         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
429         return_ACPI_STATUS(status);
430 }
431
432 /*******************************************************************************
433  *
434  * FUNCTION:    acpi_tb_release_owner_id
435  *
436  * PARAMETERS:  table_index         - Table index
437  *
438  * RETURN:      Status
439  *
440  * DESCRIPTION: Releases owner_id in table_desc
441  *
442  ******************************************************************************/
443
444 acpi_status acpi_tb_release_owner_id(acpi_native_uint table_index)
445 {
446         acpi_status status = AE_BAD_PARAMETER;
447
448         ACPI_FUNCTION_TRACE(tb_release_owner_id);
449
450         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
451         if (table_index < acpi_gbl_root_table_list.count) {
452                 acpi_ut_release_owner_id(&
453                                          (acpi_gbl_root_table_list.
454                                           tables[table_index].owner_id));
455                 status = AE_OK;
456         }
457
458         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
459         return_ACPI_STATUS(status);
460 }
461
462 /*******************************************************************************
463  *
464  * FUNCTION:    acpi_tb_get_owner_id
465  *
466  * PARAMETERS:  table_index         - Table index
467  *              owner_id            - Where the table owner_id is returned
468  *
469  * RETURN:      Status
470  *
471  * DESCRIPTION: returns owner_id for the ACPI table
472  *
473  ******************************************************************************/
474
475 acpi_status
476 acpi_tb_get_owner_id(acpi_native_uint table_index, acpi_owner_id * owner_id)
477 {
478         acpi_status status = AE_BAD_PARAMETER;
479
480         ACPI_FUNCTION_TRACE(tb_get_owner_id);
481
482         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
483         if (table_index < acpi_gbl_root_table_list.count) {
484                 *owner_id =
485                     acpi_gbl_root_table_list.tables[table_index].owner_id;
486                 status = AE_OK;
487         }
488
489         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
490         return_ACPI_STATUS(status);
491 }
492
493 /*******************************************************************************
494  *
495  * FUNCTION:    acpi_tb_is_table_loaded
496  *
497  * PARAMETERS:  table_index         - Table index
498  *
499  * RETURN:      Table Loaded Flag
500  *
501  ******************************************************************************/
502
503 u8 acpi_tb_is_table_loaded(acpi_native_uint table_index)
504 {
505         u8 is_loaded = FALSE;
506
507         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
508         if (table_index < acpi_gbl_root_table_list.count) {
509                 is_loaded = (u8)
510                     (acpi_gbl_root_table_list.tables[table_index].
511                      flags & ACPI_TABLE_IS_LOADED);
512         }
513
514         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
515         return (is_loaded);
516 }
517
518 /*******************************************************************************
519  *
520  * FUNCTION:    acpi_tb_set_table_loaded_flag
521  *
522  * PARAMETERS:  table_index         - Table index
523  *              is_loaded           - TRUE if table is loaded, FALSE otherwise
524  *
525  * RETURN:      None
526  *
527  * DESCRIPTION: Sets the table loaded flag to either TRUE or FALSE.
528  *
529  ******************************************************************************/
530
531 void acpi_tb_set_table_loaded_flag(acpi_native_uint table_index, u8 is_loaded)
532 {
533
534         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
535         if (table_index < acpi_gbl_root_table_list.count) {
536                 if (is_loaded) {
537                         acpi_gbl_root_table_list.tables[table_index].flags |=
538                             ACPI_TABLE_IS_LOADED;
539                 } else {
540                         acpi_gbl_root_table_list.tables[table_index].flags &=
541                             ~ACPI_TABLE_IS_LOADED;
542                 }
543         }
544
545         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
546 }