blob: 26fe08fa87f2dffc1bfae0ab30c45ff55fa2b63f [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Rudolf Marek293b5f52009-02-01 18:35:15 +00003
Paul Menzel0f4c0e22013-02-22 12:33:08 +01004/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +00005#define ACPIGEN_LENSTACK_SIZE 10
6
Paul Menzel0f4c0e22013-02-22 12:33:08 +01007/*
Timothy Pearson83abd812015-06-08 19:35:06 -05008 * If you need to change this, change acpigen_write_len_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +01009 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +010010 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000011
Timothy Pearson83abd812015-06-08 19:35:06 -050012#define ACPIGEN_MAXLEN 0xfffff
Rudolf Marek293b5f52009-02-01 18:35:15 +000013
Duncan Laurie3829f232016-05-09 11:08:46 -070014#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000015#include <string.h>
16#include <arch/acpigen.h>
Elyes HAOUAScd4fe0f2019-03-29 17:12:15 +010017#include <assert.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000018#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000019#include <device/device.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000020
21static char *gencurrent;
22
23char *len_stack[ACPIGEN_LENSTACK_SIZE];
24int ltop = 0;
25
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010026void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000027{
28 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010029 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000030 acpigen_emit_byte(0);
31 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050032 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000033}
34
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010035void acpigen_pop_len(void)
36{
37 int len;
38 ASSERT(ltop > 0)
39 char *p = len_stack[--ltop];
40 len = gencurrent - p;
41 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050042 /* generate store length for 0xfffff max */
43 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010044 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050045 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010046
47}
48
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000049void acpigen_set_current(char *curr)
50{
51 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000052}
53
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000054char *acpigen_get_current(void)
55{
56 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000057}
58
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010059void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000060{
61 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000062}
63
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070064void acpigen_emit_ext_op(uint8_t op)
65{
66 acpigen_emit_byte(EXT_OP_PREFIX);
67 acpigen_emit_byte(op);
68}
69
Duncan Laurie9ccae752016-05-09 07:43:19 -070070void acpigen_emit_word(unsigned int data)
71{
72 acpigen_emit_byte(data & 0xff);
73 acpigen_emit_byte((data >> 8) & 0xff);
74}
75
76void acpigen_emit_dword(unsigned int data)
77{
78 acpigen_emit_byte(data & 0xff);
79 acpigen_emit_byte((data >> 8) & 0xff);
80 acpigen_emit_byte((data >> 16) & 0xff);
81 acpigen_emit_byte((data >> 24) & 0xff);
82}
83
Duncan Laurie85d80272016-07-02 19:53:54 -070084char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000085{
Duncan Laurie85d80272016-07-02 19:53:54 -070086 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070087 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010088 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -070089 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +000090 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -070091 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +000092}
93
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010094void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000095{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070096 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +000097 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +000098}
99
Duncan Laurie9ccae752016-05-09 07:43:19 -0700100void acpigen_write_word(unsigned int data)
101{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700102 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700103 acpigen_emit_word(data);
104}
105
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100106void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000107{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700108 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700109 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000110}
111
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100112void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000113{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700114 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700115 acpigen_emit_dword(data & 0xffffffff);
116 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000117}
118
Duncan Laurief7c38762016-05-09 08:20:38 -0700119void acpigen_write_zero(void)
120{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700121 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700122}
123
124void acpigen_write_one(void)
125{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700126 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700127}
128
129void acpigen_write_ones(void)
130{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700131 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700132}
133
134void acpigen_write_integer(uint64_t data)
135{
136 if (data == 0)
137 acpigen_write_zero();
138 else if (data == 1)
139 acpigen_write_one();
140 else if (data <= 0xff)
141 acpigen_write_byte((unsigned char)data);
142 else if (data <= 0xffff)
143 acpigen_write_word((unsigned int)data);
144 else if (data <= 0xffffffff)
145 acpigen_write_dword((unsigned int)data);
146 else
147 acpigen_write_qword(data);
148}
149
150void acpigen_write_name_zero(const char *name)
151{
152 acpigen_write_name(name);
153 acpigen_write_one();
154}
155
156void acpigen_write_name_one(const char *name)
157{
158 acpigen_write_name(name);
159 acpigen_write_zero();
160}
161
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100162void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000163{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100164 acpigen_write_name(name);
165 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000166}
167
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100168void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000169{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100170 acpigen_write_name(name);
171 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000172}
173
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100174void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000175{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100176 acpigen_write_name(name);
177 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000178}
179
Duncan Laurief7c38762016-05-09 08:20:38 -0700180void acpigen_write_name_integer(const char *name, uint64_t val)
181{
182 acpigen_write_name(name);
183 acpigen_write_integer(val);
184}
185
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700186void acpigen_write_name_string(const char *name, const char *string)
187{
188 acpigen_write_name(name);
189 acpigen_write_string(string);
190}
191
Patrick Rudolph389c8272019-12-17 13:54:41 +0100192void acpigen_write_name_unicode(const char *name, const char *string)
193{
194 const size_t len = strlen(string) + 1;
195 acpigen_write_name(name);
196 acpigen_emit_byte(BUFFER_OP);
197 acpigen_write_len_f();
198 acpigen_write_integer(len);
199 for (size_t i = 0; i < len; i++) {
200 const char c = string[i];
201 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
202 acpigen_emit_word(c >= 0 ? c : '?');
203 }
204 acpigen_pop_len();
205}
206
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100207void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000208{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000209 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700210 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000211 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000212}
213
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700214void acpigen_emit_string(const char *string)
215{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200216 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700217 acpigen_emit_byte('\0'); /* NUL */
218}
219
220void acpigen_write_string(const char *string)
221{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700222 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700223 acpigen_emit_string(string);
224}
225
Duncan Laurie37319032016-05-26 12:47:05 -0700226void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
227{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800228 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700229
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700230 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700231 acpigen_write_name_string("_HID", hid);
232}
233
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100234/*
235 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600236 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
237 * and "By convention, when an ASL compiler pads a name shorter than 4
238 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100239 *
240 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
241 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000242
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700243static void acpigen_emit_simple_namestring(const char *name)
244{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100245 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000246 char ud[] = "____";
247 for (i = 0; i < 4; i++) {
248 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100249 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000250 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000251 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700252 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000253 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000254}
255
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700256static void acpigen_emit_double_namestring(const char *name, int dotpos)
257{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700258 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100259 acpigen_emit_simple_namestring(name);
260 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000261}
262
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700263static void acpigen_emit_multi_namestring(const char *name)
264{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100265 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000266 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700267 acpigen_emit_byte(MULTI_NAME_PREFIX);
268 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000269 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
270
271 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100272 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000273 /* find end or next entity */
274 while ((name[0] != '.') && (name[0] != '\0'))
275 name++;
276 /* forward to next */
277 if (name[0] == '.')
278 name++;
279 count++;
280 }
281
282 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000283}
284
285
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700286void acpigen_emit_namestring(const char *namepath)
287{
Rudolf Marek3310d752009-06-21 20:26:13 +0000288 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000289 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000290
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600291 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000292 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100293 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000294 namepath++;
295 }
296
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600297 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000298 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100299 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000300 namepath++;
301 }
302
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200303 /* If we have only \\ or only ^...^. Then we need to put a null
304 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200305 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700306 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100307 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200308 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000309
310 i = 0;
311 while (namepath[i] != '\0') {
312 if (namepath[i] == '.') {
313 dotcount++;
314 dotpos = i;
315 }
316 i++;
317 }
318
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700319 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100320 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700321 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100322 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700323 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100324 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000325}
326
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100327void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000328{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700329 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100330 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000331}
332
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100333void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000334{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700335 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100336 acpigen_write_len_f();
337 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000338}
Rudolf Marekf997b552009-02-14 15:40:23 +0000339
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100340void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000341{
342/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100343 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200344 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000345*/
346 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700347 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100348 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000349
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200350 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600351 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100352 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000353 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700354 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000355 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000356}
357
Nico Huber4d211ac2017-09-01 21:14:36 +0200358void acpigen_write_processor_package(const char *const name,
359 const unsigned int first_core,
360 const unsigned int core_count)
361{
362 unsigned int i;
363 char pscope[16];
364
365 acpigen_write_name(name);
366 acpigen_write_package(core_count);
367 for (i = first_core; i < first_core + core_count; ++i) {
368 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
369 acpigen_emit_namestring(pscope);
370 }
371 acpigen_pop_len();
372}
373
Arthur Heymans8f055272018-11-28 13:18:57 +0100374/* Method to notify all CPU cores */
375void acpigen_write_processor_cnot(const unsigned int number_of_cores)
376{
377 int core_id;
378
Christian Walterbe3979c2019-12-18 15:07:59 +0100379 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100380 for (core_id = 0; core_id < number_of_cores; core_id++) {
381 char buffer[DEVICE_PATH_MAX];
382 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
383 core_id);
384 acpigen_emit_byte(NOTIFY_OP);
385 acpigen_emit_namestring(buffer);
386 acpigen_emit_byte(ARG0_OP);
387 }
388 acpigen_pop_len();
389}
390
Naresh G Solanki8535c662016-10-25 00:51:24 +0530391/*
392 * Generate ACPI AML code for OperationRegion
393 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
394 * where rname is region name, space is region space, offset is region offset &
395 * len is region length.
396 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
397 */
398void acpigen_write_opregion(struct opregion *opreg)
399{
400 /* OpregionOp */
401 acpigen_emit_ext_op(OPREGION_OP);
402 /* NameString 4 chars only */
403 acpigen_emit_simple_namestring(opreg->name);
404 /* RegionSpace */
405 acpigen_emit_byte(opreg->regionspace);
406 /* RegionOffset & RegionLen, it can be byte word or double word */
407 acpigen_write_integer(opreg->regionoffset);
408 acpigen_write_integer(opreg->regionlen);
409}
410
Patrick Rudolph32bae492019-08-08 12:47:30 +0200411/*
412 * Generate ACPI AML code for Mutex
413 * Arg0: Pointer to name of mutex
414 * Arg1: Initial value of mutex
415 */
416void acpigen_write_mutex(const char *name, const uint8_t flags)
417{
418 /* MutexOp */
419 acpigen_emit_ext_op(MUTEX_OP);
420 /* NameString 4 chars only */
421 acpigen_emit_simple_namestring(name);
422 acpigen_emit_byte(flags);
423}
424
425void acpigen_write_acquire(const char *name, const uint16_t val)
426{
427 /* AcquireOp */
428 acpigen_emit_ext_op(ACQUIRE_OP);
429 /* NameString 4 chars only */
430 acpigen_emit_simple_namestring(name);
431 acpigen_emit_word(val);
432}
433
434void acpigen_write_release(const char *name)
435{
436 /* ReleaseOp */
437 acpigen_emit_ext_op(RELEASE_OP);
438 /* NameString 4 chars only */
439 acpigen_emit_simple_namestring(name);
440}
441
Patrick Rudolph894977b2017-07-01 16:15:05 +0200442static void acpigen_write_field_length(uint32_t len)
443{
444 uint8_t i, j;
445 uint8_t emit[4];
446
447 i = 1;
448 if (len < 0x40) {
449 emit[0] = len & 0x3F;
450 } else {
451 emit[0] = len & 0xF;
452 len >>= 4;
453 while (len) {
454 emit[i] = len & 0xFF;
455 i++;
456 len >>= 8;
457 }
458 }
459 /* Update bit 7:6 : Number of bytes followed by emit[0] */
460 emit[0] |= (i - 1) << 6;
461
462 for (j = 0; j < i; j++)
463 acpigen_emit_byte(emit[j]);
464}
465
Naresh G Solanki8535c662016-10-25 00:51:24 +0530466static void acpigen_write_field_offset(uint32_t offset,
467 uint32_t current_bit_pos)
468{
469 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530470
471 if (offset < current_bit_pos) {
472 printk(BIOS_WARNING, "%s: Cannot move offset backward",
473 __func__);
474 return;
475 }
476
477 diff_bits = offset - current_bit_pos;
478 /* Upper limit */
479 if (diff_bits > 0xFFFFFFF) {
480 printk(BIOS_WARNING, "%s: Offset very large to encode",
481 __func__);
482 return;
483 }
484
Naresh G Solanki8535c662016-10-25 00:51:24 +0530485 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200486 acpigen_write_field_length(diff_bits);
487}
488
489static void acpigen_write_field_name(const char *name, uint32_t size)
490{
491 acpigen_emit_simple_namestring(name);
492 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530493}
494
495/*
496 * Generate ACPI AML code for Field
497 * Arg0: region name
498 * Arg1: Pointer to struct fieldlist.
499 * Arg2: no. of entries in Arg1
500 * Arg3: flags which indicate filed access type, lock rule & update rule.
501 * Example with fieldlist
502 * struct fieldlist l[] = {
503 * FIELDLIST_OFFSET(0x84),
504 * FIELDLIST_NAMESTR("PMCS", 2),
505 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200506 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530507 * FIELD_PRESERVE);
508 * Output:
509 * Field (UART, AnyAcc, NoLock, Preserve)
510 * {
511 * Offset (0x84),
512 * PMCS, 2
513 * }
514 */
515void acpigen_write_field(const char *name, struct fieldlist *l, size_t count,
516 uint8_t flags)
517{
518 uint16_t i;
519 uint32_t current_bit_pos = 0;
520
521 /* FieldOp */
522 acpigen_emit_ext_op(FIELD_OP);
523 /* Package Length */
524 acpigen_write_len_f();
525 /* NameString 4 chars only */
526 acpigen_emit_simple_namestring(name);
527 /* Field Flag */
528 acpigen_emit_byte(flags);
529
530 for (i = 0; i < count; i++) {
531 switch (l[i].type) {
532 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200533 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530534 current_bit_pos += l[i].bits;
535 break;
536 case OFFSET:
537 acpigen_write_field_offset(l[i].bits, current_bit_pos);
538 current_bit_pos = l[i].bits;
539 break;
540 default:
541 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
542 , __func__, l[i].type);
543 break;
544 }
545 }
546 acpigen_pop_len();
547}
548
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200549/*
550 * Generate ACPI AML code for IndexField
551 * Arg0: region name
552 * Arg1: Pointer to struct fieldlist.
553 * Arg2: no. of entries in Arg1
554 * Arg3: flags which indicate filed access type, lock rule & update rule.
555 * Example with fieldlist
556 * struct fieldlist l[] = {
557 * FIELDLIST_OFFSET(0x84),
558 * FIELDLIST_NAMESTR("PMCS", 2),
559 * };
560 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
561 * FIELD_NOLOCK |
562 * FIELD_PRESERVE);
563 * Output:
564 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
565 * {
566 * Offset (0x84),
567 * PMCS, 2
568 * }
569 */
570void acpigen_write_indexfield(const char *idx, const char *data,
571 struct fieldlist *l, size_t count, uint8_t flags)
572{
573 uint16_t i;
574 uint32_t current_bit_pos = 0;
575
576 /* FieldOp */
577 acpigen_emit_ext_op(INDEX_FIELD_OP);
578 /* Package Length */
579 acpigen_write_len_f();
580 /* NameString 4 chars only */
581 acpigen_emit_simple_namestring(idx);
582 /* NameString 4 chars only */
583 acpigen_emit_simple_namestring(data);
584 /* Field Flag */
585 acpigen_emit_byte(flags);
586
587 for (i = 0; i < count; i++) {
588 switch (l[i].type) {
589 case NAME_STRING:
590 acpigen_write_field_name(l[i].name, l[i].bits);
591 current_bit_pos += l[i].bits;
592 break;
593 case OFFSET:
594 acpigen_write_field_offset(l[i].bits, current_bit_pos);
595 current_bit_pos = l[i].bits;
596 break;
597 default:
598 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
599 , __func__, l[i].type);
600 break;
601 }
602 }
603 acpigen_pop_len();
604}
605
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100606void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000607{
608/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200609 Name (_PCT, Package (0x02)
610 {
611 ResourceTemplate ()
612 {
613 Register (FFixedHW,
614 0x00, // Bit Width
615 0x00, // Bit Offset
616 0x0000000000000000, // Address
617 ,)
618 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000619
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200620 ResourceTemplate ()
621 {
622 Register (FFixedHW,
623 0x00, // Bit Width
624 0x00, // Bit Offset
625 0x0000000000000000, // Address
626 ,)
627 }
628 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000629*/
630 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700631 /* 00000030 "0._PCT.," */
632 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
633 /* 00000038 "........" */
634 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
635 /* 00000040 "........" */
636 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
637 /* 00000048 "....y..." */
638 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
639 /* 00000050 "........" */
640 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
641 /* 00000058 "........" */
642 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000643 0x00, 0x79, 0x00
644 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100645 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000646}
647
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100648void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200649{
650/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200651 Name (_PTC, Package (0x02)
652 {
653 ResourceTemplate ()
654 {
655 Register (FFixedHW,
656 0x00, // Bit Width
657 0x00, // Bit Offset
658 0x0000000000000000, // Address
659 ,)
660 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200661
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200662 ResourceTemplate ()
663 {
664 Register (FFixedHW,
665 0x00, // Bit Width
666 0x00, // Bit Offset
667 0x0000000000000000, // Address
668 ,)
669 }
670 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200671*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200672 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100673 .space_id = ACPI_ADDRESS_SPACE_FIXED,
674 .bit_width = 0,
675 .bit_offset = 0,
676 .access_size = 0,
677 .addrl = 0,
678 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200679 };
680
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100681 acpigen_write_name("_PTC");
682 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200683
684 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700685 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200686
687 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700688 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200689
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100690 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200691}
692
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700693static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100694{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700695 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100696 acpigen_write_len_f();
697 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700698 acpigen_emit_byte(flags);
699}
700
701/* Method (name, nargs, NotSerialized) */
702void acpigen_write_method(const char *name, int nargs)
703{
704 __acpigen_write_method(name, (nargs & 7));
705}
706
707/* Method (name, nargs, Serialized) */
708void acpigen_write_method_serialized(const char *name, int nargs)
709{
710 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100711}
712
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100713void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100714{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700715 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100716 acpigen_write_len_f();
717 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100718}
719
Duncan Laurieabe2de82016-05-09 11:08:46 -0700720void acpigen_write_STA(uint8_t status)
721{
722 /*
723 * Method (_STA, 0, NotSerialized) { Return (status) }
724 */
725 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700726 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700727 acpigen_write_byte(status);
728 acpigen_pop_len();
729}
730
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100731/*
732 * Generates a func with max supported P-states.
733 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100734void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000735{
736/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200737 Method (_PPC, 0, NotSerialized)
738 {
739 Return (nr)
740 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000741*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100742 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700743 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000744 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100745 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100746 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000747}
748
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100749/*
750 * Generates a func with max supported P-states saved
751 * in the variable PPCM.
752 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100753void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700754{
755/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200756 Method (_PPC, 0, NotSerialized)
757 {
758 Return (PPCM)
759 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700760*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100761 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700762 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700763 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100764 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100765 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700766}
767
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100768void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200769{
770/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200771 // Sample _TPC method
772 Method (_TPC, 0, NotSerialized)
773 {
774 Return (\TLVL)
775 }
776*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100777 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700778 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100779 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100780 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200781}
782
Duncan Laurieabe2de82016-05-09 11:08:46 -0700783void acpigen_write_PRW(u32 wake, u32 level)
784{
785 /*
786 * Name (_PRW, Package () { wake, level }
787 */
788 acpigen_write_name("_PRW");
789 acpigen_write_package(2);
790 acpigen_write_integer(wake);
791 acpigen_write_integer(level);
792 acpigen_pop_len();
793}
794
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100795void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000796 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000797{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100798 acpigen_write_package(6);
799 acpigen_write_dword(coreFreq);
800 acpigen_write_dword(power);
801 acpigen_write_dword(transLat);
802 acpigen_write_dword(busmLat);
803 acpigen_write_dword(control);
804 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100805 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700806
807 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
808 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000809}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000810
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100811void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000812{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100813 acpigen_write_name("_PSD");
814 acpigen_write_package(1);
815 acpigen_write_package(5);
816 acpigen_write_byte(5); // 5 values
817 acpigen_write_byte(0); // revision 0
818 acpigen_write_dword(domain);
819 acpigen_write_dword(coordtype);
820 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100821 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100822 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000823}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000824
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100825void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200826{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100827 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700828 acpigen_write_register_resource(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100829 acpigen_write_dword(cstate->ctype);
830 acpigen_write_dword(cstate->latency);
831 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100832 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200833}
834
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100835void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200836{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100837 int i;
838 acpigen_write_name("_CST");
839 acpigen_write_package(nentries+1);
840 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200841
842 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100843 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200844
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100845 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200846}
847
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700848void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
849 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500850{
851 acpigen_write_name("_CSD");
852 acpigen_write_package(1);
853 acpigen_write_package(6);
854 acpigen_write_byte(6); // 6 values
855 acpigen_write_byte(0); // revision 0
856 acpigen_write_dword(domain);
857 acpigen_write_dword(coordtype);
858 acpigen_write_dword(numprocs);
859 acpigen_write_dword(index);
860 acpigen_pop_len();
861 acpigen_pop_len();
862}
863
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100864void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200865{
866/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200867 Sample _TSS package with 100% and 50% duty cycles
868 Name (_TSS, Package (0x02)
869 {
870 Package(){100, 1000, 0, 0x00, 0)
871 Package(){50, 520, 0, 0x18, 0)
872 })
873*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100874 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200875 acpi_tstate_t *tstate = tstate_list;
876
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100877 acpigen_write_name("_TSS");
878 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200879
880 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100881 acpigen_write_package(5);
882 acpigen_write_dword(tstate->percent);
883 acpigen_write_dword(tstate->power);
884 acpigen_write_dword(tstate->latency);
885 acpigen_write_dword(tstate->control);
886 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100887 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200888 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200889 }
890
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100891 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200892}
893
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100894void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200895{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100896 acpigen_write_name("_TSD");
897 acpigen_write_package(1);
898 acpigen_write_package(5);
899 acpigen_write_byte(5); // 5 values
900 acpigen_write_byte(0); // revision 0
901 acpigen_write_dword(domain);
902 acpigen_write_dword(coordtype);
903 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100904 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100905 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200906}
907
908
909
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100910void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000911{
912 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200913 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000914 * Byte 0:
915 * Bit7 : 1 => big item
916 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
917 */
918 acpigen_emit_byte(0x86);
919 /* Byte 1+2: length (0x0009) */
920 acpigen_emit_byte(0x09);
921 acpigen_emit_byte(0x00);
922 /* bit1-7 are ignored */
923 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700924 acpigen_emit_dword(base);
925 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000926}
927
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700928static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200929{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200930 acpigen_emit_byte(0x82); /* Register Descriptor */
931 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
932 acpigen_emit_byte(0x00); /* Register Length 15:8 */
933 acpigen_emit_byte(addr->space_id); /* Address Space ID */
934 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
935 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100936 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700937 acpigen_emit_dword(addr->addrl); /* Register Address Low */
938 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200939}
940
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700941void acpigen_write_register_resource(const acpi_addr_t *addr)
942{
943 acpigen_write_resourcetemplate_header();
944 acpigen_write_register(addr);
945 acpigen_write_resourcetemplate_footer();
946}
947
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100948void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100949{
950 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200951 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100952 * Byte 0:
953 * Bit7 : 0 => small item
954 * Bit6-3: 0100 (0x4) => IRQ port descriptor
955 * Bit2-0: 010 (0x2) => 2 Bytes long
956 */
957 acpigen_emit_byte(0x22);
958 acpigen_emit_byte(mask & 0xff);
959 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100960}
961
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100962void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000963{
964 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200965 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000966 * Byte 0:
967 * Bit7 : 0 => small item
968 * Bit6-3: 1000 (0x8) => I/O port descriptor
969 * Bit2-0: 111 (0x7) => 7 Bytes long
970 */
971 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100972 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000973 /* bit1-7 are ignored */
974 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
975 /* minimum base address the device may be configured for */
976 acpigen_emit_byte(min & 0xff);
977 acpigen_emit_byte((min >> 8) & 0xff);
978 /* maximum base address the device may be configured for */
979 acpigen_emit_byte(max & 0xff);
980 acpigen_emit_byte((max >> 8) & 0xff);
981 /* alignment for min base */
982 acpigen_emit_byte(align & 0xff);
983 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000984}
985
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100986void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000987{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000988 /*
989 * A ResourceTemplate() is a Buffer() with a
990 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100991 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000992 * (small item 0xf, len 1)
993 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700994 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100995 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700996 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000997 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +0200998 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +0100999 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001000 acpigen_emit_byte(0x00);
1001 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001002}
1003
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001004void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001005{
1006 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001007 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001008 /*
1009 * end tag (acpi 4.0 Section 6.4.2.8)
1010 * 0x79 <checksum>
1011 * 0x00 is treated as a good checksum according to the spec
1012 * and is what iasl generates.
1013 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001014 acpigen_emit_byte(0x79);
1015 acpigen_emit_byte(0x00);
1016
Nico Huber7d89ce32017-04-14 00:08:18 +02001017 /* Start counting past the 2-bytes length added in
1018 acpigen_write_resourcetemplate() above. */
1019 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001020
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001021 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001022 p[0] = len & 0xff;
1023 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001024 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001025 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001026}
1027
1028static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1029 struct resource *res)
1030{
1031 acpigen_write_mem32fixed(0, res->base, res->size);
1032}
1033
1034static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1035 struct resource *res)
1036{
1037 resource_t base = res->base;
1038 resource_t size = res->size;
1039 while (size > 0) {
1040 resource_t sz = size > 255 ? 255 : size;
1041 acpigen_write_io16(base, base, 0, sz, 1);
1042 size -= sz;
1043 base += sz;
1044 }
1045}
1046
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001047void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001048{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001049 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001050
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001051 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001052 search_global_resources(
1053 IORESOURCE_MEM | IORESOURCE_RESERVE,
1054 IORESOURCE_MEM | IORESOURCE_RESERVE,
1055 acpigen_add_mainboard_rsvd_mem32, 0);
1056
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001057 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001058 search_global_resources(
1059 IORESOURCE_IO | IORESOURCE_RESERVE,
1060 IORESOURCE_IO | IORESOURCE_RESERVE,
1061 acpigen_add_mainboard_rsvd_io, 0);
1062
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001063 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001064}
1065
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001066void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001067{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001068 acpigen_write_scope(scope);
1069 acpigen_write_name(name);
1070 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001071 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001072}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001073
1074static int hex2bin(const char c)
1075{
1076 if (c >= 'A' && c <= 'F')
1077 return c - 'A' + 10;
1078 if (c >= 'a' && c <= 'f')
1079 return c - 'a' + 10;
1080 return c - '0';
1081}
1082
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001083void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001084{
1085 u32 compact = 0;
1086
1087 /* Clamping individual values would be better but
1088 there is a disagreement over what is a valid
1089 EISA id, so accept anything and don't clamp,
1090 parent code should create a valid EISAid.
1091 */
1092 compact |= (eisaid[0] - 'A' + 1) << 26;
1093 compact |= (eisaid[1] - 'A' + 1) << 21;
1094 compact |= (eisaid[2] - 'A' + 1) << 16;
1095 compact |= hex2bin(eisaid[3]) << 12;
1096 compact |= hex2bin(eisaid[4]) << 8;
1097 compact |= hex2bin(eisaid[5]) << 4;
1098 compact |= hex2bin(eisaid[6]);
1099
1100 acpigen_emit_byte(0xc);
1101 acpigen_emit_byte((compact >> 24) & 0xff);
1102 acpigen_emit_byte((compact >> 16) & 0xff);
1103 acpigen_emit_byte((compact >> 8) & 0xff);
1104 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001105}
Duncan Laurie3829f232016-05-09 11:08:46 -07001106
1107/*
1108 * ToUUID(uuid)
1109 *
1110 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1111 * order for the bytes that make up a UUID Buffer object.
1112 * UUID byte order for input:
1113 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1114 * UUID byte order for output:
1115 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1116 */
1117#define UUID_LEN 16
1118void acpigen_write_uuid(const char *uuid)
1119{
1120 uint8_t buf[UUID_LEN];
1121 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1122 8, 9, 10, 11, 12, 13, 14, 15 };
1123
1124 /* Parse UUID string into bytes */
1125 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1126 return;
1127
1128 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001129 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001130 acpigen_write_len_f();
1131
1132 /* Buffer length in bytes */
1133 acpigen_write_word(UUID_LEN);
1134
1135 /* Output UUID in expected order */
1136 for (i = 0; i < UUID_LEN; i++)
1137 acpigen_emit_byte(buf[order[i]]);
1138
1139 acpigen_pop_len();
1140}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001141
1142/*
1143 * Name (_PRx, Package(One) { name })
1144 * ...
1145 * PowerResource (name, level, order)
1146 */
1147void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
1148 const char *dev_states[], size_t dev_states_count)
1149{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001150 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001151 for (i = 0; i < dev_states_count; i++) {
1152 acpigen_write_name(dev_states[i]);
1153 acpigen_write_package(1);
1154 acpigen_emit_simple_namestring(name);
1155 acpigen_pop_len(); /* Package */
1156 }
1157
1158 acpigen_emit_ext_op(POWER_RES_OP);
1159
1160 acpigen_write_len_f();
1161
1162 acpigen_emit_simple_namestring(name);
1163 acpigen_emit_byte(level);
1164 acpigen_emit_word(order);
1165}
1166
1167/* Sleep (ms) */
1168void acpigen_write_sleep(uint64_t sleep_ms)
1169{
1170 acpigen_emit_ext_op(SLEEP_OP);
1171 acpigen_write_integer(sleep_ms);
1172}
1173
1174void acpigen_write_store(void)
1175{
1176 acpigen_emit_byte(STORE_OP);
1177}
1178
1179/* Store (src, dst) */
1180void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1181{
1182 acpigen_write_store();
1183 acpigen_emit_byte(src);
1184 acpigen_emit_byte(dst);
1185}
1186
1187/* Or (arg1, arg2, res) */
1188void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1189{
1190 acpigen_emit_byte(OR_OP);
1191 acpigen_emit_byte(arg1);
1192 acpigen_emit_byte(arg2);
1193 acpigen_emit_byte(res);
1194}
1195
Rajat Jain310623b2020-02-26 11:02:53 -08001196/* Xor (arg1, arg2, res) */
1197void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1198{
1199 acpigen_emit_byte(XOR_OP);
1200 acpigen_emit_byte(arg1);
1201 acpigen_emit_byte(arg2);
1202 acpigen_emit_byte(res);
1203}
1204
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001205/* And (arg1, arg2, res) */
1206void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1207{
1208 acpigen_emit_byte(AND_OP);
1209 acpigen_emit_byte(arg1);
1210 acpigen_emit_byte(arg2);
1211 acpigen_emit_byte(res);
1212}
1213
1214/* Not (arg, res) */
1215void acpigen_write_not(uint8_t arg, uint8_t res)
1216{
1217 acpigen_emit_byte(NOT_OP);
1218 acpigen_emit_byte(arg);
1219 acpigen_emit_byte(res);
1220}
1221
1222/* Store (str, DEBUG) */
1223void acpigen_write_debug_string(const char *str)
1224{
1225 acpigen_write_store();
1226 acpigen_write_string(str);
1227 acpigen_emit_ext_op(DEBUG_OP);
1228}
1229
1230/* Store (val, DEBUG) */
1231void acpigen_write_debug_integer(uint64_t val)
1232{
1233 acpigen_write_store();
1234 acpigen_write_integer(val);
1235 acpigen_emit_ext_op(DEBUG_OP);
1236}
1237
1238/* Store (op, DEBUG) */
1239void acpigen_write_debug_op(uint8_t op)
1240{
1241 acpigen_write_store();
1242 acpigen_emit_byte(op);
1243 acpigen_emit_ext_op(DEBUG_OP);
1244}
1245
1246void acpigen_write_if(void)
1247{
1248 acpigen_emit_byte(IF_OP);
1249 acpigen_write_len_f();
1250}
1251
1252/* If (And (arg1, arg2)) */
1253void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1254{
1255 acpigen_write_if();
1256 acpigen_emit_byte(AND_OP);
1257 acpigen_emit_byte(arg1);
1258 acpigen_emit_byte(arg2);
1259}
1260
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001261/*
1262 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1263 * operand1 is ACPI op and operand2 is an integer.
1264 *
1265 * If (Lequal (op, val))
1266 */
1267void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001268{
1269 acpigen_write_if();
1270 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001271 acpigen_emit_byte(op);
1272 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001273}
1274
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001275void acpigen_write_else(void)
1276{
1277 acpigen_emit_byte(ELSE_OP);
1278 acpigen_write_len_f();
1279}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001280
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001281void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1282{
1283 acpigen_emit_byte(TO_BUFFER_OP);
1284 acpigen_emit_byte(src);
1285 acpigen_emit_byte(dst);
1286}
1287
1288void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1289{
1290 acpigen_emit_byte(TO_INTEGER_OP);
1291 acpigen_emit_byte(src);
1292 acpigen_emit_byte(dst);
1293}
1294
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301295void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001296{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301297 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001298
1299 acpigen_emit_byte(BUFFER_OP);
1300 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301301 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001302
1303 for (i = 0; i < size; i++)
1304 acpigen_emit_byte(arr[i]);
1305
1306 acpigen_pop_len();
1307}
1308
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301309void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001310{
1311 acpigen_emit_byte(RETURN_OP);
1312 acpigen_write_byte_buffer(arr, size);
1313}
1314
1315void acpigen_write_return_singleton_buffer(uint8_t arg)
1316{
1317 acpigen_write_return_byte_buffer(&arg, 1);
1318}
1319
1320void acpigen_write_return_byte(uint8_t arg)
1321{
1322 acpigen_emit_byte(RETURN_OP);
1323 acpigen_write_byte(arg);
1324}
1325
Naresh G Solanki05abe432016-11-17 00:16:29 +05301326void acpigen_write_return_integer(uint64_t arg)
1327{
1328 acpigen_emit_byte(RETURN_OP);
1329 acpigen_write_integer(arg);
1330}
1331
1332void acpigen_write_return_string(const char *arg)
1333{
1334 acpigen_emit_byte(RETURN_OP);
1335 acpigen_write_string(arg);
1336}
1337
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001338void acpigen_write_upc(enum acpi_upc_type type)
1339{
1340 acpigen_write_name("_UPC");
1341 acpigen_write_package(4);
1342 /* Connectable */
1343 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1344 /* Type */
1345 acpigen_write_byte(type);
1346 /* Reserved0 */
1347 acpigen_write_zero();
1348 /* Reserved1 */
1349 acpigen_write_zero();
1350 acpigen_pop_len();
1351}
1352
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001353void acpigen_write_pld(const struct acpi_pld *pld)
1354{
1355 uint8_t buf[20];
1356
1357 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1358 return;
1359
1360 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001361 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001362 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001363 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001364}
1365
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301366void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1367 size_t count, void *arg)
1368{
1369 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1370 acpigen_write_dsm_uuid_arr(&id, 1);
1371}
1372
1373static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1374{
1375 size_t i;
1376
1377 /* If (LEqual (Local0, ToUUID(uuid))) */
1378 acpigen_write_if();
1379 acpigen_emit_byte(LEQUAL_OP);
1380 acpigen_emit_byte(LOCAL0_OP);
1381 acpigen_write_uuid(id->uuid);
1382
1383 /* ToInteger (Arg2, Local1) */
1384 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1385
1386 for (i = 0; i < id->count; i++) {
1387 /* If (LEqual (Local1, i)) */
1388 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1389
1390 /* Callback to write if handler. */
1391 if (id->callbacks[i])
1392 id->callbacks[i](id->arg);
1393
1394 acpigen_pop_len(); /* If */
1395 }
1396
1397 /* Default case: Return (Buffer (One) { 0x0 }) */
1398 acpigen_write_return_singleton_buffer(0x0);
1399
1400 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1401
1402}
1403
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001404/*
1405 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301406 * This function takes as input array of uuid for the device, set of callbacks
1407 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1408 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1409 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001410 *
1411 * Arguments passed into _DSM method:
1412 * Arg0 = UUID
1413 * Arg1 = Revision
1414 * Arg2 = Function index
1415 * Arg3 = Function specific arguments
1416 *
1417 * AML code generated would look like:
1418 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301419 * ToBuffer (Arg0, Local0)
1420 * If (LEqual (Local0, ToUUID(uuid))) {
1421 * ToInteger (Arg2, Local1)
1422 * If (LEqual (Local1, 0)) {
1423 * <acpigen by callback[0]>
1424 * }
1425 * ...
1426 * If (LEqual (Local1, n)) {
1427 * <acpigen by callback[n]>
1428 * }
1429 * Return (Buffer (One) { 0x0 })
1430 * }
1431 * ...
1432 * If (LEqual (Local0, ToUUID(uuidn))) {
1433 * ...
1434 * }
1435 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001436 * }
1437 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301438void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001439{
1440 size_t i;
1441
1442 /* Method (_DSM, 4, Serialized) */
1443 acpigen_write_method_serialized("_DSM", 0x4);
1444
1445 /* ToBuffer (Arg0, Local0) */
1446 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1447
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001448 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301449 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001450
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301451 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001452 acpigen_write_return_singleton_buffer(0x0);
1453
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001454 acpigen_pop_len(); /* Method _DSM */
1455}
1456
Matt Delcob425bc82018-08-13 13:36:27 -07001457#define CPPC_PACKAGE_NAME "\\GCPC"
1458
1459void acpigen_write_CPPC_package(const struct cppc_config *config)
1460{
1461 u32 i;
1462 u32 max;
1463 switch (config->version) {
1464 case 1:
1465 max = CPPC_MAX_FIELDS_VER_1;
1466 break;
1467 case 2:
1468 max = CPPC_MAX_FIELDS_VER_2;
1469 break;
1470 case 3:
1471 max = CPPC_MAX_FIELDS_VER_3;
1472 break;
1473 default:
1474 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1475 config->version);
1476 return;
1477 }
1478 acpigen_write_name(CPPC_PACKAGE_NAME);
1479
1480 /* Adding 2 to account for length and version fields */
1481 acpigen_write_package(max + 2);
1482 acpigen_write_dword(max + 2);
1483
1484 acpigen_write_byte(config->version);
1485
1486 for (i = 0; i < max; ++i) {
1487 const acpi_addr_t *reg = &(config->regs[i]);
1488 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
1489 reg->bit_width == 32 && reg->access_size == 0) {
1490 acpigen_write_dword(reg->addrl);
1491 } else {
1492 acpigen_write_register_resource(reg);
1493 }
1494 }
1495 acpigen_pop_len();
1496}
1497
1498void acpigen_write_CPPC_method(void)
1499{
1500 acpigen_write_method("_CPC", 0);
1501 acpigen_emit_byte(RETURN_OP);
1502 acpigen_emit_namestring(CPPC_PACKAGE_NAME);
1503 acpigen_pop_len();
1504}
1505
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001506/*
1507 * Generate ACPI AML code for _ROM method.
1508 * This function takes as input ROM data and ROM length.
1509 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001510 * The ACPI spec isn't clear about what should happen at the end of the
1511 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1512 * bytes in the returned buffer with zeros.
1513 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001514 * Arguments passed into _DSM method:
1515 * Arg0 = Offset in Bytes
1516 * Arg1 = Bytes to return
1517 *
1518 * Example:
1519 * acpigen_write_rom(0xdeadbeef, 0x10000)
1520 *
1521 * AML code generated would look like:
1522 * Method (_ROM, 2, NotSerialized) {
1523 *
1524 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1525 * Field (ROMS, AnyAcc, NoLock, Preserve)
1526 * {
1527 * Offset (0),
1528 * RBF0, 0x80000
1529 * }
1530 *
1531 * Store (Arg0, Local0)
1532 * Store (Arg1, Local1)
1533 *
1534 * If (LGreater (Local1, 0x1000))
1535 * {
1536 * Store (0x1000, Local1)
1537 * }
1538 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001539 * Store (Local1, Local3)
1540 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001541 * If (LGreater (Local0, 0x10000))
1542 * {
1543 * Return(Buffer(Local1){0})
1544 * }
1545 *
1546 * If (LGreater (Local0, 0x0f000))
1547 * {
1548 * Subtract (0x10000, Local0, Local2)
1549 * If (LGreater (Local1, Local2))
1550 * {
1551 * Store (Local2, Local1)
1552 * }
1553 * }
1554 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001555 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001556 *
1557 * Multiply (Local0, 0x08, Local0)
1558 * Multiply (Local1, 0x08, Local1)
1559 *
1560 * CreateField (RBF0, Local0, Local1, TMPB)
1561 * Store (TMPB, ROM1)
1562 * Return (ROM1)
1563 * }
1564 */
1565
1566void acpigen_write_rom(void *bios, const size_t length)
1567{
1568 ASSERT(bios)
1569 ASSERT(length)
1570
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001571 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001572 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001573
1574 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1575 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1576 (uintptr_t)bios, length);
1577 acpigen_write_opregion(&opreg);
1578
1579 struct fieldlist l[] = {
1580 FIELDLIST_OFFSET(0),
1581 FIELDLIST_NAMESTR("RBF0", 8 * length),
1582 };
1583
1584 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1585 * {
1586 * Offset (0),
1587 * RBF0, 0x80000
1588 * } */
1589 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1590 FIELD_NOLOCK | FIELD_PRESERVE);
1591
1592 /* Store (Arg0, Local0) */
1593 acpigen_write_store();
1594 acpigen_emit_byte(ARG0_OP);
1595 acpigen_emit_byte(LOCAL0_OP);
1596
1597 /* Store (Arg1, Local1) */
1598 acpigen_write_store();
1599 acpigen_emit_byte(ARG1_OP);
1600 acpigen_emit_byte(LOCAL1_OP);
1601
1602 /* ACPI SPEC requires to return at maximum 4KiB */
1603 /* If (LGreater (Local1, 0x1000)) */
1604 acpigen_write_if();
1605 acpigen_emit_byte(LGREATER_OP);
1606 acpigen_emit_byte(LOCAL1_OP);
1607 acpigen_write_integer(0x1000);
1608
1609 /* Store (0x1000, Local1) */
1610 acpigen_write_store();
1611 acpigen_write_integer(0x1000);
1612 acpigen_emit_byte(LOCAL1_OP);
1613
1614 /* Pop if */
1615 acpigen_pop_len();
1616
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001617 /* Store (Local1, Local3) */
1618 acpigen_write_store();
1619 acpigen_emit_byte(LOCAL1_OP);
1620 acpigen_emit_byte(LOCAL3_OP);
1621
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001622 /* If (LGreater (Local0, length)) */
1623 acpigen_write_if();
1624 acpigen_emit_byte(LGREATER_OP);
1625 acpigen_emit_byte(LOCAL0_OP);
1626 acpigen_write_integer(length);
1627
1628 /* Return(Buffer(Local1){0}) */
1629 acpigen_emit_byte(RETURN_OP);
1630 acpigen_emit_byte(BUFFER_OP);
1631 acpigen_write_len_f();
1632 acpigen_emit_byte(LOCAL1_OP);
1633 acpigen_emit_byte(0);
1634 acpigen_pop_len();
1635
1636 /* Pop if */
1637 acpigen_pop_len();
1638
1639 /* If (LGreater (Local0, length - 4096)) */
1640 acpigen_write_if();
1641 acpigen_emit_byte(LGREATER_OP);
1642 acpigen_emit_byte(LOCAL0_OP);
1643 acpigen_write_integer(length - 4096);
1644
1645 /* Subtract (length, Local0, Local2) */
1646 acpigen_emit_byte(SUBTRACT_OP);
1647 acpigen_write_integer(length);
1648 acpigen_emit_byte(LOCAL0_OP);
1649 acpigen_emit_byte(LOCAL2_OP);
1650
1651 /* If (LGreater (Local1, Local2)) */
1652 acpigen_write_if();
1653 acpigen_emit_byte(LGREATER_OP);
1654 acpigen_emit_byte(LOCAL1_OP);
1655 acpigen_emit_byte(LOCAL2_OP);
1656
1657 /* Store (Local2, Local1) */
1658 acpigen_write_store();
1659 acpigen_emit_byte(LOCAL2_OP);
1660 acpigen_emit_byte(LOCAL1_OP);
1661
1662 /* Pop if */
1663 acpigen_pop_len();
1664
1665 /* Pop if */
1666 acpigen_pop_len();
1667
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001668 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001669 acpigen_write_name("ROM1");
1670 acpigen_emit_byte(BUFFER_OP);
1671 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001672 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001673 acpigen_emit_byte(0);
1674 acpigen_pop_len();
1675
1676 /* Multiply (Local1, 0x08, Local1) */
1677 acpigen_emit_byte(MULTIPLY_OP);
1678 acpigen_emit_byte(LOCAL1_OP);
1679 acpigen_write_integer(0x08);
1680 acpigen_emit_byte(LOCAL1_OP);
1681
1682 /* Multiply (Local0, 0x08, Local0) */
1683 acpigen_emit_byte(MULTIPLY_OP);
1684 acpigen_emit_byte(LOCAL0_OP);
1685 acpigen_write_integer(0x08);
1686 acpigen_emit_byte(LOCAL0_OP);
1687
1688 /* CreateField (RBF0, Local0, Local1, TMPB) */
1689 acpigen_emit_ext_op(CREATEFIELD_OP);
1690 acpigen_emit_namestring("RBF0");
1691 acpigen_emit_byte(LOCAL0_OP);
1692 acpigen_emit_byte(LOCAL1_OP);
1693 acpigen_emit_namestring("TMPB");
1694
1695 /* Store (TMPB, ROM1) */
1696 acpigen_write_store();
1697 acpigen_emit_namestring("TMPB");
1698 acpigen_emit_namestring("ROM1");
1699
1700 /* Return (ROM1) */
1701 acpigen_emit_byte(RETURN_OP);
1702 acpigen_emit_namestring("ROM1");
1703
1704 /* Pop method */
1705 acpigen_pop_len();
1706}
1707
1708
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001709/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001710int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001711{
1712 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1713 acpigen_write_debug_string("read_rx_gpio not available");
1714 return -1;
1715}
1716
Aaron Durbin64031672018-04-21 14:45:32 -06001717int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001718{
1719 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1720 acpigen_write_debug_string("get_tx_gpio not available");
1721 return -1;
1722}
1723
Aaron Durbin64031672018-04-21 14:45:32 -06001724int __weak acpigen_soc_set_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001725{
1726 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1727 acpigen_write_debug_string("set_tx_gpio not available");
1728 return -1;
1729}
1730
Aaron Durbin64031672018-04-21 14:45:32 -06001731int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001732{
1733 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1734 acpigen_write_debug_string("clear_tx_gpio not available");
1735 return -1;
1736}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001737
1738/*
1739 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1740 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1741 * make callbacks into SoC acpigen code.
1742 *
1743 * Returns 0 on success and -1 on error.
1744 */
1745int acpigen_enable_tx_gpio(struct acpi_gpio *gpio)
1746{
1747 if (gpio->polarity == ACPI_GPIO_ACTIVE_HIGH)
1748 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1749 else
1750 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1751}
1752
1753int acpigen_disable_tx_gpio(struct acpi_gpio *gpio)
1754{
1755 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1756 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1757 else
1758 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1759}
Jonathan Zhang71299c22020-01-27 11:38:57 -08001760
Rajat Jain310623b2020-02-26 11:02:53 -08001761void acpigen_get_rx_gpio(struct acpi_gpio *gpio)
1762{
1763 acpigen_soc_read_rx_gpio(gpio->pins[0]);
1764
1765 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1766 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1767}
1768
Jonathan Zhang71299c22020-01-27 11:38:57 -08001769/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
1770void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran,
1771 u16 range_min, u16 range_max, u16 translation, u16 length)
1772{
1773 acpigen_emit_byte(0x88);
1774 /* Byte 1+2: length (0x000d) */
1775 acpigen_emit_byte(0x0d);
1776 acpigen_emit_byte(0x00);
1777 /* resource type */
1778 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1779 /* general flags */
1780 acpigen_emit_byte(gen_flags);
1781 /* type flags */
1782 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1783 acpigen_emit_byte(type_flags);
1784 /* granularity, min, max, translation, length */
1785 acpigen_emit_word(gran);
1786 acpigen_emit_word(range_min);
1787 acpigen_emit_word(range_max);
1788 acpigen_emit_word(translation);
1789 acpigen_emit_word(length);
1790}
1791
1792/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
1793void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags,
1794 u32 gran, u32 range_min, u32 range_max, u32 translation, u32 length)
1795{
1796 acpigen_emit_byte(0x87);
1797 /* Byte 1+2: length (0023) */
1798 acpigen_emit_byte(23);
1799 acpigen_emit_byte(0x00);
1800 /* resource type */
1801 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1802 /* general flags */
1803 acpigen_emit_byte(gen_flags);
1804 /* type flags */
1805 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1806 acpigen_emit_byte(type_flags);
1807 /* granularity, min, max, translation, length */
1808 acpigen_emit_dword(gran);
1809 acpigen_emit_dword(range_min);
1810 acpigen_emit_dword(range_max);
1811 acpigen_emit_dword(translation);
1812 acpigen_emit_dword(length);
1813}
1814
1815static void acpigen_emit_qword(u64 data)
1816{
1817 acpigen_emit_dword(data & 0xffffffff);
1818 acpigen_emit_dword((data >> 32) & 0xffffffff);
1819}
1820
1821/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
1822void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags,
1823 u64 gran, u64 range_min, u64 range_max, u64 translation, u64 length)
1824{
1825 acpigen_emit_byte(0x8a);
1826 /* Byte 1+2: length (0x002b) */
1827 acpigen_emit_byte(0x2b);
1828 acpigen_emit_byte(0x00);
1829 /* resource type */
1830 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1831 /* general flags */
1832 acpigen_emit_byte(gen_flags);
1833 /* type flags */
1834 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1835 acpigen_emit_byte(type_flags);
1836 /* granularity, min, max, translation, length */
1837 acpigen_emit_qword(gran);
1838 acpigen_emit_qword(range_min);
1839 acpigen_emit_qword(range_max);
1840 acpigen_emit_qword(translation);
1841 acpigen_emit_qword(length);
1842}