acpi_table.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Generic code used to generate ACPI tables
  4. *
  5. * Copyright 2019 Google LLC
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <cpu.h>
  10. #include <mapmem.h>
  11. #include <tables_csum.h>
  12. #include <version.h>
  13. #include <acpi/acpi_table.h>
  14. #include <dm/acpi.h>
  15. int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
  16. {
  17. struct acpi_table_header *header = &dmar->header;
  18. struct cpu_info info;
  19. struct udevice *cpu;
  20. int ret;
  21. ret = uclass_first_device(UCLASS_CPU, &cpu);
  22. if (ret)
  23. return log_msg_ret("cpu", ret);
  24. ret = cpu_get_info(cpu, &info);
  25. if (ret)
  26. return log_msg_ret("info", ret);
  27. memset((void *)dmar, 0, sizeof(struct acpi_dmar));
  28. /* Fill out header fields. */
  29. acpi_fill_header(&dmar->header, "DMAR");
  30. header->length = sizeof(struct acpi_dmar);
  31. header->revision = acpi_get_table_revision(ACPITAB_DMAR);
  32. dmar->host_address_width = info.address_width - 1;
  33. dmar->flags = flags;
  34. return 0;
  35. }
  36. int acpi_get_table_revision(enum acpi_tables table)
  37. {
  38. switch (table) {
  39. case ACPITAB_FADT:
  40. return ACPI_FADT_REV_ACPI_3_0;
  41. case ACPITAB_MADT:
  42. return ACPI_MADT_REV_ACPI_3_0;
  43. case ACPITAB_MCFG:
  44. return ACPI_MCFG_REV_ACPI_3_0;
  45. case ACPITAB_TCPA:
  46. /* This version and the rest are open-coded */
  47. return 2;
  48. case ACPITAB_TPM2:
  49. return 4;
  50. case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
  51. return 2;
  52. case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
  53. return 1; /* TODO Should probably be upgraded to 2 */
  54. case ACPITAB_DMAR:
  55. return 1;
  56. case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
  57. return 1;
  58. case ACPITAB_SPMI: /* IMPI 2.0 */
  59. return 5;
  60. case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
  61. return 1;
  62. case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
  63. return 1;
  64. case ACPITAB_IVRS:
  65. return IVRS_FORMAT_FIXED;
  66. case ACPITAB_DBG2:
  67. return 0;
  68. case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
  69. return 1;
  70. case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
  71. return 1;
  72. case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
  73. return 1;
  74. case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
  75. return 2;
  76. case ACPITAB_HEST:
  77. return 1;
  78. case ACPITAB_NHLT:
  79. return 5;
  80. case ACPITAB_BERT:
  81. return 1;
  82. case ACPITAB_SPCR:
  83. return 2;
  84. default:
  85. return -EINVAL;
  86. }
  87. }
  88. void acpi_fill_header(struct acpi_table_header *header, char *signature)
  89. {
  90. memcpy(header->signature, signature, 4);
  91. memcpy(header->oem_id, OEM_ID, 6);
  92. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  93. header->oem_revision = U_BOOT_BUILD_DATE;
  94. memcpy(header->aslc_id, ASLC_ID, 4);
  95. }
  96. void acpi_align(struct acpi_ctx *ctx)
  97. {
  98. ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
  99. }
  100. void acpi_align64(struct acpi_ctx *ctx)
  101. {
  102. ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
  103. }
  104. void acpi_inc(struct acpi_ctx *ctx, uint amount)
  105. {
  106. ctx->current += amount;
  107. }
  108. void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
  109. {
  110. ctx->current += amount;
  111. acpi_align(ctx);
  112. }
  113. /**
  114. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  115. * and checksum.
  116. */
  117. int acpi_add_table(struct acpi_ctx *ctx, void *table)
  118. {
  119. int i, entries_num;
  120. struct acpi_rsdt *rsdt;
  121. struct acpi_xsdt *xsdt;
  122. /* The RSDT is mandatory while the XSDT is not */
  123. rsdt = ctx->rsdt;
  124. /* This should always be MAX_ACPI_TABLES */
  125. entries_num = ARRAY_SIZE(rsdt->entry);
  126. for (i = 0; i < entries_num; i++) {
  127. if (rsdt->entry[i] == 0)
  128. break;
  129. }
  130. if (i >= entries_num) {
  131. log_err("ACPI: Error: too many tables\n");
  132. return -E2BIG;
  133. }
  134. /* Add table to the RSDT */
  135. rsdt->entry[i] = map_to_sysmem(table);
  136. /* Fix RSDT length or the kernel will assume invalid entries */
  137. rsdt->header.length = sizeof(struct acpi_table_header) +
  138. (sizeof(u32) * (i + 1));
  139. /* Re-calculate checksum */
  140. rsdt->header.checksum = 0;
  141. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  142. rsdt->header.length);
  143. /*
  144. * And now the same thing for the XSDT. We use the same index as for
  145. * now we want the XSDT and RSDT to always be in sync in U-Boot
  146. */
  147. xsdt = ctx->xsdt;
  148. /* Add table to the XSDT */
  149. xsdt->entry[i] = map_to_sysmem(table);
  150. /* Fix XSDT length */
  151. xsdt->header.length = sizeof(struct acpi_table_header) +
  152. (sizeof(u64) * (i + 1));
  153. /* Re-calculate checksum */
  154. xsdt->header.checksum = 0;
  155. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  156. xsdt->header.length);
  157. return 0;
  158. }
  159. static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  160. struct acpi_xsdt *xsdt)
  161. {
  162. memset(rsdp, 0, sizeof(struct acpi_rsdp));
  163. memcpy(rsdp->signature, RSDP_SIG, 8);
  164. memcpy(rsdp->oem_id, OEM_ID, 6);
  165. rsdp->length = sizeof(struct acpi_rsdp);
  166. rsdp->rsdt_address = map_to_sysmem(rsdt);
  167. rsdp->xsdt_address = map_to_sysmem(xsdt);
  168. rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  169. /* Calculate checksums */
  170. rsdp->checksum = table_compute_checksum(rsdp, 20);
  171. rsdp->ext_checksum = table_compute_checksum(rsdp,
  172. sizeof(struct acpi_rsdp));
  173. }
  174. static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  175. {
  176. struct acpi_table_header *header = &rsdt->header;
  177. /* Fill out header fields */
  178. acpi_fill_header(header, "RSDT");
  179. header->length = sizeof(struct acpi_rsdt);
  180. header->revision = 1;
  181. /* Entries are filled in later, we come with an empty set */
  182. /* Fix checksum */
  183. header->checksum = table_compute_checksum(rsdt,
  184. sizeof(struct acpi_rsdt));
  185. }
  186. static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  187. {
  188. struct acpi_table_header *header = &xsdt->header;
  189. /* Fill out header fields */
  190. acpi_fill_header(header, "XSDT");
  191. header->length = sizeof(struct acpi_xsdt);
  192. header->revision = 1;
  193. /* Entries are filled in later, we come with an empty set */
  194. /* Fix checksum */
  195. header->checksum = table_compute_checksum(xsdt,
  196. sizeof(struct acpi_xsdt));
  197. }
  198. void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
  199. {
  200. ctx->current = start;
  201. /* Align ACPI tables to 16 byte */
  202. acpi_align(ctx);
  203. gd->arch.acpi_start = map_to_sysmem(ctx->current);
  204. /* We need at least an RSDP and an RSDT Table */
  205. ctx->rsdp = ctx->current;
  206. acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
  207. ctx->rsdt = ctx->current;
  208. acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
  209. ctx->xsdt = ctx->current;
  210. acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
  211. /* clear all table memory */
  212. memset((void *)start, '\0', ctx->current - start);
  213. acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
  214. acpi_write_rsdt(ctx->rsdt);
  215. acpi_write_xsdt(ctx->xsdt);
  216. /*
  217. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  218. * boundary (Windows checks this, but Linux does not).
  219. */
  220. acpi_align64(ctx);
  221. }