18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015-2018 Etnaviv Project
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef __ETNAVIV_GEM_H__
78c2ecf20Sopenharmony_ci#define __ETNAVIV_GEM_H__
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/dma-resv.h>
108c2ecf20Sopenharmony_ci#include "etnaviv_cmdbuf.h"
118c2ecf20Sopenharmony_ci#include "etnaviv_drv.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistruct dma_fence;
148c2ecf20Sopenharmony_cistruct etnaviv_gem_ops;
158c2ecf20Sopenharmony_cistruct etnaviv_gem_object;
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistruct etnaviv_gem_userptr {
188c2ecf20Sopenharmony_ci	uintptr_t ptr;
198c2ecf20Sopenharmony_ci	struct mm_struct *mm;
208c2ecf20Sopenharmony_ci	bool ro;
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct etnaviv_vram_mapping {
248c2ecf20Sopenharmony_ci	struct list_head obj_node;
258c2ecf20Sopenharmony_ci	struct list_head scan_node;
268c2ecf20Sopenharmony_ci	struct list_head mmu_node;
278c2ecf20Sopenharmony_ci	struct etnaviv_gem_object *object;
288c2ecf20Sopenharmony_ci	struct etnaviv_iommu_context *context;
298c2ecf20Sopenharmony_ci	struct drm_mm_node vram_node;
308c2ecf20Sopenharmony_ci	unsigned int use;
318c2ecf20Sopenharmony_ci	u32 iova;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct etnaviv_gem_object {
358c2ecf20Sopenharmony_ci	struct drm_gem_object base;
368c2ecf20Sopenharmony_ci	const struct etnaviv_gem_ops *ops;
378c2ecf20Sopenharmony_ci	struct mutex lock;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	u32 flags;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	struct list_head gem_node;
428c2ecf20Sopenharmony_ci	struct etnaviv_gpu *gpu;     /* non-null if active */
438c2ecf20Sopenharmony_ci	atomic_t gpu_active;
448c2ecf20Sopenharmony_ci	u32 access;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	struct page **pages;
478c2ecf20Sopenharmony_ci	struct sg_table *sgt;
488c2ecf20Sopenharmony_ci	void *vaddr;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	struct list_head vram_list;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	/* cache maintenance */
538c2ecf20Sopenharmony_ci	u32 last_cpu_prep_op;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	struct etnaviv_gem_userptr userptr;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic inline
598c2ecf20Sopenharmony_cistruct etnaviv_gem_object *to_etnaviv_bo(struct drm_gem_object *obj)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	return container_of(obj, struct etnaviv_gem_object, base);
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct etnaviv_gem_ops {
658c2ecf20Sopenharmony_ci	int (*get_pages)(struct etnaviv_gem_object *);
668c2ecf20Sopenharmony_ci	void (*release)(struct etnaviv_gem_object *);
678c2ecf20Sopenharmony_ci	void *(*vmap)(struct etnaviv_gem_object *);
688c2ecf20Sopenharmony_ci	int (*mmap)(struct etnaviv_gem_object *, struct vm_area_struct *);
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic inline bool is_active(struct etnaviv_gem_object *etnaviv_obj)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	return atomic_read(&etnaviv_obj->gpu_active) != 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define MAX_CMDS 4
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistruct etnaviv_gem_submit_bo {
798c2ecf20Sopenharmony_ci	u32 flags;
808c2ecf20Sopenharmony_ci	u64 va;
818c2ecf20Sopenharmony_ci	struct etnaviv_gem_object *obj;
828c2ecf20Sopenharmony_ci	struct etnaviv_vram_mapping *mapping;
838c2ecf20Sopenharmony_ci	struct dma_fence *excl;
848c2ecf20Sopenharmony_ci	unsigned int nr_shared;
858c2ecf20Sopenharmony_ci	struct dma_fence **shared;
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
898c2ecf20Sopenharmony_ci * associated with the cmdstream submission for synchronization (and
908c2ecf20Sopenharmony_ci * make it easier to unwind when things go wrong, etc).
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cistruct etnaviv_gem_submit {
938c2ecf20Sopenharmony_ci	struct drm_sched_job sched_job;
948c2ecf20Sopenharmony_ci	struct kref refcount;
958c2ecf20Sopenharmony_ci	struct etnaviv_file_private *ctx;
968c2ecf20Sopenharmony_ci	struct etnaviv_gpu *gpu;
978c2ecf20Sopenharmony_ci	struct etnaviv_iommu_context *mmu_context, *prev_mmu_context;
988c2ecf20Sopenharmony_ci	struct dma_fence *out_fence, *in_fence;
998c2ecf20Sopenharmony_ci	int out_fence_id;
1008c2ecf20Sopenharmony_ci	struct list_head node; /* GPU active submit list */
1018c2ecf20Sopenharmony_ci	struct etnaviv_cmdbuf cmdbuf;
1028c2ecf20Sopenharmony_ci	bool runtime_resumed;
1038c2ecf20Sopenharmony_ci	u32 exec_state;
1048c2ecf20Sopenharmony_ci	u32 flags;
1058c2ecf20Sopenharmony_ci	unsigned int nr_pmrs;
1068c2ecf20Sopenharmony_ci	struct etnaviv_perfmon_request *pmrs;
1078c2ecf20Sopenharmony_ci	unsigned int nr_bos;
1088c2ecf20Sopenharmony_ci	struct etnaviv_gem_submit_bo bos[];
1098c2ecf20Sopenharmony_ci	/* No new members here, the previous one is variable-length! */
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_civoid etnaviv_submit_put(struct etnaviv_gem_submit * submit);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciint etnaviv_gem_wait_bo(struct etnaviv_gpu *gpu, struct drm_gem_object *obj,
1158c2ecf20Sopenharmony_ci	struct drm_etnaviv_timespec *timeout);
1168c2ecf20Sopenharmony_ciint etnaviv_gem_new_private(struct drm_device *dev, size_t size, u32 flags,
1178c2ecf20Sopenharmony_ci	const struct etnaviv_gem_ops *ops, struct etnaviv_gem_object **res);
1188c2ecf20Sopenharmony_civoid etnaviv_gem_obj_add(struct drm_device *dev, struct drm_gem_object *obj);
1198c2ecf20Sopenharmony_cistruct page **etnaviv_gem_get_pages(struct etnaviv_gem_object *obj);
1208c2ecf20Sopenharmony_civoid etnaviv_gem_put_pages(struct etnaviv_gem_object *obj);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistruct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
1238c2ecf20Sopenharmony_ci	struct drm_gem_object *obj, struct etnaviv_iommu_context *mmu_context,
1248c2ecf20Sopenharmony_ci	u64 va);
1258c2ecf20Sopenharmony_civoid etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping *mapping);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#endif /* __ETNAVIV_GEM_H__ */
128