blob: a612149ef2fca1ea6f156672e5ec2a71ef3d74fc [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
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100126void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000127{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100128 acpigen_write_name(name);
129 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000130}
131
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100132void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000133{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100134 acpigen_write_name(name);
135 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000136}
137
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100138void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000139{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100140 acpigen_write_name(name);
141 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000142}
143
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100144void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000145{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000146 int i;
147 for (i = 0; i < size; i++) {
148 acpigen_emit_byte(data[i]);
149 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000150}
151
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100152/*
153 * The naming conventions for ACPI namespace names are a bit tricky as
154 * each element has to be 4 chars wide (»All names are a fixed 32 bits.«)
155 * and »By convention, when an ASL compiler pads a name shorter than 4
156 * characters, it is done so with trailing underscores (‘_’).«.
157 *
158 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
159 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000160
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100161static void acpigen_emit_simple_namestring(const char *name) {
162 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000163 char ud[] = "____";
164 for (i = 0; i < 4; i++) {
165 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100166 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000167 break;
168 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100169 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000170 }
171 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000172}
173
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100174static void acpigen_emit_double_namestring(const char *name, int dotpos) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000175 /* mark dual name prefix */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100176 acpigen_emit_byte(0x2e);
177 acpigen_emit_simple_namestring(name);
178 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000179}
180
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100181static void acpigen_emit_multi_namestring(const char *name) {
182 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000183 unsigned char *pathlen;
Rudolf Marek3310d752009-06-21 20:26:13 +0000184 /* mark multi name prefix */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100185 acpigen_emit_byte(0x2f);
186 acpigen_emit_byte(0x0);
Rudolf Marek3310d752009-06-21 20:26:13 +0000187 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
188
189 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100190 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000191 /* find end or next entity */
192 while ((name[0] != '.') && (name[0] != '\0'))
193 name++;
194 /* forward to next */
195 if (name[0] == '.')
196 name++;
197 count++;
198 }
199
200 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000201}
202
203
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100204void acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000205 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000206 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000207
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600208 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000209 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100210 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000211 namepath++;
212 }
213
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600214 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000215 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100216 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000217 namepath++;
218 }
219
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200220 /* If we have only \\ or only ^...^. Then we need to put a null
221 name (0x00). */
222 if(namepath[0] == '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100223 acpigen_emit_byte(0x00);
224 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200225 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000226
227 i = 0;
228 while (namepath[i] != '\0') {
229 if (namepath[i] == '.') {
230 dotcount++;
231 dotpos = i;
232 }
233 i++;
234 }
235
236 if (dotcount == 0) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100237 acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000238 } else if (dotcount == 1) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100239 acpigen_emit_double_namestring(namepath, dotpos);
Rudolf Marek3310d752009-06-21 20:26:13 +0000240 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100241 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000242 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000243}
244
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100245void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000246{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000247 /* name op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100248 acpigen_emit_byte(0x8);
249 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000250}
251
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100252void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000253{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000254 /* scope op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100255 acpigen_emit_byte(0x10);
256 acpigen_write_len_f();
257 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000258}
Rudolf Marekf997b552009-02-14 15:40:23 +0000259
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100260void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000261{
262/*
263 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
264 {
265*/
266 char pscope[16];
Rudolf Marekf997b552009-02-14 15:40:23 +0000267 /* processor op */
268 acpigen_emit_byte(0x5b);
269 acpigen_emit_byte(0x83);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100270 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000271
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100272 snprintf(pscope, sizeof (pscope),
Timothy Pearson033bb4b2015-02-10 22:21:39 -0600273 "\\_PR.CP%02d", (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100274 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000275 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700276 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000277 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000278}
279
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100280void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000281{
282/*
283 Name (_PCT, Package (0x02)
284 {
285 ResourceTemplate ()
286 {
287 Register (FFixedHW,
288 0x00, // Bit Width
289 0x00, // Bit Offset
290 0x0000000000000000, // Address
291 ,)
292 },
293
294 ResourceTemplate ()
295 {
296 Register (FFixedHW,
297 0x00, // Bit Width
298 0x00, // Bit Offset
299 0x0000000000000000, // Address
300 ,)
301 }
302 })
303*/
304 static char stream[] = {
305 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
306 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
307 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
308 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
309 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
311 0x00, 0x79, 0x00
312 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100313 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000314}
315
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100316void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200317{
318/*
319 Name (_PTC, Package (0x02)
320 {
321 ResourceTemplate ()
322 {
323 Register (FFixedHW,
324 0x00, // Bit Width
325 0x00, // Bit Offset
326 0x0000000000000000, // Address
327 ,)
328 },
329
330 ResourceTemplate ()
331 {
332 Register (FFixedHW,
333 0x00, // Bit Width
334 0x00, // Bit Offset
335 0x0000000000000000, // Address
336 ,)
337 }
338 })
339*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200340 acpi_addr_t addr = {
341 .space_id = ACPI_ADDRESS_SPACE_FIXED,
342 .bit_width = 0,
343 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800344 {
345 .resv = 0
346 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200347 .addrl = 0,
348 .addrh = 0,
349 };
350
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100351 acpigen_write_name("_PTC");
352 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200353
354 /* ControlRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100355 acpigen_write_resourcetemplate_header();
356 acpigen_write_register(&addr);
357 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200358
359 /* StatusRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100360 acpigen_write_resourcetemplate_header();
361 acpigen_write_register(&addr);
362 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200363
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100364 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200365}
366
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100367void acpigen_write_method(const char *name, int nargs)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100368{
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100369 /* method op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100370 acpigen_emit_byte(0x14);
371 acpigen_write_len_f();
372 acpigen_emit_namestring(name);
373 acpigen_emit_byte(nargs & 7);
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100374}
375
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100376void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100377{
Duncan Laurie9ccae752016-05-09 07:43:19 -0700378 /* device op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100379 acpigen_emit_byte(0x5b);
380 acpigen_emit_byte(0x82);
381 acpigen_write_len_f();
382 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100383}
384
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100385/*
386 * Generates a func with max supported P-states.
387 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100388void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000389{
390/*
391 Method (_PPC, 0, NotSerialized)
392 {
393 Return (nr)
394 }
395*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100396 acpigen_write_method("_PPC", 0);
Rudolf Marekf997b552009-02-14 15:40:23 +0000397 /* return */
398 acpigen_emit_byte(0xa4);
399 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100400 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100401 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000402}
403
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100404/*
405 * Generates a func with max supported P-states saved
406 * in the variable PPCM.
407 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100408void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700409{
410/*
411 Method (_PPC, 0, NotSerialized)
412 {
413 Return (PPCM)
414 }
415*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100416 acpigen_write_method("_PPC", 0);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700417 /* return */
418 acpigen_emit_byte(0xa4);
419 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100420 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100421 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700422}
423
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100424void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200425{
426/*
427 // Sample _TPC method
428 Method (_TPC, 0, NotSerialized)
429 {
430 Return (\TLVL)
431 }
432 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100433 acpigen_write_method("_TPC", 0);
434 acpigen_emit_byte(0xa4); /* ReturnOp */
435 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100436 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200437}
438
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100439void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000440 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000441{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100442 acpigen_write_package(6);
443 acpigen_write_dword(coreFreq);
444 acpigen_write_dword(power);
445 acpigen_write_dword(transLat);
446 acpigen_write_dword(busmLat);
447 acpigen_write_dword(control);
448 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100449 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700450
451 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
452 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000453}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000454
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100455void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000456{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100457 acpigen_write_name("_PSD");
458 acpigen_write_package(1);
459 acpigen_write_package(5);
460 acpigen_write_byte(5); // 5 values
461 acpigen_write_byte(0); // revision 0
462 acpigen_write_dword(domain);
463 acpigen_write_dword(coordtype);
464 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100465 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100466 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000467}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000468
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100469void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200470{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100471 acpigen_write_package(4);
472 acpigen_write_resourcetemplate_header();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200473 acpigen_write_register(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100474 acpigen_write_resourcetemplate_footer();
475 acpigen_write_dword(cstate->ctype);
476 acpigen_write_dword(cstate->latency);
477 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100478 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200479}
480
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100481void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200482{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100483 int i;
484 acpigen_write_name("_CST");
485 acpigen_write_package(nentries+1);
486 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200487
488 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100489 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200490
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100491 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200492}
493
Timothy Pearson83abd812015-06-08 19:35:06 -0500494void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype, u32 index)
495{
496 acpigen_write_name("_CSD");
497 acpigen_write_package(1);
498 acpigen_write_package(6);
499 acpigen_write_byte(6); // 6 values
500 acpigen_write_byte(0); // revision 0
501 acpigen_write_dword(domain);
502 acpigen_write_dword(coordtype);
503 acpigen_write_dword(numprocs);
504 acpigen_write_dword(index);
505 acpigen_pop_len();
506 acpigen_pop_len();
507}
508
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100509void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200510{
511/*
512 Sample _TSS package with 100% and 50% duty cycles
513 Name (_TSS, Package (0x02)
514 {
515 Package(){100, 1000, 0, 0x00, 0)
516 Package(){50, 520, 0, 0x18, 0)
517 })
518 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100519 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200520 acpi_tstate_t *tstate = tstate_list;
521
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100522 acpigen_write_name("_TSS");
523 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200524
525 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100526 acpigen_write_package(5);
527 acpigen_write_dword(tstate->percent);
528 acpigen_write_dword(tstate->power);
529 acpigen_write_dword(tstate->latency);
530 acpigen_write_dword(tstate->control);
531 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100532 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200533 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200534 }
535
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100536 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200537}
538
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100539void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200540{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100541 acpigen_write_name("_TSD");
542 acpigen_write_package(1);
543 acpigen_write_package(5);
544 acpigen_write_byte(5); // 5 values
545 acpigen_write_byte(0); // revision 0
546 acpigen_write_dword(domain);
547 acpigen_write_dword(coordtype);
548 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100549 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100550 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200551}
552
553
554
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100555void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000556{
557 /*
558 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
559 * Byte 0:
560 * Bit7 : 1 => big item
561 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
562 */
563 acpigen_emit_byte(0x86);
564 /* Byte 1+2: length (0x0009) */
565 acpigen_emit_byte(0x09);
566 acpigen_emit_byte(0x00);
567 /* bit1-7 are ignored */
568 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700569 acpigen_emit_dword(base);
570 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000571}
572
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100573void acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200574{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200575 acpigen_emit_byte(0x82); /* Register Descriptor */
576 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
577 acpigen_emit_byte(0x00); /* Register Length 15:8 */
578 acpigen_emit_byte(addr->space_id); /* Address Space ID */
579 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
580 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
581 acpigen_emit_byte(addr->resv); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700582 acpigen_emit_dword(addr->addrl); /* Register Address Low */
583 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200584}
585
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100586void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100587{
588 /*
589 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
590 * Byte 0:
591 * Bit7 : 0 => small item
592 * Bit6-3: 0100 (0x4) => IRQ port descriptor
593 * Bit2-0: 010 (0x2) => 2 Bytes long
594 */
595 acpigen_emit_byte(0x22);
596 acpigen_emit_byte(mask & 0xff);
597 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100598}
599
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100600void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000601{
602 /*
603 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
604 * Byte 0:
605 * Bit7 : 0 => small item
606 * Bit6-3: 1000 (0x8) => I/O port descriptor
607 * Bit2-0: 111 (0x7) => 7 Bytes long
608 */
609 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100610 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000611 /* bit1-7 are ignored */
612 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
613 /* minimum base address the device may be configured for */
614 acpigen_emit_byte(min & 0xff);
615 acpigen_emit_byte((min >> 8) & 0xff);
616 /* maximum base address the device may be configured for */
617 acpigen_emit_byte(max & 0xff);
618 acpigen_emit_byte((max >> 8) & 0xff);
619 /* alignment for min base */
620 acpigen_emit_byte(align & 0xff);
621 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000622}
623
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100624void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000625{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000626 /*
627 * A ResourceTemplate() is a Buffer() with a
628 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100629 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000630 * (small item 0xf, len 1)
631 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100632 acpigen_emit_byte(0x11); /* Buffer opcode */
633 acpigen_write_len_f();
634 acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000635 len_stack[ltop++] = acpigen_get_current();
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100636 acpigen_emit_byte(0x00);
637 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000638}
639
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100640void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000641{
642 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100643 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000644 /*
645 * end tag (acpi 4.0 Section 6.4.2.8)
646 * 0x79 <checksum>
647 * 0x00 is treated as a good checksum according to the spec
648 * and is what iasl generates.
649 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100650 acpigen_emit_byte(0x79);
651 acpigen_emit_byte(0x00);
652
Martin Rothe3690102016-01-06 15:21:02 -0700653 len = gencurrent - p;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100654
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000655 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100656 p[0] = len & 0xff;
657 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000658 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100659 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000660}
661
662static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
663 struct resource *res)
664{
665 acpigen_write_mem32fixed(0, res->base, res->size);
666}
667
668static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
669 struct resource *res)
670{
671 resource_t base = res->base;
672 resource_t size = res->size;
673 while (size > 0) {
674 resource_t sz = size > 255 ? 255 : size;
675 acpigen_write_io16(base, base, 0, sz, 1);
676 size -= sz;
677 base += sz;
678 }
679}
680
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100681void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000682{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100683 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000684
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100685 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000686 search_global_resources(
687 IORESOURCE_MEM | IORESOURCE_RESERVE,
688 IORESOURCE_MEM | IORESOURCE_RESERVE,
689 acpigen_add_mainboard_rsvd_mem32, 0);
690
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100691 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000692 search_global_resources(
693 IORESOURCE_IO | IORESOURCE_RESERVE,
694 IORESOURCE_IO | IORESOURCE_RESERVE,
695 acpigen_add_mainboard_rsvd_io, 0);
696
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100697 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000698}
699
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100700void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000701{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100702 acpigen_write_scope(scope);
703 acpigen_write_name(name);
704 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100705 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000706}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100707
708static int hex2bin(const char c)
709{
710 if (c >= 'A' && c <= 'F')
711 return c - 'A' + 10;
712 if (c >= 'a' && c <= 'f')
713 return c - 'a' + 10;
714 return c - '0';
715}
716
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100717void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100718{
719 u32 compact = 0;
720
721 /* Clamping individual values would be better but
722 there is a disagreement over what is a valid
723 EISA id, so accept anything and don't clamp,
724 parent code should create a valid EISAid.
725 */
726 compact |= (eisaid[0] - 'A' + 1) << 26;
727 compact |= (eisaid[1] - 'A' + 1) << 21;
728 compact |= (eisaid[2] - 'A' + 1) << 16;
729 compact |= hex2bin(eisaid[3]) << 12;
730 compact |= hex2bin(eisaid[4]) << 8;
731 compact |= hex2bin(eisaid[5]) << 4;
732 compact |= hex2bin(eisaid[6]);
733
734 acpigen_emit_byte(0xc);
735 acpigen_emit_byte((compact >> 24) & 0xff);
736 acpigen_emit_byte((compact >> 16) & 0xff);
737 acpigen_emit_byte((compact >> 8) & 0xff);
738 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100739}