thermal_netlink.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 Linaro Limited
  4. *
  5. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  6. *
  7. * Generic netlink for thermal management framework
  8. */
  9. #include <linux/module.h>
  10. #include <linux/notifier.h>
  11. #include <linux/kernel.h>
  12. #include <net/sock.h>
  13. #include <net/genetlink.h>
  14. #include <uapi/linux/thermal.h>
  15. #include "thermal_core.h"
  16. static const struct genl_multicast_group thermal_genl_mcgrps[] = {
  17. [THERMAL_GENL_SAMPLING_GROUP] = { .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
  18. [THERMAL_GENL_EVENT_GROUP] = { .name = THERMAL_GENL_EVENT_GROUP_NAME, },
  19. };
  20. static const struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
  21. /* Thermal zone */
  22. [THERMAL_GENL_ATTR_TZ] = { .type = NLA_NESTED },
  23. [THERMAL_GENL_ATTR_TZ_ID] = { .type = NLA_U32 },
  24. [THERMAL_GENL_ATTR_TZ_TEMP] = { .type = NLA_U32 },
  25. [THERMAL_GENL_ATTR_TZ_TRIP] = { .type = NLA_NESTED },
  26. [THERMAL_GENL_ATTR_TZ_TRIP_ID] = { .type = NLA_U32 },
  27. [THERMAL_GENL_ATTR_TZ_TRIP_TEMP] = { .type = NLA_U32 },
  28. [THERMAL_GENL_ATTR_TZ_TRIP_TYPE] = { .type = NLA_U32 },
  29. [THERMAL_GENL_ATTR_TZ_TRIP_HYST] = { .type = NLA_U32 },
  30. [THERMAL_GENL_ATTR_TZ_MODE] = { .type = NLA_U32 },
  31. [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT] = { .type = NLA_U32 },
  32. [THERMAL_GENL_ATTR_TZ_NAME] = { .type = NLA_STRING,
  33. .len = THERMAL_NAME_LENGTH },
  34. /* Governor(s) */
  35. [THERMAL_GENL_ATTR_TZ_GOV] = { .type = NLA_NESTED },
  36. [THERMAL_GENL_ATTR_TZ_GOV_NAME] = { .type = NLA_STRING,
  37. .len = THERMAL_NAME_LENGTH },
  38. /* Cooling devices */
  39. [THERMAL_GENL_ATTR_CDEV] = { .type = NLA_NESTED },
  40. [THERMAL_GENL_ATTR_CDEV_ID] = { .type = NLA_U32 },
  41. [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 },
  42. [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 },
  43. [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING,
  44. .len = THERMAL_NAME_LENGTH },
  45. /* CPU capabilities */
  46. [THERMAL_GENL_ATTR_CPU_CAPABILITY] = { .type = NLA_NESTED },
  47. [THERMAL_GENL_ATTR_CPU_CAPABILITY_ID] = { .type = NLA_U32 },
  48. [THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE] = { .type = NLA_U32 },
  49. [THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY] = { .type = NLA_U32 },
  50. /* Thresholds */
  51. [THERMAL_GENL_ATTR_THRESHOLD] = { .type = NLA_NESTED },
  52. [THERMAL_GENL_ATTR_THRESHOLD_TEMP] = { .type = NLA_U32 },
  53. [THERMAL_GENL_ATTR_THRESHOLD_DIRECTION] = { .type = NLA_U32 },
  54. };
  55. struct param {
  56. struct nlattr **attrs;
  57. struct sk_buff *msg;
  58. const char *name;
  59. int tz_id;
  60. int cdev_id;
  61. int trip_id;
  62. int trip_temp;
  63. int trip_type;
  64. int trip_hyst;
  65. int temp;
  66. int prev_temp;
  67. int direction;
  68. int cdev_state;
  69. int cdev_max_state;
  70. struct thermal_genl_cpu_caps *cpu_capabilities;
  71. int cpu_capabilities_count;
  72. };
  73. typedef int (*cb_t)(struct param *);
  74. static struct genl_family thermal_genl_family;
  75. static BLOCKING_NOTIFIER_HEAD(thermal_genl_chain);
  76. static int thermal_group_has_listeners(enum thermal_genl_multicast_groups group)
  77. {
  78. return genl_has_listeners(&thermal_genl_family, &init_net, group);
  79. }
  80. /************************** Sampling encoding *******************************/
  81. int thermal_genl_sampling_temp(int id, int temp)
  82. {
  83. struct sk_buff *skb;
  84. void *hdr;
  85. if (!thermal_group_has_listeners(THERMAL_GENL_SAMPLING_GROUP))
  86. return 0;
  87. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  88. if (!skb)
  89. return -ENOMEM;
  90. hdr = genlmsg_put(skb, 0, 0, &thermal_genl_family, 0,
  91. THERMAL_GENL_SAMPLING_TEMP);
  92. if (!hdr)
  93. goto out_free;
  94. if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_ID, id))
  95. goto out_cancel;
  96. if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_TEMP, temp))
  97. goto out_cancel;
  98. genlmsg_end(skb, hdr);
  99. genlmsg_multicast(&thermal_genl_family, skb, 0, THERMAL_GENL_SAMPLING_GROUP, GFP_KERNEL);
  100. return 0;
  101. out_cancel:
  102. genlmsg_cancel(skb, hdr);
  103. out_free:
  104. nlmsg_free(skb);
  105. return -EMSGSIZE;
  106. }
  107. /**************************** Event encoding *********************************/
  108. static int thermal_genl_event_tz_create(struct param *p)
  109. {
  110. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  111. nla_put_string(p->msg, THERMAL_GENL_ATTR_TZ_NAME, p->name))
  112. return -EMSGSIZE;
  113. return 0;
  114. }
  115. static int thermal_genl_event_tz(struct param *p)
  116. {
  117. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
  118. return -EMSGSIZE;
  119. return 0;
  120. }
  121. static int thermal_genl_event_tz_trip_up(struct param *p)
  122. {
  123. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  124. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
  125. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TEMP, p->temp))
  126. return -EMSGSIZE;
  127. return 0;
  128. }
  129. static int thermal_genl_event_tz_trip_change(struct param *p)
  130. {
  131. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  132. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
  133. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, p->trip_type) ||
  134. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, p->trip_temp) ||
  135. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, p->trip_hyst))
  136. return -EMSGSIZE;
  137. return 0;
  138. }
  139. static int thermal_genl_event_cdev_add(struct param *p)
  140. {
  141. if (nla_put_string(p->msg, THERMAL_GENL_ATTR_CDEV_NAME,
  142. p->name) ||
  143. nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
  144. p->cdev_id) ||
  145. nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_MAX_STATE,
  146. p->cdev_max_state))
  147. return -EMSGSIZE;
  148. return 0;
  149. }
  150. static int thermal_genl_event_cdev_delete(struct param *p)
  151. {
  152. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID, p->cdev_id))
  153. return -EMSGSIZE;
  154. return 0;
  155. }
  156. static int thermal_genl_event_cdev_state_update(struct param *p)
  157. {
  158. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
  159. p->cdev_id) ||
  160. nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_CUR_STATE,
  161. p->cdev_state))
  162. return -EMSGSIZE;
  163. return 0;
  164. }
  165. static int thermal_genl_event_gov_change(struct param *p)
  166. {
  167. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  168. nla_put_string(p->msg, THERMAL_GENL_ATTR_GOV_NAME, p->name))
  169. return -EMSGSIZE;
  170. return 0;
  171. }
  172. static int thermal_genl_event_cpu_capability_change(struct param *p)
  173. {
  174. struct thermal_genl_cpu_caps *cpu_cap = p->cpu_capabilities;
  175. struct sk_buff *msg = p->msg;
  176. struct nlattr *start_cap;
  177. int i;
  178. start_cap = nla_nest_start(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY);
  179. if (!start_cap)
  180. return -EMSGSIZE;
  181. for (i = 0; i < p->cpu_capabilities_count; ++i) {
  182. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_ID,
  183. cpu_cap->cpu))
  184. goto out_cancel_nest;
  185. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE,
  186. cpu_cap->performance))
  187. goto out_cancel_nest;
  188. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY,
  189. cpu_cap->efficiency))
  190. goto out_cancel_nest;
  191. ++cpu_cap;
  192. }
  193. nla_nest_end(msg, start_cap);
  194. return 0;
  195. out_cancel_nest:
  196. nla_nest_cancel(msg, start_cap);
  197. return -EMSGSIZE;
  198. }
  199. static int thermal_genl_event_threshold_add(struct param *p)
  200. {
  201. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  202. nla_put_u32(p->msg, THERMAL_GENL_ATTR_THRESHOLD_TEMP, p->temp) ||
  203. nla_put_u32(p->msg, THERMAL_GENL_ATTR_THRESHOLD_DIRECTION, p->direction))
  204. return -EMSGSIZE;
  205. return 0;
  206. }
  207. static int thermal_genl_event_threshold_flush(struct param *p)
  208. {
  209. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
  210. return -EMSGSIZE;
  211. return 0;
  212. }
  213. static int thermal_genl_event_threshold_up(struct param *p)
  214. {
  215. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  216. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_PREV_TEMP, p->prev_temp) ||
  217. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TEMP, p->temp))
  218. return -EMSGSIZE;
  219. return 0;
  220. }
  221. int thermal_genl_event_tz_delete(struct param *p)
  222. __attribute__((alias("thermal_genl_event_tz")));
  223. int thermal_genl_event_tz_enable(struct param *p)
  224. __attribute__((alias("thermal_genl_event_tz")));
  225. int thermal_genl_event_tz_disable(struct param *p)
  226. __attribute__((alias("thermal_genl_event_tz")));
  227. int thermal_genl_event_tz_trip_down(struct param *p)
  228. __attribute__((alias("thermal_genl_event_tz_trip_up")));
  229. int thermal_genl_event_threshold_delete(struct param *p)
  230. __attribute__((alias("thermal_genl_event_threshold_add")));
  231. int thermal_genl_event_threshold_down(struct param *p)
  232. __attribute__((alias("thermal_genl_event_threshold_up")));
  233. static cb_t event_cb[] = {
  234. [THERMAL_GENL_EVENT_TZ_CREATE] = thermal_genl_event_tz_create,
  235. [THERMAL_GENL_EVENT_TZ_DELETE] = thermal_genl_event_tz_delete,
  236. [THERMAL_GENL_EVENT_TZ_ENABLE] = thermal_genl_event_tz_enable,
  237. [THERMAL_GENL_EVENT_TZ_DISABLE] = thermal_genl_event_tz_disable,
  238. [THERMAL_GENL_EVENT_TZ_TRIP_UP] = thermal_genl_event_tz_trip_up,
  239. [THERMAL_GENL_EVENT_TZ_TRIP_DOWN] = thermal_genl_event_tz_trip_down,
  240. [THERMAL_GENL_EVENT_TZ_TRIP_CHANGE] = thermal_genl_event_tz_trip_change,
  241. [THERMAL_GENL_EVENT_CDEV_ADD] = thermal_genl_event_cdev_add,
  242. [THERMAL_GENL_EVENT_CDEV_DELETE] = thermal_genl_event_cdev_delete,
  243. [THERMAL_GENL_EVENT_CDEV_STATE_UPDATE] = thermal_genl_event_cdev_state_update,
  244. [THERMAL_GENL_EVENT_TZ_GOV_CHANGE] = thermal_genl_event_gov_change,
  245. [THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE] = thermal_genl_event_cpu_capability_change,
  246. [THERMAL_GENL_EVENT_THRESHOLD_ADD] = thermal_genl_event_threshold_add,
  247. [THERMAL_GENL_EVENT_THRESHOLD_DELETE] = thermal_genl_event_threshold_delete,
  248. [THERMAL_GENL_EVENT_THRESHOLD_FLUSH] = thermal_genl_event_threshold_flush,
  249. [THERMAL_GENL_EVENT_THRESHOLD_DOWN] = thermal_genl_event_threshold_down,
  250. [THERMAL_GENL_EVENT_THRESHOLD_UP] = thermal_genl_event_threshold_up,
  251. };
  252. /*
  253. * Generic netlink event encoding
  254. */
  255. static int thermal_genl_send_event(enum thermal_genl_event event,
  256. struct param *p)
  257. {
  258. struct sk_buff *msg;
  259. int ret = -EMSGSIZE;
  260. void *hdr;
  261. if (!thermal_group_has_listeners(THERMAL_GENL_EVENT_GROUP))
  262. return 0;
  263. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  264. if (!msg)
  265. return -ENOMEM;
  266. p->msg = msg;
  267. hdr = genlmsg_put(msg, 0, 0, &thermal_genl_family, 0, event);
  268. if (!hdr)
  269. goto out_free_msg;
  270. ret = event_cb[event](p);
  271. if (ret)
  272. goto out_cancel_msg;
  273. genlmsg_end(msg, hdr);
  274. genlmsg_multicast(&thermal_genl_family, msg, 0, THERMAL_GENL_EVENT_GROUP, GFP_KERNEL);
  275. return 0;
  276. out_cancel_msg:
  277. genlmsg_cancel(msg, hdr);
  278. out_free_msg:
  279. nlmsg_free(msg);
  280. return ret;
  281. }
  282. int thermal_notify_tz_create(const struct thermal_zone_device *tz)
  283. {
  284. struct param p = { .tz_id = tz->id, .name = tz->type };
  285. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
  286. }
  287. int thermal_notify_tz_delete(const struct thermal_zone_device *tz)
  288. {
  289. struct param p = { .tz_id = tz->id };
  290. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
  291. }
  292. int thermal_notify_tz_enable(const struct thermal_zone_device *tz)
  293. {
  294. struct param p = { .tz_id = tz->id };
  295. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
  296. }
  297. int thermal_notify_tz_disable(const struct thermal_zone_device *tz)
  298. {
  299. struct param p = { .tz_id = tz->id };
  300. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
  301. }
  302. int thermal_notify_tz_trip_down(const struct thermal_zone_device *tz,
  303. const struct thermal_trip *trip)
  304. {
  305. struct param p = { .tz_id = tz->id,
  306. .trip_id = thermal_zone_trip_id(tz, trip),
  307. .temp = tz->temperature };
  308. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
  309. }
  310. int thermal_notify_tz_trip_up(const struct thermal_zone_device *tz,
  311. const struct thermal_trip *trip)
  312. {
  313. struct param p = { .tz_id = tz->id,
  314. .trip_id = thermal_zone_trip_id(tz, trip),
  315. .temp = tz->temperature };
  316. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
  317. }
  318. int thermal_notify_tz_trip_change(const struct thermal_zone_device *tz,
  319. const struct thermal_trip *trip)
  320. {
  321. struct param p = { .tz_id = tz->id,
  322. .trip_id = thermal_zone_trip_id(tz, trip),
  323. .trip_type = trip->type,
  324. .trip_temp = trip->temperature,
  325. .trip_hyst = trip->hysteresis };
  326. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
  327. }
  328. int thermal_notify_cdev_state_update(const struct thermal_cooling_device *cdev,
  329. int state)
  330. {
  331. struct param p = { .cdev_id = cdev->id, .cdev_state = state };
  332. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_STATE_UPDATE, &p);
  333. }
  334. int thermal_notify_cdev_add(const struct thermal_cooling_device *cdev)
  335. {
  336. struct param p = { .cdev_id = cdev->id, .name = cdev->type,
  337. .cdev_max_state = cdev->max_state };
  338. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
  339. }
  340. int thermal_notify_cdev_delete(const struct thermal_cooling_device *cdev)
  341. {
  342. struct param p = { .cdev_id = cdev->id };
  343. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
  344. }
  345. int thermal_notify_tz_gov_change(const struct thermal_zone_device *tz,
  346. const char *name)
  347. {
  348. struct param p = { .tz_id = tz->id, .name = name };
  349. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
  350. }
  351. int thermal_genl_cpu_capability_event(int count,
  352. struct thermal_genl_cpu_caps *caps)
  353. {
  354. struct param p = { .cpu_capabilities_count = count, .cpu_capabilities = caps };
  355. return thermal_genl_send_event(THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE, &p);
  356. }
  357. EXPORT_SYMBOL_GPL(thermal_genl_cpu_capability_event);
  358. int thermal_notify_threshold_add(const struct thermal_zone_device *tz,
  359. int temperature, int direction)
  360. {
  361. struct param p = { .tz_id = tz->id, .temp = temperature, .direction = direction };
  362. return thermal_genl_send_event(THERMAL_GENL_EVENT_THRESHOLD_ADD, &p);
  363. }
  364. int thermal_notify_threshold_delete(const struct thermal_zone_device *tz,
  365. int temperature, int direction)
  366. {
  367. struct param p = { .tz_id = tz->id, .temp = temperature, .direction = direction };
  368. return thermal_genl_send_event(THERMAL_GENL_EVENT_THRESHOLD_DELETE, &p);
  369. }
  370. int thermal_notify_threshold_flush(const struct thermal_zone_device *tz)
  371. {
  372. struct param p = { .tz_id = tz->id };
  373. return thermal_genl_send_event(THERMAL_GENL_EVENT_THRESHOLD_FLUSH, &p);
  374. }
  375. int thermal_notify_threshold_down(const struct thermal_zone_device *tz)
  376. {
  377. struct param p = { .tz_id = tz->id, .temp = tz->temperature, .prev_temp = tz->last_temperature };
  378. return thermal_genl_send_event(THERMAL_GENL_EVENT_THRESHOLD_DOWN, &p);
  379. }
  380. int thermal_notify_threshold_up(const struct thermal_zone_device *tz)
  381. {
  382. struct param p = { .tz_id = tz->id, .temp = tz->temperature, .prev_temp = tz->last_temperature };
  383. return thermal_genl_send_event(THERMAL_GENL_EVENT_THRESHOLD_UP, &p);
  384. }
  385. /*************************** Command encoding ********************************/
  386. static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
  387. void *data)
  388. {
  389. struct sk_buff *msg = data;
  390. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
  391. nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
  392. return -EMSGSIZE;
  393. return 0;
  394. }
  395. static int thermal_genl_cmd_tz_get_id(struct param *p)
  396. {
  397. struct sk_buff *msg = p->msg;
  398. struct nlattr *start_tz;
  399. int ret;
  400. start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
  401. if (!start_tz)
  402. return -EMSGSIZE;
  403. ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get_id, msg);
  404. if (ret)
  405. goto out_cancel_nest;
  406. nla_nest_end(msg, start_tz);
  407. return 0;
  408. out_cancel_nest:
  409. nla_nest_cancel(msg, start_tz);
  410. return ret;
  411. }
  412. static int thermal_genl_cmd_tz_get_trip(struct param *p)
  413. {
  414. struct sk_buff *msg = p->msg;
  415. const struct thermal_trip_desc *td;
  416. struct nlattr *start_trip;
  417. int id;
  418. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  419. return -EINVAL;
  420. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  421. CLASS(thermal_zone_get_by_id, tz)(id);
  422. if (!tz)
  423. return -EINVAL;
  424. start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
  425. if (!start_trip)
  426. return -EMSGSIZE;
  427. guard(thermal_zone)(tz);
  428. for_each_trip_desc(tz, td) {
  429. const struct thermal_trip *trip = &td->trip;
  430. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID,
  431. thermal_zone_trip_id(tz, trip)) ||
  432. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip->type) ||
  433. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, trip->temperature) ||
  434. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, trip->hysteresis))
  435. return -EMSGSIZE;
  436. }
  437. nla_nest_end(msg, start_trip);
  438. return 0;
  439. }
  440. static int thermal_genl_cmd_tz_get_temp(struct param *p)
  441. {
  442. struct sk_buff *msg = p->msg;
  443. int temp, ret, id;
  444. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  445. return -EINVAL;
  446. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  447. CLASS(thermal_zone_get_by_id, tz)(id);
  448. if (!tz)
  449. return -EINVAL;
  450. ret = thermal_zone_get_temp(tz, &temp);
  451. if (ret)
  452. return ret;
  453. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
  454. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
  455. return -EMSGSIZE;
  456. return 0;
  457. }
  458. static int thermal_genl_cmd_tz_get_gov(struct param *p)
  459. {
  460. struct sk_buff *msg = p->msg;
  461. int id;
  462. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  463. return -EINVAL;
  464. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  465. CLASS(thermal_zone_get_by_id, tz)(id);
  466. if (!tz)
  467. return -EINVAL;
  468. guard(thermal_zone)(tz);
  469. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
  470. nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
  471. tz->governor->name))
  472. return -EMSGSIZE;
  473. return 0;
  474. }
  475. static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
  476. void *data)
  477. {
  478. struct sk_buff *msg = data;
  479. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
  480. return -EMSGSIZE;
  481. if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
  482. return -EMSGSIZE;
  483. return 0;
  484. }
  485. static int thermal_genl_cmd_cdev_get(struct param *p)
  486. {
  487. struct sk_buff *msg = p->msg;
  488. struct nlattr *start_cdev;
  489. int ret;
  490. start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
  491. if (!start_cdev)
  492. return -EMSGSIZE;
  493. ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
  494. if (ret)
  495. goto out_cancel_nest;
  496. nla_nest_end(msg, start_cdev);
  497. return 0;
  498. out_cancel_nest:
  499. nla_nest_cancel(msg, start_cdev);
  500. return ret;
  501. }
  502. static int __thermal_genl_cmd_threshold_get(struct user_threshold *threshold, void *arg)
  503. {
  504. struct sk_buff *msg = arg;
  505. if (nla_put_u32(msg, THERMAL_GENL_ATTR_THRESHOLD_TEMP, threshold->temperature) ||
  506. nla_put_u32(msg, THERMAL_GENL_ATTR_THRESHOLD_DIRECTION, threshold->direction))
  507. return -1;
  508. return 0;
  509. }
  510. static int thermal_genl_cmd_threshold_get(struct param *p)
  511. {
  512. struct sk_buff *msg = p->msg;
  513. struct nlattr *start_trip;
  514. int id, ret;
  515. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  516. return -EINVAL;
  517. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  518. CLASS(thermal_zone_get_by_id, tz)(id);
  519. if (!tz)
  520. return -EINVAL;
  521. start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_THRESHOLD);
  522. if (!start_trip)
  523. return -EMSGSIZE;
  524. ret = thermal_thresholds_for_each(tz, __thermal_genl_cmd_threshold_get, msg);
  525. if (ret)
  526. return -EMSGSIZE;
  527. nla_nest_end(msg, start_trip);
  528. return 0;
  529. }
  530. static int thermal_genl_cmd_threshold_add(struct param *p)
  531. {
  532. int id, temp, direction;
  533. if (!capable(CAP_SYS_ADMIN))
  534. return -EPERM;
  535. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID] ||
  536. !p->attrs[THERMAL_GENL_ATTR_THRESHOLD_TEMP] ||
  537. !p->attrs[THERMAL_GENL_ATTR_THRESHOLD_DIRECTION])
  538. return -EINVAL;
  539. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  540. temp = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_THRESHOLD_TEMP]);
  541. direction = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_THRESHOLD_DIRECTION]);
  542. CLASS(thermal_zone_get_by_id, tz)(id);
  543. if (!tz)
  544. return -EINVAL;
  545. guard(thermal_zone)(tz);
  546. return thermal_thresholds_add(tz, temp, direction);
  547. }
  548. static int thermal_genl_cmd_threshold_delete(struct param *p)
  549. {
  550. int id, temp, direction;
  551. if (!capable(CAP_SYS_ADMIN))
  552. return -EPERM;
  553. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID] ||
  554. !p->attrs[THERMAL_GENL_ATTR_THRESHOLD_TEMP] ||
  555. !p->attrs[THERMAL_GENL_ATTR_THRESHOLD_DIRECTION])
  556. return -EINVAL;
  557. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  558. temp = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_THRESHOLD_TEMP]);
  559. direction = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_THRESHOLD_DIRECTION]);
  560. CLASS(thermal_zone_get_by_id, tz)(id);
  561. if (!tz)
  562. return -EINVAL;
  563. guard(thermal_zone)(tz);
  564. return thermal_thresholds_delete(tz, temp, direction);
  565. }
  566. static int thermal_genl_cmd_threshold_flush(struct param *p)
  567. {
  568. int id;
  569. if (!capable(CAP_SYS_ADMIN))
  570. return -EPERM;
  571. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  572. return -EINVAL;
  573. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  574. CLASS(thermal_zone_get_by_id, tz)(id);
  575. if (!tz)
  576. return -EINVAL;
  577. guard(thermal_zone)(tz);
  578. thermal_thresholds_flush(tz);
  579. return 0;
  580. }
  581. static cb_t cmd_cb[] = {
  582. [THERMAL_GENL_CMD_TZ_GET_ID] = thermal_genl_cmd_tz_get_id,
  583. [THERMAL_GENL_CMD_TZ_GET_TRIP] = thermal_genl_cmd_tz_get_trip,
  584. [THERMAL_GENL_CMD_TZ_GET_TEMP] = thermal_genl_cmd_tz_get_temp,
  585. [THERMAL_GENL_CMD_TZ_GET_GOV] = thermal_genl_cmd_tz_get_gov,
  586. [THERMAL_GENL_CMD_CDEV_GET] = thermal_genl_cmd_cdev_get,
  587. [THERMAL_GENL_CMD_THRESHOLD_GET] = thermal_genl_cmd_threshold_get,
  588. [THERMAL_GENL_CMD_THRESHOLD_ADD] = thermal_genl_cmd_threshold_add,
  589. [THERMAL_GENL_CMD_THRESHOLD_DELETE] = thermal_genl_cmd_threshold_delete,
  590. [THERMAL_GENL_CMD_THRESHOLD_FLUSH] = thermal_genl_cmd_threshold_flush,
  591. };
  592. static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
  593. struct netlink_callback *cb)
  594. {
  595. struct param p = { .msg = skb };
  596. const struct genl_dumpit_info *info = genl_dumpit_info(cb);
  597. int cmd = info->op.cmd;
  598. int ret;
  599. void *hdr;
  600. hdr = genlmsg_put(skb, 0, 0, &thermal_genl_family, 0, cmd);
  601. if (!hdr)
  602. return -EMSGSIZE;
  603. ret = cmd_cb[cmd](&p);
  604. if (ret)
  605. goto out_cancel_msg;
  606. genlmsg_end(skb, hdr);
  607. return 0;
  608. out_cancel_msg:
  609. genlmsg_cancel(skb, hdr);
  610. return ret;
  611. }
  612. static int thermal_genl_cmd_doit(struct sk_buff *skb,
  613. struct genl_info *info)
  614. {
  615. struct param p = { .attrs = info->attrs };
  616. struct sk_buff *msg;
  617. void *hdr;
  618. int cmd = info->genlhdr->cmd;
  619. int ret = -EMSGSIZE;
  620. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  621. if (!msg)
  622. return -ENOMEM;
  623. p.msg = msg;
  624. hdr = genlmsg_put_reply(msg, info, &thermal_genl_family, 0, cmd);
  625. if (!hdr)
  626. goto out_free_msg;
  627. ret = cmd_cb[cmd](&p);
  628. if (ret)
  629. goto out_cancel_msg;
  630. genlmsg_end(msg, hdr);
  631. return genlmsg_reply(msg, info);
  632. out_cancel_msg:
  633. genlmsg_cancel(msg, hdr);
  634. out_free_msg:
  635. nlmsg_free(msg);
  636. return ret;
  637. }
  638. static int thermal_genl_bind(int mcgrp)
  639. {
  640. struct thermal_genl_notify n = { .mcgrp = mcgrp };
  641. if (WARN_ON_ONCE(mcgrp > THERMAL_GENL_MAX_GROUP))
  642. return -EINVAL;
  643. blocking_notifier_call_chain(&thermal_genl_chain, THERMAL_NOTIFY_BIND, &n);
  644. return 0;
  645. }
  646. static void thermal_genl_unbind(int mcgrp)
  647. {
  648. struct thermal_genl_notify n = { .mcgrp = mcgrp };
  649. if (WARN_ON_ONCE(mcgrp > THERMAL_GENL_MAX_GROUP))
  650. return;
  651. blocking_notifier_call_chain(&thermal_genl_chain, THERMAL_NOTIFY_UNBIND, &n);
  652. }
  653. static const struct genl_small_ops thermal_genl_ops[] = {
  654. {
  655. .cmd = THERMAL_GENL_CMD_TZ_GET_ID,
  656. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  657. .dumpit = thermal_genl_cmd_dumpit,
  658. },
  659. {
  660. .cmd = THERMAL_GENL_CMD_TZ_GET_TRIP,
  661. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  662. .doit = thermal_genl_cmd_doit,
  663. },
  664. {
  665. .cmd = THERMAL_GENL_CMD_TZ_GET_TEMP,
  666. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  667. .doit = thermal_genl_cmd_doit,
  668. },
  669. {
  670. .cmd = THERMAL_GENL_CMD_TZ_GET_GOV,
  671. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  672. .doit = thermal_genl_cmd_doit,
  673. },
  674. {
  675. .cmd = THERMAL_GENL_CMD_CDEV_GET,
  676. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  677. .dumpit = thermal_genl_cmd_dumpit,
  678. },
  679. {
  680. .cmd = THERMAL_GENL_CMD_THRESHOLD_GET,
  681. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  682. .doit = thermal_genl_cmd_doit,
  683. },
  684. {
  685. .cmd = THERMAL_GENL_CMD_THRESHOLD_ADD,
  686. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  687. .doit = thermal_genl_cmd_doit,
  688. },
  689. {
  690. .cmd = THERMAL_GENL_CMD_THRESHOLD_DELETE,
  691. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  692. .doit = thermal_genl_cmd_doit,
  693. },
  694. {
  695. .cmd = THERMAL_GENL_CMD_THRESHOLD_FLUSH,
  696. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  697. .doit = thermal_genl_cmd_doit,
  698. },
  699. };
  700. static struct genl_family thermal_genl_family __ro_after_init = {
  701. .hdrsize = 0,
  702. .name = THERMAL_GENL_FAMILY_NAME,
  703. .version = THERMAL_GENL_VERSION,
  704. .maxattr = THERMAL_GENL_ATTR_MAX,
  705. .policy = thermal_genl_policy,
  706. .bind = thermal_genl_bind,
  707. .unbind = thermal_genl_unbind,
  708. .small_ops = thermal_genl_ops,
  709. .n_small_ops = ARRAY_SIZE(thermal_genl_ops),
  710. .resv_start_op = __THERMAL_GENL_CMD_MAX,
  711. .mcgrps = thermal_genl_mcgrps,
  712. .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps),
  713. };
  714. int thermal_genl_register_notifier(struct notifier_block *nb)
  715. {
  716. return blocking_notifier_chain_register(&thermal_genl_chain, nb);
  717. }
  718. int thermal_genl_unregister_notifier(struct notifier_block *nb)
  719. {
  720. return blocking_notifier_chain_unregister(&thermal_genl_chain, nb);
  721. }
  722. int __init thermal_netlink_init(void)
  723. {
  724. return genl_register_family(&thermal_genl_family);
  725. }
  726. void __init thermal_netlink_exit(void)
  727. {
  728. genl_unregister_family(&thermal_genl_family);
  729. }