sst-baytrail-ipc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. return IPC_HEADER_MSG_ID(msg_id) | IPC_HEADER_STR_ID(str_id) |
  159. IPC_HEADER_LARGE(large) | IPC_HEADER_DATA(data) |
  160. SST_BYT_IPCX_BUSY;
  161. }
  162. static inline u16 sst_byt_header_msg_id(u64 header)
  163. {
  164. return header & IPC_HEADER_MSG_ID_MASK;
  165. }
  166. static inline u8 sst_byt_header_str_id(u64 header)
  167. {
  168. return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK;
  169. }
  170. static inline u16 sst_byt_header_data(u64 header)
  171. {
  172. return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK;
  173. }
  174. static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt,
  175. int stream_id)
  176. {
  177. struct sst_byt_stream *stream;
  178. list_for_each_entry(stream, &byt->stream_list, node) {
  179. if (stream->str_id == stream_id)
  180. return stream;
  181. }
  182. return NULL;
  183. }
  184. static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg)
  185. {
  186. struct sst_byt_stream *stream;
  187. u64 header = msg->header;
  188. u8 stream_id = sst_byt_header_str_id(header);
  189. u8 stream_msg = sst_byt_header_msg_id(header);
  190. stream = sst_byt_get_stream(byt, stream_id);
  191. if (stream == NULL)
  192. return;
  193. switch (stream_msg) {
  194. case IPC_IA_DROP_STREAM:
  195. case IPC_IA_PAUSE_STREAM:
  196. case IPC_IA_FREE_STREAM:
  197. stream->running = false;
  198. break;
  199. case IPC_IA_START_STREAM:
  200. case IPC_IA_RESUME_STREAM:
  201. stream->running = true;
  202. break;
  203. }
  204. }
  205. static int sst_byt_process_reply(struct sst_byt *byt, u64 header)
  206. {
  207. struct ipc_message *msg;
  208. msg = sst_ipc_reply_find_msg(&byt->ipc, header);
  209. if (msg == NULL)
  210. return 1;
  211. if (header & IPC_HEADER_LARGE(true)) {
  212. msg->rx_size = sst_byt_header_data(header);
  213. sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size);
  214. }
  215. /* update any stream states */
  216. sst_byt_stream_update(byt, msg);
  217. list_del(&msg->list);
  218. /* wake up */
  219. sst_ipc_tx_msg_reply_complete(&byt->ipc, msg);
  220. return 1;
  221. }
  222. static void sst_byt_fw_ready(struct sst_byt *byt, u64 header)
  223. {
  224. dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header);
  225. byt->boot_complete = true;
  226. wake_up(&byt->boot_wait);
  227. }
  228. static int sst_byt_process_notification(struct sst_byt *byt,
  229. unsigned long *flags)
  230. {
  231. struct sst_dsp *sst = byt->dsp;
  232. struct sst_byt_stream *stream;
  233. u64 header;
  234. u8 msg_id, stream_id;
  235. int handled = 1;
  236. header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
  237. msg_id = sst_byt_header_msg_id(header);
  238. switch (msg_id) {
  239. case IPC_SST_PERIOD_ELAPSED:
  240. stream_id = sst_byt_header_str_id(header);
  241. stream = sst_byt_get_stream(byt, stream_id);
  242. if (stream && stream->running && stream->notify_position) {
  243. spin_unlock_irqrestore(&sst->spinlock, *flags);
  244. stream->notify_position(stream, stream->pdata);
  245. spin_lock_irqsave(&sst->spinlock, *flags);
  246. }
  247. break;
  248. case IPC_IA_FW_INIT_CMPLT:
  249. sst_byt_fw_ready(byt, header);
  250. break;
  251. }
  252. return handled;
  253. }
  254. static irqreturn_t sst_byt_irq_thread(int irq, void *context)
  255. {
  256. struct sst_dsp *sst = (struct sst_dsp *) context;
  257. struct sst_byt *byt = sst_dsp_get_thread_context(sst);
  258. struct sst_generic_ipc *ipc = &byt->ipc;
  259. u64 header;
  260. unsigned long flags;
  261. spin_lock_irqsave(&sst->spinlock, flags);
  262. header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
  263. if (header & SST_BYT_IPCD_BUSY) {
  264. if (header & IPC_NOTIFICATION) {
  265. /* message from ADSP */
  266. sst_byt_process_notification(byt, &flags);
  267. } else {
  268. /* reply from ADSP */
  269. sst_byt_process_reply(byt, header);
  270. }
  271. /*
  272. * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
  273. * processed the message and can accept new. Clear data part
  274. * of the header
  275. */
  276. sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD,
  277. SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY |
  278. IPC_HEADER_DATA(IPC_HEADER_DATA_MASK),
  279. SST_BYT_IPCD_DONE);
  280. /* unmask message request interrupts */
  281. sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX,
  282. SST_BYT_IMRX_REQUEST, 0);
  283. }
  284. spin_unlock_irqrestore(&sst->spinlock, flags);
  285. /* continue to send any remaining messages... */
  286. kthread_queue_work(&ipc->kworker, &ipc->kwork);
  287. return IRQ_HANDLED;
  288. }
  289. /* stream API */
  290. struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id,
  291. u32 (*notify_position)(struct sst_byt_stream *stream, void *data),
  292. void *data)
  293. {
  294. struct sst_byt_stream *stream;
  295. struct sst_dsp *sst = byt->dsp;
  296. unsigned long flags;
  297. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  298. if (stream == NULL)
  299. return NULL;
  300. spin_lock_irqsave(&sst->spinlock, flags);
  301. list_add(&stream->node, &byt->stream_list);
  302. stream->notify_position = notify_position;
  303. stream->pdata = data;
  304. stream->byt = byt;
  305. stream->str_id = id;
  306. spin_unlock_irqrestore(&sst->spinlock, flags);
  307. return stream;
  308. }
  309. int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream,
  310. int bits)
  311. {
  312. stream->request.pcm_params.pcm_wd_sz = bits;
  313. return 0;
  314. }
  315. int sst_byt_stream_set_channels(struct sst_byt *byt,
  316. struct sst_byt_stream *stream, u8 channels)
  317. {
  318. stream->request.pcm_params.num_chan = channels;
  319. return 0;
  320. }
  321. int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream,
  322. unsigned int rate)
  323. {
  324. stream->request.pcm_params.sfreq = rate;
  325. return 0;
  326. }
  327. /* stream sonfiguration */
  328. int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream,
  329. int codec_type, int stream_type, int operation)
  330. {
  331. stream->request.str_type.codec_type = codec_type;
  332. stream->request.str_type.str_type = stream_type;
  333. stream->request.str_type.operation = operation;
  334. stream->request.str_type.time_slots = 0xc;
  335. return 0;
  336. }
  337. int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream,
  338. uint32_t buffer_addr, uint32_t buffer_size)
  339. {
  340. stream->request.frame_info.num_entries = 1;
  341. stream->request.frame_info.ring_buf_info[0].addr = buffer_addr;
  342. stream->request.frame_info.ring_buf_info[0].size = buffer_size;
  343. /* calculate bytes per 4 ms fragment */
  344. stream->request.frame_info.frag_size =
  345. stream->request.pcm_params.sfreq *
  346. stream->request.pcm_params.num_chan *
  347. stream->request.pcm_params.pcm_wd_sz / 8 *
  348. 4 / 1000;
  349. return 0;
  350. }
  351. int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream)
  352. {
  353. struct sst_byt_alloc_params *str_req = &stream->request;
  354. struct sst_byt_alloc_response *reply = &stream->reply;
  355. u64 header;
  356. int ret;
  357. header = sst_byt_header(IPC_IA_ALLOC_STREAM,
  358. sizeof(*str_req) + sizeof(u32),
  359. true, stream->str_id);
  360. ret = sst_ipc_tx_message_wait(&byt->ipc, header, str_req,
  361. sizeof(*str_req),
  362. reply, sizeof(*reply));
  363. if (ret < 0) {
  364. dev_err(byt->dev, "ipc: error stream commit failed\n");
  365. return ret;
  366. }
  367. stream->commited = true;
  368. return 0;
  369. }
  370. int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream)
  371. {
  372. u64 header;
  373. int ret = 0;
  374. struct sst_dsp *sst = byt->dsp;
  375. unsigned long flags;
  376. if (!stream->commited)
  377. goto out;
  378. header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id);
  379. ret = sst_ipc_tx_message_wait(&byt->ipc, header, NULL, 0, NULL, 0);
  380. if (ret < 0) {
  381. dev_err(byt->dev, "ipc: free stream %d failed\n",
  382. stream->str_id);
  383. return -EAGAIN;
  384. }
  385. stream->commited = false;
  386. out:
  387. spin_lock_irqsave(&sst->spinlock, flags);
  388. list_del(&stream->node);
  389. kfree(stream);
  390. spin_unlock_irqrestore(&sst->spinlock, flags);
  391. return ret;
  392. }
  393. static int sst_byt_stream_operations(struct sst_byt *byt, int type,
  394. int stream_id, int wait)
  395. {
  396. u64 header;
  397. header = sst_byt_header(type, 0, false, stream_id);
  398. if (wait)
  399. return sst_ipc_tx_message_wait(&byt->ipc, header, NULL,
  400. 0, NULL, 0);
  401. else
  402. return sst_ipc_tx_message_nowait(&byt->ipc, header,
  403. NULL, 0);
  404. }
  405. /* stream ALSA trigger operations */
  406. int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream,
  407. u32 start_offset)
  408. {
  409. struct sst_byt_start_stream_params start_stream;
  410. void *tx_msg;
  411. size_t size;
  412. u64 header;
  413. int ret;
  414. start_stream.byte_offset = start_offset;
  415. header = sst_byt_header(IPC_IA_START_STREAM,
  416. sizeof(start_stream) + sizeof(u32),
  417. true, stream->str_id);
  418. tx_msg = &start_stream;
  419. size = sizeof(start_stream);
  420. ret = sst_ipc_tx_message_nowait(&byt->ipc, header, tx_msg, size);
  421. if (ret < 0)
  422. dev_err(byt->dev, "ipc: error failed to start stream %d\n",
  423. stream->str_id);
  424. return ret;
  425. }
  426. int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream)
  427. {
  428. int ret;
  429. /* don't stop streams that are not commited */
  430. if (!stream->commited)
  431. return 0;
  432. ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM,
  433. stream->str_id, 0);
  434. if (ret < 0)
  435. dev_err(byt->dev, "ipc: error failed to stop stream %d\n",
  436. stream->str_id);
  437. return ret;
  438. }
  439. int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream)
  440. {
  441. int ret;
  442. ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM,
  443. stream->str_id, 0);
  444. if (ret < 0)
  445. dev_err(byt->dev, "ipc: error failed to pause stream %d\n",
  446. stream->str_id);
  447. return ret;
  448. }
  449. int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream)
  450. {
  451. int ret;
  452. ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM,
  453. stream->str_id, 0);
  454. if (ret < 0)
  455. dev_err(byt->dev, "ipc: error failed to resume stream %d\n",
  456. stream->str_id);
  457. return ret;
  458. }
  459. int sst_byt_get_dsp_position(struct sst_byt *byt,
  460. struct sst_byt_stream *stream, int buffer_size)
  461. {
  462. struct sst_dsp *sst = byt->dsp;
  463. struct sst_byt_tstamp fw_tstamp;
  464. u8 str_id = stream->str_id;
  465. u32 tstamp_offset;
  466. tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp);
  467. memcpy_fromio(&fw_tstamp,
  468. sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp));
  469. return do_div(fw_tstamp.ring_buffer_counter, buffer_size);
  470. }
  471. struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt)
  472. {
  473. return byt->dsp;
  474. }
  475. static struct sst_dsp_device byt_dev = {
  476. .thread = sst_byt_irq_thread,
  477. .ops = &sst_byt_ops,
  478. };
  479. int sst_byt_dsp_suspend_late(struct device *dev, struct sst_pdata *pdata)
  480. {
  481. struct sst_byt *byt = pdata->dsp;
  482. dev_dbg(byt->dev, "dsp reset\n");
  483. sst_dsp_reset(byt->dsp);
  484. sst_ipc_drop_all(&byt->ipc);
  485. dev_dbg(byt->dev, "dsp in reset\n");
  486. dev_dbg(byt->dev, "free all blocks and unload fw\n");
  487. sst_fw_unload(byt->fw);
  488. return 0;
  489. }
  490. EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late);
  491. int sst_byt_dsp_boot(struct device *dev, struct sst_pdata *pdata)
  492. {
  493. struct sst_byt *byt = pdata->dsp;
  494. int ret;
  495. dev_dbg(byt->dev, "reload dsp fw\n");
  496. sst_dsp_reset(byt->dsp);
  497. ret = sst_fw_reload(byt->fw);
  498. if (ret < 0) {
  499. dev_err(dev, "error: failed to reload firmware\n");
  500. return ret;
  501. }
  502. /* wait for DSP boot completion */
  503. byt->boot_complete = false;
  504. sst_dsp_boot(byt->dsp);
  505. dev_dbg(byt->dev, "dsp booting...\n");
  506. return 0;
  507. }
  508. EXPORT_SYMBOL_GPL(sst_byt_dsp_boot);
  509. int sst_byt_dsp_wait_for_ready(struct device *dev, struct sst_pdata *pdata)
  510. {
  511. struct sst_byt *byt = pdata->dsp;
  512. int err;
  513. dev_dbg(byt->dev, "wait for dsp reboot\n");
  514. err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
  515. msecs_to_jiffies(IPC_BOOT_MSECS));
  516. if (err == 0) {
  517. dev_err(byt->dev, "ipc: error DSP boot timeout\n");
  518. return -EIO;
  519. }
  520. dev_dbg(byt->dev, "dsp rebooted\n");
  521. return 0;
  522. }
  523. EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready);
  524. static void byt_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
  525. {
  526. if (msg->header & IPC_HEADER_LARGE(true))
  527. sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
  528. sst_dsp_shim_write64_unlocked(ipc->dsp, SST_IPCX, msg->header);
  529. }
  530. static void byt_shim_dbg(struct sst_generic_ipc *ipc, const char *text)
  531. {
  532. struct sst_dsp *sst = ipc->dsp;
  533. u64 isr, ipcd, imrx, ipcx;
  534. ipcx = sst_dsp_shim_read64_unlocked(sst, SST_IPCX);
  535. isr = sst_dsp_shim_read64_unlocked(sst, SST_ISRX);
  536. ipcd = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
  537. imrx = sst_dsp_shim_read64_unlocked(sst, SST_IMRX);
  538. dev_err(ipc->dev,
  539. "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
  540. text, ipcx, isr, ipcd, imrx);
  541. }
  542. static void byt_tx_data_copy(struct ipc_message *msg, char *tx_data,
  543. size_t tx_size)
  544. {
  545. /* msg content = lower 32-bit of the header + data */
  546. *(u32 *)msg->tx_data = (u32)(msg->header & (u32)-1);
  547. memcpy(msg->tx_data + sizeof(u32), tx_data, tx_size);
  548. msg->tx_size += sizeof(u32);
  549. }
  550. static u64 byt_reply_msg_match(u64 header, u64 *mask)
  551. {
  552. /* match reply to message sent based on msg and stream IDs */
  553. *mask = IPC_HEADER_MSG_ID_MASK |
  554. IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT;
  555. header &= *mask;
  556. return header;
  557. }
  558. static bool byt_is_dsp_busy(struct sst_dsp *dsp)
  559. {
  560. u64 ipcx;
  561. ipcx = sst_dsp_shim_read_unlocked(dsp, SST_IPCX);
  562. return (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE));
  563. }
  564. int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata)
  565. {
  566. struct sst_byt *byt;
  567. struct sst_generic_ipc *ipc;
  568. struct sst_fw *byt_sst_fw;
  569. struct sst_byt_fw_init init;
  570. int err;
  571. dev_dbg(dev, "initialising Byt DSP IPC\n");
  572. byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL);
  573. if (byt == NULL)
  574. return -ENOMEM;
  575. byt->dev = dev;
  576. ipc = &byt->ipc;
  577. ipc->dev = dev;
  578. ipc->ops.tx_msg = byt_tx_msg;
  579. ipc->ops.shim_dbg = byt_shim_dbg;
  580. ipc->ops.tx_data_copy = byt_tx_data_copy;
  581. ipc->ops.reply_msg_match = byt_reply_msg_match;
  582. ipc->ops.is_dsp_busy = byt_is_dsp_busy;
  583. ipc->tx_data_max_size = IPC_MAX_MAILBOX_BYTES;
  584. ipc->rx_data_max_size = IPC_MAX_MAILBOX_BYTES;
  585. err = sst_ipc_init(ipc);
  586. if (err != 0)
  587. goto ipc_init_err;
  588. INIT_LIST_HEAD(&byt->stream_list);
  589. init_waitqueue_head(&byt->boot_wait);
  590. byt_dev.thread_context = byt;
  591. /* init SST shim */
  592. byt->dsp = sst_dsp_new(dev, &byt_dev, pdata);
  593. if (byt->dsp == NULL) {
  594. err = -ENODEV;
  595. goto dsp_new_err;
  596. }
  597. ipc->dsp = byt->dsp;
  598. /* keep the DSP in reset state for base FW loading */
  599. sst_dsp_reset(byt->dsp);
  600. byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt);
  601. if (byt_sst_fw == NULL) {
  602. err = -ENODEV;
  603. dev_err(dev, "error: failed to load firmware\n");
  604. goto fw_err;
  605. }
  606. /* wait for DSP boot completion */
  607. sst_dsp_boot(byt->dsp);
  608. err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
  609. msecs_to_jiffies(IPC_BOOT_MSECS));
  610. if (err == 0) {
  611. err = -EIO;
  612. dev_err(byt->dev, "ipc: error DSP boot timeout\n");
  613. goto boot_err;
  614. }
  615. /* show firmware information */
  616. sst_dsp_inbox_read(byt->dsp, &init, sizeof(init));
  617. dev_info(byt->dev, "FW version: %02x.%02x.%02x.%02x\n",
  618. init.fw_version.major, init.fw_version.minor,
  619. init.fw_version.build, init.fw_version.type);
  620. dev_info(byt->dev, "Build type: %x\n", init.fw_version.type);
  621. dev_info(byt->dev, "Build date: %s %s\n",
  622. init.build_info.date, init.build_info.time);
  623. pdata->dsp = byt;
  624. byt->fw = byt_sst_fw;
  625. return 0;
  626. boot_err:
  627. sst_dsp_reset(byt->dsp);
  628. sst_fw_free(byt_sst_fw);
  629. fw_err:
  630. sst_dsp_free(byt->dsp);
  631. dsp_new_err:
  632. sst_ipc_fini(ipc);
  633. ipc_init_err:
  634. return err;
  635. }
  636. EXPORT_SYMBOL_GPL(sst_byt_dsp_init);
  637. void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata)
  638. {
  639. struct sst_byt *byt = pdata->dsp;
  640. sst_dsp_reset(byt->dsp);
  641. sst_fw_free_all(byt->dsp);
  642. sst_dsp_free(byt->dsp);
  643. sst_ipc_fini(&byt->ipc);
  644. }
  645. EXPORT_SYMBOL_GPL(sst_byt_dsp_free);