vserver 1.9.3
[linux-2.6.git] / drivers / acpi / tables / tbxfroot.c
1 /******************************************************************************
2  *
3  * Module Name: tbxfroot - Find the root ACPI table (RSDT)
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    ("tbxfroot")
51
52
53 /*******************************************************************************
54  *
55  * FUNCTION:    acpi_tb_find_table
56  *
57  * PARAMETERS:  Signature           - String with ACPI table signature
58  *              oem_id              - String with the table OEM ID
59  *              oem_table_id        - String with the OEM Table ID.
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Find an ACPI table (in the RSDT/XSDT) that matches the
64  *              Signature, OEM ID and OEM Table ID.
65  *
66  ******************************************************************************/
67
68 acpi_status
69 acpi_tb_find_table (
70         char                            *signature,
71         char                            *oem_id,
72         char                            *oem_table_id,
73         struct acpi_table_header        **table_ptr)
74 {
75         acpi_status                     status;
76         struct acpi_table_header        *table;
77
78
79         ACPI_FUNCTION_TRACE ("tb_find_table");
80
81
82         /* Validate string lengths */
83
84         if ((ACPI_STRLEN (signature)  > ACPI_NAME_SIZE) ||
85                 (ACPI_STRLEN (oem_id)     > sizeof (table->oem_id)) ||
86                 (ACPI_STRLEN (oem_table_id) > sizeof (table->oem_table_id))) {
87                 return_ACPI_STATUS (AE_AML_STRING_LIMIT);
88         }
89
90         /* Find the table */
91
92         status = acpi_get_firmware_table (signature, 1,
93                            ACPI_LOGICAL_ADDRESSING, &table);
94         if (ACPI_FAILURE (status)) {
95                 return_ACPI_STATUS (status);
96         }
97
98         /* Check oem_id and oem_table_id */
99
100         if ((oem_id[0]     && ACPI_STRCMP (oem_id, table->oem_id)) ||
101                 (oem_table_id[0] && ACPI_STRCMP (oem_table_id, table->oem_table_id))) {
102                 return_ACPI_STATUS (AE_AML_NAME_NOT_FOUND);
103         }
104
105         *table_ptr = table;
106         return_ACPI_STATUS (AE_OK);
107 }
108
109
110 /*******************************************************************************
111  *
112  * FUNCTION:    acpi_get_firmware_table
113  *
114  * PARAMETERS:  Signature       - Any ACPI table signature
115  *              Instance        - the non zero instance of the table, allows
116  *                                support for multiple tables of the same type
117  *              Flags           - Physical/Virtual support
118  *              table_pointer   - Where a buffer containing the table is
119  *                                returned
120  *
121  * RETURN:      Status
122  *
123  * DESCRIPTION: This function is called to get an ACPI table. A buffer is
124  *              allocated for the table and returned in table_pointer.
125  *              This table will be a complete table including the header.
126  *
127  ******************************************************************************/
128
129 acpi_status
130 acpi_get_firmware_table (
131         acpi_string                     signature,
132         u32                             instance,
133         u32                             flags,
134         struct acpi_table_header        **table_pointer)
135 {
136         acpi_status                     status;
137         struct acpi_pointer             address;
138         struct acpi_table_header        *header = NULL;
139         struct acpi_table_desc          *table_info = NULL;
140         struct acpi_table_desc          *rsdt_info;
141         u32                             table_count;
142         u32                             i;
143         u32                             j;
144
145
146         ACPI_FUNCTION_TRACE ("acpi_get_firmware_table");
147
148
149         /*
150          * Ensure that at least the table manager is initialized.  We don't
151          * require that the entire ACPI subsystem is up for this interface.
152          * If we have a buffer, we must have a length too
153          */
154         if ((instance == 0)     ||
155                 (!signature)        ||
156                 (!table_pointer)) {
157                 return_ACPI_STATUS (AE_BAD_PARAMETER);
158         }
159
160         /* Ensure that we have a RSDP */
161
162         if (!acpi_gbl_RSDP) {
163                 /* Get the RSDP */
164
165                 status = acpi_os_get_root_pointer (flags, &address);
166                 if (ACPI_FAILURE (status)) {
167                         ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "RSDP not found\n"));
168                         return_ACPI_STATUS (AE_NO_ACPI_TABLES);
169                 }
170
171                 /* Map and validate the RSDP */
172
173                 if ((flags & ACPI_MEMORY_MODE) == ACPI_LOGICAL_ADDRESSING) {
174                         status = acpi_os_map_memory (address.pointer.physical, sizeof (struct rsdp_descriptor),
175                                           (void *) &acpi_gbl_RSDP);
176                         if (ACPI_FAILURE (status)) {
177                                 return_ACPI_STATUS (status);
178                         }
179                 }
180                 else {
181                         acpi_gbl_RSDP = address.pointer.logical;
182                 }
183
184                 /* The signature and checksum must both be correct */
185
186                 if (ACPI_STRNCMP ((char *) acpi_gbl_RSDP, RSDP_SIG, sizeof (RSDP_SIG)-1) != 0) {
187                         /* Nope, BAD Signature */
188
189                         return_ACPI_STATUS (AE_BAD_SIGNATURE);
190                 }
191
192                 if (acpi_tb_checksum (acpi_gbl_RSDP, ACPI_RSDP_CHECKSUM_LENGTH) != 0) {
193                         /* Nope, BAD Checksum */
194
195                         return_ACPI_STATUS (AE_BAD_CHECKSUM);
196                 }
197         }
198
199         /* Get the RSDT address via the RSDP */
200
201         acpi_tb_get_rsdt_address (&address);
202         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
203                 "RSDP located at %p, RSDT physical=%8.8X%8.8X \n",
204                 acpi_gbl_RSDP,
205                 ACPI_FORMAT_UINT64 (address.pointer.value)));
206
207         /* Insert processor_mode flags */
208
209         address.pointer_type |= flags;
210
211         /* Get and validate the RSDT */
212
213         rsdt_info = ACPI_MEM_CALLOCATE (sizeof (struct acpi_table_desc));
214         if (!rsdt_info) {
215                 return_ACPI_STATUS (AE_NO_MEMORY);
216         }
217
218         status = acpi_tb_get_table (&address, rsdt_info);
219         if (ACPI_FAILURE (status)) {
220                 goto cleanup;
221         }
222
223         status = acpi_tb_validate_rsdt (rsdt_info->pointer);
224         if (ACPI_FAILURE (status)) {
225                 goto cleanup;
226         }
227
228         /* Allocate a scratch table header and table descriptor */
229
230         header = ACPI_MEM_ALLOCATE (sizeof (struct acpi_table_header));
231         if (!header) {
232                 status = AE_NO_MEMORY;
233                 goto cleanup;
234         }
235
236         table_info = ACPI_MEM_ALLOCATE (sizeof (struct acpi_table_desc));
237         if (!table_info) {
238                 status = AE_NO_MEMORY;
239                 goto cleanup;
240         }
241
242         /* Get the number of table pointers within the RSDT */
243
244         table_count = acpi_tb_get_table_count (acpi_gbl_RSDP, rsdt_info->pointer);
245         address.pointer_type = acpi_gbl_table_flags | flags;
246
247         /*
248          * Search the RSDT/XSDT for the correct instance of the
249          * requested table
250          */
251         for (i = 0, j = 0; i < table_count; i++) {
252                 /* Get the next table pointer, handle RSDT vs. XSDT */
253
254                 if (acpi_gbl_RSDP->revision < 2) {
255                         address.pointer.value = (ACPI_CAST_PTR (
256                                 RSDT_DESCRIPTOR, rsdt_info->pointer))->table_offset_entry[i];
257                 }
258                 else {
259                         address.pointer.value = (ACPI_CAST_PTR (
260                                 XSDT_DESCRIPTOR, rsdt_info->pointer))->table_offset_entry[i];
261                 }
262
263                 /* Get the table header */
264
265                 status = acpi_tb_get_table_header (&address, header);
266                 if (ACPI_FAILURE (status)) {
267                         goto cleanup;
268                 }
269
270                 /* Compare table signatures and table instance */
271
272                 if (!ACPI_STRNCMP (header->signature, signature, ACPI_NAME_SIZE)) {
273                         /* An instance of the table was found */
274
275                         j++;
276                         if (j >= instance) {
277                                 /* Found the correct instance, get the entire table */
278
279                                 status = acpi_tb_get_table_body (&address, header, table_info);
280                                 if (ACPI_FAILURE (status)) {
281                                         goto cleanup;
282                                 }
283
284                                 *table_pointer = table_info->pointer;
285                                 goto cleanup;
286                         }
287                 }
288         }
289
290         /* Did not find the table */
291
292         status = AE_NOT_EXIST;
293
294
295 cleanup:
296         acpi_os_unmap_memory (rsdt_info->pointer, (acpi_size) rsdt_info->pointer->length);
297         ACPI_MEM_FREE (rsdt_info);
298
299         if (header) {
300                 ACPI_MEM_FREE (header);
301         }
302         if (table_info) {
303                 ACPI_MEM_FREE (table_info);
304         }
305         return_ACPI_STATUS (status);
306 }
307
308
309 /* TBD: Move to a new file */
310
311 #if ACPI_MACHINE_WIDTH != 16
312
313 /*******************************************************************************
314  *
315  * FUNCTION:    acpi_find_root_pointer
316  *
317  * PARAMETERS:  **rsdp_address          - Where to place the RSDP address
318  *              Flags                   - Logical/Physical addressing
319  *
320  * RETURN:      Status, Physical address of the RSDP
321  *
322  * DESCRIPTION: Find the RSDP
323  *
324  ******************************************************************************/
325
326 acpi_status
327 acpi_find_root_pointer (
328         u32                             flags,
329         struct acpi_pointer             *rsdp_address)
330 {
331         struct acpi_table_desc          table_info;
332         acpi_status                     status;
333
334
335         ACPI_FUNCTION_TRACE ("acpi_find_root_pointer");
336
337
338         /* Get the RSDP */
339
340         status = acpi_tb_find_rsdp (&table_info, flags);
341         if (ACPI_FAILURE (status)) {
342                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "RSDP structure not found, %s Flags=%X\n",
343                         acpi_format_exception (status), flags));
344                 return_ACPI_STATUS (AE_NO_ACPI_TABLES);
345         }
346
347         rsdp_address->pointer_type = ACPI_PHYSICAL_POINTER;
348         rsdp_address->pointer.physical = table_info.physical_address;
349         return_ACPI_STATUS (AE_OK);
350 }
351
352
353 /*******************************************************************************
354  *
355  * FUNCTION:    acpi_tb_scan_memory_for_rsdp
356  *
357  * PARAMETERS:  start_address       - Starting pointer for search
358  *              Length              - Maximum length to search
359  *
360  * RETURN:      Pointer to the RSDP if found, otherwise NULL.
361  *
362  * DESCRIPTION: Search a block of memory for the RSDP signature
363  *
364  ******************************************************************************/
365
366 u8 *
367 acpi_tb_scan_memory_for_rsdp (
368         u8                              *start_address,
369         u32                             length)
370 {
371         u32                             offset;
372         u8                              *mem_rover;
373
374
375         ACPI_FUNCTION_TRACE ("tb_scan_memory_for_rsdp");
376
377
378         /* Search from given start addr for the requested length  */
379
380         for (offset = 0, mem_rover = start_address;
381                  offset < length;
382                  offset += ACPI_RSDP_SCAN_STEP, mem_rover += ACPI_RSDP_SCAN_STEP) {
383
384                 /* The signature and checksum must both be correct */
385
386                 if (ACPI_STRNCMP ((char *) mem_rover,
387                                 RSDP_SIG, sizeof (RSDP_SIG)-1) == 0 &&
388                         acpi_tb_checksum (mem_rover, ACPI_RSDP_CHECKSUM_LENGTH) == 0) {
389                         /* If so, we have found the RSDP */
390
391                         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
392                                 "RSDP located at physical address %p\n",mem_rover));
393                         return_PTR (mem_rover);
394                 }
395         }
396
397         /* Searched entire block, no RSDP was found */
398
399         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,"Searched entire block, no RSDP was found.\n"));
400         return_PTR (NULL);
401 }
402
403
404 /*******************************************************************************
405  *
406  * FUNCTION:    acpi_tb_find_rsdp
407  *
408  * PARAMETERS:  *table_info             - Where the table info is returned
409  *              Flags                   - Current memory mode (logical vs.
410  *                                        physical addressing)
411  *
412  * RETURN:      Status, RSDP physical address
413  *
414  * DESCRIPTION: search lower 1_mbyte of memory for the root system descriptor
415  *              pointer structure.  If it is found, set *RSDP to point to it.
416  *
417  *              NOTE1: The RSDp must be either in the first 1_k of the Extended
418  *              BIOS Data Area or between E0000 and FFFFF (From ACPI Spec.)
419  *              Only a 32-bit physical address is necessary.
420  *
421  *              NOTE2: This function is always available, regardless of the
422  *              initialization state of the rest of ACPI.
423  *
424  ******************************************************************************/
425
426 acpi_status
427 acpi_tb_find_rsdp (
428         struct acpi_table_desc          *table_info,
429         u32                             flags)
430 {
431         u8                              *table_ptr;
432         u8                              *mem_rover;
433         u32                             physical_address;
434         acpi_status                     status;
435
436
437         ACPI_FUNCTION_TRACE ("tb_find_rsdp");
438
439
440         /*
441          * Scan supports either 1) Logical addressing or 2) Physical addressing
442          */
443         if ((flags & ACPI_MEMORY_MODE) == ACPI_LOGICAL_ADDRESSING) {
444                 /*
445                  * 1a) Get the location of the EBDA
446                  */
447                 status = acpi_os_map_memory ((acpi_physical_address) ACPI_EBDA_PTR_LOCATION,
448                                   ACPI_EBDA_PTR_LENGTH,
449                                   (void *) &table_ptr);
450                 if (ACPI_FAILURE (status)) {
451                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not map memory at %8.8X for length %X\n",
452                                 ACPI_EBDA_PTR_LOCATION, ACPI_EBDA_PTR_LENGTH));
453                         return_ACPI_STATUS (status);
454                 }
455
456                 ACPI_MOVE_16_TO_32 (&physical_address, table_ptr);
457                 physical_address <<= 4;                 /* Convert segment to physical address */
458                 acpi_os_unmap_memory (table_ptr, ACPI_EBDA_PTR_LENGTH);
459
460                 /* EBDA present? */
461
462                 if (physical_address > 0x400) {
463                         /*
464                          * 1b) Search EBDA paragraphs (EBDa is required to be a minimum of 1_k length)
465                          */
466                         status = acpi_os_map_memory ((acpi_physical_address) physical_address,
467                                           ACPI_EBDA_WINDOW_SIZE,
468                                           (void *) &table_ptr);
469                         if (ACPI_FAILURE (status)) {
470                                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not map memory at %8.8X for length %X\n",
471                                         physical_address, ACPI_EBDA_WINDOW_SIZE));
472                                 return_ACPI_STATUS (status);
473                         }
474
475                         mem_rover = acpi_tb_scan_memory_for_rsdp (table_ptr, ACPI_EBDA_WINDOW_SIZE);
476                         acpi_os_unmap_memory (table_ptr, ACPI_EBDA_WINDOW_SIZE);
477
478                         if (mem_rover) {
479                                 /* Found it, return the physical address */
480
481                                 physical_address += ACPI_PTR_DIFF (mem_rover, table_ptr);
482
483                                 table_info->physical_address = (acpi_physical_address) physical_address;
484                                 return_ACPI_STATUS (AE_OK);
485                         }
486                 }
487
488                 /*
489                  * 2) Search upper memory: 16-byte boundaries in E0000h-FFFFFh
490                  */
491                 status = acpi_os_map_memory ((acpi_physical_address) ACPI_HI_RSDP_WINDOW_BASE,
492                                   ACPI_HI_RSDP_WINDOW_SIZE,
493                                   (void *) &table_ptr);
494                 if (ACPI_FAILURE (status)) {
495                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not map memory at %8.8X for length %X\n",
496                                 ACPI_HI_RSDP_WINDOW_BASE, ACPI_HI_RSDP_WINDOW_SIZE));
497                         return_ACPI_STATUS (status);
498                 }
499
500                 mem_rover = acpi_tb_scan_memory_for_rsdp (table_ptr, ACPI_HI_RSDP_WINDOW_SIZE);
501                 acpi_os_unmap_memory (table_ptr, ACPI_HI_RSDP_WINDOW_SIZE);
502
503                 if (mem_rover) {
504                         /* Found it, return the physical address */
505
506                         physical_address = ACPI_HI_RSDP_WINDOW_BASE + ACPI_PTR_DIFF (mem_rover, table_ptr);
507
508                         table_info->physical_address = (acpi_physical_address) physical_address;
509                         return_ACPI_STATUS (AE_OK);
510                 }
511         }
512
513         /*
514          * Physical addressing
515          */
516         else {
517                 /*
518                  * 1a) Get the location of the EBDA
519                  */
520                 ACPI_MOVE_16_TO_32 (&physical_address, ACPI_EBDA_PTR_LOCATION);
521                 physical_address <<= 4;     /* Convert segment to physical address */
522
523                 /* EBDA present? */
524
525                 if (physical_address > 0x400) {
526                         /*
527                          * 1b) Search EBDA paragraphs (EBDa is required to be a minimum of 1_k length)
528                          */
529                         mem_rover = acpi_tb_scan_memory_for_rsdp (ACPI_PHYSADDR_TO_PTR (physical_address),
530                                           ACPI_EBDA_WINDOW_SIZE);
531                         if (mem_rover) {
532                                 /* Found it, return the physical address */
533
534                                 table_info->physical_address = ACPI_TO_INTEGER (mem_rover);
535                                 return_ACPI_STATUS (AE_OK);
536                         }
537                 }
538
539                 /*
540                  * 2) Search upper memory: 16-byte boundaries in E0000h-FFFFFh
541                  */
542                 mem_rover = acpi_tb_scan_memory_for_rsdp (ACPI_PHYSADDR_TO_PTR (ACPI_HI_RSDP_WINDOW_BASE),
543                                   ACPI_HI_RSDP_WINDOW_SIZE);
544                 if (mem_rover) {
545                         /* Found it, return the physical address */
546
547                         table_info->physical_address = ACPI_TO_INTEGER (mem_rover);
548                         return_ACPI_STATUS (AE_OK);
549                 }
550         }
551
552         /* RSDP signature was not found */
553
554         return_ACPI_STATUS (AE_NOT_FOUND);
555 }
556
557 #endif
558