summaryrefslogtreecommitdiff
path: root/drivers/edgetpu/abrolhos-thermal.c
blob: 2740d08115f64f388f8e61974c18544431bc96ca (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * EdgeTPU thermal driver for Abrolhos.
 *
 * Copyright (C) 2020 Google, Inc.
 */

#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/gfp.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/thermal.h>
#include <linux/version.h>

#include "abrolhos-firmware.h"
#include "abrolhos-platform.h"
#include "abrolhos-pm.h"
#include "edgetpu-config.h"
#include "edgetpu-internal.h"
#include "edgetpu-mmu.h"
#include "edgetpu-thermal.h"

static const unsigned long state_mapping[] = {
	TPU_ACTIVE_OD,
	TPU_ACTIVE_NOM,
	TPU_ACTIVE_UD,
	TPU_ACTIVE_SUD,
	TPU_RETENTION_CLOCKS_SLOW,
	TPU_SLEEP_CLOCKS_SLOW,
	TPU_SLEEP_CLOCKS_OFF,
	TPU_DEEP_SLEEP_CLOCKS_FAST,
	TPU_DEEP_SLEEP_CLOCKS_SLOW,
	TPU_DEEP_SLEEP_CLOCKS_OFF,
	TPU_OFF,
};

/*
 * Sequence need to be kept to make power increasing
 * to make sure we always return the highest state.
 */
static const struct edgetpu_state_pwr state_pwr_map[] = {
	{ TPU_ACTIVE_OD, 198 },
	{ TPU_ACTIVE_NOM, 165 },
	{ TPU_ACTIVE_UD, 131 },
	{ TPU_ACTIVE_SUD, 102 },
	{ TPU_SLEEP_CLOCKS_SLOW, 66 },
	{ TPU_SLEEP_CLOCKS_OFF, 66 },
	{ TPU_RETENTION_CLOCKS_SLOW, 49 },
	{ TPU_DEEP_SLEEP_CLOCKS_FAST, 43 },
	{ TPU_DEEP_SLEEP_CLOCKS_SLOW, 6 },
	{ TPU_DEEP_SLEEP_CLOCKS_OFF, 6 },
	{ TPU_OFF, 0 },
};

static int edgetpu_get_max_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
	*state = ARRAY_SIZE(state_mapping) - 1;
	return 0;
}

/*
 * Set cooling state.
 */
static int edgetpu_set_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long state_original)
{
	int ret;
	struct edgetpu_thermal *cooling = cdev->devdata;
	struct device *dev = cooling->dev;
	unsigned long pwr_state;

	if (state_original >= ARRAY_SIZE(state_mapping)) {
		dev_err(dev, "%s: invalid cooling state %lu\n", __func__,
			state_original);
		return -EINVAL;
	}

	mutex_lock(&cooling->lock);
	pwr_state = state_mapping[state_original];
	if (state_original != cooling->cooling_state) {
		ret = exynos_acpm_set_policy(TPU_ACPM_DOMAIN, pwr_state);
		if (ret) {
			dev_err(dev, "error setting tpu policy: %d\n", ret);
			goto out;
		}
		cooling->cooling_state = state_original;
	} else {
		ret = -EALREADY;
	}

out:
	mutex_unlock(&cooling->lock);
	return ret;
}

static int edgetpu_get_cur_state(struct thermal_cooling_device *cdev,
				 unsigned long *state)
{
	int ret = 0;
	struct edgetpu_thermal *cooling = cdev->devdata;

	*state = cooling->cooling_state;
	if (*state >= ARRAY_SIZE(state_mapping)) {
		dev_warn(cooling->dev,
			 "Unknown cooling state: %lu, resetting\n", *state);
		mutex_lock(&cooling->lock);

		ret = exynos_acpm_set_policy(TPU_ACPM_DOMAIN, TPU_ACTIVE_OD);
		if (ret) {
			dev_err(cooling->dev, "error setting tpu policy: %d\n",
				ret);
			mutex_unlock(&cooling->lock);
			return ret;
		}

		//setting back to "no cooling"
		cooling->cooling_state = 0;
		mutex_unlock(&cooling->lock);
	}

	return 0;
}

static int edgetpu_state2power_internal(unsigned long state, u32 *power,
					struct device *dev)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(state_pwr_map); i++) {
		if (state == state_pwr_map[i].state) {
			*power = state_pwr_map[i].power;
			return 0;
		}
	}
	dev_err(dev, "Unknown state req for: %lu\n", state);
	*power = 0;
	return -EINVAL;
}

