]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_gem.c
drm/nouveau: new gem pushbuf interface, bump to 0.0.16
[mirror_ubuntu-artful-kernel.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
a1606a95
BS
246 if (unlikely(nvbo->validate_mapped)) {
247 ttm_bo_kunmap(&nvbo->kmap);
248 nvbo->validate_mapped = false;
249 }
250
6ee73861
BS
251 list_del(&nvbo->entry);
252 nvbo->reserved_by = NULL;
253 ttm_bo_unreserve(&nvbo->bo);
254 drm_gem_object_unreference(nvbo->gem);
255 }
256}
257
258static void
234896a7 259validate_fini(struct validate_op *op, struct nouveau_fence* fence)
6ee73861 260{
234896a7
LB
261 validate_fini_list(&op->vram_list, fence);
262 validate_fini_list(&op->gart_list, fence);
263 validate_fini_list(&op->both_list, fence);
6ee73861
BS
264}
265
266static int
267validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
268 struct drm_nouveau_gem_pushbuf_bo *pbbo,
269 int nr_buffers, struct validate_op *op)
270{
271 struct drm_device *dev = chan->dev;
272 struct drm_nouveau_private *dev_priv = dev->dev_private;
273 uint32_t sequence;
274 int trycnt = 0;
275 int ret, i;
276
277 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
278retry:
279 if (++trycnt > 100000) {
280 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
281 return -EINVAL;
282 }
283
284 for (i = 0; i < nr_buffers; i++) {
285 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
286 struct drm_gem_object *gem;
287 struct nouveau_bo *nvbo;
288
289 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
290 if (!gem) {
291 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
292 validate_fini(op, NULL);
293 return -EINVAL;
294 }
295 nvbo = gem->driver_private;
296
297 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
298 NV_ERROR(dev, "multiple instances of buffer %d on "
299 "validation list\n", b->handle);
300 validate_fini(op, NULL);
301 return -EINVAL;
302 }
303
304 ret = ttm_bo_reserve(&nvbo->bo, false, false, true, sequence);
305 if (ret) {
306 validate_fini(op, NULL);
307 if (ret == -EAGAIN)
308 ret = ttm_bo_wait_unreserved(&nvbo->bo, false);
309 drm_gem_object_unreference(gem);
a1606a95
BS
310 if (ret) {
311 NV_ERROR(dev, "fail reserve\n");
6ee73861 312 return ret;
a1606a95 313 }
6ee73861
BS
314 goto retry;
315 }
316
a1606a95 317 b->user_priv = (uint64_t)(unsigned long)nvbo;
6ee73861
BS
318 nvbo->reserved_by = file_priv;
319 nvbo->pbbo_index = i;
320 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
321 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
322 list_add_tail(&nvbo->entry, &op->both_list);
323 else
324 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
325 list_add_tail(&nvbo->entry, &op->vram_list);
326 else
327 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
328 list_add_tail(&nvbo->entry, &op->gart_list);
329 else {
330 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
331 b->valid_domains);
0208843d 332 list_add_tail(&nvbo->entry, &op->both_list);
6ee73861
BS
333 validate_fini(op, NULL);
334 return -EINVAL;
335 }
336
337 if (unlikely(atomic_read(&nvbo->bo.cpu_writers) > 0)) {
338 validate_fini(op, NULL);
339
340 if (nvbo->cpu_filp == file_priv) {
341 NV_ERROR(dev, "bo %p mapped by process trying "
342 "to validate it!\n", nvbo);
343 return -EINVAL;
344 }
345
346 ret = ttm_bo_wait_cpu(&nvbo->bo, false);
a1606a95
BS
347 if (ret) {
348 NV_ERROR(dev, "fail wait_cpu\n");
6ee73861 349 return ret;
a1606a95 350 }
6ee73861
BS
351 goto retry;
352 }
353 }
354
355 return 0;
356}
357
358static int
359validate_list(struct nouveau_channel *chan, struct list_head *list,
360 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
361{
362 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
363 (void __force __user *)(uintptr_t)user_pbbo_ptr;
a1606a95 364 struct drm_device *dev = chan->dev;
6ee73861
BS
365 struct nouveau_bo *nvbo;
366 int ret, relocs = 0;
367
368 list_for_each_entry(nvbo, list, entry) {
369 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
370 struct nouveau_fence *prev_fence = nvbo->bo.sync_obj;
371
372 if (prev_fence && nouveau_fence_channel(prev_fence) != chan) {
373 spin_lock(&nvbo->bo.lock);
374 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
375 spin_unlock(&nvbo->bo.lock);
a1606a95
BS
376 if (unlikely(ret)) {
377 NV_ERROR(dev, "fail wait other chan\n");
6ee73861 378 return ret;
a1606a95 379 }
6ee73861
BS
380 }
381
382 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
383 b->write_domains,
384 b->valid_domains);
a1606a95
BS
385 if (unlikely(ret)) {
386 NV_ERROR(dev, "fail set_domain\n");
6ee73861 387 return ret;
a1606a95 388 }
6ee73861
BS
389
390 nvbo->channel = chan;
391 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
392 false, false);
393 nvbo->channel = NULL;
a1606a95
BS
394 if (unlikely(ret)) {
395 NV_ERROR(dev, "fail ttm_validate\n");
6ee73861 396 return ret;
a1606a95 397 }
6ee73861 398
a1606a95 399 if (nvbo->bo.offset == b->presumed.offset &&
6ee73861 400 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
a1606a95 401 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
6ee73861 402 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
a1606a95 403 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
6ee73861
BS
404 continue;
405
406 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
a1606a95 407 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
6ee73861 408 else
a1606a95
BS
409 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
410 b->presumed.offset = nvbo->bo.offset;
411 b->presumed.valid = 0;
6ee73861
BS
412 relocs++;
413
a1606a95
BS
414 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
415 &b->presumed, sizeof(b->presumed)))
6ee73861
BS
416 return -EFAULT;
417 }
418
419 return relocs;
420}
421
422static int
423nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
424 struct drm_file *file_priv,
425 struct drm_nouveau_gem_pushbuf_bo *pbbo,
426 uint64_t user_buffers, int nr_buffers,
427 struct validate_op *op, int *apply_relocs)
428{
a1606a95 429 struct drm_device *dev = chan->dev;
6ee73861
BS
430 int ret, relocs = 0;
431
432 INIT_LIST_HEAD(&op->vram_list);
433 INIT_LIST_HEAD(&op->gart_list);
434 INIT_LIST_HEAD(&op->both_list);
435
6ee73861
BS
436 if (nr_buffers == 0)
437 return 0;
438
439 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
a1606a95
BS
440 if (unlikely(ret)) {
441 NV_ERROR(dev, "validate_init\n");
6ee73861 442 return ret;
a1606a95 443 }
6ee73861
BS
444
445 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
446 if (unlikely(ret < 0)) {
a1606a95 447 NV_ERROR(dev, "validate vram_list\n");
6ee73861
BS
448 validate_fini(op, NULL);
449 return ret;
450 }
451 relocs += ret;
452
453 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
454 if (unlikely(ret < 0)) {
a1606a95 455 NV_ERROR(dev, "validate gart_list\n");
6ee73861
BS
456 validate_fini(op, NULL);
457 return ret;
458 }
459 relocs += ret;
460
461 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
462 if (unlikely(ret < 0)) {
a1606a95 463 NV_ERROR(dev, "validate both_list\n");
6ee73861
BS
464 validate_fini(op, NULL);
465 return ret;
466 }
467 relocs += ret;
468
469 *apply_relocs = relocs;
470 return 0;
471}
472
473static inline void *
474u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
475{
476 void *mem;
477 void __user *userptr = (void __force __user *)(uintptr_t)user;
478
479 mem = kmalloc(nmemb * size, GFP_KERNEL);
480 if (!mem)
481 return ERR_PTR(-ENOMEM);
482
483 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
484 kfree(mem);
485 return ERR_PTR(-EFAULT);
486 }
487
488 return mem;
489}
490
491static int
a1606a95
BS
492nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
493 struct drm_nouveau_gem_pushbuf *req,
494 struct drm_nouveau_gem_pushbuf_bo *bo)
6ee73861
BS
495{
496 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
12f735b7
LB
497 int ret = 0;
498 unsigned i;
6ee73861 499
a1606a95 500 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
6ee73861
BS
501 if (IS_ERR(reloc))
502 return PTR_ERR(reloc);
503
a1606a95 504 for (i = 0; i < req->nr_relocs; i++) {
6ee73861
BS
505 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
506 struct drm_nouveau_gem_pushbuf_bo *b;
a1606a95 507 struct nouveau_bo *nvbo;
6ee73861
BS
508 uint32_t data;
509
a1606a95
BS
510 if (unlikely(r->bo_index > req->nr_buffers)) {
511 NV_ERROR(dev, "reloc bo index invalid\n");
6ee73861
BS
512 ret = -EINVAL;
513 break;
514 }
515
516 b = &bo[r->bo_index];
a1606a95 517 if (b->presumed.valid)
6ee73861
BS
518 continue;
519
a1606a95
BS
520 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
521 NV_ERROR(dev, "reloc container bo index invalid\n");
522 ret = -EINVAL;
523 break;
524 }
525 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
526
527 if (unlikely(r->reloc_bo_offset + 4 >
528 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
529 NV_ERROR(dev, "reloc outside of bo\n");
530 ret = -EINVAL;
531 break;
532 }
533
534 if (!nvbo->kmap.virtual) {
535 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
536 &nvbo->kmap);
537 if (ret) {
538 NV_ERROR(dev, "failed kmap for reloc\n");
539 break;
540 }
541 nvbo->validate_mapped = true;
542 }
543
6ee73861 544 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
a1606a95 545 data = b->presumed.offset + r->data;
6ee73861
BS
546 else
547 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
a1606a95 548 data = (b->presumed.offset + r->data) >> 32;
6ee73861
BS
549 else
550 data = r->data;
551
552 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
a1606a95 553 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
6ee73861
BS
554 data |= r->tor;
555 else
556 data |= r->vor;
557 }
558
a1606a95
BS
559 spin_lock(&nvbo->bo.lock);
560 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
561 if (ret) {
562 NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret);
563 break;
564 }
565 spin_unlock(&nvbo->bo.lock);
566
567 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
6ee73861
BS
568 }
569
570 kfree(reloc);
571 return ret;
572}
573
574int
575nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
576 struct drm_file *file_priv)
577{
a1606a95 578 struct drm_nouveau_private *dev_priv = dev->dev_private;
6ee73861 579 struct drm_nouveau_gem_pushbuf *req = data;
a1606a95
BS
580 struct drm_nouveau_gem_pushbuf_push *push;
581 struct drm_nouveau_gem_pushbuf_bo *bo;
6ee73861
BS
582 struct nouveau_channel *chan;
583 struct validate_op op;
a1606a95
BS
584 struct nouveau_fence *fence = 0;
585 int i, j, ret = 0, do_reloc = 0;
6ee73861
BS
586
587 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
588 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan);
589
a1606a95
BS
590 req->vram_available = dev_priv->fb_aper_free;
591 req->gart_available = dev_priv->gart_info.aper_free;
592 if (unlikely(req->nr_push == 0))
593 goto out_next;
6ee73861 594
a1606a95
BS
595 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
596 NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n",
597 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
598 return -EINVAL;
6ee73861
BS
599 }
600
a1606a95
BS
601 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
602 NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n",
603 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
604 return -EINVAL;
6ee73861
BS
605 }
606
a1606a95
BS
607 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
608 NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n",
609 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
6ee73861
BS
610 return -EINVAL;
611 }
612
a1606a95
BS
613 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
614 if (IS_ERR(push))
615 return PTR_ERR(push);
616
6ee73861 617 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
a1606a95
BS
618 if (IS_ERR(bo)) {
619 kfree(push);
6ee73861 620 return PTR_ERR(bo);
a1606a95 621 }
6ee73861
BS
622
623 mutex_lock(&dev->struct_mutex);
624
625 /* Validate buffer list */
626 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
627 req->nr_buffers, &op, &do_reloc);
628 if (ret) {
629 NV_ERROR(dev, "validate: %d\n", ret);
630 goto out;
631 }
632
6ee73861
BS
633 /* Apply any relocations that are required */
634 if (do_reloc) {
a1606a95 635 ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo);
6ee73861
BS
636 if (ret) {
637 NV_ERROR(dev, "reloc apply: %d\n", ret);
638 goto out;
639 }
640 }
641
9a391ad8 642 if (chan->dma.ib_max) {
a1606a95 643 ret = nouveau_dma_wait(chan, req->nr_push + 1, 6);
9a391ad8
BS
644 if (ret) {
645 NV_INFO(dev, "nv50cal_space: %d\n", ret);
646 goto out;
647 }
648
a1606a95
BS
649 for (i = 0; i < req->nr_push; i++) {
650 struct nouveau_bo *nvbo = (void *)(unsigned long)
651 bo[push[i].bo_index].user_priv;
652
653 nv50_dma_push(chan, nvbo, push[i].offset,
654 push[i].length);
655 }
9a391ad8 656 } else
2ccb04ec 657 if (dev_priv->card_type >= NV_20) {
a1606a95 658 ret = RING_SPACE(chan, req->nr_push * 2);
6ee73861
BS
659 if (ret) {
660 NV_ERROR(dev, "cal_space: %d\n", ret);
661 goto out;
662 }
a1606a95
BS
663
664 for (i = 0; i < req->nr_push; i++) {
665 struct nouveau_bo *nvbo = (void *)(unsigned long)
666 bo[push[i].bo_index].user_priv;
667 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
668
669 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
670 push[i].offset) | 2);
671 OUT_RING(chan, 0);
672 }
6ee73861 673 } else {
a1606a95 674 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
6ee73861
BS
675 if (ret) {
676 NV_ERROR(dev, "jmp_space: %d\n", ret);
677 goto out;
678 }
6ee73861 679
a1606a95
BS
680 for (i = 0; i < req->nr_push; i++) {
681 struct nouveau_bo *nvbo = (void *)(unsigned long)
682 bo[push[i].bo_index].user_priv;
683 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
684 uint32_t cmd;
685
686 cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
687 cmd |= 0x20000000;
688 if (unlikely(cmd != req->suffix0)) {
689 if (!nvbo->kmap.virtual) {
690 ret = ttm_bo_kmap(&nvbo->bo, 0,
691 nvbo->bo.mem.
692 num_pages,
693 &nvbo->kmap);
694 if (ret) {
695 WIND_RING(chan);
696 goto out;
697 }
698 nvbo->validate_mapped = true;
699 }
700
701 nouveau_bo_wr32(nvbo, (push[i].offset +
702 push[i].length - 8) / 4, cmd);
703 }
704
705 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
706 push[i].offset) | 0x20000000);
6ee73861 707 OUT_RING(chan, 0);
a1606a95
BS
708 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
709 OUT_RING(chan, 0);
710 }
6ee73861
BS
711 }
712
234896a7 713 ret = nouveau_fence_new(chan, &fence, true);
6ee73861
BS
714 if (ret) {
715 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
716 WIND_RING(chan);
717 goto out;
718 }
719
720out:
234896a7
LB
721 validate_fini(&op, fence);
722 nouveau_fence_unref((void**)&fence);
6ee73861
BS
723 mutex_unlock(&dev->struct_mutex);
724 kfree(bo);
a1606a95 725 kfree(push);
6ee73861
BS
726
727out_next:
9a391ad8
BS
728 if (chan->dma.ib_max) {
729 req->suffix0 = 0x00000000;
730 req->suffix1 = 0x00000000;
731 } else
2ccb04ec 732 if (dev_priv->card_type >= NV_20) {
6ee73861
BS
733 req->suffix0 = 0x00020000;
734 req->suffix1 = 0x00000000;
735 } else {
736 req->suffix0 = 0x20000000 |
737 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
738 req->suffix1 = 0x00000000;
739 }
740
741 return ret;
742}
743
6ee73861
BS
744static inline uint32_t
745domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
746{
747 uint32_t flags = 0;
748
749 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
750 flags |= TTM_PL_FLAG_VRAM;
751 if (domain & NOUVEAU_GEM_DOMAIN_GART)
752 flags |= TTM_PL_FLAG_TT;
753
754 return flags;
755}
756
6ee73861
BS
757int
758nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
759 struct drm_file *file_priv)
760{
761 struct drm_nouveau_gem_cpu_prep *req = data;
762 struct drm_gem_object *gem;
763 struct nouveau_bo *nvbo;
764 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
765 int ret = -EINVAL;
766
767 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
768
769 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
770 if (!gem)
771 return ret;
772 nvbo = nouveau_gem_object(gem);
773
774 if (nvbo->cpu_filp) {
775 if (nvbo->cpu_filp == file_priv)
776 goto out;
777
778 ret = ttm_bo_wait_cpu(&nvbo->bo, no_wait);
6ee73861
BS
779 if (ret)
780 goto out;
781 }
782
783 if (req->flags & NOUVEAU_GEM_CPU_PREP_NOBLOCK) {
f0fbe3eb 784 spin_lock(&nvbo->bo.lock);
6ee73861 785 ret = ttm_bo_wait(&nvbo->bo, false, false, no_wait);
f0fbe3eb 786 spin_unlock(&nvbo->bo.lock);
6ee73861
BS
787 } else {
788 ret = ttm_bo_synccpu_write_grab(&nvbo->bo, no_wait);
6ee73861
BS
789 if (ret == 0)
790 nvbo->cpu_filp = file_priv;
791 }
792
793out:
794 mutex_lock(&dev->struct_mutex);
795 drm_gem_object_unreference(gem);
796 mutex_unlock(&dev->struct_mutex);
797 return ret;
798}
799
800int
801nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
802 struct drm_file *file_priv)
803{
804 struct drm_nouveau_gem_cpu_prep *req = data;
805 struct drm_gem_object *gem;
806 struct nouveau_bo *nvbo;
807 int ret = -EINVAL;
808
809 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
810
811 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
812 if (!gem)
813 return ret;
814 nvbo = nouveau_gem_object(gem);
815
816 if (nvbo->cpu_filp != file_priv)
817 goto out;
818 nvbo->cpu_filp = NULL;
819
820 ttm_bo_synccpu_write_release(&nvbo->bo);
821 ret = 0;
822
823out:
824 mutex_lock(&dev->struct_mutex);
825 drm_gem_object_unreference(gem);
826 mutex_unlock(&dev->struct_mutex);
827 return ret;
828}
829
830int
831nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
832 struct drm_file *file_priv)
833{
834 struct drm_nouveau_gem_info *req = data;
835 struct drm_gem_object *gem;
836 int ret;
837
838 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
839
840 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
841 if (!gem)
842 return -EINVAL;
843
844 ret = nouveau_gem_info(gem, req);
845 mutex_lock(&dev->struct_mutex);
846 drm_gem_object_unreference(gem);
847 mutex_unlock(&dev->struct_mutex);
848 return ret;
849}
850