slavio_timer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * QEMU Sparc SLAVIO timer controller emulation
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/timer.h"
  26. #include "hw/irq.h"
  27. #include "hw/ptimer.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/sysbus.h"
  30. #include "migration/vmstate.h"
  31. #include "trace.h"
  32. #include "qemu/module.h"
  33. #include "qom/object.h"
  34. /*
  35. * Registers of hardware timer in sun4m.
  36. *
  37. * This is the timer/counter part of chip STP2001 (Slave I/O), also
  38. * produced as NCR89C105. See
  39. * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
  40. *
  41. * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
  42. * are zero. Bit 31 is 1 when count has been reached.
  43. *
  44. * Per-CPU timers interrupt local CPU, system timer uses normal
  45. * interrupt routing.
  46. *
  47. */
  48. #define MAX_CPUS 16
  49. typedef struct CPUTimerState {
  50. qemu_irq irq;
  51. ptimer_state *timer;
  52. uint32_t count, counthigh, reached;
  53. /* processor only */
  54. uint32_t run;
  55. uint64_t limit;
  56. } CPUTimerState;
  57. #define TYPE_SLAVIO_TIMER "slavio_timer"
  58. OBJECT_DECLARE_SIMPLE_TYPE(SLAVIO_TIMERState, SLAVIO_TIMER)
  59. struct SLAVIO_TIMERState {
  60. SysBusDevice parent_obj;
  61. uint32_t num_cpus;
  62. uint32_t cputimer_mode;
  63. CPUTimerState cputimer[MAX_CPUS + 1];
  64. };
  65. typedef struct TimerContext {
  66. MemoryRegion iomem;
  67. SLAVIO_TIMERState *s;
  68. unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
  69. } TimerContext;
  70. #define SYS_TIMER_SIZE 0x14
  71. #define CPU_TIMER_SIZE 0x10
  72. #define TIMER_LIMIT 0
  73. #define TIMER_COUNTER 1
  74. #define TIMER_COUNTER_NORST 2
  75. #define TIMER_STATUS 3
  76. #define TIMER_MODE 4
  77. #define TIMER_COUNT_MASK32 0xfffffe00
  78. #define TIMER_LIMIT_MASK32 0x7fffffff
  79. #define TIMER_MAX_COUNT64 0x7ffffffffffffe00ULL
  80. #define TIMER_MAX_COUNT32 0x7ffffe00ULL
  81. #define TIMER_REACHED 0x80000000
  82. #define TIMER_PERIOD 500ULL // 500ns
  83. #define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
  84. #define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
  85. static int slavio_timer_is_user(TimerContext *tc)
  86. {
  87. SLAVIO_TIMERState *s = tc->s;
  88. unsigned int timer_index = tc->timer_index;
  89. return timer_index != 0 && (s->cputimer_mode & (1 << (timer_index - 1)));
  90. }
  91. // Update count, set irq, update expire_time
  92. // Convert from ptimer countdown units
  93. static void slavio_timer_get_out(CPUTimerState *t)
  94. {
  95. uint64_t count, limit;
  96. if (t->limit == 0) { /* free-run system or processor counter */
  97. limit = TIMER_MAX_COUNT32;
  98. } else {
  99. limit = t->limit;
  100. }
  101. count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));
  102. trace_slavio_timer_get_out(t->limit, t->counthigh, t->count);
  103. t->count = count & TIMER_COUNT_MASK32;
  104. t->counthigh = count >> 32;
  105. }
  106. // timer callback
  107. static void slavio_timer_irq(void *opaque)
  108. {
  109. TimerContext *tc = opaque;
  110. SLAVIO_TIMERState *s = tc->s;
  111. CPUTimerState *t = &s->cputimer[tc->timer_index];
  112. slavio_timer_get_out(t);
  113. trace_slavio_timer_irq(t->counthigh, t->count);
  114. /* if limit is 0 (free-run), there will be no match */
  115. if (t->limit != 0) {
  116. t->reached = TIMER_REACHED;
  117. }
  118. /* there is no interrupt if user timer or free-run */
  119. if (!slavio_timer_is_user(tc) && t->limit != 0) {
  120. qemu_irq_raise(t->irq);
  121. }
  122. }
  123. static uint64_t slavio_timer_mem_readl(void *opaque, hwaddr addr,
  124. unsigned size)
  125. {
  126. TimerContext *tc = opaque;
  127. SLAVIO_TIMERState *s = tc->s;
  128. uint32_t saddr, ret;
  129. unsigned int timer_index = tc->timer_index;
  130. CPUTimerState *t = &s->cputimer[timer_index];
  131. saddr = addr >> 2;
  132. switch (saddr) {
  133. case TIMER_LIMIT:
  134. // read limit (system counter mode) or read most signifying
  135. // part of counter (user mode)
  136. if (slavio_timer_is_user(tc)) {
  137. // read user timer MSW
  138. slavio_timer_get_out(t);
  139. ret = t->counthigh | t->reached;
  140. } else {
  141. // read limit
  142. // clear irq
  143. qemu_irq_lower(t->irq);
  144. t->reached = 0;
  145. ret = t->limit & TIMER_LIMIT_MASK32;
  146. }
  147. break;
  148. case TIMER_COUNTER:
  149. // read counter and reached bit (system mode) or read lsbits
  150. // of counter (user mode)
  151. slavio_timer_get_out(t);
  152. if (slavio_timer_is_user(tc)) { // read user timer LSW
  153. ret = t->count & TIMER_MAX_COUNT64;
  154. } else { // read limit
  155. ret = (t->count & TIMER_MAX_COUNT32) |
  156. t->reached;
  157. }
  158. break;
  159. case TIMER_STATUS:
  160. // only available in processor counter/timer
  161. // read start/stop status
  162. if (timer_index > 0) {
  163. ret = t->run;
  164. } else {
  165. ret = 0;
  166. }
  167. break;
  168. case TIMER_MODE:
  169. // only available in system counter
  170. // read user/system mode
  171. ret = s->cputimer_mode;
  172. break;
  173. default:
  174. trace_slavio_timer_mem_readl_invalid(addr);
  175. ret = 0;
  176. break;
  177. }
  178. trace_slavio_timer_mem_readl(addr, ret);
  179. return ret;
  180. }
  181. static void slavio_timer_mem_writel(void *opaque, hwaddr addr,
  182. uint64_t val, unsigned size)
  183. {
  184. TimerContext *tc = opaque;
  185. SLAVIO_TIMERState *s = tc->s;
  186. uint32_t saddr;
  187. unsigned int timer_index = tc->timer_index;
  188. CPUTimerState *t = &s->cputimer[timer_index];
  189. trace_slavio_timer_mem_writel(addr, val);
  190. saddr = addr >> 2;
  191. switch (saddr) {
  192. case TIMER_LIMIT:
  193. ptimer_transaction_begin(t->timer);
  194. if (slavio_timer_is_user(tc)) {
  195. uint64_t count;
  196. // set user counter MSW, reset counter
  197. t->limit = TIMER_MAX_COUNT64;
  198. t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
  199. t->reached = 0;
  200. count = ((uint64_t)t->counthigh << 32) | t->count;
  201. trace_slavio_timer_mem_writel_limit(timer_index, count);
  202. ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
  203. } else {
  204. // set limit, reset counter
  205. qemu_irq_lower(t->irq);
  206. t->limit = val & TIMER_MAX_COUNT32;
  207. if (t->limit == 0) { /* free-run */
  208. ptimer_set_limit(t->timer,
  209. LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
  210. } else {
  211. ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
  212. }
  213. }
  214. ptimer_transaction_commit(t->timer);
  215. break;
  216. case TIMER_COUNTER:
  217. if (slavio_timer_is_user(tc)) {
  218. uint64_t count;
  219. // set user counter LSW, reset counter
  220. t->limit = TIMER_MAX_COUNT64;
  221. t->count = val & TIMER_MAX_COUNT64;
  222. t->reached = 0;
  223. count = ((uint64_t)t->counthigh) << 32 | t->count;
  224. trace_slavio_timer_mem_writel_limit(timer_index, count);
  225. ptimer_transaction_begin(t->timer);
  226. ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
  227. ptimer_transaction_commit(t->timer);
  228. } else {
  229. trace_slavio_timer_mem_writel_counter_invalid();
  230. }
  231. break;
  232. case TIMER_COUNTER_NORST:
  233. // set limit without resetting counter
  234. t->limit = val & TIMER_MAX_COUNT32;
  235. ptimer_transaction_begin(t->timer);
  236. if (t->limit == 0) { /* free-run */
  237. ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
  238. } else {
  239. ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 0);
  240. }
  241. ptimer_transaction_commit(t->timer);
  242. break;
  243. case TIMER_STATUS:
  244. ptimer_transaction_begin(t->timer);
  245. if (slavio_timer_is_user(tc)) {
  246. // start/stop user counter
  247. if (val & 1) {
  248. trace_slavio_timer_mem_writel_status_start(timer_index);
  249. ptimer_run(t->timer, 0);
  250. } else {
  251. trace_slavio_timer_mem_writel_status_stop(timer_index);
  252. ptimer_stop(t->timer);
  253. }
  254. }
  255. t->run = val & 1;
  256. ptimer_transaction_commit(t->timer);
  257. break;
  258. case TIMER_MODE:
  259. if (timer_index == 0) {
  260. unsigned int i;
  261. for (i = 0; i < s->num_cpus; i++) {
  262. unsigned int processor = 1 << i;
  263. CPUTimerState *curr_timer = &s->cputimer[i + 1];
  264. ptimer_transaction_begin(curr_timer->timer);
  265. // check for a change in timer mode for this processor
  266. if ((val & processor) != (s->cputimer_mode & processor)) {
  267. if (val & processor) { // counter -> user timer
  268. qemu_irq_lower(curr_timer->irq);
  269. // counters are always running
  270. if (!curr_timer->run) {
  271. ptimer_stop(curr_timer->timer);
  272. }
  273. // user timer limit is always the same
  274. curr_timer->limit = TIMER_MAX_COUNT64;
  275. ptimer_set_limit(curr_timer->timer,
  276. LIMIT_TO_PERIODS(curr_timer->limit),
  277. 1);
  278. // set this processors user timer bit in config
  279. // register
  280. s->cputimer_mode |= processor;
  281. trace_slavio_timer_mem_writel_mode_user(timer_index);
  282. } else { // user timer -> counter
  283. // start the counter
  284. ptimer_run(curr_timer->timer, 0);
  285. // clear this processors user timer bit in config
  286. // register
  287. s->cputimer_mode &= ~processor;
  288. trace_slavio_timer_mem_writel_mode_counter(timer_index);
  289. }
  290. }
  291. ptimer_transaction_commit(curr_timer->timer);
  292. }
  293. } else {
  294. trace_slavio_timer_mem_writel_mode_invalid();
  295. }
  296. break;
  297. default:
  298. trace_slavio_timer_mem_writel_invalid(addr);
  299. break;
  300. }
  301. }
  302. static const MemoryRegionOps slavio_timer_mem_ops = {
  303. .read = slavio_timer_mem_readl,
  304. .write = slavio_timer_mem_writel,
  305. .endianness = DEVICE_NATIVE_ENDIAN,
  306. .valid = {
  307. .min_access_size = 4,
  308. .max_access_size = 8,
  309. },
  310. .impl = {
  311. .min_access_size = 4,
  312. .max_access_size = 4,
  313. },
  314. };
  315. static const VMStateDescription vmstate_timer = {
  316. .name ="timer",
  317. .version_id = 3,
  318. .minimum_version_id = 3,
  319. .fields = (VMStateField[]) {
  320. VMSTATE_UINT64(limit, CPUTimerState),
  321. VMSTATE_UINT32(count, CPUTimerState),
  322. VMSTATE_UINT32(counthigh, CPUTimerState),
  323. VMSTATE_UINT32(reached, CPUTimerState),
  324. VMSTATE_UINT32(run , CPUTimerState),
  325. VMSTATE_PTIMER(timer, CPUTimerState),
  326. VMSTATE_END_OF_LIST()
  327. }
  328. };
  329. static const VMStateDescription vmstate_slavio_timer = {
  330. .name ="slavio_timer",
  331. .version_id = 3,
  332. .minimum_version_id = 3,
  333. .fields = (VMStateField[]) {
  334. VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
  335. vmstate_timer, CPUTimerState),
  336. VMSTATE_END_OF_LIST()
  337. }
  338. };
  339. static void slavio_timer_reset(DeviceState *d)
  340. {
  341. SLAVIO_TIMERState *s = SLAVIO_TIMER(d);
  342. unsigned int i;
  343. CPUTimerState *curr_timer;
  344. for (i = 0; i <= MAX_CPUS; i++) {
  345. curr_timer = &s->cputimer[i];
  346. curr_timer->limit = 0;
  347. curr_timer->count = 0;
  348. curr_timer->reached = 0;
  349. if (i <= s->num_cpus) {
  350. ptimer_transaction_begin(curr_timer->timer);
  351. ptimer_set_limit(curr_timer->timer,
  352. LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
  353. ptimer_run(curr_timer->timer, 0);
  354. curr_timer->run = 1;
  355. ptimer_transaction_commit(curr_timer->timer);
  356. }
  357. }
  358. s->cputimer_mode = 0;
  359. }
  360. static void slavio_timer_init(Object *obj)
  361. {
  362. SLAVIO_TIMERState *s = SLAVIO_TIMER(obj);
  363. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  364. unsigned int i;
  365. TimerContext *tc;
  366. for (i = 0; i <= MAX_CPUS; i++) {
  367. uint64_t size;
  368. char timer_name[20];
  369. tc = g_new0(TimerContext, 1);
  370. tc->s = s;
  371. tc->timer_index = i;
  372. s->cputimer[i].timer = ptimer_init(slavio_timer_irq, tc,
  373. PTIMER_POLICY_DEFAULT);
  374. ptimer_transaction_begin(s->cputimer[i].timer);
  375. ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
  376. ptimer_transaction_commit(s->cputimer[i].timer);
  377. size = i == 0 ? SYS_TIMER_SIZE : CPU_TIMER_SIZE;
  378. snprintf(timer_name, sizeof(timer_name), "timer-%i", i);
  379. memory_region_init_io(&tc->iomem, obj, &slavio_timer_mem_ops, tc,
  380. timer_name, size);
  381. sysbus_init_mmio(dev, &tc->iomem);
  382. sysbus_init_irq(dev, &s->cputimer[i].irq);
  383. }
  384. }
  385. static Property slavio_timer_properties[] = {
  386. DEFINE_PROP_UINT32("num_cpus", SLAVIO_TIMERState, num_cpus, 0),
  387. DEFINE_PROP_END_OF_LIST(),
  388. };
  389. static void slavio_timer_class_init(ObjectClass *klass, void *data)
  390. {
  391. DeviceClass *dc = DEVICE_CLASS(klass);
  392. dc->reset = slavio_timer_reset;
  393. dc->vmsd = &vmstate_slavio_timer;
  394. device_class_set_props(dc, slavio_timer_properties);
  395. }
  396. static const TypeInfo slavio_timer_info = {
  397. .name = TYPE_SLAVIO_TIMER,
  398. .parent = TYPE_SYS_BUS_DEVICE,
  399. .instance_size = sizeof(SLAVIO_TIMERState),
  400. .instance_init = slavio_timer_init,
  401. .class_init = slavio_timer_class_init,
  402. };
  403. static void slavio_timer_register_types(void)
  404. {
  405. type_register_static(&slavio_timer_info);
  406. }
  407. type_init(slavio_timer_register_types)