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
->tx
.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 msg
->rx
.header
= header
;
244 if (header
& IPC_HEADER_LARGE(true)) {
245 msg
->rx
.size
= sst_byt_header_data(header
);
246 sst_dsp_inbox_read(byt
->dsp
, msg
->rx
.data
, msg
->rx
.size
);
249 /* update any stream states */
250 sst_byt_stream_update(byt
, msg
);
252 list_del(&msg
->list
);
254 sst_ipc_tx_msg_reply_complete(&byt
->ipc
, msg
);
259 static void sst_byt_fw_ready(struct sst_byt
*byt
, u64 header
)
261 dev_dbg(byt
->dev
, "ipc: DSP is ready 0x%llX\n", header
);
263 byt
->boot_complete
= true;
264 wake_up(&byt
->boot_wait
);
267 static int sst_byt_process_notification(struct sst_byt
*byt
,
268 unsigned long *flags
)
270 struct sst_dsp
*sst
= byt
->dsp
;
271 struct sst_byt_stream
*stream
;
273 u8 msg_id
, stream_id
;
275 header
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
276 msg_id
= sst_byt_header_msg_id(header
);
279 case IPC_SST_PERIOD_ELAPSED
:
280 stream_id
= sst_byt_header_str_id(header
);
281 stream
= sst_byt_get_stream(byt
, stream_id
);
282 if (stream
&& stream
->running
&& stream
->notify_position
) {
283 spin_unlock_irqrestore(&sst
->spinlock
, *flags
);
284 stream
->notify_position(stream
, stream
->pdata
);
285 spin_lock_irqsave(&sst
->spinlock
, *flags
);
288 case IPC_IA_FW_INIT_CMPLT
:
289 sst_byt_fw_ready(byt
, header
);
296 static irqreturn_t
sst_byt_irq_thread(int irq
, void *context
)
298 struct sst_dsp
*sst
= (struct sst_dsp
*) context
;
299 struct sst_byt
*byt
= sst_dsp_get_thread_context(sst
);
300 struct sst_generic_ipc
*ipc
= &byt
->ipc
;
304 spin_lock_irqsave(&sst
->spinlock
, flags
);
306 header
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
307 if (header
& SST_BYT_IPCD_BUSY
) {
308 if (header
& IPC_NOTIFICATION
) {
309 /* message from ADSP */
310 sst_byt_process_notification(byt
, &flags
);
312 /* reply from ADSP */
313 sst_byt_process_reply(byt
, header
);
316 * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
317 * processed the message and can accept new. Clear data part
320 sst_dsp_shim_update_bits64_unlocked(sst
, SST_IPCD
,
321 SST_BYT_IPCD_DONE
| SST_BYT_IPCD_BUSY
|
322 IPC_HEADER_DATA(IPC_HEADER_DATA_MASK
),
324 /* unmask message request interrupts */
325 sst_dsp_shim_update_bits64_unlocked(sst
, SST_IMRX
,
326 SST_BYT_IMRX_REQUEST
, 0);
329 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
331 /* continue to send any remaining messages... */
332 schedule_work(&ipc
->kwork
);
338 struct sst_byt_stream
*sst_byt_stream_new(struct sst_byt
*byt
, int id
,
339 u32 (*notify_position
)(struct sst_byt_stream
*stream
, void *data
),
342 struct sst_byt_stream
*stream
;
343 struct sst_dsp
*sst
= byt
->dsp
;
346 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
350 spin_lock_irqsave(&sst
->spinlock
, flags
);
351 list_add(&stream
->node
, &byt
->stream_list
);
352 stream
->notify_position
= notify_position
;
353 stream
->pdata
= data
;
356 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
361 int sst_byt_stream_set_bits(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
364 stream
->request
.pcm_params
.pcm_wd_sz
= bits
;
368 int sst_byt_stream_set_channels(struct sst_byt
*byt
,
369 struct sst_byt_stream
*stream
, u8 channels
)
371 stream
->request
.pcm_params
.num_chan
= channels
;
375 int sst_byt_stream_set_rate(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
378 stream
->request
.pcm_params
.sfreq
= rate
;
382 /* stream sonfiguration */
383 int sst_byt_stream_type(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
384 int codec_type
, int stream_type
, int operation
)
386 stream
->request
.str_type
.codec_type
= codec_type
;
387 stream
->request
.str_type
.str_type
= stream_type
;
388 stream
->request
.str_type
.operation
= operation
;
389 stream
->request
.str_type
.time_slots
= 0xc;
394 int sst_byt_stream_buffer(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
395 uint32_t buffer_addr
, uint32_t buffer_size
)
397 stream
->request
.frame_info
.num_entries
= 1;
398 stream
->request
.frame_info
.ring_buf_info
[0].addr
= buffer_addr
;
399 stream
->request
.frame_info
.ring_buf_info
[0].size
= buffer_size
;
400 /* calculate bytes per 4 ms fragment */
401 stream
->request
.frame_info
.frag_size
=
402 stream
->request
.pcm_params
.sfreq
*
403 stream
->request
.pcm_params
.num_chan
*
404 stream
->request
.pcm_params
.pcm_wd_sz
/ 8 *
409 int sst_byt_stream_commit(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
411 struct sst_ipc_message request
, reply
= {0};
414 request
.header
= sst_byt_header(IPC_IA_ALLOC_STREAM
,
415 sizeof(stream
->request
) + sizeof(u32
),
416 true, stream
->str_id
);
417 request
.data
= &stream
->request
;
418 request
.size
= sizeof(stream
->request
);
419 reply
.data
= &stream
->reply
;
420 reply
.size
= sizeof(stream
->reply
);
422 ret
= sst_ipc_tx_message_wait(&byt
->ipc
, request
, &reply
);
424 dev_err(byt
->dev
, "ipc: error stream commit failed\n");
428 stream
->commited
= true;
433 int sst_byt_stream_free(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
435 struct sst_ipc_message request
= {0};
437 struct sst_dsp
*sst
= byt
->dsp
;
440 if (!stream
->commited
)
443 request
.header
= sst_byt_header(IPC_IA_FREE_STREAM
,
444 0, false, stream
->str_id
);
445 ret
= sst_ipc_tx_message_wait(&byt
->ipc
, request
, NULL
);
447 dev_err(byt
->dev
, "ipc: free stream %d failed\n",
452 stream
->commited
= false;
454 spin_lock_irqsave(&sst
->spinlock
, flags
);
455 list_del(&stream
->node
);
457 spin_unlock_irqrestore(&sst
->spinlock
, flags
);
462 static int sst_byt_stream_operations(struct sst_byt
*byt
, int type
,
463 int stream_id
, int wait
)
465 struct sst_ipc_message request
= {0};
467 request
.header
= sst_byt_header(type
, 0, false, stream_id
);
469 return sst_ipc_tx_message_wait(&byt
->ipc
, request
, NULL
);
471 return sst_ipc_tx_message_nowait(&byt
->ipc
, request
);
474 /* stream ALSA trigger operations */
475 int sst_byt_stream_start(struct sst_byt
*byt
, struct sst_byt_stream
*stream
,
478 struct sst_byt_start_stream_params start_stream
;
479 struct sst_ipc_message request
;
482 start_stream
.byte_offset
= start_offset
;
483 request
.header
= sst_byt_header(IPC_IA_START_STREAM
,
484 sizeof(start_stream
) + sizeof(u32
),
485 true, stream
->str_id
);
486 request
.data
= &start_stream
;
487 request
.size
= sizeof(start_stream
);
489 ret
= sst_ipc_tx_message_nowait(&byt
->ipc
, request
);
491 dev_err(byt
->dev
, "ipc: error failed to start stream %d\n",
497 int sst_byt_stream_stop(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
501 /* don't stop streams that are not commited */
502 if (!stream
->commited
)
505 ret
= sst_byt_stream_operations(byt
, IPC_IA_DROP_STREAM
,
508 dev_err(byt
->dev
, "ipc: error failed to stop stream %d\n",
513 int sst_byt_stream_pause(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
517 ret
= sst_byt_stream_operations(byt
, IPC_IA_PAUSE_STREAM
,
520 dev_err(byt
->dev
, "ipc: error failed to pause stream %d\n",
526 int sst_byt_stream_resume(struct sst_byt
*byt
, struct sst_byt_stream
*stream
)
530 ret
= sst_byt_stream_operations(byt
, IPC_IA_RESUME_STREAM
,
533 dev_err(byt
->dev
, "ipc: error failed to resume stream %d\n",
539 int sst_byt_get_dsp_position(struct sst_byt
*byt
,
540 struct sst_byt_stream
*stream
, int buffer_size
)
542 struct sst_dsp
*sst
= byt
->dsp
;
543 struct sst_byt_tstamp fw_tstamp
;
544 u8 str_id
= stream
->str_id
;
547 tstamp_offset
= SST_BYT_TIMESTAMP_OFFSET
+ str_id
* sizeof(fw_tstamp
);
548 memcpy_fromio(&fw_tstamp
,
549 sst
->addr
.lpe
+ tstamp_offset
, sizeof(fw_tstamp
));
551 return do_div(fw_tstamp
.ring_buffer_counter
, buffer_size
);
554 struct sst_dsp
*sst_byt_get_dsp(struct sst_byt
*byt
)
559 static struct sst_dsp_device byt_dev
= {
560 .thread
= sst_byt_irq_thread
,
564 int sst_byt_dsp_suspend_late(struct device
*dev
, struct sst_pdata
*pdata
)
566 struct sst_byt
*byt
= pdata
->dsp
;
568 dev_dbg(byt
->dev
, "dsp reset\n");
569 sst_dsp_reset(byt
->dsp
);
570 sst_ipc_drop_all(&byt
->ipc
);
571 dev_dbg(byt
->dev
, "dsp in reset\n");
573 dev_dbg(byt
->dev
, "free all blocks and unload fw\n");
574 sst_fw_unload(byt
->fw
);
578 EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late
);
580 int sst_byt_dsp_boot(struct device
*dev
, struct sst_pdata
*pdata
)
582 struct sst_byt
*byt
= pdata
->dsp
;
585 dev_dbg(byt
->dev
, "reload dsp fw\n");
587 sst_dsp_reset(byt
->dsp
);
589 ret
= sst_fw_reload(byt
->fw
);
591 dev_err(dev
, "error: failed to reload firmware\n");
595 /* wait for DSP boot completion */
596 byt
->boot_complete
= false;
597 sst_dsp_boot(byt
->dsp
);
598 dev_dbg(byt
->dev
, "dsp booting...\n");
602 EXPORT_SYMBOL_GPL(sst_byt_dsp_boot
);
604 int sst_byt_dsp_wait_for_ready(struct device
*dev
, struct sst_pdata
*pdata
)
606 struct sst_byt
*byt
= pdata
->dsp
;
609 dev_dbg(byt
->dev
, "wait for dsp reboot\n");
611 err
= wait_event_timeout(byt
->boot_wait
, byt
->boot_complete
,
612 msecs_to_jiffies(IPC_BOOT_MSECS
));
614 dev_err(byt
->dev
, "ipc: error DSP boot timeout\n");
618 dev_dbg(byt
->dev
, "dsp rebooted\n");
621 EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready
);
623 static void byt_tx_msg(struct sst_generic_ipc
*ipc
, struct ipc_message
*msg
)
625 if (msg
->tx
.header
& IPC_HEADER_LARGE(true))
626 sst_dsp_outbox_write(ipc
->dsp
, msg
->tx
.data
, msg
->tx
.size
);
628 sst_dsp_shim_write64_unlocked(ipc
->dsp
, SST_IPCX
, msg
->tx
.header
);
631 static void byt_shim_dbg(struct sst_generic_ipc
*ipc
, const char *text
)
633 struct sst_dsp
*sst
= ipc
->dsp
;
634 u64 isr
, ipcd
, imrx
, ipcx
;
636 ipcx
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCX
);
637 isr
= sst_dsp_shim_read64_unlocked(sst
, SST_ISRX
);
638 ipcd
= sst_dsp_shim_read64_unlocked(sst
, SST_IPCD
);
639 imrx
= sst_dsp_shim_read64_unlocked(sst
, SST_IMRX
);
642 "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
643 text
, ipcx
, isr
, ipcd
, imrx
);
646 static void byt_tx_data_copy(struct ipc_message
*msg
, char *tx_data
,
649 /* msg content = lower 32-bit of the header + data */
650 *(u32
*)msg
->tx
.data
= (u32
)(msg
->tx
.header
& (u32
)-1);
651 memcpy(msg
->tx
.data
+ sizeof(u32
), tx_data
, tx_size
);
652 msg
->tx
.size
+= sizeof(u32
);
655 static u64
byt_reply_msg_match(u64 header
, u64
*mask
)
657 /* match reply to message sent based on msg and stream IDs */
658 *mask
= IPC_HEADER_MSG_ID_MASK
|
659 IPC_HEADER_STR_ID_MASK
<< IPC_HEADER_STR_ID_SHIFT
;
665 static bool byt_is_dsp_busy(struct sst_dsp
*dsp
)
669 ipcx
= sst_dsp_shim_read_unlocked(dsp
, SST_IPCX
);
670 return (ipcx
& (SST_IPCX_BUSY
| SST_IPCX_DONE
));
673 int sst_byt_dsp_init(struct device
*dev
, struct sst_pdata
*pdata
)
676 struct sst_generic_ipc
*ipc
;
677 struct sst_fw
*byt_sst_fw
;
678 struct sst_byt_fw_init init
;
681 dev_dbg(dev
, "initialising Byt DSP IPC\n");
683 byt
= devm_kzalloc(dev
, sizeof(*byt
), GFP_KERNEL
);
691 ipc
->ops
.tx_msg
= byt_tx_msg
;
692 ipc
->ops
.shim_dbg
= byt_shim_dbg
;
693 ipc
->ops
.tx_data_copy
= byt_tx_data_copy
;
694 ipc
->ops
.reply_msg_match
= byt_reply_msg_match
;
695 ipc
->ops
.is_dsp_busy
= byt_is_dsp_busy
;
696 ipc
->tx_data_max_size
= IPC_MAX_MAILBOX_BYTES
;
697 ipc
->rx_data_max_size
= IPC_MAX_MAILBOX_BYTES
;
699 err
= sst_ipc_init(ipc
);
703 INIT_LIST_HEAD(&byt
->stream_list
);
704 init_waitqueue_head(&byt
->boot_wait
);
705 byt_dev
.thread_context
= byt
;
708 byt
->dsp
= sst_dsp_new(dev
, &byt_dev
, pdata
);
709 if (byt
->dsp
== NULL
) {
716 /* keep the DSP in reset state for base FW loading */
717 sst_dsp_reset(byt
->dsp
);
719 byt_sst_fw
= sst_fw_new(byt
->dsp
, pdata
->fw
, byt
);
720 if (byt_sst_fw
== NULL
) {
722 dev_err(dev
, "error: failed to load firmware\n");
726 /* wait for DSP boot completion */
727 sst_dsp_boot(byt
->dsp
);
728 err
= wait_event_timeout(byt
->boot_wait
, byt
->boot_complete
,
729 msecs_to_jiffies(IPC_BOOT_MSECS
));
732 dev_err(byt
->dev
, "ipc: error DSP boot timeout\n");
736 /* show firmware information */
737 sst_dsp_inbox_read(byt
->dsp
, &init
, sizeof(init
));
738 dev_info(byt
->dev
, "FW version: %02x.%02x.%02x.%02x\n",
739 init
.fw_version
.major
, init
.fw_version
.minor
,
740 init
.fw_version
.build
, init
.fw_version
.type
);
741 dev_info(byt
->dev
, "Build type: %x\n", init
.fw_version
.type
);
742 dev_info(byt
->dev
, "Build date: %s %s\n",
743 init
.build_info
.date
, init
.build_info
.time
);
746 byt
->fw
= byt_sst_fw
;
751 sst_dsp_reset(byt
->dsp
);
752 sst_fw_free(byt_sst_fw
);
754 sst_dsp_free(byt
->dsp
);
761 EXPORT_SYMBOL_GPL(sst_byt_dsp_init
);
763 void sst_byt_dsp_free(struct device
*dev
, struct sst_pdata
*pdata
)
765 struct sst_byt
*byt
= pdata
->dsp
;
767 sst_dsp_reset(byt
->dsp
);
768 sst_fw_free_all(byt
->dsp
);
769 sst_dsp_free(byt
->dsp
);
770 sst_ipc_fini(&byt
->ipc
);
772 EXPORT_SYMBOL_GPL(sst_byt_dsp_free
);