blob: 0689273b0af609efbb43a7cac819ac4a7c6ce6f0 [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
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010060void acpigen_pop_len(void)
61{
62 int len;
63 ASSERT(ltop > 0)
64 char *p = len_stack[--ltop];
65 len = gencurrent - p;
66 ASSERT(len <= ACPIGEN_MAXLEN)
67 /* generate store length for 0xfff max */
68 p[0] = (0x40 | (len & 0xf));
69 p[1] = (len >> 4 & 0xff);
70
71}
72
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000073void acpigen_set_current(char *curr)
74{
75 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000076}
77
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000078char *acpigen_get_current(void)
79{
80 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000081}
82
83int acpigen_emit_byte(unsigned char b)
84{
85 (*gencurrent++) = b;
86 return 1;
87}
88
89int acpigen_write_package(int nr_el)
90{
91 int len;
92 /* package op */
93 acpigen_emit_byte(0x12);
94 len = acpigen_write_len_f();
95 acpigen_emit_byte(nr_el);
96 return len + 2;
97}
98
99int acpigen_write_byte(unsigned int data)
100{
101 /* byte op */
102 acpigen_emit_byte(0xa);
103 acpigen_emit_byte(data & 0xff);
104 return 2;
105}
106
107int acpigen_write_dword(unsigned int data)
108{
109 /* dword op */
110 acpigen_emit_byte(0xc);
111 acpigen_emit_byte(data & 0xff);
112 acpigen_emit_byte((data >> 8) & 0xff);
113 acpigen_emit_byte((data >> 16) & 0xff);
114 acpigen_emit_byte((data >> 24) & 0xff);
115 return 5;
116}
117
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000118int acpigen_write_qword(uint64_t data)
119{
120 /* qword op */
121 acpigen_emit_byte(0xe);
122 acpigen_emit_byte(data & 0xff);
123 acpigen_emit_byte((data >> 8) & 0xff);
124 acpigen_emit_byte((data >> 16) & 0xff);
125 acpigen_emit_byte((data >> 24) & 0xff);
126 acpigen_emit_byte((data >> 32) & 0xff);
127 acpigen_emit_byte((data >> 40) & 0xff);
128 acpigen_emit_byte((data >> 48) & 0xff);
129 acpigen_emit_byte((data >> 56) & 0xff);
130 return 9;
131}
132
Myles Watson3fe6b702009-10-09 20:13:43 +0000133int acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000134{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000135 int len;
136 len = acpigen_write_name(name);
137 len += acpigen_write_byte(val);
138 return len;
139}
140
Myles Watson3fe6b702009-10-09 20:13:43 +0000141int acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000142{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000143 int len;
144 len = acpigen_write_name(name);
145 len += acpigen_write_dword(val);
146 return len;
147}
148
Myles Watson3fe6b702009-10-09 20:13:43 +0000149int acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000150{
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000151 int len;
152 len = acpigen_write_name(name);
153 len += acpigen_write_qword(val);
154 return len;
155}
156
Myles Watson3fe6b702009-10-09 20:13:43 +0000157int acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000158{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000159 int i;
160 for (i = 0; i < size; i++) {
161 acpigen_emit_byte(data[i]);
162 }
163 return size;
164}
165
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100166/*
167 * The naming conventions for ACPI namespace names are a bit tricky as
168 * each element has to be 4 chars wide (»All names are a fixed 32 bits.«)
169 * and »By convention, when an ASL compiler pads a name shorter than 4
170 * characters, it is done so with trailing underscores (‘_’).«.
171 *
172 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
173 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000174
Myles Watson3fe6b702009-10-09 20:13:43 +0000175static int acpigen_emit_simple_namestring(const char *name) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000176 int i, len = 0;
177 char ud[] = "____";
178 for (i = 0; i < 4; i++) {
179 if ((name[i] == '\0') || (name[i] == '.')) {
180 len += acpigen_emit_stream(ud, 4 - i);
181 break;
182 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000183 len += acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000184 }
185 }
186 return len;
187}
188
Myles Watson3fe6b702009-10-09 20:13:43 +0000189static int acpigen_emit_double_namestring(const char *name, int dotpos) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000190 int len = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000191 /* mark dual name prefix */
192 len += acpigen_emit_byte(0x2e);
193 len += acpigen_emit_simple_namestring(name);
194 len += acpigen_emit_simple_namestring(&name[dotpos + 1]);
195 return len;
196}
197
Myles Watson3fe6b702009-10-09 20:13:43 +0000198static int acpigen_emit_multi_namestring(const char *name) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000199 int len = 0, count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000200 unsigned char *pathlen;
Rudolf Marek3310d752009-06-21 20:26:13 +0000201 /* mark multi name prefix */
202 len += acpigen_emit_byte(0x2f);
203 len += acpigen_emit_byte(0x0);
204 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
205
206 while (name[0] != '\0') {
207 len += acpigen_emit_simple_namestring(name);
208 /* find end or next entity */
209 while ((name[0] != '.') && (name[0] != '\0'))
210 name++;
211 /* forward to next */
212 if (name[0] == '.')
213 name++;
214 count++;
215 }
216
217 pathlen[0] = count;
218 return len;
219}
220
221
Myles Watson3fe6b702009-10-09 20:13:43 +0000222int acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000223 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000224 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000225 int len = 0;
226
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600227 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000228 if (namepath[0] == '\\') {
229 len += acpigen_emit_byte('\\');
230 namepath++;
231 }
232
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600233 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000234 while (namepath[0] == '^') {
235 len += acpigen_emit_byte('^');
236 namepath++;
237 }
238
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200239 /* If we have only \\ or only ^...^. Then we need to put a null
240 name (0x00). */
241 if(namepath[0] == '\0') {
242 len += acpigen_emit_byte(0x00);
243 return len;
244 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000245
246 i = 0;
247 while (namepath[i] != '\0') {
248 if (namepath[i] == '.') {
249 dotcount++;
250 dotpos = i;
251 }
252 i++;
253 }
254
255 if (dotcount == 0) {
256 len += acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000257 } else if (dotcount == 1) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000258 len += acpigen_emit_double_namestring(namepath, dotpos);
259 } else {
260 len += acpigen_emit_multi_namestring(namepath);
261 }
262 return len;
263}
264
Myles Watson3fe6b702009-10-09 20:13:43 +0000265int acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000266{
Rudolf Marek3310d752009-06-21 20:26:13 +0000267 int len;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000268 /* name op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000269 len = acpigen_emit_byte(0x8);
270 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000271}
272
Myles Watson3fe6b702009-10-09 20:13:43 +0000273int acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000274{
275 int len;
276 /* scope op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000277 len = acpigen_emit_byte(0x10);
278 len += acpigen_write_len_f();
279 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000280}
Rudolf Marekf997b552009-02-14 15:40:23 +0000281
282int acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
283{
284/*
285 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
286 {
287*/
288 char pscope[16];
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000289 int len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000290 /* processor op */
291 acpigen_emit_byte(0x5b);
292 acpigen_emit_byte(0x83);
293 len = acpigen_write_len_f();
294
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100295 snprintf(pscope, sizeof (pscope),
296 "\\_PR.CPU%x", (unsigned int) cpuindex);
Rudolf Marek3310d752009-06-21 20:26:13 +0000297 len += acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000298 acpigen_emit_byte(cpuindex);
299 acpigen_emit_byte(pblock_addr & 0xff);
300 acpigen_emit_byte((pblock_addr >> 8) & 0xff);
301 acpigen_emit_byte((pblock_addr >> 16) & 0xff);
302 acpigen_emit_byte((pblock_addr >> 24) & 0xff);
303 acpigen_emit_byte(pblock_len);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000304 return 6 + 2 + len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000305}
306
307int acpigen_write_empty_PCT(void)
308{
309/*
310 Name (_PCT, Package (0x02)
311 {
312 ResourceTemplate ()
313 {
314 Register (FFixedHW,
315 0x00, // Bit Width
316 0x00, // Bit Offset
317 0x0000000000000000, // Address
318 ,)
319 },
320
321 ResourceTemplate ()
322 {
323 Register (FFixedHW,
324 0x00, // Bit Width
325 0x00, // Bit Offset
326 0x0000000000000000, // Address
327 ,)
328 }
329 })
330*/
331 static char stream[] = {
332 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
333 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
334 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
335 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
336 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
337 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
338 0x00, 0x79, 0x00
339 };
340 return acpigen_emit_stream(stream, ARRAY_SIZE(stream));
341}
342
Stefan Reinauer39205c62012-04-27 21:49:28 +0200343int acpigen_write_empty_PTC(void)
344{
345/*
346 Name (_PTC, Package (0x02)
347 {
348 ResourceTemplate ()
349 {
350 Register (FFixedHW,
351 0x00, // Bit Width
352 0x00, // Bit Offset
353 0x0000000000000000, // Address
354 ,)
355 },
356
357 ResourceTemplate ()
358 {
359 Register (FFixedHW,
360 0x00, // Bit Width
361 0x00, // Bit Offset
362 0x0000000000000000, // Address
363 ,)
364 }
365 })
366*/
367 int len, nlen, rlen;
368 acpi_addr_t addr = {
369 .space_id = ACPI_ADDRESS_SPACE_FIXED,
370 .bit_width = 0,
371 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800372 {
373 .resv = 0
374 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200375 .addrl = 0,
376 .addrh = 0,
377 };
378
379 nlen = acpigen_write_name("_PTC");
380 len = acpigen_write_package(2);
381
382 /* ControlRegister */
383 rlen = acpigen_write_resourcetemplate_header();
384 rlen += acpigen_write_register(&addr);
385 len += acpigen_write_resourcetemplate_footer(rlen);
386 len += rlen;
387
388 /* StatusRegister */
389 rlen = acpigen_write_resourcetemplate_header();
390 rlen += acpigen_write_register(&addr);
391 len += acpigen_write_resourcetemplate_footer(rlen);
392 len += rlen;
393
394 acpigen_patch_len(len - 1);
395 return len + nlen;
396}
397
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100398/*
399 * Generates a func with max supported P-states.
400 */
Rudolf Marekf997b552009-02-14 15:40:23 +0000401int acpigen_write_PPC(u8 nr)
402{
403/*
404 Method (_PPC, 0, NotSerialized)
405 {
406 Return (nr)
407 }
408*/
409 int len;
410 /* method op */
411 acpigen_emit_byte(0x14);
412 len = acpigen_write_len_f();
Rudolf Marek3310d752009-06-21 20:26:13 +0000413 len += acpigen_emit_namestring("_PPC");
Rudolf Marekf997b552009-02-14 15:40:23 +0000414 /* no fnarg */
415 acpigen_emit_byte(0x00);
416 /* return */
417 acpigen_emit_byte(0xa4);
418 /* arg */
419 len += acpigen_write_byte(nr);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000420 /* add all single bytes */
421 len += 3;
Rudolf Marekf997b552009-02-14 15:40:23 +0000422 acpigen_patch_len(len - 1);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000423 return len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000424}
425
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100426/*
427 * Generates a func with max supported P-states saved
428 * in the variable PPCM.
429 */
Duncan Laurie0eefa002012-07-16 12:11:53 -0700430int acpigen_write_PPC_NVS(void)
431{
432/*
433 Method (_PPC, 0, NotSerialized)
434 {
435 Return (PPCM)
436 }
437*/
438 int len;
439 /* method op */
440 acpigen_emit_byte(0x14);
441 len = acpigen_write_len_f();
442 len += acpigen_emit_namestring("_PPC");
443 /* no fnarg */
444 acpigen_emit_byte(0x00);
445 /* return */
446 acpigen_emit_byte(0xa4);
447 /* arg */
448 len += acpigen_emit_namestring("PPCM");
449 /* add all single bytes */
450 len += 3;
451 acpigen_patch_len(len - 1);
452 return len;
453}
454
Stefan Reinauer39205c62012-04-27 21:49:28 +0200455int acpigen_write_TPC(const char *gnvs_tpc_limit)
456{
457/*
458 // Sample _TPC method
459 Method (_TPC, 0, NotSerialized)
460 {
461 Return (\TLVL)
462 }
463 */
464 int len;
465
466 len = acpigen_emit_byte(0x14); /* MethodOp */
467 len += acpigen_write_len_f(); /* PkgLength */
468 len += acpigen_emit_namestring("_TPC");
469 len += acpigen_emit_byte(0x00); /* No Arguments */
470 len += acpigen_emit_byte(0xa4); /* ReturnOp */
471 len += acpigen_emit_namestring(gnvs_tpc_limit);
472 acpigen_patch_len(len - 1);
473 return len;
474}
475
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000476int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
477 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000478{
479 int len;
480 len = acpigen_write_package(6);
481 len += acpigen_write_dword(coreFreq);
482 len += acpigen_write_dword(power);
483 len += acpigen_write_dword(transLat);
484 len += acpigen_write_dword(busmLat);
485 len += acpigen_write_dword(control);
486 len += acpigen_write_dword(status);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700487 // pkglen without the len opcode
Rudolf Marekf997b552009-02-14 15:40:23 +0000488 acpigen_patch_len(len - 1);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700489
490 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
491 coreFreq, power, control, status);
492
Rudolf Marekf997b552009-02-14 15:40:23 +0000493 return len;
494}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000495
496int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
497{
498 int len, lenh, lenp;
499 lenh = acpigen_write_name("_PSD");
500 lenp = acpigen_write_package(1);
501 len = acpigen_write_package(5);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000502 len += acpigen_write_byte(5); // 5 values
503 len += acpigen_write_byte(0); // revision 0
Patrick Georgidf444bf2009-04-21 20:34:36 +0000504 len += acpigen_write_dword(domain);
505 len += acpigen_write_dword(coordtype);
506 len += acpigen_write_dword(numprocs);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000507 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000508 len += lenp;
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000509 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000510 return len + lenh;
511}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000512
Stefan Reinauerf69b4682012-04-27 23:12:08 +0200513int acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200514{
515 int len, len0;
516 char *start, *end;
517
518 len0 = acpigen_write_package(4);
519 len = acpigen_write_resourcetemplate_header();
520 start = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200521 acpigen_write_register(&cstate->resource);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200522 end = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200523 len += end - start;
Sven Schnelle0b86c762011-10-21 21:46:47 +0200524 len += acpigen_write_resourcetemplate_footer(len);
525 len += len0;
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200526 len += acpigen_write_dword(cstate->ctype);
527 len += acpigen_write_dword(cstate->latency);
528 len += acpigen_write_dword(cstate->power);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200529 acpigen_patch_len(len - 1);
530 return len;
531}
532
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200533int acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200534{
535 int len, lenh, lenp, i;
536 lenh = acpigen_write_name("_CST");
537 lenp = acpigen_write_package(nentries+1);
538 len = acpigen_write_dword(nentries);
539
540 for (i = 0; i < nentries; i++)
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200541 len += acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200542
543 len += lenp;
544 acpigen_patch_len(len - 1);
545 return len + lenh;
546}
547
Stefan Reinauer39205c62012-04-27 21:49:28 +0200548int acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
549{
550/*
551 Sample _TSS package with 100% and 50% duty cycles
552 Name (_TSS, Package (0x02)
553 {
554 Package(){100, 1000, 0, 0x00, 0)
555 Package(){50, 520, 0, 0x18, 0)
556 })
557 */
558 int i, len, plen, nlen;
559 acpi_tstate_t *tstate = tstate_list;
560
561 nlen = acpigen_write_name("_TSS");
562 plen = acpigen_write_package(entries);
563
564 for (i = 0; i < entries; i++) {
565 len = acpigen_write_package(5);
566 len += acpigen_write_dword(tstate->percent);
567 len += acpigen_write_dword(tstate->power);
568 len += acpigen_write_dword(tstate->latency);
569 len += acpigen_write_dword(tstate->control);
570 len += acpigen_write_dword(tstate->status);
571 acpigen_patch_len(len - 1);
572 tstate++;
573 plen += len;
574 }
575
576 acpigen_patch_len(plen - 1);
577 return plen + nlen;
578}
579
580int acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
581{
582 int len, lenh, lenp;
583 lenh = acpigen_write_name("_TSD");
584 lenp = acpigen_write_package(1);
585 len = acpigen_write_package(5);
586 len += acpigen_write_byte(5); // 5 values
587 len += acpigen_write_byte(0); // revision 0
588 len += acpigen_write_dword(domain);
589 len += acpigen_write_dword(coordtype);
590 len += acpigen_write_dword(numprocs);
591 acpigen_patch_len(len - 1);
592 len += lenp;
593 acpigen_patch_len(len - 1);
594 return len + lenh;
595}
596
597
598
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000599int acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
600{
601 /*
602 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
603 * Byte 0:
604 * Bit7 : 1 => big item
605 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
606 */
607 acpigen_emit_byte(0x86);
608 /* Byte 1+2: length (0x0009) */
609 acpigen_emit_byte(0x09);
610 acpigen_emit_byte(0x00);
611 /* bit1-7 are ignored */
612 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
613 acpigen_emit_byte(base & 0xff);
614 acpigen_emit_byte((base >> 8) & 0xff);
615 acpigen_emit_byte((base >> 16) & 0xff);
616 acpigen_emit_byte((base >> 24) & 0xff);
617 acpigen_emit_byte(size & 0xff);
618 acpigen_emit_byte((size >> 8) & 0xff);
619 acpigen_emit_byte((size >> 16) & 0xff);
620 acpigen_emit_byte((size >> 24) & 0xff);
621 return 12;
622}
623
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200624int acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200625{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200626 acpigen_emit_byte(0x82); /* Register Descriptor */
627 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
628 acpigen_emit_byte(0x00); /* Register Length 15:8 */
629 acpigen_emit_byte(addr->space_id); /* Address Space ID */
630 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
631 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
632 acpigen_emit_byte(addr->resv); /* Register Access Size */
633 acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */
634 acpigen_emit_byte((addr->addrl >> 8) & 0xff);
635 acpigen_emit_byte((addr->addrl >> 16) & 0xff);
636 acpigen_emit_byte((addr->addrl >> 24) & 0xff);
637 acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */
638 acpigen_emit_byte((addr->addrh >> 8) & 0xff);
639 acpigen_emit_byte((addr->addrh >> 16) & 0xff);
640 acpigen_emit_byte((addr->addrh >> 24) & 0xff);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200641 return 15;
642}
643
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100644int acpigen_write_irq(u16 mask)
645{
646 /*
647 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
648 * Byte 0:
649 * Bit7 : 0 => small item
650 * Bit6-3: 0100 (0x4) => IRQ port descriptor
651 * Bit2-0: 010 (0x2) => 2 Bytes long
652 */
653 acpigen_emit_byte(0x22);
654 acpigen_emit_byte(mask & 0xff);
655 acpigen_emit_byte((mask >> 8) & 0xff);
656 return 3;
657}
658
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000659int acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
660{
661 /*
662 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
663 * Byte 0:
664 * Bit7 : 0 => small item
665 * Bit6-3: 1000 (0x8) => I/O port descriptor
666 * Bit2-0: 111 (0x7) => 7 Bytes long
667 */
668 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100669 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000670 /* bit1-7 are ignored */
671 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
672 /* minimum base address the device may be configured for */
673 acpigen_emit_byte(min & 0xff);
674 acpigen_emit_byte((min >> 8) & 0xff);
675 /* maximum base address the device may be configured for */
676 acpigen_emit_byte(max & 0xff);
677 acpigen_emit_byte((max >> 8) & 0xff);
678 /* alignment for min base */
679 acpigen_emit_byte(align & 0xff);
680 acpigen_emit_byte(len & 0xff);
681 return 8;
682}
683
684int acpigen_write_resourcetemplate_header(void)
685{
686 int len;
687 /*
688 * A ResourceTemplate() is a Buffer() with a
689 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100690 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000691 * (small item 0xf, len 1)
692 */
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100693 len = acpigen_emit_byte(0x11); /* Buffer opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000694 len += acpigen_write_len_f();
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100695 len += acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000696 len_stack[ltop++] = acpigen_get_current();
697 len += acpigen_emit_byte(0x00);
698 len += acpigen_emit_byte(0x00);
699 return len;
700}
701
702int acpigen_write_resourcetemplate_footer(int len)
703{
704 char *p = len_stack[--ltop];
705 /*
706 * end tag (acpi 4.0 Section 6.4.2.8)
707 * 0x79 <checksum>
708 * 0x00 is treated as a good checksum according to the spec
709 * and is what iasl generates.
710 */
711 len += acpigen_emit_byte(0x79);
712 len += acpigen_emit_byte(0x00);
713 /* patch len word */
714 p[0] = (len-6) & 0xff;
715 p[1] = ((len-6) >> 8) & 0xff;
716 /* patch len field */
717 acpigen_patch_len(len-1);
718 return 2;
719}
720
721static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
722 struct resource *res)
723{
724 acpigen_write_mem32fixed(0, res->base, res->size);
725}
726
727static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
728 struct resource *res)
729{
730 resource_t base = res->base;
731 resource_t size = res->size;
732 while (size > 0) {
733 resource_t sz = size > 255 ? 255 : size;
734 acpigen_write_io16(base, base, 0, sz, 1);
735 size -= sz;
736 base += sz;
737 }
738}
739
740int acpigen_write_mainboard_resource_template(void)
741{
742 int len;
743 char *start;
744 char *end;
745 len = acpigen_write_resourcetemplate_header();
746 start = acpigen_get_current();
747
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100748 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000749 search_global_resources(
750 IORESOURCE_MEM | IORESOURCE_RESERVE,
751 IORESOURCE_MEM | IORESOURCE_RESERVE,
752 acpigen_add_mainboard_rsvd_mem32, 0);
753
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100754 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000755 search_global_resources(
756 IORESOURCE_IO | IORESOURCE_RESERVE,
757 IORESOURCE_IO | IORESOURCE_RESERVE,
758 acpigen_add_mainboard_rsvd_io, 0);
759
760 end = acpigen_get_current();
761 len += end-start;
762 len += acpigen_write_resourcetemplate_footer(len);
763 return len;
764}
765
766int acpigen_write_mainboard_resources(const char *scope, const char *name)
767{
768 int len;
769 len = acpigen_write_scope(scope);
770 len += acpigen_write_name(name);
771 len += acpigen_write_mainboard_resource_template();
772 acpigen_patch_len(len - 1);
773 return len;
774}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100775
776static int hex2bin(const char c)
777{
778 if (c >= 'A' && c <= 'F')
779 return c - 'A' + 10;
780 if (c >= 'a' && c <= 'f')
781 return c - 'a' + 10;
782 return c - '0';
783}
784
785int acpigen_emit_eisaid(const char *eisaid)
786{
787 u32 compact = 0;
788
789 /* Clamping individual values would be better but
790 there is a disagreement over what is a valid
791 EISA id, so accept anything and don't clamp,
792 parent code should create a valid EISAid.
793 */
794 compact |= (eisaid[0] - 'A' + 1) << 26;
795 compact |= (eisaid[1] - 'A' + 1) << 21;
796 compact |= (eisaid[2] - 'A' + 1) << 16;
797 compact |= hex2bin(eisaid[3]) << 12;
798 compact |= hex2bin(eisaid[4]) << 8;
799 compact |= hex2bin(eisaid[5]) << 4;
800 compact |= hex2bin(eisaid[6]);
801
802 acpigen_emit_byte(0xc);
803 acpigen_emit_byte((compact >> 24) & 0xff);
804 acpigen_emit_byte((compact >> 16) & 0xff);
805 acpigen_emit_byte((compact >> 8) & 0xff);
806 acpigen_emit_byte(compact & 0xff);
807 return 5;
808}