apr_tal_glink.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (c) 2016-2017 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/mutex.h>
  19. #include <linux/sched.h>
  20. #include <linux/wait.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/slab.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/delay.h>
  27. #include <linux/clk.h>
  28. #include <soc/qcom/glink.h>
  29. #include <ipc/apr_tal.h>
  30. #define APR_MAXIMUM_NUM_OF_RETRIES 2
  31. struct apr_tx_buf {
  32. struct apr_pkt_priv pkt_priv;
  33. char buf[APR_MAX_BUF];
  34. };
  35. struct link_state {
  36. uint32_t dest;
  37. void *handle;
  38. enum glink_link_state link_state;
  39. wait_queue_head_t wait;
  40. };
  41. static struct link_state link_state[APR_DEST_MAX];
  42. static char *svc_names[APR_DEST_MAX][APR_CLIENT_MAX] = {
  43. {
  44. "apr_audio_svc",
  45. "apr_voice_svc",
  46. },
  47. {
  48. "apr_audio_svc",
  49. "apr_voice_svc",
  50. },
  51. };
  52. static struct apr_svc_ch_dev
  53. apr_svc_ch[APR_DL_MAX][APR_DEST_MAX][APR_CLIENT_MAX];
  54. static struct apr_tx_buf *apr_alloc_buf(int len)
  55. {
  56. if (len > APR_MAX_BUF) {
  57. pr_err("%s: buf too large [%d]\n", __func__, len);
  58. return ERR_PTR(-EINVAL);
  59. }
  60. return kzalloc(sizeof(struct apr_tx_buf), GFP_ATOMIC);
  61. }
  62. static void apr_free_buf(const void *ptr)
  63. {
  64. struct apr_pkt_priv *apr_pkt_priv = (struct apr_pkt_priv *)ptr;
  65. struct apr_tx_buf *tx_buf;
  66. if (!apr_pkt_priv) {
  67. pr_err("%s: Invalid apr_pkt_priv\n", __func__);
  68. return;
  69. }
  70. if (apr_pkt_priv->pkt_owner == APR_PKT_OWNER_DRIVER) {
  71. tx_buf = container_of((void *)apr_pkt_priv,
  72. struct apr_tx_buf, pkt_priv);
  73. pr_debug("%s: Freeing buffer %pK", __func__, tx_buf);
  74. kfree(tx_buf);
  75. }
  76. }
  77. static int __apr_tal_write(struct apr_svc_ch_dev *apr_ch, void *data,
  78. struct apr_pkt_priv *pkt_priv, int len)
  79. {
  80. int rc = 0;
  81. unsigned long flags;
  82. spin_lock_irqsave(&apr_ch->w_lock, flags);
  83. rc = glink_tx(apr_ch->handle, pkt_priv, data, len, GLINK_TX_ATOMIC);
  84. spin_unlock_irqrestore(&apr_ch->w_lock, flags);
  85. if (rc)
  86. pr_err("%s: glink_tx failed, rc[%d]\n", __func__, rc);
  87. else
  88. rc = len;
  89. return rc;
  90. }
  91. int apr_tal_write(struct apr_svc_ch_dev *apr_ch, void *data,
  92. struct apr_pkt_priv *pkt_priv, int len)
  93. {
  94. int rc = 0, retries = 0;
  95. void *pkt_data = NULL;
  96. struct apr_tx_buf *tx_buf = NULL;
  97. struct apr_pkt_priv *pkt_priv_ptr = pkt_priv;
  98. if (!apr_ch->handle || !pkt_priv)
  99. return -EINVAL;
  100. if (pkt_priv->pkt_owner == APR_PKT_OWNER_DRIVER) {
  101. tx_buf = apr_alloc_buf(len);
  102. if (IS_ERR_OR_NULL(tx_buf)) {
  103. rc = -EINVAL;
  104. goto exit;
  105. }
  106. memcpy(tx_buf->buf, data, len);
  107. memcpy(&tx_buf->pkt_priv, pkt_priv, sizeof(tx_buf->pkt_priv));
  108. pkt_priv_ptr = &tx_buf->pkt_priv;
  109. pkt_data = tx_buf->buf;
  110. } else {
  111. pkt_data = data;
  112. }
  113. do {
  114. if (rc == -EAGAIN)
  115. udelay(50);
  116. rc = __apr_tal_write(apr_ch, pkt_data, pkt_priv_ptr, len);
  117. } while (rc == -EAGAIN && retries++ < APR_MAXIMUM_NUM_OF_RETRIES);
  118. if (rc < 0) {
  119. pr_err("%s: Unable to send the packet, rc:%d\n", __func__, rc);
  120. if (pkt_priv->pkt_owner == APR_PKT_OWNER_DRIVER)
  121. kfree(tx_buf);
  122. }
  123. exit:
  124. return rc;
  125. }
  126. void apr_tal_notify_rx(void *handle, const void *priv, const void *pkt_priv,
  127. const void *ptr, size_t size)
  128. {
  129. struct apr_svc_ch_dev *apr_ch = (struct apr_svc_ch_dev *)priv;
  130. unsigned long flags;
  131. if (!apr_ch || !ptr) {
  132. pr_err("%s: Invalid apr_ch or ptr\n", __func__);
  133. return;
  134. }
  135. pr_debug("%s: Rx packet received\n", __func__);
  136. spin_lock_irqsave(&apr_ch->r_lock, flags);
  137. if (apr_ch->func)
  138. apr_ch->func((void *)ptr, size, (void *)pkt_priv);
  139. spin_unlock_irqrestore(&apr_ch->r_lock, flags);
  140. glink_rx_done(apr_ch->handle, ptr, true);
  141. }
  142. static void apr_tal_notify_tx_abort(void *handle, const void *priv,
  143. const void *pkt_priv)
  144. {
  145. pr_debug("%s: tx_abort received for pkt_priv:%pK\n",
  146. __func__, pkt_priv);
  147. apr_free_buf(pkt_priv);
  148. }
  149. void apr_tal_notify_tx_done(void *handle, const void *priv,
  150. const void *pkt_priv, const void *ptr)
  151. {
  152. pr_debug("%s: tx_done received for pkt_priv:%pK\n",
  153. __func__, pkt_priv);
  154. apr_free_buf(pkt_priv);
  155. }
  156. bool apr_tal_notify_rx_intent_req(void *handle, const void *priv,
  157. size_t req_size)
  158. {
  159. struct apr_svc_ch_dev *apr_ch = (struct apr_svc_ch_dev *)priv;
  160. if (!apr_ch) {
  161. pr_err("%s: Invalid apr_ch\n", __func__);
  162. return false;
  163. }
  164. pr_err("%s: No rx intents queued, unable to receive\n", __func__);
  165. return false;
  166. }
  167. static void apr_tal_notify_remote_rx_intent(void *handle, const void *priv,
  168. size_t size)
  169. {
  170. struct apr_svc_ch_dev *apr_ch = (struct apr_svc_ch_dev *)priv;
  171. if (!apr_ch) {
  172. pr_err("%s: Invalid apr_ch\n", __func__);
  173. return;
  174. }
  175. /*
  176. * This is to make sure that the far end has queued at least one intent
  177. * before we attmpt any IPC. A simple bool flag is used here instead of
  178. * a counter, as the far end is required to guarantee intent
  179. * availability for all use cases once the channel is fully opened.
  180. */
  181. pr_debug("%s: remote queued an intent\n", __func__);
  182. apr_ch->if_remote_intent_ready = true;
  183. wake_up(&apr_ch->wait);
  184. }
  185. void apr_tal_notify_state(void *handle, const void *priv, unsigned int event)
  186. {
  187. struct apr_svc_ch_dev *apr_ch = (struct apr_svc_ch_dev *)priv;
  188. if (!apr_ch) {
  189. pr_err("%s: Invalid apr_ch\n", __func__);
  190. return;
  191. }
  192. apr_ch->channel_state = event;
  193. pr_info("%s: Channel state[%d]\n", __func__, event);
  194. if (event == GLINK_CONNECTED)
  195. wake_up(&apr_ch->wait);
  196. }
  197. int apr_tal_rx_intents_config(struct apr_svc_ch_dev *apr_ch,
  198. int num_of_intents, uint32_t size)
  199. {
  200. int i;
  201. int rc = 0;
  202. if (!apr_ch || !num_of_intents || !size) {
  203. pr_err("%s: Invalid parameter\n", __func__);
  204. return -EINVAL;
  205. }
  206. for (i = 0; i < num_of_intents; i++) {
  207. rc = glink_queue_rx_intent(apr_ch->handle, apr_ch, size);
  208. if (rc) {
  209. pr_err("%s: Failed to queue rx intent, iteration[%d]\n",
  210. __func__, i);
  211. break;
  212. }
  213. }
  214. return rc;
  215. }
  216. struct apr_svc_ch_dev *apr_tal_open(uint32_t clnt, uint32_t dest, uint32_t dl,
  217. apr_svc_cb_fn func, void *priv)
  218. {
  219. int rc;
  220. struct glink_open_config open_cfg;
  221. struct apr_svc_ch_dev *apr_ch;
  222. if ((clnt >= APR_CLIENT_MAX) || (dest >= APR_DEST_MAX) ||
  223. (dl >= APR_DL_MAX)) {
  224. pr_err("%s: Invalid params, clnt:%d, dest:%d, dl:%d\n",
  225. __func__, clnt, dest, dl);
  226. return NULL;
  227. }
  228. apr_ch = &apr_svc_ch[dl][dest][clnt];
  229. mutex_lock(&apr_ch->m_lock);
  230. if (apr_ch->handle) {
  231. pr_err("%s: This channel is already opened\n", __func__);
  232. rc = -EBUSY;
  233. goto unlock;
  234. }
  235. if (link_state[dest].link_state != GLINK_LINK_STATE_UP) {
  236. rc = wait_event_timeout(link_state[dest].wait,
  237. link_state[dest].link_state == GLINK_LINK_STATE_UP,
  238. msecs_to_jiffies(APR_OPEN_TIMEOUT_MS));
  239. if (rc == 0) {
  240. pr_err("%s: Open timeout, dest:%d\n", __func__, dest);
  241. rc = -ETIMEDOUT;
  242. goto unlock;
  243. }
  244. pr_debug("%s: Wakeup done, dest:%d\n", __func__, dest);
  245. }
  246. memset(&open_cfg, 0, sizeof(struct glink_open_config));
  247. open_cfg.options = GLINK_OPT_INITIAL_XPORT;
  248. if (dest == APR_DEST_MODEM)
  249. open_cfg.edge = "mpss";
  250. else
  251. open_cfg.edge = "lpass";
  252. open_cfg.name = svc_names[dest][clnt];
  253. open_cfg.notify_rx = apr_tal_notify_rx;
  254. open_cfg.notify_tx_done = apr_tal_notify_tx_done;
  255. open_cfg.notify_state = apr_tal_notify_state;
  256. open_cfg.notify_rx_intent_req = apr_tal_notify_rx_intent_req;
  257. open_cfg.notify_remote_rx_intent = apr_tal_notify_remote_rx_intent;
  258. open_cfg.notify_tx_abort = apr_tal_notify_tx_abort;
  259. open_cfg.priv = apr_ch;
  260. open_cfg.transport = "smem";
  261. apr_ch->channel_state = GLINK_REMOTE_DISCONNECTED;
  262. apr_ch->handle = glink_open(&open_cfg);
  263. if (IS_ERR_OR_NULL(apr_ch->handle)) {
  264. pr_err("%s: glink_open failed %s\n", __func__,
  265. svc_names[dest][clnt]);
  266. apr_ch->handle = NULL;
  267. rc = -EINVAL;
  268. goto unlock;
  269. }
  270. rc = wait_event_timeout(apr_ch->wait,
  271. (apr_ch->channel_state == GLINK_CONNECTED), 5 * HZ);
  272. if (rc == 0) {
  273. pr_err("%s: TIMEOUT for OPEN event\n", __func__);
  274. rc = -ETIMEDOUT;
  275. goto close_link;
  276. }
  277. /*
  278. * Remote intent is not required for GLINK <--> SMD IPC, so this is
  279. * designed not to fail the open call.
  280. */
  281. rc = wait_event_timeout(apr_ch->wait,
  282. apr_ch->if_remote_intent_ready, 5 * HZ);
  283. if (rc == 0)
  284. pr_err("%s: TIMEOUT for remote intent readiness\n", __func__);
  285. rc = apr_tal_rx_intents_config(apr_ch, APR_DEFAULT_NUM_OF_INTENTS,
  286. APR_MAX_BUF);
  287. if (rc) {
  288. pr_err("%s: Unable to queue intents\n", __func__);
  289. goto close_link;
  290. }
  291. apr_ch->func = func;
  292. apr_ch->priv = priv;
  293. close_link:
  294. if (rc) {
  295. glink_close(apr_ch->handle);
  296. apr_ch->handle = NULL;
  297. }
  298. unlock:
  299. mutex_unlock(&apr_ch->m_lock);
  300. return rc ? NULL : apr_ch;
  301. }
  302. int apr_tal_start_rx_rt(struct apr_svc_ch_dev *apr_ch)
  303. {
  304. int rc = 0;
  305. if (!apr_ch || !apr_ch->handle) {
  306. rc = -EINVAL;
  307. goto exit;
  308. }
  309. mutex_lock(&apr_ch->m_lock);
  310. rc = glink_start_rx_rt(apr_ch->handle);
  311. mutex_unlock(&apr_ch->m_lock);
  312. exit:
  313. return rc;
  314. }
  315. int apr_tal_end_rx_rt(struct apr_svc_ch_dev *apr_ch)
  316. {
  317. int rc = 0;
  318. if (!apr_ch || !apr_ch->handle) {
  319. rc = -EINVAL;
  320. goto exit;
  321. }
  322. mutex_lock(&apr_ch->m_lock);
  323. rc = glink_end_rx_rt(apr_ch->handle);
  324. mutex_unlock(&apr_ch->m_lock);
  325. exit:
  326. return rc;
  327. }
  328. int apr_tal_close(struct apr_svc_ch_dev *apr_ch)
  329. {
  330. int rc;
  331. if (!apr_ch || !apr_ch->handle) {
  332. rc = -EINVAL;
  333. goto exit;
  334. }
  335. mutex_lock(&apr_ch->m_lock);
  336. rc = glink_close(apr_ch->handle);
  337. apr_ch->handle = NULL;
  338. apr_ch->func = NULL;
  339. apr_ch->priv = NULL;
  340. apr_ch->if_remote_intent_ready = false;
  341. mutex_unlock(&apr_ch->m_lock);
  342. exit:
  343. return rc;
  344. }
  345. static void apr_tal_link_state_cb(struct glink_link_state_cb_info *cb_info,
  346. void *priv)
  347. {
  348. uint32_t dest;
  349. if (!cb_info) {
  350. pr_err("%s: Invalid cb_info\n", __func__);
  351. return;
  352. }
  353. if (!strcmp(cb_info->edge, "mpss"))
  354. dest = APR_DEST_MODEM;
  355. else if (!strcmp(cb_info->edge, "lpass"))
  356. dest = APR_DEST_QDSP6;
  357. else {
  358. pr_err("%s:Unknown edge[%s]\n", __func__, cb_info->edge);
  359. return;
  360. }
  361. pr_info("%s: edge[%s] link state[%d]\n", __func__, cb_info->edge,
  362. cb_info->link_state);
  363. link_state[dest].link_state = cb_info->link_state;
  364. if (link_state[dest].link_state == GLINK_LINK_STATE_UP)
  365. wake_up(&link_state[dest].wait);
  366. }
  367. static struct glink_link_info mpss_link_info = {
  368. .transport = "smem",
  369. .edge = "mpss",
  370. .glink_link_state_notif_cb = apr_tal_link_state_cb,
  371. };
  372. static struct glink_link_info lpass_link_info = {
  373. .transport = "smem",
  374. .edge = "lpass",
  375. .glink_link_state_notif_cb = apr_tal_link_state_cb,
  376. };
  377. int apr_tal_init(void)
  378. {
  379. int i, j, k;
  380. for (i = 0; i < APR_DL_MAX; i++) {
  381. for (j = 0; j < APR_DEST_MAX; j++) {
  382. for (k = 0; k < APR_CLIENT_MAX; k++) {
  383. init_waitqueue_head(&apr_svc_ch[i][j][k].wait);
  384. spin_lock_init(&apr_svc_ch[i][j][k].w_lock);
  385. spin_lock_init(&apr_svc_ch[i][j][k].r_lock);
  386. mutex_init(&apr_svc_ch[i][j][k].m_lock);
  387. }
  388. }
  389. }
  390. for (i = 0; i < APR_DEST_MAX; i++)
  391. init_waitqueue_head(&link_state[i].wait);
  392. link_state[APR_DEST_MODEM].link_state = GLINK_LINK_STATE_DOWN;
  393. link_state[APR_DEST_MODEM].handle =
  394. glink_register_link_state_cb(&mpss_link_info, NULL);
  395. if (!link_state[APR_DEST_MODEM].handle)
  396. pr_err("%s: Unable to register mpss link state\n", __func__);
  397. link_state[APR_DEST_QDSP6].link_state = GLINK_LINK_STATE_DOWN;
  398. link_state[APR_DEST_QDSP6].handle =
  399. glink_register_link_state_cb(&lpass_link_info, NULL);
  400. if (!link_state[APR_DEST_QDSP6].handle)
  401. pr_err("%s: Unable to register lpass link state\n", __func__);
  402. return 0;
  403. }