Commit 27dbc542 authored by Leonard Crestez's avatar Leonard Crestez Committed by Chanwoo Choi

PM / devfreq: Use PM QoS for sysfs min/max_freq

Switch the handling of min_freq and max_freq from sysfs to use the
dev_pm_qos_request interface.

Since PM QoS handles frequencies as kHz this change reduces the
precision of min_freq and max_freq. This shouldn't introduce problems
because frequencies which are not an integer number of kHz are likely
not an integer number of Hz either.

Try to ensure compatibility by rounding min values down and rounding
max values up.
Signed-off-by: default avatarLeonard Crestez <leonard.crestez@nxp.com>
Acked-by: default avatarChanwoo Choi <cw00.choi@samsung.com>
Reviewed-by: default avatarMatthias Kaehlcke <mka@chromium.org>
Tested-by: default avatarMatthias Kaehlcke <mka@chromium.org>
[cw00.choi: Return -EAGAIN instead of -EINVAL if dev_pm_qos is inactive]
Signed-off-by: default avatarChanwoo Choi <cw00.choi@samsung.com>
parent 05d7ae15
...@@ -141,10 +141,6 @@ static void get_freq_range(struct devfreq *devfreq, ...@@ -141,10 +141,6 @@ static void get_freq_range(struct devfreq *devfreq,
*max_freq = min(*max_freq, *max_freq = min(*max_freq,
(unsigned long)HZ_PER_KHZ * qos_max_freq); (unsigned long)HZ_PER_KHZ * qos_max_freq);
/* Apply constraints from sysfs */
*min_freq = max(*min_freq, devfreq->min_freq);
*max_freq = min(*max_freq, devfreq->max_freq);
/* Apply constraints from OPP interface */ /* Apply constraints from OPP interface */
*min_freq = max(*min_freq, devfreq->scaling_min_freq); *min_freq = max(*min_freq, devfreq->scaling_min_freq);
*max_freq = min(*max_freq, devfreq->scaling_max_freq); *max_freq = min(*max_freq, devfreq->scaling_max_freq);
...@@ -705,6 +701,19 @@ static void devfreq_dev_release(struct device *dev) ...@@ -705,6 +701,19 @@ static void devfreq_dev_release(struct device *dev)
dev_warn(dev->parent, dev_warn(dev->parent,
"Failed to remove min_freq notifier: %d\n", err); "Failed to remove min_freq notifier: %d\n", err);
if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
if (err)
dev_warn(dev->parent,
"Failed to remove max_freq request: %d\n", err);
}
if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
if (err)
dev_warn(dev->parent,
"Failed to remove min_freq request: %d\n", err);
}
if (devfreq->profile->exit) if (devfreq->profile->exit)
devfreq->profile->exit(devfreq->dev.parent); devfreq->profile->exit(devfreq->dev.parent);
...@@ -778,7 +787,6 @@ struct devfreq *devfreq_add_device(struct device *dev, ...@@ -778,7 +787,6 @@ struct devfreq *devfreq_add_device(struct device *dev,
err = -EINVAL; err = -EINVAL;
goto err_dev; goto err_dev;
} }
devfreq->min_freq = devfreq->scaling_min_freq;
devfreq->scaling_max_freq = find_available_max_freq(devfreq); devfreq->scaling_max_freq = find_available_max_freq(devfreq);
if (!devfreq->scaling_max_freq) { if (!devfreq->scaling_max_freq) {
...@@ -786,7 +794,6 @@ struct devfreq *devfreq_add_device(struct device *dev, ...@@ -786,7 +794,6 @@ struct devfreq *devfreq_add_device(struct device *dev,
err = -EINVAL; err = -EINVAL;
goto err_dev; goto err_dev;
} }
devfreq->max_freq = devfreq->scaling_max_freq;
devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev); devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
atomic_set(&devfreq->suspend_count, 0); atomic_set(&devfreq->suspend_count, 0);
...@@ -827,6 +834,16 @@ struct devfreq *devfreq_add_device(struct device *dev, ...@@ -827,6 +834,16 @@ struct devfreq *devfreq_add_device(struct device *dev,
mutex_unlock(&devfreq->lock); mutex_unlock(&devfreq->lock);
err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
DEV_PM_QOS_MIN_FREQUENCY, 0);
if (err < 0)
goto err_devfreq;
err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
DEV_PM_QOS_MAX_FREQUENCY,
PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
if (err < 0)
goto err_devfreq;
devfreq->nb_min.notifier_call = qos_min_notifier_call; devfreq->nb_min.notifier_call = qos_min_notifier_call;
err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_min, err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_min,
DEV_PM_QOS_MIN_FREQUENCY); DEV_PM_QOS_MIN_FREQUENCY);
...@@ -1412,14 +1429,22 @@ static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr, ...@@ -1412,14 +1429,22 @@ static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
unsigned long value; unsigned long value;
int ret; int ret;
/*
* Protect against theoretical sysfs writes between
* device_add and dev_pm_qos_add_request
*/
if (!dev_pm_qos_request_active(&df->user_min_freq_req))
return -EAGAIN;
ret = sscanf(buf, "%lu", &value); ret = sscanf(buf, "%lu", &value);
if (ret != 1) if (ret != 1)
return -EINVAL; return -EINVAL;
mutex_lock(&df->lock); /* Round down to kHz for PM QoS */
df->min_freq = value; ret = dev_pm_qos_update_request(&df->user_min_freq_req,
update_devfreq(df); value / HZ_PER_KHZ);
mutex_unlock(&df->lock); if (ret < 0)
return ret;
return count; return count;
} }
...@@ -1444,18 +1469,35 @@ static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr, ...@@ -1444,18 +1469,35 @@ static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
unsigned long value; unsigned long value;
int ret; int ret;
/*
* Protect against theoretical sysfs writes between
* device_add and dev_pm_qos_add_request
*/
if (!dev_pm_qos_request_active(&df->user_max_freq_req))
return -EINVAL;
ret = sscanf(buf, "%lu", &value); ret = sscanf(buf, "%lu", &value);
if (ret != 1) if (ret != 1)
return -EINVAL; return -EINVAL;
mutex_lock(&df->lock); /*
* PM QoS frequencies are in kHz so we need to convert. Convert by
if (!value) * rounding upwards so that the acceptable interval never shrinks.
value = ULONG_MAX; *
* For example if the user writes "666666666" to sysfs this value will
* be converted to 666667 kHz and back to 666667000 Hz before an OPP
* lookup, this ensures that an OPP of 666666666Hz is still accepted.
*
* A value of zero means "no limit".
*/
if (value)
value = DIV_ROUND_UP(value, HZ_PER_KHZ);
else
value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
df->max_freq = value; ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
update_devfreq(df); if (ret < 0)
mutex_unlock(&df->lock); return ret;
return count; return count;
} }
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/notifier.h> #include <linux/notifier.h>
#include <linux/pm_opp.h> #include <linux/pm_opp.h>
#include <linux/pm_qos.h>
#define DEVFREQ_NAME_LEN 16 #define DEVFREQ_NAME_LEN 16
...@@ -123,8 +124,8 @@ struct devfreq_dev_profile { ...@@ -123,8 +124,8 @@ struct devfreq_dev_profile {
* @previous_freq: previously configured frequency value. * @previous_freq: previously configured frequency value.
* @data: Private data of the governor. The devfreq framework does not * @data: Private data of the governor. The devfreq framework does not
* touch this. * touch this.
* @min_freq: Limit minimum frequency requested by user (0: none) * @user_min_freq_req: PM QoS minimum frequency request from user (via sysfs)
* @max_freq: Limit maximum frequency requested by user (0: none) * @user_max_freq_req: PM QoS maximum frequency request from user (via sysfs)
* @scaling_min_freq: Limit minimum frequency requested by OPP interface * @scaling_min_freq: Limit minimum frequency requested by OPP interface
* @scaling_max_freq: Limit maximum frequency requested by OPP interface * @scaling_max_freq: Limit maximum frequency requested by OPP interface
* @stop_polling: devfreq polling status of a device. * @stop_polling: devfreq polling status of a device.
...@@ -163,8 +164,8 @@ struct devfreq { ...@@ -163,8 +164,8 @@ struct devfreq {
void *data; /* private data for governors */ void *data; /* private data for governors */
unsigned long min_freq; struct dev_pm_qos_request user_min_freq_req;
unsigned long max_freq; struct dev_pm_qos_request user_max_freq_req;
unsigned long scaling_min_freq; unsigned long scaling_min_freq;
unsigned long scaling_max_freq; unsigned long scaling_max_freq;
bool stop_polling; bool stop_polling;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment