qfw.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <errno.h>
  8. #include <malloc.h>
  9. #include <qfw.h>
  10. #include <asm/io.h>
  11. #ifdef CONFIG_GENERATE_ACPI_TABLE
  12. #include <asm/tables.h>
  13. #endif
  14. #include <linux/list.h>
  15. static bool fwcfg_present;
  16. static bool fwcfg_dma_present;
  17. static struct fw_cfg_arch_ops *fwcfg_arch_ops;
  18. static LIST_HEAD(fw_list);
  19. #ifdef CONFIG_GENERATE_ACPI_TABLE
  20. /*
  21. * This function allocates memory for ACPI tables
  22. *
  23. * @entry : BIOS linker command entry which tells where to allocate memory
  24. * (either high memory or low memory)
  25. * @addr : The address that should be used for low memory allcation. If the
  26. * memory allocation request is 'ZONE_HIGH' then this parameter will
  27. * be ignored.
  28. * @return: 0 on success, or negative value on failure
  29. */
  30. static int bios_linker_allocate(struct bios_linker_entry *entry, ulong *addr)
  31. {
  32. uint32_t size, align;
  33. struct fw_file *file;
  34. unsigned long aligned_addr;
  35. align = le32_to_cpu(entry->alloc.align);
  36. /* align must be power of 2 */
  37. if (align & (align - 1)) {
  38. printf("error: wrong alignment %u\n", align);
  39. return -EINVAL;
  40. }
  41. file = qemu_fwcfg_find_file(entry->alloc.file);
  42. if (!file) {
  43. printf("error: can't find file %s\n", entry->alloc.file);
  44. return -ENOENT;
  45. }
  46. size = be32_to_cpu(file->cfg.size);
  47. /*
  48. * ZONE_HIGH means we need to allocate from high memory, since
  49. * malloc space is already at the end of RAM, so we directly use it.
  50. * If allocation zone is ZONE_FSEG, then we use the 'addr' passed
  51. * in which is low memory
  52. */
  53. if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH) {
  54. aligned_addr = (unsigned long)memalign(align, size);
  55. if (!aligned_addr) {
  56. printf("error: allocating resource\n");
  57. return -ENOMEM;
  58. }
  59. } else if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG) {
  60. aligned_addr = ALIGN(*addr, align);
  61. } else {
  62. printf("error: invalid allocation zone\n");
  63. return -EINVAL;
  64. }
  65. debug("bios_linker_allocate: allocate file %s, size %u, zone %d, align %u, addr 0x%lx\n",
  66. file->cfg.name, size, entry->alloc.zone, align, aligned_addr);
  67. qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
  68. size, (void *)aligned_addr);
  69. file->addr = aligned_addr;
  70. /* adjust address for low memory allocation */
  71. if (entry->alloc.zone == BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG)
  72. *addr = (aligned_addr + size);
  73. return 0;
  74. }
  75. /*
  76. * This function patches ACPI tables previously loaded
  77. * by bios_linker_allocate()
  78. *
  79. * @entry : BIOS linker command entry which tells how to patch
  80. * ACPI tables
  81. * @return: 0 on success, or negative value on failure
  82. */
  83. static int bios_linker_add_pointer(struct bios_linker_entry *entry)
  84. {
  85. struct fw_file *dest, *src;
  86. uint32_t offset = le32_to_cpu(entry->pointer.offset);
  87. uint64_t pointer = 0;
  88. dest = qemu_fwcfg_find_file(entry->pointer.dest_file);
  89. if (!dest || !dest->addr)
  90. return -ENOENT;
  91. src = qemu_fwcfg_find_file(entry->pointer.src_file);
  92. if (!src || !src->addr)
  93. return -ENOENT;
  94. debug("bios_linker_add_pointer: dest->addr 0x%lx, src->addr 0x%lx, offset 0x%x size %u, 0x%llx\n",
  95. dest->addr, src->addr, offset, entry->pointer.size, pointer);
  96. memcpy(&pointer, (char *)dest->addr + offset, entry->pointer.size);
  97. pointer = le64_to_cpu(pointer);
  98. pointer += (unsigned long)src->addr;
  99. pointer = cpu_to_le64(pointer);
  100. memcpy((char *)dest->addr + offset, &pointer, entry->pointer.size);
  101. return 0;
  102. }
  103. /*
  104. * This function updates checksum fields of ACPI tables previously loaded
  105. * by bios_linker_allocate()
  106. *
  107. * @entry : BIOS linker command entry which tells where to update ACPI table
  108. * checksums
  109. * @return: 0 on success, or negative value on failure
  110. */
  111. static int bios_linker_add_checksum(struct bios_linker_entry *entry)
  112. {
  113. struct fw_file *file;
  114. uint8_t *data, cksum = 0;
  115. uint8_t *cksum_start;
  116. file = qemu_fwcfg_find_file(entry->cksum.file);
  117. if (!file || !file->addr)
  118. return -ENOENT;
  119. data = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.offset));
  120. cksum_start = (uint8_t *)(file->addr + le32_to_cpu(entry->cksum.start));
  121. cksum = table_compute_checksum(cksum_start,
  122. le32_to_cpu(entry->cksum.length));
  123. *data = cksum;
  124. return 0;
  125. }
  126. /* This function loads and patches ACPI tables provided by QEMU */
  127. ulong write_acpi_tables(ulong addr)
  128. {
  129. int i, ret = 0;
  130. struct fw_file *file;
  131. struct bios_linker_entry *table_loader;
  132. struct bios_linker_entry *entry;
  133. uint32_t size;
  134. /* make sure fw_list is loaded */
  135. ret = qemu_fwcfg_read_firmware_list();
  136. if (ret) {
  137. printf("error: can't read firmware file list\n");
  138. return addr;
  139. }
  140. file = qemu_fwcfg_find_file("etc/table-loader");
  141. if (!file) {
  142. printf("error: can't find etc/table-loader\n");
  143. return addr;
  144. }
  145. size = be32_to_cpu(file->cfg.size);
  146. if ((size % sizeof(*entry)) != 0) {
  147. printf("error: table-loader maybe corrupted\n");
  148. return addr;
  149. }
  150. table_loader = malloc(size);
  151. if (!table_loader) {
  152. printf("error: no memory for table-loader\n");
  153. return addr;
  154. }
  155. qemu_fwcfg_read_entry(be16_to_cpu(file->cfg.select),
  156. size, table_loader);
  157. for (i = 0; i < (size / sizeof(*entry)); i++) {
  158. entry = table_loader + i;
  159. switch (le32_to_cpu(entry->command)) {
  160. case BIOS_LINKER_LOADER_COMMAND_ALLOCATE:
  161. ret = bios_linker_allocate(entry, &addr);
  162. if (ret)
  163. goto out;
  164. break;
  165. case BIOS_LINKER_LOADER_COMMAND_ADD_POINTER:
  166. ret = bios_linker_add_pointer(entry);
  167. if (ret)
  168. goto out;
  169. break;
  170. case BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM:
  171. ret = bios_linker_add_checksum(entry);
  172. if (ret)
  173. goto out;
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. out:
  180. if (ret) {
  181. struct fw_cfg_file_iter iter;
  182. for (file = qemu_fwcfg_file_iter_init(&iter);
  183. !qemu_fwcfg_file_iter_end(&iter);
  184. file = qemu_fwcfg_file_iter_next(&iter)) {
  185. if (file->addr) {
  186. free((void *)file->addr);
  187. file->addr = 0;
  188. }
  189. }
  190. }
  191. free(table_loader);
  192. return addr;
  193. }
  194. ulong acpi_get_rsdp_addr(void)
  195. {
  196. struct fw_file *file;
  197. file = qemu_fwcfg_find_file("etc/acpi/rsdp");
  198. return file->addr;
  199. }
  200. #endif
  201. /* Read configuration item using fw_cfg PIO interface */
  202. static void qemu_fwcfg_read_entry_pio(uint16_t entry,
  203. uint32_t size, void *address)
  204. {
  205. debug("qemu_fwcfg_read_entry_pio: entry 0x%x, size %u address %p\n",
  206. entry, size, address);
  207. return fwcfg_arch_ops->arch_read_pio(entry, size, address);
  208. }
  209. /* Read configuration item using fw_cfg DMA interface */
  210. static void qemu_fwcfg_read_entry_dma(uint16_t entry,
  211. uint32_t size, void *address)
  212. {
  213. struct fw_cfg_dma_access dma;
  214. dma.length = cpu_to_be32(size);
  215. dma.address = cpu_to_be64((uintptr_t)address);
  216. dma.control = cpu_to_be32(FW_CFG_DMA_READ);
  217. /*
  218. * writting FW_CFG_INVALID will cause read operation to resume at
  219. * last offset, otherwise read will start at offset 0
  220. */
  221. if (entry != FW_CFG_INVALID)
  222. dma.control |= cpu_to_be32(FW_CFG_DMA_SELECT | (entry << 16));
  223. barrier();
  224. debug("qemu_fwcfg_read_entry_dma: entry 0x%x, size %u address %p, control 0x%x\n",
  225. entry, size, address, be32_to_cpu(dma.control));
  226. fwcfg_arch_ops->arch_read_dma(&dma);
  227. }
  228. bool qemu_fwcfg_present(void)
  229. {
  230. return fwcfg_present;
  231. }
  232. bool qemu_fwcfg_dma_present(void)
  233. {
  234. return fwcfg_dma_present;
  235. }
  236. void qemu_fwcfg_read_entry(uint16_t entry, uint32_t length, void *address)
  237. {
  238. if (fwcfg_dma_present)
  239. qemu_fwcfg_read_entry_dma(entry, length, address);
  240. else
  241. qemu_fwcfg_read_entry_pio(entry, length, address);
  242. }
  243. int qemu_fwcfg_online_cpus(void)
  244. {
  245. uint16_t nb_cpus;
  246. if (!fwcfg_present)
  247. return -ENODEV;
  248. qemu_fwcfg_read_entry(FW_CFG_NB_CPUS, 2, &nb_cpus);
  249. return le16_to_cpu(nb_cpus);
  250. }
  251. int qemu_fwcfg_read_firmware_list(void)
  252. {
  253. int i;
  254. uint32_t count;
  255. struct fw_file *file;
  256. struct list_head *entry;
  257. /* don't read it twice */
  258. if (!list_empty(&fw_list))
  259. return 0;
  260. qemu_fwcfg_read_entry(FW_CFG_FILE_DIR, 4, &count);
  261. if (!count)
  262. return 0;
  263. count = be32_to_cpu(count);
  264. for (i = 0; i < count; i++) {
  265. file = malloc(sizeof(*file));
  266. if (!file) {
  267. printf("error: allocating resource\n");
  268. goto err;
  269. }
  270. qemu_fwcfg_read_entry(FW_CFG_INVALID,
  271. sizeof(struct fw_cfg_file), &file->cfg);
  272. file->addr = 0;
  273. list_add_tail(&file->list, &fw_list);
  274. }
  275. return 0;
  276. err:
  277. list_for_each(entry, &fw_list) {
  278. file = list_entry(entry, struct fw_file, list);
  279. free(file);
  280. }
  281. return -ENOMEM;
  282. }
  283. struct fw_file *qemu_fwcfg_find_file(const char *name)
  284. {
  285. struct list_head *entry;
  286. struct fw_file *file;
  287. list_for_each(entry, &fw_list) {
  288. file = list_entry(entry, struct fw_file, list);
  289. if (!strcmp(file->cfg.name, name))
  290. return file;
  291. }
  292. return NULL;
  293. }
  294. struct fw_file *qemu_fwcfg_file_iter_init(struct fw_cfg_file_iter *iter)
  295. {
  296. iter->entry = fw_list.next;
  297. return list_entry((struct list_head *)iter->entry,
  298. struct fw_file, list);
  299. }
  300. struct fw_file *qemu_fwcfg_file_iter_next(struct fw_cfg_file_iter *iter)
  301. {
  302. iter->entry = ((struct list_head *)iter->entry)->next;
  303. return list_entry((struct list_head *)iter->entry,
  304. struct fw_file, list);
  305. }
  306. bool qemu_fwcfg_file_iter_end(struct fw_cfg_file_iter *iter)
  307. {
  308. return iter->entry == &fw_list;
  309. }
  310. void qemu_fwcfg_init(struct fw_cfg_arch_ops *ops)
  311. {
  312. uint32_t qemu;
  313. uint32_t dma_enabled;
  314. fwcfg_present = false;
  315. fwcfg_dma_present = false;
  316. fwcfg_arch_ops = NULL;
  317. if (!ops || !ops->arch_read_pio || !ops->arch_read_dma)
  318. return;
  319. fwcfg_arch_ops = ops;
  320. qemu_fwcfg_read_entry_pio(FW_CFG_SIGNATURE, 4, &qemu);
  321. if (be32_to_cpu(qemu) == QEMU_FW_CFG_SIGNATURE)
  322. fwcfg_present = true;
  323. if (fwcfg_present) {
  324. qemu_fwcfg_read_entry_pio(FW_CFG_ID, 1, &dma_enabled);
  325. if (dma_enabled & FW_CFG_DMA_ENABLED)
  326. fwcfg_dma_present = true;
  327. }
  328. }