ASoC: Intel: Sample Baytrail DSP DMA pointer only after each period
[linux-2.6-block.git] / sound / soc / intel / sst-baytrail-ipc.c
CommitLineData
a4b12990
MB
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 */
70struct sst_byt_address_info {
71 u32 addr;
72 u32 size;
73} __packed;
74
75struct 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
85struct 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
94struct 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
101struct 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
107struct sst_byt_alloc_response {
108 struct sst_byt_str_type str_type;
109 u8 reserved[88];
110} __packed;
111
112struct sst_byt_start_stream_params {
113 u32 byte_offset;
114} __packed;
115
116struct 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 */
127struct 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
143struct sst_byt_stream;
144struct sst_byt;
145
146/* stream infomation */
147struct 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 */
166struct 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
188static 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
201static inline u16 sst_byt_header_msg_id(u64 header)
202{
203 return header & IPC_HEADER_MSG_ID_MASK;
204}
205
206static 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
211static inline u16 sst_byt_header_data(u64 header)
212{
213 return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK;
214}
215
216static 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
229static 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 */
245static 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
258static 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
291static 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
302static 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
331static 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
371static 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
379static 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
386static 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
410out:
411 return msg;
412}
413
414static 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
438static 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
461static 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
469static 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
499static 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 */
540struct 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
546 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
547 if (stream == NULL)
548 return NULL;
549
550 list_add(&stream->node, &byt->stream_list);
551 stream->notify_position = notify_position;
552 stream->pdata = data;
553 stream->byt = byt;
554 stream->str_id = id;
555
556 return stream;
557}
558
559int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream,
560 int bits)
561{
562 stream->request.pcm_params.pcm_wd_sz = bits;
563 return 0;
564}
565
566int sst_byt_stream_set_channels(struct sst_byt *byt,
567 struct sst_byt_stream *stream, u8 channels)
568{
569 stream->request.pcm_params.num_chan = channels;
570 return 0;
571}
572
573int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream,
574 unsigned int rate)
575{
576 stream->request.pcm_params.sfreq = rate;
577 return 0;
578}
579
580/* stream sonfiguration */
581int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream,
582 int codec_type, int stream_type, int operation)
583{
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;
588
589 return 0;
590}
591
592int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream,
593 uint32_t buffer_addr, uint32_t buffer_size)
594{
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 *
603 4 / 1000;
604 return 0;
605}
606
607int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream)
608{
609 struct sst_byt_alloc_params *str_req = &stream->request;
610 struct sst_byt_alloc_response *reply = &stream->reply;
611 u64 header;
612 int ret;
613
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));
619 if (ret < 0) {
620 dev_err(byt->dev, "ipc: error stream commit failed\n");
621 return ret;
622 }
623
624 stream->commited = true;
625
626 return 0;
627}
628
629int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream)
630{
631 u64 header;
632 int ret = 0;
633
634 if (!stream->commited)
635 goto out;
636
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);
639 if (ret < 0) {
640 dev_err(byt->dev, "ipc: free stream %d failed\n",
641 stream->str_id);
642 return -EAGAIN;
643 }
644
645 stream->commited = false;
646out:
647 list_del(&stream->node);
648 kfree(stream);
649
650 return ret;
651}
652
653static int sst_byt_stream_operations(struct sst_byt *byt, int type,
654 int stream_id, int wait)
655{
656 struct sst_byt_start_stream_params start_stream;
657 u64 header;
658 void *tx_msg = NULL;
659 size_t size = 0;
660
661 if (type != IPC_IA_START_STREAM) {
662 header = sst_byt_header(type, 0, false, stream_id);
663 } else {
664 start_stream.byte_offset = 0;
665 header = sst_byt_header(IPC_IA_START_STREAM,
666 sizeof(start_stream) + sizeof(u32),
667 true, stream_id);
668 tx_msg = &start_stream;
669 size = sizeof(start_stream);
670 }
671
672 if (wait)
673 return sst_byt_ipc_tx_msg_wait(byt, header,
674 tx_msg, size, NULL, 0);
675 else
676 return sst_byt_ipc_tx_msg_nowait(byt, header, tx_msg, size);
677}
678
679/* stream ALSA trigger operations */
680int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream)
681{
682 int ret;
683
684 ret = sst_byt_stream_operations(byt, IPC_IA_START_STREAM,
685 stream->str_id, 0);
686 if (ret < 0)
687 dev_err(byt->dev, "ipc: error failed to start stream %d\n",
688 stream->str_id);
689
690 return ret;
691}
692
693int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream)
694{
695 int ret;
696
697 /* don't stop streams that are not commited */
698 if (!stream->commited)
699 return 0;
700
701 ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM,
702 stream->str_id, 0);
703 if (ret < 0)
704 dev_err(byt->dev, "ipc: error failed to stop stream %d\n",
705 stream->str_id);
706 return ret;
707}
708
709int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream)
710{
711 int ret;
712
713 ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM,
714 stream->str_id, 0);
715 if (ret < 0)
716 dev_err(byt->dev, "ipc: error failed to pause stream %d\n",
717 stream->str_id);
718
719 return ret;
720}
721
722int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream)
723{
724 int ret;
725
726 ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM,
727 stream->str_id, 0);
728 if (ret < 0)
729 dev_err(byt->dev, "ipc: error failed to resume stream %d\n",
730 stream->str_id);
731
732 return ret;
733}
734
735int sst_byt_get_dsp_position(struct sst_byt *byt,
736 struct sst_byt_stream *stream, int buffer_size)
737{
738 struct sst_dsp *sst = byt->dsp;
739 struct sst_byt_tstamp fw_tstamp;
740 u8 str_id = stream->str_id;
741 u32 tstamp_offset;
742
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));
746
747 return do_div(fw_tstamp.ring_buffer_counter, buffer_size);
748}
749
750static int msg_empty_list_init(struct sst_byt *byt)
751{
752 struct ipc_message *msg;
753 int i;
754
755 byt->msg = kzalloc(sizeof(*msg) * IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
756 if (byt->msg == NULL)
757 return -ENOMEM;
758
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);
762 }
763
764 return 0;
765}
766
767struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt)
768{
769 return byt->dsp;
770}
771
772static struct sst_dsp_device byt_dev = {
773 .thread = sst_byt_irq_thread,
774 .ops = &sst_byt_ops,
775};
776
777int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata)
778{
779 struct sst_byt *byt;
780 struct sst_fw *byt_sst_fw;
781 int err;
782
783 dev_dbg(dev, "initialising Byt DSP IPC\n");
784
785 byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL);
786 if (byt == NULL)
787 return -ENOMEM;
788
789 byt->dev = dev;
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);
796
797 err = msg_empty_list_init(byt);
798 if (err < 0)
799 return -ENOMEM;
800
801 /* start the IPC message thread */
802 init_kthread_worker(&byt->kworker);
803 byt->tx_thread = kthread_run(kthread_worker_fn,
804 &byt->kworker,
805 dev_name(byt->dev));
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");
809 goto err_free_msg;
810 }
811 init_kthread_work(&byt->kwork, sst_byt_ipc_tx_msgs);
812
813 byt_dev.thread_context = byt;
814
815 /* init SST shim */
816 byt->dsp = sst_dsp_new(dev, &byt_dev, pdata);
817 if (byt->dsp == NULL) {
818 err = -ENODEV;
819 goto err_free_msg;
820 }
821
822 /* keep the DSP in reset state for base FW loading */
823 sst_dsp_reset(byt->dsp);
824
825 byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt);
826 if (byt_sst_fw == NULL) {
827 err = -ENODEV;
828 dev_err(dev, "error: failed to load firmware\n");
829 goto fw_err;
830 }
831
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));
836 if (err == 0) {
837 err = -EIO;
838 dev_err(byt->dev, "ipc: error DSP boot timeout\n");
839 goto boot_err;
840 }
841
842 pdata->dsp = byt;
843
844 return 0;
845
846boot_err:
847 sst_dsp_reset(byt->dsp);
848 sst_fw_free(byt_sst_fw);
849fw_err:
850 sst_dsp_free(byt->dsp);
851err_free_msg:
852 kfree(byt->msg);
853
854 return err;
855}
856EXPORT_SYMBOL_GPL(sst_byt_dsp_init);
857
858void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata)
859{
860 struct sst_byt *byt = pdata->dsp;
861
862 sst_dsp_reset(byt->dsp);
863 sst_fw_free_all(byt->dsp);
864 sst_dsp_free(byt->dsp);
865 kfree(byt->msg);
866}
867EXPORT_SYMBOL_GPL(sst_byt_dsp_free);