Bring in acpica-unix-20031203. As with other contrib imports, this import
[dragonfly.git] / sys / contrib / dev / acpica-unix-20031203 / tables / tbget.c
1 /******************************************************************************
2  *
3  * Module Name: tbget - ACPI Table get* routines
4  *              $Revision: 87 $
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
117 #define __TBGET_C__
118
119 #include "acpi.h"
120 #include "actables.h"
121
122
123 #define _COMPONENT          ACPI_TABLES
124         ACPI_MODULE_NAME    ("tbget")
125
126
127 /*******************************************************************************
128  *
129  * FUNCTION:    AcpiTbGetTable
130  *
131  * PARAMETERS:  Address             - Address of table to retrieve.  Can be
132  *                                    Logical or Physical
133  *              TableInfo           - Where table info is returned
134  *
135  * RETURN:      None
136  *
137  * DESCRIPTION: Get entire table of unknown size.
138  *
139  ******************************************************************************/
140
141 ACPI_STATUS
142 AcpiTbGetTable (
143     ACPI_POINTER            *Address,
144     ACPI_TABLE_DESC         *TableInfo)
145 {
146     ACPI_STATUS             Status;
147     ACPI_TABLE_HEADER       Header;
148
149
150     ACPI_FUNCTION_TRACE ("TbGetTable");
151
152
153     /*
154      * Get the header in order to get signature and table size
155      */
156     Status = AcpiTbGetTableHeader (Address, &Header);
157     if (ACPI_FAILURE (Status))
158     {
159         return_ACPI_STATUS (Status);
160     }
161
162     /* Get the entire table */
163
164     Status = AcpiTbGetTableBody (Address, &Header, TableInfo);
165     if (ACPI_FAILURE (Status))
166     {
167         ACPI_REPORT_ERROR (("Could not get ACPI table (size %X), %s\n",
168             Header.Length, AcpiFormatException (Status)));
169         return_ACPI_STATUS (Status);
170     }
171
172     return_ACPI_STATUS (AE_OK);
173 }
174
175
176 /*******************************************************************************
177  *
178  * FUNCTION:    AcpiTbGetTableHeader
179  *
180  * PARAMETERS:  Address             - Address of table to retrieve.  Can be
181  *                                    Logical or Physical
182  *              ReturnHeader        - Where the table header is returned
183  *
184  * RETURN:      Status
185  *
186  * DESCRIPTION: Get an ACPI table header.  Works in both physical or virtual
187  *              addressing mode.  Works with both physical or logical pointers.
188  *              Table is either copied or mapped, depending on the pointer
189  *              type and mode of the processor.
190  *
191  ******************************************************************************/
192
193 ACPI_STATUS
194 AcpiTbGetTableHeader (
195     ACPI_POINTER            *Address,
196     ACPI_TABLE_HEADER       *ReturnHeader)
197 {
198     ACPI_STATUS             Status = AE_OK;
199     ACPI_TABLE_HEADER       *Header = NULL;
200
201
202     ACPI_FUNCTION_TRACE ("TbGetTableHeader");
203
204
205     /*
206      * Flags contains the current processor mode (Virtual or Physical addressing)
207      * The PointerType is either Logical or Physical
208      */
209     switch (Address->PointerType)
210     {
211     case ACPI_PHYSMODE_PHYSPTR:
212     case ACPI_LOGMODE_LOGPTR:
213
214         /* Pointer matches processor mode, copy the header */
215
216         ACPI_MEMCPY (ReturnHeader, Address->Pointer.Logical, sizeof (ACPI_TABLE_HEADER));
217         break;
218
219
220     case ACPI_LOGMODE_PHYSPTR:
221
222         /* Create a logical address for the physical pointer*/
223
224         Status = AcpiOsMapMemory (Address->Pointer.Physical, sizeof (ACPI_TABLE_HEADER),
225                                     (void *) &Header);
226         if (ACPI_FAILURE (Status))
227         {
228             ACPI_REPORT_ERROR (("Could not map memory at %8.8X%8.8X for length %X\n",
229                 ACPI_FORMAT_UINT64 (Address->Pointer.Physical),
230                 sizeof (ACPI_TABLE_HEADER)));
231             return_ACPI_STATUS (Status);
232         }
233
234         /* Copy header and delete mapping */
235
236         ACPI_MEMCPY (ReturnHeader, Header, sizeof (ACPI_TABLE_HEADER));
237         AcpiOsUnmapMemory (Header, sizeof (ACPI_TABLE_HEADER));
238         break;
239
240
241     default:
242
243         ACPI_REPORT_ERROR (("Invalid address flags %X\n",
244             Address->PointerType));
245         return_ACPI_STATUS (AE_BAD_PARAMETER);
246     }
247
248     return_ACPI_STATUS (AE_OK);
249 }
250
251
252 /*******************************************************************************
253  *
254  * FUNCTION:    AcpiTbGetTableBody
255  *
256  * PARAMETERS:  Address             - Address of table to retrieve.  Can be
257  *                                    Logical or Physical
258  *              Header              - Header of the table to retrieve
259  *              TableInfo           - Where the table info is returned
260  *
261  * RETURN:      Status
262  *
263  * DESCRIPTION: Get an entire ACPI table with support to allow the host OS to
264  *              replace the table with a newer version (table override.)
265  *              Works in both physical or virtual
266  *              addressing mode.  Works with both physical or logical pointers.
267  *              Table is either copied or mapped, depending on the pointer
268  *              type and mode of the processor.
269  *
270  ******************************************************************************/
271
272 ACPI_STATUS
273 AcpiTbGetTableBody (
274     ACPI_POINTER            *Address,
275     ACPI_TABLE_HEADER       *Header,
276     ACPI_TABLE_DESC         *TableInfo)
277 {
278     ACPI_STATUS             Status;
279
280
281     ACPI_FUNCTION_TRACE ("TbGetTableBody");
282
283
284     if (!TableInfo || !Address)
285     {
286         return_ACPI_STATUS (AE_BAD_PARAMETER);
287     }
288
289     /*
290      * Attempt table override.
291      */
292     Status = AcpiTbTableOverride (Header, TableInfo);
293     if (ACPI_SUCCESS (Status))
294     {
295         /* Table was overridden by the host OS */
296
297         return_ACPI_STATUS (Status);
298     }
299
300     /* No override, get the original table */
301
302     Status = AcpiTbGetThisTable (Address, Header, TableInfo);
303     return_ACPI_STATUS (Status);
304 }
305
306
307 /*******************************************************************************
308  *
309  * FUNCTION:    AcpiTbTableOverride
310  *
311  * PARAMETERS:  Header              - Pointer to table header
312  *              TableInfo           - Return info if table is overridden
313  *
314  * RETURN:      None
315  *
316  * DESCRIPTION: Attempts override of current table with a new one if provided
317  *              by the host OS.
318  *
319  ******************************************************************************/
320
321 ACPI_STATUS
322 AcpiTbTableOverride (
323     ACPI_TABLE_HEADER       *Header,
324     ACPI_TABLE_DESC         *TableInfo)
325 {
326     ACPI_TABLE_HEADER       *NewTable;
327     ACPI_STATUS             Status;
328     ACPI_POINTER            Address;
329
330
331     ACPI_FUNCTION_TRACE ("TbTableOverride");
332
333
334     /*
335      * The OSL will examine the header and decide whether to override this
336      * table.  If it decides to override, a table will be returned in NewTable,
337      * which we will then copy.
338      */
339     Status = AcpiOsTableOverride (Header, &NewTable);
340     if (ACPI_FAILURE (Status))
341     {
342         /* Some severe error from the OSL, but we basically ignore it */
343
344         ACPI_REPORT_ERROR (("Could not override ACPI table, %s\n",
345             AcpiFormatException (Status)));
346         return_ACPI_STATUS (Status);
347     }
348
349     if (!NewTable)
350     {
351         /* No table override */
352
353         return_ACPI_STATUS (AE_NO_ACPI_TABLES);
354     }
355
356     /*
357      * We have a new table to override the old one.  Get a copy of
358      * the new one.  We know that the new table has a logical pointer.
359      */
360     Address.PointerType     = ACPI_LOGICAL_POINTER | ACPI_LOGICAL_ADDRESSING;
361     Address.Pointer.Logical = NewTable;
362
363     Status = AcpiTbGetThisTable (&Address, NewTable, TableInfo);
364     if (ACPI_FAILURE (Status))
365     {
366         ACPI_REPORT_ERROR (("Could not copy override ACPI table, %s\n",
367             AcpiFormatException (Status)));
368         return_ACPI_STATUS (Status);
369     }
370
371     /* Copy the table info */
372
373     ACPI_REPORT_INFO (("Table [%4.4s] replaced by host OS\n",
374         TableInfo->Pointer->Signature));
375
376     return_ACPI_STATUS (AE_OK);
377 }
378
379
380 /*******************************************************************************
381  *
382  * FUNCTION:    AcpiTbGetThisTable
383  *
384  * PARAMETERS:  Address             - Address of table to retrieve.  Can be
385  *                                    Logical or Physical
386  *              Header              - Header of the table to retrieve
387  *              TableInfo           - Where the table info is returned
388  *
389  * RETURN:      Status
390  *
391  * DESCRIPTION: Get an entire ACPI table.  Works in both physical or virtual
392  *              addressing mode.  Works with both physical or logical pointers.
393  *              Table is either copied or mapped, depending on the pointer
394  *              type and mode of the processor.
395  *
396  ******************************************************************************/
397
398 ACPI_STATUS
399 AcpiTbGetThisTable (
400     ACPI_POINTER            *Address,
401     ACPI_TABLE_HEADER       *Header,
402     ACPI_TABLE_DESC         *TableInfo)
403 {
404     ACPI_TABLE_HEADER       *FullTable = NULL;
405     UINT8                   Allocation;
406     ACPI_STATUS             Status = AE_OK;
407
408
409     ACPI_FUNCTION_TRACE ("TbGetThisTable");
410
411
412     /*
413      * Flags contains the current processor mode (Virtual or Physical addressing)
414      * The PointerType is either Logical or Physical
415      */
416     switch (Address->PointerType)
417     {
418     case ACPI_PHYSMODE_PHYSPTR:
419     case ACPI_LOGMODE_LOGPTR:
420
421         /* Pointer matches processor mode, copy the table to a new buffer */
422
423         FullTable = ACPI_MEM_ALLOCATE (Header->Length);
424         if (!FullTable)
425         {
426             ACPI_REPORT_ERROR (("Could not allocate table memory for [%4.4s] length %X\n",
427                 Header->Signature, Header->Length));
428             return_ACPI_STATUS (AE_NO_MEMORY);
429         }
430
431         /* Copy the entire table (including header) to the local buffer */
432
433         ACPI_MEMCPY (FullTable, Address->Pointer.Logical, Header->Length);
434
435         /* Save allocation type */
436
437         Allocation = ACPI_MEM_ALLOCATED;
438         break;
439
440
441     case ACPI_LOGMODE_PHYSPTR:
442
443         /*
444          * Just map the table's physical memory
445          * into our address space.
446          */
447         Status = AcpiOsMapMemory (Address->Pointer.Physical, (ACPI_SIZE) Header->Length,
448                                     (void *) &FullTable);
449         if (ACPI_FAILURE (Status))
450         {
451             ACPI_REPORT_ERROR (("Could not map memory for table [%4.4s] at %8.8X%8.8X for length %X\n",
452                 Header->Signature,
453                 ACPI_FORMAT_UINT64 (Address->Pointer.Physical), Header->Length));
454             return (Status);
455         }
456
457         /* Save allocation type */
458
459         Allocation = ACPI_MEM_MAPPED;
460         break;
461
462
463     default:
464
465         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid address flags %X\n",
466             Address->PointerType));
467         return_ACPI_STATUS (AE_BAD_PARAMETER);
468     }
469
470     /*
471      * Validate checksum for _most_ tables,
472      * even the ones whose signature we don't recognize
473      */
474     if (TableInfo->Type != ACPI_TABLE_FACS)
475     {
476         Status = AcpiTbVerifyTableChecksum (FullTable);
477
478 #if (!ACPI_CHECKSUM_ABORT)
479         if (ACPI_FAILURE (Status))
480         {
481             /* Ignore the error if configuration says so */
482
483             Status = AE_OK;
484         }
485 #endif
486     }
487
488     /* Return values */
489
490     TableInfo->Pointer      = FullTable;
491     TableInfo->Length       = (ACPI_SIZE) Header->Length;
492     TableInfo->Allocation   = Allocation;
493
494     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
495         "Found table [%4.4s] at %8.8X%8.8X, mapped/copied to %p\n",
496         FullTable->Signature,
497         ACPI_FORMAT_UINT64 (Address->Pointer.Physical), FullTable));
498
499     return_ACPI_STATUS (Status);
500 }
501
502
503 /*******************************************************************************
504  *
505  * FUNCTION:    AcpiTbGetTablePtr
506  *
507  * PARAMETERS:  TableType       - one of the defined table types
508  *              Instance        - Which table of this type
509  *              TablePtrLoc     - pointer to location to place the pointer for
510  *                                return
511  *
512  * RETURN:      Status
513  *
514  * DESCRIPTION: This function is called to get the pointer to an ACPI table.
515  *
516  ******************************************************************************/
517
518 ACPI_STATUS
519 AcpiTbGetTablePtr (
520     ACPI_TABLE_TYPE         TableType,
521     UINT32                  Instance,
522     ACPI_TABLE_HEADER       **TablePtrLoc)
523 {
524     ACPI_TABLE_DESC         *TableDesc;
525     UINT32                  i;
526
527
528     ACPI_FUNCTION_TRACE ("TbGetTablePtr");
529
530
531     if (!AcpiGbl_DSDT)
532     {
533         return_ACPI_STATUS (AE_NO_ACPI_TABLES);
534     }
535
536     if (TableType > ACPI_TABLE_MAX)
537     {
538         return_ACPI_STATUS (AE_BAD_PARAMETER);
539     }
540
541     /*
542      * For all table types (Single/Multiple), the first
543      * instance is always in the list head.
544      */
545     if (Instance == 1)
546     {
547         /* Get the first */
548
549         *TablePtrLoc = NULL;
550         if (AcpiGbl_TableLists[TableType].Next)
551         {
552             *TablePtrLoc = AcpiGbl_TableLists[TableType].Next->Pointer;
553         }
554         return_ACPI_STATUS (AE_OK);
555     }
556
557     /*
558      * Check for instance out of range
559      */
560     if (Instance > AcpiGbl_TableLists[TableType].Count)
561     {
562         return_ACPI_STATUS (AE_NOT_EXIST);
563     }
564
565     /* Walk the list to get the desired table
566      * Since the if (Instance == 1) check above checked for the
567      * first table, setting TableDesc equal to the .Next member
568      * is actually pointing to the second table.  Therefore, we
569      * need to walk from the 2nd table until we reach the Instance
570      * that the user is looking for and return its table pointer.
571      */
572     TableDesc = AcpiGbl_TableLists[TableType].Next;
573     for (i = 2; i < Instance; i++)
574     {
575         TableDesc = TableDesc->Next;
576     }
577
578     /* We are now pointing to the requested table's descriptor */
579
580     *TablePtrLoc = TableDesc->Pointer;
581
582     return_ACPI_STATUS (AE_OK);
583 }
584