4 * Copyright GreenSocs 2016-2020
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"
18 #define CLOCK_PATH(_clk) (_clk->canonical_path)
20 void clock_setup_canonical_path(Clock *clk)
22 g_free(clk->canonical_path);
23 clk->canonical_path = object_get_canonical_path(OBJECT(clk));
26 void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque)
29 clk->callback_opaque = opaque;
32 void clock_clear_callback(Clock *clk)
34 clock_set_callback(clk, NULL, NULL);
37 bool clock_set(Clock *clk, uint64_t period)
39 if (clk->period == period) {
42 trace_clock_set(CLOCK_PATH(clk), CLOCK_PERIOD_TO_NS(clk->period),
43 CLOCK_PERIOD_TO_NS(period));
49 static void clock_propagate_period(Clock *clk, bool call_callbacks)
53 QLIST_FOREACH(child, &clk->children, sibling) {
54 if (child->period != clk->period) {
55 child->period = clk->period;
56 trace_clock_update(CLOCK_PATH(child), CLOCK_PATH(clk),
57 CLOCK_PERIOD_TO_NS(clk->period),
59 if (call_callbacks && child->callback) {
60 child->callback(child->callback_opaque);
62 clock_propagate_period(child, call_callbacks);
67 void clock_propagate(Clock *clk)
69 assert(clk->source == NULL);
70 trace_clock_propagate(CLOCK_PATH(clk));
71 clock_propagate_period(clk, true);
74 void clock_set_source(Clock *clk, Clock *src)
76 /* changing clock source is not supported */
79 trace_clock_set_source(CLOCK_PATH(clk), CLOCK_PATH(src));
81 clk->period = src->period;
82 QLIST_INSERT_HEAD(&src->children, clk, sibling);
84 clock_propagate_period(clk, false);
87 static void clock_disconnect(Clock *clk)
89 if (clk->source == NULL) {
93 trace_clock_disconnect(CLOCK_PATH(clk));
96 QLIST_REMOVE(clk, sibling);
99 static void clock_initfn(Object *obj)
101 Clock *clk = CLOCK(obj);
103 QLIST_INIT(&clk->children);
106 static void clock_finalizefn(Object *obj)
108 Clock *clk = CLOCK(obj);
111 /* clear our list of children */
112 QLIST_FOREACH_SAFE(child, &clk->children, sibling, next) {
113 clock_disconnect(child);
116 /* remove us from source's children list */
117 clock_disconnect(clk);
119 g_free(clk->canonical_path);
122 static const TypeInfo clock_info = {
124 .parent = TYPE_OBJECT,
125 .instance_size = sizeof(Clock),
126 .instance_init = clock_initfn,
127 .instance_finalize = clock_finalizefn,
130 static void clock_register_types(void)
132 type_register_static(&clock_info);
135 type_init(clock_register_types)