Merge from vendor branch TCPDUMP:
[dragonfly.git] / sys / dev / acpica5 / acpivar.h
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  *
28  * $FreeBSD: src/sys/dev/acpica/acpivar.h,v 1.54 2004/02/19 18:20:03 njl Exp $
29  * $DragonFly: src/sys/dev/acpica5/acpivar.h,v 1.1 2004/02/21 06:48:08 dillon Exp $
30  */
31
32 #include "bus_if.h"
33 #include <sys/eventhandler.h>
34 #include <sys/sysctl.h>
35 #if __FreeBSD_version >= 500000
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #endif
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42
43 struct acpi_softc {
44     device_t            acpi_dev;
45     dev_t               acpi_dev_t;
46
47     struct resource     *acpi_irq;
48     int                 acpi_irq_rid;
49     void                *acpi_irq_handle;
50
51     int                 acpi_enabled;
52     int                 acpi_sstate;
53     int                 acpi_sleep_disabled;
54
55     struct sysctl_ctx_list acpi_sysctl_ctx;
56     struct sysctl_oid   *acpi_sysctl_tree;
57 #define ACPI_POWER_BUTTON_DEFAULT_SX    ACPI_STATE_S5;
58 #define ACPI_SLEEP_BUTTON_DEFAULT_SX    ACPI_STATE_S1;
59 #define ACPI_LID_SWITCH_DEFAULT_SX      ACPI_STATE_S1;
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;
70
71     int                 acpi_verbose;
72
73     bus_dma_tag_t       acpi_waketag;
74     bus_dmamap_t        acpi_wakemap;
75     vm_offset_t         acpi_wakeaddr;
76     vm_paddr_t          acpi_wakephys;
77
78     struct sysctl_ctx_list       acpi_battery_sysctl_ctx;
79     struct sysctl_oid           *acpi_battery_sysctl_tree;
80 };
81
82 struct acpi_device {
83     /* ACPI ivars */
84     ACPI_HANDLE                 ad_handle;
85     int                         ad_magic;
86     void                        *ad_private;
87
88     /* resources */
89     struct resource_list        ad_rl;
90
91 };
92
93 #if defined(__DragonFly__)
94 /*
95  * In 4.x, ACPI is protected by splhigh().
96  */
97 # define ACPI_LOCK                      s = splhigh()
98 # define ACPI_UNLOCK                    splx(s)
99 # define ACPI_ASSERTLOCK
100 # define ACPI_MSLEEP(a, b, c, d, e)     tsleep(a, c, d, e)
101 # define ACPI_LOCK_DECL                 int s
102 # define kthread_create(a, b, c, d, e, f)       kthread_create(a, b, c, f)
103 #elif __FreeBSD_version < 500000
104 /*
105  * In 4.x, ACPI is protected by splhigh().
106  */
107 # define ACPI_LOCK                      s = splhigh()
108 # define ACPI_UNLOCK                    splx(s)
109 # define ACPI_ASSERTLOCK
110 # define ACPI_MSLEEP(a, b, c, d, e)     tsleep(a, c, d, e)
111 # define ACPI_LOCK_DECL                 int s
112 # define kthread_create(a, b, c, d, e, f)       kthread_create(a, b, c, f)
113 # define tc_init(a)                     init_timecounter(a)
114 #else
115 # define ACPI_LOCK
116 # define ACPI_UNLOCK
117 # define ACPI_ASSERTLOCK
118 # define ACPI_LOCK_DECL
119 #endif
120
121 /*
122  * ACPI CA does not define layers for non-ACPI CA drivers.
123  * We define some here within the range provided.
124  */
125 #define ACPI_BUS                0x00010000
126 #define ACPI_SYSTEM             0x00020000
127 #define ACPI_POWER              0x00040000
128 #define ACPI_EC                 0x00080000
129 #define ACPI_AC_ADAPTER         0x00100000
130 #define ACPI_BATTERY            0x00200000
131 #define ACPI_BUTTON             0x00400000
132 #define ACPI_PROCESSOR          0x00800000
133 #define ACPI_THERMAL            0x01000000
134 #define ACPI_FAN                0x02000000
135 #define ACPI_TOSHIBA            0x04000000
136
137 /*
138  * Constants for different interrupt models used with acpi_SetIntrModel().
139  */
140 #define ACPI_INTR_PIC           0
141 #define ACPI_INTR_APIC          1
142 #define ACPI_INTR_SAPIC         2
143
144 /*
145  * Note that the low ivar values are reserved to provide
146  * interface compatibility with ISA drivers which can also
147  * attach to ACPI.
148  */
149 #define ACPI_IVAR_HANDLE        0x100
150 #define ACPI_IVAR_MAGIC         0x101
151 #define ACPI_IVAR_PRIVATE       0x102
152
153 extern ACPI_HANDLE      acpi_get_handle(device_t dev);
154 extern int              acpi_set_handle(device_t dev, ACPI_HANDLE h);
155 extern int              acpi_get_magic(device_t dev);
156 extern int              acpi_set_magic(device_t dev, int m);
157 extern void *           acpi_get_private(device_t dev);
158 extern int              acpi_set_private(device_t dev, void *p);
159 extern ACPI_OBJECT_TYPE acpi_get_type(device_t dev);
160 struct resource *       acpi_bus_alloc_gas(device_t dev, int *rid,
161                                            ACPI_GENERIC_ADDRESS *gas);
162
163 #ifdef ACPI_DEBUGGER
164 extern void             acpi_EnterDebugger(void);
165 #endif
166
167 #ifdef ACPI_DEBUG
168 #include <sys/cons.h>
169 #define STEP(x)         do {printf x, printf("\n"); cngetc();} while (0)
170 #else
171 #define STEP(x)
172 #endif
173
174 #define ACPI_VPRINT(dev, acpi_sc, x...) do {                    \
175     if (acpi_get_verbose(acpi_sc))                              \
176         device_printf(dev, x);                                  \
177 } while (0)
178
179 #define ACPI_DEVINFO_PRESENT(x) (((x) & 0x9) == 9)
180 extern BOOLEAN          acpi_DeviceIsPresent(device_t dev);
181 extern BOOLEAN          acpi_BatteryIsPresent(device_t dev);
182 extern BOOLEAN          acpi_MatchHid(device_t dev, char *hid);
183 extern ACPI_STATUS      acpi_GetHandleInScope(ACPI_HANDLE parent, char *path,
184                                               ACPI_HANDLE *result);
185 extern ACPI_BUFFER      *acpi_AllocBuffer(int size);
186 extern ACPI_STATUS      acpi_EvaluateInteger(ACPI_HANDLE handle, char *path,
187                                              int *number);
188 extern ACPI_STATUS      acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp,
189                                                     int *number);
190 extern ACPI_STATUS      acpi_ForeachPackageObject(ACPI_OBJECT *obj, 
191                                 void (*func)(ACPI_OBJECT *comp, void *arg),
192                                 void *arg);
193 extern ACPI_STATUS      acpi_FindIndexedResource(ACPI_BUFFER *buf, int index,
194                                                  ACPI_RESOURCE **resp);
195 extern ACPI_STATUS      acpi_AppendBufferResource(ACPI_BUFFER *buf,
196                                                   ACPI_RESOURCE *res);
197 extern ACPI_STATUS      acpi_OverrideInterruptLevel(UINT32 InterruptNumber);
198 extern ACPI_STATUS      acpi_SetIntrModel(int model);
199 extern ACPI_STATUS      acpi_SetSleepState(struct acpi_softc *sc, int state);
200 extern ACPI_STATUS      acpi_Startup(void);
201 extern ACPI_STATUS      acpi_Enable(struct acpi_softc *sc);
202 extern ACPI_STATUS      acpi_Disable(struct acpi_softc *sc);
203 extern void             acpi_UserNotify(const char *subsystem, ACPI_HANDLE h,
204                                         uint8_t notify);
205
206 struct acpi_parse_resource_set {
207     void        (*set_init)(device_t dev, void **context);
208     void        (*set_done)(device_t dev, void *context);
209     void        (*set_ioport)(device_t dev, void *context, u_int32_t base,
210                               u_int32_t length);
211     void        (*set_iorange)(device_t dev, void *context,
212                                u_int32_t low, u_int32_t high, 
213                                u_int32_t length, u_int32_t align);
214     void        (*set_memory)(device_t dev, void *context, u_int32_t base,
215                               u_int32_t length);
216     void        (*set_memoryrange)(device_t dev, void *context, u_int32_t low,
217                                    u_int32_t high, u_int32_t length,
218                                    u_int32_t align);
219     void        (*set_irq)(device_t dev, void *context, u_int32_t *irq,
220                            int count, int trig, int pol);
221     void        (*set_drq)(device_t dev, void *context, u_int32_t *drq,
222                            int count);
223     void        (*set_start_dependant)(device_t dev, void *context,
224                                        int preference);
225     void        (*set_end_dependant)(device_t dev, void *context);
226 };
227
228 extern struct acpi_parse_resource_set   acpi_res_parse_set;
229 extern ACPI_STATUS      acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
230                             struct acpi_parse_resource_set *set);
231
232 /* ACPI event handling */
233 extern UINT32   acpi_event_power_button_sleep(void *context);
234 extern UINT32   acpi_event_power_button_wake(void *context);
235 extern UINT32   acpi_event_sleep_button_sleep(void *context);
236 extern UINT32   acpi_event_sleep_button_wake(void *context);
237
238 #define ACPI_EVENT_PRI_FIRST      0
239 #define ACPI_EVENT_PRI_DEFAULT    10000
240 #define ACPI_EVENT_PRI_LAST       20000
241
242 typedef void (*acpi_event_handler_t)(void *, int);
243
244 EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t);
245 EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t);
246
247 /* Device power control. */
248 extern ACPI_STATUS      acpi_pwr_switch_consumer(ACPI_HANDLE consumer,
249                                                  int state);
250
251 /* Misc. */
252 static __inline struct acpi_softc *
253 acpi_device_get_parent_softc(device_t child)
254 {
255     device_t    parent;
256
257     parent = device_get_parent(child);
258     if (parent == NULL)
259         return (NULL);
260     return (device_get_softc(parent));
261 }
262
263 static __inline int
264 acpi_get_verbose(struct acpi_softc *sc)
265 {
266     if (sc)
267         return (sc->acpi_verbose);
268     return (0);
269 }
270
271 extern char     *acpi_name(ACPI_HANDLE handle);
272 extern int      acpi_avoid(ACPI_HANDLE handle);
273 extern int      acpi_disabled(char *subsys);
274 extern void     acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable);
275 extern void     acpi_device_enable_wake_event(ACPI_HANDLE h);
276 extern int      acpi_machdep_init(device_t dev);
277 extern void     acpi_install_wakeup_handler(struct acpi_softc *sc);
278 extern int      acpi_sleep_machdep(struct acpi_softc *sc, int state);
279
280 /* Battery Abstraction. */
281 struct acpi_battinfo;
282 struct acpi_battdesc;
283
284 extern int      acpi_battery_register(int, int);
285 extern int      acpi_battery_get_battinfo(int, struct acpi_battinfo *);
286 extern int      acpi_battery_get_units(void);
287 extern int      acpi_battery_get_info_expire(void);
288 extern int      acpi_battery_get_battdesc(int, struct acpi_battdesc *);
289
290 extern int      acpi_cmbat_get_battinfo(int, struct acpi_battinfo *);
291
292 /* Embedded controller. */
293 extern void     acpi_ec_ecdt_probe(device_t);
294
295 /* AC adapter interface. */
296 extern int      acpi_acad_get_acline(int *);
297
298 /* Package manipulation convenience functions. */
299 #define ACPI_PKG_VALID(pkg, size)                               \
300     ((pkg) != NULL && (pkg)->Type == ACPI_TYPE_PACKAGE &&       \
301      (pkg)->Package.Count >= (size))
302 int             acpi_PkgInt(ACPI_OBJECT *res, int idx, ACPI_INTEGER *dst);
303 int             acpi_PkgInt32(ACPI_OBJECT *res, int idx, uint32_t *dst);
304 int             acpi_PkgStr(ACPI_OBJECT *res, int idx, void *dst, size_t size);
305 int             acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *rid,
306                             struct resource **dst);
307
308 #if __FreeBSD_version >= 500000
309 #ifndef ACPI_MAX_THREADS
310 #define ACPI_MAX_THREADS        3
311 #endif
312 #if ACPI_MAX_THREADS > 0
313 #define ACPI_USE_THREADS
314 #endif
315 #endif
316
317 #ifdef ACPI_USE_THREADS
318 /* ACPI task kernel thread initialization. */
319 extern int      acpi_task_thread_init(void);
320 #endif