2
0

arm_mptimer.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "sysbus.h"
  22. #include "qemu/timer.h"
  23. /* This device implements the per-cpu private timer and watchdog block
  24. * which is used in both the ARM11MPCore and Cortex-A9MP.
  25. */
  26. #define MAX_CPUS 4
  27. /* State of a single timer or watchdog block */
  28. typedef struct {
  29. uint32_t count;
  30. uint32_t load;
  31. uint32_t control;
  32. uint32_t status;
  33. int64_t tick;
  34. QEMUTimer *timer;
  35. qemu_irq irq;
  36. MemoryRegion iomem;
  37. } timerblock;
  38. typedef struct {
  39. SysBusDevice busdev;
  40. uint32_t num_cpu;
  41. timerblock timerblock[MAX_CPUS * 2];
  42. MemoryRegion iomem[2];
  43. } arm_mptimer_state;
  44. static inline int get_current_cpu(arm_mptimer_state *s)
  45. {
  46. CPUState *cpu_single_cpu = ENV_GET_CPU(cpu_single_env);
  47. if (cpu_single_cpu->cpu_index >= s->num_cpu) {
  48. hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
  49. s->num_cpu, cpu_single_cpu->cpu_index);
  50. }
  51. return cpu_single_cpu->cpu_index;
  52. }
  53. static inline void timerblock_update_irq(timerblock *tb)
  54. {
  55. qemu_set_irq(tb->irq, tb->status);
  56. }
  57. /* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
  58. static inline uint32_t timerblock_scale(timerblock *tb)
  59. {
  60. return (((tb->control >> 8) & 0xff) + 1) * 10;
  61. }
  62. static void timerblock_reload(timerblock *tb, int restart)
  63. {
  64. if (tb->count == 0) {
  65. return;
  66. }
  67. if (restart) {
  68. tb->tick = qemu_get_clock_ns(vm_clock);
  69. }
  70. tb->tick += (int64_t)tb->count * timerblock_scale(tb);
  71. qemu_mod_timer(tb->timer, tb->tick);
  72. }
  73. static void timerblock_tick(void *opaque)
  74. {
  75. timerblock *tb = (timerblock *)opaque;
  76. tb->status = 1;
  77. if (tb->control & 2) {
  78. tb->count = tb->load;
  79. timerblock_reload(tb, 0);
  80. } else {
  81. tb->count = 0;
  82. }
  83. timerblock_update_irq(tb);
  84. }
  85. static uint64_t timerblock_read(void *opaque, hwaddr addr,
  86. unsigned size)
  87. {
  88. timerblock *tb = (timerblock *)opaque;
  89. int64_t val;
  90. switch (addr) {
  91. case 0: /* Load */
  92. return tb->load;
  93. case 4: /* Counter. */
  94. if (((tb->control & 1) == 0) || (tb->count == 0)) {
  95. return 0;
  96. }
  97. /* Slow and ugly, but hopefully won't happen too often. */
  98. val = tb->tick - qemu_get_clock_ns(vm_clock);
  99. val /= timerblock_scale(tb);
  100. if (val < 0) {
  101. val = 0;
  102. }
  103. return val;
  104. case 8: /* Control. */
  105. return tb->control;
  106. case 12: /* Interrupt status. */
  107. return tb->status;
  108. default:
  109. return 0;
  110. }
  111. }
  112. static void timerblock_write(void *opaque, hwaddr addr,
  113. uint64_t value, unsigned size)
  114. {
  115. timerblock *tb = (timerblock *)opaque;
  116. int64_t old;
  117. switch (addr) {
  118. case 0: /* Load */
  119. tb->load = value;
  120. /* Fall through. */
  121. case 4: /* Counter. */
  122. if ((tb->control & 1) && tb->count) {
  123. /* Cancel the previous timer. */
  124. qemu_del_timer(tb->timer);
  125. }
  126. tb->count = value;
  127. if (tb->control & 1) {
  128. timerblock_reload(tb, 1);
  129. }
  130. break;
  131. case 8: /* Control. */
  132. old = tb->control;
  133. tb->control = value;
  134. if (((old & 1) == 0) && (value & 1)) {
  135. if (tb->count == 0 && (tb->control & 2)) {
  136. tb->count = tb->load;
  137. }
  138. timerblock_reload(tb, 1);
  139. }
  140. break;
  141. case 12: /* Interrupt status. */
  142. tb->status &= ~value;
  143. timerblock_update_irq(tb);
  144. break;
  145. }
  146. }
  147. /* Wrapper functions to implement the "read timer/watchdog for
  148. * the current CPU" memory regions.
  149. */
  150. static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
  151. unsigned size)
  152. {
  153. arm_mptimer_state *s = (arm_mptimer_state *)opaque;
  154. int id = get_current_cpu(s);
  155. return timerblock_read(&s->timerblock[id * 2], addr, size);
  156. }
  157. static void arm_thistimer_write(void *opaque, hwaddr addr,
  158. uint64_t value, unsigned size)
  159. {
  160. arm_mptimer_state *s = (arm_mptimer_state *)opaque;
  161. int id = get_current_cpu(s);
  162. timerblock_write(&s->timerblock[id * 2], addr, value, size);
  163. }
  164. static uint64_t arm_thiswdog_read(void *opaque, hwaddr addr,
  165. unsigned size)
  166. {
  167. arm_mptimer_state *s = (arm_mptimer_state *)opaque;
  168. int id = get_current_cpu(s);
  169. return timerblock_read(&s->timerblock[id * 2 + 1], addr, size);
  170. }
  171. static void arm_thiswdog_write(void *opaque, hwaddr addr,
  172. uint64_t value, unsigned size)
  173. {
  174. arm_mptimer_state *s = (arm_mptimer_state *)opaque;
  175. int id = get_current_cpu(s);
  176. timerblock_write(&s->timerblock[id * 2 + 1], addr, value, size);
  177. }
  178. static const MemoryRegionOps arm_thistimer_ops = {
  179. .read = arm_thistimer_read,
  180. .write = arm_thistimer_write,
  181. .valid = {
  182. .min_access_size = 4,
  183. .max_access_size = 4,
  184. },
  185. .endianness = DEVICE_NATIVE_ENDIAN,
  186. };
  187. static const MemoryRegionOps arm_thiswdog_ops = {
  188. .read = arm_thiswdog_read,
  189. .write = arm_thiswdog_write,
  190. .valid = {
  191. .min_access_size = 4,
  192. .max_access_size = 4,
  193. },
  194. .endianness = DEVICE_NATIVE_ENDIAN,
  195. };
  196. static const MemoryRegionOps timerblock_ops = {
  197. .read = timerblock_read,
  198. .write = timerblock_write,
  199. .valid = {
  200. .min_access_size = 4,
  201. .max_access_size = 4,
  202. },
  203. .endianness = DEVICE_NATIVE_ENDIAN,
  204. };
  205. static void timerblock_reset(timerblock *tb)
  206. {
  207. tb->count = 0;
  208. tb->load = 0;
  209. tb->control = 0;
  210. tb->status = 0;
  211. tb->tick = 0;
  212. if (tb->timer) {
  213. qemu_del_timer(tb->timer);
  214. }
  215. }
  216. static void arm_mptimer_reset(DeviceState *dev)
  217. {
  218. arm_mptimer_state *s =
  219. FROM_SYSBUS(arm_mptimer_state, SYS_BUS_DEVICE(dev));
  220. int i;
  221. /* We reset every timer in the array, not just the ones we're using,
  222. * because vmsave will look at every array element.
  223. */
  224. for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
  225. timerblock_reset(&s->timerblock[i]);
  226. }
  227. }
  228. static int arm_mptimer_init(SysBusDevice *dev)
  229. {
  230. arm_mptimer_state *s = FROM_SYSBUS(arm_mptimer_state, dev);
  231. int i;
  232. if (s->num_cpu < 1 || s->num_cpu > MAX_CPUS) {
  233. hw_error("%s: num-cpu must be between 1 and %d\n", __func__, MAX_CPUS);
  234. }
  235. /* We implement one timer and one watchdog block per CPU, and
  236. * expose multiple MMIO regions:
  237. * * region 0 is "timer for this core"
  238. * * region 1 is "watchdog for this core"
  239. * * region 2 is "timer for core 0"
  240. * * region 3 is "watchdog for core 0"
  241. * * region 4 is "timer for core 1"
  242. * * region 5 is "watchdog for core 1"
  243. * and so on.
  244. * The outgoing interrupt lines are
  245. * * timer for core 0
  246. * * watchdog for core 0
  247. * * timer for core 1
  248. * * watchdog for core 1
  249. * and so on.
  250. */
  251. memory_region_init_io(&s->iomem[0], &arm_thistimer_ops, s,
  252. "arm_mptimer_timer", 0x20);
  253. sysbus_init_mmio(dev, &s->iomem[0]);
  254. memory_region_init_io(&s->iomem[1], &arm_thiswdog_ops, s,
  255. "arm_mptimer_wdog", 0x20);
  256. sysbus_init_mmio(dev, &s->iomem[1]);
  257. for (i = 0; i < (s->num_cpu * 2); i++) {
  258. timerblock *tb = &s->timerblock[i];
  259. tb->timer = qemu_new_timer_ns(vm_clock, timerblock_tick, tb);
  260. sysbus_init_irq(dev, &tb->irq);
  261. memory_region_init_io(&tb->iomem, &timerblock_ops, tb,
  262. "arm_mptimer_timerblock", 0x20);
  263. sysbus_init_mmio(dev, &tb->iomem);
  264. }
  265. return 0;
  266. }
  267. static const VMStateDescription vmstate_timerblock = {
  268. .name = "arm_mptimer_timerblock",
  269. .version_id = 1,
  270. .minimum_version_id = 1,
  271. .fields = (VMStateField[]) {
  272. VMSTATE_UINT32(count, timerblock),
  273. VMSTATE_UINT32(load, timerblock),
  274. VMSTATE_UINT32(control, timerblock),
  275. VMSTATE_UINT32(status, timerblock),
  276. VMSTATE_INT64(tick, timerblock),
  277. VMSTATE_END_OF_LIST()
  278. }
  279. };
  280. static const VMStateDescription vmstate_arm_mptimer = {
  281. .name = "arm_mptimer",
  282. .version_id = 1,
  283. .minimum_version_id = 1,
  284. .fields = (VMStateField[]) {
  285. VMSTATE_STRUCT_ARRAY(timerblock, arm_mptimer_state, (MAX_CPUS * 2),
  286. 1, vmstate_timerblock, timerblock),
  287. VMSTATE_END_OF_LIST()
  288. }
  289. };
  290. static Property arm_mptimer_properties[] = {
  291. DEFINE_PROP_UINT32("num-cpu", arm_mptimer_state, num_cpu, 0),
  292. DEFINE_PROP_END_OF_LIST()
  293. };
  294. static void arm_mptimer_class_init(ObjectClass *klass, void *data)
  295. {
  296. DeviceClass *dc = DEVICE_CLASS(klass);
  297. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
  298. sbc->init = arm_mptimer_init;
  299. dc->vmsd = &vmstate_arm_mptimer;
  300. dc->reset = arm_mptimer_reset;
  301. dc->no_user = 1;
  302. dc->props = arm_mptimer_properties;
  303. }
  304. static const TypeInfo arm_mptimer_info = {
  305. .name = "arm_mptimer",
  306. .parent = TYPE_SYS_BUS_DEVICE,
  307. .instance_size = sizeof(arm_mptimer_state),
  308. .class_init = arm_mptimer_class_init,
  309. };
  310. static void arm_mptimer_register_types(void)
  311. {
  312. type_register_static(&arm_mptimer_info);
  313. }
  314. type_init(arm_mptimer_register_types)