xref: /openbsd/sys/dev/acpi/amltypes.h (revision 50b7f1f5)
1 /* $OpenBSD: amltypes.h,v 1.50 2024/06/02 11:08:41 kettenis Exp $ */
2 /*
3  * Copyright (c) 2005 Jordan Hargrave <jordan@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef __DEV_ACPI_AMLTYPES_H__
19 #define __DEV_ACPI_AMLTYPES_H__
20 
21 /* AML Opcodes */
22 #define AMLOP_ZERO		0x00
23 #define AMLOP_ONE		0x01
24 #define AMLOP_ALIAS		0x06
25 #define AMLOP_NAME		0x08
26 #define AMLOP_BYTEPREFIX	0x0A
27 #define AMLOP_WORDPREFIX	0x0B
28 #define AMLOP_DWORDPREFIX	0x0C
29 #define AMLOP_STRINGPREFIX	0x0D
30 #define AMLOP_QWORDPREFIX	0x0E
31 #define AMLOP_SCOPE		0x10
32 #define AMLOP_BUFFER		0x11
33 #define AMLOP_PACKAGE		0x12
34 #define AMLOP_VARPACKAGE	0x13
35 #define AMLOP_METHOD		0x14
36 #define AMLOP_DUALNAMEPREFIX	0x2E
37 #define AMLOP_MULTINAMEPREFIX	0x2F
38 #define AMLOP_EXTPREFIX		0x5B
39 #define AMLOP_MUTEX		0x5B01
40 #define AMLOP_EVENT		0x5B02
41 #define AMLOP_CONDREFOF		0x5B12
42 #define AMLOP_CREATEFIELD	0x5B13
43 #define AMLOP_LOADTABLE		0x5B1F
44 #define AMLOP_LOAD		0x5B20
45 #define AMLOP_STALL		0x5B21
46 #define AMLOP_SLEEP		0x5B22
47 #define AMLOP_ACQUIRE		0x5B23
48 #define AMLOP_SIGNAL		0x5B24
49 #define AMLOP_WAIT		0x5B25
50 #define AMLOP_RESET		0x5B26
51 #define AMLOP_RELEASE		0x5B27
52 #define AMLOP_FROMBCD		0x5B28
53 #define AMLOP_TOBCD		0x5B29
54 #define AMLOP_UNLOAD		0x5B2A
55 #define AMLOP_REVISION		0x5B30
56 #define AMLOP_DEBUG		0x5B31
57 #define AMLOP_FATAL		0x5B32
58 #define AMLOP_TIMER		0x5B33
59 #define AMLOP_OPREGION		0x5B80
60 #define AMLOP_FIELD		0x5B81
61 #define AMLOP_DEVICE		0x5B82
62 #define AMLOP_PROCESSOR		0x5B83
63 #define AMLOP_POWERRSRC		0x5B84
64 #define AMLOP_THERMALZONE	0x5B85
65 #define AMLOP_INDEXFIELD	0x5B86
66 #define AMLOP_BANKFIELD		0x5B87
67 #define AMLOP_DATAREGION	0x5B88
68 #define AMLOP_ROOTCHAR		0x5C
69 #define AMLOP_PARENTPREFIX	0x5E
70 #define AMLOP_NAMECHAR		0x5F
71 #define AMLOP_LOCAL0		0x60
72 #define AMLOP_LOCAL1		0x61
73 #define AMLOP_LOCAL2		0x62
74 #define AMLOP_LOCAL3		0x63
75 #define AMLOP_LOCAL4		0x64
76 #define AMLOP_LOCAL5		0x65
77 #define AMLOP_LOCAL6		0x66
78 #define AMLOP_LOCAL7		0x67
79 #define AMLOP_ARG0		0x68
80 #define AMLOP_ARG1		0x69
81 #define AMLOP_ARG2		0x6A
82 #define AMLOP_ARG3		0x6B
83 #define AMLOP_ARG4		0x6C
84 #define AMLOP_ARG5		0x6D
85 #define AMLOP_ARG6		0x6E
86 #define AMLOP_STORE		0x70
87 #define AMLOP_REFOF		0x71
88 #define AMLOP_ADD		0x72
89 #define AMLOP_CONCAT		0x73
90 #define AMLOP_SUBTRACT		0x74
91 #define AMLOP_INCREMENT		0x75
92 #define AMLOP_DECREMENT		0x76
93 #define AMLOP_MULTIPLY		0x77
94 #define AMLOP_DIVIDE		0x78
95 #define AMLOP_SHL		0x79
96 #define AMLOP_SHR		0x7A
97 #define AMLOP_AND		0x7B
98 #define AMLOP_NAND		0x7C
99 #define AMLOP_OR		0x7D
100 #define AMLOP_NOR		0x7E
101 #define AMLOP_XOR		0x7F
102 #define AMLOP_NOT		0x80
103 #define AMLOP_FINDSETLEFTBIT	0x81
104 #define AMLOP_FINDSETRIGHTBIT	0x82
105 #define AMLOP_DEREFOF		0x83
106 #define AMLOP_CONCATRES		0x84
107 #define AMLOP_MOD		0x85
108 #define AMLOP_NOTIFY		0x86
109 #define AMLOP_SIZEOF		0x87
110 #define AMLOP_INDEX		0x88
111 #define AMLOP_MATCH		0x89
112 #define AMLOP_CREATEDWORDFIELD	0x8A
113 #define AMLOP_CREATEWORDFIELD	0x8B
114 #define AMLOP_CREATEBYTEFIELD	0x8C
115 #define AMLOP_CREATEBITFIELD	0x8D
116 #define AMLOP_OBJECTTYPE	0x8E
117 #define AMLOP_CREATEQWORDFIELD	0x8F
118 #define AMLOP_LAND		0x90
119 #define AMLOP_LOR		0x91
120 #define AMLOP_LNOT		0x92
121 #define AMLOP_LNOTEQUAL		0x9293
122 #define AMLOP_LLESSEQUAL	0x9294
123 #define AMLOP_LGREATEREQUAL	0x9295
124 #define AMLOP_LEQUAL		0x93
125 #define AMLOP_LGREATER		0x94
126 #define AMLOP_LLESS		0x95
127 #define AMLOP_TOBUFFER		0x96
128 #define AMLOP_TODECSTRING	0x97
129 #define AMLOP_TOHEXSTRING	0x98
130 #define AMLOP_TOINTEGER		0x99
131 #define AMLOP_TOSTRING		0x9C
132 #define AMLOP_COPYOBJECT	0x9D
133 #define AMLOP_MID		0x9E
134 #define AMLOP_CONTINUE		0x9F
135 #define AMLOP_IF		0xA0
136 #define AMLOP_ELSE		0xA1
137 #define AMLOP_WHILE		0xA2
138 #define AMLOP_NOP		0xA3
139 #define AMLOP_RETURN		0xA4
140 #define AMLOP_BREAK		0xA5
141 #define AMLOP_BREAKPOINT	0xCC
142 #define AMLOP_ONES		0xFF
143 
144 #define AMLOP_FIELDUNIT		0xFE00
145 #define AML_ANYINT		0xFF00
146 
147 /*
148  * Comparison types for Match()
149  *
150  *  true,==,<=,<,>=,>
151  */
152 #define AML_MATCH_TR		0
153 #define AML_MATCH_EQ		1
154 #define AML_MATCH_LE		2
155 #define AML_MATCH_LT		3
156 #define AML_MATCH_GE		4
157 #define AML_MATCH_GT		5
158 
159 /* Defined types for ObjectType() */
160 enum aml_objecttype {
161 	AML_OBJTYPE_UNINITIALIZED = 0,
162 	AML_OBJTYPE_INTEGER,
163 	AML_OBJTYPE_STRING,
164 	AML_OBJTYPE_BUFFER,
165 	AML_OBJTYPE_PACKAGE,
166 	AML_OBJTYPE_FIELDUNIT,
167 	AML_OBJTYPE_DEVICE,
168 	AML_OBJTYPE_EVENT,
169 	AML_OBJTYPE_METHOD,
170 	AML_OBJTYPE_MUTEX,
171 	AML_OBJTYPE_OPREGION,
172 	AML_OBJTYPE_POWERRSRC,
173 	AML_OBJTYPE_PROCESSOR,
174 	AML_OBJTYPE_THERMZONE,
175 	AML_OBJTYPE_BUFFERFIELD,
176 	AML_OBJTYPE_DDBHANDLE,
177 	AML_OBJTYPE_DEBUGOBJ,
178 
179 	AML_OBJTYPE_NAMEREF = 0x100,
180 	AML_OBJTYPE_OBJREF,
181 	AML_OBJTYPE_SCOPE,
182 	AML_OBJTYPE_NOTARGET,
183 	AML_OBJTYPE_HEXSTRING,
184 	AML_OBJTYPE_DECSTRING,
185 };
186 
187 /* AML Opcode Arguments */
188 #define AML_ARG_INTEGER		'i'
189 #define AML_ARG_BYTE		'b'
190 #define AML_ARG_WORD		'w'
191 #define AML_ARG_DWORD		'd'
192 #define AML_ARG_QWORD		'q'
193 #define AML_ARG_IMPBYTE		'!'
194 #define AML_ARG_OBJLEN		'p'
195 #define AML_ARG_STRING		'a'
196 #define AML_ARG_BYTELIST	'B'
197 #define AML_ARG_REVISION	'R'
198 
199 #define AML_ARG_METHOD		'M'
200 #define AML_ARG_NAMESTRING	'N'
201 #define AML_ARG_NAMEREF		'n'
202 #define AML_ARG_FIELDLIST	'F'
203 #define AML_ARG_FLAG		'f'
204 
205 #define AML_ARG_DATAOBJLIST	'O'
206 #define AML_ARG_DATAOBJ		'o'
207 
208 #define AML_ARG_SIMPLENAME	's'
209 #define AML_ARG_SUPERNAME	'S'
210 
211 #define AML_ARG_TERMOBJLIST	'T'
212 #define AML_ARG_TERMOBJ		't'
213 
214 #define AML_ARG_IFELSE          'I'
215 #define AML_ARG_BUFFER          'B'
216 #define AML_ARG_SEARCHNAME      'n'
217 #define AML_ARG_CREATENAME      'N'
218 #define AML_ARG_STKARG          'A'
219 #define AML_ARG_STKLOCAL        'L'
220 #define AML_ARG_DEBUG           'D'
221 #define AML_ARG_CONST           'c'
222 #define AML_ARG_TARGET          'r'
223 
224 #define AML_METHOD_ARGCOUNT(v)	 (((v) >> 0) & 0x7)
225 #define AML_METHOD_SERIALIZED(v) (((v) >> 3) & 0x1)
226 #define AML_METHOD_SYNCLEVEL(v)	 (((v) >> 4) & 0xF)
227 
228 #define AML_FIELD_ACCESSMASK	0x0F
229 #define AML_FIELD_SETATTR(f,t,a) (((f) & 0xF0) | ((t) & 0xF) | ((a)<<8))
230 #define AML_FIELD_ACCESS(v)	(((v) >> 0) & 0xF)
231 # define AML_FIELD_ANYACC	0x0
232 # define AML_FIELD_BYTEACC	0x1
233 # define AML_FIELD_WORDACC	0x2
234 # define AML_FIELD_DWORDACC	0x3
235 # define AML_FIELD_QWORDACC	0x4
236 # define AML_FIELD_BUFFERACC	0x5
237 #define AML_FIELD_LOCK(v)	(((v) >> 4) & 0x1)
238 # define AML_FIELD_LOCK_OFF	0x0
239 # define AML_FIELD_LOCK_ON	0x1
240 #define AML_FIELD_UPDATE(v)	(((v) >> 5) & 0x3)
241 # define AML_FIELD_PRESERVE	0x0
242 # define AML_FIELD_WRITEASONES	0x1
243 # define AML_FIELD_WRITEASZEROES 0x2
244 #define AML_FIELD_ATTR(v)	((v) >> 8)
245 #define AML_FIELD_RESERVED	0x00
246 /* XXX fix this name */
247 #define AML_FIELD_ATTR__		0x01
248 
249 struct aml_scope;
250 struct aml_node;
251 
252 struct aml_waitq {
253 	struct aml_scope          *scope;
254 	SIMPLEQ_ENTRY(aml_waitq)   link;
255 };
256 SIMPLEQ_HEAD(aml_waitq_head, aml_waitq);
257 
258 /* AML Object Value */
259 struct aml_value {
260 	int	type;
261 	int	length;
262 	int	refcnt;
263 	int	stack;
264 	struct aml_node *node;
265 	union {
266 		int64_t		vinteger;
267 		char		*vstring;
268 		uint8_t		*vbuffer;
269 		struct aml_value **vpackage;
270 		struct {
271 			uint8_t		iospace;
272 			uint64_t	iobase;
273 			uint32_t	iolen;
274 			int		flag;
275 		} vopregion;
276 		struct {
277 			int		flags;
278 			uint8_t		*start;
279 			uint8_t		*end;
280 			struct aml_value *(*fneval)(struct aml_scope *, struct aml_value *);
281 			uint8_t	        *base;
282 		} vmethod;
283 		struct {
284 			uint16_t	 type;
285 			uint16_t	 flags;
286 			uint32_t	 bitpos;
287 			uint32_t	 bitlen;
288 			struct aml_value *ref1;
289 			struct aml_value *ref2;
290 			int		 ref3;
291 		} vfield;
292 		struct {
293 			uint8_t		proc_id;
294 			uint32_t	proc_addr;
295 			uint8_t		proc_len;
296 		} vprocessor;
297 		struct {
298 			int		type;
299 			int		index;
300 			struct aml_value *ref;
301 		} vobjref;
302 		struct {
303 			uint8_t		pwr_level;
304 			uint16_t	pwr_order;
305 		} vpowerrsrc;
306 		struct acpi_mutex	*vmutex;
307 		struct {
308 			uint8_t	         *name;
309 			struct aml_node  *node;
310 		} vnameref;
311 		struct {
312 			int               synclvl;
313 			int               savelvl;
314 			int               count;
315 			char              ownername[5];
316 			struct aml_scope *owner;
317 			struct aml_waitq_head    waiters;
318 		} Vmutex;
319 		struct {
320 			int               state;
321 			struct aml_waitq_head    waiters;
322 		} Vevent;
323 	} _;
324 };
325 
326 #define v_nameref		_.vbuffer
327 #define v_objref		_.vobjref
328 #define v_integer		_.vinteger
329 #define v_string		_.vstring
330 #define v_buffer		_.vbuffer
331 #define v_package		_.vpackage
332 #define v_field			_.vfield
333 #define v_opregion		_.vopregion
334 #define v_method		_.vmethod
335 #define v_processor		_.vprocessor
336 #define v_powerrsrc		_.vpowerrsrc
337 #define v_mutex			_.vmutex
338 #define v_mtx                   _.Vmutex
339 #define v_evt                   _.Vevent
340 
341 #define xaml_intval(v)		((v)->v_integer)
342 #define aml_strlen(v)		((v)->length)
343 #define aml_strval(v)		((v)->v_string ? (v)->v_string : "bad string")
344 #define aml_buflen(v)		((v)->length)
345 #define aml_bufval(v)		((v)->v_buffer)
346 #define aml_pkglen(v)		((v)->length)
347 #define aml_pkgval(v,i)		(&(v)->v_package[(i)])
348 
349 struct acpi_pci {
350 	TAILQ_ENTRY(acpi_pci)		next;
351 
352 	struct aml_node			*node;
353 	struct device			*device;
354 
355 	int				sub;
356 	int				seg;
357 	int				bus;
358 	int				dev;
359 	int				fun;
360 
361 	int				_s0w;
362 	int				_s3d;
363 	int				_s3w;
364 	int				_s4d;
365 	int				_s4w;
366 };
367 
368 struct acpi_gpio {
369 	void	*cookie;
370 	int	(*read_pin)(void *, int);
371 	void	(*write_pin)(void *, int, int);
372 	void	(*intr_establish)(void *, int, int, int (*)(void *), void *);
373 	void	(*intr_enable)(void *, int);
374 	void	(*intr_disable)(void *, int);
375 };
376 
377 struct i2c_controller;
378 
379 struct aml_node {
380 	struct aml_node *parent;
381 
382 	SIMPLEQ_HEAD(,aml_node)	son;
383 	SIMPLEQ_ENTRY(aml_node)	sib;
384 
385 	int		attached;
386 
387 	char		name[5];
388 	uint16_t	opcode;
389 	uint8_t		*start;
390 	uint8_t		*end;
391 
392 	struct aml_value *value;
393 	struct acpi_pci *pci;
394 	struct acpi_gpio *gpio;
395 	struct i2c_controller *i2c;
396 };
397 
398 #define aml_bitmask(n)		(1L << ((n) & 0x7))
399 #define aml_bitpos(n)		((n)&0x7)
400 #define aml_bytepos(n)		((n)>>3)
401 #define aml_bytelen(n)		(((n)+7)>>3)
402 #define aml_bytealigned(x)	!((x)&0x7)
403 
404 #define AML_NO_TIMEOUT		0xffff
405 
406 #endif /* __DEV_ACPI_AMLTYPES_H__ */
407