blob: 4d9395fab7c01ff11015336b0bad743994068a2a [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
Michael Niewöhnerb20aac02020-10-14 19:30:46 +020013#define CPPC_PACKAGE_NAME "GCPC"
14
Duncan Laurie3829f232016-05-09 11:08:46 -070015#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000016#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070017#include <acpi/acpigen.h>
Elyes HAOUAScd4fe0f2019-03-29 17:12:15 +010018#include <assert.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000019#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000020#include <device/device.h>
Furquan Shaikh1a829232020-04-23 11:11:05 -070021#include <device/pci_def.h>
22#include <device/pci_type.h>
Duncan Laurie08a942f2020-04-29 12:13:14 -070023#include <device/soundwire.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010024#include <types.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000025
26static char *gencurrent;
27
28char *len_stack[ACPIGEN_LENSTACK_SIZE];
29int ltop = 0;
30
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010031void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000032{
33 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010034 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000035 acpigen_emit_byte(0);
36 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050037 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000038}
39
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010040void acpigen_pop_len(void)
41{
42 int len;
43 ASSERT(ltop > 0)
44 char *p = len_stack[--ltop];
45 len = gencurrent - p;
46 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050047 /* generate store length for 0xfffff max */
48 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010049 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050050 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010051
52}
53
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000054void acpigen_set_current(char *curr)
55{
56 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000057}
58
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000059char *acpigen_get_current(void)
60{
61 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000062}
63
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010064void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000065{
66 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000067}
68
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070069void acpigen_emit_ext_op(uint8_t op)
70{
71 acpigen_emit_byte(EXT_OP_PREFIX);
72 acpigen_emit_byte(op);
73}
74
Duncan Laurie9ccae752016-05-09 07:43:19 -070075void acpigen_emit_word(unsigned int data)
76{
77 acpigen_emit_byte(data & 0xff);
78 acpigen_emit_byte((data >> 8) & 0xff);
79}
80
81void acpigen_emit_dword(unsigned int data)
82{
83 acpigen_emit_byte(data & 0xff);
84 acpigen_emit_byte((data >> 8) & 0xff);
85 acpigen_emit_byte((data >> 16) & 0xff);
86 acpigen_emit_byte((data >> 24) & 0xff);
87}
88
Duncan Laurie85d80272016-07-02 19:53:54 -070089char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000090{
Duncan Laurie85d80272016-07-02 19:53:54 -070091 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070092 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010093 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -070094 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +000095 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -070096 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +000097}
98
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010099void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000100{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700101 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000102 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000103}
104
Duncan Laurie9ccae752016-05-09 07:43:19 -0700105void acpigen_write_word(unsigned int data)
106{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700107 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700108 acpigen_emit_word(data);
109}
110
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100111void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000112{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700113 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700114 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000115}
116
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100117void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000118{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700119 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700120 acpigen_emit_dword(data & 0xffffffff);
121 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000122}
123
Duncan Laurief7c38762016-05-09 08:20:38 -0700124void acpigen_write_zero(void)
125{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700126 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700127}
128
129void acpigen_write_one(void)
130{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700131 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700132}
133
134void acpigen_write_ones(void)
135{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700136 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700137}
138
139void acpigen_write_integer(uint64_t data)
140{
141 if (data == 0)
142 acpigen_write_zero();
143 else if (data == 1)
144 acpigen_write_one();
145 else if (data <= 0xff)
146 acpigen_write_byte((unsigned char)data);
147 else if (data <= 0xffff)
148 acpigen_write_word((unsigned int)data);
149 else if (data <= 0xffffffff)
150 acpigen_write_dword((unsigned int)data);
151 else
152 acpigen_write_qword(data);
153}
154
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100155void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000156{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100157 acpigen_write_name(name);
158 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000159}
160
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100161void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000162{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100163 acpigen_write_name(name);
164 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000165}
166
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100167void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000168{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100169 acpigen_write_name(name);
170 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000171}
172
Duncan Laurief7c38762016-05-09 08:20:38 -0700173void acpigen_write_name_integer(const char *name, uint64_t val)
174{
175 acpigen_write_name(name);
176 acpigen_write_integer(val);
177}
178
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700179void acpigen_write_name_string(const char *name, const char *string)
180{
181 acpigen_write_name(name);
182 acpigen_write_string(string);
183}
184
Patrick Rudolph389c8272019-12-17 13:54:41 +0100185void acpigen_write_name_unicode(const char *name, const char *string)
186{
187 const size_t len = strlen(string) + 1;
188 acpigen_write_name(name);
189 acpigen_emit_byte(BUFFER_OP);
190 acpigen_write_len_f();
191 acpigen_write_integer(len);
192 for (size_t i = 0; i < len; i++) {
193 const char c = string[i];
194 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
195 acpigen_emit_word(c >= 0 ? c : '?');
196 }
197 acpigen_pop_len();
198}
199
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100200void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000201{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000202 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700203 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000204 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000205}
206
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700207void acpigen_emit_string(const char *string)
208{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200209 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700210 acpigen_emit_byte('\0'); /* NUL */
211}
212
213void acpigen_write_string(const char *string)
214{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700215 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700216 acpigen_emit_string(string);
217}
218
Duncan Laurie37319032016-05-26 12:47:05 -0700219void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
220{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800221 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700222
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700223 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700224 acpigen_write_name_string("_HID", hid);
225}
226
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100227/*
228 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600229 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
230 * and "By convention, when an ASL compiler pads a name shorter than 4
231 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100232 *
233 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
234 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000235
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700236static void acpigen_emit_simple_namestring(const char *name)
237{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100238 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000239 char ud[] = "____";
240 for (i = 0; i < 4; i++) {
241 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100242 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000243 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000244 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700245 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000246 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000247}
248
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700249static void acpigen_emit_double_namestring(const char *name, int dotpos)
250{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700251 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100252 acpigen_emit_simple_namestring(name);
253 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000254}
255
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700256static void acpigen_emit_multi_namestring(const char *name)
257{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100258 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000259 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700260 acpigen_emit_byte(MULTI_NAME_PREFIX);
261 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000262 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
263
264 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100265 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000266 /* find end or next entity */
267 while ((name[0] != '.') && (name[0] != '\0'))
268 name++;
269 /* forward to next */
270 if (name[0] == '.')
271 name++;
272 count++;
273 }
274
275 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000276}
277
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700278void acpigen_emit_namestring(const char *namepath)
279{
Rudolf Marek3310d752009-06-21 20:26:13 +0000280 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000281 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000282
Ronak Kanabar7c0f0072020-11-30 15:40:51 +0530283 /* Check for NULL pointer */
284 if (!namepath)
285 return;
286
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600287 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000288 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100289 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000290 namepath++;
291 }
292
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600293 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000294 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100295 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000296 namepath++;
297 }
298
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200299 /* If we have only \\ or only ^...^. Then we need to put a null
300 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200301 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700302 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100303 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200304 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000305
306 i = 0;
307 while (namepath[i] != '\0') {
308 if (namepath[i] == '.') {
309 dotcount++;
310 dotpos = i;
311 }
312 i++;
313 }
314
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700315 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100316 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700317 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100318 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700319 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100320 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000321}
322
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100323void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000324{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700325 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100326 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000327}
328
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100329void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000330{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700331 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100332 acpigen_write_len_f();
333 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000334}
Rudolf Marekf997b552009-02-14 15:40:23 +0000335
Duncan Laurie2fe74712020-06-01 17:36:56 -0700336void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
337{
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800338 /* <dest_op> = DeRefOf (<package_op>[<element>]) */
Duncan Laurie2fe74712020-06-01 17:36:56 -0700339 acpigen_write_store();
340 acpigen_emit_byte(DEREF_OP);
341 acpigen_emit_byte(INDEX_OP);
342 acpigen_emit_byte(package_op);
343 acpigen_write_integer(element);
344 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
345 acpigen_emit_byte(dest_op);
346}
347
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800348void acpigen_set_package_op_element_int(uint8_t package_op, unsigned int element, uint64_t src)
349{
350 /* DeRefOf (<package>[<element>]) = <src> */
351 acpigen_write_store();
352 acpigen_write_integer(src);
353 acpigen_emit_byte(DEREF_OP);
354 acpigen_emit_byte(INDEX_OP);
355 acpigen_emit_byte(package_op);
356 acpigen_write_integer(element);
357 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
358}
359
360void acpigen_get_package_element(const char *package, unsigned int element, uint8_t dest_op)
361{
362 /* <dest_op> = <package>[<element>] */
363 acpigen_write_store();
364 acpigen_emit_byte(INDEX_OP);
365 acpigen_emit_namestring(package);
366 acpigen_write_integer(element);
367 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
368 acpigen_emit_byte(dest_op);
369}
370
371void acpigen_set_package_element_int(const char *package, unsigned int element, uint64_t src)
372{
373 /* <package>[<element>] = <src> */
374 acpigen_write_store();
375 acpigen_write_integer(src);
376 acpigen_emit_byte(INDEX_OP);
377 acpigen_emit_namestring(package);
378 acpigen_write_integer(element);
379 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
380}
381
382void acpigen_set_package_element_namestr(const char *package, unsigned int element,
383 const char *src)
384{
385 /* <package>[<element>] = <src> */
386 acpigen_write_store();
387 acpigen_emit_namestring(src);
388 acpigen_emit_byte(INDEX_OP);
389 acpigen_emit_namestring(package);
390 acpigen_write_integer(element);
391 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
392}
393
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100394void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000395{
396/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100397 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200398 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000399*/
400 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700401 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100402 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000403
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200404 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600405 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100406 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000407 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700408 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000409 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000410}
411
Nico Huber4d211ac2017-09-01 21:14:36 +0200412void acpigen_write_processor_package(const char *const name,
413 const unsigned int first_core,
414 const unsigned int core_count)
415{
416 unsigned int i;
417 char pscope[16];
418
419 acpigen_write_name(name);
420 acpigen_write_package(core_count);
421 for (i = first_core; i < first_core + core_count; ++i) {
422 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
423 acpigen_emit_namestring(pscope);
424 }
425 acpigen_pop_len();
426}
427
Arthur Heymans8f055272018-11-28 13:18:57 +0100428/* Method to notify all CPU cores */
429void acpigen_write_processor_cnot(const unsigned int number_of_cores)
430{
431 int core_id;
432
Christian Walterbe3979c2019-12-18 15:07:59 +0100433 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100434 for (core_id = 0; core_id < number_of_cores; core_id++) {
435 char buffer[DEVICE_PATH_MAX];
436 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
437 core_id);
438 acpigen_emit_byte(NOTIFY_OP);
439 acpigen_emit_namestring(buffer);
440 acpigen_emit_byte(ARG0_OP);
441 }
442 acpigen_pop_len();
443}
444
Naresh G Solanki8535c662016-10-25 00:51:24 +0530445/*
446 * Generate ACPI AML code for OperationRegion
447 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
448 * where rname is region name, space is region space, offset is region offset &
449 * len is region length.
450 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
451 */
Duncan Laurie35029602020-10-09 17:50:25 +0000452void acpigen_write_opregion(const struct opregion *opreg)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530453{
454 /* OpregionOp */
455 acpigen_emit_ext_op(OPREGION_OP);
456 /* NameString 4 chars only */
457 acpigen_emit_simple_namestring(opreg->name);
458 /* RegionSpace */
459 acpigen_emit_byte(opreg->regionspace);
460 /* RegionOffset & RegionLen, it can be byte word or double word */
461 acpigen_write_integer(opreg->regionoffset);
462 acpigen_write_integer(opreg->regionlen);
463}
464
Patrick Rudolph32bae492019-08-08 12:47:30 +0200465/*
466 * Generate ACPI AML code for Mutex
467 * Arg0: Pointer to name of mutex
468 * Arg1: Initial value of mutex
469 */
470void acpigen_write_mutex(const char *name, const uint8_t flags)
471{
472 /* MutexOp */
473 acpigen_emit_ext_op(MUTEX_OP);
474 /* NameString 4 chars only */
475 acpigen_emit_simple_namestring(name);
476 acpigen_emit_byte(flags);
477}
478
479void acpigen_write_acquire(const char *name, const uint16_t val)
480{
481 /* AcquireOp */
482 acpigen_emit_ext_op(ACQUIRE_OP);
483 /* NameString 4 chars only */
484 acpigen_emit_simple_namestring(name);
485 acpigen_emit_word(val);
486}
487
488void acpigen_write_release(const char *name)
489{
490 /* ReleaseOp */
491 acpigen_emit_ext_op(RELEASE_OP);
492 /* NameString 4 chars only */
493 acpigen_emit_simple_namestring(name);
494}
495
Patrick Rudolph894977b2017-07-01 16:15:05 +0200496static void acpigen_write_field_length(uint32_t len)
497{
498 uint8_t i, j;
499 uint8_t emit[4];
500
501 i = 1;
502 if (len < 0x40) {
503 emit[0] = len & 0x3F;
504 } else {
505 emit[0] = len & 0xF;
506 len >>= 4;
507 while (len) {
508 emit[i] = len & 0xFF;
509 i++;
510 len >>= 8;
511 }
512 }
513 /* Update bit 7:6 : Number of bytes followed by emit[0] */
514 emit[0] |= (i - 1) << 6;
515
516 for (j = 0; j < i; j++)
517 acpigen_emit_byte(emit[j]);
518}
519
Naresh G Solanki8535c662016-10-25 00:51:24 +0530520static void acpigen_write_field_offset(uint32_t offset,
521 uint32_t current_bit_pos)
522{
523 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530524
525 if (offset < current_bit_pos) {
526 printk(BIOS_WARNING, "%s: Cannot move offset backward",
527 __func__);
528 return;
529 }
530
531 diff_bits = offset - current_bit_pos;
532 /* Upper limit */
533 if (diff_bits > 0xFFFFFFF) {
534 printk(BIOS_WARNING, "%s: Offset very large to encode",
535 __func__);
536 return;
537 }
538
Naresh G Solanki8535c662016-10-25 00:51:24 +0530539 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200540 acpigen_write_field_length(diff_bits);
541}
542
543static void acpigen_write_field_name(const char *name, uint32_t size)
544{
545 acpigen_emit_simple_namestring(name);
546 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530547}
548
Duncan Laurie095bbf92020-09-30 23:09:29 +0000549static void acpigen_write_field_reserved(uint32_t size)
550{
551 acpigen_emit_byte(0);
552 acpigen_write_field_length(size);
553}
554
Naresh G Solanki8535c662016-10-25 00:51:24 +0530555/*
556 * Generate ACPI AML code for Field
557 * Arg0: region name
558 * Arg1: Pointer to struct fieldlist.
559 * Arg2: no. of entries in Arg1
560 * Arg3: flags which indicate filed access type, lock rule & update rule.
561 * Example with fieldlist
562 * struct fieldlist l[] = {
563 * FIELDLIST_OFFSET(0x84),
564 * FIELDLIST_NAMESTR("PMCS", 2),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000565 * FIELDLIST_RESERVED(6),
Naresh G Solanki8535c662016-10-25 00:51:24 +0530566 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200567 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530568 * FIELD_PRESERVE);
569 * Output:
570 * Field (UART, AnyAcc, NoLock, Preserve)
571 * {
572 * Offset (0x84),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000573 * PMCS, 2,
574 * , 6,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530575 * }
576 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700577void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530578 uint8_t flags)
579{
580 uint16_t i;
581 uint32_t current_bit_pos = 0;
582
583 /* FieldOp */
584 acpigen_emit_ext_op(FIELD_OP);
585 /* Package Length */
586 acpigen_write_len_f();
587 /* NameString 4 chars only */
588 acpigen_emit_simple_namestring(name);
589 /* Field Flag */
590 acpigen_emit_byte(flags);
591
592 for (i = 0; i < count; i++) {
593 switch (l[i].type) {
594 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200595 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530596 current_bit_pos += l[i].bits;
597 break;
Duncan Laurie095bbf92020-09-30 23:09:29 +0000598 case RESERVED:
599 acpigen_write_field_reserved(l[i].bits);
600 current_bit_pos += l[i].bits;
601 break;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530602 case OFFSET:
603 acpigen_write_field_offset(l[i].bits, current_bit_pos);
604 current_bit_pos = l[i].bits;
605 break;
606 default:
607 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
608 , __func__, l[i].type);
609 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 */
636void acpigen_write_indexfield(const char *idx, const char *data,
637 struct fieldlist *l, size_t count, uint8_t flags)
638{
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:
664 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
665 , __func__, l[i].type);
666 break;
667 }
668 }
669 acpigen_pop_len();
670}
671
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100672void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000673{
674/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200675 Name (_PCT, Package (0x02)
676 {
677 ResourceTemplate ()
678 {
679 Register (FFixedHW,
680 0x00, // Bit Width
681 0x00, // Bit Offset
682 0x0000000000000000, // Address
683 ,)
684 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000685
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200686 ResourceTemplate ()
687 {
688 Register (FFixedHW,
689 0x00, // Bit Width
690 0x00, // Bit Offset
691 0x0000000000000000, // Address
692 ,)
693 }
694 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000695*/
696 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700697 /* 00000030 "0._PCT.," */
698 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
699 /* 00000038 "........" */
700 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
701 /* 00000040 "........" */
702 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
703 /* 00000048 "....y..." */
704 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
705 /* 00000050 "........" */
706 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
707 /* 00000058 "........" */
708 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000709 0x00, 0x79, 0x00
710 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100711 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000712}
713
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100714void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200715{
716/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200717 Name (_PTC, Package (0x02)
718 {
719 ResourceTemplate ()
720 {
721 Register (FFixedHW,
722 0x00, // Bit Width
723 0x00, // Bit Offset
724 0x0000000000000000, // Address
725 ,)
726 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200727
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200728 ResourceTemplate ()
729 {
730 Register (FFixedHW,
731 0x00, // Bit Width
732 0x00, // Bit Offset
733 0x0000000000000000, // Address
734 ,)
735 }
736 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200737*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200738 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100739 .space_id = ACPI_ADDRESS_SPACE_FIXED,
740 .bit_width = 0,
741 .bit_offset = 0,
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +0200742 .access_size = ACPI_ACCESS_SIZE_UNDEFINED,
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100743 .addrl = 0,
744 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200745 };
746
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100747 acpigen_write_name("_PTC");
748 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200749
750 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700751 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200752
753 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700754 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200755
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100756 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200757}
758
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700759static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100760{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700761 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100762 acpigen_write_len_f();
763 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700764 acpigen_emit_byte(flags);
765}
766
767/* Method (name, nargs, NotSerialized) */
768void acpigen_write_method(const char *name, int nargs)
769{
770 __acpigen_write_method(name, (nargs & 7));
771}
772
773/* Method (name, nargs, Serialized) */
774void acpigen_write_method_serialized(const char *name, int nargs)
775{
776 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100777}
778
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100779void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100780{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700781 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100782 acpigen_write_len_f();
783 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100784}
785
Duncan Laurieabe2de82016-05-09 11:08:46 -0700786void acpigen_write_STA(uint8_t status)
787{
788 /*
789 * Method (_STA, 0, NotSerialized) { Return (status) }
790 */
791 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700792 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700793 acpigen_write_byte(status);
794 acpigen_pop_len();
795}
796
Sugnan Prabhu S4f77f612020-06-10 09:51:02 +0530797void acpigen_write_STA_ext(const char *namestring)
798{
799 /*
800 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
801 */
802 acpigen_write_method("_STA", 0);
803 acpigen_emit_byte(RETURN_OP);
804 acpigen_emit_namestring(namestring);
805 acpigen_pop_len();
806}
807
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100808/*
809 * Generates a func with max supported P-states.
810 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100811void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000812{
813/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200814 Method (_PPC, 0, NotSerialized)
815 {
816 Return (nr)
817 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000818*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100819 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700820 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000821 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100822 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100823 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000824}
825
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100826/*
827 * Generates a func with max supported P-states saved
828 * in the variable PPCM.
829 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100830void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700831{
832/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200833 Method (_PPC, 0, NotSerialized)
834 {
835 Return (PPCM)
836 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700837*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100838 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700839 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700840 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100841 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100842 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700843}
844
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100845void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200846{
847/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200848 // Sample _TPC method
849 Method (_TPC, 0, NotSerialized)
850 {
851 Return (\TLVL)
852 }
853*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100854 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700855 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100856 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100857 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200858}
859
Duncan Laurieabe2de82016-05-09 11:08:46 -0700860void acpigen_write_PRW(u32 wake, u32 level)
861{
862 /*
863 * Name (_PRW, Package () { wake, level }
864 */
865 acpigen_write_name("_PRW");
866 acpigen_write_package(2);
867 acpigen_write_integer(wake);
868 acpigen_write_integer(level);
869 acpigen_pop_len();
870}
871
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100872void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000873 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000874{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100875 acpigen_write_package(6);
876 acpigen_write_dword(coreFreq);
877 acpigen_write_dword(power);
878 acpigen_write_dword(transLat);
879 acpigen_write_dword(busmLat);
880 acpigen_write_dword(control);
881 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100882 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700883
884 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
885 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000886}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000887
Jason Gleneskca36aed2020-09-15 21:01:57 -0700888void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
889{
890 size_t pstate;
891
892 acpigen_write_name("_PSS");
893 acpigen_write_package(nentries);
894 for (pstate = 0; pstate < nentries; pstate++) {
895 acpigen_write_PSS_package(
896 pstate_values->core_freq, pstate_values->power,
897 pstate_values->transition_latency, pstate_values->bus_master_latency,
898 pstate_values->control_value, pstate_values->status_value);
899 pstate_values++;
900 }
901
902 acpigen_pop_len();
903}
904
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100905void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000906{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100907 acpigen_write_name("_PSD");
908 acpigen_write_package(1);
909 acpigen_write_package(5);
910 acpigen_write_byte(5); // 5 values
911 acpigen_write_byte(0); // revision 0
912 acpigen_write_dword(domain);
913 acpigen_write_dword(coordtype);
914 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100915 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100916 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000917}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000918
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100919void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200920{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100921 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700922 acpigen_write_register_resource(&cstate->resource);
Jason Glenesk201acca2020-09-11 12:36:15 -0700923 acpigen_write_byte(cstate->ctype);
924 acpigen_write_word(cstate->latency);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100925 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100926 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200927}
928
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100929void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200930{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100931 int i;
932 acpigen_write_name("_CST");
933 acpigen_write_package(nentries+1);
Jason Glenesk201acca2020-09-11 12:36:15 -0700934 acpigen_write_integer(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200935
936 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100937 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200938
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100939 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200940}
941
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700942void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
943 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500944{
945 acpigen_write_name("_CSD");
946 acpigen_write_package(1);
947 acpigen_write_package(6);
Jason Glenesk201acca2020-09-11 12:36:15 -0700948 acpigen_write_integer(6); // 6 values
Timothy Pearson83abd812015-06-08 19:35:06 -0500949 acpigen_write_byte(0); // revision 0
950 acpigen_write_dword(domain);
951 acpigen_write_dword(coordtype);
952 acpigen_write_dword(numprocs);
953 acpigen_write_dword(index);
954 acpigen_pop_len();
955 acpigen_pop_len();
956}
957
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100958void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200959{
960/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200961 Sample _TSS package with 100% and 50% duty cycles
962 Name (_TSS, Package (0x02)
963 {
964 Package(){100, 1000, 0, 0x00, 0)
965 Package(){50, 520, 0, 0x18, 0)
966 })
967*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100968 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200969 acpi_tstate_t *tstate = tstate_list;
970
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100971 acpigen_write_name("_TSS");
972 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200973
974 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100975 acpigen_write_package(5);
976 acpigen_write_dword(tstate->percent);
977 acpigen_write_dword(tstate->power);
978 acpigen_write_dword(tstate->latency);
979 acpigen_write_dword(tstate->control);
980 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100981 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200982 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200983 }
984
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100985 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200986}
987
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100988void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200989{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100990 acpigen_write_name("_TSD");
991 acpigen_write_package(1);
992 acpigen_write_package(5);
993 acpigen_write_byte(5); // 5 values
994 acpigen_write_byte(0); // revision 0
995 acpigen_write_dword(domain);
996 acpigen_write_dword(coordtype);
997 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100998 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100999 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001000}
1001
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001002void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001003{
1004 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001005 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001006 * Byte 0:
1007 * Bit7 : 1 => big item
1008 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
1009 */
1010 acpigen_emit_byte(0x86);
1011 /* Byte 1+2: length (0x0009) */
1012 acpigen_emit_byte(0x09);
1013 acpigen_emit_byte(0x00);
1014 /* bit1-7 are ignored */
1015 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -07001016 acpigen_emit_dword(base);
1017 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001018}
1019
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001020static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001021{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +02001022 acpigen_emit_byte(0x82); /* Register Descriptor */
1023 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
1024 acpigen_emit_byte(0x00); /* Register Length 15:8 */
1025 acpigen_emit_byte(addr->space_id); /* Address Space ID */
1026 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
1027 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +01001028 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -07001029 acpigen_emit_dword(addr->addrl); /* Register Address Low */
1030 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +02001031}
1032
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001033void acpigen_write_register_resource(const acpi_addr_t *addr)
1034{
1035 acpigen_write_resourcetemplate_header();
1036 acpigen_write_register(addr);
1037 acpigen_write_resourcetemplate_footer();
1038}
1039
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001040void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001041{
1042 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001043 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001044 * Byte 0:
1045 * Bit7 : 0 => small item
1046 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1047 * Bit2-0: 010 (0x2) => 2 Bytes long
1048 */
1049 acpigen_emit_byte(0x22);
1050 acpigen_emit_byte(mask & 0xff);
1051 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001052}
1053
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001054void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001055{
1056 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001057 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001058 * Byte 0:
1059 * Bit7 : 0 => small item
1060 * Bit6-3: 1000 (0x8) => I/O port descriptor
1061 * Bit2-0: 111 (0x7) => 7 Bytes long
1062 */
1063 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001064 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001065 /* bit1-7 are ignored */
1066 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1067 /* minimum base address the device may be configured for */
1068 acpigen_emit_byte(min & 0xff);
1069 acpigen_emit_byte((min >> 8) & 0xff);
1070 /* maximum base address the device may be configured for */
1071 acpigen_emit_byte(max & 0xff);
1072 acpigen_emit_byte((max >> 8) & 0xff);
1073 /* alignment for min base */
1074 acpigen_emit_byte(align & 0xff);
1075 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001076}
1077
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001078void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001079{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001080 /*
1081 * A ResourceTemplate() is a Buffer() with a
1082 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001083 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001084 * (small item 0xf, len 1)
1085 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001086 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001087 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001088 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001089 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001090 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001091 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001092 acpigen_emit_byte(0x00);
1093 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001094}
1095
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001096void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001097{
1098 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001099 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001100 /*
1101 * end tag (acpi 4.0 Section 6.4.2.8)
1102 * 0x79 <checksum>
1103 * 0x00 is treated as a good checksum according to the spec
1104 * and is what iasl generates.
1105 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001106 acpigen_emit_byte(0x79);
1107 acpigen_emit_byte(0x00);
1108
Nico Huber7d89ce32017-04-14 00:08:18 +02001109 /* Start counting past the 2-bytes length added in
1110 acpigen_write_resourcetemplate() above. */
1111 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001112
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001113 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001114 p[0] = len & 0xff;
1115 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001116 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001117 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001118}
1119
1120static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1121 struct resource *res)
1122{
1123 acpigen_write_mem32fixed(0, res->base, res->size);
1124}
1125
1126static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1127 struct resource *res)
1128{
1129 resource_t base = res->base;
1130 resource_t size = res->size;
1131 while (size > 0) {
1132 resource_t sz = size > 255 ? 255 : size;
1133 acpigen_write_io16(base, base, 0, sz, 1);
1134 size -= sz;
1135 base += sz;
1136 }
1137}
1138
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001139void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001140{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001141 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001142
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001143 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001144 search_global_resources(
1145 IORESOURCE_MEM | IORESOURCE_RESERVE,
1146 IORESOURCE_MEM | IORESOURCE_RESERVE,
1147 acpigen_add_mainboard_rsvd_mem32, 0);
1148
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001149 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001150 search_global_resources(
1151 IORESOURCE_IO | IORESOURCE_RESERVE,
1152 IORESOURCE_IO | IORESOURCE_RESERVE,
1153 acpigen_add_mainboard_rsvd_io, 0);
1154
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001155 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001156}
1157
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001158void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001159{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001160 acpigen_write_scope(scope);
1161 acpigen_write_name(name);
1162 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001163 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001164}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001165
1166static int hex2bin(const char c)
1167{
1168 if (c >= 'A' && c <= 'F')
1169 return c - 'A' + 10;
1170 if (c >= 'a' && c <= 'f')
1171 return c - 'a' + 10;
1172 return c - '0';
1173}
1174
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001175void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001176{
1177 u32 compact = 0;
1178
1179 /* Clamping individual values would be better but
1180 there is a disagreement over what is a valid
1181 EISA id, so accept anything and don't clamp,
1182 parent code should create a valid EISAid.
1183 */
1184 compact |= (eisaid[0] - 'A' + 1) << 26;
1185 compact |= (eisaid[1] - 'A' + 1) << 21;
1186 compact |= (eisaid[2] - 'A' + 1) << 16;
1187 compact |= hex2bin(eisaid[3]) << 12;
1188 compact |= hex2bin(eisaid[4]) << 8;
1189 compact |= hex2bin(eisaid[5]) << 4;
1190 compact |= hex2bin(eisaid[6]);
1191
1192 acpigen_emit_byte(0xc);
1193 acpigen_emit_byte((compact >> 24) & 0xff);
1194 acpigen_emit_byte((compact >> 16) & 0xff);
1195 acpigen_emit_byte((compact >> 8) & 0xff);
1196 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001197}
Duncan Laurie3829f232016-05-09 11:08:46 -07001198
1199/*
1200 * ToUUID(uuid)
1201 *
1202 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1203 * order for the bytes that make up a UUID Buffer object.
1204 * UUID byte order for input:
1205 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1206 * UUID byte order for output:
1207 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1208 */
1209#define UUID_LEN 16
1210void acpigen_write_uuid(const char *uuid)
1211{
1212 uint8_t buf[UUID_LEN];
1213 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1214 8, 9, 10, 11, 12, 13, 14, 15 };
1215
1216 /* Parse UUID string into bytes */
1217 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1218 return;
1219
1220 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001221 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001222 acpigen_write_len_f();
1223
1224 /* Buffer length in bytes */
1225 acpigen_write_word(UUID_LEN);
1226
1227 /* Output UUID in expected order */
1228 for (i = 0; i < UUID_LEN; i++)
1229 acpigen_emit_byte(buf[order[i]]);
1230
1231 acpigen_pop_len();
1232}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001233
1234/*
1235 * Name (_PRx, Package(One) { name })
1236 * ...
1237 * PowerResource (name, level, order)
1238 */
1239void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001240 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001241{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001242 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001243 for (i = 0; i < dev_states_count; i++) {
1244 acpigen_write_name(dev_states[i]);
1245 acpigen_write_package(1);
1246 acpigen_emit_simple_namestring(name);
1247 acpigen_pop_len(); /* Package */
1248 }
1249
1250 acpigen_emit_ext_op(POWER_RES_OP);
1251
1252 acpigen_write_len_f();
1253
1254 acpigen_emit_simple_namestring(name);
1255 acpigen_emit_byte(level);
1256 acpigen_emit_word(order);
1257}
1258
1259/* Sleep (ms) */
1260void acpigen_write_sleep(uint64_t sleep_ms)
1261{
1262 acpigen_emit_ext_op(SLEEP_OP);
1263 acpigen_write_integer(sleep_ms);
1264}
1265
1266void acpigen_write_store(void)
1267{
1268 acpigen_emit_byte(STORE_OP);
1269}
1270
1271/* Store (src, dst) */
1272void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1273{
1274 acpigen_write_store();
1275 acpigen_emit_byte(src);
1276 acpigen_emit_byte(dst);
1277}
1278
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001279/* Store (src, "namestr") */
1280void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1281{
1282 acpigen_write_store();
1283 acpigen_emit_byte(src);
1284 acpigen_emit_namestring(dst);
1285}
1286
Duncan Laurie8e391d32020-09-30 23:17:41 +00001287/* Store (src, "namestr") */
1288void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst)
1289{
1290 acpigen_write_store();
1291 acpigen_write_integer(src);
1292 acpigen_emit_namestring(dst);
1293}
1294
1295/* Store (src, dst) */
1296void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst)
1297{
1298 acpigen_write_store();
1299 acpigen_write_integer(src);
1300 acpigen_emit_byte(dst);
1301}
1302
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001303/* Or (arg1, arg2, res) */
1304void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1305{
1306 acpigen_emit_byte(OR_OP);
1307 acpigen_emit_byte(arg1);
1308 acpigen_emit_byte(arg2);
1309 acpigen_emit_byte(res);
1310}
1311
Rajat Jain310623b2020-02-26 11:02:53 -08001312/* Xor (arg1, arg2, res) */
1313void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1314{
1315 acpigen_emit_byte(XOR_OP);
1316 acpigen_emit_byte(arg1);
1317 acpigen_emit_byte(arg2);
1318 acpigen_emit_byte(res);
1319}
1320
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001321/* And (arg1, arg2, res) */
1322void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1323{
1324 acpigen_emit_byte(AND_OP);
1325 acpigen_emit_byte(arg1);
1326 acpigen_emit_byte(arg2);
1327 acpigen_emit_byte(res);
1328}
1329
1330/* Not (arg, res) */
1331void acpigen_write_not(uint8_t arg, uint8_t res)
1332{
1333 acpigen_emit_byte(NOT_OP);
1334 acpigen_emit_byte(arg);
1335 acpigen_emit_byte(res);
1336}
1337
1338/* Store (str, DEBUG) */
1339void acpigen_write_debug_string(const char *str)
1340{
1341 acpigen_write_store();
1342 acpigen_write_string(str);
1343 acpigen_emit_ext_op(DEBUG_OP);
1344}
1345
1346/* Store (val, DEBUG) */
1347void acpigen_write_debug_integer(uint64_t val)
1348{
1349 acpigen_write_store();
1350 acpigen_write_integer(val);
1351 acpigen_emit_ext_op(DEBUG_OP);
1352}
1353
1354/* Store (op, DEBUG) */
1355void acpigen_write_debug_op(uint8_t op)
1356{
1357 acpigen_write_store();
1358 acpigen_emit_byte(op);
1359 acpigen_emit_ext_op(DEBUG_OP);
1360}
1361
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001362/* Store (str, DEBUG) */
1363void acpigen_write_debug_namestr(const char *str)
1364{
1365 acpigen_write_store();
1366 acpigen_emit_namestring(str);
1367 acpigen_emit_ext_op(DEBUG_OP);
1368}
1369
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001370void acpigen_write_if(void)
1371{
1372 acpigen_emit_byte(IF_OP);
1373 acpigen_write_len_f();
1374}
1375
1376/* If (And (arg1, arg2)) */
1377void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1378{
1379 acpigen_write_if();
1380 acpigen_emit_byte(AND_OP);
1381 acpigen_emit_byte(arg1);
1382 acpigen_emit_byte(arg2);
1383}
1384
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001385/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001386 * Generates ACPI code for checking if operand1 and operand2 are equal.
1387 * Both operand1 and operand2 are ACPI ops.
1388 *
1389 * If (Lequal (op,1 op2))
1390 */
1391void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1392{
1393 acpigen_write_if();
1394 acpigen_emit_byte(LEQUAL_OP);
1395 acpigen_emit_byte(op1);
1396 acpigen_emit_byte(op2);
1397}
1398
1399/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001400 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1401 * operand1 is ACPI op and operand2 is an integer.
1402 *
1403 * If (Lequal (op, val))
1404 */
1405void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001406{
1407 acpigen_write_if();
1408 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001409 acpigen_emit_byte(op);
1410 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001411}
1412
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001413/*
1414 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1415 * operand1 is namestring and operand2 is an integer.
1416 *
1417 * If (Lequal ("namestr", val))
1418 */
1419void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1420{
1421 acpigen_write_if();
1422 acpigen_emit_byte(LEQUAL_OP);
1423 acpigen_emit_namestring(namestr);
1424 acpigen_write_integer(val);
1425}
1426
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001427void acpigen_write_else(void)
1428{
1429 acpigen_emit_byte(ELSE_OP);
1430 acpigen_write_len_f();
1431}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001432
Duncan Laurie36858202020-10-06 21:01:51 +00001433void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count)
1434{
1435 acpigen_emit_byte(SHIFT_LEFT_OP);
1436 acpigen_emit_byte(src_result);
1437 acpigen_write_integer(count);
1438 acpigen_emit_byte(ZERO_OP);
1439}
1440
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001441void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1442{
1443 acpigen_emit_byte(TO_BUFFER_OP);
1444 acpigen_emit_byte(src);
1445 acpigen_emit_byte(dst);
1446}
1447
1448void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1449{
1450 acpigen_emit_byte(TO_INTEGER_OP);
1451 acpigen_emit_byte(src);
1452 acpigen_emit_byte(dst);
1453}
1454
Aaron Durbin80bc0912020-08-19 23:17:42 -06001455void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op)
1456{
1457 acpigen_emit_byte(TO_INTEGER_OP);
1458 acpigen_emit_namestring(source);
1459 acpigen_emit_byte(dst_op);
1460}
1461
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301462void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001463{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301464 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001465
1466 acpigen_emit_byte(BUFFER_OP);
1467 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301468 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001469
1470 for (i = 0; i < size; i++)
1471 acpigen_emit_byte(arr[i]);
1472
1473 acpigen_pop_len();
1474}
1475
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301476void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001477{
1478 acpigen_emit_byte(RETURN_OP);
1479 acpigen_write_byte_buffer(arr, size);
1480}
1481
1482void acpigen_write_return_singleton_buffer(uint8_t arg)
1483{
1484 acpigen_write_return_byte_buffer(&arg, 1);
1485}
1486
Duncan Laurie2fe74712020-06-01 17:36:56 -07001487void acpigen_write_return_op(uint8_t arg)
1488{
1489 acpigen_emit_byte(RETURN_OP);
1490 acpigen_emit_byte(arg);
1491}
1492
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001493void acpigen_write_return_byte(uint8_t arg)
1494{
1495 acpigen_emit_byte(RETURN_OP);
1496 acpigen_write_byte(arg);
1497}
1498
Naresh G Solanki05abe432016-11-17 00:16:29 +05301499void acpigen_write_return_integer(uint64_t arg)
1500{
1501 acpigen_emit_byte(RETURN_OP);
1502 acpigen_write_integer(arg);
1503}
1504
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001505void acpigen_write_return_namestr(const char *arg)
1506{
1507 acpigen_emit_byte(RETURN_OP);
1508 acpigen_emit_namestring(arg);
1509}
1510
Naresh G Solanki05abe432016-11-17 00:16:29 +05301511void acpigen_write_return_string(const char *arg)
1512{
1513 acpigen_emit_byte(RETURN_OP);
1514 acpigen_write_string(arg);
1515}
1516
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001517void acpigen_write_upc(enum acpi_upc_type type)
1518{
1519 acpigen_write_name("_UPC");
1520 acpigen_write_package(4);
1521 /* Connectable */
1522 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1523 /* Type */
1524 acpigen_write_byte(type);
1525 /* Reserved0 */
1526 acpigen_write_zero();
1527 /* Reserved1 */
1528 acpigen_write_zero();
1529 acpigen_pop_len();
1530}
1531
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001532void acpigen_write_pld(const struct acpi_pld *pld)
1533{
1534 uint8_t buf[20];
1535
1536 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1537 return;
1538
1539 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001540 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001541 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001542 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001543}
1544
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301545void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1546 size_t count, void *arg)
1547{
1548 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1549 acpigen_write_dsm_uuid_arr(&id, 1);
1550}
1551
1552static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1553{
1554 size_t i;
1555
1556 /* If (LEqual (Local0, ToUUID(uuid))) */
1557 acpigen_write_if();
1558 acpigen_emit_byte(LEQUAL_OP);
1559 acpigen_emit_byte(LOCAL0_OP);
1560 acpigen_write_uuid(id->uuid);
1561
1562 /* ToInteger (Arg2, Local1) */
1563 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1564
1565 for (i = 0; i < id->count; i++) {
1566 /* If (LEqual (Local1, i)) */
1567 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1568
1569 /* Callback to write if handler. */
1570 if (id->callbacks[i])
1571 id->callbacks[i](id->arg);
1572
1573 acpigen_pop_len(); /* If */
1574 }
1575
1576 /* Default case: Return (Buffer (One) { 0x0 }) */
1577 acpigen_write_return_singleton_buffer(0x0);
1578
1579 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1580
1581}
1582
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001583/*
1584 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301585 * This function takes as input array of uuid for the device, set of callbacks
1586 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1587 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1588 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001589 *
1590 * Arguments passed into _DSM method:
1591 * Arg0 = UUID
1592 * Arg1 = Revision
1593 * Arg2 = Function index
1594 * Arg3 = Function specific arguments
1595 *
1596 * AML code generated would look like:
1597 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301598 * ToBuffer (Arg0, Local0)
1599 * If (LEqual (Local0, ToUUID(uuid))) {
1600 * ToInteger (Arg2, Local1)
1601 * If (LEqual (Local1, 0)) {
1602 * <acpigen by callback[0]>
1603 * }
1604 * ...
1605 * If (LEqual (Local1, n)) {
1606 * <acpigen by callback[n]>
1607 * }
1608 * Return (Buffer (One) { 0x0 })
1609 * }
1610 * ...
1611 * If (LEqual (Local0, ToUUID(uuidn))) {
1612 * ...
1613 * }
1614 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001615 * }
1616 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301617void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001618{
1619 size_t i;
1620
1621 /* Method (_DSM, 4, Serialized) */
1622 acpigen_write_method_serialized("_DSM", 0x4);
1623
1624 /* ToBuffer (Arg0, Local0) */
1625 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1626
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001627 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301628 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001629
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301630 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001631 acpigen_write_return_singleton_buffer(0x0);
1632
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001633 acpigen_pop_len(); /* Method _DSM */
1634}
1635
Matt Delcob425bc82018-08-13 13:36:27 -07001636void acpigen_write_CPPC_package(const struct cppc_config *config)
1637{
1638 u32 i;
1639 u32 max;
1640 switch (config->version) {
1641 case 1:
1642 max = CPPC_MAX_FIELDS_VER_1;
1643 break;
1644 case 2:
1645 max = CPPC_MAX_FIELDS_VER_2;
1646 break;
1647 case 3:
1648 max = CPPC_MAX_FIELDS_VER_3;
1649 break;
1650 default:
1651 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1652 config->version);
1653 return;
1654 }
1655 acpigen_write_name(CPPC_PACKAGE_NAME);
1656
1657 /* Adding 2 to account for length and version fields */
1658 acpigen_write_package(max + 2);
1659 acpigen_write_dword(max + 2);
1660
1661 acpigen_write_byte(config->version);
1662
1663 for (i = 0; i < max; ++i) {
1664 const acpi_addr_t *reg = &(config->regs[i]);
1665 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +02001666 reg->bit_width == 32 && reg->access_size == ACPI_ACCESS_SIZE_UNDEFINED) {
Matt Delcob425bc82018-08-13 13:36:27 -07001667 acpigen_write_dword(reg->addrl);
1668 } else {
1669 acpigen_write_register_resource(reg);
1670 }
1671 }
1672 acpigen_pop_len();
1673}
1674
1675void acpigen_write_CPPC_method(void)
1676{
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001677 char pscope[16];
1678 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
1679
Matt Delcob425bc82018-08-13 13:36:27 -07001680 acpigen_write_method("_CPC", 0);
1681 acpigen_emit_byte(RETURN_OP);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001682 acpigen_emit_namestring(pscope);
Matt Delcob425bc82018-08-13 13:36:27 -07001683 acpigen_pop_len();
1684}
1685
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001686/*
1687 * Generate ACPI AML code for _ROM method.
1688 * This function takes as input ROM data and ROM length.
1689 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001690 * The ACPI spec isn't clear about what should happen at the end of the
1691 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1692 * bytes in the returned buffer with zeros.
1693 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001694 * Arguments passed into _DSM method:
1695 * Arg0 = Offset in Bytes
1696 * Arg1 = Bytes to return
1697 *
1698 * Example:
1699 * acpigen_write_rom(0xdeadbeef, 0x10000)
1700 *
1701 * AML code generated would look like:
1702 * Method (_ROM, 2, NotSerialized) {
1703 *
1704 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1705 * Field (ROMS, AnyAcc, NoLock, Preserve)
1706 * {
1707 * Offset (0),
1708 * RBF0, 0x80000
1709 * }
1710 *
1711 * Store (Arg0, Local0)
1712 * Store (Arg1, Local1)
1713 *
1714 * If (LGreater (Local1, 0x1000))
1715 * {
1716 * Store (0x1000, Local1)
1717 * }
1718 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001719 * Store (Local1, Local3)
1720 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001721 * If (LGreater (Local0, 0x10000))
1722 * {
1723 * Return(Buffer(Local1){0})
1724 * }
1725 *
1726 * If (LGreater (Local0, 0x0f000))
1727 * {
1728 * Subtract (0x10000, Local0, Local2)
1729 * If (LGreater (Local1, Local2))
1730 * {
1731 * Store (Local2, Local1)
1732 * }
1733 * }
1734 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001735 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001736 *
1737 * Multiply (Local0, 0x08, Local0)
1738 * Multiply (Local1, 0x08, Local1)
1739 *
1740 * CreateField (RBF0, Local0, Local1, TMPB)
1741 * Store (TMPB, ROM1)
1742 * Return (ROM1)
1743 * }
1744 */
1745
1746void acpigen_write_rom(void *bios, const size_t length)
1747{
1748 ASSERT(bios)
1749 ASSERT(length)
1750
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001751 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001752 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001753
1754 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1755 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1756 (uintptr_t)bios, length);
1757 acpigen_write_opregion(&opreg);
1758
1759 struct fieldlist l[] = {
1760 FIELDLIST_OFFSET(0),
1761 FIELDLIST_NAMESTR("RBF0", 8 * length),
1762 };
1763
1764 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1765 * {
1766 * Offset (0),
1767 * RBF0, 0x80000
1768 * } */
1769 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1770 FIELD_NOLOCK | FIELD_PRESERVE);
1771
1772 /* Store (Arg0, Local0) */
1773 acpigen_write_store();
1774 acpigen_emit_byte(ARG0_OP);
1775 acpigen_emit_byte(LOCAL0_OP);
1776
1777 /* Store (Arg1, Local1) */
1778 acpigen_write_store();
1779 acpigen_emit_byte(ARG1_OP);
1780 acpigen_emit_byte(LOCAL1_OP);
1781
1782 /* ACPI SPEC requires to return at maximum 4KiB */
1783 /* If (LGreater (Local1, 0x1000)) */
1784 acpigen_write_if();
1785 acpigen_emit_byte(LGREATER_OP);
1786 acpigen_emit_byte(LOCAL1_OP);
1787 acpigen_write_integer(0x1000);
1788
1789 /* Store (0x1000, Local1) */
1790 acpigen_write_store();
1791 acpigen_write_integer(0x1000);
1792 acpigen_emit_byte(LOCAL1_OP);
1793
1794 /* Pop if */
1795 acpigen_pop_len();
1796
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001797 /* Store (Local1, Local3) */
1798 acpigen_write_store();
1799 acpigen_emit_byte(LOCAL1_OP);
1800 acpigen_emit_byte(LOCAL3_OP);
1801
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001802 /* If (LGreater (Local0, length)) */
1803 acpigen_write_if();
1804 acpigen_emit_byte(LGREATER_OP);
1805 acpigen_emit_byte(LOCAL0_OP);
1806 acpigen_write_integer(length);
1807
1808 /* Return(Buffer(Local1){0}) */
1809 acpigen_emit_byte(RETURN_OP);
1810 acpigen_emit_byte(BUFFER_OP);
1811 acpigen_write_len_f();
1812 acpigen_emit_byte(LOCAL1_OP);
1813 acpigen_emit_byte(0);
1814 acpigen_pop_len();
1815
1816 /* Pop if */
1817 acpigen_pop_len();
1818
1819 /* If (LGreater (Local0, length - 4096)) */
1820 acpigen_write_if();
1821 acpigen_emit_byte(LGREATER_OP);
1822 acpigen_emit_byte(LOCAL0_OP);
1823 acpigen_write_integer(length - 4096);
1824
1825 /* Subtract (length, Local0, Local2) */
1826 acpigen_emit_byte(SUBTRACT_OP);
1827 acpigen_write_integer(length);
1828 acpigen_emit_byte(LOCAL0_OP);
1829 acpigen_emit_byte(LOCAL2_OP);
1830
1831 /* If (LGreater (Local1, Local2)) */
1832 acpigen_write_if();
1833 acpigen_emit_byte(LGREATER_OP);
1834 acpigen_emit_byte(LOCAL1_OP);
1835 acpigen_emit_byte(LOCAL2_OP);
1836
1837 /* Store (Local2, Local1) */
1838 acpigen_write_store();
1839 acpigen_emit_byte(LOCAL2_OP);
1840 acpigen_emit_byte(LOCAL1_OP);
1841
1842 /* Pop if */
1843 acpigen_pop_len();
1844
1845 /* Pop if */
1846 acpigen_pop_len();
1847
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001848 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001849 acpigen_write_name("ROM1");
1850 acpigen_emit_byte(BUFFER_OP);
1851 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001852 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001853 acpigen_emit_byte(0);
1854 acpigen_pop_len();
1855
1856 /* Multiply (Local1, 0x08, Local1) */
1857 acpigen_emit_byte(MULTIPLY_OP);
1858 acpigen_emit_byte(LOCAL1_OP);
1859 acpigen_write_integer(0x08);
1860 acpigen_emit_byte(LOCAL1_OP);
1861
1862 /* Multiply (Local0, 0x08, Local0) */
1863 acpigen_emit_byte(MULTIPLY_OP);
1864 acpigen_emit_byte(LOCAL0_OP);
1865 acpigen_write_integer(0x08);
1866 acpigen_emit_byte(LOCAL0_OP);
1867
1868 /* CreateField (RBF0, Local0, Local1, TMPB) */
1869 acpigen_emit_ext_op(CREATEFIELD_OP);
1870 acpigen_emit_namestring("RBF0");
1871 acpigen_emit_byte(LOCAL0_OP);
1872 acpigen_emit_byte(LOCAL1_OP);
1873 acpigen_emit_namestring("TMPB");
1874
1875 /* Store (TMPB, ROM1) */
1876 acpigen_write_store();
1877 acpigen_emit_namestring("TMPB");
1878 acpigen_emit_namestring("ROM1");
1879
1880 /* Return (ROM1) */
1881 acpigen_emit_byte(RETURN_OP);
1882 acpigen_emit_namestring("ROM1");
1883
1884 /* Pop method */
1885 acpigen_pop_len();
1886}
1887
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001888/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001889int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001890{
1891 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1892 acpigen_write_debug_string("read_rx_gpio not available");
1893 return -1;
1894}
1895
Aaron Durbin64031672018-04-21 14:45:32 -06001896int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001897{
1898 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1899 acpigen_write_debug_string("get_tx_gpio not available");
1900 return -1;
1901}
1902
Aaron Durbin64031672018-04-21 14:45:32 -06001903int __weak acpigen_soc_set_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001904{
1905 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1906 acpigen_write_debug_string("set_tx_gpio not available");
1907 return -1;
1908}
1909
Aaron Durbin64031672018-04-21 14:45:32 -06001910int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001911{
1912 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1913 acpigen_write_debug_string("clear_tx_gpio not available");
1914 return -1;
1915}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001916
1917/*
1918 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1919 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1920 * make callbacks into SoC acpigen code.
1921 *
1922 * Returns 0 on success and -1 on error.
1923 */
Duncan Laurie30c3f912020-10-09 04:48:53 +00001924int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001925{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001926 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001927 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001928 else
1929 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001930}
1931
Duncan Laurie30c3f912020-10-09 04:48:53 +00001932int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001933{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001934 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001935 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1936 else
1937 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1938}
Jonathan Zhang71299c22020-01-27 11:38:57 -08001939
Duncan Laurie30c3f912020-10-09 04:48:53 +00001940void acpigen_get_rx_gpio(const struct acpi_gpio *gpio)
Rajat Jain310623b2020-02-26 11:02:53 -08001941{
1942 acpigen_soc_read_rx_gpio(gpio->pins[0]);
1943
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001944 if (gpio->active_low)
Rajat Jain310623b2020-02-26 11:02:53 -08001945 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1946}
1947
Duncan Laurie30c3f912020-10-09 04:48:53 +00001948void acpigen_get_tx_gpio(const struct acpi_gpio *gpio)
Duncan Laurie2fe74712020-06-01 17:36:56 -07001949{
1950 acpigen_soc_get_tx_gpio(gpio->pins[0]);
1951
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001952 if (gpio->active_low)
Duncan Laurie2fe74712020-06-01 17:36:56 -07001953 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1954}
1955
Jonathan Zhang71299c22020-01-27 11:38:57 -08001956/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
1957void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran,
1958 u16 range_min, u16 range_max, u16 translation, u16 length)
1959{
1960 acpigen_emit_byte(0x88);
1961 /* Byte 1+2: length (0x000d) */
1962 acpigen_emit_byte(0x0d);
1963 acpigen_emit_byte(0x00);
1964 /* resource type */
1965 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1966 /* general flags */
1967 acpigen_emit_byte(gen_flags);
1968 /* type flags */
1969 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1970 acpigen_emit_byte(type_flags);
1971 /* granularity, min, max, translation, length */
1972 acpigen_emit_word(gran);
1973 acpigen_emit_word(range_min);
1974 acpigen_emit_word(range_max);
1975 acpigen_emit_word(translation);
1976 acpigen_emit_word(length);
1977}
1978
1979/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
1980void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags,
1981 u32 gran, u32 range_min, u32 range_max, u32 translation, u32 length)
1982{
1983 acpigen_emit_byte(0x87);
1984 /* Byte 1+2: length (0023) */
1985 acpigen_emit_byte(23);
1986 acpigen_emit_byte(0x00);
1987 /* resource type */
1988 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1989 /* general flags */
1990 acpigen_emit_byte(gen_flags);
1991 /* type flags */
1992 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1993 acpigen_emit_byte(type_flags);
1994 /* granularity, min, max, translation, length */
1995 acpigen_emit_dword(gran);
1996 acpigen_emit_dword(range_min);
1997 acpigen_emit_dword(range_max);
1998 acpigen_emit_dword(translation);
1999 acpigen_emit_dword(length);
2000}
2001
2002static void acpigen_emit_qword(u64 data)
2003{
2004 acpigen_emit_dword(data & 0xffffffff);
2005 acpigen_emit_dword((data >> 32) & 0xffffffff);
2006}
2007
2008/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
2009void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags,
2010 u64 gran, u64 range_min, u64 range_max, u64 translation, u64 length)
2011{
2012 acpigen_emit_byte(0x8a);
2013 /* Byte 1+2: length (0x002b) */
2014 acpigen_emit_byte(0x2b);
2015 acpigen_emit_byte(0x00);
2016 /* resource type */
2017 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2018 /* general flags */
2019 acpigen_emit_byte(gen_flags);
2020 /* type flags */
2021 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2022 acpigen_emit_byte(type_flags);
2023 /* granularity, min, max, translation, length */
2024 acpigen_emit_qword(gran);
2025 acpigen_emit_qword(range_min);
2026 acpigen_emit_qword(range_max);
2027 acpigen_emit_qword(translation);
2028 acpigen_emit_qword(length);
2029}
Furquan Shaikh1a829232020-04-23 11:11:05 -07002030
2031void acpigen_write_ADR(uint64_t adr)
2032{
2033 acpigen_write_name_qword("_ADR", adr);
2034}
2035
2036void acpigen_write_ADR_pci_devfn(pci_devfn_t devfn)
2037{
2038 /*
2039 * _ADR for PCI Bus is encoded as follows:
2040 * [63:32] - unused
2041 * [31:16] - device #
2042 * [15:0] - function #
2043 */
2044 acpigen_write_ADR(PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn));
2045}
2046
2047void acpigen_write_ADR_pci_device(const struct device *dev)
2048{
2049 assert(dev->path.type == DEVICE_PATH_PCI);
2050 acpigen_write_ADR_pci_devfn(dev->path.pci.devfn);
2051}
Duncan Laurie08a942f2020-04-29 12:13:14 -07002052
2053/**
2054 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2055 * @address: SoundWire device address properties.
2056 *
2057 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2058 *
2059 * 63..52 - Reserved (0)
2060 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2061 * Used when a Controller has multiple master devices, each producing a
2062 * separate SoundWire Link. Set to 0 for single-link controllers.
2063 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2064 * 47..44 - SoundWire specification version that this device supports
2065 * 43..40 - Unique ID for multiple devices
2066 * 39..24 - MIPI standard manufacturer code
2067 * 23..08 - Vendor defined part ID
2068 * 07..00 - MIPI class encoding
2069 */
2070void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
2071{
2072 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
2073 (((uint64_t)address->version & 0xf) << 44) |
2074 (((uint64_t)address->unique_id & 0xf) << 40) |
2075 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
2076 (((uint64_t)address->part_id & 0xffff) << 8) |
2077 (((uint64_t)address->class & 0xff)));
2078}
Tim Wawrzynczakf6945022020-06-04 15:18:47 -06002079
2080void acpigen_notify(const char *namestr, int value)
2081{
2082 acpigen_emit_byte(NOTIFY_OP);
2083 acpigen_emit_namestring(namestr);
2084 acpigen_write_integer(value);
2085}
Aaron Durbin80bc0912020-08-19 23:17:42 -06002086
2087static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name)
2088{
2089 acpigen_emit_byte(aml_op);
2090 acpigen_emit_byte(srcop);
2091 acpigen_write_integer(byte_offset);
2092 acpigen_emit_namestring(name);
2093}
2094
2095void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name)
2096{
2097 _create_field(CREATE_BYTE_OP, op, byte_offset, name);
2098}
2099
2100void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name)
2101{
2102 _create_field(CREATE_WORD_OP, op, byte_offset, name);
2103}
2104
2105void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name)
2106{
2107 _create_field(CREATE_DWORD_OP, op, byte_offset, name);
2108}
2109
2110void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name)
2111{
2112 _create_field(CREATE_QWORD_OP, op, byte_offset, name);
2113}
Jason Gleneskca36aed2020-09-15 21:01:57 -07002114
2115void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
2116{
2117 acpigen_write_name("_PCT");
2118 acpigen_write_package(0x02);
2119 acpigen_write_register_resource(perf_ctrl);
2120 acpigen_write_register_resource(perf_sts);
2121
2122 acpigen_pop_len();
2123}
2124
2125void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value)
2126{
2127 acpigen_write_package(0x08);
2128 acpigen_write_dword(pstate_value->core_freq);
2129 acpigen_write_dword(pstate_value->power);
2130 acpigen_write_dword(pstate_value->transition_latency);
2131 acpigen_write_dword(pstate_value->bus_master_latency);
2132
2133 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t));
2134 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t));
2135 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t));
2136 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t));
2137
2138 acpigen_pop_len();
2139}
2140
2141void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
2142{
2143 size_t pstate;
2144
2145 acpigen_write_name("XPSS");
2146 acpigen_write_package(nentries);
2147 for (pstate = 0; pstate < nentries; pstate++) {
2148 acpigen_write_xpss_package(pstate_values);
2149 pstate_values++;
2150 }
2151
2152 acpigen_pop_len();
2153}
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002154
2155/* Delay up to wait_ms until provided namestr matches expected value. */
2156void acpigen_write_delay_until_namestr_int(uint32_t wait_ms, const char *name, uint64_t value)
2157{
2158 uint32_t wait_ms_segment = 1;
2159 uint32_t segments = wait_ms;
2160
2161 /* Sleep in 16ms segments if delay is more than 32ms. */
2162 if (wait_ms > 32) {
2163 wait_ms_segment = 16;
2164 segments = wait_ms / 16;
2165 }
2166
2167 acpigen_write_store_int_to_op(segments, LOCAL7_OP);
2168 acpigen_emit_byte(WHILE_OP);
2169 acpigen_write_len_f();
2170 acpigen_emit_byte(LGREATER_OP);
2171 acpigen_emit_byte(LOCAL7_OP);
2172 acpigen_emit_byte(ZERO_OP);
2173
2174 /* If name is not provided then just delay in a loop. */
2175 if (name) {
2176 acpigen_write_if_lequal_namestr_int(name, value);
2177 acpigen_emit_byte(BREAK_OP);
2178 acpigen_pop_len(); /* If */
2179 }
2180
2181 acpigen_write_sleep(wait_ms_segment);
2182 acpigen_emit_byte(DECREMENT_OP);
2183 acpigen_emit_byte(LOCAL7_OP);
2184 acpigen_pop_len(); /* While */
2185}