blob: 7fa6882c2942fce07913f6ee7ca9c4061d65b175 [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);
208 struct nouveau_fb *pfb = nouveau_fb(drm->device);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209 struct drm_nouveau_gem_new *req = data;
210 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000211 int ret = 0;
212
Ben Skeggsebb945a2012-07-20 08:17:34 +1000213 drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214
Ben Skeggsebb945a2012-07-20 08:17:34 +1000215 if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
216 NV_ERROR(drm, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000217 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000218 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000219
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000220 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000221 req->info.domain, req->info.tile_mode,
222 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223 if (ret)
224 return ret;
225
Ben Skeggs6ee73862009-12-11 19:24:15 +1000226 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000227 if (ret == 0) {
228 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info);
229 if (ret)
230 drm_gem_handle_delete(file_priv, req->info.handle);
231 }
232
Dave Airlie29d08b32010-09-27 16:17:17 +1000233 /* drop reference from allocate - handle holds it now */
234 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235 return ret;
236}
237
238static int
239nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
240 uint32_t write_domains, uint32_t valid_domains)
241{
242 struct nouveau_bo *nvbo = gem->driver_private;
243 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000244 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100245 (write_domains ? write_domains : read_domains);
246 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000247
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100248 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000249 return -EINVAL;
250
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100251 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
252 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000253
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100254 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
255 valid_flags |= TTM_PL_FLAG_TT;
256
257 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
258 bo->mem.mem_type == TTM_PL_VRAM)
259 pref_flags |= TTM_PL_FLAG_VRAM;
260
261 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
262 bo->mem.mem_type == TTM_PL_TT)
263 pref_flags |= TTM_PL_FLAG_TT;
264
265 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
266 pref_flags |= TTM_PL_FLAG_VRAM;
267
268 else
269 pref_flags |= TTM_PL_FLAG_TT;
270
271 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
272
Ben Skeggs6ee73862009-12-11 19:24:15 +1000273 return 0;
274}
275
276struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000277 struct list_head vram_list;
278 struct list_head gart_list;
279 struct list_head both_list;
280};
281
282static void
283validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
284{
285 struct list_head *entry, *tmp;
286 struct nouveau_bo *nvbo;
287
288 list_for_each_safe(entry, tmp, list) {
289 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000290
Francisco Jerez332b2422010-10-20 23:35:40 +0200291 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000292
Ben Skeggsa1606a92010-02-12 10:27:35 +1000293 if (unlikely(nvbo->validate_mapped)) {
294 ttm_bo_kunmap(&nvbo->kmap);
295 nvbo->validate_mapped = false;
296 }
297
Ben Skeggs6ee73862009-12-11 19:24:15 +1000298 list_del(&nvbo->entry);
299 nvbo->reserved_by = NULL;
300 ttm_bo_unreserve(&nvbo->bo);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200301 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000302 }
303}
304
305static void
Luca Barbieri234896a2010-01-06 04:02:45 +0100306validate_fini(struct validate_op *op, struct nouveau_fence* fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000307{
Luca Barbieri234896a2010-01-06 04:02:45 +0100308 validate_fini_list(&op->vram_list, fence);
309 validate_fini_list(&op->gart_list, fence);
310 validate_fini_list(&op->both_list, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000311}
312
313static int
314validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
315 struct drm_nouveau_gem_pushbuf_bo *pbbo,
316 int nr_buffers, struct validate_op *op)
317{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000318 struct drm_device *dev = chan->drm->dev;
319 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000320 uint32_t sequence;
321 int trycnt = 0;
322 int ret, i;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100323 struct nouveau_bo *res_bo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000324
Maarten Lankhorst979ee292013-01-15 14:54:22 +0100325 sequence = atomic_add_return(1, &drm->ttm.validate_sequence);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100326retry:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000327 if (++trycnt > 100000) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000328 NV_ERROR(drm, "%s failed and gave up.\n", __func__);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000329 return -EINVAL;
330 }
331
332 for (i = 0; i < nr_buffers; i++) {
333 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
334 struct drm_gem_object *gem;
335 struct nouveau_bo *nvbo;
336
337 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
338 if (!gem) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000339 NV_ERROR(drm, "Unknown handle 0x%08x\n", b->handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000340 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100341 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000342 }
343 nvbo = gem->driver_private;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100344 if (nvbo == res_bo) {
345 res_bo = NULL;
346 drm_gem_object_unreference_unlocked(gem);
347 continue;
348 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000349
350 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000351 NV_ERROR(drm, "multiple instances of buffer %d on "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000352 "validation list\n", b->handle);
Ben Skeggs5086f692012-06-25 18:04:27 +1000353 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000354 validate_fini(op, NULL);
355 return -EINVAL;
356 }
357
Ben Skeggs938c40e2010-10-12 09:54:54 +1000358 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000359 if (ret) {
360 validate_fini(op, NULL);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100361 if (unlikely(ret == -EAGAIN)) {
362 sequence = atomic_add_return(1, &drm->ttm.validate_sequence);
363 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
364 sequence);
365 if (!ret)
366 res_bo = nvbo;
367 }
Ben Skeggs938c40e2010-10-12 09:54:54 +1000368 if (unlikely(ret)) {
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100369 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000370 if (ret != -ERESTARTSYS)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000371 NV_ERROR(drm, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000372 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000373 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000374 }
375
Ben Skeggsa1606a92010-02-12 10:27:35 +1000376 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000377 nvbo->reserved_by = file_priv;
378 nvbo->pbbo_index = i;
379 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
380 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
381 list_add_tail(&nvbo->entry, &op->both_list);
382 else
383 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
384 list_add_tail(&nvbo->entry, &op->vram_list);
385 else
386 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
387 list_add_tail(&nvbo->entry, &op->gart_list);
388 else {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000389 NV_ERROR(drm, "invalid valid domains: 0x%08x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000390 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000391 list_add_tail(&nvbo->entry, &op->both_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000392 validate_fini(op, NULL);
393 return -EINVAL;
394 }
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100395 if (nvbo == res_bo)
396 goto retry;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000397 }
398
399 return 0;
400}
401
402static int
Ben Skeggs525895b2012-01-10 10:18:28 +1000403validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
404{
405 struct nouveau_fence *fence = NULL;
406 int ret = 0;
407
408 spin_lock(&nvbo->bo.bdev->fence_lock);
409 if (nvbo->bo.sync_obj)
410 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
411 spin_unlock(&nvbo->bo.bdev->fence_lock);
412
413 if (fence) {
414 ret = nouveau_fence_sync(fence, chan);
415 nouveau_fence_unref(&fence);
416 }
417
418 return ret;
419}
420
421static int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000422validate_list(struct nouveau_channel *chan, struct list_head *list,
423 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
424{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000425 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000426 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
427 (void __force __user *)(uintptr_t)user_pbbo_ptr;
428 struct nouveau_bo *nvbo;
429 int ret, relocs = 0;
430
431 list_for_each_entry(nvbo, list, entry) {
432 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000433
Ben Skeggs525895b2012-01-10 10:18:28 +1000434 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000435 if (unlikely(ret)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000436 NV_ERROR(drm, "fail pre-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000437 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000438 }
439
440 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
441 b->write_domains,
442 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000443 if (unlikely(ret)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000444 NV_ERROR(drm, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000445 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000446 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000447
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000448 ret = nouveau_bo_validate(nvbo, true, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000449 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000450 if (ret != -ERESTARTSYS)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000451 NV_ERROR(drm, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000452 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000453 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000454
Ben Skeggs525895b2012-01-10 10:18:28 +1000455 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000456 if (unlikely(ret)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000457 NV_ERROR(drm, "fail post-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000458 return ret;
459 }
460
Ben Skeggsebb945a2012-07-20 08:17:34 +1000461 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000462 if (nvbo->bo.offset == b->presumed.offset &&
463 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
464 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
465 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
466 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
467 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000468
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000469 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
470 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
471 else
472 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
473 b->presumed.offset = nvbo->bo.offset;
474 b->presumed.valid = 0;
475 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000476
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000477 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
478 &b->presumed, sizeof(b->presumed)))
479 return -EFAULT;
480 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000481 }
482
483 return relocs;
484}
485
486static int
487nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
488 struct drm_file *file_priv,
489 struct drm_nouveau_gem_pushbuf_bo *pbbo,
490 uint64_t user_buffers, int nr_buffers,
491 struct validate_op *op, int *apply_relocs)
492{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000493 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000494 int ret, relocs = 0;
495
496 INIT_LIST_HEAD(&op->vram_list);
497 INIT_LIST_HEAD(&op->gart_list);
498 INIT_LIST_HEAD(&op->both_list);
499
Ben Skeggs6ee73862009-12-11 19:24:15 +1000500 if (nr_buffers == 0)
501 return 0;
502
503 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000504 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000505 if (ret != -ERESTARTSYS)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000506 NV_ERROR(drm, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000507 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000508 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000509
510 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
511 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000512 if (ret != -ERESTARTSYS)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000513 NV_ERROR(drm, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000514 validate_fini(op, NULL);
515 return ret;
516 }
517 relocs += ret;
518
519 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
520 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000521 if (ret != -ERESTARTSYS)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000522 NV_ERROR(drm, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000523 validate_fini(op, NULL);
524 return ret;
525 }
526 relocs += ret;
527
528 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
529 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000530 if (ret != -ERESTARTSYS)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000531 NV_ERROR(drm, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000532 validate_fini(op, NULL);
533 return ret;
534 }
535 relocs += ret;
536
537 *apply_relocs = relocs;
538 return 0;
539}
540
541static inline void *
542u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
543{
544 void *mem;
545 void __user *userptr = (void __force __user *)(uintptr_t)user;
546
547 mem = kmalloc(nmemb * size, GFP_KERNEL);
548 if (!mem)
549 return ERR_PTR(-ENOMEM);
550
551 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
552 kfree(mem);
553 return ERR_PTR(-EFAULT);
554 }
555
556 return mem;
557}
558
559static int
Ben Skeggsa1606a92010-02-12 10:27:35 +1000560nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
561 struct drm_nouveau_gem_pushbuf *req,
562 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000563{
Ben Skeggs77145f12012-07-31 16:16:21 +1000564 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000565 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100566 int ret = 0;
567 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000568
Ben Skeggsa1606a92010-02-12 10:27:35 +1000569 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000570 if (IS_ERR(reloc))
571 return PTR_ERR(reloc);
572
Ben Skeggsa1606a92010-02-12 10:27:35 +1000573 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000574 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
575 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000576 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000577 uint32_t data;
578
Ben Skeggsa1606a92010-02-12 10:27:35 +1000579 if (unlikely(r->bo_index > req->nr_buffers)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000580 NV_ERROR(drm, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000581 ret = -EINVAL;
582 break;
583 }
584
585 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000586 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000587 continue;
588
Ben Skeggsa1606a92010-02-12 10:27:35 +1000589 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000590 NV_ERROR(drm, "reloc container bo index invalid\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000591 ret = -EINVAL;
592 break;
593 }
594 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
595
596 if (unlikely(r->reloc_bo_offset + 4 >
597 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000598 NV_ERROR(drm, "reloc outside of bo\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000599 ret = -EINVAL;
600 break;
601 }
602
603 if (!nvbo->kmap.virtual) {
604 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
605 &nvbo->kmap);
606 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000607 NV_ERROR(drm, "failed kmap for reloc\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000608 break;
609 }
610 nvbo->validate_mapped = true;
611 }
612
Ben Skeggs6ee73862009-12-11 19:24:15 +1000613 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000614 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000615 else
616 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000617 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000618 else
619 data = r->data;
620
621 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000622 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000623 data |= r->tor;
624 else
625 data |= r->vor;
626 }
627
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000628 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000629 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000630 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000631 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000632 NV_ERROR(drm, "reloc wait_idle failed: %d\n", ret);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000633 break;
634 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000635
636 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000637 }
638
639 kfree(reloc);
640 return ret;
641}
642
643int
644nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
645 struct drm_file *file_priv)
646{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000647 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
648 struct nouveau_abi16_chan *temp;
649 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000650 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000651 struct drm_nouveau_gem_pushbuf_push *push;
652 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000653 struct nouveau_channel *chan = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000654 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200655 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000656 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000657
Ben Skeggsebb945a2012-07-20 08:17:34 +1000658 if (unlikely(!abi16))
659 return -ENOMEM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000660
Ben Skeggsebb945a2012-07-20 08:17:34 +1000661 list_for_each_entry(temp, &abi16->channels, head) {
662 if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
663 chan = temp->chan;
664 break;
665 }
666 }
667
668 if (!chan)
669 return nouveau_abi16_put(abi16, -ENOENT);
670
671 req->vram_available = drm->gem.vram_available;
672 req->gart_available = drm->gem.gart_available;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000673 if (unlikely(req->nr_push == 0))
674 goto out_next;
675
676 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000677 NV_ERROR(drm, "pushbuf push count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000678 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000679 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000680 }
681
Ben Skeggsa1606a92010-02-12 10:27:35 +1000682 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000683 NV_ERROR(drm, "pushbuf bo count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000684 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000685 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000686 }
687
688 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000689 NV_ERROR(drm, "pushbuf reloc count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000690 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000691 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000692 }
693
694 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggsebb945a2012-07-20 08:17:34 +1000695 if (IS_ERR(push))
696 return nouveau_abi16_put(abi16, PTR_ERR(push));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000697
698 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
699 if (IS_ERR(bo)) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000700 kfree(push);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000701 return nouveau_abi16_put(abi16, PTR_ERR(bo));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000702 }
703
Ben Skeggsaccf9492012-03-16 12:40:17 +1000704 /* Ensure all push buffers are on validate list */
Ben Skeggs415e6182010-07-23 09:06:52 +1000705 for (i = 0; i < req->nr_push; i++) {
706 if (push[i].bo_index >= req->nr_buffers) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000707 NV_ERROR(drm, "push %d buffer not in list\n", i);
Ben Skeggs415e6182010-07-23 09:06:52 +1000708 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100709 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000710 }
Ben Skeggs415e6182010-07-23 09:06:52 +1000711 }
712
Ben Skeggs6ee73862009-12-11 19:24:15 +1000713 /* Validate buffer list */
714 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
715 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000716 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000717 if (ret != -ERESTARTSYS)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000718 NV_ERROR(drm, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100719 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000720 }
721
Ben Skeggs6ee73862009-12-11 19:24:15 +1000722 /* Apply any relocations that are required */
723 if (do_reloc) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000724 ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000725 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000726 NV_ERROR(drm, "reloc apply: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000727 goto out;
728 }
729 }
730
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000731 if (chan->dma.ib_max) {
Ben Skeggs5e120f62012-04-30 13:55:29 +1000732 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000733 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000734 NV_ERROR(drm, "nv50cal_space: %d\n", ret);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000735 goto out;
736 }
737
Ben Skeggsa1606a92010-02-12 10:27:35 +1000738 for (i = 0; i < req->nr_push; i++) {
739 struct nouveau_bo *nvbo = (void *)(unsigned long)
740 bo[push[i].bo_index].user_priv;
741
742 nv50_dma_push(chan, nvbo, push[i].offset,
743 push[i].length);
744 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000745 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000746 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000747 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000748 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000749 NV_ERROR(drm, "cal_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000750 goto out;
751 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000752
753 for (i = 0; i < req->nr_push; i++) {
754 struct nouveau_bo *nvbo = (void *)(unsigned long)
755 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000756
Ben Skeggs3a92d372012-08-04 05:46:01 +1000757 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000758 OUT_RING(chan, 0);
759 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000760 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000761 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000762 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000763 NV_ERROR(drm, "jmp_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764 goto out;
765 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000766
Ben Skeggsa1606a92010-02-12 10:27:35 +1000767 for (i = 0; i < req->nr_push; i++) {
768 struct nouveau_bo *nvbo = (void *)(unsigned long)
769 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000770 uint32_t cmd;
771
Ben Skeggsebb945a2012-07-20 08:17:34 +1000772 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000773 cmd |= 0x20000000;
774 if (unlikely(cmd != req->suffix0)) {
775 if (!nvbo->kmap.virtual) {
776 ret = ttm_bo_kmap(&nvbo->bo, 0,
777 nvbo->bo.mem.
778 num_pages,
779 &nvbo->kmap);
780 if (ret) {
781 WIND_RING(chan);
782 goto out;
783 }
784 nvbo->validate_mapped = true;
785 }
786
787 nouveau_bo_wr32(nvbo, (push[i].offset +
788 push[i].length - 8) / 4, cmd);
789 }
790
Ben Skeggs3a92d372012-08-04 05:46:01 +1000791 OUT_RING(chan, 0x20000000 |
792 (nvbo->bo.offset + push[i].offset));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000793 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000794 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
795 OUT_RING(chan, 0);
796 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000797 }
798
Ben Skeggsd375e7d52012-04-30 13:30:00 +1000799 ret = nouveau_fence_new(chan, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000800 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000801 NV_ERROR(drm, "error fencing pushbuf: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000802 WIND_RING(chan);
803 goto out;
804 }
805
806out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100807 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200808 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100809
810out_prevalid:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811 kfree(bo);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000812 kfree(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000813
814out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000815 if (chan->dma.ib_max) {
816 req->suffix0 = 0x00000000;
817 req->suffix1 = 0x00000000;
818 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000819 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000820 req->suffix0 = 0x00020000;
821 req->suffix1 = 0x00000000;
822 } else {
823 req->suffix0 = 0x20000000 |
Ben Skeggsebb945a2012-07-20 08:17:34 +1000824 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000825 req->suffix1 = 0x00000000;
826 }
827
Ben Skeggsebb945a2012-07-20 08:17:34 +1000828 return nouveau_abi16_put(abi16, ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000829}
830
Ben Skeggs6ee73862009-12-11 19:24:15 +1000831static inline uint32_t
832domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
833{
834 uint32_t flags = 0;
835
836 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
837 flags |= TTM_PL_FLAG_VRAM;
838 if (domain & NOUVEAU_GEM_DOMAIN_GART)
839 flags |= TTM_PL_FLAG_TT;
840
841 return flags;
842}
843
844int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000845nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
846 struct drm_file *file_priv)
847{
848 struct drm_nouveau_gem_cpu_prep *req = data;
849 struct drm_gem_object *gem;
850 struct nouveau_bo *nvbo;
851 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
852 int ret = -EINVAL;
853
Ben Skeggs6ee73862009-12-11 19:24:15 +1000854 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
855 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100856 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000857 nvbo = nouveau_gem_object(gem);
858
Ben Skeggs21e86c12010-10-11 11:48:45 +1000859 spin_lock(&nvbo->bo.bdev->fence_lock);
860 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
861 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000862 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000863 return ret;
864}
865
866int
867nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
868 struct drm_file *file_priv)
869{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000870 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000871}
872
873int
874nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
875 struct drm_file *file_priv)
876{
877 struct drm_nouveau_gem_info *req = data;
878 struct drm_gem_object *gem;
879 int ret;
880
Ben Skeggs6ee73862009-12-11 19:24:15 +1000881 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
882 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100883 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000884
Ben Skeggse758a312011-06-07 13:28:11 +1000885 ret = nouveau_gem_info(file_priv, gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000886 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000887 return ret;
888}
889