blob: 6618318abf50ac2e5afbd3539d3a44db28e36e3f [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{
David Herrmann55fb74a2013-10-02 10:15:17 +020046 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +100047 struct ttm_buffer_object *bo = &nvbo->bo;
48
Dave Airlie22b33e82012-04-02 11:53:06 +010049 if (gem->import_attach)
50 drm_prime_gem_destroy(gem, nvbo->bo.sg);
51
Daniel Vetterfd632aa2010-04-09 19:05:05 +000052 drm_gem_object_release(gem);
David Herrmann55fb74a2013-10-02 10:15:17 +020053
54 /* reset filp so nouveau_bo_del_ttm() can test for it */
55 gem->filp = NULL;
56 ttm_bo_unref(&bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +100057}
58
59int
Ben Skeggs639212d2011-06-03 16:18:26 +100060nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
61{
Ben Skeggsebb945a2012-07-20 08:17:34 +100062 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100063 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
64 struct nouveau_vma *vma;
65 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100066
Ben Skeggsebb945a2012-07-20 08:17:34 +100067 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +100068 return 0;
69
Ben Skeggs2fd3db62011-06-07 15:25:12 +100070 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
71 if (ret)
72 return ret;
73
Ben Skeggsebb945a2012-07-20 08:17:34 +100074 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100075 if (!vma) {
76 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
77 if (!vma) {
78 ret = -ENOMEM;
79 goto out;
80 }
81
Ben Skeggsebb945a2012-07-20 08:17:34 +100082 ret = nouveau_bo_vma_add(nvbo, cli->base.vm, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100083 if (ret) {
84 kfree(vma);
85 goto out;
86 }
87 } else {
88 vma->refcount++;
89 }
90
91out:
92 ttm_bo_unreserve(&nvbo->bo);
93 return ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100094}
95
Ben Skeggsc4c70442013-05-07 09:48:30 +100096static void
97nouveau_gem_object_delete(void *data)
98{
99 struct nouveau_vma *vma = data;
100 nouveau_vm_unmap(vma);
101 nouveau_vm_put(vma);
102 kfree(vma);
103}
104
105static void
106nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
107{
108 const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
109 struct nouveau_fence *fence = NULL;
110
111 list_del(&vma->head);
112
113 if (mapped) {
114 spin_lock(&nvbo->bo.bdev->fence_lock);
115 if (nvbo->bo.sync_obj)
116 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
117 spin_unlock(&nvbo->bo.bdev->fence_lock);
118 }
119
120 if (fence) {
121 nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
122 } else {
123 if (mapped)
124 nouveau_vm_unmap(vma);
125 nouveau_vm_put(vma);
126 kfree(vma);
127 }
128 nouveau_fence_unref(&fence);
129}
130
Ben Skeggs639212d2011-06-03 16:18:26 +1000131void
132nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
133{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000134 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000135 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
136 struct nouveau_vma *vma;
137 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000138
Ben Skeggsebb945a2012-07-20 08:17:34 +1000139 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +1000140 return;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000141
142 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
143 if (ret)
144 return;
145
Ben Skeggsebb945a2012-07-20 08:17:34 +1000146 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000147 if (vma) {
Ben Skeggsc4c70442013-05-07 09:48:30 +1000148 if (--vma->refcount == 0)
149 nouveau_gem_object_unmap(nvbo, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000150 }
151 ttm_bo_unreserve(&nvbo->bo);
Ben Skeggs639212d2011-06-03 16:18:26 +1000152}
153
154int
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000155nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
156 uint32_t tile_mode, uint32_t tile_flags,
157 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000158{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000159 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000160 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000161 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000162 int ret;
163
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000164 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
165 flags |= TTM_PL_FLAG_VRAM;
166 if (domain & NOUVEAU_GEM_DOMAIN_GART)
167 flags |= TTM_PL_FLAG_TT;
168 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
169 flags |= TTM_PL_FLAG_SYSTEM;
170
Ben Skeggs7375c952011-06-07 14:21:29 +1000171 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
Dave Airlie22b33e82012-04-02 11:53:06 +0100172 tile_flags, NULL, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000173 if (ret)
174 return ret;
175 nvbo = *pnvbo;
176
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000177 /* we restrict allowed domains on nv50+ to only the types
178 * that were requested at creation time. not possibly on
179 * earlier chips without busting the ABI.
180 */
181 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
182 NOUVEAU_GEM_DOMAIN_GART;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000183 if (nv_device(drm->device)->card_type >= NV_50)
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000184 nvbo->valid_domains &= domain;
185
David Herrmann55fb74a2013-10-02 10:15:17 +0200186 /* Initialize the embedded gem-object. We return a single gem-reference
187 * to the caller, instead of a normal nouveau_bo ttm reference. */
188 ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size);
189 if (ret) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000190 nouveau_bo_ref(NULL, pnvbo);
191 return -ENOMEM;
192 }
193
David Herrmann55fb74a2013-10-02 10:15:17 +0200194 nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000195 return 0;
196}
197
198static int
Ben Skeggse758a312011-06-07 13:28:11 +1000199nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
200 struct drm_nouveau_gem_info *rep)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000201{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000202 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000203 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggse758a312011-06-07 13:28:11 +1000204 struct nouveau_vma *vma;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000205
206 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
207 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
208 else
209 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
210
Ben Skeggse758a312011-06-07 13:28:11 +1000211 rep->offset = nvbo->bo.offset;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000212 if (cli->base.vm) {
213 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggse758a312011-06-07 13:28:11 +1000214 if (!vma)
215 return -EINVAL;
216
217 rep->offset = vma->offset;
218 }
219
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
David Herrmann72525b32013-07-24 21:08:53 +0200221 rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222 rep->tile_mode = nvbo->tile_mode;
223 rep->tile_flags = nvbo->tile_flags;
224 return 0;
225}
226
Ben Skeggs6ee73862009-12-11 19:24:15 +1000227int
228nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
229 struct drm_file *file_priv)
230{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000231 struct nouveau_drm *drm = nouveau_drm(dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100232 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000233 struct nouveau_fb *pfb = nouveau_fb(drm->device);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000234 struct drm_nouveau_gem_new *req = data;
235 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000236 int ret = 0;
237
Ben Skeggsebb945a2012-07-20 08:17:34 +1000238 drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000239
Ben Skeggsebb945a2012-07-20 08:17:34 +1000240 if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100241 NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000242 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000243 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000244
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000245 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000246 req->info.domain, req->info.tile_mode,
247 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000248 if (ret)
249 return ret;
250
David Herrmann55fb74a2013-10-02 10:15:17 +0200251 ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000252 if (ret == 0) {
David Herrmann55fb74a2013-10-02 10:15:17 +0200253 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
Ben Skeggse758a312011-06-07 13:28:11 +1000254 if (ret)
255 drm_gem_handle_delete(file_priv, req->info.handle);
256 }
257
Dave Airlie29d08b32010-09-27 16:17:17 +1000258 /* drop reference from allocate - handle holds it now */
David Herrmann55fb74a2013-10-02 10:15:17 +0200259 drm_gem_object_unreference_unlocked(&nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000260 return ret;
261}
262
263static int
264nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
265 uint32_t write_domains, uint32_t valid_domains)
266{
David Herrmann55fb74a2013-10-02 10:15:17 +0200267 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000268 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000269 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100270 (write_domains ? write_domains : read_domains);
271 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000272
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100273 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000274 return -EINVAL;
275
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100276 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
277 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000278
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100279 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
280 valid_flags |= TTM_PL_FLAG_TT;
281
282 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
283 bo->mem.mem_type == TTM_PL_VRAM)
284 pref_flags |= TTM_PL_FLAG_VRAM;
285
286 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
287 bo->mem.mem_type == TTM_PL_TT)
288 pref_flags |= TTM_PL_FLAG_TT;
289
290 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
291 pref_flags |= TTM_PL_FLAG_VRAM;
292
293 else
294 pref_flags |= TTM_PL_FLAG_TT;
295
296 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
297
Ben Skeggs6ee73862009-12-11 19:24:15 +1000298 return 0;
299}
300
301struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000302 struct list_head vram_list;
303 struct list_head gart_list;
304 struct list_head both_list;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200305 struct ww_acquire_ctx ticket;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000306};
307
308static void
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200309validate_fini_list(struct list_head *list, struct nouveau_fence *fence,
310 struct ww_acquire_ctx *ticket)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000311{
312 struct list_head *entry, *tmp;
313 struct nouveau_bo *nvbo;
314
315 list_for_each_safe(entry, tmp, list) {
316 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000317
Francisco Jerez332b2422010-10-20 23:35:40 +0200318 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000319
Ben Skeggsa1606a92010-02-12 10:27:35 +1000320 if (unlikely(nvbo->validate_mapped)) {
321 ttm_bo_kunmap(&nvbo->kmap);
322 nvbo->validate_mapped = false;
323 }
324
Ben Skeggs6ee73862009-12-11 19:24:15 +1000325 list_del(&nvbo->entry);
326 nvbo->reserved_by = NULL;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200327 ttm_bo_unreserve_ticket(&nvbo->bo, ticket);
David Herrmann55fb74a2013-10-02 10:15:17 +0200328 drm_gem_object_unreference_unlocked(&nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000329 }
330}
331
332static void
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200333validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000334{
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200335 validate_fini_list(&op->vram_list, fence, &op->ticket);
336 validate_fini_list(&op->gart_list, fence, &op->ticket);
337 validate_fini_list(&op->both_list, fence, &op->ticket);
338}
339
340static void
341validate_fini(struct validate_op *op, struct nouveau_fence *fence)
342{
343 validate_fini_no_ticket(op, fence);
344 ww_acquire_fini(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000345}
346
347static int
348validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
349 struct drm_nouveau_gem_pushbuf_bo *pbbo,
350 int nr_buffers, struct validate_op *op)
351{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100352 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000353 struct drm_device *dev = chan->drm->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000354 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
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200358 ww_acquire_init(&op->ticket, &reservation_ww_class);
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);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200373 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000374 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100375 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000376 }
David Herrmann55fb74a2013-10-02 10:15:17 +0200377 nvbo = nouveau_gem_object(gem);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100378 if (nvbo == res_bo) {
379 res_bo = NULL;
380 drm_gem_object_unreference_unlocked(gem);
381 continue;
382 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000383
384 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100385 NV_ERROR(cli, "multiple instances of buffer %d on "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000386 "validation list\n", b->handle);
Ben Skeggs5086f692012-06-25 18:04:27 +1000387 drm_gem_object_unreference_unlocked(gem);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200388 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000389 validate_fini(op, NULL);
390 return -EINVAL;
391 }
392
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200393 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000394 if (ret) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200395 validate_fini_no_ticket(op, NULL);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200396 if (unlikely(ret == -EDEADLK)) {
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100397 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200398 &op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100399 if (!ret)
400 res_bo = nvbo;
401 }
Ben Skeggs938c40e2010-10-12 09:54:54 +1000402 if (unlikely(ret)) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200403 ww_acquire_done(&op->ticket);
404 ww_acquire_fini(&op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100405 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000406 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100407 NV_ERROR(cli, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000408 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000409 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000410 }
411
Ben Skeggsa1606a92010-02-12 10:27:35 +1000412 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000413 nvbo->reserved_by = file_priv;
414 nvbo->pbbo_index = i;
415 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
416 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
417 list_add_tail(&nvbo->entry, &op->both_list);
418 else
419 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
420 list_add_tail(&nvbo->entry, &op->vram_list);
421 else
422 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
423 list_add_tail(&nvbo->entry, &op->gart_list);
424 else {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100425 NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000426 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000427 list_add_tail(&nvbo->entry, &op->both_list);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200428 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000429 validate_fini(op, NULL);
430 return -EINVAL;
431 }
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100432 if (nvbo == res_bo)
433 goto retry;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000434 }
435
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200436 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000437 return 0;
438}
439
440static int
Ben Skeggs525895b2012-01-10 10:18:28 +1000441validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
442{
443 struct nouveau_fence *fence = NULL;
444 int ret = 0;
445
446 spin_lock(&nvbo->bo.bdev->fence_lock);
447 if (nvbo->bo.sync_obj)
448 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
449 spin_unlock(&nvbo->bo.bdev->fence_lock);
450
451 if (fence) {
452 ret = nouveau_fence_sync(fence, chan);
453 nouveau_fence_unref(&fence);
454 }
455
456 return ret;
457}
458
459static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100460validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
461 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
462 uint64_t user_pbbo_ptr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000463{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000464 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000465 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
466 (void __force __user *)(uintptr_t)user_pbbo_ptr;
467 struct nouveau_bo *nvbo;
468 int ret, relocs = 0;
469
470 list_for_each_entry(nvbo, list, entry) {
471 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000472
Ben Skeggs525895b2012-01-10 10:18:28 +1000473 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000474 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100475 NV_ERROR(cli, "fail pre-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000476 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000477 }
478
David Herrmann55fb74a2013-10-02 10:15:17 +0200479 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000480 b->write_domains,
481 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000482 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100483 NV_ERROR(cli, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000484 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000485 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000486
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000487 ret = nouveau_bo_validate(nvbo, true, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000488 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000489 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100490 NV_ERROR(cli, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000491 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000492 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000493
Ben Skeggs525895b2012-01-10 10:18:28 +1000494 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000495 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100496 NV_ERROR(cli, "fail post-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000497 return ret;
498 }
499
Ben Skeggsebb945a2012-07-20 08:17:34 +1000500 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000501 if (nvbo->bo.offset == b->presumed.offset &&
502 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
503 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
504 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
505 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
506 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000507
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000508 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
509 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
510 else
511 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
512 b->presumed.offset = nvbo->bo.offset;
513 b->presumed.valid = 0;
514 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000515
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000516 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
517 &b->presumed, sizeof(b->presumed)))
518 return -EFAULT;
519 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000520 }
521
522 return relocs;
523}
524
525static int
526nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
527 struct drm_file *file_priv,
528 struct drm_nouveau_gem_pushbuf_bo *pbbo,
529 uint64_t user_buffers, int nr_buffers,
530 struct validate_op *op, int *apply_relocs)
531{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100532 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000533 int ret, relocs = 0;
534
535 INIT_LIST_HEAD(&op->vram_list);
536 INIT_LIST_HEAD(&op->gart_list);
537 INIT_LIST_HEAD(&op->both_list);
538
Ben Skeggs6ee73862009-12-11 19:24:15 +1000539 if (nr_buffers == 0)
540 return 0;
541
542 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000543 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000544 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100545 NV_ERROR(cli, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000546 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000547 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000548
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100549 ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000550 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000551 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100552 NV_ERROR(cli, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000553 validate_fini(op, NULL);
554 return ret;
555 }
556 relocs += ret;
557
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100558 ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000559 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000560 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100561 NV_ERROR(cli, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000562 validate_fini(op, NULL);
563 return ret;
564 }
565 relocs += ret;
566
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100567 ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000568 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000569 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100570 NV_ERROR(cli, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000571 validate_fini(op, NULL);
572 return ret;
573 }
574 relocs += ret;
575
576 *apply_relocs = relocs;
577 return 0;
578}
579
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200580static inline void
581u_free(void *addr)
582{
583 if (!is_vmalloc_addr(addr))
584 kfree(addr);
585 else
586 vfree(addr);
587}
588
Ben Skeggs6ee73862009-12-11 19:24:15 +1000589static inline void *
590u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
591{
592 void *mem;
593 void __user *userptr = (void __force __user *)(uintptr_t)user;
594
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200595 size *= nmemb;
596
597 mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
598 if (!mem)
599 mem = vmalloc(size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000600 if (!mem)
601 return ERR_PTR(-ENOMEM);
602
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200603 if (DRM_COPY_FROM_USER(mem, userptr, size)) {
604 u_free(mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000605 return ERR_PTR(-EFAULT);
606 }
607
608 return mem;
609}
610
611static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100612nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
Ben Skeggsa1606a92010-02-12 10:27:35 +1000613 struct drm_nouveau_gem_pushbuf *req,
614 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000615{
616 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100617 int ret = 0;
618 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000619
Ben Skeggsa1606a92010-02-12 10:27:35 +1000620 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000621 if (IS_ERR(reloc))
622 return PTR_ERR(reloc);
623
Ben Skeggsa1606a92010-02-12 10:27:35 +1000624 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000625 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
626 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000627 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000628 uint32_t data;
629
Ben Skeggsa1606a92010-02-12 10:27:35 +1000630 if (unlikely(r->bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100631 NV_ERROR(cli, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000632 ret = -EINVAL;
633 break;
634 }
635
636 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000637 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000638 continue;
639
Ben Skeggsa1606a92010-02-12 10:27:35 +1000640 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100641 NV_ERROR(cli, "reloc container bo index invalid\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000642 ret = -EINVAL;
643 break;
644 }
645 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
646
647 if (unlikely(r->reloc_bo_offset + 4 >
648 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100649 NV_ERROR(cli, "reloc outside of bo\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000650 ret = -EINVAL;
651 break;
652 }
653
654 if (!nvbo->kmap.virtual) {
655 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
656 &nvbo->kmap);
657 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100658 NV_ERROR(cli, "failed kmap for reloc\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000659 break;
660 }
661 nvbo->validate_mapped = true;
662 }
663
Ben Skeggs6ee73862009-12-11 19:24:15 +1000664 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000665 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000666 else
667 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000668 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000669 else
670 data = r->data;
671
672 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000673 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000674 data |= r->tor;
675 else
676 data |= r->vor;
677 }
678
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000679 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000680 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000681 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000682 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100683 NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000684 break;
685 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000686
687 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000688 }
689
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200690 u_free(reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000691 return ret;
692}
693
694int
695nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
696 struct drm_file *file_priv)
697{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000698 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100699 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000700 struct nouveau_abi16_chan *temp;
701 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000702 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000703 struct drm_nouveau_gem_pushbuf_push *push;
704 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000705 struct nouveau_channel *chan = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000706 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200707 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000708 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000709
Ben Skeggsebb945a2012-07-20 08:17:34 +1000710 if (unlikely(!abi16))
711 return -ENOMEM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000712
Ben Skeggsebb945a2012-07-20 08:17:34 +1000713 list_for_each_entry(temp, &abi16->channels, head) {
714 if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
715 chan = temp->chan;
716 break;
717 }
718 }
719
720 if (!chan)
721 return nouveau_abi16_put(abi16, -ENOENT);
722
723 req->vram_available = drm->gem.vram_available;
724 req->gart_available = drm->gem.gart_available;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000725 if (unlikely(req->nr_push == 0))
726 goto out_next;
727
728 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100729 NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000730 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000731 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000732 }
733
Ben Skeggsa1606a92010-02-12 10:27:35 +1000734 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100735 NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000736 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000737 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000738 }
739
740 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100741 NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000742 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000743 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000744 }
745
746 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggsebb945a2012-07-20 08:17:34 +1000747 if (IS_ERR(push))
748 return nouveau_abi16_put(abi16, PTR_ERR(push));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000749
750 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
751 if (IS_ERR(bo)) {
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200752 u_free(push);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000753 return nouveau_abi16_put(abi16, PTR_ERR(bo));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000754 }
755
Ben Skeggsaccf9492012-03-16 12:40:17 +1000756 /* Ensure all push buffers are on validate list */
Ben Skeggs415e6182010-07-23 09:06:52 +1000757 for (i = 0; i < req->nr_push; i++) {
758 if (push[i].bo_index >= req->nr_buffers) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100759 NV_ERROR(cli, "push %d buffer not in list\n", i);
Ben Skeggs415e6182010-07-23 09:06:52 +1000760 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100761 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000762 }
Ben Skeggs415e6182010-07-23 09:06:52 +1000763 }
764
Ben Skeggs6ee73862009-12-11 19:24:15 +1000765 /* Validate buffer list */
766 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
767 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000768 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000769 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100770 NV_ERROR(cli, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100771 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000772 }
773
Ben Skeggs6ee73862009-12-11 19:24:15 +1000774 /* Apply any relocations that are required */
775 if (do_reloc) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100776 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000777 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100778 NV_ERROR(cli, "reloc apply: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000779 goto out;
780 }
781 }
782
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000783 if (chan->dma.ib_max) {
Ben Skeggs5e120f62012-04-30 13:55:29 +1000784 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000785 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100786 NV_ERROR(cli, "nv50cal_space: %d\n", ret);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000787 goto out;
788 }
789
Ben Skeggsa1606a92010-02-12 10:27:35 +1000790 for (i = 0; i < req->nr_push; i++) {
791 struct nouveau_bo *nvbo = (void *)(unsigned long)
792 bo[push[i].bo_index].user_priv;
793
794 nv50_dma_push(chan, nvbo, push[i].offset,
795 push[i].length);
796 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000797 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000798 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000799 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000800 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100801 NV_ERROR(cli, "cal_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000802 goto out;
803 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000804
805 for (i = 0; i < req->nr_push; i++) {
806 struct nouveau_bo *nvbo = (void *)(unsigned long)
807 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000808
Ben Skeggs3a92d372012-08-04 05:46:01 +1000809 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000810 OUT_RING(chan, 0);
811 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000812 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000813 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000814 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100815 NV_ERROR(cli, "jmp_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000816 goto out;
817 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000818
Ben Skeggsa1606a92010-02-12 10:27:35 +1000819 for (i = 0; i < req->nr_push; i++) {
820 struct nouveau_bo *nvbo = (void *)(unsigned long)
821 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000822 uint32_t cmd;
823
Ben Skeggsebb945a2012-07-20 08:17:34 +1000824 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000825 cmd |= 0x20000000;
826 if (unlikely(cmd != req->suffix0)) {
827 if (!nvbo->kmap.virtual) {
828 ret = ttm_bo_kmap(&nvbo->bo, 0,
829 nvbo->bo.mem.
830 num_pages,
831 &nvbo->kmap);
832 if (ret) {
833 WIND_RING(chan);
834 goto out;
835 }
836 nvbo->validate_mapped = true;
837 }
838
839 nouveau_bo_wr32(nvbo, (push[i].offset +
840 push[i].length - 8) / 4, cmd);
841 }
842
Ben Skeggs3a92d372012-08-04 05:46:01 +1000843 OUT_RING(chan, 0x20000000 |
844 (nvbo->bo.offset + push[i].offset));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000845 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000846 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
847 OUT_RING(chan, 0);
848 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000849 }
850
Ben Skeggs264ce192013-02-14 13:43:21 +1000851 ret = nouveau_fence_new(chan, false, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000852 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100853 NV_ERROR(cli, "error fencing pushbuf: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000854 WIND_RING(chan);
855 goto out;
856 }
857
858out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100859 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200860 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100861
862out_prevalid:
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200863 u_free(bo);
864 u_free(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000865
866out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000867 if (chan->dma.ib_max) {
868 req->suffix0 = 0x00000000;
869 req->suffix1 = 0x00000000;
870 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000871 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000872 req->suffix0 = 0x00020000;
873 req->suffix1 = 0x00000000;
874 } else {
875 req->suffix0 = 0x20000000 |
Ben Skeggsebb945a2012-07-20 08:17:34 +1000876 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000877 req->suffix1 = 0x00000000;
878 }
879
Ben Skeggsebb945a2012-07-20 08:17:34 +1000880 return nouveau_abi16_put(abi16, ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000881}
882
Ben Skeggs6ee73862009-12-11 19:24:15 +1000883static inline uint32_t
884domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
885{
886 uint32_t flags = 0;
887
888 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
889 flags |= TTM_PL_FLAG_VRAM;
890 if (domain & NOUVEAU_GEM_DOMAIN_GART)
891 flags |= TTM_PL_FLAG_TT;
892
893 return flags;
894}
895
896int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000897nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
898 struct drm_file *file_priv)
899{
900 struct drm_nouveau_gem_cpu_prep *req = data;
901 struct drm_gem_object *gem;
902 struct nouveau_bo *nvbo;
903 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
904 int ret = -EINVAL;
905
Ben Skeggs6ee73862009-12-11 19:24:15 +1000906 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
907 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100908 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000909 nvbo = nouveau_gem_object(gem);
910
Ben Skeggs21e86c12010-10-11 11:48:45 +1000911 spin_lock(&nvbo->bo.bdev->fence_lock);
912 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
913 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000914 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000915 return ret;
916}
917
918int
919nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
920 struct drm_file *file_priv)
921{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000922 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000923}
924
925int
926nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
927 struct drm_file *file_priv)
928{
929 struct drm_nouveau_gem_info *req = data;
930 struct drm_gem_object *gem;
931 int ret;
932
Ben Skeggs6ee73862009-12-11 19:24:15 +1000933 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
934 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100935 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000936
Ben Skeggse758a312011-06-07 13:28:11 +1000937 ret = nouveau_gem_info(file_priv, gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000938 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000939 return ret;
940}
941