blob: 222a2db284828f3d1c62e40b9f3bf86e895b0c3f [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
Paul Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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
25 * acpigen_patch_len
26 */
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
Stefan Reinauer5b73d4d2012-04-27 21:55:05 +020040int 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);
46 return 2;
47}
48
49void acpigen_patch_len(int len)
50{
51 ASSERT(len <= ACPIGEN_MAXLEN)
Paul Menzel0f4c0e22013-02-22 12:33:08 +010052 ASSERT(ltop > 0)
Rudolf Marek293b5f52009-02-01 18:35:15 +000053 char *p = len_stack[--ltop];
54 /* generate store length for 0xfff max */
55 p[0] = (0x40 | (len & 0xf));
56 p[1] = (len >> 4 & 0xff);
57
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
70int acpigen_emit_byte(unsigned char b)
71{
72 (*gencurrent++) = b;
73 return 1;
74}
75
76int acpigen_write_package(int nr_el)
77{
78 int len;
79 /* package op */
80 acpigen_emit_byte(0x12);
81 len = acpigen_write_len_f();
82 acpigen_emit_byte(nr_el);
83 return len + 2;
84}
85
86int acpigen_write_byte(unsigned int data)
87{
88 /* byte op */
89 acpigen_emit_byte(0xa);
90 acpigen_emit_byte(data & 0xff);
91 return 2;
92}
93
94int acpigen_write_dword(unsigned int data)
95{
96 /* dword op */
97 acpigen_emit_byte(0xc);
98 acpigen_emit_byte(data & 0xff);
99 acpigen_emit_byte((data >> 8) & 0xff);
100 acpigen_emit_byte((data >> 16) & 0xff);
101 acpigen_emit_byte((data >> 24) & 0xff);
102 return 5;
103}
104
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000105int acpigen_write_qword(uint64_t data)
106{
107 /* qword op */
108 acpigen_emit_byte(0xe);
109 acpigen_emit_byte(data & 0xff);
110 acpigen_emit_byte((data >> 8) & 0xff);
111 acpigen_emit_byte((data >> 16) & 0xff);
112 acpigen_emit_byte((data >> 24) & 0xff);
113 acpigen_emit_byte((data >> 32) & 0xff);
114 acpigen_emit_byte((data >> 40) & 0xff);
115 acpigen_emit_byte((data >> 48) & 0xff);
116 acpigen_emit_byte((data >> 56) & 0xff);
117 return 9;
118}
119
Myles Watson3fe6b702009-10-09 20:13:43 +0000120int acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000121{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000122 int len;
123 len = acpigen_write_name(name);
124 len += acpigen_write_byte(val);
125 return len;
126}
127
Myles Watson3fe6b702009-10-09 20:13:43 +0000128int acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000129{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000130 int len;
131 len = acpigen_write_name(name);
132 len += acpigen_write_dword(val);
133 return len;
134}
135
Myles Watson3fe6b702009-10-09 20:13:43 +0000136int acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000137{
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000138 int len;
139 len = acpigen_write_name(name);
140 len += acpigen_write_qword(val);
141 return len;
142}
143
Myles Watson3fe6b702009-10-09 20:13:43 +0000144int acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000145{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000146 int i;
147 for (i = 0; i < size; i++) {
148 acpigen_emit_byte(data[i]);
149 }
150 return size;
151}
152
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100153/*
154 * The naming conventions for ACPI namespace names are a bit tricky as
155 * each element has to be 4 chars wide (»All names are a fixed 32 bits.«)
156 * and »By convention, when an ASL compiler pads a name shorter than 4
157 * characters, it is done so with trailing underscores (‘_’).«.
158 *
159 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
160 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000161
Myles Watson3fe6b702009-10-09 20:13:43 +0000162static int acpigen_emit_simple_namestring(const char *name) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000163 int i, len = 0;
164 char ud[] = "____";
165 for (i = 0; i < 4; i++) {
166 if ((name[i] == '\0') || (name[i] == '.')) {
167 len += acpigen_emit_stream(ud, 4 - i);
168 break;
169 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000170 len += acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000171 }
172 }
173 return len;
174}
175
Myles Watson3fe6b702009-10-09 20:13:43 +0000176static int acpigen_emit_double_namestring(const char *name, int dotpos) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000177 int len = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000178 /* mark dual name prefix */
179 len += acpigen_emit_byte(0x2e);
180 len += acpigen_emit_simple_namestring(name);
181 len += acpigen_emit_simple_namestring(&name[dotpos + 1]);
182 return len;
183}
184
Myles Watson3fe6b702009-10-09 20:13:43 +0000185static int acpigen_emit_multi_namestring(const char *name) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000186 int len = 0, count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000187 unsigned char *pathlen;
Rudolf Marek3310d752009-06-21 20:26:13 +0000188 /* mark multi name prefix */
189 len += acpigen_emit_byte(0x2f);
190 len += acpigen_emit_byte(0x0);
191 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
192
193 while (name[0] != '\0') {
194 len += acpigen_emit_simple_namestring(name);
195 /* find end or next entity */
196 while ((name[0] != '.') && (name[0] != '\0'))
197 name++;
198 /* forward to next */
199 if (name[0] == '.')
200 name++;
201 count++;
202 }
203
204 pathlen[0] = count;
205 return len;
206}
207
208
Myles Watson3fe6b702009-10-09 20:13:43 +0000209int acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000210 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000211 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000212 int len = 0;
213
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600214 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000215 if (namepath[0] == '\\') {
216 len += acpigen_emit_byte('\\');
217 namepath++;
218 }
219
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600220 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000221 while (namepath[0] == '^') {
222 len += acpigen_emit_byte('^');
223 namepath++;
224 }
225
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200226 /* If we have only \\ or only ^...^. Then we need to put a null
227 name (0x00). */
228 if(namepath[0] == '\0') {
229 len += acpigen_emit_byte(0x00);
230 return len;
231 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000232
233 i = 0;
234 while (namepath[i] != '\0') {
235 if (namepath[i] == '.') {
236 dotcount++;
237 dotpos = i;
238 }
239 i++;
240 }
241
242 if (dotcount == 0) {
243 len += acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000244 } else if (dotcount == 1) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000245 len += acpigen_emit_double_namestring(namepath, dotpos);
246 } else {
247 len += acpigen_emit_multi_namestring(namepath);
248 }
249 return len;
250}
251
Myles Watson3fe6b702009-10-09 20:13:43 +0000252int acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000253{
Rudolf Marek3310d752009-06-21 20:26:13 +0000254 int len;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000255 /* name op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000256 len = acpigen_emit_byte(0x8);
257 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000258}
259
Myles Watson3fe6b702009-10-09 20:13:43 +0000260int acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000261{
262 int len;
263 /* scope op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000264 len = acpigen_emit_byte(0x10);
265 len += acpigen_write_len_f();
266 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000267}
Rudolf Marekf997b552009-02-14 15:40:23 +0000268
269int acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
270{
271/*
272 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
273 {
274*/
275 char pscope[16];
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000276 int len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000277 /* processor op */
278 acpigen_emit_byte(0x5b);
279 acpigen_emit_byte(0x83);
280 len = acpigen_write_len_f();
281
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100282 snprintf(pscope, sizeof (pscope),
283 "\\_PR.CPU%x", (unsigned int) cpuindex);
Rudolf Marek3310d752009-06-21 20:26:13 +0000284 len += acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000285 acpigen_emit_byte(cpuindex);
286 acpigen_emit_byte(pblock_addr & 0xff);
287 acpigen_emit_byte((pblock_addr >> 8) & 0xff);
288 acpigen_emit_byte((pblock_addr >> 16) & 0xff);
289 acpigen_emit_byte((pblock_addr >> 24) & 0xff);
290 acpigen_emit_byte(pblock_len);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000291 return 6 + 2 + len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000292}
293
294int acpigen_write_empty_PCT(void)
295{
296/*
297 Name (_PCT, Package (0x02)
298 {
299 ResourceTemplate ()
300 {
301 Register (FFixedHW,
302 0x00, // Bit Width
303 0x00, // Bit Offset
304 0x0000000000000000, // Address
305 ,)
306 },
307
308 ResourceTemplate ()
309 {
310 Register (FFixedHW,
311 0x00, // Bit Width
312 0x00, // Bit Offset
313 0x0000000000000000, // Address
314 ,)
315 }
316 })
317*/
318 static char stream[] = {
319 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
320 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
321 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
322 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
323 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
324 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
325 0x00, 0x79, 0x00
326 };
327 return acpigen_emit_stream(stream, ARRAY_SIZE(stream));
328}
329
Stefan Reinauer39205c62012-04-27 21:49:28 +0200330int acpigen_write_empty_PTC(void)
331{
332/*
333 Name (_PTC, Package (0x02)
334 {
335 ResourceTemplate ()
336 {
337 Register (FFixedHW,
338 0x00, // Bit Width
339 0x00, // Bit Offset
340 0x0000000000000000, // Address
341 ,)
342 },
343
344 ResourceTemplate ()
345 {
346 Register (FFixedHW,
347 0x00, // Bit Width
348 0x00, // Bit Offset
349 0x0000000000000000, // Address
350 ,)
351 }
352 })
353*/
354 int len, nlen, rlen;
355 acpi_addr_t addr = {
356 .space_id = ACPI_ADDRESS_SPACE_FIXED,
357 .bit_width = 0,
358 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800359 {
360 .resv = 0
361 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200362 .addrl = 0,
363 .addrh = 0,
364 };
365
366 nlen = acpigen_write_name("_PTC");
367 len = acpigen_write_package(2);
368
369 /* ControlRegister */
370 rlen = acpigen_write_resourcetemplate_header();
371 rlen += acpigen_write_register(&addr);
372 len += acpigen_write_resourcetemplate_footer(rlen);
373 len += rlen;
374
375 /* StatusRegister */
376 rlen = acpigen_write_resourcetemplate_header();
377 rlen += acpigen_write_register(&addr);
378 len += acpigen_write_resourcetemplate_footer(rlen);
379 len += rlen;
380
381 acpigen_patch_len(len - 1);
382 return len + nlen;
383}
384
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100385/*
386 * Generates a func with max supported P-states.
387 */
Rudolf Marekf997b552009-02-14 15:40:23 +0000388int acpigen_write_PPC(u8 nr)
389{
390/*
391 Method (_PPC, 0, NotSerialized)
392 {
393 Return (nr)
394 }
395*/
396 int len;
397 /* method op */
398 acpigen_emit_byte(0x14);
399 len = acpigen_write_len_f();
Rudolf Marek3310d752009-06-21 20:26:13 +0000400 len += acpigen_emit_namestring("_PPC");
Rudolf Marekf997b552009-02-14 15:40:23 +0000401 /* no fnarg */
402 acpigen_emit_byte(0x00);
403 /* return */
404 acpigen_emit_byte(0xa4);
405 /* arg */
406 len += acpigen_write_byte(nr);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000407 /* add all single bytes */
408 len += 3;
Rudolf Marekf997b552009-02-14 15:40:23 +0000409 acpigen_patch_len(len - 1);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000410 return len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000411}
412
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100413/*
414 * Generates a func with max supported P-states saved
415 * in the variable PPCM.
416 */
Duncan Laurie0eefa002012-07-16 12:11:53 -0700417int acpigen_write_PPC_NVS(void)
418{
419/*
420 Method (_PPC, 0, NotSerialized)
421 {
422 Return (PPCM)
423 }
424*/
425 int len;
426 /* method op */
427 acpigen_emit_byte(0x14);
428 len = acpigen_write_len_f();
429 len += acpigen_emit_namestring("_PPC");
430 /* no fnarg */
431 acpigen_emit_byte(0x00);
432 /* return */
433 acpigen_emit_byte(0xa4);
434 /* arg */
435 len += acpigen_emit_namestring("PPCM");
436 /* add all single bytes */
437 len += 3;
438 acpigen_patch_len(len - 1);
439 return len;
440}
441
Stefan Reinauer39205c62012-04-27 21:49:28 +0200442int acpigen_write_TPC(const char *gnvs_tpc_limit)
443{
444/*
445 // Sample _TPC method
446 Method (_TPC, 0, NotSerialized)
447 {
448 Return (\TLVL)
449 }
450 */
451 int len;
452
453 len = acpigen_emit_byte(0x14); /* MethodOp */
454 len += acpigen_write_len_f(); /* PkgLength */
455 len += acpigen_emit_namestring("_TPC");
456 len += acpigen_emit_byte(0x00); /* No Arguments */
457 len += acpigen_emit_byte(0xa4); /* ReturnOp */
458 len += acpigen_emit_namestring(gnvs_tpc_limit);
459 acpigen_patch_len(len - 1);
460 return len;
461}
462
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000463int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
464 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000465{
466 int len;
467 len = acpigen_write_package(6);
468 len += acpigen_write_dword(coreFreq);
469 len += acpigen_write_dword(power);
470 len += acpigen_write_dword(transLat);
471 len += acpigen_write_dword(busmLat);
472 len += acpigen_write_dword(control);
473 len += acpigen_write_dword(status);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700474 // pkglen without the len opcode
Rudolf Marekf997b552009-02-14 15:40:23 +0000475 acpigen_patch_len(len - 1);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700476
477 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
478 coreFreq, power, control, status);
479
Rudolf Marekf997b552009-02-14 15:40:23 +0000480 return len;
481}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000482
483int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
484{
485 int len, lenh, lenp;
486 lenh = acpigen_write_name("_PSD");
487 lenp = acpigen_write_package(1);
488 len = acpigen_write_package(5);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000489 len += acpigen_write_byte(5); // 5 values
490 len += acpigen_write_byte(0); // revision 0
Patrick Georgidf444bf2009-04-21 20:34:36 +0000491 len += acpigen_write_dword(domain);
492 len += acpigen_write_dword(coordtype);
493 len += acpigen_write_dword(numprocs);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000494 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000495 len += lenp;
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000496 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000497 return len + lenh;
498}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000499
Stefan Reinauerf69b4682012-04-27 23:12:08 +0200500int acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200501{
502 int len, len0;
503 char *start, *end;
504
505 len0 = acpigen_write_package(4);
506 len = acpigen_write_resourcetemplate_header();
507 start = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200508 acpigen_write_register(&cstate->resource);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200509 end = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200510 len += end - start;
Sven Schnelle0b86c762011-10-21 21:46:47 +0200511 len += acpigen_write_resourcetemplate_footer(len);
512 len += len0;
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200513 len += acpigen_write_dword(cstate->ctype);
514 len += acpigen_write_dword(cstate->latency);
515 len += acpigen_write_dword(cstate->power);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200516 acpigen_patch_len(len - 1);
517 return len;
518}
519
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200520int acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200521{
522 int len, lenh, lenp, i;
523 lenh = acpigen_write_name("_CST");
524 lenp = acpigen_write_package(nentries+1);
525 len = acpigen_write_dword(nentries);
526
527 for (i = 0; i < nentries; i++)
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200528 len += acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200529
530 len += lenp;
531 acpigen_patch_len(len - 1);
532 return len + lenh;
533}
534
Stefan Reinauer39205c62012-04-27 21:49:28 +0200535int acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
536{
537/*
538 Sample _TSS package with 100% and 50% duty cycles
539 Name (_TSS, Package (0x02)
540 {
541 Package(){100, 1000, 0, 0x00, 0)
542 Package(){50, 520, 0, 0x18, 0)
543 })
544 */
545 int i, len, plen, nlen;
546 acpi_tstate_t *tstate = tstate_list;
547
548 nlen = acpigen_write_name("_TSS");
549 plen = acpigen_write_package(entries);
550
551 for (i = 0; i < entries; i++) {
552 len = acpigen_write_package(5);
553 len += acpigen_write_dword(tstate->percent);
554 len += acpigen_write_dword(tstate->power);
555 len += acpigen_write_dword(tstate->latency);
556 len += acpigen_write_dword(tstate->control);
557 len += acpigen_write_dword(tstate->status);
558 acpigen_patch_len(len - 1);
559 tstate++;
560 plen += len;
561 }
562
563 acpigen_patch_len(plen - 1);
564 return plen + nlen;
565}
566
567int acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
568{
569 int len, lenh, lenp;
570 lenh = acpigen_write_name("_TSD");
571 lenp = acpigen_write_package(1);
572 len = acpigen_write_package(5);
573 len += acpigen_write_byte(5); // 5 values
574 len += acpigen_write_byte(0); // revision 0
575 len += acpigen_write_dword(domain);
576 len += acpigen_write_dword(coordtype);
577 len += acpigen_write_dword(numprocs);
578 acpigen_patch_len(len - 1);
579 len += lenp;
580 acpigen_patch_len(len - 1);
581 return len + lenh;
582}
583
584
585
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000586int acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
587{
588 /*
589 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
590 * Byte 0:
591 * Bit7 : 1 => big item
592 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
593 */
594 acpigen_emit_byte(0x86);
595 /* Byte 1+2: length (0x0009) */
596 acpigen_emit_byte(0x09);
597 acpigen_emit_byte(0x00);
598 /* bit1-7 are ignored */
599 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
600 acpigen_emit_byte(base & 0xff);
601 acpigen_emit_byte((base >> 8) & 0xff);
602 acpigen_emit_byte((base >> 16) & 0xff);
603 acpigen_emit_byte((base >> 24) & 0xff);
604 acpigen_emit_byte(size & 0xff);
605 acpigen_emit_byte((size >> 8) & 0xff);
606 acpigen_emit_byte((size >> 16) & 0xff);
607 acpigen_emit_byte((size >> 24) & 0xff);
608 return 12;
609}
610
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200611int acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200612{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200613 acpigen_emit_byte(0x82); /* Register Descriptor */
614 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
615 acpigen_emit_byte(0x00); /* Register Length 15:8 */
616 acpigen_emit_byte(addr->space_id); /* Address Space ID */
617 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
618 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
619 acpigen_emit_byte(addr->resv); /* Register Access Size */
620 acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */
621 acpigen_emit_byte((addr->addrl >> 8) & 0xff);
622 acpigen_emit_byte((addr->addrl >> 16) & 0xff);
623 acpigen_emit_byte((addr->addrl >> 24) & 0xff);
624 acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */
625 acpigen_emit_byte((addr->addrh >> 8) & 0xff);
626 acpigen_emit_byte((addr->addrh >> 16) & 0xff);
627 acpigen_emit_byte((addr->addrh >> 24) & 0xff);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200628 return 15;
629}
630
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100631int acpigen_write_irq(u16 mask)
632{
633 /*
634 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
635 * Byte 0:
636 * Bit7 : 0 => small item
637 * Bit6-3: 0100 (0x4) => IRQ port descriptor
638 * Bit2-0: 010 (0x2) => 2 Bytes long
639 */
640 acpigen_emit_byte(0x22);
641 acpigen_emit_byte(mask & 0xff);
642 acpigen_emit_byte((mask >> 8) & 0xff);
643 return 3;
644}
645
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000646int acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
647{
648 /*
649 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
650 * Byte 0:
651 * Bit7 : 0 => small item
652 * Bit6-3: 1000 (0x8) => I/O port descriptor
653 * Bit2-0: 111 (0x7) => 7 Bytes long
654 */
655 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100656 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000657 /* bit1-7 are ignored */
658 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
659 /* minimum base address the device may be configured for */
660 acpigen_emit_byte(min & 0xff);
661 acpigen_emit_byte((min >> 8) & 0xff);
662 /* maximum base address the device may be configured for */
663 acpigen_emit_byte(max & 0xff);
664 acpigen_emit_byte((max >> 8) & 0xff);
665 /* alignment for min base */
666 acpigen_emit_byte(align & 0xff);
667 acpigen_emit_byte(len & 0xff);
668 return 8;
669}
670
671int acpigen_write_resourcetemplate_header(void)
672{
673 int len;
674 /*
675 * A ResourceTemplate() is a Buffer() with a
676 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100677 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000678 * (small item 0xf, len 1)
679 */
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100680 len = acpigen_emit_byte(0x11); /* Buffer opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000681 len += acpigen_write_len_f();
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100682 len += acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000683 len_stack[ltop++] = acpigen_get_current();
684 len += acpigen_emit_byte(0x00);
685 len += acpigen_emit_byte(0x00);
686 return len;
687}
688
689int acpigen_write_resourcetemplate_footer(int len)
690{
691 char *p = len_stack[--ltop];
692 /*
693 * end tag (acpi 4.0 Section 6.4.2.8)
694 * 0x79 <checksum>
695 * 0x00 is treated as a good checksum according to the spec
696 * and is what iasl generates.
697 */
698 len += acpigen_emit_byte(0x79);
699 len += acpigen_emit_byte(0x00);
700 /* patch len word */
701 p[0] = (len-6) & 0xff;
702 p[1] = ((len-6) >> 8) & 0xff;
703 /* patch len field */
704 acpigen_patch_len(len-1);
705 return 2;
706}
707
708static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
709 struct resource *res)
710{
711 acpigen_write_mem32fixed(0, res->base, res->size);
712}
713
714static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
715 struct resource *res)
716{
717 resource_t base = res->base;
718 resource_t size = res->size;
719 while (size > 0) {
720 resource_t sz = size > 255 ? 255 : size;
721 acpigen_write_io16(base, base, 0, sz, 1);
722 size -= sz;
723 base += sz;
724 }
725}
726
727int acpigen_write_mainboard_resource_template(void)
728{
729 int len;
730 char *start;
731 char *end;
732 len = acpigen_write_resourcetemplate_header();
733 start = acpigen_get_current();
734
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100735 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000736 search_global_resources(
737 IORESOURCE_MEM | IORESOURCE_RESERVE,
738 IORESOURCE_MEM | IORESOURCE_RESERVE,
739 acpigen_add_mainboard_rsvd_mem32, 0);
740
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100741 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000742 search_global_resources(
743 IORESOURCE_IO | IORESOURCE_RESERVE,
744 IORESOURCE_IO | IORESOURCE_RESERVE,
745 acpigen_add_mainboard_rsvd_io, 0);
746
747 end = acpigen_get_current();
748 len += end-start;
749 len += acpigen_write_resourcetemplate_footer(len);
750 return len;
751}
752
753int acpigen_write_mainboard_resources(const char *scope, const char *name)
754{
755 int len;
756 len = acpigen_write_scope(scope);
757 len += acpigen_write_name(name);
758 len += acpigen_write_mainboard_resource_template();
759 acpigen_patch_len(len - 1);
760 return len;
761}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100762
763static int hex2bin(const char c)
764{
765 if (c >= 'A' && c <= 'F')
766 return c - 'A' + 10;
767 if (c >= 'a' && c <= 'f')
768 return c - 'a' + 10;
769 return c - '0';
770}
771
772int acpigen_emit_eisaid(const char *eisaid)
773{
774 u32 compact = 0;
775
776 /* Clamping individual values would be better but
777 there is a disagreement over what is a valid
778 EISA id, so accept anything and don't clamp,
779 parent code should create a valid EISAid.
780 */
781 compact |= (eisaid[0] - 'A' + 1) << 26;
782 compact |= (eisaid[1] - 'A' + 1) << 21;
783 compact |= (eisaid[2] - 'A' + 1) << 16;
784 compact |= hex2bin(eisaid[3]) << 12;
785 compact |= hex2bin(eisaid[4]) << 8;
786 compact |= hex2bin(eisaid[5]) << 4;
787 compact |= hex2bin(eisaid[6]);
788
789 acpigen_emit_byte(0xc);
790 acpigen_emit_byte((compact >> 24) & 0xff);
791 acpigen_emit_byte((compact >> 16) & 0xff);
792 acpigen_emit_byte((compact >> 8) & 0xff);
793 acpigen_emit_byte(compact & 0xff);
794 return 5;
795}