2020-04-17 17:54:28 +02:00
|
|
|
/*
|
|
|
|
* Hardware Clocks
|
|
|
|
*
|
|
|
|
* Copyright GreenSocs 2016-2020
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Frederic Konrad
|
|
|
|
* Damien Hedde
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2020-12-15 16:09:29 +01:00
|
|
|
#include "qemu/cutils.h"
|
2020-04-17 17:54:28 +02:00
|
|
|
#include "hw/clock.h"
|
|
|
|
#include "trace.h"
|
|
|
|
|
|
|
|
#define CLOCK_PATH(_clk) (_clk->canonical_path)
|
|
|
|
|
|
|
|
void clock_setup_canonical_path(Clock *clk)
|
|
|
|
{
|
|
|
|
g_free(clk->canonical_path);
|
|
|
|
clk->canonical_path = object_get_canonical_path(OBJECT(clk));
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:57:47 +02:00
|
|
|
Clock *clock_new(Object *parent, const char *name)
|
|
|
|
{
|
|
|
|
Object *obj;
|
|
|
|
Clock *clk;
|
|
|
|
|
|
|
|
obj = object_new(TYPE_CLOCK);
|
|
|
|
object_property_add_child(parent, name, obj);
|
|
|
|
object_unref(obj);
|
|
|
|
|
|
|
|
clk = CLOCK(obj);
|
|
|
|
clock_setup_canonical_path(clk);
|
|
|
|
|
|
|
|
return clk;
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:45:34 +01:00
|
|
|
void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque,
|
|
|
|
unsigned int events)
|
2020-04-17 17:54:28 +02:00
|
|
|
{
|
|
|
|
clk->callback = cb;
|
|
|
|
clk->callback_opaque = opaque;
|
2021-02-19 15:45:34 +01:00
|
|
|
clk->callback_events = events;
|
2020-04-17 17:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void clock_clear_callback(Clock *clk)
|
|
|
|
{
|
2021-02-19 15:45:34 +01:00
|
|
|
clock_set_callback(clk, NULL, NULL, 0);
|
2020-04-17 17:54:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:02:44 +02:00
|
|
|
bool clock_set(Clock *clk, uint64_t period)
|
2020-04-17 17:54:28 +02:00
|
|
|
{
|
2020-08-28 11:02:44 +02:00
|
|
|
if (clk->period == period) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-10 15:57:46 +02:00
|
|
|
trace_clock_set(CLOCK_PATH(clk), CLOCK_PERIOD_TO_HZ(clk->period),
|
|
|
|
CLOCK_PERIOD_TO_HZ(period));
|
2020-04-17 17:54:28 +02:00
|
|
|
clk->period = period;
|
2020-08-28 11:02:44 +02:00
|
|
|
|
|
|
|
return true;
|
2020-04-17 17:54:28 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 11:33:40 +02:00
|
|
|
static uint64_t clock_get_child_period(Clock *clk)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Return the period to be used for child clocks, which is the parent
|
2022-07-07 18:37:15 +02:00
|
|
|
* clock period adjusted for multiplier and divider effects.
|
2021-08-12 11:33:40 +02:00
|
|
|
*/
|
|
|
|
return muldiv64(clk->period, clk->multiplier, clk->divider);
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:45:34 +01:00
|
|
|
static void clock_call_callback(Clock *clk, ClockEvent event)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Call the Clock's callback for this event, if it has one and
|
|
|
|
* is interested in this event.
|
|
|
|
*/
|
|
|
|
if (clk->callback && (clk->callback_events & event)) {
|
|
|
|
clk->callback(clk->callback_opaque, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 17:54:28 +02:00
|
|
|
static void clock_propagate_period(Clock *clk, bool call_callbacks)
|
|
|
|
{
|
|
|
|
Clock *child;
|
2021-08-12 11:33:40 +02:00
|
|
|
uint64_t child_period = clock_get_child_period(clk);
|
2020-04-17 17:54:28 +02:00
|
|
|
|
|
|
|
QLIST_FOREACH(child, &clk->children, sibling) {
|
2021-08-12 11:33:40 +02:00
|
|
|
if (child->period != child_period) {
|
2021-02-19 15:45:35 +01:00
|
|
|
if (call_callbacks) {
|
|
|
|
clock_call_callback(child, ClockPreUpdate);
|
|
|
|
}
|
2021-08-12 11:33:40 +02:00
|
|
|
child->period = child_period;
|
2020-04-17 17:54:28 +02:00
|
|
|
trace_clock_update(CLOCK_PATH(child), CLOCK_PATH(clk),
|
2021-08-12 11:33:40 +02:00
|
|
|
CLOCK_PERIOD_TO_HZ(child->period),
|
2020-04-17 17:54:28 +02:00
|
|
|
call_callbacks);
|
2021-02-19 15:45:34 +01:00
|
|
|
if (call_callbacks) {
|
|
|
|
clock_call_callback(child, ClockUpdate);
|
2020-04-17 17:54:28 +02:00
|
|
|
}
|
|
|
|
clock_propagate_period(child, call_callbacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clock_propagate(Clock *clk)
|
|
|
|
{
|
|
|
|
assert(clk->source == NULL);
|
|
|
|
trace_clock_propagate(CLOCK_PATH(clk));
|
|
|
|
clock_propagate_period(clk, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void clock_set_source(Clock *clk, Clock *src)
|
|
|
|
{
|
|
|
|
/* changing clock source is not supported */
|
|
|
|
assert(!clk->source);
|
|
|
|
|
|
|
|
trace_clock_set_source(CLOCK_PATH(clk), CLOCK_PATH(src));
|
|
|
|
|
2021-08-12 11:33:40 +02:00
|
|
|
clk->period = clock_get_child_period(src);
|
2020-04-17 17:54:28 +02:00
|
|
|
QLIST_INSERT_HEAD(&src->children, clk, sibling);
|
|
|
|
clk->source = src;
|
|
|
|
clock_propagate_period(clk, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_disconnect(Clock *clk)
|
|
|
|
{
|
|
|
|
if (clk->source == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_clock_disconnect(CLOCK_PATH(clk));
|
|
|
|
|
|
|
|
clk->source = NULL;
|
|
|
|
QLIST_REMOVE(clk, sibling);
|
|
|
|
}
|
|
|
|
|
2020-12-15 16:09:29 +01:00
|
|
|
char *clock_display_freq(Clock *clk)
|
|
|
|
{
|
|
|
|
return freq_to_str(clock_get_hz(clk));
|
|
|
|
}
|
|
|
|
|
2021-08-12 11:33:40 +02:00
|
|
|
void clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
|
|
|
|
{
|
|
|
|
assert(divider != 0);
|
|
|
|
|
|
|
|
trace_clock_set_mul_div(CLOCK_PATH(clk), clk->multiplier, multiplier,
|
|
|
|
clk->divider, divider);
|
|
|
|
clk->multiplier = multiplier;
|
|
|
|
clk->divider = divider;
|
|
|
|
}
|
|
|
|
|
2020-04-17 17:54:28 +02:00
|
|
|
static void clock_initfn(Object *obj)
|
|
|
|
{
|
|
|
|
Clock *clk = CLOCK(obj);
|
|
|
|
|
2021-08-12 11:33:40 +02:00
|
|
|
clk->multiplier = 1;
|
|
|
|
clk->divider = 1;
|
|
|
|
|
2020-04-17 17:54:28 +02:00
|
|
|
QLIST_INIT(&clk->children);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_finalizefn(Object *obj)
|
|
|
|
{
|
|
|
|
Clock *clk = CLOCK(obj);
|
|
|
|
Clock *child, *next;
|
|
|
|
|
|
|
|
/* clear our list of children */
|
|
|
|
QLIST_FOREACH_SAFE(child, &clk->children, sibling, next) {
|
|
|
|
clock_disconnect(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove us from source's children list */
|
|
|
|
clock_disconnect(clk);
|
|
|
|
|
|
|
|
g_free(clk->canonical_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo clock_info = {
|
|
|
|
.name = TYPE_CLOCK,
|
|
|
|
.parent = TYPE_OBJECT,
|
|
|
|
.instance_size = sizeof(Clock),
|
|
|
|
.instance_init = clock_initfn,
|
|
|
|
.instance_finalize = clock_finalizefn,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void clock_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&clock_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(clock_register_types)
|