acpigen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #define LOG_CATEGORY LOGC_ACPI
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <log.h>
  12. #include <uuid.h>
  13. #include <acpi/acpigen.h>
  14. #include <acpi/acpi_device.h>
  15. #include <dm/acpi.h>
  16. u8 *acpigen_get_current(struct acpi_ctx *ctx)
  17. {
  18. return ctx->current;
  19. }
  20. void acpigen_emit_byte(struct acpi_ctx *ctx, uint data)
  21. {
  22. *(u8 *)ctx->current++ = data;
  23. }
  24. void acpigen_emit_word(struct acpi_ctx *ctx, uint data)
  25. {
  26. acpigen_emit_byte(ctx, data & 0xff);
  27. acpigen_emit_byte(ctx, (data >> 8) & 0xff);
  28. }
  29. void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
  30. {
  31. /* Output the value in little-endian format */
  32. acpigen_emit_byte(ctx, data & 0xff);
  33. acpigen_emit_byte(ctx, (data >> 8) & 0xff);
  34. acpigen_emit_byte(ctx, (data >> 16) & 0xff);
  35. acpigen_emit_byte(ctx, (data >> 24) & 0xff);
  36. }
  37. /*
  38. * Maximum length for an ACPI object generated by this code,
  39. *
  40. * If you need to change this, change acpigen_write_len_f(ctx) and
  41. * acpigen_pop_len(ctx)
  42. */
  43. #define ACPIGEN_MAXLEN 0xfffff
  44. void acpigen_write_len_f(struct acpi_ctx *ctx)
  45. {
  46. assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
  47. ctx->len_stack[ctx->ltop++] = ctx->current;
  48. acpigen_emit_byte(ctx, 0);
  49. acpigen_emit_byte(ctx, 0);
  50. acpigen_emit_byte(ctx, 0);
  51. }
  52. void acpigen_pop_len(struct acpi_ctx *ctx)
  53. {
  54. int len;
  55. char *p;
  56. assert(ctx->ltop > 0);
  57. p = ctx->len_stack[--ctx->ltop];
  58. len = ctx->current - (void *)p;
  59. assert(len <= ACPIGEN_MAXLEN);
  60. /* generate store length for 0xfffff max */
  61. p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
  62. p[1] = len >> 4 & 0xff;
  63. p[2] = len >> 12 & 0xff;
  64. }
  65. void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op)
  66. {
  67. acpigen_emit_byte(ctx, EXT_OP_PREFIX);
  68. acpigen_emit_byte(ctx, op);
  69. }
  70. char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
  71. {
  72. char *p;
  73. acpigen_emit_byte(ctx, PACKAGE_OP);
  74. acpigen_write_len_f(ctx);
  75. p = ctx->current;
  76. acpigen_emit_byte(ctx, nr_el);
  77. return p;
  78. }
  79. void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data)
  80. {
  81. acpigen_emit_byte(ctx, BYTE_PREFIX);
  82. acpigen_emit_byte(ctx, data & 0xff);
  83. }
  84. void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data)
  85. {
  86. acpigen_emit_byte(ctx, WORD_PREFIX);
  87. acpigen_emit_word(ctx, data);
  88. }
  89. void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data)
  90. {
  91. acpigen_emit_byte(ctx, DWORD_PREFIX);
  92. acpigen_emit_dword(ctx, data);
  93. }
  94. void acpigen_write_qword(struct acpi_ctx *ctx, u64 data)
  95. {
  96. acpigen_emit_byte(ctx, QWORD_PREFIX);
  97. acpigen_emit_dword(ctx, data & 0xffffffff);
  98. acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff);
  99. }
  100. void acpigen_write_zero(struct acpi_ctx *ctx)
  101. {
  102. acpigen_emit_byte(ctx, ZERO_OP);
  103. }
  104. void acpigen_write_one(struct acpi_ctx *ctx)
  105. {
  106. acpigen_emit_byte(ctx, ONE_OP);
  107. }
  108. void acpigen_write_integer(struct acpi_ctx *ctx, u64 data)
  109. {
  110. if (data == 0)
  111. acpigen_write_zero(ctx);
  112. else if (data == 1)
  113. acpigen_write_one(ctx);
  114. else if (data <= 0xff)
  115. acpigen_write_byte(ctx, (unsigned char)data);
  116. else if (data <= 0xffff)
  117. acpigen_write_word(ctx, (unsigned int)data);
  118. else if (data <= 0xffffffff)
  119. acpigen_write_dword(ctx, (unsigned int)data);
  120. else
  121. acpigen_write_qword(ctx, data);
  122. }
  123. void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
  124. {
  125. int i;
  126. for (i = 0; i < size; i++)
  127. acpigen_emit_byte(ctx, data[i]);
  128. }
  129. void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
  130. {
  131. acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
  132. acpigen_emit_byte(ctx, '\0');
  133. }
  134. void acpigen_write_string(struct acpi_ctx *ctx, const char *str)
  135. {
  136. acpigen_emit_byte(ctx, STRING_PREFIX);
  137. acpigen_emit_string(ctx, str);
  138. }
  139. /*
  140. * The naming conventions for ACPI namespace names are a bit tricky as
  141. * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
  142. * and "By convention, when an ASL compiler pads a name shorter than 4
  143. * characters, it is done so with trailing underscores ('_')".
  144. *
  145. * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details.
  146. */
  147. static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx,
  148. const char *name)
  149. {
  150. const char *ptr;
  151. int i;
  152. for (i = 0, ptr = name; i < 4; i++) {
  153. if (!*ptr || *ptr == '.')
  154. acpigen_emit_byte(ctx, '_');
  155. else
  156. acpigen_emit_byte(ctx, *ptr++);
  157. }
  158. }
  159. static void acpigen_emit_double_namestring(struct acpi_ctx *ctx,
  160. const char *name, int dotpos)
  161. {
  162. acpigen_emit_byte(ctx, DUAL_NAME_PREFIX);
  163. acpigen_emit_simple_namestring(ctx, name);
  164. acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]);
  165. }
  166. static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx,
  167. const char *name)
  168. {
  169. unsigned char *pathlen;
  170. int count = 0;
  171. acpigen_emit_byte(ctx, MULTI_NAME_PREFIX);
  172. pathlen = ctx->current;
  173. acpigen_emit_byte(ctx, 0);
  174. while (*name) {
  175. acpigen_emit_simple_namestring(ctx, name);
  176. /* find end or next entity */
  177. while (*name != '.' && *name)
  178. name++;
  179. /* forward to next */
  180. if (*name == '.')
  181. name++;
  182. count++;
  183. }
  184. *pathlen = count;
  185. }
  186. void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath)
  187. {
  188. int dotcount;
  189. int dotpos;
  190. int i;
  191. /* We can start with a '\' */
  192. if (*namepath == '\\') {
  193. acpigen_emit_byte(ctx, '\\');
  194. namepath++;
  195. }
  196. /* And there can be any number of '^' */
  197. while (*namepath == '^') {
  198. acpigen_emit_byte(ctx, '^');
  199. namepath++;
  200. }
  201. for (i = 0, dotcount = 0; namepath[i]; i++) {
  202. if (namepath[i] == '.') {
  203. dotcount++;
  204. dotpos = i;
  205. }
  206. }
  207. /* If we have only \\ or only ^* then we need to add a null name */
  208. if (!*namepath)
  209. acpigen_emit_byte(ctx, ZERO_OP);
  210. else if (dotcount == 0)
  211. acpigen_emit_simple_namestring(ctx, namepath);
  212. else if (dotcount == 1)
  213. acpigen_emit_double_namestring(ctx, namepath, dotpos);
  214. else
  215. acpigen_emit_multi_namestring(ctx, namepath);
  216. }
  217. void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath)
  218. {
  219. acpigen_emit_byte(ctx, NAME_OP);
  220. acpigen_emit_namestring(ctx, namepath);
  221. }
  222. void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope)
  223. {
  224. acpigen_emit_byte(ctx, SCOPE_OP);
  225. acpigen_write_len_f(ctx);
  226. acpigen_emit_namestring(ctx, scope);
  227. }
  228. static void acpigen_write_method_internal(struct acpi_ctx *ctx,
  229. const char *name, uint flags)
  230. {
  231. acpigen_emit_byte(ctx, METHOD_OP);
  232. acpigen_write_len_f(ctx);
  233. acpigen_emit_namestring(ctx, name);
  234. acpigen_emit_byte(ctx, flags);
  235. }
  236. /* Method (name, nargs, NotSerialized) */
  237. void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs)
  238. {
  239. acpigen_write_method_internal(ctx, name,
  240. nargs & ACPI_METHOD_NARGS_MASK);
  241. }
  242. /* Method (name, nargs, Serialized) */
  243. void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
  244. int nargs)
  245. {
  246. acpigen_write_method_internal(ctx, name,
  247. (nargs & ACPI_METHOD_NARGS_MASK) |
  248. ACPI_METHOD_SERIALIZED_MASK);
  249. }
  250. void acpigen_write_sta(struct acpi_ctx *ctx, uint status)
  251. {
  252. /* Method (_STA, 0, NotSerialized) { Return (status) } */
  253. acpigen_write_method(ctx, "_STA", 0);
  254. acpigen_emit_byte(ctx, RETURN_OP);
  255. acpigen_write_byte(ctx, status);
  256. acpigen_pop_len(ctx);
  257. }
  258. /*
  259. * ToUUID(uuid)
  260. *
  261. * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the
  262. * bytes that make up a UUID Buffer object:
  263. *
  264. * UUID byte order for input to this function:
  265. * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
  266. *
  267. * UUID byte order output by this function:
  268. * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
  269. */
  270. int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
  271. {
  272. u8 buf[UUID_BIN_LEN];
  273. int ret;
  274. /* Parse UUID string into bytes */
  275. ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
  276. if (ret)
  277. return log_msg_ret("bad hex", -EINVAL);
  278. /* BufferOp */
  279. acpigen_emit_byte(ctx, BUFFER_OP);
  280. acpigen_write_len_f(ctx);
  281. /* Buffer length in bytes */
  282. acpigen_write_word(ctx, UUID_BIN_LEN);
  283. /* Output UUID in expected order */
  284. acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN);
  285. acpigen_pop_len(ctx);
  286. return 0;
  287. }
  288. void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
  289. uint order, const char *const dev_states[],
  290. size_t dev_states_count)
  291. {
  292. size_t i;
  293. for (i = 0; i < dev_states_count; i++) {
  294. acpigen_write_name(ctx, dev_states[i]);
  295. acpigen_write_package(ctx, 1);
  296. acpigen_emit_simple_namestring(ctx, name);
  297. acpigen_pop_len(ctx); /* Package */
  298. }
  299. acpigen_emit_ext_op(ctx, POWER_RES_OP);
  300. acpigen_write_len_f(ctx);
  301. acpigen_emit_simple_namestring(ctx, name);
  302. acpigen_emit_byte(ctx, level);
  303. acpigen_emit_word(ctx, order);
  304. }
  305. /* Sleep (ms) */
  306. void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
  307. {
  308. acpigen_emit_ext_op(ctx, SLEEP_OP);
  309. acpigen_write_integer(ctx, sleep_ms);
  310. }
  311. void acpigen_write_store(struct acpi_ctx *ctx)
  312. {
  313. acpigen_emit_byte(ctx, STORE_OP);
  314. }
  315. /* Or (arg1, arg2, res) */
  316. void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
  317. {
  318. acpigen_emit_byte(ctx, OR_OP);
  319. acpigen_emit_byte(ctx, arg1);
  320. acpigen_emit_byte(ctx, arg2);
  321. acpigen_emit_byte(ctx, res);
  322. }
  323. /* And (arg1, arg2, res) */
  324. void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
  325. {
  326. acpigen_emit_byte(ctx, AND_OP);
  327. acpigen_emit_byte(ctx, arg1);
  328. acpigen_emit_byte(ctx, arg2);
  329. acpigen_emit_byte(ctx, res);
  330. }
  331. /* Not (arg, res) */
  332. void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res)
  333. {
  334. acpigen_emit_byte(ctx, NOT_OP);
  335. acpigen_emit_byte(ctx, arg);
  336. acpigen_emit_byte(ctx, res);
  337. }
  338. /* Store (str, DEBUG) */
  339. void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
  340. {
  341. acpigen_write_store(ctx);
  342. acpigen_write_string(ctx, str);
  343. acpigen_emit_ext_op(ctx, DEBUG_OP);
  344. }
  345. /**
  346. * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
  347. *
  348. * Store (\_SB.GPC0 (addr), Local5)
  349. *
  350. * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in
  351. * the board's gpiolib.asl
  352. *
  353. * The value needs to be stored in a local variable so that it can be used in
  354. * expressions in the ACPI code.
  355. *
  356. * @ctx: ACPI context pointer
  357. * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0"
  358. * @addr: GPIO pin configuration register address
  359. *
  360. */
  361. static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx,
  362. const char *dw0_read, ulong addr)
  363. {
  364. acpigen_write_store(ctx);
  365. acpigen_emit_namestring(ctx, dw0_read);
  366. acpigen_write_integer(ctx, addr);
  367. acpigen_emit_byte(ctx, LOCAL5_OP);
  368. }
  369. /**
  370. * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off
  371. *
  372. * @ctx: ACPI context pointer
  373. * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
  374. * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
  375. * @gpio_num: GPIO number to adjust
  376. * @vaL: true to set on, false to set off
  377. */
  378. static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val,
  379. const char *dw0_read, const char *dw0_write,
  380. struct acpi_gpio *gpio, bool val)
  381. {
  382. acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr);
  383. /* Store (0x40, Local0) */
  384. acpigen_write_store(ctx);
  385. acpigen_write_integer(ctx, tx_state_val);
  386. acpigen_emit_byte(ctx, LOCAL0_OP);
  387. if (val) {
  388. /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */
  389. acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP);
  390. } else {
  391. /* Not (PAD_CFG0_TX_STATE, Local6) */
  392. acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP);
  393. /* And (Local5, Local6, Local5) */
  394. acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP);
  395. }
  396. /*
  397. * \_SB.SPC0 (addr, Local5)
  398. * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in
  399. * gpiolib.asl.
  400. */
  401. acpigen_emit_namestring(ctx, dw0_write);
  402. acpigen_write_integer(ctx, gpio->pin0_addr);
  403. acpigen_emit_byte(ctx, LOCAL5_OP);
  404. return 0;
  405. }
  406. int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
  407. const char *dw0_read, const char *dw0_write,
  408. struct acpi_gpio *gpio, bool enable)
  409. {
  410. bool set;
  411. int ret;
  412. set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable;
  413. ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio,
  414. set);
  415. if (ret)
  416. return log_msg_ret("call", ret);
  417. return 0;
  418. }