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 "../../common/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 /* 115 * sst_wait_timeout - wait on event for timeout 116 * 117 * @sst_drv_ctx: Driver context 118 * @block: Driver block to wait on 119 * 120 * This function waits with a timeout value (and is not interruptible) on a 121 * given block event 122 */ 123 int sst_wait_timeout(struct intel_sst_drv *sst_drv_ctx, struct sst_block *block) 124 { 125 int retval = 0; 126 127 /* 128 * NOTE: 129 * Observed that FW processes the alloc msg and replies even 130 * before the alloc thread has finished execution 131 */ 132 dev_dbg(sst_drv_ctx->dev, 133 "waiting for condition %x ipc %d drv_id %d\n", 134 block->condition, block->msg_id, block->drv_id); 135 if (wait_event_timeout(sst_drv_ctx->wait_queue, 136 block->condition, 137 msecs_to_jiffies(SST_BLOCK_TIMEOUT))) { 138 /* event wake */ 139 dev_dbg(sst_drv_ctx->dev, "Event wake %x\n", 140 block->condition); 141 dev_dbg(sst_drv_ctx->dev, "message ret: %d\n", 142 block->ret_code); 143 retval = -block->ret_code; 144 } else { 145 block->on = false; 146 dev_err(sst_drv_ctx->dev, 147 "Wait timed-out condition:%#x, msg_id:%#x fw_state %#x\n", 148 block->condition, block->msg_id, sst_drv_ctx->sst_state); 149 sst_drv_ctx->sst_state = SST_RESET; 150 151 retval = -EBUSY; 152 } 153 return retval; 154 } 155 156 /* 157 * sst_create_ipc_msg - create a IPC message 158 * 159 * @arg: ipc message 160 * @large: large or short message 161 * 162 * this function allocates structures to send a large or short 163 * message to the firmware 164 */ 165 int sst_create_ipc_msg(struct ipc_post **arg, bool large) 166 { 167 struct ipc_post *msg; 168 169 msg = kzalloc(sizeof(*msg), GFP_KERNEL); 170 if (!msg) 171 return -ENOMEM; 172 if (large) { 173 msg->mailbox_data = kzalloc(SST_MAILBOX_SIZE, GFP_KERNEL); 174 if (!msg->mailbox_data) { 175 kfree(msg); 176 return -ENOMEM; 177 } 178 } else { 179 msg->mailbox_data = NULL; 180 } 181 msg->is_large = large; 182 *arg = msg; 183 return 0; 184 } 185 186 /* 187 * sst_create_block_and_ipc_msg - Creates IPC message and sst block 188 * @arg: passed to sst_create_ipc_message API 189 * @large: large or short message 190 * @sst_drv_ctx: sst driver context 191 * @block: return block allocated 192 * @msg_id: IPC 193 * @drv_id: stream id or private id 194 */ 195 int sst_create_block_and_ipc_msg(struct ipc_post **arg, bool large, 196 struct intel_sst_drv *sst_drv_ctx, struct sst_block **block, 197 u32 msg_id, u32 drv_id) 198 { 199 int retval = 0; 200 201 retval = sst_create_ipc_msg(arg, large); 202 if (retval) 203 return retval; 204 *block = sst_create_block(sst_drv_ctx, msg_id, drv_id); 205 if (*block == NULL) { 206 kfree(*arg); 207 return -ENOMEM; 208 } 209 return retval; 210 } 211 212 /* 213 * sst_clean_stream - clean the stream context 214 * 215 * @stream: stream structure 216 * 217 * this function resets the stream contexts 218 * should be called in free 219 */ 220 void sst_clean_stream(struct stream_info *stream) 221 { 222 stream->status = STREAM_UN_INIT; 223 stream->prev = STREAM_UN_INIT; 224 mutex_lock(&stream->lock); 225 stream->cumm_bytes = 0; 226 mutex_unlock(&stream->lock); 227 } 228 229 int sst_prepare_and_post_msg(struct intel_sst_drv *sst, 230 int task_id, int ipc_msg, int cmd_id, int pipe_id, 231 size_t mbox_data_len, const void *mbox_data, void **data, 232 bool large, bool fill_dsp, bool sync, bool response) 233 { 234 struct ipc_post *msg = NULL; 235 struct ipc_dsp_hdr dsp_hdr; 236 struct sst_block *block; 237 int ret = 0, pvt_id; 238 239 pvt_id = sst_assign_pvt_id(sst); 240 if (pvt_id < 0) 241 return pvt_id; 242 243 if (response) 244 ret = sst_create_block_and_ipc_msg( 245 &msg, large, sst, &block, ipc_msg, pvt_id); 246 else 247 ret = sst_create_ipc_msg(&msg, large); 248 249 if (ret < 0) { 250 test_and_clear_bit(pvt_id, &sst->pvt_id); 251 return -ENOMEM; 252 } 253 254 dev_dbg(sst->dev, "pvt_id = %d, pipe id = %d, task = %d ipc_msg: %d\n", 255 pvt_id, pipe_id, task_id, ipc_msg); 256 sst_fill_header_mrfld(&msg->mrfld_header, ipc_msg, 257 task_id, large, pvt_id); 258 msg->mrfld_header.p.header_low_payload = sizeof(dsp_hdr) + mbox_data_len; 259 msg->mrfld_header.p.header_high.part.res_rqd = !sync; 260 dev_dbg(sst->dev, "header:%x\n", 261 msg->mrfld_header.p.header_high.full); 262 dev_dbg(sst->dev, "response rqd: %x", 263 msg->mrfld_header.p.header_high.part.res_rqd); 264 dev_dbg(sst->dev, "msg->mrfld_header.p.header_low_payload:%d", 265 msg->mrfld_header.p.header_low_payload); 266 if (fill_dsp) { 267 sst_fill_header_dsp(&dsp_hdr, cmd_id, pipe_id, mbox_data_len); 268 memcpy(msg->mailbox_data, &dsp_hdr, sizeof(dsp_hdr)); 269 if (mbox_data_len) { 270 memcpy(msg->mailbox_data + sizeof(dsp_hdr), 271 mbox_data, mbox_data_len); 272 } 273 } 274 275 if (sync) 276 sst->ops->post_message(sst, msg, true); 277 else 278 sst_add_to_dispatch_list_and_post(sst, msg); 279 280 if (response) { 281 ret = sst_wait_timeout(sst, block); 282 if (ret < 0) 283 goto out; 284 285 if (data && block->data) { 286 *data = kmemdup(block->data, block->size, GFP_KERNEL); 287 if (!*data) { 288 ret = -ENOMEM; 289 goto out; 290 } 291 } 292 } 293 out: 294 if (response) 295 sst_free_block(sst, block); 296 test_and_clear_bit(pvt_id, &sst->pvt_id); 297 return ret; 298 } 299 300 int sst_pm_runtime_put(struct intel_sst_drv *sst_drv) 301 { 302 int ret; 303 304 pm_runtime_mark_last_busy(sst_drv->dev); 305 ret = pm_runtime_put_autosuspend(sst_drv->dev); 306 if (ret < 0) 307 return ret; 308 return 0; 309 } 310 311 void sst_fill_header_mrfld(union ipc_header_mrfld *header, 312 int msg, int task_id, int large, int drv_id) 313 { 314 header->full = 0; 315 header->p.header_high.part.msg_id = msg; 316 header->p.header_high.part.task_id = task_id; 317 header->p.header_high.part.large = large; 318 header->p.header_high.part.drv_id = drv_id; 319 header->p.header_high.part.done = 0; 320 header->p.header_high.part.busy = 1; 321 header->p.header_high.part.res_rqd = 1; 322 } 323 324 void sst_fill_header_dsp(struct ipc_dsp_hdr *dsp, int msg, 325 int pipe_id, int len) 326 { 327 dsp->cmd_id = msg; 328 dsp->mod_index_id = 0xff; 329 dsp->pipe_id = pipe_id; 330 dsp->length = len; 331 dsp->mod_id = 0; 332 } 333 334 #define SST_MAX_BLOCKS 15 335 /* 336 * sst_assign_pvt_id - assign a pvt id for stream 337 * 338 * @sst_drv_ctx : driver context 339 * 340 * this function assigns a private id for calls that dont have stream 341 * context yet, should be called with lock held 342 * uses bits for the id, and finds first free bits and assigns that 343 */ 344 int sst_assign_pvt_id(struct intel_sst_drv *drv) 345 { 346 int local; 347 348 spin_lock(&drv->block_lock); 349 /* find first zero index from lsb */ 350 local = ffz(drv->pvt_id); 351 dev_dbg(drv->dev, "pvt_id assigned --> %d\n", local); 352 if (local >= SST_MAX_BLOCKS){ 353 spin_unlock(&drv->block_lock); 354 dev_err(drv->dev, "PVT _ID error: no free id blocks "); 355 return -EINVAL; 356 } 357 /* toggle the index */ 358 change_bit(local, &drv->pvt_id); 359 spin_unlock(&drv->block_lock); 360 return local; 361 } 362 363 int sst_validate_strid( 364 struct intel_sst_drv *sst_drv_ctx, int str_id) 365 { 366 if (str_id <= 0 || str_id > sst_drv_ctx->info.max_streams) { 367 dev_err(sst_drv_ctx->dev, 368 "SST ERR: invalid stream id : %d, max %d\n", 369 str_id, sst_drv_ctx->info.max_streams); 370 return -EINVAL; 371 } 372 373 return 0; 374 } 375 376 struct stream_info *get_stream_info( 377 struct intel_sst_drv *sst_drv_ctx, int str_id) 378 { 379 if (sst_validate_strid(sst_drv_ctx, str_id)) 380 return NULL; 381 return &sst_drv_ctx->streams[str_id]; 382 } 383 384 int get_stream_id_mrfld(struct intel_sst_drv *sst_drv_ctx, 385 u32 pipe_id) 386 { 387 int i; 388 389 for (i = 1; i <= sst_drv_ctx->info.max_streams; i++) 390 if (pipe_id == sst_drv_ctx->streams[i].pipe_id) 391 return i; 392 393 dev_dbg(sst_drv_ctx->dev, "no such pipe_id(%u)", pipe_id); 394 return -1; 395 } 396 397 u32 relocate_imr_addr_mrfld(u32 base_addr) 398 { 399 /* Get the difference from 512MB aligned base addr */ 400 /* relocate the base */ 401 base_addr = MRFLD_FW_VIRTUAL_BASE + (base_addr % (512 * 1024 * 1024)); 402 return base_addr; 403 } 404 EXPORT_SYMBOL_GPL(relocate_imr_addr_mrfld); 405 406 void sst_add_to_dispatch_list_and_post(struct intel_sst_drv *sst, 407 struct ipc_post *msg) 408 { 409 unsigned long irq_flags; 410 411 spin_lock_irqsave(&sst->ipc_spin_lock, irq_flags); 412 list_add_tail(&msg->node, &sst->ipc_dispatch_list); 413 spin_unlock_irqrestore(&sst->ipc_spin_lock, irq_flags); 414 sst->ops->post_message(sst, NULL, false); 415 } 416