devfreq_cooling.c 7.88 KB
Newer Older
Abhijith PA's avatar
Abhijith PA committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/*
 * devfreq_cooling: Thermal cooling device implementation for devices using
 *                  devfreq
 *
 * Copyright (C) 2014 ARM Limited
 *
 * 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.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 * kind, whether express or implied; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/devfreq.h>
#include <linux/devfreq_cooling.h>
#include <linux/export.h>
#include <linux/slab.h>
#include <linux/pm_opp.h>
#include <linux/thermal.h>

static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
		unsigned long *state)
{
	struct devfreq_cooling_device *dfc = cdev->devdata;
	struct devfreq *df = dfc->devfreq;

	*state = df->profile->max_state - 1;

	return 0;
}

static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
		unsigned long *state)
{
	struct devfreq_cooling_device *dfc = cdev->devdata;

	*state = dfc->cooling_state;

	return 0;
}

static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
		unsigned long state)
{
	struct devfreq_cooling_device *dfc = cdev->devdata;
	struct devfreq *df = dfc->devfreq;
	struct device *dev = df->dev.parent;
	unsigned long freq;

	if (state == dfc->cooling_state)
		return 0;

	dev_dbg(dev, "Setting cooling state %lu\n", state);

	if (state == THERMAL_NO_LIMIT) {
		freq = 0;
	} else {
		unsigned long index;
		unsigned long max_state = df->profile->max_state;

		if (state >= max_state)
			return -EINVAL;

		index = (max_state - 1) - state;
		freq = df->profile->freq_table[index];
	}

	if (df->max_freq != freq)
		devfreq_qos_set_max(df, freq);

	dfc->cooling_state = state;

	return 0;
}

static unsigned long
freq_get_state(struct devfreq *df, unsigned long freq)
{
	unsigned long state = THERMAL_CSTATE_INVALID;
	int i;

	for (i = 0; i < df->profile->max_state; i++) {
		if (df->profile->freq_table[i] == freq)
			state = (df->profile->max_state - 1) - i;
	}

	return state;
}

static
int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
					struct thermal_zone_device *tz,
					u32 *power)
{
	struct devfreq_cooling_device *dfc = cdev->devdata;
	struct devfreq_dev_status *status = &dfc->last_status;
	struct devfreq *df = dfc->devfreq;
	unsigned long state;

	/* Get power for state */
	state = freq_get_state(df, status->current_frequency);
	*power = dfc->power_table[state];

	/* Scale power for utilization */
	*power = (*power * status->busy_time) / status->total_time;
	return 0;
}

static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
				       struct thermal_zone_device *tz,
				       unsigned long state,
				       u32 *power)
{
	struct devfreq_cooling_device *dfc = cdev->devdata;
	*power = dfc->power_table[state];
	return 0;
}

static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
				       struct thermal_zone_device *tz,
				       u32 power,
				       unsigned long *state)
{
	struct devfreq_cooling_device *dfc = cdev->devdata;
	struct devfreq *df = dfc->devfreq;
	int i;

	/* Find the first cooling state that is within the power budget. */
	for (i = 0; i < df->profile->max_state - 1; i++)
		if (power >= dfc->power_table[i])
			break;

	*state = i;
	return 0;
}

static struct thermal_cooling_device_ops const devfreq_cooling_ops = {
	.get_max_state = devfreq_cooling_get_max_state,
	.get_cur_state = devfreq_cooling_get_cur_state,
	.set_cur_state = devfreq_cooling_set_cur_state,
	.get_requested_power = devfreq_cooling_get_requested_power,
	.state2power = devfreq_cooling_state2power,
	.power2state = devfreq_cooling_power2state,
};

/**
 * devfreq_cooling_gen_power_table(): - Generate power table.
 * @dfc: Pointer to devfreq cooling device.
 *
 * Generate a table with the device's maximum power usage at each cooling
 * state (OPP). The static and dynamic powerm using the appropriate voltage and
 * frequency for the state, is aquired from the struct devfreq_cooling_ops, and
 * summed to make the maximum power draw.
 *
 * The table is malloced, and a pointer put in dfc->power_table. This must be
 * freed when unregistering the devfreq cooling device.
 *
 * Return: 0 on success, negative error code on failure.
 */
