Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / core / clock.c
blobfc5a99683f895a3166b25add22de849f7adbedcb
1 /*
2 * Hardware Clocks
4 * Copyright GreenSocs 2016-2020
6 * Authors:
7 * Frederic Konrad
8 * Damien Hedde
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.
14 #include "qemu/osdep.h"
15 #include "qemu/cutils.h"
16 #include "hw/clock.h"
17 #include "trace.h"
19 #define CLOCK_PATH(_clk) (_clk->canonical_path)
21 void clock_setup_canonical_path(Clock *clk)
23 g_free(clk->canonical_path);
24 clk->canonical_path = object_get_canonical_path(OBJECT(clk));
27 Clock *clock_new(Object *parent, const char *name)
29 Object *obj;
30 Clock *clk;
32 obj = object_new(TYPE_CLOCK);
33 object_property_add_child(parent, name, obj);
34 object_unref(obj);
36 clk = CLOCK(obj);
37 clock_setup_canonical_path(clk);
39 return clk;
42 void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque,
43 unsigned int events)
45 clk->callback = cb;
46 clk->callback_opaque = opaque;
47 clk->callback_events = events;
50 void clock_clear_callback(Clock *clk)
52 clock_set_callback(clk, NULL, NULL, 0);
55 bool clock_set(Clock *clk, uint64_t period)
57 if (clk->period == period) {
58 return false;
60 trace_clock_set(CLOCK_PATH(clk), CLOCK_PERIOD_TO_HZ(clk->period),
61 CLOCK_PERIOD_TO_HZ(period));
62 clk->period = period;
64 return true;
67 static void clock_call_callback(Clock *clk, ClockEvent event)
70 * Call the Clock's callback for this event, if it has one and
71 * is interested in this event.
73 if (clk->callback && (clk->callback_events & event)) {
74 clk->callback(clk->callback_opaque, event);
78 static void clock_propagate_period(Clock *clk, bool call_callbacks)
80 Clock *child;
82 QLIST_FOREACH(child, &clk->children, sibling) {
83 if (child->period != clk->period) {
84 if (call_callbacks) {
85 clock_call_callback(child, ClockPreUpdate);
87 child->period = clk->period;
88 trace_clock_update(CLOCK_PATH(child), CLOCK_PATH(clk),
89 CLOCK_PERIOD_TO_HZ(clk->period),
90 call_callbacks);
91 if (call_callbacks) {
92 clock_call_callback(child, ClockUpdate);
94 clock_propagate_period(child, call_callbacks);
99 void clock_propagate(Clock *clk)
101 assert(clk->source == NULL);
102 trace_clock_propagate(CLOCK_PATH(clk));
103 clock_propagate_period(clk, true);
106 void clock_set_source(Clock *clk, Clock *src)
108 /* changing clock source is not supported */
109 assert(!clk->source);
111 trace_clock_set_source(CLOCK_PATH(clk), CLOCK_PATH(src));
113 clk->period = src->period;
114 QLIST_INSERT_HEAD(&src->children, clk, sibling);
115 clk->source = src;
116 clock_propagate_period(clk, false);
119 static void clock_disconnect(Clock *clk)
121 if (clk->source == NULL) {
122 return;
125 trace_clock_disconnect(CLOCK_PATH(clk));
127 clk->source = NULL;
128 QLIST_REMOVE(clk, sibling);
131 char *clock_display_freq(Clock *clk)
133 return freq_to_str(clock_get_hz(clk));
136 static void clock_initfn(Object *obj)
138 Clock *clk = CLOCK(obj);
140 QLIST_INIT(&clk->children);
143 static void clock_finalizefn(Object *obj)
145 Clock *clk = CLOCK(obj);
146 Clock *child, *next;
148 /* clear our list of children */
149 QLIST_FOREACH_SAFE(child, &clk->children, sibling, next) {
150 clock_disconnect(child);
153 /* remove us from source's children list */
154 clock_disconnect(clk);
156 g_free(clk->canonical_path);
159 static const TypeInfo clock_info = {
160 .name = TYPE_CLOCK,
161 .parent = TYPE_OBJECT,
162 .instance_size = sizeof(Clock),
163 .instance_init = clock_initfn,
164 .instance_finalize = clock_finalizefn,
167 static void clock_register_types(void)
169 type_register_static(&clock_info);
172 type_init(clock_register_types)