/* SPDX-License-Identifier: MIT */
/*
 * Copyright(c) 2024, Intel Corporation. All rights reserved.
 */

#ifndef _XE_PXP_TYPES_H_
#define _XE_PXP_TYPES_H_

#include <linux/completion.h>
#include <linux/iosys-map.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/types.h>
#include <linux/workqueue.h>

struct xe_bo;
struct xe_exec_queue;
struct xe_device;
struct xe_gt;
struct xe_vm;

enum xe_pxp_status {
	XE_PXP_ERROR = -1,
	XE_PXP_NEEDS_TERMINATION = 0, /* starting status */
	XE_PXP_NEEDS_ADDITIONAL_TERMINATION,
	XE_PXP_TERMINATION_IN_PROGRESS,
	XE_PXP_READY_TO_START,
	XE_PXP_START_IN_PROGRESS,
	XE_PXP_ACTIVE,
	XE_PXP_SUSPENDED,
};

/**
 * struct xe_pxp_gsc_client_resources - resources for GSC submission by a PXP
 * client. The GSC FW supports multiple GSC client active at the same time.
 */
struct xe_pxp_gsc_client_resources {
	/**
	 * @host_session_handle: handle used to identify the client in messages
	 * sent to the GSC firmware.
	 */
	u64 host_session_handle;
	/** @vm: VM used for PXP submissions to the GSCCS */
	struct xe_vm *vm;
	/** @q: GSCCS exec queue for PXP submissions */
	struct xe_exec_queue *q;

	/**
	 * @bo: BO used for submissions to the GSCCS and GSC FW. It includes
	 * space for the GSCCS batch and the input/output buffers read/written
	 * by the FW
	 */
	struct xe_bo *bo;
	/** @inout_size: size of each of the msg_in/out sections individually */
	u32 inout_size;
	/** @batch: iosys_map to the batch memory within the BO */
	struct iosys_map batch;
	/** @msg_in: iosys_map to the input memory within the BO */
	struct iosys_map msg_in;
	/** @msg_out: iosys_map to the output memory within the BO */
	struct iosys_map msg_out;
};

/**
 * struct xe_pxp - pxp state
 */
struct xe_pxp {
	/** @xe: Backpoiner to the xe_device struct */
	struct xe_device *xe;

	/**
	 * @gt: pointer to the gt that owns the submission-side of PXP
	 * (VDBOX, KCR and GSC)
	 */
	struct xe_gt *gt;

	/** @vcs_exec: kernel-owned objects for PXP submissions to the VCS */
	struct {
		/** @vcs_exec.q: kernel-owned VCS exec queue used for PXP terminations */
		struct xe_exec_queue *q;
		/** @vcs_exec.bo: BO used for submissions to the VCS */
		struct xe_bo *bo;
	} vcs_exec;

	/** @gsc_res: kernel-owned objects for PXP submissions to the GSCCS */
	struct xe_pxp_gsc_client_resources gsc_res;

	/** @events: wrapper for the worker and queue used for PXP event handling */
	struct {
		/** @events.work: worker that manages termination events. */
		struct work_struct work;
		/** @events.wq: workqueue on which to queue the work. */
		struct workqueue_struct *wq;
		/** @events.pending: pending events */
		atomic_t pending;
#define PXP_TERMINATION_REQUEST_IRQ		BIT(0)
#define PXP_TERMINATION_REQUEST_ON_RESUME	BIT(1)
#define PXP_TERMINATION_REQUEST	(PXP_TERMINATION_REQUEST_IRQ | \
				 PXP_TERMINATION_REQUEST_ON_RESUME)
#define PXP_TERMINATION_COMPLETE_IRQ		BIT(2)
	} events;

	/** @mutex: protects the pxp status and the queue list */
	struct mutex mutex;
	/** @status: the current pxp status */
	enum xe_pxp_status status;
	/** @activation: completion struct that tracks pxp start */
	struct completion activation;
	/** @termination: completion struct that tracks terminations */
	struct completion termination;

	/** @queues: management of exec_queues that use PXP */
	struct {
		/** @queues.lock: spinlock protecting the queue management */
		spinlock_t lock;
		/** @queues.list: list of exec_queues that use PXP */
		struct list_head list;
	} queues;

	/**
	 * @key_instance: keep track of the current iteration of the PXP key.
	 * Note that, due to the time needed for PXP termination and re-start
	 * to complete, the minimum time between 2 subsequent increases of this
	 * variable is 50ms, and even that only if there is a continuous attack;
	 * normal behavior is for this to increase much much slower than that.
	 * This means that we don't expect this to ever wrap and don't implement
	 * that case in the code.
	 */
	u32 key_instance;
	/**
	 * @last_suspend_key_instance: value of key_instance at the last
	 * suspend. Used to check if any PXP session has been created between
	 * suspend cycles.
	 */
	u32 last_suspend_key_instance;
	/**
	 * @needs_termination_on_resume: indicates if PXP termination is needed
	 * on resume. This is set if PXP was active when we suspend and it is
	 * cleared when we queue the termination on resume. Since the suspend
	 * and resume calls cannot execute at the same time, this variable does
	 * not need to be protected by the PXP lock.
	 */
	bool needs_termination_on_resume;
};

#endif /* _XE_PXP_TYPES_H_ */