Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 08900a71 authored by Suresh Vankadara's avatar Suresh Vankadara Committed by Gerrit - the friendly Code Review server
Browse files

msm: camera: ope: Add support to OPE driver



OPE is camera offline engine, support is added
to enable camera OPE hardware.

CRs-Fixed: 2520602
Change-Id: I8b08ecb34323ee927f2be88707ad65ad2444447d
Signed-off-by: default avatarSuresh Vankadara <svankada@codeaurora.org>
Signed-off-by: default avatarRavikishore Pampana <rpampana@codeaurora.org>
parent cae1cb38
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12,3 +12,4 @@ obj-$(CONFIG_SPECTRA_CAMERA) += cam_jpeg/
obj-$(CONFIG_SPECTRA_CAMERA) += cam_fd/
obj-$(CONFIG_SPECTRA_CAMERA) += cam_lrme/
obj-$(CONFIG_SPECTRA_CAMERA) += cam_cust/
obj-$(CONFIG_SPECTRA_CAMERA) += cam_ope/
+15 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: GPL-2.0-only

ccflags-y += -I$(srctree)/techpack/camera/drivers/cam_utils
ccflags-y += -I$(srctree)/techpack/camera/drivers/cam_req_mgr
ccflags-y += -I$(srctree)/techpack/camera/drivers/cam_core
ccflags-y += -I$(srctree)/techpack/camera/drivers/cam_sync
ccflags-y += -I$(srctree)/techpack/camera/drivers/cam_ope
ccflags-y += -I$(srctree)/techpack/camera/drivers/cam_ope/ope_hw_mgr
ccflags-y += -I$(srctree)/techpack/camera/drivers
ccflags-y += -I$(srctree)/techpack/camera/drivers/cam_cpas/include
ccflags-y += -I$(srctree)/techpack/camera/drivers/cam_smmu/

obj-$(CONFIG_SPECTRA_CAMERA) += ope_hw_mgr/
obj-$(CONFIG_SPECTRA_CAMERA) += cam_ope_subdev.o cam_ope_context.o
+273 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2019, The Linux Foundation. All rights reserved.
 */

#include <linux/debugfs.h>
#include <linux/videodev2.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <media/cam_sync.h>
#include <media/cam_defs.h>
#include <media/cam_ope.h>
#include "cam_sync_api.h"
#include "cam_node.h"
#include "cam_context.h"
#include "cam_context_utils.h"
#include "cam_ope_context.h"
#include "cam_req_mgr_util.h"
#include "cam_mem_mgr.h"
#include "cam_trace.h"
#include "cam_debug_util.h"
#include "cam_packet_util.h"

static const char ope_dev_name[] = "cam-ope";

static int cam_ope_context_dump_active_request(void *data, unsigned long iova,
	uint32_t buf_info)
{
	struct cam_context *ctx = (struct cam_context *)data;
	struct cam_ctx_request          *req = NULL;
	struct cam_ctx_request          *req_temp = NULL;
	struct cam_hw_mgr_dump_pf_data  *pf_dbg_entry = NULL;
	int rc = 0;
	bool b_mem_found = false;

	if (!ctx) {
		CAM_ERR(CAM_OPE, "Invalid ctx");
		return -EINVAL;
	}

	mutex_lock(&ctx->ctx_mutex);
	if (ctx->state < CAM_CTX_ACQUIRED || ctx->state > CAM_CTX_ACTIVATED) {
		CAM_ERR(CAM_ICP, "Invalid state icp ctx %d state %d",
			ctx->ctx_id, ctx->state);
		goto end;
	}

	CAM_INFO(CAM_OPE, "iommu fault for ope ctx %d state %d",
		ctx->ctx_id, ctx->state);

	list_for_each_entry_safe(req, req_temp,
			&ctx->active_req_list, list) {
		pf_dbg_entry = &(req->pf_data);
		CAM_INFO(CAM_OPE, "req_id : %lld", req->request_id);

		rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
			iova, buf_info, &b_mem_found);
		if (rc)
			CAM_ERR(CAM_OPE, "Failed to dump pf info");

		if (b_mem_found)
			CAM_ERR(CAM_OPE, "Found page fault in req %lld %d",
				req->request_id, rc);
	}

