Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 28ffa724 authored by Viresh Kumar's avatar Viresh Kumar Committed by Junjie Wu
Browse files

cpufreq: Implement light weight ->target_index() routine



Currently, the prototype of cpufreq_drivers target routines is:

int target(struct cpufreq_policy *policy, unsigned int target_freq,
		unsigned int relation);

And most of the drivers call cpufreq_frequency_table_target() to get a valid
index of their frequency table which is closest to the target_freq. And they
don't use target_freq and relation after that.

So, it makes sense to just do this work in cpufreq core before calling
cpufreq_frequency_table_target() and simply pass index instead. But this can be
done only with drivers which expose their frequency table with cpufreq core. For
others we need to stick with the old prototype of target() until those drivers
are converted to expose frequency tables.

This patch implements the new light weight prototype for target_index() routine.
It looks like this:

int target_index(struct cpufreq_policy *policy, unsigned int index);

CPUFreq core will call cpufreq_frequency_table_target() before calling this
routine and pass index to it. Because CPUFreq core now requires to call routines
present in freq_table.c CONFIG_CPU_FREQ_TABLE must be enabled all the time.

This also marks target() interface as deprecated. So, that new drivers avoid
using it. And Documentation is updated accordingly.

It also converts existing .target() to newly defined light weight
.target_index() routine for many driver.

Acked-by: default avatarHans-Christian Egtvedt <egtvedt@samfundet.no>
Acked-by: default avatarJesper Nilsson <jesper.nilsson@axis.com>
Acked-by: default avatarLinus Walleij <linus.walleij@linaro.org>
Acked-by: default avatarRussell King <linux@arm.linux.org.uk>
Acked-by: default avatarDavid S. Miller <davem@davemloft.net>
Tested-by: default avatarAndrew Lunn <andrew@lunn.ch>
Signed-off-by: default avatarViresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: default avatarRafael J. Wysocki <rjw@rjwysocki.net>
Git-commit: 9c0ebcf78fde0ffa348a95a544c6d3f2dac5af65
Git-repo: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git


[junjiew@codeaurora.org: ignored all arch specific files that generated
 conflicts]
Signed-off-by: default avatarJunjie Wu <junjiew@codeaurora.org>
parent ebb72a01
Loading
Loading
Loading
Loading
+18 −9
Original line number Diff line number Diff line
@@ -23,8 +23,8 @@ Contents:
1.1  Initialization
1.2  Per-CPU Initialization
1.3  verify
1.4  target or setpolicy?
1.5  target
1.4  target/target_index or setpolicy?
1.5  target/target_index
1.6  setpolicy
2.   Frequency Table Helpers

@@ -56,7 +56,8 @@ cpufreq_driver.init - A pointer to the per-CPU initialization
cpufreq_driver.verify -		A pointer to a "verification" function.

cpufreq_driver.setpolicy _or_ 
cpufreq_driver.target -		See below on the differences.
cpufreq_driver.target/
target_index		-	See below on the differences.

And optionally

@@ -66,7 +67,7 @@ cpufreq_driver.resume - A pointer to a per-CPU resume function
				which is called with interrupts disabled
				and _before_ the pre-suspend frequency
				and/or policy is restored by a call to
				->target or ->setpolicy.
				->target/target_index or ->setpolicy.

cpufreq_driver.attr -		A pointer to a NULL-terminated list of
				"struct freq_attr" which allow to
@@ -103,8 +104,8 @@ policy->governor must contain the "default policy" for
				this CPU. A few moments later,
				cpufreq_driver.verify and either
				cpufreq_driver.setpolicy or
				cpufreq_driver.target is called with
				these values.
				cpufreq_driver.target/target_index is called
				with these values.

For setting some of these values (cpuinfo.min[max]_freq, policy->min[max]), the
frequency table helpers might be helpful. See the section 2 for more information
@@ -133,20 +134,28 @@ range) is within policy->min and policy->max. If necessary, increase
policy->max first, and only if this is no solution, decrease policy->min.


1.4 target or setpolicy?
1.4 target/target_index or setpolicy?
----------------------------

Most cpufreq drivers or even most cpu frequency scaling algorithms 
only allow the CPU to be set to one frequency. For these, you use the
->target call.
->target/target_index call.

Some cpufreq-capable processors switch the frequency between certain
limits on their own. These shall use the ->setpolicy call


1.4. target
1.4. target/target_index
-------------

The target_index call has two arguments: struct cpufreq_policy *policy,
and unsigned int index (into the exposed frequency table).

The CPUfreq driver must set the new frequency when called here. The
actual frequency must be determined by freq_table[index].frequency.

