// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2013--2024 Intel Corporation
 */

#include <linux/cacheflush.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/pm_runtime.h>
#include <linux/spinlock.h>
#include <linux/types.h>

#include "ipu6-bus.h"
#include "ipu6-fw-com.h"
#include "ipu6-isys.h"
#include "ipu6-platform-isys-csi2-reg.h"
#include "ipu6-platform-regs.h"
#include "ipu7-fw-isys.h"

static const char send_msg_types[N_IPU6_FW_ISYS_SEND_TYPE][32] = {
	"STREAM_OPEN",
	"STREAM_START",
	"STREAM_START_AND_CAPTURE",
	"STREAM_CAPTURE",
	"STREAM_STOP",
	"STREAM_FLUSH",
	"STREAM_CLOSE"
};

static int handle_proxy_response(struct ipu6_isys *isys, unsigned int req_id)
{
	struct device *dev = &isys->adev->auxdev.dev;
	struct ipu6_fw_isys_proxy_resp_info_abi *resp;
	int ret;

	resp = ipu6_recv_get_token(isys->fwctx, IPU6_BASE_PROXY_RECV_QUEUES);
	if (!resp)
		return 1;

	dev_dbg(dev, "Proxy response: id %u, error %u, details %u\n",
		resp->request_id, resp->error_info.error,
		resp->error_info.error_details);

	ret = req_id == resp->request_id ? 0 : -EIO;

	ipu6_recv_put_token(isys->fwctx, IPU6_BASE_PROXY_RECV_QUEUES);

	return ret;
}

int ipu6_fw_isys_send_proxy_token(struct ipu6_isys *isys,
				  unsigned int req_id,
				  unsigned int index,
				  unsigned int offset, u32 value)
{
	struct ipu6_fw_com_context *ctx = isys->fwctx;
	struct device *dev = &isys->adev->auxdev.dev;
	struct ipu6_fw_proxy_send_queue_token *token;
	unsigned int timeout = 1000;
	int ret;

	dev_dbg(dev,
		"proxy send: req_id 0x%x, index %d, offset 0x%x, value 0x%x\n",
		req_id, index, offset, value);

	token = ipu6_send_get_token(ctx, IPU6_BASE_PROXY_SEND_QUEUES);
	if (!token)
		return -EBUSY;

	token->request_id = req_id;
	token->region_index = index;
	token->offset = offset;
	token->value = value;
	ipu6_send_put_token(ctx, IPU6_BASE_PROXY_SEND_QUEUES);

	do {
		usleep_range(100, 110);
		ret = handle_proxy_response(isys, req_id);
		if (!ret)
			break;
		if (ret == -EIO) {
			dev_err(dev, "Proxy respond with unexpected id\n");
			break;
		}
		timeout--;
	} while (ret && timeout);

	if (!timeout)
		dev_err(dev, "Proxy response timed out\n");

	return ret;
}

static int ipu6_fw_isys_complex_cmd(struct ipu6_isys *isys,
				    const unsigned int stream_handle,
				    void *cpu_mapped_buf,
				    dma_addr_t dma_mapped_buf,
				    size_t size, u16 send_type)
{
	struct ipu6_fw_com_context *ctx = isys->fwctx;
	struct device *dev = &isys->adev->auxdev.dev;
	struct ipu6_fw_send_queue_token *token;

	if (send_type >= N_IPU6_FW_ISYS_SEND_TYPE)
		return -EINVAL;

	dev_dbg(dev, "send_token: %s\n", send_msg_types[send_type]);

	/*
	 * Time to flush cache in case we have some payload. Not all messages
	 * have that
	 */
	if (cpu_mapped_buf)
		clflush_cache_range(cpu_mapped_buf, size);

	token = ipu6_send_get_token(ctx,
				    stream_handle + IPU6_BASE_MSG_SEND_QUEUES);
	if (!token)
		return -EBUSY;

	token->payload = dma_mapped_buf;
	token->buf_handle = (unsigned long)cpu_mapped_buf;
	token->send_type = send_type;

	ipu6_send_put_token(ctx, stream_handle + IPU6_BASE_MSG_SEND_QUEUES);

	return 0;
}

static int ipu6_fw_isys_close(struct ipu6_isys *isys)
{
	struct device *dev = &isys->adev->auxdev.dev;
	int retry = IPU6_ISYS_CLOSE_RETRY;
	void *fwctx;
	int ret;

	/*
	 * Stop the isys fw. Actual close takes
	 * some time as the FW must stop its actions including code fetch
	 * to SP icache.
	 * spinlock to wait the interrupt handler to be finished
	 */
	ret = ipu6_fw_com_close(isys->fwctx);
	fwctx = isys->fwctx;
	isys->fwctx = NULL;
	if (ret)
		dev_err(dev, "Device close failure: %d\n", ret);

	/* release probably fails if the close failed. Let's try still */
	do {
		usleep_range(400, 500);
		ret = ipu6_fw_com_release(fwctx, 0);
		retry--;
	} while (ret && retry);

	if (ret) {
		dev_err(dev, "Device release time out %d\n", ret);
		isys->fwctx = fwctx;
	}

	return ret;
}

