Merge branch 'drm-next-3.18' of git://people.freedesktop.org/~agd5f/linux into drm...
[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 */
6ee73861 26
ebb945a9 27#include "nouveau_drm.h"
6ee73861 28#include "nouveau_dma.h"
d375e7d5 29#include "nouveau_fence.h"
ebb945a9 30#include "nouveau_abi16.h"
6ee73861 31
ebb945a9
BS
32#include "nouveau_ttm.h"
33#include "nouveau_gem.h"
6ee73861 34
6ee73861
BS
35void
36nouveau_gem_object_del(struct drm_gem_object *gem)
37{
55fb74ad 38 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
6ee73861
BS
39 struct ttm_buffer_object *bo = &nvbo->bo;
40
22b33e8e
DA
41 if (gem->import_attach)
42 drm_prime_gem_destroy(gem, nvbo->bo.sg);
43
fd632aa3 44 drm_gem_object_release(gem);
55fb74ad
DH
45
46 /* reset filp so nouveau_bo_del_ttm() can test for it */
47 gem->filp = NULL;
48 ttm_bo_unref(&bo);
6ee73861
BS
49}
50
639212d0
BS
51int
52nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
53{
ebb945a9 54 struct nouveau_cli *cli = nouveau_cli(file_priv);
2fd3db6f
BS
55 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
56 struct nouveau_vma *vma;
57 int ret;
639212d0 58
3ee6f5b5 59 if (!cli->vm)
639212d0
BS
60 return 0;
61
ee3939e0 62 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
2fd3db6f
BS
63 if (ret)
64 return ret;
65
3ee6f5b5 66 vma = nouveau_bo_vma_find(nvbo, cli->vm);
2fd3db6f
BS
67 if (!vma) {
68 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
69 if (!vma) {
70 ret = -ENOMEM;
71 goto out;
72 }
73
3ee6f5b5 74 ret = nouveau_bo_vma_add(nvbo, cli->vm, vma);
2fd3db6f
BS
75 if (ret) {
76 kfree(vma);
77 goto out;
78 }
79 } else {
80 vma->refcount++;
81 }
82
83out:
84 ttm_bo_unreserve(&nvbo->bo);
85 return ret;
639212d0
BS
86}
87
c4c7044f
BS
88static void
89nouveau_gem_object_delete(void *data)
90{
91 struct nouveau_vma *vma = data;
92 nouveau_vm_unmap(vma);
93 nouveau_vm_put(vma);
94 kfree(vma);
95}
96
97static void
98nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
99{
100 const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
101 struct nouveau_fence *fence = NULL;
102
103 list_del(&vma->head);
104
105 if (mapped) {
106 spin_lock(&nvbo->bo.bdev->fence_lock);
5d216f60 107 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
c4c7044f
BS
108 spin_unlock(&nvbo->bo.bdev->fence_lock);
109 }
110
111 if (fence) {
112 nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
113 } else {
114 if (mapped)
115 nouveau_vm_unmap(vma);
116 nouveau_vm_put(vma);
117 kfree(vma);
118 }
119 nouveau_fence_unref(&fence);
120}
121
639212d0
BS
122void
123nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
124{
ebb945a9 125 struct nouveau_cli *cli = nouveau_cli(file_priv);
2fd3db6f
BS
126 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
127 struct nouveau_vma *vma;
128 int ret;
639212d0 129
3ee6f5b5 130 if (!cli->vm)
639212d0 131 return;
2fd3db6f 132
ee3939e0 133 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL);
2fd3db6f
BS
134 if (ret)
135 return;
136
3ee6f5b5 137 vma = nouveau_bo_vma_find(nvbo, cli->vm);
2fd3db6f 138 if (vma) {
c4c7044f
BS
139 if (--vma->refcount == 0)
140 nouveau_gem_object_unmap(nvbo, vma);
2fd3db6f
BS
141 }
142 ttm_bo_unreserve(&nvbo->bo);
639212d0
BS
143}
144
6ee73861 145int
f6d4e621
BS
146nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
147 uint32_t tile_mode, uint32_t tile_flags,
148 struct nouveau_bo **pnvbo)
6ee73861 149{
ebb945a9 150 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861 151 struct nouveau_bo *nvbo;
6ba9a683 152 u32 flags = 0;
6ee73861
BS
153 int ret;
154
6ba9a683
BS
155 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
156 flags |= TTM_PL_FLAG_VRAM;
157 if (domain & NOUVEAU_GEM_DOMAIN_GART)
158 flags |= TTM_PL_FLAG_TT;
159 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
160 flags |= TTM_PL_FLAG_SYSTEM;
161
7375c95b 162 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
22b33e8e 163 tile_flags, NULL, pnvbo);
6ee73861
BS
164 if (ret)
165 return ret;
166 nvbo = *pnvbo;
167
db5c8e29
BS
168 /* we restrict allowed domains on nv50+ to only the types
169 * that were requested at creation time. not possibly on
170 * earlier chips without busting the ABI.
171 */
172 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
173 NOUVEAU_GEM_DOMAIN_GART;
967e7bde 174 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
db5c8e29
BS
175 nvbo->valid_domains &= domain;
176
55fb74ad
DH
177 /* Initialize the embedded gem-object. We return a single gem-reference
178 * to the caller, instead of a normal nouveau_bo ttm reference. */
179 ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size);
180 if (ret) {
6ee73861
BS
181 nouveau_bo_ref(NULL, pnvbo);
182 return -ENOMEM;
183 }
184
55fb74ad 185 nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
6ee73861
BS
186 return 0;
187}
188
189static int
e758a311
BS
190nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
191 struct drm_nouveau_gem_info *rep)
6ee73861 192{
ebb945a9 193 struct nouveau_cli *cli = nouveau_cli(file_priv);
6ee73861 194 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
e758a311 195 struct nouveau_vma *vma;
6ee73861
BS
196
197 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
198 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
199 else
200 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
201
e758a311 202 rep->offset = nvbo->bo.offset;
3ee6f5b5
BS
203 if (cli->vm) {
204 vma = nouveau_bo_vma_find(nvbo, cli->vm);
e758a311
BS
205 if (!vma)
206 return -EINVAL;
207
208 rep->offset = vma->offset;
209 }
210
6ee73861 211 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
72525b3f 212 rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
6ee73861
BS
213 rep->tile_mode = nvbo->tile_mode;
214 rep->tile_flags = nvbo->tile_flags;
215 return 0;
216}
217
6ee73861
BS
218int
219nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
220 struct drm_file *file_priv)
221{
ebb945a9 222 struct nouveau_drm *drm = nouveau_drm(dev);
a84fa1a3 223 struct nouveau_cli *cli = nouveau_cli(file_priv);
967e7bde 224 struct nouveau_fb *pfb = nvkm_fb(&drm->device);
6ee73861
BS
225 struct drm_nouveau_gem_new *req = data;
226 struct nouveau_bo *nvbo = NULL;
6ee73861
BS
227 int ret = 0;
228
ebb945a9 229 if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
fa2bade9 230 NV_PRINTK(error, cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
6ee73861 231 return -EINVAL;
60d2a88a 232 }
6ee73861 233
f6d4e621 234 ret = nouveau_gem_new(dev, req->info.size, req->align,
6ba9a683
BS
235 req->info.domain, req->info.tile_mode,
236 req->info.tile_flags, &nvbo);
6ee73861
BS
237 if (ret)
238 return ret;
239
55fb74ad 240 ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
e758a311 241 if (ret == 0) {
55fb74ad 242 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
e758a311
BS
243 if (ret)
244 drm_gem_handle_delete(file_priv, req->info.handle);
245 }
246
29d08b3e 247 /* drop reference from allocate - handle holds it now */
55fb74ad 248 drm_gem_object_unreference_unlocked(&nvbo->gem);
6ee73861
BS
249 return ret;
250}
251
252static int
253nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
254 uint32_t write_domains, uint32_t valid_domains)
255{
55fb74ad 256 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
6ee73861 257 struct ttm_buffer_object *bo = &nvbo->bo;
db5c8e29 258 uint32_t domains = valid_domains & nvbo->valid_domains &
78ad0f7b
FJ
259 (write_domains ? write_domains : read_domains);
260 uint32_t pref_flags = 0, valid_flags = 0;
6ee73861 261
78ad0f7b 262 if (!domains)
6ee73861
BS
263 return -EINVAL;
264
78ad0f7b
FJ
265 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
266 valid_flags |= TTM_PL_FLAG_VRAM;
267
268 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
269 valid_flags |= TTM_PL_FLAG_TT;
270
271 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
272 bo->mem.mem_type == TTM_PL_VRAM)
273 pref_flags |= TTM_PL_FLAG_VRAM;
274
275 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
276 bo->mem.mem_type == TTM_PL_TT)
277 pref_flags |= TTM_PL_FLAG_TT;
278
279 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
280 pref_flags |= TTM_PL_FLAG_VRAM;
281
282 else
283 pref_flags |= TTM_PL_FLAG_TT;
284
285 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
6ee73861 286
6ee73861
BS
287 return 0;
288}
289
290struct validate_op {
6ee73861
BS
291 struct list_head vram_list;
292 struct list_head gart_list;
293 struct list_head both_list;
ecff665f 294 struct ww_acquire_ctx ticket;
6ee73861
BS
295};
296
297static void
ecff665f
ML
298validate_fini_list(struct list_head *list, struct nouveau_fence *fence,
299 struct ww_acquire_ctx *ticket)
6ee73861
BS
300{
301 struct list_head *entry, *tmp;
302 struct nouveau_bo *nvbo;
303
304 list_for_each_safe(entry, tmp, list) {
305 nvbo = list_entry(entry, struct nouveau_bo, entry);
332b242f 306
9360bd11
BS
307 if (likely(fence))
308 nouveau_bo_fence(nvbo, fence);
6ee73861 309
a1606a95
BS
310 if (unlikely(nvbo->validate_mapped)) {
311 ttm_bo_kunmap(&nvbo->kmap);
312 nvbo->validate_mapped = false;
313 }
314
6ee73861
BS
315 list_del(&nvbo->entry);
316 nvbo->reserved_by = NULL;
ecff665f 317 ttm_bo_unreserve_ticket(&nvbo->bo, ticket);
55fb74ad 318 drm_gem_object_unreference_unlocked(&nvbo->gem);
6ee73861
BS
319 }
320}
321
322static void
ecff665f 323validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence)
6ee73861 324{
ecff665f
ML
325 validate_fini_list(&op->vram_list, fence, &op->ticket);
326 validate_fini_list(&op->gart_list, fence, &op->ticket);
327 validate_fini_list(&op->both_list, fence, &op->ticket);
328}
329
330static void
331validate_fini(struct validate_op *op, struct nouveau_fence *fence)
332{
333 validate_fini_no_ticket(op, fence);
334 ww_acquire_fini(&op->ticket);
6ee73861
BS
335}
336
337static int
338validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
339 struct drm_nouveau_gem_pushbuf_bo *pbbo,
340 int nr_buffers, struct validate_op *op)
341{
a84fa1a3 342 struct nouveau_cli *cli = nouveau_cli(file_priv);
ebb945a9 343 struct drm_device *dev = chan->drm->dev;
6ee73861
BS
344 int trycnt = 0;
345 int ret, i;
c354c893 346 struct nouveau_bo *res_bo = NULL;
6ee73861 347
ecff665f 348 ww_acquire_init(&op->ticket, &reservation_ww_class);
6ee73861
BS
349retry:
350 if (++trycnt > 100000) {
fa2bade9 351 NV_PRINTK(error, cli, "%s failed and gave up.\n", __func__);
6ee73861
BS
352 return -EINVAL;
353 }
354
355 for (i = 0; i < nr_buffers; i++) {
356 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
357 struct drm_gem_object *gem;
358 struct nouveau_bo *nvbo;
359
360 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
361 if (!gem) {
fa2bade9 362 NV_PRINTK(error, cli, "Unknown handle 0x%08x\n", b->handle);
ecff665f 363 ww_acquire_done(&op->ticket);
6ee73861 364 validate_fini(op, NULL);
bf79cb91 365 return -ENOENT;
6ee73861 366 }
55fb74ad 367 nvbo = nouveau_gem_object(gem);
c354c893
ML
368 if (nvbo == res_bo) {
369 res_bo = NULL;
370 drm_gem_object_unreference_unlocked(gem);
371 continue;
372 }
6ee73861
BS
373
374 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
fa2bade9 375 NV_PRINTK(error, cli, "multiple instances of buffer %d on "
6ee73861 376 "validation list\n", b->handle);
5086f69e 377 drm_gem_object_unreference_unlocked(gem);
ecff665f 378 ww_acquire_done(&op->ticket);
6ee73861
BS
379 validate_fini(op, NULL);
380 return -EINVAL;
381 }
382
ecff665f 383 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
6ee73861 384 if (ret) {
ecff665f 385 validate_fini_no_ticket(op, NULL);
5e338405 386 if (unlikely(ret == -EDEADLK)) {
c354c893 387 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
ecff665f 388 &op->ticket);
c354c893
ML
389 if (!ret)
390 res_bo = nvbo;
391 }
938c40ed 392 if (unlikely(ret)) {
ecff665f
ML
393 ww_acquire_done(&op->ticket);
394 ww_acquire_fini(&op->ticket);
c354c893 395 drm_gem_object_unreference_unlocked(gem);
938c40ed 396 if (ret != -ERESTARTSYS)
fa2bade9 397 NV_PRINTK(error, cli, "fail reserve\n");
6ee73861 398 return ret;
a1606a95 399 }
6ee73861
BS
400 }
401
a1606a95 402 b->user_priv = (uint64_t)(unsigned long)nvbo;
6ee73861
BS
403 nvbo->reserved_by = file_priv;
404 nvbo->pbbo_index = i;
405 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
406 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
407 list_add_tail(&nvbo->entry, &op->both_list);
408 else
409 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
410 list_add_tail(&nvbo->entry, &op->vram_list);
411 else
412 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
413 list_add_tail(&nvbo->entry, &op->gart_list);
414 else {
fa2bade9 415 NV_PRINTK(error, cli, "invalid valid domains: 0x%08x\n",
6ee73861 416 b->valid_domains);
0208843d 417 list_add_tail(&nvbo->entry, &op->both_list);
ecff665f 418 ww_acquire_done(&op->ticket);
6ee73861
BS
419 validate_fini(op, NULL);
420 return -EINVAL;
421 }
c354c893
ML
422 if (nvbo == res_bo)
423 goto retry;
6ee73861
BS
424 }
425
ecff665f 426 ww_acquire_done(&op->ticket);
6ee73861
BS
427 return 0;
428}
429
525895ba
BS
430static int
431validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
432{
433 struct nouveau_fence *fence = NULL;
434 int ret = 0;
435
436 spin_lock(&nvbo->bo.bdev->fence_lock);
5d216f60 437 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
525895ba
BS
438 spin_unlock(&nvbo->bo.bdev->fence_lock);
439
440 if (fence) {
441 ret = nouveau_fence_sync(fence, chan);
442 nouveau_fence_unref(&fence);
443 }
444
445 return ret;
446}
447
6ee73861 448static int
a84fa1a3
MS
449validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
450 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
451 uint64_t user_pbbo_ptr)
6ee73861 452{
ebb945a9 453 struct nouveau_drm *drm = chan->drm;
6ee73861
BS
454 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
455 (void __force __user *)(uintptr_t)user_pbbo_ptr;
456 struct nouveau_bo *nvbo;
457 int ret, relocs = 0;
458
459 list_for_each_entry(nvbo, list, entry) {
460 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
6ee73861 461
55fb74ad 462 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
6ee73861
BS
463 b->write_domains,
464 b->valid_domains);
a1606a95 465 if (unlikely(ret)) {
fa2bade9 466 NV_PRINTK(error, cli, "fail set_domain\n");
6ee73861 467 return ret;
a1606a95 468 }
6ee73861 469
97a875cb 470 ret = nouveau_bo_validate(nvbo, true, false);
a1606a95 471 if (unlikely(ret)) {
938c40ed 472 if (ret != -ERESTARTSYS)
fa2bade9 473 NV_PRINTK(error, cli, "fail ttm_validate\n");
6ee73861 474 return ret;
a1606a95 475 }
6ee73861 476
525895ba 477 ret = validate_sync(chan, nvbo);
415e6186 478 if (unlikely(ret)) {
fa2bade9 479 NV_PRINTK(error, cli, "fail post-validate sync\n");
415e6186
BS
480 return ret;
481 }
482
967e7bde 483 if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
a3fcd0a9
BS
484 if (nvbo->bo.offset == b->presumed.offset &&
485 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
486 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
487 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
488 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
489 continue;
490
491 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
492 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
493 else
494 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
495 b->presumed.offset = nvbo->bo.offset;
496 b->presumed.valid = 0;
497 relocs++;
498
1d6ac185 499 if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
a3fcd0a9
BS
500 &b->presumed, sizeof(b->presumed)))
501 return -EFAULT;
502 }
6ee73861
BS
503 }
504
505 return relocs;
506}
507
508static int
509nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
510 struct drm_file *file_priv,
511 struct drm_nouveau_gem_pushbuf_bo *pbbo,
512 uint64_t user_buffers, int nr_buffers,
513 struct validate_op *op, int *apply_relocs)
514{
a84fa1a3 515 struct nouveau_cli *cli = nouveau_cli(file_priv);
6ee73861
BS
516 int ret, relocs = 0;
517
518 INIT_LIST_HEAD(&op->vram_list);
519 INIT_LIST_HEAD(&op->gart_list);
520 INIT_LIST_HEAD(&op->both_list);
521
6ee73861
BS
522 if (nr_buffers == 0)
523 return 0;
524
525 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
a1606a95 526 if (unlikely(ret)) {
938c40ed 527 if (ret != -ERESTARTSYS)
fa2bade9 528 NV_PRINTK(error, cli, "validate_init\n");
6ee73861 529 return ret;
a1606a95 530 }
6ee73861 531
a84fa1a3 532 ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
6ee73861 533 if (unlikely(ret < 0)) {
938c40ed 534 if (ret != -ERESTARTSYS)
fa2bade9 535 NV_PRINTK(error, cli, "validate vram_list\n");
6ee73861
BS
536 validate_fini(op, NULL);
537 return ret;
538 }
539 relocs += ret;
540
a84fa1a3 541 ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
6ee73861 542 if (unlikely(ret < 0)) {
938c40ed 543 if (ret != -ERESTARTSYS)
fa2bade9 544 NV_PRINTK(error, cli, "validate gart_list\n");
6ee73861
BS
545 validate_fini(op, NULL);
546 return ret;
547 }
548 relocs += ret;
549
a84fa1a3 550 ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
6ee73861 551 if (unlikely(ret < 0)) {
938c40ed 552 if (ret != -ERESTARTSYS)
fa2bade9 553 NV_PRINTK(error, cli, "validate both_list\n");
6ee73861
BS
554 validate_fini(op, NULL);
555 return ret;
556 }
557 relocs += ret;
558
559 *apply_relocs = relocs;
560 return 0;
561}
562
c859074e
ML
563static inline void
564u_free(void *addr)
565{
566 if (!is_vmalloc_addr(addr))
567 kfree(addr);
568 else
569 vfree(addr);
570}
571
6ee73861
BS
572static inline void *
573u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
574{
575 void *mem;
576 void __user *userptr = (void __force __user *)(uintptr_t)user;
577
c859074e
ML
578 size *= nmemb;
579
580 mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
581 if (!mem)
582 mem = vmalloc(size);
6ee73861
BS
583 if (!mem)
584 return ERR_PTR(-ENOMEM);
585
1d6ac185 586 if (copy_from_user(mem, userptr, size)) {
c859074e 587 u_free(mem);
6ee73861
BS
588 return ERR_PTR(-EFAULT);
589 }
590
591 return mem;
592}
593
594static int
a84fa1a3 595nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
a1606a95
BS
596 struct drm_nouveau_gem_pushbuf *req,
597 struct drm_nouveau_gem_pushbuf_bo *bo)
6ee73861
BS
598{
599 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
12f735b7
LB
600 int ret = 0;
601 unsigned i;
6ee73861 602
a1606a95 603 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
6ee73861
BS
604 if (IS_ERR(reloc))
605 return PTR_ERR(reloc);
606
a1606a95 607 for (i = 0; i < req->nr_relocs; i++) {
6ee73861
BS
608 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
609 struct drm_nouveau_gem_pushbuf_bo *b;
a1606a95 610 struct nouveau_bo *nvbo;
6ee73861
BS
611 uint32_t data;
612
a1606a95 613 if (unlikely(r->bo_index > req->nr_buffers)) {
fa2bade9 614 NV_PRINTK(error, cli, "reloc bo index invalid\n");
6ee73861
BS
615 ret = -EINVAL;
616 break;
617 }
618
619 b = &bo[r->bo_index];
a1606a95 620 if (b->presumed.valid)
6ee73861
BS
621 continue;
622
a1606a95 623 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
fa2bade9 624 NV_PRINTK(error, cli, "reloc container bo index invalid\n");
a1606a95
BS
625 ret = -EINVAL;
626 break;
627 }
628 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
629
630 if (unlikely(r->reloc_bo_offset + 4 >
631 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
fa2bade9 632 NV_PRINTK(error, cli, "reloc outside of bo\n");
a1606a95
BS
633 ret = -EINVAL;
634 break;
635 }
636
637 if (!nvbo->kmap.virtual) {
638 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
639 &nvbo->kmap);
640 if (ret) {
fa2bade9 641 NV_PRINTK(error, cli, "failed kmap for reloc\n");
a1606a95
BS
642 break;
643 }
644 nvbo->validate_mapped = true;
645 }
646
6ee73861 647 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
a1606a95 648 data = b->presumed.offset + r->data;
6ee73861
BS
649 else
650 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
a1606a95 651 data = (b->presumed.offset + r->data) >> 32;
6ee73861
BS
652 else
653 data = r->data;
654
655 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
a1606a95 656 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
6ee73861
BS
657 data |= r->tor;
658 else
659 data |= r->vor;
660 }
661
702adba2 662 spin_lock(&nvbo->bo.bdev->fence_lock);
a1606a95 663 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
702adba2 664 spin_unlock(&nvbo->bo.bdev->fence_lock);
a1606a95 665 if (ret) {
fa2bade9 666 NV_PRINTK(error, cli, "reloc wait_idle failed: %d\n", ret);
a1606a95
BS
667 break;
668 }
a1606a95
BS
669
670 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
6ee73861
BS
671 }
672
c859074e 673 u_free(reloc);
6ee73861
BS
674 return ret;
675}
676
677int
678nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
679 struct drm_file *file_priv)
680{
ebb945a9 681 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
a84fa1a3 682 struct nouveau_cli *cli = nouveau_cli(file_priv);
ebb945a9
BS
683 struct nouveau_abi16_chan *temp;
684 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861 685 struct drm_nouveau_gem_pushbuf *req = data;
a1606a95
BS
686 struct drm_nouveau_gem_pushbuf_push *push;
687 struct drm_nouveau_gem_pushbuf_bo *bo;
ebb945a9 688 struct nouveau_channel *chan = NULL;
6ee73861 689 struct validate_op op;
6e86e041 690 struct nouveau_fence *fence = NULL;
a1606a95 691 int i, j, ret = 0, do_reloc = 0;
6ee73861 692
ebb945a9
BS
693 if (unlikely(!abi16))
694 return -ENOMEM;
6ee73861 695
ebb945a9 696 list_for_each_entry(temp, &abi16->channels, head) {
0ad72863 697 if (temp->chan->object->handle == (NVDRM_CHAN | req->channel)) {
ebb945a9
BS
698 chan = temp->chan;
699 break;
700 }
701 }
6ee73861 702
ebb945a9
BS
703 if (!chan)
704 return nouveau_abi16_put(abi16, -ENOENT);
705
706 req->vram_available = drm->gem.vram_available;
707 req->gart_available = drm->gem.gart_available;
a1606a95
BS
708 if (unlikely(req->nr_push == 0))
709 goto out_next;
6ee73861 710
a1606a95 711 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
fa2bade9 712 NV_PRINTK(error, cli, "pushbuf push count exceeds limit: %d max %d\n",
a1606a95 713 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
ebb945a9 714 return nouveau_abi16_put(abi16, -EINVAL);
6ee73861
BS
715 }
716
a1606a95 717 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
fa2bade9 718 NV_PRINTK(error, cli, "pushbuf bo count exceeds limit: %d max %d\n",
a1606a95 719 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
ebb945a9 720 return nouveau_abi16_put(abi16, -EINVAL);
6ee73861
BS
721 }
722
a1606a95 723 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
fa2bade9 724 NV_PRINTK(error, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
a1606a95 725 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
ebb945a9 726 return nouveau_abi16_put(abi16, -EINVAL);
6ee73861
BS
727 }
728
a1606a95 729 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
ebb945a9
BS
730 if (IS_ERR(push))
731 return nouveau_abi16_put(abi16, PTR_ERR(push));
a1606a95 732
6ee73861 733 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
a1606a95 734 if (IS_ERR(bo)) {
c859074e 735 u_free(push);
ebb945a9 736 return nouveau_abi16_put(abi16, PTR_ERR(bo));
a1606a95 737 }
6ee73861 738
accf9496 739 /* Ensure all push buffers are on validate list */
415e6186
BS
740 for (i = 0; i < req->nr_push; i++) {
741 if (push[i].bo_index >= req->nr_buffers) {
fa2bade9 742 NV_PRINTK(error, cli, "push %d buffer not in list\n", i);
415e6186 743 ret = -EINVAL;
7fa0cba2 744 goto out_prevalid;
415e6186 745 }
415e6186
BS
746 }
747
6ee73861
BS
748 /* Validate buffer list */
749 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
750 req->nr_buffers, &op, &do_reloc);
751 if (ret) {
938c40ed 752 if (ret != -ERESTARTSYS)
fa2bade9 753 NV_PRINTK(error, cli, "validate: %d\n", ret);
7fa0cba2 754 goto out_prevalid;
6ee73861
BS
755 }
756
6ee73861
BS
757 /* Apply any relocations that are required */
758 if (do_reloc) {
a84fa1a3 759 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
6ee73861 760 if (ret) {
fa2bade9 761 NV_PRINTK(error, cli, "reloc apply: %d\n", ret);
6ee73861
BS
762 goto out;
763 }
6ee73861 764 }
6ee73861 765
9a391ad8 766 if (chan->dma.ib_max) {
5e120f6e 767 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
6ee73861 768 if (ret) {
fa2bade9 769 NV_PRINTK(error, cli, "nv50cal_space: %d\n", ret);
6ee73861
BS
770 goto out;
771 }
6ee73861 772
a1606a95
BS
773 for (i = 0; i < req->nr_push; i++) {
774 struct nouveau_bo *nvbo = (void *)(unsigned long)
775 bo[push[i].bo_index].user_priv;
776
777 nv50_dma_push(chan, nvbo, push[i].offset,
778 push[i].length);
779 }
9a391ad8 780 } else
967e7bde 781 if (drm->device.info.chipset >= 0x25) {
a1606a95 782 ret = RING_SPACE(chan, req->nr_push * 2);
6ee73861 783 if (ret) {
fa2bade9 784 NV_PRINTK(error, cli, "cal_space: %d\n", ret);
6ee73861
BS
785 goto out;
786 }
a1606a95
BS
787
788 for (i = 0; i < req->nr_push; i++) {
789 struct nouveau_bo *nvbo = (void *)(unsigned long)
790 bo[push[i].bo_index].user_priv;
a1606a95 791
3a92d37e 792 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
a1606a95
BS
793 OUT_RING(chan, 0);
794 }
6ee73861 795 } else {
a1606a95 796 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
6ee73861 797 if (ret) {
fa2bade9 798 NV_PRINTK(error, cli, "jmp_space: %d\n", ret);
6ee73861
BS
799 goto out;
800 }
6ee73861 801
a1606a95
BS
802 for (i = 0; i < req->nr_push; i++) {
803 struct nouveau_bo *nvbo = (void *)(unsigned long)
804 bo[push[i].bo_index].user_priv;
a1606a95
BS
805 uint32_t cmd;
806
ebb945a9 807 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
a1606a95
BS
808 cmd |= 0x20000000;
809 if (unlikely(cmd != req->suffix0)) {
810 if (!nvbo->kmap.virtual) {
811 ret = ttm_bo_kmap(&nvbo->bo, 0,
812 nvbo->bo.mem.
813 num_pages,
814 &nvbo->kmap);
815 if (ret) {
816 WIND_RING(chan);
817 goto out;
818 }
819 nvbo->validate_mapped = true;
820 }
821
822 nouveau_bo_wr32(nvbo, (push[i].offset +
823 push[i].length - 8) / 4, cmd);
824 }
825
3a92d37e
BS
826 OUT_RING(chan, 0x20000000 |
827 (nvbo->bo.offset + push[i].offset));
6ee73861 828 OUT_RING(chan, 0);
a1606a95
BS
829 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
830 OUT_RING(chan, 0);
831 }
6ee73861
BS
832 }
833
264ce192 834 ret = nouveau_fence_new(chan, false, &fence);
6ee73861 835 if (ret) {
fa2bade9 836 NV_PRINTK(error, cli, "error fencing pushbuf: %d\n", ret);
6ee73861
BS
837 WIND_RING(chan);
838 goto out;
839 }
840
841out:
234896a7 842 validate_fini(&op, fence);
382d62e5 843 nouveau_fence_unref(&fence);
7fa0cba2
MS
844
845out_prevalid:
c859074e
ML
846 u_free(bo);
847 u_free(push);
6ee73861
BS
848
849out_next:
9a391ad8
BS
850 if (chan->dma.ib_max) {
851 req->suffix0 = 0x00000000;
852 req->suffix1 = 0x00000000;
853 } else
967e7bde 854 if (drm->device.info.chipset >= 0x25) {
6ee73861
BS
855 req->suffix0 = 0x00020000;
856 req->suffix1 = 0x00000000;
857 } else {
858 req->suffix0 = 0x20000000 |
ebb945a9 859 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
6ee73861
BS
860 req->suffix1 = 0x00000000;
861 }
862
ebb945a9 863 return nouveau_abi16_put(abi16, ret);
6ee73861
BS
864}
865
6ee73861
BS
866static inline uint32_t
867domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
868{
869 uint32_t flags = 0;
870
871 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
872 flags |= TTM_PL_FLAG_VRAM;
873 if (domain & NOUVEAU_GEM_DOMAIN_GART)
874 flags |= TTM_PL_FLAG_TT;
875
876 return flags;
877}
878
6ee73861
BS
879int
880nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
881 struct drm_file *file_priv)
882{
883 struct drm_nouveau_gem_cpu_prep *req = data;
884 struct drm_gem_object *gem;
885 struct nouveau_bo *nvbo;
886 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
887 int ret = -EINVAL;
888
6ee73861
BS
889 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
890 if (!gem)
bf79cb91 891 return -ENOENT;
6ee73861
BS
892 nvbo = nouveau_gem_object(gem);
893
21e86c1c
BS
894 spin_lock(&nvbo->bo.bdev->fence_lock);
895 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
896 spin_unlock(&nvbo->bo.bdev->fence_lock);
bc9025bd 897 drm_gem_object_unreference_unlocked(gem);
6ee73861
BS
898 return ret;
899}
900
901int
902nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
903 struct drm_file *file_priv)
904{
21e86c1c 905 return 0;
6ee73861
BS
906}
907
908int
909nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
910 struct drm_file *file_priv)
911{
912 struct drm_nouveau_gem_info *req = data;
913 struct drm_gem_object *gem;
914 int ret;
915
6ee73861
BS
916 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
917 if (!gem)
bf79cb91 918 return -ENOENT;
6ee73861 919
e758a311 920 ret = nouveau_gem_info(file_priv, gem, req);
bc9025bd 921 drm_gem_object_unreference_unlocked(gem);
6ee73861
BS
922 return ret;
923}
924
This page took 0.408332 seconds and 5 git commands to generate.