blob: 4a7dfc9325b1788b01a17c067736f619782c36e1 [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{
345 /* <dest_op> = DeRefOf (<package_op>[<element]) */
346 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
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100355void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000356{
357/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100358 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200359 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000360*/
361 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700362 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100363 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000364
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200365 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600366 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100367 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000368 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700369 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000370 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000371}
372
Nico Huber4d211ac2017-09-01 21:14:36 +0200373void acpigen_write_processor_package(const char *const name,
374 const unsigned int first_core,
375 const unsigned int core_count)
376{
377 unsigned int i;
378 char pscope[16];
379
380 acpigen_write_name(name);
381 acpigen_write_package(core_count);
382 for (i = first_core; i < first_core + core_count; ++i) {
383 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
384 acpigen_emit_namestring(pscope);
385 }
386 acpigen_pop_len();
387}
388
Arthur Heymans8f055272018-11-28 13:18:57 +0100389/* Method to notify all CPU cores */
390void acpigen_write_processor_cnot(const unsigned int number_of_cores)
391{
392 int core_id;
393
Christian Walterbe3979c2019-12-18 15:07:59 +0100394 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100395 for (core_id = 0; core_id < number_of_cores; core_id++) {
396 char buffer[DEVICE_PATH_MAX];
397 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
398 core_id);
399 acpigen_emit_byte(NOTIFY_OP);
400 acpigen_emit_namestring(buffer);
401 acpigen_emit_byte(ARG0_OP);
402 }
403 acpigen_pop_len();
404}
405
Naresh G Solanki8535c662016-10-25 00:51:24 +0530406/*
407 * Generate ACPI AML code for OperationRegion
408 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
409 * where rname is region name, space is region space, offset is region offset &
410 * len is region length.
411 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
412 */
Duncan Laurie35029602020-10-09 17:50:25 +0000413void acpigen_write_opregion(const struct opregion *opreg)
Naresh G Solanki8535c662016-10-25 00:51:24 +0530414{
415 /* OpregionOp */
416 acpigen_emit_ext_op(OPREGION_OP);
417 /* NameString 4 chars only */
418 acpigen_emit_simple_namestring(opreg->name);
419 /* RegionSpace */
420 acpigen_emit_byte(opreg->regionspace);
421 /* RegionOffset & RegionLen, it can be byte word or double word */
422 acpigen_write_integer(opreg->regionoffset);
423 acpigen_write_integer(opreg->regionlen);
424}
425
Patrick Rudolph32bae492019-08-08 12:47:30 +0200426/*
427 * Generate ACPI AML code for Mutex
428 * Arg0: Pointer to name of mutex
429 * Arg1: Initial value of mutex
430 */
431void acpigen_write_mutex(const char *name, const uint8_t flags)
432{
433 /* MutexOp */
434 acpigen_emit_ext_op(MUTEX_OP);
435 /* NameString 4 chars only */
436 acpigen_emit_simple_namestring(name);
437 acpigen_emit_byte(flags);
438}
439
440void acpigen_write_acquire(const char *name, const uint16_t val)
441{
442 /* AcquireOp */
443 acpigen_emit_ext_op(ACQUIRE_OP);
444 /* NameString 4 chars only */
445 acpigen_emit_simple_namestring(name);
446 acpigen_emit_word(val);
447}
448
449void acpigen_write_release(const char *name)
450{
451 /* ReleaseOp */
452 acpigen_emit_ext_op(RELEASE_OP);
453 /* NameString 4 chars only */
454 acpigen_emit_simple_namestring(name);
455}
456
Patrick Rudolph894977b2017-07-01 16:15:05 +0200457static void acpigen_write_field_length(uint32_t len)
458{
459 uint8_t i, j;
460 uint8_t emit[4];
461
462 i = 1;
463 if (len < 0x40) {
464 emit[0] = len & 0x3F;
465 } else {
466 emit[0] = len & 0xF;
467 len >>= 4;
468 while (len) {
469 emit[i] = len & 0xFF;
470 i++;
471 len >>= 8;
472 }
473 }
474 /* Update bit 7:6 : Number of bytes followed by emit[0] */
475 emit[0] |= (i - 1) << 6;
476
477 for (j = 0; j < i; j++)
478 acpigen_emit_byte(emit[j]);
479}
480
Naresh G Solanki8535c662016-10-25 00:51:24 +0530481static void acpigen_write_field_offset(uint32_t offset,
482 uint32_t current_bit_pos)
483{
484 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530485
486 if (offset < current_bit_pos) {
487 printk(BIOS_WARNING, "%s: Cannot move offset backward",
488 __func__);
489 return;
490 }
491
492 diff_bits = offset - current_bit_pos;
493 /* Upper limit */
494 if (diff_bits > 0xFFFFFFF) {
495 printk(BIOS_WARNING, "%s: Offset very large to encode",
496 __func__);
497 return;
498 }
499
Naresh G Solanki8535c662016-10-25 00:51:24 +0530500 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200501 acpigen_write_field_length(diff_bits);
502}
503
504static void acpigen_write_field_name(const char *name, uint32_t size)
505{
506 acpigen_emit_simple_namestring(name);
507 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530508}
509
Duncan Laurie095bbf92020-09-30 23:09:29 +0000510static void acpigen_write_field_reserved(uint32_t size)
511{
512 acpigen_emit_byte(0);
513 acpigen_write_field_length(size);
514}
515
Naresh G Solanki8535c662016-10-25 00:51:24 +0530516/*
517 * Generate ACPI AML code for Field
518 * Arg0: region name
519 * Arg1: Pointer to struct fieldlist.
520 * Arg2: no. of entries in Arg1
521 * Arg3: flags which indicate filed access type, lock rule & update rule.
522 * Example with fieldlist
523 * struct fieldlist l[] = {
524 * FIELDLIST_OFFSET(0x84),
525 * FIELDLIST_NAMESTR("PMCS", 2),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000526 * FIELDLIST_RESERVED(6),
Naresh G Solanki8535c662016-10-25 00:51:24 +0530527 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200528 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530529 * FIELD_PRESERVE);
530 * Output:
531 * Field (UART, AnyAcc, NoLock, Preserve)
532 * {
533 * Offset (0x84),
Duncan Laurie095bbf92020-09-30 23:09:29 +0000534 * PMCS, 2,
535 * , 6,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530536 * }
537 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700538void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530539 uint8_t flags)
540{
541 uint16_t i;
542 uint32_t current_bit_pos = 0;
543
544 /* FieldOp */
545 acpigen_emit_ext_op(FIELD_OP);
546 /* Package Length */
547 acpigen_write_len_f();
548 /* NameString 4 chars only */
549 acpigen_emit_simple_namestring(name);
550 /* Field Flag */
551 acpigen_emit_byte(flags);
552
553 for (i = 0; i < count; i++) {
554 switch (l[i].type) {
555 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200556 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530557 current_bit_pos += l[i].bits;
558 break;
Duncan Laurie095bbf92020-09-30 23:09:29 +0000559 case RESERVED:
560 acpigen_write_field_reserved(l[i].bits);
561 current_bit_pos += l[i].bits;
562 break;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530563 case OFFSET:
564 acpigen_write_field_offset(l[i].bits, current_bit_pos);
565 current_bit_pos = l[i].bits;
566 break;
567 default:
568 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
569 , __func__, l[i].type);
570 break;
571 }
572 }
573 acpigen_pop_len();
574}
575
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200576/*
577 * Generate ACPI AML code for IndexField
578 * Arg0: region name
579 * Arg1: Pointer to struct fieldlist.
580 * Arg2: no. of entries in Arg1
581 * Arg3: flags which indicate filed access type, lock rule & update rule.
582 * Example with fieldlist
583 * struct fieldlist l[] = {
584 * FIELDLIST_OFFSET(0x84),
585 * FIELDLIST_NAMESTR("PMCS", 2),
586 * };
587 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
588 * FIELD_NOLOCK |
589 * FIELD_PRESERVE);
590 * Output:
591 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
592 * {
593 * Offset (0x84),
594 * PMCS, 2
595 * }
596 */
597void acpigen_write_indexfield(const char *idx, const char *data,
598 struct fieldlist *l, size_t count, uint8_t flags)
599{
600 uint16_t i;
601 uint32_t current_bit_pos = 0;
602
603 /* FieldOp */
604 acpigen_emit_ext_op(INDEX_FIELD_OP);
605 /* Package Length */
606 acpigen_write_len_f();
607 /* NameString 4 chars only */
608 acpigen_emit_simple_namestring(idx);
609 /* NameString 4 chars only */
610 acpigen_emit_simple_namestring(data);
611 /* Field Flag */
612 acpigen_emit_byte(flags);
613
614 for (i = 0; i < count; i++) {
615 switch (l[i].type) {
616 case NAME_STRING:
617 acpigen_write_field_name(l[i].name, l[i].bits);
618 current_bit_pos += l[i].bits;
619 break;
620 case OFFSET:
621 acpigen_write_field_offset(l[i].bits, current_bit_pos);
622 current_bit_pos = l[i].bits;
623 break;
624 default:
625 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
626 , __func__, l[i].type);
627 break;
628 }
629 }
630 acpigen_pop_len();
631}
632
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100633void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000634{
635/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200636 Name (_PCT, Package (0x02)
637 {
638 ResourceTemplate ()
639 {
640 Register (FFixedHW,
641 0x00, // Bit Width
642 0x00, // Bit Offset
643 0x0000000000000000, // Address
644 ,)
645 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000646
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200647 ResourceTemplate ()
648 {
649 Register (FFixedHW,
650 0x00, // Bit Width
651 0x00, // Bit Offset
652 0x0000000000000000, // Address
653 ,)
654 }
655 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000656*/
657 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700658 /* 00000030 "0._PCT.," */
659 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
660 /* 00000038 "........" */
661 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
662 /* 00000040 "........" */
663 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
664 /* 00000048 "....y..." */
665 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
666 /* 00000050 "........" */
667 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
668 /* 00000058 "........" */
669 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000670 0x00, 0x79, 0x00
671 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100672 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000673}
674
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100675void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200676{
677/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200678 Name (_PTC, Package (0x02)
679 {
680 ResourceTemplate ()
681 {
682 Register (FFixedHW,
683 0x00, // Bit Width
684 0x00, // Bit Offset
685 0x0000000000000000, // Address
686 ,)
687 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200688
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200689 ResourceTemplate ()
690 {
691 Register (FFixedHW,
692 0x00, // Bit Width
693 0x00, // Bit Offset
694 0x0000000000000000, // Address
695 ,)
696 }
697 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200698*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200699 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100700 .space_id = ACPI_ADDRESS_SPACE_FIXED,
701 .bit_width = 0,
702 .bit_offset = 0,
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +0200703 .access_size = ACPI_ACCESS_SIZE_UNDEFINED,
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100704 .addrl = 0,
705 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200706 };
707
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100708 acpigen_write_name("_PTC");
709 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200710
711 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700712 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200713
714 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700715 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200716
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100717 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200718}
719
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700720static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100721{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700722 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100723 acpigen_write_len_f();
724 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700725 acpigen_emit_byte(flags);
726}
727
728/* Method (name, nargs, NotSerialized) */
729void acpigen_write_method(const char *name, int nargs)
730{
731 __acpigen_write_method(name, (nargs & 7));
732}
733
734/* Method (name, nargs, Serialized) */
735void acpigen_write_method_serialized(const char *name, int nargs)
736{
737 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100738}
739
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100740void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100741{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700742 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100743 acpigen_write_len_f();
744 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100745}
746
Duncan Laurieabe2de82016-05-09 11:08:46 -0700747void acpigen_write_STA(uint8_t status)
748{
749 /*
750 * Method (_STA, 0, NotSerialized) { Return (status) }
751 */
752 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700753 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700754 acpigen_write_byte(status);
755 acpigen_pop_len();
756}
757
Sugnan Prabhu S4f77f612020-06-10 09:51:02 +0530758void acpigen_write_STA_ext(const char *namestring)
759{
760 /*
761 * Method (_STA, 0, NotSerialized) { Return (ext_val) }
762 */
763 acpigen_write_method("_STA", 0);
764 acpigen_emit_byte(RETURN_OP);
765 acpigen_emit_namestring(namestring);
766 acpigen_pop_len();
767}
768
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100769/*
770 * Generates a func with max supported P-states.
771 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100772void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000773{
774/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200775 Method (_PPC, 0, NotSerialized)
776 {
777 Return (nr)
778 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000779*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100780 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700781 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000782 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100783 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100784 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000785}
786
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100787/*
788 * Generates a func with max supported P-states saved
789 * in the variable PPCM.
790 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100791void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700792{
793/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200794 Method (_PPC, 0, NotSerialized)
795 {
796 Return (PPCM)
797 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700798*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100799 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700800 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700801 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100802 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100803 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700804}
805
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100806void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200807{
808/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200809 // Sample _TPC method
810 Method (_TPC, 0, NotSerialized)
811 {
812 Return (\TLVL)
813 }
814*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100815 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700816 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100817 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100818 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200819}
820
Duncan Laurieabe2de82016-05-09 11:08:46 -0700821void acpigen_write_PRW(u32 wake, u32 level)
822{
823 /*
824 * Name (_PRW, Package () { wake, level }
825 */
826 acpigen_write_name("_PRW");
827 acpigen_write_package(2);
828 acpigen_write_integer(wake);
829 acpigen_write_integer(level);
830 acpigen_pop_len();
831}
832
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100833void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000834 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000835{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100836 acpigen_write_package(6);
837 acpigen_write_dword(coreFreq);
838 acpigen_write_dword(power);
839 acpigen_write_dword(transLat);
840 acpigen_write_dword(busmLat);
841 acpigen_write_dword(control);
842 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100843 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700844
845 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
846 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000847}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000848
Jason Gleneskca36aed2020-09-15 21:01:57 -0700849void acpigen_write_pss_object(const struct acpi_sw_pstate *pstate_values, size_t nentries)
850{
851 size_t pstate;
852
853 acpigen_write_name("_PSS");
854 acpigen_write_package(nentries);
855 for (pstate = 0; pstate < nentries; pstate++) {
856 acpigen_write_PSS_package(
857 pstate_values->core_freq, pstate_values->power,
858 pstate_values->transition_latency, pstate_values->bus_master_latency,
859 pstate_values->control_value, pstate_values->status_value);
860 pstate_values++;
861 }
862
863 acpigen_pop_len();
864}
865
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100866void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000867{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100868 acpigen_write_name("_PSD");
869 acpigen_write_package(1);
870 acpigen_write_package(5);
871 acpigen_write_byte(5); // 5 values
872 acpigen_write_byte(0); // revision 0
873 acpigen_write_dword(domain);
874 acpigen_write_dword(coordtype);
875 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100876 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100877 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000878}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000879
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100880void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200881{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100882 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700883 acpigen_write_register_resource(&cstate->resource);
Jason Glenesk201acca2020-09-11 12:36:15 -0700884 acpigen_write_byte(cstate->ctype);
885 acpigen_write_word(cstate->latency);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100886 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100887 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200888}
889
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100890void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200891{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100892 int i;
893 acpigen_write_name("_CST");
894 acpigen_write_package(nentries+1);
Jason Glenesk201acca2020-09-11 12:36:15 -0700895 acpigen_write_integer(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200896
897 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100898 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200899
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100900 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200901}
902
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700903void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
904 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500905{
906 acpigen_write_name("_CSD");
907 acpigen_write_package(1);
908 acpigen_write_package(6);
Jason Glenesk201acca2020-09-11 12:36:15 -0700909 acpigen_write_integer(6); // 6 values
Timothy Pearson83abd812015-06-08 19:35:06 -0500910 acpigen_write_byte(0); // revision 0
911 acpigen_write_dword(domain);
912 acpigen_write_dword(coordtype);
913 acpigen_write_dword(numprocs);
914 acpigen_write_dword(index);
915 acpigen_pop_len();
916 acpigen_pop_len();
917}
918
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100919void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200920{
921/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200922 Sample _TSS package with 100% and 50% duty cycles
923 Name (_TSS, Package (0x02)
924 {
925 Package(){100, 1000, 0, 0x00, 0)
926 Package(){50, 520, 0, 0x18, 0)
927 })
928*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100929 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200930 acpi_tstate_t *tstate = tstate_list;
931
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100932 acpigen_write_name("_TSS");
933 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200934
935 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100936 acpigen_write_package(5);
937 acpigen_write_dword(tstate->percent);
938 acpigen_write_dword(tstate->power);
939 acpigen_write_dword(tstate->latency);
940 acpigen_write_dword(tstate->control);
941 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100942 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200943 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200944 }
945
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100946 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200947}
948
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100949void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200950{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100951 acpigen_write_name("_TSD");
952 acpigen_write_package(1);
953 acpigen_write_package(5);
954 acpigen_write_byte(5); // 5 values
955 acpigen_write_byte(0); // revision 0
956 acpigen_write_dword(domain);
957 acpigen_write_dword(coordtype);
958 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100959 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100960 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200961}
962
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100963void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000964{
965 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200966 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000967 * Byte 0:
968 * Bit7 : 1 => big item
969 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
970 */
971 acpigen_emit_byte(0x86);
972 /* Byte 1+2: length (0x0009) */
973 acpigen_emit_byte(0x09);
974 acpigen_emit_byte(0x00);
975 /* bit1-7 are ignored */
976 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700977 acpigen_emit_dword(base);
978 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000979}
980
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700981static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200982{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200983 acpigen_emit_byte(0x82); /* Register Descriptor */
984 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
985 acpigen_emit_byte(0x00); /* Register Length 15:8 */
986 acpigen_emit_byte(addr->space_id); /* Address Space ID */
987 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
988 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100989 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700990 acpigen_emit_dword(addr->addrl); /* Register Address Low */
991 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200992}
993
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700994void acpigen_write_register_resource(const acpi_addr_t *addr)
995{
996 acpigen_write_resourcetemplate_header();
997 acpigen_write_register(addr);
998 acpigen_write_resourcetemplate_footer();
999}
1000
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001001void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001002{
1003 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001004 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001005 * Byte 0:
1006 * Bit7 : 0 => small item
1007 * Bit6-3: 0100 (0x4) => IRQ port descriptor
1008 * Bit2-0: 010 (0x2) => 2 Bytes long
1009 */
1010 acpigen_emit_byte(0x22);
1011 acpigen_emit_byte(mask & 0xff);
1012 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +01001013}
1014
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001015void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001016{
1017 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +02001018 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001019 * Byte 0:
1020 * Bit7 : 0 => small item
1021 * Bit6-3: 1000 (0x8) => I/O port descriptor
1022 * Bit2-0: 111 (0x7) => 7 Bytes long
1023 */
1024 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001025 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001026 /* bit1-7 are ignored */
1027 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
1028 /* minimum base address the device may be configured for */
1029 acpigen_emit_byte(min & 0xff);
1030 acpigen_emit_byte((min >> 8) & 0xff);
1031 /* maximum base address the device may be configured for */
1032 acpigen_emit_byte(max & 0xff);
1033 acpigen_emit_byte((max >> 8) & 0xff);
1034 /* alignment for min base */
1035 acpigen_emit_byte(align & 0xff);
1036 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001037}
1038
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001039void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001040{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001041 /*
1042 * A ResourceTemplate() is a Buffer() with a
1043 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001044 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001045 * (small item 0xf, len 1)
1046 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001047 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001048 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001049 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001050 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001051 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001052 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001053 acpigen_emit_byte(0x00);
1054 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001055}
1056
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001057void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001058{
1059 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001060 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001061 /*
1062 * end tag (acpi 4.0 Section 6.4.2.8)
1063 * 0x79 <checksum>
1064 * 0x00 is treated as a good checksum according to the spec
1065 * and is what iasl generates.
1066 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001067 acpigen_emit_byte(0x79);
1068 acpigen_emit_byte(0x00);
1069
Nico Huber7d89ce32017-04-14 00:08:18 +02001070 /* Start counting past the 2-bytes length added in
1071 acpigen_write_resourcetemplate() above. */
1072 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001073
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001074 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001075 p[0] = len & 0xff;
1076 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001077 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001078 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001079}
1080
1081static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1082 struct resource *res)
1083{
1084 acpigen_write_mem32fixed(0, res->base, res->size);
1085}
1086
1087static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1088 struct resource *res)
1089{
1090 resource_t base = res->base;
1091 resource_t size = res->size;
1092 while (size > 0) {
1093 resource_t sz = size > 255 ? 255 : size;
1094 acpigen_write_io16(base, base, 0, sz, 1);
1095 size -= sz;
1096 base += sz;
1097 }
1098}
1099
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001100void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001101{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001102 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001103
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001104 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001105 search_global_resources(
1106 IORESOURCE_MEM | IORESOURCE_RESERVE,
1107 IORESOURCE_MEM | IORESOURCE_RESERVE,
1108 acpigen_add_mainboard_rsvd_mem32, 0);
1109
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001110 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001111 search_global_resources(
1112 IORESOURCE_IO | IORESOURCE_RESERVE,
1113 IORESOURCE_IO | IORESOURCE_RESERVE,
1114 acpigen_add_mainboard_rsvd_io, 0);
1115
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001116 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001117}
1118
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001119void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001120{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001121 acpigen_write_scope(scope);
1122 acpigen_write_name(name);
1123 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001124 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001125}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001126
1127static int hex2bin(const char c)
1128{
1129 if (c >= 'A' && c <= 'F')
1130 return c - 'A' + 10;
1131 if (c >= 'a' && c <= 'f')
1132 return c - 'a' + 10;
1133 return c - '0';
1134}
1135
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001136void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001137{
1138 u32 compact = 0;
1139
1140 /* Clamping individual values would be better but
1141 there is a disagreement over what is a valid
1142 EISA id, so accept anything and don't clamp,
1143 parent code should create a valid EISAid.
1144 */
1145 compact |= (eisaid[0] - 'A' + 1) << 26;
1146 compact |= (eisaid[1] - 'A' + 1) << 21;
1147 compact |= (eisaid[2] - 'A' + 1) << 16;
1148 compact |= hex2bin(eisaid[3]) << 12;
1149 compact |= hex2bin(eisaid[4]) << 8;
1150 compact |= hex2bin(eisaid[5]) << 4;
1151 compact |= hex2bin(eisaid[6]);
1152
1153 acpigen_emit_byte(0xc);
1154 acpigen_emit_byte((compact >> 24) & 0xff);
1155 acpigen_emit_byte((compact >> 16) & 0xff);
1156 acpigen_emit_byte((compact >> 8) & 0xff);
1157 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001158}
Duncan Laurie3829f232016-05-09 11:08:46 -07001159
1160/*
1161 * ToUUID(uuid)
1162 *
1163 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1164 * order for the bytes that make up a UUID Buffer object.
1165 * UUID byte order for input:
1166 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1167 * UUID byte order for output:
1168 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1169 */
1170#define UUID_LEN 16
1171void acpigen_write_uuid(const char *uuid)
1172{
1173 uint8_t buf[UUID_LEN];
1174 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1175 8, 9, 10, 11, 12, 13, 14, 15 };
1176
1177 /* Parse UUID string into bytes */
1178 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1179 return;
1180
1181 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001182 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001183 acpigen_write_len_f();
1184
1185 /* Buffer length in bytes */
1186 acpigen_write_word(UUID_LEN);
1187
1188 /* Output UUID in expected order */
1189 for (i = 0; i < UUID_LEN; i++)
1190 acpigen_emit_byte(buf[order[i]]);
1191
1192 acpigen_pop_len();
1193}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001194
1195/*
1196 * Name (_PRx, Package(One) { name })
1197 * ...
1198 * PowerResource (name, level, order)
1199 */
1200void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001201 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001202{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001203 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001204 for (i = 0; i < dev_states_count; i++) {
1205 acpigen_write_name(dev_states[i]);
1206 acpigen_write_package(1);
1207 acpigen_emit_simple_namestring(name);
1208 acpigen_pop_len(); /* Package */
1209 }
1210
1211 acpigen_emit_ext_op(POWER_RES_OP);
1212
1213 acpigen_write_len_f();
1214
1215 acpigen_emit_simple_namestring(name);
1216 acpigen_emit_byte(level);
1217 acpigen_emit_word(order);
1218}
1219
1220/* Sleep (ms) */
1221void acpigen_write_sleep(uint64_t sleep_ms)
1222{
1223 acpigen_emit_ext_op(SLEEP_OP);
1224 acpigen_write_integer(sleep_ms);
1225}
1226
1227void acpigen_write_store(void)
1228{
1229 acpigen_emit_byte(STORE_OP);
1230}
1231
1232/* Store (src, dst) */
1233void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1234{
1235 acpigen_write_store();
1236 acpigen_emit_byte(src);
1237 acpigen_emit_byte(dst);
1238}
1239
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001240/* Store (src, "namestr") */
1241void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1242{
1243 acpigen_write_store();
1244 acpigen_emit_byte(src);
1245 acpigen_emit_namestring(dst);
1246}
1247
Duncan Laurie8e391d32020-09-30 23:17:41 +00001248/* Store (src, "namestr") */
1249void acpigen_write_store_int_to_namestr(uint64_t src, const char *dst)
1250{
1251 acpigen_write_store();
1252 acpigen_write_integer(src);
1253 acpigen_emit_namestring(dst);
1254}
1255
1256/* Store (src, dst) */
1257void acpigen_write_store_int_to_op(uint64_t src, uint8_t dst)
1258{
1259 acpigen_write_store();
1260 acpigen_write_integer(src);
1261 acpigen_emit_byte(dst);
1262}
1263
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001264/* Or (arg1, arg2, res) */
1265void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1266{
1267 acpigen_emit_byte(OR_OP);
1268 acpigen_emit_byte(arg1);
1269 acpigen_emit_byte(arg2);
1270 acpigen_emit_byte(res);
1271}
1272
Rajat Jain310623b2020-02-26 11:02:53 -08001273/* Xor (arg1, arg2, res) */
1274void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1275{
1276 acpigen_emit_byte(XOR_OP);
1277 acpigen_emit_byte(arg1);
1278 acpigen_emit_byte(arg2);
1279 acpigen_emit_byte(res);
1280}
1281
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001282/* And (arg1, arg2, res) */
1283void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1284{
1285 acpigen_emit_byte(AND_OP);
1286 acpigen_emit_byte(arg1);
1287 acpigen_emit_byte(arg2);
1288 acpigen_emit_byte(res);
1289}
1290
1291/* Not (arg, res) */
1292void acpigen_write_not(uint8_t arg, uint8_t res)
1293{
1294 acpigen_emit_byte(NOT_OP);
1295 acpigen_emit_byte(arg);
1296 acpigen_emit_byte(res);
1297}
1298
1299/* Store (str, DEBUG) */
1300void acpigen_write_debug_string(const char *str)
1301{
1302 acpigen_write_store();
1303 acpigen_write_string(str);
1304 acpigen_emit_ext_op(DEBUG_OP);
1305}
1306
1307/* Store (val, DEBUG) */
1308void acpigen_write_debug_integer(uint64_t val)
1309{
1310 acpigen_write_store();
1311 acpigen_write_integer(val);
1312 acpigen_emit_ext_op(DEBUG_OP);
1313}
1314
1315/* Store (op, DEBUG) */
1316void acpigen_write_debug_op(uint8_t op)
1317{
1318 acpigen_write_store();
1319 acpigen_emit_byte(op);
1320 acpigen_emit_ext_op(DEBUG_OP);
1321}
1322
1323void acpigen_write_if(void)
1324{
1325 acpigen_emit_byte(IF_OP);
1326 acpigen_write_len_f();
1327}
1328
1329/* If (And (arg1, arg2)) */
1330void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1331{
1332 acpigen_write_if();
1333 acpigen_emit_byte(AND_OP);
1334 acpigen_emit_byte(arg1);
1335 acpigen_emit_byte(arg2);
1336}
1337
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001338/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001339 * Generates ACPI code for checking if operand1 and operand2 are equal.
1340 * Both operand1 and operand2 are ACPI ops.
1341 *
1342 * If (Lequal (op,1 op2))
1343 */
1344void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1345{
1346 acpigen_write_if();
1347 acpigen_emit_byte(LEQUAL_OP);
1348 acpigen_emit_byte(op1);
1349 acpigen_emit_byte(op2);
1350}
1351
1352/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001353 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1354 * operand1 is ACPI op and operand2 is an integer.
1355 *
1356 * If (Lequal (op, val))
1357 */
1358void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001359{
1360 acpigen_write_if();
1361 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001362 acpigen_emit_byte(op);
1363 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001364}
1365
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001366/*
1367 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1368 * operand1 is namestring and operand2 is an integer.
1369 *
1370 * If (Lequal ("namestr", val))
1371 */
1372void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1373{
1374 acpigen_write_if();
1375 acpigen_emit_byte(LEQUAL_OP);
1376 acpigen_emit_namestring(namestr);
1377 acpigen_write_integer(val);
1378}
1379
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001380void acpigen_write_else(void)
1381{
1382 acpigen_emit_byte(ELSE_OP);
1383 acpigen_write_len_f();
1384}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001385
Duncan Laurie36858202020-10-06 21:01:51 +00001386void acpigen_write_shiftleft_op_int(uint8_t src_result, uint64_t count)
1387{
1388 acpigen_emit_byte(SHIFT_LEFT_OP);
1389 acpigen_emit_byte(src_result);
1390 acpigen_write_integer(count);
1391 acpigen_emit_byte(ZERO_OP);
1392}
1393
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001394void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1395{
1396 acpigen_emit_byte(TO_BUFFER_OP);
1397 acpigen_emit_byte(src);
1398 acpigen_emit_byte(dst);
1399}
1400
1401void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1402{
1403 acpigen_emit_byte(TO_INTEGER_OP);
1404 acpigen_emit_byte(src);
1405 acpigen_emit_byte(dst);
1406}
1407
Aaron Durbin80bc0912020-08-19 23:17:42 -06001408void acpigen_write_to_integer_from_namestring(const char *source, uint8_t dst_op)
1409{
1410 acpigen_emit_byte(TO_INTEGER_OP);
1411 acpigen_emit_namestring(source);
1412 acpigen_emit_byte(dst_op);
1413}
1414
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301415void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001416{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301417 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001418
1419 acpigen_emit_byte(BUFFER_OP);
1420 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301421 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001422
1423 for (i = 0; i < size; i++)
1424 acpigen_emit_byte(arr[i]);
1425
1426 acpigen_pop_len();
1427}
1428
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301429void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001430{
1431 acpigen_emit_byte(RETURN_OP);
1432 acpigen_write_byte_buffer(arr, size);
1433}
1434
1435void acpigen_write_return_singleton_buffer(uint8_t arg)
1436{
1437 acpigen_write_return_byte_buffer(&arg, 1);
1438}
1439
Duncan Laurie2fe74712020-06-01 17:36:56 -07001440void acpigen_write_return_op(uint8_t arg)
1441{
1442 acpigen_emit_byte(RETURN_OP);
1443 acpigen_emit_byte(arg);
1444}
1445
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001446void acpigen_write_return_byte(uint8_t arg)
1447{
1448 acpigen_emit_byte(RETURN_OP);
1449 acpigen_write_byte(arg);
1450}
1451
Naresh G Solanki05abe432016-11-17 00:16:29 +05301452void acpigen_write_return_integer(uint64_t arg)
1453{
1454 acpigen_emit_byte(RETURN_OP);
1455 acpigen_write_integer(arg);
1456}
1457
1458void acpigen_write_return_string(const char *arg)
1459{
1460 acpigen_emit_byte(RETURN_OP);
1461 acpigen_write_string(arg);
1462}
1463
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001464void acpigen_write_upc(enum acpi_upc_type type)
1465{
1466 acpigen_write_name("_UPC");
1467 acpigen_write_package(4);
1468 /* Connectable */
1469 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1470 /* Type */
1471 acpigen_write_byte(type);
1472 /* Reserved0 */
1473 acpigen_write_zero();
1474 /* Reserved1 */
1475 acpigen_write_zero();
1476 acpigen_pop_len();
1477}
1478
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001479void acpigen_write_pld(const struct acpi_pld *pld)
1480{
1481 uint8_t buf[20];
1482
1483 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1484 return;
1485
1486 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001487 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001488 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001489 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001490}
1491
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301492void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1493 size_t count, void *arg)
1494{
1495 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1496 acpigen_write_dsm_uuid_arr(&id, 1);
1497}
1498
1499static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1500{
1501 size_t i;
1502
1503 /* If (LEqual (Local0, ToUUID(uuid))) */
1504 acpigen_write_if();
1505 acpigen_emit_byte(LEQUAL_OP);
1506 acpigen_emit_byte(LOCAL0_OP);
1507 acpigen_write_uuid(id->uuid);
1508
1509 /* ToInteger (Arg2, Local1) */
1510 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1511
1512 for (i = 0; i < id->count; i++) {
1513 /* If (LEqual (Local1, i)) */
1514 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1515
1516 /* Callback to write if handler. */
1517 if (id->callbacks[i])
1518 id->callbacks[i](id->arg);
1519
1520 acpigen_pop_len(); /* If */
1521 }
1522
1523 /* Default case: Return (Buffer (One) { 0x0 }) */
1524 acpigen_write_return_singleton_buffer(0x0);
1525
1526 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1527
1528}
1529
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001530/*
1531 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301532 * This function takes as input array of uuid for the device, set of callbacks
1533 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1534 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1535 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001536 *
1537 * Arguments passed into _DSM method:
1538 * Arg0 = UUID
1539 * Arg1 = Revision
1540 * Arg2 = Function index
1541 * Arg3 = Function specific arguments
1542 *
1543 * AML code generated would look like:
1544 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301545 * ToBuffer (Arg0, Local0)
1546 * If (LEqual (Local0, ToUUID(uuid))) {
1547 * ToInteger (Arg2, Local1)
1548 * If (LEqual (Local1, 0)) {
1549 * <acpigen by callback[0]>
1550 * }
1551 * ...
1552 * If (LEqual (Local1, n)) {
1553 * <acpigen by callback[n]>
1554 * }
1555 * Return (Buffer (One) { 0x0 })
1556 * }
1557 * ...
1558 * If (LEqual (Local0, ToUUID(uuidn))) {
1559 * ...
1560 * }
1561 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001562 * }
1563 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301564void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001565{
1566 size_t i;
1567
1568 /* Method (_DSM, 4, Serialized) */
1569 acpigen_write_method_serialized("_DSM", 0x4);
1570
1571 /* ToBuffer (Arg0, Local0) */
1572 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1573
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001574 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301575 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001576
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301577 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001578 acpigen_write_return_singleton_buffer(0x0);
1579
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001580 acpigen_pop_len(); /* Method _DSM */
1581}
1582
Matt Delcob425bc82018-08-13 13:36:27 -07001583void acpigen_write_CPPC_package(const struct cppc_config *config)
1584{
1585 u32 i;
1586 u32 max;
1587 switch (config->version) {
1588 case 1:
1589 max = CPPC_MAX_FIELDS_VER_1;
1590 break;
1591 case 2:
1592 max = CPPC_MAX_FIELDS_VER_2;
1593 break;
1594 case 3:
1595 max = CPPC_MAX_FIELDS_VER_3;
1596 break;
1597 default:
1598 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1599 config->version);
1600 return;
1601 }
1602 acpigen_write_name(CPPC_PACKAGE_NAME);
1603
1604 /* Adding 2 to account for length and version fields */
1605 acpigen_write_package(max + 2);
1606 acpigen_write_dword(max + 2);
1607
1608 acpigen_write_byte(config->version);
1609
1610 for (i = 0; i < max; ++i) {
1611 const acpi_addr_t *reg = &(config->regs[i]);
1612 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
Elyes HAOUAS721cb8a2020-06-23 09:13:42 +02001613 reg->bit_width == 32 && reg->access_size == ACPI_ACCESS_SIZE_UNDEFINED) {
Matt Delcob425bc82018-08-13 13:36:27 -07001614 acpigen_write_dword(reg->addrl);
1615 } else {
1616 acpigen_write_register_resource(reg);
1617 }
1618 }
1619 acpigen_pop_len();
1620}
1621
1622void acpigen_write_CPPC_method(void)
1623{
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001624 char pscope[16];
1625 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING "." CPPC_PACKAGE_NAME, 0);
1626
Matt Delcob425bc82018-08-13 13:36:27 -07001627 acpigen_write_method("_CPC", 0);
1628 acpigen_emit_byte(RETURN_OP);
Michael Niewöhnerb20aac02020-10-14 19:30:46 +02001629 acpigen_emit_namestring(pscope);
Matt Delcob425bc82018-08-13 13:36:27 -07001630 acpigen_pop_len();
1631}
1632
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001633/*
1634 * Generate ACPI AML code for _ROM method.
1635 * This function takes as input ROM data and ROM length.
1636 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001637 * The ACPI spec isn't clear about what should happen at the end of the
1638 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1639 * bytes in the returned buffer with zeros.
1640 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001641 * Arguments passed into _DSM method:
1642 * Arg0 = Offset in Bytes
1643 * Arg1 = Bytes to return
1644 *
1645 * Example:
1646 * acpigen_write_rom(0xdeadbeef, 0x10000)
1647 *
1648 * AML code generated would look like:
1649 * Method (_ROM, 2, NotSerialized) {
1650 *
1651 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1652 * Field (ROMS, AnyAcc, NoLock, Preserve)
1653 * {
1654 * Offset (0),
1655 * RBF0, 0x80000
1656 * }
1657 *
1658 * Store (Arg0, Local0)
1659 * Store (Arg1, Local1)
1660 *
1661 * If (LGreater (Local1, 0x1000))
1662 * {
1663 * Store (0x1000, Local1)
1664 * }
1665 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001666 * Store (Local1, Local3)
1667 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001668 * If (LGreater (Local0, 0x10000))
1669 * {
1670 * Return(Buffer(Local1){0})
1671 * }
1672 *
1673 * If (LGreater (Local0, 0x0f000))
1674 * {
1675 * Subtract (0x10000, Local0, Local2)
1676 * If (LGreater (Local1, Local2))
1677 * {
1678 * Store (Local2, Local1)
1679 * }
1680 * }
1681 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001682 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001683 *
1684 * Multiply (Local0, 0x08, Local0)
1685 * Multiply (Local1, 0x08, Local1)
1686 *
1687 * CreateField (RBF0, Local0, Local1, TMPB)
1688 * Store (TMPB, ROM1)
1689 * Return (ROM1)
1690 * }
1691 */
1692
1693void acpigen_write_rom(void *bios, const size_t length)
1694{
1695 ASSERT(bios)
1696 ASSERT(length)
1697
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001698 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001699 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001700
1701 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1702 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1703 (uintptr_t)bios, length);
1704 acpigen_write_opregion(&opreg);
1705
1706 struct fieldlist l[] = {
1707 FIELDLIST_OFFSET(0),
1708 FIELDLIST_NAMESTR("RBF0", 8 * length),
1709 };
1710
1711 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1712 * {
1713 * Offset (0),
1714 * RBF0, 0x80000
1715 * } */
1716 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1717 FIELD_NOLOCK | FIELD_PRESERVE);
1718
1719 /* Store (Arg0, Local0) */
1720 acpigen_write_store();
1721 acpigen_emit_byte(ARG0_OP);
1722 acpigen_emit_byte(LOCAL0_OP);
1723
1724 /* Store (Arg1, Local1) */
1725 acpigen_write_store();
1726 acpigen_emit_byte(ARG1_OP);
1727 acpigen_emit_byte(LOCAL1_OP);
1728
1729 /* ACPI SPEC requires to return at maximum 4KiB */
1730 /* If (LGreater (Local1, 0x1000)) */
1731 acpigen_write_if();
1732 acpigen_emit_byte(LGREATER_OP);
1733 acpigen_emit_byte(LOCAL1_OP);
1734 acpigen_write_integer(0x1000);
1735
1736 /* Store (0x1000, Local1) */
1737 acpigen_write_store();
1738 acpigen_write_integer(0x1000);
1739 acpigen_emit_byte(LOCAL1_OP);
1740
1741 /* Pop if */
1742 acpigen_pop_len();
1743
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001744 /* Store (Local1, Local3) */
1745 acpigen_write_store();
1746 acpigen_emit_byte(LOCAL1_OP);
1747 acpigen_emit_byte(LOCAL3_OP);
1748
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001749 /* If (LGreater (Local0, length)) */
1750 acpigen_write_if();
1751 acpigen_emit_byte(LGREATER_OP);
1752 acpigen_emit_byte(LOCAL0_OP);
1753 acpigen_write_integer(length);
1754
1755 /* Return(Buffer(Local1){0}) */
1756 acpigen_emit_byte(RETURN_OP);
1757 acpigen_emit_byte(BUFFER_OP);
1758 acpigen_write_len_f();
1759 acpigen_emit_byte(LOCAL1_OP);
1760 acpigen_emit_byte(0);
1761 acpigen_pop_len();
1762
1763 /* Pop if */
1764 acpigen_pop_len();
1765
1766 /* If (LGreater (Local0, length - 4096)) */
1767 acpigen_write_if();
1768 acpigen_emit_byte(LGREATER_OP);
1769 acpigen_emit_byte(LOCAL0_OP);
1770 acpigen_write_integer(length - 4096);
1771
1772 /* Subtract (length, Local0, Local2) */
1773 acpigen_emit_byte(SUBTRACT_OP);
1774 acpigen_write_integer(length);
1775 acpigen_emit_byte(LOCAL0_OP);
1776 acpigen_emit_byte(LOCAL2_OP);
1777
1778 /* If (LGreater (Local1, Local2)) */
1779 acpigen_write_if();
1780 acpigen_emit_byte(LGREATER_OP);
1781 acpigen_emit_byte(LOCAL1_OP);
1782 acpigen_emit_byte(LOCAL2_OP);
1783
1784 /* Store (Local2, Local1) */
1785 acpigen_write_store();
1786 acpigen_emit_byte(LOCAL2_OP);
1787 acpigen_emit_byte(LOCAL1_OP);
1788
1789 /* Pop if */
1790 acpigen_pop_len();
1791
1792 /* Pop if */
1793 acpigen_pop_len();
1794
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001795 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001796 acpigen_write_name("ROM1");
1797 acpigen_emit_byte(BUFFER_OP);
1798 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001799 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001800 acpigen_emit_byte(0);
1801 acpigen_pop_len();
1802
1803 /* Multiply (Local1, 0x08, Local1) */
1804 acpigen_emit_byte(MULTIPLY_OP);
1805 acpigen_emit_byte(LOCAL1_OP);
1806 acpigen_write_integer(0x08);
1807 acpigen_emit_byte(LOCAL1_OP);
1808
1809 /* Multiply (Local0, 0x08, Local0) */
1810 acpigen_emit_byte(MULTIPLY_OP);
1811 acpigen_emit_byte(LOCAL0_OP);
1812 acpigen_write_integer(0x08);
1813 acpigen_emit_byte(LOCAL0_OP);
1814
1815 /* CreateField (RBF0, Local0, Local1, TMPB) */
1816 acpigen_emit_ext_op(CREATEFIELD_OP);
1817 acpigen_emit_namestring("RBF0");
1818 acpigen_emit_byte(LOCAL0_OP);
1819 acpigen_emit_byte(LOCAL1_OP);
1820 acpigen_emit_namestring("TMPB");
1821
1822 /* Store (TMPB, ROM1) */
1823 acpigen_write_store();
1824 acpigen_emit_namestring("TMPB");
1825 acpigen_emit_namestring("ROM1");
1826
1827 /* Return (ROM1) */
1828 acpigen_emit_byte(RETURN_OP);
1829 acpigen_emit_namestring("ROM1");
1830
1831 /* Pop method */
1832 acpigen_pop_len();
1833}
1834
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001835/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001836int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001837{
1838 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1839 acpigen_write_debug_string("read_rx_gpio not available");
1840 return -1;
1841}
1842
Aaron Durbin64031672018-04-21 14:45:32 -06001843int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001844{
1845 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1846 acpigen_write_debug_string("get_tx_gpio not available");
1847 return -1;
1848}
1849
Aaron Durbin64031672018-04-21 14:45:32 -06001850int __weak acpigen_soc_set_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001851{
1852 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1853 acpigen_write_debug_string("set_tx_gpio not available");
1854 return -1;
1855}
1856
Aaron Durbin64031672018-04-21 14:45:32 -06001857int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001858{
1859 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1860 acpigen_write_debug_string("clear_tx_gpio not available");
1861 return -1;
1862}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001863
1864/*
1865 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1866 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1867 * make callbacks into SoC acpigen code.
1868 *
1869 * Returns 0 on success and -1 on error.
1870 */
Duncan Laurie30c3f912020-10-09 04:48:53 +00001871int acpigen_enable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001872{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001873 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001874 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001875 else
1876 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001877}
1878
Duncan Laurie30c3f912020-10-09 04:48:53 +00001879int acpigen_disable_tx_gpio(const struct acpi_gpio *gpio)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001880{
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001881 if (gpio->active_low)
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001882 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1883 else
1884 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1885}
Jonathan Zhang71299c22020-01-27 11:38:57 -08001886
Duncan Laurie30c3f912020-10-09 04:48:53 +00001887void acpigen_get_rx_gpio(const struct acpi_gpio *gpio)
Rajat Jain310623b2020-02-26 11:02:53 -08001888{
1889 acpigen_soc_read_rx_gpio(gpio->pins[0]);
1890
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001891 if (gpio->active_low)
Rajat Jain310623b2020-02-26 11:02:53 -08001892 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1893}
1894
Duncan Laurie30c3f912020-10-09 04:48:53 +00001895void acpigen_get_tx_gpio(const struct acpi_gpio *gpio)
Duncan Laurie2fe74712020-06-01 17:36:56 -07001896{
1897 acpigen_soc_get_tx_gpio(gpio->pins[0]);
1898
Furquan Shaikhd2d5e442020-07-01 01:37:13 -07001899 if (gpio->active_low)
Duncan Laurie2fe74712020-06-01 17:36:56 -07001900 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1901}
1902
Jonathan Zhang71299c22020-01-27 11:38:57 -08001903/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
1904void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran,
1905 u16 range_min, u16 range_max, u16 translation, u16 length)
1906{
1907 acpigen_emit_byte(0x88);
1908 /* Byte 1+2: length (0x000d) */
1909 acpigen_emit_byte(0x0d);
1910 acpigen_emit_byte(0x00);
1911 /* resource type */
1912 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1913 /* general flags */
1914 acpigen_emit_byte(gen_flags);
1915 /* type flags */
1916 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1917 acpigen_emit_byte(type_flags);
1918 /* granularity, min, max, translation, length */
1919 acpigen_emit_word(gran);
1920 acpigen_emit_word(range_min);
1921 acpigen_emit_word(range_max);
1922 acpigen_emit_word(translation);
1923 acpigen_emit_word(length);
1924}
1925
1926/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
1927void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags,
1928 u32 gran, u32 range_min, u32 range_max, u32 translation, u32 length)
1929{
1930 acpigen_emit_byte(0x87);
1931 /* Byte 1+2: length (0023) */
1932 acpigen_emit_byte(23);
1933 acpigen_emit_byte(0x00);
1934 /* resource type */
1935 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1936 /* general flags */
1937 acpigen_emit_byte(gen_flags);
1938 /* type flags */
1939 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1940 acpigen_emit_byte(type_flags);
1941 /* granularity, min, max, translation, length */
1942 acpigen_emit_dword(gran);
1943 acpigen_emit_dword(range_min);
1944 acpigen_emit_dword(range_max);
1945 acpigen_emit_dword(translation);
1946 acpigen_emit_dword(length);
1947}
1948
1949static void acpigen_emit_qword(u64 data)
1950{
1951 acpigen_emit_dword(data & 0xffffffff);
1952 acpigen_emit_dword((data >> 32) & 0xffffffff);
1953}
1954
1955/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
1956void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags,
1957 u64 gran, u64 range_min, u64 range_max, u64 translation, u64 length)
1958{
1959 acpigen_emit_byte(0x8a);
1960 /* Byte 1+2: length (0x002b) */
1961 acpigen_emit_byte(0x2b);
1962 acpigen_emit_byte(0x00);
1963 /* resource type */
1964 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1965 /* general flags */
1966 acpigen_emit_byte(gen_flags);
1967 /* type flags */
1968 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1969 acpigen_emit_byte(type_flags);
1970 /* granularity, min, max, translation, length */
1971 acpigen_emit_qword(gran);
1972 acpigen_emit_qword(range_min);
1973 acpigen_emit_qword(range_max);
1974 acpigen_emit_qword(translation);
1975 acpigen_emit_qword(length);
1976}
Furquan Shaikh1a829232020-04-23 11:11:05 -07001977
1978void acpigen_write_ADR(uint64_t adr)
1979{
1980 acpigen_write_name_qword("_ADR", adr);
1981}
1982
1983void acpigen_write_ADR_pci_devfn(pci_devfn_t devfn)
1984{
1985 /*
1986 * _ADR for PCI Bus is encoded as follows:
1987 * [63:32] - unused
1988 * [31:16] - device #
1989 * [15:0] - function #
1990 */
1991 acpigen_write_ADR(PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn));
1992}
1993
1994void acpigen_write_ADR_pci_device(const struct device *dev)
1995{
1996 assert(dev->path.type == DEVICE_PATH_PCI);
1997 acpigen_write_ADR_pci_devfn(dev->path.pci.devfn);
1998}
Duncan Laurie08a942f2020-04-29 12:13:14 -07001999
2000/**
2001 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
2002 * @address: SoundWire device address properties.
2003 *
2004 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
2005 *
2006 * 63..52 - Reserved (0)
2007 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
2008 * Used when a Controller has multiple master devices, each producing a
2009 * separate SoundWire Link. Set to 0 for single-link controllers.
2010 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
2011 * 47..44 - SoundWire specification version that this device supports
2012 * 43..40 - Unique ID for multiple devices
2013 * 39..24 - MIPI standard manufacturer code
2014 * 23..08 - Vendor defined part ID
2015 * 07..00 - MIPI class encoding
2016 */
2017void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
2018{
2019 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
2020 (((uint64_t)address->version & 0xf) << 44) |
2021 (((uint64_t)address->unique_id & 0xf) << 40) |
2022 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
2023 (((uint64_t)address->part_id & 0xffff) << 8) |
2024 (((uint64_t)address->class & 0xff)));
2025}
Tim Wawrzynczakf6945022020-06-04 15:18:47 -06002026
2027void acpigen_notify(const char *namestr, int value)
2028{
2029 acpigen_emit_byte(NOTIFY_OP);
2030 acpigen_emit_namestring(namestr);
2031 acpigen_write_integer(value);
2032}
Aaron Durbin80bc0912020-08-19 23:17:42 -06002033
2034static void _create_field(uint8_t aml_op, uint8_t srcop, size_t byte_offset, const char *name)
2035{
2036 acpigen_emit_byte(aml_op);
2037 acpigen_emit_byte(srcop);
2038 acpigen_write_integer(byte_offset);
2039 acpigen_emit_namestring(name);
2040}
2041
2042void acpigen_write_create_byte_field(uint8_t op, size_t byte_offset, const char *name)
2043{
2044 _create_field(CREATE_BYTE_OP, op, byte_offset, name);
2045}
2046
2047void acpigen_write_create_word_field(uint8_t op, size_t byte_offset, const char *name)
2048{
2049 _create_field(CREATE_WORD_OP, op, byte_offset, name);
2050}
2051
2052void acpigen_write_create_dword_field(uint8_t op, size_t byte_offset, const char *name)
2053{
2054 _create_field(CREATE_DWORD_OP, op, byte_offset, name);
2055}
2056
2057void acpigen_write_create_qword_field(uint8_t op, size_t byte_offset, const char *name)
2058{
2059 _create_field(CREATE_QWORD_OP, op, byte_offset, name);
2060}
Jason Gleneskca36aed2020-09-15 21:01:57 -07002061
2062void acpigen_write_pct_package(const acpi_addr_t *perf_ctrl, const acpi_addr_t *perf_sts)
2063{
2064 acpigen_write_name("_PCT");
2065 acpigen_write_package(0x02);
2066 acpigen_write_register_resource(perf_ctrl);
2067 acpigen_write_register_resource(perf_sts);
2068
2069 acpigen_pop_len();
2070}
2071
2072void acpigen_write_xpss_package(const struct acpi_xpss_sw_pstate *pstate_value)
2073{
2074 acpigen_write_package(0x08);
2075 acpigen_write_dword(pstate_value->core_freq);
2076 acpigen_write_dword(pstate_value->power);
2077 acpigen_write_dword(pstate_value->transition_latency);
2078 acpigen_write_dword(pstate_value->bus_master_latency);
2079
2080 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_value, sizeof(uint64_t));
2081 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_value, sizeof(uint64_t));
2082 acpigen_write_byte_buffer((uint8_t *)&pstate_value->control_mask, sizeof(uint64_t));
2083 acpigen_write_byte_buffer((uint8_t *)&pstate_value->status_mask, sizeof(uint64_t));
2084
2085 acpigen_pop_len();
2086}
2087
2088void acpigen_write_xpss_object(const struct acpi_xpss_sw_pstate *pstate_values, size_t nentries)
2089{
2090 size_t pstate;
2091
2092 acpigen_write_name("XPSS");
2093 acpigen_write_package(nentries);
2094 for (pstate = 0; pstate < nentries; pstate++) {
2095 acpigen_write_xpss_package(pstate_values);
2096 pstate_values++;
2097 }
2098
2099 acpigen_pop_len();
2100}