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
)
189 header
= IPC_HEADER_MSG_ID(msg_id
) |
190 IPC_HEADER_STR_ID(str_id
) |
191 IPC_HEADER_LARGE(large
) |
192 IPC_HEADER_DATA(data
) |
198 static inline u16
sst_byt_header_msg_id(u64 header
)
200 return header
& IPC_HEADER_MSG_ID_MASK
;
203 static inline u8
sst_byt_header_str_id(u64 header
)
205 return (header
>> IPC_HEADER_STR_ID_SHIFT
) & IPC_HEADER_STR_ID_MASK
;
208 static inline u16
sst_byt_header_data(u64 header
)
210 return (header
>> IPC_HEADER_DATA_SHIFT
) & IPC_HEADER_DATA_MASK
;
213 static struct sst_byt_stream
*sst_byt_get_stream(struct sst_byt
*byt
,
216 struct sst_byt_stream
*stream
;
218 list_for_each_entry(stream
, &byt
->stream_list
, node
) {
219 if (stream
->str_id
== stream_id
)
226 static void sst_byt_stream_update(struct sst_byt
*byt
, struct ipc_message
*msg
)
228 struct sst_byt_stream
*stream
;
229 u64 header
= msg
->header
;
230 u8 stream_id
= sst_byt_header_str_id(header
);
231 u8 stream_msg
= sst_byt_header_msg_id(header
);
233 stream
= sst_byt_get_stream(byt
, stream_id
);
237 switch (stream_msg
) {
238 case IPC_IA_DROP_STREAM
:
239 case IPC_IA_PAUSE_STREAM
:
240 case IPC_IA_FREE_STREAM
:
241 stream
->running
= false;
243 case IPC_IA_START_STREAM
:
244 case IPC_IA_RESUME_STREAM
:
245 stream
->running
= true;
250 static int sst_byt_process_reply(struct sst_byt
*byt
, u64 header
)
252 struct ipc_message
*msg
;
254 msg
= sst_ipc_reply_find_msg(&byt
->ipc
, header
);
258 if (header
& IPC_HEADER_LARGE(true)) {
259 msg
->rx_size
= sst_byt_header_data(header
);
260 sst_dsp_inbox_read(byt
->dsp
, msg
->rx_data
, msg
->rx_size
);
263 /* update any stream states */
264 sst_byt_stream_update(byt
, msg
);
266 list_del(&msg
->list
);
268 sst_ipc_tx_msg_reply_complete(&byt
->ipc
, msg
);
273 static void sst_byt_fw_ready(struct sst_byt
*byt
, u64 header
)
275 dev_dbg(byt
->dev
, "ipc: DSP is ready 0x%llX\n", header
);
277 byt
->boot_complete
= true;
278 wake_up(&byt
->boot_wait
);
281 static int sst_byt_process_notification(struct sst_byt
*byt
,
282 unsigned long *flags
)
284 struct sst_dsp
*sst
= byt
->dsp
;
285 struct sst_byt_stream
*stream
;
287 u8 msg_id
, stream_id
;
290 header
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
291 msg_id
= sst_byt_header_msg_id(header
);
294 case IPC_SST_PERIOD_ELAPSED
:
295 stream_id
= sst_byt_header_str_id(header
);
296 stream
= sst_byt_get_stream(byt
, stream_id
);
297 if (stream
&& stream
->running
&& stream
->notify_position
) {
298 spin_unlock_irqrestore(&sst
->spinlock
, *flags
);
299 stream
->notify_position(stream
, stream
->pdata
);
300 spin_lock_irqsave(&sst
->spinlock
, *flags
);
303 case IPC_IA_FW_INIT_CMPLT
:
304 sst_byt_fw_ready(byt
, header
);
311 static irqreturn_t
sst_byt_irq_thread(int irq
, void *context
)
313 struct sst_dsp
*sst
= (struct sst_dsp
*) context
;
314 struct sst_byt
*byt
= sst_dsp_get_thread_context(sst
);
315 struct sst_generic_ipc
*ipc
= &byt
->ipc
;
319 spin_lock_irqsave(&sst
->spinlock
, flags
);
321 header
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
322 if (header
& SST_BYT_IPCD_BUSY
) {
323 if (header
& IPC_NOTIFICATION
) {
324 /* message from ADSP */
325 sst_byt_process_notification(byt
, &flags
);
327 /* reply from ADSP */
328 sst_byt_process_reply(byt
, header
);
331 * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
332 * processed the message and can accept new. Clear data part
335 sst_dsp_shim_update_bits64_unlocked(sst
, SST_IPCD
,
336 SST_BYT_IPCD_DONE
| SST_BYT_IPCD_BUSY
|
337 IPC_HEADER_DATA(IPC_HEADER_DATA_MASK
),
339 /* unmask message request interrupts */
340 sst_dsp_shim_update_bits64_unlocked(sst
, SST_IMRX
,
341 SST_BYT_IMRX_REQUEST
, 0);
344 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
346 /* continue to send any remaining messages... */
347 queue_kthread_work(&ipc
->kworker
, &ipc
->kwork
);
353 struct sst_byt_stream
*sst_byt_stream_new(struct sst_byt
*byt
, int id
,
354 u32 (*notify_position
)(struct sst_byt_stream
*stream
, void *data
),
357 struct sst_byt_stream
*stream
;
358 struct sst_dsp
*sst
= byt
->dsp
;
361 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
365 spin_lock_irqsave(&sst
->spinlock
, flags
);
366 list_add(&stream
->node
, &byt
->stream_list
);
367 stream
->notify_position
= notify_position
;
368 stream
->pdata
= data
;
371 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
376 int sst_byt_stream_set_bits(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
379 stream
->request
.pcm_params
.pcm_wd_sz
= bits
;
383 int sst_byt_stream_set_channels(struct sst_byt
*byt
,
384 struct sst_byt_stream
*stream
, u8 channels
)
386 stream
->request
.pcm_params
.num_chan
= channels
;
390 int sst_byt_stream_set_rate(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
393 stream
->request
.pcm_params
.sfreq
= rate
;
397 /* stream sonfiguration */
398 int sst_byt_stream_type(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
399 int codec_type
, int stream_type
, int operation
)
401 stream
->request
.str_type
.codec_type
= codec_type
;
402 stream
->request
.str_type
.str_type
= stream_type
;
403 stream
->request
.str_type
.operation
= operation
;
404 stream
->request
.str_type
.time_slots
= 0xc;
409 int sst_byt_stream_buffer(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
410 uint32_t buffer_addr
, uint32_t buffer_size
)
412 stream
->request
.frame_info
.num_entries
= 1;
413 stream
->request
.frame_info
.ring_buf_info
[0].addr
= buffer_addr
;
414 stream
->request
.frame_info
.ring_buf_info
[0].size
= buffer_size
;
415 /* calculate bytes per 4 ms fragment */
416 stream
->request
.frame_info
.frag_size
=
417 stream
->request
.pcm_params
.sfreq
*
418 stream
->request
.pcm_params
.num_chan
*
419 stream
->request
.pcm_params
.pcm_wd_sz
/ 8 *
424 int sst_byt_stream_commit(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
426 struct sst_byt_alloc_params
*str_req
= &stream
->request
;
427 struct sst_byt_alloc_response
*reply
= &stream
->reply
;
431 header
= sst_byt_header(IPC_IA_ALLOC_STREAM
,
432 sizeof(*str_req
) + sizeof(u32
),
433 true, stream
->str_id
);
434 ret
= sst_ipc_tx_message_wait(&byt
->ipc
, header
, str_req
,
436 reply
, sizeof(*reply
));
438 dev_err(byt
->dev
, "ipc: error stream commit failed\n");
442 stream
->commited
= true;
447 int sst_byt_stream_free(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
451 struct sst_dsp
*sst
= byt
->dsp
;
454 if (!stream
->commited
)
457 header
= sst_byt_header(IPC_IA_FREE_STREAM
, 0, false, stream
->str_id
);
458 ret
= sst_ipc_tx_message_wait(&byt
->ipc
, header
, NULL
, 0, NULL
, 0);
460 dev_err(byt
->dev
, "ipc: free stream %d failed\n",
465 stream
->commited
= false;
467 spin_lock_irqsave(&sst
->spinlock
, flags
);
468 list_del(&stream
->node
);
470 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
475 static int sst_byt_stream_operations(struct sst_byt
*byt
, int type
,
476 int stream_id
, int wait
)
480 header
= sst_byt_header(type
, 0, false, stream_id
);
482 return sst_ipc_tx_message_wait(&byt
->ipc
, header
, NULL
,
485 return sst_ipc_tx_message_nowait(&byt
->ipc
, header
,
489 /* stream ALSA trigger operations */
490 int sst_byt_stream_start(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
493 struct sst_byt_start_stream_params start_stream
;
499 start_stream
.byte_offset
= start_offset
;
500 header
= sst_byt_header(IPC_IA_START_STREAM
,
501 sizeof(start_stream
) + sizeof(u32
),
502 true, stream
->str_id
);
503 tx_msg
= &start_stream
;
504 size
= sizeof(start_stream
);
506 ret
= sst_ipc_tx_message_nowait(&byt
->ipc
, header
, tx_msg
, size
);
508 dev_err(byt
->dev
, "ipc: error failed to start stream %d\n",
514 int sst_byt_stream_stop(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
518 /* don't stop streams that are not commited */
519 if (!stream
->commited
)
522 ret
= sst_byt_stream_operations(byt
, IPC_IA_DROP_STREAM
,
525 dev_err(byt
->dev
, "ipc: error failed to stop stream %d\n",
530 int sst_byt_stream_pause(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
534 ret
= sst_byt_stream_operations(byt
, IPC_IA_PAUSE_STREAM
,
537 dev_err(byt
->dev
, "ipc: error failed to pause stream %d\n",
543 int sst_byt_stream_resume(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
547 ret
= sst_byt_stream_operations(byt
, IPC_IA_RESUME_STREAM
,
550 dev_err(byt
->dev
, "ipc: error failed to resume stream %d\n",
556 int sst_byt_get_dsp_position(struct sst_byt
*byt
,
557 struct sst_byt_stream
*stream
, int buffer_size
)
559 struct sst_dsp
*sst
= byt
->dsp
;
560 struct sst_byt_tstamp fw_tstamp
;
561 u8 str_id
= stream
->str_id
;
564 tstamp_offset
= SST_BYT_TIMESTAMP_OFFSET
+ str_id
* sizeof(fw_tstamp
);
565 memcpy_fromio(&fw_tstamp
,
566 sst
->addr
.lpe
+ tstamp_offset
, sizeof(fw_tstamp
));
568 return do_div(fw_tstamp
.ring_buffer_counter
, buffer_size
);
571 struct sst_dsp
*sst_byt_get_dsp(struct sst_byt
*byt
)
576 static struct sst_dsp_device byt_dev
= {
577 .thread
= sst_byt_irq_thread
,
581 int sst_byt_dsp_suspend_late(struct device
*dev
, struct sst_pdata
*pdata
)
583 struct sst_byt
*byt
= pdata
->dsp
;
585 dev_dbg(byt
->dev
, "dsp reset\n");
586 sst_dsp_reset(byt
->dsp
);
587 sst_ipc_drop_all(&byt
->ipc
);
588 dev_dbg(byt
->dev
, "dsp in reset\n");
590 dev_dbg(byt
->dev
, "free all blocks and unload fw\n");
591 sst_fw_unload(byt
->fw
);
595 EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late
);
597 int sst_byt_dsp_boot(struct device
*dev
, struct sst_pdata
*pdata
)
599 struct sst_byt
*byt
= pdata
->dsp
;
602 dev_dbg(byt
->dev
, "reload dsp fw\n");
604 sst_dsp_reset(byt
->dsp
);
606 ret
= sst_fw_reload(byt
->fw
);
608 dev_err(dev
, "error: failed to reload firmware\n");
612 /* wait for DSP boot completion */
613 byt
->boot_complete
= false;
614 sst_dsp_boot(byt
->dsp
);
615 dev_dbg(byt
->dev
, "dsp booting...\n");
619 EXPORT_SYMBOL_GPL(sst_byt_dsp_boot
);
621 int sst_byt_dsp_wait_for_ready(struct device
*dev
, struct sst_pdata
*pdata
)
623 struct sst_byt
*byt
= pdata
->dsp
;
626 dev_dbg(byt
->dev
, "wait for dsp reboot\n");
628 err
= wait_event_timeout(byt
->boot_wait
, byt
->boot_complete
,
629 msecs_to_jiffies(IPC_BOOT_MSECS
));
631 dev_err(byt
->dev
, "ipc: error DSP boot timeout\n");
635 dev_dbg(byt
->dev
, "dsp rebooted\n");
638 EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready
);
640 static void byt_tx_msg(struct sst_generic_ipc
*ipc
, struct ipc_message
*msg
)
642 if (msg
->header
& IPC_HEADER_LARGE(true))
643 sst_dsp_outbox_write(ipc
->dsp
, msg
->tx_data
, msg
->tx_size
);
645 sst_dsp_shim_write64_unlocked(ipc
->dsp
, SST_IPCX
, msg
->header
);
648 static void byt_shim_dbg(struct sst_generic_ipc
*ipc
, const char *text
)
650 struct sst_dsp
*sst
= ipc
->dsp
;
651 u64 isr
, ipcd
, imrx
, ipcx
;
653 ipcx
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCX
);
654 isr
= sst_dsp_shim_read64_unlocked(sst
, SST_ISRX
);
655 ipcd
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
656 imrx
= sst_dsp_shim_read64_unlocked(sst
, SST_IMRX
);
659 "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
660 text
, ipcx
, isr
, ipcd
, imrx
);
663 static void byt_tx_data_copy(struct ipc_message
*msg
, char *tx_data
,
666 /* msg content = lower 32-bit of the header + data */
667 *(u32
*)msg
->tx_data
= (u32
)(msg
->header
& (u32
)-1);
668 memcpy(msg
->tx_data
+ sizeof(u32
), tx_data
, tx_size
);
669 msg
->tx_size
+= sizeof(u32
);
672 static u64
byt_reply_msg_match(u64 header
, u64
*mask
)
674 /* match reply to message sent based on msg and stream IDs */
675 *mask
= IPC_HEADER_MSG_ID_MASK
|
676 IPC_HEADER_STR_ID_MASK
<< IPC_HEADER_STR_ID_SHIFT
;
682 static bool byt_is_dsp_busy(struct sst_dsp
*dsp
)
686 ipcx
= sst_dsp_shim_read_unlocked(dsp
, SST_IPCX
);
687 return (ipcx
& (SST_IPCX_BUSY
| SST_IPCX_DONE
));
690 int sst_byt_dsp_init(struct device
*dev
, struct sst_pdata
*pdata
)
693 struct sst_generic_ipc
*ipc
;
694 struct sst_fw
*byt_sst_fw
;
695 struct sst_byt_fw_init init
;
698 dev_dbg(dev
, "initialising Byt DSP IPC\n");
700 byt
= devm_kzalloc(dev
, sizeof(*byt
), GFP_KERNEL
);
708 ipc
->ops
.tx_msg
= byt_tx_msg
;
709 ipc
->ops
.shim_dbg
= byt_shim_dbg
;
710 ipc
->ops
.tx_data_copy
= byt_tx_data_copy
;
711 ipc
->ops
.reply_msg_match
= byt_reply_msg_match
;
712 ipc
->ops
.is_dsp_busy
= byt_is_dsp_busy
;
713 ipc
->tx_data_max_size
= IPC_MAX_MAILBOX_BYTES
;
714 ipc
->rx_data_max_size
= IPC_MAX_MAILBOX_BYTES
;
716 err
= sst_ipc_init(ipc
);
720 INIT_LIST_HEAD(&byt
->stream_list
);
721 init_waitqueue_head(&byt
->boot_wait
);
722 byt_dev
.thread_context
= byt
;
725 byt
->dsp
= sst_dsp_new(dev
, &byt_dev
, pdata
);
726 if (byt
->dsp
== NULL
) {
733 /* keep the DSP in reset state for base FW loading */
734 sst_dsp_reset(byt
->dsp
);
736 byt_sst_fw
= sst_fw_new(byt
->dsp
, pdata
->fw
, byt
);
737 if (byt_sst_fw
== NULL
) {
739 dev_err(dev
, "error: failed to load firmware\n");
743 /* wait for DSP boot completion */
744 sst_dsp_boot(byt
->dsp
);
745 err
= wait_event_timeout(byt
->boot_wait
, byt
->boot_complete
,
746 msecs_to_jiffies(IPC_BOOT_MSECS
));
749 dev_err(byt
->dev
, "ipc: error DSP boot timeout\n");
753 /* show firmware information */
754 sst_dsp_inbox_read(byt
->dsp
, &init
, sizeof(init
));
755 dev_info(byt
->dev
, "FW version: %02x.%02x.%02x.%02x\n",
756 init
.fw_version
.major
, init
.fw_version
.minor
,
757 init
.fw_version
.build
, init
.fw_version
.type
);
758 dev_info(byt
->dev
, "Build type: %x\n", init
.fw_version
.type
);
759 dev_info(byt
->dev
, "Build date: %s %s\n",
760 init
.build_info
.date
, init
.build_info
.time
);
763 byt
->fw
= byt_sst_fw
;
768 sst_dsp_reset(byt
->dsp
);
769 sst_fw_free(byt_sst_fw
);
771 sst_dsp_free(byt
->dsp
);
778 EXPORT_SYMBOL_GPL(sst_byt_dsp_init
);
780 void sst_byt_dsp_free(struct device
*dev
, struct sst_pdata
*pdata
)
782 struct sst_byt
*byt
= pdata
->dsp
;
784 sst_dsp_reset(byt
->dsp
);
785 sst_fw_free_all(byt
->dsp
);
786 sst_dsp_free(byt
->dsp
);
787 sst_ipc_fini(&byt
->ipc
);
789 EXPORT_SYMBOL_GPL(sst_byt_dsp_free
);