ibex_plic.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * QEMU RISC-V lowRISC Ibex PLIC
  3. *
  4. * Copyright (c) 2020 Western Digital
  5. *
  6. * Documentation avaliable: https://docs.opentitan.org/hw/ip/rv_plic/doc/
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2 or later, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/log.h"
  22. #include "hw/qdev-properties.h"
  23. #include "hw/core/cpu.h"
  24. #include "hw/boards.h"
  25. #include "hw/pci/msi.h"
  26. #include "target/riscv/cpu_bits.h"
  27. #include "target/riscv/cpu.h"
  28. #include "hw/intc/ibex_plic.h"
  29. static bool addr_between(uint32_t addr, uint32_t base, uint32_t num)
  30. {
  31. uint32_t end = base + (num * 0x04);
  32. if (addr >= base && addr < end) {
  33. return true;
  34. }
  35. return false;
  36. }
  37. static void ibex_plic_irqs_set_pending(IbexPlicState *s, int irq, bool level)
  38. {
  39. int pending_num = irq / 32;
  40. if (s->claimed[pending_num] & 1 << (irq % 32)) {
  41. /*
  42. * The interrupt has been claimed, but not compelted.
  43. * The pending bit can't be set.
  44. */
  45. return;
  46. }
  47. s->pending[pending_num] |= level << (irq % 32);
  48. }
  49. static bool ibex_plic_irqs_pending(IbexPlicState *s, uint32_t context)
  50. {
  51. int i;
  52. uint32_t max_irq = 0;
  53. uint32_t max_prio = s->threshold;
  54. for (i = 0; i < s->pending_num; i++) {
  55. uint32_t irq_num = ctz64(s->pending[i]) + (i * 32);
  56. if (!(s->pending[i] & s->enable[i])) {
  57. /* No pending and enabled IRQ */
  58. continue;
  59. }
  60. if (s->priority[irq_num] > max_prio) {
  61. max_irq = irq_num;
  62. max_prio = s->priority[irq_num];
  63. }
  64. }
  65. if (max_irq) {
  66. s->claim = max_irq;
  67. return true;
  68. }
  69. return false;
  70. }
  71. static void ibex_plic_update(IbexPlicState *s)
  72. {
  73. CPUState *cpu;
  74. int level, i;
  75. for (i = 0; i < s->num_cpus; i++) {
  76. cpu = qemu_get_cpu(i);
  77. if (!cpu) {
  78. continue;
  79. }
  80. level = ibex_plic_irqs_pending(s, 0);
  81. riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_MEIP, BOOL_TO_MASK(level));
  82. }
  83. }
  84. static void ibex_plic_reset(DeviceState *dev)
  85. {
  86. IbexPlicState *s = IBEX_PLIC(dev);
  87. s->threshold = 0x00000000;
  88. s->claim = 0x00000000;
  89. }
  90. static uint64_t ibex_plic_read(void *opaque, hwaddr addr,
  91. unsigned int size)
  92. {
  93. IbexPlicState *s = opaque;
  94. int offset;
  95. uint32_t ret = 0;
  96. if (addr_between(addr, s->pending_base, s->pending_num)) {
  97. offset = (addr - s->pending_base) / 4;
  98. ret = s->pending[offset];
  99. } else if (addr_between(addr, s->source_base, s->source_num)) {
  100. qemu_log_mask(LOG_UNIMP,
  101. "%s: Interrupt source mode not supported\n", __func__);
  102. } else if (addr_between(addr, s->priority_base, s->priority_num)) {
  103. offset = (addr - s->priority_base) / 4;
  104. ret = s->priority[offset];
  105. } else if (addr_between(addr, s->enable_base, s->enable_num)) {
  106. offset = (addr - s->enable_base) / 4;
  107. ret = s->enable[offset];
  108. } else if (addr_between(addr, s->threshold_base, 1)) {
  109. ret = s->threshold;
  110. } else if (addr_between(addr, s->claim_base, 1)) {
  111. int pending_num = s->claim / 32;
  112. s->pending[pending_num] &= ~(1 << (s->claim % 32));
  113. /* Set the interrupt as claimed, but not compelted */
  114. s->claimed[pending_num] |= 1 << (s->claim % 32);
  115. /* Return the current claimed interrupt */
  116. ret = s->claim;
  117. /* Update the interrupt status after the claim */
  118. ibex_plic_update(s);
  119. }
  120. return ret;
  121. }
  122. static void ibex_plic_write(void *opaque, hwaddr addr,
  123. uint64_t value, unsigned int size)
  124. {
  125. IbexPlicState *s = opaque;
  126. if (addr_between(addr, s->pending_base, s->pending_num)) {
  127. qemu_log_mask(LOG_GUEST_ERROR,
  128. "%s: Pending registers are read only\n", __func__);
  129. } else if (addr_between(addr, s->source_base, s->source_num)) {
  130. qemu_log_mask(LOG_UNIMP,
  131. "%s: Interrupt source mode not supported\n", __func__);
  132. } else if (addr_between(addr, s->priority_base, s->priority_num)) {
  133. uint32_t irq = ((addr - s->priority_base) >> 2) + 1;
  134. s->priority[irq] = value & 7;
  135. ibex_plic_update(s);
  136. } else if (addr_between(addr, s->enable_base, s->enable_num)) {
  137. uint32_t enable_reg = (addr - s->enable_base) / 4;
  138. s->enable[enable_reg] = value;
  139. } else if (addr_between(addr, s->threshold_base, 1)) {
  140. s->threshold = value & 3;
  141. } else if (addr_between(addr, s->claim_base, 1)) {
  142. if (s->claim == value) {
  143. /* Interrupt was completed */
  144. s->claim = 0;
  145. }
  146. if (s->claimed[value / 32] & 1 << (value % 32)) {
  147. /* This value was already claimed, clear it. */
  148. s->claimed[value / 32] &= ~(1 << (value % 32));
  149. }
  150. }
  151. ibex_plic_update(s);
  152. }
  153. static const MemoryRegionOps ibex_plic_ops = {
  154. .read = ibex_plic_read,
  155. .write = ibex_plic_write,
  156. .endianness = DEVICE_NATIVE_ENDIAN,
  157. .valid = {
  158. .min_access_size = 4,
  159. .max_access_size = 4
  160. }
  161. };
  162. static void ibex_plic_irq_request(void *opaque, int irq, int level)
  163. {
  164. IbexPlicState *s = opaque;
  165. ibex_plic_irqs_set_pending(s, irq, level > 0);
  166. ibex_plic_update(s);
  167. }
  168. static Property ibex_plic_properties[] = {
  169. DEFINE_PROP_UINT32("num-cpus", IbexPlicState, num_cpus, 1),
  170. DEFINE_PROP_UINT32("num-sources", IbexPlicState, num_sources, 80),
  171. DEFINE_PROP_UINT32("pending-base", IbexPlicState, pending_base, 0),
  172. DEFINE_PROP_UINT32("pending-num", IbexPlicState, pending_num, 3),
  173. DEFINE_PROP_UINT32("source-base", IbexPlicState, source_base, 0x0c),
  174. DEFINE_PROP_UINT32("source-num", IbexPlicState, source_num, 3),
  175. DEFINE_PROP_UINT32("priority-base", IbexPlicState, priority_base, 0x18),
  176. DEFINE_PROP_UINT32("priority-num", IbexPlicState, priority_num, 80),
  177. DEFINE_PROP_UINT32("enable-base", IbexPlicState, enable_base, 0x200),
  178. DEFINE_PROP_UINT32("enable-num", IbexPlicState, enable_num, 3),
  179. DEFINE_PROP_UINT32("threshold-base", IbexPlicState, threshold_base, 0x20c),
  180. DEFINE_PROP_UINT32("claim-base", IbexPlicState, claim_base, 0x210),
  181. DEFINE_PROP_END_OF_LIST(),
  182. };
  183. static void ibex_plic_init(Object *obj)
  184. {
  185. IbexPlicState *s = IBEX_PLIC(obj);
  186. memory_region_init_io(&s->mmio, obj, &ibex_plic_ops, s,
  187. TYPE_IBEX_PLIC, 0x400);
  188. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  189. }
  190. static void ibex_plic_realize(DeviceState *dev, Error **errp)
  191. {
  192. IbexPlicState *s = IBEX_PLIC(dev);
  193. int i;
  194. s->pending = g_new0(uint32_t, s->pending_num);
  195. s->claimed = g_new0(uint32_t, s->pending_num);
  196. s->source = g_new0(uint32_t, s->source_num);
  197. s->priority = g_new0(uint32_t, s->priority_num);
  198. s->enable = g_new0(uint32_t, s->enable_num);
  199. qdev_init_gpio_in(dev, ibex_plic_irq_request, s->num_sources);
  200. /*
  201. * We can't allow the supervisor to control SEIP as this would allow the
  202. * supervisor to clear a pending external interrupt which will result in
  203. * a lost interrupt in the case a PLIC is attached. The SEIP bit must be
  204. * hardware controlled when a PLIC is attached.
  205. */
  206. MachineState *ms = MACHINE(qdev_get_machine());
  207. unsigned int smp_cpus = ms->smp.cpus;
  208. for (i = 0; i < smp_cpus; i++) {
  209. RISCVCPU *cpu = RISCV_CPU(qemu_get_cpu(i));
  210. if (riscv_cpu_claim_interrupts(cpu, MIP_SEIP) < 0) {
  211. error_report("SEIP already claimed");
  212. exit(1);
  213. }
  214. }
  215. msi_nonbroken = true;
  216. }
  217. static void ibex_plic_class_init(ObjectClass *klass, void *data)
  218. {
  219. DeviceClass *dc = DEVICE_CLASS(klass);
  220. dc->reset = ibex_plic_reset;
  221. device_class_set_props(dc, ibex_plic_properties);
  222. dc->realize = ibex_plic_realize;
  223. }
  224. static const TypeInfo ibex_plic_info = {
  225. .name = TYPE_IBEX_PLIC,
  226. .parent = TYPE_SYS_BUS_DEVICE,
  227. .instance_size = sizeof(IbexPlicState),
  228. .instance_init = ibex_plic_init,
  229. .class_init = ibex_plic_class_init,
  230. };
  231. static void ibex_plic_register_types(void)
  232. {
  233. type_register_static(&ibex_plic_info);
  234. }
  235. type_init(ibex_plic_register_types)