sst_pvt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * sst_pvt.c - Intel SST Driver for audio engine
  3. *
  4. * Copyright (C) 2008-14 Intel Corp
  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/kobject.h>
  23. #include <linux/pci.h>
  24. #include <linux/fs.h>
  25. #include <linux/firmware.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/sched.h>
  28. #include <linux/delay.h>
  29. #include <sound/asound.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/soc.h>
  33. #include <sound/compress_driver.h>
  34. #include <asm/platform_sst_audio.h>
  35. #include "../sst-mfld-platform.h"
  36. #include "sst.h"
  37. #include "../../common/sst-dsp.h"
  38. int sst_shim_write(void __iomem *addr, int offset, int value)
  39. {
  40. writel(value, addr + offset);
  41. return 0;
  42. }
  43. u32 sst_shim_read(void __iomem *addr, int offset)
  44. {
  45. return readl(addr + offset);
  46. }
  47. u64 sst_reg_read64(void __iomem *addr, int offset)
  48. {
  49. u64 val = 0;
  50. memcpy_fromio(&val, addr + offset, sizeof(val));
  51. return val;
  52. }
  53. int sst_shim_write64(void __iomem *addr, int offset, u64 value)
  54. {
  55. memcpy_toio(addr + offset, &value, sizeof(value));
  56. return 0;
  57. }
  58. u64 sst_shim_read64(void __iomem *addr, int offset)
  59. {
  60. u64 val = 0;
  61. memcpy_fromio(&val, addr + offset, sizeof(val));
  62. return val;
  63. }
  64. void sst_set_fw_state_locked(
  65. struct intel_sst_drv *sst_drv_ctx, int sst_state)
  66. {
  67. mutex_lock(&sst_drv_ctx->sst_lock);
  68. sst_drv_ctx->sst_state = sst_state;
  69. mutex_unlock(&sst_drv_ctx->sst_lock);
  70. }
  71. /*
  72. * sst_wait_interruptible - wait on event
  73. *
  74. * @sst_drv_ctx: Driver context
  75. * @block: Driver block to wait on
  76. *
  77. * This function waits without a timeout (and is interruptable) for a
  78. * given block event
  79. */
  80. int sst_wait_interruptible(struct intel_sst_drv *sst_drv_ctx,
  81. struct sst_block *block)
  82. {
  83. int retval = 0;
  84. if (!wait_event_interruptible(sst_drv_ctx->wait_queue,
  85. block->condition)) {
  86. /* event wake */
  87. if (block->ret_code < 0) {
  88. dev_err(sst_drv_ctx->dev,
  89. "stream failed %d\n", block->ret_code);
  90. retval = -EBUSY;
  91. } else {
  92. dev_dbg(sst_drv_ctx->dev, "event up\n");
  93. retval = 0;
  94. }
  95. } else {
  96. dev_err(sst_drv_ctx->dev, "signal interrupted\n");
  97. retval = -EINTR;
  98. }
  99. return retval;
  100. }
  101. /*
  102. * sst_wait_timeout - wait on event for timeout
  103. *
  104. * @sst_drv_ctx: Driver context
  105. * @block: Driver block to wait on
  106. *
  107. * This function waits with a timeout value (and is not interruptible) on a
  108. * given block event
  109. */
  110. int sst_wait_timeout(struct intel_sst_drv *sst_drv_ctx, struct sst_block *block)
  111. {
  112. int retval = 0;
  113. /*
  114. * NOTE:
  115. * Observed that FW processes the alloc msg and replies even
  116. * before the alloc thread has finished execution
  117. */
  118. dev_dbg(sst_drv_ctx->dev,
  119. "waiting for condition %x ipc %d drv_id %d\n",
  120. block->condition, block->msg_id, block->drv_id);
  121. if (wait_event_timeout(sst_drv_ctx->wait_queue,
  122. block->condition,
  123. msecs_to_jiffies(SST_BLOCK_TIMEOUT))) {
  124. /* event wake */
  125. dev_dbg(sst_drv_ctx->dev, "Event wake %x\n",
  126. block->condition);
  127. dev_dbg(sst_drv_ctx->dev, "message ret: %d\n",
  128. block->ret_code);
  129. retval = -block->ret_code;
  130. } else {
  131. block->on = false;
  132. dev_err(sst_drv_ctx->dev,
  133. "Wait timed-out condition:%#x, msg_id:%#x fw_state %#x\n",
  134. block->condition, block->msg_id, sst_drv_ctx->sst_state);
  135. sst_drv_ctx->sst_state = SST_RESET;
  136. retval = -EBUSY;
  137. }
  138. return retval;
  139. }
  140. /*
  141. * sst_create_ipc_msg - create a IPC message
  142. *
  143. * @arg: ipc message
  144. * @large: large or short message
  145. *
  146. * this function allocates structures to send a large or short
  147. * message to the firmware
  148. */
  149. int sst_create_ipc_msg(struct ipc_post **arg, bool large)
  150. {
  151. struct ipc_post *msg;
  152. msg = kzalloc(sizeof(struct ipc_post), GFP_ATOMIC);
  153. if (!msg)
  154. return -ENOMEM;
  155. if (large) {
  156. msg->mailbox_data = kzalloc(SST_MAILBOX_SIZE, GFP_ATOMIC);
  157. if (!msg->mailbox_data) {
  158. kfree(msg);
  159. return -ENOMEM;
  160. }
  161. } else {
  162. msg->mailbox_data = NULL;
  163. }
  164. msg->is_large = large;
  165. *arg = msg;
  166. return 0;
  167. }
  168. /*
  169. * sst_create_block_and_ipc_msg - Creates IPC message and sst block
  170. * @arg: passed to sst_create_ipc_message API
  171. * @large: large or short message
  172. * @sst_drv_ctx: sst driver context
  173. * @block: return block allocated
  174. * @msg_id: IPC
  175. * @drv_id: stream id or private id
  176. */
  177. int sst_create_block_and_ipc_msg(struct ipc_post **arg, bool large,
  178. struct intel_sst_drv *sst_drv_ctx, struct sst_block **block,
  179. u32 msg_id, u32 drv_id)
  180. {
  181. int retval = 0;
  182. retval = sst_create_ipc_msg(arg, large);
  183. if (retval)
  184. return retval;
  185. *block = sst_create_block(sst_drv_ctx, msg_id, drv_id);
  186. if (*block == NULL) {
  187. kfree(*arg);
  188. return -ENOMEM;
  189. }
  190. return retval;
  191. }
  192. /*
  193. * sst_clean_stream - clean the stream context
  194. *
  195. * @stream: stream structure
  196. *
  197. * this function resets the stream contexts
  198. * should be called in free
  199. */
  200. void sst_clean_stream(struct stream_info *stream)
  201. {
  202. stream->status = STREAM_UN_INIT;
  203. stream->prev = STREAM_UN_INIT;
  204. mutex_lock(&stream->lock);
  205. stream->cumm_bytes = 0;
  206. mutex_unlock(&stream->lock);
  207. }
  208. int sst_prepare_and_post_msg(struct intel_sst_drv *sst,
  209. int task_id, int ipc_msg, int cmd_id, int pipe_id,
  210. size_t mbox_data_len, const void *mbox_data, void **data,
  211. bool large, bool fill_dsp, bool sync, bool response)
  212. {
  213. struct ipc_post *msg = NULL;
  214. struct ipc_dsp_hdr dsp_hdr;
  215. struct sst_block *block;
  216. int ret = 0, pvt_id;
  217. pvt_id = sst_assign_pvt_id(sst);
  218. if (pvt_id < 0)
  219. return pvt_id;
  220. if (response)
  221. ret = sst_create_block_and_ipc_msg(
  222. &msg, large, sst, &block, ipc_msg, pvt_id);
  223. else
  224. ret = sst_create_ipc_msg(&msg, large);
  225. if (ret < 0) {
  226. test_and_clear_bit(pvt_id, &sst->pvt_id);
  227. return -ENOMEM;
  228. }
  229. dev_dbg(sst->dev, "pvt_id = %d, pipe id = %d, task = %d ipc_msg: %d\n",
  230. pvt_id, pipe_id, task_id, ipc_msg);
  231. sst_fill_header_mrfld(&msg->mrfld_header, ipc_msg,
  232. task_id, large, pvt_id);
  233. msg->mrfld_header.p.header_low_payload = sizeof(dsp_hdr) + mbox_data_len;
  234. msg->mrfld_header.p.header_high.part.res_rqd = !sync;
  235. dev_dbg(sst->dev, "header:%x\n",
  236. msg->mrfld_header.p.header_high.full);
  237. dev_dbg(sst->dev, "response rqd: %x",
  238. msg->mrfld_header.p.header_high.part.res_rqd);
  239. dev_dbg(sst->dev, "msg->mrfld_header.p.header_low_payload:%d",
  240. msg->mrfld_header.p.header_low_payload);
  241. if (fill_dsp) {
  242. sst_fill_header_dsp(&dsp_hdr, cmd_id, pipe_id, mbox_data_len);
  243. memcpy(msg->mailbox_data, &dsp_hdr, sizeof(dsp_hdr));
  244. if (mbox_data_len) {
  245. memcpy(msg->mailbox_data + sizeof(dsp_hdr),
  246. mbox_data, mbox_data_len);
  247. }
  248. }
  249. if (sync)
  250. sst->ops->post_message(sst, msg, true);
  251. else
  252. sst_add_to_dispatch_list_and_post(sst, msg);
  253. if (response) {
  254. ret = sst_wait_timeout(sst, block);
  255. if (ret < 0)
  256. goto out;
  257. if (data && block->data) {
  258. *data = kmemdup(block->data, block->size, GFP_KERNEL);
  259. if (!*data) {
  260. ret = -ENOMEM;
  261. goto out;
  262. }
  263. }
  264. }
  265. out:
  266. if (response)
  267. sst_free_block(sst, block);
  268. test_and_clear_bit(pvt_id, &sst->pvt_id);
  269. return ret;
  270. }
  271. int sst_pm_runtime_put(struct intel_sst_drv *sst_drv)
  272. {
  273. int ret;
  274. pm_runtime_mark_last_busy(sst_drv->dev);
  275. ret = pm_runtime_put_autosuspend(sst_drv->dev);
  276. if (ret < 0)
  277. return ret;
  278. return 0;
  279. }
  280. void sst_fill_header_mrfld(union ipc_header_mrfld *header,
  281. int msg, int task_id, int large, int drv_id)
  282. {
  283. header->full = 0;
  284. header->p.header_high.part.msg_id = msg;
  285. header->p.header_high.part.task_id = task_id;
  286. header->p.header_high.part.large = large;
  287. header->p.header_high.part.drv_id = drv_id;
  288. header->p.header_high.part.done = 0;
  289. header->p.header_high.part.busy = 1;
  290. header->p.header_high.part.res_rqd = 1;
  291. }
  292. void sst_fill_header_dsp(struct ipc_dsp_hdr *dsp, int msg,
  293. int pipe_id, int len)
  294. {
  295. dsp->cmd_id = msg;
  296. dsp->mod_index_id = 0xff;
  297. dsp->pipe_id = pipe_id;
  298. dsp->length = len;
  299. dsp->mod_id = 0;
  300. }
  301. #define SST_MAX_BLOCKS 15
  302. /*
  303. * sst_assign_pvt_id - assign a pvt id for stream
  304. *
  305. * @sst_drv_ctx : driver context
  306. *
  307. * this function assigns a private id for calls that dont have stream
  308. * context yet, should be called with lock held
  309. * uses bits for the id, and finds first free bits and assigns that
  310. */
  311. int sst_assign_pvt_id(struct intel_sst_drv *drv)
  312. {
  313. int local;
  314. spin_lock(&drv->block_lock);
  315. /* find first zero index from lsb */
  316. local = ffz(drv->pvt_id);
  317. dev_dbg(drv->dev, "pvt_id assigned --> %d\n", local);
  318. if (local >= SST_MAX_BLOCKS){
  319. spin_unlock(&drv->block_lock);
  320. dev_err(drv->dev, "PVT _ID error: no free id blocks ");
  321. return -EINVAL;
  322. }
  323. /* toggle the index */
  324. change_bit(local, &drv->pvt_id);
  325. spin_unlock(&drv->block_lock);
  326. return local;
  327. }
  328. void sst_init_stream(struct stream_info *stream,
  329. int codec, int sst_id, int ops, u8 slot)
  330. {
  331. stream->status = STREAM_INIT;
  332. stream->prev = STREAM_UN_INIT;
  333. stream->ops = ops;
  334. }
  335. int sst_validate_strid(
  336. struct intel_sst_drv *sst_drv_ctx, int str_id)
  337. {
  338. if (str_id <= 0 || str_id > sst_drv_ctx->info.max_streams) {
  339. dev_err(sst_drv_ctx->dev,
  340. "SST ERR: invalid stream id : %d, max %d\n",
  341. str_id, sst_drv_ctx->info.max_streams);
  342. return -EINVAL;
  343. }
  344. return 0;
  345. }
  346. struct stream_info *get_stream_info(
  347. struct intel_sst_drv *sst_drv_ctx, int str_id)
  348. {
  349. if (sst_validate_strid(sst_drv_ctx, str_id))
  350. return NULL;
  351. return &sst_drv_ctx->streams[str_id];
  352. }
  353. int get_stream_id_mrfld(struct intel_sst_drv *sst_drv_ctx,
  354. u32 pipe_id)
  355. {
  356. int i;
  357. for (i = 1; i <= sst_drv_ctx->info.max_streams; i++)
  358. if (pipe_id == sst_drv_ctx->streams[i].pipe_id)
  359. return i;
  360. dev_dbg(sst_drv_ctx->dev, "no such pipe_id(%u)", pipe_id);
  361. return -1;
  362. }
  363. u32 relocate_imr_addr_mrfld(u32 base_addr)
  364. {
  365. /* Get the difference from 512MB aligned base addr */
  366. /* relocate the base */
  367. base_addr = MRFLD_FW_VIRTUAL_BASE + (base_addr % (512 * 1024 * 1024));
  368. return base_addr;
  369. }
  370. EXPORT_SYMBOL_GPL(relocate_imr_addr_mrfld);
  371. void sst_add_to_dispatch_list_and_post(struct intel_sst_drv *sst,
  372. struct ipc_post *msg)
  373. {
  374. unsigned long irq_flags;
  375. spin_lock_irqsave(&sst->ipc_spin_lock, irq_flags);
  376. list_add_tail(&msg->node, &sst->ipc_dispatch_list);
  377. spin_unlock_irqrestore(&sst->ipc_spin_lock, irq_flags);
  378. sst->ops->post_message(sst, NULL, false);
  379. }