From patchwork Fri Feb 7 17:43:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 24304 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qa0-f70.google.com (mail-qa0-f70.google.com [209.85.216.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id E5DDE2096A for ; Fri, 7 Feb 2014 17:43:35 +0000 (UTC) Received: by mail-qa0-f70.google.com with SMTP id ii20sf7363582qab.9 for ; Fri, 07 Feb 2014 09:43:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=9CVzbBqB2BP51z23WZ6Ws++UFOn7l43WtO1ayNF1I/c=; b=MXRg+tthwMND2Hr2X1UnHLWIU27DKMMllTHyaJ+hLdsNocE8aQxjc3E2nZ9ssjhudy Ksp0kU2N/xQWm3Yq5Sn6+RR1FcQIDmheiNOi1gZ12ceg+xx7b3Kf6i8p+2v+OblqXn8V XNL7mcy26bekm4KR0EoGDMPFGrl8vd5zgIcIegNuATy56KZJ++GOJ+Anr1se503Wl5Dx mcKm+4RB0B4y3WJPuUdNnNOrOdiVytDEKr6TLEvAsqVUGYxzAVVa7RDrT/5GqOqNhowb 8QYRTUB3nD5GaDYaP3Oe5runYMNuSbKqO/s4mN3jLOs+af1UHhbRrxSrOGLJHi2yt+VS zOqw== X-Gm-Message-State: ALoCoQllU9OW6wDH7mv1SUQkX4LIu5KsKsjjDIt1P/qPBTTDGT9aTmM1K4WiR5smwgeVoLF1Sop6 X-Received: by 10.52.179.138 with SMTP id dg10mr5476975vdc.2.1391795015092; Fri, 07 Feb 2014 09:43:35 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.108.135 with SMTP id j7ls1111668qgf.88.gmail; Fri, 07 Feb 2014 09:43:35 -0800 (PST) X-Received: by 10.52.181.199 with SMTP id dy7mr911543vdc.43.1391795014864; Fri, 07 Feb 2014 09:43:34 -0800 (PST) Received: from mail-ve0-f180.google.com (mail-ve0-f180.google.com [209.85.128.180]) by mx.google.com with ESMTPS id tt2si1549061vdc.9.2014.02.07.09.43.34 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 07 Feb 2014 09:43:34 -0800 (PST) Received-SPF: neutral (google.com: 209.85.128.180 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.180; Received: by mail-ve0-f180.google.com with SMTP id db12so2953959veb.25 for ; Fri, 07 Feb 2014 09:43:34 -0800 (PST) X-Received: by 10.58.90.202 with SMTP id by10mr11086768veb.6.1391795014688; Fri, 07 Feb 2014 09:43:34 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp123591vcz; Fri, 7 Feb 2014 09:43:33 -0800 (PST) X-Received: by 10.14.179.73 with SMTP id g49mr5644391eem.71.1391795011478; Fri, 07 Feb 2014 09:43:31 -0800 (PST) Received: from mail-ea0-f178.google.com (mail-ea0-f178.google.com [209.85.215.178]) by mx.google.com with ESMTPS id w1si9552035eeo.149.2014.02.07.09.43.30 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 07 Feb 2014 09:43:31 -0800 (PST) Received-SPF: neutral (google.com: 209.85.215.178 is neither permitted nor denied by best guess record for domain of julien.grall@linaro.org) client-ip=209.85.215.178; Received: by mail-ea0-f178.google.com with SMTP id a15so1755324eae.9 for ; Fri, 07 Feb 2014 09:43:30 -0800 (PST) X-Received: by 10.14.202.136 with SMTP id d8mr18200499eeo.46.1391795010884; Fri, 07 Feb 2014 09:43:30 -0800 (PST) Received: from belegaer.uk.xensource.com. ([185.25.64.249]) by mx.google.com with ESMTPSA id 8sm18849063eef.1.2014.02.07.09.43.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Feb 2014 09:43:29 -0800 (PST) From: Julien Grall To: xen-devel@lists.xenproject.org Cc: patches@linaro.org, ian.campbell@citrix.com, tim@xen.org, stefano.stabellini@citrix.com, Julien Grall Subject: [RFC for-4.5 05/12] xen/dts: Add dt_parse_phandle_with_args and dt_parse_phandle Date: Fri, 7 Feb 2014 17:43:04 +0000 Message-Id: <1391794991-5919-6-git-send-email-julien.grall@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1391794991-5919-1-git-send-email-julien.grall@linaro.org> References: <1391794991-5919-1-git-send-email-julien.grall@linaro.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.180 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 Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Code adapted from linux drivers/of/base.c (commit ef42c58). Signed-off-by: Julien Grall --- 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..37a025a 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 */ /*