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_DRV_H__
78c2ecf20Sopenharmony_ci#define __ETNAVIV_DRV_H__
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/list.h>
108c2ecf20Sopenharmony_ci#include <linux/mm_types.h>
118c2ecf20Sopenharmony_ci#include <linux/sizes.h>
128c2ecf20Sopenharmony_ci#include <linux/time64.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <drm/drm_fb_helper.h>
168c2ecf20Sopenharmony_ci#include <drm/drm_gem.h>
178c2ecf20Sopenharmony_ci#include <drm/etnaviv_drm.h>
188c2ecf20Sopenharmony_ci#include <drm/gpu_scheduler.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistruct etnaviv_cmdbuf;
218c2ecf20Sopenharmony_cistruct etnaviv_gpu;
228c2ecf20Sopenharmony_cistruct etnaviv_mmu;
238c2ecf20Sopenharmony_cistruct etnaviv_gem_object;
248c2ecf20Sopenharmony_cistruct etnaviv_gem_submit;
258c2ecf20Sopenharmony_cistruct etnaviv_iommu_global;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define ETNAVIV_SOFTPIN_START_ADDRESS	SZ_4M /* must be >= SUBALLOC_SIZE */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct etnaviv_file_private {
308c2ecf20Sopenharmony_ci	struct etnaviv_iommu_context	*mmu;
318c2ecf20Sopenharmony_ci	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistruct etnaviv_drm_private {
358c2ecf20Sopenharmony_ci	int num_gpus;
368c2ecf20Sopenharmony_ci	struct device_dma_parameters dma_parms;
378c2ecf20Sopenharmony_ci	struct etnaviv_gpu *gpu[ETNA_MAX_PIPES];
388c2ecf20Sopenharmony_ci	gfp_t shm_gfp_mask;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	struct etnaviv_cmdbuf_suballoc *cmdbuf_suballoc;
418c2ecf20Sopenharmony_ci	struct etnaviv_iommu_global *mmu_global;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/* list of GEM objects: */
448c2ecf20Sopenharmony_ci	struct mutex gem_lock;
458c2ecf20Sopenharmony_ci	struct list_head gem_list;
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciint etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
498c2ecf20Sopenharmony_ci		struct drm_file *file);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciint etnaviv_gem_mmap(struct file *filp, struct vm_area_struct *vma);
528c2ecf20Sopenharmony_civm_fault_t etnaviv_gem_fault(struct vm_fault *vmf);
538c2ecf20Sopenharmony_ciint etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset);
548c2ecf20Sopenharmony_cistruct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj);
558c2ecf20Sopenharmony_civoid *etnaviv_gem_prime_vmap(struct drm_gem_object *obj);
568c2ecf20Sopenharmony_civoid etnaviv_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
578c2ecf20Sopenharmony_ciint etnaviv_gem_prime_mmap(struct drm_gem_object *obj,
588c2ecf20Sopenharmony_ci			   struct vm_area_struct *vma);
598c2ecf20Sopenharmony_cistruct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev,
608c2ecf20Sopenharmony_ci	struct dma_buf_attachment *attach, struct sg_table *sg);
618c2ecf20Sopenharmony_ciint etnaviv_gem_prime_pin(struct drm_gem_object *obj);
628c2ecf20Sopenharmony_civoid etnaviv_gem_prime_unpin(struct drm_gem_object *obj);
638c2ecf20Sopenharmony_civoid *etnaviv_gem_vmap(struct drm_gem_object *obj);
648c2ecf20Sopenharmony_ciint etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op,
658c2ecf20Sopenharmony_ci		struct drm_etnaviv_timespec *timeout);
668c2ecf20Sopenharmony_ciint etnaviv_gem_cpu_fini(struct drm_gem_object *obj);
678c2ecf20Sopenharmony_civoid etnaviv_gem_free_object(struct drm_gem_object *obj);
688c2ecf20Sopenharmony_ciint etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file,
698c2ecf20Sopenharmony_ci		u32 size, u32 flags, u32 *handle);
708c2ecf20Sopenharmony_ciint etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
718c2ecf20Sopenharmony_ci	uintptr_t ptr, u32 size, u32 flags, u32 *handle);
728c2ecf20Sopenharmony_ciu16 etnaviv_buffer_init(struct etnaviv_gpu *gpu);
738c2ecf20Sopenharmony_ciu16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe_addr);
748c2ecf20Sopenharmony_ciu16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu, unsigned short id);
758c2ecf20Sopenharmony_civoid etnaviv_buffer_end(struct etnaviv_gpu *gpu);
768c2ecf20Sopenharmony_civoid etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event);
778c2ecf20Sopenharmony_civoid etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
788c2ecf20Sopenharmony_ci	struct etnaviv_iommu_context *mmu,
798c2ecf20Sopenharmony_ci	unsigned int event, struct etnaviv_cmdbuf *cmdbuf);
808c2ecf20Sopenharmony_civoid etnaviv_validate_init(void);
818c2ecf20Sopenharmony_cibool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu,
828c2ecf20Sopenharmony_ci	u32 *stream, unsigned int size,
838c2ecf20Sopenharmony_ci	struct drm_etnaviv_gem_submit_reloc *relocs, unsigned int reloc_size);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
868c2ecf20Sopenharmony_civoid etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv,
878c2ecf20Sopenharmony_ci	struct seq_file *m);
888c2ecf20Sopenharmony_ci#endif
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
918c2ecf20Sopenharmony_ci#define VERB(fmt, ...) if (0) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/*
948c2ecf20Sopenharmony_ci * Return the storage size of a structure with a variable length array.
958c2ecf20Sopenharmony_ci * The array is nelem elements of elem_size, where the base structure
968c2ecf20Sopenharmony_ci * is defined by base.  If the size overflows size_t, return zero.
978c2ecf20Sopenharmony_ci */
988c2ecf20Sopenharmony_cistatic inline size_t size_vstruct(size_t nelem, size_t elem_size, size_t base)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	if (elem_size && nelem > (SIZE_MAX - base) / elem_size)
1018c2ecf20Sopenharmony_ci		return 0;
1028c2ecf20Sopenharmony_ci	return base + nelem * elem_size;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/*
1068c2ecf20Sopenharmony_ci * Etnaviv timeouts are specified wrt CLOCK_MONOTONIC, not jiffies.
1078c2ecf20Sopenharmony_ci * We need to calculate the timeout in terms of number of jiffies
1088c2ecf20Sopenharmony_ci * between the specified timeout and the current CLOCK_MONOTONIC time.
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_cistatic inline unsigned long etnaviv_timeout_to_jiffies(
1118c2ecf20Sopenharmony_ci	const struct drm_etnaviv_timespec *timeout)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct timespec64 ts, to = {
1148c2ecf20Sopenharmony_ci		.tv_sec = timeout->tv_sec,
1158c2ecf20Sopenharmony_ci		.tv_nsec = timeout->tv_nsec,
1168c2ecf20Sopenharmony_ci	};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	ktime_get_ts64(&ts);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* timeouts before "now" have already expired */
1218c2ecf20Sopenharmony_ci	if (timespec64_compare(&to, &ts) <= 0)
1228c2ecf20Sopenharmony_ci		return 0;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	ts = timespec64_sub(to, ts);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	return timespec64_to_jiffies(&ts);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#endif /* __ETNAVIV_DRV_H__ */
130