blob: e5d76ba58e0df5a877e5b6c1156e19ba7a7f1f78 [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;
Felix Held6ac6f6a2023-11-10 19:57:03 +010032 /* Reserve 3 bytes for PkgLength. The actual byte values will be written later in the
33 acpigen_pop_len call. */
Rudolf Marek293b5f52009-02-01 18:35:15 +000034 acpigen_emit_byte(0);
35 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050036 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000037}
38
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010039void acpigen_pop_len(void)
40{
Felix Helda6f74592023-11-10 19:57:27 +010041 size_t len;
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010042 ASSERT(ltop > 0)
43 char *p = len_stack[--ltop];
44 len = gencurrent - p;
45 ASSERT(len <= ACPIGEN_MAXLEN)
Felix Helda6f74592023-11-10 19:57:27 +010046 const size_t payload_len = len - 3;
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010047
Felix Helda6f74592023-11-10 19:57:27 +010048 if (len <= 0x3f + 2) {
49 /* PkgLength of up to 0x3f can be encoded in one PkgLength byte instead of the
50 reserved 3 bytes. Since only 1 PkgLength byte will be written, the payload
51 data needs to be moved by 2 bytes */
52 memmove(&p[1], &p[3], payload_len);
53 /* Adjust the PkgLength to take into account that we only use 1 of the 3
54 reserved bytes */
55 len -= 2;
56 /* The two most significant bits of PkgLength get the value of 0 to indicate
57 there are no additional PkgLength bytes. In this case the single PkgLength
58 byte encodes the length in its lower 6 bits */
59 p[0] = len;
60 /* Adjust pointer for next ACPI bytecode byte */
61 acpigen_set_current(p + len);
62 } else if (len <= 0xfff + 1) {
63 /* PkgLength of up to 0xfff can be encoded in 2 PkgLength bytes instead of the
64 reserved 3 bytes. Since only 2 PkgLength bytes will be written, the payload
65 data needs to be moved by 1 byte */
66 memmove(&p[2], &p[3], payload_len);
67 /* Adjust the PkgLength to take into account that we only use 2 of the 3
68 reserved bytes */
69 len -= 1;
70 /* The two most significant bits of PkgLength get the value of 1 to indicate
71 there's a second PkgLength byte. The lower 4 bits of the first PkgLength
72 byte and the second PkgLength byte encode the length */
73 p[0] = (0x1 << 6 | (len & 0xf));
74 p[1] = (len >> 4 & 0xff);
75 /* Adjust pointer for next ACPI bytecode byte */
76 acpigen_set_current(p + len);
77 } else if (len <= 0xfffff) {
78 /* PkgLength of up to 0xfffff can be encoded in 3 PkgLength bytes. Since this
79 is the amount of reserved bytes, no need to move the payload in this case */
80 /* The two most significant bits of PkgLength get the value of 2 to indicate
81 there are two more PkgLength bytes following the first one. The lower 4 bits
82 of the first PkgLength byte and the two following PkgLength bytes encode the
83 length */
84 p[0] = (0x2 << 6 | (len & 0xf));
85 p[1] = (len >> 4 & 0xff);
86 p[2] = (len >> 12 & 0xff);
87 /* No need to adjust pointer for next ACPI bytecode byte */
88 } else {
89 /* The case of PkgLength up to 0xfffffff isn't supported at the moment */
90 printk(BIOS_ERR, "%s: package length exceeds maximum of 0xfffff.\n", __func__);
91 }
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010092}
93
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000094void acpigen_set_current(char *curr)
95{
96 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000097}
98
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000099char *acpigen_get_current(void)
100{
101 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000102}
103
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100104void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000105{
106 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000107}
108
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700109void acpigen_emit_ext_op(uint8_t op)
110{
111 acpigen_emit_byte(EXT_OP_PREFIX);
112 acpigen_emit_byte(op);
113}
114
Duncan Laurie9ccae752016-05-09 07:43:19 -0700115void acpigen_emit_word(unsigned int data)
116{
117 acpigen_emit_byte(data & 0xff);
118 acpigen_emit_byte((data >> 8) & 0xff);
119}
120
121void acpigen_emit_dword(unsigned int data)
122{
123 acpigen_emit_byte(data & 0xff);
124 acpigen_emit_byte((data >> 8) & 0xff);
125 acpigen_emit_byte((data >> 16) & 0xff);
126 acpigen_emit_byte((data >> 24) & 0xff);
127}
128
Duncan Laurie85d80272016-07-02 19:53:54 -0700129char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000130{
Duncan Laurie85d80272016-07-02 19:53:54 -0700131 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700132 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100133 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -0700134 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +0000135 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -0700136 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000137}
138
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100139void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000140{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700141 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000142 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000143}
144
Duncan Laurie9ccae752016-05-09 07:43:19 -0700145void acpigen_write_word(unsigned int data)
146{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700147 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700148 acpigen_emit_word(data);
149}
150
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100151void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000152{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700153 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700154 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000155}
156
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100157void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000158{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700159 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700160 acpigen_emit_dword(data & 0xffffffff);
161 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000162}
163
Duncan Laurief7c38762016-05-09 08:20:38 -0700164void acpigen_write_zero(void)
165{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700166 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700167}
168
169void acpigen_write_one(void)
170{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700171 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700172}
173
174void acpigen_write_ones(void)
175{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700176 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700177}
178
179void acpigen_write_integer(uint64_t data)
180{
181 if (data == 0)
182 acpigen_write_zero();
183 else if (data == 1)
184 acpigen_write_one();
185 else if (data <= 0xff)
186 acpigen_write_byte((unsigned char)data);
187 else if (data <= 0xffff)
188 acpigen_write_word((unsigned int)data);
189 else if (data <= 0xffffffff)
190 acpigen_write_dword((unsigned int)data);
191 else
192 acpigen_write_qword(data);
193}
194
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100195void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000196{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100197 acpigen_write_name(name);
198 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000199}
200
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100201void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000202{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100203 acpigen_write_name(name);
204 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000205}
206
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100207void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000208{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100209 acpigen_write_name(name);
210 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000211}
212
Duncan Laurief7c38762016-05-09 08:20:38 -0700213void acpigen_write_name_integer(const char *name, uint64_t val)
214{
215 acpigen_write_name(name);
216 acpigen_write_integer(val);
217}
218
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700219void acpigen_write_name_string(const char *name, const char *string)
220{
221 acpigen_write_name(name);
222 acpigen_write_string(string);
223}
224
Patrick Rudolph389c8272019-12-17 13:54:41 +0100225void acpigen_write_name_unicode(const char *name, const char *string)
226{
227 const size_t len = strlen(string) + 1;
228 acpigen_write_name(name);
229 acpigen_emit_byte(BUFFER_OP);
230 acpigen_write_len_f();
Matt DeVillierd4d40c62023-11-09 15:04:58 -0600231 acpigen_write_integer(2 * len);
Patrick Rudolph389c8272019-12-17 13:54:41 +0100232 for (size_t i = 0; i < len; i++) {
Arthur Heymans62ea7a82023-06-22 20:39:31 +0200233 const signed char c = string[i];
Patrick Rudolph389c8272019-12-17 13:54:41 +0100234 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
235 acpigen_emit_word(c >= 0 ? c : '?');
236 }
237 acpigen_pop_len();
238}
239
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100240void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000241{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000242 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700243 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000244 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000245}
246
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700247void acpigen_emit_string(const char *string)
248{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200249 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700250 acpigen_emit_byte('\0'); /* NUL */
251}
252
253void acpigen_write_string(const char *string)
254{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700255 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700256 acpigen_emit_string(string);
257}
258
Duncan Laurie37319032016-05-26 12:47:05 -0700259void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
260{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800261 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700262
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700263 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700264 acpigen_write_name_string("_HID", hid);
265}
266
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100267/*
268 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600269 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
270 * and "By convention, when an ASL compiler pads a name shorter than 4
271 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100272 *
273 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
274 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000275
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700276static void acpigen_emit_simple_namestring(const char *name)
277{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100278 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000279 char ud[] = "____";
280 for (i = 0; i < 4; i++) {
281 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100282 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000283 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000284 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700285 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000286 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000287}
288
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700289static void acpigen_emit_double_namestring(const char *name, int dotpos)
290{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700291 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100292 acpigen_emit_simple_namestring(name);
293 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000294}
295
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700296static void acpigen_emit_multi_namestring(const char *name)
297{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100298 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000299 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700300 acpigen_emit_byte(MULTI_NAME_PREFIX);
301 acpigen_emit_byte(ZERO_OP);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100302 pathlen = ((unsigned char *)acpigen_get_current()) - 1;
Rudolf Marek3310d752009-06-21 20:26:13 +0000303
304 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100305 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000306 /* find end or next entity */
307 while ((name[0] != '.') && (name[0] != '\0'))
308 name++;
309 /* forward to next */
310 if (name[0] == '.')
311 name++;
312 count++;
313 }
314
315 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000316}
317
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700318void acpigen_emit_namestring(const char *namepath)
319{
Rudolf Marek3310d752009-06-21 20:26:13 +0000320 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000321 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000322
Ronak Kanabar7c0f0072020-11-30 15:40:51 +0530323 /* Check for NULL pointer */
324 if (!namepath)
325 return;
326
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600327 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000328 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100329 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000330 namepath++;
331 }
332
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600333 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000334 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100335 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000336 namepath++;
337 }
338
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200339 /* If we have only \\ or only ^...^. Then we need to put a null
340 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200341 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700342 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100343 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200344 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000345
346 i = 0;
347 while (namepath[i] != '\0') {
348 if (namepath[i] == '.') {
349 dotcount++;
350 dotpos = i;
351 }
352 i++;
353 }
354
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700355 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100356 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700357 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100358 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700359 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100360 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000361}
362
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100363void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000364{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700365 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100366 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000367}
368
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100369void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000370{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700371 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100372 acpigen_write_len_f();
373 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000374}
Rudolf Marekf997b552009-02-14 15:40:23 +0000375
Duncan Laurie2fe74712020-06-01 17:36:56 -0700376void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
377{
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800378 /* <dest_op> = DeRefOf (<package_op>[<element>]) */
Duncan Laurie2fe74712020-06-01 17:36:56 -0700379 acpigen_write_store();
380 acpigen_emit_byte(DEREF_OP);
381 acpigen_emit_byte(INDEX_OP);
382 acpigen_emit_byte(package_op);
383 acpigen_write_integer(element);
384 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
385 acpigen_emit_byte(dest_op);
386}
387
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800388void acpigen_set_package_op_element_int(uint8_t package_op, unsigned int element, uint64_t src)
389{
390 /* DeRefOf (<package>[<element>]) = <src> */
391 acpigen_write_store();
392 acpigen_write_integer(src);
393 acpigen_emit_byte(DEREF_OP);
394 acpigen_emit_byte(INDEX_OP);
395 acpigen_emit_byte(package_op);
396 acpigen_write_integer(element);
397 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
398}
399
400void acpigen_get_package_element(const char *package, unsigned int element, uint8_t dest_op)
401{
402 /* <dest_op> = <package>[<element>] */
403 acpigen_write_store();
404 acpigen_emit_byte(INDEX_OP);
405 acpigen_emit_namestring(package);
406 acpigen_write_integer(element);
407 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
408 acpigen_emit_byte(dest_op);
409}
410
411void acpigen_set_package_element_int(const char *package, unsigned int element, uint64_t src)
412{
413 /* <package>[<element>] = <src> */
414 acpigen_write_store();
415 acpigen_write_integer(src);
416 acpigen_emit_byte(INDEX_OP);
417 acpigen_emit_namestring(package);
418 acpigen_write_integer(element);
419 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
420}
421
422void acpigen_set_package_element_namestr(const char *package, unsigned int element,
423 const char *src)
424{
425 /* <package>[<element>] = <src> */
426 acpigen_write_store();
427 acpigen_emit_namestring(src);
428 acpigen_emit_byte(INDEX_OP);
429 acpigen_emit_namestring(package);
430 acpigen_write_integer(element);
431 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
432}
433
Felix Heldb57b12f2023-01-24 19:31:00 +0100434void acpigen_write_processor_namestring(unsigned int cpu_index)
435{
436 char buffer[16];
Felix Heldf0a8b042023-05-12 15:55:06 +0200437 snprintf(buffer, sizeof(buffer), "\\_SB." CONFIG_ACPI_CPU_STRING, cpu_index);
Felix Heldb57b12f2023-01-24 19:31:00 +0100438 acpigen_emit_namestring(buffer);
439}
440
Elyes Haouas9c824912023-01-28 09:13:17 +0100441/* Processor() operator is deprecated as of ACPI 6.0, use Device() instead. */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100442void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000443{
444/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100445 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200446 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000447*/
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700448 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100449 acpigen_write_len_f();
Felix Heldb57b12f2023-01-24 19:31:00 +0100450 acpigen_write_processor_namestring(cpuindex);
Rudolf Marekf997b552009-02-14 15:40:23 +0000451 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700452 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000453 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000454}
455
Felix Held32bba182023-01-28 03:37:21 +0100456void acpigen_write_processor_device(unsigned int cpu_index)
457{
458 acpigen_emit_ext_op(DEVICE_OP);
459 acpigen_write_len_f();
460 acpigen_write_processor_namestring(cpu_index);
461 acpigen_write_name_string("_HID", "ACPI0007");
462 acpigen_write_name_integer("_UID", cpu_index);
463}
464
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100465void acpigen_write_processor_package(const char *const name, const unsigned int first_core,
Nico Huber4d211ac2017-09-01 21:14:36 +0200466 const unsigned int core_count)
467{
468 unsigned int i;
Nico Huber4d211ac2017-09-01 21:14:36 +0200469
470 acpigen_write_name(name);
471 acpigen_write_package(core_count);
Felix Heldb57b12f2023-01-24 19:31:00 +0100472
473 for (i = first_core; i < first_core + core_count; ++i)
474 acpigen_write_processor_namestring(i);
475
Nico Huber4d211ac2017-09-01 21:14:36 +0200476 acpigen_pop_len();
477}
478
Arthur Heymans8f055272018-11-28 13:18:57 +0100479/* Method to notify all CPU cores */
480void acpigen_write_processor_cnot(const unsigned int number_of_cores)
481{
482 int core_id;
483
Christian Walterbe3979c2019-12-18 15:07:59 +0100484 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100485 for (core_id = 0; core_id < number_of_cores; core_id++) {
Arthur Heymans8f055272018-11-28 13:18:57 +0100486 acpigen_emit_byte(NOTIFY_OP);
Felix Heldb57b12f2023-01-24 19:31:00 +0100487 acpigen_write_processor_namestring(core_id);
Arthur Heymans8f055272018-11-28 13:18:57 +0100488 acpigen_emit_byte(ARG0_OP);
489 }
490 acpigen_pop_len();
491}
492
Naresh G Solanki8535c662016-10-25 00:51:24 +0530493/*
494 * Generate ACPI AML code for OperationRegion
495 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
496 * where rname is region name, space is region space, offset is region offset &
497 * len is region length.
498 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
499 */
Duncan Laurie35029602020-10-09 17:50:25 +0000500void acpigen_write_opregion(const struct opregion *opreg)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530501{
502 /* OpregionOp */
503 acpigen_emit_ext_op(OPREGION_OP);
504 /* NameString 4 chars only */
505 acpigen_emit_simple_namestring(opreg->name);
506 /* RegionSpace */
507 acpigen_emit_byte(opreg->regionspace);
508 /* RegionOffset & RegionLen, it can be byte word or double word */
509 acpigen_write_integer(opreg->regionoffset);
510 acpigen_write_integer(opreg->regionlen);
511}
512
Patrick Rudolph32bae492019-08-08 12:47:30 +0200513/*
514 * Generate ACPI AML code for Mutex
515 * Arg0: Pointer to name of mutex
516 * Arg1: Initial value of mutex
517 */
518void acpigen_write_mutex(const char *name, const uint8_t flags)
519{
520 /* MutexOp */
521 acpigen_emit_ext_op(MUTEX_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000522 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200523 acpigen_emit_byte(flags);
524}
525
526void acpigen_write_acquire(const char *name, const uint16_t val)
527{
528 /* AcquireOp */
529 acpigen_emit_ext_op(ACQUIRE_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000530 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200531 acpigen_emit_word(val);
532}
533
534void acpigen_write_release(const char *name)
535{
536 /* ReleaseOp */
537 acpigen_emit_ext_op(RELEASE_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000538 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200539}
540
Patrick Rudolph894977b2017-07-01 16:15:05 +0200541static void acpigen_write_field_length(uint32_t len)
542{
543 uint8_t i, j;
544 uint8_t emit[4];
545
546 i = 1;
547 if (len < 0x40) {
548 emit[0] = len & 0x3F;
549 } else {
550 emit[0] = len & 0xF;
551 len >>= 4;
552 while (len) {
553 emit[i] = len & 0xFF;
554 i++;
555 len >>= 8;
556 }
557 }
558 /* Update bit 7:6 : Number of bytes followed by emit[0] */
559 emit[0] |= (i - 1) << 6;
560
561 for (j = 0; j < i; j++)
562 acpigen_emit_byte(emit[j]);
563}
564
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100565static void acpigen_write_field_offset(uint32_t offset, uint32_t current_bit_pos)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530566{
567 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530568
569 if (offset < current_bit_pos) {
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100570 printk(BIOS_WARNING, "%s: Cannot move offset backward", __func__);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530571 return;
572 }
573
574 diff_bits = offset - current_bit_pos;
575 /* Upper limit */
576 if (diff_bits > 0xFFFFFFF) {
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100577 printk(BIOS_WARNING, "%s: Offset very large to encode", __func__);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530578 return;
579 }
580
Naresh G Solanki8535c662016-10-25 00:51:24 +0530581 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200582 acpigen_write_field_length(diff_bits);
583}
584
Michael Niewöhner060dc7b2022-05-13 00:04:19 +0200585void acpigen_write_field_name(const char *name, uint32_t size)
Patrick Rudolph894977b2017-07-01 16:15:05 +0200586{
587 acpigen_emit_simple_namestring(name);
588 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530589}
590
Duncan Laurie095bbf92020-09-30 23:09:29 +0000591static void acpigen_write_field_reserved(uint32_t size)
592{
593 acpigen_emit_byte(0);
594 acpigen_write_field_length(size);
595}
596
Naresh G Solanki8535c662016-10-25 00:51:24 +0530597/*
598 * Generate ACPI AML code for Field
599 * Arg0: region name
600 * Arg1: Pointer to struct fieldlist.
601 * Arg2: no. of entries in Arg1
602 * Arg3: flags which indicate filed access type, lock rule & update rule.
603 * Example with fieldlist
604 * struct fieldlist l[] = {
605 * FIELDLIST_OFFSET(0x84),
606 * FIELDLIST_NAMESTR("PMCS", 2),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000607 * FIELDLIST_RESERVED(6),
Naresh G Solanki8535c662016-10-25 00:51:24 +0530608 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200609 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530610 * FIELD_PRESERVE);
611 * Output:
612 * Field (UART, AnyAcc, NoLock, Preserve)
613 * {
614 * Offset (0x84),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000615 * PMCS, 2,
616 * , 6,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530617 * }
618 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700619void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530620 uint8_t flags)
621{
622 uint16_t i;
623 uint32_t current_bit_pos = 0;
624
625 /* FieldOp */
626 acpigen_emit_ext_op(FIELD_OP);
627 /* Package Length */
628 acpigen_write_len_f();
629 /* NameString 4 chars only */
630 acpigen_emit_simple_namestring(name);
631 /* Field Flag */
632 acpigen_emit_byte(flags);
633
634 for (i = 0; i < count; i++) {
635 switch (l[i].type) {
636 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200637 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530638 current_bit_pos += l[i].bits;
639 break;
Duncan Laurie095bbf92020-09-30 23:09:29 +0000640 case RESERVED:
641 acpigen_write_field_reserved(l[i].bits);
642 current_bit_pos += l[i].bits;
643 break;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530644 case OFFSET:
645 acpigen_write_field_offset(l[i].bits, current_bit_pos);
646 current_bit_pos = l[i].bits;
647 break;
648 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100649 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530650 break;
651 }
652 }
653 acpigen_pop_len();
654}
655
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200656/*
657 * Generate ACPI AML code for IndexField
658 * Arg0: region name
659 * Arg1: Pointer to struct fieldlist.
660 * Arg2: no. of entries in Arg1
661 * Arg3: flags which indicate filed access type, lock rule & update rule.
662 * Example with fieldlist
663 * struct fieldlist l[] = {
664 * FIELDLIST_OFFSET(0x84),
665 * FIELDLIST_NAMESTR("PMCS", 2),
666 * };
667 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
668 * FIELD_NOLOCK |
669 * FIELD_PRESERVE);
670 * Output:
671 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
672 * {
673 * Offset (0x84),
674 * PMCS, 2
675 * }
676 */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100677void acpigen_write_indexfield(const char *idx, const char *data, struct fieldlist *l,
678 size_t count, uint8_t flags)
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200679{
680 uint16_t i;
681 uint32_t current_bit_pos = 0;
682
683 /* FieldOp */
684 acpigen_emit_ext_op(INDEX_FIELD_OP);
685 /* Package Length */
686 acpigen_write_len_f();
687 /* NameString 4 chars only */
688 acpigen_emit_simple_namestring(idx);
689 /* NameString 4 chars only */
690 acpigen_emit_simple_namestring(data);
691 /* Field Flag */
692 acpigen_emit_byte(flags);
693
694 for (i = 0; i < count; i++) {
695 switch (l[i].type) {
696 case NAME_STRING:
697 acpigen_write_field_name(l[i].name, l[i].bits);
698 current_bit_pos += l[i].bits;
699 break;
700 case OFFSET:
701 acpigen_write_field_offset(l[i].bits, current_bit_pos);
702 current_bit_pos = l[i].bits;
703 break;
704 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100705 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type);
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200706 break;
707 }
708 }
709 acpigen_pop_len();
710}
711
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100712void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000713{
714/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200715 Name (_PCT, Package (0x02)
716 {
717 ResourceTemplate ()
718 {
719 Register (FFixedHW,
720 0x00, // Bit Width
721 0x00, // Bit Offset
722 0x0000000000000000, // Address
723 ,)
724 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000725
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200726 ResourceTemplate ()
727 {
728 Register (FFixedHW,
729 0x00, // Bit Width
730 0x00, // Bit Offset
731 0x0000000000000000, // Address
732 ,)
733 }
734 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000735*/
736 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700737 /* 00000030 "0._PCT.," */
738 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
739 /* 00000038 "........" */
740 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
741 /* 00000040 "........" */
742 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
743 /* 00000048 "....y..." */
744 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
745 /* 00000050 "........" */
746 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
747 /* 00000058 "........" */
748 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000749 0x00, 0x79, 0x00
750 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100751 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000752}
753
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300754void acpigen_write_PTC(uint8_t duty_width, uint8_t duty_offset, uint16_t p_cnt)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200755{
756/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200757 Name (_PTC, Package (0x02)
758 {
759 ResourceTemplate ()
760 {
761 Register (FFixedHW,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300762 0x00, // Duty Width
763 0x00, // Duty Offset
764 0x0000000000000000, // P_CNT IO Address
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200765 ,)
766 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200767
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200768 ResourceTemplate ()
769 {
770 Register (FFixedHW,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300771 0x00, // Duty Width
772 0x00, // Duty Offset
773 0x0000000000000000, // P_CNT IO Address
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200774 ,)
775 }
776 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200777*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200778 acpi_addr_t addr = {
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300779 .bit_width = duty_width,
780 .bit_offset = duty_offset,
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +0200781 .access_size = ACPI_ACCESS_SIZE_UNDEFINED,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300782 .addrl = p_cnt,
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100783 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200784 };
785
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300786 if (addr.addrl != 0)
787 addr.space_id = ACPI_ADDRESS_SPACE_IO;
788 else
789 addr.space_id = ACPI_ADDRESS_SPACE_FIXED;
790
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100791 acpigen_write_name("_PTC");
792 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200793
794 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700795 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200796
797 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700798 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200799
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100800 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200801}
802
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300803void acpigen_write_empty_PTC(void)
804{
805 acpigen_write_PTC(0, 0, 0);
806}
807
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700808static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100809{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700810 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100811 acpigen_write_len_f();
812 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700813 acpigen_emit_byte(flags);
814}
815
816/* Method (name, nargs, NotSerialized) */
817void acpigen_write_method(const char *name, int nargs)
818{
819 __acpigen_write_method(name, (nargs & 7));
820}
821
822/* Method (name, nargs, Serialized) */
823void acpigen_write_method_serialized(const char *name, int nargs)
824{
825 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100826}
827
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100828void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100829{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700830 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100831 acpigen_write_len_f();
832 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100833}
834
Raul E Rangel052c9632021-05-12 17:01:30 -0600835void acpigen_write_thermal_zone(const char *name)
836{
837 acpigen_emit_ext_op(THERMAL_ZONE_OP);
838 acpigen_write_len_f();
839 acpigen_emit_namestring(name);
840}
841
Duncan Laurieabe2de82016-05-09 11:08:46 -0700842void acpigen_write_STA(uint8_t status)
843{
844 /*
845 * Method (_STA, 0, NotSerialized) { Return (status) }
846 */
847 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700848 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700849 acpigen_write_byte(status);
850 acpigen_pop_len();
851}
852
Sugnan Prabhu S4f77f612020-06-10 09:51:02 +0530853void acpigen_write_STA_ext(const char *namestring)
854{
855 /*
856 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
857 */
858 acpigen_write_method("_STA", 0);
859 acpigen_emit_byte(RETURN_OP);
860 acpigen_emit_namestring(namestring);
861 acpigen_pop_len();
862}
863
Felix Held2d4112f2023-05-04 23:00:06 +0200864void acpigen_write_BBN(uint8_t base_bus_number)
865{
866 /*
867 * Method (_BBN, 0, NotSerialized) { Return (status) }
868 */
869 acpigen_write_method("_BBN", 0);
870 acpigen_emit_byte(RETURN_OP);
871 acpigen_write_byte(base_bus_number);
872 acpigen_pop_len();
873}
874
Raul E Rangelc7048322021-04-19 15:58:25 -0600875void acpigen_write_LPI_package(u64 level, const struct acpi_lpi_state *states, u16 nentries)
876{
877 /*
878 * Name (_LPI, Package (0x06) // _LPI: Low Power Idle States
879 * {
880 * 0x0000,
881 * 0x0000000000000000,
882 * 0x0003,
883 * Package (0x0A)
884 * {
885 * 0x00000002,
886 * 0x00000001,
887 * 0x00000001,
888 * 0x00000000,
889 * 0x00000000,
890 * 0x00000000,
891 * ResourceTemplate ()
892 * {
893 * Register (FFixedHW,
894 * 0x02, // Bit Width
895 * 0x02, // Bit Offset
896 * 0x0000000000000000, // Address
897 * ,)
898 * },
899 *
900 * ResourceTemplate ()
901 * {
902 * Register (SystemMemory,
903 * 0x00, // Bit Width
904 * 0x00, // Bit Offset
905 * 0x0000000000000000, // Address
906 * ,)
907 * },
908 *
909 * ResourceTemplate ()
910 * {
911 * Register (SystemMemory,
912 * 0x00, // Bit Width
913 * 0x00, // Bit Offset
914 * 0x0000000000000000, // Address
915 * ,)
916 * },
917 *
918 * "C1"
919 * },
920 * ...
921 * }
922 */
923
924 acpigen_write_name("_LPI");
925 acpigen_write_package(3 + nentries);
926 acpigen_write_word(0); /* Revision */
927 acpigen_write_qword(level);
928 acpigen_write_word(nentries);
929
930 for (size_t i = 0; i < nentries; i++, states++) {
931 acpigen_write_package(0xA);
932 acpigen_write_dword(states->min_residency_us);
933 acpigen_write_dword(states->worst_case_wakeup_latency_us);
934 acpigen_write_dword(states->flags);
935 acpigen_write_dword(states->arch_context_lost_flags);
936 acpigen_write_dword(states->residency_counter_frequency_hz);
937 acpigen_write_dword(states->enabled_parent_state);
938 acpigen_write_register_resource(&states->entry_method);
939 acpigen_write_register_resource(&states->residency_counter_register);
940 acpigen_write_register_resource(&states->usage_counter_register);
941 acpigen_write_string(states->state_name);
942 acpigen_pop_len();
943 }
944 acpigen_pop_len();
945}
946
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100947/*
948 * Generates a func with max supported P-states.
949 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100950void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000951{
952/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200953 Method (_PPC, 0, NotSerialized)
954 {
955 Return (nr)
956 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000957*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100958 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700959 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000960 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100961 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100962 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000963}
964
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100965/*
966 * Generates a func with max supported P-states saved
967 * in the variable PPCM.
968 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100969void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700970{
971/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200972 Method (_PPC, 0, NotSerialized)
973 {
974 Return (PPCM)
975 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700976*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100977 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700978 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700979 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100980 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100981 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700982}
983
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100984void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200985{
986/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200987 // Sample _TPC method
988 Method (_TPC, 0, NotSerialized)
989 {
990 Return (\TLVL)
991 }
992*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100993 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700994 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100995 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100996 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200997}
998
Duncan Laurieabe2de82016-05-09 11:08:46 -0700999void acpigen_write_PRW(u32 wake, u32 level)
1000{
1001 /*
1002 * Name (_PRW, Package () { wake, level }
1003 */
1004 acpigen_write_name("_PRW");
1005 acpigen_write_package(2);
1006 acpigen_write_integer(wake);
1007 acpigen_write_integer(level);
1008 acpigen_pop_len();
1009}
1010
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001011void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat, u32 busmLat, u32 control,
1012 u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +00001013{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001014 acpigen_write_package(6);
1015 acpigen_write_dword(coreFreq);
1016 acpigen_write_dword(power);
1017 acpigen_write_dword(transLat);
1018 acpigen_write_dword(busmLat);
1019 acpigen_write_dword(control);
1020 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001021 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -07001022
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001023 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n", coreFreq, power,
1024 control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +00001025}
Patrick Georgidf444bf2009-04-21 20:34:36 +00001026
Jason Gleneskca36aed2020-09-15 21:01:57 -07001027void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
1028{
1029 size_t pstate;
1030
1031 acpigen_write_name("_PSS");
1032 acpigen_write_package(nentries);
1033 for (pstate = 0; pstate < nentries; pstate++) {
1034 acpigen_write_PSS_package(
1035 pstate_values->core_freq, pstate_values->power,
1036 pstate_values->transition_latency, pstate_values->bus_master_latency,
1037 pstate_values->control_value, pstate_values->status_value);
1038 pstate_values++;
1039 }
1040
1041 acpigen_pop_len();
1042}
1043
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001044void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +00001045{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001046 acpigen_write_name("_PSD");
1047 acpigen_write_package(1);
1048 acpigen_write_package(5);
1049 acpigen_write_byte(5); // 5 values
1050 acpigen_write_byte(0); // revision 0
1051 acpigen_write_dword(domain);
1052 acpigen_write_dword(coordtype);
1053 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001054 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001055 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +00001056}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001057
Angel Ponsd2794ce2021-10-17 12:59:43 +02001058void acpigen_write_CST_package_entry(const acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001059{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001060 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001061 acpigen_write_register_resource(&cstate->resource);
Jason Glenesk201acca2020-09-11 12:36:15 -07001062 acpigen_write_byte(cstate->ctype);
1063 acpigen_write_word(cstate->latency);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001064 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001065 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001066}
1067
Angel Ponsd2794ce2021-10-17 12:59:43 +02001068void acpigen_write_CST_package(const acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001069{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001070 int i;
1071 acpigen_write_name("_CST");
1072 acpigen_write_package(nentries+1);
Jason Glenesk201acca2020-09-11 12:36:15 -07001073 acpigen_write_integer(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001074
1075 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001076 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001077
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001078 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001079}
1080
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001081void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
1082 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -05001083{
1084 acpigen_write_name("_CSD");
1085 acpigen_write_package(1);
1086 acpigen_write_package(6);
Jason Glenesk201acca2020-09-11 12:36:15 -07001087 acpigen_write_integer(6); // 6 values
Timothy Pearson83abd812015-06-08 19:35:06 -05001088 acpigen_write_byte(0); // revision 0
1089 acpigen_write_dword(domain);
1090 acpigen_write_dword(coordtype);
1091 acpigen_write_dword(numprocs);
1092 acpigen_write_dword(index);
1093 acpigen_pop_len();
1094 acpigen_pop_len();
1095}
1096
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001097void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001098{
1099/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +02001100 Sample _TSS package with 100% and 50% duty cycles
1101 Name (_TSS, Package (0x02)
1102 {
1103 Package(){100, 1000, 0, 0x00, 0)
1104 Package(){50, 520, 0, 0x18, 0)
1105 })
1106*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001107 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001108 acpi_tstate_t *tstate = tstate_list;
1109
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001110 acpigen_write_name("_TSS");
1111 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +02001112
1113 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001114 acpigen_write_package(5);
1115 acpigen_write_dword(tstate->percent);
1116 acpigen_write_dword(tstate->power);
1117 acpigen_write_dword(tstate->latency);
1118 acpigen_write_dword(tstate->control);
1119 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001120 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001121 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001122 }
1123
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001124 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001125}
1126
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001127void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001128{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001129 acpigen_write_name("_TSD");
1130 acpigen_write_package(1);
1131 acpigen_write_package(5);
1132 acpigen_write_byte(5); // 5 values
1133 acpigen_write_byte(0); // revision 0
1134 acpigen_write_dword(domain);
1135 acpigen_write_dword(coordtype);
1136 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001137 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001138 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001139}
1140
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001141void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001142{
1143 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001144 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001145 * Byte 0:
1146 * Bit7 : 1 => big item
1147 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
1148 */
1149 acpigen_emit_byte(0x86);
1150 /* Byte 1+2: length (0x0009) */
1151 acpigen_emit_byte(0x09);
1152 acpigen_emit_byte(0x00);
1153 /* bit1-7 are ignored */
1154 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -07001155 acpigen_emit_dword(base);
1156 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001157}
1158
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001159static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001160{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +02001161 acpigen_emit_byte(0x82); /* Register Descriptor */
1162 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
1163 acpigen_emit_byte(0x00); /* Register Length 15:8 */
1164 acpigen_emit_byte(addr->space_id); /* Address Space ID */
1165 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
1166 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +01001167 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -07001168 acpigen_emit_dword(addr->addrl); /* Register Address Low */
1169 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +02001170}
1171
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001172void acpigen_write_register_resource(const acpi_addr_t *addr)
1173{
1174 acpigen_write_resourcetemplate_header();
1175 acpigen_write_register(addr);
1176 acpigen_write_resourcetemplate_footer();
1177}
1178
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001179void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001180{
1181 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001182 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001183 * Byte 0:
1184 * Bit7 : 0 => small item
1185 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1186 * Bit2-0: 010 (0x2) => 2 Bytes long
1187 */
1188 acpigen_emit_byte(0x22);
1189 acpigen_emit_byte(mask & 0xff);
1190 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001191}
1192
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001193void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001194{
1195 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001196 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001197 * Byte 0:
1198 * Bit7 : 0 => small item
1199 * Bit6-3: 1000 (0x8) => I/O port descriptor
1200 * Bit2-0: 111 (0x7) => 7 Bytes long
1201 */
1202 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001203 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001204 /* bit1-7 are ignored */
1205 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1206 /* minimum base address the device may be configured for */
1207 acpigen_emit_byte(min & 0xff);
1208 acpigen_emit_byte((min >> 8) & 0xff);
1209 /* maximum base address the device may be configured for */
1210 acpigen_emit_byte(max & 0xff);
1211 acpigen_emit_byte((max >> 8) & 0xff);
1212 /* alignment for min base */
1213 acpigen_emit_byte(align & 0xff);
1214 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001215}
1216
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001217void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001218{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001219 /*
1220 * A ResourceTemplate() is a Buffer() with a
1221 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001222 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001223 * (small item 0xf, len 1)
1224 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001225 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001226 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001227 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001228 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001229 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001230 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001231 acpigen_emit_byte(0x00);
1232 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001233}
1234
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001235void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001236{
1237 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001238 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001239 /*
1240 * end tag (acpi 4.0 Section 6.4.2.8)
1241 * 0x79 <checksum>
1242 * 0x00 is treated as a good checksum according to the spec
1243 * and is what iasl generates.
1244 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001245 acpigen_emit_byte(0x79);
1246 acpigen_emit_byte(0x00);
1247
Nico Huber7d89ce32017-04-14 00:08:18 +02001248 /* Start counting past the 2-bytes length added in
1249 acpigen_write_resourcetemplate() above. */
1250 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001251
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001252 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001253 p[0] = len & 0xff;
1254 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001255 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001256 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001257}
1258
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001259static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev, struct resource *res)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001260{
1261 acpigen_write_mem32fixed(0, res->base, res->size);
1262}
1263
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001264static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev, struct resource *res)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001265{
1266 resource_t base = res->base;
1267 resource_t size = res->size;
1268 while (size > 0) {
1269 resource_t sz = size > 255 ? 255 : size;
1270 acpigen_write_io16(base, base, 0, sz, 1);
1271 size -= sz;
1272 base += sz;
1273 }
1274}
1275
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001276void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001277{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001278 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001279
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001280 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001281 search_global_resources(
1282 IORESOURCE_MEM | IORESOURCE_RESERVE,
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001283 IORESOURCE_MEM | IORESOURCE_RESERVE,
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001284 acpigen_add_mainboard_rsvd_mem32, 0);
1285
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001286 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001287 search_global_resources(
1288 IORESOURCE_IO | IORESOURCE_RESERVE,
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001289 IORESOURCE_IO | IORESOURCE_RESERVE,
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001290 acpigen_add_mainboard_rsvd_io, 0);
1291
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001292 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001293}
1294
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001295void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001296{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001297 acpigen_write_scope(scope);
1298 acpigen_write_name(name);
1299 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001300 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001301}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001302
1303static int hex2bin(const char c)
1304{
1305 if (c >= 'A' && c <= 'F')
1306 return c - 'A' + 10;
1307 if (c >= 'a' && c <= 'f')
1308 return c - 'a' + 10;
1309 return c - '0';
1310}
1311
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001312void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001313{
1314 u32 compact = 0;
1315
1316 /* Clamping individual values would be better but
1317 there is a disagreement over what is a valid
1318 EISA id, so accept anything and don't clamp,
1319 parent code should create a valid EISAid.
1320 */
1321 compact |= (eisaid[0] - 'A' + 1) << 26;
1322 compact |= (eisaid[1] - 'A' + 1) << 21;
1323 compact |= (eisaid[2] - 'A' + 1) << 16;
1324 compact |= hex2bin(eisaid[3]) << 12;
1325 compact |= hex2bin(eisaid[4]) << 8;
1326 compact |= hex2bin(eisaid[5]) << 4;
1327 compact |= hex2bin(eisaid[6]);
1328
1329 acpigen_emit_byte(0xc);
1330 acpigen_emit_byte((compact >> 24) & 0xff);
1331 acpigen_emit_byte((compact >> 16) & 0xff);
1332 acpigen_emit_byte((compact >> 8) & 0xff);
1333 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001334}
Duncan Laurie3829f232016-05-09 11:08:46 -07001335
1336/*
1337 * ToUUID(uuid)
1338 *
1339 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1340 * order for the bytes that make up a UUID Buffer object.
1341 * UUID byte order for input:
1342 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1343 * UUID byte order for output:
1344 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1345 */
1346#define UUID_LEN 16
1347void acpigen_write_uuid(const char *uuid)
1348{
1349 uint8_t buf[UUID_LEN];
1350 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1351 8, 9, 10, 11, 12, 13, 14, 15 };
1352
1353 /* Parse UUID string into bytes */
1354 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1355 return;
1356
1357 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001358 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001359 acpigen_write_len_f();
1360
1361 /* Buffer length in bytes */
1362 acpigen_write_word(UUID_LEN);
1363
1364 /* Output UUID in expected order */
1365 for (i = 0; i < UUID_LEN; i++)
1366 acpigen_emit_byte(buf[order[i]]);
1367
1368 acpigen_pop_len();
1369}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001370
1371/*
1372 * Name (_PRx, Package(One) { name })
1373 * ...
1374 * PowerResource (name, level, order)
1375 */
1376void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001377 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001378{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001379 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001380 for (i = 0; i < dev_states_count; i++) {
1381 acpigen_write_name(dev_states[i]);
1382 acpigen_write_package(1);
1383 acpigen_emit_simple_namestring(name);
1384 acpigen_pop_len(); /* Package */
1385 }
1386
1387 acpigen_emit_ext_op(POWER_RES_OP);
1388
1389 acpigen_write_len_f();
1390
1391 acpigen_emit_simple_namestring(name);
1392 acpigen_emit_byte(level);
1393 acpigen_emit_word(order);
1394}
1395
1396/* Sleep (ms) */
1397void acpigen_write_sleep(uint64_t sleep_ms)
1398{
1399 acpigen_emit_ext_op(SLEEP_OP);
1400 acpigen_write_integer(sleep_ms);
1401}
1402
1403void acpigen_write_store(void)
1404{
1405 acpigen_emit_byte(STORE_OP);
1406}
1407
1408/* Store (src, dst) */
1409void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1410{
1411 acpigen_write_store();
1412 acpigen_emit_byte(src);
1413 acpigen_emit_byte(dst);
1414}
1415
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001416/* Store (src, "namestr") */
1417void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1418{
1419 acpigen_write_store();
1420 acpigen_emit_byte(src);
1421 acpigen_emit_namestring(dst);
1422}
1423
Duncan Laurie8e391d32020-09-30 23:17:41 +00001424/* Store (src, "namestr") */
1425void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst)
1426{
1427 acpigen_write_store();
1428 acpigen_write_integer(src);
1429 acpigen_emit_namestring(dst);
1430}
1431
Cliff Huang24f3dc82023-02-04 21:11:51 -08001432/* Store ("namestr", dst) */
1433void acpigen_write_store_namestr_to_op(const char *src, uint8_t dst)
1434{
1435 acpigen_write_store();
1436 acpigen_emit_namestring(src);
1437 acpigen_emit_byte(dst);
1438}
1439
Duncan Laurie8e391d32020-09-30 23:17:41 +00001440/* Store (src, dst) */
1441void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst)
1442{
1443 acpigen_write_store();
1444 acpigen_write_integer(src);
1445 acpigen_emit_byte(dst);
1446}
1447
Felix Held178cf352023-02-09 16:29:46 +01001448/* Store ("namestr", "namestr") */
1449void acpigen_write_store_namestr_to_namestr(const char *src, const char *dst)
1450{
1451 acpigen_write_store();
1452 acpigen_emit_namestring(src);
1453 acpigen_emit_namestring(dst);
1454}
1455
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001456/* Or (arg1, arg2, res) */
1457void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1458{
1459 acpigen_emit_byte(OR_OP);
1460 acpigen_emit_byte(arg1);
1461 acpigen_emit_byte(arg2);
1462 acpigen_emit_byte(res);
1463}
1464
Rajat Jain310623b2020-02-26 11:02:53 -08001465/* Xor (arg1, arg2, res) */
1466void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1467{
1468 acpigen_emit_byte(XOR_OP);
1469 acpigen_emit_byte(arg1);
1470 acpigen_emit_byte(arg2);
1471 acpigen_emit_byte(res);
1472}
1473
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001474/* And (arg1, arg2, res) */
1475void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1476{
1477 acpigen_emit_byte(AND_OP);
1478 acpigen_emit_byte(arg1);
1479 acpigen_emit_byte(arg2);
1480 acpigen_emit_byte(res);
1481}
1482
1483/* Not (arg, res) */
1484void acpigen_write_not(uint8_t arg, uint8_t res)
1485{
1486 acpigen_emit_byte(NOT_OP);
1487 acpigen_emit_byte(arg);
1488 acpigen_emit_byte(res);
1489}
1490
Cliff Huange6083082023-01-19 21:18:23 -08001491/* Concatenate (str, src_res, dest_res) */
1492void acpigen_concatenate_string_op(const char *str, uint8_t src_res, uint8_t dest_res)
1493{
1494 acpigen_emit_byte(CONCATENATE_OP);
1495 acpigen_write_string(str);
1496 acpigen_emit_byte(src_res);
1497 acpigen_emit_byte(dest_res);
1498}
1499
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001500/* Store (str, DEBUG) */
1501void acpigen_write_debug_string(const char *str)
1502{
1503 acpigen_write_store();
1504 acpigen_write_string(str);
1505 acpigen_emit_ext_op(DEBUG_OP);
1506}
1507
1508/* Store (val, DEBUG) */
1509void acpigen_write_debug_integer(uint64_t val)
1510{
1511 acpigen_write_store();
1512 acpigen_write_integer(val);
1513 acpigen_emit_ext_op(DEBUG_OP);
1514}
1515
1516/* Store (op, DEBUG) */
1517void acpigen_write_debug_op(uint8_t op)
1518{
1519 acpigen_write_store();
1520 acpigen_emit_byte(op);
1521 acpigen_emit_ext_op(DEBUG_OP);
1522}
1523
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001524/* Store (str, DEBUG) */
1525void acpigen_write_debug_namestr(const char *str)
1526{
1527 acpigen_write_store();
1528 acpigen_emit_namestring(str);
1529 acpigen_emit_ext_op(DEBUG_OP);
1530}
1531
Cliff Huange6083082023-01-19 21:18:23 -08001532/* Concatenate (str1, res, tmp_res)
1533 Store(tmp_res, DEBUG) */
1534void acpigen_write_debug_concatenate_string_op(const char *str, uint8_t res,
1535 uint8_t tmp_res)
1536{
1537 acpigen_concatenate_string_op(str, res, tmp_res);
1538 acpigen_write_debug_op(tmp_res);
1539}
1540
Cliff Huang063a1b82023-02-18 00:17:26 -08001541static void acpigen_tx_byte(unsigned char byte, void *data)
1542{
1543 acpigen_emit_byte(byte);
1544}
1545
1546/* Store("formatted string", DEBUG) */
1547void acpigen_write_debug_sprintf(const char *fmt, ...)
1548{
1549 va_list args;
1550
1551 acpigen_write_store();
1552
1553 acpigen_emit_byte(STRING_PREFIX);
1554 va_start(args, fmt);
1555 vtxprintf(acpigen_tx_byte, fmt, args, NULL);
1556 va_end(args);
1557 acpigen_emit_byte('\0');
1558
1559 acpigen_emit_ext_op(DEBUG_OP);
1560}
1561
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001562void acpigen_write_if(void)
1563{
1564 acpigen_emit_byte(IF_OP);
1565 acpigen_write_len_f();
1566}
1567
1568/* If (And (arg1, arg2)) */
1569void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1570{
1571 acpigen_write_if();
1572 acpigen_emit_byte(AND_OP);
1573 acpigen_emit_byte(arg1);
1574 acpigen_emit_byte(arg2);
1575}
1576
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001577/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001578 * Generates ACPI code for checking if operand1 and operand2 are equal.
1579 * Both operand1 and operand2 are ACPI ops.
1580 *
1581 * If (Lequal (op,1 op2))
1582 */
1583void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1584{
1585 acpigen_write_if();
1586 acpigen_emit_byte(LEQUAL_OP);
1587 acpigen_emit_byte(op1);
1588 acpigen_emit_byte(op2);
1589}
1590
1591/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001592 * Generates ACPI code for checking if operand1 is greater than operand2.
1593 * Both operand1 and operand2 are ACPI ops.
1594 *
1595 * If (Lgreater (op1 op2))
1596 */
1597void acpigen_write_if_lgreater_op_op(uint8_t op1, uint8_t op2)
1598{
1599 acpigen_write_if();
1600 acpigen_emit_byte(LGREATER_OP);
1601 acpigen_emit_byte(op1);
1602 acpigen_emit_byte(op2);
1603}
1604
1605/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001606 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1607 * operand1 is ACPI op and operand2 is an integer.
1608 *
1609 * If (Lequal (op, val))
1610 */
1611void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001612{
1613 acpigen_write_if();
1614 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001615 acpigen_emit_byte(op);
1616 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001617}
1618
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001619/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001620 * Generates ACPI code for checking if operand is greater than the value, where,
1621 * operand is ACPI op and val is an integer.
1622 *
1623 * If (Lgreater (op, val))
1624 */
1625void acpigen_write_if_lgreater_op_int(uint8_t op, uint64_t val)
1626{
1627 acpigen_write_if();
1628 acpigen_emit_byte(LGREATER_OP);
1629 acpigen_emit_byte(op);
1630 acpigen_write_integer(val);
1631}
1632
1633/*
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001634 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1635 * operand1 is namestring and operand2 is an integer.
1636 *
1637 * If (Lequal ("namestr", val))
1638 */
1639void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1640{
1641 acpigen_write_if();
1642 acpigen_emit_byte(LEQUAL_OP);
1643 acpigen_emit_namestring(namestr);
1644 acpigen_write_integer(val);
1645}
1646
Tim Wawrzynczakc4ca2f62021-07-01 11:18:50 -06001647/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001648 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1649 * operand1 is namestring and operand2 is an integer.
1650 *
1651 * If (Lgreater ("namestr", val))
1652 */
1653void acpigen_write_if_lgreater_namestr_int(const char *namestr, uint64_t val)
1654{
1655 acpigen_write_if();
1656 acpigen_emit_byte(LGREATER_OP);
1657 acpigen_emit_namestring(namestr);
1658 acpigen_write_integer(val);
1659}
1660
1661/*
Tim Wawrzynczakc4ca2f62021-07-01 11:18:50 -06001662 * Generates ACPI code to check at runtime if an object named `namestring`
1663 * exists, and leaves the If scope open to continue execute code when this
1664 * is true. NOTE: Requires matching acpigen_write_if_end().
1665 *
1666 * If (CondRefOf (NAME))
1667 */
1668void acpigen_write_if_cond_ref_of(const char *namestring)
1669{
1670 acpigen_write_if();
1671 acpigen_emit_ext_op(COND_REFOF_OP);
1672 acpigen_emit_namestring(namestring);
1673 acpigen_emit_byte(ZERO_OP); /* ignore COND_REFOF_OP destination */
1674}
1675
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001676/* Closes previously opened if statement and generates ACPI code for else statement. */
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001677void acpigen_write_else(void)
1678{
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001679 acpigen_pop_len();
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001680 acpigen_emit_byte(ELSE_OP);
1681 acpigen_write_len_f();
1682}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001683
Duncan Laurie36858202020-10-06 21:01:51 +00001684void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count)
1685{
1686 acpigen_emit_byte(SHIFT_LEFT_OP);
1687 acpigen_emit_byte(src_result);
1688 acpigen_write_integer(count);
1689 acpigen_emit_byte(ZERO_OP);
1690}
1691
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001692void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1693{
1694 acpigen_emit_byte(TO_BUFFER_OP);
1695 acpigen_emit_byte(src);
1696 acpigen_emit_byte(dst);
1697}
1698
1699void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1700{
1701 acpigen_emit_byte(TO_INTEGER_OP);
1702 acpigen_emit_byte(src);
1703 acpigen_emit_byte(dst);
1704}
1705
Aaron Durbin80bc0912020-08-19 23:17:42 -06001706void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op)
1707{
1708 acpigen_emit_byte(TO_INTEGER_OP);
1709 acpigen_emit_namestring(source);
1710 acpigen_emit_byte(dst_op);
1711}
1712
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301713void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001714{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301715 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001716
1717 acpigen_emit_byte(BUFFER_OP);
1718 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301719 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001720
1721 for (i = 0; i < size; i++)
1722 acpigen_emit_byte(arr[i]);
1723
1724 acpigen_pop_len();
1725}
1726
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301727void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001728{
1729 acpigen_emit_byte(RETURN_OP);
1730 acpigen_write_byte_buffer(arr, size);
1731}
1732
1733void acpigen_write_return_singleton_buffer(uint8_t arg)
1734{
1735 acpigen_write_return_byte_buffer(&arg, 1);
1736}
1737
Duncan Laurie2fe74712020-06-01 17:36:56 -07001738void acpigen_write_return_op(uint8_t arg)
1739{
1740 acpigen_emit_byte(RETURN_OP);
1741 acpigen_emit_byte(arg);
1742}
1743
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001744void acpigen_write_return_byte(uint8_t arg)
1745{
1746 acpigen_emit_byte(RETURN_OP);
1747 acpigen_write_byte(arg);
1748}
1749
Naresh G Solanki05abe432016-11-17 00:16:29 +05301750void acpigen_write_return_integer(uint64_t arg)
1751{
1752 acpigen_emit_byte(RETURN_OP);
1753 acpigen_write_integer(arg);
1754}
1755
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001756void acpigen_write_return_namestr(const char *arg)
1757{
1758 acpigen_emit_byte(RETURN_OP);
1759 acpigen_emit_namestring(arg);
1760}
1761
Naresh G Solanki05abe432016-11-17 00:16:29 +05301762void acpigen_write_return_string(const char *arg)
1763{
1764 acpigen_emit_byte(RETURN_OP);
1765 acpigen_write_string(arg);
1766}
1767
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001768void acpigen_write_upc(enum acpi_upc_type type)
1769{
1770 acpigen_write_name("_UPC");
1771 acpigen_write_package(4);
1772 /* Connectable */
1773 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1774 /* Type */
1775 acpigen_write_byte(type);
1776 /* Reserved0 */
1777 acpigen_write_zero();
1778 /* Reserved1 */
1779 acpigen_write_zero();
1780 acpigen_pop_len();
1781}
1782
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001783void acpigen_write_pld(const struct acpi_pld *pld)
1784{
1785 uint8_t buf[20];
1786
1787 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1788 return;
1789
1790 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001791 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001792 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001793 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001794}
1795
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001796void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *), size_t count, void *arg)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301797{
1798 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1799 acpigen_write_dsm_uuid_arr(&id, 1);
1800}
1801
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001802/*
1803 * Create a supported functions bitmask
1804 * bit 0: other functions than 0 are supported
1805 * bits 1-x: function x supported
1806 */
Arthur Heymans9c1f78d2023-06-22 21:04:06 +02001807/* On GCC aarch64 the compiler is worried about alloca() having unbounded stack usage. */
1808#if defined(__GNUC__) && !defined(__clang__)
1809#pragma GCC diagnostic ignored "-Wstack-usage="
1810#endif
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001811static void acpigen_dsm_uuid_enum_functions(const struct dsm_uuid *id)
1812{
1813 const size_t bytes = DIV_ROUND_UP(id->count, BITS_PER_BYTE);
1814 uint8_t *buffer = alloca(bytes);
1815 bool set = false;
1816 size_t cb_idx = 0;
1817
1818 memset(buffer, 0, bytes);
1819
1820 for (size_t i = 0; i < bytes; i++) {
1821 for (size_t j = 0; j < BITS_PER_BYTE; j++) {
1822 if (cb_idx >= id->count)
1823 break;
1824
1825 if (id->callbacks[cb_idx++]) {
1826 set = true;
1827 buffer[i] |= BIT(j);
1828 }
1829 }
1830 }
1831
1832 if (set)
1833 buffer[0] |= BIT(0);
1834
1835 acpigen_write_return_byte_buffer(buffer, bytes);
1836}
1837
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301838static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1839{
1840 size_t i;
1841
1842 /* If (LEqual (Local0, ToUUID(uuid))) */
1843 acpigen_write_if();
1844 acpigen_emit_byte(LEQUAL_OP);
1845 acpigen_emit_byte(LOCAL0_OP);
1846 acpigen_write_uuid(id->uuid);
1847
1848 /* ToInteger (Arg2, Local1) */
1849 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1850
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001851 /* If (LEqual(Local1, 0)) */
1852 {
1853 acpigen_write_if_lequal_op_int(LOCAL1_OP, 0);
1854 if (id->callbacks[0])
1855 id->callbacks[0](id->arg);
1856 else if (id->count)
1857 acpigen_dsm_uuid_enum_functions(id);
1858 acpigen_write_if_end();
1859 }
1860
1861 for (i = 1; i < id->count; i++) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301862 /* If (LEqual (Local1, i)) */
1863 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1864
1865 /* Callback to write if handler. */
1866 if (id->callbacks[i])
1867 id->callbacks[i](id->arg);
1868
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001869 acpigen_write_if_end(); /* If */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301870 }
1871
1872 /* Default case: Return (Buffer (One) { 0x0 }) */
1873 acpigen_write_return_singleton_buffer(0x0);
1874
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001875 acpigen_write_if_end(); /* If (LEqual (Local0, ToUUID(uuid))) */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301876
1877}
1878
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001879/*
1880 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301881 * This function takes as input array of uuid for the device, set of callbacks
1882 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1883 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1884 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001885 *
1886 * Arguments passed into _DSM method:
1887 * Arg0 = UUID
1888 * Arg1 = Revision
1889 * Arg2 = Function index
1890 * Arg3 = Function specific arguments
1891 *
1892 * AML code generated would look like:
1893 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301894 * ToBuffer (Arg0, Local0)
1895 * If (LEqual (Local0, ToUUID(uuid))) {
1896 * ToInteger (Arg2, Local1)
1897 * If (LEqual (Local1, 0)) {
1898 * <acpigen by callback[0]>
1899 * }
1900 * ...
1901 * If (LEqual (Local1, n)) {
1902 * <acpigen by callback[n]>
1903 * }
1904 * Return (Buffer (One) { 0x0 })
1905 * }
1906 * ...
1907 * If (LEqual (Local0, ToUUID(uuidn))) {
1908 * ...
1909 * }
1910 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001911 * }
1912 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301913void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001914{
1915 size_t i;
1916
1917 /* Method (_DSM, 4, Serialized) */
1918 acpigen_write_method_serialized("_DSM", 0x4);
1919
1920 /* ToBuffer (Arg0, Local0) */
1921 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1922
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001923 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301924 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001925
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301926 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001927 acpigen_write_return_singleton_buffer(0x0);
1928
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001929 acpigen_pop_len(); /* Method _DSM */
1930}
1931
Matt Delcob425bc82018-08-13 13:36:27 -07001932void acpigen_write_CPPC_package(const struct cppc_config *config)
1933{
1934 u32 i;
1935 u32 max;
1936 switch (config->version) {
1937 case 1:
1938 max = CPPC_MAX_FIELDS_VER_1;
1939 break;
1940 case 2:
1941 max = CPPC_MAX_FIELDS_VER_2;
1942 break;
1943 case 3:
1944 max = CPPC_MAX_FIELDS_VER_3;
1945 break;
1946 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001947 printk(BIOS_ERR, "CPPC version %u is not implemented\n", config->version);
Matt Delcob425bc82018-08-13 13:36:27 -07001948 return;
1949 }
1950 acpigen_write_name(CPPC_PACKAGE_NAME);
1951
1952 /* Adding 2 to account for length and version fields */
1953 acpigen_write_package(max + 2);
1954 acpigen_write_dword(max + 2);
1955
1956 acpigen_write_byte(config->version);
1957
1958 for (i = 0; i < max; ++i) {
Michael Niewöhner38107fa2021-10-05 22:22:21 +02001959 const cppc_entry_t *entry = &config->entries[i];
1960 if (entry->type == CPPC_TYPE_DWORD)
1961 acpigen_write_dword(entry->dword);
1962 else
1963 acpigen_write_register_resource(&entry->reg);
Matt Delcob425bc82018-08-13 13:36:27 -07001964 }
1965 acpigen_pop_len();
1966}
1967
1968void acpigen_write_CPPC_method(void)
1969{
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001970 char pscope[16];
Felix Heldf0a8b042023-05-12 15:55:06 +02001971 snprintf(pscope, sizeof(pscope),
1972 "\\_SB." CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001973
Matt Delcob425bc82018-08-13 13:36:27 -07001974 acpigen_write_method("_CPC", 0);
1975 acpigen_emit_byte(RETURN_OP);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001976 acpigen_emit_namestring(pscope);
Matt Delcob425bc82018-08-13 13:36:27 -07001977 acpigen_pop_len();
1978}
1979
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001980/*
1981 * Generate ACPI AML code for _ROM method.
1982 * This function takes as input ROM data and ROM length.
1983 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001984 * The ACPI spec isn't clear about what should happen at the end of the
1985 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1986 * bytes in the returned buffer with zeros.
1987 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001988 * Arguments passed into _DSM method:
1989 * Arg0 = Offset in Bytes
1990 * Arg1 = Bytes to return
1991 *
1992 * Example:
1993 * acpigen_write_rom(0xdeadbeef, 0x10000)
1994 *
1995 * AML code generated would look like:
1996 * Method (_ROM, 2, NotSerialized) {
1997 *
1998 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1999 * Field (ROMS, AnyAcc, NoLock, Preserve)
2000 * {
2001 * Offset (0),
2002 * RBF0, 0x80000
2003 * }
2004 *
2005 * Store (Arg0, Local0)
2006 * Store (Arg1, Local1)
2007 *
2008 * If (LGreater (Local1, 0x1000))
2009 * {
2010 * Store (0x1000, Local1)
2011 * }
2012 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002013 * Store (Local1, Local3)
2014 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002015 * If (LGreater (Local0, 0x10000))
2016 * {
2017 * Return(Buffer(Local1){0})
2018 * }
2019 *
2020 * If (LGreater (Local0, 0x0f000))
2021 * {
2022 * Subtract (0x10000, Local0, Local2)
2023 * If (LGreater (Local1, Local2))
2024 * {
2025 * Store (Local2, Local1)
2026 * }
2027 * }
2028 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002029 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002030 *
2031 * Multiply (Local0, 0x08, Local0)
2032 * Multiply (Local1, 0x08, Local1)
2033 *
2034 * CreateField (RBF0, Local0, Local1, TMPB)
2035 * Store (TMPB, ROM1)
2036 * Return (ROM1)
2037 * }
2038 */
2039
2040void acpigen_write_rom(void *bios, const size_t length)
2041{
2042 ASSERT(bios)
2043 ASSERT(length)
2044
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02002045 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06002046 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002047
2048 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002049 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY, (uintptr_t)bios, length);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002050 acpigen_write_opregion(&opreg);
2051
2052 struct fieldlist l[] = {
2053 FIELDLIST_OFFSET(0),
2054 FIELDLIST_NAMESTR("RBF0", 8 * length),
2055 };
2056
2057 /* Field (ROMS, AnyAcc, NoLock, Preserve)
2058 * {
2059 * Offset (0),
2060 * RBF0, 0x80000
2061 * } */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002062 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002063
2064 /* Store (Arg0, Local0) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002065 acpigen_write_store_ops(ARG0_OP, LOCAL0_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002066
2067 /* Store (Arg1, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002068 acpigen_write_store_ops(ARG1_OP, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002069
2070 /* ACPI SPEC requires to return at maximum 4KiB */
2071 /* If (LGreater (Local1, 0x1000)) */
Felix Held383a06e2023-02-09 16:25:38 +01002072 acpigen_write_if_lgreater_op_int(LOCAL1_OP, 0x1000);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002073
2074 /* Store (0x1000, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002075 acpigen_write_store_int_to_op(0x1000, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002076
2077 /* Pop if */
2078 acpigen_pop_len();
2079
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002080 /* Store (Local1, Local3) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002081 acpigen_write_store_ops(LOCAL1_OP, LOCAL3_OP);
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002082
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002083 /* If (LGreater (Local0, length)) */
Felix Held383a06e2023-02-09 16:25:38 +01002084 acpigen_write_if_lgreater_op_int(LOCAL0_OP, length);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002085
2086 /* Return(Buffer(Local1){0}) */
2087 acpigen_emit_byte(RETURN_OP);
2088 acpigen_emit_byte(BUFFER_OP);
2089 acpigen_write_len_f();
2090 acpigen_emit_byte(LOCAL1_OP);
2091 acpigen_emit_byte(0);
2092 acpigen_pop_len();
2093
2094 /* Pop if */
2095 acpigen_pop_len();
2096
2097 /* If (LGreater (Local0, length - 4096)) */
Felix Held383a06e2023-02-09 16:25:38 +01002098 acpigen_write_if_lgreater_op_int(LOCAL0_OP, length - 4096);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002099
2100 /* Subtract (length, Local0, Local2) */
2101 acpigen_emit_byte(SUBTRACT_OP);
2102 acpigen_write_integer(length);
2103 acpigen_emit_byte(LOCAL0_OP);
2104 acpigen_emit_byte(LOCAL2_OP);
2105
2106 /* If (LGreater (Local1, Local2)) */
Felix Held383a06e2023-02-09 16:25:38 +01002107 acpigen_write_if_lgreater_op_op(LOCAL1_OP, LOCAL2_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002108
2109 /* Store (Local2, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002110 acpigen_write_store_ops(LOCAL2_OP, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002111
2112 /* Pop if */
2113 acpigen_pop_len();
2114
2115 /* Pop if */
2116 acpigen_pop_len();
2117
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002118 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002119 acpigen_write_name("ROM1");
2120 acpigen_emit_byte(BUFFER_OP);
2121 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002122 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002123 acpigen_emit_byte(0);
2124 acpigen_pop_len();
2125
2126 /* Multiply (Local1, 0x08, Local1) */
2127 acpigen_emit_byte(MULTIPLY_OP);
2128 acpigen_emit_byte(LOCAL1_OP);
2129 acpigen_write_integer(0x08);
2130 acpigen_emit_byte(LOCAL1_OP);
2131
2132 /* Multiply (Local0, 0x08, Local0) */
2133 acpigen_emit_byte(MULTIPLY_OP);
2134 acpigen_emit_byte(LOCAL0_OP);
2135 acpigen_write_integer(0x08);
2136 acpigen_emit_byte(LOCAL0_OP);
2137
2138 /* CreateField (RBF0, Local0, Local1, TMPB) */
2139 acpigen_emit_ext_op(CREATEFIELD_OP);
2140 acpigen_emit_namestring("RBF0");
2141 acpigen_emit_byte(LOCAL0_OP);
2142 acpigen_emit_byte(LOCAL1_OP);
2143 acpigen_emit_namestring("TMPB");
2144
2145 /* Store (TMPB, ROM1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002146 acpigen_write_store_namestr_to_namestr("TMPB", "ROM1");
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002147
2148 /* Return (ROM1) */
2149 acpigen_emit_byte(RETURN_OP);
2150 acpigen_emit_namestring("ROM1");
2151
2152 /* Pop method */
2153 acpigen_pop_len();
2154}
2155
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002156/*
2157 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
2158 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
2159 * make callbacks into SoC acpigen code.
2160 *
2161 * Returns 0 on success and -1 on error.
2162 */
Duncan Laurie30c3f912020-10-09 04:48:53 +00002163int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002164{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002165 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002166 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002167 else
2168 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002169}
2170
Duncan Laurie30c3f912020-10-09 04:48:53 +00002171int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002172{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002173 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002174 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
2175 else
2176 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
2177}
Jonathan Zhang71299c22020-01-27 11:38:57 -08002178
Duncan Laurie30c3f912020-10-09 04:48:53 +00002179void acpigen_get_rx_gpio(const struct acpi_gpio *gpio)
Rajat Jain310623b2020-02-26 11:02:53 -08002180{
2181 acpigen_soc_read_rx_gpio(gpio->pins[0]);
2182
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002183 if (gpio->active_low)
Rajat Jain310623b2020-02-26 11:02:53 -08002184 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2185}
2186
Duncan Laurie30c3f912020-10-09 04:48:53 +00002187void acpigen_get_tx_gpio(const struct acpi_gpio *gpio)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002188{
2189 acpigen_soc_get_tx_gpio(gpio->pins[0]);
2190
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002191 if (gpio->active_low)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002192 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2193}
2194
Jonathan Zhang71299c22020-01-27 11:38:57 -08002195/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002196void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran, u16 range_min,
2197 u16 range_max, u16 translation, u16 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002198{
Felix Heldb2f2b532023-05-11 22:22:06 +02002199 /* Byte 0: Type 1, Large Item Value 0x8: Word Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002200 acpigen_emit_byte(0x88);
2201 /* Byte 1+2: length (0x000d) */
2202 acpigen_emit_byte(0x0d);
2203 acpigen_emit_byte(0x00);
2204 /* resource type */
2205 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2206 /* general flags */
2207 acpigen_emit_byte(gen_flags);
2208 /* type flags */
2209 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2210 acpigen_emit_byte(type_flags);
2211 /* granularity, min, max, translation, length */
2212 acpigen_emit_word(gran);
2213 acpigen_emit_word(range_min);
2214 acpigen_emit_word(range_max);
2215 acpigen_emit_word(translation);
2216 acpigen_emit_word(length);
2217}
2218
2219/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002220void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags, u32 gran,
2221 u32 range_min, u32 range_max, u32 translation, u32 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002222{
Felix Heldb2f2b532023-05-11 22:22:06 +02002223 /* Byte 0: Type 1, Large Item Value 0x7: DWord Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002224 acpigen_emit_byte(0x87);
2225 /* Byte 1+2: length (0023) */
2226 acpigen_emit_byte(23);
2227 acpigen_emit_byte(0x00);
2228 /* resource type */
2229 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2230 /* general flags */
2231 acpigen_emit_byte(gen_flags);
2232 /* type flags */
2233 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2234 acpigen_emit_byte(type_flags);
2235 /* granularity, min, max, translation, length */
2236 acpigen_emit_dword(gran);
2237 acpigen_emit_dword(range_min);
2238 acpigen_emit_dword(range_max);
2239 acpigen_emit_dword(translation);
2240 acpigen_emit_dword(length);
2241}
2242
2243static void acpigen_emit_qword(u64 data)
2244{
2245 acpigen_emit_dword(data & 0xffffffff);
2246 acpigen_emit_dword((data >> 32) & 0xffffffff);
2247}
2248
2249/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002250void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags, u64 gran,
2251 u64 range_min, u64 range_max, u64 translation, u64 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002252{
Felix Heldb2f2b532023-05-11 22:22:06 +02002253 /* Byte 0: Type 1, Large Item Value 0xa: QWord Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002254 acpigen_emit_byte(0x8a);
2255 /* Byte 1+2: length (0x002b) */
2256 acpigen_emit_byte(0x2b);
2257 acpigen_emit_byte(0x00);
2258 /* resource type */
2259 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2260 /* general flags */
2261 acpigen_emit_byte(gen_flags);
2262 /* type flags */
2263 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2264 acpigen_emit_byte(type_flags);
2265 /* granularity, min, max, translation, length */
2266 acpigen_emit_qword(gran);
2267 acpigen_emit_qword(range_min);
2268 acpigen_emit_qword(range_max);
2269 acpigen_emit_qword(translation);
2270 acpigen_emit_qword(length);
2271}
Furquan Shaikh1a829232020-04-23 11:11:05 -07002272
Felix Held0fdede02023-05-27 01:02:37 +02002273void acpigen_resource_producer_bus_number(u16 bus_base, u16 bus_limit)
Felix Held5a82f0d2023-05-09 16:10:35 +02002274{
2275 acpigen_resource_word(RSRC_TYPE_BUS, /* res_type */
2276 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2277 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002278 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2279 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held5a82f0d2023-05-09 16:10:35 +02002280 BUS_NUM_RANGE_RESOURCE_FLAG, /* type_flags */
2281 0, /* gran */
2282 bus_base, /* range_min */
2283 bus_limit, /* range_max */
2284 0x0, /* translation */
2285 bus_limit - bus_base + 1); /* length */
2286}
2287
Felix Held0fdede02023-05-27 01:02:37 +02002288void acpigen_resource_producer_io(u16 io_base, u16 io_limit)
Felix Held6695be62023-05-09 16:18:51 +02002289{
Felix Helde3c9a042023-06-05 19:59:25 +02002290 acpigen_resource_dword(RSRC_TYPE_IO, /* res_type */
Felix Held6695be62023-05-09 16:18:51 +02002291 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2292 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002293 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2294 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held6695be62023-05-09 16:18:51 +02002295 IO_RSRC_FLAG_ENTIRE_RANGE, /* type_flags */
2296 0, /* gran */
2297 io_base, /* range_min */
2298 io_limit, /* range_max */
2299 0x0, /* translation */
2300 io_limit - io_base + 1); /* length */
2301}
2302
Felix Held0fdede02023-05-27 01:02:37 +02002303static void acpigen_resource_producer_mmio32(u32 mmio_base, u32 mmio_limit, u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002304{
2305 acpigen_resource_dword(RSRC_TYPE_MEM, /* res_type */
2306 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2307 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002308 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2309 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held21594fd12023-05-09 16:39:57 +02002310 type_flags, /* type_flags */
2311 0, /* gran */
2312 mmio_base, /* range_min */
2313 mmio_limit, /* range_max */
2314 0x0, /* translation */
2315 mmio_limit - mmio_base + 1); /* length */
2316}
2317
Felix Held0fdede02023-05-27 01:02:37 +02002318static void acpigen_resource_producer_mmio64(u64 mmio_base, u64 mmio_limit, u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002319{
2320 acpigen_resource_qword(RSRC_TYPE_MEM, /* res_type */
2321 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2322 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002323 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2324 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held21594fd12023-05-09 16:39:57 +02002325 type_flags, /* type_flags */
2326 0, /* gran */
2327 mmio_base, /* range_min */
2328 mmio_limit, /* range_max */
2329 0x0, /* translation */
2330 mmio_limit - mmio_base + 1); /* length */
2331}
2332
Felix Held0fdede02023-05-27 01:02:37 +02002333void acpigen_resource_producer_mmio(u64 mmio_base, u64 mmio_limit, u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002334{
2335 if (mmio_base < 4ULL * GiB && mmio_limit < 4ULL * GiB)
Felix Held0fdede02023-05-27 01:02:37 +02002336 acpigen_resource_producer_mmio32(mmio_base, mmio_limit, type_flags);
Felix Held21594fd12023-05-09 16:39:57 +02002337 else
Felix Held0fdede02023-05-27 01:02:37 +02002338 acpigen_resource_producer_mmio64(mmio_base, mmio_limit, type_flags);
Felix Held21594fd12023-05-09 16:39:57 +02002339}
2340
Furquan Shaikh1a829232020-04-23 11:11:05 -07002341void acpigen_write_ADR(uint64_t adr)
2342{
2343 acpigen_write_name_qword("_ADR", adr);
2344}
2345
Duncan Laurie08a942f2020-04-29 12:13:14 -07002346/**
2347 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2348 * @address: SoundWire device address properties.
2349 *
2350 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2351 *
2352 * 63..52 - Reserved (0)
2353 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2354 * Used when a Controller has multiple master devices, each producing a
2355 * separate SoundWire Link. Set to 0 for single-link controllers.
2356 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2357 * 47..44 - SoundWire specification version that this device supports
2358 * 43..40 - Unique ID for multiple devices
2359 * 39..24 - MIPI standard manufacturer code
2360 * 23..08 - Vendor defined part ID
2361 * 07..00 - MIPI class encoding
2362 */
2363void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
2364{
2365 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
2366 (((uint64_t)address->version & 0xf) << 44) |
2367 (((uint64_t)address->unique_id & 0xf) << 40) |
2368 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
2369 (((uint64_t)address->part_id & 0xffff) << 8) |
2370 (((uint64_t)address->class & 0xff)));
2371}
Tim Wawrzynczakf6945022020-06-04 15:18:47 -06002372
2373void acpigen_notify(const char *namestr, int value)
2374{
2375 acpigen_emit_byte(NOTIFY_OP);
2376 acpigen_emit_namestring(namestr);
2377 acpigen_write_integer(value);
2378}
Aaron Durbin80bc0912020-08-19 23:17:42 -06002379
2380static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name)
2381{
2382 acpigen_emit_byte(aml_op);
2383 acpigen_emit_byte(srcop);
2384 acpigen_write_integer(byte_offset);
2385 acpigen_emit_namestring(name);
2386}
2387
2388void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name)
2389{
2390 _create_field(CREATE_BYTE_OP, op, byte_offset, name);
2391}
2392
2393void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name)
2394{
2395 _create_field(CREATE_WORD_OP, op, byte_offset, name);
2396}
2397
2398void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name)
2399{
2400 _create_field(CREATE_DWORD_OP, op, byte_offset, name);
2401}
2402
2403void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name)
2404{
2405 _create_field(CREATE_QWORD_OP, op, byte_offset, name);
2406}
Jason Gleneskca36aed2020-09-15 21:01:57 -07002407
2408void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
2409{
2410 acpigen_write_name("_PCT");
2411 acpigen_write_package(0x02);
2412 acpigen_write_register_resource(perf_ctrl);
2413 acpigen_write_register_resource(perf_sts);
2414
2415 acpigen_pop_len();
2416}
2417
2418void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value)
2419{
2420 acpigen_write_package(0x08);
2421 acpigen_write_dword(pstate_value->core_freq);
2422 acpigen_write_dword(pstate_value->power);
2423 acpigen_write_dword(pstate_value->transition_latency);
2424 acpigen_write_dword(pstate_value->bus_master_latency);
2425
2426 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t));
2427 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t));
2428 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t));
2429 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t));
2430
2431 acpigen_pop_len();
2432}
2433
2434void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
2435{
2436 size_t pstate;
2437
2438 acpigen_write_name("XPSS");
2439 acpigen_write_package(nentries);
2440 for (pstate = 0; pstate < nentries; pstate++) {
2441 acpigen_write_xpss_package(pstate_values);
2442 pstate_values++;
2443 }
2444
2445 acpigen_pop_len();
2446}
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002447
2448/* Delay up to wait_ms until provided namestr matches expected value. */
2449void acpigen_write_delay_until_namestr_int(uint32_t wait_ms, const char *name, uint64_t value)
2450{
2451 uint32_t wait_ms_segment = 1;
2452 uint32_t segments = wait_ms;
2453
2454 /* Sleep in 16ms segments if delay is more than 32ms. */
2455 if (wait_ms > 32) {
2456 wait_ms_segment = 16;
2457 segments = wait_ms / 16;
2458 }
2459
2460 acpigen_write_store_int_to_op(segments, LOCAL7_OP);
2461 acpigen_emit_byte(WHILE_OP);
2462 acpigen_write_len_f();
2463 acpigen_emit_byte(LGREATER_OP);
2464 acpigen_emit_byte(LOCAL7_OP);
2465 acpigen_emit_byte(ZERO_OP);
2466
2467 /* If name is not provided then just delay in a loop. */
2468 if (name) {
2469 acpigen_write_if_lequal_namestr_int(name, value);
2470 acpigen_emit_byte(BREAK_OP);
2471 acpigen_pop_len(); /* If */
2472 }
2473
2474 acpigen_write_sleep(wait_ms_segment);
2475 acpigen_emit_byte(DECREMENT_OP);
2476 acpigen_emit_byte(LOCAL7_OP);
2477 acpigen_pop_len(); /* While */
Cliff Huangf97598f2023-03-01 14:30:41 -08002478
2479 if (name) {
2480 acpigen_write_if_lequal_op_op(LOCAL7_OP, ZERO_OP);
2481 acpigen_write_debug_sprintf("WARN: Wait loop timeout for variable %s",
2482 name);
2483 acpigen_pop_len(); /* If */
2484 }
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002485}
Arthur Heymans0eb59742023-04-25 16:23:37 +02002486
2487void acpigen_ssdt_override_sleep_states(bool enable_s1, bool enable_s2, bool enable_s3,
2488 bool enable_s4)
2489{
2490 assert(!(enable_s1 && CONFIG(ACPI_S1_NOT_SUPPORTED)));
2491 assert(!(enable_s3 && !CONFIG(HAVE_ACPI_RESUME)));
2492 assert(!(enable_s4 && CONFIG(DISABLE_ACPI_HIBERNATE)));
2493
2494 acpigen_write_scope("\\");
2495 uint32_t sleep_enable = (enable_s1 << 0) | (enable_s2 << 1)
2496 | (enable_s3 << 2) | (enable_s4 << 3);
2497 acpigen_write_name_dword("OSFG", sleep_enable);
2498 acpigen_pop_len();
2499}