This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / acpi / tables / tbconvrt.c
1 /******************************************************************************
2  *
3  * Module Name: tbconvrt - ACPI Table conversion utilities
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2004, 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    ("tbconvrt")
51
52
53 /*******************************************************************************
54  *
55  * FUNCTION:    acpi_tb_get_table_count
56  *
57  * PARAMETERS:  RSDP            - Pointer to the RSDP
58  *              RSDT            - Pointer to the RSDT/XSDT
59  *
60  * RETURN:      The number of tables pointed to by the RSDT or XSDT.
61  *
62  * DESCRIPTION: Calculate the number of tables.  Automatically handles either
63  *              an RSDT or XSDT.
64  *
65  ******************************************************************************/
66
67 u32
68 acpi_tb_get_table_count (
69         struct rsdp_descriptor          *RSDP,
70         struct acpi_table_header        *RSDT)
71 {
72         u32                             pointer_size;
73
74
75         ACPI_FUNCTION_ENTRY ();
76
77
78         if (RSDP->revision < 2) {
79                 pointer_size = sizeof (u32);
80         }
81         else {
82                 pointer_size = sizeof (u64);
83         }
84
85         /*
86          * Determine the number of tables pointed to by the RSDT/XSDT.
87          * This is defined by the ACPI Specification to be the number of
88          * pointers contained within the RSDT/XSDT.  The size of the pointers
89          * is architecture-dependent.
90          */
91         return ((RSDT->length - sizeof (struct acpi_table_header)) / pointer_size);
92 }
93
94
95 /*******************************************************************************
96  *
97  * FUNCTION:    acpi_tb_convert_to_xsdt
98  *
99  * PARAMETERS:  table_info      - Info about the RSDT
100  *
101  * RETURN:      Status
102  *
103  * DESCRIPTION: Convert an RSDT to an XSDT (internal common format)
104  *
105  ******************************************************************************/
106
107 acpi_status
108 acpi_tb_convert_to_xsdt (
109         struct acpi_table_desc          *table_info)
110 {
111         acpi_size                       table_size;
112         u32                             i;
113         XSDT_DESCRIPTOR         *new_table;
114
115
116         ACPI_FUNCTION_ENTRY ();
117
118
119         /* Compute size of the converted XSDT */
120
121         table_size = ((acpi_size) acpi_gbl_rsdt_table_count * sizeof (u64)) +
122                           sizeof (struct acpi_table_header);
123
124         /* Allocate an XSDT */
125
126         new_table = ACPI_MEM_CALLOCATE (table_size);
127         if (!new_table) {
128                 return (AE_NO_MEMORY);
129         }
130
131         /* Copy the header and set the length */
132
133         ACPI_MEMCPY (new_table, table_info->pointer, sizeof (struct acpi_table_header));
134         new_table->length = (u32) table_size;
135
136         /* Copy the table pointers */
137
138         for (i = 0; i < acpi_gbl_rsdt_table_count; i++) {
139                 if (acpi_gbl_RSDP->revision < 2) {
140                         ACPI_STORE_ADDRESS (new_table->table_offset_entry[i],
141                                 (ACPI_CAST_PTR (struct rsdt_descriptor_rev1, table_info->pointer))->table_offset_entry[i]);
142                 }
143                 else {
144                         new_table->table_offset_entry[i] =
145                                 (ACPI_CAST_PTR (XSDT_DESCRIPTOR, table_info->pointer))->table_offset_entry[i];
146                 }
147         }
148
149         /* Delete the original table (either mapped or in a buffer) */
150
151         acpi_tb_delete_single_table (table_info);
152
153         /* Point the table descriptor to the new table */
154
155         table_info->pointer     = ACPI_CAST_PTR (struct acpi_table_header, new_table);
156         table_info->length      = table_size;
157         table_info->allocation  = ACPI_MEM_ALLOCATED;
158
159         return (AE_OK);
160 }
161
162
163 /******************************************************************************
164  *
165  * FUNCTION:    acpi_tb_init_generic_address
166  *
167  * PARAMETERS:  new_gas_struct      - GAS struct to be initialized
168  *              register_bit_width  - Width of this register
169  *              Address             - Address of the register
170  *
171  * RETURN:      None
172  *
173  * DESCRIPTION: Initialize a GAS structure.
174  *
175  ******************************************************************************/
176
177 static void
178 acpi_tb_init_generic_address (
179         struct acpi_generic_address     *new_gas_struct,
180         u8                              register_bit_width,
181         acpi_physical_address           address)
182 {
183
184         ACPI_STORE_ADDRESS (new_gas_struct->address, address);
185
186         new_gas_struct->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
187         new_gas_struct->register_bit_width = register_bit_width;
188         new_gas_struct->register_bit_offset = 0;
189         new_gas_struct->reserved        = 0;
190 }
191
192
193 /*******************************************************************************
194  *
195  * FUNCTION:    acpi_tb_convert_fadt1
196  *
197  * PARAMETERS:  local_fadt      - Pointer to new FADT
198  *              original_fadt   - Pointer to old FADT
199  *
200  * RETURN:      Populates local_fadt
201  *
202  * DESCRIPTION: Convert an ACPI 1.0 FADT to common internal format
203  *
204  ******************************************************************************/
205
206 static void
207 acpi_tb_convert_fadt1 (
208         struct fadt_descriptor_rev2    *local_fadt,
209         struct fadt_descriptor_rev1    *original_fadt)
210 {
211
212
213         /* ACPI 1.0 FACS */
214         /* The BIOS stored FADT should agree with Revision 1.0 */
215
216         /*
217          * Copy the table header and the common part of the tables.
218          *
219          * The 2.0 table is an extension of the 1.0 table, so the entire 1.0
220          * table can be copied first, then expand some fields to 64 bits.
221          */
222         ACPI_MEMCPY (local_fadt, original_fadt, sizeof (struct fadt_descriptor_rev1));
223
224         /* Convert table pointers to 64-bit fields */
225
226         ACPI_STORE_ADDRESS (local_fadt->xfirmware_ctrl, local_fadt->V1_firmware_ctrl);
227         ACPI_STORE_ADDRESS (local_fadt->Xdsdt, local_fadt->V1_dsdt);
228
229         /*
230          * System Interrupt Model isn't used in ACPI 2.0 (local_fadt->Reserved1 = 0;)
231          */
232
233         /*
234          * This field is set by the OEM to convey the preferred power management
235          * profile to OSPM. It doesn't have any 1.0 equivalence.  Since we don't
236          * know what kind of 32-bit system this is, we will use "unspecified".
237          */
238         local_fadt->prefer_PM_profile = PM_UNSPECIFIED;
239
240         /*
241          * Processor Performance State Control. This is the value OSPM writes to
242          * the SMI_CMD register to assume processor performance state control
243          * responsibility. There isn't any equivalence in 1.0, leave it zeroed.
244          */
245         local_fadt->pstate_cnt = 0;
246
247         /*
248          * Support for the _CST object and C States change notification.
249          * This data item hasn't any 1.0 equivalence so leave it zero.
250          */
251         local_fadt->cst_cnt = 0;
252
253         /*
254          * Support for ACPI system reset mechanism was introduced between
255          * Spec revisions 1.0b and 2.0, for legacy free systems..
256          */
257         if (original_fadt->revision == FADT2_INTERIM_REVISION_ID && original_fadt->length == FADT2_INTERIM_LENGTH) {
258                 /*
259                  * Copy the entire GAS, plus the 1-byte reset value that
260                  * immediately follows.
261                  */
262                 ACPI_MEMCPY (&local_fadt->reset_register, &((FADT_DESCRIPTOR *)original_fadt)->reset_register, sizeof(struct acpi_generic_address) + 1);
263         } else {
264                 /*
265                  * Otherwise, there isn't any equivalence in 1.0 and it's
266                  * highly likely that a 1.0 system has legacy support.
267                  */
268                 local_fadt->iapc_boot_arch = BAF_LEGACY_DEVICES;
269         }
270
271         /*
272          * Convert the V1.0 block addresses to V2.0 GAS structures
273          */
274         acpi_tb_init_generic_address (&local_fadt->xpm1a_evt_blk, local_fadt->pm1_evt_len,
275                           (acpi_physical_address)   local_fadt->V1_pm1a_evt_blk);
276         acpi_tb_init_generic_address (&local_fadt->xpm1b_evt_blk, local_fadt->pm1_evt_len,
277                           (acpi_physical_address)   local_fadt->V1_pm1b_evt_blk);
278         acpi_tb_init_generic_address (&local_fadt->xpm1a_cnt_blk, local_fadt->pm1_cnt_len,
279                           (acpi_physical_address)   local_fadt->V1_pm1a_cnt_blk);
280         acpi_tb_init_generic_address (&local_fadt->xpm1b_cnt_blk, local_fadt->pm1_cnt_len,
281                           (acpi_physical_address)   local_fadt->V1_pm1b_cnt_blk);
282         acpi_tb_init_generic_address (&local_fadt->xpm2_cnt_blk, local_fadt->pm2_cnt_len,
283                           (acpi_physical_address)   local_fadt->V1_pm2_cnt_blk);
284         acpi_tb_init_generic_address (&local_fadt->xpm_tmr_blk, local_fadt->pm_tm_len,
285                           (acpi_physical_address)   local_fadt->V1_pm_tmr_blk);
286         acpi_tb_init_generic_address (&local_fadt->xgpe0_blk, 0,
287                           (acpi_physical_address)   local_fadt->V1_gpe0_blk);
288         acpi_tb_init_generic_address (&local_fadt->xgpe1_blk, 0,
289                           (acpi_physical_address)   local_fadt->V1_gpe1_blk);
290
291         /* Create separate GAS structs for the PM1 Enable registers */
292
293         acpi_tb_init_generic_address (&acpi_gbl_xpm1a_enable,
294                  (u8) ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len),
295                  (acpi_physical_address) (local_fadt->xpm1a_evt_blk.address +
296                         ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len)));
297
298         /* PM1B is optional; leave null if not present */
299
300         if (local_fadt->xpm1b_evt_blk.address) {
301                 acpi_tb_init_generic_address (&acpi_gbl_xpm1b_enable,
302                          (u8) ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len),
303                          (acpi_physical_address) (local_fadt->xpm1b_evt_blk.address +
304                                 ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len)));
305         }
306 }
307
308
309 /*******************************************************************************
310  *
311  * FUNCTION:    acpi_tb_convert_fadt2
312  *
313  * PARAMETERS:  local_fadt      - Pointer to new FADT
314  *              original_fadt   - Pointer to old FADT
315  *
316  * RETURN:      Populates local_fadt
317  *
318  * DESCRIPTION: Convert an ACPI 2.0 FADT to common internal format.
319  *              Handles optional "X" fields.
320  *
321  ******************************************************************************/
322
323 static void
324 acpi_tb_convert_fadt2 (
325         struct fadt_descriptor_rev2    *local_fadt,
326         struct fadt_descriptor_rev2    *original_fadt)
327 {
328
329         /* We have an ACPI 2.0 FADT but we must copy it to our local buffer */
330
331         ACPI_MEMCPY (local_fadt, original_fadt, sizeof (struct fadt_descriptor_rev2));
332
333         /*
334          * "X" fields are optional extensions to the original V1.0 fields, so
335          * we must selectively expand V1.0 fields if the corresponding X field
336          * is zero.
337          */
338         if (!(local_fadt->xfirmware_ctrl)) {
339                 ACPI_STORE_ADDRESS (local_fadt->xfirmware_ctrl, local_fadt->V1_firmware_ctrl);
340         }
341
342         if (!(local_fadt->Xdsdt)) {
343                 ACPI_STORE_ADDRESS (local_fadt->Xdsdt, local_fadt->V1_dsdt);
344         }
345
346         if (!(local_fadt->xpm1a_evt_blk.address)) {
347                 acpi_tb_init_generic_address (&local_fadt->xpm1a_evt_blk,
348                         local_fadt->pm1_evt_len, (acpi_physical_address) local_fadt->V1_pm1a_evt_blk);
349         }
350
351         if (!(local_fadt->xpm1b_evt_blk.address)) {
352                 acpi_tb_init_generic_address (&local_fadt->xpm1b_evt_blk,
353                         local_fadt->pm1_evt_len, (acpi_physical_address) local_fadt->V1_pm1b_evt_blk);
354         }
355
356         if (!(local_fadt->xpm1a_cnt_blk.address)) {
357                 acpi_tb_init_generic_address (&local_fadt->xpm1a_cnt_blk,
358                         local_fadt->pm1_cnt_len, (acpi_physical_address) local_fadt->V1_pm1a_cnt_blk);
359         }
360
361         if (!(local_fadt->xpm1b_cnt_blk.address)) {
362                 acpi_tb_init_generic_address (&local_fadt->xpm1b_cnt_blk,
363                         local_fadt->pm1_cnt_len, (acpi_physical_address) local_fadt->V1_pm1b_cnt_blk);
364         }
365
366         if (!(local_fadt->xpm2_cnt_blk.address)) {
367                 acpi_tb_init_generic_address (&local_fadt->xpm2_cnt_blk,
368                         local_fadt->pm2_cnt_len, (acpi_physical_address) local_fadt->V1_pm2_cnt_blk);
369         }
370
371         if (!(local_fadt->xpm_tmr_blk.address)) {
372                 acpi_tb_init_generic_address (&local_fadt->xpm_tmr_blk,
373                         local_fadt->pm_tm_len, (acpi_physical_address) local_fadt->V1_pm_tmr_blk);
374         }
375
376         if (!(local_fadt->xgpe0_blk.address)) {
377                 acpi_tb_init_generic_address (&local_fadt->xgpe0_blk,
378                         0, (acpi_physical_address) local_fadt->V1_gpe0_blk);
379         }
380
381         if (!(local_fadt->xgpe1_blk.address)) {
382                 acpi_tb_init_generic_address (&local_fadt->xgpe1_blk,
383                         0, (acpi_physical_address) local_fadt->V1_gpe1_blk);
384         }
385
386         /* Create separate GAS structs for the PM1 Enable registers */
387
388         acpi_tb_init_generic_address (&acpi_gbl_xpm1a_enable,
389                 (u8) ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len),
390                 (acpi_physical_address) (local_fadt->xpm1a_evt_blk.address +
391                         ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len)));
392         acpi_gbl_xpm1a_enable.address_space_id = local_fadt->xpm1a_evt_blk.address_space_id;
393
394         /* PM1B is optional; leave null if not present */
395
396         if (local_fadt->xpm1b_evt_blk.address) {
397                 acpi_tb_init_generic_address (&acpi_gbl_xpm1b_enable,
398                         (u8) ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len),
399                         (acpi_physical_address) (local_fadt->xpm1b_evt_blk.address +
400                                 ACPI_DIV_2 (acpi_gbl_FADT->pm1_evt_len)));
401                 acpi_gbl_xpm1b_enable.address_space_id = local_fadt->xpm1b_evt_blk.address_space_id;
402         }
403 }
404
405
406 /*******************************************************************************
407  *
408  * FUNCTION:    acpi_tb_convert_table_fadt
409  *
410  * PARAMETERS:  None
411  *
412  * RETURN:      Status
413  *
414  * DESCRIPTION: Converts a BIOS supplied ACPI 1.0 FADT to a local
415  *              ACPI 2.0 FADT. If the BIOS supplied a 2.0 FADT then it is simply
416  *              copied to the local FADT.  The ACPI CA software uses this
417  *              local FADT. Thus a significant amount of special #ifdef
418  *              type codeing is saved.
419  *
420  ******************************************************************************/
421
422 acpi_status
423 acpi_tb_convert_table_fadt (void)
424 {
425         struct fadt_descriptor_rev2    *local_fadt;
426         struct acpi_table_desc         *table_desc;
427
428
429         ACPI_FUNCTION_TRACE ("tb_convert_table_fadt");
430
431
432         /*
433          * acpi_gbl_FADT is valid
434          * Allocate and zero the 2.0 FADT buffer
435          */
436         local_fadt = ACPI_MEM_CALLOCATE (sizeof (struct fadt_descriptor_rev2));
437         if (local_fadt == NULL) {
438                 return_ACPI_STATUS (AE_NO_MEMORY);
439         }
440
441         /*
442          * FADT length and version validation.  The table must be at least as
443          * long as the version 1.0 FADT
444          */
445         if (acpi_gbl_FADT->length < sizeof (struct fadt_descriptor_rev1)) {
446                 ACPI_REPORT_ERROR (("Invalid FADT table length: 0x%X\n", acpi_gbl_FADT->length));
447                 return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
448         }
449
450         if (acpi_gbl_FADT->revision >= FADT2_REVISION_ID) {
451                 if (acpi_gbl_FADT->length < sizeof (struct fadt_descriptor_rev2)) {
452                         /* Length is too short to be a V2.0 table */
453
454                         ACPI_REPORT_WARNING (("Inconsistent FADT length (0x%X) and revision (0x%X), using FADT V1.0 portion of table\n",
455                                          acpi_gbl_FADT->length, acpi_gbl_FADT->revision));
456
457                         acpi_tb_convert_fadt1 (local_fadt, (void *) acpi_gbl_FADT);
458                 }
459                 else {
460                         /* Valid V2.0 table */
461
462                         acpi_tb_convert_fadt2 (local_fadt, acpi_gbl_FADT);
463                 }
464         }
465         else {
466                 /* Valid V1.0 table */
467
468                 acpi_tb_convert_fadt1 (local_fadt, (void *) acpi_gbl_FADT);
469         }
470
471         /*
472          * Global FADT pointer will point to the new common V2.0 FADT
473          */
474         acpi_gbl_FADT = local_fadt;
475         acpi_gbl_FADT->length = sizeof (FADT_DESCRIPTOR);
476
477         /* Free the original table */
478
479         table_desc = acpi_gbl_table_lists[ACPI_TABLE_FADT].next;
480         acpi_tb_delete_single_table (table_desc);
481
482         /* Install the new table */
483
484         table_desc->pointer     = ACPI_CAST_PTR (struct acpi_table_header, acpi_gbl_FADT);
485         table_desc->allocation  = ACPI_MEM_ALLOCATED;
486         table_desc->length      = sizeof (struct fadt_descriptor_rev2);
487
488         /* Dump the entire FADT */
489
490         ACPI_DEBUG_PRINT ((ACPI_DB_TABLES,
491                 "Hex dump of common internal FADT, size %d (%X)\n",
492                 acpi_gbl_FADT->length, acpi_gbl_FADT->length));
493         ACPI_DUMP_BUFFER ((u8 *) (acpi_gbl_FADT), acpi_gbl_FADT->length);
494
495         return_ACPI_STATUS (AE_OK);
496 }
497
498
499 /*******************************************************************************
500  *
501  * FUNCTION:    acpi_tb_convert_table_facs
502  *
503  * PARAMETERS:  table_info      - Info for currently installad FACS
504  *
505  * RETURN:      Status
506  *
507  * DESCRIPTION: Convert ACPI 1.0 and ACPI 2.0 FACS to a common internal
508  *              table format.
509  *
510  ******************************************************************************/
511
512 acpi_status
513 acpi_tb_build_common_facs (
514         struct acpi_table_desc          *table_info)
515 {
516
517         ACPI_FUNCTION_TRACE ("tb_build_common_facs");
518
519
520         /* Absolute minimum length is 24, but the ACPI spec says 64 */
521
522         if (acpi_gbl_FACS->length < 24) {
523                 ACPI_REPORT_ERROR (("Invalid FACS table length: 0x%X\n", acpi_gbl_FACS->length));
524                 return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
525         }
526
527         if (acpi_gbl_FACS->length < 64) {
528                 ACPI_REPORT_WARNING (("FACS is shorter than the ACPI specification allows: 0x%X, using anyway\n",
529                         acpi_gbl_FACS->length));
530         }
531
532         /* Copy fields to the new FACS */
533
534         acpi_gbl_common_fACS.global_lock = &(acpi_gbl_FACS->global_lock);
535
536         if ((acpi_gbl_RSDP->revision < 2) ||
537                 (acpi_gbl_FACS->length < 32) ||
538                 (!(acpi_gbl_FACS->xfirmware_waking_vector))) {
539                 /* ACPI 1.0 FACS or short table or optional X_ field is zero */
540
541                 acpi_gbl_common_fACS.firmware_waking_vector = ACPI_CAST_PTR (u64, &(acpi_gbl_FACS->firmware_waking_vector));
542                 acpi_gbl_common_fACS.vector_width = 32;
543         }
544         else {
545                 /* ACPI 2.0 FACS with valid X_ field */
546
547                 acpi_gbl_common_fACS.firmware_waking_vector = &acpi_gbl_FACS->xfirmware_waking_vector;
548                 acpi_gbl_common_fACS.vector_width = 64;
549         }
550
551         return_ACPI_STATUS (AE_OK);
552 }
553
554