sst-baytrail-ipc.c 19 KB

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