qcom_sysmon.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/remoteproc/qcom_rproc.h>
  15. #include <linux/rpmsg.h>
  16. #include "qcom_common.h"
  17. static BLOCKING_NOTIFIER_HEAD(sysmon_notifiers);
  18. struct qcom_sysmon {
  19. struct rproc_subdev subdev;
  20. struct rproc *rproc;
  21. int state;
  22. struct mutex state_lock;
  23. struct list_head node;
  24. const char *name;
  25. int shutdown_irq;
  26. int ssctl_version;
  27. int ssctl_instance;
  28. struct notifier_block nb;
  29. struct device *dev;
  30. struct rpmsg_endpoint *ept;
  31. struct completion comp;
  32. struct completion ind_comp;
  33. struct completion shutdown_comp;
  34. struct mutex lock;
  35. bool ssr_ack;
  36. struct qmi_handle qmi;
  37. struct sockaddr_qrtr ssctl;
  38. };
  39. enum {
  40. SSCTL_SSR_EVENT_BEFORE_POWERUP,
  41. SSCTL_SSR_EVENT_AFTER_POWERUP,
  42. SSCTL_SSR_EVENT_BEFORE_SHUTDOWN,
  43. SSCTL_SSR_EVENT_AFTER_SHUTDOWN,
  44. };
  45. static const char * const sysmon_state_string[] = {
  46. [SSCTL_SSR_EVENT_BEFORE_POWERUP] = "before_powerup",
  47. [SSCTL_SSR_EVENT_AFTER_POWERUP] = "after_powerup",
  48. [SSCTL_SSR_EVENT_BEFORE_SHUTDOWN] = "before_shutdown",
  49. [SSCTL_SSR_EVENT_AFTER_SHUTDOWN] = "after_shutdown",
  50. };
  51. struct sysmon_event {
  52. const char *subsys_name;
  53. u32 ssr_event;
  54. };
  55. static DEFINE_MUTEX(sysmon_lock);
  56. static LIST_HEAD(sysmon_list);
  57. /**
  58. * sysmon_send_event() - send notification of other remote's SSR event
  59. * @sysmon: sysmon context
  60. * @event: sysmon event context
  61. */
  62. static void sysmon_send_event(struct qcom_sysmon *sysmon,
  63. const struct sysmon_event *event)
  64. {
  65. char req[50];
  66. int len;
  67. int ret;
  68. len = snprintf(req, sizeof(req), "ssr:%s:%s", event->subsys_name,
  69. sysmon_state_string[event->ssr_event]);
  70. if (len >= sizeof(req))
  71. return;
  72. mutex_lock(&sysmon->lock);
  73. reinit_completion(&sysmon->comp);
  74. sysmon->ssr_ack = false;
  75. ret = rpmsg_send(sysmon->ept, req, len);
  76. if (ret < 0) {
  77. dev_err(sysmon->dev, "failed to send sysmon event\n");
  78. goto out_unlock;
  79. }
  80. ret = wait_for_completion_timeout(&sysmon->comp,
  81. msecs_to_jiffies(5000));
  82. if (!ret) {
  83. dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
  84. goto out_unlock;
  85. }
  86. if (!sysmon->ssr_ack)
  87. dev_err(sysmon->dev, "unexpected response to sysmon event\n");
  88. out_unlock:
  89. mutex_unlock(&sysmon->lock);
  90. }
  91. /**
  92. * sysmon_request_shutdown() - request graceful shutdown of remote
  93. * @sysmon: sysmon context
  94. */
  95. static void sysmon_request_shutdown(struct qcom_sysmon *sysmon)
  96. {
  97. char *req = "ssr:shutdown";
  98. int ret;
  99. mutex_lock(&sysmon->lock);
  100. reinit_completion(&sysmon->comp);
  101. sysmon->ssr_ack = false;
  102. ret = rpmsg_send(sysmon->ept, req, strlen(req) + 1);
  103. if (ret < 0) {
  104. dev_err(sysmon->dev, "send sysmon shutdown request failed\n");
  105. goto out_unlock;
  106. }
  107. ret = wait_for_completion_timeout(&sysmon->comp,
  108. msecs_to_jiffies(5000));
  109. if (!ret) {
  110. dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
  111. goto out_unlock;
  112. }
  113. if (!sysmon->ssr_ack)
  114. dev_err(sysmon->dev,
  115. "unexpected response to sysmon shutdown request\n");
  116. out_unlock:
  117. mutex_unlock(&sysmon->lock);
  118. }
  119. static int sysmon_callback(struct rpmsg_device *rpdev, void *data, int count,
  120. void *priv, u32 addr)
  121. {
  122. struct qcom_sysmon *sysmon = priv;
  123. const char *ssr_ack = "ssr:ack";
  124. const int ssr_ack_len = strlen(ssr_ack) + 1;
  125. if (!sysmon)
  126. return -EINVAL;
  127. if (count >= ssr_ack_len && !memcmp(data, ssr_ack, ssr_ack_len))
  128. sysmon->ssr_ack = true;
  129. complete(&sysmon->comp);
  130. return 0;
  131. }
  132. #define SSCTL_SHUTDOWN_REQ 0x21
  133. #define SSCTL_SHUTDOWN_READY_IND 0x21
  134. #define SSCTL_SUBSYS_EVENT_REQ 0x23
  135. #define SSCTL_MAX_MSG_LEN 7
  136. #define SSCTL_SUBSYS_NAME_LENGTH 15
  137. enum {
  138. SSCTL_SSR_EVENT_FORCED,
  139. SSCTL_SSR_EVENT_GRACEFUL,
  140. };
  141. struct ssctl_shutdown_resp {
  142. struct qmi_response_type_v01 resp;
  143. };
  144. static struct qmi_elem_info ssctl_shutdown_resp_ei[] = {
  145. {
  146. .data_type = QMI_STRUCT,
  147. .elem_len = 1,
  148. .elem_size = sizeof(struct qmi_response_type_v01),
  149. .array_type = NO_ARRAY,
  150. .tlv_type = 0x02,
  151. .offset = offsetof(struct ssctl_shutdown_resp, resp),
  152. .ei_array = qmi_response_type_v01_ei,
  153. },
  154. {}
  155. };
  156. struct ssctl_subsys_event_req {
  157. u8 subsys_name_len;
  158. char subsys_name[SSCTL_SUBSYS_NAME_LENGTH];
  159. u32 event;
  160. u8 evt_driven_valid;
  161. u32 evt_driven;
  162. };
  163. static struct qmi_elem_info ssctl_subsys_event_req_ei[] = {
  164. {
  165. .data_type = QMI_DATA_LEN,
  166. .elem_len = 1,
  167. .elem_size = sizeof(uint8_t),
  168. .array_type = NO_ARRAY,
  169. .tlv_type = 0x01,
  170. .offset = offsetof(struct ssctl_subsys_event_req,
  171. subsys_name_len),
  172. .ei_array = NULL,
  173. },
  174. {
  175. .data_type = QMI_UNSIGNED_1_BYTE,
  176. .elem_len = SSCTL_SUBSYS_NAME_LENGTH,
  177. .elem_size = sizeof(char),
  178. .array_type = VAR_LEN_ARRAY,
  179. .tlv_type = 0x01,
  180. .offset = offsetof(struct ssctl_subsys_event_req,
  181. subsys_name),
  182. .ei_array = NULL,
  183. },
  184. {
  185. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  186. .elem_len = 1,
  187. .elem_size = sizeof(uint32_t),
  188. .array_type = NO_ARRAY,
  189. .tlv_type = 0x02,
  190. .offset = offsetof(struct ssctl_subsys_event_req,
  191. event),
  192. .ei_array = NULL,
  193. },
  194. {
  195. .data_type = QMI_OPT_FLAG,
  196. .elem_len = 1,
  197. .elem_size = sizeof(uint8_t),
  198. .array_type = NO_ARRAY,
  199. .tlv_type = 0x10,
  200. .offset = offsetof(struct ssctl_subsys_event_req,
  201. evt_driven_valid),
  202. .ei_array = NULL,
  203. },
  204. {
  205. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  206. .elem_len = 1,
  207. .elem_size = sizeof(uint32_t),
  208. .array_type = NO_ARRAY,
  209. .tlv_type = 0x10,
  210. .offset = offsetof(struct ssctl_subsys_event_req,
  211. evt_driven),
  212. .ei_array = NULL,
  213. },
  214. {}
  215. };
  216. struct ssctl_subsys_event_resp {
  217. struct qmi_response_type_v01 resp;
  218. };
  219. static struct qmi_elem_info ssctl_subsys_event_resp_ei[] = {
  220. {
  221. .data_type = QMI_STRUCT,
  222. .elem_len = 1,
  223. .elem_size = sizeof(struct qmi_response_type_v01),
  224. .array_type = NO_ARRAY,
  225. .tlv_type = 0x02,
  226. .offset = offsetof(struct ssctl_subsys_event_resp,
  227. resp),
  228. .ei_array = qmi_response_type_v01_ei,
  229. },
  230. {}
  231. };
  232. static struct qmi_elem_info ssctl_shutdown_ind_ei[] = {
  233. {}
  234. };
  235. static void sysmon_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
  236. struct qmi_txn *txn, const void *data)
  237. {
  238. struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
  239. complete(&sysmon->ind_comp);
  240. }
  241. static struct qmi_msg_handler qmi_indication_handler[] = {
  242. {
  243. .type = QMI_INDICATION,
  244. .msg_id = SSCTL_SHUTDOWN_READY_IND,
  245. .ei = ssctl_shutdown_ind_ei,
  246. .decoded_size = 0,
  247. .fn = sysmon_ind_cb
  248. },
  249. {}
  250. };
  251. /**
  252. * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
  253. * @sysmon: sysmon context
  254. */
  255. static void ssctl_request_shutdown(struct qcom_sysmon *sysmon)
  256. {
  257. struct ssctl_shutdown_resp resp;
  258. struct qmi_txn txn;
  259. int ret;
  260. reinit_completion(&sysmon->ind_comp);
  261. reinit_completion(&sysmon->shutdown_comp);
  262. ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp);
  263. if (ret < 0) {
  264. dev_err(sysmon->dev, "failed to allocate QMI txn\n");
  265. return;
  266. }
  267. ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
  268. SSCTL_SHUTDOWN_REQ, 0, NULL, NULL);
  269. if (ret < 0) {
  270. dev_err(sysmon->dev, "failed to send shutdown request\n");
  271. qmi_txn_cancel(&txn);
  272. return;
  273. }
  274. ret = qmi_txn_wait(&txn, 5 * HZ);
  275. if (ret < 0)
  276. dev_err(sysmon->dev, "failed receiving QMI response\n");
  277. else if (resp.resp.result)
  278. dev_err(sysmon->dev, "shutdown request failed\n");
  279. else
  280. dev_dbg(sysmon->dev, "shutdown request completed\n");
  281. if (sysmon->shutdown_irq > 0) {
  282. ret = wait_for_completion_timeout(&sysmon->shutdown_comp,
  283. 10 * HZ);
  284. if (!ret) {
  285. ret = try_wait_for_completion(&sysmon->ind_comp);
  286. if (!ret)
  287. dev_err(sysmon->dev,
  288. "timeout waiting for shutdown ack\n");
  289. }
  290. }
  291. }
  292. /**
  293. * ssctl_send_event() - send notification of other remote's SSR event
  294. * @sysmon: sysmon context
  295. * @event: sysmon event context
  296. */
  297. static void ssctl_send_event(struct qcom_sysmon *sysmon,
  298. const struct sysmon_event *event)
  299. {
  300. struct ssctl_subsys_event_resp resp;
  301. struct ssctl_subsys_event_req req;
  302. struct qmi_txn txn;
  303. int ret;
  304. memset(&resp, 0, sizeof(resp));
  305. ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_subsys_event_resp_ei, &resp);
  306. if (ret < 0) {
  307. dev_err(sysmon->dev, "failed to allocate QMI txn\n");
  308. return;
  309. }
  310. memset(&req, 0, sizeof(req));
  311. strlcpy(req.subsys_name, event->subsys_name, sizeof(req.subsys_name));
  312. req.subsys_name_len = strlen(req.subsys_name);
  313. req.event = event->ssr_event;
  314. req.evt_driven_valid = true;
  315. req.evt_driven = SSCTL_SSR_EVENT_FORCED;
  316. ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
  317. SSCTL_SUBSYS_EVENT_REQ, 40,
  318. ssctl_subsys_event_req_ei, &req);
  319. if (ret < 0) {
  320. dev_err(sysmon->dev, "failed to send shutdown request\n");
  321. qmi_txn_cancel(&txn);
  322. return;
  323. }
  324. ret = qmi_txn_wait(&txn, 5 * HZ);
  325. if (ret < 0)
  326. dev_err(sysmon->dev, "failed receiving QMI response\n");
  327. else if (resp.resp.result)
  328. dev_err(sysmon->dev, "ssr event send failed\n");
  329. else
  330. dev_dbg(sysmon->dev, "ssr event send completed\n");
  331. }
  332. /**
  333. * ssctl_new_server() - QMI callback indicating a new service
  334. * @qmi: QMI handle
  335. * @svc: service information
  336. *
  337. * Return: 0 if we're interested in this service, -EINVAL otherwise.
  338. */
  339. static int ssctl_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
  340. {
  341. struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
  342. switch (svc->version) {
  343. case 1:
  344. if (svc->instance != 0)
  345. return -EINVAL;
  346. if (strcmp(sysmon->name, "modem"))
  347. return -EINVAL;
  348. break;
  349. case 2:
  350. if (svc->instance != sysmon->ssctl_instance)
  351. return -EINVAL;
  352. break;
  353. default:
  354. return -EINVAL;
  355. }
  356. sysmon->ssctl_version = svc->version;
  357. sysmon->ssctl.sq_family = AF_QIPCRTR;
  358. sysmon->ssctl.sq_node = svc->node;
  359. sysmon->ssctl.sq_port = svc->port;
  360. svc->priv = sysmon;
  361. return 0;
  362. }
  363. /**
  364. * ssctl_del_server() - QMI callback indicating that @svc is removed
  365. * @qmi: QMI handle
  366. * @svc: service information
  367. */
  368. static void ssctl_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
  369. {
  370. struct qcom_sysmon *sysmon = svc->priv;
  371. sysmon->ssctl_version = 0;
  372. }
  373. static const struct qmi_ops ssctl_ops = {
  374. .new_server = ssctl_new_server,
  375. .del_server = ssctl_del_server,
  376. };
  377. static int sysmon_prepare(struct rproc_subdev *subdev)
  378. {
  379. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
  380. subdev);
  381. struct sysmon_event event = {
  382. .subsys_name = sysmon->name,
  383. .ssr_event = SSCTL_SSR_EVENT_BEFORE_POWERUP
  384. };
  385. mutex_lock(&sysmon->state_lock);
  386. sysmon->state = SSCTL_SSR_EVENT_BEFORE_POWERUP;
  387. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
  388. mutex_unlock(&sysmon->state_lock);
  389. return 0;
  390. }
  391. /**
  392. * sysmon_start() - start callback for the sysmon remoteproc subdevice
  393. * @subdev: instance of the sysmon subdevice
  394. *
  395. * Inform all the listners of sysmon notifications that the rproc associated
  396. * to @subdev has booted up. The rproc that booted up also needs to know
  397. * which rprocs are already up and running, so send start notifications
  398. * on behalf of all the online rprocs.
  399. */
  400. static int sysmon_start(struct rproc_subdev *subdev)
  401. {
  402. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
  403. subdev);
  404. struct qcom_sysmon *target;
  405. struct sysmon_event event = {
  406. .subsys_name = sysmon->name,
  407. .ssr_event = SSCTL_SSR_EVENT_AFTER_POWERUP
  408. };
  409. mutex_lock(&sysmon->state_lock);
  410. sysmon->state = SSCTL_SSR_EVENT_AFTER_POWERUP;
  411. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
  412. mutex_unlock(&sysmon->state_lock);
  413. mutex_lock(&sysmon_lock);
  414. list_for_each_entry(target, &sysmon_list, node) {
  415. if (target == sysmon)
  416. continue;
  417. mutex_lock(&target->state_lock);
  418. event.subsys_name = target->name;
  419. event.ssr_event = target->state;
  420. if (sysmon->ssctl_version == 2)
  421. ssctl_send_event(sysmon, &event);
  422. else if (sysmon->ept)
  423. sysmon_send_event(sysmon, &event);
  424. mutex_unlock(&target->state_lock);
  425. }
  426. mutex_unlock(&sysmon_lock);
  427. return 0;
  428. }
  429. static void sysmon_stop(struct rproc_subdev *subdev, bool crashed)
  430. {
  431. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, subdev);
  432. struct sysmon_event event = {
  433. .subsys_name = sysmon->name,
  434. .ssr_event = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN
  435. };
  436. mutex_lock(&sysmon->state_lock);
  437. sysmon->state = SSCTL_SSR_EVENT_BEFORE_SHUTDOWN;
  438. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
  439. mutex_unlock(&sysmon->state_lock);
  440. /* Don't request graceful shutdown if we've crashed */
  441. if (crashed)
  442. return;
  443. if (sysmon->ssctl_version)
  444. ssctl_request_shutdown(sysmon);
  445. else if (sysmon->ept)
  446. sysmon_request_shutdown(sysmon);
  447. }
  448. static void sysmon_unprepare(struct rproc_subdev *subdev)
  449. {
  450. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
  451. subdev);
  452. struct sysmon_event event = {
  453. .subsys_name = sysmon->name,
  454. .ssr_event = SSCTL_SSR_EVENT_AFTER_SHUTDOWN
  455. };
  456. mutex_lock(&sysmon->state_lock);
  457. sysmon->state = SSCTL_SSR_EVENT_AFTER_SHUTDOWN;
  458. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)&event);
  459. mutex_unlock(&sysmon->state_lock);
  460. }
  461. /**
  462. * sysmon_notify() - notify sysmon target of another's SSR
  463. * @nb: notifier_block associated with sysmon instance
  464. * @event: unused
  465. * @data: SSR identifier of the remote that is going down
  466. */
  467. static int sysmon_notify(struct notifier_block *nb, unsigned long event,
  468. void *data)
  469. {
  470. struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb);
  471. struct sysmon_event *sysmon_event = data;
  472. /* Skip non-running rprocs and the originating instance */
  473. if (sysmon->state != SSCTL_SSR_EVENT_AFTER_POWERUP ||
  474. !strcmp(sysmon_event->subsys_name, sysmon->name)) {
  475. dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name);
  476. return NOTIFY_DONE;
  477. }
  478. /* Only SSCTL version 2 supports SSR events */
  479. if (sysmon->ssctl_version == 2)
  480. ssctl_send_event(sysmon, sysmon_event);
  481. else if (sysmon->ept)
  482. sysmon_send_event(sysmon, sysmon_event);
  483. return NOTIFY_DONE;
  484. }
  485. static irqreturn_t sysmon_shutdown_interrupt(int irq, void *data)
  486. {
  487. struct qcom_sysmon *sysmon = data;
  488. complete(&sysmon->shutdown_comp);
  489. return IRQ_HANDLED;
  490. }
  491. /**
  492. * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
  493. * @rproc: rproc context to associate the subdev with
  494. * @name: name of this subdev, to use in SSR
  495. * @ssctl_instance: instance id of the ssctl QMI service
  496. *
  497. * Return: A new qcom_sysmon object, or NULL on failure
  498. */
  499. struct qcom_sysmon *qcom_add_sysmon_subdev(struct rproc *rproc,
  500. const char *name,
  501. int ssctl_instance)
  502. {
  503. struct qcom_sysmon *sysmon;
  504. int ret;
  505. sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL);
  506. if (!sysmon)
  507. return ERR_PTR(-ENOMEM);
  508. sysmon->dev = rproc->dev.parent;
  509. sysmon->rproc = rproc;
  510. sysmon->name = name;
  511. sysmon->ssctl_instance = ssctl_instance;
  512. init_completion(&sysmon->comp);
  513. init_completion(&sysmon->ind_comp);
  514. init_completion(&sysmon->shutdown_comp);
  515. mutex_init(&sysmon->lock);
  516. mutex_init(&sysmon->state_lock);
  517. sysmon->shutdown_irq = of_irq_get_byname(sysmon->dev->of_node,
  518. "shutdown-ack");
  519. if (sysmon->shutdown_irq < 0) {
  520. if (sysmon->shutdown_irq != -ENODATA) {
  521. dev_err(sysmon->dev,
  522. "failed to retrieve shutdown-ack IRQ\n");
  523. return ERR_PTR(sysmon->shutdown_irq);
  524. }
  525. } else {
  526. ret = devm_request_threaded_irq(sysmon->dev,
  527. sysmon->shutdown_irq,
  528. NULL, sysmon_shutdown_interrupt,
  529. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  530. "q6v5 shutdown-ack", sysmon);
  531. if (ret) {
  532. dev_err(sysmon->dev,
  533. "failed to acquire shutdown-ack IRQ\n");
  534. return ERR_PTR(ret);
  535. }
  536. }
  537. ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops,
  538. qmi_indication_handler);
  539. if (ret < 0) {
  540. dev_err(sysmon->dev, "failed to initialize qmi handle\n");
  541. kfree(sysmon);
  542. return ERR_PTR(ret);
  543. }
  544. qmi_add_lookup(&sysmon->qmi, 43, 0, 0);
  545. sysmon->subdev.prepare = sysmon_prepare;
  546. sysmon->subdev.start = sysmon_start;
  547. sysmon->subdev.stop = sysmon_stop;
  548. sysmon->subdev.unprepare = sysmon_unprepare;
  549. rproc_add_subdev(rproc, &sysmon->subdev);
  550. sysmon->nb.notifier_call = sysmon_notify;
  551. blocking_notifier_chain_register(&sysmon_notifiers, &sysmon->nb);
  552. mutex_lock(&sysmon_lock);
  553. list_add(&sysmon->node, &sysmon_list);
  554. mutex_unlock(&sysmon_lock);
  555. return sysmon;
  556. }
  557. EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev);
  558. /**
  559. * qcom_remove_sysmon_subdev() - release a qcom_sysmon
  560. * @sysmon: sysmon context, as retrieved by qcom_add_sysmon_subdev()
  561. */
  562. void qcom_remove_sysmon_subdev(struct qcom_sysmon *sysmon)
  563. {
  564. if (!sysmon)
  565. return;
  566. mutex_lock(&sysmon_lock);
  567. list_del(&sysmon->node);
  568. mutex_unlock(&sysmon_lock);
  569. blocking_notifier_chain_unregister(&sysmon_notifiers, &sysmon->nb);
  570. rproc_remove_subdev(sysmon->rproc, &sysmon->subdev);
  571. qmi_handle_release(&sysmon->qmi);
  572. kfree(sysmon);
  573. }
  574. EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev);
  575. /**
  576. * sysmon_probe() - probe sys_mon channel
  577. * @rpdev: rpmsg device handle
  578. *
  579. * Find the sysmon context associated with the ancestor remoteproc and assign
  580. * this rpmsg device with said sysmon context.
  581. *
  582. * Return: 0 on success, negative errno on failure.
  583. */
  584. static int sysmon_probe(struct rpmsg_device *rpdev)
  585. {
  586. struct qcom_sysmon *sysmon;
  587. struct rproc *rproc;
  588. rproc = rproc_get_by_child(&rpdev->dev);
  589. if (!rproc) {
  590. dev_err(&rpdev->dev, "sysmon device not child of rproc\n");
  591. return -EINVAL;
  592. }
  593. mutex_lock(&sysmon_lock);
  594. list_for_each_entry(sysmon, &sysmon_list, node) {
  595. if (sysmon->rproc == rproc)
  596. goto found;
  597. }
  598. mutex_unlock(&sysmon_lock);
  599. dev_err(&rpdev->dev, "no sysmon associated with parent rproc\n");
  600. return -EINVAL;
  601. found:
  602. mutex_unlock(&sysmon_lock);
  603. rpdev->ept->priv = sysmon;
  604. sysmon->ept = rpdev->ept;
  605. return 0;
  606. }
  607. /**
  608. * sysmon_remove() - sys_mon channel remove handler
  609. * @rpdev: rpmsg device handle
  610. *
  611. * Disassociate the rpmsg device with the sysmon instance.
  612. */
  613. static void sysmon_remove(struct rpmsg_device *rpdev)
  614. {
  615. struct qcom_sysmon *sysmon = rpdev->ept->priv;
  616. sysmon->ept = NULL;
  617. }
  618. static const struct rpmsg_device_id sysmon_match[] = {
  619. { "sys_mon" },
  620. {}
  621. };
  622. static struct rpmsg_driver sysmon_driver = {
  623. .probe = sysmon_probe,
  624. .remove = sysmon_remove,
  625. .callback = sysmon_callback,
  626. .id_table = sysmon_match,
  627. .drv = {
  628. .name = "qcom_sysmon",
  629. },
  630. };
  631. module_rpmsg_driver(sysmon_driver);
  632. MODULE_DESCRIPTION("Qualcomm sysmon driver");
  633. MODULE_LICENSE("GPL v2");