1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Baytrail SST IPC Support
4 * Copyright (c) 2014, Intel Corporation.
7 #include <linux/types.h>
8 #include <linux/kernel.h>
9 #include <linux/list.h>
10 #include <linux/device.h>
11 #include <linux/wait.h>
12 #include <linux/spinlock.h>
13 #include <linux/workqueue.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/firmware.h>
20 #include <asm/div64.h>
22 #include "sst-baytrail-ipc.h"
23 #include "../common/sst-dsp.h"
24 #include "../common/sst-dsp-priv.h"
25 #include "../common/sst-ipc.h"
27 /* IPC message timeout */
28 #define IPC_TIMEOUT_MSECS 300
29 #define IPC_BOOT_MSECS 200
31 #define IPC_EMPTY_LIST_SIZE 8
34 #define IPC_HEADER_MSG_ID_MASK 0xff
35 #define IPC_HEADER_MSG_ID(x) ((x) & IPC_HEADER_MSG_ID_MASK)
36 #define IPC_HEADER_STR_ID_SHIFT 8
37 #define IPC_HEADER_STR_ID_MASK 0x1f
38 #define IPC_HEADER_STR_ID(x) (((x) & 0x1f) << IPC_HEADER_STR_ID_SHIFT)
39 #define IPC_HEADER_LARGE_SHIFT 13
40 #define IPC_HEADER_LARGE(x) (((x) & 0x1) << IPC_HEADER_LARGE_SHIFT)
41 #define IPC_HEADER_DATA_SHIFT 16
42 #define IPC_HEADER_DATA_MASK 0x3fff
43 #define IPC_HEADER_DATA(x) (((x) & 0x3fff) << IPC_HEADER_DATA_SHIFT)
45 /* mask for differentiating between notification and reply message */
46 #define IPC_NOTIFICATION (0x1 << 7)
48 /* I2L Stream config/control msgs */
49 #define IPC_IA_ALLOC_STREAM 0x20
50 #define IPC_IA_FREE_STREAM 0x21
51 #define IPC_IA_PAUSE_STREAM 0x24
52 #define IPC_IA_RESUME_STREAM 0x25
53 #define IPC_IA_DROP_STREAM 0x26
54 #define IPC_IA_START_STREAM 0x30
56 /* notification messages */
57 #define IPC_IA_FW_INIT_CMPLT 0x81
58 #define IPC_SST_PERIOD_ELAPSED 0x97
60 /* IPC messages between host and ADSP */
61 struct sst_byt_address_info {
66 struct sst_byt_str_type {
76 struct sst_byt_pcm_params {
85 struct sst_byt_frames_info {
89 struct sst_byt_address_info ring_buf_info[8];
92 struct sst_byt_alloc_params {
93 struct sst_byt_str_type str_type;
94 struct sst_byt_pcm_params pcm_params;
95 struct sst_byt_frames_info frame_info;
98 struct sst_byt_alloc_response {
99 struct sst_byt_str_type str_type;
103 struct sst_byt_start_stream_params {
107 struct sst_byt_tstamp {
108 u64 ring_buffer_counter;
109 u64 hardware_counter;
113 u32 sampling_frequency;
117 struct sst_byt_fw_version {
124 struct sst_byt_fw_build_info {
129 struct sst_byt_fw_init {
130 struct sst_byt_fw_version fw_version;
131 struct sst_byt_fw_build_info build_info;
137 struct sst_byt_stream;
140 /* stream infomation */
141 struct sst_byt_stream {
142 struct list_head node;
145 struct sst_byt_alloc_params request;
146 struct sst_byt_alloc_response reply;
154 /* driver callback */
155 u32 (*notify_position)(struct sst_byt_stream *stream, void *data);
159 /* SST Baytrail IPC data */
165 struct list_head stream_list;
168 wait_queue_head_t boot_wait;
173 struct sst_generic_ipc ipc;
176 static inline u64 sst_byt_header(int msg_id, int data, bool large, int str_id)
178 return IPC_HEADER_MSG_ID(msg_id) | IPC_HEADER_STR_ID(str_id) |
179 IPC_HEADER_LARGE(large) | IPC_HEADER_DATA(data) |
183 static inline u16 sst_byt_header_msg_id(u64 header)
185 return header & IPC_HEADER_MSG_ID_MASK;
188 static inline u8 sst_byt_header_str_id(u64 header)
190 return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK;
193 static inline u16 sst_byt_header_data(u64 header)
195 return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK;
198 static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt,
201 struct sst_byt_stream *stream;
203 list_for_each_entry(stream, &byt->stream_list, node) {
204 if (stream->str_id == stream_id)
211 static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg)
213 struct sst_byt_stream *stream;
214 u64 header = msg->header;
215 u8 stream_id = sst_byt_header_str_id(header);
216 u8 stream_msg = sst_byt_header_msg_id(header);
218 stream = sst_byt_get_stream(byt, stream_id);
222 switch (stream_msg) {
223 case IPC_IA_DROP_STREAM:
224 case IPC_IA_PAUSE_STREAM:
225 case IPC_IA_FREE_STREAM:
226 stream->running = false;
228 case IPC_IA_START_STREAM:
229 case IPC_IA_RESUME_STREAM:
230 stream->running = true;
235 static int sst_byt_process_reply(struct sst_byt *byt, u64 header)
237 struct ipc_message *msg;
239 msg = sst_ipc_reply_find_msg(&byt->ipc, header);
243 if (header & IPC_HEADER_LARGE(true)) {
244 msg->rx_size = sst_byt_header_data(header);
245 sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size);
248 /* update any stream states */
249 sst_byt_stream_update(byt, msg);
251 list_del(&msg->list);
253 sst_ipc_tx_msg_reply_complete(&byt->ipc, msg);
258 static void sst_byt_fw_ready(struct sst_byt *byt, u64 header)
260 dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header);
262 byt->boot_complete = true;
263 wake_up(&byt->boot_wait);
266 static int sst_byt_process_notification(struct sst_byt *byt,
267 unsigned long *flags)
269 struct sst_dsp *sst = byt->dsp;
270 struct sst_byt_stream *stream;
272 u8 msg_id, stream_id;
274 header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
275 msg_id = sst_byt_header_msg_id(header);
278 case IPC_SST_PERIOD_ELAPSED:
279 stream_id = sst_byt_header_str_id(header);
280 stream = sst_byt_get_stream(byt, stream_id);
281 if (stream && stream->running && stream->notify_position) {
282 spin_unlock_irqrestore(&sst->spinlock, *flags);
283 stream->notify_position(stream, stream->pdata);
284 spin_lock_irqsave(&sst->spinlock, *flags);
287 case IPC_IA_FW_INIT_CMPLT:
288 sst_byt_fw_ready(byt, header);
295 static irqreturn_t sst_byt_irq_thread(int irq, void *context)
297 struct sst_dsp *sst = (struct sst_dsp *) context;
298 struct sst_byt *byt = sst_dsp_get_thread_context(sst);
299 struct sst_generic_ipc *ipc = &byt->ipc;
303 spin_lock_irqsave(&sst->spinlock, flags);
305 header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
306 if (header & SST_BYT_IPCD_BUSY) {
307 if (header & IPC_NOTIFICATION) {
308 /* message from ADSP */
309 sst_byt_process_notification(byt, &flags);
311 /* reply from ADSP */
312 sst_byt_process_reply(byt, header);
315 * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
316 * processed the message and can accept new. Clear data part
319 sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD,
320 SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY |
321 IPC_HEADER_DATA(IPC_HEADER_DATA_MASK),
323 /* unmask message request interrupts */
324 sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX,
325 SST_BYT_IMRX_REQUEST, 0);
328 spin_unlock_irqrestore(&sst->spinlock, flags);
330 /* continue to send any remaining messages... */
331 schedule_work(&ipc->kwork);
337 struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id,
338 u32 (*notify_position)(struct sst_byt_stream *stream, void *data),
341 struct sst_byt_stream *stream;
342 struct sst_dsp *sst = byt->dsp;
345 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
349 spin_lock_irqsave(&sst->spinlock, flags);
350 list_add(&stream->node, &byt->stream_list);
351 stream->notify_position = notify_position;
352 stream->pdata = data;
355 spin_unlock_irqrestore(&sst->spinlock, flags);
360 int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream,
363 stream->request.pcm_params.pcm_wd_sz = bits;
367 int sst_byt_stream_set_channels(struct sst_byt *byt,
368 struct sst_byt_stream *stream, u8 channels)
370 stream->request.pcm_params.num_chan = channels;
374 int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream,
377 stream->request.pcm_params.sfreq = rate;
381 /* stream sonfiguration */
382 int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream,
383 int codec_type, int stream_type, int operation)
385 stream->request.str_type.codec_type = codec_type;
386 stream->request.str_type.str_type = stream_type;
387 stream->request.str_type.operation = operation;
388 stream->request.str_type.time_slots = 0xc;
393 int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream,
394 uint32_t buffer_addr, uint32_t buffer_size)
396 stream->request.frame_info.num_entries = 1;
397 stream->request.frame_info.ring_buf_info[0].addr = buffer_addr;
398 stream->request.frame_info.ring_buf_info[0].size = buffer_size;
399 /* calculate bytes per 4 ms fragment */
400 stream->request.frame_info.frag_size =
401 stream->request.pcm_params.sfreq *
402 stream->request.pcm_params.num_chan *
403 stream->request.pcm_params.pcm_wd_sz / 8 *
408 int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream)
410 struct sst_byt_alloc_params *str_req = &stream->request;
411 struct sst_byt_alloc_response *reply = &stream->reply;
415 header = sst_byt_header(IPC_IA_ALLOC_STREAM,
416 sizeof(*str_req) + sizeof(u32),
417 true, stream->str_id);
418 ret = sst_ipc_tx_message_wait(&byt->ipc, header, str_req,
420 reply, sizeof(*reply));
422 dev_err(byt->dev, "ipc: error stream commit failed\n");
426 stream->commited = true;
431 int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream)
435 struct sst_dsp *sst = byt->dsp;
438 if (!stream->commited)
441 header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id);
442 ret = sst_ipc_tx_message_wait(&byt->ipc, header, NULL, 0, NULL, 0);
444 dev_err(byt->dev, "ipc: free stream %d failed\n",
449 stream->commited = false;
451 spin_lock_irqsave(&sst->spinlock, flags);
452 list_del(&stream->node);
454 spin_unlock_irqrestore(&sst->spinlock, flags);
459 static int sst_byt_stream_operations(struct sst_byt *byt, int type,
460 int stream_id, int wait)
464 header = sst_byt_header(type, 0, false, stream_id);
466 return sst_ipc_tx_message_wait(&byt->ipc, header, NULL,
469 return sst_ipc_tx_message_nowait(&byt->ipc, header,
473 /* stream ALSA trigger operations */
474 int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream,
477 struct sst_byt_start_stream_params start_stream;
483 start_stream.byte_offset = start_offset;
484 header = sst_byt_header(IPC_IA_START_STREAM,
485 sizeof(start_stream) + sizeof(u32),
486 true, stream->str_id);
487 tx_msg = &start_stream;
488 size = sizeof(start_stream);
490 ret = sst_ipc_tx_message_nowait(&byt->ipc, header, tx_msg, size);
492 dev_err(byt->dev, "ipc: error failed to start stream %d\n",
498 int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream)
502 /* don't stop streams that are not commited */
503 if (!stream->commited)
506 ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM,
509 dev_err(byt->dev, "ipc: error failed to stop stream %d\n",
514 int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream)
518 ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM,
521 dev_err(byt->dev, "ipc: error failed to pause stream %d\n",
527 int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream)
531 ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM,
534 dev_err(byt->dev, "ipc: error failed to resume stream %d\n",
540 int sst_byt_get_dsp_position(struct sst_byt *byt,
541 struct sst_byt_stream *stream, int buffer_size)
543 struct sst_dsp *sst = byt->dsp;
544 struct sst_byt_tstamp fw_tstamp;
545 u8 str_id = stream->str_id;
548 tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp);
549 memcpy_fromio(&fw_tstamp,
550 sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp));
552 return do_div(fw_tstamp.ring_buffer_counter, buffer_size);
555 struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt)
560 static struct sst_dsp_device byt_dev = {
561 .thread = sst_byt_irq_thread,
565 int sst_byt_dsp_suspend_late(struct device *dev, struct sst_pdata *pdata)
567 struct sst_byt *byt = pdata->dsp;
569 dev_dbg(byt->dev, "dsp reset\n");
570 sst_dsp_reset(byt->dsp);
571 sst_ipc_drop_all(&byt->ipc);
572 dev_dbg(byt->dev, "dsp in reset\n");
574 dev_dbg(byt->dev, "free all blocks and unload fw\n");
575 sst_fw_unload(byt->fw);
579 EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late);
581 int sst_byt_dsp_boot(struct device *dev, struct sst_pdata *pdata)
583 struct sst_byt *byt = pdata->dsp;
586 dev_dbg(byt->dev, "reload dsp fw\n");
588 sst_dsp_reset(byt->dsp);
590 ret = sst_fw_reload(byt->fw);
592 dev_err(dev, "error: failed to reload firmware\n");
596 /* wait for DSP boot completion */
597 byt->boot_complete = false;
598 sst_dsp_boot(byt->dsp);
599 dev_dbg(byt->dev, "dsp booting...\n");
603 EXPORT_SYMBOL_GPL(sst_byt_dsp_boot);
605 int sst_byt_dsp_wait_for_ready(struct device *dev, struct sst_pdata *pdata)
607 struct sst_byt *byt = pdata->dsp;
610 dev_dbg(byt->dev, "wait for dsp reboot\n");
612 err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
613 msecs_to_jiffies(IPC_BOOT_MSECS));
615 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
619 dev_dbg(byt->dev, "dsp rebooted\n");
622 EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready);
624 static void byt_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
626 if (msg->header & IPC_HEADER_LARGE(true))
627 sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
629 sst_dsp_shim_write64_unlocked(ipc->dsp, SST_IPCX, msg->header);
632 static void byt_shim_dbg(struct sst_generic_ipc *ipc, const char *text)
634 struct sst_dsp *sst = ipc->dsp;
635 u64 isr, ipcd, imrx, ipcx;
637 ipcx = sst_dsp_shim_read64_unlocked(sst, SST_IPCX);
638 isr = sst_dsp_shim_read64_unlocked(sst, SST_ISRX);
639 ipcd = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
640 imrx = sst_dsp_shim_read64_unlocked(sst, SST_IMRX);
643 "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
644 text, ipcx, isr, ipcd, imrx);
647 static void byt_tx_data_copy(struct ipc_message *msg, char *tx_data,
650 /* msg content = lower 32-bit of the header + data */
651 *(u32 *)msg->tx_data = (u32)(msg->header & (u32)-1);
652 memcpy(msg->tx_data + sizeof(u32), tx_data, tx_size);
653 msg->tx_size += sizeof(u32);
656 static u64 byt_reply_msg_match(u64 header, u64 *mask)
658 /* match reply to message sent based on msg and stream IDs */
659 *mask = IPC_HEADER_MSG_ID_MASK |
660 IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT;
666 static bool byt_is_dsp_busy(struct sst_dsp *dsp)
670 ipcx = sst_dsp_shim_read_unlocked(dsp, SST_IPCX);
671 return (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE));
674 int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata)
677 struct sst_generic_ipc *ipc;
678 struct sst_fw *byt_sst_fw;
679 struct sst_byt_fw_init init;
682 dev_dbg(dev, "initialising Byt DSP IPC\n");
684 byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL);
692 ipc->ops.tx_msg = byt_tx_msg;
693 ipc->ops.shim_dbg = byt_shim_dbg;
694 ipc->ops.tx_data_copy = byt_tx_data_copy;
695 ipc->ops.reply_msg_match = byt_reply_msg_match;
696 ipc->ops.is_dsp_busy = byt_is_dsp_busy;
697 ipc->tx_data_max_size = IPC_MAX_MAILBOX_BYTES;
698 ipc->rx_data_max_size = IPC_MAX_MAILBOX_BYTES;
700 err = sst_ipc_init(ipc);
704 INIT_LIST_HEAD(&byt->stream_list);
705 init_waitqueue_head(&byt->boot_wait);
706 byt_dev.thread_context = byt;
709 byt->dsp = sst_dsp_new(dev, &byt_dev, pdata);
710 if (byt->dsp == NULL) {
717 /* keep the DSP in reset state for base FW loading */
718 sst_dsp_reset(byt->dsp);
720 byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt);
721 if (byt_sst_fw == NULL) {
723 dev_err(dev, "error: failed to load firmware\n");
727 /* wait for DSP boot completion */
728 sst_dsp_boot(byt->dsp);
729 err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
730 msecs_to_jiffies(IPC_BOOT_MSECS));
733 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
737 /* show firmware information */
738 sst_dsp_inbox_read(byt->dsp, &init, sizeof(init));
739 dev_info(byt->dev, "FW version: %02x.%02x.%02x.%02x\n",
740 init.fw_version.major, init.fw_version.minor,
741 init.fw_version.build, init.fw_version.type);
742 dev_info(byt->dev, "Build type: %x\n", init.fw_version.type);
743 dev_info(byt->dev, "Build date: %s %s\n",
744 init.build_info.date, init.build_info.time);
747 byt->fw = byt_sst_fw;
752 sst_dsp_reset(byt->dsp);
753 sst_fw_free(byt_sst_fw);
755 sst_dsp_free(byt->dsp);
762 EXPORT_SYMBOL_GPL(sst_byt_dsp_init);
764 void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata)
766 struct sst_byt *byt = pdata->dsp;
768 sst_dsp_reset(byt->dsp);
769 sst_fw_free_all(byt->dsp);
770 sst_dsp_free(byt->dsp);
771 sst_ipc_fini(&byt->ipc);
773 EXPORT_SYMBOL_GPL(sst_byt_dsp_free);