machine/stdarg provides __va* macros, not va* macros, use <stdarg.h> instead
[dragonfly.git] / sys / contrib / dev / acpica / tbxfroot.c
1 /******************************************************************************
2  *
3  * Module Name: tbxfroot - Find the root ACPI table (RSDT)
4  *              $Revision: 68 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/tbxfroot.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $                                                               */
117
118 #define __TBXFROOT_C__
119
120 #include "acpi.h"
121 #include "actables.h"
122
123
124 #define _COMPONENT          ACPI_TABLES
125         ACPI_MODULE_NAME    ("tbxfroot")
126
127
128 /*******************************************************************************
129  *
130  * FUNCTION:    AcpiTbFindTable
131  *
132  * PARAMETERS:  Signature           - String with ACPI table signature
133  *              OemId               - String with the table OEM ID
134  *              OemTableId          - String with the OEM Table ID.
135  *
136  * RETURN:      Status
137  *
138  * DESCRIPTION: Find an ACPI table (in the RSDT/XSDT) that matches the
139  *              Signature, OEM ID and OEM Table ID.
140  *
141  ******************************************************************************/
142
143 ACPI_STATUS
144 AcpiTbFindTable (
145     char                    *Signature,
146     char                    *OemId,
147     char                    *OemTableId,
148     ACPI_TABLE_HEADER       **TablePtr)
149 {
150     ACPI_STATUS             Status;
151     ACPI_TABLE_HEADER       *Table;
152
153
154     ACPI_FUNCTION_TRACE ("TbFindTable");
155
156
157     /* Validate string lengths */
158
159     if ((ACPI_STRLEN (Signature)  > ACPI_NAME_SIZE) ||
160         (ACPI_STRLEN (OemId)      > sizeof (Table->OemId)) ||
161         (ACPI_STRLEN (OemTableId) > sizeof (Table->OemTableId)))
162     {
163         return_ACPI_STATUS (AE_AML_STRING_LIMIT);
164     }
165
166     /* Find the table */
167
168     Status = AcpiGetFirmwareTable (Signature, 1,
169                         ACPI_LOGICAL_ADDRESSING, &Table);
170     if (ACPI_FAILURE (Status))
171     {
172         return_ACPI_STATUS (Status);
173     }
174
175     /* Check OemId and OemTableId */
176
177     if ((OemId[0]      && ACPI_STRCMP (OemId, Table->OemId)) ||
178         (OemTableId[0] && ACPI_STRCMP (OemTableId, Table->OemTableId)))
179     {
180         return_ACPI_STATUS (AE_AML_NAME_NOT_FOUND);
181     }
182
183     *TablePtr = Table;
184     return_ACPI_STATUS (AE_OK);
185 }
186
187
188 /*******************************************************************************
189  *
190  * FUNCTION:    AcpiGetFirmwareTable
191  *
192  * PARAMETERS:  Signature       - Any ACPI table signature
193  *              Instance        - the non zero instance of the table, allows
194  *                                support for multiple tables of the same type
195  *              Flags           - Physical/Virtual support
196  *              RetBuffer       - pointer to a structure containing a buffer to
197  *                                receive the table
198  *
199  * RETURN:      Status
200  *
201  * DESCRIPTION: This function is called to get an ACPI table.  The caller
202  *              supplies an OutBuffer large enough to contain the entire ACPI
203  *              table.  Upon completion
204  *              the OutBuffer->Length field will indicate the number of bytes
205  *              copied into the OutBuffer->BufPtr buffer.  This table will be
206  *              a complete table including the header.
207  *
208  ******************************************************************************/
209
210 ACPI_STATUS
211 AcpiGetFirmwareTable (
212     ACPI_STRING             Signature,
213     UINT32                  Instance,
214     UINT32                  Flags,
215     ACPI_TABLE_HEADER       **TablePointer)
216 {
217     ACPI_POINTER            RsdpAddress;
218     ACPI_POINTER            Address;
219     ACPI_STATUS             Status;
220     ACPI_TABLE_HEADER       Header;
221     ACPI_TABLE_DESC         TableInfo;
222     ACPI_TABLE_DESC         RsdtInfo;
223     UINT32                  TableCount;
224     UINT32                  i;
225     UINT32                  j;
226
227
228     ACPI_FUNCTION_TRACE ("AcpiGetFirmwareTable");
229
230
231     /*
232      * Ensure that at least the table manager is initialized.  We don't
233      * require that the entire ACPI subsystem is up for this interface
234      */
235
236     /*
237      *  If we have a buffer, we must have a length too
238      */
239     if ((Instance == 0)                 ||
240         (!Signature)                    ||
241         (!TablePointer))
242     {
243         return_ACPI_STATUS (AE_BAD_PARAMETER);
244     }
245
246     RsdtInfo.Pointer = NULL;
247
248     if (!AcpiGbl_RSDP)
249     {
250         /* Get the RSDP */
251
252         Status = AcpiOsGetRootPointer (Flags, &RsdpAddress);
253         if (ACPI_FAILURE (Status))
254         {
255             ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "RSDP  not found\n"));
256             return_ACPI_STATUS (AE_NO_ACPI_TABLES);
257         }
258
259         /* Map and validate the RSDP */
260
261         if ((Flags & ACPI_MEMORY_MODE) == ACPI_LOGICAL_ADDRESSING)
262         {
263             Status = AcpiOsMapMemory (RsdpAddress.Pointer.Physical, sizeof (RSDP_DESCRIPTOR),
264                                         (void **) &AcpiGbl_RSDP);
265             if (ACPI_FAILURE (Status))
266             {
267                 return_ACPI_STATUS (Status);
268             }
269         }
270         else
271         {
272             AcpiGbl_RSDP = RsdpAddress.Pointer.Logical;
273         }
274
275         /*
276          *  The signature and checksum must both be correct
277          */
278         if (ACPI_STRNCMP ((char *) AcpiGbl_RSDP, RSDP_SIG, sizeof (RSDP_SIG)-1) != 0)
279         {
280             /* Nope, BAD Signature */
281
282             return_ACPI_STATUS (AE_BAD_SIGNATURE);
283         }
284
285         if (AcpiTbChecksum (AcpiGbl_RSDP, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
286         {
287             /* Nope, BAD Checksum */
288
289             return_ACPI_STATUS (AE_BAD_CHECKSUM);
290         }
291     }
292
293     /* Get the RSDT and validate it */
294
295     AcpiTbGetRsdtAddress (&Address);
296
297     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
298         "RSDP located at %p, RSDT physical=%8.8X%8.8X \n",
299         AcpiGbl_RSDP,
300         ACPI_HIDWORD (Address.Pointer.Value),
301         ACPI_LODWORD (Address.Pointer.Value)));
302
303     /* Insert ProcessorMode flags */
304
305     Address.PointerType |= Flags;
306
307     Status = AcpiTbGetTable (&Address, &RsdtInfo);
308     if (ACPI_FAILURE (Status))
309     {
310         return_ACPI_STATUS (Status);
311     }
312
313     Status = AcpiTbValidateRsdt (RsdtInfo.Pointer);
314     if (ACPI_FAILURE (Status))
315     {
316         goto Cleanup;
317     }
318
319     /* Get the number of table pointers within the RSDT */
320
321     TableCount = AcpiTbGetTableCount (AcpiGbl_RSDP, RsdtInfo.Pointer);
322
323     Address.PointerType = AcpiGbl_TableFlags | Flags;
324
325     /*
326      * Search the RSDT/XSDT for the correct instance of the
327      * requested table
328      */
329     for (i = 0, j = 0; i < TableCount; i++)
330     {
331         /* Get the next table pointer, handle RSDT vs. XSDT */
332
333         if (AcpiGbl_RSDP->Revision < 2)
334         {
335             Address.Pointer.Value = ((RSDT_DESCRIPTOR *) RsdtInfo.Pointer)->TableOffsetEntry[i];
336         }
337         else
338         {
339             Address.Pointer.Value = ACPI_GET_ADDRESS (
340                 ((XSDT_DESCRIPTOR *) RsdtInfo.Pointer)->TableOffsetEntry[i]);
341         }
342
343         /* Get the table header */
344
345         Status = AcpiTbGetTableHeader (&Address, &Header);
346         if (ACPI_FAILURE (Status))
347         {
348             goto Cleanup;
349         }
350
351         /* Compare table signatures and table instance */
352
353         if (!ACPI_STRNCMP (Header.Signature, Signature, ACPI_NAME_SIZE))
354         {
355             /* An instance of the table was found */
356
357             j++;
358             if (j >= Instance)
359             {
360                 /* Found the correct instance, get the entire table */
361
362                 Status = AcpiTbGetTableBody (&Address, &Header, &TableInfo);
363                 if (ACPI_FAILURE (Status))
364                 {
365                     goto Cleanup;
366                 }
367
368                 *TablePointer = TableInfo.Pointer;
369                 goto Cleanup;
370             }
371         }
372     }
373
374     /* Did not find the table */
375
376     Status = AE_NOT_EXIST;
377
378
379 Cleanup:
380     AcpiOsUnmapMemory (RsdtInfo.Pointer, (ACPI_SIZE) RsdtInfo.Pointer->Length);
381     return_ACPI_STATUS (Status);
382 }
383
384
385 /* TBD: Move to a new file */
386
387 #if ACPI_MACHINE_WIDTH != 16
388
389 /*******************************************************************************
390  *
391  * FUNCTION:    AcpiFindRootPointer
392  *
393  * PARAMETERS:  **RsdpAddress           - Where to place the RSDP address
394  *              Flags                   - Logical/Physical addressing
395  *
396  * RETURN:      Status, Physical address of the RSDP
397  *
398  * DESCRIPTION: Find the RSDP
399  *
400  ******************************************************************************/
401
402 ACPI_STATUS
403 AcpiFindRootPointer (
404     UINT32                  Flags,
405     ACPI_POINTER            *RsdpAddress)
406 {
407     ACPI_TABLE_DESC         TableInfo;
408     ACPI_STATUS             Status;
409
410
411     ACPI_FUNCTION_TRACE ("AcpiFindRootPointer");
412
413
414     /* Get the RSDP */
415
416     Status = AcpiTbFindRsdp (&TableInfo, Flags);
417     if (ACPI_FAILURE (Status))
418     {
419         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "RSDP structure not found, %s Flags=%X\n",
420             AcpiFormatException (Status), Flags));
421         return_ACPI_STATUS (AE_NO_ACPI_TABLES);
422     }
423
424     RsdpAddress->PointerType = ACPI_PHYSICAL_POINTER;
425     RsdpAddress->Pointer.Physical = TableInfo.PhysicalAddress;
426     return_ACPI_STATUS (AE_OK);
427 }
428
429
430 /*******************************************************************************
431  *
432  * FUNCTION:    AcpiTbScanMemoryForRsdp
433  *
434  * PARAMETERS:  StartAddress        - Starting pointer for search
435  *              Length              - Maximum length to search
436  *
437  * RETURN:      Pointer to the RSDP if found, otherwise NULL.
438  *
439  * DESCRIPTION: Search a block of memory for the RSDP signature
440  *
441  ******************************************************************************/
442
443 UINT8 *
444 AcpiTbScanMemoryForRsdp (
445     UINT8                   *StartAddress,
446     UINT32                  Length)
447 {
448     UINT32                  Offset;
449     UINT8                   *MemRover;
450
451
452     ACPI_FUNCTION_TRACE ("TbScanMemoryForRsdp");
453
454
455     /* Search from given start addr for the requested length  */
456
457     for (Offset = 0, MemRover = StartAddress;
458          Offset < Length;
459          Offset += ACPI_RSDP_SCAN_STEP, MemRover += ACPI_RSDP_SCAN_STEP)
460     {
461
462         /* The signature and checksum must both be correct */
463
464         if (ACPI_STRNCMP ((char *) MemRover,
465                 RSDP_SIG, sizeof (RSDP_SIG)-1) == 0 &&
466             AcpiTbChecksum (MemRover, ACPI_RSDP_CHECKSUM_LENGTH) == 0)
467         {
468             /* If so, we have found the RSDP */
469
470             ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
471                 "RSDP located at physical address %p\n",MemRover));
472             return_PTR (MemRover);
473         }
474     }
475
476     /* Searched entire block, no RSDP was found */
477
478     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,"Searched entire block, no RSDP was found.\n"));
479     return_PTR (NULL);
480 }
481
482
483 /*******************************************************************************
484  *
485  * FUNCTION:    AcpiTbFindRsdp
486  *
487  * PARAMETERS:  *TableInfo              - Where the table info is returned
488  *              Flags                   - Current memory mode (logical vs.
489  *                                        physical addressing)
490  *
491  * RETURN:      Status
492  *
493  * DESCRIPTION: Search lower 1Mbyte of memory for the root system descriptor
494  *              pointer structure.  If it is found, set *RSDP to point to it.
495  *
496  *              NOTE: The RSDP must be either in the first 1K of the Extended
497  *              BIOS Data Area or between E0000 and FFFFF (ACPI 1.0 section
498  *              5.2.2; assertion #421).
499  *
500  ******************************************************************************/
501
502 ACPI_STATUS
503 AcpiTbFindRsdp (
504     ACPI_TABLE_DESC         *TableInfo,
505     UINT32                  Flags)
506 {
507     UINT8                   *TablePtr;
508     UINT8                   *MemRover;
509     UINT64                  PhysAddr;
510     ACPI_STATUS             Status = AE_OK;
511
512
513     ACPI_FUNCTION_TRACE ("TbFindRsdp");
514
515
516     /*
517      * Scan supports either 1) Logical addressing or 2) Physical addressing
518      */
519     if ((Flags & ACPI_MEMORY_MODE) == ACPI_LOGICAL_ADDRESSING)
520     {
521         /*
522          * 1) Search EBDA (low memory) paragraphs
523          */
524         Status = AcpiOsMapMemory ((UINT64) ACPI_LO_RSDP_WINDOW_BASE, ACPI_LO_RSDP_WINDOW_SIZE,
525                                     (void **) &TablePtr);
526         if (ACPI_FAILURE (Status))
527         {
528             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not map memory at %X for length %X\n",
529                 ACPI_LO_RSDP_WINDOW_BASE, ACPI_LO_RSDP_WINDOW_SIZE));
530             return_ACPI_STATUS (Status);
531         }
532
533         MemRover = AcpiTbScanMemoryForRsdp (TablePtr, ACPI_LO_RSDP_WINDOW_SIZE);
534         AcpiOsUnmapMemory (TablePtr, ACPI_LO_RSDP_WINDOW_SIZE);
535
536         if (MemRover)
537         {
538             /* Found it, return the physical address */
539
540             PhysAddr = ACPI_LO_RSDP_WINDOW_BASE;
541             PhysAddr += ACPI_PTR_DIFF (MemRover,TablePtr);
542
543             TableInfo->PhysicalAddress = PhysAddr;
544             return_ACPI_STATUS (AE_OK);
545         }
546
547         /*
548          * 2) Search upper memory: 16-byte boundaries in E0000h-F0000h
549          */
550         Status = AcpiOsMapMemory ((UINT64) ACPI_HI_RSDP_WINDOW_BASE, ACPI_HI_RSDP_WINDOW_SIZE,
551                                     (void **) &TablePtr);
552         if (ACPI_FAILURE (Status))
553         {
554             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not map memory at %X for length %X\n",
555                 ACPI_HI_RSDP_WINDOW_BASE, ACPI_HI_RSDP_WINDOW_SIZE));
556             return_ACPI_STATUS (Status);
557         }
558
559         MemRover = AcpiTbScanMemoryForRsdp (TablePtr, ACPI_HI_RSDP_WINDOW_SIZE);
560         AcpiOsUnmapMemory (TablePtr, ACPI_HI_RSDP_WINDOW_SIZE);
561
562         if (MemRover)
563         {
564             /* Found it, return the physical address */
565
566             PhysAddr = ACPI_HI_RSDP_WINDOW_BASE;
567             PhysAddr += ACPI_PTR_DIFF (MemRover, TablePtr);
568
569             TableInfo->PhysicalAddress = PhysAddr;
570             return_ACPI_STATUS (AE_OK);
571         }
572     }
573
574     /*
575      * Physical addressing
576      */
577     else
578     {
579         /*
580          * 1) Search EBDA (low memory) paragraphs
581          */
582         MemRover = AcpiTbScanMemoryForRsdp (ACPI_PHYSADDR_TO_PTR (ACPI_LO_RSDP_WINDOW_BASE),
583                         ACPI_LO_RSDP_WINDOW_SIZE);
584         if (MemRover)
585         {
586             /* Found it, return the physical address */
587
588             TableInfo->PhysicalAddress = ACPI_TO_INTEGER (MemRover);
589             return_ACPI_STATUS (AE_OK);
590         }
591
592         /*
593          * 2) Search upper memory: 16-byte boundaries in E0000h-F0000h
594          */
595         MemRover = AcpiTbScanMemoryForRsdp (ACPI_PHYSADDR_TO_PTR (ACPI_HI_RSDP_WINDOW_BASE),
596                         ACPI_HI_RSDP_WINDOW_SIZE);
597         if (MemRover)
598         {
599             /* Found it, return the physical address */
600
601             TableInfo->PhysicalAddress = ACPI_TO_INTEGER (MemRover);
602             return_ACPI_STATUS (AE_OK);
603         }
604     }
605
606     /* RSDP signature was not found */
607
608     return_ACPI_STATUS (AE_NOT_FOUND);
609 }
610
611 #endif
612