mc146818rtc.c 29 KB

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