drm/nv50: prevent a possible ctxprog hang
[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 {
223 struct nouveau_fence *fence;
224 struct list_head vram_list;
225 struct list_head gart_list;
226 struct list_head both_list;
227};
228
229static void
230validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
231{
232 struct list_head *entry, *tmp;
233 struct nouveau_bo *nvbo;
234
235 list_for_each_safe(entry, tmp, list) {
236 nvbo = list_entry(entry, struct nouveau_bo, entry);
237 if (likely(fence)) {
238 struct nouveau_fence *prev_fence;
239
240 spin_lock(&nvbo->bo.lock);
241 prev_fence = nvbo->bo.sync_obj;
242 nvbo->bo.sync_obj = nouveau_fence_ref(fence);
243 spin_unlock(&nvbo->bo.lock);
244 nouveau_fence_unref((void *)&prev_fence);
245 }
246
247 list_del(&nvbo->entry);
248 nvbo->reserved_by = NULL;
249 ttm_bo_unreserve(&nvbo->bo);
250 drm_gem_object_unreference(nvbo->gem);
251 }
252}
253
254static void
255validate_fini(struct validate_op *op, bool success)
256{
257 struct nouveau_fence *fence = op->fence;
258
259 if (unlikely(!success))
260 op->fence = NULL;
261
262 validate_fini_list(&op->vram_list, op->fence);
263 validate_fini_list(&op->gart_list, op->fence);
264 validate_fini_list(&op->both_list, op->fence);
265 nouveau_fence_unref((void *)&fence);
266}
267
268static int
269validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
270 struct drm_nouveau_gem_pushbuf_bo *pbbo,
271 int nr_buffers, struct validate_op *op)
272{
273 struct drm_device *dev = chan->dev;
274 struct drm_nouveau_private *dev_priv = dev->dev_private;
275 uint32_t sequence;
276 int trycnt = 0;
277 int ret, i;
278
279 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
280retry:
281 if (++trycnt > 100000) {
282 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
283 return -EINVAL;
284 }
285
286 for (i = 0; i < nr_buffers; i++) {
287 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
288 struct drm_gem_object *gem;
289 struct nouveau_bo *nvbo;
290
291 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
292 if (!gem) {
293 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
294 validate_fini(op, NULL);
295 return -EINVAL;
296 }
297 nvbo = gem->driver_private;
298
299 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
300 NV_ERROR(dev, "multiple instances of buffer %d on "
301 "validation list\n", b->handle);
302 validate_fini(op, NULL);
303 return -EINVAL;
304 }
305
306 ret = ttm_bo_reserve(&nvbo->bo, false, false, true, sequence);
307 if (ret) {
308 validate_fini(op, NULL);
309 if (ret == -EAGAIN)
310 ret = ttm_bo_wait_unreserved(&nvbo->bo, false);
311 drm_gem_object_unreference(gem);
312 if (ret)
313 return ret;
314 goto retry;
315 }
316
317 nvbo->reserved_by = file_priv;
318 nvbo->pbbo_index = i;
319 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
320 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
321 list_add_tail(&nvbo->entry, &op->both_list);
322 else
323 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
324 list_add_tail(&nvbo->entry, &op->vram_list);
325 else
326 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
327 list_add_tail(&nvbo->entry, &op->gart_list);
328 else {
329 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
330 b->valid_domains);
331 validate_fini(op, NULL);
332 return -EINVAL;
333 }
334
335 if (unlikely(atomic_read(&nvbo->bo.cpu_writers) > 0)) {
336 validate_fini(op, NULL);
337
338 if (nvbo->cpu_filp == file_priv) {
339 NV_ERROR(dev, "bo %p mapped by process trying "
340 "to validate it!\n", nvbo);
341 return -EINVAL;
342 }
343
344 ret = ttm_bo_wait_cpu(&nvbo->bo, false);
6ee73861
BS
345 if (ret)
346 return ret;
347 goto retry;
348 }
349 }
350
351 return 0;
352}
353
354static int
355validate_list(struct nouveau_channel *chan, struct list_head *list,
356 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
357{
358 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
359 (void __force __user *)(uintptr_t)user_pbbo_ptr;
360 struct nouveau_bo *nvbo;
361 int ret, relocs = 0;
362
363 list_for_each_entry(nvbo, list, entry) {
364 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
365 struct nouveau_fence *prev_fence = nvbo->bo.sync_obj;
366
367 if (prev_fence && nouveau_fence_channel(prev_fence) != chan) {
368 spin_lock(&nvbo->bo.lock);
369 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
370 spin_unlock(&nvbo->bo.lock);
371 if (unlikely(ret))
372 return ret;
373 }
374
375 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
376 b->write_domains,
377 b->valid_domains);
378 if (unlikely(ret))
379 return ret;
380
381 nvbo->channel = chan;
382 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
383 false, false);
384 nvbo->channel = NULL;
385 if (unlikely(ret))
386 return ret;
387
388 if (nvbo->bo.offset == b->presumed_offset &&
389 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
390 b->presumed_domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
391 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
392 b->presumed_domain & NOUVEAU_GEM_DOMAIN_GART)))
393 continue;
394
395 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
396 b->presumed_domain = NOUVEAU_GEM_DOMAIN_GART;
397 else
398 b->presumed_domain = NOUVEAU_GEM_DOMAIN_VRAM;
399 b->presumed_offset = nvbo->bo.offset;
400 b->presumed_ok = 0;
401 relocs++;
402
403 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index], b, sizeof(*b)))
404 return -EFAULT;
405 }
406
407 return relocs;
408}
409
410static int
411nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
412 struct drm_file *file_priv,
413 struct drm_nouveau_gem_pushbuf_bo *pbbo,
414 uint64_t user_buffers, int nr_buffers,
415 struct validate_op *op, int *apply_relocs)
416{
417 int ret, relocs = 0;
418
419 INIT_LIST_HEAD(&op->vram_list);
420 INIT_LIST_HEAD(&op->gart_list);
421 INIT_LIST_HEAD(&op->both_list);
422
423 ret = nouveau_fence_new(chan, &op->fence, false);
424 if (ret)
425 return ret;
426
427 if (nr_buffers == 0)
428 return 0;
429
430 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
431 if (unlikely(ret))
432 return ret;
433
434 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
435 if (unlikely(ret < 0)) {
436 validate_fini(op, NULL);
437 return ret;
438 }
439 relocs += ret;
440
441 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
442 if (unlikely(ret < 0)) {
443 validate_fini(op, NULL);
444 return ret;
445 }
446 relocs += ret;
447
448 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
449 if (unlikely(ret < 0)) {
450 validate_fini(op, NULL);
451 return ret;
452 }
453 relocs += ret;
454
455 *apply_relocs = relocs;
456 return 0;
457}
458
459static inline void *
460u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
461{
462 void *mem;
463 void __user *userptr = (void __force __user *)(uintptr_t)user;
464
465 mem = kmalloc(nmemb * size, GFP_KERNEL);
466 if (!mem)
467 return ERR_PTR(-ENOMEM);
468
469 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
470 kfree(mem);
471 return ERR_PTR(-EFAULT);
472 }
473
474 return mem;
475}
476
477static int
478nouveau_gem_pushbuf_reloc_apply(struct nouveau_channel *chan, int nr_bo,
479 struct drm_nouveau_gem_pushbuf_bo *bo,
480 int nr_relocs, uint64_t ptr_relocs,
481 int nr_dwords, int first_dword,
482 uint32_t *pushbuf, bool is_iomem)
483{
484 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
485 struct drm_device *dev = chan->dev;
486 int ret = 0, i;
487
488 reloc = u_memcpya(ptr_relocs, nr_relocs, sizeof(*reloc));
489 if (IS_ERR(reloc))
490 return PTR_ERR(reloc);
491
492 for (i = 0; i < nr_relocs; i++) {
493 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
494 struct drm_nouveau_gem_pushbuf_bo *b;
495 uint32_t data;
496
497 if (r->bo_index >= nr_bo || r->reloc_index < first_dword ||
498 r->reloc_index >= first_dword + nr_dwords) {
499 NV_ERROR(dev, "Bad relocation %d\n", i);
500 NV_ERROR(dev, " bo: %d max %d\n", r->bo_index, nr_bo);
501 NV_ERROR(dev, " id: %d max %d\n", r->reloc_index, nr_dwords);
502 ret = -EINVAL;
503 break;
504 }
505
506 b = &bo[r->bo_index];
507 if (b->presumed_ok)
508 continue;
509
510 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
511 data = b->presumed_offset + r->data;
512 else
513 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
514 data = (b->presumed_offset + r->data) >> 32;
515 else
516 data = r->data;
517
518 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
519 if (b->presumed_domain == NOUVEAU_GEM_DOMAIN_GART)
520 data |= r->tor;
521 else
522 data |= r->vor;
523 }
524
525 if (is_iomem)
526 iowrite32_native(data, (void __force __iomem *)
527 &pushbuf[r->reloc_index]);
528 else
529 pushbuf[r->reloc_index] = data;
530 }
531
532 kfree(reloc);
533 return ret;
534}
535
536int
537nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
538 struct drm_file *file_priv)
539{
540 struct drm_nouveau_gem_pushbuf *req = data;
541 struct drm_nouveau_gem_pushbuf_bo *bo = NULL;
542 struct nouveau_channel *chan;
543 struct validate_op op;
544 uint32_t *pushbuf = NULL;
545 int ret = 0, do_reloc = 0, i;
546
547 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
548 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
549
550 if (req->nr_dwords >= chan->dma.max ||
551 req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS ||
552 req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS) {
553 NV_ERROR(dev, "Pushbuf config exceeds limits:\n");
554 NV_ERROR(dev, " dwords : %d max %d\n", req->nr_dwords,
555 chan->dma.max - 1);
556 NV_ERROR(dev, " buffers: %d max %d\n", req->nr_buffers,
557 NOUVEAU_GEM_MAX_BUFFERS);
558 NV_ERROR(dev, " relocs : %d max %d\n", req->nr_relocs,
559 NOUVEAU_GEM_MAX_RELOCS);
560 return -EINVAL;
561 }
562
563 pushbuf = u_memcpya(req->dwords, req->nr_dwords, sizeof(uint32_t));
564 if (IS_ERR(pushbuf))
565 return PTR_ERR(pushbuf);
566
567 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
568 if (IS_ERR(bo)) {
569 kfree(pushbuf);
570 return PTR_ERR(bo);
571 }
572
573 mutex_lock(&dev->struct_mutex);
574
575 /* Validate buffer list */
576 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
577 req->nr_buffers, &op, &do_reloc);
578 if (ret)
579 goto out;
580
581 /* Apply any relocations that are required */
582 if (do_reloc) {
583 ret = nouveau_gem_pushbuf_reloc_apply(chan, req->nr_buffers,
584 bo, req->nr_relocs,
585 req->relocs,
586 req->nr_dwords, 0,
587 pushbuf, false);
588 if (ret)
589 goto out;
590 }
591
592 /* Emit push buffer to the hw
593 */
594 ret = RING_SPACE(chan, req->nr_dwords);
595 if (ret)
596 goto out;
597
598 OUT_RINGp(chan, pushbuf, req->nr_dwords);
599
600 ret = nouveau_fence_emit(op.fence);
601 if (ret) {
602 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
603 WIND_RING(chan);
604 goto out;
605 }
606
607 if (nouveau_gem_pushbuf_sync(chan)) {
608 ret = nouveau_fence_wait(op.fence, NULL, false, false);
609 if (ret) {
610 for (i = 0; i < req->nr_dwords; i++)
611 NV_ERROR(dev, "0x%08x\n", pushbuf[i]);
612 NV_ERROR(dev, "^^ above push buffer is fail :(\n");
613 }
614 }
615
616out:
617 validate_fini(&op, ret == 0);
618 mutex_unlock(&dev->struct_mutex);
619 kfree(pushbuf);
620 kfree(bo);
621 return ret;
622}
623
624#define PUSHBUF_CAL (dev_priv->card_type >= NV_20)
625
626int
627nouveau_gem_ioctl_pushbuf_call(struct drm_device *dev, void *data,
628 struct drm_file *file_priv)
629{
630 struct drm_nouveau_private *dev_priv = dev->dev_private;
631 struct drm_nouveau_gem_pushbuf_call *req = data;
632 struct drm_nouveau_gem_pushbuf_bo *bo = NULL;
633 struct nouveau_channel *chan;
634 struct drm_gem_object *gem;
635 struct nouveau_bo *pbbo;
636 struct validate_op op;
637 int i, ret = 0, do_reloc = 0;
638
639 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
640 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
641
642 if (unlikely(req->handle == 0))
643 goto out_next;
644
645 if (req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS ||
646 req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS) {
647 NV_ERROR(dev, "Pushbuf config exceeds limits:\n");
648 NV_ERROR(dev, " buffers: %d max %d\n", req->nr_buffers,
649 NOUVEAU_GEM_MAX_BUFFERS);
650 NV_ERROR(dev, " relocs : %d max %d\n", req->nr_relocs,
651 NOUVEAU_GEM_MAX_RELOCS);
652 return -EINVAL;
653 }
654
655 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
656 if (IS_ERR(bo))
657 return PTR_ERR(bo);
658
659 mutex_lock(&dev->struct_mutex);
660
661 /* Validate buffer list */
662 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
663 req->nr_buffers, &op, &do_reloc);
664 if (ret) {
665 NV_ERROR(dev, "validate: %d\n", ret);
666 goto out;
667 }
668
669 /* Validate DMA push buffer */
670 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
671 if (!gem) {
672 NV_ERROR(dev, "Unknown pb handle 0x%08x\n", req->handle);
673 ret = -EINVAL;
674 goto out;
675 }
676 pbbo = nouveau_gem_object(gem);
677
678 ret = ttm_bo_reserve(&pbbo->bo, false, false, true,
679 chan->fence.sequence);
680 if (ret) {
681 NV_ERROR(dev, "resv pb: %d\n", ret);
682 drm_gem_object_unreference(gem);
683 goto out;
684 }
685
686 nouveau_bo_placement_set(pbbo, 1 << chan->pushbuf_bo->bo.mem.mem_type);
687 ret = ttm_bo_validate(&pbbo->bo, &pbbo->placement, false, false);
688 if (ret) {
689 NV_ERROR(dev, "validate pb: %d\n", ret);
690 ttm_bo_unreserve(&pbbo->bo);
691 drm_gem_object_unreference(gem);
692 goto out;
693 }
694
695 list_add_tail(&pbbo->entry, &op.both_list);
696
697 /* If presumed return address doesn't match, we need to map the
698 * push buffer and fix it..
699 */
700 if (!PUSHBUF_CAL) {
701 uint32_t retaddy;
702
703 if (chan->dma.free < 4 + NOUVEAU_DMA_SKIPS) {
704 ret = nouveau_dma_wait(chan, 4 + NOUVEAU_DMA_SKIPS);
705 if (ret) {
706 NV_ERROR(dev, "jmp_space: %d\n", ret);
707 goto out;
708 }
709 }
710
711 retaddy = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
712 retaddy |= 0x20000000;
713 if (retaddy != req->suffix0) {
714 req->suffix0 = retaddy;
715 do_reloc = 1;
716 }
717 }
718
719 /* Apply any relocations that are required */
720 if (do_reloc) {
721 void *pbvirt;
722 bool is_iomem;
723 ret = ttm_bo_kmap(&pbbo->bo, 0, pbbo->bo.mem.num_pages,
724 &pbbo->kmap);
725 if (ret) {
726 NV_ERROR(dev, "kmap pb: %d\n", ret);
727 goto out;
728 }
729
730 pbvirt = ttm_kmap_obj_virtual(&pbbo->kmap, &is_iomem);
731 ret = nouveau_gem_pushbuf_reloc_apply(chan, req->nr_buffers, bo,
732 req->nr_relocs,
733 req->relocs,
734 req->nr_dwords,
735 req->offset / 4,
736 pbvirt, is_iomem);
737
738 if (!PUSHBUF_CAL) {
739 nouveau_bo_wr32(pbbo,
740 req->offset / 4 + req->nr_dwords - 2,
741 req->suffix0);
742 }
743
744 ttm_bo_kunmap(&pbbo->kmap);
745 if (ret) {
746 NV_ERROR(dev, "reloc apply: %d\n", ret);
747 goto out;
748 }
749 }
750
751 if (PUSHBUF_CAL) {
752 ret = RING_SPACE(chan, 2);
753 if (ret) {
754 NV_ERROR(dev, "cal_space: %d\n", ret);
755 goto out;
756 }
757 OUT_RING(chan, ((pbbo->bo.mem.mm_node->start << PAGE_SHIFT) +
758 req->offset) | 2);
759 OUT_RING(chan, 0);
760 } else {
761 ret = RING_SPACE(chan, 2 + NOUVEAU_DMA_SKIPS);
762 if (ret) {
763 NV_ERROR(dev, "jmp_space: %d\n", ret);
764 goto out;
765 }
766 OUT_RING(chan, ((pbbo->bo.mem.mm_node->start << PAGE_SHIFT) +
767 req->offset) | 0x20000000);
768 OUT_RING(chan, 0);
769
770 /* Space the jumps apart with NOPs. */
771 for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
772 OUT_RING(chan, 0);
773 }
774
775 ret = nouveau_fence_emit(op.fence);
776 if (ret) {
777 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
778 WIND_RING(chan);
779 goto out;
780 }
781
782out:
783 validate_fini(&op, ret == 0);
784 mutex_unlock(&dev->struct_mutex);
785 kfree(bo);
786
787out_next:
788 if (PUSHBUF_CAL) {
789 req->suffix0 = 0x00020000;
790 req->suffix1 = 0x00000000;
791 } else {
792 req->suffix0 = 0x20000000 |
793 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
794 req->suffix1 = 0x00000000;
795 }
796
797 return ret;
798}
799
800int
801nouveau_gem_ioctl_pushbuf_call2(struct drm_device *dev, void *data,
802 struct drm_file *file_priv)
803{
804 struct drm_nouveau_private *dev_priv = dev->dev_private;
805 struct drm_nouveau_gem_pushbuf_call *req = data;
806
807 req->vram_available = dev_priv->fb_aper_free;
808 req->gart_available = dev_priv->gart_info.aper_free;
809
810 return nouveau_gem_ioctl_pushbuf_call(dev, data, file_priv);
811}
812
813static inline uint32_t
814domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
815{
816 uint32_t flags = 0;
817
818 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
819 flags |= TTM_PL_FLAG_VRAM;
820 if (domain & NOUVEAU_GEM_DOMAIN_GART)
821 flags |= TTM_PL_FLAG_TT;
822
823 return flags;
824}
825
826int
827nouveau_gem_ioctl_pin(struct drm_device *dev, void *data,
828 struct drm_file *file_priv)
829{
830 struct drm_nouveau_gem_pin *req = data;
831 struct drm_gem_object *gem;
832 struct nouveau_bo *nvbo;
833 int ret = 0;
834
835 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
836
837 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
838 NV_ERROR(dev, "pin only allowed without kernel modesetting\n");
839 return -EINVAL;
840 }
841
842 if (!DRM_SUSER(DRM_CURPROC))
843 return -EPERM;
844
845 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
846 if (!gem)
847 return -EINVAL;
848 nvbo = nouveau_gem_object(gem);
849
850 ret = nouveau_bo_pin(nvbo, domain_to_ttm(nvbo, req->domain));
851 if (ret)
852 goto out;
853
854 req->offset = nvbo->bo.offset;
855 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
856 req->domain = NOUVEAU_GEM_DOMAIN_GART;
857 else
858 req->domain = NOUVEAU_GEM_DOMAIN_VRAM;
859
860out:
861 mutex_lock(&dev->struct_mutex);
862 drm_gem_object_unreference(gem);
863 mutex_unlock(&dev->struct_mutex);
864
865 return ret;
866}
867
868int
869nouveau_gem_ioctl_unpin(struct drm_device *dev, void *data,
870 struct drm_file *file_priv)
871{
872 struct drm_nouveau_gem_pin *req = data;
873 struct drm_gem_object *gem;
874 int ret;
875
876 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
877
878 if (drm_core_check_feature(dev, DRIVER_MODESET))
879 return -EINVAL;
880
881 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
882 if (!gem)
883 return -EINVAL;
884
885 ret = nouveau_bo_unpin(nouveau_gem_object(gem));
886
887 mutex_lock(&dev->struct_mutex);
888 drm_gem_object_unreference(gem);
889 mutex_unlock(&dev->struct_mutex);
890
891 return ret;
892}
893
894int
895nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
896 struct drm_file *file_priv)
897{
898 struct drm_nouveau_gem_cpu_prep *req = data;
899 struct drm_gem_object *gem;
900 struct nouveau_bo *nvbo;
901 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
902 int ret = -EINVAL;
903
904 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
905
906 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
907 if (!gem)
908 return ret;
909 nvbo = nouveau_gem_object(gem);
910
911 if (nvbo->cpu_filp) {
912 if (nvbo->cpu_filp == file_priv)
913 goto out;
914
915 ret = ttm_bo_wait_cpu(&nvbo->bo, no_wait);
6ee73861
BS
916 if (ret)
917 goto out;
918 }
919
920 if (req->flags & NOUVEAU_GEM_CPU_PREP_NOBLOCK) {
921 ret = ttm_bo_wait(&nvbo->bo, false, false, no_wait);
922 } else {
923 ret = ttm_bo_synccpu_write_grab(&nvbo->bo, no_wait);
6ee73861
BS
924 if (ret == 0)
925 nvbo->cpu_filp = file_priv;
926 }
927
928out:
929 mutex_lock(&dev->struct_mutex);
930 drm_gem_object_unreference(gem);
931 mutex_unlock(&dev->struct_mutex);
932 return ret;
933}
934
935int
936nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
937 struct drm_file *file_priv)
938{
939 struct drm_nouveau_gem_cpu_prep *req = data;
940 struct drm_gem_object *gem;
941 struct nouveau_bo *nvbo;
942 int ret = -EINVAL;
943
944 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
945
946 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
947 if (!gem)
948 return ret;
949 nvbo = nouveau_gem_object(gem);
950
951 if (nvbo->cpu_filp != file_priv)
952 goto out;
953 nvbo->cpu_filp = NULL;
954
955 ttm_bo_synccpu_write_release(&nvbo->bo);
956 ret = 0;
957
958out:
959 mutex_lock(&dev->struct_mutex);
960 drm_gem_object_unreference(gem);
961 mutex_unlock(&dev->struct_mutex);
962 return ret;
963}
964
965int
966nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
967 struct drm_file *file_priv)
968{
969 struct drm_nouveau_gem_info *req = data;
970 struct drm_gem_object *gem;
971 int ret;
972
973 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
974
975 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
976 if (!gem)
977 return -EINVAL;
978
979 ret = nouveau_gem_info(gem, req);
980 mutex_lock(&dev->struct_mutex);
981 drm_gem_object_unreference(gem);
982 mutex_unlock(&dev->struct_mutex);
983 return ret;
984}
985
This page took 0.110838 seconds and 5 git commands to generate.