mc146818rtc.c 33 KB

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