static void ipu6_fw_isys_cleanup(struct ipu6_isys *isys)
{
	int ret;

	ret = ipu6_fw_com_release(isys->fwctx, 1);
	if (ret < 0)
		dev_warn(&isys->adev->auxdev.dev,
			 "Device busy, fw_com release failed.");
	isys->fwctx = NULL;
}

static void start_sp(struct ipu6_bus_device *adev)
{
	struct ipu6_isys *isys = ipu6_bus_get_drvdata(adev);
	void __iomem *spc_regs_base = isys->pdata->base +
		isys->pdata->ipdata->hw_variant.spc_offset;
	u32 val = IPU6_ISYS_SPC_STATUS_START |
		IPU6_ISYS_SPC_STATUS_RUN |
		IPU6_ISYS_SPC_STATUS_CTRL_ICACHE_INVALIDATE;

	val |= isys->icache_prefetch ? IPU6_ISYS_SPC_STATUS_ICACHE_PREFETCH : 0;

	writel(val, spc_regs_base + IPU6_ISYS_REG_SPC_STATUS_CTRL);
}

static int query_sp(struct ipu6_bus_device *adev)
{
	struct ipu6_isys *isys = ipu6_bus_get_drvdata(adev);
	void __iomem *spc_regs_base = isys->pdata->base +
		isys->pdata->ipdata->hw_variant.spc_offset;
	u32 val;

	val = readl(spc_regs_base + IPU6_ISYS_REG_SPC_STATUS_CTRL);
	/* return true when READY == 1, START == 0 */
	val &= IPU6_ISYS_SPC_STATUS_READY | IPU6_ISYS_SPC_STATUS_START;

	return val == IPU6_ISYS_SPC_STATUS_READY;
}

static int ipu6_isys_fwcom_cfg_init(struct ipu6_isys *isys,
				    struct ipu6_fw_com_cfg *fwcom_cfg,
				    unsigned int num_streams)
{
	unsigned int max_send_queues, max_sram_blocks, max_devq_size;
	struct ipu6_fw_syscom_queue_config *input_queue_cfg;
	struct ipu6_fw_syscom_queue_config *output_queue_cfg;
	int type_proxy = IPU6_FW_ISYS_QUEUE_TYPE_PROXY;
	int type_dev = IPU6_FW_ISYS_QUEUE_TYPE_DEV;
	int type_msg = IPU6_FW_ISYS_QUEUE_TYPE_MSG;
	int base_dev_send = IPU6_BASE_DEV_SEND_QUEUES;
	int base_msg_send = IPU6_BASE_MSG_SEND_QUEUES;
	int base_msg_recv = IPU6_BASE_MSG_RECV_QUEUES;
	struct ipu6_fw_isys_fw_config *isys_fw_cfg;
	u32 num_in_message_queues;
	unsigned int max_streams;
	unsigned int i;

	max_streams = isys->pdata->ipdata->max_streams;
	max_send_queues = isys->pdata->ipdata->max_send_queues;
	max_sram_blocks = isys->pdata->ipdata->max_sram_blocks;
	max_devq_size = isys->pdata->ipdata->max_devq_size;
	num_in_message_queues = clamp(num_streams, 1U, max_streams);
	isys_fw_cfg = kzalloc_obj(*isys_fw_cfg);
	if (!isys_fw_cfg)
		return -ENOMEM;

	isys_fw_cfg->num_send_queues[type_proxy] = IPU6_N_MAX_PROXY_SEND_QUEUES;
	isys_fw_cfg->num_send_queues[type_dev] = IPU6_N_MAX_DEV_SEND_QUEUES;
	isys_fw_cfg->num_send_queues[type_msg] = num_in_message_queues;
	isys_fw_cfg->num_recv_queues[type_proxy] = IPU6_N_MAX_PROXY_RECV_QUEUES;
	/* Common msg/dev return queue */
	isys_fw_cfg->num_recv_queues[type_dev] = 0;
	isys_fw_cfg->num_recv_queues[type_msg] = 1;

	input_queue_cfg = kzalloc_objs(*input_queue_cfg, max_send_queues);
	if (!input_queue_cfg)
		goto err_free_fw_cfg;

	output_queue_cfg = kzalloc_objs(*output_queue_cfg,
					IPU6_N_MAX_RECV_QUEUES);
	if (!output_queue_cfg)
		goto err_free_input_queue_cfg;

	fwcom_cfg->input = input_queue_cfg;
	fwcom_cfg->output = output_queue_cfg;

	fwcom_cfg->num_input_queues =
		isys_fw_cfg->num_send_queues[type_proxy] +
		isys_fw_cfg->num_send_queues[type_dev] +
		isys_fw_cfg->num_send_queues[type_msg];

	fwcom_cfg->num_output_queues =
		isys_fw_cfg->num_recv_queues[type_proxy] +
		isys_fw_cfg->num_recv_queues[type_dev] +
		isys_fw_cfg->num_recv_queues[type_msg];

	/* SRAM partitioning. Equal partitioning is set. */
	for (i = 0; i < max_sram_blocks; i++) {
		if (i < num_in_message_queues)
			isys_fw_cfg->buffer_partition.num_gda_pages[i] =
				(IPU6_DEVICE_GDA_NR_PAGES *
				 IPU6_DEVICE_GDA_VIRT_FACTOR) /
				num_in_message_queues;
		else
			isys_fw_cfg->buffer_partition.num_gda_pages[i] = 0;
	}

	/* FW assumes proxy interface at fwcom_cfg queue 0 */
	for (i = 0; i < isys_fw_cfg->num_send_queues[type_proxy]; i++) {
		input_queue_cfg[i].token_size =
			sizeof(struct ipu6_fw_proxy_send_queue_token);
		input_queue_cfg[i].queue_size = IPU6_ISYS_SIZE_PROXY_SEND_QUEUE;
	}

	for (i = 0; i < isys_fw_cfg->num_send_queues[type_dev]; i++) {
		input_queue_cfg[base_dev_send + i].token_size =
			sizeof(struct ipu6_fw_send_queue_token);
		input_queue_cfg[base_dev_send + i].queue_size = max_devq_size;
	}

	for (i = 0; i < isys_fw_cfg->num_send_queues[type_msg]; i++) {
		input_queue_cfg[base_msg_send + i].token_size =
			sizeof(struct ipu6_fw_send_queue_token);
		input_queue_cfg[base_msg_send + i].queue_size =
			IPU6_ISYS_SIZE_SEND_QUEUE;
	}

	for (i = 0; i < isys_fw_cfg->num_recv_queues[type_proxy]; i++) {
		output_queue_cfg[i].token_size =
			sizeof(struct ipu6_fw_proxy_resp_queue_token);
		output_queue_cfg[i].queue_size =
			IPU6_ISYS_SIZE_PROXY_RECV_QUEUE;
	}
	/* There is no recv DEV queue */
	for (i = 0; i < isys_fw_cfg->num_recv_queues[type_msg]; i++) {
		output_queue_cfg[base_msg_recv + i].token_size =
			sizeof(struct ipu6_fw_resp_queue_token);
		output_queue_cfg[base_msg_recv + i].queue_size =
			IPU6_ISYS_SIZE_RECV_QUEUE;
	}

	fwcom_cfg->dmem_addr = isys->pdata->ipdata->hw_variant.dmem_offset;
	fwcom_cfg->specific_addr = isys_fw_cfg;
	fwcom_cfg->specific_size = sizeof(*isys_fw_cfg);

	return 0;

err_free_input_queue_cfg:
	kfree(input_queue_cfg);
err_free_fw_cfg:
	kfree(isys_fw_cfg);

	return -ENOMEM;
}

