blob: 4049df0dd633b335de3e38533d4afd72ecc13ff6 [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
Felix Held575ee132023-11-13 14:50:35 +01006/* If you need to change this, change acpigen_pop_len too */
7#define ACPIGEN_RSVD_PKGLEN_BYTES 3
Rudolf Marek293b5f52009-02-01 18:35:15 +00008
Duncan Laurie3829f232016-05-09 11:08:46 -07009#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000010#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070011#include <acpi/acpigen.h>
Elyes HAOUAScd4fe0f2019-03-29 17:12:15 +010012#include <assert.h>
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -060013#include <commonlib/helpers.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000014#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000015#include <device/device.h>
Duncan Laurie08a942f2020-04-29 12:13:14 -070016#include <device/soundwire.h>
Elyes Haouasbdd03c22024-05-27 11:20:07 +020017#include <stdio.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010018#include <types.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000019
20static char *gencurrent;
21
22char *len_stack[ACPIGEN_LENSTACK_SIZE];
23int ltop = 0;
24
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010025void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000026{
27 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010028 len_stack[ltop++] = gencurrent;
Felix Held575ee132023-11-13 14:50:35 +010029 /* Reserve ACPIGEN_RSVD_PKGLEN_BYTES bytes for PkgLength. The actual byte values will
30 be written later in the corresponding acpigen_pop_len call. */
31 for (size_t i = 0; i < ACPIGEN_RSVD_PKGLEN_BYTES; i++)
32 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000033}
34
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010035void acpigen_pop_len(void)
36{
Felix Helda6f74592023-11-10 19:57:27 +010037 size_t len;
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010038 ASSERT(ltop > 0)
39 char *p = len_stack[--ltop];
40 len = gencurrent - p;
Felix Held575ee132023-11-13 14:50:35 +010041 const size_t payload_len = len - ACPIGEN_RSVD_PKGLEN_BYTES;
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010042
Felix Helda6f74592023-11-10 19:57:27 +010043 if (len <= 0x3f + 2) {
44 /* PkgLength of up to 0x3f can be encoded in one PkgLength byte instead of the
45 reserved 3 bytes. Since only 1 PkgLength byte will be written, the payload
46 data needs to be moved by 2 bytes */
Felix Held575ee132023-11-13 14:50:35 +010047 memmove(&p[ACPIGEN_RSVD_PKGLEN_BYTES - 2],
48 &p[ACPIGEN_RSVD_PKGLEN_BYTES], payload_len);
Felix Helda6f74592023-11-10 19:57:27 +010049 /* Adjust the PkgLength to take into account that we only use 1 of the 3
50 reserved bytes */
51 len -= 2;
52 /* The two most significant bits of PkgLength get the value of 0 to indicate
53 there are no additional PkgLength bytes. In this case the single PkgLength
54 byte encodes the length in its lower 6 bits */
55 p[0] = len;
56 /* Adjust pointer for next ACPI bytecode byte */
57 acpigen_set_current(p + len);
58 } else if (len <= 0xfff + 1) {
59 /* PkgLength of up to 0xfff can be encoded in 2 PkgLength bytes instead of the
60 reserved 3 bytes. Since only 2 PkgLength bytes will be written, the payload
61 data needs to be moved by 1 byte */
Felix Held575ee132023-11-13 14:50:35 +010062 memmove(&p[ACPIGEN_RSVD_PKGLEN_BYTES - 1],
63 &p[ACPIGEN_RSVD_PKGLEN_BYTES], payload_len);
Felix Helda6f74592023-11-10 19:57:27 +010064 /* Adjust the PkgLength to take into account that we only use 2 of the 3
65 reserved bytes */
66 len -= 1;
67 /* The two most significant bits of PkgLength get the value of 1 to indicate
68 there's a second PkgLength byte. The lower 4 bits of the first PkgLength
69 byte and the second PkgLength byte encode the length */
70 p[0] = (0x1 << 6 | (len & 0xf));
71 p[1] = (len >> 4 & 0xff);
72 /* Adjust pointer for next ACPI bytecode byte */
73 acpigen_set_current(p + len);
74 } else if (len <= 0xfffff) {
75 /* PkgLength of up to 0xfffff can be encoded in 3 PkgLength bytes. Since this
76 is the amount of reserved bytes, no need to move the payload in this case */
77 /* The two most significant bits of PkgLength get the value of 2 to indicate
78 there are two more PkgLength bytes following the first one. The lower 4 bits
79 of the first PkgLength byte and the two following PkgLength bytes encode the
80 length */
81 p[0] = (0x2 << 6 | (len & 0xf));
82 p[1] = (len >> 4 & 0xff);
83 p[2] = (len >> 12 & 0xff);
84 /* No need to adjust pointer for next ACPI bytecode byte */
85 } else {
86 /* The case of PkgLength up to 0xfffffff isn't supported at the moment */
87 printk(BIOS_ERR, "%s: package length exceeds maximum of 0xfffff.\n", __func__);
88 }
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010089}
90
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000091void acpigen_set_current(char *curr)
92{
93 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000094}
95
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000096char *acpigen_get_current(void)
97{
98 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000099}
100
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100101void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000102{
103 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000104}
105
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700106void acpigen_emit_ext_op(uint8_t op)
107{
108 acpigen_emit_byte(EXT_OP_PREFIX);
109 acpigen_emit_byte(op);
110}
111
Duncan Laurie9ccae752016-05-09 07:43:19 -0700112void acpigen_emit_word(unsigned int data)
113{
114 acpigen_emit_byte(data & 0xff);
115 acpigen_emit_byte((data >> 8) & 0xff);
116}
117
118void acpigen_emit_dword(unsigned int data)
119{
120 acpigen_emit_byte(data & 0xff);
121 acpigen_emit_byte((data >> 8) & 0xff);
122 acpigen_emit_byte((data >> 16) & 0xff);
123 acpigen_emit_byte((data >> 24) & 0xff);
124}
125
Duncan Laurie85d80272016-07-02 19:53:54 -0700126char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000127{
Duncan Laurie85d80272016-07-02 19:53:54 -0700128 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700129 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100130 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -0700131 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +0000132 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -0700133 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000134}
135
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100136void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000137{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700138 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000139 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000140}
141
Duncan Laurie9ccae752016-05-09 07:43:19 -0700142void acpigen_write_word(unsigned int data)
143{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700144 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700145 acpigen_emit_word(data);
146}
147
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100148void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000149{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700150 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700151 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000152}
153
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100154void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000155{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700156 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700157 acpigen_emit_dword(data & 0xffffffff);
158 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000159}
160
Duncan Laurief7c38762016-05-09 08:20:38 -0700161void acpigen_write_zero(void)
162{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700163 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700164}
165
166void acpigen_write_one(void)
167{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700168 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700169}
170
171void acpigen_write_ones(void)
172{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700173 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700174}
175
176void acpigen_write_integer(uint64_t data)
177{
178 if (data == 0)
179 acpigen_write_zero();
180 else if (data == 1)
181 acpigen_write_one();
182 else if (data <= 0xff)
183 acpigen_write_byte((unsigned char)data);
184 else if (data <= 0xffff)
185 acpigen_write_word((unsigned int)data);
186 else if (data <= 0xffffffff)
187 acpigen_write_dword((unsigned int)data);
188 else
189 acpigen_write_qword(data);
190}
191
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100192void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000193{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100194 acpigen_write_name(name);
195 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000196}
197
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100198void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000199{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100200 acpigen_write_name(name);
201 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000202}
203
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100204void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000205{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100206 acpigen_write_name(name);
207 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000208}
209
Duncan Laurief7c38762016-05-09 08:20:38 -0700210void acpigen_write_name_integer(const char *name, uint64_t val)
211{
212 acpigen_write_name(name);
213 acpigen_write_integer(val);
214}
215
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700216void acpigen_write_name_string(const char *name, const char *string)
217{
218 acpigen_write_name(name);
219 acpigen_write_string(string);
220}
221
Patrick Rudolph389c8272019-12-17 13:54:41 +0100222void acpigen_write_name_unicode(const char *name, const char *string)
223{
224 const size_t len = strlen(string) + 1;
225 acpigen_write_name(name);
226 acpigen_emit_byte(BUFFER_OP);
227 acpigen_write_len_f();
Matt DeVillierd4d40c62023-11-09 15:04:58 -0600228 acpigen_write_integer(2 * len);
Patrick Rudolph389c8272019-12-17 13:54:41 +0100229 for (size_t i = 0; i < len; i++) {
Arthur Heymans62ea7a82023-06-22 20:39:31 +0200230 const signed char c = string[i];
Patrick Rudolph389c8272019-12-17 13:54:41 +0100231 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
232 acpigen_emit_word(c >= 0 ? c : '?');
233 }
234 acpigen_pop_len();
235}
236
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100237void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000238{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000239 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700240 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000241 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000242}
243
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700244void acpigen_emit_string(const char *string)
245{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200246 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700247 acpigen_emit_byte('\0'); /* NUL */
248}
249
250void acpigen_write_string(const char *string)
251{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700252 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700253 acpigen_emit_string(string);
254}
255
Duncan Laurie37319032016-05-26 12:47:05 -0700256void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
257{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800258 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700259
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700260 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700261 acpigen_write_name_string("_HID", hid);
262}
263
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100264/*
265 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600266 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
267 * and "By convention, when an ASL compiler pads a name shorter than 4
268 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100269 *
270 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
271 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000272
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700273static void acpigen_emit_simple_namestring(const char *name)
274{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100275 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000276 char ud[] = "____";
277 for (i = 0; i < 4; i++) {
278 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100279 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000280 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000281 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700282 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000283 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000284}
285
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700286static void acpigen_emit_double_namestring(const char *name, int dotpos)
287{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700288 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100289 acpigen_emit_simple_namestring(name);
290 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000291}
292
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700293static void acpigen_emit_multi_namestring(const char *name)
294{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100295 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000296 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700297 acpigen_emit_byte(MULTI_NAME_PREFIX);
298 acpigen_emit_byte(ZERO_OP);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100299 pathlen = ((unsigned char *)acpigen_get_current()) - 1;
Rudolf Marek3310d752009-06-21 20:26:13 +0000300
301 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100302 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000303 /* find end or next entity */
304 while ((name[0] != '.') && (name[0] != '\0'))
305 name++;
306 /* forward to next */
307 if (name[0] == '.')
308 name++;
309 count++;
310 }
311
312 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000313}
314
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700315void acpigen_emit_namestring(const char *namepath)
316{
Rudolf Marek3310d752009-06-21 20:26:13 +0000317 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000318 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000319
Ronak Kanabar7c0f0072020-11-30 15:40:51 +0530320 /* Check for NULL pointer */
321 if (!namepath)
322 return;
323
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600324 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000325 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100326 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000327 namepath++;
328 }
329
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600330 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000331 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100332 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000333 namepath++;
334 }
335
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200336 /* If we have only \\ or only ^...^. Then we need to put a null
337 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200338 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700339 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100340 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200341 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000342
343 i = 0;
344 while (namepath[i] != '\0') {
345 if (namepath[i] == '.') {
346 dotcount++;
347 dotpos = i;
348 }
349 i++;
350 }
351
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700352 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100353 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700354 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100355 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700356 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100357 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000358}
359
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100360void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000361{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700362 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100363 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000364}
365
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100366void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000367{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700368 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100369 acpigen_write_len_f();
370 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000371}
Rudolf Marekf997b552009-02-14 15:40:23 +0000372
Duncan Laurie2fe74712020-06-01 17:36:56 -0700373void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
374{
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800375 /* <dest_op> = DeRefOf (<package_op>[<element>]) */
Duncan Laurie2fe74712020-06-01 17:36:56 -0700376 acpigen_write_store();
377 acpigen_emit_byte(DEREF_OP);
378 acpigen_emit_byte(INDEX_OP);
379 acpigen_emit_byte(package_op);
380 acpigen_write_integer(element);
381 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
382 acpigen_emit_byte(dest_op);
383}
384
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800385void acpigen_set_package_op_element_int(uint8_t package_op, unsigned int element, uint64_t src)
386{
387 /* DeRefOf (<package>[<element>]) = <src> */
388 acpigen_write_store();
389 acpigen_write_integer(src);
390 acpigen_emit_byte(DEREF_OP);
391 acpigen_emit_byte(INDEX_OP);
392 acpigen_emit_byte(package_op);
393 acpigen_write_integer(element);
394 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
395}
396
397void acpigen_get_package_element(const char *package, unsigned int element, uint8_t dest_op)
398{
399 /* <dest_op> = <package>[<element>] */
400 acpigen_write_store();
401 acpigen_emit_byte(INDEX_OP);
402 acpigen_emit_namestring(package);
403 acpigen_write_integer(element);
404 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
405 acpigen_emit_byte(dest_op);
406}
407
408void acpigen_set_package_element_int(const char *package, unsigned int element, uint64_t src)
409{
410 /* <package>[<element>] = <src> */
411 acpigen_write_store();
412 acpigen_write_integer(src);
413 acpigen_emit_byte(INDEX_OP);
414 acpigen_emit_namestring(package);
415 acpigen_write_integer(element);
416 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
417}
418
419void acpigen_set_package_element_namestr(const char *package, unsigned int element,
420 const char *src)
421{
422 /* <package>[<element>] = <src> */
423 acpigen_write_store();
424 acpigen_emit_namestring(src);
425 acpigen_emit_byte(INDEX_OP);
426 acpigen_emit_namestring(package);
427 acpigen_write_integer(element);
428 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
429}
430
Felix Heldb57b12f2023-01-24 19:31:00 +0100431void acpigen_write_processor_namestring(unsigned int cpu_index)
432{
433 char buffer[16];
Felix Heldf0a8b042023-05-12 15:55:06 +0200434 snprintf(buffer, sizeof(buffer), "\\_SB." CONFIG_ACPI_CPU_STRING, cpu_index);
Felix Heldb57b12f2023-01-24 19:31:00 +0100435 acpigen_emit_namestring(buffer);
436}
437
Elyes Haouas9c824912023-01-28 09:13:17 +0100438/* Processor() operator is deprecated as of ACPI 6.0, use Device() instead. */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100439void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000440{
441/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100442 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200443 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000444*/
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700445 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100446 acpigen_write_len_f();
Felix Heldb57b12f2023-01-24 19:31:00 +0100447 acpigen_write_processor_namestring(cpuindex);
Rudolf Marekf997b552009-02-14 15:40:23 +0000448 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700449 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000450 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000451}
452
Felix Held32bba182023-01-28 03:37:21 +0100453void acpigen_write_processor_device(unsigned int cpu_index)
454{
455 acpigen_emit_ext_op(DEVICE_OP);
456 acpigen_write_len_f();
457 acpigen_write_processor_namestring(cpu_index);
458 acpigen_write_name_string("_HID", "ACPI0007");
459 acpigen_write_name_integer("_UID", cpu_index);
460}
461
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100462void acpigen_write_processor_package(const char *const name, const unsigned int first_core,
Nico Huber4d211ac2017-09-01 21:14:36 +0200463 const unsigned int core_count)
464{
465 unsigned int i;
Nico Huber4d211ac2017-09-01 21:14:36 +0200466
467 acpigen_write_name(name);
468 acpigen_write_package(core_count);
Felix Heldb57b12f2023-01-24 19:31:00 +0100469
470 for (i = first_core; i < first_core + core_count; ++i)
471 acpigen_write_processor_namestring(i);
472
Nico Huber4d211ac2017-09-01 21:14:36 +0200473 acpigen_pop_len();
474}
475
Arthur Heymans8f055272018-11-28 13:18:57 +0100476/* Method to notify all CPU cores */
477void acpigen_write_processor_cnot(const unsigned int number_of_cores)
478{
479 int core_id;
480
Christian Walterbe3979c2019-12-18 15:07:59 +0100481 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100482 for (core_id = 0; core_id < number_of_cores; core_id++) {
Arthur Heymans8f055272018-11-28 13:18:57 +0100483 acpigen_emit_byte(NOTIFY_OP);
Felix Heldb57b12f2023-01-24 19:31:00 +0100484 acpigen_write_processor_namestring(core_id);
Arthur Heymans8f055272018-11-28 13:18:57 +0100485 acpigen_emit_byte(ARG0_OP);
486 }
487 acpigen_pop_len();
488}
489
Naresh G Solanki8535c662016-10-25 00:51:24 +0530490/*
491 * Generate ACPI AML code for OperationRegion
492 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
493 * where rname is region name, space is region space, offset is region offset &
494 * len is region length.
495 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
496 */
Duncan Laurie35029602020-10-09 17:50:25 +0000497void acpigen_write_opregion(const struct opregion *opreg)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530498{
499 /* OpregionOp */
500 acpigen_emit_ext_op(OPREGION_OP);
501 /* NameString 4 chars only */
502 acpigen_emit_simple_namestring(opreg->name);
503 /* RegionSpace */
504 acpigen_emit_byte(opreg->regionspace);
505 /* RegionOffset & RegionLen, it can be byte word or double word */
506 acpigen_write_integer(opreg->regionoffset);
507 acpigen_write_integer(opreg->regionlen);
508}
509
Patrick Rudolph32bae492019-08-08 12:47:30 +0200510/*
511 * Generate ACPI AML code for Mutex
512 * Arg0: Pointer to name of mutex
513 * Arg1: Initial value of mutex
514 */
515void acpigen_write_mutex(const char *name, const uint8_t flags)
516{
517 /* MutexOp */
518 acpigen_emit_ext_op(MUTEX_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000519 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200520 acpigen_emit_byte(flags);
521}
522
523void acpigen_write_acquire(const char *name, const uint16_t val)
524{
525 /* AcquireOp */
526 acpigen_emit_ext_op(ACQUIRE_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000527 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200528 acpigen_emit_word(val);
529}
530
531void acpigen_write_release(const char *name)
532{
533 /* ReleaseOp */
534 acpigen_emit_ext_op(RELEASE_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000535 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200536}
537
Patrick Rudolph894977b2017-07-01 16:15:05 +0200538static void acpigen_write_field_length(uint32_t len)
539{
540 uint8_t i, j;
541 uint8_t emit[4];
542
543 i = 1;
544 if (len < 0x40) {
545 emit[0] = len & 0x3F;
546 } else {
547 emit[0] = len & 0xF;
548 len >>= 4;
549 while (len) {
550 emit[i] = len & 0xFF;
551 i++;
552 len >>= 8;
553 }
554 }
555 /* Update bit 7:6 : Number of bytes followed by emit[0] */
556 emit[0] |= (i - 1) << 6;
557
558 for (j = 0; j < i; j++)
559 acpigen_emit_byte(emit[j]);
560}
561
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100562static void acpigen_write_field_offset(uint32_t offset, uint32_t current_bit_pos)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530563{
564 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530565
566 if (offset < current_bit_pos) {
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100567 printk(BIOS_WARNING, "%s: Cannot move offset backward", __func__);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530568 return;
569 }
570
571 diff_bits = offset - current_bit_pos;
572 /* Upper limit */
573 if (diff_bits > 0xFFFFFFF) {
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100574 printk(BIOS_WARNING, "%s: Offset very large to encode", __func__);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530575 return;
576 }
577
Naresh G Solanki8535c662016-10-25 00:51:24 +0530578 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200579 acpigen_write_field_length(diff_bits);
580}
581
Michael Niewöhner060dc7b2022-05-13 00:04:19 +0200582void acpigen_write_field_name(const char *name, uint32_t size)
Patrick Rudolph894977b2017-07-01 16:15:05 +0200583{
584 acpigen_emit_simple_namestring(name);
585 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530586}
587
Duncan Laurie095bbf92020-09-30 23:09:29 +0000588static void acpigen_write_field_reserved(uint32_t size)
589{
590 acpigen_emit_byte(0);
591 acpigen_write_field_length(size);
592}
593
Naresh G Solanki8535c662016-10-25 00:51:24 +0530594/*
595 * Generate ACPI AML code for Field
596 * Arg0: region name
597 * Arg1: Pointer to struct fieldlist.
598 * Arg2: no. of entries in Arg1
599 * Arg3: flags which indicate filed access type, lock rule & update rule.
600 * Example with fieldlist
601 * struct fieldlist l[] = {
602 * FIELDLIST_OFFSET(0x84),
603 * FIELDLIST_NAMESTR("PMCS", 2),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000604 * FIELDLIST_RESERVED(6),
Naresh G Solanki8535c662016-10-25 00:51:24 +0530605 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200606 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530607 * FIELD_PRESERVE);
608 * Output:
609 * Field (UART, AnyAcc, NoLock, Preserve)
610 * {
611 * Offset (0x84),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000612 * PMCS, 2,
613 * , 6,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530614 * }
615 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700616void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530617 uint8_t flags)
618{
619 uint16_t i;
620 uint32_t current_bit_pos = 0;
621
622 /* FieldOp */
623 acpigen_emit_ext_op(FIELD_OP);
624 /* Package Length */
625 acpigen_write_len_f();
626 /* NameString 4 chars only */
627 acpigen_emit_simple_namestring(name);
628 /* Field Flag */
629 acpigen_emit_byte(flags);
630
631 for (i = 0; i < count; i++) {
632 switch (l[i].type) {
633 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200634 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530635 current_bit_pos += l[i].bits;
636 break;
Duncan Laurie095bbf92020-09-30 23:09:29 +0000637 case RESERVED:
638 acpigen_write_field_reserved(l[i].bits);
639 current_bit_pos += l[i].bits;
640 break;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530641 case OFFSET:
642 acpigen_write_field_offset(l[i].bits, current_bit_pos);
643 current_bit_pos = l[i].bits;
644 break;
645 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100646 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530647 break;
648 }
649 }
650 acpigen_pop_len();
651}
652
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200653/*
654 * Generate ACPI AML code for IndexField
655 * Arg0: region name
656 * Arg1: Pointer to struct fieldlist.
657 * Arg2: no. of entries in Arg1
658 * Arg3: flags which indicate filed access type, lock rule & update rule.
659 * Example with fieldlist
660 * struct fieldlist l[] = {
661 * FIELDLIST_OFFSET(0x84),
662 * FIELDLIST_NAMESTR("PMCS", 2),
663 * };
664 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
665 * FIELD_NOLOCK |
666 * FIELD_PRESERVE);
667 * Output:
668 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
669 * {
670 * Offset (0x84),
671 * PMCS, 2
672 * }
673 */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100674void acpigen_write_indexfield(const char *idx, const char *data, struct fieldlist *l,
675 size_t count, uint8_t flags)
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200676{
677 uint16_t i;
678 uint32_t current_bit_pos = 0;
679
680 /* FieldOp */
681 acpigen_emit_ext_op(INDEX_FIELD_OP);
682 /* Package Length */
683 acpigen_write_len_f();
684 /* NameString 4 chars only */
685 acpigen_emit_simple_namestring(idx);
686 /* NameString 4 chars only */
687 acpigen_emit_simple_namestring(data);
688 /* Field Flag */
689 acpigen_emit_byte(flags);
690
691 for (i = 0; i < count; i++) {
692 switch (l[i].type) {
693 case NAME_STRING:
694 acpigen_write_field_name(l[i].name, l[i].bits);
695 current_bit_pos += l[i].bits;
696 break;
697 case OFFSET:
698 acpigen_write_field_offset(l[i].bits, current_bit_pos);
699 current_bit_pos = l[i].bits;
700 break;
701 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100702 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type);
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200703 break;
704 }
705 }
706 acpigen_pop_len();
707}
708
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100709void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000710{
711/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200712 Name (_PCT, Package (0x02)
713 {
714 ResourceTemplate ()
715 {
716 Register (FFixedHW,
717 0x00, // Bit Width
718 0x00, // Bit Offset
719 0x0000000000000000, // Address
720 ,)
721 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000722
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200723 ResourceTemplate ()
724 {
725 Register (FFixedHW,
726 0x00, // Bit Width
727 0x00, // Bit Offset
728 0x0000000000000000, // Address
729 ,)
730 }
731 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000732*/
733 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700734 /* 00000030 "0._PCT.," */
735 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
736 /* 00000038 "........" */
737 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
738 /* 00000040 "........" */
739 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
740 /* 00000048 "....y..." */
741 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
742 /* 00000050 "........" */
743 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
744 /* 00000058 "........" */
745 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000746 0x00, 0x79, 0x00
747 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100748 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000749}
750
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300751void acpigen_write_PTC(uint8_t duty_width, uint8_t duty_offset, uint16_t p_cnt)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200752{
753/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200754 Name (_PTC, Package (0x02)
755 {
756 ResourceTemplate ()
757 {
758 Register (FFixedHW,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300759 0x00, // Duty Width
760 0x00, // Duty Offset
761 0x0000000000000000, // P_CNT IO Address
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200762 ,)
763 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200764
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200765 ResourceTemplate ()
766 {
767 Register (FFixedHW,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300768 0x00, // Duty Width
769 0x00, // Duty Offset
770 0x0000000000000000, // P_CNT IO Address
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200771 ,)
772 }
773 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200774*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200775 acpi_addr_t addr = {
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300776 .bit_width = duty_width,
777 .bit_offset = duty_offset,
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +0200778 .access_size = ACPI_ACCESS_SIZE_UNDEFINED,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300779 .addrl = p_cnt,
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100780 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200781 };
782
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300783 if (addr.addrl != 0)
784 addr.space_id = ACPI_ADDRESS_SPACE_IO;
785 else
786 addr.space_id = ACPI_ADDRESS_SPACE_FIXED;
787
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100788 acpigen_write_name("_PTC");
789 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200790
791 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700792 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200793
794 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700795 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200796
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100797 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200798}
799
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300800void acpigen_write_empty_PTC(void)
801{
802 acpigen_write_PTC(0, 0, 0);
803}
804
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700805static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100806{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700807 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100808 acpigen_write_len_f();
809 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700810 acpigen_emit_byte(flags);
811}
812
813/* Method (name, nargs, NotSerialized) */
814void acpigen_write_method(const char *name, int nargs)
815{
816 __acpigen_write_method(name, (nargs & 7));
817}
818
819/* Method (name, nargs, Serialized) */
820void acpigen_write_method_serialized(const char *name, int nargs)
821{
822 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100823}
824
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100825void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100826{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700827 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100828 acpigen_write_len_f();
829 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100830}
831
Raul E Rangel052c9632021-05-12 17:01:30 -0600832void acpigen_write_thermal_zone(const char *name)
833{
834 acpigen_emit_ext_op(THERMAL_ZONE_OP);
835 acpigen_write_len_f();
836 acpigen_emit_namestring(name);
837}
838
Duncan Laurieabe2de82016-05-09 11:08:46 -0700839void acpigen_write_STA(uint8_t status)
840{
841 /*
842 * Method (_STA, 0, NotSerialized) { Return (status) }
843 */
844 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700845 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700846 acpigen_write_byte(status);
847 acpigen_pop_len();
848}
849
Sugnan Prabhu S4f77f612020-06-10 09:51:02 +0530850void acpigen_write_STA_ext(const char *namestring)
851{
852 /*
853 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
854 */
855 acpigen_write_method("_STA", 0);
856 acpigen_emit_byte(RETURN_OP);
857 acpigen_emit_namestring(namestring);
858 acpigen_pop_len();
859}
860
Felix Held2d4112f2023-05-04 23:00:06 +0200861void acpigen_write_BBN(uint8_t base_bus_number)
862{
863 /*
864 * Method (_BBN, 0, NotSerialized) { Return (status) }
865 */
866 acpigen_write_method("_BBN", 0);
867 acpigen_emit_byte(RETURN_OP);
868 acpigen_write_byte(base_bus_number);
869 acpigen_pop_len();
870}
871
Felix Held75c4d442024-01-11 23:57:38 +0100872void acpigen_write_SEG(uint8_t segment_group_number)
873{
874 /*
875 * Method (_SEG, 0, NotSerialized) { Return (status) }
876 */
877 acpigen_write_method("_SEG", 0);
878 acpigen_emit_byte(RETURN_OP);
879 acpigen_write_byte(segment_group_number);
880 acpigen_pop_len();
881}
882
Raul E Rangelc7048322021-04-19 15:58:25 -0600883void acpigen_write_LPI_package(u64 level, const struct acpi_lpi_state *states, u16 nentries)
884{
885 /*
886 * Name (_LPI, Package (0x06) // _LPI: Low Power Idle States
887 * {
888 * 0x0000,
889 * 0x0000000000000000,
890 * 0x0003,
891 * Package (0x0A)
892 * {
893 * 0x00000002,
894 * 0x00000001,
895 * 0x00000001,
896 * 0x00000000,
897 * 0x00000000,
898 * 0x00000000,
899 * ResourceTemplate ()
900 * {
901 * Register (FFixedHW,
902 * 0x02, // Bit Width
903 * 0x02, // Bit Offset
904 * 0x0000000000000000, // Address
905 * ,)
906 * },
907 *
908 * ResourceTemplate ()
909 * {
910 * Register (SystemMemory,
911 * 0x00, // Bit Width
912 * 0x00, // Bit Offset
913 * 0x0000000000000000, // Address
914 * ,)
915 * },
916 *
917 * ResourceTemplate ()
918 * {
919 * Register (SystemMemory,
920 * 0x00, // Bit Width
921 * 0x00, // Bit Offset
922 * 0x0000000000000000, // Address
923 * ,)
924 * },
925 *
926 * "C1"
927 * },
928 * ...
929 * }
930 */
931
932 acpigen_write_name("_LPI");
933 acpigen_write_package(3 + nentries);
934 acpigen_write_word(0); /* Revision */
935 acpigen_write_qword(level);
936 acpigen_write_word(nentries);
937
938 for (size_t i = 0; i < nentries; i++, states++) {
939 acpigen_write_package(0xA);
940 acpigen_write_dword(states->min_residency_us);
941 acpigen_write_dword(states->worst_case_wakeup_latency_us);
942 acpigen_write_dword(states->flags);
943 acpigen_write_dword(states->arch_context_lost_flags);
944 acpigen_write_dword(states->residency_counter_frequency_hz);
945 acpigen_write_dword(states->enabled_parent_state);
946 acpigen_write_register_resource(&states->entry_method);
947 acpigen_write_register_resource(&states->residency_counter_register);
948 acpigen_write_register_resource(&states->usage_counter_register);
949 acpigen_write_string(states->state_name);
950 acpigen_pop_len();
951 }
952 acpigen_pop_len();
953}
954
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100955/*
956 * Generates a func with max supported P-states.
957 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100958void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000959{
960/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200961 Method (_PPC, 0, NotSerialized)
962 {
963 Return (nr)
964 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000965*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100966 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700967 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000968 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100969 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100970 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000971}
972
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100973/*
974 * Generates a func with max supported P-states saved
975 * in the variable PPCM.
976 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100977void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700978{
979/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200980 Method (_PPC, 0, NotSerialized)
981 {
982 Return (PPCM)
983 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700984*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100985 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700986 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700987 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100988 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100989 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700990}
991
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100992void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200993{
994/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200995 // Sample _TPC method
996 Method (_TPC, 0, NotSerialized)
997 {
998 Return (\TLVL)
999 }
1000*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001001 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001002 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001003 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001004 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001005}
1006
Duncan Laurieabe2de82016-05-09 11:08:46 -07001007void acpigen_write_PRW(u32 wake, u32 level)
1008{
1009 /*
1010 * Name (_PRW, Package () { wake, level }
1011 */
1012 acpigen_write_name("_PRW");
1013 acpigen_write_package(2);
1014 acpigen_write_integer(wake);
1015 acpigen_write_integer(level);
1016 acpigen_pop_len();
1017}
1018
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001019void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat, u32 busmLat, u32 control,
1020 u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +00001021{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001022 acpigen_write_package(6);
1023 acpigen_write_dword(coreFreq);
1024 acpigen_write_dword(power);
1025 acpigen_write_dword(transLat);
1026 acpigen_write_dword(busmLat);
1027 acpigen_write_dword(control);
1028 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001029 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -07001030
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001031 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n", coreFreq, power,
1032 control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +00001033}
Patrick Georgidf444bf2009-04-21 20:34:36 +00001034
Jason Gleneskca36aed2020-09-15 21:01:57 -07001035void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
1036{
1037 size_t pstate;
1038
1039 acpigen_write_name("_PSS");
1040 acpigen_write_package(nentries);
1041 for (pstate = 0; pstate < nentries; pstate++) {
1042 acpigen_write_PSS_package(
1043 pstate_values->core_freq, pstate_values->power,
1044 pstate_values->transition_latency, pstate_values->bus_master_latency,
1045 pstate_values->control_value, pstate_values->status_value);
1046 pstate_values++;
1047 }
1048
1049 acpigen_pop_len();
1050}
1051
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001052void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +00001053{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001054 acpigen_write_name("_PSD");
1055 acpigen_write_package(1);
1056 acpigen_write_package(5);
1057 acpigen_write_byte(5); // 5 values
1058 acpigen_write_byte(0); // revision 0
1059 acpigen_write_dword(domain);
1060 acpigen_write_dword(coordtype);
1061 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001062 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001063 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +00001064}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001065
Angel Ponsd2794ce2021-10-17 12:59:43 +02001066void acpigen_write_CST_package_entry(const acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001067{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001068 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001069 acpigen_write_register_resource(&cstate->resource);
Jason Glenesk201acca2020-09-11 12:36:15 -07001070 acpigen_write_byte(cstate->ctype);
1071 acpigen_write_word(cstate->latency);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001072 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001073 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001074}
1075
Angel Ponsd2794ce2021-10-17 12:59:43 +02001076void acpigen_write_CST_package(const acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001077{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001078 int i;
1079 acpigen_write_name("_CST");
1080 acpigen_write_package(nentries+1);
Jason Glenesk201acca2020-09-11 12:36:15 -07001081 acpigen_write_integer(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001082
1083 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001084 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001085
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001086 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001087}
1088
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001089void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
1090 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -05001091{
1092 acpigen_write_name("_CSD");
1093 acpigen_write_package(1);
1094 acpigen_write_package(6);
Jason Glenesk201acca2020-09-11 12:36:15 -07001095 acpigen_write_integer(6); // 6 values
Timothy Pearson83abd812015-06-08 19:35:06 -05001096 acpigen_write_byte(0); // revision 0
1097 acpigen_write_dword(domain);
1098 acpigen_write_dword(coordtype);
1099 acpigen_write_dword(numprocs);
1100 acpigen_write_dword(index);
1101 acpigen_pop_len();
1102 acpigen_pop_len();
1103}
1104
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001105void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001106{
1107/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +02001108 Sample _TSS package with 100% and 50% duty cycles
1109 Name (_TSS, Package (0x02)
1110 {
1111 Package(){100, 1000, 0, 0x00, 0)
1112 Package(){50, 520, 0, 0x18, 0)
1113 })
1114*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001115 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001116 acpi_tstate_t *tstate = tstate_list;
1117
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001118 acpigen_write_name("_TSS");
1119 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +02001120
1121 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001122 acpigen_write_package(5);
1123 acpigen_write_dword(tstate->percent);
1124 acpigen_write_dword(tstate->power);
1125 acpigen_write_dword(tstate->latency);
1126 acpigen_write_dword(tstate->control);
1127 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001128 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001129 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001130 }
1131
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001132 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001133}
1134
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001135void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001136{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001137 acpigen_write_name("_TSD");
1138 acpigen_write_package(1);
1139 acpigen_write_package(5);
1140 acpigen_write_byte(5); // 5 values
1141 acpigen_write_byte(0); // revision 0
1142 acpigen_write_dword(domain);
1143 acpigen_write_dword(coordtype);
1144 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001145 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001146 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001147}
1148
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001149void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001150{
1151 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001152 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001153 * Byte 0:
1154 * Bit7 : 1 => big item
1155 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
1156 */
1157 acpigen_emit_byte(0x86);
1158 /* Byte 1+2: length (0x0009) */
1159 acpigen_emit_byte(0x09);
1160 acpigen_emit_byte(0x00);
1161 /* bit1-7 are ignored */
1162 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -07001163 acpigen_emit_dword(base);
1164 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001165}
1166
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001167static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001168{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +02001169 acpigen_emit_byte(0x82); /* Register Descriptor */
1170 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
1171 acpigen_emit_byte(0x00); /* Register Length 15:8 */
1172 acpigen_emit_byte(addr->space_id); /* Address Space ID */
1173 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
1174 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +01001175 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -07001176 acpigen_emit_dword(addr->addrl); /* Register Address Low */
1177 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +02001178}
1179
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001180void acpigen_write_register_resource(const acpi_addr_t *addr)
1181{
1182 acpigen_write_resourcetemplate_header();
1183 acpigen_write_register(addr);
1184 acpigen_write_resourcetemplate_footer();
1185}
1186
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001187void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001188{
1189 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001190 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001191 * Byte 0:
1192 * Bit7 : 0 => small item
1193 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1194 * Bit2-0: 010 (0x2) => 2 Bytes long
1195 */
1196 acpigen_emit_byte(0x22);
1197 acpigen_emit_byte(mask & 0xff);
1198 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001199}
1200
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001201void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001202{
1203 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001204 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001205 * Byte 0:
1206 * Bit7 : 0 => small item
1207 * Bit6-3: 1000 (0x8) => I/O port descriptor
1208 * Bit2-0: 111 (0x7) => 7 Bytes long
1209 */
1210 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001211 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001212 /* bit1-7 are ignored */
1213 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1214 /* minimum base address the device may be configured for */
1215 acpigen_emit_byte(min & 0xff);
1216 acpigen_emit_byte((min >> 8) & 0xff);
1217 /* maximum base address the device may be configured for */
1218 acpigen_emit_byte(max & 0xff);
1219 acpigen_emit_byte((max >> 8) & 0xff);
1220 /* alignment for min base */
1221 acpigen_emit_byte(align & 0xff);
1222 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001223}
1224
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001225void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001226{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001227 /*
1228 * A ResourceTemplate() is a Buffer() with a
1229 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001230 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001231 * (small item 0xf, len 1)
1232 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001233 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001234 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001235 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001236 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001237 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001238 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001239 acpigen_emit_byte(0x00);
1240 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001241}
1242
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001243void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001244{
1245 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001246 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001247 /*
1248 * end tag (acpi 4.0 Section 6.4.2.8)
1249 * 0x79 <checksum>
1250 * 0x00 is treated as a good checksum according to the spec
1251 * and is what iasl generates.
1252 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001253 acpigen_emit_byte(0x79);
1254 acpigen_emit_byte(0x00);
1255
Nico Huber7d89ce32017-04-14 00:08:18 +02001256 /* Start counting past the 2-bytes length added in
1257 acpigen_write_resourcetemplate() above. */
1258 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001259
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001260 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001261 p[0] = len & 0xff;
1262 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001263 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001264 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001265}
1266
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001267static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev, struct resource *res)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001268{
1269 acpigen_write_mem32fixed(0, res->base, res->size);
1270}
1271
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001272static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev, struct resource *res)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001273{
1274 resource_t base = res->base;
1275 resource_t size = res->size;
1276 while (size > 0) {
1277 resource_t sz = size > 255 ? 255 : size;
1278 acpigen_write_io16(base, base, 0, sz, 1);
1279 size -= sz;
1280 base += sz;
1281 }
1282}
1283
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001284void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001285{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001286 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001287
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001288 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001289 search_global_resources(
1290 IORESOURCE_MEM | IORESOURCE_RESERVE,
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001291 IORESOURCE_MEM | IORESOURCE_RESERVE,
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001292 acpigen_add_mainboard_rsvd_mem32, 0);
1293
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001294 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001295 search_global_resources(
1296 IORESOURCE_IO | IORESOURCE_RESERVE,
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001297 IORESOURCE_IO | IORESOURCE_RESERVE,
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001298 acpigen_add_mainboard_rsvd_io, 0);
1299
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001300 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001301}
1302
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001303void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001304{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001305 acpigen_write_scope(scope);
1306 acpigen_write_name(name);
1307 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001308 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001309}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001310
1311static int hex2bin(const char c)
1312{
1313 if (c >= 'A' && c <= 'F')
1314 return c - 'A' + 10;
1315 if (c >= 'a' && c <= 'f')
1316 return c - 'a' + 10;
1317 return c - '0';
1318}
1319
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001320void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001321{
1322 u32 compact = 0;
1323
1324 /* Clamping individual values would be better but
1325 there is a disagreement over what is a valid
1326 EISA id, so accept anything and don't clamp,
1327 parent code should create a valid EISAid.
1328 */
1329 compact |= (eisaid[0] - 'A' + 1) << 26;
1330 compact |= (eisaid[1] - 'A' + 1) << 21;
1331 compact |= (eisaid[2] - 'A' + 1) << 16;
1332 compact |= hex2bin(eisaid[3]) << 12;
1333 compact |= hex2bin(eisaid[4]) << 8;
1334 compact |= hex2bin(eisaid[5]) << 4;
1335 compact |= hex2bin(eisaid[6]);
1336
1337 acpigen_emit_byte(0xc);
1338 acpigen_emit_byte((compact >> 24) & 0xff);
1339 acpigen_emit_byte((compact >> 16) & 0xff);
1340 acpigen_emit_byte((compact >> 8) & 0xff);
1341 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001342}
Duncan Laurie3829f232016-05-09 11:08:46 -07001343
1344/*
1345 * ToUUID(uuid)
1346 *
1347 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1348 * order for the bytes that make up a UUID Buffer object.
1349 * UUID byte order for input:
1350 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1351 * UUID byte order for output:
1352 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1353 */
1354#define UUID_LEN 16
1355void acpigen_write_uuid(const char *uuid)
1356{
1357 uint8_t buf[UUID_LEN];
1358 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1359 8, 9, 10, 11, 12, 13, 14, 15 };
1360
1361 /* Parse UUID string into bytes */
1362 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1363 return;
1364
1365 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001366 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001367 acpigen_write_len_f();
1368
1369 /* Buffer length in bytes */
1370 acpigen_write_word(UUID_LEN);
1371
1372 /* Output UUID in expected order */
1373 for (i = 0; i < UUID_LEN; i++)
1374 acpigen_emit_byte(buf[order[i]]);
1375
1376 acpigen_pop_len();
1377}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001378
1379/*
1380 * Name (_PRx, Package(One) { name })
1381 * ...
1382 * PowerResource (name, level, order)
1383 */
1384void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001385 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001386{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001387 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001388 for (i = 0; i < dev_states_count; i++) {
1389 acpigen_write_name(dev_states[i]);
1390 acpigen_write_package(1);
1391 acpigen_emit_simple_namestring(name);
1392 acpigen_pop_len(); /* Package */
1393 }
1394
1395 acpigen_emit_ext_op(POWER_RES_OP);
1396
1397 acpigen_write_len_f();
1398
1399 acpigen_emit_simple_namestring(name);
1400 acpigen_emit_byte(level);
1401 acpigen_emit_word(order);
1402}
1403
1404/* Sleep (ms) */
1405void acpigen_write_sleep(uint64_t sleep_ms)
1406{
1407 acpigen_emit_ext_op(SLEEP_OP);
1408 acpigen_write_integer(sleep_ms);
1409}
1410
1411void acpigen_write_store(void)
1412{
1413 acpigen_emit_byte(STORE_OP);
1414}
1415
1416/* Store (src, dst) */
1417void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1418{
1419 acpigen_write_store();
1420 acpigen_emit_byte(src);
1421 acpigen_emit_byte(dst);
1422}
1423
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001424/* Store (src, "namestr") */
1425void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1426{
1427 acpigen_write_store();
1428 acpigen_emit_byte(src);
1429 acpigen_emit_namestring(dst);
1430}
1431
Duncan Laurie8e391d32020-09-30 23:17:41 +00001432/* Store (src, "namestr") */
1433void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst)
1434{
1435 acpigen_write_store();
1436 acpigen_write_integer(src);
1437 acpigen_emit_namestring(dst);
1438}
1439
Cliff Huang24f3dc82023-02-04 21:11:51 -08001440/* Store ("namestr", dst) */
1441void acpigen_write_store_namestr_to_op(const char *src, uint8_t dst)
1442{
1443 acpigen_write_store();
1444 acpigen_emit_namestring(src);
1445 acpigen_emit_byte(dst);
1446}
1447
Duncan Laurie8e391d32020-09-30 23:17:41 +00001448/* Store (src, dst) */
1449void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst)
1450{
1451 acpigen_write_store();
1452 acpigen_write_integer(src);
1453 acpigen_emit_byte(dst);
1454}
1455
Felix Held178cf352023-02-09 16:29:46 +01001456/* Store ("namestr", "namestr") */
1457void acpigen_write_store_namestr_to_namestr(const char *src, const char *dst)
1458{
1459 acpigen_write_store();
1460 acpigen_emit_namestring(src);
1461 acpigen_emit_namestring(dst);
1462}
1463
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001464/* Or (arg1, arg2, res) */
1465void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1466{
1467 acpigen_emit_byte(OR_OP);
1468 acpigen_emit_byte(arg1);
1469 acpigen_emit_byte(arg2);
1470 acpigen_emit_byte(res);
1471}
1472
Rajat Jain310623b2020-02-26 11:02:53 -08001473/* Xor (arg1, arg2, res) */
1474void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1475{
1476 acpigen_emit_byte(XOR_OP);
1477 acpigen_emit_byte(arg1);
1478 acpigen_emit_byte(arg2);
1479 acpigen_emit_byte(res);
1480}
1481
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001482/* And (arg1, arg2, res) */
1483void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1484{
1485 acpigen_emit_byte(AND_OP);
1486 acpigen_emit_byte(arg1);
1487 acpigen_emit_byte(arg2);
1488 acpigen_emit_byte(res);
1489}
1490
1491/* Not (arg, res) */
1492void acpigen_write_not(uint8_t arg, uint8_t res)
1493{
1494 acpigen_emit_byte(NOT_OP);
1495 acpigen_emit_byte(arg);
1496 acpigen_emit_byte(res);
1497}
1498
Cliff Huange6083082023-01-19 21:18:23 -08001499/* Concatenate (str, src_res, dest_res) */
1500void acpigen_concatenate_string_op(const char *str, uint8_t src_res, uint8_t dest_res)
1501{
1502 acpigen_emit_byte(CONCATENATE_OP);
1503 acpigen_write_string(str);
1504 acpigen_emit_byte(src_res);
1505 acpigen_emit_byte(dest_res);
1506}
1507
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001508/* Store (str, DEBUG) */
1509void acpigen_write_debug_string(const char *str)
1510{
1511 acpigen_write_store();
1512 acpigen_write_string(str);
1513 acpigen_emit_ext_op(DEBUG_OP);
1514}
1515
1516/* Store (val, DEBUG) */
1517void acpigen_write_debug_integer(uint64_t val)
1518{
1519 acpigen_write_store();
1520 acpigen_write_integer(val);
1521 acpigen_emit_ext_op(DEBUG_OP);
1522}
1523
1524/* Store (op, DEBUG) */
1525void acpigen_write_debug_op(uint8_t op)
1526{
1527 acpigen_write_store();
1528 acpigen_emit_byte(op);
1529 acpigen_emit_ext_op(DEBUG_OP);
1530}
1531
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001532/* Store (str, DEBUG) */
1533void acpigen_write_debug_namestr(const char *str)
1534{
1535 acpigen_write_store();
1536 acpigen_emit_namestring(str);
1537 acpigen_emit_ext_op(DEBUG_OP);
1538}
1539
Cliff Huange6083082023-01-19 21:18:23 -08001540/* Concatenate (str1, res, tmp_res)
1541 Store(tmp_res, DEBUG) */
1542void acpigen_write_debug_concatenate_string_op(const char *str, uint8_t res,
1543 uint8_t tmp_res)
1544{
1545 acpigen_concatenate_string_op(str, res, tmp_res);
1546 acpigen_write_debug_op(tmp_res);
1547}
1548
Cliff Huang063a1b82023-02-18 00:17:26 -08001549static void acpigen_tx_byte(unsigned char byte, void *data)
1550{
1551 acpigen_emit_byte(byte);
1552}
1553
1554/* Store("formatted string", DEBUG) */
1555void acpigen_write_debug_sprintf(const char *fmt, ...)
1556{
1557 va_list args;
1558
1559 acpigen_write_store();
1560
1561 acpigen_emit_byte(STRING_PREFIX);
1562 va_start(args, fmt);
1563 vtxprintf(acpigen_tx_byte, fmt, args, NULL);
1564 va_end(args);
1565 acpigen_emit_byte('\0');
1566
1567 acpigen_emit_ext_op(DEBUG_OP);
1568}
1569
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001570void acpigen_write_if(void)
1571{
1572 acpigen_emit_byte(IF_OP);
1573 acpigen_write_len_f();
1574}
1575
1576/* If (And (arg1, arg2)) */
1577void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1578{
1579 acpigen_write_if();
1580 acpigen_emit_byte(AND_OP);
1581 acpigen_emit_byte(arg1);
1582 acpigen_emit_byte(arg2);
1583}
1584
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001585/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001586 * Generates ACPI code for checking if operand1 and operand2 are equal.
1587 * Both operand1 and operand2 are ACPI ops.
1588 *
1589 * If (Lequal (op,1 op2))
1590 */
1591void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1592{
1593 acpigen_write_if();
1594 acpigen_emit_byte(LEQUAL_OP);
1595 acpigen_emit_byte(op1);
1596 acpigen_emit_byte(op2);
1597}
1598
1599/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001600 * Generates ACPI code for checking if operand1 is greater than operand2.
1601 * Both operand1 and operand2 are ACPI ops.
1602 *
1603 * If (Lgreater (op1 op2))
1604 */
1605void acpigen_write_if_lgreater_op_op(uint8_t op1, uint8_t op2)
1606{
1607 acpigen_write_if();
1608 acpigen_emit_byte(LGREATER_OP);
1609 acpigen_emit_byte(op1);
1610 acpigen_emit_byte(op2);
1611}
1612
1613/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001614 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1615 * operand1 is ACPI op and operand2 is an integer.
1616 *
1617 * If (Lequal (op, val))
1618 */
1619void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001620{
1621 acpigen_write_if();
1622 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001623 acpigen_emit_byte(op);
1624 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001625}
1626
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001627/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001628 * Generates ACPI code for checking if operand is greater than the value, where,
1629 * operand is ACPI op and val is an integer.
1630 *
1631 * If (Lgreater (op, val))
1632 */
1633void acpigen_write_if_lgreater_op_int(uint8_t op, uint64_t val)
1634{
1635 acpigen_write_if();
1636 acpigen_emit_byte(LGREATER_OP);
1637 acpigen_emit_byte(op);
1638 acpigen_write_integer(val);
1639}
1640
1641/*
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001642 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1643 * operand1 is namestring and operand2 is an integer.
1644 *
1645 * If (Lequal ("namestr", val))
1646 */
1647void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1648{
1649 acpigen_write_if();
1650 acpigen_emit_byte(LEQUAL_OP);
1651 acpigen_emit_namestring(namestr);
1652 acpigen_write_integer(val);
1653}
1654
Tim Wawrzynczakc4ca2f62021-07-01 11:18:50 -06001655/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001656 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1657 * operand1 is namestring and operand2 is an integer.
1658 *
1659 * If (Lgreater ("namestr", val))
1660 */
1661void acpigen_write_if_lgreater_namestr_int(const char *namestr, uint64_t val)
1662{
1663 acpigen_write_if();
1664 acpigen_emit_byte(LGREATER_OP);
1665 acpigen_emit_namestring(namestr);
1666 acpigen_write_integer(val);
1667}
1668
1669/*
Tim Wawrzynczakc4ca2f62021-07-01 11:18:50 -06001670 * Generates ACPI code to check at runtime if an object named `namestring`
1671 * exists, and leaves the If scope open to continue execute code when this
1672 * is true. NOTE: Requires matching acpigen_write_if_end().
1673 *
1674 * If (CondRefOf (NAME))
1675 */
1676void acpigen_write_if_cond_ref_of(const char *namestring)
1677{
1678 acpigen_write_if();
1679 acpigen_emit_ext_op(COND_REFOF_OP);
1680 acpigen_emit_namestring(namestring);
1681 acpigen_emit_byte(ZERO_OP); /* ignore COND_REFOF_OP destination */
1682}
1683
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001684/* Closes previously opened if statement and generates ACPI code for else statement. */
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001685void acpigen_write_else(void)
1686{
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001687 acpigen_pop_len();
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001688 acpigen_emit_byte(ELSE_OP);
1689 acpigen_write_len_f();
1690}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001691
Duncan Laurie36858202020-10-06 21:01:51 +00001692void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count)
1693{
1694 acpigen_emit_byte(SHIFT_LEFT_OP);
1695 acpigen_emit_byte(src_result);
1696 acpigen_write_integer(count);
1697 acpigen_emit_byte(ZERO_OP);
1698}
1699
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001700void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1701{
1702 acpigen_emit_byte(TO_BUFFER_OP);
1703 acpigen_emit_byte(src);
1704 acpigen_emit_byte(dst);
1705}
1706
1707void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1708{
1709 acpigen_emit_byte(TO_INTEGER_OP);
1710 acpigen_emit_byte(src);
1711 acpigen_emit_byte(dst);
1712}
1713
Aaron Durbin80bc0912020-08-19 23:17:42 -06001714void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op)
1715{
1716 acpigen_emit_byte(TO_INTEGER_OP);
1717 acpigen_emit_namestring(source);
1718 acpigen_emit_byte(dst_op);
1719}
1720
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301721void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001722{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301723 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001724
1725 acpigen_emit_byte(BUFFER_OP);
1726 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301727 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001728
1729 for (i = 0; i < size; i++)
1730 acpigen_emit_byte(arr[i]);
1731
1732 acpigen_pop_len();
1733}
1734
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301735void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001736{
1737 acpigen_emit_byte(RETURN_OP);
1738 acpigen_write_byte_buffer(arr, size);
1739}
1740
1741void acpigen_write_return_singleton_buffer(uint8_t arg)
1742{
1743 acpigen_write_return_byte_buffer(&arg, 1);
1744}
1745
Duncan Laurie2fe74712020-06-01 17:36:56 -07001746void acpigen_write_return_op(uint8_t arg)
1747{
1748 acpigen_emit_byte(RETURN_OP);
1749 acpigen_emit_byte(arg);
1750}
1751
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001752void acpigen_write_return_byte(uint8_t arg)
1753{
1754 acpigen_emit_byte(RETURN_OP);
1755 acpigen_write_byte(arg);
1756}
1757
Naresh G Solanki05abe432016-11-17 00:16:29 +05301758void acpigen_write_return_integer(uint64_t arg)
1759{
1760 acpigen_emit_byte(RETURN_OP);
1761 acpigen_write_integer(arg);
1762}
1763
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001764void acpigen_write_return_namestr(const char *arg)
1765{
1766 acpigen_emit_byte(RETURN_OP);
1767 acpigen_emit_namestring(arg);
1768}
1769
Naresh G Solanki05abe432016-11-17 00:16:29 +05301770void acpigen_write_return_string(const char *arg)
1771{
1772 acpigen_emit_byte(RETURN_OP);
1773 acpigen_write_string(arg);
1774}
1775
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001776void acpigen_write_upc(enum acpi_upc_type type)
1777{
1778 acpigen_write_name("_UPC");
1779 acpigen_write_package(4);
1780 /* Connectable */
1781 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1782 /* Type */
1783 acpigen_write_byte(type);
1784 /* Reserved0 */
1785 acpigen_write_zero();
1786 /* Reserved1 */
1787 acpigen_write_zero();
1788 acpigen_pop_len();
1789}
1790
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001791void acpigen_write_pld(const struct acpi_pld *pld)
1792{
1793 uint8_t buf[20];
1794
1795 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1796 return;
1797
1798 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001799 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001800 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001801 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001802}
1803
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001804void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *), size_t count, void *arg)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301805{
1806 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1807 acpigen_write_dsm_uuid_arr(&id, 1);
1808}
1809
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001810/*
1811 * Create a supported functions bitmask
1812 * bit 0: other functions than 0 are supported
1813 * bits 1-x: function x supported
1814 */
Arthur Heymans9c1f78d2023-06-22 21:04:06 +02001815/* On GCC aarch64 the compiler is worried about alloca() having unbounded stack usage. */
1816#if defined(__GNUC__) && !defined(__clang__)
1817#pragma GCC diagnostic ignored "-Wstack-usage="
1818#endif
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001819static void acpigen_dsm_uuid_enum_functions(const struct dsm_uuid *id)
1820{
1821 const size_t bytes = DIV_ROUND_UP(id->count, BITS_PER_BYTE);
1822 uint8_t *buffer = alloca(bytes);
1823 bool set = false;
1824 size_t cb_idx = 0;
1825
1826 memset(buffer, 0, bytes);
1827
1828 for (size_t i = 0; i < bytes; i++) {
1829 for (size_t j = 0; j < BITS_PER_BYTE; j++) {
1830 if (cb_idx >= id->count)
1831 break;
1832
1833 if (id->callbacks[cb_idx++]) {
1834 set = true;
1835 buffer[i] |= BIT(j);
1836 }
1837 }
1838 }
1839
1840 if (set)
1841 buffer[0] |= BIT(0);
1842
1843 acpigen_write_return_byte_buffer(buffer, bytes);
1844}
1845
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301846static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1847{
1848 size_t i;
1849
1850 /* If (LEqual (Local0, ToUUID(uuid))) */
1851 acpigen_write_if();
1852 acpigen_emit_byte(LEQUAL_OP);
1853 acpigen_emit_byte(LOCAL0_OP);
1854 acpigen_write_uuid(id->uuid);
1855
1856 /* ToInteger (Arg2, Local1) */
1857 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1858
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001859 /* If (LEqual(Local1, 0)) */
1860 {
1861 acpigen_write_if_lequal_op_int(LOCAL1_OP, 0);
1862 if (id->callbacks[0])
1863 id->callbacks[0](id->arg);
1864 else if (id->count)
1865 acpigen_dsm_uuid_enum_functions(id);
1866 acpigen_write_if_end();
1867 }
1868
1869 for (i = 1; i < id->count; i++) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301870 /* If (LEqual (Local1, i)) */
1871 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1872
1873 /* Callback to write if handler. */
1874 if (id->callbacks[i])
1875 id->callbacks[i](id->arg);
1876
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001877 acpigen_write_if_end(); /* If */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301878 }
1879
1880 /* Default case: Return (Buffer (One) { 0x0 }) */
1881 acpigen_write_return_singleton_buffer(0x0);
1882
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001883 acpigen_write_if_end(); /* If (LEqual (Local0, ToUUID(uuid))) */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301884}
1885
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001886/*
1887 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301888 * This function takes as input array of uuid for the device, set of callbacks
1889 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1890 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1891 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001892 *
1893 * Arguments passed into _DSM method:
1894 * Arg0 = UUID
1895 * Arg1 = Revision
1896 * Arg2 = Function index
1897 * Arg3 = Function specific arguments
1898 *
1899 * AML code generated would look like:
1900 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301901 * ToBuffer (Arg0, Local0)
1902 * If (LEqual (Local0, ToUUID(uuid))) {
1903 * ToInteger (Arg2, Local1)
1904 * If (LEqual (Local1, 0)) {
1905 * <acpigen by callback[0]>
1906 * }
1907 * ...
1908 * If (LEqual (Local1, n)) {
1909 * <acpigen by callback[n]>
1910 * }
1911 * Return (Buffer (One) { 0x0 })
1912 * }
1913 * ...
1914 * If (LEqual (Local0, ToUUID(uuidn))) {
1915 * ...
1916 * }
1917 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001918 * }
1919 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301920void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001921{
1922 size_t i;
1923
1924 /* Method (_DSM, 4, Serialized) */
1925 acpigen_write_method_serialized("_DSM", 0x4);
1926
1927 /* ToBuffer (Arg0, Local0) */
1928 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1929
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001930 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301931 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001932
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301933 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001934 acpigen_write_return_singleton_buffer(0x0);
1935
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001936 acpigen_pop_len(); /* Method _DSM */
1937}
1938
Matt Delcob425bc82018-08-13 13:36:27 -07001939void acpigen_write_CPPC_package(const struct cppc_config *config)
1940{
1941 u32 i;
1942 u32 max;
1943 switch (config->version) {
1944 case 1:
1945 max = CPPC_MAX_FIELDS_VER_1;
1946 break;
1947 case 2:
1948 max = CPPC_MAX_FIELDS_VER_2;
1949 break;
1950 case 3:
1951 max = CPPC_MAX_FIELDS_VER_3;
1952 break;
1953 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001954 printk(BIOS_ERR, "CPPC version %u is not implemented\n", config->version);
Matt Delcob425bc82018-08-13 13:36:27 -07001955 return;
1956 }
1957 acpigen_write_name(CPPC_PACKAGE_NAME);
1958
1959 /* Adding 2 to account for length and version fields */
1960 acpigen_write_package(max + 2);
1961 acpigen_write_dword(max + 2);
1962
1963 acpigen_write_byte(config->version);
1964
1965 for (i = 0; i < max; ++i) {
Michael Niewöhner38107fa2021-10-05 22:22:21 +02001966 const cppc_entry_t *entry = &config->entries[i];
1967 if (entry->type == CPPC_TYPE_DWORD)
1968 acpigen_write_dword(entry->dword);
1969 else
1970 acpigen_write_register_resource(&entry->reg);
Matt Delcob425bc82018-08-13 13:36:27 -07001971 }
1972 acpigen_pop_len();
1973}
1974
1975void acpigen_write_CPPC_method(void)
1976{
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001977 char pscope[16];
Felix Heldf0a8b042023-05-12 15:55:06 +02001978 snprintf(pscope, sizeof(pscope),
1979 "\\_SB." CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001980
Matt Delcob425bc82018-08-13 13:36:27 -07001981 acpigen_write_method("_CPC", 0);
1982 acpigen_emit_byte(RETURN_OP);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001983 acpigen_emit_namestring(pscope);
Matt Delcob425bc82018-08-13 13:36:27 -07001984 acpigen_pop_len();
1985}
1986
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001987/*
1988 * Generate ACPI AML code for _ROM method.
1989 * This function takes as input ROM data and ROM length.
1990 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001991 * The ACPI spec isn't clear about what should happen at the end of the
1992 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1993 * bytes in the returned buffer with zeros.
1994 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001995 * Arguments passed into _DSM method:
1996 * Arg0 = Offset in Bytes
1997 * Arg1 = Bytes to return
1998 *
1999 * Example:
2000 * acpigen_write_rom(0xdeadbeef, 0x10000)
2001 *
2002 * AML code generated would look like:
2003 * Method (_ROM, 2, NotSerialized) {
2004 *
2005 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
2006 * Field (ROMS, AnyAcc, NoLock, Preserve)
2007 * {
2008 * Offset (0),
2009 * RBF0, 0x80000
2010 * }
2011 *
2012 * Store (Arg0, Local0)
2013 * Store (Arg1, Local1)
2014 *
2015 * If (LGreater (Local1, 0x1000))
2016 * {
2017 * Store (0x1000, Local1)
2018 * }
2019 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002020 * Store (Local1, Local3)
2021 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002022 * If (LGreater (Local0, 0x10000))
2023 * {
2024 * Return(Buffer(Local1){0})
2025 * }
2026 *
2027 * If (LGreater (Local0, 0x0f000))
2028 * {
2029 * Subtract (0x10000, Local0, Local2)
2030 * If (LGreater (Local1, Local2))
2031 * {
2032 * Store (Local2, Local1)
2033 * }
2034 * }
2035 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002036 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002037 *
2038 * Multiply (Local0, 0x08, Local0)
2039 * Multiply (Local1, 0x08, Local1)
2040 *
2041 * CreateField (RBF0, Local0, Local1, TMPB)
2042 * Store (TMPB, ROM1)
2043 * Return (ROM1)
2044 * }
2045 */
2046
2047void acpigen_write_rom(void *bios, const size_t length)
2048{
2049 ASSERT(bios)
2050 ASSERT(length)
2051
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02002052 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06002053 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002054
2055 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002056 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY, (uintptr_t)bios, length);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002057 acpigen_write_opregion(&opreg);
2058
2059 struct fieldlist l[] = {
2060 FIELDLIST_OFFSET(0),
2061 FIELDLIST_NAMESTR("RBF0", 8 * length),
2062 };
2063
2064 /* Field (ROMS, AnyAcc, NoLock, Preserve)
2065 * {
2066 * Offset (0),
2067 * RBF0, 0x80000
2068 * } */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002069 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002070
2071 /* Store (Arg0, Local0) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002072 acpigen_write_store_ops(ARG0_OP, LOCAL0_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002073
2074 /* Store (Arg1, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002075 acpigen_write_store_ops(ARG1_OP, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002076
2077 /* ACPI SPEC requires to return at maximum 4KiB */
2078 /* If (LGreater (Local1, 0x1000)) */
Felix Held383a06e2023-02-09 16:25:38 +01002079 acpigen_write_if_lgreater_op_int(LOCAL1_OP, 0x1000);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002080
2081 /* Store (0x1000, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002082 acpigen_write_store_int_to_op(0x1000, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002083
2084 /* Pop if */
2085 acpigen_pop_len();
2086
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002087 /* Store (Local1, Local3) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002088 acpigen_write_store_ops(LOCAL1_OP, LOCAL3_OP);
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002089
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002090 /* If (LGreater (Local0, length)) */
Felix Held383a06e2023-02-09 16:25:38 +01002091 acpigen_write_if_lgreater_op_int(LOCAL0_OP, length);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002092
2093 /* Return(Buffer(Local1){0}) */
2094 acpigen_emit_byte(RETURN_OP);
2095 acpigen_emit_byte(BUFFER_OP);
2096 acpigen_write_len_f();
2097 acpigen_emit_byte(LOCAL1_OP);
2098 acpigen_emit_byte(0);
2099 acpigen_pop_len();
2100
2101 /* Pop if */
2102 acpigen_pop_len();
2103
2104 /* If (LGreater (Local0, length - 4096)) */
Felix Held383a06e2023-02-09 16:25:38 +01002105 acpigen_write_if_lgreater_op_int(LOCAL0_OP, length - 4096);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002106
2107 /* Subtract (length, Local0, Local2) */
2108 acpigen_emit_byte(SUBTRACT_OP);
2109 acpigen_write_integer(length);
2110 acpigen_emit_byte(LOCAL0_OP);
2111 acpigen_emit_byte(LOCAL2_OP);
2112
2113 /* If (LGreater (Local1, Local2)) */
Felix Held383a06e2023-02-09 16:25:38 +01002114 acpigen_write_if_lgreater_op_op(LOCAL1_OP, LOCAL2_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002115
2116 /* Store (Local2, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002117 acpigen_write_store_ops(LOCAL2_OP, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002118
2119 /* Pop if */
2120 acpigen_pop_len();
2121
2122 /* Pop if */
2123 acpigen_pop_len();
2124
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002125 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002126 acpigen_write_name("ROM1");
2127 acpigen_emit_byte(BUFFER_OP);
2128 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002129 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002130 acpigen_emit_byte(0);
2131 acpigen_pop_len();
2132
2133 /* Multiply (Local1, 0x08, Local1) */
2134 acpigen_emit_byte(MULTIPLY_OP);
2135 acpigen_emit_byte(LOCAL1_OP);
2136 acpigen_write_integer(0x08);
2137 acpigen_emit_byte(LOCAL1_OP);
2138
2139 /* Multiply (Local0, 0x08, Local0) */
2140 acpigen_emit_byte(MULTIPLY_OP);
2141 acpigen_emit_byte(LOCAL0_OP);
2142 acpigen_write_integer(0x08);
2143 acpigen_emit_byte(LOCAL0_OP);
2144
2145 /* CreateField (RBF0, Local0, Local1, TMPB) */
2146 acpigen_emit_ext_op(CREATEFIELD_OP);
2147 acpigen_emit_namestring("RBF0");
2148 acpigen_emit_byte(LOCAL0_OP);
2149 acpigen_emit_byte(LOCAL1_OP);
2150 acpigen_emit_namestring("TMPB");
2151
2152 /* Store (TMPB, ROM1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002153 acpigen_write_store_namestr_to_namestr("TMPB", "ROM1");
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002154
2155 /* Return (ROM1) */
2156 acpigen_emit_byte(RETURN_OP);
2157 acpigen_emit_namestring("ROM1");
2158
2159 /* Pop method */
2160 acpigen_pop_len();
2161}
2162
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002163/*
2164 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
2165 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
2166 * make callbacks into SoC acpigen code.
2167 *
2168 * Returns 0 on success and -1 on error.
2169 */
Duncan Laurie30c3f912020-10-09 04:48:53 +00002170int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002171{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002172 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002173 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002174 else
2175 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002176}
2177
Duncan Laurie30c3f912020-10-09 04:48:53 +00002178int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002179{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002180 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002181 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
2182 else
2183 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
2184}
Jonathan Zhang71299c22020-01-27 11:38:57 -08002185
Duncan Laurie30c3f912020-10-09 04:48:53 +00002186void acpigen_get_rx_gpio(const struct acpi_gpio *gpio)
Rajat Jain310623b2020-02-26 11:02:53 -08002187{
2188 acpigen_soc_read_rx_gpio(gpio->pins[0]);
2189
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002190 if (gpio->active_low)
Rajat Jain310623b2020-02-26 11:02:53 -08002191 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2192}
2193
Duncan Laurie30c3f912020-10-09 04:48:53 +00002194void acpigen_get_tx_gpio(const struct acpi_gpio *gpio)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002195{
2196 acpigen_soc_get_tx_gpio(gpio->pins[0]);
2197
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002198 if (gpio->active_low)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002199 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2200}
2201
Jonathan Zhang71299c22020-01-27 11:38:57 -08002202/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002203void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran, u16 range_min,
2204 u16 range_max, u16 translation, u16 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002205{
Felix Heldb2f2b532023-05-11 22:22:06 +02002206 /* Byte 0: Type 1, Large Item Value 0x8: Word Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002207 acpigen_emit_byte(0x88);
2208 /* Byte 1+2: length (0x000d) */
2209 acpigen_emit_byte(0x0d);
2210 acpigen_emit_byte(0x00);
2211 /* resource type */
2212 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2213 /* general flags */
2214 acpigen_emit_byte(gen_flags);
2215 /* type flags */
2216 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2217 acpigen_emit_byte(type_flags);
2218 /* granularity, min, max, translation, length */
2219 acpigen_emit_word(gran);
2220 acpigen_emit_word(range_min);
2221 acpigen_emit_word(range_max);
2222 acpigen_emit_word(translation);
2223 acpigen_emit_word(length);
2224}
2225
2226/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002227void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags, u32 gran,
2228 u32 range_min, u32 range_max, u32 translation, u32 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002229{
Felix Heldb2f2b532023-05-11 22:22:06 +02002230 /* Byte 0: Type 1, Large Item Value 0x7: DWord Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002231 acpigen_emit_byte(0x87);
2232 /* Byte 1+2: length (0023) */
2233 acpigen_emit_byte(23);
2234 acpigen_emit_byte(0x00);
2235 /* resource type */
2236 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2237 /* general flags */
2238 acpigen_emit_byte(gen_flags);
2239 /* type flags */
2240 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2241 acpigen_emit_byte(type_flags);
2242 /* granularity, min, max, translation, length */
2243 acpigen_emit_dword(gran);
2244 acpigen_emit_dword(range_min);
2245 acpigen_emit_dword(range_max);
2246 acpigen_emit_dword(translation);
2247 acpigen_emit_dword(length);
2248}
2249
2250static void acpigen_emit_qword(u64 data)
2251{
2252 acpigen_emit_dword(data & 0xffffffff);
2253 acpigen_emit_dword((data >> 32) & 0xffffffff);
2254}
2255
2256/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002257void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags, u64 gran,
2258 u64 range_min, u64 range_max, u64 translation, u64 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002259{
Felix Heldb2f2b532023-05-11 22:22:06 +02002260 /* Byte 0: Type 1, Large Item Value 0xa: QWord Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002261 acpigen_emit_byte(0x8a);
2262 /* Byte 1+2: length (0x002b) */
2263 acpigen_emit_byte(0x2b);
2264 acpigen_emit_byte(0x00);
2265 /* resource type */
2266 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2267 /* general flags */
2268 acpigen_emit_byte(gen_flags);
2269 /* type flags */
2270 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2271 acpigen_emit_byte(type_flags);
2272 /* granularity, min, max, translation, length */
2273 acpigen_emit_qword(gran);
2274 acpigen_emit_qword(range_min);
2275 acpigen_emit_qword(range_max);
2276 acpigen_emit_qword(translation);
2277 acpigen_emit_qword(length);
2278}
Furquan Shaikh1a829232020-04-23 11:11:05 -07002279
Felix Held0fdede02023-05-27 01:02:37 +02002280void acpigen_resource_producer_bus_number(u16 bus_base, u16 bus_limit)
Felix Held5a82f0d2023-05-09 16:10:35 +02002281{
2282 acpigen_resource_word(RSRC_TYPE_BUS, /* res_type */
2283 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2284 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002285 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2286 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held5a82f0d2023-05-09 16:10:35 +02002287 BUS_NUM_RANGE_RESOURCE_FLAG, /* type_flags */
2288 0, /* gran */
2289 bus_base, /* range_min */
2290 bus_limit, /* range_max */
2291 0x0, /* translation */
2292 bus_limit - bus_base + 1); /* length */
2293}
2294
Felix Held0fdede02023-05-27 01:02:37 +02002295void acpigen_resource_producer_io(u16 io_base, u16 io_limit)
Felix Held6695be62023-05-09 16:18:51 +02002296{
Felix Helde3c9a042023-06-05 19:59:25 +02002297 acpigen_resource_dword(RSRC_TYPE_IO, /* res_type */
Felix Held6695be62023-05-09 16:18:51 +02002298 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2299 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002300 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2301 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held6695be62023-05-09 16:18:51 +02002302 IO_RSRC_FLAG_ENTIRE_RANGE, /* type_flags */
2303 0, /* gran */
2304 io_base, /* range_min */
2305 io_limit, /* range_max */
2306 0x0, /* translation */
2307 io_limit - io_base + 1); /* length */
2308}
2309
Arthur Heymans62f788e2023-11-21 18:49:33 +01002310static void acpigen_resource_mmio32(u32 mmio_base, u32 mmio_limit, u16 gen_flags,
2311 u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002312{
2313 acpigen_resource_dword(RSRC_TYPE_MEM, /* res_type */
Arthur Heymans62f788e2023-11-21 18:49:33 +01002314 gen_flags, /* gen_flags */
2315 type_flags, /* type_flags */
2316 0, /* gran */
2317 mmio_base, /* range_min */
2318 mmio_limit, /* range_max */
2319 0x0, /* translation */
2320 mmio_limit - mmio_base + 1); /* length */
Felix Held21594fd12023-05-09 16:39:57 +02002321}
2322
Arthur Heymans62f788e2023-11-21 18:49:33 +01002323static void acpigen_resource_mmio64(u64 mmio_base, u64 mmio_limit, u16 gen_flags,
2324 u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002325{
2326 acpigen_resource_qword(RSRC_TYPE_MEM, /* res_type */
Arthur Heymans62f788e2023-11-21 18:49:33 +01002327 gen_flags, /* gen_flags */
2328 type_flags, /* type_flags */
2329 0, /* gran */
2330 mmio_base, /* range_min */
2331 mmio_limit, /* range_max */
2332 0x0, /* translation */
2333 mmio_limit - mmio_base + 1); /* length */
2334}
2335
2336static void acpigen_resource_mmio(u64 mmio_base, u64 mmio_limit, bool is_producer, u16 type_flags)
2337{
2338 const u16 gen_flags = ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2339 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
2340 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2341 | (is_producer ? ADDR_SPACE_GENERAL_FLAG_PRODUCER
2342 : ADDR_SPACE_GENERAL_FLAG_CONSUMER);
2343
2344 if (mmio_base < 4ULL * GiB && mmio_limit < 4ULL * GiB)
2345 acpigen_resource_mmio32(mmio_base, mmio_limit, gen_flags, type_flags);
2346 else
2347 acpigen_resource_mmio64(mmio_base, mmio_limit, gen_flags, type_flags);
Felix Held21594fd12023-05-09 16:39:57 +02002348}
2349
Felix Held0fdede02023-05-27 01:02:37 +02002350void acpigen_resource_producer_mmio(u64 mmio_base, u64 mmio_limit, u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002351{
Arthur Heymans62f788e2023-11-21 18:49:33 +01002352 acpigen_resource_mmio(mmio_base, mmio_limit, true, type_flags);
2353}
2354
2355void acpigen_resource_consumer_mmio(u64 mmio_base, u64 mmio_limit, u16 type_flags)
2356{
2357 acpigen_resource_mmio(mmio_base, mmio_limit, false, type_flags);
Felix Held21594fd12023-05-09 16:39:57 +02002358}
2359
Furquan Shaikh1a829232020-04-23 11:11:05 -07002360void acpigen_write_ADR(uint64_t adr)
2361{
2362 acpigen_write_name_qword("_ADR", adr);
2363}
2364
Duncan Laurie08a942f2020-04-29 12:13:14 -07002365/**
2366 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2367 * @address: SoundWire device address properties.
2368 *
2369 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2370 *
2371 * 63..52 - Reserved (0)
2372 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2373 * Used when a Controller has multiple master devices, each producing a
2374 * separate SoundWire Link. Set to 0 for single-link controllers.
2375 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2376 * 47..44 - SoundWire specification version that this device supports
2377 * 43..40 - Unique ID for multiple devices
2378 * 39..24 - MIPI standard manufacturer code
2379 * 23..08 - Vendor defined part ID
2380 * 07..00 - MIPI class encoding
2381 */
2382void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
2383{
2384 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
2385 (((uint64_t)address->version & 0xf) << 44) |
2386 (((uint64_t)address->unique_id & 0xf) << 40) |
2387 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
2388 (((uint64_t)address->part_id & 0xffff) << 8) |
2389 (((uint64_t)address->class & 0xff)));
2390}
Tim Wawrzynczakf6945022020-06-04 15:18:47 -06002391
2392void acpigen_notify(const char *namestr, int value)
2393{
2394 acpigen_emit_byte(NOTIFY_OP);
2395 acpigen_emit_namestring(namestr);
2396 acpigen_write_integer(value);
2397}
Aaron Durbin80bc0912020-08-19 23:17:42 -06002398
2399static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name)
2400{
2401 acpigen_emit_byte(aml_op);
2402 acpigen_emit_byte(srcop);
2403 acpigen_write_integer(byte_offset);
2404 acpigen_emit_namestring(name);
2405}
2406
2407void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name)
2408{
2409 _create_field(CREATE_BYTE_OP, op, byte_offset, name);
2410}
2411
2412void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name)
2413{
2414 _create_field(CREATE_WORD_OP, op, byte_offset, name);
2415}
2416
2417void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name)
2418{
2419 _create_field(CREATE_DWORD_OP, op, byte_offset, name);
2420}
2421
2422void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name)
2423{
2424 _create_field(CREATE_QWORD_OP, op, byte_offset, name);
2425}
Jason Gleneskca36aed2020-09-15 21:01:57 -07002426
2427void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
2428{
2429 acpigen_write_name("_PCT");
2430 acpigen_write_package(0x02);
2431 acpigen_write_register_resource(perf_ctrl);
2432 acpigen_write_register_resource(perf_sts);
2433
2434 acpigen_pop_len();
2435}
2436
2437void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value)
2438{
2439 acpigen_write_package(0x08);
2440 acpigen_write_dword(pstate_value->core_freq);
2441 acpigen_write_dword(pstate_value->power);
2442 acpigen_write_dword(pstate_value->transition_latency);
2443 acpigen_write_dword(pstate_value->bus_master_latency);
2444
2445 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t));
2446 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t));
2447 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t));
2448 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t));
2449
2450 acpigen_pop_len();
2451}
2452
2453void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
2454{
2455 size_t pstate;
2456
2457 acpigen_write_name("XPSS");
2458 acpigen_write_package(nentries);
2459 for (pstate = 0; pstate < nentries; pstate++) {
2460 acpigen_write_xpss_package(pstate_values);
2461 pstate_values++;
2462 }
2463
2464 acpigen_pop_len();
2465}
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002466
2467/* Delay up to wait_ms until provided namestr matches expected value. */
2468void acpigen_write_delay_until_namestr_int(uint32_t wait_ms, const char *name, uint64_t value)
2469{
2470 uint32_t wait_ms_segment = 1;
2471 uint32_t segments = wait_ms;
2472
Sukumar Ghorai9b3c5af2023-12-07 18:33:43 -08002473 /* Sleep in 2ms segments if delay is more than 2ms. */
2474 if (wait_ms > 2) {
2475 wait_ms_segment = 2;
2476 segments = wait_ms / wait_ms_segment;
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002477 }
2478
2479 acpigen_write_store_int_to_op(segments, LOCAL7_OP);
2480 acpigen_emit_byte(WHILE_OP);
2481 acpigen_write_len_f();
2482 acpigen_emit_byte(LGREATER_OP);
2483 acpigen_emit_byte(LOCAL7_OP);
2484 acpigen_emit_byte(ZERO_OP);
2485
2486 /* If name is not provided then just delay in a loop. */
2487 if (name) {
2488 acpigen_write_if_lequal_namestr_int(name, value);
2489 acpigen_emit_byte(BREAK_OP);
2490 acpigen_pop_len(); /* If */
2491 }
2492
2493 acpigen_write_sleep(wait_ms_segment);
2494 acpigen_emit_byte(DECREMENT_OP);
2495 acpigen_emit_byte(LOCAL7_OP);
2496 acpigen_pop_len(); /* While */
Cliff Huangf97598f2023-03-01 14:30:41 -08002497
2498 if (name) {
2499 acpigen_write_if_lequal_op_op(LOCAL7_OP, ZERO_OP);
2500 acpigen_write_debug_sprintf("WARN: Wait loop timeout for variable %s",
2501 name);
2502 acpigen_pop_len(); /* If */
2503 }
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002504}
Arthur Heymans0eb59742023-04-25 16:23:37 +02002505
2506void acpigen_ssdt_override_sleep_states(bool enable_s1, bool enable_s2, bool enable_s3,
2507 bool enable_s4)
2508{
2509 assert(!(enable_s1 && CONFIG(ACPI_S1_NOT_SUPPORTED)));
2510 assert(!(enable_s3 && !CONFIG(HAVE_ACPI_RESUME)));
2511 assert(!(enable_s4 && CONFIG(DISABLE_ACPI_HIBERNATE)));
2512
2513 acpigen_write_scope("\\");
2514 uint32_t sleep_enable = (enable_s1 << 0) | (enable_s2 << 1)
2515 | (enable_s3 << 2) | (enable_s4 << 3);
2516 acpigen_write_name_dword("OSFG", sleep_enable);
2517 acpigen_pop_len();
2518}