blob: 2b2077d1e2f71d5ef7560a2f3b80e6bcbb2d5a7c [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
105void
106nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
107{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000108 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000109 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
110 struct nouveau_vma *vma;
111 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000112
Ben Skeggsebb945a2012-07-20 08:17:34 +1000113 if (!cli->base.vm)
Ben Skeggs639212d2011-06-03 16:18:26 +1000114 return;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000115
116 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
117 if (ret)
118 return;
119
Ben Skeggsebb945a2012-07-20 08:17:34 +1000120 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000121 if (vma) {
Marcin Slusarz8fe198b2011-06-23 16:34:30 +0200122 if (--vma->refcount == 0) {
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000123 nouveau_bo_vma_del(nvbo, vma);
Marcin Slusarz8fe198b2011-06-23 16:34:30 +0200124 kfree(vma);
125 }
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000126 }
127 ttm_bo_unreserve(&nvbo->bo);
Ben Skeggs639212d2011-06-03 16:18:26 +1000128}
129
130int
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000131nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
132 uint32_t tile_mode, uint32_t tile_flags,
133 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000134{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000135 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000136 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000137 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000138 int ret;
139
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000140 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
141 flags |= TTM_PL_FLAG_VRAM;
142 if (domain & NOUVEAU_GEM_DOMAIN_GART)
143 flags |= TTM_PL_FLAG_TT;
144 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
145 flags |= TTM_PL_FLAG_SYSTEM;
146
Ben Skeggs7375c952011-06-07 14:21:29 +1000147 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
Dave Airlie22b33e82012-04-02 11:53:06 +0100148 tile_flags, NULL, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000149 if (ret)
150 return ret;
151 nvbo = *pnvbo;
152
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000153 /* we restrict allowed domains on nv50+ to only the types
154 * that were requested at creation time. not possibly on
155 * earlier chips without busting the ABI.
156 */
157 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
158 NOUVEAU_GEM_DOMAIN_GART;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000159 if (nv_device(drm->device)->card_type >= NV_50)
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000160 nvbo->valid_domains &= domain;
161
Ben Skeggs6ee73862009-12-11 19:24:15 +1000162 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
163 if (!nvbo->gem) {
164 nouveau_bo_ref(NULL, pnvbo);
165 return -ENOMEM;
166 }
167
Jan Engelhardt5df23972011-04-04 01:25:18 +0200168 nvbo->bo.persistent_swap_storage = nvbo->gem->filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000169 nvbo->gem->driver_private = nvbo;
170 return 0;
171}
172
173static int
Ben Skeggse758a312011-06-07 13:28:11 +1000174nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
175 struct drm_nouveau_gem_info *rep)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000176{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000177 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000178 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggse758a312011-06-07 13:28:11 +1000179 struct nouveau_vma *vma;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000180
181 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
182 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
183 else
184 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
185
Ben Skeggse758a312011-06-07 13:28:11 +1000186 rep->offset = nvbo->bo.offset;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000187 if (cli->base.vm) {
188 vma = nouveau_bo_vma_find(nvbo, cli->base.vm);
Ben Skeggse758a312011-06-07 13:28:11 +1000189 if (!vma)
190 return -EINVAL;
191
192 rep->offset = vma->offset;
193 }
194
Ben Skeggs6ee73862009-12-11 19:24:15 +1000195 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
Ben Skeggsd550c412011-02-16 08:41:56 +1000196 rep->map_handle = nvbo->bo.addr_space_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000197 rep->tile_mode = nvbo->tile_mode;
198 rep->tile_flags = nvbo->tile_flags;
199 return 0;
200}
201
Ben Skeggs6ee73862009-12-11 19:24:15 +1000202int
203nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
204 struct drm_file *file_priv)
205{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000206 struct nouveau_drm *drm = nouveau_drm(dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100207 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000208 struct nouveau_fb *pfb = nouveau_fb(drm->device);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209 struct drm_nouveau_gem_new *req = data;
210 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000211 int ret = 0;
212
Ben Skeggsebb945a2012-07-20 08:17:34 +1000213 drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214
Ben Skeggsebb945a2012-07-20 08:17:34 +1000215 if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100216 NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000217 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000218 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000219
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000220 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000221 req->info.domain, req->info.tile_mode,
222 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223 if (ret)
224 return ret;
225
Ben Skeggs6ee73862009-12-11 19:24:15 +1000226 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000227 if (ret == 0) {
228 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info);
229 if (ret)
230 drm_gem_handle_delete(file_priv, req->info.handle);
231 }
232
Dave Airlie29d08b32010-09-27 16:17:17 +1000233 /* drop reference from allocate - handle holds it now */
234 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235 return ret;
236}
237
238static int
239nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
240 uint32_t write_domains, uint32_t valid_domains)
241{
242 struct nouveau_bo *nvbo = gem->driver_private;
243 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000244 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100245 (write_domains ? write_domains : read_domains);
246 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000247
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100248 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000249 return -EINVAL;
250
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100251 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
252 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000253
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100254 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
255 valid_flags |= TTM_PL_FLAG_TT;
256
257 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
258 bo->mem.mem_type == TTM_PL_VRAM)
259 pref_flags |= TTM_PL_FLAG_VRAM;
260
261 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
262 bo->mem.mem_type == TTM_PL_TT)
263 pref_flags |= TTM_PL_FLAG_TT;
264
265 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
266 pref_flags |= TTM_PL_FLAG_VRAM;
267
268 else
269 pref_flags |= TTM_PL_FLAG_TT;
270
271 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
272
Ben Skeggs6ee73862009-12-11 19:24:15 +1000273 return 0;
274}
275
276struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000277 struct list_head vram_list;
278 struct list_head gart_list;
279 struct list_head both_list;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200280 struct ww_acquire_ctx ticket;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000281};
282
283static void
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200284validate_fini_list(struct list_head *list, struct nouveau_fence *fence,
285 struct ww_acquire_ctx *ticket)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000286{
287 struct list_head *entry, *tmp;
288 struct nouveau_bo *nvbo;
289
290 list_for_each_safe(entry, tmp, list) {
291 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000292
Francisco Jerez332b2422010-10-20 23:35:40 +0200293 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000294
Ben Skeggsa1606a92010-02-12 10:27:35 +1000295 if (unlikely(nvbo->validate_mapped)) {
296 ttm_bo_kunmap(&nvbo->kmap);
297 nvbo->validate_mapped = false;
298 }
299
Ben Skeggs6ee73862009-12-11 19:24:15 +1000300 list_del(&nvbo->entry);
301 nvbo->reserved_by = NULL;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200302 ttm_bo_unreserve_ticket(&nvbo->bo, ticket);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200303 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000304 }
305}
306
307static void
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200308validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000309{
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200310 validate_fini_list(&op->vram_list, fence, &op->ticket);
311 validate_fini_list(&op->gart_list, fence, &op->ticket);
312 validate_fini_list(&op->both_list, fence, &op->ticket);
313}
314
315static void
316validate_fini(struct validate_op *op, struct nouveau_fence *fence)
317{
318 validate_fini_no_ticket(op, fence);
319 ww_acquire_fini(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000320}
321
322static int
323validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
324 struct drm_nouveau_gem_pushbuf_bo *pbbo,
325 int nr_buffers, struct validate_op *op)
326{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100327 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000328 struct drm_device *dev = chan->drm->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000329 int trycnt = 0;
330 int ret, i;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100331 struct nouveau_bo *res_bo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000332
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200333 ww_acquire_init(&op->ticket, &reservation_ww_class);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000334retry:
335 if (++trycnt > 100000) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100336 NV_ERROR(cli, "%s failed and gave up.\n", __func__);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000337 return -EINVAL;
338 }
339
340 for (i = 0; i < nr_buffers; i++) {
341 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
342 struct drm_gem_object *gem;
343 struct nouveau_bo *nvbo;
344
345 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
346 if (!gem) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100347 NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200348 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000349 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100350 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000351 }
352 nvbo = gem->driver_private;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100353 if (nvbo == res_bo) {
354 res_bo = NULL;
355 drm_gem_object_unreference_unlocked(gem);
356 continue;
357 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000358
359 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100360 NV_ERROR(cli, "multiple instances of buffer %d on "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000361 "validation list\n", b->handle);
Ben Skeggs5086f692012-06-25 18:04:27 +1000362 drm_gem_object_unreference_unlocked(gem);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200363 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000364 validate_fini(op, NULL);
365 return -EINVAL;
366 }
367
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200368 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000369 if (ret) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200370 validate_fini_no_ticket(op, NULL);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200371 if (unlikely(ret == -EDEADLK)) {
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100372 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200373 &op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100374 if (!ret)
375 res_bo = nvbo;
376 }
Ben Skeggs938c40e2010-10-12 09:54:54 +1000377 if (unlikely(ret)) {
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200378 ww_acquire_done(&op->ticket);
379 ww_acquire_fini(&op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100380 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000381 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100382 NV_ERROR(cli, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000383 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000384 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000385 }
386
Ben Skeggsa1606a92010-02-12 10:27:35 +1000387 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000388 nvbo->reserved_by = file_priv;
389 nvbo->pbbo_index = i;
390 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
391 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
392 list_add_tail(&nvbo->entry, &op->both_list);
393 else
394 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
395 list_add_tail(&nvbo->entry, &op->vram_list);
396 else
397 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
398 list_add_tail(&nvbo->entry, &op->gart_list);
399 else {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100400 NV_ERROR(cli, "invalid valid domains: 0x%08x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000401 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000402 list_add_tail(&nvbo->entry, &op->both_list);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200403 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000404 validate_fini(op, NULL);
405 return -EINVAL;
406 }
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100407 if (nvbo == res_bo)
408 goto retry;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000409 }
410
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200411 ww_acquire_done(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000412 return 0;
413}
414
415static int
Ben Skeggs525895b2012-01-10 10:18:28 +1000416validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo)
417{
418 struct nouveau_fence *fence = NULL;
419 int ret = 0;
420
421 spin_lock(&nvbo->bo.bdev->fence_lock);
422 if (nvbo->bo.sync_obj)
423 fence = nouveau_fence_ref(nvbo->bo.sync_obj);
424 spin_unlock(&nvbo->bo.bdev->fence_lock);
425
426 if (fence) {
427 ret = nouveau_fence_sync(fence, chan);
428 nouveau_fence_unref(&fence);
429 }
430
431 return ret;
432}
433
434static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100435validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
436 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
437 uint64_t user_pbbo_ptr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000438{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000439 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000440 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
441 (void __force __user *)(uintptr_t)user_pbbo_ptr;
442 struct nouveau_bo *nvbo;
443 int ret, relocs = 0;
444
445 list_for_each_entry(nvbo, list, entry) {
446 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000447
Ben Skeggs525895b2012-01-10 10:18:28 +1000448 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000449 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100450 NV_ERROR(cli, "fail pre-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000451 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000452 }
453
454 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
455 b->write_domains,
456 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000457 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100458 NV_ERROR(cli, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000459 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000460 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000461
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000462 ret = nouveau_bo_validate(nvbo, true, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000463 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000464 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100465 NV_ERROR(cli, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000466 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000467 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000468
Ben Skeggs525895b2012-01-10 10:18:28 +1000469 ret = validate_sync(chan, nvbo);
Ben Skeggs415e6182010-07-23 09:06:52 +1000470 if (unlikely(ret)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100471 NV_ERROR(cli, "fail post-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000472 return ret;
473 }
474
Ben Skeggsebb945a2012-07-20 08:17:34 +1000475 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000476 if (nvbo->bo.offset == b->presumed.offset &&
477 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
478 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
479 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
480 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
481 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000482
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000483 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
484 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
485 else
486 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
487 b->presumed.offset = nvbo->bo.offset;
488 b->presumed.valid = 0;
489 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000490
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000491 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
492 &b->presumed, sizeof(b->presumed)))
493 return -EFAULT;
494 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000495 }
496
497 return relocs;
498}
499
500static int
501nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
502 struct drm_file *file_priv,
503 struct drm_nouveau_gem_pushbuf_bo *pbbo,
504 uint64_t user_buffers, int nr_buffers,
505 struct validate_op *op, int *apply_relocs)
506{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100507 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000508 int ret, relocs = 0;
509
510 INIT_LIST_HEAD(&op->vram_list);
511 INIT_LIST_HEAD(&op->gart_list);
512 INIT_LIST_HEAD(&op->both_list);
513
Ben Skeggs6ee73862009-12-11 19:24:15 +1000514 if (nr_buffers == 0)
515 return 0;
516
517 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000518 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000519 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100520 NV_ERROR(cli, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000521 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000522 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000523
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100524 ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000525 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000526 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100527 NV_ERROR(cli, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000528 validate_fini(op, NULL);
529 return ret;
530 }
531 relocs += ret;
532
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100533 ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000534 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000535 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100536 NV_ERROR(cli, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000537 validate_fini(op, NULL);
538 return ret;
539 }
540 relocs += ret;
541
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100542 ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000543 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000544 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100545 NV_ERROR(cli, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000546 validate_fini(op, NULL);
547 return ret;
548 }
549 relocs += ret;
550
551 *apply_relocs = relocs;
552 return 0;
553}
554
555static inline void *
556u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
557{
558 void *mem;
559 void __user *userptr = (void __force __user *)(uintptr_t)user;
560
561 mem = kmalloc(nmemb * size, GFP_KERNEL);
562 if (!mem)
563 return ERR_PTR(-ENOMEM);
564
565 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
566 kfree(mem);
567 return ERR_PTR(-EFAULT);
568 }
569
570 return mem;
571}
572
573static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100574nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
Ben Skeggsa1606a92010-02-12 10:27:35 +1000575 struct drm_nouveau_gem_pushbuf *req,
576 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000577{
578 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100579 int ret = 0;
580 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000581
Ben Skeggsa1606a92010-02-12 10:27:35 +1000582 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000583 if (IS_ERR(reloc))
584 return PTR_ERR(reloc);
585
Ben Skeggsa1606a92010-02-12 10:27:35 +1000586 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000587 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
588 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000589 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000590 uint32_t data;
591
Ben Skeggsa1606a92010-02-12 10:27:35 +1000592 if (unlikely(r->bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100593 NV_ERROR(cli, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000594 ret = -EINVAL;
595 break;
596 }
597
598 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000599 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000600 continue;
601
Ben Skeggsa1606a92010-02-12 10:27:35 +1000602 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100603 NV_ERROR(cli, "reloc container bo index invalid\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000604 ret = -EINVAL;
605 break;
606 }
607 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
608
609 if (unlikely(r->reloc_bo_offset + 4 >
610 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100611 NV_ERROR(cli, "reloc outside of bo\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000612 ret = -EINVAL;
613 break;
614 }
615
616 if (!nvbo->kmap.virtual) {
617 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
618 &nvbo->kmap);
619 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100620 NV_ERROR(cli, "failed kmap for reloc\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000621 break;
622 }
623 nvbo->validate_mapped = true;
624 }
625
Ben Skeggs6ee73862009-12-11 19:24:15 +1000626 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000627 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000628 else
629 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000630 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000631 else
632 data = r->data;
633
634 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000635 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000636 data |= r->tor;
637 else
638 data |= r->vor;
639 }
640
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000641 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000642 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000643 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000644 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100645 NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000646 break;
647 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000648
649 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000650 }
651
652 kfree(reloc);
653 return ret;
654}
655
656int
657nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
658 struct drm_file *file_priv)
659{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000660 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100661 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000662 struct nouveau_abi16_chan *temp;
663 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000664 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000665 struct drm_nouveau_gem_pushbuf_push *push;
666 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000667 struct nouveau_channel *chan = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000668 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200669 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000670 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000671
Ben Skeggsebb945a2012-07-20 08:17:34 +1000672 if (unlikely(!abi16))
673 return -ENOMEM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000674
Ben Skeggsebb945a2012-07-20 08:17:34 +1000675 list_for_each_entry(temp, &abi16->channels, head) {
676 if (temp->chan->handle == (NVDRM_CHAN | req->channel)) {
677 chan = temp->chan;
678 break;
679 }
680 }
681
682 if (!chan)
683 return nouveau_abi16_put(abi16, -ENOENT);
684
685 req->vram_available = drm->gem.vram_available;
686 req->gart_available = drm->gem.gart_available;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000687 if (unlikely(req->nr_push == 0))
688 goto out_next;
689
690 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100691 NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000692 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000693 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000694 }
695
Ben Skeggsa1606a92010-02-12 10:27:35 +1000696 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100697 NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000698 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000699 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000700 }
701
702 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100703 NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000704 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000705 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000706 }
707
708 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggsebb945a2012-07-20 08:17:34 +1000709 if (IS_ERR(push))
710 return nouveau_abi16_put(abi16, PTR_ERR(push));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000711
712 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
713 if (IS_ERR(bo)) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000714 kfree(push);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000715 return nouveau_abi16_put(abi16, PTR_ERR(bo));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000716 }
717
Ben Skeggsaccf9492012-03-16 12:40:17 +1000718 /* Ensure all push buffers are on validate list */
Ben Skeggs415e6182010-07-23 09:06:52 +1000719 for (i = 0; i < req->nr_push; i++) {
720 if (push[i].bo_index >= req->nr_buffers) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100721 NV_ERROR(cli, "push %d buffer not in list\n", i);
Ben Skeggs415e6182010-07-23 09:06:52 +1000722 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100723 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000724 }
Ben Skeggs415e6182010-07-23 09:06:52 +1000725 }
726
Ben Skeggs6ee73862009-12-11 19:24:15 +1000727 /* Validate buffer list */
728 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
729 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000730 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000731 if (ret != -ERESTARTSYS)
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100732 NV_ERROR(cli, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100733 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000734 }
735
Ben Skeggs6ee73862009-12-11 19:24:15 +1000736 /* Apply any relocations that are required */
737 if (do_reloc) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100738 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000739 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100740 NV_ERROR(cli, "reloc apply: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000741 goto out;
742 }
743 }
744
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000745 if (chan->dma.ib_max) {
Ben Skeggs5e120f62012-04-30 13:55:29 +1000746 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000747 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100748 NV_ERROR(cli, "nv50cal_space: %d\n", ret);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000749 goto out;
750 }
751
Ben Skeggsa1606a92010-02-12 10:27:35 +1000752 for (i = 0; i < req->nr_push; i++) {
753 struct nouveau_bo *nvbo = (void *)(unsigned long)
754 bo[push[i].bo_index].user_priv;
755
756 nv50_dma_push(chan, nvbo, push[i].offset,
757 push[i].length);
758 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000759 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000760 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000761 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000762 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100763 NV_ERROR(cli, "cal_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764 goto out;
765 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000766
767 for (i = 0; i < req->nr_push; i++) {
768 struct nouveau_bo *nvbo = (void *)(unsigned long)
769 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000770
Ben Skeggs3a92d372012-08-04 05:46:01 +1000771 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000772 OUT_RING(chan, 0);
773 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000774 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000775 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100777 NV_ERROR(cli, "jmp_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000778 goto out;
779 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000780
Ben Skeggsa1606a92010-02-12 10:27:35 +1000781 for (i = 0; i < req->nr_push; i++) {
782 struct nouveau_bo *nvbo = (void *)(unsigned long)
783 bo[push[i].bo_index].user_priv;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000784 uint32_t cmd;
785
Ben Skeggsebb945a2012-07-20 08:17:34 +1000786 cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000787 cmd |= 0x20000000;
788 if (unlikely(cmd != req->suffix0)) {
789 if (!nvbo->kmap.virtual) {
790 ret = ttm_bo_kmap(&nvbo->bo, 0,
791 nvbo->bo.mem.
792 num_pages,
793 &nvbo->kmap);
794 if (ret) {
795 WIND_RING(chan);
796 goto out;
797 }
798 nvbo->validate_mapped = true;
799 }
800
801 nouveau_bo_wr32(nvbo, (push[i].offset +
802 push[i].length - 8) / 4, cmd);
803 }
804
Ben Skeggs3a92d372012-08-04 05:46:01 +1000805 OUT_RING(chan, 0x20000000 |
806 (nvbo->bo.offset + push[i].offset));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000807 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000808 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
809 OUT_RING(chan, 0);
810 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811 }
812
Ben Skeggs264ce192013-02-14 13:43:21 +1000813 ret = nouveau_fence_new(chan, false, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000814 if (ret) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100815 NV_ERROR(cli, "error fencing pushbuf: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000816 WIND_RING(chan);
817 goto out;
818 }
819
820out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100821 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200822 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100823
824out_prevalid:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000825 kfree(bo);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000826 kfree(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000827
828out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000829 if (chan->dma.ib_max) {
830 req->suffix0 = 0x00000000;
831 req->suffix1 = 0x00000000;
832 } else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000833 if (nv_device(drm->device)->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000834 req->suffix0 = 0x00020000;
835 req->suffix1 = 0x00000000;
836 } else {
837 req->suffix0 = 0x20000000 |
Ben Skeggsebb945a2012-07-20 08:17:34 +1000838 (chan->push.vma.offset + ((chan->dma.cur + 2) << 2));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000839 req->suffix1 = 0x00000000;
840 }
841
Ben Skeggsebb945a2012-07-20 08:17:34 +1000842 return nouveau_abi16_put(abi16, ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000843}
844
Ben Skeggs6ee73862009-12-11 19:24:15 +1000845static inline uint32_t
846domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
847{
848 uint32_t flags = 0;
849
850 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
851 flags |= TTM_PL_FLAG_VRAM;
852 if (domain & NOUVEAU_GEM_DOMAIN_GART)
853 flags |= TTM_PL_FLAG_TT;
854
855 return flags;
856}
857
858int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000859nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
860 struct drm_file *file_priv)
861{
862 struct drm_nouveau_gem_cpu_prep *req = data;
863 struct drm_gem_object *gem;
864 struct nouveau_bo *nvbo;
865 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
866 int ret = -EINVAL;
867
Ben Skeggs6ee73862009-12-11 19:24:15 +1000868 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
869 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100870 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000871 nvbo = nouveau_gem_object(gem);
872
Ben Skeggs21e86c12010-10-11 11:48:45 +1000873 spin_lock(&nvbo->bo.bdev->fence_lock);
874 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
875 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000876 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000877 return ret;
878}
879
880int
881nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
882 struct drm_file *file_priv)
883{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000884 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000885}
886
887int
888nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
889 struct drm_file *file_priv)
890{
891 struct drm_nouveau_gem_info *req = data;
892 struct drm_gem_object *gem;
893 int ret;
894
Ben Skeggs6ee73862009-12-11 19:24:15 +1000895 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
896 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100897 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000898
Ben Skeggse758a312011-06-07 13:28:11 +1000899 ret = nouveau_gem_info(file_priv, gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000900 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000901 return ret;
902}
903