xref: /haiku/src/add-ons/kernel/bus_managers/acpi/ACPICAHaiku.cpp (revision 65ecbac0dc1c49a82a4acc238fc0c7e458e29594)
1775afec4SFredrik Holmqvist /******************************************************************************
2775afec4SFredrik Holmqvist  *
3775afec4SFredrik Holmqvist  * Module Name: oshaiku - Haiku OSL interfaces
4775afec4SFredrik Holmqvist  *
5775afec4SFredrik Holmqvist  *****************************************************************************/
6775afec4SFredrik Holmqvist 
7775afec4SFredrik Holmqvist /******************************************************************************
8775afec4SFredrik Holmqvist  *
9775afec4SFredrik Holmqvist  * 1. Copyright Notice
10775afec4SFredrik Holmqvist  *
11775afec4SFredrik Holmqvist  * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
12775afec4SFredrik Holmqvist  * All rights reserved.
13775afec4SFredrik Holmqvist  *
14775afec4SFredrik Holmqvist  * 2. License
15775afec4SFredrik Holmqvist  *
16775afec4SFredrik Holmqvist  * 2.1. This is your license from Intel Corp. under its intellectual property
17775afec4SFredrik Holmqvist  * rights.  You may have additional license terms from the party that provided
18775afec4SFredrik Holmqvist  * you this software, covering your right to use that party's intellectual
19775afec4SFredrik Holmqvist  * property rights.
20775afec4SFredrik Holmqvist  *
21775afec4SFredrik Holmqvist  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22775afec4SFredrik Holmqvist  * copy of the source code appearing in this file ("Covered Code") an
23775afec4SFredrik Holmqvist  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24775afec4SFredrik Holmqvist  * base code distributed originally by Intel ("Original Intel Code") to copy,
25775afec4SFredrik Holmqvist  * make derivatives, distribute, use and display any portion of the Covered
26775afec4SFredrik Holmqvist  * Code in any form, with the right to sublicense such rights; and
27775afec4SFredrik Holmqvist  *
28775afec4SFredrik Holmqvist  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29775afec4SFredrik Holmqvist  * license (with the right to sublicense), under only those claims of Intel
30775afec4SFredrik Holmqvist  * patents that are infringed by the Original Intel Code, to make, use, sell,
31775afec4SFredrik Holmqvist  * offer to sell, and import the Covered Code and derivative works thereof
32775afec4SFredrik Holmqvist  * solely to the minimum extent necessary to exercise the above copyright
33775afec4SFredrik Holmqvist  * license, and in no event shall the patent license extend to any additions
34775afec4SFredrik Holmqvist  * to or modifications of the Original Intel Code.  No other license or right
35775afec4SFredrik Holmqvist  * is granted directly or by implication, estoppel or otherwise;
36775afec4SFredrik Holmqvist  *
37775afec4SFredrik Holmqvist  * The above copyright and patent license is granted only if the following
38775afec4SFredrik Holmqvist  * conditions are met:
39775afec4SFredrik Holmqvist  *
40775afec4SFredrik Holmqvist  * 3. Conditions
41775afec4SFredrik Holmqvist  *
42775afec4SFredrik Holmqvist  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43775afec4SFredrik Holmqvist  * Redistribution of source code of any substantial portion of the Covered
44775afec4SFredrik Holmqvist  * Code or modification with rights to further distribute source must include
45775afec4SFredrik Holmqvist  * the above Copyright Notice, the above License, this list of Conditions,
46775afec4SFredrik Holmqvist  * and the following Disclaimer and Export Compliance provision.  In addition,
47775afec4SFredrik Holmqvist  * Licensee must cause all Covered Code to which Licensee contributes to
48775afec4SFredrik Holmqvist  * contain a file documenting the changes Licensee made to create that Covered
49775afec4SFredrik Holmqvist  * Code and the date of any change.  Licensee must include in that file the
50775afec4SFredrik Holmqvist  * documentation of any changes made by any predecessor Licensee.  Licensee
51775afec4SFredrik Holmqvist  * must include a prominent statement that the modification is derived,
52775afec4SFredrik Holmqvist  * directly or indirectly, from Original Intel Code.
53775afec4SFredrik Holmqvist  *
54775afec4SFredrik Holmqvist  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55775afec4SFredrik Holmqvist  * Redistribution of source code of any substantial portion of the Covered
56775afec4SFredrik Holmqvist  * Code or modification without rights to further distribute source must
57775afec4SFredrik Holmqvist  * include the following Disclaimer and Export Compliance provision in the
58775afec4SFredrik Holmqvist  * documentation and/or other materials provided with distribution.  In
59775afec4SFredrik Holmqvist  * addition, Licensee may not authorize further sublicense of source of any
60775afec4SFredrik Holmqvist  * portion of the Covered Code, and must include terms to the effect that the
61775afec4SFredrik Holmqvist  * license from Licensee to its licensee is limited to the intellectual
62775afec4SFredrik Holmqvist  * property embodied in the software Licensee provides to its licensee, and
63775afec4SFredrik Holmqvist  * not to intellectual property embodied in modifications its licensee may
64775afec4SFredrik Holmqvist  * make.
65775afec4SFredrik Holmqvist  *
66775afec4SFredrik Holmqvist  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67775afec4SFredrik Holmqvist  * substantial portion of the Covered Code or modification must reproduce the
68775afec4SFredrik Holmqvist  * above Copyright Notice, and the following Disclaimer and Export Compliance
69775afec4SFredrik Holmqvist  * provision in the documentation and/or other materials provided with the
70775afec4SFredrik Holmqvist  * distribution.
71775afec4SFredrik Holmqvist  *
72775afec4SFredrik Holmqvist  * 3.4. Intel retains all right, title, and interest in and to the Original
73775afec4SFredrik Holmqvist  * Intel Code.
74775afec4SFredrik Holmqvist  *
75775afec4SFredrik Holmqvist  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76775afec4SFredrik Holmqvist  * Intel shall be used in advertising or otherwise to promote the sale, use or
77775afec4SFredrik Holmqvist  * other dealings in products derived from or relating to the Covered Code
78775afec4SFredrik Holmqvist  * without prior written authorization from Intel.
79775afec4SFredrik Holmqvist  *
80775afec4SFredrik Holmqvist  * 4. Disclaimer and Export Compliance
81775afec4SFredrik Holmqvist  *
82775afec4SFredrik Holmqvist  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83775afec4SFredrik Holmqvist  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84775afec4SFredrik Holmqvist  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
85775afec4SFredrik Holmqvist  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
86775afec4SFredrik Holmqvist  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
87775afec4SFredrik Holmqvist  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88775afec4SFredrik Holmqvist  * PARTICULAR PURPOSE.
89775afec4SFredrik Holmqvist  *
90775afec4SFredrik Holmqvist  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91775afec4SFredrik Holmqvist  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92775afec4SFredrik Holmqvist  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93775afec4SFredrik Holmqvist  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94775afec4SFredrik Holmqvist  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95775afec4SFredrik Holmqvist  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
96775afec4SFredrik Holmqvist  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97775afec4SFredrik Holmqvist  * LIMITED REMEDY.
98775afec4SFredrik Holmqvist  *
99775afec4SFredrik Holmqvist  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100775afec4SFredrik Holmqvist  * software or system incorporating such software without first obtaining any
101775afec4SFredrik Holmqvist  * required license or other approval from the U. S. Department of Commerce or
102775afec4SFredrik Holmqvist  * any other agency or department of the United States Government.  In the
103775afec4SFredrik Holmqvist  * event Licensee exports any such software from the United States or
104775afec4SFredrik Holmqvist  * re-exports any such software from a foreign destination, Licensee shall
105775afec4SFredrik Holmqvist  * ensure that the distribution and export/re-export of the software is in
106775afec4SFredrik Holmqvist  * compliance with all laws, regulations, orders, or other restrictions of the
107775afec4SFredrik Holmqvist  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108775afec4SFredrik Holmqvist  * any of its subsidiaries will export/re-export any technical data, process,
109775afec4SFredrik Holmqvist  * software, or service, directly or indirectly, to any country for which the
110775afec4SFredrik Holmqvist  * United States government or any agency thereof requires an export license,
111775afec4SFredrik Holmqvist  * other governmental approval, or letter of assurance, without first obtaining
112775afec4SFredrik Holmqvist  * such license, approval or letter.
113775afec4SFredrik Holmqvist  *
114775afec4SFredrik Holmqvist  *****************************************************************************/
115775afec4SFredrik Holmqvist 
116775afec4SFredrik Holmqvist 
117775afec4SFredrik Holmqvist #include <stdio.h>
118775afec4SFredrik Holmqvist #include <sys/cdefs.h>
119775afec4SFredrik Holmqvist #include <time.h>
120775afec4SFredrik Holmqvist #include <unistd.h>
121775afec4SFredrik Holmqvist 
122775afec4SFredrik Holmqvist #include <OS.h>
123775afec4SFredrik Holmqvist 
124775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
125775afec4SFredrik Holmqvist #	include <KernelExport.h>
126775afec4SFredrik Holmqvist 
127775afec4SFredrik Holmqvist #	include <dpc.h>
128775afec4SFredrik Holmqvist #	include <PCI.h>
129775afec4SFredrik Holmqvist 
130775afec4SFredrik Holmqvist #	include <kernel.h>
131775afec4SFredrik Holmqvist #	include <vm/vm.h>
132775afec4SFredrik Holmqvist #endif
133775afec4SFredrik Holmqvist 
134775afec4SFredrik Holmqvist __BEGIN_DECLS
135775afec4SFredrik Holmqvist #include "acpi.h"
136775afec4SFredrik Holmqvist #include "accommon.h"
137775afec4SFredrik Holmqvist #include "amlcode.h"
138775afec4SFredrik Holmqvist #include "acparser.h"
139775afec4SFredrik Holmqvist #include "acdebug.h"
140775afec4SFredrik Holmqvist __END_DECLS
141775afec4SFredrik Holmqvist 
142775afec4SFredrik Holmqvist 
143775afec4SFredrik Holmqvist ACPI_MODULE_NAME("Haiku ACPI Module")
144775afec4SFredrik Holmqvist 
145775afec4SFredrik Holmqvist #define _COMPONENT ACPI_OS_SERVICES
146775afec4SFredrik Holmqvist 
147775afec4SFredrik Holmqvist // verbosity level 0 = off, 1 = normal, 2 = all
148775afec4SFredrik Holmqvist #define DEBUG_OSHAIKU 0
149775afec4SFredrik Holmqvist 
150775afec4SFredrik Holmqvist #if DEBUG_OSHAIKU <= 0
151775afec4SFredrik Holmqvist // No debugging, do nothing
152775afec4SFredrik Holmqvist #	define DEBUG_FUNCTION()
153775afec4SFredrik Holmqvist #	define DEBUG_FUNCTION_F(x, y...)
154775afec4SFredrik Holmqvist #	define DEBUG_FUNCTION_V()
155775afec4SFredrik Holmqvist #	define DEBUG_FUNCTION_VF(x, y...)
156775afec4SFredrik Holmqvist #else
157775afec4SFredrik Holmqvist #	define DEBUG_FUNCTION() \
158775afec4SFredrik Holmqvist 		dprintf("acpi[%ld]: %s\n", find_thread(NULL), __PRETTY_FUNCTION__);
159775afec4SFredrik Holmqvist #	define DEBUG_FUNCTION_F(x, y...) \
160775afec4SFredrik Holmqvist 		dprintf("acpi[%ld]: %s(" x ")\n", find_thread(NULL), __PRETTY_FUNCTION__, y);
161775afec4SFredrik Holmqvist #	if DEBUG_OSHAIKU == 1
162775afec4SFredrik Holmqvist // No verbose debugging, do nothing
163775afec4SFredrik Holmqvist #		define DEBUG_FUNCTION_V()
164775afec4SFredrik Holmqvist #		define DEBUG_FUNCTION_VF(x, y...)
165775afec4SFredrik Holmqvist #	else
166775afec4SFredrik Holmqvist // Full debugging
167775afec4SFredrik Holmqvist #		define DEBUG_FUNCTION_V() \
168775afec4SFredrik Holmqvist 			dprintf("acpi[%ld]: %s\n", find_thread(NULL), __PRETTY_FUNCTION__);
169775afec4SFredrik Holmqvist #		define DEBUG_FUNCTION_VF(x, y...) \
170775afec4SFredrik Holmqvist 			dprintf("acpi[%ld]: %s(" x ")\n", find_thread(NULL), __PRETTY_FUNCTION__, y);
171775afec4SFredrik Holmqvist #	endif
172775afec4SFredrik Holmqvist #endif
173775afec4SFredrik Holmqvist 
174775afec4SFredrik Holmqvist 
175775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
176775afec4SFredrik Holmqvist extern pci_module_info *gPCIManager;
177775afec4SFredrik Holmqvist extern dpc_module_info *gDPC;
178775afec4SFredrik Holmqvist extern void *gDPCHandle;
179775afec4SFredrik Holmqvist #endif
180775afec4SFredrik Holmqvist 
181775afec4SFredrik Holmqvist extern FILE *AcpiGbl_DebugFile;
182775afec4SFredrik Holmqvist FILE *AcpiGbl_OutputFile;
183775afec4SFredrik Holmqvist 
184775afec4SFredrik Holmqvist static uint32 sACPIRoot = 0;
185775afec4SFredrik Holmqvist static void *sInterruptHandlerData[32];
186775afec4SFredrik Holmqvist 
187775afec4SFredrik Holmqvist 
188775afec4SFredrik Holmqvist /******************************************************************************
189775afec4SFredrik Holmqvist  *
190775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsInitialize, AcpiOsTerminate
191775afec4SFredrik Holmqvist  *
192775afec4SFredrik Holmqvist  * PARAMETERS:  None
193775afec4SFredrik Holmqvist  *
194775afec4SFredrik Holmqvist  * RETURN:      Status
195775afec4SFredrik Holmqvist  *
196775afec4SFredrik Holmqvist  * DESCRIPTION: Init and terminate.  Nothing to do.
197775afec4SFredrik Holmqvist  *
198775afec4SFredrik Holmqvist  *****************************************************************************/
199775afec4SFredrik Holmqvist ACPI_STATUS
200775afec4SFredrik Holmqvist AcpiOsInitialize()
201775afec4SFredrik Holmqvist {
202775afec4SFredrik Holmqvist #ifndef _KERNEL_MODE
203775afec4SFredrik Holmqvist 	AcpiGbl_OutputFile = stdout;
204775afec4SFredrik Holmqvist #else
205775afec4SFredrik Holmqvist 	AcpiGbl_OutputFile = NULL;
206775afec4SFredrik Holmqvist #endif
207775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
208775afec4SFredrik Holmqvist 	return AE_OK;
209775afec4SFredrik Holmqvist }
210775afec4SFredrik Holmqvist 
211775afec4SFredrik Holmqvist 
212775afec4SFredrik Holmqvist ACPI_STATUS
213775afec4SFredrik Holmqvist AcpiOsTerminate()
214775afec4SFredrik Holmqvist {
215775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
216775afec4SFredrik Holmqvist 	return AE_OK;
217775afec4SFredrik Holmqvist }
218775afec4SFredrik Holmqvist 
219775afec4SFredrik Holmqvist 
220775afec4SFredrik Holmqvist /******************************************************************************
221775afec4SFredrik Holmqvist  *
222775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsGetRootPointer
223775afec4SFredrik Holmqvist  *
224775afec4SFredrik Holmqvist  * PARAMETERS:  None
225775afec4SFredrik Holmqvist  *
226775afec4SFredrik Holmqvist  * RETURN:      RSDP physical address
227775afec4SFredrik Holmqvist  *
228775afec4SFredrik Holmqvist  * DESCRIPTION: Gets the root pointer (RSDP)
229775afec4SFredrik Holmqvist  *
230775afec4SFredrik Holmqvist  *****************************************************************************/
231775afec4SFredrik Holmqvist ACPI_PHYSICAL_ADDRESS
232775afec4SFredrik Holmqvist AcpiOsGetRootPointer()
233775afec4SFredrik Holmqvist {
234775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
235*65ecbac0SFredrik Holmqvist 	ACPI_PHYSICAL_ADDRESS address;
236775afec4SFredrik Holmqvist 	ACPI_STATUS status;
237775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
238775afec4SFredrik Holmqvist 	if (sACPIRoot == 0) {
239775afec4SFredrik Holmqvist 		status = AcpiFindRootPointer(&address);
240775afec4SFredrik Holmqvist 		if (status == AE_OK)
241775afec4SFredrik Holmqvist 			sACPIRoot = address;
242775afec4SFredrik Holmqvist 	}
243775afec4SFredrik Holmqvist 	return sACPIRoot;
244775afec4SFredrik Holmqvist #else
245775afec4SFredrik Holmqvist 	return AeLocalGetRootPointer();
246775afec4SFredrik Holmqvist #endif
247775afec4SFredrik Holmqvist }
248775afec4SFredrik Holmqvist 
249775afec4SFredrik Holmqvist 
250775afec4SFredrik Holmqvist /******************************************************************************
251775afec4SFredrik Holmqvist  *
252775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsPredefinedOverride
253775afec4SFredrik Holmqvist  *
254775afec4SFredrik Holmqvist  * PARAMETERS:  initVal     - Initial value of the predefined object
255775afec4SFredrik Holmqvist  *              newVal      - The new value for the object
256775afec4SFredrik Holmqvist  *
257775afec4SFredrik Holmqvist  * RETURN:      Status, pointer to value.  Null pointer returned if not
258775afec4SFredrik Holmqvist  *              overriding.
259775afec4SFredrik Holmqvist  *
260775afec4SFredrik Holmqvist  * DESCRIPTION: Allow the OS to override predefined names
261775afec4SFredrik Holmqvist  *
262775afec4SFredrik Holmqvist  *****************************************************************************/
263775afec4SFredrik Holmqvist ACPI_STATUS
264775afec4SFredrik Holmqvist AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES *initVal,
265775afec4SFredrik Holmqvist 		ACPI_STRING *newVal)
266775afec4SFredrik Holmqvist {
267775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
268775afec4SFredrik Holmqvist 	if (!initVal || !newVal)
269775afec4SFredrik Holmqvist 		return AE_BAD_PARAMETER;
270775afec4SFredrik Holmqvist 
271775afec4SFredrik Holmqvist 	*newVal = NULL;
272775afec4SFredrik Holmqvist 	return AE_OK;
273775afec4SFredrik Holmqvist }
274775afec4SFredrik Holmqvist 
275775afec4SFredrik Holmqvist 
276775afec4SFredrik Holmqvist /******************************************************************************
277775afec4SFredrik Holmqvist  *
278775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsTableOverride
279775afec4SFredrik Holmqvist  *
280775afec4SFredrik Holmqvist  * PARAMETERS:  existingTable   - Header of current table (probably firmware)
281775afec4SFredrik Holmqvist  *              newTable        - Where an entire new table is returned.
282775afec4SFredrik Holmqvist  *
283775afec4SFredrik Holmqvist  * RETURN:      Status, pointer to new table.  Null pointer returned if no
284775afec4SFredrik Holmqvist  *              table is available to override
285775afec4SFredrik Holmqvist  *
286775afec4SFredrik Holmqvist  * DESCRIPTION: Return a different version of a table if one is available
287775afec4SFredrik Holmqvist  *
288775afec4SFredrik Holmqvist  *****************************************************************************/
289775afec4SFredrik Holmqvist ACPI_STATUS
290775afec4SFredrik Holmqvist AcpiOsTableOverride(ACPI_TABLE_HEADER *existingTable,
291775afec4SFredrik Holmqvist 		ACPI_TABLE_HEADER **newTable)
292775afec4SFredrik Holmqvist {
293775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
294775afec4SFredrik Holmqvist 	if (!existingTable || !newTable)
295775afec4SFredrik Holmqvist 		return AE_BAD_PARAMETER;
296775afec4SFredrik Holmqvist 
297775afec4SFredrik Holmqvist 	*newTable = NULL;
298775afec4SFredrik Holmqvist 
299775afec4SFredrik Holmqvist #ifdef ACPI_EXEC_APP
300775afec4SFredrik Holmqvist 	AeTableOverride(existingTable, newTable);
301775afec4SFredrik Holmqvist 	return AE_OK;
302775afec4SFredrik Holmqvist #else
303775afec4SFredrik Holmqvist 	return AE_NO_ACPI_TABLES;
304775afec4SFredrik Holmqvist #endif
305775afec4SFredrik Holmqvist }
306775afec4SFredrik Holmqvist 
307775afec4SFredrik Holmqvist 
308775afec4SFredrik Holmqvist /******************************************************************************
309775afec4SFredrik Holmqvist  *
310775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsPhysicalTableOverride
311775afec4SFredrik Holmqvist  *
312775afec4SFredrik Holmqvist  * PARAMETERS:  existingTable       - Header of current table (probably firmware)
313775afec4SFredrik Holmqvist  *              newAddress          - Where new table address is returned
314775afec4SFredrik Holmqvist  *                                    (Physical address)
315775afec4SFredrik Holmqvist  *              newTableLength      - Where new table length is returned
316775afec4SFredrik Holmqvist  *
317775afec4SFredrik Holmqvist  * RETURN:      Status, address/length of new table. Null pointer returned
318775afec4SFredrik Holmqvist  *              if no table is available to override.
319775afec4SFredrik Holmqvist  *
320775afec4SFredrik Holmqvist  * DESCRIPTION: Returns AE_SUPPORT, function not used in user space.
321775afec4SFredrik Holmqvist  *
322775afec4SFredrik Holmqvist  *****************************************************************************/
323775afec4SFredrik Holmqvist 
324775afec4SFredrik Holmqvist ACPI_STATUS
325775afec4SFredrik Holmqvist AcpiOsPhysicalTableOverride(ACPI_TABLE_HEADER *existingTable,
326775afec4SFredrik Holmqvist 	ACPI_PHYSICAL_ADDRESS *newAddress, UINT32 *newTableLength)
327775afec4SFredrik Holmqvist {
328775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
329775afec4SFredrik Holmqvist     return (AE_SUPPORT);
330775afec4SFredrik Holmqvist }
331775afec4SFredrik Holmqvist 
332775afec4SFredrik Holmqvist 
333775afec4SFredrik Holmqvist /******************************************************************************
334775afec4SFredrik Holmqvist  *
335775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsRedirectOutput
336775afec4SFredrik Holmqvist  *
337775afec4SFredrik Holmqvist  * PARAMETERS:  destination         - An open file handle/pointer
338775afec4SFredrik Holmqvist  *
339775afec4SFredrik Holmqvist  * RETURN:      None
340775afec4SFredrik Holmqvist  *
341775afec4SFredrik Holmqvist  * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
342775afec4SFredrik Holmqvist  *
343775afec4SFredrik Holmqvist  *****************************************************************************/
344775afec4SFredrik Holmqvist void
345775afec4SFredrik Holmqvist AcpiOsRedirectOutput(void *destination)
346775afec4SFredrik Holmqvist {
347775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
348775afec4SFredrik Holmqvist 	AcpiGbl_OutputFile = (FILE*)destination;
349775afec4SFredrik Holmqvist }
350775afec4SFredrik Holmqvist 
351775afec4SFredrik Holmqvist 
352775afec4SFredrik Holmqvist /******************************************************************************
353775afec4SFredrik Holmqvist  *
354775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsPrintf
355775afec4SFredrik Holmqvist  *
356775afec4SFredrik Holmqvist  * PARAMETERS:  fmt, ...            Standard printf format
357775afec4SFredrik Holmqvist  *
358775afec4SFredrik Holmqvist  * RETURN:      None
359775afec4SFredrik Holmqvist  *
360775afec4SFredrik Holmqvist  * DESCRIPTION: Formatted output
361775afec4SFredrik Holmqvist  *
362775afec4SFredrik Holmqvist  *****************************************************************************/
363775afec4SFredrik Holmqvist void ACPI_INTERNAL_VAR_XFACE
364775afec4SFredrik Holmqvist AcpiOsPrintf(const char *fmt, ...)
365775afec4SFredrik Holmqvist {
366775afec4SFredrik Holmqvist 	va_list args;
367775afec4SFredrik Holmqvist 
368775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
369775afec4SFredrik Holmqvist 	va_start(args, fmt);
370775afec4SFredrik Holmqvist 	AcpiOsVprintf(fmt, args);
371775afec4SFredrik Holmqvist 	va_end(args);
372775afec4SFredrik Holmqvist }
373775afec4SFredrik Holmqvist 
374775afec4SFredrik Holmqvist 
375775afec4SFredrik Holmqvist /******************************************************************************
376775afec4SFredrik Holmqvist  *
377775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsVprintf
378775afec4SFredrik Holmqvist  *
379775afec4SFredrik Holmqvist  * PARAMETERS:  fmt                 Standard printf format
380775afec4SFredrik Holmqvist  *              args                Argument list
381775afec4SFredrik Holmqvist  *
382775afec4SFredrik Holmqvist  * RETURN:      None
383775afec4SFredrik Holmqvist  *
384775afec4SFredrik Holmqvist  * DESCRIPTION: Formatted output with argument list pointer
385775afec4SFredrik Holmqvist  *
386775afec4SFredrik Holmqvist  *****************************************************************************/
387775afec4SFredrik Holmqvist void
388775afec4SFredrik Holmqvist AcpiOsVprintf(const char *fmt, va_list args)
389775afec4SFredrik Holmqvist {
390775afec4SFredrik Holmqvist #ifndef _KERNEL_MODE
391775afec4SFredrik Holmqvist 	UINT8 flags;
392775afec4SFredrik Holmqvist 
393775afec4SFredrik Holmqvist 	flags = AcpiGbl_DbOutputFlags;
394775afec4SFredrik Holmqvist 	if (flags & ACPI_DB_REDIRECTABLE_OUTPUT) {
395775afec4SFredrik Holmqvist 		// Output is directable to either a file (if open) or the console
396775afec4SFredrik Holmqvist 		if (AcpiGbl_DebugFile) {
397775afec4SFredrik Holmqvist 			// Output file is open, send the output there
398775afec4SFredrik Holmqvist 			vfprintf(AcpiGbl_DebugFile, fmt, args);
399775afec4SFredrik Holmqvist 		} else {
400775afec4SFredrik Holmqvist 			// No redirection, send output to console (once only!)
401775afec4SFredrik Holmqvist 			flags |= ACPI_DB_CONSOLE_OUTPUT;
402775afec4SFredrik Holmqvist 		}
403775afec4SFredrik Holmqvist 	}
404775afec4SFredrik Holmqvist 
405775afec4SFredrik Holmqvist 	if (flags & ACPI_DB_CONSOLE_OUTPUT) {
406775afec4SFredrik Holmqvist 		vfprintf(AcpiGbl_OutputFile, fmt, args);
407775afec4SFredrik Holmqvist     }
408775afec4SFredrik Holmqvist #else
409775afec4SFredrik Holmqvist 	static char outputBuffer[1024];
410775afec4SFredrik Holmqvist 	vsnprintf(outputBuffer, 1024, fmt, args);
411775afec4SFredrik Holmqvist 	dprintf("%s", outputBuffer);
412775afec4SFredrik Holmqvist #endif
413775afec4SFredrik Holmqvist }
414775afec4SFredrik Holmqvist 
415775afec4SFredrik Holmqvist 
416775afec4SFredrik Holmqvist /******************************************************************************
417775afec4SFredrik Holmqvist  *
418775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsGetLine
419775afec4SFredrik Holmqvist  *
420775afec4SFredrik Holmqvist  * PARAMETERS:  fmt                 Standard printf format
421775afec4SFredrik Holmqvist  *              args                Argument list
422775afec4SFredrik Holmqvist  *
423775afec4SFredrik Holmqvist  * RETURN:      Actual bytes read
424775afec4SFredrik Holmqvist  *
425775afec4SFredrik Holmqvist  * DESCRIPTION: Formatted input with argument list pointer
426775afec4SFredrik Holmqvist  *
427775afec4SFredrik Holmqvist  *****************************************************************************/
428775afec4SFredrik Holmqvist UINT32
429775afec4SFredrik Holmqvist AcpiOsGetLine(char *buffer)
430775afec4SFredrik Holmqvist {
431775afec4SFredrik Holmqvist 	uint32 i = 0;
432775afec4SFredrik Holmqvist 
433775afec4SFredrik Holmqvist #ifndef _KERNEL_MODE
434775afec4SFredrik Holmqvist 	uint8 temp;
435775afec4SFredrik Holmqvist 
436775afec4SFredrik Holmqvist 	for (i = 0; ; i++) {
437775afec4SFredrik Holmqvist 		scanf("%1c", &temp);
438775afec4SFredrik Holmqvist 		if (!temp || temp == '\n')
439775afec4SFredrik Holmqvist 			break;
440775afec4SFredrik Holmqvist 
441775afec4SFredrik Holmqvist 		buffer[i] = temp;
442775afec4SFredrik Holmqvist 	}
443775afec4SFredrik Holmqvist #endif
444775afec4SFredrik Holmqvist 
445775afec4SFredrik Holmqvist 	buffer[i] = 0;
446775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("buffer: \"%s\"; result: %lu", buffer, i);
447775afec4SFredrik Holmqvist 	return i;
448775afec4SFredrik Holmqvist }
449775afec4SFredrik Holmqvist 
450775afec4SFredrik Holmqvist 
451775afec4SFredrik Holmqvist /******************************************************************************
452775afec4SFredrik Holmqvist  *
453775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsMapMemory
454775afec4SFredrik Holmqvist  *
455775afec4SFredrik Holmqvist  * PARAMETERS:  where               Physical address of memory to be mapped
456775afec4SFredrik Holmqvist  *              length              How much memory to map
457775afec4SFredrik Holmqvist  *
458775afec4SFredrik Holmqvist  * RETURN:      Pointer to mapped memory.  Null on error.
459775afec4SFredrik Holmqvist  *
460775afec4SFredrik Holmqvist  * DESCRIPTION: Map physical memory into caller's address space
461775afec4SFredrik Holmqvist  *
462775afec4SFredrik Holmqvist  *****************************************************************************/
463775afec4SFredrik Holmqvist void *
464775afec4SFredrik Holmqvist AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS where, ACPI_SIZE length)
465775afec4SFredrik Holmqvist {
466775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
467775afec4SFredrik Holmqvist 	void *there;
468775afec4SFredrik Holmqvist 	area_id area = map_physical_memory("acpi_physical_mem_area", where, length,
469775afec4SFredrik Holmqvist 		B_ANY_KERNEL_ADDRESS, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA, &there);
470775afec4SFredrik Holmqvist 
471775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("addr: 0x%08lx; length: %lu; mapped: %p; area: %ld",
472775afec4SFredrik Holmqvist 		(addr_t)where, (size_t)length, there, area);
473775afec4SFredrik Holmqvist 	if (area < 0) {
474775afec4SFredrik Holmqvist 		dprintf("ACPI: cannot map memory at 0x%08x, length %d\n", where, length);
475775afec4SFredrik Holmqvist 		return NULL;
476775afec4SFredrik Holmqvist 	}
477775afec4SFredrik Holmqvist 	return there;
478775afec4SFredrik Holmqvist #else
479775afec4SFredrik Holmqvist 	return NULL;
480775afec4SFredrik Holmqvist #endif
481775afec4SFredrik Holmqvist 
482775afec4SFredrik Holmqvist 	// return ACPI_TO_POINTER((ACPI_SIZE) where);
483775afec4SFredrik Holmqvist }
484775afec4SFredrik Holmqvist 
485775afec4SFredrik Holmqvist 
486775afec4SFredrik Holmqvist /******************************************************************************
487775afec4SFredrik Holmqvist  *
488775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsUnmapMemory
489775afec4SFredrik Holmqvist  *
490775afec4SFredrik Holmqvist  * PARAMETERS:  where               Logical address of memory to be unmapped
491775afec4SFredrik Holmqvist  *              length              How much memory to unmap
492775afec4SFredrik Holmqvist  *
493775afec4SFredrik Holmqvist  * RETURN:      None.
494775afec4SFredrik Holmqvist  *
495775afec4SFredrik Holmqvist  * DESCRIPTION: Delete a previously created mapping.  Where and Length must
496775afec4SFredrik Holmqvist  *              correspond to a previous mapping exactly.
497775afec4SFredrik Holmqvist  *
498775afec4SFredrik Holmqvist  *****************************************************************************/
499775afec4SFredrik Holmqvist void
500775afec4SFredrik Holmqvist AcpiOsUnmapMemory(void *where, ACPI_SIZE length)
501775afec4SFredrik Holmqvist {
502775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("mapped: %p; length: %lu", where, (size_t)length);
503775afec4SFredrik Holmqvist 	delete_area(area_for(where));
504775afec4SFredrik Holmqvist }
505775afec4SFredrik Holmqvist 
506775afec4SFredrik Holmqvist 
507775afec4SFredrik Holmqvist /******************************************************************************
508775afec4SFredrik Holmqvist  *
509775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsAllocate
510775afec4SFredrik Holmqvist  *
511775afec4SFredrik Holmqvist  * PARAMETERS:  size                Amount to allocate, in bytes
512775afec4SFredrik Holmqvist  *
513775afec4SFredrik Holmqvist  * RETURN:      Pointer to the new allocation.  Null on error.
514775afec4SFredrik Holmqvist  *
515775afec4SFredrik Holmqvist  * DESCRIPTION: Allocate memory.  Algorithm is dependent on the OS.
516775afec4SFredrik Holmqvist  *
517775afec4SFredrik Holmqvist  *****************************************************************************/
518775afec4SFredrik Holmqvist void *
519775afec4SFredrik Holmqvist AcpiOsAllocate(ACPI_SIZE size)
520775afec4SFredrik Holmqvist {
521775afec4SFredrik Holmqvist 	void *mem = (void *) malloc(size);
522775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_VF("result: %p", mem);
523775afec4SFredrik Holmqvist 	return mem;
524775afec4SFredrik Holmqvist }
525775afec4SFredrik Holmqvist 
526775afec4SFredrik Holmqvist 
527775afec4SFredrik Holmqvist /******************************************************************************
528775afec4SFredrik Holmqvist  *
529775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsFree
530775afec4SFredrik Holmqvist  *
531775afec4SFredrik Holmqvist  * PARAMETERS:  mem                 Pointer to previously allocated memory
532775afec4SFredrik Holmqvist  *
533775afec4SFredrik Holmqvist  * RETURN:      None.
534775afec4SFredrik Holmqvist  *
535775afec4SFredrik Holmqvist  * DESCRIPTION: Free memory allocated via AcpiOsAllocate
536775afec4SFredrik Holmqvist  *
537775afec4SFredrik Holmqvist  *****************************************************************************/
538775afec4SFredrik Holmqvist void
539775afec4SFredrik Holmqvist AcpiOsFree(void *mem)
540775afec4SFredrik Holmqvist {
541775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_VF("mem: %p", mem);
542775afec4SFredrik Holmqvist 	free(mem);
543775afec4SFredrik Holmqvist }
544775afec4SFredrik Holmqvist 
545775afec4SFredrik Holmqvist 
546775afec4SFredrik Holmqvist /******************************************************************************
547775afec4SFredrik Holmqvist  *
548775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsCreateSemaphore
549775afec4SFredrik Holmqvist  *
550775afec4SFredrik Holmqvist  * PARAMETERS:  initialUnits        - Units to be assigned to the new semaphore
551775afec4SFredrik Holmqvist  *              outHandle           - Where a handle will be returned
552775afec4SFredrik Holmqvist  *
553775afec4SFredrik Holmqvist  * RETURN:      Status
554775afec4SFredrik Holmqvist  *
555775afec4SFredrik Holmqvist  * DESCRIPTION: Create an OS semaphore
556775afec4SFredrik Holmqvist  *
557775afec4SFredrik Holmqvist  *****************************************************************************/
558775afec4SFredrik Holmqvist ACPI_STATUS
559775afec4SFredrik Holmqvist AcpiOsCreateSemaphore(UINT32 maxUnits, UINT32 initialUnits,
560775afec4SFredrik Holmqvist 		ACPI_SEMAPHORE *outHandle)
561775afec4SFredrik Holmqvist {
562775afec4SFredrik Holmqvist 	if (!outHandle)
563775afec4SFredrik Holmqvist     	return AE_BAD_PARAMETER;
564775afec4SFredrik Holmqvist 
565775afec4SFredrik Holmqvist 	*outHandle = create_sem(initialUnits, "acpi_sem");
566775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("max: %lu; count: %lu; result: %ld",
567775afec4SFredrik Holmqvist 		maxUnits, initialUnits, *outHandle);
568775afec4SFredrik Holmqvist 
569775afec4SFredrik Holmqvist 	if (*outHandle >= B_OK)
570775afec4SFredrik Holmqvist 		return AE_OK;
571775afec4SFredrik Holmqvist 
572775afec4SFredrik Holmqvist 	return *outHandle == B_BAD_VALUE ? AE_BAD_PARAMETER : AE_NO_MEMORY;
573775afec4SFredrik Holmqvist }
574775afec4SFredrik Holmqvist 
575775afec4SFredrik Holmqvist 
576775afec4SFredrik Holmqvist /******************************************************************************
577775afec4SFredrik Holmqvist  *
578775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsDeleteSemaphore
579775afec4SFredrik Holmqvist  *
580775afec4SFredrik Holmqvist  * PARAMETERS:  handle              - Handle returned by AcpiOsCreateSemaphore
581775afec4SFredrik Holmqvist  *
582775afec4SFredrik Holmqvist  * RETURN:      Status
583775afec4SFredrik Holmqvist  *
584775afec4SFredrik Holmqvist  * DESCRIPTION: Delete an OS semaphore
585775afec4SFredrik Holmqvist  *
586775afec4SFredrik Holmqvist  *****************************************************************************/
587775afec4SFredrik Holmqvist ACPI_STATUS
588775afec4SFredrik Holmqvist AcpiOsDeleteSemaphore(ACPI_SEMAPHORE handle)
589775afec4SFredrik Holmqvist {
590775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("sem: %ld", handle);
591775afec4SFredrik Holmqvist 	return delete_sem(handle) == B_OK ? AE_OK : AE_BAD_PARAMETER;
592775afec4SFredrik Holmqvist }
593775afec4SFredrik Holmqvist 
594775afec4SFredrik Holmqvist 
595775afec4SFredrik Holmqvist /******************************************************************************
596775afec4SFredrik Holmqvist  *
597775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsWaitSemaphore
598775afec4SFredrik Holmqvist  *
599775afec4SFredrik Holmqvist  * PARAMETERS:  handle              - Handle returned by AcpiOsCreateSemaphore
600775afec4SFredrik Holmqvist  *              units               - How many units to wait for
601775afec4SFredrik Holmqvist  *              timeout             - How long to wait
602775afec4SFredrik Holmqvist  *
603775afec4SFredrik Holmqvist  * RETURN:      Status
604775afec4SFredrik Holmqvist  *
605775afec4SFredrik Holmqvist  * DESCRIPTION: Wait for units
606775afec4SFredrik Holmqvist  *
607775afec4SFredrik Holmqvist  *****************************************************************************/
608775afec4SFredrik Holmqvist ACPI_STATUS
609775afec4SFredrik Holmqvist AcpiOsWaitSemaphore(ACPI_SEMAPHORE handle, UINT32 units, UINT16 timeout)
610775afec4SFredrik Holmqvist {
611775afec4SFredrik Holmqvist 	ACPI_STATUS result = AE_OK;
612775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_VF("sem: %ld; count: %lu; timeout: %u",
613775afec4SFredrik Holmqvist 		handle, units, timeout);
614775afec4SFredrik Holmqvist 
615775afec4SFredrik Holmqvist 	if (timeout == ACPI_WAIT_FOREVER) {
616775afec4SFredrik Holmqvist 		result = acquire_sem_etc(handle, units, 0, 0)
617775afec4SFredrik Holmqvist 			== B_OK ? AE_OK : AE_BAD_PARAMETER;
618775afec4SFredrik Holmqvist 	} else {
619775afec4SFredrik Holmqvist 		switch (acquire_sem_etc(handle, units, B_RELATIVE_TIMEOUT,
620775afec4SFredrik Holmqvist 			(bigtime_t)timeout * 1000)) {
621775afec4SFredrik Holmqvist 			case B_OK:
622775afec4SFredrik Holmqvist 				result = AE_OK;
623775afec4SFredrik Holmqvist 				break;
624775afec4SFredrik Holmqvist 			case B_INTERRUPTED:
625775afec4SFredrik Holmqvist 			case B_TIMED_OUT:
626775afec4SFredrik Holmqvist 			case B_WOULD_BLOCK:
627775afec4SFredrik Holmqvist 				result = AE_TIME;
628775afec4SFredrik Holmqvist 				break;
629775afec4SFredrik Holmqvist 			case B_BAD_VALUE:
630775afec4SFredrik Holmqvist 			default:
631775afec4SFredrik Holmqvist 				result = AE_BAD_PARAMETER;
632775afec4SFredrik Holmqvist 				break;
633775afec4SFredrik Holmqvist 		}
634775afec4SFredrik Holmqvist 	}
635775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_VF("sem: %ld; count: %lu; timeout: %u result: %lu",
636775afec4SFredrik Holmqvist 		handle, units, timeout, (uint32)result);
637775afec4SFredrik Holmqvist 	return result;
638775afec4SFredrik Holmqvist }
639775afec4SFredrik Holmqvist 
640775afec4SFredrik Holmqvist 
641775afec4SFredrik Holmqvist /******************************************************************************
642775afec4SFredrik Holmqvist  *
643775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsSignalSemaphore
644775afec4SFredrik Holmqvist  *
645775afec4SFredrik Holmqvist  * PARAMETERS:  handle              - Handle returned by AcpiOsCreateSemaphore
646775afec4SFredrik Holmqvist  *              units               - Number of units to send
647775afec4SFredrik Holmqvist  *
648775afec4SFredrik Holmqvist  * RETURN:      Status
649775afec4SFredrik Holmqvist  *
650775afec4SFredrik Holmqvist  * DESCRIPTION: Send units
651775afec4SFredrik Holmqvist  *
652775afec4SFredrik Holmqvist  *****************************************************************************/
653775afec4SFredrik Holmqvist ACPI_STATUS
654775afec4SFredrik Holmqvist AcpiOsSignalSemaphore(ACPI_SEMAPHORE handle, UINT32 units)
655775afec4SFredrik Holmqvist {
656775afec4SFredrik Holmqvist 	status_t result;
657775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_VF("sem: %ld; count: %lu", handle, units);
658775afec4SFredrik Holmqvist 	// We can be called from interrupt handler, so don't reschedule
659775afec4SFredrik Holmqvist 	result = release_sem_etc(handle, units, B_DO_NOT_RESCHEDULE);
660775afec4SFredrik Holmqvist 	return result == B_OK ? AE_OK : AE_BAD_PARAMETER;
661775afec4SFredrik Holmqvist }
662775afec4SFredrik Holmqvist 
663775afec4SFredrik Holmqvist 
664775afec4SFredrik Holmqvist /******************************************************************************
665775afec4SFredrik Holmqvist  *
666775afec4SFredrik Holmqvist  * FUNCTION:    Spinlock interfaces
667775afec4SFredrik Holmqvist  *
668775afec4SFredrik Holmqvist  * DESCRIPTION: Map these interfaces to semaphore interfaces
669775afec4SFredrik Holmqvist  *
670775afec4SFredrik Holmqvist  *****************************************************************************/
671775afec4SFredrik Holmqvist ACPI_STATUS
672775afec4SFredrik Holmqvist AcpiOsCreateLock(ACPI_SPINLOCK *outHandle)
673775afec4SFredrik Holmqvist {
674775afec4SFredrik Holmqvist 	*outHandle = (ACPI_SPINLOCK) malloc(sizeof(spinlock));
675775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("result: %p", *outHandle);
676775afec4SFredrik Holmqvist 	if (*outHandle == NULL)
677775afec4SFredrik Holmqvist 		return AE_NO_MEMORY;
678775afec4SFredrik Holmqvist 
6791ee1d0cbSPawel Dziepak 	B_INITIALIZE_SPINLOCK(*outHandle);
680775afec4SFredrik Holmqvist 	return AE_OK;
681775afec4SFredrik Holmqvist }
682775afec4SFredrik Holmqvist 
683775afec4SFredrik Holmqvist 
684775afec4SFredrik Holmqvist void
685775afec4SFredrik Holmqvist AcpiOsDeleteLock(ACPI_SPINLOCK handle)
686775afec4SFredrik Holmqvist {
687775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
688775afec4SFredrik Holmqvist 	free((void*)handle);
689775afec4SFredrik Holmqvist }
690775afec4SFredrik Holmqvist 
691775afec4SFredrik Holmqvist 
692775afec4SFredrik Holmqvist ACPI_CPU_FLAGS
693775afec4SFredrik Holmqvist AcpiOsAcquireLock(ACPI_SPINLOCK handle)
694775afec4SFredrik Holmqvist {
695775afec4SFredrik Holmqvist 	cpu_status cpu;
696775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("spinlock: %p", handle);
697775afec4SFredrik Holmqvist 	cpu = disable_interrupts();
698775afec4SFredrik Holmqvist 	acquire_spinlock(handle);
699775afec4SFredrik Holmqvist 	return cpu;
700775afec4SFredrik Holmqvist }
701775afec4SFredrik Holmqvist 
702775afec4SFredrik Holmqvist 
703775afec4SFredrik Holmqvist void
704775afec4SFredrik Holmqvist AcpiOsReleaseLock(ACPI_SPINLOCK handle, ACPI_CPU_FLAGS flags)
705775afec4SFredrik Holmqvist {
706775afec4SFredrik Holmqvist 	release_spinlock(handle);
707775afec4SFredrik Holmqvist 	restore_interrupts(flags);
708775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("spinlock: %p", handle);
709775afec4SFredrik Holmqvist }
710775afec4SFredrik Holmqvist 
711775afec4SFredrik Holmqvist 
712775afec4SFredrik Holmqvist /******************************************************************************
713775afec4SFredrik Holmqvist  *
714775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsInstallInterruptHandler
715775afec4SFredrik Holmqvist  *
716775afec4SFredrik Holmqvist  * PARAMETERS:  interruptNumber     Level handler should respond to.
717775afec4SFredrik Holmqvist  *              Isr                 Address of the ACPI interrupt handler
718775afec4SFredrik Holmqvist  *              ExceptPtr           Where status is returned
719775afec4SFredrik Holmqvist  *
720775afec4SFredrik Holmqvist  * RETURN:      Handle to the newly installed handler.
721775afec4SFredrik Holmqvist  *
722775afec4SFredrik Holmqvist  * DESCRIPTION: Install an interrupt handler.  Used to install the ACPI
723775afec4SFredrik Holmqvist  *              OS-independent handler.
724775afec4SFredrik Holmqvist  *
725775afec4SFredrik Holmqvist  *****************************************************************************/
726775afec4SFredrik Holmqvist UINT32
727775afec4SFredrik Holmqvist AcpiOsInstallInterruptHandler(UINT32 interruptNumber,
728775afec4SFredrik Holmqvist 		ACPI_OSD_HANDLER serviceRoutine, void *context)
729775afec4SFredrik Holmqvist {
730775afec4SFredrik Holmqvist 	status_t result;
731775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("vector: %lu; handler: %p context %p",
732775afec4SFredrik Holmqvist 		interruptNumber, serviceRoutine, context);
733775afec4SFredrik Holmqvist 
734775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
735775afec4SFredrik Holmqvist 	// It so happens that the Haiku and ACPI-CA interrupt handler routines
736775afec4SFredrik Holmqvist 	// return the same values with the same meanings
737775afec4SFredrik Holmqvist 	sInterruptHandlerData[interruptNumber] = context;
738775afec4SFredrik Holmqvist 	result = install_io_interrupt_handler(interruptNumber,
739775afec4SFredrik Holmqvist 		(interrupt_handler)serviceRoutine, context, 0);
740775afec4SFredrik Holmqvist 
741775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("vector: %lu; handler: %p context %p returned %d",
742775afec4SFredrik Holmqvist 		interruptNumber, serviceRoutine, context, result);
743775afec4SFredrik Holmqvist 
744775afec4SFredrik Holmqvist 	return result == B_OK ? AE_OK : AE_BAD_PARAMETER;
745775afec4SFredrik Holmqvist #else
746775afec4SFredrik Holmqvist 	return AE_BAD_PARAMETER;
747775afec4SFredrik Holmqvist #endif
748775afec4SFredrik Holmqvist }
749775afec4SFredrik Holmqvist 
750775afec4SFredrik Holmqvist 
751775afec4SFredrik Holmqvist /******************************************************************************
752775afec4SFredrik Holmqvist  *
753775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsRemoveInterruptHandler
754775afec4SFredrik Holmqvist  *
755775afec4SFredrik Holmqvist  * PARAMETERS:  Handle              Returned when handler was installed
756775afec4SFredrik Holmqvist  *
757775afec4SFredrik Holmqvist  * RETURN:      Status
758775afec4SFredrik Holmqvist  *
759775afec4SFredrik Holmqvist  * DESCRIPTION: Uninstalls an interrupt handler.
760775afec4SFredrik Holmqvist  *
761775afec4SFredrik Holmqvist  *****************************************************************************/
762775afec4SFredrik Holmqvist ACPI_STATUS
763775afec4SFredrik Holmqvist AcpiOsRemoveInterruptHandler(UINT32 interruptNumber,
764775afec4SFredrik Holmqvist 		ACPI_OSD_HANDLER serviceRoutine)
765775afec4SFredrik Holmqvist {
766775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("vector: %lu; handler: %p", interruptNumber,
767775afec4SFredrik Holmqvist 		serviceRoutine);
768775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
769775afec4SFredrik Holmqvist 	remove_io_interrupt_handler(interruptNumber,
770775afec4SFredrik Holmqvist 		(interrupt_handler) serviceRoutine,
771775afec4SFredrik Holmqvist 		sInterruptHandlerData[interruptNumber]);
772775afec4SFredrik Holmqvist 	return AE_OK;
773775afec4SFredrik Holmqvist #else
774775afec4SFredrik Holmqvist 	return AE_ERROR;
775775afec4SFredrik Holmqvist #endif
776775afec4SFredrik Holmqvist }
777775afec4SFredrik Holmqvist 
778775afec4SFredrik Holmqvist 
779775afec4SFredrik Holmqvist /******************************************************************************
780775afec4SFredrik Holmqvist  *
781775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsExecute
782775afec4SFredrik Holmqvist  *
783775afec4SFredrik Holmqvist  * PARAMETERS:  type            - Type of execution
784775afec4SFredrik Holmqvist  *              function        - Address of the function to execute
785775afec4SFredrik Holmqvist  *              context         - Passed as a parameter to the function
786775afec4SFredrik Holmqvist  *
787775afec4SFredrik Holmqvist  * RETURN:      Status.
788775afec4SFredrik Holmqvist  *
789775afec4SFredrik Holmqvist  * DESCRIPTION: Execute a new thread
790775afec4SFredrik Holmqvist  *
791775afec4SFredrik Holmqvist  *****************************************************************************/
792775afec4SFredrik Holmqvist ACPI_STATUS
793775afec4SFredrik Holmqvist AcpiOsExecute(ACPI_EXECUTE_TYPE type, ACPI_OSD_EXEC_CALLBACK  function,
794775afec4SFredrik Holmqvist 		void *context)
795775afec4SFredrik Holmqvist {
796775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
797775afec4SFredrik Holmqvist /* TODO: Prioritize urgent?
798775afec4SFredrik Holmqvist 	switch (type) {
799775afec4SFredrik Holmqvist 		case OSL_GLOBAL_LOCK_HANDLER:
800775afec4SFredrik Holmqvist 		case OSL_NOTIFY_HANDLER:
801775afec4SFredrik Holmqvist 		case OSL_GPE_HANDLER:
802775afec4SFredrik Holmqvist 		case OSL_DEBUGGER_THREAD:
803775afec4SFredrik Holmqvist 		case OSL_EC_POLL_HANDLER:
804775afec4SFredrik Holmqvist 		case OSL_EC_BURST_HANDLER:
805775afec4SFredrik Holmqvist 			break;
806775afec4SFredrik Holmqvist 	}
807775afec4SFredrik Holmqvist */
808775afec4SFredrik Holmqvist 
809775afec4SFredrik Holmqvist 	if (gDPC->queue_dpc(gDPCHandle, function, context) != B_OK) {
810775afec4SFredrik Holmqvist 		DEBUG_FUNCTION_F("Serious failure in AcpiOsExecute! function: %p",
811775afec4SFredrik Holmqvist 			function);
812775afec4SFredrik Holmqvist 		return AE_BAD_PARAMETER;
813775afec4SFredrik Holmqvist 	}
814775afec4SFredrik Holmqvist 	return AE_OK;
815775afec4SFredrik Holmqvist }
816775afec4SFredrik Holmqvist 
817775afec4SFredrik Holmqvist 
818775afec4SFredrik Holmqvist /******************************************************************************
819775afec4SFredrik Holmqvist  *
820775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsStall
821775afec4SFredrik Holmqvist  *
822775afec4SFredrik Holmqvist  * PARAMETERS:  microseconds        To sleep
823775afec4SFredrik Holmqvist  *
824775afec4SFredrik Holmqvist  * RETURN:      Blocks until sleep is completed.
825775afec4SFredrik Holmqvist  *
826775afec4SFredrik Holmqvist  * DESCRIPTION: Sleep at microsecond granularity
827775afec4SFredrik Holmqvist  *
828775afec4SFredrik Holmqvist  *****************************************************************************/
829775afec4SFredrik Holmqvist void
830775afec4SFredrik Holmqvist AcpiOsStall(UINT32 microseconds)
831775afec4SFredrik Holmqvist {
832775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("microseconds: %lu", microseconds);
833775afec4SFredrik Holmqvist 	if (microseconds)
834775afec4SFredrik Holmqvist 		spin(microseconds);
835775afec4SFredrik Holmqvist }
836775afec4SFredrik Holmqvist 
837775afec4SFredrik Holmqvist 
838775afec4SFredrik Holmqvist /******************************************************************************
839775afec4SFredrik Holmqvist  *
840775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsSleep
841775afec4SFredrik Holmqvist  *
842775afec4SFredrik Holmqvist  * PARAMETERS:  milliseconds        To sleep
843775afec4SFredrik Holmqvist  *
844775afec4SFredrik Holmqvist  * RETURN:      Blocks until sleep is completed.
845775afec4SFredrik Holmqvist  *
846775afec4SFredrik Holmqvist  * DESCRIPTION: Sleep at millisecond granularity
847775afec4SFredrik Holmqvist  *
848775afec4SFredrik Holmqvist  *****************************************************************************/
849775afec4SFredrik Holmqvist void
850775afec4SFredrik Holmqvist AcpiOsSleep(ACPI_INTEGER milliseconds)
851775afec4SFredrik Holmqvist {
852775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("milliseconds: %lu", milliseconds);
853775afec4SFredrik Holmqvist 	if (gKernelStartup)
854775afec4SFredrik Holmqvist 		spin(milliseconds * 1000);
855775afec4SFredrik Holmqvist 	else
856775afec4SFredrik Holmqvist 		snooze(milliseconds * 1000);
857775afec4SFredrik Holmqvist }
858775afec4SFredrik Holmqvist 
859775afec4SFredrik Holmqvist 
860775afec4SFredrik Holmqvist /******************************************************************************
861775afec4SFredrik Holmqvist  *
862775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsGetTimer
863775afec4SFredrik Holmqvist  *
864775afec4SFredrik Holmqvist  * PARAMETERS:  None
865775afec4SFredrik Holmqvist  *
866775afec4SFredrik Holmqvist  * RETURN:      Current time in 100 nanosecond units
867775afec4SFredrik Holmqvist  *
868775afec4SFredrik Holmqvist  * DESCRIPTION: Get the current system time
869775afec4SFredrik Holmqvist  *
870775afec4SFredrik Holmqvist  *****************************************************************************/
871775afec4SFredrik Holmqvist UINT64
872775afec4SFredrik Holmqvist AcpiOsGetTimer()
873775afec4SFredrik Holmqvist {
874775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
875775afec4SFredrik Holmqvist 	return system_time() * 10;
876775afec4SFredrik Holmqvist }
877775afec4SFredrik Holmqvist 
878775afec4SFredrik Holmqvist 
879775afec4SFredrik Holmqvist /******************************************************************************
880775afec4SFredrik Holmqvist  *
881775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsReadPciConfiguration
882775afec4SFredrik Holmqvist  *
883775afec4SFredrik Holmqvist  * PARAMETERS:  pciId               Seg/Bus/Dev
884775afec4SFredrik Holmqvist  *              reg                 Device Register
885775afec4SFredrik Holmqvist  *              value               Buffer where value is placed
886775afec4SFredrik Holmqvist  *              width               Number of bits
887775afec4SFredrik Holmqvist  *
888775afec4SFredrik Holmqvist  * RETURN:      Status
889775afec4SFredrik Holmqvist  *
890775afec4SFredrik Holmqvist  * DESCRIPTION: Read data from PCI configuration space
891775afec4SFredrik Holmqvist  *
892775afec4SFredrik Holmqvist  *****************************************************************************/
893775afec4SFredrik Holmqvist ACPI_STATUS
894775afec4SFredrik Holmqvist AcpiOsReadPciConfiguration(ACPI_PCI_ID *pciId, UINT32 reg, UINT64 *value,
895775afec4SFredrik Holmqvist 		UINT32 width)
896775afec4SFredrik Holmqvist {
897775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
898775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
899775afec4SFredrik Holmqvist 
900775afec4SFredrik Holmqvist 	switch (width) {
901775afec4SFredrik Holmqvist 		case 8:
902775afec4SFredrik Holmqvist 		case 16:
903775afec4SFredrik Holmqvist 		case 32:
904775afec4SFredrik Holmqvist 			*value = gPCIManager->read_pci_config(
905775afec4SFredrik Holmqvist 				pciId->Bus, pciId->Device, pciId->Function, reg, width / 8);
906775afec4SFredrik Holmqvist 			break;
907775afec4SFredrik Holmqvist 		default:
908775afec4SFredrik Holmqvist 			return AE_ERROR;
909775afec4SFredrik Holmqvist 	}
910775afec4SFredrik Holmqvist 	return AE_OK;
911775afec4SFredrik Holmqvist #else
912775afec4SFredrik Holmqvist 	return AE_ERROR;
913775afec4SFredrik Holmqvist #endif
914775afec4SFredrik Holmqvist }
915775afec4SFredrik Holmqvist 
916775afec4SFredrik Holmqvist 
917775afec4SFredrik Holmqvist /******************************************************************************
918775afec4SFredrik Holmqvist  *
919775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsWritePciConfiguration
920775afec4SFredrik Holmqvist  *
921775afec4SFredrik Holmqvist  * PARAMETERS:  pciId               Seg/Bus/Dev
922775afec4SFredrik Holmqvist  *              reg                 Device Register
923775afec4SFredrik Holmqvist  *              value               Value to be written
924775afec4SFredrik Holmqvist  *              width               Number of bits
925775afec4SFredrik Holmqvist  *
926775afec4SFredrik Holmqvist  * RETURN:      Status.
927775afec4SFredrik Holmqvist  *
928775afec4SFredrik Holmqvist  * DESCRIPTION: Write data to PCI configuration space
929775afec4SFredrik Holmqvist  *
930775afec4SFredrik Holmqvist  *****************************************************************************/
931775afec4SFredrik Holmqvist ACPI_STATUS
932775afec4SFredrik Holmqvist AcpiOsWritePciConfiguration(ACPI_PCI_ID *pciId, UINT32 reg,
933775afec4SFredrik Holmqvist 		ACPI_INTEGER value, UINT32 width)
934775afec4SFredrik Holmqvist {
935775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
936775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
937775afec4SFredrik Holmqvist 	gPCIManager->write_pci_config(
938775afec4SFredrik Holmqvist 		pciId->Bus, pciId->Device, pciId->Function, reg, width / 8, value);
939775afec4SFredrik Holmqvist 	return AE_OK;
940775afec4SFredrik Holmqvist #else
941775afec4SFredrik Holmqvist 	return AE_ERROR;
942775afec4SFredrik Holmqvist #endif
943775afec4SFredrik Holmqvist }
944775afec4SFredrik Holmqvist 
945775afec4SFredrik Holmqvist 
946775afec4SFredrik Holmqvist /******************************************************************************
947775afec4SFredrik Holmqvist  *
948775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsReadPort
949775afec4SFredrik Holmqvist  *
950775afec4SFredrik Holmqvist  * PARAMETERS:  address             Address of I/O port/register to read
951775afec4SFredrik Holmqvist  *              Value               Where value is placed
952775afec4SFredrik Holmqvist  *              width               Number of bits
953775afec4SFredrik Holmqvist  *
954775afec4SFredrik Holmqvist  * RETURN:      Value read from port
955775afec4SFredrik Holmqvist  *
956775afec4SFredrik Holmqvist  * DESCRIPTION: Read data from an I/O port or register
957775afec4SFredrik Holmqvist  *
958775afec4SFredrik Holmqvist  *****************************************************************************/
959775afec4SFredrik Holmqvist ACPI_STATUS
960775afec4SFredrik Holmqvist AcpiOsReadPort(ACPI_IO_ADDRESS address, UINT32 *value, UINT32 width)
961775afec4SFredrik Holmqvist {
962775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
963775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("addr: 0x%08lx; width: %lu", (addr_t)address, width);
964775afec4SFredrik Holmqvist 	switch (width) {
965775afec4SFredrik Holmqvist 		case 8:
966775afec4SFredrik Holmqvist 			*value = gPCIManager->read_io_8(address);
967775afec4SFredrik Holmqvist 			break;
968775afec4SFredrik Holmqvist 
969775afec4SFredrik Holmqvist 		case 16:
970775afec4SFredrik Holmqvist 			*value = gPCIManager->read_io_16(address);
971775afec4SFredrik Holmqvist 			break;
972775afec4SFredrik Holmqvist 
973775afec4SFredrik Holmqvist 		case 32:
974775afec4SFredrik Holmqvist 			*value = gPCIManager->read_io_32(address);
975775afec4SFredrik Holmqvist 			break;
976775afec4SFredrik Holmqvist 
977775afec4SFredrik Holmqvist 		default:
978775afec4SFredrik Holmqvist 			return AE_ERROR;
979775afec4SFredrik Holmqvist 	}
980775afec4SFredrik Holmqvist 
981775afec4SFredrik Holmqvist 	return AE_OK;
982775afec4SFredrik Holmqvist #else
983775afec4SFredrik Holmqvist 	return AE_ERROR;
984775afec4SFredrik Holmqvist #endif
985775afec4SFredrik Holmqvist }
986775afec4SFredrik Holmqvist 
987775afec4SFredrik Holmqvist 
988775afec4SFredrik Holmqvist /******************************************************************************
989775afec4SFredrik Holmqvist  *
990775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsWritePort
991775afec4SFredrik Holmqvist  *
992775afec4SFredrik Holmqvist  * PARAMETERS:  address             Address of I/O port/register to write
993775afec4SFredrik Holmqvist  *              value               Value to write
994775afec4SFredrik Holmqvist  *              width               Number of bits
995775afec4SFredrik Holmqvist  *
996775afec4SFredrik Holmqvist  * RETURN:      None
997775afec4SFredrik Holmqvist  *
998775afec4SFredrik Holmqvist  * DESCRIPTION: Write data to an I/O port or register
999775afec4SFredrik Holmqvist  *
1000775afec4SFredrik Holmqvist  *****************************************************************************/
1001775afec4SFredrik Holmqvist ACPI_STATUS
1002775afec4SFredrik Holmqvist AcpiOsWritePort(ACPI_IO_ADDRESS address, UINT32 value, UINT32 width)
1003775afec4SFredrik Holmqvist {
1004775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
1005775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("addr: 0x%08lx; value: %lu; width: %lu",
1006775afec4SFredrik Holmqvist 		(addr_t)address, value, width);
1007775afec4SFredrik Holmqvist 	switch (width) {
1008775afec4SFredrik Holmqvist 		case 8:
1009775afec4SFredrik Holmqvist 			gPCIManager->write_io_8(address, value);
1010775afec4SFredrik Holmqvist 			break;
1011775afec4SFredrik Holmqvist 
1012775afec4SFredrik Holmqvist 		case 16:
1013775afec4SFredrik Holmqvist 			gPCIManager->write_io_16(address,value);
1014775afec4SFredrik Holmqvist 			break;
1015775afec4SFredrik Holmqvist 
1016775afec4SFredrik Holmqvist 		case 32:
1017775afec4SFredrik Holmqvist 			gPCIManager->write_io_32(address,value);
1018775afec4SFredrik Holmqvist 			break;
1019775afec4SFredrik Holmqvist 
1020775afec4SFredrik Holmqvist 		default:
1021775afec4SFredrik Holmqvist 			return AE_ERROR;
1022775afec4SFredrik Holmqvist 	}
1023775afec4SFredrik Holmqvist 
1024775afec4SFredrik Holmqvist 	return AE_OK;
1025775afec4SFredrik Holmqvist #else
1026775afec4SFredrik Holmqvist 	return AE_ERROR;
1027775afec4SFredrik Holmqvist #endif
1028775afec4SFredrik Holmqvist }
1029775afec4SFredrik Holmqvist 
1030775afec4SFredrik Holmqvist 
1031775afec4SFredrik Holmqvist /******************************************************************************
1032775afec4SFredrik Holmqvist  *
1033775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsReadMemory
1034775afec4SFredrik Holmqvist  *
1035775afec4SFredrik Holmqvist  * PARAMETERS:  address             Physical Memory Address to read
1036775afec4SFredrik Holmqvist  *              value               Where value is placed
1037775afec4SFredrik Holmqvist  *              width               Number of bits
1038775afec4SFredrik Holmqvist  *
1039775afec4SFredrik Holmqvist  * RETURN:      Value read from physical memory address
1040775afec4SFredrik Holmqvist  *
1041775afec4SFredrik Holmqvist  * DESCRIPTION: Read data from a physical memory address
1042775afec4SFredrik Holmqvist  *
1043775afec4SFredrik Holmqvist  *****************************************************************************/
1044775afec4SFredrik Holmqvist ACPI_STATUS
1045775afec4SFredrik Holmqvist AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS address, UINT64 *value, UINT32 width)
1046775afec4SFredrik Holmqvist {
1047775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
1048775afec4SFredrik Holmqvist 	if (vm_memcpy_from_physical(value, (addr_t)address, width / 8, false)
1049775afec4SFredrik Holmqvist 		!= B_OK) {
1050775afec4SFredrik Holmqvist 		return AE_ERROR;
1051775afec4SFredrik Holmqvist 	}
1052775afec4SFredrik Holmqvist 	return AE_OK;
1053775afec4SFredrik Holmqvist #else
1054775afec4SFredrik Holmqvist 	return AE_ERROR;
1055775afec4SFredrik Holmqvist #endif
1056775afec4SFredrik Holmqvist }
1057775afec4SFredrik Holmqvist 
1058775afec4SFredrik Holmqvist 
1059775afec4SFredrik Holmqvist /******************************************************************************
1060775afec4SFredrik Holmqvist  *
1061775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsWriteMemory
1062775afec4SFredrik Holmqvist  *
1063775afec4SFredrik Holmqvist  * PARAMETERS:  address             Physical Memory Address to write
1064775afec4SFredrik Holmqvist  *              value               Value to write
1065775afec4SFredrik Holmqvist  *              width               Number of bits
1066775afec4SFredrik Holmqvist  *
1067775afec4SFredrik Holmqvist  * RETURN:      None
1068775afec4SFredrik Holmqvist  *
1069775afec4SFredrik Holmqvist  * DESCRIPTION: Write data to a physical memory address
1070775afec4SFredrik Holmqvist  *
1071775afec4SFredrik Holmqvist  *****************************************************************************/
1072775afec4SFredrik Holmqvist ACPI_STATUS
1073775afec4SFredrik Holmqvist AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS address, UINT64 value, UINT32 width)
1074775afec4SFredrik Holmqvist {
1075775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
1076775afec4SFredrik Holmqvist 	if (vm_memcpy_to_physical((addr_t)address, &value, width / 8, false)
1077775afec4SFredrik Holmqvist 			!= B_OK) {
1078775afec4SFredrik Holmqvist 		return AE_ERROR;
1079775afec4SFredrik Holmqvist 	}
1080775afec4SFredrik Holmqvist 	return AE_OK;
1081775afec4SFredrik Holmqvist #else
1082775afec4SFredrik Holmqvist 	return AE_ERROR;
1083775afec4SFredrik Holmqvist #endif
1084775afec4SFredrik Holmqvist }
1085775afec4SFredrik Holmqvist 
1086775afec4SFredrik Holmqvist 
1087775afec4SFredrik Holmqvist /******************************************************************************
1088775afec4SFredrik Holmqvist  *
1089775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsReadable
1090775afec4SFredrik Holmqvist  *
1091775afec4SFredrik Holmqvist  * PARAMETERS:  pointer             - Area to be verified
1092775afec4SFredrik Holmqvist  *              length              - Size of area
1093775afec4SFredrik Holmqvist  *
1094775afec4SFredrik Holmqvist  * RETURN:      TRUE if readable for entire length
1095775afec4SFredrik Holmqvist  *
1096775afec4SFredrik Holmqvist  * DESCRIPTION: Verify that a pointer is valid for reading
1097775afec4SFredrik Holmqvist  *
1098775afec4SFredrik Holmqvist  *****************************************************************************/
1099775afec4SFredrik Holmqvist BOOLEAN
1100775afec4SFredrik Holmqvist AcpiOsReadable(void *pointer, ACPI_SIZE length)
1101775afec4SFredrik Holmqvist {
1102775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
1103775afec4SFredrik Holmqvist 	return true;
1104775afec4SFredrik Holmqvist #else
1105775afec4SFredrik Holmqvist 	area_id id;
1106775afec4SFredrik Holmqvist 	area_info info;
1107775afec4SFredrik Holmqvist 
1108775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("addr: %p; length: %lu", pointer, (size_t)length);
1109775afec4SFredrik Holmqvist 
1110775afec4SFredrik Holmqvist 	id = area_for(pointer);
1111775afec4SFredrik Holmqvist 	if (id == B_ERROR) return false;
1112775afec4SFredrik Holmqvist 	if (get_area_info(id, &info) != B_OK) return false;
1113775afec4SFredrik Holmqvist 	return (info.protection & B_READ_AREA) != 0 &&
1114775afec4SFredrik Holmqvist 			pointer + length <= info.address + info.ram_size;
1115775afec4SFredrik Holmqvist #endif
1116775afec4SFredrik Holmqvist }
1117775afec4SFredrik Holmqvist 
1118775afec4SFredrik Holmqvist 
1119775afec4SFredrik Holmqvist /******************************************************************************
1120775afec4SFredrik Holmqvist  *
1121775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsWritable
1122775afec4SFredrik Holmqvist  *
1123775afec4SFredrik Holmqvist  * PARAMETERS:  pointer             - Area to be verified
1124775afec4SFredrik Holmqvist  *              length              - Size of area
1125775afec4SFredrik Holmqvist  *
1126775afec4SFredrik Holmqvist  * RETURN:      TRUE if writable for entire length
1127775afec4SFredrik Holmqvist  *
1128775afec4SFredrik Holmqvist  * DESCRIPTION: Verify that a pointer is valid for writing
1129775afec4SFredrik Holmqvist  *
1130775afec4SFredrik Holmqvist  *****************************************************************************/
1131775afec4SFredrik Holmqvist BOOLEAN
1132775afec4SFredrik Holmqvist AcpiOsWritable(void *pointer, ACPI_SIZE length)
1133775afec4SFredrik Holmqvist {
1134775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
1135775afec4SFredrik Holmqvist 	return true;
1136775afec4SFredrik Holmqvist #else
1137775afec4SFredrik Holmqvist 	area_id id;
1138775afec4SFredrik Holmqvist 	area_info info;
1139775afec4SFredrik Holmqvist 
1140775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("addr: %p; length: %lu", pointer, (size_t)length);
1141775afec4SFredrik Holmqvist 
1142775afec4SFredrik Holmqvist 	id = area_for(pointer);
1143775afec4SFredrik Holmqvist 	if (id == B_ERROR) return false;
1144775afec4SFredrik Holmqvist 	if (get_area_info(id, &info) != B_OK) return false;
1145775afec4SFredrik Holmqvist 	return (info.protection & B_READ_AREA) != 0 &&
1146775afec4SFredrik Holmqvist 			(info.protection & B_WRITE_AREA) != 0 &&
1147775afec4SFredrik Holmqvist 			pointer + length <= info.address + info.ram_size;
1148775afec4SFredrik Holmqvist #endif
1149775afec4SFredrik Holmqvist }
1150775afec4SFredrik Holmqvist 
1151775afec4SFredrik Holmqvist 
1152775afec4SFredrik Holmqvist /******************************************************************************
1153775afec4SFredrik Holmqvist  *
1154775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsGetThreadId
1155775afec4SFredrik Holmqvist  *
1156775afec4SFredrik Holmqvist  * PARAMETERS:  None
1157775afec4SFredrik Holmqvist  *
1158775afec4SFredrik Holmqvist  * RETURN:      Id of the running thread
1159775afec4SFredrik Holmqvist  *
1160775afec4SFredrik Holmqvist  * DESCRIPTION: Get the Id of the current (running) thread
1161775afec4SFredrik Holmqvist  *
1162775afec4SFredrik Holmqvist  * NOTE:        The environment header should contain this line:
1163775afec4SFredrik Holmqvist  *                  #define ACPI_THREAD_ID pthread_t
1164775afec4SFredrik Holmqvist  *
1165775afec4SFredrik Holmqvist  *****************************************************************************/
1166775afec4SFredrik Holmqvist ACPI_THREAD_ID
1167775afec4SFredrik Holmqvist AcpiOsGetThreadId()
1168775afec4SFredrik Holmqvist {
1169775afec4SFredrik Holmqvist 	thread_id thread = find_thread(NULL);
1170775afec4SFredrik Holmqvist 	// TODO: We arn't allowed threads with id 0, handle this case.
1171775afec4SFredrik Holmqvist 	// ACPI treats a 0 return as an error,
1172775afec4SFredrik Holmqvist 	// but we are thread 0 in early boot
1173775afec4SFredrik Holmqvist 	return thread;
1174775afec4SFredrik Holmqvist }
1175775afec4SFredrik Holmqvist 
1176775afec4SFredrik Holmqvist 
1177775afec4SFredrik Holmqvist /******************************************************************************
1178775afec4SFredrik Holmqvist  *
1179775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsSignal
1180775afec4SFredrik Holmqvist  *
1181775afec4SFredrik Holmqvist  * PARAMETERS:  function            ACPI CA signal function code
1182775afec4SFredrik Holmqvist  *              info                Pointer to function-dependent structure
1183775afec4SFredrik Holmqvist  *
1184775afec4SFredrik Holmqvist  * RETURN:      Status
1185775afec4SFredrik Holmqvist  *
1186775afec4SFredrik Holmqvist  * DESCRIPTION: Miscellaneous functions. Example implementation only.
1187775afec4SFredrik Holmqvist  *
1188775afec4SFredrik Holmqvist  *****************************************************************************/
1189775afec4SFredrik Holmqvist ACPI_STATUS
1190775afec4SFredrik Holmqvist AcpiOsSignal(UINT32 function, void *info)
1191775afec4SFredrik Holmqvist {
1192775afec4SFredrik Holmqvist 	DEBUG_FUNCTION();
1193775afec4SFredrik Holmqvist 
1194775afec4SFredrik Holmqvist 	switch (function) {
1195775afec4SFredrik Holmqvist 		case ACPI_SIGNAL_FATAL:
1196775afec4SFredrik Holmqvist #ifdef _KERNEL_MODE
1197775afec4SFredrik Holmqvist 			panic(info == NULL ? "AcpiOsSignal: fatal" : (const char*)info);
1198775afec4SFredrik Holmqvist 			break;
1199775afec4SFredrik Holmqvist #endif
1200775afec4SFredrik Holmqvist 		case ACPI_SIGNAL_BREAKPOINT:
1201775afec4SFredrik Holmqvist 			if (info != NULL)
1202775afec4SFredrik Holmqvist 				AcpiOsPrintf("AcpiOsBreakpoint: %s ****\n", info);
1203775afec4SFredrik Holmqvist 			else
1204775afec4SFredrik Holmqvist 				AcpiOsPrintf("At AcpiOsBreakpoint ****\n");
1205775afec4SFredrik Holmqvist 			break;
1206775afec4SFredrik Holmqvist 	}
1207775afec4SFredrik Holmqvist 
1208775afec4SFredrik Holmqvist 	return AE_OK;
1209775afec4SFredrik Holmqvist }
1210775afec4SFredrik Holmqvist 
1211775afec4SFredrik Holmqvist 
1212775afec4SFredrik Holmqvist /*
1213775afec4SFredrik Holmqvist  * Adapted from FreeBSD since the documentation of its intended impl
1214775afec4SFredrik Holmqvist  * is lacking.
1215775afec4SFredrik Holmqvist  *  Section 5.2.10.1: global lock acquire/release functions */
1216775afec4SFredrik Holmqvist #define GL_ACQUIRED     (-1)
1217775afec4SFredrik Holmqvist #define GL_BUSY         0
1218775afec4SFredrik Holmqvist #define GL_BIT_PENDING  0x01
1219775afec4SFredrik Holmqvist #define GL_BIT_OWNED    0x02
1220775afec4SFredrik Holmqvist #define GL_BIT_MASK     (GL_BIT_PENDING | GL_BIT_OWNED)
1221775afec4SFredrik Holmqvist 
1222775afec4SFredrik Holmqvist 
1223775afec4SFredrik Holmqvist /*
1224775afec4SFredrik Holmqvist  * Adapted from FreeBSD since the documentation of its intended impl
1225775afec4SFredrik Holmqvist  * is lacking.
1226775afec4SFredrik Holmqvist  * Acquire the global lock.  If busy, set the pending bit.  The caller
1227775afec4SFredrik Holmqvist  * will wait for notification from the BIOS that the lock is available
1228775afec4SFredrik Holmqvist  * and then attempt to acquire it again.
1229775afec4SFredrik Holmqvist  */
1230775afec4SFredrik Holmqvist int
1231775afec4SFredrik Holmqvist AcpiOsAcquireGlobalLock(uint32 *lock)
1232775afec4SFredrik Holmqvist {
1233775afec4SFredrik Holmqvist 	uint32 newValue;
1234775afec4SFredrik Holmqvist 	uint32 oldValue;
1235775afec4SFredrik Holmqvist 
1236775afec4SFredrik Holmqvist 	do {
1237775afec4SFredrik Holmqvist 		oldValue = *lock;
1238775afec4SFredrik Holmqvist 		newValue = ((oldValue & ~GL_BIT_MASK) | GL_BIT_OWNED) |
1239775afec4SFredrik Holmqvist 				((oldValue >> 1) & GL_BIT_PENDING);
1240775afec4SFredrik Holmqvist 		atomic_test_and_set((int32*)lock, newValue, oldValue);
1241775afec4SFredrik Holmqvist 	} while (*lock == oldValue);
1242775afec4SFredrik Holmqvist 	return ((newValue < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
1243775afec4SFredrik Holmqvist }
1244775afec4SFredrik Holmqvist 
1245775afec4SFredrik Holmqvist 
1246775afec4SFredrik Holmqvist /*
1247775afec4SFredrik Holmqvist  * Adapted from FreeBSD since the documentation of its intended impl
1248775afec4SFredrik Holmqvist  * is lacking.
1249775afec4SFredrik Holmqvist  * Release the global lock, returning whether there is a waiter pending.
1250775afec4SFredrik Holmqvist  * If the BIOS set the pending bit, OSPM must notify the BIOS when it
1251775afec4SFredrik Holmqvist  * releases the lock.
1252775afec4SFredrik Holmqvist  */
1253775afec4SFredrik Holmqvist int
1254775afec4SFredrik Holmqvist AcpiOsReleaseGlobalLock(uint32 *lock)
1255775afec4SFredrik Holmqvist {
1256775afec4SFredrik Holmqvist 	uint32 newValue;
1257775afec4SFredrik Holmqvist 	uint32 oldValue;
1258775afec4SFredrik Holmqvist 
1259775afec4SFredrik Holmqvist 	do {
1260775afec4SFredrik Holmqvist 		oldValue = *lock;
1261775afec4SFredrik Holmqvist 		newValue = oldValue & ~GL_BIT_MASK;
1262775afec4SFredrik Holmqvist 		atomic_test_and_set((int32*)lock, newValue, oldValue);
1263775afec4SFredrik Holmqvist 	} while (*lock == oldValue);
1264775afec4SFredrik Holmqvist 	return (oldValue & GL_BIT_PENDING);
1265775afec4SFredrik Holmqvist }
1266775afec4SFredrik Holmqvist 
1267775afec4SFredrik Holmqvist 
1268775afec4SFredrik Holmqvist ACPI_STATUS
1269775afec4SFredrik Holmqvist AcpiOsCreateMutex(ACPI_MUTEX* outHandle)
1270775afec4SFredrik Holmqvist {
1271775afec4SFredrik Holmqvist 	*outHandle = (ACPI_MUTEX) malloc(sizeof(mutex));
1272775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("result: %p", *outHandle);
1273775afec4SFredrik Holmqvist 	if (*outHandle == NULL)
1274775afec4SFredrik Holmqvist 		return AE_NO_MEMORY;
1275775afec4SFredrik Holmqvist 
1276775afec4SFredrik Holmqvist 	mutex_init(*outHandle, "acpi mutex");
1277775afec4SFredrik Holmqvist 	return AE_OK;
1278775afec4SFredrik Holmqvist }
1279775afec4SFredrik Holmqvist 
1280775afec4SFredrik Holmqvist 
1281775afec4SFredrik Holmqvist void
1282775afec4SFredrik Holmqvist AcpiOsDeleteMutex(ACPI_MUTEX handle)
1283775afec4SFredrik Holmqvist {
1284775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("mutex: %ld", handle);
1285775afec4SFredrik Holmqvist 	mutex_destroy(handle);
1286775afec4SFredrik Holmqvist 	free((void*)handle);
1287775afec4SFredrik Holmqvist }
1288775afec4SFredrik Holmqvist 
1289775afec4SFredrik Holmqvist 
1290775afec4SFredrik Holmqvist ACPI_STATUS
1291775afec4SFredrik Holmqvist AcpiOsAcquireMutex(ACPI_MUTEX handle, UINT16 timeout)
1292775afec4SFredrik Holmqvist {
1293775afec4SFredrik Holmqvist 	ACPI_STATUS result = AE_OK;
1294775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_VF("mutex: %ld; timeout: %u", handle, timeout);
1295775afec4SFredrik Holmqvist 
1296775afec4SFredrik Holmqvist 	if (timeout == ACPI_WAIT_FOREVER)
1297775afec4SFredrik Holmqvist 		result = mutex_lock(handle) == B_OK ? AE_OK : AE_BAD_PARAMETER;
1298775afec4SFredrik Holmqvist 	else {
1299775afec4SFredrik Holmqvist 		switch (mutex_lock_with_timeout(handle, B_RELATIVE_TIMEOUT,
1300775afec4SFredrik Holmqvist 			(bigtime_t)timeout * 1000)) {
1301775afec4SFredrik Holmqvist 			case B_OK:
1302775afec4SFredrik Holmqvist 				result = AE_OK;
1303775afec4SFredrik Holmqvist 				break;
1304775afec4SFredrik Holmqvist 			case B_INTERRUPTED:
1305775afec4SFredrik Holmqvist 			case B_TIMED_OUT:
1306775afec4SFredrik Holmqvist 			case B_WOULD_BLOCK:
1307775afec4SFredrik Holmqvist 				result = AE_TIME;
1308775afec4SFredrik Holmqvist 				break;
1309775afec4SFredrik Holmqvist 			case B_BAD_VALUE:
1310775afec4SFredrik Holmqvist 			default:
1311775afec4SFredrik Holmqvist 				result = AE_BAD_PARAMETER;
1312775afec4SFredrik Holmqvist 				break;
1313775afec4SFredrik Holmqvist 		}
1314775afec4SFredrik Holmqvist 	}
1315775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_VF("mutex: %ld; timeout: %u result: %lu",
1316775afec4SFredrik Holmqvist 		handle, timeout, (uint32)result);
1317775afec4SFredrik Holmqvist 	return result;
1318775afec4SFredrik Holmqvist }
1319775afec4SFredrik Holmqvist 
1320775afec4SFredrik Holmqvist 
1321775afec4SFredrik Holmqvist void
1322775afec4SFredrik Holmqvist AcpiOsReleaseMutex(ACPI_MUTEX handle)
1323775afec4SFredrik Holmqvist {
1324775afec4SFredrik Holmqvist 	DEBUG_FUNCTION_F("mutex: %p", handle);
1325775afec4SFredrik Holmqvist 	mutex_unlock(handle);
1326775afec4SFredrik Holmqvist }
1327775afec4SFredrik Holmqvist 
1328775afec4SFredrik Holmqvist 
1329775afec4SFredrik Holmqvist /******************************************************************************
1330775afec4SFredrik Holmqvist  *
1331775afec4SFredrik Holmqvist  * FUNCTION:    AcpiOsWaitEventsComplete
1332775afec4SFredrik Holmqvist  *
1333775afec4SFredrik Holmqvist  * PARAMETERS:  None
1334775afec4SFredrik Holmqvist  *
1335775afec4SFredrik Holmqvist  * RETURN:      None
1336775afec4SFredrik Holmqvist  *
1337775afec4SFredrik Holmqvist  * DESCRIPTION: Wait for all asynchronous events to complete. This
1338775afec4SFredrik Holmqvist  *              implementation does nothing.
1339775afec4SFredrik Holmqvist  *
1340775afec4SFredrik Holmqvist  *****************************************************************************/
1341775afec4SFredrik Holmqvist void
1342775afec4SFredrik Holmqvist AcpiOsWaitEventsComplete()
1343775afec4SFredrik Holmqvist {
1344775afec4SFredrik Holmqvist     //TODO: FreeBSD See description.
1345775afec4SFredrik Holmqvist     return;
1346775afec4SFredrik Holmqvist }
1347