gpucore_cooling.c 9.11 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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
/*
 *  linux/drivers/thermal/cpu_cooling.c
 *
 *  Copyright (C) 2012	Samsung Electronics Co., Ltd(http://www.samsung.com)
 *  Copyright (C) 2012  Amit Daniel <amit.kachhap@linaro.org>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#include <linux/module.h>
#include <linux/thermal.h>
#include <linux/cpufreq.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <linux/gpucore_cooling.h>
#include <linux/thermal_core.h>
#include <linux/device.h>

/**
 * struct gpucore_cooling_device - data for cooling device with gpucore
 * @id: unique integer value corresponding to each gpucore_cooling_device
 *	registered.
 * @cool_dev: thermal_cooling_device pointer to keep track of the
 *	registered cooling device.
 * @gpucore_state: integer value representing the current state of gpucore
 *	cooling	devices.
 * @gpucore_val: integer value representing the absolute value of the clipped
 *	frequency.
 * @allowed_cpus: all the cpus involved for this gpucore_cooling_device.
 *
 * This structure is required for keeping information of each
 * gpucore_cooling_device registered. In order to prevent corruption of this a
 * mutex lock cooling_gpucore_lock is used.
 */

static DEFINE_IDR(gpucore_idr);
static DEFINE_MUTEX(cooling_gpucore_lock);

/* notify_table passes value to the gpucore_ADJUST callback function. */
#define NOTIFY_INVALID NULL

static struct device_node *np;
static int save_flag = -1;

void save_gpucore_thermal_para(struct device_node *n)
{
	if (!n)
		return;

	if (save_flag == -1) {
		save_flag = 1;
		np = n;
	}
}

/**
 * get_idr - function to get a unique id.
 * @idr: struct idr * handle used to create a id.
 * @id: int * value generated by this function.
 *
 * This function will populate @id with an unique
 * id, using the idr API.
 *
 * Return: 0 on success, an error code on failure.
 */
static int get_idr(struct idr *idr, int *id)
{
	int ret;

	mutex_lock(&cooling_gpucore_lock);
	ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
	mutex_unlock(&cooling_gpucore_lock);
	if (unlikely(ret < 0))
		return ret;
	*id = ret;

	return 0;
}

/**
 * release_idr - function to free the unique id.
 * @idr: struct idr * handle used for creating the id.
 * @id: int value representing the unique id.
 */
static void release_idr(struct idr *idr, int id)
{
	mutex_lock(&cooling_gpucore_lock);
	idr_remove(idr, id);
	mutex_unlock(&cooling_gpucore_lock);
}

/* gpucore cooling device callback functions are defined below */

/**
 * gpucore_get_max_state - callback function to get the max cooling state.
 * @cdev: thermal cooling device pointer.
 * @state: fill this variable with the max cooling state.
 *
 * Callback for the thermal cooling device to return the gpucore
 * max cooling state.
 *
 * Return: 0 on success, an error code otherwise.
 */
static int gpucore_get_max_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
	struct gpucore_cooling_device *gpucore_device = cdev->devdata;
	*state = gpucore_device->max_gpu_core_num;
	pr_debug("max Gpu core=%ld\n", *state);
	return 0;
}

/**
 * gpucore_get_cur_state - callback function to get the current cooling state.
 * @cdev: thermal cooling device pointer.
 * @state: fill this variable with the current cooling state.
 *
 * Callback for the thermal cooling device to return the gpucore
 * current cooling state.
 *
 * Return: 0 on success, an error code otherwise.
 */
static int gpucore_get_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
	struct gpucore_cooling_device *gpucore_device = cdev->devdata;
	*state = gpucore_device->gpucore_state;
	pr_debug("current state=%ld\n", *state);
	return 0;
}

/**
 * gpucore_set_cur_state - callback function to set the current cooling state.
 * @cdev: thermal cooling device pointer.
 * @state: set this variable to the current cooling state.
 *
 * Callback for the thermal cooling device to change the gpucore
 * current cooling state.
 *
 * Return: 0 on success, an error code otherwise.
 */
static int gpucore_set_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long state)
{
	struct gpucore_cooling_device *gpucore_device = cdev->devdata;
	int set_max_num;
	mutex_lock(&cooling_gpucore_lock);
	if (gpucore_device->stop_flag) {
		mutex_unlock(&cooling_gpucore_lock);
		return 0;
	}
	if ((state & GPU_STOP) == GPU_STOP) {
		gpucore_device->stop_flag = 1;
		state = state&(~GPU_STOP);
	}
	mutex_unlock(&cooling_gpucore_lock);
	set_max_num = gpucore_device->max_gpu_core_num - state;
	/* pp should not be 0 */
	if (!set_max_num)
		return -EINVAL;

	gpucore_device->gpucore_state = state;
	gpucore_device->set_max_pp_num((unsigned int)set_max_num);
	pr_debug("need set max gpu num=%d,state=%ld\n", set_max_num, state);
	return 0;
}

