sst-baytrail-ipc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Intel Baytrail SST IPC Support
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/device.h>
  18. #include <linux/wait.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/export.h>
  22. #include <linux/slab.h>
  23. #include <linux/delay.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/kthread.h>
  26. #include <linux/firmware.h>
  27. #include <linux/io.h>
  28. #include <asm/div64.h>
  29. #include "sst-baytrail-ipc.h"
  30. #include "../common/sst-dsp.h"
  31. #include "../common/sst-dsp-priv.h"
  32. #include "../common/sst-ipc.h"
  33. /* IPC message timeout */
  34. #define IPC_TIMEOUT_MSECS 300
  35. #define IPC_BOOT_MSECS 200
  36. #define IPC_EMPTY_LIST_SIZE 8
  37. /* IPC header bits */
  38. #define IPC_HEADER_MSG_ID_MASK 0xff
  39. #define IPC_HEADER_MSG_ID(x) ((x) & IPC_HEADER_MSG_ID_MASK)
  40. #define IPC_HEADER_STR_ID_SHIFT 8
  41. #define IPC_HEADER_STR_ID_MASK 0x1f
  42. #define IPC_HEADER_STR_ID(x) (((x) & 0x1f) << IPC_HEADER_STR_ID_SHIFT)
  43. #define IPC_HEADER_LARGE_SHIFT 13
  44. #define IPC_HEADER_LARGE(x) (((x) & 0x1) << IPC_HEADER_LARGE_SHIFT)
  45. #define IPC_HEADER_DATA_SHIFT 16
  46. #define IPC_HEADER_DATA_MASK 0x3fff
  47. #define IPC_HEADER_DATA(x) (((x) & 0x3fff) << IPC_HEADER_DATA_SHIFT)
  48. /* mask for differentiating between notification and reply message */
  49. #define IPC_NOTIFICATION (0x1 << 7)
  50. /* I2L Stream config/control msgs */
  51. #define IPC_IA_ALLOC_STREAM 0x20
  52. #define IPC_IA_FREE_STREAM 0x21
  53. #define IPC_IA_PAUSE_STREAM 0x24
  54. #define IPC_IA_RESUME_STREAM 0x25
  55. #define IPC_IA_DROP_STREAM 0x26
  56. #define IPC_IA_START_STREAM 0x30
  57. /* notification messages */
  58. #define IPC_IA_FW_INIT_CMPLT 0x81
  59. #define IPC_SST_PERIOD_ELAPSED 0x97
  60. /* IPC messages between host and ADSP */
  61. struct sst_byt_address_info {
  62. u32 addr;
  63. u32 size;
  64. } __packed;
  65. struct sst_byt_str_type {
  66. u8 codec_type;
  67. u8 str_type;
  68. u8 operation;
  69. u8 protected_str;
  70. u8 time_slots;
  71. u8 reserved;
  72. u16 result;
  73. } __packed;
  74. struct sst_byt_pcm_params {
  75. u8 num_chan;
  76. u8 pcm_wd_sz;
  77. u8 use_offload_path;
  78. u8 reserved;
  79. u32 sfreq;
  80. u8 channel_map[8];
  81. } __packed;
  82. struct sst_byt_frames_info {
  83. u16 num_entries;
  84. u16 rsrvd;
  85. u32 frag_size;
  86. struct sst_byt_address_info ring_buf_info[8];
  87. } __packed;
  88. struct sst_byt_alloc_params {
  89. struct sst_byt_str_type str_type;
  90. struct sst_byt_pcm_params pcm_params;
  91. struct sst_byt_frames_info frame_info;
  92. } __packed;
  93. struct sst_byt_alloc_response {
  94. struct sst_byt_str_type str_type;
  95. u8 reserved[88];
  96. } __packed;
  97. struct sst_byt_start_stream_params {
  98. u32 byte_offset;
  99. } __packed;
  100. struct sst_byt_tstamp {
  101. u64 ring_buffer_counter;
  102. u64 hardware_counter;
  103. u64 frames_decoded;
  104. u64 bytes_decoded;
  105. u64 bytes_copied;
  106. u32 sampling_frequency;
  107. u32 channel_peak[8];
  108. } __packed;
  109. struct sst_byt_fw_version {
  110. u8 build;
  111. u8 minor;
  112. u8 major;
  113. u8 type;
  114. } __packed;
  115. struct sst_byt_fw_build_info {
  116. u8 date[16];
  117. u8 time[16];
  118. } __packed;
  119. struct sst_byt_fw_init {
  120. struct sst_byt_fw_version fw_version;
  121. struct sst_byt_fw_build_info build_info;
  122. u16 result;
  123. u8 module_id;
  124. u8 debug_info;
  125. } __packed;
  126. struct sst_byt_stream;
  127. struct sst_byt;
  128. /* stream infomation */
  129. struct sst_byt_stream {
  130. struct list_head node;
  131. /* configuration */
  132. struct sst_byt_alloc_params request;
  133. struct sst_byt_alloc_response reply;
  134. /* runtime info */
  135. struct sst_byt *byt;
  136. int str_id;
  137. bool commited;
  138. bool running;
  139. /* driver callback */
  140. u32 (*notify_position)(struct sst_byt_stream *stream, void *data);
  141. void *pdata;
  142. };
  143. /* SST Baytrail IPC data */
  144. struct sst_byt {
  145. struct device *dev;
  146. struct sst_dsp *dsp;
  147. /* stream */
  148. struct list_head stream_list;
  149. /* boot */
  150. wait_queue_head_t boot_wait;
  151. bool boot_complete;
  152. struct sst_fw *fw;
  153. /* IPC messaging */
  154. struct sst_generic_ipc ipc;
  155. };
  156. static inline u64 sst_byt_header(int msg_id, int data, bool large, int str_id)
  157. {
  158. u64 header;
  159. header = IPC_HEADER_MSG_ID(msg_id) |
  160. IPC_HEADER_STR_ID(str_id) |
  161. IPC_HEADER_LARGE(large) |
  162. IPC_HEADER_DATA(data) |
  163. SST_BYT_IPCX_BUSY;
  164. return header;
  165. }
  166. static inline u16 sst_byt_header_msg_id(u64 header)
  167. {
  168. return header & IPC_HEADER_MSG_ID_MASK;
  169. }
  170. static inline u8 sst_byt_header_str_id(u64 header)
  171. {
  172. return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK;
  173. }
  174. static inline u16 sst_byt_header_data(u64 header)
  175. {
  176. return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK;
  177. }
  178. static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt,
  179. int stream_id)
  180. {
  181. struct sst_byt_stream *stream;
  182. list_for_each_entry(stream, &byt->stream_list, node) {
  183. if (stream->str_id == stream_id)
  184. return stream;
  185. }
  186. return NULL;
  187. }
  188. static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg)
  189. {
  190. struct sst_byt_stream *stream;
  191. u64 header = msg->header;
  192. u8 stream_id = sst_byt_header_str_id(header);
  193. u8 stream_msg = sst_byt_header_msg_id(header);
  194. stream = sst_byt_get_stream(byt, stream_id);
  195. if (stream == NULL)
  196. return;
  197. switch (stream_msg) {
  198. case IPC_IA_DROP_STREAM:
  199. case IPC_IA_PAUSE_STREAM:
  200. case IPC_IA_FREE_STREAM:
  201. stream->running = false;
  202. break;
  203. case IPC_IA_START_STREAM:
  204. case IPC_IA_RESUME_STREAM:
  205. stream->running = true;
  206. break;
  207. }
  208. }
  209. static int sst_byt_process_reply(struct sst_byt *byt, u64 header)
  210. {
  211. struct ipc_message *msg;
  212. msg = sst_ipc_reply_find_msg(&byt->ipc, header);
  213. if (msg == NULL)
  214. return 1;
  215. if (header & IPC_HEADER_LARGE(true)) {
  216. msg->rx_size = sst_byt_header_data(header);
  217. sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size);
  218. }
  219. /* update any stream states */
  220. sst_byt_stream_update(byt, msg);
  221. list_del(&msg->list);
  222. /* wake up */
  223. sst_ipc_tx_msg_reply_complete(&byt->ipc, msg);
  224. return 1;
  225. }
  226. static void sst_byt_fw_ready(struct sst_byt *byt, u64 header)
  227. {
  228. dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header);
  229. byt->boot_complete = true;
  230. wake_up(&byt->boot_wait);
  231. }
  232. static int sst_byt_process_notification(struct sst_byt *byt,
  233. unsigned long *flags)
  234. {
  235. struct sst_dsp *sst = byt->dsp;
  236. struct sst_byt_stream *stream;
  237. u64 header;
  238. u8 msg_id, stream_id;
  239. int handled = 1;
  240. header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
  241. msg_id = sst_byt_header_msg_id(header);
  242. switch (msg_id) {
  243. case IPC_SST_PERIOD_ELAPSED:
  244. stream_id = sst_byt_header_str_id(header);
  245. stream = sst_byt_get_stream(byt, stream_id);
  246. if (stream && stream->running && stream->notify_position) {
  247. spin_unlock_irqrestore(&sst->spinlock, *flags);
  248. stream->notify_position(stream, stream->pdata);
  249. spin_lock_irqsave(&sst->spinlock, *flags);
  250. }
  251. break;
  252. case IPC_IA_FW_INIT_CMPLT:
  253. sst_byt_fw_ready(byt, header);
  254. break;
  255. }
  256. return handled;
  257. }
  258. static irqreturn_t sst_byt_irq_thread(int irq, void *context)
  259. {
  260. struct sst_dsp *sst = (struct sst_dsp *) context;
  261. struct sst_byt *byt = sst_dsp_get_thread_context(sst);
  262. struct sst_generic_ipc *ipc = &byt->ipc;
  263. u64 header;
  264. unsigned long flags;
  265. spin_lock_irqsave(&sst->spinlock, flags);
  266. header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
  267. if (header & SST_BYT_IPCD_BUSY) {
  268. if (header & IPC_NOTIFICATION) {
  269. /* message from ADSP */
  270. sst_byt_process_notification(byt, &flags);
  271. } else {
  272. /* reply from ADSP */
  273. sst_byt_process_reply(byt, header);
  274. }
  275. /*
  276. * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
  277. * processed the message and can accept new. Clear data part
  278. * of the header
  279. */
  280. sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD,
  281. SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY |
  282. IPC_HEADER_DATA(IPC_HEADER_DATA_MASK),
  283. SST_BYT_IPCD_DONE);
  284. /* unmask message request interrupts */
  285. sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX,
  286. SST_BYT_IMRX_REQUEST, 0);
  287. }
  288. spin_unlock_irqrestore(&sst->spinlock, flags);
  289. /* continue to send any remaining messages... */
  290. queue_kthread_work(&ipc->kworker, &ipc->kwork);
  291. return IRQ_HANDLED;
  292. }
  293. /* stream API */
  294. struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id,
  295. u32 (*notify_position)(struct sst_byt_stream *stream, void *data),
  296. void *data)
  297. {
  298. struct sst_byt_stream *stream;
  299. struct sst_dsp *sst = byt->dsp;
  300. unsigned long flags;
  301. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  302. if (stream == NULL)
  303. return NULL;
  304. spin_lock_irqsave(&sst->spinlock, flags);
  305. list_add(&stream->node, &byt->stream_list);
  306. stream->notify_position = notify_position;
  307. stream->pdata = data;
  308. stream->byt = byt;
  309. stream->str_id = id;
  310. spin_unlock_irqrestore(&sst->spinlock, flags);
  311. return stream;
  312. }
  313. int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream,
  314. int bits)
  315. {
  316. stream->request.pcm_params.pcm_wd_sz = bits;
  317. return 0;
  318. }
  319. int sst_byt_stream_set_channels(struct sst_byt *byt,
  320. struct sst_byt_stream *stream, u8 channels)
  321. {
  322. stream->request.pcm_params.num_chan = channels;
  323. return 0;
  324. }
  325. int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream,
  326. unsigned int rate)
  327. {
  328. stream->request.pcm_params.sfreq = rate;
  329. return 0;
  330. }
  331. /* stream sonfiguration */
  332. int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream,
  333. int codec_type, int stream_type, int operation)
  334. {
  335. stream->request.str_type.codec_type = codec_type;
  336. stream->request.str_type.str_type = stream_type;
  337. stream->request.str_type.operation = operation;
  338. stream->request.str_type.time_slots = 0xc;
  339. return 0;
  340. }
  341. int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream,
  342. uint32_t buffer_addr, uint32_t buffer_size)
  343. {
  344. stream->request.frame_info.num_entries = 1;
  345. stream->request.frame_info.ring_buf_info[0].addr = buffer_addr;
  346. stream->request.frame_info.ring_buf_info[0].size = buffer_size;
  347. /* calculate bytes per 4 ms fragment */
  348. stream->request.frame_info.frag_size =
  349. stream->request.pcm_params.sfreq *
  350. stream->request.pcm_params.num_chan *
  351. stream->request.pcm_params.pcm_wd_sz / 8 *
  352. 4 / 1000;
  353. return 0;
  354. }
  355. int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream)
  356. {
  357. struct sst_byt_alloc_params *str_req = &stream->request;
  358. struct sst_byt_alloc_response *reply = &stream->reply;
  359. u64 header;
  360. int ret;
  361. header = sst_byt_header(IPC_IA_ALLOC_STREAM,
  362. sizeof(*str_req) + sizeof(u32),
  363. true, stream->str_id);
  364. ret = sst_ipc_tx_message_wait(&byt->ipc, header, str_req,
  365. sizeof(*str_req),
  366. reply, sizeof(*reply));
  367. if (ret < 0) {
  368. dev_err(byt->dev, "ipc: error stream commit failed\n");
  369. return ret;
  370. }
  371. stream->commited = true;
  372. return 0;
  373. }
  374. int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream)
  375. {
  376. u64 header;
  377. int ret = 0;
  378. struct sst_dsp *sst = byt->dsp;
  379. unsigned long flags;
  380. if (!stream->commited)
  381. goto out;
  382. header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id);
  383. ret = sst_ipc_tx_message_wait(&byt->ipc, header, NULL, 0, NULL, 0);
  384. if (ret < 0) {
  385. dev_err(byt->dev, "ipc: free stream %d failed\n",
  386. stream->str_id);
  387. return -EAGAIN;
  388. }
  389. stream->commited = false;
  390. out:
  391. spin_lock_irqsave(&sst->spinlock, flags);
  392. list_del(&stream->node);
  393. kfree(stream);
  394. spin_unlock_irqrestore(&sst->spinlock, flags);
  395. return ret;
  396. }
  397. static int sst_byt_stream_operations(struct sst_byt *byt, int type,
  398. int stream_id, int wait)
  399. {
  400. u64 header;
  401. header = sst_byt_header(type, 0, false, stream_id);
  402. if (wait)
  403. return sst_ipc_tx_message_wait(&byt->ipc, header, NULL,
  404. 0, NULL, 0);
  405. else
  406. return sst_ipc_tx_message_nowait(&byt->ipc, header,
  407. NULL, 0);
  408. }
  409. /* stream ALSA trigger operations */
  410. int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream,
  411. u32 start_offset)
  412. {
  413. struct sst_byt_start_stream_params start_stream;
  414. void *tx_msg;
  415. size_t size;
  416. u64 header;
  417. int ret;
  418. start_stream.byte_offset = start_offset;
  419. header = sst_byt_header(IPC_IA_START_STREAM,
  420. sizeof(start_stream) + sizeof(u32),
  421. true, stream->str_id);
  422. tx_msg = &start_stream;
  423. size = sizeof(start_stream);
  424. ret = sst_ipc_tx_message_nowait(&byt->ipc, header, tx_msg, size);
  425. if (ret < 0)
  426. dev_err(byt->dev, "ipc: error failed to start stream %d\n",
  427. stream->str_id);
  428. return ret;
  429. }
  430. int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream)
  431. {
  432. int ret;
  433. /* don't stop streams that are not commited */
  434. if (!stream->commited)
  435. return 0;
  436. ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM,
  437. stream->str_id, 0);
  438. if (ret < 0)
  439. dev_err(byt->dev, "ipc: error failed to stop stream %d\n",
  440. stream->str_id);
  441. return ret;
  442. }
  443. int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream)
  444. {
  445. int ret;
  446. ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM,
  447. stream->str_id, 0);
  448. if (ret < 0)
  449. dev_err(byt->dev, "ipc: error failed to pause stream %d\n",
  450. stream->str_id);
  451. return ret;
  452. }
  453. int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream)
  454. {
  455. int ret;
  456. ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM,
  457. stream->str_id, 0);
  458. if (ret < 0)
  459. dev_err(byt->dev, "ipc: error failed to resume stream %d\n",
  460. stream->str_id);
  461. return ret;
  462. }
  463. int sst_byt_get_dsp_position(struct sst_byt *byt,
  464. struct sst_byt_stream *stream, int buffer_size)
  465. {
  466. struct sst_dsp *sst = byt->dsp;
  467. struct sst_byt_tstamp fw_tstamp;
  468. u8 str_id = stream->str_id;
  469. u32 tstamp_offset;
  470. tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp);
  471. memcpy_fromio(&fw_tstamp,
  472. sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp));
  473. return do_div(fw_tstamp.ring_buffer_counter, buffer_size);
  474. }
  475. struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt)
  476. {
  477. return byt->dsp;
  478. }
  479. static struct sst_dsp_device byt_dev = {
  480. .thread = sst_byt_irq_thread,
  481. .ops = &sst_byt_ops,
  482. };
  483. int sst_byt_dsp_suspend_late(struct device *dev, struct sst_pdata *pdata)
  484. {
  485. struct sst_byt *byt = pdata->dsp;
  486. dev_dbg(byt->dev, "dsp reset\n");
  487. sst_dsp_reset(byt->dsp);
  488. sst_ipc_drop_all(&byt->ipc);
  489. dev_dbg(byt->dev, "dsp in reset\n");
  490. dev_dbg(byt->dev, "free all blocks and unload fw\n");
  491. sst_fw_unload(byt->fw);
  492. return 0;
  493. }
  494. EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late);
  495. int sst_byt_dsp_boot(struct device *dev, struct sst_pdata *pdata)
  496. {
  497. struct sst_byt *byt = pdata->dsp;
  498. int ret;
  499. dev_dbg(byt->dev, "reload dsp fw\n");
  500. sst_dsp_reset(byt->dsp);
  501. ret = sst_fw_reload(byt->fw);
  502. if (ret < 0) {
  503. dev_err(dev, "error: failed to reload firmware\n");
  504. return ret;
  505. }
  506. /* wait for DSP boot completion */
  507. byt->boot_complete = false;
  508. sst_dsp_boot(byt->dsp);
  509. dev_dbg(byt->dev, "dsp booting...\n");
  510. return 0;
  511. }
  512. EXPORT_SYMBOL_GPL(sst_byt_dsp_boot);
  513. int sst_byt_dsp_wait_for_ready(struct device *dev, struct sst_pdata *pdata)
  514. {
  515. struct sst_byt *byt = pdata->dsp;
  516. int err;
  517. dev_dbg(byt->dev, "wait for dsp reboot\n");
  518. err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
  519. msecs_to_jiffies(IPC_BOOT_MSECS));
  520. if (err == 0) {
  521. dev_err(byt->dev, "ipc: error DSP boot timeout\n");
  522. return -EIO;
  523. }
  524. dev_dbg(byt->dev, "dsp rebooted\n");
  525. return 0;
  526. }
  527. EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready);
  528. static void byt_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
  529. {
  530. if (msg->header & IPC_HEADER_LARGE(true))
  531. sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
  532. sst_dsp_shim_write64_unlocked(ipc->dsp, SST_IPCX, msg->header);
  533. }
  534. static void byt_shim_dbg(struct sst_generic_ipc *ipc, const char *text)
  535. {
  536. struct sst_dsp *sst = ipc->dsp;
  537. u64 isr, ipcd, imrx, ipcx;
  538. ipcx = sst_dsp_shim_read64_unlocked(sst, SST_IPCX);
  539. isr = sst_dsp_shim_read64_unlocked(sst, SST_ISRX);
  540. ipcd = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
  541. imrx = sst_dsp_shim_read64_unlocked(sst, SST_IMRX);
  542. dev_err(ipc->dev,
  543. "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
  544. text, ipcx, isr, ipcd, imrx);
  545. }
  546. static void byt_tx_data_copy(struct ipc_message *msg, char *tx_data,
  547. size_t tx_size)
  548. {
  549. /* msg content = lower 32-bit of the header + data */
  550. *(u32 *)msg->tx_data = (u32)(msg->header & (u32)-1);
  551. memcpy(msg->tx_data + sizeof(u32), tx_data, tx_size);
  552. msg->tx_size += sizeof(u32);
  553. }
  554. static u64 byt_reply_msg_match(u64 header, u64 *mask)
  555. {
  556. /* match reply to message sent based on msg and stream IDs */
  557. *mask = IPC_HEADER_MSG_ID_MASK |
  558. IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT;
  559. header &= *mask;
  560. return header;
  561. }
  562. static bool byt_is_dsp_busy(struct sst_dsp *dsp)
  563. {
  564. u64 ipcx;
  565. ipcx = sst_dsp_shim_read_unlocked(dsp, SST_IPCX);
  566. return (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE));
  567. }
  568. int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata)
  569. {
  570. struct sst_byt *byt;
  571. struct sst_generic_ipc *ipc;
  572. struct sst_fw *byt_sst_fw;
  573. struct sst_byt_fw_init init;
  574. int err;
  575. dev_dbg(dev, "initialising Byt DSP IPC\n");
  576. byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL);
  577. if (byt == NULL)
  578. return -ENOMEM;
  579. byt->dev = dev;
  580. ipc = &byt->ipc;
  581. ipc->dev = dev;
  582. ipc->ops.tx_msg = byt_tx_msg;
  583. ipc->ops.shim_dbg = byt_shim_dbg;
  584. ipc->ops.tx_data_copy = byt_tx_data_copy;
  585. ipc->ops.reply_msg_match = byt_reply_msg_match;
  586. ipc->ops.is_dsp_busy = byt_is_dsp_busy;
  587. ipc->tx_data_max_size = IPC_MAX_MAILBOX_BYTES;
  588. ipc->rx_data_max_size = IPC_MAX_MAILBOX_BYTES;
  589. err = sst_ipc_init(ipc);
  590. if (err != 0)
  591. goto ipc_init_err;
  592. INIT_LIST_HEAD(&byt->stream_list);
  593. init_waitqueue_head(&byt->boot_wait);
  594. byt_dev.thread_context = byt;
  595. /* init SST shim */
  596. byt->dsp = sst_dsp_new(dev, &byt_dev, pdata);
  597. if (byt->dsp == NULL) {
  598. err = -ENODEV;
  599. goto dsp_new_err;
  600. }
  601. ipc->dsp = byt->dsp;
  602. /* keep the DSP in reset state for base FW loading */
  603. sst_dsp_reset(byt->dsp);
  604. byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt);
  605. if (byt_sst_fw == NULL) {
  606. err = -ENODEV;
  607. dev_err(dev, "error: failed to load firmware\n");
  608. goto fw_err;
  609. }
  610. /* wait for DSP boot completion */
  611. sst_dsp_boot(byt->dsp);
  612. err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
  613. msecs_to_jiffies(IPC_BOOT_MSECS));
  614. if (err == 0) {
  615. err = -EIO;
  616. dev_err(byt->dev, "ipc: error DSP boot timeout\n");
  617. goto boot_err;
  618. }
  619. /* show firmware information */
  620. sst_dsp_inbox_read(byt->dsp, &init, sizeof(init));
  621. dev_info(byt->dev, "FW version: %02x.%02x.%02x.%02x\n",
  622. init.fw_version.major, init.fw_version.minor,
  623. init.fw_version.build, init.fw_version.type);
  624. dev_info(byt->dev, "Build type: %x\n", init.fw_version.type);
  625. dev_info(byt->dev, "Build date: %s %s\n",
  626. init.build_info.date, init.build_info.time);
  627. pdata->dsp = byt;
  628. byt->fw = byt_sst_fw;
  629. return 0;
  630. boot_err:
  631. sst_dsp_reset(byt->dsp);
  632. sst_fw_free(byt_sst_fw);
  633. fw_err:
  634. sst_dsp_free(byt->dsp);
  635. dsp_new_err:
  636. sst_ipc_fini(ipc);
  637. ipc_init_err:
  638. return err;
  639. }
  640. EXPORT_SYMBOL_GPL(sst_byt_dsp_init);
  641. void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata)
  642. {
  643. struct sst_byt *byt = pdata->dsp;
  644. sst_dsp_reset(byt->dsp);
  645. sst_fw_free_all(byt->dsp);
  646. sst_dsp_free(byt->dsp);
  647. sst_ipc_fini(&byt->ipc);
  648. }
  649. EXPORT_SYMBOL_GPL(sst_byt_dsp_free);