blob: c90c0dc0afe8aba5c8730491b046232b8a1a574a [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
Ben Skeggs6ee73862009-12-11 19:24:15 +100037void
38nouveau_gem_object_del(struct drm_gem_object *gem)
39{
David Herrmann55fb74a2013-10-02 10:15:17 +020040 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +100041 struct ttm_buffer_object *bo = &nvbo->bo;
42
Dave Airlie22b33e82012-04-02 11:53:06 +010043 if (gem->import_attach)
44 drm_prime_gem_destroy(gem, nvbo->bo.sg);
45
Daniel Vetterfd632aa2010-04-09 19:05:05 +000046 drm_gem_object_release(gem);
David Herrmann55fb74a2013-10-02 10:15:17 +020047
48 /* reset filp so nouveau_bo_del_ttm() can test for it */
49 gem->filp = NULL;
50 ttm_bo_unref(&bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +100051}
52
53int
Ben Skeggs639212d2011-06-03 16:18:26 +100054nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
55{
Ben Skeggsebb945a2012-07-20 08:17:34 +100056 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100057 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
58 struct nouveau_vma *vma;
59 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100060
Ben Skeggsebb945a2012-07-20 08:17:34 +100061 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +100062 return 0;
63
Ben Skeggs2fd3db62011-06-07 15:25:12 +100064 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
65 if (ret)
66 return ret;
67
Ben Skeggsebb945a2012-07-20 08:17:34 +100068 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100069 if (!vma) {
70 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
71 if (!vma) {
72 ret = -ENOMEM;
73 goto out;
74 }
75
Ben Skeggsebb945a2012-07-20 08:17:34 +100076 ret = nouveau_bo_vma_add(nvbo, cli->base.vm, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100077 if (ret) {
78 kfree(vma);
79 goto out;
80 }
81 } else {
82 vma->refcount++;
83 }
84
85out:
86 ttm_bo_unreserve(&nvbo->bo);
87 return ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100088}
89
Ben Skeggsc4c70442013-05-07 09:48:30 +100090static void
91nouveau_gem_object_delete(void *data)
92{
93 struct nouveau_vma *vma = data;
94 nouveau_vm_unmap(vma);
95 nouveau_vm_put(vma);
96 kfree(vma);
97}
98
99static void
100nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
101{
102 const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM;
103 struct nouveau_fence *fence = NULL;
104
105 list_del(&vma->head);
106
107 if (mapped) {
108 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggs5d216f62013-11-13 10:23:46 +1000109 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
Ben Skeggsc4c70442013-05-07 09:48:30 +1000110 spin_unlock(&nvbo->bo.bdev->fence_lock);
111 }
112
113 if (fence) {
114 nouveau_fence_work(fence, nouveau_gem_object_delete, vma);
115 } else {
116 if (mapped)
117 nouveau_vm_unmap(vma);
118 nouveau_vm_put(vma);
119 kfree(vma);
120 }
121 nouveau_fence_unref(&fence);
122}
123
Ben Skeggs639212d2011-06-03 16:18:26 +1000124void
125nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
126{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000127 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000128 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
129 struct nouveau_vma *vma;
130 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000131
Ben Skeggsebb945a2012-07-20 08:17:34 +1000132 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +1000133 return;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000134
135 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
136 if (ret)
137 return;
138
Ben Skeggsebb945a2012-07-20 08:17:34 +1000139 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000140 if (vma) {
Ben Skeggsc4c70442013-05-07 09:48:30 +1000141 if (--vma->refcount == 0)
142 nouveau_gem_object_unmap(nvbo, vma);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000143 }
144 ttm_bo_unreserve(&nvbo->bo);
Ben Skeggs639212d2011-06-03 16:18:26 +1000145}
146
147int
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000148nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
149 uint32_t tile_mode, uint32_t tile_flags,
150 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000151{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000152 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000153 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000154 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000155 int ret;
156
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000157 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
158 flags |= TTM_PL_FLAG_VRAM;
159 if (domain & NOUVEAU_GEM_DOMAIN_GART)
160 flags |= TTM_PL_FLAG_TT;
161 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
162 flags |= TTM_PL_FLAG_SYSTEM;
163
Ben Skeggs7375c952011-06-07 14:21:29 +1000164 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
Dave Airlie22b33e82012-04-02 11:53:06 +0100165 tile_flags, NULL, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000166 if (ret)
167 return ret;
168 nvbo = *pnvbo;
169
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000170 /* we restrict allowed domains on nv50+ to only the types
171 * that were requested at creation time. not possibly on
172 * earlier chips without busting the ABI.
173 */
174 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
175 NOUVEAU_GEM_DOMAIN_GART;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000176 if (nv_device(drm->device)->card_type >= NV_50)
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000177 nvbo->valid_domains &= domain;
178
David Herrmann55fb74a2013-10-02 10:15:17 +0200179 /* Initialize the embedded gem-object. We return a single gem-reference
180 * to the caller, instead of a normal nouveau_bo ttm reference. */
181 ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size);
182 if (ret) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000183 nouveau_bo_ref(NULL, pnvbo);
184 return -ENOMEM;
185 }
186
David Herrmann55fb74a2013-10-02 10:15:17 +0200187 nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000188 return 0;
189}
190
191static int
Ben Skeggse758a312011-06-07 13:28:11 +1000192nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
193 struct drm_nouveau_gem_info *rep)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000194{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000195 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000196 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggse758a312011-06-07 13:28:11 +1000197 struct nouveau_vma *vma;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000198
199 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
200 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
201 else
202 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
203
Ben Skeggse758a312011-06-07 13:28:11 +1000204 rep->offset = nvbo->bo.offset;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000205 if (cli->base.vm) {
206 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggse758a312011-06-07 13:28:11 +1000207 if (!vma)
208 return -EINVAL;
209
210 rep->offset = vma->offset;
211 }
212
Ben Skeggs6ee73862009-12-11 19:24:15 +1000213 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
David Herrmann72525b32013-07-24 21:08:53 +0200214 rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000215 rep->tile_mode = nvbo->tile_mode;
216 rep->tile_flags = nvbo->tile_flags;
217 return 0;
218}
219
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220int
221nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
222 struct drm_file *file_priv)
223{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000224 struct nouveau_drm *drm = nouveau_drm(dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100225 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000226 struct nouveau_fb *pfb = nouveau_fb(drm->device);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000227 struct drm_nouveau_gem_new *req = data;
228 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000229 int ret = 0;
230
Ben Skeggsebb945a2012-07-20 08:17:34 +1000231 if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100232 NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000233 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000234 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000236 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000237 req->info.domain, req->info.tile_mode,
238 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000239 if (ret)
240 return ret;
241
David Herrmann55fb74a2013-10-02 10:15:17 +0200242 ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000243 if (ret == 0) {
David Herrmann55fb74a2013-10-02 10:15:17 +0200244 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
Ben Skeggse758a312011-06-07 13:28:11 +1000245 if (ret)
246 drm_gem_handle_delete(file_priv, req->info.handle);
247 }
248
Dave Airlie29d08b32010-09-27 16:17:17 +1000249 /* drop reference from allocate - handle holds it now */
David Herrmann55fb74a2013-10-02 10:15:17 +0200250 drm_gem_object_unreference_unlocked(&nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000251 return ret;
252}
253
254static int
255nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
256 uint32_t write_domains, uint32_t valid_domains)
257{
David Herrmann55fb74a2013-10-02 10:15:17 +0200258 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000259 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000260 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100261 (write_domains ? write_domains : read_domains);
262 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000263
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100264 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000265 return -EINVAL;
266
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100267 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
268 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000269
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100270 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
271 valid_flags |= TTM_PL_FLAG_TT;
272
273 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
274 bo->mem.mem_type == TTM_PL_VRAM)
275 pref_flags |= TTM_PL_FLAG_VRAM;
276
277 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
278 bo->mem.mem_type == TTM_PL_TT)
279 pref_flags |= TTM_PL_FLAG_TT;
280
281 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
282 pref_flags |= TTM_PL_FLAG_VRAM;
283
284 else
285 pref_flags |= TTM_PL_FLAG_TT;
286
287 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
288
Ben Skeggs6ee73862009-12-11 19:24:15 +1000289 return 0;
290}
291
292struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000293 struct list_head vram_list;
294 struct list_head gart_list;
295 struct list_head both_list;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200296 struct ww_acquire_ctx ticket;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000297};
298
299static void
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200300validate_fini_list(struct list_head *list, struct nouveau_fence *fence,
301 struct ww_acquire_ctx *ticket)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000302{
303 struct list_head *entry, *tmp;
304 struct nouveau_bo *nvbo;
305
306 list_for_each_safe(entry, tmp, list) {
307 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000308
Ben Skeggs9360bd12013-11-13 15:18:32 +1000309 if (likely(fence))
310 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000311
Ben Skeggsa1606a92010-02-12 10:27:35 +1000312 if (unlikely(nvbo->validate_mapped)) {
313 ttm_bo_kunmap(&nvbo->kmap);
314 nvbo->validate_mapped = false;
315 }
316
Ben Skeggs6ee73862009-12-11 19:24:15 +1000317 list_del(&nvbo->entry);
318 nvbo->reserved_by = NULL;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200319 ttm_bo_unreserve_ticket(&nvbo->bo, ticket);
David Herrmann55fb74a2013-10-02 10:15:17 +0200320 drm_gem_object_unreference_unlocked(&nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000321 }
322}
323
324static void
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200325validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000326{
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200327 validate_fini_list(&op->vram_list, fence, &op->ticket);
328 validate_fini_list(&op->gart_list, fence, &op->ticket);
329 validate_fini_list(&op->both_list, fence, &op->ticket);
330}
331
332static void
333validate_fini(struct validate_op *op, struct nouveau_fence *fence)
334{
335 validate_fini_no_ticket(op, fence);
336 ww_acquire_fini(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000337}
338
339static int
340validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
341 struct drm_nouveau_gem_pushbuf_bo *pbbo,
342 int nr_buffers, struct validate_op *op)
343{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100344 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000345 struct drm_device *dev = chan->drm->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000346 int trycnt = 0;
347 int ret, i;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100348 struct nouveau_bo *res_bo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000349
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200350 ww_acquire_init(&op->ticket, &reservation_ww_class);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000351retry:
352 if (++trycnt > 100000) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100353 NV_ERROR(cli, "%s failed and gave up.\n", __func__);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000354 return -EINVAL;
355 }
356
357 for (i = 0; i < nr_buffers; i++) {
358 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
359 struct drm_gem_object *gem;
360 struct nouveau_bo *nvbo;
361
362 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
363 if (!gem) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100364 NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200365 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000366 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100367 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000368 }
David Herrmann55fb74a2013-10-02 10:15:17 +0200369 nvbo = nouveau_gem_object(gem);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100370 if (nvbo == res_bo) {
371 res_bo = NULL;
372 drm_gem_object_unreference_unlocked(gem);
373 continue;
374 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000375
376 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100377 NV_ERROR(cli, "multiple instances of buffer %d on "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000378 "validation list\n", b->handle);
Ben Skeggs5086f692012-06-25 18:04:27 +1000379 drm_gem_object_unreference_unlocked(gem);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200380 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000381 validate_fini(op, NULL);
382 return -EINVAL;
383 }
384
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200385 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000386 if (ret) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200387 validate_fini_no_ticket(op, NULL);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200388 if (unlikely(ret == -EDEADLK)) {
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100389 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200390 &op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100391 if (!ret)
392 res_bo = nvbo;
393 }
Ben Skeggs938c40e2010-10-12 09:54:54 +1000394 if (unlikely(ret)) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200395 ww_acquire_done(&op->ticket);
396 ww_acquire_fini(&op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100397 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000398 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100399 NV_ERROR(cli, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000400 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000401 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000402 }
403
Ben Skeggsa1606a92010-02-12 10:27:35 +1000404 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000405 nvbo->reserved_by = file_priv;
406 nvbo->pbbo_index = i;
407 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
408 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
409 list_add_tail(&nvbo->entry, &op->both_list);
410 else
411 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
412 list_add_tail(&nvbo->entry, &op->vram_list);
413 else
414 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
415 list_add_tail(&nvbo->entry, &op->gart_list);
416 else {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100417 NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000418 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000419 list_add_tail(&nvbo->entry, &op->both_list);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200420 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000421 validate_fini(op, NULL);
422 return -EINVAL;
423 }
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100424 if (nvbo == res_bo)
425 goto retry;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000426 }
427
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200428 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000429 return 0;
430}
431
432static int
Ben Skeggs525895b2012-01-10 10:18:28 +1000433validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
434{
435 struct nouveau_fence *fence = NULL;
436 int ret = 0;
437
438 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggs5d216f62013-11-13 10:23:46 +1000439 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
Ben Skeggs525895b2012-01-10 10:18:28 +1000440 spin_unlock(&nvbo->bo.bdev->fence_lock);
441
442 if (fence) {
443 ret = nouveau_fence_sync(fence, chan);
444 nouveau_fence_unref(&fence);
445 }
446
447 return ret;
448}
449
450static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100451validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
452 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
453 uint64_t user_pbbo_ptr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000454{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000455 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000456 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
457 (void __force __user *)(uintptr_t)user_pbbo_ptr;
458 struct nouveau_bo *nvbo;
459 int ret, relocs = 0;
460
461 list_for_each_entry(nvbo, list, entry) {
462 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000463
David Herrmann55fb74a2013-10-02 10:15:17 +0200464 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000465 b->write_domains,
466 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000467 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100468 NV_ERROR(cli, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000469 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000470 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000471
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000472 ret = nouveau_bo_validate(nvbo, true, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000473 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000474 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100475 NV_ERROR(cli, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000476 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000477 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000478
Ben Skeggs525895b2012-01-10 10:18:28 +1000479 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000480 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100481 NV_ERROR(cli, "fail post-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000482 return ret;
483 }
484
Ben Skeggsebb945a2012-07-20 08:17:34 +1000485 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000486 if (nvbo->bo.offset == b->presumed.offset &&
487 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
488 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
489 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
490 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
491 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000492
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000493 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
494 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
495 else
496 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
497 b->presumed.offset = nvbo->bo.offset;
498 b->presumed.valid = 0;
499 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000500
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100501 if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000502 &b->presumed, sizeof(b->presumed)))
503 return -EFAULT;
504 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000505 }
506
507 return relocs;
508}
509
510static int
511nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
512 struct drm_file *file_priv,
513 struct drm_nouveau_gem_pushbuf_bo *pbbo,
514 uint64_t user_buffers, int nr_buffers,
515 struct validate_op *op, int *apply_relocs)
516{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100517 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000518 int ret, relocs = 0;
519
520 INIT_LIST_HEAD(&op->vram_list);
521 INIT_LIST_HEAD(&op->gart_list);
522 INIT_LIST_HEAD(&op->both_list);
523
Ben Skeggs6ee73862009-12-11 19:24:15 +1000524 if (nr_buffers == 0)
525 return 0;
526
527 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000528 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000529 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100530 NV_ERROR(cli, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000531 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000532 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000533
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100534 ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000535 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000536 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100537 NV_ERROR(cli, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000538 validate_fini(op, NULL);
539 return ret;
540 }
541 relocs += ret;
542
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100543 ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000544 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000545 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100546 NV_ERROR(cli, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000547 validate_fini(op, NULL);
548 return ret;
549 }
550 relocs += ret;
551
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100552 ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000553 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000554 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100555 NV_ERROR(cli, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000556 validate_fini(op, NULL);
557 return ret;
558 }
559 relocs += ret;
560
561 *apply_relocs = relocs;
562 return 0;
563}
564
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200565static inline void
566u_free(void *addr)
567{
568 if (!is_vmalloc_addr(addr))
569 kfree(addr);
570 else
571 vfree(addr);
572}
573
Ben Skeggs6ee73862009-12-11 19:24:15 +1000574static inline void *
575u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
576{
577 void *mem;
578 void __user *userptr = (void __force __user *)(uintptr_t)user;
579
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200580 size *= nmemb;
581
582 mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
583 if (!mem)
584 mem = vmalloc(size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000585 if (!mem)
586 return ERR_PTR(-ENOMEM);
587
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100588 if (copy_from_user(mem, userptr, size)) {
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200589 u_free(mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000590 return ERR_PTR(-EFAULT);
591 }
592
593 return mem;
594}
595
596static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100597nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
Ben Skeggsa1606a92010-02-12 10:27:35 +1000598 struct drm_nouveau_gem_pushbuf *req,
599 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000600{
601 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100602 int ret = 0;
603 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000604
Ben Skeggsa1606a92010-02-12 10:27:35 +1000605 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000606 if (IS_ERR(reloc))
607 return PTR_ERR(reloc);
608
Ben Skeggsa1606a92010-02-12 10:27:35 +1000609 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
611 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000612 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000613 uint32_t data;
614
Ben Skeggsa1606a92010-02-12 10:27:35 +1000615 if (unlikely(r->bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100616 NV_ERROR(cli, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000617 ret = -EINVAL;
618 break;
619 }
620
621 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000622 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000623 continue;
624
Ben Skeggsa1606a92010-02-12 10:27:35 +1000625 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100626 NV_ERROR(cli, "reloc container bo index invalid\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000627 ret = -EINVAL;
628 break;
629 }
630 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
631
632 if (unlikely(r->reloc_bo_offset + 4 >
633 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100634 NV_ERROR(cli, "reloc outside of bo\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000635 ret = -EINVAL;
636 break;
637 }
638
639 if (!nvbo->kmap.virtual) {
640 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
641 &nvbo->kmap);
642 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100643 NV_ERROR(cli, "failed kmap for reloc\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000644 break;
645 }
646 nvbo->validate_mapped = true;
647 }
648
Ben Skeggs6ee73862009-12-11 19:24:15 +1000649 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000650 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000651 else
652 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000653 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000654 else
655 data = r->data;
656
657 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000658 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000659 data |= r->tor;
660 else
661 data |= r->vor;
662 }
663
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000664 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000665 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000666 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000667 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100668 NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000669 break;
670 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000671
672 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000673 }
674
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200675 u_free(reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000676 return ret;
677}
678
679int
680nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
681 struct drm_file *file_priv)
682{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000683 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100684 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000685 struct nouveau_abi16_chan *temp;
686 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000687 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000688 struct drm_nouveau_gem_pushbuf_push *push;
689 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000690 struct nouveau_channel *chan = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000691 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200692 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000693 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000694
Ben Skeggsebb945a2012-07-20 08:17:34 +1000695 if (unlikely(!abi16))
696 return -ENOMEM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000697
Ben Skeggsebb945a2012-07-20 08:17:34 +1000698 list_for_each_entry(temp, &abi16->channels, head) {
699 if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
700 chan = temp->chan;
701 break;
702 }
703 }
704
705 if (!chan)
706 return nouveau_abi16_put(abi16, -ENOENT);
707
708 req->vram_available = drm->gem.vram_available;
709 req->gart_available = drm->gem.gart_available;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000710 if (unlikely(req->nr_push == 0))
711 goto out_next;
712
713 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100714 NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000715 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000716 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000717 }
718
Ben Skeggsa1606a92010-02-12 10:27:35 +1000719 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100720 NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000721 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000722 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000723 }
724
725 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100726 NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000727 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000728 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000729 }
730
731 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggsebb945a2012-07-20 08:17:34 +1000732 if (IS_ERR(push))
733 return nouveau_abi16_put(abi16, PTR_ERR(push));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000734
735 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
736 if (IS_ERR(bo)) {
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200737 u_free(push);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000738 return nouveau_abi16_put(abi16, PTR_ERR(bo));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000739 }
740
Ben Skeggsaccf9492012-03-16 12:40:17 +1000741 /* Ensure all push buffers are on validate list */
Ben Skeggs415e6182010-07-23 09:06:52 +1000742 for (i = 0; i < req->nr_push; i++) {
743 if (push[i].bo_index >= req->nr_buffers) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100744 NV_ERROR(cli, "push %d buffer not in list\n", i);
Ben Skeggs415e6182010-07-23 09:06:52 +1000745 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100746 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000747 }
Ben Skeggs415e6182010-07-23 09:06:52 +1000748 }
749
Ben Skeggs6ee73862009-12-11 19:24:15 +1000750 /* Validate buffer list */
751 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
752 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000753 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000754 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100755 NV_ERROR(cli, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100756 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000757 }
758
Ben Skeggs6ee73862009-12-11 19:24:15 +1000759 /* Apply any relocations that are required */
760 if (do_reloc) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100761 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000762 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100763 NV_ERROR(cli, "reloc apply: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764 goto out;
765 }
766 }
767
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000768 if (chan->dma.ib_max) {
Ben Skeggs5e120f62012-04-30 13:55:29 +1000769 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000770 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100771 NV_ERROR(cli, "nv50cal_space: %d\n", ret);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000772 goto out;
773 }
774
Ben Skeggsa1606a92010-02-12 10:27:35 +1000775 for (i = 0; i < req->nr_push; i++) {
776 struct nouveau_bo *nvbo = (void *)(unsigned long)
777 bo[push[i].bo_index].user_priv;
778
779 nv50_dma_push(chan, nvbo, push[i].offset,
780 push[i].length);
781 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000782 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000783 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000784 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000785 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100786 NV_ERROR(cli, "cal_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000787 goto out;
788 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000789
790 for (i = 0; i < req->nr_push; i++) {
791 struct nouveau_bo *nvbo = (void *)(unsigned long)
792 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000793
Ben Skeggs3a92d372012-08-04 05:46:01 +1000794 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000795 OUT_RING(chan, 0);
796 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000797 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000798 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000799 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100800 NV_ERROR(cli, "jmp_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000801 goto out;
802 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000803
Ben Skeggsa1606a92010-02-12 10:27:35 +1000804 for (i = 0; i < req->nr_push; i++) {
805 struct nouveau_bo *nvbo = (void *)(unsigned long)
806 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000807 uint32_t cmd;
808
Ben Skeggsebb945a2012-07-20 08:17:34 +1000809 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000810 cmd |= 0x20000000;
811 if (unlikely(cmd != req->suffix0)) {
812 if (!nvbo->kmap.virtual) {
813 ret = ttm_bo_kmap(&nvbo->bo, 0,
814 nvbo->bo.mem.
815 num_pages,
816 &nvbo->kmap);
817 if (ret) {
818 WIND_RING(chan);
819 goto out;
820 }
821 nvbo->validate_mapped = true;
822 }
823
824 nouveau_bo_wr32(nvbo, (push[i].offset +
825 push[i].length - 8) / 4, cmd);
826 }
827
Ben Skeggs3a92d372012-08-04 05:46:01 +1000828 OUT_RING(chan, 0x20000000 |
829 (nvbo->bo.offset + push[i].offset));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000830 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000831 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
832 OUT_RING(chan, 0);
833 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000834 }
835
Ben Skeggs264ce192013-02-14 13:43:21 +1000836 ret = nouveau_fence_new(chan, false, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000837 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100838 NV_ERROR(cli, "error fencing pushbuf: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000839 WIND_RING(chan);
840 goto out;
841 }
842
843out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100844 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200845 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100846
847out_prevalid:
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200848 u_free(bo);
849 u_free(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000850
851out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000852 if (chan->dma.ib_max) {
853 req->suffix0 = 0x00000000;
854 req->suffix1 = 0x00000000;
855 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000856 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000857 req->suffix0 = 0x00020000;
858 req->suffix1 = 0x00000000;
859 } else {
860 req->suffix0 = 0x20000000 |
Ben Skeggsebb945a2012-07-20 08:17:34 +1000861 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000862 req->suffix1 = 0x00000000;
863 }
864
Ben Skeggsebb945a2012-07-20 08:17:34 +1000865 return nouveau_abi16_put(abi16, ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000866}
867
Ben Skeggs6ee73862009-12-11 19:24:15 +1000868static inline uint32_t
869domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
870{
871 uint32_t flags = 0;
872
873 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
874 flags |= TTM_PL_FLAG_VRAM;
875 if (domain & NOUVEAU_GEM_DOMAIN_GART)
876 flags |= TTM_PL_FLAG_TT;
877
878 return flags;
879}
880
881int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000882nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
883 struct drm_file *file_priv)
884{
885 struct drm_nouveau_gem_cpu_prep *req = data;
886 struct drm_gem_object *gem;
887 struct nouveau_bo *nvbo;
888 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
889 int ret = -EINVAL;
890
Ben Skeggs6ee73862009-12-11 19:24:15 +1000891 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
892 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100893 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000894 nvbo = nouveau_gem_object(gem);
895
Ben Skeggs21e86c12010-10-11 11:48:45 +1000896 spin_lock(&nvbo->bo.bdev->fence_lock);
897 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
898 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000899 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000900 return ret;
901}
902
903int
904nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
905 struct drm_file *file_priv)
906{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000907 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000908}
909
910int
911nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
912 struct drm_file *file_priv)
913{
914 struct drm_nouveau_gem_info *req = data;
915 struct drm_gem_object *gem;
916 int ret;
917
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
919 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100920 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000921
Ben Skeggse758a312011-06-07 13:28:11 +1000922 ret = nouveau_gem_info(file_priv, gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000923 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000924 return ret;
925}
926