sst_ipc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * sst_ipc.c - Intel SST Driver for audio engine
  3. *
  4. * Copyright (C) 2008-14 Intel Corporation
  5. * Authors: Vinod Koul <vinod.koul@intel.com>
  6. * Harsha Priya <priya.harsha@intel.com>
  7. * Dharageswari R <dharageswari.r@intel.com>
  8. * KP Jeeja <jeeja.kp@intel.com>
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. */
  22. #include <linux/pci.h>
  23. #include <linux/firmware.h>
  24. #include <linux/sched.h>
  25. #include <linux/delay.h>
  26. #include <linux/pm_runtime.h>
  27. #include <sound/core.h>
  28. #include <sound/pcm.h>
  29. #include <sound/soc.h>
  30. #include <sound/compress_driver.h>
  31. #include <asm/intel-mid.h>
  32. #include <asm/platform_sst_audio.h>
  33. #include "../sst-mfld-platform.h"
  34. #include "sst.h"
  35. #include "../../common/sst-dsp.h"
  36. struct sst_block *sst_create_block(struct intel_sst_drv *ctx,
  37. u32 msg_id, u32 drv_id)
  38. {
  39. struct sst_block *msg = NULL;
  40. dev_dbg(ctx->dev, "Enter\n");
  41. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  42. if (!msg)
  43. return NULL;
  44. msg->condition = false;
  45. msg->on = true;
  46. msg->msg_id = msg_id;
  47. msg->drv_id = drv_id;
  48. spin_lock_bh(&ctx->block_lock);
  49. list_add_tail(&msg->node, &ctx->block_list);
  50. spin_unlock_bh(&ctx->block_lock);
  51. return msg;
  52. }
  53. /*
  54. * while handling the interrupts, we need to check for message status and
  55. * then if we are blocking for a message
  56. *
  57. * here we are unblocking the blocked ones, this is based on id we have
  58. * passed and search that for block threads.
  59. * We will not find block in two cases
  60. * a) when its small message and block in not there, so silently ignore
  61. * them
  62. * b) when we are actually not able to find the block (bug perhaps)
  63. *
  64. * Since we have bit of small messages we can spam kernel log with err
  65. * print on above so need to keep as debug prints which should be enabled
  66. * via dynamic debug while debugging IPC issues
  67. */
  68. int sst_wake_up_block(struct intel_sst_drv *ctx, int result,
  69. u32 drv_id, u32 ipc, void *data, u32 size)
  70. {
  71. struct sst_block *block = NULL;
  72. dev_dbg(ctx->dev, "Enter\n");
  73. spin_lock_bh(&ctx->block_lock);
  74. list_for_each_entry(block, &ctx->block_list, node) {
  75. dev_dbg(ctx->dev, "Block ipc %d, drv_id %d\n", block->msg_id,
  76. block->drv_id);
  77. if (block->msg_id == ipc && block->drv_id == drv_id) {
  78. dev_dbg(ctx->dev, "free up the block\n");
  79. block->ret_code = result;
  80. block->data = data;
  81. block->size = size;
  82. block->condition = true;
  83. spin_unlock_bh(&ctx->block_lock);
  84. wake_up(&ctx->wait_queue);
  85. return 0;
  86. }
  87. }
  88. spin_unlock_bh(&ctx->block_lock);
  89. dev_dbg(ctx->dev,
  90. "Block not found or a response received for a short msg for ipc %d, drv_id %d\n",
  91. ipc, drv_id);
  92. return -EINVAL;
  93. }
  94. int sst_free_block(struct intel_sst_drv *ctx, struct sst_block *freed)
  95. {
  96. struct sst_block *block = NULL, *__block;
  97. dev_dbg(ctx->dev, "Enter\n");
  98. spin_lock_bh(&ctx->block_lock);
  99. list_for_each_entry_safe(block, __block, &ctx->block_list, node) {
  100. if (block == freed) {
  101. pr_debug("pvt_id freed --> %d\n", freed->drv_id);
  102. /* toggle the index position of pvt_id */
  103. list_del(&freed->node);
  104. spin_unlock_bh(&ctx->block_lock);
  105. kfree(freed->data);
  106. freed->data = NULL;
  107. kfree(freed);
  108. return 0;
  109. }
  110. }
  111. spin_unlock_bh(&ctx->block_lock);
  112. dev_err(ctx->dev, "block is already freed!!!\n");
  113. return -EINVAL;
  114. }
  115. int sst_post_message_mrfld(struct intel_sst_drv *sst_drv_ctx,
  116. struct ipc_post *ipc_msg, bool sync)
  117. {
  118. struct ipc_post *msg = ipc_msg;
  119. union ipc_header_mrfld header;
  120. unsigned int loop_count = 0;
  121. int retval = 0;
  122. unsigned long irq_flags;
  123. dev_dbg(sst_drv_ctx->dev, "Enter: sync: %d\n", sync);
  124. spin_lock_irqsave(&sst_drv_ctx->ipc_spin_lock, irq_flags);
  125. header.full = sst_shim_read64(sst_drv_ctx->shim, SST_IPCX);
  126. if (sync) {
  127. while (header.p.header_high.part.busy) {
  128. if (loop_count > 25) {
  129. dev_err(sst_drv_ctx->dev,
  130. "sst: Busy wait failed, cant send this msg\n");
  131. retval = -EBUSY;
  132. goto out;
  133. }
  134. cpu_relax();
  135. loop_count++;
  136. header.full = sst_shim_read64(sst_drv_ctx->shim, SST_IPCX);
  137. }
  138. } else {
  139. if (list_empty(&sst_drv_ctx->ipc_dispatch_list)) {
  140. /* queue is empty, nothing to send */
  141. spin_unlock_irqrestore(&sst_drv_ctx->ipc_spin_lock, irq_flags);
  142. dev_dbg(sst_drv_ctx->dev,
  143. "Empty msg queue... NO Action\n");
  144. return 0;
  145. }
  146. if (header.p.header_high.part.busy) {
  147. spin_unlock_irqrestore(&sst_drv_ctx->ipc_spin_lock, irq_flags);
  148. dev_dbg(sst_drv_ctx->dev, "Busy not free... post later\n");
  149. return 0;
  150. }
  151. /* copy msg from list */
  152. msg = list_entry(sst_drv_ctx->ipc_dispatch_list.next,
  153. struct ipc_post, node);
  154. list_del(&msg->node);
  155. }
  156. dev_dbg(sst_drv_ctx->dev, "sst: Post message: header = %x\n",
  157. msg->mrfld_header.p.header_high.full);
  158. dev_dbg(sst_drv_ctx->dev, "sst: size = 0x%x\n",
  159. msg->mrfld_header.p.header_low_payload);
  160. if (msg->mrfld_header.p.header_high.part.large)
  161. memcpy_toio(sst_drv_ctx->mailbox + SST_MAILBOX_SEND,
  162. msg->mailbox_data,
  163. msg->mrfld_header.p.header_low_payload);
  164. sst_shim_write64(sst_drv_ctx->shim, SST_IPCX, msg->mrfld_header.full);
  165. out:
  166. spin_unlock_irqrestore(&sst_drv_ctx->ipc_spin_lock, irq_flags);
  167. kfree(msg->mailbox_data);
  168. kfree(msg);
  169. return retval;
  170. }
  171. void intel_sst_clear_intr_mrfld(struct intel_sst_drv *sst_drv_ctx)
  172. {
  173. union interrupt_reg_mrfld isr;
  174. union interrupt_reg_mrfld imr;
  175. union ipc_header_mrfld clear_ipc;
  176. unsigned long irq_flags;
  177. spin_lock_irqsave(&sst_drv_ctx->ipc_spin_lock, irq_flags);
  178. imr.full = sst_shim_read64(sst_drv_ctx->shim, SST_IMRX);
  179. isr.full = sst_shim_read64(sst_drv_ctx->shim, SST_ISRX);
  180. /* write 1 to clear*/
  181. isr.part.busy_interrupt = 1;
  182. sst_shim_write64(sst_drv_ctx->shim, SST_ISRX, isr.full);
  183. /* Set IA done bit */
  184. clear_ipc.full = sst_shim_read64(sst_drv_ctx->shim, SST_IPCD);
  185. clear_ipc.p.header_high.part.busy = 0;
  186. clear_ipc.p.header_high.part.done = 1;
  187. clear_ipc.p.header_low_payload = IPC_ACK_SUCCESS;
  188. sst_shim_write64(sst_drv_ctx->shim, SST_IPCD, clear_ipc.full);
  189. /* un mask busy interrupt */
  190. imr.part.busy_interrupt = 0;
  191. sst_shim_write64(sst_drv_ctx->shim, SST_IMRX, imr.full);
  192. spin_unlock_irqrestore(&sst_drv_ctx->ipc_spin_lock, irq_flags);
  193. }
  194. /*
  195. * process_fw_init - process the FW init msg
  196. *
  197. * @msg: IPC message mailbox data from FW
  198. *
  199. * This function processes the FW init msg from FW
  200. * marks FW state and prints debug info of loaded FW
  201. */
  202. static void process_fw_init(struct intel_sst_drv *sst_drv_ctx,
  203. void *msg)
  204. {
  205. struct ipc_header_fw_init *init =
  206. (struct ipc_header_fw_init *)msg;
  207. int retval = 0;
  208. dev_dbg(sst_drv_ctx->dev, "*** FW Init msg came***\n");
  209. if (init->result) {
  210. sst_set_fw_state_locked(sst_drv_ctx, SST_RESET);
  211. dev_err(sst_drv_ctx->dev, "FW Init failed, Error %x\n",
  212. init->result);
  213. retval = init->result;
  214. goto ret;
  215. }
  216. ret:
  217. sst_wake_up_block(sst_drv_ctx, retval, FW_DWNL_ID, 0 , NULL, 0);
  218. }
  219. static void process_fw_async_msg(struct intel_sst_drv *sst_drv_ctx,
  220. struct ipc_post *msg)
  221. {
  222. u32 msg_id;
  223. int str_id;
  224. u32 data_size, i;
  225. void *data_offset;
  226. struct stream_info *stream;
  227. union ipc_header_high msg_high;
  228. u32 msg_low, pipe_id;
  229. msg_high = msg->mrfld_header.p.header_high;
  230. msg_low = msg->mrfld_header.p.header_low_payload;
  231. msg_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->cmd_id;
  232. data_offset = (msg->mailbox_data + sizeof(struct ipc_dsp_hdr));
  233. data_size = msg_low - (sizeof(struct ipc_dsp_hdr));
  234. switch (msg_id) {
  235. case IPC_SST_PERIOD_ELAPSED_MRFLD:
  236. pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
  237. str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
  238. if (str_id > 0) {
  239. dev_dbg(sst_drv_ctx->dev,
  240. "Period elapsed rcvd for pipe id 0x%x\n",
  241. pipe_id);
  242. stream = &sst_drv_ctx->streams[str_id];
  243. /* If stream is dropped, skip processing this message*/
  244. if (stream->status == STREAM_INIT)
  245. break;
  246. if (stream->period_elapsed)
  247. stream->period_elapsed(stream->pcm_substream);
  248. if (stream->compr_cb)
  249. stream->compr_cb(stream->compr_cb_param);
  250. }
  251. break;
  252. case IPC_IA_DRAIN_STREAM_MRFLD:
  253. pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
  254. str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
  255. if (str_id > 0) {
  256. stream = &sst_drv_ctx->streams[str_id];
  257. if (stream->drain_notify)
  258. stream->drain_notify(stream->drain_cb_param);
  259. }
  260. break;
  261. case IPC_IA_FW_ASYNC_ERR_MRFLD:
  262. dev_err(sst_drv_ctx->dev, "FW sent async error msg:\n");
  263. for (i = 0; i < (data_size/4); i++)
  264. print_hex_dump(KERN_DEBUG, NULL, DUMP_PREFIX_NONE,
  265. 16, 4, data_offset, data_size, false);
  266. break;
  267. case IPC_IA_FW_INIT_CMPLT_MRFLD:
  268. process_fw_init(sst_drv_ctx, data_offset);
  269. break;
  270. case IPC_IA_BUF_UNDER_RUN_MRFLD:
  271. pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
  272. str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
  273. if (str_id > 0)
  274. dev_err(sst_drv_ctx->dev,
  275. "Buffer under-run for pipe:%#x str_id:%d\n",
  276. pipe_id, str_id);
  277. break;
  278. default:
  279. dev_err(sst_drv_ctx->dev,
  280. "Unrecognized async msg from FW msg_id %#x\n", msg_id);
  281. }
  282. }
  283. void sst_process_reply_mrfld(struct intel_sst_drv *sst_drv_ctx,
  284. struct ipc_post *msg)
  285. {
  286. unsigned int drv_id;
  287. void *data;
  288. union ipc_header_high msg_high;
  289. u32 msg_low;
  290. struct ipc_dsp_hdr *dsp_hdr;
  291. msg_high = msg->mrfld_header.p.header_high;
  292. msg_low = msg->mrfld_header.p.header_low_payload;
  293. dev_dbg(sst_drv_ctx->dev, "IPC process message header %x payload %x\n",
  294. msg->mrfld_header.p.header_high.full,
  295. msg->mrfld_header.p.header_low_payload);
  296. drv_id = msg_high.part.drv_id;
  297. /* Check for async messages first */
  298. if (drv_id == SST_ASYNC_DRV_ID) {
  299. /*FW sent async large message*/
  300. process_fw_async_msg(sst_drv_ctx, msg);
  301. return;
  302. }
  303. /* FW sent short error response for an IPC */
  304. if (msg_high.part.result && drv_id && !msg_high.part.large) {
  305. /* 32-bit FW error code in msg_low */
  306. dev_err(sst_drv_ctx->dev, "FW sent error response 0x%x", msg_low);
  307. sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
  308. msg_high.part.drv_id,
  309. msg_high.part.msg_id, NULL, 0);
  310. return;
  311. }
  312. /*
  313. * Process all valid responses
  314. * if it is a large message, the payload contains the size to
  315. * copy from mailbox
  316. **/
  317. if (msg_high.part.large) {
  318. data = kmemdup((void *)msg->mailbox_data, msg_low, GFP_KERNEL);
  319. if (!data)
  320. return;
  321. /* Copy command id so that we can use to put sst to reset */
  322. dsp_hdr = (struct ipc_dsp_hdr *)data;
  323. dev_dbg(sst_drv_ctx->dev, "cmd_id %d\n", dsp_hdr->cmd_id);
  324. if (sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
  325. msg_high.part.drv_id,
  326. msg_high.part.msg_id, data, msg_low))
  327. kfree(data);
  328. } else {
  329. sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
  330. msg_high.part.drv_id,
  331. msg_high.part.msg_id, NULL, 0);
  332. }
  333. }