14cba075eSPeter Maydell /* 24cba075eSPeter Maydell * Hardware Clocks 34cba075eSPeter Maydell * 44cba075eSPeter Maydell * Copyright GreenSocs 2016-2020 54cba075eSPeter Maydell * 64cba075eSPeter Maydell * Authors: 74cba075eSPeter Maydell * Frederic Konrad 84cba075eSPeter Maydell * Damien Hedde 94cba075eSPeter Maydell * 104cba075eSPeter Maydell * This work is licensed under the terms of the GNU GPL, version 2 or later. 114cba075eSPeter Maydell * See the COPYING file in the top-level directory. 124cba075eSPeter Maydell */ 134cba075eSPeter Maydell 144cba075eSPeter Maydell #include "qemu/osdep.h" 15b7cd9c1eSPeter Maydell #include "qemu/cutils.h" 164cba075eSPeter Maydell #include "hw/clock.h" 174cba075eSPeter Maydell #include "trace.h" 184cba075eSPeter Maydell 194cba075eSPeter Maydell #define CLOCK_PATH(_clk) (_clk->canonical_path) 204cba075eSPeter Maydell 214cba075eSPeter Maydell void clock_setup_canonical_path(Clock *clk) 224cba075eSPeter Maydell { 234cba075eSPeter Maydell g_free(clk->canonical_path); 244cba075eSPeter Maydell clk->canonical_path = object_get_canonical_path(OBJECT(clk)); 254cba075eSPeter Maydell } 264cba075eSPeter Maydell 275ebc6648SLuc Michel Clock *clock_new(Object *parent, const char *name) 285ebc6648SLuc Michel { 295ebc6648SLuc Michel Object *obj; 305ebc6648SLuc Michel Clock *clk; 315ebc6648SLuc Michel 325ebc6648SLuc Michel obj = object_new(TYPE_CLOCK); 335ebc6648SLuc Michel object_property_add_child(parent, name, obj); 345ebc6648SLuc Michel object_unref(obj); 355ebc6648SLuc Michel 365ebc6648SLuc Michel clk = CLOCK(obj); 375ebc6648SLuc Michel clock_setup_canonical_path(clk); 385ebc6648SLuc Michel 395ebc6648SLuc Michel return clk; 405ebc6648SLuc Michel } 415ebc6648SLuc Michel 42*5ee0abedSPeter Maydell void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque, 43*5ee0abedSPeter Maydell unsigned int events) 444cba075eSPeter Maydell { 454cba075eSPeter Maydell clk->callback = cb; 464cba075eSPeter Maydell clk->callback_opaque = opaque; 47*5ee0abedSPeter Maydell clk->callback_events = events; 484cba075eSPeter Maydell } 494cba075eSPeter Maydell 504cba075eSPeter Maydell void clock_clear_callback(Clock *clk) 514cba075eSPeter Maydell { 52*5ee0abedSPeter Maydell clock_set_callback(clk, NULL, NULL, 0); 534cba075eSPeter Maydell } 544cba075eSPeter Maydell 5515aa2876SPhilippe Mathieu-Daudé bool clock_set(Clock *clk, uint64_t period) 564cba075eSPeter Maydell { 5715aa2876SPhilippe Mathieu-Daudé if (clk->period == period) { 5815aa2876SPhilippe Mathieu-Daudé return false; 5915aa2876SPhilippe Mathieu-Daudé } 60a6414d3bSLuc Michel trace_clock_set(CLOCK_PATH(clk), CLOCK_PERIOD_TO_HZ(clk->period), 61a6414d3bSLuc Michel CLOCK_PERIOD_TO_HZ(period)); 624cba075eSPeter Maydell clk->period = period; 6315aa2876SPhilippe Mathieu-Daudé 6415aa2876SPhilippe Mathieu-Daudé return true; 654cba075eSPeter Maydell } 664cba075eSPeter Maydell 67*5ee0abedSPeter Maydell static void clock_call_callback(Clock *clk, ClockEvent event) 68*5ee0abedSPeter Maydell { 69*5ee0abedSPeter Maydell /* 70*5ee0abedSPeter Maydell * Call the Clock's callback for this event, if it has one and 71*5ee0abedSPeter Maydell * is interested in this event. 72*5ee0abedSPeter Maydell */ 73*5ee0abedSPeter Maydell if (clk->callback && (clk->callback_events & event)) { 74*5ee0abedSPeter Maydell clk->callback(clk->callback_opaque, event); 75*5ee0abedSPeter Maydell } 76*5ee0abedSPeter Maydell } 77*5ee0abedSPeter Maydell 784cba075eSPeter Maydell static void clock_propagate_period(Clock *clk, bool call_callbacks) 794cba075eSPeter Maydell { 804cba075eSPeter Maydell Clock *child; 814cba075eSPeter Maydell 824cba075eSPeter Maydell QLIST_FOREACH(child, &clk->children, sibling) { 834cba075eSPeter Maydell if (child->period != clk->period) { 844cba075eSPeter Maydell child->period = clk->period; 854cba075eSPeter Maydell trace_clock_update(CLOCK_PATH(child), CLOCK_PATH(clk), 86a6414d3bSLuc Michel CLOCK_PERIOD_TO_HZ(clk->period), 874cba075eSPeter Maydell call_callbacks); 88*5ee0abedSPeter Maydell if (call_callbacks) { 89*5ee0abedSPeter Maydell clock_call_callback(child, ClockUpdate); 904cba075eSPeter Maydell } 914cba075eSPeter Maydell clock_propagate_period(child, call_callbacks); 924cba075eSPeter Maydell } 934cba075eSPeter Maydell } 944cba075eSPeter Maydell } 954cba075eSPeter Maydell 964cba075eSPeter Maydell void clock_propagate(Clock *clk) 974cba075eSPeter Maydell { 984cba075eSPeter Maydell assert(clk->source == NULL); 994cba075eSPeter Maydell trace_clock_propagate(CLOCK_PATH(clk)); 1004cba075eSPeter Maydell clock_propagate_period(clk, true); 1014cba075eSPeter Maydell } 1024cba075eSPeter Maydell 1034cba075eSPeter Maydell void clock_set_source(Clock *clk, Clock *src) 1044cba075eSPeter Maydell { 1054cba075eSPeter Maydell /* changing clock source is not supported */ 1064cba075eSPeter Maydell assert(!clk->source); 1074cba075eSPeter Maydell 1084cba075eSPeter Maydell trace_clock_set_source(CLOCK_PATH(clk), CLOCK_PATH(src)); 1094cba075eSPeter Maydell 1104cba075eSPeter Maydell clk->period = src->period; 1114cba075eSPeter Maydell QLIST_INSERT_HEAD(&src->children, clk, sibling); 1124cba075eSPeter Maydell clk->source = src; 1134cba075eSPeter Maydell clock_propagate_period(clk, false); 1144cba075eSPeter Maydell } 1154cba075eSPeter Maydell 1164cba075eSPeter Maydell static void clock_disconnect(Clock *clk) 1174cba075eSPeter Maydell { 1184cba075eSPeter Maydell if (clk->source == NULL) { 1194cba075eSPeter Maydell return; 1204cba075eSPeter Maydell } 1214cba075eSPeter Maydell 1224cba075eSPeter Maydell trace_clock_disconnect(CLOCK_PATH(clk)); 1234cba075eSPeter Maydell 1244cba075eSPeter Maydell clk->source = NULL; 1254cba075eSPeter Maydell QLIST_REMOVE(clk, sibling); 1264cba075eSPeter Maydell } 1274cba075eSPeter Maydell 128b7cd9c1eSPeter Maydell char *clock_display_freq(Clock *clk) 129b7cd9c1eSPeter Maydell { 130b7cd9c1eSPeter Maydell return freq_to_str(clock_get_hz(clk)); 131b7cd9c1eSPeter Maydell } 132b7cd9c1eSPeter Maydell 1334cba075eSPeter Maydell static void clock_initfn(Object *obj) 1344cba075eSPeter Maydell { 1354cba075eSPeter Maydell Clock *clk = CLOCK(obj); 1364cba075eSPeter Maydell 1374cba075eSPeter Maydell QLIST_INIT(&clk->children); 1384cba075eSPeter Maydell } 1394cba075eSPeter Maydell 1404cba075eSPeter Maydell static void clock_finalizefn(Object *obj) 1414cba075eSPeter Maydell { 1424cba075eSPeter Maydell Clock *clk = CLOCK(obj); 1434cba075eSPeter Maydell Clock *child, *next; 1444cba075eSPeter Maydell 1454cba075eSPeter Maydell /* clear our list of children */ 1464cba075eSPeter Maydell QLIST_FOREACH_SAFE(child, &clk->children, sibling, next) { 1474cba075eSPeter Maydell clock_disconnect(child); 1484cba075eSPeter Maydell } 1494cba075eSPeter Maydell 1504cba075eSPeter Maydell /* remove us from source's children list */ 1514cba075eSPeter Maydell clock_disconnect(clk); 1524cba075eSPeter Maydell 1534cba075eSPeter Maydell g_free(clk->canonical_path); 1544cba075eSPeter Maydell } 1554cba075eSPeter Maydell 1564cba075eSPeter Maydell static const TypeInfo clock_info = { 1574cba075eSPeter Maydell .name = TYPE_CLOCK, 1584cba075eSPeter Maydell .parent = TYPE_OBJECT, 1594cba075eSPeter Maydell .instance_size = sizeof(Clock), 1604cba075eSPeter Maydell .instance_init = clock_initfn, 1614cba075eSPeter Maydell .instance_finalize = clock_finalizefn, 1624cba075eSPeter Maydell }; 1634cba075eSPeter Maydell 1644cba075eSPeter Maydell static void clock_register_types(void) 1654cba075eSPeter Maydell { 1664cba075eSPeter Maydell type_register_static(&clock_info); 1674cba075eSPeter Maydell } 1684cba075eSPeter Maydell 1694cba075eSPeter Maydell type_init(clock_register_types) 170