summaryrefslogtreecommitdiff
path: root/lib/acpi/acpigen.c
blob: 4fd29ccb81e9ee6091027e1601d36e36cd6a29fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// SPDX-License-Identifier: GPL-2.0
/*
 * Generation of ACPI (Advanced Configuration and Power Interface) tables
 *
 * Copyright 2019 Google LLC
 * Mostly taken from coreboot
 */

#define LOG_CATEGORY LOGC_ACPI

#include <common.h>
#include <dm.h>
#include <log.h>
#include <uuid.h>
#include <acpi/acpigen.h>
#include <dm/acpi.h>

u8 *acpigen_get_current(struct acpi_ctx *ctx)
{
	return ctx->current;
}

void acpigen_emit_byte(struct acpi_ctx *ctx, uint data)
{
	*(u8 *)ctx->current++ = data;
}

void acpigen_emit_word(struct acpi_ctx *ctx, uint data)
{
	acpigen_emit_byte(ctx, data & 0xff);
	acpigen_emit_byte(ctx, (data >> 8) & 0xff);
}

void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
{
	/* Output the value in little-endian format */
	acpigen_emit_byte(ctx, data & 0xff);
	acpigen_emit_byte(ctx, (data >> 8) & 0xff);
	acpigen_emit_byte(ctx, (data >> 16) & 0xff);
	acpigen_emit_byte(ctx, (data >> 24) & 0xff);
}

/*
 * Maximum length for an ACPI object generated by this code,
 *
 * If you need to change this, change acpigen_write_len_f(ctx) and
 * acpigen_pop_len(ctx)
 */
#define ACPIGEN_MAXLEN 0xfffff

void acpigen_write_len_f(struct acpi_ctx *ctx)
{
	assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
	ctx->len_stack[ctx->ltop++] = ctx->current;
	acpigen_emit_byte(ctx, 0);
	acpigen_emit_byte(ctx, 0);
	acpigen_emit_byte(ctx, 0);
}

void acpigen_pop_len(struct acpi_ctx *ctx)
{
	int len;
	char *p;

	assert(ctx->ltop > 0);
	p = ctx->len_stack[--ctx->ltop];
	len = ctx->current - (void *)p;
	assert(len <= ACPIGEN_MAXLEN);
	/* generate store length for 0xfffff max */
	p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
	p[1] = len >> 4 & 0xff;
	p[2] = len >> 12 & 0xff;
}

void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op)
{
	acpigen_emit_byte(ctx, EXT_OP_PREFIX);
	acpigen_emit_byte(ctx, op);
}

char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
{
	char *p;

	acpigen_emit_byte(ctx, PACKAGE_OP);
	acpigen_write_len_f(ctx);
	p = ctx->current;
	acpigen_emit_byte(ctx, nr_el);

	return p;
}

void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data)
{
	acpigen_emit_byte(ctx, BYTE_PREFIX);
	acpigen_emit_byte(ctx, data & 0xff);
}

void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data)
{
	acpigen_emit_byte(ctx, WORD_PREFIX);
	acpigen_emit_word(ctx, data);
}

void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data)
{
	acpigen_emit_byte(ctx, DWORD_PREFIX);
	acpigen_emit_dword(ctx, data);
}

void acpigen_write_qword(struct acpi_ctx *ctx, u64 data)
{
	acpigen_emit_byte(ctx, QWORD_PREFIX);
	acpigen_emit_dword(ctx, data & 0xffffffff);
	acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff);
}

void acpigen_write_zero(struct acpi_ctx *ctx)
{
	acpigen_emit_byte(ctx, ZERO_OP);
}

void acpigen_write_one(struct acpi_ctx *ctx)
{
	acpigen_emit_byte(ctx, ONE_OP);
}

void acpigen_write_integer(struct acpi_ctx *ctx, u64 data)
{
	if (data == 0)
		acpigen_write_zero(ctx);
	else if (data == 1)
		acpigen_write_one(ctx);
	else if (data <= 0xff)
		acpigen_write_byte(ctx, (unsigned char)data);
	else if (data <= 0xffff)
		acpigen_write_word(ctx, (unsigned int)data);
	else if (data <= 0xffffffff)
		acpigen_write_dword(ctx, (unsigned int)data);
	else
		acpigen_write_qword(ctx, data);
}

void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
{
	int i;

	for (i = 0; i < size; i++)
		acpigen_emit_byte(ctx, data[i]);
}

void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
{
	acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
	acpigen_emit_byte(ctx, '\0');
}

void acpigen_write_string(struct acpi_ctx *ctx, const char *str)
{
	acpigen_emit_byte(ctx, STRING_PREFIX);
	acpigen_emit_string(ctx, str);
}

/*
 * The naming conventions for ACPI namespace names are a bit tricky as
 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
 * and "By convention, when an ASL compiler pads a name shorter than 4
 * characters, it is done so with trailing underscores ('_')".
 *
 * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details.
 */
static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx,
					   const char *name)
{
	const char *ptr;
	int i;

	for (i = 0, ptr = name; i < 4; i++) {
		if (!*ptr || *ptr == '.')
			acpigen_emit_byte(ctx, '_');
		else
			acpigen_emit_byte(ctx, *ptr++);
	}
}

