blob: fdb7e02c9b9c7ed40a931bf299274f9add3c3fb8 [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
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100277 snprintf(pscope, sizeof (pscope),
278 "\\_PR.CPU%x", (unsigned int) cpuindex);
Rudolf Marek3310d752009-06-21 20:26:13 +0000279 len += acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000280 acpigen_emit_byte(cpuindex);
281 acpigen_emit_byte(pblock_addr & 0xff);
282 acpigen_emit_byte((pblock_addr >> 8) & 0xff);
283 acpigen_emit_byte((pblock_addr >> 16) & 0xff);
284 acpigen_emit_byte((pblock_addr >> 24) & 0xff);
285 acpigen_emit_byte(pblock_len);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000286 return 6 + 2 + len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000287}
288
289int acpigen_write_empty_PCT(void)
290{
291/*
292 Name (_PCT, Package (0x02)
293 {
294 ResourceTemplate ()
295 {
296 Register (FFixedHW,
297 0x00, // Bit Width
298 0x00, // Bit Offset
299 0x0000000000000000, // Address
300 ,)
301 },
302
303 ResourceTemplate ()
304 {
305 Register (FFixedHW,
306 0x00, // Bit Width
307 0x00, // Bit Offset
308 0x0000000000000000, // Address
309 ,)
310 }
311 })
312*/
313 static char stream[] = {
314 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
315 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
316 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
317 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
318 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
320 0x00, 0x79, 0x00
321 };
322 return acpigen_emit_stream(stream, ARRAY_SIZE(stream));
323}
324
Stefan Reinauer39205c62012-04-27 21:49:28 +0200325int acpigen_write_empty_PTC(void)
326{
327/*
328 Name (_PTC, Package (0x02)
329 {
330 ResourceTemplate ()
331 {
332 Register (FFixedHW,
333 0x00, // Bit Width
334 0x00, // Bit Offset
335 0x0000000000000000, // Address
336 ,)
337 },
338
339 ResourceTemplate ()
340 {
341 Register (FFixedHW,
342 0x00, // Bit Width
343 0x00, // Bit Offset
344 0x0000000000000000, // Address
345 ,)
346 }
347 })
348*/
349 int len, nlen, rlen;
350 acpi_addr_t addr = {
351 .space_id = ACPI_ADDRESS_SPACE_FIXED,
352 .bit_width = 0,
353 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800354 {
355 .resv = 0
356 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200357 .addrl = 0,
358 .addrh = 0,
359 };
360
361 nlen = acpigen_write_name("_PTC");
362 len = acpigen_write_package(2);
363
364 /* ControlRegister */
365 rlen = acpigen_write_resourcetemplate_header();
366 rlen += acpigen_write_register(&addr);
367 len += acpigen_write_resourcetemplate_footer(rlen);
368 len += rlen;
369
370 /* StatusRegister */
371 rlen = acpigen_write_resourcetemplate_header();
372 rlen += acpigen_write_register(&addr);
373 len += acpigen_write_resourcetemplate_footer(rlen);
374 len += rlen;
375
376 acpigen_patch_len(len - 1);
377 return len + nlen;
378}
379
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100380/*
381 * Generates a func with max supported P-states.
382 */
Rudolf Marekf997b552009-02-14 15:40:23 +0000383int acpigen_write_PPC(u8 nr)
384{
385/*
386 Method (_PPC, 0, NotSerialized)
387 {
388 Return (nr)
389 }
390*/
391 int len;
392 /* method op */
393 acpigen_emit_byte(0x14);
394 len = acpigen_write_len_f();
Rudolf Marek3310d752009-06-21 20:26:13 +0000395 len += acpigen_emit_namestring("_PPC");
Rudolf Marekf997b552009-02-14 15:40:23 +0000396 /* no fnarg */
397 acpigen_emit_byte(0x00);
398 /* return */
399 acpigen_emit_byte(0xa4);
400 /* arg */
401 len += acpigen_write_byte(nr);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000402 /* add all single bytes */
403 len += 3;
Rudolf Marekf997b552009-02-14 15:40:23 +0000404 acpigen_patch_len(len - 1);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000405 return len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000406}
407
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100408/*
409 * Generates a func with max supported P-states saved
410 * in the variable PPCM.
411 */
Duncan Laurie0eefa002012-07-16 12:11:53 -0700412int acpigen_write_PPC_NVS(void)
413{
414/*
415 Method (_PPC, 0, NotSerialized)
416 {
417 Return (PPCM)
418 }
419*/
420 int len;
421 /* method op */
422 acpigen_emit_byte(0x14);
423 len = acpigen_write_len_f();
424 len += acpigen_emit_namestring("_PPC");
425 /* no fnarg */
426 acpigen_emit_byte(0x00);
427 /* return */
428 acpigen_emit_byte(0xa4);
429 /* arg */
430 len += acpigen_emit_namestring("PPCM");
431 /* add all single bytes */
432 len += 3;
433 acpigen_patch_len(len - 1);
434 return len;
435}
436
Stefan Reinauer39205c62012-04-27 21:49:28 +0200437int acpigen_write_TPC(const char *gnvs_tpc_limit)
438{
439/*
440 // Sample _TPC method
441 Method (_TPC, 0, NotSerialized)
442 {
443 Return (\TLVL)
444 }
445 */
446 int len;
447
448 len = acpigen_emit_byte(0x14); /* MethodOp */
449 len += acpigen_write_len_f(); /* PkgLength */
450 len += acpigen_emit_namestring("_TPC");
451 len += acpigen_emit_byte(0x00); /* No Arguments */
452 len += acpigen_emit_byte(0xa4); /* ReturnOp */
453 len += acpigen_emit_namestring(gnvs_tpc_limit);
454 acpigen_patch_len(len - 1);
455 return len;
456}
457
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000458int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
459 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000460{
461 int len;
462 len = acpigen_write_package(6);
463 len += acpigen_write_dword(coreFreq);
464 len += acpigen_write_dword(power);
465 len += acpigen_write_dword(transLat);
466 len += acpigen_write_dword(busmLat);
467 len += acpigen_write_dword(control);
468 len += acpigen_write_dword(status);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700469 // pkglen without the len opcode
Rudolf Marekf997b552009-02-14 15:40:23 +0000470 acpigen_patch_len(len - 1);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700471
472 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
473 coreFreq, power, control, status);
474
Rudolf Marekf997b552009-02-14 15:40:23 +0000475 return len;
476}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000477
478int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
479{
480 int len, lenh, lenp;
481 lenh = acpigen_write_name("_PSD");
482 lenp = acpigen_write_package(1);
483 len = acpigen_write_package(5);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000484 len += acpigen_write_byte(5); // 5 values
485 len += acpigen_write_byte(0); // revision 0
Patrick Georgidf444bf2009-04-21 20:34:36 +0000486 len += acpigen_write_dword(domain);
487 len += acpigen_write_dword(coordtype);
488 len += acpigen_write_dword(numprocs);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000489 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000490 len += lenp;
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000491 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000492 return len + lenh;
493}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000494
Stefan Reinauerf69b4682012-04-27 23:12:08 +0200495int acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200496{
497 int len, len0;
498 char *start, *end;
499
500 len0 = acpigen_write_package(4);
501 len = acpigen_write_resourcetemplate_header();
502 start = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200503 acpigen_write_register(&cstate->resource);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200504 end = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200505 len += end - start;
Sven Schnelle0b86c762011-10-21 21:46:47 +0200506 len += acpigen_write_resourcetemplate_footer(len);
507 len += len0;
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200508 len += acpigen_write_dword(cstate->ctype);
509 len += acpigen_write_dword(cstate->latency);
510 len += acpigen_write_dword(cstate->power);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200511 acpigen_patch_len(len - 1);
512 return len;
513}
514
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200515int acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200516{
517 int len, lenh, lenp, i;
518 lenh = acpigen_write_name("_CST");
519 lenp = acpigen_write_package(nentries+1);
520 len = acpigen_write_dword(nentries);
521
522 for (i = 0; i < nentries; i++)
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200523 len += acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200524
525 len += lenp;
526 acpigen_patch_len(len - 1);
527 return len + lenh;
528}
529
Stefan Reinauer39205c62012-04-27 21:49:28 +0200530int acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
531{
532/*
533 Sample _TSS package with 100% and 50% duty cycles
534 Name (_TSS, Package (0x02)
535 {
536 Package(){100, 1000, 0, 0x00, 0)
537 Package(){50, 520, 0, 0x18, 0)
538 })
539 */
540 int i, len, plen, nlen;
541 acpi_tstate_t *tstate = tstate_list;
542
543 nlen = acpigen_write_name("_TSS");
544 plen = acpigen_write_package(entries);
545
546 for (i = 0; i < entries; i++) {
547 len = acpigen_write_package(5);
548 len += acpigen_write_dword(tstate->percent);
549 len += acpigen_write_dword(tstate->power);
550 len += acpigen_write_dword(tstate->latency);
551 len += acpigen_write_dword(tstate->control);
552 len += acpigen_write_dword(tstate->status);
553 acpigen_patch_len(len - 1);
554 tstate++;
555 plen += len;
556 }
557
558 acpigen_patch_len(plen - 1);
559 return plen + nlen;
560}
561
562int acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
563{
564 int len, lenh, lenp;
565 lenh = acpigen_write_name("_TSD");
566 lenp = acpigen_write_package(1);
567 len = acpigen_write_package(5);
568 len += acpigen_write_byte(5); // 5 values
569 len += acpigen_write_byte(0); // revision 0
570 len += acpigen_write_dword(domain);
571 len += acpigen_write_dword(coordtype);
572 len += acpigen_write_dword(numprocs);
573 acpigen_patch_len(len - 1);
574 len += lenp;
575 acpigen_patch_len(len - 1);
576 return len + lenh;
577}
578
579
580
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000581int acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
582{
583 /*
584 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
585 * Byte 0:
586 * Bit7 : 1 => big item
587 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
588 */
589 acpigen_emit_byte(0x86);
590 /* Byte 1+2: length (0x0009) */
591 acpigen_emit_byte(0x09);
592 acpigen_emit_byte(0x00);
593 /* bit1-7 are ignored */
594 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
595 acpigen_emit_byte(base & 0xff);
596 acpigen_emit_byte((base >> 8) & 0xff);
597 acpigen_emit_byte((base >> 16) & 0xff);
598 acpigen_emit_byte((base >> 24) & 0xff);
599 acpigen_emit_byte(size & 0xff);
600 acpigen_emit_byte((size >> 8) & 0xff);
601 acpigen_emit_byte((size >> 16) & 0xff);
602 acpigen_emit_byte((size >> 24) & 0xff);
603 return 12;
604}
605
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200606int acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200607{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200608 acpigen_emit_byte(0x82); /* Register Descriptor */
609 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
610 acpigen_emit_byte(0x00); /* Register Length 15:8 */
611 acpigen_emit_byte(addr->space_id); /* Address Space ID */
612 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
613 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
614 acpigen_emit_byte(addr->resv); /* Register Access Size */
615 acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */
616 acpigen_emit_byte((addr->addrl >> 8) & 0xff);
617 acpigen_emit_byte((addr->addrl >> 16) & 0xff);
618 acpigen_emit_byte((addr->addrl >> 24) & 0xff);
619 acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */
620 acpigen_emit_byte((addr->addrh >> 8) & 0xff);
621 acpigen_emit_byte((addr->addrh >> 16) & 0xff);
622 acpigen_emit_byte((addr->addrh >> 24) & 0xff);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200623 return 15;
624}
625
Vladimir Serbinenko20ea0402014-02-15 18:57:17 +0100626int acpigen_write_irq(u16 mask)
627{
628 /*
629 * acpi 3.0b section 6.4.2.1: IRQ Descriptor
630 * Byte 0:
631 * Bit7 : 0 => small item
632 * Bit6-3: 0100 (0x4) => IRQ port descriptor
633 * Bit2-0: 010 (0x2) => 2 Bytes long
634 */
635 acpigen_emit_byte(0x22);
636 acpigen_emit_byte(mask & 0xff);
637 acpigen_emit_byte((mask >> 8) & 0xff);
638 return 3;
639}
640
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000641int acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
642{
643 /*
644 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
645 * Byte 0:
646 * Bit7 : 0 => small item
647 * Bit6-3: 1000 (0x8) => I/O port descriptor
648 * Bit2-0: 111 (0x7) => 7 Bytes long
649 */
650 acpigen_emit_byte(0x47);
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100651 /* Does the device decode all 16 or just 10 bits? */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000652 /* bit1-7 are ignored */
653 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
654 /* minimum base address the device may be configured for */
655 acpigen_emit_byte(min & 0xff);
656 acpigen_emit_byte((min >> 8) & 0xff);
657 /* maximum base address the device may be configured for */
658 acpigen_emit_byte(max & 0xff);
659 acpigen_emit_byte((max >> 8) & 0xff);
660 /* alignment for min base */
661 acpigen_emit_byte(align & 0xff);
662 acpigen_emit_byte(len & 0xff);
663 return 8;
664}
665
666int acpigen_write_resourcetemplate_header(void)
667{
668 int len;
669 /*
670 * A ResourceTemplate() is a Buffer() with a
671 * (Byte|Word|DWord) containing the length, followed by one or more
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100672 * resource items, terminated by the end tag.
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000673 * (small item 0xf, len 1)
674 */
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100675 len = acpigen_emit_byte(0x11); /* Buffer opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000676 len += acpigen_write_len_f();
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100677 len += acpigen_emit_byte(0x0b); /* Word opcode */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000678 len_stack[ltop++] = acpigen_get_current();
679 len += acpigen_emit_byte(0x00);
680 len += acpigen_emit_byte(0x00);
681 return len;
682}
683
684int acpigen_write_resourcetemplate_footer(int len)
685{
686 char *p = len_stack[--ltop];
687 /*
688 * end tag (acpi 4.0 Section 6.4.2.8)
689 * 0x79 <checksum>
690 * 0x00 is treated as a good checksum according to the spec
691 * and is what iasl generates.
692 */
693 len += acpigen_emit_byte(0x79);
694 len += acpigen_emit_byte(0x00);
695 /* patch len word */
696 p[0] = (len-6) & 0xff;
697 p[1] = ((len-6) >> 8) & 0xff;
698 /* patch len field */
699 acpigen_patch_len(len-1);
700 return 2;
701}
702
703static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
704 struct resource *res)
705{
706 acpigen_write_mem32fixed(0, res->base, res->size);
707}
708
709static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
710 struct resource *res)
711{
712 resource_t base = res->base;
713 resource_t size = res->size;
714 while (size > 0) {
715 resource_t sz = size > 255 ? 255 : size;
716 acpigen_write_io16(base, base, 0, sz, 1);
717 size -= sz;
718 base += sz;
719 }
720}
721
722int acpigen_write_mainboard_resource_template(void)
723{
724 int len;
725 char *start;
726 char *end;
727 len = acpigen_write_resourcetemplate_header();
728 start = acpigen_get_current();
729
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100730 /* Add reserved memory ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000731 search_global_resources(
732 IORESOURCE_MEM | IORESOURCE_RESERVE,
733 IORESOURCE_MEM | IORESOURCE_RESERVE,
734 acpigen_add_mainboard_rsvd_mem32, 0);
735
Paul Menzel0f4c0e22013-02-22 12:33:08 +0100736 /* Add reserved io ranges. */
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000737 search_global_resources(
738 IORESOURCE_IO | IORESOURCE_RESERVE,
739 IORESOURCE_IO | IORESOURCE_RESERVE,
740 acpigen_add_mainboard_rsvd_io, 0);
741
742 end = acpigen_get_current();
743 len += end-start;
744 len += acpigen_write_resourcetemplate_footer(len);
745 return len;
746}
747
748int acpigen_write_mainboard_resources(const char *scope, const char *name)
749{
750 int len;
751 len = acpigen_write_scope(scope);
752 len += acpigen_write_name(name);
753 len += acpigen_write_mainboard_resource_template();
754 acpigen_patch_len(len - 1);
755 return len;
756}