blob: e72d09c068a8e8bcb8de4b47abd1ea0340415c12 [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
Maarten Lankhorst27f06b22013-06-27 13:38:22 +020053 /* Lockdep hates you for doing reserve with gem object lock held */
54 if (WARN_ON_ONCE(nvbo->pin_refcnt)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +100055 nvbo->pin_refcnt = 1;
56 nouveau_bo_unpin(nvbo);
57 }
58
Dave Airlie22b33e82012-04-02 11:53:06 +010059 if (gem->import_attach)
60 drm_prime_gem_destroy(gem, nvbo->bo.sg);
61
Ben Skeggs6ee73862009-12-11 19:24:15 +100062 ttm_bo_unref(&bo);
Daniel Vetterfd632aa2010-04-09 19:05:05 +000063
64 drm_gem_object_release(gem);
65 kfree(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +100066}
67
68int
Ben Skeggs639212d2011-06-03 16:18:26 +100069nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
70{
Ben Skeggsebb945a2012-07-20 08:17:34 +100071 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100072 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
73 struct nouveau_vma *vma;
74 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100075
Ben Skeggsebb945a2012-07-20 08:17:34 +100076 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +100077 return 0;
78
Ben Skeggs2fd3db62011-06-07 15:25:12 +100079 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
80 if (ret)
81 return ret;
82
Ben Skeggsebb945a2012-07-20 08:17:34 +100083 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100084 if (!vma) {
85 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
86 if (!vma) {
87 ret = -ENOMEM;
88 goto out;
89 }
90
Ben Skeggsebb945a2012-07-20 08:17:34 +100091 ret = nouveau_bo_vma_add(nvbo, cli->base.vm, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100092 if (ret) {
93 kfree(vma);
94 goto out;
95 }
96 } else {
97 vma->refcount++;
98 }
99
100out:
101 ttm_bo_unreserve(&nvbo->bo);
102 return ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000103}
104
Ben Skeggsc4c70442013-05-07 09:48:30 +1000105static void
106nouveau_gem_object_delete(void *data)
107{
108 struct nouveau_vma *vma = data;
109 nouveau_vm_unmap(vma);
110 nouveau_vm_put(vma);
111 kfree(vma);
112}
113
114static void
115nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
116{
117 const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
118 struct nouveau_fence *fence = NULL;
119
120 list_del(&vma->head);
121
122 if (mapped) {
123 spin_lock(&nvbo->bo.bdev->fence_lock);
124 if (nvbo->bo.sync_obj)
125 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
126 spin_unlock(&nvbo->bo.bdev->fence_lock);
127 }
128
129 if (fence) {
130 nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
131 } else {
132 if (mapped)
133 nouveau_vm_unmap(vma);
134 nouveau_vm_put(vma);
135 kfree(vma);
136 }
137 nouveau_fence_unref(&fence);
138}
139
Ben Skeggs639212d2011-06-03 16:18:26 +1000140void
141nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
142{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000143 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000144 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
145 struct nouveau_vma *vma;
146 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000147
Ben Skeggsebb945a2012-07-20 08:17:34 +1000148 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +1000149 return;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000150
151 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
152 if (ret)
153 return;
154
Ben Skeggsebb945a2012-07-20 08:17:34 +1000155 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000156 if (vma) {
Ben Skeggsc4c70442013-05-07 09:48:30 +1000157 if (--vma->refcount == 0)
158 nouveau_gem_object_unmap(nvbo, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000159 }
160 ttm_bo_unreserve(&nvbo->bo);
Ben Skeggs639212d2011-06-03 16:18:26 +1000161}
162
163int
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000164nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
165 uint32_t tile_mode, uint32_t tile_flags,
166 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000167{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000168 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000169 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000170 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000171 int ret;
172
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000173 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
174 flags |= TTM_PL_FLAG_VRAM;
175 if (domain & NOUVEAU_GEM_DOMAIN_GART)
176 flags |= TTM_PL_FLAG_TT;
177 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
178 flags |= TTM_PL_FLAG_SYSTEM;
179
Ben Skeggs7375c952011-06-07 14:21:29 +1000180 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
Dave Airlie22b33e82012-04-02 11:53:06 +0100181 tile_flags, NULL, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000182 if (ret)
183 return ret;
184 nvbo = *pnvbo;
185
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000186 /* we restrict allowed domains on nv50+ to only the types
187 * that were requested at creation time. not possibly on
188 * earlier chips without busting the ABI.
189 */
190 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
191 NOUVEAU_GEM_DOMAIN_GART;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000192 if (nv_device(drm->device)->card_type >= NV_50)
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000193 nvbo->valid_domains &= domain;
194
Ben Skeggs6ee73862009-12-11 19:24:15 +1000195 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
196 if (!nvbo->gem) {
197 nouveau_bo_ref(NULL, pnvbo);
198 return -ENOMEM;
199 }
200
Jan Engelhardt5df23972011-04-04 01:25:18 +0200201 nvbo->bo.persistent_swap_storage = nvbo->gem->filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000202 nvbo->gem->driver_private = nvbo;
203 return 0;
204}
205
206static int
Ben Skeggse758a312011-06-07 13:28:11 +1000207nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
208 struct drm_nouveau_gem_info *rep)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000210 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000211 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggse758a312011-06-07 13:28:11 +1000212 struct nouveau_vma *vma;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000213
214 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
215 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
216 else
217 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
218
Ben Skeggse758a312011-06-07 13:28:11 +1000219 rep->offset = nvbo->bo.offset;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000220 if (cli->base.vm) {
221 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggse758a312011-06-07 13:28:11 +1000222 if (!vma)
223 return -EINVAL;
224
225 rep->offset = vma->offset;
226 }
227
Ben Skeggs6ee73862009-12-11 19:24:15 +1000228 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
Ben Skeggsd550c412011-02-16 08:41:56 +1000229 rep->map_handle = nvbo->bo.addr_space_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000230 rep->tile_mode = nvbo->tile_mode;
231 rep->tile_flags = nvbo->tile_flags;
232 return 0;
233}
234
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235int
236nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
237 struct drm_file *file_priv)
238{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000239 struct nouveau_drm *drm = nouveau_drm(dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100240 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000241 struct nouveau_fb *pfb = nouveau_fb(drm->device);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000242 struct drm_nouveau_gem_new *req = data;
243 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000244 int ret = 0;
245
Ben Skeggsebb945a2012-07-20 08:17:34 +1000246 drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000247
Ben Skeggsebb945a2012-07-20 08:17:34 +1000248 if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100249 NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000250 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000251 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000252
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000253 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000254 req->info.domain, req->info.tile_mode,
255 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000256 if (ret)
257 return ret;
258
Ben Skeggs6ee73862009-12-11 19:24:15 +1000259 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000260 if (ret == 0) {
261 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info);
262 if (ret)
263 drm_gem_handle_delete(file_priv, req->info.handle);
264 }
265
Dave Airlie29d08b32010-09-27 16:17:17 +1000266 /* drop reference from allocate - handle holds it now */
267 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000268 return ret;
269}
270
271static int
272nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
273 uint32_t write_domains, uint32_t valid_domains)
274{
275 struct nouveau_bo *nvbo = gem->driver_private;
276 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000277 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100278 (write_domains ? write_domains : read_domains);
279 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000280
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100281 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000282 return -EINVAL;
283
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100284 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
285 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000286
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100287 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
288 valid_flags |= TTM_PL_FLAG_TT;
289
290 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
291 bo->mem.mem_type == TTM_PL_VRAM)
292 pref_flags |= TTM_PL_FLAG_VRAM;
293
294 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
295 bo->mem.mem_type == TTM_PL_TT)
296 pref_flags |= TTM_PL_FLAG_TT;
297
298 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
299 pref_flags |= TTM_PL_FLAG_VRAM;
300
301 else
302 pref_flags |= TTM_PL_FLAG_TT;
303
304 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
305
Ben Skeggs6ee73862009-12-11 19:24:15 +1000306 return 0;
307}
308
309struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000310 struct list_head vram_list;
311 struct list_head gart_list;
312 struct list_head both_list;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200313 struct ww_acquire_ctx ticket;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000314};
315
316static void
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200317validate_fini_list(struct list_head *list, struct nouveau_fence *fence,
318 struct ww_acquire_ctx *ticket)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000319{
320 struct list_head *entry, *tmp;
321 struct nouveau_bo *nvbo;
322
323 list_for_each_safe(entry, tmp, list) {
324 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000325
Francisco Jerez332b2422010-10-20 23:35:40 +0200326 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000327
Ben Skeggsa1606a92010-02-12 10:27:35 +1000328 if (unlikely(nvbo->validate_mapped)) {
329 ttm_bo_kunmap(&nvbo->kmap);
330 nvbo->validate_mapped = false;
331 }
332
Ben Skeggs6ee73862009-12-11 19:24:15 +1000333 list_del(&nvbo->entry);
334 nvbo->reserved_by = NULL;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200335 ttm_bo_unreserve_ticket(&nvbo->bo, ticket);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200336 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000337 }
338}
339
340static void
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200341validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000342{
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200343 validate_fini_list(&op->vram_list, fence, &op->ticket);
344 validate_fini_list(&op->gart_list, fence, &op->ticket);
345 validate_fini_list(&op->both_list, fence, &op->ticket);
346}
347
348static void
349validate_fini(struct validate_op *op, struct nouveau_fence *fence)
350{
351 validate_fini_no_ticket(op, fence);
352 ww_acquire_fini(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000353}
354
355static int
356validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
357 struct drm_nouveau_gem_pushbuf_bo *pbbo,
358 int nr_buffers, struct validate_op *op)
359{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100360 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000361 struct drm_device *dev = chan->drm->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000362 int trycnt = 0;
363 int ret, i;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100364 struct nouveau_bo *res_bo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000365
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200366 ww_acquire_init(&op->ticket, &reservation_ww_class);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000367retry:
368 if (++trycnt > 100000) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100369 NV_ERROR(cli, "%s failed and gave up.\n", __func__);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000370 return -EINVAL;
371 }
372
373 for (i = 0; i < nr_buffers; i++) {
374 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
375 struct drm_gem_object *gem;
376 struct nouveau_bo *nvbo;
377
378 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
379 if (!gem) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100380 NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200381 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000382 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100383 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000384 }
385 nvbo = gem->driver_private;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100386 if (nvbo == res_bo) {
387 res_bo = NULL;
388 drm_gem_object_unreference_unlocked(gem);
389 continue;
390 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000391
392 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100393 NV_ERROR(cli, "multiple instances of buffer %d on "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000394 "validation list\n", b->handle);
Ben Skeggs5086f692012-06-25 18:04:27 +1000395 drm_gem_object_unreference_unlocked(gem);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200396 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000397 validate_fini(op, NULL);
398 return -EINVAL;
399 }
400
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200401 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000402 if (ret) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200403 validate_fini_no_ticket(op, NULL);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200404 if (unlikely(ret == -EDEADLK)) {
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100405 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200406 &op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100407 if (!ret)
408 res_bo = nvbo;
409 }
Ben Skeggs938c40e2010-10-12 09:54:54 +1000410 if (unlikely(ret)) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200411 ww_acquire_done(&op->ticket);
412 ww_acquire_fini(&op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100413 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000414 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100415 NV_ERROR(cli, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000416 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000417 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000418 }
419
Ben Skeggsa1606a92010-02-12 10:27:35 +1000420 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000421 nvbo->reserved_by = file_priv;
422 nvbo->pbbo_index = i;
423 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
424 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
425 list_add_tail(&nvbo->entry, &op->both_list);
426 else
427 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
428 list_add_tail(&nvbo->entry, &op->vram_list);
429 else
430 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
431 list_add_tail(&nvbo->entry, &op->gart_list);
432 else {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100433 NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000434 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000435 list_add_tail(&nvbo->entry, &op->both_list);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200436 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000437 validate_fini(op, NULL);
438 return -EINVAL;
439 }
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100440 if (nvbo == res_bo)
441 goto retry;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000442 }
443
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200444 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000445 return 0;
446}
447
448static int
Ben Skeggs525895b2012-01-10 10:18:28 +1000449validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
450{
451 struct nouveau_fence *fence = NULL;
452 int ret = 0;
453
454 spin_lock(&nvbo->bo.bdev->fence_lock);
455 if (nvbo->bo.sync_obj)
456 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
457 spin_unlock(&nvbo->bo.bdev->fence_lock);
458
459 if (fence) {
460 ret = nouveau_fence_sync(fence, chan);
461 nouveau_fence_unref(&fence);
462 }
463
464 return ret;
465}
466
467static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100468validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
469 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
470 uint64_t user_pbbo_ptr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000471{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000472 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000473 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
474 (void __force __user *)(uintptr_t)user_pbbo_ptr;
475 struct nouveau_bo *nvbo;
476 int ret, relocs = 0;
477
478 list_for_each_entry(nvbo, list, entry) {
479 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000480
Ben Skeggs525895b2012-01-10 10:18:28 +1000481 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000482 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100483 NV_ERROR(cli, "fail pre-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000484 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000485 }
486
487 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
488 b->write_domains,
489 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000490 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100491 NV_ERROR(cli, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000492 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000493 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000494
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000495 ret = nouveau_bo_validate(nvbo, true, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000496 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000497 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100498 NV_ERROR(cli, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000499 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000500 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000501
Ben Skeggs525895b2012-01-10 10:18:28 +1000502 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000503 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100504 NV_ERROR(cli, "fail post-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000505 return ret;
506 }
507
Ben Skeggsebb945a2012-07-20 08:17:34 +1000508 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000509 if (nvbo->bo.offset == b->presumed.offset &&
510 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
511 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
512 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
513 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
514 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000515
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000516 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
517 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
518 else
519 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
520 b->presumed.offset = nvbo->bo.offset;
521 b->presumed.valid = 0;
522 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000523
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000524 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
525 &b->presumed, sizeof(b->presumed)))
526 return -EFAULT;
527 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000528 }
529
530 return relocs;
531}
532
533static int
534nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
535 struct drm_file *file_priv,
536 struct drm_nouveau_gem_pushbuf_bo *pbbo,
537 uint64_t user_buffers, int nr_buffers,
538 struct validate_op *op, int *apply_relocs)
539{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100540 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000541 int ret, relocs = 0;
542
543 INIT_LIST_HEAD(&op->vram_list);
544 INIT_LIST_HEAD(&op->gart_list);
545 INIT_LIST_HEAD(&op->both_list);
546
Ben Skeggs6ee73862009-12-11 19:24:15 +1000547 if (nr_buffers == 0)
548 return 0;
549
550 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000551 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000552 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100553 NV_ERROR(cli, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000554 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000555 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000556
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100557 ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000558 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000559 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100560 NV_ERROR(cli, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000561 validate_fini(op, NULL);
562 return ret;
563 }
564 relocs += ret;
565
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100566 ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000567 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000568 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100569 NV_ERROR(cli, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000570 validate_fini(op, NULL);
571 return ret;
572 }
573 relocs += ret;
574
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100575 ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000576 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000577 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100578 NV_ERROR(cli, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000579 validate_fini(op, NULL);
580 return ret;
581 }
582 relocs += ret;
583
584 *apply_relocs = relocs;
585 return 0;
586}
587
588static inline void *
589u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
590{
591 void *mem;
592 void __user *userptr = (void __force __user *)(uintptr_t)user;
593
594 mem = kmalloc(nmemb * size, GFP_KERNEL);
595 if (!mem)
596 return ERR_PTR(-ENOMEM);
597
598 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
599 kfree(mem);
600 return ERR_PTR(-EFAULT);
601 }
602
603 return mem;
604}
605
606static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100607nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
Ben Skeggsa1606a92010-02-12 10:27:35 +1000608 struct drm_nouveau_gem_pushbuf *req,
609 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610{
611 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100612 int ret = 0;
613 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000614
Ben Skeggsa1606a92010-02-12 10:27:35 +1000615 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000616 if (IS_ERR(reloc))
617 return PTR_ERR(reloc);
618
Ben Skeggsa1606a92010-02-12 10:27:35 +1000619 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000620 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
621 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000622 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000623 uint32_t data;
624
Ben Skeggsa1606a92010-02-12 10:27:35 +1000625 if (unlikely(r->bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100626 NV_ERROR(cli, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000627 ret = -EINVAL;
628 break;
629 }
630
631 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000632 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000633 continue;
634
Ben Skeggsa1606a92010-02-12 10:27:35 +1000635 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100636 NV_ERROR(cli, "reloc container bo index invalid\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000637 ret = -EINVAL;
638 break;
639 }
640 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
641
642 if (unlikely(r->reloc_bo_offset + 4 >
643 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100644 NV_ERROR(cli, "reloc outside of bo\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000645 ret = -EINVAL;
646 break;
647 }
648
649 if (!nvbo->kmap.virtual) {
650 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
651 &nvbo->kmap);
652 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100653 NV_ERROR(cli, "failed kmap for reloc\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000654 break;
655 }
656 nvbo->validate_mapped = true;
657 }
658
Ben Skeggs6ee73862009-12-11 19:24:15 +1000659 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000660 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000661 else
662 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000663 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000664 else
665 data = r->data;
666
667 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000668 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000669 data |= r->tor;
670 else
671 data |= r->vor;
672 }
673
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000674 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000675 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000676 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000677 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100678 NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000679 break;
680 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000681
682 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000683 }
684
685 kfree(reloc);
686 return ret;
687}
688
689int
690nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
691 struct drm_file *file_priv)
692{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000693 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100694 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000695 struct nouveau_abi16_chan *temp;
696 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000697 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000698 struct drm_nouveau_gem_pushbuf_push *push;
699 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000700 struct nouveau_channel *chan = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000701 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200702 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000703 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000704
Ben Skeggsebb945a2012-07-20 08:17:34 +1000705 if (unlikely(!abi16))
706 return -ENOMEM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000707
Ben Skeggsebb945a2012-07-20 08:17:34 +1000708 list_for_each_entry(temp, &abi16->channels, head) {
709 if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
710 chan = temp->chan;
711 break;
712 }
713 }
714
715 if (!chan)
716 return nouveau_abi16_put(abi16, -ENOENT);
717
718 req->vram_available = drm->gem.vram_available;
719 req->gart_available = drm->gem.gart_available;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000720 if (unlikely(req->nr_push == 0))
721 goto out_next;
722
723 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100724 NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000725 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000726 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000727 }
728
Ben Skeggsa1606a92010-02-12 10:27:35 +1000729 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100730 NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000731 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000732 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000733 }
734
735 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100736 NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000737 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000738 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000739 }
740
741 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggsebb945a2012-07-20 08:17:34 +1000742 if (IS_ERR(push))
743 return nouveau_abi16_put(abi16, PTR_ERR(push));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000744
745 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
746 if (IS_ERR(bo)) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000747 kfree(push);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000748 return nouveau_abi16_put(abi16, PTR_ERR(bo));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000749 }
750
Ben Skeggsaccf9492012-03-16 12:40:17 +1000751 /* Ensure all push buffers are on validate list */
Ben Skeggs415e6182010-07-23 09:06:52 +1000752 for (i = 0; i < req->nr_push; i++) {
753 if (push[i].bo_index >= req->nr_buffers) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100754 NV_ERROR(cli, "push %d buffer not in list\n", i);
Ben Skeggs415e6182010-07-23 09:06:52 +1000755 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100756 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000757 }
Ben Skeggs415e6182010-07-23 09:06:52 +1000758 }
759
Ben Skeggs6ee73862009-12-11 19:24:15 +1000760 /* Validate buffer list */
761 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
762 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000763 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000764 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100765 NV_ERROR(cli, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100766 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000767 }
768
Ben Skeggs6ee73862009-12-11 19:24:15 +1000769 /* Apply any relocations that are required */
770 if (do_reloc) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100771 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000772 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100773 NV_ERROR(cli, "reloc apply: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000774 goto out;
775 }
776 }
777
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000778 if (chan->dma.ib_max) {
Ben Skeggs5e120f62012-04-30 13:55:29 +1000779 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000780 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100781 NV_ERROR(cli, "nv50cal_space: %d\n", ret);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000782 goto out;
783 }
784
Ben Skeggsa1606a92010-02-12 10:27:35 +1000785 for (i = 0; i < req->nr_push; i++) {
786 struct nouveau_bo *nvbo = (void *)(unsigned long)
787 bo[push[i].bo_index].user_priv;
788
789 nv50_dma_push(chan, nvbo, push[i].offset,
790 push[i].length);
791 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000792 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000793 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000794 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000795 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100796 NV_ERROR(cli, "cal_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000797 goto out;
798 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000799
800 for (i = 0; i < req->nr_push; i++) {
801 struct nouveau_bo *nvbo = (void *)(unsigned long)
802 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000803
Ben Skeggs3a92d372012-08-04 05:46:01 +1000804 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000805 OUT_RING(chan, 0);
806 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000807 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000808 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000809 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100810 NV_ERROR(cli, "jmp_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811 goto out;
812 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000813
Ben Skeggsa1606a92010-02-12 10:27:35 +1000814 for (i = 0; i < req->nr_push; i++) {
815 struct nouveau_bo *nvbo = (void *)(unsigned long)
816 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000817 uint32_t cmd;
818
Ben Skeggsebb945a2012-07-20 08:17:34 +1000819 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000820 cmd |= 0x20000000;
821 if (unlikely(cmd != req->suffix0)) {
822 if (!nvbo->kmap.virtual) {
823 ret = ttm_bo_kmap(&nvbo->bo, 0,
824 nvbo->bo.mem.
825 num_pages,
826 &nvbo->kmap);
827 if (ret) {
828 WIND_RING(chan);
829 goto out;
830 }
831 nvbo->validate_mapped = true;
832 }
833
834 nouveau_bo_wr32(nvbo, (push[i].offset +
835 push[i].length - 8) / 4, cmd);
836 }
837
Ben Skeggs3a92d372012-08-04 05:46:01 +1000838 OUT_RING(chan, 0x20000000 |
839 (nvbo->bo.offset + push[i].offset));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000840 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000841 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
842 OUT_RING(chan, 0);
843 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000844 }
845
Ben Skeggs264ce192013-02-14 13:43:21 +1000846 ret = nouveau_fence_new(chan, false, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000847 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100848 NV_ERROR(cli, "error fencing pushbuf: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000849 WIND_RING(chan);
850 goto out;
851 }
852
853out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100854 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200855 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100856
857out_prevalid:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000858 kfree(bo);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000859 kfree(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000860
861out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000862 if (chan->dma.ib_max) {
863 req->suffix0 = 0x00000000;
864 req->suffix1 = 0x00000000;
865 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000866 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000867 req->suffix0 = 0x00020000;
868 req->suffix1 = 0x00000000;
869 } else {
870 req->suffix0 = 0x20000000 |
Ben Skeggsebb945a2012-07-20 08:17:34 +1000871 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000872 req->suffix1 = 0x00000000;
873 }
874
Ben Skeggsebb945a2012-07-20 08:17:34 +1000875 return nouveau_abi16_put(abi16, ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000876}
877
Ben Skeggs6ee73862009-12-11 19:24:15 +1000878static inline uint32_t
879domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
880{
881 uint32_t flags = 0;
882
883 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
884 flags |= TTM_PL_FLAG_VRAM;
885 if (domain & NOUVEAU_GEM_DOMAIN_GART)
886 flags |= TTM_PL_FLAG_TT;
887
888 return flags;
889}
890
891int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000892nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
893 struct drm_file *file_priv)
894{
895 struct drm_nouveau_gem_cpu_prep *req = data;
896 struct drm_gem_object *gem;
897 struct nouveau_bo *nvbo;
898 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
899 int ret = -EINVAL;
900
Ben Skeggs6ee73862009-12-11 19:24:15 +1000901 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
902 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100903 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000904 nvbo = nouveau_gem_object(gem);
905
Ben Skeggs21e86c12010-10-11 11:48:45 +1000906 spin_lock(&nvbo->bo.bdev->fence_lock);
907 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
908 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000909 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000910 return ret;
911}
912
913int
914nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
915 struct drm_file *file_priv)
916{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000917 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918}
919
920int
921nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
922 struct drm_file *file_priv)
923{
924 struct drm_nouveau_gem_info *req = data;
925 struct drm_gem_object *gem;
926 int ret;
927
Ben Skeggs6ee73862009-12-11 19:24:15 +1000928 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
929 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100930 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000931
Ben Skeggse758a312011-06-07 13:28:11 +1000932 ret = nouveau_gem_info(file_priv, gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000933 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000934 return ret;
935}
936