18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Intel SST generic IPC Support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015, Intel Corporation. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/types.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/list.h>
118c2ecf20Sopenharmony_ci#include <linux/wait.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
148c2ecf20Sopenharmony_ci#include <linux/device.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
178c2ecf20Sopenharmony_ci#include <linux/sched.h>
188c2ecf20Sopenharmony_ci#include <linux/delay.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci#include <sound/asound.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "sst-dsp.h"
238c2ecf20Sopenharmony_ci#include "sst-dsp-priv.h"
248c2ecf20Sopenharmony_ci#include "sst-ipc.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* IPC message timeout (msecs) */
278c2ecf20Sopenharmony_ci#define IPC_TIMEOUT_MSECS	300
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define IPC_EMPTY_LIST_SIZE	8
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* locks held by caller */
328c2ecf20Sopenharmony_cistatic struct ipc_message *msg_get_empty(struct sst_generic_ipc *ipc)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct ipc_message *msg = NULL;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	if (!list_empty(&ipc->empty_list)) {
378c2ecf20Sopenharmony_ci		msg = list_first_entry(&ipc->empty_list, struct ipc_message,
388c2ecf20Sopenharmony_ci			list);
398c2ecf20Sopenharmony_ci		list_del(&msg->list);
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	return msg;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int tx_wait_done(struct sst_generic_ipc *ipc,
468c2ecf20Sopenharmony_ci	struct ipc_message *msg, struct sst_ipc_message *reply)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	unsigned long flags;
498c2ecf20Sopenharmony_ci	int ret;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	/* wait for DSP completion (in all cases atm inc pending) */
528c2ecf20Sopenharmony_ci	ret = wait_event_timeout(msg->waitq, msg->complete,
538c2ecf20Sopenharmony_ci		msecs_to_jiffies(IPC_TIMEOUT_MSECS));
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ipc->dsp->spinlock, flags);
568c2ecf20Sopenharmony_ci	if (ret == 0) {
578c2ecf20Sopenharmony_ci		if (ipc->ops.shim_dbg != NULL)
588c2ecf20Sopenharmony_ci			ipc->ops.shim_dbg(ipc, "message timeout");
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci		list_del(&msg->list);
618c2ecf20Sopenharmony_ci		ret = -ETIMEDOUT;
628c2ecf20Sopenharmony_ci	} else {
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		/* copy the data returned from DSP */
658c2ecf20Sopenharmony_ci		if (reply) {
668c2ecf20Sopenharmony_ci			reply->header = msg->rx.header;
678c2ecf20Sopenharmony_ci			if (reply->data)
688c2ecf20Sopenharmony_ci				memcpy(reply->data, msg->rx.data, msg->rx.size);
698c2ecf20Sopenharmony_ci		}
708c2ecf20Sopenharmony_ci		ret = msg->errno;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	list_add_tail(&msg->list, &ipc->empty_list);
748c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
758c2ecf20Sopenharmony_ci	return ret;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic int ipc_tx_message(struct sst_generic_ipc *ipc,
798c2ecf20Sopenharmony_ci	struct sst_ipc_message request,
808c2ecf20Sopenharmony_ci	struct sst_ipc_message *reply, int wait)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	struct ipc_message *msg;
838c2ecf20Sopenharmony_ci	unsigned long flags;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ipc->dsp->spinlock, flags);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	msg = msg_get_empty(ipc);
888c2ecf20Sopenharmony_ci	if (msg == NULL) {
898c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
908c2ecf20Sopenharmony_ci		return -EBUSY;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	msg->tx.header = request.header;
948c2ecf20Sopenharmony_ci	msg->tx.size = request.size;
958c2ecf20Sopenharmony_ci	msg->rx.header = 0;
968c2ecf20Sopenharmony_ci	msg->rx.size = reply ? reply->size : 0;
978c2ecf20Sopenharmony_ci	msg->wait = wait;
988c2ecf20Sopenharmony_ci	msg->errno = 0;
998c2ecf20Sopenharmony_ci	msg->pending = false;
1008c2ecf20Sopenharmony_ci	msg->complete = false;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if ((request.size) && (ipc->ops.tx_data_copy != NULL))
1038c2ecf20Sopenharmony_ci		ipc->ops.tx_data_copy(msg, request.data, request.size);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	list_add_tail(&msg->list, &ipc->tx_list);
1068c2ecf20Sopenharmony_ci	schedule_work(&ipc->kwork);
1078c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ipc->dsp->spinlock, flags);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (wait)
1108c2ecf20Sopenharmony_ci		return tx_wait_done(ipc, msg, reply);
1118c2ecf20Sopenharmony_ci	else
1128c2ecf20Sopenharmony_ci		return 0;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int msg_empty_list_init(struct sst_generic_ipc *ipc)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	int i;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	ipc->msg = kcalloc(IPC_EMPTY_LIST_SIZE, sizeof(struct ipc_message),
1208c2ecf20Sopenharmony_ci			   GFP_KERNEL);
1218c2ecf20Sopenharmony_ci	if (ipc->msg == NULL)
1228c2ecf20Sopenharmony_ci		return -ENOMEM;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
1258c2ecf20Sopenharmony_ci		ipc->msg[i].tx.data = kzalloc(ipc->tx_data_max_size, GFP_KERNEL);
1268c2ecf20Sopenharmony_ci		if (ipc->msg[i].tx.data == NULL)
1278c2ecf20Sopenharmony_ci			goto free_mem;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci		ipc->msg[i].rx.data = kzalloc(ipc->rx_data_max_size, GFP_KERNEL);
1308c2ecf20Sopenharmony_ci		if (ipc->msg[i].rx.data == NULL) {
1318c2ecf20Sopenharmony_ci			kfree(ipc->msg[i].tx.data);
1328c2ecf20Sopenharmony_ci			goto free_mem;
1338c2ecf20Sopenharmony_ci		}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci		init_waitqueue_head(&ipc->msg[i].waitq);
1368c2ecf20Sopenharmony_ci		list_add(&ipc->msg[i].list, &ipc->empty_list);
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return 0;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cifree_mem:
1428c2ecf20Sopenharmony_ci	while (i > 0) {
1438c2ecf20Sopenharmony_ci		kfree(ipc->msg[i-1].tx.data);
1448c2ecf20Sopenharmony_ci		kfree(ipc->msg[i-1].rx.data);
1458c2ecf20Sopenharmony_ci		--i;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci	kfree(ipc->msg);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return -ENOMEM;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic void ipc_tx_msgs(struct work_struct *work)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct sst_generic_ipc *ipc =
1558c2ecf20Sopenharmony_ci		container_of(work, struct sst_generic_ipc, kwork);
1568c2ecf20Sopenharmony_ci	struct ipc_message *msg;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	spin_lock_irq(&ipc->dsp->spinlock);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	while (!list_empty(&ipc->tx_list) && !ipc->pending) {
1618c2ecf20Sopenharmony_ci		/* if the DSP is busy, we will TX messages after IRQ.
1628c2ecf20Sopenharmony_ci		 * also postpone if we are in the middle of processing
1638c2ecf20Sopenharmony_ci		 * completion irq
1648c2ecf20Sopenharmony_ci		 */
1658c2ecf20Sopenharmony_ci		if (ipc->ops.is_dsp_busy && ipc->ops.is_dsp_busy(ipc->dsp)) {
1668c2ecf20Sopenharmony_ci			dev_dbg(ipc->dev, "ipc_tx_msgs dsp busy\n");
1678c2ecf20Sopenharmony_ci			break;
1688c2ecf20Sopenharmony_ci		}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		msg = list_first_entry(&ipc->tx_list, struct ipc_message, list);
1718c2ecf20Sopenharmony_ci		list_move(&msg->list, &ipc->rx_list);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci		if (ipc->ops.tx_msg != NULL)
1748c2ecf20Sopenharmony_ci			ipc->ops.tx_msg(ipc, msg);
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	spin_unlock_irq(&ipc->dsp->spinlock);
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciint sst_ipc_tx_message_wait(struct sst_generic_ipc *ipc,
1818c2ecf20Sopenharmony_ci	struct sst_ipc_message request, struct sst_ipc_message *reply)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	int ret;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	/*
1868c2ecf20Sopenharmony_ci	 * DSP maybe in lower power active state, so
1878c2ecf20Sopenharmony_ci	 * check if the DSP supports DSP lp On method
1888c2ecf20Sopenharmony_ci	 * if so invoke that before sending IPC
1898c2ecf20Sopenharmony_ci	 */
1908c2ecf20Sopenharmony_ci	if (ipc->ops.check_dsp_lp_on)
1918c2ecf20Sopenharmony_ci		if (ipc->ops.check_dsp_lp_on(ipc->dsp, true))
1928c2ecf20Sopenharmony_ci			return -EIO;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	ret = ipc_tx_message(ipc, request, reply, 1);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (ipc->ops.check_dsp_lp_on)
1978c2ecf20Sopenharmony_ci		if (ipc->ops.check_dsp_lp_on(ipc->dsp, false))
1988c2ecf20Sopenharmony_ci			return -EIO;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return ret;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_ipc_tx_message_wait);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciint sst_ipc_tx_message_nowait(struct sst_generic_ipc *ipc,
2058c2ecf20Sopenharmony_ci	struct sst_ipc_message request)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	return ipc_tx_message(ipc, request, NULL, 0);
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_ipc_tx_message_nowait);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ciint sst_ipc_tx_message_nopm(struct sst_generic_ipc *ipc,
2128c2ecf20Sopenharmony_ci	struct sst_ipc_message request, struct sst_ipc_message *reply)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	return ipc_tx_message(ipc, request, reply, 1);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_ipc_tx_message_nopm);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistruct ipc_message *sst_ipc_reply_find_msg(struct sst_generic_ipc *ipc,
2198c2ecf20Sopenharmony_ci	u64 header)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct ipc_message *msg;
2228c2ecf20Sopenharmony_ci	u64 mask;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if (ipc->ops.reply_msg_match != NULL)
2258c2ecf20Sopenharmony_ci		header = ipc->ops.reply_msg_match(header, &mask);
2268c2ecf20Sopenharmony_ci	else
2278c2ecf20Sopenharmony_ci		mask = (u64)-1;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (list_empty(&ipc->rx_list)) {
2308c2ecf20Sopenharmony_ci		dev_err(ipc->dev, "error: rx list empty but received 0x%llx\n",
2318c2ecf20Sopenharmony_ci			header);
2328c2ecf20Sopenharmony_ci		return NULL;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	list_for_each_entry(msg, &ipc->rx_list, list) {
2368c2ecf20Sopenharmony_ci		if ((msg->tx.header & mask) == header)
2378c2ecf20Sopenharmony_ci			return msg;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	return NULL;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_ipc_reply_find_msg);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci/* locks held by caller */
2458c2ecf20Sopenharmony_civoid sst_ipc_tx_msg_reply_complete(struct sst_generic_ipc *ipc,
2468c2ecf20Sopenharmony_ci	struct ipc_message *msg)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	msg->complete = true;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (!msg->wait)
2518c2ecf20Sopenharmony_ci		list_add_tail(&msg->list, &ipc->empty_list);
2528c2ecf20Sopenharmony_ci	else
2538c2ecf20Sopenharmony_ci		wake_up(&msg->waitq);
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_ipc_tx_msg_reply_complete);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ciint sst_ipc_init(struct sst_generic_ipc *ipc)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	int ret;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ipc->tx_list);
2628c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ipc->rx_list);
2638c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&ipc->empty_list);
2648c2ecf20Sopenharmony_ci	init_waitqueue_head(&ipc->wait_txq);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	ret = msg_empty_list_init(ipc);
2678c2ecf20Sopenharmony_ci	if (ret < 0)
2688c2ecf20Sopenharmony_ci		return -ENOMEM;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	INIT_WORK(&ipc->kwork, ipc_tx_msgs);
2718c2ecf20Sopenharmony_ci	return 0;
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_ipc_init);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_civoid sst_ipc_fini(struct sst_generic_ipc *ipc)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	int i;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	cancel_work_sync(&ipc->kwork);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (ipc->msg) {
2828c2ecf20Sopenharmony_ci		for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
2838c2ecf20Sopenharmony_ci			kfree(ipc->msg[i].tx.data);
2848c2ecf20Sopenharmony_ci			kfree(ipc->msg[i].rx.data);
2858c2ecf20Sopenharmony_ci		}
2868c2ecf20Sopenharmony_ci		kfree(ipc->msg);
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(sst_ipc_fini);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci/* Module information */
2928c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jin Yao");
2938c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Intel SST IPC generic");
2948c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
295