hpet.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * High Precisition Event Timer emulation
  3. *
  4. * Copyright (c) 2007 Alexander Graf
  5. * Copyright (c) 2008 IBM Corporation
  6. *
  7. * Authors: Beth Kon <bkon@us.ibm.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library 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 GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  22. *
  23. * *****************************************************************
  24. *
  25. * This driver attempts to emulate an HPET device in software.
  26. */
  27. #include "hw.h"
  28. #include "pc.h"
  29. #include "console.h"
  30. #include "qemu-timer.h"
  31. #include "hpet_emul.h"
  32. //#define HPET_DEBUG
  33. #ifdef HPET_DEBUG
  34. #define dprintf printf
  35. #else
  36. #define dprintf(...)
  37. #endif
  38. static HPETState *hpet_statep;
  39. uint32_t hpet_in_legacy_mode(void)
  40. {
  41. if (hpet_statep)
  42. return hpet_statep->config & HPET_CFG_LEGACY;
  43. else
  44. return 0;
  45. }
  46. static uint32_t timer_int_route(struct HPETTimer *timer)
  47. {
  48. uint32_t route;
  49. route = (timer->config & HPET_TN_INT_ROUTE_MASK) >> HPET_TN_INT_ROUTE_SHIFT;
  50. return route;
  51. }
  52. static uint32_t hpet_enabled(void)
  53. {
  54. return hpet_statep->config & HPET_CFG_ENABLE;
  55. }
  56. static uint32_t timer_is_periodic(HPETTimer *t)
  57. {
  58. return t->config & HPET_TN_PERIODIC;
  59. }
  60. static uint32_t timer_enabled(HPETTimer *t)
  61. {
  62. return t->config & HPET_TN_ENABLE;
  63. }
  64. static uint32_t hpet_time_after(uint64_t a, uint64_t b)
  65. {
  66. return ((int32_t)(b) - (int32_t)(a) < 0);
  67. }
  68. static uint32_t hpet_time_after64(uint64_t a, uint64_t b)
  69. {
  70. return ((int64_t)(b) - (int64_t)(a) < 0);
  71. }
  72. static uint64_t ticks_to_ns(uint64_t value)
  73. {
  74. return (muldiv64(value, HPET_CLK_PERIOD, FS_PER_NS));
  75. }
  76. static uint64_t ns_to_ticks(uint64_t value)
  77. {
  78. return (muldiv64(value, FS_PER_NS, HPET_CLK_PERIOD));
  79. }
  80. static uint64_t hpet_fixup_reg(uint64_t new, uint64_t old, uint64_t mask)
  81. {
  82. new &= mask;
  83. new |= old & ~mask;
  84. return new;
  85. }
  86. static int activating_bit(uint64_t old, uint64_t new, uint64_t mask)
  87. {
  88. return (!(old & mask) && (new & mask));
  89. }
  90. static int deactivating_bit(uint64_t old, uint64_t new, uint64_t mask)
  91. {
  92. return ((old & mask) && !(new & mask));
  93. }
  94. static uint64_t hpet_get_ticks(void)
  95. {
  96. uint64_t ticks;
  97. ticks = ns_to_ticks(qemu_get_clock(vm_clock) + hpet_statep->hpet_offset);
  98. return ticks;
  99. }
  100. /*
  101. * calculate diff between comparator value and current ticks
  102. */
  103. static inline uint64_t hpet_calculate_diff(HPETTimer *t, uint64_t current)
  104. {
  105. if (t->config & HPET_TN_32BIT) {
  106. uint32_t diff, cmp;
  107. cmp = (uint32_t)t->cmp;
  108. diff = cmp - (uint32_t)current;
  109. diff = (int32_t)diff > 0 ? diff : (uint32_t)0;
  110. return (uint64_t)diff;
  111. } else {
  112. uint64_t diff, cmp;
  113. cmp = t->cmp;
  114. diff = cmp - current;
  115. diff = (int64_t)diff > 0 ? diff : (uint64_t)0;
  116. return diff;
  117. }
  118. }
  119. static void update_irq(struct HPETTimer *timer)
  120. {
  121. qemu_irq irq;
  122. int route;
  123. if (timer->tn <= 1 && hpet_in_legacy_mode()) {
  124. /* if LegacyReplacementRoute bit is set, HPET specification requires
  125. * timer0 be routed to IRQ0 in NON-APIC or IRQ2 in the I/O APIC,
  126. * timer1 be routed to IRQ8 in NON-APIC or IRQ8 in the I/O APIC.
  127. */
  128. if (timer->tn == 0) {
  129. irq=timer->state->irqs[0];
  130. } else
  131. irq=timer->state->irqs[8];
  132. } else {
  133. route=timer_int_route(timer);
  134. irq=timer->state->irqs[route];
  135. }
  136. if (timer_enabled(timer) && hpet_enabled()) {
  137. qemu_irq_pulse(irq);
  138. }
  139. }
  140. static void hpet_save(QEMUFile *f, void *opaque)
  141. {
  142. HPETState *s = opaque;
  143. int i;
  144. qemu_put_be64s(f, &s->config);
  145. qemu_put_be64s(f, &s->isr);
  146. /* save current counter value */
  147. s->hpet_counter = hpet_get_ticks();
  148. qemu_put_be64s(f, &s->hpet_counter);
  149. for (i = 0; i < HPET_NUM_TIMERS; i++) {
  150. qemu_put_8s(f, &s->timer[i].tn);
  151. qemu_put_be64s(f, &s->timer[i].config);
  152. qemu_put_be64s(f, &s->timer[i].cmp);
  153. qemu_put_be64s(f, &s->timer[i].fsb);
  154. qemu_put_be64s(f, &s->timer[i].period);
  155. qemu_put_8s(f, &s->timer[i].wrap_flag);
  156. if (s->timer[i].qemu_timer) {
  157. qemu_put_timer(f, s->timer[i].qemu_timer);
  158. }
  159. }
  160. }
  161. static int hpet_load(QEMUFile *f, void *opaque, int version_id)
  162. {
  163. HPETState *s = opaque;
  164. int i;
  165. if (version_id != 1)
  166. return -EINVAL;
  167. qemu_get_be64s(f, &s->config);
  168. qemu_get_be64s(f, &s->isr);
  169. qemu_get_be64s(f, &s->hpet_counter);
  170. /* Recalculate the offset between the main counter and guest time */
  171. s->hpet_offset = ticks_to_ns(s->hpet_counter) - qemu_get_clock(vm_clock);
  172. for (i = 0; i < HPET_NUM_TIMERS; i++) {
  173. qemu_get_8s(f, &s->timer[i].tn);
  174. qemu_get_be64s(f, &s->timer[i].config);
  175. qemu_get_be64s(f, &s->timer[i].cmp);
  176. qemu_get_be64s(f, &s->timer[i].fsb);
  177. qemu_get_be64s(f, &s->timer[i].period);
  178. qemu_get_8s(f, &s->timer[i].wrap_flag);
  179. if (s->timer[i].qemu_timer) {
  180. qemu_get_timer(f, s->timer[i].qemu_timer);
  181. }
  182. }
  183. return 0;
  184. }
  185. /*
  186. * timer expiration callback
  187. */
  188. static void hpet_timer(void *opaque)
  189. {
  190. HPETTimer *t = (HPETTimer*)opaque;
  191. uint64_t diff;
  192. uint64_t period = t->period;
  193. uint64_t cur_tick = hpet_get_ticks();
  194. if (timer_is_periodic(t) && period != 0) {
  195. if (t->config & HPET_TN_32BIT) {
  196. while (hpet_time_after(cur_tick, t->cmp))
  197. t->cmp = (uint32_t)(t->cmp + t->period);
  198. } else
  199. while (hpet_time_after64(cur_tick, t->cmp))
  200. t->cmp += period;
  201. diff = hpet_calculate_diff(t, cur_tick);
  202. qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock)
  203. + (int64_t)ticks_to_ns(diff));
  204. } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
  205. if (t->wrap_flag) {
  206. diff = hpet_calculate_diff(t, cur_tick);
  207. qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock)
  208. + (int64_t)ticks_to_ns(diff));
  209. t->wrap_flag = 0;
  210. }
  211. }
  212. update_irq(t);
  213. }
  214. static void hpet_set_timer(HPETTimer *t)
  215. {
  216. uint64_t diff;
  217. uint32_t wrap_diff; /* how many ticks until we wrap? */
  218. uint64_t cur_tick = hpet_get_ticks();
  219. /* whenever new timer is being set up, make sure wrap_flag is 0 */
  220. t->wrap_flag = 0;
  221. diff = hpet_calculate_diff(t, cur_tick);
  222. /* hpet spec says in one-shot 32-bit mode, generate an interrupt when
  223. * counter wraps in addition to an interrupt with comparator match.
  224. */
  225. if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) {
  226. wrap_diff = 0xffffffff - (uint32_t)cur_tick;
  227. if (wrap_diff < (uint32_t)diff) {
  228. diff = wrap_diff;
  229. t->wrap_flag = 1;
  230. }
  231. }
  232. qemu_mod_timer(t->qemu_timer, qemu_get_clock(vm_clock)
  233. + (int64_t)ticks_to_ns(diff));
  234. }
  235. static void hpet_del_timer(HPETTimer *t)
  236. {
  237. qemu_del_timer(t->qemu_timer);
  238. }
  239. #ifdef HPET_DEBUG
  240. static uint32_t hpet_ram_readb(void *opaque, target_phys_addr_t addr)
  241. {
  242. printf("qemu: hpet_read b at %" PRIx64 "\n", addr);
  243. return 0;
  244. }
  245. static uint32_t hpet_ram_readw(void *opaque, target_phys_addr_t addr)
  246. {
  247. printf("qemu: hpet_read w at %" PRIx64 "\n", addr);
  248. return 0;
  249. }
  250. #endif
  251. static uint32_t hpet_ram_readl(void *opaque, target_phys_addr_t addr)
  252. {
  253. HPETState *s = (HPETState *)opaque;
  254. uint64_t cur_tick, index;
  255. dprintf("qemu: Enter hpet_ram_readl at %" PRIx64 "\n", addr);
  256. index = addr;
  257. /*address range of all TN regs*/
  258. if (index >= 0x100 && index <= 0x3ff) {
  259. uint8_t timer_id = (addr - 0x100) / 0x20;
  260. if (timer_id > HPET_NUM_TIMERS - 1) {
  261. printf("qemu: timer id out of range\n");
  262. return 0;
  263. }
  264. HPETTimer *timer = &s->timer[timer_id];
  265. switch ((addr - 0x100) % 0x20) {
  266. case HPET_TN_CFG:
  267. return timer->config;
  268. case HPET_TN_CFG + 4: // Interrupt capabilities
  269. return timer->config >> 32;
  270. case HPET_TN_CMP: // comparator register
  271. return timer->cmp;
  272. case HPET_TN_CMP + 4:
  273. return timer->cmp >> 32;
  274. case HPET_TN_ROUTE:
  275. return timer->fsb >> 32;
  276. default:
  277. dprintf("qemu: invalid hpet_ram_readl\n");
  278. break;
  279. }
  280. } else {
  281. switch (index) {
  282. case HPET_ID:
  283. return s->capability;
  284. case HPET_PERIOD:
  285. return s->capability >> 32;
  286. case HPET_CFG:
  287. return s->config;
  288. case HPET_CFG + 4:
  289. dprintf("qemu: invalid HPET_CFG + 4 hpet_ram_readl \n");
  290. return 0;
  291. case HPET_COUNTER:
  292. if (hpet_enabled())
  293. cur_tick = hpet_get_ticks();
  294. else
  295. cur_tick = s->hpet_counter;
  296. dprintf("qemu: reading counter = %" PRIx64 "\n", cur_tick);
  297. return cur_tick;
  298. case HPET_COUNTER + 4:
  299. if (hpet_enabled())
  300. cur_tick = hpet_get_ticks();
  301. else
  302. cur_tick = s->hpet_counter;
  303. dprintf("qemu: reading counter + 4 = %" PRIx64 "\n", cur_tick);
  304. return cur_tick >> 32;
  305. case HPET_STATUS:
  306. return s->isr;
  307. default:
  308. dprintf("qemu: invalid hpet_ram_readl\n");
  309. break;
  310. }
  311. }
  312. return 0;
  313. }
  314. #ifdef HPET_DEBUG
  315. static void hpet_ram_writeb(void *opaque, target_phys_addr_t addr,
  316. uint32_t value)
  317. {
  318. printf("qemu: invalid hpet_write b at %" PRIx64 " = %#x\n",
  319. addr, value);
  320. }
  321. static void hpet_ram_writew(void *opaque, target_phys_addr_t addr,
  322. uint32_t value)
  323. {
  324. printf("qemu: invalid hpet_write w at %" PRIx64 " = %#x\n",
  325. addr, value);
  326. }
  327. #endif
  328. static void hpet_ram_writel(void *opaque, target_phys_addr_t addr,
  329. uint32_t value)
  330. {
  331. int i;
  332. HPETState *s = (HPETState *)opaque;
  333. uint64_t old_val, new_val, index;
  334. dprintf("qemu: Enter hpet_ram_writel at %" PRIx64 " = %#x\n", addr, value);
  335. index = addr;
  336. old_val = hpet_ram_readl(opaque, addr);
  337. new_val = value;
  338. /*address range of all TN regs*/
  339. if (index >= 0x100 && index <= 0x3ff) {
  340. uint8_t timer_id = (addr - 0x100) / 0x20;
  341. dprintf("qemu: hpet_ram_writel timer_id = %#x \n", timer_id);
  342. HPETTimer *timer = &s->timer[timer_id];
  343. switch ((addr - 0x100) % 0x20) {
  344. case HPET_TN_CFG:
  345. dprintf("qemu: hpet_ram_writel HPET_TN_CFG\n");
  346. timer->config = hpet_fixup_reg(new_val, old_val,
  347. HPET_TN_CFG_WRITE_MASK);
  348. if (new_val & HPET_TN_32BIT) {
  349. timer->cmp = (uint32_t)timer->cmp;
  350. timer->period = (uint32_t)timer->period;
  351. }
  352. if (new_val & HPET_TIMER_TYPE_LEVEL) {
  353. printf("qemu: level-triggered hpet not supported\n");
  354. exit (-1);
  355. }
  356. break;
  357. case HPET_TN_CFG + 4: // Interrupt capabilities
  358. dprintf("qemu: invalid HPET_TN_CFG+4 write\n");
  359. break;
  360. case HPET_TN_CMP: // comparator register
  361. dprintf("qemu: hpet_ram_writel HPET_TN_CMP \n");
  362. if (timer->config & HPET_TN_32BIT)
  363. new_val = (uint32_t)new_val;
  364. if (!timer_is_periodic(timer) ||
  365. (timer->config & HPET_TN_SETVAL))
  366. timer->cmp = (timer->cmp & 0xffffffff00000000ULL)
  367. | new_val;
  368. if (timer_is_periodic(timer)) {
  369. /*
  370. * FIXME: Clamp period to reasonable min value?
  371. * Clamp period to reasonable max value
  372. */
  373. new_val &= (timer->config & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
  374. timer->period = (timer->period & 0xffffffff00000000ULL)
  375. | new_val;
  376. }
  377. timer->config &= ~HPET_TN_SETVAL;
  378. if (hpet_enabled())
  379. hpet_set_timer(timer);
  380. break;
  381. case HPET_TN_CMP + 4: // comparator register high order
  382. dprintf("qemu: hpet_ram_writel HPET_TN_CMP + 4\n");
  383. if (!timer_is_periodic(timer) ||
  384. (timer->config & HPET_TN_SETVAL))
  385. timer->cmp = (timer->cmp & 0xffffffffULL)
  386. | new_val << 32;
  387. else {
  388. /*
  389. * FIXME: Clamp period to reasonable min value?
  390. * Clamp period to reasonable max value
  391. */
  392. new_val &= (timer->config
  393. & HPET_TN_32BIT ? ~0u : ~0ull) >> 1;
  394. timer->period = (timer->period & 0xffffffffULL)
  395. | new_val << 32;
  396. }
  397. timer->config &= ~HPET_TN_SETVAL;
  398. if (hpet_enabled())
  399. hpet_set_timer(timer);
  400. break;
  401. case HPET_TN_ROUTE + 4:
  402. dprintf("qemu: hpet_ram_writel HPET_TN_ROUTE + 4\n");
  403. break;
  404. default:
  405. dprintf("qemu: invalid hpet_ram_writel\n");
  406. break;
  407. }
  408. return;
  409. } else {
  410. switch (index) {
  411. case HPET_ID:
  412. return;
  413. case HPET_CFG:
  414. s->config = hpet_fixup_reg(new_val, old_val,
  415. HPET_CFG_WRITE_MASK);
  416. if (activating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
  417. /* Enable main counter and interrupt generation. */
  418. s->hpet_offset = ticks_to_ns(s->hpet_counter)
  419. - qemu_get_clock(vm_clock);
  420. for (i = 0; i < HPET_NUM_TIMERS; i++)
  421. if ((&s->timer[i])->cmp != ~0ULL)
  422. hpet_set_timer(&s->timer[i]);
  423. }
  424. else if (deactivating_bit(old_val, new_val, HPET_CFG_ENABLE)) {
  425. /* Halt main counter and disable interrupt generation. */
  426. s->hpet_counter = hpet_get_ticks();
  427. for (i = 0; i < HPET_NUM_TIMERS; i++)
  428. hpet_del_timer(&s->timer[i]);
  429. }
  430. /* i8254 and RTC are disabled when HPET is in legacy mode */
  431. if (activating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
  432. hpet_pit_disable();
  433. } else if (deactivating_bit(old_val, new_val, HPET_CFG_LEGACY)) {
  434. hpet_pit_enable();
  435. }
  436. break;
  437. case HPET_CFG + 4:
  438. dprintf("qemu: invalid HPET_CFG+4 write \n");
  439. break;
  440. case HPET_STATUS:
  441. /* FIXME: need to handle level-triggered interrupts */
  442. break;
  443. case HPET_COUNTER:
  444. if (hpet_enabled())
  445. printf("qemu: Writing counter while HPET enabled!\n");
  446. s->hpet_counter = (s->hpet_counter & 0xffffffff00000000ULL)
  447. | value;
  448. dprintf("qemu: HPET counter written. ctr = %#x -> %" PRIx64 "\n",
  449. value, s->hpet_counter);
  450. break;
  451. case HPET_COUNTER + 4:
  452. if (hpet_enabled())
  453. printf("qemu: Writing counter while HPET enabled!\n");
  454. s->hpet_counter = (s->hpet_counter & 0xffffffffULL)
  455. | (((uint64_t)value) << 32);
  456. dprintf("qemu: HPET counter + 4 written. ctr = %#x -> %" PRIx64 "\n",
  457. value, s->hpet_counter);
  458. break;
  459. default:
  460. dprintf("qemu: invalid hpet_ram_writel\n");
  461. break;
  462. }
  463. }
  464. }
  465. static CPUReadMemoryFunc *hpet_ram_read[] = {
  466. #ifdef HPET_DEBUG
  467. hpet_ram_readb,
  468. hpet_ram_readw,
  469. #else
  470. NULL,
  471. NULL,
  472. #endif
  473. hpet_ram_readl,
  474. };
  475. static CPUWriteMemoryFunc *hpet_ram_write[] = {
  476. #ifdef HPET_DEBUG
  477. hpet_ram_writeb,
  478. hpet_ram_writew,
  479. #else
  480. NULL,
  481. NULL,
  482. #endif
  483. hpet_ram_writel,
  484. };
  485. static void hpet_reset(void *opaque) {
  486. HPETState *s = opaque;
  487. int i;
  488. static int count = 0;
  489. for (i=0; i<HPET_NUM_TIMERS; i++) {
  490. HPETTimer *timer = &s->timer[i];
  491. hpet_del_timer(timer);
  492. timer->tn = i;
  493. timer->cmp = ~0ULL;
  494. timer->config = HPET_TN_PERIODIC_CAP | HPET_TN_SIZE_CAP;
  495. /* advertise availability of irqs 5,10,11 */
  496. timer->config |= 0x00000c20ULL << 32;
  497. timer->state = s;
  498. timer->period = 0ULL;
  499. timer->wrap_flag = 0;
  500. }
  501. s->hpet_counter = 0ULL;
  502. s->hpet_offset = 0ULL;
  503. /* 64-bit main counter; 3 timers supported; LegacyReplacementRoute. */
  504. s->capability = 0x8086a201ULL;
  505. s->capability |= ((HPET_CLK_PERIOD) << 32);
  506. s->config = 0ULL;
  507. if (count > 0)
  508. /* we don't enable pit when hpet_reset is first called (by hpet_init)
  509. * because hpet is taking over for pit here. On subsequent invocations,
  510. * hpet_reset is called due to system reset. At this point control must
  511. * be returned to pit until SW reenables hpet.
  512. */
  513. hpet_pit_enable();
  514. count = 1;
  515. }
  516. void hpet_init(qemu_irq *irq) {
  517. int i, iomemtype;
  518. HPETState *s;
  519. dprintf ("hpet_init\n");
  520. s = qemu_mallocz(sizeof(HPETState));
  521. hpet_statep = s;
  522. s->irqs = irq;
  523. for (i=0; i<HPET_NUM_TIMERS; i++) {
  524. HPETTimer *timer = &s->timer[i];
  525. timer->qemu_timer = qemu_new_timer(vm_clock, hpet_timer, timer);
  526. }
  527. hpet_reset(s);
  528. register_savevm("hpet", -1, 1, hpet_save, hpet_load, s);
  529. qemu_register_reset(hpet_reset, s);
  530. /* HPET Area */
  531. iomemtype = cpu_register_io_memory(0, hpet_ram_read,
  532. hpet_ram_write, s);
  533. cpu_register_physical_memory(HPET_BASE, 0x400, iomemtype);
  534. }