Commit | Line | Data |
---|---|---|
7198e6b0 RC |
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 | ||
7198e6b0 | 26 | /* make sure these don't conflict w/ MSM_SUBMIT_BO_x */ |
340faef2 | 27 | #define BO_VALID 0x8000 /* is current addr in cmdstream correct/valid? */ |
7198e6b0 RC |
28 | #define BO_LOCKED 0x4000 |
29 | #define BO_PINNED 0x2000 | |
30 | ||
7198e6b0 | 31 | static struct msm_gem_submit *submit_create(struct drm_device *dev, |
6b597ce2 | 32 | struct msm_gpu *gpu, int nr_bos, int nr_cmds) |
7198e6b0 RC |
33 | { |
34 | struct msm_gem_submit *submit; | |
6b597ce2 RC |
35 | int sz = sizeof(*submit) + (nr_bos * sizeof(submit->bos[0])) + |
36 | (nr_cmds * sizeof(*submit->cmd)); | |
7198e6b0 RC |
37 | |
38 | submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); | |
6860b56c RC |
39 | if (!submit) |
40 | return NULL; | |
7198e6b0 | 41 | |
6860b56c RC |
42 | submit->dev = dev; |
43 | submit->gpu = gpu; | |
ba344afd | 44 | submit->fence = NULL; |
4816b626 | 45 | submit->pid = get_pid(task_pid(current)); |
6b597ce2 | 46 | submit->cmd = (void *)&submit->bos[nr_bos]; |
7198e6b0 | 47 | |
6860b56c RC |
48 | /* initially, until copy_from_user() and bo lookup succeeds: */ |
49 | submit->nr_bos = 0; | |
50 | submit->nr_cmds = 0; | |
51 | ||
ba344afd | 52 | INIT_LIST_HEAD(&submit->node); |
6860b56c RC |
53 | INIT_LIST_HEAD(&submit->bo_list); |
54 | ww_acquire_init(&submit->ticket, &reservation_ww_class); | |
7198e6b0 RC |
55 | |
56 | return submit; | |
57 | } | |
58 | ||
40e6815b RC |
59 | void msm_gem_submit_free(struct msm_gem_submit *submit) |
60 | { | |
61 | fence_put(submit->fence); | |
62 | list_del(&submit->node); | |
4816b626 | 63 | put_pid(submit->pid); |
40e6815b RC |
64 | kfree(submit); |
65 | } | |
66 | ||
7198e6b0 RC |
67 | static int submit_lookup_objects(struct msm_gem_submit *submit, |
68 | struct drm_msm_gem_submit *args, struct drm_file *file) | |
69 | { | |
70 | unsigned i; | |
71 | int ret = 0; | |
72 | ||
73 | spin_lock(&file->table_lock); | |
74 | ||
75 | for (i = 0; i < args->nr_bos; i++) { | |
76 | struct drm_msm_gem_submit_bo submit_bo; | |
77 | struct drm_gem_object *obj; | |
78 | struct msm_gem_object *msm_obj; | |
79 | void __user *userptr = | |
3ed605bc | 80 | u64_to_user_ptr(args->bos + (i * sizeof(submit_bo))); |
7198e6b0 | 81 | |
a9e26cab RC |
82 | /* make sure we don't have garbage flags, in case we hit |
83 | * error path before flags is initialized: | |
84 | */ | |
85 | submit->bos[i].flags = 0; | |
86 | ||
7198e6b0 RC |
87 | ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo)); |
88 | if (ret) { | |
89 | ret = -EFAULT; | |
90 | goto out_unlock; | |
91 | } | |
92 | ||
93ddb0d3 | 93 | if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) { |
19872533 | 94 | DRM_ERROR("invalid flags: %x\n", submit_bo.flags); |
7198e6b0 RC |
95 | ret = -EINVAL; |
96 | goto out_unlock; | |
97 | } | |
98 | ||
99 | submit->bos[i].flags = submit_bo.flags; | |
100 | /* in validate_objects() we figure out if this is true: */ | |
101 | submit->bos[i].iova = submit_bo.presumed; | |
102 | ||
103 | /* normally use drm_gem_object_lookup(), but for bulk lookup | |
104 | * all under single table_lock just hit object_idr directly: | |
105 | */ | |
106 | obj = idr_find(&file->object_idr, submit_bo.handle); | |
107 | if (!obj) { | |
19872533 | 108 | DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i); |
7198e6b0 RC |
109 | ret = -EINVAL; |
110 | goto out_unlock; | |
111 | } | |
112 | ||
113 | msm_obj = to_msm_bo(obj); | |
114 | ||
115 | if (!list_empty(&msm_obj->submit_entry)) { | |
19872533 | 116 | DRM_ERROR("handle %u at index %u already on submit list\n", |
7198e6b0 RC |
117 | submit_bo.handle, i); |
118 | ret = -EINVAL; | |
119 | goto out_unlock; | |
120 | } | |
121 | ||
122 | drm_gem_object_reference(obj); | |
123 | ||
124 | submit->bos[i].obj = msm_obj; | |
125 | ||
126 | list_add_tail(&msm_obj->submit_entry, &submit->bo_list); | |
127 | } | |
128 | ||
129 | out_unlock: | |
130 | submit->nr_bos = i; | |
131 | spin_unlock(&file->table_lock); | |
132 | ||
133 | return ret; | |
134 | } | |
135 | ||
136 | static void submit_unlock_unpin_bo(struct msm_gem_submit *submit, int i) | |
137 | { | |
138 | struct msm_gem_object *msm_obj = submit->bos[i].obj; | |
139 | ||
140 | if (submit->bos[i].flags & BO_PINNED) | |
141 | msm_gem_put_iova(&msm_obj->base, submit->gpu->id); | |
142 | ||
143 | if (submit->bos[i].flags & BO_LOCKED) | |
144 | ww_mutex_unlock(&msm_obj->resv->lock); | |
145 | ||
146 | if (!(submit->bos[i].flags & BO_VALID)) | |
147 | submit->bos[i].iova = 0; | |
148 | ||
149 | submit->bos[i].flags &= ~(BO_LOCKED | BO_PINNED); | |
150 | } | |
151 | ||
152 | /* This is where we make sure all the bo's are reserved and pin'd: */ | |
340faef2 | 153 | static int submit_lock_objects(struct msm_gem_submit *submit) |
7198e6b0 RC |
154 | { |
155 | int contended, slow_locked = -1, i, ret = 0; | |
156 | ||
157 | retry: | |
7198e6b0 RC |
158 | for (i = 0; i < submit->nr_bos; i++) { |
159 | struct msm_gem_object *msm_obj = submit->bos[i].obj; | |
7198e6b0 RC |
160 | |
161 | if (slow_locked == i) | |
162 | slow_locked = -1; | |
163 | ||
164 | contended = i; | |
165 | ||
166 | if (!(submit->bos[i].flags & BO_LOCKED)) { | |
167 | ret = ww_mutex_lock_interruptible(&msm_obj->resv->lock, | |
168 | &submit->ticket); | |
169 | if (ret) | |
170 | goto fail; | |
171 | submit->bos[i].flags |= BO_LOCKED; | |
172 | } | |
7198e6b0 RC |
173 | } |
174 | ||
175 | ww_acquire_done(&submit->ticket); | |
176 | ||
177 | return 0; | |
178 | ||
179 | fail: | |
180 | for (; i >= 0; i--) | |
181 | submit_unlock_unpin_bo(submit, i); | |
182 | ||
183 | if (slow_locked > 0) | |
184 | submit_unlock_unpin_bo(submit, slow_locked); | |
185 | ||
186 | if (ret == -EDEADLK) { | |
187 | struct msm_gem_object *msm_obj = submit->bos[contended].obj; | |
188 | /* we lost out in a seqno race, lock and retry.. */ | |
189 | ret = ww_mutex_lock_slow_interruptible(&msm_obj->resv->lock, | |
190 | &submit->ticket); | |
191 | if (!ret) { | |
192 | submit->bos[contended].flags |= BO_LOCKED; | |
193 | slow_locked = contended; | |
194 | goto retry; | |
195 | } | |
196 | } | |
197 | ||
198 | return ret; | |
199 | } | |
200 | ||
b6295f9a RC |
201 | static int submit_fence_sync(struct msm_gem_submit *submit) |
202 | { | |
203 | int i, ret = 0; | |
204 | ||
205 | for (i = 0; i < submit->nr_bos; i++) { | |
206 | struct msm_gem_object *msm_obj = submit->bos[i].obj; | |
207 | bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE; | |
208 | ||
209 | ret = msm_gem_sync_object(&msm_obj->base, submit->gpu->fctx, write); | |
210 | if (ret) | |
211 | break; | |
212 | } | |
213 | ||
214 | return ret; | |
215 | } | |
216 | ||
340faef2 RC |
217 | static int submit_pin_objects(struct msm_gem_submit *submit) |
218 | { | |
219 | int i, ret = 0; | |
220 | ||
221 | submit->valid = true; | |
222 | ||
223 | for (i = 0; i < submit->nr_bos; i++) { | |
224 | struct msm_gem_object *msm_obj = submit->bos[i].obj; | |
225 | uint32_t iova; | |
226 | ||
227 | /* if locking succeeded, pin bo: */ | |
228 | ret = msm_gem_get_iova_locked(&msm_obj->base, | |
229 | submit->gpu->id, &iova); | |
230 | ||
231 | if (ret) | |
232 | break; | |
233 | ||
234 | submit->bos[i].flags |= BO_PINNED; | |
235 | ||
236 | if (iova == submit->bos[i].iova) { | |
237 | submit->bos[i].flags |= BO_VALID; | |
238 | } else { | |
239 | submit->bos[i].iova = iova; | |
240 | /* iova changed, so address in cmdstream is not valid: */ | |
241 | submit->bos[i].flags &= ~BO_VALID; | |
242 | submit->valid = false; | |
243 | } | |
244 | } | |
245 | ||
246 | return ret; | |
247 | } | |
248 | ||
7198e6b0 RC |
249 | static int submit_bo(struct msm_gem_submit *submit, uint32_t idx, |
250 | struct msm_gem_object **obj, uint32_t *iova, bool *valid) | |
251 | { | |
252 | if (idx >= submit->nr_bos) { | |
19872533 RC |
253 | DRM_ERROR("invalid buffer index: %u (out of %u)\n", |
254 | idx, submit->nr_bos); | |
255 | return -EINVAL; | |
7198e6b0 RC |
256 | } |
257 | ||
258 | if (obj) | |
259 | *obj = submit->bos[idx].obj; | |
260 | if (iova) | |
261 | *iova = submit->bos[idx].iova; | |
262 | if (valid) | |
263 | *valid = !!(submit->bos[idx].flags & BO_VALID); | |
264 | ||
265 | return 0; | |
266 | } | |
267 | ||
268 | /* process the reloc's and patch up the cmdstream as needed: */ | |
269 | static int submit_reloc(struct msm_gem_submit *submit, struct msm_gem_object *obj, | |
270 | uint32_t offset, uint32_t nr_relocs, uint64_t relocs) | |
271 | { | |
272 | uint32_t i, last_offset = 0; | |
273 | uint32_t *ptr; | |
274 | int ret; | |
275 | ||
276 | if (offset % 4) { | |
19872533 | 277 | DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset); |
7198e6b0 RC |
278 | return -EINVAL; |
279 | } | |
280 | ||
281 | /* For now, just map the entire thing. Eventually we probably | |
282 | * to do it page-by-page, w/ kmap() if not vmap()d.. | |
283 | */ | |
18f23049 | 284 | ptr = msm_gem_get_vaddr_locked(&obj->base); |
7198e6b0 RC |
285 | |
286 | if (IS_ERR(ptr)) { | |
287 | ret = PTR_ERR(ptr); | |
288 | DBG("failed to map: %d", ret); | |
289 | return ret; | |
290 | } | |
291 | ||
292 | for (i = 0; i < nr_relocs; i++) { | |
293 | struct drm_msm_gem_submit_reloc submit_reloc; | |
294 | void __user *userptr = | |
3ed605bc | 295 | u64_to_user_ptr(relocs + (i * sizeof(submit_reloc))); |
7198e6b0 RC |
296 | uint32_t iova, off; |
297 | bool valid; | |
298 | ||
299 | ret = copy_from_user(&submit_reloc, userptr, sizeof(submit_reloc)); | |
300 | if (ret) | |
301 | return -EFAULT; | |
302 | ||
303 | if (submit_reloc.submit_offset % 4) { | |
19872533 | 304 | DRM_ERROR("non-aligned reloc offset: %u\n", |
7198e6b0 RC |
305 | submit_reloc.submit_offset); |
306 | return -EINVAL; | |
307 | } | |
308 | ||
309 | /* offset in dwords: */ | |
310 | off = submit_reloc.submit_offset / 4; | |
311 | ||
312 | if ((off >= (obj->base.size / 4)) || | |
313 | (off < last_offset)) { | |
19872533 | 314 | DRM_ERROR("invalid offset %u at reloc %u\n", off, i); |
7198e6b0 RC |
315 | return -EINVAL; |
316 | } | |
317 | ||
318 | ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova, &valid); | |
319 | if (ret) | |
320 | return ret; | |
321 | ||
322 | if (valid) | |
323 | continue; | |
324 | ||
325 | iova += submit_reloc.reloc_offset; | |
326 | ||
327 | if (submit_reloc.shift < 0) | |
328 | iova >>= -submit_reloc.shift; | |
329 | else | |
330 | iova <<= submit_reloc.shift; | |
331 | ||
332 | ptr[off] = iova | submit_reloc.or; | |
333 | ||
334 | last_offset = off; | |
335 | } | |
336 | ||
18f23049 RC |
337 | msm_gem_put_vaddr_locked(&obj->base); |
338 | ||
7198e6b0 RC |
339 | return 0; |
340 | } | |
341 | ||
40e6815b | 342 | static void submit_cleanup(struct msm_gem_submit *submit) |
7198e6b0 RC |
343 | { |
344 | unsigned i; | |
345 | ||
7198e6b0 RC |
346 | for (i = 0; i < submit->nr_bos; i++) { |
347 | struct msm_gem_object *msm_obj = submit->bos[i].obj; | |
348 | submit_unlock_unpin_bo(submit, i); | |
349 | list_del_init(&msm_obj->submit_entry); | |
350 | drm_gem_object_unreference(&msm_obj->base); | |
351 | } | |
7198e6b0 RC |
352 | |
353 | ww_acquire_fini(&submit->ticket); | |
7198e6b0 RC |
354 | } |
355 | ||
356 | int msm_ioctl_gem_submit(struct drm_device *dev, void *data, | |
357 | struct drm_file *file) | |
358 | { | |
359 | struct msm_drm_private *priv = dev->dev_private; | |
360 | struct drm_msm_gem_submit *args = data; | |
361 | struct msm_file_private *ctx = file->driver_priv; | |
362 | struct msm_gem_submit *submit; | |
c01a958e | 363 | struct msm_gpu *gpu = priv->gpu; |
7198e6b0 RC |
364 | unsigned i; |
365 | int ret; | |
366 | ||
c01a958e RC |
367 | if (!gpu) |
368 | return -ENXIO; | |
369 | ||
7198e6b0 RC |
370 | /* for now, we just have 3d pipe.. eventually this would need to |
371 | * be more clever to dispatch to appropriate gpu module: | |
372 | */ | |
373 | if (args->pipe != MSM_PIPE_3D0) | |
374 | return -EINVAL; | |
375 | ||
b5b4c264 RC |
376 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
377 | if (ret) | |
378 | return ret; | |
687f084a | 379 | |
6b597ce2 | 380 | submit = submit_create(dev, gpu, args->nr_bos, args->nr_cmds); |
b5b4c264 RC |
381 | if (!submit) { |
382 | ret = -ENOMEM; | |
383 | goto out_unlock; | |
384 | } | |
7198e6b0 RC |
385 | |
386 | ret = submit_lookup_objects(submit, args, file); | |
387 | if (ret) | |
388 | goto out; | |
389 | ||
340faef2 RC |
390 | ret = submit_lock_objects(submit); |
391 | if (ret) | |
392 | goto out; | |
393 | ||
b6295f9a RC |
394 | ret = submit_fence_sync(submit); |
395 | if (ret) | |
396 | goto out; | |
397 | ||
340faef2 | 398 | ret = submit_pin_objects(submit); |
7198e6b0 RC |
399 | if (ret) |
400 | goto out; | |
401 | ||
402 | for (i = 0; i < args->nr_cmds; i++) { | |
403 | struct drm_msm_gem_submit_cmd submit_cmd; | |
404 | void __user *userptr = | |
3ed605bc | 405 | u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd))); |
7198e6b0 RC |
406 | struct msm_gem_object *msm_obj; |
407 | uint32_t iova; | |
408 | ||
409 | ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd)); | |
410 | if (ret) { | |
411 | ret = -EFAULT; | |
412 | goto out; | |
413 | } | |
414 | ||
93ddb0d3 RC |
415 | /* validate input from userspace: */ |
416 | switch (submit_cmd.type) { | |
417 | case MSM_SUBMIT_CMD_BUF: | |
418 | case MSM_SUBMIT_CMD_IB_TARGET_BUF: | |
419 | case MSM_SUBMIT_CMD_CTX_RESTORE_BUF: | |
420 | break; | |
421 | default: | |
422 | DRM_ERROR("invalid type: %08x\n", submit_cmd.type); | |
423 | ret = -EINVAL; | |
424 | goto out; | |
425 | } | |
426 | ||
7198e6b0 RC |
427 | ret = submit_bo(submit, submit_cmd.submit_idx, |
428 | &msm_obj, &iova, NULL); | |
429 | if (ret) | |
430 | goto out; | |
431 | ||
432 | if (submit_cmd.size % 4) { | |
19872533 | 433 | DRM_ERROR("non-aligned cmdstream buffer size: %u\n", |
7198e6b0 RC |
434 | submit_cmd.size); |
435 | ret = -EINVAL; | |
436 | goto out; | |
437 | } | |
438 | ||
19872533 RC |
439 | if ((submit_cmd.size + submit_cmd.submit_offset) >= |
440 | msm_obj->base.size) { | |
441 | DRM_ERROR("invalid cmdstream size: %u\n", submit_cmd.size); | |
7198e6b0 RC |
442 | ret = -EINVAL; |
443 | goto out; | |
444 | } | |
445 | ||
446 | submit->cmd[i].type = submit_cmd.type; | |
447 | submit->cmd[i].size = submit_cmd.size / 4; | |
448 | submit->cmd[i].iova = iova + submit_cmd.submit_offset; | |
a7d3c950 | 449 | submit->cmd[i].idx = submit_cmd.submit_idx; |
7198e6b0 RC |
450 | |
451 | if (submit->valid) | |
452 | continue; | |
453 | ||
454 | ret = submit_reloc(submit, msm_obj, submit_cmd.submit_offset, | |
455 | submit_cmd.nr_relocs, submit_cmd.relocs); | |
456 | if (ret) | |
457 | goto out; | |
458 | } | |
459 | ||
460 | submit->nr_cmds = i; | |
461 | ||
462 | ret = msm_gpu_submit(gpu, submit, ctx); | |
463 | ||
b6295f9a | 464 | args->fence = submit->fence->seqno; |
7198e6b0 RC |
465 | |
466 | out: | |
40e6815b RC |
467 | submit_cleanup(submit); |
468 | if (ret) | |
469 | msm_gem_submit_free(submit); | |
b5b4c264 | 470 | out_unlock: |
c2703b13 | 471 | mutex_unlock(&dev->struct_mutex); |
7198e6b0 RC |
472 | return ret; |
473 | } |