From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567602546196663.679316690497; Wed, 4 Sep 2019 06:09:06 -0700 (PDT) Received: from localhost ([::1]:57612 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5V20-0000UU-Qe for importer@patchew.org; Wed, 04 Sep 2019 09:09:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53656) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Upc-0006WH-RV for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5Upa-0005Zk-Ak for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:16 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:45880) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5UpW-0005Vg-8m; Wed, 04 Sep 2019 08:56:10 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id 91A1896F66; Wed, 4 Sep 2019 12:56:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601769; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R8mQ/VeYnnaf29gnx/Elu5cV5iT7NOOhQ80ID/ERnPg=; b=ksg8tQpWiq7xHwurYJxFTtVkiM7df8UYu99r14IsehsbTwxYlv+M0FZK6BBn32jMJkLaNY mHOqvFJpLZ8Zj3l9iPT1EzqNXicWxrXV2PAApBIk02e5e8cP8fm3h6+GZzz5ztcl0cXUrK tkEwAUAZLBQYHZa054jTI497kK1RePQ= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:23 +0200 Message-Id: <20190904125531.27545-2-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601769; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R8mQ/VeYnnaf29gnx/Elu5cV5iT7NOOhQ80ID/ERnPg=; b=MdaUthTJ02mlLLZ4/3Z09FkAWPmxhzrMpFT2eO4RgdVhpZZd8rdRpLOEcppOqDVHZEEX9r QRX8Lt8CxHVkH4CidoINfpJEs8eKl0X3eFrb0LvMIZ4RjzClmhf/SNGNyC6akwMZ71Makm 7QX4nZma05aQQnaCPCvil+zpQ2SaQjY= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601769; a=rsa-sha256; cv=none; b=TRpWwC/1uL0RwdJtTxGfeb3GRoSbuXh9GPNcCQ5hJeMUaoilwN36+yQ7b2uCLLXLXQzlHX YQ0ij+f540uMzu90QZIqWJ47Vvxk0bQvS/e5EDXHoefJBDI9P6EoF8YkD5loPnrpz3TsUv 2Whns+vFFQrIzTQixfJdnbmlqgU3bH8= ARC-Authentication-Results: i=1; beetle.greensocs.com; none Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 1/9] hw/core/clock: introduce clock objects X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Introduce clock objects: ClockIn and ClockOut. These objects may be used to distribute clocks from an object to several other objects. Each ClockIn object contains the current state of the clock: the frequency; it allows an object to migrate its input clock state independently of other objects. A ClockIn may be connected to a ClockOut so that it receives update, through a callback, whenever the Clockout is updated using the ClockOut's set function. This is based on the original work of Frederic Konrad. Signed-off-by: Damien Hedde Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 --- Makefile.objs | 1 + hw/core/Makefile.objs | 1 + hw/core/clock.c | 144 ++++++++++++++++++++++++++++++++++++++++++ hw/core/trace-events | 6 ++ include/hw/clock.h | 124 ++++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+) create mode 100644 hw/core/clock.c create mode 100644 include/hw/clock.h diff --git a/Makefile.objs b/Makefile.objs index a723a47e14..4da623c759 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -153,6 +153,7 @@ trace-events-subdirs +=3D hw/audio trace-events-subdirs +=3D hw/block trace-events-subdirs +=3D hw/block/dataplane trace-events-subdirs +=3D hw/char +trace-events-subdirs +=3D hw/core trace-events-subdirs +=3D hw/dma trace-events-subdirs +=3D hw/hppa trace-events-subdirs +=3D hw/i2c diff --git a/hw/core/Makefile.objs b/hw/core/Makefile.objs index 69b408ad1c..c66a5b2c6b 100644 --- a/hw/core/Makefile.objs +++ b/hw/core/Makefile.objs @@ -7,6 +7,7 @@ common-obj-$(CONFIG_SOFTMMU) +=3D fw-path-provider.o # irq.o needed for qdev GPIO handling: common-obj-y +=3D irq.o common-obj-y +=3D hotplug.o +common-obj-y +=3D clock.o common-obj-$(CONFIG_SOFTMMU) +=3D nmi.o common-obj-$(CONFIG_SOFTMMU) +=3D vm-change-state-handler.o =20 diff --git a/hw/core/clock.c b/hw/core/clock.c new file mode 100644 index 0000000000..888f247f2a --- /dev/null +++ b/hw/core/clock.c @@ -0,0 +1,144 @@ +/* + * Clock inputs and outputs + * + * Copyright GreenSocs 2016-2018 + * + * Authors: + * Frederic Konrad + * Damien Hedde + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "hw/clock.h" +#include "trace.h" + +#define CLOCK_PATH(_clk) (_clk->canonical_path) + +void clock_out_setup_canonical_path(ClockOut *clk) +{ + g_free(clk->canonical_path); + clk->canonical_path =3D object_get_canonical_path(OBJECT(clk)); +} + +void clock_in_setup_canonical_path(ClockIn *clk) +{ + g_free(clk->canonical_path); + clk->canonical_path =3D object_get_canonical_path(OBJECT(clk)); +} + +void clock_set_callback(ClockIn *clk, ClockCallback *cb, void *opaque) +{ + assert(clk); + + clk->callback =3D cb; + clk->callback_opaque =3D opaque; +} + +void clock_init_frequency(ClockIn *clk, uint64_t freq) +{ + assert(clk); + + clk->frequency =3D freq; +} + +void clock_clear_callback(ClockIn *clk) +{ + clock_set_callback(clk, NULL, NULL); +} + +void clock_connect(ClockIn *clkin, ClockOut *clkout) +{ + assert(clkin && clkin->driver =3D=3D NULL); + assert(clkout); + + trace_clock_connect(CLOCK_PATH(clkin), CLOCK_PATH(clkout)); + + QLIST_INSERT_HEAD(&clkout->followers, clkin, sibling); + clkin->driver =3D clkout; +} + +static void clock_disconnect(ClockIn *clk) +{ + if (clk->driver =3D=3D NULL) { + return; + } + + trace_clock_disconnect(CLOCK_PATH(clk)); + + clk->driver =3D NULL; + QLIST_REMOVE(clk, sibling); +} + +void clock_set_frequency(ClockOut *clk, uint64_t freq) +{ + ClockIn *follower; + trace_clock_set_frequency(CLOCK_PATH(clk), freq); + + QLIST_FOREACH(follower, &clk->followers, sibling) { + trace_clock_propagate(CLOCK_PATH(clk), CLOCK_PATH(follower)); + if (follower->frequency !=3D freq) { + follower->frequency =3D freq; + if (follower->callback) { + follower->callback(follower->callback_opaque); + } + } + } +} + +static void clock_out_initfn(Object *obj) +{ + ClockOut *clk =3D CLOCK_OUT(obj); + + QLIST_INIT(&clk->followers); +} + +static void clock_out_finalizefn(Object *obj) +{ + ClockOut *clk =3D CLOCK_OUT(obj); + ClockIn *follower, *next; + + /* clear our list of followers */ + QLIST_FOREACH_SAFE(follower, &clk->followers, sibling, next) { + clock_disconnect(follower); + } + + g_free(clk->canonical_path); + clk->canonical_path =3D NULL; +} + +static void clock_in_finalizefn(Object *obj) +{ + ClockIn *clk =3D CLOCK_IN(obj); + + /* remove us from driver's followers list */ + clock_disconnect(clk); + + g_free(clk->canonical_path); + clk->canonical_path =3D NULL; +} + +static const TypeInfo clock_out_info =3D { + .name =3D TYPE_CLOCK_OUT, + .parent =3D TYPE_OBJECT, + .instance_size =3D sizeof(ClockOut), + .instance_init =3D clock_out_initfn, + .instance_finalize =3D clock_out_finalizefn, +}; + +static const TypeInfo clock_in_info =3D { + .name =3D TYPE_CLOCK_IN, + .parent =3D TYPE_OBJECT, + .instance_size =3D sizeof(ClockIn), + .instance_finalize =3D clock_in_finalizefn, +}; + +static void clock_register_types(void) +{ + type_register_static(&clock_in_info); + type_register_static(&clock_out_info); +} + +type_init(clock_register_types) diff --git a/hw/core/trace-events b/hw/core/trace-events index ecf966c314..aa940e268b 100644 --- a/hw/core/trace-events +++ b/hw/core/trace-events @@ -34,3 +34,9 @@ resettable_phase_hold_end(void *obj, int needed) "obj=3D%= p needed=3D%d" resettable_phase_exit(void *obj, const char *type) "obj=3D%p(%s)" resettable_phase_exit_end(void *obj, uint32_t count) "obj=3D%p count=3D%" = PRIu32 resettable_count_underflow(void *obj) "obj=3D%p" + +# hw/core/clock-port.c +clock_connect(const char *clk, const char *driver) "'%s' drived-by '%s'" +clock_disconnect(const char *clk) "'%s'" +clock_set_frequency(const char *clk, uint64_t freq) "'%s' freq_hz=3D%" PRI= u64 +clock_propagate(const char *clko, const char *clki) "'%s' =3D> '%s'" diff --git a/include/hw/clock.h b/include/hw/clock.h new file mode 100644 index 0000000000..fd11202ba4 --- /dev/null +++ b/include/hw/clock.h @@ -0,0 +1,124 @@ +#ifndef QEMU_HW_CLOCK_H +#define QEMU_HW_CLOCK_H + +#include "qom/object.h" +#include "qemu/queue.h" + +#define TYPE_CLOCK_IN "clock-in" +#define CLOCK_IN(obj) OBJECT_CHECK(ClockIn, (obj), TYPE_CLOCK_IN) +#define TYPE_CLOCK_OUT "clock-out" +#define CLOCK_OUT(obj) OBJECT_CHECK(ClockOut, (obj), TYPE_CLOCK_OUT) + +typedef void ClockCallback(void *opaque); + +typedef struct ClockOut ClockOut; +typedef struct ClockIn ClockIn; + +struct ClockIn { + /*< private >*/ + Object parent_obj; + /*< private >*/ + uint64_t frequency; + char *canonical_path; /* clock path cache */ + ClockOut *driver; /* clock output controlling this clock */ + ClockCallback *callback; /* local callback */ + void *callback_opaque; /* opaque argument for the callback */ + QLIST_ENTRY(ClockIn) sibling; /* entry in a followers list */ +}; + +struct ClockOut { + /*< private >*/ + Object parent_obj; + /*< private >*/ + char *canonical_path; /* clock path cache */ + QLIST_HEAD(, ClockIn) followers; /* list of registered clocks */ +}; + +/** + * clock_out_setup_canonical_path: + * @clk: clock + * + * compute the canonical path of the clock (used by log messages) + */ +void clock_out_setup_canonical_path(ClockOut *clk); + +/** + * clock_in_setup_canonical_path: + * @clk: clock + * + * compute the canonical path of the clock (used by log messages) + */ +void clock_in_setup_canonical_path(ClockIn *clk); + +/** + * clock_add_callback: + * @clk: the clock to register the callback into + * @cb: the callback function + * @opaque: the argument to the callback + * + * Register a callback called on every clock update. + */ +void clock_set_callback(ClockIn *clk, ClockCallback *cb, void *opaque); + +/** + * clock_clear_callback: + * @clk: the clock to delete the callback from + * + * Unregister the callback registered with clock_set_callback. + */ +void clock_clear_callback(ClockIn *clk); + +/** + * clock_init_frequency: + * @clk: the clock to initialize. + * @freq: the clock's frequency in Hz or 0 if unclocked. + * + * Initialize the local cached frequency value of @clk to @freq. + * Note: this function must only be called during device inititialization + * or migration. + */ +void clock_init_frequency(ClockIn *clk, uint64_t freq); + +/** + * clock_connect: + * @clkin: the drived clock. + * @clkout: the driving clock. + * + * Setup @clkout to drive @clkin: Any @clkout update will be propagated + * to @clkin. + */ +void clock_connect(ClockIn *clkin, ClockOut *clkout); + +/** + * clock_set_frequency: + * @clk: the clock to update. + * @freq: the new clock's frequency in Hz or 0 if unclocked. + * + * Update the @clk to the new @freq. + * This change will be propagated through registered clock inputs. + */ +void clock_set_frequency(ClockOut *clk, uint64_t freq); + +/** + * clock_get_frequency: + * @clk: the clk to fetch the clock + * + * @return: the current frequency of @clk in Hz. If @clk is NULL, return 0. + */ +static inline uint64_t clock_get_frequency(const ClockIn *clk) +{ + return clk ? clk->frequency : 0; +} + +/** + * clock_is_enabled: + * @clk: a clock state + * + * @return: true if the clock is running. If @clk is NULL return false. + */ +static inline bool clock_is_enabled(const ClockIn *clk) +{ + return clock_get_frequency(clk) !=3D 0; +} + +#endif /* QEMU_HW_CLOCK_H */ --=20 2.22.0 From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567602674347974.3199035006435; Wed, 4 Sep 2019 06:11:14 -0700 (PDT) Received: from localhost ([::1]:57648 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5V44-0002LC-Tg for importer@patchew.org; Wed, 04 Sep 2019 09:11:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53609) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Upa-0006Tv-Lv for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5UpZ-0005Yt-Gs for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:14 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:45908) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5UpW-0005WQ-Tz; Wed, 04 Sep 2019 08:56:11 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id 87F8896F65; Wed, 4 Sep 2019 12:56:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L1cYSywxsNjFTbyO7aWViJMbAww4xP344l5lA8bvklQ=; b=FpzhLdV1/pkM4IfvexiVv2kMoSjWDI1jcgcZLHg9tnz4ocOkihTkBte1hq07fTr3mLnkJ/ NqIU5OF2N0I4tXuhMtEdLFv9MSDTyiEZgiXov7m9ONoFHLPZscu8ruAMQCxL07xd9QUKpw jC4QrRStYnYgJdqpJd9PWIO60yvhuN0= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:24 +0200 Message-Id: <20190904125531.27545-3-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L1cYSywxsNjFTbyO7aWViJMbAww4xP344l5lA8bvklQ=; b=fV/ccsRfVBAx9ltMid7Bl4D5Pc4fchLwCmZIfa2GutHCRluL/ERVjZIT9pK0gZNCtcDSSO qys4JdUsZCflEcLoieYzzjiDTrokA1t+axxGKlhAlbhClkql5ZOS//Xi1Cgrg0XmQGmCIr 4pLgO4RKHGx7FuHD6YzBnu5ODrnea2o= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601770; a=rsa-sha256; cv=none; b=PyRokgH1uWH1zed9Pc729W8qXFWKLt7J1XYK6S9ide8COsIoSsnbHmb2QFyxhh4vLlqr51 AjQL+yKQ3fWUBGZloy1FG0GF56+x50iG/glrQ6DCqXiX3lptU3HRPzmYyFwQ5cErJuHsQl 34CpTKgfoVQx6lUCYz/ng2Z/2rrvkKk= ARC-Authentication-Results: i=1; beetle.greensocs.com; none X-Spam: Yes Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 2/9] hw/core/clock-vmstate: define a vmstate entry for clock state X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Damien Hedde Reviewed-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- This was in the previous reviewed commit. But it can't be in the clock.c file in order to allow linux-user builds. --- hw/core/Makefile.objs | 1 + hw/core/clock-vmstate.c | 25 +++++++++++++++++++++++++ include/hw/clock.h | 9 +++++++++ 3 files changed, 35 insertions(+) create mode 100644 hw/core/clock-vmstate.c diff --git a/hw/core/Makefile.objs b/hw/core/Makefile.objs index c66a5b2c6b..8fcebf2e67 100644 --- a/hw/core/Makefile.objs +++ b/hw/core/Makefile.objs @@ -4,6 +4,7 @@ common-obj-y +=3D bus.o reset.o common-obj-y +=3D resettable.o common-obj-$(CONFIG_SOFTMMU) +=3D qdev-fw.o common-obj-$(CONFIG_SOFTMMU) +=3D fw-path-provider.o +common-obj-$(CONFIG_SOFTMMU) +=3D clock-vmstate.o # irq.o needed for qdev GPIO handling: common-obj-y +=3D irq.o common-obj-y +=3D hotplug.o diff --git a/hw/core/clock-vmstate.c b/hw/core/clock-vmstate.c new file mode 100644 index 0000000000..c781369c15 --- /dev/null +++ b/hw/core/clock-vmstate.c @@ -0,0 +1,25 @@ +/* + * Clock migration structure + * + * Copyright GreenSocs 2019 + * + * Authors: + * Damien Hedde + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "migration/vmstate.h" +#include "hw/clock.h" + +const VMStateDescription vmstate_clockin =3D { + .name =3D "clock", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_UINT64(frequency, ClockIn), + VMSTATE_END_OF_LIST() + } +}; diff --git a/include/hw/clock.h b/include/hw/clock.h index fd11202ba4..e7efb6ad17 100644 --- a/include/hw/clock.h +++ b/include/hw/clock.h @@ -34,6 +34,15 @@ struct ClockOut { QLIST_HEAD(, ClockIn) followers; /* list of registered clocks */ }; =20 +/* + * vmstate description entry to be added in device vmsd. + */ +extern const VMStateDescription vmstate_clockin; +#define VMSTATE_CLOCKIN(_field, _state) \ + VMSTATE_CLOCKIN_V(_field, _state, 0) +#define VMSTATE_CLOCKIN_V(_field, _state, _version) \ + VMSTATE_STRUCT_POINTER_V(_field, _state, _version, vmstate_clockin, Cl= ockIn) + /** * clock_out_setup_canonical_path: * @clk: clock --=20 2.22.0 From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567603685692441.40669892577307; Wed, 4 Sep 2019 06:28:05 -0700 (PDT) Received: from localhost ([::1]:57912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5VKJ-0001Sr-J6 for importer@patchew.org; Wed, 04 Sep 2019 09:27:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53717) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Upf-0006ae-UV for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5Upc-0005bv-Tx for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:19 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:45926) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5UpX-0005Ws-KL; Wed, 04 Sep 2019 08:56:11 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id 302A896F67; Wed, 4 Sep 2019 12:56:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nWgo2WvyT7oMTlGOfjSje7UzEcT0OjZXGMk4UD6u5Vc=; b=hsLWkF6lWLhHmZWH//GP/cYOVQkwQiafYuJdvYQ9ZC+jFvmfracfYw1MwmJ2KIkIc5PIRK yhpUmOmRsMnXmIJC9AL/7VQctXrrZU2cwqb/0ldcOayAnMbZSYng9hqQfFlpA47+SOEfCd aSadXak/ZPOwsJuGh9PTOh/h32LF8Wc= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:25 +0200 Message-Id: <20190904125531.27545-4-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nWgo2WvyT7oMTlGOfjSje7UzEcT0OjZXGMk4UD6u5Vc=; b=sLKwEeW/RF1a1iXmG2qpmwBooUEgZuZpbqQ7svYC97fF0bg4WATbE0jIf86AABCwsIWeVf nBKvWi1jLc47Gls5GTFi3lCwvzpMAR0+EW93eW4s0xY06LekI9SkCd842cWOatQm4Tyv1J EPdZh+YfWZdTSfD6Wm33934U7xKNd28= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601770; a=rsa-sha256; cv=none; b=s76IFon6gGPVKqgJUGUzxGhEfbdB4JgQ+Un4XzBVbkWyzXokycAv4PBbdl5/h6s2oEhwhk GcSqpqCw2GprkKjxwlTW0OVlsTaDMMnraSgwW732sU2V383lShVMErQrIELJR1nIX31QDk pCD8MHKGaE25p4Yl4/atuMAs7a5IXFk= ARC-Authentication-Results: i=1; beetle.greensocs.com; none X-Spam: Yes Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 3/9] qdev: add clock input&output support to devices. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add functions to easily add input or output clocks to a device. A clock objects is added as a child of the device. The api is very similar the gpio's one. This is based on the original work of Frederic Konrad. Signed-off-by: Damien Hedde Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- I've removed the reviewed-by/tested-by of Philippe because I did a small modification. qdev_connect_clock() which allowed to connect an input to an output is now split in 2: + qdev_get_clock_in() which gets a given input from a device + qdev_connect_clock_out() which connect a given output to a clock (previously fetched by qdev_get_clock_in()) This part is located in (qdev-clock.[c|h]). It better matches gpios api and also add the possibility to connect a device's input clock to a random output clock (used in patch 9). Also add missing qdev-clock in the test-qdev-global-props so that tests pass. --- hw/core/Makefile.objs | 2 +- hw/core/qdev-clock.c | 155 ++++++++++++++++++++++++++++++++++++++++ hw/core/qdev.c | 32 +++++++++ include/hw/qdev-clock.h | 67 +++++++++++++++++ include/hw/qdev-core.h | 14 ++++ tests/Makefile.include | 1 + 6 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 hw/core/qdev-clock.c create mode 100644 include/hw/qdev-clock.h diff --git a/hw/core/Makefile.objs b/hw/core/Makefile.objs index 8fcebf2e67..4523d3b5c7 100644 --- a/hw/core/Makefile.objs +++ b/hw/core/Makefile.objs @@ -1,5 +1,5 @@ # core qdev-related obj files, also used by *-user: -common-obj-y +=3D qdev.o qdev-properties.o +common-obj-y +=3D qdev.o qdev-properties.o qdev-clock.o common-obj-y +=3D bus.o reset.o common-obj-y +=3D resettable.o common-obj-$(CONFIG_SOFTMMU) +=3D qdev-fw.o diff --git a/hw/core/qdev-clock.c b/hw/core/qdev-clock.c new file mode 100644 index 0000000000..bebdd8fa15 --- /dev/null +++ b/hw/core/qdev-clock.c @@ -0,0 +1,155 @@ +/* + * Device's clock + * + * Copyright GreenSocs 2016-2018 + * + * Authors: + * Frederic Konrad + * Damien Hedde + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "hw/qdev-clock.h" +#include "hw/qdev-core.h" +#include "qapi/error.h" + +static NamedClockList *qdev_init_clocklist(DeviceState *dev, const char *n= ame, + bool forward) +{ + NamedClockList *ncl; + + /* + * The clock path will be computed by the device's realize function ca= ll. + * This is required to ensure the clock's canonical path is right and = log + * messages are meaningfull. + */ + assert(name); + assert(!dev->realized); + + /* The ncl structure will be freed in device's finalize function call = */ + ncl =3D g_malloc0(sizeof(*ncl)); + ncl->name =3D g_strdup(name); + ncl->forward =3D forward; + + QLIST_INSERT_HEAD(&dev->clocks, ncl, node); + return ncl; +} + +ClockOut *qdev_init_clock_out(DeviceState *dev, const char *name) +{ + NamedClockList *ncl; + Object *clk; + + ncl =3D qdev_init_clocklist(dev, name, false); + + clk =3D object_new(TYPE_CLOCK_OUT); + + /* will fail if name already exists */ + object_property_add_child(OBJECT(dev), name, clk, &error_abort); + object_unref(clk); /* remove the initial ref made by object_new */ + + ncl->out =3D CLOCK_OUT(clk); + return ncl->out; +} + +ClockIn *qdev_init_clock_in(DeviceState *dev, const char *name, + ClockCallback *callback, void *opaque) +{ + NamedClockList *ncl; + Object *clk; + + ncl =3D qdev_init_clocklist(dev, name, false); + + clk =3D object_new(TYPE_CLOCK_IN); + /* + * the ref initialized by object_new will be cleared during dev finali= ze. + * It allows us to safely remove the callback. + */ + + /* will fail if name already exists */ + object_property_add_child(OBJECT(dev), name, clk, &error_abort); + + ncl->in =3D CLOCK_IN(clk); + if (callback) { + clock_set_callback(ncl->in, callback, opaque); + } + return ncl->in; +} + +static NamedClockList *qdev_get_clocklist(DeviceState *dev, const char *na= me) +{ + NamedClockList *ncl; + + QLIST_FOREACH(ncl, &dev->clocks, node) { + if (strcmp(name, ncl->name) =3D=3D 0) { + return ncl; + } + } + + return NULL; +} + +void qdev_pass_clock(DeviceState *dev, const char *name, + DeviceState *container, const char *cont_name) +{ + NamedClockList *original_ncl, *ncl; + Object **clk; + + assert(container && cont_name); + + original_ncl =3D qdev_get_clocklist(container, cont_name); + assert(original_ncl); /* clock must exist in origin */ + + ncl =3D qdev_init_clocklist(dev, name, true); + + if (ncl->out) { + clk =3D (Object **)&ncl->out; + } else { + clk =3D (Object **)&ncl->in; + } + + /* will fail if name already exists */ + object_property_add_link(OBJECT(dev), name, object_get_typename(*clk), + clk, NULL, OBJ_PROP_LINK_STRONG, &error_abort= ); +} + +ClockIn *qdev_get_clock_in(DeviceState *dev, const char *name) +{ + NamedClockList *ncl; + + assert(dev && name); + + ncl =3D qdev_get_clocklist(dev, name); + return ncl ? ncl->in : NULL; +} + +static ClockOut *qdev_get_clock_out(DeviceState *dev, const char *name) +{ + NamedClockList *ncl; + + assert(dev && name); + + ncl =3D qdev_get_clocklist(dev, name); + return ncl ? ncl->out : NULL; +} + +void qdev_connect_clock_out(DeviceState *dev, const char *name, ClockIn *c= lk, + Error **errp) +{ + ClockOut *clkout =3D qdev_get_clock_out(dev, name); + + if (!clk) { + error_setg(errp, "NULL input clock"); + return; + } + + if (!clkout) { + error_setg(errp, "no output clock '%s' in device", name); + return; + } + + clock_connect(clk, clkout); +} diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 9095f2b9c1..eae6cd3e09 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -37,6 +37,7 @@ #include "hw/qdev-properties.h" #include "hw/boards.h" #include "hw/sysbus.h" +#include "hw/clock.h" #include "migration/vmstate.h" =20 bool qdev_hotplug =3D false; @@ -821,6 +822,7 @@ static void device_set_realized(Object *obj, bool value= , Error **errp) DeviceClass *dc =3D DEVICE_GET_CLASS(dev); HotplugHandler *hotplug_ctrl; BusState *bus; + NamedClockList *clk; Error *local_err =3D NULL; bool unattached_parent =3D false; static int unattached_count; @@ -869,6 +871,15 @@ static void device_set_realized(Object *obj, bool valu= e, Error **errp) */ g_free(dev->canonical_path); dev->canonical_path =3D object_get_canonical_path(OBJECT(dev)); + QLIST_FOREACH(clk, &dev->clocks, node) { + if (clk->forward) { + continue; + } else if (clk->in !=3D NULL) { + clock_in_setup_canonical_path(clk->in); + } else { + clock_out_setup_canonical_path(clk->out); + } + } =20 if (qdev_get_vmsd(dev)) { if (vmstate_register_with_alias_id(dev, -1, qdev_get_vmsd(dev)= , dev, @@ -999,6 +1010,7 @@ static void device_initfn(Object *obj) (Object **)&dev->parent_bus, NULL, 0, &error_abort); QLIST_INIT(&dev->gpios); + QLIST_INIT(&dev->clocks); } =20 static void device_post_init(Object *obj) @@ -1015,6 +1027,7 @@ static void device_post_init(Object *obj) static void device_finalize(Object *obj) { NamedGPIOList *ngl, *next; + NamedClockList *clk, *clk_next; =20 DeviceState *dev =3D DEVICE(obj); =20 @@ -1028,6 +1041,25 @@ static void device_finalize(Object *obj) */ } =20 + QLIST_FOREACH_SAFE(clk, &dev->clocks, node, clk_next) { + QLIST_REMOVE(clk, node); + if (!clk->forward && clk->in) { + /* + * if this clock is not forwarded, clk->in is a child of dev. + * At this point the child property and associated reference is + * already deleted but we kept a ref on clk->in to ensure it l= ives + * up to this point and we can safely remove the callback. + * It avoids having a lost callback to a deleted device if the + * clk->in is still referenced somewhere else (eg: by a clock + * output). + */ + clock_clear_callback(clk->in); + object_unref(OBJECT(clk->in)); + } + g_free(clk->name); + g_free(clk); + } + /* Only send event if the device had been completely realized */ if (dev->pending_deleted_event) { g_assert(dev->canonical_path); diff --git a/include/hw/qdev-clock.h b/include/hw/qdev-clock.h new file mode 100644 index 0000000000..c4ea912fdc --- /dev/null +++ b/include/hw/qdev-clock.h @@ -0,0 +1,67 @@ +#ifndef QDEV_CLOCK_H +#define QDEV_CLOCK_H + +#include "hw/clock.h" + +/** + * qdev_init_clock_in: + * @dev: the device in which to add a clock + * @name: the name of the clock (can't be NULL). + * @callback: optional callback to be called on update or NULL. + * @opaque: argument for the callback + * @returns: a pointer to the newly added clock + * + * Add a input clock to device @dev as a clock named @name. + * This adds a child<> property. + * The callback will be called with @dev as opaque parameter. + */ +ClockIn *qdev_init_clock_in(DeviceState *dev, const char *name, + ClockCallback *callback, void *opaque); + +/** + * qdev_init_clock_out: + * @dev: the device to add a clock to + * @name: the name of the clock (can't be NULL). + * @callback: optional callback to be called on update or NULL. + * @returns: a pointer to the newly added clock + * + * Add a output clock to device @dev as a clock named @name. + * This adds a child<> property. + */ +ClockOut *qdev_init_clock_out(DeviceState *dev, const char *name); + +/** + * qdev_get_clock_in: + * @dev: the device which has the clock + * @name: the name of the clock (can't be NULL). + * @returns: a pointer to the clock + * + * Get the clock @name from @dev or NULL if does not exists. + */ +ClockIn *qdev_get_clock_in(DeviceState *dev, const char *name); + +/** + * qdev_connect_clock_out: + * @dev: the device which has the clock + * @name: the name of the clock (can't be NULL). + * @errp: error report + * + * Connect @clk to the output clock @name of @dev. + * Reports an error if clk is NULL or @name does not exists in @dev. + */ +void qdev_connect_clock_out(DeviceState *dev, const char *name, ClockIn *c= lk, + Error **errp); + +/** + * qdev_pass_clock: + * @dev: the device to forward the clock to + * @name: the name of the clock to be added (can't be NULL) + * @container: the device which already has the clock + * @cont_name: the name of the clock in the container device + * + * Add a clock @name to @dev which forward to the clock @cont_name in @con= tainer + */ +void qdev_pass_clock(DeviceState *dev, const char *name, + DeviceState *container, const char *cont_name); + +#endif /* QDEV_CLOCK_H */ diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index eb11f0f801..60a65f6142 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -131,6 +131,19 @@ struct NamedGPIOList { QLIST_ENTRY(NamedGPIOList) node; }; =20 +typedef struct NamedClockList NamedClockList; + +typedef struct ClockIn ClockIn; +typedef struct ClockOut ClockOut; + +struct NamedClockList { + char *name; + bool forward; + ClockIn *in; + ClockOut *out; + QLIST_ENTRY(NamedClockList) node; +}; + /** * DeviceState: * @realized: Indicates whether the device has been fully constructed. @@ -152,6 +165,7 @@ struct DeviceState { int hotplugged; BusState *parent_bus; QLIST_HEAD(, NamedGPIOList) gpios; + QLIST_HEAD(, NamedClockList) clocks; QLIST_HEAD(, BusState) child_bus; int num_child_bus; int instance_id_alias; diff --git a/tests/Makefile.include b/tests/Makefile.include index f0b4628cc6..5c54beb29e 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -566,6 +566,7 @@ tests/test-qdev-global-props$(EXESUF): tests/test-qdev-= global-props.o \ hw/core/irq.o \ hw/core/fw-path-provider.o \ hw/core/reset.o \ + hw/core/clock.o \ $(test-qapi-obj-y) tests/test-vmstate$(EXESUF): tests/test-vmstate.o \ migration/vmstate.o migration/vmstate-types.o migration/qemu-file.o \ --=20 2.22.0 From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567602174860592.3527543419782; Wed, 4 Sep 2019 06:02:54 -0700 (PDT) Received: from localhost ([::1]:57510 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Uw1-0001mN-Fc for importer@patchew.org; Wed, 04 Sep 2019 09:02:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53654) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Upc-0006WB-PZ for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5Upa-0005aJ-Vf for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:16 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:45948) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5UpY-0005XM-9G; Wed, 04 Sep 2019 08:56:12 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id D325E96F68; Wed, 4 Sep 2019 12:56:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601771; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bMR1IJwAws7qpyrYbUSBXvP6m2j7eLRWVAmWXlvoh+8=; b=pZAL8KcGVT9kSm2TFkkaHElhRVauk7gla+OqTVSZwhi9fQ/spFlufI8OdRHBsaSgZd6tDS GekOPzgOozT/tUacyEPS8zt0j0M89FXEXpfK6UoUxFrZcuBgRhGyNQ8Hotdtc6tee1HzXC 7zxqfEwne6QTs1WgtR8R1luYGBr5XcM= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:26 +0200 Message-Id: <20190904125531.27545-5-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601771; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bMR1IJwAws7qpyrYbUSBXvP6m2j7eLRWVAmWXlvoh+8=; b=XjX/Q5CYq7ClhTdFhLmqKvOyxepe7D8ypHrVqfZleFHgRuIXyvq4/ddYFXUmeyF/qfNEdq e1dCFviAZF9aOb+2tBrkMYPIfrZPOr1sePiTQI6NtluYzHLFCPAobTJlXIkfoOLgfaFTxJ QkaRU8i7oDsz9Jti08XjE5/+rtVZmpg= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601771; a=rsa-sha256; cv=none; b=IiS9/cAHtLEl8DJOOf7dy7+LTaNfQiL8drJIvWmXvY9zbyfbAgukqoATSKzH4vALFUMM0p BZkWaNm2jyA4dQ1TAjI4+XGhINlAEkt4+l8wNPhCnPKdrwuIf3Xv8Sf7OTrUZmCER3Pr4j ityF7Muf9MWoQs+CJOaR9VOwzORu64Q= ARC-Authentication-Results: i=1; beetle.greensocs.com; none Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 4/9] qdev-monitor: print the device's clock with info qtree X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This prints the clocks attached to a DeviceState when using "info qtree" mo= nitor command. For every clock, it displays the direction, the name and if the clock is forwarded. For input clock, it displays also the frequency. This is based on the original work of Frederic Konrad. Signed-off-by: Damien Hedde Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Peter Maydell --- qdev-monitor.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/qdev-monitor.c b/qdev-monitor.c index a0003bf2a9..d5b8be956b 100644 --- a/qdev-monitor.c +++ b/qdev-monitor.c @@ -19,6 +19,7 @@ =20 #include "qemu/osdep.h" #include "hw/sysbus.h" +#include "hw/clock.h" #include "monitor/monitor.h" #include "monitor/qdev.h" #include "sysemu/arch_init.h" @@ -689,6 +690,7 @@ static void qdev_print(Monitor *mon, DeviceState *dev, = int indent) ObjectClass *class; BusState *child; NamedGPIOList *ngl; + NamedClockList *clk; =20 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), dev->id ? dev->id : ""); @@ -703,6 +705,17 @@ static void qdev_print(Monitor *mon, DeviceState *dev,= int indent) ngl->num_out); } } + QLIST_FOREACH(clk, &dev->clocks, node) { + if (clk->out) { + qdev_printf("clock-out%s \"%s\"\n", + clk->forward ? " (fw)" : "", + clk->name); + } else { + qdev_printf("clock-in%s \"%s\" freq_hz=3D%" PRIu64"\n", + clk->forward ? " (fw)" : "", + clk->name, clock_get_frequency(clk->in)); + } + } class =3D object_get_class(OBJECT(dev)); do { qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent); --=20 2.22.0 From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 156760322245672.73626167224086; Wed, 4 Sep 2019 06:20:22 -0700 (PDT) Received: from localhost ([::1]:57734 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5VCs-00010q-2J for importer@patchew.org; Wed, 04 Sep 2019 09:20:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53679) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Upd-0006Xq-WC for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5Upc-0005bK-Cc for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:17 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:45960) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5UpY-0005Xu-UU; Wed, 04 Sep 2019 08:56:13 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id 821B796F6A; Wed, 4 Sep 2019 12:56:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601772; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gdlTh3yyxOVxrasubJkegjXhqH/FOw0TXWBho063ltM=; b=vJoaRRoDYYVe144qOD97w4IdS4osxlKnocOOrdJB4UdUAouUXbwqrUsUBNdI44Gx9IIE9A IhsUTkN3MzUmpfSGRg0E5dNNnPuEhQc3ELkxk0FBJ4TMGmi6QMrVkbqpIcXqfvNDZojuRq 7430xJihcj8+EnSGpRDh1ovhwZ8HMp0= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:27 +0200 Message-Id: <20190904125531.27545-6-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601772; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gdlTh3yyxOVxrasubJkegjXhqH/FOw0TXWBho063ltM=; b=VZJJy4yaIh2A8LgJAd6huc7lrize3zQK99mcq78v2X7QGGE2x/VApmz3cR9HErMerx8rwW T6XHBn4Wj4SZh/OZKqGsQtH/eoBS0A1BvbX9sXaSc05IqbsNWUfQA6kL8JNSd57+h13cLX sljYKzfdtTfONwjkDSateqqeRvCqYTE= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601772; a=rsa-sha256; cv=none; b=nhS4/aSgHYx3589Dq0LCSflM3b6sQ6n94EeWiis8y7wGQn/pxtZ2S8hER/YH9amjG0Ittz XSYiEnNEJPSUo1WD70VCbJ37NHtG8yXCH6m8xvKgjxf9b+m2+zyimkqei+N+Z3jEcRacIV SueBAELtxFVwqysbdlKBPXFiKWYcqnI= ARC-Authentication-Results: i=1; beetle.greensocs.com; none Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 5/9] qdev-clock: introduce an init array to ease the device construction X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Introduce a function and macro helpers to setup several clocks in a device from a static array description. An element of the array describes the clock (name and direction) as well as the related callback and an optional offset to store the created object pointer in the device state structure. The array must be terminated by a special element QDEV_CLOCK_END. This is based on the original work of Frederic Konrad. Signed-off-by: Damien Hedde Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/core/qdev-clock.c | 26 ++++++++++++++++ include/hw/qdev-clock.h | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/hw/core/qdev-clock.c b/hw/core/qdev-clock.c index bebdd8fa15..32ad45c061 100644 --- a/hw/core/qdev-clock.c +++ b/hw/core/qdev-clock.c @@ -153,3 +153,29 @@ void qdev_connect_clock_out(DeviceState *dev, const ch= ar *name, ClockIn *clk, =20 clock_connect(clk, clkout); } + +void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks) +{ + const struct ClockPortInitElem *elem; + + assert(dev); + assert(clocks); + + for (elem =3D &clocks[0]; elem->name !=3D NULL; elem++) { + /* offset cannot be inside the DeviceState part */ + assert(elem->offset =3D=3D 0 || elem->offset > sizeof(DeviceState)= ); + if (elem->is_output) { + ClockOut *clk; + clk =3D qdev_init_clock_out(dev, elem->name); + if (elem->offset) { + *(ClockOut **)(((void *) dev) + elem->offset) =3D clk; + } + } else { + ClockIn *clk; + clk =3D qdev_init_clock_in(dev, elem->name, elem->callback, de= v); + if (elem->offset) { + *(ClockIn **)(((void *) dev) + elem->offset) =3D clk; + } + } + } +} diff --git a/include/hw/qdev-clock.h b/include/hw/qdev-clock.h index c4ea912fdc..b6edb9421b 100644 --- a/include/hw/qdev-clock.h +++ b/include/hw/qdev-clock.h @@ -64,4 +64,71 @@ void qdev_connect_clock_out(DeviceState *dev, const char= *name, ClockIn *clk, void qdev_pass_clock(DeviceState *dev, const char *name, DeviceState *container, const char *cont_name); =20 +/** + * ClockInitElem: + * @name: name of the clock (can't be NULL) + * @is_output: indicates whether the clock is input or output + * @callback: for inputs, optional callback to be called on clock's update + * with device as opaque + * @offset: optional offset to store the ClockIn or ClockOut pointer in de= vice + * state structure (0 means unused) + */ +struct ClockPortInitElem { + const char *name; + bool is_output; + ClockCallback *callback; + size_t offset; +}; + +#define clock_offset_value(_type, _devstate, _field) \ + (offsetof(_devstate, _field) + \ + type_check(_type *, typeof_field(_devstate, _field))) + +#define QDEV_CLOCK(_is_output, _type, _devstate, _field, _callback) { \ + .name =3D (stringify(_field)), \ + .is_output =3D _is_output, \ + .callback =3D _callback, \ + .offset =3D clock_offset_value(_type, _devstate, _field), \ +} + +/** + * QDEV_CLOCK_(IN|OUT): + * @_devstate: structure type. @dev argument of qdev_init_clocks below mus= t be + * a pointer to that same type. + * @_field: a field in @_devstate (must be ClockIn* or ClockOut*) + * @_callback: (for input only) callback (or NULL) to be called with the d= evice + * state as argument + * + * The name of the clock will be derived from @_field + */ +#define QDEV_CLOCK_IN(_devstate, _field, _callback) \ + QDEV_CLOCK(false, ClockIn, _devstate, _field, _callback) + +#define QDEV_CLOCK_OUT(_devstate, _field) \ + QDEV_CLOCK(true, ClockOut, _devstate, _field, NULL) + +/** + * QDEV_CLOCK_IN_NOFIELD: + * @_name: name of the clock + * @_callback: callback (or NULL) to be called with the device state as ar= gument + */ +#define QDEV_CLOCK_IN_NOFIELD(_name, _callback) { \ + .name =3D _name, \ + .is_output =3D false, \ + .callback =3D _callback, \ + .offset =3D 0, \ +} + +#define QDEV_CLOCK_END { .name =3D NULL } + +typedef struct ClockPortInitElem ClockPortInitArray[]; + +/** + * qdev_init_clocks: + * @dev: the device to add clocks + * @clocks: a QDEV_CLOCK_END-terminated array which contains the + * clocks information. + */ +void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks); + #endif /* QDEV_CLOCK_H */ --=20 2.22.0 From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567602973979912.7675527622848; Wed, 4 Sep 2019 06:16:13 -0700 (PDT) Received: from localhost ([::1]:57698 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5V8u-0006g4-CX for importer@patchew.org; Wed, 04 Sep 2019 09:16:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53743) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Uph-0006ct-Ey for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5Upf-0005dW-Dp for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:21 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:45970) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5UpZ-0005YT-Jo; Wed, 04 Sep 2019 08:56:13 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id 2E23E96F6C; Wed, 4 Sep 2019 12:56:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601772; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TALlTq4Hby/4+HqsOnAnIF2pH1v+JAWkyFTp6VC39dg=; b=d4spiVFj1KLJEYgRvsIfZ1umh6Kj7GAp8cvyhBp89GHqgUGBBdr67C2gG1ivh7PNdAZXrq I+JzHZ5IiWDiTFhMM6N6Q3VXes67gzFE0hw/bAtmsh2PakUiop+v6exiJxalP9dcydIkNI Jf4nbf+m53GdK2+YfMww2tINOntnySc= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:28 +0200 Message-Id: <20190904125531.27545-7-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601772; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TALlTq4Hby/4+HqsOnAnIF2pH1v+JAWkyFTp6VC39dg=; b=QXjrESdfthLnDxB/VuzDG6ySS9kha/x+DxhXn38eY8allh9Th4SfrwWiChGyma9y3QFHRl 2o90e10YgC2FfIdempM2f1oyZhkn1ICOAecqP3S0C3vnGOg05d5IlXZ/f2ojp+PDELUY/P V4jBPtHRZzh2NKt4T7SoVTzQB2EgihM= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601772; a=rsa-sha256; cv=none; b=LMdpQZN1yvRWUyDImbm04VIcDFepl3oCGRHUQ0qEpBw/GWtq2sGnL1rTSOQOGlQfJ8eVRK 0pmWC2mH3OGYukY195pgye4l3mOI+9V6ewV9zbBcMqd44g39u+jO6PYCLLbP3W/xsszzlm cZFjZPCDb1iVNG1nJQwr2UGsgNIl78c= ARC-Authentication-Results: i=1; beetle.greensocs.com; none X-Spam: Yes Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 6/9] docs/clocks: add device's clock documentation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add the documentation about the clock inputs and outputs in devices. This is based on the original work of Frederic Konrad. Signed-off-by: Damien Hedde --- docs/devel/clock.txt | 246 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 246 insertions(+) create mode 100644 docs/devel/clock.txt diff --git a/docs/devel/clock.txt b/docs/devel/clock.txt new file mode 100644 index 0000000000..18f79922d0 --- /dev/null +++ b/docs/devel/clock.txt @@ -0,0 +1,246 @@ + +What are clocks +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Clocks are objects representing input and output clocks of objects. They a= re +QOM objects developed for the purpose of modeling the distribution of cloc= ks in +QEMU. + +This allows us to model the clock distribution of a platform and detect +configuration errors in the clock tree such as badly configured PLL, clock +source selection or disabled clock. + +The objects are ClockIn for the input and ClockOut for the output. The QOM +names are respectively CLOCK_IN and CLOCK_OUT. + +CLOCK_IN and CLOCK_OUT are typically child of some device and created in a +similar way as gpios. ClockIn and ClockOut of different devices can be +connected together. It is possible to create a clock which is not related +to a device; for example to model a clock source of a machine. + +Here is an example of devices with clocks: + + +-------------------+ +------------------= --+ + | Device B | | Device C = | + +--------------------+ | +-----+ | | +-----+ = | + | Device A | | |Clock|>>----->>|Clock| = | + | +-----+ | | +-----+ |Out 3| | | |In 5| = | + | |Clock|>>--+-->>|Clock| +-----+ | | +-----+ = | + | |Out 1| | | | |In 2 | +-----+ | | +-----+ = | + | +-----+ | | | +-----+ |Clock|>>----->>|Clock| = | + +--------------------+ | | |Out 4| | | |In 6| = | + | | +-----+ | | +-----+ = | + | +-------------------+ +------------------= --+ + | + | +--------------------+ + | | Device D | + | | +-----+ | + +-->>|Clock| | + | |In 7| | + | +-----+ | + +--------------------+ + +Clocks are defined in include/hw/clock.h header and device related functio= ns +are defined in hw/qdev-clock.h header. + +The clock state +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The state of a clock is its frequency; it is stored as an integer represen= ting +it in Hertz. The special value of 0 is used to represent the clock being +inactive or gated. The clocks do not model the signal itself (pin toggling) +or other properties such as the duty cycle. + +Only the CLOCK_IN object keeps the value of a clock; this allows a device = to +fetch the current input frequency at any time. When an output is updated, = the +value is immediately propagated to all connected CLOCK_IN. + +Adding clocks to a device +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + +Adding clocks to a device must be done during the init method of the Device +instance. + +To add an input clock to a device, the function qdev_init_clock_in must be= used. +It takes the name, a callback, and an opaque parameter for the clock. +Output is more simple, only the name is required. Typically: +qdev_init_clock_in(DEVICE(dev), "clk_in", clk_in_callback, dev); +qdev_init_clock_out(DEVICE(dev), "clk_out"); + +Both functions return the created ClockIn/ClockOut pointer, which should b= e saved +in the device's state structure for further use. + +These objects will be automatically deleted by the QOM reference mechanism. + +Note that it is possible to create a static array describing clock inputs = and +outputs. The function qdev_init_clocks() must be called with the array as +parameter to initialize the clocks: it has the same behaviour as calling t= he +qdev_init_clock_in/out() for each clock in the array. To ease the array +construction, some macros are defined in include/hw/qdev-clock.h. +As an example, the following creates 2 clocks to a device: 1 input and 1 +output. + +/* device structure containing pointer to the clock objects */ +typedef struct MyDeviceState { + DeviceState parent_obj; + ClockIn *clk_in; + ClockOut *clk_out; +} MyDeviceState; + +/* + * callback for the input clock (see "Callback on input clock change" sect= ion + * below for more information). + */ +static void clk_in_callback(void *opaque); + +/* + * static array describing clocks: + * + a clock input named "clk_in", whose pointer is stored in clk_in + * field of a MyDeviceState structure with callback clk_in_callback. + * + a clock output named "clk_out" whose pointer is stored in clk_out + * field of a MyDeviceState structure. + */ +static const ClockPortInitArray mydev_clocks =3D { + QDEV_CLOCK_IN(MyDeviceState, clk_in, clk_in_callback), + QDEV_CLOCK_OUT(MyDeviceState, clk_out), + QDEV_CLOCK_END +}; + +/* device initialization function */ +static void mydev_init(Object *obj) +{ + /* cast to MyDeviceState */ + MyDeviceState *mydev =3D MYDEVICE(obj); + /* create and fill the pointer fields in the MyDeviceState */ + qdev_init_clocks(mydev, mydev_clocks); + [...] +} + +Connecting two clocks together +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D + +Let's say we have 2 devices A and B. A has an output clock named "clk_out"= and +B has an input clock named "clk_in". + +The clocks are connected together using the function qdev_connect_clock: +qdev_connect_clock(B, "clk_in", A, "clk_out", &error_abort); +The device which has the input must be the first argument. + +It is possible to connect several input clocks to the same output. Every +input callback will be called when the output changes. + +It is not possible to disconnect a clock or to change the clock connection +after it is done. + +Unconnected input clocks +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +A newly created input clock has a stored frequency value of 0. It means the +clock will be considered as disabled until one sets a new frequency to the +output clock it is connected to. If the clock remains unconnected it will +always keep its initial value of 0. +If this is not the wanted behaviour, clock_init_frequency should be called +on the ClockIn object during device instance init. +For example: +clk =3D qdev_init_clock_in(DEVICE(dev), "clk-in", clk_in_callback, dev); +clock_init_frequency(clk, 100 * 1000 * 1000); // default value is 100Mhz + +Using clock input frequency +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D + +A device can get the current frequency of an input using the +clock_get_frequency(). It returns the last set frequency (or the init valu= e). + +It is also possible to register a callback on input clock frequency change= s. +Here is an example: +void clock_callback(void *opaque) { + MyDeviceState *s =3D (MyDeviceState *) opaque; + /* + * opaque may not be the device state pointer, but most probably it is. + * (It depends on what is given to the qdev_init_clock_in function) + */ + + /* do something with the new frequency */ + fprintf(stdout, "device new frequency is %" PRIu64 "Hz\n", + clock_get_frequency(dev->my_clk_input)); +} + +Changing a clock output +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +A device can change its outputs using the clock_set_frequency function. It +will trigger updates on every connected inputs. + +For example, let's say that we have an output clock "clkout" and we have a +pointer to it in the device state because we did the following in init pha= se: +dev->clkout =3D qdev_init_clock_out(DEVICE(dev), "clkout"); + +Then at any time (apart from the cases listed below), it is possible to +change the clock value by doing: +clock_set_frequency(dev->clkout, 1000 * 1000 * 1000); /* 1Ghz */ +This operation must be done while holding the qemu io lock. + +One can change clocks only when it is allowed to have side effects on other +objects. In consequence, it is forbidden: ++ during migration, ++ and in the init phase of reset. + +Forwarding clocks +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Sometimes, one needs to forward, or inherit, a clock from another device. +Typically, when doing device composition, a device might expose a sub-devi= ce's +clock without interfering with it. +The function qdev_pass_clock() can be used to achieve this behaviour. Note= , that +it is possible to expose the clock under a different name. This works for = both +inputs or outputs. + +For example, if device B is a child of device A, device_a_instance_init may +do something like this: +void device_a_instance_init(Object *obj) +{ + AState *A =3D DEVICE_A(obj); + BState *B; + /* create B object as child of A */ + [...] + /* forward B's clock to A */ + qdev_pass_clock(A, "b_clk", B, "clk"); + /* + * Now A has a clock "b_clk" which forwards to + * the "clk" of its child B. + */ +} + +This function does not return any clock object. It is not possible to add +a callback on a forwarded input clock: in the above example, only B can use +the clock. + +Migration +=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Only the ClockIn object has a state. ClockOut is not concerned by migratio= n. + +In case the frequency of in input clock is needed for a device's migration, +this state must be migrated. The VMSTATE_CLOCKIN macro defines an entry to +be added in a vmstate description. + +For example, if a device has a clock input and the device state looks like: +MyDeviceState { + DeviceState parent_obj; + ClockIn *clk; +}; + +Then, to add the clock frequency to the device's migrated state, the vmsta= te +description is: +VMStateDescription my_device_vmstate =3D { + .name =3D "my_device", + .fields =3D (VMStateField[]) { + VMSTATE_CLOCKIN(clk, MyDeviceState), + VMSTATE_END_OF_LIST() + } +}; + +When adding a input clock support to an existing device, you must care abo= ut +migration compatibility. To this end, you can use the clock_init_frequency= in +a pre_load function to setup a default value in case the source vm does not +migrate the frequency. --=20 2.22.0 From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567602921447966.4317067729817; Wed, 4 Sep 2019 06:15:21 -0700 (PDT) Received: from localhost ([::1]:57686 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5V81-0005tZ-1n for importer@patchew.org; Wed, 04 Sep 2019 09:15:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53726) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Upg-0006bi-Fv for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5Upe-0005d5-QV for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:20 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:45988) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5Upa-0005Yx-5D; Wed, 04 Sep 2019 08:56:14 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id CE89196F65; Wed, 4 Sep 2019 12:56:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v7sDKq7aWXDonSFSKGhw9/2WYTabyRQyQupDcGMYv/E=; b=7UTeVXTgZdy6WLIz59AfgO9wc9TiYpWuUFoCmABJHqaox3SwgB3wOp75s+Uwu2OHOB2Lzn kXv1XAvL+tnA4XtmkcQxEpnejhsoz1BdfI2TjDuSs4L25YhT8RpufwejZToMexr1PZkUQb +ZgmYQDR1M9P6wkAFuMcjTLuXe76FhE= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:29 +0200 Message-Id: <20190904125531.27545-8-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v7sDKq7aWXDonSFSKGhw9/2WYTabyRQyQupDcGMYv/E=; b=yRTk2zxVRVo/Ktd2R4ntC76x0ww3qH7E/pXebmjmPD/zYMeFqJkPIf0XO+9+H5sX5Aex9l rYobSdObK66ufsIGgWS5nKzoUUKB2Bj20FscrsgelWv76Mn/cuqq1W4qGuXUq0Ftm3uQLv f437aaJg4Buv2Fwk1wpLbWqoXXc5rIQ= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601773; a=rsa-sha256; cv=none; b=5elN5/1A1Jer9V6rvMLTys4y8kF0+Ol1JK1lexd0G72xRzIRcIyFTKlZl1GkmVXbw5ZCT9 715y6YK1y1RL4101K7BajfU0sZ5rZftg1IHcSmPXghluwjUKQA41DXx1JFI27EJcBdt29E 7wU2SAkvJy/boWn4wWtiG1M0D3bCfVY= ARC-Authentication-Results: i=1; beetle.greensocs.com; none X-Spam: Yes Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 7/9] hw/misc/zynq_slcr: add clock generation for uarts X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Switch the slcr to multi-phase reset and add some clocks: + the main input clock (ps_clk) + the reference clock outputs for each uart (uart0 & 1) The clock frequencies are computed using the internal pll & uart configurat= ion registers and the ps_clk frequency. Signed-off-by: Damien Hedde --- hw/misc/zynq_slcr.c | 145 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 3 deletions(-) diff --git a/hw/misc/zynq_slcr.c b/hw/misc/zynq_slcr.c index b9a38272d9..fa2ac608eb 100644 --- a/hw/misc/zynq_slcr.c +++ b/hw/misc/zynq_slcr.c @@ -22,6 +22,7 @@ #include "qemu/log.h" #include "qemu/module.h" #include "hw/registerfields.h" +#include "hw/qdev-clock.h" =20 #ifndef ZYNQ_SLCR_ERR_DEBUG #define ZYNQ_SLCR_ERR_DEBUG 0 @@ -45,6 +46,12 @@ REG32(LOCKSTA, 0x00c) REG32(ARM_PLL_CTRL, 0x100) REG32(DDR_PLL_CTRL, 0x104) REG32(IO_PLL_CTRL, 0x108) +/* fields for [ARM|DDR|IO]_PLL_CTRL registers */ + FIELD(xxx_PLL_CTRL, PLL_RESET, 0, 1) + FIELD(xxx_PLL_CTRL, PLL_PWRDWN, 1, 1) + FIELD(xxx_PLL_CTRL, PLL_BYPASS_QUAL, 3, 1) + FIELD(xxx_PLL_CTRL, PLL_BYPASS_FORCE, 4, 1) + FIELD(xxx_PLL_CTRL, PLL_FPDIV, 12, 7) REG32(PLL_STATUS, 0x10c) REG32(ARM_PLL_CFG, 0x110) REG32(DDR_PLL_CFG, 0x114) @@ -64,6 +71,10 @@ REG32(SMC_CLK_CTRL, 0x148) REG32(LQSPI_CLK_CTRL, 0x14c) REG32(SDIO_CLK_CTRL, 0x150) REG32(UART_CLK_CTRL, 0x154) + FIELD(UART_CLK_CTRL, CLKACT0, 0, 1) + FIELD(UART_CLK_CTRL, CLKACT1, 1, 1) + FIELD(UART_CLK_CTRL, SRCSEL, 4, 2) + FIELD(UART_CLK_CTRL, DIVISOR, 8, 6) REG32(SPI_CLK_CTRL, 0x158) REG32(CAN_CLK_CTRL, 0x15c) REG32(CAN_MIOCLK_CTRL, 0x160) @@ -179,11 +190,106 @@ typedef struct ZynqSLCRState { MemoryRegion iomem; =20 uint32_t regs[ZYNQ_SLCR_NUM_REGS]; + + ClockIn *ps_clk; + ClockOut *uart0_ref_clk; + ClockOut *uart1_ref_clk; } ZynqSLCRState; =20 -static void zynq_slcr_reset(DeviceState *d) +/* + * return the output frequency of ARM/DDR/IO pll + * using input frequency and PLL_CTRL register + */ +static uint64_t zynq_slcr_compute_pll(uint64_t input, uint32_t ctrl_reg) +{ + uint32_t mult =3D ((ctrl_reg & R_xxx_PLL_CTRL_PLL_FPDIV_MASK) >> + R_xxx_PLL_CTRL_PLL_FPDIV_SHIFT); + + /* first, check if pll is bypassed */ + if (ctrl_reg & R_xxx_PLL_CTRL_PLL_BYPASS_FORCE_MASK) { + return input; + } + + /* is pll disabled ? */ + if (ctrl_reg & (R_xxx_PLL_CTRL_PLL_RESET_MASK | + R_xxx_PLL_CTRL_PLL_PWRDWN_MASK)) { + return 0; + } + + return input * mult; +} + +/* + * return the output frequency of a clock given: + * + the frequencies in an array corresponding to mux's indexes + * + the register xxx_CLK_CTRL value + * + enable bit index in ctrl register + * + * This function make the assumption that ctrl_reg value is organized as f= ollow: + * + bits[13:8] clock divisor + * + bits[5:4] clock mux selector (index in array) + * + bits[index] clock enable + */ +static uint64_t zynq_slcr_compute_clock(const uint64_t mux[], + uint32_t ctrl_reg, + unsigned index) +{ + uint32_t srcsel =3D extract32(ctrl_reg, 4, 2); /* bits [5:4] */ + uint32_t divisor =3D extract32(ctrl_reg, 8, 6); /* bits [13:8] */ + + /* first, check if clock is enabled */ + if (((ctrl_reg >> index) & 1u) =3D=3D 0) { + return 0; + } + + /* + * according to the Zynq technical ref. manual UG585 v1.12.2 in + * "Clocks" chapter, section 25.10.1 page 705" the range of the divisor + * is [1;63]. + * So divide the source while avoiding division-by-zero. + */ + return mux[srcsel] / (divisor ? divisor : 1u); +} + +/* + * macro helper around zynq_slcr_compute_clock to avoid repeating + * the register name. + */ +#define ZYNQ_COMPUTE_CLOCK(_state, _plls, _reg, _enable_field) \ + zynq_slcr_compute_clock((_plls), (_state)->regs[R_ ## _reg], \ + R_ ## _reg ## _ ## _enable_field ## _SHIFT) + +static void zynq_slcr_compute_clocks(ZynqSLCRState *s) +{ + uint64_t ps_clk =3D clock_get_frequency(s->ps_clk); + + /* consider all output clocks are disabled while in reset */ + if (device_is_resetting(DEVICE(s))) { + ps_clk =3D 0; + } + + uint64_t io_pll =3D zynq_slcr_compute_pll(ps_clk, s->regs[R_IO_PLL_CTR= L]); + uint64_t arm_pll =3D zynq_slcr_compute_pll(ps_clk, s->regs[R_ARM_PLL_C= TRL]); + uint64_t ddr_pll =3D zynq_slcr_compute_pll(ps_clk, s->regs[R_DDR_PLL_C= TRL]); + + uint64_t uart_mux[4] =3D {io_pll, io_pll, arm_pll, ddr_pll}; + + /* compute uartX reference clocks */ + clock_set_frequency(s->uart0_ref_clk, + ZYNQ_COMPUTE_CLOCK(s, uart_mux, UART_CLK_CTRL, CLKACT0)); + clock_set_frequency(s->uart1_ref_clk, + ZYNQ_COMPUTE_CLOCK(s, uart_mux, UART_CLK_CTRL, CLKACT1)); +} + +static void zynq_slcr_ps_clk_callback(void *opaque) +{ + ZynqSLCRState *s =3D (ZynqSLCRState *) opaque; + zynq_slcr_compute_clocks(s); +} + +static void zynq_slcr_reset_init(Object *obj, ResetType type) { - ZynqSLCRState *s =3D ZYNQ_SLCR(d); + ZynqSLCRState *s =3D ZYNQ_SLCR(obj); int i; =20 DB_PRINT("RESET\n"); @@ -277,6 +383,21 @@ static void zynq_slcr_reset(DeviceState *d) s->regs[R_DDRIOB + 12] =3D 0x00000021; } =20 +static void zynq_slcr_reset_hold(Object *obj) +{ + ZynqSLCRState *s =3D ZYNQ_SLCR(obj); + + /* will disable all output clocks */ + zynq_slcr_compute_clocks(s); +} + +static void zynq_slcr_reset_exit(Object *obj) +{ + ZynqSLCRState *s =3D ZYNQ_SLCR(obj); + + /* will compute output clocks according to ps_clk and registers */ + zynq_slcr_compute_clocks(s); +} =20 static bool zynq_slcr_check_offset(hwaddr offset, bool rnw) { @@ -409,6 +530,12 @@ static void zynq_slcr_write(void *opaque, hwaddr offse= t, qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); } break; + case R_IO_PLL_CTRL: + case R_ARM_PLL_CTRL: + case R_DDR_PLL_CTRL: + case R_UART_CLK_CTRL: + zynq_slcr_compute_clocks(s); + break; } } =20 @@ -418,6 +545,13 @@ static const MemoryRegionOps slcr_ops =3D { .endianness =3D DEVICE_NATIVE_ENDIAN, }; =20 +static const ClockPortInitArray zynq_slcr_clocks =3D { + QDEV_CLOCK_IN(ZynqSLCRState, ps_clk, zynq_slcr_ps_clk_callback), + QDEV_CLOCK_OUT(ZynqSLCRState, uart0_ref_clk), + QDEV_CLOCK_OUT(ZynqSLCRState, uart1_ref_clk), + QDEV_CLOCK_END +}; + static void zynq_slcr_init(Object *obj) { ZynqSLCRState *s =3D ZYNQ_SLCR(obj); @@ -425,6 +559,8 @@ static void zynq_slcr_init(Object *obj) memory_region_init_io(&s->iomem, obj, &slcr_ops, s, "slcr", ZYNQ_SLCR_MMIO_SIZE); sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem); + + qdev_init_clocks(DEVICE(obj), zynq_slcr_clocks); } =20 static const VMStateDescription vmstate_zynq_slcr =3D { @@ -440,9 +576,12 @@ static const VMStateDescription vmstate_zynq_slcr =3D { static void zynq_slcr_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); + ResettableClass *rc =3D RESETTABLE_CLASS(klass); =20 dc->vmsd =3D &vmstate_zynq_slcr; - dc->reset =3D zynq_slcr_reset; + rc->phases.init =3D zynq_slcr_reset_init; + rc->phases.hold =3D zynq_slcr_reset_hold; + rc->phases.exit =3D zynq_slcr_reset_exit; } =20 static const TypeInfo zynq_slcr_info =3D { --=20 2.22.0 From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567602588991214.91105532121753; Wed, 4 Sep 2019 06:09:48 -0700 (PDT) Received: from localhost ([::1]:57632 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5V2h-00010c-J3 for importer@patchew.org; Wed, 04 Sep 2019 09:09:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53742) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Uph-0006cr-DM for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5Upf-0005du-JR for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:21 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:46006) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5Upa-0005ZX-RL; Wed, 04 Sep 2019 08:56:15 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id 6ECE796F66; Wed, 4 Sep 2019 12:56:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Nxofqdzwalp2vEQOq4dTbBQ6QL66ivAoBoMwSi+rsdg=; b=jW7c9Fwu4u9XecB4vtwbsjT9Ed5qkG0GgbNPNaLFVdSpneqWb4HjErswe7n6+RJhlv3o5Y +yNCiRFIYSOfF/f9TKDZFghQYep5BbmGefYnAquBxHPKc0FuNhPwzZWgzoRy18XGP1QeHg mJZABzy/pe8oKuAyY8lLlYHrXr7F3uY= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:30 +0200 Message-Id: <20190904125531.27545-9-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Nxofqdzwalp2vEQOq4dTbBQ6QL66ivAoBoMwSi+rsdg=; b=jpMbE6TkGG5AqLaHoCwv65DLVX96ZEga/l7qE5PSFZtLxPYTACu5f3+VWvToXFKA8sflCM b2I+txe+DxdH9NjsTPKBVL4oU8mjZxtsTui8vgDQ+DOw/W6IWULXj0ZNF2EkLV7QKrqs3o h0Jb4VA8WRo/3xSePGBo+dhj93zHHYg= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601773; a=rsa-sha256; cv=none; b=GZk4B5GY3xvsSu8PiPGq9P4JsSF9YAW9G5lmiqOCS/cO004y4ZfXcELGUAkFUORGuirP9V V6spt3dHF3qyKXD/Iman8TWO+nTA+DtLOJ8VkJxtKK6C9/zDtXG04lYIo2o5jU3uWkbrrP I1BC6aQD4kbwsLR2d8uw3RKIIBcrxiw= ARC-Authentication-Results: i=1; beetle.greensocs.com; none X-Spam: Yes Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 8/9] hw/char/cadence_uart: add clock support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Switch the cadence uart to multi-phase reset and add the reference clock input. The input clock frequency is added to the migration structure. The reference clock controls the baudrate generation. If it disabled, any input characters and events are ignored. If this clock remains unconnected, the uart behaves as before (it default to a 50MHz ref clock). Signed-off-by: Damien Hedde --- hw/char/cadence_uart.c | 85 ++++++++++++++++++++++++++++++---- hw/char/trace-events | 3 ++ include/hw/char/cadence_uart.h | 1 + 3 files changed, 79 insertions(+), 10 deletions(-) diff --git a/hw/char/cadence_uart.c b/hw/char/cadence_uart.c index 0e315b2376..bae43a9679 100644 --- a/hw/char/cadence_uart.c +++ b/hw/char/cadence_uart.c @@ -31,6 +31,8 @@ #include "qemu/module.h" #include "hw/char/cadence_uart.h" #include "hw/irq.h" +#include "hw/qdev-clock.h" +#include "trace.h" =20 #ifdef CADENCE_UART_ERR_DEBUG #define DB_PRINT(...) do { \ @@ -97,7 +99,7 @@ #define LOCAL_LOOPBACK (0x2 << UART_MR_CHMODE_SH) #define REMOTE_LOOPBACK (0x3 << UART_MR_CHMODE_SH) =20 -#define UART_INPUT_CLK 50000000 +#define UART_DEFAULT_REF_CLK (50 * 1000 * 1000) =20 #define R_CR (0x00/4) #define R_MR (0x04/4) @@ -171,12 +173,15 @@ static void uart_send_breaks(CadenceUARTState *s) static void uart_parameters_setup(CadenceUARTState *s) { QEMUSerialSetParams ssp; - unsigned int baud_rate, packet_size; + unsigned int baud_rate, packet_size, input_clk; + input_clk =3D clock_get_frequency(s->refclk); =20 - baud_rate =3D (s->r[R_MR] & UART_MR_CLKS) ? - UART_INPUT_CLK / 8 : UART_INPUT_CLK; + baud_rate =3D (s->r[R_MR] & UART_MR_CLKS) ? input_clk / 8 : input_clk; + baud_rate /=3D (s->r[R_BRGR] * (s->r[R_BDIV] + 1)); + trace_cadence_uart_baudrate(baud_rate); + + ssp.speed =3D baud_rate; =20 - ssp.speed =3D baud_rate / (s->r[R_BRGR] * (s->r[R_BDIV] + 1)); packet_size =3D 1; =20 switch (s->r[R_MR] & UART_MR_PAR) { @@ -215,6 +220,13 @@ static void uart_parameters_setup(CadenceUARTState *s) } =20 packet_size +=3D ssp.data_bits + ssp.stop_bits; + if (ssp.speed =3D=3D 0) { + /* + * Avoid division-by-zero below. + * TODO: find something better + */ + ssp.speed =3D 1; + } s->char_tx_time =3D (NANOSECONDS_PER_SECOND / ssp.speed) * packet_size; qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp); } @@ -340,6 +352,11 @@ static void uart_receive(void *opaque, const uint8_t *= buf, int size) CadenceUARTState *s =3D opaque; uint32_t ch_mode =3D s->r[R_MR] & UART_MR_CHMODE; =20 + /* ignore characters when unclocked or in reset */ + if (!clock_is_enabled(s->refclk) || device_is_resetting(DEVICE(s))) { + return; + } + if (ch_mode =3D=3D NORMAL_MODE || ch_mode =3D=3D ECHO_MODE) { uart_write_rx_fifo(opaque, buf, size); } @@ -353,6 +370,11 @@ static void uart_event(void *opaque, int event) CadenceUARTState *s =3D opaque; uint8_t buf =3D '\0'; =20 + /* ignore characters when unclocked or in reset */ + if (!clock_is_enabled(s->refclk) || device_is_resetting(DEVICE(s))) { + return; + } + if (event =3D=3D CHR_EVENT_BREAK) { uart_write_rx_fifo(opaque, &buf, 1); } @@ -462,9 +484,9 @@ static const MemoryRegionOps uart_ops =3D { .endianness =3D DEVICE_NATIVE_ENDIAN, }; =20 -static void cadence_uart_reset(DeviceState *dev) +static void cadence_uart_reset_init(Object *obj, ResetType type) { - CadenceUARTState *s =3D CADENCE_UART(dev); + CadenceUARTState *s =3D CADENCE_UART(obj); =20 s->r[R_CR] =3D 0x00000128; s->r[R_IMR] =3D 0; @@ -473,6 +495,11 @@ static void cadence_uart_reset(DeviceState *dev) s->r[R_BRGR] =3D 0x0000028B; s->r[R_BDIV] =3D 0x0000000F; s->r[R_TTRIG] =3D 0x00000020; +} + +static void cadence_uart_reset_hold(Object *obj) +{ + CadenceUARTState *s =3D CADENCE_UART(obj); =20 uart_rx_reset(s); uart_tx_reset(s); @@ -491,6 +518,14 @@ static void cadence_uart_realize(DeviceState *dev, Err= or **errp) uart_event, NULL, s, NULL, true); } =20 +static void cadence_uart_refclk_update(void *opaque) +{ + CadenceUARTState *s =3D opaque; + + /* recompute uart's speed on clock change */ + uart_parameters_setup(s); +} + static void cadence_uart_init(Object *obj) { SysBusDevice *sbd =3D SYS_BUS_DEVICE(obj); @@ -500,9 +535,23 @@ static void cadence_uart_init(Object *obj) sysbus_init_mmio(sbd, &s->iomem); sysbus_init_irq(sbd, &s->irq); =20 + s->refclk =3D qdev_init_clock_in(DEVICE(obj), "refclk", + cadence_uart_refclk_update, s); + /* initialize the frequency in case the clock remains unconnected */ + clock_init_frequency(s->refclk, UART_DEFAULT_REF_CLK); + s->char_tx_time =3D (NANOSECONDS_PER_SECOND / 9600) * 10; } =20 +static int cadence_uart_pre_load(void *opaque) +{ + CadenceUARTState *s =3D opaque; + + /* the frequency will be overriden if the subsection is present */ + clock_init_frequency(s->refclk, UART_DEFAULT_REF_CLK); + return 0; +} + static int cadence_uart_post_load(void *opaque, int version_id) { CadenceUARTState *s =3D opaque; @@ -519,10 +568,21 @@ static int cadence_uart_post_load(void *opaque, int v= ersion_id) return 0; } =20 +static const VMStateDescription vmstate_cadence_uart_refclk =3D { + .name =3D "cadence_uart_refclk", + .version_id =3D 0, + .minimum_version_id =3D 0, + .fields =3D (VMStateField[]) { + VMSTATE_CLOCKIN(refclk, CadenceUARTState), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_cadence_uart =3D { .name =3D "cadence_uart", .version_id =3D 2, .minimum_version_id =3D 2, + .pre_load =3D cadence_uart_pre_load, .post_load =3D cadence_uart_post_load, .fields =3D (VMStateField[]) { VMSTATE_UINT32_ARRAY(r, CadenceUARTState, CADENCE_UART_R_MAX), @@ -535,7 +595,10 @@ static const VMStateDescription vmstate_cadence_uart = =3D { VMSTATE_UINT32(rx_wpos, CadenceUARTState), VMSTATE_TIMER_PTR(fifo_trigger_handle, CadenceUARTState), VMSTATE_END_OF_LIST() - } + }, + .subsections =3D (const VMStateDescription * []) { + &vmstate_cadence_uart_refclk, + }, }; =20 static Property cadence_uart_properties[] =3D { @@ -546,12 +609,14 @@ static Property cadence_uart_properties[] =3D { static void cadence_uart_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); + ResettableClass *rc =3D RESETTABLE_CLASS(klass); =20 dc->realize =3D cadence_uart_realize; dc->vmsd =3D &vmstate_cadence_uart; - dc->reset =3D cadence_uart_reset; dc->props =3D cadence_uart_properties; - } + rc->phases.init =3D cadence_uart_reset_init; + rc->phases.hold =3D cadence_uart_reset_hold; +} =20 static const TypeInfo cadence_uart_info =3D { .name =3D TYPE_CADENCE_UART, diff --git a/hw/char/trace-events b/hw/char/trace-events index 2ce7f2f998..502a7d0507 100644 --- a/hw/char/trace-events +++ b/hw/char/trace-events @@ -77,3 +77,6 @@ cmsdk_apb_uart_set_params(int speed) "CMSDK APB UART: par= ams set to %d 8N1" # nrf51_uart.c nrf51_uart_read(uint64_t addr, uint64_t r, unsigned int size) "addr 0x%" P= RIx64 " value 0x%" PRIx64 " size %u" nrf51_uart_write(uint64_t addr, uint64_t value, unsigned int size) "addr 0= x%" PRIx64 " value 0x%" PRIx64 " size %u" + +# hw/char/cadence_uart.c +cadence_uart_baudrate(unsigned baudrate) "baudrate %u" diff --git a/include/hw/char/cadence_uart.h b/include/hw/char/cadence_uart.h index 47cec956c4..a09afe4ed5 100644 --- a/include/hw/char/cadence_uart.h +++ b/include/hw/char/cadence_uart.h @@ -49,6 +49,7 @@ typedef struct { CharBackend chr; qemu_irq irq; QEMUTimer *fifo_trigger_handle; + ClockIn *refclk; } CadenceUARTState; =20 static inline DeviceState *cadence_uart_create(hwaddr addr, --=20 2.22.0 From nobody Thu May 2 03:00:15 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567602473038619.0957177817698; Wed, 4 Sep 2019 06:07:53 -0700 (PDT) Received: from localhost ([::1]:57602 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5V0p-0007oH-P4 for importer@patchew.org; Wed, 04 Sep 2019 09:07:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53735) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i5Upg-0006cC-Ts for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i5Upf-0005dc-Em for qemu-devel@nongnu.org; Wed, 04 Sep 2019 08:56:20 -0400 Received: from beetle.greensocs.com ([5.135.226.135]:46018) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i5Upb-0005aE-J5; Wed, 04 Sep 2019 08:56:15 -0400 Received: from crumble.bar.greensocs.com (crumble.bar.greensocs.com [172.16.11.102]) by beetle.greensocs.com (Postfix) with ESMTPS id 14C6696F67; Wed, 4 Sep 2019 12:56:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2QO3PaqNeg78Ip8/N8KO3gugdTsVuiBQPuVrNkq52dI=; b=akPs/rpGosaxnYItz5lI5UtX0QdMfoIYSChaXAcWpWTBrtFxwJpMqNAEXLV8NaaH/VYuUA goJvO32CDhRNDH+R39m14xo8iKSi0tzGakayCbt+xCAM/b5MR2kbL45uuKJC5nCyvtTUNR 4qpDoXhVqHa3XWUuZW0oMqn3lcctHis= From: Damien Hedde To: qemu-devel@nongnu.org Date: Wed, 4 Sep 2019 14:55:31 +0200 Message-Id: <20190904125531.27545-10-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190904125531.27545-1-damien.hedde@greensocs.com> References: <20190904125531.27545-1-damien.hedde@greensocs.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=greensocs.com; s=mail; t=1567601774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2QO3PaqNeg78Ip8/N8KO3gugdTsVuiBQPuVrNkq52dI=; b=RfjUNOd4T4b9oyqwQq/boFuuu8PYVfFp02wnCFEl5JNe9ARZ9D5ecu3WViYjZ06ITRreK2 GkaY1zeorKhGDjynZLv0JLwIvjEzO0MLy0PyW94mwZYvxGbdgHzBBWWsMfb94hGRXX+Wg2 VTyN1DvI9cXHFXXf/0WwIywHp2lhEXg= ARC-Seal: i=1; s=mail; d=greensocs.com; t=1567601774; a=rsa-sha256; cv=none; b=NKw41Ja1C7sguu59hwfaNfQ4gZwa6lonNLe1Ba/WxtuTEBB1mHWQvM/ogSYv/2d9xjzhZw 0BpPOcEGIh0T4Sy7KFR5+ZraeKkjl5w0mnko5ggy/xru+QrP/GTN2HoWTsWdAx9ku2X5up bt0B6btmhvv02D1Bs4849ZSKpFTr9n4= ARC-Authentication-Results: i=1; beetle.greensocs.com; none X-Spam: Yes Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 5.135.226.135 Subject: [Qemu-devel] [PATCH v6 9/9] hw/arm/xilinx_zynq: connect uart clocks to slcr X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Damien Hedde , peter.maydell@linaro.org, berrange@redhat.com, ehabkost@redhat.com, alistair@alistair23.me, mark.burton@greensocs.com, pbonzini@redhat.com, qemu-arm@nongnu.org, marcandre.lureau@redhat.com, edgar.iglesias@gmail.com, philmd@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add the connection between the slcr's output clocks and the uarts inputs. Also add the main board clock 'ps_clk', which is hard-coded to 33.33MHz (the default frequency). This clock is used to feed the slcr's input clock. Signed-off-by: Damien Hedde --- hw/arm/xilinx_zynq.c | 64 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 8 deletions(-) diff --git a/hw/arm/xilinx_zynq.c b/hw/arm/xilinx_zynq.c index 89da34808b..4d5d214ea9 100644 --- a/hw/arm/xilinx_zynq.c +++ b/hw/arm/xilinx_zynq.c @@ -33,6 +33,15 @@ #include "hw/char/cadence_uart.h" #include "hw/net/cadence_gem.h" #include "hw/cpu/a9mpcore.h" +#include "hw/qdev-clock.h" +#include "sysemu/reset.h" + +#define TYPE_ZYNQ_MACHINE MACHINE_TYPE_NAME("xilinx-zynq-a9") +#define ZYNQ_MACHINE(obj) \ + OBJECT_CHECK(ZynqMachineState, (obj), TYPE_ZYNQ_MACHINE) + +/* board base frequency: 33.333333 MHz */ +#define PS_CLK_FREQUENCY (100 * 1000 * 1000 / 3) =20 #define NUM_SPI_FLASHES 4 #define NUM_QSPI_FLASHES 2 @@ -73,6 +82,11 @@ static const int dma_irqs[8] =3D { 0xe3401000 + ARMV7_IMM16(extract32((val), 16, 16)), /* movt r1 ... */ \ 0xe5801000 + (addr) =20 +typedef struct ZynqMachineState { + MachineState parent; + ClockOut *ps_clk; +} ZynqMachineState; + static void zynq_write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info) { @@ -157,6 +171,7 @@ static inline void zynq_init_spi_flashes(uint32_t base_= addr, qemu_irq irq, =20 static void zynq_init(MachineState *machine) { + ZynqMachineState *zynq_machine =3D ZYNQ_MACHINE(machine); ram_addr_t ram_size =3D machine->ram_size; const char *kernel_filename =3D machine->kernel_filename; const char *kernel_cmdline =3D machine->kernel_cmdline; @@ -165,7 +180,7 @@ static void zynq_init(MachineState *machine) MemoryRegion *address_space_mem =3D get_system_memory(); MemoryRegion *ext_ram =3D g_new(MemoryRegion, 1); MemoryRegion *ocm_ram =3D g_new(MemoryRegion, 1); - DeviceState *dev; + DeviceState *dev, *slcr; SysBusDevice *busdev; qemu_irq pic[64]; int n; @@ -210,9 +225,17 @@ static void zynq_init(MachineState *machine) 1, 0x0066, 0x0022, 0x0000, 0x0000, 0x0555, 0x2aa, 0); =20 - dev =3D qdev_create(NULL, "xilinx,zynq_slcr"); - qdev_init_nofail(dev); - sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, 0xF8000000); + /* Create slcr, keep a pointer to connect clocks */ + slcr =3D qdev_create(NULL, "xilinx,zynq_slcr"); + qdev_init_nofail(slcr); + sysbus_mmio_map(SYS_BUS_DEVICE(slcr), 0, 0xF8000000); + + /* Create the main clock source, and feed slcr with it */ + zynq_machine->ps_clk =3D CLOCK_OUT(object_new(TYPE_CLOCK_OUT)); + object_property_add_child(OBJECT(zynq_machine), "ps_clk", + OBJECT(zynq_machine->ps_clk), &error_abort); + object_unref(OBJECT(zynq_machine->ps_clk)); + clock_connect(qdev_get_clock_in(slcr, "ps_clk"), zynq_machine->ps_clk); =20 dev =3D qdev_create(NULL, TYPE_A9MPCORE_PRIV); qdev_prop_set_uint32(dev, "num-cpu", 1); @@ -233,8 +256,12 @@ static void zynq_init(MachineState *machine) sysbus_create_simple("xlnx,ps7-usb", 0xE0002000, pic[53-IRQ_OFFSET]); sysbus_create_simple("xlnx,ps7-usb", 0xE0003000, pic[76-IRQ_OFFSET]); =20 - cadence_uart_create(0xE0000000, pic[59 - IRQ_OFFSET], serial_hd(0)); - cadence_uart_create(0xE0001000, pic[82 - IRQ_OFFSET], serial_hd(1)); + dev =3D cadence_uart_create(0xE0000000, pic[59 - IRQ_OFFSET], serial_h= d(0)); + qdev_connect_clock_out(slcr, "uart0_ref_clk", + qdev_get_clock_in(dev, "refclk"), &error_abort); + dev =3D cadence_uart_create(0xE0001000, pic[82 - IRQ_OFFSET], serial_h= d(1)); + qdev_connect_clock_out(slcr, "uart1_ref_clk", + qdev_get_clock_in(dev, "refclk"), &error_abort); =20 sysbus_create_varargs("cadence_ttc", 0xF8001000, pic[42-IRQ_OFFSET], pic[43-IRQ_OFFSET], pic[44-IRQ_OFFSET], NU= LL); @@ -315,14 +342,35 @@ static void zynq_init(MachineState *machine) arm_load_kernel(ARM_CPU(first_cpu), &zynq_binfo); } =20 -static void zynq_machine_init(MachineClass *mc) +static void zynq_reset(MachineState *machine) +{ + ZynqMachineState *zynq_machine =3D ZYNQ_MACHINE(machine); + qemu_devices_reset(); + clock_set_frequency(zynq_machine->ps_clk, PS_CLK_FREQUENCY); +} + +static void zynq_machine_class_init(ObjectClass *oc, void *data) { + MachineClass *mc =3D MACHINE_CLASS(oc); mc->desc =3D "Xilinx Zynq Platform Baseboard for Cortex-A9"; mc->init =3D zynq_init; + mc->reset =3D zynq_reset; mc->max_cpus =3D 1; mc->no_sdcard =3D 1; mc->ignore_memory_transaction_failures =3D true; mc->default_cpu_type =3D ARM_CPU_TYPE_NAME("cortex-a9"); } =20 -DEFINE_MACHINE("xilinx-zynq-a9", zynq_machine_init) +static const TypeInfo zynq_machine_type =3D { + .name =3D TYPE_ZYNQ_MACHINE, + .parent =3D TYPE_MACHINE, + .class_init =3D zynq_machine_class_init, + .instance_size =3D sizeof(ZynqMachineState), +}; + +static void zynq_machine_register_types(void) +{ + type_register_static(&zynq_machine_type); +} + +type_init(zynq_machine_register_types) --=20 2.22.0