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