Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Generation of ACPI (Advanced Configuration and Power Interface) tables |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | * Mostly taken from coreboot |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY LOGC_ACPI |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 48342b0 | 2020-07-07 13:11:55 -0600 | [diff] [blame] | 14 | #include <uuid.h> |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 15 | #include <acpi/acpigen.h> |
Simon Glass | 8a20076 | 2020-07-07 13:12:01 -0600 | [diff] [blame] | 16 | #include <acpi/acpi_device.h> |
Simon Glass | 837127f | 2020-07-07 21:32:11 -0600 | [diff] [blame] | 17 | #include <acpi/acpi_table.h> |
Simon Glass | 98528d4 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 18 | #include <dm/acpi.h> |
| 19 | |
| 20 | u8 *acpigen_get_current(struct acpi_ctx *ctx) |
| 21 | { |
| 22 | return ctx->current; |
| 23 | } |
| 24 | |
| 25 | void acpigen_emit_byte(struct acpi_ctx *ctx, uint data) |
| 26 | { |
| 27 | *(u8 *)ctx->current++ = data; |
| 28 | } |
| 29 | |
| 30 | void acpigen_emit_word(struct acpi_ctx *ctx, uint data) |
| 31 | { |
| 32 | acpigen_emit_byte(ctx, data & 0xff); |
| 33 | acpigen_emit_byte(ctx, (data >> 8) & 0xff); |
| 34 | } |
| 35 | |
| 36 | void acpigen_emit_dword(struct acpi_ctx *ctx, uint data) |
| 37 | { |
| 38 | /* Output the value in little-endian format */ |
| 39 | acpigen_emit_byte(ctx, data & 0xff); |
| 40 | acpigen_emit_byte(ctx, (data >> 8) & 0xff); |
| 41 | acpigen_emit_byte(ctx, (data >> 16) & 0xff); |
| 42 | acpigen_emit_byte(ctx, (data >> 24) & 0xff); |
| 43 | } |
Simon Glass | 071c4a5 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 44 | |
Simon Glass | 0f27763 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 45 | /* |
| 46 | * Maximum length for an ACPI object generated by this code, |
| 47 | * |
| 48 | * If you need to change this, change acpigen_write_len_f(ctx) and |
| 49 | * acpigen_pop_len(ctx) |
| 50 | */ |
| 51 | #define ACPIGEN_MAXLEN 0xfffff |
| 52 | |
| 53 | void acpigen_write_len_f(struct acpi_ctx *ctx) |
| 54 | { |
| 55 | assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1)); |
| 56 | ctx->len_stack[ctx->ltop++] = ctx->current; |
| 57 | acpigen_emit_byte(ctx, 0); |
| 58 | acpigen_emit_byte(ctx, 0); |
| 59 | acpigen_emit_byte(ctx, 0); |
| 60 | } |
| 61 | |
| 62 | void acpigen_pop_len(struct acpi_ctx *ctx) |
| 63 | { |
| 64 | int len; |
| 65 | char *p; |
| 66 | |
| 67 | assert(ctx->ltop > 0); |
| 68 | p = ctx->len_stack[--ctx->ltop]; |
| 69 | len = ctx->current - (void *)p; |
| 70 | assert(len <= ACPIGEN_MAXLEN); |
| 71 | /* generate store length for 0xfffff max */ |
| 72 | p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf); |
| 73 | p[1] = len >> 4 & 0xff; |
| 74 | p[2] = len >> 12 & 0xff; |
| 75 | } |
| 76 | |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 77 | void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op) |
| 78 | { |
| 79 | acpigen_emit_byte(ctx, EXT_OP_PREFIX); |
| 80 | acpigen_emit_byte(ctx, op); |
| 81 | } |
| 82 | |
Simon Glass | edc2680 | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 83 | char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el) |
| 84 | { |
| 85 | char *p; |
| 86 | |
| 87 | acpigen_emit_byte(ctx, PACKAGE_OP); |
| 88 | acpigen_write_len_f(ctx); |
| 89 | p = ctx->current; |
| 90 | acpigen_emit_byte(ctx, nr_el); |
| 91 | |
| 92 | return p; |
| 93 | } |
| 94 | |
Simon Glass | 8715ce0 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 95 | void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data) |
| 96 | { |
| 97 | acpigen_emit_byte(ctx, BYTE_PREFIX); |
| 98 | acpigen_emit_byte(ctx, data & 0xff); |
| 99 | } |
| 100 | |
| 101 | void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data) |
| 102 | { |
| 103 | acpigen_emit_byte(ctx, WORD_PREFIX); |
| 104 | acpigen_emit_word(ctx, data); |
| 105 | } |
| 106 | |
| 107 | void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data) |
| 108 | { |
| 109 | acpigen_emit_byte(ctx, DWORD_PREFIX); |
| 110 | acpigen_emit_dword(ctx, data); |
| 111 | } |
| 112 | |
| 113 | void acpigen_write_qword(struct acpi_ctx *ctx, u64 data) |
| 114 | { |
| 115 | acpigen_emit_byte(ctx, QWORD_PREFIX); |
| 116 | acpigen_emit_dword(ctx, data & 0xffffffff); |
| 117 | acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff); |
| 118 | } |
| 119 | |
| 120 | void acpigen_write_zero(struct acpi_ctx *ctx) |
| 121 | { |
| 122 | acpigen_emit_byte(ctx, ZERO_OP); |
| 123 | } |
| 124 | |
| 125 | void acpigen_write_one(struct acpi_ctx *ctx) |
| 126 | { |
| 127 | acpigen_emit_byte(ctx, ONE_OP); |
| 128 | } |
| 129 | |
| 130 | void acpigen_write_integer(struct acpi_ctx *ctx, u64 data) |
| 131 | { |
| 132 | if (data == 0) |
| 133 | acpigen_write_zero(ctx); |
| 134 | else if (data == 1) |
| 135 | acpigen_write_one(ctx); |
| 136 | else if (data <= 0xff) |
| 137 | acpigen_write_byte(ctx, (unsigned char)data); |
| 138 | else if (data <= 0xffff) |
| 139 | acpigen_write_word(ctx, (unsigned int)data); |
| 140 | else if (data <= 0xffffffff) |
| 141 | acpigen_write_dword(ctx, (unsigned int)data); |
| 142 | else |
| 143 | acpigen_write_qword(ctx, data); |
| 144 | } |
| 145 | |
Simon Glass | b4df078 | 2020-07-07 21:32:15 -0600 | [diff] [blame] | 146 | void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name) |
| 147 | { |
| 148 | acpigen_write_name(ctx, name); |
| 149 | acpigen_write_zero(ctx); |
| 150 | } |
| 151 | |
| 152 | void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name) |
| 153 | { |
| 154 | acpigen_write_name(ctx, name); |
| 155 | acpigen_write_one(ctx); |
| 156 | } |
| 157 | |
| 158 | void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val) |
| 159 | { |
| 160 | acpigen_write_name(ctx, name); |
| 161 | acpigen_write_byte(ctx, val); |
| 162 | } |
| 163 | |
| 164 | void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val) |
| 165 | { |
| 166 | acpigen_write_name(ctx, name); |
| 167 | acpigen_write_word(ctx, val); |
| 168 | } |
| 169 | |
| 170 | void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val) |
| 171 | { |
| 172 | acpigen_write_name(ctx, name); |
| 173 | acpigen_write_dword(ctx, val); |
| 174 | } |
| 175 | |
| 176 | void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val) |
| 177 | { |
| 178 | acpigen_write_name(ctx, name); |
| 179 | acpigen_write_qword(ctx, val); |
| 180 | } |
| 181 | |
| 182 | void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name, u64 val) |
| 183 | { |
| 184 | acpigen_write_name(ctx, name); |
| 185 | acpigen_write_integer(ctx, val); |
| 186 | } |
| 187 | |
| 188 | void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name, |
| 189 | const char *string) |
| 190 | { |
| 191 | acpigen_write_name(ctx, name); |
| 192 | acpigen_write_string(ctx, string); |
| 193 | } |
| 194 | |
Simon Glass | 071c4a5 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 195 | void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < size; i++) |
| 200 | acpigen_emit_byte(ctx, data[i]); |
| 201 | } |
| 202 | |
| 203 | void acpigen_emit_string(struct acpi_ctx *ctx, const char *str) |
| 204 | { |
| 205 | acpigen_emit_stream(ctx, str, str ? strlen(str) : 0); |
| 206 | acpigen_emit_byte(ctx, '\0'); |
| 207 | } |
Simon Glass | 45d6d95 | 2020-07-07 13:11:53 -0600 | [diff] [blame] | 208 | |
| 209 | void acpigen_write_string(struct acpi_ctx *ctx, const char *str) |
| 210 | { |
| 211 | acpigen_emit_byte(ctx, STRING_PREFIX); |
| 212 | acpigen_emit_string(ctx, str); |
| 213 | } |
Simon Glass | 0dc3d51 | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * The naming conventions for ACPI namespace names are a bit tricky as |
| 217 | * each element has to be 4 chars wide ("All names are a fixed 32 bits.") |
| 218 | * and "By convention, when an ASL compiler pads a name shorter than 4 |
| 219 | * characters, it is done so with trailing underscores ('_')". |
| 220 | * |
| 221 | * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details. |
| 222 | */ |
| 223 | static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx, |
| 224 | const char *name) |
| 225 | { |
| 226 | const char *ptr; |
| 227 | int i; |
| 228 | |
| 229 | for (i = 0, ptr = name; i < 4; i++) { |
| 230 | if (!*ptr || *ptr == '.') |
| 231 | acpigen_emit_byte(ctx, '_'); |
| 232 | else |
| 233 | acpigen_emit_byte(ctx, *ptr++); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | static void acpigen_emit_double_namestring(struct acpi_ctx *ctx, |
| 238 | const char *name, int dotpos) |
| 239 | { |
| 240 | acpigen_emit_byte(ctx, DUAL_NAME_PREFIX); |
| 241 | acpigen_emit_simple_namestring(ctx, name); |
| 242 | acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]); |
| 243 | } |
| 244 | |
| 245 | static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx, |
| 246 | const char *name) |
| 247 | { |
| 248 | unsigned char *pathlen; |
| 249 | int count = 0; |
| 250 | |
| 251 | acpigen_emit_byte(ctx, MULTI_NAME_PREFIX); |
| 252 | pathlen = ctx->current; |
| 253 | acpigen_emit_byte(ctx, 0); |
| 254 | |
| 255 | while (*name) { |
| 256 | acpigen_emit_simple_namestring(ctx, name); |
| 257 | /* find end or next entity */ |
| 258 | while (*name != '.' && *name) |
| 259 | name++; |
| 260 | /* forward to next */ |
| 261 | if (*name == '.') |
| 262 | name++; |
| 263 | count++; |
| 264 | } |
| 265 | |
| 266 | *pathlen = count; |
| 267 | } |
| 268 | |
| 269 | void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath) |
| 270 | { |
| 271 | int dotcount; |
| 272 | int dotpos; |
| 273 | int i; |
| 274 | |
| 275 | /* We can start with a '\' */ |
| 276 | if (*namepath == '\\') { |
| 277 | acpigen_emit_byte(ctx, '\\'); |
| 278 | namepath++; |
| 279 | } |
| 280 | |
| 281 | /* And there can be any number of '^' */ |
| 282 | while (*namepath == '^') { |
| 283 | acpigen_emit_byte(ctx, '^'); |
| 284 | namepath++; |
| 285 | } |
| 286 | |
| 287 | for (i = 0, dotcount = 0; namepath[i]; i++) { |
| 288 | if (namepath[i] == '.') { |
| 289 | dotcount++; |
| 290 | dotpos = i; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* If we have only \\ or only ^* then we need to add a null name */ |
| 295 | if (!*namepath) |
| 296 | acpigen_emit_byte(ctx, ZERO_OP); |
| 297 | else if (dotcount == 0) |
| 298 | acpigen_emit_simple_namestring(ctx, namepath); |
| 299 | else if (dotcount == 1) |
| 300 | acpigen_emit_double_namestring(ctx, namepath, dotpos); |
| 301 | else |
| 302 | acpigen_emit_multi_namestring(ctx, namepath); |
| 303 | } |
| 304 | |
| 305 | void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath) |
| 306 | { |
| 307 | acpigen_emit_byte(ctx, NAME_OP); |
| 308 | acpigen_emit_namestring(ctx, namepath); |
| 309 | } |
Simon Glass | 48342b0 | 2020-07-07 13:11:55 -0600 | [diff] [blame] | 310 | |
Simon Glass | 6fcdaf1 | 2020-07-07 21:32:10 -0600 | [diff] [blame] | 311 | void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope) |
| 312 | { |
| 313 | acpigen_emit_byte(ctx, SCOPE_OP); |
| 314 | acpigen_write_len_f(ctx); |
| 315 | acpigen_emit_namestring(ctx, scope); |
| 316 | } |
| 317 | |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 318 | static void acpigen_write_method_internal(struct acpi_ctx *ctx, |
| 319 | const char *name, uint flags) |
| 320 | { |
| 321 | acpigen_emit_byte(ctx, METHOD_OP); |
| 322 | acpigen_write_len_f(ctx); |
| 323 | acpigen_emit_namestring(ctx, name); |
| 324 | acpigen_emit_byte(ctx, flags); |
| 325 | } |
| 326 | |
| 327 | /* Method (name, nargs, NotSerialized) */ |
| 328 | void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs) |
| 329 | { |
| 330 | acpigen_write_method_internal(ctx, name, |
| 331 | nargs & ACPI_METHOD_NARGS_MASK); |
| 332 | } |
| 333 | |
| 334 | /* Method (name, nargs, Serialized) */ |
| 335 | void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name, |
| 336 | int nargs) |
| 337 | { |
| 338 | acpigen_write_method_internal(ctx, name, |
| 339 | (nargs & ACPI_METHOD_NARGS_MASK) | |
| 340 | ACPI_METHOD_SERIALIZED_MASK); |
| 341 | } |
| 342 | |
Simon Glass | a186d93 | 2020-07-07 21:32:14 -0600 | [diff] [blame] | 343 | void acpigen_write_device(struct acpi_ctx *ctx, const char *name) |
| 344 | { |
| 345 | acpigen_emit_ext_op(ctx, DEVICE_OP); |
| 346 | acpigen_write_len_f(ctx); |
| 347 | acpigen_emit_namestring(ctx, name); |
| 348 | } |
| 349 | |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 350 | void acpigen_write_sta(struct acpi_ctx *ctx, uint status) |
| 351 | { |
| 352 | /* Method (_STA, 0, NotSerialized) { Return (status) } */ |
| 353 | acpigen_write_method(ctx, "_STA", 0); |
| 354 | acpigen_emit_byte(ctx, RETURN_OP); |
| 355 | acpigen_write_byte(ctx, status); |
| 356 | acpigen_pop_len(ctx); |
| 357 | } |
| 358 | |
Simon Glass | 837127f | 2020-07-07 21:32:11 -0600 | [diff] [blame] | 359 | static void acpigen_write_register(struct acpi_ctx *ctx, |
| 360 | const struct acpi_gen_regaddr *addr) |
| 361 | { |
| 362 | /* See ACPI v6.3 section 6.4.3.7: Generic Register Descriptor */ |
| 363 | acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_REGISTER); |
| 364 | acpigen_emit_byte(ctx, 0x0c); /* Register Length 7:0 */ |
| 365 | acpigen_emit_byte(ctx, 0x00); /* Register Length 15:8 */ |
| 366 | acpigen_emit_byte(ctx, addr->space_id); |
| 367 | acpigen_emit_byte(ctx, addr->bit_width); |
| 368 | acpigen_emit_byte(ctx, addr->bit_offset); |
| 369 | acpigen_emit_byte(ctx, addr->access_size); |
| 370 | acpigen_emit_dword(ctx, addr->addrl); |
| 371 | acpigen_emit_dword(ctx, addr->addrh); |
| 372 | } |
| 373 | |
| 374 | void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx) |
| 375 | { |
| 376 | /* |
| 377 | * A ResourceTemplate() is a Buffer() with a |
| 378 | * (Byte|Word|DWord) containing the length, followed by one or more |
| 379 | * resource items, terminated by the end tag. |
| 380 | * (small item 0xf, len 1) |
| 381 | */ |
| 382 | acpigen_emit_byte(ctx, BUFFER_OP); |
| 383 | acpigen_write_len_f(ctx); |
| 384 | acpigen_emit_byte(ctx, WORD_PREFIX); |
| 385 | ctx->len_stack[ctx->ltop++] = ctx->current; |
| 386 | |
| 387 | /* |
| 388 | * Add two dummy bytes for the ACPI word (keep aligned with the |
| 389 | * calculation in acpigen_write_resourcetemplate_footer() below) |
| 390 | */ |
| 391 | acpigen_emit_byte(ctx, 0x00); |
| 392 | acpigen_emit_byte(ctx, 0x00); |
| 393 | } |
| 394 | |
| 395 | void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx) |
| 396 | { |
| 397 | char *p = ctx->len_stack[--ctx->ltop]; |
| 398 | int len; |
| 399 | /* |
| 400 | * See ACPI v6.3 section 6.4.2.9: End Tag |
| 401 | * 0x79 <checksum> |
| 402 | * 0x00 is treated as a good checksum according to the spec |
| 403 | * and is what iasl generates. |
| 404 | */ |
| 405 | acpigen_emit_byte(ctx, ACPI_END_TAG); |
| 406 | acpigen_emit_byte(ctx, 0x00); |
| 407 | |
| 408 | /* |
| 409 | * Start counting past the 2-bytes length added in |
| 410 | * acpigen_write_resourcetemplate_header() above |
| 411 | */ |
| 412 | len = (char *)ctx->current - (p + 2); |
| 413 | |
| 414 | /* patch len word */ |
| 415 | p[0] = len & 0xff; |
| 416 | p[1] = (len >> 8) & 0xff; |
| 417 | |
| 418 | acpigen_pop_len(ctx); |
| 419 | } |
| 420 | |
| 421 | void acpigen_write_register_resource(struct acpi_ctx *ctx, |
| 422 | const struct acpi_gen_regaddr *addr) |
| 423 | { |
| 424 | acpigen_write_resourcetemplate_header(ctx); |
| 425 | acpigen_write_register(ctx, addr); |
| 426 | acpigen_write_resourcetemplate_footer(ctx); |
| 427 | } |
| 428 | |
Simon Glass | 77dfd03 | 2020-09-22 12:44:56 -0600 | [diff] [blame] | 429 | void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level) |
| 430 | { |
| 431 | /* Name (_PRW, Package () { wake, level } */ |
| 432 | acpigen_write_name(ctx, "_PRW"); |
| 433 | acpigen_write_package(ctx, 2); |
| 434 | acpigen_write_integer(ctx, wake); |
| 435 | acpigen_write_integer(ctx, level); |
| 436 | acpigen_pop_len(ctx); |
| 437 | } |
| 438 | |
Simon Glass | 48342b0 | 2020-07-07 13:11:55 -0600 | [diff] [blame] | 439 | /* |
| 440 | * ToUUID(uuid) |
| 441 | * |
| 442 | * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the |
| 443 | * bytes that make up a UUID Buffer object: |
| 444 | * |
| 445 | * UUID byte order for input to this function: |
| 446 | * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp |
| 447 | * |
| 448 | * UUID byte order output by this function: |
| 449 | * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp |
| 450 | */ |
| 451 | int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid) |
| 452 | { |
| 453 | u8 buf[UUID_BIN_LEN]; |
| 454 | int ret; |
| 455 | |
| 456 | /* Parse UUID string into bytes */ |
| 457 | ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID); |
| 458 | if (ret) |
| 459 | return log_msg_ret("bad hex", -EINVAL); |
| 460 | |
| 461 | /* BufferOp */ |
| 462 | acpigen_emit_byte(ctx, BUFFER_OP); |
| 463 | acpigen_write_len_f(ctx); |
| 464 | |
| 465 | /* Buffer length in bytes */ |
| 466 | acpigen_write_word(ctx, UUID_BIN_LEN); |
| 467 | |
| 468 | /* Output UUID in expected order */ |
| 469 | acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN); |
| 470 | |
| 471 | acpigen_pop_len(ctx); |
| 472 | |
| 473 | return 0; |
| 474 | } |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 475 | |
Simon Glass | d0f7d9b | 2020-07-07 13:12:00 -0600 | [diff] [blame] | 476 | void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level, |
| 477 | uint order, const char *const dev_states[], |
| 478 | size_t dev_states_count) |
| 479 | { |
| 480 | size_t i; |
| 481 | |
| 482 | for (i = 0; i < dev_states_count; i++) { |
| 483 | acpigen_write_name(ctx, dev_states[i]); |
| 484 | acpigen_write_package(ctx, 1); |
| 485 | acpigen_emit_simple_namestring(ctx, name); |
| 486 | acpigen_pop_len(ctx); /* Package */ |
| 487 | } |
| 488 | |
| 489 | acpigen_emit_ext_op(ctx, POWER_RES_OP); |
| 490 | |
| 491 | acpigen_write_len_f(ctx); |
| 492 | |
| 493 | acpigen_emit_simple_namestring(ctx, name); |
| 494 | acpigen_emit_byte(ctx, level); |
| 495 | acpigen_emit_word(ctx, order); |
| 496 | } |
| 497 | |
Simon Glass | 9238fac | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 498 | /* Sleep (ms) */ |
| 499 | void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms) |
| 500 | { |
| 501 | acpigen_emit_ext_op(ctx, SLEEP_OP); |
| 502 | acpigen_write_integer(ctx, sleep_ms); |
| 503 | } |
| 504 | |
| 505 | void acpigen_write_store(struct acpi_ctx *ctx) |
| 506 | { |
| 507 | acpigen_emit_byte(ctx, STORE_OP); |
| 508 | } |
| 509 | |
| 510 | /* Or (arg1, arg2, res) */ |
| 511 | void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res) |
| 512 | { |
| 513 | acpigen_emit_byte(ctx, OR_OP); |
| 514 | acpigen_emit_byte(ctx, arg1); |
| 515 | acpigen_emit_byte(ctx, arg2); |
| 516 | acpigen_emit_byte(ctx, res); |
| 517 | } |
| 518 | |
| 519 | /* And (arg1, arg2, res) */ |
| 520 | void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res) |
| 521 | { |
| 522 | acpigen_emit_byte(ctx, AND_OP); |
| 523 | acpigen_emit_byte(ctx, arg1); |
| 524 | acpigen_emit_byte(ctx, arg2); |
| 525 | acpigen_emit_byte(ctx, res); |
| 526 | } |
| 527 | |
| 528 | /* Not (arg, res) */ |
| 529 | void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res) |
| 530 | { |
| 531 | acpigen_emit_byte(ctx, NOT_OP); |
| 532 | acpigen_emit_byte(ctx, arg); |
| 533 | acpigen_emit_byte(ctx, res); |
| 534 | } |
| 535 | |
| 536 | /* Store (str, DEBUG) */ |
| 537 | void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str) |
| 538 | { |
| 539 | acpigen_write_store(ctx); |
| 540 | acpigen_write_string(ctx, str); |
| 541 | acpigen_emit_ext_op(ctx, DEBUG_OP); |
| 542 | } |
Simon Glass | 8a20076 | 2020-07-07 13:12:01 -0600 | [diff] [blame] | 543 | |
Simon Glass | ca72763 | 2020-09-22 12:44:57 -0600 | [diff] [blame^] | 544 | void acpigen_write_if(struct acpi_ctx *ctx) |
| 545 | { |
| 546 | acpigen_emit_byte(ctx, IF_OP); |
| 547 | acpigen_write_len_f(ctx); |
| 548 | } |
| 549 | |
| 550 | void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val) |
| 551 | { |
| 552 | acpigen_write_if(ctx); |
| 553 | acpigen_emit_byte(ctx, LEQUAL_OP); |
| 554 | acpigen_emit_byte(ctx, op); |
| 555 | acpigen_write_integer(ctx, val); |
| 556 | } |
| 557 | |
| 558 | void acpigen_write_else(struct acpi_ctx *ctx) |
| 559 | { |
| 560 | acpigen_emit_byte(ctx, ELSE_OP); |
| 561 | acpigen_write_len_f(ctx); |
| 562 | } |
| 563 | |
| 564 | void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst) |
| 565 | { |
| 566 | acpigen_emit_byte(ctx, TO_BUFFER_OP); |
| 567 | acpigen_emit_byte(ctx, src); |
| 568 | acpigen_emit_byte(ctx, dst); |
| 569 | } |
| 570 | |
| 571 | void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst) |
| 572 | { |
| 573 | acpigen_emit_byte(ctx, TO_INTEGER_OP); |
| 574 | acpigen_emit_byte(ctx, src); |
| 575 | acpigen_emit_byte(ctx, dst); |
| 576 | } |
| 577 | |
| 578 | void acpigen_write_byte_buffer(struct acpi_ctx *ctx, u8 *arr, size_t size) |
| 579 | { |
| 580 | size_t i; |
| 581 | |
| 582 | acpigen_emit_byte(ctx, BUFFER_OP); |
| 583 | acpigen_write_len_f(ctx); |
| 584 | acpigen_write_integer(ctx, size); |
| 585 | |
| 586 | for (i = 0; i < size; i++) |
| 587 | acpigen_emit_byte(ctx, arr[i]); |
| 588 | |
| 589 | acpigen_pop_len(ctx); |
| 590 | } |
| 591 | |
| 592 | void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr, |
| 593 | size_t size) |
| 594 | { |
| 595 | acpigen_emit_byte(ctx, RETURN_OP); |
| 596 | acpigen_write_byte_buffer(ctx, arr, size); |
| 597 | } |
| 598 | |
| 599 | void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg) |
| 600 | { |
| 601 | u8 buf = arg; |
| 602 | |
| 603 | acpigen_write_return_byte_buffer(ctx, &buf, 1); |
| 604 | } |
| 605 | |
| 606 | void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg) |
| 607 | { |
| 608 | acpigen_emit_byte(ctx, RETURN_OP); |
| 609 | acpigen_write_byte(ctx, arg); |
| 610 | } |
| 611 | |
Simon Glass | 8a20076 | 2020-07-07 13:12:01 -0600 | [diff] [blame] | 612 | /** |
| 613 | * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5 |
| 614 | * |
| 615 | * Store (\_SB.GPC0 (addr), Local5) |
| 616 | * |
| 617 | * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in |
| 618 | * the board's gpiolib.asl |
| 619 | * |
| 620 | * The value needs to be stored in a local variable so that it can be used in |
| 621 | * expressions in the ACPI code. |
| 622 | * |
| 623 | * @ctx: ACPI context pointer |
| 624 | * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0" |
| 625 | * @addr: GPIO pin configuration register address |
| 626 | * |
| 627 | */ |
| 628 | static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx, |
| 629 | const char *dw0_read, ulong addr) |
| 630 | { |
| 631 | acpigen_write_store(ctx); |
| 632 | acpigen_emit_namestring(ctx, dw0_read); |
| 633 | acpigen_write_integer(ctx, addr); |
| 634 | acpigen_emit_byte(ctx, LOCAL5_OP); |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off |
| 639 | * |
| 640 | * @ctx: ACPI context pointer |
| 641 | * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0" |
| 642 | * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0" |
| 643 | * @gpio_num: GPIO number to adjust |
| 644 | * @vaL: true to set on, false to set off |
| 645 | */ |
| 646 | static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val, |
| 647 | const char *dw0_read, const char *dw0_write, |
| 648 | struct acpi_gpio *gpio, bool val) |
| 649 | { |
| 650 | acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr); |
| 651 | |
| 652 | /* Store (0x40, Local0) */ |
| 653 | acpigen_write_store(ctx); |
| 654 | acpigen_write_integer(ctx, tx_state_val); |
| 655 | acpigen_emit_byte(ctx, LOCAL0_OP); |
| 656 | |
| 657 | if (val) { |
| 658 | /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */ |
| 659 | acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP); |
| 660 | } else { |
| 661 | /* Not (PAD_CFG0_TX_STATE, Local6) */ |
| 662 | acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP); |
| 663 | |
| 664 | /* And (Local5, Local6, Local5) */ |
| 665 | acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP); |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * \_SB.SPC0 (addr, Local5) |
| 670 | * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in |
| 671 | * gpiolib.asl. |
| 672 | */ |
| 673 | acpigen_emit_namestring(ctx, dw0_write); |
| 674 | acpigen_write_integer(ctx, gpio->pin0_addr); |
| 675 | acpigen_emit_byte(ctx, LOCAL5_OP); |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val, |
| 681 | const char *dw0_read, const char *dw0_write, |
| 682 | struct acpi_gpio *gpio, bool enable) |
| 683 | { |
| 684 | bool set; |
| 685 | int ret; |
| 686 | |
| 687 | set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable; |
| 688 | ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio, |
| 689 | set); |
| 690 | if (ret) |
| 691 | return log_msg_ret("call", ret); |
| 692 | |
| 693 | return 0; |
| 694 | } |