1 /******************************************************************************
3 * Module Name: examples - Example ACPICA initialization and execution code
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
46 #define _COMPONENT ACPI_EXAMPLE
47 ACPI_MODULE_NAME ("examples")
50 /******************************************************************************
54 * This module contains examples of how the host OS should interface to the
57 * 1) How to use the platform/acenv.h file and how to set configuration
60 * 2) main - using the debug output mechanism and the error/warning output
63 * 3) Two examples of the ACPICA initialization sequence. The first is a
64 * initialization with no "early" ACPI table access. The second shows
65 * how to use ACPICA to obtain the tables very early during kernel
66 * initialization, even before dynamic memory is available.
68 * 4) How to invoke a control method, including argument setup and how to
69 * access the return value.
71 *****************************************************************************/
74 /* Local Prototypes */
77 InitializeFullAcpica (void);
80 InstallHandlers (void);
91 ACPI_PHYSICAL_ADDRESS Address,
99 ACPI_HANDLE RegionHandle,
101 void *HandlerContext,
102 void **RegionContext);
111 InitializeAcpiTables (
119 /******************************************************************************
123 * PARAMETERS: argc, argv
127 * DESCRIPTION: Main routine. Shows the use of the various output macros, as
128 * well as the use of the debug layer/level globals.
130 *****************************************************************************/
132 int ACPI_SYSTEM_XFACE
138 ACPI_DEBUG_INITIALIZE (); /* For debug version only */
140 printf (ACPI_COMMON_SIGNON ("ACPI Example Code"));
142 /* Initialize the local ACPI tables (RSDP/RSDT/XSDT/FADT/DSDT/FACS) */
144 ExInitializeAcpiTables ();
146 /* Initialize the ACPICA subsystem */
148 InitializeFullAcpica ();
150 /* Example warning and error output */
152 ACPI_INFO (("Example ACPICA info message"));
153 ACPI_WARNING ((AE_INFO, "Example ACPICA warning message"));
154 ACPI_ERROR ((AE_INFO, "Example ACPICA error message"));
155 ACPI_EXCEPTION ((AE_INFO, AE_AML_OPERAND_TYPE,
156 "Example ACPICA exception message"));
164 /******************************************************************************
166 * Example ACPICA initialization code. This shows a full initialization with
167 * no early ACPI table access.
169 *****************************************************************************/
172 InitializeFullAcpica (void)
177 /* Initialize the ACPICA subsystem */
179 Status = AcpiInitializeSubsystem ();
180 if (ACPI_FAILURE (Status))
182 ACPI_EXCEPTION ((AE_INFO, Status, "While initializing ACPICA"));
186 /* Initialize the ACPICA Table Manager and get all ACPI tables */
188 ACPI_INFO (("Loading ACPI tables"));
190 Status = AcpiInitializeTables (NULL, 16, FALSE);
191 if (ACPI_FAILURE (Status))
193 ACPI_EXCEPTION ((AE_INFO, Status, "While initializing Table Manager"));
197 /* Install local handlers */
199 Status = InstallHandlers ();
200 if (ACPI_FAILURE (Status))
202 ACPI_EXCEPTION ((AE_INFO, Status, "While installing handlers"));
206 /* Initialize the ACPI hardware */
208 Status = AcpiEnableSubsystem (ACPI_FULL_INITIALIZATION);
209 if (ACPI_FAILURE (Status))
211 ACPI_EXCEPTION ((AE_INFO, Status, "While enabling ACPICA"));
215 /* Create the ACPI namespace from ACPI tables */
217 Status = AcpiLoadTables ();
218 if (ACPI_FAILURE (Status))
220 ACPI_EXCEPTION ((AE_INFO, Status, "While loading ACPI tables"));
224 /* Complete the ACPI namespace object initialization */
226 Status = AcpiInitializeObjects (ACPI_FULL_INITIALIZATION);
227 if (ACPI_FAILURE (Status))
229 ACPI_EXCEPTION ((AE_INFO, Status, "While initializing ACPICA objects"));
237 /******************************************************************************
239 * Example ACPICA initialization code with early ACPI table access. This shows
240 * an initialization that requires early access to ACPI tables (before
241 * kernel dynamic memory is available)
243 *****************************************************************************/
246 * The purpose of this static table array is to avoid the use of kernel
247 * dynamic memory which may not be available during early ACPI table
250 #define ACPI_MAX_INIT_TABLES 16
251 static ACPI_TABLE_DESC TableArray[ACPI_MAX_INIT_TABLES];
255 * This function would be called early in kernel initialization. After this
256 * is called, all ACPI tables are available to the host.
259 InitializeAcpiTables (
265 /* Initialize the ACPICA Table Manager and get all ACPI tables */
267 Status = AcpiInitializeTables (TableArray, ACPI_MAX_INIT_TABLES, TRUE);
273 * This function would be called after the kernel is initialized and
274 * dynamic/virtual memory is available. It completes the initialization of
275 * the ACPICA subsystem.
284 /* Initialize the ACPICA subsystem */
286 Status = AcpiInitializeSubsystem ();
287 if (ACPI_FAILURE (Status))
292 /* Copy the root table list to dynamic memory */
294 Status = AcpiReallocateRootTable ();
295 if (ACPI_FAILURE (Status))
300 /* Install local handlers */
302 Status = InstallHandlers ();
303 if (ACPI_FAILURE (Status))
305 ACPI_EXCEPTION ((AE_INFO, Status, "While installing handlers"));
309 /* Initialize the ACPI hardware */
311 Status = AcpiEnableSubsystem (ACPI_FULL_INITIALIZATION);
312 if (ACPI_FAILURE (Status))
317 /* Create the ACPI namespace from ACPI tables */
319 Status = AcpiLoadTables ();
320 if (ACPI_FAILURE (Status))
325 /* Complete the ACPI namespace object initialization */
327 Status = AcpiInitializeObjects (ACPI_FULL_INITIALIZATION);
328 if (ACPI_FAILURE (Status))
337 /******************************************************************************
339 * Example ACPICA handler and handler installation
341 *****************************************************************************/
350 ACPI_INFO (("Received a notify 0x%X", Value));
356 ACPI_HANDLE RegionHandle,
358 void *HandlerContext,
359 void **RegionContext)
362 if (Function == ACPI_REGION_DEACTIVATE)
364 *RegionContext = NULL;
368 *RegionContext = RegionHandle;
378 ACPI_PHYSICAL_ADDRESS Address,
381 void *HandlerContext,
385 ACPI_INFO (("Received a region access"));
392 InstallHandlers (void)
397 /* Install global notify handler */
399 Status = AcpiInstallNotifyHandler (ACPI_ROOT_OBJECT,
400 ACPI_SYSTEM_NOTIFY, NotifyHandler, NULL);
401 if (ACPI_FAILURE (Status))
403 ACPI_EXCEPTION ((AE_INFO, Status, "While installing Notify handler"));
407 Status = AcpiInstallAddressSpaceHandler (ACPI_ROOT_OBJECT,
408 ACPI_ADR_SPACE_SYSTEM_MEMORY, RegionHandler, RegionInit, NULL);
409 if (ACPI_FAILURE (Status))
411 ACPI_EXCEPTION ((AE_INFO, Status, "While installing an OpRegion handler"));
419 /******************************************************************************
421 * Examples of control method execution.
423 * _OSI is a predefined method that is implemented internally within ACPICA.
425 * Shows the following elements:
427 * 1) How to setup a control method argument and argument list
428 * 2) How to setup the return value object
429 * 3) How to invoke AcpiEvaluateObject
430 * 4) How to check the returned ACPI_STATUS
431 * 5) How to analyze the return value
433 *****************************************************************************/
439 ACPI_OBJECT_LIST ArgList;
441 ACPI_BUFFER ReturnValue;
445 ACPI_INFO (("Executing _OSI reserved method"));
447 /* Setup input argument */
450 ArgList.Pointer = Arg;
452 Arg[0].Type = ACPI_TYPE_STRING;
453 Arg[0].String.Pointer = "Windows 2001";
454 Arg[0].String.Length = strlen (Arg[0].String.Pointer);
456 /* Ask ACPICA to allocate space for the return object */
458 ReturnValue.Length = ACPI_ALLOCATE_BUFFER;
460 Status = AcpiEvaluateObject (NULL, "\\_OSI", &ArgList, &ReturnValue);
461 if (ACPI_FAILURE (Status))
463 ACPI_EXCEPTION ((AE_INFO, Status, "While executing _OSI"));
467 /* Ensure that the return object is large enough */
469 if (ReturnValue.Length < sizeof (ACPI_OBJECT))
471 AcpiOsPrintf ("Return value from _OSI method too small, %.8X\n",
476 /* Expect an integer return value from execution of _OSI */
478 Object = ReturnValue.Pointer;
479 if (Object->Type != ACPI_TYPE_INTEGER)
481 AcpiOsPrintf ("Invalid return type from _OSI, %.2X\n", Object->Type);
484 ACPI_INFO (("_OSI returned 0x%8.8X",
485 (UINT32) Object->Integer.Value));
490 /* Free a buffer created via ACPI_ALLOCATE_BUFFER */
492 AcpiOsFree (ReturnValue.Pointer);
496 /******************************************************************************
498 * Execute an actual control method in the DSDT (MAIN)
500 *****************************************************************************/
506 ACPI_OBJECT_LIST ArgList;
508 ACPI_BUFFER ReturnValue;
512 ACPI_INFO (("Executing MAIN method"));
514 /* Setup input argument */
517 ArgList.Pointer = Arg;
519 Arg[0].Type = ACPI_TYPE_STRING;
520 Arg[0].String.Pointer = "Method [MAIN] is executing";
521 Arg[0].String.Length = strlen (Arg[0].String.Pointer);
523 /* Ask ACPICA to allocate space for the return object */
525 ReturnValue.Length = ACPI_ALLOCATE_BUFFER;
527 Status = AcpiEvaluateObject (NULL, "\\MAIN", &ArgList, &ReturnValue);
528 if (ACPI_FAILURE (Status))
530 ACPI_EXCEPTION ((AE_INFO, Status, "While executing MAIN"));
534 if (ReturnValue.Pointer)
536 /* Obtain and validate the returned ACPI_OBJECT */
538 Object = ReturnValue.Pointer;
539 if (Object->Type == ACPI_TYPE_STRING)
541 AcpiOsPrintf ("Method [MAIN] returned: \"%s\"\n",
542 Object->String.Pointer);
545 ACPI_FREE (ReturnValue.Pointer);