blob: 63953b86b1d9480a79df148d96d16974abccb6d3 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Rudolf Marek293b5f52009-02-01 18:35:15 +00002
Paul Menzel0f4c0e22013-02-22 12:33:08 +01003/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +00004#define ACPIGEN_LENSTACK_SIZE 10
5
Paul Menzel0f4c0e22013-02-22 12:33:08 +01006/*
Timothy Pearson83abd812015-06-08 19:35:06 -05007 * If you need to change this, change acpigen_write_len_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +01008 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +01009 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000010
Timothy Pearson83abd812015-06-08 19:35:06 -050011#define ACPIGEN_MAXLEN 0xfffff
Rudolf Marek293b5f52009-02-01 18:35:15 +000012
Duncan Laurie3829f232016-05-09 11:08:46 -070013#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000014#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070015#include <acpi/acpigen.h>
Elyes HAOUAScd4fe0f2019-03-29 17:12:15 +010016#include <assert.h>
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -060017#include <commonlib/helpers.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000018#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000019#include <device/device.h>
Duncan Laurie08a942f2020-04-29 12:13:14 -070020#include <device/soundwire.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010021#include <types.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000022
23static char *gencurrent;
24
25char *len_stack[ACPIGEN_LENSTACK_SIZE];
26int ltop = 0;
27
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010028void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000029{
30 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010031 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000032 acpigen_emit_byte(0);
33 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050034 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000035}
36
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010037void acpigen_pop_len(void)
38{
39 int len;
40 ASSERT(ltop > 0)
41 char *p = len_stack[--ltop];
42 len = gencurrent - p;
43 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050044 /* generate store length for 0xfffff max */
45 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010046 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050047 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010048
49}
50
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000051void acpigen_set_current(char *curr)
52{
53 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000054}
55
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000056char *acpigen_get_current(void)
57{
58 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000059}
60
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010061void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000062{
63 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000064}
65
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070066void acpigen_emit_ext_op(uint8_t op)
67{
68 acpigen_emit_byte(EXT_OP_PREFIX);
69 acpigen_emit_byte(op);
70}
71
Duncan Laurie9ccae752016-05-09 07:43:19 -070072void acpigen_emit_word(unsigned int data)
73{
74 acpigen_emit_byte(data & 0xff);
75 acpigen_emit_byte((data >> 8) & 0xff);
76}
77
78void acpigen_emit_dword(unsigned int data)
79{
80 acpigen_emit_byte(data & 0xff);
81 acpigen_emit_byte((data >> 8) & 0xff);
82 acpigen_emit_byte((data >> 16) & 0xff);
83 acpigen_emit_byte((data >> 24) & 0xff);
84}
85
Duncan Laurie85d80272016-07-02 19:53:54 -070086char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000087{
Duncan Laurie85d80272016-07-02 19:53:54 -070088 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070089 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010090 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -070091 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +000092 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -070093 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +000094}
95
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010096void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000097{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070098 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +000099 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000100}
101
Duncan Laurie9ccae752016-05-09 07:43:19 -0700102void acpigen_write_word(unsigned int data)
103{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700104 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700105 acpigen_emit_word(data);
106}
107
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100108void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000109{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700110 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700111 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000112}
113
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100114void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000115{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700116 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700117 acpigen_emit_dword(data & 0xffffffff);
118 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000119}
120
Duncan Laurief7c38762016-05-09 08:20:38 -0700121void acpigen_write_zero(void)
122{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700123 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700124}
125
126void acpigen_write_one(void)
127{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700128 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700129}
130
131void acpigen_write_ones(void)
132{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700133 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700134}
135
136void acpigen_write_integer(uint64_t data)
137{
138 if (data == 0)
139 acpigen_write_zero();
140 else if (data == 1)
141 acpigen_write_one();
142 else if (data <= 0xff)
143 acpigen_write_byte((unsigned char)data);
144 else if (data <= 0xffff)
145 acpigen_write_word((unsigned int)data);
146 else if (data <= 0xffffffff)
147 acpigen_write_dword((unsigned int)data);
148 else
149 acpigen_write_qword(data);
150}
151
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100152void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000153{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100154 acpigen_write_name(name);
155 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000156}
157
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100158void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000159{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100160 acpigen_write_name(name);
161 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000162}
163
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100164void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000165{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100166 acpigen_write_name(name);
167 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000168}
169
Duncan Laurief7c38762016-05-09 08:20:38 -0700170void acpigen_write_name_integer(const char *name, uint64_t val)
171{
172 acpigen_write_name(name);
173 acpigen_write_integer(val);
174}
175
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700176void acpigen_write_name_string(const char *name, const char *string)
177{
178 acpigen_write_name(name);
179 acpigen_write_string(string);
180}
181
Patrick Rudolph389c8272019-12-17 13:54:41 +0100182void acpigen_write_name_unicode(const char *name, const char *string)
183{
184 const size_t len = strlen(string) + 1;
185 acpigen_write_name(name);
186 acpigen_emit_byte(BUFFER_OP);
187 acpigen_write_len_f();
188 acpigen_write_integer(len);
189 for (size_t i = 0; i < len; i++) {
190 const char c = string[i];
191 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
192 acpigen_emit_word(c >= 0 ? c : '?');
193 }
194 acpigen_pop_len();
195}
196
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100197void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000198{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000199 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700200 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000201 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000202}
203
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700204void acpigen_emit_string(const char *string)
205{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200206 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700207 acpigen_emit_byte('\0'); /* NUL */
208}
209
210void acpigen_write_string(const char *string)
211{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700212 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700213 acpigen_emit_string(string);
214}
215
Duncan Laurie37319032016-05-26 12:47:05 -0700216void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
217{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800218 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700219
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700220 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700221 acpigen_write_name_string("_HID", hid);
222}
223
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100224/*
225 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600226 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
227 * and "By convention, when an ASL compiler pads a name shorter than 4
228 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100229 *
230 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
231 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000232
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700233static void acpigen_emit_simple_namestring(const char *name)
234{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100235 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000236 char ud[] = "____";
237 for (i = 0; i < 4; i++) {
238 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100239 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000240 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000241 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700242 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000243 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000244}
245
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700246static void acpigen_emit_double_namestring(const char *name, int dotpos)
247{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700248 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100249 acpigen_emit_simple_namestring(name);
250 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000251}
252
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700253static void acpigen_emit_multi_namestring(const char *name)
254{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100255 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000256 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700257 acpigen_emit_byte(MULTI_NAME_PREFIX);
258 acpigen_emit_byte(ZERO_OP);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100259 pathlen = ((unsigned char *)acpigen_get_current()) - 1;
Rudolf Marek3310d752009-06-21 20:26:13 +0000260
261 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100262 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000263 /* find end or next entity */
264 while ((name[0] != '.') && (name[0] != '\0'))
265 name++;
266 /* forward to next */
267 if (name[0] == '.')
268 name++;
269 count++;
270 }
271
272 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000273}
274
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700275void acpigen_emit_namestring(const char *namepath)
276{
Rudolf Marek3310d752009-06-21 20:26:13 +0000277 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000278 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000279
Ronak Kanabar7c0f0072020-11-30 15:40:51 +0530280 /* Check for NULL pointer */
281 if (!namepath)
282 return;
283
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600284 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000285 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100286 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000287 namepath++;
288 }
289
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600290 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000291 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100292 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000293 namepath++;
294 }
295
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200296 /* If we have only \\ or only ^...^. Then we need to put a null
297 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200298 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700299 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100300 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200301 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000302
303 i = 0;
304 while (namepath[i] != '\0') {
305 if (namepath[i] == '.') {
306 dotcount++;
307 dotpos = i;
308 }
309 i++;
310 }
311
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700312 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100313 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700314 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100315 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700316 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100317 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000318}
319
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100320void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000321{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700322 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100323 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000324}
325
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100326void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000327{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700328 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100329 acpigen_write_len_f();
330 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000331}
Rudolf Marekf997b552009-02-14 15:40:23 +0000332
Duncan Laurie2fe74712020-06-01 17:36:56 -0700333void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
334{
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800335 /* <dest_op> = DeRefOf (<package_op>[<element>]) */
Duncan Laurie2fe74712020-06-01 17:36:56 -0700336 acpigen_write_store();
337 acpigen_emit_byte(DEREF_OP);
338 acpigen_emit_byte(INDEX_OP);
339 acpigen_emit_byte(package_op);
340 acpigen_write_integer(element);
341 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
342 acpigen_emit_byte(dest_op);
343}
344
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800345void acpigen_set_package_op_element_int(uint8_t package_op, unsigned int element, uint64_t src)
346{
347 /* DeRefOf (<package>[<element>]) = <src> */
348 acpigen_write_store();
349 acpigen_write_integer(src);
350 acpigen_emit_byte(DEREF_OP);
351 acpigen_emit_byte(INDEX_OP);
352 acpigen_emit_byte(package_op);
353 acpigen_write_integer(element);
354 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
355}
356
357void acpigen_get_package_element(const char *package, unsigned int element, uint8_t dest_op)
358{
359 /* <dest_op> = <package>[<element>] */
360 acpigen_write_store();
361 acpigen_emit_byte(INDEX_OP);
362 acpigen_emit_namestring(package);
363 acpigen_write_integer(element);
364 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
365 acpigen_emit_byte(dest_op);
366}
367
368void acpigen_set_package_element_int(const char *package, unsigned int element, uint64_t src)
369{
370 /* <package>[<element>] = <src> */
371 acpigen_write_store();
372 acpigen_write_integer(src);
373 acpigen_emit_byte(INDEX_OP);
374 acpigen_emit_namestring(package);
375 acpigen_write_integer(element);
376 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
377}
378
379void acpigen_set_package_element_namestr(const char *package, unsigned int element,
380 const char *src)
381{
382 /* <package>[<element>] = <src> */
383 acpigen_write_store();
384 acpigen_emit_namestring(src);
385 acpigen_emit_byte(INDEX_OP);
386 acpigen_emit_namestring(package);
387 acpigen_write_integer(element);
388 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
389}
390
Felix Heldb57b12f2023-01-24 19:31:00 +0100391void acpigen_write_processor_namestring(unsigned int cpu_index)
392{
393 char buffer[16];
394 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING, cpu_index);
395 acpigen_emit_namestring(buffer);
396}
397
Elyes Haouas9c824912023-01-28 09:13:17 +0100398/* Processor() operator is deprecated as of ACPI 6.0, use Device() instead. */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100399void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000400{
401/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100402 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200403 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000404*/
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700405 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100406 acpigen_write_len_f();
Felix Heldb57b12f2023-01-24 19:31:00 +0100407 acpigen_write_processor_namestring(cpuindex);
Rudolf Marekf997b552009-02-14 15:40:23 +0000408 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700409 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000410 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000411}
412
Felix Held32bba182023-01-28 03:37:21 +0100413void acpigen_write_processor_device(unsigned int cpu_index)
414{
415 acpigen_emit_ext_op(DEVICE_OP);
416 acpigen_write_len_f();
417 acpigen_write_processor_namestring(cpu_index);
418 acpigen_write_name_string("_HID", "ACPI0007");
419 acpigen_write_name_integer("_UID", cpu_index);
420}
421
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100422void acpigen_write_processor_package(const char *const name, const unsigned int first_core,
Nico Huber4d211ac2017-09-01 21:14:36 +0200423 const unsigned int core_count)
424{
425 unsigned int i;
Nico Huber4d211ac2017-09-01 21:14:36 +0200426
427 acpigen_write_name(name);
428 acpigen_write_package(core_count);
Felix Heldb57b12f2023-01-24 19:31:00 +0100429
430 for (i = first_core; i < first_core + core_count; ++i)
431 acpigen_write_processor_namestring(i);
432
Nico Huber4d211ac2017-09-01 21:14:36 +0200433 acpigen_pop_len();
434}
435
Arthur Heymans8f055272018-11-28 13:18:57 +0100436/* Method to notify all CPU cores */
437void acpigen_write_processor_cnot(const unsigned int number_of_cores)
438{
439 int core_id;
440
Christian Walterbe3979c2019-12-18 15:07:59 +0100441 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100442 for (core_id = 0; core_id < number_of_cores; core_id++) {
Arthur Heymans8f055272018-11-28 13:18:57 +0100443 acpigen_emit_byte(NOTIFY_OP);
Felix Heldb57b12f2023-01-24 19:31:00 +0100444 acpigen_write_processor_namestring(core_id);
Arthur Heymans8f055272018-11-28 13:18:57 +0100445 acpigen_emit_byte(ARG0_OP);
446 }
447 acpigen_pop_len();
448}
449
Naresh G Solanki8535c662016-10-25 00:51:24 +0530450/*
451 * Generate ACPI AML code for OperationRegion
452 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
453 * where rname is region name, space is region space, offset is region offset &
454 * len is region length.
455 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
456 */
Duncan Laurie35029602020-10-09 17:50:25 +0000457void acpigen_write_opregion(const struct opregion *opreg)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530458{
459 /* OpregionOp */
460 acpigen_emit_ext_op(OPREGION_OP);
461 /* NameString 4 chars only */
462 acpigen_emit_simple_namestring(opreg->name);
463 /* RegionSpace */
464 acpigen_emit_byte(opreg->regionspace);
465 /* RegionOffset & RegionLen, it can be byte word or double word */
466 acpigen_write_integer(opreg->regionoffset);
467 acpigen_write_integer(opreg->regionlen);
468}
469
Patrick Rudolph32bae492019-08-08 12:47:30 +0200470/*
471 * Generate ACPI AML code for Mutex
472 * Arg0: Pointer to name of mutex
473 * Arg1: Initial value of mutex
474 */
475void acpigen_write_mutex(const char *name, const uint8_t flags)
476{
477 /* MutexOp */
478 acpigen_emit_ext_op(MUTEX_OP);
479 /* NameString 4 chars only */
480 acpigen_emit_simple_namestring(name);
481 acpigen_emit_byte(flags);
482}
483
484void acpigen_write_acquire(const char *name, const uint16_t val)
485{
486 /* AcquireOp */
487 acpigen_emit_ext_op(ACQUIRE_OP);
488 /* NameString 4 chars only */
489 acpigen_emit_simple_namestring(name);
490 acpigen_emit_word(val);
491}
492
493void acpigen_write_release(const char *name)
494{
495 /* ReleaseOp */
496 acpigen_emit_ext_op(RELEASE_OP);
497 /* NameString 4 chars only */
498 acpigen_emit_simple_namestring(name);
499}
500
Patrick Rudolph894977b2017-07-01 16:15:05 +0200501static void acpigen_write_field_length(uint32_t len)
502{
503 uint8_t i, j;
504 uint8_t emit[4];
505
506 i = 1;
507 if (len < 0x40) {
508 emit[0] = len & 0x3F;
509 } else {
510 emit[0] = len & 0xF;
511 len >>= 4;
512 while (len) {
513 emit[i] = len & 0xFF;
514 i++;
515 len >>= 8;
516 }
517 }
518 /* Update bit 7:6 : Number of bytes followed by emit[0] */
519 emit[0] |= (i - 1) << 6;
520
521 for (j = 0; j < i; j++)
522 acpigen_emit_byte(emit[j]);
523}
524
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100525static void acpigen_write_field_offset(uint32_t offset, uint32_t current_bit_pos)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530526{
527 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530528
529 if (offset < current_bit_pos) {
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100530 printk(BIOS_WARNING, "%s: Cannot move offset backward", __func__);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530531 return;
532 }
533
534 diff_bits = offset - current_bit_pos;
535 /* Upper limit */
536 if (diff_bits > 0xFFFFFFF) {
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100537 printk(BIOS_WARNING, "%s: Offset very large to encode", __func__);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530538 return;
539 }
540
Naresh G Solanki8535c662016-10-25 00:51:24 +0530541 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200542 acpigen_write_field_length(diff_bits);
543}
544
Michael Niewöhner060dc7b2022-05-13 00:04:19 +0200545void acpigen_write_field_name(const char *name, uint32_t size)
Patrick Rudolph894977b2017-07-01 16:15:05 +0200546{
547 acpigen_emit_simple_namestring(name);
548 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530549}
550
Duncan Laurie095bbf92020-09-30 23:09:29 +0000551static void acpigen_write_field_reserved(uint32_t size)
552{
553 acpigen_emit_byte(0);
554 acpigen_write_field_length(size);
555}
556
Naresh G Solanki8535c662016-10-25 00:51:24 +0530557/*
558 * Generate ACPI AML code for Field
559 * Arg0: region name
560 * Arg1: Pointer to struct fieldlist.
561 * Arg2: no. of entries in Arg1
562 * Arg3: flags which indicate filed access type, lock rule & update rule.
563 * Example with fieldlist
564 * struct fieldlist l[] = {
565 * FIELDLIST_OFFSET(0x84),
566 * FIELDLIST_NAMESTR("PMCS", 2),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000567 * FIELDLIST_RESERVED(6),
Naresh G Solanki8535c662016-10-25 00:51:24 +0530568 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200569 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530570 * FIELD_PRESERVE);
571 * Output:
572 * Field (UART, AnyAcc, NoLock, Preserve)
573 * {
574 * Offset (0x84),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000575 * PMCS, 2,
576 * , 6,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530577 * }
578 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700579void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530580 uint8_t flags)
581{
582 uint16_t i;
583 uint32_t current_bit_pos = 0;
584
585 /* FieldOp */
586 acpigen_emit_ext_op(FIELD_OP);
587 /* Package Length */
588 acpigen_write_len_f();
589 /* NameString 4 chars only */
590 acpigen_emit_simple_namestring(name);
591 /* Field Flag */
592 acpigen_emit_byte(flags);
593
594 for (i = 0; i < count; i++) {
595 switch (l[i].type) {
596 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200597 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530598 current_bit_pos += l[i].bits;
599 break;
Duncan Laurie095bbf92020-09-30 23:09:29 +0000600 case RESERVED:
601 acpigen_write_field_reserved(l[i].bits);
602 current_bit_pos += l[i].bits;
603 break;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530604 case OFFSET:
605 acpigen_write_field_offset(l[i].bits, current_bit_pos);
606 current_bit_pos = l[i].bits;
607 break;
608 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100609 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530610 break;
611 }
612 }
613 acpigen_pop_len();
614}
615
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200616/*
617 * Generate ACPI AML code for IndexField
618 * Arg0: region name
619 * Arg1: Pointer to struct fieldlist.
620 * Arg2: no. of entries in Arg1
621 * Arg3: flags which indicate filed access type, lock rule & update rule.
622 * Example with fieldlist
623 * struct fieldlist l[] = {
624 * FIELDLIST_OFFSET(0x84),
625 * FIELDLIST_NAMESTR("PMCS", 2),
626 * };
627 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
628 * FIELD_NOLOCK |
629 * FIELD_PRESERVE);
630 * Output:
631 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
632 * {
633 * Offset (0x84),
634 * PMCS, 2
635 * }
636 */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100637void acpigen_write_indexfield(const char *idx, const char *data, struct fieldlist *l,
638 size_t count, uint8_t flags)
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200639{
640 uint16_t i;
641 uint32_t current_bit_pos = 0;
642
643 /* FieldOp */
644 acpigen_emit_ext_op(INDEX_FIELD_OP);
645 /* Package Length */
646 acpigen_write_len_f();
647 /* NameString 4 chars only */
648 acpigen_emit_simple_namestring(idx);
649 /* NameString 4 chars only */
650 acpigen_emit_simple_namestring(data);
651 /* Field Flag */
652 acpigen_emit_byte(flags);
653
654 for (i = 0; i < count; i++) {
655 switch (l[i].type) {
656 case NAME_STRING:
657 acpigen_write_field_name(l[i].name, l[i].bits);
658 current_bit_pos += l[i].bits;
659 break;
660 case OFFSET:
661 acpigen_write_field_offset(l[i].bits, current_bit_pos);
662 current_bit_pos = l[i].bits;
663 break;
664 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100665 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type);
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200666 break;
667 }
668 }
669 acpigen_pop_len();
670}
671
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100672void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000673{
674/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200675 Name (_PCT, Package (0x02)
676 {
677 ResourceTemplate ()
678 {
679 Register (FFixedHW,
680 0x00, // Bit Width
681 0x00, // Bit Offset
682 0x0000000000000000, // Address
683 ,)
684 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000685
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200686 ResourceTemplate ()
687 {
688 Register (FFixedHW,
689 0x00, // Bit Width
690 0x00, // Bit Offset
691 0x0000000000000000, // Address
692 ,)
693 }
694 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000695*/
696 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700697 /* 00000030 "0._PCT.," */
698 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
699 /* 00000038 "........" */
700 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
701 /* 00000040 "........" */
702 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
703 /* 00000048 "....y..." */
704 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
705 /* 00000050 "........" */
706 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
707 /* 00000058 "........" */
708 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000709 0x00, 0x79, 0x00
710 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100711 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000712}
713
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100714void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200715{
716/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200717 Name (_PTC, Package (0x02)
718 {
719 ResourceTemplate ()
720 {
721 Register (FFixedHW,
722 0x00, // Bit Width
723 0x00, // Bit Offset
724 0x0000000000000000, // Address
725 ,)
726 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200727
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200728 ResourceTemplate ()
729 {
730 Register (FFixedHW,
731 0x00, // Bit Width
732 0x00, // Bit Offset
733 0x0000000000000000, // Address
734 ,)
735 }
736 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200737*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200738 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100739 .space_id = ACPI_ADDRESS_SPACE_FIXED,
740 .bit_width = 0,
741 .bit_offset = 0,
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +0200742 .access_size = ACPI_ACCESS_SIZE_UNDEFINED,
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100743 .addrl = 0,
744 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200745 };
746
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100747 acpigen_write_name("_PTC");
748 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200749
750 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700751 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200752
753 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700754 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200755
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100756 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200757}
758
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700759static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100760{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700761 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100762 acpigen_write_len_f();
763 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700764 acpigen_emit_byte(flags);
765}
766
767/* Method (name, nargs, NotSerialized) */
768void acpigen_write_method(const char *name, int nargs)
769{
770 __acpigen_write_method(name, (nargs & 7));
771}
772
773/* Method (name, nargs, Serialized) */
774void acpigen_write_method_serialized(const char *name, int nargs)
775{
776 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100777}
778
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100779void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100780{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700781 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100782 acpigen_write_len_f();
783 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100784}
785
Raul E Rangel052c9632021-05-12 17:01:30 -0600786void acpigen_write_thermal_zone(const char *name)
787{
788 acpigen_emit_ext_op(THERMAL_ZONE_OP);
789 acpigen_write_len_f();
790 acpigen_emit_namestring(name);
791}
792
Duncan Laurieabe2de82016-05-09 11:08:46 -0700793void acpigen_write_STA(uint8_t status)
794{
795 /*
796 * Method (_STA, 0, NotSerialized) { Return (status) }
797 */
798 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700799 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700800 acpigen_write_byte(status);
801 acpigen_pop_len();
802}
803
Sugnan Prabhu S4f77f612020-06-10 09:51:02 +0530804void acpigen_write_STA_ext(const char *namestring)
805{
806 /*
807 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
808 */
809 acpigen_write_method("_STA", 0);
810 acpigen_emit_byte(RETURN_OP);
811 acpigen_emit_namestring(namestring);
812 acpigen_pop_len();
813}
814
Raul E Rangelc7048322021-04-19 15:58:25 -0600815void acpigen_write_LPI_package(u64 level, const struct acpi_lpi_state *states, u16 nentries)
816{
817 /*
818 * Name (_LPI, Package (0x06) // _LPI: Low Power Idle States
819 * {
820 * 0x0000,
821 * 0x0000000000000000,
822 * 0x0003,
823 * Package (0x0A)
824 * {
825 * 0x00000002,
826 * 0x00000001,
827 * 0x00000001,
828 * 0x00000000,
829 * 0x00000000,
830 * 0x00000000,
831 * ResourceTemplate ()
832 * {
833 * Register (FFixedHW,
834 * 0x02, // Bit Width
835 * 0x02, // Bit Offset
836 * 0x0000000000000000, // Address
837 * ,)
838 * },
839 *
840 * ResourceTemplate ()
841 * {
842 * Register (SystemMemory,
843 * 0x00, // Bit Width
844 * 0x00, // Bit Offset
845 * 0x0000000000000000, // Address
846 * ,)
847 * },
848 *
849 * ResourceTemplate ()
850 * {
851 * Register (SystemMemory,
852 * 0x00, // Bit Width
853 * 0x00, // Bit Offset
854 * 0x0000000000000000, // Address
855 * ,)
856 * },
857 *
858 * "C1"
859 * },
860 * ...
861 * }
862 */
863
864 acpigen_write_name("_LPI");
865 acpigen_write_package(3 + nentries);
866 acpigen_write_word(0); /* Revision */
867 acpigen_write_qword(level);
868 acpigen_write_word(nentries);
869
870 for (size_t i = 0; i < nentries; i++, states++) {
871 acpigen_write_package(0xA);
872 acpigen_write_dword(states->min_residency_us);
873 acpigen_write_dword(states->worst_case_wakeup_latency_us);
874 acpigen_write_dword(states->flags);
875 acpigen_write_dword(states->arch_context_lost_flags);
876 acpigen_write_dword(states->residency_counter_frequency_hz);
877 acpigen_write_dword(states->enabled_parent_state);
878 acpigen_write_register_resource(&states->entry_method);
879 acpigen_write_register_resource(&states->residency_counter_register);
880 acpigen_write_register_resource(&states->usage_counter_register);
881 acpigen_write_string(states->state_name);
882 acpigen_pop_len();
883 }
884 acpigen_pop_len();
885}
886
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100887/*
888 * Generates a func with max supported P-states.
889 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100890void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000891{
892/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200893 Method (_PPC, 0, NotSerialized)
894 {
895 Return (nr)
896 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000897*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100898 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700899 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000900 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100901 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100902 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000903}
904
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100905/*
906 * Generates a func with max supported P-states saved
907 * in the variable PPCM.
908 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100909void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700910{
911/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200912 Method (_PPC, 0, NotSerialized)
913 {
914 Return (PPCM)
915 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700916*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100917 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700918 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700919 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100920 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100921 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700922}
923
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100924void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200925{
926/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200927 // Sample _TPC method
928 Method (_TPC, 0, NotSerialized)
929 {
930 Return (\TLVL)
931 }
932*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100933 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700934 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100935 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100936 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200937}
938
Duncan Laurieabe2de82016-05-09 11:08:46 -0700939void acpigen_write_PRW(u32 wake, u32 level)
940{
941 /*
942 * Name (_PRW, Package () { wake, level }
943 */
944 acpigen_write_name("_PRW");
945 acpigen_write_package(2);
946 acpigen_write_integer(wake);
947 acpigen_write_integer(level);
948 acpigen_pop_len();
949}
950
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100951void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat, u32 busmLat, u32 control,
952 u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000953{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100954 acpigen_write_package(6);
955 acpigen_write_dword(coreFreq);
956 acpigen_write_dword(power);
957 acpigen_write_dword(transLat);
958 acpigen_write_dword(busmLat);
959 acpigen_write_dword(control);
960 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100961 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700962
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100963 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n", coreFreq, power,
964 control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000965}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000966
Jason Gleneskca36aed2020-09-15 21:01:57 -0700967void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
968{
969 size_t pstate;
970
971 acpigen_write_name("_PSS");
972 acpigen_write_package(nentries);
973 for (pstate = 0; pstate < nentries; pstate++) {
974 acpigen_write_PSS_package(
975 pstate_values->core_freq, pstate_values->power,
976 pstate_values->transition_latency, pstate_values->bus_master_latency,
977 pstate_values->control_value, pstate_values->status_value);
978 pstate_values++;
979 }
980
981 acpigen_pop_len();
982}
983
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100984void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000985{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100986 acpigen_write_name("_PSD");
987 acpigen_write_package(1);
988 acpigen_write_package(5);
989 acpigen_write_byte(5); // 5 values
990 acpigen_write_byte(0); // revision 0
991 acpigen_write_dword(domain);
992 acpigen_write_dword(coordtype);
993 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100994 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100995 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000996}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000997
Angel Ponsd2794ce2021-10-17 12:59:43 +0200998void acpigen_write_CST_package_entry(const acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200999{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001000 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001001 acpigen_write_register_resource(&cstate->resource);
Jason Glenesk201acca2020-09-11 12:36:15 -07001002 acpigen_write_byte(cstate->ctype);
1003 acpigen_write_word(cstate->latency);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001004 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001005 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001006}
1007
Angel Ponsd2794ce2021-10-17 12:59:43 +02001008void acpigen_write_CST_package(const acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001009{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001010 int i;
1011 acpigen_write_name("_CST");
1012 acpigen_write_package(nentries+1);
Jason Glenesk201acca2020-09-11 12:36:15 -07001013 acpigen_write_integer(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001014
1015 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001016 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001017
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001018 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001019}
1020
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001021void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
1022 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -05001023{
1024 acpigen_write_name("_CSD");
1025 acpigen_write_package(1);
1026 acpigen_write_package(6);
Jason Glenesk201acca2020-09-11 12:36:15 -07001027 acpigen_write_integer(6); // 6 values
Timothy Pearson83abd812015-06-08 19:35:06 -05001028 acpigen_write_byte(0); // revision 0
1029 acpigen_write_dword(domain);
1030 acpigen_write_dword(coordtype);
1031 acpigen_write_dword(numprocs);
1032 acpigen_write_dword(index);
1033 acpigen_pop_len();
1034 acpigen_pop_len();
1035}
1036
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001037void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001038{
1039/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +02001040 Sample _TSS package with 100% and 50% duty cycles
1041 Name (_TSS, Package (0x02)
1042 {
1043 Package(){100, 1000, 0, 0x00, 0)
1044 Package(){50, 520, 0, 0x18, 0)
1045 })
1046*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001047 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001048 acpi_tstate_t *tstate = tstate_list;
1049
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001050 acpigen_write_name("_TSS");
1051 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +02001052
1053 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001054 acpigen_write_package(5);
1055 acpigen_write_dword(tstate->percent);
1056 acpigen_write_dword(tstate->power);
1057 acpigen_write_dword(tstate->latency);
1058 acpigen_write_dword(tstate->control);
1059 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001060 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001061 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001062 }
1063
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001064 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001065}
1066
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001067void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001068{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001069 acpigen_write_name("_TSD");
1070 acpigen_write_package(1);
1071 acpigen_write_package(5);
1072 acpigen_write_byte(5); // 5 values
1073 acpigen_write_byte(0); // revision 0
1074 acpigen_write_dword(domain);
1075 acpigen_write_dword(coordtype);
1076 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001077 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001078 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001079}
1080
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001081void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001082{
1083 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001084 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001085 * Byte 0:
1086 * Bit7 : 1 => big item
1087 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
1088 */
1089 acpigen_emit_byte(0x86);
1090 /* Byte 1+2: length (0x0009) */
1091 acpigen_emit_byte(0x09);
1092 acpigen_emit_byte(0x00);
1093 /* bit1-7 are ignored */
1094 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -07001095 acpigen_emit_dword(base);
1096 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001097}
1098
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001099static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001100{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +02001101 acpigen_emit_byte(0x82); /* Register Descriptor */
1102 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
1103 acpigen_emit_byte(0x00); /* Register Length 15:8 */
1104 acpigen_emit_byte(addr->space_id); /* Address Space ID */
1105 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
1106 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +01001107 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -07001108 acpigen_emit_dword(addr->addrl); /* Register Address Low */
1109 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +02001110}
1111
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001112void acpigen_write_register_resource(const acpi_addr_t *addr)
1113{
1114 acpigen_write_resourcetemplate_header();
1115 acpigen_write_register(addr);
1116 acpigen_write_resourcetemplate_footer();
1117}
1118
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001119void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001120{
1121 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001122 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001123 * Byte 0:
1124 * Bit7 : 0 => small item
1125 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1126 * Bit2-0: 010 (0x2) => 2 Bytes long
1127 */
1128 acpigen_emit_byte(0x22);
1129 acpigen_emit_byte(mask & 0xff);
1130 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001131}
1132
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001133void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001134{
1135 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001136 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001137 * Byte 0:
1138 * Bit7 : 0 => small item
1139 * Bit6-3: 1000 (0x8) => I/O port descriptor
1140 * Bit2-0: 111 (0x7) => 7 Bytes long
1141 */
1142 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001143 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001144 /* bit1-7 are ignored */
1145 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1146 /* minimum base address the device may be configured for */
1147 acpigen_emit_byte(min & 0xff);
1148 acpigen_emit_byte((min >> 8) & 0xff);
1149 /* maximum base address the device may be configured for */
1150 acpigen_emit_byte(max & 0xff);
1151 acpigen_emit_byte((max >> 8) & 0xff);
1152 /* alignment for min base */
1153 acpigen_emit_byte(align & 0xff);
1154 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001155}
1156
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001157void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001158{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001159 /*
1160 * A ResourceTemplate() is a Buffer() with a
1161 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001162 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001163 * (small item 0xf, len 1)
1164 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001165 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001166 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001167 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001168 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001169 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001170 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001171 acpigen_emit_byte(0x00);
1172 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001173}
1174
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001175void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001176{
1177 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001178 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001179 /*
1180 * end tag (acpi 4.0 Section 6.4.2.8)
1181 * 0x79 <checksum>
1182 * 0x00 is treated as a good checksum according to the spec
1183 * and is what iasl generates.
1184 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001185 acpigen_emit_byte(0x79);
1186 acpigen_emit_byte(0x00);
1187
Nico Huber7d89ce32017-04-14 00:08:18 +02001188 /* Start counting past the 2-bytes length added in
1189 acpigen_write_resourcetemplate() above. */
1190 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001191
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001192 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001193 p[0] = len & 0xff;
1194 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001195 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001196 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001197}
1198
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001199static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev, struct resource *res)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001200{
1201 acpigen_write_mem32fixed(0, res->base, res->size);
1202}
1203
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001204static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev, struct resource *res)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001205{
1206 resource_t base = res->base;
1207 resource_t size = res->size;
1208 while (size > 0) {
1209 resource_t sz = size > 255 ? 255 : size;
1210 acpigen_write_io16(base, base, 0, sz, 1);
1211 size -= sz;
1212 base += sz;
1213 }
1214}
1215
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001216void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001217{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001218 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001219
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001220 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001221 search_global_resources(
1222 IORESOURCE_MEM | IORESOURCE_RESERVE,
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001223 IORESOURCE_MEM | IORESOURCE_RESERVE,
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001224 acpigen_add_mainboard_rsvd_mem32, 0);
1225
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001226 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001227 search_global_resources(
1228 IORESOURCE_IO | IORESOURCE_RESERVE,
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001229 IORESOURCE_IO | IORESOURCE_RESERVE,
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001230 acpigen_add_mainboard_rsvd_io, 0);
1231
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001232 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001233}
1234
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001235void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001236{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001237 acpigen_write_scope(scope);
1238 acpigen_write_name(name);
1239 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001240 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001241}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001242
1243static int hex2bin(const char c)
1244{
1245 if (c >= 'A' && c <= 'F')
1246 return c - 'A' + 10;
1247 if (c >= 'a' && c <= 'f')
1248 return c - 'a' + 10;
1249 return c - '0';
1250}
1251
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001252void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001253{
1254 u32 compact = 0;
1255
1256 /* Clamping individual values would be better but
1257 there is a disagreement over what is a valid
1258 EISA id, so accept anything and don't clamp,
1259 parent code should create a valid EISAid.
1260 */
1261 compact |= (eisaid[0] - 'A' + 1) << 26;
1262 compact |= (eisaid[1] - 'A' + 1) << 21;
1263 compact |= (eisaid[2] - 'A' + 1) << 16;
1264 compact |= hex2bin(eisaid[3]) << 12;
1265 compact |= hex2bin(eisaid[4]) << 8;
1266 compact |= hex2bin(eisaid[5]) << 4;
1267 compact |= hex2bin(eisaid[6]);
1268
1269 acpigen_emit_byte(0xc);
1270 acpigen_emit_byte((compact >> 24) & 0xff);
1271 acpigen_emit_byte((compact >> 16) & 0xff);
1272 acpigen_emit_byte((compact >> 8) & 0xff);
1273 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001274}
Duncan Laurie3829f232016-05-09 11:08:46 -07001275
1276/*
1277 * ToUUID(uuid)
1278 *
1279 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1280 * order for the bytes that make up a UUID Buffer object.
1281 * UUID byte order for input:
1282 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1283 * UUID byte order for output:
1284 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1285 */
1286#define UUID_LEN 16
1287void acpigen_write_uuid(const char *uuid)
1288{
1289 uint8_t buf[UUID_LEN];
1290 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1291 8, 9, 10, 11, 12, 13, 14, 15 };
1292
1293 /* Parse UUID string into bytes */
1294 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1295 return;
1296
1297 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001298 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001299 acpigen_write_len_f();
1300
1301 /* Buffer length in bytes */
1302 acpigen_write_word(UUID_LEN);
1303
1304 /* Output UUID in expected order */
1305 for (i = 0; i < UUID_LEN; i++)
1306 acpigen_emit_byte(buf[order[i]]);
1307
1308 acpigen_pop_len();
1309}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001310
1311/*
1312 * Name (_PRx, Package(One) { name })
1313 * ...
1314 * PowerResource (name, level, order)
1315 */
1316void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001317 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001318{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001319 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001320 for (i = 0; i < dev_states_count; i++) {
1321 acpigen_write_name(dev_states[i]);
1322 acpigen_write_package(1);
1323 acpigen_emit_simple_namestring(name);
1324 acpigen_pop_len(); /* Package */
1325 }
1326
1327 acpigen_emit_ext_op(POWER_RES_OP);
1328
1329 acpigen_write_len_f();
1330
1331 acpigen_emit_simple_namestring(name);
1332 acpigen_emit_byte(level);
1333 acpigen_emit_word(order);
1334}
1335
1336/* Sleep (ms) */
1337void acpigen_write_sleep(uint64_t sleep_ms)
1338{
1339 acpigen_emit_ext_op(SLEEP_OP);
1340 acpigen_write_integer(sleep_ms);
1341}
1342
1343void acpigen_write_store(void)
1344{
1345 acpigen_emit_byte(STORE_OP);
1346}
1347
1348/* Store (src, dst) */
1349void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1350{
1351 acpigen_write_store();
1352 acpigen_emit_byte(src);
1353 acpigen_emit_byte(dst);
1354}
1355
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001356/* Store (src, "namestr") */
1357void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1358{
1359 acpigen_write_store();
1360 acpigen_emit_byte(src);
1361 acpigen_emit_namestring(dst);
1362}
1363
Duncan Laurie8e391d32020-09-30 23:17:41 +00001364/* Store (src, "namestr") */
1365void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst)
1366{
1367 acpigen_write_store();
1368 acpigen_write_integer(src);
1369 acpigen_emit_namestring(dst);
1370}
1371
Cliff Huang24f3dc82023-02-04 21:11:51 -08001372/* Store ("namestr", dst) */
1373void acpigen_write_store_namestr_to_op(const char *src, uint8_t dst)
1374{
1375 acpigen_write_store();
1376 acpigen_emit_namestring(src);
1377 acpigen_emit_byte(dst);
1378}
1379
Duncan Laurie8e391d32020-09-30 23:17:41 +00001380/* Store (src, dst) */
1381void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst)
1382{
1383 acpigen_write_store();
1384 acpigen_write_integer(src);
1385 acpigen_emit_byte(dst);
1386}
1387
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001388/* Or (arg1, arg2, res) */
1389void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1390{
1391 acpigen_emit_byte(OR_OP);
1392 acpigen_emit_byte(arg1);
1393 acpigen_emit_byte(arg2);
1394 acpigen_emit_byte(res);
1395}
1396
Rajat Jain310623b2020-02-26 11:02:53 -08001397/* Xor (arg1, arg2, res) */
1398void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1399{
1400 acpigen_emit_byte(XOR_OP);
1401 acpigen_emit_byte(arg1);
1402 acpigen_emit_byte(arg2);
1403 acpigen_emit_byte(res);
1404}
1405
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001406/* And (arg1, arg2, res) */
1407void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1408{
1409 acpigen_emit_byte(AND_OP);
1410 acpigen_emit_byte(arg1);
1411 acpigen_emit_byte(arg2);
1412 acpigen_emit_byte(res);
1413}
1414
1415/* Not (arg, res) */
1416void acpigen_write_not(uint8_t arg, uint8_t res)
1417{
1418 acpigen_emit_byte(NOT_OP);
1419 acpigen_emit_byte(arg);
1420 acpigen_emit_byte(res);
1421}
1422
Cliff Huange6083082023-01-19 21:18:23 -08001423/* Concatenate (str1, str2, res) */
1424void acpigen_concatenate_string_string(const char *str1, const char *str2, uint8_t res)
1425{
1426 acpigen_emit_byte(CONCATENATE_OP);
1427 acpigen_write_string(str1);
1428 acpigen_write_string(str2);
1429 acpigen_emit_byte(res);
1430}
1431
1432/* Concatenate (str, val, tmp_res) */
1433void acpigen_concatenate_string_int(const char *str, uint64_t val, uint8_t res)
1434{
1435 acpigen_emit_byte(CONCATENATE_OP);
1436 acpigen_write_string(str);
1437 acpigen_write_integer(val);
1438 acpigen_emit_byte(res);
1439}
1440
1441/* Concatenate (str, src_res, dest_res) */
1442void acpigen_concatenate_string_op(const char *str, uint8_t src_res, uint8_t dest_res)
1443{
1444 acpigen_emit_byte(CONCATENATE_OP);
1445 acpigen_write_string(str);
1446 acpigen_emit_byte(src_res);
1447 acpigen_emit_byte(dest_res);
1448}
1449
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001450/* Store (str, DEBUG) */
1451void acpigen_write_debug_string(const char *str)
1452{
1453 acpigen_write_store();
1454 acpigen_write_string(str);
1455 acpigen_emit_ext_op(DEBUG_OP);
1456}
1457
1458/* Store (val, DEBUG) */
1459void acpigen_write_debug_integer(uint64_t val)
1460{
1461 acpigen_write_store();
1462 acpigen_write_integer(val);
1463 acpigen_emit_ext_op(DEBUG_OP);
1464}
1465
1466/* Store (op, DEBUG) */
1467void acpigen_write_debug_op(uint8_t op)
1468{
1469 acpigen_write_store();
1470 acpigen_emit_byte(op);
1471 acpigen_emit_ext_op(DEBUG_OP);
1472}
1473
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001474/* Store (str, DEBUG) */
1475void acpigen_write_debug_namestr(const char *str)
1476{
1477 acpigen_write_store();
1478 acpigen_emit_namestring(str);
1479 acpigen_emit_ext_op(DEBUG_OP);
1480}
1481
Cliff Huange6083082023-01-19 21:18:23 -08001482/* Concatenate (str1, str2, tmp_res)
1483 Store(tmp_res, DEBUG) */
1484void acpigen_write_debug_concatenate_string_string(const char *str1, const char *str2,
1485 uint8_t tmp_res)
1486{
1487 acpigen_concatenate_string_string(str1, str2, tmp_res);
1488 acpigen_write_debug_op(tmp_res);
1489}
1490
1491/* Concatenate (str1, val, tmp_res)
1492 Store(tmp_res, DEBUG) */
1493void acpigen_write_debug_concatenate_string_int(const char *str, uint64_t val,
1494 uint8_t tmp_res)
1495{
1496 acpigen_concatenate_string_int(str, val, tmp_res);
1497 acpigen_write_debug_op(tmp_res);
1498}
1499
1500/* Concatenate (str1, res, tmp_res)
1501 Store(tmp_res, DEBUG) */
1502void acpigen_write_debug_concatenate_string_op(const char *str, uint8_t res,
1503 uint8_t tmp_res)
1504{
1505 acpigen_concatenate_string_op(str, res, tmp_res);
1506 acpigen_write_debug_op(tmp_res);
1507}
1508
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001509void acpigen_write_if(void)
1510{
1511 acpigen_emit_byte(IF_OP);
1512 acpigen_write_len_f();
1513}
1514
1515/* If (And (arg1, arg2)) */
1516void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1517{
1518 acpigen_write_if();
1519 acpigen_emit_byte(AND_OP);
1520 acpigen_emit_byte(arg1);
1521 acpigen_emit_byte(arg2);
1522}
1523
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001524/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001525 * Generates ACPI code for checking if operand1 and operand2 are equal.
1526 * Both operand1 and operand2 are ACPI ops.
1527 *
1528 * If (Lequal (op,1 op2))
1529 */
1530void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1531{
1532 acpigen_write_if();
1533 acpigen_emit_byte(LEQUAL_OP);
1534 acpigen_emit_byte(op1);
1535 acpigen_emit_byte(op2);
1536}
1537
1538/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001539 * Generates ACPI code for checking if operand1 is greater than operand2.
1540 * Both operand1 and operand2 are ACPI ops.
1541 *
1542 * If (Lgreater (op1 op2))
1543 */
1544void acpigen_write_if_lgreater_op_op(uint8_t op1, uint8_t op2)
1545{
1546 acpigen_write_if();
1547 acpigen_emit_byte(LGREATER_OP);
1548 acpigen_emit_byte(op1);
1549 acpigen_emit_byte(op2);
1550}
1551
1552/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001553 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1554 * operand1 is ACPI op and operand2 is an integer.
1555 *
1556 * If (Lequal (op, val))
1557 */
1558void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001559{
1560 acpigen_write_if();
1561 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001562 acpigen_emit_byte(op);
1563 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001564}
1565
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001566/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001567 * Generates ACPI code for checking if operand is greater than the value, where,
1568 * operand is ACPI op and val is an integer.
1569 *
1570 * If (Lgreater (op, val))
1571 */
1572void acpigen_write_if_lgreater_op_int(uint8_t op, uint64_t val)
1573{
1574 acpigen_write_if();
1575 acpigen_emit_byte(LGREATER_OP);
1576 acpigen_emit_byte(op);
1577 acpigen_write_integer(val);
1578}
1579
1580/*
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001581 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1582 * operand1 is namestring and operand2 is an integer.
1583 *
1584 * If (Lequal ("namestr", val))
1585 */
1586void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1587{
1588 acpigen_write_if();
1589 acpigen_emit_byte(LEQUAL_OP);
1590 acpigen_emit_namestring(namestr);
1591 acpigen_write_integer(val);
1592}
1593
Tim Wawrzynczakc4ca2f62021-07-01 11:18:50 -06001594/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001595 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1596 * operand1 is namestring and operand2 is an integer.
1597 *
1598 * If (Lgreater ("namestr", val))
1599 */
1600void acpigen_write_if_lgreater_namestr_int(const char *namestr, uint64_t val)
1601{
1602 acpigen_write_if();
1603 acpigen_emit_byte(LGREATER_OP);
1604 acpigen_emit_namestring(namestr);
1605 acpigen_write_integer(val);
1606}
1607
1608/*
Tim Wawrzynczakc4ca2f62021-07-01 11:18:50 -06001609 * Generates ACPI code to check at runtime if an object named `namestring`
1610 * exists, and leaves the If scope open to continue execute code when this
1611 * is true. NOTE: Requires matching acpigen_write_if_end().
1612 *
1613 * If (CondRefOf (NAME))
1614 */
1615void acpigen_write_if_cond_ref_of(const char *namestring)
1616{
1617 acpigen_write_if();
1618 acpigen_emit_ext_op(COND_REFOF_OP);
1619 acpigen_emit_namestring(namestring);
1620 acpigen_emit_byte(ZERO_OP); /* ignore COND_REFOF_OP destination */
1621}
1622
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001623/* Closes previously opened if statement and generates ACPI code for else statement. */
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001624void acpigen_write_else(void)
1625{
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001626 acpigen_pop_len();
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001627 acpigen_emit_byte(ELSE_OP);
1628 acpigen_write_len_f();
1629}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001630
Duncan Laurie36858202020-10-06 21:01:51 +00001631void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count)
1632{
1633 acpigen_emit_byte(SHIFT_LEFT_OP);
1634 acpigen_emit_byte(src_result);
1635 acpigen_write_integer(count);
1636 acpigen_emit_byte(ZERO_OP);
1637}
1638
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001639void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1640{
1641 acpigen_emit_byte(TO_BUFFER_OP);
1642 acpigen_emit_byte(src);
1643 acpigen_emit_byte(dst);
1644}
1645
1646void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1647{
1648 acpigen_emit_byte(TO_INTEGER_OP);
1649 acpigen_emit_byte(src);
1650 acpigen_emit_byte(dst);
1651}
1652
Aaron Durbin80bc0912020-08-19 23:17:42 -06001653void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op)
1654{
1655 acpigen_emit_byte(TO_INTEGER_OP);
1656 acpigen_emit_namestring(source);
1657 acpigen_emit_byte(dst_op);
1658}
1659
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301660void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001661{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301662 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001663
1664 acpigen_emit_byte(BUFFER_OP);
1665 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301666 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001667
1668 for (i = 0; i < size; i++)
1669 acpigen_emit_byte(arr[i]);
1670
1671 acpigen_pop_len();
1672}
1673
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301674void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001675{
1676 acpigen_emit_byte(RETURN_OP);
1677 acpigen_write_byte_buffer(arr, size);
1678}
1679
1680void acpigen_write_return_singleton_buffer(uint8_t arg)
1681{
1682 acpigen_write_return_byte_buffer(&arg, 1);
1683}
1684
Duncan Laurie2fe74712020-06-01 17:36:56 -07001685void acpigen_write_return_op(uint8_t arg)
1686{
1687 acpigen_emit_byte(RETURN_OP);
1688 acpigen_emit_byte(arg);
1689}
1690
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001691void acpigen_write_return_byte(uint8_t arg)
1692{
1693 acpigen_emit_byte(RETURN_OP);
1694 acpigen_write_byte(arg);
1695}
1696
Naresh G Solanki05abe432016-11-17 00:16:29 +05301697void acpigen_write_return_integer(uint64_t arg)
1698{
1699 acpigen_emit_byte(RETURN_OP);
1700 acpigen_write_integer(arg);
1701}
1702
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001703void acpigen_write_return_namestr(const char *arg)
1704{
1705 acpigen_emit_byte(RETURN_OP);
1706 acpigen_emit_namestring(arg);
1707}
1708
Naresh G Solanki05abe432016-11-17 00:16:29 +05301709void acpigen_write_return_string(const char *arg)
1710{
1711 acpigen_emit_byte(RETURN_OP);
1712 acpigen_write_string(arg);
1713}
1714
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001715void acpigen_write_upc(enum acpi_upc_type type)
1716{
1717 acpigen_write_name("_UPC");
1718 acpigen_write_package(4);
1719 /* Connectable */
1720 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1721 /* Type */
1722 acpigen_write_byte(type);
1723 /* Reserved0 */
1724 acpigen_write_zero();
1725 /* Reserved1 */
1726 acpigen_write_zero();
1727 acpigen_pop_len();
1728}
1729
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001730void acpigen_write_pld(const struct acpi_pld *pld)
1731{
1732 uint8_t buf[20];
1733
1734 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1735 return;
1736
1737 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001738 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001739 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001740 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001741}
1742
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001743void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *), size_t count, void *arg)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301744{
1745 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1746 acpigen_write_dsm_uuid_arr(&id, 1);
1747}
1748
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001749/*
1750 * Create a supported functions bitmask
1751 * bit 0: other functions than 0 are supported
1752 * bits 1-x: function x supported
1753 */
1754static void acpigen_dsm_uuid_enum_functions(const struct dsm_uuid *id)
1755{
1756 const size_t bytes = DIV_ROUND_UP(id->count, BITS_PER_BYTE);
1757 uint8_t *buffer = alloca(bytes);
1758 bool set = false;
1759 size_t cb_idx = 0;
1760
1761 memset(buffer, 0, bytes);
1762
1763 for (size_t i = 0; i < bytes; i++) {
1764 for (size_t j = 0; j < BITS_PER_BYTE; j++) {
1765 if (cb_idx >= id->count)
1766 break;
1767
1768 if (id->callbacks[cb_idx++]) {
1769 set = true;
1770 buffer[i] |= BIT(j);
1771 }
1772 }
1773 }
1774
1775 if (set)
1776 buffer[0] |= BIT(0);
1777
1778 acpigen_write_return_byte_buffer(buffer, bytes);
1779}
1780
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301781static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1782{
1783 size_t i;
1784
1785 /* If (LEqual (Local0, ToUUID(uuid))) */
1786 acpigen_write_if();
1787 acpigen_emit_byte(LEQUAL_OP);
1788 acpigen_emit_byte(LOCAL0_OP);
1789 acpigen_write_uuid(id->uuid);
1790
1791 /* ToInteger (Arg2, Local1) */
1792 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1793
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001794 /* If (LEqual(Local1, 0)) */
1795 {
1796 acpigen_write_if_lequal_op_int(LOCAL1_OP, 0);
1797 if (id->callbacks[0])
1798 id->callbacks[0](id->arg);
1799 else if (id->count)
1800 acpigen_dsm_uuid_enum_functions(id);
1801 acpigen_write_if_end();
1802 }
1803
1804 for (i = 1; i < id->count; i++) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301805 /* If (LEqual (Local1, i)) */
1806 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1807
1808 /* Callback to write if handler. */
1809 if (id->callbacks[i])
1810 id->callbacks[i](id->arg);
1811
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001812 acpigen_write_if_end(); /* If */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301813 }
1814
1815 /* Default case: Return (Buffer (One) { 0x0 }) */
1816 acpigen_write_return_singleton_buffer(0x0);
1817
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001818 acpigen_write_if_end(); /* If (LEqual (Local0, ToUUID(uuid))) */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301819
1820}
1821
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001822/*
1823 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301824 * This function takes as input array of uuid for the device, set of callbacks
1825 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1826 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1827 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001828 *
1829 * Arguments passed into _DSM method:
1830 * Arg0 = UUID
1831 * Arg1 = Revision
1832 * Arg2 = Function index
1833 * Arg3 = Function specific arguments
1834 *
1835 * AML code generated would look like:
1836 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301837 * ToBuffer (Arg0, Local0)
1838 * If (LEqual (Local0, ToUUID(uuid))) {
1839 * ToInteger (Arg2, Local1)
1840 * If (LEqual (Local1, 0)) {
1841 * <acpigen by callback[0]>
1842 * }
1843 * ...
1844 * If (LEqual (Local1, n)) {
1845 * <acpigen by callback[n]>
1846 * }
1847 * Return (Buffer (One) { 0x0 })
1848 * }
1849 * ...
1850 * If (LEqual (Local0, ToUUID(uuidn))) {
1851 * ...
1852 * }
1853 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001854 * }
1855 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301856void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001857{
1858 size_t i;
1859
1860 /* Method (_DSM, 4, Serialized) */
1861 acpigen_write_method_serialized("_DSM", 0x4);
1862
1863 /* ToBuffer (Arg0, Local0) */
1864 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1865
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001866 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301867 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001868
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301869 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001870 acpigen_write_return_singleton_buffer(0x0);
1871
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001872 acpigen_pop_len(); /* Method _DSM */
1873}
1874
Matt Delcob425bc82018-08-13 13:36:27 -07001875void acpigen_write_CPPC_package(const struct cppc_config *config)
1876{
1877 u32 i;
1878 u32 max;
1879 switch (config->version) {
1880 case 1:
1881 max = CPPC_MAX_FIELDS_VER_1;
1882 break;
1883 case 2:
1884 max = CPPC_MAX_FIELDS_VER_2;
1885 break;
1886 case 3:
1887 max = CPPC_MAX_FIELDS_VER_3;
1888 break;
1889 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001890 printk(BIOS_ERR, "CPPC version %u is not implemented\n", config->version);
Matt Delcob425bc82018-08-13 13:36:27 -07001891 return;
1892 }
1893 acpigen_write_name(CPPC_PACKAGE_NAME);
1894
1895 /* Adding 2 to account for length and version fields */
1896 acpigen_write_package(max + 2);
1897 acpigen_write_dword(max + 2);
1898
1899 acpigen_write_byte(config->version);
1900
1901 for (i = 0; i < max; ++i) {
Michael Niewöhner38107fa2021-10-05 22:22:21 +02001902 const cppc_entry_t *entry = &config->entries[i];
1903 if (entry->type == CPPC_TYPE_DWORD)
1904 acpigen_write_dword(entry->dword);
1905 else
1906 acpigen_write_register_resource(&entry->reg);
Matt Delcob425bc82018-08-13 13:36:27 -07001907 }
1908 acpigen_pop_len();
1909}
1910
1911void acpigen_write_CPPC_method(void)
1912{
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001913 char pscope[16];
1914 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
1915
Matt Delcob425bc82018-08-13 13:36:27 -07001916 acpigen_write_method("_CPC", 0);
1917 acpigen_emit_byte(RETURN_OP);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001918 acpigen_emit_namestring(pscope);
Matt Delcob425bc82018-08-13 13:36:27 -07001919 acpigen_pop_len();
1920}
1921
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001922/*
1923 * Generate ACPI AML code for _ROM method.
1924 * This function takes as input ROM data and ROM length.
1925 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001926 * The ACPI spec isn't clear about what should happen at the end of the
1927 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1928 * bytes in the returned buffer with zeros.
1929 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001930 * Arguments passed into _DSM method:
1931 * Arg0 = Offset in Bytes
1932 * Arg1 = Bytes to return
1933 *
1934 * Example:
1935 * acpigen_write_rom(0xdeadbeef, 0x10000)
1936 *
1937 * AML code generated would look like:
1938 * Method (_ROM, 2, NotSerialized) {
1939 *
1940 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1941 * Field (ROMS, AnyAcc, NoLock, Preserve)
1942 * {
1943 * Offset (0),
1944 * RBF0, 0x80000
1945 * }
1946 *
1947 * Store (Arg0, Local0)
1948 * Store (Arg1, Local1)
1949 *
1950 * If (LGreater (Local1, 0x1000))
1951 * {
1952 * Store (0x1000, Local1)
1953 * }
1954 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001955 * Store (Local1, Local3)
1956 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001957 * If (LGreater (Local0, 0x10000))
1958 * {
1959 * Return(Buffer(Local1){0})
1960 * }
1961 *
1962 * If (LGreater (Local0, 0x0f000))
1963 * {
1964 * Subtract (0x10000, Local0, Local2)
1965 * If (LGreater (Local1, Local2))
1966 * {
1967 * Store (Local2, Local1)
1968 * }
1969 * }
1970 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001971 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001972 *
1973 * Multiply (Local0, 0x08, Local0)
1974 * Multiply (Local1, 0x08, Local1)
1975 *
1976 * CreateField (RBF0, Local0, Local1, TMPB)
1977 * Store (TMPB, ROM1)
1978 * Return (ROM1)
1979 * }
1980 */
1981
1982void acpigen_write_rom(void *bios, const size_t length)
1983{
1984 ASSERT(bios)
1985 ASSERT(length)
1986
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001987 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001988 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001989
1990 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001991 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY, (uintptr_t)bios, length);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001992 acpigen_write_opregion(&opreg);
1993
1994 struct fieldlist l[] = {
1995 FIELDLIST_OFFSET(0),
1996 FIELDLIST_NAMESTR("RBF0", 8 * length),
1997 };
1998
1999 /* Field (ROMS, AnyAcc, NoLock, Preserve)
2000 * {
2001 * Offset (0),
2002 * RBF0, 0x80000
2003 * } */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002004 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002005
2006 /* Store (Arg0, Local0) */
2007 acpigen_write_store();
2008 acpigen_emit_byte(ARG0_OP);
2009 acpigen_emit_byte(LOCAL0_OP);
2010
2011 /* Store (Arg1, Local1) */
2012 acpigen_write_store();
2013 acpigen_emit_byte(ARG1_OP);
2014 acpigen_emit_byte(LOCAL1_OP);
2015
2016 /* ACPI SPEC requires to return at maximum 4KiB */
2017 /* If (LGreater (Local1, 0x1000)) */
2018 acpigen_write_if();
2019 acpigen_emit_byte(LGREATER_OP);
2020 acpigen_emit_byte(LOCAL1_OP);
2021 acpigen_write_integer(0x1000);
2022
2023 /* Store (0x1000, Local1) */
2024 acpigen_write_store();
2025 acpigen_write_integer(0x1000);
2026 acpigen_emit_byte(LOCAL1_OP);
2027
2028 /* Pop if */
2029 acpigen_pop_len();
2030
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002031 /* Store (Local1, Local3) */
2032 acpigen_write_store();
2033 acpigen_emit_byte(LOCAL1_OP);
2034 acpigen_emit_byte(LOCAL3_OP);
2035
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002036 /* If (LGreater (Local0, length)) */
2037 acpigen_write_if();
2038 acpigen_emit_byte(LGREATER_OP);
2039 acpigen_emit_byte(LOCAL0_OP);
2040 acpigen_write_integer(length);
2041
2042 /* Return(Buffer(Local1){0}) */
2043 acpigen_emit_byte(RETURN_OP);
2044 acpigen_emit_byte(BUFFER_OP);
2045 acpigen_write_len_f();
2046 acpigen_emit_byte(LOCAL1_OP);
2047 acpigen_emit_byte(0);
2048 acpigen_pop_len();
2049
2050 /* Pop if */
2051 acpigen_pop_len();
2052
2053 /* If (LGreater (Local0, length - 4096)) */
2054 acpigen_write_if();
2055 acpigen_emit_byte(LGREATER_OP);
2056 acpigen_emit_byte(LOCAL0_OP);
2057 acpigen_write_integer(length - 4096);
2058
2059 /* Subtract (length, Local0, Local2) */
2060 acpigen_emit_byte(SUBTRACT_OP);
2061 acpigen_write_integer(length);
2062 acpigen_emit_byte(LOCAL0_OP);
2063 acpigen_emit_byte(LOCAL2_OP);
2064
2065 /* If (LGreater (Local1, Local2)) */
2066 acpigen_write_if();
2067 acpigen_emit_byte(LGREATER_OP);
2068 acpigen_emit_byte(LOCAL1_OP);
2069 acpigen_emit_byte(LOCAL2_OP);
2070
2071 /* Store (Local2, Local1) */
2072 acpigen_write_store();
2073 acpigen_emit_byte(LOCAL2_OP);
2074 acpigen_emit_byte(LOCAL1_OP);
2075
2076 /* Pop if */
2077 acpigen_pop_len();
2078
2079 /* Pop if */
2080 acpigen_pop_len();
2081
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002082 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002083 acpigen_write_name("ROM1");
2084 acpigen_emit_byte(BUFFER_OP);
2085 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002086 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002087 acpigen_emit_byte(0);
2088 acpigen_pop_len();
2089
2090 /* Multiply (Local1, 0x08, Local1) */
2091 acpigen_emit_byte(MULTIPLY_OP);
2092 acpigen_emit_byte(LOCAL1_OP);
2093 acpigen_write_integer(0x08);
2094 acpigen_emit_byte(LOCAL1_OP);
2095
2096 /* Multiply (Local0, 0x08, Local0) */
2097 acpigen_emit_byte(MULTIPLY_OP);
2098 acpigen_emit_byte(LOCAL0_OP);
2099 acpigen_write_integer(0x08);
2100 acpigen_emit_byte(LOCAL0_OP);
2101
2102 /* CreateField (RBF0, Local0, Local1, TMPB) */
2103 acpigen_emit_ext_op(CREATEFIELD_OP);
2104 acpigen_emit_namestring("RBF0");
2105 acpigen_emit_byte(LOCAL0_OP);
2106 acpigen_emit_byte(LOCAL1_OP);
2107 acpigen_emit_namestring("TMPB");
2108
2109 /* Store (TMPB, ROM1) */
2110 acpigen_write_store();
2111 acpigen_emit_namestring("TMPB");
2112 acpigen_emit_namestring("ROM1");
2113
2114 /* Return (ROM1) */
2115 acpigen_emit_byte(RETURN_OP);
2116 acpigen_emit_namestring("ROM1");
2117
2118 /* Pop method */
2119 acpigen_pop_len();
2120}
2121
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002122/*
2123 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
2124 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
2125 * make callbacks into SoC acpigen code.
2126 *
2127 * Returns 0 on success and -1 on error.
2128 */
Duncan Laurie30c3f912020-10-09 04:48:53 +00002129int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002130{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002131 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002132 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002133 else
2134 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002135}
2136
Duncan Laurie30c3f912020-10-09 04:48:53 +00002137int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002138{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002139 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002140 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
2141 else
2142 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
2143}
Jonathan Zhang71299c22020-01-27 11:38:57 -08002144
Duncan Laurie30c3f912020-10-09 04:48:53 +00002145void acpigen_get_rx_gpio(const struct acpi_gpio *gpio)
Rajat Jain310623b2020-02-26 11:02:53 -08002146{
2147 acpigen_soc_read_rx_gpio(gpio->pins[0]);
2148
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002149 if (gpio->active_low)
Rajat Jain310623b2020-02-26 11:02:53 -08002150 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2151}
2152
Duncan Laurie30c3f912020-10-09 04:48:53 +00002153void acpigen_get_tx_gpio(const struct acpi_gpio *gpio)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002154{
2155 acpigen_soc_get_tx_gpio(gpio->pins[0]);
2156
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002157 if (gpio->active_low)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002158 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2159}
2160
Jonathan Zhang71299c22020-01-27 11:38:57 -08002161/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002162void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran, u16 range_min,
2163 u16 range_max, u16 translation, u16 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002164{
2165 acpigen_emit_byte(0x88);
2166 /* Byte 1+2: length (0x000d) */
2167 acpigen_emit_byte(0x0d);
2168 acpigen_emit_byte(0x00);
2169 /* resource type */
2170 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2171 /* general flags */
2172 acpigen_emit_byte(gen_flags);
2173 /* type flags */
2174 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2175 acpigen_emit_byte(type_flags);
2176 /* granularity, min, max, translation, length */
2177 acpigen_emit_word(gran);
2178 acpigen_emit_word(range_min);
2179 acpigen_emit_word(range_max);
2180 acpigen_emit_word(translation);
2181 acpigen_emit_word(length);
2182}
2183
2184/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002185void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags, u32 gran,
2186 u32 range_min, u32 range_max, u32 translation, u32 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002187{
2188 acpigen_emit_byte(0x87);
2189 /* Byte 1+2: length (0023) */
2190 acpigen_emit_byte(23);
2191 acpigen_emit_byte(0x00);
2192 /* resource type */
2193 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2194 /* general flags */
2195 acpigen_emit_byte(gen_flags);
2196 /* type flags */
2197 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2198 acpigen_emit_byte(type_flags);
2199 /* granularity, min, max, translation, length */
2200 acpigen_emit_dword(gran);
2201 acpigen_emit_dword(range_min);
2202 acpigen_emit_dword(range_max);
2203 acpigen_emit_dword(translation);
2204 acpigen_emit_dword(length);
2205}
2206
2207static void acpigen_emit_qword(u64 data)
2208{
2209 acpigen_emit_dword(data & 0xffffffff);
2210 acpigen_emit_dword((data >> 32) & 0xffffffff);
2211}
2212
2213/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002214void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags, u64 gran,
2215 u64 range_min, u64 range_max, u64 translation, u64 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002216{
2217 acpigen_emit_byte(0x8a);
2218 /* Byte 1+2: length (0x002b) */
2219 acpigen_emit_byte(0x2b);
2220 acpigen_emit_byte(0x00);
2221 /* resource type */
2222 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2223 /* general flags */
2224 acpigen_emit_byte(gen_flags);
2225 /* type flags */
2226 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2227 acpigen_emit_byte(type_flags);
2228 /* granularity, min, max, translation, length */
2229 acpigen_emit_qword(gran);
2230 acpigen_emit_qword(range_min);
2231 acpigen_emit_qword(range_max);
2232 acpigen_emit_qword(translation);
2233 acpigen_emit_qword(length);
2234}
Furquan Shaikh1a829232020-04-23 11:11:05 -07002235
2236void acpigen_write_ADR(uint64_t adr)
2237{
2238 acpigen_write_name_qword("_ADR", adr);
2239}
2240
Duncan Laurie08a942f2020-04-29 12:13:14 -07002241/**
2242 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2243 * @address: SoundWire device address properties.
2244 *
2245 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2246 *
2247 * 63..52 - Reserved (0)
2248 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2249 * Used when a Controller has multiple master devices, each producing a
2250 * separate SoundWire Link. Set to 0 for single-link controllers.
2251 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2252 * 47..44 - SoundWire specification version that this device supports
2253 * 43..40 - Unique ID for multiple devices
2254 * 39..24 - MIPI standard manufacturer code
2255 * 23..08 - Vendor defined part ID
2256 * 07..00 - MIPI class encoding
2257 */
2258void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
2259{
2260 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
2261 (((uint64_t)address->version & 0xf) << 44) |
2262 (((uint64_t)address->unique_id & 0xf) << 40) |
2263 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
2264 (((uint64_t)address->part_id & 0xffff) << 8) |
2265 (((uint64_t)address->class & 0xff)));
2266}
Tim Wawrzynczakf6945022020-06-04 15:18:47 -06002267
2268void acpigen_notify(const char *namestr, int value)
2269{
2270 acpigen_emit_byte(NOTIFY_OP);
2271 acpigen_emit_namestring(namestr);
2272 acpigen_write_integer(value);
2273}
Aaron Durbin80bc0912020-08-19 23:17:42 -06002274
2275static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name)
2276{
2277 acpigen_emit_byte(aml_op);
2278 acpigen_emit_byte(srcop);
2279 acpigen_write_integer(byte_offset);
2280 acpigen_emit_namestring(name);
2281}
2282
2283void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name)
2284{
2285 _create_field(CREATE_BYTE_OP, op, byte_offset, name);
2286}
2287
2288void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name)
2289{
2290 _create_field(CREATE_WORD_OP, op, byte_offset, name);
2291}
2292
2293void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name)
2294{
2295 _create_field(CREATE_DWORD_OP, op, byte_offset, name);
2296}
2297
2298void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name)
2299{
2300 _create_field(CREATE_QWORD_OP, op, byte_offset, name);
2301}
Jason Gleneskca36aed2020-09-15 21:01:57 -07002302
2303void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
2304{
2305 acpigen_write_name("_PCT");
2306 acpigen_write_package(0x02);
2307 acpigen_write_register_resource(perf_ctrl);
2308 acpigen_write_register_resource(perf_sts);
2309
2310 acpigen_pop_len();
2311}
2312
2313void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value)
2314{
2315 acpigen_write_package(0x08);
2316 acpigen_write_dword(pstate_value->core_freq);
2317 acpigen_write_dword(pstate_value->power);
2318 acpigen_write_dword(pstate_value->transition_latency);
2319 acpigen_write_dword(pstate_value->bus_master_latency);
2320
2321 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t));
2322 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t));
2323 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t));
2324 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t));
2325
2326 acpigen_pop_len();
2327}
2328
2329void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
2330{
2331 size_t pstate;
2332
2333 acpigen_write_name("XPSS");
2334 acpigen_write_package(nentries);
2335 for (pstate = 0; pstate < nentries; pstate++) {
2336 acpigen_write_xpss_package(pstate_values);
2337 pstate_values++;
2338 }
2339
2340 acpigen_pop_len();
2341}
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002342
2343/* Delay up to wait_ms until provided namestr matches expected value. */
2344void acpigen_write_delay_until_namestr_int(uint32_t wait_ms, const char *name, uint64_t value)
2345{
2346 uint32_t wait_ms_segment = 1;
2347 uint32_t segments = wait_ms;
2348
2349 /* Sleep in 16ms segments if delay is more than 32ms. */
2350 if (wait_ms > 32) {
2351 wait_ms_segment = 16;
2352 segments = wait_ms / 16;
2353 }
2354
2355 acpigen_write_store_int_to_op(segments, LOCAL7_OP);
2356 acpigen_emit_byte(WHILE_OP);
2357 acpigen_write_len_f();
2358 acpigen_emit_byte(LGREATER_OP);
2359 acpigen_emit_byte(LOCAL7_OP);
2360 acpigen_emit_byte(ZERO_OP);
2361
2362 /* If name is not provided then just delay in a loop. */
2363 if (name) {
2364 acpigen_write_if_lequal_namestr_int(name, value);
2365 acpigen_emit_byte(BREAK_OP);
2366 acpigen_pop_len(); /* If */
2367 }
2368
2369 acpigen_write_sleep(wait_ms_segment);
2370 acpigen_emit_byte(DECREMENT_OP);
2371 acpigen_emit_byte(LOCAL7_OP);
2372 acpigen_pop_len(); /* While */
2373}