power: supply: sbs-battery: simplify DT parsing
[deliverable/linux.git] / drivers / gpu / drm / msm / msm_gem_submit.c
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 /* is current addr in cmdstream correct/valid? */
28 #define BO_LOCKED 0x4000
29 #define BO_PINNED 0x2000
30
31 static struct msm_gem_submit *submit_create(struct drm_device *dev,
32 struct msm_gpu *gpu, int nr_bos, int nr_cmds)
33 {
34 struct msm_gem_submit *submit;
35 int sz = sizeof(*submit) + (nr_bos * sizeof(submit->bos[0])) +
36 (nr_cmds * sizeof(*submit->cmd));
37
38 submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
39 if (!submit)
40 return NULL;
41
42 submit->dev = dev;
43 submit->gpu = gpu;
44 submit->fence = NULL;
45 submit->pid = get_pid(task_pid(current));
46 submit->cmd = (void *)&submit->bos[nr_bos];
47
48 /* initially, until copy_from_user() and bo lookup succeeds: */
49 submit->nr_bos = 0;
50 submit->nr_cmds = 0;
51
52 INIT_LIST_HEAD(&submit->node);
53 INIT_LIST_HEAD(&submit->bo_list);
54 ww_acquire_init(&submit->ticket, &reservation_ww_class);
55
56 return submit;
57 }
58
59 void msm_gem_submit_free(struct msm_gem_submit *submit)
60 {
61 fence_put(submit->fence);
62 list_del(&submit->node);
63 put_pid(submit->pid);
64 kfree(submit);
65 }
66
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 =
80 u64_to_user_ptr(args->bos + (i * sizeof(submit_bo)));
81
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
87 ret = copy_from_user(&submit_bo, userptr, sizeof(submit_bo));
88 if (ret) {
89 ret = -EFAULT;
90 goto out_unlock;
91 }
92
93 if (submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) {
94 DRM_ERROR("invalid flags: %x\n", submit_bo.flags);
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) {
108 DRM_ERROR("invalid handle %u at index %u\n", submit_bo.handle, i);
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)) {
116 DRM_ERROR("handle %u at index %u already on submit list\n",
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: */
153 static int submit_lock_objects(struct msm_gem_submit *submit)
154 {
155 int contended, slow_locked = -1, i, ret = 0;
156
157 retry:
158 for (i = 0; i < submit->nr_bos; i++) {
159 struct msm_gem_object *msm_obj = submit->bos[i].obj;
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 }
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
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
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
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) {
253 DRM_ERROR("invalid buffer index: %u (out of %u)\n",
254 idx, submit->nr_bos);
255 return -EINVAL;
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) {
277 DRM_ERROR("non-aligned cmdstream buffer: %u\n", offset);
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 */
284 ptr = msm_gem_get_vaddr_locked(&obj->base);
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 =
295 u64_to_user_ptr(relocs + (i * sizeof(submit_reloc)));
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) {
304 DRM_ERROR("non-aligned reloc offset: %u\n",
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)) {
314 DRM_ERROR("invalid offset %u at reloc %u\n", off, i);
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
337 msm_gem_put_vaddr_locked(&obj->base);
338
339 return 0;
340 }
341
342 static void submit_cleanup(struct msm_gem_submit *submit)
343 {
344 unsigned i;
345
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 }
352
353 ww_acquire_fini(&submit->ticket);
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;
363 struct msm_gpu *gpu = priv->gpu;
364 unsigned i;
365 int ret;
366
367 if (!gpu)
368 return -ENXIO;
369
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
376 ret = mutex_lock_interruptible(&dev->struct_mutex);
377 if (ret)
378 return ret;
379
380 submit = submit_create(dev, gpu, args->nr_bos, args->nr_cmds);
381 if (!submit) {
382 ret = -ENOMEM;
383 goto out_unlock;
384 }
385
386 ret = submit_lookup_objects(submit, args, file);
387 if (ret)
388 goto out;
389
390 ret = submit_lock_objects(submit);
391 if (ret)
392 goto out;
393
394 ret = submit_fence_sync(submit);
395 if (ret)
396 goto out;
397
398 ret = submit_pin_objects(submit);
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 =
405 u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd)));
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
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
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) {
433 DRM_ERROR("non-aligned cmdstream buffer size: %u\n",
434 submit_cmd.size);
435 ret = -EINVAL;
436 goto out;
437 }
438
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);
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;
449 submit->cmd[i].idx = submit_cmd.submit_idx;
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
464 args->fence = submit->fence->seqno;
465
466 out:
467 submit_cleanup(submit);
468 if (ret)
469 msm_gem_submit_free(submit);
470 out_unlock:
471 mutex_unlock(&dev->struct_mutex);
472 return ret;
473 }
This page took 0.044936 seconds and 5 git commands to generate.