blob: fc6cc66f8fe6ce9b48491a4290bee00ee9256c98 [file] [log] [blame]
Rudolf Marek293b5f52009-02-01 18:35:15 +00001/*
2 * This file is part of the coreboot project.
3 *
Lee Leahy6f80ccc2017-03-16 15:18:22 -07004 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>,
5 * Raptor Engineering
Rudolf Marek293b5f52009-02-01 18:35:15 +00006 * Copyright (C) 2009 Rudolf Marek <r.marek@assembler.cz>
7 *
8 * This program is free software; you can redistribute it and/or modify
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
Rudolf Marek293b5f52009-02-01 18:35:15 +000011 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Rudolf Marek293b5f52009-02-01 18:35:15 +000016 */
17
Paul Menzel0f4c0e22013-02-22 12:33:08 +010018/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +000019#define ACPIGEN_LENSTACK_SIZE 10
20
Paul Menzel0f4c0e22013-02-22 12:33:08 +010021/*
Timothy Pearson83abd812015-06-08 19:35:06 -050022 * If you need to change this, change acpigen_write_len_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +010023 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +010024 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000025
Timothy Pearson83abd812015-06-08 19:35:06 -050026#define ACPIGEN_MAXLEN 0xfffff
Rudolf Marek293b5f52009-02-01 18:35:15 +000027
Duncan Laurie3829f232016-05-09 11:08:46 -070028#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000029#include <string.h>
30#include <arch/acpigen.h>
31#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000032#include <device/device.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000033
34static char *gencurrent;
35
36char *len_stack[ACPIGEN_LENSTACK_SIZE];
37int ltop = 0;
38
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010039void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000040{
41 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010042 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000043 acpigen_emit_byte(0);
44 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050045 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000046}
47
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010048void acpigen_pop_len(void)
49{
50 int len;
51 ASSERT(ltop > 0)
52 char *p = len_stack[--ltop];
53 len = gencurrent - p;
54 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050055 /* generate store length for 0xfffff max */
56 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010057 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050058 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010059
60}
61
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000062void acpigen_set_current(char *curr)
63{
64 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000065}
66
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000067char *acpigen_get_current(void)
68{
69 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000070}
71
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010072void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000073{
74 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000075}
76
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070077void acpigen_emit_ext_op(uint8_t op)
78{
79 acpigen_emit_byte(EXT_OP_PREFIX);
80 acpigen_emit_byte(op);
81}
82
Duncan Laurie9ccae752016-05-09 07:43:19 -070083void acpigen_emit_word(unsigned int data)
84{
85 acpigen_emit_byte(data & 0xff);
86 acpigen_emit_byte((data >> 8) & 0xff);
87}
88
89void acpigen_emit_dword(unsigned int data)
90{
91 acpigen_emit_byte(data & 0xff);
92 acpigen_emit_byte((data >> 8) & 0xff);
93 acpigen_emit_byte((data >> 16) & 0xff);
94 acpigen_emit_byte((data >> 24) & 0xff);
95}
96
Duncan Laurie85d80272016-07-02 19:53:54 -070097char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000098{
Duncan Laurie85d80272016-07-02 19:53:54 -070099 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700100 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100101 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -0700102 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +0000103 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -0700104 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000105}
106
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100107void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000108{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700109 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000110 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000111}
112
Duncan Laurie9ccae752016-05-09 07:43:19 -0700113void acpigen_write_word(unsigned int data)
114{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700115 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700116 acpigen_emit_word(data);
117}
118
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100119void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000120{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700121 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700122 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000123}
124
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100125void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000126{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700127 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700128 acpigen_emit_dword(data & 0xffffffff);
129 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000130}
131
Duncan Laurief7c38762016-05-09 08:20:38 -0700132void acpigen_write_zero(void)
133{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700134 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700135}
136
137void acpigen_write_one(void)
138{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700139 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700140}
141
142void acpigen_write_ones(void)
143{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700144 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700145}
146
147void acpigen_write_integer(uint64_t data)
148{
149 if (data == 0)
150 acpigen_write_zero();
151 else if (data == 1)
152 acpigen_write_one();
153 else if (data <= 0xff)
154 acpigen_write_byte((unsigned char)data);
155 else if (data <= 0xffff)
156 acpigen_write_word((unsigned int)data);
157 else if (data <= 0xffffffff)
158 acpigen_write_dword((unsigned int)data);
159 else
160 acpigen_write_qword(data);
161}
162
163void acpigen_write_name_zero(const char *name)
164{
165 acpigen_write_name(name);
166 acpigen_write_one();
167}
168
169void acpigen_write_name_one(const char *name)
170{
171 acpigen_write_name(name);
172 acpigen_write_zero();
173}
174
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100175void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000176{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100177 acpigen_write_name(name);
178 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000179}
180
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100181void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000182{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100183 acpigen_write_name(name);
184 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000185}
186
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100187void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000188{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100189 acpigen_write_name(name);
190 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000191}
192
Duncan Laurief7c38762016-05-09 08:20:38 -0700193void acpigen_write_name_integer(const char *name, uint64_t val)
194{
195 acpigen_write_name(name);
196 acpigen_write_integer(val);
197}
198
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700199void acpigen_write_name_string(const char *name, const char *string)
200{
201 acpigen_write_name(name);
202 acpigen_write_string(string);
203}
204
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100205void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000206{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000207 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700208 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000209 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000210}
211
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700212void acpigen_emit_string(const char *string)
213{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200214 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700215 acpigen_emit_byte('\0'); /* NUL */
216}
217
218void acpigen_write_string(const char *string)
219{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700220 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700221 acpigen_emit_string(string);
222}
223
Duncan Laurie37319032016-05-26 12:47:05 -0700224void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
225{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800226 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700227
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700228 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700229 acpigen_write_name_string("_HID", hid);
230}
231
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100232/*
233 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600234 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
235 * and "By convention, when an ASL compiler pads a name shorter than 4
236 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100237 *
238 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
239 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000240
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700241static void acpigen_emit_simple_namestring(const char *name)
242{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100243 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000244 char ud[] = "____";
245 for (i = 0; i < 4; i++) {
246 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100247 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000248 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000249 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700250 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000251 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000252}
253
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700254static void acpigen_emit_double_namestring(const char *name, int dotpos)
255{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700256 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100257 acpigen_emit_simple_namestring(name);
258 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000259}
260
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700261static void acpigen_emit_multi_namestring(const char *name)
262{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100263 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000264 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700265 acpigen_emit_byte(MULTI_NAME_PREFIX);
266 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000267 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
268
269 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100270 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000271 /* find end or next entity */
272 while ((name[0] != '.') && (name[0] != '\0'))
273 name++;
274 /* forward to next */
275 if (name[0] == '.')
276 name++;
277 count++;
278 }
279
280 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000281}
282
283
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700284void acpigen_emit_namestring(const char *namepath)
285{
Rudolf Marek3310d752009-06-21 20:26:13 +0000286 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000287 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000288
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600289 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000290 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100291 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000292 namepath++;
293 }
294
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600295 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000296 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100297 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000298 namepath++;
299 }
300
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200301 /* If we have only \\ or only ^...^. Then we need to put a null
302 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200303 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700304 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100305 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200306 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000307
308 i = 0;
309 while (namepath[i] != '\0') {
310 if (namepath[i] == '.') {
311 dotcount++;
312 dotpos = i;
313 }
314 i++;
315 }
316
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700317 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100318 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700319 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100320 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700321 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100322 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000323}
324
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100325void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000326{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700327 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100328 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000329}
330
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100331void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000332{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700333 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100334 acpigen_write_len_f();
335 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000336}
Rudolf Marekf997b552009-02-14 15:40:23 +0000337
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100338void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000339{
340/*
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600341 Processor (\_PR.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200342 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000343*/
344 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700345 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100346 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000347
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200348 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600349 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100350 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000351 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700352 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000353 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000354}
355
Nico Huber4d211ac2017-09-01 21:14:36 +0200356void acpigen_write_processor_package(const char *const name,
357 const unsigned int first_core,
358 const unsigned int core_count)
359{
360 unsigned int i;
361 char pscope[16];
362
363 acpigen_write_name(name);
364 acpigen_write_package(core_count);
365 for (i = first_core; i < first_core + core_count; ++i) {
366 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
367 acpigen_emit_namestring(pscope);
368 }
369 acpigen_pop_len();
370}
371
Naresh G Solanki8535c662016-10-25 00:51:24 +0530372/*
373 * Generate ACPI AML code for OperationRegion
374 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
375 * where rname is region name, space is region space, offset is region offset &
376 * len is region length.
377 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
378 */
379void acpigen_write_opregion(struct opregion *opreg)
380{
381 /* OpregionOp */
382 acpigen_emit_ext_op(OPREGION_OP);
383 /* NameString 4 chars only */
384 acpigen_emit_simple_namestring(opreg->name);
385 /* RegionSpace */
386 acpigen_emit_byte(opreg->regionspace);
387 /* RegionOffset & RegionLen, it can be byte word or double word */
388 acpigen_write_integer(opreg->regionoffset);
389 acpigen_write_integer(opreg->regionlen);
390}
391
Patrick Rudolph894977b2017-07-01 16:15:05 +0200392static void acpigen_write_field_length(uint32_t len)
393{
394 uint8_t i, j;
395 uint8_t emit[4];
396
397 i = 1;
398 if (len < 0x40) {
399 emit[0] = len & 0x3F;
400 } else {
401 emit[0] = len & 0xF;
402 len >>= 4;
403 while (len) {
404 emit[i] = len & 0xFF;
405 i++;
406 len >>= 8;
407 }
408 }
409 /* Update bit 7:6 : Number of bytes followed by emit[0] */
410 emit[0] |= (i - 1) << 6;
411
412 for (j = 0; j < i; j++)
413 acpigen_emit_byte(emit[j]);
414}
415
Naresh G Solanki8535c662016-10-25 00:51:24 +0530416static void acpigen_write_field_offset(uint32_t offset,
417 uint32_t current_bit_pos)
418{
419 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530420
421 if (offset < current_bit_pos) {
422 printk(BIOS_WARNING, "%s: Cannot move offset backward",
423 __func__);
424 return;
425 }
426
427 diff_bits = offset - current_bit_pos;
428 /* Upper limit */
429 if (diff_bits > 0xFFFFFFF) {
430 printk(BIOS_WARNING, "%s: Offset very large to encode",
431 __func__);
432 return;
433 }
434
Naresh G Solanki8535c662016-10-25 00:51:24 +0530435 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200436 acpigen_write_field_length(diff_bits);
437}
438
439static void acpigen_write_field_name(const char *name, uint32_t size)
440{
441 acpigen_emit_simple_namestring(name);
442 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530443}
444
445/*
446 * Generate ACPI AML code for Field
447 * Arg0: region name
448 * Arg1: Pointer to struct fieldlist.
449 * Arg2: no. of entries in Arg1
450 * Arg3: flags which indicate filed access type, lock rule & update rule.
451 * Example with fieldlist
452 * struct fieldlist l[] = {
453 * FIELDLIST_OFFSET(0x84),
454 * FIELDLIST_NAMESTR("PMCS", 2),
455 * };
456 * acpigen_write_field("UART", l ,ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
457 * FIELD_PRESERVE);
458 * Output:
459 * Field (UART, AnyAcc, NoLock, Preserve)
460 * {
461 * Offset (0x84),
462 * PMCS, 2
463 * }
464 */
465void acpigen_write_field(const char *name, struct fieldlist *l, size_t count,
466 uint8_t flags)
467{
468 uint16_t i;
469 uint32_t current_bit_pos = 0;
470
471 /* FieldOp */
472 acpigen_emit_ext_op(FIELD_OP);
473 /* Package Length */
474 acpigen_write_len_f();
475 /* NameString 4 chars only */
476 acpigen_emit_simple_namestring(name);
477 /* Field Flag */
478 acpigen_emit_byte(flags);
479
480 for (i = 0; i < count; i++) {
481 switch (l[i].type) {
482 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200483 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530484 current_bit_pos += l[i].bits;
485 break;
486 case OFFSET:
487 acpigen_write_field_offset(l[i].bits, current_bit_pos);
488 current_bit_pos = l[i].bits;
489 break;
490 default:
491 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
492 , __func__, l[i].type);
493 break;
494 }
495 }
496 acpigen_pop_len();
497}
498
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100499void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000500{
501/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200502 Name (_PCT, Package (0x02)
503 {
504 ResourceTemplate ()
505 {
506 Register (FFixedHW,
507 0x00, // Bit Width
508 0x00, // Bit Offset
509 0x0000000000000000, // Address
510 ,)
511 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000512
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200513 ResourceTemplate ()
514 {
515 Register (FFixedHW,
516 0x00, // Bit Width
517 0x00, // Bit Offset
518 0x0000000000000000, // Address
519 ,)
520 }
521 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000522*/
523 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700524 /* 00000030 "0._PCT.," */
525 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
526 /* 00000038 "........" */
527 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
528 /* 00000040 "........" */
529 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
530 /* 00000048 "....y..." */
531 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
532 /* 00000050 "........" */
533 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
534 /* 00000058 "........" */
535 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000536 0x00, 0x79, 0x00
537 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100538 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000539}
540
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100541void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200542{
543/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200544 Name (_PTC, Package (0x02)
545 {
546 ResourceTemplate ()
547 {
548 Register (FFixedHW,
549 0x00, // Bit Width
550 0x00, // Bit Offset
551 0x0000000000000000, // Address
552 ,)
553 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200554
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200555 ResourceTemplate ()
556 {
557 Register (FFixedHW,
558 0x00, // Bit Width
559 0x00, // Bit Offset
560 0x0000000000000000, // Address
561 ,)
562 }
563 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200564*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200565 acpi_addr_t addr = {
566 .space_id = ACPI_ADDRESS_SPACE_FIXED,
567 .bit_width = 0,
568 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800569 {
570 .resv = 0
571 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200572 .addrl = 0,
573 .addrh = 0,
574 };
575
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100576 acpigen_write_name("_PTC");
577 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200578
579 /* ControlRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100580 acpigen_write_resourcetemplate_header();
581 acpigen_write_register(&addr);
582 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200583
584 /* StatusRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100585 acpigen_write_resourcetemplate_header();
586 acpigen_write_register(&addr);
587 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200588
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100589 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200590}
591
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700592static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100593{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700594 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100595 acpigen_write_len_f();
596 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700597 acpigen_emit_byte(flags);
598}
599
600/* Method (name, nargs, NotSerialized) */
601void acpigen_write_method(const char *name, int nargs)
602{
603 __acpigen_write_method(name, (nargs & 7));
604}
605
606/* Method (name, nargs, Serialized) */
607void acpigen_write_method_serialized(const char *name, int nargs)
608{
609 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100610}
611
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100612void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100613{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700614 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100615 acpigen_write_len_f();
616 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100617}
618
Duncan Laurieabe2de82016-05-09 11:08:46 -0700619void acpigen_write_STA(uint8_t status)
620{
621 /*
622 * Method (_STA, 0, NotSerialized) { Return (status) }
623 */
624 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700625 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700626 acpigen_write_byte(status);
627 acpigen_pop_len();
628}
629
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100630/*
631 * Generates a func with max supported P-states.
632 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100633void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000634{
635/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200636 Method (_PPC, 0, NotSerialized)
637 {
638 Return (nr)
639 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000640*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100641 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700642 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000643 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100644 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100645 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000646}
647
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100648/*
649 * Generates a func with max supported P-states saved
650 * in the variable PPCM.
651 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100652void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700653{
654/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200655 Method (_PPC, 0, NotSerialized)
656 {
657 Return (PPCM)
658 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700659*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100660 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700661 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700662 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100663 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100664 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700665}
666
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100667void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200668{
669/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200670 // Sample _TPC method
671 Method (_TPC, 0, NotSerialized)
672 {
673 Return (\TLVL)
674 }
675*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100676 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700677 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100678 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100679 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200680}
681
Duncan Laurieabe2de82016-05-09 11:08:46 -0700682void acpigen_write_PRW(u32 wake, u32 level)
683{
684 /*
685 * Name (_PRW, Package () { wake, level }
686 */
687 acpigen_write_name("_PRW");
688 acpigen_write_package(2);
689 acpigen_write_integer(wake);
690 acpigen_write_integer(level);
691 acpigen_pop_len();
692}
693
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100694void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000695 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000696{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100697 acpigen_write_package(6);
698 acpigen_write_dword(coreFreq);
699 acpigen_write_dword(power);
700 acpigen_write_dword(transLat);
701 acpigen_write_dword(busmLat);
702 acpigen_write_dword(control);
703 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100704 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700705
706 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
707 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000708}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000709
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100710void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000711{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100712 acpigen_write_name("_PSD");
713 acpigen_write_package(1);
714 acpigen_write_package(5);
715 acpigen_write_byte(5); // 5 values
716 acpigen_write_byte(0); // revision 0
717 acpigen_write_dword(domain);
718 acpigen_write_dword(coordtype);
719 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100720 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100721 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000722}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000723
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100724void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200725{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100726 acpigen_write_package(4);
727 acpigen_write_resourcetemplate_header();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200728 acpigen_write_register(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100729 acpigen_write_resourcetemplate_footer();
730 acpigen_write_dword(cstate->ctype);
731 acpigen_write_dword(cstate->latency);
732 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100733 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200734}
735
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100736void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200737{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100738 int i;
739 acpigen_write_name("_CST");
740 acpigen_write_package(nentries+1);
741 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200742
743 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100744 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200745
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100746 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200747}
748
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700749void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
750 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500751{
752 acpigen_write_name("_CSD");
753 acpigen_write_package(1);
754 acpigen_write_package(6);
755 acpigen_write_byte(6); // 6 values
756 acpigen_write_byte(0); // revision 0
757 acpigen_write_dword(domain);
758 acpigen_write_dword(coordtype);
759 acpigen_write_dword(numprocs);
760 acpigen_write_dword(index);
761 acpigen_pop_len();
762 acpigen_pop_len();
763}
764
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100765void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200766{
767/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200768 Sample _TSS package with 100% and 50% duty cycles
769 Name (_TSS, Package (0x02)
770 {
771 Package(){100, 1000, 0, 0x00, 0)
772 Package(){50, 520, 0, 0x18, 0)
773 })
774*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100775 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200776 acpi_tstate_t *tstate = tstate_list;
777
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100778 acpigen_write_name("_TSS");
779 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200780
781 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100782 acpigen_write_package(5);
783 acpigen_write_dword(tstate->percent);
784 acpigen_write_dword(tstate->power);
785 acpigen_write_dword(tstate->latency);
786 acpigen_write_dword(tstate->control);
787 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100788 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200789 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200790 }
791
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100792 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200793}
794
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100795void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200796{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100797 acpigen_write_name("_TSD");
798 acpigen_write_package(1);
799 acpigen_write_package(5);
800 acpigen_write_byte(5); // 5 values
801 acpigen_write_byte(0); // revision 0
802 acpigen_write_dword(domain);
803 acpigen_write_dword(coordtype);
804 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100805 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100806 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200807}
808
809
810
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100811void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000812{
813 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200814 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000815 * Byte 0:
816 * Bit7 : 1 => big item
817 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
818 */
819 acpigen_emit_byte(0x86);
820 /* Byte 1+2: length (0x0009) */
821 acpigen_emit_byte(0x09);
822 acpigen_emit_byte(0x00);
823 /* bit1-7 are ignored */
824 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700825 acpigen_emit_dword(base);
826 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000827}
828
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100829void acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200830{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200831 acpigen_emit_byte(0x82); /* Register Descriptor */
832 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
833 acpigen_emit_byte(0x00); /* Register Length 15:8 */
834 acpigen_emit_byte(addr->space_id); /* Address Space ID */
835 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
836 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
837 acpigen_emit_byte(addr->resv); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700838 acpigen_emit_dword(addr->addrl); /* Register Address Low */
839 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200840}
841
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100842void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100843{
844 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200845 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100846 * Byte 0:
847 * Bit7 : 0 => small item
848 * Bit6-3: 0100 (0x4) => IRQ port descriptor
849 * Bit2-0: 010 (0x2) => 2 Bytes long
850 */
851 acpigen_emit_byte(0x22);
852 acpigen_emit_byte(mask & 0xff);
853 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100854}
855
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100856void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000857{
858 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200859 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000860 * Byte 0:
861 * Bit7 : 0 => small item
862 * Bit6-3: 1000 (0x8) => I/O port descriptor
863 * Bit2-0: 111 (0x7) => 7 Bytes long
864 */
865 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100866 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000867 /* bit1-7 are ignored */
868 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
869 /* minimum base address the device may be configured for */
870 acpigen_emit_byte(min & 0xff);
871 acpigen_emit_byte((min >> 8) & 0xff);
872 /* maximum base address the device may be configured for */
873 acpigen_emit_byte(max & 0xff);
874 acpigen_emit_byte((max >> 8) & 0xff);
875 /* alignment for min base */
876 acpigen_emit_byte(align & 0xff);
877 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000878}
879
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100880void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000881{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000882 /*
883 * A ResourceTemplate() is a Buffer() with a
884 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100885 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000886 * (small item 0xf, len 1)
887 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700888 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100889 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700890 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000891 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +0200892 /* Add 2 dummy bytes for the ACPI word (keep aligned with
893 the calclulation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100894 acpigen_emit_byte(0x00);
895 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000896}
897
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100898void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000899{
900 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100901 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000902 /*
903 * end tag (acpi 4.0 Section 6.4.2.8)
904 * 0x79 <checksum>
905 * 0x00 is treated as a good checksum according to the spec
906 * and is what iasl generates.
907 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100908 acpigen_emit_byte(0x79);
909 acpigen_emit_byte(0x00);
910
Nico Huber7d89ce32017-04-14 00:08:18 +0200911 /* Start counting past the 2-bytes length added in
912 acpigen_write_resourcetemplate() above. */
913 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100914
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000915 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100916 p[0] = len & 0xff;
917 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000918 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100919 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000920}
921
922static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
923 struct resource *res)
924{
925 acpigen_write_mem32fixed(0, res->base, res->size);
926}
927
928static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
929 struct resource *res)
930{
931 resource_t base = res->base;
932 resource_t size = res->size;
933 while (size > 0) {
934 resource_t sz = size > 255 ? 255 : size;
935 acpigen_write_io16(base, base, 0, sz, 1);
936 size -= sz;
937 base += sz;
938 }
939}
940
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100941void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000942{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100943 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000944
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100945 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000946 search_global_resources(
947 IORESOURCE_MEM | IORESOURCE_RESERVE,
948 IORESOURCE_MEM | IORESOURCE_RESERVE,
949 acpigen_add_mainboard_rsvd_mem32, 0);
950
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100951 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000952 search_global_resources(
953 IORESOURCE_IO | IORESOURCE_RESERVE,
954 IORESOURCE_IO | IORESOURCE_RESERVE,
955 acpigen_add_mainboard_rsvd_io, 0);
956
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100957 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000958}
959
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100960void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000961{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100962 acpigen_write_scope(scope);
963 acpigen_write_name(name);
964 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100965 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000966}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100967
968static int hex2bin(const char c)
969{
970 if (c >= 'A' && c <= 'F')
971 return c - 'A' + 10;
972 if (c >= 'a' && c <= 'f')
973 return c - 'a' + 10;
974 return c - '0';
975}
976
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100977void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100978{
979 u32 compact = 0;
980
981 /* Clamping individual values would be better but
982 there is a disagreement over what is a valid
983 EISA id, so accept anything and don't clamp,
984 parent code should create a valid EISAid.
985 */
986 compact |= (eisaid[0] - 'A' + 1) << 26;
987 compact |= (eisaid[1] - 'A' + 1) << 21;
988 compact |= (eisaid[2] - 'A' + 1) << 16;
989 compact |= hex2bin(eisaid[3]) << 12;
990 compact |= hex2bin(eisaid[4]) << 8;
991 compact |= hex2bin(eisaid[5]) << 4;
992 compact |= hex2bin(eisaid[6]);
993
994 acpigen_emit_byte(0xc);
995 acpigen_emit_byte((compact >> 24) & 0xff);
996 acpigen_emit_byte((compact >> 16) & 0xff);
997 acpigen_emit_byte((compact >> 8) & 0xff);
998 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100999}
Duncan Laurie3829f232016-05-09 11:08:46 -07001000
1001/*
1002 * ToUUID(uuid)
1003 *
1004 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1005 * order for the bytes that make up a UUID Buffer object.
1006 * UUID byte order for input:
1007 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1008 * UUID byte order for output:
1009 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1010 */
1011#define UUID_LEN 16
1012void acpigen_write_uuid(const char *uuid)
1013{
1014 uint8_t buf[UUID_LEN];
1015 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1016 8, 9, 10, 11, 12, 13, 14, 15 };
1017
1018 /* Parse UUID string into bytes */
1019 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1020 return;
1021
1022 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001023 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001024 acpigen_write_len_f();
1025
1026 /* Buffer length in bytes */
1027 acpigen_write_word(UUID_LEN);
1028
1029 /* Output UUID in expected order */
1030 for (i = 0; i < UUID_LEN; i++)
1031 acpigen_emit_byte(buf[order[i]]);
1032
1033 acpigen_pop_len();
1034}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001035
1036/*
1037 * Name (_PRx, Package(One) { name })
1038 * ...
1039 * PowerResource (name, level, order)
1040 */
1041void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
1042 const char *dev_states[], size_t dev_states_count)
1043{
1044 int i;
1045 for (i = 0; i < dev_states_count; i++) {
1046 acpigen_write_name(dev_states[i]);
1047 acpigen_write_package(1);
1048 acpigen_emit_simple_namestring(name);
1049 acpigen_pop_len(); /* Package */
1050 }
1051
1052 acpigen_emit_ext_op(POWER_RES_OP);
1053
1054 acpigen_write_len_f();
1055
1056 acpigen_emit_simple_namestring(name);
1057 acpigen_emit_byte(level);
1058 acpigen_emit_word(order);
1059}
1060
1061/* Sleep (ms) */
1062void acpigen_write_sleep(uint64_t sleep_ms)
1063{
1064 acpigen_emit_ext_op(SLEEP_OP);
1065 acpigen_write_integer(sleep_ms);
1066}
1067
1068void acpigen_write_store(void)
1069{
1070 acpigen_emit_byte(STORE_OP);
1071}
1072
1073/* Store (src, dst) */
1074void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1075{
1076 acpigen_write_store();
1077 acpigen_emit_byte(src);
1078 acpigen_emit_byte(dst);
1079}
1080
1081/* Or (arg1, arg2, res) */
1082void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1083{
1084 acpigen_emit_byte(OR_OP);
1085 acpigen_emit_byte(arg1);
1086 acpigen_emit_byte(arg2);
1087 acpigen_emit_byte(res);
1088}
1089
1090/* And (arg1, arg2, res) */
1091void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1092{
1093 acpigen_emit_byte(AND_OP);
1094 acpigen_emit_byte(arg1);
1095 acpigen_emit_byte(arg2);
1096 acpigen_emit_byte(res);
1097}
1098
1099/* Not (arg, res) */
1100void acpigen_write_not(uint8_t arg, uint8_t res)
1101{
1102 acpigen_emit_byte(NOT_OP);
1103 acpigen_emit_byte(arg);
1104 acpigen_emit_byte(res);
1105}
1106
1107/* Store (str, DEBUG) */
1108void acpigen_write_debug_string(const char *str)
1109{
1110 acpigen_write_store();
1111 acpigen_write_string(str);
1112 acpigen_emit_ext_op(DEBUG_OP);
1113}
1114
1115/* Store (val, DEBUG) */
1116void acpigen_write_debug_integer(uint64_t val)
1117{
1118 acpigen_write_store();
1119 acpigen_write_integer(val);
1120 acpigen_emit_ext_op(DEBUG_OP);
1121}
1122
1123/* Store (op, DEBUG) */
1124void acpigen_write_debug_op(uint8_t op)
1125{
1126 acpigen_write_store();
1127 acpigen_emit_byte(op);
1128 acpigen_emit_ext_op(DEBUG_OP);
1129}
1130
1131void acpigen_write_if(void)
1132{
1133 acpigen_emit_byte(IF_OP);
1134 acpigen_write_len_f();
1135}
1136
1137/* If (And (arg1, arg2)) */
1138void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1139{
1140 acpigen_write_if();
1141 acpigen_emit_byte(AND_OP);
1142 acpigen_emit_byte(arg1);
1143 acpigen_emit_byte(arg2);
1144}
1145
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001146/*
1147 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1148 * operand1 is ACPI op and operand2 is an integer.
1149 *
1150 * If (Lequal (op, val))
1151 */
1152void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001153{
1154 acpigen_write_if();
1155 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001156 acpigen_emit_byte(op);
1157 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001158}
1159
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001160void acpigen_write_else(void)
1161{
1162 acpigen_emit_byte(ELSE_OP);
1163 acpigen_write_len_f();
1164}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001165
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001166void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1167{
1168 acpigen_emit_byte(TO_BUFFER_OP);
1169 acpigen_emit_byte(src);
1170 acpigen_emit_byte(dst);
1171}
1172
1173void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1174{
1175 acpigen_emit_byte(TO_INTEGER_OP);
1176 acpigen_emit_byte(src);
1177 acpigen_emit_byte(dst);
1178}
1179
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301180void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001181{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301182 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001183
1184 acpigen_emit_byte(BUFFER_OP);
1185 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301186 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001187
1188 for (i = 0; i < size; i++)
1189 acpigen_emit_byte(arr[i]);
1190
1191 acpigen_pop_len();
1192}
1193
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301194void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001195{
1196 acpigen_emit_byte(RETURN_OP);
1197 acpigen_write_byte_buffer(arr, size);
1198}
1199
1200void acpigen_write_return_singleton_buffer(uint8_t arg)
1201{
1202 acpigen_write_return_byte_buffer(&arg, 1);
1203}
1204
1205void acpigen_write_return_byte(uint8_t arg)
1206{
1207 acpigen_emit_byte(RETURN_OP);
1208 acpigen_write_byte(arg);
1209}
1210
Naresh G Solanki05abe432016-11-17 00:16:29 +05301211void acpigen_write_return_integer(uint64_t arg)
1212{
1213 acpigen_emit_byte(RETURN_OP);
1214 acpigen_write_integer(arg);
1215}
1216
1217void acpigen_write_return_string(const char *arg)
1218{
1219 acpigen_emit_byte(RETURN_OP);
1220 acpigen_write_string(arg);
1221}
1222
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301223void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1224 size_t count, void *arg)
1225{
1226 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1227 acpigen_write_dsm_uuid_arr(&id, 1);
1228}
1229
1230static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1231{
1232 size_t i;
1233
1234 /* If (LEqual (Local0, ToUUID(uuid))) */
1235 acpigen_write_if();
1236 acpigen_emit_byte(LEQUAL_OP);
1237 acpigen_emit_byte(LOCAL0_OP);
1238 acpigen_write_uuid(id->uuid);
1239
1240 /* ToInteger (Arg2, Local1) */
1241 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1242
1243 for (i = 0; i < id->count; i++) {
1244 /* If (LEqual (Local1, i)) */
1245 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1246
1247 /* Callback to write if handler. */
1248 if (id->callbacks[i])
1249 id->callbacks[i](id->arg);
1250
1251 acpigen_pop_len(); /* If */
1252 }
1253
1254 /* Default case: Return (Buffer (One) { 0x0 }) */
1255 acpigen_write_return_singleton_buffer(0x0);
1256
1257 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1258
1259}
1260
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001261/*
1262 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301263 * This function takes as input array of uuid for the device, set of callbacks
1264 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1265 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1266 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001267 *
1268 * Arguments passed into _DSM method:
1269 * Arg0 = UUID
1270 * Arg1 = Revision
1271 * Arg2 = Function index
1272 * Arg3 = Function specific arguments
1273 *
1274 * AML code generated would look like:
1275 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301276 * ToBuffer (Arg0, Local0)
1277 * If (LEqual (Local0, ToUUID(uuid))) {
1278 * ToInteger (Arg2, Local1)
1279 * If (LEqual (Local1, 0)) {
1280 * <acpigen by callback[0]>
1281 * }
1282 * ...
1283 * If (LEqual (Local1, n)) {
1284 * <acpigen by callback[n]>
1285 * }
1286 * Return (Buffer (One) { 0x0 })
1287 * }
1288 * ...
1289 * If (LEqual (Local0, ToUUID(uuidn))) {
1290 * ...
1291 * }
1292 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001293 * }
1294 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301295void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001296{
1297 size_t i;
1298
1299 /* Method (_DSM, 4, Serialized) */
1300 acpigen_write_method_serialized("_DSM", 0x4);
1301
1302 /* ToBuffer (Arg0, Local0) */
1303 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1304
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001305 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301306 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001307
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301308 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001309 acpigen_write_return_singleton_buffer(0x0);
1310
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001311 acpigen_pop_len(); /* Method _DSM */
1312}
1313
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001314/*
1315 * Generate ACPI AML code for _ROM method.
1316 * This function takes as input ROM data and ROM length.
1317 *
1318 * Arguments passed into _DSM method:
1319 * Arg0 = Offset in Bytes
1320 * Arg1 = Bytes to return
1321 *
1322 * Example:
1323 * acpigen_write_rom(0xdeadbeef, 0x10000)
1324 *
1325 * AML code generated would look like:
1326 * Method (_ROM, 2, NotSerialized) {
1327 *
1328 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1329 * Field (ROMS, AnyAcc, NoLock, Preserve)
1330 * {
1331 * Offset (0),
1332 * RBF0, 0x80000
1333 * }
1334 *
1335 * Store (Arg0, Local0)
1336 * Store (Arg1, Local1)
1337 *
1338 * If (LGreater (Local1, 0x1000))
1339 * {
1340 * Store (0x1000, Local1)
1341 * }
1342 *
1343 * If (LGreater (Local0, 0x10000))
1344 * {
1345 * Return(Buffer(Local1){0})
1346 * }
1347 *
1348 * If (LGreater (Local0, 0x0f000))
1349 * {
1350 * Subtract (0x10000, Local0, Local2)
1351 * If (LGreater (Local1, Local2))
1352 * {
1353 * Store (Local2, Local1)
1354 * }
1355 * }
1356 *
1357 * Name (ROM1, Buffer (Local1) {0})
1358 *
1359 * Multiply (Local0, 0x08, Local0)
1360 * Multiply (Local1, 0x08, Local1)
1361 *
1362 * CreateField (RBF0, Local0, Local1, TMPB)
1363 * Store (TMPB, ROM1)
1364 * Return (ROM1)
1365 * }
1366 */
1367
1368void acpigen_write_rom(void *bios, const size_t length)
1369{
1370 ASSERT(bios)
1371 ASSERT(length)
1372
1373 /* Method (_ROM, 2, NotSerialized) */
1374 acpigen_write_method("_ROM", 2);
1375
1376 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1377 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1378 (uintptr_t)bios, length);
1379 acpigen_write_opregion(&opreg);
1380
1381 struct fieldlist l[] = {
1382 FIELDLIST_OFFSET(0),
1383 FIELDLIST_NAMESTR("RBF0", 8 * length),
1384 };
1385
1386 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1387 * {
1388 * Offset (0),
1389 * RBF0, 0x80000
1390 * } */
1391 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1392 FIELD_NOLOCK | FIELD_PRESERVE);
1393
1394 /* Store (Arg0, Local0) */
1395 acpigen_write_store();
1396 acpigen_emit_byte(ARG0_OP);
1397 acpigen_emit_byte(LOCAL0_OP);
1398
1399 /* Store (Arg1, Local1) */
1400 acpigen_write_store();
1401 acpigen_emit_byte(ARG1_OP);
1402 acpigen_emit_byte(LOCAL1_OP);
1403
1404 /* ACPI SPEC requires to return at maximum 4KiB */
1405 /* If (LGreater (Local1, 0x1000)) */
1406 acpigen_write_if();
1407 acpigen_emit_byte(LGREATER_OP);
1408 acpigen_emit_byte(LOCAL1_OP);
1409 acpigen_write_integer(0x1000);
1410
1411 /* Store (0x1000, Local1) */
1412 acpigen_write_store();
1413 acpigen_write_integer(0x1000);
1414 acpigen_emit_byte(LOCAL1_OP);
1415
1416 /* Pop if */
1417 acpigen_pop_len();
1418
1419 /* If (LGreater (Local0, length)) */
1420 acpigen_write_if();
1421 acpigen_emit_byte(LGREATER_OP);
1422 acpigen_emit_byte(LOCAL0_OP);
1423 acpigen_write_integer(length);
1424
1425 /* Return(Buffer(Local1){0}) */
1426 acpigen_emit_byte(RETURN_OP);
1427 acpigen_emit_byte(BUFFER_OP);
1428 acpigen_write_len_f();
1429 acpigen_emit_byte(LOCAL1_OP);
1430 acpigen_emit_byte(0);
1431 acpigen_pop_len();
1432
1433 /* Pop if */
1434 acpigen_pop_len();
1435
1436 /* If (LGreater (Local0, length - 4096)) */
1437 acpigen_write_if();
1438 acpigen_emit_byte(LGREATER_OP);
1439 acpigen_emit_byte(LOCAL0_OP);
1440 acpigen_write_integer(length - 4096);
1441
1442 /* Subtract (length, Local0, Local2) */
1443 acpigen_emit_byte(SUBTRACT_OP);
1444 acpigen_write_integer(length);
1445 acpigen_emit_byte(LOCAL0_OP);
1446 acpigen_emit_byte(LOCAL2_OP);
1447
1448 /* If (LGreater (Local1, Local2)) */
1449 acpigen_write_if();
1450 acpigen_emit_byte(LGREATER_OP);
1451 acpigen_emit_byte(LOCAL1_OP);
1452 acpigen_emit_byte(LOCAL2_OP);
1453
1454 /* Store (Local2, Local1) */
1455 acpigen_write_store();
1456 acpigen_emit_byte(LOCAL2_OP);
1457 acpigen_emit_byte(LOCAL1_OP);
1458
1459 /* Pop if */
1460 acpigen_pop_len();
1461
1462 /* Pop if */
1463 acpigen_pop_len();
1464
1465 /* Name (ROM1, Buffer (Local1) {0}) */
1466 acpigen_write_name("ROM1");
1467 acpigen_emit_byte(BUFFER_OP);
1468 acpigen_write_len_f();
1469 acpigen_emit_byte(LOCAL1_OP);
1470 acpigen_emit_byte(0);
1471 acpigen_pop_len();
1472
1473 /* Multiply (Local1, 0x08, Local1) */
1474 acpigen_emit_byte(MULTIPLY_OP);
1475 acpigen_emit_byte(LOCAL1_OP);
1476 acpigen_write_integer(0x08);
1477 acpigen_emit_byte(LOCAL1_OP);
1478
1479 /* Multiply (Local0, 0x08, Local0) */
1480 acpigen_emit_byte(MULTIPLY_OP);
1481 acpigen_emit_byte(LOCAL0_OP);
1482 acpigen_write_integer(0x08);
1483 acpigen_emit_byte(LOCAL0_OP);
1484
1485 /* CreateField (RBF0, Local0, Local1, TMPB) */
1486 acpigen_emit_ext_op(CREATEFIELD_OP);
1487 acpigen_emit_namestring("RBF0");
1488 acpigen_emit_byte(LOCAL0_OP);
1489 acpigen_emit_byte(LOCAL1_OP);
1490 acpigen_emit_namestring("TMPB");
1491
1492 /* Store (TMPB, ROM1) */
1493 acpigen_write_store();
1494 acpigen_emit_namestring("TMPB");
1495 acpigen_emit_namestring("ROM1");
1496
1497 /* Return (ROM1) */
1498 acpigen_emit_byte(RETURN_OP);
1499 acpigen_emit_namestring("ROM1");
1500
1501 /* Pop method */
1502 acpigen_pop_len();
1503}
1504
1505
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001506/* Soc-implemented functions -- weak definitions. */
1507int __attribute__((weak)) acpigen_soc_read_rx_gpio(unsigned int gpio_num)
1508{
1509 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1510 acpigen_write_debug_string("read_rx_gpio not available");
1511 return -1;
1512}
1513
1514int __attribute__((weak)) acpigen_soc_get_tx_gpio(unsigned int gpio_num)
1515{
1516 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1517 acpigen_write_debug_string("get_tx_gpio not available");
1518 return -1;
1519}
1520
1521int __attribute__((weak)) acpigen_soc_set_tx_gpio(unsigned int gpio_num)
1522{
1523 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1524 acpigen_write_debug_string("set_tx_gpio not available");
1525 return -1;
1526}
1527
1528int __attribute__((weak)) acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
1529{
1530 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1531 acpigen_write_debug_string("clear_tx_gpio not available");
1532 return -1;
1533}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001534
1535/*
1536 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1537 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1538 * make callbacks into SoC acpigen code.
1539 *
1540 * Returns 0 on success and -1 on error.
1541 */
1542int acpigen_enable_tx_gpio(struct acpi_gpio *gpio)
1543{
1544 if (gpio->polarity == ACPI_GPIO_ACTIVE_HIGH)
1545 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1546 else
1547 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1548}
1549
1550int acpigen_disable_tx_gpio(struct acpi_gpio *gpio)
1551{
1552 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1553 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1554 else
1555 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1556}