sifive_clint.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "qapi/error.h"
  23. #include "qemu/error-report.h"
  24. #include "qemu/module.h"
  25. #include "hw/sysbus.h"
  26. #include "target/riscv/cpu.h"
  27. #include "hw/qdev-properties.h"
  28. #include "hw/intc/sifive_clint.h"
  29. #include "qemu/timer.h"
  30. static uint64_t cpu_riscv_read_rtc(uint32_t timebase_freq)
  31. {
  32. return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
  33. timebase_freq, NANOSECONDS_PER_SECOND);
  34. }
  35. /*
  36. * Called when timecmp is written to update the QEMU timer or immediately
  37. * trigger timer interrupt if mtimecmp <= current timer value.
  38. */
  39. static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value,
  40. uint32_t timebase_freq)
  41. {
  42. uint64_t next;
  43. uint64_t diff;
  44. uint64_t rtc_r = cpu_riscv_read_rtc(timebase_freq);
  45. cpu->env.timecmp = value;
  46. if (cpu->env.timecmp <= rtc_r) {
  47. /* if we're setting an MTIMECMP value in the "past",
  48. immediately raise the timer interrupt */
  49. riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(1));
  50. return;
  51. }
  52. /* otherwise, set up the future timer interrupt */
  53. riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(0));
  54. diff = cpu->env.timecmp - rtc_r;
  55. /* back to ns (note args switched in muldiv64) */
  56. next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  57. muldiv64(diff, NANOSECONDS_PER_SECOND, timebase_freq);
  58. timer_mod(cpu->env.timer, next);
  59. }
  60. /*
  61. * Callback used when the timer set using timer_mod expires.
  62. * Should raise the timer interrupt line
  63. */
  64. static void sifive_clint_timer_cb(void *opaque)
  65. {
  66. RISCVCPU *cpu = opaque;
  67. riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(1));
  68. }
  69. /* CPU wants to read rtc or timecmp register */
  70. static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
  71. {
  72. SiFiveCLINTState *clint = opaque;
  73. if (addr >= clint->sip_base &&
  74. addr < clint->sip_base + (clint->num_harts << 2)) {
  75. size_t hartid = clint->hartid_base + ((addr - clint->sip_base) >> 2);
  76. CPUState *cpu = qemu_get_cpu(hartid);
  77. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  78. if (!env) {
  79. error_report("clint: invalid timecmp hartid: %zu", hartid);
  80. } else if ((addr & 0x3) == 0) {
  81. return (env->mip & MIP_MSIP) > 0;
  82. } else {
  83. error_report("clint: invalid read: %08x", (uint32_t)addr);
  84. return 0;
  85. }
  86. } else if (addr >= clint->timecmp_base &&
  87. addr < clint->timecmp_base + (clint->num_harts << 3)) {
  88. size_t hartid = clint->hartid_base +
  89. ((addr - clint->timecmp_base) >> 3);
  90. CPUState *cpu = qemu_get_cpu(hartid);
  91. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  92. if (!env) {
  93. error_report("clint: invalid timecmp hartid: %zu", hartid);
  94. } else if ((addr & 0x7) == 0) {
  95. /* timecmp_lo */
  96. uint64_t timecmp = env->timecmp;
  97. return timecmp & 0xFFFFFFFF;
  98. } else if ((addr & 0x7) == 4) {
  99. /* timecmp_hi */
  100. uint64_t timecmp = env->timecmp;
  101. return (timecmp >> 32) & 0xFFFFFFFF;
  102. } else {
  103. error_report("clint: invalid read: %08x", (uint32_t)addr);
  104. return 0;
  105. }
  106. } else if (addr == clint->time_base) {
  107. /* time_lo */
  108. return cpu_riscv_read_rtc(clint->timebase_freq) & 0xFFFFFFFF;
  109. } else if (addr == clint->time_base + 4) {
  110. /* time_hi */
  111. return (cpu_riscv_read_rtc(clint->timebase_freq) >> 32) & 0xFFFFFFFF;
  112. }
  113. error_report("clint: invalid read: %08x", (uint32_t)addr);
  114. return 0;
  115. }
  116. /* CPU wrote to rtc or timecmp register */
  117. static void sifive_clint_write(void *opaque, hwaddr addr, uint64_t value,
  118. unsigned size)
  119. {
  120. SiFiveCLINTState *clint = opaque;
  121. if (addr >= clint->sip_base &&
  122. addr < clint->sip_base + (clint->num_harts << 2)) {
  123. size_t hartid = clint->hartid_base + ((addr - clint->sip_base) >> 2);
  124. CPUState *cpu = qemu_get_cpu(hartid);
  125. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  126. if (!env) {
  127. error_report("clint: invalid timecmp hartid: %zu", hartid);
  128. } else if ((addr & 0x3) == 0) {
  129. riscv_cpu_update_mip(RISCV_CPU(cpu), MIP_MSIP, BOOL_TO_MASK(value));
  130. } else {
  131. error_report("clint: invalid sip write: %08x", (uint32_t)addr);
  132. }
  133. return;
  134. } else if (addr >= clint->timecmp_base &&
  135. addr < clint->timecmp_base + (clint->num_harts << 3)) {
  136. size_t hartid = clint->hartid_base +
  137. ((addr - clint->timecmp_base) >> 3);
  138. CPUState *cpu = qemu_get_cpu(hartid);
  139. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  140. if (!env) {
  141. error_report("clint: invalid timecmp hartid: %zu", hartid);
  142. } else if ((addr & 0x7) == 0) {
  143. /* timecmp_lo */
  144. uint64_t timecmp_hi = env->timecmp >> 32;
  145. sifive_clint_write_timecmp(RISCV_CPU(cpu),
  146. timecmp_hi << 32 | (value & 0xFFFFFFFF), clint->timebase_freq);
  147. return;
  148. } else if ((addr & 0x7) == 4) {
  149. /* timecmp_hi */
  150. uint64_t timecmp_lo = env->timecmp;
  151. sifive_clint_write_timecmp(RISCV_CPU(cpu),
  152. value << 32 | (timecmp_lo & 0xFFFFFFFF), clint->timebase_freq);
  153. } else {
  154. error_report("clint: invalid timecmp write: %08x", (uint32_t)addr);
  155. }
  156. return;
  157. } else if (addr == clint->time_base) {
  158. /* time_lo */
  159. error_report("clint: time_lo write not implemented");
  160. return;
  161. } else if (addr == clint->time_base + 4) {
  162. /* time_hi */
  163. error_report("clint: time_hi write not implemented");
  164. return;
  165. }
  166. error_report("clint: invalid write: %08x", (uint32_t)addr);
  167. }
  168. static const MemoryRegionOps sifive_clint_ops = {
  169. .read = sifive_clint_read,
  170. .write = sifive_clint_write,
  171. .endianness = DEVICE_LITTLE_ENDIAN,
  172. .valid = {
  173. .min_access_size = 4,
  174. .max_access_size = 8
  175. }
  176. };
  177. static Property sifive_clint_properties[] = {
  178. DEFINE_PROP_UINT32("hartid-base", SiFiveCLINTState, hartid_base, 0),
  179. DEFINE_PROP_UINT32("num-harts", SiFiveCLINTState, num_harts, 0),
  180. DEFINE_PROP_UINT32("sip-base", SiFiveCLINTState, sip_base, 0),
  181. DEFINE_PROP_UINT32("timecmp-base", SiFiveCLINTState, timecmp_base, 0),
  182. DEFINE_PROP_UINT32("time-base", SiFiveCLINTState, time_base, 0),
  183. DEFINE_PROP_UINT32("aperture-size", SiFiveCLINTState, aperture_size, 0),
  184. DEFINE_PROP_UINT32("timebase-freq", SiFiveCLINTState, timebase_freq, 0),
  185. DEFINE_PROP_END_OF_LIST(),
  186. };
  187. static void sifive_clint_realize(DeviceState *dev, Error **errp)
  188. {
  189. SiFiveCLINTState *s = SIFIVE_CLINT(dev);
  190. memory_region_init_io(&s->mmio, OBJECT(dev), &sifive_clint_ops, s,
  191. TYPE_SIFIVE_CLINT, s->aperture_size);
  192. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio);
  193. }
  194. static void sifive_clint_class_init(ObjectClass *klass, void *data)
  195. {
  196. DeviceClass *dc = DEVICE_CLASS(klass);
  197. dc->realize = sifive_clint_realize;
  198. device_class_set_props(dc, sifive_clint_properties);
  199. }
  200. static const TypeInfo sifive_clint_info = {
  201. .name = TYPE_SIFIVE_CLINT,
  202. .parent = TYPE_SYS_BUS_DEVICE,
  203. .instance_size = sizeof(SiFiveCLINTState),
  204. .class_init = sifive_clint_class_init,
  205. };
  206. static void sifive_clint_register_types(void)
  207. {
  208. type_register_static(&sifive_clint_info);
  209. }
  210. type_init(sifive_clint_register_types)
  211. /*
  212. * Create CLINT device.
  213. */
  214. DeviceState *sifive_clint_create(hwaddr addr, hwaddr size,
  215. uint32_t hartid_base, uint32_t num_harts, uint32_t sip_base,
  216. uint32_t timecmp_base, uint32_t time_base, uint32_t timebase_freq,
  217. bool provide_rdtime)
  218. {
  219. int i;
  220. for (i = 0; i < num_harts; i++) {
  221. CPUState *cpu = qemu_get_cpu(hartid_base + i);
  222. CPURISCVState *env = cpu ? cpu->env_ptr : NULL;
  223. if (!env) {
  224. continue;
  225. }
  226. if (provide_rdtime) {
  227. riscv_cpu_set_rdtime_fn(env, cpu_riscv_read_rtc, timebase_freq);
  228. }
  229. env->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  230. &sifive_clint_timer_cb, cpu);
  231. env->timecmp = 0;
  232. }
  233. DeviceState *dev = qdev_new(TYPE_SIFIVE_CLINT);
  234. qdev_prop_set_uint32(dev, "hartid-base", hartid_base);
  235. qdev_prop_set_uint32(dev, "num-harts", num_harts);
  236. qdev_prop_set_uint32(dev, "sip-base", sip_base);
  237. qdev_prop_set_uint32(dev, "timecmp-base", timecmp_base);
  238. qdev_prop_set_uint32(dev, "time-base", time_base);
  239. qdev_prop_set_uint32(dev, "aperture-size", size);
  240. qdev_prop_set_uint32(dev, "timebase-freq", timebase_freq);
  241. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  242. sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr);
  243. return dev;
  244. }