/* SPDX-License-Identifier: MIT */
/*
 * Copyright © 2022 Intel Corporation
 */

#ifndef _XE_GUC_EXEC_QUEUE_TYPES_H_
#define _XE_GUC_EXEC_QUEUE_TYPES_H_

#include <linux/spinlock.h>
#include <linux/workqueue.h>

#include "xe_gpu_scheduler_types.h"
#include "xe_hw_fence_types.h"

struct dma_fence;
struct xe_exec_queue;

/**
 * struct xe_guc_exec_queue - GuC specific state for an xe_exec_queue
 */
struct xe_guc_exec_queue {
	/** @q: Backpointer to parent xe_exec_queue */
	struct xe_exec_queue *q;
	/** @rcu: For safe freeing of exported dma fences */
	struct rcu_head rcu;
	/** @sched: GPU scheduler for this xe_exec_queue */
	struct xe_gpu_scheduler sched;
	/**
	 * @name: Scheduler timeline name, kept with @sched until RCU free.
	 */
	char name[MAX_FENCE_NAME_LEN];
	/** @entity: Scheduler entity for this xe_exec_queue */
	struct xe_sched_entity entity;
	/**
	 * @static_msgs: Static messages for this xe_exec_queue, used when
	 * a message needs to sent through the GPU scheduler but memory
	 * allocations are not allowed.
	 */
#define MAX_STATIC_MSG_TYPE	4
	struct xe_sched_msg static_msgs[MAX_STATIC_MSG_TYPE];
	/** @destroy_async: do final destroy async from this worker */
	struct work_struct destroy_async;
	/** @resume_time: time of last resume */
	u64 resume_time;
	/** @state: GuC specific state for this xe_exec_queue */
	atomic_t state;
	/** @wqi_head: work queue item tail */
	u32 wqi_head;
	/** @wqi_tail: work queue item tail */
	u32 wqi_tail;
	/** @id: GuC id for this exec_queue */
	u16 id;
	/** @suspend_wait: wait queue used to wait on pending suspends */
	wait_queue_head_t suspend_wait;
	/**
	 * @suspend_pending: a suspend of the exec_queue is pending.
	 * Protected by @sched.msg_lock.
	 */
	bool suspend_pending;
	/**
	 * @suspend_count: Reference count of active suspend requests. The
	 * exec_queue remains suspended while this is non-zero, allowing
	 * multiple concurrent callers to independently hold a suspend without
	 * prematurely re-enabling the queue. Protected by @sched.msg_lock.
	 */
	int suspend_count;
	/**
	 * @needs_cleanup: Needs a cleanup message during VF post migration
	 * recovery.
	 */
	bool needs_cleanup;
	/**
	 * @needs_suspend: Needs a suspend message during VF post migration
	 * recovery.
	 */
	bool needs_suspend;
	/**
	 * @needs_resume: Needs a resume message during VF post migration
	 * recovery.
	 */
	bool needs_resume;
	/** @multi_queue: multi-queue group CGP state for VF post migration recovery */
	struct {
		/**
		 * @multi_queue.needs_cgp_sync: Needs a CGP_SYNC (dynamic CGP
		 * update) message replayed during recovery.
		 */
		u8 needs_cgp_sync:1;
		/**
		 * @multi_queue.re_register: A registration-time CGP update was
		 * interrupted by recovery; the queue must be re-registered.
		 */
		u8 re_register:1;
		/**
		 * @multi_queue.re_update: A dynamic CGP update was interrupted
		 * by recovery; the CGP update must be replayed.
		 */
		u8 re_update:1;
		/**
		 * @multi_queue.registering_cgp: This queue's currently
		 * outstanding CGP_SYNC is a registration (matched against
		 * group->cgp_update_q in revert).
		 */
		u8 registering_cgp:1;
		/**
		 * @multi_queue.updating_cgp: This queue's currently outstanding
		 * CGP_SYNC is a dynamic update (matched against
		 * group->cgp_update_q in revert).
		 */
		u8 updating_cgp:1;
	} multi_queue;
};

#endif