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"
15 #include "qemu/cutils.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
)
32 obj
= object_new(TYPE_CLOCK
);
33 object_property_add_child(parent
, name
, obj
);
37 clock_setup_canonical_path(clk
);
42 void clock_set_callback(Clock
*clk
, ClockCallback
*cb
, void *opaque
)
45 clk
->callback_opaque
= opaque
;
48 void clock_clear_callback(Clock
*clk
)
50 clock_set_callback(clk
, NULL
, NULL
);
53 bool clock_set(Clock
*clk
, uint64_t period
)
55 if (clk
->period
== period
) {
58 trace_clock_set(CLOCK_PATH(clk
), CLOCK_PERIOD_TO_HZ(clk
->period
),
59 CLOCK_PERIOD_TO_HZ(period
));
65 static void clock_propagate_period(Clock
*clk
, bool call_callbacks
)
69 QLIST_FOREACH(child
, &clk
->children
, sibling
) {
70 if (child
->period
!= clk
->period
) {
71 child
->period
= clk
->period
;
72 trace_clock_update(CLOCK_PATH(child
), CLOCK_PATH(clk
),
73 CLOCK_PERIOD_TO_HZ(clk
->period
),
75 if (call_callbacks
&& child
->callback
) {
76 child
->callback(child
->callback_opaque
);
78 clock_propagate_period(child
, call_callbacks
);
83 void clock_propagate(Clock
*clk
)
85 assert(clk
->source
== NULL
);
86 trace_clock_propagate(CLOCK_PATH(clk
));
87 clock_propagate_period(clk
, true);
90 void clock_set_source(Clock
*clk
, Clock
*src
)
92 /* changing clock source is not supported */
95 trace_clock_set_source(CLOCK_PATH(clk
), CLOCK_PATH(src
));
97 clk
->period
= src
->period
;
98 QLIST_INSERT_HEAD(&src
->children
, clk
, sibling
);
100 clock_propagate_period(clk
, false);
103 static void clock_disconnect(Clock
*clk
)
105 if (clk
->source
== NULL
) {
109 trace_clock_disconnect(CLOCK_PATH(clk
));
112 QLIST_REMOVE(clk
, sibling
);
115 char *clock_display_freq(Clock
*clk
)
117 return freq_to_str(clock_get_hz(clk
));
120 static void clock_initfn(Object
*obj
)
122 Clock
*clk
= CLOCK(obj
);
124 QLIST_INIT(&clk
->children
);
127 static void clock_finalizefn(Object
*obj
)
129 Clock
*clk
= CLOCK(obj
);
132 /* clear our list of children */
133 QLIST_FOREACH_SAFE(child
, &clk
->children
, sibling
, next
) {
134 clock_disconnect(child
);
137 /* remove us from source's children list */
138 clock_disconnect(clk
);
140 g_free(clk
->canonical_path
);
143 static const TypeInfo clock_info
= {
145 .parent
= TYPE_OBJECT
,
146 .instance_size
= sizeof(Clock
),
147 .instance_init
= clock_initfn
,
148 .instance_finalize
= clock_finalizefn
,
151 static void clock_register_types(void)
153 type_register_static(&clock_info
);
156 type_init(clock_register_types
)