acpi_table.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_string.h>
  14. #include <acpi/acpi_table.h>
  15. #include <asm/global_data.h>
  16. #include <dm/acpi.h>
  17. /*
  18. * OEM_REVISION is 32-bit unsigned number. It should be increased only when
  19. * changing software version. Therefore it should not depend on build time.
  20. * U-Boot calculates it from U-Boot version and represent it in hexadecimal
  21. * notation. As U-Boot version is in form year.month set low 8 bits to 0x01
  22. * to have valid date. So for U-Boot version 2021.04 OEM_REVISION is set to
  23. * value 0x20210401.
  24. */
  25. #define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \
  26. (((version_num / 100) % 10) << 24) | \
  27. (((version_num / 10) % 10) << 20) | \
  28. ((version_num % 10) << 16) | \
  29. (((version_num_patch / 10) % 10) << 12) | \
  30. ((version_num_patch % 10) << 8) | \
  31. 0x01)
  32. int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags)
  33. {
  34. struct acpi_table_header *header = &dmar->header;
  35. struct cpu_info info;
  36. struct udevice *cpu;
  37. int ret;
  38. ret = uclass_first_device_err(UCLASS_CPU, &cpu);
  39. if (ret)
  40. return log_msg_ret("cpu", ret);
  41. ret = cpu_get_info(cpu, &info);
  42. if (ret)
  43. return log_msg_ret("info", ret);
  44. memset((void *)dmar, 0, sizeof(struct acpi_dmar));
  45. /* Fill out header fields. */
  46. acpi_fill_header(&dmar->header, "DMAR");
  47. header->length = sizeof(struct acpi_dmar);
  48. header->revision = acpi_get_table_revision(ACPITAB_DMAR);
  49. dmar->host_address_width = info.address_width - 1;
  50. dmar->flags = flags;
  51. return 0;
  52. }
  53. int acpi_get_table_revision(enum acpi_tables table)
  54. {
  55. switch (table) {
  56. case ACPITAB_FADT:
  57. return ACPI_FADT_REV_ACPI_3_0;
  58. case ACPITAB_MADT:
  59. return ACPI_MADT_REV_ACPI_3_0;
  60. case ACPITAB_MCFG:
  61. return ACPI_MCFG_REV_ACPI_3_0;
  62. case ACPITAB_TCPA:
  63. /* This version and the rest are open-coded */
  64. return 2;
  65. case ACPITAB_TPM2:
  66. return 4;
  67. case ACPITAB_SSDT: /* ACPI 3.0 upto 6.3: 2 */
  68. return 2;
  69. case ACPITAB_SRAT: /* ACPI 2.0: 1, ACPI 3.0: 2, ACPI 4.0 to 6.3: 3 */
  70. return 1; /* TODO Should probably be upgraded to 2 */
  71. case ACPITAB_DMAR:
  72. return 1;
  73. case ACPITAB_SLIT: /* ACPI 2.0 upto 6.3: 1 */
  74. return 1;
  75. case ACPITAB_SPMI: /* IMPI 2.0 */
  76. return 5;
  77. case ACPITAB_HPET: /* Currently 1. Table added in ACPI 2.0 */
  78. return 1;
  79. case ACPITAB_VFCT: /* ACPI 2.0/3.0/4.0: 1 */
  80. return 1;
  81. case ACPITAB_IVRS:
  82. return IVRS_FORMAT_FIXED;
  83. case ACPITAB_DBG2:
  84. return 0;
  85. case ACPITAB_FACS: /* ACPI 2.0/3.0: 1, ACPI 4.0 to 6.3: 2 */
  86. return 1;
  87. case ACPITAB_RSDT: /* ACPI 1.0 upto 6.3: 1 */
  88. return 1;
  89. case ACPITAB_XSDT: /* ACPI 2.0 upto 6.3: 1 */
  90. return 1;
  91. case ACPITAB_RSDP: /* ACPI 2.0 upto 6.3: 2 */
  92. return 2;
  93. case ACPITAB_HEST:
  94. return 1;
  95. case ACPITAB_NHLT:
  96. return 5;
  97. case ACPITAB_BERT:
  98. return 1;
  99. case ACPITAB_SPCR:
  100. return 2;
  101. default:
  102. return -EINVAL;
  103. }
  104. }
  105. void acpi_fill_header(struct acpi_table_header *header, char *signature)
  106. {
  107. memcpy(header->signature, signature, 4);
  108. memcpy(header->oem_id, OEM_ID, 6);
  109. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  110. header->oem_revision = OEM_REVISION;
  111. memcpy(header->aslc_id, ASLC_ID, 4);
  112. }
  113. void acpi_align(struct acpi_ctx *ctx)
  114. {
  115. ctx->current = (void *)ALIGN((ulong)ctx->current, 16);
  116. }
  117. void acpi_align64(struct acpi_ctx *ctx)
  118. {
  119. ctx->current = (void *)ALIGN((ulong)ctx->current, 64);
  120. }
  121. void acpi_inc(struct acpi_ctx *ctx, uint amount)
  122. {
  123. ctx->current += amount;
  124. }
  125. void acpi_inc_align(struct acpi_ctx *ctx, uint amount)
  126. {
  127. ctx->current += amount;
  128. acpi_align(ctx);
  129. }
  130. /**
  131. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  132. * and checksum.
  133. */
  134. int acpi_add_table(struct acpi_ctx *ctx, void *table)
  135. {
  136. int i, entries_num;
  137. struct acpi_rsdt *rsdt;
  138. struct acpi_xsdt *xsdt;
  139. /* The RSDT is mandatory while the XSDT is not */
  140. rsdt = ctx->rsdt;
  141. /* This should always be MAX_ACPI_TABLES */
  142. entries_num = ARRAY_SIZE(rsdt->entry);
  143. for (i = 0; i < entries_num; i++) {
  144. if (rsdt->entry[i] == 0)
  145. break;
  146. }
  147. if (i >= entries_num) {
  148. log_err("ACPI: Error: too many tables\n");
  149. return -E2BIG;
  150. }
  151. /* Add table to the RSDT */
  152. rsdt->entry[i] = map_to_sysmem(table);
  153. /* Fix RSDT length or the kernel will assume invalid entries */
  154. rsdt->header.length = sizeof(struct acpi_table_header) +
  155. (sizeof(u32) * (i + 1));
  156. /* Re-calculate checksum */
  157. rsdt->header.checksum = 0;
  158. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  159. rsdt->header.length);
  160. /*
  161. * And now the same thing for the XSDT. We use the same index as for
  162. * now we want the XSDT and RSDT to always be in sync in U-Boot
  163. */
  164. xsdt = ctx->xsdt;
  165. /* Add table to the XSDT */
  166. xsdt->entry[i] = map_to_sysmem(table);
  167. /* Fix XSDT length */
  168. xsdt->header.length = sizeof(struct acpi_table_header) +
  169. (sizeof(u64) * (i + 1));
  170. /* Re-calculate checksum */
  171. xsdt->header.checksum = 0;
  172. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  173. xsdt->header.length);
  174. return 0;
  175. }
  176. void acpi_create_dbg2(struct acpi_dbg2_header *dbg2,
  177. int port_type, int port_subtype,
  178. struct acpi_gen_regaddr *address, u32 address_size,
  179. const char *device_path)
  180. {
  181. uintptr_t current;
  182. struct acpi_dbg2_device *device;
  183. u32 *dbg2_addr_size;
  184. struct acpi_table_header *header;
  185. size_t path_len;
  186. const char *path;
  187. char *namespace;
  188. /* Fill out header fields. */
  189. current = (uintptr_t)dbg2;
  190. memset(dbg2, '\0', sizeof(struct acpi_dbg2_header));
  191. header = &dbg2->header;
  192. header->revision = acpi_get_table_revision(ACPITAB_DBG2);
  193. acpi_fill_header(header, "DBG2");
  194. header->aslc_revision = ASL_REVISION;
  195. /* One debug device defined */
  196. dbg2->devices_offset = sizeof(struct acpi_dbg2_header);
  197. dbg2->devices_count = 1;
  198. current += sizeof(struct acpi_dbg2_header);
  199. /* Device comes after the header */
  200. device = (struct acpi_dbg2_device *)current;
  201. memset(device, 0, sizeof(struct acpi_dbg2_device));
  202. current += sizeof(struct acpi_dbg2_device);
  203. device->revision = 0;
  204. device->address_count = 1;
  205. device->port_type = port_type;
  206. device->port_subtype = port_subtype;
  207. /* Base Address comes after device structure */
  208. memcpy((void *)current, address, sizeof(struct acpi_gen_regaddr));
  209. device->base_address_offset = current - (uintptr_t)device;
  210. current += sizeof(struct acpi_gen_regaddr);
  211. /* Address Size comes after address structure */
  212. dbg2_addr_size = (uint32_t *)current;
  213. device->address_size_offset = current - (uintptr_t)device;
  214. *dbg2_addr_size = address_size;
  215. current += sizeof(uint32_t);
  216. /* Namespace string comes last, use '.' if not provided */
  217. path = device_path ? : ".";
  218. /* Namespace string length includes NULL terminator */
  219. path_len = strlen(path) + 1;
  220. namespace = (char *)current;
  221. device->namespace_string_length = path_len;
  222. device->namespace_string_offset = current - (uintptr_t)device;
  223. strncpy(namespace, path, path_len);
  224. current += path_len;
  225. /* Update structure lengths and checksum */
  226. device->length = current - (uintptr_t)device;
  227. header->length = current - (uintptr_t)dbg2;
  228. header->checksum = table_compute_checksum(dbg2, header->length);
  229. }