xref: /openbmc/qemu/hw/s390x/tod-tcg.c (revision 477c3b934a47adf7de285863f59d6e4503dd1a6d)
185f1b67dSCho, Yu-Chen /*
285f1b67dSCho, Yu-Chen  * TOD (Time Of Day) clock - TCG implementation
385f1b67dSCho, Yu-Chen  *
485f1b67dSCho, Yu-Chen  * Copyright 2018 Red Hat, Inc.
585f1b67dSCho, Yu-Chen  * Author(s): David Hildenbrand <david@redhat.com>
685f1b67dSCho, Yu-Chen  *
785f1b67dSCho, Yu-Chen  * This work is licensed under the terms of the GNU GPL, version 2 or later.
885f1b67dSCho, Yu-Chen  * See the COPYING file in the top-level directory.
985f1b67dSCho, Yu-Chen  */
1085f1b67dSCho, Yu-Chen 
1185f1b67dSCho, Yu-Chen #include "qemu/osdep.h"
1285f1b67dSCho, Yu-Chen #include "qapi/error.h"
1385f1b67dSCho, Yu-Chen #include "hw/s390x/tod.h"
1485f1b67dSCho, Yu-Chen #include "qemu/timer.h"
1585f1b67dSCho, Yu-Chen #include "qemu/cutils.h"
1685f1b67dSCho, Yu-Chen #include "qemu/module.h"
1785f1b67dSCho, Yu-Chen #include "cpu.h"
18c9274b6bSCho, Yu-Chen #include "tcg/tcg_s390x.h"
192f93d8b0SPeter Maydell #include "sysemu/rtc.h"
2085f1b67dSCho, Yu-Chen 
qemu_s390_tod_get(const S390TODState * td,S390TOD * tod,Error ** errp)2185f1b67dSCho, Yu-Chen static void qemu_s390_tod_get(const S390TODState *td, S390TOD *tod,
2285f1b67dSCho, Yu-Chen                               Error **errp)
2385f1b67dSCho, Yu-Chen {
2485f1b67dSCho, Yu-Chen     *tod = td->base;
2585f1b67dSCho, Yu-Chen 
2685f1b67dSCho, Yu-Chen     tod->low += time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
2785f1b67dSCho, Yu-Chen     if (tod->low < td->base.low) {
2885f1b67dSCho, Yu-Chen         tod->high++;
2985f1b67dSCho, Yu-Chen     }
3085f1b67dSCho, Yu-Chen }
3185f1b67dSCho, Yu-Chen 
qemu_s390_tod_set(S390TODState * td,const S390TOD * tod,Error ** errp)3285f1b67dSCho, Yu-Chen static void qemu_s390_tod_set(S390TODState *td, const S390TOD *tod,
3385f1b67dSCho, Yu-Chen                               Error **errp)
3485f1b67dSCho, Yu-Chen {
3585f1b67dSCho, Yu-Chen     CPUState *cpu;
3685f1b67dSCho, Yu-Chen 
3785f1b67dSCho, Yu-Chen     td->base = *tod;
3885f1b67dSCho, Yu-Chen 
3985f1b67dSCho, Yu-Chen     td->base.low -= time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
4085f1b67dSCho, Yu-Chen     if (td->base.low > tod->low) {
4185f1b67dSCho, Yu-Chen         td->base.high--;
4285f1b67dSCho, Yu-Chen     }
4385f1b67dSCho, Yu-Chen 
4485f1b67dSCho, Yu-Chen     /*
4585f1b67dSCho, Yu-Chen      * The TOD has been changed and we have to recalculate the CKC values
4685f1b67dSCho, Yu-Chen      * for all CPUs. We do this asynchronously, as "SET CLOCK should be
4785f1b67dSCho, Yu-Chen      * issued only while all other activity on all CPUs .. has been
4885f1b67dSCho, Yu-Chen      * suspended".
4985f1b67dSCho, Yu-Chen      */
5085f1b67dSCho, Yu-Chen     CPU_FOREACH(cpu) {
5185f1b67dSCho, Yu-Chen         async_run_on_cpu(cpu, tcg_s390_tod_updated, RUN_ON_CPU_NULL);
5285f1b67dSCho, Yu-Chen     }
5385f1b67dSCho, Yu-Chen }
5485f1b67dSCho, Yu-Chen 
qemu_s390_tod_class_init(ObjectClass * oc,void * data)5585f1b67dSCho, Yu-Chen static void qemu_s390_tod_class_init(ObjectClass *oc, void *data)
5685f1b67dSCho, Yu-Chen {
5785f1b67dSCho, Yu-Chen     S390TODClass *tdc = S390_TOD_CLASS(oc);
5885f1b67dSCho, Yu-Chen 
5985f1b67dSCho, Yu-Chen     tdc->get = qemu_s390_tod_get;
6085f1b67dSCho, Yu-Chen     tdc->set = qemu_s390_tod_set;
6185f1b67dSCho, Yu-Chen }
6285f1b67dSCho, Yu-Chen 
qemu_s390_tod_init(Object * obj)6385f1b67dSCho, Yu-Chen static void qemu_s390_tod_init(Object *obj)
6485f1b67dSCho, Yu-Chen {
6585f1b67dSCho, Yu-Chen     S390TODState *td = S390_TOD(obj);
6685f1b67dSCho, Yu-Chen     struct tm tm;
6785f1b67dSCho, Yu-Chen 
6885f1b67dSCho, Yu-Chen     qemu_get_timedate(&tm, 0);
6985f1b67dSCho, Yu-Chen     td->base.high = 0;
7085f1b67dSCho, Yu-Chen     td->base.low = TOD_UNIX_EPOCH + (time2tod(mktimegm(&tm)) * 1000000000ULL);
7185f1b67dSCho, Yu-Chen     if (td->base.low < TOD_UNIX_EPOCH) {
7285f1b67dSCho, Yu-Chen         td->base.high += 1;
7385f1b67dSCho, Yu-Chen     }
7485f1b67dSCho, Yu-Chen }
7585f1b67dSCho, Yu-Chen 
76*5e78c98bSBernhard Beschow static const TypeInfo qemu_s390_tod_info = {
7785f1b67dSCho, Yu-Chen     .name = TYPE_QEMU_S390_TOD,
7885f1b67dSCho, Yu-Chen     .parent = TYPE_S390_TOD,
7985f1b67dSCho, Yu-Chen     .instance_size = sizeof(S390TODState),
8085f1b67dSCho, Yu-Chen     .instance_init = qemu_s390_tod_init,
8185f1b67dSCho, Yu-Chen     .class_init = qemu_s390_tod_class_init,
8285f1b67dSCho, Yu-Chen     .class_size = sizeof(S390TODClass),
8385f1b67dSCho, Yu-Chen };
8485f1b67dSCho, Yu-Chen 
register_types(void)8585f1b67dSCho, Yu-Chen static void register_types(void)
8685f1b67dSCho, Yu-Chen {
8785f1b67dSCho, Yu-Chen     type_register_static(&qemu_s390_tod_info);
8885f1b67dSCho, Yu-Chen }
8985f1b67dSCho, Yu-Chen type_init(register_types);
90