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/list.h>
26 #include <linux/platform_device.h>
27 #include <linux/kthread.h>
28 #include <linux/firmware.h>
30 #include <asm/div64.h>
32 #include "sst-baytrail-ipc.h"
34 #include "sst-dsp-priv.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 /* driver internal IPC message structure */
128 struct list_head list;
131 /* direction wrt host CPU */
132 char tx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE];
134 char rx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE];
137 wait_queue_head_t waitq;
143 struct sst_byt_stream;
146 /* stream infomation */
147 struct sst_byt_stream {
148 struct list_head node;
151 struct sst_byt_alloc_params request;
152 struct sst_byt_alloc_response reply;
160 /* driver callback */
161 u32 (*notify_position)(struct sst_byt_stream *stream, void *data);
165 /* SST Baytrail IPC data */
171 struct list_head stream_list;
174 wait_queue_head_t boot_wait;
178 struct list_head tx_list;
179 struct list_head rx_list;
180 struct list_head empty_list;
181 wait_queue_head_t wait_txq;
182 struct task_struct *tx_thread;
183 struct kthread_worker kworker;
184 struct kthread_work kwork;
185 struct ipc_message *msg;
188 static inline u64 sst_byt_header(int msg_id, int data, bool large, int str_id)
192 header = IPC_HEADER_MSG_ID(msg_id) |
193 IPC_HEADER_STR_ID(str_id) |
194 IPC_HEADER_LARGE(large) |
195 IPC_HEADER_DATA(data) |
201 static inline u16 sst_byt_header_msg_id(u64 header)
203 return header & IPC_HEADER_MSG_ID_MASK;
206 static inline u8 sst_byt_header_str_id(u64 header)
208 return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK;
211 static inline u16 sst_byt_header_data(u64 header)
213 return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK;
216 static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt,
219 struct sst_byt_stream *stream;
221 list_for_each_entry(stream, &byt->stream_list, node) {
222 if (stream->str_id == stream_id)
229 static void sst_byt_ipc_shim_dbg(struct sst_byt *byt, const char *text)
231 struct sst_dsp *sst = byt->dsp;
232 u64 isr, ipcd, imrx, ipcx;
234 ipcx = sst_dsp_shim_read64_unlocked(sst, SST_IPCX);
235 isr = sst_dsp_shim_read64_unlocked(sst, SST_ISRX);
236 ipcd = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
237 imrx = sst_dsp_shim_read64_unlocked(sst, SST_IMRX);
240 "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
241 text, ipcx, isr, ipcd, imrx);
244 /* locks held by caller */
245 static struct ipc_message *sst_byt_msg_get_empty(struct sst_byt *byt)
247 struct ipc_message *msg = NULL;
249 if (!list_empty(&byt->empty_list)) {
250 msg = list_first_entry(&byt->empty_list,
251 struct ipc_message, list);
252 list_del(&msg->list);
258 static void sst_byt_ipc_tx_msgs(struct kthread_work *work)
260 struct sst_byt *byt =
261 container_of(work, struct sst_byt, kwork);
262 struct ipc_message *msg;
266 spin_lock_irqsave(&byt->dsp->spinlock, flags);
267 if (list_empty(&byt->tx_list)) {
268 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
272 /* if the DSP is busy we will TX messages after IRQ */
273 ipcx = sst_dsp_shim_read64_unlocked(byt->dsp, SST_IPCX);
274 if (ipcx & SST_BYT_IPCX_BUSY) {
275 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
279 msg = list_first_entry(&byt->tx_list, struct ipc_message, list);
281 list_move(&msg->list, &byt->rx_list);
283 /* send the message */
284 if (msg->header & IPC_HEADER_LARGE(true))
285 sst_dsp_outbox_write(byt->dsp, msg->tx_data, msg->tx_size);
286 sst_dsp_shim_write64_unlocked(byt->dsp, SST_IPCX, msg->header);
288 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
291 static inline void sst_byt_tx_msg_reply_complete(struct sst_byt *byt,
292 struct ipc_message *msg)
294 msg->complete = true;
297 list_add_tail(&msg->list, &byt->empty_list);
299 wake_up(&msg->waitq);
302 static int sst_byt_tx_wait_done(struct sst_byt *byt, struct ipc_message *msg,
308 /* wait for DSP completion */
309 ret = wait_event_timeout(msg->waitq, msg->complete,
310 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
312 spin_lock_irqsave(&byt->dsp->spinlock, flags);
314 list_del(&msg->list);
315 sst_byt_ipc_shim_dbg(byt, "message timeout");
320 /* copy the data returned from DSP */
322 memcpy(rx_data, msg->rx_data, msg->rx_size);
326 list_add_tail(&msg->list, &byt->empty_list);
327 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
331 static int sst_byt_ipc_tx_message(struct sst_byt *byt, u64 header,
332 void *tx_data, size_t tx_bytes,
333 void *rx_data, size_t rx_bytes, int wait)
336 struct ipc_message *msg;
338 spin_lock_irqsave(&byt->dsp->spinlock, flags);
340 msg = sst_byt_msg_get_empty(byt);
342 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
346 msg->header = header;
347 msg->tx_size = tx_bytes;
348 msg->rx_size = rx_bytes;
351 msg->complete = false;
354 /* msg content = lower 32-bit of the header + data */
355 *(u32 *)msg->tx_data = (u32)(header & (u32)-1);
356 memcpy(msg->tx_data + sizeof(u32), tx_data, tx_bytes);
357 msg->tx_size += sizeof(u32);
360 list_add_tail(&msg->list, &byt->tx_list);
361 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
363 queue_kthread_work(&byt->kworker, &byt->kwork);
366 return sst_byt_tx_wait_done(byt, msg, rx_data);
371 static inline int sst_byt_ipc_tx_msg_wait(struct sst_byt *byt, u64 header,
372 void *tx_data, size_t tx_bytes,
373 void *rx_data, size_t rx_bytes)
375 return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes,
376 rx_data, rx_bytes, 1);
379 static inline int sst_byt_ipc_tx_msg_nowait(struct sst_byt *byt, u64 header,
380 void *tx_data, size_t tx_bytes)
382 return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes,
386 static struct ipc_message *sst_byt_reply_find_msg(struct sst_byt *byt,
389 struct ipc_message *msg = NULL, *_msg;
392 /* match reply to message sent based on msg and stream IDs */
393 mask = IPC_HEADER_MSG_ID_MASK |
394 IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT;
397 if (list_empty(&byt->rx_list)) {
399 "ipc: rx list is empty but received 0x%llx\n", header);
403 list_for_each_entry(_msg, &byt->rx_list, list) {
404 if ((_msg->header & mask) == header) {
414 static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg)
416 struct sst_byt_stream *stream;
417 u64 header = msg->header;
418 u8 stream_id = sst_byt_header_str_id(header);
419 u8 stream_msg = sst_byt_header_msg_id(header);
421 stream = sst_byt_get_stream(byt, stream_id);
425 switch (stream_msg) {
426 case IPC_IA_DROP_STREAM:
427 case IPC_IA_PAUSE_STREAM:
428 case IPC_IA_FREE_STREAM:
429 stream->running = false;
431 case IPC_IA_START_STREAM:
432 case IPC_IA_RESUME_STREAM:
433 stream->running = true;
438 static int sst_byt_process_reply(struct sst_byt *byt, u64 header)
440 struct ipc_message *msg;
442 msg = sst_byt_reply_find_msg(byt, header);
446 if (header & IPC_HEADER_LARGE(true)) {
447 msg->rx_size = sst_byt_header_data(header);
448 sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size);
451 /* update any stream states */
452 sst_byt_stream_update(byt, msg);
454 list_del(&msg->list);
456 sst_byt_tx_msg_reply_complete(byt, msg);
461 static void sst_byt_fw_ready(struct sst_byt *byt, u64 header)
463 dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header);
465 byt->boot_complete = true;
466 wake_up(&byt->boot_wait);
469 static int sst_byt_process_notification(struct sst_byt *byt,
470 unsigned long *flags)
472 struct sst_dsp *sst = byt->dsp;
473 struct sst_byt_stream *stream;
475 u8 msg_id, stream_id;
478 header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
479 msg_id = sst_byt_header_msg_id(header);
482 case IPC_SST_PERIOD_ELAPSED:
483 stream_id = sst_byt_header_str_id(header);
484 stream = sst_byt_get_stream(byt, stream_id);
485 if (stream && stream->running && stream->notify_position) {
486 spin_unlock_irqrestore(&sst->spinlock, *flags);
487 stream->notify_position(stream, stream->pdata);
488 spin_lock_irqsave(&sst->spinlock, *flags);
491 case IPC_IA_FW_INIT_CMPLT:
492 sst_byt_fw_ready(byt, header);
499 static irqreturn_t sst_byt_irq_thread(int irq, void *context)
501 struct sst_dsp *sst = (struct sst_dsp *) context;
502 struct sst_byt *byt = sst_dsp_get_thread_context(sst);
506 spin_lock_irqsave(&sst->spinlock, flags);
508 header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
509 if (header & SST_BYT_IPCD_BUSY) {
510 if (header & IPC_NOTIFICATION) {
511 /* message from ADSP */
512 sst_byt_process_notification(byt, &flags);
514 /* reply from ADSP */
515 sst_byt_process_reply(byt, header);
518 * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
519 * processed the message and can accept new. Clear data part
522 sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD,
523 SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY |
524 IPC_HEADER_DATA(IPC_HEADER_DATA_MASK),
526 /* unmask message request interrupts */
527 sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX,
528 SST_BYT_IMRX_REQUEST, 0);
531 spin_unlock_irqrestore(&sst->spinlock, flags);
533 /* continue to send any remaining messages... */
534 queue_kthread_work(&byt->kworker, &byt->kwork);
540 struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id,
541 u32 (*notify_position)(struct sst_byt_stream *stream, void *data),
544 struct sst_byt_stream *stream;
546 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
550 list_add(&stream->node, &byt->stream_list);
551 stream->notify_position = notify_position;
552 stream->pdata = data;
559 int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream,
562 stream->request.pcm_params.pcm_wd_sz = bits;
566 int sst_byt_stream_set_channels(struct sst_byt *byt,
567 struct sst_byt_stream *stream, u8 channels)
569 stream->request.pcm_params.num_chan = channels;
573 int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream,
576 stream->request.pcm_params.sfreq = rate;
580 /* stream sonfiguration */
581 int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream,
582 int codec_type, int stream_type, int operation)
584 stream->request.str_type.codec_type = codec_type;
585 stream->request.str_type.str_type = stream_type;
586 stream->request.str_type.operation = operation;
587 stream->request.str_type.time_slots = 0xc;
592 int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream,
593 uint32_t buffer_addr, uint32_t buffer_size)
595 stream->request.frame_info.num_entries = 1;
596 stream->request.frame_info.ring_buf_info[0].addr = buffer_addr;
597 stream->request.frame_info.ring_buf_info[0].size = buffer_size;
598 /* calculate bytes per 4 ms fragment */
599 stream->request.frame_info.frag_size =
600 stream->request.pcm_params.sfreq *
601 stream->request.pcm_params.num_chan *
602 stream->request.pcm_params.pcm_wd_sz / 8 *
607 int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream)
609 struct sst_byt_alloc_params *str_req = &stream->request;
610 struct sst_byt_alloc_response *reply = &stream->reply;
614 header = sst_byt_header(IPC_IA_ALLOC_STREAM,
615 sizeof(*str_req) + sizeof(u32),
616 true, stream->str_id);
617 ret = sst_byt_ipc_tx_msg_wait(byt, header, str_req, sizeof(*str_req),
618 reply, sizeof(*reply));
620 dev_err(byt->dev, "ipc: error stream commit failed\n");
624 stream->commited = true;
629 int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream)
634 if (!stream->commited)
637 header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id);
638 ret = sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0);
640 dev_err(byt->dev, "ipc: free stream %d failed\n",
645 stream->commited = false;
647 list_del(&stream->node);
653 static int sst_byt_stream_operations(struct sst_byt *byt, int type,
654 int stream_id, int wait)
656 struct sst_byt_start_stream_params start_stream;
661 if (type != IPC_IA_START_STREAM) {
662 header = sst_byt_header(type, 0, false, stream_id);
664 start_stream.byte_offset = 0;
665 header = sst_byt_header(IPC_IA_START_STREAM,
666 sizeof(start_stream) + sizeof(u32),
668 tx_msg = &start_stream;
669 size = sizeof(start_stream);
673 return sst_byt_ipc_tx_msg_wait(byt, header,
674 tx_msg, size, NULL, 0);
676 return sst_byt_ipc_tx_msg_nowait(byt, header, tx_msg, size);
679 /* stream ALSA trigger operations */
680 int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream)
684 ret = sst_byt_stream_operations(byt, IPC_IA_START_STREAM,
687 dev_err(byt->dev, "ipc: error failed to start stream %d\n",
693 int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream)
697 /* don't stop streams that are not commited */
698 if (!stream->commited)
701 ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM,
704 dev_err(byt->dev, "ipc: error failed to stop stream %d\n",
709 int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream)
713 ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM,
716 dev_err(byt->dev, "ipc: error failed to pause stream %d\n",
722 int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream)
726 ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM,
729 dev_err(byt->dev, "ipc: error failed to resume stream %d\n",
735 int sst_byt_get_dsp_position(struct sst_byt *byt,
736 struct sst_byt_stream *stream, int buffer_size)
738 struct sst_dsp *sst = byt->dsp;
739 struct sst_byt_tstamp fw_tstamp;
740 u8 str_id = stream->str_id;
743 tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp);
744 memcpy_fromio(&fw_tstamp,
745 sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp));
747 return do_div(fw_tstamp.ring_buffer_counter, buffer_size);
750 static int msg_empty_list_init(struct sst_byt *byt)
752 struct ipc_message *msg;
755 byt->msg = kzalloc(sizeof(*msg) * IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
756 if (byt->msg == NULL)
759 for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
760 init_waitqueue_head(&byt->msg[i].waitq);
761 list_add(&byt->msg[i].list, &byt->empty_list);
767 struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt)
772 static struct sst_dsp_device byt_dev = {
773 .thread = sst_byt_irq_thread,
777 int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata)
780 struct sst_fw *byt_sst_fw;
783 dev_dbg(dev, "initialising Byt DSP IPC\n");
785 byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL);
790 INIT_LIST_HEAD(&byt->stream_list);
791 INIT_LIST_HEAD(&byt->tx_list);
792 INIT_LIST_HEAD(&byt->rx_list);
793 INIT_LIST_HEAD(&byt->empty_list);
794 init_waitqueue_head(&byt->boot_wait);
795 init_waitqueue_head(&byt->wait_txq);
797 err = msg_empty_list_init(byt);
801 /* start the IPC message thread */
802 init_kthread_worker(&byt->kworker);
803 byt->tx_thread = kthread_run(kthread_worker_fn,
806 if (IS_ERR(byt->tx_thread)) {
807 err = PTR_ERR(byt->tx_thread);
808 dev_err(byt->dev, "error failed to create message TX task\n");
811 init_kthread_work(&byt->kwork, sst_byt_ipc_tx_msgs);
813 byt_dev.thread_context = byt;
816 byt->dsp = sst_dsp_new(dev, &byt_dev, pdata);
817 if (byt->dsp == NULL) {
822 /* keep the DSP in reset state for base FW loading */
823 sst_dsp_reset(byt->dsp);
825 byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt);
826 if (byt_sst_fw == NULL) {
828 dev_err(dev, "error: failed to load firmware\n");
832 /* wait for DSP boot completion */
833 sst_dsp_boot(byt->dsp);
834 err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
835 msecs_to_jiffies(IPC_BOOT_MSECS));
838 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
847 sst_dsp_reset(byt->dsp);
848 sst_fw_free(byt_sst_fw);
850 sst_dsp_free(byt->dsp);
856 EXPORT_SYMBOL_GPL(sst_byt_dsp_init);
858 void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata)
860 struct sst_byt *byt = pdata->dsp;
862 sst_dsp_reset(byt->dsp);
863 sst_fw_free_all(byt->dsp);
864 sst_dsp_free(byt->dsp);
867 EXPORT_SYMBOL_GPL(sst_byt_dsp_free);