blob: 022393777805fd15672e67f38a2ab92b1da8b78f [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 */
26#include "drmP.h"
27#include "drm.h"
28
29#include "nouveau_drv.h"
30#include "nouveau_drm.h"
31#include "nouveau_dma.h"
32
33#define nouveau_gem_pushbuf_sync(chan) 0
34
35int
36nouveau_gem_object_new(struct drm_gem_object *gem)
37{
38 return 0;
39}
40
41void
42nouveau_gem_object_del(struct drm_gem_object *gem)
43{
44 struct nouveau_bo *nvbo = gem->driver_private;
45 struct ttm_buffer_object *bo = &nvbo->bo;
46
47 if (!nvbo)
48 return;
49 nvbo->gem = NULL;
50
Ben Skeggs6ee73862009-12-11 19:24:15 +100051 if (unlikely(nvbo->pin_refcnt)) {
52 nvbo->pin_refcnt = 1;
53 nouveau_bo_unpin(nvbo);
54 }
55
56 ttm_bo_unref(&bo);
Daniel Vetterfd632aa2010-04-09 19:05:05 +000057
58 drm_gem_object_release(gem);
59 kfree(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +100060}
61
62int
Ben Skeggs639212d2011-06-03 16:18:26 +100063nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
64{
65 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100066 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
67 struct nouveau_vma *vma;
68 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100069
70 if (!fpriv->vm)
71 return 0;
72
Ben Skeggs2fd3db62011-06-07 15:25:12 +100073 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
74 if (ret)
75 return ret;
76
77 vma = nouveau_bo_vma_find(nvbo, fpriv->vm);
78 if (!vma) {
79 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
80 if (!vma) {
81 ret = -ENOMEM;
82 goto out;
83 }
84
85 ret = nouveau_bo_vma_add(nvbo, fpriv->vm, vma);
86 if (ret) {
87 kfree(vma);
88 goto out;
89 }
90 } else {
91 vma->refcount++;
92 }
93
94out:
95 ttm_bo_unreserve(&nvbo->bo);
96 return ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100097}
98
99void
100nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
101{
102 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000103 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
104 struct nouveau_vma *vma;
105 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000106
107 if (!fpriv->vm)
108 return;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000109
110 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
111 if (ret)
112 return;
113
114 vma = nouveau_bo_vma_find(nvbo, fpriv->vm);
115 if (vma) {
116 if (--vma->refcount == 0)
117 nouveau_bo_vma_del(nvbo, vma);
118 }
119 ttm_bo_unreserve(&nvbo->bo);
Ben Skeggs639212d2011-06-03 16:18:26 +1000120}
121
122int
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000123nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
124 uint32_t tile_mode, uint32_t tile_flags,
125 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000126{
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000127 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000128 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000129 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000130 int ret;
131
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000132 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
133 flags |= TTM_PL_FLAG_VRAM;
134 if (domain & NOUVEAU_GEM_DOMAIN_GART)
135 flags |= TTM_PL_FLAG_TT;
136 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
137 flags |= TTM_PL_FLAG_SYSTEM;
138
Ben Skeggs7375c952011-06-07 14:21:29 +1000139 ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
Ben Skeggsd550c412011-02-16 08:41:56 +1000140 tile_flags, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000141 if (ret)
142 return ret;
143 nvbo = *pnvbo;
144
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000145 /* we restrict allowed domains on nv50+ to only the types
146 * that were requested at creation time. not possibly on
147 * earlier chips without busting the ABI.
148 */
149 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
150 NOUVEAU_GEM_DOMAIN_GART;
151 if (dev_priv->card_type >= NV_50)
152 nvbo->valid_domains &= domain;
153
Ben Skeggs6ee73862009-12-11 19:24:15 +1000154 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
155 if (!nvbo->gem) {
156 nouveau_bo_ref(NULL, pnvbo);
157 return -ENOMEM;
158 }
159
Jan Engelhardt5df23972011-04-04 01:25:18 +0200160 nvbo->bo.persistent_swap_storage = nvbo->gem->filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000161 nvbo->gem->driver_private = nvbo;
162 return 0;
163}
164
165static int
Ben Skeggse758a312011-06-07 13:28:11 +1000166nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
167 struct drm_nouveau_gem_info *rep)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000168{
Ben Skeggse758a312011-06-07 13:28:11 +1000169 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000170 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggse758a312011-06-07 13:28:11 +1000171 struct nouveau_vma *vma;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000172
173 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
174 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
175 else
176 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
177
Ben Skeggse758a312011-06-07 13:28:11 +1000178 rep->offset = nvbo->bo.offset;
179 if (fpriv->vm) {
180 vma = nouveau_bo_vma_find(nvbo, fpriv->vm);
181 if (!vma)
182 return -EINVAL;
183
184 rep->offset = vma->offset;
185 }
186
Ben Skeggs6ee73862009-12-11 19:24:15 +1000187 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
Ben Skeggsd550c412011-02-16 08:41:56 +1000188 rep->map_handle = nvbo->bo.addr_space_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000189 rep->tile_mode = nvbo->tile_mode;
190 rep->tile_flags = nvbo->tile_flags;
191 return 0;
192}
193
Ben Skeggs6ee73862009-12-11 19:24:15 +1000194int
195nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
196 struct drm_file *file_priv)
197{
198 struct drm_nouveau_private *dev_priv = dev->dev_private;
199 struct drm_nouveau_gem_new *req = data;
200 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000201 int ret = 0;
202
Ben Skeggs6ee73862009-12-11 19:24:15 +1000203 if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL))
204 dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping;
205
Ben Skeggs60d2a882010-12-06 15:28:54 +1000206 if (!dev_priv->engine.vram.flags_valid(dev, req->info.tile_flags)) {
207 NV_ERROR(dev, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000208 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000209 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000210
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000211 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000212 req->info.domain, req->info.tile_mode,
213 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214 if (ret)
215 return ret;
216
Ben Skeggs6ee73862009-12-11 19:24:15 +1000217 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000218 if (ret == 0) {
219 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info);
220 if (ret)
221 drm_gem_handle_delete(file_priv, req->info.handle);
222 }
223
Dave Airlie29d08b32010-09-27 16:17:17 +1000224 /* drop reference from allocate - handle holds it now */
225 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000226 return ret;
227}
228
229static int
230nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
231 uint32_t write_domains, uint32_t valid_domains)
232{
233 struct nouveau_bo *nvbo = gem->driver_private;
234 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000235 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100236 (write_domains ? write_domains : read_domains);
237 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000238
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100239 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000240 return -EINVAL;
241
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100242 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
243 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000244
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100245 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
246 valid_flags |= TTM_PL_FLAG_TT;
247
248 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
249 bo->mem.mem_type == TTM_PL_VRAM)
250 pref_flags |= TTM_PL_FLAG_VRAM;
251
252 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
253 bo->mem.mem_type == TTM_PL_TT)
254 pref_flags |= TTM_PL_FLAG_TT;
255
256 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
257 pref_flags |= TTM_PL_FLAG_VRAM;
258
259 else
260 pref_flags |= TTM_PL_FLAG_TT;
261
262 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
263
Ben Skeggs6ee73862009-12-11 19:24:15 +1000264 return 0;
265}
266
267struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000268 struct list_head vram_list;
269 struct list_head gart_list;
270 struct list_head both_list;
271};
272
273static void
274validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
275{
276 struct list_head *entry, *tmp;
277 struct nouveau_bo *nvbo;
278
279 list_for_each_safe(entry, tmp, list) {
280 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000281
Francisco Jerez332b2422010-10-20 23:35:40 +0200282 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000283
Ben Skeggsa1606a92010-02-12 10:27:35 +1000284 if (unlikely(nvbo->validate_mapped)) {
285 ttm_bo_kunmap(&nvbo->kmap);
286 nvbo->validate_mapped = false;
287 }
288
Ben Skeggs6ee73862009-12-11 19:24:15 +1000289 list_del(&nvbo->entry);
290 nvbo->reserved_by = NULL;
291 ttm_bo_unreserve(&nvbo->bo);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200292 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000293 }
294}
295
296static void
Luca Barbieri234896a2010-01-06 04:02:45 +0100297validate_fini(struct validate_op *op, struct nouveau_fence* fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000298{
Luca Barbieri234896a2010-01-06 04:02:45 +0100299 validate_fini_list(&op->vram_list, fence);
300 validate_fini_list(&op->gart_list, fence);
301 validate_fini_list(&op->both_list, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000302}
303
304static int
305validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
306 struct drm_nouveau_gem_pushbuf_bo *pbbo,
307 int nr_buffers, struct validate_op *op)
308{
309 struct drm_device *dev = chan->dev;
310 struct drm_nouveau_private *dev_priv = dev->dev_private;
311 uint32_t sequence;
312 int trycnt = 0;
313 int ret, i;
314
315 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
316retry:
317 if (++trycnt > 100000) {
318 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
319 return -EINVAL;
320 }
321
322 for (i = 0; i < nr_buffers; i++) {
323 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
324 struct drm_gem_object *gem;
325 struct nouveau_bo *nvbo;
326
327 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
328 if (!gem) {
329 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
330 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100331 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000332 }
333 nvbo = gem->driver_private;
334
335 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
336 NV_ERROR(dev, "multiple instances of buffer %d on "
337 "validation list\n", b->handle);
338 validate_fini(op, NULL);
339 return -EINVAL;
340 }
341
Ben Skeggs938c40e2010-10-12 09:54:54 +1000342 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000343 if (ret) {
344 validate_fini(op, NULL);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000345 if (unlikely(ret == -EAGAIN))
346 ret = ttm_bo_wait_unreserved(&nvbo->bo, true);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200347 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000348 if (unlikely(ret)) {
349 if (ret != -ERESTARTSYS)
350 NV_ERROR(dev, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000351 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000352 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000353 goto retry;
354 }
355
Ben Skeggsa1606a92010-02-12 10:27:35 +1000356 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000357 nvbo->reserved_by = file_priv;
358 nvbo->pbbo_index = i;
359 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
360 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
361 list_add_tail(&nvbo->entry, &op->both_list);
362 else
363 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
364 list_add_tail(&nvbo->entry, &op->vram_list);
365 else
366 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
367 list_add_tail(&nvbo->entry, &op->gart_list);
368 else {
369 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
370 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000371 list_add_tail(&nvbo->entry, &op->both_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000372 validate_fini(op, NULL);
373 return -EINVAL;
374 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000375 }
376
377 return 0;
378}
379
380static int
381validate_list(struct nouveau_channel *chan, struct list_head *list,
382 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
383{
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000384 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000385 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
386 (void __force __user *)(uintptr_t)user_pbbo_ptr;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000387 struct drm_device *dev = chan->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000388 struct nouveau_bo *nvbo;
389 int ret, relocs = 0;
390
391 list_for_each_entry(nvbo, list, entry) {
392 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000393
Francisco Jerez27307232010-09-21 18:57:11 +0200394 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
Ben Skeggs415e6182010-07-23 09:06:52 +1000395 if (unlikely(ret)) {
396 NV_ERROR(dev, "fail pre-validate sync\n");
397 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000398 }
399
400 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
401 b->write_domains,
402 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000403 if (unlikely(ret)) {
404 NV_ERROR(dev, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000405 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000406 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000407
Ben Skeggs415e6182010-07-23 09:06:52 +1000408 nvbo->channel = (b->read_domains & (1 << 31)) ? NULL : chan;
Ben Skeggs7a45d762010-11-22 08:50:27 +1000409 ret = nouveau_bo_validate(nvbo, true, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000410 nvbo->channel = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000411 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000412 if (ret != -ERESTARTSYS)
413 NV_ERROR(dev, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000414 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000415 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000416
Francisco Jerez27307232010-09-21 18:57:11 +0200417 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
Ben Skeggs415e6182010-07-23 09:06:52 +1000418 if (unlikely(ret)) {
419 NV_ERROR(dev, "fail post-validate sync\n");
420 return ret;
421 }
422
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000423 if (dev_priv->card_type < NV_50) {
424 if (nvbo->bo.offset == b->presumed.offset &&
425 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
426 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
427 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
428 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
429 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000430
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000431 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
432 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
433 else
434 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
435 b->presumed.offset = nvbo->bo.offset;
436 b->presumed.valid = 0;
437 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000438
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000439 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
440 &b->presumed, sizeof(b->presumed)))
441 return -EFAULT;
442 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000443 }
444
445 return relocs;
446}
447
448static int
449nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
450 struct drm_file *file_priv,
451 struct drm_nouveau_gem_pushbuf_bo *pbbo,
452 uint64_t user_buffers, int nr_buffers,
453 struct validate_op *op, int *apply_relocs)
454{
Ben Skeggsa1606a92010-02-12 10:27:35 +1000455 struct drm_device *dev = chan->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000456 int ret, relocs = 0;
457
458 INIT_LIST_HEAD(&op->vram_list);
459 INIT_LIST_HEAD(&op->gart_list);
460 INIT_LIST_HEAD(&op->both_list);
461
Ben Skeggs6ee73862009-12-11 19:24:15 +1000462 if (nr_buffers == 0)
463 return 0;
464
465 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000466 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000467 if (ret != -ERESTARTSYS)
468 NV_ERROR(dev, "validate_init\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
472 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
473 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000474 if (ret != -ERESTARTSYS)
475 NV_ERROR(dev, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000476 validate_fini(op, NULL);
477 return ret;
478 }
479 relocs += ret;
480
481 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
482 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000483 if (ret != -ERESTARTSYS)
484 NV_ERROR(dev, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000485 validate_fini(op, NULL);
486 return ret;
487 }
488 relocs += ret;
489
490 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
491 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000492 if (ret != -ERESTARTSYS)
493 NV_ERROR(dev, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000494 validate_fini(op, NULL);
495 return ret;
496 }
497 relocs += ret;
498
499 *apply_relocs = relocs;
500 return 0;
501}
502
503static inline void *
504u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
505{
506 void *mem;
507 void __user *userptr = (void __force __user *)(uintptr_t)user;
508
509 mem = kmalloc(nmemb * size, GFP_KERNEL);
510 if (!mem)
511 return ERR_PTR(-ENOMEM);
512
513 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
514 kfree(mem);
515 return ERR_PTR(-EFAULT);
516 }
517
518 return mem;
519}
520
521static int
Ben Skeggsa1606a92010-02-12 10:27:35 +1000522nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
523 struct drm_nouveau_gem_pushbuf *req,
524 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000525{
526 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100527 int ret = 0;
528 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000529
Ben Skeggsa1606a92010-02-12 10:27:35 +1000530 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000531 if (IS_ERR(reloc))
532 return PTR_ERR(reloc);
533
Ben Skeggsa1606a92010-02-12 10:27:35 +1000534 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000535 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
536 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000537 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000538 uint32_t data;
539
Ben Skeggsa1606a92010-02-12 10:27:35 +1000540 if (unlikely(r->bo_index > req->nr_buffers)) {
541 NV_ERROR(dev, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000542 ret = -EINVAL;
543 break;
544 }
545
546 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000547 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000548 continue;
549
Ben Skeggsa1606a92010-02-12 10:27:35 +1000550 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
551 NV_ERROR(dev, "reloc container bo index invalid\n");
552 ret = -EINVAL;
553 break;
554 }
555 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
556
557 if (unlikely(r->reloc_bo_offset + 4 >
558 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
559 NV_ERROR(dev, "reloc outside of bo\n");
560 ret = -EINVAL;
561 break;
562 }
563
564 if (!nvbo->kmap.virtual) {
565 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
566 &nvbo->kmap);
567 if (ret) {
568 NV_ERROR(dev, "failed kmap for reloc\n");
569 break;
570 }
571 nvbo->validate_mapped = true;
572 }
573
Ben Skeggs6ee73862009-12-11 19:24:15 +1000574 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000575 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000576 else
577 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000578 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000579 else
580 data = r->data;
581
582 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000583 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000584 data |= r->tor;
585 else
586 data |= r->vor;
587 }
588
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000589 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000590 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000591 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000592 if (ret) {
593 NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret);
594 break;
595 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000596
597 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000598 }
599
600 kfree(reloc);
601 return ret;
602}
603
604int
605nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
606 struct drm_file *file_priv)
607{
Ben Skeggsa1606a92010-02-12 10:27:35 +1000608 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000609 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000610 struct drm_nouveau_gem_pushbuf_push *push;
611 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000612 struct nouveau_channel *chan;
613 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200614 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000615 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000616
Ben Skeggse8a863c2011-06-01 19:18:48 +1000617 chan = nouveau_channel_get(file_priv, req->channel);
Ben Skeggscff5c132010-10-06 16:16:59 +1000618 if (IS_ERR(chan))
619 return PTR_ERR(chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000620
Ben Skeggsa1606a92010-02-12 10:27:35 +1000621 req->vram_available = dev_priv->fb_aper_free;
622 req->gart_available = dev_priv->gart_info.aper_free;
623 if (unlikely(req->nr_push == 0))
624 goto out_next;
625
626 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
627 NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n",
628 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggscff5c132010-10-06 16:16:59 +1000629 nouveau_channel_put(&chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000630 return -EINVAL;
631 }
632
Ben Skeggsa1606a92010-02-12 10:27:35 +1000633 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
634 NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n",
635 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggscff5c132010-10-06 16:16:59 +1000636 nouveau_channel_put(&chan);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000637 return -EINVAL;
638 }
639
640 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
641 NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n",
642 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggscff5c132010-10-06 16:16:59 +1000643 nouveau_channel_put(&chan);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000644 return -EINVAL;
645 }
646
647 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggscff5c132010-10-06 16:16:59 +1000648 if (IS_ERR(push)) {
649 nouveau_channel_put(&chan);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000650 return PTR_ERR(push);
Ben Skeggscff5c132010-10-06 16:16:59 +1000651 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000652
653 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
654 if (IS_ERR(bo)) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000655 kfree(push);
Ben Skeggscff5c132010-10-06 16:16:59 +1000656 nouveau_channel_put(&chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000657 return PTR_ERR(bo);
658 }
659
Ben Skeggs415e6182010-07-23 09:06:52 +1000660 /* Mark push buffers as being used on PFIFO, the validation code
661 * will then make sure that if the pushbuf bo moves, that they
662 * happen on the kernel channel, which will in turn cause a sync
663 * to happen before we try and submit the push buffer.
664 */
665 for (i = 0; i < req->nr_push; i++) {
666 if (push[i].bo_index >= req->nr_buffers) {
667 NV_ERROR(dev, "push %d buffer not in list\n", i);
668 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100669 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000670 }
671
672 bo[push[i].bo_index].read_domains |= (1 << 31);
673 }
674
Ben Skeggs6ee73862009-12-11 19:24:15 +1000675 /* Validate buffer list */
676 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
677 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000678 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000679 if (ret != -ERESTARTSYS)
680 NV_ERROR(dev, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100681 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000682 }
683
Ben Skeggs6ee73862009-12-11 19:24:15 +1000684 /* Apply any relocations that are required */
685 if (do_reloc) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000686 ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000687 if (ret) {
688 NV_ERROR(dev, "reloc apply: %d\n", ret);
689 goto out;
690 }
691 }
692
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000693 if (chan->dma.ib_max) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000694 ret = nouveau_dma_wait(chan, req->nr_push + 1, 6);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000695 if (ret) {
696 NV_INFO(dev, "nv50cal_space: %d\n", ret);
697 goto out;
698 }
699
Ben Skeggsa1606a92010-02-12 10:27:35 +1000700 for (i = 0; i < req->nr_push; i++) {
701 struct nouveau_bo *nvbo = (void *)(unsigned long)
702 bo[push[i].bo_index].user_priv;
703
704 nv50_dma_push(chan, nvbo, push[i].offset,
705 push[i].length);
706 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000707 } else
Francisco Jerezee508b82010-08-25 12:54:53 +0200708 if (dev_priv->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000709 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000710 if (ret) {
711 NV_ERROR(dev, "cal_space: %d\n", ret);
712 goto out;
713 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000714
715 for (i = 0; i < req->nr_push; i++) {
716 struct nouveau_bo *nvbo = (void *)(unsigned long)
717 bo[push[i].bo_index].user_priv;
718 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
719
720 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
721 push[i].offset) | 2);
722 OUT_RING(chan, 0);
723 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000724 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000725 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000726 if (ret) {
727 NV_ERROR(dev, "jmp_space: %d\n", ret);
728 goto out;
729 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000730
Ben Skeggsa1606a92010-02-12 10:27:35 +1000731 for (i = 0; i < req->nr_push; i++) {
732 struct nouveau_bo *nvbo = (void *)(unsigned long)
733 bo[push[i].bo_index].user_priv;
734 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
735 uint32_t cmd;
736
737 cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
738 cmd |= 0x20000000;
739 if (unlikely(cmd != req->suffix0)) {
740 if (!nvbo->kmap.virtual) {
741 ret = ttm_bo_kmap(&nvbo->bo, 0,
742 nvbo->bo.mem.
743 num_pages,
744 &nvbo->kmap);
745 if (ret) {
746 WIND_RING(chan);
747 goto out;
748 }
749 nvbo->validate_mapped = true;
750 }
751
752 nouveau_bo_wr32(nvbo, (push[i].offset +
753 push[i].length - 8) / 4, cmd);
754 }
755
756 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
757 push[i].offset) | 0x20000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000758 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000759 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
760 OUT_RING(chan, 0);
761 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000762 }
763
Luca Barbieri234896a2010-01-06 04:02:45 +0100764 ret = nouveau_fence_new(chan, &fence, true);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000765 if (ret) {
766 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
767 WIND_RING(chan);
768 goto out;
769 }
770
771out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100772 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200773 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100774
775out_prevalid:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776 kfree(bo);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000777 kfree(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000778
779out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000780 if (chan->dma.ib_max) {
781 req->suffix0 = 0x00000000;
782 req->suffix1 = 0x00000000;
783 } else
Francisco Jerezee508b82010-08-25 12:54:53 +0200784 if (dev_priv->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000785 req->suffix0 = 0x00020000;
786 req->suffix1 = 0x00000000;
787 } else {
788 req->suffix0 = 0x20000000 |
789 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
790 req->suffix1 = 0x00000000;
791 }
792
Ben Skeggscff5c132010-10-06 16:16:59 +1000793 nouveau_channel_put(&chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000794 return ret;
795}
796
Ben Skeggs6ee73862009-12-11 19:24:15 +1000797static inline uint32_t
798domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
799{
800 uint32_t flags = 0;
801
802 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
803 flags |= TTM_PL_FLAG_VRAM;
804 if (domain & NOUVEAU_GEM_DOMAIN_GART)
805 flags |= TTM_PL_FLAG_TT;
806
807 return flags;
808}
809
810int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000811nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
812 struct drm_file *file_priv)
813{
814 struct drm_nouveau_gem_cpu_prep *req = data;
815 struct drm_gem_object *gem;
816 struct nouveau_bo *nvbo;
817 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
818 int ret = -EINVAL;
819
Ben Skeggs6ee73862009-12-11 19:24:15 +1000820 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
821 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100822 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000823 nvbo = nouveau_gem_object(gem);
824
Ben Skeggs21e86c12010-10-11 11:48:45 +1000825 spin_lock(&nvbo->bo.bdev->fence_lock);
826 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
827 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000828 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000829 return ret;
830}
831
832int
833nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
834 struct drm_file *file_priv)
835{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000836 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000837}
838
839int
840nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
841 struct drm_file *file_priv)
842{
843 struct drm_nouveau_gem_info *req = data;
844 struct drm_gem_object *gem;
845 int ret;
846
Ben Skeggs6ee73862009-12-11 19:24:15 +1000847 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
848 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100849 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000850
Ben Skeggse758a312011-06-07 13:28:11 +1000851 ret = nouveau_gem_info(file_priv, gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000852 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000853 return ret;
854}
855