Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 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 |
Uwe Hermann | c70e9fc | 2010-02-15 23:10:19 +0000 | [diff] [blame] | 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 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> |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 31 | #include <device/device.h> |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 32 | |
| 33 | static char *gencurrent; |
| 34 | |
| 35 | char *len_stack[ACPIGEN_LENSTACK_SIZE]; |
| 36 | int ltop = 0; |
| 37 | |
Stefan Reinauer | 5b73d4d | 2012-04-27 21:55:05 +0200 | [diff] [blame] | 38 | int acpigen_write_len_f(void) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 39 | { |
| 40 | ASSERT(ltop < (ACPIGEN_LENSTACK_SIZE - 1)) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 41 | len_stack[ltop++] = gencurrent; |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 42 | acpigen_emit_byte(0); |
| 43 | acpigen_emit_byte(0); |
| 44 | return 2; |
| 45 | } |
| 46 | |
| 47 | void acpigen_patch_len(int len) |
| 48 | { |
| 49 | ASSERT(len <= ACPIGEN_MAXLEN) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 50 | ASSERT(ltop > 0) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 51 | 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 Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 58 | void acpigen_set_current(char *curr) |
| 59 | { |
| 60 | gencurrent = curr; |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 63 | char *acpigen_get_current(void) |
| 64 | { |
| 65 | return gencurrent; |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int acpigen_emit_byte(unsigned char b) |
| 69 | { |
| 70 | (*gencurrent++) = b; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | int 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 | |
| 84 | int 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 | |
| 92 | int 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 Hailfinger | d58671c | 2009-02-17 21:38:51 +0000 | [diff] [blame] | 103 | int 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 Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 118 | int acpigen_write_name_byte(const char *name, uint8_t val) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 119 | { |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 120 | int len; |
| 121 | len = acpigen_write_name(name); |
| 122 | len += acpigen_write_byte(val); |
| 123 | return len; |
| 124 | } |
| 125 | |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 126 | int acpigen_write_name_dword(const char *name, uint32_t val) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 127 | { |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 128 | int len; |
| 129 | len = acpigen_write_name(name); |
| 130 | len += acpigen_write_dword(val); |
| 131 | return len; |
| 132 | } |
| 133 | |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 134 | int acpigen_write_name_qword(const char *name, uint64_t val) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 135 | { |
Carl-Daniel Hailfinger | d58671c | 2009-02-17 21:38:51 +0000 | [diff] [blame] | 136 | int len; |
| 137 | len = acpigen_write_name(name); |
| 138 | len += acpigen_write_qword(val); |
| 139 | return len; |
| 140 | } |
| 141 | |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 142 | int acpigen_emit_stream(const char *data, int size) |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 143 | { |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 144 | int i; |
| 145 | for (i = 0; i < size; i++) { |
| 146 | acpigen_emit_byte(data[i]); |
| 147 | } |
| 148 | return size; |
| 149 | } |
| 150 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 151 | /* 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 Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 153 | and 5.3 of ACPI specs 3.0 for details |
| 154 | */ |
| 155 | |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 156 | static int acpigen_emit_simple_namestring(const char *name) { |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 157 | 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 Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 164 | len += acpigen_emit_byte(name[i]); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | return len; |
| 168 | } |
| 169 | |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 170 | static int acpigen_emit_double_namestring(const char *name, int dotpos) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 171 | int len = 0; |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 172 | /* 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 Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 179 | static int acpigen_emit_multi_namestring(const char *name) { |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 180 | int len = 0, count = 0; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 181 | unsigned char *pathlen; |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 182 | /* 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 Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 203 | int acpigen_emit_namestring(const char *namepath) { |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 204 | int dotcount = 0, i; |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 205 | int dotpos = 0; |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 206 | 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 Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 233 | } else if (dotcount == 1) { |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 234 | len += acpigen_emit_double_namestring(namepath, dotpos); |
| 235 | } else { |
| 236 | len += acpigen_emit_multi_namestring(namepath); |
| 237 | } |
| 238 | return len; |
| 239 | } |
| 240 | |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 241 | int acpigen_write_name(const char *name) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 242 | { |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 243 | int len; |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 244 | /* name op */ |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 245 | len = acpigen_emit_byte(0x8); |
| 246 | return len + acpigen_emit_namestring(name); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Myles Watson | 3fe6b70 | 2009-10-09 20:13:43 +0000 | [diff] [blame] | 249 | int acpigen_write_scope(const char *name) |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 250 | { |
| 251 | int len; |
| 252 | /* scope op */ |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 253 | len = acpigen_emit_byte(0x10); |
| 254 | len += acpigen_write_len_f(); |
| 255 | return len + acpigen_emit_namestring(name); |
Rudolf Marek | 293b5f5 | 2009-02-01 18:35:15 +0000 | [diff] [blame] | 256 | } |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 257 | |
| 258 | int 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 Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 265 | int len; |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 266 | /* processor op */ |
| 267 | acpigen_emit_byte(0x5b); |
| 268 | acpigen_emit_byte(0x83); |
| 269 | len = acpigen_write_len_f(); |
| 270 | |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 271 | sprintf(pscope, "\\_PR.CPU%x", (unsigned int) cpuindex); |
| 272 | len += acpigen_emit_namestring(pscope); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 273 | 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 Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 279 | return 6 + 2 + len; |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | int 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 | |
| 318 | /* generates a func with max supported P states */ |
| 319 | int acpigen_write_PPC(u8 nr) |
| 320 | { |
| 321 | /* |
| 322 | Method (_PPC, 0, NotSerialized) |
| 323 | { |
| 324 | Return (nr) |
| 325 | } |
| 326 | */ |
| 327 | int len; |
| 328 | /* method op */ |
| 329 | acpigen_emit_byte(0x14); |
| 330 | len = acpigen_write_len_f(); |
Rudolf Marek | 3310d75 | 2009-06-21 20:26:13 +0000 | [diff] [blame] | 331 | len += acpigen_emit_namestring("_PPC"); |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 332 | /* no fnarg */ |
| 333 | acpigen_emit_byte(0x00); |
| 334 | /* return */ |
| 335 | acpigen_emit_byte(0xa4); |
| 336 | /* arg */ |
| 337 | len += acpigen_write_byte(nr); |
Rudolf Marek | 6b2e760 | 2009-03-02 22:45:31 +0000 | [diff] [blame] | 338 | /* add all single bytes */ |
| 339 | len += 3; |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 340 | acpigen_patch_len(len - 1); |
Rudolf Marek | 6b2e760 | 2009-03-02 22:45:31 +0000 | [diff] [blame] | 341 | return len; |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 344 | int acpigen_write_PSS_package(u32 coreFreq, u32 power, u32 transLat, |
| 345 | u32 busmLat, u32 control, u32 status) |
Rudolf Marek | f997b55 | 2009-02-14 15:40:23 +0000 | [diff] [blame] | 346 | { |
| 347 | int len; |
| 348 | len = acpigen_write_package(6); |
| 349 | len += acpigen_write_dword(coreFreq); |
| 350 | len += acpigen_write_dword(power); |
| 351 | len += acpigen_write_dword(transLat); |
| 352 | len += acpigen_write_dword(busmLat); |
| 353 | len += acpigen_write_dword(control); |
| 354 | len += acpigen_write_dword(status); |
| 355 | //pkglen without the len opcode |
| 356 | acpigen_patch_len(len - 1); |
| 357 | return len; |
| 358 | } |
Patrick Georgi | df444bf | 2009-04-21 20:34:36 +0000 | [diff] [blame] | 359 | |
| 360 | int acpigen_write_PSD_package(u32 domain, u32 numprocs, PSD_coord coordtype) |
| 361 | { |
| 362 | int len, lenh, lenp; |
| 363 | lenh = acpigen_write_name("_PSD"); |
| 364 | lenp = acpigen_write_package(1); |
| 365 | len = acpigen_write_package(5); |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 366 | len += acpigen_write_byte(5); // 5 values |
| 367 | len += acpigen_write_byte(0); // revision 0 |
Patrick Georgi | df444bf | 2009-04-21 20:34:36 +0000 | [diff] [blame] | 368 | len += acpigen_write_dword(domain); |
| 369 | len += acpigen_write_dword(coordtype); |
| 370 | len += acpigen_write_dword(numprocs); |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 371 | acpigen_patch_len(len - 1); |
Patrick Georgi | df444bf | 2009-04-21 20:34:36 +0000 | [diff] [blame] | 372 | len += lenp; |
Stefan Reinauer | f96c2d9 | 2009-04-22 16:23:47 +0000 | [diff] [blame] | 373 | acpigen_patch_len(len - 1); |
Patrick Georgi | df444bf | 2009-04-21 20:34:36 +0000 | [diff] [blame] | 374 | return len + lenh; |
| 375 | } |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 376 | |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame^] | 377 | static int acpigen_write_CST_package_entry(acpi_cstate_t *cstate) |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 378 | { |
| 379 | int len, len0; |
| 380 | char *start, *end; |
| 381 | |
| 382 | len0 = acpigen_write_package(4); |
| 383 | len = acpigen_write_resourcetemplate_header(); |
| 384 | start = acpigen_get_current(); |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame^] | 385 | acpigen_write_register(&cstate->resource); |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 386 | end = acpigen_get_current(); |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame^] | 387 | len += end - start; |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 388 | len += acpigen_write_resourcetemplate_footer(len); |
| 389 | len += len0; |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame^] | 390 | len += acpigen_write_dword(cstate->ctype); |
| 391 | len += acpigen_write_dword(cstate->latency); |
| 392 | len += acpigen_write_dword(cstate->power); |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 393 | acpigen_patch_len(len - 1); |
| 394 | return len; |
| 395 | } |
| 396 | |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame^] | 397 | int acpigen_write_CST_package(acpi_cstate_t *cstate, int nentries) |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 398 | { |
| 399 | int len, lenh, lenp, i; |
| 400 | lenh = acpigen_write_name("_CST"); |
| 401 | lenp = acpigen_write_package(nentries+1); |
| 402 | len = acpigen_write_dword(nentries); |
| 403 | |
| 404 | for (i = 0; i < nentries; i++) |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame^] | 405 | len += acpigen_write_CST_package_entry(cstate + i); |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 406 | |
| 407 | len += lenp; |
| 408 | acpigen_patch_len(len - 1); |
| 409 | return len + lenh; |
| 410 | } |
| 411 | |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 412 | int acpigen_write_mem32fixed(int readwrite, u32 base, u32 size) |
| 413 | { |
| 414 | /* |
| 415 | * acpi 4.0 section 6.4.3.4: 32-Bit Fixed Memory Range Descriptor |
| 416 | * Byte 0: |
| 417 | * Bit7 : 1 => big item |
| 418 | * Bit6-0: 0000110 (0x6) => 32-bit fixed memory |
| 419 | */ |
| 420 | acpigen_emit_byte(0x86); |
| 421 | /* Byte 1+2: length (0x0009) */ |
| 422 | acpigen_emit_byte(0x09); |
| 423 | acpigen_emit_byte(0x00); |
| 424 | /* bit1-7 are ignored */ |
| 425 | acpigen_emit_byte(readwrite ? 0x01 : 0x00); |
| 426 | acpigen_emit_byte(base & 0xff); |
| 427 | acpigen_emit_byte((base >> 8) & 0xff); |
| 428 | acpigen_emit_byte((base >> 16) & 0xff); |
| 429 | acpigen_emit_byte((base >> 24) & 0xff); |
| 430 | acpigen_emit_byte(size & 0xff); |
| 431 | acpigen_emit_byte((size >> 8) & 0xff); |
| 432 | acpigen_emit_byte((size >> 16) & 0xff); |
| 433 | acpigen_emit_byte((size >> 24) & 0xff); |
| 434 | return 12; |
| 435 | } |
| 436 | |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame^] | 437 | int acpigen_write_register(acpi_addr_t *addr) |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 438 | { |
Stefan Reinauer | 4cc8c70 | 2012-04-27 21:34:16 +0200 | [diff] [blame^] | 439 | acpigen_emit_byte(0x82); /* Register Descriptor */ |
| 440 | acpigen_emit_byte(0x0c); /* Register Length 7:0 */ |
| 441 | acpigen_emit_byte(0x00); /* Register Length 15:8 */ |
| 442 | acpigen_emit_byte(addr->space_id); /* Address Space ID */ |
| 443 | acpigen_emit_byte(addr->bit_width); /* Register Bit Width */ |
| 444 | acpigen_emit_byte(addr->bit_offset); /* Register Bit Offset */ |
| 445 | acpigen_emit_byte(addr->resv); /* Register Access Size */ |
| 446 | acpigen_emit_byte(addr->addrl & 0xff); /* Register Address Low */ |
| 447 | acpigen_emit_byte((addr->addrl >> 8) & 0xff); |
| 448 | acpigen_emit_byte((addr->addrl >> 16) & 0xff); |
| 449 | acpigen_emit_byte((addr->addrl >> 24) & 0xff); |
| 450 | acpigen_emit_byte(addr->addrh & 0xff); /* Register Address High */ |
| 451 | acpigen_emit_byte((addr->addrh >> 8) & 0xff); |
| 452 | acpigen_emit_byte((addr->addrh >> 16) & 0xff); |
| 453 | acpigen_emit_byte((addr->addrh >> 24) & 0xff); |
Sven Schnelle | 0b86c76 | 2011-10-21 21:46:47 +0200 | [diff] [blame] | 454 | return 15; |
| 455 | } |
| 456 | |
Tobias Diedrich | 0fe6e9a | 2010-11-17 16:27:06 +0000 | [diff] [blame] | 457 | int acpigen_write_io16(u16 min, u16 max, u8 align, u8 len, u8 decode16) |
| 458 | { |
| 459 | /* |
| 460 | * acpi 4.0 section 6.4.2.6: I/O Port Descriptor |
| 461 | * Byte 0: |
| 462 | * Bit7 : 0 => small item |
| 463 | * Bit6-3: 1000 (0x8) => I/O port descriptor |
| 464 | * Bit2-0: 111 (0x7) => 7 Bytes long |
| 465 | */ |
| 466 | acpigen_emit_byte(0x47); |
| 467 | /* does the device decode all 16 or just 10 bits? */ |
| 468 | /* bit1-7 are ignored */ |
| 469 | acpigen_emit_byte(decode16 ? 0x01 : 0x00); |
| 470 | /* minimum base address the device may be configured for */ |
| 471 | acpigen_emit_byte(min & 0xff); |
| 472 | acpigen_emit_byte((min >> 8) & 0xff); |
| 473 | /* maximum base address the device may be configured for */ |
| 474 | acpigen_emit_byte(max & 0xff); |
| 475 | acpigen_emit_byte((max >> 8) & 0xff); |
| 476 | /* alignment for min base */ |
| 477 | acpigen_emit_byte(align & 0xff); |
| 478 | acpigen_emit_byte(len & 0xff); |
| 479 | return 8; |
| 480 | } |
| 481 | |
| 482 | int acpigen_write_resourcetemplate_header(void) |
| 483 | { |
| 484 | int len; |
| 485 | /* |
| 486 | * A ResourceTemplate() is a Buffer() with a |
| 487 | * (Byte|Word|DWord) containing the length, followed by one or more |
| 488 | * resource items, terminated by the end tag |
| 489 | * (small item 0xf, len 1) |
| 490 | */ |
| 491 | len = acpigen_emit_byte(0x11); /* Buffer opcode */ |
| 492 | len += acpigen_write_len_f(); |
| 493 | len += acpigen_emit_byte(0x0b); /* Word opcode */ |
| 494 | len_stack[ltop++] = acpigen_get_current(); |
| 495 | len += acpigen_emit_byte(0x00); |
| 496 | len += acpigen_emit_byte(0x00); |
| 497 | return len; |
| 498 | } |
| 499 | |
| 500 | int acpigen_write_resourcetemplate_footer(int len) |
| 501 | { |
| 502 | char *p = len_stack[--ltop]; |
| 503 | /* |
| 504 | * end tag (acpi 4.0 Section 6.4.2.8) |
| 505 | * 0x79 <checksum> |
| 506 | * 0x00 is treated as a good checksum according to the spec |
| 507 | * and is what iasl generates. |
| 508 | */ |
| 509 | len += acpigen_emit_byte(0x79); |
| 510 | len += acpigen_emit_byte(0x00); |
| 511 | /* patch len word */ |
| 512 | p[0] = (len-6) & 0xff; |
| 513 | p[1] = ((len-6) >> 8) & 0xff; |
| 514 | /* patch len field */ |
| 515 | acpigen_patch_len(len-1); |
| 516 | return 2; |
| 517 | } |
| 518 | |
| 519 | static void acpigen_add_mainboard_rsvd_mem32(void *gp, struct device *dev, |
| 520 | struct resource *res) |
| 521 | { |
| 522 | acpigen_write_mem32fixed(0, res->base, res->size); |
| 523 | } |
| 524 | |
| 525 | static void acpigen_add_mainboard_rsvd_io(void *gp, struct device *dev, |
| 526 | struct resource *res) |
| 527 | { |
| 528 | resource_t base = res->base; |
| 529 | resource_t size = res->size; |
| 530 | while (size > 0) { |
| 531 | resource_t sz = size > 255 ? 255 : size; |
| 532 | acpigen_write_io16(base, base, 0, sz, 1); |
| 533 | size -= sz; |
| 534 | base += sz; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | int acpigen_write_mainboard_resource_template(void) |
| 539 | { |
| 540 | int len; |
| 541 | char *start; |
| 542 | char *end; |
| 543 | len = acpigen_write_resourcetemplate_header(); |
| 544 | start = acpigen_get_current(); |
| 545 | |
| 546 | /* Add reserved memory ranges */ |
| 547 | search_global_resources( |
| 548 | IORESOURCE_MEM | IORESOURCE_RESERVE, |
| 549 | IORESOURCE_MEM | IORESOURCE_RESERVE, |
| 550 | acpigen_add_mainboard_rsvd_mem32, 0); |
| 551 | |
| 552 | /* Add reserved io ranges */ |
| 553 | search_global_resources( |
| 554 | IORESOURCE_IO | IORESOURCE_RESERVE, |
| 555 | IORESOURCE_IO | IORESOURCE_RESERVE, |
| 556 | acpigen_add_mainboard_rsvd_io, 0); |
| 557 | |
| 558 | end = acpigen_get_current(); |
| 559 | len += end-start; |
| 560 | len += acpigen_write_resourcetemplate_footer(len); |
| 561 | return len; |
| 562 | } |
| 563 | |
| 564 | int acpigen_write_mainboard_resources(const char *scope, const char *name) |
| 565 | { |
| 566 | int len; |
| 567 | len = acpigen_write_scope(scope); |
| 568 | len += acpigen_write_name(name); |
| 569 | len += acpigen_write_mainboard_resource_template(); |
| 570 | acpigen_patch_len(len - 1); |
| 571 | return len; |
| 572 | } |