drm/nouveau: Fix pageflip event
[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
6ee73861
BS
51 if (unlikely(nvbo->pin_refcnt)) {
52 nvbo->pin_refcnt = 1;
53 nouveau_bo_unpin(nvbo);
54 }
55
56 ttm_bo_unref(&bo);
fd632aa3
DV
57
58 drm_gem_object_release(gem);
59 kfree(gem);
6ee73861
BS
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
6ee73861
BS
106int
107nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
108 struct drm_file *file_priv)
109{
110 struct drm_nouveau_private *dev_priv = dev->dev_private;
111 struct drm_nouveau_gem_new *req = data;
112 struct nouveau_bo *nvbo = NULL;
113 struct nouveau_channel *chan = NULL;
114 uint32_t flags = 0;
115 int ret = 0;
116
6ee73861
BS
117 if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL))
118 dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping;
119
6ee73861
BS
120 if (req->info.domain & NOUVEAU_GEM_DOMAIN_VRAM)
121 flags |= TTM_PL_FLAG_VRAM;
122 if (req->info.domain & NOUVEAU_GEM_DOMAIN_GART)
123 flags |= TTM_PL_FLAG_TT;
124 if (!flags || req->info.domain & NOUVEAU_GEM_DOMAIN_CPU)
125 flags |= TTM_PL_FLAG_SYSTEM;
126
60d2a88a
BS
127 if (!dev_priv->engine.vram.flags_valid(dev, req->info.tile_flags)) {
128 NV_ERROR(dev, "bad page flags: 0x%08x\n", req->info.tile_flags);
6ee73861 129 return -EINVAL;
60d2a88a 130 }
6ee73861 131
cff5c133
BS
132 if (req->channel_hint) {
133 chan = nouveau_channel_get(dev, file_priv, req->channel_hint);
134 if (IS_ERR(chan))
135 return PTR_ERR(chan);
136 }
137
6ee73861
BS
138 ret = nouveau_gem_new(dev, chan, req->info.size, req->align, flags,
139 req->info.tile_mode, req->info.tile_flags, false,
140 (req->info.domain & NOUVEAU_GEM_DOMAIN_MAPPABLE),
141 &nvbo);
cff5c133
BS
142 if (chan)
143 nouveau_channel_put(&chan);
6ee73861
BS
144 if (ret)
145 return ret;
146
147 ret = nouveau_gem_info(nvbo->gem, &req->info);
148 if (ret)
149 goto out;
150
151 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
29d08b3e
DA
152 /* drop reference from allocate - handle holds it now */
153 drm_gem_object_unreference_unlocked(nvbo->gem);
6ee73861 154out:
6ee73861
BS
155 return ret;
156}
157
158static int
159nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
160 uint32_t write_domains, uint32_t valid_domains)
161{
162 struct nouveau_bo *nvbo = gem->driver_private;
163 struct ttm_buffer_object *bo = &nvbo->bo;
78ad0f7b
FJ
164 uint32_t domains = valid_domains &
165 (write_domains ? write_domains : read_domains);
166 uint32_t pref_flags = 0, valid_flags = 0;
6ee73861 167
78ad0f7b 168 if (!domains)
6ee73861
BS
169 return -EINVAL;
170
78ad0f7b
FJ
171 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
172 valid_flags |= TTM_PL_FLAG_VRAM;
173
174 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
175 valid_flags |= TTM_PL_FLAG_TT;
176
177 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
178 bo->mem.mem_type == TTM_PL_VRAM)
179 pref_flags |= TTM_PL_FLAG_VRAM;
180
181 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
182 bo->mem.mem_type == TTM_PL_TT)
183 pref_flags |= TTM_PL_FLAG_TT;
184
185 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
186 pref_flags |= TTM_PL_FLAG_VRAM;
187
188 else
189 pref_flags |= TTM_PL_FLAG_TT;
190
191 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
6ee73861 192
6ee73861
BS
193 return 0;
194}
195
196struct validate_op {
6ee73861
BS
197 struct list_head vram_list;
198 struct list_head gart_list;
199 struct list_head both_list;
200};
201
202static void
203validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
204{
205 struct list_head *entry, *tmp;
206 struct nouveau_bo *nvbo;
207
208 list_for_each_safe(entry, tmp, list) {
209 nvbo = list_entry(entry, struct nouveau_bo, entry);
332b242f
FJ
210
211 nouveau_bo_fence(nvbo, fence);
6ee73861 212
a1606a95
BS
213 if (unlikely(nvbo->validate_mapped)) {
214 ttm_bo_kunmap(&nvbo->kmap);
215 nvbo->validate_mapped = false;
216 }
217
6ee73861
BS
218 list_del(&nvbo->entry);
219 nvbo->reserved_by = NULL;
220 ttm_bo_unreserve(&nvbo->bo);
374c3af8 221 drm_gem_object_unreference_unlocked(nvbo->gem);
6ee73861
BS
222 }
223}
224
225static void
234896a7 226validate_fini(struct validate_op *op, struct nouveau_fence* fence)
6ee73861 227{
234896a7
LB
228 validate_fini_list(&op->vram_list, fence);
229 validate_fini_list(&op->gart_list, fence);
230 validate_fini_list(&op->both_list, fence);
6ee73861
BS
231}
232
233static int
234validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
235 struct drm_nouveau_gem_pushbuf_bo *pbbo,
236 int nr_buffers, struct validate_op *op)
237{
238 struct drm_device *dev = chan->dev;
239 struct drm_nouveau_private *dev_priv = dev->dev_private;
240 uint32_t sequence;
241 int trycnt = 0;
242 int ret, i;
243
244 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
245retry:
246 if (++trycnt > 100000) {
247 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
248 return -EINVAL;
249 }
250
251 for (i = 0; i < nr_buffers; i++) {
252 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
253 struct drm_gem_object *gem;
254 struct nouveau_bo *nvbo;
255
256 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
257 if (!gem) {
258 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
259 validate_fini(op, NULL);
bf79cb91 260 return -ENOENT;
6ee73861
BS
261 }
262 nvbo = gem->driver_private;
263
264 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
265 NV_ERROR(dev, "multiple instances of buffer %d on "
266 "validation list\n", b->handle);
267 validate_fini(op, NULL);
268 return -EINVAL;
269 }
270
938c40ed 271 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
6ee73861
BS
272 if (ret) {
273 validate_fini(op, NULL);
938c40ed
BS
274 if (unlikely(ret == -EAGAIN))
275 ret = ttm_bo_wait_unreserved(&nvbo->bo, true);
374c3af8 276 drm_gem_object_unreference_unlocked(gem);
938c40ed
BS
277 if (unlikely(ret)) {
278 if (ret != -ERESTARTSYS)
279 NV_ERROR(dev, "fail reserve\n");
6ee73861 280 return ret;
a1606a95 281 }
6ee73861
BS
282 goto retry;
283 }
284
a1606a95 285 b->user_priv = (uint64_t)(unsigned long)nvbo;
6ee73861
BS
286 nvbo->reserved_by = file_priv;
287 nvbo->pbbo_index = i;
288 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
289 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
290 list_add_tail(&nvbo->entry, &op->both_list);
291 else
292 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
293 list_add_tail(&nvbo->entry, &op->vram_list);
294 else
295 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
296 list_add_tail(&nvbo->entry, &op->gart_list);
297 else {
298 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
299 b->valid_domains);
0208843d 300 list_add_tail(&nvbo->entry, &op->both_list);
6ee73861
BS
301 validate_fini(op, NULL);
302 return -EINVAL;
303 }
6ee73861
BS
304 }
305
306 return 0;
307}
308
309static int
310validate_list(struct nouveau_channel *chan, struct list_head *list,
311 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
312{
313 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
314 (void __force __user *)(uintptr_t)user_pbbo_ptr;
a1606a95 315 struct drm_device *dev = chan->dev;
6ee73861
BS
316 struct nouveau_bo *nvbo;
317 int ret, relocs = 0;
318
319 list_for_each_entry(nvbo, list, entry) {
320 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
6ee73861 321
2730723b 322 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
415e6186
BS
323 if (unlikely(ret)) {
324 NV_ERROR(dev, "fail pre-validate sync\n");
325 return ret;
6ee73861
BS
326 }
327
328 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
329 b->write_domains,
330 b->valid_domains);
a1606a95
BS
331 if (unlikely(ret)) {
332 NV_ERROR(dev, "fail set_domain\n");
6ee73861 333 return ret;
a1606a95 334 }
6ee73861 335
415e6186 336 nvbo->channel = (b->read_domains & (1 << 31)) ? NULL : chan;
7a45d764 337 ret = nouveau_bo_validate(nvbo, true, false, false);
6ee73861 338 nvbo->channel = NULL;
a1606a95 339 if (unlikely(ret)) {
938c40ed
BS
340 if (ret != -ERESTARTSYS)
341 NV_ERROR(dev, "fail ttm_validate\n");
6ee73861 342 return ret;
a1606a95 343 }
6ee73861 344
2730723b 345 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
415e6186
BS
346 if (unlikely(ret)) {
347 NV_ERROR(dev, "fail post-validate sync\n");
348 return ret;
349 }
350
a1606a95 351 if (nvbo->bo.offset == b->presumed.offset &&
6ee73861 352 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
a1606a95 353 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
6ee73861 354 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
a1606a95 355 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
6ee73861
BS
356 continue;
357
358 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
a1606a95 359 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
6ee73861 360 else
a1606a95
BS
361 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
362 b->presumed.offset = nvbo->bo.offset;
363 b->presumed.valid = 0;
6ee73861
BS
364 relocs++;
365
a1606a95
BS
366 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
367 &b->presumed, sizeof(b->presumed)))
6ee73861
BS
368 return -EFAULT;
369 }
370
371 return relocs;
372}
373
374static int
375nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
376 struct drm_file *file_priv,
377 struct drm_nouveau_gem_pushbuf_bo *pbbo,
378 uint64_t user_buffers, int nr_buffers,
379 struct validate_op *op, int *apply_relocs)
380{
a1606a95 381 struct drm_device *dev = chan->dev;
6ee73861
BS
382 int ret, relocs = 0;
383
384 INIT_LIST_HEAD(&op->vram_list);
385 INIT_LIST_HEAD(&op->gart_list);
386 INIT_LIST_HEAD(&op->both_list);
387
6ee73861
BS
388 if (nr_buffers == 0)
389 return 0;
390
391 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
a1606a95 392 if (unlikely(ret)) {
938c40ed
BS
393 if (ret != -ERESTARTSYS)
394 NV_ERROR(dev, "validate_init\n");
6ee73861 395 return ret;
a1606a95 396 }
6ee73861
BS
397
398 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
399 if (unlikely(ret < 0)) {
938c40ed
BS
400 if (ret != -ERESTARTSYS)
401 NV_ERROR(dev, "validate vram_list\n");
6ee73861
BS
402 validate_fini(op, NULL);
403 return ret;
404 }
405 relocs += ret;
406
407 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
408 if (unlikely(ret < 0)) {
938c40ed
BS
409 if (ret != -ERESTARTSYS)
410 NV_ERROR(dev, "validate gart_list\n");
6ee73861
BS
411 validate_fini(op, NULL);
412 return ret;
413 }
414 relocs += ret;
415
416 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
417 if (unlikely(ret < 0)) {
938c40ed
BS
418 if (ret != -ERESTARTSYS)
419 NV_ERROR(dev, "validate both_list\n");
6ee73861
BS
420 validate_fini(op, NULL);
421 return ret;
422 }
423 relocs += ret;
424
425 *apply_relocs = relocs;
426 return 0;
427}
428
429static inline void *
430u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
431{
432 void *mem;
433 void __user *userptr = (void __force __user *)(uintptr_t)user;
434
435 mem = kmalloc(nmemb * size, GFP_KERNEL);
436 if (!mem)
437 return ERR_PTR(-ENOMEM);
438
439 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
440 kfree(mem);
441 return ERR_PTR(-EFAULT);
442 }
443
444 return mem;
445}
446
447static int
a1606a95
BS
448nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
449 struct drm_nouveau_gem_pushbuf *req,
450 struct drm_nouveau_gem_pushbuf_bo *bo)
6ee73861
BS
451{
452 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
12f735b7
LB
453 int ret = 0;
454 unsigned i;
6ee73861 455
a1606a95 456 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
6ee73861
BS
457 if (IS_ERR(reloc))
458 return PTR_ERR(reloc);
459
a1606a95 460 for (i = 0; i < req->nr_relocs; i++) {
6ee73861
BS
461 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
462 struct drm_nouveau_gem_pushbuf_bo *b;
a1606a95 463 struct nouveau_bo *nvbo;
6ee73861
BS
464 uint32_t data;
465
a1606a95
BS
466 if (unlikely(r->bo_index > req->nr_buffers)) {
467 NV_ERROR(dev, "reloc bo index invalid\n");
6ee73861
BS
468 ret = -EINVAL;
469 break;
470 }
471
472 b = &bo[r->bo_index];
a1606a95 473 if (b->presumed.valid)
6ee73861
BS
474 continue;
475
a1606a95
BS
476 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
477 NV_ERROR(dev, "reloc container bo index invalid\n");
478 ret = -EINVAL;
479 break;
480 }
481 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
482
483 if (unlikely(r->reloc_bo_offset + 4 >
484 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
485 NV_ERROR(dev, "reloc outside of bo\n");
486 ret = -EINVAL;
487 break;
488 }
489
490 if (!nvbo->kmap.virtual) {
491 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
492 &nvbo->kmap);
493 if (ret) {
494 NV_ERROR(dev, "failed kmap for reloc\n");
495 break;
496 }
497 nvbo->validate_mapped = true;
498 }
499
6ee73861 500 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
a1606a95 501 data = b->presumed.offset + r->data;
6ee73861
BS
502 else
503 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
a1606a95 504 data = (b->presumed.offset + r->data) >> 32;
6ee73861
BS
505 else
506 data = r->data;
507
508 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
a1606a95 509 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
6ee73861
BS
510 data |= r->tor;
511 else
512 data |= r->vor;
513 }
514
702adba2 515 spin_lock(&nvbo->bo.bdev->fence_lock);
a1606a95 516 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
702adba2 517 spin_unlock(&nvbo->bo.bdev->fence_lock);
a1606a95
BS
518 if (ret) {
519 NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret);
520 break;
521 }
a1606a95
BS
522
523 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
6ee73861
BS
524 }
525
526 kfree(reloc);
527 return ret;
528}
529
530int
531nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
532 struct drm_file *file_priv)
533{
a1606a95 534 struct drm_nouveau_private *dev_priv = dev->dev_private;
6ee73861 535 struct drm_nouveau_gem_pushbuf *req = data;
a1606a95
BS
536 struct drm_nouveau_gem_pushbuf_push *push;
537 struct drm_nouveau_gem_pushbuf_bo *bo;
6ee73861
BS
538 struct nouveau_channel *chan;
539 struct validate_op op;
6e86e041 540 struct nouveau_fence *fence = NULL;
a1606a95 541 int i, j, ret = 0, do_reloc = 0;
6ee73861 542
cff5c133
BS
543 chan = nouveau_channel_get(dev, file_priv, req->channel);
544 if (IS_ERR(chan))
545 return PTR_ERR(chan);
6ee73861 546
a1606a95
BS
547 req->vram_available = dev_priv->fb_aper_free;
548 req->gart_available = dev_priv->gart_info.aper_free;
549 if (unlikely(req->nr_push == 0))
550 goto out_next;
6ee73861 551
a1606a95
BS
552 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
553 NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n",
554 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
cff5c133 555 nouveau_channel_put(&chan);
a1606a95 556 return -EINVAL;
6ee73861
BS
557 }
558
a1606a95
BS
559 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
560 NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n",
561 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
cff5c133 562 nouveau_channel_put(&chan);
a1606a95 563 return -EINVAL;
6ee73861
BS
564 }
565
a1606a95
BS
566 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
567 NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n",
568 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
cff5c133 569 nouveau_channel_put(&chan);
6ee73861
BS
570 return -EINVAL;
571 }
572
a1606a95 573 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
cff5c133
BS
574 if (IS_ERR(push)) {
575 nouveau_channel_put(&chan);
a1606a95 576 return PTR_ERR(push);
cff5c133 577 }
a1606a95 578
6ee73861 579 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
a1606a95
BS
580 if (IS_ERR(bo)) {
581 kfree(push);
cff5c133 582 nouveau_channel_put(&chan);
6ee73861 583 return PTR_ERR(bo);
a1606a95 584 }
6ee73861 585
415e6186
BS
586 /* Mark push buffers as being used on PFIFO, the validation code
587 * will then make sure that if the pushbuf bo moves, that they
588 * happen on the kernel channel, which will in turn cause a sync
589 * to happen before we try and submit the push buffer.
590 */
591 for (i = 0; i < req->nr_push; i++) {
592 if (push[i].bo_index >= req->nr_buffers) {
593 NV_ERROR(dev, "push %d buffer not in list\n", i);
594 ret = -EINVAL;
595 goto out;
596 }
597
598 bo[push[i].bo_index].read_domains |= (1 << 31);
599 }
600
6ee73861
BS
601 /* Validate buffer list */
602 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
603 req->nr_buffers, &op, &do_reloc);
604 if (ret) {
938c40ed
BS
605 if (ret != -ERESTARTSYS)
606 NV_ERROR(dev, "validate: %d\n", ret);
6ee73861
BS
607 goto out;
608 }
609
6ee73861
BS
610 /* Apply any relocations that are required */
611 if (do_reloc) {
a1606a95 612 ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo);
6ee73861 613 if (ret) {
6ee73861 614 NV_ERROR(dev, "reloc apply: %d\n", ret);
6ee73861
BS
615 goto out;
616 }
6ee73861 617 }
6ee73861 618
9a391ad8 619 if (chan->dma.ib_max) {
a1606a95 620 ret = nouveau_dma_wait(chan, req->nr_push + 1, 6);
6ee73861 621 if (ret) {
9a391ad8 622 NV_INFO(dev, "nv50cal_space: %d\n", ret);
6ee73861
BS
623 goto out;
624 }
6ee73861 625
a1606a95
BS
626 for (i = 0; i < req->nr_push; i++) {
627 struct nouveau_bo *nvbo = (void *)(unsigned long)
628 bo[push[i].bo_index].user_priv;
629
630 nv50_dma_push(chan, nvbo, push[i].offset,
631 push[i].length);
632 }
9a391ad8 633 } else
ee508b82 634 if (dev_priv->chipset >= 0x25) {
a1606a95 635 ret = RING_SPACE(chan, req->nr_push * 2);
6ee73861
BS
636 if (ret) {
637 NV_ERROR(dev, "cal_space: %d\n", ret);
638 goto out;
639 }
a1606a95
BS
640
641 for (i = 0; i < req->nr_push; i++) {
642 struct nouveau_bo *nvbo = (void *)(unsigned long)
643 bo[push[i].bo_index].user_priv;
644 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
645
646 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
647 push[i].offset) | 2);
648 OUT_RING(chan, 0);
649 }
6ee73861 650 } else {
a1606a95 651 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
6ee73861
BS
652 if (ret) {
653 NV_ERROR(dev, "jmp_space: %d\n", ret);
654 goto out;
655 }
6ee73861 656
a1606a95
BS
657 for (i = 0; i < req->nr_push; i++) {
658 struct nouveau_bo *nvbo = (void *)(unsigned long)
659 bo[push[i].bo_index].user_priv;
660 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
661 uint32_t cmd;
662
663 cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
664 cmd |= 0x20000000;
665 if (unlikely(cmd != req->suffix0)) {
666 if (!nvbo->kmap.virtual) {
667 ret = ttm_bo_kmap(&nvbo->bo, 0,
668 nvbo->bo.mem.
669 num_pages,
670 &nvbo->kmap);
671 if (ret) {
672 WIND_RING(chan);
673 goto out;
674 }
675 nvbo->validate_mapped = true;
676 }
677
678 nouveau_bo_wr32(nvbo, (push[i].offset +
679 push[i].length - 8) / 4, cmd);
680 }
681
682 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
683 push[i].offset) | 0x20000000);
6ee73861 684 OUT_RING(chan, 0);
a1606a95
BS
685 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
686 OUT_RING(chan, 0);
687 }
6ee73861
BS
688 }
689
234896a7 690 ret = nouveau_fence_new(chan, &fence, true);
6ee73861
BS
691 if (ret) {
692 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
693 WIND_RING(chan);
694 goto out;
695 }
696
697out:
234896a7 698 validate_fini(&op, fence);
382d62e5 699 nouveau_fence_unref(&fence);
6ee73861 700 kfree(bo);
a1606a95 701 kfree(push);
6ee73861
BS
702
703out_next:
9a391ad8
BS
704 if (chan->dma.ib_max) {
705 req->suffix0 = 0x00000000;
706 req->suffix1 = 0x00000000;
707 } else
ee508b82 708 if (dev_priv->chipset >= 0x25) {
6ee73861
BS
709 req->suffix0 = 0x00020000;
710 req->suffix1 = 0x00000000;
711 } else {
712 req->suffix0 = 0x20000000 |
713 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
714 req->suffix1 = 0x00000000;
715 }
716
cff5c133 717 nouveau_channel_put(&chan);
6ee73861
BS
718 return ret;
719}
720
6ee73861
BS
721static inline uint32_t
722domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
723{
724 uint32_t flags = 0;
725
726 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
727 flags |= TTM_PL_FLAG_VRAM;
728 if (domain & NOUVEAU_GEM_DOMAIN_GART)
729 flags |= TTM_PL_FLAG_TT;
730
731 return flags;
732}
733
6ee73861
BS
734int
735nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
736 struct drm_file *file_priv)
737{
738 struct drm_nouveau_gem_cpu_prep *req = data;
739 struct drm_gem_object *gem;
740 struct nouveau_bo *nvbo;
741 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
742 int ret = -EINVAL;
743
6ee73861
BS
744 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
745 if (!gem)
bf79cb91 746 return -ENOENT;
6ee73861
BS
747 nvbo = nouveau_gem_object(gem);
748
21e86c1c
BS
749 spin_lock(&nvbo->bo.bdev->fence_lock);
750 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
751 spin_unlock(&nvbo->bo.bdev->fence_lock);
bc9025bd 752 drm_gem_object_unreference_unlocked(gem);
6ee73861
BS
753 return ret;
754}
755
756int
757nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
758 struct drm_file *file_priv)
759{
21e86c1c 760 return 0;
6ee73861
BS
761}
762
763int
764nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
765 struct drm_file *file_priv)
766{
767 struct drm_nouveau_gem_info *req = data;
768 struct drm_gem_object *gem;
769 int ret;
770
6ee73861
BS
771 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
772 if (!gem)
bf79cb91 773 return -ENOENT;
6ee73861
BS
774
775 ret = nouveau_gem_info(gem, req);
bc9025bd 776 drm_gem_object_unreference_unlocked(gem);
6ee73861
BS
777 return ret;
778}
779
This page took 0.136187 seconds and 5 git commands to generate.