From patchwork Fri Feb 19 13:17:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8360971 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id A740CC0553 for ; Fri, 19 Feb 2016 13:19:55 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CBAC420412 for ; Fri, 19 Feb 2016 13:19:53 +0000 (UTC) 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.kernel.org (Postfix) with ESMTPS id E75CF203F7 for ; Fri, 19 Feb 2016 13:19:52 +0000 (UTC) Received: from localhost ([::1]:52198 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWkyO-0004VG-6c for patchwork-qemu-devel@patchwork.kernel.org; Fri, 19 Feb 2016 08:19:52 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53171) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWkwC-0000yo-Ix for qemu-devel@nongnu.org; Fri, 19 Feb 2016 08:17:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aWkwB-00066d-6F for qemu-devel@nongnu.org; Fri, 19 Feb 2016 08:17:36 -0500 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]:38252) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWkwA-00064C-Sz for qemu-devel@nongnu.org; Fri, 19 Feb 2016 08:17:35 -0500 Received: by mail-wm0-x230.google.com with SMTP id a4so70266189wme.1 for ; Fri, 19 Feb 2016 05:17:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8sJvUm4R5Cl12SbCEQyTDnySf3Bx2FLPV8JM909X9+A=; b=GZvlKaabvi63AOlOeVQam7Ld/deR0ZzVe/8rn0hEqHZxdKewz+Sr8NGFBZlFYHcfuo 5bs5b7BP5TclpoVqC/sknr21zMBWU8PFKk0GOJ8bDoOIwpA0MNVb7mFYA3o5oglchxL8 TQcTdDs2WiVIcESmUmzU2o86Ne1Dksrw8tSy8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=8sJvUm4R5Cl12SbCEQyTDnySf3Bx2FLPV8JM909X9+A=; b=hfCavSJrFZ6SqaRoqIuD7dzLZrkm0HSXHA41DceIv0TdgXWiGdYuDnwVCd7usTDfE1 XIid4d320fMwAHr2BmZ1jfjhZ5DoY0ebZI9abby7XF7RlTzNsjnyqMHG2rh5i4I1Oh7N K53lgMqq81qG3S0Cn3X/9aVYzFh2efPCvg0hRE3drQxIZmEhXjLvrx+N25PVvAEVtTvA kI9wilmGRfDvLXFb7538ucP4RV59oyOVngvJASWX5dyRfvVGOiaRmIagh8fv0UK/KWAZ L78GjTTjMDhJmakcZ0u3c/fMWn/EM/ZMUYtzdd6C1GJMkKMHPCdtt0YqeAzoYfgMkbOZ GkOA== X-Gm-Message-State: AG10YORT3HxPiQT72TwpkUNd2OqtIztpOOkUMTAIZuTaTsY20Q8kFVFW4HTB/+4wjaMoLWeN X-Received: by 10.194.84.2 with SMTP id u2mr10051575wjy.61.1455887853271; Fri, 19 Feb 2016 05:17:33 -0800 (PST) Received: from midway01-04-00.lavalab ([81.128.185.50]) by smtp.gmail.com with ESMTPSA id bg1sm11229846wjc.27.2016.02.19.05.17.30 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 19 Feb 2016 05:17:31 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, alex.williamson@redhat.com, david@gibson.dropbear.id.au Date: Fri, 19 Feb 2016 13:17:11 +0000 Message-Id: <1455887833-7306-7-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1455887833-7306-1-git-send-email-eric.auger@linaro.org> References: <1455887833-7306-1-git-send-email-eric.auger@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::230 Cc: thomas.lendacky@amd.com, thuth@redhat.com, b.reynal@virtualopensystems.com, patches@linaro.org, crosthwaitepeter@gmail.com, suravee.suthikulpanit@amd.com, pbonzini@redhat.com, alex.bennee@linaro.org, christoffer.dall@linaro.org Subject: [Qemu-devel] [PATCH v8 6/8] hw/arm/sysbus-fdt: helpers for clock node generation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Some passthrough'ed devices depend on clock nodes. Those need to be generated in the guest device tree. This patch introduces some helpers to build a clock node from information retrieved in the host device tree. - copy_properties_from_host copies properties from a host device tree node to a guest device tree node - fdt_build_clock_node builds a guest clock node and checks the host fellow clock is a fixed one. fdt_build_clock_node will become static as soon as it gets used. A dummy pre-declaration is needed for compilation of this patch. Signed-off-by: Eric Auger Reviewed-by: Peter Maydell --- v5 -> v6: - replace self-asserts - add Peter's R-b v4 -> v5: - renamed inherit_properties v1 -> v2: - inherit properties now outputs an error message in case qemu_fdt_getprop fails for an existing optional property - no hardcoded fixed buffer length - fdt_build_clock_node becomes void and auto-asserts on error - use boolean values when defining the clock properties RFC -> v1: - use the new proto of qemu_fdt_getprop - remove newline in error_report - fix some style issues --- hw/arm/sysbus-fdt.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/hw/arm/sysbus-fdt.c b/hw/arm/sysbus-fdt.c index 68a3de5..8575cfe 100644 --- a/hw/arm/sysbus-fdt.c +++ b/hw/arm/sysbus-fdt.c @@ -22,6 +22,7 @@ */ #include "qemu/osdep.h" +#include #include "hw/arm/sysbus-fdt.h" #include "qemu/error-report.h" #include "sysemu/device_tree.h" @@ -57,6 +58,125 @@ typedef struct NodeCreationPair { int (*add_fdt_node_fn)(SysBusDevice *sbdev, void *opaque); } NodeCreationPair; +/* helpers */ + +typedef struct HostProperty { + const char *name; + bool optional; +} HostProperty; + +/** + * copy_properties_from_host + * + * copies properties listed in an array from host device tree to + * guest device tree. If a non optional property is not found, the + * function asserts. An optional property is ignored if not found + * in the host device tree. + * @props: array of HostProperty to copy + * @nb_props: number of properties in the array + * @host_dt: host device tree blob + * @guest_dt: guest device tree blob + * @node_path: host dt node path where the property is supposed to be + found + * @nodename: guest node name the properties should be added to + */ +static void copy_properties_from_host(HostProperty *props, int nb_props, + void *host_fdt, void *guest_fdt, + char *node_path, char *nodename) +{ + int i, prop_len; + const void *r; + Error *err = NULL; + + for (i = 0; i < nb_props; i++) { + r = qemu_fdt_getprop(host_fdt, node_path, + props[i].name, + &prop_len, + props[i].optional ? &err : &error_fatal); + if (r) { + qemu_fdt_setprop(guest_fdt, nodename, + props[i].name, r, prop_len); + } else { + if (prop_len != -FDT_ERR_NOTFOUND) { + /* optional property not returned although property exists */ + error_report_err(err); + } else { + error_free(err); + } + } + } +} + +/* clock properties whose values are copied/pasted from host */ +static HostProperty clock_copied_properties[] = { + {"compatible", false}, + {"#clock-cells", false}, + {"clock-frequency", true}, + {"clock-output-names", true}, +}; + +/** + * fdt_build_clock_node + * + * Build a guest clock node, used as a dependency from a passthrough'ed + * device. Most information are retrieved from the host clock node. + * Also check the host clock is a fixed one. + * + * @host_fdt: host device tree blob from which info are retrieved + * @guest_fdt: guest device tree blob where the clock node is added + * @host_phandle: phandle of the clock in host device tree + * @guest_phandle: phandle to assign to the guest node + */ +void fdt_build_clock_node(void *host_fdt, void *guest_fdt, + uint32_t host_phandle, + uint32_t guest_phandle); +void fdt_build_clock_node(void *host_fdt, void *guest_fdt, + uint32_t host_phandle, + uint32_t guest_phandle) +{ + char *node_path = NULL; + char *nodename; + const void *r; + int ret, node_offset, prop_len, path_len = 16; + + node_offset = fdt_node_offset_by_phandle(host_fdt, host_phandle); + if (node_offset <= 0) { + error_setg(&error_fatal, + "not able to locate clock handle %d in host device tree", + host_phandle); + } + node_path = g_malloc(path_len); + while ((ret = fdt_get_path(host_fdt, node_offset, node_path, path_len)) + == -FDT_ERR_NOSPACE) { + path_len += 16; + node_path = g_realloc(node_path, path_len); + } + if (ret < 0) { + error_setg(&error_fatal, + "not able to retrieve node path for clock handle %d", + host_phandle); + } + + r = qemu_fdt_getprop(host_fdt, node_path, "compatible", &prop_len, + &error_fatal); + if (strcmp(r, "fixed-clock")) { + error_setg(&error_fatal, + "clock handle %d is not a fixed clock", host_phandle); + } + + nodename = strrchr(node_path, '/'); + qemu_fdt_add_subnode(guest_fdt, nodename); + + copy_properties_from_host(clock_copied_properties, + ARRAY_SIZE(clock_copied_properties), + host_fdt, guest_fdt, + node_path, nodename); + + qemu_fdt_setprop_cell(guest_fdt, nodename, "phandle", guest_phandle); + + g_free(node_path); +} + /* Device Specific Code */ /**