blob: 706113880622614f88df7bba778c159e1535617e [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
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 */
Ben Skeggs6ee73862009-12-11 19:24:15 +100026
Ben Skeggsebb945a2012-07-20 08:17:34 +100027#include <linux/dma-buf.h>
Ben Skeggsebb945a2012-07-20 08:17:34 +100028
29#include <subdev/fb.h>
30
31#include "nouveau_drm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100032#include "nouveau_dma.h"
Ben Skeggsd375e7d52012-04-30 13:30:00 +100033#include "nouveau_fence.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100034#include "nouveau_abi16.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100035
Ben Skeggsebb945a2012-07-20 08:17:34 +100036#include "nouveau_ttm.h"
37#include "nouveau_gem.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100038
39int
40nouveau_gem_object_new(struct drm_gem_object *gem)
41{
42 return 0;
43}
44
45void
46nouveau_gem_object_del(struct drm_gem_object *gem)
47{
48 struct nouveau_bo *nvbo = gem->driver_private;
49 struct ttm_buffer_object *bo = &nvbo->bo;
50
51 if (!nvbo)
52 return;
53 nvbo->gem = NULL;
54
Ben Skeggs6ee73862009-12-11 19:24:15 +100055 if (unlikely(nvbo->pin_refcnt)) {
56 nvbo->pin_refcnt = 1;
57 nouveau_bo_unpin(nvbo);
58 }
59
Dave Airlie22b33e82012-04-02 11:53:06 +010060 if (gem->import_attach)
61 drm_prime_gem_destroy(gem, nvbo->bo.sg);
62
Ben Skeggs6ee73862009-12-11 19:24:15 +100063 ttm_bo_unref(&bo);
Daniel Vetterfd632aa2010-04-09 19:05:05 +000064
65 drm_gem_object_release(gem);
66 kfree(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +100067}
68
69int
Ben Skeggs639212d2011-06-03 16:18:26 +100070nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
71{
Ben Skeggsebb945a2012-07-20 08:17:34 +100072 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100073 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
74 struct nouveau_vma *vma;
75 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100076
Ben Skeggsebb945a2012-07-20 08:17:34 +100077 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +100078 return 0;
79
Ben Skeggs2fd3db62011-06-07 15:25:12 +100080 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
81 if (ret)
82 return ret;
83
Ben Skeggsebb945a2012-07-20 08:17:34 +100084 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100085 if (!vma) {
86 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
87 if (!vma) {
88 ret = -ENOMEM;
89 goto out;
90 }
91
Ben Skeggsebb945a2012-07-20 08:17:34 +100092 ret = nouveau_bo_vma_add(nvbo, cli->base.vm, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100093 if (ret) {
94 kfree(vma);
95 goto out;
96 }
97 } else {
98 vma->refcount++;
99 }
100
101out:
102 ttm_bo_unreserve(&nvbo->bo);
103 return ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000104}
105
106void
107nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
108{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000109 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000110 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
111 struct nouveau_vma *vma;
112 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000113
Ben Skeggsebb945a2012-07-20 08:17:34 +1000114 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +1000115 return;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000116
117 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
118 if (ret)
119 return;
120
Ben Skeggsebb945a2012-07-20 08:17:34 +1000121 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000122 if (vma) {
Marcin Slusarz8fe198b2011-06-23 16:34:30 +0200123 if (--vma->refcount == 0) {
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000124 nouveau_bo_vma_del(nvbo, vma);
Marcin Slusarz8fe198b2011-06-23 16:34:30 +0200125 kfree(vma);
126 }
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000127 }
128 ttm_bo_unreserve(&nvbo->bo);
Ben Skeggs639212d2011-06-03 16:18:26 +1000129}
130
131int
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000132nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
133 uint32_t tile_mode, uint32_t tile_flags,
134 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000135{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000136 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000137 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000138 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000139 int ret;
140
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000141 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
142 flags |= TTM_PL_FLAG_VRAM;
143 if (domain & NOUVEAU_GEM_DOMAIN_GART)
144 flags |= TTM_PL_FLAG_TT;
145 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
146 flags |= TTM_PL_FLAG_SYSTEM;
147
Ben Skeggs7375c952011-06-07 14:21:29 +1000148 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
Dave Airlie22b33e82012-04-02 11:53:06 +0100149 tile_flags, NULL, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000150 if (ret)
151 return ret;
152 nvbo = *pnvbo;
153
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000154 /* we restrict allowed domains on nv50+ to only the types
155 * that were requested at creation time. not possibly on
156 * earlier chips without busting the ABI.
157 */
158 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
159 NOUVEAU_GEM_DOMAIN_GART;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000160 if (nv_device(drm->device)->card_type >= NV_50)
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000161 nvbo->valid_domains &= domain;
162
Ben Skeggs6ee73862009-12-11 19:24:15 +1000163 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
164 if (!nvbo->gem) {
165 nouveau_bo_ref(NULL, pnvbo);
166 return -ENOMEM;
167 }
168
Jan Engelhardt5df23972011-04-04 01:25:18 +0200169 nvbo->bo.persistent_swap_storage = nvbo->gem->filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000170 nvbo->gem->driver_private = nvbo;
171 return 0;
172}
173
174static int
Ben Skeggse758a312011-06-07 13:28:11 +1000175nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
176 struct drm_nouveau_gem_info *rep)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000177{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000178 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000179 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggse758a312011-06-07 13:28:11 +1000180 struct nouveau_vma *vma;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000181
182 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
183 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
184 else
185 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
186
Ben Skeggse758a312011-06-07 13:28:11 +1000187 rep->offset = nvbo->bo.offset;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000188 if (cli->base.vm) {
189 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggse758a312011-06-07 13:28:11 +1000190 if (!vma)
191 return -EINVAL;
192
193 rep->offset = vma->offset;
194 }
195
Ben Skeggs6ee73862009-12-11 19:24:15 +1000196 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
Ben Skeggsd550c412011-02-16 08:41:56 +1000197 rep->map_handle = nvbo->bo.addr_space_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000198 rep->tile_mode = nvbo->tile_mode;
199 rep->tile_flags = nvbo->tile_flags;
200 return 0;
201}
202
Ben Skeggs6ee73862009-12-11 19:24:15 +1000203int
204nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
205 struct drm_file *file_priv)
206{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000207 struct nouveau_drm *drm = nouveau_drm(dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100208 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000209 struct nouveau_fb *pfb = nouveau_fb(drm->device);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000210 struct drm_nouveau_gem_new *req = data;
211 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000212 int ret = 0;
213
Ben Skeggsebb945a2012-07-20 08:17:34 +1000214 drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000215
Ben Skeggsebb945a2012-07-20 08:17:34 +1000216 if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100217 NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000218 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000219 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000221 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000222 req->info.domain, req->info.tile_mode,
223 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000224 if (ret)
225 return ret;
226
Ben Skeggs6ee73862009-12-11 19:24:15 +1000227 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000228 if (ret == 0) {
229 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info);
230 if (ret)
231 drm_gem_handle_delete(file_priv, req->info.handle);
232 }
233
Dave Airlie29d08b32010-09-27 16:17:17 +1000234 /* drop reference from allocate - handle holds it now */
235 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000236 return ret;
237}
238
239static int
240nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
241 uint32_t write_domains, uint32_t valid_domains)
242{
243 struct nouveau_bo *nvbo = gem->driver_private;
244 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000245 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100246 (write_domains ? write_domains : read_domains);
247 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000248
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100249 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000250 return -EINVAL;
251
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100252 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
253 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100255 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
256 valid_flags |= TTM_PL_FLAG_TT;
257
258 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
259 bo->mem.mem_type == TTM_PL_VRAM)
260 pref_flags |= TTM_PL_FLAG_VRAM;
261
262 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
263 bo->mem.mem_type == TTM_PL_TT)
264 pref_flags |= TTM_PL_FLAG_TT;
265
266 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
267 pref_flags |= TTM_PL_FLAG_VRAM;
268
269 else
270 pref_flags |= TTM_PL_FLAG_TT;
271
272 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
273
Ben Skeggs6ee73862009-12-11 19:24:15 +1000274 return 0;
275}
276
277struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000278 struct list_head vram_list;
279 struct list_head gart_list;
280 struct list_head both_list;
281};
282
283static void
284validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
285{
286 struct list_head *entry, *tmp;
287 struct nouveau_bo *nvbo;
288
289 list_for_each_safe(entry, tmp, list) {
290 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000291
Francisco Jerez332b2422010-10-20 23:35:40 +0200292 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000293
Ben Skeggsa1606a92010-02-12 10:27:35 +1000294 if (unlikely(nvbo->validate_mapped)) {
295 ttm_bo_kunmap(&nvbo->kmap);
296 nvbo->validate_mapped = false;
297 }
298
Ben Skeggs6ee73862009-12-11 19:24:15 +1000299 list_del(&nvbo->entry);
300 nvbo->reserved_by = NULL;
301 ttm_bo_unreserve(&nvbo->bo);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200302 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000303 }
304}
305
306static void
Luca Barbieri234896a2010-01-06 04:02:45 +0100307validate_fini(struct validate_op *op, struct nouveau_fence* fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000308{
Luca Barbieri234896a2010-01-06 04:02:45 +0100309 validate_fini_list(&op->vram_list, fence);
310 validate_fini_list(&op->gart_list, fence);
311 validate_fini_list(&op->both_list, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000312}
313
314static int
315validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
316 struct drm_nouveau_gem_pushbuf_bo *pbbo,
317 int nr_buffers, struct validate_op *op)
318{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100319 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000320 struct drm_device *dev = chan->drm->dev;
321 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000322 uint32_t sequence;
323 int trycnt = 0;
324 int ret, i;
325
Ben Skeggsebb945a2012-07-20 08:17:34 +1000326 sequence = atomic_add_return(1, &drm->ttm.validate_sequence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000327retry:
328 if (++trycnt > 100000) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100329 NV_ERROR(cli, "%s failed and gave up.\n", __func__);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000330 return -EINVAL;
331 }
332
333 for (i = 0; i < nr_buffers; i++) {
334 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
335 struct drm_gem_object *gem;
336 struct nouveau_bo *nvbo;
337
338 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
339 if (!gem) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100340 NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000341 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100342 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000343 }
344 nvbo = gem->driver_private;
345
346 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100347 NV_ERROR(cli, "multiple instances of buffer %d on "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000348 "validation list\n", b->handle);
Ben Skeggs5086f692012-06-25 18:04:27 +1000349 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000350 validate_fini(op, NULL);
351 return -EINVAL;
352 }
353
Ben Skeggs938c40e2010-10-12 09:54:54 +1000354 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000355 if (ret) {
356 validate_fini(op, NULL);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000357 if (unlikely(ret == -EAGAIN))
358 ret = ttm_bo_wait_unreserved(&nvbo->bo, true);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200359 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000360 if (unlikely(ret)) {
361 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100362 NV_ERROR(cli, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000363 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000364 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000365 goto retry;
366 }
367
Ben Skeggsa1606a92010-02-12 10:27:35 +1000368 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000369 nvbo->reserved_by = file_priv;
370 nvbo->pbbo_index = i;
371 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
372 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
373 list_add_tail(&nvbo->entry, &op->both_list);
374 else
375 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
376 list_add_tail(&nvbo->entry, &op->vram_list);
377 else
378 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
379 list_add_tail(&nvbo->entry, &op->gart_list);
380 else {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100381 NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000382 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000383 list_add_tail(&nvbo->entry, &op->both_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000384 validate_fini(op, NULL);
385 return -EINVAL;
386 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000387 }
388
389 return 0;
390}
391
392static int
Ben Skeggs525895b2012-01-10 10:18:28 +1000393validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
394{
395 struct nouveau_fence *fence = NULL;
396 int ret = 0;
397
398 spin_lock(&nvbo->bo.bdev->fence_lock);
399 if (nvbo->bo.sync_obj)
400 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
401 spin_unlock(&nvbo->bo.bdev->fence_lock);
402
403 if (fence) {
404 ret = nouveau_fence_sync(fence, chan);
405 nouveau_fence_unref(&fence);
406 }
407
408 return ret;
409}
410
411static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100412validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
413 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
414 uint64_t user_pbbo_ptr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000415{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000416 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000417 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
418 (void __force __user *)(uintptr_t)user_pbbo_ptr;
419 struct nouveau_bo *nvbo;
420 int ret, relocs = 0;
421
422 list_for_each_entry(nvbo, list, entry) {
423 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000424
Ben Skeggs525895b2012-01-10 10:18:28 +1000425 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000426 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100427 NV_ERROR(cli, "fail pre-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000428 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000429 }
430
431 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
432 b->write_domains,
433 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000434 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100435 NV_ERROR(cli, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000436 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000437 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000438
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000439 ret = nouveau_bo_validate(nvbo, true, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000440 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000441 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100442 NV_ERROR(cli, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000443 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000444 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000445
Ben Skeggs525895b2012-01-10 10:18:28 +1000446 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000447 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100448 NV_ERROR(cli, "fail post-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000449 return ret;
450 }
451
Ben Skeggsebb945a2012-07-20 08:17:34 +1000452 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000453 if (nvbo->bo.offset == b->presumed.offset &&
454 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
455 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
456 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
457 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
458 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000459
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000460 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
461 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
462 else
463 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
464 b->presumed.offset = nvbo->bo.offset;
465 b->presumed.valid = 0;
466 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000467
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000468 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
469 &b->presumed, sizeof(b->presumed)))
470 return -EFAULT;
471 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000472 }
473
474 return relocs;
475}
476
477static int
478nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
479 struct drm_file *file_priv,
480 struct drm_nouveau_gem_pushbuf_bo *pbbo,
481 uint64_t user_buffers, int nr_buffers,
482 struct validate_op *op, int *apply_relocs)
483{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100484 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000485 int ret, relocs = 0;
486
487 INIT_LIST_HEAD(&op->vram_list);
488 INIT_LIST_HEAD(&op->gart_list);
489 INIT_LIST_HEAD(&op->both_list);
490
Ben Skeggs6ee73862009-12-11 19:24:15 +1000491 if (nr_buffers == 0)
492 return 0;
493
494 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000495 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000496 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100497 NV_ERROR(cli, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000498 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000499 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000500
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100501 ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000502 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000503 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100504 NV_ERROR(cli, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000505 validate_fini(op, NULL);
506 return ret;
507 }
508 relocs += ret;
509
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100510 ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000511 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000512 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100513 NV_ERROR(cli, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000514 validate_fini(op, NULL);
515 return ret;
516 }
517 relocs += ret;
518
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100519 ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000520 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000521 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100522 NV_ERROR(cli, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000523 validate_fini(op, NULL);
524 return ret;
525 }
526 relocs += ret;
527
528 *apply_relocs = relocs;
529 return 0;
530}
531
532static inline void *
533u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
534{
535 void *mem;
536 void __user *userptr = (void __force __user *)(uintptr_t)user;
537
538 mem = kmalloc(nmemb * size, GFP_KERNEL);
539 if (!mem)
540 return ERR_PTR(-ENOMEM);
541
542 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
543 kfree(mem);
544 return ERR_PTR(-EFAULT);
545 }
546
547 return mem;
548}
549
550static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100551nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
Ben Skeggsa1606a92010-02-12 10:27:35 +1000552 struct drm_nouveau_gem_pushbuf *req,
553 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000554{
555 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100556 int ret = 0;
557 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000558
Ben Skeggsa1606a92010-02-12 10:27:35 +1000559 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560 if (IS_ERR(reloc))
561 return PTR_ERR(reloc);
562
Ben Skeggsa1606a92010-02-12 10:27:35 +1000563 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000564 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
565 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000566 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000567 uint32_t data;
568
Ben Skeggsa1606a92010-02-12 10:27:35 +1000569 if (unlikely(r->bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100570 NV_ERROR(cli, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000571 ret = -EINVAL;
572 break;
573 }
574
575 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000576 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000577 continue;
578
Ben Skeggsa1606a92010-02-12 10:27:35 +1000579 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100580 NV_ERROR(cli, "reloc container bo index invalid\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000581 ret = -EINVAL;
582 break;
583 }
584 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
585
586 if (unlikely(r->reloc_bo_offset + 4 >
587 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100588 NV_ERROR(cli, "reloc outside of bo\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000589 ret = -EINVAL;
590 break;
591 }
592
593 if (!nvbo->kmap.virtual) {
594 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
595 &nvbo->kmap);
596 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100597 NV_ERROR(cli, "failed kmap for reloc\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000598 break;
599 }
600 nvbo->validate_mapped = true;
601 }
602
Ben Skeggs6ee73862009-12-11 19:24:15 +1000603 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000604 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000605 else
606 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000607 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000608 else
609 data = r->data;
610
611 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000612 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000613 data |= r->tor;
614 else
615 data |= r->vor;
616 }
617
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000618 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000619 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000620 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000621 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100622 NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000623 break;
624 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000625
626 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000627 }
628
629 kfree(reloc);
630 return ret;
631}
632
633int
634nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
635 struct drm_file *file_priv)
636{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000637 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100638 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000639 struct nouveau_abi16_chan *temp;
640 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000641 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000642 struct drm_nouveau_gem_pushbuf_push *push;
643 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000644 struct nouveau_channel *chan = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000645 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200646 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000647 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000648
Ben Skeggsebb945a2012-07-20 08:17:34 +1000649 if (unlikely(!abi16))
650 return -ENOMEM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000651
Ben Skeggsebb945a2012-07-20 08:17:34 +1000652 list_for_each_entry(temp, &abi16->channels, head) {
653 if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
654 chan = temp->chan;
655 break;
656 }
657 }
658
659 if (!chan)
660 return nouveau_abi16_put(abi16, -ENOENT);
661
662 req->vram_available = drm->gem.vram_available;
663 req->gart_available = drm->gem.gart_available;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000664 if (unlikely(req->nr_push == 0))
665 goto out_next;
666
667 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100668 NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000669 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000670 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000671 }
672
Ben Skeggsa1606a92010-02-12 10:27:35 +1000673 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100674 NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000675 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000676 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000677 }
678
679 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100680 NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000681 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000682 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000683 }
684
685 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggsebb945a2012-07-20 08:17:34 +1000686 if (IS_ERR(push))
687 return nouveau_abi16_put(abi16, PTR_ERR(push));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000688
689 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
690 if (IS_ERR(bo)) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000691 kfree(push);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000692 return nouveau_abi16_put(abi16, PTR_ERR(bo));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000693 }
694
Ben Skeggsaccf9492012-03-16 12:40:17 +1000695 /* Ensure all push buffers are on validate list */
Ben Skeggs415e6182010-07-23 09:06:52 +1000696 for (i = 0; i < req->nr_push; i++) {
697 if (push[i].bo_index >= req->nr_buffers) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100698 NV_ERROR(cli, "push %d buffer not in list\n", i);
Ben Skeggs415e6182010-07-23 09:06:52 +1000699 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100700 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000701 }
Ben Skeggs415e6182010-07-23 09:06:52 +1000702 }
703
Ben Skeggs6ee73862009-12-11 19:24:15 +1000704 /* Validate buffer list */
705 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
706 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000707 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000708 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100709 NV_ERROR(cli, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100710 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000711 }
712
Ben Skeggs6ee73862009-12-11 19:24:15 +1000713 /* Apply any relocations that are required */
714 if (do_reloc) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100715 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000716 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100717 NV_ERROR(cli, "reloc apply: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000718 goto out;
719 }
720 }
721
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000722 if (chan->dma.ib_max) {
Ben Skeggs5e120f62012-04-30 13:55:29 +1000723 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000724 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100725 NV_ERROR(cli, "nv50cal_space: %d\n", ret);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000726 goto out;
727 }
728
Ben Skeggsa1606a92010-02-12 10:27:35 +1000729 for (i = 0; i < req->nr_push; i++) {
730 struct nouveau_bo *nvbo = (void *)(unsigned long)
731 bo[push[i].bo_index].user_priv;
732
733 nv50_dma_push(chan, nvbo, push[i].offset,
734 push[i].length);
735 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000736 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000737 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000738 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000739 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100740 NV_ERROR(cli, "cal_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000741 goto out;
742 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000743
744 for (i = 0; i < req->nr_push; i++) {
745 struct nouveau_bo *nvbo = (void *)(unsigned long)
746 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000747
Ben Skeggs3a92d372012-08-04 05:46:01 +1000748 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000749 OUT_RING(chan, 0);
750 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000751 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000752 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000753 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100754 NV_ERROR(cli, "jmp_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000755 goto out;
756 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000757
Ben Skeggsa1606a92010-02-12 10:27:35 +1000758 for (i = 0; i < req->nr_push; i++) {
759 struct nouveau_bo *nvbo = (void *)(unsigned long)
760 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000761 uint32_t cmd;
762
Ben Skeggsebb945a2012-07-20 08:17:34 +1000763 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000764 cmd |= 0x20000000;
765 if (unlikely(cmd != req->suffix0)) {
766 if (!nvbo->kmap.virtual) {
767 ret = ttm_bo_kmap(&nvbo->bo, 0,
768 nvbo->bo.mem.
769 num_pages,
770 &nvbo->kmap);
771 if (ret) {
772 WIND_RING(chan);
773 goto out;
774 }
775 nvbo->validate_mapped = true;
776 }
777
778 nouveau_bo_wr32(nvbo, (push[i].offset +
779 push[i].length - 8) / 4, cmd);
780 }
781
Ben Skeggs3a92d372012-08-04 05:46:01 +1000782 OUT_RING(chan, 0x20000000 |
783 (nvbo->bo.offset + push[i].offset));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000784 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000785 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
786 OUT_RING(chan, 0);
787 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000788 }
789
Ben Skeggsd375e7d52012-04-30 13:30:00 +1000790 ret = nouveau_fence_new(chan, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000791 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100792 NV_ERROR(cli, "error fencing pushbuf: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000793 WIND_RING(chan);
794 goto out;
795 }
796
797out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100798 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200799 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100800
801out_prevalid:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000802 kfree(bo);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000803 kfree(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000804
805out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000806 if (chan->dma.ib_max) {
807 req->suffix0 = 0x00000000;
808 req->suffix1 = 0x00000000;
809 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000810 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811 req->suffix0 = 0x00020000;
812 req->suffix1 = 0x00000000;
813 } else {
814 req->suffix0 = 0x20000000 |
Ben Skeggsebb945a2012-07-20 08:17:34 +1000815 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000816 req->suffix1 = 0x00000000;
817 }
818
Ben Skeggsebb945a2012-07-20 08:17:34 +1000819 return nouveau_abi16_put(abi16, ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000820}
821
Ben Skeggs6ee73862009-12-11 19:24:15 +1000822static inline uint32_t
823domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
824{
825 uint32_t flags = 0;
826
827 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
828 flags |= TTM_PL_FLAG_VRAM;
829 if (domain & NOUVEAU_GEM_DOMAIN_GART)
830 flags |= TTM_PL_FLAG_TT;
831
832 return flags;
833}
834
835int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000836nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
837 struct drm_file *file_priv)
838{
839 struct drm_nouveau_gem_cpu_prep *req = data;
840 struct drm_gem_object *gem;
841 struct nouveau_bo *nvbo;
842 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
843 int ret = -EINVAL;
844
Ben Skeggs6ee73862009-12-11 19:24:15 +1000845 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
846 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100847 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000848 nvbo = nouveau_gem_object(gem);
849
Ben Skeggs21e86c12010-10-11 11:48:45 +1000850 spin_lock(&nvbo->bo.bdev->fence_lock);
851 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
852 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000853 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000854 return ret;
855}
856
857int
858nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
859 struct drm_file *file_priv)
860{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000861 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000862}
863
864int
865nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
866 struct drm_file *file_priv)
867{
868 struct drm_nouveau_gem_info *req = data;
869 struct drm_gem_object *gem;
870 int ret;
871
Ben Skeggs6ee73862009-12-11 19:24:15 +1000872 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
873 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100874 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000875
Ben Skeggse758a312011-06-07 13:28:11 +1000876 ret = nouveau_gem_info(file_priv, gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000877 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000878 return ret;
879}
880