blob: 47845a01a670cdf0ebc646b7fecce2c53d517d18 [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
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>
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +000031#include <device/device.h>
Rudolf Marek293b5f52009-02-01 18:35:15 +000032
33static char *gencurrent;
34
35char *len_stack[ACPIGEN_LENSTACK_SIZE];
36int ltop = 0;
37
Stefan Reinauer5b73d4d2012-04-27 21:55:05 +020038int acpigen_write_len_f(void)
Rudolf Marek293b5f52009-02-01 18:35:15 +000039{
40 ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000041 len_stack[ltop++] = gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000042 acpigen_emit_byte(0);
43 acpigen_emit_byte(0);
44 return 2;
45}
46
47void acpigen_patch_len(int len)
48{
49 ASSERT(len <= ACPIGEN_MAXLEN)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000050 ASSERT(ltop > 0)
Rudolf Marek293b5f52009-02-01 18:35:15 +000051 char *p = len_stack[--ltop];
52 /* generate store length for 0xfff max */
53 p[0] = (0x40 | (len & 0xf));
54 p[1] = (len >> 4 & 0xff);
55
56}
57
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000058void acpigen_set_current(char *curr)
59{
60 gencurrent = curr;
Rudolf Marek293b5f52009-02-01 18:35:15 +000061}
62
Stefan Reinauerf96c2d92009-04-22 16:23:47 +000063char *acpigen_get_current(void)
64{
65 return gencurrent;
Rudolf Marek293b5f52009-02-01 18:35:15 +000066}
67
68int acpigen_emit_byte(unsigned char b)
69{
70 (*gencurrent++) = b;
71 return 1;
72}
73
74int acpigen_write_package(int nr_el)
75{
76 int len;
77 /* package op */
78 acpigen_emit_byte(0x12);
79 len = acpigen_write_len_f();
80 acpigen_emit_byte(nr_el);
81 return len + 2;
82}
83
84int acpigen_write_byte(unsigned int data)
85{
86 /* byte op */
87 acpigen_emit_byte(0xa);
88 acpigen_emit_byte(data & 0xff);
89 return 2;
90}
91
92int acpigen_write_dword(unsigned int data)
93{
94 /* dword op */
95 acpigen_emit_byte(0xc);
96 acpigen_emit_byte(data & 0xff);
97 acpigen_emit_byte((data >> 8) & 0xff);
98 acpigen_emit_byte((data >> 16) & 0xff);
99 acpigen_emit_byte((data >> 24) & 0xff);
100 return 5;
101}
102
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000103int acpigen_write_qword(uint64_t data)
104{
105 /* qword op */
106 acpigen_emit_byte(0xe);
107 acpigen_emit_byte(data & 0xff);
108 acpigen_emit_byte((data >> 8) & 0xff);
109 acpigen_emit_byte((data >> 16) & 0xff);
110 acpigen_emit_byte((data >> 24) & 0xff);
111 acpigen_emit_byte((data >> 32) & 0xff);
112 acpigen_emit_byte((data >> 40) & 0xff);
113 acpigen_emit_byte((data >> 48) & 0xff);
114 acpigen_emit_byte((data >> 56) & 0xff);
115 return 9;
116}
117
Myles Watson3fe6b702009-10-09 20:13:43 +0000118int acpigen_write_name_byte(const char *name, uint8_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000119{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000120 int len;
121 len = acpigen_write_name(name);
122 len += acpigen_write_byte(val);
123 return len;
124}
125
Myles Watson3fe6b702009-10-09 20:13:43 +0000126int acpigen_write_name_dword(const char *name, uint32_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000127{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000128 int len;
129 len = acpigen_write_name(name);
130 len += acpigen_write_dword(val);
131 return len;
132}
133
Myles Watson3fe6b702009-10-09 20:13:43 +0000134int acpigen_write_name_qword(const char *name, uint64_t val)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000135{
Carl-Daniel Hailfingerd58671c2009-02-17 21:38:51 +0000136 int len;
137 len = acpigen_write_name(name);
138 len += acpigen_write_qword(val);
139 return len;
140}
141
Myles Watson3fe6b702009-10-09 20:13:43 +0000142int acpigen_emit_stream(const char *data, int size)
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000143{
Rudolf Marek293b5f52009-02-01 18:35:15 +0000144 int i;
145 for (i = 0; i < size; i++) {
146 acpigen_emit_byte(data[i]);
147 }
148 return size;
149}
150
Stefan Reinauer14e22772010-04-27 06:56:47 +0000151/* The NameString are bit tricky, each element can be 4 chars, if
152 less its padded with underscore. Check 18.2.2 and 18.4
Rudolf Marek3310d752009-06-21 20:26:13 +0000153 and 5.3 of ACPI specs 3.0 for details
154*/
155
Myles Watson3fe6b702009-10-09 20:13:43 +0000156static int acpigen_emit_simple_namestring(const char *name) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000157 int i, len = 0;
158 char ud[] = "____";
159 for (i = 0; i < 4; i++) {
160 if ((name[i] == '\0') || (name[i] == '.')) {
161 len += acpigen_emit_stream(ud, 4 - i);
162 break;
163 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000164 len += acpigen_emit_byte(name[i]);
Rudolf Marek3310d752009-06-21 20:26:13 +0000165 }
166 }
167 return len;
168}
169
Myles Watson3fe6b702009-10-09 20:13:43 +0000170static int acpigen_emit_double_namestring(const char *name, int dotpos) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000171 int len = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000172 /* mark dual name prefix */
173 len += acpigen_emit_byte(0x2e);
174 len += acpigen_emit_simple_namestring(name);
175 len += acpigen_emit_simple_namestring(&name[dotpos + 1]);
176 return len;
177}
178
Myles Watson3fe6b702009-10-09 20:13:43 +0000179static int acpigen_emit_multi_namestring(const char *name) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000180 int len = 0, count = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000181 unsigned char *pathlen;
Rudolf Marek3310d752009-06-21 20:26:13 +0000182 /* mark multi name prefix */
183 len += acpigen_emit_byte(0x2f);
184 len += acpigen_emit_byte(0x0);
185 pathlen = ((unsigned char *) acpigen_get_current()) - 1;
186
187 while (name[0] != '\0') {
188 len += acpigen_emit_simple_namestring(name);
189 /* find end or next entity */
190 while ((name[0] != '.') && (name[0] != '\0'))
191 name++;
192 /* forward to next */
193 if (name[0] == '.')
194 name++;
195 count++;
196 }
197
198 pathlen[0] = count;
199 return len;
200}
201
202
Myles Watson3fe6b702009-10-09 20:13:43 +0000203int acpigen_emit_namestring(const char *namepath) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000204 int dotcount = 0, i;
Myles Watson3fe6b702009-10-09 20:13:43 +0000205 int dotpos = 0;
Rudolf Marek3310d752009-06-21 20:26:13 +0000206 int len = 0;
207
208 /* we can start with a \ */
209 if (namepath[0] == '\\') {
210 len += acpigen_emit_byte('\\');
211 namepath++;
212 }
213
214 /* and there can be any number of ^ */
215 while (namepath[0] == '^') {
216 len += acpigen_emit_byte('^');
217 namepath++;
218 }
219
220 ASSERT(namepath[0] != '\0');
221
222 i = 0;
223 while (namepath[i] != '\0') {
224 if (namepath[i] == '.') {
225 dotcount++;
226 dotpos = i;
227 }
228 i++;
229 }
230
231 if (dotcount == 0) {
232 len += acpigen_emit_simple_namestring(namepath);
Stefan Reinauer14e22772010-04-27 06:56:47 +0000233 } else if (dotcount == 1) {
Rudolf Marek3310d752009-06-21 20:26:13 +0000234 len += acpigen_emit_double_namestring(namepath, dotpos);
235 } else {
236 len += acpigen_emit_multi_namestring(namepath);
237 }
238 return len;
239}
240
Myles Watson3fe6b702009-10-09 20:13:43 +0000241int acpigen_write_name(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000242{
Rudolf Marek3310d752009-06-21 20:26:13 +0000243 int len;
Rudolf Marek293b5f52009-02-01 18:35:15 +0000244 /* name op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000245 len = acpigen_emit_byte(0x8);
246 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000247}
248
Myles Watson3fe6b702009-10-09 20:13:43 +0000249int acpigen_write_scope(const char *name)
Rudolf Marek293b5f52009-02-01 18:35:15 +0000250{
251 int len;
252 /* scope op */
Rudolf Marek3310d752009-06-21 20:26:13 +0000253 len = acpigen_emit_byte(0x10);
254 len += acpigen_write_len_f();
255 return len + acpigen_emit_namestring(name);
Rudolf Marek293b5f52009-02-01 18:35:15 +0000256}
Rudolf Marekf997b552009-02-14 15:40:23 +0000257
258int acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
259{
260/*
261 Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
262 {
263*/
264 char pscope[16];
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000265 int len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000266 /* processor op */
267 acpigen_emit_byte(0x5b);
268 acpigen_emit_byte(0x83);
269 len = acpigen_write_len_f();
270
Rudolf Marek3310d752009-06-21 20:26:13 +0000271 sprintf(pscope, "\\_PR.CPU%x", (unsigned int) cpuindex);
272 len += acpigen_emit_namestring(pscope);
Rudolf Marekf997b552009-02-14 15:40:23 +0000273 acpigen_emit_byte(cpuindex);
274 acpigen_emit_byte(pblock_addr & 0xff);
275 acpigen_emit_byte((pblock_addr >> 8) & 0xff);
276 acpigen_emit_byte((pblock_addr >> 16) & 0xff);
277 acpigen_emit_byte((pblock_addr >> 24) & 0xff);
278 acpigen_emit_byte(pblock_len);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000279 return 6 + 2 + len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000280}
281
282int acpigen_write_empty_PCT(void)
283{
284/*
285 Name (_PCT, Package (0x02)
286 {
287 ResourceTemplate ()
288 {
289 Register (FFixedHW,
290 0x00, // Bit Width
291 0x00, // Bit Offset
292 0x0000000000000000, // Address
293 ,)
294 },
295
296 ResourceTemplate ()
297 {
298 Register (FFixedHW,
299 0x00, // Bit Width
300 0x00, // Bit Offset
301 0x0000000000000000, // Address
302 ,)
303 }
304 })
305*/
306 static char stream[] = {
307 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C, /* 00000030 "0._PCT.," */
308 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038 "........" */
309 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040 "........" */
310 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048 "....y..." */
311 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050 "........" */
312 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058 "........" */
313 0x00, 0x79, 0x00
314 };
315 return acpigen_emit_stream(stream, ARRAY_SIZE(stream));
316}
317
Stefan Reinauer39205c62012-04-27 21:49:28 +0200318int acpigen_write_empty_PTC(void)
319{
320/*
321 Name (_PTC, Package (0x02)
322 {
323 ResourceTemplate ()
324 {
325 Register (FFixedHW,
326 0x00, // Bit Width
327 0x00, // Bit Offset
328 0x0000000000000000, // Address
329 ,)
330 },
331
332 ResourceTemplate ()
333 {
334 Register (FFixedHW,
335 0x00, // Bit Width
336 0x00, // Bit Offset
337 0x0000000000000000, // Address
338 ,)
339 }
340 })
341*/
342 int len, nlen, rlen;
343 acpi_addr_t addr = {
344 .space_id = ACPI_ADDRESS_SPACE_FIXED,
345 .bit_width = 0,
346 .bit_offset = 0,
zbaoee8a9f6c2012-05-29 14:59:38 +0800347 {
348 .resv = 0
349 },
Stefan Reinauer39205c62012-04-27 21:49:28 +0200350 .addrl = 0,
351 .addrh = 0,
352 };
353
354 nlen = acpigen_write_name("_PTC");
355 len = acpigen_write_package(2);
356
357 /* ControlRegister */
358 rlen = acpigen_write_resourcetemplate_header();
359 rlen += acpigen_write_register(&addr);
360 len += acpigen_write_resourcetemplate_footer(rlen);
361 len += rlen;
362
363 /* StatusRegister */
364 rlen = acpigen_write_resourcetemplate_header();
365 rlen += acpigen_write_register(&addr);
366 len += acpigen_write_resourcetemplate_footer(rlen);
367 len += rlen;
368
369 acpigen_patch_len(len - 1);
370 return len + nlen;
371}
372
Rudolf Marekf997b552009-02-14 15:40:23 +0000373/* generates a func with max supported P states */
374int acpigen_write_PPC(u8 nr)
375{
376/*
377 Method (_PPC, 0, NotSerialized)
378 {
379 Return (nr)
380 }
381*/
382 int len;
383 /* method op */
384 acpigen_emit_byte(0x14);
385 len = acpigen_write_len_f();
Rudolf Marek3310d752009-06-21 20:26:13 +0000386 len += acpigen_emit_namestring("_PPC");
Rudolf Marekf997b552009-02-14 15:40:23 +0000387 /* no fnarg */
388 acpigen_emit_byte(0x00);
389 /* return */
390 acpigen_emit_byte(0xa4);
391 /* arg */
392 len += acpigen_write_byte(nr);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000393 /* add all single bytes */
394 len += 3;
Rudolf Marekf997b552009-02-14 15:40:23 +0000395 acpigen_patch_len(len - 1);
Rudolf Marek6b2e7602009-03-02 22:45:31 +0000396 return len;
Rudolf Marekf997b552009-02-14 15:40:23 +0000397}
398
Duncan Laurie0eefa002012-07-16 12:11:53 -0700399/* generates a func with max supported P states */
400int acpigen_write_PPC_NVS(void)
401{
402/*
403 Method (_PPC, 0, NotSerialized)
404 {
405 Return (PPCM)
406 }
407*/
408 int len;
409 /* method op */
410 acpigen_emit_byte(0x14);
411 len = acpigen_write_len_f();
412 len += acpigen_emit_namestring("_PPC");
413 /* no fnarg */
414 acpigen_emit_byte(0x00);
415 /* return */
416 acpigen_emit_byte(0xa4);
417 /* arg */
418 len += acpigen_emit_namestring("PPCM");
419 /* add all single bytes */
420 len += 3;
421 acpigen_patch_len(len - 1);
422 return len;
423}
424
Stefan Reinauer39205c62012-04-27 21:49:28 +0200425int acpigen_write_TPC(const char *gnvs_tpc_limit)
426{
427/*
428 // Sample _TPC method
429 Method (_TPC, 0, NotSerialized)
430 {
431 Return (\TLVL)
432 }
433 */
434 int len;
435
436 len = acpigen_emit_byte(0x14); /* MethodOp */
437 len += acpigen_write_len_f(); /* PkgLength */
438 len += acpigen_emit_namestring("_TPC");
439 len += acpigen_emit_byte(0x00); /* No Arguments */
440 len += acpigen_emit_byte(0xa4); /* ReturnOp */
441 len += acpigen_emit_namestring(gnvs_tpc_limit);
442 acpigen_patch_len(len - 1);
443 return len;
444}
445
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000446int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
447 u32 busmLat, u32 control, u32 status)
Rudolf Marekf997b552009-02-14 15:40:23 +0000448{
449 int len;
450 len = acpigen_write_package(6);
451 len += acpigen_write_dword(coreFreq);
452 len += acpigen_write_dword(power);
453 len += acpigen_write_dword(transLat);
454 len += acpigen_write_dword(busmLat);
455 len += acpigen_write_dword(control);
456 len += acpigen_write_dword(status);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700457 // pkglen without the len opcode
Rudolf Marekf997b552009-02-14 15:40:23 +0000458 acpigen_patch_len(len - 1);
Stefan Reinauerd4bacf92012-05-02 16:38:47 -0700459
460 printk(BIOS_DEBUG, "PSS: %uMHz power %u control 0x%x status 0x%x\n",
461 coreFreq, power, control, status);
462
Rudolf Marekf997b552009-02-14 15:40:23 +0000463 return len;
464}
Patrick Georgidf444bf2009-04-21 20:34:36 +0000465
466int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
467{
468 int len, lenh, lenp;
469 lenh = acpigen_write_name("_PSD");
470 lenp = acpigen_write_package(1);
471 len = acpigen_write_package(5);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000472 len += acpigen_write_byte(5); // 5 values
473 len += acpigen_write_byte(0); // revision 0
Patrick Georgidf444bf2009-04-21 20:34:36 +0000474 len += acpigen_write_dword(domain);
475 len += acpigen_write_dword(coordtype);
476 len += acpigen_write_dword(numprocs);
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000477 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000478 len += lenp;
Stefan Reinauerf96c2d92009-04-22 16:23:47 +0000479 acpigen_patch_len(len - 1);
Patrick Georgidf444bf2009-04-21 20:34:36 +0000480 return len + lenh;
481}
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000482
Stefan Reinauerf69b4682012-04-27 23:12:08 +0200483int acpigen_write_CST_package_entry(acpi_cstate_t *cstate)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200484{
485 int len, len0;
486 char *start, *end;
487
488 len0 = acpigen_write_package(4);
489 len = acpigen_write_resourcetemplate_header();
490 start = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200491 acpigen_write_register(&cstate->resource);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200492 end = acpigen_get_current();
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200493 len += end - start;
Sven Schnelle0b86c762011-10-21 21:46:47 +0200494 len += acpigen_write_resourcetemplate_footer(len);
495 len += len0;
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200496 len += acpigen_write_dword(cstate->ctype);
497 len += acpigen_write_dword(cstate->latency);
498 len += acpigen_write_dword(cstate->power);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200499 acpigen_patch_len(len - 1);
500 return len;
501}
502
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200503int acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200504{
505 int len, lenh, lenp, i;
506 lenh = acpigen_write_name("_CST");
507 lenp = acpigen_write_package(nentries+1);
508 len = acpigen_write_dword(nentries);
509
510 for (i = 0; i < nentries; i++)
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200511 len += acpigen_write_CST_package_entry(cstate + i);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200512
513 len += lenp;
514 acpigen_patch_len(len - 1);
515 return len + lenh;
516}
517
Stefan Reinauer39205c62012-04-27 21:49:28 +0200518int acpigen_write_TSS_package(int entries, acpi_tstate_t *tstate_list)
519{
520/*
521 Sample _TSS package with 100% and 50% duty cycles
522 Name (_TSS, Package (0x02)
523 {
524 Package(){100, 1000, 0, 0x00, 0)
525 Package(){50, 520, 0, 0x18, 0)
526 })
527 */
528 int i, len, plen, nlen;
529 acpi_tstate_t *tstate = tstate_list;
530
531 nlen = acpigen_write_name("_TSS");
532 plen = acpigen_write_package(entries);
533
534 for (i = 0; i < entries; i++) {
535 len = acpigen_write_package(5);
536 len += acpigen_write_dword(tstate->percent);
537 len += acpigen_write_dword(tstate->power);
538 len += acpigen_write_dword(tstate->latency);
539 len += acpigen_write_dword(tstate->control);
540 len += acpigen_write_dword(tstate->status);
541 acpigen_patch_len(len - 1);
542 tstate++;
543 plen += len;
544 }
545
546 acpigen_patch_len(plen - 1);
547 return plen + nlen;
548}
549
550int acpigen_write_TSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
551{
552 int len, lenh, lenp;
553 lenh = acpigen_write_name("_TSD");
554 lenp = acpigen_write_package(1);
555 len = acpigen_write_package(5);
556 len += acpigen_write_byte(5); // 5 values
557 len += acpigen_write_byte(0); // revision 0
558 len += acpigen_write_dword(domain);
559 len += acpigen_write_dword(coordtype);
560 len += acpigen_write_dword(numprocs);
561 acpigen_patch_len(len - 1);
562 len += lenp;
563 acpigen_patch_len(len - 1);
564 return len + lenh;
565}
566
567
568
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000569int acpigen_write_mem32fixed(int readwrite, u32 base, u32 size)
570{
571 /*
572 * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor
573 * Byte 0:
574 * Bit7 : 1 => big item
575 * Bit6-0: 0000110 (0x6) => 32-bit fixed memory
576 */
577 acpigen_emit_byte(0x86);
578 /* Byte 1+2: length (0x0009) */
579 acpigen_emit_byte(0x09);
580 acpigen_emit_byte(0x00);
581 /* bit1-7 are ignored */
582 acpigen_emit_byte(readwrite ? 0x01 : 0x00);
583 acpigen_emit_byte(base & 0xff);
584 acpigen_emit_byte((base >> 8) & 0xff);
585 acpigen_emit_byte((base >> 16) & 0xff);
586 acpigen_emit_byte((base >> 24) & 0xff);
587 acpigen_emit_byte(size & 0xff);
588 acpigen_emit_byte((size >> 8) & 0xff);
589 acpigen_emit_byte((size >> 16) & 0xff);
590 acpigen_emit_byte((size >> 24) & 0xff);
591 return 12;
592}
593
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200594int acpigen_write_register(acpi_addr_t *addr)
Sven Schnelle0b86c762011-10-21 21:46:47 +0200595{
Stefan Reinauer4cc8c702012-04-27 21:34:16 +0200596 acpigen_emit_byte(0x82); /* Register Descriptor */
597 acpigen_emit_byte(0x0c); /* Register Length 7:0 */
598 acpigen_emit_byte(0x00); /* Register Length 15:8 */
599 acpigen_emit_byte(addr->space_id); /* Address Space ID */
600 acpigen_emit_byte(addr->bit_width); /* Register Bit Width */
601 acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */
602 acpigen_emit_byte(addr->resv); /* Register Access Size */
603 acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */
604 acpigen_emit_byte((addr->addrl >> 8) & 0xff);
605 acpigen_emit_byte((addr->addrl >> 16) & 0xff);
606 acpigen_emit_byte((addr->addrl >> 24) & 0xff);
607 acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */
608 acpigen_emit_byte((addr->addrh >> 8) & 0xff);
609 acpigen_emit_byte((addr->addrh >> 16) & 0xff);
610 acpigen_emit_byte((addr->addrh >> 24) & 0xff);
Sven Schnelle0b86c762011-10-21 21:46:47 +0200611 return 15;
612}
613
Tobias Diedrich0fe6e9a2010-11-17 16:27:06 +0000614int acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16)
615{
616 /*
617 * acpi 4.0 section 6.4.2.6: I/O Port Descriptor
618 * Byte 0:
619 * Bit7 : 0 => small item
620 * Bit6-3: 1000 (0x8) => I/O port descriptor
621 * Bit2-0: 111 (0x7) => 7 Bytes long
622 */
623 acpigen_emit_byte(0x47);
624 /* does the device decode all 16 or just 10 bits? */
625 /* bit1-7 are ignored */
626 acpigen_emit_byte(decode16 ? 0x01 : 0x00);
627 /* minimum base address the device may be configured for */
628 acpigen_emit_byte(min & 0xff);
629 acpigen_emit_byte((min >> 8) & 0xff);
630 /* maximum base address the device may be configured for */
631 acpigen_emit_byte(max & 0xff);
632 acpigen_emit_byte((max >> 8) & 0xff);
633 /* alignment for min base */
634 acpigen_emit_byte(align & 0xff);
635 acpigen_emit_byte(len & 0xff);
636 return 8;
637}
638
639int acpigen_write_resourcetemplate_header(void)
640{
641 int len;
642 /*
643 * A ResourceTemplate() is a Buffer() with a
644 * (Byte|Word|DWord) containing the length, followed by one or more
645 * resource items, terminated by the end tag
646 * (small item 0xf, len 1)
647 */
648 len = acpigen_emit_byte(0x11); /* Buffer opcode */
649 len += acpigen_write_len_f();
650 len += acpigen_emit_byte(0x0b); /* Word opcode */
651 len_stack[ltop++] = acpigen_get_current();
652 len += acpigen_emit_byte(0x00);
653 len += acpigen_emit_byte(0x00);
654 return len;
655}
656
657int acpigen_write_resourcetemplate_footer(int len)
658{
659 char *p = len_stack[--ltop];
660 /*
661 * end tag (acpi 4.0 Section 6.4.2.8)
662 * 0x79 <checksum>
663 * 0x00 is treated as a good checksum according to the spec
664 * and is what iasl generates.
665 */
666 len += acpigen_emit_byte(0x79);
667 len += acpigen_emit_byte(0x00);
668 /* patch len word */
669 p[0] = (len-6) & 0xff;
670 p[1] = ((len-6) >> 8) & 0xff;
671 /* patch len field */
672 acpigen_patch_len(len-1);
673 return 2;
674}
675
676static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev,
677 struct resource *res)
678{
679 acpigen_write_mem32fixed(0, res->base, res->size);
680}
681
682static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev,
683 struct resource *res)
684{
685 resource_t base = res->base;
686 resource_t size = res->size;
687 while (size > 0) {
688 resource_t sz = size > 255 ? 255 : size;
689 acpigen_write_io16(base, base, 0, sz, 1);
690 size -= sz;
691 base += sz;
692 }
693}
694
695int acpigen_write_mainboard_resource_template(void)
696{
697 int len;
698 char *start;
699 char *end;
700 len = acpigen_write_resourcetemplate_header();
701 start = acpigen_get_current();
702
703 /* Add reserved memory ranges */
704 search_global_resources(
705 IORESOURCE_MEM | IORESOURCE_RESERVE,
706 IORESOURCE_MEM | IORESOURCE_RESERVE,
707 acpigen_add_mainboard_rsvd_mem32, 0);
708
709 /* Add reserved io ranges */
710 search_global_resources(
711 IORESOURCE_IO | IORESOURCE_RESERVE,
712 IORESOURCE_IO | IORESOURCE_RESERVE,
713 acpigen_add_mainboard_rsvd_io, 0);
714
715 end = acpigen_get_current();
716 len += end-start;
717 len += acpigen_write_resourcetemplate_footer(len);
718 return len;
719}
720
721int acpigen_write_mainboard_resources(const char *scope, const char *name)
722{
723 int len;
724 len = acpigen_write_scope(scope);
725 len += acpigen_write_name(name);
726 len += acpigen_write_mainboard_resource_template();
727 acpigen_patch_len(len - 1);
728 return len;
729}