1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2024, Intel Corporation
4 *
5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
6 *
7 * Thermal zone tempalates handling for thermal core testing.
8 */
9
10 #define pr_fmt(fmt) "thermal-testing: " fmt
11
12 #include <linux/debugfs.h>
13 #include <linux/idr.h>
14 #include <linux/list.h>
15 #include <linux/thermal.h>
16 #include <linux/workqueue.h>
17
18 #include "thermal_testing.h"
19
20 #define TT_MAX_FILE_NAME_LENGTH 16
21
22 /**
23 * struct tt_thermal_zone - Testing thermal zone template
24 *
25 * Represents a template of a thermal zone that can be used for registering
26 * a test thermal zone with the thermal core.
27 *
28 * @list_node: Node in the list of all testing thermal zone templates.
29 * @trips: List of trip point templates for this thermal zone template.
30 * @d_tt_zone: Directory in debugfs representing this template.
31 * @tz: Test thermal zone based on this template, if present.
32 * @lock: Mutex for synchronizing changes of this template.
33 * @ida: IDA for trip point IDs.
34 * @id: The ID of this template for the debugfs interface.
35 * @temp: Temperature value.
36 * @tz_temp: Current thermal zone temperature (after registration).
37 * @num_trips: Number of trip points in the @trips list.
38 * @refcount: Reference counter for usage and removal synchronization.
39 */
40 struct tt_thermal_zone {
41 struct list_head list_node;
42 struct list_head trips;
43 struct dentry *d_tt_zone;
44 struct thermal_zone_device *tz;
45 struct mutex lock;
46 struct ida ida;
47 int id;
48 int temp;
49 int tz_temp;
50 unsigned int num_trips;
51 unsigned int refcount;
52 };
53
54 DEFINE_GUARD(tt_zone, struct tt_thermal_zone *, mutex_lock(&_T->lock), mutex_unlock(&_T->lock))
55
56 /**
57 * struct tt_trip - Testing trip point template
58 *
59 * Represents a template of a trip point to be used for populating a trip point
60 * during the registration of a thermal zone based on a given zone template.
61 *
62 * @list_node: Node in the list of all trip templates in the zone template.
63 * @trip: Trip point data to use for thernal zone registration.
64 * @id: The ID of this trip template for the debugfs interface.
65 */
66 struct tt_trip {
67 struct list_head list_node;
68 struct thermal_trip trip;
69 int id;
70 };
71
72 /*
73 * It is both questionable and potentially problematic from the sychnronization
74 * perspective to attempt to manipulate debugfs from within a debugfs file
75 * "write" operation, so auxiliary work items are used for that. The majority
76 * of zone-related command functions have a part that runs from a workqueue and
77 * make changes in debugs, among other things.
78 */
79 struct tt_work {
80 struct work_struct work;
81 struct tt_thermal_zone *tt_zone;
82 struct tt_trip *tt_trip;
83 };
84
tt_work_of_work(struct work_struct * work)85 static inline struct tt_work *tt_work_of_work(struct work_struct *work)
86 {
87 return container_of(work, struct tt_work, work);
88 }
89
90 static LIST_HEAD(tt_thermal_zones);
91 static DEFINE_IDA(tt_thermal_zones_ida);
92 static DEFINE_MUTEX(tt_thermal_zones_lock);
93
tt_int_get(void * data,u64 * val)94 static int tt_int_get(void *data, u64 *val)
95 {
96 *val = *(int *)data;
97 return 0;
98 }
tt_int_set(void * data,u64 val)99 static int tt_int_set(void *data, u64 val)
100 {
101 if ((int)val < THERMAL_TEMP_INVALID)
102 return -EINVAL;
103
104 *(int *)data = val;
105 return 0;
106 }
107 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(tt_int_attr, tt_int_get, tt_int_set, "%lld\n");
108 DEFINE_DEBUGFS_ATTRIBUTE(tt_unsigned_int_attr, tt_int_get, tt_int_set, "%llu\n");
109
tt_zone_tz_temp_get(void * data,u64 * val)110 static int tt_zone_tz_temp_get(void *data, u64 *val)
111 {
112 struct tt_thermal_zone *tt_zone = data;
113
114 guard(tt_zone)(tt_zone);
115
116 if (!tt_zone->tz)
117 return -EBUSY;
118
119 *val = tt_zone->tz_temp;
120
121 return 0;
122 }
tt_zone_tz_temp_set(void * data,u64 val)123 static int tt_zone_tz_temp_set(void *data, u64 val)
124 {
125 struct tt_thermal_zone *tt_zone = data;
126
127 guard(tt_zone)(tt_zone);
128
129 if (!tt_zone->tz)
130 return -EBUSY;
131
132 WRITE_ONCE(tt_zone->tz_temp, val);
133 thermal_zone_device_update(tt_zone->tz, THERMAL_EVENT_TEMP_SAMPLE);
134
135 return 0;
136 }
137 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(tt_zone_tz_temp_attr, tt_zone_tz_temp_get,
138 tt_zone_tz_temp_set, "%lld\n");
139
tt_zone_free_trips(struct tt_thermal_zone * tt_zone)140 static void tt_zone_free_trips(struct tt_thermal_zone *tt_zone)
141 {
142 struct tt_trip *tt_trip, *aux;
143
144 list_for_each_entry_safe(tt_trip, aux, &tt_zone->trips, list_node) {
145 list_del(&tt_trip->list_node);
146 ida_free(&tt_zone->ida, tt_trip->id);
147 kfree(tt_trip);
148 }
149 }
150
tt_zone_free(struct tt_thermal_zone * tt_zone)151 static void tt_zone_free(struct tt_thermal_zone *tt_zone)
152 {
153 tt_zone_free_trips(tt_zone);
154 ida_free(&tt_thermal_zones_ida, tt_zone->id);
155 ida_destroy(&tt_zone->ida);
156 kfree(tt_zone);
157 }
158
tt_add_tz_work_fn(struct work_struct * work)159 static void tt_add_tz_work_fn(struct work_struct *work)
160 {
161 struct tt_work *tt_work = tt_work_of_work(work);
162 struct tt_thermal_zone *tt_zone = tt_work->tt_zone;
163 char f_name[TT_MAX_FILE_NAME_LENGTH];
164
165 kfree(tt_work);
166
167 snprintf(f_name, TT_MAX_FILE_NAME_LENGTH, "tz%d", tt_zone->id);
168 tt_zone->d_tt_zone = debugfs_create_dir(f_name, d_testing);
169 if (IS_ERR(tt_zone->d_tt_zone)) {
170 tt_zone_free(tt_zone);
171 return;
172 }
173
174 debugfs_create_file_unsafe("temp", 0600, tt_zone->d_tt_zone, tt_zone,
175 &tt_zone_tz_temp_attr);
176
177 debugfs_create_file_unsafe("init_temp", 0600, tt_zone->d_tt_zone,
178 &tt_zone->temp, &tt_int_attr);
179
180 guard(mutex)(&tt_thermal_zones_lock);
181
182 list_add_tail(&tt_zone->list_node, &tt_thermal_zones);
183 }
184
tt_add_tz(void)185 int tt_add_tz(void)
186 {
187 int ret;
188
189 struct tt_thermal_zone *tt_zone __free(kfree) = kzalloc_obj(*tt_zone);
190 if (!tt_zone)
191 return -ENOMEM;
192
193 struct tt_work *tt_work __free(kfree) = kzalloc_obj(*tt_work);
194 if (!tt_work)
195 return -ENOMEM;
196
197 INIT_LIST_HEAD(&tt_zone->trips);
198 mutex_init(&tt_zone->lock);
199 ida_init(&tt_zone->ida);
200 tt_zone->temp = THERMAL_TEMP_INVALID;
201
202 ret = ida_alloc(&tt_thermal_zones_ida, GFP_KERNEL);
203 if (ret < 0)
204 return ret;
205
206 tt_zone->id = ret;
207
208 INIT_WORK(&tt_work->work, tt_add_tz_work_fn);
209 tt_work->tt_zone = no_free_ptr(tt_zone);
210 schedule_work(&(no_free_ptr(tt_work)->work));
211
212 return 0;
213 }
214
tt_del_tz_work_fn(struct work_struct * work)215 static void tt_del_tz_work_fn(struct work_struct *work)
216 {
217 struct tt_work *tt_work = tt_work_of_work(work);
218 struct tt_thermal_zone *tt_zone = tt_work->tt_zone;
219
220 kfree(tt_work);
221
222 debugfs_remove(tt_zone->d_tt_zone);
223 tt_zone_free(tt_zone);
224 }
225
tt_zone_unregister_tz(struct tt_thermal_zone * tt_zone)226 static void tt_zone_unregister_tz(struct tt_thermal_zone *tt_zone)
227 {
228 guard(tt_zone)(tt_zone);
229
230 if (tt_zone->tz) {
231 thermal_zone_device_unregister(tt_zone->tz);
232 tt_zone->tz = NULL;
233 }
234 }
235
tt_del_tz(const char * arg)236 int tt_del_tz(const char *arg)
237 {
238 struct tt_thermal_zone *tt_zone, *aux;
239 int ret;
240 int id;
241
242 ret = sscanf(arg, "%d", &id);
243 if (ret != 1)
244 return -EINVAL;
245
246 struct tt_work *tt_work __free(kfree) = kzalloc_obj(*tt_work);
247 if (!tt_work)
248 return -ENOMEM;
249
250 guard(mutex)(&tt_thermal_zones_lock);
251
252 ret = -EINVAL;
253 list_for_each_entry_safe(tt_zone, aux, &tt_thermal_zones, list_node) {
254 if (tt_zone->id == id) {
255 if (tt_zone->refcount) {
256 ret = -EBUSY;
257 } else {
258 list_del(&tt_zone->list_node);
259 ret = 0;
260 }
261 break;
262 }
263 }
264
265 if (ret)
266 return ret;
267
268 tt_zone_unregister_tz(tt_zone);
269
270 INIT_WORK(&tt_work->work, tt_del_tz_work_fn);
271 tt_work->tt_zone = tt_zone;
272 schedule_work(&(no_free_ptr(tt_work)->work));
273
274 return 0;
275 }
276
tt_get_tt_zone(const char * arg)277 static struct tt_thermal_zone *tt_get_tt_zone(const char *arg)
278 {
279 struct tt_thermal_zone *tt_zone;
280 int ret, id;
281
282 ret = sscanf(arg, "%d", &id);
283 if (ret != 1)
284 return ERR_PTR(-EINVAL);
285
286 guard(mutex)(&tt_thermal_zones_lock);
287
288 list_for_each_entry(tt_zone, &tt_thermal_zones, list_node) {
289 if (tt_zone->id == id) {
290 tt_zone->refcount++;
291 return tt_zone;
292 }
293 }
294
295 return ERR_PTR(-EINVAL);
296 }
297
tt_put_tt_zone(struct tt_thermal_zone * tt_zone)298 static void tt_put_tt_zone(struct tt_thermal_zone *tt_zone)
299 {
300 guard(mutex)(&tt_thermal_zones_lock);
301
302 tt_zone->refcount--;
303 }
304
305 DEFINE_FREE(put_tt_zone, struct tt_thermal_zone *,
306 if (!IS_ERR_OR_NULL(_T)) tt_put_tt_zone(_T))
307
tt_zone_add_trip_work_fn(struct work_struct * work)308 static void tt_zone_add_trip_work_fn(struct work_struct *work)
309 {
310 struct tt_work *tt_work = tt_work_of_work(work);
311 struct tt_thermal_zone *tt_zone = tt_work->tt_zone;
312 struct tt_trip *tt_trip = tt_work->tt_trip;
313 char d_name[TT_MAX_FILE_NAME_LENGTH];
314
315 kfree(tt_work);
316
317 snprintf(d_name, TT_MAX_FILE_NAME_LENGTH, "trip_%d_temp", tt_trip->id);
318 debugfs_create_file_unsafe(d_name, 0600, tt_zone->d_tt_zone,
319 &tt_trip->trip.temperature, &tt_int_attr);
320
321 snprintf(d_name, TT_MAX_FILE_NAME_LENGTH, "trip_%d_hyst", tt_trip->id);
322 debugfs_create_file_unsafe(d_name, 0600, tt_zone->d_tt_zone,
323 &tt_trip->trip.hysteresis, &tt_unsigned_int_attr);
324
325 tt_put_tt_zone(tt_zone);
326 }
327
tt_zone_add_trip(const char * arg)328 int tt_zone_add_trip(const char *arg)
329 {
330 int id;
331
332 struct tt_work *tt_work __free(kfree) = kzalloc_obj(*tt_work);
333 if (!tt_work)
334 return -ENOMEM;
335
336 struct tt_trip *tt_trip __free(kfree) = kzalloc_obj(*tt_trip);
337 if (!tt_trip)
338 return -ENOMEM;
339
340 struct tt_thermal_zone *tt_zone __free(put_tt_zone) = tt_get_tt_zone(arg);
341 if (IS_ERR(tt_zone))
342 return PTR_ERR(tt_zone);
343
344 id = ida_alloc(&tt_zone->ida, GFP_KERNEL);
345 if (id < 0)
346 return id;
347
348 tt_trip->trip.type = THERMAL_TRIP_ACTIVE;
349 tt_trip->trip.temperature = THERMAL_TEMP_INVALID;
350 tt_trip->trip.flags = THERMAL_TRIP_FLAG_RW;
351 tt_trip->id = id;
352
353 guard(tt_zone)(tt_zone);
354
355 list_add_tail(&tt_trip->list_node, &tt_zone->trips);
356 tt_zone->num_trips++;
357
358 INIT_WORK(&tt_work->work, tt_zone_add_trip_work_fn);
359 tt_work->tt_zone = no_free_ptr(tt_zone);
360 tt_work->tt_trip = no_free_ptr(tt_trip);
361 schedule_work(&(no_free_ptr(tt_work)->work));
362
363 return 0;
364 }
365
tt_zone_get_temp(struct thermal_zone_device * tz,int * temp)366 static int tt_zone_get_temp(struct thermal_zone_device *tz, int *temp)
367 {
368 struct tt_thermal_zone *tt_zone = thermal_zone_device_priv(tz);
369
370 *temp = READ_ONCE(tt_zone->tz_temp);
371
372 if (*temp < THERMAL_TEMP_INVALID)
373 return -ENODATA;
374
375 return 0;
376 }
377
378 static const struct thermal_zone_device_ops tt_zone_ops = {
379 .get_temp = tt_zone_get_temp,
380 };
381
tt_zone_register_tz(struct tt_thermal_zone * tt_zone)382 static int tt_zone_register_tz(struct tt_thermal_zone *tt_zone)
383 {
384 struct thermal_zone_device *tz;
385 struct tt_trip *tt_trip;
386 int i;
387
388 guard(tt_zone)(tt_zone);
389
390 if (tt_zone->tz)
391 return -EINVAL;
392
393 struct thermal_trip *trips __free(kfree) = kzalloc_objs(*trips,
394 tt_zone->num_trips);
395 if (!trips)
396 return -ENOMEM;
397
398 i = 0;
399 list_for_each_entry(tt_trip, &tt_zone->trips, list_node)
400 trips[i++] = tt_trip->trip;
401
402 tt_zone->tz_temp = tt_zone->temp;
403
404 tz = thermal_zone_device_register_with_trips("test_tz", trips, i, tt_zone,
405 &tt_zone_ops, NULL, 0, 0);
406 if (IS_ERR(tz))
407 return PTR_ERR(tz);
408
409 tt_zone->tz = tz;
410
411 thermal_zone_device_enable(tz);
412
413 return 0;
414 }
415
tt_zone_reg(const char * arg)416 int tt_zone_reg(const char *arg)
417 {
418 struct tt_thermal_zone *tt_zone __free(put_tt_zone) = tt_get_tt_zone(arg);
419 if (IS_ERR(tt_zone))
420 return PTR_ERR(tt_zone);
421
422 return tt_zone_register_tz(tt_zone);
423 }
424
tt_zone_unreg(const char * arg)425 int tt_zone_unreg(const char *arg)
426 {
427 struct tt_thermal_zone *tt_zone __free(put_tt_zone) = tt_get_tt_zone(arg);
428 if (IS_ERR(tt_zone))
429 return PTR_ERR(tt_zone);
430
431 tt_zone_unregister_tz(tt_zone);
432
433 return 0;
434 }
435
tt_zone_cleanup(void)436 void tt_zone_cleanup(void)
437 {
438 struct tt_thermal_zone *tt_zone, *aux;
439
440 list_for_each_entry_safe(tt_zone, aux, &tt_thermal_zones, list_node) {
441 tt_zone_unregister_tz(tt_zone);
442
443 list_del(&tt_zone->list_node);
444
445 tt_zone_free(tt_zone);
446 }
447 }
448