hw/scsi/scsi-disk: Disallow block sizes smaller than 512 [CVE-2023-42467]
[qemu/ar7.git] / hw / core / clock.c
blobd82e44cd1aa8d39dc4aa71ba2c061e9373494714
1 /*
2 * Hardware Clocks
4 * Copyright GreenSocs 2016-2020
6 * Authors:
7 * Frederic Konrad
8 * Damien Hedde
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"
16 #include "hw/clock.h"
17 #include "trace.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)
29 Object *obj;
30 Clock *clk;
32 obj = object_new(TYPE_CLOCK);
33 object_property_add_child(parent, name, obj);
34 object_unref(obj);
36 clk = CLOCK(obj);
37 clock_setup_canonical_path(clk);
39 return clk;
42 void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque,
43 unsigned int events)
45 clk->callback = cb;
46 clk->callback_opaque = opaque;
47 clk->callback_events = events;
50 void clock_clear_callback(Clock *clk)
52 clock_set_callback(clk, NULL, NULL, 0);
55 bool clock_set(Clock *clk, uint64_t period)
57 if (clk->period == period) {
58 return false;
60 trace_clock_set(CLOCK_PATH(clk), CLOCK_PERIOD_TO_HZ(clk->period),
61 CLOCK_PERIOD_TO_HZ(period));
62 clk->period = period;
64 return true;
67 static uint64_t clock_get_child_period(Clock *clk)
70 * Return the period to be used for child clocks, which is the parent
71 * clock period adjusted for multiplier and divider effects.
73 return muldiv64(clk->period, clk->multiplier, clk->divider);
76 static void clock_call_callback(Clock *clk, ClockEvent event)
79 * Call the Clock's callback for this event, if it has one and
80 * is interested in this event.
82 if (clk->callback && (clk->callback_events & event)) {
83 clk->callback(clk->callback_opaque, event);
87 static void clock_propagate_period(Clock *clk, bool call_callbacks)
89 Clock *child;
90 uint64_t child_period = clock_get_child_period(clk);
92 QLIST_FOREACH(child, &clk->children, sibling) {
93 if (child->period != child_period) {
94 if (call_callbacks) {
95 clock_call_callback(child, ClockPreUpdate);
97 child->period = child_period;
98 trace_clock_update(CLOCK_PATH(child), CLOCK_PATH(clk),
99 CLOCK_PERIOD_TO_HZ(child->period),
100 call_callbacks);
101 if (call_callbacks) {
102 clock_call_callback(child, ClockUpdate);
104 clock_propagate_period(child, call_callbacks);
109 void clock_propagate(Clock *clk)
111 assert(clk->source == NULL);
112 trace_clock_propagate(CLOCK_PATH(clk));
113 clock_propagate_period(clk, true);
116 void clock_set_source(Clock *clk, Clock *src)
118 /* changing clock source is not supported */
119 assert(!clk->source);
121 trace_clock_set_source(CLOCK_PATH(clk), CLOCK_PATH(src));
123 clk->period = clock_get_child_period(src);
124 QLIST_INSERT_HEAD(&src->children, clk, sibling);
125 clk->source = src;
126 clock_propagate_period(clk, false);
129 static void clock_disconnect(Clock *clk)
131 if (clk->source == NULL) {
132 return;
135 trace_clock_disconnect(CLOCK_PATH(clk));
137 clk->source = NULL;
138 QLIST_REMOVE(clk, sibling);
141 char *clock_display_freq(Clock *clk)
143 return freq_to_str(clock_get_hz(clk));
146 void clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
148 assert(divider != 0);
150 trace_clock_set_mul_div(CLOCK_PATH(clk), clk->multiplier, multiplier,
151 clk->divider, divider);
152 clk->multiplier = multiplier;
153 clk->divider = divider;
156 static void clock_initfn(Object *obj)
158 Clock *clk = CLOCK(obj);
160 clk->multiplier = 1;
161 clk->divider = 1;
163 QLIST_INIT(&clk->children);
166 static void clock_finalizefn(Object *obj)
168 Clock *clk = CLOCK(obj);
169 Clock *child, *next;
171 /* clear our list of children */
172 QLIST_FOREACH_SAFE(child, &clk->children, sibling, next) {
173 clock_disconnect(child);
176 /* remove us from source's children list */
177 clock_disconnect(clk);
179 g_free(clk->canonical_path);
182 static const TypeInfo clock_info = {
183 .name = TYPE_CLOCK,
184 .parent = TYPE_OBJECT,
185 .instance_size = sizeof(Clock),
186 .instance_init = clock_initfn,
187 .instance_finalize = clock_finalizefn,
190 static void clock_register_types(void)
192 type_register_static(&clock_info);
195 type_init(clock_register_types)