blob: 3aa823c3633ab873ea16db0e8a48550bd8fe31af [file] [log] [blame]
Rudolf Marek293b5f52009-02-01 18:35:15 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2009 Rudolf Marek <r.marek@assembler.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
Uwe Hermannc70e9fc2010-02-15 23:10:19 +00007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
Rudolf Marek293b5f52009-02-01 18:35:15 +00009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Rudolf Marek293b5f52009-02-01 18:35:15 +000018 */
19
Paul Menzel0f4c0e22013-02-22 12:33:08 +010020/* How much nesting do we support? */
Rudolf Marek293b5f52009-02-01 18:35:15 +000021#define ACPIGEN_LENSTACK_SIZE 10
22
Paul Menzel0f4c0e22013-02-22 12:33:08 +010023/*
24 * If you need to change this, change acpigen_write_f and
Vladimir Serbinenko8104da72014-11-09 03:33:51 +010025 * acpigen_pop_len
Paul Menzel0f4c0e22013-02-22 12:33:08 +010026 */
Rudolf Marek293b5f52009-02-01 18:35:15 +000027
28#define ACPIGEN_MAXLEN 0xfff
29
30#include <string.h>
31#include <arch/acpigen.h>
32#include <console/console.h>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000033#include <device/device.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000034
35static char *gencurrent;
36
37char *len_stack[ACPIGEN_LENSTACK_SIZE];
38int ltop = 0;
39
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010040void acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000041{
42 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Paul Menzel0f4c0e22013-02-22 12:33:08 +010043 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000044 acpigen_emit_byte(0);
45 acpigen_emit_byte(0);
Rudolf Marek293b5f52009-02-01 18:35:15 +000046}
47
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010048void acpigen_pop_len(void)
49{
50 int len;
51 ASSERT(ltop > 0)
52 char *p = len_stack[--ltop];
53 len = gencurrent - p;
54 ASSERT(len <= ACPIGEN_MAXLEN)
55 /* generate store length for 0xfff max */
56 p[0] = (0x40 | (len & 0xf));
57 p[1] = (len >> 4 & 0xff);
58
59}
60
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000061void acpigen_set_current(char *curr)
62{
63 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000064}
65
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000066char *acpigen_get_current(void)
67{
68 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000069}
70
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010071void acpigen_emit_byte(unsigned char b)
Rudolf Marek293b5f52009-02-01 18:35:15 +000072{
73 (*gencurrent++) = b;
Rudolf Marek293b5f52009-02-01 18:35:15 +000074}
75
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010076void acpigen_write_package(int nr_el)
Rudolf Marek293b5f52009-02-01 18:35:15 +000077{
Rudolf Marek293b5f52009-02-01 18:35:15 +000078 /* package op */
79 acpigen_emit_byte(0x12);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010080 acpigen_write_len_f();
Rudolf Marek293b5f52009-02-01 18:35:15 +000081 acpigen_emit_byte(nr_el);
Rudolf Marek293b5f52009-02-01 18:35:15 +000082}
83
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010084void acpigen_write_byte(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000085{
86 /* byte op */
87 acpigen_emit_byte(0xa);
88 acpigen_emit_byte(data & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +000089}
90
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +010091void acpigen_write_dword(unsigned int data)
Rudolf Marek293b5f52009-02-01 18:35:15 +000092{
93 /* dword op */
94 acpigen_emit_byte(0xc);
95 acpigen_emit_byte(data & 0xff);
96 acpigen_emit_byte((data >> 8) & 0xff);
97 acpigen_emit_byte((data >> 16) & 0xff);
98 acpigen_emit_byte((data >> 24) & 0xff);
Rudolf Marek293b5f52009-02-01 18:35:15 +000099}
100
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100101void acpigen_write_qword(uint64_t data)
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000102{
103 /* qword op */
104 acpigen_emit_byte(0xe);
105 acpigen_emit_byte(data & 0xff);
106 acpigen_emit_byte((data >> 8) & 0xff);
107 acpigen_emit_byte((data >> 16) & 0xff);
108 acpigen_emit_byte((data >> 24) & 0xff);
109 acpigen_emit_byte((data >> 32) & 0xff);
110 acpigen_emit_byte((data >> 40) & 0xff);
111 acpigen_emit_byte((data >> 48) & 0xff);
112 acpigen_emit_byte((data >> 56) & 0xff);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000113}
114
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100115void acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000116{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100117 acpigen_write_name(name);
118 acpigen_write_byte(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000119}
120
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100121void acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000122{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100123 acpigen_write_name(name);
124 acpigen_write_dword(val);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000125}
126
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100127void acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000128{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100129 acpigen_write_name(name);
130 acpigen_write_qword(val);
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000131}
132
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100133void acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000134{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000135 int i;
136 for (i = 0; i < size; i++) {
137 acpigen_emit_byte(data[i]);
138 }
Rudolf Marek293b5f52009-02-01 18:35:15 +0000139}
140
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100141/*
142 * The naming conventions for ACPI namespace names are a bit tricky as
143 * each element has to be 4 chars wide (»All names are a fixed 32 bits.«)
144 * and »By convention, when an ASL compiler pads a name shorter than 4
145 * characters, it is done so with trailing underscores (‘_’).«.
146 *
147 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
148 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000149
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100150static void acpigen_emit_simple_namestring(const char *name) {
151 int i;
Rudolf Marek3310d752009-06-21 20:26:13 +0000152 char ud[] = "____";
153 for (i = 0; i < 4; i++) {
154 if ((name[i] == '\0') || (name[i] == '.')) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100155 acpigen_emit_stream(ud, 4 - i);
Rudolf Marek3310d752009-06-21 20:26:13 +0000156 break;
157 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100158 acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000159 }
160 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000161}
162
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100163static void acpigen_emit_double_namestring(const char *name, int dotpos) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000164 /* mark dual name prefix */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100165 acpigen_emit_byte(0x2e);
166 acpigen_emit_simple_namestring(name);
167 acpigen_emit_simple_namestring(&name[dotpos + 1]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000168}
169
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100170static void acpigen_emit_multi_namestring(const char *name) {
171 int count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000172 unsigned char *pathlen;
Rudolf Marek3310d752009-06-21 20:26:13 +0000173 /* mark multi name prefix */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100174 acpigen_emit_byte(0x2f);
175 acpigen_emit_byte(0x0);
Rudolf Marek3310d752009-06-21 20:26:13 +0000176 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
177
178 while (name[0] != '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100179 acpigen_emit_simple_namestring(name);
Rudolf Marek3310d752009-06-21 20:26:13 +0000180 /* find end or next entity */
181 while ((name[0] != '.') && (name[0] != '\0'))
182 name++;
183 /* forward to next */
184 if (name[0] == '.')
185 name++;
186 count++;
187 }
188
189 pathlen[0] = count;
Rudolf Marek3310d752009-06-21 20:26:13 +0000190}
191
192
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100193void acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000194 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000195 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000196
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600197 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000198 if (namepath[0] == '\\') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100199 acpigen_emit_byte('\\');
Rudolf Marek3310d752009-06-21 20:26:13 +0000200 namepath++;
201 }
202
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600203 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000204 while (namepath[0] == '^') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100205 acpigen_emit_byte('^');
Rudolf Marek3310d752009-06-21 20:26:13 +0000206 namepath++;
207 }
208
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200209 /* If we have only \\ or only ^...^. Then we need to put a null
210 name (0x00). */
211 if(namepath[0] == '\0') {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100212 acpigen_emit_byte(0x00);
213 return;
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200214 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000215
216 i = 0;
217 while (namepath[i] != '\0') {
218 if (namepath[i] == '.') {
219 dotcount++;
220 dotpos = i;
221 }
222 i++;
223 }
224
225 if (dotcount == 0) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100226 acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000227 } else if (dotcount == 1) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100228 acpigen_emit_double_namestring(namepath, dotpos);
Rudolf Marek3310d752009-06-21 20:26:13 +0000229 } else {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100230 acpigen_emit_multi_namestring(namepath);
Rudolf Marek3310d752009-06-21 20:26:13 +0000231 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000232}
233
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100234void acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000235{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000236 /* name op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100237 acpigen_emit_byte(0x8);
238 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000239}
240
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100241void acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000242{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000243 /* scope op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100244 acpigen_emit_byte(0x10);
245 acpigen_write_len_f();
246 acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000247}
Rudolf Marekf997b552009-02-14 15:40:23 +0000248
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100249void acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
Rudolf Marekf997b552009-02-14 15:40:23 +0000250{
251/*
252 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
253 {
254*/
255 char pscope[16];
Rudolf Marekf997b552009-02-14 15:40:23 +0000256 /* processor op */
257 acpigen_emit_byte(0x5b);
258 acpigen_emit_byte(0x83);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100259 acpigen_write_len_f();
Rudolf Marekf997b552009-02-14 15:40:23 +0000260
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100261 snprintf(pscope, sizeof (pscope),
Timothy Pearson033bb4b2015-02-10 22:21:39 -0600262 "\\_PR.CP%02d", (unsigned int) cpuindex);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100263 acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000264 acpigen_emit_byte(cpuindex);
265 acpigen_emit_byte(pblock_addr & 0xff);
266 acpigen_emit_byte((pblock_addr >> 8) & 0xff);
267 acpigen_emit_byte((pblock_addr >> 16) & 0xff);
268 acpigen_emit_byte((pblock_addr >> 24) & 0xff);
269 acpigen_emit_byte(pblock_len);
Rudolf Marekf997b552009-02-14 15:40:23 +0000270}
271
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100272void acpigen_write_empty_PCT(void)
Rudolf Marekf997b552009-02-14 15:40:23 +0000273{
274/*
275 Name (_PCT, Package (0x02)
276 {
277 ResourceTemplate ()
278 {
279 Register (FFixedHW,
280 0x00, // Bit Width
281 0x00, // Bit Offset
282 0x0000000000000000, // Address
283 ,)
284 },
285
286 ResourceTemplate ()
287 {
288 Register (FFixedHW,
289 0x00, // Bit Width
290 0x00, // Bit Offset
291 0x0000000000000000, // Address
292 ,)
293 }
294 })
295*/
296 static char stream[] = {
297 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
298 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
299 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
300 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
301 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
302 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
303 0x00, 0x79, 0x00
304 };
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100305 acpigen_emit_stream(stream, ARRAY_SIZE(stream));
Rudolf Marekf997b552009-02-14 15:40:23 +0000306}
307
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100308void acpigen_write_empty_PTC(void)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200309{
310/*
311 Name (_PTC, Package (0x02)
312 {
313 ResourceTemplate ()
314 {
315 Register (FFixedHW,
316 0x00, // Bit Width
317 0x00, // Bit Offset
318 0x0000000000000000, // Address
319 ,)
320 },
321
322 ResourceTemplate ()
323 {
324 Register (FFixedHW,
325 0x00, // Bit Width
326 0x00, // Bit Offset
327 0x0000000000000000, // Address
328 ,)
329 }
330 })
331*/
Stefan Reinauer39205c62012-04-27 21:49:28 +0200332 acpi_addr_t addr = {
333 .space_id = ACPI_ADDRESS_SPACE_FIXED,
334 .bit_width = 0,
335 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800336 {
337 .resv = 0
338 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200339 .addrl = 0,
340 .addrh = 0,
341 };
342
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100343 acpigen_write_name("_PTC");
344 acpigen_write_package(2);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200345
346 /* ControlRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100347 acpigen_write_resourcetemplate_header();
348 acpigen_write_register(&addr);
349 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200350
351 /* StatusRegister */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100352 acpigen_write_resourcetemplate_header();
353 acpigen_write_register(&addr);
354 acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200355
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100356 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200357}
358
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100359void acpigen_write_method(const char *name, int nargs)
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100360{
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100361 /* method op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100362 acpigen_emit_byte(0x14);
363 acpigen_write_len_f();
364 acpigen_emit_namestring(name);
365 acpigen_emit_byte(nargs & 7);
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100366}
367
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100368void acpigen_write_device(const char *name)
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100369{
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100370 /* method op */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100371 acpigen_emit_byte(0x5b);
372 acpigen_emit_byte(0x82);
373 acpigen_write_len_f();
374 acpigen_emit_namestring(name);
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100375}
376
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100377/*
378 * Generates a func with max supported P-states.
379 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100380void acpigen_write_PPC(u8 nr)
Rudolf Marekf997b552009-02-14 15:40:23 +0000381{
382/*
383 Method (_PPC, 0, NotSerialized)
384 {
385 Return (nr)
386 }
387*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100388 acpigen_write_method("_PPC", 0);
Rudolf Marekf997b552009-02-14 15:40:23 +0000389 /* return */
390 acpigen_emit_byte(0xa4);
391 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100392 acpigen_write_byte(nr);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100393 acpigen_pop_len();
Rudolf Marekf997b552009-02-14 15:40:23 +0000394}
395
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100396/*
397 * Generates a func with max supported P-states saved
398 * in the variable PPCM.
399 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100400void acpigen_write_PPC_NVS(void)
Duncan Laurie0eefa002012-07-16 12:11:53 -0700401{
402/*
403 Method (_PPC, 0, NotSerialized)
404 {
405 Return (PPCM)
406 }
407*/
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100408 acpigen_write_method("_PPC", 0);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700409 /* return */
410 acpigen_emit_byte(0xa4);
411 /* arg */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100412 acpigen_emit_namestring("PPCM");
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100413 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700414}
415
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100416void acpigen_write_TPC(const char *gnvs_tpc_limit)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200417{
418/*
419 // Sample _TPC method
420 Method (_TPC, 0, NotSerialized)
421 {
422 Return (\TLVL)
423 }
424 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100425 acpigen_write_method("_TPC", 0);
426 acpigen_emit_byte(0xa4); /* ReturnOp */
427 acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100428 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200429}
430
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100431void acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000432 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000433{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100434 acpigen_write_package(6);
435 acpigen_write_dword(coreFreq);
436 acpigen_write_dword(power);
437 acpigen_write_dword(transLat);
438 acpigen_write_dword(busmLat);
439 acpigen_write_dword(control);
440 acpigen_write_dword(status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100441 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700442
443 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
444 coreFreq, power, control, status);
Rudolf Marekf997b552009-02-14 15:40:23 +0000445}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000446
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100447void acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Patrick Georgidf444bf2009-04-21 20:34:36 +0000448{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100449 acpigen_write_name("_PSD");
450 acpigen_write_package(1);
451 acpigen_write_package(5);
452 acpigen_write_byte(5); // 5 values
453 acpigen_write_byte(0); // revision 0
454 acpigen_write_dword(domain);
455 acpigen_write_dword(coordtype);
456 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100457 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100458 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000459}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000460
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100461void acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200462{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100463 acpigen_write_package(4);
464 acpigen_write_resourcetemplate_header();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200465 acpigen_write_register(&cstate->resource);
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100466 acpigen_write_resourcetemplate_footer();
467 acpigen_write_dword(cstate->ctype);
468 acpigen_write_dword(cstate->latency);
469 acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100470 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200471}
472
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100473void acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200474{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100475 int i;
476 acpigen_write_name("_CST");
477 acpigen_write_package(nentries+1);
478 acpigen_write_dword(nentries);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200479
480 for (i = 0; i < nentries; i++)
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100481 acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200482
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100483 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200484}
485
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100486void acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200487{
488/*
489 Sample _TSS package with 100% and 50% duty cycles
490 Name (_TSS, Package (0x02)
491 {
492 Package(){100, 1000, 0, 0x00, 0)
493 Package(){50, 520, 0, 0x18, 0)
494 })
495 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100496 int i;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200497 acpi_tstate_t *tstate = tstate_list;
498
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100499 acpigen_write_name("_TSS");
500 acpigen_write_package(entries);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200501
502 for (i = 0; i < entries; i++) {
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100503 acpigen_write_package(5);
504 acpigen_write_dword(tstate->percent);
505 acpigen_write_dword(tstate->power);
506 acpigen_write_dword(tstate->latency);
507 acpigen_write_dword(tstate->control);
508 acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100509 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200510 tstate++;
Stefan Reinauer39205c62012-04-27 21:49:28 +0200511 }
512
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100513 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200514}
515
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100516void acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
Stefan Reinauer39205c62012-04-27 21:49:28 +0200517{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100518 acpigen_write_name("_TSD");
519 acpigen_write_package(1);
520 acpigen_write_package(5);
521 acpigen_write_byte(5); // 5 values
522 acpigen_write_byte(0); // revision 0
523 acpigen_write_dword(domain);
524 acpigen_write_dword(coordtype);
525 acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100526 acpigen_pop_len();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100527 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200528}
529
530
531
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100532void acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000533{
534 /*
535 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
536 * Byte 0:
537 * Bit7 : 1 => big item
538 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
539 */
540 acpigen_emit_byte(0x86);
541 /* Byte 1+2: length (0x0009) */
542 acpigen_emit_byte(0x09);
543 acpigen_emit_byte(0x00);
544 /* bit1-7 are ignored */
545 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
546 acpigen_emit_byte(base & 0xff);
547 acpigen_emit_byte((base >> 8) & 0xff);
548 acpigen_emit_byte((base >> 16) & 0xff);
549 acpigen_emit_byte((base >> 24) & 0xff);
550 acpigen_emit_byte(size & 0xff);
551 acpigen_emit_byte((size >> 8) & 0xff);
552 acpigen_emit_byte((size >> 16) & 0xff);
553 acpigen_emit_byte((size >> 24) & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000554}
555
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100556void acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200557{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200558 acpigen_emit_byte(0x82); /* Register Descriptor */
559 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
560 acpigen_emit_byte(0x00); /* Register Length 15:8 */
561 acpigen_emit_byte(addr->space_id); /* Address Space ID */
562 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
563 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
564 acpigen_emit_byte(addr->resv); /* Register Access Size */
565 acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */
566 acpigen_emit_byte((addr->addrl >> 8) & 0xff);
567 acpigen_emit_byte((addr->addrl >> 16) & 0xff);
568 acpigen_emit_byte((addr->addrl >> 24) & 0xff);
569 acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */
570 acpigen_emit_byte((addr->addrh >> 8) & 0xff);
571 acpigen_emit_byte((addr->addrh >> 16) & 0xff);
572 acpigen_emit_byte((addr->addrh >> 24) & 0xff);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200573}
574
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100575void acpigen_write_irq(u16 mask)
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100576{
577 /*
578 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
579 * Byte 0:
580 * Bit7 : 0 => small item
581 * Bit6-3: 0100 (0x4) => IRQ port descriptor
582 * Bit2-0: 010 (0x2) => 2 Bytes long
583 */
584 acpigen_emit_byte(0x22);
585 acpigen_emit_byte(mask & 0xff);
586 acpigen_emit_byte((mask >> 8) & 0xff);
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100587}
588
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100589void acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000590{
591 /*
592 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
593 * Byte 0:
594 * Bit7 : 0 => small item
595 * Bit6-3: 1000 (0x8) => I/O port descriptor
596 * Bit2-0: 111 (0x7) => 7 Bytes long
597 */
598 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100599 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000600 /* bit1-7 are ignored */
601 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
602 /* minimum base address the device may be configured for */
603 acpigen_emit_byte(min & 0xff);
604 acpigen_emit_byte((min >> 8) & 0xff);
605 /* maximum base address the device may be configured for */
606 acpigen_emit_byte(max & 0xff);
607 acpigen_emit_byte((max >> 8) & 0xff);
608 /* alignment for min base */
609 acpigen_emit_byte(align & 0xff);
610 acpigen_emit_byte(len & 0xff);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000611}
612
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100613void acpigen_write_resourcetemplate_header(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000614{
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000615 /*
616 * A ResourceTemplate() is a Buffer() with a
617 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100618 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000619 * (small item 0xf, len 1)
620 */
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100621 acpigen_emit_byte(0x11); /* Buffer opcode */
622 acpigen_write_len_f();
623 acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000624 len_stack[ltop++] = acpigen_get_current();
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100625 acpigen_emit_byte(0x00);
626 acpigen_emit_byte(0x00);
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000627}
628
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100629void acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000630{
631 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100632 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000633 /*
634 * end tag (acpi 4.0 Section 6.4.2.8)
635 * 0x79 <checksum>
636 * 0x00 is treated as a good checksum according to the spec
637 * and is what iasl generates.
638 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100639 acpigen_emit_byte(0x79);
640 acpigen_emit_byte(0x00);
641
642 len = gencurrent - p;
643
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000644 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100645 p[0] = len & 0xff;
646 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000647 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100648 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000649}
650
651static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
652 struct resource *res)
653{
654 acpigen_write_mem32fixed(0, res->base, res->size);
655}
656
657static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
658 struct resource *res)
659{
660 resource_t base = res->base;
661 resource_t size = res->size;
662 while (size > 0) {
663 resource_t sz = size > 255 ? 255 : size;
664 acpigen_write_io16(base, base, 0, sz, 1);
665 size -= sz;
666 base += sz;
667 }
668}
669
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100670void acpigen_write_mainboard_resource_template(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000671{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100672 acpigen_write_resourcetemplate_header();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000673
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100674 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000675 search_global_resources(
676 IORESOURCE_MEM | IORESOURCE_RESERVE,
677 IORESOURCE_MEM | IORESOURCE_RESERVE,
678 acpigen_add_mainboard_rsvd_mem32, 0);
679
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100680 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000681 search_global_resources(
682 IORESOURCE_IO | IORESOURCE_RESERVE,
683 IORESOURCE_IO | IORESOURCE_RESERVE,
684 acpigen_add_mainboard_rsvd_io, 0);
685
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100686 acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000687}
688
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100689void acpigen_write_mainboard_resources(const char *scope, const char *name)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000690{
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100691 acpigen_write_scope(scope);
692 acpigen_write_name(name);
693 acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100694 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000695}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100696
697static int hex2bin(const char c)
698{
699 if (c >= 'A' && c <= 'F')
700 return c - 'A' + 10;
701 if (c >= 'a' && c <= 'f')
702 return c - 'a' + 10;
703 return c - '0';
704}
705
Vladimir Serbinenko9bb5c5c2014-11-09 03:51:32 +0100706void acpigen_emit_eisaid(const char *eisaid)
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100707{
708 u32 compact = 0;
709
710 /* Clamping individual values would be better but
711 there is a disagreement over what is a valid
712 EISA id, so accept anything and don't clamp,
713 parent code should create a valid EISAid.
714 */
715 compact |= (eisaid[0] - 'A' + 1) << 26;
716 compact |= (eisaid[1] - 'A' + 1) << 21;
717 compact |= (eisaid[2] - 'A' + 1) << 16;
718 compact |= hex2bin(eisaid[3]) << 12;
719 compact |= hex2bin(eisaid[4]) << 8;
720 compact |= hex2bin(eisaid[5]) << 4;
721 compact |= hex2bin(eisaid[6]);
722
723 acpigen_emit_byte(0xc);
724 acpigen_emit_byte((compact >> 24) & 0xff);
725 acpigen_emit_byte((compact >> 16) & 0xff);
726 acpigen_emit_byte((compact >> 8) & 0xff);
727 acpigen_emit_byte(compact & 0xff);
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100728}