qcom_sysmon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017, Linaro Ltd.
  4. */
  5. #include <linux/firmware.h>
  6. #include <linux/module.h>
  7. #include <linux/notifier.h>
  8. #include <linux/slab.h>
  9. #include <linux/io.h>
  10. #include <linux/notifier.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/remoteproc/qcom_rproc.h>
  14. #include <linux/rpmsg.h>
  15. #include "qcom_common.h"
  16. static BLOCKING_NOTIFIER_HEAD(sysmon_notifiers);
  17. struct qcom_sysmon {
  18. struct rproc_subdev subdev;
  19. struct rproc *rproc;
  20. struct list_head node;
  21. const char *name;
  22. int ssctl_version;
  23. int ssctl_instance;
  24. struct notifier_block nb;
  25. struct device *dev;
  26. struct rpmsg_endpoint *ept;
  27. struct completion comp;
  28. struct mutex lock;
  29. bool ssr_ack;
  30. struct qmi_handle qmi;
  31. struct sockaddr_qrtr ssctl;
  32. };
  33. static DEFINE_MUTEX(sysmon_lock);
  34. static LIST_HEAD(sysmon_list);
  35. /**
  36. * sysmon_send_event() - send notification of other remote's SSR event
  37. * @sysmon: sysmon context
  38. * @name: other remote's name
  39. */
  40. static void sysmon_send_event(struct qcom_sysmon *sysmon, const char *name)
  41. {
  42. char req[50];
  43. int len;
  44. int ret;
  45. len = snprintf(req, sizeof(req), "ssr:%s:before_shutdown", name);
  46. if (len >= sizeof(req))
  47. return;
  48. mutex_lock(&sysmon->lock);
  49. reinit_completion(&sysmon->comp);
  50. sysmon->ssr_ack = false;
  51. ret = rpmsg_send(sysmon->ept, req, len);
  52. if (ret < 0) {
  53. dev_err(sysmon->dev, "failed to send sysmon event\n");
  54. goto out_unlock;
  55. }
  56. ret = wait_for_completion_timeout(&sysmon->comp,
  57. msecs_to_jiffies(5000));
  58. if (!ret) {
  59. dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
  60. goto out_unlock;
  61. }
  62. if (!sysmon->ssr_ack)
  63. dev_err(sysmon->dev, "unexpected response to sysmon event\n");
  64. out_unlock:
  65. mutex_unlock(&sysmon->lock);
  66. }
  67. /**
  68. * sysmon_request_shutdown() - request graceful shutdown of remote
  69. * @sysmon: sysmon context
  70. */
  71. static void sysmon_request_shutdown(struct qcom_sysmon *sysmon)
  72. {
  73. char *req = "ssr:shutdown";
  74. int ret;
  75. mutex_lock(&sysmon->lock);
  76. reinit_completion(&sysmon->comp);
  77. sysmon->ssr_ack = false;
  78. ret = rpmsg_send(sysmon->ept, req, strlen(req) + 1);
  79. if (ret < 0) {
  80. dev_err(sysmon->dev, "send sysmon shutdown request failed\n");
  81. goto out_unlock;
  82. }
  83. ret = wait_for_completion_timeout(&sysmon->comp,
  84. msecs_to_jiffies(5000));
  85. if (!ret) {
  86. dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
  87. goto out_unlock;
  88. }
  89. if (!sysmon->ssr_ack)
  90. dev_err(sysmon->dev,
  91. "unexpected response to sysmon shutdown request\n");
  92. out_unlock:
  93. mutex_unlock(&sysmon->lock);
  94. }
  95. static int sysmon_callback(struct rpmsg_device *rpdev, void *data, int count,
  96. void *priv, u32 addr)
  97. {
  98. struct qcom_sysmon *sysmon = priv;
  99. const char *ssr_ack = "ssr:ack";
  100. const int ssr_ack_len = strlen(ssr_ack) + 1;
  101. if (!sysmon)
  102. return -EINVAL;
  103. if (count >= ssr_ack_len && !memcmp(data, ssr_ack, ssr_ack_len))
  104. sysmon->ssr_ack = true;
  105. complete(&sysmon->comp);
  106. return 0;
  107. }
  108. #define SSCTL_SHUTDOWN_REQ 0x21
  109. #define SSCTL_SUBSYS_EVENT_REQ 0x23
  110. #define SSCTL_MAX_MSG_LEN 7
  111. #define SSCTL_SUBSYS_NAME_LENGTH 15
  112. enum {
  113. SSCTL_SSR_EVENT_BEFORE_POWERUP,
  114. SSCTL_SSR_EVENT_AFTER_POWERUP,
  115. SSCTL_SSR_EVENT_BEFORE_SHUTDOWN,
  116. SSCTL_SSR_EVENT_AFTER_SHUTDOWN,
  117. };
  118. enum {
  119. SSCTL_SSR_EVENT_FORCED,
  120. SSCTL_SSR_EVENT_GRACEFUL,
  121. };
  122. struct ssctl_shutdown_resp {
  123. struct qmi_response_type_v01 resp;
  124. };
  125. static struct qmi_elem_info ssctl_shutdown_resp_ei[] = {
  126. {
  127. .data_type = QMI_STRUCT,
  128. .elem_len = 1,
  129. .elem_size = sizeof(struct qmi_response_type_v01),
  130. .array_type = NO_ARRAY,
  131. .tlv_type = 0x02,
  132. .offset = offsetof(struct ssctl_shutdown_resp, resp),
  133. .ei_array = qmi_response_type_v01_ei,
  134. },
  135. {}
  136. };
  137. struct ssctl_subsys_event_req {
  138. u8 subsys_name_len;
  139. char subsys_name[SSCTL_SUBSYS_NAME_LENGTH];
  140. u32 event;
  141. u8 evt_driven_valid;
  142. u32 evt_driven;
  143. };
  144. static struct qmi_elem_info ssctl_subsys_event_req_ei[] = {
  145. {
  146. .data_type = QMI_DATA_LEN,
  147. .elem_len = 1,
  148. .elem_size = sizeof(uint8_t),
  149. .array_type = NO_ARRAY,
  150. .tlv_type = 0x01,
  151. .offset = offsetof(struct ssctl_subsys_event_req,
  152. subsys_name_len),
  153. .ei_array = NULL,
  154. },
  155. {
  156. .data_type = QMI_UNSIGNED_1_BYTE,
  157. .elem_len = SSCTL_SUBSYS_NAME_LENGTH,
  158. .elem_size = sizeof(char),
  159. .array_type = VAR_LEN_ARRAY,
  160. .tlv_type = 0x01,
  161. .offset = offsetof(struct ssctl_subsys_event_req,
  162. subsys_name),
  163. .ei_array = NULL,
  164. },
  165. {
  166. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  167. .elem_len = 1,
  168. .elem_size = sizeof(uint32_t),
  169. .array_type = NO_ARRAY,
  170. .tlv_type = 0x02,
  171. .offset = offsetof(struct ssctl_subsys_event_req,
  172. event),
  173. .ei_array = NULL,
  174. },
  175. {
  176. .data_type = QMI_OPT_FLAG,
  177. .elem_len = 1,
  178. .elem_size = sizeof(uint8_t),
  179. .array_type = NO_ARRAY,
  180. .tlv_type = 0x10,
  181. .offset = offsetof(struct ssctl_subsys_event_req,
  182. evt_driven_valid),
  183. .ei_array = NULL,
  184. },
  185. {
  186. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  187. .elem_len = 1,
  188. .elem_size = sizeof(uint32_t),
  189. .array_type = NO_ARRAY,
  190. .tlv_type = 0x10,
  191. .offset = offsetof(struct ssctl_subsys_event_req,
  192. evt_driven),
  193. .ei_array = NULL,
  194. },
  195. {}
  196. };
  197. struct ssctl_subsys_event_resp {
  198. struct qmi_response_type_v01 resp;
  199. };
  200. static struct qmi_elem_info ssctl_subsys_event_resp_ei[] = {
  201. {
  202. .data_type = QMI_STRUCT,
  203. .elem_len = 1,
  204. .elem_size = sizeof(struct qmi_response_type_v01),
  205. .array_type = NO_ARRAY,
  206. .tlv_type = 0x02,
  207. .offset = offsetof(struct ssctl_subsys_event_resp,
  208. resp),
  209. .ei_array = qmi_response_type_v01_ei,
  210. },
  211. {}
  212. };
  213. /**
  214. * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
  215. * @sysmon: sysmon context
  216. */
  217. static void ssctl_request_shutdown(struct qcom_sysmon *sysmon)
  218. {
  219. struct ssctl_shutdown_resp resp;
  220. struct qmi_txn txn;
  221. int ret;
  222. ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp);
  223. if (ret < 0) {
  224. dev_err(sysmon->dev, "failed to allocate QMI txn\n");
  225. return;
  226. }
  227. ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
  228. SSCTL_SHUTDOWN_REQ, 0, NULL, NULL);
  229. if (ret < 0) {
  230. dev_err(sysmon->dev, "failed to send shutdown request\n");
  231. qmi_txn_cancel(&txn);
  232. return;
  233. }
  234. ret = qmi_txn_wait(&txn, 5 * HZ);
  235. if (ret < 0)
  236. dev_err(sysmon->dev, "failed receiving QMI response\n");
  237. else if (resp.resp.result)
  238. dev_err(sysmon->dev, "shutdown request failed\n");
  239. else
  240. dev_dbg(sysmon->dev, "shutdown request completed\n");
  241. }
  242. /**
  243. * ssctl_send_event() - send notification of other remote's SSR event
  244. * @sysmon: sysmon context
  245. * @name: other remote's name
  246. */
  247. static void ssctl_send_event(struct qcom_sysmon *sysmon, const char *name)
  248. {
  249. struct ssctl_subsys_event_resp resp;
  250. struct ssctl_subsys_event_req req;
  251. struct qmi_txn txn;
  252. int ret;
  253. memset(&resp, 0, sizeof(resp));
  254. ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_subsys_event_resp_ei, &resp);
  255. if (ret < 0) {
  256. dev_err(sysmon->dev, "failed to allocate QMI txn\n");
  257. return;
  258. }
  259. memset(&req, 0, sizeof(req));
  260. strlcpy(req.subsys_name, name, sizeof(req.subsys_name));
  261. req.subsys_name_len = strlen(req.subsys_name);
  262. req.event = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN;
  263. req.evt_driven_valid = true;
  264. req.evt_driven = SSCTL_SSR_EVENT_FORCED;
  265. ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
  266. SSCTL_SUBSYS_EVENT_REQ, 40,
  267. ssctl_subsys_event_req_ei, &req);
  268. if (ret < 0) {
  269. dev_err(sysmon->dev, "failed to send shutdown request\n");
  270. qmi_txn_cancel(&txn);
  271. return;
  272. }
  273. ret = qmi_txn_wait(&txn, 5 * HZ);
  274. if (ret < 0)
  275. dev_err(sysmon->dev, "failed receiving QMI response\n");
  276. else if (resp.resp.result)
  277. dev_err(sysmon->dev, "ssr event send failed\n");
  278. else
  279. dev_dbg(sysmon->dev, "ssr event send completed\n");
  280. }
  281. /**
  282. * ssctl_new_server() - QMI callback indicating a new service
  283. * @qmi: QMI handle
  284. * @svc: service information
  285. *
  286. * Return: 0 if we're interested in this service, -EINVAL otherwise.
  287. */
  288. static int ssctl_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
  289. {
  290. struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
  291. switch (svc->version) {
  292. case 1:
  293. if (svc->instance != 0)
  294. return -EINVAL;
  295. if (strcmp(sysmon->name, "modem"))
  296. return -EINVAL;
  297. break;
  298. case 2:
  299. if (svc->instance != sysmon->ssctl_instance)
  300. return -EINVAL;
  301. break;
  302. default:
  303. return -EINVAL;
  304. };
  305. sysmon->ssctl_version = svc->version;
  306. sysmon->ssctl.sq_family = AF_QIPCRTR;
  307. sysmon->ssctl.sq_node = svc->node;
  308. sysmon->ssctl.sq_port = svc->port;
  309. svc->priv = sysmon;
  310. return 0;
  311. }
  312. /**
  313. * ssctl_del_server() - QMI callback indicating that @svc is removed
  314. * @qmi: QMI handle
  315. * @svc: service information
  316. */
  317. static void ssctl_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
  318. {
  319. struct qcom_sysmon *sysmon = svc->priv;
  320. sysmon->ssctl_version = 0;
  321. }
  322. static const struct qmi_ops ssctl_ops = {
  323. .new_server = ssctl_new_server,
  324. .del_server = ssctl_del_server,
  325. };
  326. static int sysmon_start(struct rproc_subdev *subdev)
  327. {
  328. return 0;
  329. }
  330. static void sysmon_stop(struct rproc_subdev *subdev, bool crashed)
  331. {
  332. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, subdev);
  333. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)sysmon->name);
  334. /* Don't request graceful shutdown if we've crashed */
  335. if (crashed)
  336. return;
  337. if (sysmon->ssctl_version)
  338. ssctl_request_shutdown(sysmon);
  339. else if (sysmon->ept)
  340. sysmon_request_shutdown(sysmon);
  341. }
  342. /**
  343. * sysmon_notify() - notify sysmon target of another's SSR
  344. * @nb: notifier_block associated with sysmon instance
  345. * @event: unused
  346. * @data: SSR identifier of the remote that is going down
  347. */
  348. static int sysmon_notify(struct notifier_block *nb, unsigned long event,
  349. void *data)
  350. {
  351. struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb);
  352. struct rproc *rproc = sysmon->rproc;
  353. const char *ssr_name = data;
  354. /* Skip non-running rprocs and the originating instance */
  355. if (rproc->state != RPROC_RUNNING || !strcmp(data, sysmon->name)) {
  356. dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name);
  357. return NOTIFY_DONE;
  358. }
  359. /* Only SSCTL version 2 supports SSR events */
  360. if (sysmon->ssctl_version == 2)
  361. ssctl_send_event(sysmon, ssr_name);
  362. else if (sysmon->ept)
  363. sysmon_send_event(sysmon, ssr_name);
  364. return NOTIFY_DONE;
  365. }
  366. /**
  367. * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
  368. * @rproc: rproc context to associate the subdev with
  369. * @name: name of this subdev, to use in SSR
  370. * @ssctl_instance: instance id of the ssctl QMI service
  371. *
  372. * Return: A new qcom_sysmon object, or NULL on failure
  373. */
  374. struct qcom_sysmon *qcom_add_sysmon_subdev(struct rproc *rproc,
  375. const char *name,
  376. int ssctl_instance)
  377. {
  378. struct qcom_sysmon *sysmon;
  379. int ret;
  380. sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL);
  381. if (!sysmon)
  382. return NULL;
  383. sysmon->dev = rproc->dev.parent;
  384. sysmon->rproc = rproc;
  385. sysmon->name = name;
  386. sysmon->ssctl_instance = ssctl_instance;
  387. init_completion(&sysmon->comp);
  388. mutex_init(&sysmon->lock);
  389. ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops, NULL);
  390. if (ret < 0) {
  391. dev_err(sysmon->dev, "failed to initialize qmi handle\n");
  392. kfree(sysmon);
  393. return NULL;
  394. }
  395. qmi_add_lookup(&sysmon->qmi, 43, 0, 0);
  396. sysmon->subdev.start = sysmon_start;
  397. sysmon->subdev.stop = sysmon_stop;
  398. rproc_add_subdev(rproc, &sysmon->subdev);
  399. sysmon->nb.notifier_call = sysmon_notify;
  400. blocking_notifier_chain_register(&sysmon_notifiers, &sysmon->nb);
  401. mutex_lock(&sysmon_lock);
  402. list_add(&sysmon->node, &sysmon_list);
  403. mutex_unlock(&sysmon_lock);
  404. return sysmon;
  405. }
  406. EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev);
  407. /**
  408. * qcom_remove_sysmon_subdev() - release a qcom_sysmon
  409. * @sysmon: sysmon context, as retrieved by qcom_add_sysmon_subdev()
  410. */
  411. void qcom_remove_sysmon_subdev(struct qcom_sysmon *sysmon)
  412. {
  413. if (!sysmon)
  414. return;
  415. mutex_lock(&sysmon_lock);
  416. list_del(&sysmon->node);
  417. mutex_unlock(&sysmon_lock);
  418. blocking_notifier_chain_unregister(&sysmon_notifiers, &sysmon->nb);
  419. rproc_remove_subdev(sysmon->rproc, &sysmon->subdev);
  420. qmi_handle_release(&sysmon->qmi);
  421. kfree(sysmon);
  422. }
  423. EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev);
  424. /**
  425. * sysmon_probe() - probe sys_mon channel
  426. * @rpdev: rpmsg device handle
  427. *
  428. * Find the sysmon context associated with the ancestor remoteproc and assign
  429. * this rpmsg device with said sysmon context.
  430. *
  431. * Return: 0 on success, negative errno on failure.
  432. */
  433. static int sysmon_probe(struct rpmsg_device *rpdev)
  434. {
  435. struct qcom_sysmon *sysmon;
  436. struct rproc *rproc;
  437. rproc = rproc_get_by_child(&rpdev->dev);
  438. if (!rproc) {
  439. dev_err(&rpdev->dev, "sysmon device not child of rproc\n");
  440. return -EINVAL;
  441. }
  442. mutex_lock(&sysmon_lock);
  443. list_for_each_entry(sysmon, &sysmon_list, node) {
  444. if (sysmon->rproc == rproc)
  445. goto found;
  446. }
  447. mutex_unlock(&sysmon_lock);
  448. dev_err(&rpdev->dev, "no sysmon associated with parent rproc\n");
  449. return -EINVAL;
  450. found:
  451. mutex_unlock(&sysmon_lock);
  452. rpdev->ept->priv = sysmon;
  453. sysmon->ept = rpdev->ept;
  454. return 0;
  455. }
  456. /**
  457. * sysmon_remove() - sys_mon channel remove handler
  458. * @rpdev: rpmsg device handle
  459. *
  460. * Disassociate the rpmsg device with the sysmon instance.
  461. */
  462. static void sysmon_remove(struct rpmsg_device *rpdev)
  463. {
  464. struct qcom_sysmon *sysmon = rpdev->ept->priv;
  465. sysmon->ept = NULL;
  466. }
  467. static const struct rpmsg_device_id sysmon_match[] = {
  468. { "sys_mon" },
  469. {}
  470. };
  471. static struct rpmsg_driver sysmon_driver = {
  472. .probe = sysmon_probe,
  473. .remove = sysmon_remove,
  474. .callback = sysmon_callback,
  475. .id_table = sysmon_match,
  476. .drv = {
  477. .name = "qcom_sysmon",
  478. },
  479. };
  480. module_rpmsg_driver(sysmon_driver);
  481. MODULE_DESCRIPTION("Qualcomm sysmon driver");
  482. MODULE_LICENSE("GPL v2");