blob: 9162cdb371ac499670b8bc954431ce9cdf2a9307 [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
Patrick Rudolph389c8272019-12-17 13:54:41 +0100202void acpigen_write_name_unicode(const char *name, const char *string)
203{
204 const size_t len = strlen(string) + 1;
205 acpigen_write_name(name);
206 acpigen_emit_byte(BUFFER_OP);
207 acpigen_write_len_f();
208 acpigen_write_integer(len);
209 for (size_t i = 0; i < len; i++) {
210 const char c = string[i];
211 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
212 acpigen_emit_word(c >= 0 ? c : '?');
213 }
214 acpigen_pop_len();
215}
216
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100217void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000218{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000219 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700220 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000221 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000222}
223
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700224void acpigen_emit_string(const char *string)
225{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200226 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700227 acpigen_emit_byte('\0'); /* NUL */
228}
229
230void acpigen_write_string(const char *string)
231{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700232 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700233 acpigen_emit_string(string);
234}
235
Duncan Laurie37319032016-05-26 12:47:05 -0700236void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
237{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800238 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700239
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700240 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700241 acpigen_write_name_string("_HID", hid);
242}
243
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100244/*
245 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600246 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
247 * and "By convention, when an ASL compiler pads a name shorter than 4
248 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100249 *
250 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
251 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000252
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700253static void acpigen_emit_simple_namestring(const char *name)
254{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100255 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000256 char ud[] = "____";
257 for (i = 0; i < 4; i++) {
258 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100259 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000260 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000261 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700262 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000263 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000264}
265
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700266static void acpigen_emit_double_namestring(const char *name, int dotpos)
267{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700268 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100269 acpigen_emit_simple_namestring(name);
270 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000271}
272
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700273static void acpigen_emit_multi_namestring(const char *name)
274{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100275 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000276 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700277 acpigen_emit_byte(MULTI_NAME_PREFIX);
278 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000279 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
280
281 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100282 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000283 /* find end or next entity */
284 while ((name[0] != '.') && (name[0] != '\0'))
285 name++;
286 /* forward to next */
287 if (name[0] == '.')
288 name++;
289 count++;
290 }
291
292 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000293}
294
295
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700296void acpigen_emit_namestring(const char *namepath)
297{
Rudolf Marek3310d752009-06-21 20:26:13 +0000298 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000299 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000300
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600301 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000302 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100303 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000304 namepath++;
305 }
306
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600307 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000308 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100309 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000310 namepath++;
311 }
312
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200313 /* If we have only \\ or only ^...^. Then we need to put a null
314 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200315 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700316 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100317 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200318 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000319
320 i = 0;
321 while (namepath[i] != '\0') {
322 if (namepath[i] == '.') {
323 dotcount++;
324 dotpos = i;
325 }
326 i++;
327 }
328
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700329 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100330 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700331 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100332 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700333 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100334 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000335}
336
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100337void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000338{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700339 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100340 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000341}
342
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100343void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000344{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700345 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100346 acpigen_write_len_f();
347 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000348}
Rudolf Marekf997b552009-02-14 15:40:23 +0000349
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100350void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000351{
352/*
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600353 Processor (\_PR.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200354 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000355*/
356 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700357 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100358 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000359
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200360 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600361 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100362 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000363 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700364 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000365 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000366}
367
Nico Huber4d211ac2017-09-01 21:14:36 +0200368void acpigen_write_processor_package(const char *const name,
369 const unsigned int first_core,
370 const unsigned int core_count)
371{
372 unsigned int i;
373 char pscope[16];
374
375 acpigen_write_name(name);
376 acpigen_write_package(core_count);
377 for (i = first_core; i < first_core + core_count; ++i) {
378 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
379 acpigen_emit_namestring(pscope);
380 }
381 acpigen_pop_len();
382}
383
Arthur Heymans8f055272018-11-28 13:18:57 +0100384/* Method to notify all CPU cores */
385void acpigen_write_processor_cnot(const unsigned int number_of_cores)
386{
387 int core_id;
388
389 acpigen_write_method("\\_PR.CNOT", 1);
390 for (core_id = 0; core_id < number_of_cores; core_id++) {
391 char buffer[DEVICE_PATH_MAX];
392 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
393 core_id);
394 acpigen_emit_byte(NOTIFY_OP);
395 acpigen_emit_namestring(buffer);
396 acpigen_emit_byte(ARG0_OP);
397 }
398 acpigen_pop_len();
399}
400
Naresh G Solanki8535c662016-10-25 00:51:24 +0530401/*
402 * Generate ACPI AML code for OperationRegion
403 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
404 * where rname is region name, space is region space, offset is region offset &
405 * len is region length.
406 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
407 */
408void acpigen_write_opregion(struct opregion *opreg)
409{
410 /* OpregionOp */
411 acpigen_emit_ext_op(OPREGION_OP);
412 /* NameString 4 chars only */
413 acpigen_emit_simple_namestring(opreg->name);
414 /* RegionSpace */
415 acpigen_emit_byte(opreg->regionspace);
416 /* RegionOffset & RegionLen, it can be byte word or double word */
417 acpigen_write_integer(opreg->regionoffset);
418 acpigen_write_integer(opreg->regionlen);
419}
420
Patrick Rudolph32bae492019-08-08 12:47:30 +0200421/*
422 * Generate ACPI AML code for Mutex
423 * Arg0: Pointer to name of mutex
424 * Arg1: Initial value of mutex
425 */
426void acpigen_write_mutex(const char *name, const uint8_t flags)
427{
428 /* MutexOp */
429 acpigen_emit_ext_op(MUTEX_OP);
430 /* NameString 4 chars only */
431 acpigen_emit_simple_namestring(name);
432 acpigen_emit_byte(flags);
433}
434
435void acpigen_write_acquire(const char *name, const uint16_t val)
436{
437 /* AcquireOp */
438 acpigen_emit_ext_op(ACQUIRE_OP);
439 /* NameString 4 chars only */
440 acpigen_emit_simple_namestring(name);
441 acpigen_emit_word(val);
442}
443
444void acpigen_write_release(const char *name)
445{
446 /* ReleaseOp */
447 acpigen_emit_ext_op(RELEASE_OP);
448 /* NameString 4 chars only */
449 acpigen_emit_simple_namestring(name);
450}
451
Patrick Rudolph894977b2017-07-01 16:15:05 +0200452static void acpigen_write_field_length(uint32_t len)
453{
454 uint8_t i, j;
455 uint8_t emit[4];
456
457 i = 1;
458 if (len < 0x40) {
459 emit[0] = len & 0x3F;
460 } else {
461 emit[0] = len & 0xF;
462 len >>= 4;
463 while (len) {
464 emit[i] = len & 0xFF;
465 i++;
466 len >>= 8;
467 }
468 }
469 /* Update bit 7:6 : Number of bytes followed by emit[0] */
470 emit[0] |= (i - 1) << 6;
471
472 for (j = 0; j < i; j++)
473 acpigen_emit_byte(emit[j]);
474}
475
Naresh G Solanki8535c662016-10-25 00:51:24 +0530476static void acpigen_write_field_offset(uint32_t offset,
477 uint32_t current_bit_pos)
478{
479 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530480
481 if (offset < current_bit_pos) {
482 printk(BIOS_WARNING, "%s: Cannot move offset backward",
483 __func__);
484 return;
485 }
486
487 diff_bits = offset - current_bit_pos;
488 /* Upper limit */
489 if (diff_bits > 0xFFFFFFF) {
490 printk(BIOS_WARNING, "%s: Offset very large to encode",
491 __func__);
492 return;
493 }
494
Naresh G Solanki8535c662016-10-25 00:51:24 +0530495 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200496 acpigen_write_field_length(diff_bits);
497}
498
499static void acpigen_write_field_name(const char *name, uint32_t size)
500{
501 acpigen_emit_simple_namestring(name);
502 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530503}
504
505/*
506 * Generate ACPI AML code for Field
507 * Arg0: region name
508 * Arg1: Pointer to struct fieldlist.
509 * Arg2: no. of entries in Arg1
510 * Arg3: flags which indicate filed access type, lock rule & update rule.
511 * Example with fieldlist
512 * struct fieldlist l[] = {
513 * FIELDLIST_OFFSET(0x84),
514 * FIELDLIST_NAMESTR("PMCS", 2),
515 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200516 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530517 * FIELD_PRESERVE);
518 * Output:
519 * Field (UART, AnyAcc, NoLock, Preserve)
520 * {
521 * Offset (0x84),
522 * PMCS, 2
523 * }
524 */
525void acpigen_write_field(const char *name, struct fieldlist *l, size_t count,
526 uint8_t flags)
527{
528 uint16_t i;
529 uint32_t current_bit_pos = 0;
530
531 /* FieldOp */
532 acpigen_emit_ext_op(FIELD_OP);
533 /* Package Length */
534 acpigen_write_len_f();
535 /* NameString 4 chars only */
536 acpigen_emit_simple_namestring(name);
537 /* Field Flag */
538 acpigen_emit_byte(flags);
539
540 for (i = 0; i < count; i++) {
541 switch (l[i].type) {
542 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200543 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530544 current_bit_pos += l[i].bits;
545 break;
546 case OFFSET:
547 acpigen_write_field_offset(l[i].bits, current_bit_pos);
548 current_bit_pos = l[i].bits;
549 break;
550 default:
551 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
552 , __func__, l[i].type);
553 break;
554 }
555 }
556 acpigen_pop_len();
557}
558
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200559/*
560 * Generate ACPI AML code for IndexField
561 * Arg0: region name
562 * Arg1: Pointer to struct fieldlist.
563 * Arg2: no. of entries in Arg1
564 * Arg3: flags which indicate filed access type, lock rule & update rule.
565 * Example with fieldlist
566 * struct fieldlist l[] = {
567 * FIELDLIST_OFFSET(0x84),
568 * FIELDLIST_NAMESTR("PMCS", 2),
569 * };
570 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
571 * FIELD_NOLOCK |
572 * FIELD_PRESERVE);
573 * Output:
574 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
575 * {
576 * Offset (0x84),
577 * PMCS, 2
578 * }
579 */
580void acpigen_write_indexfield(const char *idx, const char *data,
581 struct fieldlist *l, size_t count, uint8_t flags)
582{
583 uint16_t i;
584 uint32_t current_bit_pos = 0;
585
586 /* FieldOp */
587 acpigen_emit_ext_op(INDEX_FIELD_OP);
588 /* Package Length */
589 acpigen_write_len_f();
590 /* NameString 4 chars only */
591 acpigen_emit_simple_namestring(idx);
592 /* NameString 4 chars only */
593 acpigen_emit_simple_namestring(data);
594 /* Field Flag */
595 acpigen_emit_byte(flags);
596
597 for (i = 0; i < count; i++) {
598 switch (l[i].type) {
599 case NAME_STRING:
600 acpigen_write_field_name(l[i].name, l[i].bits);
601 current_bit_pos += l[i].bits;
602 break;
603 case OFFSET:
604 acpigen_write_field_offset(l[i].bits, current_bit_pos);
605 current_bit_pos = l[i].bits;
606 break;
607 default:
608 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
609 , __func__, l[i].type);
610 break;
611 }
612 }
613 acpigen_pop_len();
614}
615
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100616void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000617{
618/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200619 Name (_PCT, Package (0x02)
620 {
621 ResourceTemplate ()
622 {
623 Register (FFixedHW,
624 0x00, // Bit Width
625 0x00, // Bit Offset
626 0x0000000000000000, // Address
627 ,)
628 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000629
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200630 ResourceTemplate ()
631 {
632 Register (FFixedHW,
633 0x00, // Bit Width
634 0x00, // Bit Offset
635 0x0000000000000000, // Address
636 ,)
637 }
638 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000639*/
640 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700641 /* 00000030 "0._PCT.," */
642 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
643 /* 00000038 "........" */
644 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
645 /* 00000040 "........" */
646 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
647 /* 00000048 "....y..." */
648 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
649 /* 00000050 "........" */
650 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
651 /* 00000058 "........" */
652 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000653 0x00, 0x79, 0x00
654 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100655 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000656}
657
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100658void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200659{
660/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200661 Name (_PTC, Package (0x02)
662 {
663 ResourceTemplate ()
664 {
665 Register (FFixedHW,
666 0x00, // Bit Width
667 0x00, // Bit Offset
668 0x0000000000000000, // Address
669 ,)
670 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200671
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200672 ResourceTemplate ()
673 {
674 Register (FFixedHW,
675 0x00, // Bit Width
676 0x00, // Bit Offset
677 0x0000000000000000, // Address
678 ,)
679 }
680 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200681*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200682 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100683 .space_id = ACPI_ADDRESS_SPACE_FIXED,
684 .bit_width = 0,
685 .bit_offset = 0,
686 .access_size = 0,
687 .addrl = 0,
688 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200689 };
690
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100691 acpigen_write_name("_PTC");
692 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200693
694 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700695 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200696
697 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700698 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200699
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100700 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200701}
702
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700703static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100704{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700705 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100706 acpigen_write_len_f();
707 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700708 acpigen_emit_byte(flags);
709}
710
711/* Method (name, nargs, NotSerialized) */
712void acpigen_write_method(const char *name, int nargs)
713{
714 __acpigen_write_method(name, (nargs & 7));
715}
716
717/* Method (name, nargs, Serialized) */
718void acpigen_write_method_serialized(const char *name, int nargs)
719{
720 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100721}
722
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100723void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100724{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700725 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100726 acpigen_write_len_f();
727 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100728}
729
Duncan Laurieabe2de82016-05-09 11:08:46 -0700730void acpigen_write_STA(uint8_t status)
731{
732 /*
733 * Method (_STA, 0, NotSerialized) { Return (status) }
734 */
735 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700736 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700737 acpigen_write_byte(status);
738 acpigen_pop_len();
739}
740
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100741/*
742 * Generates a func with max supported P-states.
743 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100744void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000745{
746/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200747 Method (_PPC, 0, NotSerialized)
748 {
749 Return (nr)
750 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000751*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100752 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700753 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000754 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100755 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100756 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000757}
758
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100759/*
760 * Generates a func with max supported P-states saved
761 * in the variable PPCM.
762 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100763void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700764{
765/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200766 Method (_PPC, 0, NotSerialized)
767 {
768 Return (PPCM)
769 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700770*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100771 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700772 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700773 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100774 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100775 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700776}
777
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100778void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200779{
780/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200781 // Sample _TPC method
782 Method (_TPC, 0, NotSerialized)
783 {
784 Return (\TLVL)
785 }
786*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100787 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700788 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100789 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100790 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200791}
792
Duncan Laurieabe2de82016-05-09 11:08:46 -0700793void acpigen_write_PRW(u32 wake, u32 level)
794{
795 /*
796 * Name (_PRW, Package () { wake, level }
797 */
798 acpigen_write_name("_PRW");
799 acpigen_write_package(2);
800 acpigen_write_integer(wake);
801 acpigen_write_integer(level);
802 acpigen_pop_len();
803}
804
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100805void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000806 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000807{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100808 acpigen_write_package(6);
809 acpigen_write_dword(coreFreq);
810 acpigen_write_dword(power);
811 acpigen_write_dword(transLat);
812 acpigen_write_dword(busmLat);
813 acpigen_write_dword(control);
814 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100815 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700816
817 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
818 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000819}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000820
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100821void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000822{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100823 acpigen_write_name("_PSD");
824 acpigen_write_package(1);
825 acpigen_write_package(5);
826 acpigen_write_byte(5); // 5 values
827 acpigen_write_byte(0); // revision 0
828 acpigen_write_dword(domain);
829 acpigen_write_dword(coordtype);
830 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100831 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100832 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000833}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000834
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100835void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200836{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100837 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700838 acpigen_write_register_resource(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100839 acpigen_write_dword(cstate->ctype);
840 acpigen_write_dword(cstate->latency);
841 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100842 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200843}
844
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100845void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200846{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100847 int i;
848 acpigen_write_name("_CST");
849 acpigen_write_package(nentries+1);
850 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200851
852 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100853 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200854
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100855 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200856}
857
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700858void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
859 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500860{
861 acpigen_write_name("_CSD");
862 acpigen_write_package(1);
863 acpigen_write_package(6);
864 acpigen_write_byte(6); // 6 values
865 acpigen_write_byte(0); // revision 0
866 acpigen_write_dword(domain);
867 acpigen_write_dword(coordtype);
868 acpigen_write_dword(numprocs);
869 acpigen_write_dword(index);
870 acpigen_pop_len();
871 acpigen_pop_len();
872}
873
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100874void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200875{
876/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200877 Sample _TSS package with 100% and 50% duty cycles
878 Name (_TSS, Package (0x02)
879 {
880 Package(){100, 1000, 0, 0x00, 0)
881 Package(){50, 520, 0, 0x18, 0)
882 })
883*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100884 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200885 acpi_tstate_t *tstate = tstate_list;
886
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100887 acpigen_write_name("_TSS");
888 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200889
890 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100891 acpigen_write_package(5);
892 acpigen_write_dword(tstate->percent);
893 acpigen_write_dword(tstate->power);
894 acpigen_write_dword(tstate->latency);
895 acpigen_write_dword(tstate->control);
896 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100897 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200898 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200899 }
900
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100901 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200902}
903
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100904void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200905{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100906 acpigen_write_name("_TSD");
907 acpigen_write_package(1);
908 acpigen_write_package(5);
909 acpigen_write_byte(5); // 5 values
910 acpigen_write_byte(0); // revision 0
911 acpigen_write_dword(domain);
912 acpigen_write_dword(coordtype);
913 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100914 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100915 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200916}
917
918
919
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100920void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000921{
922 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200923 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000924 * Byte 0:
925 * Bit7 : 1 => big item
926 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
927 */
928 acpigen_emit_byte(0x86);
929 /* Byte 1+2: length (0x0009) */
930 acpigen_emit_byte(0x09);
931 acpigen_emit_byte(0x00);
932 /* bit1-7 are ignored */
933 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700934 acpigen_emit_dword(base);
935 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000936}
937
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700938static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200939{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200940 acpigen_emit_byte(0x82); /* Register Descriptor */
941 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
942 acpigen_emit_byte(0x00); /* Register Length 15:8 */
943 acpigen_emit_byte(addr->space_id); /* Address Space ID */
944 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
945 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100946 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700947 acpigen_emit_dword(addr->addrl); /* Register Address Low */
948 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200949}
950
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700951void acpigen_write_register_resource(const acpi_addr_t *addr)
952{
953 acpigen_write_resourcetemplate_header();
954 acpigen_write_register(addr);
955 acpigen_write_resourcetemplate_footer();
956}
957
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100958void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100959{
960 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200961 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100962 * Byte 0:
963 * Bit7 : 0 => small item
964 * Bit6-3: 0100 (0x4) => IRQ port descriptor
965 * Bit2-0: 010 (0x2) => 2 Bytes long
966 */
967 acpigen_emit_byte(0x22);
968 acpigen_emit_byte(mask & 0xff);
969 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100970}
971
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100972void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000973{
974 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200975 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000976 * Byte 0:
977 * Bit7 : 0 => small item
978 * Bit6-3: 1000 (0x8) => I/O port descriptor
979 * Bit2-0: 111 (0x7) => 7 Bytes long
980 */
981 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100982 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000983 /* bit1-7 are ignored */
984 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
985 /* minimum base address the device may be configured for */
986 acpigen_emit_byte(min & 0xff);
987 acpigen_emit_byte((min >> 8) & 0xff);
988 /* maximum base address the device may be configured for */
989 acpigen_emit_byte(max & 0xff);
990 acpigen_emit_byte((max >> 8) & 0xff);
991 /* alignment for min base */
992 acpigen_emit_byte(align & 0xff);
993 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000994}
995
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100996void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000997{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000998 /*
999 * A ResourceTemplate() is a Buffer() with a
1000 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001001 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001002 * (small item 0xf, len 1)
1003 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001004 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001005 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001006 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001007 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001008 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001009 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001010 acpigen_emit_byte(0x00);
1011 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001012}
1013
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001014void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001015{
1016 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001017 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001018 /*
1019 * end tag (acpi 4.0 Section 6.4.2.8)
1020 * 0x79 <checksum>
1021 * 0x00 is treated as a good checksum according to the spec
1022 * and is what iasl generates.
1023 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001024 acpigen_emit_byte(0x79);
1025 acpigen_emit_byte(0x00);
1026
Nico Huber7d89ce32017-04-14 00:08:18 +02001027 /* Start counting past the 2-bytes length added in
1028 acpigen_write_resourcetemplate() above. */
1029 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001030
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001031 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001032 p[0] = len & 0xff;
1033 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001034 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001035 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001036}
1037
1038static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1039 struct resource *res)
1040{
1041 acpigen_write_mem32fixed(0, res->base, res->size);
1042}
1043
1044static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1045 struct resource *res)
1046{
1047 resource_t base = res->base;
1048 resource_t size = res->size;
1049 while (size > 0) {
1050 resource_t sz = size > 255 ? 255 : size;
1051 acpigen_write_io16(base, base, 0, sz, 1);
1052 size -= sz;
1053 base += sz;
1054 }
1055}
1056
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001057void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001058{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001059 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001060
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001061 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001062 search_global_resources(
1063 IORESOURCE_MEM | IORESOURCE_RESERVE,
1064 IORESOURCE_MEM | IORESOURCE_RESERVE,
1065 acpigen_add_mainboard_rsvd_mem32, 0);
1066
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001067 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001068 search_global_resources(
1069 IORESOURCE_IO | IORESOURCE_RESERVE,
1070 IORESOURCE_IO | IORESOURCE_RESERVE,
1071 acpigen_add_mainboard_rsvd_io, 0);
1072
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001073 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001074}
1075
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001076void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001077{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001078 acpigen_write_scope(scope);
1079 acpigen_write_name(name);
1080 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001081 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001082}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001083
1084static int hex2bin(const char c)
1085{
1086 if (c >= 'A' && c <= 'F')
1087 return c - 'A' + 10;
1088 if (c >= 'a' && c <= 'f')
1089 return c - 'a' + 10;
1090 return c - '0';
1091}
1092
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001093void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001094{
1095 u32 compact = 0;
1096
1097 /* Clamping individual values would be better but
1098 there is a disagreement over what is a valid
1099 EISA id, so accept anything and don't clamp,
1100 parent code should create a valid EISAid.
1101 */
1102 compact |= (eisaid[0] - 'A' + 1) << 26;
1103 compact |= (eisaid[1] - 'A' + 1) << 21;
1104 compact |= (eisaid[2] - 'A' + 1) << 16;
1105 compact |= hex2bin(eisaid[3]) << 12;
1106 compact |= hex2bin(eisaid[4]) << 8;
1107 compact |= hex2bin(eisaid[5]) << 4;
1108 compact |= hex2bin(eisaid[6]);
1109
1110 acpigen_emit_byte(0xc);
1111 acpigen_emit_byte((compact >> 24) & 0xff);
1112 acpigen_emit_byte((compact >> 16) & 0xff);
1113 acpigen_emit_byte((compact >> 8) & 0xff);
1114 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001115}
Duncan Laurie3829f232016-05-09 11:08:46 -07001116
1117/*
1118 * ToUUID(uuid)
1119 *
1120 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1121 * order for the bytes that make up a UUID Buffer object.
1122 * UUID byte order for input:
1123 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1124 * UUID byte order for output:
1125 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1126 */
1127#define UUID_LEN 16
1128void acpigen_write_uuid(const char *uuid)
1129{
1130 uint8_t buf[UUID_LEN];
1131 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1132 8, 9, 10, 11, 12, 13, 14, 15 };
1133
1134 /* Parse UUID string into bytes */
1135 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1136 return;
1137
1138 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001139 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001140 acpigen_write_len_f();
1141
1142 /* Buffer length in bytes */
1143 acpigen_write_word(UUID_LEN);
1144
1145 /* Output UUID in expected order */
1146 for (i = 0; i < UUID_LEN; i++)
1147 acpigen_emit_byte(buf[order[i]]);
1148
1149 acpigen_pop_len();
1150}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001151
1152/*
1153 * Name (_PRx, Package(One) { name })
1154 * ...
1155 * PowerResource (name, level, order)
1156 */
1157void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
1158 const char *dev_states[], size_t dev_states_count)
1159{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001160 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001161 for (i = 0; i < dev_states_count; i++) {
1162 acpigen_write_name(dev_states[i]);
1163 acpigen_write_package(1);
1164 acpigen_emit_simple_namestring(name);
1165 acpigen_pop_len(); /* Package */
1166 }
1167
1168 acpigen_emit_ext_op(POWER_RES_OP);
1169
1170 acpigen_write_len_f();
1171
1172 acpigen_emit_simple_namestring(name);
1173 acpigen_emit_byte(level);
1174 acpigen_emit_word(order);
1175}
1176
1177/* Sleep (ms) */
1178void acpigen_write_sleep(uint64_t sleep_ms)
1179{
1180 acpigen_emit_ext_op(SLEEP_OP);
1181 acpigen_write_integer(sleep_ms);
1182}
1183
1184void acpigen_write_store(void)
1185{
1186 acpigen_emit_byte(STORE_OP);
1187}
1188
1189/* Store (src, dst) */
1190void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1191{
1192 acpigen_write_store();
1193 acpigen_emit_byte(src);
1194 acpigen_emit_byte(dst);
1195}
1196
1197/* Or (arg1, arg2, res) */
1198void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1199{
1200 acpigen_emit_byte(OR_OP);
1201 acpigen_emit_byte(arg1);
1202 acpigen_emit_byte(arg2);
1203 acpigen_emit_byte(res);
1204}
1205
Rajat Jain310623b2020-02-26 11:02:53 -08001206/* Xor (arg1, arg2, res) */
1207void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1208{
1209 acpigen_emit_byte(XOR_OP);
1210 acpigen_emit_byte(arg1);
1211 acpigen_emit_byte(arg2);
1212 acpigen_emit_byte(res);
1213}
1214
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001215/* And (arg1, arg2, res) */
1216void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1217{
1218 acpigen_emit_byte(AND_OP);
1219 acpigen_emit_byte(arg1);
1220 acpigen_emit_byte(arg2);
1221 acpigen_emit_byte(res);
1222}
1223
1224/* Not (arg, res) */
1225void acpigen_write_not(uint8_t arg, uint8_t res)
1226{
1227 acpigen_emit_byte(NOT_OP);
1228 acpigen_emit_byte(arg);
1229 acpigen_emit_byte(res);
1230}
1231
1232/* Store (str, DEBUG) */
1233void acpigen_write_debug_string(const char *str)
1234{
1235 acpigen_write_store();
1236 acpigen_write_string(str);
1237 acpigen_emit_ext_op(DEBUG_OP);
1238}
1239
1240/* Store (val, DEBUG) */
1241void acpigen_write_debug_integer(uint64_t val)
1242{
1243 acpigen_write_store();
1244 acpigen_write_integer(val);
1245 acpigen_emit_ext_op(DEBUG_OP);
1246}
1247
1248/* Store (op, DEBUG) */
1249void acpigen_write_debug_op(uint8_t op)
1250{
1251 acpigen_write_store();
1252 acpigen_emit_byte(op);
1253 acpigen_emit_ext_op(DEBUG_OP);
1254}
1255
1256void acpigen_write_if(void)
1257{
1258 acpigen_emit_byte(IF_OP);
1259 acpigen_write_len_f();
1260}
1261
1262/* If (And (arg1, arg2)) */
1263void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1264{
1265 acpigen_write_if();
1266 acpigen_emit_byte(AND_OP);
1267 acpigen_emit_byte(arg1);
1268 acpigen_emit_byte(arg2);
1269}
1270
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001271/*
1272 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1273 * operand1 is ACPI op and operand2 is an integer.
1274 *
1275 * If (Lequal (op, val))
1276 */
1277void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001278{
1279 acpigen_write_if();
1280 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001281 acpigen_emit_byte(op);
1282 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001283}
1284
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001285void acpigen_write_else(void)
1286{
1287 acpigen_emit_byte(ELSE_OP);
1288 acpigen_write_len_f();
1289}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001290
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001291void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1292{
1293 acpigen_emit_byte(TO_BUFFER_OP);
1294 acpigen_emit_byte(src);
1295 acpigen_emit_byte(dst);
1296}
1297
1298void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1299{
1300 acpigen_emit_byte(TO_INTEGER_OP);
1301 acpigen_emit_byte(src);
1302 acpigen_emit_byte(dst);
1303}
1304
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301305void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001306{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301307 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001308
1309 acpigen_emit_byte(BUFFER_OP);
1310 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301311 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001312
1313 for (i = 0; i < size; i++)
1314 acpigen_emit_byte(arr[i]);
1315
1316 acpigen_pop_len();
1317}
1318
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301319void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001320{
1321 acpigen_emit_byte(RETURN_OP);
1322 acpigen_write_byte_buffer(arr, size);
1323}
1324
1325void acpigen_write_return_singleton_buffer(uint8_t arg)
1326{
1327 acpigen_write_return_byte_buffer(&arg, 1);
1328}
1329
1330void acpigen_write_return_byte(uint8_t arg)
1331{
1332 acpigen_emit_byte(RETURN_OP);
1333 acpigen_write_byte(arg);
1334}
1335
Naresh G Solanki05abe432016-11-17 00:16:29 +05301336void acpigen_write_return_integer(uint64_t arg)
1337{
1338 acpigen_emit_byte(RETURN_OP);
1339 acpigen_write_integer(arg);
1340}
1341
1342void acpigen_write_return_string(const char *arg)
1343{
1344 acpigen_emit_byte(RETURN_OP);
1345 acpigen_write_string(arg);
1346}
1347
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001348void acpigen_write_upc(enum acpi_upc_type type)
1349{
1350 acpigen_write_name("_UPC");
1351 acpigen_write_package(4);
1352 /* Connectable */
1353 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1354 /* Type */
1355 acpigen_write_byte(type);
1356 /* Reserved0 */
1357 acpigen_write_zero();
1358 /* Reserved1 */
1359 acpigen_write_zero();
1360 acpigen_pop_len();
1361}
1362
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001363void acpigen_write_pld(const struct acpi_pld *pld)
1364{
1365 uint8_t buf[20];
1366
1367 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1368 return;
1369
1370 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001371 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001372 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001373 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001374}
1375
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301376void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1377 size_t count, void *arg)
1378{
1379 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1380 acpigen_write_dsm_uuid_arr(&id, 1);
1381}
1382
1383static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1384{
1385 size_t i;
1386
1387 /* If (LEqual (Local0, ToUUID(uuid))) */
1388 acpigen_write_if();
1389 acpigen_emit_byte(LEQUAL_OP);
1390 acpigen_emit_byte(LOCAL0_OP);
1391 acpigen_write_uuid(id->uuid);
1392
1393 /* ToInteger (Arg2, Local1) */
1394 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1395
1396 for (i = 0; i < id->count; i++) {
1397 /* If (LEqual (Local1, i)) */
1398 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1399
1400 /* Callback to write if handler. */
1401 if (id->callbacks[i])
1402 id->callbacks[i](id->arg);
1403
1404 acpigen_pop_len(); /* If */
1405 }
1406
1407 /* Default case: Return (Buffer (One) { 0x0 }) */
1408 acpigen_write_return_singleton_buffer(0x0);
1409
1410 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1411
1412}
1413
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001414/*
1415 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301416 * This function takes as input array of uuid for the device, set of callbacks
1417 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1418 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1419 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001420 *
1421 * Arguments passed into _DSM method:
1422 * Arg0 = UUID
1423 * Arg1 = Revision
1424 * Arg2 = Function index
1425 * Arg3 = Function specific arguments
1426 *
1427 * AML code generated would look like:
1428 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301429 * ToBuffer (Arg0, Local0)
1430 * If (LEqual (Local0, ToUUID(uuid))) {
1431 * ToInteger (Arg2, Local1)
1432 * If (LEqual (Local1, 0)) {
1433 * <acpigen by callback[0]>
1434 * }
1435 * ...
1436 * If (LEqual (Local1, n)) {
1437 * <acpigen by callback[n]>
1438 * }
1439 * Return (Buffer (One) { 0x0 })
1440 * }
1441 * ...
1442 * If (LEqual (Local0, ToUUID(uuidn))) {
1443 * ...
1444 * }
1445 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001446 * }
1447 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301448void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001449{
1450 size_t i;
1451
1452 /* Method (_DSM, 4, Serialized) */
1453 acpigen_write_method_serialized("_DSM", 0x4);
1454
1455 /* ToBuffer (Arg0, Local0) */
1456 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1457
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001458 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301459 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001460
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301461 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001462 acpigen_write_return_singleton_buffer(0x0);
1463
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001464 acpigen_pop_len(); /* Method _DSM */
1465}
1466
Matt Delcob425bc82018-08-13 13:36:27 -07001467#define CPPC_PACKAGE_NAME "\\GCPC"
1468
1469void acpigen_write_CPPC_package(const struct cppc_config *config)
1470{
1471 u32 i;
1472 u32 max;
1473 switch (config->version) {
1474 case 1:
1475 max = CPPC_MAX_FIELDS_VER_1;
1476 break;
1477 case 2:
1478 max = CPPC_MAX_FIELDS_VER_2;
1479 break;
1480 case 3:
1481 max = CPPC_MAX_FIELDS_VER_3;
1482 break;
1483 default:
1484 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1485 config->version);
1486 return;
1487 }
1488 acpigen_write_name(CPPC_PACKAGE_NAME);
1489
1490 /* Adding 2 to account for length and version fields */
1491 acpigen_write_package(max + 2);
1492 acpigen_write_dword(max + 2);
1493
1494 acpigen_write_byte(config->version);
1495
1496 for (i = 0; i < max; ++i) {
1497 const acpi_addr_t *reg = &(config->regs[i]);
1498 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
1499 reg->bit_width == 32 && reg->access_size == 0) {
1500 acpigen_write_dword(reg->addrl);
1501 } else {
1502 acpigen_write_register_resource(reg);
1503 }
1504 }
1505 acpigen_pop_len();
1506}
1507
1508void acpigen_write_CPPC_method(void)
1509{
1510 acpigen_write_method("_CPC", 0);
1511 acpigen_emit_byte(RETURN_OP);
1512 acpigen_emit_namestring(CPPC_PACKAGE_NAME);
1513 acpigen_pop_len();
1514}
1515
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001516/*
1517 * Generate ACPI AML code for _ROM method.
1518 * This function takes as input ROM data and ROM length.
1519 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001520 * The ACPI spec isn't clear about what should happen at the end of the
1521 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1522 * bytes in the returned buffer with zeros.
1523 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001524 * Arguments passed into _DSM method:
1525 * Arg0 = Offset in Bytes
1526 * Arg1 = Bytes to return
1527 *
1528 * Example:
1529 * acpigen_write_rom(0xdeadbeef, 0x10000)
1530 *
1531 * AML code generated would look like:
1532 * Method (_ROM, 2, NotSerialized) {
1533 *
1534 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1535 * Field (ROMS, AnyAcc, NoLock, Preserve)
1536 * {
1537 * Offset (0),
1538 * RBF0, 0x80000
1539 * }
1540 *
1541 * Store (Arg0, Local0)
1542 * Store (Arg1, Local1)
1543 *
1544 * If (LGreater (Local1, 0x1000))
1545 * {
1546 * Store (0x1000, Local1)
1547 * }
1548 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001549 * Store (Local1, Local3)
1550 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001551 * If (LGreater (Local0, 0x10000))
1552 * {
1553 * Return(Buffer(Local1){0})
1554 * }
1555 *
1556 * If (LGreater (Local0, 0x0f000))
1557 * {
1558 * Subtract (0x10000, Local0, Local2)
1559 * If (LGreater (Local1, Local2))
1560 * {
1561 * Store (Local2, Local1)
1562 * }
1563 * }
1564 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001565 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001566 *
1567 * Multiply (Local0, 0x08, Local0)
1568 * Multiply (Local1, 0x08, Local1)
1569 *
1570 * CreateField (RBF0, Local0, Local1, TMPB)
1571 * Store (TMPB, ROM1)
1572 * Return (ROM1)
1573 * }
1574 */
1575
1576void acpigen_write_rom(void *bios, const size_t length)
1577{
1578 ASSERT(bios)
1579 ASSERT(length)
1580
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001581 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001582 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001583
1584 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1585 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1586 (uintptr_t)bios, length);
1587 acpigen_write_opregion(&opreg);
1588
1589 struct fieldlist l[] = {
1590 FIELDLIST_OFFSET(0),
1591 FIELDLIST_NAMESTR("RBF0", 8 * length),
1592 };
1593
1594 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1595 * {
1596 * Offset (0),
1597 * RBF0, 0x80000
1598 * } */
1599 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1600 FIELD_NOLOCK | FIELD_PRESERVE);
1601
1602 /* Store (Arg0, Local0) */
1603 acpigen_write_store();
1604 acpigen_emit_byte(ARG0_OP);
1605 acpigen_emit_byte(LOCAL0_OP);
1606
1607 /* Store (Arg1, Local1) */
1608 acpigen_write_store();
1609 acpigen_emit_byte(ARG1_OP);
1610 acpigen_emit_byte(LOCAL1_OP);
1611
1612 /* ACPI SPEC requires to return at maximum 4KiB */
1613 /* If (LGreater (Local1, 0x1000)) */
1614 acpigen_write_if();
1615 acpigen_emit_byte(LGREATER_OP);
1616 acpigen_emit_byte(LOCAL1_OP);
1617 acpigen_write_integer(0x1000);
1618
1619 /* Store (0x1000, Local1) */
1620 acpigen_write_store();
1621 acpigen_write_integer(0x1000);
1622 acpigen_emit_byte(LOCAL1_OP);
1623
1624 /* Pop if */
1625 acpigen_pop_len();
1626
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001627 /* Store (Local1, Local3) */
1628 acpigen_write_store();
1629 acpigen_emit_byte(LOCAL1_OP);
1630 acpigen_emit_byte(LOCAL3_OP);
1631
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001632 /* If (LGreater (Local0, length)) */
1633 acpigen_write_if();
1634 acpigen_emit_byte(LGREATER_OP);
1635 acpigen_emit_byte(LOCAL0_OP);
1636 acpigen_write_integer(length);
1637
1638 /* Return(Buffer(Local1){0}) */
1639 acpigen_emit_byte(RETURN_OP);
1640 acpigen_emit_byte(BUFFER_OP);
1641 acpigen_write_len_f();
1642 acpigen_emit_byte(LOCAL1_OP);
1643 acpigen_emit_byte(0);
1644 acpigen_pop_len();
1645
1646 /* Pop if */
1647 acpigen_pop_len();
1648
1649 /* If (LGreater (Local0, length - 4096)) */
1650 acpigen_write_if();
1651 acpigen_emit_byte(LGREATER_OP);
1652 acpigen_emit_byte(LOCAL0_OP);
1653 acpigen_write_integer(length - 4096);
1654
1655 /* Subtract (length, Local0, Local2) */
1656 acpigen_emit_byte(SUBTRACT_OP);
1657 acpigen_write_integer(length);
1658 acpigen_emit_byte(LOCAL0_OP);
1659 acpigen_emit_byte(LOCAL2_OP);
1660
1661 /* If (LGreater (Local1, Local2)) */
1662 acpigen_write_if();
1663 acpigen_emit_byte(LGREATER_OP);
1664 acpigen_emit_byte(LOCAL1_OP);
1665 acpigen_emit_byte(LOCAL2_OP);
1666
1667 /* Store (Local2, Local1) */
1668 acpigen_write_store();
1669 acpigen_emit_byte(LOCAL2_OP);
1670 acpigen_emit_byte(LOCAL1_OP);
1671
1672 /* Pop if */
1673 acpigen_pop_len();
1674
1675 /* Pop if */
1676 acpigen_pop_len();
1677
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001678 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001679 acpigen_write_name("ROM1");
1680 acpigen_emit_byte(BUFFER_OP);
1681 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001682 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001683 acpigen_emit_byte(0);
1684 acpigen_pop_len();
1685
1686 /* Multiply (Local1, 0x08, Local1) */
1687 acpigen_emit_byte(MULTIPLY_OP);
1688 acpigen_emit_byte(LOCAL1_OP);
1689 acpigen_write_integer(0x08);
1690 acpigen_emit_byte(LOCAL1_OP);
1691
1692 /* Multiply (Local0, 0x08, Local0) */
1693 acpigen_emit_byte(MULTIPLY_OP);
1694 acpigen_emit_byte(LOCAL0_OP);
1695 acpigen_write_integer(0x08);
1696 acpigen_emit_byte(LOCAL0_OP);
1697
1698 /* CreateField (RBF0, Local0, Local1, TMPB) */
1699 acpigen_emit_ext_op(CREATEFIELD_OP);
1700 acpigen_emit_namestring("RBF0");
1701 acpigen_emit_byte(LOCAL0_OP);
1702 acpigen_emit_byte(LOCAL1_OP);
1703 acpigen_emit_namestring("TMPB");
1704
1705 /* Store (TMPB, ROM1) */
1706 acpigen_write_store();
1707 acpigen_emit_namestring("TMPB");
1708 acpigen_emit_namestring("ROM1");
1709
1710 /* Return (ROM1) */
1711 acpigen_emit_byte(RETURN_OP);
1712 acpigen_emit_namestring("ROM1");
1713
1714 /* Pop method */
1715 acpigen_pop_len();
1716}
1717
1718
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001719/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001720int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001721{
1722 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1723 acpigen_write_debug_string("read_rx_gpio not available");
1724 return -1;
1725}
1726
Aaron Durbin64031672018-04-21 14:45:32 -06001727int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001728{
1729 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1730 acpigen_write_debug_string("get_tx_gpio not available");
1731 return -1;
1732}
1733
Aaron Durbin64031672018-04-21 14:45:32 -06001734int __weak acpigen_soc_set_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001735{
1736 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1737 acpigen_write_debug_string("set_tx_gpio not available");
1738 return -1;
1739}
1740
Aaron Durbin64031672018-04-21 14:45:32 -06001741int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001742{
1743 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1744 acpigen_write_debug_string("clear_tx_gpio not available");
1745 return -1;
1746}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001747
1748/*
1749 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1750 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1751 * make callbacks into SoC acpigen code.
1752 *
1753 * Returns 0 on success and -1 on error.
1754 */
1755int acpigen_enable_tx_gpio(struct acpi_gpio *gpio)
1756{
1757 if (gpio->polarity == ACPI_GPIO_ACTIVE_HIGH)
1758 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1759 else
1760 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1761}
1762
1763int acpigen_disable_tx_gpio(struct acpi_gpio *gpio)
1764{
1765 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1766 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1767 else
1768 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1769}
Jonathan Zhang71299c22020-01-27 11:38:57 -08001770
Rajat Jain310623b2020-02-26 11:02:53 -08001771void acpigen_get_rx_gpio(struct acpi_gpio *gpio)
1772{
1773 acpigen_soc_read_rx_gpio(gpio->pins[0]);
1774
1775 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1776 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1777}
1778
Jonathan Zhang71299c22020-01-27 11:38:57 -08001779/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
1780void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran,
1781 u16 range_min, u16 range_max, u16 translation, u16 length)
1782{
1783 acpigen_emit_byte(0x88);
1784 /* Byte 1+2: length (0x000d) */
1785 acpigen_emit_byte(0x0d);
1786 acpigen_emit_byte(0x00);
1787 /* resource type */
1788 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1789 /* general flags */
1790 acpigen_emit_byte(gen_flags);
1791 /* type flags */
1792 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1793 acpigen_emit_byte(type_flags);
1794 /* granularity, min, max, translation, length */
1795 acpigen_emit_word(gran);
1796 acpigen_emit_word(range_min);
1797 acpigen_emit_word(range_max);
1798 acpigen_emit_word(translation);
1799 acpigen_emit_word(length);
1800}
1801
1802/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
1803void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags,
1804 u32 gran, u32 range_min, u32 range_max, u32 translation, u32 length)
1805{
1806 acpigen_emit_byte(0x87);
1807 /* Byte 1+2: length (0023) */
1808 acpigen_emit_byte(23);
1809 acpigen_emit_byte(0x00);
1810 /* resource type */
1811 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1812 /* general flags */
1813 acpigen_emit_byte(gen_flags);
1814 /* type flags */
1815 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1816 acpigen_emit_byte(type_flags);
1817 /* granularity, min, max, translation, length */
1818 acpigen_emit_dword(gran);
1819 acpigen_emit_dword(range_min);
1820 acpigen_emit_dword(range_max);
1821 acpigen_emit_dword(translation);
1822 acpigen_emit_dword(length);
1823}
1824
1825static void acpigen_emit_qword(u64 data)
1826{
1827 acpigen_emit_dword(data & 0xffffffff);
1828 acpigen_emit_dword((data >> 32) & 0xffffffff);
1829}
1830
1831/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
1832void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags,
1833 u64 gran, u64 range_min, u64 range_max, u64 translation, u64 length)
1834{
1835 acpigen_emit_byte(0x8a);
1836 /* Byte 1+2: length (0x002b) */
1837 acpigen_emit_byte(0x2b);
1838 acpigen_emit_byte(0x00);
1839 /* resource type */
1840 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1841 /* general flags */
1842 acpigen_emit_byte(gen_flags);
1843 /* type flags */
1844 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1845 acpigen_emit_byte(type_flags);
1846 /* granularity, min, max, translation, length */
1847 acpigen_emit_qword(gran);
1848 acpigen_emit_qword(range_min);
1849 acpigen_emit_qword(range_max);
1850 acpigen_emit_qword(translation);
1851 acpigen_emit_qword(length);
1852}