sst-ipc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <sound/asound.h>
  29. #include "sst-dsp.h"
  30. #include "sst-dsp-priv.h"
  31. #include "sst-ipc.h"
  32. /* IPC message timeout (msecs) */
  33. #define IPC_TIMEOUT_MSECS 300
  34. #define IPC_EMPTY_LIST_SIZE 8
  35. /* locks held by caller */
  36. static struct ipc_message *msg_get_empty(struct sst_generic_ipc *ipc)
  37. {
  38. struct ipc_message *msg = NULL;
  39. if (!list_empty(&ipc->empty_list)) {
  40. msg = list_first_entry(&ipc->empty_list, struct ipc_message,
  41. list);
  42. list_del(&msg->list);
  43. }
  44. return msg;
  45. }
  46. static int tx_wait_done(struct sst_generic_ipc *ipc,
  47. struct ipc_message *msg, void *rx_data)
  48. {
  49. unsigned long flags;
  50. int ret;
  51. /* wait for DSP completion (in all cases atm inc pending) */
  52. ret = wait_event_timeout(msg->waitq, msg->complete,
  53. msecs_to_jiffies(IPC_TIMEOUT_MSECS));
  54. spin_lock_irqsave(&ipc->dsp->spinlock, flags);
  55. if (ret == 0) {
  56. if (ipc->ops.shim_dbg != NULL)
  57. ipc->ops.shim_dbg(ipc, "message timeout");
  58. list_del(&msg->list);
  59. ret = -ETIMEDOUT;
  60. } else {
  61. /* copy the data returned from DSP */
  62. if (msg->rx_size)
  63. memcpy(rx_data, msg->rx_data, msg->rx_size);
  64. ret = msg->errno;
  65. }
  66. list_add_tail(&msg->list, &ipc->empty_list);
  67. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  68. return ret;
  69. }
  70. static int ipc_tx_message(struct sst_generic_ipc *ipc, u64 header,
  71. void *tx_data, size_t tx_bytes, void *rx_data,
  72. size_t rx_bytes, int wait)
  73. {
  74. struct ipc_message *msg;
  75. unsigned long flags;
  76. spin_lock_irqsave(&ipc->dsp->spinlock, flags);
  77. msg = msg_get_empty(ipc);
  78. if (msg == NULL) {
  79. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  80. return -EBUSY;
  81. }
  82. msg->header = header;
  83. msg->tx_size = tx_bytes;
  84. msg->rx_size = rx_bytes;
  85. msg->wait = wait;
  86. msg->errno = 0;
  87. msg->pending = false;
  88. msg->complete = false;
  89. if ((tx_bytes) && (ipc->ops.tx_data_copy != NULL))
  90. ipc->ops.tx_data_copy(msg, tx_data, tx_bytes);
  91. list_add_tail(&msg->list, &ipc->tx_list);
  92. schedule_work(&ipc->kwork);
  93. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  94. if (wait)
  95. return tx_wait_done(ipc, msg, rx_data);
  96. else
  97. return 0;
  98. }
  99. static int msg_empty_list_init(struct sst_generic_ipc *ipc)
  100. {
  101. int i;
  102. ipc->msg = kcalloc(IPC_EMPTY_LIST_SIZE, sizeof(struct ipc_message),
  103. GFP_KERNEL);
  104. if (ipc->msg == NULL)
  105. return -ENOMEM;
  106. for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
  107. ipc->msg[i].tx_data = kzalloc(ipc->tx_data_max_size, GFP_KERNEL);
  108. if (ipc->msg[i].tx_data == NULL)
  109. goto free_mem;
  110. ipc->msg[i].rx_data = kzalloc(ipc->rx_data_max_size, GFP_KERNEL);
  111. if (ipc->msg[i].rx_data == NULL) {
  112. kfree(ipc->msg[i].tx_data);
  113. goto free_mem;
  114. }
  115. init_waitqueue_head(&ipc->msg[i].waitq);
  116. list_add(&ipc->msg[i].list, &ipc->empty_list);
  117. }
  118. return 0;
  119. free_mem:
  120. while (i > 0) {
  121. kfree(ipc->msg[i-1].tx_data);
  122. kfree(ipc->msg[i-1].rx_data);
  123. --i;
  124. }
  125. kfree(ipc->msg);
  126. return -ENOMEM;
  127. }
  128. static void ipc_tx_msgs(struct work_struct *work)
  129. {
  130. struct sst_generic_ipc *ipc =
  131. container_of(work, struct sst_generic_ipc, kwork);
  132. struct ipc_message *msg;
  133. spin_lock_irq(&ipc->dsp->spinlock);
  134. while (!list_empty(&ipc->tx_list) && !ipc->pending) {
  135. /* if the DSP is busy, we will TX messages after IRQ.
  136. * also postpone if we are in the middle of processing
  137. * completion irq
  138. */
  139. if (ipc->ops.is_dsp_busy && ipc->ops.is_dsp_busy(ipc->dsp)) {
  140. dev_dbg(ipc->dev, "ipc_tx_msgs dsp busy\n");
  141. break;
  142. }
  143. msg = list_first_entry(&ipc->tx_list, struct ipc_message, list);
  144. list_move(&msg->list, &ipc->rx_list);
  145. if (ipc->ops.tx_msg != NULL)
  146. ipc->ops.tx_msg(ipc, msg);
  147. }
  148. spin_unlock_irq(&ipc->dsp->spinlock);
  149. }
  150. int sst_ipc_tx_message_wait(struct sst_generic_ipc *ipc, u64 header,
  151. void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes)
  152. {
  153. int ret;
  154. /*
  155. * DSP maybe in lower power active state, so
  156. * check if the DSP supports DSP lp On method
  157. * if so invoke that before sending IPC
  158. */
  159. if (ipc->ops.check_dsp_lp_on)
  160. if (ipc->ops.check_dsp_lp_on(ipc->dsp, true))
  161. return -EIO;
  162. ret = ipc_tx_message(ipc, header, tx_data, tx_bytes,
  163. rx_data, rx_bytes, 1);
  164. if (ipc->ops.check_dsp_lp_on)
  165. if (ipc->ops.check_dsp_lp_on(ipc->dsp, false))
  166. return -EIO;
  167. return ret;
  168. }
  169. EXPORT_SYMBOL_GPL(sst_ipc_tx_message_wait);
  170. int sst_ipc_tx_message_nowait(struct sst_generic_ipc *ipc, u64 header,
  171. void *tx_data, size_t tx_bytes)
  172. {
  173. return ipc_tx_message(ipc, header, tx_data, tx_bytes,
  174. NULL, 0, 0);
  175. }
  176. EXPORT_SYMBOL_GPL(sst_ipc_tx_message_nowait);
  177. int sst_ipc_tx_message_nopm(struct sst_generic_ipc *ipc, u64 header,
  178. void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes)
  179. {
  180. return ipc_tx_message(ipc, header, tx_data, tx_bytes,
  181. rx_data, rx_bytes, 1);
  182. }
  183. EXPORT_SYMBOL_GPL(sst_ipc_tx_message_nopm);
  184. struct ipc_message *sst_ipc_reply_find_msg(struct sst_generic_ipc *ipc,
  185. u64 header)
  186. {
  187. struct ipc_message *msg;
  188. u64 mask;
  189. if (ipc->ops.reply_msg_match != NULL)
  190. header = ipc->ops.reply_msg_match(header, &mask);
  191. else
  192. mask = (u64)-1;
  193. if (list_empty(&ipc->rx_list)) {
  194. dev_err(ipc->dev, "error: rx list empty but received 0x%llx\n",
  195. header);
  196. return NULL;
  197. }
  198. list_for_each_entry(msg, &ipc->rx_list, list) {
  199. if ((msg->header & mask) == header)
  200. return msg;
  201. }
  202. return NULL;
  203. }
  204. EXPORT_SYMBOL_GPL(sst_ipc_reply_find_msg);
  205. /* locks held by caller */
  206. void sst_ipc_tx_msg_reply_complete(struct sst_generic_ipc *ipc,
  207. struct ipc_message *msg)
  208. {
  209. msg->complete = true;
  210. if (!msg->wait)
  211. list_add_tail(&msg->list, &ipc->empty_list);
  212. else
  213. wake_up(&msg->waitq);
  214. }
  215. EXPORT_SYMBOL_GPL(sst_ipc_tx_msg_reply_complete);
  216. void sst_ipc_drop_all(struct sst_generic_ipc *ipc)
  217. {
  218. struct ipc_message *msg, *tmp;
  219. unsigned long flags;
  220. int tx_drop_cnt = 0, rx_drop_cnt = 0;
  221. /* drop all TX and Rx messages before we stall + reset DSP */
  222. spin_lock_irqsave(&ipc->dsp->spinlock, flags);
  223. list_for_each_entry_safe(msg, tmp, &ipc->tx_list, list) {
  224. list_move(&msg->list, &ipc->empty_list);
  225. tx_drop_cnt++;
  226. }
  227. list_for_each_entry_safe(msg, tmp, &ipc->rx_list, list) {
  228. list_move(&msg->list, &ipc->empty_list);
  229. rx_drop_cnt++;
  230. }
  231. spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
  232. if (tx_drop_cnt || rx_drop_cnt)
  233. dev_err(ipc->dev, "dropped IPC msg RX=%d, TX=%d\n",
  234. tx_drop_cnt, rx_drop_cnt);
  235. }
  236. EXPORT_SYMBOL_GPL(sst_ipc_drop_all);
  237. int sst_ipc_init(struct sst_generic_ipc *ipc)
  238. {
  239. int ret;
  240. INIT_LIST_HEAD(&ipc->tx_list);
  241. INIT_LIST_HEAD(&ipc->rx_list);
  242. INIT_LIST_HEAD(&ipc->empty_list);
  243. init_waitqueue_head(&ipc->wait_txq);
  244. ret = msg_empty_list_init(ipc);
  245. if (ret < 0)
  246. return -ENOMEM;
  247. INIT_WORK(&ipc->kwork, ipc_tx_msgs);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL_GPL(sst_ipc_init);
  251. void sst_ipc_fini(struct sst_generic_ipc *ipc)
  252. {
  253. int i;
  254. cancel_work_sync(&ipc->kwork);
  255. if (ipc->msg) {
  256. for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
  257. kfree(ipc->msg[i].tx_data);
  258. kfree(ipc->msg[i].rx_data);
  259. }
  260. kfree(ipc->msg);
  261. }
  262. }
  263. EXPORT_SYMBOL_GPL(sst_ipc_fini);
  264. /* Module information */
  265. MODULE_AUTHOR("Jin Yao");
  266. MODULE_DESCRIPTION("Intel SST IPC generic");
  267. MODULE_LICENSE("GPL v2");