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