2
0

slavio_timer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 "hw/sparc/sun4m.h"
  25. #include "qemu/timer.h"
  26. #include "hw/ptimer.h"
  27. #include "hw/sysbus.h"
  28. #include "trace.h"
  29. #include "qemu/main-loop.h"
  30. /*
  31. * Registers of hardware timer in sun4m.
  32. *
  33. * This is the timer/counter part of chip STP2001 (Slave I/O), also
  34. * produced as NCR89C105. See
  35. * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
  36. *
  37. * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
  38. * are zero. Bit 31 is 1 when count has been reached.
  39. *
  40. * Per-CPU timers interrupt local CPU, system timer uses normal
  41. * interrupt routing.
  42. *
  43. */
  44. #define MAX_CPUS 16
  45. typedef struct CPUTimerState {
  46. qemu_irq irq;
  47. ptimer_state *timer;
  48. uint32_t count, counthigh, reached;
  49. /* processor only */
  50. uint32_t run;
  51. uint64_t limit;
  52. } CPUTimerState;
  53. #define TYPE_SLAVIO_TIMER "slavio_timer"
  54. #define SLAVIO_TIMER(obj) \
  55. OBJECT_CHECK(SLAVIO_TIMERState, (obj), TYPE_SLAVIO_TIMER)
  56. typedef struct SLAVIO_TIMERState {
  57. SysBusDevice parent_obj;
  58. uint32_t num_cpus;
  59. uint32_t cputimer_mode;
  60. CPUTimerState cputimer[MAX_CPUS + 1];
  61. } SLAVIO_TIMERState;
  62. typedef struct TimerContext {
  63. MemoryRegion iomem;
  64. SLAVIO_TIMERState *s;
  65. unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
  66. } TimerContext;
  67. #define SYS_TIMER_SIZE 0x14
  68. #define CPU_TIMER_SIZE 0x10
  69. #define TIMER_LIMIT 0
  70. #define TIMER_COUNTER 1
  71. #define TIMER_COUNTER_NORST 2
  72. #define TIMER_STATUS 3
  73. #define TIMER_MODE 4
  74. #define TIMER_COUNT_MASK32 0xfffffe00
  75. #define TIMER_LIMIT_MASK32 0x7fffffff
  76. #define TIMER_MAX_COUNT64 0x7ffffffffffffe00ULL
  77. #define TIMER_MAX_COUNT32 0x7ffffe00ULL
  78. #define TIMER_REACHED 0x80000000
  79. #define TIMER_PERIOD 500ULL // 500ns
  80. #define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
  81. #define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
  82. static int slavio_timer_is_user(TimerContext *tc)
  83. {
  84. SLAVIO_TIMERState *s = tc->s;
  85. unsigned int timer_index = tc->timer_index;
  86. return timer_index != 0 && (s->cputimer_mode & (1 << (timer_index - 1)));
  87. }
  88. // Update count, set irq, update expire_time
  89. // Convert from ptimer countdown units
  90. static void slavio_timer_get_out(CPUTimerState *t)
  91. {
  92. uint64_t count, limit;
  93. if (t->limit == 0) { /* free-run system or processor counter */
  94. limit = TIMER_MAX_COUNT32;
  95. } else {
  96. limit = t->limit;
  97. }
  98. count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));
  99. trace_slavio_timer_get_out(t->limit, t->counthigh, t->count);
  100. t->count = count & TIMER_COUNT_MASK32;
  101. t->counthigh = count >> 32;
  102. }
  103. // timer callback
  104. static void slavio_timer_irq(void *opaque)
  105. {
  106. TimerContext *tc = opaque;
  107. SLAVIO_TIMERState *s = tc->s;
  108. CPUTimerState *t = &s->cputimer[tc->timer_index];
  109. slavio_timer_get_out(t);
  110. trace_slavio_timer_irq(t->counthigh, t->count);
  111. /* if limit is 0 (free-run), there will be no match */
  112. if (t->limit != 0) {
  113. t->reached = TIMER_REACHED;
  114. }
  115. /* there is no interrupt if user timer or free-run */
  116. if (!slavio_timer_is_user(tc) && t->limit != 0) {
  117. qemu_irq_raise(t->irq);
  118. }
  119. }
  120. static uint64_t slavio_timer_mem_readl(void *opaque, hwaddr addr,
  121. unsigned size)
  122. {
  123. TimerContext *tc = opaque;
  124. SLAVIO_TIMERState *s = tc->s;
  125. uint32_t saddr, ret;
  126. unsigned int timer_index = tc->timer_index;
  127. CPUTimerState *t = &s->cputimer[timer_index];
  128. saddr = addr >> 2;
  129. switch (saddr) {
  130. case TIMER_LIMIT:
  131. // read limit (system counter mode) or read most signifying
  132. // part of counter (user mode)
  133. if (slavio_timer_is_user(tc)) {
  134. // read user timer MSW
  135. slavio_timer_get_out(t);
  136. ret = t->counthigh | t->reached;
  137. } else {
  138. // read limit
  139. // clear irq
  140. qemu_irq_lower(t->irq);
  141. t->reached = 0;
  142. ret = t->limit & TIMER_LIMIT_MASK32;
  143. }
  144. break;
  145. case TIMER_COUNTER:
  146. // read counter and reached bit (system mode) or read lsbits
  147. // of counter (user mode)
  148. slavio_timer_get_out(t);
  149. if (slavio_timer_is_user(tc)) { // read user timer LSW
  150. ret = t->count & TIMER_MAX_COUNT64;
  151. } else { // read limit
  152. ret = (t->count & TIMER_MAX_COUNT32) |
  153. t->reached;
  154. }
  155. break;
  156. case TIMER_STATUS:
  157. // only available in processor counter/timer
  158. // read start/stop status
  159. if (timer_index > 0) {
  160. ret = t->run;
  161. } else {
  162. ret = 0;
  163. }
  164. break;
  165. case TIMER_MODE:
  166. // only available in system counter
  167. // read user/system mode
  168. ret = s->cputimer_mode;
  169. break;
  170. default:
  171. trace_slavio_timer_mem_readl_invalid(addr);
  172. ret = 0;
  173. break;
  174. }
  175. trace_slavio_timer_mem_readl(addr, ret);
  176. return ret;
  177. }
  178. static void slavio_timer_mem_writel(void *opaque, hwaddr addr,
  179. uint64_t val, unsigned size)
  180. {
  181. TimerContext *tc = opaque;
  182. SLAVIO_TIMERState *s = tc->s;
  183. uint32_t saddr;
  184. unsigned int timer_index = tc->timer_index;
  185. CPUTimerState *t = &s->cputimer[timer_index];
  186. trace_slavio_timer_mem_writel(addr, val);
  187. saddr = addr >> 2;
  188. switch (saddr) {
  189. case TIMER_LIMIT:
  190. if (slavio_timer_is_user(tc)) {
  191. uint64_t count;
  192. // set user counter MSW, reset counter
  193. t->limit = TIMER_MAX_COUNT64;
  194. t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
  195. t->reached = 0;
  196. count = ((uint64_t)t->counthigh << 32) | t->count;
  197. trace_slavio_timer_mem_writel_limit(timer_index, count);
  198. ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
  199. } else {
  200. // set limit, reset counter
  201. qemu_irq_lower(t->irq);
  202. t->limit = val & TIMER_MAX_COUNT32;
  203. if (t->timer) {
  204. if (t->limit == 0) { /* free-run */
  205. ptimer_set_limit(t->timer,
  206. LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
  207. } else {
  208. ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
  209. }
  210. }
  211. }
  212. break;
  213. case TIMER_COUNTER:
  214. if (slavio_timer_is_user(tc)) {
  215. uint64_t count;
  216. // set user counter LSW, reset counter
  217. t->limit = TIMER_MAX_COUNT64;
  218. t->count = val & TIMER_MAX_COUNT64;
  219. t->reached = 0;
  220. count = ((uint64_t)t->counthigh) << 32 | t->count;
  221. trace_slavio_timer_mem_writel_limit(timer_index, count);
  222. ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
  223. } else {
  224. trace_slavio_timer_mem_writel_counter_invalid();
  225. }
  226. break;
  227. case TIMER_COUNTER_NORST:
  228. // set limit without resetting counter
  229. t->limit = val & TIMER_MAX_COUNT32;
  230. if (t->limit == 0) { /* free-run */
  231. ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
  232. } else {
  233. ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 0);
  234. }
  235. break;
  236. case TIMER_STATUS:
  237. if (slavio_timer_is_user(tc)) {
  238. // start/stop user counter
  239. if (val & 1) {
  240. trace_slavio_timer_mem_writel_status_start(timer_index);
  241. ptimer_run(t->timer, 0);
  242. } else {
  243. trace_slavio_timer_mem_writel_status_stop(timer_index);
  244. ptimer_stop(t->timer);
  245. }
  246. }
  247. t->run = val & 1;
  248. break;
  249. case TIMER_MODE:
  250. if (timer_index == 0) {
  251. unsigned int i;
  252. for (i = 0; i < s->num_cpus; i++) {
  253. unsigned int processor = 1 << i;
  254. CPUTimerState *curr_timer = &s->cputimer[i + 1];
  255. // check for a change in timer mode for this processor
  256. if ((val & processor) != (s->cputimer_mode & processor)) {
  257. if (val & processor) { // counter -> user timer
  258. qemu_irq_lower(curr_timer->irq);
  259. // counters are always running
  260. if (!curr_timer->run) {
  261. ptimer_stop(curr_timer->timer);
  262. }
  263. // user timer limit is always the same
  264. curr_timer->limit = TIMER_MAX_COUNT64;
  265. ptimer_set_limit(curr_timer->timer,
  266. LIMIT_TO_PERIODS(curr_timer->limit),
  267. 1);
  268. // set this processors user timer bit in config
  269. // register
  270. s->cputimer_mode |= processor;
  271. trace_slavio_timer_mem_writel_mode_user(timer_index);
  272. } else { // user timer -> counter
  273. // start the counter
  274. ptimer_run(curr_timer->timer, 0);
  275. // clear this processors user timer bit in config
  276. // register
  277. s->cputimer_mode &= ~processor;
  278. trace_slavio_timer_mem_writel_mode_counter(timer_index);
  279. }
  280. }
  281. }
  282. } else {
  283. trace_slavio_timer_mem_writel_mode_invalid();
  284. }
  285. break;
  286. default:
  287. trace_slavio_timer_mem_writel_invalid(addr);
  288. break;
  289. }
  290. }
  291. static const MemoryRegionOps slavio_timer_mem_ops = {
  292. .read = slavio_timer_mem_readl,
  293. .write = slavio_timer_mem_writel,
  294. .endianness = DEVICE_NATIVE_ENDIAN,
  295. .valid = {
  296. .min_access_size = 4,
  297. .max_access_size = 4,
  298. },
  299. };
  300. static const VMStateDescription vmstate_timer = {
  301. .name ="timer",
  302. .version_id = 3,
  303. .minimum_version_id = 3,
  304. .fields = (VMStateField[]) {
  305. VMSTATE_UINT64(limit, CPUTimerState),
  306. VMSTATE_UINT32(count, CPUTimerState),
  307. VMSTATE_UINT32(counthigh, CPUTimerState),
  308. VMSTATE_UINT32(reached, CPUTimerState),
  309. VMSTATE_UINT32(run , CPUTimerState),
  310. VMSTATE_PTIMER(timer, CPUTimerState),
  311. VMSTATE_END_OF_LIST()
  312. }
  313. };
  314. static const VMStateDescription vmstate_slavio_timer = {
  315. .name ="slavio_timer",
  316. .version_id = 3,
  317. .minimum_version_id = 3,
  318. .fields = (VMStateField[]) {
  319. VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
  320. vmstate_timer, CPUTimerState),
  321. VMSTATE_END_OF_LIST()
  322. }
  323. };
  324. static void slavio_timer_reset(DeviceState *d)
  325. {
  326. SLAVIO_TIMERState *s = SLAVIO_TIMER(d);
  327. unsigned int i;
  328. CPUTimerState *curr_timer;
  329. for (i = 0; i <= MAX_CPUS; i++) {
  330. curr_timer = &s->cputimer[i];
  331. curr_timer->limit = 0;
  332. curr_timer->count = 0;
  333. curr_timer->reached = 0;
  334. if (i <= s->num_cpus) {
  335. ptimer_set_limit(curr_timer->timer,
  336. LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
  337. ptimer_run(curr_timer->timer, 0);
  338. curr_timer->run = 1;
  339. }
  340. }
  341. s->cputimer_mode = 0;
  342. }
  343. static int slavio_timer_init1(SysBusDevice *dev)
  344. {
  345. SLAVIO_TIMERState *s = SLAVIO_TIMER(dev);
  346. QEMUBH *bh;
  347. unsigned int i;
  348. TimerContext *tc;
  349. for (i = 0; i <= MAX_CPUS; i++) {
  350. uint64_t size;
  351. char timer_name[20];
  352. tc = g_malloc0(sizeof(TimerContext));
  353. tc->s = s;
  354. tc->timer_index = i;
  355. bh = qemu_bh_new(slavio_timer_irq, tc);
  356. s->cputimer[i].timer = ptimer_init(bh);
  357. ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
  358. size = i == 0 ? SYS_TIMER_SIZE : CPU_TIMER_SIZE;
  359. snprintf(timer_name, sizeof(timer_name), "timer-%i", i);
  360. memory_region_init_io(&tc->iomem, OBJECT(s), &slavio_timer_mem_ops, tc,
  361. timer_name, size);
  362. sysbus_init_mmio(dev, &tc->iomem);
  363. sysbus_init_irq(dev, &s->cputimer[i].irq);
  364. }
  365. return 0;
  366. }
  367. static Property slavio_timer_properties[] = {
  368. DEFINE_PROP_UINT32("num_cpus", SLAVIO_TIMERState, num_cpus, 0),
  369. DEFINE_PROP_END_OF_LIST(),
  370. };
  371. static void slavio_timer_class_init(ObjectClass *klass, void *data)
  372. {
  373. DeviceClass *dc = DEVICE_CLASS(klass);
  374. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  375. k->init = slavio_timer_init1;
  376. dc->reset = slavio_timer_reset;
  377. dc->vmsd = &vmstate_slavio_timer;
  378. dc->props = slavio_timer_properties;
  379. }
  380. static const TypeInfo slavio_timer_info = {
  381. .name = TYPE_SLAVIO_TIMER,
  382. .parent = TYPE_SYS_BUS_DEVICE,
  383. .instance_size = sizeof(SLAVIO_TIMERState),
  384. .class_init = slavio_timer_class_init,
  385. };
  386. static void slavio_timer_register_types(void)
  387. {
  388. type_register_static(&slavio_timer_info);
  389. }
  390. type_init(slavio_timer_register_types)