xref: /openbmc/qemu/hw/core/qdev-clock.c (revision dd205025)
1 /*
2  * Device's clock input and output
3  *
4  * Copyright GreenSocs 2016-2020
5  *
6  * Authors:
7  *  Frederic Konrad
8  *  Damien Hedde
9  *
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.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "hw/qdev-clock.h"
16 #include "hw/qdev-core.h"
17 #include "qapi/error.h"
18 
19 /*
20  * qdev_init_clocklist:
21  * Add a new clock in a device
22  */
23 static NamedClockList *qdev_init_clocklist(DeviceState *dev, const char *name,
24                                            bool output, Clock *clk)
25 {
26     NamedClockList *ncl;
27 
28     /*
29      * Clock must be added before realize() so that we can compute the
30      * clock's canonical path during device_realize().
31      */
32     assert(!dev->realized);
33 
34     /*
35      * The ncl structure is freed by qdev_finalize_clocklist() which will
36      * be called during @dev's device_finalize().
37      */
38     ncl = g_new0(NamedClockList, 1);
39     ncl->name = g_strdup(name);
40     ncl->output = output;
41     ncl->alias = (clk != NULL);
42 
43     /*
44      * Trying to create a clock whose name clashes with some other
45      * clock or property is a bug in the caller and we will abort().
46      */
47     if (clk == NULL) {
48         clk = CLOCK(object_new(TYPE_CLOCK));
49         object_property_add_child(OBJECT(dev), name, OBJECT(clk));
50         if (output) {
51             /*
52              * Remove object_new()'s initial reference.
53              * Note that for inputs, the reference created by object_new()
54              * will be deleted in qdev_finalize_clocklist().
55              */
56             object_unref(OBJECT(clk));
57         }
58     } else {
59         object_property_add_link(OBJECT(dev), name,
60                                  object_get_typename(OBJECT(clk)),
61                                  (Object **) &ncl->clock,
62                                  NULL, OBJ_PROP_LINK_STRONG);
63     }
64 
65     ncl->clock = clk;
66 
67     QLIST_INSERT_HEAD(&dev->clocks, ncl, node);
68     return ncl;
69 }
70 
71 void qdev_finalize_clocklist(DeviceState *dev)
72 {
73     /* called by @dev's device_finalize() */
74     NamedClockList *ncl, *ncl_next;
75 
76     QLIST_FOREACH_SAFE(ncl, &dev->clocks, node, ncl_next) {
77         QLIST_REMOVE(ncl, node);
78         if (!ncl->output && !ncl->alias) {
79             /*
80              * We kept a reference on the input clock to ensure it lives up to
81              * this point so we can safely remove the callback.
82              * It avoids having a callback to a deleted object if ncl->clock
83              * is still referenced somewhere else (eg: by a clock output).
84              */
85             clock_clear_callback(ncl->clock);
86             object_unref(OBJECT(ncl->clock));
87         }
88         g_free(ncl->name);
89         g_free(ncl);
90     }
91 }
92 
93 Clock *qdev_init_clock_out(DeviceState *dev, const char *name)
94 {
95     NamedClockList *ncl;
96 
97     assert(name);
98 
99     ncl = qdev_init_clocklist(dev, name, true, NULL);
100 
101     return ncl->clock;
102 }
103 
104 Clock *qdev_init_clock_in(DeviceState *dev, const char *name,
105                             ClockCallback *callback, void *opaque)
106 {
107     NamedClockList *ncl;
108 
109     assert(name);
110 
111     ncl = qdev_init_clocklist(dev, name, false, NULL);
112 
113     if (callback) {
114         clock_set_callback(ncl->clock, callback, opaque);
115     }
116     return ncl->clock;
117 }
118 
119 void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks)
120 {
121     const struct ClockPortInitElem *elem;
122 
123     for (elem = &clocks[0]; elem->name != NULL; elem++) {
124         Clock **clkp;
125         /* offset cannot be inside the DeviceState part */
126         assert(elem->offset > sizeof(DeviceState));
127         clkp = (Clock **)(((void *) dev) + elem->offset);
128         if (elem->is_output) {
129             *clkp = qdev_init_clock_out(dev, elem->name);
130         } else {
131             *clkp = qdev_init_clock_in(dev, elem->name, elem->callback, dev);
132         }
133     }
134 }
135 
136 static NamedClockList *qdev_get_clocklist(DeviceState *dev, const char *name)
137 {
138     NamedClockList *ncl;
139 
140     QLIST_FOREACH(ncl, &dev->clocks, node) {
141         if (strcmp(name, ncl->name) == 0) {
142             return ncl;
143         }
144     }
145 
146     return NULL;
147 }
148 
149 Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
150 {
151     NamedClockList *ncl;
152 
153     assert(name);
154 
155     ncl = qdev_get_clocklist(dev, name);
156     assert(!ncl->output);
157 
158     return ncl->clock;
159 }
160 
161 Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
162 {
163     NamedClockList *ncl;
164 
165     assert(name);
166 
167     ncl = qdev_get_clocklist(dev, name);
168     assert(ncl->output);
169 
170     return ncl->clock;
171 }
172 
173 Clock *qdev_alias_clock(DeviceState *dev, const char *name,
174                         DeviceState *alias_dev, const char *alias_name)
175 {
176     NamedClockList *ncl;
177 
178     assert(name && alias_name);
179 
180     ncl = qdev_get_clocklist(dev, name);
181 
182     qdev_init_clocklist(alias_dev, alias_name, ncl->output, ncl->clock);
183 
184     return ncl->clock;
185 }
186 
187 void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source)
188 {
189     assert(!dev->realized);
190     clock_set_source(qdev_get_clock_in(dev, name), source);
191 }
192