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

#ifndef _XE_BO_TYPES_H_
#define _XE_BO_TYPES_H_

#include <linux/iosys-map.h>

#include <drm/drm_gpusvm.h>
#include <drm/drm_pagemap.h>
#include <drm/ttm/ttm_bo.h>
#include <drm/ttm/ttm_device.h>
#include <drm/ttm/ttm_placement.h>

#include "xe_device_types.h"
#include "xe_ggtt_types.h"

struct xe_device;
struct xe_mem_pool_node;
struct xe_vm;
struct xe_exec_queue;

#define XE_BO_MAX_PLACEMENTS	3

/* TODO: To be selected with VM_MADVISE */
#define	XE_BO_PRIORITY_NORMAL	1

/**
 * struct xe_bo - Xe buffer object
 */
struct xe_bo {
	/** @ttm: TTM base buffer object */
	struct ttm_buffer_object ttm;
	/** @backup_obj: The backup object when pinned and suspended (vram only) */
	struct xe_bo *backup_obj;
	/** @parent_obj: Ref to parent bo if this a backup_obj */
	struct xe_bo *parent_obj;
	/** @dma_buf: Imported dma-buf ref to keep its resv alive. */
	struct dma_buf *dma_buf;
	/** @flags: flags for this buffer object */
	u32 flags;
	/** @vm: VM this BO is attached to, for extobj this will be NULL */
	struct xe_vm *vm;
	/**
	 * @q: Queue this BO is attached to, mostly for LRC BO, NULL otherwise.
	 * Protected by the BO dma_resv: readers must hold it across both the
	 * read and xe_exec_queue_get_unless_zero(). The BO holds no reference
	 * on the queue.
	 */
	struct xe_exec_queue *q;
	/** @tile: Tile this BO is attached to (kernel BO only) */
	struct xe_tile *tile;
	/** @placements: valid placements for this BO */
	struct ttm_place placements[XE_BO_MAX_PLACEMENTS];
	/** @placement: current placement for this BO */
	struct ttm_placement placement;
	/** @ggtt_node: Array of GGTT nodes if this BO is mapped in the GGTTs */
	struct xe_ggtt_node *ggtt_node[XE_MAX_TILES_PER_DEVICE];
	/** @vmap: iosys map of this buffer */
	struct iosys_map vmap;
	/** @kmap: TTM bo kmap object for internal use only. Keep off. */
	struct ttm_bo_kmap_obj kmap;
	/** @pinned_link: link to present / evicted list of pinned BO */
	struct list_head pinned_link;
#ifdef CONFIG_PROC_FS
	/**
	 * @client: @xe_drm_client which created the bo
	 */
	struct xe_drm_client *client;
	/**
	 * @client_link: Link into @xe_drm_client.objects_list
	 */
	struct list_head client_link;
#endif
	/** @attr: User controlled attributes for bo */
	struct {
		/**
		 * @attr.atomic_access: type of atomic access bo needs
		 * protected by bo dma-resv lock
		 */
		u32 atomic_access;
	} attr;
	/**
	 * @pxp_key_instance: PXP key instance this BO was created against. A
	 * 0 in this variable indicates that the BO does not use PXP encryption.
	 */
	u32 pxp_key_instance;

	/** @freed: List node for delayed put. */
	struct llist_node freed;
	/** @created: Whether the bo has passed initial creation */
	bool created;

	/** @ccs_cleared: true means that CCS region of BO is already cleared */
	bool ccs_cleared;

	/** @bb_ccs: BB instructions of CCS read/write. Valid only for VF */
	struct xe_mem_pool_node *bb_ccs[XE_SRIOV_VF_CCS_CTX_COUNT];

	/**
	 * @cpu_caching: CPU caching mode. Currently only used for userspace
	 * objects. Exceptions are system memory on DGFX, which is always
	 * WB.
	 */
	u16 cpu_caching;

	/** @devmem_allocation: SVM device memory allocation */
	struct drm_pagemap_devmem devmem_allocation;

	/** @vram_userfault_link: Link into @mem_access.vram_userfault.list */
	struct list_head vram_userfault_link;

	/**
	 * @min_align: minimum alignment needed for this BO if different
	 * from default
	 */
	u64 min_align;

	/**
	 * @purgeable: Purgeability state and accounting.
	 *
	 * All fields are protected by the BO's dma-resv lock.
	 */
	struct {
		/**
		 * @purgeable.state: BO purgeability state
		 *                   (WILLNEED/DONTNEED/PURGED).
		 */
		u32 state;

		/**
		 * @purgeable.vma_count: Number of VMAs currently mapping this BO.
		 */
		u32 vma_count;

		/**
		 * @purgeable.willneed_count: Number of active WILLNEED holders.
		 *
		 * Counts WILLNEED VMAs plus active dma-buf exports for
		 * non-imported BOs. The BO flips to DONTNEED on a 1->0
		 * transition only when VMAs still exist; if the last VMA is
		 * removed, the previous BO state is preserved.
		 */
		u32 willneed_count;
	} purgeable;
};

#endif