1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2013 Red Hat
10 #include <linux/kref.h>
11 #include <linux/dma-resv.h>
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 */
18 struct msm_gem_address_space {
20 /* NOTE: mm managed at the page level, size is in # of pages
21 * and position mm_node->start is in # of pages:
24 spinlock_t lock; /* Protects drm_mm node allocation/removal */
28 /* For address spaces associated with a specific process, this
35 struct drm_mm_node node;
37 struct msm_gem_address_space *aspace;
38 struct list_head list; /* node in msm_gem_object::vmas */
43 struct msm_gem_object {
44 struct drm_gem_object base;
49 * Advice: are the backing pages purgeable?
54 * count of active vmap'ing
58 /* And object is either:
59 * inactive - on priv->inactive_list
60 * active - on one one of the gpu's active_list.. well, at
61 * least for now we don't have (I don't think) hw sync between
62 * 2d and 3d one devices which have both, meaning we need to
63 * block on submit if a bo is already on other ring
66 struct list_head mm_list;
67 struct msm_gpu *gpu; /* non-null if active */
69 /* Transiently in the process of submit ioctl, objects associated
70 * with the submit are on submit->bo_list.. this only lasts for
71 * the duration of the ioctl, so one bo can never be on multiple
74 struct list_head submit_entry;
80 struct list_head vmas; /* list of msm_gem_vma */
82 struct llist_node freed;
84 /* For physically contiguous buffers. Used when we don't have
85 * an IOMMU. Also used for stolen/splashscreen buffer.
87 struct drm_mm_node *vram_node;
88 struct mutex lock; /* Protects resources associated with bo */
90 char name[32]; /* Identifier to print for the debugfs files */
92 atomic_t active_count;
94 #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
96 static inline bool is_active(struct msm_gem_object *msm_obj)
98 return atomic_read(&msm_obj->active_count);
101 static inline bool is_purgeable(struct msm_gem_object *msm_obj)
103 WARN_ON(!mutex_is_locked(&msm_obj->base.dev->struct_mutex));
104 return (msm_obj->madv == MSM_MADV_DONTNEED) && msm_obj->sgt &&
105 !msm_obj->base.dma_buf && !msm_obj->base.import_attach;
108 static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
110 return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
113 /* The shrinker can be triggered while we hold objA->lock, and need
114 * to grab objB->lock to purge it. Lockdep just sees these as a single
115 * class of lock, so we use subclasses to teach it the difference.
117 * OBJ_LOCK_NORMAL is implicit (ie. normal mutex_lock() call), and
118 * OBJ_LOCK_SHRINKER is used by shrinker.
120 * It is *essential* that we never go down paths that could trigger the
121 * shrinker for a purgable object. This is ensured by checking that
122 * msm_obj->madv == MSM_MADV_WILLNEED.
129 void msm_gem_purge(struct drm_gem_object *obj, enum msm_gem_lock subclass);
130 void msm_gem_vunmap(struct drm_gem_object *obj, enum msm_gem_lock subclass);
131 void msm_gem_free_work(struct work_struct *work);
133 /* Created per submit-ioctl, to track bo's and cmdstream bufs, etc,
134 * associated with the cmdstream submission for synchronization (and
135 * make it easier to unwind when things go wrong, etc). This only
136 * lasts for the duration of the submit-ioctl.
138 struct msm_gem_submit {
139 struct drm_device *dev;
141 struct msm_gem_address_space *aspace;
142 struct list_head node; /* node in ring submit list */
143 struct list_head bo_list;
144 struct ww_acquire_ctx ticket;
145 uint32_t seqno; /* Sequence number of the submit on the ring */
146 struct dma_fence *fence;
147 struct msm_gpu_submitqueue *queue;
148 struct pid *pid; /* submitting process */
149 bool valid; /* true if no cmdstream patching needed */
150 bool in_rb; /* "sudo" mode, copy cmds into RB */
151 struct msm_ringbuffer *ring;
152 struct msm_file_private *ctx;
153 unsigned int nr_cmds;
155 u32 ident; /* A "identifier" for the submit for logging */
158 uint32_t size; /* in dwords */
160 uint32_t idx; /* cmdstream buffer idx in bos[] */
161 } *cmd; /* array of size nr_cmds */
165 struct msm_gem_object *obj;
172 /* helper to determine of a buffer in submit should be dumped, used for both
173 * devcoredump and debugfs cmdstream dumping:
176 should_dump(struct msm_gem_submit *submit, int idx)
179 return rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP);
182 #endif /* __MSM_GEM_H__ */