ACPICA: FADT verification is now table driven.
[sfrench/cifs-2.6.git] / drivers / acpi / tables / tbfadt.c
1 /******************************************************************************
2  *
3  * Module Name: tbfadt   - FADT table utilities
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2006, 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/actables.h>
46
47 #define _COMPONENT          ACPI_TABLES
48 ACPI_MODULE_NAME("tbfadt")
49
50 /* Local prototypes */
51 static void inline
52 acpi_tb_init_generic_address(struct acpi_generic_address *generic_address,
53                              u8 bit_width, u64 address);
54
55 /* Table for conversion of FADT to common internal format and FADT validation */
56
57 typedef struct acpi_fadt_info {
58         char *name;
59         u8 target;
60         u8 source;
61         u8 length;
62         u8 type;
63
64 } acpi_fadt_info;
65
66 #define ACPI_FADT_REQUIRED          1
67 #define ACPI_FADT_SEPARATE_LENGTH   2
68
69 static struct acpi_fadt_info fadt_info_table[] = {
70         {"Pm1aEventBlock", ACPI_FADT_OFFSET(xpm1a_event_block),
71          ACPI_FADT_OFFSET(pm1a_event_block),
72          ACPI_FADT_OFFSET(pm1_event_length), ACPI_FADT_REQUIRED},
73
74         {"Pm1bEventBlock", ACPI_FADT_OFFSET(xpm1b_event_block),
75          ACPI_FADT_OFFSET(pm1b_event_block),
76          ACPI_FADT_OFFSET(pm1_event_length), 0},
77
78         {"Pm1aControlBlock", ACPI_FADT_OFFSET(xpm1a_control_block),
79          ACPI_FADT_OFFSET(pm1a_control_block),
80          ACPI_FADT_OFFSET(pm1_control_length), ACPI_FADT_REQUIRED},
81
82         {"Pm1bControlBlock", ACPI_FADT_OFFSET(xpm1b_control_block),
83          ACPI_FADT_OFFSET(pm1b_control_block),
84          ACPI_FADT_OFFSET(pm1_control_length), 0},
85
86         {"Pm2ControlBlock", ACPI_FADT_OFFSET(xpm2_control_block),
87          ACPI_FADT_OFFSET(pm2_control_block),
88          ACPI_FADT_OFFSET(pm2_control_length), ACPI_FADT_SEPARATE_LENGTH},
89
90         {"PmTimerBlock", ACPI_FADT_OFFSET(xpm_timer_block),
91          ACPI_FADT_OFFSET(pm_timer_block),
92          ACPI_FADT_OFFSET(pm_timer_length), ACPI_FADT_REQUIRED},
93
94         {"Gpe0Block", ACPI_FADT_OFFSET(xgpe0_block),
95          ACPI_FADT_OFFSET(gpe0_block),
96          ACPI_FADT_OFFSET(gpe0_block_length), ACPI_FADT_SEPARATE_LENGTH},
97
98         {"Gpe1Block", ACPI_FADT_OFFSET(xgpe1_block),
99          ACPI_FADT_OFFSET(gpe1_block),
100          ACPI_FADT_OFFSET(gpe1_block_length), ACPI_FADT_SEPARATE_LENGTH}
101 };
102
103 #define ACPI_FADT_INFO_ENTRIES        (sizeof (fadt_info_table) / sizeof (struct acpi_fadt_info))
104
105 /*******************************************************************************
106  *
107  * FUNCTION:    acpi_tb_init_generic_address
108  *
109  * PARAMETERS:  generic_address     - GAS struct to be initialized
110  *              bit_width           - Width of this register
111  *              Address             - Address of the register
112  *
113  * RETURN:      None
114  *
115  * DESCRIPTION: Initialize a Generic Address Structure (GAS)
116  *              See the ACPI specification for a full description and
117  *              definition of this structure.
118  *
119  ******************************************************************************/
120
121 static void inline
122 acpi_tb_init_generic_address(struct acpi_generic_address *generic_address,
123                              u8 bit_width, u64 address)
124 {
125
126         /*
127          * The 64-bit Address field is non-aligned in the byte packed
128          * GAS struct.
129          */
130         ACPI_MOVE_64_TO_64(&generic_address->address, &address);
131
132         /* All other fields are byte-wide */
133
134         generic_address->space_id = ACPI_ADR_SPACE_SYSTEM_IO;
135         generic_address->bit_width = bit_width;
136         generic_address->bit_offset = 0;
137         generic_address->access_width = 0;
138 }
139
140 /*******************************************************************************
141  *
142  * FUNCTION:    acpi_tb_parse_fadt
143  *
144  * PARAMETERS:  table_index         - Index for the FADT
145  *              Flags               - Flags
146  *
147  * RETURN:      None
148  *
149  * DESCRIPTION: Initialize the FADT, DSDT and FACS tables
150  *              (FADT contains the addresses of the DSDT and FACS)
151  *
152  ******************************************************************************/
153
154 void acpi_tb_parse_fadt(acpi_native_uint table_index, u8 flags)
155 {
156         u32 length;
157         struct acpi_table_header *table;
158
159         /*
160          * The FADT has multiple versions with different lengths,
161          * and it contains pointers to both the DSDT and FACS tables.
162          *
163          * Get a local copy of the FADT and convert it to a common format
164          * Map entire FADT, assumed to be smaller than one page.
165          */
166         length = acpi_gbl_root_table_list.tables[table_index].length;
167
168         table =
169             acpi_os_map_memory(acpi_gbl_root_table_list.tables[table_index].
170                                address, length);
171         if (!table) {
172                 return;
173         }
174
175         /*
176          * Validate the FADT checksum before we copy the table. Ignore
177          * checksum error as we want to try to get the DSDT and FACS.
178          */
179         (void)acpi_tb_verify_checksum(table, length);
180
181         /*
182          * If the FADT is larger than what we know about, we have a problem.
183          * Truncate the table, but make some noise.
184          */
185         if (length > sizeof(struct acpi_table_fadt)) {
186                 ACPI_WARNING((AE_INFO,
187                               "FADT (revision %u) is too large, truncating length 0x%X to 0x%X",
188                               table->revision, length,
189                               sizeof(struct acpi_table_fadt)));
190         }
191
192         /* Copy the entire FADT locally. Zero first for tb_convert_fadt */
193
194         ACPI_MEMSET(&acpi_gbl_FADT, 0, sizeof(struct acpi_table_fadt));
195         ACPI_MEMCPY(&acpi_gbl_FADT, table,
196                     ACPI_MIN(length, sizeof(struct acpi_table_fadt)));
197
198         /* All done with the real FADT, unmap it */
199
200         acpi_os_unmap_memory(table, length);
201
202         /*
203          * 1) Convert the local copy of the FADT to the common internal format
204          * 2) Validate some of the important values within the FADT
205          */
206         acpi_tb_convert_fadt();
207         acpi_tb_validate_fadt(&acpi_gbl_FADT);
208
209         /* Obtain the DSDT and FACS tables via their addresses within the FADT */
210
211         acpi_tb_install_table((acpi_physical_address) acpi_gbl_FADT.Xdsdt,
212                               flags, ACPI_SIG_DSDT, ACPI_TABLE_INDEX_DSDT);
213
214         acpi_tb_install_table((acpi_physical_address) acpi_gbl_FADT.Xfacs,
215                               flags, ACPI_SIG_FACS, ACPI_TABLE_INDEX_FACS);
216 }
217
218 /*******************************************************************************
219  *
220  * FUNCTION:    acpi_tb_convert_fadt
221  *
222  * PARAMETERS:  None, uses acpi_gbl_FADT
223  *
224  * RETURN:      None
225  *
226  * DESCRIPTION: Converts all versions of the FADT to a common internal format.
227  *              -> Expand all 32-bit addresses to 64-bit.
228  *
229  * NOTE:        acpi_gbl_FADT must be of size (struct acpi_table_fadt),
230  *              and must contain a copy of the actual FADT.
231  *
232  * ACPICA will use the "X" fields of the FADT for all addresses.
233  *
234  * "X" fields are optional extensions to the original V1.0 fields. Even if
235  * they are present in the structure, they can be optionally not used by
236  * setting them to zero. Therefore, we must selectively expand V1.0 fields
237  * if the corresponding X field is zero.
238  *
239  * For ACPI 1.0 FADTs, all address fields are expanded to the corresponding
240  * "X" fields.
241  *
242  * For ACPI 2.0 FADTs, any "X" fields that are NULL are filled in by
243  * expanding the corresponding ACPI 1.0 field.
244  *
245  ******************************************************************************/
246
247 void acpi_tb_convert_fadt(void)
248 {
249         u8 pm1_register_length;
250         struct acpi_generic_address *target;
251         acpi_native_uint i;
252
253         /* Update the local FADT table header length */
254
255         acpi_gbl_FADT.header.length = sizeof(struct acpi_table_fadt);
256
257         /* Expand the 32-bit FACS and DSDT addresses to 64-bit as necessary */
258
259         if (!acpi_gbl_FADT.Xfacs) {
260                 acpi_gbl_FADT.Xfacs = (u64) acpi_gbl_FADT.facs;
261         }
262
263         if (!acpi_gbl_FADT.Xdsdt) {
264                 acpi_gbl_FADT.Xdsdt = (u64) acpi_gbl_FADT.dsdt;
265         }
266
267         /*
268          * Expand the 32-bit V1.0 addresses to the 64-bit "X" generic address
269          * structures as necessary.
270          */
271         for (i = 0; i < ACPI_FADT_INFO_ENTRIES; i++) {
272                 target =
273                     ACPI_ADD_PTR(struct acpi_generic_address, &acpi_gbl_FADT,
274                                  fadt_info_table[i].target);
275
276                 /* Expand only if the X target is null */
277
278                 if (!target->address) {
279                         acpi_tb_init_generic_address(target,
280                                                      *ACPI_ADD_PTR(u8,
281                                                                    &acpi_gbl_FADT,
282                                                                    fadt_info_table
283                                                                    [i].length),
284                                                      (u64) * ACPI_ADD_PTR(u32,
285                                                                           &acpi_gbl_FADT,
286                                                                           fadt_info_table
287                                                                           [i].
288                                                                           source));
289                 }
290         }
291
292         /*
293          * Calculate separate GAS structs for the PM1 Enable registers.
294          * These addresses do not appear (directly) in the FADT, so it is
295          * useful to calculate them once, here.
296          *
297          * The PM event blocks are split into two register blocks, first is the
298          * PM Status Register block, followed immediately by the PM Enable Register
299          * block. Each is of length (pm1_event_length/2)
300          */
301         pm1_register_length = (u8) ACPI_DIV_2(acpi_gbl_FADT.pm1_event_length);
302
303         /* The PM1A register block is required */
304
305         acpi_tb_init_generic_address(&acpi_gbl_xpm1a_enable,
306                                      pm1_register_length,
307                                      (acpi_gbl_FADT.xpm1a_event_block.address +
308                                       pm1_register_length));
309
310         /* The PM1B register block is optional, ignore if not present */
311
312         if (acpi_gbl_FADT.xpm1b_event_block.address) {
313                 acpi_tb_init_generic_address(&acpi_gbl_xpm1b_enable,
314                                              pm1_register_length,
315                                              (acpi_gbl_FADT.xpm1b_event_block.
316                                               address + pm1_register_length));
317         }
318 }
319
320 /******************************************************************************
321  *
322  * FUNCTION:    acpi_tb_validate_fadt
323  *
324  * PARAMETERS:  Table           - Pointer to the FADT to be validated
325  *
326  * RETURN:      None
327  *
328  * DESCRIPTION: Validate various important fields within the FADT. If a problem
329  *              is found, issue a message, but no status is returned.
330  *              Used by both the table manager and the disassembler.
331  *
332  * Possible additional checks:
333  * (acpi_gbl_FADT.pm1_event_length >= 4)
334  * (acpi_gbl_FADT.pm1_control_length >= 2)
335  * (acpi_gbl_FADT.pm_timer_length >= 4)
336  * Gpe block lengths must be multiple of 2
337  *
338  ******************************************************************************/
339
340 void acpi_tb_validate_fadt(struct acpi_table_fadt *table)
341 {
342         u32 *address32;
343         struct acpi_generic_address *address64;
344         u8 length;
345         acpi_native_uint i;
346
347         /* Examine all of the 64-bit extended address fields (X fields) */
348
349         for (i = 0; i < ACPI_FADT_INFO_ENTRIES; i++) {
350
351                 /* Generate pointers to the 32-bit and 64-bit addresses and get the length */
352
353                 address64 =
354                     ACPI_ADD_PTR(struct acpi_generic_address, table,
355                                  fadt_info_table[i].target);
356                 address32 = ACPI_ADD_PTR(u32, table, fadt_info_table[i].source);
357                 length = *ACPI_ADD_PTR(u8, table, fadt_info_table[i].length);
358
359                 if (fadt_info_table[i].type & ACPI_FADT_REQUIRED) {
360                         /*
361                          * Field is required (Pm1a_event, Pm1a_control, pm_timer).
362                          * Both the address and length must be non-zero.
363                          */
364                         if (!address64->address || !length) {
365                                 ACPI_ERROR((AE_INFO,
366                                             "Required field \"%s\" has zero address and/or length: %8.8X%8.8X/%X",
367                                             fadt_info_table[i].name,
368                                             ACPI_FORMAT_UINT64(address64->
369                                                                address),
370                                             length));
371                         }
372                 } else if (fadt_info_table[i].type & ACPI_FADT_SEPARATE_LENGTH) {
373                         /*
374                          * Field is optional (PM2Control, GPE0, GPE1) AND has its own
375                          * length field. If present, both the address and length must be valid.
376                          */
377                         if ((address64->address && !length)
378                             || (!address64->address && length)) {
379                                 ACPI_WARNING((AE_INFO,
380                                               "Optional field \"%s\" has zero address or length: %8.8X%8.8X/%X",
381                                               fadt_info_table[i].name,
382                                               ACPI_FORMAT_UINT64(address64->
383                                                                  address),
384                                               length));
385                         }
386                 }
387
388                 /* If both 32- and 64-bit addresses are valid (non-zero), they must match */
389
390                 if (address64->address && *address32 &&
391                     (address64->address != (u64) * address32)) {
392                         ACPI_ERROR((AE_INFO,
393                                     "32/64X address mismatch in \"%s\": [%8.8X] [%8.8X%8.8X], using 64X",
394                                     fadt_info_table[i].name, *address32,
395                                     ACPI_FORMAT_UINT64(address64->address)));
396                 }
397         }
398 }