a9gtimer.c 11 KB

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