xref: /openbmc/linux/sound/soc/intel/atom/sst/sst.c (revision 7f6964c5)
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/firmware.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/pm_qos.h>
20 #include <linux/async.h>
21 #include <linux/acpi.h>
22 #include <linux/sysfs.h>
23 #include <sound/core.h>
24 #include <sound/soc.h>
25 #include <asm/platform_sst_audio.h>
26 #include "../sst-mfld-platform.h"
27 #include "sst.h"
28 #include "../../common/sst-dsp.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 
35 static inline bool sst_is_process_reply(u32 msg_id)
36 {
37 	return ((msg_id & PROCESS_MSG) ? true : false);
38 }
39 
40 static inline bool sst_validate_mailbox_size(unsigned int size)
41 {
42 	return ((size <= SST_MAILBOX_SIZE) ? true : false);
43 }
44 
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 = 0;
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 	pm_qos_add_request(ctx->qos, PM_QOS_CPU_DMA_LATENCY,
328 				PM_QOS_DEFAULT_VALUE);
329 
330 	dev_dbg(ctx->dev, "Requesting FW %s now...\n", ctx->firmware_name);
331 	ret = request_firmware_nowait(THIS_MODULE, true, ctx->firmware_name,
332 				      ctx->dev, GFP_KERNEL, ctx, sst_firmware_load_cb);
333 	if (ret) {
334 		dev_err(ctx->dev, "Firmware download failed:%d\n", ret);
335 		goto do_free_mem;
336 	}
337 
338 	ret = sysfs_create_group(&ctx->dev->kobj,
339 				 &sst_fw_version_attr_group);
340 	if (ret) {
341 		dev_err(ctx->dev,
342 			"Unable to create sysfs\n");
343 		goto err_sysfs;
344 	}
345 
346 	sst_register(ctx->dev);
347 	return 0;
348 err_sysfs:
349 	sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
350 
351 do_free_mem:
352 	destroy_workqueue(ctx->post_msg_wq);
353 	return ret;
354 }
355 EXPORT_SYMBOL_GPL(sst_context_init);
356 
357 void sst_context_cleanup(struct intel_sst_drv *ctx)
358 {
359 	pm_runtime_get_noresume(ctx->dev);
360 	pm_runtime_disable(ctx->dev);
361 	sst_unregister(ctx->dev);
362 	sst_set_fw_state_locked(ctx, SST_SHUTDOWN);
363 	sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
364 	flush_scheduled_work();
365 	destroy_workqueue(ctx->post_msg_wq);
366 	pm_qos_remove_request(ctx->qos);
367 	kfree(ctx->fw_sg_list.src);
368 	kfree(ctx->fw_sg_list.dst);
369 	ctx->fw_sg_list.list_len = 0;
370 	kfree(ctx->fw_in_mem);
371 	ctx->fw_in_mem = NULL;
372 	sst_memcpy_free_resources(ctx);
373 	ctx = NULL;
374 }
375 EXPORT_SYMBOL_GPL(sst_context_cleanup);
376 
377 void sst_configure_runtime_pm(struct intel_sst_drv *ctx)
378 {
379 	pm_runtime_set_autosuspend_delay(ctx->dev, SST_SUSPEND_DELAY);
380 	pm_runtime_use_autosuspend(ctx->dev);
381 	/*
382 	 * For acpi devices, the actual physical device state is
383 	 * initially active. So change the state to active before
384 	 * enabling the pm
385 	 */
386 
387 	if (!acpi_disabled)
388 		pm_runtime_set_active(ctx->dev);
389 
390 	pm_runtime_enable(ctx->dev);
391 
392 	if (acpi_disabled)
393 		pm_runtime_set_active(ctx->dev);
394 	else
395 		pm_runtime_put_noidle(ctx->dev);
396 }
397 EXPORT_SYMBOL_GPL(sst_configure_runtime_pm);
398 
399 static int intel_sst_runtime_suspend(struct device *dev)
400 {
401 	int ret = 0;
402 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
403 
404 	if (ctx->sst_state == SST_RESET) {
405 		dev_dbg(dev, "LPE is already in RESET state, No action\n");
406 		return 0;
407 	}
408 	/* save fw context */
409 	if (ctx->ops->save_dsp_context(ctx))
410 		return -EBUSY;
411 
412 	/* Move the SST state to Reset */
413 	sst_set_fw_state_locked(ctx, SST_RESET);
414 
415 	synchronize_irq(ctx->irq_num);
416 	flush_workqueue(ctx->post_msg_wq);
417 
418 	ctx->ops->reset(ctx);
419 
420 	return ret;
421 }
422 
423 static int intel_sst_suspend(struct device *dev)
424 {
425 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
426 	struct sst_fw_save *fw_save;
427 	int i, ret = 0;
428 
429 	/* check first if we are already in SW reset */
430 	if (ctx->sst_state == SST_RESET)
431 		return 0;
432 
433 	/*
434 	 * check if any stream is active and running
435 	 * they should already by suspend by soc_suspend
436 	 */
437 	for (i = 1; i <= ctx->info.max_streams; i++) {
438 		struct stream_info *stream = &ctx->streams[i];
439 
440 		if (stream->status == STREAM_RUNNING) {
441 			dev_err(dev, "stream %d is running, can't suspend, abort\n", i);
442 			return -EBUSY;
443 		}
444 
445 		if (ctx->pdata->streams_lost_on_suspend) {
446 			stream->resume_status = stream->status;
447 			stream->resume_prev = stream->prev;
448 			if (stream->status != STREAM_UN_INIT)
449 				sst_free_stream(ctx, i);
450 		}
451 	}
452 	synchronize_irq(ctx->irq_num);
453 	flush_workqueue(ctx->post_msg_wq);
454 
455 	/* Move the SST state to Reset */
456 	sst_set_fw_state_locked(ctx, SST_RESET);
457 
458 	/* tell DSP we are suspending */
459 	if (ctx->ops->save_dsp_context(ctx))
460 		return -EBUSY;
461 
462 	/* save the memories */
463 	fw_save = kzalloc(sizeof(*fw_save), GFP_KERNEL);
464 	if (!fw_save)
465 		return -ENOMEM;
466 	fw_save->iram = kvzalloc(ctx->iram_end - ctx->iram_base, GFP_KERNEL);
467 	if (!fw_save->iram) {
468 		ret = -ENOMEM;
469 		goto iram;
470 	}
471 	fw_save->dram = kvzalloc(ctx->dram_end - ctx->dram_base, GFP_KERNEL);
472 	if (!fw_save->dram) {
473 		ret = -ENOMEM;
474 		goto dram;
475 	}
476 	fw_save->sram = kvzalloc(SST_MAILBOX_SIZE, GFP_KERNEL);
477 	if (!fw_save->sram) {
478 		ret = -ENOMEM;
479 		goto sram;
480 	}
481 
482 	fw_save->ddr = kvzalloc(ctx->ddr_end - ctx->ddr_base, GFP_KERNEL);
483 	if (!fw_save->ddr) {
484 		ret = -ENOMEM;
485 		goto ddr;
486 	}
487 
488 	memcpy32_fromio(fw_save->iram, ctx->iram, ctx->iram_end - ctx->iram_base);
489 	memcpy32_fromio(fw_save->dram, ctx->dram, ctx->dram_end - ctx->dram_base);
490 	memcpy32_fromio(fw_save->sram, ctx->mailbox, SST_MAILBOX_SIZE);
491 	memcpy32_fromio(fw_save->ddr, ctx->ddr, ctx->ddr_end - ctx->ddr_base);
492 
493 	ctx->fw_save = fw_save;
494 	ctx->ops->reset(ctx);
495 	return 0;
496 ddr:
497 	kvfree(fw_save->sram);
498 sram:
499 	kvfree(fw_save->dram);
500 dram:
501 	kvfree(fw_save->iram);
502 iram:
503 	kfree(fw_save);
504 	return ret;
505 }
506 
507 static int intel_sst_resume(struct device *dev)
508 {
509 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
510 	struct sst_fw_save *fw_save = ctx->fw_save;
511 	struct sst_block *block;
512 	int i, ret = 0;
513 
514 	if (!fw_save)
515 		return 0;
516 
517 	sst_set_fw_state_locked(ctx, SST_FW_LOADING);
518 
519 	/* we have to restore the memory saved */
520 	ctx->ops->reset(ctx);
521 
522 	ctx->fw_save = NULL;
523 
524 	memcpy32_toio(ctx->iram, fw_save->iram, ctx->iram_end - ctx->iram_base);
525 	memcpy32_toio(ctx->dram, fw_save->dram, ctx->dram_end - ctx->dram_base);
526 	memcpy32_toio(ctx->mailbox, fw_save->sram, SST_MAILBOX_SIZE);
527 	memcpy32_toio(ctx->ddr, fw_save->ddr, ctx->ddr_end - ctx->ddr_base);
528 
529 	kvfree(fw_save->sram);
530 	kvfree(fw_save->dram);
531 	kvfree(fw_save->iram);
532 	kvfree(fw_save->ddr);
533 	kfree(fw_save);
534 
535 	block = sst_create_block(ctx, 0, FW_DWNL_ID);
536 	if (block == NULL)
537 		return -ENOMEM;
538 
539 
540 	/* start and wait for ack */
541 	ctx->ops->start(ctx);
542 	ret = sst_wait_timeout(ctx, block);
543 	if (ret) {
544 		dev_err(ctx->dev, "fw download failed %d\n", ret);
545 		/* FW download failed due to timeout */
546 		ret = -EBUSY;
547 
548 	} else {
549 		sst_set_fw_state_locked(ctx, SST_FW_RUNNING);
550 	}
551 
552 	if (ctx->pdata->streams_lost_on_suspend) {
553 		for (i = 1; i <= ctx->info.max_streams; i++) {
554 			struct stream_info *stream = &ctx->streams[i];
555 
556 			if (stream->resume_status != STREAM_UN_INIT) {
557 				dev_dbg(ctx->dev, "Re-allocing stream %d status %d prev %d\n",
558 					i, stream->resume_status,
559 					stream->resume_prev);
560 				sst_realloc_stream(ctx, i);
561 				stream->status = stream->resume_status;
562 				stream->prev = stream->resume_prev;
563 			}
564 		}
565 	}
566 
567 	sst_free_block(ctx, block);
568 	return ret;
569 }
570 
571 const struct dev_pm_ops intel_sst_pm = {
572 	.suspend = intel_sst_suspend,
573 	.resume = intel_sst_resume,
574 	.runtime_suspend = intel_sst_runtime_suspend,
575 };
576 EXPORT_SYMBOL_GPL(intel_sst_pm);
577