1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020 Linaro Limited
5 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
7 * Generic netlink for thermal management framework
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <net/genetlink.h>
12 #include <uapi/linux/thermal.h>
14 #include "thermal_core.h"
16 static const struct genl_multicast_group thermal_genl_mcgrps[] = {
17 { .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
18 { .name = THERMAL_GENL_EVENT_GROUP_NAME, },
21 static const struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
23 [THERMAL_GENL_ATTR_TZ] = { .type = NLA_NESTED },
24 [THERMAL_GENL_ATTR_TZ_ID] = { .type = NLA_U32 },
25 [THERMAL_GENL_ATTR_TZ_TEMP] = { .type = NLA_U32 },
26 [THERMAL_GENL_ATTR_TZ_TRIP] = { .type = NLA_NESTED },
27 [THERMAL_GENL_ATTR_TZ_TRIP_ID] = { .type = NLA_U32 },
28 [THERMAL_GENL_ATTR_TZ_TRIP_TEMP] = { .type = NLA_U32 },
29 [THERMAL_GENL_ATTR_TZ_TRIP_TYPE] = { .type = NLA_U32 },
30 [THERMAL_GENL_ATTR_TZ_TRIP_HYST] = { .type = NLA_U32 },
31 [THERMAL_GENL_ATTR_TZ_MODE] = { .type = NLA_U32 },
32 [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT] = { .type = NLA_U32 },
33 [THERMAL_GENL_ATTR_TZ_NAME] = { .type = NLA_STRING,
34 .len = THERMAL_NAME_LENGTH },
36 [THERMAL_GENL_ATTR_TZ_GOV] = { .type = NLA_NESTED },
37 [THERMAL_GENL_ATTR_TZ_GOV_NAME] = { .type = NLA_STRING,
38 .len = THERMAL_NAME_LENGTH },
40 [THERMAL_GENL_ATTR_CDEV] = { .type = NLA_NESTED },
41 [THERMAL_GENL_ATTR_CDEV_ID] = { .type = NLA_U32 },
42 [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 },
43 [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 },
44 [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING,
45 .len = THERMAL_NAME_LENGTH },
46 /* CPU capabilities */
47 [THERMAL_GENL_ATTR_CPU_CAPABILITY] = { .type = NLA_NESTED },
48 [THERMAL_GENL_ATTR_CPU_CAPABILITY_ID] = { .type = NLA_U32 },
49 [THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE] = { .type = NLA_U32 },
50 [THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY] = { .type = NLA_U32 },
54 struct nlattr **attrs;
66 struct thermal_genl_cpu_caps *cpu_capabilities;
67 int cpu_capabilities_count;
70 typedef int (*cb_t)(struct param *);
72 static struct genl_family thermal_gnl_family;
74 /************************** Sampling encoding *******************************/
76 int thermal_genl_sampling_temp(int id, int temp)
81 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
85 hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0,
86 THERMAL_GENL_SAMPLING_TEMP);
90 if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_ID, id))
93 if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_TEMP, temp))
96 genlmsg_end(skb, hdr);
98 genlmsg_multicast(&thermal_gnl_family, skb, 0, 0, GFP_KERNEL);
102 genlmsg_cancel(skb, hdr);
109 /**************************** Event encoding *********************************/
111 static int thermal_genl_event_tz_create(struct param *p)
113 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
114 nla_put_string(p->msg, THERMAL_GENL_ATTR_TZ_NAME, p->name))
120 static int thermal_genl_event_tz(struct param *p)
122 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
128 static int thermal_genl_event_tz_trip_up(struct param *p)
130 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
131 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
132 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TEMP, p->temp))
138 static int thermal_genl_event_tz_trip_add(struct param *p)
140 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
141 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
142 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, p->trip_type) ||
143 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, p->trip_temp) ||
144 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, p->trip_hyst))
150 static int thermal_genl_event_tz_trip_delete(struct param *p)
152 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
153 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id))
159 static int thermal_genl_event_cdev_add(struct param *p)
161 if (nla_put_string(p->msg, THERMAL_GENL_ATTR_CDEV_NAME,
163 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
165 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_MAX_STATE,
172 static int thermal_genl_event_cdev_delete(struct param *p)
174 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID, p->cdev_id))
180 static int thermal_genl_event_cdev_state_update(struct param *p)
182 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
184 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_CUR_STATE,
191 static int thermal_genl_event_gov_change(struct param *p)
193 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
194 nla_put_string(p->msg, THERMAL_GENL_ATTR_GOV_NAME, p->name))
200 static int thermal_genl_event_cpu_capability_change(struct param *p)
202 struct thermal_genl_cpu_caps *cpu_cap = p->cpu_capabilities;
203 struct sk_buff *msg = p->msg;
204 struct nlattr *start_cap;
207 start_cap = nla_nest_start(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY);
211 for (i = 0; i < p->cpu_capabilities_count; ++i) {
212 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_ID,
214 goto out_cancel_nest;
216 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE,
217 cpu_cap->performance))
218 goto out_cancel_nest;
220 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY,
221 cpu_cap->efficiency))
222 goto out_cancel_nest;
227 nla_nest_end(msg, start_cap);
231 nla_nest_cancel(msg, start_cap);
236 int thermal_genl_event_tz_delete(struct param *p)
237 __attribute__((alias("thermal_genl_event_tz")));
239 int thermal_genl_event_tz_enable(struct param *p)
240 __attribute__((alias("thermal_genl_event_tz")));
242 int thermal_genl_event_tz_disable(struct param *p)
243 __attribute__((alias("thermal_genl_event_tz")));
245 int thermal_genl_event_tz_trip_down(struct param *p)
246 __attribute__((alias("thermal_genl_event_tz_trip_up")));
248 int thermal_genl_event_tz_trip_change(struct param *p)
249 __attribute__((alias("thermal_genl_event_tz_trip_add")));
251 static cb_t event_cb[] = {
252 [THERMAL_GENL_EVENT_TZ_CREATE] = thermal_genl_event_tz_create,
253 [THERMAL_GENL_EVENT_TZ_DELETE] = thermal_genl_event_tz_delete,
254 [THERMAL_GENL_EVENT_TZ_ENABLE] = thermal_genl_event_tz_enable,
255 [THERMAL_GENL_EVENT_TZ_DISABLE] = thermal_genl_event_tz_disable,
256 [THERMAL_GENL_EVENT_TZ_TRIP_UP] = thermal_genl_event_tz_trip_up,
257 [THERMAL_GENL_EVENT_TZ_TRIP_DOWN] = thermal_genl_event_tz_trip_down,
258 [THERMAL_GENL_EVENT_TZ_TRIP_CHANGE] = thermal_genl_event_tz_trip_change,
259 [THERMAL_GENL_EVENT_TZ_TRIP_ADD] = thermal_genl_event_tz_trip_add,
260 [THERMAL_GENL_EVENT_TZ_TRIP_DELETE] = thermal_genl_event_tz_trip_delete,
261 [THERMAL_GENL_EVENT_CDEV_ADD] = thermal_genl_event_cdev_add,
262 [THERMAL_GENL_EVENT_CDEV_DELETE] = thermal_genl_event_cdev_delete,
263 [THERMAL_GENL_EVENT_CDEV_STATE_UPDATE] = thermal_genl_event_cdev_state_update,
264 [THERMAL_GENL_EVENT_TZ_GOV_CHANGE] = thermal_genl_event_gov_change,
265 [THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE] = thermal_genl_event_cpu_capability_change,
269 * Generic netlink event encoding
271 static int thermal_genl_send_event(enum thermal_genl_event event,
278 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
283 hdr = genlmsg_put(msg, 0, 0, &thermal_gnl_family, 0, event);
287 ret = event_cb[event](p);
291 genlmsg_end(msg, hdr);
293 genlmsg_multicast(&thermal_gnl_family, msg, 0, 1, GFP_KERNEL);
298 genlmsg_cancel(msg, hdr);
305 int thermal_notify_tz_create(int tz_id, const char *name)
307 struct param p = { .tz_id = tz_id, .name = name };
309 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
312 int thermal_notify_tz_delete(int tz_id)
314 struct param p = { .tz_id = tz_id };
316 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
319 int thermal_notify_tz_enable(int tz_id)
321 struct param p = { .tz_id = tz_id };
323 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
326 int thermal_notify_tz_disable(int tz_id)
328 struct param p = { .tz_id = tz_id };
330 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
333 int thermal_notify_tz_trip_down(int tz_id, int trip_id, int temp)
335 struct param p = { .tz_id = tz_id, .trip_id = trip_id, .temp = temp };
337 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
340 int thermal_notify_tz_trip_up(int tz_id, int trip_id, int temp)
342 struct param p = { .tz_id = tz_id, .trip_id = trip_id, .temp = temp };
344 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
347 int thermal_notify_tz_trip_add(int tz_id, int trip_id, int trip_type,
348 int trip_temp, int trip_hyst)
350 struct param p = { .tz_id = tz_id, .trip_id = trip_id,
351 .trip_type = trip_type, .trip_temp = trip_temp,
352 .trip_hyst = trip_hyst };
354 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_ADD, &p);
357 int thermal_notify_tz_trip_delete(int tz_id, int trip_id)
359 struct param p = { .tz_id = tz_id, .trip_id = trip_id };
361 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DELETE, &p);
364 int thermal_notify_tz_trip_change(int tz_id, int trip_id, int trip_type,
365 int trip_temp, int trip_hyst)
367 struct param p = { .tz_id = tz_id, .trip_id = trip_id,
368 .trip_type = trip_type, .trip_temp = trip_temp,
369 .trip_hyst = trip_hyst };
371 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
374 int thermal_notify_cdev_state_update(int cdev_id, int cdev_state)
376 struct param p = { .cdev_id = cdev_id, .cdev_state = cdev_state };
378 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_STATE_UPDATE, &p);
381 int thermal_notify_cdev_add(int cdev_id, const char *name, int cdev_max_state)
383 struct param p = { .cdev_id = cdev_id, .name = name,
384 .cdev_max_state = cdev_max_state };
386 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
389 int thermal_notify_cdev_delete(int cdev_id)
391 struct param p = { .cdev_id = cdev_id };
393 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
396 int thermal_notify_tz_gov_change(int tz_id, const char *name)
398 struct param p = { .tz_id = tz_id, .name = name };
400 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
403 int thermal_genl_cpu_capability_event(int count,
404 struct thermal_genl_cpu_caps *caps)
406 struct param p = { .cpu_capabilities_count = count, .cpu_capabilities = caps };
408 return thermal_genl_send_event(THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE, &p);
410 EXPORT_SYMBOL_GPL(thermal_genl_cpu_capability_event);
412 /*************************** Command encoding ********************************/
414 static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
417 struct sk_buff *msg = data;
419 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
420 nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
426 static int thermal_genl_cmd_tz_get_id(struct param *p)
428 struct sk_buff *msg = p->msg;
429 struct nlattr *start_tz;
432 start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
436 ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get_id, msg);
438 goto out_cancel_nest;
440 nla_nest_end(msg, start_tz);
445 nla_nest_cancel(msg, start_tz);
450 static int thermal_genl_cmd_tz_get_trip(struct param *p)
452 struct sk_buff *msg = p->msg;
453 struct thermal_zone_device *tz;
454 struct nlattr *start_trip;
455 struct thermal_trip trip;
458 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
461 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
463 tz = thermal_zone_get_by_id(id);
467 start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
471 mutex_lock(&tz->lock);
473 for (i = 0; i < tz->num_trips; i++) {
475 ret = __thermal_zone_get_trip(tz, i, &trip);
477 goto out_cancel_nest;
479 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, i) ||
480 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip.type) ||
481 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, trip.temperature) ||
482 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, trip.hysteresis))
483 goto out_cancel_nest;
486 mutex_unlock(&tz->lock);
488 nla_nest_end(msg, start_trip);
493 mutex_unlock(&tz->lock);
498 static int thermal_genl_cmd_tz_get_temp(struct param *p)
500 struct sk_buff *msg = p->msg;
501 struct thermal_zone_device *tz;
504 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
507 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
509 tz = thermal_zone_get_by_id(id);
513 ret = thermal_zone_get_temp(tz, &temp);
517 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
518 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
524 static int thermal_genl_cmd_tz_get_gov(struct param *p)
526 struct sk_buff *msg = p->msg;
527 struct thermal_zone_device *tz;
530 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
533 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
535 tz = thermal_zone_get_by_id(id);
539 mutex_lock(&tz->lock);
541 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
542 nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
546 mutex_unlock(&tz->lock);
551 static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
554 struct sk_buff *msg = data;
556 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
559 if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
565 static int thermal_genl_cmd_cdev_get(struct param *p)
567 struct sk_buff *msg = p->msg;
568 struct nlattr *start_cdev;
571 start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
575 ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
577 goto out_cancel_nest;
579 nla_nest_end(msg, start_cdev);
583 nla_nest_cancel(msg, start_cdev);
588 static cb_t cmd_cb[] = {
589 [THERMAL_GENL_CMD_TZ_GET_ID] = thermal_genl_cmd_tz_get_id,
590 [THERMAL_GENL_CMD_TZ_GET_TRIP] = thermal_genl_cmd_tz_get_trip,
591 [THERMAL_GENL_CMD_TZ_GET_TEMP] = thermal_genl_cmd_tz_get_temp,
592 [THERMAL_GENL_CMD_TZ_GET_GOV] = thermal_genl_cmd_tz_get_gov,
593 [THERMAL_GENL_CMD_CDEV_GET] = thermal_genl_cmd_cdev_get,
596 static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
597 struct netlink_callback *cb)
599 struct param p = { .msg = skb };
600 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
601 int cmd = info->op.cmd;
605 hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0, cmd);
609 ret = cmd_cb[cmd](&p);
613 genlmsg_end(skb, hdr);
618 genlmsg_cancel(skb, hdr);
623 static int thermal_genl_cmd_doit(struct sk_buff *skb,
624 struct genl_info *info)
626 struct param p = { .attrs = info->attrs };
629 int cmd = info->genlhdr->cmd;
632 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
637 hdr = genlmsg_put_reply(msg, info, &thermal_gnl_family, 0, cmd);
641 ret = cmd_cb[cmd](&p);
645 genlmsg_end(msg, hdr);
647 return genlmsg_reply(msg, info);
650 genlmsg_cancel(msg, hdr);
657 static const struct genl_small_ops thermal_genl_ops[] = {
659 .cmd = THERMAL_GENL_CMD_TZ_GET_ID,
660 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
661 .dumpit = thermal_genl_cmd_dumpit,
664 .cmd = THERMAL_GENL_CMD_TZ_GET_TRIP,
665 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
666 .doit = thermal_genl_cmd_doit,
669 .cmd = THERMAL_GENL_CMD_TZ_GET_TEMP,
670 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
671 .doit = thermal_genl_cmd_doit,
674 .cmd = THERMAL_GENL_CMD_TZ_GET_GOV,
675 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
676 .doit = thermal_genl_cmd_doit,
679 .cmd = THERMAL_GENL_CMD_CDEV_GET,
680 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
681 .dumpit = thermal_genl_cmd_dumpit,
685 static struct genl_family thermal_gnl_family __ro_after_init = {
687 .name = THERMAL_GENL_FAMILY_NAME,
688 .version = THERMAL_GENL_VERSION,
689 .maxattr = THERMAL_GENL_ATTR_MAX,
690 .policy = thermal_genl_policy,
691 .small_ops = thermal_genl_ops,
692 .n_small_ops = ARRAY_SIZE(thermal_genl_ops),
693 .resv_start_op = THERMAL_GENL_CMD_CDEV_GET + 1,
694 .mcgrps = thermal_genl_mcgrps,
695 .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps),
698 int __init thermal_netlink_init(void)
700 return genl_register_family(&thermal_gnl_family);
703 void __init thermal_netlink_exit(void)
705 genl_unregister_family(&thermal_gnl_family);