blob: c0e324b557c1b50cd9f4455e30a24da5be75ec83 [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 <subdev/fb.h>
28
29#include "nouveau_drm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100030#include "nouveau_dma.h"
Ben Skeggsd375e7d52012-04-30 13:30:00 +100031#include "nouveau_fence.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100032#include "nouveau_abi16.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100033
Ben Skeggsebb945a2012-07-20 08:17:34 +100034#include "nouveau_ttm.h"
35#include "nouveau_gem.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100036
37int
38nouveau_gem_object_new(struct drm_gem_object *gem)
39{
40 return 0;
41}
42
43void
44nouveau_gem_object_del(struct drm_gem_object *gem)
45{
46 struct nouveau_bo *nvbo = gem->driver_private;
47 struct ttm_buffer_object *bo = &nvbo->bo;
48
49 if (!nvbo)
50 return;
51 nvbo->gem = NULL;
52
Ben Skeggs6ee73862009-12-11 19:24:15 +100053 if (unlikely(nvbo->pin_refcnt)) {
54 nvbo->pin_refcnt = 1;
55 nouveau_bo_unpin(nvbo);
56 }
57
Dave Airlie22b33e82012-04-02 11:53:06 +010058 if (gem->import_attach)
59 drm_prime_gem_destroy(gem, nvbo->bo.sg);
60
Ben Skeggs6ee73862009-12-11 19:24:15 +100061 ttm_bo_unref(&bo);
Daniel Vetterfd632aa2010-04-09 19:05:05 +000062
63 drm_gem_object_release(gem);
64 kfree(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +100065}
66
67int
Ben Skeggs639212d2011-06-03 16:18:26 +100068nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
69{
Ben Skeggsebb945a2012-07-20 08:17:34 +100070 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100071 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
72 struct nouveau_vma *vma;
73 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100074
Ben Skeggsebb945a2012-07-20 08:17:34 +100075 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +100076 return 0;
77
Ben Skeggs2fd3db62011-06-07 15:25:12 +100078 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
79 if (ret)
80 return ret;
81
Ben Skeggsebb945a2012-07-20 08:17:34 +100082 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100083 if (!vma) {
84 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
85 if (!vma) {
86 ret = -ENOMEM;
87 goto out;
88 }
89
Ben Skeggsebb945a2012-07-20 08:17:34 +100090 ret = nouveau_bo_vma_add(nvbo, cli->base.vm, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100091 if (ret) {
92 kfree(vma);
93 goto out;
94 }
95 } else {
96 vma->refcount++;
97 }
98
99out:
100 ttm_bo_unreserve(&nvbo->bo);
101 return ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000102}
103
Ben Skeggsc4c70442013-05-07 09:48:30 +1000104static void
105nouveau_gem_object_delete(void *data)
106{
107 struct nouveau_vma *vma = data;
108 nouveau_vm_unmap(vma);
109 nouveau_vm_put(vma);
110 kfree(vma);
111}
112
113static void
114nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
115{
116 const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
117 struct nouveau_fence *fence = NULL;
118
119 list_del(&vma->head);
120
121 if (mapped) {
122 spin_lock(&nvbo->bo.bdev->fence_lock);
123 if (nvbo->bo.sync_obj)
124 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
125 spin_unlock(&nvbo->bo.bdev->fence_lock);
126 }
127
128 if (fence) {
129 nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
130 } else {
131 if (mapped)
132 nouveau_vm_unmap(vma);
133 nouveau_vm_put(vma);
134 kfree(vma);
135 }
136 nouveau_fence_unref(&fence);
137}
138
Ben Skeggs639212d2011-06-03 16:18:26 +1000139void
140nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
141{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000142 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000143 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
144 struct nouveau_vma *vma;
145 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000146
Ben Skeggsebb945a2012-07-20 08:17:34 +1000147 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +1000148 return;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000149
150 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
151 if (ret)
152 return;
153
Ben Skeggsebb945a2012-07-20 08:17:34 +1000154 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000155 if (vma) {
Ben Skeggsc4c70442013-05-07 09:48:30 +1000156 if (--vma->refcount == 0)
157 nouveau_gem_object_unmap(nvbo, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000158 }
159 ttm_bo_unreserve(&nvbo->bo);
Ben Skeggs639212d2011-06-03 16:18:26 +1000160}
161
162int
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000163nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
164 uint32_t tile_mode, uint32_t tile_flags,
165 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000166{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000167 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000168 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000169 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000170 int ret;
171
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000172 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
173 flags |= TTM_PL_FLAG_VRAM;
174 if (domain & NOUVEAU_GEM_DOMAIN_GART)
175 flags |= TTM_PL_FLAG_TT;
176 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
177 flags |= TTM_PL_FLAG_SYSTEM;
178
Ben Skeggs7375c952011-06-07 14:21:29 +1000179 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
Dave Airlie22b33e82012-04-02 11:53:06 +0100180 tile_flags, NULL, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000181 if (ret)
182 return ret;
183 nvbo = *pnvbo;
184
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000185 /* we restrict allowed domains on nv50+ to only the types
186 * that were requested at creation time. not possibly on
187 * earlier chips without busting the ABI.
188 */
189 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
190 NOUVEAU_GEM_DOMAIN_GART;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000191 if (nv_device(drm->device)->card_type >= NV_50)
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000192 nvbo->valid_domains &= domain;
193
Ben Skeggs6ee73862009-12-11 19:24:15 +1000194 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
195 if (!nvbo->gem) {
196 nouveau_bo_ref(NULL, pnvbo);
197 return -ENOMEM;
198 }
199
Jan Engelhardt5df23972011-04-04 01:25:18 +0200200 nvbo->bo.persistent_swap_storage = nvbo->gem->filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000201 nvbo->gem->driver_private = nvbo;
202 return 0;
203}
204
205static int
Ben Skeggse758a312011-06-07 13:28:11 +1000206nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
207 struct drm_nouveau_gem_info *rep)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000208{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000209 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000210 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggse758a312011-06-07 13:28:11 +1000211 struct nouveau_vma *vma;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000212
213 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
214 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
215 else
216 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
217
Ben Skeggse758a312011-06-07 13:28:11 +1000218 rep->offset = nvbo->bo.offset;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000219 if (cli->base.vm) {
220 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggse758a312011-06-07 13:28:11 +1000221 if (!vma)
222 return -EINVAL;
223
224 rep->offset = vma->offset;
225 }
226
Ben Skeggs6ee73862009-12-11 19:24:15 +1000227 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
Ben Skeggsd550c412011-02-16 08:41:56 +1000228 rep->map_handle = nvbo->bo.addr_space_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000229 rep->tile_mode = nvbo->tile_mode;
230 rep->tile_flags = nvbo->tile_flags;
231 return 0;
232}
233
Ben Skeggs6ee73862009-12-11 19:24:15 +1000234int
235nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
236 struct drm_file *file_priv)
237{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000238 struct nouveau_drm *drm = nouveau_drm(dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100239 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000240 struct nouveau_fb *pfb = nouveau_fb(drm->device);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000241 struct drm_nouveau_gem_new *req = data;
242 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000243 int ret = 0;
244
Ben Skeggsebb945a2012-07-20 08:17:34 +1000245 drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000246
Ben Skeggsebb945a2012-07-20 08:17:34 +1000247 if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100248 NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000249 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000250 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000251
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000252 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000253 req->info.domain, req->info.tile_mode,
254 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000255 if (ret)
256 return ret;
257
Ben Skeggs6ee73862009-12-11 19:24:15 +1000258 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000259 if (ret == 0) {
260 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info);
261 if (ret)
262 drm_gem_handle_delete(file_priv, req->info.handle);
263 }
264
Dave Airlie29d08b32010-09-27 16:17:17 +1000265 /* drop reference from allocate - handle holds it now */
266 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000267 return ret;
268}
269
270static int
271nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
272 uint32_t write_domains, uint32_t valid_domains)
273{
274 struct nouveau_bo *nvbo = gem->driver_private;
275 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000276 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100277 (write_domains ? write_domains : read_domains);
278 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000279
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100280 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000281 return -EINVAL;
282
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100283 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
284 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000285
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100286 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
287 valid_flags |= TTM_PL_FLAG_TT;
288
289 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
290 bo->mem.mem_type == TTM_PL_VRAM)
291 pref_flags |= TTM_PL_FLAG_VRAM;
292
293 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
294 bo->mem.mem_type == TTM_PL_TT)
295 pref_flags |= TTM_PL_FLAG_TT;
296
297 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
298 pref_flags |= TTM_PL_FLAG_VRAM;
299
300 else
301 pref_flags |= TTM_PL_FLAG_TT;
302
303 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
304
Ben Skeggs6ee73862009-12-11 19:24:15 +1000305 return 0;
306}
307
308struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000309 struct list_head vram_list;
310 struct list_head gart_list;
311 struct list_head both_list;
312};
313
314static void
315validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
316{
317 struct list_head *entry, *tmp;
318 struct nouveau_bo *nvbo;
319
320 list_for_each_safe(entry, tmp, list) {
321 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000322
Francisco Jerez332b2422010-10-20 23:35:40 +0200323 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000324
Ben Skeggsa1606a92010-02-12 10:27:35 +1000325 if (unlikely(nvbo->validate_mapped)) {
326 ttm_bo_kunmap(&nvbo->kmap);
327 nvbo->validate_mapped = false;
328 }
329
Ben Skeggs6ee73862009-12-11 19:24:15 +1000330 list_del(&nvbo->entry);
331 nvbo->reserved_by = NULL;
332 ttm_bo_unreserve(&nvbo->bo);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200333 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000334 }
335}
336
337static void
Luca Barbieri234896a2010-01-06 04:02:45 +0100338validate_fini(struct validate_op *op, struct nouveau_fence* fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000339{
Luca Barbieri234896a2010-01-06 04:02:45 +0100340 validate_fini_list(&op->vram_list, fence);
341 validate_fini_list(&op->gart_list, fence);
342 validate_fini_list(&op->both_list, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000343}
344
345static int
346validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
347 struct drm_nouveau_gem_pushbuf_bo *pbbo,
348 int nr_buffers, struct validate_op *op)
349{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100350 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000351 struct drm_device *dev = chan->drm->dev;
352 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000353 uint32_t sequence;
354 int trycnt = 0;
355 int ret, i;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100356 struct nouveau_bo *res_bo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000357
Ben Skeggsebb945a2012-07-20 08:17:34 +1000358 sequence = atomic_add_return(1, &drm->ttm.validate_sequence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000359retry:
360 if (++trycnt > 100000) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100361 NV_ERROR(cli, "%s failed and gave up.\n", __func__);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000362 return -EINVAL;
363 }
364
365 for (i = 0; i < nr_buffers; i++) {
366 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
367 struct drm_gem_object *gem;
368 struct nouveau_bo *nvbo;
369
370 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
371 if (!gem) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100372 NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000373 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100374 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000375 }
376 nvbo = gem->driver_private;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100377 if (nvbo == res_bo) {
378 res_bo = NULL;
379 drm_gem_object_unreference_unlocked(gem);
380 continue;
381 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000382
383 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100384 NV_ERROR(cli, "multiple instances of buffer %d on "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000385 "validation list\n", b->handle);
Ben Skeggs5086f692012-06-25 18:04:27 +1000386 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000387 validate_fini(op, NULL);
388 return -EINVAL;
389 }
390
Ben Skeggs938c40e2010-10-12 09:54:54 +1000391 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000392 if (ret) {
393 validate_fini(op, NULL);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100394 if (unlikely(ret == -EAGAIN)) {
395 sequence = atomic_add_return(1, &drm->ttm.validate_sequence);
396 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
397 sequence);
398 if (!ret)
399 res_bo = nvbo;
400 }
Ben Skeggs938c40e2010-10-12 09:54:54 +1000401 if (unlikely(ret)) {
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100402 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000403 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100404 NV_ERROR(cli, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000405 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000406 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000407 }
408
Ben Skeggsa1606a92010-02-12 10:27:35 +1000409 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000410 nvbo->reserved_by = file_priv;
411 nvbo->pbbo_index = i;
412 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
413 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
414 list_add_tail(&nvbo->entry, &op->both_list);
415 else
416 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
417 list_add_tail(&nvbo->entry, &op->vram_list);
418 else
419 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
420 list_add_tail(&nvbo->entry, &op->gart_list);
421 else {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100422 NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000423 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000424 list_add_tail(&nvbo->entry, &op->both_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000425 validate_fini(op, NULL);
426 return -EINVAL;
427 }
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100428 if (nvbo == res_bo)
429 goto retry;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000430 }
431
432 return 0;
433}
434
435static int
Ben Skeggs525895b2012-01-10 10:18:28 +1000436validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
437{
438 struct nouveau_fence *fence = NULL;
439 int ret = 0;
440
441 spin_lock(&nvbo->bo.bdev->fence_lock);
442 if (nvbo->bo.sync_obj)
443 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
444 spin_unlock(&nvbo->bo.bdev->fence_lock);
445
446 if (fence) {
447 ret = nouveau_fence_sync(fence, chan);
448 nouveau_fence_unref(&fence);
449 }
450
451 return ret;
452}
453
454static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100455validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
456 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
457 uint64_t user_pbbo_ptr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000458{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000459 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000460 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
461 (void __force __user *)(uintptr_t)user_pbbo_ptr;
462 struct nouveau_bo *nvbo;
463 int ret, relocs = 0;
464
465 list_for_each_entry(nvbo, list, entry) {
466 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000467
Ben Skeggs525895b2012-01-10 10:18:28 +1000468 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000469 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100470 NV_ERROR(cli, "fail pre-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000471 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000472 }
473
474 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
475 b->write_domains,
476 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000477 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100478 NV_ERROR(cli, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000479 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000480 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000481
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000482 ret = nouveau_bo_validate(nvbo, true, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000483 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000484 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100485 NV_ERROR(cli, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000486 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000487 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000488
Ben Skeggs525895b2012-01-10 10:18:28 +1000489 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000490 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100491 NV_ERROR(cli, "fail post-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000492 return ret;
493 }
494
Ben Skeggsebb945a2012-07-20 08:17:34 +1000495 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000496 if (nvbo->bo.offset == b->presumed.offset &&
497 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
498 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
499 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
500 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
501 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000502
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000503 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
504 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
505 else
506 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
507 b->presumed.offset = nvbo->bo.offset;
508 b->presumed.valid = 0;
509 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000510
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000511 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
512 &b->presumed, sizeof(b->presumed)))
513 return -EFAULT;
514 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000515 }
516
517 return relocs;
518}
519
520static int
521nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
522 struct drm_file *file_priv,
523 struct drm_nouveau_gem_pushbuf_bo *pbbo,
524 uint64_t user_buffers, int nr_buffers,
525 struct validate_op *op, int *apply_relocs)
526{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100527 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000528 int ret, relocs = 0;
529
530 INIT_LIST_HEAD(&op->vram_list);
531 INIT_LIST_HEAD(&op->gart_list);
532 INIT_LIST_HEAD(&op->both_list);
533
Ben Skeggs6ee73862009-12-11 19:24:15 +1000534 if (nr_buffers == 0)
535 return 0;
536
537 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000538 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000539 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100540 NV_ERROR(cli, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000541 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000542 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000543
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100544 ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000545 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000546 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100547 NV_ERROR(cli, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000548 validate_fini(op, NULL);
549 return ret;
550 }
551 relocs += ret;
552
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100553 ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000554 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000555 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100556 NV_ERROR(cli, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000557 validate_fini(op, NULL);
558 return ret;
559 }
560 relocs += ret;
561
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100562 ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000563 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000564 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100565 NV_ERROR(cli, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000566 validate_fini(op, NULL);
567 return ret;
568 }
569 relocs += ret;
570
571 *apply_relocs = relocs;
572 return 0;
573}
574
575static inline void *
576u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
577{
578 void *mem;
579 void __user *userptr = (void __force __user *)(uintptr_t)user;
580
581 mem = kmalloc(nmemb * size, GFP_KERNEL);
582 if (!mem)
583 return ERR_PTR(-ENOMEM);
584
585 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
586 kfree(mem);
587 return ERR_PTR(-EFAULT);
588 }
589
590 return mem;
591}
592
593static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100594nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
Ben Skeggsa1606a92010-02-12 10:27:35 +1000595 struct drm_nouveau_gem_pushbuf *req,
596 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000597{
598 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100599 int ret = 0;
600 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000601
Ben Skeggsa1606a92010-02-12 10:27:35 +1000602 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000603 if (IS_ERR(reloc))
604 return PTR_ERR(reloc);
605
Ben Skeggsa1606a92010-02-12 10:27:35 +1000606 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000607 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
608 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000609 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610 uint32_t data;
611
Ben Skeggsa1606a92010-02-12 10:27:35 +1000612 if (unlikely(r->bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100613 NV_ERROR(cli, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000614 ret = -EINVAL;
615 break;
616 }
617
618 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000619 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000620 continue;
621
Ben Skeggsa1606a92010-02-12 10:27:35 +1000622 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100623 NV_ERROR(cli, "reloc container bo index invalid\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000624 ret = -EINVAL;
625 break;
626 }
627 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
628
629 if (unlikely(r->reloc_bo_offset + 4 >
630 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100631 NV_ERROR(cli, "reloc outside of bo\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000632 ret = -EINVAL;
633 break;
634 }
635
636 if (!nvbo->kmap.virtual) {
637 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
638 &nvbo->kmap);
639 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100640 NV_ERROR(cli, "failed kmap for reloc\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000641 break;
642 }
643 nvbo->validate_mapped = true;
644 }
645
Ben Skeggs6ee73862009-12-11 19:24:15 +1000646 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000647 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000648 else
649 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000650 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000651 else
652 data = r->data;
653
654 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000655 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000656 data |= r->tor;
657 else
658 data |= r->vor;
659 }
660
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000661 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000662 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000663 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000664 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100665 NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000666 break;
667 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000668
669 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000670 }
671
672 kfree(reloc);
673 return ret;
674}
675
676int
677nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
678 struct drm_file *file_priv)
679{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000680 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100681 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000682 struct nouveau_abi16_chan *temp;
683 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000684 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000685 struct drm_nouveau_gem_pushbuf_push *push;
686 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000687 struct nouveau_channel *chan = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000688 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200689 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000690 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000691
Ben Skeggsebb945a2012-07-20 08:17:34 +1000692 if (unlikely(!abi16))
693 return -ENOMEM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000694
Ben Skeggsebb945a2012-07-20 08:17:34 +1000695 list_for_each_entry(temp, &abi16->channels, head) {
696 if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
697 chan = temp->chan;
698 break;
699 }
700 }
701
702 if (!chan)
703 return nouveau_abi16_put(abi16, -ENOENT);
704
705 req->vram_available = drm->gem.vram_available;
706 req->gart_available = drm->gem.gart_available;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000707 if (unlikely(req->nr_push == 0))
708 goto out_next;
709
710 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100711 NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000712 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000713 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000714 }
715
Ben Skeggsa1606a92010-02-12 10:27:35 +1000716 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100717 NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000718 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000719 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000720 }
721
722 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100723 NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000724 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000725 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000726 }
727
728 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggsebb945a2012-07-20 08:17:34 +1000729 if (IS_ERR(push))
730 return nouveau_abi16_put(abi16, PTR_ERR(push));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000731
732 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
733 if (IS_ERR(bo)) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000734 kfree(push);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000735 return nouveau_abi16_put(abi16, PTR_ERR(bo));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000736 }
737
Ben Skeggsaccf9492012-03-16 12:40:17 +1000738 /* Ensure all push buffers are on validate list */
Ben Skeggs415e6182010-07-23 09:06:52 +1000739 for (i = 0; i < req->nr_push; i++) {
740 if (push[i].bo_index >= req->nr_buffers) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100741 NV_ERROR(cli, "push %d buffer not in list\n", i);
Ben Skeggs415e6182010-07-23 09:06:52 +1000742 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100743 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000744 }
Ben Skeggs415e6182010-07-23 09:06:52 +1000745 }
746
Ben Skeggs6ee73862009-12-11 19:24:15 +1000747 /* Validate buffer list */
748 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
749 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000750 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000751 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100752 NV_ERROR(cli, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100753 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000754 }
755
Ben Skeggs6ee73862009-12-11 19:24:15 +1000756 /* Apply any relocations that are required */
757 if (do_reloc) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100758 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000759 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100760 NV_ERROR(cli, "reloc apply: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000761 goto out;
762 }
763 }
764
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000765 if (chan->dma.ib_max) {
Ben Skeggs5e120f62012-04-30 13:55:29 +1000766 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000767 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100768 NV_ERROR(cli, "nv50cal_space: %d\n", ret);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000769 goto out;
770 }
771
Ben Skeggsa1606a92010-02-12 10:27:35 +1000772 for (i = 0; i < req->nr_push; i++) {
773 struct nouveau_bo *nvbo = (void *)(unsigned long)
774 bo[push[i].bo_index].user_priv;
775
776 nv50_dma_push(chan, nvbo, push[i].offset,
777 push[i].length);
778 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000779 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000780 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000781 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000782 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100783 NV_ERROR(cli, "cal_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000784 goto out;
785 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000786
787 for (i = 0; i < req->nr_push; i++) {
788 struct nouveau_bo *nvbo = (void *)(unsigned long)
789 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000790
Ben Skeggs3a92d372012-08-04 05:46:01 +1000791 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000792 OUT_RING(chan, 0);
793 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000794 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000795 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000796 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100797 NV_ERROR(cli, "jmp_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000798 goto out;
799 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000800
Ben Skeggsa1606a92010-02-12 10:27:35 +1000801 for (i = 0; i < req->nr_push; i++) {
802 struct nouveau_bo *nvbo = (void *)(unsigned long)
803 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000804 uint32_t cmd;
805
Ben Skeggsebb945a2012-07-20 08:17:34 +1000806 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000807 cmd |= 0x20000000;
808 if (unlikely(cmd != req->suffix0)) {
809 if (!nvbo->kmap.virtual) {
810 ret = ttm_bo_kmap(&nvbo->bo, 0,
811 nvbo->bo.mem.
812 num_pages,
813 &nvbo->kmap);
814 if (ret) {
815 WIND_RING(chan);
816 goto out;
817 }
818 nvbo->validate_mapped = true;
819 }
820
821 nouveau_bo_wr32(nvbo, (push[i].offset +
822 push[i].length - 8) / 4, cmd);
823 }
824
Ben Skeggs3a92d372012-08-04 05:46:01 +1000825 OUT_RING(chan, 0x20000000 |
826 (nvbo->bo.offset + push[i].offset));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000827 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000828 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
829 OUT_RING(chan, 0);
830 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000831 }
832
Ben Skeggs264ce192013-02-14 13:43:21 +1000833 ret = nouveau_fence_new(chan, false, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000834 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100835 NV_ERROR(cli, "error fencing pushbuf: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000836 WIND_RING(chan);
837 goto out;
838 }
839
840out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100841 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200842 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100843
844out_prevalid:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000845 kfree(bo);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000846 kfree(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000847
848out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000849 if (chan->dma.ib_max) {
850 req->suffix0 = 0x00000000;
851 req->suffix1 = 0x00000000;
852 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000853 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000854 req->suffix0 = 0x00020000;
855 req->suffix1 = 0x00000000;
856 } else {
857 req->suffix0 = 0x20000000 |
Ben Skeggsebb945a2012-07-20 08:17:34 +1000858 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000859 req->suffix1 = 0x00000000;
860 }
861
Ben Skeggsebb945a2012-07-20 08:17:34 +1000862 return nouveau_abi16_put(abi16, ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000863}
864
Ben Skeggs6ee73862009-12-11 19:24:15 +1000865static inline uint32_t
866domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
867{
868 uint32_t flags = 0;
869
870 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
871 flags |= TTM_PL_FLAG_VRAM;
872 if (domain & NOUVEAU_GEM_DOMAIN_GART)
873 flags |= TTM_PL_FLAG_TT;
874
875 return flags;
876}
877
878int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000879nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
880 struct drm_file *file_priv)
881{
882 struct drm_nouveau_gem_cpu_prep *req = data;
883 struct drm_gem_object *gem;
884 struct nouveau_bo *nvbo;
885 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
886 int ret = -EINVAL;
887
Ben Skeggs6ee73862009-12-11 19:24:15 +1000888 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
889 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100890 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000891 nvbo = nouveau_gem_object(gem);
892
Ben Skeggs21e86c12010-10-11 11:48:45 +1000893 spin_lock(&nvbo->bo.bdev->fence_lock);
894 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
895 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000896 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000897 return ret;
898}
899
900int
901nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
902 struct drm_file *file_priv)
903{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000904 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000905}
906
907int
908nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
909 struct drm_file *file_priv)
910{
911 struct drm_nouveau_gem_info *req = data;
912 struct drm_gem_object *gem;
913 int ret;
914
Ben Skeggs6ee73862009-12-11 19:24:15 +1000915 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
916 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100917 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918
Ben Skeggse758a312011-06-07 13:28:11 +1000919 ret = nouveau_gem_info(file_priv, gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000920 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000921 return ret;
922}
923