blob: 9818dc296cbfdef76495698ccfda2cd5d7775deb [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>
Duncan Laurie08a942f2020-04-29 12:13:14 -070021#include <device/soundwire.h>
Elyes HAOUAScdd2f632021-02-11 19:37:11 +010022#include <types.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000023
24static char *gencurrent;
25
26char *len_stack[ACPIGEN_LENSTACK_SIZE];
27int ltop = 0;
28
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010029void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000030{
31 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010032 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000033 acpigen_emit_byte(0);
34 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050035 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000036}
37
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010038void acpigen_pop_len(void)
39{
40 int len;
41 ASSERT(ltop > 0)
42 char *p = len_stack[--ltop];
43 len = gencurrent - p;
44 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050045 /* generate store length for 0xfffff max */
46 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010047 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050048 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010049
50}
51
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000052void acpigen_set_current(char *curr)
53{
54 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000055}
56
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000057char *acpigen_get_current(void)
58{
59 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000060}
61
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010062void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000063{
64 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000065}
66
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070067void acpigen_emit_ext_op(uint8_t op)
68{
69 acpigen_emit_byte(EXT_OP_PREFIX);
70 acpigen_emit_byte(op);
71}
72
Duncan Laurie9ccae752016-05-09 07:43:19 -070073void acpigen_emit_word(unsigned int data)
74{
75 acpigen_emit_byte(data & 0xff);
76 acpigen_emit_byte((data >> 8) & 0xff);
77}
78
79void acpigen_emit_dword(unsigned int data)
80{
81 acpigen_emit_byte(data & 0xff);
82 acpigen_emit_byte((data >> 8) & 0xff);
83 acpigen_emit_byte((data >> 16) & 0xff);
84 acpigen_emit_byte((data >> 24) & 0xff);
85}
86
Duncan Laurie85d80272016-07-02 19:53:54 -070087char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000088{
Duncan Laurie85d80272016-07-02 19:53:54 -070089 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070090 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010091 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -070092 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +000093 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -070094 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +000095}
96
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010097void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000098{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070099 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000100 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000101}
102
Duncan Laurie9ccae752016-05-09 07:43:19 -0700103void acpigen_write_word(unsigned int data)
104{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700105 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700106 acpigen_emit_word(data);
107}
108
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100109void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000110{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700111 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700112 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000113}
114
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100115void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000116{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700117 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700118 acpigen_emit_dword(data & 0xffffffff);
119 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000120}
121
Duncan Laurief7c38762016-05-09 08:20:38 -0700122void acpigen_write_zero(void)
123{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700124 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700125}
126
127void acpigen_write_one(void)
128{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700129 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700130}
131
132void acpigen_write_ones(void)
133{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700134 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700135}
136
137void acpigen_write_integer(uint64_t data)
138{
139 if (data == 0)
140 acpigen_write_zero();
141 else if (data == 1)
142 acpigen_write_one();
143 else if (data <= 0xff)
144 acpigen_write_byte((unsigned char)data);
145 else if (data <= 0xffff)
146 acpigen_write_word((unsigned int)data);
147 else if (data <= 0xffffffff)
148 acpigen_write_dword((unsigned int)data);
149 else
150 acpigen_write_qword(data);
151}
152
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100153void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000154{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100155 acpigen_write_name(name);
156 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000157}
158
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100159void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000160{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100161 acpigen_write_name(name);
162 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000163}
164
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100165void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000166{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100167 acpigen_write_name(name);
168 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000169}
170
Duncan Laurief7c38762016-05-09 08:20:38 -0700171void acpigen_write_name_integer(const char *name, uint64_t val)
172{
173 acpigen_write_name(name);
174 acpigen_write_integer(val);
175}
176
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700177void acpigen_write_name_string(const char *name, const char *string)
178{
179 acpigen_write_name(name);
180 acpigen_write_string(string);
181}
182
Patrick Rudolph389c8272019-12-17 13:54:41 +0100183void acpigen_write_name_unicode(const char *name, const char *string)
184{
185 const size_t len = strlen(string) + 1;
186 acpigen_write_name(name);
187 acpigen_emit_byte(BUFFER_OP);
188 acpigen_write_len_f();
189 acpigen_write_integer(len);
190 for (size_t i = 0; i < len; i++) {
191 const char c = string[i];
192 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
193 acpigen_emit_word(c >= 0 ? c : '?');
194 }
195 acpigen_pop_len();
196}
197
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100198void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000199{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000200 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700201 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000202 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000203}
204
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700205void acpigen_emit_string(const char *string)
206{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200207 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700208 acpigen_emit_byte('\0'); /* NUL */
209}
210
211void acpigen_write_string(const char *string)
212{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700213 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700214 acpigen_emit_string(string);
215}
216
Duncan Laurie37319032016-05-26 12:47:05 -0700217void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
218{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800219 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700220
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700221 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700222 acpigen_write_name_string("_HID", hid);
223}
224
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100225/*
226 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600227 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
228 * and "By convention, when an ASL compiler pads a name shorter than 4
229 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100230 *
231 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
232 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000233
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700234static void acpigen_emit_simple_namestring(const char *name)
235{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100236 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000237 char ud[] = "____";
238 for (i = 0; i < 4; i++) {
239 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100240 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000241 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000242 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700243 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000244 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000245}
246
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700247static void acpigen_emit_double_namestring(const char *name, int dotpos)
248{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700249 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100250 acpigen_emit_simple_namestring(name);
251 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000252}
253
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700254static void acpigen_emit_multi_namestring(const char *name)
255{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100256 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000257 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700258 acpigen_emit_byte(MULTI_NAME_PREFIX);
259 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000260 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
261
262 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100263 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000264 /* find end or next entity */
265 while ((name[0] != '.') && (name[0] != '\0'))
266 name++;
267 /* forward to next */
268 if (name[0] == '.')
269 name++;
270 count++;
271 }
272
273 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000274}
275
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700276void acpigen_emit_namestring(const char *namepath)
277{
Rudolf Marek3310d752009-06-21 20:26:13 +0000278 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000279 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000280
Ronak Kanabar7c0f0072020-11-30 15:40:51 +0530281 /* Check for NULL pointer */
282 if (!namepath)
283 return;
284
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600285 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000286 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100287 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000288 namepath++;
289 }
290
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600291 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000292 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100293 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000294 namepath++;
295 }
296
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200297 /* If we have only \\ or only ^...^. Then we need to put a null
298 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200299 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700300 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100301 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200302 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000303
304 i = 0;
305 while (namepath[i] != '\0') {
306 if (namepath[i] == '.') {
307 dotcount++;
308 dotpos = i;
309 }
310 i++;
311 }
312
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700313 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100314 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700315 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100316 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700317 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100318 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000319}
320
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100321void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000322{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700323 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100324 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000325}
326
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100327void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000328{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700329 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100330 acpigen_write_len_f();
331 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000332}
Rudolf Marekf997b552009-02-14 15:40:23 +0000333
Duncan Laurie2fe74712020-06-01 17:36:56 -0700334void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
335{
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800336 /* <dest_op> = DeRefOf (<package_op>[<element>]) */
Duncan Laurie2fe74712020-06-01 17:36:56 -0700337 acpigen_write_store();
338 acpigen_emit_byte(DEREF_OP);
339 acpigen_emit_byte(INDEX_OP);
340 acpigen_emit_byte(package_op);
341 acpigen_write_integer(element);
342 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
343 acpigen_emit_byte(dest_op);
344}
345
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800346void acpigen_set_package_op_element_int(uint8_t package_op, unsigned int element, uint64_t src)
347{
348 /* DeRefOf (<package>[<element>]) = <src> */
349 acpigen_write_store();
350 acpigen_write_integer(src);
351 acpigen_emit_byte(DEREF_OP);
352 acpigen_emit_byte(INDEX_OP);
353 acpigen_emit_byte(package_op);
354 acpigen_write_integer(element);
355 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
356}
357
358void acpigen_get_package_element(const char *package, unsigned int element, uint8_t dest_op)
359{
360 /* <dest_op> = <package>[<element>] */
361 acpigen_write_store();
362 acpigen_emit_byte(INDEX_OP);
363 acpigen_emit_namestring(package);
364 acpigen_write_integer(element);
365 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
366 acpigen_emit_byte(dest_op);
367}
368
369void acpigen_set_package_element_int(const char *package, unsigned int element, uint64_t src)
370{
371 /* <package>[<element>] = <src> */
372 acpigen_write_store();
373 acpigen_write_integer(src);
374 acpigen_emit_byte(INDEX_OP);
375 acpigen_emit_namestring(package);
376 acpigen_write_integer(element);
377 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
378}
379
380void acpigen_set_package_element_namestr(const char *package, unsigned int element,
381 const char *src)
382{
383 /* <package>[<element>] = <src> */
384 acpigen_write_store();
385 acpigen_emit_namestring(src);
386 acpigen_emit_byte(INDEX_OP);
387 acpigen_emit_namestring(package);
388 acpigen_write_integer(element);
389 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
390}
391
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100392void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000393{
394/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100395 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200396 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000397*/
398 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700399 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100400 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000401
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200402 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600403 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100404 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000405 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700406 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000407 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000408}
409
Nico Huber4d211ac2017-09-01 21:14:36 +0200410void acpigen_write_processor_package(const char *const name,
411 const unsigned int first_core,
412 const unsigned int core_count)
413{
414 unsigned int i;
415 char pscope[16];
416
417 acpigen_write_name(name);
418 acpigen_write_package(core_count);
419 for (i = first_core; i < first_core + core_count; ++i) {
420 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
421 acpigen_emit_namestring(pscope);
422 }
423 acpigen_pop_len();
424}
425
Arthur Heymans8f055272018-11-28 13:18:57 +0100426/* Method to notify all CPU cores */
427void acpigen_write_processor_cnot(const unsigned int number_of_cores)
428{
429 int core_id;
430
Christian Walterbe3979c2019-12-18 15:07:59 +0100431 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100432 for (core_id = 0; core_id < number_of_cores; core_id++) {
433 char buffer[DEVICE_PATH_MAX];
434 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
435 core_id);
436 acpigen_emit_byte(NOTIFY_OP);
437 acpigen_emit_namestring(buffer);
438 acpigen_emit_byte(ARG0_OP);
439 }
440 acpigen_pop_len();
441}
442
Naresh G Solanki8535c662016-10-25 00:51:24 +0530443/*
444 * Generate ACPI AML code for OperationRegion
445 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
446 * where rname is region name, space is region space, offset is region offset &
447 * len is region length.
448 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
449 */
Duncan Laurie35029602020-10-09 17:50:25 +0000450void acpigen_write_opregion(const struct opregion *opreg)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530451{
452 /* OpregionOp */
453 acpigen_emit_ext_op(OPREGION_OP);
454 /* NameString 4 chars only */
455 acpigen_emit_simple_namestring(opreg->name);
456 /* RegionSpace */
457 acpigen_emit_byte(opreg->regionspace);
458 /* RegionOffset & RegionLen, it can be byte word or double word */
459 acpigen_write_integer(opreg->regionoffset);
460 acpigen_write_integer(opreg->regionlen);
461}
462
Patrick Rudolph32bae492019-08-08 12:47:30 +0200463/*
464 * Generate ACPI AML code for Mutex
465 * Arg0: Pointer to name of mutex
466 * Arg1: Initial value of mutex
467 */
468void acpigen_write_mutex(const char *name, const uint8_t flags)
469{
470 /* MutexOp */
471 acpigen_emit_ext_op(MUTEX_OP);
472 /* NameString 4 chars only */
473 acpigen_emit_simple_namestring(name);
474 acpigen_emit_byte(flags);
475}
476
477void acpigen_write_acquire(const char *name, const uint16_t val)
478{
479 /* AcquireOp */
480 acpigen_emit_ext_op(ACQUIRE_OP);
481 /* NameString 4 chars only */
482 acpigen_emit_simple_namestring(name);
483 acpigen_emit_word(val);
484}
485
486void acpigen_write_release(const char *name)
487{
488 /* ReleaseOp */
489 acpigen_emit_ext_op(RELEASE_OP);
490 /* NameString 4 chars only */
491 acpigen_emit_simple_namestring(name);
492}
493
Patrick Rudolph894977b2017-07-01 16:15:05 +0200494static void acpigen_write_field_length(uint32_t len)
495{
496 uint8_t i, j;
497 uint8_t emit[4];
498
499 i = 1;
500 if (len < 0x40) {
501 emit[0] = len & 0x3F;
502 } else {
503 emit[0] = len & 0xF;
504 len >>= 4;
505 while (len) {
506 emit[i] = len & 0xFF;
507 i++;
508 len >>= 8;
509 }
510 }
511 /* Update bit 7:6 : Number of bytes followed by emit[0] */
512 emit[0] |= (i - 1) << 6;
513
514 for (j = 0; j < i; j++)
515 acpigen_emit_byte(emit[j]);
516}
517
Naresh G Solanki8535c662016-10-25 00:51:24 +0530518static void acpigen_write_field_offset(uint32_t offset,
519 uint32_t current_bit_pos)
520{
521 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530522
523 if (offset < current_bit_pos) {
524 printk(BIOS_WARNING, "%s: Cannot move offset backward",
525 __func__);
526 return;
527 }
528
529 diff_bits = offset - current_bit_pos;
530 /* Upper limit */
531 if (diff_bits > 0xFFFFFFF) {
532 printk(BIOS_WARNING, "%s: Offset very large to encode",
533 __func__);
534 return;
535 }
536
Naresh G Solanki8535c662016-10-25 00:51:24 +0530537 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200538 acpigen_write_field_length(diff_bits);
539}
540
541static void acpigen_write_field_name(const char *name, uint32_t size)
542{
543 acpigen_emit_simple_namestring(name);
544 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530545}
546
Duncan Laurie095bbf92020-09-30 23:09:29 +0000547static void acpigen_write_field_reserved(uint32_t size)
548{
549 acpigen_emit_byte(0);
550 acpigen_write_field_length(size);
551}
552
Naresh G Solanki8535c662016-10-25 00:51:24 +0530553/*
554 * Generate ACPI AML code for Field
555 * Arg0: region name
556 * Arg1: Pointer to struct fieldlist.
557 * Arg2: no. of entries in Arg1
558 * Arg3: flags which indicate filed access type, lock rule & update rule.
559 * Example with fieldlist
560 * struct fieldlist l[] = {
561 * FIELDLIST_OFFSET(0x84),
562 * FIELDLIST_NAMESTR("PMCS", 2),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000563 * FIELDLIST_RESERVED(6),
Naresh G Solanki8535c662016-10-25 00:51:24 +0530564 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200565 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530566 * FIELD_PRESERVE);
567 * Output:
568 * Field (UART, AnyAcc, NoLock, Preserve)
569 * {
570 * Offset (0x84),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000571 * PMCS, 2,
572 * , 6,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530573 * }
574 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700575void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530576 uint8_t flags)
577{
578 uint16_t i;
579 uint32_t current_bit_pos = 0;
580
581 /* FieldOp */
582 acpigen_emit_ext_op(FIELD_OP);
583 /* Package Length */
584 acpigen_write_len_f();
585 /* NameString 4 chars only */
586 acpigen_emit_simple_namestring(name);
587 /* Field Flag */
588 acpigen_emit_byte(flags);
589
590 for (i = 0; i < count; i++) {
591 switch (l[i].type) {
592 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200593 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530594 current_bit_pos += l[i].bits;
595 break;
Duncan Laurie095bbf92020-09-30 23:09:29 +0000596 case RESERVED:
597 acpigen_write_field_reserved(l[i].bits);
598 current_bit_pos += l[i].bits;
599 break;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530600 case OFFSET:
601 acpigen_write_field_offset(l[i].bits, current_bit_pos);
602 current_bit_pos = l[i].bits;
603 break;
604 default:
605 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
606 , __func__, l[i].type);
607 break;
608 }
609 }
610 acpigen_pop_len();
611}
612
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200613/*
614 * Generate ACPI AML code for IndexField
615 * Arg0: region name
616 * Arg1: Pointer to struct fieldlist.
617 * Arg2: no. of entries in Arg1
618 * Arg3: flags which indicate filed access type, lock rule & update rule.
619 * Example with fieldlist
620 * struct fieldlist l[] = {
621 * FIELDLIST_OFFSET(0x84),
622 * FIELDLIST_NAMESTR("PMCS", 2),
623 * };
624 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
625 * FIELD_NOLOCK |
626 * FIELD_PRESERVE);
627 * Output:
628 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
629 * {
630 * Offset (0x84),
631 * PMCS, 2
632 * }
633 */
634void acpigen_write_indexfield(const char *idx, const char *data,
635 struct fieldlist *l, size_t count, uint8_t flags)
636{
637 uint16_t i;
638 uint32_t current_bit_pos = 0;
639
640 /* FieldOp */
641 acpigen_emit_ext_op(INDEX_FIELD_OP);
642 /* Package Length */
643 acpigen_write_len_f();
644 /* NameString 4 chars only */
645 acpigen_emit_simple_namestring(idx);
646 /* NameString 4 chars only */
647 acpigen_emit_simple_namestring(data);
648 /* Field Flag */
649 acpigen_emit_byte(flags);
650
651 for (i = 0; i < count; i++) {
652 switch (l[i].type) {
653 case NAME_STRING:
654 acpigen_write_field_name(l[i].name, l[i].bits);
655 current_bit_pos += l[i].bits;
656 break;
657 case OFFSET:
658 acpigen_write_field_offset(l[i].bits, current_bit_pos);
659 current_bit_pos = l[i].bits;
660 break;
661 default:
662 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
663 , __func__, l[i].type);
664 break;
665 }
666 }
667 acpigen_pop_len();
668}
669
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100670void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000671{
672/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200673 Name (_PCT, Package (0x02)
674 {
675 ResourceTemplate ()
676 {
677 Register (FFixedHW,
678 0x00, // Bit Width
679 0x00, // Bit Offset
680 0x0000000000000000, // Address
681 ,)
682 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000683
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200684 ResourceTemplate ()
685 {
686 Register (FFixedHW,
687 0x00, // Bit Width
688 0x00, // Bit Offset
689 0x0000000000000000, // Address
690 ,)
691 }
692 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000693*/
694 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700695 /* 00000030 "0._PCT.," */
696 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
697 /* 00000038 "........" */
698 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
699 /* 00000040 "........" */
700 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
701 /* 00000048 "....y..." */
702 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
703 /* 00000050 "........" */
704 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
705 /* 00000058 "........" */
706 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000707 0x00, 0x79, 0x00
708 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100709 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000710}
711
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100712void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200713{
714/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200715 Name (_PTC, Package (0x02)
716 {
717 ResourceTemplate ()
718 {
719 Register (FFixedHW,
720 0x00, // Bit Width
721 0x00, // Bit Offset
722 0x0000000000000000, // Address
723 ,)
724 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200725
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200726 ResourceTemplate ()
727 {
728 Register (FFixedHW,
729 0x00, // Bit Width
730 0x00, // Bit Offset
731 0x0000000000000000, // Address
732 ,)
733 }
734 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200735*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200736 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100737 .space_id = ACPI_ADDRESS_SPACE_FIXED,
738 .bit_width = 0,
739 .bit_offset = 0,
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +0200740 .access_size = ACPI_ACCESS_SIZE_UNDEFINED,
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100741 .addrl = 0,
742 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200743 };
744
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100745 acpigen_write_name("_PTC");
746 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200747
748 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700749 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200750
751 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700752 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200753
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100754 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200755}
756
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700757static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100758{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700759 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100760 acpigen_write_len_f();
761 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700762 acpigen_emit_byte(flags);
763}
764
765/* Method (name, nargs, NotSerialized) */
766void acpigen_write_method(const char *name, int nargs)
767{
768 __acpigen_write_method(name, (nargs & 7));
769}
770
771/* Method (name, nargs, Serialized) */
772void acpigen_write_method_serialized(const char *name, int nargs)
773{
774 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100775}
776
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100777void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100778{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700779 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100780 acpigen_write_len_f();
781 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100782}
783
Raul E Rangel052c9632021-05-12 17:01:30 -0600784void acpigen_write_thermal_zone(const char *name)
785{
786 acpigen_emit_ext_op(THERMAL_ZONE_OP);
787 acpigen_write_len_f();
788 acpigen_emit_namestring(name);
789}
790
Duncan Laurieabe2de82016-05-09 11:08:46 -0700791void acpigen_write_STA(uint8_t status)
792{
793 /*
794 * Method (_STA, 0, NotSerialized) { Return (status) }
795 */
796 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700797 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700798 acpigen_write_byte(status);
799 acpigen_pop_len();
800}
801
Sugnan Prabhu S4f77f612020-06-10 09:51:02 +0530802void acpigen_write_STA_ext(const char *namestring)
803{
804 /*
805 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
806 */
807 acpigen_write_method("_STA", 0);
808 acpigen_emit_byte(RETURN_OP);
809 acpigen_emit_namestring(namestring);
810 acpigen_pop_len();
811}
812
Raul E Rangelc7048322021-04-19 15:58:25 -0600813void acpigen_write_LPI_package(u64 level, const struct acpi_lpi_state *states, u16 nentries)
814{
815 /*
816 * Name (_LPI, Package (0x06) // _LPI: Low Power Idle States
817 * {
818 * 0x0000,
819 * 0x0000000000000000,
820 * 0x0003,
821 * Package (0x0A)
822 * {
823 * 0x00000002,
824 * 0x00000001,
825 * 0x00000001,
826 * 0x00000000,
827 * 0x00000000,
828 * 0x00000000,
829 * ResourceTemplate ()
830 * {
831 * Register (FFixedHW,
832 * 0x02, // Bit Width
833 * 0x02, // Bit Offset
834 * 0x0000000000000000, // Address
835 * ,)
836 * },
837 *
838 * ResourceTemplate ()
839 * {
840 * Register (SystemMemory,
841 * 0x00, // Bit Width
842 * 0x00, // Bit Offset
843 * 0x0000000000000000, // Address
844 * ,)
845 * },
846 *
847 * ResourceTemplate ()
848 * {
849 * Register (SystemMemory,
850 * 0x00, // Bit Width
851 * 0x00, // Bit Offset
852 * 0x0000000000000000, // Address
853 * ,)
854 * },
855 *
856 * "C1"
857 * },
858 * ...
859 * }
860 */
861
862 acpigen_write_name("_LPI");
863 acpigen_write_package(3 + nentries);
864 acpigen_write_word(0); /* Revision */
865 acpigen_write_qword(level);
866 acpigen_write_word(nentries);
867
868 for (size_t i = 0; i < nentries; i++, states++) {
869 acpigen_write_package(0xA);
870 acpigen_write_dword(states->min_residency_us);
871 acpigen_write_dword(states->worst_case_wakeup_latency_us);
872 acpigen_write_dword(states->flags);
873 acpigen_write_dword(states->arch_context_lost_flags);
874 acpigen_write_dword(states->residency_counter_frequency_hz);
875 acpigen_write_dword(states->enabled_parent_state);
876 acpigen_write_register_resource(&states->entry_method);
877 acpigen_write_register_resource(&states->residency_counter_register);
878 acpigen_write_register_resource(&states->usage_counter_register);
879 acpigen_write_string(states->state_name);
880 acpigen_pop_len();
881 }
882 acpigen_pop_len();
883}
884
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100885/*
886 * Generates a func with max supported P-states.
887 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100888void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000889{
890/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200891 Method (_PPC, 0, NotSerialized)
892 {
893 Return (nr)
894 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000895*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100896 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700897 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000898 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100899 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100900 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000901}
902
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100903/*
904 * Generates a func with max supported P-states saved
905 * in the variable PPCM.
906 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100907void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700908{
909/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200910 Method (_PPC, 0, NotSerialized)
911 {
912 Return (PPCM)
913 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700914*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100915 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700916 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700917 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100918 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100919 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700920}
921
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100922void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200923{
924/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200925 // Sample _TPC method
926 Method (_TPC, 0, NotSerialized)
927 {
928 Return (\TLVL)
929 }
930*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100931 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700932 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100933 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100934 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200935}
936
Duncan Laurieabe2de82016-05-09 11:08:46 -0700937void acpigen_write_PRW(u32 wake, u32 level)
938{
939 /*
940 * Name (_PRW, Package () { wake, level }
941 */
942 acpigen_write_name("_PRW");
943 acpigen_write_package(2);
944 acpigen_write_integer(wake);
945 acpigen_write_integer(level);
946 acpigen_pop_len();
947}
948
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100949void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000950 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000951{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100952 acpigen_write_package(6);
953 acpigen_write_dword(coreFreq);
954 acpigen_write_dword(power);
955 acpigen_write_dword(transLat);
956 acpigen_write_dword(busmLat);
957 acpigen_write_dword(control);
958 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100959 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700960
961 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
962 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000963}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000964
Jason Gleneskca36aed2020-09-15 21:01:57 -0700965void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
966{
967 size_t pstate;
968
969 acpigen_write_name("_PSS");
970 acpigen_write_package(nentries);
971 for (pstate = 0; pstate < nentries; pstate++) {
972 acpigen_write_PSS_package(
973 pstate_values->core_freq, pstate_values->power,
974 pstate_values->transition_latency, pstate_values->bus_master_latency,
975 pstate_values->control_value, pstate_values->status_value);
976 pstate_values++;
977 }
978
979 acpigen_pop_len();
980}
981
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100982void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000983{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100984 acpigen_write_name("_PSD");
985 acpigen_write_package(1);
986 acpigen_write_package(5);
987 acpigen_write_byte(5); // 5 values
988 acpigen_write_byte(0); // revision 0
989 acpigen_write_dword(domain);
990 acpigen_write_dword(coordtype);
991 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100992 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100993 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000994}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000995
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100996void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200997{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100998 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700999 acpigen_write_register_resource(&cstate->resource);
Jason Glenesk201acca2020-09-11 12:36:15 -07001000 acpigen_write_byte(cstate->ctype);
1001 acpigen_write_word(cstate->latency);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001002 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001003 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001004}
1005
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001006void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001007{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001008 int i;
1009 acpigen_write_name("_CST");
1010 acpigen_write_package(nentries+1);
Jason Glenesk201acca2020-09-11 12:36:15 -07001011 acpigen_write_integer(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001012
1013 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001014 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +02001015
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001016 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +02001017}
1018
Lee Leahy6f80ccc2017-03-16 15:18:22 -07001019void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
1020 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -05001021{
1022 acpigen_write_name("_CSD");
1023 acpigen_write_package(1);
1024 acpigen_write_package(6);
Jason Glenesk201acca2020-09-11 12:36:15 -07001025 acpigen_write_integer(6); // 6 values
Timothy Pearson83abd812015-06-08 19:35:06 -05001026 acpigen_write_byte(0); // revision 0
1027 acpigen_write_dword(domain);
1028 acpigen_write_dword(coordtype);
1029 acpigen_write_dword(numprocs);
1030 acpigen_write_dword(index);
1031 acpigen_pop_len();
1032 acpigen_pop_len();
1033}
1034
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001035void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001036{
1037/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +02001038 Sample _TSS package with 100% and 50% duty cycles
1039 Name (_TSS, Package (0x02)
1040 {
1041 Package(){100, 1000, 0, 0x00, 0)
1042 Package(){50, 520, 0, 0x18, 0)
1043 })
1044*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001045 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001046 acpi_tstate_t *tstate = tstate_list;
1047
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001048 acpigen_write_name("_TSS");
1049 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +02001050
1051 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001052 acpigen_write_package(5);
1053 acpigen_write_dword(tstate->percent);
1054 acpigen_write_dword(tstate->power);
1055 acpigen_write_dword(tstate->latency);
1056 acpigen_write_dword(tstate->control);
1057 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001058 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001059 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +02001060 }
1061
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001062 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001063}
1064
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001065void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +02001066{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001067 acpigen_write_name("_TSD");
1068 acpigen_write_package(1);
1069 acpigen_write_package(5);
1070 acpigen_write_byte(5); // 5 values
1071 acpigen_write_byte(0); // revision 0
1072 acpigen_write_dword(domain);
1073 acpigen_write_dword(coordtype);
1074 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001075 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001076 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001077}
1078
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001079void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001080{
1081 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001082 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001083 * Byte 0:
1084 * Bit7 : 1 => big item
1085 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
1086 */
1087 acpigen_emit_byte(0x86);
1088 /* Byte 1+2: length (0x0009) */
1089 acpigen_emit_byte(0x09);
1090 acpigen_emit_byte(0x00);
1091 /* bit1-7 are ignored */
1092 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -07001093 acpigen_emit_dword(base);
1094 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001095}
1096
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001097static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001098{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +02001099 acpigen_emit_byte(0x82); /* Register Descriptor */
1100 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
1101 acpigen_emit_byte(0x00); /* Register Length 15:8 */
1102 acpigen_emit_byte(addr->space_id); /* Address Space ID */
1103 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
1104 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +01001105 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -07001106 acpigen_emit_dword(addr->addrl); /* Register Address Low */
1107 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +02001108}
1109
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001110void acpigen_write_register_resource(const acpi_addr_t *addr)
1111{
1112 acpigen_write_resourcetemplate_header();
1113 acpigen_write_register(addr);
1114 acpigen_write_resourcetemplate_footer();
1115}
1116
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001117void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001118{
1119 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001120 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001121 * Byte 0:
1122 * Bit7 : 0 => small item
1123 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1124 * Bit2-0: 010 (0x2) => 2 Bytes long
1125 */
1126 acpigen_emit_byte(0x22);
1127 acpigen_emit_byte(mask & 0xff);
1128 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001129}
1130
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001131void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001132{
1133 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001134 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001135 * Byte 0:
1136 * Bit7 : 0 => small item
1137 * Bit6-3: 1000 (0x8) => I/O port descriptor
1138 * Bit2-0: 111 (0x7) => 7 Bytes long
1139 */
1140 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001141 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001142 /* bit1-7 are ignored */
1143 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1144 /* minimum base address the device may be configured for */
1145 acpigen_emit_byte(min & 0xff);
1146 acpigen_emit_byte((min >> 8) & 0xff);
1147 /* maximum base address the device may be configured for */
1148 acpigen_emit_byte(max & 0xff);
1149 acpigen_emit_byte((max >> 8) & 0xff);
1150 /* alignment for min base */
1151 acpigen_emit_byte(align & 0xff);
1152 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001153}
1154
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001155void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001156{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001157 /*
1158 * A ResourceTemplate() is a Buffer() with a
1159 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001160 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001161 * (small item 0xf, len 1)
1162 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001163 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001164 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001165 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001166 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001167 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001168 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001169 acpigen_emit_byte(0x00);
1170 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001171}
1172
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001173void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001174{
1175 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001176 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001177 /*
1178 * end tag (acpi 4.0 Section 6.4.2.8)
1179 * 0x79 <checksum>
1180 * 0x00 is treated as a good checksum according to the spec
1181 * and is what iasl generates.
1182 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001183 acpigen_emit_byte(0x79);
1184 acpigen_emit_byte(0x00);
1185
Nico Huber7d89ce32017-04-14 00:08:18 +02001186 /* Start counting past the 2-bytes length added in
1187 acpigen_write_resourcetemplate() above. */
1188 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001189
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001190 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001191 p[0] = len & 0xff;
1192 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001193 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001194 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001195}
1196
1197static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1198 struct resource *res)
1199{
1200 acpigen_write_mem32fixed(0, res->base, res->size);
1201}
1202
1203static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1204 struct resource *res)
1205{
1206 resource_t base = res->base;
1207 resource_t size = res->size;
1208 while (size > 0) {
1209 resource_t sz = size > 255 ? 255 : size;
1210 acpigen_write_io16(base, base, 0, sz, 1);
1211 size -= sz;
1212 base += sz;
1213 }
1214}
1215
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001216void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001217{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001218 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001219
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001220 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001221 search_global_resources(
1222 IORESOURCE_MEM | IORESOURCE_RESERVE,
1223 IORESOURCE_MEM | IORESOURCE_RESERVE,
1224 acpigen_add_mainboard_rsvd_mem32, 0);
1225
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001226 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001227 search_global_resources(
1228 IORESOURCE_IO | IORESOURCE_RESERVE,
1229 IORESOURCE_IO | IORESOURCE_RESERVE,
1230 acpigen_add_mainboard_rsvd_io, 0);
1231
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001232 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001233}
1234
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001235void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001236{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001237 acpigen_write_scope(scope);
1238 acpigen_write_name(name);
1239 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001240 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001241}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001242
1243static int hex2bin(const char c)
1244{
1245 if (c >= 'A' && c <= 'F')
1246 return c - 'A' + 10;
1247 if (c >= 'a' && c <= 'f')
1248 return c - 'a' + 10;
1249 return c - '0';
1250}
1251
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001252void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001253{
1254 u32 compact = 0;
1255
1256 /* Clamping individual values would be better but
1257 there is a disagreement over what is a valid
1258 EISA id, so accept anything and don't clamp,
1259 parent code should create a valid EISAid.
1260 */
1261 compact |= (eisaid[0] - 'A' + 1) << 26;
1262 compact |= (eisaid[1] - 'A' + 1) << 21;
1263 compact |= (eisaid[2] - 'A' + 1) << 16;
1264 compact |= hex2bin(eisaid[3]) << 12;
1265 compact |= hex2bin(eisaid[4]) << 8;
1266 compact |= hex2bin(eisaid[5]) << 4;
1267 compact |= hex2bin(eisaid[6]);
1268
1269 acpigen_emit_byte(0xc);
1270 acpigen_emit_byte((compact >> 24) & 0xff);
1271 acpigen_emit_byte((compact >> 16) & 0xff);
1272 acpigen_emit_byte((compact >> 8) & 0xff);
1273 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001274}
Duncan Laurie3829f232016-05-09 11:08:46 -07001275
1276/*
1277 * ToUUID(uuid)
1278 *
1279 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1280 * order for the bytes that make up a UUID Buffer object.
1281 * UUID byte order for input:
1282 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1283 * UUID byte order for output:
1284 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1285 */
1286#define UUID_LEN 16
1287void acpigen_write_uuid(const char *uuid)
1288{
1289 uint8_t buf[UUID_LEN];
1290 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1291 8, 9, 10, 11, 12, 13, 14, 15 };
1292
1293 /* Parse UUID string into bytes */
1294 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1295 return;
1296
1297 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001298 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001299 acpigen_write_len_f();
1300
1301 /* Buffer length in bytes */
1302 acpigen_write_word(UUID_LEN);
1303
1304 /* Output UUID in expected order */
1305 for (i = 0; i < UUID_LEN; i++)
1306 acpigen_emit_byte(buf[order[i]]);
1307
1308 acpigen_pop_len();
1309}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001310
1311/*
1312 * Name (_PRx, Package(One) { name })
1313 * ...
1314 * PowerResource (name, level, order)
1315 */
1316void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001317 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001318{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001319 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001320 for (i = 0; i < dev_states_count; i++) {
1321 acpigen_write_name(dev_states[i]);
1322 acpigen_write_package(1);
1323 acpigen_emit_simple_namestring(name);
1324 acpigen_pop_len(); /* Package */
1325 }
1326
1327 acpigen_emit_ext_op(POWER_RES_OP);
1328
1329 acpigen_write_len_f();
1330
1331 acpigen_emit_simple_namestring(name);
1332 acpigen_emit_byte(level);
1333 acpigen_emit_word(order);
1334}
1335
1336/* Sleep (ms) */
1337void acpigen_write_sleep(uint64_t sleep_ms)
1338{
1339 acpigen_emit_ext_op(SLEEP_OP);
1340 acpigen_write_integer(sleep_ms);
1341}
1342
1343void acpigen_write_store(void)
1344{
1345 acpigen_emit_byte(STORE_OP);
1346}
1347
1348/* Store (src, dst) */
1349void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1350{
1351 acpigen_write_store();
1352 acpigen_emit_byte(src);
1353 acpigen_emit_byte(dst);
1354}
1355
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001356/* Store (src, "namestr") */
1357void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1358{
1359 acpigen_write_store();
1360 acpigen_emit_byte(src);
1361 acpigen_emit_namestring(dst);
1362}
1363
Duncan Laurie8e391d32020-09-30 23:17:41 +00001364/* Store (src, "namestr") */
1365void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst)
1366{
1367 acpigen_write_store();
1368 acpigen_write_integer(src);
1369 acpigen_emit_namestring(dst);
1370}
1371
1372/* Store (src, dst) */
1373void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst)
1374{
1375 acpigen_write_store();
1376 acpigen_write_integer(src);
1377 acpigen_emit_byte(dst);
1378}
1379
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001380/* Or (arg1, arg2, res) */
1381void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1382{
1383 acpigen_emit_byte(OR_OP);
1384 acpigen_emit_byte(arg1);
1385 acpigen_emit_byte(arg2);
1386 acpigen_emit_byte(res);
1387}
1388
Rajat Jain310623b2020-02-26 11:02:53 -08001389/* Xor (arg1, arg2, res) */
1390void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1391{
1392 acpigen_emit_byte(XOR_OP);
1393 acpigen_emit_byte(arg1);
1394 acpigen_emit_byte(arg2);
1395 acpigen_emit_byte(res);
1396}
1397
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001398/* And (arg1, arg2, res) */
1399void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1400{
1401 acpigen_emit_byte(AND_OP);
1402 acpigen_emit_byte(arg1);
1403 acpigen_emit_byte(arg2);
1404 acpigen_emit_byte(res);
1405}
1406
1407/* Not (arg, res) */
1408void acpigen_write_not(uint8_t arg, uint8_t res)
1409{
1410 acpigen_emit_byte(NOT_OP);
1411 acpigen_emit_byte(arg);
1412 acpigen_emit_byte(res);
1413}
1414
1415/* Store (str, DEBUG) */
1416void acpigen_write_debug_string(const char *str)
1417{
1418 acpigen_write_store();
1419 acpigen_write_string(str);
1420 acpigen_emit_ext_op(DEBUG_OP);
1421}
1422
1423/* Store (val, DEBUG) */
1424void acpigen_write_debug_integer(uint64_t val)
1425{
1426 acpigen_write_store();
1427 acpigen_write_integer(val);
1428 acpigen_emit_ext_op(DEBUG_OP);
1429}
1430
1431/* Store (op, DEBUG) */
1432void acpigen_write_debug_op(uint8_t op)
1433{
1434 acpigen_write_store();
1435 acpigen_emit_byte(op);
1436 acpigen_emit_ext_op(DEBUG_OP);
1437}
1438
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001439/* Store (str, DEBUG) */
1440void acpigen_write_debug_namestr(const char *str)
1441{
1442 acpigen_write_store();
1443 acpigen_emit_namestring(str);
1444 acpigen_emit_ext_op(DEBUG_OP);
1445}
1446
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001447void acpigen_write_if(void)
1448{
1449 acpigen_emit_byte(IF_OP);
1450 acpigen_write_len_f();
1451}
1452
1453/* If (And (arg1, arg2)) */
1454void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1455{
1456 acpigen_write_if();
1457 acpigen_emit_byte(AND_OP);
1458 acpigen_emit_byte(arg1);
1459 acpigen_emit_byte(arg2);
1460}
1461
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001462/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001463 * Generates ACPI code for checking if operand1 and operand2 are equal.
1464 * Both operand1 and operand2 are ACPI ops.
1465 *
1466 * If (Lequal (op,1 op2))
1467 */
1468void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1469{
1470 acpigen_write_if();
1471 acpigen_emit_byte(LEQUAL_OP);
1472 acpigen_emit_byte(op1);
1473 acpigen_emit_byte(op2);
1474}
1475
1476/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001477 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1478 * operand1 is ACPI op and operand2 is an integer.
1479 *
1480 * If (Lequal (op, val))
1481 */
1482void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001483{
1484 acpigen_write_if();
1485 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001486 acpigen_emit_byte(op);
1487 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001488}
1489
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001490/*
1491 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1492 * operand1 is namestring and operand2 is an integer.
1493 *
1494 * If (Lequal ("namestr", val))
1495 */
1496void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1497{
1498 acpigen_write_if();
1499 acpigen_emit_byte(LEQUAL_OP);
1500 acpigen_emit_namestring(namestr);
1501 acpigen_write_integer(val);
1502}
1503
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001504/* Closes previously opened if statement and generates ACPI code for else statement. */
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001505void acpigen_write_else(void)
1506{
Jakub Czapiga61fcb7e2021-02-19 11:44:22 +01001507 acpigen_pop_len();
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001508 acpigen_emit_byte(ELSE_OP);
1509 acpigen_write_len_f();
1510}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001511
Duncan Laurie36858202020-10-06 21:01:51 +00001512void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count)
1513{
1514 acpigen_emit_byte(SHIFT_LEFT_OP);
1515 acpigen_emit_byte(src_result);
1516 acpigen_write_integer(count);
1517 acpigen_emit_byte(ZERO_OP);
1518}
1519
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001520void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1521{
1522 acpigen_emit_byte(TO_BUFFER_OP);
1523 acpigen_emit_byte(src);
1524 acpigen_emit_byte(dst);
1525}
1526
1527void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1528{
1529 acpigen_emit_byte(TO_INTEGER_OP);
1530 acpigen_emit_byte(src);
1531 acpigen_emit_byte(dst);
1532}
1533
Aaron Durbin80bc0912020-08-19 23:17:42 -06001534void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op)
1535{
1536 acpigen_emit_byte(TO_INTEGER_OP);
1537 acpigen_emit_namestring(source);
1538 acpigen_emit_byte(dst_op);
1539}
1540
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301541void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001542{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301543 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001544
1545 acpigen_emit_byte(BUFFER_OP);
1546 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301547 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001548
1549 for (i = 0; i < size; i++)
1550 acpigen_emit_byte(arr[i]);
1551
1552 acpigen_pop_len();
1553}
1554
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301555void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001556{
1557 acpigen_emit_byte(RETURN_OP);
1558 acpigen_write_byte_buffer(arr, size);
1559}
1560
1561void acpigen_write_return_singleton_buffer(uint8_t arg)
1562{
1563 acpigen_write_return_byte_buffer(&arg, 1);
1564}
1565
Duncan Laurie2fe74712020-06-01 17:36:56 -07001566void acpigen_write_return_op(uint8_t arg)
1567{
1568 acpigen_emit_byte(RETURN_OP);
1569 acpigen_emit_byte(arg);
1570}
1571
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001572void acpigen_write_return_byte(uint8_t arg)
1573{
1574 acpigen_emit_byte(RETURN_OP);
1575 acpigen_write_byte(arg);
1576}
1577
Naresh G Solanki05abe432016-11-17 00:16:29 +05301578void acpigen_write_return_integer(uint64_t arg)
1579{
1580 acpigen_emit_byte(RETURN_OP);
1581 acpigen_write_integer(arg);
1582}
1583
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001584void acpigen_write_return_namestr(const char *arg)
1585{
1586 acpigen_emit_byte(RETURN_OP);
1587 acpigen_emit_namestring(arg);
1588}
1589
Naresh G Solanki05abe432016-11-17 00:16:29 +05301590void acpigen_write_return_string(const char *arg)
1591{
1592 acpigen_emit_byte(RETURN_OP);
1593 acpigen_write_string(arg);
1594}
1595
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001596void acpigen_write_upc(enum acpi_upc_type type)
1597{
1598 acpigen_write_name("_UPC");
1599 acpigen_write_package(4);
1600 /* Connectable */
1601 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1602 /* Type */
1603 acpigen_write_byte(type);
1604 /* Reserved0 */
1605 acpigen_write_zero();
1606 /* Reserved1 */
1607 acpigen_write_zero();
1608 acpigen_pop_len();
1609}
1610
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001611void acpigen_write_pld(const struct acpi_pld *pld)
1612{
1613 uint8_t buf[20];
1614
1615 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1616 return;
1617
1618 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001619 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001620 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001621 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001622}
1623
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301624void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1625 size_t count, void *arg)
1626{
1627 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1628 acpigen_write_dsm_uuid_arr(&id, 1);
1629}
1630
1631static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1632{
1633 size_t i;
1634
1635 /* If (LEqual (Local0, ToUUID(uuid))) */
1636 acpigen_write_if();
1637 acpigen_emit_byte(LEQUAL_OP);
1638 acpigen_emit_byte(LOCAL0_OP);
1639 acpigen_write_uuid(id->uuid);
1640
1641 /* ToInteger (Arg2, Local1) */
1642 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1643
1644 for (i = 0; i < id->count; i++) {
1645 /* If (LEqual (Local1, i)) */
1646 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1647
1648 /* Callback to write if handler. */
1649 if (id->callbacks[i])
1650 id->callbacks[i](id->arg);
1651
1652 acpigen_pop_len(); /* If */
1653 }
1654
1655 /* Default case: Return (Buffer (One) { 0x0 }) */
1656 acpigen_write_return_singleton_buffer(0x0);
1657
1658 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1659
1660}
1661
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001662/*
1663 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301664 * This function takes as input array of uuid for the device, set of callbacks
1665 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1666 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1667 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001668 *
1669 * Arguments passed into _DSM method:
1670 * Arg0 = UUID
1671 * Arg1 = Revision
1672 * Arg2 = Function index
1673 * Arg3 = Function specific arguments
1674 *
1675 * AML code generated would look like:
1676 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301677 * ToBuffer (Arg0, Local0)
1678 * If (LEqual (Local0, ToUUID(uuid))) {
1679 * ToInteger (Arg2, Local1)
1680 * If (LEqual (Local1, 0)) {
1681 * <acpigen by callback[0]>
1682 * }
1683 * ...
1684 * If (LEqual (Local1, n)) {
1685 * <acpigen by callback[n]>
1686 * }
1687 * Return (Buffer (One) { 0x0 })
1688 * }
1689 * ...
1690 * If (LEqual (Local0, ToUUID(uuidn))) {
1691 * ...
1692 * }
1693 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001694 * }
1695 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301696void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001697{
1698 size_t i;
1699
1700 /* Method (_DSM, 4, Serialized) */
1701 acpigen_write_method_serialized("_DSM", 0x4);
1702
1703 /* ToBuffer (Arg0, Local0) */
1704 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1705
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001706 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301707 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001708
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301709 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001710 acpigen_write_return_singleton_buffer(0x0);
1711
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001712 acpigen_pop_len(); /* Method _DSM */
1713}
1714
Matt Delcob425bc82018-08-13 13:36:27 -07001715void acpigen_write_CPPC_package(const struct cppc_config *config)
1716{
1717 u32 i;
1718 u32 max;
1719 switch (config->version) {
1720 case 1:
1721 max = CPPC_MAX_FIELDS_VER_1;
1722 break;
1723 case 2:
1724 max = CPPC_MAX_FIELDS_VER_2;
1725 break;
1726 case 3:
1727 max = CPPC_MAX_FIELDS_VER_3;
1728 break;
1729 default:
1730 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1731 config->version);
1732 return;
1733 }
1734 acpigen_write_name(CPPC_PACKAGE_NAME);
1735
1736 /* Adding 2 to account for length and version fields */
1737 acpigen_write_package(max + 2);
1738 acpigen_write_dword(max + 2);
1739
1740 acpigen_write_byte(config->version);
1741
1742 for (i = 0; i < max; ++i) {
1743 const acpi_addr_t *reg = &(config->regs[i]);
1744 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +02001745 reg->bit_width == 32 && reg->access_size == ACPI_ACCESS_SIZE_UNDEFINED) {
Matt Delcob425bc82018-08-13 13:36:27 -07001746 acpigen_write_dword(reg->addrl);
1747 } else {
1748 acpigen_write_register_resource(reg);
1749 }
1750 }
1751 acpigen_pop_len();
1752}
1753
1754void acpigen_write_CPPC_method(void)
1755{
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001756 char pscope[16];
1757 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
1758
Matt Delcob425bc82018-08-13 13:36:27 -07001759 acpigen_write_method("_CPC", 0);
1760 acpigen_emit_byte(RETURN_OP);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001761 acpigen_emit_namestring(pscope);
Matt Delcob425bc82018-08-13 13:36:27 -07001762 acpigen_pop_len();
1763}
1764
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001765/*
1766 * Generate ACPI AML code for _ROM method.
1767 * This function takes as input ROM data and ROM length.
1768 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001769 * The ACPI spec isn't clear about what should happen at the end of the
1770 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1771 * bytes in the returned buffer with zeros.
1772 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001773 * Arguments passed into _DSM method:
1774 * Arg0 = Offset in Bytes
1775 * Arg1 = Bytes to return
1776 *
1777 * Example:
1778 * acpigen_write_rom(0xdeadbeef, 0x10000)
1779 *
1780 * AML code generated would look like:
1781 * Method (_ROM, 2, NotSerialized) {
1782 *
1783 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1784 * Field (ROMS, AnyAcc, NoLock, Preserve)
1785 * {
1786 * Offset (0),
1787 * RBF0, 0x80000
1788 * }
1789 *
1790 * Store (Arg0, Local0)
1791 * Store (Arg1, Local1)
1792 *
1793 * If (LGreater (Local1, 0x1000))
1794 * {
1795 * Store (0x1000, Local1)
1796 * }
1797 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001798 * Store (Local1, Local3)
1799 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001800 * If (LGreater (Local0, 0x10000))
1801 * {
1802 * Return(Buffer(Local1){0})
1803 * }
1804 *
1805 * If (LGreater (Local0, 0x0f000))
1806 * {
1807 * Subtract (0x10000, Local0, Local2)
1808 * If (LGreater (Local1, Local2))
1809 * {
1810 * Store (Local2, Local1)
1811 * }
1812 * }
1813 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001814 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001815 *
1816 * Multiply (Local0, 0x08, Local0)
1817 * Multiply (Local1, 0x08, Local1)
1818 *
1819 * CreateField (RBF0, Local0, Local1, TMPB)
1820 * Store (TMPB, ROM1)
1821 * Return (ROM1)
1822 * }
1823 */
1824
1825void acpigen_write_rom(void *bios, const size_t length)
1826{
1827 ASSERT(bios)
1828 ASSERT(length)
1829
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001830 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001831 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001832
1833 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1834 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1835 (uintptr_t)bios, length);
1836 acpigen_write_opregion(&opreg);
1837
1838 struct fieldlist l[] = {
1839 FIELDLIST_OFFSET(0),
1840 FIELDLIST_NAMESTR("RBF0", 8 * length),
1841 };
1842
1843 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1844 * {
1845 * Offset (0),
1846 * RBF0, 0x80000
1847 * } */
1848 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1849 FIELD_NOLOCK | FIELD_PRESERVE);
1850
1851 /* Store (Arg0, Local0) */
1852 acpigen_write_store();
1853 acpigen_emit_byte(ARG0_OP);
1854 acpigen_emit_byte(LOCAL0_OP);
1855
1856 /* Store (Arg1, Local1) */
1857 acpigen_write_store();
1858 acpigen_emit_byte(ARG1_OP);
1859 acpigen_emit_byte(LOCAL1_OP);
1860
1861 /* ACPI SPEC requires to return at maximum 4KiB */
1862 /* If (LGreater (Local1, 0x1000)) */
1863 acpigen_write_if();
1864 acpigen_emit_byte(LGREATER_OP);
1865 acpigen_emit_byte(LOCAL1_OP);
1866 acpigen_write_integer(0x1000);
1867
1868 /* Store (0x1000, Local1) */
1869 acpigen_write_store();
1870 acpigen_write_integer(0x1000);
1871 acpigen_emit_byte(LOCAL1_OP);
1872
1873 /* Pop if */
1874 acpigen_pop_len();
1875
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001876 /* Store (Local1, Local3) */
1877 acpigen_write_store();
1878 acpigen_emit_byte(LOCAL1_OP);
1879 acpigen_emit_byte(LOCAL3_OP);
1880
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001881 /* If (LGreater (Local0, length)) */
1882 acpigen_write_if();
1883 acpigen_emit_byte(LGREATER_OP);
1884 acpigen_emit_byte(LOCAL0_OP);
1885 acpigen_write_integer(length);
1886
1887 /* Return(Buffer(Local1){0}) */
1888 acpigen_emit_byte(RETURN_OP);
1889 acpigen_emit_byte(BUFFER_OP);
1890 acpigen_write_len_f();
1891 acpigen_emit_byte(LOCAL1_OP);
1892 acpigen_emit_byte(0);
1893 acpigen_pop_len();
1894
1895 /* Pop if */
1896 acpigen_pop_len();
1897
1898 /* If (LGreater (Local0, length - 4096)) */
1899 acpigen_write_if();
1900 acpigen_emit_byte(LGREATER_OP);
1901 acpigen_emit_byte(LOCAL0_OP);
1902 acpigen_write_integer(length - 4096);
1903
1904 /* Subtract (length, Local0, Local2) */
1905 acpigen_emit_byte(SUBTRACT_OP);
1906 acpigen_write_integer(length);
1907 acpigen_emit_byte(LOCAL0_OP);
1908 acpigen_emit_byte(LOCAL2_OP);
1909
1910 /* If (LGreater (Local1, Local2)) */
1911 acpigen_write_if();
1912 acpigen_emit_byte(LGREATER_OP);
1913 acpigen_emit_byte(LOCAL1_OP);
1914 acpigen_emit_byte(LOCAL2_OP);
1915
1916 /* Store (Local2, Local1) */
1917 acpigen_write_store();
1918 acpigen_emit_byte(LOCAL2_OP);
1919 acpigen_emit_byte(LOCAL1_OP);
1920
1921 /* Pop if */
1922 acpigen_pop_len();
1923
1924 /* Pop if */
1925 acpigen_pop_len();
1926
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001927 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001928 acpigen_write_name("ROM1");
1929 acpigen_emit_byte(BUFFER_OP);
1930 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001931 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001932 acpigen_emit_byte(0);
1933 acpigen_pop_len();
1934
1935 /* Multiply (Local1, 0x08, Local1) */
1936 acpigen_emit_byte(MULTIPLY_OP);
1937 acpigen_emit_byte(LOCAL1_OP);
1938 acpigen_write_integer(0x08);
1939 acpigen_emit_byte(LOCAL1_OP);
1940
1941 /* Multiply (Local0, 0x08, Local0) */
1942 acpigen_emit_byte(MULTIPLY_OP);
1943 acpigen_emit_byte(LOCAL0_OP);
1944 acpigen_write_integer(0x08);
1945 acpigen_emit_byte(LOCAL0_OP);
1946
1947 /* CreateField (RBF0, Local0, Local1, TMPB) */
1948 acpigen_emit_ext_op(CREATEFIELD_OP);
1949 acpigen_emit_namestring("RBF0");
1950 acpigen_emit_byte(LOCAL0_OP);
1951 acpigen_emit_byte(LOCAL1_OP);
1952 acpigen_emit_namestring("TMPB");
1953
1954 /* Store (TMPB, ROM1) */
1955 acpigen_write_store();
1956 acpigen_emit_namestring("TMPB");
1957 acpigen_emit_namestring("ROM1");
1958
1959 /* Return (ROM1) */
1960 acpigen_emit_byte(RETURN_OP);
1961 acpigen_emit_namestring("ROM1");
1962
1963 /* Pop method */
1964 acpigen_pop_len();
1965}
1966
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001967/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001968int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001969{
1970 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1971 acpigen_write_debug_string("read_rx_gpio not available");
1972 return -1;
1973}
1974
Aaron Durbin64031672018-04-21 14:45:32 -06001975int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001976{
1977 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1978 acpigen_write_debug_string("get_tx_gpio not available");
1979 return -1;
1980}
1981
Aaron Durbin64031672018-04-21 14:45:32 -06001982int __weak acpigen_soc_set_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001983{
1984 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1985 acpigen_write_debug_string("set_tx_gpio not available");
1986 return -1;
1987}
1988
Aaron Durbin64031672018-04-21 14:45:32 -06001989int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001990{
1991 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1992 acpigen_write_debug_string("clear_tx_gpio not available");
1993 return -1;
1994}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001995
1996/*
1997 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1998 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1999 * make callbacks into SoC acpigen code.
2000 *
2001 * Returns 0 on success and -1 on error.
2002 */
Duncan Laurie30c3f912020-10-09 04:48:53 +00002003int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002004{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002005 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002006 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002007 else
2008 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002009}
2010
Duncan Laurie30c3f912020-10-09 04:48:53 +00002011int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002012{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002013 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08002014 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
2015 else
2016 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
2017}
Jonathan Zhang71299c22020-01-27 11:38:57 -08002018
Duncan Laurie30c3f912020-10-09 04:48:53 +00002019void acpigen_get_rx_gpio(const struct acpi_gpio *gpio)
Rajat Jain310623b2020-02-26 11:02:53 -08002020{
2021 acpigen_soc_read_rx_gpio(gpio->pins[0]);
2022
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002023 if (gpio->active_low)
Rajat Jain310623b2020-02-26 11:02:53 -08002024 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2025}
2026
Duncan Laurie30c3f912020-10-09 04:48:53 +00002027void acpigen_get_tx_gpio(const struct acpi_gpio *gpio)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002028{
2029 acpigen_soc_get_tx_gpio(gpio->pins[0]);
2030
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07002031 if (gpio->active_low)
Duncan Laurie2fe74712020-06-01 17:36:56 -07002032 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
2033}
2034
Jonathan Zhang71299c22020-01-27 11:38:57 -08002035/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
2036void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran,
2037 u16 range_min, u16 range_max, u16 translation, u16 length)
2038{
2039 acpigen_emit_byte(0x88);
2040 /* Byte 1+2: length (0x000d) */
2041 acpigen_emit_byte(0x0d);
2042 acpigen_emit_byte(0x00);
2043 /* resource type */
2044 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2045 /* general flags */
2046 acpigen_emit_byte(gen_flags);
2047 /* type flags */
2048 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2049 acpigen_emit_byte(type_flags);
2050 /* granularity, min, max, translation, length */
2051 acpigen_emit_word(gran);
2052 acpigen_emit_word(range_min);
2053 acpigen_emit_word(range_max);
2054 acpigen_emit_word(translation);
2055 acpigen_emit_word(length);
2056}
2057
2058/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
2059void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags,
2060 u32 gran, u32 range_min, u32 range_max, u32 translation, u32 length)
2061{
2062 acpigen_emit_byte(0x87);
2063 /* Byte 1+2: length (0023) */
2064 acpigen_emit_byte(23);
2065 acpigen_emit_byte(0x00);
2066 /* resource type */
2067 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2068 /* general flags */
2069 acpigen_emit_byte(gen_flags);
2070 /* type flags */
2071 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2072 acpigen_emit_byte(type_flags);
2073 /* granularity, min, max, translation, length */
2074 acpigen_emit_dword(gran);
2075 acpigen_emit_dword(range_min);
2076 acpigen_emit_dword(range_max);
2077 acpigen_emit_dword(translation);
2078 acpigen_emit_dword(length);
2079}
2080
2081static void acpigen_emit_qword(u64 data)
2082{
2083 acpigen_emit_dword(data & 0xffffffff);
2084 acpigen_emit_dword((data >> 32) & 0xffffffff);
2085}
2086
2087/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
2088void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags,
2089 u64 gran, u64 range_min, u64 range_max, u64 translation, u64 length)
2090{
2091 acpigen_emit_byte(0x8a);
2092 /* Byte 1+2: length (0x002b) */
2093 acpigen_emit_byte(0x2b);
2094 acpigen_emit_byte(0x00);
2095 /* resource type */
2096 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2097 /* general flags */
2098 acpigen_emit_byte(gen_flags);
2099 /* type flags */
2100 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2101 acpigen_emit_byte(type_flags);
2102 /* granularity, min, max, translation, length */
2103 acpigen_emit_qword(gran);
2104 acpigen_emit_qword(range_min);
2105 acpigen_emit_qword(range_max);
2106 acpigen_emit_qword(translation);
2107 acpigen_emit_qword(length);
2108}
Furquan Shaikh1a829232020-04-23 11:11:05 -07002109
2110void acpigen_write_ADR(uint64_t adr)
2111{
2112 acpigen_write_name_qword("_ADR", adr);
2113}
2114
Duncan Laurie08a942f2020-04-29 12:13:14 -07002115/**
2116 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2117 * @address: SoundWire device address properties.
2118 *
2119 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2120 *
2121 * 63..52 - Reserved (0)
2122 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2123 * Used when a Controller has multiple master devices, each producing a
2124 * separate SoundWire Link. Set to 0 for single-link controllers.
2125 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2126 * 47..44 - SoundWire specification version that this device supports
2127 * 43..40 - Unique ID for multiple devices
2128 * 39..24 - MIPI standard manufacturer code
2129 * 23..08 - Vendor defined part ID
2130 * 07..00 - MIPI class encoding
2131 */
2132void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
2133{
2134 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
2135 (((uint64_t)address->version & 0xf) << 44) |
2136 (((uint64_t)address->unique_id & 0xf) << 40) |
2137 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
2138 (((uint64_t)address->part_id & 0xffff) << 8) |
2139 (((uint64_t)address->class & 0xff)));
2140}
Tim Wawrzynczakf6945022020-06-04 15:18:47 -06002141
2142void acpigen_notify(const char *namestr, int value)
2143{
2144 acpigen_emit_byte(NOTIFY_OP);
2145 acpigen_emit_namestring(namestr);
2146 acpigen_write_integer(value);
2147}
Aaron Durbin80bc0912020-08-19 23:17:42 -06002148
2149static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name)
2150{
2151 acpigen_emit_byte(aml_op);
2152 acpigen_emit_byte(srcop);
2153 acpigen_write_integer(byte_offset);
2154 acpigen_emit_namestring(name);
2155}
2156
2157void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name)
2158{
2159 _create_field(CREATE_BYTE_OP, op, byte_offset, name);
2160}
2161
2162void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name)
2163{
2164 _create_field(CREATE_WORD_OP, op, byte_offset, name);
2165}
2166
2167void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name)
2168{
2169 _create_field(CREATE_DWORD_OP, op, byte_offset, name);
2170}
2171
2172void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name)
2173{
2174 _create_field(CREATE_QWORD_OP, op, byte_offset, name);
2175}
Jason Gleneskca36aed2020-09-15 21:01:57 -07002176
2177void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
2178{
2179 acpigen_write_name("_PCT");
2180 acpigen_write_package(0x02);
2181 acpigen_write_register_resource(perf_ctrl);
2182 acpigen_write_register_resource(perf_sts);
2183
2184 acpigen_pop_len();
2185}
2186
2187void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value)
2188{
2189 acpigen_write_package(0x08);
2190 acpigen_write_dword(pstate_value->core_freq);
2191 acpigen_write_dword(pstate_value->power);
2192 acpigen_write_dword(pstate_value->transition_latency);
2193 acpigen_write_dword(pstate_value->bus_master_latency);
2194
2195 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t));
2196 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t));
2197 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t));
2198 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t));
2199
2200 acpigen_pop_len();
2201}
2202
2203void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
2204{
2205 size_t pstate;
2206
2207 acpigen_write_name("XPSS");
2208 acpigen_write_package(nentries);
2209 for (pstate = 0; pstate < nentries; pstate++) {
2210 acpigen_write_xpss_package(pstate_values);
2211 pstate_values++;
2212 }
2213
2214 acpigen_pop_len();
2215}
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002216
2217/* Delay up to wait_ms until provided namestr matches expected value. */
2218void acpigen_write_delay_until_namestr_int(uint32_t wait_ms, const char *name, uint64_t value)
2219{
2220 uint32_t wait_ms_segment = 1;
2221 uint32_t segments = wait_ms;
2222
2223 /* Sleep in 16ms segments if delay is more than 32ms. */
2224 if (wait_ms > 32) {
2225 wait_ms_segment = 16;
2226 segments = wait_ms / 16;
2227 }
2228
2229 acpigen_write_store_int_to_op(segments, LOCAL7_OP);
2230 acpigen_emit_byte(WHILE_OP);
2231 acpigen_write_len_f();
2232 acpigen_emit_byte(LGREATER_OP);
2233 acpigen_emit_byte(LOCAL7_OP);
2234 acpigen_emit_byte(ZERO_OP);
2235
2236 /* If name is not provided then just delay in a loop. */
2237 if (name) {
2238 acpigen_write_if_lequal_namestr_int(name, value);
2239 acpigen_emit_byte(BREAK_OP);
2240 acpigen_pop_len(); /* If */
2241 }
2242
2243 acpigen_write_sleep(wait_ms_segment);
2244 acpigen_emit_byte(DECREMENT_OP);
2245 acpigen_emit_byte(LOCAL7_OP);
2246 acpigen_pop_len(); /* While */
2247}