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 enum thermal_genl_multicast_groups {
17 THERMAL_GENL_SAMPLING_GROUP = 0,
18 THERMAL_GENL_EVENT_GROUP = 1,
21 static const struct genl_multicast_group thermal_genl_mcgrps[] = {
22 [THERMAL_GENL_SAMPLING_GROUP] = { .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
23 [THERMAL_GENL_EVENT_GROUP] = { .name = THERMAL_GENL_EVENT_GROUP_NAME, },
26 static const struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
28 [THERMAL_GENL_ATTR_TZ] = { .type = NLA_NESTED },
29 [THERMAL_GENL_ATTR_TZ_ID] = { .type = NLA_U32 },
30 [THERMAL_GENL_ATTR_TZ_TEMP] = { .type = NLA_U32 },
31 [THERMAL_GENL_ATTR_TZ_TRIP] = { .type = NLA_NESTED },
32 [THERMAL_GENL_ATTR_TZ_TRIP_ID] = { .type = NLA_U32 },
33 [THERMAL_GENL_ATTR_TZ_TRIP_TEMP] = { .type = NLA_U32 },
34 [THERMAL_GENL_ATTR_TZ_TRIP_TYPE] = { .type = NLA_U32 },
35 [THERMAL_GENL_ATTR_TZ_TRIP_HYST] = { .type = NLA_U32 },
36 [THERMAL_GENL_ATTR_TZ_MODE] = { .type = NLA_U32 },
37 [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT] = { .type = NLA_U32 },
38 [THERMAL_GENL_ATTR_TZ_NAME] = { .type = NLA_STRING,
39 .len = THERMAL_NAME_LENGTH },
41 [THERMAL_GENL_ATTR_TZ_GOV] = { .type = NLA_NESTED },
42 [THERMAL_GENL_ATTR_TZ_GOV_NAME] = { .type = NLA_STRING,
43 .len = THERMAL_NAME_LENGTH },
45 [THERMAL_GENL_ATTR_CDEV] = { .type = NLA_NESTED },
46 [THERMAL_GENL_ATTR_CDEV_ID] = { .type = NLA_U32 },
47 [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 },
48 [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 },
49 [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING,
50 .len = THERMAL_NAME_LENGTH },
51 /* CPU capabilities */
52 [THERMAL_GENL_ATTR_CPU_CAPABILITY] = { .type = NLA_NESTED },
53 [THERMAL_GENL_ATTR_CPU_CAPABILITY_ID] = { .type = NLA_U32 },
54 [THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE] = { .type = NLA_U32 },
55 [THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY] = { .type = NLA_U32 },
59 struct nlattr **attrs;
71 struct thermal_genl_cpu_caps *cpu_capabilities;
72 int cpu_capabilities_count;
75 typedef int (*cb_t)(struct param *);
77 static struct genl_family thermal_gnl_family;
79 static int thermal_group_has_listeners(enum thermal_genl_multicast_groups group)
81 return genl_has_listeners(&thermal_gnl_family, &init_net, group);
84 /************************** Sampling encoding *******************************/
86 int thermal_genl_sampling_temp(int id, int temp)
91 if (!thermal_group_has_listeners(THERMAL_GENL_SAMPLING_GROUP))
94 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
98 hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0,
99 THERMAL_GENL_SAMPLING_TEMP);
103 if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_ID, id))
106 if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_TEMP, temp))
109 genlmsg_end(skb, hdr);
111 genlmsg_multicast(&thermal_gnl_family, skb, 0, THERMAL_GENL_SAMPLING_GROUP, GFP_KERNEL);
115 genlmsg_cancel(skb, hdr);
122 /**************************** Event encoding *********************************/
124 static int thermal_genl_event_tz_create(struct param *p)
126 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
127 nla_put_string(p->msg, THERMAL_GENL_ATTR_TZ_NAME, p->name))
133 static int thermal_genl_event_tz(struct param *p)
135 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
141 static int thermal_genl_event_tz_trip_up(struct param *p)
143 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
144 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
145 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TEMP, p->temp))
151 static int thermal_genl_event_tz_trip_change(struct param *p)
153 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
154 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
155 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, p->trip_type) ||
156 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, p->trip_temp) ||
157 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, p->trip_hyst))
163 static int thermal_genl_event_cdev_add(struct param *p)
165 if (nla_put_string(p->msg, THERMAL_GENL_ATTR_CDEV_NAME,
167 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
169 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_MAX_STATE,
176 static int thermal_genl_event_cdev_delete(struct param *p)
178 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID, p->cdev_id))
184 static int thermal_genl_event_cdev_state_update(struct param *p)
186 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
188 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_CUR_STATE,
195 static int thermal_genl_event_gov_change(struct param *p)
197 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
198 nla_put_string(p->msg, THERMAL_GENL_ATTR_GOV_NAME, p->name))
204 static int thermal_genl_event_cpu_capability_change(struct param *p)
206 struct thermal_genl_cpu_caps *cpu_cap = p->cpu_capabilities;
207 struct sk_buff *msg = p->msg;
208 struct nlattr *start_cap;
211 start_cap = nla_nest_start(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY);
215 for (i = 0; i < p->cpu_capabilities_count; ++i) {
216 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_ID,
218 goto out_cancel_nest;
220 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE,
221 cpu_cap->performance))
222 goto out_cancel_nest;
224 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY,
225 cpu_cap->efficiency))
226 goto out_cancel_nest;
231 nla_nest_end(msg, start_cap);
235 nla_nest_cancel(msg, start_cap);
240 int thermal_genl_event_tz_delete(struct param *p)
241 __attribute__((alias("thermal_genl_event_tz")));
243 int thermal_genl_event_tz_enable(struct param *p)
244 __attribute__((alias("thermal_genl_event_tz")));
246 int thermal_genl_event_tz_disable(struct param *p)
247 __attribute__((alias("thermal_genl_event_tz")));
249 int thermal_genl_event_tz_trip_down(struct param *p)
250 __attribute__((alias("thermal_genl_event_tz_trip_up")));
252 static cb_t event_cb[] = {
253 [THERMAL_GENL_EVENT_TZ_CREATE] = thermal_genl_event_tz_create,
254 [THERMAL_GENL_EVENT_TZ_DELETE] = thermal_genl_event_tz_delete,
255 [THERMAL_GENL_EVENT_TZ_ENABLE] = thermal_genl_event_tz_enable,
256 [THERMAL_GENL_EVENT_TZ_DISABLE] = thermal_genl_event_tz_disable,
257 [THERMAL_GENL_EVENT_TZ_TRIP_UP] = thermal_genl_event_tz_trip_up,
258 [THERMAL_GENL_EVENT_TZ_TRIP_DOWN] = thermal_genl_event_tz_trip_down,
259 [THERMAL_GENL_EVENT_TZ_TRIP_CHANGE] = thermal_genl_event_tz_trip_change,
260 [THERMAL_GENL_EVENT_CDEV_ADD] = thermal_genl_event_cdev_add,
261 [THERMAL_GENL_EVENT_CDEV_DELETE] = thermal_genl_event_cdev_delete,
262 [THERMAL_GENL_EVENT_CDEV_STATE_UPDATE] = thermal_genl_event_cdev_state_update,
263 [THERMAL_GENL_EVENT_TZ_GOV_CHANGE] = thermal_genl_event_gov_change,
264 [THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE] = thermal_genl_event_cpu_capability_change,
268 * Generic netlink event encoding
270 static int thermal_genl_send_event(enum thermal_genl_event event,
277 if (!thermal_group_has_listeners(THERMAL_GENL_EVENT_GROUP))
280 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
285 hdr = genlmsg_put(msg, 0, 0, &thermal_gnl_family, 0, event);
289 ret = event_cb[event](p);
293 genlmsg_end(msg, hdr);
295 genlmsg_multicast(&thermal_gnl_family, msg, 0, THERMAL_GENL_EVENT_GROUP, GFP_KERNEL);
300 genlmsg_cancel(msg, hdr);
307 int thermal_notify_tz_create(const struct thermal_zone_device *tz)
309 struct param p = { .tz_id = tz->id, .name = tz->type };
311 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
314 int thermal_notify_tz_delete(const struct thermal_zone_device *tz)
316 struct param p = { .tz_id = tz->id };
318 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
321 int thermal_notify_tz_enable(const struct thermal_zone_device *tz)
323 struct param p = { .tz_id = tz->id };
325 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
328 int thermal_notify_tz_disable(const struct thermal_zone_device *tz)
330 struct param p = { .tz_id = tz->id };
332 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
335 int thermal_notify_tz_trip_down(const struct thermal_zone_device *tz,
336 const struct thermal_trip *trip)
338 struct param p = { .tz_id = tz->id,
339 .trip_id = thermal_zone_trip_id(tz, trip),
340 .temp = tz->temperature };
342 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
345 int thermal_notify_tz_trip_up(const struct thermal_zone_device *tz,
346 const struct thermal_trip *trip)
348 struct param p = { .tz_id = tz->id,
349 .trip_id = thermal_zone_trip_id(tz, trip),
350 .temp = tz->temperature };
352 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
355 int thermal_notify_tz_trip_change(const struct thermal_zone_device *tz,
356 const struct thermal_trip *trip)
358 struct param p = { .tz_id = tz->id,
359 .trip_id = thermal_zone_trip_id(tz, trip),
360 .trip_type = trip->type,
361 .trip_temp = trip->temperature,
362 .trip_hyst = trip->hysteresis };
364 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
367 int thermal_notify_cdev_state_update(const struct thermal_cooling_device *cdev,
370 struct param p = { .cdev_id = cdev->id, .cdev_state = state };
372 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_STATE_UPDATE, &p);
375 int thermal_notify_cdev_add(const struct thermal_cooling_device *cdev)
377 struct param p = { .cdev_id = cdev->id, .name = cdev->type,
378 .cdev_max_state = cdev->max_state };
380 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
383 int thermal_notify_cdev_delete(const struct thermal_cooling_device *cdev)
385 struct param p = { .cdev_id = cdev->id };
387 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
390 int thermal_notify_tz_gov_change(const struct thermal_zone_device *tz,
393 struct param p = { .tz_id = tz->id, .name = name };
395 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
398 int thermal_genl_cpu_capability_event(int count,
399 struct thermal_genl_cpu_caps *caps)
401 struct param p = { .cpu_capabilities_count = count, .cpu_capabilities = caps };
403 return thermal_genl_send_event(THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE, &p);
405 EXPORT_SYMBOL_GPL(thermal_genl_cpu_capability_event);
407 /*************************** Command encoding ********************************/
409 static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
412 struct sk_buff *msg = data;
414 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
415 nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
421 static int thermal_genl_cmd_tz_get_id(struct param *p)
423 struct sk_buff *msg = p->msg;
424 struct nlattr *start_tz;
427 start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
431 ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get_id, msg);
433 goto out_cancel_nest;
435 nla_nest_end(msg, start_tz);
440 nla_nest_cancel(msg, start_tz);
445 static int thermal_genl_cmd_tz_get_trip(struct param *p)
447 struct sk_buff *msg = p->msg;
448 const struct thermal_trip *trip;
449 struct thermal_zone_device *tz;
450 struct nlattr *start_trip;
453 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
456 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
458 tz = thermal_zone_get_by_id(id);
462 start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
466 mutex_lock(&tz->lock);
468 for_each_trip(tz, trip) {
469 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID,
470 thermal_zone_trip_id(tz, trip)) ||
471 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip->type) ||
472 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, trip->temperature) ||
473 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, trip->hysteresis))
474 goto out_cancel_nest;
477 mutex_unlock(&tz->lock);
479 nla_nest_end(msg, start_trip);
484 mutex_unlock(&tz->lock);
489 static int thermal_genl_cmd_tz_get_temp(struct param *p)
491 struct sk_buff *msg = p->msg;
492 struct thermal_zone_device *tz;
495 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
498 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
500 tz = thermal_zone_get_by_id(id);
504 ret = thermal_zone_get_temp(tz, &temp);
508 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
509 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
515 static int thermal_genl_cmd_tz_get_gov(struct param *p)
517 struct sk_buff *msg = p->msg;
518 struct thermal_zone_device *tz;
521 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
524 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
526 tz = thermal_zone_get_by_id(id);
530 mutex_lock(&tz->lock);
532 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
533 nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
537 mutex_unlock(&tz->lock);
542 static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
545 struct sk_buff *msg = data;
547 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
550 if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
556 static int thermal_genl_cmd_cdev_get(struct param *p)
558 struct sk_buff *msg = p->msg;
559 struct nlattr *start_cdev;
562 start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
566 ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
568 goto out_cancel_nest;
570 nla_nest_end(msg, start_cdev);
574 nla_nest_cancel(msg, start_cdev);
579 static cb_t cmd_cb[] = {
580 [THERMAL_GENL_CMD_TZ_GET_ID] = thermal_genl_cmd_tz_get_id,
581 [THERMAL_GENL_CMD_TZ_GET_TRIP] = thermal_genl_cmd_tz_get_trip,
582 [THERMAL_GENL_CMD_TZ_GET_TEMP] = thermal_genl_cmd_tz_get_temp,
583 [THERMAL_GENL_CMD_TZ_GET_GOV] = thermal_genl_cmd_tz_get_gov,
584 [THERMAL_GENL_CMD_CDEV_GET] = thermal_genl_cmd_cdev_get,
587 static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
588 struct netlink_callback *cb)
590 struct param p = { .msg = skb };
591 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
592 int cmd = info->op.cmd;
596 hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0, cmd);
600 ret = cmd_cb[cmd](&p);
604 genlmsg_end(skb, hdr);
609 genlmsg_cancel(skb, hdr);
614 static int thermal_genl_cmd_doit(struct sk_buff *skb,
615 struct genl_info *info)
617 struct param p = { .attrs = info->attrs };
620 int cmd = info->genlhdr->cmd;
623 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
628 hdr = genlmsg_put_reply(msg, info, &thermal_gnl_family, 0, cmd);
632 ret = cmd_cb[cmd](&p);
636 genlmsg_end(msg, hdr);
638 return genlmsg_reply(msg, info);
641 genlmsg_cancel(msg, hdr);
648 static const struct genl_small_ops thermal_genl_ops[] = {
650 .cmd = THERMAL_GENL_CMD_TZ_GET_ID,
651 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
652 .dumpit = thermal_genl_cmd_dumpit,
655 .cmd = THERMAL_GENL_CMD_TZ_GET_TRIP,
656 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
657 .doit = thermal_genl_cmd_doit,
660 .cmd = THERMAL_GENL_CMD_TZ_GET_TEMP,
661 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
662 .doit = thermal_genl_cmd_doit,
665 .cmd = THERMAL_GENL_CMD_TZ_GET_GOV,
666 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
667 .doit = thermal_genl_cmd_doit,
670 .cmd = THERMAL_GENL_CMD_CDEV_GET,
671 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
672 .dumpit = thermal_genl_cmd_dumpit,
676 static struct genl_family thermal_gnl_family __ro_after_init = {
678 .name = THERMAL_GENL_FAMILY_NAME,
679 .version = THERMAL_GENL_VERSION,
680 .maxattr = THERMAL_GENL_ATTR_MAX,
681 .policy = thermal_genl_policy,
682 .small_ops = thermal_genl_ops,
683 .n_small_ops = ARRAY_SIZE(thermal_genl_ops),
684 .resv_start_op = THERMAL_GENL_CMD_CDEV_GET + 1,
685 .mcgrps = thermal_genl_mcgrps,
686 .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps),
689 int __init thermal_netlink_init(void)
691 return genl_register_family(&thermal_gnl_family);
694 void __init thermal_netlink_exit(void)
696 genl_unregister_family(&thermal_gnl_family);