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