qcom_sysmon.c 20 KB

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