static void ipu6_isys_fwcom_cfg_free(struct ipu6_fw_com_cfg *fwcom_cfg)
{
	kfree(fwcom_cfg->specific_addr);
	kfree(fwcom_cfg->output);
	kfree(fwcom_cfg->input);
}

static int ipu6_fw_isys_init(struct ipu6_isys *isys, unsigned int num_streams)
{
	struct device *dev = &isys->adev->auxdev.dev;
	int retry = IPU6_ISYS_OPEN_RETRY;
	struct ipu6_fw_com_cfg fwcom_cfg = {
		.cell_start = start_sp,
		.cell_ready = query_sp,
		.buttress_boot_offset = SYSCOM_BUTTRESS_FW_PARAMS_ISYS_OFFSET,
	};
	int ret;

	ret = ipu6_isys_fwcom_cfg_init(isys, &fwcom_cfg, num_streams);
	if (ret)
		return ret;

	isys->fwctx = ipu6_fw_com_prepare(&fwcom_cfg, isys->adev,
					  isys->pdata->base);
	ipu6_isys_fwcom_cfg_free(&fwcom_cfg);
	if (!isys->fwctx) {
		dev_err(dev, "isys fw com prepare failed\n");
		return -EIO;
	}

	ret = ipu6_fw_com_open(isys->fwctx);
	if (ret) {
		dev_err(dev, "isys fw com open failed %d\n", ret);
		return ret;
	}

	do {
		usleep_range(400, 500);
		if (ipu6_fw_com_ready(isys->fwctx))
			break;
		retry--;
	} while (retry > 0);

	if (!retry) {
		dev_err(dev, "isys port open ready failed %d\n", ret);
		ipu6_fw_isys_close(isys);
		ret = -EIO;
	}

	return ret;
}

static struct ipu6_fw_isys_resp_info_abi *
ipu6_fw_isys_get_resp(struct ipu6_isys *isys)
{
	return ipu6_recv_get_token(isys->fwctx, IPU6_BASE_MSG_RECV_QUEUES);
}

static void ipu6_fw_isys_put_resp(struct ipu6_isys *isys)
{
	ipu6_recv_put_token(isys->fwctx, IPU6_BASE_MSG_RECV_QUEUES);
}

static void ipu6_fw_isys_dump_stream_cfg(struct device *dev,
					 struct isys_fw_msgs *msg)
{
	struct ipu6_fw_isys_stream_cfg_data_abi *cfg = &msg->ipu6.stream;
	unsigned int i;

	dev_dbg(dev, "-----------------------------------------------------\n");
	dev_dbg(dev, "IPU6_FW_ISYS_STREAM_CFG_DATA\n");

	dev_dbg(dev, "compfmt = %d\n", cfg->vc);
	dev_dbg(dev, "src = %d\n", cfg->src);
	dev_dbg(dev, "vc = %d\n", cfg->vc);
	dev_dbg(dev, "isl_use = %d\n", cfg->isl_use);
	dev_dbg(dev, "sensor_type = %d\n", cfg->sensor_type);

	dev_dbg(dev, "send_irq_sof_discarded = %d\n",
		cfg->send_irq_sof_discarded);
	dev_dbg(dev, "send_irq_eof_discarded = %d\n",
		cfg->send_irq_eof_discarded);
	dev_dbg(dev, "send_resp_sof_discarded = %d\n",
		cfg->send_resp_sof_discarded);
	dev_dbg(dev, "send_resp_eof_discarded = %d\n",
		cfg->send_resp_eof_discarded);

	dev_dbg(dev, "crop:\n");
	dev_dbg(dev, "\t.left_top = [%d, %d]\n", cfg->crop.left_offset,
		cfg->crop.top_offset);
	dev_dbg(dev, "\t.right_bottom = [%d, %d]\n", cfg->crop.right_offset,
		cfg->crop.bottom_offset);

	dev_dbg(dev, "nof_input_pins = %d\n", cfg->nof_input_pins);
	for (i = 0; i < cfg->nof_input_pins; i++) {
		dev_dbg(dev, "input pin[%d]:\n", i);
		dev_dbg(dev, "\t.dt = 0x%0x\n", cfg->input_pins[i].dt);
		dev_dbg(dev, "\t.mipi_store_mode = %d\n",
			cfg->input_pins[i].mipi_store_mode);
		dev_dbg(dev, "\t.bits_per_pix = %d\n",
			cfg->input_pins[i].bits_per_pix);
		dev_dbg(dev, "\t.mapped_dt = 0x%0x\n",
			cfg->input_pins[i].mapped_dt);
		dev_dbg(dev, "\t.input_res = %dx%d\n",
			cfg->input_pins[i].input_res.width,
			cfg->input_pins[i].input_res.height);
		dev_dbg(dev, "\t.mipi_decompression = %d\n",
			cfg->input_pins[i].mipi_decompression);
		dev_dbg(dev, "\t.capture_mode = %d\n",
			cfg->input_pins[i].capture_mode);
	}

	dev_dbg(dev, "nof_output_pins = %d\n", cfg->nof_output_pins);
	for (i = 0; i < cfg->nof_output_pins; i++) {
		dev_dbg(dev, "output_pin[%d]:\n", i);
		dev_dbg(dev, "\t.input_pin_id = %d\n",
			cfg->output_pins[i].input_pin_id);
		dev_dbg(dev, "\t.output_res = %dx%d\n",
			cfg->output_pins[i].output_res.width,
			cfg->output_pins[i].output_res.height);
		dev_dbg(dev, "\t.stride = %d\n", cfg->output_pins[i].stride);
		dev_dbg(dev, "\t.pt = %d\n", cfg->output_pins[i].pt);
		dev_dbg(dev, "\t.payload_buf_size = %d\n",
			cfg->output_pins[i].payload_buf_size);
		dev_dbg(dev, "\t.ft = %d\n", cfg->output_pins[i].ft);
		dev_dbg(dev, "\t.watermark_in_lines = %d\n",
			cfg->output_pins[i].watermark_in_lines);
		dev_dbg(dev, "\t.send_irq = %d\n",
			cfg->output_pins[i].send_irq);
		dev_dbg(dev, "\t.reserve_compression = %d\n",
			cfg->output_pins[i].reserve_compression);
		dev_dbg(dev, "\t.snoopable = %d\n",
			cfg->output_pins[i].snoopable);
		dev_dbg(dev, "\t.error_handling_enable = %d\n",
			cfg->output_pins[i].error_handling_enable);
		dev_dbg(dev, "\t.sensor_type = %d\n",
			cfg->output_pins[i].sensor_type);
	}
	dev_dbg(dev, "-----------------------------------------------------\n");
}

static void
ipu6_fw_isys_dump_frame_buf_set(struct device *dev, struct isys_fw_msgs *msg,
				unsigned int outputs)
{
	struct ipu6_fw_isys_frame_buff_set_abi *buf;
	unsigned int i;

	buf = &msg->ipu6.frame;

	dev_dbg(dev, "-----------------------------------------------------\n");
	dev_dbg(dev, "IPU6_FW_ISYS_FRAME_BUFF_SET\n");

	for (i = 0; i < outputs; i++) {
		dev_dbg(dev, "output_pin[%d]:\n", i);
		dev_dbg(dev, "\t.out_buf_id = %llu\n",
			buf->output_pins[i].out_buf_id);
		dev_dbg(dev, "\t.addr = 0x%x\n", buf->output_pins[i].addr);
		dev_dbg(dev, "\t.compress = %d\n",
			buf->output_pins[i].compress);
	}

	dev_dbg(dev, "send_irq_sof = 0x%x\n", buf->send_irq_sof);
	dev_dbg(dev, "send_irq_eof = 0x%x\n", buf->send_irq_eof);
	dev_dbg(dev, "send_resp_sof = 0x%x\n", buf->send_resp_sof);
	dev_dbg(dev, "send_resp_eof = 0x%x\n", buf->send_resp_eof);
	dev_dbg(dev, "send_irq_capture_ack = 0x%x\n",
		buf->send_irq_capture_ack);
	dev_dbg(dev, "send_irq_capture_done = 0x%x\n",
		buf->send_irq_capture_done);
	dev_dbg(dev, "send_resp_capture_ack = 0x%x\n",
		buf->send_resp_capture_ack);
	dev_dbg(dev, "send_resp_capture_done = 0x%x\n",
		buf->send_resp_capture_done);

	dev_dbg(dev, "-----------------------------------------------------\n");
}

