sst-ipc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Intel SST generic IPC Support
  3. *
  4. * Copyright (C) 2015, Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/wait.h>
  20. #include <linux/module.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/device.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/sched.h>
  26. #include <linux/delay.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/kthread.h>
  29. #include <sound/asound.h>
  30. #include "sst-dsp.h"
  31. #include "sst-dsp-priv.h"
  32. #include "sst-ipc.h"
  33. /* IPC message timeout (msecs) */
  34. #define IPC_TIMEOUT_MSECS 300
  35. #define IPC_EMPTY_LIST_SIZE 8
  36. /* locks held by caller */
  37. static struct ipc_message *msg_get_empty(struct sst_generic_ipc *ipc)
  38. {
  39. struct ipc_message *msg = NULL;
  40. if (!list_empty(&ipc->empty_list)) {
  41. msg = list_first_entry(&ipc->empty_list, struct ipc_message,
  42. list);
  43. list_del(&msg->list);
  44. }
  45. return msg;
  46. }
  47. static int tx_wait_done(struct sst_generic_ipc *ipc,
  48. struct ipc_message *msg, void *rx_data)
  49. {
  50. unsigned long flags;
  51. int ret;
  52. /* wait for DSP completion (in all cases atm inc pending) */
  53. ret = wait_event_timeout(msg->waitq, msg->complete,
  54. msecs_to_jiffies(IPC_TIMEOUT_MSECS));
  55. spin_lock_irqsave(&ipc->dsp->spinlock, flags);
  56. if (ret == 0) {
  57. if (ipc->ops.shim_dbg != NULL)
  58. ipc->ops.shim_dbg(ipc, "message timeout");
  59. list_del(&msg->list);
  60. ret = -ETIMEDOUT;
  61. } else {
  62. /* copy the data returned from DSP */
  63. if (msg->rx_size)
  64. memcpy(rx_data, msg->rx_data, msg->rx_size);
  65. ret = msg->errno;
  66. }
  67. list_add_tail(&msg->list, &ipc->empty_list);
  68. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  69. return ret;
  70. }
  71. static int ipc_tx_message(struct sst_generic_ipc *ipc, u64 header,
  72. void *tx_data, size_t tx_bytes, void *rx_data,
  73. size_t rx_bytes, int wait)
  74. {
  75. struct ipc_message *msg;
  76. unsigned long flags;
  77. spin_lock_irqsave(&ipc->dsp->spinlock, flags);
  78. msg = msg_get_empty(ipc);
  79. if (msg == NULL) {
  80. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  81. return -EBUSY;
  82. }
  83. msg->header = header;
  84. msg->tx_size = tx_bytes;
  85. msg->rx_size = rx_bytes;
  86. msg->wait = wait;
  87. msg->errno = 0;
  88. msg->pending = false;
  89. msg->complete = false;
  90. if ((tx_bytes) && (ipc->ops.tx_data_copy != NULL))
  91. ipc->ops.tx_data_copy(msg, tx_data, tx_bytes);
  92. list_add_tail(&msg->list, &ipc->tx_list);
  93. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  94. queue_kthread_work(&ipc->kworker, &ipc->kwork);
  95. if (wait)
  96. return tx_wait_done(ipc, msg, rx_data);
  97. else
  98. return 0;
  99. }
  100. static int msg_empty_list_init(struct sst_generic_ipc *ipc)
  101. {
  102. int i;
  103. ipc->msg = kzalloc(sizeof(struct ipc_message) *
  104. IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
  105. if (ipc->msg == NULL)
  106. return -ENOMEM;
  107. for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
  108. ipc->msg[i].tx_data = kzalloc(ipc->tx_data_max_size, GFP_KERNEL);
  109. if (ipc->msg[i].tx_data == NULL)
  110. goto free_mem;
  111. ipc->msg[i].rx_data = kzalloc(ipc->rx_data_max_size, GFP_KERNEL);
  112. if (ipc->msg[i].rx_data == NULL) {
  113. kfree(ipc->msg[i].tx_data);
  114. goto free_mem;
  115. }
  116. init_waitqueue_head(&ipc->msg[i].waitq);
  117. list_add(&ipc->msg[i].list, &ipc->empty_list);
  118. }
  119. return 0;
  120. free_mem:
  121. while (i > 0) {
  122. kfree(ipc->msg[i-1].tx_data);
  123. kfree(ipc->msg[i-1].rx_data);
  124. --i;
  125. }
  126. kfree(ipc->msg);
  127. return -ENOMEM;
  128. }
  129. static void ipc_tx_msgs(struct kthread_work *work)
  130. {
  131. struct sst_generic_ipc *ipc =
  132. container_of(work, struct sst_generic_ipc, kwork);
  133. struct ipc_message *msg;
  134. unsigned long flags;
  135. spin_lock_irqsave(&ipc->dsp->spinlock, flags);
  136. if (list_empty(&ipc->tx_list) || ipc->pending) {
  137. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  138. return;
  139. }
  140. /* if the DSP is busy, we will TX messages after IRQ.
  141. * also postpone if we are in the middle of procesing completion irq*/
  142. if (ipc->ops.is_dsp_busy && ipc->ops.is_dsp_busy(ipc->dsp)) {
  143. dev_dbg(ipc->dev, "ipc_tx_msgs dsp busy\n");
  144. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  145. return;
  146. }
  147. msg = list_first_entry(&ipc->tx_list, struct ipc_message, list);
  148. list_move(&msg->list, &ipc->rx_list);
  149. if (ipc->ops.tx_msg != NULL)
  150. ipc->ops.tx_msg(ipc, msg);
  151. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  152. }
  153. int sst_ipc_tx_message_wait(struct sst_generic_ipc *ipc, u64 header,
  154. void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes)
  155. {
  156. return ipc_tx_message(ipc, header, tx_data, tx_bytes,
  157. rx_data, rx_bytes, 1);
  158. }
  159. EXPORT_SYMBOL_GPL(sst_ipc_tx_message_wait);
  160. int sst_ipc_tx_message_nowait(struct sst_generic_ipc *ipc, u64 header,
  161. void *tx_data, size_t tx_bytes)
  162. {
  163. return ipc_tx_message(ipc, header, tx_data, tx_bytes,
  164. NULL, 0, 0);
  165. }
  166. EXPORT_SYMBOL_GPL(sst_ipc_tx_message_nowait);
  167. struct ipc_message *sst_ipc_reply_find_msg(struct sst_generic_ipc *ipc,
  168. u64 header)
  169. {
  170. struct ipc_message *msg;
  171. u64 mask;
  172. if (ipc->ops.reply_msg_match != NULL)
  173. header = ipc->ops.reply_msg_match(header, &mask);
  174. if (list_empty(&ipc->rx_list)) {
  175. dev_err(ipc->dev, "error: rx list empty but received 0x%llx\n",
  176. header);
  177. return NULL;
  178. }
  179. list_for_each_entry(msg, &ipc->rx_list, list) {
  180. if ((msg->header & mask) == header)
  181. return msg;
  182. }
  183. return NULL;
  184. }
  185. EXPORT_SYMBOL_GPL(sst_ipc_reply_find_msg);
  186. /* locks held by caller */
  187. void sst_ipc_tx_msg_reply_complete(struct sst_generic_ipc *ipc,
  188. struct ipc_message *msg)
  189. {
  190. msg->complete = true;
  191. if (!msg->wait)
  192. list_add_tail(&msg->list, &ipc->empty_list);
  193. else
  194. wake_up(&msg->waitq);
  195. }
  196. EXPORT_SYMBOL_GPL(sst_ipc_tx_msg_reply_complete);
  197. void sst_ipc_drop_all(struct sst_generic_ipc *ipc)
  198. {
  199. struct ipc_message *msg, *tmp;
  200. unsigned long flags;
  201. int tx_drop_cnt = 0, rx_drop_cnt = 0;
  202. /* drop all TX and Rx messages before we stall + reset DSP */
  203. spin_lock_irqsave(&ipc->dsp->spinlock, flags);
  204. list_for_each_entry_safe(msg, tmp, &ipc->tx_list, list) {
  205. list_move(&msg->list, &ipc->empty_list);
  206. tx_drop_cnt++;
  207. }
  208. list_for_each_entry_safe(msg, tmp, &ipc->rx_list, list) {
  209. list_move(&msg->list, &ipc->empty_list);
  210. rx_drop_cnt++;
  211. }
  212. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  213. if (tx_drop_cnt || rx_drop_cnt)
  214. dev_err(ipc->dev, "dropped IPC msg RX=%d, TX=%d\n",
  215. tx_drop_cnt, rx_drop_cnt);
  216. }
  217. EXPORT_SYMBOL_GPL(sst_ipc_drop_all);
  218. int sst_ipc_init(struct sst_generic_ipc *ipc)
  219. {
  220. int ret;
  221. INIT_LIST_HEAD(&ipc->tx_list);
  222. INIT_LIST_HEAD(&ipc->rx_list);
  223. INIT_LIST_HEAD(&ipc->empty_list);
  224. init_waitqueue_head(&ipc->wait_txq);
  225. ret = msg_empty_list_init(ipc);
  226. if (ret < 0)
  227. return -ENOMEM;
  228. /* start the IPC message thread */
  229. init_kthread_worker(&ipc->kworker);
  230. ipc->tx_thread = kthread_run(kthread_worker_fn,
  231. &ipc->kworker, "%s",
  232. dev_name(ipc->dev));
  233. if (IS_ERR(ipc->tx_thread)) {
  234. dev_err(ipc->dev, "error: failed to create message TX task\n");
  235. ret = PTR_ERR(ipc->tx_thread);
  236. kfree(ipc->msg);
  237. return ret;
  238. }
  239. init_kthread_work(&ipc->kwork, ipc_tx_msgs);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(sst_ipc_init);
  243. void sst_ipc_fini(struct sst_generic_ipc *ipc)
  244. {
  245. int i;
  246. if (ipc->tx_thread)
  247. kthread_stop(ipc->tx_thread);
  248. if (ipc->msg) {
  249. for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
  250. kfree(ipc->msg[i].tx_data);
  251. kfree(ipc->msg[i].rx_data);
  252. }
  253. kfree(ipc->msg);
  254. }
  255. }
  256. EXPORT_SYMBOL_GPL(sst_ipc_fini);
  257. /* Module information */
  258. MODULE_AUTHOR("Jin Yao");
  259. MODULE_DESCRIPTION("Intel SST IPC generic");
  260. MODULE_LICENSE("GPL v2");