• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "msm_drv.h"
19 #include "msm_gpu.h"
20 #include "msm_gem.h"
21 
22 /*
23  * Cmdstream submission:
24  */
25 
26 /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */
27 #define BO_VALID    0x8000
28 #define BO_LOCKED   0x4000
29 #define BO_PINNED   0x2000
30 
submit_create(struct drm_device * dev,struct msm_gpu * gpu,uint32_t nr)31 static struct msm_gem_submit *submit_create(struct drm_device *dev,
32 		struct msm_gpu *gpu, uint32_t nr)
33 {
34 	struct msm_gem_submit *submit;
35 	uint64_t sz = sizeof(*submit) + ((u64)nr * sizeof(submit->bos[0]));
36 
37 	if (sz > SIZE_MAX)
38 		return NULL;
39 
40 	submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
41 	if (submit) {
42 		submit->dev = dev;
43 		submit->gpu = gpu;
44 
45 		/* initially, until copy_from_user() and bo lookup succeeds: */
46 		submit->nr_bos = 0;
47 		submit->nr_cmds = 0;
48 
49 		INIT_LIST_HEAD(&submit->bo_list);
50 		ww_acquire_init(&submit->ticket, &reservation_ww_class);
51 	}
52 
53 	return submit;
54 }
55 
56 static inline unsigned long __must_check
copy_from_user_inatomic(void * to,const void __user * from,unsigned long n)57 copy_from_user_inatomic(void *to, const void __user *from, unsigned long n)
58 {
59 	if (access_ok(VERIFY_READ, from, n))
60 		return __copy_from_user_inatomic(to, from, n);
61 	return -EFAULT;
62 }
63 
submit_lookup_objects(struct msm_gem_submit * submit,struct drm_msm_gem_submit * args,struct drm_file * file)64 static int submit_lookup_objects(struct msm_gem_submit *submit,
65 		struct drm_msm_gem_submit *args, struct drm_file *file)
66 {
67 	unsigned i;
68 	int ret = 0;
69 
70 	spin_lock(&file->table_lock);
71 	pagefault_disable();
72 
73 	for (i = 0; i < args->nr_bos; i++) {
74 		struct drm_msm_gem_submit_bo submit_bo;
75 		struct drm_gem_object *obj;
76 		struct msm_gem_object *msm_obj;
77 		void __user *userptr =
78 			u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
79 
80 		ret = copy_from_user_inatomic(&submit_bo, userptr, sizeof(submit_bo));
81 		if (unlikely(ret)) {
82 			pagefault_enable();
83 			spin_unlock(&file->table_lock);
84 			ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
85 			if (ret)
86 				goto out;
87 			spin_lock(&file->table_lock);
88 			pagefault_disable();
89 		}
90 
91 		if ((submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) ||
92 			!(submit_bo.flags & MSM_SUBMIT_BO_FLAGS)) {
93 			DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
94 			ret = -EINVAL;
95 			goto out_unlock;
96 		}
97 
98 		submit->bos[i].flags = submit_bo.flags;
99 		/* in validate_objects() we figure out if this is true: */
100 		submit->bos[i].iova  = submit_bo.presumed;
101 
102 		/* normally use drm_gem_object_lookup(), but for bulk lookup
103 		 * all under single table_lock just hit object_idr directly:
104 		 */
105 		obj = idr_find(&file->object_idr, submit_bo.handle);
106 		if (!obj) {
107 			DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
108 			ret = -EINVAL;
109 			goto out_unlock;
110 		}
111 
112 		msm_obj = to_msm_bo(obj);
113 
114 		if (!list_empty(&msm_obj->submit_entry)) {
115 			DRM_ERROR("handle %u at index %u already on submit list\n",
116 					submit_bo.handle, i);
117 			ret = -EINVAL;
118 			goto out_unlock;
119 		}
120 
121 		drm_gem_object_reference(obj);
122 
123 		submit->bos[i].obj = msm_obj;
124 
125 		list_add_tail(&msm_obj->submit_entry, &submit->bo_list);
126 	}
127 
128 out_unlock:
129 	pagefault_enable();
130 	spin_unlock(&file->table_lock);
131 
132 out:
133 	submit->nr_bos = i;
134 
135 	return ret;
136 }
137 
submit_unlock_unpin_bo(struct msm_gem_submit * submit,int i)138 static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i)
139 {
140 	struct msm_gem_object *msm_obj = submit->bos[i].obj;
141 
142 	if (submit->bos[i].flags & BO_PINNED)
143 		msm_gem_put_iova(&msm_obj->base, submit->gpu->id);
144 
145 	if (submit->bos[i].flags & BO_LOCKED)
146 		ww_mutex_unlock(&msm_obj->resv->lock);
147 
148 	if (!(submit->bos[i].flags & BO_VALID))
149 		submit->bos[i].iova = 0;
150 
151 	submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED);
152 }
153 
154 /* This is where we make sure all the bo's are reserved and pin'd: */
submit_validate_objects(struct msm_gem_submit * submit)155 static int submit_validate_objects(struct msm_gem_submit *submit)
156 {
157 	int contended, slow_locked = -1, i, ret = 0;
158 
159 retry:
160 	submit->valid = true;
161 
162 	for (i = 0; i < submit->nr_bos; i++) {
163 		struct msm_gem_object *msm_obj = submit->bos[i].obj;
164 		uint32_t iova;
165 
166 		if (slow_locked == i)
167 			slow_locked = -1;
168 
169 		contended = i;
170 
171 		if (!(submit->bos[i].flags & BO_LOCKED)) {
172 			ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock,
173 					&submit->ticket);
174 			if (ret)
175 				goto fail;
176 			submit->bos[i].flags |= BO_LOCKED;
177 		}
178 
179 
180 		/* if locking succeeded, pin bo: */
181 		ret = msm_gem_get_iova_locked(&msm_obj->base,
182 				submit->gpu->id, &iova);
183 
184 		/* this would break the logic in the fail path.. there is no
185 		 * reason for this to happen, but just to be on the safe side
186 		 * let's notice if this starts happening in the future:
187 		 */
188 		WARN_ON(ret == -EDEADLK);
189 
190 		if (ret)
191 			goto fail;
192 
193 		submit->bos[i].flags |= BO_PINNED;
194 
195 		if (iova == submit->bos[i].iova) {
196 			submit->bos[i].flags |= BO_VALID;
197 		} else {
198 			submit->bos[i].iova = iova;
199 			submit->bos[i].flags &= ~BO_VALID;
200 			submit->valid = false;
201 		}
202 	}
203 
204 	ww_acquire_done(&submit->ticket);
205 
206 	return 0;
207 
208 fail:
209 	for (; i >= 0; i--)
210 		submit_unlock_unpin_bo(submit, i);
211 
212 	if (slow_locked > 0)
213 		submit_unlock_unpin_bo(submit, slow_locked);
214 
215 	if (ret == -EDEADLK) {
216 		struct msm_gem_object *msm_obj = submit->bos[contended].obj;
217 		/* we lost out in a seqno race, lock and retry.. */
218 		ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock,
219 				&submit->ticket);
220 		if (!ret) {
221 			submit->bos[contended].flags |= BO_LOCKED;
222 			slow_locked = contended;
223 			goto retry;
224 		}
225 	}
226 
227 	return ret;
228 }
229 
submit_bo(struct msm_gem_submit * submit,uint32_t idx,struct msm_gem_object ** obj,uint32_t * iova,bool * valid)230 static int submit_bo(struct msm_gem_submit *submit, uint32_t idx,
231 		struct msm_gem_object **obj, uint32_t *iova, bool *valid)
232 {
233 	if (idx >= submit->nr_bos) {
234 		DRM_ERROR("invalid buffer index: %u (out of %u)\n",
235 				idx, submit->nr_bos);
236 		return -EINVAL;
237 	}
238 
239 	if (obj)
240 		*obj = submit->bos[idx].obj;
241 	if (iova)
242 		*iova = submit->bos[idx].iova;
243 	if (valid)
244 		*valid = !!(submit->bos[idx].flags & BO_VALID);
245 
246 	return 0;
247 }
248 
249 /* process the reloc's and patch up the cmdstream as needed: */
submit_reloc(struct msm_gem_submit * submit,struct msm_gem_object * obj,uint32_t offset,uint32_t nr_relocs,uint64_t relocs)250 static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj,
251 		uint32_t offset, uint32_t nr_relocs, uint64_t relocs)
252 {
253 	uint32_t i, last_offset = 0;
254 	uint32_t *ptr;
255 	int ret;
256 
257 	if (offset % 4) {
258 		DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
259 		return -EINVAL;
260 	}
261 
262 	/* For now, just map the entire thing.  Eventually we probably
263 	 * to do it page-by-page, w/ kmap() if not vmap()d..
264 	 */
265 	ptr = msm_gem_vaddr_locked(&obj->base);
266 
267 	if (IS_ERR(ptr)) {
268 		ret = PTR_ERR(ptr);
269 		DBG("failed to map: %d", ret);
270 		return ret;
271 	}
272 
273 	for (i = 0; i < nr_relocs; i++) {
274 		struct drm_msm_gem_submit_reloc submit_reloc;
275 		void __user *userptr =
276 			u64_to_user_ptr(relocs + (i * sizeof(submit_reloc)));
277 		uint32_t iova, off;
278 		bool valid;
279 
280 		ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc));
281 		if (ret)
282 			return -EFAULT;
283 
284 		if (submit_reloc.submit_offset % 4) {
285 			DRM_ERROR("non-aligned reloc offset: %u\n",
286 					submit_reloc.submit_offset);
287 			return -EINVAL;
288 		}
289 
290 		/* offset in dwords: */
291 		off = submit_reloc.submit_offset / 4;
292 
293 		if ((off >= (obj->base.size / 4)) ||
294 				(off < last_offset)) {
295 			DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
296 			return -EINVAL;
297 		}
298 
299 		ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid);
300 		if (ret)
301 			return ret;
302 
303 		if (valid)
304 			continue;
305 
306 		iova += submit_reloc.reloc_offset;
307 
308 		if (submit_reloc.shift < 0)
309 			iova >>= -submit_reloc.shift;
310 		else
311 			iova <<= submit_reloc.shift;
312 
313 		ptr[off] = iova | submit_reloc.or;
314 
315 		last_offset = off;
316 	}
317 
318 	return 0;
319 }
320 
submit_cleanup(struct msm_gem_submit * submit,bool fail)321 static void submit_cleanup(struct msm_gem_submit *submit, bool fail)
322 {
323 	unsigned i;
324 
325 	for (i = 0; i < submit->nr_bos; i++) {
326 		struct msm_gem_object *msm_obj = submit->bos[i].obj;
327 		submit_unlock_unpin_bo(submit, i);
328 		list_del_init(&msm_obj->submit_entry);
329 		drm_gem_object_unreference(&msm_obj->base);
330 	}
331 
332 	ww_acquire_fini(&submit->ticket);
333 }
334 
msm_ioctl_gem_submit(struct drm_device * dev,void * data,struct drm_file * file)335 int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
336 		struct drm_file *file)
337 {
338 	struct msm_drm_private *priv = dev->dev_private;
339 	struct drm_msm_gem_submit *args = data;
340 	struct msm_file_private *ctx = file->driver_priv;
341 	struct msm_gem_submit *submit;
342 	struct msm_gpu *gpu;
343 	unsigned i;
344 	int ret;
345 
346 	/* for now, we just have 3d pipe.. eventually this would need to
347 	 * be more clever to dispatch to appropriate gpu module:
348 	 */
349 	if (args->pipe != MSM_PIPE_3D0)
350 		return -EINVAL;
351 
352 	gpu = priv->gpu;
353 
354 	if (args->nr_cmds > MAX_CMDS)
355 		return -EINVAL;
356 
357 	mutex_lock(&dev->struct_mutex);
358 
359 	submit = submit_create(dev, gpu, args->nr_bos);
360 	if (!submit) {
361 		ret = -ENOMEM;
362 		goto out;
363 	}
364 
365 	ret = submit_lookup_objects(submit, args, file);
366 	if (ret)
367 		goto out;
368 
369 	ret = submit_validate_objects(submit);
370 	if (ret)
371 		goto out;
372 
373 	for (i = 0; i < args->nr_cmds; i++) {
374 		struct drm_msm_gem_submit_cmd submit_cmd;
375 		void __user *userptr =
376 			u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
377 		struct msm_gem_object *msm_obj;
378 		uint32_t iova;
379 
380 		ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd));
381 		if (ret) {
382 			ret = -EFAULT;
383 			goto out;
384 		}
385 
386 		/* validate input from userspace: */
387 		switch (submit_cmd.type) {
388 		case MSM_SUBMIT_CMD_BUF:
389 		case MSM_SUBMIT_CMD_IB_TARGET_BUF:
390 		case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
391 			break;
392 		default:
393 			DRM_ERROR("invalid type: %08x\n", submit_cmd.type);
394 			ret = -EINVAL;
395 			goto out;
396 		}
397 
398 		ret = submit_bo(submit, submit_cmd.submit_idx,
399 				&msm_obj, &iova, NULL);
400 		if (ret)
401 			goto out;
402 
403 		if (submit_cmd.size % 4) {
404 			DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
405 					submit_cmd.size);
406 			ret = -EINVAL;
407 			goto out;
408 		}
409 
410 		if ((submit_cmd.size + submit_cmd.submit_offset) >=
411 				msm_obj->base.size) {
412 			DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size);
413 			ret = -EINVAL;
414 			goto out;
415 		}
416 
417 		submit->cmd[i].type = submit_cmd.type;
418 		submit->cmd[i].size = submit_cmd.size / 4;
419 		submit->cmd[i].iova = iova + submit_cmd.submit_offset;
420 		submit->cmd[i].idx  = submit_cmd.submit_idx;
421 
422 		if (submit->valid)
423 			continue;
424 
425 		ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset,
426 				submit_cmd.nr_relocs, submit_cmd.relocs);
427 		if (ret)
428 			goto out;
429 	}
430 
431 	submit->nr_cmds = i;
432 
433 	ret = msm_gpu_submit(gpu, submit, ctx);
434 
435 	args->fence = submit->fence;
436 
437 out:
438 	if (submit)
439 		submit_cleanup(submit, !!ret);
440 	mutex_unlock(&dev->struct_mutex);
441 	return ret;
442 }
443