struct fwmsg {
	int type;
	char *msg;
	bool valid_ts;
};

static const struct fwmsg fw_msg[] = {
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_OPEN_DONE, "STREAM_OPEN_DONE", 0},
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_CLOSE_ACK, "STREAM_CLOSE_ACK", 0},
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_START_ACK, "STREAM_START_ACK", 0},
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_START_AND_CAPTURE_ACK,
	 "STREAM_START_AND_CAPTURE_ACK", 0},
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_STOP_ACK, "STREAM_STOP_ACK", 0},
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_FLUSH_ACK, "STREAM_FLUSH_ACK", 0},
	{IPU6_FW_ISYS_RESP_TYPE_PIN_DATA_READY, "PIN_DATA_READY", 1},
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_CAPTURE_ACK, "STREAM_CAPTURE_ACK", 0},
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_START_AND_CAPTURE_DONE,
	 "STREAM_START_AND_CAPTURE_DONE", 1},
	{IPU6_FW_ISYS_RESP_TYPE_STREAM_CAPTURE_DONE, "STREAM_CAPTURE_DONE", 1},
	{IPU6_FW_ISYS_RESP_TYPE_FRAME_SOF, "FRAME_SOF", 1},
	{IPU6_FW_ISYS_RESP_TYPE_FRAME_EOF, "FRAME_EOF", 1},
	{IPU6_FW_ISYS_RESP_TYPE_STATS_DATA_READY, "STATS_READY", 1},
	{-1, "UNKNOWN MESSAGE", 0}
};

static u32 resp_type_to_index(int type)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(fw_msg); i++)
		if (fw_msg[i].type == type)
			return i;

	return  ARRAY_SIZE(fw_msg) - 1;
}

int ipu6_isys_isr_one(struct ipu6_bus_device *adev)
{
	struct ipu6_isys *isys = ipu6_bus_get_drvdata(adev);
	struct ipu6_fw_isys_resp_info_abi *resp;
	struct ipu6_isys_stream *stream;
	struct ipu6_isys_csi2 *csi2 = NULL;
	struct isys_fw_msgs *isys_fw_msg = NULL;
	u32 index;
	u64 ts;

	resp = ipu6_fw_isys_get_resp(isys);
	if (!resp)
		return 1;

	ts = (u64)resp->timestamp[1] << 32 | resp->timestamp[0];

	index = resp_type_to_index(resp->type);
	dev_dbg(&adev->auxdev.dev,
		"FW resp %02d %s, stream %u, ts 0x%16.16llx, pin %d\n",
		resp->type, fw_msg[index].msg, resp->stream_handle,
		fw_msg[index].valid_ts ? ts : 0, resp->pin_id);

	if (resp->error_info.error == IPU6_FW_ISYS_ERROR_STREAM_IN_SUSPENSION)
		/* Suspension is kind of special case: not enough buffers */
		dev_dbg(&adev->auxdev.dev,
			"FW error resp SUSPENSION, details %d\n",
			resp->error_info.error_details);
	else if (resp->error_info.error)
		dev_dbg(&adev->auxdev.dev,
			"FW error resp error %d, details %d\n",
			resp->error_info.error, resp->error_info.error_details);

	guard(spinlock_irqsave)(&isys->streams_lock);

	if (resp->stream_handle >= IPU6_ISYS_MAX_STREAMS) {
		dev_err(&adev->auxdev.dev, "bad stream handle %u\n",
			resp->stream_handle);
		goto leave;
	}

	stream = resp->stream_handle < IPU6_ISYS_MAX_STREAMS ?
		isys->streams_by_handle[resp->stream_handle] : NULL;
	if (!stream) {
		dev_err(&adev->auxdev.dev, "stream of stream_handle %u is unused\n",
			resp->stream_handle);
		goto leave;
	}
	stream->error = resp->error_info.error;

	csi2 = ipu6_isys_subdev_to_csi2(stream->asd);

	switch (resp->type) {
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_OPEN_DONE:
		complete(&stream->stream_open_completion);
		break;
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_CLOSE_ACK:
		complete(&stream->stream_close_completion);
		break;
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_START_ACK:
		complete(&stream->stream_start_completion);
		break;
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_START_AND_CAPTURE_ACK:
		complete(&stream->stream_start_completion);
		break;
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_STOP_ACK:
		complete(&stream->stream_stop_completion);
		break;
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_FLUSH_ACK:
		complete(&stream->stream_stop_completion);
		break;
	case IPU6_FW_ISYS_RESP_TYPE_PIN_DATA_READY:
		/*
		 * firmware only release the capture message until software
		 * get pin_data_ready event
		 */
		if (!resp->buf_id) {
			dev_warn(&adev->auxdev.dev, "%d: Invalid buf ID\n",
				 resp->stream_handle);
			goto leave;
		}

		isys_fw_msg = container_of((void *)(uintptr_t)resp->buf_id,
					   struct isys_fw_msgs, dummy);

		ipu6_put_fw_msg_buf(ipu6_bus_get_drvdata(adev), isys_fw_msg);
		if (resp->pin_id < IPU6_ISYS_OUTPUT_PINS &&
		    stream->output_pins_queue[resp->pin_id])
			ipu6_isys_queue_buf_ready(stream, resp);
		else
			dev_warn(&adev->auxdev.dev,
				 "%d:No queue for pin id %d\n",
				 resp->stream_handle, resp->pin_id);
		if (csi2)
			ipu6_isys_csi2_error(csi2);

		break;
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_CAPTURE_ACK:
		break;
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_START_AND_CAPTURE_DONE:
	case IPU6_FW_ISYS_RESP_TYPE_STREAM_CAPTURE_DONE:
		break;
	case IPU6_FW_ISYS_RESP_TYPE_FRAME_SOF:

		ipu6_isys_csi2_sof_event_by_stream(stream);
		stream->seq[stream->seq_index].sequence =
			atomic_read(&stream->sequence) - 1;
		stream->seq[stream->seq_index].timestamp = ts;
		dev_dbg(&adev->auxdev.dev,
			"sof: handle %d: (index %u), timestamp 0x%16.16llx\n",
			resp->stream_handle,
			stream->seq[stream->seq_index].sequence, ts);
		stream->seq_index = (stream->seq_index + 1)
			% IPU6_ISYS_MAX_PARALLEL_SOF;
		break;
	case IPU6_FW_ISYS_RESP_TYPE_FRAME_EOF:
		ipu6_isys_csi2_eof_event_by_stream(stream);
		dev_dbg(&adev->auxdev.dev,
			"eof: handle %d: (index %u), timestamp 0x%16.16llx\n",
			resp->stream_handle,
			stream->seq[stream->seq_index].sequence, ts);
		break;
	case IPU6_FW_ISYS_RESP_TYPE_STATS_DATA_READY:
		break;
	default:
		dev_err(&adev->auxdev.dev, "%d:unknown response type %u\n",
			resp->stream_handle, resp->type);
		break;
	}

leave:
	ipu6_fw_isys_put_resp(isys);
	return 0;
}

