2
0

slavio_timer.c 14 KB

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