blob: efc5a16195a3e42a27dbbbdca3bf7d7707ed4f56 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Rudolf Marek293b5f52009-02-01 18:35:15 +00002
Paul Menzel0f4c0e22013-02-22 12:33:08 +01003/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +00004#define ACPIGEN_LENSTACK_SIZE 10
5
Paul Menzel0f4c0e22013-02-22 12:33:08 +01006/*
Timothy Pearson83abd812015-06-08 19:35:06 -05007 * If you need to change this, change acpigen_write_len_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +01008 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +01009 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000010
Timothy Pearson83abd812015-06-08 19:35:06 -050011#define ACPIGEN_MAXLEN 0xfffff
Rudolf Marek293b5f52009-02-01 18:35:15 +000012
Duncan Laurie3829f232016-05-09 11:08:46 -070013#include <lib.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000014#include <string.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -070015#include <acpi/acpigen.h>
Elyes HAOUAScd4fe0f2019-03-29 17:12:15 +010016#include <assert.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000017#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000018#include <device/device.h>
Furquan Shaikh1a829232020-04-23 11:11:05 -070019#include <device/pci_def.h>
20#include <device/pci_type.h>
Duncan Laurie08a942f2020-04-29 12:13:14 -070021#include <device/soundwire.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000022
23static char *gencurrent;
24
25char *len_stack[ACPIGEN_LENSTACK_SIZE];
26int ltop = 0;
27
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010028void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000029{
30 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010031 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000032 acpigen_emit_byte(0);
33 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050034 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000035}
36
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010037void acpigen_pop_len(void)
38{
39 int len;
40 ASSERT(ltop > 0)
41 char *p = len_stack[--ltop];
42 len = gencurrent - p;
43 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050044 /* generate store length for 0xfffff max */
45 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010046 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050047 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010048
49}
50
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000051void acpigen_set_current(char *curr)
52{
53 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000054}
55
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000056char *acpigen_get_current(void)
57{
58 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000059}
60
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010061void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000062{
63 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000064}
65
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070066void acpigen_emit_ext_op(uint8_t op)
67{
68 acpigen_emit_byte(EXT_OP_PREFIX);
69 acpigen_emit_byte(op);
70}
71
Duncan Laurie9ccae752016-05-09 07:43:19 -070072void acpigen_emit_word(unsigned int data)
73{
74 acpigen_emit_byte(data & 0xff);
75 acpigen_emit_byte((data >> 8) & 0xff);
76}
77
78void acpigen_emit_dword(unsigned int data)
79{
80 acpigen_emit_byte(data & 0xff);
81 acpigen_emit_byte((data >> 8) & 0xff);
82 acpigen_emit_byte((data >> 16) & 0xff);
83 acpigen_emit_byte((data >> 24) & 0xff);
84}
85
Duncan Laurie85d80272016-07-02 19:53:54 -070086char *acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000087{
Duncan Laurie85d80272016-07-02 19:53:54 -070088 char *p;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070089 acpigen_emit_byte(PACKAGE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010090 acpigen_write_len_f();
Duncan Laurie85d80272016-07-02 19:53:54 -070091 p = acpigen_get_current();
Rudolf Marek293b5f52009-02-01 18:35:15 +000092 acpigen_emit_byte(nr_el);
Duncan Laurie85d80272016-07-02 19:53:54 -070093 return p;
Rudolf Marek293b5f52009-02-01 18:35:15 +000094}
95
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010096void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000097{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -070098 acpigen_emit_byte(BYTE_PREFIX);
Rudolf Marek293b5f52009-02-01 18:35:15 +000099 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000100}
101
Duncan Laurie9ccae752016-05-09 07:43:19 -0700102void acpigen_write_word(unsigned int data)
103{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700104 acpigen_emit_byte(WORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700105 acpigen_emit_word(data);
106}
107
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100108void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000109{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700110 acpigen_emit_byte(DWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700111 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000112}
113
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100114void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000115{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700116 acpigen_emit_byte(QWORD_PREFIX);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700117 acpigen_emit_dword(data & 0xffffffff);
118 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000119}
120
Duncan Laurief7c38762016-05-09 08:20:38 -0700121void acpigen_write_zero(void)
122{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700123 acpigen_emit_byte(ZERO_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700124}
125
126void acpigen_write_one(void)
127{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700128 acpigen_emit_byte(ONE_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700129}
130
131void acpigen_write_ones(void)
132{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700133 acpigen_emit_byte(ONES_OP);
Duncan Laurief7c38762016-05-09 08:20:38 -0700134}
135
136void acpigen_write_integer(uint64_t data)
137{
138 if (data == 0)
139 acpigen_write_zero();
140 else if (data == 1)
141 acpigen_write_one();
142 else if (data <= 0xff)
143 acpigen_write_byte((unsigned char)data);
144 else if (data <= 0xffff)
145 acpigen_write_word((unsigned int)data);
146 else if (data <= 0xffffffff)
147 acpigen_write_dword((unsigned int)data);
148 else
149 acpigen_write_qword(data);
150}
151
152void acpigen_write_name_zero(const char *name)
153{
154 acpigen_write_name(name);
155 acpigen_write_one();
156}
157
158void acpigen_write_name_one(const char *name)
159{
160 acpigen_write_name(name);
161 acpigen_write_zero();
162}
163
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100164void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000165{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100166 acpigen_write_name(name);
167 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000168}
169
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100170void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000171{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100172 acpigen_write_name(name);
173 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000174}
175
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100176void acpigen_write_name_qword(const char *name, uint64_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_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000180}
181
Duncan Laurief7c38762016-05-09 08:20:38 -0700182void acpigen_write_name_integer(const char *name, uint64_t val)
183{
184 acpigen_write_name(name);
185 acpigen_write_integer(val);
186}
187
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700188void acpigen_write_name_string(const char *name, const char *string)
189{
190 acpigen_write_name(name);
191 acpigen_write_string(string);
192}
193
Patrick Rudolph389c8272019-12-17 13:54:41 +0100194void acpigen_write_name_unicode(const char *name, const char *string)
195{
196 const size_t len = strlen(string) + 1;
197 acpigen_write_name(name);
198 acpigen_emit_byte(BUFFER_OP);
199 acpigen_write_len_f();
200 acpigen_write_integer(len);
201 for (size_t i = 0; i < len; i++) {
202 const char c = string[i];
203 /* Simple ASCII to UTF-16 conversion, replace non ASCII characters */
204 acpigen_emit_word(c >= 0 ? c : '?');
205 }
206 acpigen_pop_len();
207}
208
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100209void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000210{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000211 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700212 for (i = 0; i < size; i++)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000213 acpigen_emit_byte(data[i]);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000214}
215
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700216void acpigen_emit_string(const char *string)
217{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200218 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700219 acpigen_emit_byte('\0'); /* NUL */
220}
221
222void acpigen_write_string(const char *string)
223{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700224 acpigen_emit_byte(STRING_PREFIX);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700225 acpigen_emit_string(string);
226}
227
Duncan Laurie37319032016-05-26 12:47:05 -0700228void acpigen_write_coreboot_hid(enum coreboot_acpi_ids id)
229{
Joel Kitching3ab36b842018-03-08 12:09:32 +0800230 char hid[9]; /* BOOTxxxx */
Duncan Laurie37319032016-05-26 12:47:05 -0700231
Duncan Laurie02fdb3e2016-09-22 14:08:33 -0700232 snprintf(hid, sizeof(hid), "%.4s%04X", COREBOOT_ACPI_ID, id);
Duncan Laurie37319032016-05-26 12:47:05 -0700233 acpigen_write_name_string("_HID", hid);
234}
235
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100236/*
237 * The naming conventions for ACPI namespace names are a bit tricky as
Martin Roth0cd338e2016-07-29 14:07:30 -0600238 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
239 * and "By convention, when an ASL compiler pads a name shorter than 4
240 * characters, it is done so with trailing underscores ('_')".
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100241 *
242 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
243 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000244
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700245static void acpigen_emit_simple_namestring(const char *name)
246{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100247 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000248 char ud[] = "____";
249 for (i = 0; i < 4; i++) {
250 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100251 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000252 break;
Rudolf Marek3310d752009-06-21 20:26:13 +0000253 }
Lee Leahy0b5678f2017-03-16 16:01:40 -0700254 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000255 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000256}
257
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700258static void acpigen_emit_double_namestring(const char *name, int dotpos)
259{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700260 acpigen_emit_byte(DUAL_NAME_PREFIX);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100261 acpigen_emit_simple_namestring(name);
262 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000263}
264
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700265static void acpigen_emit_multi_namestring(const char *name)
266{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100267 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000268 unsigned char *pathlen;
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700269 acpigen_emit_byte(MULTI_NAME_PREFIX);
270 acpigen_emit_byte(ZERO_OP);
Rudolf Marek3310d752009-06-21 20:26:13 +0000271 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
272
273 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100274 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000275 /* find end or next entity */
276 while ((name[0] != '.') && (name[0] != '\0'))
277 name++;
278 /* forward to next */
279 if (name[0] == '.')
280 name++;
281 count++;
282 }
283
284 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000285}
286
287
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700288void acpigen_emit_namestring(const char *namepath)
289{
Rudolf Marek3310d752009-06-21 20:26:13 +0000290 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000291 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000292
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600293 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000294 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100295 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000296 namepath++;
297 }
298
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600299 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000300 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100301 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000302 namepath++;
303 }
304
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200305 /* If we have only \\ or only ^...^. Then we need to put a null
306 name (0x00). */
Elyes HAOUASdbf30672016-08-21 17:37:15 +0200307 if (namepath[0] == '\0') {
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700308 acpigen_emit_byte(ZERO_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100309 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200310 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000311
312 i = 0;
313 while (namepath[i] != '\0') {
314 if (namepath[i] == '.') {
315 dotcount++;
316 dotpos = i;
317 }
318 i++;
319 }
320
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700321 if (dotcount == 0)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100322 acpigen_emit_simple_namestring(namepath);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700323 else if (dotcount == 1)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100324 acpigen_emit_double_namestring(namepath, dotpos);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700325 else
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100326 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000327}
328
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100329void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000330{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700331 acpigen_emit_byte(NAME_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100332 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000333}
334
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100335void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000336{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700337 acpigen_emit_byte(SCOPE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100338 acpigen_write_len_f();
339 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000340}
Rudolf Marekf997b552009-02-14 15:40:23 +0000341
Duncan Laurie2fe74712020-06-01 17:36:56 -0700342void acpigen_get_package_op_element(uint8_t package_op, unsigned int element, uint8_t dest_op)
343{
344 /* <dest_op> = DeRefOf (<package_op>[<element]) */
345 acpigen_write_store();
346 acpigen_emit_byte(DEREF_OP);
347 acpigen_emit_byte(INDEX_OP);
348 acpigen_emit_byte(package_op);
349 acpigen_write_integer(element);
350 acpigen_emit_byte(ZERO_OP); /* Ignore Index() Destination */
351 acpigen_emit_byte(dest_op);
352}
353
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100354void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000355{
356/*
Christian Walterbe3979c2019-12-18 15:07:59 +0100357 Processor (\_SB.CPcpuindex, cpuindex, pblock_addr, pblock_len)
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200358 {
Rudolf Marekf997b552009-02-14 15:40:23 +0000359*/
360 char pscope[16];
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700361 acpigen_emit_ext_op(PROCESSOR_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100362 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000363
Elyes HAOUAS7d87e762016-10-03 22:11:07 +0200364 snprintf(pscope, sizeof(pscope),
Marc Jones7a2d4ea2017-08-25 18:54:23 -0600365 CONFIG_ACPI_CPU_STRING, (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100366 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000367 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700368 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000369 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000370}
371
Nico Huber4d211ac2017-09-01 21:14:36 +0200372void acpigen_write_processor_package(const char *const name,
373 const unsigned int first_core,
374 const unsigned int core_count)
375{
376 unsigned int i;
377 char pscope[16];
378
379 acpigen_write_name(name);
380 acpigen_write_package(core_count);
381 for (i = first_core; i < first_core + core_count; ++i) {
382 snprintf(pscope, sizeof(pscope), CONFIG_ACPI_CPU_STRING, i);
383 acpigen_emit_namestring(pscope);
384 }
385 acpigen_pop_len();
386}
387
Arthur Heymans8f055272018-11-28 13:18:57 +0100388/* Method to notify all CPU cores */
389void acpigen_write_processor_cnot(const unsigned int number_of_cores)
390{
391 int core_id;
392
Christian Walterbe3979c2019-12-18 15:07:59 +0100393 acpigen_write_method("\\_SB.CNOT", 1);
Arthur Heymans8f055272018-11-28 13:18:57 +0100394 for (core_id = 0; core_id < number_of_cores; core_id++) {
395 char buffer[DEVICE_PATH_MAX];
396 snprintf(buffer, sizeof(buffer), CONFIG_ACPI_CPU_STRING,
397 core_id);
398 acpigen_emit_byte(NOTIFY_OP);
399 acpigen_emit_namestring(buffer);
400 acpigen_emit_byte(ARG0_OP);
401 }
402 acpigen_pop_len();
403}
404
Naresh G Solanki8535c662016-10-25 00:51:24 +0530405/*
406 * Generate ACPI AML code for OperationRegion
407 * Arg0: Pointer to struct opregion opreg = OPREGION(rname, space, offset, len)
408 * where rname is region name, space is region space, offset is region offset &
409 * len is region length.
410 * OperationRegion(regionname, regionspace, regionoffset, regionlength)
411 */
412void acpigen_write_opregion(struct opregion *opreg)
413{
414 /* OpregionOp */
415 acpigen_emit_ext_op(OPREGION_OP);
416 /* NameString 4 chars only */
417 acpigen_emit_simple_namestring(opreg->name);
418 /* RegionSpace */
419 acpigen_emit_byte(opreg->regionspace);
420 /* RegionOffset & RegionLen, it can be byte word or double word */
421 acpigen_write_integer(opreg->regionoffset);
422 acpigen_write_integer(opreg->regionlen);
423}
424
Patrick Rudolph32bae492019-08-08 12:47:30 +0200425/*
426 * Generate ACPI AML code for Mutex
427 * Arg0: Pointer to name of mutex
428 * Arg1: Initial value of mutex
429 */
430void acpigen_write_mutex(const char *name, const uint8_t flags)
431{
432 /* MutexOp */
433 acpigen_emit_ext_op(MUTEX_OP);
434 /* NameString 4 chars only */
435 acpigen_emit_simple_namestring(name);
436 acpigen_emit_byte(flags);
437}
438
439void acpigen_write_acquire(const char *name, const uint16_t val)
440{
441 /* AcquireOp */
442 acpigen_emit_ext_op(ACQUIRE_OP);
443 /* NameString 4 chars only */
444 acpigen_emit_simple_namestring(name);
445 acpigen_emit_word(val);
446}
447
448void acpigen_write_release(const char *name)
449{
450 /* ReleaseOp */
451 acpigen_emit_ext_op(RELEASE_OP);
452 /* NameString 4 chars only */
453 acpigen_emit_simple_namestring(name);
454}
455
Patrick Rudolph894977b2017-07-01 16:15:05 +0200456static void acpigen_write_field_length(uint32_t len)
457{
458 uint8_t i, j;
459 uint8_t emit[4];
460
461 i = 1;
462 if (len < 0x40) {
463 emit[0] = len & 0x3F;
464 } else {
465 emit[0] = len & 0xF;
466 len >>= 4;
467 while (len) {
468 emit[i] = len & 0xFF;
469 i++;
470 len >>= 8;
471 }
472 }
473 /* Update bit 7:6 : Number of bytes followed by emit[0] */
474 emit[0] |= (i - 1) << 6;
475
476 for (j = 0; j < i; j++)
477 acpigen_emit_byte(emit[j]);
478}
479
Naresh G Solanki8535c662016-10-25 00:51:24 +0530480static void acpigen_write_field_offset(uint32_t offset,
481 uint32_t current_bit_pos)
482{
483 uint32_t diff_bits;
Naresh G Solanki8535c662016-10-25 00:51:24 +0530484
485 if (offset < current_bit_pos) {
486 printk(BIOS_WARNING, "%s: Cannot move offset backward",
487 __func__);
488 return;
489 }
490
491 diff_bits = offset - current_bit_pos;
492 /* Upper limit */
493 if (diff_bits > 0xFFFFFFF) {
494 printk(BIOS_WARNING, "%s: Offset very large to encode",
495 __func__);
496 return;
497 }
498
Naresh G Solanki8535c662016-10-25 00:51:24 +0530499 acpigen_emit_byte(0);
Patrick Rudolph894977b2017-07-01 16:15:05 +0200500 acpigen_write_field_length(diff_bits);
501}
502
503static void acpigen_write_field_name(const char *name, uint32_t size)
504{
505 acpigen_emit_simple_namestring(name);
506 acpigen_write_field_length(size);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530507}
508
509/*
510 * Generate ACPI AML code for Field
511 * Arg0: region name
512 * Arg1: Pointer to struct fieldlist.
513 * Arg2: no. of entries in Arg1
514 * Arg3: flags which indicate filed access type, lock rule & update rule.
515 * Example with fieldlist
516 * struct fieldlist l[] = {
517 * FIELDLIST_OFFSET(0x84),
518 * FIELDLIST_NAMESTR("PMCS", 2),
519 * };
Elyes HAOUASa342f392018-10-17 10:56:26 +0200520 * acpigen_write_field("UART", l, ARRAY_SIZE(l), FIELD_ANYACC | FIELD_NOLOCK |
Naresh G Solanki8535c662016-10-25 00:51:24 +0530521 * FIELD_PRESERVE);
522 * Output:
523 * Field (UART, AnyAcc, NoLock, Preserve)
524 * {
525 * Offset (0x84),
526 * PMCS, 2
527 * }
528 */
Furquan Shaikhf9392992020-04-27 23:18:12 -0700529void acpigen_write_field(const char *name, const struct fieldlist *l, size_t count,
Naresh G Solanki8535c662016-10-25 00:51:24 +0530530 uint8_t flags)
531{
532 uint16_t i;
533 uint32_t current_bit_pos = 0;
534
535 /* FieldOp */
536 acpigen_emit_ext_op(FIELD_OP);
537 /* Package Length */
538 acpigen_write_len_f();
539 /* NameString 4 chars only */
540 acpigen_emit_simple_namestring(name);
541 /* Field Flag */
542 acpigen_emit_byte(flags);
543
544 for (i = 0; i < count; i++) {
545 switch (l[i].type) {
546 case NAME_STRING:
Patrick Rudolph894977b2017-07-01 16:15:05 +0200547 acpigen_write_field_name(l[i].name, l[i].bits);
Naresh G Solanki8535c662016-10-25 00:51:24 +0530548 current_bit_pos += l[i].bits;
549 break;
550 case OFFSET:
551 acpigen_write_field_offset(l[i].bits, current_bit_pos);
552 current_bit_pos = l[i].bits;
553 break;
554 default:
555 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
556 , __func__, l[i].type);
557 break;
558 }
559 }
560 acpigen_pop_len();
561}
562
Patrick Rudolph34846ad2019-05-22 11:59:23 +0200563/*
564 * Generate ACPI AML code for IndexField
565 * Arg0: region name
566 * Arg1: Pointer to struct fieldlist.
567 * Arg2: no. of entries in Arg1
568 * Arg3: flags which indicate filed access type, lock rule & update rule.
569 * Example with fieldlist
570 * struct fieldlist l[] = {
571 * FIELDLIST_OFFSET(0x84),
572 * FIELDLIST_NAMESTR("PMCS", 2),
573 * };
574 * acpigen_write_field("IDX", "DATA" l, ARRAY_SIZE(l), FIELD_ANYACC |
575 * FIELD_NOLOCK |
576 * FIELD_PRESERVE);
577 * Output:
578 * IndexField (IDX, DATA, AnyAcc, NoLock, Preserve)
579 * {
580 * Offset (0x84),
581 * PMCS, 2
582 * }
583 */
584void acpigen_write_indexfield(const char *idx, const char *data,
585 struct fieldlist *l, size_t count, uint8_t flags)
586{
587 uint16_t i;
588 uint32_t current_bit_pos = 0;
589
590 /* FieldOp */
591 acpigen_emit_ext_op(INDEX_FIELD_OP);
592 /* Package Length */
593 acpigen_write_len_f();
594 /* NameString 4 chars only */
595 acpigen_emit_simple_namestring(idx);
596 /* NameString 4 chars only */
597 acpigen_emit_simple_namestring(data);
598 /* Field Flag */
599 acpigen_emit_byte(flags);
600
601 for (i = 0; i < count; i++) {
602 switch (l[i].type) {
603 case NAME_STRING:
604 acpigen_write_field_name(l[i].name, l[i].bits);
605 current_bit_pos += l[i].bits;
606 break;
607 case OFFSET:
608 acpigen_write_field_offset(l[i].bits, current_bit_pos);
609 current_bit_pos = l[i].bits;
610 break;
611 default:
612 printk(BIOS_ERR, "%s: Invalid field type 0x%X\n"
613 , __func__, l[i].type);
614 break;
615 }
616 }
617 acpigen_pop_len();
618}
619
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100620void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000621{
622/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200623 Name (_PCT, Package (0x02)
624 {
625 ResourceTemplate ()
626 {
627 Register (FFixedHW,
628 0x00, // Bit Width
629 0x00, // Bit Offset
630 0x0000000000000000, // Address
631 ,)
632 },
Rudolf Marekf997b552009-02-14 15:40:23 +0000633
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200634 ResourceTemplate ()
635 {
636 Register (FFixedHW,
637 0x00, // Bit Width
638 0x00, // Bit Offset
639 0x0000000000000000, // Address
640 ,)
641 }
642 })
Rudolf Marekf997b552009-02-14 15:40:23 +0000643*/
644 static char stream[] = {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700645 /* 00000030 "0._PCT.," */
646 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,
647 /* 00000038 "........" */
648 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00,
649 /* 00000040 "........" */
650 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
651 /* 00000048 "....y..." */
652 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14,
653 /* 00000050 "........" */
654 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00,
655 /* 00000058 "........" */
656 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Rudolf Marekf997b552009-02-14 15:40:23 +0000657 0x00, 0x79, 0x00
658 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100659 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000660}
661
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100662void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200663{
664/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200665 Name (_PTC, Package (0x02)
666 {
667 ResourceTemplate ()
668 {
669 Register (FFixedHW,
670 0x00, // Bit Width
671 0x00, // Bit Offset
672 0x0000000000000000, // Address
673 ,)
674 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200675
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200676 ResourceTemplate ()
677 {
678 Register (FFixedHW,
679 0x00, // Bit Width
680 0x00, // Bit Offset
681 0x0000000000000000, // Address
682 ,)
683 }
684 })
Stefan Reinauer39205c62012-04-27 21:49:28 +0200685*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200686 acpi_addr_t addr = {
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100687 .space_id = ACPI_ADDRESS_SPACE_FIXED,
688 .bit_width = 0,
689 .bit_offset = 0,
690 .access_size = 0,
691 .addrl = 0,
692 .addrh = 0,
Stefan Reinauer39205c62012-04-27 21:49:28 +0200693 };
694
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100695 acpigen_write_name("_PTC");
696 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200697
698 /* ControlRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700699 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200700
701 /* StatusRegister */
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700702 acpigen_write_register_resource(&addr);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200703
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100704 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200705}
706
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700707static void __acpigen_write_method(const char *name, uint8_t flags)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100708{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700709 acpigen_emit_byte(METHOD_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100710 acpigen_write_len_f();
711 acpigen_emit_namestring(name);
Furquan Shaikhfcc7a042016-10-20 23:12:38 -0700712 acpigen_emit_byte(flags);
713}
714
715/* Method (name, nargs, NotSerialized) */
716void acpigen_write_method(const char *name, int nargs)
717{
718 __acpigen_write_method(name, (nargs & 7));
719}
720
721/* Method (name, nargs, Serialized) */
722void acpigen_write_method_serialized(const char *name, int nargs)
723{
724 __acpigen_write_method(name, (nargs & 7) | (1 << 3));
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100725}
726
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100727void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100728{
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700729 acpigen_emit_ext_op(DEVICE_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100730 acpigen_write_len_f();
731 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100732}
733
Duncan Laurieabe2de82016-05-09 11:08:46 -0700734void acpigen_write_STA(uint8_t status)
735{
736 /*
737 * Method (_STA, 0, NotSerialized) { Return (status) }
738 */
739 acpigen_write_method("_STA", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700740 acpigen_emit_byte(RETURN_OP);
Duncan Laurieabe2de82016-05-09 11:08:46 -0700741 acpigen_write_byte(status);
742 acpigen_pop_len();
743}
744
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100745/*
746 * Generates a func with max supported P-states.
747 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100748void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000749{
750/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200751 Method (_PPC, 0, NotSerialized)
752 {
753 Return (nr)
754 }
Rudolf Marekf997b552009-02-14 15:40:23 +0000755*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100756 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700757 acpigen_emit_byte(RETURN_OP);
Rudolf Marekf997b552009-02-14 15:40:23 +0000758 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100759 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100760 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000761}
762
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100763/*
764 * Generates a func with max supported P-states saved
765 * in the variable PPCM.
766 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100767void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700768{
769/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200770 Method (_PPC, 0, NotSerialized)
771 {
772 Return (PPCM)
773 }
Duncan Laurie0eefa002012-07-16 12:11:53 -0700774*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100775 acpigen_write_method("_PPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700776 acpigen_emit_byte(RETURN_OP);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700777 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100778 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100779 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700780}
781
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100782void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200783{
784/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200785 // Sample _TPC method
786 Method (_TPC, 0, NotSerialized)
787 {
788 Return (\TLVL)
789 }
790*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100791 acpigen_write_method("_TPC", 0);
Furquan Shaikhe4e9b942016-10-20 23:09:05 -0700792 acpigen_emit_byte(RETURN_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100793 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100794 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200795}
796
Duncan Laurieabe2de82016-05-09 11:08:46 -0700797void acpigen_write_PRW(u32 wake, u32 level)
798{
799 /*
800 * Name (_PRW, Package () { wake, level }
801 */
802 acpigen_write_name("_PRW");
803 acpigen_write_package(2);
804 acpigen_write_integer(wake);
805 acpigen_write_integer(level);
806 acpigen_pop_len();
807}
808
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100809void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000810 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000811{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100812 acpigen_write_package(6);
813 acpigen_write_dword(coreFreq);
814 acpigen_write_dword(power);
815 acpigen_write_dword(transLat);
816 acpigen_write_dword(busmLat);
817 acpigen_write_dword(control);
818 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100819 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700820
821 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
822 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000823}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000824
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100825void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000826{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100827 acpigen_write_name("_PSD");
828 acpigen_write_package(1);
829 acpigen_write_package(5);
830 acpigen_write_byte(5); // 5 values
831 acpigen_write_byte(0); // revision 0
832 acpigen_write_dword(domain);
833 acpigen_write_dword(coordtype);
834 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100835 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100836 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000837}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000838
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100839void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200840{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100841 acpigen_write_package(4);
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700842 acpigen_write_register_resource(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100843 acpigen_write_dword(cstate->ctype);
844 acpigen_write_dword(cstate->latency);
845 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100846 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200847}
848
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100849void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200850{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100851 int i;
852 acpigen_write_name("_CST");
853 acpigen_write_package(nentries+1);
854 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200855
856 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100857 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200858
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100859 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200860}
861
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700862void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype,
863 u32 index)
Timothy Pearson83abd812015-06-08 19:35:06 -0500864{
865 acpigen_write_name("_CSD");
866 acpigen_write_package(1);
867 acpigen_write_package(6);
868 acpigen_write_byte(6); // 6 values
869 acpigen_write_byte(0); // revision 0
870 acpigen_write_dword(domain);
871 acpigen_write_dword(coordtype);
872 acpigen_write_dword(numprocs);
873 acpigen_write_dword(index);
874 acpigen_pop_len();
875 acpigen_pop_len();
876}
877
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100878void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200879{
880/*
Elyes HAOUAS6b727872016-09-03 08:28:48 +0200881 Sample _TSS package with 100% and 50% duty cycles
882 Name (_TSS, Package (0x02)
883 {
884 Package(){100, 1000, 0, 0x00, 0)
885 Package(){50, 520, 0, 0x18, 0)
886 })
887*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100888 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200889 acpi_tstate_t *tstate = tstate_list;
890
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100891 acpigen_write_name("_TSS");
892 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200893
894 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100895 acpigen_write_package(5);
896 acpigen_write_dword(tstate->percent);
897 acpigen_write_dword(tstate->power);
898 acpigen_write_dword(tstate->latency);
899 acpigen_write_dword(tstate->control);
900 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100901 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200902 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200903 }
904
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100905 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200906}
907
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100908void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200909{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100910 acpigen_write_name("_TSD");
911 acpigen_write_package(1);
912 acpigen_write_package(5);
913 acpigen_write_byte(5); // 5 values
914 acpigen_write_byte(0); // revision 0
915 acpigen_write_dword(domain);
916 acpigen_write_dword(coordtype);
917 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100918 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100919 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200920}
921
922
923
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100924void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000925{
926 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200927 * ACPI 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000928 * Byte 0:
929 * Bit7 : 1 => big item
930 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
931 */
932 acpigen_emit_byte(0x86);
933 /* Byte 1+2: length (0x0009) */
934 acpigen_emit_byte(0x09);
935 acpigen_emit_byte(0x00);
936 /* bit1-7 are ignored */
937 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700938 acpigen_emit_dword(base);
939 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000940}
941
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700942static void acpigen_write_register(const acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200943{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200944 acpigen_emit_byte(0x82); /* Register Descriptor */
945 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
946 acpigen_emit_byte(0x00); /* Register Length 15:8 */
947 acpigen_emit_byte(addr->space_id); /* Address Space ID */
948 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
949 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
Elyes HAOUAS8ee161d2019-03-03 12:49:56 +0100950 acpigen_emit_byte(addr->access_size); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700951 acpigen_emit_dword(addr->addrl); /* Register Address Low */
952 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200953}
954
Matt Delcoc3f9d7a2018-07-27 14:01:05 -0700955void acpigen_write_register_resource(const acpi_addr_t *addr)
956{
957 acpigen_write_resourcetemplate_header();
958 acpigen_write_register(addr);
959 acpigen_write_resourcetemplate_footer();
960}
961
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100962void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100963{
964 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200965 * ACPI 3.0b section 6.4.2.1: IRQ Descriptor
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100966 * Byte 0:
967 * Bit7 : 0 => small item
968 * Bit6-3: 0100 (0x4) => IRQ port descriptor
969 * Bit2-0: 010 (0x2) => 2 Bytes long
970 */
971 acpigen_emit_byte(0x22);
972 acpigen_emit_byte(mask & 0xff);
973 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100974}
975
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100976void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000977{
978 /*
Elyes HAOUAS2078e752016-08-21 10:41:44 +0200979 * ACPI 4.0 section 6.4.2.6: I/O Port Descriptor
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000980 * Byte 0:
981 * Bit7 : 0 => small item
982 * Bit6-3: 1000 (0x8) => I/O port descriptor
983 * Bit2-0: 111 (0x7) => 7 Bytes long
984 */
985 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100986 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000987 /* bit1-7 are ignored */
988 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
989 /* minimum base address the device may be configured for */
990 acpigen_emit_byte(min & 0xff);
991 acpigen_emit_byte((min >> 8) & 0xff);
992 /* maximum base address the device may be configured for */
993 acpigen_emit_byte(max & 0xff);
994 acpigen_emit_byte((max >> 8) & 0xff);
995 /* alignment for min base */
996 acpigen_emit_byte(align & 0xff);
997 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000998}
999
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001000void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001001{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001002 /*
1003 * A ResourceTemplate() is a Buffer() with a
1004 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001005 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001006 * (small item 0xf, len 1)
1007 */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001008 acpigen_emit_byte(BUFFER_OP);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001009 acpigen_write_len_f();
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001010 acpigen_emit_byte(WORD_PREFIX);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001011 len_stack[ltop++] = acpigen_get_current();
Nico Huber7d89ce32017-04-14 00:08:18 +02001012 /* Add 2 dummy bytes for the ACPI word (keep aligned with
Elyes HAOUAS6716bab2020-01-09 21:29:25 +01001013 the calculation in acpigen_write_resourcetemplate() below). */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001014 acpigen_emit_byte(0x00);
1015 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001016}
1017
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001018void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001019{
1020 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001021 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001022 /*
1023 * end tag (acpi 4.0 Section 6.4.2.8)
1024 * 0x79 <checksum>
1025 * 0x00 is treated as a good checksum according to the spec
1026 * and is what iasl generates.
1027 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001028 acpigen_emit_byte(0x79);
1029 acpigen_emit_byte(0x00);
1030
Nico Huber7d89ce32017-04-14 00:08:18 +02001031 /* Start counting past the 2-bytes length added in
1032 acpigen_write_resourcetemplate() above. */
1033 len = acpigen_get_current() - (p + 2);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001034
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001035 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +01001036 p[0] = len & 0xff;
1037 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001038 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001039 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001040}
1041
1042static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
1043 struct resource *res)
1044{
1045 acpigen_write_mem32fixed(0, res->base, res->size);
1046}
1047
1048static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
1049 struct resource *res)
1050{
1051 resource_t base = res->base;
1052 resource_t size = res->size;
1053 while (size > 0) {
1054 resource_t sz = size > 255 ? 255 : size;
1055 acpigen_write_io16(base, base, 0, sz, 1);
1056 size -= sz;
1057 base += sz;
1058 }
1059}
1060
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001061void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001062{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001063 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001064
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001065 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001066 search_global_resources(
1067 IORESOURCE_MEM | IORESOURCE_RESERVE,
1068 IORESOURCE_MEM | IORESOURCE_RESERVE,
1069 acpigen_add_mainboard_rsvd_mem32, 0);
1070
Paul Menzel0f4c0e22013-02-22 12:33:08 +01001071 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001072 search_global_resources(
1073 IORESOURCE_IO | IORESOURCE_RESERVE,
1074 IORESOURCE_IO | IORESOURCE_RESERVE,
1075 acpigen_add_mainboard_rsvd_io, 0);
1076
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001077 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001078}
1079
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001080void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001081{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001082 acpigen_write_scope(scope);
1083 acpigen_write_name(name);
1084 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +01001085 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +00001086}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001087
1088static int hex2bin(const char c)
1089{
1090 if (c >= 'A' && c <= 'F')
1091 return c - 'A' + 10;
1092 if (c >= 'a' && c <= 'f')
1093 return c - 'a' + 10;
1094 return c - '0';
1095}
1096
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +01001097void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001098{
1099 u32 compact = 0;
1100
1101 /* Clamping individual values would be better but
1102 there is a disagreement over what is a valid
1103 EISA id, so accept anything and don't clamp,
1104 parent code should create a valid EISAid.
1105 */
1106 compact |= (eisaid[0] - 'A' + 1) << 26;
1107 compact |= (eisaid[1] - 'A' + 1) << 21;
1108 compact |= (eisaid[2] - 'A' + 1) << 16;
1109 compact |= hex2bin(eisaid[3]) << 12;
1110 compact |= hex2bin(eisaid[4]) << 8;
1111 compact |= hex2bin(eisaid[5]) << 4;
1112 compact |= hex2bin(eisaid[6]);
1113
1114 acpigen_emit_byte(0xc);
1115 acpigen_emit_byte((compact >> 24) & 0xff);
1116 acpigen_emit_byte((compact >> 16) & 0xff);
1117 acpigen_emit_byte((compact >> 8) & 0xff);
1118 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +01001119}
Duncan Laurie3829f232016-05-09 11:08:46 -07001120
1121/*
1122 * ToUUID(uuid)
1123 *
1124 * ACPI 6.1 Section 19.6.136 table 19-385 defines a special output
1125 * order for the bytes that make up a UUID Buffer object.
1126 * UUID byte order for input:
1127 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1128 * UUID byte order for output:
1129 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
1130 */
1131#define UUID_LEN 16
1132void acpigen_write_uuid(const char *uuid)
1133{
1134 uint8_t buf[UUID_LEN];
1135 size_t i, order[UUID_LEN] = { 3, 2, 1, 0, 5, 4, 7, 6,
1136 8, 9, 10, 11, 12, 13, 14, 15 };
1137
1138 /* Parse UUID string into bytes */
1139 if (hexstrtobin(uuid, buf, UUID_LEN) < UUID_LEN)
1140 return;
1141
1142 /* BufferOp */
Furquan Shaikhe4e9b942016-10-20 23:09:05 -07001143 acpigen_emit_byte(BUFFER_OP);
Duncan Laurie3829f232016-05-09 11:08:46 -07001144 acpigen_write_len_f();
1145
1146 /* Buffer length in bytes */
1147 acpigen_write_word(UUID_LEN);
1148
1149 /* Output UUID in expected order */
1150 for (i = 0; i < UUID_LEN; i++)
1151 acpigen_emit_byte(buf[order[i]]);
1152
1153 acpigen_pop_len();
1154}
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001155
1156/*
1157 * Name (_PRx, Package(One) { name })
1158 * ...
1159 * PowerResource (name, level, order)
1160 */
1161void acpigen_write_power_res(const char *name, uint8_t level, uint16_t order,
Furquan Shaikhdc782752020-04-30 22:49:39 -07001162 const char * const dev_states[], size_t dev_states_count)
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001163{
Elyes HAOUAScca6e002019-06-26 12:12:00 +02001164 size_t i;
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001165 for (i = 0; i < dev_states_count; i++) {
1166 acpigen_write_name(dev_states[i]);
1167 acpigen_write_package(1);
1168 acpigen_emit_simple_namestring(name);
1169 acpigen_pop_len(); /* Package */
1170 }
1171
1172 acpigen_emit_ext_op(POWER_RES_OP);
1173
1174 acpigen_write_len_f();
1175
1176 acpigen_emit_simple_namestring(name);
1177 acpigen_emit_byte(level);
1178 acpigen_emit_word(order);
1179}
1180
1181/* Sleep (ms) */
1182void acpigen_write_sleep(uint64_t sleep_ms)
1183{
1184 acpigen_emit_ext_op(SLEEP_OP);
1185 acpigen_write_integer(sleep_ms);
1186}
1187
1188void acpigen_write_store(void)
1189{
1190 acpigen_emit_byte(STORE_OP);
1191}
1192
1193/* Store (src, dst) */
1194void acpigen_write_store_ops(uint8_t src, uint8_t dst)
1195{
1196 acpigen_write_store();
1197 acpigen_emit_byte(src);
1198 acpigen_emit_byte(dst);
1199}
1200
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001201/* Store (src, "namestr") */
1202void acpigen_write_store_op_to_namestr(uint8_t src, const char *dst)
1203{
1204 acpigen_write_store();
1205 acpigen_emit_byte(src);
1206 acpigen_emit_namestring(dst);
1207}
1208
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001209/* Or (arg1, arg2, res) */
1210void acpigen_write_or(uint8_t arg1, uint8_t arg2, uint8_t res)
1211{
1212 acpigen_emit_byte(OR_OP);
1213 acpigen_emit_byte(arg1);
1214 acpigen_emit_byte(arg2);
1215 acpigen_emit_byte(res);
1216}
1217
Rajat Jain310623b2020-02-26 11:02:53 -08001218/* Xor (arg1, arg2, res) */
1219void acpigen_write_xor(uint8_t arg1, uint8_t arg2, uint8_t res)
1220{
1221 acpigen_emit_byte(XOR_OP);
1222 acpigen_emit_byte(arg1);
1223 acpigen_emit_byte(arg2);
1224 acpigen_emit_byte(res);
1225}
1226
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001227/* And (arg1, arg2, res) */
1228void acpigen_write_and(uint8_t arg1, uint8_t arg2, uint8_t res)
1229{
1230 acpigen_emit_byte(AND_OP);
1231 acpigen_emit_byte(arg1);
1232 acpigen_emit_byte(arg2);
1233 acpigen_emit_byte(res);
1234}
1235
1236/* Not (arg, res) */
1237void acpigen_write_not(uint8_t arg, uint8_t res)
1238{
1239 acpigen_emit_byte(NOT_OP);
1240 acpigen_emit_byte(arg);
1241 acpigen_emit_byte(res);
1242}
1243
1244/* Store (str, DEBUG) */
1245void acpigen_write_debug_string(const char *str)
1246{
1247 acpigen_write_store();
1248 acpigen_write_string(str);
1249 acpigen_emit_ext_op(DEBUG_OP);
1250}
1251
1252/* Store (val, DEBUG) */
1253void acpigen_write_debug_integer(uint64_t val)
1254{
1255 acpigen_write_store();
1256 acpigen_write_integer(val);
1257 acpigen_emit_ext_op(DEBUG_OP);
1258}
1259
1260/* Store (op, DEBUG) */
1261void acpigen_write_debug_op(uint8_t op)
1262{
1263 acpigen_write_store();
1264 acpigen_emit_byte(op);
1265 acpigen_emit_ext_op(DEBUG_OP);
1266}
1267
1268void acpigen_write_if(void)
1269{
1270 acpigen_emit_byte(IF_OP);
1271 acpigen_write_len_f();
1272}
1273
1274/* If (And (arg1, arg2)) */
1275void acpigen_write_if_and(uint8_t arg1, uint8_t arg2)
1276{
1277 acpigen_write_if();
1278 acpigen_emit_byte(AND_OP);
1279 acpigen_emit_byte(arg1);
1280 acpigen_emit_byte(arg2);
1281}
1282
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001283/*
Duncan Laurie2fe74712020-06-01 17:36:56 -07001284 * Generates ACPI code for checking if operand1 and operand2 are equal.
1285 * Both operand1 and operand2 are ACPI ops.
1286 *
1287 * If (Lequal (op,1 op2))
1288 */
1289void acpigen_write_if_lequal_op_op(uint8_t op1, uint8_t op2)
1290{
1291 acpigen_write_if();
1292 acpigen_emit_byte(LEQUAL_OP);
1293 acpigen_emit_byte(op1);
1294 acpigen_emit_byte(op2);
1295}
1296
1297/*
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001298 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1299 * operand1 is ACPI op and operand2 is an integer.
1300 *
1301 * If (Lequal (op, val))
1302 */
1303void acpigen_write_if_lequal_op_int(uint8_t op, uint64_t val)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001304{
1305 acpigen_write_if();
1306 acpigen_emit_byte(LEQUAL_OP);
Furquan Shaikh1fc6bb92016-11-14 14:16:26 -08001307 acpigen_emit_byte(op);
1308 acpigen_write_integer(val);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001309}
1310
Furquan Shaikh2c213d32020-04-27 23:06:30 -07001311/*
1312 * Generates ACPI code for checking if operand1 and operand2 are equal, where,
1313 * operand1 is namestring and operand2 is an integer.
1314 *
1315 * If (Lequal ("namestr", val))
1316 */
1317void acpigen_write_if_lequal_namestr_int(const char *namestr, uint64_t val)
1318{
1319 acpigen_write_if();
1320 acpigen_emit_byte(LEQUAL_OP);
1321 acpigen_emit_namestring(namestr);
1322 acpigen_write_integer(val);
1323}
1324
Furquan Shaikhfcc7a042016-10-20 23:12:38 -07001325void acpigen_write_else(void)
1326{
1327 acpigen_emit_byte(ELSE_OP);
1328 acpigen_write_len_f();
1329}
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001330
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001331void acpigen_write_to_buffer(uint8_t src, uint8_t dst)
1332{
1333 acpigen_emit_byte(TO_BUFFER_OP);
1334 acpigen_emit_byte(src);
1335 acpigen_emit_byte(dst);
1336}
1337
1338void acpigen_write_to_integer(uint8_t src, uint8_t dst)
1339{
1340 acpigen_emit_byte(TO_INTEGER_OP);
1341 acpigen_emit_byte(src);
1342 acpigen_emit_byte(dst);
1343}
1344
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301345void acpigen_write_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001346{
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301347 size_t i;
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001348
1349 acpigen_emit_byte(BUFFER_OP);
1350 acpigen_write_len_f();
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301351 acpigen_write_integer(size);
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001352
1353 for (i = 0; i < size; i++)
1354 acpigen_emit_byte(arr[i]);
1355
1356 acpigen_pop_len();
1357}
1358
Rizwan Qureshiaca4c942017-03-06 21:50:26 +05301359void acpigen_write_return_byte_buffer(uint8_t *arr, size_t size)
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001360{
1361 acpigen_emit_byte(RETURN_OP);
1362 acpigen_write_byte_buffer(arr, size);
1363}
1364
1365void acpigen_write_return_singleton_buffer(uint8_t arg)
1366{
1367 acpigen_write_return_byte_buffer(&arg, 1);
1368}
1369
Duncan Laurie2fe74712020-06-01 17:36:56 -07001370void acpigen_write_return_op(uint8_t arg)
1371{
1372 acpigen_emit_byte(RETURN_OP);
1373 acpigen_emit_byte(arg);
1374}
1375
Furquan Shaikh9b39adb2016-10-21 16:21:07 -07001376void acpigen_write_return_byte(uint8_t arg)
1377{
1378 acpigen_emit_byte(RETURN_OP);
1379 acpigen_write_byte(arg);
1380}
1381
Naresh G Solanki05abe432016-11-17 00:16:29 +05301382void acpigen_write_return_integer(uint64_t arg)
1383{
1384 acpigen_emit_byte(RETURN_OP);
1385 acpigen_write_integer(arg);
1386}
1387
1388void acpigen_write_return_string(const char *arg)
1389{
1390 acpigen_emit_byte(RETURN_OP);
1391 acpigen_write_string(arg);
1392}
1393
Duncan Lauriebeb2af42018-05-07 14:26:59 -07001394void acpigen_write_upc(enum acpi_upc_type type)
1395{
1396 acpigen_write_name("_UPC");
1397 acpigen_write_package(4);
1398 /* Connectable */
1399 acpigen_write_byte(type == UPC_TYPE_UNUSED ? 0 : 0xff);
1400 /* Type */
1401 acpigen_write_byte(type);
1402 /* Reserved0 */
1403 acpigen_write_zero();
1404 /* Reserved1 */
1405 acpigen_write_zero();
1406 acpigen_pop_len();
1407}
1408
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001409void acpigen_write_pld(const struct acpi_pld *pld)
1410{
1411 uint8_t buf[20];
1412
1413 if (acpi_pld_to_buffer(pld, buf, ARRAY_SIZE(buf)) < 0)
1414 return;
1415
1416 acpigen_write_name("_PLD");
Matt Delco4929f432019-01-30 11:40:44 -08001417 acpigen_write_package(1);
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001418 acpigen_write_byte_buffer(buf, ARRAY_SIZE(buf));
Matt Delco4929f432019-01-30 11:40:44 -08001419 acpigen_pop_len();
Duncan Laurie3e7197a2018-05-07 14:18:13 -07001420}
1421
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301422void acpigen_write_dsm(const char *uuid, void (**callbacks)(void *),
1423 size_t count, void *arg)
1424{
1425 struct dsm_uuid id = DSM_UUID(uuid, callbacks, count, arg);
1426 acpigen_write_dsm_uuid_arr(&id, 1);
1427}
1428
1429static void acpigen_write_dsm_uuid(struct dsm_uuid *id)
1430{
1431 size_t i;
1432
1433 /* If (LEqual (Local0, ToUUID(uuid))) */
1434 acpigen_write_if();
1435 acpigen_emit_byte(LEQUAL_OP);
1436 acpigen_emit_byte(LOCAL0_OP);
1437 acpigen_write_uuid(id->uuid);
1438
1439 /* ToInteger (Arg2, Local1) */
1440 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
1441
1442 for (i = 0; i < id->count; i++) {
1443 /* If (LEqual (Local1, i)) */
1444 acpigen_write_if_lequal_op_int(LOCAL1_OP, i);
1445
1446 /* Callback to write if handler. */
1447 if (id->callbacks[i])
1448 id->callbacks[i](id->arg);
1449
1450 acpigen_pop_len(); /* If */
1451 }
1452
1453 /* Default case: Return (Buffer (One) { 0x0 }) */
1454 acpigen_write_return_singleton_buffer(0x0);
1455
1456 acpigen_pop_len(); /* If (LEqual (Local0, ToUUID(uuid))) */
1457
1458}
1459
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001460/*
1461 * Generate ACPI AML code for _DSM method.
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301462 * This function takes as input array of uuid for the device, set of callbacks
1463 * and argument to pass into the callbacks. Callbacks should ensure that Local0
1464 * and Local1 are left untouched. Use of Local2-Local7 is permitted in
1465 * callbacks.
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001466 *
1467 * Arguments passed into _DSM method:
1468 * Arg0 = UUID
1469 * Arg1 = Revision
1470 * Arg2 = Function index
1471 * Arg3 = Function specific arguments
1472 *
1473 * AML code generated would look like:
1474 * Method (_DSM, 4, Serialized) {
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301475 * ToBuffer (Arg0, Local0)
1476 * If (LEqual (Local0, ToUUID(uuid))) {
1477 * ToInteger (Arg2, Local1)
1478 * If (LEqual (Local1, 0)) {
1479 * <acpigen by callback[0]>
1480 * }
1481 * ...
1482 * If (LEqual (Local1, n)) {
1483 * <acpigen by callback[n]>
1484 * }
1485 * Return (Buffer (One) { 0x0 })
1486 * }
1487 * ...
1488 * If (LEqual (Local0, ToUUID(uuidn))) {
1489 * ...
1490 * }
1491 * Return (Buffer (One) { 0x0 })
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001492 * }
1493 */
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301494void acpigen_write_dsm_uuid_arr(struct dsm_uuid *ids, size_t count)
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001495{
1496 size_t i;
1497
1498 /* Method (_DSM, 4, Serialized) */
1499 acpigen_write_method_serialized("_DSM", 0x4);
1500
1501 /* ToBuffer (Arg0, Local0) */
1502 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
1503
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001504 for (i = 0; i < count; i++)
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301505 acpigen_write_dsm_uuid(&ids[i]);
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001506
Naresh G Solankiab77cd42016-11-15 10:13:15 +05301507 /* Return (Buffer (One) { 0x0 }) */
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001508 acpigen_write_return_singleton_buffer(0x0);
1509
Furquan Shaikhc00bd182016-10-21 16:37:41 -07001510 acpigen_pop_len(); /* Method _DSM */
1511}
1512
Matt Delcob425bc82018-08-13 13:36:27 -07001513#define CPPC_PACKAGE_NAME "\\GCPC"
1514
1515void acpigen_write_CPPC_package(const struct cppc_config *config)
1516{
1517 u32 i;
1518 u32 max;
1519 switch (config->version) {
1520 case 1:
1521 max = CPPC_MAX_FIELDS_VER_1;
1522 break;
1523 case 2:
1524 max = CPPC_MAX_FIELDS_VER_2;
1525 break;
1526 case 3:
1527 max = CPPC_MAX_FIELDS_VER_3;
1528 break;
1529 default:
1530 printk(BIOS_ERR, "ERROR: CPPC version %u is not implemented\n",
1531 config->version);
1532 return;
1533 }
1534 acpigen_write_name(CPPC_PACKAGE_NAME);
1535
1536 /* Adding 2 to account for length and version fields */
1537 acpigen_write_package(max + 2);
1538 acpigen_write_dword(max + 2);
1539
1540 acpigen_write_byte(config->version);
1541
1542 for (i = 0; i < max; ++i) {
1543 const acpi_addr_t *reg = &(config->regs[i]);
1544 if (reg->space_id == ACPI_ADDRESS_SPACE_MEMORY &&
1545 reg->bit_width == 32 && reg->access_size == 0) {
1546 acpigen_write_dword(reg->addrl);
1547 } else {
1548 acpigen_write_register_resource(reg);
1549 }
1550 }
1551 acpigen_pop_len();
1552}
1553
1554void acpigen_write_CPPC_method(void)
1555{
1556 acpigen_write_method("_CPC", 0);
1557 acpigen_emit_byte(RETURN_OP);
1558 acpigen_emit_namestring(CPPC_PACKAGE_NAME);
1559 acpigen_pop_len();
1560}
1561
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001562/*
1563 * Generate ACPI AML code for _ROM method.
1564 * This function takes as input ROM data and ROM length.
1565 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001566 * The ACPI spec isn't clear about what should happen at the end of the
1567 * ROM. Tests showed that it shouldn't truncate, but fill the remaining
1568 * bytes in the returned buffer with zeros.
1569 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001570 * Arguments passed into _DSM method:
1571 * Arg0 = Offset in Bytes
1572 * Arg1 = Bytes to return
1573 *
1574 * Example:
1575 * acpigen_write_rom(0xdeadbeef, 0x10000)
1576 *
1577 * AML code generated would look like:
1578 * Method (_ROM, 2, NotSerialized) {
1579 *
1580 * OperationRegion("ROMS", SYSTEMMEMORY, 0xdeadbeef, 0x10000)
1581 * Field (ROMS, AnyAcc, NoLock, Preserve)
1582 * {
1583 * Offset (0),
1584 * RBF0, 0x80000
1585 * }
1586 *
1587 * Store (Arg0, Local0)
1588 * Store (Arg1, Local1)
1589 *
1590 * If (LGreater (Local1, 0x1000))
1591 * {
1592 * Store (0x1000, Local1)
1593 * }
1594 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001595 * Store (Local1, Local3)
1596 *
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001597 * If (LGreater (Local0, 0x10000))
1598 * {
1599 * Return(Buffer(Local1){0})
1600 * }
1601 *
1602 * If (LGreater (Local0, 0x0f000))
1603 * {
1604 * Subtract (0x10000, Local0, Local2)
1605 * If (LGreater (Local1, Local2))
1606 * {
1607 * Store (Local2, Local1)
1608 * }
1609 * }
1610 *
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001611 * Name (ROM1, Buffer (Local3) {0})
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001612 *
1613 * Multiply (Local0, 0x08, Local0)
1614 * Multiply (Local1, 0x08, Local1)
1615 *
1616 * CreateField (RBF0, Local0, Local1, TMPB)
1617 * Store (TMPB, ROM1)
1618 * Return (ROM1)
1619 * }
1620 */
1621
1622void acpigen_write_rom(void *bios, const size_t length)
1623{
1624 ASSERT(bios)
1625 ASSERT(length)
1626
Jonathan Neuschäfer6b0102d2018-09-12 12:26:45 +02001627 /* Method (_ROM, 2, Serialized) */
Marc Jones24462e62018-08-15 23:57:28 -06001628 acpigen_write_method_serialized("_ROM", 2);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001629
1630 /* OperationRegion("ROMS", SYSTEMMEMORY, current, length) */
1631 struct opregion opreg = OPREGION("ROMS", SYSTEMMEMORY,
1632 (uintptr_t)bios, length);
1633 acpigen_write_opregion(&opreg);
1634
1635 struct fieldlist l[] = {
1636 FIELDLIST_OFFSET(0),
1637 FIELDLIST_NAMESTR("RBF0", 8 * length),
1638 };
1639
1640 /* Field (ROMS, AnyAcc, NoLock, Preserve)
1641 * {
1642 * Offset (0),
1643 * RBF0, 0x80000
1644 * } */
1645 acpigen_write_field(opreg.name, l, 2, FIELD_ANYACC |
1646 FIELD_NOLOCK | FIELD_PRESERVE);
1647
1648 /* Store (Arg0, Local0) */
1649 acpigen_write_store();
1650 acpigen_emit_byte(ARG0_OP);
1651 acpigen_emit_byte(LOCAL0_OP);
1652
1653 /* Store (Arg1, Local1) */
1654 acpigen_write_store();
1655 acpigen_emit_byte(ARG1_OP);
1656 acpigen_emit_byte(LOCAL1_OP);
1657
1658 /* ACPI SPEC requires to return at maximum 4KiB */
1659 /* If (LGreater (Local1, 0x1000)) */
1660 acpigen_write_if();
1661 acpigen_emit_byte(LGREATER_OP);
1662 acpigen_emit_byte(LOCAL1_OP);
1663 acpigen_write_integer(0x1000);
1664
1665 /* Store (0x1000, Local1) */
1666 acpigen_write_store();
1667 acpigen_write_integer(0x1000);
1668 acpigen_emit_byte(LOCAL1_OP);
1669
1670 /* Pop if */
1671 acpigen_pop_len();
1672
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001673 /* Store (Local1, Local3) */
1674 acpigen_write_store();
1675 acpigen_emit_byte(LOCAL1_OP);
1676 acpigen_emit_byte(LOCAL3_OP);
1677
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001678 /* If (LGreater (Local0, length)) */
1679 acpigen_write_if();
1680 acpigen_emit_byte(LGREATER_OP);
1681 acpigen_emit_byte(LOCAL0_OP);
1682 acpigen_write_integer(length);
1683
1684 /* Return(Buffer(Local1){0}) */
1685 acpigen_emit_byte(RETURN_OP);
1686 acpigen_emit_byte(BUFFER_OP);
1687 acpigen_write_len_f();
1688 acpigen_emit_byte(LOCAL1_OP);
1689 acpigen_emit_byte(0);
1690 acpigen_pop_len();
1691
1692 /* Pop if */
1693 acpigen_pop_len();
1694
1695 /* If (LGreater (Local0, length - 4096)) */
1696 acpigen_write_if();
1697 acpigen_emit_byte(LGREATER_OP);
1698 acpigen_emit_byte(LOCAL0_OP);
1699 acpigen_write_integer(length - 4096);
1700
1701 /* Subtract (length, Local0, Local2) */
1702 acpigen_emit_byte(SUBTRACT_OP);
1703 acpigen_write_integer(length);
1704 acpigen_emit_byte(LOCAL0_OP);
1705 acpigen_emit_byte(LOCAL2_OP);
1706
1707 /* If (LGreater (Local1, Local2)) */
1708 acpigen_write_if();
1709 acpigen_emit_byte(LGREATER_OP);
1710 acpigen_emit_byte(LOCAL1_OP);
1711 acpigen_emit_byte(LOCAL2_OP);
1712
1713 /* Store (Local2, Local1) */
1714 acpigen_write_store();
1715 acpigen_emit_byte(LOCAL2_OP);
1716 acpigen_emit_byte(LOCAL1_OP);
1717
1718 /* Pop if */
1719 acpigen_pop_len();
1720
1721 /* Pop if */
1722 acpigen_pop_len();
1723
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001724 /* Name (ROM1, Buffer (Local3) {0}) */
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001725 acpigen_write_name("ROM1");
1726 acpigen_emit_byte(BUFFER_OP);
1727 acpigen_write_len_f();
Patrick Rudolph65cbbe72018-05-02 19:07:57 +02001728 acpigen_emit_byte(LOCAL3_OP);
Patrick Rudolph6be6df02017-04-28 16:34:26 +02001729 acpigen_emit_byte(0);
1730 acpigen_pop_len();
1731
1732 /* Multiply (Local1, 0x08, Local1) */
1733 acpigen_emit_byte(MULTIPLY_OP);
1734 acpigen_emit_byte(LOCAL1_OP);
1735 acpigen_write_integer(0x08);
1736 acpigen_emit_byte(LOCAL1_OP);
1737
1738 /* Multiply (Local0, 0x08, Local0) */
1739 acpigen_emit_byte(MULTIPLY_OP);
1740 acpigen_emit_byte(LOCAL0_OP);
1741 acpigen_write_integer(0x08);
1742 acpigen_emit_byte(LOCAL0_OP);
1743
1744 /* CreateField (RBF0, Local0, Local1, TMPB) */
1745 acpigen_emit_ext_op(CREATEFIELD_OP);
1746 acpigen_emit_namestring("RBF0");
1747 acpigen_emit_byte(LOCAL0_OP);
1748 acpigen_emit_byte(LOCAL1_OP);
1749 acpigen_emit_namestring("TMPB");
1750
1751 /* Store (TMPB, ROM1) */
1752 acpigen_write_store();
1753 acpigen_emit_namestring("TMPB");
1754 acpigen_emit_namestring("ROM1");
1755
1756 /* Return (ROM1) */
1757 acpigen_emit_byte(RETURN_OP);
1758 acpigen_emit_namestring("ROM1");
1759
1760 /* Pop method */
1761 acpigen_pop_len();
1762}
1763
1764
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001765/* Soc-implemented functions -- weak definitions. */
Aaron Durbin64031672018-04-21 14:45:32 -06001766int __weak acpigen_soc_read_rx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001767{
1768 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1769 acpigen_write_debug_string("read_rx_gpio not available");
1770 return -1;
1771}
1772
Aaron Durbin64031672018-04-21 14:45:32 -06001773int __weak acpigen_soc_get_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001774{
1775 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1776 acpigen_write_debug_string("get_tx_gpio not available");
1777 return -1;
1778}
1779
Aaron Durbin64031672018-04-21 14:45:32 -06001780int __weak acpigen_soc_set_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001781{
1782 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1783 acpigen_write_debug_string("set_tx_gpio not available");
1784 return -1;
1785}
1786
Aaron Durbin64031672018-04-21 14:45:32 -06001787int __weak acpigen_soc_clear_tx_gpio(unsigned int gpio_num)
Furquan Shaikh0a48aee2016-10-20 07:12:49 -07001788{
1789 printk(BIOS_ERR, "ERROR: %s not implemented\n", __func__);
1790 acpigen_write_debug_string("clear_tx_gpio not available");
1791 return -1;
1792}
Furquan Shaikhbf4845d2017-02-20 22:56:25 -08001793
1794/*
1795 * Helper functions for enabling/disabling Tx GPIOs based on the GPIO
1796 * polarity. These functions end up calling acpigen_soc_{set,clear}_tx_gpio to
1797 * make callbacks into SoC acpigen code.
1798 *
1799 * Returns 0 on success and -1 on error.
1800 */
1801int acpigen_enable_tx_gpio(struct acpi_gpio *gpio)
1802{
1803 if (gpio->polarity == ACPI_GPIO_ACTIVE_HIGH)
1804 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1805 else
1806 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1807}
1808
1809int acpigen_disable_tx_gpio(struct acpi_gpio *gpio)
1810{
1811 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1812 return acpigen_soc_set_tx_gpio(gpio->pins[0]);
1813 else
1814 return acpigen_soc_clear_tx_gpio(gpio->pins[0]);
1815}
Jonathan Zhang71299c22020-01-27 11:38:57 -08001816
Rajat Jain310623b2020-02-26 11:02:53 -08001817void acpigen_get_rx_gpio(struct acpi_gpio *gpio)
1818{
1819 acpigen_soc_read_rx_gpio(gpio->pins[0]);
1820
1821 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1822 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1823}
1824
Duncan Laurie2fe74712020-06-01 17:36:56 -07001825void acpigen_get_tx_gpio(struct acpi_gpio *gpio)
1826{
1827 acpigen_soc_get_tx_gpio(gpio->pins[0]);
1828
1829 if (gpio->polarity == ACPI_GPIO_ACTIVE_LOW)
1830 acpigen_write_xor(LOCAL0_OP, 1, LOCAL0_OP);
1831}
1832
Jonathan Zhang71299c22020-01-27 11:38:57 -08001833/* refer to ACPI 6.4.3.5.3 Word Address Space Descriptor section for details */
1834void acpigen_resource_word(u16 res_type, u16 gen_flags, u16 type_flags, u16 gran,
1835 u16 range_min, u16 range_max, u16 translation, u16 length)
1836{
1837 acpigen_emit_byte(0x88);
1838 /* Byte 1+2: length (0x000d) */
1839 acpigen_emit_byte(0x0d);
1840 acpigen_emit_byte(0x00);
1841 /* resource type */
1842 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1843 /* general flags */
1844 acpigen_emit_byte(gen_flags);
1845 /* type flags */
1846 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1847 acpigen_emit_byte(type_flags);
1848 /* granularity, min, max, translation, length */
1849 acpigen_emit_word(gran);
1850 acpigen_emit_word(range_min);
1851 acpigen_emit_word(range_max);
1852 acpigen_emit_word(translation);
1853 acpigen_emit_word(length);
1854}
1855
1856/* refer to ACPI 6.4.3.5.2 DWord Address Space Descriptor section for details */
1857void acpigen_resource_dword(u16 res_type, u16 gen_flags, u16 type_flags,
1858 u32 gran, u32 range_min, u32 range_max, u32 translation, u32 length)
1859{
1860 acpigen_emit_byte(0x87);
1861 /* Byte 1+2: length (0023) */
1862 acpigen_emit_byte(23);
1863 acpigen_emit_byte(0x00);
1864 /* resource type */
1865 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1866 /* general flags */
1867 acpigen_emit_byte(gen_flags);
1868 /* type flags */
1869 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1870 acpigen_emit_byte(type_flags);
1871 /* granularity, min, max, translation, length */
1872 acpigen_emit_dword(gran);
1873 acpigen_emit_dword(range_min);
1874 acpigen_emit_dword(range_max);
1875 acpigen_emit_dword(translation);
1876 acpigen_emit_dword(length);
1877}
1878
1879static void acpigen_emit_qword(u64 data)
1880{
1881 acpigen_emit_dword(data & 0xffffffff);
1882 acpigen_emit_dword((data >> 32) & 0xffffffff);
1883}
1884
1885/* refer to ACPI 6.4.3.5.1 QWord Address Space Descriptor section for details */
1886void acpigen_resource_qword(u16 res_type, u16 gen_flags, u16 type_flags,
1887 u64 gran, u64 range_min, u64 range_max, u64 translation, u64 length)
1888{
1889 acpigen_emit_byte(0x8a);
1890 /* Byte 1+2: length (0x002b) */
1891 acpigen_emit_byte(0x2b);
1892 acpigen_emit_byte(0x00);
1893 /* resource type */
1894 acpigen_emit_byte(res_type); // 0 - mem, 1 - io, 2 - bus
1895 /* general flags */
1896 acpigen_emit_byte(gen_flags);
1897 /* type flags */
1898 // refer to ACPI Table 6-234 (Memory), 6-235 (IO), 6-236 (Bus) for details
1899 acpigen_emit_byte(type_flags);
1900 /* granularity, min, max, translation, length */
1901 acpigen_emit_qword(gran);
1902 acpigen_emit_qword(range_min);
1903 acpigen_emit_qword(range_max);
1904 acpigen_emit_qword(translation);
1905 acpigen_emit_qword(length);
1906}
Furquan Shaikh1a829232020-04-23 11:11:05 -07001907
1908void acpigen_write_ADR(uint64_t adr)
1909{
1910 acpigen_write_name_qword("_ADR", adr);
1911}
1912
1913void acpigen_write_ADR_pci_devfn(pci_devfn_t devfn)
1914{
1915 /*
1916 * _ADR for PCI Bus is encoded as follows:
1917 * [63:32] - unused
1918 * [31:16] - device #
1919 * [15:0] - function #
1920 */
1921 acpigen_write_ADR(PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn));
1922}
1923
1924void acpigen_write_ADR_pci_device(const struct device *dev)
1925{
1926 assert(dev->path.type == DEVICE_PATH_PCI);
1927 acpigen_write_ADR_pci_devfn(dev->path.pci.devfn);
1928}
Duncan Laurie08a942f2020-04-29 12:13:14 -07001929
1930/**
1931 * acpigen_write_ADR_soundwire_device() - SoundWire ACPI Device Address Encoding.
1932 * @address: SoundWire device address properties.
1933 *
1934 * From SoundWire Discovery and Configuration Specification Version 1.0 Table 3.
1935 *
1936 * 63..52 - Reserved (0)
1937 * 51..48 - Zero-based SoundWire Link ID, relative to the immediate parent.
1938 * Used when a Controller has multiple master devices, each producing a
1939 * separate SoundWire Link. Set to 0 for single-link controllers.
1940 * 47..0 - SoundWire Device ID Encoding from specification version 1.2 table 88
1941 * 47..44 - SoundWire specification version that this device supports
1942 * 43..40 - Unique ID for multiple devices
1943 * 39..24 - MIPI standard manufacturer code
1944 * 23..08 - Vendor defined part ID
1945 * 07..00 - MIPI class encoding
1946 */
1947void acpigen_write_ADR_soundwire_device(const struct soundwire_address *address)
1948{
1949 acpigen_write_ADR((((uint64_t)address->link_id & 0xf) << 48) |
1950 (((uint64_t)address->version & 0xf) << 44) |
1951 (((uint64_t)address->unique_id & 0xf) << 40) |
1952 (((uint64_t)address->manufacturer_id & 0xffff) << 24) |
1953 (((uint64_t)address->part_id & 0xffff) << 8) |
1954 (((uint64_t)address->class & 0xff)));
1955}