static void ipu6_isys_csi2_isr(struct ipu6_isys_csi2 *csi2)
{
	struct ipu6_isys_stream *stream;
	unsigned int i;
	u32 status;

	ipu6_isys_register_errors(csi2);

	status = readl(csi2->base + CSI_PORT_REG_BASE_IRQ_CSI_SYNC +
		       CSI_PORT_REG_BASE_IRQ_STATUS_OFFSET);

	writel(status, csi2->base + CSI_PORT_REG_BASE_IRQ_CSI_SYNC +
	       CSI_PORT_REG_BASE_IRQ_CLEAR_OFFSET);

	scoped_guard(spinlock, &csi2->isys->streams_lock) {
		for (i = 0; i < NR_OF_CSI2_VC; i++) {
			if (status & IPU_CSI_RX_IRQ_FS_VC(i)) {
				stream = csi2->streams_by_vc[i];
				if (!stream)
					continue;

				ipu6_isys_csi2_sof_event_by_stream(stream);
			}

			if (status & IPU_CSI_RX_IRQ_FE_VC(i)) {
				stream = csi2->streams_by_vc[i];
				if (!stream)
					continue;

				ipu6_isys_csi2_eof_event_by_stream(stream);
			}
		}
	}
}

irqreturn_t ipu6_isys_isr(struct ipu6_bus_device *adev)
{
	struct ipu6_isys *isys = ipu6_bus_get_drvdata(adev);
	void __iomem *base = isys->pdata->base;
	u32 status_sw, status_csi;
	u32 ctrl0_status, ctrl0_clear;
	int pm_status;

	pm_status = pm_runtime_get_if_active(&adev->auxdev.dev);
	if (!pm_status)
		return 0;

	ctrl0_status = isys->pdata->ipdata->csi2.ctrl0_irq_status;
	ctrl0_clear = isys->pdata->ipdata->csi2.ctrl0_irq_clear;

	status_csi = readl(isys->pdata->base + ctrl0_status);
	status_sw = readl(isys->pdata->base +
			  IPU6_REG_ISYS_UNISPART_IRQ_STATUS);

	writel(ISYS_UNISPART_IRQS & ~IPU6_ISYS_UNISPART_IRQ_SW,
	       base + IPU6_REG_ISYS_UNISPART_IRQ_MASK);

	do {
		writel(status_csi, isys->pdata->base + ctrl0_clear);

		writel(status_sw, isys->pdata->base +
		       IPU6_REG_ISYS_UNISPART_IRQ_CLEAR);

		if (isys->isr_csi2_bits & status_csi) {
			unsigned int i;

			for (i = 0; i < isys->pdata->ipdata->csi2.nports; i++) {
				/* irq from not enabled port */
				if (!isys->csi2[i].base)
					continue;
				if (status_csi & IPU6_ISYS_UNISPART_IRQ_CSI2(i))
					ipu6_isys_csi2_isr(&isys->csi2[i]);
			}
		}

		writel(0, base + IPU6_REG_ISYS_UNISPART_SW_IRQ_REG);

		if (!ipu6_isys_isr_one(adev))
			status_sw = IPU6_ISYS_UNISPART_IRQ_SW;
		else
			status_sw = 0;

		status_csi = readl(isys->pdata->base + ctrl0_status);
		status_sw |= readl(isys->pdata->base +
				   IPU6_REG_ISYS_UNISPART_IRQ_STATUS);
	} while ((status_csi & isys->isr_csi2_bits) ||
		 (status_sw & IPU6_ISYS_UNISPART_IRQ_SW));

	writel(ISYS_UNISPART_IRQS, base + IPU6_REG_ISYS_UNISPART_IRQ_MASK);

	if (pm_status > 0)
		pm_runtime_put(&adev->auxdev.dev);

	return IRQ_HANDLED;
}

