brintos

brintos / linux-shallow public Read only

0
0
Text · 14.9 KiB · e0357d2 Raw
580 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 *  sst.c - Intel SST Driver for audio engine4 *5 *  Copyright (C) 2008-14	Intel Corp6 *  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/pci.h>20#include <linux/pm_runtime.h>21#include <linux/pm_qos.h>22#include <linux/async.h>23#include <linux/acpi.h>24#include <linux/sysfs.h>25#include <sound/core.h>26#include <sound/soc.h>27#include <asm/platform_sst_audio.h>28#include "../sst-mfld-platform.h"29#include "sst.h"30 31MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");32MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");33MODULE_DESCRIPTION("Intel (R) SST(R) Audio Engine Driver");34MODULE_LICENSE("GPL v2");35 36static inline bool sst_is_process_reply(u32 msg_id)37{38	return ((msg_id & PROCESS_MSG) ? true : false);39}40 41static inline bool sst_validate_mailbox_size(unsigned int size)42{43	return ((size <= SST_MAILBOX_SIZE) ? true : false);44}45 46static 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;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 115static 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;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		else133			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 144static 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 161static 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 174int sst_driver_ops(struct intel_sst_drv *sst)175{176 177	switch (sst->dev_id) {178	case PCI_DEVICE_ID_INTEL_SST_TNG:179	case PCI_DEVICE_ID_INTEL_SST_BYT:180	case PCI_DEVICE_ID_INTEL_SST_BSW: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 193void 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 201static 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 217static 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/*226 * Driver handles PCI IDs in ACPI - sst_acpi_probe() - and we are using only227 * device ID part. If real ACPI ID appears, the kstrtouint() returns error, so228 * we are fine with using unsigned short as dev_id type.229 */230int sst_alloc_drv_context(struct intel_sst_drv **ctx,231		struct device *dev, unsigned short dev_id)232{233	*ctx = devm_kzalloc(dev, sizeof(struct intel_sst_drv), GFP_KERNEL);234	if (!(*ctx))235		return -ENOMEM;236 237	(*ctx)->dev = dev;238	(*ctx)->dev_id = dev_id;239 240	return 0;241}242EXPORT_SYMBOL_GPL(sst_alloc_drv_context);243 244static ssize_t firmware_version_show(struct device *dev,245			    struct device_attribute *attr, char *buf)246{247	struct intel_sst_drv *ctx = dev_get_drvdata(dev);248 249	if (ctx->fw_version.type == 0 && ctx->fw_version.major == 0 &&250	    ctx->fw_version.minor == 0 && ctx->fw_version.build == 0)251		return sysfs_emit(buf, "FW not yet loaded\n");252	else253		return sysfs_emit(buf, "v%02x.%02x.%02x.%02x\n",254				  ctx->fw_version.type, ctx->fw_version.major,255				  ctx->fw_version.minor, ctx->fw_version.build);256 257}258 259static DEVICE_ATTR_RO(firmware_version);260 261static const struct attribute *sst_fw_version_attrs[] = {262	&dev_attr_firmware_version.attr,263	NULL,264};265 266static const struct attribute_group sst_fw_version_attr_group = {267	.attrs = (struct attribute **)sst_fw_version_attrs,268};269 270int sst_context_init(struct intel_sst_drv *ctx)271{272	int ret = 0, i;273 274	if (!ctx->pdata)275		return -EINVAL;276 277	if (!ctx->pdata->probe_data)278		return -EINVAL;279 280	memcpy(&ctx->info, ctx->pdata->probe_data, sizeof(ctx->info));281 282	ret = sst_driver_ops(ctx);283	if (ret != 0)284		return -EINVAL;285 286	sst_init_locks(ctx);287	sst_set_fw_state_locked(ctx, SST_RESET);288 289	/* pvt_id 0 reserved for async messages */290	ctx->pvt_id = 1;291	ctx->stream_cnt = 0;292	ctx->fw_in_mem = NULL;293	/* we use memcpy, so set to 0 */294	ctx->use_dma = 0;295	ctx->use_lli = 0;296 297	if (sst_workqueue_init(ctx))298		return -EINVAL;299 300	ctx->mailbox_recv_offset = ctx->pdata->ipc_info->mbox_recv_off;301	ctx->ipc_reg.ipcx = SST_IPCX + ctx->pdata->ipc_info->ipc_offset;302	ctx->ipc_reg.ipcd = SST_IPCD + ctx->pdata->ipc_info->ipc_offset;303 304	dev_info(ctx->dev, "Got drv data max stream %d\n",305				ctx->info.max_streams);306 307	for (i = 1; i <= ctx->info.max_streams; i++) {308		struct stream_info *stream = &ctx->streams[i];309 310		memset(stream, 0, sizeof(*stream));311		stream->pipe_id = PIPE_RSVD;312		mutex_init(&stream->lock);313	}314 315	/* Register the ISR */316	ret = devm_request_threaded_irq(ctx->dev, ctx->irq_num, ctx->ops->interrupt,317					ctx->ops->irq_thread, 0, SST_DRV_NAME,318					ctx);319	if (ret)320		goto do_free_mem;321 322	dev_dbg(ctx->dev, "Registered IRQ %#x\n", ctx->irq_num);323 324	/* default intr are unmasked so set this as masked */325	sst_shim_write64(ctx->shim, SST_IMRX, 0xFFFF0038);326 327	ctx->qos = devm_kzalloc(ctx->dev,328		sizeof(struct pm_qos_request), GFP_KERNEL);329	if (!ctx->qos) {330		ret = -ENOMEM;331		goto do_free_mem;332	}333	cpu_latency_qos_add_request(ctx->qos, PM_QOS_DEFAULT_VALUE);334 335	dev_dbg(ctx->dev, "Requesting FW %s now...\n", ctx->firmware_name);336	ret = request_firmware_nowait(THIS_MODULE, true, ctx->firmware_name,337				      ctx->dev, GFP_KERNEL, ctx, sst_firmware_load_cb);338	if (ret) {339		dev_err(ctx->dev, "Firmware download failed:%d\n", ret);340		goto do_free_mem;341	}342 343	ret = sysfs_create_group(&ctx->dev->kobj,344				 &sst_fw_version_attr_group);345	if (ret) {346		dev_err(ctx->dev,347			"Unable to create sysfs\n");348		goto err_sysfs;349	}350 351	sst_register(ctx->dev);352	return 0;353err_sysfs:354	sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);355 356do_free_mem:357	destroy_workqueue(ctx->post_msg_wq);358	return ret;359}360EXPORT_SYMBOL_GPL(sst_context_init);361 362void sst_context_cleanup(struct intel_sst_drv *ctx)363{364	pm_runtime_get_noresume(ctx->dev);365	pm_runtime_disable(ctx->dev);366	sst_unregister(ctx->dev);367	sst_set_fw_state_locked(ctx, SST_SHUTDOWN);368	sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);369	destroy_workqueue(ctx->post_msg_wq);370	cpu_latency_qos_remove_request(ctx->qos);371	kfree(ctx->fw_sg_list.src);372	kfree(ctx->fw_sg_list.dst);373	ctx->fw_sg_list.list_len = 0;374	kfree(ctx->fw_in_mem);375	ctx->fw_in_mem = NULL;376	sst_memcpy_free_resources(ctx);377}378EXPORT_SYMBOL_GPL(sst_context_cleanup);379 380void sst_configure_runtime_pm(struct intel_sst_drv *ctx)381{382	pm_runtime_set_autosuspend_delay(ctx->dev, SST_SUSPEND_DELAY);383	pm_runtime_use_autosuspend(ctx->dev);384	/*385	 * For acpi devices, the actual physical device state is386	 * initially active. So change the state to active before387	 * enabling the pm388	 */389 390	if (!acpi_disabled)391		pm_runtime_set_active(ctx->dev);392 393	pm_runtime_enable(ctx->dev);394 395	if (acpi_disabled)396		pm_runtime_set_active(ctx->dev);397	else398		pm_runtime_put_noidle(ctx->dev);399}400EXPORT_SYMBOL_GPL(sst_configure_runtime_pm);401 402static int intel_sst_runtime_suspend(struct device *dev)403{404	int ret = 0;405	struct intel_sst_drv *ctx = dev_get_drvdata(dev);406 407	if (ctx->sst_state == SST_RESET) {408		dev_dbg(dev, "LPE is already in RESET state, No action\n");409		return 0;410	}411	/* save fw context */412	if (ctx->ops->save_dsp_context(ctx))413		return -EBUSY;414 415	/* Move the SST state to Reset */416	sst_set_fw_state_locked(ctx, SST_RESET);417 418	synchronize_irq(ctx->irq_num);419	flush_workqueue(ctx->post_msg_wq);420 421	ctx->ops->reset(ctx);422 423	return ret;424}425 426static int intel_sst_suspend(struct device *dev)427{428	struct intel_sst_drv *ctx = dev_get_drvdata(dev);429	struct sst_fw_save *fw_save;430	int i, ret;431 432	/* check first if we are already in SW reset */433	if (ctx->sst_state == SST_RESET)434		return 0;435 436	/*437	 * check if any stream is active and running438	 * they should already by suspend by soc_suspend439	 */440	for (i = 1; i <= ctx->info.max_streams; i++) {441		struct stream_info *stream = &ctx->streams[i];442 443		if (stream->status == STREAM_RUNNING) {444			dev_err(dev, "stream %d is running, can't suspend, abort\n", i);445			return -EBUSY;446		}447 448		if (ctx->pdata->streams_lost_on_suspend) {449			stream->resume_status = stream->status;450			stream->resume_prev = stream->prev;451			if (stream->status != STREAM_UN_INIT)452				sst_free_stream(ctx, i);453		}454	}455	synchronize_irq(ctx->irq_num);456	flush_workqueue(ctx->post_msg_wq);457 458	/* Move the SST state to Reset */459	sst_set_fw_state_locked(ctx, SST_RESET);460 461	/* tell DSP we are suspending */462	if (ctx->ops->save_dsp_context(ctx))463		return -EBUSY;464 465	/* save the memories */466	fw_save = kzalloc(sizeof(*fw_save), GFP_KERNEL);467	if (!fw_save)468		return -ENOMEM;469	fw_save->iram = kvzalloc(ctx->iram_end - ctx->iram_base, GFP_KERNEL);470	if (!fw_save->iram) {471		ret = -ENOMEM;472		goto iram;473	}474	fw_save->dram = kvzalloc(ctx->dram_end - ctx->dram_base, GFP_KERNEL);475	if (!fw_save->dram) {476		ret = -ENOMEM;477		goto dram;478	}479	fw_save->sram = kvzalloc(SST_MAILBOX_SIZE, GFP_KERNEL);480	if (!fw_save->sram) {481		ret = -ENOMEM;482		goto sram;483	}484 485	fw_save->ddr = kvzalloc(ctx->ddr_end - ctx->ddr_base, GFP_KERNEL);486	if (!fw_save->ddr) {487		ret = -ENOMEM;488		goto ddr;489	}490 491	memcpy32_fromio(fw_save->iram, ctx->iram, ctx->iram_end - ctx->iram_base);492	memcpy32_fromio(fw_save->dram, ctx->dram, ctx->dram_end - ctx->dram_base);493	memcpy32_fromio(fw_save->sram, ctx->mailbox, SST_MAILBOX_SIZE);494	memcpy32_fromio(fw_save->ddr, ctx->ddr, ctx->ddr_end - ctx->ddr_base);495 496	ctx->fw_save = fw_save;497	ctx->ops->reset(ctx);498	return 0;499ddr:500	kvfree(fw_save->sram);501sram:502	kvfree(fw_save->dram);503dram:504	kvfree(fw_save->iram);505iram:506	kfree(fw_save);507	return ret;508}509 510static int intel_sst_resume(struct device *dev)511{512	struct intel_sst_drv *ctx = dev_get_drvdata(dev);513	struct sst_fw_save *fw_save = ctx->fw_save;514	struct sst_block *block;515	int i, ret = 0;516 517	if (!fw_save)518		return 0;519 520	sst_set_fw_state_locked(ctx, SST_FW_LOADING);521 522	/* we have to restore the memory saved */523	ctx->ops->reset(ctx);524 525	ctx->fw_save = NULL;526 527	memcpy32_toio(ctx->iram, fw_save->iram, ctx->iram_end - ctx->iram_base);528	memcpy32_toio(ctx->dram, fw_save->dram, ctx->dram_end - ctx->dram_base);529	memcpy32_toio(ctx->mailbox, fw_save->sram, SST_MAILBOX_SIZE);530	memcpy32_toio(ctx->ddr, fw_save->ddr, ctx->ddr_end - ctx->ddr_base);531 532	kvfree(fw_save->sram);533	kvfree(fw_save->dram);534	kvfree(fw_save->iram);535	kvfree(fw_save->ddr);536	kfree(fw_save);537 538	block = sst_create_block(ctx, 0, FW_DWNL_ID);539	if (block == NULL)540		return -ENOMEM;541 542 543	/* start and wait for ack */544	ctx->ops->start(ctx);545	ret = sst_wait_timeout(ctx, block);546	if (ret) {547		dev_err(ctx->dev, "fw download failed %d\n", ret);548		/* FW download failed due to timeout */549		ret = -EBUSY;550 551	} else {552		sst_set_fw_state_locked(ctx, SST_FW_RUNNING);553	}554 555	if (ctx->pdata->streams_lost_on_suspend) {556		for (i = 1; i <= ctx->info.max_streams; i++) {557			struct stream_info *stream = &ctx->streams[i];558 559			if (stream->resume_status != STREAM_UN_INIT) {560				dev_dbg(ctx->dev, "Re-allocing stream %d status %d prev %d\n",561					i, stream->resume_status,562					stream->resume_prev);563				sst_realloc_stream(ctx, i);564				stream->status = stream->resume_status;565				stream->prev = stream->resume_prev;566			}567		}568	}569 570	sst_free_block(ctx, block);571	return ret;572}573 574const struct dev_pm_ops intel_sst_pm = {575	.suspend = intel_sst_suspend,576	.resume = intel_sst_resume,577	.runtime_suspend = intel_sst_runtime_suspend,578};579EXPORT_SYMBOL_GPL(intel_sst_pm);580