blob: 121cb22f050f10c4e69d1fbf6fa2f503520ab53e [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
226 ASSERT(namepath[0] != '\0');
227
228 i = 0;
229 while (namepath[i] != '\0') {
230 if (namepath[i] == '.') {
231 dotcount++;
232 dotpos = i;
233 }
234 i++;
235 }
236
237 if (dotcount == 0) {
238 len += acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000239 } else if (dotcount == 1) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000240 len += acpigen_emit_double_namestring(namepath, dotpos);
241 } else {
242 len += acpigen_emit_multi_namestring(namepath);
243 }
244 return len;
245}
246
Myles Watson3fe6b702009-10-09 20:13:43 +0000247int acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000248{
Rudolf Marek3310d752009-06-21 20:26:13 +0000249 int len;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000250 /* name op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000251 len = acpigen_emit_byte(0x8);
252 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000253}
254
Myles Watson3fe6b702009-10-09 20:13:43 +0000255int acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000256{
257 int len;
258 /* scope op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000259 len = acpigen_emit_byte(0x10);
260 len += acpigen_write_len_f();
261 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000262}
Rudolf Marekf997b552009-02-14 15:40:23 +0000263
264int acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
265{
266/*
267 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
268 {
269*/
270 char pscope[16];
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000271 int len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000272 /* processor op */
273 acpigen_emit_byte(0x5b);
274 acpigen_emit_byte(0x83);
275 len = acpigen_write_len_f();
276
Rudolf Marek3310d752009-06-21 20:26:13 +0000277 sprintf(pscope, "\\_PR.CPU%x", (unsigned int) cpuindex);
278 len += acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000279 acpigen_emit_byte(cpuindex);
280 acpigen_emit_byte(pblock_addr & 0xff);
281 acpigen_emit_byte((pblock_addr >> 8) & 0xff);
282 acpigen_emit_byte((pblock_addr >> 16) & 0xff);
283 acpigen_emit_byte((pblock_addr >> 24) & 0xff);
284 acpigen_emit_byte(pblock_len);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000285 return 6 + 2 + len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000286}
287
288int acpigen_write_empty_PCT(void)
289{
290/*
291 Name (_PCT, Package (0x02)
292 {
293 ResourceTemplate ()
294 {
295 Register (FFixedHW,
296 0x00, // Bit Width
297 0x00, // Bit Offset
298 0x0000000000000000, // Address
299 ,)
300 },
301
302 ResourceTemplate ()
303 {
304 Register (FFixedHW,
305 0x00, // Bit Width
306 0x00, // Bit Offset
307 0x0000000000000000, // Address
308 ,)
309 }
310 })
311*/
312 static char stream[] = {
313 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
314 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
315 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
316 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
317 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
319 0x00, 0x79, 0x00
320 };
321 return acpigen_emit_stream(stream, ARRAY_SIZE(stream));
322}
323
Stefan Reinauer39205c62012-04-27 21:49:28 +0200324int acpigen_write_empty_PTC(void)
325{
326/*
327 Name (_PTC, Package (0x02)
328 {
329 ResourceTemplate ()
330 {
331 Register (FFixedHW,
332 0x00, // Bit Width
333 0x00, // Bit Offset
334 0x0000000000000000, // Address
335 ,)
336 },
337
338 ResourceTemplate ()
339 {
340 Register (FFixedHW,
341 0x00, // Bit Width
342 0x00, // Bit Offset
343 0x0000000000000000, // Address
344 ,)
345 }
346 })
347*/
348 int len, nlen, rlen;
349 acpi_addr_t addr = {
350 .space_id = ACPI_ADDRESS_SPACE_FIXED,
351 .bit_width = 0,
352 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800353 {
354 .resv = 0
355 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200356 .addrl = 0,
357 .addrh = 0,
358 };
359
360 nlen = acpigen_write_name("_PTC");
361 len = acpigen_write_package(2);
362
363 /* ControlRegister */
364 rlen = acpigen_write_resourcetemplate_header();
365 rlen += acpigen_write_register(&addr);
366 len += acpigen_write_resourcetemplate_footer(rlen);
367 len += rlen;
368
369 /* StatusRegister */
370 rlen = acpigen_write_resourcetemplate_header();
371 rlen += acpigen_write_register(&addr);
372 len += acpigen_write_resourcetemplate_footer(rlen);
373 len += rlen;
374
375 acpigen_patch_len(len - 1);
376 return len + nlen;
377}
378
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100379/*
380 * Generates a func with max supported P-states.
381 */
Rudolf Marekf997b552009-02-14 15:40:23 +0000382int acpigen_write_PPC(u8 nr)
383{
384/*
385 Method (_PPC, 0, NotSerialized)
386 {
387 Return (nr)
388 }
389*/
390 int len;
391 /* method op */
392 acpigen_emit_byte(0x14);
393 len = acpigen_write_len_f();
Rudolf Marek3310d752009-06-21 20:26:13 +0000394 len += acpigen_emit_namestring("_PPC");
Rudolf Marekf997b552009-02-14 15:40:23 +0000395 /* no fnarg */
396 acpigen_emit_byte(0x00);
397 /* return */
398 acpigen_emit_byte(0xa4);
399 /* arg */
400 len += acpigen_write_byte(nr);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000401 /* add all single bytes */
402 len += 3;
Rudolf Marekf997b552009-02-14 15:40:23 +0000403 acpigen_patch_len(len - 1);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000404 return len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000405}
406
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100407/*
408 * Generates a func with max supported P-states saved
409 * in the variable PPCM.
410 */
Duncan Laurie0eefa002012-07-16 12:11:53 -0700411int acpigen_write_PPC_NVS(void)
412{
413/*
414 Method (_PPC, 0, NotSerialized)
415 {
416 Return (PPCM)
417 }
418*/
419 int len;
420 /* method op */
421 acpigen_emit_byte(0x14);
422 len = acpigen_write_len_f();
423 len += acpigen_emit_namestring("_PPC");
424 /* no fnarg */
425 acpigen_emit_byte(0x00);
426 /* return */
427 acpigen_emit_byte(0xa4);
428 /* arg */
429 len += acpigen_emit_namestring("PPCM");
430 /* add all single bytes */
431 len += 3;
432 acpigen_patch_len(len - 1);
433 return len;
434}
435
Stefan Reinauer39205c62012-04-27 21:49:28 +0200436int acpigen_write_TPC(const char *gnvs_tpc_limit)
437{
438/*
439 // Sample _TPC method
440 Method (_TPC, 0, NotSerialized)
441 {
442 Return (\TLVL)
443 }
444 */
445 int len;
446
447 len = acpigen_emit_byte(0x14); /* MethodOp */
448 len += acpigen_write_len_f(); /* PkgLength */
449 len += acpigen_emit_namestring("_TPC");
450 len += acpigen_emit_byte(0x00); /* No Arguments */
451 len += acpigen_emit_byte(0xa4); /* ReturnOp */
452 len += acpigen_emit_namestring(gnvs_tpc_limit);
453 acpigen_patch_len(len - 1);
454 return len;
455}
456
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000457int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
458 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000459{
460 int len;
461 len = acpigen_write_package(6);
462 len += acpigen_write_dword(coreFreq);
463 len += acpigen_write_dword(power);
464 len += acpigen_write_dword(transLat);
465 len += acpigen_write_dword(busmLat);
466 len += acpigen_write_dword(control);
467 len += acpigen_write_dword(status);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700468 // pkglen without the len opcode
Rudolf Marekf997b552009-02-14 15:40:23 +0000469 acpigen_patch_len(len - 1);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700470
471 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
472 coreFreq, power, control, status);
473
Rudolf Marekf997b552009-02-14 15:40:23 +0000474 return len;
475}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000476
477int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
478{
479 int len, lenh, lenp;
480 lenh = acpigen_write_name("_PSD");
481 lenp = acpigen_write_package(1);
482 len = acpigen_write_package(5);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000483 len += acpigen_write_byte(5); // 5 values
484 len += acpigen_write_byte(0); // revision 0
Patrick Georgidf444bf2009-04-21 20:34:36 +0000485 len += acpigen_write_dword(domain);
486 len += acpigen_write_dword(coordtype);
487 len += acpigen_write_dword(numprocs);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000488 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000489 len += lenp;
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000490 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000491 return len + lenh;
492}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000493
Stefan Reinauerf69b4682012-04-27 23:12:08 +0200494int acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200495{
496 int len, len0;
497 char *start, *end;
498
499 len0 = acpigen_write_package(4);
500 len = acpigen_write_resourcetemplate_header();
501 start = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200502 acpigen_write_register(&cstate->resource);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200503 end = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200504 len += end - start;
Sven Schnelle0b86c762011-10-21 21:46:47 +0200505 len += acpigen_write_resourcetemplate_footer(len);
506 len += len0;
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200507 len += acpigen_write_dword(cstate->ctype);
508 len += acpigen_write_dword(cstate->latency);
509 len += acpigen_write_dword(cstate->power);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200510 acpigen_patch_len(len - 1);
511 return len;
512}
513
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200514int acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200515{
516 int len, lenh, lenp, i;
517 lenh = acpigen_write_name("_CST");
518 lenp = acpigen_write_package(nentries+1);
519 len = acpigen_write_dword(nentries);
520
521 for (i = 0; i < nentries; i++)
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200522 len += acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200523
524 len += lenp;
525 acpigen_patch_len(len - 1);
526 return len + lenh;
527}
528
Stefan Reinauer39205c62012-04-27 21:49:28 +0200529int acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
530{
531/*
532 Sample _TSS package with 100% and 50% duty cycles
533 Name (_TSS, Package (0x02)
534 {
535 Package(){100, 1000, 0, 0x00, 0)
536 Package(){50, 520, 0, 0x18, 0)
537 })
538 */
539 int i, len, plen, nlen;
540 acpi_tstate_t *tstate = tstate_list;
541
542 nlen = acpigen_write_name("_TSS");
543 plen = acpigen_write_package(entries);
544
545 for (i = 0; i < entries; i++) {
546 len = acpigen_write_package(5);
547 len += acpigen_write_dword(tstate->percent);
548 len += acpigen_write_dword(tstate->power);
549 len += acpigen_write_dword(tstate->latency);
550 len += acpigen_write_dword(tstate->control);
551 len += acpigen_write_dword(tstate->status);
552 acpigen_patch_len(len - 1);
553 tstate++;
554 plen += len;
555 }
556
557 acpigen_patch_len(plen - 1);
558 return plen + nlen;
559}
560
561int acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
562{
563 int len, lenh, lenp;
564 lenh = acpigen_write_name("_TSD");
565 lenp = acpigen_write_package(1);
566 len = acpigen_write_package(5);
567 len += acpigen_write_byte(5); // 5 values
568 len += acpigen_write_byte(0); // revision 0
569 len += acpigen_write_dword(domain);
570 len += acpigen_write_dword(coordtype);
571 len += acpigen_write_dword(numprocs);
572 acpigen_patch_len(len - 1);
573 len += lenp;
574 acpigen_patch_len(len - 1);
575 return len + lenh;
576}
577
578
579
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000580int acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
581{
582 /*
583 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
584 * Byte 0:
585 * Bit7 : 1 => big item
586 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
587 */
588 acpigen_emit_byte(0x86);
589 /* Byte 1+2: length (0x0009) */
590 acpigen_emit_byte(0x09);
591 acpigen_emit_byte(0x00);
592 /* bit1-7 are ignored */
593 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
594 acpigen_emit_byte(base & 0xff);
595 acpigen_emit_byte((base >> 8) & 0xff);
596 acpigen_emit_byte((base >> 16) & 0xff);
597 acpigen_emit_byte((base >> 24) & 0xff);
598 acpigen_emit_byte(size & 0xff);
599 acpigen_emit_byte((size >> 8) & 0xff);
600 acpigen_emit_byte((size >> 16) & 0xff);
601 acpigen_emit_byte((size >> 24) & 0xff);
602 return 12;
603}
604
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200605int acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200606{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200607 acpigen_emit_byte(0x82); /* Register Descriptor */
608 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
609 acpigen_emit_byte(0x00); /* Register Length 15:8 */
610 acpigen_emit_byte(addr->space_id); /* Address Space ID */
611 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
612 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
613 acpigen_emit_byte(addr->resv); /* Register Access Size */
614 acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */
615 acpigen_emit_byte((addr->addrl >> 8) & 0xff);
616 acpigen_emit_byte((addr->addrl >> 16) & 0xff);
617 acpigen_emit_byte((addr->addrl >> 24) & 0xff);
618 acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */
619 acpigen_emit_byte((addr->addrh >> 8) & 0xff);
620 acpigen_emit_byte((addr->addrh >> 16) & 0xff);
621 acpigen_emit_byte((addr->addrh >> 24) & 0xff);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200622 return 15;
623}
624
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000625int acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
626{
627 /*
628 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
629 * Byte 0:
630 * Bit7 : 0 => small item
631 * Bit6-3: 1000 (0x8) => I/O port descriptor
632 * Bit2-0: 111 (0x7) => 7 Bytes long
633 */
634 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100635 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000636 /* bit1-7 are ignored */
637 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
638 /* minimum base address the device may be configured for */
639 acpigen_emit_byte(min & 0xff);
640 acpigen_emit_byte((min >> 8) & 0xff);
641 /* maximum base address the device may be configured for */
642 acpigen_emit_byte(max & 0xff);
643 acpigen_emit_byte((max >> 8) & 0xff);
644 /* alignment for min base */
645 acpigen_emit_byte(align & 0xff);
646 acpigen_emit_byte(len & 0xff);
647 return 8;
648}
649
650int acpigen_write_resourcetemplate_header(void)
651{
652 int len;
653 /*
654 * A ResourceTemplate() is a Buffer() with a
655 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100656 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000657 * (small item 0xf, len 1)
658 */
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100659 len = acpigen_emit_byte(0x11); /* Buffer opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000660 len += acpigen_write_len_f();
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100661 len += acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000662 len_stack[ltop++] = acpigen_get_current();
663 len += acpigen_emit_byte(0x00);
664 len += acpigen_emit_byte(0x00);
665 return len;
666}
667
668int acpigen_write_resourcetemplate_footer(int len)
669{
670 char *p = len_stack[--ltop];
671 /*
672 * end tag (acpi 4.0 Section 6.4.2.8)
673 * 0x79 <checksum>
674 * 0x00 is treated as a good checksum according to the spec
675 * and is what iasl generates.
676 */
677 len += acpigen_emit_byte(0x79);
678 len += acpigen_emit_byte(0x00);
679 /* patch len word */
680 p[0] = (len-6) & 0xff;
681 p[1] = ((len-6) >> 8) & 0xff;
682 /* patch len field */
683 acpigen_patch_len(len-1);
684 return 2;
685}
686
687static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
688 struct resource *res)
689{
690 acpigen_write_mem32fixed(0, res->base, res->size);
691}
692
693static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
694 struct resource *res)
695{
696 resource_t base = res->base;
697 resource_t size = res->size;
698 while (size > 0) {
699 resource_t sz = size > 255 ? 255 : size;
700 acpigen_write_io16(base, base, 0, sz, 1);
701 size -= sz;
702 base += sz;
703 }
704}
705
706int acpigen_write_mainboard_resource_template(void)
707{
708 int len;
709 char *start;
710 char *end;
711 len = acpigen_write_resourcetemplate_header();
712 start = acpigen_get_current();
713
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100714 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000715 search_global_resources(
716 IORESOURCE_MEM | IORESOURCE_RESERVE,
717 IORESOURCE_MEM | IORESOURCE_RESERVE,
718 acpigen_add_mainboard_rsvd_mem32, 0);
719
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100720 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000721 search_global_resources(
722 IORESOURCE_IO | IORESOURCE_RESERVE,
723 IORESOURCE_IO | IORESOURCE_RESERVE,
724 acpigen_add_mainboard_rsvd_io, 0);
725
726 end = acpigen_get_current();
727 len += end-start;
728 len += acpigen_write_resourcetemplate_footer(len);
729 return len;
730}
731
732int acpigen_write_mainboard_resources(const char *scope, const char *name)
733{
734 int len;
735 len = acpigen_write_scope(scope);
736 len += acpigen_write_name(name);
737 len += acpigen_write_mainboard_resource_template();
738 acpigen_patch_len(len - 1);
739 return len;
740}