From patchwork Thu Aug 14 11:29:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Pieralisi X-Patchwork-Id: 35400 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f70.google.com (mail-pa0-f70.google.com [209.85.220.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 43062203C5 for ; Thu, 14 Aug 2014 11:31:18 +0000 (UTC) Received: by mail-pa0-f70.google.com with SMTP id lf10sf7921896pab.1 for ; Thu, 14 Aug 2014 04:31:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:date:from:to:subject:message-id :references:mime-version:in-reply-to:user-agent:cc:precedence :list-id:list-unsubscribe:list-archive:list-post:list-help :list-subscribe:sender:errors-to:x-original-sender :x-original-authentication-results:mailing-list:content-disposition :content-type:content-transfer-encoding; bh=zJ14biPm3pQQn+Pn6AkAqyk4Rh7/A1mTqU7NqWj4dF0=; b=BVoHE7JqfbjJrS9eZjmJ6mbQ1gwYHT5tXLyNOj2+SISuhaVoxui+PkWR1JyDYdBtnE WK14fgu3U2Ku5pSjEpORpSZuKddttvIGGxH/q64OPFF1WSgmYXWF2noO9D1ZWtyWY9JM LWP+G2/4crNSgejF2PbUSyQ95ETigLjJlrSib436y3wjLUOrHM3WZh5gk+XHkffOc6gs btNhq7t2Y7Um2Npd/Zx9G3SubsSpKewk5+JlYNX4iVapQUgMwiGEnua7U5DDDoKy6OSR LyVcbQAONqqm1l3twCRu1BP+wns42ScC/88B5N+kpozkwQl8MuawhiOUXeUpkqFz/EGA JwuQ== X-Gm-Message-State: ALoCoQleYmOKVvAoQVG5EK6MhHOkIQUw0snCl/Ur9lJQOEzkbwNOPCMJEyWRrNXU0pcP9f5NZ6Xz X-Received: by 10.66.218.162 with SMTP id ph2mr5633103pac.3.1408015877580; Thu, 14 Aug 2014 04:31:17 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.27.176 with SMTP id 45ls901519qgx.16.gmail; Thu, 14 Aug 2014 04:31:17 -0700 (PDT) X-Received: by 10.52.94.108 with SMTP id db12mr3630799vdb.8.1408015877377; Thu, 14 Aug 2014 04:31:17 -0700 (PDT) Received: from mail-vc0-f173.google.com (mail-vc0-f173.google.com [209.85.220.173]) by mx.google.com with ESMTPS id e5si2460554vcz.37.2014.08.14.04.31.17 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 14 Aug 2014 04:31:17 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.173 as permitted sender) client-ip=209.85.220.173; Received: by mail-vc0-f173.google.com with SMTP id hy10so1185430vcb.4 for ; Thu, 14 Aug 2014 04:31:17 -0700 (PDT) X-Received: by 10.220.74.10 with SMTP id s10mr419204vcj.61.1408015877220; Thu, 14 Aug 2014 04:31:17 -0700 (PDT) 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.221.37.5 with SMTP id tc5csp26083vcb; Thu, 14 Aug 2014 04:31:16 -0700 (PDT) X-Received: by 10.70.36.162 with SMTP id r2mr9779563pdj.97.1408015875763; Thu, 14 Aug 2014 04:31:15 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id e1si3733502pdg.5.2014.08.14.04.31.15 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Aug 2014 04:31:15 -0700 (PDT) Received-SPF: none (google.com: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org does not designate permitted sender hosts) client-ip=2001:1868:205::9; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XHtE2-0007Qa-6R; Thu, 14 Aug 2014 11:29:46 +0000 Received: from service87.mimecast.com ([91.220.42.44]) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XHtDy-0007Kw-Mp for linux-arm-kernel@lists.infradead.org; Thu, 14 Aug 2014 11:29:44 +0000 Received: from cam-owa2.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.21]) by service87.mimecast.com; Thu, 14 Aug 2014 12:29:14 +0100 Received: from red-moon ([10.1.255.212]) by cam-owa2.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Thu, 14 Aug 2014 12:29:13 +0100 Date: Thu, 14 Aug 2014 12:29:11 +0100 From: Lorenzo Pieralisi To: Nicolas Pitre Subject: Re: [PATCH v7 3/8] drivers: cpuidle: implement DT based idle states infrastructure Message-ID: <20140814112911.GA5039@red-moon> References: <1407945127-27554-1-git-send-email-lorenzo.pieralisi@arm.com> <1407945127-27554-4-git-send-email-lorenzo.pieralisi@arm.com> <20140813170440.GA30822@e102568-lin.cambridge.arm.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-OriginalArrivalTime: 14 Aug 2014 11:29:13.0369 (UTC) FILETIME=[F96BD890:01CFB7B2] X-MC-Unique: 114081412291400401 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140814_042943_346173_07F2324B X-CRM114-Status: GOOD ( 34.92 ) X-Spam-Score: -2.3 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.3 points) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, medium trust [91.220.42.44 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [91.220.42.44 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_WL Mailspike good senders Cc: Mark Rutland , Catalin Marinas , Tomasz Figa , Lina Iyer , Chander Kashyap , Vincent Guittot , Daniel Lezcano , ashwin.chaugule@linaro.org, "linux-arm-kernel@lists.infradead.org" , "grant.likely@linaro.org" , Charles Garcia-Tobin , "devicetree@vger.kernel.org" , Kevin Hilman , "linux-pm@vger.kernel.org" , Sebastian Capella , Mark Brown , Antti Miettinen , Paul Walmsley , Geoff Levand , Peter De Schrijver , Stephen Boyd , Amit Kucheria , Bartlomiej Zolnierkiewicz , Rob Herring , Santosh Shilimkar , Sudeep Holla X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: lorenzo.pieralisi@arm.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.173 as permitted sender) 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 Content-Disposition: inline On Wed, Aug 13, 2014 at 06:29:49PM +0100, Nicolas Pitre wrote: > On Wed, 13 Aug 2014, Lorenzo Pieralisi wrote: [...] > > > > The idle states list is obtained from the first cpu in the driver > > > > cpumask, which implicitly means the parsing code expects idle states > > > > (and related list of phandles) to be the same for all CPUs in the > > > > CPUidle driver mask. The kernel does not check this assumption, it must > > > > be enforced by the bootloader to ensure correct system behaviour. > > > > > > Can we make the kernel a little less reliant on bootloader to ensure > > > correct system behaviour please? If assumptions are assumed by the > > > kernel, it should at least print a warning and simply ignore the > > > information when such assumption are not respected. > > > > I think the check adds complexity (it means stashing the idle states > > phandles for the first cpu, loop through the cpus in the mask and compare > > the phandles to the ones stashed for the first cpu for all cpus in the > > driver mask) for not much. > > > > I was told that it is not up to the kernel to validate the DT, but if > > you want I can implement the check even though I really think it is > > overkill. > > DT validation is not the same as resiliance against messed-up DT > content. If the kernel is going to boot regardless then this is fine. > If the kernel is going to crash, or work suboptimally without returning > a clue because some implicit assumptions are not respected then this is > bad. > > And people _will_ mess up their DT from time to time. > > So if you tell me a messed-up DT won't bear much consequences then I'm > fine with that. Nico, on second thoughts, since Ashwin raised the point too and I think that at these early stages it might turn out useful, I gave coding the check a go. I tried to make the check self contained so that we can yank it out if we do not want it in the final version or we will want to remove it later. Here the refreshed patch is: -- >8 -- Subject: [PATCH] drivers: cpuidle: implement DT based idle states infrastructure On most common ARM systems, the low-power states a CPU can be put into are not discoverable in HW and require device tree bindings to describe power down suspend operations and idle states parameters. In order to enable DT based idle states and configure idle drivers, this patch implements the bulk infrastructure required to parse the device tree idle states bindings and initialize the corresponding CPUidle driver states data. The parsing API accepts a start index that defines the first idle state that should be initialized by the parsing code in order to give new and legacy driver flexibility over which states should be parsed using the new DT mechanism. The idle states list is obtained from the first cpu in the driver cpumask, which implicitly means the parsing code expects idle states (and related list of phandles) to be the same for all CPUs in the CPUidle driver mask. The kernel does not check this assumption, it must be enforced by the bootloader to ensure correct system behaviour. Signed-off-by: Lorenzo Pieralisi Acked-by: Catalin Marinas --- drivers/cpuidle/Kconfig | 3 + drivers/cpuidle/Makefile | 1 + drivers/cpuidle/dt_idle_states.c | 175 +++++++++++++++++++++++++++++++++++++++ drivers/cpuidle/dt_idle_states.h | 5 ++ 4 files changed, 184 insertions(+) create mode 100644 drivers/cpuidle/dt_idle_states.c create mode 100644 drivers/cpuidle/dt_idle_states.h diff --git a/drivers/cpuidle/Kconfig b/drivers/cpuidle/Kconfig index 32748c3..8deb934 100644 --- a/drivers/cpuidle/Kconfig +++ b/drivers/cpuidle/Kconfig @@ -25,6 +25,9 @@ config CPU_IDLE_GOV_MENU bool "Menu governor (for tickless system)" default y +config DT_IDLE_STATES + bool + menu "ARM CPU Idle Drivers" depends on ARM source "drivers/cpuidle/Kconfig.arm" diff --git a/drivers/cpuidle/Makefile b/drivers/cpuidle/Makefile index 11edb31..002b653 100644 --- a/drivers/cpuidle/Makefile +++ b/drivers/cpuidle/Makefile @@ -4,6 +4,7 @@ obj-y += cpuidle.o driver.o governor.o sysfs.o governors/ obj-$(CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED) += coupled.o +obj-$(CONFIG_DT_IDLE_STATES) += dt_idle_states.o ################################################################################## # ARM SoC drivers diff --git a/drivers/cpuidle/dt_idle_states.c b/drivers/cpuidle/dt_idle_states.c new file mode 100644 index 0000000..13273e2 --- /dev/null +++ b/drivers/cpuidle/dt_idle_states.c @@ -0,0 +1,175 @@ +/* + * DT idle states parsing code. + * + * Copyright (C) 2014 ARM Ltd. + * Author: Lorenzo Pieralisi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#define pr_fmt(fmt) "DT idle-states: " fmt + +#include +#include +#include +#include +#include +#include +#include + +#include "dt_idle_states.h" + +static int init_state_node(struct cpuidle_state *idle_state, + struct device_node *state_node) +{ + int err; + + err = of_property_read_u32(state_node, "wakeup-latency-us", + &idle_state->exit_latency); + if (err) { + u32 entry_latency, exit_latency; + + err = of_property_read_u32(state_node, "entry-latency-us", + &entry_latency); + if (err) { + pr_debug(" * %s missing entry-latency-us property\n", + state_node->full_name); + return -EINVAL; + } + + err = of_property_read_u32(state_node, "exit-latency-us", + &exit_latency); + if (err) { + pr_debug(" * %s missing exit-latency-us property\n", + state_node->full_name); + return -EINVAL; + } + /* + * If wakeup-latency-us is missing, default to entry+exit + * latencies as defined in idle states bindings + */ + idle_state->exit_latency = entry_latency + exit_latency; + } + + err = of_property_read_u32(state_node, "min-residency-us", + &idle_state->target_residency); + if (err) { + pr_debug(" * %s missing min-residency-us property\n", + state_node->full_name); + return -EINVAL; + } + + idle_state->flags = CPUIDLE_FLAG_TIME_VALID; + if (of_property_read_bool(state_node, "local-timer-stop")) + idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP; + /* + * TODO: + * replace with kstrdup and pointer assignment when name + * and desc become string pointers + */ + strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1); + strncpy(idle_state->desc, state_node->name, CPUIDLE_DESC_LEN - 1); + return 0; +} + +/* + * Check that the idle state is uniform across all CPUs in the CPUidle driver + * cpumask + */ +static bool idle_state_valid(struct device_node *state_node, unsigned int idx, + const cpumask_t *cpumask) +{ + int cpu; + struct device_node *cpu_node, *curr_state_node; + + /* + * Compare idle state phandles for index idx on all CPUs in the + * CPUidle driver cpumask. Start from next logical cpu following + * cpumask_first(cpumask) since that's the CPU state_node was + * retrieved from. If a mismatch is found bail out straight + * away since we certainly hit a firmware misconfiguration. + */ + for (cpu = cpumask_next(cpumask_first(cpumask), cpumask); + cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) { + cpu_node = of_cpu_device_node_get(cpu); + curr_state_node = of_parse_phandle(cpu_node, "cpu-idle-states", + idx); + if (state_node != curr_state_node) + return false; + } + + return true; +} + +/** + * dt_init_idle_driver() - Parse the DT idle states and initialize the + * idle driver states array + * @drv: Pointer to CPU idle driver to be initialized + * @start_idx: First idle state index to be initialized + * + * If DT idle states are detected and are valid the state count and states + * array entries in the cpuidle driver are initialized accordingly starting + * from index start_idx. + * + * Return: number of valid DT idle states parsed, <0 on failure + */ +int dt_init_idle_driver(struct cpuidle_driver *drv, unsigned int start_idx) +{ + struct cpuidle_state *idle_state; + struct device_node *state_node, *cpu_node; + int i; + unsigned int state_idx = start_idx; + + if (state_idx >= CPUIDLE_STATE_MAX) + return -EINVAL; + /* + * We get the idle states for the first logical cpu in the + * driver mask. The kernel does not check idle states on all + * cpus in the driver mask, they are assumed to be the same + * by default. + */ + cpu_node = of_cpu_device_node_get(cpumask_first(drv->cpumask)); + + for (i = 0; ; i++) { + int err; + + state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i); + if (!state_node) + break; + + if (!idle_state_valid(state_node, i, drv->cpumask)) { + pr_warn("%s idle state not valid, bailing out\n", + state_node->full_name); + return -EINVAL; + } + + if (state_idx == CPUIDLE_STATE_MAX) { + pr_warn("State index reached static CPU idle driver states array size\n"); + break; + } + + idle_state = &drv->states[state_idx++]; + err = init_state_node(idle_state, state_node); + if (err) { + pr_err("Parsing idle state node %s failed with err %d\n", + state_node->full_name, err); + return err; + } + } + /* + * Update the driver state count only if some valid DT idle states + * were detected + */ + if (i) + drv->state_count = state_idx; + + /* + * Return the number of present and valid DT idle states, which can + * also be 0 on platforms with missing DT idle states or legacy DT + * configuration predating the DT idle states bindings. + */ + return i; +} +EXPORT_SYMBOL_GPL(dt_init_idle_driver); diff --git a/drivers/cpuidle/dt_idle_states.h b/drivers/cpuidle/dt_idle_states.h new file mode 100644 index 0000000..728c37c --- /dev/null +++ b/drivers/cpuidle/dt_idle_states.h @@ -0,0 +1,5 @@ +#ifndef __DT_IDLE_STATES +#define __DT_IDLE_STATES + +int dt_init_idle_driver(struct cpuidle_driver *drv, unsigned int start_idx); +#endif