convert spl*() to critical sections.
[dragonfly.git] / sys / dev / acpica / 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.43.2.1 2003/08/22 20:49:20 jhb Exp $
29  *      $DragonFly: src/sys/dev/acpica/Attic/acpivar.h,v 1.4 2005/06/04 14:25:45 corecode Exp $ 
30  */
31
32 #include "bus_if.h"
33 #include <sys/eventhandler.h>
34 #include <sys/sysctl.h>
35 #if defined(__FreeBSD__) && __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 callout      acpi_sleep_timer;
48     struct resource     *acpi_irq;
49     int                 acpi_irq_rid;
50     void                *acpi_irq_handle;
51
52     int                 acpi_enabled;
53     int                 acpi_sstate;
54     int                 acpi_sleep_disabled;
55
56     struct sysctl_ctx_list acpi_sysctl_ctx;
57     struct sysctl_oid   *acpi_sysctl_tree;
58 #define ACPI_POWER_BUTTON_DEFAULT_SX    ACPI_STATE_S5;
59 #define ACPI_SLEEP_BUTTON_DEFAULT_SX    ACPI_STATE_S1;
60 #define ACPI_LID_SWITCH_DEFAULT_SX      ACPI_STATE_S1;
61     int                 acpi_power_button_sx;
62     int                 acpi_sleep_button_sx;
63     int                 acpi_lid_switch_sx;
64
65     int                 acpi_standby_sx;
66     int                 acpi_suspend_sx;
67
68     int                 acpi_sleep_delay;
69     int                 acpi_s4bios;
70     int                 acpi_disable_on_poweroff;
71
72     int                 acpi_verbose;
73
74     bus_dma_tag_t       acpi_waketag;
75     bus_dmamap_t        acpi_wakemap;
76     vm_offset_t         acpi_wakeaddr;
77     vm_paddr_t          acpi_wakephys;
78
79     struct sysctl_ctx_list       acpi_battery_sysctl_ctx;
80     struct sysctl_oid           *acpi_battery_sysctl_tree;
81 };
82
83 struct acpi_device {
84     /* ACPI ivars */
85     ACPI_HANDLE                 ad_handle;
86     int                         ad_magic;
87     void                        *ad_private;
88
89     /* resources */
90     struct resource_list        ad_rl;
91
92 };
93
94 #if defined(__DragonFly__)
95 /*
96  * In DragonFly, ACPI is protected by critical sections.
97  */
98 # define ACPI_LOCK                      crit_enter()
99 # define ACPI_UNLOCK                    crit_exit()
100 # define ACPI_ASSERTLOCK
101 # define ACPI_MSLEEP(a, b, c, d, e)     tsleep(a, c, d, e)
102 # define ACPI_LOCK_DECL
103 # define kthread_create(a, b, c, d, e, f)       kthread_create(a, b, c, f)
104 # define tc_init(a)                     init_timecounter(a)
105 #elif __FreeBSD_version < 500000
106 /*
107  * In 4.x, ACPI is protected by splhigh().
108  */
109 # define ACPI_LOCK                      s = splhigh()
110 # define ACPI_UNLOCK                    splx(s)
111 # define ACPI_ASSERTLOCK
112 # define ACPI_MSLEEP(a, b, c, d, e)     tsleep(a, c, d, e)
113 # define ACPI_LOCK_DECL                 int s
114 # define kthread_create(a, b, c, d, e, f)       kthread_create(a, b, c, f)
115 # define tc_init(a)                     init_timecounter(a)
116 #elif 0
117 /*
118  * The ACPI subsystem lives under a single mutex.  You *must*
119  * acquire this mutex before calling any of the acpi_ or Acpi* functions.
120  */
121 extern struct mtx       acpi_mutex;
122 # define ACPI_LOCK                      mtx_lock(&acpi_mutex)
123 # define ACPI_UNLOCK                    mtx_unlock(&acpi_mutex)
124 # define ACPI_ASSERTLOCK                mtx_assert(&acpi_mutex, MA_OWNED)
125 # define ACPI_MSLEEP(a, b, c, d, e)     msleep(a, b, c, d, e)
126 # define ACPI_LOCK_DECL
127 #else
128 # define ACPI_LOCK
129 # define ACPI_UNLOCK
130 # define ACPI_ASSERTLOCK
131 # define ACPI_MSLEEP(a, b, c, d, e)     tsleep(a, c, d, e)
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  */
139 #define ACPI_BUS                0x00010000
140 #define ACPI_SYSTEM             0x00020000
141 #define ACPI_POWER              0x00040000
142 #define ACPI_EC                 0x00080000
143 #define ACPI_AC_ADAPTER         0x00100000
144 #define ACPI_BATTERY            0x00110000
145 #define ACPI_BUTTON             0x00120000
146 #define ACPI_PROCESSOR          0x00140000
147 #define ACPI_THERMAL            0x00180000
148 #define ACPI_FAN                0x00200000
149
150 /*
151  * Constants for different interrupt models used with acpi_SetIntrModel().
152  */
153 #define ACPI_INTR_PIC           0
154 #define ACPI_INTR_APIC          1
155 #define ACPI_INTR_SAPIC         2
156
157 /* XXX this is no longer referenced anywhere, remove? */
158 #if 0
159 /*
160  * This is a cheap and nasty way to get around the horrid counted list
161  * argument format that AcpiEvalateObject uses.
162  */
163 #define ACPI_OBJECTLIST_MAX     16
164 struct acpi_object_list {
165     UINT32      count;
166     ACPI_OBJECT *pointer[ACPI_OBJECTLIST_MAX];
167     ACPI_OBJECT object[ACPI_OBJECTLIST_MAX];
168 };
169
170 static __inline struct acpi_object_list *
171 acpi_AllocObjectList(int nobj)
172 {
173     struct acpi_object_list     *l;
174     int                         i;
175
176     if (nobj > ACPI_OBJECTLIST_MAX)
177         return(NULL);
178     if ((l = AcpiOsAllocate(sizeof(*l))) == NULL)
179         return(NULL);
180     bzero(l, sizeof(*l));
181     for (i = 0; i < ACPI_OBJECTLIST_MAX; i++)
182         l->pointer[i] = &l->object[i];
183     l->count = nobj;
184     return(l);
185 }
186 #endif /* unused */
187
188 /*
189  * Note that the low ivar values are reserved to provide
190  * interface compatibility with ISA drivers which can also
191  * attach to ACPI.
192  */
193 #define ACPI_IVAR_HANDLE        0x100
194 #define ACPI_IVAR_MAGIC         0x101
195 #define ACPI_IVAR_PRIVATE       0x102
196
197 static __inline ACPI_HANDLE
198 acpi_get_handle(device_t dev)
199 {
200     uintptr_t up;
201     ACPI_HANDLE h;
202
203     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up))
204         return(NULL);
205     h = (ACPI_HANDLE)up;
206     return(h);
207 }
208             
209 static __inline int
210 acpi_set_handle(device_t dev, ACPI_HANDLE h)
211 {
212     uintptr_t up;
213
214     up = (uintptr_t)h;
215     return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up));
216 }
217             
218 static __inline int
219 acpi_get_magic(device_t dev)
220 {
221     uintptr_t up;
222     int m;
223
224     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up))
225         return(0);
226     m = (int)up;
227     return(m);
228 }
229
230 static __inline int
231 acpi_set_magic(device_t dev, int m)
232 {
233     uintptr_t up;
234
235     up = (uintptr_t)m;
236     return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up));
237 }
238
239 static __inline void *
240 acpi_get_private(device_t dev)
241 {
242     uintptr_t up;
243     void *p;
244
245     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up))
246         return(NULL);
247     p = (void *)up;
248     return(p);
249 }
250
251 static __inline int
252 acpi_set_private(device_t dev, void *p)
253 {
254     uintptr_t up;
255
256     up = (uintptr_t)p;
257     return(BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up));
258 }
259
260 static __inline ACPI_OBJECT_TYPE
261 acpi_get_type(device_t dev)
262 {
263     ACPI_HANDLE         h;
264     ACPI_OBJECT_TYPE    t;
265
266     if ((h = acpi_get_handle(dev)) == NULL)
267         return(ACPI_TYPE_NOT_FOUND);
268     if (AcpiGetType(h, &t) != AE_OK)
269         return(ACPI_TYPE_NOT_FOUND);
270     return(t);
271 }
272
273 #ifdef ACPI_DEBUGGER
274 extern void             acpi_EnterDebugger(void);
275 #endif
276
277 #ifdef ACPI_DEBUG
278 #include <sys/cons.h>
279 #define STEP(x)         do {printf x, printf("\n"); cngetc();} while (0)
280 #else
281 #define STEP(x)
282 #endif
283
284 #define ACPI_VPRINT(dev, acpi_sc, x...) do {                            \
285         if (acpi_get_verbose(acpi_sc))                                  \
286                 device_printf(dev, x);                                  \
287 } while (0)
288
289 #define ACPI_DEVINFO_PRESENT(x) (((x) & 0x9) == 9)
290 extern BOOLEAN          acpi_DeviceIsPresent(device_t dev);
291 extern BOOLEAN          acpi_BatteryIsPresent(device_t dev);
292 extern BOOLEAN          acpi_MatchHid(device_t dev, char *hid);
293 extern ACPI_STATUS      acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result);
294 extern ACPI_BUFFER      *acpi_AllocBuffer(int size);
295 extern ACPI_STATUS      acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number);
296 extern ACPI_STATUS      acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number);
297 extern ACPI_STATUS      acpi_ForeachPackageObject(ACPI_OBJECT *obj, 
298                                                   void (* func)(ACPI_OBJECT *comp, void *arg),
299                                                   void *arg);
300 extern ACPI_STATUS      acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp);
301 extern ACPI_STATUS      acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res);
302 extern ACPI_STATUS      acpi_SetIntrModel(int model);
303
304 extern ACPI_STATUS      acpi_SetSleepState(struct acpi_softc *sc, int state);
305 extern ACPI_STATUS      acpi_Enable(struct acpi_softc *sc);
306 extern ACPI_STATUS      acpi_Disable(struct acpi_softc *sc);
307
308 struct acpi_parse_resource_set {
309     void        (* set_init)(device_t dev, void **context);
310     void        (* set_done)(device_t dev, void *context);
311     void        (* set_ioport)(device_t dev, void *context, u_int32_t base, u_int32_t length);
312     void        (* set_iorange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 
313                                 u_int32_t length, u_int32_t align);
314     void        (* set_memory)(device_t dev, void *context, u_int32_t base, u_int32_t length);
315     void        (* set_memoryrange)(device_t dev, void *context, u_int32_t low, u_int32_t high, 
316                                     u_int32_t length, u_int32_t align);
317     void        (* set_irq)(device_t dev, void *context, u_int32_t *irq, int cout);
318     void        (* set_drq)(device_t dev, void *context, u_int32_t *drq, int count);
319     void        (* set_start_dependant)(device_t dev, void *context, int preference);
320     void        (* set_end_dependant)(device_t dev, void *context);
321 };
322
323 extern struct acpi_parse_resource_set   acpi_res_parse_set;
324 extern ACPI_STATUS      acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
325                                              struct acpi_parse_resource_set *set);
326 /* XXX until Intel fix this in their headers, based on NEXT_RESOURCE */
327 #define ACPI_RESOURCE_NEXT(Res)      (ACPI_RESOURCE *)((UINT8 *) Res + Res->Length)
328
329 /* 
330  * ACPI event handling
331  */
332 extern UINT32           acpi_eventhandler_power_button_for_sleep(void *context);
333 extern UINT32           acpi_eventhandler_power_button_for_wakeup(void *context);
334 extern UINT32           acpi_eventhandler_sleep_button_for_sleep(void *context);
335 extern UINT32           acpi_eventhandler_sleep_button_for_wakeup(void *context);
336
337 #define ACPI_EVENT_PRI_FIRST      0
338 #define ACPI_EVENT_PRI_DEFAULT    10000
339 #define ACPI_EVENT_PRI_LAST       20000
340
341 typedef void (*acpi_event_handler_t)(void *, int);
342
343 EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t);
344 EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t);
345
346 /*
347  * Device power control.
348  */
349 extern ACPI_STATUS      acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state);
350
351 /* 
352  * Misc. 
353  */
354 static __inline struct acpi_softc *
355 acpi_device_get_parent_softc(device_t child)
356 {
357     device_t    parent;
358
359     parent = device_get_parent(child);
360     if (parent == NULL) {
361         return(NULL);
362     }
363     return(device_get_softc(parent));
364 }
365
366 static __inline int
367 acpi_get_verbose(struct acpi_softc *sc)
368 {
369     if (sc)
370         return(sc->acpi_verbose);
371     return(0);
372 }
373
374 extern char     *acpi_name(ACPI_HANDLE handle);
375 extern int      acpi_avoid(ACPI_HANDLE handle);
376 extern int      acpi_disabled(char *subsys);
377
378 extern void     acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable);
379 extern void     acpi_device_enable_wake_event(ACPI_HANDLE h);
380
381 extern int      acpi_machdep_init(device_t dev);
382 extern void     acpi_install_wakeup_handler(struct acpi_softc *sc);
383 extern int      acpi_sleep_machdep(struct acpi_softc *sc, int state);
384
385 /*
386  * Battery Abstraction.
387  */
388 struct acpi_battinfo;
389 struct acpi_battdesc;
390
391 extern int      acpi_battery_register(int, int);
392 extern int      acpi_battery_get_battinfo(int, struct acpi_battinfo *);
393 extern int      acpi_battery_get_units(void);
394 extern int      acpi_battery_get_info_expire(void);
395 extern int      acpi_battery_get_battdesc(int, struct acpi_battdesc *);
396
397 extern int      acpi_cmbat_get_battinfo(int, struct acpi_battinfo *);
398
399 /*
400  * AC adapter interface.
401  */
402
403 extern int      acpi_acad_get_acline(int *);
404
405 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
406 #ifndef ACPI_MAX_THREADS
407 #define ACPI_MAX_THREADS        3
408 #endif
409 #if ACPI_MAX_THREADS > 0
410 #define ACPI_USE_THREADS
411 #endif
412 #endif
413
414 #ifdef ACPI_USE_THREADS
415 /*
416  * ACPI task kernel thread initialization.
417  */
418 extern int      acpi_task_thread_init(void);
419 #endif
420