]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/acpi/tables/tbinstal.c
ACPICA 20050708 from Bob Moore <robert.moore@intel.com>
[linux-2.6.git] / drivers / acpi / tables / tbinstal.c
1 /******************************************************************************
2  *
3  * Module Name: tbinstal - ACPI table installation and removal
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, 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
45 #include <acpi/acpi.h>
46 #include <acpi/actables.h>
47
48
49 #define _COMPONENT          ACPI_TABLES
50          ACPI_MODULE_NAME    ("tbinstal")
51
52 /* Local prototypes */
53
54 static acpi_status
55 acpi_tb_match_signature (
56         char                            *signature,
57         struct acpi_table_desc          *table_info,
58         u8                              search_type);
59
60
61 /*******************************************************************************
62  *
63  * FUNCTION:    acpi_tb_match_signature
64  *
65  * PARAMETERS:  Signature           - Table signature to match
66  *              table_info          - Return data
67  *              search_type         - Table type to match (primary/secondary)
68  *
69  * RETURN:      Status
70  *
71  * DESCRIPTION: Compare signature against the list of "ACPI-subsystem-owned"
72  *              tables (DSDT/FADT/SSDT, etc.) Returns the table_type_iD on match.
73  *
74  ******************************************************************************/
75
76 static acpi_status
77 acpi_tb_match_signature (
78         char                            *signature,
79         struct acpi_table_desc          *table_info,
80         u8                              search_type)
81 {
82         acpi_native_uint                i;
83
84
85         ACPI_FUNCTION_TRACE ("tb_match_signature");
86
87
88         /* Search for a signature match among the known table types */
89
90         for (i = 0; i < NUM_ACPI_TABLE_TYPES; i++) {
91                 if (!(acpi_gbl_table_data[i].flags & search_type)) {
92                         continue;
93                 }
94
95                 if (!ACPI_STRNCMP (signature, acpi_gbl_table_data[i].signature,
96                                    acpi_gbl_table_data[i].sig_length)) {
97                         /* Found a signature match, return index if requested */
98
99                         if (table_info) {
100                                 table_info->type = (u8) i;
101                         }
102
103                         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
104                                 "Table [%4.4s] is an ACPI table consumed by the core subsystem\n",
105                                 (char *) acpi_gbl_table_data[i].signature));
106
107                         return_ACPI_STATUS (AE_OK);
108                 }
109         }
110
111         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
112                 "Table [%4.4s] is not an ACPI table consumed by the core subsystem - ignored\n",
113                 (char *) signature));
114
115         return_ACPI_STATUS (AE_TABLE_NOT_SUPPORTED);
116 }
117
118
119 /*******************************************************************************
120  *
121  * FUNCTION:    acpi_tb_install_table
122  *
123  * PARAMETERS:  table_info          - Return value from acpi_tb_get_table_body
124  *
125  * RETURN:      Status
126  *
127  * DESCRIPTION: Load and validate all tables other than the RSDT.  The RSDT must
128  *              already be loaded and validated.
129  *              Install the table into the global data structs.
130  *
131  ******************************************************************************/
132
133 acpi_status
134 acpi_tb_install_table (
135         struct acpi_table_desc          *table_info)
136 {
137         acpi_status                     status;
138
139         ACPI_FUNCTION_TRACE ("tb_install_table");
140
141
142         /* Lock tables while installing */
143
144         status = acpi_ut_acquire_mutex (ACPI_MTX_TABLES);
145         if (ACPI_FAILURE (status)) {
146                 ACPI_REPORT_ERROR (("Could not acquire table mutex for [%4.4s], %s\n",
147                         table_info->pointer->signature, acpi_format_exception (status)));
148                 return_ACPI_STATUS (status);
149         }
150
151         /* Install the table into the global data structure */
152
153         status = acpi_tb_init_table_descriptor (table_info->type, table_info);
154         if (ACPI_FAILURE (status)) {
155                 ACPI_REPORT_ERROR (("Could not install ACPI table [%4.4s], %s\n",
156                         table_info->pointer->signature, acpi_format_exception (status)));
157         }
158
159         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "%s located at %p\n",
160                 acpi_gbl_table_data[table_info->type].name, table_info->pointer));
161
162         (void) acpi_ut_release_mutex (ACPI_MTX_TABLES);
163         return_ACPI_STATUS (status);
164 }
165
166
167 /*******************************************************************************
168  *
169  * FUNCTION:    acpi_tb_recognize_table
170  *
171  * PARAMETERS:  table_info          - Return value from acpi_tb_get_table_body
172  *              search_type         - Table type to match (primary/secondary)
173  *
174  * RETURN:      Status
175  *
176  * DESCRIPTION: Check a table signature for a match against known table types
177  *
178  * NOTE:  All table pointers are validated as follows:
179  *          1) Table pointer must point to valid physical memory
180  *          2) Signature must be 4 ASCII chars, even if we don't recognize the
181  *             name
182  *          3) Table must be readable for length specified in the header
183  *          4) Table checksum must be valid (with the exception of the FACS
184  *             which has no checksum for some odd reason)
185  *
186  ******************************************************************************/
187
188 acpi_status
189 acpi_tb_recognize_table (
190         struct acpi_table_desc          *table_info,
191         u8                              search_type)
192 {
193         struct acpi_table_header        *table_header;
194         acpi_status                     status;
195
196
197         ACPI_FUNCTION_TRACE ("tb_recognize_table");
198
199
200         /* Ensure that we have a valid table pointer */
201
202         table_header = (struct acpi_table_header *) table_info->pointer;
203         if (!table_header) {
204                 return_ACPI_STATUS (AE_BAD_PARAMETER);
205         }
206
207         /*
208          * We only "recognize" a limited number of ACPI tables -- namely, the
209          * ones that are used by the subsystem (DSDT, FADT, etc.)
210          *
211          * An AE_TABLE_NOT_SUPPORTED means that the table was not recognized.
212          * This can be any one of many valid ACPI tables, it just isn't one of
213          * the tables that is consumed by the core subsystem
214          */
215         status = acpi_tb_match_signature (table_header->signature,
216                          table_info, search_type);
217         if (ACPI_FAILURE (status)) {
218                 return_ACPI_STATUS (status);
219         }
220
221         status = acpi_tb_validate_table_header (table_header);
222         if (ACPI_FAILURE (status)) {
223                 return_ACPI_STATUS (status);
224         }
225
226         /* Return the table type and length via the info struct */
227
228         table_info->length = (acpi_size) table_header->length;
229
230         return_ACPI_STATUS (status);
231 }
232
233
234 /*******************************************************************************
235  *
236  * FUNCTION:    acpi_tb_init_table_descriptor
237  *
238  * PARAMETERS:  table_type          - The type of the table
239  *              table_info          - A table info struct
240  *
241  * RETURN:      None.
242  *
243  * DESCRIPTION: Install a table into the global data structs.
244  *
245  ******************************************************************************/
246
247 acpi_status
248 acpi_tb_init_table_descriptor (
249         acpi_table_type                 table_type,
250         struct acpi_table_desc          *table_info)
251 {
252         struct acpi_table_list          *list_head;
253         struct acpi_table_desc          *table_desc;
254         acpi_status                     status;
255
256
257         ACPI_FUNCTION_TRACE_U32 ("tb_init_table_descriptor", table_type);
258
259
260         /* Allocate a descriptor for this table */
261
262         table_desc = ACPI_MEM_CALLOCATE (sizeof (struct acpi_table_desc));
263         if (!table_desc) {
264                 return_ACPI_STATUS (AE_NO_MEMORY);
265         }
266
267         /* Get a new owner ID for the table */
268
269         status = acpi_ut_allocate_owner_id (&table_desc->owner_id);
270         if (ACPI_FAILURE (status)) {
271                 return_ACPI_STATUS (status);
272         }
273
274         /* Install the table into the global data structure */
275
276         list_head = &acpi_gbl_table_lists[table_type];
277
278         /*
279          * Two major types of tables:  1) Only one instance is allowed.  This
280          * includes most ACPI tables such as the DSDT.  2) Multiple instances of
281          * the table are allowed.  This includes SSDT and PSDTs.
282          */
283         if (ACPI_IS_SINGLE_TABLE (acpi_gbl_table_data[table_type].flags)) {
284                 /*
285                  * Only one table allowed, and a table has alread been installed
286                  * at this location, so return an error.
287                  */
288                 if (list_head->next) {
289                         ACPI_MEM_FREE (table_desc);
290                         return_ACPI_STATUS (AE_ALREADY_EXISTS);
291                 }
292
293                 table_desc->next = list_head->next;
294                 list_head->next = table_desc;
295
296                 if (table_desc->next) {
297                         table_desc->next->prev = table_desc;
298                 }
299
300                 list_head->count++;
301         }
302         else {
303                 /*
304                  * Link the new table in to the list of tables of this type.
305                  * Insert at the end of the list, order IS IMPORTANT.
306                  *
307                  * table_desc->Prev & Next are already NULL from calloc()
308                  */
309                 list_head->count++;
310
311                 if (!list_head->next) {
312                         list_head->next = table_desc;
313                 }
314                 else {
315                         table_desc->next = list_head->next;
316
317                         while (table_desc->next->next) {
318                                 table_desc->next = table_desc->next->next;
319                         }
320
321                         table_desc->next->next = table_desc;
322                         table_desc->prev = table_desc->next;
323                         table_desc->next = NULL;
324                 }
325         }
326
327         /* Finish initialization of the table descriptor */
328
329         table_desc->type                = (u8) table_type;
330         table_desc->pointer             = table_info->pointer;
331         table_desc->length              = table_info->length;
332         table_desc->allocation          = table_info->allocation;
333         table_desc->aml_start           = (u8 *) (table_desc->pointer + 1),
334         table_desc->aml_length          = (u32) (table_desc->length -
335                          (u32) sizeof (struct acpi_table_header));
336         table_desc->loaded_into_namespace = FALSE;
337
338         /*
339          * Set the appropriate global pointer (if there is one) to point to the
340          * newly installed table
341          */
342         if (acpi_gbl_table_data[table_type].global_ptr) {
343                 *(acpi_gbl_table_data[table_type].global_ptr) = table_info->pointer;
344         }
345
346         /* Return Data */
347
348         table_info->owner_id        = table_desc->owner_id;
349         table_info->installed_desc  = table_desc;
350
351         return_ACPI_STATUS (AE_OK);
352 }
353
354
355 /*******************************************************************************
356  *
357  * FUNCTION:    acpi_tb_delete_all_tables
358  *
359  * PARAMETERS:  None.
360  *
361  * RETURN:      None.
362  *
363  * DESCRIPTION: Delete all internal ACPI tables
364  *
365  ******************************************************************************/
366
367 void
368 acpi_tb_delete_all_tables (
369         void)
370 {
371         acpi_table_type                 type;
372
373
374         /*
375          * Free memory allocated for ACPI tables
376          * Memory can either be mapped or allocated
377          */
378         for (type = 0; type < NUM_ACPI_TABLE_TYPES; type++) {
379                 acpi_tb_delete_tables_by_type (type);
380         }
381 }
382
383
384 /*******************************************************************************
385  *
386  * FUNCTION:    acpi_tb_delete_tables_by_type
387  *
388  * PARAMETERS:  Type                - The table type to be deleted
389  *
390  * RETURN:      None.
391  *
392  * DESCRIPTION: Delete an internal ACPI table
393  *              Locks the ACPI table mutex
394  *
395  ******************************************************************************/
396
397 void
398 acpi_tb_delete_tables_by_type (
399         acpi_table_type                 type)
400 {
401         struct acpi_table_desc          *table_desc;
402         u32                             count;
403         u32                             i;
404
405
406         ACPI_FUNCTION_TRACE_U32 ("tb_delete_tables_by_type", type);
407
408
409         if (type > ACPI_TABLE_MAX) {
410                 return_VOID;
411         }
412
413         if (ACPI_FAILURE (acpi_ut_acquire_mutex (ACPI_MTX_TABLES))) {
414                 return;
415         }
416
417         /* Clear the appropriate "typed" global table pointer */
418
419         switch (type) {
420         case ACPI_TABLE_RSDP:
421                 acpi_gbl_RSDP = NULL;
422                 break;
423
424         case ACPI_TABLE_DSDT:
425                 acpi_gbl_DSDT = NULL;
426                 break;
427
428         case ACPI_TABLE_FADT:
429                 acpi_gbl_FADT = NULL;
430                 break;
431
432         case ACPI_TABLE_FACS:
433                 acpi_gbl_FACS = NULL;
434                 break;
435
436         case ACPI_TABLE_XSDT:
437                 acpi_gbl_XSDT = NULL;
438                 break;
439
440         case ACPI_TABLE_SSDT:
441         case ACPI_TABLE_PSDT:
442         default:
443                 break;
444         }
445
446         /*
447          * Free the table
448          * 1) Get the head of the list
449          */
450         table_desc = acpi_gbl_table_lists[type].next;
451         count     = acpi_gbl_table_lists[type].count;
452
453         /*
454          * 2) Walk the entire list, deleting both the allocated tables
455          *    and the table descriptors
456          */
457         for (i = 0; i < count; i++) {
458                 table_desc = acpi_tb_uninstall_table (table_desc);
459         }
460
461         (void) acpi_ut_release_mutex (ACPI_MTX_TABLES);
462         return_VOID;
463 }
464
465
466 /*******************************************************************************
467  *
468  * FUNCTION:    acpi_tb_delete_single_table
469  *
470  * PARAMETERS:  table_info          - A table info struct
471  *
472  * RETURN:      None.
473  *
474  * DESCRIPTION: Low-level free for a single ACPI table.  Handles cases where
475  *              the table was allocated a buffer or was mapped.
476  *
477  ******************************************************************************/
478
479 void
480 acpi_tb_delete_single_table (
481         struct acpi_table_desc          *table_desc)
482 {
483
484         /* Must have a valid table descriptor and pointer */
485
486         if ((!table_desc) ||
487                  (!table_desc->pointer)) {
488                 return;
489         }
490
491         /* Valid table, determine type of memory allocation */
492
493         switch (table_desc->allocation) {
494         case ACPI_MEM_NOT_ALLOCATED:
495                 break;
496
497         case ACPI_MEM_ALLOCATED:
498
499                 ACPI_MEM_FREE (table_desc->pointer);
500                 break;
501
502         case ACPI_MEM_MAPPED:
503
504                 acpi_os_unmap_memory (table_desc->pointer, table_desc->length);
505                 break;
506
507         default:
508                 break;
509         }
510 }
511
512
513 /*******************************************************************************
514  *
515  * FUNCTION:    acpi_tb_uninstall_table
516  *
517  * PARAMETERS:  table_info          - A table info struct
518  *
519  * RETURN:      Pointer to the next table in the list (of same type)
520  *
521  * DESCRIPTION: Free the memory associated with an internal ACPI table that
522  *              is either installed or has never been installed.
523  *              Table mutex should be locked.
524  *
525  ******************************************************************************/
526
527 struct acpi_table_desc *
528 acpi_tb_uninstall_table (
529         struct acpi_table_desc          *table_desc)
530 {
531         struct acpi_table_desc          *next_desc;
532
533
534         ACPI_FUNCTION_TRACE_PTR ("tb_uninstall_table", table_desc);
535
536
537         if (!table_desc) {
538                 return_PTR (NULL);
539         }
540
541         /* Unlink the descriptor from the doubly linked list */
542
543         if (table_desc->prev) {
544                 table_desc->prev->next = table_desc->next;
545         }
546         else {
547                 /* Is first on list, update list head */
548
549                 acpi_gbl_table_lists[table_desc->type].next = table_desc->next;
550         }
551
552         if (table_desc->next) {
553                 table_desc->next->prev = table_desc->prev;
554         }
555
556         /* Free the memory allocated for the table itself */
557
558         acpi_tb_delete_single_table (table_desc);
559
560         /* Free the table descriptor */
561
562         next_desc = table_desc->next;
563         ACPI_MEM_FREE (table_desc);
564
565         /* Return pointer to the next descriptor */
566
567         return_PTR (next_desc);
568 }
569
570