1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2015-2018 Etnaviv Project
4  */
5 
6 #ifndef __ETNAVIV_DRV_H__
7 #define __ETNAVIV_DRV_H__
8 
9 #include <linux/list.h>
10 #include <linux/mm_types.h>
11 #include <linux/sizes.h>
12 #include <linux/time64.h>
13 #include <linux/types.h>
14 
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_gem.h>
17 #include <drm/etnaviv_drm.h>
18 #include <drm/gpu_scheduler.h>
19 
20 struct etnaviv_cmdbuf;
21 struct etnaviv_gpu;
22 struct etnaviv_mmu;
23 struct etnaviv_gem_object;
24 struct etnaviv_gem_submit;
25 struct etnaviv_iommu_global;
26 
27 #define ETNAVIV_SOFTPIN_START_ADDRESS	SZ_4M /* must be >= SUBALLOC_SIZE */
28 
29 struct etnaviv_file_private {
30 	struct etnaviv_iommu_context	*mmu;
31 	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
32 };
33 
34 struct etnaviv_drm_private {
35 	int num_gpus;
36 	struct etnaviv_gpu *gpu[ETNA_MAX_PIPES];
37 	gfp_t shm_gfp_mask;
38 
39 	struct etnaviv_cmdbuf_suballoc *cmdbuf_suballoc;
40 	struct etnaviv_iommu_global *mmu_global;
41 
42 	/* list of GEM objects: */
43 	struct mutex gem_lock;
44 	struct list_head gem_list;
45 };
46 
47 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
48 		struct drm_file *file);
49 
50 int etnaviv_gem_mmap(struct file *filp, struct vm_area_struct *vma);
51 int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset);
52 struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj);
53 int etnaviv_gem_prime_vmap(struct drm_gem_object *obj, struct dma_buf_map *map);
54 int etnaviv_gem_prime_mmap(struct drm_gem_object *obj,
55 			   struct vm_area_struct *vma);
56 struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev,
57 	struct dma_buf_attachment *attach, struct sg_table *sg);
58 int etnaviv_gem_prime_pin(struct drm_gem_object *obj);
59 void etnaviv_gem_prime_unpin(struct drm_gem_object *obj);
60 void *etnaviv_gem_vmap(struct drm_gem_object *obj);
61 int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op,
62 		struct drm_etnaviv_timespec *timeout);
63 int etnaviv_gem_cpu_fini(struct drm_gem_object *obj);
64 void etnaviv_gem_free_object(struct drm_gem_object *obj);
65 int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file,
66 		u32 size, u32 flags, u32 *handle);
67 int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
68 	uintptr_t ptr, u32 size, u32 flags, u32 *handle);
69 u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu);
70 u16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe_addr);
71 u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu, unsigned short id);
72 void etnaviv_buffer_end(struct etnaviv_gpu *gpu);
73 void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event);
74 void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
75 	struct etnaviv_iommu_context *mmu,
76 	unsigned int event, struct etnaviv_cmdbuf *cmdbuf);
77 void etnaviv_validate_init(void);
78 bool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu,
79 	u32 *stream, unsigned int size,
80 	struct drm_etnaviv_gem_submit_reloc *relocs, unsigned int reloc_size);
81 
82 #ifdef CONFIG_DEBUG_FS
83 void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv,
84 	struct seq_file *m);
85 #endif
86 
87 #define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
88 #define VERB(fmt, ...) if (0) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
89 
90 /*
91  * Return the storage size of a structure with a variable length array.
92  * The array is nelem elements of elem_size, where the base structure
93  * is defined by base.  If the size overflows size_t, return zero.
94  */
95 static inline size_t size_vstruct(size_t nelem, size_t elem_size, size_t base)
96 {
97 	if (elem_size && nelem > (SIZE_MAX - base) / elem_size)
98 		return 0;
99 	return base + nelem * elem_size;
100 }
101 
102 /*
103  * Etnaviv timeouts are specified wrt CLOCK_MONOTONIC, not jiffies.
104  * We need to calculate the timeout in terms of number of jiffies
105  * between the specified timeout and the current CLOCK_MONOTONIC time.
106  */
107 static inline unsigned long etnaviv_timeout_to_jiffies(
108 	const struct drm_etnaviv_timespec *timeout)
109 {
110 	struct timespec64 ts, to = {
111 		.tv_sec = timeout->tv_sec,
112 		.tv_nsec = timeout->tv_nsec,
113 	};
114 
115 	ktime_get_ts64(&ts);
116 
117 	/* timeouts before "now" have already expired */
118 	if (timespec64_compare(&to, &ts) <= 0)
119 		return 0;
120 
121 	ts = timespec64_sub(to, ts);
122 
123 	return timespec64_to_jiffies(&ts);
124 }
125 
126 #endif /* __ETNAVIV_DRV_H__ */
127