3dec6714944130048342faea75049e78e54c01b2
[coreboot.git] / src / arch / i386 / boot / acpigen.c
1 /*
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
32 static char *gencurrent;
33
34 char *len_stack[ACPIGEN_LENSTACK_SIZE];
35 int ltop = 0;
36
37 static int acpigen_write_len_f(void)
38 {
39         ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1))
40             len_stack[ltop++] = gencurrent;
41         acpigen_emit_byte(0);
42         acpigen_emit_byte(0);
43         return 2;
44 }
45
46 void acpigen_patch_len(int len)
47 {
48         ASSERT(len <= ACPIGEN_MAXLEN)
49             ASSERT(ltop > 0)
50         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
57 void acpigen_set_current(char *curr)
58 {
59         gencurrent = curr;
60 }
61
62 char *acpigen_get_current(void)
63 {
64         return gencurrent;
65 }
66
67 int acpigen_emit_byte(unsigned char b)
68 {
69         (*gencurrent++) = b;
70         return 1;
71 }
72
73 int 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
83 int 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
91 int 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
102 int 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
117 int acpigen_write_name_byte(char *name, uint8_t val)
118 {
119         int len;
120         len = acpigen_write_name(name);
121         len += acpigen_write_byte(val);
122         return len;
123 }
124
125 int acpigen_write_name_dword(char *name, uint32_t val)
126 {
127         int len;
128         len = acpigen_write_name(name);
129         len += acpigen_write_dword(val);
130         return len;
131 }
132
133 int acpigen_write_name_qword(char *name, uint64_t val)
134 {
135         int len;
136         len = acpigen_write_name(name);
137         len += acpigen_write_qword(val);
138         return len;
139 }
140
141 int acpigen_emit_stream(char *data, int size)
142 {
143         int i;
144         for (i = 0; i < size; i++) {
145                 acpigen_emit_byte(data[i]);
146         }
147         return size;
148 }
149
150 int acpigen_write_name(char *name)
151 {
152         int len = strlen(name);
153         /* name op */
154         acpigen_emit_byte(0x8);
155         acpigen_emit_stream(name, len);
156         return len + 1;
157 }
158
159 int acpigen_write_scope(char *name)
160 {
161         int len;
162         /* scope op */
163         acpigen_emit_byte(0x10);
164         len = acpigen_write_len_f();
165         return len + acpigen_emit_stream(name, strlen(name)) + 1;
166 }
167
168 int acpigen_write_processor(u8 cpuindex, u32 pblock_addr, u8 pblock_len)
169 {
170 /*
171         Processor (\_PR.CPUcpuindex, cpuindex, pblock_addr, pblock_len)
172         {
173 */
174         char pscope[16];
175         int len;
176         /* processor op */
177         acpigen_emit_byte(0x5b);
178         acpigen_emit_byte(0x83);
179         len = acpigen_write_len_f();
180
181         sprintf(pscope, "\\._PR_CPU%x", (unsigned int) cpuindex);
182         len += acpigen_emit_stream(pscope, strlen(pscope));
183         acpigen_emit_byte(cpuindex);
184         acpigen_emit_byte(pblock_addr & 0xff);
185         acpigen_emit_byte((pblock_addr >> 8) & 0xff);
186         acpigen_emit_byte((pblock_addr >> 16) & 0xff);
187         acpigen_emit_byte((pblock_addr >> 24) & 0xff);
188         acpigen_emit_byte(pblock_len);
189         return 6 + 2 + len;
190 }
191
192 int acpigen_write_empty_PCT(void)
193 {
194 /*
195     Name (_PCT, Package (0x02)
196     {
197         ResourceTemplate ()
198         {
199             Register (FFixedHW,
200                 0x00,               // Bit Width
201                 0x00,               // Bit Offset
202                 0x0000000000000000, // Address
203                 ,)
204         },
205
206         ResourceTemplate ()
207         {
208             Register (FFixedHW,
209                 0x00,               // Bit Width
210                 0x00,               // Bit Offset
211                 0x0000000000000000, // Address
212                 ,)
213         }
214     })
215 */
216         static char stream[] = {
217                 0x08, 0x5F, 0x50, 0x43, 0x54, 0x12, 0x2C,       /* 00000030    "0._PCT.," */
218                 0x02, 0x11, 0x14, 0x0A, 0x11, 0x82, 0x0C, 0x00, /* 00000038    "........" */
219                 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000040    "........" */
220                 0x00, 0x00, 0x00, 0x00, 0x79, 0x00, 0x11, 0x14, /* 00000048    "....y..." */
221                 0x0A, 0x11, 0x82, 0x0C, 0x00, 0x7F, 0x00, 0x00, /* 00000050    "........" */
222                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000058    "........" */
223                 0x00, 0x79, 0x00
224         };
225         return acpigen_emit_stream(stream, ARRAY_SIZE(stream));
226 }
227
228 /* generates a func with max supported P states */
229 int acpigen_write_PPC(u8 nr)
230 {
231 /*
232     Method (_PPC, 0, NotSerialized)
233     {
234         Return (nr)
235     }
236 */
237         int len;
238         /* method op */
239         acpigen_emit_byte(0x14);
240         len = acpigen_write_len_f();
241         len += acpigen_emit_stream("_PPC", 4);
242         /* no fnarg */
243         acpigen_emit_byte(0x00);
244         /* return */
245         acpigen_emit_byte(0xa4);
246         /* arg */
247         len += acpigen_write_byte(nr);
248         /* add all single bytes */
249         len += 3;
250         acpigen_patch_len(len - 1);
251         return len;
252 }
253
254 int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat,
255                               u32 busmLat, u32 control, u32 status)
256 {
257         int len;
258         len = acpigen_write_package(6);
259         len += acpigen_write_dword(coreFreq);
260         len += acpigen_write_dword(power);
261         len += acpigen_write_dword(transLat);
262         len += acpigen_write_dword(busmLat);
263         len += acpigen_write_dword(control);
264         len += acpigen_write_dword(status);
265         //pkglen without the len opcode
266         acpigen_patch_len(len - 1);
267         return len;
268 }
269
270 int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype)
271 {
272         int len, lenh, lenp;
273         lenh = acpigen_write_name("_PSD");
274         lenp = acpigen_write_package(1);
275         len = acpigen_write_package(5);
276         len += acpigen_write_byte(5);   // 5 values
277         len += acpigen_write_byte(0);   // revision 0
278         len += acpigen_write_dword(domain);
279         len += acpigen_write_dword(coordtype);
280         len += acpigen_write_dword(numprocs);
281         acpigen_patch_len(len - 1);
282         len += lenp;
283         acpigen_patch_len(len - 1);
284         return len + lenh;
285 }