7c7d54bad189bbc4042c08bbd56e65ac3255caeb
[linux-2.6-microblaze.git] / drivers / gpu / drm / msm / msm_gem.h
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 "msm_drv.h"
13
14 /* Additional internal-use only BO flags: */
15 #define MSM_BO_STOLEN        0x10000000    /* try to use stolen/splash memory */
16 #define MSM_BO_MAP_PRIV      0x20000000    /* use IOMMU_PRIV when mapping */
17
18 struct msm_gem_address_space {
19         const char *name;
20         /* NOTE: mm managed at the page level, size is in # of pages
21          * and position mm_node->start is in # of pages:
22          */
23         struct drm_mm mm;
24         spinlock_t lock; /* Protects drm_mm node allocation/removal */
25         struct msm_mmu *mmu;
26         struct kref kref;
27
28         /* For address spaces associated with a specific process, this
29          * will be non-NULL:
30          */
31         struct pid *pid;
32 };
33
34 struct msm_gem_vma {
35         struct drm_mm_node node;
36         uint64_t iova;
37         struct msm_gem_address_space *aspace;
38         struct list_head list;    /* node in msm_gem_object::vmas */
39         bool mapped;
40         int inuse;
41 };
42
43 struct msm_gem_object {
44         struct drm_gem_object base;
45
46         uint32_t flags;
47
48         /**
49          * Advice: are the backing pages purgeable?
50          */
51         uint8_t madv;
52
53         /**
54          * Is object on inactive_dontneed list (ie. counted in priv->shrinkable_count)?
55          */
56         bool dontneed : 1;
57
58         /**
59          * count of active vmap'ing
60          */
61         uint8_t vmap_count;
62
63         /**
64          * Node in list of all objects (mainly for debugfs, protected by
65          * priv->obj_lock
66          */
67         struct list_head node;
68
69         /**
70          * An object is either:
71          *  inactive - on priv->inactive_dontneed or priv->inactive_willneed
72          *     (depending on purgability status)
73          *  active   - on one one of the gpu's active_list..  well, at
74          *     least for now we don't have (I don't think) hw sync between
75          *     2d and 3d one devices which have both, meaning we need to
76          *     block on submit if a bo is already on other ring
77          */
78         struct list_head mm_list;
79
80         /* Transiently in the process of submit ioctl, objects associated
81          * with the submit are on submit->bo_list.. this only lasts for
82          * the duration of the ioctl, so one bo can never be on multiple
83          * submit lists.
84          */
85         struct list_head submit_entry;
86
87         struct page **pages;
88         struct sg_table *sgt;
89         void *vaddr;
90
91         struct list_head vmas;    /* list of msm_gem_vma */
92
93         /* For physically contiguous buffers.  Used when we don't have
94          * an IOMMU.  Also used for stolen/splashscreen buffer.
95          */
96         struct drm_mm_node *vram_node;
97
98         char name[32]; /* Identifier to print for the debugfs files */
99
100         int active_count;
101 };
102 #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
103
104 int msm_gem_mmap_obj(struct drm_gem_object *obj,
105                         struct vm_area_struct *vma);
106 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
107 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj);
108 int msm_gem_get_iova(struct drm_gem_object *obj,
109                 struct msm_gem_address_space *aspace, uint64_t *iova);
110 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj,
111                 struct msm_gem_address_space *aspace, uint64_t *iova,
112                 u64 range_start, u64 range_end);
113 int msm_gem_get_and_pin_iova_locked(struct drm_gem_object *obj,
114                 struct msm_gem_address_space *aspace, uint64_t *iova);
115 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
116                 struct msm_gem_address_space *aspace, uint64_t *iova);
117 uint64_t msm_gem_iova(struct drm_gem_object *obj,
118                 struct msm_gem_address_space *aspace);
119 void msm_gem_unpin_iova_locked(struct drm_gem_object *obj,
120                 struct msm_gem_address_space *aspace);
121 void msm_gem_unpin_iova(struct drm_gem_object *obj,
122                 struct msm_gem_address_space *aspace);
123 struct page **msm_gem_get_pages(struct drm_gem_object *obj);
124 void msm_gem_put_pages(struct drm_gem_object *obj);
125 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
126                 struct drm_mode_create_dumb *args);
127 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
128                 uint32_t handle, uint64_t *offset);
129 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj);
130 void *msm_gem_get_vaddr(struct drm_gem_object *obj);
131 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj);
132 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj);
133 void msm_gem_put_vaddr(struct drm_gem_object *obj);
134 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv);
135 int msm_gem_sync_object(struct drm_gem_object *obj,
136                 struct msm_fence_context *fctx, bool exclusive);
137 void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu);
138 void msm_gem_active_put(struct drm_gem_object *obj);
139 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout);
140 int msm_gem_cpu_fini(struct drm_gem_object *obj);
141 void msm_gem_free_object(struct drm_gem_object *obj);
142 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
143                 uint32_t size, uint32_t flags, uint32_t *handle, char *name);
144 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
145                 uint32_t size, uint32_t flags);
146 struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev,
147                 uint32_t size, uint32_t flags);
148 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
149                 uint32_t flags, struct msm_gem_address_space *aspace,
150                 struct drm_gem_object **bo, uint64_t *iova);
151 void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size,
152                 uint32_t flags, struct msm_gem_address_space *aspace,
153                 struct drm_gem_object **bo, uint64_t *iova);
154 void msm_gem_kernel_put(struct drm_gem_object *bo,
155                 struct msm_gem_address_space *aspace, bool locked);
156 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
157                 struct dma_buf *dmabuf, struct sg_table *sgt);
158 __printf(2, 3)
159 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...);
160 #ifdef CONFIG_DEBUG_FS
161
162 struct msm_gem_stats {
163         struct {
164                 unsigned count;
165                 size_t size;
166         } all, active, purgable, purged;
167 };
168
169 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m,
170                 struct msm_gem_stats *stats);
171 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m);
172 #endif
173
174 static inline void
175 msm_gem_lock(struct drm_gem_object *obj)
176 {
177         dma_resv_lock(obj->resv, NULL);
178 }
179
180 static inline bool __must_check
181 msm_gem_trylock(struct drm_gem_object *obj)
182 {
183         return dma_resv_trylock(obj->resv);
184 }
185
186 static inline int
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
193 msm_gem_unlock(struct drm_gem_object *obj)
194 {
195         dma_resv_unlock(obj->resv);
196 }
197
198 static inline bool
199 msm_gem_is_locked(struct drm_gem_object *obj)
200 {
201         return dma_resv_is_locked(obj->resv);
202 }
203
204 static inline bool is_active(struct msm_gem_object *msm_obj)
205 {
206         WARN_ON(!msm_gem_is_locked(&msm_obj->base));
207         return msm_obj->active_count;
208 }
209
210 /* imported/exported objects are not purgable: */
211 static inline bool is_unpurgable(struct msm_gem_object *msm_obj)
212 {
213         return msm_obj->base.dma_buf && msm_obj->base.import_attach;
214 }
215
216 static inline bool is_purgeable(struct msm_gem_object *msm_obj)
217 {
218         return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt &&
219                         !is_unpurgable(msm_obj);
220 }
221
222 static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
223 {
224         WARN_ON(!msm_gem_is_locked(&msm_obj->base));
225         return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
226 }
227
228 static inline void mark_purgable(struct msm_gem_object *msm_obj)
229 {
230         struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
231
232         WARN_ON(!mutex_is_locked(&priv->mm_lock));
233
234         if (is_unpurgable(msm_obj))
235                 return;
236
237         if (WARN_ON(msm_obj->dontneed))
238                 return;
239
240         priv->shrinkable_count += msm_obj->base.size >> PAGE_SHIFT;
241         msm_obj->dontneed = true;
242 }
243
244 static inline void mark_unpurgable(struct msm_gem_object *msm_obj)
245 {
246         struct msm_drm_private *priv = msm_obj->base.dev->dev_private;
247
248         WARN_ON(!mutex_is_locked(&priv->mm_lock));
249
250         if (is_unpurgable(msm_obj))
251                 return;
252
253         if (WARN_ON(!msm_obj->dontneed))
254                 return;
255
256         priv->shrinkable_count -= msm_obj->base.size >> PAGE_SHIFT;
257         WARN_ON(priv->shrinkable_count < 0);
258         msm_obj->dontneed = false;
259 }
260
261 void msm_gem_purge(struct drm_gem_object *obj);
262 void msm_gem_vunmap(struct drm_gem_object *obj);
263
264 /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
265  * associated with the cmdstream submission for synchronization (and
266  * make it easier to unwind when things go wrong, etc).  This only
267  * lasts for the duration of the submit-ioctl.
268  */
269 struct msm_gem_submit {
270         struct kref ref;
271         struct drm_device *dev;
272         struct msm_gpu *gpu;
273         struct msm_gem_address_space *aspace;
274         struct list_head node;   /* node in ring submit list */
275         struct list_head bo_list;
276         struct ww_acquire_ctx ticket;
277         uint32_t seqno;         /* Sequence number of the submit on the ring */
278         struct dma_fence *fence;
279         struct msm_gpu_submitqueue *queue;
280         struct pid *pid;    /* submitting process */
281         bool valid;         /* true if no cmdstream patching needed */
282         bool in_rb;         /* "sudo" mode, copy cmds into RB */
283         struct msm_ringbuffer *ring;
284         struct msm_file_private *ctx;
285         unsigned int nr_cmds;
286         unsigned int nr_bos;
287         u32 ident;         /* A "identifier" for the submit for logging */
288         struct {
289                 uint32_t type;
290                 uint32_t size;  /* in dwords */
291                 uint64_t iova;
292                 uint32_t offset;/* in dwords */
293                 uint32_t idx;   /* cmdstream buffer idx in bos[] */
294                 uint32_t nr_relocs;
295                 struct drm_msm_gem_submit_reloc *relocs;
296         } *cmd;  /* array of size nr_cmds */
297         struct {
298                 uint32_t flags;
299                 union {
300                         struct msm_gem_object *obj;
301                         uint32_t handle;
302                 };
303                 uint64_t iova;
304         } bos[];
305 };
306
307 void __msm_gem_submit_destroy(struct kref *kref);
308
309 static inline void msm_gem_submit_get(struct msm_gem_submit *submit)
310 {
311         kref_get(&submit->ref);
312 }
313
314 static inline void msm_gem_submit_put(struct msm_gem_submit *submit)
315 {
316         kref_put(&submit->ref, __msm_gem_submit_destroy);
317 }
318
319 /* helper to determine of a buffer in submit should be dumped, used for both
320  * devcoredump and debugfs cmdstream dumping:
321  */
322 static inline bool
323 should_dump(struct msm_gem_submit *submit, int idx)
324 {
325         extern bool rd_full;
326         return rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP);
327 }
328
329 #endif /* __MSM_GEM_H__ */