From patchwork Sun Feb 23 22:16:22 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 25148 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ie0-f199.google.com (mail-ie0-f199.google.com [209.85.223.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1CC6A21406 for ; Sun, 23 Feb 2014 22:18:08 +0000 (UTC) Received: by mail-ie0-f199.google.com with SMTP id rl12sf19137300iec.2 for ; Sun, 23 Feb 2014 14:18:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:cc:subject:precedence:list-id:list-unsubscribe:list-post :list-help:list-subscribe:mime-version:sender:errors-to :x-original-sender:x-original-authentication-results:mailing-list :list-archive:content-type:content-transfer-encoding; bh=iNnk0moHLX3ltkKvVR7LUkIEZ6Z5qcxxEpCCKPIhv7w=; b=Mqbj2E8XlAzUK1GRmjr611VB75IIri1pq4NewFkl+eLIViGMwnbWtYdB37IEwwFvhK Lbt5FfTDqI5rUvg0wWDTsqoK32biC5KrmoDxCvBIQmpL4AiY222Eu616Ba7RmUok/ueW V+mTUMdulB0XLHqKsXCQD1zVkKgJQYWOSQxS7XVqclt0Qf7zYzvcGinz2xlZTc7IO+TC oJANERY2KdprhSmyjofaVrvKX5JqhJdzCI7j+V4N93zcZ8G1IbfDa2cA48jlvhLS8Q6K 425dOe5XtOI6+sQXLa/LKWeEKEXY0tqeRqmttEyXYldYhIGMWjoE5IGVEACGtMFULFoJ bsWQ== X-Gm-Message-State: ALoCoQkef7ov0nBcUkdOTa7h99E42unJ8QdhcjZMtgxGJRvch5B17/JrL8b9DmqvuTnb0Q/IAzzy X-Received: by 10.182.153.68 with SMTP id ve4mr7770556obb.39.1393193887614; Sun, 23 Feb 2014 14:18:07 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.29.102 with SMTP id a93ls1662941qga.33.gmail; Sun, 23 Feb 2014 14:18:07 -0800 (PST) X-Received: by 10.220.67.18 with SMTP id p18mr10734784vci.14.1393193887463; Sun, 23 Feb 2014 14:18:07 -0800 (PST) Received: from mail-ve0-f172.google.com (mail-ve0-f172.google.com [209.85.128.172]) by mx.google.com with ESMTPS id at8si5306845vec.75.2014.02.23.14.18.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 23 Feb 2014 14:18:07 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.172 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.172; Received: by mail-ve0-f172.google.com with SMTP id jz11so3217594veb.31 for ; Sun, 23 Feb 2014 14:18:07 -0800 (PST) X-Received: by 10.52.23.68 with SMTP id k4mr9049307vdf.24.1393193887359; Sun, 23 Feb 2014 14:18:07 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.174.196 with SMTP id u4csp20470vcz; Sun, 23 Feb 2014 14:18:07 -0800 (PST) X-Received: by 10.52.116.171 with SMTP id jx11mr3025132vdb.88.1393193886725; Sun, 23 Feb 2014 14:18:06 -0800 (PST) Received: from lists.xen.org (lists.xen.org. [50.57.142.19]) by mx.google.com with ESMTPS id zp2si5305509vec.84.2014.02.23.14.18.06 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Sun, 23 Feb 2014 14:18:06 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of xen-devel-bounces@lists.xen.org designates 50.57.142.19 as permitted sender) client-ip=50.57.142.19; Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WHhLr-0006q7-CI; Sun, 23 Feb 2014 22:16:47 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WHhLp-0006pY-LL for xen-devel@lists.xenproject.org; Sun, 23 Feb 2014 22:16:45 +0000 Received: from [85.158.139.211:34693] by server-11.bemta-5.messagelabs.com id D2/0C-23886-C437A035; Sun, 23 Feb 2014 22:16:44 +0000 X-Env-Sender: julien.grall@linaro.org X-Msg-Ref: server-8.tower-206.messagelabs.com!1393193803!5717360!1 X-Originating-IP: [74.125.83.54] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 6.9.16; banners=-,-,- X-VirusChecked: Checked Received: (qmail 7530 invoked from network); 23 Feb 2014 22:16:43 -0000 Received: from mail-ee0-f54.google.com (HELO mail-ee0-f54.google.com) (74.125.83.54) by server-8.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 23 Feb 2014 22:16:43 -0000 Received: by mail-ee0-f54.google.com with SMTP id c41so1879469eek.27 for ; Sun, 23 Feb 2014 14:16:43 -0800 (PST) X-Received: by 10.15.76.135 with SMTP id n7mr5974163eey.36.1393193803757; Sun, 23 Feb 2014 14:16:43 -0800 (PST) Received: from belegaer.uk.xensource.com. ([185.25.64.249]) by mx.google.com with ESMTPSA id g1sm55994749eet.6.2014.02.23.14.16.42 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 23 Feb 2014 14:16:42 -0800 (PST) From: Julien Grall To: xen-devel@lists.xenproject.org Date: Sun, 23 Feb 2014 22:16:22 +0000 Message-Id: <1393193792-20008-6-git-send-email-julien.grall@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1393193792-20008-1-git-send-email-julien.grall@linaro.org> References: <1393193792-20008-1-git-send-email-julien.grall@linaro.org> Cc: stefano.stabellini@citrix.com, Julien Grall , tim@xen.org, ian.campbell@citrix.com Subject: [Xen-devel] [PATCH v2 05/15] xen/dts: Add dt_parse_phandle_with_args and dt_parse_phandle X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: julien.grall@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.172 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Archive: Code adapted from linux drivers/of/base.c (commit ef42c58). Signed-off-by: Julien Grall --- Changes in v2: - Remove hard tabs in dt_parse_phandle --- xen/common/device_tree.c | 151 ++++++++++++++++++++++++++++++++++++++++- xen/include/xen/device_tree.h | 54 +++++++++++++++ 2 files changed, 203 insertions(+), 2 deletions(-) diff --git a/xen/common/device_tree.c b/xen/common/device_tree.c index ccdb7ff..564f2bb 100644 --- a/xen/common/device_tree.c +++ b/xen/common/device_tree.c @@ -1090,9 +1090,9 @@ int dt_device_get_address(const struct dt_device_node *dev, int index, * * Returns a node pointer. */ -static const struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle) +static struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle) { - const struct dt_device_node *np; + struct dt_device_node *np; dt_for_each_device_node(dt_host, np) if ( np->phandle == handle ) @@ -1477,6 +1477,153 @@ bool_t dt_device_is_available(const struct dt_device_node *device) return 0; } +static int __dt_parse_phandle_with_args(const struct dt_device_node *np, + const char *list_name, + const char *cells_name, + int cell_count, int index, + struct dt_phandle_args *out_args) +{ + const __be32 *list, *list_end; + int rc = 0, cur_index = 0; + u32 size, count = 0; + struct dt_device_node *node = NULL; + dt_phandle phandle; + + /* Retrieve the phandle list property */ + list = dt_get_property(np, list_name, &size); + if ( !list ) + return -ENOENT; + list_end = list + size / sizeof(*list); + + /* Loop over the phandles until all the requested entry is found */ + while ( list < list_end ) + { + rc = -EINVAL; + count = 0; + + /* + * If phandle is 0, then it is an empty entry with no + * arguments. Skip forward to the next entry. + * */ + phandle = be32_to_cpup(list++); + if ( phandle ) + { + /* + * Find the provider node and parse the #*-cells + * property to determine the argument length. + * + * This is not needed if the cell count is hard-coded + * (i.e. cells_name not set, but cell_count is set), + * except when we're going to return the found node + * below. + */ + if ( cells_name || cur_index == index ) + { + node = dt_find_node_by_phandle(phandle); + if ( !node ) + { + dt_printk(XENLOG_ERR "%s: could not find phandle\n", + np->full_name); + goto err; + } + } + + if ( cells_name ) + { + if ( !dt_property_read_u32(node, cells_name, &count) ) + { + dt_printk("%s: could not get %s for %s\n", + np->full_name, cells_name, node->full_name); + goto err; + } + } + else + count = cell_count; + + /* + * Make sure that the arguments actually fit in the + * remaining property data length + */ + if ( list + count > list_end ) + { + dt_printk(XENLOG_ERR "%s: arguments longer than property\n", + np->full_name); + goto err; + } + } + + /* + * All of the error cases above bail out of the loop, so at + * this point, the parsing is successful. If the requested + * index matches, then fill the out_args structure and return, + * or return -ENOENT for an empty entry. + */ + rc = -ENOENT; + if ( cur_index == index ) + { + if (!phandle) + goto err; + + if ( out_args ) + { + int i; + + WARN_ON(count > MAX_PHANDLE_ARGS); + if (count > MAX_PHANDLE_ARGS) + count = MAX_PHANDLE_ARGS; + out_args->np = node; + out_args->args_count = count; + for ( i = 0; i < count; i++ ) + out_args->args[i] = be32_to_cpup(list++); + } + + /* Found it! return success */ + return 0; + } + + node = NULL; + list += count; + cur_index++; + } + + /* + * Returning result will be one of: + * -ENOENT : index is for empty phandle + * -EINVAL : parsing error on data + * [1..n] : Number of phandle (count mode; when index = -1) + */ + rc = index < 0 ? cur_index : -ENOENT; +err: + return rc; +} + +struct dt_device_node *dt_parse_phandle(const struct dt_device_node *np, + const char *phandle_name, int index) +{ + struct dt_phandle_args args; + + if (index < 0) + return NULL; + + if (__dt_parse_phandle_with_args(np, phandle_name, NULL, 0, + index, &args)) + return NULL; + + return args.np; +} + + +int dt_parse_phandle_with_args(const struct dt_device_node *np, + const char *list_name, + const char *cells_name, int index, + struct dt_phandle_args *out_args) +{ + if ( index < 0 ) + return -EINVAL; + return __dt_parse_phandle_with_args(np, list_name, cells_name, 0, + index, out_args); +} + /** * unflatten_dt_node - Alloc and populate a device_node from the flat tree * @fdt: The parent device tree blob diff --git a/xen/include/xen/device_tree.h b/xen/include/xen/device_tree.h index 7c075d9..d429e60 100644 --- a/xen/include/xen/device_tree.h +++ b/xen/include/xen/device_tree.h @@ -112,6 +112,13 @@ struct dt_device_node { }; +#define MAX_PHANDLE_ARGS 16 +struct dt_phandle_args { + struct dt_device_node *np; + int args_count; + uint32_t args[MAX_PHANDLE_ARGS]; +}; + /** * IRQ line type. * @@ -621,6 +628,53 @@ void dt_set_range(__be32 **cellp, const struct dt_device_node *np, void dt_get_range(const __be32 **cellp, const struct dt_device_node *np, u64 *address, u64 *size); +/** + * dt_parse_phandle - Resolve a phandle property to a device_node pointer + * @np: Pointer to device node holding phandle property + * @phandle_name: Name of property holding a phandle value + * @index: For properties holding a table of phandles, this is the index into + * the table + * + * Returns the device_node pointer. + */ +struct dt_device_node *dt_parse_phandle(const struct dt_device_node *np, + const char *phandle_name, + int index); + +/** + * dt_parse_phandle_with_args() - Find a node pointed by phandle in a list + * @np: pointer to a device tree node containing a list + * @list_name: property name that contains a list + * @cells_name: property name that specifies phandles' arguments count + * @index: index of a phandle to parse out + * @out_args: optional pointer to output arguments structure (will be filled) + * + * This function is useful to parse lists of phandles and their arguments. + * Returns 0 on success and fills out_args, on error returns appropriate + * errno value. + * + * Example: + * + * phandle1: node1 { + * #list-cells = <2>; + * } + * + * phandle2: node2 { + * #list-cells = <1>; + * } + * + * node3 { + * list = <&phandle1 1 2 &phandle2 3>; + * } + * + * To get a device_node of the `node2' node you may call this: + * dt_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); + */ +int dt_parse_phandle_with_args(const struct dt_device_node *np, + const char *list_name, + const char *cells_name, int index, + struct dt_phandle_args *out_args); + #endif /* __XEN_DEVICE_TREE_H */ /*