xref: /openbmc/qemu/include/hw/s390x/tod.h (revision 31cf4b97)
1 /*
2  * TOD (Time Of Day) clock
3  *
4  * Copyright 2018 Red Hat, Inc.
5  * Author(s): David Hildenbrand <david@redhat.com>
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8  * See the COPYING file in the top-level directory.
9  */
10 
11 #ifndef HW_S390_TOD_H
12 #define HW_S390_TOD_H
13 
14 #include "hw/qdev.h"
15 
16 typedef struct S390TOD {
17     uint8_t high;
18     uint64_t low;
19 } S390TOD;
20 
21 #define TYPE_S390_TOD "s390-tod"
22 #define S390_TOD(obj) OBJECT_CHECK(S390TODState, (obj), TYPE_S390_TOD)
23 #define S390_TOD_CLASS(oc) OBJECT_CLASS_CHECK(S390TODClass, (oc), \
24                                               TYPE_S390_TOD)
25 #define S390_TOD_GET_CLASS(obj) OBJECT_GET_CLASS(S390TODClass, (obj), \
26                                                  TYPE_S390_TOD)
27 #define TYPE_KVM_S390_TOD TYPE_S390_TOD "-kvm"
28 #define TYPE_QEMU_S390_TOD TYPE_S390_TOD "-qemu"
29 
30 typedef struct S390TODState {
31     /* private */
32     DeviceState parent_obj;
33 
34     /*
35      * Used by TCG to remember the time base. Used by KVM to backup the TOD
36      * while the TOD is stopped.
37      */
38     S390TOD base;
39     /* Used by KVM to remember if the TOD is stopped and base is valid. */
40     bool stopped;
41 } S390TODState;
42 
43 typedef struct S390TODClass {
44     /* private */
45     DeviceClass parent_class;
46     void (*parent_realize)(DeviceState *dev, Error **errp);
47 
48     /* public */
49     void (*get)(const S390TODState *td, S390TOD *tod, Error **errp);
50     void (*set)(S390TODState *td, const S390TOD *tod, Error **errp);
51 } S390TODClass;
52 
53 /* The value of the TOD clock for 1.1.1970. */
54 #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
55 
56 /* Converts ns to s390's clock format */
57 static inline uint64_t time2tod(uint64_t ns)
58 {
59     return (ns << 9) / 125 + (((ns & 0xff80000000000000ull) / 125) << 9);
60 }
61 
62 /* Converts s390's clock format to ns */
63 static inline uint64_t tod2time(uint64_t t)
64 {
65     return ((t >> 9) * 125) + (((t & 0x1ff) * 125) >> 9);
66 }
67 
68 void s390_init_tod(void);
69 S390TODState *s390_get_todstate(void);
70 
71 #endif
72