xref: /openbmc/linux/drivers/gpu/drm/msm/msm_gem.h (revision afba8b0a)
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 	 * count of active vmap'ing
55 	 */
56 	uint8_t vmap_count;
57 
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
64 	 *
65 	 */
66 	struct list_head mm_list;
67 	struct msm_gpu *gpu;     /* non-null if active */
68 
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
72 	 * submit lists.
73 	 */
74 	struct list_head submit_entry;
75 
76 	struct page **pages;
77 	struct sg_table *sgt;
78 	void *vaddr;
79 
80 	struct list_head vmas;    /* list of msm_gem_vma */
81 
82 	struct llist_node freed;
83 
84 	/* For physically contiguous buffers.  Used when we don't have
85 	 * an IOMMU.  Also used for stolen/splashscreen buffer.
86 	 */
87 	struct drm_mm_node *vram_node;
88 	struct mutex lock; /* Protects resources associated with bo */
89 
90 	char name[32]; /* Identifier to print for the debugfs files */
91 
92 	atomic_t active_count;
93 };
94 #define to_msm_bo(x) container_of(x, struct msm_gem_object, base)
95 
96 static inline bool is_active(struct msm_gem_object *msm_obj)
97 {
98 	return atomic_read(&msm_obj->active_count);
99 }
100 
101 static inline bool is_purgeable(struct msm_gem_object *msm_obj)
102 {
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;
106 }
107 
108 static inline bool is_vunmapable(struct msm_gem_object *msm_obj)
109 {
110 	return (msm_obj->vmap_count == 0) && msm_obj->vaddr;
111 }
112 
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.
116  *
117  * OBJ_LOCK_NORMAL is implicit (ie. normal mutex_lock() call), and
118  * OBJ_LOCK_SHRINKER is used by shrinker.
119  *
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.
123  */
124 enum msm_gem_lock {
125 	OBJ_LOCK_NORMAL,
126 	OBJ_LOCK_SHRINKER,
127 };
128 
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);
132 
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.
137  */
138 struct msm_gem_submit {
139 	struct drm_device *dev;
140 	struct msm_gpu *gpu;
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;
154 	unsigned int nr_bos;
155 	u32 ident;	   /* A "identifier" for the submit for logging */
156 	struct {
157 		uint32_t type;
158 		uint32_t size;  /* in dwords */
159 		uint64_t iova;
160 		uint32_t idx;   /* cmdstream buffer idx in bos[] */
161 	} *cmd;  /* array of size nr_cmds */
162 	struct {
163 		uint32_t flags;
164 		union {
165 			struct msm_gem_object *obj;
166 			uint32_t handle;
167 		};
168 		uint64_t iova;
169 	} bos[];
170 };
171 
172 /* helper to determine of a buffer in submit should be dumped, used for both
173  * devcoredump and debugfs cmdstream dumping:
174  */
175 static inline bool
176 should_dump(struct msm_gem_submit *submit, int idx)
177 {
178 	extern bool rd_full;
179 	return rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP);
180 }
181 
182 #endif /* __MSM_GEM_H__ */
183