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/kernel.h>
10 #include <linux/clk.h>
11 #include <linux/cpufreq.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/time64.h>
19 #include <linux/types.h>
20 #include <linux/sizes.h>
21 #include <linux/mm_types.h>
22 
23 #include <drm/drmP.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_fb_helper.h>
26 #include <drm/drm_gem.h>
27 #include <drm/etnaviv_drm.h>
28 #include <drm/gpu_scheduler.h>
29 
30 struct etnaviv_cmdbuf;
31 struct etnaviv_gpu;
32 struct etnaviv_mmu;
33 struct etnaviv_gem_object;
34 struct etnaviv_gem_submit;
35 
36 struct etnaviv_file_private {
37 	/*
38 	 * When per-context address spaces are supported we'd keep track of
39 	 * the context's page-tables here.
40 	 */
41 	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
42 };
43 
44 struct etnaviv_drm_private {
45 	int num_gpus;
46 	struct etnaviv_gpu *gpu[ETNA_MAX_PIPES];
47 
48 	/* list of GEM objects: */
49 	struct mutex gem_lock;
50 	struct list_head gem_list;
51 };
52 
53 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
54 		struct drm_file *file);
55 
56 int etnaviv_gem_mmap(struct file *filp, struct vm_area_struct *vma);
57 vm_fault_t etnaviv_gem_fault(struct vm_fault *vmf);
58 int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset);
59 struct sg_table *etnaviv_gem_prime_get_sg_table(struct drm_gem_object *obj);
60 void *etnaviv_gem_prime_vmap(struct drm_gem_object *obj);
61 void etnaviv_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
62 int etnaviv_gem_prime_mmap(struct drm_gem_object *obj,
63 			   struct vm_area_struct *vma);
64 struct reservation_object *etnaviv_gem_prime_res_obj(struct drm_gem_object *obj);
65 struct drm_gem_object *etnaviv_gem_prime_import_sg_table(struct drm_device *dev,
66 	struct dma_buf_attachment *attach, struct sg_table *sg);
67 int etnaviv_gem_prime_pin(struct drm_gem_object *obj);
68 void etnaviv_gem_prime_unpin(struct drm_gem_object *obj);
69 void *etnaviv_gem_vmap(struct drm_gem_object *obj);
70 int etnaviv_gem_cpu_prep(struct drm_gem_object *obj, u32 op,
71 		struct timespec *timeout);
72 int etnaviv_gem_cpu_fini(struct drm_gem_object *obj);
73 void etnaviv_gem_free_object(struct drm_gem_object *obj);
74 int etnaviv_gem_new_handle(struct drm_device *dev, struct drm_file *file,
75 		u32 size, u32 flags, u32 *handle);
76 int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
77 	uintptr_t ptr, u32 size, u32 flags, u32 *handle);
78 u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu);
79 u16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe_addr);
80 u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu);
81 void etnaviv_buffer_end(struct etnaviv_gpu *gpu);
82 void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event);
83 void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
84 	unsigned int event, struct etnaviv_cmdbuf *cmdbuf);
85 void etnaviv_validate_init(void);
86 bool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu,
87 	u32 *stream, unsigned int size,
88 	struct drm_etnaviv_gem_submit_reloc *relocs, unsigned int reloc_size);
89 
90 #ifdef CONFIG_DEBUG_FS
91 void etnaviv_gem_describe_objects(struct etnaviv_drm_private *priv,
92 	struct seq_file *m);
93 #endif
94 
95 #define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
96 #define VERB(fmt, ...) if (0) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
97 
98 /*
99  * Return the storage size of a structure with a variable length array.
100  * The array is nelem elements of elem_size, where the base structure
101  * is defined by base.  If the size overflows size_t, return zero.
102  */
103 static inline size_t size_vstruct(size_t nelem, size_t elem_size, size_t base)
104 {
105 	if (elem_size && nelem > (SIZE_MAX - base) / elem_size)
106 		return 0;
107 	return base + nelem * elem_size;
108 }
109 
110 /*
111  * Etnaviv timeouts are specified wrt CLOCK_MONOTONIC, not jiffies.
112  * We need to calculate the timeout in terms of number of jiffies
113  * between the specified timeout and the current CLOCK_MONOTONIC time.
114  */
115 static inline unsigned long etnaviv_timeout_to_jiffies(
116 	const struct timespec *timeout)
117 {
118 	struct timespec64 ts, to;
119 
120 	to = timespec_to_timespec64(*timeout);
121 
122 	ktime_get_ts64(&ts);
123 
124 	/* timeouts before "now" have already expired */
125 	if (timespec64_compare(&to, &ts) <= 0)
126 		return 0;
127 
128 	ts = timespec64_sub(to, ts);
129 
130 	return timespec64_to_jiffies(&ts);
131 }
132 
133 #endif /* __ETNAVIV_DRV_H__ */
134