qfw.c 9.5 KB

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