1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  sst.c - Intel SST Driver for audio engine
4  *
5  *  Copyright (C) 2008-14	Intel Corp
6  *  Authors:	Vinod Koul <vinod.koul@intel.com>
7  *		Harsha Priya <priya.harsha@intel.com>
8  *		Dharageswari R <dharageswari.r@intel.com>
9  *		KP Jeeja <jeeja.kp@intel.com>
10  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13  */
14 #include <linux/module.h>
15 #include <linux/fs.h>
16 #include <linux/interrupt.h>
17 #include <linux/io.h>
18 #include <linux/firmware.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/pm_qos.h>
21 #include <linux/async.h>
22 #include <linux/acpi.h>
23 #include <linux/sysfs.h>
24 #include <sound/core.h>
25 #include <sound/soc.h>
26 #include <asm/platform_sst_audio.h>
27 #include "../sst-mfld-platform.h"
28 #include "sst.h"
29 
30 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
31 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
32 MODULE_DESCRIPTION("Intel (R) SST(R) Audio Engine Driver");
33 MODULE_LICENSE("GPL v2");
34 
sst_is_process_reply(u32 msg_id)35 static inline bool sst_is_process_reply(u32 msg_id)
36 {
37 	return ((msg_id & PROCESS_MSG) ? true : false);
38 }
39 
sst_validate_mailbox_size(unsigned int size)40 static inline bool sst_validate_mailbox_size(unsigned int size)
41 {
42 	return ((size <= SST_MAILBOX_SIZE) ? true : false);
43 }
44 
intel_sst_interrupt_mrfld(int irq,void * context)45 static irqreturn_t intel_sst_interrupt_mrfld(int irq, void *context)
46 {
47 	union interrupt_reg_mrfld isr;
48 	union ipc_header_mrfld header;
49 	union sst_imr_reg_mrfld imr;
50 	struct ipc_post *msg = NULL;
51 	unsigned int size;
52 	struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
53 	irqreturn_t retval = IRQ_HANDLED;
54 
55 	/* Interrupt arrived, check src */
56 	isr.full = sst_shim_read64(drv->shim, SST_ISRX);
57 
58 	if (isr.part.done_interrupt) {
59 		/* Clear done bit */
60 		spin_lock(&drv->ipc_spin_lock);
61 		header.full = sst_shim_read64(drv->shim,
62 					drv->ipc_reg.ipcx);
63 		header.p.header_high.part.done = 0;
64 		sst_shim_write64(drv->shim, drv->ipc_reg.ipcx, header.full);
65 
66 		/* write 1 to clear status register */;
67 		isr.part.done_interrupt = 1;
68 		sst_shim_write64(drv->shim, SST_ISRX, isr.full);
69 		spin_unlock(&drv->ipc_spin_lock);
70 
71 		/* we can send more messages to DSP so trigger work */
72 		queue_work(drv->post_msg_wq, &drv->ipc_post_msg_wq);
73 		retval = IRQ_HANDLED;
74 	}
75 
76 	if (isr.part.busy_interrupt) {
77 		/* message from dsp so copy that */
78 		spin_lock(&drv->ipc_spin_lock);
79 		imr.full = sst_shim_read64(drv->shim, SST_IMRX);
80 		imr.part.busy_interrupt = 1;
81 		sst_shim_write64(drv->shim, SST_IMRX, imr.full);
82 		spin_unlock(&drv->ipc_spin_lock);
83 		header.full =  sst_shim_read64(drv->shim, drv->ipc_reg.ipcd);
84 
85 		if (sst_create_ipc_msg(&msg, header.p.header_high.part.large)) {
86 			drv->ops->clear_interrupt(drv);
87 			return IRQ_HANDLED;
88 		}
89 
90 		if (header.p.header_high.part.large) {
91 			size = header.p.header_low_payload;
92 			if (sst_validate_mailbox_size(size)) {
93 				memcpy_fromio(msg->mailbox_data,
94 					drv->mailbox + drv->mailbox_recv_offset, size);
95 			} else {
96 				dev_err(drv->dev,
97 					"Mailbox not copied, payload size is: %u\n", size);
98 				header.p.header_low_payload = 0;
99 			}
100 		}
101 
102 		msg->mrfld_header = header;
103 		msg->is_process_reply =
104 			sst_is_process_reply(header.p.header_high.part.msg_id);
105 		spin_lock(&drv->rx_msg_lock);
106 		list_add_tail(&msg->node, &drv->rx_list);
107 		spin_unlock(&drv->rx_msg_lock);
108 		drv->ops->clear_interrupt(drv);
109 		retval = IRQ_WAKE_THREAD;
110 	}
111 	return retval;
112 }
113 
intel_sst_irq_thread_mrfld(int irq,void * context)114 static irqreturn_t intel_sst_irq_thread_mrfld(int irq, void *context)
115 {
116 	struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
117 	struct ipc_post *__msg, *msg = NULL;
118 	unsigned long irq_flags;
119 
120 	spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
121 	if (list_empty(&drv->rx_list)) {
122 		spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
123 		return IRQ_HANDLED;
124 	}
125 
126 	list_for_each_entry_safe(msg, __msg, &drv->rx_list, node) {
127 		list_del(&msg->node);
128 		spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
129 		if (msg->is_process_reply)
130 			drv->ops->process_message(msg);
131 		else
132 			drv->ops->process_reply(drv, msg);
133 
134 		if (msg->is_large)
135 			kfree(msg->mailbox_data);
136 		kfree(msg);
137 		spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
138 	}
139 	spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
140 	return IRQ_HANDLED;
141 }
142 
sst_save_dsp_context_v2(struct intel_sst_drv * sst)143 static int sst_save_dsp_context_v2(struct intel_sst_drv *sst)
144 {
145 	int ret = 0;
146 
147 	ret = sst_prepare_and_post_msg(sst, SST_TASK_ID_MEDIA, IPC_CMD,
148 			IPC_PREP_D3, PIPE_RSVD, 0, NULL, NULL,
149 			true, true, false, true);
150 
151 	if (ret < 0) {
152 		dev_err(sst->dev, "not suspending FW!!, Err: %d\n", ret);
153 		return -EIO;
154 	}
155 
156 	return 0;
157 }
158 
159 
160 static struct intel_sst_ops mrfld_ops = {
161 	.interrupt = intel_sst_interrupt_mrfld,
162 	.irq_thread = intel_sst_irq_thread_mrfld,
163 	.clear_interrupt = intel_sst_clear_intr_mrfld,
164 	.start = sst_start_mrfld,
165 	.reset = intel_sst_reset_dsp_mrfld,
166 	.post_message = sst_post_message_mrfld,
167 	.process_reply = sst_process_reply_mrfld,
168 	.save_dsp_context =  sst_save_dsp_context_v2,
169 	.alloc_stream = sst_alloc_stream_mrfld,
170 	.post_download = sst_post_download_mrfld,
171 };
172 
sst_driver_ops(struct intel_sst_drv * sst)173 int sst_driver_ops(struct intel_sst_drv *sst)
174 {
175 
176 	switch (sst->dev_id) {
177 	case SST_MRFLD_PCI_ID:
178 	case SST_BYT_ACPI_ID:
179 	case SST_CHV_ACPI_ID:
180 		sst->tstamp = SST_TIME_STAMP_MRFLD;
181 		sst->ops = &mrfld_ops;
182 		return 0;
183 
184 	default:
185 		dev_err(sst->dev,
186 			"SST Driver capabilities missing for dev_id: %x",
187 			sst->dev_id);
188 		return -EINVAL;
189 	}
190 }
191 
sst_process_pending_msg(struct work_struct * work)192 void sst_process_pending_msg(struct work_struct *work)
193 {
194 	struct intel_sst_drv *ctx = container_of(work,
195 			struct intel_sst_drv, ipc_post_msg_wq);
196 
197 	ctx->ops->post_message(ctx, NULL, false);
198 }
199 
sst_workqueue_init(struct intel_sst_drv * ctx)200 static int sst_workqueue_init(struct intel_sst_drv *ctx)
201 {
202 	INIT_LIST_HEAD(&ctx->memcpy_list);
203 	INIT_LIST_HEAD(&ctx->rx_list);
204 	INIT_LIST_HEAD(&ctx->ipc_dispatch_list);
205 	INIT_LIST_HEAD(&ctx->block_list);
206 	INIT_WORK(&ctx->ipc_post_msg_wq, sst_process_pending_msg);
207 	init_waitqueue_head(&ctx->wait_queue);
208 
209 	ctx->post_msg_wq =
210 		create_singlethread_workqueue("sst_post_msg_wq");
211 	if (!ctx->post_msg_wq)
212 		return -EBUSY;
213 	return 0;
214 }
215 
sst_init_locks(struct intel_sst_drv * ctx)216 static void sst_init_locks(struct intel_sst_drv *ctx)
217 {
218 	mutex_init(&ctx->sst_lock);
219 	spin_lock_init(&ctx->rx_msg_lock);
220 	spin_lock_init(&ctx->ipc_spin_lock);
221 	spin_lock_init(&ctx->block_lock);
222 }
223 
sst_alloc_drv_context(struct intel_sst_drv ** ctx,struct device * dev,unsigned int dev_id)224 int sst_alloc_drv_context(struct intel_sst_drv **ctx,
225 		struct device *dev, unsigned int dev_id)
226 {
227 	*ctx = devm_kzalloc(dev, sizeof(struct intel_sst_drv), GFP_KERNEL);
228 	if (!(*ctx))
229 		return -ENOMEM;
230 
231 	(*ctx)->dev = dev;
232 	(*ctx)->dev_id = dev_id;
233 
234 	return 0;
235 }
236 EXPORT_SYMBOL_GPL(sst_alloc_drv_context);
237 
firmware_version_show(struct device * dev,struct device_attribute * attr,char * buf)238 static ssize_t firmware_version_show(struct device *dev,
239 			    struct device_attribute *attr, char *buf)
240 {
241 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
242 
243 	if (ctx->fw_version.type == 0 && ctx->fw_version.major == 0 &&
244 	    ctx->fw_version.minor == 0 && ctx->fw_version.build == 0)
245 		return sprintf(buf, "FW not yet loaded\n");
246 	else
247 		return sprintf(buf, "v%02x.%02x.%02x.%02x\n",
248 			       ctx->fw_version.type, ctx->fw_version.major,
249 			       ctx->fw_version.minor, ctx->fw_version.build);
250 
251 }
252 
253 static DEVICE_ATTR_RO(firmware_version);
254 
255 static const struct attribute *sst_fw_version_attrs[] = {
256 	&dev_attr_firmware_version.attr,
257 	NULL,
258 };
259 
260 static const struct attribute_group sst_fw_version_attr_group = {
261 	.attrs = (struct attribute **)sst_fw_version_attrs,
262 };
263 
sst_context_init(struct intel_sst_drv * ctx)264 int sst_context_init(struct intel_sst_drv *ctx)
265 {
266 	int ret = 0, i;
267 
268 	if (!ctx->pdata)
269 		return -EINVAL;
270 
271 	if (!ctx->pdata->probe_data)
272 		return -EINVAL;
273 
274 	memcpy(&ctx->info, ctx->pdata->probe_data, sizeof(ctx->info));
275 
276 	ret = sst_driver_ops(ctx);
277 	if (ret != 0)
278 		return -EINVAL;
279 
280 	sst_init_locks(ctx);
281 	sst_set_fw_state_locked(ctx, SST_RESET);
282 
283 	/* pvt_id 0 reserved for async messages */
284 	ctx->pvt_id = 1;
285 	ctx->stream_cnt = 0;
286 	ctx->fw_in_mem = NULL;
287 	/* we use memcpy, so set to 0 */
288 	ctx->use_dma = 0;
289 	ctx->use_lli = 0;
290 
291 	if (sst_workqueue_init(ctx))
292 		return -EINVAL;
293 
294 	ctx->mailbox_recv_offset = ctx->pdata->ipc_info->mbox_recv_off;
295 	ctx->ipc_reg.ipcx = SST_IPCX + ctx->pdata->ipc_info->ipc_offset;
296 	ctx->ipc_reg.ipcd = SST_IPCD + ctx->pdata->ipc_info->ipc_offset;
297 
298 	dev_info(ctx->dev, "Got drv data max stream %d\n",
299 				ctx->info.max_streams);
300 
301 	for (i = 1; i <= ctx->info.max_streams; i++) {
302 		struct stream_info *stream = &ctx->streams[i];
303 
304 		memset(stream, 0, sizeof(*stream));
305 		stream->pipe_id = PIPE_RSVD;
306 		mutex_init(&stream->lock);
307 	}
308 
309 	/* Register the ISR */
310 	ret = devm_request_threaded_irq(ctx->dev, ctx->irq_num, ctx->ops->interrupt,
311 					ctx->ops->irq_thread, 0, SST_DRV_NAME,
312 					ctx);
313 	if (ret)
314 		goto do_free_mem;
315 
316 	dev_dbg(ctx->dev, "Registered IRQ %#x\n", ctx->irq_num);
317 
318 	/* default intr are unmasked so set this as masked */
319 	sst_shim_write64(ctx->shim, SST_IMRX, 0xFFFF0038);
320 
321 	ctx->qos = devm_kzalloc(ctx->dev,
322 		sizeof(struct pm_qos_request), GFP_KERNEL);
323 	if (!ctx->qos) {
324 		ret = -ENOMEM;
325 		goto do_free_mem;
326 	}
327 	cpu_latency_qos_add_request(ctx->qos, PM_QOS_DEFAULT_VALUE);
328 
329 	dev_dbg(ctx->dev, "Requesting FW %s now...\n", ctx->firmware_name);
330 	ret = request_firmware_nowait(THIS_MODULE, true, ctx->firmware_name,
331 				      ctx->dev, GFP_KERNEL, ctx, sst_firmware_load_cb);
332 	if (ret) {
333 		dev_err(ctx->dev, "Firmware download failed:%d\n", ret);
334 		goto do_free_mem;
335 	}
336 
337 	ret = sysfs_create_group(&ctx->dev->kobj,
338 				 &sst_fw_version_attr_group);
339 	if (ret) {
340 		dev_err(ctx->dev,
341 			"Unable to create sysfs\n");
342 		goto err_sysfs;
343 	}
344 
345 	sst_register(ctx->dev);
346 	return 0;
347 err_sysfs:
348 	sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
349 
350 do_free_mem:
351 	destroy_workqueue(ctx->post_msg_wq);
352 	return ret;
353 }
354 EXPORT_SYMBOL_GPL(sst_context_init);
355 
sst_context_cleanup(struct intel_sst_drv * ctx)356 void sst_context_cleanup(struct intel_sst_drv *ctx)
357 {
358 	pm_runtime_get_noresume(ctx->dev);
359 	pm_runtime_disable(ctx->dev);
360 	sst_unregister(ctx->dev);
361 	sst_set_fw_state_locked(ctx, SST_SHUTDOWN);
362 	sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
363 	flush_scheduled_work();
364 	destroy_workqueue(ctx->post_msg_wq);
365 	cpu_latency_qos_remove_request(ctx->qos);
366 	kfree(ctx->fw_sg_list.src);
367 	kfree(ctx->fw_sg_list.dst);
368 	ctx->fw_sg_list.list_len = 0;
369 	kfree(ctx->fw_in_mem);
370 	ctx->fw_in_mem = NULL;
371 	sst_memcpy_free_resources(ctx);
372 }
373 EXPORT_SYMBOL_GPL(sst_context_cleanup);
374 
sst_configure_runtime_pm(struct intel_sst_drv * ctx)375 void sst_configure_runtime_pm(struct intel_sst_drv *ctx)
376 {
377 	pm_runtime_set_autosuspend_delay(ctx->dev, SST_SUSPEND_DELAY);
378 	pm_runtime_use_autosuspend(ctx->dev);
379 	/*
380 	 * For acpi devices, the actual physical device state is
381 	 * initially active. So change the state to active before
382 	 * enabling the pm
383 	 */
384 
385 	if (!acpi_disabled)
386 		pm_runtime_set_active(ctx->dev);
387 
388 	pm_runtime_enable(ctx->dev);
389 
390 	if (acpi_disabled)
391 		pm_runtime_set_active(ctx->dev);
392 	else
393 		pm_runtime_put_noidle(ctx->dev);
394 }
395 EXPORT_SYMBOL_GPL(sst_configure_runtime_pm);
396 
intel_sst_runtime_suspend(struct device * dev)397 static int intel_sst_runtime_suspend(struct device *dev)
398 {
399 	int ret = 0;
400 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
401 
402 	if (ctx->sst_state == SST_RESET) {
403 		dev_dbg(dev, "LPE is already in RESET state, No action\n");
404 		return 0;
405 	}
406 	/* save fw context */
407 	if (ctx->ops->save_dsp_context(ctx))
408 		return -EBUSY;
409 
410 	/* Move the SST state to Reset */
411 	sst_set_fw_state_locked(ctx, SST_RESET);
412 
413 	synchronize_irq(ctx->irq_num);
414 	flush_workqueue(ctx->post_msg_wq);
415 
416 	ctx->ops->reset(ctx);
417 
418 	return ret;
419 }
420 
intel_sst_suspend(struct device * dev)421 static int intel_sst_suspend(struct device *dev)
422 {
423 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
424 	struct sst_fw_save *fw_save;
425 	int i, ret;
426 
427 	/* check first if we are already in SW reset */
428 	if (ctx->sst_state == SST_RESET)
429 		return 0;
430 
431 	/*
432 	 * check if any stream is active and running
433 	 * they should already by suspend by soc_suspend
434 	 */
435 	for (i = 1; i <= ctx->info.max_streams; i++) {
436 		struct stream_info *stream = &ctx->streams[i];
437 
438 		if (stream->status == STREAM_RUNNING) {
439 			dev_err(dev, "stream %d is running, can't suspend, abort\n", i);
440 			return -EBUSY;
441 		}
442 
443 		if (ctx->pdata->streams_lost_on_suspend) {
444 			stream->resume_status = stream->status;
445 			stream->resume_prev = stream->prev;
446 			if (stream->status != STREAM_UN_INIT)
447 				sst_free_stream(ctx, i);
448 		}
449 	}
450 	synchronize_irq(ctx->irq_num);
451 	flush_workqueue(ctx->post_msg_wq);
452 
453 	/* Move the SST state to Reset */
454 	sst_set_fw_state_locked(ctx, SST_RESET);
455 
456 	/* tell DSP we are suspending */
457 	if (ctx->ops->save_dsp_context(ctx))
458 		return -EBUSY;
459 
460 	/* save the memories */
461 	fw_save = kzalloc(sizeof(*fw_save), GFP_KERNEL);
462 	if (!fw_save)
463 		return -ENOMEM;
464 	fw_save->iram = kvzalloc(ctx->iram_end - ctx->iram_base, GFP_KERNEL);
465 	if (!fw_save->iram) {
466 		ret = -ENOMEM;
467 		goto iram;
468 	}
469 	fw_save->dram = kvzalloc(ctx->dram_end - ctx->dram_base, GFP_KERNEL);
470 	if (!fw_save->dram) {
471 		ret = -ENOMEM;
472 		goto dram;
473 	}
474 	fw_save->sram = kvzalloc(SST_MAILBOX_SIZE, GFP_KERNEL);
475 	if (!fw_save->sram) {
476 		ret = -ENOMEM;
477 		goto sram;
478 	}
479 
480 	fw_save->ddr = kvzalloc(ctx->ddr_end - ctx->ddr_base, GFP_KERNEL);
481 	if (!fw_save->ddr) {
482 		ret = -ENOMEM;
483 		goto ddr;
484 	}
485 
486 	memcpy32_fromio(fw_save->iram, ctx->iram, ctx->iram_end - ctx->iram_base);
487 	memcpy32_fromio(fw_save->dram, ctx->dram, ctx->dram_end - ctx->dram_base);
488 	memcpy32_fromio(fw_save->sram, ctx->mailbox, SST_MAILBOX_SIZE);
489 	memcpy32_fromio(fw_save->ddr, ctx->ddr, ctx->ddr_end - ctx->ddr_base);
490 
491 	ctx->fw_save = fw_save;
492 	ctx->ops->reset(ctx);
493 	return 0;
494 ddr:
495 	kvfree(fw_save->sram);
496 sram:
497 	kvfree(fw_save->dram);
498 dram:
499 	kvfree(fw_save->iram);
500 iram:
501 	kfree(fw_save);
502 	return ret;
503 }
504 
intel_sst_resume(struct device * dev)505 static int intel_sst_resume(struct device *dev)
506 {
507 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
508 	struct sst_fw_save *fw_save = ctx->fw_save;
509 	struct sst_block *block;
510 	int i, ret = 0;
511 
512 	if (!fw_save)
513 		return 0;
514 
515 	sst_set_fw_state_locked(ctx, SST_FW_LOADING);
516 
517 	/* we have to restore the memory saved */
518 	ctx->ops->reset(ctx);
519 
520 	ctx->fw_save = NULL;
521 
522 	memcpy32_toio(ctx->iram, fw_save->iram, ctx->iram_end - ctx->iram_base);
523 	memcpy32_toio(ctx->dram, fw_save->dram, ctx->dram_end - ctx->dram_base);
524 	memcpy32_toio(ctx->mailbox, fw_save->sram, SST_MAILBOX_SIZE);
525 	memcpy32_toio(ctx->ddr, fw_save->ddr, ctx->ddr_end - ctx->ddr_base);
526 
527 	kvfree(fw_save->sram);
528 	kvfree(fw_save->dram);
529 	kvfree(fw_save->iram);
530 	kvfree(fw_save->ddr);
531 	kfree(fw_save);
532 
533 	block = sst_create_block(ctx, 0, FW_DWNL_ID);
534 	if (block == NULL)
535 		return -ENOMEM;
536 
537 
538 	/* start and wait for ack */
539 	ctx->ops->start(ctx);
540 	ret = sst_wait_timeout(ctx, block);
541 	if (ret) {
542 		dev_err(ctx->dev, "fw download failed %d\n", ret);
543 		/* FW download failed due to timeout */
544 		ret = -EBUSY;
545 
546 	} else {
547 		sst_set_fw_state_locked(ctx, SST_FW_RUNNING);
548 	}
549 
550 	if (ctx->pdata->streams_lost_on_suspend) {
551 		for (i = 1; i <= ctx->info.max_streams; i++) {
552 			struct stream_info *stream = &ctx->streams[i];
553 
554 			if (stream->resume_status != STREAM_UN_INIT) {
555 				dev_dbg(ctx->dev, "Re-allocing stream %d status %d prev %d\n",
556 					i, stream->resume_status,
557 					stream->resume_prev);
558 				sst_realloc_stream(ctx, i);
559 				stream->status = stream->resume_status;
560 				stream->prev = stream->resume_prev;
561 			}
562 		}
563 	}
564 
565 	sst_free_block(ctx, block);
566 	return ret;
567 }
568 
569 const struct dev_pm_ops intel_sst_pm = {
570 	.suspend = intel_sst_suspend,
571 	.resume = intel_sst_resume,
572 	.runtime_suspend = intel_sst_runtime_suspend,
573 };
574 EXPORT_SYMBOL_GPL(intel_sst_pm);
575