blob: e10d3077b23788b2ac8a26d0f0298a010019f1e7 [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>
Aaron Durbin64031672018-04-21 14:45:32 -060031#include <compiler.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000032#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000033#include <device/device.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000034
35static char *gencurrent;
36
37char *len_stack[ACPIGEN_LENSTACK_SIZE];
38int ltop = 0;
39
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010040void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000041{
42 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010043 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000044 acpigen_emit_byte(0);
45 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050046 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000047}
48
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010049void acpigen_pop_len(void)
50{
51 int len;
52 ASSERT(ltop > 0)
53 char *p = len_stack[--ltop];
54 len = gencurrent - p;
55 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050056 /* generate store length for 0xfffff max */
57 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010058 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050059 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010060
61}
62
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000063void acpigen_set_current(char *curr)
64{
65 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000066}
67
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000068char *acpigen_get_current(void)
69{
70 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000071}
72
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010073void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000074{
75 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000076}
77
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070078void acpigen_emit_ext_op(uint8_t op)
79{
80 acpigen_emit_byte(EXT_OP_PREFIX);
81 acpigen_emit_byte(op);
82}
83
Duncan Laurie9ccae752016-05-09 07:43:19 -070084void acpigen_emit_word(unsigned int data)
85{
86 acpigen_emit_byte(data & 0xff);
87 acpigen_emit_byte((data >> 8) & 0xff);
88}
89
90void acpigen_emit_dword(unsigned int data)
91{
92 acpigen_emit_byte(data & 0xff);
93 acpigen_emit_byte((data >> 8) & 0xff);
94 acpigen_emit_byte((data >> 16) & 0xff);
95 acpigen_emit_byte((data >> 24) & 0xff);
96}
97
Duncan Laurie85d80272016-07-02 19:53:54 -070098char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000099{
Duncan Laurie85d80272016-07-02 19:53:54 -0700100 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700101 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100102 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -0700103 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +0000104 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -0700105 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000106}
107
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100108void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000109{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700110 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000111 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000112}
113
Duncan Laurie9ccae752016-05-09 07:43:19 -0700114void acpigen_write_word(unsigned int data)
115{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700116 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700117 acpigen_emit_word(data);
118}
119
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100120void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000121{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700122 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700123 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000124}
125
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100126void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000127{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700128 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700129 acpigen_emit_dword(data & 0xffffffff);
130 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000131}
132
Duncan Laurief7c38762016-05-09 08:20:38 -0700133void acpigen_write_zero(void)
134{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700135 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700136}
137
138void acpigen_write_one(void)
139{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700140 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700141}
142
143void acpigen_write_ones(void)
144{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700145 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700146}
147
148void acpigen_write_integer(uint64_t data)
149{
150 if (data == 0)
151 acpigen_write_zero();
152 else if (data == 1)
153 acpigen_write_one();
154 else if (data <= 0xff)
155 acpigen_write_byte((unsigned char)data);
156 else if (data <= 0xffff)
157 acpigen_write_word((unsigned int)data);
158 else if (data <= 0xffffffff)
159 acpigen_write_dword((unsigned int)data);
160 else
161 acpigen_write_qword(data);
162}
163
164void acpigen_write_name_zero(const char *name)
165{
166 acpigen_write_name(name);
167 acpigen_write_one();
168}
169
170void acpigen_write_name_one(const char *name)
171{
172 acpigen_write_name(name);
173 acpigen_write_zero();
174}
175
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100176void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000177{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100178 acpigen_write_name(name);
179 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000180}
181
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100182void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000183{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100184 acpigen_write_name(name);
185 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000186}
187
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100188void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000189{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100190 acpigen_write_name(name);
191 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000192}
193
Duncan Laurief7c38762016-05-09 08:20:38 -0700194void acpigen_write_name_integer(const char *name, uint64_t val)
195{
196 acpigen_write_name(name);
197 acpigen_write_integer(val);
198}
199
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700200void acpigen_write_name_string(const char *name, const char *string)
201{
202 acpigen_write_name(name);
203 acpigen_write_string(string);
204}
205
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100206void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000207{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000208 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700209 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000210 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000211}
212
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700213void acpigen_emit_string(const char *string)
214{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200215 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700216 acpigen_emit_byte('\0'); /* NUL */
217}
218
219void acpigen_write_string(const char *string)
220{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700221 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700222 acpigen_emit_string(string);
223}
224
Duncan Laurie37319032016-05-26 12:47:05 -0700225void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
226{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800227 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700228
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700229 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700230 acpigen_write_name_string("_HID", hid);
231}
232
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100233/*
234 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600235 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
236 * and "By convention, when an ASL compiler pads a name shorter than 4
237 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100238 *
239 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
240 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000241
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700242static void acpigen_emit_simple_namestring(const char *name)
243{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100244 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000245 char ud[] = "____";
246 for (i = 0; i < 4; i++) {
247 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100248 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000249 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000250 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700251 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000252 }
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/*
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600342 Processor (\_PR.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200343 {
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),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600350 CONFIG_ACPI_CPU_STRING, (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
Nico Huber4d211ac2017-09-01 21:14:36 +0200357void acpigen_write_processor_package(const char *const name,
358 const unsigned int first_core,
359 const unsigned int core_count)
360{
361 unsigned int i;
362 char pscope[16];
363
364 acpigen_write_name(name);
365 acpigen_write_package(core_count);
366 for (i = first_core; i < first_core + core_count; ++i) {
367 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
368 acpigen_emit_namestring(pscope);
369 }
370 acpigen_pop_len();
371}
372
Naresh G Solanki8535c662016-10-25 00:51:24 +0530373/*
374 * Generate ACPI AML code for OperationRegion
375 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
376 * where rname is region name, space is region space, offset is region offset &
377 * len is region length.
378 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
379 */
380void acpigen_write_opregion(struct opregion *opreg)
381{
382 /* OpregionOp */
383 acpigen_emit_ext_op(OPREGION_OP);
384 /* NameString 4 chars only */
385 acpigen_emit_simple_namestring(opreg->name);
386 /* RegionSpace */
387 acpigen_emit_byte(opreg->regionspace);
388 /* RegionOffset & RegionLen, it can be byte word or double word */
389 acpigen_write_integer(opreg->regionoffset);
390 acpigen_write_integer(opreg->regionlen);
391}
392
Patrick Rudolph894977b2017-07-01 16:15:05 +0200393static void acpigen_write_field_length(uint32_t len)
394{
395 uint8_t i, j;
396 uint8_t emit[4];
397
398 i = 1;
399 if (len < 0x40) {
400 emit[0] = len & 0x3F;
401 } else {
402 emit[0] = len & 0xF;
403 len >>= 4;
404 while (len) {
405 emit[i] = len & 0xFF;
406 i++;
407 len >>= 8;
408 }
409 }
410 /* Update bit 7:6 : Number of bytes followed by emit[0] */
411 emit[0] |= (i - 1) << 6;
412
413 for (j = 0; j < i; j++)
414 acpigen_emit_byte(emit[j]);
415}
416
Naresh G Solanki8535c662016-10-25 00:51:24 +0530417static void acpigen_write_field_offset(uint32_t offset,
418 uint32_t current_bit_pos)
419{
420 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530421
422 if (offset < current_bit_pos) {
423 printk(BIOS_WARNING, "%s: Cannot move offset backward",
424 __func__);
425 return;
426 }
427
428 diff_bits = offset - current_bit_pos;
429 /* Upper limit */
430 if (diff_bits > 0xFFFFFFF) {
431 printk(BIOS_WARNING, "%s: Offset very large to encode",
432 __func__);
433 return;
434 }
435
Naresh G Solanki8535c662016-10-25 00:51:24 +0530436 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200437 acpigen_write_field_length(diff_bits);
438}
439
440static void acpigen_write_field_name(const char *name, uint32_t size)
441{
442 acpigen_emit_simple_namestring(name);
443 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530444}
445
446/*
447 * Generate ACPI AML code for Field
448 * Arg0: region name
449 * Arg1: Pointer to struct fieldlist.
450 * Arg2: no. of entries in Arg1
451 * Arg3: flags which indicate filed access type, lock rule & update rule.
452 * Example with fieldlist
453 * struct fieldlist l[] = {
454 * FIELDLIST_OFFSET(0x84),
455 * FIELDLIST_NAMESTR("PMCS", 2),
456 * };
457 * acpigen_write_field("UART", l ,ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
458 * FIELD_PRESERVE);
459 * Output:
460 * Field (UART, AnyAcc, NoLock, Preserve)
461 * {
462 * Offset (0x84),
463 * PMCS, 2
464 * }
465 */
466void acpigen_write_field(const char *name, struct fieldlist *l, size_t count,
467 uint8_t flags)
468{
469 uint16_t i;
470 uint32_t current_bit_pos = 0;
471
472 /* FieldOp */
473 acpigen_emit_ext_op(FIELD_OP);
474 /* Package Length */
475 acpigen_write_len_f();
476 /* NameString 4 chars only */
477 acpigen_emit_simple_namestring(name);
478 /* Field Flag */
479 acpigen_emit_byte(flags);
480
481 for (i = 0; i < count; i++) {
482 switch (l[i].type) {
483 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200484 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530485 current_bit_pos += l[i].bits;
486 break;
487 case OFFSET:
488 acpigen_write_field_offset(l[i].bits, current_bit_pos);
489 current_bit_pos = l[i].bits;
490 break;
491 default:
492 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
493 , __func__, l[i].type);
494 break;
495 }
496 }
497 acpigen_pop_len();
498}
499
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100500void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000501{
502/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200503 Name (_PCT, Package (0x02)
504 {
505 ResourceTemplate ()
506 {
507 Register (FFixedHW,
508 0x00, // Bit Width
509 0x00, // Bit Offset
510 0x0000000000000000, // Address
511 ,)
512 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000513
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200514 ResourceTemplate ()
515 {
516 Register (FFixedHW,
517 0x00, // Bit Width
518 0x00, // Bit Offset
519 0x0000000000000000, // Address
520 ,)
521 }
522 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000523*/
524 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700525 /* 00000030 "0._PCT.," */
526 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
527 /* 00000038 "........" */
528 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
529 /* 00000040 "........" */
530 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
531 /* 00000048 "....y..." */
532 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
533 /* 00000050 "........" */
534 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
535 /* 00000058 "........" */
536 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000537 0x00, 0x79, 0x00
538 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100539 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000540}
541
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100542void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200543{
544/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200545 Name (_PTC, Package (0x02)
546 {
547 ResourceTemplate ()
548 {
549 Register (FFixedHW,
550 0x00, // Bit Width
551 0x00, // Bit Offset
552 0x0000000000000000, // Address
553 ,)
554 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200555
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200556 ResourceTemplate ()
557 {
558 Register (FFixedHW,
559 0x00, // Bit Width
560 0x00, // Bit Offset
561 0x0000000000000000, // Address
562 ,)
563 }
564 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200565*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200566 acpi_addr_t addr = {
567 .space_id = ACPI_ADDRESS_SPACE_FIXED,
568 .bit_width = 0,
569 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800570 {
571 .resv = 0
572 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200573 .addrl = 0,
574 .addrh = 0,
575 };
576
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100577 acpigen_write_name("_PTC");
578 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200579
580 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700581 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200582
583 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700584 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200585
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100586 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200587}
588
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700589static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100590{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700591 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100592 acpigen_write_len_f();
593 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700594 acpigen_emit_byte(flags);
595}
596
597/* Method (name, nargs, NotSerialized) */
598void acpigen_write_method(const char *name, int nargs)
599{
600 __acpigen_write_method(name, (nargs & 7));
601}
602
603/* Method (name, nargs, Serialized) */
604void acpigen_write_method_serialized(const char *name, int nargs)
605{
606 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100607}
608
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100609void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100610{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700611 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100612 acpigen_write_len_f();
613 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100614}
615
Duncan Laurieabe2de82016-05-09 11:08:46 -0700616void acpigen_write_STA(uint8_t status)
617{
618 /*
619 * Method (_STA, 0, NotSerialized) { Return (status) }
620 */
621 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700622 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700623 acpigen_write_byte(status);
624 acpigen_pop_len();
625}
626
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100627/*
628 * Generates a func with max supported P-states.
629 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100630void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000631{
632/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200633 Method (_PPC, 0, NotSerialized)
634 {
635 Return (nr)
636 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000637*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100638 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700639 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000640 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100641 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100642 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000643}
644
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100645/*
646 * Generates a func with max supported P-states saved
647 * in the variable PPCM.
648 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100649void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700650{
651/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200652 Method (_PPC, 0, NotSerialized)
653 {
654 Return (PPCM)
655 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700656*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100657 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700658 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700659 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100660 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100661 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700662}
663
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100664void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200665{
666/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200667 // Sample _TPC method
668 Method (_TPC, 0, NotSerialized)
669 {
670 Return (\TLVL)
671 }
672*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100673 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700674 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100675 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100676 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200677}
678
Duncan Laurieabe2de82016-05-09 11:08:46 -0700679void acpigen_write_PRW(u32 wake, u32 level)
680{
681 /*
682 * Name (_PRW, Package () { wake, level }
683 */
684 acpigen_write_name("_PRW");
685 acpigen_write_package(2);
686 acpigen_write_integer(wake);
687 acpigen_write_integer(level);
688 acpigen_pop_len();
689}
690
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100691void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000692 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000693{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100694 acpigen_write_package(6);
695 acpigen_write_dword(coreFreq);
696 acpigen_write_dword(power);
697 acpigen_write_dword(transLat);
698 acpigen_write_dword(busmLat);
699 acpigen_write_dword(control);
700 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100701 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700702
703 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
704 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000705}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000706
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100707void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000708{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100709 acpigen_write_name("_PSD");
710 acpigen_write_package(1);
711 acpigen_write_package(5);
712 acpigen_write_byte(5); // 5 values
713 acpigen_write_byte(0); // revision 0
714 acpigen_write_dword(domain);
715 acpigen_write_dword(coordtype);
716 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100717 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100718 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000719}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000720
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100721void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200722{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100723 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700724 acpigen_write_register_resource(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100725 acpigen_write_dword(cstate->ctype);
726 acpigen_write_dword(cstate->latency);
727 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100728 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200729}
730
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100731void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200732{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100733 int i;
734 acpigen_write_name("_CST");
735 acpigen_write_package(nentries+1);
736 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200737
738 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100739 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200740
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100741 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200742}
743
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700744void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
745 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500746{
747 acpigen_write_name("_CSD");
748 acpigen_write_package(1);
749 acpigen_write_package(6);
750 acpigen_write_byte(6); // 6 values
751 acpigen_write_byte(0); // revision 0
752 acpigen_write_dword(domain);
753 acpigen_write_dword(coordtype);
754 acpigen_write_dword(numprocs);
755 acpigen_write_dword(index);
756 acpigen_pop_len();
757 acpigen_pop_len();
758}
759
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100760void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200761{
762/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200763 Sample _TSS package with 100% and 50% duty cycles
764 Name (_TSS, Package (0x02)
765 {
766 Package(){100, 1000, 0, 0x00, 0)
767 Package(){50, 520, 0, 0x18, 0)
768 })
769*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100770 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200771 acpi_tstate_t *tstate = tstate_list;
772
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100773 acpigen_write_name("_TSS");
774 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200775
776 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100777 acpigen_write_package(5);
778 acpigen_write_dword(tstate->percent);
779 acpigen_write_dword(tstate->power);
780 acpigen_write_dword(tstate->latency);
781 acpigen_write_dword(tstate->control);
782 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100783 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200784 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200785 }
786
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100787 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200788}
789
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100790void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200791{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100792 acpigen_write_name("_TSD");
793 acpigen_write_package(1);
794 acpigen_write_package(5);
795 acpigen_write_byte(5); // 5 values
796 acpigen_write_byte(0); // revision 0
797 acpigen_write_dword(domain);
798 acpigen_write_dword(coordtype);
799 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100800 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100801 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200802}
803
804
805
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100806void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000807{
808 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200809 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000810 * Byte 0:
811 * Bit7 : 1 => big item
812 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
813 */
814 acpigen_emit_byte(0x86);
815 /* Byte 1+2: length (0x0009) */
816 acpigen_emit_byte(0x09);
817 acpigen_emit_byte(0x00);
818 /* bit1-7 are ignored */
819 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700820 acpigen_emit_dword(base);
821 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000822}
823
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700824static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200825{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200826 acpigen_emit_byte(0x82); /* Register Descriptor */
827 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
828 acpigen_emit_byte(0x00); /* Register Length 15:8 */
829 acpigen_emit_byte(addr->space_id); /* Address Space ID */
830 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
831 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
832 acpigen_emit_byte(addr->resv); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700833 acpigen_emit_dword(addr->addrl); /* Register Address Low */
834 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200835}
836
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700837void acpigen_write_register_resource(const acpi_addr_t *addr)
838{
839 acpigen_write_resourcetemplate_header();
840 acpigen_write_register(addr);
841 acpigen_write_resourcetemplate_footer();
842}
843
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100844void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100845{
846 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200847 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100848 * Byte 0:
849 * Bit7 : 0 => small item
850 * Bit6-3: 0100 (0x4) => IRQ port descriptor
851 * Bit2-0: 010 (0x2) => 2 Bytes long
852 */
853 acpigen_emit_byte(0x22);
854 acpigen_emit_byte(mask & 0xff);
855 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100856}
857
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100858void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000859{
860 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200861 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000862 * Byte 0:
863 * Bit7 : 0 => small item
864 * Bit6-3: 1000 (0x8) => I/O port descriptor
865 * Bit2-0: 111 (0x7) => 7 Bytes long
866 */
867 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100868 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000869 /* bit1-7 are ignored */
870 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
871 /* minimum base address the device may be configured for */
872 acpigen_emit_byte(min & 0xff);
873 acpigen_emit_byte((min >> 8) & 0xff);
874 /* maximum base address the device may be configured for */
875 acpigen_emit_byte(max & 0xff);
876 acpigen_emit_byte((max >> 8) & 0xff);
877 /* alignment for min base */
878 acpigen_emit_byte(align & 0xff);
879 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000880}
881
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100882void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000883{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000884 /*
885 * A ResourceTemplate() is a Buffer() with a
886 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100887 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000888 * (small item 0xf, len 1)
889 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700890 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100891 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700892 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000893 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +0200894 /* Add 2 dummy bytes for the ACPI word (keep aligned with
895 the calclulation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100896 acpigen_emit_byte(0x00);
897 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000898}
899
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100900void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000901{
902 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100903 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000904 /*
905 * end tag (acpi 4.0 Section 6.4.2.8)
906 * 0x79 <checksum>
907 * 0x00 is treated as a good checksum according to the spec
908 * and is what iasl generates.
909 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100910 acpigen_emit_byte(0x79);
911 acpigen_emit_byte(0x00);
912
Nico Huber7d89ce32017-04-14 00:08:18 +0200913 /* Start counting past the 2-bytes length added in
914 acpigen_write_resourcetemplate() above. */
915 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100916
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000917 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100918 p[0] = len & 0xff;
919 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000920 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100921 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000922}
923
924static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
925 struct resource *res)
926{
927 acpigen_write_mem32fixed(0, res->base, res->size);
928}
929
930static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
931 struct resource *res)
932{
933 resource_t base = res->base;
934 resource_t size = res->size;
935 while (size > 0) {
936 resource_t sz = size > 255 ? 255 : size;
937 acpigen_write_io16(base, base, 0, sz, 1);
938 size -= sz;
939 base += sz;
940 }
941}
942
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100943void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000944{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100945 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000946
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100947 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000948 search_global_resources(
949 IORESOURCE_MEM | IORESOURCE_RESERVE,
950 IORESOURCE_MEM | IORESOURCE_RESERVE,
951 acpigen_add_mainboard_rsvd_mem32, 0);
952
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100953 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000954 search_global_resources(
955 IORESOURCE_IO | IORESOURCE_RESERVE,
956 IORESOURCE_IO | IORESOURCE_RESERVE,
957 acpigen_add_mainboard_rsvd_io, 0);
958
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100959 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000960}
961
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100962void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000963{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100964 acpigen_write_scope(scope);
965 acpigen_write_name(name);
966 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100967 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000968}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100969
970static int hex2bin(const char c)
971{
972 if (c >= 'A' && c <= 'F')
973 return c - 'A' + 10;
974 if (c >= 'a' && c <= 'f')
975 return c - 'a' + 10;
976 return c - '0';
977}
978
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100979void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100980{
981 u32 compact = 0;
982
983 /* Clamping individual values would be better but
984 there is a disagreement over what is a valid
985 EISA id, so accept anything and don't clamp,
986 parent code should create a valid EISAid.
987 */
988 compact |= (eisaid[0] - 'A' + 1) << 26;
989 compact |= (eisaid[1] - 'A' + 1) << 21;
990 compact |= (eisaid[2] - 'A' + 1) << 16;
991 compact |= hex2bin(eisaid[3]) << 12;
992 compact |= hex2bin(eisaid[4]) << 8;
993 compact |= hex2bin(eisaid[5]) << 4;
994 compact |= hex2bin(eisaid[6]);
995
996 acpigen_emit_byte(0xc);
997 acpigen_emit_byte((compact >> 24) & 0xff);
998 acpigen_emit_byte((compact >> 16) & 0xff);
999 acpigen_emit_byte((compact >> 8) & 0xff);
1000 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001001}
Duncan Laurie3829f232016-05-09 11:08:46 -07001002
1003/*
1004 * ToUUID(uuid)
1005 *
1006 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1007 * order for the bytes that make up a UUID Buffer object.
1008 * UUID byte order for input:
1009 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1010 * UUID byte order for output:
1011 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1012 */
1013#define UUID_LEN 16
1014void acpigen_write_uuid(const char *uuid)
1015{
1016 uint8_t buf[UUID_LEN];
1017 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1018 8, 9, 10, 11, 12, 13, 14, 15 };
1019
1020 /* Parse UUID string into bytes */
1021 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1022 return;
1023
1024 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001025 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001026 acpigen_write_len_f();
1027
1028 /* Buffer length in bytes */
1029 acpigen_write_word(UUID_LEN);
1030
1031 /* Output UUID in expected order */
1032 for (i = 0; i < UUID_LEN; i++)
1033 acpigen_emit_byte(buf[order[i]]);
1034
1035 acpigen_pop_len();
1036}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001037
1038/*
1039 * Name (_PRx, Package(One) { name })
1040 * ...
1041 * PowerResource (name, level, order)
1042 */
1043void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
1044 const char *dev_states[], size_t dev_states_count)
1045{
1046 int i;
1047 for (i = 0; i < dev_states_count; i++) {
1048 acpigen_write_name(dev_states[i]);
1049 acpigen_write_package(1);
1050 acpigen_emit_simple_namestring(name);
1051 acpigen_pop_len(); /* Package */
1052 }
1053
1054 acpigen_emit_ext_op(POWER_RES_OP);
1055
1056 acpigen_write_len_f();
1057
1058 acpigen_emit_simple_namestring(name);
1059 acpigen_emit_byte(level);
1060 acpigen_emit_word(order);
1061}
1062
1063/* Sleep (ms) */
1064void acpigen_write_sleep(uint64_t sleep_ms)
1065{
1066 acpigen_emit_ext_op(SLEEP_OP);
1067 acpigen_write_integer(sleep_ms);
1068}
1069
1070void acpigen_write_store(void)
1071{
1072 acpigen_emit_byte(STORE_OP);
1073}
1074
1075/* Store (src, dst) */
1076void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1077{
1078 acpigen_write_store();
1079 acpigen_emit_byte(src);
1080 acpigen_emit_byte(dst);
1081}
1082
1083/* Or (arg1, arg2, res) */
1084void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1085{
1086 acpigen_emit_byte(OR_OP);
1087 acpigen_emit_byte(arg1);
1088 acpigen_emit_byte(arg2);
1089 acpigen_emit_byte(res);
1090}
1091
1092/* And (arg1, arg2, res) */
1093void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1094{
1095 acpigen_emit_byte(AND_OP);
1096 acpigen_emit_byte(arg1);
1097 acpigen_emit_byte(arg2);
1098 acpigen_emit_byte(res);
1099}
1100
1101/* Not (arg, res) */
1102void acpigen_write_not(uint8_t arg, uint8_t res)
1103{
1104 acpigen_emit_byte(NOT_OP);
1105 acpigen_emit_byte(arg);
1106 acpigen_emit_byte(res);
1107}
1108
1109/* Store (str, DEBUG) */
1110void acpigen_write_debug_string(const char *str)
1111{
1112 acpigen_write_store();
1113 acpigen_write_string(str);
1114 acpigen_emit_ext_op(DEBUG_OP);
1115}
1116
1117/* Store (val, DEBUG) */
1118void acpigen_write_debug_integer(uint64_t val)
1119{
1120 acpigen_write_store();
1121 acpigen_write_integer(val);
1122 acpigen_emit_ext_op(DEBUG_OP);
1123}
1124
1125/* Store (op, DEBUG) */
1126void acpigen_write_debug_op(uint8_t op)
1127{
1128 acpigen_write_store();
1129 acpigen_emit_byte(op);
1130 acpigen_emit_ext_op(DEBUG_OP);
1131}
1132
1133void acpigen_write_if(void)
1134{
1135 acpigen_emit_byte(IF_OP);
1136 acpigen_write_len_f();
1137}
1138
1139/* If (And (arg1, arg2)) */
1140void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1141{
1142 acpigen_write_if();
1143 acpigen_emit_byte(AND_OP);
1144 acpigen_emit_byte(arg1);
1145 acpigen_emit_byte(arg2);
1146}
1147
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001148/*
1149 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1150 * operand1 is ACPI op and operand2 is an integer.
1151 *
1152 * If (Lequal (op, val))
1153 */
1154void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001155{
1156 acpigen_write_if();
1157 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001158 acpigen_emit_byte(op);
1159 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001160}
1161
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001162void acpigen_write_else(void)
1163{
1164 acpigen_emit_byte(ELSE_OP);
1165 acpigen_write_len_f();
1166}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001167
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001168void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1169{
1170 acpigen_emit_byte(TO_BUFFER_OP);
1171 acpigen_emit_byte(src);
1172 acpigen_emit_byte(dst);
1173}
1174
1175void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1176{
1177 acpigen_emit_byte(TO_INTEGER_OP);
1178 acpigen_emit_byte(src);
1179 acpigen_emit_byte(dst);
1180}
1181
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301182void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001183{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301184 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001185
1186 acpigen_emit_byte(BUFFER_OP);
1187 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301188 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001189
1190 for (i = 0; i < size; i++)
1191 acpigen_emit_byte(arr[i]);
1192
1193 acpigen_pop_len();
1194}
1195
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301196void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001197{
1198 acpigen_emit_byte(RETURN_OP);
1199 acpigen_write_byte_buffer(arr, size);
1200}
1201
1202void acpigen_write_return_singleton_buffer(uint8_t arg)
1203{
1204 acpigen_write_return_byte_buffer(&arg, 1);
1205}
1206
1207void acpigen_write_return_byte(uint8_t arg)
1208{
1209 acpigen_emit_byte(RETURN_OP);
1210 acpigen_write_byte(arg);
1211}
1212
Naresh G Solanki05abe432016-11-17 00:16:29 +05301213void acpigen_write_return_integer(uint64_t arg)
1214{
1215 acpigen_emit_byte(RETURN_OP);
1216 acpigen_write_integer(arg);
1217}
1218
1219void acpigen_write_return_string(const char *arg)
1220{
1221 acpigen_emit_byte(RETURN_OP);
1222 acpigen_write_string(arg);
1223}
1224
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001225void acpigen_write_upc(enum acpi_upc_type type)
1226{
1227 acpigen_write_name("_UPC");
1228 acpigen_write_package(4);
1229 /* Connectable */
1230 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1231 /* Type */
1232 acpigen_write_byte(type);
1233 /* Reserved0 */
1234 acpigen_write_zero();
1235 /* Reserved1 */
1236 acpigen_write_zero();
1237 acpigen_pop_len();
1238}
1239
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001240void acpigen_write_pld(const struct acpi_pld *pld)
1241{
1242 uint8_t buf[20];
1243
1244 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1245 return;
1246
1247 acpigen_write_name("_PLD");
1248 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
1249}
1250
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301251void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1252 size_t count, void *arg)
1253{
1254 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1255 acpigen_write_dsm_uuid_arr(&id, 1);
1256}
1257
1258static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1259{
1260 size_t i;
1261
1262 /* If (LEqual (Local0, ToUUID(uuid))) */
1263 acpigen_write_if();
1264 acpigen_emit_byte(LEQUAL_OP);
1265 acpigen_emit_byte(LOCAL0_OP);
1266 acpigen_write_uuid(id->uuid);
1267
1268 /* ToInteger (Arg2, Local1) */
1269 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1270
1271 for (i = 0; i < id->count; i++) {
1272 /* If (LEqual (Local1, i)) */
1273 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1274
1275 /* Callback to write if handler. */
1276 if (id->callbacks[i])
1277 id->callbacks[i](id->arg);
1278
1279 acpigen_pop_len(); /* If */
1280 }
1281
1282 /* Default case: Return (Buffer (One) { 0x0 }) */
1283 acpigen_write_return_singleton_buffer(0x0);
1284
1285 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1286
1287}
1288
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001289/*
1290 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301291 * This function takes as input array of uuid for the device, set of callbacks
1292 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1293 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1294 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001295 *
1296 * Arguments passed into _DSM method:
1297 * Arg0 = UUID
1298 * Arg1 = Revision
1299 * Arg2 = Function index
1300 * Arg3 = Function specific arguments
1301 *
1302 * AML code generated would look like:
1303 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301304 * ToBuffer (Arg0, Local0)
1305 * If (LEqual (Local0, ToUUID(uuid))) {
1306 * ToInteger (Arg2, Local1)
1307 * If (LEqual (Local1, 0)) {
1308 * <acpigen by callback[0]>
1309 * }
1310 * ...
1311 * If (LEqual (Local1, n)) {
1312 * <acpigen by callback[n]>
1313 * }
1314 * Return (Buffer (One) { 0x0 })
1315 * }
1316 * ...
1317 * If (LEqual (Local0, ToUUID(uuidn))) {
1318 * ...
1319 * }
1320 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001321 * }
1322 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301323void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001324{
1325 size_t i;
1326
1327 /* Method (_DSM, 4, Serialized) */
1328 acpigen_write_method_serialized("_DSM", 0x4);
1329
1330 /* ToBuffer (Arg0, Local0) */
1331 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1332
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001333 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301334 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001335
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301336 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001337 acpigen_write_return_singleton_buffer(0x0);
1338
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001339 acpigen_pop_len(); /* Method _DSM */
1340}
1341
Matt Delcob425bc82018-08-13 13:36:27 -07001342#define CPPC_PACKAGE_NAME "\\GCPC"
1343
1344void acpigen_write_CPPC_package(const struct cppc_config *config)
1345{
1346 u32 i;
1347 u32 max;
1348 switch (config->version) {
1349 case 1:
1350 max = CPPC_MAX_FIELDS_VER_1;
1351 break;
1352 case 2:
1353 max = CPPC_MAX_FIELDS_VER_2;
1354 break;
1355 case 3:
1356 max = CPPC_MAX_FIELDS_VER_3;
1357 break;
1358 default:
1359 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1360 config->version);
1361 return;
1362 }
1363 acpigen_write_name(CPPC_PACKAGE_NAME);
1364
1365 /* Adding 2 to account for length and version fields */
1366 acpigen_write_package(max + 2);
1367 acpigen_write_dword(max + 2);
1368
1369 acpigen_write_byte(config->version);
1370
1371 for (i = 0; i < max; ++i) {
1372 const acpi_addr_t *reg = &(config->regs[i]);
1373 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
1374 reg->bit_width == 32 && reg->access_size == 0) {
1375 acpigen_write_dword(reg->addrl);
1376 } else {
1377 acpigen_write_register_resource(reg);
1378 }
1379 }
1380 acpigen_pop_len();
1381}
1382
1383void acpigen_write_CPPC_method(void)
1384{
1385 acpigen_write_method("_CPC", 0);
1386 acpigen_emit_byte(RETURN_OP);
1387 acpigen_emit_namestring(CPPC_PACKAGE_NAME);
1388 acpigen_pop_len();
1389}
1390
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001391/*
1392 * Generate ACPI AML code for _ROM method.
1393 * This function takes as input ROM data and ROM length.
1394 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001395 * The ACPI spec isn't clear about what should happen at the end of the
1396 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1397 * bytes in the returned buffer with zeros.
1398 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001399 * Arguments passed into _DSM method:
1400 * Arg0 = Offset in Bytes
1401 * Arg1 = Bytes to return
1402 *
1403 * Example:
1404 * acpigen_write_rom(0xdeadbeef, 0x10000)
1405 *
1406 * AML code generated would look like:
1407 * Method (_ROM, 2, NotSerialized) {
1408 *
1409 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1410 * Field (ROMS, AnyAcc, NoLock, Preserve)
1411 * {
1412 * Offset (0),
1413 * RBF0, 0x80000
1414 * }
1415 *
1416 * Store (Arg0, Local0)
1417 * Store (Arg1, Local1)
1418 *
1419 * If (LGreater (Local1, 0x1000))
1420 * {
1421 * Store (0x1000, Local1)
1422 * }
1423 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001424 * Store (Local1, Local3)
1425 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001426 * If (LGreater (Local0, 0x10000))
1427 * {
1428 * Return(Buffer(Local1){0})
1429 * }
1430 *
1431 * If (LGreater (Local0, 0x0f000))
1432 * {
1433 * Subtract (0x10000, Local0, Local2)
1434 * If (LGreater (Local1, Local2))
1435 * {
1436 * Store (Local2, Local1)
1437 * }
1438 * }
1439 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001440 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001441 *
1442 * Multiply (Local0, 0x08, Local0)
1443 * Multiply (Local1, 0x08, Local1)
1444 *
1445 * CreateField (RBF0, Local0, Local1, TMPB)
1446 * Store (TMPB, ROM1)
1447 * Return (ROM1)
1448 * }
1449 */
1450
1451void acpigen_write_rom(void *bios, const size_t length)
1452{
1453 ASSERT(bios)
1454 ASSERT(length)
1455
1456 /* Method (_ROM, 2, NotSerialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001457 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001458
1459 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1460 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1461 (uintptr_t)bios, length);
1462 acpigen_write_opregion(&opreg);
1463
1464 struct fieldlist l[] = {
1465 FIELDLIST_OFFSET(0),
1466 FIELDLIST_NAMESTR("RBF0", 8 * length),
1467 };
1468
1469 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1470 * {
1471 * Offset (0),
1472 * RBF0, 0x80000
1473 * } */
1474 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1475 FIELD_NOLOCK | FIELD_PRESERVE);
1476
1477 /* Store (Arg0, Local0) */
1478 acpigen_write_store();
1479 acpigen_emit_byte(ARG0_OP);
1480 acpigen_emit_byte(LOCAL0_OP);
1481
1482 /* Store (Arg1, Local1) */
1483 acpigen_write_store();
1484 acpigen_emit_byte(ARG1_OP);
1485 acpigen_emit_byte(LOCAL1_OP);
1486
1487 /* ACPI SPEC requires to return at maximum 4KiB */
1488 /* If (LGreater (Local1, 0x1000)) */
1489 acpigen_write_if();
1490 acpigen_emit_byte(LGREATER_OP);
1491 acpigen_emit_byte(LOCAL1_OP);
1492 acpigen_write_integer(0x1000);
1493
1494 /* Store (0x1000, Local1) */
1495 acpigen_write_store();
1496 acpigen_write_integer(0x1000);
1497 acpigen_emit_byte(LOCAL1_OP);
1498
1499 /* Pop if */
1500 acpigen_pop_len();
1501
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001502 /* Store (Local1, Local3) */
1503 acpigen_write_store();
1504 acpigen_emit_byte(LOCAL1_OP);
1505 acpigen_emit_byte(LOCAL3_OP);
1506
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001507 /* If (LGreater (Local0, length)) */
1508 acpigen_write_if();
1509 acpigen_emit_byte(LGREATER_OP);
1510 acpigen_emit_byte(LOCAL0_OP);
1511 acpigen_write_integer(length);
1512
1513 /* Return(Buffer(Local1){0}) */
1514 acpigen_emit_byte(RETURN_OP);
1515 acpigen_emit_byte(BUFFER_OP);
1516 acpigen_write_len_f();
1517 acpigen_emit_byte(LOCAL1_OP);
1518 acpigen_emit_byte(0);
1519 acpigen_pop_len();
1520
1521 /* Pop if */
1522 acpigen_pop_len();
1523
1524 /* If (LGreater (Local0, length - 4096)) */
1525 acpigen_write_if();
1526 acpigen_emit_byte(LGREATER_OP);
1527 acpigen_emit_byte(LOCAL0_OP);
1528 acpigen_write_integer(length - 4096);
1529
1530 /* Subtract (length, Local0, Local2) */
1531 acpigen_emit_byte(SUBTRACT_OP);
1532 acpigen_write_integer(length);
1533 acpigen_emit_byte(LOCAL0_OP);
1534 acpigen_emit_byte(LOCAL2_OP);
1535
1536 /* If (LGreater (Local1, Local2)) */
1537 acpigen_write_if();
1538 acpigen_emit_byte(LGREATER_OP);
1539 acpigen_emit_byte(LOCAL1_OP);
1540 acpigen_emit_byte(LOCAL2_OP);
1541
1542 /* Store (Local2, Local1) */
1543 acpigen_write_store();
1544 acpigen_emit_byte(LOCAL2_OP);
1545 acpigen_emit_byte(LOCAL1_OP);
1546
1547 /* Pop if */
1548 acpigen_pop_len();
1549
1550 /* Pop if */
1551 acpigen_pop_len();
1552
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001553 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001554 acpigen_write_name("ROM1");
1555 acpigen_emit_byte(BUFFER_OP);
1556 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001557 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001558 acpigen_emit_byte(0);
1559 acpigen_pop_len();
1560
1561 /* Multiply (Local1, 0x08, Local1) */
1562 acpigen_emit_byte(MULTIPLY_OP);
1563 acpigen_emit_byte(LOCAL1_OP);
1564 acpigen_write_integer(0x08);
1565 acpigen_emit_byte(LOCAL1_OP);
1566
1567 /* Multiply (Local0, 0x08, Local0) */
1568 acpigen_emit_byte(MULTIPLY_OP);
1569 acpigen_emit_byte(LOCAL0_OP);
1570 acpigen_write_integer(0x08);
1571 acpigen_emit_byte(LOCAL0_OP);
1572
1573 /* CreateField (RBF0, Local0, Local1, TMPB) */
1574 acpigen_emit_ext_op(CREATEFIELD_OP);
1575 acpigen_emit_namestring("RBF0");
1576 acpigen_emit_byte(LOCAL0_OP);
1577 acpigen_emit_byte(LOCAL1_OP);
1578 acpigen_emit_namestring("TMPB");
1579
1580 /* Store (TMPB, ROM1) */
1581 acpigen_write_store();
1582 acpigen_emit_namestring("TMPB");
1583 acpigen_emit_namestring("ROM1");
1584
1585 /* Return (ROM1) */
1586 acpigen_emit_byte(RETURN_OP);
1587 acpigen_emit_namestring("ROM1");
1588
1589 /* Pop method */
1590 acpigen_pop_len();
1591}
1592
1593
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001594/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001595int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001596{
1597 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1598 acpigen_write_debug_string("read_rx_gpio not available");
1599 return -1;
1600}
1601
Aaron Durbin64031672018-04-21 14:45:32 -06001602int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001603{
1604 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1605 acpigen_write_debug_string("get_tx_gpio not available");
1606 return -1;
1607}
1608
Aaron Durbin64031672018-04-21 14:45:32 -06001609int __weak acpigen_soc_set_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001610{
1611 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1612 acpigen_write_debug_string("set_tx_gpio not available");
1613 return -1;
1614}
1615
Aaron Durbin64031672018-04-21 14:45:32 -06001616int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001617{
1618 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1619 acpigen_write_debug_string("clear_tx_gpio not available");
1620 return -1;
1621}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001622
1623/*
1624 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1625 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1626 * make callbacks into SoC acpigen code.
1627 *
1628 * Returns 0 on success and -1 on error.
1629 */
1630int acpigen_enable_tx_gpio(struct acpi_gpio *gpio)
1631{
1632 if (gpio->polarity == ACPI_GPIO_ACTIVE_HIGH)
1633 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1634 else
1635 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1636}
1637
1638int acpigen_disable_tx_gpio(struct acpi_gpio *gpio)
1639{
1640 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1641 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1642 else
1643 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1644}