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
|
/*
* drivers/thermal/userspace_alert.c
*
* Userspace thermal alert cooling device.
*
* Copyright (C) 2016-2021 NVIDIA Corporation. All rights reserved.
*
* 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 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/kernel.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/thermal.h>
#include <linux/platform_device.h>
/* Define cooling device alert state */
#define NO_ALERT 0
#define ALERT 1
/* Maximum throttle states supported */
#define CDEV_MAX_STATE ALERT
struct usr_alrt_strct {
unsigned long cur_state;
struct thermal_cooling_device *cdev;
uint32_t therm_alert;
uint32_t alert_timeout_ms;
wait_queue_head_t alert_wait_queue;
struct mutex alert_timeout_lock;
struct mutex alert_lock;
bool syfs_created;
bool syfs_symlink_created;
};
static int userspace_alert_send_uevent(struct thermal_cooling_device *tcd)
{
char trip[25];
char *envp[] = { trip, NULL };
int ret = 0;
snprintf(trip, sizeof(trip), "USERSPACE_ALERT=%s", "TRIPPED");
ret = kobject_uevent_env(&tcd->device.kobj, KOBJ_CHANGE, envp);
if (ret < 0)
pr_err("userspace_alert_send_uevent: kobj_uevent error!\n");
return ret;
}
/* Get maximum throttle state supported by cooling device. */
static int userspace_alert_cdev_max_state(struct thermal_cooling_device *tcd,
unsigned long *state)
{
struct usr_alrt_strct *alert_data = tcd->devdata;
if (!alert_data)
return -EINVAL;
*state = CDEV_MAX_STATE;
return 0;
}
/* Get current throttle state of the cooling device. */
static int userspace_alert_cdev_cur_state(struct thermal_cooling_device *tcd,
unsigned long *state)
{
struct usr_alrt_strct *alert_data = tcd->devdata;
if (!alert_data)
return -EINVAL;
*state = alert_data->cur_state;
return 0;
}
/* Set current throttle state of the cooling device. */
static int userspace_alert_cdev_set_state(struct thermal_cooling_device *tcd,
unsigned long state)
{
struct usr_alrt_strct *alert_data = tcd->devdata;
if (!alert_data)
return -EINVAL;
if (state == alert_data->cur_state)
return 0;
pr_notice("%s cooling state: %ld -> %lu\n",
tcd->type, alert_data->cur_state, state);
mutex_lock(&alert_data->alert_lock);
alert_data->cur_state = state;
if (state > 0) {
/* Send 'uevent' notification to userspace */
userspace_alert_send_uevent(tcd);
alert_data->therm_alert = ALERT;
/*
* Write barrier for other processes to see the therm_alert
* updated value.
*/
smp_wmb();
/* Wakeup the processes waiting for the alert to occur */
if (waitqueue_active(&alert_data->alert_wait_queue))
wake_up_all(&alert_data->alert_wait_queue);
pr_debug("thermal alert: USERSPACE_ALERT_TRIPPED!\n");
} else {
alert_data->therm_alert = NO_ALERT;
/*
* Write barrier for other processes to see the therm_alert
* updated value.
*/
smp_wmb();
}
mutex_unlock(&alert_data->alert_lock);
return 0;
}
/*
* Blocks the read call until an alert occurs or the specified duration
* times out. When alert occurs or if alert is preset, then read return
* with value 1
*/
static ssize_t thermal_alert_block_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
uint32_t alert_timeout_ms;
struct usr_alrt_strct *alert_data = dev_get_drvdata(dev);
int ret;
if (!alert_data)
return -EINVAL;
alert_timeout_ms = alert_data->alert_timeout_ms;
if (alert_timeout_ms > 0)
ret = wait_event_interruptible_timeout(alert_data->alert_wait_queue,
alert_data->therm_alert == ALERT,
msecs_to_jiffies(alert_timeout_ms));
else
ret = wait_event_interruptible(
alert_data->alert_wait_queue,
alert_data->therm_alert == ALERT);
/*
*-ERESTARTSYS is returned so as to avoid false alert and to
* resume the call.
*/
if (ret < 0)
return ret;
return sprintf(buf, "%d\n", alert_data->therm_alert);
}
static ssize_t thermal_alert_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct usr_alrt_strct *alert_data = dev_get_drvdata(dev);
if (!alert_data)
return -EINVAL;
return sprintf(buf, "%d\n", alert_data->therm_alert);
}
/*
* Set the time out duration(in ms) to wait when alert is not preset.
* When alert occurs or if alert is already preset, this value has
* no significance
*/
static ssize_t thermal_alert_block_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
long val;
struct usr_alrt_strct *alert_data = dev_get_drvdata(dev);
if (!alert_data)
return -EINVAL;
if (kstrtol(buf, 0, &val))
return -EINVAL;
mutex_lock(&alert_data->alert_timeout_lock);
alert_data->alert_timeout_ms = val;
mutex_unlock(&alert_data->alert_timeout_lock);
return count;
}
/*
* Cooling device operations.
*/
static struct thermal_cooling_device_ops userspace_alert_cdev_ops = {
.get_max_state = userspace_alert_cdev_max_state,
.get_cur_state = userspace_alert_cdev_cur_state,
.set_cur_state = userspace_alert_cdev_set_state,
};
static DEVICE_ATTR(thermal_alert_block, S_IRUGO | S_IWUSR,
thermal_alert_block_show, thermal_alert_block_store);
static DEVICE_ATTR(thermal_alert, S_IRUGO, thermal_alert_show, NULL);
static const struct attribute *userspace_cdev_attr[] = {
&dev_attr_thermal_alert_block.attr,
&dev_attr_thermal_alert.attr,
NULL,
};
static int userspace_alert_therm_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
const char *str;
char *cdev_type;
struct usr_alrt_strct *alert_data;
struct device_node *np = dev->of_node;
alert_data = devm_kzalloc(dev, sizeof(struct usr_alrt_strct),
GFP_KERNEL);
if (!alert_data)
return -ENOMEM;
if (of_property_read_string(np, "cdev-type", &str) == 0) {
cdev_type = (char *)str;
} else {
dev_err(dev, "missing cdev-type\n");
return -EINVAL;
}
alert_data->cur_state = NO_ALERT;
alert_data->therm_alert = 0;
alert_data->alert_timeout_ms = 0;
alert_data->syfs_created = 0;
alert_data->syfs_symlink_created = 0;
mutex_init(&alert_data->alert_lock);
mutex_init(&alert_data->alert_timeout_lock);
init_waitqueue_head(&alert_data->alert_wait_queue);
dev_set_drvdata(dev, alert_data);
alert_data->cdev = thermal_of_cooling_device_register(np, cdev_type,
alert_data, &userspace_alert_cdev_ops);
if (IS_ERR(alert_data->cdev))
return PTR_ERR(alert_data->cdev);
if (sysfs_create_files(&dev->kobj, userspace_cdev_attr) < 0)
pr_warn("\n userspace alert cdev: failed to create sysfs files\n");
else
alert_data->syfs_created = 1;
if (sysfs_create_link(&alert_data->cdev->device.kobj, &dev->kobj,
"userspace_alert") < 0)
pr_warn("\n userspace alert cdev: failed to create sysfs symlink\n");
else
alert_data->syfs_symlink_created = 1;
pr_info("%s cooling device registered.\n", cdev_type);
return 0;
}
static int userspace_alert_therm_remove(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct usr_alrt_strct *alert_data = dev_get_drvdata(dev);
struct thermal_cooling_device *cdev;
if (alert_data) {
cdev = alert_data->cdev;
if ((cdev != NULL) && (alert_data->syfs_symlink_created))
sysfs_remove_link(&cdev->device.kobj,
"userspace_alert");
if (alert_data->syfs_created)
sysfs_remove_files(&dev->kobj, userspace_cdev_attr);
mutex_destroy(&alert_data->alert_lock);
mutex_destroy(&alert_data->alert_timeout_lock);
thermal_cooling_device_unregister(alert_data->cdev);
}
return 0;
}
static const struct of_device_id userspace_therm_of_match[] = {
{ .compatible = "userspace-therm-alert", },
{ },
};
MODULE_DEVICE_TABLE(of, userspace_therm_of_match);
static struct platform_driver userspace_therm_driver = {
.driver = {
.name = "userspace-therm-alert",
.owner = THIS_MODULE,
.of_match_table = userspace_therm_of_match,
},
.probe = userspace_alert_therm_probe,
.remove = userspace_alert_therm_remove,
};
module_platform_driver(userspace_therm_driver);
MODULE_DESCRIPTION("Userspace thermal alert cooling device module");
MODULE_AUTHOR("NVIDIA");
MODULE_LICENSE("GPL v2");
|