blob: 2ad2a92cde5d0cd1027a6196fc83c89d3cebdb9b [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
7 * it under the terms of the GNU General Public License v2 as published by
8 * the Free Software Foundation.
9 *
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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/* how many nesting we support */
21#define ACPIGEN_LENSTACK_SIZE 10
22
23/* if you need to change this, change the acpigen_write_f and
24 acpigen_patch_len */
25
26#define ACPIGEN_MAXLEN 0xfff
27
28#include <string.h>
29#include <arch/acpigen.h>
30#include <console/console.h>
31
32static char *gencurrent;
33
34char *len_stack[ACPIGEN_LENSTACK_SIZE];
35int ltop = 0;
36
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000037static int acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000038{
39 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000040 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000041 acpigen_emit_byte(0);
42 acpigen_emit_byte(0);
43 return 2;
44}
45
46void acpigen_patch_len(int len)
47{
48 ASSERT(len <= ACPIGEN_MAXLEN)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000049 ASSERT(ltop > 0)
Rudolf Marek293b5f52009-02-01 18:35:15 +000050 char *p = len_stack[--ltop];
51 /* generate store length for 0xfff max */
52 p[0] = (0x40 | (len & 0xf));
53 p[1] = (len >> 4 & 0xff);
54
55}
56
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000057void acpigen_set_current(char *curr)
58{
59 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000060}
61
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000062char *acpigen_get_current(void)
63{
64 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000065}
66
67int acpigen_emit_byte(unsigned char b)
68{
69 (*gencurrent++) = b;
70 return 1;
71}
72
73int acpigen_write_package(int nr_el)
74{
75 int len;
76 /* package op */
77 acpigen_emit_byte(0x12);
78 len = acpigen_write_len_f();
79 acpigen_emit_byte(nr_el);
80 return len + 2;
81}
82
83int acpigen_write_byte(unsigned int data)
84{
85 /* byte op */
86 acpigen_emit_byte(0xa);
87 acpigen_emit_byte(data & 0xff);
88 return 2;
89}
90
91int acpigen_write_dword(unsigned int data)
92{
93 /* dword op */
94 acpigen_emit_byte(0xc);
95 acpigen_emit_byte(data & 0xff);
96 acpigen_emit_byte((data >> 8) & 0xff);
97 acpigen_emit_byte((data >> 16) & 0xff);
98 acpigen_emit_byte((data >> 24) & 0xff);
99 return 5;
100}
101
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000102int acpigen_write_qword(uint64_t data)
103{
104 /* qword op */
105 acpigen_emit_byte(0xe);
106 acpigen_emit_byte(data & 0xff);
107 acpigen_emit_byte((data >> 8) & 0xff);
108 acpigen_emit_byte((data >> 16) & 0xff);
109 acpigen_emit_byte((data >> 24) & 0xff);
110 acpigen_emit_byte((data >> 32) & 0xff);
111 acpigen_emit_byte((data >> 40) & 0xff);
112 acpigen_emit_byte((data >> 48) & 0xff);
113 acpigen_emit_byte((data >> 56) & 0xff);
114 return 9;
115}
116
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000117int acpigen_write_name_byte(char *name, uint8_t val)
118{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000119 int len;
120 len = acpigen_write_name(name);
121 len += acpigen_write_byte(val);
122 return len;
123}
124
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000125int acpigen_write_name_dword(char *name, uint32_t val)
126{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000127 int len;
128 len = acpigen_write_name(name);
129 len += acpigen_write_dword(val);
130 return len;
131}
132
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000133int acpigen_write_name_qword(char *name, uint64_t val)
134{
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000135 int len;
136 len = acpigen_write_name(name);
137 len += acpigen_write_qword(val);
138 return len;
139}
140
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000141int acpigen_emit_stream(char *data, int size)
142{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000143 int i;
144 for (i = 0; i < size; i++) {
145 acpigen_emit_byte(data[i]);
146 }
147 return size;
148}
149
Rudolf Marek3310d752009-06-21 20:26:13 +0000150/* The NameString are bit tricky, each element can be 4 chars, if
151 less its padded with underscore. Check 18.2.2 and 18.4
152 and 5.3 of ACPI specs 3.0 for details
153*/
154
155static int acpigen_emit_simple_namestring(char *name) {
156 int i, len = 0;
157 char ud[] = "____";
158 for (i = 0; i < 4; i++) {
159 if ((name[i] == '\0') || (name[i] == '.')) {
160 len += acpigen_emit_stream(ud, 4 - i);
161 break;
162 } else {
163 len += acpigen_emit_byte(name[i]);
164 }
165 }
166 return len;
167}
168
169static int acpigen_emit_double_namestring(char *name, int dotpos) {
170 int len = 0;
171 /* mark dual name prefix */
172 len += acpigen_emit_byte(0x2e);
173 len += acpigen_emit_simple_namestring(name);
174 len += acpigen_emit_simple_namestring(&name[dotpos + 1]);
175 return len;
176}
177
178static int acpigen_emit_multi_namestring(char *name) {
179 int len = 0, count = 0;
180 unsigned char *pathlen;
181 /* mark multi name prefix */
182 len += acpigen_emit_byte(0x2f);
183 len += acpigen_emit_byte(0x0);
184 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
185
186 while (name[0] != '\0') {
187 len += acpigen_emit_simple_namestring(name);
188 /* find end or next entity */
189 while ((name[0] != '.') && (name[0] != '\0'))
190 name++;
191 /* forward to next */
192 if (name[0] == '.')
193 name++;
194 count++;
195 }
196
197 pathlen[0] = count;
198 return len;
199}
200
201
202int acpigen_emit_namestring(char *namepath) {
203 int dotcount = 0, i;
204 int dotpos;
205 int len = 0;
206
207 /* we can start with a \ */
208 if (namepath[0] == '\\') {
209 len += acpigen_emit_byte('\\');
210 namepath++;
211 }
212
213 /* and there can be any number of ^ */
214 while (namepath[0] == '^') {
215 len += acpigen_emit_byte('^');
216 namepath++;
217 }
218
219 ASSERT(namepath[0] != '\0');
220
221 i = 0;
222 while (namepath[i] != '\0') {
223 if (namepath[i] == '.') {
224 dotcount++;
225 dotpos = i;
226 }
227 i++;
228 }
229
230 if (dotcount == 0) {
231 len += acpigen_emit_simple_namestring(namepath);
232 } else if (dotcount == 1) {
233 len += acpigen_emit_double_namestring(namepath, dotpos);
234 } else {
235 len += acpigen_emit_multi_namestring(namepath);
236 }
237 return len;
238}
239
Rudolf Marek293b5f52009-02-01 18:35:15 +0000240int acpigen_write_name(char *name)
241{
Rudolf Marek3310d752009-06-21 20:26:13 +0000242 int len;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000243 /* name op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000244 len = acpigen_emit_byte(0x8);
245 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000246}
247
248int acpigen_write_scope(char *name)
249{
250 int len;
251 /* scope op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000252 len = acpigen_emit_byte(0x10);
253 len += acpigen_write_len_f();
254 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000255}
Rudolf Marekf997b552009-02-14 15:40:23 +0000256
257int acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
258{
259/*
260 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
261 {
262*/
263 char pscope[16];
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000264 int len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000265 /* processor op */
266 acpigen_emit_byte(0x5b);
267 acpigen_emit_byte(0x83);
268 len = acpigen_write_len_f();
269
Rudolf Marek3310d752009-06-21 20:26:13 +0000270 sprintf(pscope, "\\_PR.CPU%x", (unsigned int) cpuindex);
271 len += acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000272 acpigen_emit_byte(cpuindex);
273 acpigen_emit_byte(pblock_addr & 0xff);
274 acpigen_emit_byte((pblock_addr >> 8) & 0xff);
275 acpigen_emit_byte((pblock_addr >> 16) & 0xff);
276 acpigen_emit_byte((pblock_addr >> 24) & 0xff);
277 acpigen_emit_byte(pblock_len);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000278 return 6 + 2 + len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000279}
280
281int acpigen_write_empty_PCT(void)
282{
283/*
284 Name (_PCT, Package (0x02)
285 {
286 ResourceTemplate ()
287 {
288 Register (FFixedHW,
289 0x00, // Bit Width
290 0x00, // Bit Offset
291 0x0000000000000000, // Address
292 ,)
293 },
294
295 ResourceTemplate ()
296 {
297 Register (FFixedHW,
298 0x00, // Bit Width
299 0x00, // Bit Offset
300 0x0000000000000000, // Address
301 ,)
302 }
303 })
304*/
305 static char stream[] = {
306 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
307 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
308 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
309 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
310 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
311 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
312 0x00, 0x79, 0x00
313 };
314 return acpigen_emit_stream(stream, ARRAY_SIZE(stream));
315}
316
317/* generates a func with max supported P states */
318int acpigen_write_PPC(u8 nr)
319{
320/*
321 Method (_PPC, 0, NotSerialized)
322 {
323 Return (nr)
324 }
325*/
326 int len;
327 /* method op */
328 acpigen_emit_byte(0x14);
329 len = acpigen_write_len_f();
Rudolf Marek3310d752009-06-21 20:26:13 +0000330 len += acpigen_emit_namestring("_PPC");
Rudolf Marekf997b552009-02-14 15:40:23 +0000331 /* no fnarg */
332 acpigen_emit_byte(0x00);
333 /* return */
334 acpigen_emit_byte(0xa4);
335 /* arg */
336 len += acpigen_write_byte(nr);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000337 /* add all single bytes */
338 len += 3;
Rudolf Marekf997b552009-02-14 15:40:23 +0000339 acpigen_patch_len(len - 1);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000340 return len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000341}
342
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000343int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
344 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000345{
346 int len;
347 len = acpigen_write_package(6);
348 len += acpigen_write_dword(coreFreq);
349 len += acpigen_write_dword(power);
350 len += acpigen_write_dword(transLat);
351 len += acpigen_write_dword(busmLat);
352 len += acpigen_write_dword(control);
353 len += acpigen_write_dword(status);
354 //pkglen without the len opcode
355 acpigen_patch_len(len - 1);
356 return len;
357}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000358
359int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
360{
361 int len, lenh, lenp;
362 lenh = acpigen_write_name("_PSD");
363 lenp = acpigen_write_package(1);
364 len = acpigen_write_package(5);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000365 len += acpigen_write_byte(5); // 5 values
366 len += acpigen_write_byte(0); // revision 0
Patrick Georgidf444bf2009-04-21 20:34:36 +0000367 len += acpigen_write_dword(domain);
368 len += acpigen_write_dword(coordtype);
369 len += acpigen_write_dword(numprocs);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000370 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000371 len += lenp;
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000372 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000373 return len + lenh;
374}