Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[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 - 2008, 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 <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                 status = AE_ALREADY_EXISTS;
173                 goto release;
174         }
175
176         /*
177          * Add the table to the global table list
178          */
179         status = acpi_tb_store_table(table_desc->address, table_desc->pointer,
180                                      table_desc->length, table_desc->flags,
181                                      table_index);
182         if (ACPI_FAILURE(status)) {
183                 goto release;
184         }
185
186         acpi_tb_print_table_header(table_desc->address, table_desc->pointer);
187
188       release:
189         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
190         return_ACPI_STATUS(status);
191 }
192
193 /*******************************************************************************
194  *
195  * FUNCTION:    acpi_tb_resize_root_table_list
196  *
197  * PARAMETERS:  None
198  *
199  * RETURN:      Status
200  *
201  * DESCRIPTION: Expand the size of global table array
202  *
203  ******************************************************************************/
204
205 acpi_status acpi_tb_resize_root_table_list(void)
206 {
207         struct acpi_table_desc *tables;
208
209         ACPI_FUNCTION_TRACE(tb_resize_root_table_list);
210
211         /* allow_resize flag is a parameter to acpi_initialize_tables */
212
213         if (!(acpi_gbl_root_table_list.flags & ACPI_ROOT_ALLOW_RESIZE)) {
214                 ACPI_ERROR((AE_INFO,
215                             "Resize of Root Table Array is not allowed"));
216                 return_ACPI_STATUS(AE_SUPPORT);
217         }
218
219         /* Increase the Table Array size */
220
221         tables = ACPI_ALLOCATE_ZEROED((acpi_gbl_root_table_list.size +
222                                        ACPI_ROOT_TABLE_SIZE_INCREMENT)
223                                       * sizeof(struct acpi_table_desc));
224         if (!tables) {
225                 ACPI_ERROR((AE_INFO,
226                             "Could not allocate new root table array"));
227                 return_ACPI_STATUS(AE_NO_MEMORY);
228         }
229
230         /* Copy and free the previous table array */
231
232         if (acpi_gbl_root_table_list.tables) {
233                 ACPI_MEMCPY(tables, acpi_gbl_root_table_list.tables,
234                             acpi_gbl_root_table_list.size *
235                             sizeof(struct acpi_table_desc));
236
237                 if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
238                         ACPI_FREE(acpi_gbl_root_table_list.tables);
239                 }
240         }
241
242         acpi_gbl_root_table_list.tables = tables;
243         acpi_gbl_root_table_list.size += ACPI_ROOT_TABLE_SIZE_INCREMENT;
244         acpi_gbl_root_table_list.flags |= (u8) ACPI_ROOT_ORIGIN_ALLOCATED;
245
246         return_ACPI_STATUS(AE_OK);
247 }
248
249 /*******************************************************************************
250  *
251  * FUNCTION:    acpi_tb_store_table
252  *
253  * PARAMETERS:  Address             - Table address
254  *              Table               - Table header
255  *              Length              - Table length
256  *              Flags               - flags
257  *
258  * RETURN:      Status and table index.
259  *
260  * DESCRIPTION: Add an ACPI table to the global table list
261  *
262  ******************************************************************************/
263
264 acpi_status
265 acpi_tb_store_table(acpi_physical_address address,
266                     struct acpi_table_header *table,
267                     u32 length, u8 flags, acpi_native_uint * table_index)
268 {
269         acpi_status status = AE_OK;
270
271         /* Ensure that there is room for the table in the Root Table List */
272
273         if (acpi_gbl_root_table_list.count >= acpi_gbl_root_table_list.size) {
274                 status = acpi_tb_resize_root_table_list();
275                 if (ACPI_FAILURE(status)) {
276                         return (status);
277                 }
278         }
279
280         /* Initialize added table */
281
282         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
283             address = address;
284         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
285             pointer = table;
286         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].length =
287             length;
288         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
289             owner_id = 0;
290         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].flags =
291             flags;
292
293         ACPI_MOVE_32_TO_32(&
294                            (acpi_gbl_root_table_list.
295                             tables[acpi_gbl_root_table_list.count].signature),
296                            table->signature);
297
298         *table_index = acpi_gbl_root_table_list.count;
299         acpi_gbl_root_table_list.count++;
300         return (status);
301 }
302
303 /*******************************************************************************
304  *
305  * FUNCTION:    acpi_tb_delete_table
306  *
307  * PARAMETERS:  table_index         - Table index
308  *
309  * RETURN:      None
310  *
311  * DESCRIPTION: Delete one internal ACPI table
312  *
313  ******************************************************************************/
314
315 void acpi_tb_delete_table(struct acpi_table_desc *table_desc)
316 {
317         /* Table must be mapped or allocated */
318         if (!table_desc->pointer) {
319                 return;
320         }
321         switch (table_desc->flags & ACPI_TABLE_ORIGIN_MASK) {
322         case ACPI_TABLE_ORIGIN_MAPPED:
323                 acpi_os_unmap_memory(table_desc->pointer, table_desc->length);
324                 break;
325         case ACPI_TABLE_ORIGIN_ALLOCATED:
326                 ACPI_FREE(table_desc->pointer);
327                 break;
328         default:;
329         }
330
331         table_desc->pointer = NULL;
332 }
333
334 /*******************************************************************************
335  *
336  * FUNCTION:    acpi_tb_terminate
337  *
338  * PARAMETERS:  None
339  *
340  * RETURN:      None
341  *
342  * DESCRIPTION: Delete all internal ACPI tables
343  *
344  ******************************************************************************/
345
346 void acpi_tb_terminate(void)
347 {
348         acpi_native_uint i;
349
350         ACPI_FUNCTION_TRACE(tb_terminate);
351
352         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
353
354         /* Delete the individual tables */
355
356         for (i = 0; i < acpi_gbl_root_table_list.count; ++i) {
357                 acpi_tb_delete_table(&acpi_gbl_root_table_list.tables[i]);
358         }
359
360         /*
361          * Delete the root table array if allocated locally. Array cannot be
362          * mapped, so we don't need to check for that flag.
363          */
364         if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
365                 ACPI_FREE(acpi_gbl_root_table_list.tables);
366         }
367
368         acpi_gbl_root_table_list.tables = NULL;
369         acpi_gbl_root_table_list.flags = 0;
370         acpi_gbl_root_table_list.count = 0;
371
372         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "ACPI Tables freed\n"));
373         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
374 }
375
376 /*******************************************************************************
377  *
378  * FUNCTION:    acpi_tb_delete_namespace_by_owner
379  *
380  * PARAMETERS:  table_index         - Table index
381  *
382  * RETURN:      None
383  *
384  * DESCRIPTION: Delete all namespace objects created when this table was loaded.
385  *
386  ******************************************************************************/
387
388 void acpi_tb_delete_namespace_by_owner(acpi_native_uint table_index)
389 {
390         acpi_owner_id owner_id;
391
392         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
393         if (table_index < acpi_gbl_root_table_list.count) {
394                 owner_id =
395                     acpi_gbl_root_table_list.tables[table_index].owner_id;
396         } else {
397                 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
398                 return;
399         }
400
401         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
402         acpi_ns_delete_namespace_by_owner(owner_id);
403 }
404
405 /*******************************************************************************
406  *
407  * FUNCTION:    acpi_tb_allocate_owner_id
408  *
409  * PARAMETERS:  table_index         - Table index
410  *
411  * RETURN:      Status
412  *
413  * DESCRIPTION: Allocates owner_id in table_desc
414  *
415  ******************************************************************************/
416
417 acpi_status acpi_tb_allocate_owner_id(acpi_native_uint table_index)
418 {
419         acpi_status status = AE_BAD_PARAMETER;
420
421         ACPI_FUNCTION_TRACE(tb_allocate_owner_id);
422
423         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
424         if (table_index < acpi_gbl_root_table_list.count) {
425                 status = acpi_ut_allocate_owner_id
426                     (&(acpi_gbl_root_table_list.tables[table_index].owner_id));
427         }
428
429         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
430         return_ACPI_STATUS(status);
431 }
432
433 /*******************************************************************************
434  *
435  * FUNCTION:    acpi_tb_release_owner_id
436  *
437  * PARAMETERS:  table_index         - Table index
438  *
439  * RETURN:      Status
440  *
441  * DESCRIPTION: Releases owner_id in table_desc
442  *
443  ******************************************************************************/
444
445 acpi_status acpi_tb_release_owner_id(acpi_native_uint table_index)
446 {
447         acpi_status status = AE_BAD_PARAMETER;
448
449         ACPI_FUNCTION_TRACE(tb_release_owner_id);
450
451         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
452         if (table_index < acpi_gbl_root_table_list.count) {
453                 acpi_ut_release_owner_id(&
454                                          (acpi_gbl_root_table_list.
455                                           tables[table_index].owner_id));
456                 status = AE_OK;
457         }
458
459         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
460         return_ACPI_STATUS(status);
461 }
462
463 /*******************************************************************************
464  *
465  * FUNCTION:    acpi_tb_get_owner_id
466  *
467  * PARAMETERS:  table_index         - Table index
468  *              owner_id            - Where the table owner_id is returned
469  *
470  * RETURN:      Status
471  *
472  * DESCRIPTION: returns owner_id for the ACPI table
473  *
474  ******************************************************************************/
475
476 acpi_status
477 acpi_tb_get_owner_id(acpi_native_uint table_index, acpi_owner_id * owner_id)
478 {
479         acpi_status status = AE_BAD_PARAMETER;
480
481         ACPI_FUNCTION_TRACE(tb_get_owner_id);
482
483         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
484         if (table_index < acpi_gbl_root_table_list.count) {
485                 *owner_id =
486                     acpi_gbl_root_table_list.tables[table_index].owner_id;
487                 status = AE_OK;
488         }
489
490         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
491         return_ACPI_STATUS(status);
492 }
493
494 /*******************************************************************************
495  *
496  * FUNCTION:    acpi_tb_is_table_loaded
497  *
498  * PARAMETERS:  table_index         - Table index
499  *
500  * RETURN:      Table Loaded Flag
501  *
502  ******************************************************************************/
503
504 u8 acpi_tb_is_table_loaded(acpi_native_uint table_index)
505 {
506         u8 is_loaded = FALSE;
507
508         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
509         if (table_index < acpi_gbl_root_table_list.count) {
510                 is_loaded = (u8)
511                     (acpi_gbl_root_table_list.tables[table_index].
512                      flags & ACPI_TABLE_IS_LOADED);
513         }
514
515         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
516         return (is_loaded);
517 }
518
519 /*******************************************************************************
520  *
521  * FUNCTION:    acpi_tb_set_table_loaded_flag
522  *
523  * PARAMETERS:  table_index         - Table index
524  *              is_loaded           - TRUE if table is loaded, FALSE otherwise
525  *
526  * RETURN:      None
527  *
528  * DESCRIPTION: Sets the table loaded flag to either TRUE or FALSE.
529  *
530  ******************************************************************************/
531
532 void acpi_tb_set_table_loaded_flag(acpi_native_uint table_index, u8 is_loaded)
533 {
534
535         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
536         if (table_index < acpi_gbl_root_table_list.count) {
537                 if (is_loaded) {
538                         acpi_gbl_root_table_list.tables[table_index].flags |=
539                             ACPI_TABLE_IS_LOADED;
540                 } else {
541                         acpi_gbl_root_table_list.tables[table_index].flags &=
542                             ~ACPI_TABLE_IS_LOADED;
543                 }
544         }
545
546         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
547 }