blob: cc724a0cc1804e0168a459ad4a972dff21c14671 [file] [log] [blame]
Rudolf Marek293b5f52009-02-01 18:35:15 +00001/*
2 * This file is part of the coreboot project.
3 *
Rudolf Marek293b5f52009-02-01 18:35:15 +00004 * This program is free software; you can redistribute it and/or modify
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00005 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
Rudolf Marek293b5f52009-02-01 18:35:15 +00007 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Rudolf Marek293b5f52009-02-01 18:35:15 +000012 */
13
Paul Menzel0f4c0e22013-02-22 12:33:08 +010014/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +000015#define ACPIGEN_LENSTACK_SIZE 10
16
Paul Menzel0f4c0e22013-02-22 12:33:08 +010017/*
Timothy Pearson83abd812015-06-08 19:35:06 -050018 * If you need to change this, change acpigen_write_len_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +010019 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +010020 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000021
Timothy Pearson83abd812015-06-08 19:35:06 -050022#define ACPIGEN_MAXLEN 0xfffff
Rudolf Marek293b5f52009-02-01 18:35:15 +000023
Duncan Laurie3829f232016-05-09 11:08:46 -070024#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000025#include <string.h>
26#include <arch/acpigen.h>
Elyes HAOUAScd4fe0f2019-03-29 17:12:15 +010027#include <assert.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000028#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000029#include <device/device.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000030
31static char *gencurrent;
32
33char *len_stack[ACPIGEN_LENSTACK_SIZE];
34int ltop = 0;
35
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010036void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000037{
38 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010039 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000040 acpigen_emit_byte(0);
41 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050042 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000043}
44
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010045void acpigen_pop_len(void)
46{
47 int len;
48 ASSERT(ltop > 0)
49 char *p = len_stack[--ltop];
50 len = gencurrent - p;
51 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050052 /* generate store length for 0xfffff max */
53 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010054 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050055 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010056
57}
58
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000059void acpigen_set_current(char *curr)
60{
61 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000062}
63
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000064char *acpigen_get_current(void)
65{
66 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000067}
68
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010069void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000070{
71 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000072}
73
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070074void acpigen_emit_ext_op(uint8_t op)
75{
76 acpigen_emit_byte(EXT_OP_PREFIX);
77 acpigen_emit_byte(op);
78}
79
Duncan Laurie9ccae752016-05-09 07:43:19 -070080void acpigen_emit_word(unsigned int data)
81{
82 acpigen_emit_byte(data & 0xff);
83 acpigen_emit_byte((data >> 8) & 0xff);
84}
85
86void acpigen_emit_dword(unsigned int data)
87{
88 acpigen_emit_byte(data & 0xff);
89 acpigen_emit_byte((data >> 8) & 0xff);
90 acpigen_emit_byte((data >> 16) & 0xff);
91 acpigen_emit_byte((data >> 24) & 0xff);
92}
93
Duncan Laurie85d80272016-07-02 19:53:54 -070094char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000095{
Duncan Laurie85d80272016-07-02 19:53:54 -070096 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070097 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010098 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -070099 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +0000100 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -0700101 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000102}
103
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100104void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000105{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700106 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000107 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000108}
109
Duncan Laurie9ccae752016-05-09 07:43:19 -0700110void acpigen_write_word(unsigned int data)
111{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700112 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700113 acpigen_emit_word(data);
114}
115
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100116void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000117{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700118 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700119 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000120}
121
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100122void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000123{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700124 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700125 acpigen_emit_dword(data & 0xffffffff);
126 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000127}
128
Duncan Laurief7c38762016-05-09 08:20:38 -0700129void acpigen_write_zero(void)
130{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700131 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700132}
133
134void acpigen_write_one(void)
135{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700136 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700137}
138
139void acpigen_write_ones(void)
140{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700141 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700142}
143
144void acpigen_write_integer(uint64_t data)
145{
146 if (data == 0)
147 acpigen_write_zero();
148 else if (data == 1)
149 acpigen_write_one();
150 else if (data <= 0xff)
151 acpigen_write_byte((unsigned char)data);
152 else if (data <= 0xffff)
153 acpigen_write_word((unsigned int)data);
154 else if (data <= 0xffffffff)
155 acpigen_write_dword((unsigned int)data);
156 else
157 acpigen_write_qword(data);
158}
159
160void acpigen_write_name_zero(const char *name)
161{
162 acpigen_write_name(name);
163 acpigen_write_one();
164}
165
166void acpigen_write_name_one(const char *name)
167{
168 acpigen_write_name(name);
169 acpigen_write_zero();
170}
171
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100172void acpigen_write_name_byte(const char *name, uint8_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_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000176}
177
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100178void acpigen_write_name_dword(const char *name, uint32_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_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000182}
183
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100184void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000185{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100186 acpigen_write_name(name);
187 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000188}
189
Duncan Laurief7c38762016-05-09 08:20:38 -0700190void acpigen_write_name_integer(const char *name, uint64_t val)
191{
192 acpigen_write_name(name);
193 acpigen_write_integer(val);
194}
195
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700196void acpigen_write_name_string(const char *name, const char *string)
197{
198 acpigen_write_name(name);
199 acpigen_write_string(string);
200}
201
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100202void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000203{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000204 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700205 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000206 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000207}
208
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700209void acpigen_emit_string(const char *string)
210{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200211 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700212 acpigen_emit_byte('\0'); /* NUL */
213}
214
215void acpigen_write_string(const char *string)
216{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700217 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700218 acpigen_emit_string(string);
219}
220
Duncan Laurie37319032016-05-26 12:47:05 -0700221void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
222{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800223 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700224
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700225 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700226 acpigen_write_name_string("_HID", hid);
227}
228
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100229/*
230 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600231 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
232 * and "By convention, when an ASL compiler pads a name shorter than 4
233 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100234 *
235 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
236 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000237
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700238static void acpigen_emit_simple_namestring(const char *name)
239{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100240 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000241 char ud[] = "____";
242 for (i = 0; i < 4; i++) {
243 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100244 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000245 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000246 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700247 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000248 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000249}
250
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700251static void acpigen_emit_double_namestring(const char *name, int dotpos)
252{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700253 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100254 acpigen_emit_simple_namestring(name);
255 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000256}
257
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700258static void acpigen_emit_multi_namestring(const char *name)
259{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100260 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000261 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700262 acpigen_emit_byte(MULTI_NAME_PREFIX);
263 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000264 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
265
266 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100267 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000268 /* find end or next entity */
269 while ((name[0] != '.') && (name[0] != '\0'))
270 name++;
271 /* forward to next */
272 if (name[0] == '.')
273 name++;
274 count++;
275 }
276
277 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000278}
279
280
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700281void acpigen_emit_namestring(const char *namepath)
282{
Rudolf Marek3310d752009-06-21 20:26:13 +0000283 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000284 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000285
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600286 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000287 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100288 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000289 namepath++;
290 }
291
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600292 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000293 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100294 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000295 namepath++;
296 }
297
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200298 /* If we have only \\ or only ^...^. Then we need to put a null
299 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200300 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700301 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100302 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200303 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000304
305 i = 0;
306 while (namepath[i] != '\0') {
307 if (namepath[i] == '.') {
308 dotcount++;
309 dotpos = i;
310 }
311 i++;
312 }
313
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700314 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100315 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700316 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100317 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700318 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100319 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000320}
321
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100322void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000323{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700324 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100325 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000326}
327
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100328void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000329{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700330 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100331 acpigen_write_len_f();
332 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000333}
Rudolf Marekf997b552009-02-14 15:40:23 +0000334
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100335void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000336{
337/*
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600338 Processor (\_PR.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200339 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000340*/
341 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700342 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100343 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000344
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200345 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600346 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100347 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000348 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700349 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000350 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000351}
352
Nico Huber4d211ac2017-09-01 21:14:36 +0200353void acpigen_write_processor_package(const char *const name,
354 const unsigned int first_core,
355 const unsigned int core_count)
356{
357 unsigned int i;
358 char pscope[16];
359
360 acpigen_write_name(name);
361 acpigen_write_package(core_count);
362 for (i = first_core; i < first_core + core_count; ++i) {
363 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
364 acpigen_emit_namestring(pscope);
365 }
366 acpigen_pop_len();
367}
368
Arthur Heymans8f055272018-11-28 13:18:57 +0100369/* Method to notify all CPU cores */
370void acpigen_write_processor_cnot(const unsigned int number_of_cores)
371{
372 int core_id;
373
374 acpigen_write_method("\\_PR.CNOT", 1);
375 for (core_id = 0; core_id < number_of_cores; core_id++) {
376 char buffer[DEVICE_PATH_MAX];
377 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
378 core_id);
379 acpigen_emit_byte(NOTIFY_OP);
380 acpigen_emit_namestring(buffer);
381 acpigen_emit_byte(ARG0_OP);
382 }
383 acpigen_pop_len();
384}
385
Naresh G Solanki8535c662016-10-25 00:51:24 +0530386/*
387 * Generate ACPI AML code for OperationRegion
388 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
389 * where rname is region name, space is region space, offset is region offset &
390 * len is region length.
391 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
392 */
393void acpigen_write_opregion(struct opregion *opreg)
394{
395 /* OpregionOp */
396 acpigen_emit_ext_op(OPREGION_OP);
397 /* NameString 4 chars only */
398 acpigen_emit_simple_namestring(opreg->name);
399 /* RegionSpace */
400 acpigen_emit_byte(opreg->regionspace);
401 /* RegionOffset & RegionLen, it can be byte word or double word */
402 acpigen_write_integer(opreg->regionoffset);
403 acpigen_write_integer(opreg->regionlen);
404}
405
Patrick Rudolph32bae492019-08-08 12:47:30 +0200406/*
407 * Generate ACPI AML code for Mutex
408 * Arg0: Pointer to name of mutex
409 * Arg1: Initial value of mutex
410 */
411void acpigen_write_mutex(const char *name, const uint8_t flags)
412{
413 /* MutexOp */
414 acpigen_emit_ext_op(MUTEX_OP);
415 /* NameString 4 chars only */
416 acpigen_emit_simple_namestring(name);
417 acpigen_emit_byte(flags);
418}
419
420void acpigen_write_acquire(const char *name, const uint16_t val)
421{
422 /* AcquireOp */
423 acpigen_emit_ext_op(ACQUIRE_OP);
424 /* NameString 4 chars only */
425 acpigen_emit_simple_namestring(name);
426 acpigen_emit_word(val);
427}
428
429void acpigen_write_release(const char *name)
430{
431 /* ReleaseOp */
432 acpigen_emit_ext_op(RELEASE_OP);
433 /* NameString 4 chars only */
434 acpigen_emit_simple_namestring(name);
435}
436
Patrick Rudolph894977b2017-07-01 16:15:05 +0200437static void acpigen_write_field_length(uint32_t len)
438{
439 uint8_t i, j;
440 uint8_t emit[4];
441
442 i = 1;
443 if (len < 0x40) {
444 emit[0] = len & 0x3F;
445 } else {
446 emit[0] = len & 0xF;
447 len >>= 4;
448 while (len) {
449 emit[i] = len & 0xFF;
450 i++;
451 len >>= 8;
452 }
453 }
454 /* Update bit 7:6 : Number of bytes followed by emit[0] */
455 emit[0] |= (i - 1) << 6;
456
457 for (j = 0; j < i; j++)
458 acpigen_emit_byte(emit[j]);
459}
460
Naresh G Solanki8535c662016-10-25 00:51:24 +0530461static void acpigen_write_field_offset(uint32_t offset,
462 uint32_t current_bit_pos)
463{
464 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530465
466 if (offset < current_bit_pos) {
467 printk(BIOS_WARNING, "%s: Cannot move offset backward",
468 __func__);
469 return;
470 }
471
472 diff_bits = offset - current_bit_pos;
473 /* Upper limit */
474 if (diff_bits > 0xFFFFFFF) {
475 printk(BIOS_WARNING, "%s: Offset very large to encode",
476 __func__);
477 return;
478 }
479
Naresh G Solanki8535c662016-10-25 00:51:24 +0530480 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200481 acpigen_write_field_length(diff_bits);
482}
483
484static void acpigen_write_field_name(const char *name, uint32_t size)
485{
486 acpigen_emit_simple_namestring(name);
487 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530488}
489
490/*
491 * Generate ACPI AML code for Field
492 * Arg0: region name
493 * Arg1: Pointer to struct fieldlist.
494 * Arg2: no. of entries in Arg1
495 * Arg3: flags which indicate filed access type, lock rule & update rule.
496 * Example with fieldlist
497 * struct fieldlist l[] = {
498 * FIELDLIST_OFFSET(0x84),
499 * FIELDLIST_NAMESTR("PMCS", 2),
500 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200501 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530502 * FIELD_PRESERVE);
503 * Output:
504 * Field (UART, AnyAcc, NoLock, Preserve)
505 * {
506 * Offset (0x84),
507 * PMCS, 2
508 * }
509 */
510void acpigen_write_field(const char *name, struct fieldlist *l, size_t count,
511 uint8_t flags)
512{
513 uint16_t i;
514 uint32_t current_bit_pos = 0;
515
516 /* FieldOp */
517 acpigen_emit_ext_op(FIELD_OP);
518 /* Package Length */
519 acpigen_write_len_f();
520 /* NameString 4 chars only */
521 acpigen_emit_simple_namestring(name);
522 /* Field Flag */
523 acpigen_emit_byte(flags);
524
525 for (i = 0; i < count; i++) {
526 switch (l[i].type) {
527 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200528 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530529 current_bit_pos += l[i].bits;
530 break;
531 case OFFSET:
532 acpigen_write_field_offset(l[i].bits, current_bit_pos);
533 current_bit_pos = l[i].bits;
534 break;
535 default:
536 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
537 , __func__, l[i].type);
538 break;
539 }
540 }
541 acpigen_pop_len();
542}
543
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200544/*
545 * Generate ACPI AML code for IndexField
546 * Arg0: region name
547 * Arg1: Pointer to struct fieldlist.
548 * Arg2: no. of entries in Arg1
549 * Arg3: flags which indicate filed access type, lock rule & update rule.
550 * Example with fieldlist
551 * struct fieldlist l[] = {
552 * FIELDLIST_OFFSET(0x84),
553 * FIELDLIST_NAMESTR("PMCS", 2),
554 * };
555 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
556 * FIELD_NOLOCK |
557 * FIELD_PRESERVE);
558 * Output:
559 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
560 * {
561 * Offset (0x84),
562 * PMCS, 2
563 * }
564 */
565void acpigen_write_indexfield(const char *idx, const char *data,
566 struct fieldlist *l, size_t count, uint8_t flags)
567{
568 uint16_t i;
569 uint32_t current_bit_pos = 0;
570
571 /* FieldOp */
572 acpigen_emit_ext_op(INDEX_FIELD_OP);
573 /* Package Length */
574 acpigen_write_len_f();
575 /* NameString 4 chars only */
576 acpigen_emit_simple_namestring(idx);
577 /* NameString 4 chars only */
578 acpigen_emit_simple_namestring(data);
579 /* Field Flag */
580 acpigen_emit_byte(flags);
581
582 for (i = 0; i < count; i++) {
583 switch (l[i].type) {
584 case NAME_STRING:
585 acpigen_write_field_name(l[i].name, l[i].bits);
586 current_bit_pos += l[i].bits;
587 break;
588 case OFFSET:
589 acpigen_write_field_offset(l[i].bits, current_bit_pos);
590 current_bit_pos = l[i].bits;
591 break;
592 default:
593 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
594 , __func__, l[i].type);
595 break;
596 }
597 }
598 acpigen_pop_len();
599}
600
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100601void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000602{
603/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200604 Name (_PCT, Package (0x02)
605 {
606 ResourceTemplate ()
607 {
608 Register (FFixedHW,
609 0x00, // Bit Width
610 0x00, // Bit Offset
611 0x0000000000000000, // Address
612 ,)
613 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000614
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200615 ResourceTemplate ()
616 {
617 Register (FFixedHW,
618 0x00, // Bit Width
619 0x00, // Bit Offset
620 0x0000000000000000, // Address
621 ,)
622 }
623 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000624*/
625 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700626 /* 00000030 "0._PCT.," */
627 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
628 /* 00000038 "........" */
629 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
630 /* 00000040 "........" */
631 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
632 /* 00000048 "....y..." */
633 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
634 /* 00000050 "........" */
635 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
636 /* 00000058 "........" */
637 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000638 0x00, 0x79, 0x00
639 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100640 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000641}
642
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100643void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200644{
645/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200646 Name (_PTC, Package (0x02)
647 {
648 ResourceTemplate ()
649 {
650 Register (FFixedHW,
651 0x00, // Bit Width
652 0x00, // Bit Offset
653 0x0000000000000000, // Address
654 ,)
655 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200656
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200657 ResourceTemplate ()
658 {
659 Register (FFixedHW,
660 0x00, // Bit Width
661 0x00, // Bit Offset
662 0x0000000000000000, // Address
663 ,)
664 }
665 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200666*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200667 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100668 .space_id = ACPI_ADDRESS_SPACE_FIXED,
669 .bit_width = 0,
670 .bit_offset = 0,
671 .access_size = 0,
672 .addrl = 0,
673 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200674 };
675
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100676 acpigen_write_name("_PTC");
677 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200678
679 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700680 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200681
682 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700683 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200684
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100685 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200686}
687
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700688static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100689{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700690 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100691 acpigen_write_len_f();
692 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700693 acpigen_emit_byte(flags);
694}
695
696/* Method (name, nargs, NotSerialized) */
697void acpigen_write_method(const char *name, int nargs)
698{
699 __acpigen_write_method(name, (nargs & 7));
700}
701
702/* Method (name, nargs, Serialized) */
703void acpigen_write_method_serialized(const char *name, int nargs)
704{
705 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100706}
707
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100708void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100709{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700710 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100711 acpigen_write_len_f();
712 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100713}
714
Duncan Laurieabe2de82016-05-09 11:08:46 -0700715void acpigen_write_STA(uint8_t status)
716{
717 /*
718 * Method (_STA, 0, NotSerialized) { Return (status) }
719 */
720 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700721 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700722 acpigen_write_byte(status);
723 acpigen_pop_len();
724}
725
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100726/*
727 * Generates a func with max supported P-states.
728 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100729void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000730{
731/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200732 Method (_PPC, 0, NotSerialized)
733 {
734 Return (nr)
735 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000736*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100737 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700738 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000739 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100740 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100741 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000742}
743
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100744/*
745 * Generates a func with max supported P-states saved
746 * in the variable PPCM.
747 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100748void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700749{
750/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200751 Method (_PPC, 0, NotSerialized)
752 {
753 Return (PPCM)
754 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700755*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100756 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700757 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700758 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100759 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100760 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700761}
762
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100763void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200764{
765/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200766 // Sample _TPC method
767 Method (_TPC, 0, NotSerialized)
768 {
769 Return (\TLVL)
770 }
771*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100772 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700773 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100774 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100775 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200776}
777
Duncan Laurieabe2de82016-05-09 11:08:46 -0700778void acpigen_write_PRW(u32 wake, u32 level)
779{
780 /*
781 * Name (_PRW, Package () { wake, level }
782 */
783 acpigen_write_name("_PRW");
784 acpigen_write_package(2);
785 acpigen_write_integer(wake);
786 acpigen_write_integer(level);
787 acpigen_pop_len();
788}
789
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100790void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000791 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000792{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100793 acpigen_write_package(6);
794 acpigen_write_dword(coreFreq);
795 acpigen_write_dword(power);
796 acpigen_write_dword(transLat);
797 acpigen_write_dword(busmLat);
798 acpigen_write_dword(control);
799 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100800 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700801
802 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
803 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000804}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000805
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100806void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000807{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100808 acpigen_write_name("_PSD");
809 acpigen_write_package(1);
810 acpigen_write_package(5);
811 acpigen_write_byte(5); // 5 values
812 acpigen_write_byte(0); // revision 0
813 acpigen_write_dword(domain);
814 acpigen_write_dword(coordtype);
815 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100816 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100817 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000818}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000819
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100820void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200821{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100822 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700823 acpigen_write_register_resource(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100824 acpigen_write_dword(cstate->ctype);
825 acpigen_write_dword(cstate->latency);
826 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100827 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200828}
829
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100830void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200831{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100832 int i;
833 acpigen_write_name("_CST");
834 acpigen_write_package(nentries+1);
835 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200836
837 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100838 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200839
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100840 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200841}
842
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700843void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
844 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500845{
846 acpigen_write_name("_CSD");
847 acpigen_write_package(1);
848 acpigen_write_package(6);
849 acpigen_write_byte(6); // 6 values
850 acpigen_write_byte(0); // revision 0
851 acpigen_write_dword(domain);
852 acpigen_write_dword(coordtype);
853 acpigen_write_dword(numprocs);
854 acpigen_write_dword(index);
855 acpigen_pop_len();
856 acpigen_pop_len();
857}
858
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100859void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200860{
861/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200862 Sample _TSS package with 100% and 50% duty cycles
863 Name (_TSS, Package (0x02)
864 {
865 Package(){100, 1000, 0, 0x00, 0)
866 Package(){50, 520, 0, 0x18, 0)
867 })
868*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100869 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200870 acpi_tstate_t *tstate = tstate_list;
871
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100872 acpigen_write_name("_TSS");
873 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200874
875 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100876 acpigen_write_package(5);
877 acpigen_write_dword(tstate->percent);
878 acpigen_write_dword(tstate->power);
879 acpigen_write_dword(tstate->latency);
880 acpigen_write_dword(tstate->control);
881 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100882 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200883 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200884 }
885
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100886 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200887}
888
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100889void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200890{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100891 acpigen_write_name("_TSD");
892 acpigen_write_package(1);
893 acpigen_write_package(5);
894 acpigen_write_byte(5); // 5 values
895 acpigen_write_byte(0); // revision 0
896 acpigen_write_dword(domain);
897 acpigen_write_dword(coordtype);
898 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100899 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100900 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200901}
902
903
904
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100905void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000906{
907 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200908 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000909 * Byte 0:
910 * Bit7 : 1 => big item
911 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
912 */
913 acpigen_emit_byte(0x86);
914 /* Byte 1+2: length (0x0009) */
915 acpigen_emit_byte(0x09);
916 acpigen_emit_byte(0x00);
917 /* bit1-7 are ignored */
918 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700919 acpigen_emit_dword(base);
920 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000921}
922
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700923static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200924{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200925 acpigen_emit_byte(0x82); /* Register Descriptor */
926 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
927 acpigen_emit_byte(0x00); /* Register Length 15:8 */
928 acpigen_emit_byte(addr->space_id); /* Address Space ID */
929 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
930 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100931 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700932 acpigen_emit_dword(addr->addrl); /* Register Address Low */
933 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200934}
935
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700936void acpigen_write_register_resource(const acpi_addr_t *addr)
937{
938 acpigen_write_resourcetemplate_header();
939 acpigen_write_register(addr);
940 acpigen_write_resourcetemplate_footer();
941}
942
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100943void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100944{
945 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200946 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100947 * Byte 0:
948 * Bit7 : 0 => small item
949 * Bit6-3: 0100 (0x4) => IRQ port descriptor
950 * Bit2-0: 010 (0x2) => 2 Bytes long
951 */
952 acpigen_emit_byte(0x22);
953 acpigen_emit_byte(mask & 0xff);
954 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100955}
956
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100957void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000958{
959 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200960 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000961 * Byte 0:
962 * Bit7 : 0 => small item
963 * Bit6-3: 1000 (0x8) => I/O port descriptor
964 * Bit2-0: 111 (0x7) => 7 Bytes long
965 */
966 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100967 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000968 /* bit1-7 are ignored */
969 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
970 /* minimum base address the device may be configured for */
971 acpigen_emit_byte(min & 0xff);
972 acpigen_emit_byte((min >> 8) & 0xff);
973 /* maximum base address the device may be configured for */
974 acpigen_emit_byte(max & 0xff);
975 acpigen_emit_byte((max >> 8) & 0xff);
976 /* alignment for min base */
977 acpigen_emit_byte(align & 0xff);
978 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000979}
980
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100981void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000982{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000983 /*
984 * A ResourceTemplate() is a Buffer() with a
985 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100986 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000987 * (small item 0xf, len 1)
988 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700989 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100990 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700991 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000992 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +0200993 /* Add 2 dummy bytes for the ACPI word (keep aligned with
994 the calclulation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100995 acpigen_emit_byte(0x00);
996 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000997}
998
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100999void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001000{
1001 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001002 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001003 /*
1004 * end tag (acpi 4.0 Section 6.4.2.8)
1005 * 0x79 <checksum>
1006 * 0x00 is treated as a good checksum according to the spec
1007 * and is what iasl generates.
1008 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001009 acpigen_emit_byte(0x79);
1010 acpigen_emit_byte(0x00);
1011
Nico Huber7d89ce32017-04-14 00:08:18 +02001012 /* Start counting past the 2-bytes length added in
1013 acpigen_write_resourcetemplate() above. */
1014 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001015
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001016 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001017 p[0] = len & 0xff;
1018 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001019 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001020 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001021}
1022
1023static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1024 struct resource *res)
1025{
1026 acpigen_write_mem32fixed(0, res->base, res->size);
1027}
1028
1029static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1030 struct resource *res)
1031{
1032 resource_t base = res->base;
1033 resource_t size = res->size;
1034 while (size > 0) {
1035 resource_t sz = size > 255 ? 255 : size;
1036 acpigen_write_io16(base, base, 0, sz, 1);
1037 size -= sz;
1038 base += sz;
1039 }
1040}
1041
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001042void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001043{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001044 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001045
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001046 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001047 search_global_resources(
1048 IORESOURCE_MEM | IORESOURCE_RESERVE,
1049 IORESOURCE_MEM | IORESOURCE_RESERVE,
1050 acpigen_add_mainboard_rsvd_mem32, 0);
1051
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001052 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001053 search_global_resources(
1054 IORESOURCE_IO | IORESOURCE_RESERVE,
1055 IORESOURCE_IO | IORESOURCE_RESERVE,
1056 acpigen_add_mainboard_rsvd_io, 0);
1057
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001058 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001059}
1060
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001061void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001062{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001063 acpigen_write_scope(scope);
1064 acpigen_write_name(name);
1065 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001066 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001067}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001068
1069static int hex2bin(const char c)
1070{
1071 if (c >= 'A' && c <= 'F')
1072 return c - 'A' + 10;
1073 if (c >= 'a' && c <= 'f')
1074 return c - 'a' + 10;
1075 return c - '0';
1076}
1077
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001078void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001079{
1080 u32 compact = 0;
1081
1082 /* Clamping individual values would be better but
1083 there is a disagreement over what is a valid
1084 EISA id, so accept anything and don't clamp,
1085 parent code should create a valid EISAid.
1086 */
1087 compact |= (eisaid[0] - 'A' + 1) << 26;
1088 compact |= (eisaid[1] - 'A' + 1) << 21;
1089 compact |= (eisaid[2] - 'A' + 1) << 16;
1090 compact |= hex2bin(eisaid[3]) << 12;
1091 compact |= hex2bin(eisaid[4]) << 8;
1092 compact |= hex2bin(eisaid[5]) << 4;
1093 compact |= hex2bin(eisaid[6]);
1094
1095 acpigen_emit_byte(0xc);
1096 acpigen_emit_byte((compact >> 24) & 0xff);
1097 acpigen_emit_byte((compact >> 16) & 0xff);
1098 acpigen_emit_byte((compact >> 8) & 0xff);
1099 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001100}
Duncan Laurie3829f232016-05-09 11:08:46 -07001101
1102/*
1103 * ToUUID(uuid)
1104 *
1105 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1106 * order for the bytes that make up a UUID Buffer object.
1107 * UUID byte order for input:
1108 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1109 * UUID byte order for output:
1110 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1111 */
1112#define UUID_LEN 16
1113void acpigen_write_uuid(const char *uuid)
1114{
1115 uint8_t buf[UUID_LEN];
1116 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1117 8, 9, 10, 11, 12, 13, 14, 15 };
1118
1119 /* Parse UUID string into bytes */
1120 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1121 return;
1122
1123 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001124 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001125 acpigen_write_len_f();
1126
1127 /* Buffer length in bytes */
1128 acpigen_write_word(UUID_LEN);
1129
1130 /* Output UUID in expected order */
1131 for (i = 0; i < UUID_LEN; i++)
1132 acpigen_emit_byte(buf[order[i]]);
1133
1134 acpigen_pop_len();
1135}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001136
1137/*
1138 * Name (_PRx, Package(One) { name })
1139 * ...
1140 * PowerResource (name, level, order)
1141 */
1142void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
1143 const char *dev_states[], size_t dev_states_count)
1144{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001145 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001146 for (i = 0; i < dev_states_count; i++) {
1147 acpigen_write_name(dev_states[i]);
1148 acpigen_write_package(1);
1149 acpigen_emit_simple_namestring(name);
1150 acpigen_pop_len(); /* Package */
1151 }
1152
1153 acpigen_emit_ext_op(POWER_RES_OP);
1154
1155 acpigen_write_len_f();
1156
1157 acpigen_emit_simple_namestring(name);
1158 acpigen_emit_byte(level);
1159 acpigen_emit_word(order);
1160}
1161
1162/* Sleep (ms) */
1163void acpigen_write_sleep(uint64_t sleep_ms)
1164{
1165 acpigen_emit_ext_op(SLEEP_OP);
1166 acpigen_write_integer(sleep_ms);
1167}
1168
1169void acpigen_write_store(void)
1170{
1171 acpigen_emit_byte(STORE_OP);
1172}
1173
1174/* Store (src, dst) */
1175void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1176{
1177 acpigen_write_store();
1178 acpigen_emit_byte(src);
1179 acpigen_emit_byte(dst);
1180}
1181
1182/* Or (arg1, arg2, res) */
1183void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1184{
1185 acpigen_emit_byte(OR_OP);
1186 acpigen_emit_byte(arg1);
1187 acpigen_emit_byte(arg2);
1188 acpigen_emit_byte(res);
1189}
1190
1191/* And (arg1, arg2, res) */
1192void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1193{
1194 acpigen_emit_byte(AND_OP);
1195 acpigen_emit_byte(arg1);
1196 acpigen_emit_byte(arg2);
1197 acpigen_emit_byte(res);
1198}
1199
1200/* Not (arg, res) */
1201void acpigen_write_not(uint8_t arg, uint8_t res)
1202{
1203 acpigen_emit_byte(NOT_OP);
1204 acpigen_emit_byte(arg);
1205 acpigen_emit_byte(res);
1206}
1207
1208/* Store (str, DEBUG) */
1209void acpigen_write_debug_string(const char *str)
1210{
1211 acpigen_write_store();
1212 acpigen_write_string(str);
1213 acpigen_emit_ext_op(DEBUG_OP);
1214}
1215
1216/* Store (val, DEBUG) */
1217void acpigen_write_debug_integer(uint64_t val)
1218{
1219 acpigen_write_store();
1220 acpigen_write_integer(val);
1221 acpigen_emit_ext_op(DEBUG_OP);
1222}
1223
1224/* Store (op, DEBUG) */
1225void acpigen_write_debug_op(uint8_t op)
1226{
1227 acpigen_write_store();
1228 acpigen_emit_byte(op);
1229 acpigen_emit_ext_op(DEBUG_OP);
1230}
1231
1232void acpigen_write_if(void)
1233{
1234 acpigen_emit_byte(IF_OP);
1235 acpigen_write_len_f();
1236}
1237
1238/* If (And (arg1, arg2)) */
1239void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1240{
1241 acpigen_write_if();
1242 acpigen_emit_byte(AND_OP);
1243 acpigen_emit_byte(arg1);
1244 acpigen_emit_byte(arg2);
1245}
1246
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001247/*
1248 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1249 * operand1 is ACPI op and operand2 is an integer.
1250 *
1251 * If (Lequal (op, val))
1252 */
1253void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001254{
1255 acpigen_write_if();
1256 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001257 acpigen_emit_byte(op);
1258 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001259}
1260
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001261void acpigen_write_else(void)
1262{
1263 acpigen_emit_byte(ELSE_OP);
1264 acpigen_write_len_f();
1265}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001266
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001267void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1268{
1269 acpigen_emit_byte(TO_BUFFER_OP);
1270 acpigen_emit_byte(src);
1271 acpigen_emit_byte(dst);
1272}
1273
1274void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1275{
1276 acpigen_emit_byte(TO_INTEGER_OP);
1277 acpigen_emit_byte(src);
1278 acpigen_emit_byte(dst);
1279}
1280
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301281void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001282{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301283 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001284
1285 acpigen_emit_byte(BUFFER_OP);
1286 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301287 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001288
1289 for (i = 0; i < size; i++)
1290 acpigen_emit_byte(arr[i]);
1291
1292 acpigen_pop_len();
1293}
1294
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301295void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001296{
1297 acpigen_emit_byte(RETURN_OP);
1298 acpigen_write_byte_buffer(arr, size);
1299}
1300
1301void acpigen_write_return_singleton_buffer(uint8_t arg)
1302{
1303 acpigen_write_return_byte_buffer(&arg, 1);
1304}
1305
1306void acpigen_write_return_byte(uint8_t arg)
1307{
1308 acpigen_emit_byte(RETURN_OP);
1309 acpigen_write_byte(arg);
1310}
1311
Naresh G Solanki05abe432016-11-17 00:16:29 +05301312void acpigen_write_return_integer(uint64_t arg)
1313{
1314 acpigen_emit_byte(RETURN_OP);
1315 acpigen_write_integer(arg);
1316}
1317
1318void acpigen_write_return_string(const char *arg)
1319{
1320 acpigen_emit_byte(RETURN_OP);
1321 acpigen_write_string(arg);
1322}
1323
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001324void acpigen_write_upc(enum acpi_upc_type type)
1325{
1326 acpigen_write_name("_UPC");
1327 acpigen_write_package(4);
1328 /* Connectable */
1329 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1330 /* Type */
1331 acpigen_write_byte(type);
1332 /* Reserved0 */
1333 acpigen_write_zero();
1334 /* Reserved1 */
1335 acpigen_write_zero();
1336 acpigen_pop_len();
1337}
1338
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001339void acpigen_write_pld(const struct acpi_pld *pld)
1340{
1341 uint8_t buf[20];
1342
1343 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1344 return;
1345
1346 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001347 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001348 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001349 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001350}
1351
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301352void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1353 size_t count, void *arg)
1354{
1355 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1356 acpigen_write_dsm_uuid_arr(&id, 1);
1357}
1358
1359static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1360{
1361 size_t i;
1362
1363 /* If (LEqual (Local0, ToUUID(uuid))) */
1364 acpigen_write_if();
1365 acpigen_emit_byte(LEQUAL_OP);
1366 acpigen_emit_byte(LOCAL0_OP);
1367 acpigen_write_uuid(id->uuid);
1368
1369 /* ToInteger (Arg2, Local1) */
1370 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1371
1372 for (i = 0; i < id->count; i++) {
1373 /* If (LEqual (Local1, i)) */
1374 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1375
1376 /* Callback to write if handler. */
1377 if (id->callbacks[i])
1378 id->callbacks[i](id->arg);
1379
1380 acpigen_pop_len(); /* If */
1381 }
1382
1383 /* Default case: Return (Buffer (One) { 0x0 }) */
1384 acpigen_write_return_singleton_buffer(0x0);
1385
1386 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1387
1388}
1389
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001390/*
1391 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301392 * This function takes as input array of uuid for the device, set of callbacks
1393 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1394 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1395 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001396 *
1397 * Arguments passed into _DSM method:
1398 * Arg0 = UUID
1399 * Arg1 = Revision
1400 * Arg2 = Function index
1401 * Arg3 = Function specific arguments
1402 *
1403 * AML code generated would look like:
1404 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301405 * ToBuffer (Arg0, Local0)
1406 * If (LEqual (Local0, ToUUID(uuid))) {
1407 * ToInteger (Arg2, Local1)
1408 * If (LEqual (Local1, 0)) {
1409 * <acpigen by callback[0]>
1410 * }
1411 * ...
1412 * If (LEqual (Local1, n)) {
1413 * <acpigen by callback[n]>
1414 * }
1415 * Return (Buffer (One) { 0x0 })
1416 * }
1417 * ...
1418 * If (LEqual (Local0, ToUUID(uuidn))) {
1419 * ...
1420 * }
1421 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001422 * }
1423 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301424void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001425{
1426 size_t i;
1427
1428 /* Method (_DSM, 4, Serialized) */
1429 acpigen_write_method_serialized("_DSM", 0x4);
1430
1431 /* ToBuffer (Arg0, Local0) */
1432 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1433
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001434 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301435 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001436
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301437 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001438 acpigen_write_return_singleton_buffer(0x0);
1439
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001440 acpigen_pop_len(); /* Method _DSM */
1441}
1442
Matt Delcob425bc82018-08-13 13:36:27 -07001443#define CPPC_PACKAGE_NAME "\\GCPC"
1444
1445void acpigen_write_CPPC_package(const struct cppc_config *config)
1446{
1447 u32 i;
1448 u32 max;
1449 switch (config->version) {
1450 case 1:
1451 max = CPPC_MAX_FIELDS_VER_1;
1452 break;
1453 case 2:
1454 max = CPPC_MAX_FIELDS_VER_2;
1455 break;
1456 case 3:
1457 max = CPPC_MAX_FIELDS_VER_3;
1458 break;
1459 default:
1460 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1461 config->version);
1462 return;
1463 }
1464 acpigen_write_name(CPPC_PACKAGE_NAME);
1465
1466 /* Adding 2 to account for length and version fields */
1467 acpigen_write_package(max + 2);
1468 acpigen_write_dword(max + 2);
1469
1470 acpigen_write_byte(config->version);
1471
1472 for (i = 0; i < max; ++i) {
1473 const acpi_addr_t *reg = &(config->regs[i]);
1474 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
1475 reg->bit_width == 32 && reg->access_size == 0) {
1476 acpigen_write_dword(reg->addrl);
1477 } else {
1478 acpigen_write_register_resource(reg);
1479 }
1480 }
1481 acpigen_pop_len();
1482}
1483
1484void acpigen_write_CPPC_method(void)
1485{
1486 acpigen_write_method("_CPC", 0);
1487 acpigen_emit_byte(RETURN_OP);
1488 acpigen_emit_namestring(CPPC_PACKAGE_NAME);
1489 acpigen_pop_len();
1490}
1491
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001492/*
1493 * Generate ACPI AML code for _ROM method.
1494 * This function takes as input ROM data and ROM length.
1495 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001496 * The ACPI spec isn't clear about what should happen at the end of the
1497 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1498 * bytes in the returned buffer with zeros.
1499 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001500 * Arguments passed into _DSM method:
1501 * Arg0 = Offset in Bytes
1502 * Arg1 = Bytes to return
1503 *
1504 * Example:
1505 * acpigen_write_rom(0xdeadbeef, 0x10000)
1506 *
1507 * AML code generated would look like:
1508 * Method (_ROM, 2, NotSerialized) {
1509 *
1510 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1511 * Field (ROMS, AnyAcc, NoLock, Preserve)
1512 * {
1513 * Offset (0),
1514 * RBF0, 0x80000
1515 * }
1516 *
1517 * Store (Arg0, Local0)
1518 * Store (Arg1, Local1)
1519 *
1520 * If (LGreater (Local1, 0x1000))
1521 * {
1522 * Store (0x1000, Local1)
1523 * }
1524 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001525 * Store (Local1, Local3)
1526 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001527 * If (LGreater (Local0, 0x10000))
1528 * {
1529 * Return(Buffer(Local1){0})
1530 * }
1531 *
1532 * If (LGreater (Local0, 0x0f000))
1533 * {
1534 * Subtract (0x10000, Local0, Local2)
1535 * If (LGreater (Local1, Local2))
1536 * {
1537 * Store (Local2, Local1)
1538 * }
1539 * }
1540 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001541 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001542 *
1543 * Multiply (Local0, 0x08, Local0)
1544 * Multiply (Local1, 0x08, Local1)
1545 *
1546 * CreateField (RBF0, Local0, Local1, TMPB)
1547 * Store (TMPB, ROM1)
1548 * Return (ROM1)
1549 * }
1550 */
1551
1552void acpigen_write_rom(void *bios, const size_t length)
1553{
1554 ASSERT(bios)
1555 ASSERT(length)
1556
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001557 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001558 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001559
1560 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1561 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1562 (uintptr_t)bios, length);
1563 acpigen_write_opregion(&opreg);
1564
1565 struct fieldlist l[] = {
1566 FIELDLIST_OFFSET(0),
1567 FIELDLIST_NAMESTR("RBF0", 8 * length),
1568 };
1569
1570 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1571 * {
1572 * Offset (0),
1573 * RBF0, 0x80000
1574 * } */
1575 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1576 FIELD_NOLOCK | FIELD_PRESERVE);
1577
1578 /* Store (Arg0, Local0) */
1579 acpigen_write_store();
1580 acpigen_emit_byte(ARG0_OP);
1581 acpigen_emit_byte(LOCAL0_OP);
1582
1583 /* Store (Arg1, Local1) */
1584 acpigen_write_store();
1585 acpigen_emit_byte(ARG1_OP);
1586 acpigen_emit_byte(LOCAL1_OP);
1587
1588 /* ACPI SPEC requires to return at maximum 4KiB */
1589 /* If (LGreater (Local1, 0x1000)) */
1590 acpigen_write_if();
1591 acpigen_emit_byte(LGREATER_OP);
1592 acpigen_emit_byte(LOCAL1_OP);
1593 acpigen_write_integer(0x1000);
1594
1595 /* Store (0x1000, Local1) */
1596 acpigen_write_store();
1597 acpigen_write_integer(0x1000);
1598 acpigen_emit_byte(LOCAL1_OP);
1599
1600 /* Pop if */
1601 acpigen_pop_len();
1602
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001603 /* Store (Local1, Local3) */
1604 acpigen_write_store();
1605 acpigen_emit_byte(LOCAL1_OP);
1606 acpigen_emit_byte(LOCAL3_OP);
1607
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001608 /* If (LGreater (Local0, length)) */
1609 acpigen_write_if();
1610 acpigen_emit_byte(LGREATER_OP);
1611 acpigen_emit_byte(LOCAL0_OP);
1612 acpigen_write_integer(length);
1613
1614 /* Return(Buffer(Local1){0}) */
1615 acpigen_emit_byte(RETURN_OP);
1616 acpigen_emit_byte(BUFFER_OP);
1617 acpigen_write_len_f();
1618 acpigen_emit_byte(LOCAL1_OP);
1619 acpigen_emit_byte(0);
1620 acpigen_pop_len();
1621
1622 /* Pop if */
1623 acpigen_pop_len();
1624
1625 /* If (LGreater (Local0, length - 4096)) */
1626 acpigen_write_if();
1627 acpigen_emit_byte(LGREATER_OP);
1628 acpigen_emit_byte(LOCAL0_OP);
1629 acpigen_write_integer(length - 4096);
1630
1631 /* Subtract (length, Local0, Local2) */
1632 acpigen_emit_byte(SUBTRACT_OP);
1633 acpigen_write_integer(length);
1634 acpigen_emit_byte(LOCAL0_OP);
1635 acpigen_emit_byte(LOCAL2_OP);
1636
1637 /* If (LGreater (Local1, Local2)) */
1638 acpigen_write_if();
1639 acpigen_emit_byte(LGREATER_OP);
1640 acpigen_emit_byte(LOCAL1_OP);
1641 acpigen_emit_byte(LOCAL2_OP);
1642
1643 /* Store (Local2, Local1) */
1644 acpigen_write_store();
1645 acpigen_emit_byte(LOCAL2_OP);
1646 acpigen_emit_byte(LOCAL1_OP);
1647
1648 /* Pop if */
1649 acpigen_pop_len();
1650
1651 /* Pop if */
1652 acpigen_pop_len();
1653
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001654 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001655 acpigen_write_name("ROM1");
1656 acpigen_emit_byte(BUFFER_OP);
1657 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001658 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001659 acpigen_emit_byte(0);
1660 acpigen_pop_len();
1661
1662 /* Multiply (Local1, 0x08, Local1) */
1663 acpigen_emit_byte(MULTIPLY_OP);
1664 acpigen_emit_byte(LOCAL1_OP);
1665 acpigen_write_integer(0x08);
1666 acpigen_emit_byte(LOCAL1_OP);
1667
1668 /* Multiply (Local0, 0x08, Local0) */
1669 acpigen_emit_byte(MULTIPLY_OP);
1670 acpigen_emit_byte(LOCAL0_OP);
1671 acpigen_write_integer(0x08);
1672 acpigen_emit_byte(LOCAL0_OP);
1673
1674 /* CreateField (RBF0, Local0, Local1, TMPB) */
1675 acpigen_emit_ext_op(CREATEFIELD_OP);
1676 acpigen_emit_namestring("RBF0");
1677 acpigen_emit_byte(LOCAL0_OP);
1678 acpigen_emit_byte(LOCAL1_OP);
1679 acpigen_emit_namestring("TMPB");
1680
1681 /* Store (TMPB, ROM1) */
1682 acpigen_write_store();
1683 acpigen_emit_namestring("TMPB");
1684 acpigen_emit_namestring("ROM1");
1685
1686 /* Return (ROM1) */
1687 acpigen_emit_byte(RETURN_OP);
1688 acpigen_emit_namestring("ROM1");
1689
1690 /* Pop method */
1691 acpigen_pop_len();
1692}
1693
1694
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001695/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001696int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001697{
1698 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1699 acpigen_write_debug_string("read_rx_gpio not available");
1700 return -1;
1701}
1702
Aaron Durbin64031672018-04-21 14:45:32 -06001703int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001704{
1705 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1706 acpigen_write_debug_string("get_tx_gpio not available");
1707 return -1;
1708}
1709
Aaron Durbin64031672018-04-21 14:45:32 -06001710int __weak acpigen_soc_set_tx_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("set_tx_gpio not available");
1714 return -1;
1715}
1716
Aaron Durbin64031672018-04-21 14:45:32 -06001717int __weak acpigen_soc_clear_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("clear_tx_gpio not available");
1721 return -1;
1722}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001723
1724/*
1725 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1726 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1727 * make callbacks into SoC acpigen code.
1728 *
1729 * Returns 0 on success and -1 on error.
1730 */
1731int acpigen_enable_tx_gpio(struct acpi_gpio *gpio)
1732{
1733 if (gpio->polarity == ACPI_GPIO_ACTIVE_HIGH)
1734 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1735 else
1736 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1737}
1738
1739int acpigen_disable_tx_gpio(struct acpi_gpio *gpio)
1740{
1741 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1742 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1743 else
1744 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1745}