Merge branch 'drm-sti-next-add-dvo' of git://git.linaro.org/people/benjamin.gaignard...
[linux-2.6-block.git] / sound / soc / intel / sst / sst_pvt.c
1 /*
2  *  sst_pvt.c - Intel SST Driver for audio engine
3  *
4  *  Copyright (C) 2008-14       Intel Corp
5  *  Authors:    Vinod Koul <vinod.koul@intel.com>
6  *              Harsha Priya <priya.harsha@intel.com>
7  *              Dharageswari R <dharageswari.r@intel.com>
8  *              KP Jeeja <jeeja.kp@intel.com>
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; version 2 of the License.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21  */
22 #include <linux/kobject.h>
23 #include <linux/pci.h>
24 #include <linux/fs.h>
25 #include <linux/firmware.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/sched.h>
28 #include <linux/delay.h>
29 #include <sound/asound.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/soc.h>
33 #include <sound/compress_driver.h>
34 #include <asm/platform_sst_audio.h>
35 #include "../sst-mfld-platform.h"
36 #include "sst.h"
37 #include "../sst-dsp.h"
38
39 int sst_shim_write(void __iomem *addr, int offset, int value)
40 {
41         writel(value, addr + offset);
42         return 0;
43 }
44
45 u32 sst_shim_read(void __iomem *addr, int offset)
46 {
47         return readl(addr + offset);
48 }
49
50 u64 sst_reg_read64(void __iomem *addr, int offset)
51 {
52         u64 val = 0;
53
54         memcpy_fromio(&val, addr + offset, sizeof(val));
55
56         return val;
57 }
58
59 int sst_shim_write64(void __iomem *addr, int offset, u64 value)
60 {
61         memcpy_toio(addr + offset, &value, sizeof(value));
62         return 0;
63 }
64
65 u64 sst_shim_read64(void __iomem *addr, int offset)
66 {
67         u64 val = 0;
68
69         memcpy_fromio(&val, addr + offset, sizeof(val));
70         return val;
71 }
72
73 void sst_set_fw_state_locked(
74                 struct intel_sst_drv *sst_drv_ctx, int sst_state)
75 {
76         mutex_lock(&sst_drv_ctx->sst_lock);
77         sst_drv_ctx->sst_state = sst_state;
78         mutex_unlock(&sst_drv_ctx->sst_lock);
79 }
80
81 /*
82  * sst_wait_interruptible - wait on event
83  *
84  * @sst_drv_ctx: Driver context
85  * @block: Driver block to wait on
86  *
87  * This function waits without a timeout (and is interruptable) for a
88  * given block event
89  */
90 int sst_wait_interruptible(struct intel_sst_drv *sst_drv_ctx,
91                                 struct sst_block *block)
92 {
93         int retval = 0;
94
95         if (!wait_event_interruptible(sst_drv_ctx->wait_queue,
96                                 block->condition)) {
97                 /* event wake */
98                 if (block->ret_code < 0) {
99                         dev_err(sst_drv_ctx->dev,
100                                 "stream failed %d\n", block->ret_code);
101                         retval = -EBUSY;
102                 } else {
103                         dev_dbg(sst_drv_ctx->dev, "event up\n");
104                         retval = 0;
105                 }
106         } else {
107                 dev_err(sst_drv_ctx->dev, "signal interrupted\n");
108                 retval = -EINTR;
109         }
110         return retval;
111
112 }
113
114 unsigned long long read_shim_data(struct intel_sst_drv *sst, int addr)
115 {
116         unsigned long long val = 0;
117
118         switch (sst->dev_id) {
119         case SST_MRFLD_PCI_ID:
120         case SST_BYT_ACPI_ID:
121                 val = sst_shim_read64(sst->shim, addr);
122                 break;
123         }
124         return val;
125 }
126
127 void write_shim_data(struct intel_sst_drv *sst, int addr,
128                                 unsigned long long data)
129 {
130         switch (sst->dev_id) {
131         case SST_MRFLD_PCI_ID:
132         case SST_BYT_ACPI_ID:
133                 sst_shim_write64(sst->shim, addr, (u64) data);
134                 break;
135         }
136 }
137
138 /*
139  * sst_wait_timeout - wait on event for timeout
140  *
141  * @sst_drv_ctx: Driver context
142  * @block: Driver block to wait on
143  *
144  * This function waits with a timeout value (and is not interruptible) on a
145  * given block event
146  */
147 int sst_wait_timeout(struct intel_sst_drv *sst_drv_ctx, struct sst_block *block)
148 {
149         int retval = 0;
150
151         /*
152          * NOTE:
153          * Observed that FW processes the alloc msg and replies even
154          * before the alloc thread has finished execution
155          */
156         dev_dbg(sst_drv_ctx->dev,
157                 "waiting for condition %x ipc %d drv_id %d\n",
158                 block->condition, block->msg_id, block->drv_id);
159         if (wait_event_timeout(sst_drv_ctx->wait_queue,
160                                 block->condition,
161                                 msecs_to_jiffies(SST_BLOCK_TIMEOUT))) {
162                 /* event wake */
163                 dev_dbg(sst_drv_ctx->dev, "Event wake %x\n",
164                                 block->condition);
165                 dev_dbg(sst_drv_ctx->dev, "message ret: %d\n",
166                                 block->ret_code);
167                 retval = -block->ret_code;
168         } else {
169                 block->on = false;
170                 dev_err(sst_drv_ctx->dev,
171                         "Wait timed-out condition:%#x, msg_id:%#x fw_state %#x\n",
172                         block->condition, block->msg_id, sst_drv_ctx->sst_state);
173                 sst_drv_ctx->sst_state = SST_RESET;
174
175                 retval = -EBUSY;
176         }
177         return retval;
178 }
179
180 /*
181  * sst_create_ipc_msg - create a IPC message
182  *
183  * @arg: ipc message
184  * @large: large or short message
185  *
186  * this function allocates structures to send a large or short
187  * message to the firmware
188  */
189 int sst_create_ipc_msg(struct ipc_post **arg, bool large)
190 {
191         struct ipc_post *msg;
192
193         msg = kzalloc(sizeof(struct ipc_post), GFP_ATOMIC);
194         if (!msg)
195                 return -ENOMEM;
196         if (large) {
197                 msg->mailbox_data = kzalloc(SST_MAILBOX_SIZE, GFP_ATOMIC);
198                 if (!msg->mailbox_data) {
199                         kfree(msg);
200                         return -ENOMEM;
201                 }
202         } else {
203                 msg->mailbox_data = NULL;
204         }
205         msg->is_large = large;
206         *arg = msg;
207         return 0;
208 }
209
210 /*
211  * sst_create_block_and_ipc_msg - Creates IPC message and sst block
212  * @arg: passed to sst_create_ipc_message API
213  * @large: large or short message
214  * @sst_drv_ctx: sst driver context
215  * @block: return block allocated
216  * @msg_id: IPC
217  * @drv_id: stream id or private id
218  */
219 int sst_create_block_and_ipc_msg(struct ipc_post **arg, bool large,
220                 struct intel_sst_drv *sst_drv_ctx, struct sst_block **block,
221                 u32 msg_id, u32 drv_id)
222 {
223         int retval = 0;
224
225         retval = sst_create_ipc_msg(arg, large);
226         if (retval)
227                 return retval;
228         *block = sst_create_block(sst_drv_ctx, msg_id, drv_id);
229         if (*block == NULL) {
230                 kfree(*arg);
231                 return -ENOMEM;
232         }
233         return retval;
234 }
235
236 /*
237  * sst_clean_stream - clean the stream context
238  *
239  * @stream: stream structure
240  *
241  * this function resets the stream contexts
242  * should be called in free
243  */
244 void sst_clean_stream(struct stream_info *stream)
245 {
246         stream->status = STREAM_UN_INIT;
247         stream->prev = STREAM_UN_INIT;
248         mutex_lock(&stream->lock);
249         stream->cumm_bytes = 0;
250         mutex_unlock(&stream->lock);
251 }
252
253 int sst_prepare_and_post_msg(struct intel_sst_drv *sst,
254                 int task_id, int ipc_msg, int cmd_id, int pipe_id,
255                 size_t mbox_data_len, const void *mbox_data, void **data,
256                 bool large, bool fill_dsp, bool sync, bool response)
257 {
258         struct ipc_post *msg = NULL;
259         struct ipc_dsp_hdr dsp_hdr;
260         struct sst_block *block;
261         int ret = 0, pvt_id;
262
263         pvt_id = sst_assign_pvt_id(sst);
264         if (pvt_id < 0)
265                 return pvt_id;
266
267         if (response)
268                 ret = sst_create_block_and_ipc_msg(
269                                 &msg, large, sst, &block, ipc_msg, pvt_id);
270         else
271                 ret = sst_create_ipc_msg(&msg, large);
272
273         if (ret < 0) {
274                 test_and_clear_bit(pvt_id, &sst->pvt_id);
275                 return -ENOMEM;
276         }
277
278         dev_dbg(sst->dev, "pvt_id = %d, pipe id = %d, task = %d ipc_msg: %d\n",
279                  pvt_id, pipe_id, task_id, ipc_msg);
280         sst_fill_header_mrfld(&msg->mrfld_header, ipc_msg,
281                                         task_id, large, pvt_id);
282         msg->mrfld_header.p.header_low_payload = sizeof(dsp_hdr) + mbox_data_len;
283         msg->mrfld_header.p.header_high.part.res_rqd = !sync;
284         dev_dbg(sst->dev, "header:%x\n",
285                         msg->mrfld_header.p.header_high.full);
286         dev_dbg(sst->dev, "response rqd: %x",
287                         msg->mrfld_header.p.header_high.part.res_rqd);
288         dev_dbg(sst->dev, "msg->mrfld_header.p.header_low_payload:%d",
289                         msg->mrfld_header.p.header_low_payload);
290         if (fill_dsp) {
291                 sst_fill_header_dsp(&dsp_hdr, cmd_id, pipe_id, mbox_data_len);
292                 memcpy(msg->mailbox_data, &dsp_hdr, sizeof(dsp_hdr));
293                 if (mbox_data_len) {
294                         memcpy(msg->mailbox_data + sizeof(dsp_hdr),
295                                         mbox_data, mbox_data_len);
296                 }
297         }
298
299         if (sync)
300                 sst->ops->post_message(sst, msg, true);
301         else
302                 sst_add_to_dispatch_list_and_post(sst, msg);
303
304         if (response) {
305                 ret = sst_wait_timeout(sst, block);
306                 if (ret < 0) {
307                         goto out;
308                 } else if(block->data) {
309                         if (!data)
310                                 goto out;
311                         *data = kzalloc(block->size, GFP_KERNEL);
312                         if (!(*data)) {
313                                 ret = -ENOMEM;
314                                 goto out;
315                         } else
316                                 memcpy(data, (void *) block->data, block->size);
317                 }
318         }
319 out:
320         if (response)
321                 sst_free_block(sst, block);
322         test_and_clear_bit(pvt_id, &sst->pvt_id);
323         return ret;
324 }
325
326 int sst_pm_runtime_put(struct intel_sst_drv *sst_drv)
327 {
328         int ret;
329
330         pm_runtime_mark_last_busy(sst_drv->dev);
331         ret = pm_runtime_put_autosuspend(sst_drv->dev);
332         if (ret < 0)
333                 return ret;
334         return 0;
335 }
336
337 void sst_fill_header_mrfld(union ipc_header_mrfld *header,
338                                 int msg, int task_id, int large, int drv_id)
339 {
340         header->full = 0;
341         header->p.header_high.part.msg_id = msg;
342         header->p.header_high.part.task_id = task_id;
343         header->p.header_high.part.large = large;
344         header->p.header_high.part.drv_id = drv_id;
345         header->p.header_high.part.done = 0;
346         header->p.header_high.part.busy = 1;
347         header->p.header_high.part.res_rqd = 1;
348 }
349
350 void sst_fill_header_dsp(struct ipc_dsp_hdr *dsp, int msg,
351                                         int pipe_id, int len)
352 {
353         dsp->cmd_id = msg;
354         dsp->mod_index_id = 0xff;
355         dsp->pipe_id = pipe_id;
356         dsp->length = len;
357         dsp->mod_id = 0;
358 }
359
360 #define SST_MAX_BLOCKS 15
361 /*
362  * sst_assign_pvt_id - assign a pvt id for stream
363  *
364  * @sst_drv_ctx : driver context
365  *
366  * this function assigns a private id for calls that dont have stream
367  * context yet, should be called with lock held
368  * uses bits for the id, and finds first free bits and assigns that
369  */
370 int sst_assign_pvt_id(struct intel_sst_drv *drv)
371 {
372         int local;
373
374         spin_lock(&drv->block_lock);
375         /* find first zero index from lsb */
376         local = ffz(drv->pvt_id);
377         dev_dbg(drv->dev, "pvt_id assigned --> %d\n", local);
378         if (local >= SST_MAX_BLOCKS){
379                 spin_unlock(&drv->block_lock);
380                 dev_err(drv->dev, "PVT _ID error: no free id blocks ");
381                 return -EINVAL;
382         }
383         /* toggle the index */
384         change_bit(local, &drv->pvt_id);
385         spin_unlock(&drv->block_lock);
386         return local;
387 }
388
389 void sst_init_stream(struct stream_info *stream,
390                 int codec, int sst_id, int ops, u8 slot)
391 {
392         stream->status = STREAM_INIT;
393         stream->prev = STREAM_UN_INIT;
394         stream->ops = ops;
395 }
396
397 int sst_validate_strid(
398                 struct intel_sst_drv *sst_drv_ctx, int str_id)
399 {
400         if (str_id <= 0 || str_id > sst_drv_ctx->info.max_streams) {
401                 dev_err(sst_drv_ctx->dev,
402                         "SST ERR: invalid stream id : %d, max %d\n",
403                         str_id, sst_drv_ctx->info.max_streams);
404                 return -EINVAL;
405         }
406
407         return 0;
408 }
409
410 struct stream_info *get_stream_info(
411                 struct intel_sst_drv *sst_drv_ctx, int str_id)
412 {
413         if (sst_validate_strid(sst_drv_ctx, str_id))
414                 return NULL;
415         return &sst_drv_ctx->streams[str_id];
416 }
417
418 int get_stream_id_mrfld(struct intel_sst_drv *sst_drv_ctx,
419                 u32 pipe_id)
420 {
421         int i;
422
423         for (i = 1; i <= sst_drv_ctx->info.max_streams; i++)
424                 if (pipe_id == sst_drv_ctx->streams[i].pipe_id)
425                         return i;
426
427         dev_dbg(sst_drv_ctx->dev, "no such pipe_id(%u)", pipe_id);
428         return -1;
429 }
430
431 u32 relocate_imr_addr_mrfld(u32 base_addr)
432 {
433         /* Get the difference from 512MB aligned base addr */
434         /* relocate the base */
435         base_addr = MRFLD_FW_VIRTUAL_BASE + (base_addr % (512 * 1024 * 1024));
436         return base_addr;
437 }
438 EXPORT_SYMBOL_GPL(relocate_imr_addr_mrfld);
439
440 void sst_add_to_dispatch_list_and_post(struct intel_sst_drv *sst,
441                                                 struct ipc_post *msg)
442 {
443         unsigned long irq_flags;
444
445         spin_lock_irqsave(&sst->ipc_spin_lock, irq_flags);
446         list_add_tail(&msg->node, &sst->ipc_dispatch_list);
447         spin_unlock_irqrestore(&sst->ipc_spin_lock, irq_flags);
448         sst->ops->post_message(sst, NULL, false);
449 }