a9gtimer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Global peripheral timer block for ARM A9MP
  3. *
  4. * (C) 2013 Xilinx Inc.
  5. *
  6. * Written by François LEGAL
  7. * Written by Peter Crosthwaite <peter.crosthwaite@xilinx.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "hw/hw.h"
  24. #include "hw/irq.h"
  25. #include "hw/qdev-properties.h"
  26. #include "hw/timer/a9gtimer.h"
  27. #include "migration/vmstate.h"
  28. #include "qapi/error.h"
  29. #include "qemu/timer.h"
  30. #include "qemu/bitops.h"
  31. #include "qemu/log.h"
  32. #include "qemu/module.h"
  33. #include "hw/core/cpu.h"
  34. #ifndef A9_GTIMER_ERR_DEBUG
  35. #define A9_GTIMER_ERR_DEBUG 0
  36. #endif
  37. #define DB_PRINT_L(level, ...) do { \
  38. if (A9_GTIMER_ERR_DEBUG > (level)) { \
  39. fprintf(stderr, ": %s: ", __func__); \
  40. fprintf(stderr, ## __VA_ARGS__); \
  41. } \
  42. } while (0)
  43. #define DB_PRINT(...) DB_PRINT_L(0, ## __VA_ARGS__)
  44. static inline int a9_gtimer_get_current_cpu(A9GTimerState *s)
  45. {
  46. if (current_cpu->cpu_index >= s->num_cpu) {
  47. hw_error("a9gtimer: num-cpu %d but this cpu is %d!\n",
  48. s->num_cpu, current_cpu->cpu_index);
  49. }
  50. return current_cpu->cpu_index;
  51. }
  52. static inline uint64_t a9_gtimer_get_conv(A9GTimerState *s)
  53. {
  54. uint64_t prescale = extract32(s->control, R_CONTROL_PRESCALER_SHIFT,
  55. R_CONTROL_PRESCALER_LEN);
  56. return (prescale + 1) * 10;
  57. }
  58. static A9GTimerUpdate a9_gtimer_get_update(A9GTimerState *s)
  59. {
  60. A9GTimerUpdate ret;
  61. ret.now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  62. ret.new = s->ref_counter +
  63. (ret.now - s->cpu_ref_time) / a9_gtimer_get_conv(s);
  64. return ret;
  65. }
  66. static void a9_gtimer_update(A9GTimerState *s, bool sync)
  67. {
  68. A9GTimerUpdate update = a9_gtimer_get_update(s);
  69. int i;
  70. int64_t next_cdiff = 0;
  71. for (i = 0; i < s->num_cpu; ++i) {
  72. A9GTimerPerCPU *gtb = &s->per_cpu[i];
  73. int64_t cdiff = 0;
  74. if ((s->control & R_CONTROL_TIMER_ENABLE) &&
  75. (gtb->control & R_CONTROL_COMP_ENABLE)) {
  76. /* R2p0+, where the compare function is >= */
  77. if (gtb->compare < update.new) {
  78. DB_PRINT("Compare event happened for CPU %d\n", i);
  79. gtb->status = 1;
  80. if (gtb->control & R_CONTROL_AUTO_INCREMENT && gtb->inc) {
  81. uint64_t inc =
  82. QEMU_ALIGN_UP(update.new - gtb->compare, gtb->inc);
  83. DB_PRINT("Auto incrementing timer compare by %"
  84. PRId64 "\n", inc);
  85. gtb->compare += inc;
  86. }
  87. }
  88. cdiff = (int64_t)gtb->compare - (int64_t)update.new + 1;
  89. if (cdiff > 0 && (cdiff < next_cdiff || !next_cdiff)) {
  90. next_cdiff = cdiff;
  91. }
  92. }
  93. qemu_set_irq(gtb->irq,
  94. gtb->status && (gtb->control & R_CONTROL_IRQ_ENABLE));
  95. }
  96. timer_del(s->timer);
  97. if (next_cdiff) {
  98. DB_PRINT("scheduling qemu_timer to fire again in %"
  99. PRIx64 " cycles\n", next_cdiff);
  100. timer_mod(s->timer, update.now + next_cdiff * a9_gtimer_get_conv(s));
  101. }
  102. if (s->control & R_CONTROL_TIMER_ENABLE) {
  103. s->counter = update.new;
  104. }
  105. if (sync) {
  106. s->cpu_ref_time = update.now;
  107. s->ref_counter = s->counter;
  108. }
  109. }
  110. static void a9_gtimer_update_no_sync(void *opaque)
  111. {
  112. A9GTimerState *s = A9_GTIMER(opaque);
  113. a9_gtimer_update(s, false);
  114. }
  115. static uint64_t a9_gtimer_read(void *opaque, hwaddr addr, unsigned size)
  116. {
  117. A9GTimerPerCPU *gtb = (A9GTimerPerCPU *)opaque;
  118. A9GTimerState *s = gtb->parent;
  119. A9GTimerUpdate update;
  120. uint64_t ret = 0;
  121. int shift = 0;
  122. switch (addr) {
  123. case R_COUNTER_HI:
  124. shift = 32;
  125. /* fallthrough */
  126. case R_COUNTER_LO:
  127. update = a9_gtimer_get_update(s);
  128. ret = extract64(update.new, shift, 32);
  129. break;
  130. case R_CONTROL:
  131. ret = s->control | gtb->control;
  132. break;
  133. case R_INTERRUPT_STATUS:
  134. ret = gtb->status;
  135. break;
  136. case R_COMPARATOR_HI:
  137. shift = 32;
  138. /* fallthrough */
  139. case R_COMPARATOR_LO:
  140. ret = extract64(gtb->compare, shift, 32);
  141. break;
  142. case R_AUTO_INCREMENT:
  143. ret = gtb->inc;
  144. break;
  145. default:
  146. qemu_log_mask(LOG_GUEST_ERROR, "bad a9gtimer register: %x\n",
  147. (unsigned)addr);
  148. return 0;
  149. }
  150. DB_PRINT("addr:%#x data:%#08" PRIx64 "\n", (unsigned)addr, ret);
  151. return ret;
  152. }
  153. static void a9_gtimer_write(void *opaque, hwaddr addr, uint64_t value,
  154. unsigned size)
  155. {
  156. A9GTimerPerCPU *gtb = (A9GTimerPerCPU *)opaque;
  157. A9GTimerState *s = gtb->parent;
  158. int shift = 0;
  159. DB_PRINT("addr:%#x data:%#08" PRIx64 "\n", (unsigned)addr, value);
  160. switch (addr) {
  161. case R_COUNTER_HI:
  162. shift = 32;
  163. /* fallthrough */
  164. case R_COUNTER_LO:
  165. /*
  166. * Keep it simple - ARM docco explicitly says to disable timer before
  167. * modding it, so don't bother trying to do all the difficult on the fly
  168. * timer modifications - (if they even work in real hardware??).
  169. */
  170. if (s->control & R_CONTROL_TIMER_ENABLE) {
  171. qemu_log_mask(LOG_GUEST_ERROR, "Cannot mod running ARM gtimer\n");
  172. return;
  173. }
  174. s->counter = deposit64(s->counter, shift, 32, value);
  175. return;
  176. case R_CONTROL:
  177. a9_gtimer_update(s, (value ^ s->control) & R_CONTROL_NEEDS_SYNC);
  178. gtb->control = value & R_CONTROL_BANKED;
  179. s->control = value & ~R_CONTROL_BANKED;
  180. break;
  181. case R_INTERRUPT_STATUS:
  182. a9_gtimer_update(s, false);
  183. gtb->status &= ~value;
  184. break;
  185. case R_COMPARATOR_HI:
  186. shift = 32;
  187. /* fallthrough */
  188. case R_COMPARATOR_LO:
  189. a9_gtimer_update(s, false);
  190. gtb->compare = deposit64(gtb->compare, shift, 32, value);
  191. break;
  192. case R_AUTO_INCREMENT:
  193. gtb->inc = value;
  194. return;
  195. default:
  196. return;
  197. }
  198. a9_gtimer_update(s, false);
  199. }
  200. /* Wrapper functions to implement the "read global timer for
  201. * the current CPU" memory regions.
  202. */
  203. static uint64_t a9_gtimer_this_read(void *opaque, hwaddr addr,
  204. unsigned size)
  205. {
  206. A9GTimerState *s = A9_GTIMER(opaque);
  207. int id = a9_gtimer_get_current_cpu(s);
  208. /* no \n so concatenates with message from read fn */
  209. DB_PRINT("CPU:%d:", id);
  210. return a9_gtimer_read(&s->per_cpu[id], addr, size);
  211. }
  212. static void a9_gtimer_this_write(void *opaque, hwaddr addr,
  213. uint64_t value, unsigned size)
  214. {
  215. A9GTimerState *s = A9_GTIMER(opaque);
  216. int id = a9_gtimer_get_current_cpu(s);
  217. /* no \n so concatenates with message from write fn */
  218. DB_PRINT("CPU:%d:", id);
  219. a9_gtimer_write(&s->per_cpu[id], addr, value, size);
  220. }
  221. static const MemoryRegionOps a9_gtimer_this_ops = {
  222. .read = a9_gtimer_this_read,
  223. .write = a9_gtimer_this_write,
  224. .valid = {
  225. .min_access_size = 4,
  226. .max_access_size = 4,
  227. },
  228. .endianness = DEVICE_NATIVE_ENDIAN,
  229. };
  230. static const MemoryRegionOps a9_gtimer_ops = {
  231. .read = a9_gtimer_read,
  232. .write = a9_gtimer_write,
  233. .valid = {
  234. .min_access_size = 4,
  235. .max_access_size = 4,
  236. },
  237. .endianness = DEVICE_NATIVE_ENDIAN,
  238. };
  239. static void a9_gtimer_reset(DeviceState *dev)
  240. {
  241. A9GTimerState *s = A9_GTIMER(dev);
  242. int i;
  243. s->counter = 0;
  244. s->control = 0;
  245. for (i = 0; i < s->num_cpu; i++) {
  246. A9GTimerPerCPU *gtb = &s->per_cpu[i];
  247. gtb->control = 0;
  248. gtb->status = 0;
  249. gtb->compare = 0;
  250. gtb->inc = 0;
  251. }
  252. a9_gtimer_update(s, false);
  253. }
  254. static void a9_gtimer_realize(DeviceState *dev, Error **errp)
  255. {
  256. A9GTimerState *s = A9_GTIMER(dev);
  257. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  258. int i;
  259. if (s->num_cpu < 1 || s->num_cpu > A9_GTIMER_MAX_CPUS) {
  260. error_setg(errp, "%s: num-cpu must be between 1 and %d",
  261. __func__, A9_GTIMER_MAX_CPUS);
  262. return;
  263. }
  264. memory_region_init_io(&s->iomem, OBJECT(dev), &a9_gtimer_this_ops, s,
  265. "a9gtimer shared", 0x20);
  266. sysbus_init_mmio(sbd, &s->iomem);
  267. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, a9_gtimer_update_no_sync, s);
  268. for (i = 0; i < s->num_cpu; i++) {
  269. A9GTimerPerCPU *gtb = &s->per_cpu[i];
  270. gtb->parent = s;
  271. sysbus_init_irq(sbd, &gtb->irq);
  272. memory_region_init_io(&gtb->iomem, OBJECT(dev), &a9_gtimer_ops, gtb,
  273. "a9gtimer per cpu", 0x20);
  274. sysbus_init_mmio(sbd, &gtb->iomem);
  275. }
  276. }
  277. static const VMStateDescription vmstate_a9_gtimer_per_cpu = {
  278. .name = "arm.cortex-a9-global-timer.percpu",
  279. .version_id = 1,
  280. .minimum_version_id = 1,
  281. .fields = (VMStateField[]) {
  282. VMSTATE_UINT32(control, A9GTimerPerCPU),
  283. VMSTATE_UINT64(compare, A9GTimerPerCPU),
  284. VMSTATE_UINT32(status, A9GTimerPerCPU),
  285. VMSTATE_UINT32(inc, A9GTimerPerCPU),
  286. VMSTATE_END_OF_LIST()
  287. }
  288. };
  289. static const VMStateDescription vmstate_a9_gtimer = {
  290. .name = "arm.cortex-a9-global-timer",
  291. .version_id = 1,
  292. .minimum_version_id = 1,
  293. .fields = (VMStateField[]) {
  294. VMSTATE_TIMER_PTR(timer, A9GTimerState),
  295. VMSTATE_UINT64(counter, A9GTimerState),
  296. VMSTATE_UINT64(ref_counter, A9GTimerState),
  297. VMSTATE_UINT64(cpu_ref_time, A9GTimerState),
  298. VMSTATE_STRUCT_VARRAY_UINT32(per_cpu, A9GTimerState, num_cpu,
  299. 1, vmstate_a9_gtimer_per_cpu,
  300. A9GTimerPerCPU),
  301. VMSTATE_END_OF_LIST()
  302. }
  303. };
  304. static Property a9_gtimer_properties[] = {
  305. DEFINE_PROP_UINT32("num-cpu", A9GTimerState, num_cpu, 0),
  306. DEFINE_PROP_END_OF_LIST()
  307. };
  308. static void a9_gtimer_class_init(ObjectClass *klass, void *data)
  309. {
  310. DeviceClass *dc = DEVICE_CLASS(klass);
  311. dc->realize = a9_gtimer_realize;
  312. dc->vmsd = &vmstate_a9_gtimer;
  313. dc->reset = a9_gtimer_reset;
  314. dc->props = a9_gtimer_properties;
  315. }
  316. static const TypeInfo a9_gtimer_info = {
  317. .name = TYPE_A9_GTIMER,
  318. .parent = TYPE_SYS_BUS_DEVICE,
  319. .instance_size = sizeof(A9GTimerState),
  320. .class_init = a9_gtimer_class_init,
  321. };
  322. static void a9_gtimer_register_types(void)
  323. {
  324. type_register_static(&a9_gtimer_info);
  325. }
  326. type_init(a9_gtimer_register_types)