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 43 struct msm_gem_address_space * 44 msm_gem_address_space_get(struct msm_gem_address_space *aspace); 45 46 void msm_gem_address_space_put(struct msm_gem_address_space *aspace); 47 48 struct msm_gem_address_space * 49 msm_gem_address_space_create(struct msm_mmu *mmu, const char *name, 50 u64 va_start, u64 size); 51 52 struct msm_fence_context; 53 54 struct msm_gem_vma { 55 struct drm_mm_node node; 56 uint64_t iova; 57 struct msm_gem_address_space *aspace; 58 struct list_head list; /* node in msm_gem_object::vmas */ 59 bool mapped; 60 int inuse; 61 uint32_t fence_mask; 62 uint32_t fence[MSM_GPU_MAX_RINGS]; 63 struct msm_fence_context *fctx[MSM_GPU_MAX_RINGS]; 64 }; 65 66 int msm_gem_init_vma(struct msm_gem_address_space *aspace, 67 struct msm_gem_vma *vma, int size, 68 u64 range_start, u64 range_end); 69 bool msm_gem_vma_inuse(struct msm_gem_vma *vma); 70 void msm_gem_purge_vma(struct msm_gem_address_space *aspace, 71 struct msm_gem_vma *vma); 72 void msm_gem_unpin_vma(struct msm_gem_vma *vma); 73 void msm_gem_unpin_vma_fenced(struct msm_gem_vma *vma, struct msm_fence_context *fctx); 74 int msm_gem_map_vma(struct msm_gem_address_space *aspace, 75 struct msm_gem_vma *vma, int prot, 76 struct sg_table *sgt, int size); 77 void msm_gem_close_vma(struct msm_gem_address_space *aspace, 78 struct msm_gem_vma *vma); 79 80 struct msm_gem_object { 81 struct drm_gem_object base; 82 83 uint32_t flags; 84 85 /** 86 * Advice: are the backing pages purgeable? 87 */ 88 uint8_t madv; 89 90 /** 91 * Is object on inactive_dontneed list (ie. counted in priv->shrinkable_count)? 92 */ 93 bool dontneed : 1; 94 95 /** 96 * Is object evictable (ie. counted in priv->evictable_count)? 97 */ 98 bool evictable : 1; 99 100 /** 101 * count of active vmap'ing 102 */ 103 uint8_t vmap_count; 104 105 /** 106 * Node in list of all objects (mainly for debugfs, protected by 107 * priv->obj_lock 108 */ 109 struct list_head node; 110 111 /** 112 * An object is either: 113 * inactive - on priv->inactive_dontneed or priv->inactive_willneed 114 * (depending on purgeability status) 115 * active - on one one of the gpu's active_list.. well, at 116 * least for now we don't have (I don't think) hw sync between 117 * 2d and 3d one devices which have both, meaning we need to 118 * block on submit if a bo is already on other ring 119 */ 120 struct list_head mm_list; 121 122 struct page **pages; 123 struct sg_table *sgt; 124 void *vaddr; 125 126 struct list_head vmas; /* list of msm_gem_vma */ 127 128 /* For physically contiguous buffers. Used when we don't have 129 * an IOMMU. Also used for stolen/splashscreen buffer. 130 */ 131 struct drm_mm_node *vram_node; 132 133 char name[32]; /* Identifier to print for the debugfs files */ 134 135 int active_count; 136 int pin_count; 137 }; 138 #define to_msm_bo(x) container_of(x, struct msm_gem_object, base) 139 140 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj); 141 int msm_gem_pin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma); 142 void msm_gem_unpin_vma_locked(struct drm_gem_object *obj, struct msm_gem_vma *vma); 143 struct msm_gem_vma *msm_gem_get_vma_locked(struct drm_gem_object *obj, 144 struct msm_gem_address_space *aspace); 145 int msm_gem_get_iova(struct drm_gem_object *obj, 146 struct msm_gem_address_space *aspace, uint64_t *iova); 147 int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj, 148 struct msm_gem_address_space *aspace, uint64_t *iova, 149 u64 range_start, u64 range_end); 150 int msm_gem_get_and_pin_iova(struct drm_gem_object *obj, 151 struct msm_gem_address_space *aspace, uint64_t *iova); 152 void msm_gem_unpin_iova(struct drm_gem_object *obj, 153 struct msm_gem_address_space *aspace); 154 struct page **msm_gem_get_pages(struct drm_gem_object *obj); 155 void msm_gem_put_pages(struct drm_gem_object *obj); 156 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev, 157 struct drm_mode_create_dumb *args); 158 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, 159 uint32_t handle, uint64_t *offset); 160 void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj); 161 void *msm_gem_get_vaddr(struct drm_gem_object *obj); 162 void *msm_gem_get_vaddr_active(struct drm_gem_object *obj); 163 void msm_gem_put_vaddr_locked(struct drm_gem_object *obj); 164 void msm_gem_put_vaddr(struct drm_gem_object *obj); 165 int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv); 166 void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu); 167 void msm_gem_active_put(struct drm_gem_object *obj); 168 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout); 169 int msm_gem_cpu_fini(struct drm_gem_object *obj); 170 void msm_gem_free_object(struct drm_gem_object *obj); 171 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, 172 uint32_t size, uint32_t flags, uint32_t *handle, char *name); 173 struct drm_gem_object *msm_gem_new(struct drm_device *dev, 174 uint32_t size, uint32_t flags); 175 void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size, 176 uint32_t flags, struct msm_gem_address_space *aspace, 177 struct drm_gem_object **bo, uint64_t *iova); 178 void msm_gem_kernel_put(struct drm_gem_object *bo, 179 struct msm_gem_address_space *aspace); 180 struct drm_gem_object *msm_gem_import(struct drm_device *dev, 181 struct dma_buf *dmabuf, struct sg_table *sgt); 182 __printf(2, 3) 183 void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...); 184 185 #ifdef CONFIG_DEBUG_FS 186 struct msm_gem_stats { 187 struct { 188 unsigned count; 189 size_t size; 190 } all, active, resident, purgeable, purged; 191 }; 192 193 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m, 194 struct msm_gem_stats *stats); 195 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m); 196 #endif 197 198 static inline void 199 msm_gem_lock(struct drm_gem_object *obj) 200 { 201 dma_resv_lock(obj->resv, NULL); 202 } 203 204 static inline bool __must_check 205 msm_gem_trylock(struct drm_gem_object *obj) 206 { 207 return dma_resv_trylock(obj->resv); 208 } 209 210 static inline int 211 msm_gem_lock_interruptible(struct drm_gem_object *obj) 212 { 213 return dma_resv_lock_interruptible(obj->resv, NULL); 214 } 215 216 static inline void 217 msm_gem_unlock(struct drm_gem_object *obj) 218 { 219 dma_resv_unlock(obj->resv); 220 } 221 222 static inline bool 223 msm_gem_is_locked(struct drm_gem_object *obj) 224 { 225 return dma_resv_is_locked(obj->resv); 226 } 227 228 static inline bool is_active(struct msm_gem_object *msm_obj) 229 { 230 GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base)); 231 return msm_obj->active_count; 232 } 233 234 /* imported/exported objects are not purgeable: */ 235 static inline bool is_unpurgeable(struct msm_gem_object *msm_obj) 236 { 237 return msm_obj->base.import_attach || msm_obj->pin_count; 238 } 239 240 static inline bool is_purgeable(struct msm_gem_object *msm_obj) 241 { 242 return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt && 243 !is_unpurgeable(msm_obj); 244 } 245 246 static inline bool is_vunmapable(struct msm_gem_object *msm_obj) 247 { 248 GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base)); 249 return (msm_obj->vmap_count == 0) && msm_obj->vaddr; 250 } 251 252 static inline void mark_purgeable(struct msm_gem_object *msm_obj) 253 { 254 struct msm_drm_private *priv = msm_obj->base.dev->dev_private; 255 256 GEM_WARN_ON(!mutex_is_locked(&priv->mm_lock)); 257 258 if (is_unpurgeable(msm_obj)) 259 return; 260 261 if (GEM_WARN_ON(msm_obj->dontneed)) 262 return; 263 264 priv->shrinkable_count += msm_obj->base.size >> PAGE_SHIFT; 265 msm_obj->dontneed = true; 266 } 267 268 static inline void mark_unpurgeable(struct msm_gem_object *msm_obj) 269 { 270 struct msm_drm_private *priv = msm_obj->base.dev->dev_private; 271 272 GEM_WARN_ON(!mutex_is_locked(&priv->mm_lock)); 273 274 if (is_unpurgeable(msm_obj)) 275 return; 276 277 if (GEM_WARN_ON(!msm_obj->dontneed)) 278 return; 279 280 priv->shrinkable_count -= msm_obj->base.size >> PAGE_SHIFT; 281 GEM_WARN_ON(priv->shrinkable_count < 0); 282 msm_obj->dontneed = false; 283 } 284 285 static inline bool is_unevictable(struct msm_gem_object *msm_obj) 286 { 287 return is_unpurgeable(msm_obj) || msm_obj->vaddr; 288 } 289 290 static inline void mark_evictable(struct msm_gem_object *msm_obj) 291 { 292 struct msm_drm_private *priv = msm_obj->base.dev->dev_private; 293 294 WARN_ON(!mutex_is_locked(&priv->mm_lock)); 295 296 if (is_unevictable(msm_obj)) 297 return; 298 299 if (WARN_ON(msm_obj->evictable)) 300 return; 301 302 priv->evictable_count += msm_obj->base.size >> PAGE_SHIFT; 303 msm_obj->evictable = true; 304 } 305 306 static inline void mark_unevictable(struct msm_gem_object *msm_obj) 307 { 308 struct msm_drm_private *priv = msm_obj->base.dev->dev_private; 309 310 WARN_ON(!mutex_is_locked(&priv->mm_lock)); 311 312 if (is_unevictable(msm_obj)) 313 return; 314 315 if (WARN_ON(!msm_obj->evictable)) 316 return; 317 318 priv->evictable_count -= msm_obj->base.size >> PAGE_SHIFT; 319 WARN_ON(priv->evictable_count < 0); 320 msm_obj->evictable = false; 321 } 322 323 void msm_gem_purge(struct drm_gem_object *obj); 324 void msm_gem_evict(struct drm_gem_object *obj); 325 void msm_gem_vunmap(struct drm_gem_object *obj); 326 327 /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc, 328 * associated with the cmdstream submission for synchronization (and 329 * make it easier to unwind when things go wrong, etc). 330 */ 331 struct msm_gem_submit { 332 struct drm_sched_job base; 333 struct kref ref; 334 struct drm_device *dev; 335 struct msm_gpu *gpu; 336 struct msm_gem_address_space *aspace; 337 struct list_head node; /* node in ring submit list */ 338 struct ww_acquire_ctx ticket; 339 uint32_t seqno; /* Sequence number of the submit on the ring */ 340 341 /* Hw fence, which is created when the scheduler executes the job, and 342 * is signaled when the hw finishes (via seqno write from cmdstream) 343 */ 344 struct dma_fence *hw_fence; 345 346 /* Userspace visible fence, which is signaled by the scheduler after 347 * the hw_fence is signaled. 348 */ 349 struct dma_fence *user_fence; 350 351 int fence_id; /* key into queue->fence_idr */ 352 struct msm_gpu_submitqueue *queue; 353 struct pid *pid; /* submitting process */ 354 bool fault_dumped; /* Limit devcoredump dumping to one per submit */ 355 bool valid; /* true if no cmdstream patching needed */ 356 bool in_rb; /* "sudo" mode, copy cmds into RB */ 357 struct msm_ringbuffer *ring; 358 unsigned int nr_cmds; 359 unsigned int nr_bos; 360 u32 ident; /* A "identifier" for the submit for logging */ 361 struct { 362 uint32_t type; 363 uint32_t size; /* in dwords */ 364 uint64_t iova; 365 uint32_t offset;/* in dwords */ 366 uint32_t idx; /* cmdstream buffer idx in bos[] */ 367 uint32_t nr_relocs; 368 struct drm_msm_gem_submit_reloc *relocs; 369 } *cmd; /* array of size nr_cmds */ 370 struct { 371 /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */ 372 #define BO_VALID 0x8000 /* is current addr in cmdstream correct/valid? */ 373 #define BO_LOCKED 0x4000 /* obj lock is held */ 374 #define BO_ACTIVE 0x2000 /* active refcnt is held */ 375 #define BO_PINNED 0x1000 /* obj is pinned and on active list */ 376 uint32_t flags; 377 union { 378 struct msm_gem_object *obj; 379 uint32_t handle; 380 }; 381 uint64_t iova; 382 struct msm_gem_vma *vma; 383 } bos[]; 384 }; 385 386 static inline struct msm_gem_submit *to_msm_submit(struct drm_sched_job *job) 387 { 388 return container_of(job, struct msm_gem_submit, base); 389 } 390 391 void __msm_gem_submit_destroy(struct kref *kref); 392 393 static inline void msm_gem_submit_get(struct msm_gem_submit *submit) 394 { 395 kref_get(&submit->ref); 396 } 397 398 static inline void msm_gem_submit_put(struct msm_gem_submit *submit) 399 { 400 kref_put(&submit->ref, __msm_gem_submit_destroy); 401 } 402 403 void msm_submit_retire(struct msm_gem_submit *submit); 404 405 /* helper to determine of a buffer in submit should be dumped, used for both 406 * devcoredump and debugfs cmdstream dumping: 407 */ 408 static inline bool 409 should_dump(struct msm_gem_submit *submit, int idx) 410 { 411 extern bool rd_full; 412 return rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP); 413 } 414 415 #endif /* __MSM_GEM_H__ */ 416