fw_cfg.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * QEMU Firmware configuration device emulation
  3. *
  4. * Copyright (c) 2008 Gleb Natapov
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "sysemu.h"
  26. #include "isa.h"
  27. #include "fw_cfg.h"
  28. /* debug firmware config */
  29. //#define DEBUG_FW_CFG
  30. #ifdef DEBUG_FW_CFG
  31. #define FW_CFG_DPRINTF(fmt, args...) \
  32. do { printf("FW_CFG: " fmt , ##args); } while (0)
  33. #else
  34. #define FW_CFG_DPRINTF(fmt, args...)
  35. #endif
  36. #define FW_CFG_SIZE 2
  37. typedef struct _FWCfgEntry {
  38. uint16_t len;
  39. uint8_t *data;
  40. void *callback_opaque;
  41. FWCfgCallback callback;
  42. } FWCfgEntry;
  43. typedef struct _FWCfgState {
  44. FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
  45. uint16_t cur_entry;
  46. uint16_t cur_offset;
  47. } FWCfgState;
  48. static void fw_cfg_write(FWCfgState *s, uint8_t value)
  49. {
  50. int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
  51. FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
  52. FW_CFG_DPRINTF("write %d\n", value);
  53. if (s->cur_entry & FW_CFG_WRITE_CHANNEL && s->cur_offset < e->len) {
  54. e->data[s->cur_offset++] = value;
  55. if (s->cur_offset == e->len) {
  56. e->callback(e->callback_opaque, e->data);
  57. s->cur_offset = 0;
  58. }
  59. }
  60. }
  61. static int fw_cfg_select(FWCfgState *s, uint16_t key)
  62. {
  63. int ret;
  64. s->cur_offset = 0;
  65. if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
  66. s->cur_entry = FW_CFG_INVALID;
  67. ret = 0;
  68. } else {
  69. s->cur_entry = key;
  70. ret = 1;
  71. }
  72. FW_CFG_DPRINTF("select key %d (%sfound)\n", key, ret ? "" : "not ");
  73. return ret;
  74. }
  75. static uint8_t fw_cfg_read(FWCfgState *s)
  76. {
  77. int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
  78. FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
  79. uint8_t ret;
  80. if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
  81. ret = 0;
  82. else
  83. ret = e->data[s->cur_offset++];
  84. FW_CFG_DPRINTF("read %d\n", ret);
  85. return ret;
  86. }
  87. static uint32_t fw_cfg_io_readb(void *opaque, uint32_t addr)
  88. {
  89. return fw_cfg_read(opaque);
  90. }
  91. static void fw_cfg_io_writeb(void *opaque, uint32_t addr, uint32_t value)
  92. {
  93. return fw_cfg_write(opaque, (uint8_t)value);
  94. }
  95. static void fw_cfg_io_writew(void *opaque, uint32_t addr, uint32_t value)
  96. {
  97. fw_cfg_select(opaque, (uint16_t)value);
  98. }
  99. static uint32_t fw_cfg_mem_readb(void *opaque, target_phys_addr_t addr)
  100. {
  101. return fw_cfg_read(opaque);
  102. }
  103. static void fw_cfg_mem_writeb(void *opaque, target_phys_addr_t addr,
  104. uint32_t value)
  105. {
  106. return fw_cfg_write(opaque, (uint8_t)value);
  107. }
  108. static void fw_cfg_mem_writew(void *opaque, target_phys_addr_t addr,
  109. uint32_t value)
  110. {
  111. fw_cfg_select(opaque, (uint16_t)value);
  112. }
  113. static CPUReadMemoryFunc *fw_cfg_ctl_mem_read[3] = {
  114. NULL,
  115. NULL,
  116. NULL,
  117. };
  118. static CPUWriteMemoryFunc *fw_cfg_ctl_mem_write[3] = {
  119. NULL,
  120. fw_cfg_mem_writew,
  121. NULL,
  122. };
  123. static CPUReadMemoryFunc *fw_cfg_data_mem_read[3] = {
  124. fw_cfg_mem_readb,
  125. NULL,
  126. NULL,
  127. };
  128. static CPUWriteMemoryFunc *fw_cfg_data_mem_write[3] = {
  129. fw_cfg_mem_writeb,
  130. NULL,
  131. NULL,
  132. };
  133. static void fw_cfg_reset(void *opaque)
  134. {
  135. FWCfgState *s = opaque;
  136. fw_cfg_select(s, 0);
  137. }
  138. static void fw_cfg_save(QEMUFile *f, void *opaque)
  139. {
  140. FWCfgState *s = opaque;
  141. qemu_put_be16s(f, &s->cur_entry);
  142. qemu_put_be16s(f, &s->cur_offset);
  143. }
  144. static int fw_cfg_load(QEMUFile *f, void *opaque, int version_id)
  145. {
  146. FWCfgState *s = opaque;
  147. if (version_id > 1)
  148. return -EINVAL;
  149. qemu_get_be16s(f, &s->cur_entry);
  150. qemu_get_be16s(f, &s->cur_offset);
  151. return 0;
  152. }
  153. int fw_cfg_add_bytes(void *opaque, uint16_t key, uint8_t *data, uint16_t len)
  154. {
  155. FWCfgState *s = opaque;
  156. int arch = !!(key & FW_CFG_ARCH_LOCAL);
  157. key &= FW_CFG_ENTRY_MASK;
  158. if (key >= FW_CFG_MAX_ENTRY)
  159. return 0;
  160. s->entries[arch][key].data = data;
  161. s->entries[arch][key].len = len;
  162. return 1;
  163. }
  164. int fw_cfg_add_i16(void *opaque, uint16_t key, uint16_t value)
  165. {
  166. uint16_t *copy;
  167. copy = qemu_malloc(sizeof(value));
  168. *copy = cpu_to_le16(value);
  169. return fw_cfg_add_bytes(opaque, key, (uint8_t *)copy, sizeof(value));
  170. }
  171. int fw_cfg_add_i32(void *opaque, uint16_t key, uint32_t value)
  172. {
  173. uint32_t *copy;
  174. copy = qemu_malloc(sizeof(value));
  175. *copy = cpu_to_le32(value);
  176. return fw_cfg_add_bytes(opaque, key, (uint8_t *)copy, sizeof(value));
  177. }
  178. int fw_cfg_add_i64(void *opaque, uint16_t key, uint64_t value)
  179. {
  180. uint64_t *copy;
  181. copy = qemu_malloc(sizeof(value));
  182. *copy = cpu_to_le64(value);
  183. return fw_cfg_add_bytes(opaque, key, (uint8_t *)copy, sizeof(value));
  184. }
  185. int fw_cfg_add_callback(void *opaque, uint16_t key, FWCfgCallback callback,
  186. void *callback_opaque, uint8_t *data, size_t len)
  187. {
  188. FWCfgState *s = opaque;
  189. int arch = !!(key & FW_CFG_ARCH_LOCAL);
  190. if (!(key & FW_CFG_WRITE_CHANNEL))
  191. return 0;
  192. key &= FW_CFG_ENTRY_MASK;
  193. if (key >= FW_CFG_MAX_ENTRY || len > 65535)
  194. return 0;
  195. s->entries[arch][key].data = data;
  196. s->entries[arch][key].len = len;
  197. s->entries[arch][key].callback_opaque = callback_opaque;
  198. s->entries[arch][key].callback = callback;
  199. return 1;
  200. }
  201. void *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
  202. target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
  203. {
  204. FWCfgState *s;
  205. int io_ctl_memory, io_data_memory;
  206. s = qemu_mallocz(sizeof(FWCfgState));
  207. if (ctl_port) {
  208. register_ioport_write(ctl_port, 2, 2, fw_cfg_io_writew, s);
  209. }
  210. if (data_port) {
  211. register_ioport_read(data_port, 1, 1, fw_cfg_io_readb, s);
  212. register_ioport_write(data_port, 1, 1, fw_cfg_io_writeb, s);
  213. }
  214. if (ctl_addr) {
  215. io_ctl_memory = cpu_register_io_memory(0, fw_cfg_ctl_mem_read,
  216. fw_cfg_ctl_mem_write, s);
  217. cpu_register_physical_memory(ctl_addr, FW_CFG_SIZE, io_ctl_memory);
  218. }
  219. if (data_addr) {
  220. io_data_memory = cpu_register_io_memory(0, fw_cfg_data_mem_read,
  221. fw_cfg_data_mem_write, s);
  222. cpu_register_physical_memory(data_addr, FW_CFG_SIZE, io_data_memory);
  223. }
  224. fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (uint8_t *)"QEMU", 4);
  225. fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
  226. fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)nographic);
  227. fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
  228. register_savevm("fw_cfg", -1, 1, fw_cfg_save, fw_cfg_load, s);
  229. qemu_register_reset(fw_cfg_reset, s);
  230. fw_cfg_reset(s);
  231. return s;
  232. }