a9gtimer.c 11 KB

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