blob: 8e7ce463c19e358c1b4fc46ad7023b8773211a78 [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);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100385 len += acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200386 len += rlen;
387
388 /* StatusRegister */
389 rlen = acpigen_write_resourcetemplate_header();
390 rlen += acpigen_write_register(&addr);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100391 len += acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200392 len += rlen;
393
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100394 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200395 return len + nlen;
396}
397
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100398int acpigen_write_method(const char *name, int nargs)
399{
400 int len;
401
402 /* method op */
403 len = acpigen_emit_byte(0x14);
404 len += acpigen_write_len_f();
405 len += acpigen_emit_namestring(name);
406 len += acpigen_emit_byte(nargs & 7);
407
408 return len;
409}
410
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100411int acpigen_write_device(const char *name)
412{
413 int len;
414
415 /* method op */
416 len = acpigen_emit_byte(0x5b);
417 len += acpigen_emit_byte(0x82);
418 len += acpigen_write_len_f();
419 len += acpigen_emit_namestring(name);
420
421 return len;
422}
423
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100424/*
425 * Generates a func with max supported P-states.
426 */
Rudolf Marekf997b552009-02-14 15:40:23 +0000427int acpigen_write_PPC(u8 nr)
428{
429/*
430 Method (_PPC, 0, NotSerialized)
431 {
432 Return (nr)
433 }
434*/
435 int len;
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100436 len = acpigen_write_method("_PPC", 0);
Rudolf Marekf997b552009-02-14 15:40:23 +0000437 /* return */
438 acpigen_emit_byte(0xa4);
439 /* arg */
440 len += acpigen_write_byte(nr);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000441 /* add all single bytes */
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100442 len += 1;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100443 acpigen_pop_len();
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000444 return len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000445}
446
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100447/*
448 * Generates a func with max supported P-states saved
449 * in the variable PPCM.
450 */
Duncan Laurie0eefa002012-07-16 12:11:53 -0700451int acpigen_write_PPC_NVS(void)
452{
453/*
454 Method (_PPC, 0, NotSerialized)
455 {
456 Return (PPCM)
457 }
458*/
459 int len;
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100460
461 len = acpigen_write_method("_PPC", 0);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700462 /* return */
463 acpigen_emit_byte(0xa4);
464 /* arg */
465 len += acpigen_emit_namestring("PPCM");
466 /* add all single bytes */
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100467 len += 1;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100468 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700469 return len;
470}
471
Stefan Reinauer39205c62012-04-27 21:49:28 +0200472int acpigen_write_TPC(const char *gnvs_tpc_limit)
473{
474/*
475 // Sample _TPC method
476 Method (_TPC, 0, NotSerialized)
477 {
478 Return (\TLVL)
479 }
480 */
481 int len;
482
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100483 len = acpigen_write_method("_TPC", 0);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200484 len += acpigen_emit_byte(0xa4); /* ReturnOp */
485 len += acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100486 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200487 return len;
488}
489
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000490int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
491 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000492{
493 int len;
494 len = acpigen_write_package(6);
495 len += acpigen_write_dword(coreFreq);
496 len += acpigen_write_dword(power);
497 len += acpigen_write_dword(transLat);
498 len += acpigen_write_dword(busmLat);
499 len += acpigen_write_dword(control);
500 len += acpigen_write_dword(status);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700501 // pkglen without the len opcode
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100502 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700503
504 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
505 coreFreq, power, control, status);
506
Rudolf Marekf997b552009-02-14 15:40:23 +0000507 return len;
508}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000509
510int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
511{
512 int len, lenh, lenp;
513 lenh = acpigen_write_name("_PSD");
514 lenp = acpigen_write_package(1);
515 len = acpigen_write_package(5);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000516 len += acpigen_write_byte(5); // 5 values
517 len += acpigen_write_byte(0); // revision 0
Patrick Georgidf444bf2009-04-21 20:34:36 +0000518 len += acpigen_write_dword(domain);
519 len += acpigen_write_dword(coordtype);
520 len += acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100521 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000522 len += lenp;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100523 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000524 return len + lenh;
525}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000526
Stefan Reinauerf69b4682012-04-27 23:12:08 +0200527int acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200528{
529 int len, len0;
530 char *start, *end;
531
532 len0 = acpigen_write_package(4);
533 len = acpigen_write_resourcetemplate_header();
534 start = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200535 acpigen_write_register(&cstate->resource);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200536 end = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200537 len += end - start;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100538 len += acpigen_write_resourcetemplate_footer();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200539 len += len0;
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200540 len += acpigen_write_dword(cstate->ctype);
541 len += acpigen_write_dword(cstate->latency);
542 len += acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100543 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200544 return len;
545}
546
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200547int acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200548{
549 int len, lenh, lenp, i;
550 lenh = acpigen_write_name("_CST");
551 lenp = acpigen_write_package(nentries+1);
552 len = acpigen_write_dword(nentries);
553
554 for (i = 0; i < nentries; i++)
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200555 len += acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200556
557 len += lenp;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100558 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200559 return len + lenh;
560}
561
Stefan Reinauer39205c62012-04-27 21:49:28 +0200562int acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
563{
564/*
565 Sample _TSS package with 100% and 50% duty cycles
566 Name (_TSS, Package (0x02)
567 {
568 Package(){100, 1000, 0, 0x00, 0)
569 Package(){50, 520, 0, 0x18, 0)
570 })
571 */
572 int i, len, plen, nlen;
573 acpi_tstate_t *tstate = tstate_list;
574
575 nlen = acpigen_write_name("_TSS");
576 plen = acpigen_write_package(entries);
577
578 for (i = 0; i < entries; i++) {
579 len = acpigen_write_package(5);
580 len += acpigen_write_dword(tstate->percent);
581 len += acpigen_write_dword(tstate->power);
582 len += acpigen_write_dword(tstate->latency);
583 len += acpigen_write_dword(tstate->control);
584 len += acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100585 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200586 tstate++;
587 plen += len;
588 }
589
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100590 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200591 return plen + nlen;
592}
593
594int acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
595{
596 int len, lenh, lenp;
597 lenh = acpigen_write_name("_TSD");
598 lenp = acpigen_write_package(1);
599 len = acpigen_write_package(5);
600 len += acpigen_write_byte(5); // 5 values
601 len += acpigen_write_byte(0); // revision 0
602 len += acpigen_write_dword(domain);
603 len += acpigen_write_dword(coordtype);
604 len += acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100605 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200606 len += lenp;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100607 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200608 return len + lenh;
609}
610
611
612
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000613int acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
614{
615 /*
616 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
617 * Byte 0:
618 * Bit7 : 1 => big item
619 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
620 */
621 acpigen_emit_byte(0x86);
622 /* Byte 1+2: length (0x0009) */
623 acpigen_emit_byte(0x09);
624 acpigen_emit_byte(0x00);
625 /* bit1-7 are ignored */
626 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
627 acpigen_emit_byte(base & 0xff);
628 acpigen_emit_byte((base >> 8) & 0xff);
629 acpigen_emit_byte((base >> 16) & 0xff);
630 acpigen_emit_byte((base >> 24) & 0xff);
631 acpigen_emit_byte(size & 0xff);
632 acpigen_emit_byte((size >> 8) & 0xff);
633 acpigen_emit_byte((size >> 16) & 0xff);
634 acpigen_emit_byte((size >> 24) & 0xff);
635 return 12;
636}
637
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200638int acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200639{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200640 acpigen_emit_byte(0x82); /* Register Descriptor */
641 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
642 acpigen_emit_byte(0x00); /* Register Length 15:8 */
643 acpigen_emit_byte(addr->space_id); /* Address Space ID */
644 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
645 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
646 acpigen_emit_byte(addr->resv); /* Register Access Size */
647 acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */
648 acpigen_emit_byte((addr->addrl >> 8) & 0xff);
649 acpigen_emit_byte((addr->addrl >> 16) & 0xff);
650 acpigen_emit_byte((addr->addrl >> 24) & 0xff);
651 acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */
652 acpigen_emit_byte((addr->addrh >> 8) & 0xff);
653 acpigen_emit_byte((addr->addrh >> 16) & 0xff);
654 acpigen_emit_byte((addr->addrh >> 24) & 0xff);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200655 return 15;
656}
657
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100658int acpigen_write_irq(u16 mask)
659{
660 /*
661 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
662 * Byte 0:
663 * Bit7 : 0 => small item
664 * Bit6-3: 0100 (0x4) => IRQ port descriptor
665 * Bit2-0: 010 (0x2) => 2 Bytes long
666 */
667 acpigen_emit_byte(0x22);
668 acpigen_emit_byte(mask & 0xff);
669 acpigen_emit_byte((mask >> 8) & 0xff);
670 return 3;
671}
672
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000673int acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
674{
675 /*
676 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
677 * Byte 0:
678 * Bit7 : 0 => small item
679 * Bit6-3: 1000 (0x8) => I/O port descriptor
680 * Bit2-0: 111 (0x7) => 7 Bytes long
681 */
682 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100683 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000684 /* bit1-7 are ignored */
685 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
686 /* minimum base address the device may be configured for */
687 acpigen_emit_byte(min & 0xff);
688 acpigen_emit_byte((min >> 8) & 0xff);
689 /* maximum base address the device may be configured for */
690 acpigen_emit_byte(max & 0xff);
691 acpigen_emit_byte((max >> 8) & 0xff);
692 /* alignment for min base */
693 acpigen_emit_byte(align & 0xff);
694 acpigen_emit_byte(len & 0xff);
695 return 8;
696}
697
698int acpigen_write_resourcetemplate_header(void)
699{
700 int len;
701 /*
702 * A ResourceTemplate() is a Buffer() with a
703 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100704 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000705 * (small item 0xf, len 1)
706 */
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100707 len = acpigen_emit_byte(0x11); /* Buffer opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000708 len += acpigen_write_len_f();
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100709 len += acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000710 len_stack[ltop++] = acpigen_get_current();
711 len += acpigen_emit_byte(0x00);
712 len += acpigen_emit_byte(0x00);
713 return len;
714}
715
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100716int acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000717{
718 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100719 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000720 /*
721 * end tag (acpi 4.0 Section 6.4.2.8)
722 * 0x79 <checksum>
723 * 0x00 is treated as a good checksum according to the spec
724 * and is what iasl generates.
725 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100726 acpigen_emit_byte(0x79);
727 acpigen_emit_byte(0x00);
728
729 len = gencurrent - p;
730
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000731 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100732 p[0] = len & 0xff;
733 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000734 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100735 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000736 return 2;
737}
738
739static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
740 struct resource *res)
741{
742 acpigen_write_mem32fixed(0, res->base, res->size);
743}
744
745static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
746 struct resource *res)
747{
748 resource_t base = res->base;
749 resource_t size = res->size;
750 while (size > 0) {
751 resource_t sz = size > 255 ? 255 : size;
752 acpigen_write_io16(base, base, 0, sz, 1);
753 size -= sz;
754 base += sz;
755 }
756}
757
758int acpigen_write_mainboard_resource_template(void)
759{
760 int len;
761 char *start;
762 char *end;
763 len = acpigen_write_resourcetemplate_header();
764 start = acpigen_get_current();
765
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100766 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000767 search_global_resources(
768 IORESOURCE_MEM | IORESOURCE_RESERVE,
769 IORESOURCE_MEM | IORESOURCE_RESERVE,
770 acpigen_add_mainboard_rsvd_mem32, 0);
771
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100772 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000773 search_global_resources(
774 IORESOURCE_IO | IORESOURCE_RESERVE,
775 IORESOURCE_IO | IORESOURCE_RESERVE,
776 acpigen_add_mainboard_rsvd_io, 0);
777
778 end = acpigen_get_current();
779 len += end-start;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100780 len += acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000781 return len;
782}
783
784int acpigen_write_mainboard_resources(const char *scope, const char *name)
785{
786 int len;
787 len = acpigen_write_scope(scope);
788 len += acpigen_write_name(name);
789 len += acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100790 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000791 return len;
792}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100793
794static int hex2bin(const char c)
795{
796 if (c >= 'A' && c <= 'F')
797 return c - 'A' + 10;
798 if (c >= 'a' && c <= 'f')
799 return c - 'a' + 10;
800 return c - '0';
801}
802
803int acpigen_emit_eisaid(const char *eisaid)
804{
805 u32 compact = 0;
806
807 /* Clamping individual values would be better but
808 there is a disagreement over what is a valid
809 EISA id, so accept anything and don't clamp,
810 parent code should create a valid EISAid.
811 */
812 compact |= (eisaid[0] - 'A' + 1) << 26;
813 compact |= (eisaid[1] - 'A' + 1) << 21;
814 compact |= (eisaid[2] - 'A' + 1) << 16;
815 compact |= hex2bin(eisaid[3]) << 12;
816 compact |= hex2bin(eisaid[4]) << 8;
817 compact |= hex2bin(eisaid[5]) << 4;
818 compact |= hex2bin(eisaid[6]);
819
820 acpigen_emit_byte(0xc);
821 acpigen_emit_byte((compact >> 24) & 0xff);
822 acpigen_emit_byte((compact >> 16) & 0xff);
823 acpigen_emit_byte((compact >> 8) & 0xff);
824 acpigen_emit_byte(compact & 0xff);
825 return 5;
826}