blob: e051c822e650c16a25140129feb7299a5dcf2f9e [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{
209 acpigen_emit_stream(string, string ? 0 : strlen(string));
210 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
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100452/*
453 * Generates a func with max supported P-states.
454 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100455void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000456{
457/*
458 Method (_PPC, 0, NotSerialized)
459 {
460 Return (nr)
461 }
462*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100463 acpigen_write_method("_PPC", 0);
Rudolf Marekf997b552009-02-14 15:40:23 +0000464 /* return */
465 acpigen_emit_byte(0xa4);
466 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100467 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100468 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000469}
470
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100471/*
472 * Generates a func with max supported P-states saved
473 * in the variable PPCM.
474 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100475void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700476{
477/*
478 Method (_PPC, 0, NotSerialized)
479 {
480 Return (PPCM)
481 }
482*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100483 acpigen_write_method("_PPC", 0);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700484 /* return */
485 acpigen_emit_byte(0xa4);
486 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100487 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100488 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700489}
490
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100491void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200492{
493/*
494 // Sample _TPC method
495 Method (_TPC, 0, NotSerialized)
496 {
497 Return (\TLVL)
498 }
499 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100500 acpigen_write_method("_TPC", 0);
501 acpigen_emit_byte(0xa4); /* ReturnOp */
502 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100503 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200504}
505
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100506void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000507 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000508{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100509 acpigen_write_package(6);
510 acpigen_write_dword(coreFreq);
511 acpigen_write_dword(power);
512 acpigen_write_dword(transLat);
513 acpigen_write_dword(busmLat);
514 acpigen_write_dword(control);
515 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100516 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700517
518 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
519 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000520}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000521
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100522void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000523{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100524 acpigen_write_name("_PSD");
525 acpigen_write_package(1);
526 acpigen_write_package(5);
527 acpigen_write_byte(5); // 5 values
528 acpigen_write_byte(0); // revision 0
529 acpigen_write_dword(domain);
530 acpigen_write_dword(coordtype);
531 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100532 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100533 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000534}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000535
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100536void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200537{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100538 acpigen_write_package(4);
539 acpigen_write_resourcetemplate_header();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200540 acpigen_write_register(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100541 acpigen_write_resourcetemplate_footer();
542 acpigen_write_dword(cstate->ctype);
543 acpigen_write_dword(cstate->latency);
544 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100545 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200546}
547
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100548void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200549{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100550 int i;
551 acpigen_write_name("_CST");
552 acpigen_write_package(nentries+1);
553 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200554
555 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100556 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200557
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100558 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200559}
560
Timothy Pearson83abd812015-06-08 19:35:06 -0500561void acpigen_write_CSD_package(u32 domain, u32 numprocs, CSD_coord coordtype, u32 index)
562{
563 acpigen_write_name("_CSD");
564 acpigen_write_package(1);
565 acpigen_write_package(6);
566 acpigen_write_byte(6); // 6 values
567 acpigen_write_byte(0); // revision 0
568 acpigen_write_dword(domain);
569 acpigen_write_dword(coordtype);
570 acpigen_write_dword(numprocs);
571 acpigen_write_dword(index);
572 acpigen_pop_len();
573 acpigen_pop_len();
574}
575
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100576void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200577{
578/*
579 Sample _TSS package with 100% and 50% duty cycles
580 Name (_TSS, Package (0x02)
581 {
582 Package(){100, 1000, 0, 0x00, 0)
583 Package(){50, 520, 0, 0x18, 0)
584 })
585 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100586 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200587 acpi_tstate_t *tstate = tstate_list;
588
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100589 acpigen_write_name("_TSS");
590 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200591
592 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100593 acpigen_write_package(5);
594 acpigen_write_dword(tstate->percent);
595 acpigen_write_dword(tstate->power);
596 acpigen_write_dword(tstate->latency);
597 acpigen_write_dword(tstate->control);
598 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100599 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200600 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200601 }
602
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100603 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200604}
605
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100606void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200607{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100608 acpigen_write_name("_TSD");
609 acpigen_write_package(1);
610 acpigen_write_package(5);
611 acpigen_write_byte(5); // 5 values
612 acpigen_write_byte(0); // revision 0
613 acpigen_write_dword(domain);
614 acpigen_write_dword(coordtype);
615 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100616 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100617 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200618}
619
620
621
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100622void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000623{
624 /*
625 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
626 * Byte 0:
627 * Bit7 : 1 => big item
628 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
629 */
630 acpigen_emit_byte(0x86);
631 /* Byte 1+2: length (0x0009) */
632 acpigen_emit_byte(0x09);
633 acpigen_emit_byte(0x00);
634 /* bit1-7 are ignored */
635 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
Duncan Laurie9ccae752016-05-09 07:43:19 -0700636 acpigen_emit_dword(base);
637 acpigen_emit_dword(size);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000638}
639
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100640void acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200641{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200642 acpigen_emit_byte(0x82); /* Register Descriptor */
643 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
644 acpigen_emit_byte(0x00); /* Register Length 15:8 */
645 acpigen_emit_byte(addr->space_id); /* Address Space ID */
646 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
647 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
648 acpigen_emit_byte(addr->resv); /* Register Access Size */
Duncan Laurie9ccae752016-05-09 07:43:19 -0700649 acpigen_emit_dword(addr->addrl); /* Register Address Low */
650 acpigen_emit_dword(addr->addrh); /* Register Address High */
Sven Schnelle0b86c762011-10-21 21:46:47 +0200651}
652
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100653void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100654{
655 /*
656 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
657 * Byte 0:
658 * Bit7 : 0 => small item
659 * Bit6-3: 0100 (0x4) => IRQ port descriptor
660 * Bit2-0: 010 (0x2) => 2 Bytes long
661 */
662 acpigen_emit_byte(0x22);
663 acpigen_emit_byte(mask & 0xff);
664 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100665}
666
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100667void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000668{
669 /*
670 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
671 * Byte 0:
672 * Bit7 : 0 => small item
673 * Bit6-3: 1000 (0x8) => I/O port descriptor
674 * Bit2-0: 111 (0x7) => 7 Bytes long
675 */
676 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100677 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000678 /* bit1-7 are ignored */
679 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
680 /* minimum base address the device may be configured for */
681 acpigen_emit_byte(min & 0xff);
682 acpigen_emit_byte((min >> 8) & 0xff);
683 /* maximum base address the device may be configured for */
684 acpigen_emit_byte(max & 0xff);
685 acpigen_emit_byte((max >> 8) & 0xff);
686 /* alignment for min base */
687 acpigen_emit_byte(align & 0xff);
688 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000689}
690
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100691void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000692{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000693 /*
694 * A ResourceTemplate() is a Buffer() with a
695 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100696 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000697 * (small item 0xf, len 1)
698 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100699 acpigen_emit_byte(0x11); /* Buffer opcode */
700 acpigen_write_len_f();
701 acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000702 len_stack[ltop++] = acpigen_get_current();
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100703 acpigen_emit_byte(0x00);
704 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000705}
706
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100707void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000708{
709 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100710 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000711 /*
712 * end tag (acpi 4.0 Section 6.4.2.8)
713 * 0x79 <checksum>
714 * 0x00 is treated as a good checksum according to the spec
715 * and is what iasl generates.
716 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100717 acpigen_emit_byte(0x79);
718 acpigen_emit_byte(0x00);
719
Martin Rothe3690102016-01-06 15:21:02 -0700720 len = gencurrent - p;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100721
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000722 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100723 p[0] = len & 0xff;
724 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000725 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100726 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000727}
728
729static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
730 struct resource *res)
731{
732 acpigen_write_mem32fixed(0, res->base, res->size);
733}
734
735static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
736 struct resource *res)
737{
738 resource_t base = res->base;
739 resource_t size = res->size;
740 while (size > 0) {
741 resource_t sz = size > 255 ? 255 : size;
742 acpigen_write_io16(base, base, 0, sz, 1);
743 size -= sz;
744 base += sz;
745 }
746}
747
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100748void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000749{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100750 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000751
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100752 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000753 search_global_resources(
754 IORESOURCE_MEM | IORESOURCE_RESERVE,
755 IORESOURCE_MEM | IORESOURCE_RESERVE,
756 acpigen_add_mainboard_rsvd_mem32, 0);
757
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100758 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000759 search_global_resources(
760 IORESOURCE_IO | IORESOURCE_RESERVE,
761 IORESOURCE_IO | IORESOURCE_RESERVE,
762 acpigen_add_mainboard_rsvd_io, 0);
763
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100764 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000765}
766
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100767void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000768{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100769 acpigen_write_scope(scope);
770 acpigen_write_name(name);
771 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100772 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000773}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100774
775static int hex2bin(const char c)
776{
777 if (c >= 'A' && c <= 'F')
778 return c - 'A' + 10;
779 if (c >= 'a' && c <= 'f')
780 return c - 'a' + 10;
781 return c - '0';
782}
783
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100784void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100785{
786 u32 compact = 0;
787
788 /* Clamping individual values would be better but
789 there is a disagreement over what is a valid
790 EISA id, so accept anything and don't clamp,
791 parent code should create a valid EISAid.
792 */
793 compact |= (eisaid[0] - 'A' + 1) << 26;
794 compact |= (eisaid[1] - 'A' + 1) << 21;
795 compact |= (eisaid[2] - 'A' + 1) << 16;
796 compact |= hex2bin(eisaid[3]) << 12;
797 compact |= hex2bin(eisaid[4]) << 8;
798 compact |= hex2bin(eisaid[5]) << 4;
799 compact |= hex2bin(eisaid[6]);
800
801 acpigen_emit_byte(0xc);
802 acpigen_emit_byte((compact >> 24) & 0xff);
803 acpigen_emit_byte((compact >> 16) & 0xff);
804 acpigen_emit_byte((compact >> 8) & 0xff);
805 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100806}