blob: 20b629a011de60b8cbd1303cd07229ab5b46b9ea [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;
Lu, Han76c07b82015-03-12 13:53:00 +0800340
341 /* flags bit field to track module state when resume from RTD3,
342 * each bit represent state (enabled/disabled) of single module */
343 u32 enabled_modules_rtd3;
Lu, Han3814c202015-03-12 13:53:02 +0800344
345 /* buffer to store parameter lines */
346 u32 param_idx_w; /* write index */
347 u32 param_idx_r; /* read index */
348 u8 param_buf[WAVES_PARAM_LINES][WAVES_PARAM_COUNT];
Mark Browna4b12992014-03-12 23:04:35 +0000349};
350
351#define CREATE_TRACE_POINTS
352#include <trace/events/hswadsp.h>
353
354static inline u32 msg_get_global_type(u32 msg)
355{
356 return (msg & IPC_GLB_TYPE_MASK) >> IPC_GLB_TYPE_SHIFT;
357}
358
359static inline u32 msg_get_global_reply(u32 msg)
360{
361 return (msg & IPC_GLB_REPLY_MASK) >> IPC_GLB_REPLY_SHIFT;
362}
363
364static inline u32 msg_get_stream_type(u32 msg)
365{
366 return (msg & IPC_STR_TYPE_MASK) >> IPC_STR_TYPE_SHIFT;
367}
368
369static inline u32 msg_get_stage_type(u32 msg)
370{
371 return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
372}
373
Mark Browna4b12992014-03-12 23:04:35 +0000374static inline u32 msg_get_stream_id(u32 msg)
375{
376 return (msg & IPC_STR_ID_MASK) >> IPC_STR_ID_SHIFT;
377}
378
379static inline u32 msg_get_notify_reason(u32 msg)
380{
381 return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
382}
383
Lu, Hane8e79ed2015-03-10 10:41:22 +0800384static inline u32 msg_get_module_operation(u32 msg)
385{
386 return (msg & IPC_MODULE_OPERATION_MASK) >> IPC_MODULE_OPERATION_SHIFT;
387}
388
389static inline u32 msg_get_module_id(u32 msg)
390{
391 return (msg & IPC_MODULE_ID_MASK) >> IPC_MODULE_ID_SHIFT;
392}
393
Mark Browna4b12992014-03-12 23:04:35 +0000394u32 create_channel_map(enum sst_hsw_channel_config config)
395{
396 switch (config) {
397 case SST_HSW_CHANNEL_CONFIG_MONO:
398 return (0xFFFFFFF0 | SST_HSW_CHANNEL_CENTER);
399 case SST_HSW_CHANNEL_CONFIG_STEREO:
400 return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
401 | (SST_HSW_CHANNEL_RIGHT << 4));
402 case SST_HSW_CHANNEL_CONFIG_2_POINT_1:
403 return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
404 | (SST_HSW_CHANNEL_RIGHT << 4)
405 | (SST_HSW_CHANNEL_LFE << 8 ));
406 case SST_HSW_CHANNEL_CONFIG_3_POINT_0:
407 return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
408 | (SST_HSW_CHANNEL_CENTER << 4)
409 | (SST_HSW_CHANNEL_RIGHT << 8));
410 case SST_HSW_CHANNEL_CONFIG_3_POINT_1:
411 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
412 | (SST_HSW_CHANNEL_CENTER << 4)
413 | (SST_HSW_CHANNEL_RIGHT << 8)
414 | (SST_HSW_CHANNEL_LFE << 12));
415 case SST_HSW_CHANNEL_CONFIG_QUATRO:
416 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
417 | (SST_HSW_CHANNEL_RIGHT << 4)
418 | (SST_HSW_CHANNEL_LEFT_SURROUND << 8)
419 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 12));
420 case SST_HSW_CHANNEL_CONFIG_4_POINT_0:
421 return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
422 | (SST_HSW_CHANNEL_CENTER << 4)
423 | (SST_HSW_CHANNEL_RIGHT << 8)
424 | (SST_HSW_CHANNEL_CENTER_SURROUND << 12));
425 case SST_HSW_CHANNEL_CONFIG_5_POINT_0:
426 return (0xFFF00000 | SST_HSW_CHANNEL_LEFT
427 | (SST_HSW_CHANNEL_CENTER << 4)
428 | (SST_HSW_CHANNEL_RIGHT << 8)
429 | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
430 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16));
431 case SST_HSW_CHANNEL_CONFIG_5_POINT_1:
432 return (0xFF000000 | SST_HSW_CHANNEL_CENTER
433 | (SST_HSW_CHANNEL_LEFT << 4)
434 | (SST_HSW_CHANNEL_RIGHT << 8)
435 | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
436 | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16)
437 | (SST_HSW_CHANNEL_LFE << 20));
438 case SST_HSW_CHANNEL_CONFIG_DUAL_MONO:
439 return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
440 | (SST_HSW_CHANNEL_LEFT << 4));
441 default:
442 return 0xFFFFFFFF;
443 }
444}
445
446static struct sst_hsw_stream *get_stream_by_id(struct sst_hsw *hsw,
447 int stream_id)
448{
449 struct sst_hsw_stream *stream;
450
451 list_for_each_entry(stream, &hsw->stream_list, node) {
452 if (stream->reply.stream_hw_id == stream_id)
453 return stream;
454 }
455
456 return NULL;
457}
458
459static void ipc_shim_dbg(struct sst_hsw *hsw, const char *text)
460{
461 struct sst_dsp *sst = hsw->dsp;
462 u32 isr, ipcd, imrx, ipcx;
463
464 ipcx = sst_dsp_shim_read_unlocked(sst, SST_IPCX);
465 isr = sst_dsp_shim_read_unlocked(sst, SST_ISRX);
466 ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
467 imrx = sst_dsp_shim_read_unlocked(sst, SST_IMRX);
468
469 dev_err(hsw->dev, "ipc: --%s-- ipcx 0x%8.8x isr 0x%8.8x ipcd 0x%8.8x imrx 0x%8.8x\n",
470 text, ipcx, isr, ipcd, imrx);
471}
472
473/* locks held by caller */
474static struct ipc_message *msg_get_empty(struct sst_hsw *hsw)
475{
476 struct ipc_message *msg = NULL;
477
478 if (!list_empty(&hsw->empty_list)) {
479 msg = list_first_entry(&hsw->empty_list, struct ipc_message,
480 list);
481 list_del(&msg->list);
482 }
483
484 return msg;
485}
486
487static void ipc_tx_msgs(struct kthread_work *work)
488{
489 struct sst_hsw *hsw =
490 container_of(work, struct sst_hsw, kwork);
491 struct ipc_message *msg;
492 unsigned long flags;
493 u32 ipcx;
494
495 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
496
497 if (list_empty(&hsw->tx_list) || hsw->pending) {
498 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
499 return;
500 }
501
Paweł Piskorski94ce3342014-08-01 23:09:44 +0800502 /* if the DSP is busy, we will TX messages after IRQ.
503 * also postpone if we are in the middle of procesing completion irq*/
Mark Browna4b12992014-03-12 23:04:35 +0000504 ipcx = sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX);
Paweł Piskorski94ce3342014-08-01 23:09:44 +0800505 if (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE)) {
Mark Browna4b12992014-03-12 23:04:35 +0000506 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
507 return;
508 }
509
510 msg = list_first_entry(&hsw->tx_list, struct ipc_message, list);
511
512 list_move(&msg->list, &hsw->rx_list);
513
514 /* send the message */
515 sst_dsp_outbox_write(hsw->dsp, msg->tx_data, msg->tx_size);
516 sst_dsp_ipc_msg_tx(hsw->dsp, msg->header | SST_IPCX_BUSY);
517
518 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
519}
520
521/* locks held by caller */
522static void tx_msg_reply_complete(struct sst_hsw *hsw, struct ipc_message *msg)
523{
524 msg->complete = true;
525 trace_ipc_reply("completed", msg->header);
526
527 if (!msg->wait)
528 list_add_tail(&msg->list, &hsw->empty_list);
529 else
530 wake_up(&msg->waitq);
531}
532
533static int tx_wait_done(struct sst_hsw *hsw, struct ipc_message *msg,
534 void *rx_data)
535{
536 unsigned long flags;
537 int ret;
538
539 /* wait for DSP completion (in all cases atm inc pending) */
540 ret = wait_event_timeout(msg->waitq, msg->complete,
541 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
542
543 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
544 if (ret == 0) {
545 ipc_shim_dbg(hsw, "message timeout");
546
547 trace_ipc_error("error message timeout for", msg->header);
Liam Girdwood97cfc752014-08-01 23:08:38 +0800548 list_del(&msg->list);
Mark Browna4b12992014-03-12 23:04:35 +0000549 ret = -ETIMEDOUT;
550 } else {
551
552 /* copy the data returned from DSP */
553 if (msg->rx_size)
554 memcpy(rx_data, msg->rx_data, msg->rx_size);
555 ret = msg->errno;
556 }
557
558 list_add_tail(&msg->list, &hsw->empty_list);
559 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
560 return ret;
561}
562
563static int ipc_tx_message(struct sst_hsw *hsw, u32 header, void *tx_data,
564 size_t tx_bytes, void *rx_data, size_t rx_bytes, int wait)
565{
566 struct ipc_message *msg;
567 unsigned long flags;
568
569 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
570
571 msg = msg_get_empty(hsw);
572 if (msg == NULL) {
573 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
574 return -EBUSY;
575 }
576
577 if (tx_bytes)
578 memcpy(msg->tx_data, tx_data, tx_bytes);
579
580 msg->header = header;
581 msg->tx_size = tx_bytes;
582 msg->rx_size = rx_bytes;
583 msg->wait = wait;
584 msg->errno = 0;
585 msg->pending = false;
586 msg->complete = false;
587
588 list_add_tail(&msg->list, &hsw->tx_list);
589 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
590
591 queue_kthread_work(&hsw->kworker, &hsw->kwork);
592
593 if (wait)
594 return tx_wait_done(hsw, msg, rx_data);
595 else
596 return 0;
597}
598
599static inline int ipc_tx_message_wait(struct sst_hsw *hsw, u32 header,
600 void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes)
601{
602 return ipc_tx_message(hsw, header, tx_data, tx_bytes, rx_data,
603 rx_bytes, 1);
604}
605
606static inline int ipc_tx_message_nowait(struct sst_hsw *hsw, u32 header,
607 void *tx_data, size_t tx_bytes)
608{
609 return ipc_tx_message(hsw, header, tx_data, tx_bytes, NULL, 0, 0);
610}
611
612static void hsw_fw_ready(struct sst_hsw *hsw, u32 header)
613{
614 struct sst_hsw_ipc_fw_ready fw_ready;
615 u32 offset;
Jie Yang249addd2014-07-15 08:51:12 +0800616 u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
617 char *tmp[5], *pinfo;
618 int i = 0;
Mark Browna4b12992014-03-12 23:04:35 +0000619
620 offset = (header & 0x1FFFFFFF) << 3;
621
622 dev_dbg(hsw->dev, "ipc: DSP is ready 0x%8.8x offset %d\n",
623 header, offset);
624
625 /* copy data from the DSP FW ready offset */
626 sst_dsp_read(hsw->dsp, &fw_ready, offset, sizeof(fw_ready));
627
628 sst_dsp_mailbox_init(hsw->dsp, fw_ready.inbox_offset,
629 fw_ready.inbox_size, fw_ready.outbox_offset,
630 fw_ready.outbox_size);
631
632 hsw->boot_complete = true;
633 wake_up(&hsw->boot_wait);
634
635 dev_dbg(hsw->dev, " mailbox upstream 0x%x - size 0x%x\n",
636 fw_ready.inbox_offset, fw_ready.inbox_size);
637 dev_dbg(hsw->dev, " mailbox downstream 0x%x - size 0x%x\n",
638 fw_ready.outbox_offset, fw_ready.outbox_size);
Jie Yang249addd2014-07-15 08:51:12 +0800639 if (fw_ready.fw_info_size < sizeof(fw_ready.fw_info)) {
640 fw_ready.fw_info[fw_ready.fw_info_size] = 0;
641 dev_dbg(hsw->dev, " Firmware info: %s \n", fw_ready.fw_info);
642
643 /* log the FW version info got from the mailbox here. */
644 memcpy(fw_info, fw_ready.fw_info, fw_ready.fw_info_size);
645 pinfo = &fw_info[0];
646 for (i = 0; i < sizeof(tmp) / sizeof(char *); i++)
647 tmp[i] = strsep(&pinfo, " ");
648 dev_info(hsw->dev, "FW loaded, mailbox readback FW info: type %s, - "
649 "version: %s.%s, build %s, source commit id: %s\n",
650 tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
651 }
Mark Browna4b12992014-03-12 23:04:35 +0000652}
653
654static void hsw_notification_work(struct work_struct *work)
655{
656 struct sst_hsw_stream *stream = container_of(work,
657 struct sst_hsw_stream, notify_work);
658 struct sst_hsw_ipc_stream_glitch_position *glitch = &stream->glitch;
659 struct sst_hsw_ipc_stream_get_position *pos = &stream->rpos;
660 struct sst_hsw *hsw = stream->hsw;
661 u32 reason;
662
663 reason = msg_get_notify_reason(stream->header);
664
665 switch (reason) {
666 case IPC_STG_GLITCH:
667 trace_ipc_notification("DSP stream under/overrun",
668 stream->reply.stream_hw_id);
669 sst_dsp_inbox_read(hsw->dsp, glitch, sizeof(*glitch));
670
671 dev_err(hsw->dev, "glitch %d pos 0x%x write pos 0x%x\n",
672 glitch->glitch_type, glitch->present_pos,
673 glitch->write_pos);
674 break;
675
676 case IPC_POSITION_CHANGED:
677 trace_ipc_notification("DSP stream position changed for",
678 stream->reply.stream_hw_id);
Dan Carpenter7897ab72014-04-16 18:38:11 +0300679 sst_dsp_inbox_read(hsw->dsp, pos, sizeof(*pos));
Mark Browna4b12992014-03-12 23:04:35 +0000680
681 if (stream->notify_position)
682 stream->notify_position(stream, stream->pdata);
683
684 break;
685 default:
686 dev_err(hsw->dev, "error: unknown notification 0x%x\n",
687 stream->header);
688 break;
689 }
690
691 /* tell DSP that notification has been handled */
Jie Yang09a34aa2015-01-21 07:20:23 +0800692 sst_dsp_shim_update_bits(hsw->dsp, SST_IPCD,
Mark Browna4b12992014-03-12 23:04:35 +0000693 SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
694
695 /* unmask busy interrupt */
Jie Yang09a34aa2015-01-21 07:20:23 +0800696 sst_dsp_shim_update_bits(hsw->dsp, SST_IMRX, SST_IMRX_BUSY, 0);
Mark Browna4b12992014-03-12 23:04:35 +0000697}
698
699static struct ipc_message *reply_find_msg(struct sst_hsw *hsw, u32 header)
700{
701 struct ipc_message *msg;
702
703 /* clear reply bits & status bits */
704 header &= ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
705
706 if (list_empty(&hsw->rx_list)) {
707 dev_err(hsw->dev, "error: rx list empty but received 0x%x\n",
708 header);
709 return NULL;
710 }
711
712 list_for_each_entry(msg, &hsw->rx_list, list) {
713 if (msg->header == header)
714 return msg;
715 }
716
717 return NULL;
718}
719
720static void hsw_stream_update(struct sst_hsw *hsw, struct ipc_message *msg)
721{
722 struct sst_hsw_stream *stream;
723 u32 header = msg->header & ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
724 u32 stream_id = msg_get_stream_id(header);
725 u32 stream_msg = msg_get_stream_type(header);
726
727 stream = get_stream_by_id(hsw, stream_id);
728 if (stream == NULL)
729 return;
730
731 switch (stream_msg) {
732 case IPC_STR_STAGE_MESSAGE:
733 case IPC_STR_NOTIFICATION:
Liam Girdwood81552612014-07-30 20:09:47 +0800734 break;
Mark Browna4b12992014-03-12 23:04:35 +0000735 case IPC_STR_RESET:
Liam Girdwood81552612014-07-30 20:09:47 +0800736 trace_ipc_notification("stream reset", stream->reply.stream_hw_id);
Mark Browna4b12992014-03-12 23:04:35 +0000737 break;
738 case IPC_STR_PAUSE:
739 stream->running = false;
740 trace_ipc_notification("stream paused",
741 stream->reply.stream_hw_id);
742 break;
743 case IPC_STR_RESUME:
744 stream->running = true;
745 trace_ipc_notification("stream running",
746 stream->reply.stream_hw_id);
747 break;
748 }
749}
750
751static int hsw_process_reply(struct sst_hsw *hsw, u32 header)
752{
753 struct ipc_message *msg;
754 u32 reply = msg_get_global_reply(header);
755
756 trace_ipc_reply("processing -->", header);
757
758 msg = reply_find_msg(hsw, header);
759 if (msg == NULL) {
760 trace_ipc_error("error: can't find message header", header);
761 return -EIO;
762 }
763
764 /* first process the header */
765 switch (reply) {
766 case IPC_GLB_REPLY_PENDING:
767 trace_ipc_pending_reply("received", header);
768 msg->pending = true;
769 hsw->pending = true;
770 return 1;
771 case IPC_GLB_REPLY_SUCCESS:
772 if (msg->pending) {
773 trace_ipc_pending_reply("completed", header);
774 sst_dsp_inbox_read(hsw->dsp, msg->rx_data,
775 msg->rx_size);
776 hsw->pending = false;
777 } else {
778 /* copy data from the DSP */
779 sst_dsp_outbox_read(hsw->dsp, msg->rx_data,
780 msg->rx_size);
781 }
782 break;
783 /* these will be rare - but useful for debug */
784 case IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE:
785 trace_ipc_error("error: unknown message type", header);
786 msg->errno = -EBADMSG;
787 break;
788 case IPC_GLB_REPLY_OUT_OF_RESOURCES:
789 trace_ipc_error("error: out of resources", header);
790 msg->errno = -ENOMEM;
791 break;
792 case IPC_GLB_REPLY_BUSY:
793 trace_ipc_error("error: reply busy", header);
794 msg->errno = -EBUSY;
795 break;
796 case IPC_GLB_REPLY_FAILURE:
797 trace_ipc_error("error: reply failure", header);
798 msg->errno = -EINVAL;
799 break;
800 case IPC_GLB_REPLY_STAGE_UNINITIALIZED:
801 trace_ipc_error("error: stage uninitialized", header);
802 msg->errno = -EINVAL;
803 break;
804 case IPC_GLB_REPLY_NOT_FOUND:
805 trace_ipc_error("error: reply not found", header);
806 msg->errno = -EINVAL;
807 break;
808 case IPC_GLB_REPLY_SOURCE_NOT_STARTED:
809 trace_ipc_error("error: source not started", header);
810 msg->errno = -EINVAL;
811 break;
812 case IPC_GLB_REPLY_INVALID_REQUEST:
813 trace_ipc_error("error: invalid request", header);
814 msg->errno = -EINVAL;
815 break;
816 case IPC_GLB_REPLY_ERROR_INVALID_PARAM:
817 trace_ipc_error("error: invalid parameter", header);
818 msg->errno = -EINVAL;
819 break;
820 default:
821 trace_ipc_error("error: unknown reply", header);
822 msg->errno = -EINVAL;
823 break;
824 }
825
826 /* update any stream states */
Paweł Piskorskid6e08612014-08-01 23:10:43 +0800827 if (msg_get_global_type(header) == IPC_GLB_STREAM_MESSAGE)
828 hsw_stream_update(hsw, msg);
Mark Browna4b12992014-03-12 23:04:35 +0000829
830 /* wake up and return the error if we have waiters on this message ? */
831 list_del(&msg->list);
832 tx_msg_reply_complete(hsw, msg);
833
834 return 1;
835}
836
Lu, Hane8e79ed2015-03-10 10:41:22 +0800837static int hsw_module_message(struct sst_hsw *hsw, u32 header)
838{
839 u32 operation, module_id;
840 int handled = 0;
841
842 operation = msg_get_module_operation(header);
843 module_id = msg_get_module_id(header);
844 dev_dbg(hsw->dev, "received module message header: 0x%8.8x\n",
845 header);
846 dev_dbg(hsw->dev, "operation: 0x%8.8x module_id: 0x%8.8x\n",
847 operation, module_id);
848
849 switch (operation) {
850 case IPC_MODULE_NOTIFICATION:
851 dev_dbg(hsw->dev, "module notification received");
852 handled = 1;
853 break;
854 default:
855 handled = hsw_process_reply(hsw, header);
856 break;
857 }
858
859 return handled;
860}
861
Mark Browna4b12992014-03-12 23:04:35 +0000862static int hsw_stream_message(struct sst_hsw *hsw, u32 header)
863{
864 u32 stream_msg, stream_id, stage_type;
865 struct sst_hsw_stream *stream;
866 int handled = 0;
867
868 stream_msg = msg_get_stream_type(header);
869 stream_id = msg_get_stream_id(header);
870 stage_type = msg_get_stage_type(header);
871
872 stream = get_stream_by_id(hsw, stream_id);
873 if (stream == NULL)
874 return handled;
875
876 stream->header = header;
877
878 switch (stream_msg) {
879 case IPC_STR_STAGE_MESSAGE:
880 dev_err(hsw->dev, "error: stage msg not implemented 0x%8.8x\n",
881 header);
882 break;
883 case IPC_STR_NOTIFICATION:
884 schedule_work(&stream->notify_work);
885 break;
886 default:
887 /* handle pending message complete request */
888 handled = hsw_process_reply(hsw, header);
889 break;
890 }
891
892 return handled;
893}
894
895static int hsw_log_message(struct sst_hsw *hsw, u32 header)
896{
897 u32 operation = (header & IPC_LOG_OP_MASK) >> IPC_LOG_OP_SHIFT;
898 struct sst_hsw_log_stream *stream = &hsw->log_stream;
899 int ret = 1;
900
901 if (operation != IPC_DEBUG_REQUEST_LOG_DUMP) {
902 dev_err(hsw->dev,
903 "error: log msg not implemented 0x%8.8x\n", header);
904 return 0;
905 }
906
907 mutex_lock(&stream->rw_mutex);
908 stream->last_pos = stream->curr_pos;
909 sst_dsp_inbox_read(
910 hsw->dsp, &stream->curr_pos, sizeof(stream->curr_pos));
911 mutex_unlock(&stream->rw_mutex);
912
913 schedule_work(&stream->notify_work);
914
915 return ret;
916}
917
918static int hsw_process_notification(struct sst_hsw *hsw)
919{
920 struct sst_dsp *sst = hsw->dsp;
921 u32 type, header;
922 int handled = 1;
923
924 header = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
925 type = msg_get_global_type(header);
926
927 trace_ipc_request("processing -->", header);
928
929 /* FW Ready is a special case */
930 if (!hsw->boot_complete && header & IPC_FW_READY) {
931 hsw_fw_ready(hsw, header);
932 return handled;
933 }
934
935 switch (type) {
936 case IPC_GLB_GET_FW_VERSION:
937 case IPC_GLB_ALLOCATE_STREAM:
938 case IPC_GLB_FREE_STREAM:
939 case IPC_GLB_GET_FW_CAPABILITIES:
940 case IPC_GLB_REQUEST_DUMP:
941 case IPC_GLB_GET_DEVICE_FORMATS:
942 case IPC_GLB_SET_DEVICE_FORMATS:
943 case IPC_GLB_ENTER_DX_STATE:
944 case IPC_GLB_GET_MIXER_STREAM_INFO:
945 case IPC_GLB_MAX_IPC_MESSAGE_TYPE:
946 case IPC_GLB_RESTORE_CONTEXT:
947 case IPC_GLB_SHORT_REPLY:
948 dev_err(hsw->dev, "error: message type %d header 0x%x\n",
949 type, header);
950 break;
951 case IPC_GLB_STREAM_MESSAGE:
952 handled = hsw_stream_message(hsw, header);
953 break;
954 case IPC_GLB_DEBUG_LOG_MESSAGE:
955 handled = hsw_log_message(hsw, header);
956 break;
Lu, Hane8e79ed2015-03-10 10:41:22 +0800957 case IPC_GLB_MODULE_OPERATION:
958 handled = hsw_module_message(hsw, header);
959 break;
Mark Browna4b12992014-03-12 23:04:35 +0000960 default:
961 dev_err(hsw->dev, "error: unexpected type %d hdr 0x%8.8x\n",
962 type, header);
963 break;
964 }
965
966 return handled;
967}
968
969static irqreturn_t hsw_irq_thread(int irq, void *context)
970{
971 struct sst_dsp *sst = (struct sst_dsp *) context;
972 struct sst_hsw *hsw = sst_dsp_get_thread_context(sst);
973 u32 ipcx, ipcd;
974 int handled;
975 unsigned long flags;
976
977 spin_lock_irqsave(&sst->spinlock, flags);
978
979 ipcx = sst_dsp_ipc_msg_rx(hsw->dsp);
980 ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
981
982 /* reply message from DSP */
983 if (ipcx & SST_IPCX_DONE) {
984
985 /* Handle Immediate reply from DSP Core */
986 handled = hsw_process_reply(hsw, ipcx);
987
988 if (handled > 0) {
989 /* clear DONE bit - tell DSP we have completed */
990 sst_dsp_shim_update_bits_unlocked(sst, SST_IPCX,
991 SST_IPCX_DONE, 0);
992
993 /* unmask Done interrupt */
994 sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
995 SST_IMRX_DONE, 0);
996 }
997 }
998
999 /* new message from DSP */
1000 if (ipcd & SST_IPCD_BUSY) {
1001
1002 /* Handle Notification and Delayed reply from DSP Core */
1003 handled = hsw_process_notification(hsw);
1004
1005 /* clear BUSY bit and set DONE bit - accept new messages */
1006 if (handled > 0) {
1007 sst_dsp_shim_update_bits_unlocked(sst, SST_IPCD,
1008 SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
1009
1010 /* unmask busy interrupt */
1011 sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
1012 SST_IMRX_BUSY, 0);
1013 }
1014 }
1015
1016 spin_unlock_irqrestore(&sst->spinlock, flags);
1017
1018 /* continue to send any remaining messages... */
1019 queue_kthread_work(&hsw->kworker, &hsw->kwork);
1020
1021 return IRQ_HANDLED;
1022}
1023
1024int sst_hsw_fw_get_version(struct sst_hsw *hsw,
1025 struct sst_hsw_ipc_fw_version *version)
1026{
1027 int ret;
1028
1029 ret = ipc_tx_message_wait(hsw, IPC_GLB_TYPE(IPC_GLB_GET_FW_VERSION),
1030 NULL, 0, version, sizeof(*version));
1031 if (ret < 0)
1032 dev_err(hsw->dev, "error: get version failed\n");
1033
1034 return ret;
1035}
1036
1037/* Mixer Controls */
Mark Browna4b12992014-03-12 23:04:35 +00001038int sst_hsw_stream_get_volume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1039 u32 stage_id, u32 channel, u32 *volume)
1040{
1041 if (channel > 1)
1042 return -EINVAL;
1043
1044 sst_dsp_read(hsw->dsp, volume,
Christian Engelmayerbf657d22014-04-13 19:56:36 +02001045 stream->reply.volume_register_address[channel],
1046 sizeof(*volume));
Mark Browna4b12992014-03-12 23:04:35 +00001047
1048 return 0;
1049}
1050
Mark Browna4b12992014-03-12 23:04:35 +00001051/* stream volume */
1052int sst_hsw_stream_set_volume(struct sst_hsw *hsw,
1053 struct sst_hsw_stream *stream, u32 stage_id, u32 channel, u32 volume)
1054{
1055 struct sst_hsw_ipc_volume_req *req;
1056 u32 header;
1057 int ret;
1058
1059 trace_ipc_request("set stream volume", stream->reply.stream_hw_id);
1060
Jie Yangf1e59822014-11-25 21:00:53 +08001061 if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
Mark Browna4b12992014-03-12 23:04:35 +00001062 return -EINVAL;
1063
Mark Browna4b12992014-03-12 23:04:35 +00001064 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
1065 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
1066 header |= (stream->reply.stream_hw_id << IPC_STR_ID_SHIFT);
1067 header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
1068 header |= (stage_id << IPC_STG_ID_SHIFT);
1069
1070 req = &stream->vol_req;
Mark Browna4b12992014-03-12 23:04:35 +00001071 req->target_volume = volume;
1072
Jie Yangf1e59822014-11-25 21:00:53 +08001073 /* set both at same time ? */
1074 if (channel == SST_HSW_CHANNELS_ALL) {
1075 if (hsw->mute[0] && hsw->mute[1]) {
1076 hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
1077 return 0;
1078 } else if (hsw->mute[0])
1079 req->channel = 1;
1080 else if (hsw->mute[1])
1081 req->channel = 0;
1082 else
1083 req->channel = SST_HSW_CHANNELS_ALL;
1084 } else {
1085 /* set only 1 channel */
1086 if (hsw->mute[channel]) {
1087 hsw->mute_volume[channel] = volume;
1088 return 0;
1089 }
1090 req->channel = channel;
1091 }
1092
Mark Browna4b12992014-03-12 23:04:35 +00001093 ret = ipc_tx_message_wait(hsw, header, req, sizeof(*req), NULL, 0);
1094 if (ret < 0) {
1095 dev_err(hsw->dev, "error: set stream volume failed\n");
1096 return ret;
1097 }
1098
1099 return 0;
1100}
1101
Mark Browna4b12992014-03-12 23:04:35 +00001102int sst_hsw_mixer_get_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
1103 u32 *volume)
1104{
1105 if (channel > 1)
1106 return -EINVAL;
1107
1108 sst_dsp_read(hsw->dsp, volume,
1109 hsw->mixer_info.volume_register_address[channel],
1110 sizeof(*volume));
1111
1112 return 0;
1113}
1114
Mark Browna4b12992014-03-12 23:04:35 +00001115/* global mixer volume */
1116int sst_hsw_mixer_set_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
1117 u32 volume)
1118{
1119 struct sst_hsw_ipc_volume_req req;
1120 u32 header;
1121 int ret;
1122
1123 trace_ipc_request("set mixer volume", volume);
1124
Jie Yangf1e59822014-11-25 21:00:53 +08001125 if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
1126 return -EINVAL;
1127
Mark Browna4b12992014-03-12 23:04:35 +00001128 /* set both at same time ? */
Jie Yangf1e59822014-11-25 21:00:53 +08001129 if (channel == SST_HSW_CHANNELS_ALL) {
Mark Browna4b12992014-03-12 23:04:35 +00001130 if (hsw->mute[0] && hsw->mute[1]) {
1131 hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
1132 return 0;
1133 } else if (hsw->mute[0])
1134 req.channel = 1;
1135 else if (hsw->mute[1])
1136 req.channel = 0;
1137 else
Jie Yangf1e59822014-11-25 21:00:53 +08001138 req.channel = SST_HSW_CHANNELS_ALL;
Mark Browna4b12992014-03-12 23:04:35 +00001139 } else {
1140 /* set only 1 channel */
1141 if (hsw->mute[channel]) {
1142 hsw->mute_volume[channel] = volume;
1143 return 0;
1144 }
1145 req.channel = channel;
1146 }
1147
1148 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
1149 IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
1150 header |= (hsw->mixer_info.mixer_hw_id << IPC_STR_ID_SHIFT);
1151 header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
1152 header |= (stage_id << IPC_STG_ID_SHIFT);
1153
1154 req.curve_duration = hsw->curve_duration;
1155 req.curve_type = hsw->curve_type;
1156 req.target_volume = volume;
1157
1158 ret = ipc_tx_message_wait(hsw, header, &req, sizeof(req), NULL, 0);
1159 if (ret < 0) {
1160 dev_err(hsw->dev, "error: set mixer volume failed\n");
1161 return ret;
1162 }
1163
1164 return 0;
1165}
1166
1167/* Stream API */
1168struct sst_hsw_stream *sst_hsw_stream_new(struct sst_hsw *hsw, int id,
1169 u32 (*notify_position)(struct sst_hsw_stream *stream, void *data),
1170 void *data)
1171{
1172 struct sst_hsw_stream *stream;
Wenkai Dud132cb02014-04-23 13:29:30 +03001173 struct sst_dsp *sst = hsw->dsp;
1174 unsigned long flags;
Mark Browna4b12992014-03-12 23:04:35 +00001175
1176 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1177 if (stream == NULL)
1178 return NULL;
1179
Wenkai Dud132cb02014-04-23 13:29:30 +03001180 spin_lock_irqsave(&sst->spinlock, flags);
Jie Yanga0a7c482015-01-12 17:17:34 +08001181 stream->reply.stream_hw_id = INVALID_STREAM_HW_ID;
Mark Browna4b12992014-03-12 23:04:35 +00001182 list_add(&stream->node, &hsw->stream_list);
1183 stream->notify_position = notify_position;
1184 stream->pdata = data;
1185 stream->hsw = hsw;
1186 stream->host_id = id;
1187
1188 /* work to process notification messages */
1189 INIT_WORK(&stream->notify_work, hsw_notification_work);
Wenkai Dud132cb02014-04-23 13:29:30 +03001190 spin_unlock_irqrestore(&sst->spinlock, flags);
Mark Browna4b12992014-03-12 23:04:35 +00001191
1192 return stream;
1193}
1194
1195int sst_hsw_stream_free(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1196{
1197 u32 header;
1198 int ret = 0;
Wenkai Dud132cb02014-04-23 13:29:30 +03001199 struct sst_dsp *sst = hsw->dsp;
1200 unsigned long flags;
Mark Browna4b12992014-03-12 23:04:35 +00001201
Jie Yangf81677b2015-01-07 22:07:05 +08001202 if (!stream) {
1203 dev_warn(hsw->dev, "warning: stream is NULL, no stream to free, ignore it.\n");
1204 return 0;
1205 }
1206
Mark Browna4b12992014-03-12 23:04:35 +00001207 /* dont free DSP streams that are not commited */
1208 if (!stream->commited)
1209 goto out;
1210
1211 trace_ipc_request("stream free", stream->host_id);
1212
1213 stream->free_req.stream_id = stream->reply.stream_hw_id;
1214 header = IPC_GLB_TYPE(IPC_GLB_FREE_STREAM);
1215
1216 ret = ipc_tx_message_wait(hsw, header, &stream->free_req,
1217 sizeof(stream->free_req), NULL, 0);
1218 if (ret < 0) {
1219 dev_err(hsw->dev, "error: free stream %d failed\n",
1220 stream->free_req.stream_id);
1221 return -EAGAIN;
1222 }
1223
1224 trace_hsw_stream_free_req(stream, &stream->free_req);
1225
1226out:
Jarkko Nikulade30a2c2014-04-24 10:34:36 +03001227 cancel_work_sync(&stream->notify_work);
Wenkai Dud132cb02014-04-23 13:29:30 +03001228 spin_lock_irqsave(&sst->spinlock, flags);
Mark Browna4b12992014-03-12 23:04:35 +00001229 list_del(&stream->node);
1230 kfree(stream);
Wenkai Dud132cb02014-04-23 13:29:30 +03001231 spin_unlock_irqrestore(&sst->spinlock, flags);
Mark Browna4b12992014-03-12 23:04:35 +00001232
1233 return ret;
1234}
1235
1236int sst_hsw_stream_set_bits(struct sst_hsw *hsw,
1237 struct sst_hsw_stream *stream, enum sst_hsw_bitdepth bits)
1238{
1239 if (stream->commited) {
1240 dev_err(hsw->dev, "error: stream committed for set bits\n");
1241 return -EINVAL;
1242 }
1243
1244 stream->request.format.bitdepth = bits;
1245 return 0;
1246}
1247
1248int sst_hsw_stream_set_channels(struct sst_hsw *hsw,
1249 struct sst_hsw_stream *stream, int channels)
1250{
1251 if (stream->commited) {
1252 dev_err(hsw->dev, "error: stream committed for set channels\n");
1253 return -EINVAL;
1254 }
1255
Mark Browna4b12992014-03-12 23:04:35 +00001256 stream->request.format.ch_num = channels;
1257 return 0;
1258}
1259
1260int sst_hsw_stream_set_rate(struct sst_hsw *hsw,
1261 struct sst_hsw_stream *stream, int rate)
1262{
1263 if (stream->commited) {
1264 dev_err(hsw->dev, "error: stream committed for set rate\n");
1265 return -EINVAL;
1266 }
1267
1268 stream->request.format.frequency = rate;
1269 return 0;
1270}
1271
1272int sst_hsw_stream_set_map_config(struct sst_hsw *hsw,
1273 struct sst_hsw_stream *stream, u32 map,
1274 enum sst_hsw_channel_config config)
1275{
1276 if (stream->commited) {
1277 dev_err(hsw->dev, "error: stream committed for set map\n");
1278 return -EINVAL;
1279 }
1280
1281 stream->request.format.map = map;
1282 stream->request.format.config = config;
1283 return 0;
1284}
1285
1286int sst_hsw_stream_set_style(struct sst_hsw *hsw,
1287 struct sst_hsw_stream *stream, enum sst_hsw_interleaving style)
1288{
1289 if (stream->commited) {
1290 dev_err(hsw->dev, "error: stream committed for set style\n");
1291 return -EINVAL;
1292 }
1293
1294 stream->request.format.style = style;
1295 return 0;
1296}
1297
1298int sst_hsw_stream_set_valid(struct sst_hsw *hsw,
1299 struct sst_hsw_stream *stream, u32 bits)
1300{
1301 if (stream->commited) {
1302 dev_err(hsw->dev, "error: stream committed for set valid bits\n");
1303 return -EINVAL;
1304 }
1305
1306 stream->request.format.valid_bit = bits;
1307 return 0;
1308}
1309
1310/* Stream Configuration */
1311int sst_hsw_stream_format(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1312 enum sst_hsw_stream_path_id path_id,
1313 enum sst_hsw_stream_type stream_type,
1314 enum sst_hsw_stream_format format_id)
1315{
1316 if (stream->commited) {
1317 dev_err(hsw->dev, "error: stream committed for set format\n");
1318 return -EINVAL;
1319 }
1320
1321 stream->request.path_id = path_id;
1322 stream->request.stream_type = stream_type;
1323 stream->request.format_id = format_id;
1324
1325 trace_hsw_stream_alloc_request(stream, &stream->request);
1326
1327 return 0;
1328}
1329
1330int sst_hsw_stream_buffer(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1331 u32 ring_pt_address, u32 num_pages,
1332 u32 ring_size, u32 ring_offset, u32 ring_first_pfn)
1333{
1334 if (stream->commited) {
1335 dev_err(hsw->dev, "error: stream committed for buffer\n");
1336 return -EINVAL;
1337 }
1338
1339 stream->request.ringinfo.ring_pt_address = ring_pt_address;
1340 stream->request.ringinfo.num_pages = num_pages;
1341 stream->request.ringinfo.ring_size = ring_size;
1342 stream->request.ringinfo.ring_offset = ring_offset;
1343 stream->request.ringinfo.ring_first_pfn = ring_first_pfn;
1344
1345 trace_hsw_stream_buffer(stream);
1346
1347 return 0;
1348}
1349
1350int sst_hsw_stream_set_module_info(struct sst_hsw *hsw,
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001351 struct sst_hsw_stream *stream, struct sst_module_runtime *runtime)
Mark Browna4b12992014-03-12 23:04:35 +00001352{
1353 struct sst_hsw_module_map *map = &stream->request.map;
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001354 struct sst_dsp *dsp = sst_hsw_get_dsp(hsw);
1355 struct sst_module *module = runtime->module;
Mark Browna4b12992014-03-12 23:04:35 +00001356
1357 if (stream->commited) {
1358 dev_err(hsw->dev, "error: stream committed for set module\n");
1359 return -EINVAL;
1360 }
1361
1362 /* only support initial module atm */
1363 map->module_entries_count = 1;
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001364 map->module_entries[0].module_id = module->id;
1365 map->module_entries[0].entry_point = module->entry;
Mark Browna4b12992014-03-12 23:04:35 +00001366
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001367 stream->request.persistent_mem.offset =
1368 sst_dsp_get_offset(dsp, runtime->persistent_offset, SST_MEM_DRAM);
1369 stream->request.persistent_mem.size = module->persistent_size;
Mark Browna4b12992014-03-12 23:04:35 +00001370
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001371 stream->request.scratch_mem.offset =
1372 sst_dsp_get_offset(dsp, dsp->scratch_offset, SST_MEM_DRAM);
1373 stream->request.scratch_mem.size = dsp->scratch_size;
Mark Browna4b12992014-03-12 23:04:35 +00001374
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001375 dev_dbg(hsw->dev, "module %d runtime %d using:\n", module->id,
1376 runtime->id);
1377 dev_dbg(hsw->dev, " persistent offset 0x%x bytes 0x%x\n",
1378 stream->request.persistent_mem.offset,
1379 stream->request.persistent_mem.size);
1380 dev_dbg(hsw->dev, " scratch offset 0x%x bytes 0x%x\n",
1381 stream->request.scratch_mem.offset,
1382 stream->request.scratch_mem.size);
Mark Browna4b12992014-03-12 23:04:35 +00001383
1384 return 0;
1385}
1386
1387int sst_hsw_stream_commit(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1388{
1389 struct sst_hsw_ipc_stream_alloc_req *str_req = &stream->request;
1390 struct sst_hsw_ipc_stream_alloc_reply *reply = &stream->reply;
1391 u32 header;
1392 int ret;
1393
Jie Yangf81677b2015-01-07 22:07:05 +08001394 if (!stream) {
1395 dev_warn(hsw->dev, "warning: stream is NULL, no stream to commit, ignore it.\n");
1396 return 0;
1397 }
1398
1399 if (stream->commited) {
1400 dev_warn(hsw->dev, "warning: stream is already committed, ignore it.\n");
1401 return 0;
1402 }
1403
Mark Browna4b12992014-03-12 23:04:35 +00001404 trace_ipc_request("stream alloc", stream->host_id);
1405
1406 header = IPC_GLB_TYPE(IPC_GLB_ALLOCATE_STREAM);
1407
1408 ret = ipc_tx_message_wait(hsw, header, str_req, sizeof(*str_req),
1409 reply, sizeof(*reply));
1410 if (ret < 0) {
1411 dev_err(hsw->dev, "error: stream commit failed\n");
1412 return ret;
1413 }
1414
1415 stream->commited = 1;
1416 trace_hsw_stream_alloc_reply(stream);
1417
1418 return 0;
1419}
1420
Libin Yang1b006992015-02-10 10:02:47 +08001421snd_pcm_uframes_t sst_hsw_stream_get_old_position(struct sst_hsw *hsw,
1422 struct sst_hsw_stream *stream)
1423{
1424 return stream->old_position;
1425}
1426
1427void sst_hsw_stream_set_old_position(struct sst_hsw *hsw,
1428 struct sst_hsw_stream *stream, snd_pcm_uframes_t val)
1429{
1430 stream->old_position = val;
1431}
1432
1433bool sst_hsw_stream_get_silence_start(struct sst_hsw *hsw,
1434 struct sst_hsw_stream *stream)
1435{
1436 return stream->play_silence;
1437}
1438
1439void sst_hsw_stream_set_silence_start(struct sst_hsw *hsw,
1440 struct sst_hsw_stream *stream, bool val)
1441{
1442 stream->play_silence = val;
1443}
1444
Mark Browna4b12992014-03-12 23:04:35 +00001445/* Stream Information - these calls could be inline but we want the IPC
1446 ABI to be opaque to client PCM drivers to cope with any future ABI changes */
Mark Browna4b12992014-03-12 23:04:35 +00001447int sst_hsw_mixer_get_info(struct sst_hsw *hsw)
1448{
1449 struct sst_hsw_ipc_stream_info_reply *reply;
1450 u32 header;
1451 int ret;
1452
1453 reply = &hsw->mixer_info;
1454 header = IPC_GLB_TYPE(IPC_GLB_GET_MIXER_STREAM_INFO);
1455
1456 trace_ipc_request("get global mixer info", 0);
1457
1458 ret = ipc_tx_message_wait(hsw, header, NULL, 0, reply, sizeof(*reply));
1459 if (ret < 0) {
1460 dev_err(hsw->dev, "error: get stream info failed\n");
1461 return ret;
1462 }
1463
1464 trace_hsw_mixer_info_reply(reply);
1465
1466 return 0;
1467}
1468
1469/* Send stream command */
1470static int sst_hsw_stream_operations(struct sst_hsw *hsw, int type,
1471 int stream_id, int wait)
1472{
1473 u32 header;
1474
1475 header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) | IPC_STR_TYPE(type);
1476 header |= (stream_id << IPC_STR_ID_SHIFT);
1477
1478 if (wait)
1479 return ipc_tx_message_wait(hsw, header, NULL, 0, NULL, 0);
1480 else
1481 return ipc_tx_message_nowait(hsw, header, NULL, 0);
1482}
1483
1484/* Stream ALSA trigger operations */
1485int sst_hsw_stream_pause(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1486 int wait)
1487{
1488 int ret;
1489
Jie Yangf81677b2015-01-07 22:07:05 +08001490 if (!stream) {
1491 dev_warn(hsw->dev, "warning: stream is NULL, no stream to pause, ignore it.\n");
1492 return 0;
1493 }
1494
Mark Browna4b12992014-03-12 23:04:35 +00001495 trace_ipc_request("stream pause", stream->reply.stream_hw_id);
1496
1497 ret = sst_hsw_stream_operations(hsw, IPC_STR_PAUSE,
1498 stream->reply.stream_hw_id, wait);
1499 if (ret < 0)
1500 dev_err(hsw->dev, "error: failed to pause stream %d\n",
1501 stream->reply.stream_hw_id);
1502
1503 return ret;
1504}
1505
1506int sst_hsw_stream_resume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
1507 int wait)
1508{
1509 int ret;
1510
Jie Yangf81677b2015-01-07 22:07:05 +08001511 if (!stream) {
1512 dev_warn(hsw->dev, "warning: stream is NULL, no stream to resume, ignore it.\n");
1513 return 0;
1514 }
1515
Mark Browna4b12992014-03-12 23:04:35 +00001516 trace_ipc_request("stream resume", stream->reply.stream_hw_id);
1517
1518 ret = sst_hsw_stream_operations(hsw, IPC_STR_RESUME,
1519 stream->reply.stream_hw_id, wait);
1520 if (ret < 0)
1521 dev_err(hsw->dev, "error: failed to resume stream %d\n",
1522 stream->reply.stream_hw_id);
1523
1524 return ret;
1525}
1526
1527int sst_hsw_stream_reset(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
1528{
1529 int ret, tries = 10;
1530
Jie Yangf81677b2015-01-07 22:07:05 +08001531 if (!stream) {
1532 dev_warn(hsw->dev, "warning: stream is NULL, no stream to reset, ignore it.\n");
1533 return 0;
1534 }
1535
Mark Browna4b12992014-03-12 23:04:35 +00001536 /* dont reset streams that are not commited */
1537 if (!stream->commited)
1538 return 0;
1539
1540 /* wait for pause to complete before we reset the stream */
1541 while (stream->running && tries--)
1542 msleep(1);
1543 if (!tries) {
1544 dev_err(hsw->dev, "error: reset stream %d still running\n",
1545 stream->reply.stream_hw_id);
1546 return -EINVAL;
1547 }
1548
1549 trace_ipc_request("stream reset", stream->reply.stream_hw_id);
1550
1551 ret = sst_hsw_stream_operations(hsw, IPC_STR_RESET,
1552 stream->reply.stream_hw_id, 1);
1553 if (ret < 0)
1554 dev_err(hsw->dev, "error: failed to reset stream %d\n",
1555 stream->reply.stream_hw_id);
1556 return ret;
1557}
1558
1559/* Stream pointer positions */
Liam Girdwood51b4e242014-05-02 16:56:33 +01001560u32 sst_hsw_get_dsp_position(struct sst_hsw *hsw,
Mark Browna4b12992014-03-12 23:04:35 +00001561 struct sst_hsw_stream *stream)
1562{
Liam Girdwood51b4e242014-05-02 16:56:33 +01001563 u32 rpos;
1564
1565 sst_dsp_read(hsw->dsp, &rpos,
1566 stream->reply.read_position_register_address, sizeof(rpos));
1567
1568 return rpos;
1569}
1570
1571/* Stream presentation (monotonic) positions */
1572u64 sst_hsw_get_dsp_presentation_position(struct sst_hsw *hsw,
1573 struct sst_hsw_stream *stream)
1574{
1575 u64 ppos;
1576
1577 sst_dsp_read(hsw->dsp, &ppos,
1578 stream->reply.presentation_position_register_address,
1579 sizeof(ppos));
1580
1581 return ppos;
Mark Browna4b12992014-03-12 23:04:35 +00001582}
1583
Mark Browna4b12992014-03-12 23:04:35 +00001584/* physical BE config */
1585int sst_hsw_device_set_config(struct sst_hsw *hsw,
1586 enum sst_hsw_device_id dev, enum sst_hsw_device_mclk mclk,
1587 enum sst_hsw_device_mode mode, u32 clock_divider)
1588{
1589 struct sst_hsw_ipc_device_config_req config;
1590 u32 header;
1591 int ret;
1592
1593 trace_ipc_request("set device config", dev);
1594
1595 config.ssp_interface = dev;
1596 config.clock_frequency = mclk;
1597 config.mode = mode;
1598 config.clock_divider = clock_divider;
Liam Girdwoodf07e51c2014-10-16 15:29:15 +01001599 if (mode == SST_HSW_DEVICE_TDM_CLOCK_MASTER)
1600 config.channels = 4;
1601 else
1602 config.channels = 2;
Mark Browna4b12992014-03-12 23:04:35 +00001603
1604 trace_hsw_device_config_req(&config);
1605
1606 header = IPC_GLB_TYPE(IPC_GLB_SET_DEVICE_FORMATS);
1607
1608 ret = ipc_tx_message_wait(hsw, header, &config, sizeof(config),
1609 NULL, 0);
1610 if (ret < 0)
1611 dev_err(hsw->dev, "error: set device formats failed\n");
1612
1613 return ret;
1614}
1615EXPORT_SYMBOL_GPL(sst_hsw_device_set_config);
1616
1617/* DX Config */
1618int sst_hsw_dx_set_state(struct sst_hsw *hsw,
1619 enum sst_hsw_dx_state state, struct sst_hsw_ipc_dx_reply *dx)
1620{
1621 u32 header, state_;
Liam Girdwood543ec632014-07-30 20:11:26 +08001622 int ret, item;
Mark Browna4b12992014-03-12 23:04:35 +00001623
1624 header = IPC_GLB_TYPE(IPC_GLB_ENTER_DX_STATE);
1625 state_ = state;
1626
1627 trace_ipc_request("PM enter Dx state", state);
1628
1629 ret = ipc_tx_message_wait(hsw, header, &state_, sizeof(state_),
Dan Carpenter7897ab72014-04-16 18:38:11 +03001630 dx, sizeof(*dx));
Mark Browna4b12992014-03-12 23:04:35 +00001631 if (ret < 0) {
1632 dev_err(hsw->dev, "ipc: error set dx state %d failed\n", state);
1633 return ret;
1634 }
1635
Liam Girdwood543ec632014-07-30 20:11:26 +08001636 for (item = 0; item < dx->entries_no; item++) {
1637 dev_dbg(hsw->dev,
1638 "Item[%d] offset[%x] - size[%x] - source[%x]\n",
1639 item, dx->mem_info[item].offset,
1640 dx->mem_info[item].size,
1641 dx->mem_info[item].source);
1642 }
Mark Browna4b12992014-03-12 23:04:35 +00001643 dev_dbg(hsw->dev, "ipc: got %d entry numbers for state %d\n",
1644 dx->entries_no, state);
1645
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001646 return ret;
Mark Browna4b12992014-03-12 23:04:35 +00001647}
1648
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001649struct sst_module_runtime *sst_hsw_runtime_module_create(struct sst_hsw *hsw,
1650 int mod_id, int offset)
Mark Browna4b12992014-03-12 23:04:35 +00001651{
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001652 struct sst_dsp *dsp = hsw->dsp;
1653 struct sst_module *module;
1654 struct sst_module_runtime *runtime;
1655 int err;
Mark Browna4b12992014-03-12 23:04:35 +00001656
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001657 module = sst_module_get_from_id(dsp, mod_id);
1658 if (module == NULL) {
1659 dev_err(dsp->dev, "error: failed to get module %d for pcm\n",
1660 mod_id);
1661 return NULL;
1662 }
Mark Browna4b12992014-03-12 23:04:35 +00001663
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001664 runtime = sst_module_runtime_new(module, mod_id, NULL);
1665 if (runtime == NULL) {
1666 dev_err(dsp->dev, "error: failed to create module %d runtime\n",
1667 mod_id);
1668 return NULL;
1669 }
Mark Browna4b12992014-03-12 23:04:35 +00001670
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001671 err = sst_module_runtime_alloc_blocks(runtime, offset);
1672 if (err < 0) {
1673 dev_err(dsp->dev, "error: failed to alloc blocks for module %d runtime\n",
1674 mod_id);
1675 sst_module_runtime_free(runtime);
1676 return NULL;
1677 }
Mark Browna4b12992014-03-12 23:04:35 +00001678
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001679 dev_dbg(dsp->dev, "runtime id %d created for module %d\n", runtime->id,
1680 mod_id);
1681 return runtime;
1682}
Mark Browna4b12992014-03-12 23:04:35 +00001683
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001684void sst_hsw_runtime_module_free(struct sst_module_runtime *runtime)
1685{
1686 sst_module_runtime_free_blocks(runtime);
1687 sst_module_runtime_free(runtime);
Mark Browna4b12992014-03-12 23:04:35 +00001688}
1689
Liam Girdwood35e03a82014-10-30 14:58:19 +00001690#ifdef CONFIG_PM
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001691static int sst_hsw_dx_state_dump(struct sst_hsw *hsw)
1692{
1693 struct sst_dsp *sst = hsw->dsp;
1694 u32 item, offset, size;
1695 int ret = 0;
1696
1697 trace_ipc_request("PM state dump. Items #", SST_HSW_MAX_DX_REGIONS);
1698
1699 if (hsw->dx.entries_no > SST_HSW_MAX_DX_REGIONS) {
1700 dev_err(hsw->dev,
1701 "error: number of FW context regions greater than %d\n",
1702 SST_HSW_MAX_DX_REGIONS);
1703 memset(&hsw->dx, 0, sizeof(hsw->dx));
1704 return -EINVAL;
1705 }
1706
1707 ret = sst_dsp_dma_get_channel(sst, 0);
1708 if (ret < 0) {
1709 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1710 return ret;
1711 }
1712
1713 /* set on-demond mode on engine 0 channel 3 */
1714 sst_dsp_shim_update_bits(sst, SST_HMDC,
1715 SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH,
1716 SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH);
1717
1718 for (item = 0; item < hsw->dx.entries_no; item++) {
1719 if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
1720 && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
1721 && hsw->dx.mem_info[item].offset <
1722 DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
1723
1724 offset = hsw->dx.mem_info[item].offset
1725 - DSP_DRAM_ADDR_OFFSET;
1726 size = (hsw->dx.mem_info[item].size + 3) & (~3);
1727
1728 ret = sst_dsp_dma_copyfrom(sst, hsw->dx_context_paddr + offset,
1729 sst->addr.lpe_base + offset, size);
1730 if (ret < 0) {
1731 dev_err(hsw->dev,
1732 "error: FW context dump failed\n");
1733 memset(&hsw->dx, 0, sizeof(hsw->dx));
1734 goto out;
1735 }
1736 }
1737 }
1738
1739out:
1740 sst_dsp_dma_put_channel(sst);
1741 return ret;
1742}
1743
1744static int sst_hsw_dx_state_restore(struct sst_hsw *hsw)
1745{
1746 struct sst_dsp *sst = hsw->dsp;
1747 u32 item, offset, size;
1748 int ret;
1749
1750 for (item = 0; item < hsw->dx.entries_no; item++) {
1751 if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
1752 && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
1753 && hsw->dx.mem_info[item].offset <
1754 DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
1755
1756 offset = hsw->dx.mem_info[item].offset
1757 - DSP_DRAM_ADDR_OFFSET;
1758 size = (hsw->dx.mem_info[item].size + 3) & (~3);
1759
1760 ret = sst_dsp_dma_copyto(sst, sst->addr.lpe_base + offset,
1761 hsw->dx_context_paddr + offset, size);
1762 if (ret < 0) {
1763 dev_err(hsw->dev,
1764 "error: FW context restore failed\n");
1765 return ret;
1766 }
1767 }
1768 }
1769
1770 return 0;
1771}
1772
1773static void sst_hsw_drop_all(struct sst_hsw *hsw)
1774{
1775 struct ipc_message *msg, *tmp;
1776 unsigned long flags;
1777 int tx_drop_cnt = 0, rx_drop_cnt = 0;
1778
1779 /* drop all TX and Rx messages before we stall + reset DSP */
1780 spin_lock_irqsave(&hsw->dsp->spinlock, flags);
1781
1782 list_for_each_entry_safe(msg, tmp, &hsw->tx_list, list) {
1783 list_move(&msg->list, &hsw->empty_list);
1784 tx_drop_cnt++;
1785 }
1786
1787 list_for_each_entry_safe(msg, tmp, &hsw->rx_list, list) {
1788 list_move(&msg->list, &hsw->empty_list);
1789 rx_drop_cnt++;
1790 }
1791
1792 spin_unlock_irqrestore(&hsw->dsp->spinlock, flags);
1793
1794 if (tx_drop_cnt || rx_drop_cnt)
1795 dev_err(hsw->dev, "dropped IPC msg RX=%d, TX=%d\n",
1796 tx_drop_cnt, rx_drop_cnt);
1797}
1798
1799int sst_hsw_dsp_load(struct sst_hsw *hsw)
1800{
1801 struct sst_dsp *dsp = hsw->dsp;
Lu, Han3fe06072015-02-25 08:26:21 +08001802 struct sst_fw *sst_fw, *t;
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001803 int ret;
1804
1805 dev_dbg(hsw->dev, "loading audio DSP....");
1806
1807 ret = sst_dsp_wake(dsp);
1808 if (ret < 0) {
1809 dev_err(hsw->dev, "error: failed to wake audio DSP\n");
1810 return -ENODEV;
1811 }
1812
1813 ret = sst_dsp_dma_get_channel(dsp, 0);
1814 if (ret < 0) {
1815 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1816 return ret;
1817 }
1818
Lu, Han3fe06072015-02-25 08:26:21 +08001819 list_for_each_entry_safe_reverse(sst_fw, t, &dsp->fw_list, list) {
1820 ret = sst_fw_reload(sst_fw);
1821 if (ret < 0) {
1822 dev_err(hsw->dev, "error: SST FW reload failed\n");
1823 sst_dsp_dma_put_channel(dsp);
1824 return -ENOMEM;
1825 }
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001826 }
Lu, Han3fe06072015-02-25 08:26:21 +08001827 ret = sst_block_alloc_scratch(hsw->dsp);
1828 if (ret < 0)
1829 return -EINVAL;
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001830
1831 sst_dsp_dma_put_channel(dsp);
1832 return 0;
1833}
1834
1835static int sst_hsw_dsp_restore(struct sst_hsw *hsw)
1836{
1837 struct sst_dsp *dsp = hsw->dsp;
1838 int ret;
1839
1840 dev_dbg(hsw->dev, "restoring audio DSP....");
1841
1842 ret = sst_dsp_dma_get_channel(dsp, 0);
1843 if (ret < 0) {
1844 dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
1845 return ret;
1846 }
1847
1848 ret = sst_hsw_dx_state_restore(hsw);
1849 if (ret < 0) {
1850 dev_err(hsw->dev, "error: SST FW context restore failed\n");
1851 sst_dsp_dma_put_channel(dsp);
1852 return -ENOMEM;
1853 }
1854 sst_dsp_dma_put_channel(dsp);
1855
1856 /* wait for DSP boot completion */
1857 sst_dsp_boot(dsp);
1858
1859 return ret;
1860}
1861
1862int sst_hsw_dsp_runtime_suspend(struct sst_hsw *hsw)
1863{
1864 int ret;
1865
1866 dev_dbg(hsw->dev, "audio dsp runtime suspend\n");
1867
1868 ret = sst_hsw_dx_set_state(hsw, SST_HSW_DX_STATE_D3, &hsw->dx);
1869 if (ret < 0)
1870 return ret;
1871
1872 sst_dsp_stall(hsw->dsp);
1873
1874 ret = sst_hsw_dx_state_dump(hsw);
1875 if (ret < 0)
1876 return ret;
1877
1878 sst_hsw_drop_all(hsw);
1879
1880 return 0;
1881}
1882
1883int sst_hsw_dsp_runtime_sleep(struct sst_hsw *hsw)
1884{
Lu, Han3fe06072015-02-25 08:26:21 +08001885 struct sst_fw *sst_fw, *t;
1886 struct sst_dsp *dsp = hsw->dsp;
1887
1888 list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
1889 sst_fw_unload(sst_fw);
1890 }
1891 sst_block_free_scratch(dsp);
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001892
1893 hsw->boot_complete = false;
1894
Lu, Han3fe06072015-02-25 08:26:21 +08001895 sst_dsp_sleep(dsp);
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001896
1897 return 0;
1898}
1899
1900int sst_hsw_dsp_runtime_resume(struct sst_hsw *hsw)
1901{
1902 struct device *dev = hsw->dev;
1903 int ret;
1904
1905 dev_dbg(dev, "audio dsp runtime resume\n");
1906
1907 if (hsw->boot_complete)
1908 return 1; /* tell caller no action is required */
1909
1910 ret = sst_hsw_dsp_restore(hsw);
1911 if (ret < 0)
1912 dev_err(dev, "error: audio DSP boot failure\n");
1913
Lu, Han9449d392015-03-10 10:41:20 +08001914 sst_hsw_init_module_state(hsw);
1915
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001916 ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
1917 msecs_to_jiffies(IPC_BOOT_MSECS));
1918 if (ret == 0) {
Liam Girdwoodb891f622014-10-30 14:34:00 +00001919 dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
1920 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
1921 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00001922 return -EIO;
1923 }
1924
1925 /* Set ADSP SSP port settings */
1926 ret = sst_hsw_device_set_config(hsw, SST_HSW_DEVICE_SSP_0,
1927 SST_HSW_DEVICE_MCLK_FREQ_24_MHZ,
1928 SST_HSW_DEVICE_CLOCK_MASTER, 9);
1929 if (ret < 0)
1930 dev_err(dev, "error: SSP re-initialization failed\n");
1931
1932 return ret;
1933}
1934#endif
1935
Mark Browna4b12992014-03-12 23:04:35 +00001936static int msg_empty_list_init(struct sst_hsw *hsw)
1937{
1938 int i;
1939
1940 hsw->msg = kzalloc(sizeof(struct ipc_message) *
1941 IPC_EMPTY_LIST_SIZE, GFP_KERNEL);
1942 if (hsw->msg == NULL)
1943 return -ENOMEM;
1944
1945 for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
1946 init_waitqueue_head(&hsw->msg[i].waitq);
1947 list_add(&hsw->msg[i].list, &hsw->empty_list);
1948 }
1949
1950 return 0;
1951}
1952
Mark Browna4b12992014-03-12 23:04:35 +00001953struct sst_dsp *sst_hsw_get_dsp(struct sst_hsw *hsw)
1954{
1955 return hsw->dsp;
1956}
1957
Lu, Han9449d392015-03-10 10:41:20 +08001958void sst_hsw_init_module_state(struct sst_hsw *hsw)
1959{
1960 struct sst_module *module;
1961 enum sst_hsw_module_id id;
1962
1963 /* the base fw contains several modules */
1964 for (id = SST_HSW_MODULE_BASE_FW; id < SST_HSW_MAX_MODULE_ID; id++) {
1965 module = sst_module_get_from_id(hsw->dsp, id);
Lu, Han8c43fc22015-03-10 10:41:21 +08001966 if (module) {
1967 /* module waves is active only after being enabled */
1968 if (id == SST_HSW_MODULE_WAVES)
1969 module->state = SST_MODULE_STATE_INITIALIZED;
1970 else
1971 module->state = SST_MODULE_STATE_ACTIVE;
1972 }
Lu, Han9449d392015-03-10 10:41:20 +08001973 }
1974}
1975
Lu, Han8c43fc22015-03-10 10:41:21 +08001976bool sst_hsw_is_module_loaded(struct sst_hsw *hsw, u32 module_id)
1977{
1978 struct sst_module *module;
1979
1980 module = sst_module_get_from_id(hsw->dsp, module_id);
1981 if (module == NULL || module->state == SST_MODULE_STATE_UNLOADED)
1982 return false;
1983 else
1984 return true;
1985}
1986
Lu, Hane8e79ed2015-03-10 10:41:22 +08001987bool sst_hsw_is_module_active(struct sst_hsw *hsw, u32 module_id)
1988{
1989 struct sst_module *module;
1990
1991 module = sst_module_get_from_id(hsw->dsp, module_id);
1992 if (module != NULL && module->state == SST_MODULE_STATE_ACTIVE)
1993 return true;
1994 else
1995 return false;
1996}
1997
Lu, Han76c07b82015-03-12 13:53:00 +08001998void sst_hsw_set_module_enabled_rtd3(struct sst_hsw *hsw, u32 module_id)
1999{
2000 hsw->enabled_modules_rtd3 |= (1 << module_id);
2001}
2002
2003void sst_hsw_set_module_disabled_rtd3(struct sst_hsw *hsw, u32 module_id)
2004{
2005 hsw->enabled_modules_rtd3 &= ~(1 << module_id);
2006}
2007
2008bool sst_hsw_is_module_enabled_rtd3(struct sst_hsw *hsw, u32 module_id)
2009{
2010 return hsw->enabled_modules_rtd3 & (1 << module_id);
2011}
2012
Lu, Han3814c202015-03-12 13:53:02 +08002013void sst_hsw_reset_param_buf(struct sst_hsw *hsw)
2014{
2015 hsw->param_idx_w = 0;
2016 hsw->param_idx_r = 0;
2017 memset((void *)hsw->param_buf, 0, sizeof(hsw->param_buf));
2018}
2019
2020int sst_hsw_store_param_line(struct sst_hsw *hsw, u8 *buf)
2021{
2022 /* save line to the first available position of param buffer */
2023 if (hsw->param_idx_w > WAVES_PARAM_LINES - 1) {
2024 dev_warn(hsw->dev, "warning: param buffer overflow!\n");
2025 return -EPERM;
2026 }
2027 memcpy(hsw->param_buf[hsw->param_idx_w], buf, WAVES_PARAM_COUNT);
2028 hsw->param_idx_w++;
2029 return 0;
2030}
2031
2032int sst_hsw_load_param_line(struct sst_hsw *hsw, u8 *buf)
2033{
2034 u8 id = 0;
2035
2036 /* read the first matching line from param buffer */
2037 while (hsw->param_idx_r < WAVES_PARAM_LINES) {
2038 id = hsw->param_buf[hsw->param_idx_r][0];
2039 hsw->param_idx_r++;
2040 if (buf[0] == id) {
2041 memcpy(buf, hsw->param_buf[hsw->param_idx_r],
2042 WAVES_PARAM_COUNT);
2043 break;
2044 }
2045 }
2046 if (hsw->param_idx_r > WAVES_PARAM_LINES - 1) {
2047 dev_dbg(hsw->dev, "end of buffer, roll to the beginning\n");
2048 hsw->param_idx_r = 0;
2049 return 0;
2050 }
2051 return 0;
2052}
2053
2054int sst_hsw_launch_param_buf(struct sst_hsw *hsw)
2055{
2056 int ret, idx;
2057
Lu, Han5d5b2752015-03-19 08:38:00 +08002058 if (!sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
2059 dev_dbg(hsw->dev, "module waves is not active\n");
2060 return 0;
2061 }
2062
Lu, Han3814c202015-03-12 13:53:02 +08002063 /* put all param lines to DSP through ipc */
2064 for (idx = 0; idx < hsw->param_idx_w; idx++) {
2065 ret = sst_hsw_module_set_param(hsw,
2066 SST_HSW_MODULE_WAVES, 0, hsw->param_buf[idx][0],
2067 WAVES_PARAM_COUNT, hsw->param_buf[idx]);
2068 if (ret < 0)
2069 return ret;
2070 }
2071 return 0;
2072}
2073
Lu, Han9449d392015-03-10 10:41:20 +08002074int sst_hsw_module_load(struct sst_hsw *hsw,
2075 u32 module_id, u32 instance_id, char *name)
2076{
2077 int ret = 0;
2078 const struct firmware *fw = NULL;
2079 struct sst_fw *hsw_sst_fw;
2080 struct sst_module *module;
2081 struct device *dev = hsw->dev;
2082 struct sst_dsp *dsp = hsw->dsp;
2083
2084 dev_dbg(dev, "sst_hsw_module_load id=%d, name='%s'", module_id, name);
2085
2086 module = sst_module_get_from_id(dsp, module_id);
2087 if (module == NULL) {
2088 /* loading for the first time */
2089 if (module_id == SST_HSW_MODULE_BASE_FW) {
2090 /* for base module: use fw requested in acpi probe */
2091 fw = dsp->pdata->fw;
2092 if (!fw) {
2093 dev_err(dev, "request Base fw failed\n");
2094 return -ENODEV;
2095 }
2096 } else {
2097 /* try and load any other optional modules if they are
2098 * available. Use dev_info instead of dev_err in case
2099 * request firmware failed */
2100 ret = request_firmware(&fw, name, dev);
2101 if (ret) {
2102 dev_info(dev, "fw image %s not available(%d)\n",
2103 name, ret);
2104 return ret;
2105 }
2106 }
2107 hsw_sst_fw = sst_fw_new(dsp, fw, hsw);
2108 if (hsw_sst_fw == NULL) {
2109 dev_err(dev, "error: failed to load firmware\n");
2110 ret = -ENOMEM;
2111 goto out;
2112 }
2113 module = sst_module_get_from_id(dsp, module_id);
2114 if (module == NULL) {
2115 dev_err(dev, "error: no module %d in firmware %s\n",
2116 module_id, name);
2117 }
2118 } else
2119 dev_info(dev, "module %d (%s) already loaded\n",
2120 module_id, name);
2121out:
2122 /* release fw, but base fw should be released by acpi driver */
2123 if (fw && module_id != SST_HSW_MODULE_BASE_FW)
2124 release_firmware(fw);
2125
2126 return ret;
2127}
2128
Lu, Hane8e79ed2015-03-10 10:41:22 +08002129int sst_hsw_module_enable(struct sst_hsw *hsw,
2130 u32 module_id, u32 instance_id)
2131{
2132 int ret;
2133 u32 header = 0;
2134 struct sst_hsw_ipc_module_config config;
2135 struct sst_module *module;
2136 struct sst_module_runtime *runtime;
2137 struct device *dev = hsw->dev;
2138 struct sst_dsp *dsp = hsw->dsp;
2139
2140 if (!sst_hsw_is_module_loaded(hsw, module_id)) {
2141 dev_dbg(dev, "module %d not loaded\n", module_id);
2142 return 0;
2143 }
2144
2145 if (sst_hsw_is_module_active(hsw, module_id)) {
2146 dev_info(dev, "module %d already enabled\n", module_id);
2147 return 0;
2148 }
2149
2150 module = sst_module_get_from_id(dsp, module_id);
2151 if (module == NULL) {
2152 dev_err(dev, "module %d not valid\n", module_id);
2153 return -ENXIO;
2154 }
2155
2156 runtime = sst_module_runtime_get_from_id(module, module_id);
2157 if (runtime == NULL) {
2158 dev_err(dev, "runtime %d not valid", module_id);
2159 return -ENXIO;
2160 }
2161
2162 header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
2163 IPC_MODULE_OPERATION(IPC_MODULE_ENABLE) |
2164 IPC_MODULE_ID(module_id);
2165 dev_dbg(dev, "module enable header: %x\n", header);
2166
2167 config.map.module_entries_count = 1;
2168 config.map.module_entries[0].module_id = module->id;
2169 config.map.module_entries[0].entry_point = module->entry;
2170
2171 config.persistent_mem.offset =
2172 sst_dsp_get_offset(dsp,
2173 runtime->persistent_offset, SST_MEM_DRAM);
2174 config.persistent_mem.size = module->persistent_size;
2175
2176 config.scratch_mem.offset =
2177 sst_dsp_get_offset(dsp,
2178 dsp->scratch_offset, SST_MEM_DRAM);
2179 config.scratch_mem.size = module->scratch_size;
2180 dev_dbg(dev, "mod %d enable p:%d @ %x, s:%d @ %x, ep: %x",
2181 config.map.module_entries[0].module_id,
2182 config.persistent_mem.size,
2183 config.persistent_mem.offset,
2184 config.scratch_mem.size, config.scratch_mem.offset,
2185 config.map.module_entries[0].entry_point);
2186
2187 ret = ipc_tx_message_wait(hsw, header,
2188 &config, sizeof(config), NULL, 0);
2189 if (ret < 0)
2190 dev_err(dev, "ipc: module enable failed - %d\n", ret);
2191 else
2192 module->state = SST_MODULE_STATE_ACTIVE;
2193
2194 return ret;
2195}
2196
2197int sst_hsw_module_disable(struct sst_hsw *hsw,
2198 u32 module_id, u32 instance_id)
2199{
2200 int ret;
2201 u32 header;
2202 struct sst_module *module;
2203 struct device *dev = hsw->dev;
2204 struct sst_dsp *dsp = hsw->dsp;
2205
2206 if (!sst_hsw_is_module_loaded(hsw, module_id)) {
2207 dev_dbg(dev, "module %d not loaded\n", module_id);
2208 return 0;
2209 }
2210
2211 if (!sst_hsw_is_module_active(hsw, module_id)) {
2212 dev_info(dev, "module %d already disabled\n", module_id);
2213 return 0;
2214 }
2215
2216 module = sst_module_get_from_id(dsp, module_id);
2217 if (module == NULL) {
2218 dev_err(dev, "module %d not valid\n", module_id);
2219 return -ENXIO;
2220 }
2221
2222 header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
2223 IPC_MODULE_OPERATION(IPC_MODULE_DISABLE) |
2224 IPC_MODULE_ID(module_id);
2225
2226 ret = ipc_tx_message_wait(hsw, header, NULL, 0, NULL, 0);
2227 if (ret < 0)
2228 dev_err(dev, "module disable failed - %d\n", ret);
2229 else
2230 module->state = SST_MODULE_STATE_INITIALIZED;
2231
2232 return ret;
2233}
2234
Lu, Han20189222015-03-12 13:53:01 +08002235int sst_hsw_module_set_param(struct sst_hsw *hsw,
2236 u32 module_id, u32 instance_id, u32 parameter_id,
2237 u32 param_size, char *param)
2238{
2239 int ret;
2240 unsigned char *data = NULL;
2241 u32 header = 0;
2242 u32 payload_size = 0, transfer_parameter_size = 0;
2243 dma_addr_t dma_addr = 0;
2244 struct sst_hsw_transfer_parameter *parameter;
2245 struct device *dev = hsw->dev;
2246
2247 header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
2248 IPC_MODULE_OPERATION(IPC_MODULE_SET_PARAMETER) |
2249 IPC_MODULE_ID(module_id);
2250 dev_dbg(dev, "sst_hsw_module_set_param header=%x\n", header);
2251
2252 payload_size = param_size +
2253 sizeof(struct sst_hsw_transfer_parameter) -
2254 sizeof(struct sst_hsw_transfer_list);
2255 dev_dbg(dev, "parameter size : %d\n", param_size);
2256 dev_dbg(dev, "payload size : %d\n", payload_size);
2257
2258 if (payload_size <= SST_HSW_IPC_MAX_SHORT_PARAMETER_SIZE) {
2259 /* short parameter, mailbox can contain data */
2260 dev_dbg(dev, "transfer parameter size : %d\n",
2261 transfer_parameter_size);
2262
2263 transfer_parameter_size = ALIGN(payload_size, 4);
2264 dev_dbg(dev, "transfer parameter aligned size : %d\n",
2265 transfer_parameter_size);
2266
2267 parameter = kzalloc(transfer_parameter_size, GFP_KERNEL);
2268 if (parameter == NULL)
2269 return -ENOMEM;
2270
2271 memcpy(parameter->data, param, param_size);
2272 } else {
2273 dev_warn(dev, "transfer parameter size too large!");
2274 return 0;
2275 }
2276
2277 parameter->parameter_id = parameter_id;
2278 parameter->data_size = param_size;
2279
2280 ret = ipc_tx_message_wait(hsw, header,
2281 parameter, transfer_parameter_size , NULL, 0);
2282 if (ret < 0)
2283 dev_err(dev, "ipc: module set parameter failed - %d\n", ret);
2284
2285 kfree(parameter);
2286
2287 if (data)
2288 dma_free_coherent(hsw->dsp->dma_dev,
2289 param_size, (void *)data, dma_addr);
2290
2291 return ret;
2292}
2293
Mark Browna4b12992014-03-12 23:04:35 +00002294static struct sst_dsp_device hsw_dev = {
2295 .thread = hsw_irq_thread,
2296 .ops = &haswell_ops,
2297};
2298
2299int sst_hsw_dsp_init(struct device *dev, struct sst_pdata *pdata)
2300{
2301 struct sst_hsw_ipc_fw_version version;
2302 struct sst_hsw *hsw;
Mark Browna4b12992014-03-12 23:04:35 +00002303 int ret;
2304
2305 dev_dbg(dev, "initialising Audio DSP IPC\n");
2306
2307 hsw = devm_kzalloc(dev, sizeof(*hsw), GFP_KERNEL);
2308 if (hsw == NULL)
2309 return -ENOMEM;
2310
2311 hsw->dev = dev;
2312 INIT_LIST_HEAD(&hsw->stream_list);
2313 INIT_LIST_HEAD(&hsw->tx_list);
2314 INIT_LIST_HEAD(&hsw->rx_list);
2315 INIT_LIST_HEAD(&hsw->empty_list);
2316 init_waitqueue_head(&hsw->boot_wait);
2317 init_waitqueue_head(&hsw->wait_txq);
2318
2319 ret = msg_empty_list_init(hsw);
2320 if (ret < 0)
Imre Deak9cf0e452014-05-30 10:52:29 +03002321 return -ENOMEM;
Mark Browna4b12992014-03-12 23:04:35 +00002322
2323 /* start the IPC message thread */
2324 init_kthread_worker(&hsw->kworker);
2325 hsw->tx_thread = kthread_run(kthread_worker_fn,
Kees Cook35386322014-05-22 11:43:55 -07002326 &hsw->kworker, "%s",
Mark Browna4b12992014-03-12 23:04:35 +00002327 dev_name(hsw->dev));
2328 if (IS_ERR(hsw->tx_thread)) {
2329 ret = PTR_ERR(hsw->tx_thread);
2330 dev_err(hsw->dev, "error: failed to create message TX task\n");
Imre Deak9cf0e452014-05-30 10:52:29 +03002331 goto err_free_msg;
Mark Browna4b12992014-03-12 23:04:35 +00002332 }
2333 init_kthread_work(&hsw->kwork, ipc_tx_msgs);
2334
2335 hsw_dev.thread_context = hsw;
2336
2337 /* init SST shim */
2338 hsw->dsp = sst_dsp_new(dev, &hsw_dev, pdata);
2339 if (hsw->dsp == NULL) {
2340 ret = -ENODEV;
Imre Deak9cf0e452014-05-30 10:52:29 +03002341 goto dsp_err;
Mark Browna4b12992014-03-12 23:04:35 +00002342 }
2343
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00002344 /* allocate DMA buffer for context storage */
2345 hsw->dx_context = dma_alloc_coherent(hsw->dsp->dma_dev,
2346 SST_HSW_DX_CONTEXT_SIZE, &hsw->dx_context_paddr, GFP_KERNEL);
2347 if (hsw->dx_context == NULL) {
2348 ret = -ENOMEM;
2349 goto dma_err;
2350 }
2351
Mark Browna4b12992014-03-12 23:04:35 +00002352 /* keep the DSP in reset state for base FW loading */
2353 sst_dsp_reset(hsw->dsp);
2354
Lu, Han9449d392015-03-10 10:41:20 +08002355 /* load base module and other modules in base firmware image */
2356 ret = sst_hsw_module_load(hsw, SST_HSW_MODULE_BASE_FW, 0, "Base");
2357 if (ret < 0)
Mark Browna4b12992014-03-12 23:04:35 +00002358 goto fw_err;
Mark Browna4b12992014-03-12 23:04:35 +00002359
Lu, Han8c43fc22015-03-10 10:41:21 +08002360 /* try to load module waves */
2361 sst_hsw_module_load(hsw, SST_HSW_MODULE_WAVES, 0, "intel/IntcPP01.bin");
2362
Lu, Han3fe06072015-02-25 08:26:21 +08002363 /* allocate scratch mem regions */
2364 ret = sst_block_alloc_scratch(hsw->dsp);
2365 if (ret < 0)
2366 goto boot_err;
2367
Lu, Han3814c202015-03-12 13:53:02 +08002368 /* init param buffer */
2369 sst_hsw_reset_param_buf(hsw);
2370
Mark Browna4b12992014-03-12 23:04:35 +00002371 /* wait for DSP boot completion */
2372 sst_dsp_boot(hsw->dsp);
2373 ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
2374 msecs_to_jiffies(IPC_BOOT_MSECS));
2375 if (ret == 0) {
2376 ret = -EIO;
Liam Girdwoodb891f622014-10-30 14:34:00 +00002377 dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
2378 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
2379 sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
Mark Browna4b12992014-03-12 23:04:35 +00002380 goto boot_err;
2381 }
2382
Lu, Han9449d392015-03-10 10:41:20 +08002383 /* init module state after boot */
2384 sst_hsw_init_module_state(hsw);
2385
Mark Browna4b12992014-03-12 23:04:35 +00002386 /* get the FW version */
2387 sst_hsw_fw_get_version(hsw, &version);
Mark Browna4b12992014-03-12 23:04:35 +00002388
2389 /* get the globalmixer */
2390 ret = sst_hsw_mixer_get_info(hsw);
2391 if (ret < 0) {
2392 dev_err(hsw->dev, "error: failed to get stream info\n");
2393 goto boot_err;
2394 }
2395
2396 pdata->dsp = hsw;
2397 return 0;
2398
2399boot_err:
2400 sst_dsp_reset(hsw->dsp);
Lu, Han9449d392015-03-10 10:41:20 +08002401 sst_fw_free_all(hsw->dsp);
Mark Browna4b12992014-03-12 23:04:35 +00002402fw_err:
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00002403 dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
2404 hsw->dx_context, hsw->dx_context_paddr);
2405dma_err:
Mark Browna4b12992014-03-12 23:04:35 +00002406 sst_dsp_free(hsw->dsp);
Imre Deak9cf0e452014-05-30 10:52:29 +03002407dsp_err:
2408 kthread_stop(hsw->tx_thread);
2409err_free_msg:
Mark Browna4b12992014-03-12 23:04:35 +00002410 kfree(hsw->msg);
Imre Deak9cf0e452014-05-30 10:52:29 +03002411
Mark Browna4b12992014-03-12 23:04:35 +00002412 return ret;
2413}
2414EXPORT_SYMBOL_GPL(sst_hsw_dsp_init);
2415
2416void sst_hsw_dsp_free(struct device *dev, struct sst_pdata *pdata)
2417{
2418 struct sst_hsw *hsw = pdata->dsp;
2419
2420 sst_dsp_reset(hsw->dsp);
2421 sst_fw_free_all(hsw->dsp);
Liam Girdwoodaed3c7b2014-10-29 17:40:42 +00002422 dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
2423 hsw->dx_context, hsw->dx_context_paddr);
Mark Browna4b12992014-03-12 23:04:35 +00002424 sst_dsp_free(hsw->dsp);
Imre Deak9cf0e452014-05-30 10:52:29 +03002425 kthread_stop(hsw->tx_thread);
Mark Browna4b12992014-03-12 23:04:35 +00002426 kfree(hsw->msg);
2427}
2428EXPORT_SYMBOL_GPL(sst_hsw_dsp_free);