xref: /openbmc/linux/sound/soc/intel/atom/sst/sst.c (revision 7ae5c03a)
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 
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;
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;
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 	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 
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 	destroy_workqueue(ctx->post_msg_wq);
364 	cpu_latency_qos_remove_request(ctx->qos);
365 	kfree(ctx->fw_sg_list.src);
366 	kfree(ctx->fw_sg_list.dst);
367 	ctx->fw_sg_list.list_len = 0;
368 	kfree(ctx->fw_in_mem);
369 	ctx->fw_in_mem = NULL;
370 	sst_memcpy_free_resources(ctx);
371 }
372 EXPORT_SYMBOL_GPL(sst_context_cleanup);
373 
374 void sst_configure_runtime_pm(struct intel_sst_drv *ctx)
375 {
376 	pm_runtime_set_autosuspend_delay(ctx->dev, SST_SUSPEND_DELAY);
377 	pm_runtime_use_autosuspend(ctx->dev);
378 	/*
379 	 * For acpi devices, the actual physical device state is
380 	 * initially active. So change the state to active before
381 	 * enabling the pm
382 	 */
383 
384 	if (!acpi_disabled)
385 		pm_runtime_set_active(ctx->dev);
386 
387 	pm_runtime_enable(ctx->dev);
388 
389 	if (acpi_disabled)
390 		pm_runtime_set_active(ctx->dev);
391 	else
392 		pm_runtime_put_noidle(ctx->dev);
393 }
394 EXPORT_SYMBOL_GPL(sst_configure_runtime_pm);
395 
396 static int intel_sst_runtime_suspend(struct device *dev)
397 {
398 	int ret = 0;
399 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
400 
401 	if (ctx->sst_state == SST_RESET) {
402 		dev_dbg(dev, "LPE is already in RESET state, No action\n");
403 		return 0;
404 	}
405 	/* save fw context */
406 	if (ctx->ops->save_dsp_context(ctx))
407 		return -EBUSY;
408 
409 	/* Move the SST state to Reset */
410 	sst_set_fw_state_locked(ctx, SST_RESET);
411 
412 	synchronize_irq(ctx->irq_num);
413 	flush_workqueue(ctx->post_msg_wq);
414 
415 	ctx->ops->reset(ctx);
416 
417 	return ret;
418 }
419 
420 static int intel_sst_suspend(struct device *dev)
421 {
422 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
423 	struct sst_fw_save *fw_save;
424 	int i, ret;
425 
426 	/* check first if we are already in SW reset */
427 	if (ctx->sst_state == SST_RESET)
428 		return 0;
429 
430 	/*
431 	 * check if any stream is active and running
432 	 * they should already by suspend by soc_suspend
433 	 */
434 	for (i = 1; i <= ctx->info.max_streams; i++) {
435 		struct stream_info *stream = &ctx->streams[i];
436 
437 		if (stream->status == STREAM_RUNNING) {
438 			dev_err(dev, "stream %d is running, can't suspend, abort\n", i);
439 			return -EBUSY;
440 		}
441 
442 		if (ctx->pdata->streams_lost_on_suspend) {
443 			stream->resume_status = stream->status;
444 			stream->resume_prev = stream->prev;
445 			if (stream->status != STREAM_UN_INIT)
446 				sst_free_stream(ctx, i);
447 		}
448 	}
449 	synchronize_irq(ctx->irq_num);
450 	flush_workqueue(ctx->post_msg_wq);
451 
452 	/* Move the SST state to Reset */
453 	sst_set_fw_state_locked(ctx, SST_RESET);
454 
455 	/* tell DSP we are suspending */
456 	if (ctx->ops->save_dsp_context(ctx))
457 		return -EBUSY;
458 
459 	/* save the memories */
460 	fw_save = kzalloc(sizeof(*fw_save), GFP_KERNEL);
461 	if (!fw_save)
462 		return -ENOMEM;
463 	fw_save->iram = kvzalloc(ctx->iram_end - ctx->iram_base, GFP_KERNEL);
464 	if (!fw_save->iram) {
465 		ret = -ENOMEM;
466 		goto iram;
467 	}
468 	fw_save->dram = kvzalloc(ctx->dram_end - ctx->dram_base, GFP_KERNEL);
469 	if (!fw_save->dram) {
470 		ret = -ENOMEM;
471 		goto dram;
472 	}
473 	fw_save->sram = kvzalloc(SST_MAILBOX_SIZE, GFP_KERNEL);
474 	if (!fw_save->sram) {
475 		ret = -ENOMEM;
476 		goto sram;
477 	}
478 
479 	fw_save->ddr = kvzalloc(ctx->ddr_end - ctx->ddr_base, GFP_KERNEL);
480 	if (!fw_save->ddr) {
481 		ret = -ENOMEM;
482 		goto ddr;
483 	}
484 
485 	memcpy32_fromio(fw_save->iram, ctx->iram, ctx->iram_end - ctx->iram_base);
486 	memcpy32_fromio(fw_save->dram, ctx->dram, ctx->dram_end - ctx->dram_base);
487 	memcpy32_fromio(fw_save->sram, ctx->mailbox, SST_MAILBOX_SIZE);
488 	memcpy32_fromio(fw_save->ddr, ctx->ddr, ctx->ddr_end - ctx->ddr_base);
489 
490 	ctx->fw_save = fw_save;
491 	ctx->ops->reset(ctx);
492 	return 0;
493 ddr:
494 	kvfree(fw_save->sram);
495 sram:
496 	kvfree(fw_save->dram);
497 dram:
498 	kvfree(fw_save->iram);
499 iram:
500 	kfree(fw_save);
501 	return ret;
502 }
503 
504 static int intel_sst_resume(struct device *dev)
505 {
506 	struct intel_sst_drv *ctx = dev_get_drvdata(dev);
507 	struct sst_fw_save *fw_save = ctx->fw_save;
508 	struct sst_block *block;
509 	int i, ret = 0;
510 
511 	if (!fw_save)
512 		return 0;
513 
514 	sst_set_fw_state_locked(ctx, SST_FW_LOADING);
515 
516 	/* we have to restore the memory saved */
517 	ctx->ops->reset(ctx);
518 
519 	ctx->fw_save = NULL;
520 
521 	memcpy32_toio(ctx->iram, fw_save->iram, ctx->iram_end - ctx->iram_base);
522 	memcpy32_toio(ctx->dram, fw_save->dram, ctx->dram_end - ctx->dram_base);
523 	memcpy32_toio(ctx->mailbox, fw_save->sram, SST_MAILBOX_SIZE);
524 	memcpy32_toio(ctx->ddr, fw_save->ddr, ctx->ddr_end - ctx->ddr_base);
525 
526 	kvfree(fw_save->sram);
527 	kvfree(fw_save->dram);
528 	kvfree(fw_save->iram);
529 	kvfree(fw_save->ddr);
530 	kfree(fw_save);
531 
532 	block = sst_create_block(ctx, 0, FW_DWNL_ID);
533 	if (block == NULL)
534 		return -ENOMEM;
535 
536 
537 	/* start and wait for ack */
538 	ctx->ops->start(ctx);
539 	ret = sst_wait_timeout(ctx, block);
540 	if (ret) {
541 		dev_err(ctx->dev, "fw download failed %d\n", ret);
542 		/* FW download failed due to timeout */
543 		ret = -EBUSY;
544 
545 	} else {
546 		sst_set_fw_state_locked(ctx, SST_FW_RUNNING);
547 	}
548 
549 	if (ctx->pdata->streams_lost_on_suspend) {
550 		for (i = 1; i <= ctx->info.max_streams; i++) {
551 			struct stream_info *stream = &ctx->streams[i];
552 
553 			if (stream->resume_status != STREAM_UN_INIT) {
554 				dev_dbg(ctx->dev, "Re-allocing stream %d status %d prev %d\n",
555 					i, stream->resume_status,
556 					stream->resume_prev);
557 				sst_realloc_stream(ctx, i);
558 				stream->status = stream->resume_status;
559 				stream->prev = stream->resume_prev;
560 			}
561 		}
562 	}
563 
564 	sst_free_block(ctx, block);
565 	return ret;
566 }
567 
568 const struct dev_pm_ops intel_sst_pm = {
569 	.suspend = intel_sst_suspend,
570 	.resume = intel_sst_resume,
571 	.runtime_suspend = intel_sst_runtime_suspend,
572 };
573 EXPORT_SYMBOL_GPL(intel_sst_pm);
574