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