blob: 108d6c84ebbf63e072edc5c3b7031d9fc91764c4 [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
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
Vladimir Serbinenko430363a2014-11-02 21:51:22 +010049void acpigen_pop_len(void)
50{
51 int len;
52 ASSERT(ltop > 0)
53 char *p = len_stack[--ltop];
54 len = gencurrent - p;
55 ASSERT(len <= ACPIGEN_MAXLEN)
56 /* generate store length for 0xfff max */
57 p[0] = (0x40 | (len & 0xf));
58 p[1] = (len >> 4 & 0xff);
59
60}
61
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000062void acpigen_set_current(char *curr)
63{
64 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000065}
66
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000067char *acpigen_get_current(void)
68{
69 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000070}
71
72int acpigen_emit_byte(unsigned char b)
73{
74 (*gencurrent++) = b;
75 return 1;
76}
77
78int acpigen_write_package(int nr_el)
79{
80 int len;
81 /* package op */
82 acpigen_emit_byte(0x12);
83 len = acpigen_write_len_f();
84 acpigen_emit_byte(nr_el);
85 return len + 2;
86}
87
88int acpigen_write_byte(unsigned int data)
89{
90 /* byte op */
91 acpigen_emit_byte(0xa);
92 acpigen_emit_byte(data & 0xff);
93 return 2;
94}
95
96int acpigen_write_dword(unsigned int data)
97{
98 /* dword op */
99 acpigen_emit_byte(0xc);
100 acpigen_emit_byte(data & 0xff);
101 acpigen_emit_byte((data >> 8) & 0xff);
102 acpigen_emit_byte((data >> 16) & 0xff);
103 acpigen_emit_byte((data >> 24) & 0xff);
104 return 5;
105}
106
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000107int acpigen_write_qword(uint64_t data)
108{
109 /* qword op */
110 acpigen_emit_byte(0xe);
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 acpigen_emit_byte((data >> 32) & 0xff);
116 acpigen_emit_byte((data >> 40) & 0xff);
117 acpigen_emit_byte((data >> 48) & 0xff);
118 acpigen_emit_byte((data >> 56) & 0xff);
119 return 9;
120}
121
Myles Watson3fe6b702009-10-09 20:13:43 +0000122int acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000123{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000124 int len;
125 len = acpigen_write_name(name);
126 len += acpigen_write_byte(val);
127 return len;
128}
129
Myles Watson3fe6b702009-10-09 20:13:43 +0000130int acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000131{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000132 int len;
133 len = acpigen_write_name(name);
134 len += acpigen_write_dword(val);
135 return len;
136}
137
Myles Watson3fe6b702009-10-09 20:13:43 +0000138int acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000139{
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000140 int len;
141 len = acpigen_write_name(name);
142 len += acpigen_write_qword(val);
143 return len;
144}
145
Myles Watson3fe6b702009-10-09 20:13:43 +0000146int acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000147{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000148 int i;
149 for (i = 0; i < size; i++) {
150 acpigen_emit_byte(data[i]);
151 }
152 return size;
153}
154
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100155/*
156 * The naming conventions for ACPI namespace names are a bit tricky as
157 * each element has to be 4 chars wide (»All names are a fixed 32 bits.«)
158 * and »By convention, when an ASL compiler pads a name shorter than 4
159 * characters, it is done so with trailing underscores (‘_’).«.
160 *
161 * Check sections 5.3, 18.2.2 and 18.4 of ACPI spec 3.0 for details.
162 */
Rudolf Marek3310d752009-06-21 20:26:13 +0000163
Myles Watson3fe6b702009-10-09 20:13:43 +0000164static int acpigen_emit_simple_namestring(const char *name) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000165 int i, len = 0;
166 char ud[] = "____";
167 for (i = 0; i < 4; i++) {
168 if ((name[i] == '\0') || (name[i] == '.')) {
169 len += acpigen_emit_stream(ud, 4 - i);
170 break;
171 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000172 len += acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000173 }
174 }
175 return len;
176}
177
Myles Watson3fe6b702009-10-09 20:13:43 +0000178static int acpigen_emit_double_namestring(const char *name, int dotpos) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000179 int len = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000180 /* mark dual name prefix */
181 len += acpigen_emit_byte(0x2e);
182 len += acpigen_emit_simple_namestring(name);
183 len += acpigen_emit_simple_namestring(&name[dotpos + 1]);
184 return len;
185}
186
Myles Watson3fe6b702009-10-09 20:13:43 +0000187static int acpigen_emit_multi_namestring(const char *name) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000188 int len = 0, count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000189 unsigned char *pathlen;
Rudolf Marek3310d752009-06-21 20:26:13 +0000190 /* mark multi name prefix */
191 len += acpigen_emit_byte(0x2f);
192 len += acpigen_emit_byte(0x0);
193 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
194
195 while (name[0] != '\0') {
196 len += acpigen_emit_simple_namestring(name);
197 /* find end or next entity */
198 while ((name[0] != '.') && (name[0] != '\0'))
199 name++;
200 /* forward to next */
201 if (name[0] == '.')
202 name++;
203 count++;
204 }
205
206 pathlen[0] = count;
207 return len;
208}
209
210
Myles Watson3fe6b702009-10-09 20:13:43 +0000211int acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000212 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000213 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000214 int len = 0;
215
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600216 /* We can start with a '\'. */
Rudolf Marek3310d752009-06-21 20:26:13 +0000217 if (namepath[0] == '\\') {
218 len += acpigen_emit_byte('\\');
219 namepath++;
220 }
221
Ronald G. Minnichbe738eb2013-03-07 11:05:28 -0600222 /* And there can be any number of '^' */
Rudolf Marek3310d752009-06-21 20:26:13 +0000223 while (namepath[0] == '^') {
224 len += acpigen_emit_byte('^');
225 namepath++;
226 }
227
Vladimir Serbinenkod942ed92014-08-30 20:44:37 +0200228 /* If we have only \\ or only ^...^. Then we need to put a null
229 name (0x00). */
230 if(namepath[0] == '\0') {
231 len += acpigen_emit_byte(0x00);
232 return len;
233 }
Rudolf Marek3310d752009-06-21 20:26:13 +0000234
235 i = 0;
236 while (namepath[i] != '\0') {
237 if (namepath[i] == '.') {
238 dotcount++;
239 dotpos = i;
240 }
241 i++;
242 }
243
244 if (dotcount == 0) {
245 len += acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000246 } else if (dotcount == 1) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000247 len += acpigen_emit_double_namestring(namepath, dotpos);
248 } else {
249 len += acpigen_emit_multi_namestring(namepath);
250 }
251 return len;
252}
253
Myles Watson3fe6b702009-10-09 20:13:43 +0000254int acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000255{
Rudolf Marek3310d752009-06-21 20:26:13 +0000256 int len;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000257 /* name op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000258 len = acpigen_emit_byte(0x8);
259 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000260}
261
Myles Watson3fe6b702009-10-09 20:13:43 +0000262int acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000263{
264 int len;
265 /* scope op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000266 len = acpigen_emit_byte(0x10);
267 len += acpigen_write_len_f();
268 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000269}
Rudolf Marekf997b552009-02-14 15:40:23 +0000270
271int acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
272{
273/*
274 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
275 {
276*/
277 char pscope[16];
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000278 int len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000279 /* processor op */
280 acpigen_emit_byte(0x5b);
281 acpigen_emit_byte(0x83);
282 len = acpigen_write_len_f();
283
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100284 snprintf(pscope, sizeof (pscope),
Timothy Pearson033bb4b2015-02-10 22:21:39 -0600285 "\\_PR.CP%02d", (unsigned int) cpuindex);
Rudolf Marek3310d752009-06-21 20:26:13 +0000286 len += acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000287 acpigen_emit_byte(cpuindex);
288 acpigen_emit_byte(pblock_addr & 0xff);
289 acpigen_emit_byte((pblock_addr >> 8) & 0xff);
290 acpigen_emit_byte((pblock_addr >> 16) & 0xff);
291 acpigen_emit_byte((pblock_addr >> 24) & 0xff);
292 acpigen_emit_byte(pblock_len);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000293 return 6 + 2 + len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000294}
295
296int acpigen_write_empty_PCT(void)
297{
298/*
299 Name (_PCT, Package (0x02)
300 {
301 ResourceTemplate ()
302 {
303 Register (FFixedHW,
304 0x00, // Bit Width
305 0x00, // Bit Offset
306 0x0000000000000000, // Address
307 ,)
308 },
309
310 ResourceTemplate ()
311 {
312 Register (FFixedHW,
313 0x00, // Bit Width
314 0x00, // Bit Offset
315 0x0000000000000000, // Address
316 ,)
317 }
318 })
319*/
320 static char stream[] = {
321 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
322 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
323 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
324 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
325 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
327 0x00, 0x79, 0x00
328 };
329 return acpigen_emit_stream(stream, ARRAY_SIZE(stream));
330}
331
Stefan Reinauer39205c62012-04-27 21:49:28 +0200332int acpigen_write_empty_PTC(void)
333{
334/*
335 Name (_PTC, Package (0x02)
336 {
337 ResourceTemplate ()
338 {
339 Register (FFixedHW,
340 0x00, // Bit Width
341 0x00, // Bit Offset
342 0x0000000000000000, // Address
343 ,)
344 },
345
346 ResourceTemplate ()
347 {
348 Register (FFixedHW,
349 0x00, // Bit Width
350 0x00, // Bit Offset
351 0x0000000000000000, // Address
352 ,)
353 }
354 })
355*/
356 int len, nlen, rlen;
357 acpi_addr_t addr = {
358 .space_id = ACPI_ADDRESS_SPACE_FIXED,
359 .bit_width = 0,
360 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800361 {
362 .resv = 0
363 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200364 .addrl = 0,
365 .addrh = 0,
366 };
367
368 nlen = acpigen_write_name("_PTC");
369 len = acpigen_write_package(2);
370
371 /* ControlRegister */
372 rlen = acpigen_write_resourcetemplate_header();
373 rlen += acpigen_write_register(&addr);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100374 len += acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200375 len += rlen;
376
377 /* StatusRegister */
378 rlen = acpigen_write_resourcetemplate_header();
379 rlen += acpigen_write_register(&addr);
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100380 len += acpigen_write_resourcetemplate_footer();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200381 len += rlen;
382
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100383 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200384 return len + nlen;
385}
386
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100387int acpigen_write_method(const char *name, int nargs)
388{
389 int len;
390
391 /* method op */
392 len = acpigen_emit_byte(0x14);
393 len += acpigen_write_len_f();
394 len += acpigen_emit_namestring(name);
395 len += acpigen_emit_byte(nargs & 7);
396
397 return len;
398}
399
Vladimir Serbinenko663be6e2014-11-05 21:29:45 +0100400int acpigen_write_device(const char *name)
401{
402 int len;
403
404 /* method op */
405 len = acpigen_emit_byte(0x5b);
406 len += acpigen_emit_byte(0x82);
407 len += acpigen_write_len_f();
408 len += acpigen_emit_namestring(name);
409
410 return len;
411}
412
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100413/*
414 * Generates a func with max supported P-states.
415 */
Rudolf Marekf997b552009-02-14 15:40:23 +0000416int acpigen_write_PPC(u8 nr)
417{
418/*
419 Method (_PPC, 0, NotSerialized)
420 {
421 Return (nr)
422 }
423*/
424 int len;
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100425 len = acpigen_write_method("_PPC", 0);
Rudolf Marekf997b552009-02-14 15:40:23 +0000426 /* return */
427 acpigen_emit_byte(0xa4);
428 /* arg */
429 len += acpigen_write_byte(nr);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000430 /* add all single bytes */
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100431 len += 1;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100432 acpigen_pop_len();
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000433 return len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000434}
435
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100436/*
437 * Generates a func with max supported P-states saved
438 * in the variable PPCM.
439 */
Duncan Laurie0eefa002012-07-16 12:11:53 -0700440int acpigen_write_PPC_NVS(void)
441{
442/*
443 Method (_PPC, 0, NotSerialized)
444 {
445 Return (PPCM)
446 }
447*/
448 int len;
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100449
450 len = acpigen_write_method("_PPC", 0);
Duncan Laurie0eefa002012-07-16 12:11:53 -0700451 /* return */
452 acpigen_emit_byte(0xa4);
453 /* arg */
454 len += acpigen_emit_namestring("PPCM");
455 /* add all single bytes */
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100456 len += 1;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100457 acpigen_pop_len();
Duncan Laurie0eefa002012-07-16 12:11:53 -0700458 return len;
459}
460
Stefan Reinauer39205c62012-04-27 21:49:28 +0200461int acpigen_write_TPC(const char *gnvs_tpc_limit)
462{
463/*
464 // Sample _TPC method
465 Method (_TPC, 0, NotSerialized)
466 {
467 Return (\TLVL)
468 }
469 */
470 int len;
471
Vladimir Serbinenko80fb8ed2014-11-05 10:28:28 +0100472 len = acpigen_write_method("_TPC", 0);
Stefan Reinauer39205c62012-04-27 21:49:28 +0200473 len += acpigen_emit_byte(0xa4); /* ReturnOp */
474 len += acpigen_emit_namestring(gnvs_tpc_limit);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100475 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200476 return len;
477}
478
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000479int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
480 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000481{
482 int len;
483 len = acpigen_write_package(6);
484 len += acpigen_write_dword(coreFreq);
485 len += acpigen_write_dword(power);
486 len += acpigen_write_dword(transLat);
487 len += acpigen_write_dword(busmLat);
488 len += acpigen_write_dword(control);
489 len += acpigen_write_dword(status);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700490 // pkglen without the len opcode
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100491 acpigen_pop_len();
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700492
493 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
494 coreFreq, power, control, status);
495
Rudolf Marekf997b552009-02-14 15:40:23 +0000496 return len;
497}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000498
499int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
500{
501 int len, lenh, lenp;
502 lenh = acpigen_write_name("_PSD");
503 lenp = acpigen_write_package(1);
504 len = acpigen_write_package(5);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000505 len += acpigen_write_byte(5); // 5 values
506 len += acpigen_write_byte(0); // revision 0
Patrick Georgidf444bf2009-04-21 20:34:36 +0000507 len += acpigen_write_dword(domain);
508 len += acpigen_write_dword(coordtype);
509 len += acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100510 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000511 len += lenp;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100512 acpigen_pop_len();
Patrick Georgidf444bf2009-04-21 20:34:36 +0000513 return len + lenh;
514}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000515
Stefan Reinauerf69b4682012-04-27 23:12:08 +0200516int acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200517{
518 int len, len0;
519 char *start, *end;
520
521 len0 = acpigen_write_package(4);
522 len = acpigen_write_resourcetemplate_header();
523 start = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200524 acpigen_write_register(&cstate->resource);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200525 end = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200526 len += end - start;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100527 len += acpigen_write_resourcetemplate_footer();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200528 len += len0;
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200529 len += acpigen_write_dword(cstate->ctype);
530 len += acpigen_write_dword(cstate->latency);
531 len += acpigen_write_dword(cstate->power);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100532 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200533 return len;
534}
535
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200536int acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200537{
538 int len, lenh, lenp, i;
539 lenh = acpigen_write_name("_CST");
540 lenp = acpigen_write_package(nentries+1);
541 len = acpigen_write_dword(nentries);
542
543 for (i = 0; i < nentries; i++)
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200544 len += acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200545
546 len += lenp;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100547 acpigen_pop_len();
Sven Schnelle0b86c762011-10-21 21:46:47 +0200548 return len + lenh;
549}
550
Stefan Reinauer39205c62012-04-27 21:49:28 +0200551int acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
552{
553/*
554 Sample _TSS package with 100% and 50% duty cycles
555 Name (_TSS, Package (0x02)
556 {
557 Package(){100, 1000, 0, 0x00, 0)
558 Package(){50, 520, 0, 0x18, 0)
559 })
560 */
561 int i, len, plen, nlen;
562 acpi_tstate_t *tstate = tstate_list;
563
564 nlen = acpigen_write_name("_TSS");
565 plen = acpigen_write_package(entries);
566
567 for (i = 0; i < entries; i++) {
568 len = acpigen_write_package(5);
569 len += acpigen_write_dword(tstate->percent);
570 len += acpigen_write_dword(tstate->power);
571 len += acpigen_write_dword(tstate->latency);
572 len += acpigen_write_dword(tstate->control);
573 len += acpigen_write_dword(tstate->status);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100574 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200575 tstate++;
576 plen += len;
577 }
578
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100579 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200580 return plen + nlen;
581}
582
583int acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
584{
585 int len, lenh, lenp;
586 lenh = acpigen_write_name("_TSD");
587 lenp = acpigen_write_package(1);
588 len = acpigen_write_package(5);
589 len += acpigen_write_byte(5); // 5 values
590 len += acpigen_write_byte(0); // revision 0
591 len += acpigen_write_dword(domain);
592 len += acpigen_write_dword(coordtype);
593 len += acpigen_write_dword(numprocs);
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100594 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200595 len += lenp;
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100596 acpigen_pop_len();
Stefan Reinauer39205c62012-04-27 21:49:28 +0200597 return len + lenh;
598}
599
600
601
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000602int acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
603{
604 /*
605 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
606 * Byte 0:
607 * Bit7 : 1 => big item
608 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
609 */
610 acpigen_emit_byte(0x86);
611 /* Byte 1+2: length (0x0009) */
612 acpigen_emit_byte(0x09);
613 acpigen_emit_byte(0x00);
614 /* bit1-7 are ignored */
615 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
616 acpigen_emit_byte(base & 0xff);
617 acpigen_emit_byte((base >> 8) & 0xff);
618 acpigen_emit_byte((base >> 16) & 0xff);
619 acpigen_emit_byte((base >> 24) & 0xff);
620 acpigen_emit_byte(size & 0xff);
621 acpigen_emit_byte((size >> 8) & 0xff);
622 acpigen_emit_byte((size >> 16) & 0xff);
623 acpigen_emit_byte((size >> 24) & 0xff);
624 return 12;
625}
626
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200627int acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200628{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200629 acpigen_emit_byte(0x82); /* Register Descriptor */
630 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
631 acpigen_emit_byte(0x00); /* Register Length 15:8 */
632 acpigen_emit_byte(addr->space_id); /* Address Space ID */
633 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
634 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
635 acpigen_emit_byte(addr->resv); /* Register Access Size */
636 acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */
637 acpigen_emit_byte((addr->addrl >> 8) & 0xff);
638 acpigen_emit_byte((addr->addrl >> 16) & 0xff);
639 acpigen_emit_byte((addr->addrl >> 24) & 0xff);
640 acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */
641 acpigen_emit_byte((addr->addrh >> 8) & 0xff);
642 acpigen_emit_byte((addr->addrh >> 16) & 0xff);
643 acpigen_emit_byte((addr->addrh >> 24) & 0xff);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200644 return 15;
645}
646
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100647int acpigen_write_irq(u16 mask)
648{
649 /*
650 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
651 * Byte 0:
652 * Bit7 : 0 => small item
653 * Bit6-3: 0100 (0x4) => IRQ port descriptor
654 * Bit2-0: 010 (0x2) => 2 Bytes long
655 */
656 acpigen_emit_byte(0x22);
657 acpigen_emit_byte(mask & 0xff);
658 acpigen_emit_byte((mask >> 8) & 0xff);
659 return 3;
660}
661
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000662int acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
663{
664 /*
665 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
666 * Byte 0:
667 * Bit7 : 0 => small item
668 * Bit6-3: 1000 (0x8) => I/O port descriptor
669 * Bit2-0: 111 (0x7) => 7 Bytes long
670 */
671 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100672 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000673 /* bit1-7 are ignored */
674 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
675 /* minimum base address the device may be configured for */
676 acpigen_emit_byte(min & 0xff);
677 acpigen_emit_byte((min >> 8) & 0xff);
678 /* maximum base address the device may be configured for */
679 acpigen_emit_byte(max & 0xff);
680 acpigen_emit_byte((max >> 8) & 0xff);
681 /* alignment for min base */
682 acpigen_emit_byte(align & 0xff);
683 acpigen_emit_byte(len & 0xff);
684 return 8;
685}
686
687int acpigen_write_resourcetemplate_header(void)
688{
689 int len;
690 /*
691 * A ResourceTemplate() is a Buffer() with a
692 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100693 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000694 * (small item 0xf, len 1)
695 */
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100696 len = acpigen_emit_byte(0x11); /* Buffer opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000697 len += acpigen_write_len_f();
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100698 len += acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000699 len_stack[ltop++] = acpigen_get_current();
700 len += acpigen_emit_byte(0x00);
701 len += acpigen_emit_byte(0x00);
702 return len;
703}
704
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100705int acpigen_write_resourcetemplate_footer(void)
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000706{
707 char *p = len_stack[--ltop];
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100708 int len;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000709 /*
710 * end tag (acpi 4.0 Section 6.4.2.8)
711 * 0x79 <checksum>
712 * 0x00 is treated as a good checksum according to the spec
713 * and is what iasl generates.
714 */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100715 acpigen_emit_byte(0x79);
716 acpigen_emit_byte(0x00);
717
718 len = gencurrent - p;
719
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000720 /* patch len word */
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100721 p[0] = len & 0xff;
722 p[1] = (len >> 8) & 0xff;
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000723 /* patch len field */
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100724 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000725 return 2;
726}
727
728static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
729 struct resource *res)
730{
731 acpigen_write_mem32fixed(0, res->base, res->size);
732}
733
734static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
735 struct resource *res)
736{
737 resource_t base = res->base;
738 resource_t size = res->size;
739 while (size > 0) {
740 resource_t sz = size > 255 ? 255 : size;
741 acpigen_write_io16(base, base, 0, sz, 1);
742 size -= sz;
743 base += sz;
744 }
745}
746
747int acpigen_write_mainboard_resource_template(void)
748{
749 int len;
750 char *start;
751 char *end;
752 len = acpigen_write_resourcetemplate_header();
753 start = acpigen_get_current();
754
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100755 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000756 search_global_resources(
757 IORESOURCE_MEM | IORESOURCE_RESERVE,
758 IORESOURCE_MEM | IORESOURCE_RESERVE,
759 acpigen_add_mainboard_rsvd_mem32, 0);
760
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100761 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000762 search_global_resources(
763 IORESOURCE_IO | IORESOURCE_RESERVE,
764 IORESOURCE_IO | IORESOURCE_RESERVE,
765 acpigen_add_mainboard_rsvd_io, 0);
766
767 end = acpigen_get_current();
768 len += end-start;
Vladimir Serbinenko9acc1e82014-11-09 03:37:28 +0100769 len += acpigen_write_resourcetemplate_footer();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000770 return len;
771}
772
773int acpigen_write_mainboard_resources(const char *scope, const char *name)
774{
775 int len;
776 len = acpigen_write_scope(scope);
777 len += acpigen_write_name(name);
778 len += acpigen_write_mainboard_resource_template();
Vladimir Serbinenkoa09f4db2014-11-09 03:32:58 +0100779 acpigen_pop_len();
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000780 return len;
781}
Vladimir Serbinenko8ecdc9e2014-02-15 18:59:40 +0100782
783static int hex2bin(const char c)
784{
785 if (c >= 'A' && c <= 'F')
786 return c - 'A' + 10;
787 if (c >= 'a' && c <= 'f')
788 return c - 'a' + 10;
789 return c - '0';
790}
791
792int acpigen_emit_eisaid(const char *eisaid)
793{
794 u32 compact = 0;
795
796 /* Clamping individual values would be better but
797 there is a disagreement over what is a valid
798 EISA id, so accept anything and don't clamp,
799 parent code should create a valid EISAid.
800 */
801 compact |= (eisaid[0] - 'A' + 1) << 26;
802 compact |= (eisaid[1] - 'A' + 1) << 21;
803 compact |= (eisaid[2] - 'A' + 1) << 16;
804 compact |= hex2bin(eisaid[3]) << 12;
805 compact |= hex2bin(eisaid[4]) << 8;
806 compact |= hex2bin(eisaid[5]) << 4;
807 compact |= hex2bin(eisaid[6]);
808
809 acpigen_emit_byte(0xc);
810 acpigen_emit_byte((compact >> 24) & 0xff);
811 acpigen_emit_byte((compact >> 16) & 0xff);
812 acpigen_emit_byte((compact >> 8) & 0xff);
813 acpigen_emit_byte(compact & 0xff);
814 return 5;
815}