]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/intel/sst-baytrail-ipc.c
ASoC: Intel: Fix audio crash due to race condition in stream deletion
[karo-tx-linux.git] / sound / soc / intel / sst-baytrail-ipc.c
1 /*
2  * Intel Baytrail SST IPC Support
3  * Copyright (c) 2014, Intel Corporation.
4  *
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.
8  *
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.
13  */
14
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>
29 #include <linux/io.h>
30 #include <asm/div64.h>
31
32 #include "sst-baytrail-ipc.h"
33 #include "sst-dsp.h"
34 #include "sst-dsp-priv.h"
35
36 /* IPC message timeout */
37 #define IPC_TIMEOUT_MSECS       300
38 #define IPC_BOOT_MSECS          200
39
40 #define IPC_EMPTY_LIST_SIZE     8
41
42 /* IPC header bits */
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)
53
54 /* mask for differentiating between notification and reply message */
55 #define IPC_NOTIFICATION        (0x1 << 7)
56
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
64
65 /* notification messages */
66 #define IPC_IA_FW_INIT_CMPLT    0x81
67 #define IPC_SST_PERIOD_ELAPSED  0x97
68
69 /* IPC messages between host and ADSP */
70 struct sst_byt_address_info {
71         u32 addr;
72         u32 size;
73 } __packed;
74
75 struct sst_byt_str_type {
76         u8 codec_type;
77         u8 str_type;
78         u8 operation;
79         u8 protected_str;
80         u8 time_slots;
81         u8 reserved;
82         u16 result;
83 } __packed;
84
85 struct sst_byt_pcm_params {
86         u8 num_chan;
87         u8 pcm_wd_sz;
88         u8 use_offload_path;
89         u8 reserved;
90         u32 sfreq;
91         u8 channel_map[8];
92 } __packed;
93
94 struct sst_byt_frames_info {
95         u16 num_entries;
96         u16 rsrvd;
97         u32 frag_size;
98         struct sst_byt_address_info ring_buf_info[8];
99 } __packed;
100
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;
105 } __packed;
106
107 struct sst_byt_alloc_response {
108         struct sst_byt_str_type str_type;
109         u8 reserved[88];
110 } __packed;
111
112 struct sst_byt_start_stream_params {
113         u32 byte_offset;
114 } __packed;
115
116 struct sst_byt_tstamp {
117         u64 ring_buffer_counter;
118         u64 hardware_counter;
119         u64 frames_decoded;
120         u64 bytes_decoded;
121         u64 bytes_copied;
122         u32 sampling_frequency;
123         u32 channel_peak[8];
124 } __packed;
125
126 /* driver internal IPC message structure */
127 struct ipc_message {
128         struct list_head list;
129         u64 header;
130
131         /* direction wrt host CPU */
132         char tx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE];
133         size_t tx_size;
134         char rx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE];
135         size_t rx_size;
136
137         wait_queue_head_t waitq;
138         bool complete;
139         bool wait;
140         int errno;
141 };
142
143 struct sst_byt_stream;
144 struct sst_byt;
145
146 /* stream infomation */
147 struct sst_byt_stream {
148         struct list_head node;
149
150         /* configuration */
151         struct sst_byt_alloc_params request;
152         struct sst_byt_alloc_response reply;
153
154         /* runtime info */
155         struct sst_byt *byt;
156         int str_id;
157         bool commited;
158         bool running;
159
160         /* driver callback */
161         u32 (*notify_position)(struct sst_byt_stream *stream, void *data);
162         void *pdata;
163 };
164
165 /* SST Baytrail IPC data */
166 struct sst_byt {
167         struct device *dev;
168         struct sst_dsp *dsp;
169
170         /* stream */
171         struct list_head stream_list;
172
173         /* boot */
174         wait_queue_head_t boot_wait;
175         bool boot_complete;
176
177         /* IPC messaging */
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;
186 };
187
188 static inline u64 sst_byt_header(int msg_id, int data, bool large, int str_id)
189 {
190         u64 header;
191
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) |
196                  SST_BYT_IPCX_BUSY;
197
198         return header;
199 }
200
201 static inline u16 sst_byt_header_msg_id(u64 header)
202 {
203         return header & IPC_HEADER_MSG_ID_MASK;
204 }
205
206 static inline u8 sst_byt_header_str_id(u64 header)
207 {
208         return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK;
209 }
210
211 static inline u16 sst_byt_header_data(u64 header)
212 {
213         return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK;
214 }
215
216 static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt,
217                                                  int stream_id)
218 {
219         struct sst_byt_stream *stream;
220
221         list_for_each_entry(stream, &byt->stream_list, node) {
222                 if (stream->str_id == stream_id)
223                         return stream;
224         }
225
226         return NULL;
227 }
228
229 static void sst_byt_ipc_shim_dbg(struct sst_byt *byt, const char *text)
230 {
231         struct sst_dsp *sst = byt->dsp;
232         u64 isr, ipcd, imrx, ipcx;
233
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);
238
239         dev_err(byt->dev,
240                 "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n",
241                 text, ipcx, isr, ipcd, imrx);
242 }
243
244 /* locks held by caller */
245 static struct ipc_message *sst_byt_msg_get_empty(struct sst_byt *byt)
246 {
247         struct ipc_message *msg = NULL;
248
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);
253         }
254
255         return msg;
256 }
257
258 static void sst_byt_ipc_tx_msgs(struct kthread_work *work)
259 {
260         struct sst_byt *byt =
261                 container_of(work, struct sst_byt, kwork);
262         struct ipc_message *msg;
263         u64 ipcx;
264         unsigned long flags;
265
266         spin_lock_irqsave(&byt->dsp->spinlock, flags);
267         if (list_empty(&byt->tx_list)) {
268                 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
269                 return;
270         }
271
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);
276                 return;
277         }
278
279         msg = list_first_entry(&byt->tx_list, struct ipc_message, list);
280
281         list_move(&msg->list, &byt->rx_list);
282
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);
287
288         spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
289 }
290
291 static inline void sst_byt_tx_msg_reply_complete(struct sst_byt *byt,
292                                                  struct ipc_message *msg)
293 {
294         msg->complete = true;
295
296         if (!msg->wait)
297                 list_add_tail(&msg->list, &byt->empty_list);
298         else
299                 wake_up(&msg->waitq);
300 }
301
302 static int sst_byt_tx_wait_done(struct sst_byt *byt, struct ipc_message *msg,
303                                 void *rx_data)
304 {
305         unsigned long flags;
306         int ret;
307
308         /* wait for DSP completion */
309         ret = wait_event_timeout(msg->waitq, msg->complete,
310                                  msecs_to_jiffies(IPC_TIMEOUT_MSECS));
311
312         spin_lock_irqsave(&byt->dsp->spinlock, flags);
313         if (ret == 0) {
314                 list_del(&msg->list);
315                 sst_byt_ipc_shim_dbg(byt, "message timeout");
316
317                 ret = -ETIMEDOUT;
318         } else {
319
320                 /* copy the data returned from DSP */
321                 if (msg->rx_size)
322                         memcpy(rx_data, msg->rx_data, msg->rx_size);
323                 ret = msg->errno;
324         }
325
326         list_add_tail(&msg->list, &byt->empty_list);
327         spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
328         return ret;
329 }
330
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)
334 {
335         unsigned long flags;
336         struct ipc_message *msg;
337
338         spin_lock_irqsave(&byt->dsp->spinlock, flags);
339
340         msg = sst_byt_msg_get_empty(byt);
341         if (msg == NULL) {
342                 spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
343                 return -EBUSY;
344         }
345
346         msg->header = header;
347         msg->tx_size = tx_bytes;
348         msg->rx_size = rx_bytes;
349         msg->wait = wait;
350         msg->errno = 0;
351         msg->complete = false;
352
353         if (tx_bytes) {
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);
358         }
359
360         list_add_tail(&msg->list, &byt->tx_list);
361         spin_unlock_irqrestore(&byt->dsp->spinlock, flags);
362
363         queue_kthread_work(&byt->kworker, &byt->kwork);
364
365         if (wait)
366                 return sst_byt_tx_wait_done(byt, msg, rx_data);
367         else
368                 return 0;
369 }
370
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)
374 {
375         return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes,
376                                       rx_data, rx_bytes, 1);
377 }
378
379 static inline int sst_byt_ipc_tx_msg_nowait(struct sst_byt *byt, u64 header,
380                                                 void *tx_data, size_t tx_bytes)
381 {
382         return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes,
383                                       NULL, 0, 0);
384 }
385
386 static struct ipc_message *sst_byt_reply_find_msg(struct sst_byt *byt,
387                                                   u64 header)
388 {
389         struct ipc_message *msg = NULL, *_msg;
390         u64 mask;
391
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;
395         header &= mask;
396
397         if (list_empty(&byt->rx_list)) {
398                 dev_err(byt->dev,
399                         "ipc: rx list is empty but received 0x%llx\n", header);
400                 goto out;
401         }
402
403         list_for_each_entry(_msg, &byt->rx_list, list) {
404                 if ((_msg->header & mask) == header) {
405                         msg = _msg;
406                         break;
407                 }
408         }
409
410 out:
411         return msg;
412 }
413
414 static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg)
415 {
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);
420
421         stream = sst_byt_get_stream(byt, stream_id);
422         if (stream == NULL)
423                 return;
424
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;
430                 break;
431         case IPC_IA_START_STREAM:
432         case IPC_IA_RESUME_STREAM:
433                 stream->running = true;
434                 break;
435         }
436 }
437
438 static int sst_byt_process_reply(struct sst_byt *byt, u64 header)
439 {
440         struct ipc_message *msg;
441
442         msg = sst_byt_reply_find_msg(byt, header);
443         if (msg == NULL)
444                 return 1;
445
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);
449         }
450
451         /* update any stream states */
452         sst_byt_stream_update(byt, msg);
453
454         list_del(&msg->list);
455         /* wake up */
456         sst_byt_tx_msg_reply_complete(byt, msg);
457
458         return 1;
459 }
460
461 static void sst_byt_fw_ready(struct sst_byt *byt, u64 header)
462 {
463         dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header);
464
465         byt->boot_complete = true;
466         wake_up(&byt->boot_wait);
467 }
468
469 static int sst_byt_process_notification(struct sst_byt *byt,
470                                         unsigned long *flags)
471 {
472         struct sst_dsp *sst = byt->dsp;
473         struct sst_byt_stream *stream;
474         u64 header;
475         u8 msg_id, stream_id;
476         int handled = 1;
477
478         header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD);
479         msg_id = sst_byt_header_msg_id(header);
480
481         switch (msg_id) {
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);
489                 }
490                 break;
491         case IPC_IA_FW_INIT_CMPLT:
492                 sst_byt_fw_ready(byt, header);
493                 break;
494         }
495
496         return handled;
497 }
498
499 static irqreturn_t sst_byt_irq_thread(int irq, void *context)
500 {
501         struct sst_dsp *sst = (struct sst_dsp *) context;
502         struct sst_byt *byt = sst_dsp_get_thread_context(sst);
503         u64 header;
504         unsigned long flags;
505
506         spin_lock_irqsave(&sst->spinlock, flags);
507
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);
513                 } else {
514                         /* reply from ADSP */
515                         sst_byt_process_reply(byt, header);
516                 }
517                 /*
518                  * clear IPCD BUSY bit and set DONE bit. Tell DSP we have
519                  * processed the message and can accept new. Clear data part
520                  * of the header
521                  */
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),
525                         SST_BYT_IPCD_DONE);
526                 /* unmask message request interrupts */
527                 sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX,
528                         SST_BYT_IMRX_REQUEST, 0);
529         }
530
531         spin_unlock_irqrestore(&sst->spinlock, flags);
532
533         /* continue to send any remaining messages... */
534         queue_kthread_work(&byt->kworker, &byt->kwork);
535
536         return IRQ_HANDLED;
537 }
538
539 /* stream API */
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),
542         void *data)
543 {
544         struct sst_byt_stream *stream;
545         struct sst_dsp *sst = byt->dsp;
546         unsigned long flags;
547
548         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
549         if (stream == NULL)
550                 return NULL;
551
552         spin_lock_irqsave(&sst->spinlock, flags);
553         list_add(&stream->node, &byt->stream_list);
554         stream->notify_position = notify_position;
555         stream->pdata = data;
556         stream->byt = byt;
557         stream->str_id = id;
558         spin_unlock_irqrestore(&sst->spinlock, flags);
559
560         return stream;
561 }
562
563 int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream,
564                             int bits)
565 {
566         stream->request.pcm_params.pcm_wd_sz = bits;
567         return 0;
568 }
569
570 int sst_byt_stream_set_channels(struct sst_byt *byt,
571                                 struct sst_byt_stream *stream, u8 channels)
572 {
573         stream->request.pcm_params.num_chan = channels;
574         return 0;
575 }
576
577 int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream,
578                             unsigned int rate)
579 {
580         stream->request.pcm_params.sfreq = rate;
581         return 0;
582 }
583
584 /* stream sonfiguration */
585 int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream,
586                         int codec_type, int stream_type, int operation)
587 {
588         stream->request.str_type.codec_type = codec_type;
589         stream->request.str_type.str_type = stream_type;
590         stream->request.str_type.operation = operation;
591         stream->request.str_type.time_slots = 0xc;
592
593         return 0;
594 }
595
596 int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream,
597                           uint32_t buffer_addr, uint32_t buffer_size)
598 {
599         stream->request.frame_info.num_entries = 1;
600         stream->request.frame_info.ring_buf_info[0].addr = buffer_addr;
601         stream->request.frame_info.ring_buf_info[0].size = buffer_size;
602         /* calculate bytes per 4 ms fragment */
603         stream->request.frame_info.frag_size =
604                 stream->request.pcm_params.sfreq *
605                 stream->request.pcm_params.num_chan *
606                 stream->request.pcm_params.pcm_wd_sz / 8 *
607                 4 / 1000;
608         return 0;
609 }
610
611 int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream)
612 {
613         struct sst_byt_alloc_params *str_req = &stream->request;
614         struct sst_byt_alloc_response *reply = &stream->reply;
615         u64 header;
616         int ret;
617
618         header = sst_byt_header(IPC_IA_ALLOC_STREAM,
619                                 sizeof(*str_req) + sizeof(u32),
620                                 true, stream->str_id);
621         ret = sst_byt_ipc_tx_msg_wait(byt, header, str_req, sizeof(*str_req),
622                                       reply, sizeof(*reply));
623         if (ret < 0) {
624                 dev_err(byt->dev, "ipc: error stream commit failed\n");
625                 return ret;
626         }
627
628         stream->commited = true;
629
630         return 0;
631 }
632
633 int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream)
634 {
635         u64 header;
636         int ret = 0;
637         struct sst_dsp *sst = byt->dsp;
638         unsigned long flags;
639
640         if (!stream->commited)
641                 goto out;
642
643         header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id);
644         ret = sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0);
645         if (ret < 0) {
646                 dev_err(byt->dev, "ipc: free stream %d failed\n",
647                         stream->str_id);
648                 return -EAGAIN;
649         }
650
651         stream->commited = false;
652 out:
653         spin_lock_irqsave(&sst->spinlock, flags);
654         list_del(&stream->node);
655         kfree(stream);
656         spin_unlock_irqrestore(&sst->spinlock, flags);
657
658         return ret;
659 }
660
661 static int sst_byt_stream_operations(struct sst_byt *byt, int type,
662                                      int stream_id, int wait)
663 {
664         struct sst_byt_start_stream_params start_stream;
665         u64 header;
666         void *tx_msg = NULL;
667         size_t size = 0;
668
669         if (type != IPC_IA_START_STREAM) {
670                 header = sst_byt_header(type, 0, false, stream_id);
671         } else {
672                 start_stream.byte_offset = 0;
673                 header = sst_byt_header(IPC_IA_START_STREAM,
674                                         sizeof(start_stream) + sizeof(u32),
675                                         true, stream_id);
676                 tx_msg = &start_stream;
677                 size = sizeof(start_stream);
678         }
679
680         if (wait)
681                 return sst_byt_ipc_tx_msg_wait(byt, header,
682                                                tx_msg, size, NULL, 0);
683         else
684                 return sst_byt_ipc_tx_msg_nowait(byt, header, tx_msg, size);
685 }
686
687 /* stream ALSA trigger operations */
688 int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream)
689 {
690         int ret;
691
692         ret = sst_byt_stream_operations(byt, IPC_IA_START_STREAM,
693                                         stream->str_id, 0);
694         if (ret < 0)
695                 dev_err(byt->dev, "ipc: error failed to start stream %d\n",
696                         stream->str_id);
697
698         return ret;
699 }
700
701 int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream)
702 {
703         int ret;
704
705         /* don't stop streams that are not commited */
706         if (!stream->commited)
707                 return 0;
708
709         ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM,
710                                         stream->str_id, 0);
711         if (ret < 0)
712                 dev_err(byt->dev, "ipc: error failed to stop stream %d\n",
713                         stream->str_id);
714         return ret;
715 }
716
717 int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream)
718 {
719         int ret;
720
721         ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM,
722                                         stream->str_id, 0);
723         if (ret < 0)
724                 dev_err(byt->dev, "ipc: error failed to pause stream %d\n",
725                         stream->str_id);
726
727         return ret;
728 }
729
730 int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream)
731 {
732         int ret;
733
734         ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM,
735                                         stream->str_id, 0);
736         if (ret < 0)
737                 dev_err(byt->dev, "ipc: error failed to resume stream %d\n",
738                         stream->str_id);
739
740         return ret;
741 }
742
743 int sst_byt_get_dsp_position(struct sst_byt *byt,
744                              struct sst_byt_stream *stream, int buffer_size)
745 {
746         struct sst_dsp *sst = byt->dsp;
747         struct sst_byt_tstamp fw_tstamp;
748         u8 str_id = stream->str_id;
749         u32 tstamp_offset;
750
751         tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp);
752         memcpy_fromio(&fw_tstamp,
753                       sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp));
754
755         return do_div(fw_tstamp.ring_buffer_counter, buffer_size);
756 }
757
758 static int msg_empty_list_init(struct sst_byt *byt)
759 {
760         struct ipc_message *msg;
761         int i;
762
763         byt->msg = kzalloc(sizeof(*msg) * IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
764         if (byt->msg == NULL)
765                 return -ENOMEM;
766
767         for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
768                 init_waitqueue_head(&byt->msg[i].waitq);
769                 list_add(&byt->msg[i].list, &byt->empty_list);
770         }
771
772         return 0;
773 }
774
775 struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt)
776 {
777         return byt->dsp;
778 }
779
780 static struct sst_dsp_device byt_dev = {
781         .thread = sst_byt_irq_thread,
782         .ops = &sst_byt_ops,
783 };
784
785 int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata)
786 {
787         struct sst_byt *byt;
788         struct sst_fw *byt_sst_fw;
789         int err;
790
791         dev_dbg(dev, "initialising Byt DSP IPC\n");
792
793         byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL);
794         if (byt == NULL)
795                 return -ENOMEM;
796
797         byt->dev = dev;
798         INIT_LIST_HEAD(&byt->stream_list);
799         INIT_LIST_HEAD(&byt->tx_list);
800         INIT_LIST_HEAD(&byt->rx_list);
801         INIT_LIST_HEAD(&byt->empty_list);
802         init_waitqueue_head(&byt->boot_wait);
803         init_waitqueue_head(&byt->wait_txq);
804
805         err = msg_empty_list_init(byt);
806         if (err < 0)
807                 return -ENOMEM;
808
809         /* start the IPC message thread */
810         init_kthread_worker(&byt->kworker);
811         byt->tx_thread = kthread_run(kthread_worker_fn,
812                                      &byt->kworker,
813                                      dev_name(byt->dev));
814         if (IS_ERR(byt->tx_thread)) {
815                 err = PTR_ERR(byt->tx_thread);
816                 dev_err(byt->dev, "error failed to create message TX task\n");
817                 goto err_free_msg;
818         }
819         init_kthread_work(&byt->kwork, sst_byt_ipc_tx_msgs);
820
821         byt_dev.thread_context = byt;
822
823         /* init SST shim */
824         byt->dsp = sst_dsp_new(dev, &byt_dev, pdata);
825         if (byt->dsp == NULL) {
826                 err = -ENODEV;
827                 goto err_free_msg;
828         }
829
830         /* keep the DSP in reset state for base FW loading */
831         sst_dsp_reset(byt->dsp);
832
833         byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt);
834         if (byt_sst_fw  == NULL) {
835                 err = -ENODEV;
836                 dev_err(dev, "error: failed to load firmware\n");
837                 goto fw_err;
838         }
839
840         /* wait for DSP boot completion */
841         sst_dsp_boot(byt->dsp);
842         err = wait_event_timeout(byt->boot_wait, byt->boot_complete,
843                                  msecs_to_jiffies(IPC_BOOT_MSECS));
844         if (err == 0) {
845                 err = -EIO;
846                 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
847                 goto boot_err;
848         }
849
850         pdata->dsp = byt;
851
852         return 0;
853
854 boot_err:
855         sst_dsp_reset(byt->dsp);
856         sst_fw_free(byt_sst_fw);
857 fw_err:
858         sst_dsp_free(byt->dsp);
859 err_free_msg:
860         kfree(byt->msg);
861
862         return err;
863 }
864 EXPORT_SYMBOL_GPL(sst_byt_dsp_init);
865
866 void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata)
867 {
868         struct sst_byt *byt = pdata->dsp;
869
870         sst_dsp_reset(byt->dsp);
871         sst_fw_free_all(byt->dsp);
872         sst_dsp_free(byt->dsp);
873         kfree(byt->msg);
874 }
875 EXPORT_SYMBOL_GPL(sst_byt_dsp_free);