From patchwork Thu Nov 28 14:44:11 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 21840 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ob0-f197.google.com (mail-ob0-f197.google.com [209.85.214.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7766823FC4 for ; Thu, 28 Nov 2013 14:44:26 +0000 (UTC) Received: by mail-ob0-f197.google.com with SMTP id va2sf27327041obc.4 for ; Thu, 28 Nov 2013 06:44:25 -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:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=epKl3Qfs/dn3RD+cpcl8Fk05TSmkp+r42JgPl2uTpfU=; b=m6geG4OX3dxM3wAoILuu2U+D909rlR9dVe+rnq6FfbtxtO4d5oNWZK88aF3WEh6JGC W3Te0A0KVXiVCvH7NohsY8pFN3Qq/ZDxAwNIhmizJfNb0a6PhgX1R9EIHFt06Q1ozMe1 VRky4sNjsjpCwKBLsf1lDmX0jOJ6pix4SUPuT4gPkE0oW8fP6MdjeaVDSegePNp5N0Mu 3RmFCKETrE6f5P7F4L0ST5VDk300vtFW8z01R7ccrU2FaN6TKQgnL1VNRQ6+mHSz1hvw ZR0jL3y3p8owr9Tfy73J4/RweRPyqu3nnA/S4nH9yJUfJnjDnU98WRATJIWTEnanLlsR jP0A== X-Gm-Message-State: ALoCoQlTh9Uo7Mhv5wnCJhuAU+sNqFw+ErsYgrBasEUOf2qXwfOiIUL03fdcAipVq3dOHch5WUY/ X-Received: by 10.42.66.147 with SMTP id p19mr13237379ici.12.1385649865416; Thu, 28 Nov 2013 06:44:25 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.37.195 with SMTP id a3ls3275171qek.88.gmail; Thu, 28 Nov 2013 06:44:25 -0800 (PST) X-Received: by 10.52.31.227 with SMTP id d3mr956483vdi.38.1385649865277; Thu, 28 Nov 2013 06:44:25 -0800 (PST) Received: from mail-vb0-f54.google.com (mail-vb0-f54.google.com [209.85.212.54]) by mx.google.com with ESMTPS id ph5si23035635veb.52.2013.11.28.06.44.25 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 28 Nov 2013 06:44:25 -0800 (PST) Received-SPF: neutral (google.com: 209.85.212.54 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.54; Received: by mail-vb0-f54.google.com with SMTP id p6so6101931vbe.27 for ; Thu, 28 Nov 2013 06:44:25 -0800 (PST) X-Received: by 10.52.165.131 with SMTP id yy3mr12798781vdb.25.1385649865165; Thu, 28 Nov 2013 06:44:25 -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 u4csp20337vcz; Thu, 28 Nov 2013 06:44:24 -0800 (PST) X-Received: by 10.68.191.106 with SMTP id gx10mr11018901pbc.47.1385649863963; Thu, 28 Nov 2013 06:44:23 -0800 (PST) Received: from mail-pd0-f169.google.com (mail-pd0-f169.google.com [209.85.192.169]) by mx.google.com with ESMTPS id bo6si14742208pab.317.2013.11.28.06.44.23 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 28 Nov 2013 06:44:23 -0800 (PST) Received-SPF: neutral (google.com: 209.85.192.169 is neither permitted nor denied by best guess record for domain of viresh.kumar@linaro.org) client-ip=209.85.192.169; Received: by mail-pd0-f169.google.com with SMTP id v10so12226935pde.28 for ; Thu, 28 Nov 2013 06:44:23 -0800 (PST) X-Received: by 10.68.66.33 with SMTP id c1mr11181897pbt.73.1385649863385; Thu, 28 Nov 2013 06:44:23 -0800 (PST) Received: from localhost ([122.167.133.207]) by mx.google.com with ESMTPSA id oj6sm108322956pab.9.2013.11.28.06.44.19 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 28 Nov 2013 06:44:22 -0800 (PST) From: Viresh Kumar To: rjw@rjwysocki.net Cc: linaro-kernel@lists.linaro.org, patches@linaro.org, cpufreq@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, nm@ti.com, ceh@ti.com, Viresh Kumar Subject: [PATCH V3] cpufreq: Make sure CPU is running on a freq from freq-table Date: Thu, 28 Nov 2013 20:14:11 +0530 Message-Id: X-Mailer: git-send-email 1.7.12.rc2.18.g61b472e X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: viresh.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.54 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: , Sometimes boot loaders set CPU frequency to a value outside of frequency table present with cpufreq core. In such cases CPU might be unstable if it has to run on that frequency for long duration of time and so its better to set it to a frequency which is specified in freq-table. This also makes cpufreq stats inconsistent as cpufreq-stats would fail to register because current frequency of CPU isn't found in freq-table. Because we don't want this change to effect boot process badly, we go for the next freq which is >= policy->cur ('cur' must be set by now, otherwise we will end up setting freq to lowest of the table as 'cur' is initialized to zero). In case current frequency doesn't match any frequency from freq-table, we throw warnings to user, so that user can get this fixed in their bootloaders or freq-tables. On some systems we can't really say what frequency we're running at the moment and so for these we have added another flag: CPUFREQ_SKIP_INITIAL_FREQ_CHECK. Reported-by: Carlos Hernandez Reported-and-tested-by: Nishanth Menon Signed-off-by: Viresh Kumar --- V2->V3: - Added BUG_ON() - Added another flag: CPUFREQ_SKIP_INITIAL_FREQ_CHECK drivers/cpufreq/cpufreq.c | 38 ++++++++++++++++++++++++++++++++++++++ drivers/cpufreq/freq_table.c | 24 ++++++++++++++++++++++++ include/linux/cpufreq.h | 11 +++++++++++ 3 files changed, 73 insertions(+) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 02d534d..1a8bf5d 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -1038,6 +1038,44 @@ static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif, } } + /* + * Sometimes boot loaders set CPU frequency to a value outside of + * frequency table present with cpufreq core. In such cases CPU might be + * unstable if it has to run on that frequency for long duration of time + * and so its better to set it to a frequency which is specified in + * freq-table. This also makes cpufreq stats inconsistent as + * cpufreq-stats would fail to register because current frequency of CPU + * isn't found in freq-table. + * + * Because we don't want this change to effect boot process badly, we go + * for the next freq which is >= policy->cur ('cur' must be set by now, + * otherwise we will end up setting freq to lowest of the table as 'cur' + * is initialized to zero). + * + * We are passing target-freq as "policy->cur - 1" otherwise + * __cpufreq_driver_target() would simply fail, as policy->cur will be + * equal to target-freq. + */ + if (!(cpufreq_driver->flags & CPUFREQ_SKIP_INITIAL_FREQ_CHECK) + && has_target()) { + /* Are we running at unknown frequency ? */ + ret = cpufreq_frequency_table_get_index(policy, policy->cur); + if (ret == -EINVAL) { + /* Warn user and fix it */ + pr_warn("%s: CPU%d: running at invalid freq: %u KHz\n", + __func__, policy->cpu, policy->cur); + /* + * Reaching here after boot in a few seconds may not + * mean that system will remain stable at "unknown" + * frequency for longer duration. Hence, a BUG_ON(). + */ + BUG_ON(__cpufreq_driver_target(policy, policy->cur - 1, + CPUFREQ_RELATION_L)); + pr_warn("%s: CPU%d: frequency changed to: %u KHz\n", + __func__, policy->cpu, policy->cur); + } + } + /* related cpus should atleast have policy->cpus */ cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus); diff --git a/drivers/cpufreq/freq_table.c b/drivers/cpufreq/freq_table.c index 3458d27..0d6cc0e 100644 --- a/drivers/cpufreq/freq_table.c +++ b/drivers/cpufreq/freq_table.c @@ -178,7 +178,31 @@ int cpufreq_frequency_table_target(struct cpufreq_policy *policy, } EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target); +int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, + unsigned int freq) +{ + struct cpufreq_frequency_table *table; + int index = -EINVAL, i = 0; + + table = cpufreq_frequency_get_table(policy->cpu); + if (unlikely(!table)) { + pr_debug("%s: Unable to find freq_table\n", __func__); + return -ENOENT; + } + + for (; table[i].frequency != CPUFREQ_TABLE_END; i++) { + if (table[i].frequency == freq) { + index = i; + break; + } + } + + return index; +} +EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_index); + static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table); + /** * show_available_freqs - show available frequencies for the specified CPU */ diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index dc196bb..7109c62 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -252,6 +252,15 @@ struct cpufreq_driver { */ #define CPUFREQ_ASYNC_NOTIFICATION (1 << 4) +/* + * Set by drivers which don't want cpufreq core to check if CPU is running at a + * frequency present in freq-table exposed by the driver. For other drivers if + * CPU is found running at an out of table freq, we will try to set it to a freq + * from the table. And if that fails, we will stop further boot process by + * issuing a BUG_ON(). + */ +#define CPUFREQ_SKIP_INITIAL_FREQ_CHECK (1 << 5) + int cpufreq_register_driver(struct cpufreq_driver *driver_data); int cpufreq_unregister_driver(struct cpufreq_driver *driver_data); @@ -439,6 +448,8 @@ int cpufreq_frequency_table_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation, unsigned int *index); +int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy, + unsigned int freq); void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy); ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);