1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #ifndef __MSM_GEM_H__
8 #define __MSM_GEM_H__
9 
10 #include <linux/kref.h>
11 #include <linux/dma-resv.h>
12 #include "drm/gpu_scheduler.h"
13 #include "msm_drv.h"
14 
15 /* Make all GEM related WARN_ON()s ratelimited.. when things go wrong they
16  * tend to go wrong 1000s of times in a short timespan.
17  */
18 #define GEM_WARN_ON(x)  WARN_RATELIMIT(x, "%s", __stringify(x))
19 
20 /* Additional internal-use only BO flags: */
21 #define MSM_BO_STOLEN        0x10000000    /* try to use stolen/splash memory */
22 #define MSM_BO_MAP_PRIV      0x20000000    /* use IOMMU_PRIV when mapping */
23 
24 struct msm_gem_address_space {
25 	const char *name;
26 	/* NOTE: mm managed at the page level, size is in # of pages
27 	 * and position mm_node->start is in # of pages:
28 	 */
29 	struct drm_mm mm;
30 	spinlock_t lock; /* Protects drm_mm node allocation/removal */
31 	struct msm_mmu *mmu;
32 	struct kref kref;
33 
34 	/* For address spaces associated with a specific process, this
35 	 * will be non-NULL:
36 	 */
37 	struct pid *pid;
38 
39 	/* @faults: the number of GPU hangs associated with this address space */
40 	int faults;
41 
42 	/** @va_start: lowest possible address to allocate */
43 	uint64_t va_start;
44 
45 	/** @va_size: the size of the address space (in bytes) */
46 	uint64_t va_size;
47 };
48 
49 struct msm_gem_address_space *
50 msm_gem_address_space_get(struct msm_gem_address_space *aspace);
51 
52 void msm_gem_address_space_put(struct msm_gem_address_space *aspace);
53 
54 struct msm_gem_address_space *
55 msm_gem_address_space_create(struct msm_mmu *mmu, const char *name,
56 		u64 va_start, u64 size);
57 
58 struct msm_fence_context;
59 
60 struct msm_gem_vma {
61 	struct drm_mm_node node;
62 	uint64_t iova;
63 	struct msm_gem_address_space *aspace;
64 	struct list_head list;    /* node in msm_gem_object::vmas */
65 	bool mapped;
66 };
67 
68 struct msm_gem_vma *msm_gem_vma_new(struct msm_gem_address_space *aspace);
69 int msm_gem_vma_init(struct msm_gem_vma *vma, int size,
70 		u64 range_start, u64 range_end);
71 void msm_gem_vma_purge(struct msm_gem_vma *vma);
72 int msm_gem_vma_map(struct msm_gem_vma *vma, int prot, struct sg_table *sgt, int size);
73 void msm_gem_vma_close(struct msm_gem_vma *vma);
74 
75 struct msm_gem_object {
76 	struct drm_gem_object base;
77 
78 	uint32_t flags;
79 
80 	/**
81 	 * madv: are the backing pages purgeable?
82 	 *
83 	 * Protected by obj lock and LRU lock
84 	 */
85 	uint8_t madv;
86 
87 	/**
88 	 * count of active vmap'ing
89 	 */
90 	uint8_t vmap_count;
91 
92 	/**
93 	 * Node in list of all objects (mainly for debugfs, protected by
94 	 * priv->obj_lock
95 	 */
96 	struct list_head node;
97 
98 	struct page **pages;
99 	struct sg_table *sgt;
100 	void *vaddr;
101 
102 	struct list_head vmas;    /* list of msm_gem_vma */
103 
104 	/* For physically contiguous buffers.  Used when we don't have
105 	 * an IOMMU.  Also used for stolen/splashscreen buffer.
106 	 */
107 	struct drm_mm_node *vram_node;
108 
109 	char name[32]; /* Identifier to print for the debugfs files */
110 
111 	/**
112 	 * pin_count: Number of times the pages are pinned
113 	 *
114 	 * Protected by LRU lock.
115 	 */
116 	int pin_count;
117 };
118 #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
119 
120 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj);
121 int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma);
122 void msm_gem_unpin_locked(struct drm_gem_object *obj);
123 void msm_gem_unpin_active(struct drm_gem_object *obj);
124 struct msm_gem_vma *msm_gem_get_vma_locked(struct drm_gem_object *obj,
125 					   struct msm_gem_address_space *aspace);
126 int msm_gem_get_iova(struct drm_gem_object *obj,
127 		struct msm_gem_address_space *aspace, uint64_t *iova);
128 int msm_gem_set_iova(struct drm_gem_object *obj,
129 		struct msm_gem_address_space *aspace, uint64_t iova);
130 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
131 		struct msm_gem_address_space *aspace, uint64_t *iova,
132 		u64 range_start, u64 range_end);
133 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
134 		struct msm_gem_address_space *aspace, uint64_t *iova);
135 void msm_gem_unpin_iova(struct drm_gem_object *obj,
136 		struct msm_gem_address_space *aspace);
137 void msm_gem_pin_obj_locked(struct drm_gem_object *obj);
138 struct page **msm_gem_pin_pages(struct drm_gem_object *obj);
139 void msm_gem_unpin_pages(struct drm_gem_object *obj);
140 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
141 		struct drm_mode_create_dumb *args);
142 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
143 		uint32_t handle, uint64_t *offset);
144 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj);
145 void *msm_gem_get_vaddr(struct drm_gem_object *obj);
146 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj);
147 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj);
148 void msm_gem_put_vaddr(struct drm_gem_object *obj);
149 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv);
150 bool msm_gem_active(struct drm_gem_object *obj);
151 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout);
152 int msm_gem_cpu_fini(struct drm_gem_object *obj);
153 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
154 		uint32_t size, uint32_t flags, uint32_t *handle, char *name);
155 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
156 		uint32_t size, uint32_t flags);
157 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
158 		uint32_t flags, struct msm_gem_address_space *aspace,
159 		struct drm_gem_object **bo, uint64_t *iova);
160 void msm_gem_kernel_put(struct drm_gem_object *bo,
161 		struct msm_gem_address_space *aspace);
162 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
163 		struct dma_buf *dmabuf, struct sg_table *sgt);
164 __printf(2, 3)
165 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...);
166 
167 #ifdef CONFIG_DEBUG_FS
168 struct msm_gem_stats {
169 	struct {
170 		unsigned count;
171 		size_t size;
172 	} all, active, resident, purgeable, purged;
173 };
174 
175 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
176 		struct msm_gem_stats *stats);
177 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m);
178 #endif
179 
180 static inline void
msm_gem_lock(struct drm_gem_object * obj)181 msm_gem_lock(struct drm_gem_object *obj)
182 {
183 	dma_resv_lock(obj->resv, NULL);
184 }
185 
186 static inline int
msm_gem_lock_interruptible(struct drm_gem_object * obj)187 msm_gem_lock_interruptible(struct drm_gem_object *obj)
188 {
189 	return dma_resv_lock_interruptible(obj->resv, NULL);
190 }
191 
192 static inline void
msm_gem_unlock(struct drm_gem_object * obj)193 msm_gem_unlock(struct drm_gem_object *obj)
194 {
195 	dma_resv_unlock(obj->resv);
196 }
197 
198 static inline void
msm_gem_assert_locked(struct drm_gem_object * obj)199 msm_gem_assert_locked(struct drm_gem_object *obj)
200 {
201 	/*
202 	 * Destroying the object is a special case.. msm_gem_free_object()
203 	 * calls many things that WARN_ON if the obj lock is not held.  But
204 	 * acquiring the obj lock in msm_gem_free_object() can cause a
205 	 * locking order inversion between reservation_ww_class_mutex and
206 	 * fs_reclaim.
207 	 *
208 	 * This deadlock is not actually possible, because no one should
209 	 * be already holding the lock when msm_gem_free_object() is called.
210 	 * Unfortunately lockdep is not aware of this detail.  So when the
211 	 * refcount drops to zero, we pretend it is already locked.
212 	 */
213 	lockdep_assert_once(
214 		(kref_read(&obj->refcount) == 0) ||
215 		(lockdep_is_held(&obj->resv->lock.base) != LOCK_STATE_NOT_HELD)
216 	);
217 }
218 
219 /* imported/exported objects are not purgeable: */
is_unpurgeable(struct msm_gem_object * msm_obj)220 static inline bool is_unpurgeable(struct msm_gem_object *msm_obj)
221 {
222 	return msm_obj->base.import_attach || msm_obj->pin_count;
223 }
224 
is_purgeable(struct msm_gem_object * msm_obj)225 static inline bool is_purgeable(struct msm_gem_object *msm_obj)
226 {
227 	return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt &&
228 			!is_unpurgeable(msm_obj);
229 }
230 
is_vunmapable(struct msm_gem_object * msm_obj)231 static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
232 {
233 	msm_gem_assert_locked(&msm_obj->base);
234 	return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
235 }
236 
is_unevictable(struct msm_gem_object * msm_obj)237 static inline bool is_unevictable(struct msm_gem_object *msm_obj)
238 {
239 	return is_unpurgeable(msm_obj) || msm_obj->vaddr;
240 }
241 
242 void msm_gem_purge(struct drm_gem_object *obj);
243 void msm_gem_evict(struct drm_gem_object *obj);
244 void msm_gem_vunmap(struct drm_gem_object *obj);
245 
246 /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
247  * associated with the cmdstream submission for synchronization (and
248  * make it easier to unwind when things go wrong, etc).
249  */
250 struct msm_gem_submit {
251 	struct drm_sched_job base;
252 	struct kref ref;
253 	struct drm_device *dev;
254 	struct msm_gpu *gpu;
255 	struct msm_gem_address_space *aspace;
256 	struct list_head node;   /* node in ring submit list */
257 	struct ww_acquire_ctx ticket;
258 	uint32_t seqno;		/* Sequence number of the submit on the ring */
259 
260 	/* Hw fence, which is created when the scheduler executes the job, and
261 	 * is signaled when the hw finishes (via seqno write from cmdstream)
262 	 */
263 	struct dma_fence *hw_fence;
264 
265 	/* Userspace visible fence, which is signaled by the scheduler after
266 	 * the hw_fence is signaled.
267 	 */
268 	struct dma_fence *user_fence;
269 
270 	int fence_id;       /* key into queue->fence_idr */
271 	struct msm_gpu_submitqueue *queue;
272 	struct pid *pid;    /* submitting process */
273 	bool fault_dumped;  /* Limit devcoredump dumping to one per submit */
274 	bool valid;         /* true if no cmdstream patching needed */
275 	bool in_rb;         /* "sudo" mode, copy cmds into RB */
276 	struct msm_ringbuffer *ring;
277 	unsigned int nr_cmds;
278 	unsigned int nr_bos;
279 	u32 ident;	   /* A "identifier" for the submit for logging */
280 	struct {
281 		uint32_t type;
282 		uint32_t size;  /* in dwords */
283 		uint64_t iova;
284 		uint32_t offset;/* in dwords */
285 		uint32_t idx;   /* cmdstream buffer idx in bos[] */
286 		uint32_t nr_relocs;
287 		struct drm_msm_gem_submit_reloc *relocs;
288 	} *cmd;  /* array of size nr_cmds */
289 	struct {
290 /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
291 #define BO_VALID	0x8000	/* is current addr in cmdstream correct/valid? */
292 #define BO_LOCKED	0x4000	/* obj lock is held */
293 #define BO_PINNED	0x2000	/* obj (pages) is pinned and on active list */
294 		uint32_t flags;
295 		union {
296 			struct drm_gem_object *obj;
297 			uint32_t handle;
298 		};
299 		uint64_t iova;
300 	} bos[];
301 };
302 
to_msm_submit(struct drm_sched_job * job)303 static inline struct msm_gem_submit *to_msm_submit(struct drm_sched_job *job)
304 {
305 	return container_of(job, struct msm_gem_submit, base);
306 }
307 
308 void __msm_gem_submit_destroy(struct kref *kref);
309 
msm_gem_submit_get(struct msm_gem_submit * submit)310 static inline void msm_gem_submit_get(struct msm_gem_submit *submit)
311 {
312 	kref_get(&submit->ref);
313 }
314 
msm_gem_submit_put(struct msm_gem_submit * submit)315 static inline void msm_gem_submit_put(struct msm_gem_submit *submit)
316 {
317 	kref_put(&submit->ref, __msm_gem_submit_destroy);
318 }
319 
320 void msm_submit_retire(struct msm_gem_submit *submit);
321 
322 /* helper to determine of a buffer in submit should be dumped, used for both
323  * devcoredump and debugfs cmdstream dumping:
324  */
325 static inline bool
should_dump(struct msm_gem_submit * submit,int idx)326 should_dump(struct msm_gem_submit *submit, int idx)
327 {
328 	extern bool rd_full;
329 	return rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP);
330 }
331 
332 #endif /* __MSM_GEM_H__ */
333