static void acpigen_emit_double_namestring(struct acpi_ctx *ctx,
					   const char *name, int dotpos)
{
	acpigen_emit_byte(ctx, DUAL_NAME_PREFIX);
	acpigen_emit_simple_namestring(ctx, name);
	acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]);
}

static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx,
					  const char *name)
{
	unsigned char *pathlen;
	int count = 0;

	acpigen_emit_byte(ctx, MULTI_NAME_PREFIX);
	pathlen = ctx->current;
	acpigen_emit_byte(ctx, 0);

	while (*name) {
		acpigen_emit_simple_namestring(ctx, name);
		/* find end or next entity */
		while (*name != '.' && *name)
			name++;
		/* forward to next */
		if (*name == '.')
			name++;
		count++;
	}

	*pathlen = count;
}

void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath)
{
	int dotcount;
	int dotpos;
	int i;

	/* We can start with a '\' */
	if (*namepath == '\\') {
		acpigen_emit_byte(ctx, '\\');
		namepath++;
	}

	/* And there can be any number of '^' */
	while (*namepath == '^') {
		acpigen_emit_byte(ctx, '^');
		namepath++;
	}

	for (i = 0, dotcount = 0; namepath[i]; i++) {
		if (namepath[i] == '.') {
			dotcount++;
			dotpos = i;
		}
	}

	/* If we have only \\ or only ^* then we need to add a null name */
	if (!*namepath)
		acpigen_emit_byte(ctx, ZERO_OP);
	else if (dotcount == 0)
		acpigen_emit_simple_namestring(ctx, namepath);
	else if (dotcount == 1)
		acpigen_emit_double_namestring(ctx, namepath, dotpos);
	else
		acpigen_emit_multi_namestring(ctx, namepath);
}

void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath)
{
	acpigen_emit_byte(ctx, NAME_OP);
	acpigen_emit_namestring(ctx, namepath);
}

static void acpigen_write_method_internal(struct acpi_ctx *ctx,
					  const char *name, uint flags)
{
	acpigen_emit_byte(ctx, METHOD_OP);
	acpigen_write_len_f(ctx);
	acpigen_emit_namestring(ctx, name);
	acpigen_emit_byte(ctx, flags);
}

/* Method (name, nargs, NotSerialized) */
void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs)
{
	acpigen_write_method_internal(ctx, name,
				      nargs & ACPI_METHOD_NARGS_MASK);
}

/* Method (name, nargs, Serialized) */
void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
				     int nargs)
{
	acpigen_write_method_internal(ctx, name,
				      (nargs & ACPI_METHOD_NARGS_MASK) |
				      ACPI_METHOD_SERIALIZED_MASK);
}

void acpigen_write_sta(struct acpi_ctx *ctx, uint status)
{
	/* Method (_STA, 0, NotSerialized) { Return (status) } */
	acpigen_write_method(ctx, "_STA", 0);
	acpigen_emit_byte(ctx, RETURN_OP);
	acpigen_write_byte(ctx, status);
	acpigen_pop_len(ctx);
}

/*
 * ToUUID(uuid)
 *
 * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the
 * bytes that make up a UUID Buffer object:
 *
 * UUID byte order for input to this function:
 *   aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
 *
 * UUID byte order output by this function:
 *   ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
 */
int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
{
	u8 buf[UUID_BIN_LEN];
	int ret;

	/* Parse UUID string into bytes */
	ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
	if (ret)
		return log_msg_ret("bad hex", -EINVAL);

	/* BufferOp */
	acpigen_emit_byte(ctx, BUFFER_OP);
	acpigen_write_len_f(ctx);

	/* Buffer length in bytes */
	acpigen_write_word(ctx, UUID_BIN_LEN);

	/* Output UUID in expected order */
	acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN);

	acpigen_pop_len(ctx);

	return 0;
}

/* Sleep (ms) */
void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
{
	acpigen_emit_ext_op(ctx, SLEEP_OP);
	acpigen_write_integer(ctx, sleep_ms);
}

void acpigen_write_store(struct acpi_ctx *ctx)
{
	acpigen_emit_byte(ctx, STORE_OP);
}

/* Or (arg1, arg2, res) */
void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
{
	acpigen_emit_byte(ctx, OR_OP);
	acpigen_emit_byte(ctx, arg1);
	acpigen_emit_byte(ctx, arg2);
	acpigen_emit_byte(ctx, res);
}

/* And (arg1, arg2, res) */
void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
{
	acpigen_emit_byte(ctx, AND_OP);
	acpigen_emit_byte(ctx, arg1);
	acpigen_emit_byte(ctx, arg2);
	acpigen_emit_byte(ctx, res);
}

/* Not (arg, res) */
void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res)
{
	acpigen_emit_byte(ctx, NOT_OP);
	acpigen_emit_byte(ctx, arg);
	acpigen_emit_byte(ctx, res);
}

/* Store (str, DEBUG) */
void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
{
	acpigen_write_store(ctx);
	acpigen_write_string(ctx, str);
	acpigen_emit_ext_op(ctx, DEBUG_OP);
}