static int ipu6_isys_fw_pin_cfg(struct ipu6_isys_video *av,
				struct ipu6_isys_stream *stream,
				struct media_pad *src_pad,
				struct v4l2_mbus_frame_desc_entry *entry,
				void *__cfg)
{
	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(src_pad->entity);
	struct v4l2_subdev_state *state = v4l2_subdev_get_locked_active_state(sd);
	struct ipu6_fw_isys_stream_cfg_data_abi *cfg = __cfg;
	struct ipu6_fw_isys_input_pin_info_abi *input_pin;
	struct ipu6_fw_isys_output_pin_info_abi *output_pin;
	struct ipu6_isys_queue *aq = &av->aq;
	struct v4l2_mbus_framefmt *fmt;
	const struct ipu6_isys_pixelformat *pfmt =
		ipu6_isys_get_isys_format(ipu6_isys_get_format(av), 0);
	struct v4l2_rect v4l2_crop;
	struct ipu6_isys *isys = av->isys;
	int input_pins = cfg->nof_input_pins++;
	int output_pins;
	u32 src_stream;

	src_stream = __ipu6_isys_get_src_stream_by_src_pad(state, src_pad->index);
	fmt = v4l2_subdev_state_get_format(state, src_pad->index, src_stream);
	v4l2_crop = *v4l2_subdev_state_get_crop(state, src_pad->index, src_stream);

	input_pin = &cfg->input_pins[input_pins];
	input_pin->input_res.width = fmt->width;
	input_pin->input_res.height = fmt->height;
	input_pin->dt = entry->bus.csi2.dt;
	input_pin->bits_per_pix = pfmt->bpp_packed;
	input_pin->mapped_dt = 0x40; /* invalid mipi data type */
	input_pin->mipi_decompression = 0;
	input_pin->capture_mode = IPU6_FW_ISYS_CAPTURE_MODE_REGULAR;
	input_pin->mipi_store_mode = pfmt->bpp == pfmt->bpp_packed ?
		IPU6_FW_ISYS_MIPI_STORE_MODE_DISCARD_LONG_HEADER :
		IPU6_FW_ISYS_MIPI_STORE_MODE_NORMAL;
	input_pin->crop_first_and_last_lines = v4l2_crop.top & 1;

	output_pins = cfg->nof_output_pins++;
	aq->fw_output = output_pins;
	stream->output_pins_queue[output_pins] = aq;

	output_pin = &cfg->output_pins[output_pins];
	output_pin->input_pin_id = input_pins;
	output_pin->output_res.width = ipu6_isys_get_frame_width(av);
	output_pin->output_res.height = ipu6_isys_get_frame_height(av);

	output_pin->stride = ipu6_isys_get_bytes_per_line(av);
	if (pfmt->bpp != pfmt->bpp_packed)
		output_pin->pt = IPU6_FW_ISYS_PIN_TYPE_RAW_SOC;
	else
		output_pin->pt = IPU6_FW_ISYS_PIN_TYPE_MIPI;
	output_pin->ft = pfmt->css_pixelformat;
	output_pin->send_irq = 1;
	memset(output_pin->ts_offsets, 0, sizeof(output_pin->ts_offsets));
	output_pin->s2m_pixel_soc_pixel_remapping =
		S2M_PIXEL_SOC_PIXEL_REMAPPING_FLAG_NO_REMAPPING;
	output_pin->csi_be_soc_pixel_remapping =
		CSI_BE_SOC_PIXEL_REMAPPING_FLAG_NO_REMAPPING;

	output_pin->snoopable = true;
	output_pin->error_handling_enable = false;
	output_pin->sensor_type = isys->sensor_type++;
	if (isys->sensor_type > isys->pdata->ipdata->sensor_type_end)
		isys->sensor_type = isys->pdata->ipdata->sensor_type_start;

	return 0;
}

static int ipu6_fw_isys_prepare_stream_cfg(struct ipu6_isys_stream *stream,
					   struct v4l2_mbus_frame_desc *desc,
					   struct isys_fw_msgs *msg)
{
	struct ipu6_fw_isys_stream_cfg_data_abi *stream_cfg;
	struct device *dev = &stream->isys->adev->auxdev.dev;
	int ret;

	stream_cfg = &msg->ipu6.stream;
	stream_cfg->src = stream->asd->source;
	stream_cfg->vc = stream->vc;
	stream_cfg->isl_use = 0;
	stream_cfg->sensor_type = IPU6_FW_ISYS_SENSOR_MODE_NORMAL;

	ret = ipu6_isys_fw_pins_prepare(stream, desc, ipu6_isys_fw_pin_cfg,
					stream_cfg);
	if (ret)
		return ret;

