2 * Intel Baytrail SST IPC Support
3 * Copyright (c) 2014, Intel Corporation.
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.
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
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/device.h>
19 #include <linux/wait.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
22 #include <linux/export.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
26 #include <linux/kthread.h>
27 #include <linux/firmware.h>
29 #include <asm/div64.h>
31 #include "sst-baytrail-ipc.h"
32 #include "../common/sst-dsp.h"
33 #include "../common/sst-dsp-priv.h"
34 #include "../common/sst-ipc.h"
36 /* IPC message timeout */
37 #define IPC_TIMEOUT_MSECS 300
38 #define IPC_BOOT_MSECS 200
40 #define IPC_EMPTY_LIST_SIZE 8
43 #define IPC_HEADER_MSG_ID_MASK 0xff
44 #define IPC_HEADER_MSG_ID(x) ((x) & IPC_HEADER_MSG_ID_MASK)
45 #define IPC_HEADER_STR_ID_SHIFT 8
46 #define IPC_HEADER_STR_ID_MASK 0x1f
47 #define IPC_HEADER_STR_ID(x) (((x) & 0x1f) << IPC_HEADER_STR_ID_SHIFT)
48 #define IPC_HEADER_LARGE_SHIFT 13
49 #define IPC_HEADER_LARGE(x) (((x) & 0x1) << IPC_HEADER_LARGE_SHIFT)
50 #define IPC_HEADER_DATA_SHIFT 16
51 #define IPC_HEADER_DATA_MASK 0x3fff
52 #define IPC_HEADER_DATA(x) (((x) & 0x3fff) << IPC_HEADER_DATA_SHIFT)
54 /* mask for differentiating between notification and reply message */
55 #define IPC_NOTIFICATION (0x1 << 7)
57 /* I2L Stream config/control msgs */
58 #define IPC_IA_ALLOC_STREAM 0x20
59 #define IPC_IA_FREE_STREAM 0x21
60 #define IPC_IA_PAUSE_STREAM 0x24
61 #define IPC_IA_RESUME_STREAM 0x25
62 #define IPC_IA_DROP_STREAM 0x26
63 #define IPC_IA_START_STREAM 0x30
65 /* notification messages */
66 #define IPC_IA_FW_INIT_CMPLT 0x81
67 #define IPC_SST_PERIOD_ELAPSED 0x97
69 /* IPC messages between host and ADSP */
70 struct sst_byt_address_info {
75 struct sst_byt_str_type {
85 struct sst_byt_pcm_params {
94 struct sst_byt_frames_info {
98 struct sst_byt_address_info ring_buf_info[8];
101 struct sst_byt_alloc_params {
102 struct sst_byt_str_type str_type;
103 struct sst_byt_pcm_params pcm_params;
104 struct sst_byt_frames_info frame_info;
107 struct sst_byt_alloc_response {
108 struct sst_byt_str_type str_type;
112 struct sst_byt_start_stream_params {
116 struct sst_byt_tstamp {
117 u64 ring_buffer_counter;
118 u64 hardware_counter;
122 u32 sampling_frequency;
126 struct sst_byt_fw_version {
133 struct sst_byt_fw_build_info {
138 struct sst_byt_fw_init {
139 struct sst_byt_fw_version fw_version;
140 struct sst_byt_fw_build_info build_info;
146 struct sst_byt_stream;
149 /* stream infomation */
150 struct sst_byt_stream {
151 struct list_head node;
154 struct sst_byt_alloc_params request;
155 struct sst_byt_alloc_response reply;
163 /* driver callback */
164 u32 (*notify_position)(struct sst_byt_stream *stream, void *data);
168 /* SST Baytrail IPC data */
174 struct list_head stream_list;
177 wait_queue_head_t boot_wait;
182 struct sst_generic_ipc ipc;
185 static inline u64 sst_byt_header(int msg_id, int data, bool large, int str_id)
187 return IPC_HEADER_MSG_ID(msg_id) | IPC_HEADER_STR_ID(str_id) |
188 IPC_HEADER_LARGE(large) | IPC_HEADER_DATA(data) |
192 static inline u16 sst_byt_header_msg_id(u64 header)
194 return header & IPC_HEADER_MSG_ID_MASK;
197 static inline u8 sst_byt_header_str_id(u64 header)
199 return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK;
202 static inline u16 sst_byt_header_data(u64 header)
204 return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK;
207 static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt,
210 struct sst_byt_stream *stream;
212 list_for_each_entry(stream, &byt->stream_list, node) {
213 if (stream->str_id == stream_id)
220 static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg)
222 struct sst_byt_stream *stream;
223 u64 header = msg->header;
224 u8 stream_id = sst_byt_header_str_id(header);
225 u8 stream_msg = sst_byt_header_msg_id(header);
227 stream = sst_byt_get_stream(byt, stream_id);
231 switch (stream_msg) {
232 case IPC_IA_DROP_STREAM:
233 case IPC_IA_PAUSE_STREAM:
234 case IPC_IA_FREE_STREAM:
235 stream->running = false;
237 case IPC_IA_START_STREAM:
238 case IPC_IA_RESUME_STREAM:
239 stream->running = true;
244 static int sst_byt_process_reply(struct sst_byt *byt, u64 header)
246 struct ipc_message *msg;
248 msg = sst_ipc_reply_find_msg(&byt->ipc, header);
252 if (header & IPC_HEADER_LARGE(true)) {
253 msg->rx_size = sst_byt_header_data(header);
254 sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size);
257 /* update any stream states */
258 sst_byt_stream_update(byt, msg);
260 list_del(&msg->list);
262 sst_ipc_tx_msg_reply_complete(&byt->ipc, msg);
267 static void sst_byt_fw_ready(struct sst_byt *byt, u64 header)
269 dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header);
271 byt->boot_complete = true;
272 wake_up(&byt->boot_wait);
275 static int sst_byt_process_notification(struct sst_byt *byt,
276 unsigned long *flags)
278 struct sst_dsp *sst = byt->dsp;
279 struct sst_byt_stream *stream;
281 u8 msg_id, stream_id;
284 header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
285 msg_id = sst_byt_header_msg_id(header);
288 case IPC_SST_PERIOD_ELAPSED:
289 stream_id = sst_byt_header_str_id(header);
290 stream = sst_byt_get_stream(byt, stream_id);
291 if (stream && stream->running && stream->notify_position) {
292 spin_unlock_irqrestore(&sst->spinlock, *flags);
293 stream->notify_position(stream, stream->pdata);
294 spin_lock_irqsave(&sst->spinlock, *flags);
297 case IPC_IA_FW_INIT_CMPLT:
298 sst_byt_fw_ready(byt, header);
305 static irqreturn_t sst_byt_irq_thread(int irq, void *context)
307 struct sst_dsp *sst = (struct sst_dsp *) context;
308 struct sst_byt *byt = sst_dsp_get_thread_context(sst);
309 struct sst_generic_ipc *ipc = &byt->ipc;
313 spin_lock_irqsave(&sst->spinlock, flags);
315 header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
316 if (header & SST_BYT_IPCD_BUSY) {
317 if (header & IPC_NOTIFICATION) {
318 /* message from ADSP */
319 sst_byt_process_notification(byt, &flags);
321 /* reply from ADSP */
322 sst_byt_process_reply(byt, header);
325 * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
326 * processed the message and can accept new. Clear data part
329 sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD,
330 SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY |
331 IPC_HEADER_DATA(IPC_HEADER_DATA_MASK),
333 /* unmask message request interrupts */
334 sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX,
335 SST_BYT_IMRX_REQUEST, 0);
338 spin_unlock_irqrestore(&sst->spinlock, flags);
340 /* continue to send any remaining messages... */
341 kthread_queue_work(&ipc->kworker, &ipc->kwork);
347 struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id,
348 u32 (*notify_position)(struct sst_byt_stream *stream, void *data),
351 struct sst_byt_stream *stream;
352 struct sst_dsp *sst = byt->dsp;
355 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
359 spin_lock_irqsave(&sst->spinlock, flags);
360 list_add(&stream->node, &byt->stream_list);
361 stream->notify_position = notify_position;
362 stream->pdata = data;
365 spin_unlock_irqrestore(&sst->spinlock, flags);
370 int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream,
373 stream->request.pcm_params.pcm_wd_sz = bits;
377 int sst_byt_stream_set_channels(struct sst_byt *byt,
378 struct sst_byt_stream *stream, u8 channels)
380 stream->request.pcm_params.num_chan = channels;
384 int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream,
387 stream->request.pcm_params.sfreq = rate;
391 /* stream sonfiguration */
392 int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream,
393 int codec_type, int stream_type, int operation)
395 stream->request.str_type.codec_type = codec_type;
396 stream->request.str_type.str_type = stream_type;
397 stream->request.str_type.operation = operation;
398 stream->request.str_type.time_slots = 0xc;
403 int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream,
404 uint32_t buffer_addr, uint32_t buffer_size)
406 stream->request.frame_info.num_entries = 1;
407 stream->request.frame_info.ring_buf_info[0].addr = buffer_addr;
408 stream->request.frame_info.ring_buf_info[0].size = buffer_size;
409 /* calculate bytes per 4 ms fragment */
410 stream->request.frame_info.frag_size =
411 stream->request.pcm_params.sfreq *
412 stream->request.pcm_params.num_chan *
413 stream->request.pcm_params.pcm_wd_sz / 8 *
418 int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream)
420 struct sst_byt_alloc_params *str_req = &stream->request;
421 struct sst_byt_alloc_response *reply = &stream->reply;
425 header = sst_byt_header(IPC_IA_ALLOC_STREAM,
426 sizeof(*str_req) + sizeof(u32),
427 true, stream->str_id);
428 ret = sst_ipc_tx_message_wait(&byt->ipc, header, str_req,
430 reply, sizeof(*reply));
432 dev_err(byt->dev, "ipc: error stream commit failed\n");
436 stream->commited = true;
441 int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream)
445 struct sst_dsp *sst = byt->dsp;
448 if (!stream->commited)
451 header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id);
452 ret = sst_ipc_tx_message_wait(&byt->ipc, header, NULL, 0, NULL, 0);
454 dev_err(byt->dev, "ipc: free stream %d failed\n",
459 stream->commited = false;
461 spin_lock_irqsave(&sst->spinlock, flags);
462 list_del(&stream->node);
464 spin_unlock_irqrestore(&sst->spinlock, flags);
469 static int sst_byt_stream_operations(struct sst_byt *byt, int type,
470 int stream_id, int wait)
474 header = sst_byt_header(type, 0, false, stream_id);
476 return sst_ipc_tx_message_wait(&byt->ipc, header, NULL,
479 return sst_ipc_tx_message_nowait(&byt->ipc, header,
483 /* stream ALSA trigger operations */
484 int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream,
487 struct sst_byt_start_stream_params start_stream;
493 start_stream.byte_offset = start_offset;
494 header = sst_byt_header(IPC_IA_START_STREAM,
495 sizeof(start_stream) + sizeof(u32),
496 true, stream->str_id);
497 tx_msg = &start_stream;
498 size = sizeof(start_stream);
500 ret = sst_ipc_tx_message_nowait(&byt->ipc, header, tx_msg, size);
502 dev_err(byt->dev, "ipc: error failed to start stream %d\n",
508 int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream)
512 /* don't stop streams that are not commited */
513 if (!stream->commited)
516 ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM,
519 dev_err(byt->dev, "ipc: error failed to stop stream %d\n",
524 int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream)
528 ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM,
531 dev_err(byt->dev, "ipc: error failed to pause stream %d\n",
537 int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream)
541 ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM,
544 dev_err(byt->dev, "ipc: error failed to resume stream %d\n",
550 int sst_byt_get_dsp_position(struct sst_byt *byt,
551 struct sst_byt_stream *stream, int buffer_size)
553 struct sst_dsp *sst = byt->dsp;
554 struct sst_byt_tstamp fw_tstamp;
555 u8 str_id = stream->str_id;
558 tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp);
559 memcpy_fromio(&fw_tstamp,
560 sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp));
562 return do_div(fw_tstamp.ring_buffer_counter, buffer_size);
565 struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt)
570 static struct sst_dsp_device byt_dev = {
571 .thread = sst_byt_irq_thread,
575 int sst_byt_dsp_suspend_late(struct device *dev, struct sst_pdata *pdata)
577 struct sst_byt *byt = pdata->dsp;
579 dev_dbg(byt->dev, "dsp reset\n");
580 sst_dsp_reset(byt->dsp);
581 sst_ipc_drop_all(&byt->ipc);
582 dev_dbg(byt->dev, "dsp in reset\n");
584 dev_dbg(byt->dev, "free all blocks and unload fw\n");
585 sst_fw_unload(byt->fw);
589 EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late);
591 int sst_byt_dsp_boot(struct device *dev, struct sst_pdata *pdata)
593 struct sst_byt *byt = pdata->dsp;
596 dev_dbg(byt->dev, "reload dsp fw\n");
598 sst_dsp_reset(byt->dsp);
600 ret = sst_fw_reload(byt->fw);
602 dev_err(dev, "error: failed to reload firmware\n");
606 /* wait for DSP boot completion */
607 byt->boot_complete = false;
608 sst_dsp_boot(byt->dsp);
609 dev_dbg(byt->dev, "dsp booting...\n");
613 EXPORT_SYMBOL_GPL(sst_byt_dsp_boot);
615 int sst_byt_dsp_wait_for_ready(struct device *dev, struct sst_pdata *pdata)
617 struct sst_byt *byt = pdata->dsp;
620 dev_dbg(byt->dev, "wait for dsp reboot\n");
622 err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
623 msecs_to_jiffies(IPC_BOOT_MSECS));
625 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
629 dev_dbg(byt->dev, "dsp rebooted\n");
632 EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready);
634 static void byt_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
636 if (msg->header & IPC_HEADER_LARGE(true))
637 sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
639 sst_dsp_shim_write64_unlocked(ipc->dsp, SST_IPCX, msg->header);
642 static void byt_shim_dbg(struct sst_generic_ipc *ipc, const char *text)
644 struct sst_dsp *sst = ipc->dsp;
645 u64 isr, ipcd, imrx, ipcx;
647 ipcx = sst_dsp_shim_read64_unlocked(sst, SST_IPCX);
648 isr = sst_dsp_shim_read64_unlocked(sst, SST_ISRX);
649 ipcd = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
650 imrx = sst_dsp_shim_read64_unlocked(sst, SST_IMRX);
653 "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
654 text, ipcx, isr, ipcd, imrx);
657 static void byt_tx_data_copy(struct ipc_message *msg, char *tx_data,
660 /* msg content = lower 32-bit of the header + data */
661 *(u32 *)msg->tx_data = (u32)(msg->header & (u32)-1);
662 memcpy(msg->tx_data + sizeof(u32), tx_data, tx_size);
663 msg->tx_size += sizeof(u32);
666 static u64 byt_reply_msg_match(u64 header, u64 *mask)
668 /* match reply to message sent based on msg and stream IDs */
669 *mask = IPC_HEADER_MSG_ID_MASK |
670 IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT;
676 static bool byt_is_dsp_busy(struct sst_dsp *dsp)
680 ipcx = sst_dsp_shim_read_unlocked(dsp, SST_IPCX);
681 return (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE));
684 int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata)
687 struct sst_generic_ipc *ipc;
688 struct sst_fw *byt_sst_fw;
689 struct sst_byt_fw_init init;
692 dev_dbg(dev, "initialising Byt DSP IPC\n");
694 byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL);
702 ipc->ops.tx_msg = byt_tx_msg;
703 ipc->ops.shim_dbg = byt_shim_dbg;
704 ipc->ops.tx_data_copy = byt_tx_data_copy;
705 ipc->ops.reply_msg_match = byt_reply_msg_match;
706 ipc->ops.is_dsp_busy = byt_is_dsp_busy;
707 ipc->tx_data_max_size = IPC_MAX_MAILBOX_BYTES;
708 ipc->rx_data_max_size = IPC_MAX_MAILBOX_BYTES;
710 err = sst_ipc_init(ipc);
714 INIT_LIST_HEAD(&byt->stream_list);
715 init_waitqueue_head(&byt->boot_wait);
716 byt_dev.thread_context = byt;
719 byt->dsp = sst_dsp_new(dev, &byt_dev, pdata);
720 if (byt->dsp == NULL) {
727 /* keep the DSP in reset state for base FW loading */
728 sst_dsp_reset(byt->dsp);
730 byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt);
731 if (byt_sst_fw == NULL) {
733 dev_err(dev, "error: failed to load firmware\n");
737 /* wait for DSP boot completion */
738 sst_dsp_boot(byt->dsp);
739 err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
740 msecs_to_jiffies(IPC_BOOT_MSECS));
743 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
747 /* show firmware information */
748 sst_dsp_inbox_read(byt->dsp, &init, sizeof(init));
749 dev_info(byt->dev, "FW version: %02x.%02x.%02x.%02x\n",
750 init.fw_version.major, init.fw_version.minor,
751 init.fw_version.build, init.fw_version.type);
752 dev_info(byt->dev, "Build type: %x\n", init.fw_version.type);
753 dev_info(byt->dev, "Build date: %s %s\n",
754 init.build_info.date, init.build_info.time);
757 byt->fw = byt_sst_fw;
762 sst_dsp_reset(byt->dsp);
763 sst_fw_free(byt_sst_fw);
765 sst_dsp_free(byt->dsp);
772 EXPORT_SYMBOL_GPL(sst_byt_dsp_init);
774 void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata)
776 struct sst_byt *byt = pdata->dsp;
778 sst_dsp_reset(byt->dsp);
779 sst_fw_free_all(byt->dsp);
780 sst_dsp_free(byt->dsp);
781 sst_ipc_fini(&byt->ipc);
783 EXPORT_SYMBOL_GPL(sst_byt_dsp_free);