Deprecated:
----------
The target call has three arguments: struct cpufreq_policy *policy,
unsigned int target_frequency, unsigned int relation.

+2 −2
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ Most cpufreq drivers (in fact, all except one, longrun) or even most
cpu frequency scaling algorithms only offer the CPU to be set to one
frequency. In order to offer dynamic frequency scaling, the cpufreq
core must be able to tell these drivers of a "target frequency". So
these specific drivers will be transformed to offer a "->target"
these specific drivers will be transformed to offer a "->target/target_index"
call instead of the existing "->setpolicy" call. For "longrun", all
stays the same, though.

@@ -72,7 +72,7 @@ CPU can be set to switch independently | CPU can only be set
		    /			       the limits of policy->{min,max}
		   /			            \
		  /				     \
	Using the ->setpolicy call,		 Using the ->target call,
	Using the ->setpolicy call,		 Using the ->target/target_index call,
	    the limits and the			  the frequency closest
	     "policy" is set.			  to target_freq is set.
						  It is assured that it
+5 −12
Original line number Diff line number Diff line
@@ -49,28 +49,21 @@ static unsigned int bL_cpufreq_get(unsigned int cpu)

/* Set clock frequency */
static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
		unsigned int target_freq, unsigned int relation)
		unsigned int index)
{
	struct cpufreq_freqs freqs;
	u32 cpu = policy->cpu, freq_tab_idx, cur_cluster;
	u32 cpu = policy->cpu, cur_cluster;
	int ret = 0;

	cur_cluster = cpu_to_cluster(policy->cpu);

	freqs.old = bL_cpufreq_get(policy->cpu);

	/* Determine valid target frequency using freq_table */
	cpufreq_frequency_table_target(policy, freq_table[cur_cluster],
			target_freq, relation, &freq_tab_idx);
	freqs.new = freq_table[cur_cluster][freq_tab_idx].frequency;
	freqs.new = freq_table[cur_cluster][index].frequency;

	pr_debug("%s: cpu: %d, cluster: %d, oldfreq: %d, target freq: %d, new freq: %d\n",
			__func__, cpu, cur_cluster, freqs.old, target_freq,
			__func__, cpu, cur_cluster, freqs.old, freqs.new,
			freqs.new);

	if (freqs.old == freqs.new)
		return 0;

	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);

	ret = clk_set_rate(clk[cur_cluster], freqs.new * 1000);