static int devfreq_cooling_gen_power_table(struct devfreq_cooling_device *dfc)
{
	struct devfreq *df = dfc->devfreq;
	struct device *dev = df->dev.parent;
	int num_opps;
	struct devfreq_cooling_ops *callbacks = dfc->power_ops;
	unsigned long freq;
	u32 *table;
	int i;

	rcu_read_lock();
	num_opps = dev_pm_opp_get_opp_count(dev);
	rcu_read_unlock();

	if (num_opps != dfc->devfreq->profile->max_state)
		return -ERANGE;

	table = kcalloc(num_opps, sizeof(table[0]), GFP_KERNEL);
	if (!table)
		return -ENOMEM;

	rcu_read_lock();
	for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
		unsigned long power_static, power_dyn, voltage;
		struct dev_pm_opp *opp;

		opp = dev_pm_opp_find_freq_floor(dev, &freq);
		if (IS_ERR(opp))
			break;

		voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */

		power_dyn = callbacks->get_dynamic_power(freq, voltage);
		power_static = callbacks->get_static_power(voltage);

		dev_info(dev, "Power table: %lu MHz @ %lu mV: %lu + %lu = %lu mW\n",
				freq / 1000000, voltage,
				power_dyn, power_static,
				power_dyn + power_static);

		table[i] = power_dyn + power_static;
	}
	rcu_read_unlock();

	if (i != num_opps) {
		kfree(table);
		return -EFAULT;
	}

	dfc->power_table = table;

	return 0;
}

/**
 * of_devfreq_cooling_register_power(): - Register devfreq cooling device,
 *                                      with OF and power information.
 * @np: Pointer to OF device_node.
 * @df: Pointer to devfreq device.
 * @ops: Pointer to power ops.
 */
struct devfreq_cooling_device *
of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
		struct devfreq_cooling_ops *ops)
{
	struct thermal_cooling_device *cdev;
	struct devfreq_cooling_device *dfc;

	/* freq_table is required to look map state index to frequency. */
	if (!df->profile->max_state && !df->profile->freq_table)
		return ERR_PTR(-EINVAL);


	dfc = kzalloc(sizeof(struct devfreq_cooling_device), GFP_KERNEL);
	if (!dfc)
		return ERR_PTR(-ENOMEM);

	dfc->devfreq = df;

	if (ops) {
		if (!ops->get_static_power || !ops->get_dynamic_power) {
			kfree(dfc);
			return ERR_PTR(-EINVAL);
		}
		dfc->power_ops = ops;

		devfreq_cooling_gen_power_table(dfc);
	}

	cdev = thermal_of_cooling_device_register(np, "devfreq", dfc,
			&devfreq_cooling_ops);
	if (!cdev) {
		dev_err(df->dev.parent,
			"Failed to register devfreq cooling device (%ld)\n",
			PTR_ERR(cdev));
		kfree(dfc->power_table);
		kfree(dfc);
		return ERR_CAST(cdev);
	}

	dfc->cdev = cdev;

	return dfc;
}
EXPORT_SYMBOL(of_devfreq_cooling_register_power);

/**
 * of_devfreq_cooling_register(): - Register devfreq cooling device,
 *                                with OF information.
 * @np: Pointer to OF device_node.
 * @df: Pointer to devfreq device.
 */
struct devfreq_cooling_device *
of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
{
	return of_devfreq_cooling_register_power(np, df, NULL);
}
EXPORT_SYMBOL(of_devfreq_cooling_register);

/**
 * devfreq_cooling_register(): - Register devfreq cooling device.
 * @df: Pointer to devfreq device.
 */
struct devfreq_cooling_device *devfreq_cooling_register(struct devfreq *df)
{
	return of_devfreq_cooling_register(NULL, df);
}
EXPORT_SYMBOL(devfreq_cooling_register);

/**
 * devfreq_cooling_unregister(): - Unregister devfreq cooling device.
 * @dfc: Pointer to devfreq cooling device to unregister.
 */
void devfreq_cooling_unregister(struct devfreq_cooling_device *dfc)
{
	if (!dfc)
		return;

	thermal_cooling_device_unregister(dfc->cdev);

	kfree(dfc->power_table);
	kfree(dfc);
}
EXPORT_SYMBOL(devfreq_cooling_unregister);