acpi_dp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generation of tables for particular device types
  4. *
  5. * Copyright 2019 Google LLC
  6. * Mostly taken from coreboot file acpi_device.c
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <uuid.h>
  13. #include <acpi/acpigen.h>
  14. #include <acpi/acpi_dp.h>
  15. #include <dm/acpi.h>
  16. static void acpi_dp_write_array(struct acpi_ctx *ctx,
  17. const struct acpi_dp *array);
  18. static void acpi_dp_write_value(struct acpi_ctx *ctx,
  19. const struct acpi_dp *prop)
  20. {
  21. switch (prop->type) {
  22. case ACPI_DP_TYPE_INTEGER:
  23. acpigen_write_integer(ctx, prop->integer);
  24. break;
  25. case ACPI_DP_TYPE_STRING:
  26. case ACPI_DP_TYPE_CHILD:
  27. acpigen_write_string(ctx, prop->string);
  28. break;
  29. case ACPI_DP_TYPE_REFERENCE:
  30. acpigen_emit_namestring(ctx, prop->string);
  31. break;
  32. case ACPI_DP_TYPE_ARRAY:
  33. acpi_dp_write_array(ctx, prop->array);
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. /* Package (2) { "prop->name", VALUE } */
  40. static void acpi_dp_write_property(struct acpi_ctx *ctx,
  41. const struct acpi_dp *prop)
  42. {
  43. acpigen_write_package(ctx, 2);
  44. acpigen_write_string(ctx, prop->name);
  45. acpi_dp_write_value(ctx, prop);
  46. acpigen_pop_len(ctx);
  47. }
  48. /* Write array of Device Properties */
  49. static void acpi_dp_write_array(struct acpi_ctx *ctx,
  50. const struct acpi_dp *array)
  51. {
  52. const struct acpi_dp *dp;
  53. char *pkg_count;
  54. /* Package element count determined as it is populated */
  55. pkg_count = acpigen_write_package(ctx, 0);
  56. /*
  57. * Only acpi_dp of type DP_TYPE_TABLE is allowed to be an array.
  58. * DP_TYPE_TABLE does not have a value to be written. Thus, start
  59. * the loop from next type in the array.
  60. */
  61. for (dp = array->next; dp; dp = dp->next) {
  62. acpi_dp_write_value(ctx, dp);
  63. (*pkg_count)++;
  64. }
  65. acpigen_pop_len(ctx);
  66. }
  67. static void acpi_dp_free(struct acpi_dp *dp)
  68. {
  69. assert(dp);
  70. while (dp) {
  71. struct acpi_dp *p = dp->next;
  72. switch (dp->type) {
  73. case ACPI_DP_TYPE_CHILD:
  74. acpi_dp_free(dp->child);
  75. break;
  76. case ACPI_DP_TYPE_ARRAY:
  77. acpi_dp_free(dp->array);
  78. break;
  79. default:
  80. break;
  81. }
  82. free(dp);
  83. dp = p;
  84. }
  85. }
  86. static int acpi_dp_write_internal(struct acpi_ctx *ctx, struct acpi_dp *table)
  87. {
  88. struct acpi_dp *dp, *prop;
  89. char *dp_count, *prop_count = NULL;
  90. int child_count = 0;
  91. int ret;
  92. assert(table);
  93. if (table->type != ACPI_DP_TYPE_TABLE)
  94. return 0;
  95. /* Name (name) */
  96. acpigen_write_name(ctx, table->name);
  97. /* Device Property list starts with the next entry */
  98. prop = table->next;
  99. /* Package (DP), default to assuming no properties or children */
  100. dp_count = acpigen_write_package(ctx, 0);
  101. /* Print base properties */
  102. for (dp = prop; dp; dp = dp->next) {
  103. if (dp->type == ACPI_DP_TYPE_CHILD) {
  104. child_count++;
  105. } else {
  106. /*
  107. * The UUID and package is only added when
  108. * we come across the first property. This
  109. * is to avoid creating a zero-length package
  110. * in situations where there are only children.
  111. */
  112. if (!prop_count) {
  113. *dp_count += 2;
  114. /* ToUUID (ACPI_DP_UUID) */
  115. ret = acpigen_write_uuid(ctx, ACPI_DP_UUID);
  116. if (ret)
  117. return log_msg_ret("touuid", ret);
  118. /*
  119. * Package (PROP), element count determined as
  120. * it is populated
  121. */
  122. prop_count = acpigen_write_package(ctx, 0);
  123. }
  124. (*prop_count)++;
  125. acpi_dp_write_property(ctx, dp);
  126. }
  127. }
  128. if (prop_count) {
  129. /* Package (PROP) length, if a package was written */
  130. acpigen_pop_len(ctx);
  131. }
  132. if (child_count) {
  133. /* Update DP package count to 2 or 4 */
  134. *dp_count += 2;
  135. /* ToUUID (ACPI_DP_CHILD_UUID) */
  136. ret = acpigen_write_uuid(ctx, ACPI_DP_CHILD_UUID);
  137. if (ret)
  138. return log_msg_ret("child uuid", ret);
  139. /* Print child pointer properties */
  140. acpigen_write_package(ctx, child_count);
  141. for (dp = prop; dp; dp = dp->next)
  142. if (dp->type == ACPI_DP_TYPE_CHILD)
  143. acpi_dp_write_property(ctx, dp);
  144. /* Package (CHILD) length */
  145. acpigen_pop_len(ctx);
  146. }
  147. /* Package (DP) length */
  148. acpigen_pop_len(ctx);
  149. /* Recursively parse children into separate tables */
  150. for (dp = prop; dp; dp = dp->next) {
  151. if (dp->type == ACPI_DP_TYPE_CHILD) {
  152. ret = acpi_dp_write_internal(ctx, dp->child);
  153. if (ret)
  154. return log_msg_ret("dp child", ret);
  155. }
  156. }
  157. return 0;
  158. }
  159. int acpi_dp_write(struct acpi_ctx *ctx, struct acpi_dp *table)
  160. {
  161. int ret;
  162. ret = acpi_dp_write_internal(ctx, table);
  163. /* Clean up */
  164. acpi_dp_free(table);
  165. if (ret)
  166. return log_msg_ret("write", ret);
  167. return 0;
  168. }
  169. static struct acpi_dp *acpi_dp_new(struct acpi_dp *dp, enum acpi_dp_type type,
  170. const char *name)
  171. {
  172. struct acpi_dp *new;
  173. new = malloc(sizeof(struct acpi_dp));
  174. if (!new)
  175. return NULL;
  176. memset(new, '\0', sizeof(*new));
  177. new->type = type;
  178. new->name = name;
  179. if (dp) {
  180. /* Add to end of property list */
  181. while (dp->next)
  182. dp = dp->next;
  183. dp->next = new;
  184. }
  185. return new;
  186. }
  187. struct acpi_dp *acpi_dp_new_table(const char *name)
  188. {
  189. return acpi_dp_new(NULL, ACPI_DP_TYPE_TABLE, name);
  190. }
  191. struct acpi_dp *acpi_dp_add_integer(struct acpi_dp *dp, const char *name,
  192. u64 value)
  193. {
  194. struct acpi_dp *new;
  195. assert(dp);
  196. new = acpi_dp_new(dp, ACPI_DP_TYPE_INTEGER, name);
  197. if (new)
  198. new->integer = value;
  199. return new;
  200. }
  201. struct acpi_dp *acpi_dp_add_string(struct acpi_dp *dp, const char *name,
  202. const char *string)
  203. {
  204. struct acpi_dp *new;
  205. assert(dp);
  206. new = acpi_dp_new(dp, ACPI_DP_TYPE_STRING, name);
  207. if (new)
  208. new->string = string;
  209. return new;
  210. }
  211. struct acpi_dp *acpi_dp_add_reference(struct acpi_dp *dp, const char *name,
  212. const char *reference)
  213. {
  214. struct acpi_dp *new;
  215. assert(dp);
  216. new = acpi_dp_new(dp, ACPI_DP_TYPE_REFERENCE, name);
  217. if (new)
  218. new->string = reference;
  219. return new;
  220. }
  221. struct acpi_dp *acpi_dp_add_child(struct acpi_dp *dp, const char *name,
  222. struct acpi_dp *child)
  223. {
  224. struct acpi_dp *new;
  225. assert(dp);
  226. if (child->type != ACPI_DP_TYPE_TABLE)
  227. return NULL;
  228. new = acpi_dp_new(dp, ACPI_DP_TYPE_CHILD, name);
  229. if (new) {
  230. new->child = child;
  231. new->string = child->name;
  232. }
  233. return new;
  234. }
  235. struct acpi_dp *acpi_dp_add_array(struct acpi_dp *dp, struct acpi_dp *array)
  236. {
  237. struct acpi_dp *new;
  238. assert(dp);
  239. assert(array);
  240. if (array->type != ACPI_DP_TYPE_TABLE)
  241. return NULL;
  242. new = acpi_dp_new(dp, ACPI_DP_TYPE_ARRAY, array->name);
  243. if (new)
  244. new->array = array;
  245. return new;
  246. }
  247. struct acpi_dp *acpi_dp_add_integer_array(struct acpi_dp *dp, const char *name,
  248. u64 *array, int len)
  249. {
  250. struct acpi_dp *dp_array;
  251. int i;
  252. assert(dp);
  253. if (len <= 0)
  254. return NULL;
  255. dp_array = acpi_dp_new_table(name);
  256. if (!dp_array)
  257. return NULL;
  258. for (i = 0; i < len; i++)
  259. if (!acpi_dp_add_integer(dp_array, NULL, array[i]))
  260. break;
  261. if (!acpi_dp_add_array(dp, dp_array))
  262. return NULL;
  263. return dp_array;
  264. }
  265. struct acpi_dp *acpi_dp_add_gpio(struct acpi_dp *dp, const char *name,
  266. const char *ref, int index, int pin,
  267. enum acpi_gpio_polarity polarity)
  268. {
  269. struct acpi_dp *gpio;
  270. assert(dp);
  271. gpio = acpi_dp_new_table(name);
  272. if (!gpio)
  273. return NULL;
  274. if (!acpi_dp_add_reference(gpio, NULL, ref) ||
  275. !acpi_dp_add_integer(gpio, NULL, index) ||
  276. !acpi_dp_add_integer(gpio, NULL, pin) ||
  277. !acpi_dp_add_integer(gpio, NULL, polarity == ACPI_GPIO_ACTIVE_LOW))
  278. return NULL;
  279. if (!acpi_dp_add_array(dp, gpio))
  280. return NULL;
  281. return gpio;
  282. }
  283. int acpi_dp_ofnode_copy_int(ofnode node, struct acpi_dp *dp, const char *prop)
  284. {
  285. int ret;
  286. u32 val = 0;
  287. ret = ofnode_read_u32(node, prop, &val);
  288. if (ret)
  289. return ret;
  290. if (!acpi_dp_add_integer(dp, prop, val))
  291. return log_ret(-ENOMEM);
  292. return 0;
  293. }
  294. int acpi_dp_ofnode_copy_str(ofnode node, struct acpi_dp *dp, const char *prop)
  295. {
  296. const char *val;
  297. val = ofnode_read_string(node, prop);
  298. if (!val)
  299. return -EINVAL;
  300. if (!acpi_dp_add_string(dp, prop, val))
  301. return log_ret(-ENOMEM);
  302. return 0;
  303. }
  304. int acpi_dp_dev_copy_int(const struct udevice *dev, struct acpi_dp *dp,
  305. const char *prop)
  306. {
  307. int ret;
  308. u32 val = 0;
  309. ret = dev_read_u32(dev, prop, &val);
  310. if (ret)
  311. return ret;
  312. if (!acpi_dp_add_integer(dp, prop, val))
  313. return log_ret(-ENOMEM);
  314. return ret;
  315. }
  316. int acpi_dp_dev_copy_str(const struct udevice *dev, struct acpi_dp *dp,
  317. const char *prop)
  318. {
  319. const char *val;
  320. val = dev_read_string(dev, prop);
  321. if (!val)
  322. return -EINVAL;
  323. if (!acpi_dp_add_string(dp, prop, val))
  324. return log_ret(-ENOMEM);
  325. return 0;
  326. }