/*
 * Simple mathematics model for gpu core power:
 * just for ipa hook
 */
static int gpucore_get_requested_power(struct thermal_cooling_device *cdev,
				       struct thermal_zone_device *tz,
				       u32 *power)
{
	*power = 0;

	return 0;
}

static int gpucore_state2power(struct thermal_cooling_device *cdev,
			       struct thermal_zone_device *tz,
			       unsigned long state, u32 *power)
{
	*power  = 0;

	return 0;
}

static int gpucore_power2state(struct thermal_cooling_device *cdev,
			       struct thermal_zone_device *tz, u32 power,
			       unsigned long *state)
{
	cdev->ops->get_cur_state(cdev, state);
	return 0;
}

static int gpucore_notify_state(struct thermal_cooling_device *cdev,
				struct thermal_zone_device *tz,
				enum thermal_trip_type type)
{
	unsigned long cur_state;
	long upper = -1;
	int i;
	struct thermal_instance *ins;

	switch (type) {
	case THERMAL_TRIP_HOT:
		if (tz->enter_hot) {
			for (i = 0; i < tz->trips; i++) {
				ins = get_thermal_instance(tz, cdev, i);
				if (ins && ins->upper > upper)
					upper = ins->upper;
			}
			cdev->ops->get_cur_state(cdev, &cur_state);
			cur_state += 1;
			/* do not exceed upper levels */
			if (upper != -1 && cur_state > upper)
				cur_state = upper;
			cdev->ops->set_cur_state(cdev, cur_state);
		} else {
			cur_state = 0;
			cdev->ops->set_cur_state(cdev, cur_state);
		}
		dev_info(&cdev->device, "cur_state:%ld\n", cur_state);
		break;
	default:
		break;
	}
	return 0;
}

/* Bind gpucore callbacks to thermal cooling device ops */
static struct thermal_cooling_device_ops const gpucore_cooling_ops = {
	.get_max_state = gpucore_get_max_state,
	.get_cur_state = gpucore_get_cur_state,
	.set_cur_state = gpucore_set_cur_state,
	.state2power   = gpucore_state2power,
	.power2state   = gpucore_power2state,
	.notify_state  = gpucore_notify_state,
	.get_requested_power = gpucore_get_requested_power,
};

/**
 * gpucore_cooling_register - function to create gpucore cooling device.
 * @clip_cpus: cpumask of cpus where the frequency constraints will happen.
 *
 * This interface function registers the gpucore cooling device with the name
 * "thermal-gpucore-%x". This api can support multiple instances of gpucore
 * cooling devices.
 *
 * Return: a valid struct thermal_cooling_device pointer on success,
 * on failure, it returns a corresponding ERR_PTR().
 */
struct gpucore_cooling_device *gpucore_cooling_alloc(void)
{
	struct gpucore_cooling_device *gcdev;
	gcdev = kzalloc(sizeof(struct gpucore_cooling_device), GFP_KERNEL);
	if (!gcdev) {
		pr_err("%s, %d, allocate memory fail\n", __func__, __LINE__);
		return ERR_PTR(-ENOMEM);
	}
	memset(gcdev, 0, sizeof(*gcdev));
	if (save_flag == 1)
		gcdev->np = np;
	return gcdev;
}
EXPORT_SYMBOL_GPL(gpucore_cooling_alloc);

int gpucore_cooling_register(struct gpucore_cooling_device *gpucore_dev)
{
	struct thermal_cooling_device *cool_dev;
	char dev_name[THERMAL_NAME_LENGTH];
	int ret = 0;
	ret = get_idr(&gpucore_idr, &gpucore_dev->id);
	if (ret) {
		kfree(gpucore_dev);
		return -EINVAL;
	}

	snprintf(dev_name, sizeof(dev_name), "thermal-gpucore-%d",
		 gpucore_dev->id);

	gpucore_dev->gpucore_state = 0;
	cool_dev = thermal_of_cooling_device_register(gpucore_dev->np,
			dev_name, gpucore_dev, &gpucore_cooling_ops);
	if (!cool_dev) {
		release_idr(&gpucore_idr, gpucore_dev->id);
		kfree(gpucore_dev);
		return -EINVAL;
	}
	gpucore_dev->cool_dev = cool_dev;
	return 0;
}
EXPORT_SYMBOL_GPL(gpucore_cooling_register);

/**
 * gpucore_cooling_unregister - function to remove gpucore cooling device.
 * @cdev: thermal cooling device pointer.
 *
 * This interface function unregisters the "thermal-gpucore-%x" cooling device.
 */
void gpucore_cooling_unregister(struct thermal_cooling_device *cdev)
{
	struct gpucore_cooling_device *gpucore_dev;

	if (!cdev)
		return;

	gpucore_dev = cdev->devdata;

	thermal_cooling_device_unregister(gpucore_dev->cool_dev);
	release_idr(&gpucore_idr, gpucore_dev->id);
	kfree(gpucore_dev);
}
EXPORT_SYMBOL_GPL(gpucore_cooling_unregister);