Merge branch 'merge' into next
[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, u32 *table_index)
111 {
112         u32 i;
113         acpi_status status = AE_OK;
114
115         ACPI_FUNCTION_TRACE(tb_add_table);
116
117         if (!table_desc->pointer) {
118                 status = acpi_tb_verify_table(table_desc);
119                 if (ACPI_FAILURE(status) || !table_desc->pointer) {
120                         return_ACPI_STATUS(status);
121                 }
122         }
123
124         /*
125          * Originally, we checked the table signature for "SSDT" or "PSDT" here.
126          * Next, we added support for OEMx tables, signature "OEM".
127          * Valid tables were encountered with a null signature, so we've just
128          * given up on validating the signature, since it seems to be a waste
129          * of code. The original code was removed (05/2008).
130          */
131
132         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
133
134         /* Check if table is already registered */
135
136         for (i = 0; i < acpi_gbl_root_table_list.count; ++i) {
137                 if (!acpi_gbl_root_table_list.tables[i].pointer) {
138                         status =
139                             acpi_tb_verify_table(&acpi_gbl_root_table_list.
140                                                  tables[i]);
141                         if (ACPI_FAILURE(status)
142                             || !acpi_gbl_root_table_list.tables[i].pointer) {
143                                 continue;
144                         }
145                 }
146
147                 /*
148                  * Check for a table match on the entire table length,
149                  * not just the header.
150                  */
151                 if (table_desc->length !=
152                     acpi_gbl_root_table_list.tables[i].length) {
153                         continue;
154                 }
155
156                 if (ACPI_MEMCMP(table_desc->pointer,
157                                 acpi_gbl_root_table_list.tables[i].pointer,
158                                 acpi_gbl_root_table_list.tables[i].length)) {
159                         continue;
160                 }
161
162                 /*
163                  * Note: the current mechanism does not unregister a table if it is
164                  * dynamically unloaded. The related namespace entries are deleted,
165                  * but the table remains in the root table list.
166                  *
167                  * The assumption here is that the number of different tables that
168                  * will be loaded is actually small, and there is minimal overhead
169                  * in just keeping the table in case it is needed again.
170                  *
171                  * If this assumption changes in the future (perhaps on large
172                  * machines with many table load/unload operations), tables will
173                  * need to be unregistered when they are unloaded, and slots in the
174                  * root table list should be reused when empty.
175                  */
176
177                 /*
178                  * Table is already registered.
179                  * We can delete the table that was passed as a parameter.
180                  */
181                 acpi_tb_delete_table(table_desc);
182                 *table_index = i;
183
184                 if (acpi_gbl_root_table_list.tables[i].
185                     flags & ACPI_TABLE_IS_LOADED) {
186
187                         /* Table is still loaded, this is an error */
188
189                         status = AE_ALREADY_EXISTS;
190                         goto release;
191                 } else {
192                         /* Table was unloaded, allow it to be reloaded */
193
194                         table_desc->pointer =
195                             acpi_gbl_root_table_list.tables[i].pointer;
196                         table_desc->address =
197                             acpi_gbl_root_table_list.tables[i].address;
198                         status = AE_OK;
199                         goto print_header;
200                 }
201         }
202
203         /* Add the table to the global root table list */
204
205         status = acpi_tb_store_table(table_desc->address, table_desc->pointer,
206                                      table_desc->length, table_desc->flags,
207                                      table_index);
208         if (ACPI_FAILURE(status)) {
209                 goto release;
210         }
211
212       print_header:
213         acpi_tb_print_table_header(table_desc->address, table_desc->pointer);
214
215       release:
216         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
217         return_ACPI_STATUS(status);
218 }
219
220 /*******************************************************************************
221  *
222  * FUNCTION:    acpi_tb_resize_root_table_list
223  *
224  * PARAMETERS:  None
225  *
226  * RETURN:      Status
227  *
228  * DESCRIPTION: Expand the size of global table array
229  *
230  ******************************************************************************/
231
232 acpi_status acpi_tb_resize_root_table_list(void)
233 {
234         struct acpi_table_desc *tables;
235
236         ACPI_FUNCTION_TRACE(tb_resize_root_table_list);
237
238         /* allow_resize flag is a parameter to acpi_initialize_tables */
239
240         if (!(acpi_gbl_root_table_list.flags & ACPI_ROOT_ALLOW_RESIZE)) {
241                 ACPI_ERROR((AE_INFO,
242                             "Resize of Root Table Array is not allowed"));
243                 return_ACPI_STATUS(AE_SUPPORT);
244         }
245
246         /* Increase the Table Array size */
247
248         tables = ACPI_ALLOCATE_ZEROED(((acpi_size) acpi_gbl_root_table_list.
249                                        size + ACPI_ROOT_TABLE_SIZE_INCREMENT)
250                                       * sizeof(struct acpi_table_desc));
251         if (!tables) {
252                 ACPI_ERROR((AE_INFO,
253                             "Could not allocate new root table array"));
254                 return_ACPI_STATUS(AE_NO_MEMORY);
255         }
256
257         /* Copy and free the previous table array */
258
259         if (acpi_gbl_root_table_list.tables) {
260                 ACPI_MEMCPY(tables, acpi_gbl_root_table_list.tables,
261                             (acpi_size) acpi_gbl_root_table_list.size *
262                             sizeof(struct acpi_table_desc));
263
264                 if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
265                         ACPI_FREE(acpi_gbl_root_table_list.tables);
266                 }
267         }
268
269         acpi_gbl_root_table_list.tables = tables;
270         acpi_gbl_root_table_list.size += ACPI_ROOT_TABLE_SIZE_INCREMENT;
271         acpi_gbl_root_table_list.flags |= (u8) ACPI_ROOT_ORIGIN_ALLOCATED;
272
273         return_ACPI_STATUS(AE_OK);
274 }
275
276 /*******************************************************************************
277  *
278  * FUNCTION:    acpi_tb_store_table
279  *
280  * PARAMETERS:  Address             - Table address
281  *              Table               - Table header
282  *              Length              - Table length
283  *              Flags               - flags
284  *
285  * RETURN:      Status and table index.
286  *
287  * DESCRIPTION: Add an ACPI table to the global table list
288  *
289  ******************************************************************************/
290
291 acpi_status
292 acpi_tb_store_table(acpi_physical_address address,
293                     struct acpi_table_header *table,
294                     u32 length, u8 flags, u32 *table_index)
295 {
296         acpi_status status = AE_OK;
297
298         /* Ensure that there is room for the table in the Root Table List */
299
300         if (acpi_gbl_root_table_list.count >= acpi_gbl_root_table_list.size) {
301                 status = acpi_tb_resize_root_table_list();
302                 if (ACPI_FAILURE(status)) {
303                         return (status);
304                 }
305         }
306
307         /* Initialize added table */
308
309         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
310             address = address;
311         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
312             pointer = table;
313         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].length =
314             length;
315         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
316             owner_id = 0;
317         acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].flags =
318             flags;
319
320         ACPI_MOVE_32_TO_32(&
321                            (acpi_gbl_root_table_list.
322                             tables[acpi_gbl_root_table_list.count].signature),
323                            table->signature);
324
325         *table_index = acpi_gbl_root_table_list.count;
326         acpi_gbl_root_table_list.count++;
327         return (status);
328 }
329
330 /*******************************************************************************
331  *
332  * FUNCTION:    acpi_tb_delete_table
333  *
334  * PARAMETERS:  table_index         - Table index
335  *
336  * RETURN:      None
337  *
338  * DESCRIPTION: Delete one internal ACPI table
339  *
340  ******************************************************************************/
341
342 void acpi_tb_delete_table(struct acpi_table_desc *table_desc)
343 {
344         /* Table must be mapped or allocated */
345         if (!table_desc->pointer) {
346                 return;
347         }
348         switch (table_desc->flags & ACPI_TABLE_ORIGIN_MASK) {
349         case ACPI_TABLE_ORIGIN_MAPPED:
350                 acpi_os_unmap_memory(table_desc->pointer, table_desc->length);
351                 break;
352         case ACPI_TABLE_ORIGIN_ALLOCATED:
353                 ACPI_FREE(table_desc->pointer);
354                 break;
355         default:;
356         }
357
358         table_desc->pointer = NULL;
359 }
360
361 /*******************************************************************************
362  *
363  * FUNCTION:    acpi_tb_terminate
364  *
365  * PARAMETERS:  None
366  *
367  * RETURN:      None
368  *
369  * DESCRIPTION: Delete all internal ACPI tables
370  *
371  ******************************************************************************/
372
373 void acpi_tb_terminate(void)
374 {
375         u32 i;
376
377         ACPI_FUNCTION_TRACE(tb_terminate);
378
379         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
380
381         /* Delete the individual tables */
382
383         for (i = 0; i < acpi_gbl_root_table_list.count; ++i) {
384                 acpi_tb_delete_table(&acpi_gbl_root_table_list.tables[i]);
385         }
386
387         /*
388          * Delete the root table array if allocated locally. Array cannot be
389          * mapped, so we don't need to check for that flag.
390          */
391         if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
392                 ACPI_FREE(acpi_gbl_root_table_list.tables);
393         }
394
395         acpi_gbl_root_table_list.tables = NULL;
396         acpi_gbl_root_table_list.flags = 0;
397         acpi_gbl_root_table_list.count = 0;
398
399         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "ACPI Tables freed\n"));
400         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
401 }
402
403 /*******************************************************************************
404  *
405  * FUNCTION:    acpi_tb_delete_namespace_by_owner
406  *
407  * PARAMETERS:  table_index         - Table index
408  *
409  * RETURN:      None
410  *
411  * DESCRIPTION: Delete all namespace objects created when this table was loaded.
412  *
413  ******************************************************************************/
414
415 void acpi_tb_delete_namespace_by_owner(u32 table_index)
416 {
417         acpi_owner_id owner_id;
418
419         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
420         if (table_index < acpi_gbl_root_table_list.count) {
421                 owner_id =
422                     acpi_gbl_root_table_list.tables[table_index].owner_id;
423         } else {
424                 (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
425                 return;
426         }
427
428         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
429         acpi_ns_delete_namespace_by_owner(owner_id);
430 }
431
432 /*******************************************************************************
433  *
434  * FUNCTION:    acpi_tb_allocate_owner_id
435  *
436  * PARAMETERS:  table_index         - Table index
437  *
438  * RETURN:      Status
439  *
440  * DESCRIPTION: Allocates owner_id in table_desc
441  *
442  ******************************************************************************/
443
444 acpi_status acpi_tb_allocate_owner_id(u32 table_index)
445 {
446         acpi_status status = AE_BAD_PARAMETER;
447
448         ACPI_FUNCTION_TRACE(tb_allocate_owner_id);
449
450         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
451         if (table_index < acpi_gbl_root_table_list.count) {
452                 status = acpi_ut_allocate_owner_id
453                     (&(acpi_gbl_root_table_list.tables[table_index].owner_id));
454         }
455
456         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
457         return_ACPI_STATUS(status);
458 }
459
460 /*******************************************************************************
461  *
462  * FUNCTION:    acpi_tb_release_owner_id
463  *
464  * PARAMETERS:  table_index         - Table index
465  *
466  * RETURN:      Status
467  *
468  * DESCRIPTION: Releases owner_id in table_desc
469  *
470  ******************************************************************************/
471
472 acpi_status acpi_tb_release_owner_id(u32 table_index)
473 {
474         acpi_status status = AE_BAD_PARAMETER;
475
476         ACPI_FUNCTION_TRACE(tb_release_owner_id);
477
478         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
479         if (table_index < acpi_gbl_root_table_list.count) {
480                 acpi_ut_release_owner_id(&
481                                          (acpi_gbl_root_table_list.
482                                           tables[table_index].owner_id));
483                 status = AE_OK;
484         }
485
486         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
487         return_ACPI_STATUS(status);
488 }
489
490 /*******************************************************************************
491  *
492  * FUNCTION:    acpi_tb_get_owner_id
493  *
494  * PARAMETERS:  table_index         - Table index
495  *              owner_id            - Where the table owner_id is returned
496  *
497  * RETURN:      Status
498  *
499  * DESCRIPTION: returns owner_id for the ACPI table
500  *
501  ******************************************************************************/
502
503 acpi_status acpi_tb_get_owner_id(u32 table_index, acpi_owner_id *owner_id)
504 {
505         acpi_status status = AE_BAD_PARAMETER;
506
507         ACPI_FUNCTION_TRACE(tb_get_owner_id);
508
509         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
510         if (table_index < acpi_gbl_root_table_list.count) {
511                 *owner_id =
512                     acpi_gbl_root_table_list.tables[table_index].owner_id;
513                 status = AE_OK;
514         }
515
516         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
517         return_ACPI_STATUS(status);
518 }
519
520 /*******************************************************************************
521  *
522  * FUNCTION:    acpi_tb_is_table_loaded
523  *
524  * PARAMETERS:  table_index         - Table index
525  *
526  * RETURN:      Table Loaded Flag
527  *
528  ******************************************************************************/
529
530 u8 acpi_tb_is_table_loaded(u32 table_index)
531 {
532         u8 is_loaded = FALSE;
533
534         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
535         if (table_index < acpi_gbl_root_table_list.count) {
536                 is_loaded = (u8)
537                     (acpi_gbl_root_table_list.tables[table_index].
538                      flags & ACPI_TABLE_IS_LOADED);
539         }
540
541         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
542         return (is_loaded);
543 }
544
545 /*******************************************************************************
546  *
547  * FUNCTION:    acpi_tb_set_table_loaded_flag
548  *
549  * PARAMETERS:  table_index         - Table index
550  *              is_loaded           - TRUE if table is loaded, FALSE otherwise
551  *
552  * RETURN:      None
553  *
554  * DESCRIPTION: Sets the table loaded flag to either TRUE or FALSE.
555  *
556  ******************************************************************************/
557
558 void acpi_tb_set_table_loaded_flag(u32 table_index, u8 is_loaded)
559 {
560
561         (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
562         if (table_index < acpi_gbl_root_table_list.count) {
563                 if (is_loaded) {
564                         acpi_gbl_root_table_list.tables[table_index].flags |=
565                             ACPI_TABLE_IS_LOADED;
566                 } else {
567                         acpi_gbl_root_table_list.tables[table_index].flags &=
568                             ~ACPI_TABLE_IS_LOADED;
569                 }
570         }
571
572         (void)acpi_ut_release_mutex(ACPI_MTX_TABLES);
573 }