end:
	mutex_unlock(&ctx->ctx_mutex);
	return rc;
}

static int __cam_ope_acquire_dev_in_available(struct cam_context *ctx,
	struct cam_acquire_dev_cmd *cmd)
{
	int rc;

	rc = cam_context_acquire_dev_to_hw(ctx, cmd);
	if (!rc) {
		ctx->state = CAM_CTX_ACQUIRED;
		trace_cam_context_state("OPE", ctx);
	}

	return rc;
}

static int __cam_ope_release_dev_in_acquired(struct cam_context *ctx,
	struct cam_release_dev_cmd *cmd)
{
	int rc;

	rc = cam_context_release_dev_to_hw(ctx, cmd);
	if (rc)
		CAM_ERR(CAM_OPE, "Unable to release device");

	ctx->state = CAM_CTX_AVAILABLE;
	trace_cam_context_state("OPE", ctx);
	return rc;
}

static int __cam_ope_start_dev_in_acquired(struct cam_context *ctx,
	struct cam_start_stop_dev_cmd *cmd)
{
	int rc;

	rc = cam_context_start_dev_to_hw(ctx, cmd);
	if (!rc) {
		ctx->state = CAM_CTX_READY;
		trace_cam_context_state("OPE", ctx);
	}

	return rc;
}

static int __cam_ope_flush_dev_in_ready(struct cam_context *ctx,
	struct cam_flush_dev_cmd *cmd)
{
	int rc;

	rc = cam_context_flush_dev_to_hw(ctx, cmd);
	if (rc)
		CAM_ERR(CAM_OPE, "Failed to flush device");

	return rc;
}

static int __cam_ope_config_dev_in_ready(struct cam_context *ctx,
	struct cam_config_dev_cmd *cmd)
{
	int rc;
	size_t len;
	uintptr_t packet_addr;

	rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
		&packet_addr, &len);
	if (rc) {
		CAM_ERR(CAM_OPE, "[%s][%d] Can not get packet address",
			ctx->dev_name, ctx->ctx_id);
		rc = -EINVAL;
		return rc;
	}

	rc = cam_context_prepare_dev_to_hw(ctx, cmd);

	if (rc)
		CAM_ERR(CAM_OPE, "Failed to prepare device");

	return rc;
}

static int __cam_ope_stop_dev_in_ready(struct cam_context *ctx,
	struct cam_start_stop_dev_cmd *cmd)
{
	int rc;

	rc = cam_context_stop_dev_to_hw(ctx);
	if (rc)
		CAM_ERR(CAM_OPE, "Failed to stop device");

	ctx->state = CAM_CTX_ACQUIRED;
	trace_cam_context_state("OPE", ctx);
	return rc;
}

static int __cam_ope_release_dev_in_ready(struct cam_context *ctx,
	struct cam_release_dev_cmd *cmd)
{
	int rc;

	rc = __cam_ope_stop_dev_in_ready(ctx, NULL);
	if (rc)
		CAM_ERR(CAM_OPE, "Failed to stop device");

	rc = __cam_ope_release_dev_in_acquired(ctx, cmd);
	if (rc)
		CAM_ERR(CAM_OPE, "Failed to release device");

	return rc;
}

static int __cam_ope_handle_buf_done_in_ready(void *ctx,
	uint32_t evt_id, void *done)
{
	return cam_context_buf_done_from_hw(ctx, done, evt_id);
}

