From patchwork Mon Sep 17 08:40:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Hedde X-Patchwork-Id: 10602227 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 49D516CB for ; Mon, 17 Sep 2018 08:47:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 367AC295CA for ; Mon, 17 Sep 2018 08:47:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 29C7929651; Mon, 17 Sep 2018 08:47:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 68DBE295CA for ; Mon, 17 Sep 2018 08:46:59 +0000 (UTC) Received: from localhost ([::1]:34457 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g1pBK-0001tY-Fs for patchwork-qemu-devel@patchwork.kernel.org; Mon, 17 Sep 2018 04:46:58 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46501) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g1p5x-0006Am-VO for qemu-devel@nongnu.org; Mon, 17 Sep 2018 04:41:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g1p5w-0005ca-FJ for qemu-devel@nongnu.org; Mon, 17 Sep 2018 04:41:25 -0400 Received: from greensocs.com ([193.104.36.180]:51710) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g1p5o-0005TH-Vi; Mon, 17 Sep 2018 04:41:17 -0400 Received: from localhost (localhost [127.0.0.1]) by greensocs.com (Postfix) with ESMTP id CF794521AB6; Mon, 17 Sep 2018 10:41:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=greensocs.com; s=mail; t=1537173670; bh=ad+IoHRlAKMY+vuSKfg4+m9rK/Mw7VhSQZL3cL+Z0BM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=zKMSiVbporfQbWVMpm8LehmvQjYT2kngLpDsK5uuqk5lNaZIj4bgkoskwLdl1LNiq k+kHdC7FcJg6IxbGrrwGhxIIDxyGIhWIaWVuGEtxSR5lSm2T2SKcHzg3An47Wk6BaQ fxkN94a9ti+TnxDMHJGgJqZk8Fx48eEs+rdNujQ0= X-Virus-Scanned: amavisd-new at greensocs.com Authentication-Results: gs-01.greensocs.com (amavisd-new); dkim=pass (1024-bit key) header.d=greensocs.com header.b=34Lt9d1U; dkim=pass (1024-bit key) header.d=greensocs.com header.b=34Lt9d1U Received: from greensocs.com ([127.0.0.1]) by localhost (gs-01.greensocs.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WEFZO5GAPU2j; Mon, 17 Sep 2018 10:41:10 +0200 (CEST) Received: by greensocs.com (Postfix, from userid 998) id 5983D2386FB; Mon, 17 Sep 2018 10:41:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=greensocs.com; s=mail; t=1537173669; bh=ad+IoHRlAKMY+vuSKfg4+m9rK/Mw7VhSQZL3cL+Z0BM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=34Lt9d1Up3ApSRaVWLeDTG10VgQgIzNZvuK511Tbgf6fY2ekHelwjzx6xEOP2fTvc FkRpuAIbRL0ms8tKYwDiHG/dffy0SzVyvhmJya0kGcDoVEjFOuDaazr5VCyr8T0gcT CmrcjKezVrAvnuZfv6Gt8NPxPTLKLxMcNWhuZhNk= Received: from kouign-amann.hive.antfield.fr (antfield.tima.u-ga.fr [147.171.129.253]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: damien.hedde@greensocs.com) by greensocs.com (Postfix) with ESMTPSA id D871323878C; Mon, 17 Sep 2018 10:41:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=greensocs.com; s=mail; t=1537173669; bh=ad+IoHRlAKMY+vuSKfg4+m9rK/Mw7VhSQZL3cL+Z0BM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=34Lt9d1Up3ApSRaVWLeDTG10VgQgIzNZvuK511Tbgf6fY2ekHelwjzx6xEOP2fTvc FkRpuAIbRL0ms8tKYwDiHG/dffy0SzVyvhmJya0kGcDoVEjFOuDaazr5VCyr8T0gcT CmrcjKezVrAvnuZfv6Gt8NPxPTLKLxMcNWhuZhNk= From: damien.hedde@greensocs.com To: qemu-devel@nongnu.org Date: Mon, 17 Sep 2018 10:40:10 +0200 Message-Id: <20180917084016.12750-5-damien.hedde@greensocs.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180917084016.12750-1-damien.hedde@greensocs.com> References: <20180917084016.12750-1-damien.hedde@greensocs.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 193.104.36.180 Subject: [Qemu-devel] [PATCH v4 04/10] qdev-clock: introduce an init array to ease the device construction X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: edgar.iglesias@xilinx.com, peter.maydell@linaro.org, alistair@alistair23.me, mark.burton@greensocs.com, saipava@xilinx.com, qemu-arm@nongnu.org, Damien Hedde , pbonzini@redhat.com, luc.michel@greensocs.com, fred.konrad@greensocs.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Damien Hedde 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 --- include/hw/qdev-clock.h | 67 +++++++++++++++++++++++++++++++++++++++++ hw/core/qdev-clock.c | 26 ++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/include/hw/qdev-clock.h b/include/hw/qdev-clock.h index 324eea6bcc..a5da8878af 100644 --- a/include/hw/qdev-clock.h +++ b/include/hw/qdev-clock.h @@ -59,4 +59,71 @@ void qdev_clock_connect(DeviceState *dev, const char *name, DeviceState *driver, const char *driver_name, Error **errp); +/** + * ClockInitElem: + * @name: name of the clock (can't be NULL) + * @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 clock pointer in device'state + * structure (0 means unused) + */ +struct ClockPortInitElem { + const char *name; + bool 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(_output, _type, _devstate, _field, _callback) { \ + .name = (stringify(_field)), \ + .output = _output, \ + .callback = _callback, \ + .offset = clock_offset_value(_type, _devstate, _field), \ +} + +/** + * QDEV_CLOCK_(IN|OUT): + * @_devstate: structure type. @dev argument of qdev_init_clocks below must 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 device + * 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 argument + */ +#define QDEV_CLOCK_IN_NOFIELD(_name, _callback) { \ + .name = _name, \ + .output = false, \ + .callback = _callback, \ + .offset = 0, \ +} + +#define QDEV_CLOCK_END { .name = 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 */ diff --git a/hw/core/qdev-clock.c b/hw/core/qdev-clock.c index cb7dba3f75..78d2509e04 100644 --- a/hw/core/qdev-clock.c +++ b/hw/core/qdev-clock.c @@ -138,3 +138,29 @@ void qdev_clock_connect(DeviceState *dev, const char *name, clock_connect(ncl->in , drv_ncl->out); } + +void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks) +{ + const struct ClockPortInitElem *elem; + + assert(dev); + assert(clocks); + + for (elem = &clocks[0]; elem->name != NULL; elem++) { + /* offset cannot be inside the DeviceState part */ + assert(elem->offset == 0 || elem->offset > sizeof(DeviceState)); + if (elem->output) { + ClockOut *clk; + clk = qdev_init_clock_out(dev, elem->name); + if (elem->offset) { + *(ClockOut **)(((void *) dev) + elem->offset) = clk; + } + } else { + ClockIn *clk; + clk = qdev_init_clock_in(dev, elem->name, elem->callback, dev); + if (elem->offset) { + *(ClockIn **)(((void *) dev) + elem->offset) = clk; + } + } + } +}