1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel SST Haswell/Broadwell IPC Support
5 * Copyright (C) 2013, Intel Corporation. All rights reserved.
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/list.h>
11 #include <linux/device.h>
12 #include <linux/wait.h>
13 #include <linux/spinlock.h>
14 #include <linux/workqueue.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/sched.h>
19 #include <linux/platform_device.h>
20 #include <linux/firmware.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/debugfs.h>
23 #include <linux/pm_runtime.h>
24 #include <sound/asound.h>
26 #include "sst-haswell-ipc.h"
27 #include "../common/sst-dsp.h"
28 #include "../common/sst-dsp-priv.h"
29 #include "../common/sst-ipc.h"
31 /* Global Message - Generic */
32 #define IPC_GLB_TYPE_SHIFT 24
33 #define IPC_GLB_TYPE_MASK (0x1f << IPC_GLB_TYPE_SHIFT)
34 #define IPC_GLB_TYPE(x) (x << IPC_GLB_TYPE_SHIFT)
36 /* Global Message - Reply */
37 #define IPC_GLB_REPLY_SHIFT 0
38 #define IPC_GLB_REPLY_MASK (0x1f << IPC_GLB_REPLY_SHIFT)
39 #define IPC_GLB_REPLY_TYPE(x) (x << IPC_GLB_REPLY_TYPE_SHIFT)
41 /* Stream Message - Generic */
42 #define IPC_STR_TYPE_SHIFT 20
43 #define IPC_STR_TYPE_MASK (0xf << IPC_STR_TYPE_SHIFT)
44 #define IPC_STR_TYPE(x) (x << IPC_STR_TYPE_SHIFT)
45 #define IPC_STR_ID_SHIFT 16
46 #define IPC_STR_ID_MASK (0xf << IPC_STR_ID_SHIFT)
47 #define IPC_STR_ID(x) (x << IPC_STR_ID_SHIFT)
49 /* Stream Message - Reply */
50 #define IPC_STR_REPLY_SHIFT 0
51 #define IPC_STR_REPLY_MASK (0x1f << IPC_STR_REPLY_SHIFT)
53 /* Stream Stage Message - Generic */
54 #define IPC_STG_TYPE_SHIFT 12
55 #define IPC_STG_TYPE_MASK (0xf << IPC_STG_TYPE_SHIFT)
56 #define IPC_STG_TYPE(x) (x << IPC_STG_TYPE_SHIFT)
57 #define IPC_STG_ID_SHIFT 10
58 #define IPC_STG_ID_MASK (0x3 << IPC_STG_ID_SHIFT)
59 #define IPC_STG_ID(x) (x << IPC_STG_ID_SHIFT)
61 /* Stream Stage Message - Reply */
62 #define IPC_STG_REPLY_SHIFT 0
63 #define IPC_STG_REPLY_MASK (0x1f << IPC_STG_REPLY_SHIFT)
65 /* Debug Log Message - Generic */
66 #define IPC_LOG_OP_SHIFT 20
67 #define IPC_LOG_OP_MASK (0xf << IPC_LOG_OP_SHIFT)
68 #define IPC_LOG_OP_TYPE(x) (x << IPC_LOG_OP_SHIFT)
69 #define IPC_LOG_ID_SHIFT 16
70 #define IPC_LOG_ID_MASK (0xf << IPC_LOG_ID_SHIFT)
71 #define IPC_LOG_ID(x) (x << IPC_LOG_ID_SHIFT)
74 #define IPC_MODULE_OPERATION_SHIFT 20
75 #define IPC_MODULE_OPERATION_MASK (0xf << IPC_MODULE_OPERATION_SHIFT)
76 #define IPC_MODULE_OPERATION(x) (x << IPC_MODULE_OPERATION_SHIFT)
78 #define IPC_MODULE_ID_SHIFT 16
79 #define IPC_MODULE_ID_MASK (0xf << IPC_MODULE_ID_SHIFT)
80 #define IPC_MODULE_ID(x) (x << IPC_MODULE_ID_SHIFT)
82 /* IPC message timeout (msecs) */
83 #define IPC_TIMEOUT_MSECS 300
84 #define IPC_BOOT_MSECS 200
85 #define IPC_MSG_WAIT 0
86 #define IPC_MSG_NOWAIT 1
88 /* Firmware Ready Message */
89 #define IPC_FW_READY (0x1 << 29)
90 #define IPC_STATUS_MASK (0x3 << 30)
92 #define IPC_EMPTY_LIST_SIZE 8
93 #define IPC_MAX_STREAMS 4
96 #define IPC_MAX_MAILBOX_BYTES 256
98 #define INVALID_STREAM_HW_ID 0xffffffff
100 /* Global Message - Types and Replies */
102 IPC_GLB_GET_FW_VERSION = 0, /* Retrieves firmware version */
103 IPC_GLB_PERFORMANCE_MONITOR = 1, /* Performance monitoring actions */
104 IPC_GLB_ALLOCATE_STREAM = 3, /* Request to allocate new stream */
105 IPC_GLB_FREE_STREAM = 4, /* Request to free stream */
106 IPC_GLB_GET_FW_CAPABILITIES = 5, /* Retrieves firmware capabilities */
107 IPC_GLB_STREAM_MESSAGE = 6, /* Message directed to stream or its stages */
108 /* Request to store firmware context during D0->D3 transition */
109 IPC_GLB_REQUEST_DUMP = 7,
110 /* Request to restore firmware context during D3->D0 transition */
111 IPC_GLB_RESTORE_CONTEXT = 8,
112 IPC_GLB_GET_DEVICE_FORMATS = 9, /* Set device format */
113 IPC_GLB_SET_DEVICE_FORMATS = 10, /* Get device format */
114 IPC_GLB_SHORT_REPLY = 11,
115 IPC_GLB_ENTER_DX_STATE = 12,
116 IPC_GLB_GET_MIXER_STREAM_INFO = 13, /* Request mixer stream params */
117 IPC_GLB_DEBUG_LOG_MESSAGE = 14, /* Message to or from the debug logger. */
118 IPC_GLB_MODULE_OPERATION = 15, /* Message to loadable fw module */
119 IPC_GLB_REQUEST_TRANSFER = 16, /* < Request Transfer for host */
120 IPC_GLB_MAX_IPC_MESSAGE_TYPE = 17, /* Maximum message number */
124 IPC_GLB_REPLY_SUCCESS = 0, /* The operation was successful. */
125 IPC_GLB_REPLY_ERROR_INVALID_PARAM = 1, /* Invalid parameter was passed. */
126 IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE = 2, /* Uknown message type was resceived. */
127 IPC_GLB_REPLY_OUT_OF_RESOURCES = 3, /* No resources to satisfy the request. */
128 IPC_GLB_REPLY_BUSY = 4, /* The system or resource is busy. */
129 IPC_GLB_REPLY_PENDING = 5, /* The action was scheduled for processing. */
130 IPC_GLB_REPLY_FAILURE = 6, /* Critical error happened. */
131 IPC_GLB_REPLY_INVALID_REQUEST = 7, /* Request can not be completed. */
132 IPC_GLB_REPLY_STAGE_UNINITIALIZED = 8, /* Processing stage was uninitialized. */
133 IPC_GLB_REPLY_NOT_FOUND = 9, /* Required resource can not be found. */
134 IPC_GLB_REPLY_SOURCE_NOT_STARTED = 10, /* Source was not started. */
137 enum ipc_module_operation {
138 IPC_MODULE_NOTIFICATION = 0,
139 IPC_MODULE_ENABLE = 1,
140 IPC_MODULE_DISABLE = 2,
141 IPC_MODULE_GET_PARAMETER = 3,
142 IPC_MODULE_SET_PARAMETER = 4,
143 IPC_MODULE_GET_INFO = 5,
144 IPC_MODULE_MAX_MESSAGE
147 /* Stream Message - Types */
148 enum ipc_str_operation {
152 IPC_STR_STAGE_MESSAGE = 3,
153 IPC_STR_NOTIFICATION = 4,
157 /* Stream Stage Message Types */
158 enum ipc_stg_operation {
159 IPC_STG_GET_VOLUME = 0,
161 IPC_STG_SET_WRITE_POSITION,
162 IPC_STG_SET_FX_ENABLE,
163 IPC_STG_SET_FX_DISABLE,
164 IPC_STG_SET_FX_GET_PARAM,
165 IPC_STG_SET_FX_SET_PARAM,
166 IPC_STG_SET_FX_GET_INFO,
167 IPC_STG_MUTE_LOOPBACK,
171 /* Stream Stage Message Types For Notification*/
172 enum ipc_stg_operation_notify {
173 IPC_POSITION_CHANGED = 0,
178 enum ipc_glitch_type {
179 IPC_GLITCH_UNDERRUN = 1,
180 IPC_GLITCH_DECODER_ERROR,
181 IPC_GLITCH_DOUBLED_WRITE_POS,
186 enum ipc_debug_operation {
187 IPC_DEBUG_ENABLE_LOG = 0,
188 IPC_DEBUG_DISABLE_LOG = 1,
189 IPC_DEBUG_REQUEST_LOG_DUMP = 2,
190 IPC_DEBUG_NOTIFY_LOG_DUMP = 3,
191 IPC_DEBUG_MAX_DEBUG_LOG
195 struct sst_hsw_ipc_fw_ready {
201 u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
202 } __attribute__((packed));
204 struct sst_hsw_stream;
207 /* Stream infomation */
208 struct sst_hsw_stream {
210 struct sst_hsw_ipc_stream_alloc_req request;
211 struct sst_hsw_ipc_stream_alloc_reply reply;
212 struct sst_hsw_ipc_stream_free_req free_req;
215 u32 mute_volume[SST_HSW_NO_CHANNELS];
216 u32 mute[SST_HSW_NO_CHANNELS];
224 /* Notification work */
225 struct work_struct notify_work;
228 /* Position info from DSP */
229 struct sst_hsw_ipc_stream_set_position wpos;
230 struct sst_hsw_ipc_stream_get_position rpos;
231 struct sst_hsw_ipc_stream_glitch_position glitch;
234 struct sst_hsw_ipc_volume_req vol_req;
236 /* driver callback */
237 u32 (*notify_position)(struct sst_hsw_stream *stream, void *data);
240 /* record the fw read position when playback */
241 snd_pcm_uframes_t old_position;
243 struct list_head node;
246 /* FW log ring information */
247 struct sst_hsw_log_stream {
249 unsigned char *dma_area;
250 unsigned char *ring_descr;
254 /* Notification work */
255 struct work_struct notify_work;
256 wait_queue_head_t readers_wait_q;
257 struct mutex rw_mutex;
264 u32 config[SST_HSW_FW_LOG_CONFIG_DWORDS];
269 /* SST Haswell IPC data */
273 struct platform_device *pdev_pcm;
276 struct sst_hsw_ipc_fw_ready fw_ready;
277 struct sst_hsw_ipc_fw_version version;
279 struct sst_fw *sst_fw;
282 struct list_head stream_list;
285 struct sst_hsw_ipc_stream_info_reply mixer_info;
286 enum sst_hsw_volume_curve curve_type;
288 u32 mute[SST_HSW_NO_CHANNELS];
289 u32 mute_volume[SST_HSW_NO_CHANNELS];
292 struct sst_hsw_ipc_dx_reply dx;
294 dma_addr_t dx_context_paddr;
295 enum sst_hsw_device_id dx_dev;
296 enum sst_hsw_device_mclk dx_mclk;
297 enum sst_hsw_device_mode dx_mode;
298 u32 dx_clock_divider;
301 wait_queue_head_t boot_wait;
306 struct sst_generic_ipc ipc;
309 struct sst_hsw_log_stream log_stream;
311 /* flags bit field to track module state when resume from RTD3,
312 * each bit represent state (enabled/disabled) of single module */
313 u32 enabled_modules_rtd3;
315 /* buffer to store parameter lines */
316 u32 param_idx_w; /* write index */
317 u32 param_idx_r; /* read index */
318 u8 param_buf[WAVES_PARAM_LINES][WAVES_PARAM_COUNT];
321 #define CREATE_TRACE_POINTS
322 #include <trace/events/hswadsp.h>
324 static inline u32 msg_get_global_type(u32 msg)
326 return (msg & IPC_GLB_TYPE_MASK) >> IPC_GLB_TYPE_SHIFT;
329 static inline u32 msg_get_global_reply(u32 msg)
331 return (msg & IPC_GLB_REPLY_MASK) >> IPC_GLB_REPLY_SHIFT;
334 static inline u32 msg_get_stream_type(u32 msg)
336 return (msg & IPC_STR_TYPE_MASK) >> IPC_STR_TYPE_SHIFT;
339 static inline u32 msg_get_stream_id(u32 msg)
341 return (msg & IPC_STR_ID_MASK) >> IPC_STR_ID_SHIFT;
344 static inline u32 msg_get_notify_reason(u32 msg)
346 return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
349 static inline u32 msg_get_module_operation(u32 msg)
351 return (msg & IPC_MODULE_OPERATION_MASK) >> IPC_MODULE_OPERATION_SHIFT;
354 static inline u32 msg_get_module_id(u32 msg)
356 return (msg & IPC_MODULE_ID_MASK) >> IPC_MODULE_ID_SHIFT;
359 u32 create_channel_map(enum sst_hsw_channel_config config)
362 case SST_HSW_CHANNEL_CONFIG_MONO:
363 return (0xFFFFFFF0 | SST_HSW_CHANNEL_CENTER);
364 case SST_HSW_CHANNEL_CONFIG_STEREO:
365 return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
366 | (SST_HSW_CHANNEL_RIGHT << 4));
367 case SST_HSW_CHANNEL_CONFIG_2_POINT_1:
368 return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
369 | (SST_HSW_CHANNEL_RIGHT << 4)
370 | (SST_HSW_CHANNEL_LFE << 8 ));
371 case SST_HSW_CHANNEL_CONFIG_3_POINT_0:
372 return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
373 | (SST_HSW_CHANNEL_CENTER << 4)
374 | (SST_HSW_CHANNEL_RIGHT << 8));
375 case SST_HSW_CHANNEL_CONFIG_3_POINT_1:
376 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
377 | (SST_HSW_CHANNEL_CENTER << 4)
378 | (SST_HSW_CHANNEL_RIGHT << 8)
379 | (SST_HSW_CHANNEL_LFE << 12));
380 case SST_HSW_CHANNEL_CONFIG_QUATRO:
381 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
382 | (SST_HSW_CHANNEL_RIGHT << 4)
383 | (SST_HSW_CHANNEL_LEFT_SURROUND << 8)
384 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 12));
385 case SST_HSW_CHANNEL_CONFIG_4_POINT_0:
386 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
387 | (SST_HSW_CHANNEL_CENTER << 4)
388 | (SST_HSW_CHANNEL_RIGHT << 8)
389 | (SST_HSW_CHANNEL_CENTER_SURROUND << 12));
390 case SST_HSW_CHANNEL_CONFIG_5_POINT_0:
391 return (0xFFF00000 | SST_HSW_CHANNEL_LEFT
392 | (SST_HSW_CHANNEL_CENTER << 4)
393 | (SST_HSW_CHANNEL_RIGHT << 8)
394 | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
395 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16));
396 case SST_HSW_CHANNEL_CONFIG_5_POINT_1:
397 return (0xFF000000 | SST_HSW_CHANNEL_CENTER
398 | (SST_HSW_CHANNEL_LEFT << 4)
399 | (SST_HSW_CHANNEL_RIGHT << 8)
400 | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
401 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16)
402 | (SST_HSW_CHANNEL_LFE << 20));
403 case SST_HSW_CHANNEL_CONFIG_DUAL_MONO:
404 return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
405 | (SST_HSW_CHANNEL_LEFT << 4));
411 static struct sst_hsw_stream *get_stream_by_id(struct sst_hsw *hsw,
414 struct sst_hsw_stream *stream;
416 list_for_each_entry(stream, &hsw->stream_list, node) {
417 if (stream->reply.stream_hw_id == stream_id)
424 static void hsw_fw_ready(struct sst_hsw *hsw, u32 header)
426 struct sst_hsw_ipc_fw_ready fw_ready;
428 u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
429 char *tmp[5], *pinfo;
432 offset = (header & 0x1FFFFFFF) << 3;
434 dev_dbg(hsw->dev, "ipc: DSP is ready 0x%8.8x offset %d\n",
437 /* copy data from the DSP FW ready offset */
438 sst_dsp_read(hsw->dsp, &fw_ready, offset, sizeof(fw_ready));
440 sst_dsp_mailbox_init(hsw->dsp, fw_ready.inbox_offset,
441 fw_ready.inbox_size, fw_ready.outbox_offset,
442 fw_ready.outbox_size);
444 hsw->boot_complete = true;
445 wake_up(&hsw->boot_wait);
447 dev_dbg(hsw->dev, " mailbox upstream 0x%x - size 0x%x\n",
448 fw_ready.inbox_offset, fw_ready.inbox_size);
449 dev_dbg(hsw->dev, " mailbox downstream 0x%x - size 0x%x\n",
450 fw_ready.outbox_offset, fw_ready.outbox_size);
451 if (fw_ready.fw_info_size < sizeof(fw_ready.fw_info)) {
452 fw_ready.fw_info[fw_ready.fw_info_size] = 0;
453 dev_dbg(hsw->dev, " Firmware info: %s \n", fw_ready.fw_info);
455 /* log the FW version info got from the mailbox here. */
456 memcpy(fw_info, fw_ready.fw_info, fw_ready.fw_info_size);
458 for (i = 0; i < ARRAY_SIZE(tmp); i++)
459 tmp[i] = strsep(&pinfo, " ");
460 dev_info(hsw->dev, "FW loaded, mailbox readback FW info: type %s, - "
461 "version: %s.%s, build %s, source commit id: %s\n",
462 tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
466 static void hsw_notification_work(struct work_struct *work)
468 struct sst_hsw_stream *stream = container_of(work,
469 struct sst_hsw_stream, notify_work);
470 struct sst_hsw_ipc_stream_glitch_position *glitch = &stream->glitch;
471 struct sst_hsw_ipc_stream_get_position *pos = &stream->rpos;
472 struct sst_hsw *hsw = stream->hsw;
475 reason = msg_get_notify_reason(stream->header);
479 trace_ipc_notification("DSP stream under/overrun",
480 stream->reply.stream_hw_id);
481 sst_dsp_inbox_read(hsw->dsp, glitch, sizeof(*glitch));
483 dev_err(hsw->dev, "glitch %d pos 0x%x write pos 0x%x\n",
484 glitch->glitch_type, glitch->present_pos,
488 case IPC_POSITION_CHANGED:
489 trace_ipc_notification("DSP stream position changed for",
490 stream->reply.stream_hw_id);
491 sst_dsp_inbox_read(hsw->dsp, pos, sizeof(*pos));
493 if (stream->notify_position)
494 stream->notify_position(stream, stream->pdata);
498 dev_err(hsw->dev, "error: unknown notification 0x%x\n",
503 /* tell DSP that notification has been handled */
504 sst_dsp_shim_update_bits(hsw->dsp, SST_IPCD,
505 SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
507 /* unmask busy interrupt */
508 sst_dsp_shim_update_bits(hsw->dsp, SST_IMRX, SST_IMRX_BUSY, 0);
511 static void hsw_stream_update(struct sst_hsw *hsw, struct ipc_message *msg)
513 struct sst_hsw_stream *stream;
514 u32 header = msg->header & ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
515 u32 stream_id = msg_get_stream_id(header);
516 u32 stream_msg = msg_get_stream_type(header);
518 stream = get_stream_by_id(hsw, stream_id);
522 switch (stream_msg) {
523 case IPC_STR_STAGE_MESSAGE:
524 case IPC_STR_NOTIFICATION:
527 trace_ipc_notification("stream reset", stream->reply.stream_hw_id);
530 stream->running = false;
531 trace_ipc_notification("stream paused",
532 stream->reply.stream_hw_id);
535 stream->running = true;
536 trace_ipc_notification("stream running",
537 stream->reply.stream_hw_id);
542 static int hsw_process_reply(struct sst_hsw *hsw, u32 header)
544 struct ipc_message *msg;
545 u32 reply = msg_get_global_reply(header);
547 trace_ipc_reply("processing -->", header);
549 msg = sst_ipc_reply_find_msg(&hsw->ipc, header);
551 trace_ipc_error("error: can't find message header", header);
555 /* first process the header */
557 case IPC_GLB_REPLY_PENDING:
558 trace_ipc_pending_reply("received", header);
560 hsw->ipc.pending = true;
562 case IPC_GLB_REPLY_SUCCESS:
564 trace_ipc_pending_reply("completed", header);
565 sst_dsp_inbox_read(hsw->dsp, msg->rx_data,
567 hsw->ipc.pending = false;
569 /* copy data from the DSP */
570 sst_dsp_outbox_read(hsw->dsp, msg->rx_data,
574 /* these will be rare - but useful for debug */
575 case IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE:
576 trace_ipc_error("error: unknown message type", header);
577 msg->errno = -EBADMSG;
579 case IPC_GLB_REPLY_OUT_OF_RESOURCES:
580 trace_ipc_error("error: out of resources", header);
581 msg->errno = -ENOMEM;
583 case IPC_GLB_REPLY_BUSY:
584 trace_ipc_error("error: reply busy", header);
587 case IPC_GLB_REPLY_FAILURE:
588 trace_ipc_error("error: reply failure", header);
589 msg->errno = -EINVAL;
591 case IPC_GLB_REPLY_STAGE_UNINITIALIZED:
592 trace_ipc_error("error: stage uninitialized", header);
593 msg->errno = -EINVAL;
595 case IPC_GLB_REPLY_NOT_FOUND:
596 trace_ipc_error("error: reply not found", header);
597 msg->errno = -EINVAL;
599 case IPC_GLB_REPLY_SOURCE_NOT_STARTED:
600 trace_ipc_error("error: source not started", header);
601 msg->errno = -EINVAL;
603 case IPC_GLB_REPLY_INVALID_REQUEST:
604 trace_ipc_error("error: invalid request", header);
605 msg->errno = -EINVAL;
607 case IPC_GLB_REPLY_ERROR_INVALID_PARAM:
608 trace_ipc_error("error: invalid parameter", header);
609 msg->errno = -EINVAL;
612 trace_ipc_error("error: unknown reply", header);
613 msg->errno = -EINVAL;
617 /* update any stream states */
618 if (msg_get_global_type(header) == IPC_GLB_STREAM_MESSAGE)
619 hsw_stream_update(hsw, msg);
621 /* wake up and return the error if we have waiters on this message ? */
622 list_del(&msg->list);
623 sst_ipc_tx_msg_reply_complete(&hsw->ipc, msg);
628 static int hsw_module_message(struct sst_hsw *hsw, u32 header)
630 u32 operation, module_id;
633 operation = msg_get_module_operation(header);
634 module_id = msg_get_module_id(header);
635 dev_dbg(hsw->dev, "received module message header: 0x%8.8x\n",
637 dev_dbg(hsw->dev, "operation: 0x%8.8x module_id: 0x%8.8x\n",
638 operation, module_id);
641 case IPC_MODULE_NOTIFICATION:
642 dev_dbg(hsw->dev, "module notification received");
646 handled = hsw_process_reply(hsw, header);
653 static int hsw_stream_message(struct sst_hsw *hsw, u32 header)
655 u32 stream_msg, stream_id;
656 struct sst_hsw_stream *stream;
659 stream_msg = msg_get_stream_type(header);
660 stream_id = msg_get_stream_id(header);
662 stream = get_stream_by_id(hsw, stream_id);
666 stream->header = header;
668 switch (stream_msg) {
669 case IPC_STR_STAGE_MESSAGE:
670 dev_err(hsw->dev, "error: stage msg not implemented 0x%8.8x\n",
673 case IPC_STR_NOTIFICATION:
674 schedule_work(&stream->notify_work);
677 /* handle pending message complete request */
678 handled = hsw_process_reply(hsw, header);
685 static int hsw_log_message(struct sst_hsw *hsw, u32 header)
687 u32 operation = (header & IPC_LOG_OP_MASK) >> IPC_LOG_OP_SHIFT;
688 struct sst_hsw_log_stream *stream = &hsw->log_stream;
691 if (operation != IPC_DEBUG_REQUEST_LOG_DUMP) {
693 "error: log msg not implemented 0x%8.8x\n", header);
697 mutex_lock(&stream->rw_mutex);
698 stream->last_pos = stream->curr_pos;
700 hsw->dsp, &stream->curr_pos, sizeof(stream->curr_pos));
701 mutex_unlock(&stream->rw_mutex);
703 schedule_work(&stream->notify_work);
708 static int hsw_process_notification(struct sst_hsw *hsw)
710 struct sst_dsp *sst = hsw->dsp;
714 header = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
715 type = msg_get_global_type(header);
717 trace_ipc_request("processing -->", header);
719 /* FW Ready is a special case */
720 if (!hsw->boot_complete && header & IPC_FW_READY) {
721 hsw_fw_ready(hsw, header);
726 case IPC_GLB_GET_FW_VERSION:
727 case IPC_GLB_ALLOCATE_STREAM:
728 case IPC_GLB_FREE_STREAM:
729 case IPC_GLB_GET_FW_CAPABILITIES:
730 case IPC_GLB_REQUEST_DUMP:
731 case IPC_GLB_GET_DEVICE_FORMATS:
732 case IPC_GLB_SET_DEVICE_FORMATS:
733 case IPC_GLB_ENTER_DX_STATE:
734 case IPC_GLB_GET_MIXER_STREAM_INFO:
735 case IPC_GLB_MAX_IPC_MESSAGE_TYPE:
736 case IPC_GLB_RESTORE_CONTEXT:
737 case IPC_GLB_SHORT_REPLY:
738 dev_err(hsw->dev, "error: message type %d header 0x%x\n",
741 case IPC_GLB_STREAM_MESSAGE:
742 handled = hsw_stream_message(hsw, header);
744 case IPC_GLB_DEBUG_LOG_MESSAGE:
745 handled = hsw_log_message(hsw, header);
747 case IPC_GLB_MODULE_OPERATION:
748 handled = hsw_module_message(hsw, header);
751 dev_err(hsw->dev, "error: unexpected type %d hdr 0x%8.8x\n",
759 static irqreturn_t hsw_irq_thread(int irq, void *context)
761 struct sst_dsp *sst = (struct sst_dsp *) context;
762 struct sst_hsw *hsw = sst_dsp_get_thread_context(sst);
763 struct sst_generic_ipc *ipc = &hsw->ipc;
767 spin_lock_irqsave(&sst->spinlock, flags);
769 ipcx = sst_dsp_ipc_msg_rx(hsw->dsp);
770 ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
772 /* reply message from DSP */
773 if (ipcx & SST_IPCX_DONE) {
775 /* Handle Immediate reply from DSP Core */
776 hsw_process_reply(hsw, ipcx);
778 /* clear DONE bit - tell DSP we have completed */
779 sst_dsp_shim_update_bits_unlocked(sst, SST_IPCX,
782 /* unmask Done interrupt */
783 sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
787 /* new message from DSP */
788 if (ipcd & SST_IPCD_BUSY) {
790 /* Handle Notification and Delayed reply from DSP Core */
791 hsw_process_notification(hsw);
793 /* clear BUSY bit and set DONE bit - accept new messages */
794 sst_dsp_shim_update_bits_unlocked(sst, SST_IPCD,
795 SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
797 /* unmask busy interrupt */
798 sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
802 spin_unlock_irqrestore(&sst->spinlock, flags);
804 /* continue to send any remaining messages... */
805 schedule_work(&ipc->kwork);
810 int sst_hsw_fw_get_version(struct sst_hsw *hsw,
811 struct sst_hsw_ipc_fw_version *version)
815 ret = sst_ipc_tx_message_wait(&hsw->ipc,
816 IPC_GLB_TYPE(IPC_GLB_GET_FW_VERSION),
817 NULL, 0, version, sizeof(*version));
819 dev_err(hsw->dev, "error: get version failed\n");
825 int sst_hsw_stream_get_volume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
826 u32 stage_id, u32 channel, u32 *volume)
831 sst_dsp_read(hsw->dsp, volume,
832 stream->reply.volume_register_address[channel],
839 int sst_hsw_stream_set_volume(struct sst_hsw *hsw,
840 struct sst_hsw_stream *stream, u32 stage_id, u32 channel, u32 volume)
842 struct sst_hsw_ipc_volume_req *req;
846 trace_ipc_request("set stream volume", stream->reply.stream_hw_id);
848 if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
851 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
852 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
853 header |= (stream->reply.stream_hw_id << IPC_STR_ID_SHIFT);
854 header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
855 header |= (stage_id << IPC_STG_ID_SHIFT);
857 req = &stream->vol_req;
858 req->target_volume = volume;
860 /* set both at same time ? */
861 if (channel == SST_HSW_CHANNELS_ALL) {
862 if (hsw->mute[0] && hsw->mute[1]) {
863 hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
865 } else if (hsw->mute[0])
867 else if (hsw->mute[1])
870 req->channel = SST_HSW_CHANNELS_ALL;
872 /* set only 1 channel */
873 if (hsw->mute[channel]) {
874 hsw->mute_volume[channel] = volume;
877 req->channel = channel;
880 ret = sst_ipc_tx_message_wait(&hsw->ipc, header, req,
881 sizeof(*req), NULL, 0);
883 dev_err(hsw->dev, "error: set stream volume failed\n");
890 int sst_hsw_mixer_get_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
896 sst_dsp_read(hsw->dsp, volume,
897 hsw->mixer_info.volume_register_address[channel],
903 /* global mixer volume */
904 int sst_hsw_mixer_set_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
907 struct sst_hsw_ipc_volume_req req;
911 trace_ipc_request("set mixer volume", volume);
913 if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
916 /* set both at same time ? */
917 if (channel == SST_HSW_CHANNELS_ALL) {
918 if (hsw->mute[0] && hsw->mute[1]) {
919 hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
921 } else if (hsw->mute[0])
923 else if (hsw->mute[1])
926 req.channel = SST_HSW_CHANNELS_ALL;
928 /* set only 1 channel */
929 if (hsw->mute[channel]) {
930 hsw->mute_volume[channel] = volume;
933 req.channel = channel;
936 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
937 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
938 header |= (hsw->mixer_info.mixer_hw_id << IPC_STR_ID_SHIFT);
939 header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
940 header |= (stage_id << IPC_STG_ID_SHIFT);
942 req.curve_duration = hsw->curve_duration;
943 req.curve_type = hsw->curve_type;
944 req.target_volume = volume;
946 ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &req,
947 sizeof(req), NULL, 0);
949 dev_err(hsw->dev, "error: set mixer volume failed\n");
957 struct sst_hsw_stream *sst_hsw_stream_new(struct sst_hsw *hsw, int id,
958 u32 (*notify_position)(struct sst_hsw_stream *stream, void *data),
961 struct sst_hsw_stream *stream;
962 struct sst_dsp *sst = hsw->dsp;
965 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
969 spin_lock_irqsave(&sst->spinlock, flags);
970 stream->reply.stream_hw_id = INVALID_STREAM_HW_ID;
971 list_add(&stream->node, &hsw->stream_list);
972 stream->notify_position = notify_position;
973 stream->pdata = data;
975 stream->host_id = id;
977 /* work to process notification messages */
978 INIT_WORK(&stream->notify_work, hsw_notification_work);
979 spin_unlock_irqrestore(&sst->spinlock, flags);
984 int sst_hsw_stream_free(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
988 struct sst_dsp *sst = hsw->dsp;
992 dev_warn(hsw->dev, "warning: stream is NULL, no stream to free, ignore it.\n");
996 /* dont free DSP streams that are not commited */
997 if (!stream->commited)
1000 trace_ipc_request("stream free", stream->host_id);
1002 stream->free_req.stream_id = stream->reply.stream_hw_id;
1003 header = IPC_GLB_TYPE(IPC_GLB_FREE_STREAM);
1005 ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &stream->free_req,
1006 sizeof(stream->free_req), NULL, 0);
1008 dev_err(hsw->dev, "error: free stream %d failed\n",
1009 stream->free_req.stream_id);
1013 trace_hsw_stream_free_req(stream, &stream->free_req);
1016 cancel_work_sync(&stream->notify_work);
1017 spin_lock_irqsave(&sst->spinlock, flags);
1018 list_del(&stream->node);
1020 spin_unlock_irqrestore(&sst->spinlock, flags);
1025 int sst_hsw_stream_set_bits(struct sst_hsw *hsw,
1026 struct sst_hsw_stream *stream, enum sst_hsw_bitdepth bits)
1028 if (stream->commited) {
1029 dev_err(hsw->dev, "error: stream committed for set bits\n");
1033 stream->request.format.bitdepth = bits;
1037 int sst_hsw_stream_set_channels(struct sst_hsw *hsw,
1038 struct sst_hsw_stream *stream, int channels)
1040 if (stream->commited) {
1041 dev_err(hsw->dev, "error: stream committed for set channels\n");
1045 stream->request.format.ch_num = channels;
1049 int sst_hsw_stream_set_rate(struct sst_hsw *hsw,
1050 struct sst_hsw_stream *stream, int rate)
1052 if (stream->commited) {
1053 dev_err(hsw->dev, "error: stream committed for set rate\n");
1057 stream->request.format.frequency = rate;
1061 int sst_hsw_stream_set_map_config(struct sst_hsw *hsw,
1062 struct sst_hsw_stream *stream, u32 map,
1063 enum sst_hsw_channel_config config)
1065 if (stream->commited) {
1066 dev_err(hsw->dev, "error: stream committed for set map\n");
1070 stream->request.format.map = map;
1071 stream->request.format.config = config;
1075 int sst_hsw_stream_set_style(struct sst_hsw *hsw,
1076 struct sst_hsw_stream *stream, enum sst_hsw_interleaving style)
1078 if (stream->commited) {
1079 dev_err(hsw->dev, "error: stream committed for set style\n");
1083 stream->request.format.style = style;
1087 int sst_hsw_stream_set_valid(struct sst_hsw *hsw,
1088 struct sst_hsw_stream *stream, u32 bits)
1090 if (stream->commited) {
1091 dev_err(hsw->dev, "error: stream committed for set valid bits\n");
1095 stream->request.format.valid_bit = bits;
1099 /* Stream Configuration */
1100 int sst_hsw_stream_format(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1101 enum sst_hsw_stream_path_id path_id,
1102 enum sst_hsw_stream_type stream_type,
1103 enum sst_hsw_stream_format format_id)
1105 if (stream->commited) {
1106 dev_err(hsw->dev, "error: stream committed for set format\n");
1110 stream->request.path_id = path_id;
1111 stream->request.stream_type = stream_type;
1112 stream->request.format_id = format_id;
1114 trace_hsw_stream_alloc_request(stream, &stream->request);
1119 int sst_hsw_stream_buffer(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1120 u32 ring_pt_address, u32 num_pages,
1121 u32 ring_size, u32 ring_offset, u32 ring_first_pfn)
1123 if (stream->commited) {
1124 dev_err(hsw->dev, "error: stream committed for buffer\n");
1128 stream->request.ringinfo.ring_pt_address = ring_pt_address;
1129 stream->request.ringinfo.num_pages = num_pages;
1130 stream->request.ringinfo.ring_size = ring_size;
1131 stream->request.ringinfo.ring_offset = ring_offset;
1132 stream->request.ringinfo.ring_first_pfn = ring_first_pfn;
1134 trace_hsw_stream_buffer(stream);
1139 int sst_hsw_stream_set_module_info(struct sst_hsw *hsw,
1140 struct sst_hsw_stream *stream, struct sst_module_runtime *runtime)
1142 struct sst_hsw_module_map *map = &stream->request.map;
1143 struct sst_dsp *dsp = sst_hsw_get_dsp(hsw);
1144 struct sst_module *module = runtime->module;
1146 if (stream->commited) {
1147 dev_err(hsw->dev, "error: stream committed for set module\n");
1151 /* only support initial module atm */
1152 map->module_entries_count = 1;
1153 map->module_entries[0].module_id = module->id;
1154 map->module_entries[0].entry_point = module->entry;
1156 stream->request.persistent_mem.offset =
1157 sst_dsp_get_offset(dsp, runtime->persistent_offset, SST_MEM_DRAM);
1158 stream->request.persistent_mem.size = module->persistent_size;
1160 stream->request.scratch_mem.offset =
1161 sst_dsp_get_offset(dsp, dsp->scratch_offset, SST_MEM_DRAM);
1162 stream->request.scratch_mem.size = dsp->scratch_size;
1164 dev_dbg(hsw->dev, "module %d runtime %d using:\n", module->id,
1166 dev_dbg(hsw->dev, " persistent offset 0x%x bytes 0x%x\n",
1167 stream->request.persistent_mem.offset,
1168 stream->request.persistent_mem.size);
1169 dev_dbg(hsw->dev, " scratch offset 0x%x bytes 0x%x\n",
1170 stream->request.scratch_mem.offset,
1171 stream->request.scratch_mem.size);
1176 int sst_hsw_stream_commit(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1178 struct sst_hsw_ipc_stream_alloc_req *str_req = &stream->request;
1179 struct sst_hsw_ipc_stream_alloc_reply *reply = &stream->reply;
1184 dev_warn(hsw->dev, "warning: stream is NULL, no stream to commit, ignore it.\n");
1188 if (stream->commited) {
1189 dev_warn(hsw->dev, "warning: stream is already committed, ignore it.\n");
1193 trace_ipc_request("stream alloc", stream->host_id);
1195 header = IPC_GLB_TYPE(IPC_GLB_ALLOCATE_STREAM);
1197 ret = sst_ipc_tx_message_wait(&hsw->ipc, header, str_req,
1198 sizeof(*str_req), reply, sizeof(*reply));
1200 dev_err(hsw->dev, "error: stream commit failed\n");
1204 stream->commited = true;
1205 trace_hsw_stream_alloc_reply(stream);
1210 snd_pcm_uframes_t sst_hsw_stream_get_old_position(struct sst_hsw *hsw,
1211 struct sst_hsw_stream *stream)
1213 return stream->old_position;
1216 void sst_hsw_stream_set_old_position(struct sst_hsw *hsw,
1217 struct sst_hsw_stream *stream, snd_pcm_uframes_t val)
1219 stream->old_position = val;
1222 bool sst_hsw_stream_get_silence_start(struct sst_hsw *hsw,
1223 struct sst_hsw_stream *stream)
1225 return stream->play_silence;
1228 void sst_hsw_stream_set_silence_start(struct sst_hsw *hsw,
1229 struct sst_hsw_stream *stream, bool val)
1231 stream->play_silence = val;
1234 /* Stream Information - these calls could be inline but we want the IPC
1235 ABI to be opaque to client PCM drivers to cope with any future ABI changes */
1236 int sst_hsw_mixer_get_info(struct sst_hsw *hsw)
1238 struct sst_hsw_ipc_stream_info_reply *reply;
1242 reply = &hsw->mixer_info;
1243 header = IPC_GLB_TYPE(IPC_GLB_GET_MIXER_STREAM_INFO);
1245 trace_ipc_request("get global mixer info", 0);
1247 ret = sst_ipc_tx_message_wait(&hsw->ipc, header, NULL, 0,
1248 reply, sizeof(*reply));
1250 dev_err(hsw->dev, "error: get stream info failed\n");
1254 trace_hsw_mixer_info_reply(reply);
1259 /* Send stream command */
1260 static int sst_hsw_stream_operations(struct sst_hsw *hsw, int type,
1261 int stream_id, int wait)
1265 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) | IPC_STR_TYPE(type);
1266 header |= (stream_id << IPC_STR_ID_SHIFT);
1269 return sst_ipc_tx_message_wait(&hsw->ipc, header,
1272 return sst_ipc_tx_message_nowait(&hsw->ipc, header, NULL, 0);
1275 /* Stream ALSA trigger operations */
1276 int sst_hsw_stream_pause(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1282 dev_warn(hsw->dev, "warning: stream is NULL, no stream to pause, ignore it.\n");
1286 trace_ipc_request("stream pause", stream->reply.stream_hw_id);
1288 ret = sst_hsw_stream_operations(hsw, IPC_STR_PAUSE,
1289 stream->reply.stream_hw_id, wait);
1291 dev_err(hsw->dev, "error: failed to pause stream %d\n",
1292 stream->reply.stream_hw_id);
1297 int sst_hsw_stream_resume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1303 dev_warn(hsw->dev, "warning: stream is NULL, no stream to resume, ignore it.\n");
1307 trace_ipc_request("stream resume", stream->reply.stream_hw_id);
1309 ret = sst_hsw_stream_operations(hsw, IPC_STR_RESUME,
1310 stream->reply.stream_hw_id, wait);
1312 dev_err(hsw->dev, "error: failed to resume stream %d\n",
1313 stream->reply.stream_hw_id);
1318 int sst_hsw_stream_reset(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1320 int ret, tries = 10;
1323 dev_warn(hsw->dev, "warning: stream is NULL, no stream to reset, ignore it.\n");
1327 /* dont reset streams that are not commited */
1328 if (!stream->commited)
1331 /* wait for pause to complete before we reset the stream */
1332 while (stream->running && --tries)
1335 dev_err(hsw->dev, "error: reset stream %d still running\n",
1336 stream->reply.stream_hw_id);
1340 trace_ipc_request("stream reset", stream->reply.stream_hw_id);
1342 ret = sst_hsw_stream_operations(hsw, IPC_STR_RESET,
1343 stream->reply.stream_hw_id, 1);
1345 dev_err(hsw->dev, "error: failed to reset stream %d\n",
1346 stream->reply.stream_hw_id);
1350 /* Stream pointer positions */
1351 u32 sst_hsw_get_dsp_position(struct sst_hsw *hsw,
1352 struct sst_hsw_stream *stream)
1356 sst_dsp_read(hsw->dsp, &rpos,
1357 stream->reply.read_position_register_address, sizeof(rpos));
1362 /* Stream presentation (monotonic) positions */
1363 u64 sst_hsw_get_dsp_presentation_position(struct sst_hsw *hsw,
1364 struct sst_hsw_stream *stream)
1368 sst_dsp_read(hsw->dsp, &ppos,
1369 stream->reply.presentation_position_register_address,
1375 /* physical BE config */
1376 int sst_hsw_device_set_config(struct sst_hsw *hsw,
1377 enum sst_hsw_device_id dev, enum sst_hsw_device_mclk mclk,
1378 enum sst_hsw_device_mode mode, u32 clock_divider)
1380 struct sst_hsw_ipc_device_config_req config;
1384 trace_ipc_request("set device config", dev);
1386 hsw->dx_dev = config.ssp_interface = dev;
1387 hsw->dx_mclk = config.clock_frequency = mclk;
1388 hsw->dx_mode = config.mode = mode;
1389 hsw->dx_clock_divider = config.clock_divider = clock_divider;
1390 if (mode == SST_HSW_DEVICE_TDM_CLOCK_MASTER)
1391 config.channels = 4;
1393 config.channels = 2;
1395 trace_hsw_device_config_req(&config);
1397 header = IPC_GLB_TYPE(IPC_GLB_SET_DEVICE_FORMATS);
1399 ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &config,
1400 sizeof(config), NULL, 0);
1402 dev_err(hsw->dev, "error: set device formats failed\n");
1406 EXPORT_SYMBOL_GPL(sst_hsw_device_set_config);
1409 int sst_hsw_dx_set_state(struct sst_hsw *hsw,
1410 enum sst_hsw_dx_state state, struct sst_hsw_ipc_dx_reply *dx)
1415 header = IPC_GLB_TYPE(IPC_GLB_ENTER_DX_STATE);
1418 trace_ipc_request("PM enter Dx state", state);
1420 ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &state_,
1421 sizeof(state_), dx, sizeof(*dx));
1423 dev_err(hsw->dev, "ipc: error set dx state %d failed\n", state);
1427 for (item = 0; item < dx->entries_no; item++) {
1429 "Item[%d] offset[%x] - size[%x] - source[%x]\n",
1430 item, dx->mem_info[item].offset,
1431 dx->mem_info[item].size,
1432 dx->mem_info[item].source);
1434 dev_dbg(hsw->dev, "ipc: got %d entry numbers for state %d\n",
1435 dx->entries_no, state);
1440 struct sst_module_runtime *sst_hsw_runtime_module_create(struct sst_hsw *hsw,
1441 int mod_id, int offset)
1443 struct sst_dsp *dsp = hsw->dsp;
1444 struct sst_module *module;
1445 struct sst_module_runtime *runtime;
1448 module = sst_module_get_from_id(dsp, mod_id);
1449 if (module == NULL) {
1450 dev_err(dsp->dev, "error: failed to get module %d for pcm\n",
1455 runtime = sst_module_runtime_new(module, mod_id, NULL);
1456 if (runtime == NULL) {
1457 dev_err(dsp->dev, "error: failed to create module %d runtime\n",
1462 err = sst_module_runtime_alloc_blocks(runtime, offset);
1464 dev_err(dsp->dev, "error: failed to alloc blocks for module %d runtime\n",
1466 sst_module_runtime_free(runtime);
1470 dev_dbg(dsp->dev, "runtime id %d created for module %d\n", runtime->id,
1475 void sst_hsw_runtime_module_free(struct sst_module_runtime *runtime)
1477 sst_module_runtime_free_blocks(runtime);
1478 sst_module_runtime_free(runtime);
1482 static int sst_hsw_dx_state_dump(struct sst_hsw *hsw)
1484 struct sst_dsp *sst = hsw->dsp;
1485 u32 item, offset, size;
1488 trace_ipc_request("PM state dump. Items #", SST_HSW_MAX_DX_REGIONS);
1490 if (hsw->dx.entries_no > SST_HSW_MAX_DX_REGIONS) {
1492 "error: number of FW context regions greater than %d\n",
1493 SST_HSW_MAX_DX_REGIONS);
1494 memset(&hsw->dx, 0, sizeof(hsw->dx));
1498 ret = sst_dsp_dma_get_channel(sst, 0);
1500 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1504 /* set on-demond mode on engine 0 channel 3 */
1505 sst_dsp_shim_update_bits(sst, SST_HMDC,
1506 SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH,
1507 SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH);
1509 for (item = 0; item < hsw->dx.entries_no; item++) {
1510 if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
1511 && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
1512 && hsw->dx.mem_info[item].offset <
1513 DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
1515 offset = hsw->dx.mem_info[item].offset
1516 - DSP_DRAM_ADDR_OFFSET;
1517 size = (hsw->dx.mem_info[item].size + 3) & (~3);
1519 ret = sst_dsp_dma_copyfrom(sst, hsw->dx_context_paddr + offset,
1520 sst->addr.lpe_base + offset, size);
1523 "error: FW context dump failed\n");
1524 memset(&hsw->dx, 0, sizeof(hsw->dx));
1531 sst_dsp_dma_put_channel(sst);
1535 static int sst_hsw_dx_state_restore(struct sst_hsw *hsw)
1537 struct sst_dsp *sst = hsw->dsp;
1538 u32 item, offset, size;
1541 for (item = 0; item < hsw->dx.entries_no; item++) {
1542 if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
1543 && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
1544 && hsw->dx.mem_info[item].offset <
1545 DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
1547 offset = hsw->dx.mem_info[item].offset
1548 - DSP_DRAM_ADDR_OFFSET;
1549 size = (hsw->dx.mem_info[item].size + 3) & (~3);
1551 ret = sst_dsp_dma_copyto(sst, sst->addr.lpe_base + offset,
1552 hsw->dx_context_paddr + offset, size);
1555 "error: FW context restore failed\n");
1564 int sst_hsw_dsp_load(struct sst_hsw *hsw)
1566 struct sst_dsp *dsp = hsw->dsp;
1567 struct sst_fw *sst_fw, *t;
1570 dev_dbg(hsw->dev, "loading audio DSP....");
1572 ret = sst_dsp_wake(dsp);
1574 dev_err(hsw->dev, "error: failed to wake audio DSP\n");
1578 ret = sst_dsp_dma_get_channel(dsp, 0);
1580 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1584 list_for_each_entry_safe_reverse(sst_fw, t, &dsp->fw_list, list) {
1585 ret = sst_fw_reload(sst_fw);
1587 dev_err(hsw->dev, "error: SST FW reload failed\n");
1588 sst_dsp_dma_put_channel(dsp);
1592 ret = sst_block_alloc_scratch(hsw->dsp);
1596 sst_dsp_dma_put_channel(dsp);
1600 static int sst_hsw_dsp_restore(struct sst_hsw *hsw)
1602 struct sst_dsp *dsp = hsw->dsp;
1605 dev_dbg(hsw->dev, "restoring audio DSP....");
1607 ret = sst_dsp_dma_get_channel(dsp, 0);
1609 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1613 ret = sst_hsw_dx_state_restore(hsw);
1615 dev_err(hsw->dev, "error: SST FW context restore failed\n");
1616 sst_dsp_dma_put_channel(dsp);
1619 sst_dsp_dma_put_channel(dsp);
1621 /* wait for DSP boot completion */
1627 int sst_hsw_dsp_runtime_suspend(struct sst_hsw *hsw)
1631 dev_dbg(hsw->dev, "audio dsp runtime suspend\n");
1633 ret = sst_hsw_dx_set_state(hsw, SST_HSW_DX_STATE_D3, &hsw->dx);
1637 sst_dsp_stall(hsw->dsp);
1639 ret = sst_hsw_dx_state_dump(hsw);
1643 sst_ipc_drop_all(&hsw->ipc);
1648 int sst_hsw_dsp_runtime_sleep(struct sst_hsw *hsw)
1650 struct sst_fw *sst_fw, *t;
1651 struct sst_dsp *dsp = hsw->dsp;
1653 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
1654 sst_fw_unload(sst_fw);
1656 sst_block_free_scratch(dsp);
1658 hsw->boot_complete = false;
1665 int sst_hsw_dsp_runtime_resume(struct sst_hsw *hsw)
1667 struct device *dev = hsw->dev;
1670 dev_dbg(dev, "audio dsp runtime resume\n");
1672 if (hsw->boot_complete)
1673 return 1; /* tell caller no action is required */
1675 ret = sst_hsw_dsp_restore(hsw);
1677 dev_err(dev, "error: audio DSP boot failure\n");
1679 sst_hsw_init_module_state(hsw);
1681 ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
1682 msecs_to_jiffies(IPC_BOOT_MSECS));
1684 dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
1685 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
1686 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
1690 /* Set ADSP SSP port settings - sadly the FW does not store SSP port
1691 settings as part of the PM context. */
1692 ret = sst_hsw_device_set_config(hsw, hsw->dx_dev, hsw->dx_mclk,
1693 hsw->dx_mode, hsw->dx_clock_divider);
1695 dev_err(dev, "error: SSP re-initialization failed\n");
1701 struct sst_dsp *sst_hsw_get_dsp(struct sst_hsw *hsw)
1706 void sst_hsw_init_module_state(struct sst_hsw *hsw)
1708 struct sst_module *module;
1709 enum sst_hsw_module_id id;
1711 /* the base fw contains several modules */
1712 for (id = SST_HSW_MODULE_BASE_FW; id < SST_HSW_MAX_MODULE_ID; id++) {
1713 module = sst_module_get_from_id(hsw->dsp, id);
1715 /* module waves is active only after being enabled */
1716 if (id == SST_HSW_MODULE_WAVES)
1717 module->state = SST_MODULE_STATE_INITIALIZED;
1719 module->state = SST_MODULE_STATE_ACTIVE;
1724 bool sst_hsw_is_module_loaded(struct sst_hsw *hsw, u32 module_id)
1726 struct sst_module *module;
1728 module = sst_module_get_from_id(hsw->dsp, module_id);
1729 if (module == NULL || module->state == SST_MODULE_STATE_UNLOADED)
1735 bool sst_hsw_is_module_active(struct sst_hsw *hsw, u32 module_id)
1737 struct sst_module *module;
1739 module = sst_module_get_from_id(hsw->dsp, module_id);
1740 if (module != NULL && module->state == SST_MODULE_STATE_ACTIVE)
1746 void sst_hsw_set_module_enabled_rtd3(struct sst_hsw *hsw, u32 module_id)
1748 hsw->enabled_modules_rtd3 |= (1 << module_id);
1751 void sst_hsw_set_module_disabled_rtd3(struct sst_hsw *hsw, u32 module_id)
1753 hsw->enabled_modules_rtd3 &= ~(1 << module_id);
1756 bool sst_hsw_is_module_enabled_rtd3(struct sst_hsw *hsw, u32 module_id)
1758 return hsw->enabled_modules_rtd3 & (1 << module_id);
1761 void sst_hsw_reset_param_buf(struct sst_hsw *hsw)
1763 hsw->param_idx_w = 0;
1764 hsw->param_idx_r = 0;
1765 memset((void *)hsw->param_buf, 0, sizeof(hsw->param_buf));
1768 int sst_hsw_store_param_line(struct sst_hsw *hsw, u8 *buf)
1770 /* save line to the first available position of param buffer */
1771 if (hsw->param_idx_w > WAVES_PARAM_LINES - 1) {
1772 dev_warn(hsw->dev, "warning: param buffer overflow!\n");
1775 memcpy(hsw->param_buf[hsw->param_idx_w], buf, WAVES_PARAM_COUNT);
1780 int sst_hsw_load_param_line(struct sst_hsw *hsw, u8 *buf)
1784 /* read the first matching line from param buffer */
1785 while (hsw->param_idx_r < WAVES_PARAM_LINES) {
1786 id = hsw->param_buf[hsw->param_idx_r][0];
1789 memcpy(buf, hsw->param_buf[hsw->param_idx_r],
1794 if (hsw->param_idx_r > WAVES_PARAM_LINES - 1) {
1795 dev_dbg(hsw->dev, "end of buffer, roll to the beginning\n");
1796 hsw->param_idx_r = 0;
1802 int sst_hsw_launch_param_buf(struct sst_hsw *hsw)
1806 if (!sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
1807 dev_dbg(hsw->dev, "module waves is not active\n");
1811 /* put all param lines to DSP through ipc */
1812 for (idx = 0; idx < hsw->param_idx_w; idx++) {
1813 ret = sst_hsw_module_set_param(hsw,
1814 SST_HSW_MODULE_WAVES, 0, hsw->param_buf[idx][0],
1815 WAVES_PARAM_COUNT, hsw->param_buf[idx]);
1822 int sst_hsw_module_load(struct sst_hsw *hsw,
1823 u32 module_id, u32 instance_id, char *name)
1826 const struct firmware *fw = NULL;
1827 struct sst_fw *hsw_sst_fw;
1828 struct sst_module *module;
1829 struct device *dev = hsw->dev;
1830 struct sst_dsp *dsp = hsw->dsp;
1832 dev_dbg(dev, "sst_hsw_module_load id=%d, name='%s'", module_id, name);
1834 module = sst_module_get_from_id(dsp, module_id);
1835 if (module == NULL) {
1836 /* loading for the first time */
1837 if (module_id == SST_HSW_MODULE_BASE_FW) {
1838 /* for base module: use fw requested in acpi probe */
1839 fw = dsp->pdata->fw;
1841 dev_err(dev, "request Base fw failed\n");
1845 /* try and load any other optional modules if they are
1846 * available. Use dev_info instead of dev_err in case
1847 * request firmware failed */
1848 ret = reject_firmware(&fw, name, dev);
1850 dev_info(dev, "fw image %s not available(%d)\n",
1855 hsw_sst_fw = sst_fw_new(dsp, fw, hsw);
1856 if (hsw_sst_fw == NULL) {
1857 dev_err(dev, "error: failed to load firmware\n");
1861 module = sst_module_get_from_id(dsp, module_id);
1862 if (module == NULL) {
1863 dev_err(dev, "error: no module %d in firmware %s\n",
1867 dev_info(dev, "module %d (%s) already loaded\n",
1870 /* release fw, but base fw should be released by acpi driver */
1871 if (fw && module_id != SST_HSW_MODULE_BASE_FW)
1872 release_firmware(fw);
1877 int sst_hsw_module_enable(struct sst_hsw *hsw,
1878 u32 module_id, u32 instance_id)
1882 struct sst_hsw_ipc_module_config config;
1883 struct sst_module *module;
1884 struct sst_module_runtime *runtime;
1885 struct device *dev = hsw->dev;
1886 struct sst_dsp *dsp = hsw->dsp;
1888 if (!sst_hsw_is_module_loaded(hsw, module_id)) {
1889 dev_dbg(dev, "module %d not loaded\n", module_id);
1893 if (sst_hsw_is_module_active(hsw, module_id)) {
1894 dev_info(dev, "module %d already enabled\n", module_id);
1898 module = sst_module_get_from_id(dsp, module_id);
1899 if (module == NULL) {
1900 dev_err(dev, "module %d not valid\n", module_id);
1904 runtime = sst_module_runtime_get_from_id(module, module_id);
1905 if (runtime == NULL) {
1906 dev_err(dev, "runtime %d not valid", module_id);
1910 header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
1911 IPC_MODULE_OPERATION(IPC_MODULE_ENABLE) |
1912 IPC_MODULE_ID(module_id);
1913 dev_dbg(dev, "module enable header: %x\n", header);
1915 config.map.module_entries_count = 1;
1916 config.map.module_entries[0].module_id = module->id;
1917 config.map.module_entries[0].entry_point = module->entry;
1919 config.persistent_mem.offset =
1920 sst_dsp_get_offset(dsp,
1921 runtime->persistent_offset, SST_MEM_DRAM);
1922 config.persistent_mem.size = module->persistent_size;
1924 config.scratch_mem.offset =
1925 sst_dsp_get_offset(dsp,
1926 dsp->scratch_offset, SST_MEM_DRAM);
1927 config.scratch_mem.size = module->scratch_size;
1928 dev_dbg(dev, "mod %d enable p:%d @ %x, s:%d @ %x, ep: %x",
1929 config.map.module_entries[0].module_id,
1930 config.persistent_mem.size,
1931 config.persistent_mem.offset,
1932 config.scratch_mem.size, config.scratch_mem.offset,
1933 config.map.module_entries[0].entry_point);
1935 ret = sst_ipc_tx_message_wait(&hsw->ipc, header,
1936 &config, sizeof(config), NULL, 0);
1938 dev_err(dev, "ipc: module enable failed - %d\n", ret);
1940 module->state = SST_MODULE_STATE_ACTIVE;
1945 int sst_hsw_module_disable(struct sst_hsw *hsw,
1946 u32 module_id, u32 instance_id)
1950 struct sst_module *module;
1951 struct device *dev = hsw->dev;
1952 struct sst_dsp *dsp = hsw->dsp;
1954 if (!sst_hsw_is_module_loaded(hsw, module_id)) {
1955 dev_dbg(dev, "module %d not loaded\n", module_id);
1959 if (!sst_hsw_is_module_active(hsw, module_id)) {
1960 dev_info(dev, "module %d already disabled\n", module_id);
1964 module = sst_module_get_from_id(dsp, module_id);
1965 if (module == NULL) {
1966 dev_err(dev, "module %d not valid\n", module_id);
1970 header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
1971 IPC_MODULE_OPERATION(IPC_MODULE_DISABLE) |
1972 IPC_MODULE_ID(module_id);
1974 ret = sst_ipc_tx_message_wait(&hsw->ipc, header, NULL, 0, NULL, 0);
1976 dev_err(dev, "module disable failed - %d\n", ret);
1978 module->state = SST_MODULE_STATE_INITIALIZED;
1983 int sst_hsw_module_set_param(struct sst_hsw *hsw,
1984 u32 module_id, u32 instance_id, u32 parameter_id,
1985 u32 param_size, char *param)
1989 u32 payload_size = 0, transfer_parameter_size = 0;
1990 struct sst_hsw_transfer_parameter *parameter;
1991 struct device *dev = hsw->dev;
1993 header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
1994 IPC_MODULE_OPERATION(IPC_MODULE_SET_PARAMETER) |
1995 IPC_MODULE_ID(module_id);
1996 dev_dbg(dev, "sst_hsw_module_set_param header=%x\n", header);
1998 payload_size = param_size +
1999 sizeof(struct sst_hsw_transfer_parameter) -
2000 sizeof(struct sst_hsw_transfer_list);
2001 dev_dbg(dev, "parameter size : %d\n", param_size);
2002 dev_dbg(dev, "payload size : %d\n", payload_size);
2004 if (payload_size <= SST_HSW_IPC_MAX_SHORT_PARAMETER_SIZE) {
2005 /* short parameter, mailbox can contain data */
2006 dev_dbg(dev, "transfer parameter size : %d\n",
2007 transfer_parameter_size);
2009 transfer_parameter_size = ALIGN(payload_size, 4);
2010 dev_dbg(dev, "transfer parameter aligned size : %d\n",
2011 transfer_parameter_size);
2013 parameter = kzalloc(transfer_parameter_size, GFP_KERNEL);
2014 if (parameter == NULL)
2017 memcpy(parameter->data, param, param_size);
2019 dev_warn(dev, "transfer parameter size too large!");
2023 parameter->parameter_id = parameter_id;
2024 parameter->data_size = param_size;
2026 ret = sst_ipc_tx_message_wait(&hsw->ipc, header,
2027 parameter, transfer_parameter_size , NULL, 0);
2029 dev_err(dev, "ipc: module set parameter failed - %d\n", ret);
2036 static struct sst_dsp_device hsw_dev = {
2037 .thread = hsw_irq_thread,
2038 .ops = &haswell_ops,
2041 static void hsw_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
2043 /* send the message */
2044 sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
2045 sst_dsp_ipc_msg_tx(ipc->dsp, msg->header);
2048 static void hsw_shim_dbg(struct sst_generic_ipc *ipc, const char *text)
2050 struct sst_dsp *sst = ipc->dsp;
2051 u32 isr, ipcd, imrx, ipcx;
2053 ipcx = sst_dsp_shim_read_unlocked(sst, SST_IPCX);
2054 isr = sst_dsp_shim_read_unlocked(sst, SST_ISRX);
2055 ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
2056 imrx = sst_dsp_shim_read_unlocked(sst, SST_IMRX);
2059 "ipc: --%s-- ipcx 0x%8.8x isr 0x%8.8x ipcd 0x%8.8x imrx 0x%8.8x\n",
2060 text, ipcx, isr, ipcd, imrx);
2063 static void hsw_tx_data_copy(struct ipc_message *msg, char *tx_data,
2066 memcpy(msg->tx_data, tx_data, tx_size);
2069 static u64 hsw_reply_msg_match(u64 header, u64 *mask)
2071 /* clear reply bits & status bits */
2072 header &= ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
2078 static bool hsw_is_dsp_busy(struct sst_dsp *dsp)
2082 ipcx = sst_dsp_shim_read_unlocked(dsp, SST_IPCX);
2083 return (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE));
2086 int sst_hsw_dsp_init(struct device *dev, struct sst_pdata *pdata)
2088 struct sst_hsw_ipc_fw_version version;
2089 struct sst_hsw *hsw;
2090 struct sst_generic_ipc *ipc;
2093 dev_dbg(dev, "initialising Audio DSP IPC\n");
2095 hsw = devm_kzalloc(dev, sizeof(*hsw), GFP_KERNEL);
2103 ipc->ops.tx_msg = hsw_tx_msg;
2104 ipc->ops.shim_dbg = hsw_shim_dbg;
2105 ipc->ops.tx_data_copy = hsw_tx_data_copy;
2106 ipc->ops.reply_msg_match = hsw_reply_msg_match;
2107 ipc->ops.is_dsp_busy = hsw_is_dsp_busy;
2109 ipc->tx_data_max_size = IPC_MAX_MAILBOX_BYTES;
2110 ipc->rx_data_max_size = IPC_MAX_MAILBOX_BYTES;
2112 ret = sst_ipc_init(ipc);
2116 INIT_LIST_HEAD(&hsw->stream_list);
2117 init_waitqueue_head(&hsw->boot_wait);
2118 hsw_dev.thread_context = hsw;
2121 hsw->dsp = sst_dsp_new(dev, &hsw_dev, pdata);
2122 if (hsw->dsp == NULL) {
2127 ipc->dsp = hsw->dsp;
2129 /* allocate DMA buffer for context storage */
2130 hsw->dx_context = dma_alloc_coherent(hsw->dsp->dma_dev,
2131 SST_HSW_DX_CONTEXT_SIZE, &hsw->dx_context_paddr, GFP_KERNEL);
2132 if (hsw->dx_context == NULL) {
2137 /* keep the DSP in reset state for base FW loading */
2138 sst_dsp_reset(hsw->dsp);
2140 /* load base module and other modules in base firmware image */
2141 ret = sst_hsw_module_load(hsw, SST_HSW_MODULE_BASE_FW, 0, "Base");
2145 /* try to load module waves */
2146 sst_hsw_module_load(hsw, SST_HSW_MODULE_WAVES, 0, "/*(DEBLOBBED)*/");
2148 /* allocate scratch mem regions */
2149 ret = sst_block_alloc_scratch(hsw->dsp);
2153 /* init param buffer */
2154 sst_hsw_reset_param_buf(hsw);
2156 /* wait for DSP boot completion */
2157 sst_dsp_boot(hsw->dsp);
2158 ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
2159 msecs_to_jiffies(IPC_BOOT_MSECS));
2162 dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
2163 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
2164 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
2168 /* init module state after boot */
2169 sst_hsw_init_module_state(hsw);
2171 /* get the FW version */
2172 sst_hsw_fw_get_version(hsw, &version);
2174 /* get the globalmixer */
2175 ret = sst_hsw_mixer_get_info(hsw);
2177 dev_err(hsw->dev, "error: failed to get stream info\n");
2185 sst_dsp_reset(hsw->dsp);
2186 sst_fw_free_all(hsw->dsp);
2188 dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
2189 hsw->dx_context, hsw->dx_context_paddr);
2191 sst_dsp_free(hsw->dsp);
2197 EXPORT_SYMBOL_GPL(sst_hsw_dsp_init);
2199 void sst_hsw_dsp_free(struct device *dev, struct sst_pdata *pdata)
2201 struct sst_hsw *hsw = pdata->dsp;
2203 sst_dsp_reset(hsw->dsp);
2204 sst_fw_free_all(hsw->dsp);
2205 dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
2206 hsw->dx_context, hsw->dx_context_paddr);
2207 sst_dsp_free(hsw->dsp);
2208 sst_ipc_fini(&hsw->ipc);
2210 EXPORT_SYMBOL_GPL(sst_hsw_dsp_free);