2 * Intel SST Haswell/Broadwell IPC Support
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/device.h>
21 #include <linux/wait.h>
22 #include <linux/spinlock.h>
23 #include <linux/workqueue.h>
24 #include <linux/export.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/platform_device.h>
29 #include <linux/kthread.h>
30 #include <linux/firmware.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/debugfs.h>
34 #include "sst-haswell-ipc.h"
36 #include "sst-dsp-priv.h"
38 /* Global Message - Generic */
39 #define IPC_GLB_TYPE_SHIFT 24
40 #define IPC_GLB_TYPE_MASK (0x1f << IPC_GLB_TYPE_SHIFT)
41 #define IPC_GLB_TYPE(x) (x << IPC_GLB_TYPE_SHIFT)
43 /* Global Message - Reply */
44 #define IPC_GLB_REPLY_SHIFT 0
45 #define IPC_GLB_REPLY_MASK (0x1f << IPC_GLB_REPLY_SHIFT)
46 #define IPC_GLB_REPLY_TYPE(x) (x << IPC_GLB_REPLY_TYPE_SHIFT)
48 /* Stream Message - Generic */
49 #define IPC_STR_TYPE_SHIFT 20
50 #define IPC_STR_TYPE_MASK (0xf << IPC_STR_TYPE_SHIFT)
51 #define IPC_STR_TYPE(x) (x << IPC_STR_TYPE_SHIFT)
52 #define IPC_STR_ID_SHIFT 16
53 #define IPC_STR_ID_MASK (0xf << IPC_STR_ID_SHIFT)
54 #define IPC_STR_ID(x) (x << IPC_STR_ID_SHIFT)
56 /* Stream Message - Reply */
57 #define IPC_STR_REPLY_SHIFT 0
58 #define IPC_STR_REPLY_MASK (0x1f << IPC_STR_REPLY_SHIFT)
60 /* Stream Stage Message - Generic */
61 #define IPC_STG_TYPE_SHIFT 12
62 #define IPC_STG_TYPE_MASK (0xf << IPC_STG_TYPE_SHIFT)
63 #define IPC_STG_TYPE(x) (x << IPC_STG_TYPE_SHIFT)
64 #define IPC_STG_ID_SHIFT 10
65 #define IPC_STG_ID_MASK (0x3 << IPC_STG_ID_SHIFT)
66 #define IPC_STG_ID(x) (x << IPC_STG_ID_SHIFT)
68 /* Stream Stage Message - Reply */
69 #define IPC_STG_REPLY_SHIFT 0
70 #define IPC_STG_REPLY_MASK (0x1f << IPC_STG_REPLY_SHIFT)
72 /* Debug Log Message - Generic */
73 #define IPC_LOG_OP_SHIFT 20
74 #define IPC_LOG_OP_MASK (0xf << IPC_LOG_OP_SHIFT)
75 #define IPC_LOG_OP_TYPE(x) (x << IPC_LOG_OP_SHIFT)
76 #define IPC_LOG_ID_SHIFT 16
77 #define IPC_LOG_ID_MASK (0xf << IPC_LOG_ID_SHIFT)
78 #define IPC_LOG_ID(x) (x << IPC_LOG_ID_SHIFT)
80 /* IPC message timeout (msecs) */
81 #define IPC_TIMEOUT_MSECS 300
82 #define IPC_BOOT_MSECS 200
83 #define IPC_MSG_WAIT 0
84 #define IPC_MSG_NOWAIT 1
86 /* Firmware Ready Message */
87 #define IPC_FW_READY (0x1 << 29)
88 #define IPC_STATUS_MASK (0x3 << 30)
90 #define IPC_EMPTY_LIST_SIZE 8
91 #define IPC_MAX_STREAMS 4
94 #define IPC_MAX_MAILBOX_BYTES 256
96 /* Global Message - Types and Replies */
98 IPC_GLB_GET_FW_VERSION = 0, /* Retrieves firmware version */
99 IPC_GLB_PERFORMANCE_MONITOR = 1, /* Performance monitoring actions */
100 IPC_GLB_ALLOCATE_STREAM = 3, /* Request to allocate new stream */
101 IPC_GLB_FREE_STREAM = 4, /* Request to free stream */
102 IPC_GLB_GET_FW_CAPABILITIES = 5, /* Retrieves firmware capabilities */
103 IPC_GLB_STREAM_MESSAGE = 6, /* Message directed to stream or its stages */
104 /* Request to store firmware context during D0->D3 transition */
105 IPC_GLB_REQUEST_DUMP = 7,
106 /* Request to restore firmware context during D3->D0 transition */
107 IPC_GLB_RESTORE_CONTEXT = 8,
108 IPC_GLB_GET_DEVICE_FORMATS = 9, /* Set device format */
109 IPC_GLB_SET_DEVICE_FORMATS = 10, /* Get device format */
110 IPC_GLB_SHORT_REPLY = 11,
111 IPC_GLB_ENTER_DX_STATE = 12,
112 IPC_GLB_GET_MIXER_STREAM_INFO = 13, /* Request mixer stream params */
113 IPC_GLB_DEBUG_LOG_MESSAGE = 14, /* Message to or from the debug logger. */
114 IPC_GLB_REQUEST_TRANSFER = 16, /* < Request Transfer for host */
115 IPC_GLB_MAX_IPC_MESSAGE_TYPE = 17, /* Maximum message number */
119 IPC_GLB_REPLY_SUCCESS = 0, /* The operation was successful. */
120 IPC_GLB_REPLY_ERROR_INVALID_PARAM = 1, /* Invalid parameter was passed. */
121 IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE = 2, /* Uknown message type was resceived. */
122 IPC_GLB_REPLY_OUT_OF_RESOURCES = 3, /* No resources to satisfy the request. */
123 IPC_GLB_REPLY_BUSY = 4, /* The system or resource is busy. */
124 IPC_GLB_REPLY_PENDING = 5, /* The action was scheduled for processing. */
125 IPC_GLB_REPLY_FAILURE = 6, /* Critical error happened. */
126 IPC_GLB_REPLY_INVALID_REQUEST = 7, /* Request can not be completed. */
127 IPC_GLB_REPLY_STAGE_UNINITIALIZED = 8, /* Processing stage was uninitialized. */
128 IPC_GLB_REPLY_NOT_FOUND = 9, /* Required resource can not be found. */
129 IPC_GLB_REPLY_SOURCE_NOT_STARTED = 10, /* Source was not started. */
132 /* Stream Message - Types */
133 enum ipc_str_operation {
137 IPC_STR_STAGE_MESSAGE = 3,
138 IPC_STR_NOTIFICATION = 4,
142 /* Stream Stage Message Types */
143 enum ipc_stg_operation {
144 IPC_STG_GET_VOLUME = 0,
146 IPC_STG_SET_WRITE_POSITION,
147 IPC_STG_SET_FX_ENABLE,
148 IPC_STG_SET_FX_DISABLE,
149 IPC_STG_SET_FX_GET_PARAM,
150 IPC_STG_SET_FX_SET_PARAM,
151 IPC_STG_SET_FX_GET_INFO,
152 IPC_STG_MUTE_LOOPBACK,
156 /* Stream Stage Message Types For Notification*/
157 enum ipc_stg_operation_notify {
158 IPC_POSITION_CHANGED = 0,
163 enum ipc_glitch_type {
164 IPC_GLITCH_UNDERRUN = 1,
165 IPC_GLITCH_DECODER_ERROR,
166 IPC_GLITCH_DOUBLED_WRITE_POS,
171 enum ipc_debug_operation {
172 IPC_DEBUG_ENABLE_LOG = 0,
173 IPC_DEBUG_DISABLE_LOG = 1,
174 IPC_DEBUG_REQUEST_LOG_DUMP = 2,
175 IPC_DEBUG_NOTIFY_LOG_DUMP = 3,
176 IPC_DEBUG_MAX_DEBUG_LOG
180 struct sst_hsw_ipc_fw_ready {
186 u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
187 } __attribute__((packed));
190 struct list_head list;
193 /* direction wrt host CPU */
194 char tx_data[IPC_MAX_MAILBOX_BYTES];
196 char rx_data[IPC_MAX_MAILBOX_BYTES];
199 wait_queue_head_t waitq;
206 struct sst_hsw_stream;
209 /* Stream infomation */
210 struct sst_hsw_stream {
212 struct sst_hsw_ipc_stream_alloc_req request;
213 struct sst_hsw_ipc_stream_alloc_reply reply;
214 struct sst_hsw_ipc_stream_free_req free_req;
217 u32 mute_volume[SST_HSW_NO_CHANNELS];
218 u32 mute[SST_HSW_NO_CHANNELS];
226 /* Notification work */
227 struct work_struct notify_work;
230 /* Position info from DSP */
231 struct sst_hsw_ipc_stream_set_position wpos;
232 struct sst_hsw_ipc_stream_get_position rpos;
233 struct sst_hsw_ipc_stream_glitch_position glitch;
236 struct sst_hsw_ipc_volume_req vol_req;
238 /* driver callback */
239 u32 (*notify_position)(struct sst_hsw_stream *stream, void *data);
242 struct list_head node;
245 /* FW log ring information */
246 struct sst_hsw_log_stream {
248 unsigned char *dma_area;
249 unsigned char *ring_descr;
253 /* Notification work */
254 struct work_struct notify_work;
255 wait_queue_head_t readers_wait_q;
256 struct mutex rw_mutex;
263 u32 config[SST_HSW_FW_LOG_CONFIG_DWORDS];
268 /* SST Haswell IPC data */
272 struct platform_device *pdev_pcm;
275 struct sst_hsw_ipc_fw_ready fw_ready;
276 struct sst_hsw_ipc_fw_version version;
277 struct sst_module *scratch;
281 struct list_head stream_list;
284 struct sst_hsw_ipc_stream_info_reply mixer_info;
285 enum sst_hsw_volume_curve curve_type;
287 u32 mute[SST_HSW_NO_CHANNELS];
288 u32 mute_volume[SST_HSW_NO_CHANNELS];
291 struct sst_hsw_ipc_dx_reply dx;
294 wait_queue_head_t boot_wait;
299 struct list_head tx_list;
300 struct list_head rx_list;
301 struct list_head empty_list;
302 wait_queue_head_t wait_txq;
303 struct task_struct *tx_thread;
304 struct kthread_worker kworker;
305 struct kthread_work kwork;
307 struct ipc_message *msg;
310 struct sst_hsw_log_stream log_stream;
313 #define CREATE_TRACE_POINTS
314 #include <trace/events/hswadsp.h>
316 static inline u32 msg_get_global_type(u32 msg)
318 return (msg & IPC_GLB_TYPE_MASK) >> IPC_GLB_TYPE_SHIFT;
321 static inline u32 msg_get_global_reply(u32 msg)
323 return (msg & IPC_GLB_REPLY_MASK) >> IPC_GLB_REPLY_SHIFT;
326 static inline u32 msg_get_stream_type(u32 msg)
328 return (msg & IPC_STR_TYPE_MASK) >> IPC_STR_TYPE_SHIFT;
331 static inline u32 msg_get_stage_type(u32 msg)
333 return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
336 static inline u32 msg_set_stage_type(u32 msg, u32 type)
338 return (msg & ~IPC_STG_TYPE_MASK) +
339 (type << IPC_STG_TYPE_SHIFT);
342 static inline u32 msg_get_stream_id(u32 msg)
344 return (msg & IPC_STR_ID_MASK) >> IPC_STR_ID_SHIFT;
347 static inline u32 msg_get_notify_reason(u32 msg)
349 return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
352 u32 create_channel_map(enum sst_hsw_channel_config config)
355 case SST_HSW_CHANNEL_CONFIG_MONO:
356 return (0xFFFFFFF0 | SST_HSW_CHANNEL_CENTER);
357 case SST_HSW_CHANNEL_CONFIG_STEREO:
358 return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
359 | (SST_HSW_CHANNEL_RIGHT << 4));
360 case SST_HSW_CHANNEL_CONFIG_2_POINT_1:
361 return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
362 | (SST_HSW_CHANNEL_RIGHT << 4)
363 | (SST_HSW_CHANNEL_LFE << 8 ));
364 case SST_HSW_CHANNEL_CONFIG_3_POINT_0:
365 return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
366 | (SST_HSW_CHANNEL_CENTER << 4)
367 | (SST_HSW_CHANNEL_RIGHT << 8));
368 case SST_HSW_CHANNEL_CONFIG_3_POINT_1:
369 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
370 | (SST_HSW_CHANNEL_CENTER << 4)
371 | (SST_HSW_CHANNEL_RIGHT << 8)
372 | (SST_HSW_CHANNEL_LFE << 12));
373 case SST_HSW_CHANNEL_CONFIG_QUATRO:
374 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
375 | (SST_HSW_CHANNEL_RIGHT << 4)
376 | (SST_HSW_CHANNEL_LEFT_SURROUND << 8)
377 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 12));
378 case SST_HSW_CHANNEL_CONFIG_4_POINT_0:
379 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
380 | (SST_HSW_CHANNEL_CENTER << 4)
381 | (SST_HSW_CHANNEL_RIGHT << 8)
382 | (SST_HSW_CHANNEL_CENTER_SURROUND << 12));
383 case SST_HSW_CHANNEL_CONFIG_5_POINT_0:
384 return (0xFFF00000 | SST_HSW_CHANNEL_LEFT
385 | (SST_HSW_CHANNEL_CENTER << 4)
386 | (SST_HSW_CHANNEL_RIGHT << 8)
387 | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
388 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16));
389 case SST_HSW_CHANNEL_CONFIG_5_POINT_1:
390 return (0xFF000000 | SST_HSW_CHANNEL_CENTER
391 | (SST_HSW_CHANNEL_LEFT << 4)
392 | (SST_HSW_CHANNEL_RIGHT << 8)
393 | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
394 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16)
395 | (SST_HSW_CHANNEL_LFE << 20));
396 case SST_HSW_CHANNEL_CONFIG_DUAL_MONO:
397 return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
398 | (SST_HSW_CHANNEL_LEFT << 4));
404 static struct sst_hsw_stream *get_stream_by_id(struct sst_hsw *hsw,
407 struct sst_hsw_stream *stream;
409 list_for_each_entry(stream, &hsw->stream_list, node) {
410 if (stream->reply.stream_hw_id == stream_id)
417 static void ipc_shim_dbg(struct sst_hsw *hsw, const char *text)
419 struct sst_dsp *sst = hsw->dsp;
420 u32 isr, ipcd, imrx, ipcx;
422 ipcx = sst_dsp_shim_read_unlocked(sst, SST_IPCX);
423 isr = sst_dsp_shim_read_unlocked(sst, SST_ISRX);
424 ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
425 imrx = sst_dsp_shim_read_unlocked(sst, SST_IMRX);
427 dev_err(hsw->dev, "ipc: --%s-- ipcx 0x%8.8x isr 0x%8.8x ipcd 0x%8.8x imrx 0x%8.8x\n",
428 text, ipcx, isr, ipcd, imrx);
431 /* locks held by caller */
432 static struct ipc_message *msg_get_empty(struct sst_hsw *hsw)
434 struct ipc_message *msg = NULL;
436 if (!list_empty(&hsw->empty_list)) {
437 msg = list_first_entry(&hsw->empty_list, struct ipc_message,
439 list_del(&msg->list);
445 static void ipc_tx_msgs(struct kthread_work *work)
447 struct sst_hsw *hsw =
448 container_of(work, struct sst_hsw, kwork);
449 struct ipc_message *msg;
453 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
455 if (list_empty(&hsw->tx_list) || hsw->pending) {
456 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
460 /* if the DSP is busy, we will TX messages after IRQ.
461 * also postpone if we are in the middle of procesing completion irq*/
462 ipcx = sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX);
463 if (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE)) {
464 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
468 msg = list_first_entry(&hsw->tx_list, struct ipc_message, list);
470 list_move(&msg->list, &hsw->rx_list);
472 /* send the message */
473 sst_dsp_outbox_write(hsw->dsp, msg->tx_data, msg->tx_size);
474 sst_dsp_ipc_msg_tx(hsw->dsp, msg->header | SST_IPCX_BUSY);
476 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
479 /* locks held by caller */
480 static void tx_msg_reply_complete(struct sst_hsw *hsw, struct ipc_message *msg)
482 msg->complete = true;
483 trace_ipc_reply("completed", msg->header);
486 list_add_tail(&msg->list, &hsw->empty_list);
488 wake_up(&msg->waitq);
491 static int tx_wait_done(struct sst_hsw *hsw, struct ipc_message *msg,
497 /* wait for DSP completion (in all cases atm inc pending) */
498 ret = wait_event_timeout(msg->waitq, msg->complete,
499 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
501 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
503 ipc_shim_dbg(hsw, "message timeout");
505 trace_ipc_error("error message timeout for", msg->header);
506 list_del(&msg->list);
510 /* copy the data returned from DSP */
512 memcpy(rx_data, msg->rx_data, msg->rx_size);
516 list_add_tail(&msg->list, &hsw->empty_list);
517 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
521 static int ipc_tx_message(struct sst_hsw *hsw, u32 header, void *tx_data,
522 size_t tx_bytes, void *rx_data, size_t rx_bytes, int wait)
524 struct ipc_message *msg;
527 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
529 msg = msg_get_empty(hsw);
531 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
536 memcpy(msg->tx_data, tx_data, tx_bytes);
538 msg->header = header;
539 msg->tx_size = tx_bytes;
540 msg->rx_size = rx_bytes;
543 msg->pending = false;
544 msg->complete = false;
546 list_add_tail(&msg->list, &hsw->tx_list);
547 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
549 queue_kthread_work(&hsw->kworker, &hsw->kwork);
552 return tx_wait_done(hsw, msg, rx_data);
557 static inline int ipc_tx_message_wait(struct sst_hsw *hsw, u32 header,
558 void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes)
560 return ipc_tx_message(hsw, header, tx_data, tx_bytes, rx_data,
564 static inline int ipc_tx_message_nowait(struct sst_hsw *hsw, u32 header,
565 void *tx_data, size_t tx_bytes)
567 return ipc_tx_message(hsw, header, tx_data, tx_bytes, NULL, 0, 0);
570 static void hsw_fw_ready(struct sst_hsw *hsw, u32 header)
572 struct sst_hsw_ipc_fw_ready fw_ready;
574 u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
575 char *tmp[5], *pinfo;
578 offset = (header & 0x1FFFFFFF) << 3;
580 dev_dbg(hsw->dev, "ipc: DSP is ready 0x%8.8x offset %d\n",
583 /* copy data from the DSP FW ready offset */
584 sst_dsp_read(hsw->dsp, &fw_ready, offset, sizeof(fw_ready));
586 sst_dsp_mailbox_init(hsw->dsp, fw_ready.inbox_offset,
587 fw_ready.inbox_size, fw_ready.outbox_offset,
588 fw_ready.outbox_size);
590 hsw->boot_complete = true;
591 wake_up(&hsw->boot_wait);
593 dev_dbg(hsw->dev, " mailbox upstream 0x%x - size 0x%x\n",
594 fw_ready.inbox_offset, fw_ready.inbox_size);
595 dev_dbg(hsw->dev, " mailbox downstream 0x%x - size 0x%x\n",
596 fw_ready.outbox_offset, fw_ready.outbox_size);
597 if (fw_ready.fw_info_size < sizeof(fw_ready.fw_info)) {
598 fw_ready.fw_info[fw_ready.fw_info_size] = 0;
599 dev_dbg(hsw->dev, " Firmware info: %s \n", fw_ready.fw_info);
601 /* log the FW version info got from the mailbox here. */
602 memcpy(fw_info, fw_ready.fw_info, fw_ready.fw_info_size);
604 for (i = 0; i < sizeof(tmp) / sizeof(char *); i++)
605 tmp[i] = strsep(&pinfo, " ");
606 dev_info(hsw->dev, "FW loaded, mailbox readback FW info: type %s, - "
607 "version: %s.%s, build %s, source commit id: %s\n",
608 tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
612 static void hsw_notification_work(struct work_struct *work)
614 struct sst_hsw_stream *stream = container_of(work,
615 struct sst_hsw_stream, notify_work);
616 struct sst_hsw_ipc_stream_glitch_position *glitch = &stream->glitch;
617 struct sst_hsw_ipc_stream_get_position *pos = &stream->rpos;
618 struct sst_hsw *hsw = stream->hsw;
621 reason = msg_get_notify_reason(stream->header);
625 trace_ipc_notification("DSP stream under/overrun",
626 stream->reply.stream_hw_id);
627 sst_dsp_inbox_read(hsw->dsp, glitch, sizeof(*glitch));
629 dev_err(hsw->dev, "glitch %d pos 0x%x write pos 0x%x\n",
630 glitch->glitch_type, glitch->present_pos,
634 case IPC_POSITION_CHANGED:
635 trace_ipc_notification("DSP stream position changed for",
636 stream->reply.stream_hw_id);
637 sst_dsp_inbox_read(hsw->dsp, pos, sizeof(*pos));
639 if (stream->notify_position)
640 stream->notify_position(stream, stream->pdata);
644 dev_err(hsw->dev, "error: unknown notification 0x%x\n",
649 /* tell DSP that notification has been handled */
650 sst_dsp_shim_update_bits_unlocked(hsw->dsp, SST_IPCD,
651 SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
653 /* unmask busy interrupt */
654 sst_dsp_shim_update_bits_unlocked(hsw->dsp, SST_IMRX, SST_IMRX_BUSY, 0);
657 static struct ipc_message *reply_find_msg(struct sst_hsw *hsw, u32 header)
659 struct ipc_message *msg;
661 /* clear reply bits & status bits */
662 header &= ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
664 if (list_empty(&hsw->rx_list)) {
665 dev_err(hsw->dev, "error: rx list empty but received 0x%x\n",
670 list_for_each_entry(msg, &hsw->rx_list, list) {
671 if (msg->header == header)
678 static void hsw_stream_update(struct sst_hsw *hsw, struct ipc_message *msg)
680 struct sst_hsw_stream *stream;
681 u32 header = msg->header & ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
682 u32 stream_id = msg_get_stream_id(header);
683 u32 stream_msg = msg_get_stream_type(header);
685 stream = get_stream_by_id(hsw, stream_id);
689 switch (stream_msg) {
690 case IPC_STR_STAGE_MESSAGE:
691 case IPC_STR_NOTIFICATION:
694 trace_ipc_notification("stream reset", stream->reply.stream_hw_id);
697 stream->running = false;
698 trace_ipc_notification("stream paused",
699 stream->reply.stream_hw_id);
702 stream->running = true;
703 trace_ipc_notification("stream running",
704 stream->reply.stream_hw_id);
709 static int hsw_process_reply(struct sst_hsw *hsw, u32 header)
711 struct ipc_message *msg;
712 u32 reply = msg_get_global_reply(header);
714 trace_ipc_reply("processing -->", header);
716 msg = reply_find_msg(hsw, header);
718 trace_ipc_error("error: can't find message header", header);
722 /* first process the header */
724 case IPC_GLB_REPLY_PENDING:
725 trace_ipc_pending_reply("received", header);
729 case IPC_GLB_REPLY_SUCCESS:
731 trace_ipc_pending_reply("completed", header);
732 sst_dsp_inbox_read(hsw->dsp, msg->rx_data,
734 hsw->pending = false;
736 /* copy data from the DSP */
737 sst_dsp_outbox_read(hsw->dsp, msg->rx_data,
741 /* these will be rare - but useful for debug */
742 case IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE:
743 trace_ipc_error("error: unknown message type", header);
744 msg->errno = -EBADMSG;
746 case IPC_GLB_REPLY_OUT_OF_RESOURCES:
747 trace_ipc_error("error: out of resources", header);
748 msg->errno = -ENOMEM;
750 case IPC_GLB_REPLY_BUSY:
751 trace_ipc_error("error: reply busy", header);
754 case IPC_GLB_REPLY_FAILURE:
755 trace_ipc_error("error: reply failure", header);
756 msg->errno = -EINVAL;
758 case IPC_GLB_REPLY_STAGE_UNINITIALIZED:
759 trace_ipc_error("error: stage uninitialized", header);
760 msg->errno = -EINVAL;
762 case IPC_GLB_REPLY_NOT_FOUND:
763 trace_ipc_error("error: reply not found", header);
764 msg->errno = -EINVAL;
766 case IPC_GLB_REPLY_SOURCE_NOT_STARTED:
767 trace_ipc_error("error: source not started", header);
768 msg->errno = -EINVAL;
770 case IPC_GLB_REPLY_INVALID_REQUEST:
771 trace_ipc_error("error: invalid request", header);
772 msg->errno = -EINVAL;
774 case IPC_GLB_REPLY_ERROR_INVALID_PARAM:
775 trace_ipc_error("error: invalid parameter", header);
776 msg->errno = -EINVAL;
779 trace_ipc_error("error: unknown reply", header);
780 msg->errno = -EINVAL;
784 /* update any stream states */
785 if (msg_get_global_type(header) == IPC_GLB_STREAM_MESSAGE)
786 hsw_stream_update(hsw, msg);
788 /* wake up and return the error if we have waiters on this message ? */
789 list_del(&msg->list);
790 tx_msg_reply_complete(hsw, msg);
795 static int hsw_stream_message(struct sst_hsw *hsw, u32 header)
797 u32 stream_msg, stream_id, stage_type;
798 struct sst_hsw_stream *stream;
801 stream_msg = msg_get_stream_type(header);
802 stream_id = msg_get_stream_id(header);
803 stage_type = msg_get_stage_type(header);
805 stream = get_stream_by_id(hsw, stream_id);
809 stream->header = header;
811 switch (stream_msg) {
812 case IPC_STR_STAGE_MESSAGE:
813 dev_err(hsw->dev, "error: stage msg not implemented 0x%8.8x\n",
816 case IPC_STR_NOTIFICATION:
817 schedule_work(&stream->notify_work);
820 /* handle pending message complete request */
821 handled = hsw_process_reply(hsw, header);
828 static int hsw_log_message(struct sst_hsw *hsw, u32 header)
830 u32 operation = (header & IPC_LOG_OP_MASK) >> IPC_LOG_OP_SHIFT;
831 struct sst_hsw_log_stream *stream = &hsw->log_stream;
834 if (operation != IPC_DEBUG_REQUEST_LOG_DUMP) {
836 "error: log msg not implemented 0x%8.8x\n", header);
840 mutex_lock(&stream->rw_mutex);
841 stream->last_pos = stream->curr_pos;
843 hsw->dsp, &stream->curr_pos, sizeof(stream->curr_pos));
844 mutex_unlock(&stream->rw_mutex);
846 schedule_work(&stream->notify_work);
851 static int hsw_process_notification(struct sst_hsw *hsw)
853 struct sst_dsp *sst = hsw->dsp;
857 header = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
858 type = msg_get_global_type(header);
860 trace_ipc_request("processing -->", header);
862 /* FW Ready is a special case */
863 if (!hsw->boot_complete && header & IPC_FW_READY) {
864 hsw_fw_ready(hsw, header);
869 case IPC_GLB_GET_FW_VERSION:
870 case IPC_GLB_ALLOCATE_STREAM:
871 case IPC_GLB_FREE_STREAM:
872 case IPC_GLB_GET_FW_CAPABILITIES:
873 case IPC_GLB_REQUEST_DUMP:
874 case IPC_GLB_GET_DEVICE_FORMATS:
875 case IPC_GLB_SET_DEVICE_FORMATS:
876 case IPC_GLB_ENTER_DX_STATE:
877 case IPC_GLB_GET_MIXER_STREAM_INFO:
878 case IPC_GLB_MAX_IPC_MESSAGE_TYPE:
879 case IPC_GLB_RESTORE_CONTEXT:
880 case IPC_GLB_SHORT_REPLY:
881 dev_err(hsw->dev, "error: message type %d header 0x%x\n",
884 case IPC_GLB_STREAM_MESSAGE:
885 handled = hsw_stream_message(hsw, header);
887 case IPC_GLB_DEBUG_LOG_MESSAGE:
888 handled = hsw_log_message(hsw, header);
891 dev_err(hsw->dev, "error: unexpected type %d hdr 0x%8.8x\n",
899 static irqreturn_t hsw_irq_thread(int irq, void *context)
901 struct sst_dsp *sst = (struct sst_dsp *) context;
902 struct sst_hsw *hsw = sst_dsp_get_thread_context(sst);
907 spin_lock_irqsave(&sst->spinlock, flags);
909 ipcx = sst_dsp_ipc_msg_rx(hsw->dsp);
910 ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
912 /* reply message from DSP */
913 if (ipcx & SST_IPCX_DONE) {
915 /* Handle Immediate reply from DSP Core */
916 handled = hsw_process_reply(hsw, ipcx);
919 /* clear DONE bit - tell DSP we have completed */
920 sst_dsp_shim_update_bits_unlocked(sst, SST_IPCX,
923 /* unmask Done interrupt */
924 sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
929 /* new message from DSP */
930 if (ipcd & SST_IPCD_BUSY) {
932 /* Handle Notification and Delayed reply from DSP Core */
933 handled = hsw_process_notification(hsw);
935 /* clear BUSY bit and set DONE bit - accept new messages */
937 sst_dsp_shim_update_bits_unlocked(sst, SST_IPCD,
938 SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
940 /* unmask busy interrupt */
941 sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
946 spin_unlock_irqrestore(&sst->spinlock, flags);
948 /* continue to send any remaining messages... */
949 queue_kthread_work(&hsw->kworker, &hsw->kwork);
954 int sst_hsw_fw_get_version(struct sst_hsw *hsw,
955 struct sst_hsw_ipc_fw_version *version)
959 ret = ipc_tx_message_wait(hsw, IPC_GLB_TYPE(IPC_GLB_GET_FW_VERSION),
960 NULL, 0, version, sizeof(*version));
962 dev_err(hsw->dev, "error: get version failed\n");
968 int sst_hsw_stream_mute(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
969 u32 stage_id, u32 channel)
973 ret = sst_hsw_stream_get_volume(hsw, stream, stage_id, channel,
974 &stream->mute_volume[channel]);
978 ret = sst_hsw_stream_set_volume(hsw, stream, stage_id, channel, 0);
980 dev_err(hsw->dev, "error: can't unmute stream %d channel %d\n",
981 stream->reply.stream_hw_id, channel);
985 stream->mute[channel] = 1;
989 int sst_hsw_stream_unmute(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
990 u32 stage_id, u32 channel)
995 stream->mute[channel] = 0;
996 ret = sst_hsw_stream_set_volume(hsw, stream, stage_id, channel,
997 stream->mute_volume[channel]);
999 dev_err(hsw->dev, "error: can't unmute stream %d channel %d\n",
1000 stream->reply.stream_hw_id, channel);
1007 int sst_hsw_stream_get_volume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1008 u32 stage_id, u32 channel, u32 *volume)
1013 sst_dsp_read(hsw->dsp, volume,
1014 stream->reply.volume_register_address[channel],
1020 int sst_hsw_stream_set_volume_curve(struct sst_hsw *hsw,
1021 struct sst_hsw_stream *stream, u64 curve_duration,
1022 enum sst_hsw_volume_curve curve)
1024 /* curve duration in steps of 100ns */
1025 stream->vol_req.curve_duration = curve_duration;
1026 stream->vol_req.curve_type = curve;
1032 int sst_hsw_stream_set_volume(struct sst_hsw *hsw,
1033 struct sst_hsw_stream *stream, u32 stage_id, u32 channel, u32 volume)
1035 struct sst_hsw_ipc_volume_req *req;
1039 trace_ipc_request("set stream volume", stream->reply.stream_hw_id);
1044 if (stream->mute[channel]) {
1045 stream->mute_volume[channel] = volume;
1049 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
1050 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
1051 header |= (stream->reply.stream_hw_id << IPC_STR_ID_SHIFT);
1052 header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
1053 header |= (stage_id << IPC_STG_ID_SHIFT);
1055 req = &stream->vol_req;
1056 req->channel = channel;
1057 req->target_volume = volume;
1059 ret = ipc_tx_message_wait(hsw, header, req, sizeof(*req), NULL, 0);
1061 dev_err(hsw->dev, "error: set stream volume failed\n");
1068 int sst_hsw_mixer_mute(struct sst_hsw *hsw, u32 stage_id, u32 channel)
1072 ret = sst_hsw_mixer_get_volume(hsw, stage_id, channel,
1073 &hsw->mute_volume[channel]);
1077 ret = sst_hsw_mixer_set_volume(hsw, stage_id, channel, 0);
1079 dev_err(hsw->dev, "error: failed to unmute mixer channel %d\n",
1084 hsw->mute[channel] = 1;
1088 int sst_hsw_mixer_unmute(struct sst_hsw *hsw, u32 stage_id, u32 channel)
1092 ret = sst_hsw_mixer_set_volume(hsw, stage_id, channel,
1093 hsw->mixer_info.volume_register_address[channel]);
1095 dev_err(hsw->dev, "error: failed to unmute mixer channel %d\n",
1100 hsw->mute[channel] = 0;
1104 int sst_hsw_mixer_get_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
1110 sst_dsp_read(hsw->dsp, volume,
1111 hsw->mixer_info.volume_register_address[channel],
1117 int sst_hsw_mixer_set_volume_curve(struct sst_hsw *hsw,
1118 u64 curve_duration, enum sst_hsw_volume_curve curve)
1120 /* curve duration in steps of 100ns */
1121 hsw->curve_duration = curve_duration;
1122 hsw->curve_type = curve;
1127 /* global mixer volume */
1128 int sst_hsw_mixer_set_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
1131 struct sst_hsw_ipc_volume_req req;
1135 trace_ipc_request("set mixer volume", volume);
1137 /* set both at same time ? */
1139 if (hsw->mute[0] && hsw->mute[1]) {
1140 hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
1142 } else if (hsw->mute[0])
1144 else if (hsw->mute[1])
1147 req.channel = 0xffffffff;
1149 /* set only 1 channel */
1150 if (hsw->mute[channel]) {
1151 hsw->mute_volume[channel] = volume;
1154 req.channel = channel;
1157 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
1158 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
1159 header |= (hsw->mixer_info.mixer_hw_id << IPC_STR_ID_SHIFT);
1160 header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
1161 header |= (stage_id << IPC_STG_ID_SHIFT);
1163 req.curve_duration = hsw->curve_duration;
1164 req.curve_type = hsw->curve_type;
1165 req.target_volume = volume;
1167 ret = ipc_tx_message_wait(hsw, header, &req, sizeof(req), NULL, 0);
1169 dev_err(hsw->dev, "error: set mixer volume failed\n");
1177 struct sst_hsw_stream *sst_hsw_stream_new(struct sst_hsw *hsw, int id,
1178 u32 (*notify_position)(struct sst_hsw_stream *stream, void *data),
1181 struct sst_hsw_stream *stream;
1182 struct sst_dsp *sst = hsw->dsp;
1183 unsigned long flags;
1185 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1189 spin_lock_irqsave(&sst->spinlock, flags);
1190 list_add(&stream->node, &hsw->stream_list);
1191 stream->notify_position = notify_position;
1192 stream->pdata = data;
1194 stream->host_id = id;
1196 /* work to process notification messages */
1197 INIT_WORK(&stream->notify_work, hsw_notification_work);
1198 spin_unlock_irqrestore(&sst->spinlock, flags);
1203 int sst_hsw_stream_free(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1207 struct sst_dsp *sst = hsw->dsp;
1208 unsigned long flags;
1210 /* dont free DSP streams that are not commited */
1211 if (!stream->commited)
1214 trace_ipc_request("stream free", stream->host_id);
1216 stream->free_req.stream_id = stream->reply.stream_hw_id;
1217 header = IPC_GLB_TYPE(IPC_GLB_FREE_STREAM);
1219 ret = ipc_tx_message_wait(hsw, header, &stream->free_req,
1220 sizeof(stream->free_req), NULL, 0);
1222 dev_err(hsw->dev, "error: free stream %d failed\n",
1223 stream->free_req.stream_id);
1227 trace_hsw_stream_free_req(stream, &stream->free_req);
1230 cancel_work_sync(&stream->notify_work);
1231 spin_lock_irqsave(&sst->spinlock, flags);
1232 list_del(&stream->node);
1234 spin_unlock_irqrestore(&sst->spinlock, flags);
1239 int sst_hsw_stream_set_bits(struct sst_hsw *hsw,
1240 struct sst_hsw_stream *stream, enum sst_hsw_bitdepth bits)
1242 if (stream->commited) {
1243 dev_err(hsw->dev, "error: stream committed for set bits\n");
1247 stream->request.format.bitdepth = bits;
1251 int sst_hsw_stream_set_channels(struct sst_hsw *hsw,
1252 struct sst_hsw_stream *stream, int channels)
1254 if (stream->commited) {
1255 dev_err(hsw->dev, "error: stream committed for set channels\n");
1259 /* stereo is only supported atm */
1263 stream->request.format.ch_num = channels;
1267 int sst_hsw_stream_set_rate(struct sst_hsw *hsw,
1268 struct sst_hsw_stream *stream, int rate)
1270 if (stream->commited) {
1271 dev_err(hsw->dev, "error: stream committed for set rate\n");
1275 stream->request.format.frequency = rate;
1279 int sst_hsw_stream_set_map_config(struct sst_hsw *hsw,
1280 struct sst_hsw_stream *stream, u32 map,
1281 enum sst_hsw_channel_config config)
1283 if (stream->commited) {
1284 dev_err(hsw->dev, "error: stream committed for set map\n");
1288 stream->request.format.map = map;
1289 stream->request.format.config = config;
1293 int sst_hsw_stream_set_style(struct sst_hsw *hsw,
1294 struct sst_hsw_stream *stream, enum sst_hsw_interleaving style)
1296 if (stream->commited) {
1297 dev_err(hsw->dev, "error: stream committed for set style\n");
1301 stream->request.format.style = style;
1305 int sst_hsw_stream_set_valid(struct sst_hsw *hsw,
1306 struct sst_hsw_stream *stream, u32 bits)
1308 if (stream->commited) {
1309 dev_err(hsw->dev, "error: stream committed for set valid bits\n");
1313 stream->request.format.valid_bit = bits;
1317 /* Stream Configuration */
1318 int sst_hsw_stream_format(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1319 enum sst_hsw_stream_path_id path_id,
1320 enum sst_hsw_stream_type stream_type,
1321 enum sst_hsw_stream_format format_id)
1323 if (stream->commited) {
1324 dev_err(hsw->dev, "error: stream committed for set format\n");
1328 stream->request.path_id = path_id;
1329 stream->request.stream_type = stream_type;
1330 stream->request.format_id = format_id;
1332 trace_hsw_stream_alloc_request(stream, &stream->request);
1337 int sst_hsw_stream_buffer(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1338 u32 ring_pt_address, u32 num_pages,
1339 u32 ring_size, u32 ring_offset, u32 ring_first_pfn)
1341 if (stream->commited) {
1342 dev_err(hsw->dev, "error: stream committed for buffer\n");
1346 stream->request.ringinfo.ring_pt_address = ring_pt_address;
1347 stream->request.ringinfo.num_pages = num_pages;
1348 stream->request.ringinfo.ring_size = ring_size;
1349 stream->request.ringinfo.ring_offset = ring_offset;
1350 stream->request.ringinfo.ring_first_pfn = ring_first_pfn;
1352 trace_hsw_stream_buffer(stream);
1357 int sst_hsw_stream_set_module_info(struct sst_hsw *hsw,
1358 struct sst_hsw_stream *stream, enum sst_hsw_module_id module_id,
1361 struct sst_hsw_module_map *map = &stream->request.map;
1363 if (stream->commited) {
1364 dev_err(hsw->dev, "error: stream committed for set module\n");
1368 /* only support initial module atm */
1369 map->module_entries_count = 1;
1370 map->module_entries[0].module_id = module_id;
1371 map->module_entries[0].entry_point = entry_point;
1376 int sst_hsw_stream_set_pmemory_info(struct sst_hsw *hsw,
1377 struct sst_hsw_stream *stream, u32 offset, u32 size)
1379 if (stream->commited) {
1380 dev_err(hsw->dev, "error: stream committed for set pmem\n");
1384 stream->request.persistent_mem.offset = offset;
1385 stream->request.persistent_mem.size = size;
1390 int sst_hsw_stream_set_smemory_info(struct sst_hsw *hsw,
1391 struct sst_hsw_stream *stream, u32 offset, u32 size)
1393 if (stream->commited) {
1394 dev_err(hsw->dev, "error: stream committed for set smem\n");
1398 stream->request.scratch_mem.offset = offset;
1399 stream->request.scratch_mem.size = size;
1404 int sst_hsw_stream_commit(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1406 struct sst_hsw_ipc_stream_alloc_req *str_req = &stream->request;
1407 struct sst_hsw_ipc_stream_alloc_reply *reply = &stream->reply;
1411 trace_ipc_request("stream alloc", stream->host_id);
1413 header = IPC_GLB_TYPE(IPC_GLB_ALLOCATE_STREAM);
1415 ret = ipc_tx_message_wait(hsw, header, str_req, sizeof(*str_req),
1416 reply, sizeof(*reply));
1418 dev_err(hsw->dev, "error: stream commit failed\n");
1422 stream->commited = 1;
1423 trace_hsw_stream_alloc_reply(stream);
1428 /* Stream Information - these calls could be inline but we want the IPC
1429 ABI to be opaque to client PCM drivers to cope with any future ABI changes */
1430 int sst_hsw_stream_get_hw_id(struct sst_hsw *hsw,
1431 struct sst_hsw_stream *stream)
1433 return stream->reply.stream_hw_id;
1436 int sst_hsw_stream_get_mixer_id(struct sst_hsw *hsw,
1437 struct sst_hsw_stream *stream)
1439 return stream->reply.mixer_hw_id;
1442 u32 sst_hsw_stream_get_read_reg(struct sst_hsw *hsw,
1443 struct sst_hsw_stream *stream)
1445 return stream->reply.read_position_register_address;
1448 u32 sst_hsw_stream_get_pointer_reg(struct sst_hsw *hsw,
1449 struct sst_hsw_stream *stream)
1451 return stream->reply.presentation_position_register_address;
1454 u32 sst_hsw_stream_get_peak_reg(struct sst_hsw *hsw,
1455 struct sst_hsw_stream *stream, u32 channel)
1460 return stream->reply.peak_meter_register_address[channel];
1463 u32 sst_hsw_stream_get_vol_reg(struct sst_hsw *hsw,
1464 struct sst_hsw_stream *stream, u32 channel)
1469 return stream->reply.volume_register_address[channel];
1472 int sst_hsw_mixer_get_info(struct sst_hsw *hsw)
1474 struct sst_hsw_ipc_stream_info_reply *reply;
1478 reply = &hsw->mixer_info;
1479 header = IPC_GLB_TYPE(IPC_GLB_GET_MIXER_STREAM_INFO);
1481 trace_ipc_request("get global mixer info", 0);
1483 ret = ipc_tx_message_wait(hsw, header, NULL, 0, reply, sizeof(*reply));
1485 dev_err(hsw->dev, "error: get stream info failed\n");
1489 trace_hsw_mixer_info_reply(reply);
1494 /* Send stream command */
1495 static int sst_hsw_stream_operations(struct sst_hsw *hsw, int type,
1496 int stream_id, int wait)
1500 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) | IPC_STR_TYPE(type);
1501 header |= (stream_id << IPC_STR_ID_SHIFT);
1504 return ipc_tx_message_wait(hsw, header, NULL, 0, NULL, 0);
1506 return ipc_tx_message_nowait(hsw, header, NULL, 0);
1509 /* Stream ALSA trigger operations */
1510 int sst_hsw_stream_pause(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1515 trace_ipc_request("stream pause", stream->reply.stream_hw_id);
1517 ret = sst_hsw_stream_operations(hsw, IPC_STR_PAUSE,
1518 stream->reply.stream_hw_id, wait);
1520 dev_err(hsw->dev, "error: failed to pause stream %d\n",
1521 stream->reply.stream_hw_id);
1526 int sst_hsw_stream_resume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1531 trace_ipc_request("stream resume", stream->reply.stream_hw_id);
1533 ret = sst_hsw_stream_operations(hsw, IPC_STR_RESUME,
1534 stream->reply.stream_hw_id, wait);
1536 dev_err(hsw->dev, "error: failed to resume stream %d\n",
1537 stream->reply.stream_hw_id);
1542 int sst_hsw_stream_reset(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1544 int ret, tries = 10;
1546 /* dont reset streams that are not commited */
1547 if (!stream->commited)
1550 /* wait for pause to complete before we reset the stream */
1551 while (stream->running && tries--)
1554 dev_err(hsw->dev, "error: reset stream %d still running\n",
1555 stream->reply.stream_hw_id);
1559 trace_ipc_request("stream reset", stream->reply.stream_hw_id);
1561 ret = sst_hsw_stream_operations(hsw, IPC_STR_RESET,
1562 stream->reply.stream_hw_id, 1);
1564 dev_err(hsw->dev, "error: failed to reset stream %d\n",
1565 stream->reply.stream_hw_id);
1569 /* Stream pointer positions */
1570 u32 sst_hsw_get_dsp_position(struct sst_hsw *hsw,
1571 struct sst_hsw_stream *stream)
1575 sst_dsp_read(hsw->dsp, &rpos,
1576 stream->reply.read_position_register_address, sizeof(rpos));
1581 /* Stream presentation (monotonic) positions */
1582 u64 sst_hsw_get_dsp_presentation_position(struct sst_hsw *hsw,
1583 struct sst_hsw_stream *stream)
1587 sst_dsp_read(hsw->dsp, &ppos,
1588 stream->reply.presentation_position_register_address,
1594 int sst_hsw_stream_set_write_position(struct sst_hsw *hsw,
1595 struct sst_hsw_stream *stream, u32 stage_id, u32 position)
1600 trace_stream_write_position(stream->reply.stream_hw_id, position);
1602 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
1603 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
1604 header |= (stream->reply.stream_hw_id << IPC_STR_ID_SHIFT);
1605 header |= (IPC_STG_SET_WRITE_POSITION << IPC_STG_TYPE_SHIFT);
1606 header |= (stage_id << IPC_STG_ID_SHIFT);
1607 stream->wpos.position = position;
1609 ret = ipc_tx_message_nowait(hsw, header, &stream->wpos,
1610 sizeof(stream->wpos));
1612 dev_err(hsw->dev, "error: stream %d set position %d failed\n",
1613 stream->reply.stream_hw_id, position);
1618 /* physical BE config */
1619 int sst_hsw_device_set_config(struct sst_hsw *hsw,
1620 enum sst_hsw_device_id dev, enum sst_hsw_device_mclk mclk,
1621 enum sst_hsw_device_mode mode, u32 clock_divider)
1623 struct sst_hsw_ipc_device_config_req config;
1627 trace_ipc_request("set device config", dev);
1629 config.ssp_interface = dev;
1630 config.clock_frequency = mclk;
1632 config.clock_divider = clock_divider;
1634 trace_hsw_device_config_req(&config);
1636 header = IPC_GLB_TYPE(IPC_GLB_SET_DEVICE_FORMATS);
1638 ret = ipc_tx_message_wait(hsw, header, &config, sizeof(config),
1641 dev_err(hsw->dev, "error: set device formats failed\n");
1645 EXPORT_SYMBOL_GPL(sst_hsw_device_set_config);
1648 int sst_hsw_dx_set_state(struct sst_hsw *hsw,
1649 enum sst_hsw_dx_state state, struct sst_hsw_ipc_dx_reply *dx)
1654 header = IPC_GLB_TYPE(IPC_GLB_ENTER_DX_STATE);
1657 trace_ipc_request("PM enter Dx state", state);
1659 ret = ipc_tx_message_wait(hsw, header, &state_, sizeof(state_),
1662 dev_err(hsw->dev, "ipc: error set dx state %d failed\n", state);
1666 for (item = 0; item < dx->entries_no; item++) {
1668 "Item[%d] offset[%x] - size[%x] - source[%x]\n",
1669 item, dx->mem_info[item].offset,
1670 dx->mem_info[item].size,
1671 dx->mem_info[item].source);
1673 dev_dbg(hsw->dev, "ipc: got %d entry numbers for state %d\n",
1674 dx->entries_no, state);
1676 memcpy(&hsw->dx, dx, sizeof(*dx));
1680 /* Used to save state into hsw->dx_reply */
1681 int sst_hsw_dx_get_state(struct sst_hsw *hsw, u32 item,
1682 u32 *offset, u32 *size, u32 *source)
1684 struct sst_hsw_ipc_dx_memory_item *dx_mem;
1685 struct sst_hsw_ipc_dx_reply *dx_reply;
1688 dx_reply = &hsw->dx;
1689 entry_no = dx_reply->entries_no;
1691 trace_ipc_request("PM get Dx state", entry_no);
1693 if (item >= entry_no)
1696 dx_mem = &dx_reply->mem_info[item];
1697 *offset = dx_mem->offset;
1698 *size = dx_mem->size;
1699 *source = dx_mem->source;
1704 static int msg_empty_list_init(struct sst_hsw *hsw)
1708 hsw->msg = kzalloc(sizeof(struct ipc_message) *
1709 IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
1710 if (hsw->msg == NULL)
1713 for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
1714 init_waitqueue_head(&hsw->msg[i].waitq);
1715 list_add(&hsw->msg[i].list, &hsw->empty_list);
1721 void sst_hsw_set_scratch_module(struct sst_hsw *hsw,
1722 struct sst_module *scratch)
1724 hsw->scratch = scratch;
1727 struct sst_dsp *sst_hsw_get_dsp(struct sst_hsw *hsw)
1732 static struct sst_dsp_device hsw_dev = {
1733 .thread = hsw_irq_thread,
1734 .ops = &haswell_ops,
1737 int sst_hsw_dsp_init(struct device *dev, struct sst_pdata *pdata)
1739 struct sst_hsw_ipc_fw_version version;
1740 struct sst_hsw *hsw;
1741 struct sst_fw *hsw_sst_fw;
1744 dev_dbg(dev, "initialising Audio DSP IPC\n");
1746 hsw = devm_kzalloc(dev, sizeof(*hsw), GFP_KERNEL);
1751 INIT_LIST_HEAD(&hsw->stream_list);
1752 INIT_LIST_HEAD(&hsw->tx_list);
1753 INIT_LIST_HEAD(&hsw->rx_list);
1754 INIT_LIST_HEAD(&hsw->empty_list);
1755 init_waitqueue_head(&hsw->boot_wait);
1756 init_waitqueue_head(&hsw->wait_txq);
1758 ret = msg_empty_list_init(hsw);
1762 /* start the IPC message thread */
1763 init_kthread_worker(&hsw->kworker);
1764 hsw->tx_thread = kthread_run(kthread_worker_fn,
1765 &hsw->kworker, "%s",
1766 dev_name(hsw->dev));
1767 if (IS_ERR(hsw->tx_thread)) {
1768 ret = PTR_ERR(hsw->tx_thread);
1769 dev_err(hsw->dev, "error: failed to create message TX task\n");
1772 init_kthread_work(&hsw->kwork, ipc_tx_msgs);
1774 hsw_dev.thread_context = hsw;
1777 hsw->dsp = sst_dsp_new(dev, &hsw_dev, pdata);
1778 if (hsw->dsp == NULL) {
1783 /* keep the DSP in reset state for base FW loading */
1784 sst_dsp_reset(hsw->dsp);
1786 hsw_sst_fw = sst_fw_new(hsw->dsp, pdata->fw, hsw);
1788 if (hsw_sst_fw == NULL) {
1790 dev_err(dev, "error: failed to load firmware\n");
1794 /* wait for DSP boot completion */
1795 sst_dsp_boot(hsw->dsp);
1796 ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
1797 msecs_to_jiffies(IPC_BOOT_MSECS));
1800 dev_err(hsw->dev, "error: ADSP boot timeout\n");
1804 /* get the FW version */
1805 sst_hsw_fw_get_version(hsw, &version);
1807 /* get the globalmixer */
1808 ret = sst_hsw_mixer_get_info(hsw);
1810 dev_err(hsw->dev, "error: failed to get stream info\n");
1818 sst_dsp_reset(hsw->dsp);
1819 sst_fw_free(hsw_sst_fw);
1821 sst_dsp_free(hsw->dsp);
1823 kthread_stop(hsw->tx_thread);
1829 EXPORT_SYMBOL_GPL(sst_hsw_dsp_init);
1831 void sst_hsw_dsp_free(struct device *dev, struct sst_pdata *pdata)
1833 struct sst_hsw *hsw = pdata->dsp;
1835 sst_dsp_reset(hsw->dsp);
1836 sst_fw_free_all(hsw->dsp);
1837 sst_dsp_free(hsw->dsp);
1838 kfree(hsw->scratch);
1839 kthread_stop(hsw->tx_thread);
1842 EXPORT_SYMBOL_GPL(sst_hsw_dsp_free);