static struct cam_ctx_ops
	cam_ope_ctx_state_machine[CAM_CTX_STATE_MAX] = {
	/* Uninit */
	{
		.ioctl_ops = {},
		.crm_ops = {},
		.irq_ops = NULL,
	},
	/* Available */
	{
		.ioctl_ops = {
			.acquire_dev = __cam_ope_acquire_dev_in_available,
		},
		.crm_ops = {},
		.irq_ops = NULL,
	},
	/* Acquired */
	{
		.ioctl_ops = {
			.release_dev = __cam_ope_release_dev_in_acquired,
			.start_dev = __cam_ope_start_dev_in_acquired,
			.config_dev = __cam_ope_config_dev_in_ready,
			.flush_dev = __cam_ope_flush_dev_in_ready,
		},
		.crm_ops = {},
		.irq_ops = __cam_ope_handle_buf_done_in_ready,
		.pagefault_ops = cam_ope_context_dump_active_request,
	},
	/* Ready */
	{
		.ioctl_ops = {
			.stop_dev = __cam_ope_stop_dev_in_ready,
			.release_dev = __cam_ope_release_dev_in_ready,
			.config_dev = __cam_ope_config_dev_in_ready,
			.flush_dev = __cam_ope_flush_dev_in_ready,
		},
		.crm_ops = {},
		.irq_ops = __cam_ope_handle_buf_done_in_ready,
		.pagefault_ops = cam_ope_context_dump_active_request,
	},
	/* Activated */
	{
		.ioctl_ops = {},
		.crm_ops = {},
		.irq_ops = NULL,
		.pagefault_ops = cam_ope_context_dump_active_request,
	},
};

int cam_ope_context_init(struct cam_ope_context *ctx,
	struct cam_hw_mgr_intf *hw_intf, uint32_t ctx_id)
{
	int rc;

	if ((!ctx) || (!ctx->base) || (!hw_intf)) {
		CAM_ERR(CAM_OPE, "Invalid params: %pK %pK", ctx, hw_intf);
		rc = -EINVAL;
		goto err;
	}

	rc = cam_context_init(ctx->base, ope_dev_name, CAM_OPE, ctx_id,
		NULL, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
	if (rc) {
		CAM_ERR(CAM_OPE, "Camera Context Base init failed");
		goto err;
	}

	ctx->base->state_machine = cam_ope_ctx_state_machine;
	ctx->base->ctx_priv = ctx;
	ctx->ctxt_to_hw_map = NULL;

err:
	return rc;
}

int cam_ope_context_deinit(struct cam_ope_context *ctx)
{
	if ((!ctx) || (!ctx->base)) {
		CAM_ERR(CAM_OPE, "Invalid params: %pK", ctx);
		return -EINVAL;
	}

	cam_context_deinit(ctx->base);
	memset(ctx, 0, sizeof(*ctx));

	return 0;
}

+44 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright (c) 2019, The Linux Foundation. All rights reserved.
 */

#ifndef _CAM_OPE_CONTEXT_H_
#define _CAM_OPE_CONTEXT_H_

#include "cam_context.h"

#define OPE_CTX_MAX 32

/**
 * struct cam_ope_context - ope context
 * @base:           ope context object
 * @state_machine:  state machine for OPE context
 * @req_base:       common request structure
 * @state:          ope context state
 * @ctxt_to_hw_map: context to FW handle mapping
 */
struct cam_ope_context {
	struct cam_context *base;
	struct cam_ctx_ops *state_machine;
	struct cam_ctx_request req_base[CAM_CTX_REQ_MAX];
	uint32_t state;
	void *ctxt_to_hw_map;
};

/**
 * cam_ope_context_init() - OPE context init
 * @ctx:     Pointer to context
 * @hw_intf: Pointer to OPE hardware interface
 * @ctx_id:  ID for this context
 */
int cam_ope_context_init(struct cam_ope_context *ctx,
	struct cam_hw_mgr_intf *hw_intf, uint32_t ctx_id);

/**
 * cam_ope_context_deinit() - OPE context deinit
 * @ctx: Pointer to context
 */
int cam_ope_context_deinit(struct cam_ope_context *ctx);

#endif /* _CAM_OPE_CONTEXT_H_ */
+278 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2019, The Linux Foundation. All rights reserved.
 */

#include <linux/delay.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/iommu.h>
#include <linux/timer.h>
#include <linux/platform_device.h>
#include <linux/videodev2.h>
#include <media/v4l2-fh.h>
#include <media/v4l2-device.h>
#include <media/v4l2-event.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-subdev.h>
#include <media/cam_req_mgr.h>
#include <media/cam_defs.h>
#include <media/cam_ope.h>
#include "cam_req_mgr_dev.h"
#include "cam_subdev.h"
#include "cam_node.h"
#include "cam_context.h"
#include "cam_ope_context.h"
#include "cam_ope_hw_mgr_intf.h"
#include "cam_hw_mgr_intf.h"
#include "cam_debug_util.h"
#include "cam_smmu_api.h"

#define OPE_DEV_NAME        "cam-ope"

struct cam_ope_subdev {
	struct cam_subdev sd;
	struct cam_node *node;
	struct cam_context ctx[OPE_CTX_MAX];
	struct cam_ope_context ctx_ope[OPE_CTX_MAX];
	struct mutex ope_lock;
	int32_t open_cnt;
	int32_t reserved;
};

static struct cam_ope_subdev g_ope_dev;

static void cam_ope_dev_iommu_fault_handler(
	struct iommu_domain *domain, struct device *dev, unsigned long iova,
	int flags, void *token, uint32_t buf_info)
{
	int i = 0;
	struct cam_node *node = NULL;

	if (!token) {
		CAM_ERR(CAM_OPE, "invalid token in page handler cb");
		return;
	}

	node = (struct cam_node *)token;

	for (i = 0; i < node->ctx_size; i++)
		cam_context_dump_pf_info(&(node->ctx_list[i]), iova,
			buf_info);
}

static int cam_ope_subdev_open(struct v4l2_subdev *sd,
	struct v4l2_subdev_fh *fh)
{
	struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
	struct cam_node *node = v4l2_get_subdevdata(sd);
	int rc = 0;

