blob: fa9dad32a3fecf5e7ad43b6a373b05a2b66cc0f9 [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
Simon Glass98528d42020-07-07 13:11:42 -060011#include <dm.h>
Simon Glass0f277632020-07-07 13:11:50 -060012#include <log.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010013#include <u-boot/uuid.h>
Simon Glass98528d42020-07-07 13:11:42 -060014#include <acpi/acpigen.h>
Simon Glass8a200762020-07-07 13:12:01 -060015#include <acpi/acpi_device.h>
Simon Glass837127f2020-07-07 21:32:11 -060016#include <acpi/acpi_table.h>
Simon Glass98528d42020-07-07 13:11:42 -060017#include <dm/acpi.h>
18
Simon Glass412dc4b2020-09-22 12:45:11 -060019/* CPU path format */
20#define ACPI_CPU_STRING "\\_PR.CP%02d"
21
Simon Glass98528d42020-07-07 13:11:42 -060022u8 *acpigen_get_current(struct acpi_ctx *ctx)
23{
24 return ctx->current;
25}
26
27void acpigen_emit_byte(struct acpi_ctx *ctx, uint data)
28{
29 *(u8 *)ctx->current++ = data;
30}
31
32void acpigen_emit_word(struct acpi_ctx *ctx, uint data)
33{
34 acpigen_emit_byte(ctx, data & 0xff);
35 acpigen_emit_byte(ctx, (data >> 8) & 0xff);
36}
37
38void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
39{
40 /* Output the value in little-endian format */
41 acpigen_emit_byte(ctx, data & 0xff);
42 acpigen_emit_byte(ctx, (data >> 8) & 0xff);
43 acpigen_emit_byte(ctx, (data >> 16) & 0xff);
44 acpigen_emit_byte(ctx, (data >> 24) & 0xff);
45}
Simon Glass071c4a52020-07-07 13:11:45 -060046
Simon Glass0f277632020-07-07 13:11:50 -060047/*
48 * Maximum length for an ACPI object generated by this code,
49 *
50 * If you need to change this, change acpigen_write_len_f(ctx) and
51 * acpigen_pop_len(ctx)
52 */
53#define ACPIGEN_MAXLEN 0xfffff
54
55void acpigen_write_len_f(struct acpi_ctx *ctx)
56{
57 assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
58 ctx->len_stack[ctx->ltop++] = ctx->current;
59 acpigen_emit_byte(ctx, 0);
60 acpigen_emit_byte(ctx, 0);
61 acpigen_emit_byte(ctx, 0);
62}
63
64void acpigen_pop_len(struct acpi_ctx *ctx)
65{
66 int len;
67 char *p;
68
69 assert(ctx->ltop > 0);
70 p = ctx->len_stack[--ctx->ltop];
71 len = ctx->current - (void *)p;
72 assert(len <= ACPIGEN_MAXLEN);
73 /* generate store length for 0xfffff max */
74 p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
75 p[1] = len >> 4 & 0xff;
76 p[2] = len >> 12 & 0xff;
77}
78
Simon Glass9238fac2020-07-07 13:11:59 -060079void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op)
80{
81 acpigen_emit_byte(ctx, EXT_OP_PREFIX);
82 acpigen_emit_byte(ctx, op);
83}
84
Simon Glassedc26802020-07-07 13:11:51 -060085char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
86{
87 char *p;
88
89 acpigen_emit_byte(ctx, PACKAGE_OP);
90 acpigen_write_len_f(ctx);
91 p = ctx->current;
92 acpigen_emit_byte(ctx, nr_el);
93
94 return p;
95}
96
Simon Glass8715ce02020-07-07 13:11:52 -060097void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data)
98{
99 acpigen_emit_byte(ctx, BYTE_PREFIX);
100 acpigen_emit_byte(ctx, data & 0xff);
101}
102
103void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data)
104{
105 acpigen_emit_byte(ctx, WORD_PREFIX);
106 acpigen_emit_word(ctx, data);
107}
108
109void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data)
110{
111 acpigen_emit_byte(ctx, DWORD_PREFIX);
112 acpigen_emit_dword(ctx, data);
113}
114
115void acpigen_write_qword(struct acpi_ctx *ctx, u64 data)
116{
117 acpigen_emit_byte(ctx, QWORD_PREFIX);
118 acpigen_emit_dword(ctx, data & 0xffffffff);
119 acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff);
120}
121
122void acpigen_write_zero(struct acpi_ctx *ctx)
123{
124 acpigen_emit_byte(ctx, ZERO_OP);
125}
126
127void acpigen_write_one(struct acpi_ctx *ctx)
128{
129 acpigen_emit_byte(ctx, ONE_OP);
130}
131
132void acpigen_write_integer(struct acpi_ctx *ctx, u64 data)
133{
134 if (data == 0)
135 acpigen_write_zero(ctx);
136 else if (data == 1)
137 acpigen_write_one(ctx);
138 else if (data <= 0xff)
139 acpigen_write_byte(ctx, (unsigned char)data);
140 else if (data <= 0xffff)
141 acpigen_write_word(ctx, (unsigned int)data);
142 else if (data <= 0xffffffff)
143 acpigen_write_dword(ctx, (unsigned int)data);
144 else
145 acpigen_write_qword(ctx, data);
146}
147
Simon Glassb4df0782020-07-07 21:32:15 -0600148void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name)
149{
150 acpigen_write_name(ctx, name);
151 acpigen_write_zero(ctx);
152}
153
154void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name)
155{
156 acpigen_write_name(ctx, name);
157 acpigen_write_one(ctx);
158}
159
160void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val)
161{
162 acpigen_write_name(ctx, name);
163 acpigen_write_byte(ctx, val);
164}
165
166void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val)
167{
168 acpigen_write_name(ctx, name);
169 acpigen_write_word(ctx, val);
170}
171
172void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val)
173{
174 acpigen_write_name(ctx, name);
175 acpigen_write_dword(ctx, val);
176}
177
178void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val)
179{
180 acpigen_write_name(ctx, name);
181 acpigen_write_qword(ctx, val);
182}
183
184void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name, u64 val)
185{
186 acpigen_write_name(ctx, name);
187 acpigen_write_integer(ctx, val);
188}
189
190void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
191 const char *string)
192{
193 acpigen_write_name(ctx, name);
194 acpigen_write_string(ctx, string);
195}
196
Simon Glass071c4a52020-07-07 13:11:45 -0600197void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
198{
199 int i;
200
201 for (i = 0; i < size; i++)
202 acpigen_emit_byte(ctx, data[i]);
203}
204
205void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
206{
207 acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
208 acpigen_emit_byte(ctx, '\0');
209}
Simon Glass45d6d952020-07-07 13:11:53 -0600210
211void acpigen_write_string(struct acpi_ctx *ctx, const char *str)
212{
213 acpigen_emit_byte(ctx, STRING_PREFIX);
214 acpigen_emit_string(ctx, str);
215}
Simon Glass0dc3d512020-07-07 13:11:54 -0600216
217/*
218 * The naming conventions for ACPI namespace names are a bit tricky as
219 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
220 * and "By convention, when an ASL compiler pads a name shorter than 4
221 * characters, it is done so with trailing underscores ('_')".
222 *
223 * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details.
224 */
225static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx,
226 const char *name)
227{
228 const char *ptr;
229 int i;
230
231 for (i = 0, ptr = name; i < 4; i++) {
232 if (!*ptr || *ptr == '.')
233 acpigen_emit_byte(ctx, '_');
234 else
235 acpigen_emit_byte(ctx, *ptr++);
236 }
237}
238
239static void acpigen_emit_double_namestring(struct acpi_ctx *ctx,
240 const char *name, int dotpos)
241{
242 acpigen_emit_byte(ctx, DUAL_NAME_PREFIX);
243 acpigen_emit_simple_namestring(ctx, name);
244 acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]);
245}
246
247static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx,
248 const char *name)
249{
250 unsigned char *pathlen;
251 int count = 0;
252
253 acpigen_emit_byte(ctx, MULTI_NAME_PREFIX);
254 pathlen = ctx->current;
255 acpigen_emit_byte(ctx, 0);
256
257 while (*name) {
258 acpigen_emit_simple_namestring(ctx, name);
259 /* find end or next entity */
260 while (*name != '.' && *name)
261 name++;
262 /* forward to next */
263 if (*name == '.')
264 name++;
265 count++;
266 }
267
268 *pathlen = count;
269}
270
271void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath)
272{
273 int dotcount;
274 int dotpos;
275 int i;
276
277 /* We can start with a '\' */
278 if (*namepath == '\\') {
279 acpigen_emit_byte(ctx, '\\');
280 namepath++;
281 }
282
283 /* And there can be any number of '^' */
284 while (*namepath == '^') {
285 acpigen_emit_byte(ctx, '^');
286 namepath++;
287 }
288
289 for (i = 0, dotcount = 0; namepath[i]; i++) {
290 if (namepath[i] == '.') {
291 dotcount++;
292 dotpos = i;
293 }
294 }
295
296 /* If we have only \\ or only ^* then we need to add a null name */
297 if (!*namepath)
298 acpigen_emit_byte(ctx, ZERO_OP);
299 else if (dotcount == 0)
300 acpigen_emit_simple_namestring(ctx, namepath);
301 else if (dotcount == 1)
302 acpigen_emit_double_namestring(ctx, namepath, dotpos);
303 else
304 acpigen_emit_multi_namestring(ctx, namepath);
305}
306
307void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath)
308{
309 acpigen_emit_byte(ctx, NAME_OP);
310 acpigen_emit_namestring(ctx, namepath);
311}
Simon Glass48342b02020-07-07 13:11:55 -0600312
Simon Glass6fcdaf12020-07-07 21:32:10 -0600313void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope)
314{
315 acpigen_emit_byte(ctx, SCOPE_OP);
316 acpigen_write_len_f(ctx);
317 acpigen_emit_namestring(ctx, scope);
318}
319
Simon Glass9238fac2020-07-07 13:11:59 -0600320static void acpigen_write_method_internal(struct acpi_ctx *ctx,
321 const char *name, uint flags)
322{
323 acpigen_emit_byte(ctx, METHOD_OP);
324 acpigen_write_len_f(ctx);
325 acpigen_emit_namestring(ctx, name);
326 acpigen_emit_byte(ctx, flags);
327}
328
329/* Method (name, nargs, NotSerialized) */
330void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs)
331{
332 acpigen_write_method_internal(ctx, name,
333 nargs & ACPI_METHOD_NARGS_MASK);
334}
335
336/* Method (name, nargs, Serialized) */
337void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
338 int nargs)
339{
340 acpigen_write_method_internal(ctx, name,
341 (nargs & ACPI_METHOD_NARGS_MASK) |
342 ACPI_METHOD_SERIALIZED_MASK);
343}
344
Simon Glass412dc4b2020-09-22 12:45:11 -0600345void acpigen_write_processor(struct acpi_ctx *ctx, uint cpuindex,
346 u32 pblock_addr, uint pblock_len)
347{
348 /*
349 * Processor (\_PR.CPnn, cpuindex, pblock_addr, pblock_len)
350 * {
351 */
352 char pscope[16];
353
354 acpigen_emit_ext_op(ctx, PROCESSOR_OP);
355 acpigen_write_len_f(ctx);
356
357 snprintf(pscope, sizeof(pscope), ACPI_CPU_STRING, cpuindex);
358 acpigen_emit_namestring(ctx, pscope);
359 acpigen_emit_byte(ctx, cpuindex);
360 acpigen_emit_dword(ctx, pblock_addr);
361 acpigen_emit_byte(ctx, pblock_len);
362}
363
Patrick Rudolph28885872024-10-23 15:19:58 +0200364void acpigen_write_processor_device(struct acpi_ctx *ctx, uint cpuindex)
365{
366 char pscope[16];
367
368 snprintf(pscope, sizeof(pscope), ACPI_CPU_STRING, cpuindex);
369 acpigen_write_device(ctx, pscope);
370 acpigen_write_name_string(ctx, "_HID", "ACPI0007");
371 acpigen_write_name_integer(ctx, "_UID", cpuindex);
372 acpigen_pop_len(ctx); /* Device */
373}
374
Simon Glass412dc4b2020-09-22 12:45:11 -0600375void acpigen_write_processor_package(struct acpi_ctx *ctx,
376 const char *const name,
377 const uint first_core,
378 const uint core_count)
379{
380 uint i;
381 char pscope[16];
382
383 acpigen_write_name(ctx, name);
384 acpigen_write_package(ctx, core_count);
385 for (i = first_core; i < first_core + core_count; ++i) {
386 snprintf(pscope, sizeof(pscope), ACPI_CPU_STRING, i);
387 acpigen_emit_namestring(ctx, pscope);
388 }
389 acpigen_pop_len(ctx);
390}
391
392void acpigen_write_processor_cnot(struct acpi_ctx *ctx, const uint num_cores)
393{
394 int core_id;
395
396 acpigen_write_method(ctx, "\\_PR.CNOT", 1);
397 for (core_id = 0; core_id < num_cores; core_id++) {
398 char buffer[30];
399
400 snprintf(buffer, sizeof(buffer), ACPI_CPU_STRING, core_id);
401 acpigen_emit_byte(ctx, NOTIFY_OP);
402 acpigen_emit_namestring(ctx, buffer);
403 acpigen_emit_byte(ctx, ARG0_OP);
404 }
405 acpigen_pop_len(ctx);
406}
407
Simon Glassa186d932020-07-07 21:32:14 -0600408void acpigen_write_device(struct acpi_ctx *ctx, const char *name)
409{
410 acpigen_emit_ext_op(ctx, DEVICE_OP);
411 acpigen_write_len_f(ctx);
412 acpigen_emit_namestring(ctx, name);
413}
414
Simon Glass9238fac2020-07-07 13:11:59 -0600415void acpigen_write_sta(struct acpi_ctx *ctx, uint status)
416{
417 /* Method (_STA, 0, NotSerialized) { Return (status) } */
418 acpigen_write_method(ctx, "_STA", 0);
419 acpigen_emit_byte(ctx, RETURN_OP);
420 acpigen_write_byte(ctx, status);
421 acpigen_pop_len(ctx);
422}
423
Simon Glass837127f2020-07-07 21:32:11 -0600424static void acpigen_write_register(struct acpi_ctx *ctx,
425 const struct acpi_gen_regaddr *addr)
426{
427 /* See ACPI v6.3 section 6.4.3.7: Generic Register Descriptor */
428 acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_REGISTER);
429 acpigen_emit_byte(ctx, 0x0c); /* Register Length 7:0 */
430 acpigen_emit_byte(ctx, 0x00); /* Register Length 15:8 */
431 acpigen_emit_byte(ctx, addr->space_id);
432 acpigen_emit_byte(ctx, addr->bit_width);
433 acpigen_emit_byte(ctx, addr->bit_offset);
434 acpigen_emit_byte(ctx, addr->access_size);
435 acpigen_emit_dword(ctx, addr->addrl);
436 acpigen_emit_dword(ctx, addr->addrh);
437}
438
439void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx)
440{
441 /*
442 * A ResourceTemplate() is a Buffer() with a
443 * (Byte|Word|DWord) containing the length, followed by one or more
444 * resource items, terminated by the end tag.
445 * (small item 0xf, len 1)
446 */
447 acpigen_emit_byte(ctx, BUFFER_OP);
448 acpigen_write_len_f(ctx);
449 acpigen_emit_byte(ctx, WORD_PREFIX);
450 ctx->len_stack[ctx->ltop++] = ctx->current;
451
452 /*
453 * Add two dummy bytes for the ACPI word (keep aligned with the
454 * calculation in acpigen_write_resourcetemplate_footer() below)
455 */
456 acpigen_emit_byte(ctx, 0x00);
457 acpigen_emit_byte(ctx, 0x00);
458}
459
460void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx)
461{
462 char *p = ctx->len_stack[--ctx->ltop];
463 int len;
464 /*
465 * See ACPI v6.3 section 6.4.2.9: End Tag
466 * 0x79 <checksum>
467 * 0x00 is treated as a good checksum according to the spec
468 * and is what iasl generates.
469 */
470 acpigen_emit_byte(ctx, ACPI_END_TAG);
471 acpigen_emit_byte(ctx, 0x00);
472
473 /*
474 * Start counting past the 2-bytes length added in
475 * acpigen_write_resourcetemplate_header() above
476 */
477 len = (char *)ctx->current - (p + 2);
478
479 /* patch len word */
480 p[0] = len & 0xff;
481 p[1] = (len >> 8) & 0xff;
482
483 acpigen_pop_len(ctx);
484}
485
486void acpigen_write_register_resource(struct acpi_ctx *ctx,
487 const struct acpi_gen_regaddr *addr)
488{
489 acpigen_write_resourcetemplate_header(ctx);
490 acpigen_write_register(ctx, addr);
491 acpigen_write_resourcetemplate_footer(ctx);
492}
493
Simon Glass2f8b6af2020-09-22 12:45:13 -0600494void acpigen_write_ppc(struct acpi_ctx *ctx, uint num_pstates)
495{
496 /*
497 * Method (_PPC, 0, NotSerialized)
498 * {
499 * Return (num_pstates)
500 * }
501 */
502 acpigen_write_method(ctx, "_PPC", 0);
503 acpigen_emit_byte(ctx, RETURN_OP);
504 acpigen_write_byte(ctx, num_pstates);
505 acpigen_pop_len(ctx);
506}
507
508/*
509 * Generates a func with max supported P-states saved
510 * in the variable PPCM.
511 */
512void acpigen_write_ppc_nvs(struct acpi_ctx *ctx)
513{
514 /*
515 * Method (_PPC, 0, NotSerialized)
516 * {
517 * Return (PPCM)
518 * }
519 */
520 acpigen_write_method(ctx, "_PPC", 0);
521 acpigen_emit_byte(ctx, RETURN_OP);
522 acpigen_emit_namestring(ctx, "PPCM");
523 acpigen_pop_len(ctx);
524}
525
526void acpigen_write_tpc(struct acpi_ctx *ctx, const char *gnvs_tpc_limit)
527{
528 /*
529 * // Sample _TPC method
530 * Method (_TPC, 0, NotSerialized)
531 * {
532 * Return (\TLVL)
533 * }
534 */
535 acpigen_write_method(ctx, "_TPC", 0);
536 acpigen_emit_byte(ctx, RETURN_OP);
537 acpigen_emit_namestring(ctx, gnvs_tpc_limit);
538 acpigen_pop_len(ctx);
539}
540
Simon Glass77dfd032020-09-22 12:44:56 -0600541void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level)
542{
543 /* Name (_PRW, Package () { wake, level } */
544 acpigen_write_name(ctx, "_PRW");
545 acpigen_write_package(ctx, 2);
546 acpigen_write_integer(ctx, wake);
547 acpigen_write_integer(ctx, level);
548 acpigen_pop_len(ctx);
549}
550
Simon Glass2f8b6af2020-09-22 12:45:13 -0600551void acpigen_write_pss_package(struct acpi_ctx *ctx, u32 core_freq, u32 power,
552 u32 trans_lat, u32 busm_lat, u32 control,
553 u32 status)
554{
555 acpigen_write_package(ctx, 6);
556 acpigen_write_dword(ctx, core_freq);
557 acpigen_write_dword(ctx, power);
558 acpigen_write_dword(ctx, trans_lat);
559 acpigen_write_dword(ctx, busm_lat);
560 acpigen_write_dword(ctx, control);
561 acpigen_write_dword(ctx, status);
562 acpigen_pop_len(ctx);
563
564 log_debug("PSS: %uMHz power %u control 0x%x status 0x%x\n",
565 core_freq, power, control, status);
566}
567
568void acpigen_write_psd_package(struct acpi_ctx *ctx, uint domain, uint numprocs,
569 enum psd_coord coordtype)
570{
571 acpigen_write_name(ctx, "_PSD");
572 acpigen_write_package(ctx, 1);
573 acpigen_write_package(ctx, 5);
574 acpigen_write_byte(ctx, 5); // 5 values
575 acpigen_write_byte(ctx, 0); // revision 0
576 acpigen_write_dword(ctx, domain);
577 acpigen_write_dword(ctx, coordtype);
578 acpigen_write_dword(ctx, numprocs);
579 acpigen_pop_len(ctx);
580 acpigen_pop_len(ctx);
581}
582
583static void acpigen_write_cst_package_entry(struct acpi_ctx *ctx,
584 const struct acpi_cstate *cstate)
585{
586 acpigen_write_package(ctx, 4);
587 acpigen_write_register_resource(ctx, &cstate->resource);
588 acpigen_write_dword(ctx, cstate->ctype);
589 acpigen_write_dword(ctx, cstate->latency);
590 acpigen_write_dword(ctx, cstate->power);
591 acpigen_pop_len(ctx);
592}
593
594void acpigen_write_cst_package(struct acpi_ctx *ctx,
595 const struct acpi_cstate *cstate, int nentries)
596{
597 int i;
598
599 acpigen_write_name(ctx, "_CST");
600 acpigen_write_package(ctx, nentries + 1);
601 acpigen_write_dword(ctx, nentries);
602
603 for (i = 0; i < nentries; i++)
604 acpigen_write_cst_package_entry(ctx, cstate + i);
605
606 acpigen_pop_len(ctx);
607}
608
609void acpigen_write_csd_package(struct acpi_ctx *ctx, uint domain, uint numprocs,
610 enum csd_coord coordtype, uint index)
611{
612 acpigen_write_name(ctx, "_CSD");
613 acpigen_write_package(ctx, 1);
614 acpigen_write_package(ctx, 6);
615 acpigen_write_byte(ctx, 6); // 6 values
616 acpigen_write_byte(ctx, 0); // revision 0
617 acpigen_write_dword(ctx, domain);
618 acpigen_write_dword(ctx, coordtype);
619 acpigen_write_dword(ctx, numprocs);
620 acpigen_write_dword(ctx, index);
621 acpigen_pop_len(ctx);
622 acpigen_pop_len(ctx);
623}
624
625void acpigen_write_tss_package(struct acpi_ctx *ctx,
626 struct acpi_tstate *entry, int nentries)
627{
628 /*
629 * Sample _TSS package with 100% and 50% duty cycles
630 * Name (_TSS, Package (0x02)
631 * {
632 * Package(){100, 1000, 0, 0x00, 0)
633 * Package(){50, 520, 0, 0x18, 0)
634 * })
635 */
636 struct acpi_tstate *tstate = entry;
637 int i;
638
639 acpigen_write_name(ctx, "_TSS");
640 acpigen_write_package(ctx, nentries);
641
642 for (i = 0; i < nentries; i++) {
643 acpigen_write_package(ctx, 5);
644 acpigen_write_dword(ctx, tstate->percent);
645 acpigen_write_dword(ctx, tstate->power);
646 acpigen_write_dword(ctx, tstate->latency);
647 acpigen_write_dword(ctx, tstate->control);
648 acpigen_write_dword(ctx, tstate->status);
649 acpigen_pop_len(ctx);
650 tstate++;
651 }
652
653 acpigen_pop_len(ctx);
654}
655
656void acpigen_write_tsd_package(struct acpi_ctx *ctx, u32 domain, u32 numprocs,
657 enum psd_coord coordtype)
658{
659 acpigen_write_name(ctx, "_TSD");
660 acpigen_write_package(ctx, 1);
661 acpigen_write_package(ctx, 5);
662 acpigen_write_byte(ctx, 5); // 5 values
663 acpigen_write_byte(ctx, 0); // revision 0
664 acpigen_write_dword(ctx, domain);
665 acpigen_write_dword(ctx, coordtype);
666 acpigen_write_dword(ctx, numprocs);
667 acpigen_pop_len(ctx);
668 acpigen_pop_len(ctx);
669}
670
Simon Glass48342b02020-07-07 13:11:55 -0600671/*
672 * ToUUID(uuid)
673 *
674 * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the
675 * bytes that make up a UUID Buffer object:
676 *
677 * UUID byte order for input to this function:
678 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
679 *
680 * UUID byte order output by this function:
681 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
682 */
683int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
684{
685 u8 buf[UUID_BIN_LEN];
686 int ret;
687
688 /* Parse UUID string into bytes */
689 ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
690 if (ret)
691 return log_msg_ret("bad hex", -EINVAL);
692
693 /* BufferOp */
694 acpigen_emit_byte(ctx, BUFFER_OP);
695 acpigen_write_len_f(ctx);
696
697 /* Buffer length in bytes */
698 acpigen_write_word(ctx, UUID_BIN_LEN);
699
700 /* Output UUID in expected order */
701 acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN);
702
703 acpigen_pop_len(ctx);
704
705 return 0;
706}
Simon Glass9238fac2020-07-07 13:11:59 -0600707
Simon Glassd0f7d9b2020-07-07 13:12:00 -0600708void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
709 uint order, const char *const dev_states[],
710 size_t dev_states_count)
711{
712 size_t i;
713
714 for (i = 0; i < dev_states_count; i++) {
715 acpigen_write_name(ctx, dev_states[i]);
716 acpigen_write_package(ctx, 1);
717 acpigen_emit_simple_namestring(ctx, name);
718 acpigen_pop_len(ctx); /* Package */
719 }
720
721 acpigen_emit_ext_op(ctx, POWER_RES_OP);
722
723 acpigen_write_len_f(ctx);
724
725 acpigen_emit_simple_namestring(ctx, name);
726 acpigen_emit_byte(ctx, level);
727 acpigen_emit_word(ctx, order);
728}
729
Simon Glass9238fac2020-07-07 13:11:59 -0600730/* Sleep (ms) */
731void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
732{
733 acpigen_emit_ext_op(ctx, SLEEP_OP);
734 acpigen_write_integer(ctx, sleep_ms);
735}
736
737void acpigen_write_store(struct acpi_ctx *ctx)
738{
739 acpigen_emit_byte(ctx, STORE_OP);
740}
741
742/* Or (arg1, arg2, res) */
743void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
744{
745 acpigen_emit_byte(ctx, OR_OP);
746 acpigen_emit_byte(ctx, arg1);
747 acpigen_emit_byte(ctx, arg2);
748 acpigen_emit_byte(ctx, res);
749}
750
751/* And (arg1, arg2, res) */
752void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
753{
754 acpigen_emit_byte(ctx, AND_OP);
755 acpigen_emit_byte(ctx, arg1);
756 acpigen_emit_byte(ctx, arg2);
757 acpigen_emit_byte(ctx, res);
758}
759
760/* Not (arg, res) */
761void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res)
762{
763 acpigen_emit_byte(ctx, NOT_OP);
764 acpigen_emit_byte(ctx, arg);
765 acpigen_emit_byte(ctx, res);
766}
767
768/* Store (str, DEBUG) */
769void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
770{
771 acpigen_write_store(ctx);
772 acpigen_write_string(ctx, str);
773 acpigen_emit_ext_op(ctx, DEBUG_OP);
774}
Simon Glass8a200762020-07-07 13:12:01 -0600775
Simon Glassca727632020-09-22 12:44:57 -0600776void acpigen_write_if(struct acpi_ctx *ctx)
777{
778 acpigen_emit_byte(ctx, IF_OP);
779 acpigen_write_len_f(ctx);
780}
781
782void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val)
783{
784 acpigen_write_if(ctx);
785 acpigen_emit_byte(ctx, LEQUAL_OP);
786 acpigen_emit_byte(ctx, op);
787 acpigen_write_integer(ctx, val);
788}
789
790void acpigen_write_else(struct acpi_ctx *ctx)
791{
792 acpigen_emit_byte(ctx, ELSE_OP);
793 acpigen_write_len_f(ctx);
794}
795
796void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst)
797{
798 acpigen_emit_byte(ctx, TO_BUFFER_OP);
799 acpigen_emit_byte(ctx, src);
800 acpigen_emit_byte(ctx, dst);
801}
802
803void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst)
804{
805 acpigen_emit_byte(ctx, TO_INTEGER_OP);
806 acpigen_emit_byte(ctx, src);
807 acpigen_emit_byte(ctx, dst);
808}
809
810void acpigen_write_byte_buffer(struct acpi_ctx *ctx, u8 *arr, size_t size)
811{
812 size_t i;
813
814 acpigen_emit_byte(ctx, BUFFER_OP);
815 acpigen_write_len_f(ctx);
816 acpigen_write_integer(ctx, size);
817
818 for (i = 0; i < size; i++)
819 acpigen_emit_byte(ctx, arr[i]);
820
821 acpigen_pop_len(ctx);
822}
823
824void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
825 size_t size)
826{
827 acpigen_emit_byte(ctx, RETURN_OP);
828 acpigen_write_byte_buffer(ctx, arr, size);
829}
830
831void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg)
832{
833 u8 buf = arg;
834
835 acpigen_write_return_byte_buffer(ctx, &buf, 1);
836}
837
838void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg)
839{
840 acpigen_emit_byte(ctx, RETURN_OP);
841 acpigen_write_byte(ctx, arg);
842}
843
Simon Glass4c8cbbd2020-09-22 12:44:58 -0600844void acpigen_write_dsm_start(struct acpi_ctx *ctx)
845{
846 /* Method (_DSM, 4, Serialized) */
847 acpigen_write_method_serialized(ctx, "_DSM", 4);
848
849 /* ToBuffer (Arg0, Local0) */
850 acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP);
851}
852
853int acpigen_write_dsm_uuid_start(struct acpi_ctx *ctx, const char *uuid)
854{
855 int ret;
856
857 /* If (LEqual (Local0, ToUUID(uuid))) */
858 acpigen_write_if(ctx);
859 acpigen_emit_byte(ctx, LEQUAL_OP);
860 acpigen_emit_byte(ctx, LOCAL0_OP);
861 ret = acpigen_write_uuid(ctx, uuid);
862 if (ret)
863 return log_msg_ret("uuid", ret);
864
865 /* ToInteger (Arg2, Local1) */
866 acpigen_write_to_integer(ctx, ARG2_OP, LOCAL1_OP);
867
868 return 0;
869}
870
871void acpigen_write_dsm_uuid_start_cond(struct acpi_ctx *ctx, int seq)
872{
873 /* If (LEqual (Local1, i)) */
874 acpigen_write_if_lequal_op_int(ctx, LOCAL1_OP, seq);
875}
876
877void acpigen_write_dsm_uuid_end_cond(struct acpi_ctx *ctx)
878{
879 acpigen_pop_len(ctx); /* If */
880}
881
882void acpigen_write_dsm_uuid_end(struct acpi_ctx *ctx)
883{
884 /* Default case: Return (Buffer (One) { 0x0 }) */
885 acpigen_write_return_singleton_buffer(ctx, 0x0);
886
887 acpigen_pop_len(ctx); /* If (LEqual (Local0, ToUUID(uuid))) */
888}
889
890void acpigen_write_dsm_end(struct acpi_ctx *ctx)
891{
892 /* Return (Buffer (One) { 0x0 }) */
893 acpigen_write_return_singleton_buffer(ctx, 0x0);
894
895 acpigen_pop_len(ctx); /* Method _DSM */
896}
897
Simon Glass8a200762020-07-07 13:12:01 -0600898/**
899 * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
900 *
901 * Store (\_SB.GPC0 (addr), Local5)
902 *
903 * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in
904 * the board's gpiolib.asl
905 *
906 * The value needs to be stored in a local variable so that it can be used in
907 * expressions in the ACPI code.
908 *
909 * @ctx: ACPI context pointer
910 * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0"
911 * @addr: GPIO pin configuration register address
912 *
913 */
914static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx,
915 const char *dw0_read, ulong addr)
916{
917 acpigen_write_store(ctx);
918 acpigen_emit_namestring(ctx, dw0_read);
919 acpigen_write_integer(ctx, addr);
920 acpigen_emit_byte(ctx, LOCAL5_OP);
921}
922
923/**
924 * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off
925 *
926 * @ctx: ACPI context pointer
927 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
928 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
929 * @gpio_num: GPIO number to adjust
930 * @vaL: true to set on, false to set off
931 */
932static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val,
933 const char *dw0_read, const char *dw0_write,
934 struct acpi_gpio *gpio, bool val)
935{
936 acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr);
937
938 /* Store (0x40, Local0) */
939 acpigen_write_store(ctx);
940 acpigen_write_integer(ctx, tx_state_val);
941 acpigen_emit_byte(ctx, LOCAL0_OP);
942
943 if (val) {
944 /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */
945 acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP);
946 } else {
947 /* Not (PAD_CFG0_TX_STATE, Local6) */
948 acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP);
949
950 /* And (Local5, Local6, Local5) */
951 acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP);
952 }
953
954 /*
955 * \_SB.SPC0 (addr, Local5)
956 * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in
957 * gpiolib.asl.
958 */
959 acpigen_emit_namestring(ctx, dw0_write);
960 acpigen_write_integer(ctx, gpio->pin0_addr);
961 acpigen_emit_byte(ctx, LOCAL5_OP);
962
963 return 0;
964}
965
966int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
967 const char *dw0_read, const char *dw0_write,
968 struct acpi_gpio *gpio, bool enable)
969{
970 bool set;
971 int ret;
972
973 set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable;
974 ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio,
975 set);
976 if (ret)
977 return log_msg_ret("call", ret);
978
979 return 0;
980}