static int edgetpu_get_requested_power(struct thermal_cooling_device *cdev,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)
				       struct thermal_zone_device *tz,
#endif
				       u32 *power)
{
	unsigned long state_original;
	struct edgetpu_thermal *cooling = cdev->devdata;

	state_original = exynos_acpm_get_rate(TPU_ACPM_DOMAIN, 0);
	return edgetpu_state2power_internal(state_original, power,
					    cooling->dev);
}

static int edgetpu_state2power(struct thermal_cooling_device *cdev,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)
			       struct thermal_zone_device *tz,
#endif
			       unsigned long state, u32 *power)
{
	struct edgetpu_thermal *cooling = cdev->devdata;

	if (state >= ARRAY_SIZE(state_mapping)) {
		dev_err(cooling->dev, "%s: invalid state: %lu\n", __func__,
			state);
		return -EINVAL;
	}

	return edgetpu_state2power_internal(state_mapping[state], power,
					    cooling->dev);
}

static int edgetpu_power2state(struct thermal_cooling_device *cdev,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)
			       struct thermal_zone_device *tz,
#endif
			       u32 power, unsigned long *state)
{
	int i;
	struct edgetpu_thermal *cooling = cdev->devdata;

	for (i = 0; i < ARRAY_SIZE(state_pwr_map); i++) {
		if (power >= state_pwr_map[i].power) {
			*state = i;
			return 0;
		}
	}

	dev_err(cooling->dev, "No power2state mapping found: %d\n", power);
	return -EINVAL;
}

static struct thermal_cooling_device_ops edgetpu_cooling_ops = {
	.get_max_state = edgetpu_get_max_state,
	.get_cur_state = edgetpu_get_cur_state,
	.set_cur_state = edgetpu_set_cur_state,
	.get_requested_power = edgetpu_get_requested_power,
	.state2power = edgetpu_state2power,
	.power2state = edgetpu_power2state,
};

static void tpu_thermal_exit_cooling(struct edgetpu_thermal *thermal)
{
	if (!IS_ERR_OR_NULL(thermal->cdev))
		thermal_cooling_device_unregister(thermal->cdev);
}

static void tpu_thermal_exit(struct edgetpu_thermal *thermal)
{
	tpu_thermal_exit_cooling(thermal);
	debugfs_remove_recursive(thermal->cooling_root);
}

static void devm_tpu_thermal_release(struct device *dev, void *res)
{
	struct edgetpu_thermal *thermal = res;

	tpu_thermal_exit(thermal);
}

static int
tpu_thermal_cooling_register(struct edgetpu_thermal *thermal, char *type)
{
	struct device_node *cooling_node = NULL;

	thermal->op_data = NULL;

	mutex_init(&thermal->lock);
	cooling_node = of_find_node_by_name(NULL, "tpu-cooling");
	if (!cooling_node)
		dev_warn(thermal->dev, "failed to find cooling node\n");
	// Initialize the cooling state as 0, means "no cooling"
	thermal->cooling_state = 0;
	thermal->cdev = thermal_of_cooling_device_register(
		cooling_node, type, thermal, &edgetpu_cooling_ops);
	if (IS_ERR(thermal->cdev))
		return PTR_ERR(thermal->cdev);
	return 0;
}

static int tpu_thermal_init(struct edgetpu_thermal *thermal, struct device *dev)
{
	int err;
	struct dentry *d;

	d = debugfs_create_dir("cooling", edgetpu_fs_debugfs_dir());
	/* don't let debugfs creation failure abort the init procedure */
	if (!d)
		dev_warn(dev, "failed to create debug fs for cooling");
	thermal->dev = dev;
	thermal->cooling_root = d;

	err = tpu_thermal_cooling_register(thermal, EDGETPU_COOLING_NAME);
	if (err) {
		dev_err(dev, "failed to initialize external cooling\n");
		tpu_thermal_exit(thermal);
		return err;
	}

	return 0;
}

struct edgetpu_thermal *devm_tpu_thermal_create(struct device *dev)
{
	struct edgetpu_thermal *thermal;
	int err;

	thermal = devres_alloc(devm_tpu_thermal_release, sizeof(*thermal),
			       GFP_KERNEL);
	if (!thermal)
		return ERR_PTR(-ENOMEM);

	err = tpu_thermal_init(thermal, dev);
	if (err) {
		devres_free(thermal);
		return ERR_PTR(err);
	}

	devres_add(dev, thermal);
	return thermal;
}