ibex_plic.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. s->pending[pending_num] |= level << (irq % 32);
  41. }
  42. static bool ibex_plic_irqs_pending(IbexPlicState *s, uint32_t context)
  43. {
  44. int i;
  45. for (i = 0; i < s->pending_num; i++) {
  46. uint32_t irq_num = ctz64(s->pending[i]) + (i * 32);
  47. if (!(s->pending[i] & s->enable[i])) {
  48. /* No pending and enabled IRQ */
  49. continue;
  50. }
  51. if (s->priority[irq_num] > s->threshold) {
  52. if (!s->claim) {
  53. s->claim = irq_num;
  54. }
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. static void ibex_plic_update(IbexPlicState *s)
  61. {
  62. CPUState *cpu;
  63. int level, i;
  64. for (i = 0; i < s->num_cpus; i++) {
  65. cpu = qemu_get_cpu(i);
  66. if (!cpu) {
  67. continue;
  68. }
  69. level = ibex_plic_irqs_pending(s, 0);
  70. riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_MEIP, BOOL_TO_MASK(level));
  71. }
  72. }
  73. static void ibex_plic_reset(DeviceState *dev)
  74. {
  75. IbexPlicState *s = IBEX_PLIC(dev);
  76. s->threshold = 0x00000000;
  77. s->claim = 0x00000000;
  78. }
  79. static uint64_t ibex_plic_read(void *opaque, hwaddr addr,
  80. unsigned int size)
  81. {
  82. IbexPlicState *s = opaque;
  83. int offset;
  84. uint32_t ret = 0;
  85. if (addr_between(addr, s->pending_base, s->pending_num)) {
  86. offset = (addr - s->pending_base) / 4;
  87. ret = s->pending[offset];
  88. } else if (addr_between(addr, s->source_base, s->source_num)) {
  89. qemu_log_mask(LOG_UNIMP,
  90. "%s: Interrupt source mode not supported\n", __func__);
  91. } else if (addr_between(addr, s->priority_base, s->priority_num)) {
  92. offset = (addr - s->priority_base) / 4;
  93. ret = s->priority[offset];
  94. } else if (addr_between(addr, s->enable_base, s->enable_num)) {
  95. offset = (addr - s->enable_base) / 4;
  96. ret = s->enable[offset];
  97. } else if (addr_between(addr, s->threshold_base, 1)) {
  98. ret = s->threshold;
  99. } else if (addr_between(addr, s->claim_base, 1)) {
  100. int pending_num = s->claim / 32;
  101. s->pending[pending_num] &= ~(1 << (s->claim % 32));
  102. ret = s->claim;
  103. }
  104. return ret;
  105. }
  106. static void ibex_plic_write(void *opaque, hwaddr addr,
  107. uint64_t value, unsigned int size)
  108. {
  109. IbexPlicState *s = opaque;
  110. if (addr_between(addr, s->pending_base, s->pending_num)) {
  111. qemu_log_mask(LOG_GUEST_ERROR,
  112. "%s: Pending registers are read only\n", __func__);
  113. } else if (addr_between(addr, s->source_base, s->source_num)) {
  114. qemu_log_mask(LOG_UNIMP,
  115. "%s: Interrupt source mode not supported\n", __func__);
  116. } else if (addr_between(addr, s->priority_base, s->priority_num)) {
  117. uint32_t irq = ((addr - s->priority_base) >> 2) + 1;
  118. s->priority[irq] = value & 7;
  119. } else if (addr_between(addr, s->enable_base, s->enable_num)) {
  120. uint32_t enable_reg = (addr - s->enable_base) / 4;
  121. s->enable[enable_reg] = value;
  122. } else if (addr_between(addr, s->threshold_base, 1)) {
  123. s->threshold = value & 3;
  124. } else if (addr_between(addr, s->claim_base, 1)) {
  125. if (s->claim == value) {
  126. /* Interrupt was completed */
  127. s->claim = 0;
  128. }
  129. }
  130. ibex_plic_update(s);
  131. }
  132. static const MemoryRegionOps ibex_plic_ops = {
  133. .read = ibex_plic_read,
  134. .write = ibex_plic_write,
  135. .endianness = DEVICE_NATIVE_ENDIAN,
  136. .valid = {
  137. .min_access_size = 4,
  138. .max_access_size = 4
  139. }
  140. };
  141. static void ibex_plic_irq_request(void *opaque, int irq, int level)
  142. {
  143. IbexPlicState *s = opaque;
  144. ibex_plic_irqs_set_pending(s, irq, level > 0);
  145. ibex_plic_update(s);
  146. }
  147. static Property ibex_plic_properties[] = {
  148. DEFINE_PROP_UINT32("num-cpus", IbexPlicState, num_cpus, 1),
  149. DEFINE_PROP_UINT32("num-sources", IbexPlicState, num_sources, 80),
  150. DEFINE_PROP_UINT32("pending-base", IbexPlicState, pending_base, 0),
  151. DEFINE_PROP_UINT32("pending-num", IbexPlicState, pending_num, 3),
  152. DEFINE_PROP_UINT32("source-base", IbexPlicState, source_base, 0x0c),
  153. DEFINE_PROP_UINT32("source-num", IbexPlicState, source_num, 3),
  154. DEFINE_PROP_UINT32("priority-base", IbexPlicState, priority_base, 0x18),
  155. DEFINE_PROP_UINT32("priority-num", IbexPlicState, priority_num, 80),
  156. DEFINE_PROP_UINT32("enable-base", IbexPlicState, enable_base, 0x200),
  157. DEFINE_PROP_UINT32("enable-num", IbexPlicState, enable_num, 3),
  158. DEFINE_PROP_UINT32("threshold-base", IbexPlicState, threshold_base, 0x20c),
  159. DEFINE_PROP_UINT32("claim-base", IbexPlicState, claim_base, 0x210),
  160. DEFINE_PROP_END_OF_LIST(),
  161. };
  162. static void ibex_plic_init(Object *obj)
  163. {
  164. IbexPlicState *s = IBEX_PLIC(obj);
  165. memory_region_init_io(&s->mmio, obj, &ibex_plic_ops, s,
  166. TYPE_IBEX_PLIC, 0x400);
  167. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  168. }
  169. static void ibex_plic_realize(DeviceState *dev, Error **errp)
  170. {
  171. IbexPlicState *s = IBEX_PLIC(dev);
  172. int i;
  173. s->pending = g_new0(uint32_t, s->pending_num);
  174. s->source = g_new0(uint32_t, s->source_num);
  175. s->priority = g_new0(uint32_t, s->priority_num);
  176. s->enable = g_new0(uint32_t, s->enable_num);
  177. qdev_init_gpio_in(dev, ibex_plic_irq_request, s->num_sources);
  178. /*
  179. * We can't allow the supervisor to control SEIP as this would allow the
  180. * supervisor to clear a pending external interrupt which will result in
  181. * a lost interrupt in the case a PLIC is attached. The SEIP bit must be
  182. * hardware controlled when a PLIC is attached.
  183. */
  184. MachineState *ms = MACHINE(qdev_get_machine());
  185. unsigned int smp_cpus = ms->smp.cpus;
  186. for (i = 0; i < smp_cpus; i++) {
  187. RISCVCPU *cpu = RISCV_CPU(qemu_get_cpu(i));
  188. if (riscv_cpu_claim_interrupts(cpu, MIP_SEIP) < 0) {
  189. error_report("SEIP already claimed");
  190. exit(1);
  191. }
  192. }
  193. msi_nonbroken = true;
  194. }
  195. static void ibex_plic_class_init(ObjectClass *klass, void *data)
  196. {
  197. DeviceClass *dc = DEVICE_CLASS(klass);
  198. dc->reset = ibex_plic_reset;
  199. device_class_set_props(dc, ibex_plic_properties);
  200. dc->realize = ibex_plic_realize;
  201. }
  202. static const TypeInfo ibex_plic_info = {
  203. .name = TYPE_IBEX_PLIC,
  204. .parent = TYPE_SYS_BUS_DEVICE,
  205. .instance_size = sizeof(IbexPlicState),
  206. .instance_init = ibex_plic_init,
  207. .class_init = ibex_plic_class_init,
  208. };
  209. static void ibex_plic_register_types(void)
  210. {
  211. type_register_static(&ibex_plic_info);
  212. }
  213. type_init(ibex_plic_register_types)