1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020 Linaro Limited
7 * Generic netlink for thermal management framework
9 #include <linux/module.h>
10 #include <linux/notifier.h>
11 #include <linux/kernel.h>
12 #include <net/genetlink.h>
13 #include <uapi/linux/thermal.h>
15 #include "thermal_core.h"
17 static const struct genl_multicast_group thermal_genl_mcgrps[] = {
18 [THERMAL_GENL_SAMPLING_GROUP] = { .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
19 [THERMAL_GENL_EVENT_GROUP] = { .name = THERMAL_GENL_EVENT_GROUP_NAME, },
22 static const struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
24 [THERMAL_GENL_ATTR_TZ] = { .type = NLA_NESTED },
25 [THERMAL_GENL_ATTR_TZ_ID] = { .type = NLA_U32 },
26 [THERMAL_GENL_ATTR_TZ_TEMP] = { .type = NLA_U32 },
27 [THERMAL_GENL_ATTR_TZ_TRIP] = { .type = NLA_NESTED },
28 [THERMAL_GENL_ATTR_TZ_TRIP_ID] = { .type = NLA_U32 },
29 [THERMAL_GENL_ATTR_TZ_TRIP_TEMP] = { .type = NLA_U32 },
30 [THERMAL_GENL_ATTR_TZ_TRIP_TYPE] = { .type = NLA_U32 },
31 [THERMAL_GENL_ATTR_TZ_TRIP_HYST] = { .type = NLA_U32 },
32 [THERMAL_GENL_ATTR_TZ_MODE] = { .type = NLA_U32 },
33 [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT] = { .type = NLA_U32 },
34 [THERMAL_GENL_ATTR_TZ_NAME] = { .type = NLA_STRING,
35 .len = THERMAL_NAME_LENGTH },
37 [THERMAL_GENL_ATTR_TZ_GOV] = { .type = NLA_NESTED },
38 [THERMAL_GENL_ATTR_TZ_GOV_NAME] = { .type = NLA_STRING,
39 .len = THERMAL_NAME_LENGTH },
41 [THERMAL_GENL_ATTR_CDEV] = { .type = NLA_NESTED },
42 [THERMAL_GENL_ATTR_CDEV_ID] = { .type = NLA_U32 },
43 [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 },
44 [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 },
45 [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING,
46 .len = THERMAL_NAME_LENGTH },
47 /* CPU capabilities */
48 [THERMAL_GENL_ATTR_CPU_CAPABILITY] = { .type = NLA_NESTED },
49 [THERMAL_GENL_ATTR_CPU_CAPABILITY_ID] = { .type = NLA_U32 },
50 [THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE] = { .type = NLA_U32 },
51 [THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY] = { .type = NLA_U32 },
55 struct nlattr **attrs;
67 struct thermal_genl_cpu_caps *cpu_capabilities;
68 int cpu_capabilities_count;
71 typedef int (*cb_t)(struct param *);
73 static struct genl_family thermal_genl_family;
74 static BLOCKING_NOTIFIER_HEAD(thermal_genl_chain);
76 static int thermal_group_has_listeners(enum thermal_genl_multicast_groups group)
78 return genl_has_listeners(&thermal_genl_family, &init_net, group);
81 /************************** Sampling encoding *******************************/
83 int thermal_genl_sampling_temp(int id, int temp)
88 if (!thermal_group_has_listeners(THERMAL_GENL_SAMPLING_GROUP))
91 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
95 hdr = genlmsg_put(skb, 0, 0, &thermal_genl_family, 0,
96 THERMAL_GENL_SAMPLING_TEMP);
100 if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_ID, id))
103 if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_TEMP, temp))
106 genlmsg_end(skb, hdr);
108 genlmsg_multicast(&thermal_genl_family, skb, 0, THERMAL_GENL_SAMPLING_GROUP, GFP_KERNEL);
112 genlmsg_cancel(skb, hdr);
119 /**************************** Event encoding *********************************/
121 static int thermal_genl_event_tz_create(struct param *p)
123 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
124 nla_put_string(p->msg, THERMAL_GENL_ATTR_TZ_NAME, p->name))
130 static int thermal_genl_event_tz(struct param *p)
132 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
138 static int thermal_genl_event_tz_trip_up(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_TEMP, p->temp))
148 static int thermal_genl_event_tz_trip_change(struct param *p)
150 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
151 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
152 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, p->trip_type) ||
153 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, p->trip_temp) ||
154 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, p->trip_hyst))
160 static int thermal_genl_event_cdev_add(struct param *p)
162 if (nla_put_string(p->msg, THERMAL_GENL_ATTR_CDEV_NAME,
164 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
166 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_MAX_STATE,
173 static int thermal_genl_event_cdev_delete(struct param *p)
175 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID, p->cdev_id))
181 static int thermal_genl_event_cdev_state_update(struct param *p)
183 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
185 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_CUR_STATE,
192 static int thermal_genl_event_gov_change(struct param *p)
194 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
195 nla_put_string(p->msg, THERMAL_GENL_ATTR_GOV_NAME, p->name))
201 static int thermal_genl_event_cpu_capability_change(struct param *p)
203 struct thermal_genl_cpu_caps *cpu_cap = p->cpu_capabilities;
204 struct sk_buff *msg = p->msg;
205 struct nlattr *start_cap;
208 start_cap = nla_nest_start(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY);
212 for (i = 0; i < p->cpu_capabilities_count; ++i) {
213 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_ID,
215 goto out_cancel_nest;
217 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE,
218 cpu_cap->performance))
219 goto out_cancel_nest;
221 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY,
222 cpu_cap->efficiency))
223 goto out_cancel_nest;
228 nla_nest_end(msg, start_cap);
232 nla_nest_cancel(msg, start_cap);
237 int thermal_genl_event_tz_delete(struct param *p)
238 __attribute__((alias("thermal_genl_event_tz")));
240 int thermal_genl_event_tz_enable(struct param *p)
241 __attribute__((alias("thermal_genl_event_tz")));
243 int thermal_genl_event_tz_disable(struct param *p)
244 __attribute__((alias("thermal_genl_event_tz")));
246 int thermal_genl_event_tz_trip_down(struct param *p)
247 __attribute__((alias("thermal_genl_event_tz_trip_up")));
249 static cb_t event_cb[] = {
250 [THERMAL_GENL_EVENT_TZ_CREATE] = thermal_genl_event_tz_create,
251 [THERMAL_GENL_EVENT_TZ_DELETE] = thermal_genl_event_tz_delete,
252 [THERMAL_GENL_EVENT_TZ_ENABLE] = thermal_genl_event_tz_enable,
253 [THERMAL_GENL_EVENT_TZ_DISABLE] = thermal_genl_event_tz_disable,
254 [THERMAL_GENL_EVENT_TZ_TRIP_UP] = thermal_genl_event_tz_trip_up,
255 [THERMAL_GENL_EVENT_TZ_TRIP_DOWN] = thermal_genl_event_tz_trip_down,
256 [THERMAL_GENL_EVENT_TZ_TRIP_CHANGE] = thermal_genl_event_tz_trip_change,
257 [THERMAL_GENL_EVENT_CDEV_ADD] = thermal_genl_event_cdev_add,
258 [THERMAL_GENL_EVENT_CDEV_DELETE] = thermal_genl_event_cdev_delete,
259 [THERMAL_GENL_EVENT_CDEV_STATE_UPDATE] = thermal_genl_event_cdev_state_update,
260 [THERMAL_GENL_EVENT_TZ_GOV_CHANGE] = thermal_genl_event_gov_change,
261 [THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE] = thermal_genl_event_cpu_capability_change,
265 * Generic netlink event encoding
267 static int thermal_genl_send_event(enum thermal_genl_event event,
274 if (!thermal_group_has_listeners(THERMAL_GENL_EVENT_GROUP))
277 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
282 hdr = genlmsg_put(msg, 0, 0, &thermal_genl_family, 0, event);
286 ret = event_cb[event](p);
290 genlmsg_end(msg, hdr);
292 genlmsg_multicast(&thermal_genl_family, msg, 0, THERMAL_GENL_EVENT_GROUP, GFP_KERNEL);
297 genlmsg_cancel(msg, hdr);
304 int thermal_notify_tz_create(const struct thermal_zone_device *tz)
306 struct param p = { .tz_id = tz->id, .name = tz->type };
308 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
311 int thermal_notify_tz_delete(const struct thermal_zone_device *tz)
313 struct param p = { .tz_id = tz->id };
315 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
318 int thermal_notify_tz_enable(const struct thermal_zone_device *tz)
320 struct param p = { .tz_id = tz->id };
322 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
325 int thermal_notify_tz_disable(const struct thermal_zone_device *tz)
327 struct param p = { .tz_id = tz->id };
329 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
332 int thermal_notify_tz_trip_down(const struct thermal_zone_device *tz,
333 const struct thermal_trip *trip)
335 struct param p = { .tz_id = tz->id,
336 .trip_id = thermal_zone_trip_id(tz, trip),
337 .temp = tz->temperature };
339 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
342 int thermal_notify_tz_trip_up(const struct thermal_zone_device *tz,
343 const struct thermal_trip *trip)
345 struct param p = { .tz_id = tz->id,
346 .trip_id = thermal_zone_trip_id(tz, trip),
347 .temp = tz->temperature };
349 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
352 int thermal_notify_tz_trip_change(const struct thermal_zone_device *tz,
353 const struct thermal_trip *trip)
355 struct param p = { .tz_id = tz->id,
356 .trip_id = thermal_zone_trip_id(tz, trip),
357 .trip_type = trip->type,
358 .trip_temp = trip->temperature,
359 .trip_hyst = trip->hysteresis };
361 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
364 int thermal_notify_cdev_state_update(const struct thermal_cooling_device *cdev,
367 struct param p = { .cdev_id = cdev->id, .cdev_state = state };
369 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_STATE_UPDATE, &p);
372 int thermal_notify_cdev_add(const struct thermal_cooling_device *cdev)
374 struct param p = { .cdev_id = cdev->id, .name = cdev->type,
375 .cdev_max_state = cdev->max_state };
377 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
380 int thermal_notify_cdev_delete(const struct thermal_cooling_device *cdev)
382 struct param p = { .cdev_id = cdev->id };
384 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
387 int thermal_notify_tz_gov_change(const struct thermal_zone_device *tz,
390 struct param p = { .tz_id = tz->id, .name = name };
392 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
395 int thermal_genl_cpu_capability_event(int count,
396 struct thermal_genl_cpu_caps *caps)
398 struct param p = { .cpu_capabilities_count = count, .cpu_capabilities = caps };
400 return thermal_genl_send_event(THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE, &p);
402 EXPORT_SYMBOL_GPL(thermal_genl_cpu_capability_event);
404 /*************************** Command encoding ********************************/
406 static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
409 struct sk_buff *msg = data;
411 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
412 nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
418 static int thermal_genl_cmd_tz_get_id(struct param *p)
420 struct sk_buff *msg = p->msg;
421 struct nlattr *start_tz;
424 start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
428 ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get_id, msg);
430 goto out_cancel_nest;
432 nla_nest_end(msg, start_tz);
437 nla_nest_cancel(msg, start_tz);
442 static int thermal_genl_cmd_tz_get_trip(struct param *p)
444 struct sk_buff *msg = p->msg;
445 const struct thermal_trip_desc *td;
446 struct thermal_zone_device *tz;
447 struct nlattr *start_trip;
450 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
453 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
455 tz = thermal_zone_get_by_id(id);
459 start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
463 mutex_lock(&tz->lock);
465 for_each_trip_desc(tz, td) {
466 const struct thermal_trip *trip = &td->trip;
468 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID,
469 thermal_zone_trip_id(tz, trip)) ||
470 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip->type) ||
471 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, trip->temperature) ||
472 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, trip->hysteresis))
473 goto out_cancel_nest;
476 mutex_unlock(&tz->lock);
478 nla_nest_end(msg, start_trip);
483 mutex_unlock(&tz->lock);
488 static int thermal_genl_cmd_tz_get_temp(struct param *p)
490 struct sk_buff *msg = p->msg;
491 struct thermal_zone_device *tz;
494 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
497 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
499 tz = thermal_zone_get_by_id(id);
503 ret = thermal_zone_get_temp(tz, &temp);
507 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
508 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
514 static int thermal_genl_cmd_tz_get_gov(struct param *p)
516 struct sk_buff *msg = p->msg;
517 struct thermal_zone_device *tz;
520 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
523 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
525 tz = thermal_zone_get_by_id(id);
529 mutex_lock(&tz->lock);
531 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
532 nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
536 mutex_unlock(&tz->lock);
541 static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
544 struct sk_buff *msg = data;
546 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
549 if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
555 static int thermal_genl_cmd_cdev_get(struct param *p)
557 struct sk_buff *msg = p->msg;
558 struct nlattr *start_cdev;
561 start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
565 ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
567 goto out_cancel_nest;
569 nla_nest_end(msg, start_cdev);
573 nla_nest_cancel(msg, start_cdev);
578 static cb_t cmd_cb[] = {
579 [THERMAL_GENL_CMD_TZ_GET_ID] = thermal_genl_cmd_tz_get_id,
580 [THERMAL_GENL_CMD_TZ_GET_TRIP] = thermal_genl_cmd_tz_get_trip,
581 [THERMAL_GENL_CMD_TZ_GET_TEMP] = thermal_genl_cmd_tz_get_temp,
582 [THERMAL_GENL_CMD_TZ_GET_GOV] = thermal_genl_cmd_tz_get_gov,
583 [THERMAL_GENL_CMD_CDEV_GET] = thermal_genl_cmd_cdev_get,
586 static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
587 struct netlink_callback *cb)
589 struct param p = { .msg = skb };
590 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
591 int cmd = info->op.cmd;
595 hdr = genlmsg_put(skb, 0, 0, &thermal_genl_family, 0, cmd);
599 ret = cmd_cb[cmd](&p);
603 genlmsg_end(skb, hdr);
608 genlmsg_cancel(skb, hdr);
613 static int thermal_genl_cmd_doit(struct sk_buff *skb,
614 struct genl_info *info)
616 struct param p = { .attrs = info->attrs };
619 int cmd = info->genlhdr->cmd;
622 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
627 hdr = genlmsg_put_reply(msg, info, &thermal_genl_family, 0, cmd);
631 ret = cmd_cb[cmd](&p);
635 genlmsg_end(msg, hdr);
637 return genlmsg_reply(msg, info);
640 genlmsg_cancel(msg, hdr);
647 static int thermal_genl_bind(int mcgrp)
649 struct thermal_genl_notify n = { .mcgrp = mcgrp };
651 if (WARN_ON_ONCE(mcgrp > THERMAL_GENL_MAX_GROUP))
654 blocking_notifier_call_chain(&thermal_genl_chain, THERMAL_NOTIFY_BIND, &n);
658 static void thermal_genl_unbind(int mcgrp)
660 struct thermal_genl_notify n = { .mcgrp = mcgrp };
662 if (WARN_ON_ONCE(mcgrp > THERMAL_GENL_MAX_GROUP))
665 blocking_notifier_call_chain(&thermal_genl_chain, THERMAL_NOTIFY_UNBIND, &n);
668 static const struct genl_small_ops thermal_genl_ops[] = {
670 .cmd = THERMAL_GENL_CMD_TZ_GET_ID,
671 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
672 .dumpit = thermal_genl_cmd_dumpit,
675 .cmd = THERMAL_GENL_CMD_TZ_GET_TRIP,
676 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
677 .doit = thermal_genl_cmd_doit,
680 .cmd = THERMAL_GENL_CMD_TZ_GET_TEMP,
681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
682 .doit = thermal_genl_cmd_doit,
685 .cmd = THERMAL_GENL_CMD_TZ_GET_GOV,
686 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
687 .doit = thermal_genl_cmd_doit,
690 .cmd = THERMAL_GENL_CMD_CDEV_GET,
691 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
692 .dumpit = thermal_genl_cmd_dumpit,
696 static struct genl_family thermal_genl_family __ro_after_init = {
698 .name = THERMAL_GENL_FAMILY_NAME,
699 .version = THERMAL_GENL_VERSION,
700 .maxattr = THERMAL_GENL_ATTR_MAX,
701 .policy = thermal_genl_policy,
702 .bind = thermal_genl_bind,
703 .unbind = thermal_genl_unbind,
704 .small_ops = thermal_genl_ops,
705 .n_small_ops = ARRAY_SIZE(thermal_genl_ops),
706 .resv_start_op = THERMAL_GENL_CMD_CDEV_GET + 1,
707 .mcgrps = thermal_genl_mcgrps,
708 .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps),
711 int thermal_genl_register_notifier(struct notifier_block *nb)
713 return blocking_notifier_chain_register(&thermal_genl_chain, nb);
716 int thermal_genl_unregister_notifier(struct notifier_block *nb)
718 return blocking_notifier_chain_unregister(&thermal_genl_chain, nb);
721 int __init thermal_netlink_init(void)
723 return genl_register_family(&thermal_genl_family);
726 void __init thermal_netlink_exit(void)
728 genl_unregister_family(&thermal_genl_family);