xlnx-zynqmp-ipi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * QEMU model of the IPI Inter Processor Interrupt block
  3. *
  4. * Copyright (c) 2014 Xilinx Inc.
  5. *
  6. * Written by Edgar E. Iglesias <edgar.iglesias@xilinx.com>
  7. * Written by Alistair Francis <alistair.francis@xilinx.com>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "qemu/osdep.h"
  28. #include "hw/sysbus.h"
  29. #include "migration/vmstate.h"
  30. #include "hw/register.h"
  31. #include "qemu/bitops.h"
  32. #include "qemu/log.h"
  33. #include "qemu/module.h"
  34. #include "hw/intc/xlnx-zynqmp-ipi.h"
  35. #include "hw/irq.h"
  36. #ifndef XLNX_ZYNQMP_IPI_ERR_DEBUG
  37. #define XLNX_ZYNQMP_IPI_ERR_DEBUG 0
  38. #endif
  39. #define DB_PRINT_L(lvl, fmt, args...) do {\
  40. if (XLNX_ZYNQMP_IPI_ERR_DEBUG >= lvl) {\
  41. qemu_log(TYPE_XLNX_ZYNQMP_IPI ": %s:" fmt, __func__, ## args);\
  42. } \
  43. } while (0)
  44. #define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
  45. REG32(IPI_TRIG, 0x0)
  46. FIELD(IPI_TRIG, PL_3, 27, 1)
  47. FIELD(IPI_TRIG, PL_2, 26, 1)
  48. FIELD(IPI_TRIG, PL_1, 25, 1)
  49. FIELD(IPI_TRIG, PL_0, 24, 1)
  50. FIELD(IPI_TRIG, PMU_3, 19, 1)
  51. FIELD(IPI_TRIG, PMU_2, 18, 1)
  52. FIELD(IPI_TRIG, PMU_1, 17, 1)
  53. FIELD(IPI_TRIG, PMU_0, 16, 1)
  54. FIELD(IPI_TRIG, RPU_1, 9, 1)
  55. FIELD(IPI_TRIG, RPU_0, 8, 1)
  56. FIELD(IPI_TRIG, APU, 0, 1)
  57. REG32(IPI_OBS, 0x4)
  58. FIELD(IPI_OBS, PL_3, 27, 1)
  59. FIELD(IPI_OBS, PL_2, 26, 1)
  60. FIELD(IPI_OBS, PL_1, 25, 1)
  61. FIELD(IPI_OBS, PL_0, 24, 1)
  62. FIELD(IPI_OBS, PMU_3, 19, 1)
  63. FIELD(IPI_OBS, PMU_2, 18, 1)
  64. FIELD(IPI_OBS, PMU_1, 17, 1)
  65. FIELD(IPI_OBS, PMU_0, 16, 1)
  66. FIELD(IPI_OBS, RPU_1, 9, 1)
  67. FIELD(IPI_OBS, RPU_0, 8, 1)
  68. FIELD(IPI_OBS, APU, 0, 1)
  69. REG32(IPI_ISR, 0x10)
  70. FIELD(IPI_ISR, PL_3, 27, 1)
  71. FIELD(IPI_ISR, PL_2, 26, 1)
  72. FIELD(IPI_ISR, PL_1, 25, 1)
  73. FIELD(IPI_ISR, PL_0, 24, 1)
  74. FIELD(IPI_ISR, PMU_3, 19, 1)
  75. FIELD(IPI_ISR, PMU_2, 18, 1)
  76. FIELD(IPI_ISR, PMU_1, 17, 1)
  77. FIELD(IPI_ISR, PMU_0, 16, 1)
  78. FIELD(IPI_ISR, RPU_1, 9, 1)
  79. FIELD(IPI_ISR, RPU_0, 8, 1)
  80. FIELD(IPI_ISR, APU, 0, 1)
  81. REG32(IPI_IMR, 0x14)
  82. FIELD(IPI_IMR, PL_3, 27, 1)
  83. FIELD(IPI_IMR, PL_2, 26, 1)
  84. FIELD(IPI_IMR, PL_1, 25, 1)
  85. FIELD(IPI_IMR, PL_0, 24, 1)
  86. FIELD(IPI_IMR, PMU_3, 19, 1)
  87. FIELD(IPI_IMR, PMU_2, 18, 1)
  88. FIELD(IPI_IMR, PMU_1, 17, 1)
  89. FIELD(IPI_IMR, PMU_0, 16, 1)
  90. FIELD(IPI_IMR, RPU_1, 9, 1)
  91. FIELD(IPI_IMR, RPU_0, 8, 1)
  92. FIELD(IPI_IMR, APU, 0, 1)
  93. REG32(IPI_IER, 0x18)
  94. FIELD(IPI_IER, PL_3, 27, 1)
  95. FIELD(IPI_IER, PL_2, 26, 1)
  96. FIELD(IPI_IER, PL_1, 25, 1)
  97. FIELD(IPI_IER, PL_0, 24, 1)
  98. FIELD(IPI_IER, PMU_3, 19, 1)
  99. FIELD(IPI_IER, PMU_2, 18, 1)
  100. FIELD(IPI_IER, PMU_1, 17, 1)
  101. FIELD(IPI_IER, PMU_0, 16, 1)
  102. FIELD(IPI_IER, RPU_1, 9, 1)
  103. FIELD(IPI_IER, RPU_0, 8, 1)
  104. FIELD(IPI_IER, APU, 0, 1)
  105. REG32(IPI_IDR, 0x1c)
  106. FIELD(IPI_IDR, PL_3, 27, 1)
  107. FIELD(IPI_IDR, PL_2, 26, 1)
  108. FIELD(IPI_IDR, PL_1, 25, 1)
  109. FIELD(IPI_IDR, PL_0, 24, 1)
  110. FIELD(IPI_IDR, PMU_3, 19, 1)
  111. FIELD(IPI_IDR, PMU_2, 18, 1)
  112. FIELD(IPI_IDR, PMU_1, 17, 1)
  113. FIELD(IPI_IDR, PMU_0, 16, 1)
  114. FIELD(IPI_IDR, RPU_1, 9, 1)
  115. FIELD(IPI_IDR, RPU_0, 8, 1)
  116. FIELD(IPI_IDR, APU, 0, 1)
  117. /* APU
  118. * RPU_0
  119. * RPU_1
  120. * PMU_0
  121. * PMU_1
  122. * PMU_2
  123. * PMU_3
  124. * PL_0
  125. * PL_1
  126. * PL_2
  127. * PL_3
  128. */
  129. int index_array[NUM_IPIS] = {0, 8, 9, 16, 17, 18, 19, 24, 25, 26, 27};
  130. static const char *index_array_names[NUM_IPIS] = {"APU", "RPU_0", "RPU_1",
  131. "PMU_0", "PMU_1", "PMU_2",
  132. "PMU_3", "PL_0", "PL_1",
  133. "PL_2", "PL_3"};
  134. static void xlnx_zynqmp_ipi_set_trig(XlnxZynqMPIPI *s, uint32_t val)
  135. {
  136. int i, ipi_index, ipi_mask;
  137. for (i = 0; i < NUM_IPIS; i++) {
  138. ipi_index = index_array[i];
  139. ipi_mask = (1 << ipi_index);
  140. DB_PRINT("Setting %s=%d\n", index_array_names[i],
  141. !!(val & ipi_mask));
  142. qemu_set_irq(s->irq_trig_out[i], !!(val & ipi_mask));
  143. }
  144. }
  145. static void xlnx_zynqmp_ipi_set_obs(XlnxZynqMPIPI *s, uint32_t val)
  146. {
  147. int i, ipi_index, ipi_mask;
  148. for (i = 0; i < NUM_IPIS; i++) {
  149. ipi_index = index_array[i];
  150. ipi_mask = (1 << ipi_index);
  151. DB_PRINT("Setting %s=%d\n", index_array_names[i],
  152. !!(val & ipi_mask));
  153. qemu_set_irq(s->irq_obs_out[i], !!(val & ipi_mask));
  154. }
  155. }
  156. static void xlnx_zynqmp_ipi_update_irq(XlnxZynqMPIPI *s)
  157. {
  158. bool pending = s->regs[R_IPI_ISR] & ~s->regs[R_IPI_IMR];
  159. DB_PRINT("irq=%d isr=%x mask=%x\n",
  160. pending, s->regs[R_IPI_ISR], s->regs[R_IPI_IMR]);
  161. qemu_set_irq(s->irq, pending);
  162. }
  163. static uint64_t xlnx_zynqmp_ipi_trig_prew(RegisterInfo *reg, uint64_t val64)
  164. {
  165. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(reg->opaque);
  166. xlnx_zynqmp_ipi_set_trig(s, val64);
  167. return val64;
  168. }
  169. static void xlnx_zynqmp_ipi_trig_postw(RegisterInfo *reg, uint64_t val64)
  170. {
  171. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(reg->opaque);
  172. /* TRIG generates a pulse on the outbound signals. We use the
  173. * post-write callback to bring the signal back-down.
  174. */
  175. s->regs[R_IPI_TRIG] = 0;
  176. xlnx_zynqmp_ipi_set_trig(s, 0);
  177. }
  178. static uint64_t xlnx_zynqmp_ipi_isr_prew(RegisterInfo *reg, uint64_t val64)
  179. {
  180. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(reg->opaque);
  181. xlnx_zynqmp_ipi_set_obs(s, val64);
  182. return val64;
  183. }
  184. static void xlnx_zynqmp_ipi_isr_postw(RegisterInfo *reg, uint64_t val64)
  185. {
  186. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(reg->opaque);
  187. xlnx_zynqmp_ipi_update_irq(s);
  188. }
  189. static uint64_t xlnx_zynqmp_ipi_ier_prew(RegisterInfo *reg, uint64_t val64)
  190. {
  191. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(reg->opaque);
  192. uint32_t val = val64;
  193. s->regs[R_IPI_IMR] &= ~val;
  194. xlnx_zynqmp_ipi_update_irq(s);
  195. return 0;
  196. }
  197. static uint64_t xlnx_zynqmp_ipi_idr_prew(RegisterInfo *reg, uint64_t val64)
  198. {
  199. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(reg->opaque);
  200. uint32_t val = val64;
  201. s->regs[R_IPI_IMR] |= val;
  202. xlnx_zynqmp_ipi_update_irq(s);
  203. return 0;
  204. }
  205. static const RegisterAccessInfo xlnx_zynqmp_ipi_regs_info[] = {
  206. { .name = "IPI_TRIG", .addr = A_IPI_TRIG,
  207. .rsvd = 0xf0f0fcfe,
  208. .ro = 0xf0f0fcfe,
  209. .pre_write = xlnx_zynqmp_ipi_trig_prew,
  210. .post_write = xlnx_zynqmp_ipi_trig_postw,
  211. },{ .name = "IPI_OBS", .addr = A_IPI_OBS,
  212. .rsvd = 0xf0f0fcfe,
  213. .ro = 0xffffffff,
  214. },{ .name = "IPI_ISR", .addr = A_IPI_ISR,
  215. .rsvd = 0xf0f0fcfe,
  216. .ro = 0xf0f0fcfe,
  217. .w1c = 0xf0f0301,
  218. .pre_write = xlnx_zynqmp_ipi_isr_prew,
  219. .post_write = xlnx_zynqmp_ipi_isr_postw,
  220. },{ .name = "IPI_IMR", .addr = A_IPI_IMR,
  221. .reset = 0xf0f0301,
  222. .rsvd = 0xf0f0fcfe,
  223. .ro = 0xffffffff,
  224. },{ .name = "IPI_IER", .addr = A_IPI_IER,
  225. .rsvd = 0xf0f0fcfe,
  226. .ro = 0xf0f0fcfe,
  227. .pre_write = xlnx_zynqmp_ipi_ier_prew,
  228. },{ .name = "IPI_IDR", .addr = A_IPI_IDR,
  229. .rsvd = 0xf0f0fcfe,
  230. .ro = 0xf0f0fcfe,
  231. .pre_write = xlnx_zynqmp_ipi_idr_prew,
  232. }
  233. };
  234. static void xlnx_zynqmp_ipi_reset(DeviceState *dev)
  235. {
  236. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(dev);
  237. int i;
  238. for (i = 0; i < ARRAY_SIZE(s->regs_info); ++i) {
  239. register_reset(&s->regs_info[i]);
  240. }
  241. xlnx_zynqmp_ipi_update_irq(s);
  242. }
  243. static void xlnx_zynqmp_ipi_handler(void *opaque, int n, int level)
  244. {
  245. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(opaque);
  246. uint32_t val = (!!level) << n;
  247. DB_PRINT("IPI input irq[%d]=%d\n", n, level);
  248. s->regs[R_IPI_ISR] |= val;
  249. xlnx_zynqmp_ipi_set_obs(s, s->regs[R_IPI_ISR]);
  250. xlnx_zynqmp_ipi_update_irq(s);
  251. }
  252. static void xlnx_zynqmp_obs_handler(void *opaque, int n, int level)
  253. {
  254. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(opaque);
  255. DB_PRINT("OBS input irq[%d]=%d\n", n, level);
  256. s->regs[R_IPI_OBS] &= ~(1ULL << n);
  257. s->regs[R_IPI_OBS] |= (level << n);
  258. }
  259. static const MemoryRegionOps xlnx_zynqmp_ipi_ops = {
  260. .read = register_read_memory,
  261. .write = register_write_memory,
  262. .endianness = DEVICE_LITTLE_ENDIAN,
  263. .valid = {
  264. .min_access_size = 4,
  265. .max_access_size = 4,
  266. },
  267. };
  268. static void xlnx_zynqmp_ipi_realize(DeviceState *dev, Error **errp)
  269. {
  270. qdev_init_gpio_in_named(dev, xlnx_zynqmp_ipi_handler, "IPI_INPUTS", 32);
  271. qdev_init_gpio_in_named(dev, xlnx_zynqmp_obs_handler, "OBS_INPUTS", 32);
  272. }
  273. static void xlnx_zynqmp_ipi_init(Object *obj)
  274. {
  275. XlnxZynqMPIPI *s = XLNX_ZYNQMP_IPI(obj);
  276. DeviceState *dev = DEVICE(obj);
  277. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  278. RegisterInfoArray *reg_array;
  279. char *irq_name;
  280. int i;
  281. memory_region_init(&s->iomem, obj, TYPE_XLNX_ZYNQMP_IPI,
  282. R_XLNX_ZYNQMP_IPI_MAX * 4);
  283. reg_array =
  284. register_init_block32(DEVICE(obj), xlnx_zynqmp_ipi_regs_info,
  285. ARRAY_SIZE(xlnx_zynqmp_ipi_regs_info),
  286. s->regs_info, s->regs,
  287. &xlnx_zynqmp_ipi_ops,
  288. XLNX_ZYNQMP_IPI_ERR_DEBUG,
  289. R_XLNX_ZYNQMP_IPI_MAX * 4);
  290. memory_region_add_subregion(&s->iomem,
  291. 0x0,
  292. &reg_array->mem);
  293. sysbus_init_mmio(sbd, &s->iomem);
  294. sysbus_init_irq(sbd, &s->irq);
  295. for (i = 0; i < NUM_IPIS; i++) {
  296. qdev_init_gpio_out_named(dev, &s->irq_trig_out[i],
  297. index_array_names[i], 1);
  298. irq_name = g_strdup_printf("OBS_%s", index_array_names[i]);
  299. qdev_init_gpio_out_named(dev, &s->irq_obs_out[i],
  300. irq_name, 1);
  301. g_free(irq_name);
  302. }
  303. }
  304. static const VMStateDescription vmstate_zynqmp_pmu_ipi = {
  305. .name = TYPE_XLNX_ZYNQMP_IPI,
  306. .version_id = 1,
  307. .minimum_version_id = 1,
  308. .fields = (VMStateField[]) {
  309. VMSTATE_UINT32_ARRAY(regs, XlnxZynqMPIPI, R_XLNX_ZYNQMP_IPI_MAX),
  310. VMSTATE_END_OF_LIST(),
  311. }
  312. };
  313. static void xlnx_zynqmp_ipi_class_init(ObjectClass *klass, void *data)
  314. {
  315. DeviceClass *dc = DEVICE_CLASS(klass);
  316. dc->reset = xlnx_zynqmp_ipi_reset;
  317. dc->realize = xlnx_zynqmp_ipi_realize;
  318. dc->vmsd = &vmstate_zynqmp_pmu_ipi;
  319. }
  320. static const TypeInfo xlnx_zynqmp_ipi_info = {
  321. .name = TYPE_XLNX_ZYNQMP_IPI,
  322. .parent = TYPE_SYS_BUS_DEVICE,
  323. .instance_size = sizeof(XlnxZynqMPIPI),
  324. .class_init = xlnx_zynqmp_ipi_class_init,
  325. .instance_init = xlnx_zynqmp_ipi_init,
  326. };
  327. static void xlnx_zynqmp_ipi_register_types(void)
  328. {
  329. type_register_static(&xlnx_zynqmp_ipi_info);
  330. }
  331. type_init(xlnx_zynqmp_ipi_register_types)