blob: 57f0d104886f351e9e3b9aaf2ee96db323a1d72a [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Rudolf Marek293b5f52009-02-01 18:35:15 +00002
Paul Menzel0f4c0e22013-02-22 12:33:08 +01003/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +00004#define ACPIGEN_LENSTACK_SIZE 10
5
Paul Menzel0f4c0e22013-02-22 12:33:08 +01006/*
Timothy Pearson83abd812015-06-08 19:35:06 -05007 * If you need to change this, change acpigen_write_len_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +01008 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +01009 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000010
Timothy Pearson83abd812015-06-08 19:35:06 -050011#define ACPIGEN_MAXLEN 0xfffff
Rudolf Marek293b5f52009-02-01 18:35:15 +000012
Duncan Laurie3829f232016-05-09 11:08:46 -070013#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000014#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070015#include <acpi/acpigen.h>
Elyes HAOUAScd4fe0f2019-03-29 17:12:15 +010016#include <assert.h>
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -060017#include <commonlib/helpers.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000018#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000019#include <device/device.h>
Duncan Laurie08a942f2020-04-29 12:13:14 -070020#include <device/soundwire.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010021#include <types.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000022
23static char *gencurrent;
24
25char *len_stack[ACPIGEN_LENSTACK_SIZE];
26int ltop = 0;
27
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010028void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000029{
30 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010031 len_stack[ltop++] = gencurrent;
Felix Held6ac6f6a2023-11-10 19:57:03 +010032 /* Reserve 3 bytes for PkgLength. The actual byte values will be written later in the
33 acpigen_pop_len call. */
Rudolf Marek293b5f52009-02-01 18:35:15 +000034 acpigen_emit_byte(0);
35 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050036 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000037}
38
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010039void acpigen_pop_len(void)
40{
41 int len;
42 ASSERT(ltop > 0)
43 char *p = len_stack[--ltop];
44 len = gencurrent - p;
45 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050046 /* generate store length for 0xfffff max */
47 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010048 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050049 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010050
51}
52
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000053void acpigen_set_current(char *curr)
54{
55 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000056}
57
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000058char *acpigen_get_current(void)
59{
60 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000061}
62
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010063void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000064{
65 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000066}
67
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070068void acpigen_emit_ext_op(uint8_t op)
69{
70 acpigen_emit_byte(EXT_OP_PREFIX);
71 acpigen_emit_byte(op);
72}
73
Duncan Laurie9ccae752016-05-09 07:43:19 -070074void acpigen_emit_word(unsigned int data)
75{
76 acpigen_emit_byte(data & 0xff);
77 acpigen_emit_byte((data >> 8) & 0xff);
78}
79
80void acpigen_emit_dword(unsigned int data)
81{
82 acpigen_emit_byte(data & 0xff);
83 acpigen_emit_byte((data >> 8) & 0xff);
84 acpigen_emit_byte((data >> 16) & 0xff);
85 acpigen_emit_byte((data >> 24) & 0xff);
86}
87
Duncan Laurie85d80272016-07-02 19:53:54 -070088char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000089{
Duncan Laurie85d80272016-07-02 19:53:54 -070090 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070091 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010092 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -070093 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +000094 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -070095 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +000096}
97
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010098void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000099{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700100 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000101 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000102}
103
Duncan Laurie9ccae752016-05-09 07:43:19 -0700104void acpigen_write_word(unsigned int data)
105{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700106 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700107 acpigen_emit_word(data);
108}
109
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100110void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000111{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700112 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700113 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000114}
115
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100116void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000117{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700118 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700119 acpigen_emit_dword(data & 0xffffffff);
120 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000121}
122
Duncan Laurief7c38762016-05-09 08:20:38 -0700123void acpigen_write_zero(void)
124{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700125 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700126}
127
128void acpigen_write_one(void)
129{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700130 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700131}
132
133void acpigen_write_ones(void)
134{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700135 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700136}
137
138void acpigen_write_integer(uint64_t data)
139{
140 if (data == 0)
141 acpigen_write_zero();
142 else if (data == 1)
143 acpigen_write_one();
144 else if (data <= 0xff)
145 acpigen_write_byte((unsigned char)data);
146 else if (data <= 0xffff)
147 acpigen_write_word((unsigned int)data);
148 else if (data <= 0xffffffff)
149 acpigen_write_dword((unsigned int)data);
150 else
151 acpigen_write_qword(data);
152}
153
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100154void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000155{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100156 acpigen_write_name(name);
157 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000158}
159
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100160void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000161{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100162 acpigen_write_name(name);
163 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000164}
165
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100166void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000167{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100168 acpigen_write_name(name);
169 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000170}
171
Duncan Laurief7c38762016-05-09 08:20:38 -0700172void acpigen_write_name_integer(const char *name, uint64_t val)
173{
174 acpigen_write_name(name);
175 acpigen_write_integer(val);
176}
177
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700178void acpigen_write_name_string(const char *name, const char *string)
179{
180 acpigen_write_name(name);
181 acpigen_write_string(string);
182}
183
Patrick Rudolph389c8272019-12-17 13:54:41 +0100184void acpigen_write_name_unicode(const char *name, const char *string)
185{
186 const size_t len = strlen(string) + 1;
187 acpigen_write_name(name);
188 acpigen_emit_byte(BUFFER_OP);
189 acpigen_write_len_f();
Matt DeVillierd4d40c62023-11-09 15:04:58 -0600190 acpigen_write_integer(2 * len);
Patrick Rudolph389c8272019-12-17 13:54:41 +0100191 for (size_t i = 0; i < len; i++) {
Arthur Heymans62ea7a82023-06-22 20:39:31 +0200192 const signed char c = string[i];
Patrick Rudolph389c8272019-12-17 13:54:41 +0100193 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
194 acpigen_emit_word(c >= 0 ? c : '?');
195 }
196 acpigen_pop_len();
197}
198
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100199void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000200{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000201 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700202 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000203 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000204}
205
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700206void acpigen_emit_string(const char *string)
207{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200208 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700209 acpigen_emit_byte('\0'); /* NUL */
210}
211
212void acpigen_write_string(const char *string)
213{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700214 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700215 acpigen_emit_string(string);
216}
217
Duncan Laurie37319032016-05-26 12:47:05 -0700218void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
219{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800220 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700221
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700222 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700223 acpigen_write_name_string("_HID", hid);
224}
225
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100226/*
227 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600228 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
229 * and "By convention, when an ASL compiler pads a name shorter than 4
230 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100231 *
232 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
233 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000234
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700235static void acpigen_emit_simple_namestring(const char *name)
236{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100237 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000238 char ud[] = "____";
239 for (i = 0; i < 4; i++) {
240 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100241 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000242 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000243 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700244 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000245 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000246}
247
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700248static void acpigen_emit_double_namestring(const char *name, int dotpos)
249{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700250 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100251 acpigen_emit_simple_namestring(name);
252 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000253}
254
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700255static void acpigen_emit_multi_namestring(const char *name)
256{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100257 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000258 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700259 acpigen_emit_byte(MULTI_NAME_PREFIX);
260 acpigen_emit_byte(ZERO_OP);
Elyes Haouas3141fbad2022-11-18 15:22:32 +0100261 pathlen = ((unsigned char *)acpigen_get_current()) - 1;
Rudolf Marek3310d752009-06-21 20:26:13 +0000262
263 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100264 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000265 /* find end or next entity */
266 while ((name[0] != '.') && (name[0] != '\0'))
267 name++;
268 /* forward to next */
269 if (name[0] == '.')
270 name++;
271 count++;
272 }
273
274 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000275}
276
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700277void acpigen_emit_namestring(const char *namepath)
278{
Rudolf Marek3310d752009-06-21 20:26:13 +0000279 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000280 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000281
Ronak Kanabar7c0f0072020-11-30 15:40:51 +0530282 /* Check for NULL pointer */
283 if (!namepath)
284 return;
285
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600286 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000287 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100288 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000289 namepath++;
290 }
291
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600292 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000293 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100294 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000295 namepath++;
296 }
297
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200298 /* If we have only \\ or only ^...^. Then we need to put a null
299 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200300 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700301 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100302 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200303 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000304
305 i = 0;
306 while (namepath[i] != '\0') {
307 if (namepath[i] == '.') {
308 dotcount++;
309 dotpos = i;
310 }
311 i++;
312 }
313
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700314 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100315 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700316 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100317 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700318 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100319 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000320}
321
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100322void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000323{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700324 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100325 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000326}
327
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100328void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000329{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700330 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100331 acpigen_write_len_f();
332 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000333}
Rudolf Marekf997b552009-02-14 15:40:23 +0000334
Duncan Laurie2fe74712020-06-01 17:36:56 -0700335void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
336{
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800337 /* <dest_op> = DeRefOf (<package_op>[<element>]) */
Duncan Laurie2fe74712020-06-01 17:36:56 -0700338 acpigen_write_store();
339 acpigen_emit_byte(DEREF_OP);
340 acpigen_emit_byte(INDEX_OP);
341 acpigen_emit_byte(package_op);
342 acpigen_write_integer(element);
343 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
344 acpigen_emit_byte(dest_op);
345}
346
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800347void acpigen_set_package_op_element_int(uint8_t package_op, unsigned int element, uint64_t src)
348{
349 /* DeRefOf (<package>[<element>]) = <src> */
350 acpigen_write_store();
351 acpigen_write_integer(src);
352 acpigen_emit_byte(DEREF_OP);
353 acpigen_emit_byte(INDEX_OP);
354 acpigen_emit_byte(package_op);
355 acpigen_write_integer(element);
356 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
357}
358
359void acpigen_get_package_element(const char *package, unsigned int element, uint8_t dest_op)
360{
361 /* <dest_op> = <package>[<element>] */
362 acpigen_write_store();
363 acpigen_emit_byte(INDEX_OP);
364 acpigen_emit_namestring(package);
365 acpigen_write_integer(element);
366 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
367 acpigen_emit_byte(dest_op);
368}
369
370void acpigen_set_package_element_int(const char *package, unsigned int element, uint64_t src)
371{
372 /* <package>[<element>] = <src> */
373 acpigen_write_store();
374 acpigen_write_integer(src);
375 acpigen_emit_byte(INDEX_OP);
376 acpigen_emit_namestring(package);
377 acpigen_write_integer(element);
378 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
379}
380
381void acpigen_set_package_element_namestr(const char *package, unsigned int element,
382 const char *src)
383{
384 /* <package>[<element>] = <src> */
385 acpigen_write_store();
386 acpigen_emit_namestring(src);
387 acpigen_emit_byte(INDEX_OP);
388 acpigen_emit_namestring(package);
389 acpigen_write_integer(element);
390 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
391}
392
Felix Heldb57b12f2023-01-24 19:31:00 +0100393void acpigen_write_processor_namestring(unsigned int cpu_index)
394{
395 char buffer[16];
Felix Heldf0a8b042023-05-12 15:55:06 +0200396 snprintf(buffer, sizeof(buffer), "\\_SB." CONFIG_ACPI_CPU_STRING, cpu_index);
Felix Heldb57b12f2023-01-24 19:31:00 +0100397 acpigen_emit_namestring(buffer);
398}
399
Elyes Haouas9c824912023-01-28 09:13:17 +0100400/* Processor() operator is deprecated as of ACPI 6.0, use Device() instead. */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100401void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000402{
403/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100404 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200405 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000406*/
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700407 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100408 acpigen_write_len_f();
Felix Heldb57b12f2023-01-24 19:31:00 +0100409 acpigen_write_processor_namestring(cpuindex);
Rudolf Marekf997b552009-02-14 15:40:23 +0000410 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700411 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000412 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000413}
414
Felix Held32bba182023-01-28 03:37:21 +0100415void acpigen_write_processor_device(unsigned int cpu_index)
416{
417 acpigen_emit_ext_op(DEVICE_OP);
418 acpigen_write_len_f();
419 acpigen_write_processor_namestring(cpu_index);
420 acpigen_write_name_string("_HID", "ACPI0007");
421 acpigen_write_name_integer("_UID", cpu_index);
422}
423
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100424void acpigen_write_processor_package(const char *const name, const unsigned int first_core,
Nico Huber4d211ac2017-09-01 21:14:36 +0200425 const unsigned int core_count)
426{
427 unsigned int i;
Nico Huber4d211ac2017-09-01 21:14:36 +0200428
429 acpigen_write_name(name);
430 acpigen_write_package(core_count);
Felix Heldb57b12f2023-01-24 19:31:00 +0100431
432 for (i = first_core; i < first_core + core_count; ++i)
433 acpigen_write_processor_namestring(i);
434
Nico Huber4d211ac2017-09-01 21:14:36 +0200435 acpigen_pop_len();
436}
437
Arthur Heymans8f055272018-11-28 13:18:57 +0100438/* Method to notify all CPU cores */
439void acpigen_write_processor_cnot(const unsigned int number_of_cores)
440{
441 int core_id;
442
Christian Walterbe3979c2019-12-18 15:07:59 +0100443 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100444 for (core_id = 0; core_id < number_of_cores; core_id++) {
Arthur Heymans8f055272018-11-28 13:18:57 +0100445 acpigen_emit_byte(NOTIFY_OP);
Felix Heldb57b12f2023-01-24 19:31:00 +0100446 acpigen_write_processor_namestring(core_id);
Arthur Heymans8f055272018-11-28 13:18:57 +0100447 acpigen_emit_byte(ARG0_OP);
448 }
449 acpigen_pop_len();
450}
451
Naresh G Solanki8535c662016-10-25 00:51:24 +0530452/*
453 * Generate ACPI AML code for OperationRegion
454 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
455 * where rname is region name, space is region space, offset is region offset &
456 * len is region length.
457 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
458 */
Duncan Laurie35029602020-10-09 17:50:25 +0000459void acpigen_write_opregion(const struct opregion *opreg)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530460{
461 /* OpregionOp */
462 acpigen_emit_ext_op(OPREGION_OP);
463 /* NameString 4 chars only */
464 acpigen_emit_simple_namestring(opreg->name);
465 /* RegionSpace */
466 acpigen_emit_byte(opreg->regionspace);
467 /* RegionOffset & RegionLen, it can be byte word or double word */
468 acpigen_write_integer(opreg->regionoffset);
469 acpigen_write_integer(opreg->regionlen);
470}
471
Patrick Rudolph32bae492019-08-08 12:47:30 +0200472/*
473 * Generate ACPI AML code for Mutex
474 * Arg0: Pointer to name of mutex
475 * Arg1: Initial value of mutex
476 */
477void acpigen_write_mutex(const char *name, const uint8_t flags)
478{
479 /* MutexOp */
480 acpigen_emit_ext_op(MUTEX_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000481 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200482 acpigen_emit_byte(flags);
483}
484
485void acpigen_write_acquire(const char *name, const uint16_t val)
486{
487 /* AcquireOp */
488 acpigen_emit_ext_op(ACQUIRE_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000489 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200490 acpigen_emit_word(val);
491}
492
493void acpigen_write_release(const char *name)
494{
495 /* ReleaseOp */
496 acpigen_emit_ext_op(RELEASE_OP);
Paweł Anikieled3b6882023-10-13 11:24:19 +0000497 acpigen_emit_namestring(name);
Patrick Rudolph32bae492019-08-08 12:47:30 +0200498}
499
Patrick Rudolph894977b2017-07-01 16:15:05 +0200500static void acpigen_write_field_length(uint32_t len)
501{
502 uint8_t i, j;
503 uint8_t emit[4];
504
505 i = 1;
506 if (len < 0x40) {
507 emit[0] = len & 0x3F;
508 } else {
509 emit[0] = len & 0xF;
510 len >>= 4;
511 while (len) {
512 emit[i] = len & 0xFF;
513 i++;
514 len >>= 8;
515 }
516 }
517 /* Update bit 7:6 : Number of bytes followed by emit[0] */
518 emit[0] |= (i - 1) << 6;
519
520 for (j = 0; j < i; j++)
521 acpigen_emit_byte(emit[j]);
522}
523
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100524static void acpigen_write_field_offset(uint32_t offset, uint32_t current_bit_pos)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530525{
526 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530527
528 if (offset < current_bit_pos) {
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100529 printk(BIOS_WARNING, "%s: Cannot move offset backward", __func__);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530530 return;
531 }
532
533 diff_bits = offset - current_bit_pos;
534 /* Upper limit */
535 if (diff_bits > 0xFFFFFFF) {
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100536 printk(BIOS_WARNING, "%s: Offset very large to encode", __func__);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530537 return;
538 }
539
Naresh G Solanki8535c662016-10-25 00:51:24 +0530540 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200541 acpigen_write_field_length(diff_bits);
542}
543
Michael Niewöhner060dc7b2022-05-13 00:04:19 +0200544void acpigen_write_field_name(const char *name, uint32_t size)
Patrick Rudolph894977b2017-07-01 16:15:05 +0200545{
546 acpigen_emit_simple_namestring(name);
547 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530548}
549
Duncan Laurie095bbf92020-09-30 23:09:29 +0000550static void acpigen_write_field_reserved(uint32_t size)
551{
552 acpigen_emit_byte(0);
553 acpigen_write_field_length(size);
554}
555
Naresh G Solanki8535c662016-10-25 00:51:24 +0530556/*
557 * Generate ACPI AML code for Field
558 * Arg0: region name
559 * Arg1: Pointer to struct fieldlist.
560 * Arg2: no. of entries in Arg1
561 * Arg3: flags which indicate filed access type, lock rule & update rule.
562 * Example with fieldlist
563 * struct fieldlist l[] = {
564 * FIELDLIST_OFFSET(0x84),
565 * FIELDLIST_NAMESTR("PMCS", 2),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000566 * FIELDLIST_RESERVED(6),
Naresh G Solanki8535c662016-10-25 00:51:24 +0530567 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200568 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530569 * FIELD_PRESERVE);
570 * Output:
571 * Field (UART, AnyAcc, NoLock, Preserve)
572 * {
573 * Offset (0x84),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000574 * PMCS, 2,
575 * , 6,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530576 * }
577 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700578void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530579 uint8_t flags)
580{
581 uint16_t i;
582 uint32_t current_bit_pos = 0;
583
584 /* FieldOp */
585 acpigen_emit_ext_op(FIELD_OP);
586 /* Package Length */
587 acpigen_write_len_f();
588 /* NameString 4 chars only */
589 acpigen_emit_simple_namestring(name);
590 /* Field Flag */
591 acpigen_emit_byte(flags);
592
593 for (i = 0; i < count; i++) {
594 switch (l[i].type) {
595 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200596 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530597 current_bit_pos += l[i].bits;
598 break;
Duncan Laurie095bbf92020-09-30 23:09:29 +0000599 case RESERVED:
600 acpigen_write_field_reserved(l[i].bits);
601 current_bit_pos += l[i].bits;
602 break;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530603 case OFFSET:
604 acpigen_write_field_offset(l[i].bits, current_bit_pos);
605 current_bit_pos = l[i].bits;
606 break;
607 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100608 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530609 break;
610 }
611 }
612 acpigen_pop_len();
613}
614
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200615/*
616 * Generate ACPI AML code for IndexField
617 * Arg0: region name
618 * Arg1: Pointer to struct fieldlist.
619 * Arg2: no. of entries in Arg1
620 * Arg3: flags which indicate filed access type, lock rule & update rule.
621 * Example with fieldlist
622 * struct fieldlist l[] = {
623 * FIELDLIST_OFFSET(0x84),
624 * FIELDLIST_NAMESTR("PMCS", 2),
625 * };
626 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
627 * FIELD_NOLOCK |
628 * FIELD_PRESERVE);
629 * Output:
630 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
631 * {
632 * Offset (0x84),
633 * PMCS, 2
634 * }
635 */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100636void acpigen_write_indexfield(const char *idx, const char *data, struct fieldlist *l,
637 size_t count, uint8_t flags)
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200638{
639 uint16_t i;
640 uint32_t current_bit_pos = 0;
641
642 /* FieldOp */
643 acpigen_emit_ext_op(INDEX_FIELD_OP);
644 /* Package Length */
645 acpigen_write_len_f();
646 /* NameString 4 chars only */
647 acpigen_emit_simple_namestring(idx);
648 /* NameString 4 chars only */
649 acpigen_emit_simple_namestring(data);
650 /* Field Flag */
651 acpigen_emit_byte(flags);
652
653 for (i = 0; i < count; i++) {
654 switch (l[i].type) {
655 case NAME_STRING:
656 acpigen_write_field_name(l[i].name, l[i].bits);
657 current_bit_pos += l[i].bits;
658 break;
659 case OFFSET:
660 acpigen_write_field_offset(l[i].bits, current_bit_pos);
661 current_bit_pos = l[i].bits;
662 break;
663 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100664 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n", __func__, l[i].type);
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200665 break;
666 }
667 }
668 acpigen_pop_len();
669}
670
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100671void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000672{
673/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200674 Name (_PCT, Package (0x02)
675 {
676 ResourceTemplate ()
677 {
678 Register (FFixedHW,
679 0x00, // Bit Width
680 0x00, // Bit Offset
681 0x0000000000000000, // Address
682 ,)
683 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000684
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200685 ResourceTemplate ()
686 {
687 Register (FFixedHW,
688 0x00, // Bit Width
689 0x00, // Bit Offset
690 0x0000000000000000, // Address
691 ,)
692 }
693 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000694*/
695 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700696 /* 00000030 "0._PCT.," */
697 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
698 /* 00000038 "........" */
699 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
700 /* 00000040 "........" */
701 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
702 /* 00000048 "....y..." */
703 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
704 /* 00000050 "........" */
705 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
706 /* 00000058 "........" */
707 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000708 0x00, 0x79, 0x00
709 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100710 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000711}
712
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300713void acpigen_write_PTC(uint8_t duty_width, uint8_t duty_offset, uint16_t p_cnt)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200714{
715/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200716 Name (_PTC, Package (0x02)
717 {
718 ResourceTemplate ()
719 {
720 Register (FFixedHW,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300721 0x00, // Duty Width
722 0x00, // Duty Offset
723 0x0000000000000000, // P_CNT IO Address
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200724 ,)
725 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200726
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200727 ResourceTemplate ()
728 {
729 Register (FFixedHW,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300730 0x00, // Duty Width
731 0x00, // Duty Offset
732 0x0000000000000000, // P_CNT IO Address
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200733 ,)
734 }
735 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200736*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200737 acpi_addr_t addr = {
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300738 .bit_width = duty_width,
739 .bit_offset = duty_offset,
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +0200740 .access_size = ACPI_ACCESS_SIZE_UNDEFINED,
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300741 .addrl = p_cnt,
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100742 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200743 };
744
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300745 if (addr.addrl != 0)
746 addr.space_id = ACPI_ADDRESS_SPACE_IO;
747 else
748 addr.space_id = ACPI_ADDRESS_SPACE_FIXED;
749
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100750 acpigen_write_name("_PTC");
751 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200752
753 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700754 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200755
756 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700757 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200758
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100759 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200760}
761
Kyösti Mälkkiddc37d62023-04-17 12:11:03 +0300762void acpigen_write_empty_PTC(void)
763{
764 acpigen_write_PTC(0, 0, 0);
765}
766
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700767static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100768{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700769 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100770 acpigen_write_len_f();
771 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700772 acpigen_emit_byte(flags);
773}
774
775/* Method (name, nargs, NotSerialized) */
776void acpigen_write_method(const char *name, int nargs)
777{
778 __acpigen_write_method(name, (nargs & 7));
779}
780
781/* Method (name, nargs, Serialized) */
782void acpigen_write_method_serialized(const char *name, int nargs)
783{
784 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100785}
786
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100787void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100788{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700789 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100790 acpigen_write_len_f();
791 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100792}
793
Raul E Rangel052c9632021-05-12 17:01:30 -0600794void acpigen_write_thermal_zone(const char *name)
795{
796 acpigen_emit_ext_op(THERMAL_ZONE_OP);
797 acpigen_write_len_f();
798 acpigen_emit_namestring(name);
799}
800
Duncan Laurieabe2de82016-05-09 11:08:46 -0700801void acpigen_write_STA(uint8_t status)
802{
803 /*
804 * Method (_STA, 0, NotSerialized) { Return (status) }
805 */
806 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700807 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700808 acpigen_write_byte(status);
809 acpigen_pop_len();
810}
811
Sugnan Prabhu S4f77f612020-06-10 09:51:02 +0530812void acpigen_write_STA_ext(const char *namestring)
813{
814 /*
815 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
816 */
817 acpigen_write_method("_STA", 0);
818 acpigen_emit_byte(RETURN_OP);
819 acpigen_emit_namestring(namestring);
820 acpigen_pop_len();
821}
822
Felix Held2d4112f2023-05-04 23:00:06 +0200823void acpigen_write_BBN(uint8_t base_bus_number)
824{
825 /*
826 * Method (_BBN, 0, NotSerialized) { Return (status) }
827 */
828 acpigen_write_method("_BBN", 0);
829 acpigen_emit_byte(RETURN_OP);
830 acpigen_write_byte(base_bus_number);
831 acpigen_pop_len();
832}
833
Raul E Rangelc7048322021-04-19 15:58:25 -0600834void acpigen_write_LPI_package(u64 level, const struct acpi_lpi_state *states, u16 nentries)
835{
836 /*
837 * Name (_LPI, Package (0x06) // _LPI: Low Power Idle States
838 * {
839 * 0x0000,
840 * 0x0000000000000000,
841 * 0x0003,
842 * Package (0x0A)
843 * {
844 * 0x00000002,
845 * 0x00000001,
846 * 0x00000001,
847 * 0x00000000,
848 * 0x00000000,
849 * 0x00000000,
850 * ResourceTemplate ()
851 * {
852 * Register (FFixedHW,
853 * 0x02, // Bit Width
854 * 0x02, // Bit Offset
855 * 0x0000000000000000, // Address
856 * ,)
857 * },
858 *
859 * ResourceTemplate ()
860 * {
861 * Register (SystemMemory,
862 * 0x00, // Bit Width
863 * 0x00, // Bit Offset
864 * 0x0000000000000000, // Address
865 * ,)
866 * },
867 *
868 * ResourceTemplate ()
869 * {
870 * Register (SystemMemory,
871 * 0x00, // Bit Width
872 * 0x00, // Bit Offset
873 * 0x0000000000000000, // Address
874 * ,)
875 * },
876 *
877 * "C1"
878 * },
879 * ...
880 * }
881 */
882
883 acpigen_write_name("_LPI");
884 acpigen_write_package(3 + nentries);
885 acpigen_write_word(0); /* Revision */
886 acpigen_write_qword(level);
887 acpigen_write_word(nentries);
888
889 for (size_t i = 0; i < nentries; i++, states++) {
890 acpigen_write_package(0xA);
891 acpigen_write_dword(states->min_residency_us);
892 acpigen_write_dword(states->worst_case_wakeup_latency_us);
893 acpigen_write_dword(states->flags);
894 acpigen_write_dword(states->arch_context_lost_flags);
895 acpigen_write_dword(states->residency_counter_frequency_hz);
896 acpigen_write_dword(states->enabled_parent_state);
897 acpigen_write_register_resource(&states->entry_method);
898 acpigen_write_register_resource(&states->residency_counter_register);
899 acpigen_write_register_resource(&states->usage_counter_register);
900 acpigen_write_string(states->state_name);
901 acpigen_pop_len();
902 }
903 acpigen_pop_len();
904}
905
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100906/*
907 * Generates a func with max supported P-states.
908 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100909void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000910{
911/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200912 Method (_PPC, 0, NotSerialized)
913 {
914 Return (nr)
915 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000916*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100917 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700918 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000919 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100920 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100921 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000922}
923
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100924/*
925 * Generates a func with max supported P-states saved
926 * in the variable PPCM.
927 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100928void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700929{
930/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200931 Method (_PPC, 0, NotSerialized)
932 {
933 Return (PPCM)
934 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700935*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100936 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700937 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700938 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100939 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100940 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700941}
942
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100943void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200944{
945/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200946 // Sample _TPC method
947 Method (_TPC, 0, NotSerialized)
948 {
949 Return (\TLVL)
950 }
951*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100952 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700953 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100954 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100955 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200956}
957
Duncan Laurieabe2de82016-05-09 11:08:46 -0700958void acpigen_write_PRW(u32 wake, u32 level)
959{
960 /*
961 * Name (_PRW, Package () { wake, level }
962 */
963 acpigen_write_name("_PRW");
964 acpigen_write_package(2);
965 acpigen_write_integer(wake);
966 acpigen_write_integer(level);
967 acpigen_pop_len();
968}
969
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100970void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat, u32 busmLat, u32 control,
971 u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000972{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100973 acpigen_write_package(6);
974 acpigen_write_dword(coreFreq);
975 acpigen_write_dword(power);
976 acpigen_write_dword(transLat);
977 acpigen_write_dword(busmLat);
978 acpigen_write_dword(control);
979 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100980 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700981
Elyes Haouas1f5e1b42022-02-17 17:44:07 +0100982 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n", coreFreq, power,
983 control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000984}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000985
Jason Gleneskca36aed2020-09-15 21:01:57 -0700986void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
987{
988 size_t pstate;
989
990 acpigen_write_name("_PSS");
991 acpigen_write_package(nentries);
992 for (pstate = 0; pstate < nentries; pstate++) {
993 acpigen_write_PSS_package(
994 pstate_values->core_freq, pstate_values->power,
995 pstate_values->transition_latency, pstate_values->bus_master_latency,
996 pstate_values->control_value, pstate_values->status_value);
997 pstate_values++;
998 }
999
1000 acpigen_pop_len();
1001}
1002
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001003void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +00001004{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001005 acpigen_write_name("_PSD");
1006 acpigen_write_package(1);
1007 acpigen_write_package(5);
1008 acpigen_write_byte(5); // 5 values
1009 acpigen_write_byte(0); // revision 0
1010 acpigen_write_dword(domain);
1011 acpigen_write_dword(coordtype);
1012 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001013 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001014 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +00001015}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001016
Angel Ponsd2794ce2021-10-17 12:59:43 +02001017void acpigen_write_CST_package_entry(const acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001018{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001019 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001020 acpigen_write_register_resource(&cstate->resource);
Jason Glenesk201acca2020-09-11 12:36:15 -07001021 acpigen_write_byte(cstate->ctype);
1022 acpigen_write_word(cstate->latency);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001023 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001024 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001025}
1026
Angel Ponsd2794ce2021-10-17 12:59:43 +02001027void acpigen_write_CST_package(const acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001028{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001029 int i;
1030 acpigen_write_name("_CST");
1031 acpigen_write_package(nentries+1);
Jason Glenesk201acca2020-09-11 12:36:15 -07001032 acpigen_write_integer(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001033
1034 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001035 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001036
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001037 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001038}
1039
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001040void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
1041 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -05001042{
1043 acpigen_write_name("_CSD");
1044 acpigen_write_package(1);
1045 acpigen_write_package(6);
Jason Glenesk201acca2020-09-11 12:36:15 -07001046 acpigen_write_integer(6); // 6 values
Timothy Pearson83abd812015-06-08 19:35:06 -05001047 acpigen_write_byte(0); // revision 0
1048 acpigen_write_dword(domain);
1049 acpigen_write_dword(coordtype);
1050 acpigen_write_dword(numprocs);
1051 acpigen_write_dword(index);
1052 acpigen_pop_len();
1053 acpigen_pop_len();
1054}
1055
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001056void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001057{
1058/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +02001059 Sample _TSS package with 100% and 50% duty cycles
1060 Name (_TSS, Package (0x02)
1061 {
1062 Package(){100, 1000, 0, 0x00, 0)
1063 Package(){50, 520, 0, 0x18, 0)
1064 })
1065*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001066 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001067 acpi_tstate_t *tstate = tstate_list;
1068
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001069 acpigen_write_name("_TSS");
1070 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +02001071
1072 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001073 acpigen_write_package(5);
1074 acpigen_write_dword(tstate->percent);
1075 acpigen_write_dword(tstate->power);
1076 acpigen_write_dword(tstate->latency);
1077 acpigen_write_dword(tstate->control);
1078 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001079 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001080 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001081 }
1082
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001083 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001084}
1085
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001086void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001087{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001088 acpigen_write_name("_TSD");
1089 acpigen_write_package(1);
1090 acpigen_write_package(5);
1091 acpigen_write_byte(5); // 5 values
1092 acpigen_write_byte(0); // revision 0
1093 acpigen_write_dword(domain);
1094 acpigen_write_dword(coordtype);
1095 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001096 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001097 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001098}
1099
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001100void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001101{
1102 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001103 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001104 * Byte 0:
1105 * Bit7 : 1 => big item
1106 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
1107 */
1108 acpigen_emit_byte(0x86);
1109 /* Byte 1+2: length (0x0009) */
1110 acpigen_emit_byte(0x09);
1111 acpigen_emit_byte(0x00);
1112 /* bit1-7 are ignored */
1113 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -07001114 acpigen_emit_dword(base);
1115 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001116}
1117
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001118static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001119{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +02001120 acpigen_emit_byte(0x82); /* Register Descriptor */
1121 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
1122 acpigen_emit_byte(0x00); /* Register Length 15:8 */
1123 acpigen_emit_byte(addr->space_id); /* Address Space ID */
1124 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
1125 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +01001126 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -07001127 acpigen_emit_dword(addr->addrl); /* Register Address Low */
1128 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +02001129}
1130
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001131void acpigen_write_register_resource(const acpi_addr_t *addr)
1132{
1133 acpigen_write_resourcetemplate_header();
1134 acpigen_write_register(addr);
1135 acpigen_write_resourcetemplate_footer();
1136}
1137
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001138void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001139{
1140 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001141 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001142 * Byte 0:
1143 * Bit7 : 0 => small item
1144 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1145 * Bit2-0: 010 (0x2) => 2 Bytes long
1146 */
1147 acpigen_emit_byte(0x22);
1148 acpigen_emit_byte(mask & 0xff);
1149 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001150}
1151
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001152void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001153{
1154 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001155 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001156 * Byte 0:
1157 * Bit7 : 0 => small item
1158 * Bit6-3: 1000 (0x8) => I/O port descriptor
1159 * Bit2-0: 111 (0x7) => 7 Bytes long
1160 */
1161 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001162 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001163 /* bit1-7 are ignored */
1164 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1165 /* minimum base address the device may be configured for */
1166 acpigen_emit_byte(min & 0xff);
1167 acpigen_emit_byte((min >> 8) & 0xff);
1168 /* maximum base address the device may be configured for */
1169 acpigen_emit_byte(max & 0xff);
1170 acpigen_emit_byte((max >> 8) & 0xff);
1171 /* alignment for min base */
1172 acpigen_emit_byte(align & 0xff);
1173 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001174}
1175
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001176void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001177{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001178 /*
1179 * A ResourceTemplate() is a Buffer() with a
1180 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001181 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001182 * (small item 0xf, len 1)
1183 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001184 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001185 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001186 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001187 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001188 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001189 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001190 acpigen_emit_byte(0x00);
1191 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001192}
1193
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001194void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001195{
1196 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001197 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001198 /*
1199 * end tag (acpi 4.0 Section 6.4.2.8)
1200 * 0x79 <checksum>
1201 * 0x00 is treated as a good checksum according to the spec
1202 * and is what iasl generates.
1203 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001204 acpigen_emit_byte(0x79);
1205 acpigen_emit_byte(0x00);
1206
Nico Huber7d89ce32017-04-14 00:08:18 +02001207 /* Start counting past the 2-bytes length added in
1208 acpigen_write_resourcetemplate() above. */
1209 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001210
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001211 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001212 p[0] = len & 0xff;
1213 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001214 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001215 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001216}
1217
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001218static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev, struct resource *res)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001219{
1220 acpigen_write_mem32fixed(0, res->base, res->size);
1221}
1222
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001223static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev, struct resource *res)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001224{
1225 resource_t base = res->base;
1226 resource_t size = res->size;
1227 while (size > 0) {
1228 resource_t sz = size > 255 ? 255 : size;
1229 acpigen_write_io16(base, base, 0, sz, 1);
1230 size -= sz;
1231 base += sz;
1232 }
1233}
1234
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001235void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001236{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001237 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001238
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001239 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001240 search_global_resources(
1241 IORESOURCE_MEM | IORESOURCE_RESERVE,
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001242 IORESOURCE_MEM | IORESOURCE_RESERVE,
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001243 acpigen_add_mainboard_rsvd_mem32, 0);
1244
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001245 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001246 search_global_resources(
1247 IORESOURCE_IO | IORESOURCE_RESERVE,
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001248 IORESOURCE_IO | IORESOURCE_RESERVE,
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001249 acpigen_add_mainboard_rsvd_io, 0);
1250
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001251 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001252}
1253
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001254void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001255{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001256 acpigen_write_scope(scope);
1257 acpigen_write_name(name);
1258 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001259 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001260}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001261
1262static int hex2bin(const char c)
1263{
1264 if (c >= 'A' && c <= 'F')
1265 return c - 'A' + 10;
1266 if (c >= 'a' && c <= 'f')
1267 return c - 'a' + 10;
1268 return c - '0';
1269}
1270
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001271void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001272{
1273 u32 compact = 0;
1274
1275 /* Clamping individual values would be better but
1276 there is a disagreement over what is a valid
1277 EISA id, so accept anything and don't clamp,
1278 parent code should create a valid EISAid.
1279 */
1280 compact |= (eisaid[0] - 'A' + 1) << 26;
1281 compact |= (eisaid[1] - 'A' + 1) << 21;
1282 compact |= (eisaid[2] - 'A' + 1) << 16;
1283 compact |= hex2bin(eisaid[3]) << 12;
1284 compact |= hex2bin(eisaid[4]) << 8;
1285 compact |= hex2bin(eisaid[5]) << 4;
1286 compact |= hex2bin(eisaid[6]);
1287
1288 acpigen_emit_byte(0xc);
1289 acpigen_emit_byte((compact >> 24) & 0xff);
1290 acpigen_emit_byte((compact >> 16) & 0xff);
1291 acpigen_emit_byte((compact >> 8) & 0xff);
1292 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001293}
Duncan Laurie3829f232016-05-09 11:08:46 -07001294
1295/*
1296 * ToUUID(uuid)
1297 *
1298 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1299 * order for the bytes that make up a UUID Buffer object.
1300 * UUID byte order for input:
1301 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1302 * UUID byte order for output:
1303 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1304 */
1305#define UUID_LEN 16
1306void acpigen_write_uuid(const char *uuid)
1307{
1308 uint8_t buf[UUID_LEN];
1309 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1310 8, 9, 10, 11, 12, 13, 14, 15 };
1311
1312 /* Parse UUID string into bytes */
1313 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1314 return;
1315
1316 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001317 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001318 acpigen_write_len_f();
1319
1320 /* Buffer length in bytes */
1321 acpigen_write_word(UUID_LEN);
1322
1323 /* Output UUID in expected order */
1324 for (i = 0; i < UUID_LEN; i++)
1325 acpigen_emit_byte(buf[order[i]]);
1326
1327 acpigen_pop_len();
1328}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001329
1330/*
1331 * Name (_PRx, Package(One) { name })
1332 * ...
1333 * PowerResource (name, level, order)
1334 */
1335void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001336 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001337{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001338 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001339 for (i = 0; i < dev_states_count; i++) {
1340 acpigen_write_name(dev_states[i]);
1341 acpigen_write_package(1);
1342 acpigen_emit_simple_namestring(name);
1343 acpigen_pop_len(); /* Package */
1344 }
1345
1346 acpigen_emit_ext_op(POWER_RES_OP);
1347
1348 acpigen_write_len_f();
1349
1350 acpigen_emit_simple_namestring(name);
1351 acpigen_emit_byte(level);
1352 acpigen_emit_word(order);
1353}
1354
1355/* Sleep (ms) */
1356void acpigen_write_sleep(uint64_t sleep_ms)
1357{
1358 acpigen_emit_ext_op(SLEEP_OP);
1359 acpigen_write_integer(sleep_ms);
1360}
1361
1362void acpigen_write_store(void)
1363{
1364 acpigen_emit_byte(STORE_OP);
1365}
1366
1367/* Store (src, dst) */
1368void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1369{
1370 acpigen_write_store();
1371 acpigen_emit_byte(src);
1372 acpigen_emit_byte(dst);
1373}
1374
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001375/* Store (src, "namestr") */
1376void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1377{
1378 acpigen_write_store();
1379 acpigen_emit_byte(src);
1380 acpigen_emit_namestring(dst);
1381}
1382
Duncan Laurie8e391d32020-09-30 23:17:41 +00001383/* Store (src, "namestr") */
1384void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst)
1385{
1386 acpigen_write_store();
1387 acpigen_write_integer(src);
1388 acpigen_emit_namestring(dst);
1389}
1390
Cliff Huang24f3dc82023-02-04 21:11:51 -08001391/* Store ("namestr", dst) */
1392void acpigen_write_store_namestr_to_op(const char *src, uint8_t dst)
1393{
1394 acpigen_write_store();
1395 acpigen_emit_namestring(src);
1396 acpigen_emit_byte(dst);
1397}
1398
Duncan Laurie8e391d32020-09-30 23:17:41 +00001399/* Store (src, dst) */
1400void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst)
1401{
1402 acpigen_write_store();
1403 acpigen_write_integer(src);
1404 acpigen_emit_byte(dst);
1405}
1406
Felix Held178cf352023-02-09 16:29:46 +01001407/* Store ("namestr", "namestr") */
1408void acpigen_write_store_namestr_to_namestr(const char *src, const char *dst)
1409{
1410 acpigen_write_store();
1411 acpigen_emit_namestring(src);
1412 acpigen_emit_namestring(dst);
1413}
1414
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001415/* Or (arg1, arg2, res) */
1416void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1417{
1418 acpigen_emit_byte(OR_OP);
1419 acpigen_emit_byte(arg1);
1420 acpigen_emit_byte(arg2);
1421 acpigen_emit_byte(res);
1422}
1423
Rajat Jain310623b2020-02-26 11:02:53 -08001424/* Xor (arg1, arg2, res) */
1425void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1426{
1427 acpigen_emit_byte(XOR_OP);
1428 acpigen_emit_byte(arg1);
1429 acpigen_emit_byte(arg2);
1430 acpigen_emit_byte(res);
1431}
1432
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001433/* And (arg1, arg2, res) */
1434void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1435{
1436 acpigen_emit_byte(AND_OP);
1437 acpigen_emit_byte(arg1);
1438 acpigen_emit_byte(arg2);
1439 acpigen_emit_byte(res);
1440}
1441
1442/* Not (arg, res) */
1443void acpigen_write_not(uint8_t arg, uint8_t res)
1444{
1445 acpigen_emit_byte(NOT_OP);
1446 acpigen_emit_byte(arg);
1447 acpigen_emit_byte(res);
1448}
1449
Cliff Huange6083082023-01-19 21:18:23 -08001450/* Concatenate (str, src_res, dest_res) */
1451void acpigen_concatenate_string_op(const char *str, uint8_t src_res, uint8_t dest_res)
1452{
1453 acpigen_emit_byte(CONCATENATE_OP);
1454 acpigen_write_string(str);
1455 acpigen_emit_byte(src_res);
1456 acpigen_emit_byte(dest_res);
1457}
1458
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001459/* Store (str, DEBUG) */
1460void acpigen_write_debug_string(const char *str)
1461{
1462 acpigen_write_store();
1463 acpigen_write_string(str);
1464 acpigen_emit_ext_op(DEBUG_OP);
1465}
1466
1467/* Store (val, DEBUG) */
1468void acpigen_write_debug_integer(uint64_t val)
1469{
1470 acpigen_write_store();
1471 acpigen_write_integer(val);
1472 acpigen_emit_ext_op(DEBUG_OP);
1473}
1474
1475/* Store (op, DEBUG) */
1476void acpigen_write_debug_op(uint8_t op)
1477{
1478 acpigen_write_store();
1479 acpigen_emit_byte(op);
1480 acpigen_emit_ext_op(DEBUG_OP);
1481}
1482
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001483/* Store (str, DEBUG) */
1484void acpigen_write_debug_namestr(const char *str)
1485{
1486 acpigen_write_store();
1487 acpigen_emit_namestring(str);
1488 acpigen_emit_ext_op(DEBUG_OP);
1489}
1490
Cliff Huange6083082023-01-19 21:18:23 -08001491/* Concatenate (str1, res, tmp_res)
1492 Store(tmp_res, DEBUG) */
1493void acpigen_write_debug_concatenate_string_op(const char *str, uint8_t res,
1494 uint8_t tmp_res)
1495{
1496 acpigen_concatenate_string_op(str, res, tmp_res);
1497 acpigen_write_debug_op(tmp_res);
1498}
1499
Cliff Huang063a1b82023-02-18 00:17:26 -08001500static void acpigen_tx_byte(unsigned char byte, void *data)
1501{
1502 acpigen_emit_byte(byte);
1503}
1504
1505/* Store("formatted string", DEBUG) */
1506void acpigen_write_debug_sprintf(const char *fmt, ...)
1507{
1508 va_list args;
1509
1510 acpigen_write_store();
1511
1512 acpigen_emit_byte(STRING_PREFIX);
1513 va_start(args, fmt);
1514 vtxprintf(acpigen_tx_byte, fmt, args, NULL);
1515 va_end(args);
1516 acpigen_emit_byte('\0');
1517
1518 acpigen_emit_ext_op(DEBUG_OP);
1519}
1520
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001521void acpigen_write_if(void)
1522{
1523 acpigen_emit_byte(IF_OP);
1524 acpigen_write_len_f();
1525}
1526
1527/* If (And (arg1, arg2)) */
1528void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1529{
1530 acpigen_write_if();
1531 acpigen_emit_byte(AND_OP);
1532 acpigen_emit_byte(arg1);
1533 acpigen_emit_byte(arg2);
1534}
1535
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001536/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001537 * Generates ACPI code for checking if operand1 and operand2 are equal.
1538 * Both operand1 and operand2 are ACPI ops.
1539 *
1540 * If (Lequal (op,1 op2))
1541 */
1542void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1543{
1544 acpigen_write_if();
1545 acpigen_emit_byte(LEQUAL_OP);
1546 acpigen_emit_byte(op1);
1547 acpigen_emit_byte(op2);
1548}
1549
1550/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001551 * Generates ACPI code for checking if operand1 is greater than operand2.
1552 * Both operand1 and operand2 are ACPI ops.
1553 *
1554 * If (Lgreater (op1 op2))
1555 */
1556void acpigen_write_if_lgreater_op_op(uint8_t op1, uint8_t op2)
1557{
1558 acpigen_write_if();
1559 acpigen_emit_byte(LGREATER_OP);
1560 acpigen_emit_byte(op1);
1561 acpigen_emit_byte(op2);
1562}
1563
1564/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001565 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1566 * operand1 is ACPI op and operand2 is an integer.
1567 *
1568 * If (Lequal (op, val))
1569 */
1570void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001571{
1572 acpigen_write_if();
1573 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001574 acpigen_emit_byte(op);
1575 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001576}
1577
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001578/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001579 * Generates ACPI code for checking if operand is greater than the value, where,
1580 * operand is ACPI op and val is an integer.
1581 *
1582 * If (Lgreater (op, val))
1583 */
1584void acpigen_write_if_lgreater_op_int(uint8_t op, uint64_t val)
1585{
1586 acpigen_write_if();
1587 acpigen_emit_byte(LGREATER_OP);
1588 acpigen_emit_byte(op);
1589 acpigen_write_integer(val);
1590}
1591
1592/*
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001593 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1594 * operand1 is namestring and operand2 is an integer.
1595 *
1596 * If (Lequal ("namestr", val))
1597 */
1598void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1599{
1600 acpigen_write_if();
1601 acpigen_emit_byte(LEQUAL_OP);
1602 acpigen_emit_namestring(namestr);
1603 acpigen_write_integer(val);
1604}
1605
Tim Wawrzynczakc4ca2f62021-07-01 11:18:50 -06001606/*
Cliff Huang24f3dc82023-02-04 21:11:51 -08001607 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1608 * operand1 is namestring and operand2 is an integer.
1609 *
1610 * If (Lgreater ("namestr", val))
1611 */
1612void acpigen_write_if_lgreater_namestr_int(const char *namestr, uint64_t val)
1613{
1614 acpigen_write_if();
1615 acpigen_emit_byte(LGREATER_OP);
1616 acpigen_emit_namestring(namestr);
1617 acpigen_write_integer(val);
1618}
1619
1620/*
Tim Wawrzynczakc4ca2f62021-07-01 11:18:50 -06001621 * Generates ACPI code to check at runtime if an object named `namestring`
1622 * exists, and leaves the If scope open to continue execute code when this
1623 * is true. NOTE: Requires matching acpigen_write_if_end().
1624 *
1625 * If (CondRefOf (NAME))
1626 */
1627void acpigen_write_if_cond_ref_of(const char *namestring)
1628{
1629 acpigen_write_if();
1630 acpigen_emit_ext_op(COND_REFOF_OP);
1631 acpigen_emit_namestring(namestring);
1632 acpigen_emit_byte(ZERO_OP); /* ignore COND_REFOF_OP destination */
1633}
1634
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001635/* Closes previously opened if statement and generates ACPI code for else statement. */
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001636void acpigen_write_else(void)
1637{
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001638 acpigen_pop_len();
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001639 acpigen_emit_byte(ELSE_OP);
1640 acpigen_write_len_f();
1641}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001642
Duncan Laurie36858202020-10-06 21:01:51 +00001643void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count)
1644{
1645 acpigen_emit_byte(SHIFT_LEFT_OP);
1646 acpigen_emit_byte(src_result);
1647 acpigen_write_integer(count);
1648 acpigen_emit_byte(ZERO_OP);
1649}
1650
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001651void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1652{
1653 acpigen_emit_byte(TO_BUFFER_OP);
1654 acpigen_emit_byte(src);
1655 acpigen_emit_byte(dst);
1656}
1657
1658void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1659{
1660 acpigen_emit_byte(TO_INTEGER_OP);
1661 acpigen_emit_byte(src);
1662 acpigen_emit_byte(dst);
1663}
1664
Aaron Durbin80bc0912020-08-19 23:17:42 -06001665void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op)
1666{
1667 acpigen_emit_byte(TO_INTEGER_OP);
1668 acpigen_emit_namestring(source);
1669 acpigen_emit_byte(dst_op);
1670}
1671
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301672void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001673{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301674 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001675
1676 acpigen_emit_byte(BUFFER_OP);
1677 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301678 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001679
1680 for (i = 0; i < size; i++)
1681 acpigen_emit_byte(arr[i]);
1682
1683 acpigen_pop_len();
1684}
1685
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301686void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001687{
1688 acpigen_emit_byte(RETURN_OP);
1689 acpigen_write_byte_buffer(arr, size);
1690}
1691
1692void acpigen_write_return_singleton_buffer(uint8_t arg)
1693{
1694 acpigen_write_return_byte_buffer(&arg, 1);
1695}
1696
Duncan Laurie2fe74712020-06-01 17:36:56 -07001697void acpigen_write_return_op(uint8_t arg)
1698{
1699 acpigen_emit_byte(RETURN_OP);
1700 acpigen_emit_byte(arg);
1701}
1702
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001703void acpigen_write_return_byte(uint8_t arg)
1704{
1705 acpigen_emit_byte(RETURN_OP);
1706 acpigen_write_byte(arg);
1707}
1708
Naresh G Solanki05abe432016-11-17 00:16:29 +05301709void acpigen_write_return_integer(uint64_t arg)
1710{
1711 acpigen_emit_byte(RETURN_OP);
1712 acpigen_write_integer(arg);
1713}
1714
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001715void acpigen_write_return_namestr(const char *arg)
1716{
1717 acpigen_emit_byte(RETURN_OP);
1718 acpigen_emit_namestring(arg);
1719}
1720
Naresh G Solanki05abe432016-11-17 00:16:29 +05301721void acpigen_write_return_string(const char *arg)
1722{
1723 acpigen_emit_byte(RETURN_OP);
1724 acpigen_write_string(arg);
1725}
1726
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001727void acpigen_write_upc(enum acpi_upc_type type)
1728{
1729 acpigen_write_name("_UPC");
1730 acpigen_write_package(4);
1731 /* Connectable */
1732 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1733 /* Type */
1734 acpigen_write_byte(type);
1735 /* Reserved0 */
1736 acpigen_write_zero();
1737 /* Reserved1 */
1738 acpigen_write_zero();
1739 acpigen_pop_len();
1740}
1741
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001742void acpigen_write_pld(const struct acpi_pld *pld)
1743{
1744 uint8_t buf[20];
1745
1746 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1747 return;
1748
1749 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001750 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001751 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001752 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001753}
1754
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001755void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *), size_t count, void *arg)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301756{
1757 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1758 acpigen_write_dsm_uuid_arr(&id, 1);
1759}
1760
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001761/*
1762 * Create a supported functions bitmask
1763 * bit 0: other functions than 0 are supported
1764 * bits 1-x: function x supported
1765 */
Arthur Heymans9c1f78d2023-06-22 21:04:06 +02001766/* On GCC aarch64 the compiler is worried about alloca() having unbounded stack usage. */
1767#if defined(__GNUC__) && !defined(__clang__)
1768#pragma GCC diagnostic ignored "-Wstack-usage="
1769#endif
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001770static void acpigen_dsm_uuid_enum_functions(const struct dsm_uuid *id)
1771{
1772 const size_t bytes = DIV_ROUND_UP(id->count, BITS_PER_BYTE);
1773 uint8_t *buffer = alloca(bytes);
1774 bool set = false;
1775 size_t cb_idx = 0;
1776
1777 memset(buffer, 0, bytes);
1778
1779 for (size_t i = 0; i < bytes; i++) {
1780 for (size_t j = 0; j < BITS_PER_BYTE; j++) {
1781 if (cb_idx >= id->count)
1782 break;
1783
1784 if (id->callbacks[cb_idx++]) {
1785 set = true;
1786 buffer[i] |= BIT(j);
1787 }
1788 }
1789 }
1790
1791 if (set)
1792 buffer[0] |= BIT(0);
1793
1794 acpigen_write_return_byte_buffer(buffer, bytes);
1795}
1796
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301797static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1798{
1799 size_t i;
1800
1801 /* If (LEqual (Local0, ToUUID(uuid))) */
1802 acpigen_write_if();
1803 acpigen_emit_byte(LEQUAL_OP);
1804 acpigen_emit_byte(LOCAL0_OP);
1805 acpigen_write_uuid(id->uuid);
1806
1807 /* ToInteger (Arg2, Local1) */
1808 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1809
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001810 /* If (LEqual(Local1, 0)) */
1811 {
1812 acpigen_write_if_lequal_op_int(LOCAL1_OP, 0);
1813 if (id->callbacks[0])
1814 id->callbacks[0](id->arg);
1815 else if (id->count)
1816 acpigen_dsm_uuid_enum_functions(id);
1817 acpigen_write_if_end();
1818 }
1819
1820 for (i = 1; i < id->count; i++) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301821 /* If (LEqual (Local1, i)) */
1822 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1823
1824 /* Callback to write if handler. */
1825 if (id->callbacks[i])
1826 id->callbacks[i](id->arg);
1827
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001828 acpigen_write_if_end(); /* If */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301829 }
1830
1831 /* Default case: Return (Buffer (One) { 0x0 }) */
1832 acpigen_write_return_singleton_buffer(0x0);
1833
Tim Wawrzynczakd96f3a22021-07-14 15:56:57 -06001834 acpigen_write_if_end(); /* If (LEqual (Local0, ToUUID(uuid))) */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301835
1836}
1837
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001838/*
1839 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301840 * This function takes as input array of uuid for the device, set of callbacks
1841 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1842 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1843 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001844 *
1845 * Arguments passed into _DSM method:
1846 * Arg0 = UUID
1847 * Arg1 = Revision
1848 * Arg2 = Function index
1849 * Arg3 = Function specific arguments
1850 *
1851 * AML code generated would look like:
1852 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301853 * ToBuffer (Arg0, Local0)
1854 * If (LEqual (Local0, ToUUID(uuid))) {
1855 * ToInteger (Arg2, Local1)
1856 * If (LEqual (Local1, 0)) {
1857 * <acpigen by callback[0]>
1858 * }
1859 * ...
1860 * If (LEqual (Local1, n)) {
1861 * <acpigen by callback[n]>
1862 * }
1863 * Return (Buffer (One) { 0x0 })
1864 * }
1865 * ...
1866 * If (LEqual (Local0, ToUUID(uuidn))) {
1867 * ...
1868 * }
1869 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001870 * }
1871 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301872void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001873{
1874 size_t i;
1875
1876 /* Method (_DSM, 4, Serialized) */
1877 acpigen_write_method_serialized("_DSM", 0x4);
1878
1879 /* ToBuffer (Arg0, Local0) */
1880 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1881
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001882 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301883 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001884
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301885 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001886 acpigen_write_return_singleton_buffer(0x0);
1887
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001888 acpigen_pop_len(); /* Method _DSM */
1889}
1890
Matt Delcob425bc82018-08-13 13:36:27 -07001891void acpigen_write_CPPC_package(const struct cppc_config *config)
1892{
1893 u32 i;
1894 u32 max;
1895 switch (config->version) {
1896 case 1:
1897 max = CPPC_MAX_FIELDS_VER_1;
1898 break;
1899 case 2:
1900 max = CPPC_MAX_FIELDS_VER_2;
1901 break;
1902 case 3:
1903 max = CPPC_MAX_FIELDS_VER_3;
1904 break;
1905 default:
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01001906 printk(BIOS_ERR, "CPPC version %u is not implemented\n", config->version);
Matt Delcob425bc82018-08-13 13:36:27 -07001907 return;
1908 }
1909 acpigen_write_name(CPPC_PACKAGE_NAME);
1910
1911 /* Adding 2 to account for length and version fields */
1912 acpigen_write_package(max + 2);
1913 acpigen_write_dword(max + 2);
1914
1915 acpigen_write_byte(config->version);
1916
1917 for (i = 0; i < max; ++i) {
Michael Niewöhner38107fa2021-10-05 22:22:21 +02001918 const cppc_entry_t *entry = &config->entries[i];
1919 if (entry->type == CPPC_TYPE_DWORD)
1920 acpigen_write_dword(entry->dword);
1921 else
1922 acpigen_write_register_resource(&entry->reg);
Matt Delcob425bc82018-08-13 13:36:27 -07001923 }
1924 acpigen_pop_len();
1925}
1926
1927void acpigen_write_CPPC_method(void)
1928{
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001929 char pscope[16];
Felix Heldf0a8b042023-05-12 15:55:06 +02001930 snprintf(pscope, sizeof(pscope),
1931 "\\_SB." CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001932
Matt Delcob425bc82018-08-13 13:36:27 -07001933 acpigen_write_method("_CPC", 0);
1934 acpigen_emit_byte(RETURN_OP);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001935 acpigen_emit_namestring(pscope);
Matt Delcob425bc82018-08-13 13:36:27 -07001936 acpigen_pop_len();
1937}
1938
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001939/*
1940 * Generate ACPI AML code for _ROM method.
1941 * This function takes as input ROM data and ROM length.
1942 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001943 * The ACPI spec isn't clear about what should happen at the end of the
1944 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1945 * bytes in the returned buffer with zeros.
1946 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001947 * Arguments passed into _DSM method:
1948 * Arg0 = Offset in Bytes
1949 * Arg1 = Bytes to return
1950 *
1951 * Example:
1952 * acpigen_write_rom(0xdeadbeef, 0x10000)
1953 *
1954 * AML code generated would look like:
1955 * Method (_ROM, 2, NotSerialized) {
1956 *
1957 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1958 * Field (ROMS, AnyAcc, NoLock, Preserve)
1959 * {
1960 * Offset (0),
1961 * RBF0, 0x80000
1962 * }
1963 *
1964 * Store (Arg0, Local0)
1965 * Store (Arg1, Local1)
1966 *
1967 * If (LGreater (Local1, 0x1000))
1968 * {
1969 * Store (0x1000, Local1)
1970 * }
1971 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001972 * Store (Local1, Local3)
1973 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001974 * If (LGreater (Local0, 0x10000))
1975 * {
1976 * Return(Buffer(Local1){0})
1977 * }
1978 *
1979 * If (LGreater (Local0, 0x0f000))
1980 * {
1981 * Subtract (0x10000, Local0, Local2)
1982 * If (LGreater (Local1, Local2))
1983 * {
1984 * Store (Local2, Local1)
1985 * }
1986 * }
1987 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001988 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001989 *
1990 * Multiply (Local0, 0x08, Local0)
1991 * Multiply (Local1, 0x08, Local1)
1992 *
1993 * CreateField (RBF0, Local0, Local1, TMPB)
1994 * Store (TMPB, ROM1)
1995 * Return (ROM1)
1996 * }
1997 */
1998
1999void acpigen_write_rom(void *bios, const size_t length)
2000{
2001 ASSERT(bios)
2002 ASSERT(length)
2003
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02002004 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06002005 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002006
2007 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002008 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY, (uintptr_t)bios, length);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002009 acpigen_write_opregion(&opreg);
2010
2011 struct fieldlist l[] = {
2012 FIELDLIST_OFFSET(0),
2013 FIELDLIST_NAMESTR("RBF0", 8 * length),
2014 };
2015
2016 /* Field (ROMS, AnyAcc, NoLock, Preserve)
2017 * {
2018 * Offset (0),
2019 * RBF0, 0x80000
2020 * } */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002021 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC | FIELD_NOLOCK | FIELD_PRESERVE);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002022
2023 /* Store (Arg0, Local0) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002024 acpigen_write_store_ops(ARG0_OP, LOCAL0_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002025
2026 /* Store (Arg1, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002027 acpigen_write_store_ops(ARG1_OP, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002028
2029 /* ACPI SPEC requires to return at maximum 4KiB */
2030 /* If (LGreater (Local1, 0x1000)) */
Felix Held383a06e2023-02-09 16:25:38 +01002031 acpigen_write_if_lgreater_op_int(LOCAL1_OP, 0x1000);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002032
2033 /* Store (0x1000, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002034 acpigen_write_store_int_to_op(0x1000, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002035
2036 /* Pop if */
2037 acpigen_pop_len();
2038
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002039 /* Store (Local1, Local3) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002040 acpigen_write_store_ops(LOCAL1_OP, LOCAL3_OP);
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002041
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002042 /* If (LGreater (Local0, length)) */
Felix Held383a06e2023-02-09 16:25:38 +01002043 acpigen_write_if_lgreater_op_int(LOCAL0_OP, length);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002044
2045 /* Return(Buffer(Local1){0}) */
2046 acpigen_emit_byte(RETURN_OP);
2047 acpigen_emit_byte(BUFFER_OP);
2048 acpigen_write_len_f();
2049 acpigen_emit_byte(LOCAL1_OP);
2050 acpigen_emit_byte(0);
2051 acpigen_pop_len();
2052
2053 /* Pop if */
2054 acpigen_pop_len();
2055
2056 /* If (LGreater (Local0, length - 4096)) */
Felix Held383a06e2023-02-09 16:25:38 +01002057 acpigen_write_if_lgreater_op_int(LOCAL0_OP, length - 4096);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002058
2059 /* Subtract (length, Local0, Local2) */
2060 acpigen_emit_byte(SUBTRACT_OP);
2061 acpigen_write_integer(length);
2062 acpigen_emit_byte(LOCAL0_OP);
2063 acpigen_emit_byte(LOCAL2_OP);
2064
2065 /* If (LGreater (Local1, Local2)) */
Felix Held383a06e2023-02-09 16:25:38 +01002066 acpigen_write_if_lgreater_op_op(LOCAL1_OP, LOCAL2_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002067
2068 /* Store (Local2, Local1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002069 acpigen_write_store_ops(LOCAL2_OP, LOCAL1_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002070
2071 /* Pop if */
2072 acpigen_pop_len();
2073
2074 /* Pop if */
2075 acpigen_pop_len();
2076
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002077 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002078 acpigen_write_name("ROM1");
2079 acpigen_emit_byte(BUFFER_OP);
2080 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02002081 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002082 acpigen_emit_byte(0);
2083 acpigen_pop_len();
2084
2085 /* Multiply (Local1, 0x08, Local1) */
2086 acpigen_emit_byte(MULTIPLY_OP);
2087 acpigen_emit_byte(LOCAL1_OP);
2088 acpigen_write_integer(0x08);
2089 acpigen_emit_byte(LOCAL1_OP);
2090
2091 /* Multiply (Local0, 0x08, Local0) */
2092 acpigen_emit_byte(MULTIPLY_OP);
2093 acpigen_emit_byte(LOCAL0_OP);
2094 acpigen_write_integer(0x08);
2095 acpigen_emit_byte(LOCAL0_OP);
2096
2097 /* CreateField (RBF0, Local0, Local1, TMPB) */
2098 acpigen_emit_ext_op(CREATEFIELD_OP);
2099 acpigen_emit_namestring("RBF0");
2100 acpigen_emit_byte(LOCAL0_OP);
2101 acpigen_emit_byte(LOCAL1_OP);
2102 acpigen_emit_namestring("TMPB");
2103
2104 /* Store (TMPB, ROM1) */
Felix Heldf28f27b2023-02-09 16:26:26 +01002105 acpigen_write_store_namestr_to_namestr("TMPB", "ROM1");
Patrick Rudolph6be6df02017-04-28 16:34:26 +02002106
2107 /* Return (ROM1) */
2108 acpigen_emit_byte(RETURN_OP);
2109 acpigen_emit_namestring("ROM1");
2110
2111 /* Pop method */
2112 acpigen_pop_len();
2113}
2114
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002115/*
2116 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
2117 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
2118 * make callbacks into SoC acpigen code.
2119 *
2120 * Returns 0 on success and -1 on error.
2121 */
Duncan Laurie30c3f912020-10-09 04:48:53 +00002122int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002123{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002124 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002125 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002126 else
2127 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002128}
2129
Duncan Laurie30c3f912020-10-09 04:48:53 +00002130int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002131{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002132 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002133 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
2134 else
2135 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
2136}
Jonathan Zhang71299c22020-01-27 11:38:57 -08002137
Duncan Laurie30c3f912020-10-09 04:48:53 +00002138void acpigen_get_rx_gpio(const struct acpi_gpio *gpio)
Rajat Jain310623b2020-02-26 11:02:53 -08002139{
2140 acpigen_soc_read_rx_gpio(gpio->pins[0]);
2141
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002142 if (gpio->active_low)
Rajat Jain310623b2020-02-26 11:02:53 -08002143 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2144}
2145
Duncan Laurie30c3f912020-10-09 04:48:53 +00002146void acpigen_get_tx_gpio(const struct acpi_gpio *gpio)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002147{
2148 acpigen_soc_get_tx_gpio(gpio->pins[0]);
2149
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002150 if (gpio->active_low)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002151 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2152}
2153
Jonathan Zhang71299c22020-01-27 11:38:57 -08002154/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002155void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran, u16 range_min,
2156 u16 range_max, u16 translation, u16 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002157{
Felix Heldb2f2b532023-05-11 22:22:06 +02002158 /* Byte 0: Type 1, Large Item Value 0x8: Word Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002159 acpigen_emit_byte(0x88);
2160 /* Byte 1+2: length (0x000d) */
2161 acpigen_emit_byte(0x0d);
2162 acpigen_emit_byte(0x00);
2163 /* resource type */
2164 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2165 /* general flags */
2166 acpigen_emit_byte(gen_flags);
2167 /* type flags */
2168 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2169 acpigen_emit_byte(type_flags);
2170 /* granularity, min, max, translation, length */
2171 acpigen_emit_word(gran);
2172 acpigen_emit_word(range_min);
2173 acpigen_emit_word(range_max);
2174 acpigen_emit_word(translation);
2175 acpigen_emit_word(length);
2176}
2177
2178/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002179void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags, u32 gran,
2180 u32 range_min, u32 range_max, u32 translation, u32 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002181{
Felix Heldb2f2b532023-05-11 22:22:06 +02002182 /* Byte 0: Type 1, Large Item Value 0x7: DWord Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002183 acpigen_emit_byte(0x87);
2184 /* Byte 1+2: length (0023) */
2185 acpigen_emit_byte(23);
2186 acpigen_emit_byte(0x00);
2187 /* resource type */
2188 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2189 /* general flags */
2190 acpigen_emit_byte(gen_flags);
2191 /* type flags */
2192 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2193 acpigen_emit_byte(type_flags);
2194 /* granularity, min, max, translation, length */
2195 acpigen_emit_dword(gran);
2196 acpigen_emit_dword(range_min);
2197 acpigen_emit_dword(range_max);
2198 acpigen_emit_dword(translation);
2199 acpigen_emit_dword(length);
2200}
2201
2202static void acpigen_emit_qword(u64 data)
2203{
2204 acpigen_emit_dword(data & 0xffffffff);
2205 acpigen_emit_dword((data >> 32) & 0xffffffff);
2206}
2207
2208/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
Elyes Haouas1f5e1b42022-02-17 17:44:07 +01002209void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags, u64 gran,
2210 u64 range_min, u64 range_max, u64 translation, u64 length)
Jonathan Zhang71299c22020-01-27 11:38:57 -08002211{
Felix Heldb2f2b532023-05-11 22:22:06 +02002212 /* Byte 0: Type 1, Large Item Value 0xa: QWord Address Space Descriptor */
Jonathan Zhang71299c22020-01-27 11:38:57 -08002213 acpigen_emit_byte(0x8a);
2214 /* Byte 1+2: length (0x002b) */
2215 acpigen_emit_byte(0x2b);
2216 acpigen_emit_byte(0x00);
2217 /* resource type */
2218 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2219 /* general flags */
2220 acpigen_emit_byte(gen_flags);
2221 /* type flags */
2222 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2223 acpigen_emit_byte(type_flags);
2224 /* granularity, min, max, translation, length */
2225 acpigen_emit_qword(gran);
2226 acpigen_emit_qword(range_min);
2227 acpigen_emit_qword(range_max);
2228 acpigen_emit_qword(translation);
2229 acpigen_emit_qword(length);
2230}
Furquan Shaikh1a829232020-04-23 11:11:05 -07002231
Felix Held0fdede02023-05-27 01:02:37 +02002232void acpigen_resource_producer_bus_number(u16 bus_base, u16 bus_limit)
Felix Held5a82f0d2023-05-09 16:10:35 +02002233{
2234 acpigen_resource_word(RSRC_TYPE_BUS, /* res_type */
2235 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2236 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002237 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2238 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held5a82f0d2023-05-09 16:10:35 +02002239 BUS_NUM_RANGE_RESOURCE_FLAG, /* type_flags */
2240 0, /* gran */
2241 bus_base, /* range_min */
2242 bus_limit, /* range_max */
2243 0x0, /* translation */
2244 bus_limit - bus_base + 1); /* length */
2245}
2246
Felix Held0fdede02023-05-27 01:02:37 +02002247void acpigen_resource_producer_io(u16 io_base, u16 io_limit)
Felix Held6695be62023-05-09 16:18:51 +02002248{
Felix Helde3c9a042023-06-05 19:59:25 +02002249 acpigen_resource_dword(RSRC_TYPE_IO, /* res_type */
Felix Held6695be62023-05-09 16:18:51 +02002250 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2251 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002252 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2253 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held6695be62023-05-09 16:18:51 +02002254 IO_RSRC_FLAG_ENTIRE_RANGE, /* type_flags */
2255 0, /* gran */
2256 io_base, /* range_min */
2257 io_limit, /* range_max */
2258 0x0, /* translation */
2259 io_limit - io_base + 1); /* length */
2260}
2261
Felix Held0fdede02023-05-27 01:02:37 +02002262static void acpigen_resource_producer_mmio32(u32 mmio_base, u32 mmio_limit, u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002263{
2264 acpigen_resource_dword(RSRC_TYPE_MEM, /* res_type */
2265 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2266 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002267 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2268 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held21594fd12023-05-09 16:39:57 +02002269 type_flags, /* type_flags */
2270 0, /* gran */
2271 mmio_base, /* range_min */
2272 mmio_limit, /* range_max */
2273 0x0, /* translation */
2274 mmio_limit - mmio_base + 1); /* length */
2275}
2276
Felix Held0fdede02023-05-27 01:02:37 +02002277static void acpigen_resource_producer_mmio64(u64 mmio_base, u64 mmio_limit, u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002278{
2279 acpigen_resource_qword(RSRC_TYPE_MEM, /* res_type */
2280 ADDR_SPACE_GENERAL_FLAG_MAX_FIXED
2281 | ADDR_SPACE_GENERAL_FLAG_MIN_FIXED
Felix Held0fdede02023-05-27 01:02:37 +02002282 | ADDR_SPACE_GENERAL_FLAG_DEC_POS
2283 | ADDR_SPACE_GENERAL_FLAG_PRODUCER, /* gen_flags */
Felix Held21594fd12023-05-09 16:39:57 +02002284 type_flags, /* type_flags */
2285 0, /* gran */
2286 mmio_base, /* range_min */
2287 mmio_limit, /* range_max */
2288 0x0, /* translation */
2289 mmio_limit - mmio_base + 1); /* length */
2290}
2291
Felix Held0fdede02023-05-27 01:02:37 +02002292void acpigen_resource_producer_mmio(u64 mmio_base, u64 mmio_limit, u16 type_flags)
Felix Held21594fd12023-05-09 16:39:57 +02002293{
2294 if (mmio_base < 4ULL * GiB && mmio_limit < 4ULL * GiB)
Felix Held0fdede02023-05-27 01:02:37 +02002295 acpigen_resource_producer_mmio32(mmio_base, mmio_limit, type_flags);
Felix Held21594fd12023-05-09 16:39:57 +02002296 else
Felix Held0fdede02023-05-27 01:02:37 +02002297 acpigen_resource_producer_mmio64(mmio_base, mmio_limit, type_flags);
Felix Held21594fd12023-05-09 16:39:57 +02002298}
2299
Furquan Shaikh1a829232020-04-23 11:11:05 -07002300void acpigen_write_ADR(uint64_t adr)
2301{
2302 acpigen_write_name_qword("_ADR", adr);
2303}
2304
Duncan Laurie08a942f2020-04-29 12:13:14 -07002305/**
2306 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2307 * @address: SoundWire device address properties.
2308 *
2309 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2310 *
2311 * 63..52 - Reserved (0)
2312 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2313 * Used when a Controller has multiple master devices, each producing a
2314 * separate SoundWire Link. Set to 0 for single-link controllers.
2315 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2316 * 47..44 - SoundWire specification version that this device supports
2317 * 43..40 - Unique ID for multiple devices
2318 * 39..24 - MIPI standard manufacturer code
2319 * 23..08 - Vendor defined part ID
2320 * 07..00 - MIPI class encoding
2321 */
2322void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
2323{
2324 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
2325 (((uint64_t)address->version & 0xf) << 44) |
2326 (((uint64_t)address->unique_id & 0xf) << 40) |
2327 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
2328 (((uint64_t)address->part_id & 0xffff) << 8) |
2329 (((uint64_t)address->class & 0xff)));
2330}
Tim Wawrzynczakf6945022020-06-04 15:18:47 -06002331
2332void acpigen_notify(const char *namestr, int value)
2333{
2334 acpigen_emit_byte(NOTIFY_OP);
2335 acpigen_emit_namestring(namestr);
2336 acpigen_write_integer(value);
2337}
Aaron Durbin80bc0912020-08-19 23:17:42 -06002338
2339static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name)
2340{
2341 acpigen_emit_byte(aml_op);
2342 acpigen_emit_byte(srcop);
2343 acpigen_write_integer(byte_offset);
2344 acpigen_emit_namestring(name);
2345}
2346
2347void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name)
2348{
2349 _create_field(CREATE_BYTE_OP, op, byte_offset, name);
2350}
2351
2352void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name)
2353{
2354 _create_field(CREATE_WORD_OP, op, byte_offset, name);
2355}
2356
2357void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name)
2358{
2359 _create_field(CREATE_DWORD_OP, op, byte_offset, name);
2360}
2361
2362void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name)
2363{
2364 _create_field(CREATE_QWORD_OP, op, byte_offset, name);
2365}
Jason Gleneskca36aed2020-09-15 21:01:57 -07002366
2367void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
2368{
2369 acpigen_write_name("_PCT");
2370 acpigen_write_package(0x02);
2371 acpigen_write_register_resource(perf_ctrl);
2372 acpigen_write_register_resource(perf_sts);
2373
2374 acpigen_pop_len();
2375}
2376
2377void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value)
2378{
2379 acpigen_write_package(0x08);
2380 acpigen_write_dword(pstate_value->core_freq);
2381 acpigen_write_dword(pstate_value->power);
2382 acpigen_write_dword(pstate_value->transition_latency);
2383 acpigen_write_dword(pstate_value->bus_master_latency);
2384
2385 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t));
2386 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t));
2387 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t));
2388 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t));
2389
2390 acpigen_pop_len();
2391}
2392
2393void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
2394{
2395 size_t pstate;
2396
2397 acpigen_write_name("XPSS");
2398 acpigen_write_package(nentries);
2399 for (pstate = 0; pstate < nentries; pstate++) {
2400 acpigen_write_xpss_package(pstate_values);
2401 pstate_values++;
2402 }
2403
2404 acpigen_pop_len();
2405}
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002406
2407/* Delay up to wait_ms until provided namestr matches expected value. */
2408void acpigen_write_delay_until_namestr_int(uint32_t wait_ms, const char *name, uint64_t value)
2409{
2410 uint32_t wait_ms_segment = 1;
2411 uint32_t segments = wait_ms;
2412
2413 /* Sleep in 16ms segments if delay is more than 32ms. */
2414 if (wait_ms > 32) {
2415 wait_ms_segment = 16;
2416 segments = wait_ms / 16;
2417 }
2418
2419 acpigen_write_store_int_to_op(segments, LOCAL7_OP);
2420 acpigen_emit_byte(WHILE_OP);
2421 acpigen_write_len_f();
2422 acpigen_emit_byte(LGREATER_OP);
2423 acpigen_emit_byte(LOCAL7_OP);
2424 acpigen_emit_byte(ZERO_OP);
2425
2426 /* If name is not provided then just delay in a loop. */
2427 if (name) {
2428 acpigen_write_if_lequal_namestr_int(name, value);
2429 acpigen_emit_byte(BREAK_OP);
2430 acpigen_pop_len(); /* If */
2431 }
2432
2433 acpigen_write_sleep(wait_ms_segment);
2434 acpigen_emit_byte(DECREMENT_OP);
2435 acpigen_emit_byte(LOCAL7_OP);
2436 acpigen_pop_len(); /* While */
Cliff Huangf97598f2023-03-01 14:30:41 -08002437
2438 if (name) {
2439 acpigen_write_if_lequal_op_op(LOCAL7_OP, ZERO_OP);
2440 acpigen_write_debug_sprintf("WARN: Wait loop timeout for variable %s",
2441 name);
2442 acpigen_pop_len(); /* If */
2443 }
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002444}
Arthur Heymans0eb59742023-04-25 16:23:37 +02002445
2446void acpigen_ssdt_override_sleep_states(bool enable_s1, bool enable_s2, bool enable_s3,
2447 bool enable_s4)
2448{
2449 assert(!(enable_s1 && CONFIG(ACPI_S1_NOT_SUPPORTED)));
2450 assert(!(enable_s3 && !CONFIG(HAVE_ACPI_RESUME)));
2451 assert(!(enable_s4 && CONFIG(DISABLE_ACPI_HIBERNATE)));
2452
2453 acpigen_write_scope("\\");
2454 uint32_t sleep_enable = (enable_s1 << 0) | (enable_s2 << 1)
2455 | (enable_s3 << 2) | (enable_s4 << 3);
2456 acpigen_write_name_dword("OSFG", sleep_enable);
2457 acpigen_pop_len();
2458}