xref: /freebsd/sys/dev/acpica/acpivar.h (revision 2357939bc239bd5334a169b62313806178dd8f30)
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 #if __FreeBSD_version < 500000
43 typedef vm_offset_t vm_paddr_t;
44 #endif
45 
46 struct acpi_softc {
47     device_t		acpi_dev;
48     dev_t		acpi_dev_t;
49 
50     struct resource	*acpi_irq;
51     int			acpi_irq_rid;
52     void		*acpi_irq_handle;
53 
54     int			acpi_enabled;
55     int			acpi_sstate;
56     int			acpi_sleep_disabled;
57 
58     struct sysctl_ctx_list acpi_sysctl_ctx;
59     struct sysctl_oid	*acpi_sysctl_tree;
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     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 #else
104 # define ACPI_LOCK
105 # define ACPI_UNLOCK
106 # define ACPI_ASSERTLOCK
107 # define ACPI_LOCK_DECL
108 #endif
109 
110 /*
111  * ACPI CA does not define layers for non-ACPI CA drivers.
112  * We define some here within the range provided.
113  */
114 #define	ACPI_AC_ADAPTER		0x00010000
115 #define	ACPI_BATTERY		0x00020000
116 #define	ACPI_BUS		0x00040000
117 #define	ACPI_BUTTON		0x00080000
118 #define	ACPI_EC			0x00100000
119 #define	ACPI_FAN		0x00200000
120 #define	ACPI_POWERRES		0x00400000
121 #define	ACPI_PROCESSOR		0x00800000
122 #define	ACPI_THERMAL		0x01000000
123 #define	ACPI_TIMER		0x02000000
124 #define	ACPI_ASUS		0x04000000
125 
126 /*
127  * Constants for different interrupt models used with acpi_SetIntrModel().
128  */
129 #define	ACPI_INTR_PIC		0
130 #define	ACPI_INTR_APIC		1
131 #define	ACPI_INTR_SAPIC		2
132 
133 /*
134  * Note that the low ivar values are reserved to provide
135  * interface compatibility with ISA drivers which can also
136  * attach to ACPI.
137  */
138 #define ACPI_IVAR_HANDLE	0x100
139 #define ACPI_IVAR_MAGIC		0x101
140 #define ACPI_IVAR_PRIVATE	0x102
141 
142 static __inline ACPI_HANDLE
143 acpi_get_handle(device_t dev)
144 {
145     uintptr_t up;
146 
147     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up))
148 	return (NULL);
149     return ((ACPI_HANDLE)up);
150 }
151 
152 static __inline int
153 acpi_set_handle(device_t dev, ACPI_HANDLE h)
154 {
155     uintptr_t up;
156 
157     up = (uintptr_t)h;
158     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up));
159 }
160 
161 static __inline int
162 acpi_get_magic(device_t dev)
163 {
164     uintptr_t up;
165 
166     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up))
167 	return(0);
168     return ((int)up);
169 }
170 
171 static __inline int
172 acpi_set_magic(device_t dev, int m)
173 {
174     uintptr_t up;
175 
176     up = (uintptr_t)m;
177     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up));
178 }
179 
180 static __inline void *
181 acpi_get_private(device_t dev)
182 {
183     uintptr_t up;
184 
185     if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up))
186 	return (NULL);
187     return ((void *)up);
188 }
189 
190 static __inline int
191 acpi_set_private(device_t dev, void *p)
192 {
193     uintptr_t up;
194 
195     up = (uintptr_t)p;
196     return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up));
197 }
198 
199 static __inline ACPI_OBJECT_TYPE
200 acpi_get_type(device_t dev)
201 {
202     ACPI_HANDLE		h;
203     ACPI_OBJECT_TYPE	t;
204 
205     if ((h = acpi_get_handle(dev)) == NULL)
206 	return (ACPI_TYPE_NOT_FOUND);
207     if (AcpiGetType(h, &t) != AE_OK)
208 	return (ACPI_TYPE_NOT_FOUND);
209     return (t);
210 }
211 
212 #ifdef ACPI_DEBUGGER
213 extern void		acpi_EnterDebugger(void);
214 #endif
215 
216 #ifdef ACPI_DEBUG
217 #include <sys/cons.h>
218 #define STEP(x)		do {printf x, printf("\n"); cngetc();} while (0)
219 #else
220 #define STEP(x)
221 #endif
222 
223 #define ACPI_VPRINT(dev, acpi_sc, x...) do {			\
224     if (acpi_get_verbose(acpi_sc))				\
225 	device_printf(dev, x);					\
226 } while (0)
227 
228 #define ACPI_DEVINFO_PRESENT(x)	(((x) & 0x9) == 9)
229 extern BOOLEAN		acpi_DeviceIsPresent(device_t dev);
230 extern BOOLEAN		acpi_BatteryIsPresent(device_t dev);
231 extern BOOLEAN		acpi_MatchHid(device_t dev, char *hid);
232 extern ACPI_STATUS	acpi_GetHandleInScope(ACPI_HANDLE parent, char *path,
233 					      ACPI_HANDLE *result);
234 extern uint32_t		acpi_TimerDelta(uint32_t end, uint32_t start);
235 extern ACPI_BUFFER	*acpi_AllocBuffer(int size);
236 extern ACPI_STATUS	acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp,
237 						    UINT32 *number);
238 extern ACPI_STATUS	acpi_GetInteger(ACPI_HANDLE handle, char *path,
239 					UINT32 *number);
240 extern ACPI_STATUS	acpi_SetInteger(ACPI_HANDLE handle, char *path,
241 					UINT32 number);
242 extern ACPI_STATUS	acpi_ForeachPackageObject(ACPI_OBJECT *obj,
243 				void (*func)(ACPI_OBJECT *comp, void *arg),
244 				void *arg);
245 extern ACPI_STATUS	acpi_FindIndexedResource(ACPI_BUFFER *buf, int index,
246 						 ACPI_RESOURCE **resp);
247 extern ACPI_STATUS	acpi_AppendBufferResource(ACPI_BUFFER *buf,
248 						  ACPI_RESOURCE *res);
249 extern ACPI_STATUS	acpi_OverrideInterruptLevel(UINT32 InterruptNumber);
250 extern ACPI_STATUS	acpi_SetIntrModel(int model);
251 extern ACPI_STATUS	acpi_SetSleepState(struct acpi_softc *sc, int state);
252 extern ACPI_STATUS	acpi_Startup(void);
253 extern ACPI_STATUS	acpi_Enable(struct acpi_softc *sc);
254 extern ACPI_STATUS	acpi_Disable(struct acpi_softc *sc);
255 extern void		acpi_UserNotify(const char *subsystem, ACPI_HANDLE h,
256 					uint8_t notify);
257 struct resource *	acpi_bus_alloc_gas(device_t dev, int *rid,
258 					   ACPI_GENERIC_ADDRESS *gas);
259 
260 struct acpi_parse_resource_set {
261     void	(*set_init)(device_t dev, void *arg, void **context);
262     void	(*set_done)(device_t dev, void *context);
263     void	(*set_ioport)(device_t dev, void *context, u_int32_t base,
264 			      u_int32_t length);
265     void	(*set_iorange)(device_t dev, void *context,
266 			       u_int32_t low, u_int32_t high,
267 			       u_int32_t length, u_int32_t align);
268     void	(*set_memory)(device_t dev, void *context, u_int32_t base,
269 			      u_int32_t length);
270     void	(*set_memoryrange)(device_t dev, void *context, u_int32_t low,
271 				   u_int32_t high, u_int32_t length,
272 				   u_int32_t align);
273     void	(*set_irq)(device_t dev, void *context, u_int32_t *irq,
274 			   int count, int trig, int pol);
275     void	(*set_drq)(device_t dev, void *context, u_int32_t *drq,
276 			   int count);
277     void	(*set_start_dependant)(device_t dev, void *context,
278 				       int preference);
279     void	(*set_end_dependant)(device_t dev, void *context);
280 };
281 
282 extern struct acpi_parse_resource_set	acpi_res_parse_set;
283 extern ACPI_STATUS	acpi_parse_resources(device_t dev, ACPI_HANDLE handle,
284 			    struct acpi_parse_resource_set *set, void *arg);
285 
286 /* ACPI event handling */
287 extern UINT32	acpi_event_power_button_sleep(void *context);
288 extern UINT32	acpi_event_power_button_wake(void *context);
289 extern UINT32	acpi_event_sleep_button_sleep(void *context);
290 extern UINT32	acpi_event_sleep_button_wake(void *context);
291 
292 #define ACPI_EVENT_PRI_FIRST      0
293 #define ACPI_EVENT_PRI_DEFAULT    10000
294 #define ACPI_EVENT_PRI_LAST       20000
295 
296 typedef void (*acpi_event_handler_t)(void *, int);
297 
298 EVENTHANDLER_DECLARE(acpi_sleep_event, acpi_event_handler_t);
299 EVENTHANDLER_DECLARE(acpi_wakeup_event, acpi_event_handler_t);
300 
301 /* Device power control. */
302 extern ACPI_STATUS	acpi_pwr_switch_consumer(ACPI_HANDLE consumer,
303 						 int state);
304 
305 /* Misc. */
306 static __inline struct acpi_softc *
307 acpi_device_get_parent_softc(device_t child)
308 {
309     device_t	parent;
310 
311     parent = device_get_parent(child);
312     if (parent == NULL)
313 	return (NULL);
314     return (device_get_softc(parent));
315 }
316 
317 static __inline int
318 acpi_get_verbose(struct acpi_softc *sc)
319 {
320     if (sc)
321 	return (sc->acpi_verbose);
322     return (0);
323 }
324 
325 extern char	*acpi_name(ACPI_HANDLE handle);
326 extern int	acpi_avoid(ACPI_HANDLE handle);
327 extern int	acpi_disabled(char *subsys);
328 extern void	acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable);
329 extern void	acpi_device_enable_wake_event(ACPI_HANDLE h);
330 extern int	acpi_machdep_init(device_t dev);
331 extern void	acpi_install_wakeup_handler(struct acpi_softc *sc);
332 extern int	acpi_sleep_machdep(struct acpi_softc *sc, int state);
333 
334 /* Battery Abstraction. */
335 struct acpi_battinfo;
336 struct acpi_battdesc;
337 
338 extern int	acpi_battery_register(int, int);
339 extern int	acpi_battery_get_battinfo(int, struct acpi_battinfo *);
340 extern int	acpi_battery_get_units(void);
341 extern int	acpi_battery_get_info_expire(void);
342 extern int	acpi_battery_get_battdesc(int, struct acpi_battdesc *);
343 
344 extern int	acpi_cmbat_get_battinfo(int, struct acpi_battinfo *);
345 
346 /* Embedded controller. */
347 extern void	acpi_ec_ecdt_probe(device_t);
348 
349 /* AC adapter interface. */
350 extern int	acpi_acad_get_acline(int *);
351 
352 /* Package manipulation convenience functions. */
353 #define ACPI_PKG_VALID(pkg, size)				\
354     ((pkg) != NULL && (pkg)->Type == ACPI_TYPE_PACKAGE &&	\
355      (pkg)->Package.Count >= (size))
356 int		acpi_PkgInt(ACPI_OBJECT *res, int idx, ACPI_INTEGER *dst);
357 int		acpi_PkgInt32(ACPI_OBJECT *res, int idx, uint32_t *dst);
358 int		acpi_PkgStr(ACPI_OBJECT *res, int idx, void *dst, size_t size);
359 int		acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *rid,
360 			    struct resource **dst);
361 ACPI_HANDLE	acpi_GetReference(ACPI_HANDLE scope, ACPI_OBJECT *obj);
362 
363 #if __FreeBSD_version >= 500000
364 #ifndef ACPI_MAX_THREADS
365 #define ACPI_MAX_THREADS	3
366 #endif
367 #if ACPI_MAX_THREADS > 0
368 #define ACPI_USE_THREADS
369 #endif
370 #endif
371 
372 #ifdef ACPI_USE_THREADS
373 /* ACPI task kernel thread initialization. */
374 extern int	acpi_task_thread_init(void);
375 #endif
376