acpigen.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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 <acpi/acpi_table.h>
  16. #include <dm/acpi.h>
  17. /* CPU path format */
  18. #define ACPI_CPU_STRING "\\_PR.CP%02d"
  19. u8 *acpigen_get_current(struct acpi_ctx *ctx)
  20. {
  21. return ctx->current;
  22. }
  23. void acpigen_emit_byte(struct acpi_ctx *ctx, uint data)
  24. {
  25. *(u8 *)ctx->current++ = data;
  26. }
  27. void acpigen_emit_word(struct acpi_ctx *ctx, uint data)
  28. {
  29. acpigen_emit_byte(ctx, data & 0xff);
  30. acpigen_emit_byte(ctx, (data >> 8) & 0xff);
  31. }
  32. void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
  33. {
  34. /* Output the value in little-endian format */
  35. acpigen_emit_byte(ctx, data & 0xff);
  36. acpigen_emit_byte(ctx, (data >> 8) & 0xff);
  37. acpigen_emit_byte(ctx, (data >> 16) & 0xff);
  38. acpigen_emit_byte(ctx, (data >> 24) & 0xff);
  39. }
  40. /*
  41. * Maximum length for an ACPI object generated by this code,
  42. *
  43. * If you need to change this, change acpigen_write_len_f(ctx) and
  44. * acpigen_pop_len(ctx)
  45. */
  46. #define ACPIGEN_MAXLEN 0xfffff
  47. void acpigen_write_len_f(struct acpi_ctx *ctx)
  48. {
  49. assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
  50. ctx->len_stack[ctx->ltop++] = ctx->current;
  51. acpigen_emit_byte(ctx, 0);
  52. acpigen_emit_byte(ctx, 0);
  53. acpigen_emit_byte(ctx, 0);
  54. }
  55. void acpigen_pop_len(struct acpi_ctx *ctx)
  56. {
  57. int len;
  58. char *p;
  59. assert(ctx->ltop > 0);
  60. p = ctx->len_stack[--ctx->ltop];
  61. len = ctx->current - (void *)p;
  62. assert(len <= ACPIGEN_MAXLEN);
  63. /* generate store length for 0xfffff max */
  64. p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
  65. p[1] = len >> 4 & 0xff;
  66. p[2] = len >> 12 & 0xff;
  67. }
  68. void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op)
  69. {
  70. acpigen_emit_byte(ctx, EXT_OP_PREFIX);
  71. acpigen_emit_byte(ctx, op);
  72. }
  73. char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
  74. {
  75. char *p;
  76. acpigen_emit_byte(ctx, PACKAGE_OP);
  77. acpigen_write_len_f(ctx);
  78. p = ctx->current;
  79. acpigen_emit_byte(ctx, nr_el);
  80. return p;
  81. }
  82. void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data)
  83. {
  84. acpigen_emit_byte(ctx, BYTE_PREFIX);
  85. acpigen_emit_byte(ctx, data & 0xff);
  86. }
  87. void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data)
  88. {
  89. acpigen_emit_byte(ctx, WORD_PREFIX);
  90. acpigen_emit_word(ctx, data);
  91. }
  92. void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data)
  93. {
  94. acpigen_emit_byte(ctx, DWORD_PREFIX);
  95. acpigen_emit_dword(ctx, data);
  96. }
  97. void acpigen_write_qword(struct acpi_ctx *ctx, u64 data)
  98. {
  99. acpigen_emit_byte(ctx, QWORD_PREFIX);
  100. acpigen_emit_dword(ctx, data & 0xffffffff);
  101. acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff);
  102. }
  103. void acpigen_write_zero(struct acpi_ctx *ctx)
  104. {
  105. acpigen_emit_byte(ctx, ZERO_OP);
  106. }
  107. void acpigen_write_one(struct acpi_ctx *ctx)
  108. {
  109. acpigen_emit_byte(ctx, ONE_OP);
  110. }
  111. void acpigen_write_integer(struct acpi_ctx *ctx, u64 data)
  112. {
  113. if (data == 0)
  114. acpigen_write_zero(ctx);
  115. else if (data == 1)
  116. acpigen_write_one(ctx);
  117. else if (data <= 0xff)
  118. acpigen_write_byte(ctx, (unsigned char)data);
  119. else if (data <= 0xffff)
  120. acpigen_write_word(ctx, (unsigned int)data);
  121. else if (data <= 0xffffffff)
  122. acpigen_write_dword(ctx, (unsigned int)data);
  123. else
  124. acpigen_write_qword(ctx, data);
  125. }
  126. void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name)
  127. {
  128. acpigen_write_name(ctx, name);
  129. acpigen_write_zero(ctx);
  130. }
  131. void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name)
  132. {
  133. acpigen_write_name(ctx, name);
  134. acpigen_write_one(ctx);
  135. }
  136. void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val)
  137. {
  138. acpigen_write_name(ctx, name);
  139. acpigen_write_byte(ctx, val);
  140. }
  141. void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val)
  142. {
  143. acpigen_write_name(ctx, name);
  144. acpigen_write_word(ctx, val);
  145. }
  146. void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val)
  147. {
  148. acpigen_write_name(ctx, name);
  149. acpigen_write_dword(ctx, val);
  150. }
  151. void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val)
  152. {
  153. acpigen_write_name(ctx, name);
  154. acpigen_write_qword(ctx, val);
  155. }
  156. void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name, u64 val)
  157. {
  158. acpigen_write_name(ctx, name);
  159. acpigen_write_integer(ctx, val);
  160. }
  161. void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
  162. const char *string)
  163. {
  164. acpigen_write_name(ctx, name);
  165. acpigen_write_string(ctx, string);
  166. }
  167. void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
  168. {
  169. int i;
  170. for (i = 0; i < size; i++)
  171. acpigen_emit_byte(ctx, data[i]);
  172. }
  173. void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
  174. {
  175. acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
  176. acpigen_emit_byte(ctx, '\0');
  177. }
  178. void acpigen_write_string(struct acpi_ctx *ctx, const char *str)
  179. {
  180. acpigen_emit_byte(ctx, STRING_PREFIX);
  181. acpigen_emit_string(ctx, str);
  182. }
  183. /*
  184. * The naming conventions for ACPI namespace names are a bit tricky as
  185. * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
  186. * and "By convention, when an ASL compiler pads a name shorter than 4
  187. * characters, it is done so with trailing underscores ('_')".
  188. *
  189. * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details.
  190. */
  191. static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx,
  192. const char *name)
  193. {
  194. const char *ptr;
  195. int i;
  196. for (i = 0, ptr = name; i < 4; i++) {
  197. if (!*ptr || *ptr == '.')
  198. acpigen_emit_byte(ctx, '_');
  199. else
  200. acpigen_emit_byte(ctx, *ptr++);
  201. }
  202. }
  203. static void acpigen_emit_double_namestring(struct acpi_ctx *ctx,
  204. const char *name, int dotpos)
  205. {
  206. acpigen_emit_byte(ctx, DUAL_NAME_PREFIX);
  207. acpigen_emit_simple_namestring(ctx, name);
  208. acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]);
  209. }
  210. static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx,
  211. const char *name)
  212. {
  213. unsigned char *pathlen;
  214. int count = 0;
  215. acpigen_emit_byte(ctx, MULTI_NAME_PREFIX);
  216. pathlen = ctx->current;
  217. acpigen_emit_byte(ctx, 0);
  218. while (*name) {
  219. acpigen_emit_simple_namestring(ctx, name);
  220. /* find end or next entity */
  221. while (*name != '.' && *name)
  222. name++;
  223. /* forward to next */
  224. if (*name == '.')
  225. name++;
  226. count++;
  227. }
  228. *pathlen = count;
  229. }
  230. void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath)
  231. {
  232. int dotcount;
  233. int dotpos;
  234. int i;
  235. /* We can start with a '\' */
  236. if (*namepath == '\\') {
  237. acpigen_emit_byte(ctx, '\\');
  238. namepath++;
  239. }
  240. /* And there can be any number of '^' */
  241. while (*namepath == '^') {
  242. acpigen_emit_byte(ctx, '^');
  243. namepath++;
  244. }
  245. for (i = 0, dotcount = 0; namepath[i]; i++) {
  246. if (namepath[i] == '.') {
  247. dotcount++;
  248. dotpos = i;
  249. }
  250. }
  251. /* If we have only \\ or only ^* then we need to add a null name */
  252. if (!*namepath)
  253. acpigen_emit_byte(ctx, ZERO_OP);
  254. else if (dotcount == 0)
  255. acpigen_emit_simple_namestring(ctx, namepath);
  256. else if (dotcount == 1)
  257. acpigen_emit_double_namestring(ctx, namepath, dotpos);
  258. else
  259. acpigen_emit_multi_namestring(ctx, namepath);
  260. }
  261. void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath)
  262. {
  263. acpigen_emit_byte(ctx, NAME_OP);
  264. acpigen_emit_namestring(ctx, namepath);
  265. }
  266. void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope)
  267. {
  268. acpigen_emit_byte(ctx, SCOPE_OP);
  269. acpigen_write_len_f(ctx);
  270. acpigen_emit_namestring(ctx, scope);
  271. }
  272. static void acpigen_write_method_internal(struct acpi_ctx *ctx,
  273. const char *name, uint flags)
  274. {
  275. acpigen_emit_byte(ctx, METHOD_OP);
  276. acpigen_write_len_f(ctx);
  277. acpigen_emit_namestring(ctx, name);
  278. acpigen_emit_byte(ctx, flags);
  279. }
  280. /* Method (name, nargs, NotSerialized) */
  281. void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs)
  282. {
  283. acpigen_write_method_internal(ctx, name,
  284. nargs & ACPI_METHOD_NARGS_MASK);
  285. }
  286. /* Method (name, nargs, Serialized) */
  287. void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
  288. int nargs)
  289. {
  290. acpigen_write_method_internal(ctx, name,
  291. (nargs & ACPI_METHOD_NARGS_MASK) |
  292. ACPI_METHOD_SERIALIZED_MASK);
  293. }
  294. void acpigen_write_processor(struct acpi_ctx *ctx, uint cpuindex,
  295. u32 pblock_addr, uint pblock_len)
  296. {
  297. /*
  298. * Processor (\_PR.CPnn, cpuindex, pblock_addr, pblock_len)
  299. * {
  300. */
  301. char pscope[16];
  302. acpigen_emit_ext_op(ctx, PROCESSOR_OP);
  303. acpigen_write_len_f(ctx);
  304. snprintf(pscope, sizeof(pscope), ACPI_CPU_STRING, cpuindex);
  305. acpigen_emit_namestring(ctx, pscope);
  306. acpigen_emit_byte(ctx, cpuindex);
  307. acpigen_emit_dword(ctx, pblock_addr);
  308. acpigen_emit_byte(ctx, pblock_len);
  309. }
  310. void acpigen_write_processor_package(struct acpi_ctx *ctx,
  311. const char *const name,
  312. const uint first_core,
  313. const uint core_count)
  314. {
  315. uint i;
  316. char pscope[16];
  317. acpigen_write_name(ctx, name);
  318. acpigen_write_package(ctx, core_count);
  319. for (i = first_core; i < first_core + core_count; ++i) {
  320. snprintf(pscope, sizeof(pscope), ACPI_CPU_STRING, i);
  321. acpigen_emit_namestring(ctx, pscope);
  322. }
  323. acpigen_pop_len(ctx);
  324. }
  325. void acpigen_write_processor_cnot(struct acpi_ctx *ctx, const uint num_cores)
  326. {
  327. int core_id;
  328. acpigen_write_method(ctx, "\\_PR.CNOT", 1);
  329. for (core_id = 0; core_id < num_cores; core_id++) {
  330. char buffer[30];
  331. snprintf(buffer, sizeof(buffer), ACPI_CPU_STRING, core_id);
  332. acpigen_emit_byte(ctx, NOTIFY_OP);
  333. acpigen_emit_namestring(ctx, buffer);
  334. acpigen_emit_byte(ctx, ARG0_OP);
  335. }
  336. acpigen_pop_len(ctx);
  337. }
  338. void acpigen_write_device(struct acpi_ctx *ctx, const char *name)
  339. {
  340. acpigen_emit_ext_op(ctx, DEVICE_OP);
  341. acpigen_write_len_f(ctx);
  342. acpigen_emit_namestring(ctx, name);
  343. }
  344. void acpigen_write_sta(struct acpi_ctx *ctx, uint status)
  345. {
  346. /* Method (_STA, 0, NotSerialized) { Return (status) } */
  347. acpigen_write_method(ctx, "_STA", 0);
  348. acpigen_emit_byte(ctx, RETURN_OP);
  349. acpigen_write_byte(ctx, status);
  350. acpigen_pop_len(ctx);
  351. }
  352. static void acpigen_write_register(struct acpi_ctx *ctx,
  353. const struct acpi_gen_regaddr *addr)
  354. {
  355. /* See ACPI v6.3 section 6.4.3.7: Generic Register Descriptor */
  356. acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_REGISTER);
  357. acpigen_emit_byte(ctx, 0x0c); /* Register Length 7:0 */
  358. acpigen_emit_byte(ctx, 0x00); /* Register Length 15:8 */
  359. acpigen_emit_byte(ctx, addr->space_id);
  360. acpigen_emit_byte(ctx, addr->bit_width);
  361. acpigen_emit_byte(ctx, addr->bit_offset);
  362. acpigen_emit_byte(ctx, addr->access_size);
  363. acpigen_emit_dword(ctx, addr->addrl);
  364. acpigen_emit_dword(ctx, addr->addrh);
  365. }
  366. void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx)
  367. {
  368. /*
  369. * A ResourceTemplate() is a Buffer() with a
  370. * (Byte|Word|DWord) containing the length, followed by one or more
  371. * resource items, terminated by the end tag.
  372. * (small item 0xf, len 1)
  373. */
  374. acpigen_emit_byte(ctx, BUFFER_OP);
  375. acpigen_write_len_f(ctx);
  376. acpigen_emit_byte(ctx, WORD_PREFIX);
  377. ctx->len_stack[ctx->ltop++] = ctx->current;
  378. /*
  379. * Add two dummy bytes for the ACPI word (keep aligned with the
  380. * calculation in acpigen_write_resourcetemplate_footer() below)
  381. */
  382. acpigen_emit_byte(ctx, 0x00);
  383. acpigen_emit_byte(ctx, 0x00);
  384. }
  385. void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx)
  386. {
  387. char *p = ctx->len_stack[--ctx->ltop];
  388. int len;
  389. /*
  390. * See ACPI v6.3 section 6.4.2.9: End Tag
  391. * 0x79 <checksum>
  392. * 0x00 is treated as a good checksum according to the spec
  393. * and is what iasl generates.
  394. */
  395. acpigen_emit_byte(ctx, ACPI_END_TAG);
  396. acpigen_emit_byte(ctx, 0x00);
  397. /*
  398. * Start counting past the 2-bytes length added in
  399. * acpigen_write_resourcetemplate_header() above
  400. */
  401. len = (char *)ctx->current - (p + 2);
  402. /* patch len word */
  403. p[0] = len & 0xff;
  404. p[1] = (len >> 8) & 0xff;
  405. acpigen_pop_len(ctx);
  406. }
  407. void acpigen_write_register_resource(struct acpi_ctx *ctx,
  408. const struct acpi_gen_regaddr *addr)
  409. {
  410. acpigen_write_resourcetemplate_header(ctx);
  411. acpigen_write_register(ctx, addr);
  412. acpigen_write_resourcetemplate_footer(ctx);
  413. }
  414. void acpigen_write_ppc(struct acpi_ctx *ctx, uint num_pstates)
  415. {
  416. /*
  417. * Method (_PPC, 0, NotSerialized)
  418. * {
  419. * Return (num_pstates)
  420. * }
  421. */
  422. acpigen_write_method(ctx, "_PPC", 0);
  423. acpigen_emit_byte(ctx, RETURN_OP);
  424. acpigen_write_byte(ctx, num_pstates);
  425. acpigen_pop_len(ctx);
  426. }
  427. /*
  428. * Generates a func with max supported P-states saved
  429. * in the variable PPCM.
  430. */
  431. void acpigen_write_ppc_nvs(struct acpi_ctx *ctx)
  432. {
  433. /*
  434. * Method (_PPC, 0, NotSerialized)
  435. * {
  436. * Return (PPCM)
  437. * }
  438. */
  439. acpigen_write_method(ctx, "_PPC", 0);
  440. acpigen_emit_byte(ctx, RETURN_OP);
  441. acpigen_emit_namestring(ctx, "PPCM");
  442. acpigen_pop_len(ctx);
  443. }
  444. void acpigen_write_tpc(struct acpi_ctx *ctx, const char *gnvs_tpc_limit)
  445. {
  446. /*
  447. * // Sample _TPC method
  448. * Method (_TPC, 0, NotSerialized)
  449. * {
  450. * Return (\TLVL)
  451. * }
  452. */
  453. acpigen_write_method(ctx, "_TPC", 0);
  454. acpigen_emit_byte(ctx, RETURN_OP);
  455. acpigen_emit_namestring(ctx, gnvs_tpc_limit);
  456. acpigen_pop_len(ctx);
  457. }
  458. void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level)
  459. {
  460. /* Name (_PRW, Package () { wake, level } */
  461. acpigen_write_name(ctx, "_PRW");
  462. acpigen_write_package(ctx, 2);
  463. acpigen_write_integer(ctx, wake);
  464. acpigen_write_integer(ctx, level);
  465. acpigen_pop_len(ctx);
  466. }
  467. void acpigen_write_pss_package(struct acpi_ctx *ctx, u32 core_freq, u32 power,
  468. u32 trans_lat, u32 busm_lat, u32 control,
  469. u32 status)
  470. {
  471. acpigen_write_package(ctx, 6);
  472. acpigen_write_dword(ctx, core_freq);
  473. acpigen_write_dword(ctx, power);
  474. acpigen_write_dword(ctx, trans_lat);
  475. acpigen_write_dword(ctx, busm_lat);
  476. acpigen_write_dword(ctx, control);
  477. acpigen_write_dword(ctx, status);
  478. acpigen_pop_len(ctx);
  479. log_debug("PSS: %uMHz power %u control 0x%x status 0x%x\n",
  480. core_freq, power, control, status);
  481. }
  482. void acpigen_write_psd_package(struct acpi_ctx *ctx, uint domain, uint numprocs,
  483. enum psd_coord coordtype)
  484. {
  485. acpigen_write_name(ctx, "_PSD");
  486. acpigen_write_package(ctx, 1);
  487. acpigen_write_package(ctx, 5);
  488. acpigen_write_byte(ctx, 5); // 5 values
  489. acpigen_write_byte(ctx, 0); // revision 0
  490. acpigen_write_dword(ctx, domain);
  491. acpigen_write_dword(ctx, coordtype);
  492. acpigen_write_dword(ctx, numprocs);
  493. acpigen_pop_len(ctx);
  494. acpigen_pop_len(ctx);
  495. }
  496. static void acpigen_write_cst_package_entry(struct acpi_ctx *ctx,
  497. const struct acpi_cstate *cstate)
  498. {
  499. acpigen_write_package(ctx, 4);
  500. acpigen_write_register_resource(ctx, &cstate->resource);
  501. acpigen_write_dword(ctx, cstate->ctype);
  502. acpigen_write_dword(ctx, cstate->latency);
  503. acpigen_write_dword(ctx, cstate->power);
  504. acpigen_pop_len(ctx);
  505. }
  506. void acpigen_write_cst_package(struct acpi_ctx *ctx,
  507. const struct acpi_cstate *cstate, int nentries)
  508. {
  509. int i;
  510. acpigen_write_name(ctx, "_CST");
  511. acpigen_write_package(ctx, nentries + 1);
  512. acpigen_write_dword(ctx, nentries);
  513. for (i = 0; i < nentries; i++)
  514. acpigen_write_cst_package_entry(ctx, cstate + i);
  515. acpigen_pop_len(ctx);
  516. }
  517. void acpigen_write_csd_package(struct acpi_ctx *ctx, uint domain, uint numprocs,
  518. enum csd_coord coordtype, uint index)
  519. {
  520. acpigen_write_name(ctx, "_CSD");
  521. acpigen_write_package(ctx, 1);
  522. acpigen_write_package(ctx, 6);
  523. acpigen_write_byte(ctx, 6); // 6 values
  524. acpigen_write_byte(ctx, 0); // revision 0
  525. acpigen_write_dword(ctx, domain);
  526. acpigen_write_dword(ctx, coordtype);
  527. acpigen_write_dword(ctx, numprocs);
  528. acpigen_write_dword(ctx, index);
  529. acpigen_pop_len(ctx);
  530. acpigen_pop_len(ctx);
  531. }
  532. void acpigen_write_tss_package(struct acpi_ctx *ctx,
  533. struct acpi_tstate *entry, int nentries)
  534. {
  535. /*
  536. * Sample _TSS package with 100% and 50% duty cycles
  537. * Name (_TSS, Package (0x02)
  538. * {
  539. * Package(){100, 1000, 0, 0x00, 0)
  540. * Package(){50, 520, 0, 0x18, 0)
  541. * })
  542. */
  543. struct acpi_tstate *tstate = entry;
  544. int i;
  545. acpigen_write_name(ctx, "_TSS");
  546. acpigen_write_package(ctx, nentries);
  547. for (i = 0; i < nentries; i++) {
  548. acpigen_write_package(ctx, 5);
  549. acpigen_write_dword(ctx, tstate->percent);
  550. acpigen_write_dword(ctx, tstate->power);
  551. acpigen_write_dword(ctx, tstate->latency);
  552. acpigen_write_dword(ctx, tstate->control);
  553. acpigen_write_dword(ctx, tstate->status);
  554. acpigen_pop_len(ctx);
  555. tstate++;
  556. }
  557. acpigen_pop_len(ctx);
  558. }
  559. void acpigen_write_tsd_package(struct acpi_ctx *ctx, u32 domain, u32 numprocs,
  560. enum psd_coord coordtype)
  561. {
  562. acpigen_write_name(ctx, "_TSD");
  563. acpigen_write_package(ctx, 1);
  564. acpigen_write_package(ctx, 5);
  565. acpigen_write_byte(ctx, 5); // 5 values
  566. acpigen_write_byte(ctx, 0); // revision 0
  567. acpigen_write_dword(ctx, domain);
  568. acpigen_write_dword(ctx, coordtype);
  569. acpigen_write_dword(ctx, numprocs);
  570. acpigen_pop_len(ctx);
  571. acpigen_pop_len(ctx);
  572. }
  573. /*
  574. * ToUUID(uuid)
  575. *
  576. * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the
  577. * bytes that make up a UUID Buffer object:
  578. *
  579. * UUID byte order for input to this function:
  580. * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
  581. *
  582. * UUID byte order output by this function:
  583. * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
  584. */
  585. int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
  586. {
  587. u8 buf[UUID_BIN_LEN];
  588. int ret;
  589. /* Parse UUID string into bytes */
  590. ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
  591. if (ret)
  592. return log_msg_ret("bad hex", -EINVAL);
  593. /* BufferOp */
  594. acpigen_emit_byte(ctx, BUFFER_OP);
  595. acpigen_write_len_f(ctx);
  596. /* Buffer length in bytes */
  597. acpigen_write_word(ctx, UUID_BIN_LEN);
  598. /* Output UUID in expected order */
  599. acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN);
  600. acpigen_pop_len(ctx);
  601. return 0;
  602. }
  603. void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
  604. uint order, const char *const dev_states[],
  605. size_t dev_states_count)
  606. {
  607. size_t i;
  608. for (i = 0; i < dev_states_count; i++) {
  609. acpigen_write_name(ctx, dev_states[i]);
  610. acpigen_write_package(ctx, 1);
  611. acpigen_emit_simple_namestring(ctx, name);
  612. acpigen_pop_len(ctx); /* Package */
  613. }
  614. acpigen_emit_ext_op(ctx, POWER_RES_OP);
  615. acpigen_write_len_f(ctx);
  616. acpigen_emit_simple_namestring(ctx, name);
  617. acpigen_emit_byte(ctx, level);
  618. acpigen_emit_word(ctx, order);
  619. }
  620. /* Sleep (ms) */
  621. void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
  622. {
  623. acpigen_emit_ext_op(ctx, SLEEP_OP);
  624. acpigen_write_integer(ctx, sleep_ms);
  625. }
  626. void acpigen_write_store(struct acpi_ctx *ctx)
  627. {
  628. acpigen_emit_byte(ctx, STORE_OP);
  629. }
  630. /* Or (arg1, arg2, res) */
  631. void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
  632. {
  633. acpigen_emit_byte(ctx, OR_OP);
  634. acpigen_emit_byte(ctx, arg1);
  635. acpigen_emit_byte(ctx, arg2);
  636. acpigen_emit_byte(ctx, res);
  637. }
  638. /* And (arg1, arg2, res) */
  639. void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
  640. {
  641. acpigen_emit_byte(ctx, AND_OP);
  642. acpigen_emit_byte(ctx, arg1);
  643. acpigen_emit_byte(ctx, arg2);
  644. acpigen_emit_byte(ctx, res);
  645. }
  646. /* Not (arg, res) */
  647. void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res)
  648. {
  649. acpigen_emit_byte(ctx, NOT_OP);
  650. acpigen_emit_byte(ctx, arg);
  651. acpigen_emit_byte(ctx, res);
  652. }
  653. /* Store (str, DEBUG) */
  654. void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
  655. {
  656. acpigen_write_store(ctx);
  657. acpigen_write_string(ctx, str);
  658. acpigen_emit_ext_op(ctx, DEBUG_OP);
  659. }
  660. void acpigen_write_if(struct acpi_ctx *ctx)
  661. {
  662. acpigen_emit_byte(ctx, IF_OP);
  663. acpigen_write_len_f(ctx);
  664. }
  665. void acpigen_write_if_lequal_op_int(struct acpi_ctx *ctx, uint op, u64 val)
  666. {
  667. acpigen_write_if(ctx);
  668. acpigen_emit_byte(ctx, LEQUAL_OP);
  669. acpigen_emit_byte(ctx, op);
  670. acpigen_write_integer(ctx, val);
  671. }
  672. void acpigen_write_else(struct acpi_ctx *ctx)
  673. {
  674. acpigen_emit_byte(ctx, ELSE_OP);
  675. acpigen_write_len_f(ctx);
  676. }
  677. void acpigen_write_to_buffer(struct acpi_ctx *ctx, uint src, uint dst)
  678. {
  679. acpigen_emit_byte(ctx, TO_BUFFER_OP);
  680. acpigen_emit_byte(ctx, src);
  681. acpigen_emit_byte(ctx, dst);
  682. }
  683. void acpigen_write_to_integer(struct acpi_ctx *ctx, uint src, uint dst)
  684. {
  685. acpigen_emit_byte(ctx, TO_INTEGER_OP);
  686. acpigen_emit_byte(ctx, src);
  687. acpigen_emit_byte(ctx, dst);
  688. }
  689. void acpigen_write_byte_buffer(struct acpi_ctx *ctx, u8 *arr, size_t size)
  690. {
  691. size_t i;
  692. acpigen_emit_byte(ctx, BUFFER_OP);
  693. acpigen_write_len_f(ctx);
  694. acpigen_write_integer(ctx, size);
  695. for (i = 0; i < size; i++)
  696. acpigen_emit_byte(ctx, arr[i]);
  697. acpigen_pop_len(ctx);
  698. }
  699. void acpigen_write_return_byte_buffer(struct acpi_ctx *ctx, u8 *arr,
  700. size_t size)
  701. {
  702. acpigen_emit_byte(ctx, RETURN_OP);
  703. acpigen_write_byte_buffer(ctx, arr, size);
  704. }
  705. void acpigen_write_return_singleton_buffer(struct acpi_ctx *ctx, uint arg)
  706. {
  707. u8 buf = arg;
  708. acpigen_write_return_byte_buffer(ctx, &buf, 1);
  709. }
  710. void acpigen_write_return_byte(struct acpi_ctx *ctx, uint arg)
  711. {
  712. acpigen_emit_byte(ctx, RETURN_OP);
  713. acpigen_write_byte(ctx, arg);
  714. }
  715. void acpigen_write_dsm_start(struct acpi_ctx *ctx)
  716. {
  717. /* Method (_DSM, 4, Serialized) */
  718. acpigen_write_method_serialized(ctx, "_DSM", 4);
  719. /* ToBuffer (Arg0, Local0) */
  720. acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP);
  721. }
  722. int acpigen_write_dsm_uuid_start(struct acpi_ctx *ctx, const char *uuid)
  723. {
  724. int ret;
  725. /* If (LEqual (Local0, ToUUID(uuid))) */
  726. acpigen_write_if(ctx);
  727. acpigen_emit_byte(ctx, LEQUAL_OP);
  728. acpigen_emit_byte(ctx, LOCAL0_OP);
  729. ret = acpigen_write_uuid(ctx, uuid);
  730. if (ret)
  731. return log_msg_ret("uuid", ret);
  732. /* ToInteger (Arg2, Local1) */
  733. acpigen_write_to_integer(ctx, ARG2_OP, LOCAL1_OP);
  734. return 0;
  735. }
  736. void acpigen_write_dsm_uuid_start_cond(struct acpi_ctx *ctx, int seq)
  737. {
  738. /* If (LEqual (Local1, i)) */
  739. acpigen_write_if_lequal_op_int(ctx, LOCAL1_OP, seq);
  740. }
  741. void acpigen_write_dsm_uuid_end_cond(struct acpi_ctx *ctx)
  742. {
  743. acpigen_pop_len(ctx); /* If */
  744. }
  745. void acpigen_write_dsm_uuid_end(struct acpi_ctx *ctx)
  746. {
  747. /* Default case: Return (Buffer (One) { 0x0 }) */
  748. acpigen_write_return_singleton_buffer(ctx, 0x0);
  749. acpigen_pop_len(ctx); /* If (LEqual (Local0, ToUUID(uuid))) */
  750. }
  751. void acpigen_write_dsm_end(struct acpi_ctx *ctx)
  752. {
  753. /* Return (Buffer (One) { 0x0 }) */
  754. acpigen_write_return_singleton_buffer(ctx, 0x0);
  755. acpigen_pop_len(ctx); /* Method _DSM */
  756. }
  757. /**
  758. * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
  759. *
  760. * Store (\_SB.GPC0 (addr), Local5)
  761. *
  762. * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in
  763. * the board's gpiolib.asl
  764. *
  765. * The value needs to be stored in a local variable so that it can be used in
  766. * expressions in the ACPI code.
  767. *
  768. * @ctx: ACPI context pointer
  769. * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0"
  770. * @addr: GPIO pin configuration register address
  771. *
  772. */
  773. static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx,
  774. const char *dw0_read, ulong addr)
  775. {
  776. acpigen_write_store(ctx);
  777. acpigen_emit_namestring(ctx, dw0_read);
  778. acpigen_write_integer(ctx, addr);
  779. acpigen_emit_byte(ctx, LOCAL5_OP);
  780. }
  781. /**
  782. * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off
  783. *
  784. * @ctx: ACPI context pointer
  785. * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
  786. * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
  787. * @gpio_num: GPIO number to adjust
  788. * @vaL: true to set on, false to set off
  789. */
  790. static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val,
  791. const char *dw0_read, const char *dw0_write,
  792. struct acpi_gpio *gpio, bool val)
  793. {
  794. acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr);
  795. /* Store (0x40, Local0) */
  796. acpigen_write_store(ctx);
  797. acpigen_write_integer(ctx, tx_state_val);
  798. acpigen_emit_byte(ctx, LOCAL0_OP);
  799. if (val) {
  800. /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */
  801. acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP);
  802. } else {
  803. /* Not (PAD_CFG0_TX_STATE, Local6) */
  804. acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP);
  805. /* And (Local5, Local6, Local5) */
  806. acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP);
  807. }
  808. /*
  809. * \_SB.SPC0 (addr, Local5)
  810. * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in
  811. * gpiolib.asl.
  812. */
  813. acpigen_emit_namestring(ctx, dw0_write);
  814. acpigen_write_integer(ctx, gpio->pin0_addr);
  815. acpigen_emit_byte(ctx, LOCAL5_OP);
  816. return 0;
  817. }
  818. int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
  819. const char *dw0_read, const char *dw0_write,
  820. struct acpi_gpio *gpio, bool enable)
  821. {
  822. bool set;
  823. int ret;
  824. set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable;
  825. ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio,
  826. set);
  827. if (ret)
  828. return log_msg_ret("call", ret);
  829. return 0;
  830. }