blob: 265d754a40907a324c56d2e777d1ce4bfda13fa1 [file] [log] [blame]
Mark Browna4b12992014-03-12 23:04:35 +00001/*
2 * Intel SST Haswell/Broadwell IPC Support
3 *
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
5 *
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.
9 *
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.
14 *
15 */
16
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>
Mark Browna4b12992014-03-12 23:04:35 +000028#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>
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +000033#include <linux/pm_runtime.h>
Libin Yang1b006992015-02-10 10:02:47 +080034#include <sound/asound.h>
Mark Browna4b12992014-03-12 23:04:35 +000035
36#include "sst-haswell-ipc.h"
37#include "sst-dsp.h"
38#include "sst-dsp-priv.h"
39
40/* Global Message - Generic */
41#define IPC_GLB_TYPE_SHIFT 24
42#define IPC_GLB_TYPE_MASK (0x1f << IPC_GLB_TYPE_SHIFT)
43#define IPC_GLB_TYPE(x) (x << IPC_GLB_TYPE_SHIFT)
44
45/* Global Message - Reply */
46#define IPC_GLB_REPLY_SHIFT 0
47#define IPC_GLB_REPLY_MASK (0x1f << IPC_GLB_REPLY_SHIFT)
48#define IPC_GLB_REPLY_TYPE(x) (x << IPC_GLB_REPLY_TYPE_SHIFT)
49
50/* Stream Message - Generic */
51#define IPC_STR_TYPE_SHIFT 20
52#define IPC_STR_TYPE_MASK (0xf << IPC_STR_TYPE_SHIFT)
53#define IPC_STR_TYPE(x) (x << IPC_STR_TYPE_SHIFT)
54#define IPC_STR_ID_SHIFT 16
55#define IPC_STR_ID_MASK (0xf << IPC_STR_ID_SHIFT)
56#define IPC_STR_ID(x) (x << IPC_STR_ID_SHIFT)
57
58/* Stream Message - Reply */
59#define IPC_STR_REPLY_SHIFT 0
60#define IPC_STR_REPLY_MASK (0x1f << IPC_STR_REPLY_SHIFT)
61
62/* Stream Stage Message - Generic */
63#define IPC_STG_TYPE_SHIFT 12
64#define IPC_STG_TYPE_MASK (0xf << IPC_STG_TYPE_SHIFT)
65#define IPC_STG_TYPE(x) (x << IPC_STG_TYPE_SHIFT)
66#define IPC_STG_ID_SHIFT 10
67#define IPC_STG_ID_MASK (0x3 << IPC_STG_ID_SHIFT)
68#define IPC_STG_ID(x) (x << IPC_STG_ID_SHIFT)
69
70/* Stream Stage Message - Reply */
71#define IPC_STG_REPLY_SHIFT 0
72#define IPC_STG_REPLY_MASK (0x1f << IPC_STG_REPLY_SHIFT)
73
74/* Debug Log Message - Generic */
75#define IPC_LOG_OP_SHIFT 20
76#define IPC_LOG_OP_MASK (0xf << IPC_LOG_OP_SHIFT)
77#define IPC_LOG_OP_TYPE(x) (x << IPC_LOG_OP_SHIFT)
78#define IPC_LOG_ID_SHIFT 16
79#define IPC_LOG_ID_MASK (0xf << IPC_LOG_ID_SHIFT)
80#define IPC_LOG_ID(x) (x << IPC_LOG_ID_SHIFT)
81
Lu, Hane8e79ed2015-03-10 10:41:22 +080082/* Module Message */
83#define IPC_MODULE_OPERATION_SHIFT 20
84#define IPC_MODULE_OPERATION_MASK (0xf << IPC_MODULE_OPERATION_SHIFT)
85#define IPC_MODULE_OPERATION(x) (x << IPC_MODULE_OPERATION_SHIFT)
86
87#define IPC_MODULE_ID_SHIFT 16
88#define IPC_MODULE_ID_MASK (0xf << IPC_MODULE_ID_SHIFT)
89#define IPC_MODULE_ID(x) (x << IPC_MODULE_ID_SHIFT)
90
Mark Browna4b12992014-03-12 23:04:35 +000091/* IPC message timeout (msecs) */
92#define IPC_TIMEOUT_MSECS 300
93#define IPC_BOOT_MSECS 200
94#define IPC_MSG_WAIT 0
95#define IPC_MSG_NOWAIT 1
96
97/* Firmware Ready Message */
98#define IPC_FW_READY (0x1 << 29)
99#define IPC_STATUS_MASK (0x3 << 30)
100
101#define IPC_EMPTY_LIST_SIZE 8
102#define IPC_MAX_STREAMS 4
103
104/* Mailbox */
105#define IPC_MAX_MAILBOX_BYTES 256
106
Jie Yanga0a7c482015-01-12 17:17:34 +0800107#define INVALID_STREAM_HW_ID 0xffffffff
108
Mark Browna4b12992014-03-12 23:04:35 +0000109/* Global Message - Types and Replies */
110enum ipc_glb_type {
111 IPC_GLB_GET_FW_VERSION = 0, /* Retrieves firmware version */
112 IPC_GLB_PERFORMANCE_MONITOR = 1, /* Performance monitoring actions */
113 IPC_GLB_ALLOCATE_STREAM = 3, /* Request to allocate new stream */
114 IPC_GLB_FREE_STREAM = 4, /* Request to free stream */
115 IPC_GLB_GET_FW_CAPABILITIES = 5, /* Retrieves firmware capabilities */
116 IPC_GLB_STREAM_MESSAGE = 6, /* Message directed to stream or its stages */
117 /* Request to store firmware context during D0->D3 transition */
118 IPC_GLB_REQUEST_DUMP = 7,
119 /* Request to restore firmware context during D3->D0 transition */
120 IPC_GLB_RESTORE_CONTEXT = 8,
121 IPC_GLB_GET_DEVICE_FORMATS = 9, /* Set device format */
122 IPC_GLB_SET_DEVICE_FORMATS = 10, /* Get device format */
123 IPC_GLB_SHORT_REPLY = 11,
124 IPC_GLB_ENTER_DX_STATE = 12,
125 IPC_GLB_GET_MIXER_STREAM_INFO = 13, /* Request mixer stream params */
126 IPC_GLB_DEBUG_LOG_MESSAGE = 14, /* Message to or from the debug logger. */
Lu, Hane8e79ed2015-03-10 10:41:22 +0800127 IPC_GLB_MODULE_OPERATION = 15, /* Message to loadable fw module */
Mark Browna4b12992014-03-12 23:04:35 +0000128 IPC_GLB_REQUEST_TRANSFER = 16, /* < Request Transfer for host */
129 IPC_GLB_MAX_IPC_MESSAGE_TYPE = 17, /* Maximum message number */
130};
131
132enum ipc_glb_reply {
133 IPC_GLB_REPLY_SUCCESS = 0, /* The operation was successful. */
134 IPC_GLB_REPLY_ERROR_INVALID_PARAM = 1, /* Invalid parameter was passed. */
135 IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE = 2, /* Uknown message type was resceived. */
136 IPC_GLB_REPLY_OUT_OF_RESOURCES = 3, /* No resources to satisfy the request. */
137 IPC_GLB_REPLY_BUSY = 4, /* The system or resource is busy. */
138 IPC_GLB_REPLY_PENDING = 5, /* The action was scheduled for processing. */
139 IPC_GLB_REPLY_FAILURE = 6, /* Critical error happened. */
140 IPC_GLB_REPLY_INVALID_REQUEST = 7, /* Request can not be completed. */
141 IPC_GLB_REPLY_STAGE_UNINITIALIZED = 8, /* Processing stage was uninitialized. */
142 IPC_GLB_REPLY_NOT_FOUND = 9, /* Required resource can not be found. */
143 IPC_GLB_REPLY_SOURCE_NOT_STARTED = 10, /* Source was not started. */
144};
145
Lu, Hane8e79ed2015-03-10 10:41:22 +0800146enum ipc_module_operation {
147 IPC_MODULE_NOTIFICATION = 0,
148 IPC_MODULE_ENABLE = 1,
149 IPC_MODULE_DISABLE = 2,
150 IPC_MODULE_GET_PARAMETER = 3,
151 IPC_MODULE_SET_PARAMETER = 4,
152 IPC_MODULE_GET_INFO = 5,
153 IPC_MODULE_MAX_MESSAGE
154};
155
Mark Browna4b12992014-03-12 23:04:35 +0000156/* Stream Message - Types */
157enum ipc_str_operation {
158 IPC_STR_RESET = 0,
159 IPC_STR_PAUSE = 1,
160 IPC_STR_RESUME = 2,
161 IPC_STR_STAGE_MESSAGE = 3,
162 IPC_STR_NOTIFICATION = 4,
163 IPC_STR_MAX_MESSAGE
164};
165
166/* Stream Stage Message Types */
167enum ipc_stg_operation {
168 IPC_STG_GET_VOLUME = 0,
169 IPC_STG_SET_VOLUME,
170 IPC_STG_SET_WRITE_POSITION,
171 IPC_STG_SET_FX_ENABLE,
172 IPC_STG_SET_FX_DISABLE,
173 IPC_STG_SET_FX_GET_PARAM,
174 IPC_STG_SET_FX_SET_PARAM,
175 IPC_STG_SET_FX_GET_INFO,
176 IPC_STG_MUTE_LOOPBACK,
177 IPC_STG_MAX_MESSAGE
178};
179
180/* Stream Stage Message Types For Notification*/
181enum ipc_stg_operation_notify {
182 IPC_POSITION_CHANGED = 0,
183 IPC_STG_GLITCH,
184 IPC_STG_MAX_NOTIFY
185};
186
187enum ipc_glitch_type {
188 IPC_GLITCH_UNDERRUN = 1,
189 IPC_GLITCH_DECODER_ERROR,
190 IPC_GLITCH_DOUBLED_WRITE_POS,
191 IPC_GLITCH_MAX
192};
193
194/* Debug Control */
195enum ipc_debug_operation {
196 IPC_DEBUG_ENABLE_LOG = 0,
197 IPC_DEBUG_DISABLE_LOG = 1,
198 IPC_DEBUG_REQUEST_LOG_DUMP = 2,
199 IPC_DEBUG_NOTIFY_LOG_DUMP = 3,
200 IPC_DEBUG_MAX_DEBUG_LOG
201};
202
203/* Firmware Ready */
204struct sst_hsw_ipc_fw_ready {
205 u32 inbox_offset;
206 u32 outbox_offset;
207 u32 inbox_size;
208 u32 outbox_size;
209 u32 fw_info_size;
Jie Yang249addd2014-07-15 08:51:12 +0800210 u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
Mark Browna4b12992014-03-12 23:04:35 +0000211} __attribute__((packed));
212
213struct ipc_message {
214 struct list_head list;
215 u32 header;
216
217 /* direction wrt host CPU */
218 char tx_data[IPC_MAX_MAILBOX_BYTES];
219 size_t tx_size;
220 char rx_data[IPC_MAX_MAILBOX_BYTES];
221 size_t rx_size;
222
223 wait_queue_head_t waitq;
224 bool pending;
225 bool complete;
226 bool wait;
227 int errno;
228};
229
230struct sst_hsw_stream;
231struct sst_hsw;
232
233/* Stream infomation */
234struct sst_hsw_stream {
235 /* configuration */
236 struct sst_hsw_ipc_stream_alloc_req request;
237 struct sst_hsw_ipc_stream_alloc_reply reply;
238 struct sst_hsw_ipc_stream_free_req free_req;
239
240 /* Mixer info */
241 u32 mute_volume[SST_HSW_NO_CHANNELS];
242 u32 mute[SST_HSW_NO_CHANNELS];
243
244 /* runtime info */
245 struct sst_hsw *hsw;
246 int host_id;
247 bool commited;
248 bool running;
249
250 /* Notification work */
251 struct work_struct notify_work;
252 u32 header;
253
254 /* Position info from DSP */
255 struct sst_hsw_ipc_stream_set_position wpos;
256 struct sst_hsw_ipc_stream_get_position rpos;
257 struct sst_hsw_ipc_stream_glitch_position glitch;
258
259 /* Volume info */
260 struct sst_hsw_ipc_volume_req vol_req;
261
262 /* driver callback */
263 u32 (*notify_position)(struct sst_hsw_stream *stream, void *data);
264 void *pdata;
265
Libin Yang1b006992015-02-10 10:02:47 +0800266 /* record the fw read position when playback */
267 snd_pcm_uframes_t old_position;
268 bool play_silence;
Mark Browna4b12992014-03-12 23:04:35 +0000269 struct list_head node;
270};
271
272/* FW log ring information */
273struct sst_hsw_log_stream {
274 dma_addr_t dma_addr;
275 unsigned char *dma_area;
276 unsigned char *ring_descr;
277 int pages;
278 int size;
279
280 /* Notification work */
281 struct work_struct notify_work;
282 wait_queue_head_t readers_wait_q;
283 struct mutex rw_mutex;
284
285 u32 last_pos;
286 u32 curr_pos;
287 u32 reader_pos;
288
289 /* fw log config */
290 u32 config[SST_HSW_FW_LOG_CONFIG_DWORDS];
291
292 struct sst_hsw *hsw;
293};
294
295/* SST Haswell IPC data */
296struct sst_hsw {
297 struct device *dev;
298 struct sst_dsp *dsp;
299 struct platform_device *pdev_pcm;
300
301 /* FW config */
302 struct sst_hsw_ipc_fw_ready fw_ready;
303 struct sst_hsw_ipc_fw_version version;
Mark Browna4b12992014-03-12 23:04:35 +0000304 bool fw_done;
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +0000305 struct sst_fw *sst_fw;
Mark Browna4b12992014-03-12 23:04:35 +0000306
307 /* stream */
308 struct list_head stream_list;
309
310 /* global mixer */
311 struct sst_hsw_ipc_stream_info_reply mixer_info;
312 enum sst_hsw_volume_curve curve_type;
313 u32 curve_duration;
314 u32 mute[SST_HSW_NO_CHANNELS];
315 u32 mute_volume[SST_HSW_NO_CHANNELS];
316
317 /* DX */
318 struct sst_hsw_ipc_dx_reply dx;
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +0000319 void *dx_context;
320 dma_addr_t dx_context_paddr;
Mark Browna4b12992014-03-12 23:04:35 +0000321
322 /* boot */
323 wait_queue_head_t boot_wait;
324 bool boot_complete;
325 bool shutdown;
326
327 /* IPC messaging */
328 struct list_head tx_list;
329 struct list_head rx_list;
330 struct list_head empty_list;
331 wait_queue_head_t wait_txq;
332 struct task_struct *tx_thread;
333 struct kthread_worker kworker;
334 struct kthread_work kwork;
335 bool pending;
336 struct ipc_message *msg;
337
338 /* FW log stream */
339 struct sst_hsw_log_stream log_stream;
340};
341
342#define CREATE_TRACE_POINTS
343#include <trace/events/hswadsp.h>
344
345static inline u32 msg_get_global_type(u32 msg)
346{
347 return (msg & IPC_GLB_TYPE_MASK) >> IPC_GLB_TYPE_SHIFT;
348}
349
350static inline u32 msg_get_global_reply(u32 msg)
351{
352 return (msg & IPC_GLB_REPLY_MASK) >> IPC_GLB_REPLY_SHIFT;
353}
354
355static inline u32 msg_get_stream_type(u32 msg)
356{
357 return (msg & IPC_STR_TYPE_MASK) >> IPC_STR_TYPE_SHIFT;
358}
359
360static inline u32 msg_get_stage_type(u32 msg)
361{
362 return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
363}
364
Mark Browna4b12992014-03-12 23:04:35 +0000365static inline u32 msg_get_stream_id(u32 msg)
366{
367 return (msg & IPC_STR_ID_MASK) >> IPC_STR_ID_SHIFT;
368}
369
370static inline u32 msg_get_notify_reason(u32 msg)
371{
372 return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
373}
374
Lu, Hane8e79ed2015-03-10 10:41:22 +0800375static inline u32 msg_get_module_operation(u32 msg)
376{
377 return (msg & IPC_MODULE_OPERATION_MASK) >> IPC_MODULE_OPERATION_SHIFT;
378}
379
380static inline u32 msg_get_module_id(u32 msg)
381{
382 return (msg & IPC_MODULE_ID_MASK) >> IPC_MODULE_ID_SHIFT;
383}
384
Mark Browna4b12992014-03-12 23:04:35 +0000385u32 create_channel_map(enum sst_hsw_channel_config config)
386{
387 switch (config) {
388 case SST_HSW_CHANNEL_CONFIG_MONO:
389 return (0xFFFFFFF0 | SST_HSW_CHANNEL_CENTER);
390 case SST_HSW_CHANNEL_CONFIG_STEREO:
391 return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
392 | (SST_HSW_CHANNEL_RIGHT << 4));
393 case SST_HSW_CHANNEL_CONFIG_2_POINT_1:
394 return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
395 | (SST_HSW_CHANNEL_RIGHT << 4)
396 | (SST_HSW_CHANNEL_LFE << 8 ));
397 case SST_HSW_CHANNEL_CONFIG_3_POINT_0:
398 return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
399 | (SST_HSW_CHANNEL_CENTER << 4)
400 | (SST_HSW_CHANNEL_RIGHT << 8));
401 case SST_HSW_CHANNEL_CONFIG_3_POINT_1:
402 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
403 | (SST_HSW_CHANNEL_CENTER << 4)
404 | (SST_HSW_CHANNEL_RIGHT << 8)
405 | (SST_HSW_CHANNEL_LFE << 12));
406 case SST_HSW_CHANNEL_CONFIG_QUATRO:
407 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
408 | (SST_HSW_CHANNEL_RIGHT << 4)
409 | (SST_HSW_CHANNEL_LEFT_SURROUND << 8)
410 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 12));
411 case SST_HSW_CHANNEL_CONFIG_4_POINT_0:
412 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
413 | (SST_HSW_CHANNEL_CENTER << 4)
414 | (SST_HSW_CHANNEL_RIGHT << 8)
415 | (SST_HSW_CHANNEL_CENTER_SURROUND << 12));
416 case SST_HSW_CHANNEL_CONFIG_5_POINT_0:
417 return (0xFFF00000 | SST_HSW_CHANNEL_LEFT
418 | (SST_HSW_CHANNEL_CENTER << 4)
419 | (SST_HSW_CHANNEL_RIGHT << 8)
420 | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
421 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16));
422 case SST_HSW_CHANNEL_CONFIG_5_POINT_1:
423 return (0xFF000000 | SST_HSW_CHANNEL_CENTER
424 | (SST_HSW_CHANNEL_LEFT << 4)
425 | (SST_HSW_CHANNEL_RIGHT << 8)
426 | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
427 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16)
428 | (SST_HSW_CHANNEL_LFE << 20));
429 case SST_HSW_CHANNEL_CONFIG_DUAL_MONO:
430 return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
431 | (SST_HSW_CHANNEL_LEFT << 4));
432 default:
433 return 0xFFFFFFFF;
434 }
435}
436
437static struct sst_hsw_stream *get_stream_by_id(struct sst_hsw *hsw,
438 int stream_id)
439{
440 struct sst_hsw_stream *stream;
441
442 list_for_each_entry(stream, &hsw->stream_list, node) {
443 if (stream->reply.stream_hw_id == stream_id)
444 return stream;
445 }
446
447 return NULL;
448}
449
450static void ipc_shim_dbg(struct sst_hsw *hsw, const char *text)
451{
452 struct sst_dsp *sst = hsw->dsp;
453 u32 isr, ipcd, imrx, ipcx;
454
455 ipcx = sst_dsp_shim_read_unlocked(sst, SST_IPCX);
456 isr = sst_dsp_shim_read_unlocked(sst, SST_ISRX);
457 ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
458 imrx = sst_dsp_shim_read_unlocked(sst, SST_IMRX);
459
460 dev_err(hsw->dev, "ipc: --%s-- ipcx 0x%8.8x isr 0x%8.8x ipcd 0x%8.8x imrx 0x%8.8x\n",
461 text, ipcx, isr, ipcd, imrx);
462}
463
464/* locks held by caller */
465static struct ipc_message *msg_get_empty(struct sst_hsw *hsw)
466{
467 struct ipc_message *msg = NULL;
468
469 if (!list_empty(&hsw->empty_list)) {
470 msg = list_first_entry(&hsw->empty_list, struct ipc_message,
471 list);
472 list_del(&msg->list);
473 }
474
475 return msg;
476}
477
478static void ipc_tx_msgs(struct kthread_work *work)
479{
480 struct sst_hsw *hsw =
481 container_of(work, struct sst_hsw, kwork);
482 struct ipc_message *msg;
483 unsigned long flags;
484 u32 ipcx;
485
486 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
487
488 if (list_empty(&hsw->tx_list) || hsw->pending) {
489 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
490 return;
491 }
492
Paweł Piskorski94ce3342014-08-01 23:09:44 +0800493 /* if the DSP is busy, we will TX messages after IRQ.
494 * also postpone if we are in the middle of procesing completion irq*/
Mark Browna4b12992014-03-12 23:04:35 +0000495 ipcx = sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX);
Paweł Piskorski94ce3342014-08-01 23:09:44 +0800496 if (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE)) {
Mark Browna4b12992014-03-12 23:04:35 +0000497 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
498 return;
499 }
500
501 msg = list_first_entry(&hsw->tx_list, struct ipc_message, list);
502
503 list_move(&msg->list, &hsw->rx_list);
504
505 /* send the message */
506 sst_dsp_outbox_write(hsw->dsp, msg->tx_data, msg->tx_size);
507 sst_dsp_ipc_msg_tx(hsw->dsp, msg->header | SST_IPCX_BUSY);
508
509 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
510}
511
512/* locks held by caller */
513static void tx_msg_reply_complete(struct sst_hsw *hsw, struct ipc_message *msg)
514{
515 msg->complete = true;
516 trace_ipc_reply("completed", msg->header);
517
518 if (!msg->wait)
519 list_add_tail(&msg->list, &hsw->empty_list);
520 else
521 wake_up(&msg->waitq);
522}
523
524static int tx_wait_done(struct sst_hsw *hsw, struct ipc_message *msg,
525 void *rx_data)
526{
527 unsigned long flags;
528 int ret;
529
530 /* wait for DSP completion (in all cases atm inc pending) */
531 ret = wait_event_timeout(msg->waitq, msg->complete,
532 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
533
534 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
535 if (ret == 0) {
536 ipc_shim_dbg(hsw, "message timeout");
537
538 trace_ipc_error("error message timeout for", msg->header);
Liam Girdwood97cfc752014-08-01 23:08:38 +0800539 list_del(&msg->list);
Mark Browna4b12992014-03-12 23:04:35 +0000540 ret = -ETIMEDOUT;
541 } else {
542
543 /* copy the data returned from DSP */
544 if (msg->rx_size)
545 memcpy(rx_data, msg->rx_data, msg->rx_size);
546 ret = msg->errno;
547 }
548
549 list_add_tail(&msg->list, &hsw->empty_list);
550 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
551 return ret;
552}
553
554static int ipc_tx_message(struct sst_hsw *hsw, u32 header, void *tx_data,
555 size_t tx_bytes, void *rx_data, size_t rx_bytes, int wait)
556{
557 struct ipc_message *msg;
558 unsigned long flags;
559
560 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
561
562 msg = msg_get_empty(hsw);
563 if (msg == NULL) {
564 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
565 return -EBUSY;
566 }
567
568 if (tx_bytes)
569 memcpy(msg->tx_data, tx_data, tx_bytes);
570
571 msg->header = header;
572 msg->tx_size = tx_bytes;
573 msg->rx_size = rx_bytes;
574 msg->wait = wait;
575 msg->errno = 0;
576 msg->pending = false;
577 msg->complete = false;
578
579 list_add_tail(&msg->list, &hsw->tx_list);
580 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
581
582 queue_kthread_work(&hsw->kworker, &hsw->kwork);
583
584 if (wait)
585 return tx_wait_done(hsw, msg, rx_data);
586 else
587 return 0;
588}
589
590static inline int ipc_tx_message_wait(struct sst_hsw *hsw, u32 header,
591 void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes)
592{
593 return ipc_tx_message(hsw, header, tx_data, tx_bytes, rx_data,
594 rx_bytes, 1);
595}
596
597static inline int ipc_tx_message_nowait(struct sst_hsw *hsw, u32 header,
598 void *tx_data, size_t tx_bytes)
599{
600 return ipc_tx_message(hsw, header, tx_data, tx_bytes, NULL, 0, 0);
601}
602
603static void hsw_fw_ready(struct sst_hsw *hsw, u32 header)
604{
605 struct sst_hsw_ipc_fw_ready fw_ready;
606 u32 offset;
Jie Yang249addd2014-07-15 08:51:12 +0800607 u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
608 char *tmp[5], *pinfo;
609 int i = 0;
Mark Browna4b12992014-03-12 23:04:35 +0000610
611 offset = (header & 0x1FFFFFFF) << 3;
612
613 dev_dbg(hsw->dev, "ipc: DSP is ready 0x%8.8x offset %d\n",
614 header, offset);
615
616 /* copy data from the DSP FW ready offset */
617 sst_dsp_read(hsw->dsp, &fw_ready, offset, sizeof(fw_ready));
618
619 sst_dsp_mailbox_init(hsw->dsp, fw_ready.inbox_offset,
620 fw_ready.inbox_size, fw_ready.outbox_offset,
621 fw_ready.outbox_size);
622
623 hsw->boot_complete = true;
624 wake_up(&hsw->boot_wait);
625
626 dev_dbg(hsw->dev, " mailbox upstream 0x%x - size 0x%x\n",
627 fw_ready.inbox_offset, fw_ready.inbox_size);
628 dev_dbg(hsw->dev, " mailbox downstream 0x%x - size 0x%x\n",
629 fw_ready.outbox_offset, fw_ready.outbox_size);
Jie Yang249addd2014-07-15 08:51:12 +0800630 if (fw_ready.fw_info_size < sizeof(fw_ready.fw_info)) {
631 fw_ready.fw_info[fw_ready.fw_info_size] = 0;
632 dev_dbg(hsw->dev, " Firmware info: %s \n", fw_ready.fw_info);
633
634 /* log the FW version info got from the mailbox here. */
635 memcpy(fw_info, fw_ready.fw_info, fw_ready.fw_info_size);
636 pinfo = &fw_info[0];
637 for (i = 0; i < sizeof(tmp) / sizeof(char *); i++)
638 tmp[i] = strsep(&pinfo, " ");
639 dev_info(hsw->dev, "FW loaded, mailbox readback FW info: type %s, - "
640 "version: %s.%s, build %s, source commit id: %s\n",
641 tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
642 }
Mark Browna4b12992014-03-12 23:04:35 +0000643}
644
645static void hsw_notification_work(struct work_struct *work)
646{
647 struct sst_hsw_stream *stream = container_of(work,
648 struct sst_hsw_stream, notify_work);
649 struct sst_hsw_ipc_stream_glitch_position *glitch = &stream->glitch;
650 struct sst_hsw_ipc_stream_get_position *pos = &stream->rpos;
651 struct sst_hsw *hsw = stream->hsw;
652 u32 reason;
653
654 reason = msg_get_notify_reason(stream->header);
655
656 switch (reason) {
657 case IPC_STG_GLITCH:
658 trace_ipc_notification("DSP stream under/overrun",
659 stream->reply.stream_hw_id);
660 sst_dsp_inbox_read(hsw->dsp, glitch, sizeof(*glitch));
661
662 dev_err(hsw->dev, "glitch %d pos 0x%x write pos 0x%x\n",
663 glitch->glitch_type, glitch->present_pos,
664 glitch->write_pos);
665 break;
666
667 case IPC_POSITION_CHANGED:
668 trace_ipc_notification("DSP stream position changed for",
669 stream->reply.stream_hw_id);
Dan Carpenter7897ab72014-04-16 18:38:11 +0300670 sst_dsp_inbox_read(hsw->dsp, pos, sizeof(*pos));
Mark Browna4b12992014-03-12 23:04:35 +0000671
672 if (stream->notify_position)
673 stream->notify_position(stream, stream->pdata);
674
675 break;
676 default:
677 dev_err(hsw->dev, "error: unknown notification 0x%x\n",
678 stream->header);
679 break;
680 }
681
682 /* tell DSP that notification has been handled */
Jie Yang09a34aa2015-01-21 07:20:23 +0800683 sst_dsp_shim_update_bits(hsw->dsp, SST_IPCD,
Mark Browna4b12992014-03-12 23:04:35 +0000684 SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
685
686 /* unmask busy interrupt */
Jie Yang09a34aa2015-01-21 07:20:23 +0800687 sst_dsp_shim_update_bits(hsw->dsp, SST_IMRX, SST_IMRX_BUSY, 0);
Mark Browna4b12992014-03-12 23:04:35 +0000688}
689
690static struct ipc_message *reply_find_msg(struct sst_hsw *hsw, u32 header)
691{
692 struct ipc_message *msg;
693
694 /* clear reply bits & status bits */
695 header &= ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
696
697 if (list_empty(&hsw->rx_list)) {
698 dev_err(hsw->dev, "error: rx list empty but received 0x%x\n",
699 header);
700 return NULL;
701 }
702
703 list_for_each_entry(msg, &hsw->rx_list, list) {
704 if (msg->header == header)
705 return msg;
706 }
707
708 return NULL;
709}
710
711static void hsw_stream_update(struct sst_hsw *hsw, struct ipc_message *msg)
712{
713 struct sst_hsw_stream *stream;
714 u32 header = msg->header & ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
715 u32 stream_id = msg_get_stream_id(header);
716 u32 stream_msg = msg_get_stream_type(header);
717
718 stream = get_stream_by_id(hsw, stream_id);
719 if (stream == NULL)
720 return;
721
722 switch (stream_msg) {
723 case IPC_STR_STAGE_MESSAGE:
724 case IPC_STR_NOTIFICATION:
Liam Girdwood81552612014-07-30 20:09:47 +0800725 break;
Mark Browna4b12992014-03-12 23:04:35 +0000726 case IPC_STR_RESET:
Liam Girdwood81552612014-07-30 20:09:47 +0800727 trace_ipc_notification("stream reset", stream->reply.stream_hw_id);
Mark Browna4b12992014-03-12 23:04:35 +0000728 break;
729 case IPC_STR_PAUSE:
730 stream->running = false;
731 trace_ipc_notification("stream paused",
732 stream->reply.stream_hw_id);
733 break;
734 case IPC_STR_RESUME:
735 stream->running = true;
736 trace_ipc_notification("stream running",
737 stream->reply.stream_hw_id);
738 break;
739 }
740}
741
742static int hsw_process_reply(struct sst_hsw *hsw, u32 header)
743{
744 struct ipc_message *msg;
745 u32 reply = msg_get_global_reply(header);
746
747 trace_ipc_reply("processing -->", header);
748
749 msg = reply_find_msg(hsw, header);
750 if (msg == NULL) {
751 trace_ipc_error("error: can't find message header", header);
752 return -EIO;
753 }
754
755 /* first process the header */
756 switch (reply) {
757 case IPC_GLB_REPLY_PENDING:
758 trace_ipc_pending_reply("received", header);
759 msg->pending = true;
760 hsw->pending = true;
761 return 1;
762 case IPC_GLB_REPLY_SUCCESS:
763 if (msg->pending) {
764 trace_ipc_pending_reply("completed", header);
765 sst_dsp_inbox_read(hsw->dsp, msg->rx_data,
766 msg->rx_size);
767 hsw->pending = false;
768 } else {
769 /* copy data from the DSP */
770 sst_dsp_outbox_read(hsw->dsp, msg->rx_data,
771 msg->rx_size);
772 }
773 break;
774 /* these will be rare - but useful for debug */
775 case IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE:
776 trace_ipc_error("error: unknown message type", header);
777 msg->errno = -EBADMSG;
778 break;
779 case IPC_GLB_REPLY_OUT_OF_RESOURCES:
780 trace_ipc_error("error: out of resources", header);
781 msg->errno = -ENOMEM;
782 break;
783 case IPC_GLB_REPLY_BUSY:
784 trace_ipc_error("error: reply busy", header);
785 msg->errno = -EBUSY;
786 break;
787 case IPC_GLB_REPLY_FAILURE:
788 trace_ipc_error("error: reply failure", header);
789 msg->errno = -EINVAL;
790 break;
791 case IPC_GLB_REPLY_STAGE_UNINITIALIZED:
792 trace_ipc_error("error: stage uninitialized", header);
793 msg->errno = -EINVAL;
794 break;
795 case IPC_GLB_REPLY_NOT_FOUND:
796 trace_ipc_error("error: reply not found", header);
797 msg->errno = -EINVAL;
798 break;
799 case IPC_GLB_REPLY_SOURCE_NOT_STARTED:
800 trace_ipc_error("error: source not started", header);
801 msg->errno = -EINVAL;
802 break;
803 case IPC_GLB_REPLY_INVALID_REQUEST:
804 trace_ipc_error("error: invalid request", header);
805 msg->errno = -EINVAL;
806 break;
807 case IPC_GLB_REPLY_ERROR_INVALID_PARAM:
808 trace_ipc_error("error: invalid parameter", header);
809 msg->errno = -EINVAL;
810 break;
811 default:
812 trace_ipc_error("error: unknown reply", header);
813 msg->errno = -EINVAL;
814 break;
815 }
816
817 /* update any stream states */
Paweł Piskorskid6e08612014-08-01 23:10:43 +0800818 if (msg_get_global_type(header) == IPC_GLB_STREAM_MESSAGE)
819 hsw_stream_update(hsw, msg);
Mark Browna4b12992014-03-12 23:04:35 +0000820
821 /* wake up and return the error if we have waiters on this message ? */
822 list_del(&msg->list);
823 tx_msg_reply_complete(hsw, msg);
824
825 return 1;
826}
827
Lu, Hane8e79ed2015-03-10 10:41:22 +0800828static int hsw_module_message(struct sst_hsw *hsw, u32 header)
829{
830 u32 operation, module_id;
831 int handled = 0;
832
833 operation = msg_get_module_operation(header);
834 module_id = msg_get_module_id(header);
835 dev_dbg(hsw->dev, "received module message header: 0x%8.8x\n",
836 header);
837 dev_dbg(hsw->dev, "operation: 0x%8.8x module_id: 0x%8.8x\n",
838 operation, module_id);
839
840 switch (operation) {
841 case IPC_MODULE_NOTIFICATION:
842 dev_dbg(hsw->dev, "module notification received");
843 handled = 1;
844 break;
845 default:
846 handled = hsw_process_reply(hsw, header);
847 break;
848 }
849
850 return handled;
851}
852
Mark Browna4b12992014-03-12 23:04:35 +0000853static int hsw_stream_message(struct sst_hsw *hsw, u32 header)
854{
855 u32 stream_msg, stream_id, stage_type;
856 struct sst_hsw_stream *stream;
857 int handled = 0;
858
859 stream_msg = msg_get_stream_type(header);
860 stream_id = msg_get_stream_id(header);
861 stage_type = msg_get_stage_type(header);
862
863 stream = get_stream_by_id(hsw, stream_id);
864 if (stream == NULL)
865 return handled;
866
867 stream->header = header;
868
869 switch (stream_msg) {
870 case IPC_STR_STAGE_MESSAGE:
871 dev_err(hsw->dev, "error: stage msg not implemented 0x%8.8x\n",
872 header);
873 break;
874 case IPC_STR_NOTIFICATION:
875 schedule_work(&stream->notify_work);
876 break;
877 default:
878 /* handle pending message complete request */
879 handled = hsw_process_reply(hsw, header);
880 break;
881 }
882
883 return handled;
884}
885
886static int hsw_log_message(struct sst_hsw *hsw, u32 header)
887{
888 u32 operation = (header & IPC_LOG_OP_MASK) >> IPC_LOG_OP_SHIFT;
889 struct sst_hsw_log_stream *stream = &hsw->log_stream;
890 int ret = 1;
891
892 if (operation != IPC_DEBUG_REQUEST_LOG_DUMP) {
893 dev_err(hsw->dev,
894 "error: log msg not implemented 0x%8.8x\n", header);
895 return 0;
896 }
897
898 mutex_lock(&stream->rw_mutex);
899 stream->last_pos = stream->curr_pos;
900 sst_dsp_inbox_read(
901 hsw->dsp, &stream->curr_pos, sizeof(stream->curr_pos));
902 mutex_unlock(&stream->rw_mutex);
903
904 schedule_work(&stream->notify_work);
905
906 return ret;
907}
908
909static int hsw_process_notification(struct sst_hsw *hsw)
910{
911 struct sst_dsp *sst = hsw->dsp;
912 u32 type, header;
913 int handled = 1;
914
915 header = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
916 type = msg_get_global_type(header);
917
918 trace_ipc_request("processing -->", header);
919
920 /* FW Ready is a special case */
921 if (!hsw->boot_complete && header & IPC_FW_READY) {
922 hsw_fw_ready(hsw, header);
923 return handled;
924 }
925
926 switch (type) {
927 case IPC_GLB_GET_FW_VERSION:
928 case IPC_GLB_ALLOCATE_STREAM:
929 case IPC_GLB_FREE_STREAM:
930 case IPC_GLB_GET_FW_CAPABILITIES:
931 case IPC_GLB_REQUEST_DUMP:
932 case IPC_GLB_GET_DEVICE_FORMATS:
933 case IPC_GLB_SET_DEVICE_FORMATS:
934 case IPC_GLB_ENTER_DX_STATE:
935 case IPC_GLB_GET_MIXER_STREAM_INFO:
936 case IPC_GLB_MAX_IPC_MESSAGE_TYPE:
937 case IPC_GLB_RESTORE_CONTEXT:
938 case IPC_GLB_SHORT_REPLY:
939 dev_err(hsw->dev, "error: message type %d header 0x%x\n",
940 type, header);
941 break;
942 case IPC_GLB_STREAM_MESSAGE:
943 handled = hsw_stream_message(hsw, header);
944 break;
945 case IPC_GLB_DEBUG_LOG_MESSAGE:
946 handled = hsw_log_message(hsw, header);
947 break;
Lu, Hane8e79ed2015-03-10 10:41:22 +0800948 case IPC_GLB_MODULE_OPERATION:
949 handled = hsw_module_message(hsw, header);
950 break;
Mark Browna4b12992014-03-12 23:04:35 +0000951 default:
952 dev_err(hsw->dev, "error: unexpected type %d hdr 0x%8.8x\n",
953 type, header);
954 break;
955 }
956
957 return handled;
958}
959
960static irqreturn_t hsw_irq_thread(int irq, void *context)
961{
962 struct sst_dsp *sst = (struct sst_dsp *) context;
963 struct sst_hsw *hsw = sst_dsp_get_thread_context(sst);
964 u32 ipcx, ipcd;
965 int handled;
966 unsigned long flags;
967
968 spin_lock_irqsave(&sst->spinlock, flags);
969
970 ipcx = sst_dsp_ipc_msg_rx(hsw->dsp);
971 ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
972
973 /* reply message from DSP */
974 if (ipcx & SST_IPCX_DONE) {
975
976 /* Handle Immediate reply from DSP Core */
977 handled = hsw_process_reply(hsw, ipcx);
978
979 if (handled > 0) {
980 /* clear DONE bit - tell DSP we have completed */
981 sst_dsp_shim_update_bits_unlocked(sst, SST_IPCX,
982 SST_IPCX_DONE, 0);
983
984 /* unmask Done interrupt */
985 sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
986 SST_IMRX_DONE, 0);
987 }
988 }
989
990 /* new message from DSP */
991 if (ipcd & SST_IPCD_BUSY) {
992
993 /* Handle Notification and Delayed reply from DSP Core */
994 handled = hsw_process_notification(hsw);
995
996 /* clear BUSY bit and set DONE bit - accept new messages */
997 if (handled > 0) {
998 sst_dsp_shim_update_bits_unlocked(sst, SST_IPCD,
999 SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
1000
1001 /* unmask busy interrupt */
1002 sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
1003 SST_IMRX_BUSY, 0);
1004 }
1005 }
1006
1007 spin_unlock_irqrestore(&sst->spinlock, flags);
1008
1009 /* continue to send any remaining messages... */
1010 queue_kthread_work(&hsw->kworker, &hsw->kwork);
1011
1012 return IRQ_HANDLED;
1013}
1014
1015int sst_hsw_fw_get_version(struct sst_hsw *hsw,
1016 struct sst_hsw_ipc_fw_version *version)
1017{
1018 int ret;
1019
1020 ret = ipc_tx_message_wait(hsw, IPC_GLB_TYPE(IPC_GLB_GET_FW_VERSION),
1021 NULL, 0, version, sizeof(*version));
1022 if (ret < 0)
1023 dev_err(hsw->dev, "error: get version failed\n");
1024
1025 return ret;
1026}
1027
1028/* Mixer Controls */
Mark Browna4b12992014-03-12 23:04:35 +00001029int sst_hsw_stream_get_volume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1030 u32 stage_id, u32 channel, u32 *volume)
1031{
1032 if (channel > 1)
1033 return -EINVAL;
1034
1035 sst_dsp_read(hsw->dsp, volume,
Christian Engelmayerbf657d22014-04-13 19:56:36 +02001036 stream->reply.volume_register_address[channel],
1037 sizeof(*volume));
Mark Browna4b12992014-03-12 23:04:35 +00001038
1039 return 0;
1040}
1041
Mark Browna4b12992014-03-12 23:04:35 +00001042/* stream volume */
1043int sst_hsw_stream_set_volume(struct sst_hsw *hsw,
1044 struct sst_hsw_stream *stream, u32 stage_id, u32 channel, u32 volume)
1045{
1046 struct sst_hsw_ipc_volume_req *req;
1047 u32 header;
1048 int ret;
1049
1050 trace_ipc_request("set stream volume", stream->reply.stream_hw_id);
1051
Jie Yangf1e59822014-11-25 21:00:53 +08001052 if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
Mark Browna4b12992014-03-12 23:04:35 +00001053 return -EINVAL;
1054
Mark Browna4b12992014-03-12 23:04:35 +00001055 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
1056 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
1057 header |= (stream->reply.stream_hw_id << IPC_STR_ID_SHIFT);
1058 header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
1059 header |= (stage_id << IPC_STG_ID_SHIFT);
1060
1061 req = &stream->vol_req;
Mark Browna4b12992014-03-12 23:04:35 +00001062 req->target_volume = volume;
1063
Jie Yangf1e59822014-11-25 21:00:53 +08001064 /* set both at same time ? */
1065 if (channel == SST_HSW_CHANNELS_ALL) {
1066 if (hsw->mute[0] && hsw->mute[1]) {
1067 hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
1068 return 0;
1069 } else if (hsw->mute[0])
1070 req->channel = 1;
1071 else if (hsw->mute[1])
1072 req->channel = 0;
1073 else
1074 req->channel = SST_HSW_CHANNELS_ALL;
1075 } else {
1076 /* set only 1 channel */
1077 if (hsw->mute[channel]) {
1078 hsw->mute_volume[channel] = volume;
1079 return 0;
1080 }
1081 req->channel = channel;
1082 }
1083
Mark Browna4b12992014-03-12 23:04:35 +00001084 ret = ipc_tx_message_wait(hsw, header, req, sizeof(*req), NULL, 0);
1085 if (ret < 0) {
1086 dev_err(hsw->dev, "error: set stream volume failed\n");
1087 return ret;
1088 }
1089
1090 return 0;
1091}
1092
Mark Browna4b12992014-03-12 23:04:35 +00001093int sst_hsw_mixer_get_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
1094 u32 *volume)
1095{
1096 if (channel > 1)
1097 return -EINVAL;
1098
1099 sst_dsp_read(hsw->dsp, volume,
1100 hsw->mixer_info.volume_register_address[channel],
1101 sizeof(*volume));
1102
1103 return 0;
1104}
1105
Mark Browna4b12992014-03-12 23:04:35 +00001106/* global mixer volume */
1107int sst_hsw_mixer_set_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
1108 u32 volume)
1109{
1110 struct sst_hsw_ipc_volume_req req;
1111 u32 header;
1112 int ret;
1113
1114 trace_ipc_request("set mixer volume", volume);
1115
Jie Yangf1e59822014-11-25 21:00:53 +08001116 if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
1117 return -EINVAL;
1118
Mark Browna4b12992014-03-12 23:04:35 +00001119 /* set both at same time ? */
Jie Yangf1e59822014-11-25 21:00:53 +08001120 if (channel == SST_HSW_CHANNELS_ALL) {
Mark Browna4b12992014-03-12 23:04:35 +00001121 if (hsw->mute[0] && hsw->mute[1]) {
1122 hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
1123 return 0;
1124 } else if (hsw->mute[0])
1125 req.channel = 1;
1126 else if (hsw->mute[1])
1127 req.channel = 0;
1128 else
Jie Yangf1e59822014-11-25 21:00:53 +08001129 req.channel = SST_HSW_CHANNELS_ALL;
Mark Browna4b12992014-03-12 23:04:35 +00001130 } else {
1131 /* set only 1 channel */
1132 if (hsw->mute[channel]) {
1133 hsw->mute_volume[channel] = volume;
1134 return 0;
1135 }
1136 req.channel = channel;
1137 }
1138
1139 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
1140 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
1141 header |= (hsw->mixer_info.mixer_hw_id << IPC_STR_ID_SHIFT);
1142 header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
1143 header |= (stage_id << IPC_STG_ID_SHIFT);
1144
1145 req.curve_duration = hsw->curve_duration;
1146 req.curve_type = hsw->curve_type;
1147 req.target_volume = volume;
1148
1149 ret = ipc_tx_message_wait(hsw, header, &req, sizeof(req), NULL, 0);
1150 if (ret < 0) {
1151 dev_err(hsw->dev, "error: set mixer volume failed\n");
1152 return ret;
1153 }
1154
1155 return 0;
1156}
1157
1158/* Stream API */
1159struct sst_hsw_stream *sst_hsw_stream_new(struct sst_hsw *hsw, int id,
1160 u32 (*notify_position)(struct sst_hsw_stream *stream, void *data),
1161 void *data)
1162{
1163 struct sst_hsw_stream *stream;
Wenkai Dud132cb02014-04-23 13:29:30 +03001164 struct sst_dsp *sst = hsw->dsp;
1165 unsigned long flags;
Mark Browna4b12992014-03-12 23:04:35 +00001166
1167 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1168 if (stream == NULL)
1169 return NULL;
1170
Wenkai Dud132cb02014-04-23 13:29:30 +03001171 spin_lock_irqsave(&sst->spinlock, flags);
Jie Yanga0a7c482015-01-12 17:17:34 +08001172 stream->reply.stream_hw_id = INVALID_STREAM_HW_ID;
Mark Browna4b12992014-03-12 23:04:35 +00001173 list_add(&stream->node, &hsw->stream_list);
1174 stream->notify_position = notify_position;
1175 stream->pdata = data;
1176 stream->hsw = hsw;
1177 stream->host_id = id;
1178
1179 /* work to process notification messages */
1180 INIT_WORK(&stream->notify_work, hsw_notification_work);
Wenkai Dud132cb02014-04-23 13:29:30 +03001181 spin_unlock_irqrestore(&sst->spinlock, flags);
Mark Browna4b12992014-03-12 23:04:35 +00001182
1183 return stream;
1184}
1185
1186int sst_hsw_stream_free(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1187{
1188 u32 header;
1189 int ret = 0;
Wenkai Dud132cb02014-04-23 13:29:30 +03001190 struct sst_dsp *sst = hsw->dsp;
1191 unsigned long flags;
Mark Browna4b12992014-03-12 23:04:35 +00001192
Jie Yangf81677b2015-01-07 22:07:05 +08001193 if (!stream) {
1194 dev_warn(hsw->dev, "warning: stream is NULL, no stream to free, ignore it.\n");
1195 return 0;
1196 }
1197
Mark Browna4b12992014-03-12 23:04:35 +00001198 /* dont free DSP streams that are not commited */
1199 if (!stream->commited)
1200 goto out;
1201
1202 trace_ipc_request("stream free", stream->host_id);
1203
1204 stream->free_req.stream_id = stream->reply.stream_hw_id;
1205 header = IPC_GLB_TYPE(IPC_GLB_FREE_STREAM);
1206
1207 ret = ipc_tx_message_wait(hsw, header, &stream->free_req,
1208 sizeof(stream->free_req), NULL, 0);
1209 if (ret < 0) {
1210 dev_err(hsw->dev, "error: free stream %d failed\n",
1211 stream->free_req.stream_id);
1212 return -EAGAIN;
1213 }
1214
1215 trace_hsw_stream_free_req(stream, &stream->free_req);
1216
1217out:
Jarkko Nikulade30a2c2014-04-24 10:34:36 +03001218 cancel_work_sync(&stream->notify_work);
Wenkai Dud132cb02014-04-23 13:29:30 +03001219 spin_lock_irqsave(&sst->spinlock, flags);
Mark Browna4b12992014-03-12 23:04:35 +00001220 list_del(&stream->node);
1221 kfree(stream);
Wenkai Dud132cb02014-04-23 13:29:30 +03001222 spin_unlock_irqrestore(&sst->spinlock, flags);
Mark Browna4b12992014-03-12 23:04:35 +00001223
1224 return ret;
1225}
1226
1227int sst_hsw_stream_set_bits(struct sst_hsw *hsw,
1228 struct sst_hsw_stream *stream, enum sst_hsw_bitdepth bits)
1229{
1230 if (stream->commited) {
1231 dev_err(hsw->dev, "error: stream committed for set bits\n");
1232 return -EINVAL;
1233 }
1234
1235 stream->request.format.bitdepth = bits;
1236 return 0;
1237}
1238
1239int sst_hsw_stream_set_channels(struct sst_hsw *hsw,
1240 struct sst_hsw_stream *stream, int channels)
1241{
1242 if (stream->commited) {
1243 dev_err(hsw->dev, "error: stream committed for set channels\n");
1244 return -EINVAL;
1245 }
1246
Mark Browna4b12992014-03-12 23:04:35 +00001247 stream->request.format.ch_num = channels;
1248 return 0;
1249}
1250
1251int sst_hsw_stream_set_rate(struct sst_hsw *hsw,
1252 struct sst_hsw_stream *stream, int rate)
1253{
1254 if (stream->commited) {
1255 dev_err(hsw->dev, "error: stream committed for set rate\n");
1256 return -EINVAL;
1257 }
1258
1259 stream->request.format.frequency = rate;
1260 return 0;
1261}
1262
1263int sst_hsw_stream_set_map_config(struct sst_hsw *hsw,
1264 struct sst_hsw_stream *stream, u32 map,
1265 enum sst_hsw_channel_config config)
1266{
1267 if (stream->commited) {
1268 dev_err(hsw->dev, "error: stream committed for set map\n");
1269 return -EINVAL;
1270 }
1271
1272 stream->request.format.map = map;
1273 stream->request.format.config = config;
1274 return 0;
1275}
1276
1277int sst_hsw_stream_set_style(struct sst_hsw *hsw,
1278 struct sst_hsw_stream *stream, enum sst_hsw_interleaving style)
1279{
1280 if (stream->commited) {
1281 dev_err(hsw->dev, "error: stream committed for set style\n");
1282 return -EINVAL;
1283 }
1284
1285 stream->request.format.style = style;
1286 return 0;
1287}
1288
1289int sst_hsw_stream_set_valid(struct sst_hsw *hsw,
1290 struct sst_hsw_stream *stream, u32 bits)
1291{
1292 if (stream->commited) {
1293 dev_err(hsw->dev, "error: stream committed for set valid bits\n");
1294 return -EINVAL;
1295 }
1296
1297 stream->request.format.valid_bit = bits;
1298 return 0;
1299}
1300
1301/* Stream Configuration */
1302int sst_hsw_stream_format(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1303 enum sst_hsw_stream_path_id path_id,
1304 enum sst_hsw_stream_type stream_type,
1305 enum sst_hsw_stream_format format_id)
1306{
1307 if (stream->commited) {
1308 dev_err(hsw->dev, "error: stream committed for set format\n");
1309 return -EINVAL;
1310 }
1311
1312 stream->request.path_id = path_id;
1313 stream->request.stream_type = stream_type;
1314 stream->request.format_id = format_id;
1315
1316 trace_hsw_stream_alloc_request(stream, &stream->request);
1317
1318 return 0;
1319}
1320
1321int sst_hsw_stream_buffer(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1322 u32 ring_pt_address, u32 num_pages,
1323 u32 ring_size, u32 ring_offset, u32 ring_first_pfn)
1324{
1325 if (stream->commited) {
1326 dev_err(hsw->dev, "error: stream committed for buffer\n");
1327 return -EINVAL;
1328 }
1329
1330 stream->request.ringinfo.ring_pt_address = ring_pt_address;
1331 stream->request.ringinfo.num_pages = num_pages;
1332 stream->request.ringinfo.ring_size = ring_size;
1333 stream->request.ringinfo.ring_offset = ring_offset;
1334 stream->request.ringinfo.ring_first_pfn = ring_first_pfn;
1335
1336 trace_hsw_stream_buffer(stream);
1337
1338 return 0;
1339}
1340
1341int sst_hsw_stream_set_module_info(struct sst_hsw *hsw,
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001342 struct sst_hsw_stream *stream, struct sst_module_runtime *runtime)
Mark Browna4b12992014-03-12 23:04:35 +00001343{
1344 struct sst_hsw_module_map *map = &stream->request.map;
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001345 struct sst_dsp *dsp = sst_hsw_get_dsp(hsw);
1346 struct sst_module *module = runtime->module;
Mark Browna4b12992014-03-12 23:04:35 +00001347
1348 if (stream->commited) {
1349 dev_err(hsw->dev, "error: stream committed for set module\n");
1350 return -EINVAL;
1351 }
1352
1353 /* only support initial module atm */
1354 map->module_entries_count = 1;
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001355 map->module_entries[0].module_id = module->id;
1356 map->module_entries[0].entry_point = module->entry;
Mark Browna4b12992014-03-12 23:04:35 +00001357
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001358 stream->request.persistent_mem.offset =
1359 sst_dsp_get_offset(dsp, runtime->persistent_offset, SST_MEM_DRAM);
1360 stream->request.persistent_mem.size = module->persistent_size;
Mark Browna4b12992014-03-12 23:04:35 +00001361
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001362 stream->request.scratch_mem.offset =
1363 sst_dsp_get_offset(dsp, dsp->scratch_offset, SST_MEM_DRAM);
1364 stream->request.scratch_mem.size = dsp->scratch_size;
Mark Browna4b12992014-03-12 23:04:35 +00001365
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001366 dev_dbg(hsw->dev, "module %d runtime %d using:\n", module->id,
1367 runtime->id);
1368 dev_dbg(hsw->dev, " persistent offset 0x%x bytes 0x%x\n",
1369 stream->request.persistent_mem.offset,
1370 stream->request.persistent_mem.size);
1371 dev_dbg(hsw->dev, " scratch offset 0x%x bytes 0x%x\n",
1372 stream->request.scratch_mem.offset,
1373 stream->request.scratch_mem.size);
Mark Browna4b12992014-03-12 23:04:35 +00001374
1375 return 0;
1376}
1377
1378int sst_hsw_stream_commit(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1379{
1380 struct sst_hsw_ipc_stream_alloc_req *str_req = &stream->request;
1381 struct sst_hsw_ipc_stream_alloc_reply *reply = &stream->reply;
1382 u32 header;
1383 int ret;
1384
Jie Yangf81677b2015-01-07 22:07:05 +08001385 if (!stream) {
1386 dev_warn(hsw->dev, "warning: stream is NULL, no stream to commit, ignore it.\n");
1387 return 0;
1388 }
1389
1390 if (stream->commited) {
1391 dev_warn(hsw->dev, "warning: stream is already committed, ignore it.\n");
1392 return 0;
1393 }
1394
Mark Browna4b12992014-03-12 23:04:35 +00001395 trace_ipc_request("stream alloc", stream->host_id);
1396
1397 header = IPC_GLB_TYPE(IPC_GLB_ALLOCATE_STREAM);
1398
1399 ret = ipc_tx_message_wait(hsw, header, str_req, sizeof(*str_req),
1400 reply, sizeof(*reply));
1401 if (ret < 0) {
1402 dev_err(hsw->dev, "error: stream commit failed\n");
1403 return ret;
1404 }
1405
1406 stream->commited = 1;
1407 trace_hsw_stream_alloc_reply(stream);
1408
1409 return 0;
1410}
1411
Libin Yang1b006992015-02-10 10:02:47 +08001412snd_pcm_uframes_t sst_hsw_stream_get_old_position(struct sst_hsw *hsw,
1413 struct sst_hsw_stream *stream)
1414{
1415 return stream->old_position;
1416}
1417
1418void sst_hsw_stream_set_old_position(struct sst_hsw *hsw,
1419 struct sst_hsw_stream *stream, snd_pcm_uframes_t val)
1420{
1421 stream->old_position = val;
1422}
1423
1424bool sst_hsw_stream_get_silence_start(struct sst_hsw *hsw,
1425 struct sst_hsw_stream *stream)
1426{
1427 return stream->play_silence;
1428}
1429
1430void sst_hsw_stream_set_silence_start(struct sst_hsw *hsw,
1431 struct sst_hsw_stream *stream, bool val)
1432{
1433 stream->play_silence = val;
1434}
1435
Mark Browna4b12992014-03-12 23:04:35 +00001436/* Stream Information - these calls could be inline but we want the IPC
1437 ABI to be opaque to client PCM drivers to cope with any future ABI changes */
Mark Browna4b12992014-03-12 23:04:35 +00001438int sst_hsw_mixer_get_info(struct sst_hsw *hsw)
1439{
1440 struct sst_hsw_ipc_stream_info_reply *reply;
1441 u32 header;
1442 int ret;
1443
1444 reply = &hsw->mixer_info;
1445 header = IPC_GLB_TYPE(IPC_GLB_GET_MIXER_STREAM_INFO);
1446
1447 trace_ipc_request("get global mixer info", 0);
1448
1449 ret = ipc_tx_message_wait(hsw, header, NULL, 0, reply, sizeof(*reply));
1450 if (ret < 0) {
1451 dev_err(hsw->dev, "error: get stream info failed\n");
1452 return ret;
1453 }
1454
1455 trace_hsw_mixer_info_reply(reply);
1456
1457 return 0;
1458}
1459
1460/* Send stream command */
1461static int sst_hsw_stream_operations(struct sst_hsw *hsw, int type,
1462 int stream_id, int wait)
1463{
1464 u32 header;
1465
1466 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) | IPC_STR_TYPE(type);
1467 header |= (stream_id << IPC_STR_ID_SHIFT);
1468
1469 if (wait)
1470 return ipc_tx_message_wait(hsw, header, NULL, 0, NULL, 0);
1471 else
1472 return ipc_tx_message_nowait(hsw, header, NULL, 0);
1473}
1474
1475/* Stream ALSA trigger operations */
1476int sst_hsw_stream_pause(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1477 int wait)
1478{
1479 int ret;
1480
Jie Yangf81677b2015-01-07 22:07:05 +08001481 if (!stream) {
1482 dev_warn(hsw->dev, "warning: stream is NULL, no stream to pause, ignore it.\n");
1483 return 0;
1484 }
1485
Mark Browna4b12992014-03-12 23:04:35 +00001486 trace_ipc_request("stream pause", stream->reply.stream_hw_id);
1487
1488 ret = sst_hsw_stream_operations(hsw, IPC_STR_PAUSE,
1489 stream->reply.stream_hw_id, wait);
1490 if (ret < 0)
1491 dev_err(hsw->dev, "error: failed to pause stream %d\n",
1492 stream->reply.stream_hw_id);
1493
1494 return ret;
1495}
1496
1497int sst_hsw_stream_resume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1498 int wait)
1499{
1500 int ret;
1501
Jie Yangf81677b2015-01-07 22:07:05 +08001502 if (!stream) {
1503 dev_warn(hsw->dev, "warning: stream is NULL, no stream to resume, ignore it.\n");
1504 return 0;
1505 }
1506
Mark Browna4b12992014-03-12 23:04:35 +00001507 trace_ipc_request("stream resume", stream->reply.stream_hw_id);
1508
1509 ret = sst_hsw_stream_operations(hsw, IPC_STR_RESUME,
1510 stream->reply.stream_hw_id, wait);
1511 if (ret < 0)
1512 dev_err(hsw->dev, "error: failed to resume stream %d\n",
1513 stream->reply.stream_hw_id);
1514
1515 return ret;
1516}
1517
1518int sst_hsw_stream_reset(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1519{
1520 int ret, tries = 10;
1521
Jie Yangf81677b2015-01-07 22:07:05 +08001522 if (!stream) {
1523 dev_warn(hsw->dev, "warning: stream is NULL, no stream to reset, ignore it.\n");
1524 return 0;
1525 }
1526
Mark Browna4b12992014-03-12 23:04:35 +00001527 /* dont reset streams that are not commited */
1528 if (!stream->commited)
1529 return 0;
1530
1531 /* wait for pause to complete before we reset the stream */
1532 while (stream->running && tries--)
1533 msleep(1);
1534 if (!tries) {
1535 dev_err(hsw->dev, "error: reset stream %d still running\n",
1536 stream->reply.stream_hw_id);
1537 return -EINVAL;
1538 }
1539
1540 trace_ipc_request("stream reset", stream->reply.stream_hw_id);
1541
1542 ret = sst_hsw_stream_operations(hsw, IPC_STR_RESET,
1543 stream->reply.stream_hw_id, 1);
1544 if (ret < 0)
1545 dev_err(hsw->dev, "error: failed to reset stream %d\n",
1546 stream->reply.stream_hw_id);
1547 return ret;
1548}
1549
1550/* Stream pointer positions */
Liam Girdwood51b4e242014-05-02 16:56:33 +01001551u32 sst_hsw_get_dsp_position(struct sst_hsw *hsw,
Mark Browna4b12992014-03-12 23:04:35 +00001552 struct sst_hsw_stream *stream)
1553{
Liam Girdwood51b4e242014-05-02 16:56:33 +01001554 u32 rpos;
1555
1556 sst_dsp_read(hsw->dsp, &rpos,
1557 stream->reply.read_position_register_address, sizeof(rpos));
1558
1559 return rpos;
1560}
1561
1562/* Stream presentation (monotonic) positions */
1563u64 sst_hsw_get_dsp_presentation_position(struct sst_hsw *hsw,
1564 struct sst_hsw_stream *stream)
1565{
1566 u64 ppos;
1567
1568 sst_dsp_read(hsw->dsp, &ppos,
1569 stream->reply.presentation_position_register_address,
1570 sizeof(ppos));
1571
1572 return ppos;
Mark Browna4b12992014-03-12 23:04:35 +00001573}
1574
Mark Browna4b12992014-03-12 23:04:35 +00001575/* physical BE config */
1576int sst_hsw_device_set_config(struct sst_hsw *hsw,
1577 enum sst_hsw_device_id dev, enum sst_hsw_device_mclk mclk,
1578 enum sst_hsw_device_mode mode, u32 clock_divider)
1579{
1580 struct sst_hsw_ipc_device_config_req config;
1581 u32 header;
1582 int ret;
1583
1584 trace_ipc_request("set device config", dev);
1585
1586 config.ssp_interface = dev;
1587 config.clock_frequency = mclk;
1588 config.mode = mode;
1589 config.clock_divider = clock_divider;
Liam Girdwoodf07e51c2014-10-16 15:29:15 +01001590 if (mode == SST_HSW_DEVICE_TDM_CLOCK_MASTER)
1591 config.channels = 4;
1592 else
1593 config.channels = 2;
Mark Browna4b12992014-03-12 23:04:35 +00001594
1595 trace_hsw_device_config_req(&config);
1596
1597 header = IPC_GLB_TYPE(IPC_GLB_SET_DEVICE_FORMATS);
1598
1599 ret = ipc_tx_message_wait(hsw, header, &config, sizeof(config),
1600 NULL, 0);
1601 if (ret < 0)
1602 dev_err(hsw->dev, "error: set device formats failed\n");
1603
1604 return ret;
1605}
1606EXPORT_SYMBOL_GPL(sst_hsw_device_set_config);
1607
1608/* DX Config */
1609int sst_hsw_dx_set_state(struct sst_hsw *hsw,
1610 enum sst_hsw_dx_state state, struct sst_hsw_ipc_dx_reply *dx)
1611{
1612 u32 header, state_;
Liam Girdwood543ec632014-07-30 20:11:26 +08001613 int ret, item;
Mark Browna4b12992014-03-12 23:04:35 +00001614
1615 header = IPC_GLB_TYPE(IPC_GLB_ENTER_DX_STATE);
1616 state_ = state;
1617
1618 trace_ipc_request("PM enter Dx state", state);
1619
1620 ret = ipc_tx_message_wait(hsw, header, &state_, sizeof(state_),
Dan Carpenter7897ab72014-04-16 18:38:11 +03001621 dx, sizeof(*dx));
Mark Browna4b12992014-03-12 23:04:35 +00001622 if (ret < 0) {
1623 dev_err(hsw->dev, "ipc: error set dx state %d failed\n", state);
1624 return ret;
1625 }
1626
Liam Girdwood543ec632014-07-30 20:11:26 +08001627 for (item = 0; item < dx->entries_no; item++) {
1628 dev_dbg(hsw->dev,
1629 "Item[%d] offset[%x] - size[%x] - source[%x]\n",
1630 item, dx->mem_info[item].offset,
1631 dx->mem_info[item].size,
1632 dx->mem_info[item].source);
1633 }
Mark Browna4b12992014-03-12 23:04:35 +00001634 dev_dbg(hsw->dev, "ipc: got %d entry numbers for state %d\n",
1635 dx->entries_no, state);
1636
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001637 return ret;
Mark Browna4b12992014-03-12 23:04:35 +00001638}
1639
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001640struct sst_module_runtime *sst_hsw_runtime_module_create(struct sst_hsw *hsw,
1641 int mod_id, int offset)
Mark Browna4b12992014-03-12 23:04:35 +00001642{
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001643 struct sst_dsp *dsp = hsw->dsp;
1644 struct sst_module *module;
1645 struct sst_module_runtime *runtime;
1646 int err;
Mark Browna4b12992014-03-12 23:04:35 +00001647
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001648 module = sst_module_get_from_id(dsp, mod_id);
1649 if (module == NULL) {
1650 dev_err(dsp->dev, "error: failed to get module %d for pcm\n",
1651 mod_id);
1652 return NULL;
1653 }
Mark Browna4b12992014-03-12 23:04:35 +00001654
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001655 runtime = sst_module_runtime_new(module, mod_id, NULL);
1656 if (runtime == NULL) {
1657 dev_err(dsp->dev, "error: failed to create module %d runtime\n",
1658 mod_id);
1659 return NULL;
1660 }
Mark Browna4b12992014-03-12 23:04:35 +00001661
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001662 err = sst_module_runtime_alloc_blocks(runtime, offset);
1663 if (err < 0) {
1664 dev_err(dsp->dev, "error: failed to alloc blocks for module %d runtime\n",
1665 mod_id);
1666 sst_module_runtime_free(runtime);
1667 return NULL;
1668 }
Mark Browna4b12992014-03-12 23:04:35 +00001669
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001670 dev_dbg(dsp->dev, "runtime id %d created for module %d\n", runtime->id,
1671 mod_id);
1672 return runtime;
1673}
Mark Browna4b12992014-03-12 23:04:35 +00001674
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001675void sst_hsw_runtime_module_free(struct sst_module_runtime *runtime)
1676{
1677 sst_module_runtime_free_blocks(runtime);
1678 sst_module_runtime_free(runtime);
Mark Browna4b12992014-03-12 23:04:35 +00001679}
1680
Liam Girdwood35e03a82014-10-30 14:58:19 +00001681#ifdef CONFIG_PM
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001682static int sst_hsw_dx_state_dump(struct sst_hsw *hsw)
1683{
1684 struct sst_dsp *sst = hsw->dsp;
1685 u32 item, offset, size;
1686 int ret = 0;
1687
1688 trace_ipc_request("PM state dump. Items #", SST_HSW_MAX_DX_REGIONS);
1689
1690 if (hsw->dx.entries_no > SST_HSW_MAX_DX_REGIONS) {
1691 dev_err(hsw->dev,
1692 "error: number of FW context regions greater than %d\n",
1693 SST_HSW_MAX_DX_REGIONS);
1694 memset(&hsw->dx, 0, sizeof(hsw->dx));
1695 return -EINVAL;
1696 }
1697
1698 ret = sst_dsp_dma_get_channel(sst, 0);
1699 if (ret < 0) {
1700 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1701 return ret;
1702 }
1703
1704 /* set on-demond mode on engine 0 channel 3 */
1705 sst_dsp_shim_update_bits(sst, SST_HMDC,
1706 SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH,
1707 SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH);
1708
1709 for (item = 0; item < hsw->dx.entries_no; item++) {
1710 if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
1711 && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
1712 && hsw->dx.mem_info[item].offset <
1713 DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
1714
1715 offset = hsw->dx.mem_info[item].offset
1716 - DSP_DRAM_ADDR_OFFSET;
1717 size = (hsw->dx.mem_info[item].size + 3) & (~3);
1718
1719 ret = sst_dsp_dma_copyfrom(sst, hsw->dx_context_paddr + offset,
1720 sst->addr.lpe_base + offset, size);
1721 if (ret < 0) {
1722 dev_err(hsw->dev,
1723 "error: FW context dump failed\n");
1724 memset(&hsw->dx, 0, sizeof(hsw->dx));
1725 goto out;
1726 }
1727 }
1728 }
1729
1730out:
1731 sst_dsp_dma_put_channel(sst);
1732 return ret;
1733}
1734
1735static int sst_hsw_dx_state_restore(struct sst_hsw *hsw)
1736{
1737 struct sst_dsp *sst = hsw->dsp;
1738 u32 item, offset, size;
1739 int ret;
1740
1741 for (item = 0; item < hsw->dx.entries_no; item++) {
1742 if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
1743 && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
1744 && hsw->dx.mem_info[item].offset <
1745 DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
1746
1747 offset = hsw->dx.mem_info[item].offset
1748 - DSP_DRAM_ADDR_OFFSET;
1749 size = (hsw->dx.mem_info[item].size + 3) & (~3);
1750
1751 ret = sst_dsp_dma_copyto(sst, sst->addr.lpe_base + offset,
1752 hsw->dx_context_paddr + offset, size);
1753 if (ret < 0) {
1754 dev_err(hsw->dev,
1755 "error: FW context restore failed\n");
1756 return ret;
1757 }
1758 }
1759 }
1760
1761 return 0;
1762}
1763
1764static void sst_hsw_drop_all(struct sst_hsw *hsw)
1765{
1766 struct ipc_message *msg, *tmp;
1767 unsigned long flags;
1768 int tx_drop_cnt = 0, rx_drop_cnt = 0;
1769
1770 /* drop all TX and Rx messages before we stall + reset DSP */
1771 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
1772
1773 list_for_each_entry_safe(msg, tmp, &hsw->tx_list, list) {
1774 list_move(&msg->list, &hsw->empty_list);
1775 tx_drop_cnt++;
1776 }
1777
1778 list_for_each_entry_safe(msg, tmp, &hsw->rx_list, list) {
1779 list_move(&msg->list, &hsw->empty_list);
1780 rx_drop_cnt++;
1781 }
1782
1783 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
1784
1785 if (tx_drop_cnt || rx_drop_cnt)
1786 dev_err(hsw->dev, "dropped IPC msg RX=%d, TX=%d\n",
1787 tx_drop_cnt, rx_drop_cnt);
1788}
1789
1790int sst_hsw_dsp_load(struct sst_hsw *hsw)
1791{
1792 struct sst_dsp *dsp = hsw->dsp;
Lu, Han3fe06072015-02-25 08:26:21 +08001793 struct sst_fw *sst_fw, *t;
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001794 int ret;
1795
1796 dev_dbg(hsw->dev, "loading audio DSP....");
1797
1798 ret = sst_dsp_wake(dsp);
1799 if (ret < 0) {
1800 dev_err(hsw->dev, "error: failed to wake audio DSP\n");
1801 return -ENODEV;
1802 }
1803
1804 ret = sst_dsp_dma_get_channel(dsp, 0);
1805 if (ret < 0) {
1806 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1807 return ret;
1808 }
1809
Lu, Han3fe06072015-02-25 08:26:21 +08001810 list_for_each_entry_safe_reverse(sst_fw, t, &dsp->fw_list, list) {
1811 ret = sst_fw_reload(sst_fw);
1812 if (ret < 0) {
1813 dev_err(hsw->dev, "error: SST FW reload failed\n");
1814 sst_dsp_dma_put_channel(dsp);
1815 return -ENOMEM;
1816 }
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001817 }
Lu, Han3fe06072015-02-25 08:26:21 +08001818 ret = sst_block_alloc_scratch(hsw->dsp);
1819 if (ret < 0)
1820 return -EINVAL;
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001821
1822 sst_dsp_dma_put_channel(dsp);
1823 return 0;
1824}
1825
1826static int sst_hsw_dsp_restore(struct sst_hsw *hsw)
1827{
1828 struct sst_dsp *dsp = hsw->dsp;
1829 int ret;
1830
1831 dev_dbg(hsw->dev, "restoring audio DSP....");
1832
1833 ret = sst_dsp_dma_get_channel(dsp, 0);
1834 if (ret < 0) {
1835 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1836 return ret;
1837 }
1838
1839 ret = sst_hsw_dx_state_restore(hsw);
1840 if (ret < 0) {
1841 dev_err(hsw->dev, "error: SST FW context restore failed\n");
1842 sst_dsp_dma_put_channel(dsp);
1843 return -ENOMEM;
1844 }
1845 sst_dsp_dma_put_channel(dsp);
1846
1847 /* wait for DSP boot completion */
1848 sst_dsp_boot(dsp);
1849
1850 return ret;
1851}
1852
1853int sst_hsw_dsp_runtime_suspend(struct sst_hsw *hsw)
1854{
1855 int ret;
1856
1857 dev_dbg(hsw->dev, "audio dsp runtime suspend\n");
1858
1859 ret = sst_hsw_dx_set_state(hsw, SST_HSW_DX_STATE_D3, &hsw->dx);
1860 if (ret < 0)
1861 return ret;
1862
1863 sst_dsp_stall(hsw->dsp);
1864
1865 ret = sst_hsw_dx_state_dump(hsw);
1866 if (ret < 0)
1867 return ret;
1868
1869 sst_hsw_drop_all(hsw);
1870
1871 return 0;
1872}
1873
1874int sst_hsw_dsp_runtime_sleep(struct sst_hsw *hsw)
1875{
Lu, Han3fe06072015-02-25 08:26:21 +08001876 struct sst_fw *sst_fw, *t;
1877 struct sst_dsp *dsp = hsw->dsp;
1878
1879 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
1880 sst_fw_unload(sst_fw);
1881 }
1882 sst_block_free_scratch(dsp);
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001883
1884 hsw->boot_complete = false;
1885
Lu, Han3fe06072015-02-25 08:26:21 +08001886 sst_dsp_sleep(dsp);
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001887
1888 return 0;
1889}
1890
1891int sst_hsw_dsp_runtime_resume(struct sst_hsw *hsw)
1892{
1893 struct device *dev = hsw->dev;
1894 int ret;
1895
1896 dev_dbg(dev, "audio dsp runtime resume\n");
1897
1898 if (hsw->boot_complete)
1899 return 1; /* tell caller no action is required */
1900
1901 ret = sst_hsw_dsp_restore(hsw);
1902 if (ret < 0)
1903 dev_err(dev, "error: audio DSP boot failure\n");
1904
Lu, Han9449d392015-03-10 10:41:20 +08001905 sst_hsw_init_module_state(hsw);
1906
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001907 ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
1908 msecs_to_jiffies(IPC_BOOT_MSECS));
1909 if (ret == 0) {
Liam Girdwoodb891f622014-10-30 14:34:00 +00001910 dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
1911 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
1912 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001913 return -EIO;
1914 }
1915
1916 /* Set ADSP SSP port settings */
1917 ret = sst_hsw_device_set_config(hsw, SST_HSW_DEVICE_SSP_0,
1918 SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
1919 SST_HSW_DEVICE_CLOCK_MASTER, 9);
1920 if (ret < 0)
1921 dev_err(dev, "error: SSP re-initialization failed\n");
1922
1923 return ret;
1924}
1925#endif
1926
Mark Browna4b12992014-03-12 23:04:35 +00001927static int msg_empty_list_init(struct sst_hsw *hsw)
1928{
1929 int i;
1930
1931 hsw->msg = kzalloc(sizeof(struct ipc_message) *
1932 IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
1933 if (hsw->msg == NULL)
1934 return -ENOMEM;
1935
1936 for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
1937 init_waitqueue_head(&hsw->msg[i].waitq);
1938 list_add(&hsw->msg[i].list, &hsw->empty_list);
1939 }
1940
1941 return 0;
1942}
1943
Mark Browna4b12992014-03-12 23:04:35 +00001944struct sst_dsp *sst_hsw_get_dsp(struct sst_hsw *hsw)
1945{
1946 return hsw->dsp;
1947}
1948
Lu, Han9449d392015-03-10 10:41:20 +08001949void sst_hsw_init_module_state(struct sst_hsw *hsw)
1950{
1951 struct sst_module *module;
1952 enum sst_hsw_module_id id;
1953
1954 /* the base fw contains several modules */
1955 for (id = SST_HSW_MODULE_BASE_FW; id < SST_HSW_MAX_MODULE_ID; id++) {
1956 module = sst_module_get_from_id(hsw->dsp, id);
Lu, Han8c43fc22015-03-10 10:41:21 +08001957 if (module) {
1958 /* module waves is active only after being enabled */
1959 if (id == SST_HSW_MODULE_WAVES)
1960 module->state = SST_MODULE_STATE_INITIALIZED;
1961 else
1962 module->state = SST_MODULE_STATE_ACTIVE;
1963 }
Lu, Han9449d392015-03-10 10:41:20 +08001964 }
1965}
1966
Lu, Han8c43fc22015-03-10 10:41:21 +08001967bool sst_hsw_is_module_loaded(struct sst_hsw *hsw, u32 module_id)
1968{
1969 struct sst_module *module;
1970
1971 module = sst_module_get_from_id(hsw->dsp, module_id);
1972 if (module == NULL || module->state == SST_MODULE_STATE_UNLOADED)
1973 return false;
1974 else
1975 return true;
1976}
1977
Lu, Hane8e79ed2015-03-10 10:41:22 +08001978bool sst_hsw_is_module_active(struct sst_hsw *hsw, u32 module_id)
1979{
1980 struct sst_module *module;
1981
1982 module = sst_module_get_from_id(hsw->dsp, module_id);
1983 if (module != NULL && module->state == SST_MODULE_STATE_ACTIVE)
1984 return true;
1985 else
1986 return false;
1987}
1988
Lu, Han9449d392015-03-10 10:41:20 +08001989int sst_hsw_module_load(struct sst_hsw *hsw,
1990 u32 module_id, u32 instance_id, char *name)
1991{
1992 int ret = 0;
1993 const struct firmware *fw = NULL;
1994 struct sst_fw *hsw_sst_fw;
1995 struct sst_module *module;
1996 struct device *dev = hsw->dev;
1997 struct sst_dsp *dsp = hsw->dsp;
1998
1999 dev_dbg(dev, "sst_hsw_module_load id=%d, name='%s'", module_id, name);
2000
2001 module = sst_module_get_from_id(dsp, module_id);
2002 if (module == NULL) {
2003 /* loading for the first time */
2004 if (module_id == SST_HSW_MODULE_BASE_FW) {
2005 /* for base module: use fw requested in acpi probe */
2006 fw = dsp->pdata->fw;
2007 if (!fw) {
2008 dev_err(dev, "request Base fw failed\n");
2009 return -ENODEV;
2010 }
2011 } else {
2012 /* try and load any other optional modules if they are
2013 * available. Use dev_info instead of dev_err in case
2014 * request firmware failed */
2015 ret = request_firmware(&fw, name, dev);
2016 if (ret) {
2017 dev_info(dev, "fw image %s not available(%d)\n",
2018 name, ret);
2019 return ret;
2020 }
2021 }
2022 hsw_sst_fw = sst_fw_new(dsp, fw, hsw);
2023 if (hsw_sst_fw == NULL) {
2024 dev_err(dev, "error: failed to load firmware\n");
2025 ret = -ENOMEM;
2026 goto out;
2027 }
2028 module = sst_module_get_from_id(dsp, module_id);
2029 if (module == NULL) {
2030 dev_err(dev, "error: no module %d in firmware %s\n",
2031 module_id, name);
2032 }
2033 } else
2034 dev_info(dev, "module %d (%s) already loaded\n",
2035 module_id, name);
2036out:
2037 /* release fw, but base fw should be released by acpi driver */
2038 if (fw && module_id != SST_HSW_MODULE_BASE_FW)
2039 release_firmware(fw);
2040
2041 return ret;
2042}
2043
Lu, Hane8e79ed2015-03-10 10:41:22 +08002044int sst_hsw_module_enable(struct sst_hsw *hsw,
2045 u32 module_id, u32 instance_id)
2046{
2047 int ret;
2048 u32 header = 0;
2049 struct sst_hsw_ipc_module_config config;
2050 struct sst_module *module;
2051 struct sst_module_runtime *runtime;
2052 struct device *dev = hsw->dev;
2053 struct sst_dsp *dsp = hsw->dsp;
2054
2055 if (!sst_hsw_is_module_loaded(hsw, module_id)) {
2056 dev_dbg(dev, "module %d not loaded\n", module_id);
2057 return 0;
2058 }
2059
2060 if (sst_hsw_is_module_active(hsw, module_id)) {
2061 dev_info(dev, "module %d already enabled\n", module_id);
2062 return 0;
2063 }
2064
2065 module = sst_module_get_from_id(dsp, module_id);
2066 if (module == NULL) {
2067 dev_err(dev, "module %d not valid\n", module_id);
2068 return -ENXIO;
2069 }
2070
2071 runtime = sst_module_runtime_get_from_id(module, module_id);
2072 if (runtime == NULL) {
2073 dev_err(dev, "runtime %d not valid", module_id);
2074 return -ENXIO;
2075 }
2076
2077 header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
2078 IPC_MODULE_OPERATION(IPC_MODULE_ENABLE) |
2079 IPC_MODULE_ID(module_id);
2080 dev_dbg(dev, "module enable header: %x\n", header);
2081
2082 config.map.module_entries_count = 1;
2083 config.map.module_entries[0].module_id = module->id;
2084 config.map.module_entries[0].entry_point = module->entry;
2085
2086 config.persistent_mem.offset =
2087 sst_dsp_get_offset(dsp,
2088 runtime->persistent_offset, SST_MEM_DRAM);
2089 config.persistent_mem.size = module->persistent_size;
2090
2091 config.scratch_mem.offset =
2092 sst_dsp_get_offset(dsp,
2093 dsp->scratch_offset, SST_MEM_DRAM);
2094 config.scratch_mem.size = module->scratch_size;
2095 dev_dbg(dev, "mod %d enable p:%d @ %x, s:%d @ %x, ep: %x",
2096 config.map.module_entries[0].module_id,
2097 config.persistent_mem.size,
2098 config.persistent_mem.offset,
2099 config.scratch_mem.size, config.scratch_mem.offset,
2100 config.map.module_entries[0].entry_point);
2101
2102 ret = ipc_tx_message_wait(hsw, header,
2103 &config, sizeof(config), NULL, 0);
2104 if (ret < 0)
2105 dev_err(dev, "ipc: module enable failed - %d\n", ret);
2106 else
2107 module->state = SST_MODULE_STATE_ACTIVE;
2108
2109 return ret;
2110}
2111
2112int sst_hsw_module_disable(struct sst_hsw *hsw,
2113 u32 module_id, u32 instance_id)
2114{
2115 int ret;
2116 u32 header;
2117 struct sst_module *module;
2118 struct device *dev = hsw->dev;
2119 struct sst_dsp *dsp = hsw->dsp;
2120
2121 if (!sst_hsw_is_module_loaded(hsw, module_id)) {
2122 dev_dbg(dev, "module %d not loaded\n", module_id);
2123 return 0;
2124 }
2125
2126 if (!sst_hsw_is_module_active(hsw, module_id)) {
2127 dev_info(dev, "module %d already disabled\n", module_id);
2128 return 0;
2129 }
2130
2131 module = sst_module_get_from_id(dsp, module_id);
2132 if (module == NULL) {
2133 dev_err(dev, "module %d not valid\n", module_id);
2134 return -ENXIO;
2135 }
2136
2137 header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
2138 IPC_MODULE_OPERATION(IPC_MODULE_DISABLE) |
2139 IPC_MODULE_ID(module_id);
2140
2141 ret = ipc_tx_message_wait(hsw, header, NULL, 0, NULL, 0);
2142 if (ret < 0)
2143 dev_err(dev, "module disable failed - %d\n", ret);
2144 else
2145 module->state = SST_MODULE_STATE_INITIALIZED;
2146
2147 return ret;
2148}
2149
Mark Browna4b12992014-03-12 23:04:35 +00002150static struct sst_dsp_device hsw_dev = {
2151 .thread = hsw_irq_thread,
2152 .ops = &haswell_ops,
2153};
2154
2155int sst_hsw_dsp_init(struct device *dev, struct sst_pdata *pdata)
2156{
2157 struct sst_hsw_ipc_fw_version version;
2158 struct sst_hsw *hsw;
Mark Browna4b12992014-03-12 23:04:35 +00002159 int ret;
2160
2161 dev_dbg(dev, "initialising Audio DSP IPC\n");
2162
2163 hsw = devm_kzalloc(dev, sizeof(*hsw), GFP_KERNEL);
2164 if (hsw == NULL)
2165 return -ENOMEM;
2166
2167 hsw->dev = dev;
2168 INIT_LIST_HEAD(&hsw->stream_list);
2169 INIT_LIST_HEAD(&hsw->tx_list);
2170 INIT_LIST_HEAD(&hsw->rx_list);
2171 INIT_LIST_HEAD(&hsw->empty_list);
2172 init_waitqueue_head(&hsw->boot_wait);
2173 init_waitqueue_head(&hsw->wait_txq);
2174
2175 ret = msg_empty_list_init(hsw);
2176 if (ret < 0)
Imre Deak9cf0e452014-05-30 10:52:29 +03002177 return -ENOMEM;
Mark Browna4b12992014-03-12 23:04:35 +00002178
2179 /* start the IPC message thread */
2180 init_kthread_worker(&hsw->kworker);
2181 hsw->tx_thread = kthread_run(kthread_worker_fn,
Kees Cook35386322014-05-22 11:43:55 -07002182 &hsw->kworker, "%s",
Mark Browna4b12992014-03-12 23:04:35 +00002183 dev_name(hsw->dev));
2184 if (IS_ERR(hsw->tx_thread)) {
2185 ret = PTR_ERR(hsw->tx_thread);
2186 dev_err(hsw->dev, "error: failed to create message TX task\n");
Imre Deak9cf0e452014-05-30 10:52:29 +03002187 goto err_free_msg;
Mark Browna4b12992014-03-12 23:04:35 +00002188 }
2189 init_kthread_work(&hsw->kwork, ipc_tx_msgs);
2190
2191 hsw_dev.thread_context = hsw;
2192
2193 /* init SST shim */
2194 hsw->dsp = sst_dsp_new(dev, &hsw_dev, pdata);
2195 if (hsw->dsp == NULL) {
2196 ret = -ENODEV;
Imre Deak9cf0e452014-05-30 10:52:29 +03002197 goto dsp_err;
Mark Browna4b12992014-03-12 23:04:35 +00002198 }
2199
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00002200 /* allocate DMA buffer for context storage */
2201 hsw->dx_context = dma_alloc_coherent(hsw->dsp->dma_dev,
2202 SST_HSW_DX_CONTEXT_SIZE, &hsw->dx_context_paddr, GFP_KERNEL);
2203 if (hsw->dx_context == NULL) {
2204 ret = -ENOMEM;
2205 goto dma_err;
2206 }
2207
Mark Browna4b12992014-03-12 23:04:35 +00002208 /* keep the DSP in reset state for base FW loading */
2209 sst_dsp_reset(hsw->dsp);
2210
Lu, Han9449d392015-03-10 10:41:20 +08002211 /* load base module and other modules in base firmware image */
2212 ret = sst_hsw_module_load(hsw, SST_HSW_MODULE_BASE_FW, 0, "Base");
2213 if (ret < 0)
Mark Browna4b12992014-03-12 23:04:35 +00002214 goto fw_err;
Mark Browna4b12992014-03-12 23:04:35 +00002215
Lu, Han8c43fc22015-03-10 10:41:21 +08002216 /* try to load module waves */
2217 sst_hsw_module_load(hsw, SST_HSW_MODULE_WAVES, 0, "intel/IntcPP01.bin");
2218
Lu, Han3fe06072015-02-25 08:26:21 +08002219 /* allocate scratch mem regions */
2220 ret = sst_block_alloc_scratch(hsw->dsp);
2221 if (ret < 0)
2222 goto boot_err;
2223
Mark Browna4b12992014-03-12 23:04:35 +00002224 /* wait for DSP boot completion */
2225 sst_dsp_boot(hsw->dsp);
2226 ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
2227 msecs_to_jiffies(IPC_BOOT_MSECS));
2228 if (ret == 0) {
2229 ret = -EIO;
Liam Girdwoodb891f622014-10-30 14:34:00 +00002230 dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
2231 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
2232 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
Mark Browna4b12992014-03-12 23:04:35 +00002233 goto boot_err;
2234 }
2235
Lu, Han9449d392015-03-10 10:41:20 +08002236 /* init module state after boot */
2237 sst_hsw_init_module_state(hsw);
2238
Mark Browna4b12992014-03-12 23:04:35 +00002239 /* get the FW version */
2240 sst_hsw_fw_get_version(hsw, &version);
Mark Browna4b12992014-03-12 23:04:35 +00002241
2242 /* get the globalmixer */
2243 ret = sst_hsw_mixer_get_info(hsw);
2244 if (ret < 0) {
2245 dev_err(hsw->dev, "error: failed to get stream info\n");
2246 goto boot_err;
2247 }
2248
2249 pdata->dsp = hsw;
2250 return 0;
2251
2252boot_err:
2253 sst_dsp_reset(hsw->dsp);
Lu, Han9449d392015-03-10 10:41:20 +08002254 sst_fw_free_all(hsw->dsp);
Mark Browna4b12992014-03-12 23:04:35 +00002255fw_err:
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00002256 dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
2257 hsw->dx_context, hsw->dx_context_paddr);
2258dma_err:
Mark Browna4b12992014-03-12 23:04:35 +00002259 sst_dsp_free(hsw->dsp);
Imre Deak9cf0e452014-05-30 10:52:29 +03002260dsp_err:
2261 kthread_stop(hsw->tx_thread);
2262err_free_msg:
Mark Browna4b12992014-03-12 23:04:35 +00002263 kfree(hsw->msg);
Imre Deak9cf0e452014-05-30 10:52:29 +03002264
Mark Browna4b12992014-03-12 23:04:35 +00002265 return ret;
2266}
2267EXPORT_SYMBOL_GPL(sst_hsw_dsp_init);
2268
2269void sst_hsw_dsp_free(struct device *dev, struct sst_pdata *pdata)
2270{
2271 struct sst_hsw *hsw = pdata->dsp;
2272
2273 sst_dsp_reset(hsw->dsp);
2274 sst_fw_free_all(hsw->dsp);
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00002275 dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
2276 hsw->dx_context, hsw->dx_context_paddr);
Mark Browna4b12992014-03-12 23:04:35 +00002277 sst_dsp_free(hsw->dsp);
Imre Deak9cf0e452014-05-30 10:52:29 +03002278 kthread_stop(hsw->tx_thread);
Mark Browna4b12992014-03-12 23:04:35 +00002279 kfree(hsw->msg);
2280}
2281EXPORT_SYMBOL_GPL(sst_hsw_dsp_free);