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