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 void clock_set(Clock
*clk
, uint64_t period
)
39 trace_clock_set(CLOCK_PATH(clk
), CLOCK_PERIOD_TO_NS(clk
->period
),
40 CLOCK_PERIOD_TO_NS(period
));
44 static void clock_propagate_period(Clock
*clk
, bool call_callbacks
)
48 QLIST_FOREACH(child
, &clk
->children
, sibling
) {
49 if (child
->period
!= clk
->period
) {
50 child
->period
= clk
->period
;
51 trace_clock_update(CLOCK_PATH(child
), CLOCK_PATH(clk
),
52 CLOCK_PERIOD_TO_NS(clk
->period
),
54 if (call_callbacks
&& child
->callback
) {
55 child
->callback(child
->callback_opaque
);
57 clock_propagate_period(child
, call_callbacks
);
62 void clock_propagate(Clock
*clk
)
64 assert(clk
->source
== NULL
);
65 trace_clock_propagate(CLOCK_PATH(clk
));
66 clock_propagate_period(clk
, true);
69 void clock_set_source(Clock
*clk
, Clock
*src
)
71 /* changing clock source is not supported */
74 trace_clock_set_source(CLOCK_PATH(clk
), CLOCK_PATH(src
));
76 clk
->period
= src
->period
;
77 QLIST_INSERT_HEAD(&src
->children
, clk
, sibling
);
79 clock_propagate_period(clk
, false);
82 static void clock_disconnect(Clock
*clk
)
84 if (clk
->source
== NULL
) {
88 trace_clock_disconnect(CLOCK_PATH(clk
));
91 QLIST_REMOVE(clk
, sibling
);
94 static void clock_initfn(Object
*obj
)
96 Clock
*clk
= CLOCK(obj
);
98 QLIST_INIT(&clk
->children
);
101 static void clock_finalizefn(Object
*obj
)
103 Clock
*clk
= CLOCK(obj
);
106 /* clear our list of children */
107 QLIST_FOREACH_SAFE(child
, &clk
->children
, sibling
, next
) {
108 clock_disconnect(child
);
111 /* remove us from source's children list */
112 clock_disconnect(clk
);
114 g_free(clk
->canonical_path
);
117 static const TypeInfo clock_info
= {
119 .parent
= TYPE_OBJECT
,
120 .instance_size
= sizeof(Clock
),
121 .instance_init
= clock_initfn
,
122 .instance_finalize
= clock_finalizefn
,
125 static void clock_register_types(void)
127 type_register_static(&clock_info
);
130 type_init(clock_register_types
)