blob: 244ac548c92f76cf6ba562327007d6dc93986fd5 [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>
Rudolf Marek293b5f52009-02-01 18:35:15 +000024
25static char *gencurrent;
26
27char *len_stack[ACPIGEN_LENSTACK_SIZE];
28int ltop = 0;
29
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010030void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000031{
32 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010033 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000034 acpigen_emit_byte(0);
35 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050036 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000037}
38
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010039void acpigen_pop_len(void)
40{
41 int len;
42 ASSERT(ltop > 0)
43 char *p = len_stack[--ltop];
44 len = gencurrent - p;
45 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050046 /* generate store length for 0xfffff max */
47 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010048 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050049 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010050
51}
52
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000053void acpigen_set_current(char *curr)
54{
55 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000056}
57
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000058char *acpigen_get_current(void)
59{
60 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000061}
62
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010063void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000064{
65 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000066}
67
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070068void acpigen_emit_ext_op(uint8_t op)
69{
70 acpigen_emit_byte(EXT_OP_PREFIX);
71 acpigen_emit_byte(op);
72}
73
Duncan Laurie9ccae752016-05-09 07:43:19 -070074void acpigen_emit_word(unsigned int data)
75{
76 acpigen_emit_byte(data & 0xff);
77 acpigen_emit_byte((data >> 8) & 0xff);
78}
79
80void acpigen_emit_dword(unsigned int data)
81{
82 acpigen_emit_byte(data & 0xff);
83 acpigen_emit_byte((data >> 8) & 0xff);
84 acpigen_emit_byte((data >> 16) & 0xff);
85 acpigen_emit_byte((data >> 24) & 0xff);
86}
87
Duncan Laurie85d80272016-07-02 19:53:54 -070088char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000089{
Duncan Laurie85d80272016-07-02 19:53:54 -070090 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070091 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010092 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -070093 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +000094 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -070095 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +000096}
97
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010098void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000099{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700100 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000101 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000102}
103
Duncan Laurie9ccae752016-05-09 07:43:19 -0700104void acpigen_write_word(unsigned int data)
105{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700106 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700107 acpigen_emit_word(data);
108}
109
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100110void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000111{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700112 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700113 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000114}
115
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100116void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000117{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700118 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700119 acpigen_emit_dword(data & 0xffffffff);
120 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000121}
122
Duncan Laurief7c38762016-05-09 08:20:38 -0700123void acpigen_write_zero(void)
124{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700125 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700126}
127
128void acpigen_write_one(void)
129{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700130 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700131}
132
133void acpigen_write_ones(void)
134{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700135 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700136}
137
138void acpigen_write_integer(uint64_t data)
139{
140 if (data == 0)
141 acpigen_write_zero();
142 else if (data == 1)
143 acpigen_write_one();
144 else if (data <= 0xff)
145 acpigen_write_byte((unsigned char)data);
146 else if (data <= 0xffff)
147 acpigen_write_word((unsigned int)data);
148 else if (data <= 0xffffffff)
149 acpigen_write_dword((unsigned int)data);
150 else
151 acpigen_write_qword(data);
152}
153
154void acpigen_write_name_zero(const char *name)
155{
156 acpigen_write_name(name);
157 acpigen_write_one();
158}
159
160void acpigen_write_name_one(const char *name)
161{
162 acpigen_write_name(name);
163 acpigen_write_zero();
164}
165
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100166void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000167{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100168 acpigen_write_name(name);
169 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000170}
171
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100172void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000173{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100174 acpigen_write_name(name);
175 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000176}
177
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100178void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000179{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100180 acpigen_write_name(name);
181 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000182}
183
Duncan Laurief7c38762016-05-09 08:20:38 -0700184void acpigen_write_name_integer(const char *name, uint64_t val)
185{
186 acpigen_write_name(name);
187 acpigen_write_integer(val);
188}
189
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700190void acpigen_write_name_string(const char *name, const char *string)
191{
192 acpigen_write_name(name);
193 acpigen_write_string(string);
194}
195
Patrick Rudolph389c8272019-12-17 13:54:41 +0100196void acpigen_write_name_unicode(const char *name, const char *string)
197{
198 const size_t len = strlen(string) + 1;
199 acpigen_write_name(name);
200 acpigen_emit_byte(BUFFER_OP);
201 acpigen_write_len_f();
202 acpigen_write_integer(len);
203 for (size_t i = 0; i < len; i++) {
204 const char c = string[i];
205 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
206 acpigen_emit_word(c >= 0 ? c : '?');
207 }
208 acpigen_pop_len();
209}
210
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100211void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000212{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000213 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700214 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000215 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000216}
217
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700218void acpigen_emit_string(const char *string)
219{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200220 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700221 acpigen_emit_byte('\0'); /* NUL */
222}
223
224void acpigen_write_string(const char *string)
225{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700226 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700227 acpigen_emit_string(string);
228}
229
Duncan Laurie37319032016-05-26 12:47:05 -0700230void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
231{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800232 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700233
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700234 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700235 acpigen_write_name_string("_HID", hid);
236}
237
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100238/*
239 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600240 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
241 * and "By convention, when an ASL compiler pads a name shorter than 4
242 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100243 *
244 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
245 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000246
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700247static void acpigen_emit_simple_namestring(const char *name)
248{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100249 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000250 char ud[] = "____";
251 for (i = 0; i < 4; i++) {
252 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100253 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000254 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000255 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700256 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000257 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000258}
259
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700260static void acpigen_emit_double_namestring(const char *name, int dotpos)
261{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700262 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100263 acpigen_emit_simple_namestring(name);
264 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000265}
266
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700267static void acpigen_emit_multi_namestring(const char *name)
268{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100269 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000270 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700271 acpigen_emit_byte(MULTI_NAME_PREFIX);
272 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000273 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
274
275 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100276 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000277 /* find end or next entity */
278 while ((name[0] != '.') && (name[0] != '\0'))
279 name++;
280 /* forward to next */
281 if (name[0] == '.')
282 name++;
283 count++;
284 }
285
286 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000287}
288
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700289void acpigen_emit_namestring(const char *namepath)
290{
Rudolf Marek3310d752009-06-21 20:26:13 +0000291 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000292 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000293
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600294 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000295 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100296 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000297 namepath++;
298 }
299
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600300 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000301 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100302 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000303 namepath++;
304 }
305
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200306 /* If we have only \\ or only ^...^. Then we need to put a null
307 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200308 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700309 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100310 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200311 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000312
313 i = 0;
314 while (namepath[i] != '\0') {
315 if (namepath[i] == '.') {
316 dotcount++;
317 dotpos = i;
318 }
319 i++;
320 }
321
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700322 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100323 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700324 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100325 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700326 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100327 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000328}
329
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100330void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000331{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700332 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100333 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000334}
335
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100336void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000337{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700338 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100339 acpigen_write_len_f();
340 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000341}
Rudolf Marekf997b552009-02-14 15:40:23 +0000342
Duncan Laurie2fe74712020-06-01 17:36:56 -0700343void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
344{
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800345 /* <dest_op> = DeRefOf (<package_op>[<element>]) */
Duncan Laurie2fe74712020-06-01 17:36:56 -0700346 acpigen_write_store();
347 acpigen_emit_byte(DEREF_OP);
348 acpigen_emit_byte(INDEX_OP);
349 acpigen_emit_byte(package_op);
350 acpigen_write_integer(element);
351 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
352 acpigen_emit_byte(dest_op);
353}
354
Duncan Laurieec2e3e42020-11-03 15:19:08 -0800355void acpigen_set_package_op_element_int(uint8_t package_op, unsigned int element, uint64_t src)
356{
357 /* DeRefOf (<package>[<element>]) = <src> */
358 acpigen_write_store();
359 acpigen_write_integer(src);
360 acpigen_emit_byte(DEREF_OP);
361 acpigen_emit_byte(INDEX_OP);
362 acpigen_emit_byte(package_op);
363 acpigen_write_integer(element);
364 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
365}
366
367void acpigen_get_package_element(const char *package, unsigned int element, uint8_t dest_op)
368{
369 /* <dest_op> = <package>[<element>] */
370 acpigen_write_store();
371 acpigen_emit_byte(INDEX_OP);
372 acpigen_emit_namestring(package);
373 acpigen_write_integer(element);
374 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
375 acpigen_emit_byte(dest_op);
376}
377
378void acpigen_set_package_element_int(const char *package, unsigned int element, uint64_t src)
379{
380 /* <package>[<element>] = <src> */
381 acpigen_write_store();
382 acpigen_write_integer(src);
383 acpigen_emit_byte(INDEX_OP);
384 acpigen_emit_namestring(package);
385 acpigen_write_integer(element);
386 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
387}
388
389void acpigen_set_package_element_namestr(const char *package, unsigned int element,
390 const char *src)
391{
392 /* <package>[<element>] = <src> */
393 acpigen_write_store();
394 acpigen_emit_namestring(src);
395 acpigen_emit_byte(INDEX_OP);
396 acpigen_emit_namestring(package);
397 acpigen_write_integer(element);
398 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
399}
400
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100401void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000402{
403/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100404 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200405 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000406*/
407 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700408 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100409 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000410
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200411 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600412 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100413 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000414 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700415 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000416 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000417}
418
Nico Huber4d211ac2017-09-01 21:14:36 +0200419void acpigen_write_processor_package(const char *const name,
420 const unsigned int first_core,
421 const unsigned int core_count)
422{
423 unsigned int i;
424 char pscope[16];
425
426 acpigen_write_name(name);
427 acpigen_write_package(core_count);
428 for (i = first_core; i < first_core + core_count; ++i) {
429 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
430 acpigen_emit_namestring(pscope);
431 }
432 acpigen_pop_len();
433}
434
Arthur Heymans8f055272018-11-28 13:18:57 +0100435/* Method to notify all CPU cores */
436void acpigen_write_processor_cnot(const unsigned int number_of_cores)
437{
438 int core_id;
439
Christian Walterbe3979c2019-12-18 15:07:59 +0100440 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100441 for (core_id = 0; core_id < number_of_cores; core_id++) {
442 char buffer[DEVICE_PATH_MAX];
443 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
444 core_id);
445 acpigen_emit_byte(NOTIFY_OP);
446 acpigen_emit_namestring(buffer);
447 acpigen_emit_byte(ARG0_OP);
448 }
449 acpigen_pop_len();
450}
451
Naresh G Solanki8535c662016-10-25 00:51:24 +0530452/*
453 * Generate ACPI AML code for OperationRegion
454 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
455 * where rname is region name, space is region space, offset is region offset &
456 * len is region length.
457 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
458 */
Duncan Laurie35029602020-10-09 17:50:25 +0000459void acpigen_write_opregion(const struct opregion *opreg)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530460{
461 /* OpregionOp */
462 acpigen_emit_ext_op(OPREGION_OP);
463 /* NameString 4 chars only */
464 acpigen_emit_simple_namestring(opreg->name);
465 /* RegionSpace */
466 acpigen_emit_byte(opreg->regionspace);
467 /* RegionOffset & RegionLen, it can be byte word or double word */
468 acpigen_write_integer(opreg->regionoffset);
469 acpigen_write_integer(opreg->regionlen);
470}
471
Patrick Rudolph32bae492019-08-08 12:47:30 +0200472/*
473 * Generate ACPI AML code for Mutex
474 * Arg0: Pointer to name of mutex
475 * Arg1: Initial value of mutex
476 */
477void acpigen_write_mutex(const char *name, const uint8_t flags)
478{
479 /* MutexOp */
480 acpigen_emit_ext_op(MUTEX_OP);
481 /* NameString 4 chars only */
482 acpigen_emit_simple_namestring(name);
483 acpigen_emit_byte(flags);
484}
485
486void acpigen_write_acquire(const char *name, const uint16_t val)
487{
488 /* AcquireOp */
489 acpigen_emit_ext_op(ACQUIRE_OP);
490 /* NameString 4 chars only */
491 acpigen_emit_simple_namestring(name);
492 acpigen_emit_word(val);
493}
494
495void acpigen_write_release(const char *name)
496{
497 /* ReleaseOp */
498 acpigen_emit_ext_op(RELEASE_OP);
499 /* NameString 4 chars only */
500 acpigen_emit_simple_namestring(name);
501}
502
Patrick Rudolph894977b2017-07-01 16:15:05 +0200503static void acpigen_write_field_length(uint32_t len)
504{
505 uint8_t i, j;
506 uint8_t emit[4];
507
508 i = 1;
509 if (len < 0x40) {
510 emit[0] = len & 0x3F;
511 } else {
512 emit[0] = len & 0xF;
513 len >>= 4;
514 while (len) {
515 emit[i] = len & 0xFF;
516 i++;
517 len >>= 8;
518 }
519 }
520 /* Update bit 7:6 : Number of bytes followed by emit[0] */
521 emit[0] |= (i - 1) << 6;
522
523 for (j = 0; j < i; j++)
524 acpigen_emit_byte(emit[j]);
525}
526
Naresh G Solanki8535c662016-10-25 00:51:24 +0530527static void acpigen_write_field_offset(uint32_t offset,
528 uint32_t current_bit_pos)
529{
530 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530531
532 if (offset < current_bit_pos) {
533 printk(BIOS_WARNING, "%s: Cannot move offset backward",
534 __func__);
535 return;
536 }
537
538 diff_bits = offset - current_bit_pos;
539 /* Upper limit */
540 if (diff_bits > 0xFFFFFFF) {
541 printk(BIOS_WARNING, "%s: Offset very large to encode",
542 __func__);
543 return;
544 }
545
Naresh G Solanki8535c662016-10-25 00:51:24 +0530546 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200547 acpigen_write_field_length(diff_bits);
548}
549
550static void acpigen_write_field_name(const char *name, uint32_t size)
551{
552 acpigen_emit_simple_namestring(name);
553 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530554}
555
Duncan Laurie095bbf92020-09-30 23:09:29 +0000556static void acpigen_write_field_reserved(uint32_t size)
557{
558 acpigen_emit_byte(0);
559 acpigen_write_field_length(size);
560}
561
Naresh G Solanki8535c662016-10-25 00:51:24 +0530562/*
563 * Generate ACPI AML code for Field
564 * Arg0: region name
565 * Arg1: Pointer to struct fieldlist.
566 * Arg2: no. of entries in Arg1
567 * Arg3: flags which indicate filed access type, lock rule & update rule.
568 * Example with fieldlist
569 * struct fieldlist l[] = {
570 * FIELDLIST_OFFSET(0x84),
571 * FIELDLIST_NAMESTR("PMCS", 2),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000572 * FIELDLIST_RESERVED(6),
Naresh G Solanki8535c662016-10-25 00:51:24 +0530573 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200574 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530575 * FIELD_PRESERVE);
576 * Output:
577 * Field (UART, AnyAcc, NoLock, Preserve)
578 * {
579 * Offset (0x84),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000580 * PMCS, 2,
581 * , 6,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530582 * }
583 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700584void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530585 uint8_t flags)
586{
587 uint16_t i;
588 uint32_t current_bit_pos = 0;
589
590 /* FieldOp */
591 acpigen_emit_ext_op(FIELD_OP);
592 /* Package Length */
593 acpigen_write_len_f();
594 /* NameString 4 chars only */
595 acpigen_emit_simple_namestring(name);
596 /* Field Flag */
597 acpigen_emit_byte(flags);
598
599 for (i = 0; i < count; i++) {
600 switch (l[i].type) {
601 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200602 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530603 current_bit_pos += l[i].bits;
604 break;
Duncan Laurie095bbf92020-09-30 23:09:29 +0000605 case RESERVED:
606 acpigen_write_field_reserved(l[i].bits);
607 current_bit_pos += l[i].bits;
608 break;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530609 case OFFSET:
610 acpigen_write_field_offset(l[i].bits, current_bit_pos);
611 current_bit_pos = l[i].bits;
612 break;
613 default:
614 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
615 , __func__, l[i].type);
616 break;
617 }
618 }
619 acpigen_pop_len();
620}
621
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200622/*
623 * Generate ACPI AML code for IndexField
624 * Arg0: region name
625 * Arg1: Pointer to struct fieldlist.
626 * Arg2: no. of entries in Arg1
627 * Arg3: flags which indicate filed access type, lock rule & update rule.
628 * Example with fieldlist
629 * struct fieldlist l[] = {
630 * FIELDLIST_OFFSET(0x84),
631 * FIELDLIST_NAMESTR("PMCS", 2),
632 * };
633 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
634 * FIELD_NOLOCK |
635 * FIELD_PRESERVE);
636 * Output:
637 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
638 * {
639 * Offset (0x84),
640 * PMCS, 2
641 * }
642 */
643void acpigen_write_indexfield(const char *idx, const char *data,
644 struct fieldlist *l, size_t count, uint8_t flags)
645{
646 uint16_t i;
647 uint32_t current_bit_pos = 0;
648
649 /* FieldOp */
650 acpigen_emit_ext_op(INDEX_FIELD_OP);
651 /* Package Length */
652 acpigen_write_len_f();
653 /* NameString 4 chars only */
654 acpigen_emit_simple_namestring(idx);
655 /* NameString 4 chars only */
656 acpigen_emit_simple_namestring(data);
657 /* Field Flag */
658 acpigen_emit_byte(flags);
659
660 for (i = 0; i < count; i++) {
661 switch (l[i].type) {
662 case NAME_STRING:
663 acpigen_write_field_name(l[i].name, l[i].bits);
664 current_bit_pos += l[i].bits;
665 break;
666 case OFFSET:
667 acpigen_write_field_offset(l[i].bits, current_bit_pos);
668 current_bit_pos = l[i].bits;
669 break;
670 default:
671 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
672 , __func__, l[i].type);
673 break;
674 }
675 }
676 acpigen_pop_len();
677}
678
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100679void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000680{
681/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200682 Name (_PCT, Package (0x02)
683 {
684 ResourceTemplate ()
685 {
686 Register (FFixedHW,
687 0x00, // Bit Width
688 0x00, // Bit Offset
689 0x0000000000000000, // Address
690 ,)
691 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000692
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200693 ResourceTemplate ()
694 {
695 Register (FFixedHW,
696 0x00, // Bit Width
697 0x00, // Bit Offset
698 0x0000000000000000, // Address
699 ,)
700 }
701 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000702*/
703 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700704 /* 00000030 "0._PCT.," */
705 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
706 /* 00000038 "........" */
707 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
708 /* 00000040 "........" */
709 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
710 /* 00000048 "....y..." */
711 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
712 /* 00000050 "........" */
713 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
714 /* 00000058 "........" */
715 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000716 0x00, 0x79, 0x00
717 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100718 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000719}
720
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100721void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200722{
723/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200724 Name (_PTC, Package (0x02)
725 {
726 ResourceTemplate ()
727 {
728 Register (FFixedHW,
729 0x00, // Bit Width
730 0x00, // Bit Offset
731 0x0000000000000000, // Address
732 ,)
733 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200734
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200735 ResourceTemplate ()
736 {
737 Register (FFixedHW,
738 0x00, // Bit Width
739 0x00, // Bit Offset
740 0x0000000000000000, // Address
741 ,)
742 }
743 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200744*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200745 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100746 .space_id = ACPI_ADDRESS_SPACE_FIXED,
747 .bit_width = 0,
748 .bit_offset = 0,
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +0200749 .access_size = ACPI_ACCESS_SIZE_UNDEFINED,
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100750 .addrl = 0,
751 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200752 };
753
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100754 acpigen_write_name("_PTC");
755 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200756
757 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700758 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200759
760 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700761 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200762
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100763 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200764}
765
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700766static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100767{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700768 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100769 acpigen_write_len_f();
770 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700771 acpigen_emit_byte(flags);
772}
773
774/* Method (name, nargs, NotSerialized) */
775void acpigen_write_method(const char *name, int nargs)
776{
777 __acpigen_write_method(name, (nargs & 7));
778}
779
780/* Method (name, nargs, Serialized) */
781void acpigen_write_method_serialized(const char *name, int nargs)
782{
783 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100784}
785
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100786void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100787{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700788 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100789 acpigen_write_len_f();
790 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100791}
792
Duncan Laurieabe2de82016-05-09 11:08:46 -0700793void acpigen_write_STA(uint8_t status)
794{
795 /*
796 * Method (_STA, 0, NotSerialized) { Return (status) }
797 */
798 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700799 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700800 acpigen_write_byte(status);
801 acpigen_pop_len();
802}
803
Sugnan Prabhu S4f77f612020-06-10 09:51:02 +0530804void acpigen_write_STA_ext(const char *namestring)
805{
806 /*
807 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
808 */
809 acpigen_write_method("_STA", 0);
810 acpigen_emit_byte(RETURN_OP);
811 acpigen_emit_namestring(namestring);
812 acpigen_pop_len();
813}
814
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100815/*
816 * Generates a func with max supported P-states.
817 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100818void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000819{
820/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200821 Method (_PPC, 0, NotSerialized)
822 {
823 Return (nr)
824 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000825*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100826 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700827 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000828 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100829 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100830 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000831}
832
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100833/*
834 * Generates a func with max supported P-states saved
835 * in the variable PPCM.
836 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100837void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700838{
839/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200840 Method (_PPC, 0, NotSerialized)
841 {
842 Return (PPCM)
843 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700844*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100845 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700846 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700847 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100848 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100849 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700850}
851
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100852void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200853{
854/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200855 // Sample _TPC method
856 Method (_TPC, 0, NotSerialized)
857 {
858 Return (\TLVL)
859 }
860*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100861 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700862 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100863 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100864 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200865}
866
Duncan Laurieabe2de82016-05-09 11:08:46 -0700867void acpigen_write_PRW(u32 wake, u32 level)
868{
869 /*
870 * Name (_PRW, Package () { wake, level }
871 */
872 acpigen_write_name("_PRW");
873 acpigen_write_package(2);
874 acpigen_write_integer(wake);
875 acpigen_write_integer(level);
876 acpigen_pop_len();
877}
878
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100879void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000880 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000881{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100882 acpigen_write_package(6);
883 acpigen_write_dword(coreFreq);
884 acpigen_write_dword(power);
885 acpigen_write_dword(transLat);
886 acpigen_write_dword(busmLat);
887 acpigen_write_dword(control);
888 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100889 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700890
891 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
892 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000893}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000894
Jason Gleneskca36aed2020-09-15 21:01:57 -0700895void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
896{
897 size_t pstate;
898
899 acpigen_write_name("_PSS");
900 acpigen_write_package(nentries);
901 for (pstate = 0; pstate < nentries; pstate++) {
902 acpigen_write_PSS_package(
903 pstate_values->core_freq, pstate_values->power,
904 pstate_values->transition_latency, pstate_values->bus_master_latency,
905 pstate_values->control_value, pstate_values->status_value);
906 pstate_values++;
907 }
908
909 acpigen_pop_len();
910}
911
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100912void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000913{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100914 acpigen_write_name("_PSD");
915 acpigen_write_package(1);
916 acpigen_write_package(5);
917 acpigen_write_byte(5); // 5 values
918 acpigen_write_byte(0); // revision 0
919 acpigen_write_dword(domain);
920 acpigen_write_dword(coordtype);
921 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100922 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100923 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000924}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000925
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100926void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200927{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100928 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700929 acpigen_write_register_resource(&cstate->resource);
Jason Glenesk201acca2020-09-11 12:36:15 -0700930 acpigen_write_byte(cstate->ctype);
931 acpigen_write_word(cstate->latency);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100932 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100933 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200934}
935
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100936void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200937{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100938 int i;
939 acpigen_write_name("_CST");
940 acpigen_write_package(nentries+1);
Jason Glenesk201acca2020-09-11 12:36:15 -0700941 acpigen_write_integer(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200942
943 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100944 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200945
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100946 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200947}
948
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700949void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
950 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500951{
952 acpigen_write_name("_CSD");
953 acpigen_write_package(1);
954 acpigen_write_package(6);
Jason Glenesk201acca2020-09-11 12:36:15 -0700955 acpigen_write_integer(6); // 6 values
Timothy Pearson83abd812015-06-08 19:35:06 -0500956 acpigen_write_byte(0); // revision 0
957 acpigen_write_dword(domain);
958 acpigen_write_dword(coordtype);
959 acpigen_write_dword(numprocs);
960 acpigen_write_dword(index);
961 acpigen_pop_len();
962 acpigen_pop_len();
963}
964
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100965void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200966{
967/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200968 Sample _TSS package with 100% and 50% duty cycles
969 Name (_TSS, Package (0x02)
970 {
971 Package(){100, 1000, 0, 0x00, 0)
972 Package(){50, 520, 0, 0x18, 0)
973 })
974*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100975 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200976 acpi_tstate_t *tstate = tstate_list;
977
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100978 acpigen_write_name("_TSS");
979 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200980
981 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100982 acpigen_write_package(5);
983 acpigen_write_dword(tstate->percent);
984 acpigen_write_dword(tstate->power);
985 acpigen_write_dword(tstate->latency);
986 acpigen_write_dword(tstate->control);
987 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100988 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200989 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200990 }
991
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100992 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200993}
994
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100995void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200996{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100997 acpigen_write_name("_TSD");
998 acpigen_write_package(1);
999 acpigen_write_package(5);
1000 acpigen_write_byte(5); // 5 values
1001 acpigen_write_byte(0); // revision 0
1002 acpigen_write_dword(domain);
1003 acpigen_write_dword(coordtype);
1004 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001005 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001006 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +02001007}
1008
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001009void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001010{
1011 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001012 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001013 * Byte 0:
1014 * Bit7 : 1 => big item
1015 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
1016 */
1017 acpigen_emit_byte(0x86);
1018 /* Byte 1+2: length (0x0009) */
1019 acpigen_emit_byte(0x09);
1020 acpigen_emit_byte(0x00);
1021 /* bit1-7 are ignored */
1022 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -07001023 acpigen_emit_dword(base);
1024 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001025}
1026
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001027static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +02001028{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +02001029 acpigen_emit_byte(0x82); /* Register Descriptor */
1030 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
1031 acpigen_emit_byte(0x00); /* Register Length 15:8 */
1032 acpigen_emit_byte(addr->space_id); /* Address Space ID */
1033 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
1034 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +01001035 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -07001036 acpigen_emit_dword(addr->addrl); /* Register Address Low */
1037 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +02001038}
1039
Matt Delcoc3f9d7a2018-07-27 14:01:05 -07001040void acpigen_write_register_resource(const acpi_addr_t *addr)
1041{
1042 acpigen_write_resourcetemplate_header();
1043 acpigen_write_register(addr);
1044 acpigen_write_resourcetemplate_footer();
1045}
1046
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001047void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001048{
1049 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001050 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001051 * Byte 0:
1052 * Bit7 : 0 => small item
1053 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1054 * Bit2-0: 010 (0x2) => 2 Bytes long
1055 */
1056 acpigen_emit_byte(0x22);
1057 acpigen_emit_byte(mask & 0xff);
1058 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001059}
1060
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001061void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001062{
1063 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001064 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001065 * Byte 0:
1066 * Bit7 : 0 => small item
1067 * Bit6-3: 1000 (0x8) => I/O port descriptor
1068 * Bit2-0: 111 (0x7) => 7 Bytes long
1069 */
1070 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001071 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001072 /* bit1-7 are ignored */
1073 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1074 /* minimum base address the device may be configured for */
1075 acpigen_emit_byte(min & 0xff);
1076 acpigen_emit_byte((min >> 8) & 0xff);
1077 /* maximum base address the device may be configured for */
1078 acpigen_emit_byte(max & 0xff);
1079 acpigen_emit_byte((max >> 8) & 0xff);
1080 /* alignment for min base */
1081 acpigen_emit_byte(align & 0xff);
1082 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001083}
1084
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001085void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001086{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001087 /*
1088 * A ResourceTemplate() is a Buffer() with a
1089 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001090 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001091 * (small item 0xf, len 1)
1092 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001093 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001094 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001095 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001096 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001097 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001098 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001099 acpigen_emit_byte(0x00);
1100 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001101}
1102
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001103void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001104{
1105 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001106 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001107 /*
1108 * end tag (acpi 4.0 Section 6.4.2.8)
1109 * 0x79 <checksum>
1110 * 0x00 is treated as a good checksum according to the spec
1111 * and is what iasl generates.
1112 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001113 acpigen_emit_byte(0x79);
1114 acpigen_emit_byte(0x00);
1115
Nico Huber7d89ce32017-04-14 00:08:18 +02001116 /* Start counting past the 2-bytes length added in
1117 acpigen_write_resourcetemplate() above. */
1118 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001119
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001120 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001121 p[0] = len & 0xff;
1122 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001123 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001124 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001125}
1126
1127static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1128 struct resource *res)
1129{
1130 acpigen_write_mem32fixed(0, res->base, res->size);
1131}
1132
1133static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1134 struct resource *res)
1135{
1136 resource_t base = res->base;
1137 resource_t size = res->size;
1138 while (size > 0) {
1139 resource_t sz = size > 255 ? 255 : size;
1140 acpigen_write_io16(base, base, 0, sz, 1);
1141 size -= sz;
1142 base += sz;
1143 }
1144}
1145
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001146void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001147{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001148 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001149
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001150 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001151 search_global_resources(
1152 IORESOURCE_MEM | IORESOURCE_RESERVE,
1153 IORESOURCE_MEM | IORESOURCE_RESERVE,
1154 acpigen_add_mainboard_rsvd_mem32, 0);
1155
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001156 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001157 search_global_resources(
1158 IORESOURCE_IO | IORESOURCE_RESERVE,
1159 IORESOURCE_IO | IORESOURCE_RESERVE,
1160 acpigen_add_mainboard_rsvd_io, 0);
1161
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001162 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001163}
1164
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001165void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001166{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001167 acpigen_write_scope(scope);
1168 acpigen_write_name(name);
1169 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001170 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001171}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001172
1173static int hex2bin(const char c)
1174{
1175 if (c >= 'A' && c <= 'F')
1176 return c - 'A' + 10;
1177 if (c >= 'a' && c <= 'f')
1178 return c - 'a' + 10;
1179 return c - '0';
1180}
1181
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001182void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001183{
1184 u32 compact = 0;
1185
1186 /* Clamping individual values would be better but
1187 there is a disagreement over what is a valid
1188 EISA id, so accept anything and don't clamp,
1189 parent code should create a valid EISAid.
1190 */
1191 compact |= (eisaid[0] - 'A' + 1) << 26;
1192 compact |= (eisaid[1] - 'A' + 1) << 21;
1193 compact |= (eisaid[2] - 'A' + 1) << 16;
1194 compact |= hex2bin(eisaid[3]) << 12;
1195 compact |= hex2bin(eisaid[4]) << 8;
1196 compact |= hex2bin(eisaid[5]) << 4;
1197 compact |= hex2bin(eisaid[6]);
1198
1199 acpigen_emit_byte(0xc);
1200 acpigen_emit_byte((compact >> 24) & 0xff);
1201 acpigen_emit_byte((compact >> 16) & 0xff);
1202 acpigen_emit_byte((compact >> 8) & 0xff);
1203 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001204}
Duncan Laurie3829f232016-05-09 11:08:46 -07001205
1206/*
1207 * ToUUID(uuid)
1208 *
1209 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1210 * order for the bytes that make up a UUID Buffer object.
1211 * UUID byte order for input:
1212 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1213 * UUID byte order for output:
1214 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1215 */
1216#define UUID_LEN 16
1217void acpigen_write_uuid(const char *uuid)
1218{
1219 uint8_t buf[UUID_LEN];
1220 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1221 8, 9, 10, 11, 12, 13, 14, 15 };
1222
1223 /* Parse UUID string into bytes */
1224 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1225 return;
1226
1227 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001228 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001229 acpigen_write_len_f();
1230
1231 /* Buffer length in bytes */
1232 acpigen_write_word(UUID_LEN);
1233
1234 /* Output UUID in expected order */
1235 for (i = 0; i < UUID_LEN; i++)
1236 acpigen_emit_byte(buf[order[i]]);
1237
1238 acpigen_pop_len();
1239}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001240
1241/*
1242 * Name (_PRx, Package(One) { name })
1243 * ...
1244 * PowerResource (name, level, order)
1245 */
1246void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001247 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001248{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001249 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001250 for (i = 0; i < dev_states_count; i++) {
1251 acpigen_write_name(dev_states[i]);
1252 acpigen_write_package(1);
1253 acpigen_emit_simple_namestring(name);
1254 acpigen_pop_len(); /* Package */
1255 }
1256
1257 acpigen_emit_ext_op(POWER_RES_OP);
1258
1259 acpigen_write_len_f();
1260
1261 acpigen_emit_simple_namestring(name);
1262 acpigen_emit_byte(level);
1263 acpigen_emit_word(order);
1264}
1265
1266/* Sleep (ms) */
1267void acpigen_write_sleep(uint64_t sleep_ms)
1268{
1269 acpigen_emit_ext_op(SLEEP_OP);
1270 acpigen_write_integer(sleep_ms);
1271}
1272
1273void acpigen_write_store(void)
1274{
1275 acpigen_emit_byte(STORE_OP);
1276}
1277
1278/* Store (src, dst) */
1279void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1280{
1281 acpigen_write_store();
1282 acpigen_emit_byte(src);
1283 acpigen_emit_byte(dst);
1284}
1285
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001286/* Store (src, "namestr") */
1287void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1288{
1289 acpigen_write_store();
1290 acpigen_emit_byte(src);
1291 acpigen_emit_namestring(dst);
1292}
1293
Duncan Laurie8e391d32020-09-30 23:17:41 +00001294/* Store (src, "namestr") */
1295void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst)
1296{
1297 acpigen_write_store();
1298 acpigen_write_integer(src);
1299 acpigen_emit_namestring(dst);
1300}
1301
1302/* Store (src, dst) */
1303void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst)
1304{
1305 acpigen_write_store();
1306 acpigen_write_integer(src);
1307 acpigen_emit_byte(dst);
1308}
1309
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001310/* Or (arg1, arg2, res) */
1311void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1312{
1313 acpigen_emit_byte(OR_OP);
1314 acpigen_emit_byte(arg1);
1315 acpigen_emit_byte(arg2);
1316 acpigen_emit_byte(res);
1317}
1318
Rajat Jain310623b2020-02-26 11:02:53 -08001319/* Xor (arg1, arg2, res) */
1320void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1321{
1322 acpigen_emit_byte(XOR_OP);
1323 acpigen_emit_byte(arg1);
1324 acpigen_emit_byte(arg2);
1325 acpigen_emit_byte(res);
1326}
1327
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001328/* And (arg1, arg2, res) */
1329void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1330{
1331 acpigen_emit_byte(AND_OP);
1332 acpigen_emit_byte(arg1);
1333 acpigen_emit_byte(arg2);
1334 acpigen_emit_byte(res);
1335}
1336
1337/* Not (arg, res) */
1338void acpigen_write_not(uint8_t arg, uint8_t res)
1339{
1340 acpigen_emit_byte(NOT_OP);
1341 acpigen_emit_byte(arg);
1342 acpigen_emit_byte(res);
1343}
1344
1345/* Store (str, DEBUG) */
1346void acpigen_write_debug_string(const char *str)
1347{
1348 acpigen_write_store();
1349 acpigen_write_string(str);
1350 acpigen_emit_ext_op(DEBUG_OP);
1351}
1352
1353/* Store (val, DEBUG) */
1354void acpigen_write_debug_integer(uint64_t val)
1355{
1356 acpigen_write_store();
1357 acpigen_write_integer(val);
1358 acpigen_emit_ext_op(DEBUG_OP);
1359}
1360
1361/* Store (op, DEBUG) */
1362void acpigen_write_debug_op(uint8_t op)
1363{
1364 acpigen_write_store();
1365 acpigen_emit_byte(op);
1366 acpigen_emit_ext_op(DEBUG_OP);
1367}
1368
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001369/* Store (str, DEBUG) */
1370void acpigen_write_debug_namestr(const char *str)
1371{
1372 acpigen_write_store();
1373 acpigen_emit_namestring(str);
1374 acpigen_emit_ext_op(DEBUG_OP);
1375}
1376
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001377void acpigen_write_if(void)
1378{
1379 acpigen_emit_byte(IF_OP);
1380 acpigen_write_len_f();
1381}
1382
1383/* If (And (arg1, arg2)) */
1384void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1385{
1386 acpigen_write_if();
1387 acpigen_emit_byte(AND_OP);
1388 acpigen_emit_byte(arg1);
1389 acpigen_emit_byte(arg2);
1390}
1391
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001392/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001393 * Generates ACPI code for checking if operand1 and operand2 are equal.
1394 * Both operand1 and operand2 are ACPI ops.
1395 *
1396 * If (Lequal (op,1 op2))
1397 */
1398void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1399{
1400 acpigen_write_if();
1401 acpigen_emit_byte(LEQUAL_OP);
1402 acpigen_emit_byte(op1);
1403 acpigen_emit_byte(op2);
1404}
1405
1406/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001407 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1408 * operand1 is ACPI op and operand2 is an integer.
1409 *
1410 * If (Lequal (op, val))
1411 */
1412void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001413{
1414 acpigen_write_if();
1415 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001416 acpigen_emit_byte(op);
1417 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001418}
1419
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001420/*
1421 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1422 * operand1 is namestring and operand2 is an integer.
1423 *
1424 * If (Lequal ("namestr", val))
1425 */
1426void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1427{
1428 acpigen_write_if();
1429 acpigen_emit_byte(LEQUAL_OP);
1430 acpigen_emit_namestring(namestr);
1431 acpigen_write_integer(val);
1432}
1433
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001434void acpigen_write_else(void)
1435{
1436 acpigen_emit_byte(ELSE_OP);
1437 acpigen_write_len_f();
1438}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001439
Duncan Laurie36858202020-10-06 21:01:51 +00001440void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count)
1441{
1442 acpigen_emit_byte(SHIFT_LEFT_OP);
1443 acpigen_emit_byte(src_result);
1444 acpigen_write_integer(count);
1445 acpigen_emit_byte(ZERO_OP);
1446}
1447
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001448void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1449{
1450 acpigen_emit_byte(TO_BUFFER_OP);
1451 acpigen_emit_byte(src);
1452 acpigen_emit_byte(dst);
1453}
1454
1455void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1456{
1457 acpigen_emit_byte(TO_INTEGER_OP);
1458 acpigen_emit_byte(src);
1459 acpigen_emit_byte(dst);
1460}
1461
Aaron Durbin80bc0912020-08-19 23:17:42 -06001462void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op)
1463{
1464 acpigen_emit_byte(TO_INTEGER_OP);
1465 acpigen_emit_namestring(source);
1466 acpigen_emit_byte(dst_op);
1467}
1468
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301469void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001470{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301471 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001472
1473 acpigen_emit_byte(BUFFER_OP);
1474 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301475 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001476
1477 for (i = 0; i < size; i++)
1478 acpigen_emit_byte(arr[i]);
1479
1480 acpigen_pop_len();
1481}
1482
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301483void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001484{
1485 acpigen_emit_byte(RETURN_OP);
1486 acpigen_write_byte_buffer(arr, size);
1487}
1488
1489void acpigen_write_return_singleton_buffer(uint8_t arg)
1490{
1491 acpigen_write_return_byte_buffer(&arg, 1);
1492}
1493
Duncan Laurie2fe74712020-06-01 17:36:56 -07001494void acpigen_write_return_op(uint8_t arg)
1495{
1496 acpigen_emit_byte(RETURN_OP);
1497 acpigen_emit_byte(arg);
1498}
1499
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001500void acpigen_write_return_byte(uint8_t arg)
1501{
1502 acpigen_emit_byte(RETURN_OP);
1503 acpigen_write_byte(arg);
1504}
1505
Naresh G Solanki05abe432016-11-17 00:16:29 +05301506void acpigen_write_return_integer(uint64_t arg)
1507{
1508 acpigen_emit_byte(RETURN_OP);
1509 acpigen_write_integer(arg);
1510}
1511
Duncan Laurieec2e3e42020-11-03 15:19:08 -08001512void acpigen_write_return_namestr(const char *arg)
1513{
1514 acpigen_emit_byte(RETURN_OP);
1515 acpigen_emit_namestring(arg);
1516}
1517
Naresh G Solanki05abe432016-11-17 00:16:29 +05301518void acpigen_write_return_string(const char *arg)
1519{
1520 acpigen_emit_byte(RETURN_OP);
1521 acpigen_write_string(arg);
1522}
1523
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001524void acpigen_write_upc(enum acpi_upc_type type)
1525{
1526 acpigen_write_name("_UPC");
1527 acpigen_write_package(4);
1528 /* Connectable */
1529 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1530 /* Type */
1531 acpigen_write_byte(type);
1532 /* Reserved0 */
1533 acpigen_write_zero();
1534 /* Reserved1 */
1535 acpigen_write_zero();
1536 acpigen_pop_len();
1537}
1538
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001539void acpigen_write_pld(const struct acpi_pld *pld)
1540{
1541 uint8_t buf[20];
1542
1543 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1544 return;
1545
1546 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001547 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001548 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001549 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001550}
1551
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301552void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1553 size_t count, void *arg)
1554{
1555 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1556 acpigen_write_dsm_uuid_arr(&id, 1);
1557}
1558
1559static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1560{
1561 size_t i;
1562
1563 /* If (LEqual (Local0, ToUUID(uuid))) */
1564 acpigen_write_if();
1565 acpigen_emit_byte(LEQUAL_OP);
1566 acpigen_emit_byte(LOCAL0_OP);
1567 acpigen_write_uuid(id->uuid);
1568
1569 /* ToInteger (Arg2, Local1) */
1570 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1571
1572 for (i = 0; i < id->count; i++) {
1573 /* If (LEqual (Local1, i)) */
1574 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1575
1576 /* Callback to write if handler. */
1577 if (id->callbacks[i])
1578 id->callbacks[i](id->arg);
1579
1580 acpigen_pop_len(); /* If */
1581 }
1582
1583 /* Default case: Return (Buffer (One) { 0x0 }) */
1584 acpigen_write_return_singleton_buffer(0x0);
1585
1586 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1587
1588}
1589
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001590/*
1591 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301592 * This function takes as input array of uuid for the device, set of callbacks
1593 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1594 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1595 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001596 *
1597 * Arguments passed into _DSM method:
1598 * Arg0 = UUID
1599 * Arg1 = Revision
1600 * Arg2 = Function index
1601 * Arg3 = Function specific arguments
1602 *
1603 * AML code generated would look like:
1604 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301605 * ToBuffer (Arg0, Local0)
1606 * If (LEqual (Local0, ToUUID(uuid))) {
1607 * ToInteger (Arg2, Local1)
1608 * If (LEqual (Local1, 0)) {
1609 * <acpigen by callback[0]>
1610 * }
1611 * ...
1612 * If (LEqual (Local1, n)) {
1613 * <acpigen by callback[n]>
1614 * }
1615 * Return (Buffer (One) { 0x0 })
1616 * }
1617 * ...
1618 * If (LEqual (Local0, ToUUID(uuidn))) {
1619 * ...
1620 * }
1621 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001622 * }
1623 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301624void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001625{
1626 size_t i;
1627
1628 /* Method (_DSM, 4, Serialized) */
1629 acpigen_write_method_serialized("_DSM", 0x4);
1630
1631 /* ToBuffer (Arg0, Local0) */
1632 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1633
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001634 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301635 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001636
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301637 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001638 acpigen_write_return_singleton_buffer(0x0);
1639
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001640 acpigen_pop_len(); /* Method _DSM */
1641}
1642
Matt Delcob425bc82018-08-13 13:36:27 -07001643void acpigen_write_CPPC_package(const struct cppc_config *config)
1644{
1645 u32 i;
1646 u32 max;
1647 switch (config->version) {
1648 case 1:
1649 max = CPPC_MAX_FIELDS_VER_1;
1650 break;
1651 case 2:
1652 max = CPPC_MAX_FIELDS_VER_2;
1653 break;
1654 case 3:
1655 max = CPPC_MAX_FIELDS_VER_3;
1656 break;
1657 default:
1658 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1659 config->version);
1660 return;
1661 }
1662 acpigen_write_name(CPPC_PACKAGE_NAME);
1663
1664 /* Adding 2 to account for length and version fields */
1665 acpigen_write_package(max + 2);
1666 acpigen_write_dword(max + 2);
1667
1668 acpigen_write_byte(config->version);
1669
1670 for (i = 0; i < max; ++i) {
1671 const acpi_addr_t *reg = &(config->regs[i]);
1672 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +02001673 reg->bit_width == 32 && reg->access_size == ACPI_ACCESS_SIZE_UNDEFINED) {
Matt Delcob425bc82018-08-13 13:36:27 -07001674 acpigen_write_dword(reg->addrl);
1675 } else {
1676 acpigen_write_register_resource(reg);
1677 }
1678 }
1679 acpigen_pop_len();
1680}
1681
1682void acpigen_write_CPPC_method(void)
1683{
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001684 char pscope[16];
1685 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
1686
Matt Delcob425bc82018-08-13 13:36:27 -07001687 acpigen_write_method("_CPC", 0);
1688 acpigen_emit_byte(RETURN_OP);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001689 acpigen_emit_namestring(pscope);
Matt Delcob425bc82018-08-13 13:36:27 -07001690 acpigen_pop_len();
1691}
1692
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001693/*
1694 * Generate ACPI AML code for _ROM method.
1695 * This function takes as input ROM data and ROM length.
1696 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001697 * The ACPI spec isn't clear about what should happen at the end of the
1698 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1699 * bytes in the returned buffer with zeros.
1700 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001701 * Arguments passed into _DSM method:
1702 * Arg0 = Offset in Bytes
1703 * Arg1 = Bytes to return
1704 *
1705 * Example:
1706 * acpigen_write_rom(0xdeadbeef, 0x10000)
1707 *
1708 * AML code generated would look like:
1709 * Method (_ROM, 2, NotSerialized) {
1710 *
1711 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1712 * Field (ROMS, AnyAcc, NoLock, Preserve)
1713 * {
1714 * Offset (0),
1715 * RBF0, 0x80000
1716 * }
1717 *
1718 * Store (Arg0, Local0)
1719 * Store (Arg1, Local1)
1720 *
1721 * If (LGreater (Local1, 0x1000))
1722 * {
1723 * Store (0x1000, Local1)
1724 * }
1725 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001726 * Store (Local1, Local3)
1727 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001728 * If (LGreater (Local0, 0x10000))
1729 * {
1730 * Return(Buffer(Local1){0})
1731 * }
1732 *
1733 * If (LGreater (Local0, 0x0f000))
1734 * {
1735 * Subtract (0x10000, Local0, Local2)
1736 * If (LGreater (Local1, Local2))
1737 * {
1738 * Store (Local2, Local1)
1739 * }
1740 * }
1741 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001742 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001743 *
1744 * Multiply (Local0, 0x08, Local0)
1745 * Multiply (Local1, 0x08, Local1)
1746 *
1747 * CreateField (RBF0, Local0, Local1, TMPB)
1748 * Store (TMPB, ROM1)
1749 * Return (ROM1)
1750 * }
1751 */
1752
1753void acpigen_write_rom(void *bios, const size_t length)
1754{
1755 ASSERT(bios)
1756 ASSERT(length)
1757
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001758 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001759 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001760
1761 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1762 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1763 (uintptr_t)bios, length);
1764 acpigen_write_opregion(&opreg);
1765
1766 struct fieldlist l[] = {
1767 FIELDLIST_OFFSET(0),
1768 FIELDLIST_NAMESTR("RBF0", 8 * length),
1769 };
1770
1771 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1772 * {
1773 * Offset (0),
1774 * RBF0, 0x80000
1775 * } */
1776 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1777 FIELD_NOLOCK | FIELD_PRESERVE);
1778
1779 /* Store (Arg0, Local0) */
1780 acpigen_write_store();
1781 acpigen_emit_byte(ARG0_OP);
1782 acpigen_emit_byte(LOCAL0_OP);
1783
1784 /* Store (Arg1, Local1) */
1785 acpigen_write_store();
1786 acpigen_emit_byte(ARG1_OP);
1787 acpigen_emit_byte(LOCAL1_OP);
1788
1789 /* ACPI SPEC requires to return at maximum 4KiB */
1790 /* If (LGreater (Local1, 0x1000)) */
1791 acpigen_write_if();
1792 acpigen_emit_byte(LGREATER_OP);
1793 acpigen_emit_byte(LOCAL1_OP);
1794 acpigen_write_integer(0x1000);
1795
1796 /* Store (0x1000, Local1) */
1797 acpigen_write_store();
1798 acpigen_write_integer(0x1000);
1799 acpigen_emit_byte(LOCAL1_OP);
1800
1801 /* Pop if */
1802 acpigen_pop_len();
1803
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001804 /* Store (Local1, Local3) */
1805 acpigen_write_store();
1806 acpigen_emit_byte(LOCAL1_OP);
1807 acpigen_emit_byte(LOCAL3_OP);
1808
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001809 /* If (LGreater (Local0, length)) */
1810 acpigen_write_if();
1811 acpigen_emit_byte(LGREATER_OP);
1812 acpigen_emit_byte(LOCAL0_OP);
1813 acpigen_write_integer(length);
1814
1815 /* Return(Buffer(Local1){0}) */
1816 acpigen_emit_byte(RETURN_OP);
1817 acpigen_emit_byte(BUFFER_OP);
1818 acpigen_write_len_f();
1819 acpigen_emit_byte(LOCAL1_OP);
1820 acpigen_emit_byte(0);
1821 acpigen_pop_len();
1822
1823 /* Pop if */
1824 acpigen_pop_len();
1825
1826 /* If (LGreater (Local0, length - 4096)) */
1827 acpigen_write_if();
1828 acpigen_emit_byte(LGREATER_OP);
1829 acpigen_emit_byte(LOCAL0_OP);
1830 acpigen_write_integer(length - 4096);
1831
1832 /* Subtract (length, Local0, Local2) */
1833 acpigen_emit_byte(SUBTRACT_OP);
1834 acpigen_write_integer(length);
1835 acpigen_emit_byte(LOCAL0_OP);
1836 acpigen_emit_byte(LOCAL2_OP);
1837
1838 /* If (LGreater (Local1, Local2)) */
1839 acpigen_write_if();
1840 acpigen_emit_byte(LGREATER_OP);
1841 acpigen_emit_byte(LOCAL1_OP);
1842 acpigen_emit_byte(LOCAL2_OP);
1843
1844 /* Store (Local2, Local1) */
1845 acpigen_write_store();
1846 acpigen_emit_byte(LOCAL2_OP);
1847 acpigen_emit_byte(LOCAL1_OP);
1848
1849 /* Pop if */
1850 acpigen_pop_len();
1851
1852 /* Pop if */
1853 acpigen_pop_len();
1854
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001855 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001856 acpigen_write_name("ROM1");
1857 acpigen_emit_byte(BUFFER_OP);
1858 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001859 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001860 acpigen_emit_byte(0);
1861 acpigen_pop_len();
1862
1863 /* Multiply (Local1, 0x08, Local1) */
1864 acpigen_emit_byte(MULTIPLY_OP);
1865 acpigen_emit_byte(LOCAL1_OP);
1866 acpigen_write_integer(0x08);
1867 acpigen_emit_byte(LOCAL1_OP);
1868
1869 /* Multiply (Local0, 0x08, Local0) */
1870 acpigen_emit_byte(MULTIPLY_OP);
1871 acpigen_emit_byte(LOCAL0_OP);
1872 acpigen_write_integer(0x08);
1873 acpigen_emit_byte(LOCAL0_OP);
1874
1875 /* CreateField (RBF0, Local0, Local1, TMPB) */
1876 acpigen_emit_ext_op(CREATEFIELD_OP);
1877 acpigen_emit_namestring("RBF0");
1878 acpigen_emit_byte(LOCAL0_OP);
1879 acpigen_emit_byte(LOCAL1_OP);
1880 acpigen_emit_namestring("TMPB");
1881
1882 /* Store (TMPB, ROM1) */
1883 acpigen_write_store();
1884 acpigen_emit_namestring("TMPB");
1885 acpigen_emit_namestring("ROM1");
1886
1887 /* Return (ROM1) */
1888 acpigen_emit_byte(RETURN_OP);
1889 acpigen_emit_namestring("ROM1");
1890
1891 /* Pop method */
1892 acpigen_pop_len();
1893}
1894
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001895/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001896int __weak acpigen_soc_read_rx_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("read_rx_gpio not available");
1900 return -1;
1901}
1902
Aaron Durbin64031672018-04-21 14:45:32 -06001903int __weak acpigen_soc_get_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("get_tx_gpio not available");
1907 return -1;
1908}
1909
Aaron Durbin64031672018-04-21 14:45:32 -06001910int __weak acpigen_soc_set_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("set_tx_gpio not available");
1914 return -1;
1915}
1916
Aaron Durbin64031672018-04-21 14:45:32 -06001917int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001918{
1919 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1920 acpigen_write_debug_string("clear_tx_gpio not available");
1921 return -1;
1922}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001923
1924/*
1925 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1926 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1927 * make callbacks into SoC acpigen code.
1928 *
1929 * Returns 0 on success and -1 on error.
1930 */
Duncan Laurie30c3f912020-10-09 04:48:53 +00001931int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001932{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001933 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001934 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001935 else
1936 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001937}
1938
Duncan Laurie30c3f912020-10-09 04:48:53 +00001939int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001940{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001941 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001942 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1943 else
1944 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1945}
Jonathan Zhang71299c22020-01-27 11:38:57 -08001946
Duncan Laurie30c3f912020-10-09 04:48:53 +00001947void acpigen_get_rx_gpio(const struct acpi_gpio *gpio)
Rajat Jain310623b2020-02-26 11:02:53 -08001948{
1949 acpigen_soc_read_rx_gpio(gpio->pins[0]);
1950
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001951 if (gpio->active_low)
Rajat Jain310623b2020-02-26 11:02:53 -08001952 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1953}
1954
Duncan Laurie30c3f912020-10-09 04:48:53 +00001955void acpigen_get_tx_gpio(const struct acpi_gpio *gpio)
Duncan Laurie2fe74712020-06-01 17:36:56 -07001956{
1957 acpigen_soc_get_tx_gpio(gpio->pins[0]);
1958
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001959 if (gpio->active_low)
Duncan Laurie2fe74712020-06-01 17:36:56 -07001960 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1961}
1962
Jonathan Zhang71299c22020-01-27 11:38:57 -08001963/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
1964void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran,
1965 u16 range_min, u16 range_max, u16 translation, u16 length)
1966{
1967 acpigen_emit_byte(0x88);
1968 /* Byte 1+2: length (0x000d) */
1969 acpigen_emit_byte(0x0d);
1970 acpigen_emit_byte(0x00);
1971 /* resource type */
1972 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1973 /* general flags */
1974 acpigen_emit_byte(gen_flags);
1975 /* type flags */
1976 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1977 acpigen_emit_byte(type_flags);
1978 /* granularity, min, max, translation, length */
1979 acpigen_emit_word(gran);
1980 acpigen_emit_word(range_min);
1981 acpigen_emit_word(range_max);
1982 acpigen_emit_word(translation);
1983 acpigen_emit_word(length);
1984}
1985
1986/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
1987void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags,
1988 u32 gran, u32 range_min, u32 range_max, u32 translation, u32 length)
1989{
1990 acpigen_emit_byte(0x87);
1991 /* Byte 1+2: length (0023) */
1992 acpigen_emit_byte(23);
1993 acpigen_emit_byte(0x00);
1994 /* resource type */
1995 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1996 /* general flags */
1997 acpigen_emit_byte(gen_flags);
1998 /* type flags */
1999 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2000 acpigen_emit_byte(type_flags);
2001 /* granularity, min, max, translation, length */
2002 acpigen_emit_dword(gran);
2003 acpigen_emit_dword(range_min);
2004 acpigen_emit_dword(range_max);
2005 acpigen_emit_dword(translation);
2006 acpigen_emit_dword(length);
2007}
2008
2009static void acpigen_emit_qword(u64 data)
2010{
2011 acpigen_emit_dword(data & 0xffffffff);
2012 acpigen_emit_dword((data >> 32) & 0xffffffff);
2013}
2014
2015/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
2016void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags,
2017 u64 gran, u64 range_min, u64 range_max, u64 translation, u64 length)
2018{
2019 acpigen_emit_byte(0x8a);
2020 /* Byte 1+2: length (0x002b) */
2021 acpigen_emit_byte(0x2b);
2022 acpigen_emit_byte(0x00);
2023 /* resource type */
2024 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
2025 /* general flags */
2026 acpigen_emit_byte(gen_flags);
2027 /* type flags */
2028 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
2029 acpigen_emit_byte(type_flags);
2030 /* granularity, min, max, translation, length */
2031 acpigen_emit_qword(gran);
2032 acpigen_emit_qword(range_min);
2033 acpigen_emit_qword(range_max);
2034 acpigen_emit_qword(translation);
2035 acpigen_emit_qword(length);
2036}
Furquan Shaikh1a829232020-04-23 11:11:05 -07002037
2038void acpigen_write_ADR(uint64_t adr)
2039{
2040 acpigen_write_name_qword("_ADR", adr);
2041}
2042
2043void acpigen_write_ADR_pci_devfn(pci_devfn_t devfn)
2044{
2045 /*
2046 * _ADR for PCI Bus is encoded as follows:
2047 * [63:32] - unused
2048 * [31:16] - device #
2049 * [15:0] - function #
2050 */
2051 acpigen_write_ADR(PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn));
2052}
2053
2054void acpigen_write_ADR_pci_device(const struct device *dev)
2055{
2056 assert(dev->path.type == DEVICE_PATH_PCI);
2057 acpigen_write_ADR_pci_devfn(dev->path.pci.devfn);
2058}
Duncan Laurie08a942f2020-04-29 12:13:14 -07002059
2060/**
2061 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2062 * @address: SoundWire device address properties.
2063 *
2064 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2065 *
2066 * 63..52 - Reserved (0)
2067 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2068 * Used when a Controller has multiple master devices, each producing a
2069 * separate SoundWire Link. Set to 0 for single-link controllers.
2070 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2071 * 47..44 - SoundWire specification version that this device supports
2072 * 43..40 - Unique ID for multiple devices
2073 * 39..24 - MIPI standard manufacturer code
2074 * 23..08 - Vendor defined part ID
2075 * 07..00 - MIPI class encoding
2076 */
2077void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
2078{
2079 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
2080 (((uint64_t)address->version & 0xf) << 44) |
2081 (((uint64_t)address->unique_id & 0xf) << 40) |
2082 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
2083 (((uint64_t)address->part_id & 0xffff) << 8) |
2084 (((uint64_t)address->class & 0xff)));
2085}
Tim Wawrzynczakf6945022020-06-04 15:18:47 -06002086
2087void acpigen_notify(const char *namestr, int value)
2088{
2089 acpigen_emit_byte(NOTIFY_OP);
2090 acpigen_emit_namestring(namestr);
2091 acpigen_write_integer(value);
2092}
Aaron Durbin80bc0912020-08-19 23:17:42 -06002093
2094static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name)
2095{
2096 acpigen_emit_byte(aml_op);
2097 acpigen_emit_byte(srcop);
2098 acpigen_write_integer(byte_offset);
2099 acpigen_emit_namestring(name);
2100}
2101
2102void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name)
2103{
2104 _create_field(CREATE_BYTE_OP, op, byte_offset, name);
2105}
2106
2107void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name)
2108{
2109 _create_field(CREATE_WORD_OP, op, byte_offset, name);
2110}
2111
2112void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name)
2113{
2114 _create_field(CREATE_DWORD_OP, op, byte_offset, name);
2115}
2116
2117void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name)
2118{
2119 _create_field(CREATE_QWORD_OP, op, byte_offset, name);
2120}
Jason Gleneskca36aed2020-09-15 21:01:57 -07002121
2122void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
2123{
2124 acpigen_write_name("_PCT");
2125 acpigen_write_package(0x02);
2126 acpigen_write_register_resource(perf_ctrl);
2127 acpigen_write_register_resource(perf_sts);
2128
2129 acpigen_pop_len();
2130}
2131
2132void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value)
2133{
2134 acpigen_write_package(0x08);
2135 acpigen_write_dword(pstate_value->core_freq);
2136 acpigen_write_dword(pstate_value->power);
2137 acpigen_write_dword(pstate_value->transition_latency);
2138 acpigen_write_dword(pstate_value->bus_master_latency);
2139
2140 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t));
2141 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t));
2142 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t));
2143 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t));
2144
2145 acpigen_pop_len();
2146}
2147
2148void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
2149{
2150 size_t pstate;
2151
2152 acpigen_write_name("XPSS");
2153 acpigen_write_package(nentries);
2154 for (pstate = 0; pstate < nentries; pstate++) {
2155 acpigen_write_xpss_package(pstate_values);
2156 pstate_values++;
2157 }
2158
2159 acpigen_pop_len();
2160}
Duncan Laurieec2e3e42020-11-03 15:19:08 -08002161
2162/* Delay up to wait_ms until provided namestr matches expected value. */
2163void acpigen_write_delay_until_namestr_int(uint32_t wait_ms, const char *name, uint64_t value)
2164{
2165 uint32_t wait_ms_segment = 1;
2166 uint32_t segments = wait_ms;
2167
2168 /* Sleep in 16ms segments if delay is more than 32ms. */
2169 if (wait_ms > 32) {
2170 wait_ms_segment = 16;
2171 segments = wait_ms / 16;
2172 }
2173
2174 acpigen_write_store_int_to_op(segments, LOCAL7_OP);
2175 acpigen_emit_byte(WHILE_OP);
2176 acpigen_write_len_f();
2177 acpigen_emit_byte(LGREATER_OP);
2178 acpigen_emit_byte(LOCAL7_OP);
2179 acpigen_emit_byte(ZERO_OP);
2180
2181 /* If name is not provided then just delay in a loop. */
2182 if (name) {
2183 acpigen_write_if_lequal_namestr_int(name, value);
2184 acpigen_emit_byte(BREAK_OP);
2185 acpigen_pop_len(); /* If */
2186 }
2187
2188 acpigen_write_sleep(wait_ms_segment);
2189 acpigen_emit_byte(DECREMENT_OP);
2190 acpigen_emit_byte(LOCAL7_OP);
2191 acpigen_pop_len(); /* While */
2192}