qfw.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
  4. */
  5. #ifndef __FW_CFG__
  6. #define __FW_CFG__
  7. #include <linux/list.h>
  8. /*
  9. * List of firmware configuration item selectors. The official source of truth
  10. * for these is the QEMU source itself; see
  11. * https://github.com/qemu/qemu/blob/master/hw/nvram/fw_cfg.c
  12. */
  13. enum {
  14. FW_CFG_SIGNATURE = 0x00,
  15. FW_CFG_ID = 0x01,
  16. FW_CFG_UUID = 0x02,
  17. FW_CFG_RAM_SIZE = 0x03,
  18. FW_CFG_NOGRAPHIC = 0x04,
  19. FW_CFG_NB_CPUS = 0x05,
  20. FW_CFG_MACHINE_ID = 0x06,
  21. FW_CFG_KERNEL_ADDR = 0x07,
  22. FW_CFG_KERNEL_SIZE = 0x08,
  23. FW_CFG_KERNEL_CMDLINE = 0x09,
  24. FW_CFG_INITRD_ADDR = 0x0a,
  25. FW_CFG_INITRD_SIZE = 0x0b,
  26. FW_CFG_BOOT_DEVICE = 0x0c,
  27. FW_CFG_NUMA = 0x0d,
  28. FW_CFG_BOOT_MENU = 0x0e,
  29. FW_CFG_MAX_CPUS = 0x0f,
  30. FW_CFG_KERNEL_ENTRY = 0x10,
  31. FW_CFG_KERNEL_DATA = 0x11,
  32. FW_CFG_INITRD_DATA = 0x12,
  33. FW_CFG_CMDLINE_ADDR = 0x13,
  34. FW_CFG_CMDLINE_SIZE = 0x14,
  35. FW_CFG_CMDLINE_DATA = 0x15,
  36. FW_CFG_SETUP_ADDR = 0x16,
  37. FW_CFG_SETUP_SIZE = 0x17,
  38. FW_CFG_SETUP_DATA = 0x18,
  39. FW_CFG_FILE_DIR = 0x19,
  40. FW_CFG_FILE_FIRST = 0x20,
  41. FW_CFG_WRITE_CHANNEL = 0x4000,
  42. FW_CFG_ARCH_LOCAL = 0x8000,
  43. FW_CFG_INVALID = 0xffff,
  44. };
  45. enum {
  46. BIOS_LINKER_LOADER_COMMAND_ALLOCATE = 0x1,
  47. BIOS_LINKER_LOADER_COMMAND_ADD_POINTER = 0x2,
  48. BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM = 0x3,
  49. };
  50. enum {
  51. BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1,
  52. BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2,
  53. };
  54. #define FW_CFG_FILE_SLOTS 0x10
  55. #define FW_CFG_MAX_ENTRY (FW_CFG_FILE_FIRST + FW_CFG_FILE_SLOTS)
  56. #define FW_CFG_ENTRY_MASK ~(FW_CFG_WRITE_CHANNEL | FW_CFG_ARCH_LOCAL)
  57. #define FW_CFG_MAX_FILE_PATH 56
  58. #define BIOS_LINKER_LOADER_FILESZ FW_CFG_MAX_FILE_PATH
  59. #define QEMU_FW_CFG_SIGNATURE (('Q' << 24) | ('E' << 16) | ('M' << 8) | 'U')
  60. #define FW_CFG_DMA_ERROR (1 << 0)
  61. #define FW_CFG_DMA_READ (1 << 1)
  62. #define FW_CFG_DMA_SKIP (1 << 2)
  63. #define FW_CFG_DMA_SELECT (1 << 3)
  64. /* Bit set in FW_CFG_ID response to indicate DMA interface availability. */
  65. #define FW_CFG_DMA_ENABLED (1 << 1)
  66. /* Structs read from FW_CFG_FILE_DIR. */
  67. struct fw_cfg_file {
  68. __be32 size;
  69. __be16 select;
  70. __be16 reserved;
  71. char name[FW_CFG_MAX_FILE_PATH];
  72. };
  73. struct fw_file {
  74. struct fw_cfg_file cfg; /* firmware file information */
  75. unsigned long addr; /* firmware file in-memory address */
  76. struct list_head list; /* list node to link to fw_list */
  77. };
  78. struct fw_cfg_file_iter {
  79. struct list_head *entry, *end; /* structures to iterate file list */
  80. };
  81. struct bios_linker_entry {
  82. __le32 command;
  83. union {
  84. /*
  85. * COMMAND_ALLOCATE - allocate a table from @alloc.file
  86. * subject to @alloc.align alignment (must be power of 2)
  87. * and @alloc.zone (can be HIGH or FSEG) requirements.
  88. *
  89. * Must appear exactly once for each file, and before
  90. * this file is referenced by any other command.
  91. */
  92. struct {
  93. char file[BIOS_LINKER_LOADER_FILESZ];
  94. __le32 align;
  95. uint8_t zone;
  96. } alloc;
  97. /*
  98. * COMMAND_ADD_POINTER - patch the table (originating from
  99. * @dest_file) at @pointer.offset, by adding a pointer to the
  100. * table originating from @src_file. 1,2,4 or 8 byte unsigned
  101. * addition is used depending on @pointer.size.
  102. */
  103. struct {
  104. char dest_file[BIOS_LINKER_LOADER_FILESZ];
  105. char src_file[BIOS_LINKER_LOADER_FILESZ];
  106. __le32 offset;
  107. uint8_t size;
  108. } pointer;
  109. /*
  110. * COMMAND_ADD_CHECKSUM - calculate checksum of the range
  111. * specified by @cksum_start and @cksum_length fields,
  112. * and then add the value at @cksum.offset.
  113. * Checksum simply sums -X for each byte X in the range
  114. * using 8-bit math.
  115. */
  116. struct {
  117. char file[BIOS_LINKER_LOADER_FILESZ];
  118. __le32 offset;
  119. __le32 start;
  120. __le32 length;
  121. } cksum;
  122. /* padding */
  123. char pad[124];
  124. };
  125. } __packed;
  126. /* DMA transfer control data between UCLASS_QFW and QEMU. */
  127. struct qfw_dma {
  128. __be32 control;
  129. __be32 length;
  130. __be64 address;
  131. };
  132. /* uclass per-device configuration information */
  133. struct qfw_dev {
  134. struct udevice *dev; /* Transport device */
  135. bool dma_present; /* DMA interface usable? */
  136. struct list_head fw_list; /* Cached firmware file list */
  137. };
  138. /* Ops used internally between UCLASS_QFW and its driver implementations. */
  139. struct dm_qfw_ops {
  140. /**
  141. * read_entry_io() - Read a firmware config entry using the regular
  142. * IO interface for the platform (either PIO or MMIO)
  143. *
  144. * Supply %FW_CFG_INVALID as the entry to continue a previous read. In
  145. * this case, no selector will be issued before reading.
  146. *
  147. * @dev: Device to use
  148. * @entry: Firmware config entry number (e.g. %FW_CFG_SIGNATURE)
  149. * @size: Number of bytes to read
  150. * @address: Target location for read
  151. */
  152. void (*read_entry_io)(struct udevice *dev, u16 entry, u32 size,
  153. void *address);
  154. /**
  155. * read_entry_dma() - Read a firmware config entry using the DMA
  156. * interface
  157. *
  158. * Supply FW_CFG_INVALID as the entry to continue a previous read. In
  159. * this case, no selector will be issued before reading.
  160. *
  161. * This method assumes DMA availability has already been confirmed.
  162. *
  163. * @dev: Device to use
  164. * @dma: DMA transfer control struct
  165. */
  166. void (*read_entry_dma)(struct udevice *dev, struct qfw_dma *dma);
  167. };
  168. #define dm_qfw_get_ops(dev) \
  169. ((struct dm_qfw_ops *)(dev)->driver->ops)
  170. /**
  171. * qfw_register() - Called by a qfw driver after successful probe.
  172. * @dev: Device registering itself with the uclass.
  173. *
  174. * Used internally by driver implementations on successful probe.
  175. *
  176. * Return: 0 on success, negative otherwise.
  177. */
  178. int qfw_register(struct udevice *dev);
  179. struct udevice;
  180. /**
  181. * qfw_get_dev() - Get QEMU firmware config device.
  182. * @devp: Pointer to be filled with address of the qfw device.
  183. *
  184. * Gets the active QEMU firmware config device, for use with qfw_read_entry()
  185. * and others.
  186. *
  187. * Return: 0 on success, -ENODEV if the device is not available.
  188. */
  189. int qfw_get_dev(struct udevice **devp);
  190. /**
  191. * qfw_read_entry() - Read a QEMU firmware config entry
  192. * @dev: QFW device to use.
  193. * @entry: Firmware config entry number (e.g. %FW_CFG_SIGNATURE).
  194. * @size: Number of bytes to read.
  195. * @address: Target location for read.
  196. *
  197. * Reads a QEMU firmware config entry using @dev. DMA will be used if the QEMU
  198. * machine supports it, otherwise PIO/MMIO.
  199. */
  200. void qfw_read_entry(struct udevice *dev, u16 entry, u32 size, void *address);
  201. /**
  202. * qfw_read_firmware_list() - Read and cache the QEMU firmware config file
  203. * list.
  204. * @dev: QFW device to use.
  205. *
  206. * Reads the QEMU firmware config file list, caching it against @dev for later
  207. * use with qfw_find_file().
  208. *
  209. * If the list has already been read, does nothing and returns 0 (success).
  210. *
  211. * Return: 0 on success, -ENOMEM if unable to allocate.
  212. */
  213. int qfw_read_firmware_list(struct udevice *dev);
  214. /**
  215. * qfw_find_file() - Find a file by name in the QEMU firmware config file
  216. * list.
  217. * @dev: QFW device to use.
  218. * @name: Name of file to locate (e.g. "etc/table-loader").
  219. *
  220. * Finds a file by name in the QEMU firmware config file list cached against
  221. * @dev. You must call qfw_read_firmware_list() successfully first for this to
  222. * succeed.
  223. *
  224. * Return: Pointer to &struct fw_file if found, %NULL if not present.
  225. */
  226. struct fw_file *qfw_find_file(struct udevice *dev, const char *name);
  227. /**
  228. * qfw_online_cpus() - Get number of CPUs in system from QEMU firmware config.
  229. * @dev: QFW device to use.
  230. *
  231. * Asks QEMU to report how many CPUs it is emulating for the machine.
  232. *
  233. * Return: Number of CPUs in the system.
  234. */
  235. int qfw_online_cpus(struct udevice *dev);
  236. /**
  237. * qfw_file_iter_init() - Start iterating cached firmware file list.
  238. * @dev: QFW device to use.
  239. * @iter: Iterator to be initialised.
  240. *
  241. * Starts iterating the cached firmware file list in @dev. You must call
  242. * qfw_read_firmware_list() successfully first, otherwise you will always get
  243. * an empty list.
  244. *
  245. * qfw_file_iter_init() returns the first &struct fw_file, but it may be
  246. * invalid if the list is empty. Check that ``!qfw_file_iter_end(&iter)``
  247. * first.
  248. *
  249. * Return: The first &struct fw_file item in the firmware file list, if any.
  250. * Only valid when qfw_file_iter_end() is not true after the call.
  251. */
  252. struct fw_file *qfw_file_iter_init(struct udevice *dev,
  253. struct fw_cfg_file_iter *iter);
  254. /**
  255. * qfw_file_iter_next() - Iterate cached firmware file list.
  256. * @iter: Iterator to use.
  257. *
  258. * Continues iterating the cached firmware file list in @dev. You must call
  259. * qfw_file_iter_init() first to initialise it. Check that
  260. * ``!qfw_file_iter_end(&iter)`` before using the return value of this
  261. * function.
  262. *
  263. * Return: The next &struct fw_file item in the firmware file list. Only valid
  264. * when qfw_file_iter_end() is not true after the call.
  265. */
  266. struct fw_file *qfw_file_iter_next(struct fw_cfg_file_iter *iter);
  267. /**
  268. * qfw_file_iter_end() - Check if iter is at end of list.
  269. * @iter: Iterator to use.
  270. *
  271. * Checks whether or not the iterator is at its end position. If so, the
  272. * qfw_file_iter_init() or qfw_file_iter_next() call that immediately preceded
  273. * returned invalid data.
  274. *
  275. * Return: True if the iterator is at its end; false otherwise.
  276. */
  277. bool qfw_file_iter_end(struct fw_cfg_file_iter *iter);
  278. /**
  279. * qemu_cpu_fixup() - Fix up the CPUs for QEMU.
  280. *
  281. * Return: 0 on success, -ENODEV if no CPUs, -ENOMEM if out of memory, other <
  282. * 0 on on other error.
  283. */
  284. int qemu_cpu_fixup(void);
  285. #endif