slavio_timer.c 14 KB

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