drm/nouveau: check pushbuffer bounds in ioctl
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nouveau_gem.c
CommitLineData
6ee73861
BS
1/*
2 * Copyright (C) 2008 Ben Skeggs.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26#include "drmP.h"
27#include "drm.h"
28
29#include "nouveau_drv.h"
30#include "nouveau_drm.h"
31#include "nouveau_dma.h"
32
33#define nouveau_gem_pushbuf_sync(chan) 0
34
35int
36nouveau_gem_object_new(struct drm_gem_object *gem)
37{
38 return 0;
39}
40
41void
42nouveau_gem_object_del(struct drm_gem_object *gem)
43{
44 struct nouveau_bo *nvbo = gem->driver_private;
45 struct ttm_buffer_object *bo = &nvbo->bo;
46
47 if (!nvbo)
48 return;
49 nvbo->gem = NULL;
50
51 if (unlikely(nvbo->cpu_filp))
52 ttm_bo_synccpu_write_release(bo);
53
54 if (unlikely(nvbo->pin_refcnt)) {
55 nvbo->pin_refcnt = 1;
56 nouveau_bo_unpin(nvbo);
57 }
58
59 ttm_bo_unref(&bo);
60}
61
62int
63nouveau_gem_new(struct drm_device *dev, struct nouveau_channel *chan,
64 int size, int align, uint32_t flags, uint32_t tile_mode,
65 uint32_t tile_flags, bool no_vm, bool mappable,
66 struct nouveau_bo **pnvbo)
67{
68 struct nouveau_bo *nvbo;
69 int ret;
70
71 ret = nouveau_bo_new(dev, chan, size, align, flags, tile_mode,
72 tile_flags, no_vm, mappable, pnvbo);
73 if (ret)
74 return ret;
75 nvbo = *pnvbo;
76
77 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
78 if (!nvbo->gem) {
79 nouveau_bo_ref(NULL, pnvbo);
80 return -ENOMEM;
81 }
82
83 nvbo->bo.persistant_swap_storage = nvbo->gem->filp;
84 nvbo->gem->driver_private = nvbo;
85 return 0;
86}
87
88static int
89nouveau_gem_info(struct drm_gem_object *gem, struct drm_nouveau_gem_info *rep)
90{
91 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
92
93 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
94 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
95 else
96 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
97
98 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
99 rep->offset = nvbo->bo.offset;
100 rep->map_handle = nvbo->mappable ? nvbo->bo.addr_space_offset : 0;
101 rep->tile_mode = nvbo->tile_mode;
102 rep->tile_flags = nvbo->tile_flags;
103 return 0;
104}
105
106static bool
107nouveau_gem_tile_flags_valid(struct drm_device *dev, uint32_t tile_flags) {
108 switch (tile_flags) {
109 case 0x0000:
110 case 0x1800:
111 case 0x2800:
112 case 0x4800:
113 case 0x7000:
114 case 0x7400:
115 case 0x7a00:
116 case 0xe000:
117 break;
118 default:
119 NV_ERROR(dev, "bad page flags: 0x%08x\n", tile_flags);
120 return false;
121 }
122
123 return true;
124}
125
126int
127nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
128 struct drm_file *file_priv)
129{
130 struct drm_nouveau_private *dev_priv = dev->dev_private;
131 struct drm_nouveau_gem_new *req = data;
132 struct nouveau_bo *nvbo = NULL;
133 struct nouveau_channel *chan = NULL;
134 uint32_t flags = 0;
135 int ret = 0;
136
137 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
138
139 if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL))
140 dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping;
141
142 if (req->channel_hint) {
143 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel_hint,
144 file_priv, chan);
145 }
146
147 if (req->info.domain & NOUVEAU_GEM_DOMAIN_VRAM)
148 flags |= TTM_PL_FLAG_VRAM;
149 if (req->info.domain & NOUVEAU_GEM_DOMAIN_GART)
150 flags |= TTM_PL_FLAG_TT;
151 if (!flags || req->info.domain & NOUVEAU_GEM_DOMAIN_CPU)
152 flags |= TTM_PL_FLAG_SYSTEM;
153
154 if (!nouveau_gem_tile_flags_valid(dev, req->info.tile_flags))
155 return -EINVAL;
156
157 ret = nouveau_gem_new(dev, chan, req->info.size, req->align, flags,
158 req->info.tile_mode, req->info.tile_flags, false,
159 (req->info.domain & NOUVEAU_GEM_DOMAIN_MAPPABLE),
160 &nvbo);
161 if (ret)
162 return ret;
163
164 ret = nouveau_gem_info(nvbo->gem, &req->info);
165 if (ret)
166 goto out;
167
168 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
169out:
170 mutex_lock(&dev->struct_mutex);
171 drm_gem_object_handle_unreference(nvbo->gem);
172 mutex_unlock(&dev->struct_mutex);
173
174 if (ret)
175 drm_gem_object_unreference(nvbo->gem);
176 return ret;
177}
178
179static int
180nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
181 uint32_t write_domains, uint32_t valid_domains)
182{
183 struct nouveau_bo *nvbo = gem->driver_private;
184 struct ttm_buffer_object *bo = &nvbo->bo;
185 uint64_t flags;
186
187 if (!valid_domains || (!read_domains && !write_domains))
188 return -EINVAL;
189
190 if (write_domains) {
191 if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
192 (write_domains & NOUVEAU_GEM_DOMAIN_VRAM))
193 flags = TTM_PL_FLAG_VRAM;
194 else
195 if ((valid_domains & NOUVEAU_GEM_DOMAIN_GART) &&
196 (write_domains & NOUVEAU_GEM_DOMAIN_GART))
197 flags = TTM_PL_FLAG_TT;
198 else
199 return -EINVAL;
200 } else {
201 if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
202 (read_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
203 bo->mem.mem_type == TTM_PL_VRAM)
204 flags = TTM_PL_FLAG_VRAM;
205 else
206 if ((valid_domains & NOUVEAU_GEM_DOMAIN_GART) &&
207 (read_domains & NOUVEAU_GEM_DOMAIN_GART) &&
208 bo->mem.mem_type == TTM_PL_TT)
209 flags = TTM_PL_FLAG_TT;
210 else
211 if ((valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
212 (read_domains & NOUVEAU_GEM_DOMAIN_VRAM))
213 flags = TTM_PL_FLAG_VRAM;
214 else
215 flags = TTM_PL_FLAG_TT;
216 }
217
218 nouveau_bo_placement_set(nvbo, flags);
219 return 0;
220}
221
222struct validate_op {
6ee73861
BS
223 struct list_head vram_list;
224 struct list_head gart_list;
225 struct list_head both_list;
226};
227
228static void
229validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
230{
231 struct list_head *entry, *tmp;
232 struct nouveau_bo *nvbo;
233
234 list_for_each_safe(entry, tmp, list) {
235 nvbo = list_entry(entry, struct nouveau_bo, entry);
236 if (likely(fence)) {
237 struct nouveau_fence *prev_fence;
238
239 spin_lock(&nvbo->bo.lock);
240 prev_fence = nvbo->bo.sync_obj;
241 nvbo->bo.sync_obj = nouveau_fence_ref(fence);
242 spin_unlock(&nvbo->bo.lock);
243 nouveau_fence_unref((void *)&prev_fence);
244 }
245
246 list_del(&nvbo->entry);
247 nvbo->reserved_by = NULL;
248 ttm_bo_unreserve(&nvbo->bo);
249 drm_gem_object_unreference(nvbo->gem);
250 }
251}
252
253static void
234896a7 254validate_fini(struct validate_op *op, struct nouveau_fence* fence)
6ee73861 255{
234896a7
LB
256 validate_fini_list(&op->vram_list, fence);
257 validate_fini_list(&op->gart_list, fence);
258 validate_fini_list(&op->both_list, fence);
6ee73861
BS
259}
260
261static int
262validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
263 struct drm_nouveau_gem_pushbuf_bo *pbbo,
264 int nr_buffers, struct validate_op *op)
265{
266 struct drm_device *dev = chan->dev;
267 struct drm_nouveau_private *dev_priv = dev->dev_private;
268 uint32_t sequence;
269 int trycnt = 0;
270 int ret, i;
271
272 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
273retry:
274 if (++trycnt > 100000) {
275 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
276 return -EINVAL;
277 }
278
279 for (i = 0; i < nr_buffers; i++) {
280 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
281 struct drm_gem_object *gem;
282 struct nouveau_bo *nvbo;
283
284 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
285 if (!gem) {
286 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
287 validate_fini(op, NULL);
288 return -EINVAL;
289 }
290 nvbo = gem->driver_private;
291
292 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
293 NV_ERROR(dev, "multiple instances of buffer %d on "
294 "validation list\n", b->handle);
295 validate_fini(op, NULL);
296 return -EINVAL;
297 }
298
299 ret = ttm_bo_reserve(&nvbo->bo, false, false, true, sequence);
300 if (ret) {
301 validate_fini(op, NULL);
302 if (ret == -EAGAIN)
303 ret = ttm_bo_wait_unreserved(&nvbo->bo, false);
304 drm_gem_object_unreference(gem);
305 if (ret)
306 return ret;
307 goto retry;
308 }
309
310 nvbo->reserved_by = file_priv;
311 nvbo->pbbo_index = i;
312 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
313 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
314 list_add_tail(&nvbo->entry, &op->both_list);
315 else
316 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
317 list_add_tail(&nvbo->entry, &op->vram_list);
318 else
319 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
320 list_add_tail(&nvbo->entry, &op->gart_list);
321 else {
322 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
323 b->valid_domains);
324 validate_fini(op, NULL);
325 return -EINVAL;
326 }
327
328 if (unlikely(atomic_read(&nvbo->bo.cpu_writers) > 0)) {
329 validate_fini(op, NULL);
330
331 if (nvbo->cpu_filp == file_priv) {
332 NV_ERROR(dev, "bo %p mapped by process trying "
333 "to validate it!\n", nvbo);
334 return -EINVAL;
335 }
336
337 ret = ttm_bo_wait_cpu(&nvbo->bo, false);
6ee73861
BS
338 if (ret)
339 return ret;
340 goto retry;
341 }
342 }
343
344 return 0;
345}
346
347static int
348validate_list(struct nouveau_channel *chan, struct list_head *list,
349 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
350{
351 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
352 (void __force __user *)(uintptr_t)user_pbbo_ptr;
353 struct nouveau_bo *nvbo;
354 int ret, relocs = 0;
355
356 list_for_each_entry(nvbo, list, entry) {
357 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
358 struct nouveau_fence *prev_fence = nvbo->bo.sync_obj;
359
360 if (prev_fence && nouveau_fence_channel(prev_fence) != chan) {
361 spin_lock(&nvbo->bo.lock);
362 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
363 spin_unlock(&nvbo->bo.lock);
364 if (unlikely(ret))
365 return ret;
366 }
367
368 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
369 b->write_domains,
370 b->valid_domains);
371 if (unlikely(ret))
372 return ret;
373
374 nvbo->channel = chan;
375 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
376 false, false);
377 nvbo->channel = NULL;
378 if (unlikely(ret))
379 return ret;
380
381 if (nvbo->bo.offset == b->presumed_offset &&
382 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
383 b->presumed_domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
384 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
385 b->presumed_domain & NOUVEAU_GEM_DOMAIN_GART)))
386 continue;
387
388 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
389 b->presumed_domain = NOUVEAU_GEM_DOMAIN_GART;
390 else
391 b->presumed_domain = NOUVEAU_GEM_DOMAIN_VRAM;
392 b->presumed_offset = nvbo->bo.offset;
393 b->presumed_ok = 0;
394 relocs++;
395
396 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index], b, sizeof(*b)))
397 return -EFAULT;
398 }
399
400 return relocs;
401}
402
403static int
404nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
405 struct drm_file *file_priv,
406 struct drm_nouveau_gem_pushbuf_bo *pbbo,
407 uint64_t user_buffers, int nr_buffers,
408 struct validate_op *op, int *apply_relocs)
409{
410 int ret, relocs = 0;
411
412 INIT_LIST_HEAD(&op->vram_list);
413 INIT_LIST_HEAD(&op->gart_list);
414 INIT_LIST_HEAD(&op->both_list);
415
6ee73861
BS
416 if (nr_buffers == 0)
417 return 0;
418
419 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
420 if (unlikely(ret))
421 return ret;
422
423 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
424 if (unlikely(ret < 0)) {
425 validate_fini(op, NULL);
426 return ret;
427 }
428 relocs += ret;
429
430 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
431 if (unlikely(ret < 0)) {
432 validate_fini(op, NULL);
433 return ret;
434 }
435 relocs += ret;
436
437 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
438 if (unlikely(ret < 0)) {
439 validate_fini(op, NULL);
440 return ret;
441 }
442 relocs += ret;
443
444 *apply_relocs = relocs;
445 return 0;
446}
447
448static inline void *
449u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
450{
451 void *mem;
452 void __user *userptr = (void __force __user *)(uintptr_t)user;
453
454 mem = kmalloc(nmemb * size, GFP_KERNEL);
455 if (!mem)
456 return ERR_PTR(-ENOMEM);
457
458 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
459 kfree(mem);
460 return ERR_PTR(-EFAULT);
461 }
462
463 return mem;
464}
465
466static int
467nouveau_gem_pushbuf_reloc_apply(struct nouveau_channel *chan, int nr_bo,
468 struct drm_nouveau_gem_pushbuf_bo *bo,
12f735b7
LB
469 unsigned nr_relocs, uint64_t ptr_relocs,
470 unsigned nr_dwords, unsigned first_dword,
6ee73861
BS
471 uint32_t *pushbuf, bool is_iomem)
472{
473 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
474 struct drm_device *dev = chan->dev;
12f735b7
LB
475 int ret = 0;
476 unsigned i;
6ee73861
BS
477
478 reloc = u_memcpya(ptr_relocs, nr_relocs, sizeof(*reloc));
479 if (IS_ERR(reloc))
480 return PTR_ERR(reloc);
481
482 for (i = 0; i < nr_relocs; i++) {
483 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
484 struct drm_nouveau_gem_pushbuf_bo *b;
485 uint32_t data;
486
487 if (r->bo_index >= nr_bo || r->reloc_index < first_dword ||
488 r->reloc_index >= first_dword + nr_dwords) {
489 NV_ERROR(dev, "Bad relocation %d\n", i);
490 NV_ERROR(dev, " bo: %d max %d\n", r->bo_index, nr_bo);
491 NV_ERROR(dev, " id: %d max %d\n", r->reloc_index, nr_dwords);
492 ret = -EINVAL;
493 break;
494 }
495
496 b = &bo[r->bo_index];
497 if (b->presumed_ok)
498 continue;
499
500 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
501 data = b->presumed_offset + r->data;
502 else
503 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
504 data = (b->presumed_offset + r->data) >> 32;
505 else
506 data = r->data;
507
508 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
509 if (b->presumed_domain == NOUVEAU_GEM_DOMAIN_GART)
510 data |= r->tor;
511 else
512 data |= r->vor;
513 }
514
515 if (is_iomem)
516 iowrite32_native(data, (void __force __iomem *)
517 &pushbuf[r->reloc_index]);
518 else
519 pushbuf[r->reloc_index] = data;
520 }
521
522 kfree(reloc);
523 return ret;
524}
525
526int
527nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
528 struct drm_file *file_priv)
529{
530 struct drm_nouveau_gem_pushbuf *req = data;
531 struct drm_nouveau_gem_pushbuf_bo *bo = NULL;
532 struct nouveau_channel *chan;
533 struct validate_op op;
234896a7 534 struct nouveau_fence* fence = 0;
6ee73861
BS
535 uint32_t *pushbuf = NULL;
536 int ret = 0, do_reloc = 0, i;
537
538 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
539 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
540
541 if (req->nr_dwords >= chan->dma.max ||
542 req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS ||
543 req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS) {
544 NV_ERROR(dev, "Pushbuf config exceeds limits:\n");
545 NV_ERROR(dev, " dwords : %d max %d\n", req->nr_dwords,
546 chan->dma.max - 1);
547 NV_ERROR(dev, " buffers: %d max %d\n", req->nr_buffers,
548 NOUVEAU_GEM_MAX_BUFFERS);
549 NV_ERROR(dev, " relocs : %d max %d\n", req->nr_relocs,
550 NOUVEAU_GEM_MAX_RELOCS);
551 return -EINVAL;
552 }
553
554 pushbuf = u_memcpya(req->dwords, req->nr_dwords, sizeof(uint32_t));
555 if (IS_ERR(pushbuf))
556 return PTR_ERR(pushbuf);
557
558 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
559 if (IS_ERR(bo)) {
560 kfree(pushbuf);
561 return PTR_ERR(bo);
562 }
563
564 mutex_lock(&dev->struct_mutex);
565
566 /* Validate buffer list */
567 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
568 req->nr_buffers, &op, &do_reloc);
569 if (ret)
570 goto out;
571
572 /* Apply any relocations that are required */
573 if (do_reloc) {
574 ret = nouveau_gem_pushbuf_reloc_apply(chan, req->nr_buffers,
575 bo, req->nr_relocs,
576 req->relocs,
577 req->nr_dwords, 0,
578 pushbuf, false);
579 if (ret)
580 goto out;
581 }
582
583 /* Emit push buffer to the hw
584 */
585 ret = RING_SPACE(chan, req->nr_dwords);
586 if (ret)
587 goto out;
588
589 OUT_RINGp(chan, pushbuf, req->nr_dwords);
590
234896a7 591 ret = nouveau_fence_new(chan, &fence, true);
6ee73861
BS
592 if (ret) {
593 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
594 WIND_RING(chan);
595 goto out;
596 }
597
598 if (nouveau_gem_pushbuf_sync(chan)) {
234896a7 599 ret = nouveau_fence_wait(fence, NULL, false, false);
6ee73861
BS
600 if (ret) {
601 for (i = 0; i < req->nr_dwords; i++)
602 NV_ERROR(dev, "0x%08x\n", pushbuf[i]);
603 NV_ERROR(dev, "^^ above push buffer is fail :(\n");
604 }
605 }
606
607out:
234896a7
LB
608 validate_fini(&op, fence);
609 nouveau_fence_unref((void**)&fence);
6ee73861
BS
610 mutex_unlock(&dev->struct_mutex);
611 kfree(pushbuf);
612 kfree(bo);
613 return ret;
614}
615
616#define PUSHBUF_CAL (dev_priv->card_type >= NV_20)
617
618int
619nouveau_gem_ioctl_pushbuf_call(struct drm_device *dev, void *data,
620 struct drm_file *file_priv)
621{
622 struct drm_nouveau_private *dev_priv = dev->dev_private;
623 struct drm_nouveau_gem_pushbuf_call *req = data;
624 struct drm_nouveau_gem_pushbuf_bo *bo = NULL;
625 struct nouveau_channel *chan;
626 struct drm_gem_object *gem;
627 struct nouveau_bo *pbbo;
628 struct validate_op op;
234896a7 629 struct nouveau_fence* fence = 0;
6ee73861
BS
630 int i, ret = 0, do_reloc = 0;
631
632 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
633 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
634
635 if (unlikely(req->handle == 0))
636 goto out_next;
637
638 if (req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS ||
639 req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS) {
640 NV_ERROR(dev, "Pushbuf config exceeds limits:\n");
641 NV_ERROR(dev, " buffers: %d max %d\n", req->nr_buffers,
642 NOUVEAU_GEM_MAX_BUFFERS);
643 NV_ERROR(dev, " relocs : %d max %d\n", req->nr_relocs,
644 NOUVEAU_GEM_MAX_RELOCS);
645 return -EINVAL;
646 }
647
648 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
649 if (IS_ERR(bo))
650 return PTR_ERR(bo);
651
652 mutex_lock(&dev->struct_mutex);
653
654 /* Validate buffer list */
655 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
656 req->nr_buffers, &op, &do_reloc);
657 if (ret) {
658 NV_ERROR(dev, "validate: %d\n", ret);
659 goto out;
660 }
661
662 /* Validate DMA push buffer */
663 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
664 if (!gem) {
665 NV_ERROR(dev, "Unknown pb handle 0x%08x\n", req->handle);
666 ret = -EINVAL;
667 goto out;
668 }
669 pbbo = nouveau_gem_object(gem);
670
12f735b7
LB
671 if ((req->offset & 3) || req->nr_dwords < 2 ||
672 (unsigned long)req->offset > (unsigned long)pbbo->bo.mem.size ||
673 (unsigned long)req->nr_dwords >
674 ((unsigned long)(pbbo->bo.mem.size - req->offset ) >> 2)) {
675 NV_ERROR(dev, "pb call misaligned or out of bounds: "
676 "%d + %d * 4 > %ld\n",
677 req->offset, req->nr_dwords, pbbo->bo.mem.size);
678 ret = -EINVAL;
679 drm_gem_object_unreference(gem);
680 goto out;
681 }
682
6ee73861
BS
683 ret = ttm_bo_reserve(&pbbo->bo, false, false, true,
684 chan->fence.sequence);
685 if (ret) {
686 NV_ERROR(dev, "resv pb: %d\n", ret);
687 drm_gem_object_unreference(gem);
688 goto out;
689 }
690
691 nouveau_bo_placement_set(pbbo, 1 << chan->pushbuf_bo->bo.mem.mem_type);
692 ret = ttm_bo_validate(&pbbo->bo, &pbbo->placement, false, false);
693 if (ret) {
694 NV_ERROR(dev, "validate pb: %d\n", ret);
695 ttm_bo_unreserve(&pbbo->bo);
696 drm_gem_object_unreference(gem);
697 goto out;
698 }
699
700 list_add_tail(&pbbo->entry, &op.both_list);
701
702 /* If presumed return address doesn't match, we need to map the
703 * push buffer and fix it..
704 */
705 if (!PUSHBUF_CAL) {
706 uint32_t retaddy;
707
708 if (chan->dma.free < 4 + NOUVEAU_DMA_SKIPS) {
709 ret = nouveau_dma_wait(chan, 4 + NOUVEAU_DMA_SKIPS);
710 if (ret) {
711 NV_ERROR(dev, "jmp_space: %d\n", ret);
712 goto out;
713 }
714 }
715
716 retaddy = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
717 retaddy |= 0x20000000;
718 if (retaddy != req->suffix0) {
719 req->suffix0 = retaddy;
720 do_reloc = 1;
721 }
722 }
723
724 /* Apply any relocations that are required */
725 if (do_reloc) {
726 void *pbvirt;
727 bool is_iomem;
728 ret = ttm_bo_kmap(&pbbo->bo, 0, pbbo->bo.mem.num_pages,
729 &pbbo->kmap);
730 if (ret) {
731 NV_ERROR(dev, "kmap pb: %d\n", ret);
732 goto out;
733 }
734
735 pbvirt = ttm_kmap_obj_virtual(&pbbo->kmap, &is_iomem);
736 ret = nouveau_gem_pushbuf_reloc_apply(chan, req->nr_buffers, bo,
737 req->nr_relocs,
738 req->relocs,
739 req->nr_dwords,
740 req->offset / 4,
741 pbvirt, is_iomem);
742
743 if (!PUSHBUF_CAL) {
744 nouveau_bo_wr32(pbbo,
745 req->offset / 4 + req->nr_dwords - 2,
746 req->suffix0);
747 }
748
749 ttm_bo_kunmap(&pbbo->kmap);
750 if (ret) {
751 NV_ERROR(dev, "reloc apply: %d\n", ret);
752 goto out;
753 }
754 }
755
756 if (PUSHBUF_CAL) {
757 ret = RING_SPACE(chan, 2);
758 if (ret) {
759 NV_ERROR(dev, "cal_space: %d\n", ret);
760 goto out;
761 }
762 OUT_RING(chan, ((pbbo->bo.mem.mm_node->start << PAGE_SHIFT) +
763 req->offset) | 2);
764 OUT_RING(chan, 0);
765 } else {
766 ret = RING_SPACE(chan, 2 + NOUVEAU_DMA_SKIPS);
767 if (ret) {
768 NV_ERROR(dev, "jmp_space: %d\n", ret);
769 goto out;
770 }
771 OUT_RING(chan, ((pbbo->bo.mem.mm_node->start << PAGE_SHIFT) +
772 req->offset) | 0x20000000);
773 OUT_RING(chan, 0);
774
775 /* Space the jumps apart with NOPs. */
776 for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
777 OUT_RING(chan, 0);
778 }
779
234896a7 780 ret = nouveau_fence_new(chan, &fence, true);
6ee73861
BS
781 if (ret) {
782 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
783 WIND_RING(chan);
784 goto out;
785 }
786
787out:
234896a7
LB
788 validate_fini(&op, fence);
789 nouveau_fence_unref((void**)&fence);
6ee73861
BS
790 mutex_unlock(&dev->struct_mutex);
791 kfree(bo);
792
793out_next:
794 if (PUSHBUF_CAL) {
795 req->suffix0 = 0x00020000;
796 req->suffix1 = 0x00000000;
797 } else {
798 req->suffix0 = 0x20000000 |
799 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
800 req->suffix1 = 0x00000000;
801 }
802
803 return ret;
804}
805
806int
807nouveau_gem_ioctl_pushbuf_call2(struct drm_device *dev, void *data,
808 struct drm_file *file_priv)
809{
810 struct drm_nouveau_private *dev_priv = dev->dev_private;
811 struct drm_nouveau_gem_pushbuf_call *req = data;
812
813 req->vram_available = dev_priv->fb_aper_free;
814 req->gart_available = dev_priv->gart_info.aper_free;
815
816 return nouveau_gem_ioctl_pushbuf_call(dev, data, file_priv);
817}
818
819static inline uint32_t
820domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
821{
822 uint32_t flags = 0;
823
824 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
825 flags |= TTM_PL_FLAG_VRAM;
826 if (domain & NOUVEAU_GEM_DOMAIN_GART)
827 flags |= TTM_PL_FLAG_TT;
828
829 return flags;
830}
831
832int
833nouveau_gem_ioctl_pin(struct drm_device *dev, void *data,
834 struct drm_file *file_priv)
835{
836 struct drm_nouveau_gem_pin *req = data;
837 struct drm_gem_object *gem;
838 struct nouveau_bo *nvbo;
839 int ret = 0;
840
841 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
842
843 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
844 NV_ERROR(dev, "pin only allowed without kernel modesetting\n");
845 return -EINVAL;
846 }
847
848 if (!DRM_SUSER(DRM_CURPROC))
849 return -EPERM;
850
851 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
852 if (!gem)
853 return -EINVAL;
854 nvbo = nouveau_gem_object(gem);
855
856 ret = nouveau_bo_pin(nvbo, domain_to_ttm(nvbo, req->domain));
857 if (ret)
858 goto out;
859
860 req->offset = nvbo->bo.offset;
861 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
862 req->domain = NOUVEAU_GEM_DOMAIN_GART;
863 else
864 req->domain = NOUVEAU_GEM_DOMAIN_VRAM;
865
866out:
867 mutex_lock(&dev->struct_mutex);
868 drm_gem_object_unreference(gem);
869 mutex_unlock(&dev->struct_mutex);
870
871 return ret;
872}
873
874int
875nouveau_gem_ioctl_unpin(struct drm_device *dev, void *data,
876 struct drm_file *file_priv)
877{
878 struct drm_nouveau_gem_pin *req = data;
879 struct drm_gem_object *gem;
880 int ret;
881
882 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
883
884 if (drm_core_check_feature(dev, DRIVER_MODESET))
885 return -EINVAL;
886
887 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
888 if (!gem)
889 return -EINVAL;
890
891 ret = nouveau_bo_unpin(nouveau_gem_object(gem));
892
893 mutex_lock(&dev->struct_mutex);
894 drm_gem_object_unreference(gem);
895 mutex_unlock(&dev->struct_mutex);
896
897 return ret;
898}
899
900int
901nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
902 struct drm_file *file_priv)
903{
904 struct drm_nouveau_gem_cpu_prep *req = data;
905 struct drm_gem_object *gem;
906 struct nouveau_bo *nvbo;
907 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
908 int ret = -EINVAL;
909
910 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
911
912 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
913 if (!gem)
914 return ret;
915 nvbo = nouveau_gem_object(gem);
916
917 if (nvbo->cpu_filp) {
918 if (nvbo->cpu_filp == file_priv)
919 goto out;
920
921 ret = ttm_bo_wait_cpu(&nvbo->bo, no_wait);
6ee73861
BS
922 if (ret)
923 goto out;
924 }
925
926 if (req->flags & NOUVEAU_GEM_CPU_PREP_NOBLOCK) {
927 ret = ttm_bo_wait(&nvbo->bo, false, false, no_wait);
928 } else {
929 ret = ttm_bo_synccpu_write_grab(&nvbo->bo, no_wait);
6ee73861
BS
930 if (ret == 0)
931 nvbo->cpu_filp = file_priv;
932 }
933
934out:
935 mutex_lock(&dev->struct_mutex);
936 drm_gem_object_unreference(gem);
937 mutex_unlock(&dev->struct_mutex);
938 return ret;
939}
940
941int
942nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
943 struct drm_file *file_priv)
944{
945 struct drm_nouveau_gem_cpu_prep *req = data;
946 struct drm_gem_object *gem;
947 struct nouveau_bo *nvbo;
948 int ret = -EINVAL;
949
950 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
951
952 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
953 if (!gem)
954 return ret;
955 nvbo = nouveau_gem_object(gem);
956
957 if (nvbo->cpu_filp != file_priv)
958 goto out;
959 nvbo->cpu_filp = NULL;
960
961 ttm_bo_synccpu_write_release(&nvbo->bo);
962 ret = 0;
963
964out:
965 mutex_lock(&dev->struct_mutex);
966 drm_gem_object_unreference(gem);
967 mutex_unlock(&dev->struct_mutex);
968 return ret;
969}
970
971int
972nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
973 struct drm_file *file_priv)
974{
975 struct drm_nouveau_gem_info *req = data;
976 struct drm_gem_object *gem;
977 int ret;
978
979 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
980
981 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
982 if (!gem)
983 return -EINVAL;
984
985 ret = nouveau_gem_info(gem, req);
986 mutex_lock(&dev->struct_mutex);
987 drm_gem_object_unreference(gem);
988 mutex_unlock(&dev->struct_mutex);
989 return ret;
990}
991
This page took 0.066684 seconds and 5 git commands to generate.