blob: 8ebdd0982c170978dea22f5ecf93da2afe66736e [file] [log] [blame]
Rudolf Marek293b5f52009-02-01 18:35:15 +00001/*
2 * This file is part of the coreboot project.
3 *
Timothy Pearson83abd812015-06-08 19:35:06 -05004 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Rudolf Marek293b5f52009-02-01 18:35:15 +00005 * Copyright (C) 2009 Rudolf Marek <r.marek@assembler.cz>
6 *
7 * This program is free software; you can redistribute it and/or modify
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
Rudolf Marek293b5f52009-02-01 18:35:15 +000010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Rudolf Marek293b5f52009-02-01 18:35:15 +000015 */
16
Paul Menzel0f4c0e22013-02-22 12:33:08 +010017/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +000018#define ACPIGEN_LENSTACK_SIZE 10
19
Paul Menzel0f4c0e22013-02-22 12:33:08 +010020/*
Timothy Pearson83abd812015-06-08 19:35:06 -050021 * If you need to change this, change acpigen_write_len_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +010022 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +010023 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000024
Timothy Pearson83abd812015-06-08 19:35:06 -050025#define ACPIGEN_MAXLEN 0xfffff
Rudolf Marek293b5f52009-02-01 18:35:15 +000026
Duncan Laurie3829f232016-05-09 11:08:46 -070027#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000028#include <string.h>
29#include <arch/acpigen.h>
30#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000031#include <device/device.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000032
33static char *gencurrent;
34
35char *len_stack[ACPIGEN_LENSTACK_SIZE];
36int ltop = 0;
37
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010038void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000039{
40 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010041 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000042 acpigen_emit_byte(0);
43 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050044 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000045}
46
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010047void acpigen_pop_len(void)
48{
49 int len;
50 ASSERT(ltop > 0)
51 char *p = len_stack[--ltop];
52 len = gencurrent - p;
53 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050054 /* generate store length for 0xfffff max */
55 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010056 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050057 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010058
59}
60
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000061void acpigen_set_current(char *curr)
62{
63 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000064}
65
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000066char *acpigen_get_current(void)
67{
68 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000069}
70
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010071void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000072{
73 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000074}
75
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070076void acpigen_emit_ext_op(uint8_t op)
77{
78 acpigen_emit_byte(EXT_OP_PREFIX);
79 acpigen_emit_byte(op);
80}
81
Duncan Laurie9ccae752016-05-09 07:43:19 -070082void acpigen_emit_word(unsigned int data)
83{
84 acpigen_emit_byte(data & 0xff);
85 acpigen_emit_byte((data >> 8) & 0xff);
86}
87
88void acpigen_emit_dword(unsigned int data)
89{
90 acpigen_emit_byte(data & 0xff);
91 acpigen_emit_byte((data >> 8) & 0xff);
92 acpigen_emit_byte((data >> 16) & 0xff);
93 acpigen_emit_byte((data >> 24) & 0xff);
94}
95
Duncan Laurie85d80272016-07-02 19:53:54 -070096char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000097{
Duncan Laurie85d80272016-07-02 19:53:54 -070098 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070099 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100100 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -0700101 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +0000102 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -0700103 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000104}
105
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100106void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000107{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700108 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000109 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000110}
111
Duncan Laurie9ccae752016-05-09 07:43:19 -0700112void acpigen_write_word(unsigned int data)
113{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700114 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700115 acpigen_emit_word(data);
116}
117
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100118void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000119{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700120 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700121 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000122}
123
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100124void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000125{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700126 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700127 acpigen_emit_dword(data & 0xffffffff);
128 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000129}
130
Duncan Laurief7c38762016-05-09 08:20:38 -0700131void acpigen_write_zero(void)
132{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700133 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700134}
135
136void acpigen_write_one(void)
137{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700138 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700139}
140
141void acpigen_write_ones(void)
142{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700143 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700144}
145
146void acpigen_write_integer(uint64_t data)
147{
148 if (data == 0)
149 acpigen_write_zero();
150 else if (data == 1)
151 acpigen_write_one();
152 else if (data <= 0xff)
153 acpigen_write_byte((unsigned char)data);
154 else if (data <= 0xffff)
155 acpigen_write_word((unsigned int)data);
156 else if (data <= 0xffffffff)
157 acpigen_write_dword((unsigned int)data);
158 else
159 acpigen_write_qword(data);
160}
161
162void acpigen_write_name_zero(const char *name)
163{
164 acpigen_write_name(name);
165 acpigen_write_one();
166}
167
168void acpigen_write_name_one(const char *name)
169{
170 acpigen_write_name(name);
171 acpigen_write_zero();
172}
173
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100174void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000175{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100176 acpigen_write_name(name);
177 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000178}
179
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100180void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000181{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100182 acpigen_write_name(name);
183 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000184}
185
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100186void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000187{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100188 acpigen_write_name(name);
189 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000190}
191
Duncan Laurief7c38762016-05-09 08:20:38 -0700192void acpigen_write_name_integer(const char *name, uint64_t val)
193{
194 acpigen_write_name(name);
195 acpigen_write_integer(val);
196}
197
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700198void acpigen_write_name_string(const char *name, const char *string)
199{
200 acpigen_write_name(name);
201 acpigen_write_string(string);
202}
203
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100204void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000205{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000206 int i;
207 for (i = 0; i < size; i++) {
208 acpigen_emit_byte(data[i]);
209 }
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
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100241static void acpigen_emit_simple_namestring(const char *name) {
242 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000243 char ud[] = "____";
244 for (i = 0; i < 4; i++) {
245 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100246 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000247 break;
248 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100249 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000250 }
251 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000252}
253
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100254static void acpigen_emit_double_namestring(const char *name, int dotpos) {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700255 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100256 acpigen_emit_simple_namestring(name);
257 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000258}
259
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100260static void acpigen_emit_multi_namestring(const char *name) {
261 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000262 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700263 acpigen_emit_byte(MULTI_NAME_PREFIX);
264 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000265 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
266
267 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100268 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000269 /* find end or next entity */
270 while ((name[0] != '.') && (name[0] != '\0'))
271 name++;
272 /* forward to next */
273 if (name[0] == '.')
274 name++;
275 count++;
276 }
277
278 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000279}
280
281
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100282void acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000283 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000284 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000285
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600286 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000287 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100288 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000289 namepath++;
290 }
291
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600292 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000293 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100294 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000295 namepath++;
296 }
297
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200298 /* If we have only \\ or only ^...^. Then we need to put a null
299 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200300 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700301 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100302 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200303 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000304
305 i = 0;
306 while (namepath[i] != '\0') {
307 if (namepath[i] == '.') {
308 dotcount++;
309 dotpos = i;
310 }
311 i++;
312 }
313
314 if (dotcount == 0) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100315 acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000316 } else if (dotcount == 1) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100317 acpigen_emit_double_namestring(namepath, dotpos);
Rudolf Marek3310d752009-06-21 20:26:13 +0000318 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100319 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000320 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000321}
322
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100323void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000324{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700325 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100326 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000327}
328
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100329void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000330{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700331 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100332 acpigen_write_len_f();
333 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000334}
Rudolf Marekf997b552009-02-14 15:40:23 +0000335
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100336void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000337{
338/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200339 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
340 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000341*/
342 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700343 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100344 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000345
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200346 snprintf(pscope, sizeof(pscope),
Timothy Pearson033bb4b2015-02-10 22:21:39 -0600347 "\\_PR.CP%02d", (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100348 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000349 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700350 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000351 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000352}
353
Naresh G Solanki8535c662016-10-25 00:51:24 +0530354/*
355 * Generate ACPI AML code for OperationRegion
356 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
357 * where rname is region name, space is region space, offset is region offset &
358 * len is region length.
359 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
360 */
361void acpigen_write_opregion(struct opregion *opreg)
362{
363 /* OpregionOp */
364 acpigen_emit_ext_op(OPREGION_OP);
365 /* NameString 4 chars only */
366 acpigen_emit_simple_namestring(opreg->name);
367 /* RegionSpace */
368 acpigen_emit_byte(opreg->regionspace);
369 /* RegionOffset & RegionLen, it can be byte word or double word */
370 acpigen_write_integer(opreg->regionoffset);
371 acpigen_write_integer(opreg->regionlen);
372}
373
374static void acpigen_write_field_offset(uint32_t offset,
375 uint32_t current_bit_pos)
376{
377 uint32_t diff_bits;
378 uint8_t i, j;
379 uint8_t emit[4];
380
381 if (offset < current_bit_pos) {
382 printk(BIOS_WARNING, "%s: Cannot move offset backward",
383 __func__);
384 return;
385 }
386
387 diff_bits = offset - current_bit_pos;
388 /* Upper limit */
389 if (diff_bits > 0xFFFFFFF) {
390 printk(BIOS_WARNING, "%s: Offset very large to encode",
391 __func__);
392 return;
393 }
394
395 i = 1;
396 if (diff_bits < 0x40) {
397 emit[0] = diff_bits & 0x3F;
398 } else {
399 emit[0] = diff_bits & 0xF;
400 diff_bits >>= 4;
401 while (diff_bits) {
402 emit[i] = diff_bits & 0xFF;
403 i++;
404 diff_bits >>= 8;
405 }
406 }
407 /* Update bit 7:6 : Number of bytes followed by emit[0] */
408 emit[0] |= (i - 1) << 6;
409
410 acpigen_emit_byte(0);
411 for (j = 0; j < i; j++)
412 acpigen_emit_byte(emit[j]);
413}
414
415/*
416 * Generate ACPI AML code for Field
417 * Arg0: region name
418 * Arg1: Pointer to struct fieldlist.
419 * Arg2: no. of entries in Arg1
420 * Arg3: flags which indicate filed access type, lock rule & update rule.
421 * Example with fieldlist
422 * struct fieldlist l[] = {
423 * FIELDLIST_OFFSET(0x84),
424 * FIELDLIST_NAMESTR("PMCS", 2),
425 * };
426 * acpigen_write_field("UART", l ,ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
427 * FIELD_PRESERVE);
428 * Output:
429 * Field (UART, AnyAcc, NoLock, Preserve)
430 * {
431 * Offset (0x84),
432 * PMCS, 2
433 * }
434 */
435void acpigen_write_field(const char *name, struct fieldlist *l, size_t count,
436 uint8_t flags)
437{
438 uint16_t i;
439 uint32_t current_bit_pos = 0;
440
441 /* FieldOp */
442 acpigen_emit_ext_op(FIELD_OP);
443 /* Package Length */
444 acpigen_write_len_f();
445 /* NameString 4 chars only */
446 acpigen_emit_simple_namestring(name);
447 /* Field Flag */
448 acpigen_emit_byte(flags);
449
450 for (i = 0; i < count; i++) {
451 switch (l[i].type) {
452 case NAME_STRING:
453 acpigen_emit_simple_namestring(l[i].name);
454 acpigen_emit_byte(l[i].bits);
455 current_bit_pos += l[i].bits;
456 break;
457 case OFFSET:
458 acpigen_write_field_offset(l[i].bits, current_bit_pos);
459 current_bit_pos = l[i].bits;
460 break;
461 default:
462 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
463 , __func__, l[i].type);
464 break;
465 }
466 }
467 acpigen_pop_len();
468}
469
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100470void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000471{
472/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200473 Name (_PCT, Package (0x02)
474 {
475 ResourceTemplate ()
476 {
477 Register (FFixedHW,
478 0x00, // Bit Width
479 0x00, // Bit Offset
480 0x0000000000000000, // Address
481 ,)
482 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000483
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200484 ResourceTemplate ()
485 {
486 Register (FFixedHW,
487 0x00, // Bit Width
488 0x00, // Bit Offset
489 0x0000000000000000, // Address
490 ,)
491 }
492 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000493*/
494 static char stream[] = {
495 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
496 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
497 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
498 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
499 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
500 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
501 0x00, 0x79, 0x00
502 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100503 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000504}
505
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100506void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200507{
508/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200509 Name (_PTC, Package (0x02)
510 {
511 ResourceTemplate ()
512 {
513 Register (FFixedHW,
514 0x00, // Bit Width
515 0x00, // Bit Offset
516 0x0000000000000000, // Address
517 ,)
518 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200519
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200520 ResourceTemplate ()
521 {
522 Register (FFixedHW,
523 0x00, // Bit Width
524 0x00, // Bit Offset
525 0x0000000000000000, // Address
526 ,)
527 }
528 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200529*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200530 acpi_addr_t addr = {
531 .space_id = ACPI_ADDRESS_SPACE_FIXED,
532 .bit_width = 0,
533 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800534 {
535 .resv = 0
536 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200537 .addrl = 0,
538 .addrh = 0,
539 };
540
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100541 acpigen_write_name("_PTC");
542 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200543
544 /* ControlRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100545 acpigen_write_resourcetemplate_header();
546 acpigen_write_register(&addr);
547 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200548
549 /* StatusRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100550 acpigen_write_resourcetemplate_header();
551 acpigen_write_register(&addr);
552 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200553
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100554 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200555}
556
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700557static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100558{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700559 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100560 acpigen_write_len_f();
561 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700562 acpigen_emit_byte(flags);
563}
564
565/* Method (name, nargs, NotSerialized) */
566void acpigen_write_method(const char *name, int nargs)
567{
568 __acpigen_write_method(name, (nargs & 7));
569}
570
571/* Method (name, nargs, Serialized) */
572void acpigen_write_method_serialized(const char *name, int nargs)
573{
574 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100575}
576
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100577void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100578{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700579 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100580 acpigen_write_len_f();
581 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100582}
583
Duncan Laurieabe2de82016-05-09 11:08:46 -0700584void acpigen_write_STA(uint8_t status)
585{
586 /*
587 * Method (_STA, 0, NotSerialized) { Return (status) }
588 */
589 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700590 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700591 acpigen_write_byte(status);
592 acpigen_pop_len();
593}
594
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100595/*
596 * Generates a func with max supported P-states.
597 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100598void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000599{
600/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200601 Method (_PPC, 0, NotSerialized)
602 {
603 Return (nr)
604 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000605*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100606 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700607 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000608 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100609 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100610 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000611}
612
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100613/*
614 * Generates a func with max supported P-states saved
615 * in the variable PPCM.
616 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100617void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700618{
619/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200620 Method (_PPC, 0, NotSerialized)
621 {
622 Return (PPCM)
623 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700624*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100625 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700626 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700627 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100628 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100629 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700630}
631
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100632void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200633{
634/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200635 // Sample _TPC method
636 Method (_TPC, 0, NotSerialized)
637 {
638 Return (\TLVL)
639 }
640*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100641 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700642 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100643 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100644 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200645}
646
Duncan Laurieabe2de82016-05-09 11:08:46 -0700647void acpigen_write_PRW(u32 wake, u32 level)
648{
649 /*
650 * Name (_PRW, Package () { wake, level }
651 */
652 acpigen_write_name("_PRW");
653 acpigen_write_package(2);
654 acpigen_write_integer(wake);
655 acpigen_write_integer(level);
656 acpigen_pop_len();
657}
658
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100659void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000660 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000661{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100662 acpigen_write_package(6);
663 acpigen_write_dword(coreFreq);
664 acpigen_write_dword(power);
665 acpigen_write_dword(transLat);
666 acpigen_write_dword(busmLat);
667 acpigen_write_dword(control);
668 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100669 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700670
671 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
672 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000673}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000674
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100675void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000676{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100677 acpigen_write_name("_PSD");
678 acpigen_write_package(1);
679 acpigen_write_package(5);
680 acpigen_write_byte(5); // 5 values
681 acpigen_write_byte(0); // revision 0
682 acpigen_write_dword(domain);
683 acpigen_write_dword(coordtype);
684 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100685 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100686 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000687}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000688
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100689void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200690{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100691 acpigen_write_package(4);
692 acpigen_write_resourcetemplate_header();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200693 acpigen_write_register(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100694 acpigen_write_resourcetemplate_footer();
695 acpigen_write_dword(cstate->ctype);
696 acpigen_write_dword(cstate->latency);
697 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100698 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200699}
700
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100701void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200702{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100703 int i;
704 acpigen_write_name("_CST");
705 acpigen_write_package(nentries+1);
706 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200707
708 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100709 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200710
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100711 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200712}
713
Timothy Pearson83abd812015-06-08 19:35:06 -0500714void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype, u32 index)
715{
716 acpigen_write_name("_CSD");
717 acpigen_write_package(1);
718 acpigen_write_package(6);
719 acpigen_write_byte(6); // 6 values
720 acpigen_write_byte(0); // revision 0
721 acpigen_write_dword(domain);
722 acpigen_write_dword(coordtype);
723 acpigen_write_dword(numprocs);
724 acpigen_write_dword(index);
725 acpigen_pop_len();
726 acpigen_pop_len();
727}
728
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100729void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200730{
731/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200732 Sample _TSS package with 100% and 50% duty cycles
733 Name (_TSS, Package (0x02)
734 {
735 Package(){100, 1000, 0, 0x00, 0)
736 Package(){50, 520, 0, 0x18, 0)
737 })
738*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100739 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200740 acpi_tstate_t *tstate = tstate_list;
741
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100742 acpigen_write_name("_TSS");
743 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200744
745 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100746 acpigen_write_package(5);
747 acpigen_write_dword(tstate->percent);
748 acpigen_write_dword(tstate->power);
749 acpigen_write_dword(tstate->latency);
750 acpigen_write_dword(tstate->control);
751 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100752 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200753 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200754 }
755
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100756 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200757}
758
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100759void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200760{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100761 acpigen_write_name("_TSD");
762 acpigen_write_package(1);
763 acpigen_write_package(5);
764 acpigen_write_byte(5); // 5 values
765 acpigen_write_byte(0); // revision 0
766 acpigen_write_dword(domain);
767 acpigen_write_dword(coordtype);
768 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100769 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100770 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200771}
772
773
774
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100775void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000776{
777 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200778 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000779 * Byte 0:
780 * Bit7 : 1 => big item
781 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
782 */
783 acpigen_emit_byte(0x86);
784 /* Byte 1+2: length (0x0009) */
785 acpigen_emit_byte(0x09);
786 acpigen_emit_byte(0x00);
787 /* bit1-7 are ignored */
788 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700789 acpigen_emit_dword(base);
790 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000791}
792
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100793void acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200794{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200795 acpigen_emit_byte(0x82); /* Register Descriptor */
796 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
797 acpigen_emit_byte(0x00); /* Register Length 15:8 */
798 acpigen_emit_byte(addr->space_id); /* Address Space ID */
799 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
800 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
801 acpigen_emit_byte(addr->resv); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700802 acpigen_emit_dword(addr->addrl); /* Register Address Low */
803 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200804}
805
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100806void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100807{
808 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200809 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100810 * Byte 0:
811 * Bit7 : 0 => small item
812 * Bit6-3: 0100 (0x4) => IRQ port descriptor
813 * Bit2-0: 010 (0x2) => 2 Bytes long
814 */
815 acpigen_emit_byte(0x22);
816 acpigen_emit_byte(mask & 0xff);
817 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100818}
819
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100820void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000821{
822 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200823 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000824 * Byte 0:
825 * Bit7 : 0 => small item
826 * Bit6-3: 1000 (0x8) => I/O port descriptor
827 * Bit2-0: 111 (0x7) => 7 Bytes long
828 */
829 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100830 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000831 /* bit1-7 are ignored */
832 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
833 /* minimum base address the device may be configured for */
834 acpigen_emit_byte(min & 0xff);
835 acpigen_emit_byte((min >> 8) & 0xff);
836 /* maximum base address the device may be configured for */
837 acpigen_emit_byte(max & 0xff);
838 acpigen_emit_byte((max >> 8) & 0xff);
839 /* alignment for min base */
840 acpigen_emit_byte(align & 0xff);
841 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000842}
843
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100844void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000845{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000846 /*
847 * A ResourceTemplate() is a Buffer() with a
848 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100849 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000850 * (small item 0xf, len 1)
851 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700852 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100853 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700854 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000855 len_stack[ltop++] = acpigen_get_current();
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100856 acpigen_emit_byte(0x00);
857 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000858}
859
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100860void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000861{
862 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100863 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000864 /*
865 * end tag (acpi 4.0 Section 6.4.2.8)
866 * 0x79 <checksum>
867 * 0x00 is treated as a good checksum according to the spec
868 * and is what iasl generates.
869 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100870 acpigen_emit_byte(0x79);
871 acpigen_emit_byte(0x00);
872
Martin Rothe3690102016-01-06 15:21:02 -0700873 len = gencurrent - p;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100874
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000875 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100876 p[0] = len & 0xff;
877 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000878 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100879 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000880}
881
882static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
883 struct resource *res)
884{
885 acpigen_write_mem32fixed(0, res->base, res->size);
886}
887
888static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
889 struct resource *res)
890{
891 resource_t base = res->base;
892 resource_t size = res->size;
893 while (size > 0) {
894 resource_t sz = size > 255 ? 255 : size;
895 acpigen_write_io16(base, base, 0, sz, 1);
896 size -= sz;
897 base += sz;
898 }
899}
900
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100901void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000902{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100903 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000904
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100905 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000906 search_global_resources(
907 IORESOURCE_MEM | IORESOURCE_RESERVE,
908 IORESOURCE_MEM | IORESOURCE_RESERVE,
909 acpigen_add_mainboard_rsvd_mem32, 0);
910
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100911 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000912 search_global_resources(
913 IORESOURCE_IO | IORESOURCE_RESERVE,
914 IORESOURCE_IO | IORESOURCE_RESERVE,
915 acpigen_add_mainboard_rsvd_io, 0);
916
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100917 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000918}
919
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100920void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000921{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100922 acpigen_write_scope(scope);
923 acpigen_write_name(name);
924 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100925 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000926}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100927
928static int hex2bin(const char c)
929{
930 if (c >= 'A' && c <= 'F')
931 return c - 'A' + 10;
932 if (c >= 'a' && c <= 'f')
933 return c - 'a' + 10;
934 return c - '0';
935}
936
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100937void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100938{
939 u32 compact = 0;
940
941 /* Clamping individual values would be better but
942 there is a disagreement over what is a valid
943 EISA id, so accept anything and don't clamp,
944 parent code should create a valid EISAid.
945 */
946 compact |= (eisaid[0] - 'A' + 1) << 26;
947 compact |= (eisaid[1] - 'A' + 1) << 21;
948 compact |= (eisaid[2] - 'A' + 1) << 16;
949 compact |= hex2bin(eisaid[3]) << 12;
950 compact |= hex2bin(eisaid[4]) << 8;
951 compact |= hex2bin(eisaid[5]) << 4;
952 compact |= hex2bin(eisaid[6]);
953
954 acpigen_emit_byte(0xc);
955 acpigen_emit_byte((compact >> 24) & 0xff);
956 acpigen_emit_byte((compact >> 16) & 0xff);
957 acpigen_emit_byte((compact >> 8) & 0xff);
958 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100959}
Duncan Laurie3829f232016-05-09 11:08:46 -0700960
961/*
962 * ToUUID(uuid)
963 *
964 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
965 * order for the bytes that make up a UUID Buffer object.
966 * UUID byte order for input:
967 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
968 * UUID byte order for output:
969 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
970 */
971#define UUID_LEN 16
972void acpigen_write_uuid(const char *uuid)
973{
974 uint8_t buf[UUID_LEN];
975 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
976 8, 9, 10, 11, 12, 13, 14, 15 };
977
978 /* Parse UUID string into bytes */
979 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
980 return;
981
982 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700983 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -0700984 acpigen_write_len_f();
985
986 /* Buffer length in bytes */
987 acpigen_write_word(UUID_LEN);
988
989 /* Output UUID in expected order */
990 for (i = 0; i < UUID_LEN; i++)
991 acpigen_emit_byte(buf[order[i]]);
992
993 acpigen_pop_len();
994}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700995
996/*
997 * Name (_PRx, Package(One) { name })
998 * ...
999 * PowerResource (name, level, order)
1000 */
1001void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
1002 const char *dev_states[], size_t dev_states_count)
1003{
1004 int i;
1005 for (i = 0; i < dev_states_count; i++) {
1006 acpigen_write_name(dev_states[i]);
1007 acpigen_write_package(1);
1008 acpigen_emit_simple_namestring(name);
1009 acpigen_pop_len(); /* Package */
1010 }
1011
1012 acpigen_emit_ext_op(POWER_RES_OP);
1013
1014 acpigen_write_len_f();
1015
1016 acpigen_emit_simple_namestring(name);
1017 acpigen_emit_byte(level);
1018 acpigen_emit_word(order);
1019}
1020
1021/* Sleep (ms) */
1022void acpigen_write_sleep(uint64_t sleep_ms)
1023{
1024 acpigen_emit_ext_op(SLEEP_OP);
1025 acpigen_write_integer(sleep_ms);
1026}
1027
1028void acpigen_write_store(void)
1029{
1030 acpigen_emit_byte(STORE_OP);
1031}
1032
1033/* Store (src, dst) */
1034void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1035{
1036 acpigen_write_store();
1037 acpigen_emit_byte(src);
1038 acpigen_emit_byte(dst);
1039}
1040
1041/* Or (arg1, arg2, res) */
1042void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1043{
1044 acpigen_emit_byte(OR_OP);
1045 acpigen_emit_byte(arg1);
1046 acpigen_emit_byte(arg2);
1047 acpigen_emit_byte(res);
1048}
1049
1050/* And (arg1, arg2, res) */
1051void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1052{
1053 acpigen_emit_byte(AND_OP);
1054 acpigen_emit_byte(arg1);
1055 acpigen_emit_byte(arg2);
1056 acpigen_emit_byte(res);
1057}
1058
1059/* Not (arg, res) */
1060void acpigen_write_not(uint8_t arg, uint8_t res)
1061{
1062 acpigen_emit_byte(NOT_OP);
1063 acpigen_emit_byte(arg);
1064 acpigen_emit_byte(res);
1065}
1066
1067/* Store (str, DEBUG) */
1068void acpigen_write_debug_string(const char *str)
1069{
1070 acpigen_write_store();
1071 acpigen_write_string(str);
1072 acpigen_emit_ext_op(DEBUG_OP);
1073}
1074
1075/* Store (val, DEBUG) */
1076void acpigen_write_debug_integer(uint64_t val)
1077{
1078 acpigen_write_store();
1079 acpigen_write_integer(val);
1080 acpigen_emit_ext_op(DEBUG_OP);
1081}
1082
1083/* Store (op, DEBUG) */
1084void acpigen_write_debug_op(uint8_t op)
1085{
1086 acpigen_write_store();
1087 acpigen_emit_byte(op);
1088 acpigen_emit_ext_op(DEBUG_OP);
1089}
1090
1091void acpigen_write_if(void)
1092{
1093 acpigen_emit_byte(IF_OP);
1094 acpigen_write_len_f();
1095}
1096
1097/* If (And (arg1, arg2)) */
1098void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1099{
1100 acpigen_write_if();
1101 acpigen_emit_byte(AND_OP);
1102 acpigen_emit_byte(arg1);
1103 acpigen_emit_byte(arg2);
1104}
1105
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001106/*
1107 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1108 * operand1 is ACPI op and operand2 is an integer.
1109 *
1110 * If (Lequal (op, val))
1111 */
1112void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001113{
1114 acpigen_write_if();
1115 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001116 acpigen_emit_byte(op);
1117 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001118}
1119
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001120void acpigen_write_else(void)
1121{
1122 acpigen_emit_byte(ELSE_OP);
1123 acpigen_write_len_f();
1124}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001125
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001126void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1127{
1128 acpigen_emit_byte(TO_BUFFER_OP);
1129 acpigen_emit_byte(src);
1130 acpigen_emit_byte(dst);
1131}
1132
1133void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1134{
1135 acpigen_emit_byte(TO_INTEGER_OP);
1136 acpigen_emit_byte(src);
1137 acpigen_emit_byte(dst);
1138}
1139
1140void acpigen_write_byte_buffer(uint8_t *arr, uint8_t size)
1141{
1142 uint8_t i;
1143
1144 acpigen_emit_byte(BUFFER_OP);
1145 acpigen_write_len_f();
Naresh G Solankic70cc4d2016-11-16 10:10:09 +05301146 acpigen_write_byte(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001147
1148 for (i = 0; i < size; i++)
1149 acpigen_emit_byte(arr[i]);
1150
1151 acpigen_pop_len();
1152}
1153
1154void acpigen_write_return_byte_buffer(uint8_t *arr, uint8_t size)
1155{
1156 acpigen_emit_byte(RETURN_OP);
1157 acpigen_write_byte_buffer(arr, size);
1158}
1159
1160void acpigen_write_return_singleton_buffer(uint8_t arg)
1161{
1162 acpigen_write_return_byte_buffer(&arg, 1);
1163}
1164
1165void acpigen_write_return_byte(uint8_t arg)
1166{
1167 acpigen_emit_byte(RETURN_OP);
1168 acpigen_write_byte(arg);
1169}
1170
Naresh G Solanki05abe432016-11-17 00:16:29 +05301171void acpigen_write_return_integer(uint64_t arg)
1172{
1173 acpigen_emit_byte(RETURN_OP);
1174 acpigen_write_integer(arg);
1175}
1176
1177void acpigen_write_return_string(const char *arg)
1178{
1179 acpigen_emit_byte(RETURN_OP);
1180 acpigen_write_string(arg);
1181}
1182
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301183void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1184 size_t count, void *arg)
1185{
1186 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1187 acpigen_write_dsm_uuid_arr(&id, 1);
1188}
1189
1190static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1191{
1192 size_t i;
1193
1194 /* If (LEqual (Local0, ToUUID(uuid))) */
1195 acpigen_write_if();
1196 acpigen_emit_byte(LEQUAL_OP);
1197 acpigen_emit_byte(LOCAL0_OP);
1198 acpigen_write_uuid(id->uuid);
1199
1200 /* ToInteger (Arg2, Local1) */
1201 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1202
1203 for (i = 0; i < id->count; i++) {
1204 /* If (LEqual (Local1, i)) */
1205 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1206
1207 /* Callback to write if handler. */
1208 if (id->callbacks[i])
1209 id->callbacks[i](id->arg);
1210
1211 acpigen_pop_len(); /* If */
1212 }
1213
1214 /* Default case: Return (Buffer (One) { 0x0 }) */
1215 acpigen_write_return_singleton_buffer(0x0);
1216
1217 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1218
1219}
1220
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001221/*
1222 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301223 * This function takes as input array of uuid for the device, set of callbacks
1224 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1225 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1226 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001227 *
1228 * Arguments passed into _DSM method:
1229 * Arg0 = UUID
1230 * Arg1 = Revision
1231 * Arg2 = Function index
1232 * Arg3 = Function specific arguments
1233 *
1234 * AML code generated would look like:
1235 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301236 * ToBuffer (Arg0, Local0)
1237 * If (LEqual (Local0, ToUUID(uuid))) {
1238 * ToInteger (Arg2, Local1)
1239 * If (LEqual (Local1, 0)) {
1240 * <acpigen by callback[0]>
1241 * }
1242 * ...
1243 * If (LEqual (Local1, n)) {
1244 * <acpigen by callback[n]>
1245 * }
1246 * Return (Buffer (One) { 0x0 })
1247 * }
1248 * ...
1249 * If (LEqual (Local0, ToUUID(uuidn))) {
1250 * ...
1251 * }
1252 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001253 * }
1254 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301255void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001256{
1257 size_t i;
1258
1259 /* Method (_DSM, 4, Serialized) */
1260 acpigen_write_method_serialized("_DSM", 0x4);
1261
1262 /* ToBuffer (Arg0, Local0) */
1263 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1264
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001265 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301266 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001267
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301268 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001269 acpigen_write_return_singleton_buffer(0x0);
1270
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001271 acpigen_pop_len(); /* Method _DSM */
1272}
1273
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001274/* Soc-implemented functions -- weak definitions. */
1275int __attribute__((weak)) acpigen_soc_read_rx_gpio(unsigned int gpio_num)
1276{
1277 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1278 acpigen_write_debug_string("read_rx_gpio not available");
1279 return -1;
1280}
1281
1282int __attribute__((weak)) acpigen_soc_get_tx_gpio(unsigned int gpio_num)
1283{
1284 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1285 acpigen_write_debug_string("get_tx_gpio not available");
1286 return -1;
1287}
1288
1289int __attribute__((weak)) acpigen_soc_set_tx_gpio(unsigned int gpio_num)
1290{
1291 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1292 acpigen_write_debug_string("set_tx_gpio not available");
1293 return -1;
1294}
1295
1296int __attribute__((weak)) acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
1297{
1298 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1299 acpigen_write_debug_string("clear_tx_gpio not available");
1300 return -1;
1301}