blob: b9985075cde9cf74bf030936c6b38a7c6b0152ef [file] [log] [blame]
Simon Glass98528d42020-07-07 13:11:42 -06001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Generation of ACPI (Advanced Configuration and Power Interface) tables
4 *
5 * Copyright 2019 Google LLC
6 * Mostly taken from coreboot
7 */
8
9#define LOG_CATEGORY LOGC_ACPI
10
11#include <common.h>
12#include <dm.h>
Simon Glass0f277632020-07-07 13:11:50 -060013#include <log.h>
Simon Glass48342b02020-07-07 13:11:55 -060014#include <uuid.h>
Simon Glass98528d42020-07-07 13:11:42 -060015#include <acpi/acpigen.h>
Simon Glass8a200762020-07-07 13:12:01 -060016#include <acpi/acpi_device.h>
Simon Glass837127f2020-07-07 21:32:11 -060017#include <acpi/acpi_table.h>
Simon Glass98528d42020-07-07 13:11:42 -060018#include <dm/acpi.h>
19
Simon Glass412dc4b2020-09-22 12:45:11 -060020/* CPU path format */
21#define ACPI_CPU_STRING "\\_PR.CP%02d"
22
Simon Glass98528d42020-07-07 13:11:42 -060023u8 *acpigen_get_current(struct acpi_ctx *ctx)
24{
25 return ctx->current;
26}
27
28void acpigen_emit_byte(struct acpi_ctx *ctx, uint data)
29{
30 *(u8 *)ctx->current++ = data;
31}
32
33void acpigen_emit_word(struct acpi_ctx *ctx, uint data)
34{
35 acpigen_emit_byte(ctx, data & 0xff);
36 acpigen_emit_byte(ctx, (data >> 8) & 0xff);
37}
38
39void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
40{
41 /* Output the value in little-endian format */
42 acpigen_emit_byte(ctx, data & 0xff);
43 acpigen_emit_byte(ctx, (data >> 8) & 0xff);
44 acpigen_emit_byte(ctx, (data >> 16) & 0xff);
45 acpigen_emit_byte(ctx, (data >> 24) & 0xff);
46}
Simon Glass071c4a52020-07-07 13:11:45 -060047
Simon Glass0f277632020-07-07 13:11:50 -060048/*
49 * Maximum length for an ACPI object generated by this code,
50 *
51 * If you need to change this, change acpigen_write_len_f(ctx) and
52 * acpigen_pop_len(ctx)
53 */
54#define ACPIGEN_MAXLEN 0xfffff
55
56void acpigen_write_len_f(struct acpi_ctx *ctx)
57{
58 assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
59 ctx->len_stack[ctx->ltop++] = ctx->current;
60 acpigen_emit_byte(ctx, 0);
61 acpigen_emit_byte(ctx, 0);
62 acpigen_emit_byte(ctx, 0);
63}
64
65void acpigen_pop_len(struct acpi_ctx *ctx)
66{
67 int len;
68 char *p;
69
70 assert(ctx->ltop > 0);
71 p = ctx->len_stack[--ctx->ltop];
72 len = ctx->current - (void *)p;
73 assert(len <= ACPIGEN_MAXLEN);
74 /* generate store length for 0xfffff max */
75 p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
76 p[1] = len >> 4 & 0xff;
77 p[2] = len >> 12 & 0xff;
78}
79
Simon Glass9238fac2020-07-07 13:11:59 -060080void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op)
81{
82 acpigen_emit_byte(ctx, EXT_OP_PREFIX);
83 acpigen_emit_byte(ctx, op);
84}
85
Simon Glassedc26802020-07-07 13:11:51 -060086char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
87{
88 char *p;
89
90 acpigen_emit_byte(ctx, PACKAGE_OP);
91 acpigen_write_len_f(ctx);
92 p = ctx->current;
93 acpigen_emit_byte(ctx, nr_el);
94
95 return p;
96}
97
Simon Glass8715ce02020-07-07 13:11:52 -060098void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data)
99{
100 acpigen_emit_byte(ctx, BYTE_PREFIX);
101 acpigen_emit_byte(ctx, data & 0xff);
102}
103
104void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data)
105{
106 acpigen_emit_byte(ctx, WORD_PREFIX);
107 acpigen_emit_word(ctx, data);
108}
109
110void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data)
111{
112 acpigen_emit_byte(ctx, DWORD_PREFIX);
113 acpigen_emit_dword(ctx, data);
114}
115
116void acpigen_write_qword(struct acpi_ctx *ctx, u64 data)
117{
118 acpigen_emit_byte(ctx, QWORD_PREFIX);
119 acpigen_emit_dword(ctx, data & 0xffffffff);
120 acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff);
121}
122
123void acpigen_write_zero(struct acpi_ctx *ctx)
124{
125 acpigen_emit_byte(ctx, ZERO_OP);
126}
127
128void acpigen_write_one(struct acpi_ctx *ctx)
129{
130 acpigen_emit_byte(ctx, ONE_OP);
131}
132
133void acpigen_write_integer(struct acpi_ctx *ctx, u64 data)
134{
135 if (data == 0)
136 acpigen_write_zero(ctx);
137 else if (data == 1)
138 acpigen_write_one(ctx);
139 else if (data <= 0xff)
140 acpigen_write_byte(ctx, (unsigned char)data);
141 else if (data <= 0xffff)
142 acpigen_write_word(ctx, (unsigned int)data);
143 else if (data <= 0xffffffff)
144 acpigen_write_dword(ctx, (unsigned int)data);
145 else
146 acpigen_write_qword(ctx, data);
147}
148
Simon Glassb4df0782020-07-07 21:32:15 -0600149void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name)
150{
151 acpigen_write_name(ctx, name);
152 acpigen_write_zero(ctx);
153}
154
155void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name)
156{
157 acpigen_write_name(ctx, name);
158 acpigen_write_one(ctx);
159}
160
161void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val)
162{
163 acpigen_write_name(ctx, name);
164 acpigen_write_byte(ctx, val);
165}
166
167void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val)
168{
169 acpigen_write_name(ctx, name);
170 acpigen_write_word(ctx, val);
171}
172
173void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val)
174{
175 acpigen_write_name(ctx, name);
176 acpigen_write_dword(ctx, val);
177}
178
179void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val)
180{
181 acpigen_write_name(ctx, name);
182 acpigen_write_qword(ctx, val);
183}
184
185void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name, u64 val)
186{
187 acpigen_write_name(ctx, name);
188 acpigen_write_integer(ctx, val);
189}
190
191void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
192 const char *string)
193{
194 acpigen_write_name(ctx, name);
195 acpigen_write_string(ctx, string);
196}
197
Simon Glass071c4a52020-07-07 13:11:45 -0600198void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
199{
200 int i;
201
202 for (i = 0; i < size; i++)
203 acpigen_emit_byte(ctx, data[i]);
204}
205
206void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
207{
208 acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
209 acpigen_emit_byte(ctx, '\0');
210}
Simon Glass45d6d952020-07-07 13:11:53 -0600211
212void acpigen_write_string(struct acpi_ctx *ctx, const char *str)
213{
214 acpigen_emit_byte(ctx, STRING_PREFIX);
215 acpigen_emit_string(ctx, str);
216}
Simon Glass0dc3d512020-07-07 13:11:54 -0600217
218/*
219 * The naming conventions for ACPI namespace names are a bit tricky as
220 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
221 * and "By convention, when an ASL compiler pads a name shorter than 4
222 * characters, it is done so with trailing underscores ('_')".
223 *
224 * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details.
225 */
226static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx,
227 const char *name)
228{
229 const char *ptr;
230 int i;
231
232 for (i = 0, ptr = name; i < 4; i++) {
233 if (!*ptr || *ptr == '.')
234 acpigen_emit_byte(ctx, '_');
235 else
236 acpigen_emit_byte(ctx, *ptr++);
237 }
238}
239
240static void acpigen_emit_double_namestring(struct acpi_ctx *ctx,
241 const char *name, int dotpos)
242{
243 acpigen_emit_byte(ctx, DUAL_NAME_PREFIX);
244 acpigen_emit_simple_namestring(ctx, name);
245 acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]);
246}
247
248static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx,
249 const char *name)
250{
251 unsigned char *pathlen;
252 int count = 0;
253
254 acpigen_emit_byte(ctx, MULTI_NAME_PREFIX);
255 pathlen = ctx->current;
256 acpigen_emit_byte(ctx, 0);
257
258 while (*name) {
259 acpigen_emit_simple_namestring(ctx, name);
260 /* find end or next entity */
261 while (*name != '.' && *name)
262 name++;
263 /* forward to next */
264 if (*name == '.')
265 name++;
266 count++;
267 }
268
269 *pathlen = count;
270}
271
272void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath)
273{
274 int dotcount;
275 int dotpos;
276 int i;
277
278 /* We can start with a '\' */
279 if (*namepath == '\\') {
280 acpigen_emit_byte(ctx, '\\');
281 namepath++;
282 }
283
284 /* And there can be any number of '^' */
285 while (*namepath == '^') {
286 acpigen_emit_byte(ctx, '^');
287 namepath++;
288 }
289
290 for (i = 0, dotcount = 0; namepath[i]; i++) {
291 if (namepath[i] == '.') {
292 dotcount++;
293 dotpos = i;
294 }
295 }
296
297 /* If we have only \\ or only ^* then we need to add a null name */
298 if (!*namepath)
299 acpigen_emit_byte(ctx, ZERO_OP);
300 else if (dotcount == 0)
301 acpigen_emit_simple_namestring(ctx, namepath);
302 else if (dotcount == 1)
303 acpigen_emit_double_namestring(ctx, namepath, dotpos);
304 else
305 acpigen_emit_multi_namestring(ctx, namepath);
306}
307
308void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath)
309{
310 acpigen_emit_byte(ctx, NAME_OP);
311 acpigen_emit_namestring(ctx, namepath);
312}
Simon Glass48342b02020-07-07 13:11:55 -0600313
Simon Glass6fcdaf12020-07-07 21:32:10 -0600314void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope)
315{
316 acpigen_emit_byte(ctx, SCOPE_OP);
317 acpigen_write_len_f(ctx);
318 acpigen_emit_namestring(ctx, scope);
319}
320
Simon Glass9238fac2020-07-07 13:11:59 -0600321static void acpigen_write_method_internal(struct acpi_ctx *ctx,
322 const char *name, uint flags)
323{
324 acpigen_emit_byte(ctx, METHOD_OP);
325 acpigen_write_len_f(ctx);
326 acpigen_emit_namestring(ctx, name);
327 acpigen_emit_byte(ctx, flags);
328}
329
330/* Method (name, nargs, NotSerialized) */
331void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs)
332{
333 acpigen_write_method_internal(ctx, name,
334 nargs & ACPI_METHOD_NARGS_MASK);
335}
336
337/* Method (name, nargs, Serialized) */
338void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
339 int nargs)
340{
341 acpigen_write_method_internal(ctx, name,
342 (nargs & ACPI_METHOD_NARGS_MASK) |
343 ACPI_METHOD_SERIALIZED_MASK);
344}
345
Simon Glass412dc4b2020-09-22 12:45:11 -0600346void acpigen_write_processor(struct acpi_ctx *ctx, uint cpuindex,
347 u32 pblock_addr, uint pblock_len)
348{
349 /*
350 * Processor (\_PR.CPnn, cpuindex, pblock_addr, pblock_len)
351 * {
352 */
353 char pscope[16];
354
355 acpigen_emit_ext_op(ctx, PROCESSOR_OP);
356 acpigen_write_len_f(ctx);
357
358 snprintf(pscope, sizeof(pscope), ACPI_CPU_STRING, cpuindex);
359 acpigen_emit_namestring(ctx, pscope);
360 acpigen_emit_byte(ctx, cpuindex);
361 acpigen_emit_dword(ctx, pblock_addr);
362 acpigen_emit_byte(ctx, pblock_len);
363}
364
365void acpigen_write_processor_package(struct acpi_ctx *ctx,
366 const char *const name,
367 const uint first_core,
368 const uint core_count)
369{
370 uint i;
371 char pscope[16];
372
373 acpigen_write_name(ctx, name);
374 acpigen_write_package(ctx, core_count);
375 for (i = first_core; i < first_core + core_count; ++i) {
376 snprintf(pscope, sizeof(pscope), ACPI_CPU_STRING, i);
377 acpigen_emit_namestring(ctx, pscope);
378 }
379 acpigen_pop_len(ctx);
380}
381
382void acpigen_write_processor_cnot(struct acpi_ctx *ctx, const uint num_cores)
383{
384 int core_id;
385
386 acpigen_write_method(ctx, "\\_PR.CNOT", 1);
387 for (core_id = 0; core_id < num_cores; core_id++) {
388 char buffer[30];
389
390 snprintf(buffer, sizeof(buffer), ACPI_CPU_STRING, core_id);
391 acpigen_emit_byte(ctx, NOTIFY_OP);
392 acpigen_emit_namestring(ctx, buffer);
393 acpigen_emit_byte(ctx, ARG0_OP);
394 }
395 acpigen_pop_len(ctx);
396}
397
Simon Glassa186d932020-07-07 21:32:14 -0600398void acpigen_write_device(struct acpi_ctx *ctx, const char *name)
399{
400 acpigen_emit_ext_op(ctx, DEVICE_OP);
401 acpigen_write_len_f(ctx);
402 acpigen_emit_namestring(ctx, name);
403}
404
Simon Glass9238fac2020-07-07 13:11:59 -0600405void acpigen_write_sta(struct acpi_ctx *ctx, uint status)
406{
407 /* Method (_STA, 0, NotSerialized) { Return (status) } */
408 acpigen_write_method(ctx, "_STA", 0);
409 acpigen_emit_byte(ctx, RETURN_OP);
410 acpigen_write_byte(ctx, status);
411 acpigen_pop_len(ctx);
412}
413
Simon Glass837127f2020-07-07 21:32:11 -0600414static void acpigen_write_register(struct acpi_ctx *ctx,
415 const struct acpi_gen_regaddr *addr)
416{
417 /* See ACPI v6.3 section 6.4.3.7: Generic Register Descriptor */
418 acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_REGISTER);
419 acpigen_emit_byte(ctx, 0x0c); /* Register Length 7:0 */
420 acpigen_emit_byte(ctx, 0x00); /* Register Length 15:8 */
421 acpigen_emit_byte(ctx, addr->space_id);
422 acpigen_emit_byte(ctx, addr->bit_width);
423 acpigen_emit_byte(ctx, addr->bit_offset);
424 acpigen_emit_byte(ctx, addr->access_size);
425 acpigen_emit_dword(ctx, addr->addrl);
426 acpigen_emit_dword(ctx, addr->addrh);
427}
428
429void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx)
430{
431 /*
432 * A ResourceTemplate() is a Buffer() with a
433 * (Byte|Word|DWord) containing the length, followed by one or more
434 * resource items, terminated by the end tag.
435 * (small item 0xf, len 1)
436 */
437 acpigen_emit_byte(ctx, BUFFER_OP);
438 acpigen_write_len_f(ctx);
439 acpigen_emit_byte(ctx, WORD_PREFIX);
440 ctx->len_stack[ctx->ltop++] = ctx->current;
441
442 /*
443 * Add two dummy bytes for the ACPI word (keep aligned with the
444 * calculation in acpigen_write_resourcetemplate_footer() below)
445 */
446 acpigen_emit_byte(ctx, 0x00);
447 acpigen_emit_byte(ctx, 0x00);
448}
449
450void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx)
451{
452 char *p = ctx->len_stack[--ctx->ltop];
453 int len;
454 /*
455 * See ACPI v6.3 section 6.4.2.9: End Tag
456 * 0x79 <checksum>
457 * 0x00 is treated as a good checksum according to the spec
458 * and is what iasl generates.
459 */
460 acpigen_emit_byte(ctx, ACPI_END_TAG);
461 acpigen_emit_byte(ctx, 0x00);
462
463 /*
464 * Start counting past the 2-bytes length added in
465 * acpigen_write_resourcetemplate_header() above
466 */
467 len = (char *)ctx->current - (p + 2);
468
469 /* patch len word */
470 p[0] = len & 0xff;
471 p[1] = (len >> 8) & 0xff;
472
473 acpigen_pop_len(ctx);
474}
475
476void acpigen_write_register_resource(struct acpi_ctx *ctx,
477 const struct acpi_gen_regaddr *addr)
478{
479 acpigen_write_resourcetemplate_header(ctx);
480 acpigen_write_register(ctx, addr);
481 acpigen_write_resourcetemplate_footer(ctx);
482}
483
Simon Glass77dfd032020-09-22 12:44:56 -0600484void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level)
485{
486 /* Name (_PRW, Package () { wake, level } */
487 acpigen_write_name(ctx, "_PRW");
488 acpigen_write_package(ctx, 2);
489 acpigen_write_integer(ctx, wake);
490 acpigen_write_integer(ctx, level);
491 acpigen_pop_len(ctx);
492}
493
Simon Glass48342b02020-07-07 13:11:55 -0600494/*
495 * ToUUID(uuid)
496 *
497 * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the
498 * bytes that make up a UUID Buffer object:
499 *
500 * UUID byte order for input to this function:
501 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
502 *
503 * UUID byte order output by this function:
504 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
505 */
506int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
507{
508 u8 buf[UUID_BIN_LEN];
509 int ret;
510
511 /* Parse UUID string into bytes */
512 ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
513 if (ret)
514 return log_msg_ret("bad hex", -EINVAL);
515
516 /* BufferOp */
517 acpigen_emit_byte(ctx, BUFFER_OP);
518 acpigen_write_len_f(ctx);
519
520 /* Buffer length in bytes */
521 acpigen_write_word(ctx, UUID_BIN_LEN);
522
523 /* Output UUID in expected order */
524 acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN);
525
526 acpigen_pop_len(ctx);
527
528 return 0;
529}
Simon Glass9238fac2020-07-07 13:11:59 -0600530
Simon Glassd0f7d9b2020-07-07 13:12:00 -0600531void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
532 uint order, const char *const dev_states[],
533 size_t dev_states_count)
534{
535 size_t i;
536
537 for (i = 0; i < dev_states_count; i++) {
538 acpigen_write_name(ctx, dev_states[i]);
539 acpigen_write_package(ctx, 1);
540 acpigen_emit_simple_namestring(ctx, name);
541 acpigen_pop_len(ctx); /* Package */
542 }
543
544 acpigen_emit_ext_op(ctx, POWER_RES_OP);
545
546 acpigen_write_len_f(ctx);
547
548 acpigen_emit_simple_namestring(ctx, name);
549 acpigen_emit_byte(ctx, level);
550 acpigen_emit_word(ctx, order);
551}
552
Simon Glass9238fac2020-07-07 13:11:59 -0600553/* Sleep (ms) */
554void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
555{
556 acpigen_emit_ext_op(ctx, SLEEP_OP);
557 acpigen_write_integer(ctx, sleep_ms);
558}
559
560void acpigen_write_store(struct acpi_ctx *ctx)
561{
562 acpigen_emit_byte(ctx, STORE_OP);
563}
564
565/* Or (arg1, arg2, res) */
566void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
567{
568 acpigen_emit_byte(ctx, OR_OP);
569 acpigen_emit_byte(ctx, arg1);
570 acpigen_emit_byte(ctx, arg2);
571 acpigen_emit_byte(ctx, res);
572}
573
574/* And (arg1, arg2, res) */
575void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
576{
577 acpigen_emit_byte(ctx, AND_OP);
578 acpigen_emit_byte(ctx, arg1);
579 acpigen_emit_byte(ctx, arg2);
580 acpigen_emit_byte(ctx, res);
581}
582
583/* Not (arg, res) */
584void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res)
585{
586 acpigen_emit_byte(ctx, NOT_OP);
587 acpigen_emit_byte(ctx, arg);
588 acpigen_emit_byte(ctx, res);
589}
590
591/* Store (str, DEBUG) */
592void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
593{
594 acpigen_write_store(ctx);
595 acpigen_write_string(ctx, str);
596 acpigen_emit_ext_op(ctx, DEBUG_OP);
597}
Simon Glass8a200762020-07-07 13:12:01 -0600598
Simon Glassca727632020-09-22 12:44:57 -0600599void acpigen_write_if(struct acpi_ctx *ctx)
600{
601 acpigen_emit_byte(ctx, IF_OP);
602 acpigen_write_len_f(ctx);
603}
604
605void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val)
606{
607 acpigen_write_if(ctx);
608 acpigen_emit_byte(ctx, LEQUAL_OP);
609 acpigen_emit_byte(ctx, op);
610 acpigen_write_integer(ctx, val);
611}
612
613void acpigen_write_else(struct acpi_ctx *ctx)
614{
615 acpigen_emit_byte(ctx, ELSE_OP);
616 acpigen_write_len_f(ctx);
617}
618
619void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst)
620{
621 acpigen_emit_byte(ctx, TO_BUFFER_OP);
622 acpigen_emit_byte(ctx, src);
623 acpigen_emit_byte(ctx, dst);
624}
625
626void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst)
627{
628 acpigen_emit_byte(ctx, TO_INTEGER_OP);
629 acpigen_emit_byte(ctx, src);
630 acpigen_emit_byte(ctx, dst);
631}
632
633void acpigen_write_byte_buffer(struct acpi_ctx *ctx, u8 *arr, size_t size)
634{
635 size_t i;
636
637 acpigen_emit_byte(ctx, BUFFER_OP);
638 acpigen_write_len_f(ctx);
639 acpigen_write_integer(ctx, size);
640
641 for (i = 0; i < size; i++)
642 acpigen_emit_byte(ctx, arr[i]);
643
644 acpigen_pop_len(ctx);
645}
646
647void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
648 size_t size)
649{
650 acpigen_emit_byte(ctx, RETURN_OP);
651 acpigen_write_byte_buffer(ctx, arr, size);
652}
653
654void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg)
655{
656 u8 buf = arg;
657
658 acpigen_write_return_byte_buffer(ctx, &buf, 1);
659}
660
661void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg)
662{
663 acpigen_emit_byte(ctx, RETURN_OP);
664 acpigen_write_byte(ctx, arg);
665}
666
Simon Glass4c8cbbd2020-09-22 12:44:58 -0600667void acpigen_write_dsm_start(struct acpi_ctx *ctx)
668{
669 /* Method (_DSM, 4, Serialized) */
670 acpigen_write_method_serialized(ctx, "_DSM", 4);
671
672 /* ToBuffer (Arg0, Local0) */
673 acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP);
674}
675
676int acpigen_write_dsm_uuid_start(struct acpi_ctx *ctx, const char *uuid)
677{
678 int ret;
679
680 /* If (LEqual (Local0, ToUUID(uuid))) */
681 acpigen_write_if(ctx);
682 acpigen_emit_byte(ctx, LEQUAL_OP);
683 acpigen_emit_byte(ctx, LOCAL0_OP);
684 ret = acpigen_write_uuid(ctx, uuid);
685 if (ret)
686 return log_msg_ret("uuid", ret);
687
688 /* ToInteger (Arg2, Local1) */
689 acpigen_write_to_integer(ctx, ARG2_OP, LOCAL1_OP);
690
691 return 0;
692}
693
694void acpigen_write_dsm_uuid_start_cond(struct acpi_ctx *ctx, int seq)
695{
696 /* If (LEqual (Local1, i)) */
697 acpigen_write_if_lequal_op_int(ctx, LOCAL1_OP, seq);
698}
699
700void acpigen_write_dsm_uuid_end_cond(struct acpi_ctx *ctx)
701{
702 acpigen_pop_len(ctx); /* If */
703}
704
705void acpigen_write_dsm_uuid_end(struct acpi_ctx *ctx)
706{
707 /* Default case: Return (Buffer (One) { 0x0 }) */
708 acpigen_write_return_singleton_buffer(ctx, 0x0);
709
710 acpigen_pop_len(ctx); /* If (LEqual (Local0, ToUUID(uuid))) */
711}
712
713void acpigen_write_dsm_end(struct acpi_ctx *ctx)
714{
715 /* Return (Buffer (One) { 0x0 }) */
716 acpigen_write_return_singleton_buffer(ctx, 0x0);
717
718 acpigen_pop_len(ctx); /* Method _DSM */
719}
720
Simon Glass8a200762020-07-07 13:12:01 -0600721/**
722 * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
723 *
724 * Store (\_SB.GPC0 (addr), Local5)
725 *
726 * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in
727 * the board's gpiolib.asl
728 *
729 * The value needs to be stored in a local variable so that it can be used in
730 * expressions in the ACPI code.
731 *
732 * @ctx: ACPI context pointer
733 * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0"
734 * @addr: GPIO pin configuration register address
735 *
736 */
737static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx,
738 const char *dw0_read, ulong addr)
739{
740 acpigen_write_store(ctx);
741 acpigen_emit_namestring(ctx, dw0_read);
742 acpigen_write_integer(ctx, addr);
743 acpigen_emit_byte(ctx, LOCAL5_OP);
744}
745
746/**
747 * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off
748 *
749 * @ctx: ACPI context pointer
750 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
751 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
752 * @gpio_num: GPIO number to adjust
753 * @vaL: true to set on, false to set off
754 */
755static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val,
756 const char *dw0_read, const char *dw0_write,
757 struct acpi_gpio *gpio, bool val)
758{
759 acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr);
760
761 /* Store (0x40, Local0) */
762 acpigen_write_store(ctx);
763 acpigen_write_integer(ctx, tx_state_val);
764 acpigen_emit_byte(ctx, LOCAL0_OP);
765
766 if (val) {
767 /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */
768 acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP);
769 } else {
770 /* Not (PAD_CFG0_TX_STATE, Local6) */
771 acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP);
772
773 /* And (Local5, Local6, Local5) */
774 acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP);
775 }
776
777 /*
778 * \_SB.SPC0 (addr, Local5)
779 * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in
780 * gpiolib.asl.
781 */
782 acpigen_emit_namestring(ctx, dw0_write);
783 acpigen_write_integer(ctx, gpio->pin0_addr);
784 acpigen_emit_byte(ctx, LOCAL5_OP);
785
786 return 0;
787}
788
789int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
790 const char *dw0_read, const char *dw0_write,
791 struct acpi_gpio *gpio, bool enable)
792{
793 bool set;
794 int ret;
795
796 set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable;
797 ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio,
798 set);
799 if (ret)
800 return log_msg_ret("call", ret);
801
802 return 0;
803}