@@ -200,7 +193,7 @@ static struct cpufreq_driver bL_cpufreq_driver = {
	.flags			= CPUFREQ_STICKY |
					CPUFREQ_HAVE_GOVERNOR_PER_POLICY,
	.verify			= cpufreq_generic_frequency_table_verify,
	.target			= bL_cpufreq_set_target,
	.target_index		= bL_cpufreq_set_target,
	.get			= bL_cpufreq_get,
	.init			= bL_cpufreq_init,
	.exit			= bL_cpufreq_exit,
+46 −11
Original line number Diff line number Diff line
@@ -54,6 +54,11 @@ struct cpufreq_cpu_save_data {
static DEFINE_PER_CPU(struct cpufreq_cpu_save_data, cpufreq_policy_save);
#endif

static inline bool has_target(void)
{
	return cpufreq_driver->target_index || cpufreq_driver->target;
}

/*
 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
 * all cpufreq/hotplug/workqueue/etc related lock issues.
@@ -401,7 +406,7 @@ static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
			*policy = CPUFREQ_POLICY_POWERSAVE;
			err = 0;
		}
	} else if (cpufreq_driver->target) {
	} else if (has_target()) {
		struct cpufreq_governor *t;

		mutex_lock(&cpufreq_governor_mutex);
@@ -565,7 +570,7 @@ static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
	ssize_t i = 0;
	struct cpufreq_governor *t;

	if (!cpufreq_driver->target) {
	if (!has_target()) {
		i += sprintf(buf, "performance powersave");
		goto out;
	}
@@ -850,7 +855,7 @@ static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
		if (ret)
			goto err_out_kobj_put;
	}
	if (cpufreq_driver->target) {
	if (has_target()) {
		ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
		if (ret)
			goto err_out_kobj_put;
@@ -899,10 +904,10 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
				  unsigned int cpu, struct device *dev,
				  bool frozen)
{
	int ret = 0, has_target = !!cpufreq_driver->target;
	int ret = 0;
	unsigned long flags;

	if (has_target) {
	if (has_target()) {
		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
		if (ret) {
			pr_err("%s: Failed to stop governor\n", __func__);
@@ -920,7 +925,7 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,

	unlock_policy_rwsem_write(policy->cpu);

	if (has_target) {
	if (has_target()) {
		if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
			(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
			pr_err("%s: Failed to start governor\n", __func__);
@@ -1240,7 +1245,7 @@ static int __cpufreq_remove_dev_prepare(struct device *dev,
		return -EINVAL;
	}

	if (cpufreq_driver->target) {
	if (has_target()) {
		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
		if (ret) {
			pr_err("%s: Failed to stop governor\n", __func__);
@@ -1309,7 +1314,7 @@ static int __cpufreq_remove_dev_finish(struct device *dev,

	/* If cpu is last user of policy, free policy */
	if (cpus == 1) {
		if (cpufreq_driver->target) {
		if (has_target()) {
			ret = __cpufreq_governor(policy,
					CPUFREQ_GOV_POLICY_EXIT);
			if (ret) {
@@ -1352,7 +1357,7 @@ static int __cpufreq_remove_dev_finish(struct device *dev,
		if (!frozen)
			cpufreq_policy_free(policy);
	} else {
		if (cpufreq_driver->target) {
		if (has_target()) {
			if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
					(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
				pr_err("%s: Failed to start governor\n",
@@ -1723,12 +1728,41 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
			policy->cpu, target_freq, relation, old_target_freq);

	/*
	 * This might look like a redundant call as we are checking it again
	 * after finding index. But it is left intentionally for cases where
	 * exactly same freq is called again and so we can save on few function
	 * calls.
	 */
	if (target_freq == policy->cur)
		return 0;

	if (cpufreq_driver->target)
		retval = cpufreq_driver->target(policy, target_freq, relation);
	else if (cpufreq_driver->target_index) {
		struct cpufreq_frequency_table *freq_table;
		int index;

		freq_table = cpufreq_frequency_get_table(policy->cpu);
		if (unlikely(!freq_table)) {
			pr_err("%s: Unable to find freq_table\n", __func__);
			goto out;
		}

		retval = cpufreq_frequency_table_target(policy, freq_table,
				target_freq, relation, &index);
		if (unlikely(retval)) {
			pr_err("%s: Unable to find matching freq\n", __func__);
			goto out;
		}

		if (freq_table[index].frequency == policy->cur)
			retval = 0;
		else
			retval = cpufreq_driver->target_index(policy, index);
	}

out:
	return retval;
}
EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
@@ -2056,7 +2090,7 @@ int cpufreq_update_policy(unsigned int cpu)
			pr_debug("Driver did not initialize current freq");
			policy->cur = new_policy.cur;
		} else {
			if (policy->cur != new_policy.cur && cpufreq_driver->target)
			if (policy->cur != new_policy.cur && has_target())
				cpufreq_out_of_sync(cpu, policy->cur,
								new_policy.cur);
		}
@@ -2134,7 +2168,8 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data)
		return -ENODEV;

	if (!driver_data || !driver_data->verify || !driver_data->init ||
	    ((!driver_data->setpolicy) && (!driver_data->target)))
	    !(driver_data->setpolicy || driver_data->target_index ||
		    driver_data->target))
		return -EINVAL;

	pr_debug("trying to register driver %s\n", driver_data->name);
+3 −13
Original line number Diff line number Diff line
@@ -66,28 +66,18 @@ static unsigned int davinci_getspeed(unsigned int cpu)
	return clk_get_rate(cpufreq.armclk) / 1000;
}

static int davinci_target(struct cpufreq_policy *policy,
				unsigned int target_freq, unsigned int relation)
static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
{
	int ret = 0;
	unsigned int idx;
	struct cpufreq_freqs freqs;
	struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
	struct clk *armclk = cpufreq.armclk;

	freqs.old = davinci_getspeed(0);
	freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000;

	if (freqs.old == freqs.new)
		return ret;
	freqs.new = pdata->freq_table[idx].frequency;

	dev_dbg(cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new);

	ret = cpufreq_frequency_table_target(policy, pdata->freq_table,
						freqs.new, relation, &idx);
	if (ret)
		return -EINVAL;

	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);

	/* if moving to higher frequency, up the voltage beforehand */
@@ -168,7 +158,7 @@ static struct freq_attr *davinci_cpufreq_attr[] = {
static struct cpufreq_driver davinci_driver = {
	.flags		= CPUFREQ_STICKY,
	.verify		= davinci_verify_speed,
	.target		= davinci_target,
	.target_index	= davinci_target,
	.get		= davinci_getspeed,
	.init		= davinci_cpu_init,
	.exit		= davinci_cpu_exit,
Loading