	mutex_lock(&g_ope_dev.ope_lock);
	if (g_ope_dev.open_cnt >= 1) {
		CAM_ERR(CAM_OPE, "OPE subdev is already opened");
		rc = -EALREADY;
		goto end;
	}

	if (!node) {
		CAM_ERR(CAM_OPE, "Invalid args");
		rc = -EINVAL;
		goto end;
	}

	hw_mgr_intf = &node->hw_mgr_intf;
	rc = hw_mgr_intf->hw_open(hw_mgr_intf->hw_mgr_priv, NULL);
	if (rc < 0) {
		CAM_ERR(CAM_OPE, "OPE HW open failed: %d", rc);
		goto end;
	}
	g_ope_dev.open_cnt++;
	CAM_DBG(CAM_OPE, "OPE HW open success: %d", rc);
end:
	mutex_unlock(&g_ope_dev.ope_lock);
	return rc;
}

static int cam_ope_subdev_close(struct v4l2_subdev *sd,
	struct v4l2_subdev_fh *fh)
{
	int rc = 0;
	struct cam_hw_mgr_intf *hw_mgr_intf = NULL;
	struct cam_node *node = v4l2_get_subdevdata(sd);

	mutex_lock(&g_ope_dev.ope_lock);
	if (g_ope_dev.open_cnt <= 0) {
		CAM_DBG(CAM_OPE, "OPE subdev is already closed");
		rc = -EINVAL;
		goto end;
	}
	g_ope_dev.open_cnt--;
	if (!node) {
		CAM_ERR(CAM_OPE, "Invalid args");
		rc = -EINVAL;
		goto end;
	}

	hw_mgr_intf = &node->hw_mgr_intf;
	if (!hw_mgr_intf) {
		CAM_ERR(CAM_OPE, "hw_mgr_intf is not initialized");
		rc = -EINVAL;
		goto end;
	}

