ixgbevf: Fix checksum error when using stacked vlan
[linux/fpc-iii.git] / sound / soc / intel / sst-baytrail-ipc.c
blobb4ad98c43e5c6d53a1654e75bc4577d82f15b305
1 /*
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
12 * more details.
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>
28 #include <linux/io.h>
29 #include <asm/div64.h>
31 #include "sst-baytrail-ipc.h"
32 #include "sst-dsp.h"
33 #include "sst-dsp-priv.h"
35 /* IPC message timeout */
36 #define IPC_TIMEOUT_MSECS 300
37 #define IPC_BOOT_MSECS 200
39 #define IPC_EMPTY_LIST_SIZE 8
41 /* IPC header bits */
42 #define IPC_HEADER_MSG_ID_MASK 0xff
43 #define IPC_HEADER_MSG_ID(x) ((x) & IPC_HEADER_MSG_ID_MASK)
44 #define IPC_HEADER_STR_ID_SHIFT 8
45 #define IPC_HEADER_STR_ID_MASK 0x1f
46 #define IPC_HEADER_STR_ID(x) (((x) & 0x1f) << IPC_HEADER_STR_ID_SHIFT)
47 #define IPC_HEADER_LARGE_SHIFT 13
48 #define IPC_HEADER_LARGE(x) (((x) & 0x1) << IPC_HEADER_LARGE_SHIFT)
49 #define IPC_HEADER_DATA_SHIFT 16
50 #define IPC_HEADER_DATA_MASK 0x3fff
51 #define IPC_HEADER_DATA(x) (((x) & 0x3fff) << IPC_HEADER_DATA_SHIFT)
53 /* mask for differentiating between notification and reply message */
54 #define IPC_NOTIFICATION (0x1 << 7)
56 /* I2L Stream config/control msgs */
57 #define IPC_IA_ALLOC_STREAM 0x20
58 #define IPC_IA_FREE_STREAM 0x21
59 #define IPC_IA_PAUSE_STREAM 0x24
60 #define IPC_IA_RESUME_STREAM 0x25
61 #define IPC_IA_DROP_STREAM 0x26
62 #define IPC_IA_START_STREAM 0x30
64 /* notification messages */
65 #define IPC_IA_FW_INIT_CMPLT 0x81
66 #define IPC_SST_PERIOD_ELAPSED 0x97
68 /* IPC messages between host and ADSP */
69 struct sst_byt_address_info {
70 u32 addr;
71 u32 size;
72 } __packed;
74 struct sst_byt_str_type {
75 u8 codec_type;
76 u8 str_type;
77 u8 operation;
78 u8 protected_str;
79 u8 time_slots;
80 u8 reserved;
81 u16 result;
82 } __packed;
84 struct sst_byt_pcm_params {
85 u8 num_chan;
86 u8 pcm_wd_sz;
87 u8 use_offload_path;
88 u8 reserved;
89 u32 sfreq;
90 u8 channel_map[8];
91 } __packed;
93 struct sst_byt_frames_info {
94 u16 num_entries;
95 u16 rsrvd;
96 u32 frag_size;
97 struct sst_byt_address_info ring_buf_info[8];
98 } __packed;
100 struct sst_byt_alloc_params {
101 struct sst_byt_str_type str_type;
102 struct sst_byt_pcm_params pcm_params;
103 struct sst_byt_frames_info frame_info;
104 } __packed;
106 struct sst_byt_alloc_response {
107 struct sst_byt_str_type str_type;
108 u8 reserved[88];
109 } __packed;
111 struct sst_byt_start_stream_params {
112 u32 byte_offset;
113 } __packed;
115 struct sst_byt_tstamp {
116 u64 ring_buffer_counter;
117 u64 hardware_counter;
118 u64 frames_decoded;
119 u64 bytes_decoded;
120 u64 bytes_copied;
121 u32 sampling_frequency;
122 u32 channel_peak[8];
123 } __packed;
125 struct sst_byt_fw_version {
126 u8 build;
127 u8 minor;
128 u8 major;
129 u8 type;
130 } __packed;
132 struct sst_byt_fw_build_info {
133 u8 date[16];
134 u8 time[16];
135 } __packed;
137 struct sst_byt_fw_init {
138 struct sst_byt_fw_version fw_version;
139 struct sst_byt_fw_build_info build_info;
140 u16 result;
141 u8 module_id;
142 u8 debug_info;
143 } __packed;
145 /* driver internal IPC message structure */
146 struct ipc_message {
147 struct list_head list;
148 u64 header;
150 /* direction wrt host CPU */
151 char tx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE];
152 size_t tx_size;
153 char rx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE];
154 size_t rx_size;
156 wait_queue_head_t waitq;
157 bool complete;
158 bool wait;
159 int errno;
162 struct sst_byt_stream;
163 struct sst_byt;
165 /* stream infomation */
166 struct sst_byt_stream {
167 struct list_head node;
169 /* configuration */
170 struct sst_byt_alloc_params request;
171 struct sst_byt_alloc_response reply;
173 /* runtime info */
174 struct sst_byt *byt;
175 int str_id;
176 bool commited;
177 bool running;
179 /* driver callback */
180 u32 (*notify_position)(struct sst_byt_stream *stream, void *data);
181 void *pdata;
184 /* SST Baytrail IPC data */
185 struct sst_byt {
186 struct device *dev;
187 struct sst_dsp *dsp;
189 /* stream */
190 struct list_head stream_list;
192 /* boot */
193 wait_queue_head_t boot_wait;
194 bool boot_complete;
195 struct sst_fw *fw;
197 /* IPC messaging */
198 struct list_head tx_list;
199 struct list_head rx_list;
200 struct list_head empty_list;
201 wait_queue_head_t wait_txq;
202 struct task_struct *tx_thread;
203 struct kthread_worker kworker;
204 struct kthread_work kwork;
205 struct ipc_message *msg;
208 static inline u64 sst_byt_header(int msg_id, int data, bool large, int str_id)
210 u64 header;
212 header = IPC_HEADER_MSG_ID(msg_id) |
213 IPC_HEADER_STR_ID(str_id) |
214 IPC_HEADER_LARGE(large) |
215 IPC_HEADER_DATA(data) |
216 SST_BYT_IPCX_BUSY;
218 return header;
221 static inline u16 sst_byt_header_msg_id(u64 header)
223 return header & IPC_HEADER_MSG_ID_MASK;
226 static inline u8 sst_byt_header_str_id(u64 header)
228 return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK;
231 static inline u16 sst_byt_header_data(u64 header)
233 return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK;
236 static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt,
237 int stream_id)
239 struct sst_byt_stream *stream;
241 list_for_each_entry(stream, &byt->stream_list, node) {
242 if (stream->str_id == stream_id)
243 return stream;
246 return NULL;
249 static void sst_byt_ipc_shim_dbg(struct sst_byt *byt, const char *text)
251 struct sst_dsp *sst = byt->dsp;
252 u64 isr, ipcd, imrx, ipcx;
254 ipcx = sst_dsp_shim_read64_unlocked(sst, SST_IPCX);
255 isr = sst_dsp_shim_read64_unlocked(sst, SST_ISRX);
256 ipcd = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
257 imrx = sst_dsp_shim_read64_unlocked(sst, SST_IMRX);
259 dev_err(byt->dev,
260 "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
261 text, ipcx, isr, ipcd, imrx);
264 /* locks held by caller */
265 static struct ipc_message *sst_byt_msg_get_empty(struct sst_byt *byt)
267 struct ipc_message *msg = NULL;
269 if (!list_empty(&byt->empty_list)) {
270 msg = list_first_entry(&byt->empty_list,
271 struct ipc_message, list);
272 list_del(&msg->list);
275 return msg;
278 static void sst_byt_ipc_tx_msgs(struct kthread_work *work)
280 struct sst_byt *byt =
281 container_of(work, struct sst_byt, kwork);
282 struct ipc_message *msg;
283 u64 ipcx;
284 unsigned long flags;
286 spin_lock_irqsave(&byt->dsp->spinlock, flags);
287 if (list_empty(&byt->tx_list)) {
288 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
289 return;
292 /* if the DSP is busy we will TX messages after IRQ */
293 ipcx = sst_dsp_shim_read64_unlocked(byt->dsp, SST_IPCX);
294 if (ipcx & SST_BYT_IPCX_BUSY) {
295 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
296 return;
299 msg = list_first_entry(&byt->tx_list, struct ipc_message, list);
301 list_move(&msg->list, &byt->rx_list);
303 /* send the message */
304 if (msg->header & IPC_HEADER_LARGE(true))
305 sst_dsp_outbox_write(byt->dsp, msg->tx_data, msg->tx_size);
306 sst_dsp_shim_write64_unlocked(byt->dsp, SST_IPCX, msg->header);
308 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
311 static inline void sst_byt_tx_msg_reply_complete(struct sst_byt *byt,
312 struct ipc_message *msg)
314 msg->complete = true;
316 if (!msg->wait)
317 list_add_tail(&msg->list, &byt->empty_list);
318 else
319 wake_up(&msg->waitq);
322 static void sst_byt_drop_all(struct sst_byt *byt)
324 struct ipc_message *msg, *tmp;
325 unsigned long flags;
327 /* drop all TX and Rx messages before we stall + reset DSP */
328 spin_lock_irqsave(&byt->dsp->spinlock, flags);
329 list_for_each_entry_safe(msg, tmp, &byt->tx_list, list) {
330 list_move(&msg->list, &byt->empty_list);
333 list_for_each_entry_safe(msg, tmp, &byt->rx_list, list) {
334 list_move(&msg->list, &byt->empty_list);
337 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
340 static int sst_byt_tx_wait_done(struct sst_byt *byt, struct ipc_message *msg,
341 void *rx_data)
343 unsigned long flags;
344 int ret;
346 /* wait for DSP completion */
347 ret = wait_event_timeout(msg->waitq, msg->complete,
348 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
350 spin_lock_irqsave(&byt->dsp->spinlock, flags);
351 if (ret == 0) {
352 list_del(&msg->list);
353 sst_byt_ipc_shim_dbg(byt, "message timeout");
355 ret = -ETIMEDOUT;
356 } else {
358 /* copy the data returned from DSP */
359 if (msg->rx_size)
360 memcpy(rx_data, msg->rx_data, msg->rx_size);
361 ret = msg->errno;
364 list_add_tail(&msg->list, &byt->empty_list);
365 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
366 return ret;
369 static int sst_byt_ipc_tx_message(struct sst_byt *byt, u64 header,
370 void *tx_data, size_t tx_bytes,
371 void *rx_data, size_t rx_bytes, int wait)
373 unsigned long flags;
374 struct ipc_message *msg;
376 spin_lock_irqsave(&byt->dsp->spinlock, flags);
378 msg = sst_byt_msg_get_empty(byt);
379 if (msg == NULL) {
380 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
381 return -EBUSY;
384 msg->header = header;
385 msg->tx_size = tx_bytes;
386 msg->rx_size = rx_bytes;
387 msg->wait = wait;
388 msg->errno = 0;
389 msg->complete = false;
391 if (tx_bytes) {
392 /* msg content = lower 32-bit of the header + data */
393 *(u32 *)msg->tx_data = (u32)(header & (u32)-1);
394 memcpy(msg->tx_data + sizeof(u32), tx_data, tx_bytes);
395 msg->tx_size += sizeof(u32);
398 list_add_tail(&msg->list, &byt->tx_list);
399 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
401 queue_kthread_work(&byt->kworker, &byt->kwork);
403 if (wait)
404 return sst_byt_tx_wait_done(byt, msg, rx_data);
405 else
406 return 0;
409 static inline int sst_byt_ipc_tx_msg_wait(struct sst_byt *byt, u64 header,
410 void *tx_data, size_t tx_bytes,
411 void *rx_data, size_t rx_bytes)
413 return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes,
414 rx_data, rx_bytes, 1);
417 static inline int sst_byt_ipc_tx_msg_nowait(struct sst_byt *byt, u64 header,
418 void *tx_data, size_t tx_bytes)
420 return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes,
421 NULL, 0, 0);
424 static struct ipc_message *sst_byt_reply_find_msg(struct sst_byt *byt,
425 u64 header)
427 struct ipc_message *msg = NULL, *_msg;
428 u64 mask;
430 /* match reply to message sent based on msg and stream IDs */
431 mask = IPC_HEADER_MSG_ID_MASK |
432 IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT;
433 header &= mask;
435 if (list_empty(&byt->rx_list)) {
436 dev_err(byt->dev,
437 "ipc: rx list is empty but received 0x%llx\n", header);
438 goto out;
441 list_for_each_entry(_msg, &byt->rx_list, list) {
442 if ((_msg->header & mask) == header) {
443 msg = _msg;
444 break;
448 out:
449 return msg;
452 static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg)
454 struct sst_byt_stream *stream;
455 u64 header = msg->header;
456 u8 stream_id = sst_byt_header_str_id(header);
457 u8 stream_msg = sst_byt_header_msg_id(header);
459 stream = sst_byt_get_stream(byt, stream_id);
460 if (stream == NULL)
461 return;
463 switch (stream_msg) {
464 case IPC_IA_DROP_STREAM:
465 case IPC_IA_PAUSE_STREAM:
466 case IPC_IA_FREE_STREAM:
467 stream->running = false;
468 break;
469 case IPC_IA_START_STREAM:
470 case IPC_IA_RESUME_STREAM:
471 stream->running = true;
472 break;
476 static int sst_byt_process_reply(struct sst_byt *byt, u64 header)
478 struct ipc_message *msg;
480 msg = sst_byt_reply_find_msg(byt, header);
481 if (msg == NULL)
482 return 1;
484 if (header & IPC_HEADER_LARGE(true)) {
485 msg->rx_size = sst_byt_header_data(header);
486 sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size);
489 /* update any stream states */
490 sst_byt_stream_update(byt, msg);
492 list_del(&msg->list);
493 /* wake up */
494 sst_byt_tx_msg_reply_complete(byt, msg);
496 return 1;
499 static void sst_byt_fw_ready(struct sst_byt *byt, u64 header)
501 dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header);
503 byt->boot_complete = true;
504 wake_up(&byt->boot_wait);
507 static int sst_byt_process_notification(struct sst_byt *byt,
508 unsigned long *flags)
510 struct sst_dsp *sst = byt->dsp;
511 struct sst_byt_stream *stream;
512 u64 header;
513 u8 msg_id, stream_id;
514 int handled = 1;
516 header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
517 msg_id = sst_byt_header_msg_id(header);
519 switch (msg_id) {
520 case IPC_SST_PERIOD_ELAPSED:
521 stream_id = sst_byt_header_str_id(header);
522 stream = sst_byt_get_stream(byt, stream_id);
523 if (stream && stream->running && stream->notify_position) {
524 spin_unlock_irqrestore(&sst->spinlock, *flags);
525 stream->notify_position(stream, stream->pdata);
526 spin_lock_irqsave(&sst->spinlock, *flags);
528 break;
529 case IPC_IA_FW_INIT_CMPLT:
530 sst_byt_fw_ready(byt, header);
531 break;
534 return handled;
537 static irqreturn_t sst_byt_irq_thread(int irq, void *context)
539 struct sst_dsp *sst = (struct sst_dsp *) context;
540 struct sst_byt *byt = sst_dsp_get_thread_context(sst);
541 u64 header;
542 unsigned long flags;
544 spin_lock_irqsave(&sst->spinlock, flags);
546 header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
547 if (header & SST_BYT_IPCD_BUSY) {
548 if (header & IPC_NOTIFICATION) {
549 /* message from ADSP */
550 sst_byt_process_notification(byt, &flags);
551 } else {
552 /* reply from ADSP */
553 sst_byt_process_reply(byt, header);
556 * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
557 * processed the message and can accept new. Clear data part
558 * of the header
560 sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD,
561 SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY |
562 IPC_HEADER_DATA(IPC_HEADER_DATA_MASK),
563 SST_BYT_IPCD_DONE);
564 /* unmask message request interrupts */
565 sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX,
566 SST_BYT_IMRX_REQUEST, 0);
569 spin_unlock_irqrestore(&sst->spinlock, flags);
571 /* continue to send any remaining messages... */
572 queue_kthread_work(&byt->kworker, &byt->kwork);
574 return IRQ_HANDLED;
577 /* stream API */
578 struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id,
579 u32 (*notify_position)(struct sst_byt_stream *stream, void *data),
580 void *data)
582 struct sst_byt_stream *stream;
583 struct sst_dsp *sst = byt->dsp;
584 unsigned long flags;
586 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
587 if (stream == NULL)
588 return NULL;
590 spin_lock_irqsave(&sst->spinlock, flags);
591 list_add(&stream->node, &byt->stream_list);
592 stream->notify_position = notify_position;
593 stream->pdata = data;
594 stream->byt = byt;
595 stream->str_id = id;
596 spin_unlock_irqrestore(&sst->spinlock, flags);
598 return stream;
601 int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream,
602 int bits)
604 stream->request.pcm_params.pcm_wd_sz = bits;
605 return 0;
608 int sst_byt_stream_set_channels(struct sst_byt *byt,
609 struct sst_byt_stream *stream, u8 channels)
611 stream->request.pcm_params.num_chan = channels;
612 return 0;
615 int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream,
616 unsigned int rate)
618 stream->request.pcm_params.sfreq = rate;
619 return 0;
622 /* stream sonfiguration */
623 int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream,
624 int codec_type, int stream_type, int operation)
626 stream->request.str_type.codec_type = codec_type;
627 stream->request.str_type.str_type = stream_type;
628 stream->request.str_type.operation = operation;
629 stream->request.str_type.time_slots = 0xc;
631 return 0;
634 int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream,
635 uint32_t buffer_addr, uint32_t buffer_size)
637 stream->request.frame_info.num_entries = 1;
638 stream->request.frame_info.ring_buf_info[0].addr = buffer_addr;
639 stream->request.frame_info.ring_buf_info[0].size = buffer_size;
640 /* calculate bytes per 4 ms fragment */
641 stream->request.frame_info.frag_size =
642 stream->request.pcm_params.sfreq *
643 stream->request.pcm_params.num_chan *
644 stream->request.pcm_params.pcm_wd_sz / 8 *
645 4 / 1000;
646 return 0;
649 int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream)
651 struct sst_byt_alloc_params *str_req = &stream->request;
652 struct sst_byt_alloc_response *reply = &stream->reply;
653 u64 header;
654 int ret;
656 header = sst_byt_header(IPC_IA_ALLOC_STREAM,
657 sizeof(*str_req) + sizeof(u32),
658 true, stream->str_id);
659 ret = sst_byt_ipc_tx_msg_wait(byt, header, str_req, sizeof(*str_req),
660 reply, sizeof(*reply));
661 if (ret < 0) {
662 dev_err(byt->dev, "ipc: error stream commit failed\n");
663 return ret;
666 stream->commited = true;
668 return 0;
671 int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream)
673 u64 header;
674 int ret = 0;
675 struct sst_dsp *sst = byt->dsp;
676 unsigned long flags;
678 if (!stream->commited)
679 goto out;
681 header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id);
682 ret = sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0);
683 if (ret < 0) {
684 dev_err(byt->dev, "ipc: free stream %d failed\n",
685 stream->str_id);
686 return -EAGAIN;
689 stream->commited = false;
690 out:
691 spin_lock_irqsave(&sst->spinlock, flags);
692 list_del(&stream->node);
693 kfree(stream);
694 spin_unlock_irqrestore(&sst->spinlock, flags);
696 return ret;
699 static int sst_byt_stream_operations(struct sst_byt *byt, int type,
700 int stream_id, int wait)
702 u64 header;
704 header = sst_byt_header(type, 0, false, stream_id);
705 if (wait)
706 return sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0);
707 else
708 return sst_byt_ipc_tx_msg_nowait(byt, header, NULL, 0);
711 /* stream ALSA trigger operations */
712 int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream,
713 u32 start_offset)
715 struct sst_byt_start_stream_params start_stream;
716 void *tx_msg;
717 size_t size;
718 u64 header;
719 int ret;
721 start_stream.byte_offset = start_offset;
722 header = sst_byt_header(IPC_IA_START_STREAM,
723 sizeof(start_stream) + sizeof(u32),
724 true, stream->str_id);
725 tx_msg = &start_stream;
726 size = sizeof(start_stream);
728 ret = sst_byt_ipc_tx_msg_nowait(byt, header, tx_msg, size);
729 if (ret < 0)
730 dev_err(byt->dev, "ipc: error failed to start stream %d\n",
731 stream->str_id);
733 return ret;
736 int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream)
738 int ret;
740 /* don't stop streams that are not commited */
741 if (!stream->commited)
742 return 0;
744 ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM,
745 stream->str_id, 0);
746 if (ret < 0)
747 dev_err(byt->dev, "ipc: error failed to stop stream %d\n",
748 stream->str_id);
749 return ret;
752 int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream)
754 int ret;
756 ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM,
757 stream->str_id, 0);
758 if (ret < 0)
759 dev_err(byt->dev, "ipc: error failed to pause stream %d\n",
760 stream->str_id);
762 return ret;
765 int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream)
767 int ret;
769 ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM,
770 stream->str_id, 0);
771 if (ret < 0)
772 dev_err(byt->dev, "ipc: error failed to resume stream %d\n",
773 stream->str_id);
775 return ret;
778 int sst_byt_get_dsp_position(struct sst_byt *byt,
779 struct sst_byt_stream *stream, int buffer_size)
781 struct sst_dsp *sst = byt->dsp;
782 struct sst_byt_tstamp fw_tstamp;
783 u8 str_id = stream->str_id;
784 u32 tstamp_offset;
786 tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp);
787 memcpy_fromio(&fw_tstamp,
788 sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp));
790 return do_div(fw_tstamp.ring_buffer_counter, buffer_size);
793 static int msg_empty_list_init(struct sst_byt *byt)
795 struct ipc_message *msg;
796 int i;
798 byt->msg = kzalloc(sizeof(*msg) * IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
799 if (byt->msg == NULL)
800 return -ENOMEM;
802 for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
803 init_waitqueue_head(&byt->msg[i].waitq);
804 list_add(&byt->msg[i].list, &byt->empty_list);
807 return 0;
810 struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt)
812 return byt->dsp;
815 static struct sst_dsp_device byt_dev = {
816 .thread = sst_byt_irq_thread,
817 .ops = &sst_byt_ops,
820 int sst_byt_dsp_suspend_late(struct device *dev, struct sst_pdata *pdata)
822 struct sst_byt *byt = pdata->dsp;
824 dev_dbg(byt->dev, "dsp reset\n");
825 sst_dsp_reset(byt->dsp);
826 sst_byt_drop_all(byt);
827 dev_dbg(byt->dev, "dsp in reset\n");
829 dev_dbg(byt->dev, "free all blocks and unload fw\n");
830 sst_fw_unload(byt->fw);
832 return 0;
834 EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late);
836 int sst_byt_dsp_boot(struct device *dev, struct sst_pdata *pdata)
838 struct sst_byt *byt = pdata->dsp;
839 int ret;
841 dev_dbg(byt->dev, "reload dsp fw\n");
843 sst_dsp_reset(byt->dsp);
845 ret = sst_fw_reload(byt->fw);
846 if (ret < 0) {
847 dev_err(dev, "error: failed to reload firmware\n");
848 return ret;
851 /* wait for DSP boot completion */
852 byt->boot_complete = false;
853 sst_dsp_boot(byt->dsp);
854 dev_dbg(byt->dev, "dsp booting...\n");
856 return 0;
858 EXPORT_SYMBOL_GPL(sst_byt_dsp_boot);
860 int sst_byt_dsp_wait_for_ready(struct device *dev, struct sst_pdata *pdata)
862 struct sst_byt *byt = pdata->dsp;
863 int err;
865 dev_dbg(byt->dev, "wait for dsp reboot\n");
867 err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
868 msecs_to_jiffies(IPC_BOOT_MSECS));
869 if (err == 0) {
870 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
871 return -EIO;
874 dev_dbg(byt->dev, "dsp rebooted\n");
875 return 0;
877 EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready);
879 int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata)
881 struct sst_byt *byt;
882 struct sst_fw *byt_sst_fw;
883 struct sst_byt_fw_init init;
884 int err;
886 dev_dbg(dev, "initialising Byt DSP IPC\n");
888 byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL);
889 if (byt == NULL)
890 return -ENOMEM;
892 byt->dev = dev;
893 INIT_LIST_HEAD(&byt->stream_list);
894 INIT_LIST_HEAD(&byt->tx_list);
895 INIT_LIST_HEAD(&byt->rx_list);
896 INIT_LIST_HEAD(&byt->empty_list);
897 init_waitqueue_head(&byt->boot_wait);
898 init_waitqueue_head(&byt->wait_txq);
900 err = msg_empty_list_init(byt);
901 if (err < 0)
902 return -ENOMEM;
904 /* start the IPC message thread */
905 init_kthread_worker(&byt->kworker);
906 byt->tx_thread = kthread_run(kthread_worker_fn,
907 &byt->kworker, "%s",
908 dev_name(byt->dev));
909 if (IS_ERR(byt->tx_thread)) {
910 err = PTR_ERR(byt->tx_thread);
911 dev_err(byt->dev, "error failed to create message TX task\n");
912 goto err_free_msg;
914 init_kthread_work(&byt->kwork, sst_byt_ipc_tx_msgs);
916 byt_dev.thread_context = byt;
918 /* init SST shim */
919 byt->dsp = sst_dsp_new(dev, &byt_dev, pdata);
920 if (byt->dsp == NULL) {
921 err = -ENODEV;
922 goto dsp_err;
925 /* keep the DSP in reset state for base FW loading */
926 sst_dsp_reset(byt->dsp);
928 byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt);
929 if (byt_sst_fw == NULL) {
930 err = -ENODEV;
931 dev_err(dev, "error: failed to load firmware\n");
932 goto fw_err;
935 /* wait for DSP boot completion */
936 sst_dsp_boot(byt->dsp);
937 err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
938 msecs_to_jiffies(IPC_BOOT_MSECS));
939 if (err == 0) {
940 err = -EIO;
941 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
942 goto boot_err;
945 /* show firmware information */
946 sst_dsp_inbox_read(byt->dsp, &init, sizeof(init));
947 dev_info(byt->dev, "FW version: %02x.%02x.%02x.%02x\n",
948 init.fw_version.major, init.fw_version.minor,
949 init.fw_version.build, init.fw_version.type);
950 dev_info(byt->dev, "Build type: %x\n", init.fw_version.type);
951 dev_info(byt->dev, "Build date: %s %s\n",
952 init.build_info.date, init.build_info.time);
954 pdata->dsp = byt;
955 byt->fw = byt_sst_fw;
957 return 0;
959 boot_err:
960 sst_dsp_reset(byt->dsp);
961 sst_fw_free(byt_sst_fw);
962 fw_err:
963 sst_dsp_free(byt->dsp);
964 dsp_err:
965 kthread_stop(byt->tx_thread);
966 err_free_msg:
967 kfree(byt->msg);
969 return err;
971 EXPORT_SYMBOL_GPL(sst_byt_dsp_init);
973 void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata)
975 struct sst_byt *byt = pdata->dsp;
977 sst_dsp_reset(byt->dsp);
978 sst_fw_free_all(byt->dsp);
979 sst_dsp_free(byt->dsp);
980 kthread_stop(byt->tx_thread);
981 kfree(byt->msg);
983 EXPORT_SYMBOL_GPL(sst_byt_dsp_free);