From patchwork Thu Apr 11 09:12:33 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 2428041 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) by patchwork1.kernel.org (Postfix) with ESMTP id 1EFD73FD8C for ; Thu, 11 Apr 2013 12:15:55 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UQEzm-0002V5-OW; Thu, 11 Apr 2013 10:44:48 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UQDaS-00026o-E0; Thu, 11 Apr 2013 09:14:32 +0000 Received: from service87.mimecast.com ([91.220.42.44]) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UQDaN-000262-T7 for linux-arm-kernel@lists.infradead.org; Thu, 11 Apr 2013 09:14:29 +0000 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.21]) by service87.mimecast.com; Thu, 11 Apr 2013 10:14:16 +0100 Received: from e106331-lin.cambridge.arm.com ([10.1.255.212]) by cam-owa1.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.0); Thu, 11 Apr 2013 10:14:12 +0100 From: Mark Rutland To: linux-arm-kernel@lists.infradead.org Subject: [RFC PATCH 02/11] arm: add functions to parse cpu affinity from dt Date: Thu, 11 Apr 2013 10:12:33 +0100 Message-Id: <1365671562-2403-3-git-send-email-mark.rutland@arm.com> X-Mailer: git-send-email 1.8.1.1 In-Reply-To: <1365671562-2403-1-git-send-email-mark.rutland@arm.com> References: <1365671562-2403-1-git-send-email-mark.rutland@arm.com> X-OriginalArrivalTime: 11 Apr 2013 09:14:12.0323 (UTC) FILETIME=[EE68D330:01CE3694] X-MC-Unique: 113041110141614201 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130411_051428_176851_CD9D2F51 X-CRM114-Status: GOOD ( 16.73 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [91.220.42.44 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Mark Rutland , Lorenzo.Pieralisi@arm.com, benh@kernel.crashing.org, devicetree-discuss@lists.ozlabs.org, will.deacon@arm.com, rob.herring@calxeda.com, grant.likely@secretlab.ca, dave.martin@arm.com X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org Depending on hardware configuration, some devices may only be accessible from certain CPUs, may have interrupts wired up to a subset of CPUs, or may have operations which affect subsets of CPUs. To handle these devices it is necessary to describe this affinity information in devicetree. This patch adds functions to handle parsing the CPU affinity of properties from devicetree, based on Lorenzo's topology binding, allowing subsets of CPUs to be associated with interrupts, hardware ports, etc. The functions can be used to build cpumasks and also to test whether an affinity property only targets one CPU independent of the current configuration (e.g. when the kernel supports fewer CPUs than are physically present). This is useful for dealing with mixed SPI/PPI devices. A device may have an arbitrary number of affinity properties, the meaning of which is device-specific and should be specified in a given device's binding document. For example, an affinity property describing interrupt routing may consist of a phandle pointing to a subtree of the topology nodes, indicating the set of CPUs an interrupt originates from or may be taken on. Bindings may have restrictions on the topology nodes referenced - for describing coherency controls an affinity property may indicate a whole cluster (uncluding any non-CPU logic it contains) is affected by some configuration. Signed-off-by: Mark Rutland --- arch/arm/include/asm/dt_affinity.h | 12 ++++ arch/arm/kernel/devtree.c | 139 +++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 arch/arm/include/asm/dt_affinity.h diff --git a/arch/arm/include/asm/dt_affinity.h b/arch/arm/include/asm/dt_affinity.h new file mode 100644 index 0000000..3acc7ce --- /dev/null +++ b/arch/arm/include/asm/dt_affinity.h @@ -0,0 +1,12 @@ +#ifndef ARM_ASM_DT_IRQ_H +#define ARM_ASM_DT_IRQ_H + +#include +#include +#include + +int arm_dt_affine_get_mask(struct device_node *node, char *prop, + int idx, cpumask_t *mask); +bool arm_dt_affine_is_single(struct device_node *node, char *prop, + int idx); +#endif /* ARM_ASM_DT_IRQ_H */ diff --git a/arch/arm/kernel/devtree.c b/arch/arm/kernel/devtree.c index 70f1bde..f31ab1e 100644 --- a/arch/arm/kernel/devtree.c +++ b/arch/arm/kernel/devtree.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -165,6 +166,144 @@ void __init arm_dt_init_cpu_maps(void) } } +/* + * Test if the node is within the topology tree. + * Walk up to the root, keeping refcounts balanced. + */ +static bool is_topology_node(struct device_node *node) +{ + /* + * of_get_next_parent decrements the refcount of the provided node. + * Increment it first to keep things balanced. + */ + struct device_node *parent = of_node_get(node); + if (!parent) + return false; + + do { + if (parent->name && of_node_cmp(parent->name, "cpu-map") == 0) { + of_node_put(parent); + return true; + } + } while ((parent = of_get_next_parent(parent))); + + return false; +} + +static int cpu_node_to_id(struct device_node *node) +{ + u32 reg; + + if (of_property_read_u32(node, "reg", ®) != 0) + return -EINVAL; + + return get_logical_index(reg); +} + +static int arm_dt_affine_build_mask(struct device_node *affine, + cpumask_t *mask) +{ + struct device_node *child, *parent = NULL; + int ret = -EINVAL; + + if (!is_topology_node(affine)) + return -EINVAL; + + child = of_node_get(affine); + if (!child) + goto out_invalid; + + parent = of_get_parent(child); + if (!parent) + goto out_invalid; + + if (!cpumask_empty(mask)) + goto out_invalid; + + /* + * Depth-first search over the topology tree, iterating over leaf nodes + * and adding all referenced CPUs to the cpumask. Almost all of the + * of_* iterators are built for breadth-first search, which means we + * have to do a little more work to ensure refcounts are balanced. + */ + do { + struct device_node *tmp, *cpu_node; + int cpu; + + /* head down to the leaf */ + while ((tmp = of_get_next_child(child, NULL))) { + of_node_put(parent); + parent = child; + child = tmp; + } + + cpu_node = of_parse_phandle(child, "cpu", 0); + cpu = cpu_node_to_id(cpu_node); + + if (cpu < 0) + pr_warn("Invalid or unused cpu in topology description '%s', skipping\n", + cpu_node->full_name); + else + cpumask_set_cpu(cpu, mask); + + of_node_put(cpu_node); + + /* + * Find the next sibling, or transitively a parent's sibling. + * Don't go further up the tree than the affine node we were + * handed. + */ + while (child != affine && + !(child = of_get_next_child(parent, child))) { + child = parent; + parent = of_get_parent(parent); + } + + } while (child != affine); /* all children covered. Time to stop */ + + ret = 0; + +out_invalid: + of_node_put(child); + of_node_put(parent); + return ret; +} + +int arm_dt_affine_get_mask(struct device_node *node, char *prop, + int idx, cpumask_t *mask) +{ + int ret = -EINVAL; + struct device_node *affine = of_parse_phandle(node, prop, idx); + if (!affine) + return -EINVAL; + + if (!is_topology_node(affine)) + goto out; + + ret = arm_dt_affine_build_mask(affine, mask); + +out: + of_node_put(affine); + return ret; +} + +bool arm_dt_affine_is_single(struct device_node *node, char *prop, + int idx) +{ + bool ret; + struct device_node *affine = of_parse_phandle(node, prop, idx); + if (!affine || !is_topology_node(affine)) { + pr_warn("%s called on invalid node '%s'", __func__, + node ? node->full_name : "NULL"); + of_node_put(affine); + return true; + } + + ret = !!of_get_property(affine, "cpu", NULL); + of_node_put(affine); + return ret; +} + /** * setup_machine_fdt - Machine setup when an dtb was passed to the kernel * @dt_phys: physical address of dt blob