sifive_clint.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * SiFive CLINT (Core Local Interruptor)
  3. *
  4. * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
  5. * Copyright (c) 2017 SiFive, Inc.
  6. *
  7. * This provides real-time clock, timer and interprocessor interrupts.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2 or later, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/module.h"
  24. #include "hw/sysbus.h"
  25. #include "target/riscv/cpu.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/riscv/sifive_clint.h"
  28. #include "qemu/timer.h"
  29. static uint64_t cpu_riscv_read_rtc(void)
  30. {
  31. return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
  32. SIFIVE_CLINT_TIMEBASE_FREQ, NANOSECONDS_PER_SECOND);
  33. }
  34. /*
  35. * Called when timecmp is written to update the QEMU timer or immediately
  36. * trigger timer interrupt if mtimecmp <= current timer value.
  37. */
  38. static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value)
  39. {
  40. uint64_t next;
  41. uint64_t diff;
  42. uint64_t rtc_r = cpu_riscv_read_rtc();
  43. cpu->env.timecmp = value;
  44. if (cpu->env.timecmp <= rtc_r) {
  45. /* if we're setting an MTIMECMP value in the "past",
  46. immediately raise the timer interrupt */
  47. riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(1));
  48. return;
  49. }
  50. /* otherwise, set up the future timer interrupt */
  51. riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(0));
  52. diff = cpu->env.timecmp - rtc_r;
  53. /* back to ns (note args switched in muldiv64) */
  54. next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  55. muldiv64(diff, NANOSECONDS_PER_SECOND, SIFIVE_CLINT_TIMEBASE_FREQ);
  56. timer_mod(cpu->env.timer, next);
  57. }
  58. /*
  59. * Callback used when the timer set using timer_mod expires.
  60. * Should raise the timer interrupt line
  61. */
  62. static void sifive_clint_timer_cb(void *opaque)
  63. {
  64. RISCVCPU *cpu = opaque;
  65. riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(1));
  66. }
  67. /* CPU wants to read rtc or timecmp register */
  68. static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
  69. {
  70. SiFiveCLINTState *clint = opaque;
  71. if (addr >= clint->sip_base &&
  72. addr < clint->sip_base + (clint->num_harts << 2)) {
  73. size_t hartid = (addr - clint->sip_base) >> 2;
  74. CPUState *cpu = qemu_get_cpu(hartid);
  75. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  76. if (!env) {
  77. error_report("clint: invalid timecmp hartid: %zu", hartid);
  78. } else if ((addr & 0x3) == 0) {
  79. return (env->mip & MIP_MSIP) > 0;
  80. } else {
  81. error_report("clint: invalid read: %08x", (uint32_t)addr);
  82. return 0;
  83. }
  84. } else if (addr >= clint->timecmp_base &&
  85. addr < clint->timecmp_base + (clint->num_harts << 3)) {
  86. size_t hartid = (addr - clint->timecmp_base) >> 3;
  87. CPUState *cpu = qemu_get_cpu(hartid);
  88. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  89. if (!env) {
  90. error_report("clint: invalid timecmp hartid: %zu", hartid);
  91. } else if ((addr & 0x7) == 0) {
  92. /* timecmp_lo */
  93. uint64_t timecmp = env->timecmp;
  94. return timecmp & 0xFFFFFFFF;
  95. } else if ((addr & 0x7) == 4) {
  96. /* timecmp_hi */
  97. uint64_t timecmp = env->timecmp;
  98. return (timecmp >> 32) & 0xFFFFFFFF;
  99. } else {
  100. error_report("clint: invalid read: %08x", (uint32_t)addr);
  101. return 0;
  102. }
  103. } else if (addr == clint->time_base) {
  104. /* time_lo */
  105. return cpu_riscv_read_rtc() & 0xFFFFFFFF;
  106. } else if (addr == clint->time_base + 4) {
  107. /* time_hi */
  108. return (cpu_riscv_read_rtc() >> 32) & 0xFFFFFFFF;
  109. }
  110. error_report("clint: invalid read: %08x", (uint32_t)addr);
  111. return 0;
  112. }
  113. /* CPU wrote to rtc or timecmp register */
  114. static void sifive_clint_write(void *opaque, hwaddr addr, uint64_t value,
  115. unsigned size)
  116. {
  117. SiFiveCLINTState *clint = opaque;
  118. if (addr >= clint->sip_base &&
  119. addr < clint->sip_base + (clint->num_harts << 2)) {
  120. size_t hartid = (addr - clint->sip_base) >> 2;
  121. CPUState *cpu = qemu_get_cpu(hartid);
  122. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  123. if (!env) {
  124. error_report("clint: invalid timecmp hartid: %zu", hartid);
  125. } else if ((addr & 0x3) == 0) {
  126. riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_MSIP, BOOL_TO_MASK(value));
  127. } else {
  128. error_report("clint: invalid sip write: %08x", (uint32_t)addr);
  129. }
  130. return;
  131. } else if (addr >= clint->timecmp_base &&
  132. addr < clint->timecmp_base + (clint->num_harts << 3)) {
  133. size_t hartid = (addr - clint->timecmp_base) >> 3;
  134. CPUState *cpu = qemu_get_cpu(hartid);
  135. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  136. if (!env) {
  137. error_report("clint: invalid timecmp hartid: %zu", hartid);
  138. } else if ((addr & 0x7) == 0) {
  139. /* timecmp_lo */
  140. uint64_t timecmp_hi = env->timecmp >> 32;
  141. sifive_clint_write_timecmp(RISCV_CPU(cpu),
  142. timecmp_hi << 32 | (value & 0xFFFFFFFF));
  143. return;
  144. } else if ((addr & 0x7) == 4) {
  145. /* timecmp_hi */
  146. uint64_t timecmp_lo = env->timecmp;
  147. sifive_clint_write_timecmp(RISCV_CPU(cpu),
  148. value << 32 | (timecmp_lo & 0xFFFFFFFF));
  149. } else {
  150. error_report("clint: invalid timecmp write: %08x", (uint32_t)addr);
  151. }
  152. return;
  153. } else if (addr == clint->time_base) {
  154. /* time_lo */
  155. error_report("clint: time_lo write not implemented");
  156. return;
  157. } else if (addr == clint->time_base + 4) {
  158. /* time_hi */
  159. error_report("clint: time_hi write not implemented");
  160. return;
  161. }
  162. error_report("clint: invalid write: %08x", (uint32_t)addr);
  163. }
  164. static const MemoryRegionOps sifive_clint_ops = {
  165. .read = sifive_clint_read,
  166. .write = sifive_clint_write,
  167. .endianness = DEVICE_LITTLE_ENDIAN,
  168. .valid = {
  169. .min_access_size = 4,
  170. .max_access_size = 4
  171. }
  172. };
  173. static Property sifive_clint_properties[] = {
  174. DEFINE_PROP_UINT32("num-harts", SiFiveCLINTState, num_harts, 0),
  175. DEFINE_PROP_UINT32("sip-base", SiFiveCLINTState, sip_base, 0),
  176. DEFINE_PROP_UINT32("timecmp-base", SiFiveCLINTState, timecmp_base, 0),
  177. DEFINE_PROP_UINT32("time-base", SiFiveCLINTState, time_base, 0),
  178. DEFINE_PROP_UINT32("aperture-size", SiFiveCLINTState, aperture_size, 0),
  179. DEFINE_PROP_END_OF_LIST(),
  180. };
  181. static void sifive_clint_realize(DeviceState *dev, Error **errp)
  182. {
  183. SiFiveCLINTState *s = SIFIVE_CLINT(dev);
  184. memory_region_init_io(&s->mmio, OBJECT(dev), &sifive_clint_ops, s,
  185. TYPE_SIFIVE_CLINT, s->aperture_size);
  186. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio);
  187. }
  188. static void sifive_clint_class_init(ObjectClass *klass, void *data)
  189. {
  190. DeviceClass *dc = DEVICE_CLASS(klass);
  191. dc->realize = sifive_clint_realize;
  192. dc->props = sifive_clint_properties;
  193. }
  194. static const TypeInfo sifive_clint_info = {
  195. .name = TYPE_SIFIVE_CLINT,
  196. .parent = TYPE_SYS_BUS_DEVICE,
  197. .instance_size = sizeof(SiFiveCLINTState),
  198. .class_init = sifive_clint_class_init,
  199. };
  200. static void sifive_clint_register_types(void)
  201. {
  202. type_register_static(&sifive_clint_info);
  203. }
  204. type_init(sifive_clint_register_types)
  205. /*
  206. * Create CLINT device.
  207. */
  208. DeviceState *sifive_clint_create(hwaddr addr, hwaddr size, uint32_t num_harts,
  209. uint32_t sip_base, uint32_t timecmp_base, uint32_t time_base)
  210. {
  211. int i;
  212. for (i = 0; i < num_harts; i++) {
  213. CPUState *cpu = qemu_get_cpu(i);
  214. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  215. if (!env) {
  216. continue;
  217. }
  218. env->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  219. &sifive_clint_timer_cb, cpu);
  220. env->timecmp = 0;
  221. }
  222. DeviceState *dev = qdev_create(NULL, TYPE_SIFIVE_CLINT);
  223. qdev_prop_set_uint32(dev, "num-harts", num_harts);
  224. qdev_prop_set_uint32(dev, "sip-base", sip_base);
  225. qdev_prop_set_uint32(dev, "timecmp-base", timecmp_base);
  226. qdev_prop_set_uint32(dev, "time-base", time_base);
  227. qdev_prop_set_uint32(dev, "aperture-size", size);
  228. qdev_init_nofail(dev);
  229. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
  230. return dev;
  231. }