xref: /openbmc/qemu/hw/core/clock.c (revision 7d87775f)
1 /*
2  * Hardware Clocks
3  *
4  * Copyright GreenSocs 2016-2020
5  *
6  * Authors:
7  *  Frederic Konrad
8  *  Damien Hedde
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu/cutils.h"
16 #include "qapi/visitor.h"
17 #include "sysemu/qtest.h"
18 #include "hw/clock.h"
19 #include "trace.h"
20 
21 #define CLOCK_PATH(_clk) (_clk->canonical_path)
22 
23 void clock_setup_canonical_path(Clock *clk)
24 {
25     g_free(clk->canonical_path);
26     clk->canonical_path = object_get_canonical_path(OBJECT(clk));
27 }
28 
29 Clock *clock_new(Object *parent, const char *name)
30 {
31     Object *obj;
32     Clock *clk;
33 
34     obj = object_new(TYPE_CLOCK);
35     object_property_add_child(parent, name, obj);
36     object_unref(obj);
37 
38     clk = CLOCK(obj);
39     clock_setup_canonical_path(clk);
40 
41     return clk;
42 }
43 
44 void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque,
45                         unsigned int events)
46 {
47     clk->callback = cb;
48     clk->callback_opaque = opaque;
49     clk->callback_events = events;
50 }
51 
52 void clock_clear_callback(Clock *clk)
53 {
54     clock_set_callback(clk, NULL, NULL, 0);
55 }
56 
57 bool clock_set(Clock *clk, uint64_t period)
58 {
59     if (clk->period == period) {
60         return false;
61     }
62     trace_clock_set(CLOCK_PATH(clk), CLOCK_PERIOD_TO_HZ(clk->period),
63                     CLOCK_PERIOD_TO_HZ(period));
64     clk->period = period;
65 
66     return true;
67 }
68 
69 static uint64_t clock_get_child_period(Clock *clk)
70 {
71     /*
72      * Return the period to be used for child clocks, which is the parent
73      * clock period adjusted for multiplier and divider effects.
74      */
75     return muldiv64(clk->period, clk->multiplier, clk->divider);
76 }
77 
78 static void clock_call_callback(Clock *clk, ClockEvent event)
79 {
80     /*
81      * Call the Clock's callback for this event, if it has one and
82      * is interested in this event.
83      */
84     if (clk->callback && (clk->callback_events & event)) {
85         clk->callback(clk->callback_opaque, event);
86     }
87 }
88 
89 static void clock_propagate_period(Clock *clk, bool call_callbacks)
90 {
91     Clock *child;
92     uint64_t child_period = clock_get_child_period(clk);
93 
94     QLIST_FOREACH(child, &clk->children, sibling) {
95         if (child->period != child_period) {
96             if (call_callbacks) {
97                 clock_call_callback(child, ClockPreUpdate);
98             }
99             child->period = child_period;
100             trace_clock_update(CLOCK_PATH(child), CLOCK_PATH(clk),
101                                CLOCK_PERIOD_TO_HZ(child->period),
102                                call_callbacks);
103             if (call_callbacks) {
104                 clock_call_callback(child, ClockUpdate);
105             }
106             clock_propagate_period(child, call_callbacks);
107         }
108     }
109 }
110 
111 void clock_propagate(Clock *clk)
112 {
113     trace_clock_propagate(CLOCK_PATH(clk));
114     clock_propagate_period(clk, true);
115 }
116 
117 void clock_set_source(Clock *clk, Clock *src)
118 {
119     /* changing clock source is not supported */
120     assert(!clk->source);
121 
122     trace_clock_set_source(CLOCK_PATH(clk), CLOCK_PATH(src));
123 
124     clk->period = clock_get_child_period(src);
125     QLIST_INSERT_HEAD(&src->children, clk, sibling);
126     clk->source = src;
127     clock_propagate_period(clk, false);
128 }
129 
130 static void clock_disconnect(Clock *clk)
131 {
132     if (clk->source == NULL) {
133         return;
134     }
135 
136     trace_clock_disconnect(CLOCK_PATH(clk));
137 
138     clk->source = NULL;
139     QLIST_REMOVE(clk, sibling);
140 }
141 
142 char *clock_display_freq(Clock *clk)
143 {
144     return freq_to_str(clock_get_hz(clk));
145 }
146 
147 bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
148 {
149     assert(divider != 0);
150 
151     if (clk->multiplier == multiplier && clk->divider == divider) {
152         return false;
153     }
154 
155     trace_clock_set_mul_div(CLOCK_PATH(clk), clk->multiplier, multiplier,
156                             clk->divider, divider);
157     clk->multiplier = multiplier;
158     clk->divider = divider;
159 
160     return true;
161 }
162 
163 static void clock_period_prop_get(Object *obj, Visitor *v, const char *name,
164                                 void *opaque, Error **errp)
165 {
166     Clock *clk = CLOCK(obj);
167     uint64_t period = clock_get(clk);
168     visit_type_uint64(v, name, &period, errp);
169 }
170 
171 
172 static void clock_initfn(Object *obj)
173 {
174     Clock *clk = CLOCK(obj);
175 
176     clk->multiplier = 1;
177     clk->divider = 1;
178 
179     QLIST_INIT(&clk->children);
180 
181     if (qtest_enabled()) {
182         object_property_add(obj, "qtest-clock-period", "uint64",
183                             clock_period_prop_get, NULL, NULL, NULL);
184     }
185 }
186 
187 static void clock_finalizefn(Object *obj)
188 {
189     Clock *clk = CLOCK(obj);
190     Clock *child, *next;
191 
192     /* clear our list of children */
193     QLIST_FOREACH_SAFE(child, &clk->children, sibling, next) {
194         clock_disconnect(child);
195     }
196 
197     /* remove us from source's children list */
198     clock_disconnect(clk);
199 
200     g_free(clk->canonical_path);
201 }
202 
203 static const TypeInfo clock_info = {
204     .name              = TYPE_CLOCK,
205     .parent            = TYPE_OBJECT,
206     .instance_size     = sizeof(Clock),
207     .instance_init     = clock_initfn,
208     .instance_finalize = clock_finalizefn,
209 };
210 
211 static void clock_register_types(void)
212 {
213     type_register_static(&clock_info);
214 }
215 
216 type_init(clock_register_types)
217