	rc = cam_node_shutdown(node);
	if (rc < 0) {
		CAM_ERR(CAM_OPE, "HW close failed");
		goto end;
	}
	CAM_DBG(CAM_OPE, "OPE HW close success: %d", rc);

end:
	mutex_unlock(&g_ope_dev.ope_lock);
	return rc;
}

const struct v4l2_subdev_internal_ops cam_ope_subdev_internal_ops = {
	.open = cam_ope_subdev_open,
	.close = cam_ope_subdev_close,
};

static int cam_ope_subdev_probe(struct platform_device *pdev)
{
	int rc = 0, i = 0;
	struct cam_node *node;
	struct cam_hw_mgr_intf *hw_mgr_intf;
	int iommu_hdl = -1;

	CAM_DBG(CAM_OPE, "OPE subdev probe start");
	if (!pdev) {
		CAM_ERR(CAM_OPE, "pdev is NULL");
		return -EINVAL;
	}

	g_ope_dev.sd.pdev = pdev;
	g_ope_dev.sd.internal_ops = &cam_ope_subdev_internal_ops;
	rc = cam_subdev_probe(&g_ope_dev.sd, pdev, OPE_DEV_NAME,
		CAM_OPE_DEVICE_TYPE);
	if (rc) {
		CAM_ERR(CAM_OPE, "OPE cam_subdev_probe failed:%d", rc);
		return rc;
	}

	node = (struct cam_node *) g_ope_dev.sd.token;

	hw_mgr_intf = kzalloc(sizeof(*hw_mgr_intf), GFP_KERNEL);
	if (!hw_mgr_intf) {
		rc = -EINVAL;
		goto hw_alloc_fail;
	}

	rc = cam_ope_hw_mgr_init(pdev->dev.of_node, (uint64_t *)hw_mgr_intf,
		&iommu_hdl);
	if (rc) {
		CAM_ERR(CAM_OPE, "OPE HW manager init failed: %d", rc);
		goto hw_init_fail;
	}

	for (i = 0; i < OPE_CTX_MAX; i++) {
		g_ope_dev.ctx_ope[i].base = &g_ope_dev.ctx[i];
		rc = cam_ope_context_init(&g_ope_dev.ctx_ope[i],
			hw_mgr_intf, i);
		if (rc) {
			CAM_ERR(CAM_OPE, "OPE context init failed");
			goto ctx_fail;
		}
	}

	rc = cam_node_init(node, hw_mgr_intf, g_ope_dev.ctx,
		OPE_CTX_MAX, OPE_DEV_NAME);
	if (rc) {
		CAM_ERR(CAM_OPE, "OPE node init failed");
		goto ctx_fail;
	}

	cam_smmu_set_client_page_fault_handler(iommu_hdl,
		cam_ope_dev_iommu_fault_handler, node);

	g_ope_dev.open_cnt = 0;
	mutex_init(&g_ope_dev.ope_lock);

	CAM_DBG(CAM_OPE, "OPE subdev probe complete");

	return rc;

ctx_fail:
	for (--i; i >= 0; i--)
		cam_ope_context_deinit(&g_ope_dev.ctx_ope[i]);
hw_init_fail:
	kfree(hw_mgr_intf);
hw_alloc_fail:
	cam_subdev_remove(&g_ope_dev.sd);
	return rc;
}

static int cam_ope_subdev_remove(struct platform_device *pdev)
{
	int i;
	struct v4l2_subdev *sd;
	struct cam_subdev *subdev;

	if (!pdev) {
		CAM_ERR(CAM_OPE, "pdev is NULL");
		return -ENODEV;
	}

	sd = platform_get_drvdata(pdev);
	if (!sd) {
		CAM_ERR(CAM_OPE, "V4l2 subdev is NULL");
		return -ENODEV;
	}

	subdev = v4l2_get_subdevdata(sd);
	if (!subdev) {
		CAM_ERR(CAM_OPE, "cam subdev is NULL");
		return -ENODEV;
	}

	for (i = 0; i < OPE_CTX_MAX; i++)
		cam_ope_context_deinit(&g_ope_dev.ctx_ope[i]);
	cam_node_deinit(g_ope_dev.node);
	cam_subdev_remove(&g_ope_dev.sd);
	mutex_destroy(&g_ope_dev.ope_lock);

	return 0;
}

static const struct of_device_id cam_ope_dt_match[] = {
	{.compatible = "qcom,cam-ope"},
	{}
};


static struct platform_driver cam_ope_driver = {
	.probe = cam_ope_subdev_probe,
	.remove = cam_ope_subdev_remove,
	.driver = {
		.name = "cam_ope",
		.of_match_table = cam_ope_dt_match,
		.suppress_bind_attrs = true,
	},
};

static int __init cam_ope_init_module(void)
{
	return platform_driver_register(&cam_ope_driver);
}

static void __exit cam_ope_exit_module(void)
{
	platform_driver_unregister(&cam_ope_driver);
}
module_init(cam_ope_init_module);
module_exit(cam_ope_exit_module);
MODULE_DESCRIPTION("MSM OPE driver");
MODULE_LICENSE("GPL v2");
Loading