acpi_table.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <log.h>
  11. #include <mapmem.h>
  12. #include <tables_csum.h>
  13. #include <version.h>
  14. #include <acpi/acpi_table.h>
  15. #include <dm/acpi.h>
  16. int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
  17. {
  18. struct acpi_table_header *header = &dmar->header;
  19. struct cpu_info info;
  20. struct udevice *cpu;
  21. int ret;
  22. ret = uclass_first_device(UCLASS_CPU, &cpu);
  23. if (ret)
  24. return log_msg_ret("cpu", ret);
  25. ret = cpu_get_info(cpu, &info);
  26. if (ret)
  27. return log_msg_ret("info", ret);
  28. memset((void *)dmar, 0, sizeof(struct acpi_dmar));
  29. /* Fill out header fields. */
  30. acpi_fill_header(&dmar->header, "DMAR");
  31. header->length = sizeof(struct acpi_dmar);
  32. header->revision = acpi_get_table_revision(ACPITAB_DMAR);
  33. dmar->host_address_width = info.address_width - 1;
  34. dmar->flags = flags;
  35. return 0;
  36. }
  37. int acpi_get_table_revision(enum acpi_tables table)
  38. {
  39. switch (table) {
  40. case ACPITAB_FADT:
  41. return ACPI_FADT_REV_ACPI_3_0;
  42. case ACPITAB_MADT:
  43. return ACPI_MADT_REV_ACPI_3_0;
  44. case ACPITAB_MCFG:
  45. return ACPI_MCFG_REV_ACPI_3_0;
  46. case ACPITAB_TCPA:
  47. /* This version and the rest are open-coded */
  48. return 2;
  49. case ACPITAB_TPM2:
  50. return 4;
  51. case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
  52. return 2;
  53. case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
  54. return 1; /* TODO Should probably be upgraded to 2 */
  55. case ACPITAB_DMAR:
  56. return 1;
  57. case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
  58. return 1;
  59. case ACPITAB_SPMI: /* IMPI 2.0 */
  60. return 5;
  61. case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
  62. return 1;
  63. case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
  64. return 1;
  65. case ACPITAB_IVRS:
  66. return IVRS_FORMAT_FIXED;
  67. case ACPITAB_DBG2:
  68. return 0;
  69. case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
  70. return 1;
  71. case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
  72. return 1;
  73. case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
  74. return 1;
  75. case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
  76. return 2;
  77. case ACPITAB_HEST:
  78. return 1;
  79. case ACPITAB_NHLT:
  80. return 5;
  81. case ACPITAB_BERT:
  82. return 1;
  83. case ACPITAB_SPCR:
  84. return 2;
  85. default:
  86. return -EINVAL;
  87. }
  88. }
  89. void acpi_fill_header(struct acpi_table_header *header, char *signature)
  90. {
  91. memcpy(header->signature, signature, 4);
  92. memcpy(header->oem_id, OEM_ID, 6);
  93. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  94. header->oem_revision = U_BOOT_BUILD_DATE;
  95. memcpy(header->aslc_id, ASLC_ID, 4);
  96. }
  97. void acpi_align(struct acpi_ctx *ctx)
  98. {
  99. ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
  100. }
  101. void acpi_align64(struct acpi_ctx *ctx)
  102. {
  103. ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
  104. }
  105. void acpi_inc(struct acpi_ctx *ctx, uint amount)
  106. {
  107. ctx->current += amount;
  108. }
  109. void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
  110. {
  111. ctx->current += amount;
  112. acpi_align(ctx);
  113. }
  114. /**
  115. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  116. * and checksum.
  117. */
  118. int acpi_add_table(struct acpi_ctx *ctx, void *table)
  119. {
  120. int i, entries_num;
  121. struct acpi_rsdt *rsdt;
  122. struct acpi_xsdt *xsdt;
  123. /* The RSDT is mandatory while the XSDT is not */
  124. rsdt = ctx->rsdt;
  125. /* This should always be MAX_ACPI_TABLES */
  126. entries_num = ARRAY_SIZE(rsdt->entry);
  127. for (i = 0; i < entries_num; i++) {
  128. if (rsdt->entry[i] == 0)
  129. break;
  130. }
  131. if (i >= entries_num) {
  132. log_err("ACPI: Error: too many tables\n");
  133. return -E2BIG;
  134. }
  135. /* Add table to the RSDT */
  136. rsdt->entry[i] = map_to_sysmem(table);
  137. /* Fix RSDT length or the kernel will assume invalid entries */
  138. rsdt->header.length = sizeof(struct acpi_table_header) +
  139. (sizeof(u32) * (i + 1));
  140. /* Re-calculate checksum */
  141. rsdt->header.checksum = 0;
  142. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  143. rsdt->header.length);
  144. /*
  145. * And now the same thing for the XSDT. We use the same index as for
  146. * now we want the XSDT and RSDT to always be in sync in U-Boot
  147. */
  148. xsdt = ctx->xsdt;
  149. /* Add table to the XSDT */
  150. xsdt->entry[i] = map_to_sysmem(table);
  151. /* Fix XSDT length */
  152. xsdt->header.length = sizeof(struct acpi_table_header) +
  153. (sizeof(u64) * (i + 1));
  154. /* Re-calculate checksum */
  155. xsdt->header.checksum = 0;
  156. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  157. xsdt->header.length);
  158. return 0;
  159. }
  160. static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  161. struct acpi_xsdt *xsdt)
  162. {
  163. memset(rsdp, 0, sizeof(struct acpi_rsdp));
  164. memcpy(rsdp->signature, RSDP_SIG, 8);
  165. memcpy(rsdp->oem_id, OEM_ID, 6);
  166. rsdp->length = sizeof(struct acpi_rsdp);
  167. rsdp->rsdt_address = map_to_sysmem(rsdt);
  168. rsdp->xsdt_address = map_to_sysmem(xsdt);
  169. rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  170. /* Calculate checksums */
  171. rsdp->checksum = table_compute_checksum(rsdp, 20);
  172. rsdp->ext_checksum = table_compute_checksum(rsdp,
  173. sizeof(struct acpi_rsdp));
  174. }
  175. static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  176. {
  177. struct acpi_table_header *header = &rsdt->header;
  178. /* Fill out header fields */
  179. acpi_fill_header(header, "RSDT");
  180. header->length = sizeof(struct acpi_rsdt);
  181. header->revision = 1;
  182. /* Entries are filled in later, we come with an empty set */
  183. /* Fix checksum */
  184. header->checksum = table_compute_checksum(rsdt,
  185. sizeof(struct acpi_rsdt));
  186. }
  187. static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  188. {
  189. struct acpi_table_header *header = &xsdt->header;
  190. /* Fill out header fields */
  191. acpi_fill_header(header, "XSDT");
  192. header->length = sizeof(struct acpi_xsdt);
  193. header->revision = 1;
  194. /* Entries are filled in later, we come with an empty set */
  195. /* Fix checksum */
  196. header->checksum = table_compute_checksum(xsdt,
  197. sizeof(struct acpi_xsdt));
  198. }
  199. void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start)
  200. {
  201. ctx->base = start;
  202. ctx->current = start;
  203. /* Align ACPI tables to 16 byte */
  204. acpi_align(ctx);
  205. gd->arch.acpi_start = map_to_sysmem(ctx->current);
  206. /* We need at least an RSDP and an RSDT Table */
  207. ctx->rsdp = ctx->current;
  208. acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
  209. ctx->rsdt = ctx->current;
  210. acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
  211. ctx->xsdt = ctx->current;
  212. acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
  213. /* clear all table memory */
  214. memset((void *)start, '\0', ctx->current - start);
  215. acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt);
  216. acpi_write_rsdt(ctx->rsdt);
  217. acpi_write_xsdt(ctx->xsdt);
  218. /*
  219. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  220. * boundary (Windows checks this, but Linux does not).
  221. */
  222. acpi_align64(ctx);
  223. }
  224. void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
  225. int port_type, int port_subtype,
  226. struct acpi_gen_regaddr *address, u32 address_size,
  227. const char *device_path)
  228. {
  229. uintptr_t current;
  230. struct acpi_dbg2_device *device;
  231. u32 *dbg2_addr_size;
  232. struct acpi_table_header *header;
  233. size_t path_len;
  234. const char *path;
  235. char *namespace;
  236. /* Fill out header fields. */
  237. current = (uintptr_t)dbg2;
  238. memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
  239. header = &dbg2->header;
  240. header->revision = acpi_get_table_revision(ACPITAB_DBG2);
  241. acpi_fill_header(header, "DBG2");
  242. header->aslc_revision = ASL_REVISION;
  243. /* One debug device defined */
  244. dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
  245. dbg2->devices_count = 1;
  246. current += sizeof(struct acpi_dbg2_header);
  247. /* Device comes after the header */
  248. device = (struct acpi_dbg2_device *)current;
  249. memset(device, 0, sizeof(struct acpi_dbg2_device));
  250. current += sizeof(struct acpi_dbg2_device);
  251. device->revision = 0;
  252. device->address_count = 1;
  253. device->port_type = port_type;
  254. device->port_subtype = port_subtype;
  255. /* Base Address comes after device structure */
  256. memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
  257. device->base_address_offset = current - (uintptr_t)device;
  258. current += sizeof(struct acpi_gen_regaddr);
  259. /* Address Size comes after address structure */
  260. dbg2_addr_size = (uint32_t *)current;
  261. device->address_size_offset = current - (uintptr_t)device;
  262. *dbg2_addr_size = address_size;
  263. current += sizeof(uint32_t);
  264. /* Namespace string comes last, use '.' if not provided */
  265. path = device_path ? : ".";
  266. /* Namespace string length includes NULL terminator */
  267. path_len = strlen(path) + 1;
  268. namespace = (char *)current;
  269. device->namespace_string_length = path_len;
  270. device->namespace_string_offset = current - (uintptr_t)device;
  271. strncpy(namespace, path, path_len);
  272. current += path_len;
  273. /* Update structure lengths and checksum */
  274. device->length = current - (uintptr_t)device;
  275. header->length = current - (uintptr_t)dbg2;
  276. header->checksum = table_compute_checksum(dbg2, header->length);
  277. }