qcom_sysmon.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017, Linaro Ltd.
  4. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/firmware.h>
  7. #include <linux/module.h>
  8. #include <linux/notifier.h>
  9. #include <linux/slab.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/rpmsg.h>
  16. #include <trace/events/rproc_qcom.h>
  17. #include "qcom_common.h"
  18. #define SYSMON_NOTIF_TIMEOUT CONFIG_RPROC_SYSMON_NOTIF_TIMEOUT
  19. #define SYSMON_SUBDEV_NAME "sysmon"
  20. static const char * const notif_timeout_msg = "sysmon msg from %s to %s for %s taking too long";
  21. static const char * const shutdown_timeout_msg = "sysmon_send_shutdown to %s taking too long";
  22. static BLOCKING_NOTIFIER_HEAD(sysmon_notifiers);
  23. struct qcom_sysmon;
  24. struct notif_timeout_data {
  25. struct qcom_sysmon *dest;
  26. struct timer_list timer;
  27. };
  28. struct qcom_sysmon {
  29. struct rproc_subdev subdev;
  30. struct rproc *rproc;
  31. int state;
  32. struct mutex state_lock;
  33. struct list_head node;
  34. const char *name;
  35. int shutdown_irq;
  36. int ssctl_version;
  37. int ssctl_instance;
  38. struct notif_timeout_data timeout_data;
  39. struct notifier_block nb;
  40. struct device *dev;
  41. uint32_t transaction_id;
  42. struct rpmsg_endpoint *ept;
  43. struct completion comp;
  44. struct completion ind_comp;
  45. struct completion shutdown_comp;
  46. struct completion ssctl_comp;
  47. struct mutex lock;
  48. bool ssr_ack;
  49. bool shutdown_acked;
  50. struct qmi_handle qmi;
  51. struct sockaddr_qrtr ssctl;
  52. };
  53. static DEFINE_MUTEX(sysmon_lock);
  54. static LIST_HEAD(sysmon_list);
  55. uint32_t qcom_sysmon_get_txn_id(struct qcom_sysmon *sysmon)
  56. {
  57. return sysmon->transaction_id;
  58. }
  59. EXPORT_SYMBOL(qcom_sysmon_get_txn_id);
  60. /**
  61. * sysmon_send_event() - send notification of other remote's SSR event
  62. * @sysmon: sysmon context
  63. * @event: sysmon event context
  64. */
  65. static void sysmon_send_event(struct qcom_sysmon *sysmon,
  66. const struct qcom_sysmon *source)
  67. {
  68. char req[50];
  69. int len;
  70. int ret;
  71. len = scnprintf(req, sizeof(req), "ssr:%s:%s", source->name,
  72. subdevice_state_string[source->state]);
  73. if (len >= sizeof(req))
  74. return;
  75. mutex_lock(&sysmon->lock);
  76. reinit_completion(&sysmon->comp);
  77. sysmon->ssr_ack = false;
  78. ret = rpmsg_send(sysmon->ept, req, len);
  79. if (ret < 0) {
  80. dev_err(sysmon->dev, "failed to send sysmon event\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, "unexpected response to sysmon event\n");
  91. out_unlock:
  92. mutex_unlock(&sysmon->lock);
  93. }
  94. /**
  95. * sysmon_request_shutdown() - request graceful shutdown of remote
  96. * @sysmon: sysmon context
  97. *
  98. * Return: boolean indicator of the remote processor acking the request
  99. */
  100. static bool sysmon_request_shutdown(struct qcom_sysmon *sysmon)
  101. {
  102. char *req = "ssr:shutdown";
  103. bool acked = false;
  104. int ret;
  105. mutex_lock(&sysmon->lock);
  106. reinit_completion(&sysmon->comp);
  107. sysmon->ssr_ack = false;
  108. ret = rpmsg_send(sysmon->ept, req, strlen(req) + 1);
  109. if (ret < 0) {
  110. dev_err(sysmon->dev, "send sysmon shutdown request failed\n");
  111. goto out_unlock;
  112. }
  113. ret = wait_for_completion_timeout(&sysmon->comp,
  114. msecs_to_jiffies(5000));
  115. if (!ret) {
  116. dev_err(sysmon->dev, "timeout waiting for sysmon ack\n");
  117. goto out_unlock;
  118. }
  119. if (!sysmon->ssr_ack)
  120. dev_err(sysmon->dev,
  121. "unexpected response to sysmon shutdown request\n");
  122. else
  123. acked = true;
  124. out_unlock:
  125. mutex_unlock(&sysmon->lock);
  126. return acked;
  127. }
  128. static int sysmon_callback(struct rpmsg_device *rpdev, void *data, int count,
  129. void *priv, u32 addr)
  130. {
  131. struct qcom_sysmon *sysmon = priv;
  132. const char *ssr_ack = "ssr:ack";
  133. const int ssr_ack_len = strlen(ssr_ack) + 1;
  134. if (!sysmon)
  135. return -EINVAL;
  136. if (count >= ssr_ack_len && !memcmp(data, ssr_ack, ssr_ack_len))
  137. sysmon->ssr_ack = true;
  138. complete(&sysmon->comp);
  139. return 0;
  140. }
  141. #define SSCTL_SHUTDOWN_REQ 0x21
  142. #define SSCTL_SHUTDOWN_READY_IND 0x21
  143. #define SSCTL_SUBSYS_EVENT_REQ 0x23
  144. #define SSCTL_SUBSYS_EVENT_WITH_TID_REQ 0x25
  145. #define SSCTL_MAX_MSG_LEN 7
  146. #define SSCTL_SUBSYS_NAME_LENGTH 15
  147. enum {
  148. SSCTL_SSR_EVENT_FORCED,
  149. SSCTL_SSR_EVENT_GRACEFUL,
  150. };
  151. struct ssctl_shutdown_resp {
  152. struct qmi_response_type_v01 resp;
  153. };
  154. static struct qmi_elem_info ssctl_shutdown_resp_ei[] = {
  155. {
  156. .data_type = QMI_STRUCT,
  157. .elem_len = 1,
  158. .elem_size = sizeof(struct qmi_response_type_v01),
  159. .array_type = NO_ARRAY,
  160. .tlv_type = 0x02,
  161. .offset = offsetof(struct ssctl_shutdown_resp, resp),
  162. .ei_array = qmi_response_type_v01_ei,
  163. },
  164. {}
  165. };
  166. struct ssctl_subsys_event_with_tid_req {
  167. u8 subsys_name_len;
  168. char subsys_name[SSCTL_SUBSYS_NAME_LENGTH];
  169. u32 event;
  170. u8 evt_driven_valid;
  171. u32 evt_driven;
  172. u8 transaction_id_valid;
  173. uint32_t transaction_id;
  174. };
  175. static struct qmi_elem_info ssctl_subsys_event_with_tid_req_ei[] = {
  176. {
  177. .data_type = QMI_DATA_LEN,
  178. .elem_len = 1,
  179. .elem_size = sizeof(uint8_t),
  180. .array_type = NO_ARRAY,
  181. .tlv_type = 0x01,
  182. .offset = offsetof(struct ssctl_subsys_event_with_tid_req,
  183. subsys_name_len),
  184. .ei_array = NULL,
  185. },
  186. {
  187. .data_type = QMI_UNSIGNED_1_BYTE,
  188. .elem_len = SSCTL_SUBSYS_NAME_LENGTH,
  189. .elem_size = sizeof(char),
  190. .array_type = VAR_LEN_ARRAY,
  191. .tlv_type = 0x01,
  192. .offset = offsetof(struct ssctl_subsys_event_with_tid_req,
  193. subsys_name),
  194. .ei_array = NULL,
  195. },
  196. {
  197. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  198. .elem_len = 1,
  199. .elem_size = sizeof(uint32_t),
  200. .array_type = NO_ARRAY,
  201. .tlv_type = 0x02,
  202. .offset = offsetof(struct ssctl_subsys_event_with_tid_req,
  203. event),
  204. .ei_array = NULL,
  205. },
  206. {
  207. .data_type = QMI_OPT_FLAG,
  208. .elem_len = 1,
  209. .elem_size = sizeof(uint8_t),
  210. .array_type = NO_ARRAY,
  211. .tlv_type = 0x03,
  212. .offset = offsetof(struct ssctl_subsys_event_with_tid_req,
  213. transaction_id_valid),
  214. .ei_array = NULL,
  215. },
  216. {
  217. .data_type = QMI_UNSIGNED_4_BYTE,
  218. .elem_len = 1,
  219. .elem_size = sizeof(uint32_t),
  220. .array_type = NO_ARRAY,
  221. .tlv_type = 0x03,
  222. .offset = offsetof(struct ssctl_subsys_event_with_tid_req,
  223. transaction_id),
  224. .ei_array = NULL,
  225. },
  226. {
  227. .data_type = QMI_OPT_FLAG,
  228. .elem_len = 1,
  229. .elem_size = sizeof(uint8_t),
  230. .array_type = NO_ARRAY,
  231. .tlv_type = 0x10,
  232. .offset = offsetof(struct ssctl_subsys_event_with_tid_req,
  233. evt_driven_valid),
  234. .ei_array = NULL,
  235. },
  236. {
  237. .data_type = QMI_SIGNED_4_BYTE_ENUM,
  238. .elem_len = 1,
  239. .elem_size = sizeof(uint32_t),
  240. .array_type = NO_ARRAY,
  241. .tlv_type = 0x10,
  242. .offset = offsetof(struct ssctl_subsys_event_with_tid_req,
  243. evt_driven),
  244. .ei_array = NULL,
  245. },
  246. {}
  247. };
  248. struct ssctl_subsys_event_with_tid_resp {
  249. struct qmi_response_type_v01 resp;
  250. };
  251. static struct qmi_elem_info ssctl_subsys_event_with_tid_resp_ei[] = {
  252. {
  253. .data_type = QMI_STRUCT,
  254. .elem_len = 1,
  255. .elem_size = sizeof(struct qmi_response_type_v01),
  256. .array_type = NO_ARRAY,
  257. .tlv_type = 0x02,
  258. .offset = offsetof(struct ssctl_subsys_event_with_tid_resp,
  259. resp),
  260. .ei_array = qmi_response_type_v01_ei,
  261. },
  262. {}
  263. };
  264. static struct qmi_elem_info ssctl_shutdown_ind_ei[] = {
  265. {}
  266. };
  267. static void sysmon_ind_cb(struct qmi_handle *qmi, struct sockaddr_qrtr *sq,
  268. struct qmi_txn *txn, const void *data)
  269. {
  270. struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
  271. complete(&sysmon->ind_comp);
  272. }
  273. static struct qmi_msg_handler qmi_indication_handler[] = {
  274. {
  275. .type = QMI_INDICATION,
  276. .msg_id = SSCTL_SHUTDOWN_READY_IND,
  277. .ei = ssctl_shutdown_ind_ei,
  278. .decoded_size = 0,
  279. .fn = sysmon_ind_cb
  280. },
  281. {}
  282. };
  283. static bool ssctl_request_shutdown_wait(struct qcom_sysmon *sysmon)
  284. {
  285. int ret;
  286. ret = wait_for_completion_timeout(&sysmon->shutdown_comp, 10 * HZ);
  287. if (ret)
  288. return true;
  289. ret = try_wait_for_completion(&sysmon->ind_comp);
  290. if (ret)
  291. return true;
  292. dev_err(sysmon->dev, "timeout waiting for shutdown ack\n");
  293. return false;
  294. }
  295. /**
  296. * ssctl_request_shutdown() - request shutdown via SSCTL QMI service
  297. * @sysmon: sysmon context
  298. *
  299. * Return: boolean indicator of the remote processor acking the request
  300. */
  301. static bool ssctl_request_shutdown(struct qcom_sysmon *sysmon)
  302. {
  303. struct ssctl_shutdown_resp resp;
  304. struct qmi_txn txn;
  305. bool acked = false;
  306. int ret;
  307. if (sysmon->ssctl_instance == -EINVAL)
  308. return false;
  309. reinit_completion(&sysmon->ind_comp);
  310. reinit_completion(&sysmon->shutdown_comp);
  311. ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_shutdown_resp_ei, &resp);
  312. if (ret < 0) {
  313. dev_err(sysmon->dev, "failed to allocate QMI txn\n");
  314. return false;
  315. }
  316. ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
  317. SSCTL_SHUTDOWN_REQ, 0, NULL, NULL);
  318. if (ret < 0) {
  319. dev_err(sysmon->dev, "failed to send shutdown request\n");
  320. qmi_txn_cancel(&txn);
  321. return false;
  322. }
  323. ret = qmi_txn_wait(&txn, 5 * HZ);
  324. if (ret < 0) {
  325. dev_err(sysmon->dev, "failed receiving QMI response\n");
  326. } else if (resp.resp.result) {
  327. dev_err(sysmon->dev, "shutdown request failed\n");
  328. } else {
  329. dev_dbg(sysmon->dev, "shutdown request completed\n");
  330. acked = true;
  331. }
  332. if (sysmon->shutdown_irq > 0)
  333. return ssctl_request_shutdown_wait(sysmon);
  334. return acked;
  335. }
  336. /**
  337. * ssctl_send_event() - send notification of other remote's SSR event
  338. * @sysmon: sysmon context
  339. * @event: sysmon event context
  340. */
  341. static int ssctl_send_event(struct qcom_sysmon *sysmon,
  342. const struct qcom_sysmon *source, bool is_tid_valid)
  343. {
  344. struct ssctl_subsys_event_with_tid_resp resp;
  345. struct ssctl_subsys_event_with_tid_req req;
  346. struct qmi_txn txn;
  347. int ret, ssctl_event;
  348. if (sysmon->ssctl_instance == -EINVAL)
  349. return -EINVAL;
  350. memset(&resp, 0, sizeof(resp));
  351. ret = qmi_txn_init(&sysmon->qmi, &txn, ssctl_subsys_event_with_tid_resp_ei, &resp);
  352. if (ret < 0) {
  353. dev_err(sysmon->dev, "failed to allocate QMI txn\n");
  354. return ret;
  355. }
  356. memset(&req, 0, sizeof(req));
  357. strscpy(req.subsys_name, source->name, sizeof(req.subsys_name));
  358. req.subsys_name_len = strlen(req.subsys_name);
  359. req.event = source->state;
  360. req.evt_driven_valid = true;
  361. req.evt_driven = SSCTL_SSR_EVENT_FORCED;
  362. req.transaction_id_valid = is_tid_valid ? false : true;
  363. req.transaction_id = sysmon->transaction_id;
  364. ssctl_event = is_tid_valid ? SSCTL_SUBSYS_EVENT_REQ : SSCTL_SUBSYS_EVENT_WITH_TID_REQ;
  365. if (!strcmp(dev_name(sysmon->dev), "3000000.remoteproc-adsp")) {
  366. pr_err("[%s] sending %d", __func__, req.event);
  367. }
  368. ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
  369. ssctl_event, 40, ssctl_subsys_event_with_tid_req_ei, &req);
  370. if (ret < 0) {
  371. dev_err(sysmon->dev, "failed to send shutdown request\n");
  372. qmi_txn_cancel(&txn);
  373. return ret;
  374. }
  375. ret = qmi_txn_wait(&txn, 5 * HZ);
  376. if (ret < 0) {
  377. dev_err(sysmon->dev, "failed receiving QMI response\n");
  378. if (!strcmp(dev_name(sysmon->dev), "3000000.remoteproc-adsp")) {
  379. pr_err("[%s] sending %d", __func__, req.event);
  380. }
  381. return ret;
  382. }
  383. if (resp.resp.result) {
  384. dev_err(sysmon->dev, "failed to receive %s ssr %s event. response result: %d error: %d\n",
  385. source->name, subdevice_state_string[source->state],
  386. resp.resp.result, resp.resp.error);
  387. return resp.resp.result;
  388. }
  389. dev_dbg(sysmon->dev, "ssr event send completed\n");
  390. return 0;
  391. }
  392. /**
  393. * ssctl_new_server() - QMI callback indicating a new service
  394. * @qmi: QMI handle
  395. * @svc: service information
  396. *
  397. * Return: 0 if we're interested in this service, -EINVAL otherwise.
  398. */
  399. static int ssctl_new_server(struct qmi_handle *qmi, struct qmi_service *svc)
  400. {
  401. struct qcom_sysmon *sysmon = container_of(qmi, struct qcom_sysmon, qmi);
  402. switch (svc->version) {
  403. case 1:
  404. if (svc->instance != 0)
  405. return -EINVAL;
  406. if (strcmp(sysmon->name, "modem"))
  407. return -EINVAL;
  408. break;
  409. case 2:
  410. if (svc->instance != sysmon->ssctl_instance)
  411. return -EINVAL;
  412. break;
  413. default:
  414. return -EINVAL;
  415. }
  416. sysmon->ssctl_version = svc->version;
  417. sysmon->ssctl.sq_family = AF_QIPCRTR;
  418. sysmon->ssctl.sq_node = svc->node;
  419. sysmon->ssctl.sq_port = svc->port;
  420. svc->priv = sysmon;
  421. complete(&sysmon->ssctl_comp);
  422. return 0;
  423. }
  424. /**
  425. * ssctl_del_server() - QMI callback indicating that @svc is removed
  426. * @qmi: QMI handle
  427. * @svc: service information
  428. */
  429. static void ssctl_del_server(struct qmi_handle *qmi, struct qmi_service *svc)
  430. {
  431. struct qcom_sysmon *sysmon = svc->priv;
  432. sysmon->ssctl_version = 0;
  433. }
  434. static const struct qmi_ops ssctl_ops = {
  435. .new_server = ssctl_new_server,
  436. .del_server = ssctl_del_server,
  437. };
  438. static void sysmon_notif_timeout_handler(struct timer_list *t)
  439. {
  440. struct notif_timeout_data *td = from_timer(td, t, timer);
  441. struct qcom_sysmon *sysmon = container_of(td, struct qcom_sysmon, timeout_data);
  442. if (IS_ENABLED(CONFIG_QCOM_PANIC_ON_NOTIF_TIMEOUT) &&
  443. system_state != SYSTEM_RESTART &&
  444. system_state != SYSTEM_POWER_OFF &&
  445. system_state != SYSTEM_HALT &&
  446. !qcom_device_shutdown_in_progress)
  447. panic(notif_timeout_msg, sysmon->name, td->dest->name,
  448. subdevice_state_string[sysmon->state]);
  449. else
  450. WARN(1, notif_timeout_msg, sysmon->name, td->dest->name,
  451. subdevice_state_string[sysmon->state]);
  452. }
  453. static void sysmon_shutdown_notif_timeout_handler(struct timer_list *t)
  454. {
  455. struct notif_timeout_data *td = from_timer(td, t, timer);
  456. struct qcom_sysmon *sysmon = container_of(td, struct qcom_sysmon, timeout_data);
  457. if (IS_ENABLED(CONFIG_QCOM_PANIC_ON_NOTIF_TIMEOUT) &&
  458. system_state != SYSTEM_RESTART &&
  459. system_state != SYSTEM_POWER_OFF &&
  460. system_state != SYSTEM_HALT &&
  461. !qcom_device_shutdown_in_progress)
  462. panic(shutdown_timeout_msg, sysmon->name);
  463. else
  464. WARN(1, shutdown_timeout_msg, sysmon->name);
  465. }
  466. static inline void send_event(struct qcom_sysmon *sysmon, struct qcom_sysmon *source)
  467. {
  468. unsigned long timeout;
  469. int ret;
  470. source->timeout_data.timer.function = sysmon_notif_timeout_handler;
  471. source->timeout_data.dest = sysmon;
  472. timeout = jiffies + msecs_to_jiffies(SYSMON_NOTIF_TIMEOUT);
  473. mod_timer(&source->timeout_data.timer, timeout);
  474. /* Only SSCTL version 2 supports SSR events */
  475. if (sysmon->ssctl_version == 2) {
  476. ret = ssctl_send_event(sysmon, source, false);
  477. if (ret == QMI_RESULT_FAILURE_V01) {
  478. /* Retry with older ssctl event */
  479. dev_dbg(sysmon->dev, "Retrying with no trascation id request\n");
  480. ret = ssctl_send_event(sysmon, source, true);
  481. }
  482. /* if ret !=1 we don't retry */
  483. if (ret)
  484. pr_err("Failed to send event\n");
  485. }
  486. else if (sysmon->ept)
  487. sysmon_send_event(sysmon, source);
  488. del_timer_sync(&source->timeout_data.timer);
  489. }
  490. static int sysmon_prepare(struct rproc_subdev *subdev)
  491. {
  492. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
  493. subdev);
  494. trace_rproc_qcom_event(dev_name(sysmon->rproc->dev.parent), SYSMON_SUBDEV_NAME, "prepare");
  495. mutex_lock(&sysmon->state_lock);
  496. sysmon->state = QCOM_SSR_BEFORE_POWERUP;
  497. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)sysmon);
  498. mutex_unlock(&sysmon->state_lock);
  499. return 0;
  500. }
  501. /**
  502. * sysmon_start() - start callback for the sysmon remoteproc subdevice
  503. * @subdev: instance of the sysmon subdevice
  504. *
  505. * Inform all the listners of sysmon notifications that the rproc associated
  506. * to @subdev has booted up. The rproc that booted up also needs to know
  507. * which rprocs are already up and running, so send start notifications
  508. * on behalf of all the online rprocs.
  509. */
  510. static int sysmon_start(struct rproc_subdev *subdev)
  511. {
  512. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
  513. subdev);
  514. struct qcom_sysmon *target;
  515. trace_rproc_qcom_event(dev_name(sysmon->rproc->dev.parent), SYSMON_SUBDEV_NAME, "start");
  516. reinit_completion(&sysmon->ssctl_comp);
  517. mutex_lock(&sysmon->state_lock);
  518. sysmon->state = QCOM_SSR_AFTER_POWERUP;
  519. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)sysmon);
  520. mutex_unlock(&sysmon->state_lock);
  521. mutex_lock(&sysmon_lock);
  522. list_for_each_entry(target, &sysmon_list, node) {
  523. mutex_lock(&target->state_lock);
  524. if (target == sysmon || target->state != QCOM_SSR_AFTER_POWERUP) {
  525. mutex_unlock(&target->state_lock);
  526. continue;
  527. }
  528. send_event(sysmon, target);
  529. mutex_unlock(&target->state_lock);
  530. }
  531. mutex_unlock(&sysmon_lock);
  532. return 0;
  533. }
  534. static void sysmon_stop(struct rproc_subdev *subdev, bool crashed)
  535. {
  536. unsigned long timeout;
  537. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon, subdev);
  538. trace_rproc_qcom_event(dev_name(sysmon->rproc->dev.parent), SYSMON_SUBDEV_NAME,
  539. crashed ? "crash stop" : "stop");
  540. sysmon->shutdown_acked = false;
  541. mutex_lock(&sysmon->state_lock);
  542. sysmon->state = QCOM_SSR_BEFORE_SHUTDOWN;
  543. sysmon->transaction_id++;
  544. dev_info(sysmon->dev, "Incrementing tid for %s to %d\n", sysmon->name,
  545. sysmon->transaction_id);
  546. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)sysmon);
  547. mutex_unlock(&sysmon->state_lock);
  548. /* Don't request graceful shutdown if we've crashed */
  549. if (crashed)
  550. return;
  551. sysmon->timeout_data.timer.function = sysmon_shutdown_notif_timeout_handler;
  552. timeout = jiffies + msecs_to_jiffies(SYSMON_NOTIF_TIMEOUT);
  553. mod_timer(&sysmon->timeout_data.timer, timeout);
  554. if (sysmon->ssctl_instance) {
  555. if (!wait_for_completion_timeout(&sysmon->ssctl_comp, HZ / 2))
  556. dev_err(sysmon->dev, "timeout waiting for ssctl service\n");
  557. }
  558. if (sysmon->ssctl_version)
  559. sysmon->shutdown_acked = ssctl_request_shutdown(sysmon);
  560. else if (sysmon->ept)
  561. sysmon->shutdown_acked = sysmon_request_shutdown(sysmon);
  562. del_timer_sync(&sysmon->timeout_data.timer);
  563. }
  564. static void sysmon_unprepare(struct rproc_subdev *subdev)
  565. {
  566. struct qcom_sysmon *sysmon = container_of(subdev, struct qcom_sysmon,
  567. subdev);
  568. trace_rproc_qcom_event(dev_name(sysmon->rproc->dev.parent), SYSMON_SUBDEV_NAME,
  569. "unprepare");
  570. mutex_lock(&sysmon->state_lock);
  571. sysmon->state = QCOM_SSR_AFTER_SHUTDOWN;
  572. blocking_notifier_call_chain(&sysmon_notifiers, 0, (void *)sysmon);
  573. mutex_unlock(&sysmon->state_lock);
  574. }
  575. /**
  576. * sysmon_notify() - notify sysmon target of another's SSR
  577. * @nb: notifier_block associated with sysmon instance
  578. * @event: unused
  579. * @data: SSR identifier of the remote that is going down
  580. */
  581. static int sysmon_notify(struct notifier_block *nb, unsigned long event,
  582. void *data)
  583. {
  584. struct qcom_sysmon *sysmon = container_of(nb, struct qcom_sysmon, nb);
  585. struct qcom_sysmon *source = data;
  586. /* Skip non-running rprocs and the originating instance */
  587. if (sysmon->state != QCOM_SSR_AFTER_POWERUP ||
  588. !strcmp(source->name, sysmon->name)) {
  589. dev_dbg(sysmon->dev, "not notifying %s\n", sysmon->name);
  590. return NOTIFY_DONE;
  591. }
  592. send_event(sysmon, source);
  593. return NOTIFY_DONE;
  594. }
  595. static irqreturn_t sysmon_shutdown_interrupt(int irq, void *data)
  596. {
  597. struct qcom_sysmon *sysmon = data;
  598. complete(&sysmon->shutdown_comp);
  599. return IRQ_HANDLED;
  600. }
  601. #define QMI_SSCTL_GET_FAILURE_REASON_REQ 0x0022
  602. #define QMI_SSCTL_EMPTY_MSG_LENGTH 0
  603. #define QMI_SSCTL_ERROR_MSG_LENGTH 90
  604. #define QMI_EOTI_DATA_TYPE \
  605. { \
  606. .data_type = QMI_EOTI, \
  607. .elem_len = 0, \
  608. .elem_size = 0, \
  609. .array_type = NO_ARRAY,\
  610. .tlv_type = 0x00, \
  611. .offset = 0, \
  612. .ei_array = NULL, \
  613. },
  614. struct qmi_ssctl_get_failure_reason_resp_msg {
  615. struct qmi_response_type_v01 resp;
  616. uint8_t error_message_valid;
  617. uint32_t error_message_len;
  618. char error_message[QMI_SSCTL_ERROR_MSG_LENGTH];
  619. };
  620. static struct qmi_elem_info qmi_ssctl_get_failure_reason_req_msg_ei[] = {
  621. QMI_EOTI_DATA_TYPE
  622. };
  623. static struct qmi_elem_info qmi_ssctl_get_failure_reason_resp_msg_ei[] = {
  624. {
  625. .data_type = QMI_STRUCT,
  626. .elem_len = 1,
  627. .elem_size = sizeof(struct qmi_response_type_v01),
  628. .array_type = NO_ARRAY,
  629. .tlv_type = 0x02,
  630. .offset = offsetof(
  631. struct qmi_ssctl_get_failure_reason_resp_msg,
  632. resp),
  633. .ei_array = qmi_response_type_v01_ei,
  634. },
  635. {
  636. .data_type = QMI_OPT_FLAG,
  637. .elem_len = 1,
  638. .elem_size = sizeof(uint8_t),
  639. .array_type = NO_ARRAY,
  640. .tlv_type = 0x10,
  641. .offset = offsetof(
  642. struct qmi_ssctl_get_failure_reason_resp_msg,
  643. error_message_valid),
  644. .ei_array = NULL,
  645. },
  646. {
  647. .data_type = QMI_DATA_LEN,
  648. .elem_len = 1,
  649. .elem_size = sizeof(uint8_t),
  650. .array_type = NO_ARRAY,
  651. .tlv_type = 0x10,
  652. .offset = offsetof(
  653. struct qmi_ssctl_get_failure_reason_resp_msg,
  654. error_message_len),
  655. .ei_array = NULL,
  656. },
  657. {
  658. .data_type = QMI_UNSIGNED_1_BYTE,
  659. .elem_len = QMI_SSCTL_ERROR_MSG_LENGTH,
  660. .elem_size = sizeof(char),
  661. .array_type = VAR_LEN_ARRAY,
  662. .tlv_type = 0x10,
  663. .offset = offsetof(
  664. struct qmi_ssctl_get_failure_reason_resp_msg,
  665. error_message),
  666. .ei_array = NULL,
  667. },
  668. QMI_EOTI_DATA_TYPE
  669. };
  670. /**
  671. * qcom_sysmon_get_reason() - Retrieve failure reason from a subsystem.
  672. * @dest_desc: Subsystem descriptor of the subsystem to query
  673. * @buf: Caller-allocated buffer for the returned NUL-terminated reason
  674. * @len: Length of @buf
  675. *
  676. * Reverts to using legacy sysmon API (sysmon_get_reason_no_qmi()) if client
  677. * handle is not set.
  678. *
  679. * Returns 0 for success, -EINVAL for an invalid destination, -ENODEV if
  680. * the SMD transport channel is not open, -ETIMEDOUT if the destination
  681. * subsystem does not respond, and -EPROTO if the destination subsystem
  682. * responds with something unexpected.
  683. *
  684. */
  685. int qcom_sysmon_get_reason(struct qcom_sysmon *sysmon, char *buf, size_t len)
  686. {
  687. char req = 0;
  688. struct qmi_ssctl_get_failure_reason_resp_msg resp;
  689. struct qmi_txn txn;
  690. const char *dest_ss;
  691. int ret;
  692. if (sysmon == NULL || buf == NULL || len == 0)
  693. return -EINVAL;
  694. dest_ss = sysmon->name;
  695. ret = qmi_txn_init(&sysmon->qmi, &txn, qmi_ssctl_get_failure_reason_resp_msg_ei,
  696. &resp);
  697. if (ret < 0) {
  698. pr_err("SYSMON QMI tx init failed to dest %s, ret - %d\n", dest_ss, ret);
  699. goto out;
  700. }
  701. ret = qmi_send_request(&sysmon->qmi, &sysmon->ssctl, &txn,
  702. QMI_SSCTL_GET_FAILURE_REASON_REQ,
  703. QMI_SSCTL_EMPTY_MSG_LENGTH,
  704. qmi_ssctl_get_failure_reason_req_msg_ei,
  705. &req);
  706. if (ret < 0) {
  707. pr_err("SYSMON QMI send req failed to dest %s, ret - %d\n", dest_ss, ret);
  708. qmi_txn_cancel(&txn);
  709. goto out;
  710. }
  711. ret = qmi_txn_wait(&txn, 5 * HZ);
  712. if (ret < 0) {
  713. pr_err("SYSMON QMI qmi txn wait failed to dest %s, ret - %d\n", dest_ss, ret);
  714. goto out;
  715. } else if (resp.resp.result) {
  716. dev_err(sysmon->dev, "failed to receive req. response result: %d\n",
  717. resp.resp.result);
  718. goto out;
  719. }
  720. strscpy(buf, resp.error_message, len);
  721. out:
  722. return ret;
  723. }
  724. EXPORT_SYMBOL(qcom_sysmon_get_reason);
  725. /**
  726. * qcom_add_sysmon_subdev() - create a sysmon subdev for the given remoteproc
  727. * @rproc: rproc context to associate the subdev with
  728. * @name: name of this subdev, to use in SSR
  729. * @ssctl_instance: instance id of the ssctl QMI service
  730. *
  731. * Return: A new qcom_sysmon object, or NULL on failure
  732. */
  733. struct qcom_sysmon *qcom_add_sysmon_subdev(struct rproc *rproc,
  734. const char *name,
  735. int ssctl_instance)
  736. {
  737. struct qcom_sysmon *sysmon;
  738. int ret;
  739. sysmon = kzalloc(sizeof(*sysmon), GFP_KERNEL);
  740. if (!sysmon)
  741. return ERR_PTR(-ENOMEM);
  742. sysmon->dev = rproc->dev.parent;
  743. sysmon->rproc = rproc;
  744. sysmon->name = name;
  745. sysmon->ssctl_instance = ssctl_instance;
  746. init_completion(&sysmon->comp);
  747. init_completion(&sysmon->ind_comp);
  748. init_completion(&sysmon->shutdown_comp);
  749. init_completion(&sysmon->ssctl_comp);
  750. timer_setup(&sysmon->timeout_data.timer, sysmon_notif_timeout_handler, 0);
  751. mutex_init(&sysmon->lock);
  752. mutex_init(&sysmon->state_lock);
  753. if (sysmon->ssctl_instance == -EINVAL)
  754. goto add_subdev_callbacks;
  755. sysmon->shutdown_irq = of_irq_get_byname(sysmon->dev->of_node,
  756. "shutdown-ack");
  757. if (sysmon->shutdown_irq < 0) {
  758. if (sysmon->shutdown_irq != -ENODATA) {
  759. dev_err(sysmon->dev,
  760. "failed to retrieve shutdown-ack IRQ\n");
  761. ret = sysmon->shutdown_irq;
  762. kfree(sysmon);
  763. return ERR_PTR(ret);
  764. }
  765. } else {
  766. ret = devm_request_threaded_irq(sysmon->dev,
  767. sysmon->shutdown_irq,
  768. NULL, sysmon_shutdown_interrupt,
  769. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  770. "q6v5 shutdown-ack", sysmon);
  771. if (ret) {
  772. dev_err(sysmon->dev,
  773. "failed to acquire shutdown-ack IRQ\n");
  774. kfree(sysmon);
  775. return ERR_PTR(ret);
  776. }
  777. }
  778. ret = qmi_handle_init(&sysmon->qmi, SSCTL_MAX_MSG_LEN, &ssctl_ops,
  779. qmi_indication_handler);
  780. if (ret < 0) {
  781. dev_err(sysmon->dev, "failed to initialize qmi handle\n");
  782. kfree(sysmon);
  783. return ERR_PTR(ret);
  784. }
  785. qmi_add_lookup(&sysmon->qmi, 43, 0, 0);
  786. add_subdev_callbacks:
  787. sysmon->subdev.prepare = sysmon_prepare;
  788. sysmon->subdev.start = sysmon_start;
  789. sysmon->subdev.stop = sysmon_stop;
  790. sysmon->subdev.unprepare = sysmon_unprepare;
  791. rproc_add_subdev(rproc, &sysmon->subdev);
  792. sysmon->nb.notifier_call = sysmon_notify;
  793. blocking_notifier_chain_register(&sysmon_notifiers, &sysmon->nb);
  794. mutex_lock(&sysmon_lock);
  795. list_add(&sysmon->node, &sysmon_list);
  796. mutex_unlock(&sysmon_lock);
  797. return sysmon;
  798. }
  799. EXPORT_SYMBOL_GPL(qcom_add_sysmon_subdev);
  800. /**
  801. * qcom_remove_sysmon_subdev() - release a qcom_sysmon
  802. * @sysmon: sysmon context, as retrieved by qcom_add_sysmon_subdev()
  803. */
  804. void qcom_remove_sysmon_subdev(struct qcom_sysmon *sysmon)
  805. {
  806. if (!sysmon)
  807. return;
  808. mutex_lock(&sysmon_lock);
  809. list_del(&sysmon->node);
  810. mutex_unlock(&sysmon_lock);
  811. blocking_notifier_chain_unregister(&sysmon_notifiers, &sysmon->nb);
  812. rproc_remove_subdev(sysmon->rproc, &sysmon->subdev);
  813. if (sysmon->ssctl_instance != -EINVAL)
  814. qmi_handle_release(&sysmon->qmi);
  815. kfree(sysmon);
  816. }
  817. EXPORT_SYMBOL_GPL(qcom_remove_sysmon_subdev);
  818. /**
  819. * qcom_sysmon_shutdown_acked() - query the success of the last shutdown
  820. * @sysmon: sysmon context
  821. *
  822. * When sysmon is used to request a graceful shutdown of the remote processor
  823. * this can be used by the remoteproc driver to query the success, in order to
  824. * know if it should fall back to other means of requesting a shutdown.
  825. *
  826. * Return: boolean indicator of the success of the last shutdown request
  827. */
  828. bool qcom_sysmon_shutdown_acked(struct qcom_sysmon *sysmon)
  829. {
  830. return sysmon && sysmon->shutdown_acked;
  831. }
  832. EXPORT_SYMBOL_GPL(qcom_sysmon_shutdown_acked);
  833. /**
  834. * sysmon_probe() - probe sys_mon channel
  835. * @rpdev: rpmsg device handle
  836. *
  837. * Find the sysmon context associated with the ancestor remoteproc and assign
  838. * this rpmsg device with said sysmon context.
  839. *
  840. * Return: 0 on success, negative errno on failure.
  841. */
  842. static int sysmon_probe(struct rpmsg_device *rpdev)
  843. {
  844. struct qcom_sysmon *sysmon;
  845. struct rproc *rproc;
  846. rproc = rproc_get_by_child(&rpdev->dev);
  847. if (!rproc) {
  848. dev_err(&rpdev->dev, "sysmon device not child of rproc\n");
  849. return -EINVAL;
  850. }
  851. mutex_lock(&sysmon_lock);
  852. list_for_each_entry(sysmon, &sysmon_list, node) {
  853. if (sysmon->rproc == rproc)
  854. goto found;
  855. }
  856. mutex_unlock(&sysmon_lock);
  857. dev_err(&rpdev->dev, "no sysmon associated with parent rproc\n");
  858. return -EINVAL;
  859. found:
  860. mutex_unlock(&sysmon_lock);
  861. rpdev->ept->priv = sysmon;
  862. sysmon->ept = rpdev->ept;
  863. return 0;
  864. }
  865. /**
  866. * sysmon_remove() - sys_mon channel remove handler
  867. * @rpdev: rpmsg device handle
  868. *
  869. * Disassociate the rpmsg device with the sysmon instance.
  870. */
  871. static void sysmon_remove(struct rpmsg_device *rpdev)
  872. {
  873. struct qcom_sysmon *sysmon = rpdev->ept->priv;
  874. sysmon->ept = NULL;
  875. }
  876. static const struct rpmsg_device_id sysmon_match[] = {
  877. { "sys_mon" },
  878. {}
  879. };
  880. static struct rpmsg_driver sysmon_driver = {
  881. .probe = sysmon_probe,
  882. .remove = sysmon_remove,
  883. .callback = sysmon_callback,
  884. .id_table = sysmon_match,
  885. .drv = {
  886. .name = "qcom_sysmon",
  887. },
  888. };
  889. module_rpmsg_driver(sysmon_driver);
  890. MODULE_DESCRIPTION("Qualcomm sysmon driver");
  891. MODULE_LICENSE("GPL v2");