blob: f274ea2177b2cf108f6665331e565cf7c31aedd9 [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
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700144void acpigen_write_name_string(const char *name, const char *string)
145{
146 acpigen_write_name(name);
147 acpigen_write_string(string);
148}
149
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100150void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000151{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000152 int i;
153 for (i = 0; i < size; i++) {
154 acpigen_emit_byte(data[i]);
155 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000156}
157
Duncan Laurie56b69aa2016-05-09 08:17:02 -0700158void acpigen_emit_string(const char *string)
159{
160 acpigen_emit_stream(string, string ? 0 : strlen(string));
161 acpigen_emit_byte('\0'); /* NUL */
162}
163
164void acpigen_write_string(const char *string)
165{
166 acpigen_emit_byte(0x0d);
167 acpigen_emit_string(string);
168}
169
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100170/*
171 * The naming conventions for ACPI namespace names are a bit tricky as
172 * each element has to be 4 chars wide (»All names are a fixed 32 bits.«)
173 * and »By convention, when an ASL compiler pads a name shorter than 4
174 * characters, it is done so with trailing underscores (‘_’).«.
175 *
176 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
177 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000178
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100179static void acpigen_emit_simple_namestring(const char *name) {
180 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000181 char ud[] = "____";
182 for (i = 0; i < 4; i++) {
183 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100184 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000185 break;
186 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100187 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000188 }
189 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000190}
191
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100192static void acpigen_emit_double_namestring(const char *name, int dotpos) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000193 /* mark dual name prefix */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100194 acpigen_emit_byte(0x2e);
195 acpigen_emit_simple_namestring(name);
196 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000197}
198
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100199static void acpigen_emit_multi_namestring(const char *name) {
200 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000201 unsigned char *pathlen;
Rudolf Marek3310d752009-06-21 20:26:13 +0000202 /* mark multi name prefix */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100203 acpigen_emit_byte(0x2f);
204 acpigen_emit_byte(0x0);
Rudolf Marek3310d752009-06-21 20:26:13 +0000205 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
206
207 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100208 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000209 /* find end or next entity */
210 while ((name[0] != '.') && (name[0] != '\0'))
211 name++;
212 /* forward to next */
213 if (name[0] == '.')
214 name++;
215 count++;
216 }
217
218 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000219}
220
221
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100222void acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000223 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000224 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000225
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600226 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000227 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100228 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000229 namepath++;
230 }
231
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600232 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000233 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100234 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000235 namepath++;
236 }
237
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200238 /* If we have only \\ or only ^...^. Then we need to put a null
239 name (0x00). */
240 if(namepath[0] == '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100241 acpigen_emit_byte(0x00);
242 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200243 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000244
245 i = 0;
246 while (namepath[i] != '\0') {
247 if (namepath[i] == '.') {
248 dotcount++;
249 dotpos = i;
250 }
251 i++;
252 }
253
254 if (dotcount == 0) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100255 acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000256 } else if (dotcount == 1) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100257 acpigen_emit_double_namestring(namepath, dotpos);
Rudolf Marek3310d752009-06-21 20:26:13 +0000258 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100259 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000260 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000261}
262
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100263void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000264{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000265 /* name op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100266 acpigen_emit_byte(0x8);
267 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000268}
269
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100270void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000271{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000272 /* scope op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100273 acpigen_emit_byte(0x10);
274 acpigen_write_len_f();
275 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000276}
Rudolf Marekf997b552009-02-14 15:40:23 +0000277
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100278void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000279{
280/*
281 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
282 {
283*/
284 char pscope[16];
Rudolf Marekf997b552009-02-14 15:40:23 +0000285 /* processor op */
286 acpigen_emit_byte(0x5b);
287 acpigen_emit_byte(0x83);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100288 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000289
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100290 snprintf(pscope, sizeof (pscope),
Timothy Pearson033bb4b2015-02-10 22:21:39 -0600291 "\\_PR.CP%02d", (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100292 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000293 acpigen_emit_byte(cpuindex);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700294 acpigen_emit_dword(pblock_addr);
Rudolf Marekf997b552009-02-14 15:40:23 +0000295 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000296}
297
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100298void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000299{
300/*
301 Name (_PCT, Package (0x02)
302 {
303 ResourceTemplate ()
304 {
305 Register (FFixedHW,
306 0x00, // Bit Width
307 0x00, // Bit Offset
308 0x0000000000000000, // Address
309 ,)
310 },
311
312 ResourceTemplate ()
313 {
314 Register (FFixedHW,
315 0x00, // Bit Width
316 0x00, // Bit Offset
317 0x0000000000000000, // Address
318 ,)
319 }
320 })
321*/
322 static char stream[] = {
323 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
324 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
325 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
326 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
327 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
329 0x00, 0x79, 0x00
330 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100331 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000332}
333
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100334void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200335{
336/*
337 Name (_PTC, Package (0x02)
338 {
339 ResourceTemplate ()
340 {
341 Register (FFixedHW,
342 0x00, // Bit Width
343 0x00, // Bit Offset
344 0x0000000000000000, // Address
345 ,)
346 },
347
348 ResourceTemplate ()
349 {
350 Register (FFixedHW,
351 0x00, // Bit Width
352 0x00, // Bit Offset
353 0x0000000000000000, // Address
354 ,)
355 }
356 })
357*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200358 acpi_addr_t addr = {
359 .space_id = ACPI_ADDRESS_SPACE_FIXED,
360 .bit_width = 0,
361 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800362 {
363 .resv = 0
364 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200365 .addrl = 0,
366 .addrh = 0,
367 };
368
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100369 acpigen_write_name("_PTC");
370 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200371
372 /* ControlRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100373 acpigen_write_resourcetemplate_header();
374 acpigen_write_register(&addr);
375 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200376
377 /* StatusRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100378 acpigen_write_resourcetemplate_header();
379 acpigen_write_register(&addr);
380 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200381
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100382 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200383}
384
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100385void acpigen_write_method(const char *name, int nargs)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100386{
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100387 /* method op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100388 acpigen_emit_byte(0x14);
389 acpigen_write_len_f();
390 acpigen_emit_namestring(name);
391 acpigen_emit_byte(nargs & 7);
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100392}
393
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100394void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100395{
Duncan Laurie9ccae752016-05-09 07:43:19 -0700396 /* device op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100397 acpigen_emit_byte(0x5b);
398 acpigen_emit_byte(0x82);
399 acpigen_write_len_f();
400 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100401}
402
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100403/*
404 * Generates a func with max supported P-states.
405 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100406void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000407{
408/*
409 Method (_PPC, 0, NotSerialized)
410 {
411 Return (nr)
412 }
413*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100414 acpigen_write_method("_PPC", 0);
Rudolf Marekf997b552009-02-14 15:40:23 +0000415 /* return */
416 acpigen_emit_byte(0xa4);
417 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100418 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100419 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000420}
421
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100422/*
423 * Generates a func with max supported P-states saved
424 * in the variable PPCM.
425 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100426void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700427{
428/*
429 Method (_PPC, 0, NotSerialized)
430 {
431 Return (PPCM)
432 }
433*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100434 acpigen_write_method("_PPC", 0);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700435 /* return */
436 acpigen_emit_byte(0xa4);
437 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100438 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100439 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700440}
441
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100442void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200443{
444/*
445 // Sample _TPC method
446 Method (_TPC, 0, NotSerialized)
447 {
448 Return (\TLVL)
449 }
450 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100451 acpigen_write_method("_TPC", 0);
452 acpigen_emit_byte(0xa4); /* ReturnOp */
453 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100454 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200455}
456
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100457void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000458 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000459{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100460 acpigen_write_package(6);
461 acpigen_write_dword(coreFreq);
462 acpigen_write_dword(power);
463 acpigen_write_dword(transLat);
464 acpigen_write_dword(busmLat);
465 acpigen_write_dword(control);
466 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100467 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700468
469 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
470 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000471}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000472
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100473void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000474{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100475 acpigen_write_name("_PSD");
476 acpigen_write_package(1);
477 acpigen_write_package(5);
478 acpigen_write_byte(5); // 5 values
479 acpigen_write_byte(0); // revision 0
480 acpigen_write_dword(domain);
481 acpigen_write_dword(coordtype);
482 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100483 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100484 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000485}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000486
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100487void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200488{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100489 acpigen_write_package(4);
490 acpigen_write_resourcetemplate_header();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200491 acpigen_write_register(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100492 acpigen_write_resourcetemplate_footer();
493 acpigen_write_dword(cstate->ctype);
494 acpigen_write_dword(cstate->latency);
495 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100496 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200497}
498
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100499void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200500{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100501 int i;
502 acpigen_write_name("_CST");
503 acpigen_write_package(nentries+1);
504 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200505
506 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100507 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200508
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100509 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200510}
511
Timothy Pearson83abd812015-06-08 19:35:06 -0500512void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype, u32 index)
513{
514 acpigen_write_name("_CSD");
515 acpigen_write_package(1);
516 acpigen_write_package(6);
517 acpigen_write_byte(6); // 6 values
518 acpigen_write_byte(0); // revision 0
519 acpigen_write_dword(domain);
520 acpigen_write_dword(coordtype);
521 acpigen_write_dword(numprocs);
522 acpigen_write_dword(index);
523 acpigen_pop_len();
524 acpigen_pop_len();
525}
526
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100527void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200528{
529/*
530 Sample _TSS package with 100% and 50% duty cycles
531 Name (_TSS, Package (0x02)
532 {
533 Package(){100, 1000, 0, 0x00, 0)
534 Package(){50, 520, 0, 0x18, 0)
535 })
536 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100537 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200538 acpi_tstate_t *tstate = tstate_list;
539
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100540 acpigen_write_name("_TSS");
541 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200542
543 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100544 acpigen_write_package(5);
545 acpigen_write_dword(tstate->percent);
546 acpigen_write_dword(tstate->power);
547 acpigen_write_dword(tstate->latency);
548 acpigen_write_dword(tstate->control);
549 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100550 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200551 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200552 }
553
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100554 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200555}
556
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100557void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200558{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100559 acpigen_write_name("_TSD");
560 acpigen_write_package(1);
561 acpigen_write_package(5);
562 acpigen_write_byte(5); // 5 values
563 acpigen_write_byte(0); // revision 0
564 acpigen_write_dword(domain);
565 acpigen_write_dword(coordtype);
566 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100567 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100568 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200569}
570
571
572
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100573void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000574{
575 /*
576 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
577 * Byte 0:
578 * Bit7 : 1 => big item
579 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
580 */
581 acpigen_emit_byte(0x86);
582 /* Byte 1+2: length (0x0009) */
583 acpigen_emit_byte(0x09);
584 acpigen_emit_byte(0x00);
585 /* bit1-7 are ignored */
586 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700587 acpigen_emit_dword(base);
588 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000589}
590
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100591void acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200592{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200593 acpigen_emit_byte(0x82); /* Register Descriptor */
594 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
595 acpigen_emit_byte(0x00); /* Register Length 15:8 */
596 acpigen_emit_byte(addr->space_id); /* Address Space ID */
597 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
598 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
599 acpigen_emit_byte(addr->resv); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700600 acpigen_emit_dword(addr->addrl); /* Register Address Low */
601 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200602}
603
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100604void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100605{
606 /*
607 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
608 * Byte 0:
609 * Bit7 : 0 => small item
610 * Bit6-3: 0100 (0x4) => IRQ port descriptor
611 * Bit2-0: 010 (0x2) => 2 Bytes long
612 */
613 acpigen_emit_byte(0x22);
614 acpigen_emit_byte(mask & 0xff);
615 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100616}
617
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100618void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000619{
620 /*
621 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
622 * Byte 0:
623 * Bit7 : 0 => small item
624 * Bit6-3: 1000 (0x8) => I/O port descriptor
625 * Bit2-0: 111 (0x7) => 7 Bytes long
626 */
627 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100628 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000629 /* bit1-7 are ignored */
630 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
631 /* minimum base address the device may be configured for */
632 acpigen_emit_byte(min & 0xff);
633 acpigen_emit_byte((min >> 8) & 0xff);
634 /* maximum base address the device may be configured for */
635 acpigen_emit_byte(max & 0xff);
636 acpigen_emit_byte((max >> 8) & 0xff);
637 /* alignment for min base */
638 acpigen_emit_byte(align & 0xff);
639 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000640}
641
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100642void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000643{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000644 /*
645 * A ResourceTemplate() is a Buffer() with a
646 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100647 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000648 * (small item 0xf, len 1)
649 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100650 acpigen_emit_byte(0x11); /* Buffer opcode */
651 acpigen_write_len_f();
652 acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000653 len_stack[ltop++] = acpigen_get_current();
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100654 acpigen_emit_byte(0x00);
655 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000656}
657
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100658void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000659{
660 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100661 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000662 /*
663 * end tag (acpi 4.0 Section 6.4.2.8)
664 * 0x79 <checksum>
665 * 0x00 is treated as a good checksum according to the spec
666 * and is what iasl generates.
667 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100668 acpigen_emit_byte(0x79);
669 acpigen_emit_byte(0x00);
670
Martin Rothe3690102016-01-06 15:21:02 -0700671 len = gencurrent - p;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100672
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000673 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100674 p[0] = len & 0xff;
675 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000676 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100677 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000678}
679
680static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
681 struct resource *res)
682{
683 acpigen_write_mem32fixed(0, res->base, res->size);
684}
685
686static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
687 struct resource *res)
688{
689 resource_t base = res->base;
690 resource_t size = res->size;
691 while (size > 0) {
692 resource_t sz = size > 255 ? 255 : size;
693 acpigen_write_io16(base, base, 0, sz, 1);
694 size -= sz;
695 base += sz;
696 }
697}
698
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100699void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000700{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100701 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000702
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100703 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000704 search_global_resources(
705 IORESOURCE_MEM | IORESOURCE_RESERVE,
706 IORESOURCE_MEM | IORESOURCE_RESERVE,
707 acpigen_add_mainboard_rsvd_mem32, 0);
708
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100709 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000710 search_global_resources(
711 IORESOURCE_IO | IORESOURCE_RESERVE,
712 IORESOURCE_IO | IORESOURCE_RESERVE,
713 acpigen_add_mainboard_rsvd_io, 0);
714
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100715 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000716}
717
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100718void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000719{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100720 acpigen_write_scope(scope);
721 acpigen_write_name(name);
722 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100723 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000724}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100725
726static int hex2bin(const char c)
727{
728 if (c >= 'A' && c <= 'F')
729 return c - 'A' + 10;
730 if (c >= 'a' && c <= 'f')
731 return c - 'a' + 10;
732 return c - '0';
733}
734
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100735void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100736{
737 u32 compact = 0;
738
739 /* Clamping individual values would be better but
740 there is a disagreement over what is a valid
741 EISA id, so accept anything and don't clamp,
742 parent code should create a valid EISAid.
743 */
744 compact |= (eisaid[0] - 'A' + 1) << 26;
745 compact |= (eisaid[1] - 'A' + 1) << 21;
746 compact |= (eisaid[2] - 'A' + 1) << 16;
747 compact |= hex2bin(eisaid[3]) << 12;
748 compact |= hex2bin(eisaid[4]) << 8;
749 compact |= hex2bin(eisaid[5]) << 4;
750 compact |= hex2bin(eisaid[6]);
751
752 acpigen_emit_byte(0xc);
753 acpigen_emit_byte((compact >> 24) & 0xff);
754 acpigen_emit_byte((compact >> 16) & 0xff);
755 acpigen_emit_byte((compact >> 8) & 0xff);
756 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100757}