blob: 226fba153c832be18b5ee2ff81643d915f62bc98 [file] [log] [blame]
Rudolf Marek293b5f52009-02-01 18:35:15 +00001/*
2 * This file is part of the coreboot project.
3 *
Timothy Pearson83abd812015-06-08 19:35:06 -05004 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Rudolf Marek293b5f52009-02-01 18:35:15 +00005 * Copyright (C) 2009 Rudolf Marek <r.marek@assembler.cz>
6 *
7 * This program is free software; you can redistribute it and/or modify
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
Rudolf Marek293b5f52009-02-01 18:35:15 +000010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Rudolf Marek293b5f52009-02-01 18:35:15 +000015 */
16
Paul Menzel0f4c0e22013-02-22 12:33:08 +010017/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +000018#define ACPIGEN_LENSTACK_SIZE 10
19
Paul Menzel0f4c0e22013-02-22 12:33:08 +010020/*
Timothy Pearson83abd812015-06-08 19:35:06 -050021 * If you need to change this, change acpigen_write_len_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +010022 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +010023 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000024
Timothy Pearson83abd812015-06-08 19:35:06 -050025#define ACPIGEN_MAXLEN 0xfffff
Rudolf Marek293b5f52009-02-01 18:35:15 +000026
27#include <string.h>
28#include <arch/acpigen.h>
29#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000030#include <device/device.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000031
32static char *gencurrent;
33
34char *len_stack[ACPIGEN_LENSTACK_SIZE];
35int ltop = 0;
36
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010037void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000038{
39 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010040 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000041 acpigen_emit_byte(0);
42 acpigen_emit_byte(0);
Timothy Pearson83abd812015-06-08 19:35:06 -050043 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000044}
45
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010046void acpigen_pop_len(void)
47{
48 int len;
49 ASSERT(ltop > 0)
50 char *p = len_stack[--ltop];
51 len = gencurrent - p;
52 ASSERT(len <= ACPIGEN_MAXLEN)
Timothy Pearson83abd812015-06-08 19:35:06 -050053 /* generate store length for 0xfffff max */
54 p[0] = (0x80 | (len & 0xf));
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010055 p[1] = (len >> 4 & 0xff);
Timothy Pearson83abd812015-06-08 19:35:06 -050056 p[2] = (len >> 12 & 0xff);
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010057
58}
59
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000060void acpigen_set_current(char *curr)
61{
62 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000063}
64
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000065char *acpigen_get_current(void)
66{
67 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000068}
69
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010070void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000071{
72 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000073}
74
Duncan Laurie9ccae752016-05-09 07:43:19 -070075void acpigen_emit_word(unsigned int data)
76{
77 acpigen_emit_byte(data & 0xff);
78 acpigen_emit_byte((data >> 8) & 0xff);
79}
80
81void acpigen_emit_dword(unsigned int data)
82{
83 acpigen_emit_byte(data & 0xff);
84 acpigen_emit_byte((data >> 8) & 0xff);
85 acpigen_emit_byte((data >> 16) & 0xff);
86 acpigen_emit_byte((data >> 24) & 0xff);
87}
88
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010089void acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000090{
Rudolf Marek293b5f52009-02-01 18:35:15 +000091 /* package op */
92 acpigen_emit_byte(0x12);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010093 acpigen_write_len_f();
Rudolf Marek293b5f52009-02-01 18:35:15 +000094 acpigen_emit_byte(nr_el);
Rudolf Marek293b5f52009-02-01 18:35:15 +000095}
96
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010097void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000098{
99 /* byte op */
100 acpigen_emit_byte(0xa);
101 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000102}
103
Duncan Laurie9ccae752016-05-09 07:43:19 -0700104void acpigen_write_word(unsigned int data)
105{
106 /* word op */
107 acpigen_emit_byte(0xb);
108 acpigen_emit_word(data);
109}
110
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100111void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000112{
113 /* dword op */
114 acpigen_emit_byte(0xc);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700115 acpigen_emit_dword(data);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000116}
117
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100118void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000119{
120 /* qword op */
121 acpigen_emit_byte(0xe);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700122 acpigen_emit_dword(data & 0xffffffff);
123 acpigen_emit_dword((data >> 32) & 0xffffffff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000124}
125
Duncan Laurief7c38762016-05-09 08:20:38 -0700126void acpigen_write_zero(void)
127{
128 acpigen_emit_byte(0x00);
129}
130
131void acpigen_write_one(void)
132{
133 acpigen_emit_byte(0x01);
134}
135
136void acpigen_write_ones(void)
137{
138 acpigen_emit_byte(0xff);
139}
140
141void acpigen_write_integer(uint64_t data)
142{
143 if (data == 0)
144 acpigen_write_zero();
145 else if (data == 1)
146 acpigen_write_one();
147 else if (data <= 0xff)
148 acpigen_write_byte((unsigned char)data);
149 else if (data <= 0xffff)
150 acpigen_write_word((unsigned int)data);
151 else if (data <= 0xffffffff)
152 acpigen_write_dword((unsigned int)data);
153 else
154 acpigen_write_qword(data);
155}
156
157void acpigen_write_name_zero(const char *name)
158{
159 acpigen_write_name(name);
160 acpigen_write_one();
161}
162
163void acpigen_write_name_one(const char *name)
164{
165 acpigen_write_name(name);
166 acpigen_write_zero();
167}
168
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100169void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000170{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100171 acpigen_write_name(name);
172 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000173}
174
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100175void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000176{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100177 acpigen_write_name(name);
178 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000179}
180
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100181void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000182{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100183 acpigen_write_name(name);
184 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000185}
186
Duncan Laurief7c38762016-05-09 08:20:38 -0700187void acpigen_write_name_integer(const char *name, uint64_t val)
188{
189 acpigen_write_name(name);
190 acpigen_write_integer(val);
191}
192
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700193void acpigen_write_name_string(const char *name, const char *string)
194{
195 acpigen_write_name(name);
196 acpigen_write_string(string);
197}
198
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100199void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000200{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000201 int i;
202 for (i = 0; i < size; i++) {
203 acpigen_emit_byte(data[i]);
204 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000205}
206
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700207void acpigen_emit_string(const char *string)
208{
Jonathan Neuschäfer0ba307f2016-05-17 17:40:09 +0200209 acpigen_emit_stream(string, string ? strlen(string) : 0);
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700210 acpigen_emit_byte('\0'); /* NUL */
211}
212
213void acpigen_write_string(const char *string)
214{
215 acpigen_emit_byte(0x0d);
216 acpigen_emit_string(string);
217}
218
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100219/*
220 * The naming conventions for ACPI namespace names are a bit tricky as
221 * each element has to be 4 chars wide (»All names are a fixed 32 bits.«)
222 * and »By convention, when an ASL compiler pads a name shorter than 4
223 * characters, it is done so with trailing underscores (‘_’).«.
224 *
225 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
226 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000227
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100228static void acpigen_emit_simple_namestring(const char *name) {
229 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000230 char ud[] = "____";
231 for (i = 0; i < 4; i++) {
232 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100233 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000234 break;
235 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100236 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000237 }
238 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000239}
240
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100241static void acpigen_emit_double_namestring(const char *name, int dotpos) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000242 /* mark dual name prefix */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100243 acpigen_emit_byte(0x2e);
244 acpigen_emit_simple_namestring(name);
245 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000246}
247
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100248static void acpigen_emit_multi_namestring(const char *name) {
249 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000250 unsigned char *pathlen;
Rudolf Marek3310d752009-06-21 20:26:13 +0000251 /* mark multi name prefix */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100252 acpigen_emit_byte(0x2f);
253 acpigen_emit_byte(0x0);
Rudolf Marek3310d752009-06-21 20:26:13 +0000254 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
255
256 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100257 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000258 /* find end or next entity */
259 while ((name[0] != '.') && (name[0] != '\0'))
260 name++;
261 /* forward to next */
262 if (name[0] == '.')
263 name++;
264 count++;
265 }
266
267 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000268}
269
270
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100271void acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000272 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000273 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000274
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600275 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000276 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100277 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000278 namepath++;
279 }
280
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600281 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000282 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100283 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000284 namepath++;
285 }
286
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200287 /* If we have only \\ or only ^...^. Then we need to put a null
288 name (0x00). */
289 if(namepath[0] == '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100290 acpigen_emit_byte(0x00);
291 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200292 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000293
294 i = 0;
295 while (namepath[i] != '\0') {
296 if (namepath[i] == '.') {
297 dotcount++;
298 dotpos = i;
299 }
300 i++;
301 }
302
303 if (dotcount == 0) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100304 acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000305 } else if (dotcount == 1) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100306 acpigen_emit_double_namestring(namepath, dotpos);
Rudolf Marek3310d752009-06-21 20:26:13 +0000307 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100308 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000309 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000310}
311
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100312void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000313{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000314 /* name op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100315 acpigen_emit_byte(0x8);
316 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000317}
318
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100319void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000320{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000321 /* scope op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100322 acpigen_emit_byte(0x10);
323 acpigen_write_len_f();
324 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000325}
Rudolf Marekf997b552009-02-14 15:40:23 +0000326
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100327void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000328{
329/*
330 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
331 {
332*/
333 char pscope[16];
Rudolf Marekf997b552009-02-14 15:40:23 +0000334 /* processor op */
335 acpigen_emit_byte(0x5b);
336 acpigen_emit_byte(0x83);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100337 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000338
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100339 snprintf(pscope, sizeof (pscope),
Timothy Pearson033bb4b2015-02-10 22:21:39 -0600340 "\\_PR.CP%02d", (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100341 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000342 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700343 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000344 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000345}
346
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100347void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000348{
349/*
350 Name (_PCT, Package (0x02)
351 {
352 ResourceTemplate ()
353 {
354 Register (FFixedHW,
355 0x00, // Bit Width
356 0x00, // Bit Offset
357 0x0000000000000000, // Address
358 ,)
359 },
360
361 ResourceTemplate ()
362 {
363 Register (FFixedHW,
364 0x00, // Bit Width
365 0x00, // Bit Offset
366 0x0000000000000000, // Address
367 ,)
368 }
369 })
370*/
371 static char stream[] = {
372 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
373 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
374 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
375 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
376 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
377 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
378 0x00, 0x79, 0x00
379 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100380 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000381}
382
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100383void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200384{
385/*
386 Name (_PTC, Package (0x02)
387 {
388 ResourceTemplate ()
389 {
390 Register (FFixedHW,
391 0x00, // Bit Width
392 0x00, // Bit Offset
393 0x0000000000000000, // Address
394 ,)
395 },
396
397 ResourceTemplate ()
398 {
399 Register (FFixedHW,
400 0x00, // Bit Width
401 0x00, // Bit Offset
402 0x0000000000000000, // Address
403 ,)
404 }
405 })
406*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200407 acpi_addr_t addr = {
408 .space_id = ACPI_ADDRESS_SPACE_FIXED,
409 .bit_width = 0,
410 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800411 {
412 .resv = 0
413 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200414 .addrl = 0,
415 .addrh = 0,
416 };
417
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100418 acpigen_write_name("_PTC");
419 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200420
421 /* ControlRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100422 acpigen_write_resourcetemplate_header();
423 acpigen_write_register(&addr);
424 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200425
426 /* StatusRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100427 acpigen_write_resourcetemplate_header();
428 acpigen_write_register(&addr);
429 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200430
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100431 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200432}
433
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100434void acpigen_write_method(const char *name, int nargs)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100435{
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100436 /* method op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100437 acpigen_emit_byte(0x14);
438 acpigen_write_len_f();
439 acpigen_emit_namestring(name);
440 acpigen_emit_byte(nargs & 7);
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100441}
442
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100443void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100444{
Duncan Laurie9ccae752016-05-09 07:43:19 -0700445 /* device op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100446 acpigen_emit_byte(0x5b);
447 acpigen_emit_byte(0x82);
448 acpigen_write_len_f();
449 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100450}
451
Duncan Laurieabe2de82016-05-09 11:08:46 -0700452void acpigen_write_STA(uint8_t status)
453{
454 /*
455 * Method (_STA, 0, NotSerialized) { Return (status) }
456 */
457 acpigen_write_method("_STA", 0);
458 acpigen_emit_byte(0xa4);
459 acpigen_write_byte(status);
460 acpigen_pop_len();
461}
462
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100463/*
464 * Generates a func with max supported P-states.
465 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100466void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000467{
468/*
469 Method (_PPC, 0, NotSerialized)
470 {
471 Return (nr)
472 }
473*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100474 acpigen_write_method("_PPC", 0);
Rudolf Marekf997b552009-02-14 15:40:23 +0000475 /* return */
476 acpigen_emit_byte(0xa4);
477 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100478 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100479 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000480}
481
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100482/*
483 * Generates a func with max supported P-states saved
484 * in the variable PPCM.
485 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100486void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700487{
488/*
489 Method (_PPC, 0, NotSerialized)
490 {
491 Return (PPCM)
492 }
493*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100494 acpigen_write_method("_PPC", 0);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700495 /* return */
496 acpigen_emit_byte(0xa4);
497 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100498 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100499 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700500}
501
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100502void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200503{
504/*
505 // Sample _TPC method
506 Method (_TPC, 0, NotSerialized)
507 {
508 Return (\TLVL)
509 }
510 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100511 acpigen_write_method("_TPC", 0);
512 acpigen_emit_byte(0xa4); /* ReturnOp */
513 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100514 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200515}
516
Duncan Laurieabe2de82016-05-09 11:08:46 -0700517void acpigen_write_PRW(u32 wake, u32 level)
518{
519 /*
520 * Name (_PRW, Package () { wake, level }
521 */
522 acpigen_write_name("_PRW");
523 acpigen_write_package(2);
524 acpigen_write_integer(wake);
525 acpigen_write_integer(level);
526 acpigen_pop_len();
527}
528
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100529void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000530 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000531{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100532 acpigen_write_package(6);
533 acpigen_write_dword(coreFreq);
534 acpigen_write_dword(power);
535 acpigen_write_dword(transLat);
536 acpigen_write_dword(busmLat);
537 acpigen_write_dword(control);
538 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100539 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700540
541 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
542 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000543}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000544
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100545void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000546{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100547 acpigen_write_name("_PSD");
548 acpigen_write_package(1);
549 acpigen_write_package(5);
550 acpigen_write_byte(5); // 5 values
551 acpigen_write_byte(0); // revision 0
552 acpigen_write_dword(domain);
553 acpigen_write_dword(coordtype);
554 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100555 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100556 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000557}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000558
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100559void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200560{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100561 acpigen_write_package(4);
562 acpigen_write_resourcetemplate_header();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200563 acpigen_write_register(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100564 acpigen_write_resourcetemplate_footer();
565 acpigen_write_dword(cstate->ctype);
566 acpigen_write_dword(cstate->latency);
567 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100568 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200569}
570
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100571void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200572{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100573 int i;
574 acpigen_write_name("_CST");
575 acpigen_write_package(nentries+1);
576 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200577
578 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100579 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200580
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100581 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200582}
583
Timothy Pearson83abd812015-06-08 19:35:06 -0500584void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype, u32 index)
585{
586 acpigen_write_name("_CSD");
587 acpigen_write_package(1);
588 acpigen_write_package(6);
589 acpigen_write_byte(6); // 6 values
590 acpigen_write_byte(0); // revision 0
591 acpigen_write_dword(domain);
592 acpigen_write_dword(coordtype);
593 acpigen_write_dword(numprocs);
594 acpigen_write_dword(index);
595 acpigen_pop_len();
596 acpigen_pop_len();
597}
598
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100599void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200600{
601/*
602 Sample _TSS package with 100% and 50% duty cycles
603 Name (_TSS, Package (0x02)
604 {
605 Package(){100, 1000, 0, 0x00, 0)
606 Package(){50, 520, 0, 0x18, 0)
607 })
608 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100609 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200610 acpi_tstate_t *tstate = tstate_list;
611
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100612 acpigen_write_name("_TSS");
613 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200614
615 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100616 acpigen_write_package(5);
617 acpigen_write_dword(tstate->percent);
618 acpigen_write_dword(tstate->power);
619 acpigen_write_dword(tstate->latency);
620 acpigen_write_dword(tstate->control);
621 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100622 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200623 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200624 }
625
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100626 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200627}
628
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100629void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200630{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100631 acpigen_write_name("_TSD");
632 acpigen_write_package(1);
633 acpigen_write_package(5);
634 acpigen_write_byte(5); // 5 values
635 acpigen_write_byte(0); // revision 0
636 acpigen_write_dword(domain);
637 acpigen_write_dword(coordtype);
638 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100639 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100640 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200641}
642
643
644
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100645void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000646{
647 /*
648 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
649 * Byte 0:
650 * Bit7 : 1 => big item
651 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
652 */
653 acpigen_emit_byte(0x86);
654 /* Byte 1+2: length (0x0009) */
655 acpigen_emit_byte(0x09);
656 acpigen_emit_byte(0x00);
657 /* bit1-7 are ignored */
658 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700659 acpigen_emit_dword(base);
660 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000661}
662
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100663void acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200664{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200665 acpigen_emit_byte(0x82); /* Register Descriptor */
666 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
667 acpigen_emit_byte(0x00); /* Register Length 15:8 */
668 acpigen_emit_byte(addr->space_id); /* Address Space ID */
669 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
670 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
671 acpigen_emit_byte(addr->resv); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700672 acpigen_emit_dword(addr->addrl); /* Register Address Low */
673 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200674}
675
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100676void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100677{
678 /*
679 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
680 * Byte 0:
681 * Bit7 : 0 => small item
682 * Bit6-3: 0100 (0x4) => IRQ port descriptor
683 * Bit2-0: 010 (0x2) => 2 Bytes long
684 */
685 acpigen_emit_byte(0x22);
686 acpigen_emit_byte(mask & 0xff);
687 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100688}
689
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100690void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000691{
692 /*
693 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
694 * Byte 0:
695 * Bit7 : 0 => small item
696 * Bit6-3: 1000 (0x8) => I/O port descriptor
697 * Bit2-0: 111 (0x7) => 7 Bytes long
698 */
699 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100700 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000701 /* bit1-7 are ignored */
702 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
703 /* minimum base address the device may be configured for */
704 acpigen_emit_byte(min & 0xff);
705 acpigen_emit_byte((min >> 8) & 0xff);
706 /* maximum base address the device may be configured for */
707 acpigen_emit_byte(max & 0xff);
708 acpigen_emit_byte((max >> 8) & 0xff);
709 /* alignment for min base */
710 acpigen_emit_byte(align & 0xff);
711 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000712}
713
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100714void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000715{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000716 /*
717 * A ResourceTemplate() is a Buffer() with a
718 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100719 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000720 * (small item 0xf, len 1)
721 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100722 acpigen_emit_byte(0x11); /* Buffer opcode */
723 acpigen_write_len_f();
724 acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000725 len_stack[ltop++] = acpigen_get_current();
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100726 acpigen_emit_byte(0x00);
727 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000728}
729
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100730void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000731{
732 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100733 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000734 /*
735 * end tag (acpi 4.0 Section 6.4.2.8)
736 * 0x79 <checksum>
737 * 0x00 is treated as a good checksum according to the spec
738 * and is what iasl generates.
739 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100740 acpigen_emit_byte(0x79);
741 acpigen_emit_byte(0x00);
742
Martin Rothe3690102016-01-06 15:21:02 -0700743 len = gencurrent - p;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100744
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000745 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100746 p[0] = len & 0xff;
747 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000748 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100749 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000750}
751
752static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
753 struct resource *res)
754{
755 acpigen_write_mem32fixed(0, res->base, res->size);
756}
757
758static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
759 struct resource *res)
760{
761 resource_t base = res->base;
762 resource_t size = res->size;
763 while (size > 0) {
764 resource_t sz = size > 255 ? 255 : size;
765 acpigen_write_io16(base, base, 0, sz, 1);
766 size -= sz;
767 base += sz;
768 }
769}
770
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100771void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000772{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100773 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000774
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100775 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000776 search_global_resources(
777 IORESOURCE_MEM | IORESOURCE_RESERVE,
778 IORESOURCE_MEM | IORESOURCE_RESERVE,
779 acpigen_add_mainboard_rsvd_mem32, 0);
780
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100781 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000782 search_global_resources(
783 IORESOURCE_IO | IORESOURCE_RESERVE,
784 IORESOURCE_IO | IORESOURCE_RESERVE,
785 acpigen_add_mainboard_rsvd_io, 0);
786
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100787 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000788}
789
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100790void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000791{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100792 acpigen_write_scope(scope);
793 acpigen_write_name(name);
794 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100795 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000796}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100797
798static int hex2bin(const char c)
799{
800 if (c >= 'A' && c <= 'F')
801 return c - 'A' + 10;
802 if (c >= 'a' && c <= 'f')
803 return c - 'a' + 10;
804 return c - '0';
805}
806
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100807void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100808{
809 u32 compact = 0;
810
811 /* Clamping individual values would be better but
812 there is a disagreement over what is a valid
813 EISA id, so accept anything and don't clamp,
814 parent code should create a valid EISAid.
815 */
816 compact |= (eisaid[0] - 'A' + 1) << 26;
817 compact |= (eisaid[1] - 'A' + 1) << 21;
818 compact |= (eisaid[2] - 'A' + 1) << 16;
819 compact |= hex2bin(eisaid[3]) << 12;
820 compact |= hex2bin(eisaid[4]) << 8;
821 compact |= hex2bin(eisaid[5]) << 4;
822 compact |= hex2bin(eisaid[6]);
823
824 acpigen_emit_byte(0xc);
825 acpigen_emit_byte((compact >> 24) & 0xff);
826 acpigen_emit_byte((compact >> 16) & 0xff);
827 acpigen_emit_byte((compact >> 8) & 0xff);
828 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100829}