	ipu6_fw_isys_dump_stream_cfg(dev, msg);

	stream->nr_output_pins = stream_cfg->nof_output_pins;

	return 0;
}

static int ipu6_fw_isys_stream_open(struct ipu6_isys *isys,
				    const unsigned int stream_handle,
				    struct isys_fw_msgs *msg)
{
	return ipu6_fw_isys_complex_cmd(isys, stream_handle,
				       &msg->ipu6.stream, msg->dma_addr,
				       sizeof(msg->ipu6.stream),
				       IPU6_FW_ISYS_SEND_TYPE_STREAM_OPEN);
}

static int ipu6_fw_isys_stream_close(struct ipu6_isys *isys,
				     const unsigned int stream_handle)
{
	return ipu6_fw_isys_complex_cmd(isys, stream_handle, NULL, 0, 0,
					IPU6_FW_ISYS_SEND_TYPE_STREAM_CLOSE);
}

static int ipu6_fw_isys_stream_flush(struct ipu6_isys *isys,
				     const unsigned int stream_handle)
{
	return ipu6_fw_isys_complex_cmd(isys, stream_handle, NULL, 0, 0,
					IPU6_FW_ISYS_SEND_TYPE_STREAM_FLUSH);
}

static int ipu6_fw_isys_stream_start(struct ipu6_isys *isys,
				     const unsigned int stream_handle,
				     struct isys_fw_msgs *msg, bool capture)
{
	u16 cmd_type;

	if (capture)
		cmd_type = IPU6_FW_ISYS_SEND_TYPE_STREAM_START_AND_CAPTURE;
	else
		cmd_type = IPU6_FW_ISYS_SEND_TYPE_STREAM_START;

	return ipu6_fw_isys_complex_cmd(isys, stream_handle,
					&msg->ipu6.stream, msg->dma_addr,
					sizeof(msg->ipu6.stream), cmd_type);
}

static int ipu6_fw_isys_stream_capture(struct ipu6_isys *isys,
				       const unsigned int stream_handle,
				       struct isys_fw_msgs *msg)
{
	return ipu6_fw_isys_complex_cmd(isys, stream_handle,
					&msg->ipu6.stream, msg->dma_addr,
					sizeof(msg->ipu6.stream),
					IPU6_FW_ISYS_SEND_TYPE_STREAM_CAPTURE);
}

static void
ipu6_isys_buf_to_fw_frame_buf_pin(struct vb2_buffer *vb,
				  struct ipu6_fw_isys_frame_buff_set_abi *set)
{
	struct ipu6_isys_queue *aq = vb2_queue_to_isys_queue(vb->vb2_queue);
	struct vb2_v4l2_buffer *vvb = to_vb2_v4l2_buffer(vb);
	struct ipu6_isys_video_buffer *ivb =
		vb2_buffer_to_ipu6_isys_video_buffer(vvb);

	set->output_pins[aq->fw_output].addr = ivb->dma_addr;
	set->output_pins[aq->fw_output].out_buf_id = vb->index + 1;
}

/*
 * Convert a buffer list to a isys fw ABI framebuffer set. The
 * buffer list is not modified.
 */
#define IPU6_ISYS_FRAME_NUM_THRESHOLD  (30)
static void ipu6_fw_isys_prepare_buf_set(struct isys_fw_msgs *msg,
					 struct ipu6_isys_stream *stream,
					 struct ipu6_isys_buffer_list *bl)
{
	struct ipu6_fw_isys_frame_buff_set_abi *set = &msg->ipu6.frame;
	struct ipu6_isys_buffer *ib;

	WARN_ON(!bl->nbufs);

	set->send_irq_sof = 1;
	set->send_resp_sof = 1;
	set->send_irq_eof = 0;
	set->send_resp_eof = 0;

	set->send_irq_capture_ack = 1;
	set->send_irq_capture_done = 0;

	set->send_resp_capture_ack = 1;
	set->send_resp_capture_done = 1;
	if (atomic_read(&stream->sequence) >= IPU6_ISYS_FRAME_NUM_THRESHOLD) {
		set->send_resp_capture_ack = 0;
		set->send_resp_capture_done = 0;
	}

	list_for_each_entry(ib, &bl->head, head) {
		struct vb2_buffer *vb = ipu6_isys_buffer_to_vb2_buffer(ib);

		ipu6_isys_buf_to_fw_frame_buf_pin(vb, set);
	}
}

const struct ipu6_fw_isys_ops ipu6_fw_isys_ops = {
	.init = ipu6_fw_isys_init,
	.close = ipu6_fw_isys_close,
	.cleanup = ipu6_fw_isys_cleanup,
	.prepare_stream_cfg = ipu6_fw_isys_prepare_stream_cfg,
	.prepare_buf_set = ipu6_fw_isys_prepare_buf_set,
	.stream_open = ipu6_fw_isys_stream_open,
	.stream_start = ipu6_fw_isys_stream_start,
	.stream_capture = ipu6_fw_isys_stream_capture,
	.stream_flush = ipu6_fw_isys_stream_flush,
	.stream_close = ipu6_fw_isys_stream_close,
	.dump_stream_cfg = ipu6_fw_isys_dump_stream_cfg,
	.dump_frame_buf_set = ipu6_fw_isys_dump_frame_buf_set,
}