acpi: Add support for writing a _PRW
[platform/kernel/u-boot.git] / lib / acpi / acpigen.c
1 // 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>
13 #include <log.h>
14 #include <uuid.h>
15 #include <acpi/acpigen.h>
16 #include <acpi/acpi_device.h>
17 #include <acpi/acpi_table.h>
18 #include <dm/acpi.h>
19
20 u8 *acpigen_get_current(struct acpi_ctx *ctx)
21 {
22         return ctx->current;
23 }
24
25 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data)
26 {
27         *(u8 *)ctx->current++ = data;
28 }
29
30 void acpigen_emit_word(struct acpi_ctx *ctx, uint data)
31 {
32         acpigen_emit_byte(ctx, data & 0xff);
33         acpigen_emit_byte(ctx, (data >> 8) & 0xff);
34 }
35
36 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
37 {
38         /* Output the value in little-endian format */
39         acpigen_emit_byte(ctx, data & 0xff);
40         acpigen_emit_byte(ctx, (data >> 8) & 0xff);
41         acpigen_emit_byte(ctx, (data >> 16) & 0xff);
42         acpigen_emit_byte(ctx, (data >> 24) & 0xff);
43 }
44
45 /*
46  * Maximum length for an ACPI object generated by this code,
47  *
48  * If you need to change this, change acpigen_write_len_f(ctx) and
49  * acpigen_pop_len(ctx)
50  */
51 #define ACPIGEN_MAXLEN 0xfffff
52
53 void acpigen_write_len_f(struct acpi_ctx *ctx)
54 {
55         assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
56         ctx->len_stack[ctx->ltop++] = ctx->current;
57         acpigen_emit_byte(ctx, 0);
58         acpigen_emit_byte(ctx, 0);
59         acpigen_emit_byte(ctx, 0);
60 }
61
62 void acpigen_pop_len(struct acpi_ctx *ctx)
63 {
64         int len;
65         char *p;
66
67         assert(ctx->ltop > 0);
68         p = ctx->len_stack[--ctx->ltop];
69         len = ctx->current - (void *)p;
70         assert(len <= ACPIGEN_MAXLEN);
71         /* generate store length for 0xfffff max */
72         p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
73         p[1] = len >> 4 & 0xff;
74         p[2] = len >> 12 & 0xff;
75 }
76
77 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op)
78 {
79         acpigen_emit_byte(ctx, EXT_OP_PREFIX);
80         acpigen_emit_byte(ctx, op);
81 }
82
83 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
84 {
85         char *p;
86
87         acpigen_emit_byte(ctx, PACKAGE_OP);
88         acpigen_write_len_f(ctx);
89         p = ctx->current;
90         acpigen_emit_byte(ctx, nr_el);
91
92         return p;
93 }
94
95 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data)
96 {
97         acpigen_emit_byte(ctx, BYTE_PREFIX);
98         acpigen_emit_byte(ctx, data & 0xff);
99 }
100
101 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data)
102 {
103         acpigen_emit_byte(ctx, WORD_PREFIX);
104         acpigen_emit_word(ctx, data);
105 }
106
107 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data)
108 {
109         acpigen_emit_byte(ctx, DWORD_PREFIX);
110         acpigen_emit_dword(ctx, data);
111 }
112
113 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data)
114 {
115         acpigen_emit_byte(ctx, QWORD_PREFIX);
116         acpigen_emit_dword(ctx, data & 0xffffffff);
117         acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff);
118 }
119
120 void acpigen_write_zero(struct acpi_ctx *ctx)
121 {
122         acpigen_emit_byte(ctx, ZERO_OP);
123 }
124
125 void acpigen_write_one(struct acpi_ctx *ctx)
126 {
127         acpigen_emit_byte(ctx, ONE_OP);
128 }
129
130 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data)
131 {
132         if (data == 0)
133                 acpigen_write_zero(ctx);
134         else if (data == 1)
135                 acpigen_write_one(ctx);
136         else if (data <= 0xff)
137                 acpigen_write_byte(ctx, (unsigned char)data);
138         else if (data <= 0xffff)
139                 acpigen_write_word(ctx, (unsigned int)data);
140         else if (data <= 0xffffffff)
141                 acpigen_write_dword(ctx, (unsigned int)data);
142         else
143                 acpigen_write_qword(ctx, data);
144 }
145
146 void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name)
147 {
148         acpigen_write_name(ctx, name);
149         acpigen_write_zero(ctx);
150 }
151
152 void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name)
153 {
154         acpigen_write_name(ctx, name);
155         acpigen_write_one(ctx);
156 }
157
158 void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val)
159 {
160         acpigen_write_name(ctx, name);
161         acpigen_write_byte(ctx, val);
162 }
163
164 void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val)
165 {
166         acpigen_write_name(ctx, name);
167         acpigen_write_word(ctx, val);
168 }
169
170 void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val)
171 {
172         acpigen_write_name(ctx, name);
173         acpigen_write_dword(ctx, val);
174 }
175
176 void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val)
177 {
178         acpigen_write_name(ctx, name);
179         acpigen_write_qword(ctx, val);
180 }
181
182 void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name, u64 val)
183 {
184         acpigen_write_name(ctx, name);
185         acpigen_write_integer(ctx, val);
186 }
187
188 void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
189                                const char *string)
190 {
191         acpigen_write_name(ctx, name);
192         acpigen_write_string(ctx, string);
193 }
194
195 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
196 {
197         int i;
198
199         for (i = 0; i < size; i++)
200                 acpigen_emit_byte(ctx, data[i]);
201 }
202
203 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
204 {
205         acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
206         acpigen_emit_byte(ctx, '\0');
207 }
208
209 void acpigen_write_string(struct acpi_ctx *ctx, const char *str)
210 {
211         acpigen_emit_byte(ctx, STRING_PREFIX);
212         acpigen_emit_string(ctx, str);
213 }
214
215 /*
216  * The naming conventions for ACPI namespace names are a bit tricky as
217  * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
218  * and "By convention, when an ASL compiler pads a name shorter than 4
219  * characters, it is done so with trailing underscores ('_')".
220  *
221  * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details.
222  */
223 static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx,
224                                            const char *name)
225 {
226         const char *ptr;
227         int i;
228
229         for (i = 0, ptr = name; i < 4; i++) {
230                 if (!*ptr || *ptr == '.')
231                         acpigen_emit_byte(ctx, '_');
232                 else
233                         acpigen_emit_byte(ctx, *ptr++);
234         }
235 }
236
237 static void acpigen_emit_double_namestring(struct acpi_ctx *ctx,
238                                            const char *name, int dotpos)
239 {
240         acpigen_emit_byte(ctx, DUAL_NAME_PREFIX);
241         acpigen_emit_simple_namestring(ctx, name);
242         acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]);
243 }
244
245 static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx,
246                                           const char *name)
247 {
248         unsigned char *pathlen;
249         int count = 0;
250
251         acpigen_emit_byte(ctx, MULTI_NAME_PREFIX);
252         pathlen = ctx->current;
253         acpigen_emit_byte(ctx, 0);
254
255         while (*name) {
256                 acpigen_emit_simple_namestring(ctx, name);
257                 /* find end or next entity */
258                 while (*name != '.' && *name)
259                         name++;
260                 /* forward to next */
261                 if (*name == '.')
262                         name++;
263                 count++;
264         }
265
266         *pathlen = count;
267 }
268
269 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath)
270 {
271         int dotcount;
272         int dotpos;
273         int i;
274
275         /* We can start with a '\' */
276         if (*namepath == '\\') {
277                 acpigen_emit_byte(ctx, '\\');
278                 namepath++;
279         }
280
281         /* And there can be any number of '^' */
282         while (*namepath == '^') {
283                 acpigen_emit_byte(ctx, '^');
284                 namepath++;
285         }
286
287         for (i = 0, dotcount = 0; namepath[i]; i++) {
288                 if (namepath[i] == '.') {
289                         dotcount++;
290                         dotpos = i;
291                 }
292         }
293
294         /* If we have only \\ or only ^* then we need to add a null name */
295         if (!*namepath)
296                 acpigen_emit_byte(ctx, ZERO_OP);
297         else if (dotcount == 0)
298                 acpigen_emit_simple_namestring(ctx, namepath);
299         else if (dotcount == 1)
300                 acpigen_emit_double_namestring(ctx, namepath, dotpos);
301         else
302                 acpigen_emit_multi_namestring(ctx, namepath);
303 }
304
305 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath)
306 {
307         acpigen_emit_byte(ctx, NAME_OP);
308         acpigen_emit_namestring(ctx, namepath);
309 }
310
311 void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope)
312 {
313         acpigen_emit_byte(ctx, SCOPE_OP);
314         acpigen_write_len_f(ctx);
315         acpigen_emit_namestring(ctx, scope);
316 }
317
318 static void acpigen_write_method_internal(struct acpi_ctx *ctx,
319                                           const char *name, uint flags)
320 {
321         acpigen_emit_byte(ctx, METHOD_OP);
322         acpigen_write_len_f(ctx);
323         acpigen_emit_namestring(ctx, name);
324         acpigen_emit_byte(ctx, flags);
325 }
326
327 /* Method (name, nargs, NotSerialized) */
328 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs)
329 {
330         acpigen_write_method_internal(ctx, name,
331                                       nargs & ACPI_METHOD_NARGS_MASK);
332 }
333
334 /* Method (name, nargs, Serialized) */
335 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
336                                      int nargs)
337 {
338         acpigen_write_method_internal(ctx, name,
339                                       (nargs & ACPI_METHOD_NARGS_MASK) |
340                                       ACPI_METHOD_SERIALIZED_MASK);
341 }
342
343 void acpigen_write_device(struct acpi_ctx *ctx, const char *name)
344 {
345         acpigen_emit_ext_op(ctx, DEVICE_OP);
346         acpigen_write_len_f(ctx);
347         acpigen_emit_namestring(ctx, name);
348 }
349
350 void acpigen_write_sta(struct acpi_ctx *ctx, uint status)
351 {
352         /* Method (_STA, 0, NotSerialized) { Return (status) } */
353         acpigen_write_method(ctx, "_STA", 0);
354         acpigen_emit_byte(ctx, RETURN_OP);
355         acpigen_write_byte(ctx, status);
356         acpigen_pop_len(ctx);
357 }
358
359 static void acpigen_write_register(struct acpi_ctx *ctx,
360                                    const struct acpi_gen_regaddr *addr)
361 {
362         /* See ACPI v6.3 section 6.4.3.7: Generic Register Descriptor */
363         acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_REGISTER);
364         acpigen_emit_byte(ctx, 0x0c);           /* Register Length 7:0 */
365         acpigen_emit_byte(ctx, 0x00);           /* Register Length 15:8 */
366         acpigen_emit_byte(ctx, addr->space_id);
367         acpigen_emit_byte(ctx, addr->bit_width);
368         acpigen_emit_byte(ctx, addr->bit_offset);
369         acpigen_emit_byte(ctx, addr->access_size);
370         acpigen_emit_dword(ctx, addr->addrl);
371         acpigen_emit_dword(ctx, addr->addrh);
372 }
373
374 void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx)
375 {
376         /*
377          * A ResourceTemplate() is a Buffer() with a
378          * (Byte|Word|DWord) containing the length, followed by one or more
379          * resource items, terminated by the end tag.
380          * (small item 0xf, len 1)
381          */
382         acpigen_emit_byte(ctx, BUFFER_OP);
383         acpigen_write_len_f(ctx);
384         acpigen_emit_byte(ctx, WORD_PREFIX);
385         ctx->len_stack[ctx->ltop++] = ctx->current;
386
387         /*
388          * Add two dummy bytes for the ACPI word (keep aligned with the
389          * calculation in acpigen_write_resourcetemplate_footer() below)
390          */
391         acpigen_emit_byte(ctx, 0x00);
392         acpigen_emit_byte(ctx, 0x00);
393 }
394
395 void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx)
396 {
397         char *p = ctx->len_stack[--ctx->ltop];
398         int len;
399         /*
400          * See ACPI v6.3 section 6.4.2.9: End Tag
401          * 0x79 <checksum>
402          * 0x00 is treated as a good checksum according to the spec
403          * and is what iasl generates.
404          */
405         acpigen_emit_byte(ctx, ACPI_END_TAG);
406         acpigen_emit_byte(ctx, 0x00);
407
408         /*
409          * Start counting past the 2-bytes length added in
410          * acpigen_write_resourcetemplate_header() above
411          */
412         len = (char *)ctx->current - (p + 2);
413
414         /* patch len word */
415         p[0] = len & 0xff;
416         p[1] = (len >> 8) & 0xff;
417
418         acpigen_pop_len(ctx);
419 }
420
421 void acpigen_write_register_resource(struct acpi_ctx *ctx,
422                                      const struct acpi_gen_regaddr *addr)
423 {
424         acpigen_write_resourcetemplate_header(ctx);
425         acpigen_write_register(ctx, addr);
426         acpigen_write_resourcetemplate_footer(ctx);
427 }
428
429 void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level)
430 {
431         /* Name (_PRW, Package () { wake, level } */
432         acpigen_write_name(ctx, "_PRW");
433         acpigen_write_package(ctx, 2);
434         acpigen_write_integer(ctx, wake);
435         acpigen_write_integer(ctx, level);
436         acpigen_pop_len(ctx);
437 }
438
439 /*
440  * ToUUID(uuid)
441  *
442  * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the
443  * bytes that make up a UUID Buffer object:
444  *
445  * UUID byte order for input to this function:
446  *   aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
447  *
448  * UUID byte order output by this function:
449  *   ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
450  */
451 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
452 {
453         u8 buf[UUID_BIN_LEN];
454         int ret;
455
456         /* Parse UUID string into bytes */
457         ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
458         if (ret)
459                 return log_msg_ret("bad hex", -EINVAL);
460
461         /* BufferOp */
462         acpigen_emit_byte(ctx, BUFFER_OP);
463         acpigen_write_len_f(ctx);
464
465         /* Buffer length in bytes */
466         acpigen_write_word(ctx, UUID_BIN_LEN);
467
468         /* Output UUID in expected order */
469         acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN);
470
471         acpigen_pop_len(ctx);
472
473         return 0;
474 }
475
476 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
477                              uint order, const char *const dev_states[],
478                              size_t dev_states_count)
479 {
480         size_t i;
481
482         for (i = 0; i < dev_states_count; i++) {
483                 acpigen_write_name(ctx, dev_states[i]);
484                 acpigen_write_package(ctx, 1);
485                 acpigen_emit_simple_namestring(ctx, name);
486                 acpigen_pop_len(ctx);           /* Package */
487         }
488
489         acpigen_emit_ext_op(ctx, POWER_RES_OP);
490
491         acpigen_write_len_f(ctx);
492
493         acpigen_emit_simple_namestring(ctx, name);
494         acpigen_emit_byte(ctx, level);
495         acpigen_emit_word(ctx, order);
496 }
497
498 /* Sleep (ms) */
499 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
500 {
501         acpigen_emit_ext_op(ctx, SLEEP_OP);
502         acpigen_write_integer(ctx, sleep_ms);
503 }
504
505 void acpigen_write_store(struct acpi_ctx *ctx)
506 {
507         acpigen_emit_byte(ctx, STORE_OP);
508 }
509
510 /* Or (arg1, arg2, res) */
511 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
512 {
513         acpigen_emit_byte(ctx, OR_OP);
514         acpigen_emit_byte(ctx, arg1);
515         acpigen_emit_byte(ctx, arg2);
516         acpigen_emit_byte(ctx, res);
517 }
518
519 /* And (arg1, arg2, res) */
520 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
521 {
522         acpigen_emit_byte(ctx, AND_OP);
523         acpigen_emit_byte(ctx, arg1);
524         acpigen_emit_byte(ctx, arg2);
525         acpigen_emit_byte(ctx, res);
526 }
527
528 /* Not (arg, res) */
529 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res)
530 {
531         acpigen_emit_byte(ctx, NOT_OP);
532         acpigen_emit_byte(ctx, arg);
533         acpigen_emit_byte(ctx, res);
534 }
535
536 /* Store (str, DEBUG) */
537 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
538 {
539         acpigen_write_store(ctx);
540         acpigen_write_string(ctx, str);
541         acpigen_emit_ext_op(ctx, DEBUG_OP);
542 }
543
544 /**
545  * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
546  *
547  * Store (\_SB.GPC0 (addr), Local5)
548  *
549  * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in
550  * the board's gpiolib.asl
551  *
552  * The value needs to be stored in a local variable so that it can be used in
553  * expressions in the ACPI code.
554  *
555  * @ctx: ACPI context pointer
556  * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0"
557  * @addr: GPIO pin configuration register address
558  *
559  */
560 static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx,
561                                       const char *dw0_read, ulong addr)
562 {
563         acpigen_write_store(ctx);
564         acpigen_emit_namestring(ctx, dw0_read);
565         acpigen_write_integer(ctx, addr);
566         acpigen_emit_byte(ctx, LOCAL5_OP);
567 }
568
569 /**
570  * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off
571  *
572  * @ctx: ACPI context pointer
573  * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
574  * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
575  * @gpio_num: GPIO number to adjust
576  * @vaL: true to set on, false to set off
577  */
578 static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val,
579                                 const char *dw0_read, const char *dw0_write,
580                                 struct acpi_gpio *gpio, bool val)
581 {
582         acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr);
583
584         /* Store (0x40, Local0) */
585         acpigen_write_store(ctx);
586         acpigen_write_integer(ctx, tx_state_val);
587         acpigen_emit_byte(ctx, LOCAL0_OP);
588
589         if (val) {
590                 /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */
591                 acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP);
592         } else {
593                 /* Not (PAD_CFG0_TX_STATE, Local6) */
594                 acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP);
595
596                 /* And (Local5, Local6, Local5) */
597                 acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP);
598         }
599
600         /*
601          * \_SB.SPC0 (addr, Local5)
602          * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in
603          * gpiolib.asl.
604          */
605         acpigen_emit_namestring(ctx, dw0_write);
606         acpigen_write_integer(ctx, gpio->pin0_addr);
607         acpigen_emit_byte(ctx, LOCAL5_OP);
608
609         return 0;
610 }
611
612 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
613                                const char *dw0_read, const char *dw0_write,
614                                struct acpi_gpio *gpio, bool enable)
615 {
616         bool set;
617         int ret;
618
619         set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable;
620         ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio,
621                                    set);
622         if (ret)
623                 return log_msg_ret("call", ret);
624
625         return 0;
626 }