ACPI: add ACPI_SERIAL_* functions.
[dragonfly.git] / sys / dev / acpica5 / acpivar.h
CommitLineData
5ed44076
MD
1/*-
2 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000 BSDi
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
c8b4f0e6 28 * $FreeBSD: src/sys/dev/acpica/acpivar.h,v 1.71 2004/06/13 22:52:30 njl Exp $
7d58d14b 29 * $DragonFly: src/sys/dev/acpica5/acpivar.h,v 1.16 2008/09/29 06:59:45 hasso Exp $
5ed44076
MD
30 */
31
676159d4 32#include "acpi_if.h"
5ed44076
MD
33#include "bus_if.h"
34#include <sys/eventhandler.h>
35#include <sys/sysctl.h>
44dd3cc7
AP
36#include <sys/globaldata.h>
37#include <sys/serialize.h>
38#include <sys/thread2.h>
5ed44076
MD
39#if __FreeBSD_version >= 500000
40#include <sys/lock.h>
41#include <sys/mutex.h>
42#endif
1f7ab7c9 43#include <sys/bus.h>
5ed44076
MD
44
45struct acpi_softc {
46 device_t acpi_dev;
b13267a5 47 cdev_t acpi_dev_t;
5ed44076 48
edf6eb70 49 struct callout acpi_sleep_timer;
5ed44076
MD
50 struct resource *acpi_irq;
51 int acpi_irq_rid;
52 void *acpi_irq_handle;
53
54 int acpi_enabled;
55 int acpi_sstate;
56 int acpi_sleep_disabled;
57
58 struct sysctl_ctx_list acpi_sysctl_ctx;
59 struct sysctl_oid *acpi_sysctl_tree;
5ed44076
MD
60 int acpi_power_button_sx;
61 int acpi_sleep_button_sx;
62 int acpi_lid_switch_sx;
63
64 int acpi_standby_sx;
65 int acpi_suspend_sx;
66
67 int acpi_sleep_delay;
68 int acpi_s4bios;
69 int acpi_disable_on_poweroff;
5ed44076
MD
70 int acpi_verbose;
71
72 bus_dma_tag_t acpi_waketag;
73 bus_dmamap_t acpi_wakemap;
74 vm_offset_t acpi_wakeaddr;
75 vm_paddr_t acpi_wakephys;
76
77 struct sysctl_ctx_list acpi_battery_sysctl_ctx;
78 struct sysctl_oid *acpi_battery_sysctl_tree;
79};
80
81struct acpi_device {
82 /* ACPI ivars */
83 ACPI_HANDLE ad_handle;
84 int ad_magic;
85 void *ad_private;
86
f9d8cd12 87 /* Resources */
5ed44076 88 struct resource_list ad_rl;
49e48b8a 89};
5ed44076 90
49e48b8a
MD
91struct acpi_prw_data {
92 ACPI_HANDLE gpe_handle;
93 int gpe_bit;
94 int lowest_wake;
95 void *power_res;
5ed44076
MD
96};
97
49e48b8a
MD
98/* Flags for each device defined in the AML namespace. */
99#define ACPI_FLAG_WAKE_CAPABLE 0x1
100#define ACPI_FLAG_WAKE_ENABLED 0x2
101
5ed44076
MD
102#if defined(__DragonFly__)
103/*
1a5f156c 104 * In DragonFly, ACPI is protected by critical sections.
5ed44076 105 */
1a5f156c
SS
106# define ACPI_LOCK crit_enter()
107# define ACPI_UNLOCK crit_exit()
5ed44076
MD
108# define ACPI_ASSERTLOCK
109# define ACPI_MSLEEP(a, b, c, d, e) tsleep(a, c, d, e)
1a5f156c 110# define ACPI_LOCK_DECL
5ed44076 111# define kthread_create(a, b, c, d, e, f) kthread_create(a, b, c, f)
44dd3cc7
AP
112#define ACPI_SERIAL_BEGIN(sys) lwkt_serialize_enter(&acpi_sys##_serializer)
113#define ACPI_SERIAL_END(sys) lwkt_serialize_exit(&acpi_sys##_serializer)
114#define ACPI_SERIAL_ASSERT(sys) ASSERT_SERIALIZED(&acpi_sys##_serializer)
115#define ACPI_SERIAL_DECL(sys, name) static struct lwkt_serialize acpi_sys##_serializer;
116#define ACPI_SERIAL_INIT(sys) lwkt_serialize_init(&acpi_sys##_serializer)
5ed44076
MD
117#elif __FreeBSD_version < 500000
118/*
119 * In 4.x, ACPI is protected by splhigh().
120 */
121# define ACPI_LOCK s = splhigh()
122# define ACPI_UNLOCK splx(s)
123# define ACPI_ASSERTLOCK
124# define ACPI_MSLEEP(a, b, c, d, e) tsleep(a, c, d, e)
125# define ACPI_LOCK_DECL int s
126# define kthread_create(a, b, c, d, e, f) kthread_create(a, b, c, f)
127# define tc_init(a) init_timecounter(a)
128#else
129# define ACPI_LOCK
130# define ACPI_UNLOCK
131# define ACPI_ASSERTLOCK
132# define ACPI_LOCK_DECL
133#endif
134
135/*
136 * ACPI CA does not define layers for non-ACPI CA drivers.
137 * We define some here within the range provided.
138 */
f9d8cd12
MD
139#define ACPI_AC_ADAPTER 0x00010000
140#define ACPI_BATTERY 0x00020000
141#define ACPI_BUS 0x00040000
142#define ACPI_BUTTON 0x00080000
143#define ACPI_EC 0x00100000
144#define ACPI_FAN 0x00200000
145#define ACPI_POWERRES 0x00400000
5ed44076
MD
146#define ACPI_PROCESSOR 0x00800000
147#define ACPI_THERMAL 0x01000000
f9d8cd12 148#define ACPI_TIMER 0x02000000
1abcfdcd 149#define ACPI_OEM 0x04000000
5ed44076
MD
150
151/*
152 * Constants for different interrupt models used with acpi_SetIntrModel().
153 */
154#define ACPI_INTR_PIC 0
155#define ACPI_INTR_APIC 1
156#define ACPI_INTR_SAPIC 2
157
158/*
5ebadb2c
HT
159 * Various features and capabilities for the acpi_get_features() method.
160 * In particular, these are used for the ACPI 3.0 _PDC and _OSC methods.
161 */
162#define ACPI_CAP_PERF_MSRS (1 << 0) /* Intel SpeedStep PERF_CTL MSRs */
163#define ACPI_CAP_C1_IO_HALT (1 << 1) /* Intel C1 "IO then halt" sequence */
164#define ACPI_CAP_THR_MSRS (1 << 2) /* Intel OnDemand throttling MSRs */
165#define ACPI_CAP_SMP_SAME (1 << 3) /* MP C1, Px, and Tx (all the same) */
166#define ACPI_CAP_SMP_SAME_C3 (1 << 4) /* MP C2 and C3 (all the same) */
167#define ACPI_CAP_SMP_DIFF_PX (1 << 5) /* MP Px (different, using _PSD) */
168#define ACPI_CAP_SMP_DIFF_CX (1 << 6) /* MP Cx (different, using _CSD) */
169#define ACPI_CAP_SMP_DIFF_TX (1 << 7) /* MP Tx (different, using _TSD) */
170#define ACPI_CAP_SMP_C1_NATIVE (1 << 8) /* MP C1 support other than halt */
171
172/*
5ed44076
MD
173 * Note that the low ivar values are reserved to provide
174 * interface compatibility with ISA drivers which can also
175 * attach to ACPI.
176 */
177#define ACPI_IVAR_HANDLE 0x100
178#define ACPI_IVAR_MAGIC 0x101
179#define ACPI_IVAR_PRIVATE 0x102
180
49e48b8a
MD
181/*
182 * Accessor functions for our ivars. Default value for BUS_READ_IVAR is
183 * (type) 0. The <sys/bus.h> accessor functions don't check return values.
184 */
185#define __ACPI_BUS_ACCESSOR(varp, var, ivarp, ivar, type) \
186 \
187static __inline type varp ## _get_ ## var(device_t dev) \
188{ \
189 uintptr_t v = 0; \
190 BUS_READ_IVAR(device_get_parent(dev), dev, \
191 ivarp ## _IVAR_ ## ivar, &v); \
192 return ((type) v); \
193} \
194 \
195static __inline void varp ## _set_ ## var(device_t dev, type t) \
196{ \
197 uintptr_t v = (uintptr_t) t; \
198 BUS_WRITE_IVAR(device_get_parent(dev), dev, \
199 ivarp ## _IVAR_ ## ivar, v); \
f9d8cd12
MD
200}
201
49e48b8a
MD
202__ACPI_BUS_ACCESSOR(acpi, handle, ACPI, HANDLE, ACPI_HANDLE)
203__ACPI_BUS_ACCESSOR(acpi, magic, ACPI, MAGIC, int)
204__ACPI_BUS_ACCESSOR(acpi, private, ACPI, PRIVATE, void *)
f9d8cd12 205
c8b4f0e6
YT
206void acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data);
207static __inline device_t
208acpi_get_device(ACPI_HANDLE handle)
209{
210 void *dev = NULL;
211 AcpiGetData(handle, acpi_fake_objhandler, &dev);
212 return ((device_t)dev);
213}
214
f9d8cd12
MD
215static __inline ACPI_OBJECT_TYPE
216acpi_get_type(device_t dev)
217{
218 ACPI_HANDLE h;
219 ACPI_OBJECT_TYPE t;
220
221 if ((h = acpi_get_handle(dev)) == NULL)
222 return (ACPI_TYPE_NOT_FOUND);
223 if (AcpiGetType(h, &t) != AE_OK)
224 return (ACPI_TYPE_NOT_FOUND);
225 return (t);
226}
5ed44076
MD
227
228#ifdef ACPI_DEBUGGER
49e48b8a 229void acpi_EnterDebugger(void);
5ed44076
MD
230#endif
231
232#ifdef ACPI_DEBUG
233#include <sys/cons.h>
e3869ec7 234#define STEP(x) do {kprintf x, kprintf("\n"); cngetc();} while (0)
5ed44076
MD
235#else
236#define STEP(x)
237#endif
238
239#define ACPI_VPRINT(dev, acpi_sc, x...) do { \
240 if (acpi_get_verbose(acpi_sc)) \
241 device_printf(dev, x); \
242} while (0)
243
244#define ACPI_DEVINFO_PRESENT(x) (((x) & 0x9) == 9)
49e48b8a
MD
245BOOLEAN acpi_DeviceIsPresent(device_t dev);
246BOOLEAN acpi_BatteryIsPresent(device_t dev);
c8b4f0e6 247BOOLEAN acpi_MatchHid(ACPI_HANDLE h, char *hid);
49e48b8a
MD
248ACPI_STATUS acpi_GetHandleInScope(ACPI_HANDLE parent, char *path,
249 ACPI_HANDLE *result);
250uint32_t acpi_TimerDelta(uint32_t end, uint32_t start);
251ACPI_BUFFER *acpi_AllocBuffer(int size);
252ACPI_STATUS acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp,
253 UINT32 *number);
254ACPI_STATUS acpi_GetInteger(ACPI_HANDLE handle, char *path,
255 UINT32 *number);
256ACPI_STATUS acpi_SetInteger(ACPI_HANDLE handle, char *path,
257 UINT32 number);
258ACPI_STATUS acpi_ForeachPackageObject(ACPI_OBJECT *obj,
259 void (*func)(ACPI_OBJECT *comp, void *arg), void *arg);
260ACPI_STATUS acpi_FindIndexedResource(ACPI_BUFFER *buf, int index,
261 ACPI_RESOURCE **resp);
262ACPI_STATUS acpi_AppendBufferResource(ACPI_BUFFER *buf,
263 ACPI_RESOURCE *res);
264ACPI_STATUS acpi_OverrideInterruptLevel(UINT32 InterruptNumber);
265ACPI_STATUS acpi_SetIntrModel(int model);
266ACPI_STATUS acpi_SetSleepState(struct acpi_softc *sc, int state);
267int acpi_wake_init(device_t dev, int type);
268int acpi_wake_set_enable(device_t dev, int enable);
269int acpi_wake_sleep_prep(device_t dev, int sstate);
270int acpi_wake_run_prep(device_t dev);
271ACPI_STATUS acpi_Startup(void);
272ACPI_STATUS acpi_Enable(struct acpi_softc *sc);
273ACPI_STATUS acpi_Disable(struct acpi_softc *sc);
274void acpi_UserNotify(const char *subsystem, ACPI_HANDLE h,
275 uint8_t notify);
276struct resource *acpi_bus_alloc_gas(device_t dev, int *rid,
1ce36465 277 const ACPI_GENERIC_ADDRESS *gas, u_int flags);
5ed44076
MD
278
279struct acpi_parse_resource_set {
f9d8cd12 280 void (*set_init)(device_t dev, void *arg, void **context);
5ed44076 281 void (*set_done)(device_t dev, void *context);
49e48b8a
MD
282 void (*set_ioport)(device_t dev, void *context, uint32_t base,
283 uint32_t length);
284 void (*set_iorange)(device_t dev, void *context, uint32_t low,
285 uint32_t high, uint32_t length, uint32_t align);
286 void (*set_memory)(device_t dev, void *context, uint32_t base,
287 uint32_t length);
288 void (*set_memoryrange)(device_t dev, void *context, uint32_t low,
289 uint32_t high, uint32_t length, uint32_t align);
e1eeedd0 290 void (*set_irq)(device_t dev, void *context, u_int8_t *irq,
49e48b8a 291 int count, int trig, int pol);
e1eeedd0
YT
292 void (*set_ext_irq)(device_t dev, void *context, u_int32_t *irq,
293 int count, int trig, int pol);
294 void (*set_drq)(device_t dev, void *context, u_int8_t *drq,
49e48b8a 295 int count);
5ed44076 296 void (*set_start_dependant)(device_t dev, void *context,
49e48b8a 297 int preference);
5ed44076
MD
298 void (*set_end_dependant)(device_t dev, void *context);
299};
300
49e48b8a
MD
301extern struct acpi_parse_resource_set acpi_res_parse_set;
302ACPI_STATUS acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
303 struct acpi_parse_resource_set *set, void *arg);
c8b4f0e6
YT
304extern struct rman acpi_rman_io, acpi_rman_mem;
305struct resource_list_entry *acpi_sysres_find(int type, u_long addr);
5ed44076
MD
306
307/* ACPI event handling */
49e48b8a
MD
308UINT32 acpi_event_power_button_sleep(void *context);
309UINT32 acpi_event_power_button_wake(void *context);
310UINT32 acpi_event_sleep_button_sleep(void *context);
311UINT32 acpi_event_sleep_button_wake(void *context);
5ed44076
MD
312
313#define ACPI_EVENT_PRI_FIRST 0
314#define ACPI_EVENT_PRI_DEFAULT 10000
315#define ACPI_EVENT_PRI_LAST 20000
316
317typedef void (*acpi_event_handler_t)(void *, int);
318
319EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t);
320EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t);
321
322/* Device power control. */
49e48b8a 323ACPI_STATUS acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state);
5ed44076
MD
324
325/* Misc. */
326static __inline struct acpi_softc *
327acpi_device_get_parent_softc(device_t child)
328{
329 device_t parent;
330
331 parent = device_get_parent(child);
332 if (parent == NULL)
333 return (NULL);
334 return (device_get_softc(parent));
335}
336
337static __inline int
338acpi_get_verbose(struct acpi_softc *sc)
339{
340 if (sc)
341 return (sc->acpi_verbose);
342 return (0);
343}
344
49e48b8a
MD
345char *acpi_name(ACPI_HANDLE handle);
346int acpi_avoid(ACPI_HANDLE handle);
347int acpi_disabled(char *subsys);
2df6e324 348int acpi_enabled(char *subsys);
49e48b8a
MD
349int acpi_machdep_init(device_t dev);
350void acpi_install_wakeup_handler(struct acpi_softc *sc);
351int acpi_sleep_machdep(struct acpi_softc *sc, int state);
5ed44076
MD
352
353/* Battery Abstraction. */
354struct acpi_battinfo;
5ed44076 355
7d58d14b
HT
356int acpi_battery_register(device_t dev);
357int acpi_battery_remove(device_t dev);
49e48b8a
MD
358int acpi_battery_get_units(void);
359int acpi_battery_get_info_expire(void);
7d58d14b
HT
360int acpi_battery_bst_valid(struct acpi_bst *bst);
361int acpi_battery_bif_valid(struct acpi_bif *bif);
362int acpi_battery_get_battinfo(device_t dev,
363 struct acpi_battinfo *info);
5ed44076
MD
364
365/* Embedded controller. */
49e48b8a 366void acpi_ec_ecdt_probe(device_t);
5ed44076
MD
367
368/* AC adapter interface. */
49e48b8a 369int acpi_acad_get_acline(int *);
5ed44076
MD
370
371/* Package manipulation convenience functions. */
c3a2c0df
SZ
372#define _ACPI_PKG_VALID(pkg) \
373 ((pkg) != NULL && (pkg)->Type == ACPI_TYPE_PACKAGE)
374#define ACPI_PKG_VALID(pkg, size) \
375 (_ACPI_PKG_VALID((pkg)) && (pkg)->Package.Count >= (size))
376#define ACPI_PKG_VALID_EQ(pkg, size) \
377 (_ACPI_PKG_VALID((pkg)) && (pkg)->Package.Count == (size))
5ed44076
MD
378int acpi_PkgInt(ACPI_OBJECT *res, int idx, ACPI_INTEGER *dst);
379int acpi_PkgInt32(ACPI_OBJECT *res, int idx, uint32_t *dst);
380int acpi_PkgStr(ACPI_OBJECT *res, int idx, void *dst, size_t size);
381int acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *rid,
5ebadb2c 382 struct resource **dst, u_int flags);
78e2ad38
SZ
383int acpi_PkgRawGas(ACPI_OBJECT *res, int idx,
384 ACPI_GENERIC_ADDRESS *gas);
f9d8cd12 385ACPI_HANDLE acpi_GetReference(ACPI_HANDLE scope, ACPI_OBJECT *obj);
5ed44076 386
5ed44076 387/* ACPI task kernel thread initialization. */
49e48b8a 388int acpi_task_thread_init(void);