arm_mptimer.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Private peripheral timer/watchdog blocks for ARM 11MPCore and A9MP
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Copyright (c) 2011 Linaro Limited
  6. * Written by Paul Brook, Peter Maydell
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "hw/timer/arm_mptimer.h"
  22. #include "qemu/timer.h"
  23. #include "qom/cpu.h"
  24. /* This device implements the per-cpu private timer and watchdog block
  25. * which is used in both the ARM11MPCore and Cortex-A9MP.
  26. */
  27. static inline int get_current_cpu(ARMMPTimerState *s)
  28. {
  29. if (current_cpu->cpu_index >= s->num_cpu) {
  30. hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
  31. s->num_cpu, current_cpu->cpu_index);
  32. }
  33. return current_cpu->cpu_index;
  34. }
  35. static inline void timerblock_update_irq(TimerBlock *tb)
  36. {
  37. qemu_set_irq(tb->irq, tb->status);
  38. }
  39. /* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
  40. static inline uint32_t timerblock_scale(TimerBlock *tb)
  41. {
  42. return (((tb->control >> 8) & 0xff) + 1) * 10;
  43. }
  44. static void timerblock_reload(TimerBlock *tb, int restart)
  45. {
  46. if (tb->count == 0) {
  47. return;
  48. }
  49. if (restart) {
  50. tb->tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  51. }
  52. tb->tick += (int64_t)tb->count * timerblock_scale(tb);
  53. timer_mod(tb->timer, tb->tick);
  54. }
  55. static void timerblock_tick(void *opaque)
  56. {
  57. TimerBlock *tb = (TimerBlock *)opaque;
  58. tb->status = 1;
  59. if (tb->control & 2) {
  60. tb->count = tb->load;
  61. timerblock_reload(tb, 0);
  62. } else {
  63. tb->count = 0;
  64. }
  65. timerblock_update_irq(tb);
  66. }
  67. static uint64_t timerblock_read(void *opaque, hwaddr addr,
  68. unsigned size)
  69. {
  70. TimerBlock *tb = (TimerBlock *)opaque;
  71. int64_t val;
  72. switch (addr) {
  73. case 0: /* Load */
  74. return tb->load;
  75. case 4: /* Counter. */
  76. if (((tb->control & 1) == 0) || (tb->count == 0)) {
  77. return 0;
  78. }
  79. /* Slow and ugly, but hopefully won't happen too often. */
  80. val = tb->tick - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  81. val /= timerblock_scale(tb);
  82. if (val < 0) {
  83. val = 0;
  84. }
  85. return val;
  86. case 8: /* Control. */
  87. return tb->control;
  88. case 12: /* Interrupt status. */
  89. return tb->status;
  90. default:
  91. return 0;
  92. }
  93. }
  94. static void timerblock_write(void *opaque, hwaddr addr,
  95. uint64_t value, unsigned size)
  96. {
  97. TimerBlock *tb = (TimerBlock *)opaque;
  98. int64_t old;
  99. switch (addr) {
  100. case 0: /* Load */
  101. tb->load = value;
  102. /* Fall through. */
  103. case 4: /* Counter. */
  104. if ((tb->control & 1) && tb->count) {
  105. /* Cancel the previous timer. */
  106. timer_del(tb->timer);
  107. }
  108. tb->count = value;
  109. if (tb->control & 1) {
  110. timerblock_reload(tb, 1);
  111. }
  112. break;
  113. case 8: /* Control. */
  114. old = tb->control;
  115. tb->control = value;
  116. if (((old & 1) == 0) && (value & 1)) {
  117. if (tb->count == 0 && (tb->control & 2)) {
  118. tb->count = tb->load;
  119. }
  120. timerblock_reload(tb, 1);
  121. }
  122. break;
  123. case 12: /* Interrupt status. */
  124. tb->status &= ~value;
  125. timerblock_update_irq(tb);
  126. break;
  127. }
  128. }
  129. /* Wrapper functions to implement the "read timer/watchdog for
  130. * the current CPU" memory regions.
  131. */
  132. static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
  133. unsigned size)
  134. {
  135. ARMMPTimerState *s = (ARMMPTimerState *)opaque;
  136. int id = get_current_cpu(s);
  137. return timerblock_read(&s->timerblock[id], addr, size);
  138. }
  139. static void arm_thistimer_write(void *opaque, hwaddr addr,
  140. uint64_t value, unsigned size)
  141. {
  142. ARMMPTimerState *s = (ARMMPTimerState *)opaque;
  143. int id = get_current_cpu(s);
  144. timerblock_write(&s->timerblock[id], addr, value, size);
  145. }
  146. static const MemoryRegionOps arm_thistimer_ops = {
  147. .read = arm_thistimer_read,
  148. .write = arm_thistimer_write,
  149. .valid = {
  150. .min_access_size = 4,
  151. .max_access_size = 4,
  152. },
  153. .endianness = DEVICE_NATIVE_ENDIAN,
  154. };
  155. static const MemoryRegionOps timerblock_ops = {
  156. .read = timerblock_read,
  157. .write = timerblock_write,
  158. .valid = {
  159. .min_access_size = 4,
  160. .max_access_size = 4,
  161. },
  162. .endianness = DEVICE_NATIVE_ENDIAN,
  163. };
  164. static void timerblock_reset(TimerBlock *tb)
  165. {
  166. tb->count = 0;
  167. tb->load = 0;
  168. tb->control = 0;
  169. tb->status = 0;
  170. tb->tick = 0;
  171. if (tb->timer) {
  172. timer_del(tb->timer);
  173. }
  174. }
  175. static void arm_mptimer_reset(DeviceState *dev)
  176. {
  177. ARMMPTimerState *s = ARM_MPTIMER(dev);
  178. int i;
  179. for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
  180. timerblock_reset(&s->timerblock[i]);
  181. }
  182. }
  183. static void arm_mptimer_init(Object *obj)
  184. {
  185. ARMMPTimerState *s = ARM_MPTIMER(obj);
  186. memory_region_init_io(&s->iomem, obj, &arm_thistimer_ops, s,
  187. "arm_mptimer_timer", 0x20);
  188. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
  189. }
  190. static void arm_mptimer_realize(DeviceState *dev, Error **errp)
  191. {
  192. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  193. ARMMPTimerState *s = ARM_MPTIMER(dev);
  194. int i;
  195. if (s->num_cpu < 1 || s->num_cpu > ARM_MPTIMER_MAX_CPUS) {
  196. hw_error("%s: num-cpu must be between 1 and %d\n",
  197. __func__, ARM_MPTIMER_MAX_CPUS);
  198. }
  199. /* We implement one timer block per CPU, and expose multiple MMIO regions:
  200. * * region 0 is "timer for this core"
  201. * * region 1 is "timer for core 0"
  202. * * region 2 is "timer for core 1"
  203. * and so on.
  204. * The outgoing interrupt lines are
  205. * * timer for core 0
  206. * * timer for core 1
  207. * and so on.
  208. */
  209. for (i = 0; i < s->num_cpu; i++) {
  210. TimerBlock *tb = &s->timerblock[i];
  211. tb->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, timerblock_tick, tb);
  212. sysbus_init_irq(sbd, &tb->irq);
  213. memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb,
  214. "arm_mptimer_timerblock", 0x20);
  215. sysbus_init_mmio(sbd, &tb->iomem);
  216. }
  217. }
  218. static const VMStateDescription vmstate_timerblock = {
  219. .name = "arm_mptimer_timerblock",
  220. .version_id = 2,
  221. .minimum_version_id = 2,
  222. .fields = (VMStateField[]) {
  223. VMSTATE_UINT32(count, TimerBlock),
  224. VMSTATE_UINT32(load, TimerBlock),
  225. VMSTATE_UINT32(control, TimerBlock),
  226. VMSTATE_UINT32(status, TimerBlock),
  227. VMSTATE_INT64(tick, TimerBlock),
  228. VMSTATE_TIMER(timer, TimerBlock),
  229. VMSTATE_END_OF_LIST()
  230. }
  231. };
  232. static const VMStateDescription vmstate_arm_mptimer = {
  233. .name = "arm_mptimer",
  234. .version_id = 2,
  235. .minimum_version_id = 2,
  236. .fields = (VMStateField[]) {
  237. VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
  238. 2, vmstate_timerblock, TimerBlock),
  239. VMSTATE_END_OF_LIST()
  240. }
  241. };
  242. static Property arm_mptimer_properties[] = {
  243. DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
  244. DEFINE_PROP_END_OF_LIST()
  245. };
  246. static void arm_mptimer_class_init(ObjectClass *klass, void *data)
  247. {
  248. DeviceClass *dc = DEVICE_CLASS(klass);
  249. dc->realize = arm_mptimer_realize;
  250. dc->vmsd = &vmstate_arm_mptimer;
  251. dc->reset = arm_mptimer_reset;
  252. dc->props = arm_mptimer_properties;
  253. }
  254. static const TypeInfo arm_mptimer_info = {
  255. .name = TYPE_ARM_MPTIMER,
  256. .parent = TYPE_SYS_BUS_DEVICE,
  257. .instance_size = sizeof(ARMMPTimerState),
  258. .instance_init = arm_mptimer_init,
  259. .class_init = arm_mptimer_class_init,
  260. };
  261. static void arm_mptimer_register_types(void)
  262. {
  263. type_register_static(&arm_mptimer_info);
  264. }
  265. type_init(arm_mptimer_register_types)