mc146818rtc.c 33 KB

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