blob: cbd0b5f1316502f45e7afd57192e68fbeb8a7236 [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{
226 char hid[9]; /* CORExxxx */
227
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;
249 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100250 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000251 }
252 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000253}
254
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700255static void acpigen_emit_double_namestring(const char *name, int dotpos)
256{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700257 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100258 acpigen_emit_simple_namestring(name);
259 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000260}
261
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700262static void acpigen_emit_multi_namestring(const char *name)
263{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100264 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000265 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700266 acpigen_emit_byte(MULTI_NAME_PREFIX);
267 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000268 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
269
270 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100271 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000272 /* find end or next entity */
273 while ((name[0] != '.') && (name[0] != '\0'))
274 name++;
275 /* forward to next */
276 if (name[0] == '.')
277 name++;
278 count++;
279 }
280
281 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000282}
283
284
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700285void acpigen_emit_namestring(const char *namepath)
286{
Rudolf Marek3310d752009-06-21 20:26:13 +0000287 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000288 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000289
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600290 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000291 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100292 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000293 namepath++;
294 }
295
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600296 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000297 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100298 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000299 namepath++;
300 }
301
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200302 /* If we have only \\ or only ^...^. Then we need to put a null
303 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200304 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700305 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100306 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200307 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000308
309 i = 0;
310 while (namepath[i] != '\0') {
311 if (namepath[i] == '.') {
312 dotcount++;
313 dotpos = i;
314 }
315 i++;
316 }
317
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700318 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100319 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700320 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100321 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700322 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100323 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000324}
325
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100326void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000327{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700328 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100329 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000330}
331
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100332void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000333{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700334 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100335 acpigen_write_len_f();
336 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000337}
Rudolf Marekf997b552009-02-14 15:40:23 +0000338
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100339void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000340{
341/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200342 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
343 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000344*/
345 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700346 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100347 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000348
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200349 snprintf(pscope, sizeof(pscope),
Timothy Pearson033bb4b2015-02-10 22:21:39 -0600350 "\\_PR.CP%02d", (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100351 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000352 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700353 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000354 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000355}
356
Naresh G Solanki8535c662016-10-25 00:51:24 +0530357/*
358 * Generate ACPI AML code for OperationRegion
359 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
360 * where rname is region name, space is region space, offset is region offset &
361 * len is region length.
362 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
363 */
364void acpigen_write_opregion(struct opregion *opreg)
365{
366 /* OpregionOp */
367 acpigen_emit_ext_op(OPREGION_OP);
368 /* NameString 4 chars only */
369 acpigen_emit_simple_namestring(opreg->name);
370 /* RegionSpace */
371 acpigen_emit_byte(opreg->regionspace);
372 /* RegionOffset & RegionLen, it can be byte word or double word */
373 acpigen_write_integer(opreg->regionoffset);
374 acpigen_write_integer(opreg->regionlen);
375}
376
377static void acpigen_write_field_offset(uint32_t offset,
378 uint32_t current_bit_pos)
379{
380 uint32_t diff_bits;
381 uint8_t i, j;
382 uint8_t emit[4];
383
384 if (offset < current_bit_pos) {
385 printk(BIOS_WARNING, "%s: Cannot move offset backward",
386 __func__);
387 return;
388 }
389
390 diff_bits = offset - current_bit_pos;
391 /* Upper limit */
392 if (diff_bits > 0xFFFFFFF) {
393 printk(BIOS_WARNING, "%s: Offset very large to encode",
394 __func__);
395 return;
396 }
397
398 i = 1;
399 if (diff_bits < 0x40) {
400 emit[0] = diff_bits & 0x3F;
401 } else {
402 emit[0] = diff_bits & 0xF;
403 diff_bits >>= 4;
404 while (diff_bits) {
405 emit[i] = diff_bits & 0xFF;
406 i++;
407 diff_bits >>= 8;
408 }
409 }
410 /* Update bit 7:6 : Number of bytes followed by emit[0] */
411 emit[0] |= (i - 1) << 6;
412
413 acpigen_emit_byte(0);
414 for (j = 0; j < i; j++)
415 acpigen_emit_byte(emit[j]);
416}
417
418/*
419 * Generate ACPI AML code for Field
420 * Arg0: region name
421 * Arg1: Pointer to struct fieldlist.
422 * Arg2: no. of entries in Arg1
423 * Arg3: flags which indicate filed access type, lock rule & update rule.
424 * Example with fieldlist
425 * struct fieldlist l[] = {
426 * FIELDLIST_OFFSET(0x84),
427 * FIELDLIST_NAMESTR("PMCS", 2),
428 * };
429 * acpigen_write_field("UART", l ,ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
430 * FIELD_PRESERVE);
431 * Output:
432 * Field (UART, AnyAcc, NoLock, Preserve)
433 * {
434 * Offset (0x84),
435 * PMCS, 2
436 * }
437 */
438void acpigen_write_field(const char *name, struct fieldlist *l, size_t count,
439 uint8_t flags)
440{
441 uint16_t i;
442 uint32_t current_bit_pos = 0;
443
444 /* FieldOp */
445 acpigen_emit_ext_op(FIELD_OP);
446 /* Package Length */
447 acpigen_write_len_f();
448 /* NameString 4 chars only */
449 acpigen_emit_simple_namestring(name);
450 /* Field Flag */
451 acpigen_emit_byte(flags);
452
453 for (i = 0; i < count; i++) {
454 switch (l[i].type) {
455 case NAME_STRING:
456 acpigen_emit_simple_namestring(l[i].name);
457 acpigen_emit_byte(l[i].bits);
458 current_bit_pos += l[i].bits;
459 break;
460 case OFFSET:
461 acpigen_write_field_offset(l[i].bits, current_bit_pos);
462 current_bit_pos = l[i].bits;
463 break;
464 default:
465 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
466 , __func__, l[i].type);
467 break;
468 }
469 }
470 acpigen_pop_len();
471}
472
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100473void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000474{
475/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200476 Name (_PCT, Package (0x02)
477 {
478 ResourceTemplate ()
479 {
480 Register (FFixedHW,
481 0x00, // Bit Width
482 0x00, // Bit Offset
483 0x0000000000000000, // Address
484 ,)
485 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000486
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200487 ResourceTemplate ()
488 {
489 Register (FFixedHW,
490 0x00, // Bit Width
491 0x00, // Bit Offset
492 0x0000000000000000, // Address
493 ,)
494 }
495 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000496*/
497 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700498 /* 00000030 "0._PCT.," */
499 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
500 /* 00000038 "........" */
501 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
502 /* 00000040 "........" */
503 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
504 /* 00000048 "....y..." */
505 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
506 /* 00000050 "........" */
507 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
508 /* 00000058 "........" */
509 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000510 0x00, 0x79, 0x00
511 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100512 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000513}
514
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100515void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200516{
517/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200518 Name (_PTC, Package (0x02)
519 {
520 ResourceTemplate ()
521 {
522 Register (FFixedHW,
523 0x00, // Bit Width
524 0x00, // Bit Offset
525 0x0000000000000000, // Address
526 ,)
527 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200528
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200529 ResourceTemplate ()
530 {
531 Register (FFixedHW,
532 0x00, // Bit Width
533 0x00, // Bit Offset
534 0x0000000000000000, // Address
535 ,)
536 }
537 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200538*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200539 acpi_addr_t addr = {
540 .space_id = ACPI_ADDRESS_SPACE_FIXED,
541 .bit_width = 0,
542 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800543 {
544 .resv = 0
545 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200546 .addrl = 0,
547 .addrh = 0,
548 };
549
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100550 acpigen_write_name("_PTC");
551 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200552
553 /* ControlRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100554 acpigen_write_resourcetemplate_header();
555 acpigen_write_register(&addr);
556 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200557
558 /* StatusRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100559 acpigen_write_resourcetemplate_header();
560 acpigen_write_register(&addr);
561 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200562
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100563 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200564}
565
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700566static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100567{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700568 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100569 acpigen_write_len_f();
570 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700571 acpigen_emit_byte(flags);
572}
573
574/* Method (name, nargs, NotSerialized) */
575void acpigen_write_method(const char *name, int nargs)
576{
577 __acpigen_write_method(name, (nargs & 7));
578}
579
580/* Method (name, nargs, Serialized) */
581void acpigen_write_method_serialized(const char *name, int nargs)
582{
583 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100584}
585
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100586void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100587{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700588 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100589 acpigen_write_len_f();
590 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100591}
592
Duncan Laurieabe2de82016-05-09 11:08:46 -0700593void acpigen_write_STA(uint8_t status)
594{
595 /*
596 * Method (_STA, 0, NotSerialized) { Return (status) }
597 */
598 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700599 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700600 acpigen_write_byte(status);
601 acpigen_pop_len();
602}
603
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100604/*
605 * Generates a func with max supported P-states.
606 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100607void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000608{
609/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200610 Method (_PPC, 0, NotSerialized)
611 {
612 Return (nr)
613 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000614*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100615 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700616 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000617 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100618 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100619 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000620}
621
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100622/*
623 * Generates a func with max supported P-states saved
624 * in the variable PPCM.
625 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100626void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700627{
628/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200629 Method (_PPC, 0, NotSerialized)
630 {
631 Return (PPCM)
632 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700633*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100634 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700635 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700636 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100637 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100638 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700639}
640
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100641void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200642{
643/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200644 // Sample _TPC method
645 Method (_TPC, 0, NotSerialized)
646 {
647 Return (\TLVL)
648 }
649*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100650 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700651 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100652 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100653 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200654}
655
Duncan Laurieabe2de82016-05-09 11:08:46 -0700656void acpigen_write_PRW(u32 wake, u32 level)
657{
658 /*
659 * Name (_PRW, Package () { wake, level }
660 */
661 acpigen_write_name("_PRW");
662 acpigen_write_package(2);
663 acpigen_write_integer(wake);
664 acpigen_write_integer(level);
665 acpigen_pop_len();
666}
667
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100668void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000669 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000670{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100671 acpigen_write_package(6);
672 acpigen_write_dword(coreFreq);
673 acpigen_write_dword(power);
674 acpigen_write_dword(transLat);
675 acpigen_write_dword(busmLat);
676 acpigen_write_dword(control);
677 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100678 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700679
680 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
681 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000682}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000683
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100684void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000685{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100686 acpigen_write_name("_PSD");
687 acpigen_write_package(1);
688 acpigen_write_package(5);
689 acpigen_write_byte(5); // 5 values
690 acpigen_write_byte(0); // revision 0
691 acpigen_write_dword(domain);
692 acpigen_write_dword(coordtype);
693 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100694 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100695 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000696}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000697
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100698void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200699{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100700 acpigen_write_package(4);
701 acpigen_write_resourcetemplate_header();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200702 acpigen_write_register(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100703 acpigen_write_resourcetemplate_footer();
704 acpigen_write_dword(cstate->ctype);
705 acpigen_write_dword(cstate->latency);
706 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100707 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200708}
709
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100710void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200711{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100712 int i;
713 acpigen_write_name("_CST");
714 acpigen_write_package(nentries+1);
715 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200716
717 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100718 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200719
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100720 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200721}
722
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700723void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
724 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500725{
726 acpigen_write_name("_CSD");
727 acpigen_write_package(1);
728 acpigen_write_package(6);
729 acpigen_write_byte(6); // 6 values
730 acpigen_write_byte(0); // revision 0
731 acpigen_write_dword(domain);
732 acpigen_write_dword(coordtype);
733 acpigen_write_dword(numprocs);
734 acpigen_write_dword(index);
735 acpigen_pop_len();
736 acpigen_pop_len();
737}
738
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100739void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200740{
741/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200742 Sample _TSS package with 100% and 50% duty cycles
743 Name (_TSS, Package (0x02)
744 {
745 Package(){100, 1000, 0, 0x00, 0)
746 Package(){50, 520, 0, 0x18, 0)
747 })
748*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100749 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200750 acpi_tstate_t *tstate = tstate_list;
751
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100752 acpigen_write_name("_TSS");
753 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200754
755 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100756 acpigen_write_package(5);
757 acpigen_write_dword(tstate->percent);
758 acpigen_write_dword(tstate->power);
759 acpigen_write_dword(tstate->latency);
760 acpigen_write_dword(tstate->control);
761 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100762 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200763 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200764 }
765
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100766 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200767}
768
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100769void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200770{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100771 acpigen_write_name("_TSD");
772 acpigen_write_package(1);
773 acpigen_write_package(5);
774 acpigen_write_byte(5); // 5 values
775 acpigen_write_byte(0); // revision 0
776 acpigen_write_dword(domain);
777 acpigen_write_dword(coordtype);
778 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100779 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100780 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200781}
782
783
784
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100785void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000786{
787 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200788 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000789 * Byte 0:
790 * Bit7 : 1 => big item
791 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
792 */
793 acpigen_emit_byte(0x86);
794 /* Byte 1+2: length (0x0009) */
795 acpigen_emit_byte(0x09);
796 acpigen_emit_byte(0x00);
797 /* bit1-7 are ignored */
798 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700799 acpigen_emit_dword(base);
800 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000801}
802
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100803void acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200804{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200805 acpigen_emit_byte(0x82); /* Register Descriptor */
806 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
807 acpigen_emit_byte(0x00); /* Register Length 15:8 */
808 acpigen_emit_byte(addr->space_id); /* Address Space ID */
809 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
810 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
811 acpigen_emit_byte(addr->resv); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700812 acpigen_emit_dword(addr->addrl); /* Register Address Low */
813 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200814}
815
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100816void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100817{
818 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200819 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100820 * Byte 0:
821 * Bit7 : 0 => small item
822 * Bit6-3: 0100 (0x4) => IRQ port descriptor
823 * Bit2-0: 010 (0x2) => 2 Bytes long
824 */
825 acpigen_emit_byte(0x22);
826 acpigen_emit_byte(mask & 0xff);
827 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100828}
829
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100830void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000831{
832 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200833 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000834 * Byte 0:
835 * Bit7 : 0 => small item
836 * Bit6-3: 1000 (0x8) => I/O port descriptor
837 * Bit2-0: 111 (0x7) => 7 Bytes long
838 */
839 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100840 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000841 /* bit1-7 are ignored */
842 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
843 /* minimum base address the device may be configured for */
844 acpigen_emit_byte(min & 0xff);
845 acpigen_emit_byte((min >> 8) & 0xff);
846 /* maximum base address the device may be configured for */
847 acpigen_emit_byte(max & 0xff);
848 acpigen_emit_byte((max >> 8) & 0xff);
849 /* alignment for min base */
850 acpigen_emit_byte(align & 0xff);
851 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000852}
853
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100854void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000855{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000856 /*
857 * A ResourceTemplate() is a Buffer() with a
858 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100859 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000860 * (small item 0xf, len 1)
861 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700862 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100863 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700864 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000865 len_stack[ltop++] = acpigen_get_current();
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100866 acpigen_emit_byte(0x00);
867 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000868}
869
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100870void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000871{
872 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100873 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000874 /*
875 * end tag (acpi 4.0 Section 6.4.2.8)
876 * 0x79 <checksum>
877 * 0x00 is treated as a good checksum according to the spec
878 * and is what iasl generates.
879 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100880 acpigen_emit_byte(0x79);
881 acpigen_emit_byte(0x00);
882
Martin Rothe3690102016-01-06 15:21:02 -0700883 len = gencurrent - p;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100884
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000885 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100886 p[0] = len & 0xff;
887 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000888 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100889 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000890}
891
892static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
893 struct resource *res)
894{
895 acpigen_write_mem32fixed(0, res->base, res->size);
896}
897
898static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
899 struct resource *res)
900{
901 resource_t base = res->base;
902 resource_t size = res->size;
903 while (size > 0) {
904 resource_t sz = size > 255 ? 255 : size;
905 acpigen_write_io16(base, base, 0, sz, 1);
906 size -= sz;
907 base += sz;
908 }
909}
910
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100911void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000912{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100913 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000914
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100915 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000916 search_global_resources(
917 IORESOURCE_MEM | IORESOURCE_RESERVE,
918 IORESOURCE_MEM | IORESOURCE_RESERVE,
919 acpigen_add_mainboard_rsvd_mem32, 0);
920
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100921 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000922 search_global_resources(
923 IORESOURCE_IO | IORESOURCE_RESERVE,
924 IORESOURCE_IO | IORESOURCE_RESERVE,
925 acpigen_add_mainboard_rsvd_io, 0);
926
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100927 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000928}
929
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100930void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000931{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100932 acpigen_write_scope(scope);
933 acpigen_write_name(name);
934 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100935 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000936}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100937
938static int hex2bin(const char c)
939{
940 if (c >= 'A' && c <= 'F')
941 return c - 'A' + 10;
942 if (c >= 'a' && c <= 'f')
943 return c - 'a' + 10;
944 return c - '0';
945}
946
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100947void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100948{
949 u32 compact = 0;
950
951 /* Clamping individual values would be better but
952 there is a disagreement over what is a valid
953 EISA id, so accept anything and don't clamp,
954 parent code should create a valid EISAid.
955 */
956 compact |= (eisaid[0] - 'A' + 1) << 26;
957 compact |= (eisaid[1] - 'A' + 1) << 21;
958 compact |= (eisaid[2] - 'A' + 1) << 16;
959 compact |= hex2bin(eisaid[3]) << 12;
960 compact |= hex2bin(eisaid[4]) << 8;
961 compact |= hex2bin(eisaid[5]) << 4;
962 compact |= hex2bin(eisaid[6]);
963
964 acpigen_emit_byte(0xc);
965 acpigen_emit_byte((compact >> 24) & 0xff);
966 acpigen_emit_byte((compact >> 16) & 0xff);
967 acpigen_emit_byte((compact >> 8) & 0xff);
968 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100969}
Duncan Laurie3829f232016-05-09 11:08:46 -0700970
971/*
972 * ToUUID(uuid)
973 *
974 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
975 * order for the bytes that make up a UUID Buffer object.
976 * UUID byte order for input:
977 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
978 * UUID byte order for output:
979 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
980 */
981#define UUID_LEN 16
982void acpigen_write_uuid(const char *uuid)
983{
984 uint8_t buf[UUID_LEN];
985 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
986 8, 9, 10, 11, 12, 13, 14, 15 };
987
988 /* Parse UUID string into bytes */
989 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
990 return;
991
992 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700993 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -0700994 acpigen_write_len_f();
995
996 /* Buffer length in bytes */
997 acpigen_write_word(UUID_LEN);
998
999 /* Output UUID in expected order */
1000 for (i = 0; i < UUID_LEN; i++)
1001 acpigen_emit_byte(buf[order[i]]);
1002
1003 acpigen_pop_len();
1004}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001005
1006/*
1007 * Name (_PRx, Package(One) { name })
1008 * ...
1009 * PowerResource (name, level, order)
1010 */
1011void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
1012 const char *dev_states[], size_t dev_states_count)
1013{
1014 int i;
1015 for (i = 0; i < dev_states_count; i++) {
1016 acpigen_write_name(dev_states[i]);
1017 acpigen_write_package(1);
1018 acpigen_emit_simple_namestring(name);
1019 acpigen_pop_len(); /* Package */
1020 }
1021
1022 acpigen_emit_ext_op(POWER_RES_OP);
1023
1024 acpigen_write_len_f();
1025
1026 acpigen_emit_simple_namestring(name);
1027 acpigen_emit_byte(level);
1028 acpigen_emit_word(order);
1029}
1030
1031/* Sleep (ms) */
1032void acpigen_write_sleep(uint64_t sleep_ms)
1033{
1034 acpigen_emit_ext_op(SLEEP_OP);
1035 acpigen_write_integer(sleep_ms);
1036}
1037
1038void acpigen_write_store(void)
1039{
1040 acpigen_emit_byte(STORE_OP);
1041}
1042
1043/* Store (src, dst) */
1044void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1045{
1046 acpigen_write_store();
1047 acpigen_emit_byte(src);
1048 acpigen_emit_byte(dst);
1049}
1050
1051/* Or (arg1, arg2, res) */
1052void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1053{
1054 acpigen_emit_byte(OR_OP);
1055 acpigen_emit_byte(arg1);
1056 acpigen_emit_byte(arg2);
1057 acpigen_emit_byte(res);
1058}
1059
1060/* And (arg1, arg2, res) */
1061void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1062{
1063 acpigen_emit_byte(AND_OP);
1064 acpigen_emit_byte(arg1);
1065 acpigen_emit_byte(arg2);
1066 acpigen_emit_byte(res);
1067}
1068
1069/* Not (arg, res) */
1070void acpigen_write_not(uint8_t arg, uint8_t res)
1071{
1072 acpigen_emit_byte(NOT_OP);
1073 acpigen_emit_byte(arg);
1074 acpigen_emit_byte(res);
1075}
1076
1077/* Store (str, DEBUG) */
1078void acpigen_write_debug_string(const char *str)
1079{
1080 acpigen_write_store();
1081 acpigen_write_string(str);
1082 acpigen_emit_ext_op(DEBUG_OP);
1083}
1084
1085/* Store (val, DEBUG) */
1086void acpigen_write_debug_integer(uint64_t val)
1087{
1088 acpigen_write_store();
1089 acpigen_write_integer(val);
1090 acpigen_emit_ext_op(DEBUG_OP);
1091}
1092
1093/* Store (op, DEBUG) */
1094void acpigen_write_debug_op(uint8_t op)
1095{
1096 acpigen_write_store();
1097 acpigen_emit_byte(op);
1098 acpigen_emit_ext_op(DEBUG_OP);
1099}
1100
1101void acpigen_write_if(void)
1102{
1103 acpigen_emit_byte(IF_OP);
1104 acpigen_write_len_f();
1105}
1106
1107/* If (And (arg1, arg2)) */
1108void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1109{
1110 acpigen_write_if();
1111 acpigen_emit_byte(AND_OP);
1112 acpigen_emit_byte(arg1);
1113 acpigen_emit_byte(arg2);
1114}
1115
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001116/*
1117 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1118 * operand1 is ACPI op and operand2 is an integer.
1119 *
1120 * If (Lequal (op, val))
1121 */
1122void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001123{
1124 acpigen_write_if();
1125 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001126 acpigen_emit_byte(op);
1127 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001128}
1129
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001130void acpigen_write_else(void)
1131{
1132 acpigen_emit_byte(ELSE_OP);
1133 acpigen_write_len_f();
1134}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001135
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001136void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1137{
1138 acpigen_emit_byte(TO_BUFFER_OP);
1139 acpigen_emit_byte(src);
1140 acpigen_emit_byte(dst);
1141}
1142
1143void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1144{
1145 acpigen_emit_byte(TO_INTEGER_OP);
1146 acpigen_emit_byte(src);
1147 acpigen_emit_byte(dst);
1148}
1149
1150void acpigen_write_byte_buffer(uint8_t *arr, uint8_t size)
1151{
1152 uint8_t i;
1153
1154 acpigen_emit_byte(BUFFER_OP);
1155 acpigen_write_len_f();
Naresh G Solankic70cc4d2016-11-16 10:10:09 +05301156 acpigen_write_byte(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001157
1158 for (i = 0; i < size; i++)
1159 acpigen_emit_byte(arr[i]);
1160
1161 acpigen_pop_len();
1162}
1163
1164void acpigen_write_return_byte_buffer(uint8_t *arr, uint8_t size)
1165{
1166 acpigen_emit_byte(RETURN_OP);
1167 acpigen_write_byte_buffer(arr, size);
1168}
1169
1170void acpigen_write_return_singleton_buffer(uint8_t arg)
1171{
1172 acpigen_write_return_byte_buffer(&arg, 1);
1173}
1174
1175void acpigen_write_return_byte(uint8_t arg)
1176{
1177 acpigen_emit_byte(RETURN_OP);
1178 acpigen_write_byte(arg);
1179}
1180
Naresh G Solanki05abe432016-11-17 00:16:29 +05301181void acpigen_write_return_integer(uint64_t arg)
1182{
1183 acpigen_emit_byte(RETURN_OP);
1184 acpigen_write_integer(arg);
1185}
1186
1187void acpigen_write_return_string(const char *arg)
1188{
1189 acpigen_emit_byte(RETURN_OP);
1190 acpigen_write_string(arg);
1191}
1192
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301193void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1194 size_t count, void *arg)
1195{
1196 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1197 acpigen_write_dsm_uuid_arr(&id, 1);
1198}
1199
1200static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1201{
1202 size_t i;
1203
1204 /* If (LEqual (Local0, ToUUID(uuid))) */
1205 acpigen_write_if();
1206 acpigen_emit_byte(LEQUAL_OP);
1207 acpigen_emit_byte(LOCAL0_OP);
1208 acpigen_write_uuid(id->uuid);
1209
1210 /* ToInteger (Arg2, Local1) */
1211 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1212
1213 for (i = 0; i < id->count; i++) {
1214 /* If (LEqual (Local1, i)) */
1215 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1216
1217 /* Callback to write if handler. */
1218 if (id->callbacks[i])
1219 id->callbacks[i](id->arg);
1220
1221 acpigen_pop_len(); /* If */
1222 }
1223
1224 /* Default case: Return (Buffer (One) { 0x0 }) */
1225 acpigen_write_return_singleton_buffer(0x0);
1226
1227 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1228
1229}
1230
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001231/*
1232 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301233 * This function takes as input array of uuid for the device, set of callbacks
1234 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1235 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1236 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001237 *
1238 * Arguments passed into _DSM method:
1239 * Arg0 = UUID
1240 * Arg1 = Revision
1241 * Arg2 = Function index
1242 * Arg3 = Function specific arguments
1243 *
1244 * AML code generated would look like:
1245 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301246 * ToBuffer (Arg0, Local0)
1247 * If (LEqual (Local0, ToUUID(uuid))) {
1248 * ToInteger (Arg2, Local1)
1249 * If (LEqual (Local1, 0)) {
1250 * <acpigen by callback[0]>
1251 * }
1252 * ...
1253 * If (LEqual (Local1, n)) {
1254 * <acpigen by callback[n]>
1255 * }
1256 * Return (Buffer (One) { 0x0 })
1257 * }
1258 * ...
1259 * If (LEqual (Local0, ToUUID(uuidn))) {
1260 * ...
1261 * }
1262 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001263 * }
1264 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301265void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001266{
1267 size_t i;
1268
1269 /* Method (_DSM, 4, Serialized) */
1270 acpigen_write_method_serialized("_DSM", 0x4);
1271
1272 /* ToBuffer (Arg0, Local0) */
1273 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1274
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001275 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301276 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001277
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301278 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001279 acpigen_write_return_singleton_buffer(0x0);
1280
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001281 acpigen_pop_len(); /* Method _DSM */
1282}
1283
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001284/* Soc-implemented functions -- weak definitions. */
1285int __attribute__((weak)) acpigen_soc_read_rx_gpio(unsigned int gpio_num)
1286{
1287 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1288 acpigen_write_debug_string("read_rx_gpio not available");
1289 return -1;
1290}
1291
1292int __attribute__((weak)) acpigen_soc_get_tx_gpio(unsigned int gpio_num)
1293{
1294 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1295 acpigen_write_debug_string("get_tx_gpio not available");
1296 return -1;
1297}
1298
1299int __attribute__((weak)) acpigen_soc_set_tx_gpio(unsigned int gpio_num)
1300{
1301 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1302 acpigen_write_debug_string("set_tx_gpio not available");
1303 return -1;
1304}
1305
1306int __attribute__((weak)) acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
1307{
1308 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1309 acpigen_write_debug_string("clear_tx_gpio not available");
1310 return -1;
1311}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001312
1313/*
1314 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1315 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1316 * make callbacks into SoC acpigen code.
1317 *
1318 * Returns 0 on success and -1 on error.
1319 */
1320int acpigen_enable_tx_gpio(struct acpi_gpio *gpio)
1321{
1322 if (gpio->polarity == ACPI_GPIO_ACTIVE_HIGH)
1323 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1324 else
1325 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1326}
1327
1328int acpigen_disable_tx_gpio(struct acpi_gpio *gpio)
1329{
1330 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1331 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1332 else
1333 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1334}