sst_ipc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. if (memcmp(&sst_drv_ctx->fw_version, &init->fw_version,
  217. sizeof(init->fw_version)))
  218. dev_info(sst_drv_ctx->dev, "FW Version %02x.%02x.%02x.%02x\n",
  219. init->fw_version.type, init->fw_version.major,
  220. init->fw_version.minor, init->fw_version.build);
  221. dev_dbg(sst_drv_ctx->dev, "Build date %s Time %s\n",
  222. init->build_info.date, init->build_info.time);
  223. /* Save FW version */
  224. sst_drv_ctx->fw_version.type = init->fw_version.type;
  225. sst_drv_ctx->fw_version.major = init->fw_version.major;
  226. sst_drv_ctx->fw_version.minor = init->fw_version.minor;
  227. sst_drv_ctx->fw_version.build = init->fw_version.build;
  228. ret:
  229. sst_wake_up_block(sst_drv_ctx, retval, FW_DWNL_ID, 0 , NULL, 0);
  230. }
  231. static void process_fw_async_msg(struct intel_sst_drv *sst_drv_ctx,
  232. struct ipc_post *msg)
  233. {
  234. u32 msg_id;
  235. int str_id;
  236. u32 data_size, i;
  237. void *data_offset;
  238. struct stream_info *stream;
  239. u32 msg_low, pipe_id;
  240. msg_low = msg->mrfld_header.p.header_low_payload;
  241. msg_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->cmd_id;
  242. data_offset = (msg->mailbox_data + sizeof(struct ipc_dsp_hdr));
  243. data_size = msg_low - (sizeof(struct ipc_dsp_hdr));
  244. switch (msg_id) {
  245. case IPC_SST_PERIOD_ELAPSED_MRFLD:
  246. pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
  247. str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
  248. if (str_id > 0) {
  249. dev_dbg(sst_drv_ctx->dev,
  250. "Period elapsed rcvd for pipe id 0x%x\n",
  251. pipe_id);
  252. stream = &sst_drv_ctx->streams[str_id];
  253. /* If stream is dropped, skip processing this message*/
  254. if (stream->status == STREAM_INIT)
  255. break;
  256. if (stream->period_elapsed)
  257. stream->period_elapsed(stream->pcm_substream);
  258. if (stream->compr_cb)
  259. stream->compr_cb(stream->compr_cb_param);
  260. }
  261. break;
  262. case IPC_IA_DRAIN_STREAM_MRFLD:
  263. pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
  264. str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
  265. if (str_id > 0) {
  266. stream = &sst_drv_ctx->streams[str_id];
  267. if (stream->drain_notify)
  268. stream->drain_notify(stream->drain_cb_param);
  269. }
  270. break;
  271. case IPC_IA_FW_ASYNC_ERR_MRFLD:
  272. dev_err(sst_drv_ctx->dev, "FW sent async error msg:\n");
  273. for (i = 0; i < (data_size/4); i++)
  274. print_hex_dump(KERN_DEBUG, NULL, DUMP_PREFIX_NONE,
  275. 16, 4, data_offset, data_size, false);
  276. break;
  277. case IPC_IA_FW_INIT_CMPLT_MRFLD:
  278. process_fw_init(sst_drv_ctx, data_offset);
  279. break;
  280. case IPC_IA_BUF_UNDER_RUN_MRFLD:
  281. pipe_id = ((struct ipc_dsp_hdr *)msg->mailbox_data)->pipe_id;
  282. str_id = get_stream_id_mrfld(sst_drv_ctx, pipe_id);
  283. if (str_id > 0)
  284. dev_err(sst_drv_ctx->dev,
  285. "Buffer under-run for pipe:%#x str_id:%d\n",
  286. pipe_id, str_id);
  287. break;
  288. default:
  289. dev_err(sst_drv_ctx->dev,
  290. "Unrecognized async msg from FW msg_id %#x\n", msg_id);
  291. }
  292. }
  293. void sst_process_reply_mrfld(struct intel_sst_drv *sst_drv_ctx,
  294. struct ipc_post *msg)
  295. {
  296. unsigned int drv_id;
  297. void *data;
  298. union ipc_header_high msg_high;
  299. u32 msg_low;
  300. struct ipc_dsp_hdr *dsp_hdr;
  301. msg_high = msg->mrfld_header.p.header_high;
  302. msg_low = msg->mrfld_header.p.header_low_payload;
  303. dev_dbg(sst_drv_ctx->dev, "IPC process message header %x payload %x\n",
  304. msg->mrfld_header.p.header_high.full,
  305. msg->mrfld_header.p.header_low_payload);
  306. drv_id = msg_high.part.drv_id;
  307. /* Check for async messages first */
  308. if (drv_id == SST_ASYNC_DRV_ID) {
  309. /*FW sent async large message*/
  310. process_fw_async_msg(sst_drv_ctx, msg);
  311. return;
  312. }
  313. /* FW sent short error response for an IPC */
  314. if (msg_high.part.result && drv_id && !msg_high.part.large) {
  315. /* 32-bit FW error code in msg_low */
  316. dev_err(sst_drv_ctx->dev, "FW sent error response 0x%x", msg_low);
  317. sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
  318. msg_high.part.drv_id,
  319. msg_high.part.msg_id, NULL, 0);
  320. return;
  321. }
  322. /*
  323. * Process all valid responses
  324. * if it is a large message, the payload contains the size to
  325. * copy from mailbox
  326. **/
  327. if (msg_high.part.large) {
  328. data = kmemdup((void *)msg->mailbox_data, msg_low, GFP_KERNEL);
  329. if (!data)
  330. return;
  331. /* Copy command id so that we can use to put sst to reset */
  332. dsp_hdr = (struct ipc_dsp_hdr *)data;
  333. dev_dbg(sst_drv_ctx->dev, "cmd_id %d\n", dsp_hdr->cmd_id);
  334. if (sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
  335. msg_high.part.drv_id,
  336. msg_high.part.msg_id, data, msg_low))
  337. kfree(data);
  338. } else {
  339. sst_wake_up_block(sst_drv_ctx, msg_high.part.result,
  340. msg_high.part.drv_id,
  341. msg_high.part.msg_id, NULL, 0);
  342. }
  343. }