1 /*
2  *  intel_sst_dsp.c - Intel SST Driver for audio engine
3  *
4  *  Copyright (C) 2008-10	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  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *
26  *  This driver exposes the audio engine functionalities to the ALSA
27  *	and middleware.
28  *
29  *  This file contains all dsp controlling functions like firmware download,
30  * setting/resetting dsp cores, etc
31  */
32 
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 
35 #include <linux/pci.h>
36 #include <linux/fs.h>
37 #include <linux/firmware.h>
38 #include "intel_sst.h"
39 #include "intel_sst_ioctl.h"
40 #include "intel_sst_fw_ipc.h"
41 #include "intel_sst_common.h"
42 
43 
44 /**
45  * intel_sst_reset_dsp_mrst - Resetting SST DSP
46  *
47  * This resets DSP in case of MRST platfroms
48  */
intel_sst_reset_dsp_mrst(void)49 static int intel_sst_reset_dsp_mrst(void)
50 {
51 	union config_status_reg csr;
52 
53 	pr_debug("Resetting the DSP in mrst\n");
54 	csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
55 	csr.full |= 0x382;
56 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
57 	csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
58 	csr.part.strb_cntr_rst = 0;
59 	csr.part.run_stall = 0x1;
60 	csr.part.bypass = 0x7;
61 	csr.part.sst_reset = 0x1;
62 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
63 	return 0;
64 }
65 
66 /**
67  * intel_sst_reset_dsp_medfield - Resetting SST DSP
68  *
69  * This resets DSP in case of Medfield platfroms
70  */
intel_sst_reset_dsp_medfield(void)71 static int intel_sst_reset_dsp_medfield(void)
72 {
73 	union config_status_reg csr;
74 
75 	pr_debug("Resetting the DSP in medfield\n");
76 	csr.full = 0x048303E2;
77 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
78 
79 	return 0;
80 }
81 
82 /**
83  * sst_start_mrst - Start the SST DSP processor
84  *
85  * This starts the DSP in MRST platfroms
86  */
sst_start_mrst(void)87 static int sst_start_mrst(void)
88 {
89 	union config_status_reg csr;
90 
91 	csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
92 	csr.part.bypass = 0;
93 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
94 	csr.part.run_stall = 0;
95 	csr.part.sst_reset = 0;
96 	csr.part.strb_cntr_rst = 1;
97 	pr_debug("Setting SST to execute_mrst 0x%x\n", csr.full);
98 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
99 
100 	return 0;
101 }
102 
103 /**
104  * sst_start_medfield - Start the SST DSP processor
105  *
106  * This starts the DSP in MRST platfroms
107  */
sst_start_medfield(void)108 static int sst_start_medfield(void)
109 {
110 	union config_status_reg csr;
111 
112 	csr.full = 0x04830062;
113 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
114 	csr.full = 0x04830063;
115 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
116 	csr.full = 0x04830061;
117 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
118 	pr_debug("Starting the DSP_medfld\n");
119 
120 	return 0;
121 }
122 
123 /**
124  * sst_parse_module - Parse audio FW modules
125  *
126  * @module: FW module header
127  *
128  * Parses modules that need to be placed in SST IRAM and DRAM
129  * returns error or 0 if module sizes are proper
130  */
sst_parse_module(struct fw_module_header * module)131 static int sst_parse_module(struct fw_module_header *module)
132 {
133 	struct dma_block_info *block;
134 	u32 count;
135 	void __iomem *ram;
136 
137 	pr_debug("module sign %s size %x blocks %x type %x\n",
138 			module->signature, module->mod_size,
139 			module->blocks, module->type);
140 	pr_debug("module entrypoint 0x%x\n", module->entry_point);
141 
142 	block = (void *)module + sizeof(*module);
143 
144 	for (count = 0; count < module->blocks; count++) {
145 		if (block->size <= 0) {
146 			pr_err("block size invalid\n");
147 			return -EINVAL;
148 		}
149 		switch (block->type) {
150 		case SST_IRAM:
151 			ram = sst_drv_ctx->iram;
152 			break;
153 		case SST_DRAM:
154 			ram = sst_drv_ctx->dram;
155 			break;
156 		default:
157 			pr_err("wrong ram type0x%x in block0x%x\n",
158 					block->type, count);
159 			return -EINVAL;
160 		}
161 		memcpy_toio(ram + block->ram_offset,
162 				(void *)block + sizeof(*block), block->size);
163 		block = (void *)block + sizeof(*block) + block->size;
164 	}
165 	return 0;
166 }
167 
168 /**
169  * sst_parse_fw_image - parse and load FW
170  *
171  * @sst_fw: pointer to audio fw
172  *
173  * This function is called to parse and download the FW image
174  */
sst_parse_fw_image(const struct firmware * sst_fw)175 static int sst_parse_fw_image(const struct firmware *sst_fw)
176 {
177 	struct fw_header *header;
178 	u32 count;
179 	int ret_val;
180 	struct fw_module_header *module;
181 
182 	BUG_ON(!sst_fw);
183 
184 	/* Read the header information from the data pointer */
185 	header = (struct fw_header *)sst_fw->data;
186 
187 	/* verify FW */
188 	if ((strncmp(header->signature, SST_FW_SIGN, 4) != 0) ||
189 			(sst_fw->size != header->file_size + sizeof(*header))) {
190 		/* Invalid FW signature */
191 		pr_err("Invalid FW sign/filesize mismatch\n");
192 		return -EINVAL;
193 	}
194 	pr_debug("header sign=%s size=%x modules=%x fmt=%x size=%x\n",
195 			header->signature, header->file_size, header->modules,
196 			header->file_format, sizeof(*header));
197 	module = (void *)sst_fw->data + sizeof(*header);
198 	for (count = 0; count < header->modules; count++) {
199 		/* module */
200 		ret_val = sst_parse_module(module);
201 		if (ret_val)
202 			return ret_val;
203 		module = (void *)module + sizeof(*module) + module->mod_size ;
204 	}
205 
206 	return 0;
207 }
208 
209 /**
210  * sst_load_fw - function to load FW into DSP
211  *
212  * @fw: Pointer to driver loaded FW
213  * @context: driver context
214  *
215  * This function is called by OS when the FW is loaded into kernel
216  */
sst_load_fw(const struct firmware * fw,void * context)217 int sst_load_fw(const struct firmware *fw, void *context)
218 {
219 	int ret_val;
220 
221 	pr_debug("load_fw called\n");
222 	BUG_ON(!fw);
223 
224 	if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
225 		ret_val = intel_sst_reset_dsp_mrst();
226 	else if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID)
227 		ret_val = intel_sst_reset_dsp_medfield();
228 	if (ret_val)
229 		return ret_val;
230 
231 	ret_val = sst_parse_fw_image(fw);
232 	if (ret_val)
233 		return ret_val;
234 	mutex_lock(&sst_drv_ctx->sst_lock);
235 	sst_drv_ctx->sst_state = SST_FW_LOADED;
236 	mutex_unlock(&sst_drv_ctx->sst_lock);
237 	/*  7. ask scu to reset the bypass bits */
238 	/*  8.bring sst out of reset  */
239 	if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
240 		ret_val = sst_start_mrst();
241 	else if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID)
242 		ret_val = sst_start_medfield();
243 	if (ret_val)
244 		return ret_val;
245 
246 	pr_debug("fw loaded successful!!!\n");
247 	return ret_val;
248 }
249 
250 /*This function is called when any codec/post processing library
251  needs to be downloaded*/
sst_download_library(const struct firmware * fw_lib,struct snd_sst_lib_download_info * lib)252 static int sst_download_library(const struct firmware *fw_lib,
253 				struct snd_sst_lib_download_info *lib)
254 {
255 	/* send IPC message and wait */
256 	int i;
257 	u8 pvt_id;
258 	struct ipc_post *msg = NULL;
259 	union config_status_reg csr;
260 	struct snd_sst_str_type str_type = {0};
261 	int retval = 0;
262 
263 	if (sst_create_large_msg(&msg))
264 		return -ENOMEM;
265 
266 	pvt_id = sst_assign_pvt_id(sst_drv_ctx);
267 	i = sst_get_block_stream(sst_drv_ctx);
268 	pr_debug("alloc block allocated = %d, pvt_id %d\n", i, pvt_id);
269 	if (i < 0) {
270 		kfree(msg);
271 		return -ENOMEM;
272 	}
273 	sst_drv_ctx->alloc_block[i].sst_id = pvt_id;
274 	sst_fill_header(&msg->header, IPC_IA_PREP_LIB_DNLD, 1, pvt_id);
275 	msg->header.part.data = sizeof(u32) + sizeof(str_type);
276 	str_type.codec_type = lib->dload_lib.lib_info.lib_type;
277 	/*str_type.pvt_id = pvt_id;*/
278 	memcpy(msg->mailbox_data, &msg->header, sizeof(u32));
279 	memcpy(msg->mailbox_data + sizeof(u32), &str_type, sizeof(str_type));
280 	spin_lock(&sst_drv_ctx->list_spin_lock);
281 	list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
282 	spin_unlock(&sst_drv_ctx->list_spin_lock);
283 	sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
284 	retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[i]);
285 	if (retval) {
286 		/* error */
287 		sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
288 		pr_err("Prep codec downloaded failed %d\n",
289 				retval);
290 		return -EIO;
291 	}
292 	pr_debug("FW responded, ready for download now...\n");
293 	/* downloading on success */
294 	mutex_lock(&sst_drv_ctx->sst_lock);
295 	sst_drv_ctx->sst_state = SST_FW_LOADED;
296 	mutex_unlock(&sst_drv_ctx->sst_lock);
297 	csr.full = readl(sst_drv_ctx->shim + SST_CSR);
298 	csr.part.run_stall = 1;
299 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
300 
301 	csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
302 	csr.part.bypass = 0x7;
303 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
304 
305 	sst_parse_fw_image(fw_lib);
306 
307 	/* set the FW to running again */
308 	csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
309 	csr.part.bypass = 0x0;
310 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
311 
312 	csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
313 	csr.part.run_stall = 0;
314 	sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
315 
316 	/* send download complete and wait */
317 	if (sst_create_large_msg(&msg)) {
318 		sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
319 		return -ENOMEM;
320 	}
321 
322 	sst_fill_header(&msg->header, IPC_IA_LIB_DNLD_CMPLT, 1, pvt_id);
323 	sst_drv_ctx->alloc_block[i].sst_id = pvt_id;
324 	msg->header.part.data = sizeof(u32) + sizeof(*lib);
325 	lib->pvt_id = pvt_id;
326 	memcpy(msg->mailbox_data, &msg->header, sizeof(u32));
327 	memcpy(msg->mailbox_data + sizeof(u32), lib, sizeof(*lib));
328 	spin_lock(&sst_drv_ctx->list_spin_lock);
329 	list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
330 	spin_unlock(&sst_drv_ctx->list_spin_lock);
331 	sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
332 	pr_debug("Waiting for FW response Download complete\n");
333 	sst_drv_ctx->alloc_block[i].ops_block.condition = false;
334 	retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[i]);
335 	if (retval) {
336 		/* error */
337 		mutex_lock(&sst_drv_ctx->sst_lock);
338 		sst_drv_ctx->sst_state = SST_UN_INIT;
339 		mutex_unlock(&sst_drv_ctx->sst_lock);
340 		sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
341 		return -EIO;
342 	}
343 
344 	pr_debug("FW success on Download complete\n");
345 	sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
346 	mutex_lock(&sst_drv_ctx->sst_lock);
347 	sst_drv_ctx->sst_state = SST_FW_RUNNING;
348 	mutex_unlock(&sst_drv_ctx->sst_lock);
349 	return 0;
350 
351 }
352 
353 /* This function is called before downloading the codec/postprocessing
354 library is set for download to SST DSP*/
sst_validate_library(const struct firmware * fw_lib,struct lib_slot_info * slot,u32 * entry_point)355 static int sst_validate_library(const struct firmware *fw_lib,
356 		struct lib_slot_info *slot,
357 		u32 *entry_point)
358 {
359 	struct fw_header *header;
360 	struct fw_module_header *module;
361 	struct dma_block_info *block;
362 	unsigned int n_blk, isize = 0, dsize = 0;
363 	int err = 0;
364 
365 	header = (struct fw_header *)fw_lib->data;
366 	if (header->modules != 1) {
367 		pr_err("Module no mismatch found\n");
368 		err = -EINVAL;
369 		goto exit;
370 	}
371 	module = (void *)fw_lib->data + sizeof(*header);
372 	*entry_point = module->entry_point;
373 	pr_debug("Module entry point 0x%x\n", *entry_point);
374 	pr_debug("Module Sign %s, Size 0x%x, Blocks 0x%x Type 0x%x\n",
375 			module->signature, module->mod_size,
376 			module->blocks, module->type);
377 
378 	block = (void *)module + sizeof(*module);
379 	for (n_blk = 0; n_blk < module->blocks; n_blk++) {
380 		switch (block->type) {
381 		case SST_IRAM:
382 			isize += block->size;
383 			break;
384 		case SST_DRAM:
385 			dsize += block->size;
386 			break;
387 		default:
388 			pr_err("Invalid block type for 0x%x\n", n_blk);
389 			err = -EINVAL;
390 			goto exit;
391 		}
392 		block = (void *)block + sizeof(*block) + block->size;
393 	}
394 	if (isize > slot->iram_size || dsize > slot->dram_size) {
395 		pr_err("library exceeds size allocated\n");
396 		err = -EINVAL;
397 		goto exit;
398 	} else
399 		pr_debug("Library is safe for download...\n");
400 
401 	pr_debug("iram 0x%x, dram 0x%x, iram 0x%x, dram 0x%x\n",
402 			isize, dsize, slot->iram_size, slot->dram_size);
403 exit:
404 	return err;
405 
406 }
407 
408 /* This function is called when FW requests for a particular library download
409 This function prepares the library to download*/
sst_load_library(struct snd_sst_lib_download * lib,u8 ops)410 int sst_load_library(struct snd_sst_lib_download *lib, u8 ops)
411 {
412 	char buf[20];
413 	const char *type, *dir;
414 	int len = 0, error = 0;
415 	u32 entry_point;
416 	const struct firmware *fw_lib;
417 	struct snd_sst_lib_download_info dload_info = {{{0},},};
418 
419 	memset(buf, 0, sizeof(buf));
420 
421 	pr_debug("Lib Type 0x%x, Slot 0x%x, ops 0x%x\n",
422 			lib->lib_info.lib_type, lib->slot_info.slot_num, ops);
423 	pr_debug("Version 0x%x, name %s, caps 0x%x media type 0x%x\n",
424 		lib->lib_info.lib_version, lib->lib_info.lib_name,
425 		lib->lib_info.lib_caps, lib->lib_info.media_type);
426 
427 	pr_debug("IRAM Size 0x%x, offset 0x%x\n",
428 		lib->slot_info.iram_size, lib->slot_info.iram_offset);
429 	pr_debug("DRAM Size 0x%x, offset 0x%x\n",
430 		lib->slot_info.dram_size, lib->slot_info.dram_offset);
431 
432 	switch (lib->lib_info.lib_type) {
433 	case SST_CODEC_TYPE_MP3:
434 		type = "mp3_";
435 		break;
436 	case SST_CODEC_TYPE_AAC:
437 		type = "aac_";
438 		break;
439 	case SST_CODEC_TYPE_AACP:
440 		type = "aac_v1_";
441 		break;
442 	case SST_CODEC_TYPE_eAACP:
443 		type = "aac_v2_";
444 		break;
445 	case SST_CODEC_TYPE_WMA9:
446 		type = "wma9_";
447 		break;
448 	default:
449 		pr_err("Invalid codec type\n");
450 		error = -EINVAL;
451 		goto wake;
452 	}
453 
454 	if (ops == STREAM_OPS_CAPTURE)
455 		dir = "enc_";
456 	else
457 		dir = "dec_";
458 	len = strlen(type) + strlen(dir);
459 	strncpy(buf, type, sizeof(buf)-1);
460 	strncpy(buf + strlen(type), dir, sizeof(buf)-strlen(type)-1);
461 	len += snprintf(buf + len, sizeof(buf) - len, "%d",
462 			lib->slot_info.slot_num);
463 	len += snprintf(buf + len, sizeof(buf) - len, ".bin");
464 
465 	pr_debug("Requesting %s\n", buf);
466 
467 	error = request_firmware(&fw_lib, buf, &sst_drv_ctx->pci->dev);
468 	if (error) {
469 		pr_err("library load failed %d\n", error);
470 		goto wake;
471 	}
472 	error = sst_validate_library(fw_lib, &lib->slot_info, &entry_point);
473 	if (error)
474 		goto wake_free;
475 
476 	lib->mod_entry_pt = entry_point;
477 	memcpy(&dload_info.dload_lib, lib, sizeof(*lib));
478 	error = sst_download_library(fw_lib, &dload_info);
479 	if (error)
480 		goto wake_free;
481 
482 	/* lib is downloaded and init send alloc again */
483 	pr_debug("Library is downloaded now...\n");
484 wake_free:
485 	/* sst_wake_up_alloc_block(sst_drv_ctx, pvt_id, error, NULL); */
486 	release_firmware(fw_lib);
487 wake:
488 	return error;
489 }
490 
491