mc146818rtc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * QEMU MC146818 RTC emulation
  3. *
  4. * Copyright (c) 2003-2004 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-common.h"
  26. #include "qemu/cutils.h"
  27. #include "qemu/module.h"
  28. #include "qemu/bcd.h"
  29. #include "hw/irq.h"
  30. #include "hw/qdev-properties.h"
  31. #include "qemu/timer.h"
  32. #include "sysemu/sysemu.h"
  33. #include "sysemu/replay.h"
  34. #include "sysemu/reset.h"
  35. #include "sysemu/runstate.h"
  36. #include "hw/rtc/mc146818rtc.h"
  37. #include "hw/rtc/mc146818rtc_regs.h"
  38. #include "migration/vmstate.h"
  39. #include "qapi/error.h"
  40. #include "qapi/qapi-events-misc-target.h"
  41. #include "qapi/visitor.h"
  42. #include "exec/address-spaces.h"
  43. #include "hw/rtc/mc146818rtc_regs.h"
  44. #ifdef TARGET_I386
  45. #include "qapi/qapi-commands-misc-target.h"
  46. #include "hw/i386/apic.h"
  47. #endif
  48. //#define DEBUG_CMOS
  49. //#define DEBUG_COALESCED
  50. #ifdef DEBUG_CMOS
  51. # define CMOS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  52. #else
  53. # define CMOS_DPRINTF(format, ...) do { } while (0)
  54. #endif
  55. #ifdef DEBUG_COALESCED
  56. # define DPRINTF_C(format, ...) printf(format, ## __VA_ARGS__)
  57. #else
  58. # define DPRINTF_C(format, ...) do { } while (0)
  59. #endif
  60. #define SEC_PER_MIN 60
  61. #define MIN_PER_HOUR 60
  62. #define SEC_PER_HOUR 3600
  63. #define HOUR_PER_DAY 24
  64. #define SEC_PER_DAY 86400
  65. #define RTC_REINJECT_ON_ACK_COUNT 20
  66. #define RTC_CLOCK_RATE 32768
  67. #define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
  68. static void rtc_set_time(RTCState *s);
  69. static void rtc_update_time(RTCState *s);
  70. static void rtc_set_cmos(RTCState *s, const struct tm *tm);
  71. static inline int rtc_from_bcd(RTCState *s, int a);
  72. static uint64_t get_next_alarm(RTCState *s);
  73. static inline bool rtc_running(RTCState *s)
  74. {
  75. return (!(s->cmos_data[RTC_REG_B] & REG_B_SET) &&
  76. (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20);
  77. }
  78. static uint64_t get_guest_rtc_ns(RTCState *s)
  79. {
  80. uint64_t guest_clock = qemu_clock_get_ns(rtc_clock);
  81. return s->base_rtc * NANOSECONDS_PER_SECOND +
  82. guest_clock - s->last_update + s->offset;
  83. }
  84. static void rtc_coalesced_timer_update(RTCState *s)
  85. {
  86. if (s->irq_coalesced == 0) {
  87. timer_del(s->coalesced_timer);
  88. } else {
  89. /* divide each RTC interval to 2 - 8 smaller intervals */
  90. int c = MIN(s->irq_coalesced, 7) + 1;
  91. int64_t next_clock = qemu_clock_get_ns(rtc_clock) +
  92. periodic_clock_to_ns(s->period / c);
  93. timer_mod(s->coalesced_timer, next_clock);
  94. }
  95. }
  96. static QLIST_HEAD(, RTCState) rtc_devices =
  97. QLIST_HEAD_INITIALIZER(rtc_devices);
  98. #ifdef TARGET_I386
  99. void qmp_rtc_reset_reinjection(Error **errp)
  100. {
  101. RTCState *s;
  102. QLIST_FOREACH(s, &rtc_devices, link) {
  103. s->irq_coalesced = 0;
  104. }
  105. }
  106. static bool rtc_policy_slew_deliver_irq(RTCState *s)
  107. {
  108. apic_reset_irq_delivered();
  109. qemu_irq_raise(s->irq);
  110. return apic_get_irq_delivered();
  111. }
  112. static void rtc_coalesced_timer(void *opaque)
  113. {
  114. RTCState *s = opaque;
  115. if (s->irq_coalesced != 0) {
  116. s->cmos_data[RTC_REG_C] |= 0xc0;
  117. DPRINTF_C("cmos: injecting from timer\n");
  118. if (rtc_policy_slew_deliver_irq(s)) {
  119. s->irq_coalesced--;
  120. DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
  121. s->irq_coalesced);
  122. }
  123. }
  124. rtc_coalesced_timer_update(s);
  125. }
  126. #else
  127. static bool rtc_policy_slew_deliver_irq(RTCState *s)
  128. {
  129. assert(0);
  130. return false;
  131. }
  132. #endif
  133. static uint32_t rtc_periodic_clock_ticks(RTCState *s)
  134. {
  135. int period_code;
  136. if (!(s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
  137. return 0;
  138. }
  139. period_code = s->cmos_data[RTC_REG_A] & 0x0f;
  140. return periodic_period_to_clock(period_code);
  141. }
  142. /*
  143. * handle periodic timer. @old_period indicates the periodic timer update
  144. * is just due to period adjustment.
  145. */
  146. static void
  147. periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period, bool period_change)
  148. {
  149. uint32_t period;
  150. int64_t cur_clock, next_irq_clock, lost_clock = 0;
  151. period = rtc_periodic_clock_ticks(s);
  152. s->period = period;
  153. if (!period) {
  154. s->irq_coalesced = 0;
  155. timer_del(s->periodic_timer);
  156. return;
  157. }
  158. /* compute 32 khz clock */
  159. cur_clock =
  160. muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
  161. /*
  162. * if the periodic timer's update is due to period re-configuration,
  163. * we should count the clock since last interrupt.
  164. */
  165. if (old_period && period_change) {
  166. int64_t last_periodic_clock, next_periodic_clock;
  167. next_periodic_clock = muldiv64(s->next_periodic_time,
  168. RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
  169. last_periodic_clock = next_periodic_clock - old_period;
  170. lost_clock = cur_clock - last_periodic_clock;
  171. assert(lost_clock >= 0);
  172. }
  173. /*
  174. * s->irq_coalesced can change for two reasons:
  175. *
  176. * a) if one or more periodic timer interrupts have been lost,
  177. * lost_clock will be more that a period.
  178. *
  179. * b) when the period may be reconfigured, we expect the OS to
  180. * treat delayed tick as the new period. So, when switching
  181. * from a shorter to a longer period, scale down the missing,
  182. * because the OS will treat past delayed ticks as longer
  183. * (leftovers are put back into lost_clock). When switching
  184. * to a shorter period, scale up the missing ticks since the
  185. * OS handler will treat past delayed ticks as shorter.
  186. */
  187. if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
  188. uint32_t old_irq_coalesced = s->irq_coalesced;
  189. lost_clock += old_irq_coalesced * old_period;
  190. s->irq_coalesced = lost_clock / s->period;
  191. lost_clock %= s->period;
  192. if (old_irq_coalesced != s->irq_coalesced ||
  193. old_period != s->period) {
  194. DPRINTF_C("cmos: coalesced irqs scaled from %d to %d, "
  195. "period scaled from %d to %d\n", old_irq_coalesced,
  196. s->irq_coalesced, old_period, s->period);
  197. rtc_coalesced_timer_update(s);
  198. }
  199. } else {
  200. /*
  201. * no way to compensate the interrupt if LOST_TICK_POLICY_SLEW
  202. * is not used, we should make the time progress anyway.
  203. */
  204. lost_clock = MIN(lost_clock, period);
  205. }
  206. assert(lost_clock >= 0 && lost_clock <= period);
  207. next_irq_clock = cur_clock + period - lost_clock;
  208. s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
  209. timer_mod(s->periodic_timer, s->next_periodic_time);
  210. }
  211. static void rtc_periodic_timer(void *opaque)
  212. {
  213. RTCState *s = opaque;
  214. periodic_timer_update(s, s->next_periodic_time, s->period, false);
  215. s->cmos_data[RTC_REG_C] |= REG_C_PF;
  216. if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
  217. s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
  218. if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
  219. if (s->irq_reinject_on_ack_count >= RTC_REINJECT_ON_ACK_COUNT)
  220. s->irq_reinject_on_ack_count = 0;
  221. if (!rtc_policy_slew_deliver_irq(s)) {
  222. s->irq_coalesced++;
  223. rtc_coalesced_timer_update(s);
  224. DPRINTF_C("cmos: coalesced irqs increased to %d\n",
  225. s->irq_coalesced);
  226. }
  227. } else
  228. qemu_irq_raise(s->irq);
  229. }
  230. }
  231. /* handle update-ended timer */
  232. static void check_update_timer(RTCState *s)
  233. {
  234. uint64_t next_update_time;
  235. uint64_t guest_nsec;
  236. int next_alarm_sec;
  237. /* From the data sheet: "Holding the dividers in reset prevents
  238. * interrupts from operating, while setting the SET bit allows"
  239. * them to occur.
  240. */
  241. if ((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) {
  242. assert((s->cmos_data[RTC_REG_A] & REG_A_UIP) == 0);
  243. timer_del(s->update_timer);
  244. return;
  245. }
  246. guest_nsec = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
  247. next_update_time = qemu_clock_get_ns(rtc_clock)
  248. + NANOSECONDS_PER_SECOND - guest_nsec;
  249. /* Compute time of next alarm. One second is already accounted
  250. * for in next_update_time.
  251. */
  252. next_alarm_sec = get_next_alarm(s);
  253. s->next_alarm_time = next_update_time +
  254. (next_alarm_sec - 1) * NANOSECONDS_PER_SECOND;
  255. /* If update_in_progress latched the UIP bit, we must keep the timer
  256. * programmed to the next second, so that UIP is cleared. Otherwise,
  257. * if UF is already set, we might be able to optimize.
  258. */
  259. if (!(s->cmos_data[RTC_REG_A] & REG_A_UIP) &&
  260. (s->cmos_data[RTC_REG_C] & REG_C_UF)) {
  261. /* If AF cannot change (i.e. either it is set already, or
  262. * SET=1 and then the time is not updated), nothing to do.
  263. */
  264. if ((s->cmos_data[RTC_REG_B] & REG_B_SET) ||
  265. (s->cmos_data[RTC_REG_C] & REG_C_AF)) {
  266. timer_del(s->update_timer);
  267. return;
  268. }
  269. /* UF is set, but AF is clear. Program the timer to target
  270. * the alarm time. */
  271. next_update_time = s->next_alarm_time;
  272. }
  273. if (next_update_time != timer_expire_time_ns(s->update_timer)) {
  274. timer_mod(s->update_timer, next_update_time);
  275. }
  276. }
  277. static inline uint8_t convert_hour(RTCState *s, uint8_t hour)
  278. {
  279. if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
  280. hour %= 12;
  281. if (s->cmos_data[RTC_HOURS] & 0x80) {
  282. hour += 12;
  283. }
  284. }
  285. return hour;
  286. }
  287. static uint64_t get_next_alarm(RTCState *s)
  288. {
  289. int32_t alarm_sec, alarm_min, alarm_hour, cur_hour, cur_min, cur_sec;
  290. int32_t hour, min, sec;
  291. rtc_update_time(s);
  292. alarm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS_ALARM]);
  293. alarm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES_ALARM]);
  294. alarm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS_ALARM]);
  295. alarm_hour = alarm_hour == -1 ? -1 : convert_hour(s, alarm_hour);
  296. cur_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
  297. cur_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
  298. cur_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS]);
  299. cur_hour = convert_hour(s, cur_hour);
  300. if (alarm_hour == -1) {
  301. alarm_hour = cur_hour;
  302. if (alarm_min == -1) {
  303. alarm_min = cur_min;
  304. if (alarm_sec == -1) {
  305. alarm_sec = cur_sec + 1;
  306. } else if (cur_sec > alarm_sec) {
  307. alarm_min++;
  308. }
  309. } else if (cur_min == alarm_min) {
  310. if (alarm_sec == -1) {
  311. alarm_sec = cur_sec + 1;
  312. } else {
  313. if (cur_sec > alarm_sec) {
  314. alarm_hour++;
  315. }
  316. }
  317. if (alarm_sec == SEC_PER_MIN) {
  318. /* wrap to next hour, minutes is not in don't care mode */
  319. alarm_sec = 0;
  320. alarm_hour++;
  321. }
  322. } else if (cur_min > alarm_min) {
  323. alarm_hour++;
  324. }
  325. } else if (cur_hour == alarm_hour) {
  326. if (alarm_min == -1) {
  327. alarm_min = cur_min;
  328. if (alarm_sec == -1) {
  329. alarm_sec = cur_sec + 1;
  330. } else if (cur_sec > alarm_sec) {
  331. alarm_min++;
  332. }
  333. if (alarm_sec == SEC_PER_MIN) {
  334. alarm_sec = 0;
  335. alarm_min++;
  336. }
  337. /* wrap to next day, hour is not in don't care mode */
  338. alarm_min %= MIN_PER_HOUR;
  339. } else if (cur_min == alarm_min) {
  340. if (alarm_sec == -1) {
  341. alarm_sec = cur_sec + 1;
  342. }
  343. /* wrap to next day, hours+minutes not in don't care mode */
  344. alarm_sec %= SEC_PER_MIN;
  345. }
  346. }
  347. /* values that are still don't care fire at the next min/sec */
  348. if (alarm_min == -1) {
  349. alarm_min = 0;
  350. }
  351. if (alarm_sec == -1) {
  352. alarm_sec = 0;
  353. }
  354. /* keep values in range */
  355. if (alarm_sec == SEC_PER_MIN) {
  356. alarm_sec = 0;
  357. alarm_min++;
  358. }
  359. if (alarm_min == MIN_PER_HOUR) {
  360. alarm_min = 0;
  361. alarm_hour++;
  362. }
  363. alarm_hour %= HOUR_PER_DAY;
  364. hour = alarm_hour - cur_hour;
  365. min = hour * MIN_PER_HOUR + alarm_min - cur_min;
  366. sec = min * SEC_PER_MIN + alarm_sec - cur_sec;
  367. return sec <= 0 ? sec + SEC_PER_DAY : sec;
  368. }
  369. static void rtc_update_timer(void *opaque)
  370. {
  371. RTCState *s = opaque;
  372. int32_t irqs = REG_C_UF;
  373. int32_t new_irqs;
  374. assert((s->cmos_data[RTC_REG_A] & 0x60) != 0x60);
  375. /* UIP might have been latched, update time and clear it. */
  376. rtc_update_time(s);
  377. s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
  378. if (qemu_clock_get_ns(rtc_clock) >= s->next_alarm_time) {
  379. irqs |= REG_C_AF;
  380. if (s->cmos_data[RTC_REG_B] & REG_B_AIE) {
  381. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_RTC, NULL);
  382. }
  383. }
  384. new_irqs = irqs & ~s->cmos_data[RTC_REG_C];
  385. s->cmos_data[RTC_REG_C] |= irqs;
  386. if ((new_irqs & s->cmos_data[RTC_REG_B]) != 0) {
  387. s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
  388. qemu_irq_raise(s->irq);
  389. }
  390. check_update_timer(s);
  391. }
  392. static void cmos_ioport_write(void *opaque, hwaddr addr,
  393. uint64_t data, unsigned size)
  394. {
  395. RTCState *s = opaque;
  396. uint32_t old_period;
  397. bool update_periodic_timer;
  398. if ((addr & 1) == 0) {
  399. s->cmos_index = data & 0x7f;
  400. } else {
  401. CMOS_DPRINTF("cmos: write index=0x%02x val=0x%02" PRIx64 "\n",
  402. s->cmos_index, data);
  403. switch(s->cmos_index) {
  404. case RTC_SECONDS_ALARM:
  405. case RTC_MINUTES_ALARM:
  406. case RTC_HOURS_ALARM:
  407. s->cmos_data[s->cmos_index] = data;
  408. check_update_timer(s);
  409. break;
  410. case RTC_IBM_PS2_CENTURY_BYTE:
  411. s->cmos_index = RTC_CENTURY;
  412. /* fall through */
  413. case RTC_CENTURY:
  414. case RTC_SECONDS:
  415. case RTC_MINUTES:
  416. case RTC_HOURS:
  417. case RTC_DAY_OF_WEEK:
  418. case RTC_DAY_OF_MONTH:
  419. case RTC_MONTH:
  420. case RTC_YEAR:
  421. s->cmos_data[s->cmos_index] = data;
  422. /* if in set mode, do not update the time */
  423. if (rtc_running(s)) {
  424. rtc_set_time(s);
  425. check_update_timer(s);
  426. }
  427. break;
  428. case RTC_REG_A:
  429. update_periodic_timer = (s->cmos_data[RTC_REG_A] ^ data) & 0x0f;
  430. old_period = rtc_periodic_clock_ticks(s);
  431. if ((data & 0x60) == 0x60) {
  432. if (rtc_running(s)) {
  433. rtc_update_time(s);
  434. }
  435. /* What happens to UIP when divider reset is enabled is
  436. * unclear from the datasheet. Shouldn't matter much
  437. * though.
  438. */
  439. s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
  440. } else if (((s->cmos_data[RTC_REG_A] & 0x60) == 0x60) &&
  441. (data & 0x70) <= 0x20) {
  442. /* when the divider reset is removed, the first update cycle
  443. * begins one-half second later*/
  444. if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
  445. s->offset = 500000000;
  446. rtc_set_time(s);
  447. }
  448. s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
  449. }
  450. /* UIP bit is read only */
  451. s->cmos_data[RTC_REG_A] = (data & ~REG_A_UIP) |
  452. (s->cmos_data[RTC_REG_A] & REG_A_UIP);
  453. if (update_periodic_timer) {
  454. periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
  455. old_period, true);
  456. }
  457. check_update_timer(s);
  458. break;
  459. case RTC_REG_B:
  460. update_periodic_timer = (s->cmos_data[RTC_REG_B] ^ data)
  461. & REG_B_PIE;
  462. old_period = rtc_periodic_clock_ticks(s);
  463. if (data & REG_B_SET) {
  464. /* update cmos to when the rtc was stopping */
  465. if (rtc_running(s)) {
  466. rtc_update_time(s);
  467. }
  468. /* set mode: reset UIP mode */
  469. s->cmos_data[RTC_REG_A] &= ~REG_A_UIP;
  470. data &= ~REG_B_UIE;
  471. } else {
  472. /* if disabling set mode, update the time */
  473. if ((s->cmos_data[RTC_REG_B] & REG_B_SET) &&
  474. (s->cmos_data[RTC_REG_A] & 0x70) <= 0x20) {
  475. s->offset = get_guest_rtc_ns(s) % NANOSECONDS_PER_SECOND;
  476. rtc_set_time(s);
  477. }
  478. }
  479. /* if an interrupt flag is already set when the interrupt
  480. * becomes enabled, raise an interrupt immediately. */
  481. if (data & s->cmos_data[RTC_REG_C] & REG_C_MASK) {
  482. s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
  483. qemu_irq_raise(s->irq);
  484. } else {
  485. s->cmos_data[RTC_REG_C] &= ~REG_C_IRQF;
  486. qemu_irq_lower(s->irq);
  487. }
  488. s->cmos_data[RTC_REG_B] = data;
  489. if (update_periodic_timer) {
  490. periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
  491. old_period, true);
  492. }
  493. check_update_timer(s);
  494. break;
  495. case RTC_REG_C:
  496. case RTC_REG_D:
  497. /* cannot write to them */
  498. break;
  499. default:
  500. s->cmos_data[s->cmos_index] = data;
  501. break;
  502. }
  503. }
  504. }
  505. static inline int rtc_to_bcd(RTCState *s, int a)
  506. {
  507. if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
  508. return a;
  509. } else {
  510. return ((a / 10) << 4) | (a % 10);
  511. }
  512. }
  513. static inline int rtc_from_bcd(RTCState *s, int a)
  514. {
  515. if ((a & 0xc0) == 0xc0) {
  516. return -1;
  517. }
  518. if (s->cmos_data[RTC_REG_B] & REG_B_DM) {
  519. return a;
  520. } else {
  521. return ((a >> 4) * 10) + (a & 0x0f);
  522. }
  523. }
  524. static void rtc_get_time(RTCState *s, struct tm *tm)
  525. {
  526. tm->tm_sec = rtc_from_bcd(s, s->cmos_data[RTC_SECONDS]);
  527. tm->tm_min = rtc_from_bcd(s, s->cmos_data[RTC_MINUTES]);
  528. tm->tm_hour = rtc_from_bcd(s, s->cmos_data[RTC_HOURS] & 0x7f);
  529. if (!(s->cmos_data[RTC_REG_B] & REG_B_24H)) {
  530. tm->tm_hour %= 12;
  531. if (s->cmos_data[RTC_HOURS] & 0x80) {
  532. tm->tm_hour += 12;
  533. }
  534. }
  535. tm->tm_wday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_WEEK]) - 1;
  536. tm->tm_mday = rtc_from_bcd(s, s->cmos_data[RTC_DAY_OF_MONTH]);
  537. tm->tm_mon = rtc_from_bcd(s, s->cmos_data[RTC_MONTH]) - 1;
  538. tm->tm_year =
  539. rtc_from_bcd(s, s->cmos_data[RTC_YEAR]) + s->base_year +
  540. rtc_from_bcd(s, s->cmos_data[RTC_CENTURY]) * 100 - 1900;
  541. }
  542. static void rtc_set_time(RTCState *s)
  543. {
  544. struct tm tm;
  545. rtc_get_time(s, &tm);
  546. s->base_rtc = mktimegm(&tm);
  547. s->last_update = qemu_clock_get_ns(rtc_clock);
  548. qapi_event_send_rtc_change(qemu_timedate_diff(&tm));
  549. }
  550. static void rtc_set_cmos(RTCState *s, const struct tm *tm)
  551. {
  552. int year;
  553. s->cmos_data[RTC_SECONDS] = rtc_to_bcd(s, tm->tm_sec);
  554. s->cmos_data[RTC_MINUTES] = rtc_to_bcd(s, tm->tm_min);
  555. if (s->cmos_data[RTC_REG_B] & REG_B_24H) {
  556. /* 24 hour format */
  557. s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, tm->tm_hour);
  558. } else {
  559. /* 12 hour format */
  560. int h = (tm->tm_hour % 12) ? tm->tm_hour % 12 : 12;
  561. s->cmos_data[RTC_HOURS] = rtc_to_bcd(s, h);
  562. if (tm->tm_hour >= 12)
  563. s->cmos_data[RTC_HOURS] |= 0x80;
  564. }
  565. s->cmos_data[RTC_DAY_OF_WEEK] = rtc_to_bcd(s, tm->tm_wday + 1);
  566. s->cmos_data[RTC_DAY_OF_MONTH] = rtc_to_bcd(s, tm->tm_mday);
  567. s->cmos_data[RTC_MONTH] = rtc_to_bcd(s, tm->tm_mon + 1);
  568. year = tm->tm_year + 1900 - s->base_year;
  569. s->cmos_data[RTC_YEAR] = rtc_to_bcd(s, year % 100);
  570. s->cmos_data[RTC_CENTURY] = rtc_to_bcd(s, year / 100);
  571. }
  572. static void rtc_update_time(RTCState *s)
  573. {
  574. struct tm ret;
  575. time_t guest_sec;
  576. int64_t guest_nsec;
  577. guest_nsec = get_guest_rtc_ns(s);
  578. guest_sec = guest_nsec / NANOSECONDS_PER_SECOND;
  579. gmtime_r(&guest_sec, &ret);
  580. /* Is SET flag of Register B disabled? */
  581. if ((s->cmos_data[RTC_REG_B] & REG_B_SET) == 0) {
  582. rtc_set_cmos(s, &ret);
  583. }
  584. }
  585. static int update_in_progress(RTCState *s)
  586. {
  587. int64_t guest_nsec;
  588. if (!rtc_running(s)) {
  589. return 0;
  590. }
  591. if (timer_pending(s->update_timer)) {
  592. int64_t next_update_time = timer_expire_time_ns(s->update_timer);
  593. /* Latch UIP until the timer expires. */
  594. if (qemu_clock_get_ns(rtc_clock) >=
  595. (next_update_time - UIP_HOLD_LENGTH)) {
  596. s->cmos_data[RTC_REG_A] |= REG_A_UIP;
  597. return 1;
  598. }
  599. }
  600. guest_nsec = get_guest_rtc_ns(s);
  601. /* UIP bit will be set at last 244us of every second. */
  602. if ((guest_nsec % NANOSECONDS_PER_SECOND) >=
  603. (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)) {
  604. return 1;
  605. }
  606. return 0;
  607. }
  608. static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
  609. unsigned size)
  610. {
  611. RTCState *s = opaque;
  612. int ret;
  613. if ((addr & 1) == 0) {
  614. return 0xff;
  615. } else {
  616. switch(s->cmos_index) {
  617. case RTC_IBM_PS2_CENTURY_BYTE:
  618. s->cmos_index = RTC_CENTURY;
  619. /* fall through */
  620. case RTC_CENTURY:
  621. case RTC_SECONDS:
  622. case RTC_MINUTES:
  623. case RTC_HOURS:
  624. case RTC_DAY_OF_WEEK:
  625. case RTC_DAY_OF_MONTH:
  626. case RTC_MONTH:
  627. case RTC_YEAR:
  628. /* if not in set mode, calibrate cmos before
  629. * reading*/
  630. if (rtc_running(s)) {
  631. rtc_update_time(s);
  632. }
  633. ret = s->cmos_data[s->cmos_index];
  634. break;
  635. case RTC_REG_A:
  636. ret = s->cmos_data[s->cmos_index];
  637. if (update_in_progress(s)) {
  638. ret |= REG_A_UIP;
  639. }
  640. break;
  641. case RTC_REG_C:
  642. ret = s->cmos_data[s->cmos_index];
  643. qemu_irq_lower(s->irq);
  644. s->cmos_data[RTC_REG_C] = 0x00;
  645. if (ret & (REG_C_UF | REG_C_AF)) {
  646. check_update_timer(s);
  647. }
  648. if(s->irq_coalesced &&
  649. (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
  650. s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
  651. s->irq_reinject_on_ack_count++;
  652. s->cmos_data[RTC_REG_C] |= REG_C_IRQF | REG_C_PF;
  653. DPRINTF_C("cmos: injecting on ack\n");
  654. if (rtc_policy_slew_deliver_irq(s)) {
  655. s->irq_coalesced--;
  656. DPRINTF_C("cmos: coalesced irqs decreased to %d\n",
  657. s->irq_coalesced);
  658. }
  659. }
  660. break;
  661. default:
  662. ret = s->cmos_data[s->cmos_index];
  663. break;
  664. }
  665. CMOS_DPRINTF("cmos: read index=0x%02x val=0x%02x\n",
  666. s->cmos_index, ret);
  667. return ret;
  668. }
  669. }
  670. void rtc_set_memory(ISADevice *dev, int addr, int val)
  671. {
  672. RTCState *s = MC146818_RTC(dev);
  673. if (addr >= 0 && addr <= 127)
  674. s->cmos_data[addr] = val;
  675. }
  676. int rtc_get_memory(ISADevice *dev, int addr)
  677. {
  678. RTCState *s = MC146818_RTC(dev);
  679. assert(addr >= 0 && addr <= 127);
  680. return s->cmos_data[addr];
  681. }
  682. static void rtc_set_date_from_host(ISADevice *dev)
  683. {
  684. RTCState *s = MC146818_RTC(dev);
  685. struct tm tm;
  686. qemu_get_timedate(&tm, 0);
  687. s->base_rtc = mktimegm(&tm);
  688. s->last_update = qemu_clock_get_ns(rtc_clock);
  689. s->offset = 0;
  690. /* set the CMOS date */
  691. rtc_set_cmos(s, &tm);
  692. }
  693. static int rtc_pre_save(void *opaque)
  694. {
  695. RTCState *s = opaque;
  696. rtc_update_time(s);
  697. return 0;
  698. }
  699. static int rtc_post_load(void *opaque, int version_id)
  700. {
  701. RTCState *s = opaque;
  702. if (version_id <= 2 || rtc_clock == QEMU_CLOCK_REALTIME) {
  703. rtc_set_time(s);
  704. s->offset = 0;
  705. check_update_timer(s);
  706. }
  707. s->period = rtc_periodic_clock_ticks(s);
  708. /* The periodic timer is deterministic in record/replay mode,
  709. * so there is no need to update it after loading the vmstate.
  710. * Reading RTC here would misalign record and replay.
  711. */
  712. if (replay_mode == REPLAY_MODE_NONE) {
  713. uint64_t now = qemu_clock_get_ns(rtc_clock);
  714. if (now < s->next_periodic_time ||
  715. now > (s->next_periodic_time + get_max_clock_jump())) {
  716. periodic_timer_update(s, qemu_clock_get_ns(rtc_clock), s->period, false);
  717. }
  718. }
  719. if (version_id >= 2) {
  720. if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
  721. rtc_coalesced_timer_update(s);
  722. }
  723. }
  724. return 0;
  725. }
  726. static bool rtc_irq_reinject_on_ack_count_needed(void *opaque)
  727. {
  728. RTCState *s = (RTCState *)opaque;
  729. return s->irq_reinject_on_ack_count != 0;
  730. }
  731. static const VMStateDescription vmstate_rtc_irq_reinject_on_ack_count = {
  732. .name = "mc146818rtc/irq_reinject_on_ack_count",
  733. .version_id = 1,
  734. .minimum_version_id = 1,
  735. .needed = rtc_irq_reinject_on_ack_count_needed,
  736. .fields = (VMStateField[]) {
  737. VMSTATE_UINT16(irq_reinject_on_ack_count, RTCState),
  738. VMSTATE_END_OF_LIST()
  739. }
  740. };
  741. static const VMStateDescription vmstate_rtc = {
  742. .name = "mc146818rtc",
  743. .version_id = 3,
  744. .minimum_version_id = 1,
  745. .pre_save = rtc_pre_save,
  746. .post_load = rtc_post_load,
  747. .fields = (VMStateField[]) {
  748. VMSTATE_BUFFER(cmos_data, RTCState),
  749. VMSTATE_UINT8(cmos_index, RTCState),
  750. VMSTATE_UNUSED(7*4),
  751. VMSTATE_TIMER_PTR(periodic_timer, RTCState),
  752. VMSTATE_INT64(next_periodic_time, RTCState),
  753. VMSTATE_UNUSED(3*8),
  754. VMSTATE_UINT32_V(irq_coalesced, RTCState, 2),
  755. VMSTATE_UINT32_V(period, RTCState, 2),
  756. VMSTATE_UINT64_V(base_rtc, RTCState, 3),
  757. VMSTATE_UINT64_V(last_update, RTCState, 3),
  758. VMSTATE_INT64_V(offset, RTCState, 3),
  759. VMSTATE_TIMER_PTR_V(update_timer, RTCState, 3),
  760. VMSTATE_UINT64_V(next_alarm_time, RTCState, 3),
  761. VMSTATE_END_OF_LIST()
  762. },
  763. .subsections = (const VMStateDescription*[]) {
  764. &vmstate_rtc_irq_reinject_on_ack_count,
  765. NULL
  766. }
  767. };
  768. /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
  769. BIOS will read it and start S3 resume at POST Entry */
  770. static void rtc_notify_suspend(Notifier *notifier, void *data)
  771. {
  772. RTCState *s = container_of(notifier, RTCState, suspend_notifier);
  773. rtc_set_memory(ISA_DEVICE(s), 0xF, 0xFE);
  774. }
  775. static void rtc_reset(void *opaque)
  776. {
  777. RTCState *s = opaque;
  778. s->cmos_data[RTC_REG_B] &= ~(REG_B_PIE | REG_B_AIE | REG_B_SQWE);
  779. s->cmos_data[RTC_REG_C] &= ~(REG_C_UF | REG_C_IRQF | REG_C_PF | REG_C_AF);
  780. check_update_timer(s);
  781. qemu_irq_lower(s->irq);
  782. if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
  783. s->irq_coalesced = 0;
  784. s->irq_reinject_on_ack_count = 0;
  785. }
  786. }
  787. static const MemoryRegionOps cmos_ops = {
  788. .read = cmos_ioport_read,
  789. .write = cmos_ioport_write,
  790. .impl = {
  791. .min_access_size = 1,
  792. .max_access_size = 1,
  793. },
  794. .endianness = DEVICE_LITTLE_ENDIAN,
  795. };
  796. static void rtc_get_date(Object *obj, struct tm *current_tm, Error **errp)
  797. {
  798. RTCState *s = MC146818_RTC(obj);
  799. rtc_update_time(s);
  800. rtc_get_time(s, current_tm);
  801. }
  802. static void rtc_realizefn(DeviceState *dev, Error **errp)
  803. {
  804. ISADevice *isadev = ISA_DEVICE(dev);
  805. RTCState *s = MC146818_RTC(dev);
  806. int base = 0x70;
  807. s->cmos_data[RTC_REG_A] = 0x26;
  808. s->cmos_data[RTC_REG_B] = 0x02;
  809. s->cmos_data[RTC_REG_C] = 0x00;
  810. s->cmos_data[RTC_REG_D] = 0x80;
  811. /* This is for historical reasons. The default base year qdev property
  812. * was set to 2000 for most machine types before the century byte was
  813. * implemented.
  814. *
  815. * This if statement means that the century byte will be always 0
  816. * (at least until 2079...) for base_year = 1980, but will be set
  817. * correctly for base_year = 2000.
  818. */
  819. if (s->base_year == 2000) {
  820. s->base_year = 0;
  821. }
  822. rtc_set_date_from_host(isadev);
  823. switch (s->lost_tick_policy) {
  824. #ifdef TARGET_I386
  825. case LOST_TICK_POLICY_SLEW:
  826. s->coalesced_timer =
  827. timer_new_ns(rtc_clock, rtc_coalesced_timer, s);
  828. break;
  829. #endif
  830. case LOST_TICK_POLICY_DISCARD:
  831. break;
  832. default:
  833. error_setg(errp, "Invalid lost tick policy.");
  834. return;
  835. }
  836. s->periodic_timer = timer_new_ns(rtc_clock, rtc_periodic_timer, s);
  837. s->update_timer = timer_new_ns(rtc_clock, rtc_update_timer, s);
  838. check_update_timer(s);
  839. s->suspend_notifier.notify = rtc_notify_suspend;
  840. qemu_register_suspend_notifier(&s->suspend_notifier);
  841. memory_region_init_io(&s->io, OBJECT(s), &cmos_ops, s, "rtc", 2);
  842. isa_register_ioport(isadev, &s->io, base);
  843. /* register rtc 0x70 port for coalesced_pio */
  844. memory_region_set_flush_coalesced(&s->io);
  845. memory_region_init_io(&s->coalesced_io, OBJECT(s), &cmos_ops,
  846. s, "rtc-index", 1);
  847. memory_region_add_subregion(&s->io, 0, &s->coalesced_io);
  848. memory_region_add_coalescing(&s->coalesced_io, 0, 1);
  849. qdev_set_legacy_instance_id(dev, base, 3);
  850. qemu_register_reset(rtc_reset, s);
  851. object_property_add_tm(OBJECT(s), "date", rtc_get_date, NULL);
  852. qdev_init_gpio_out(dev, &s->irq, 1);
  853. QLIST_INSERT_HEAD(&rtc_devices, s, link);
  854. }
  855. ISADevice *mc146818_rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq)
  856. {
  857. DeviceState *dev;
  858. ISADevice *isadev;
  859. isadev = isa_create(bus, TYPE_MC146818_RTC);
  860. dev = DEVICE(isadev);
  861. qdev_prop_set_int32(dev, "base_year", base_year);
  862. qdev_init_nofail(dev);
  863. if (intercept_irq) {
  864. qdev_connect_gpio_out(dev, 0, intercept_irq);
  865. } else {
  866. isa_connect_gpio_out(isadev, 0, RTC_ISA_IRQ);
  867. }
  868. object_property_add_alias(qdev_get_machine(), "rtc-time", OBJECT(isadev),
  869. "date", NULL);
  870. return isadev;
  871. }
  872. static Property mc146818rtc_properties[] = {
  873. DEFINE_PROP_INT32("base_year", RTCState, base_year, 1980),
  874. DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", RTCState,
  875. lost_tick_policy, LOST_TICK_POLICY_DISCARD),
  876. DEFINE_PROP_END_OF_LIST(),
  877. };
  878. static void rtc_resetdev(DeviceState *d)
  879. {
  880. RTCState *s = MC146818_RTC(d);
  881. /* Reason: VM do suspend self will set 0xfe
  882. * Reset any values other than 0xfe(Guest suspend case) */
  883. if (s->cmos_data[0x0f] != 0xfe) {
  884. s->cmos_data[0x0f] = 0x00;
  885. }
  886. }
  887. static void rtc_class_initfn(ObjectClass *klass, void *data)
  888. {
  889. DeviceClass *dc = DEVICE_CLASS(klass);
  890. dc->realize = rtc_realizefn;
  891. dc->reset = rtc_resetdev;
  892. dc->vmsd = &vmstate_rtc;
  893. dc->props = mc146818rtc_properties;
  894. }
  895. static const TypeInfo mc146818rtc_info = {
  896. .name = TYPE_MC146818_RTC,
  897. .parent = TYPE_ISA_DEVICE,
  898. .instance_size = sizeof(RTCState),
  899. .class_init = rtc_class_initfn,
  900. };
  901. static void mc146818rtc_register_types(void)
  902. {
  903. type_register_static(&mc146818rtc_info);
  904. }
  905. type_init(mc146818rtc_register_types)