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