blob: 2d1de2427bc3ba09953818f66fc81802797b4a93 [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);
66
67 if (!fpriv->vm)
68 return 0;
69
70 return 0;
71}
72
73void
74nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
75{
76 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
77
78 if (!fpriv->vm)
79 return;
80}
81
82int
Ben Skeggsf6d4e622011-06-07 12:25:36 +100083nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
84 uint32_t tile_mode, uint32_t tile_flags,
85 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +100086{
Ben Skeggsdb5c8e22011-02-10 13:41:01 +100087 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +100088 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +100089 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +100090 int ret;
91
Ben Skeggs6ba9a682011-02-10 14:42:08 +100092 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
93 flags |= TTM_PL_FLAG_VRAM;
94 if (domain & NOUVEAU_GEM_DOMAIN_GART)
95 flags |= TTM_PL_FLAG_TT;
96 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
97 flags |= TTM_PL_FLAG_SYSTEM;
98
Ben Skeggsf6d4e622011-06-07 12:25:36 +100099 ret = nouveau_bo_new(dev, NULL, size, align, flags, tile_mode,
Ben Skeggsd550c412011-02-16 08:41:56 +1000100 tile_flags, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000101 if (ret)
102 return ret;
103 nvbo = *pnvbo;
104
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000105 /* we restrict allowed domains on nv50+ to only the types
106 * that were requested at creation time. not possibly on
107 * earlier chips without busting the ABI.
108 */
109 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
110 NOUVEAU_GEM_DOMAIN_GART;
111 if (dev_priv->card_type >= NV_50)
112 nvbo->valid_domains &= domain;
113
Ben Skeggs6ee73862009-12-11 19:24:15 +1000114 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size);
115 if (!nvbo->gem) {
116 nouveau_bo_ref(NULL, pnvbo);
117 return -ENOMEM;
118 }
119
Jan Engelhardt5df23972011-04-04 01:25:18 +0200120 nvbo->bo.persistent_swap_storage = nvbo->gem->filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000121 nvbo->gem->driver_private = nvbo;
122 return 0;
123}
124
125static int
126nouveau_gem_info(struct drm_gem_object *gem, struct drm_nouveau_gem_info *rep)
127{
Ben Skeggs07533ea2011-06-07 11:02:38 +1000128 struct drm_nouveau_private *dev_priv = gem->dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000129 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
130
131 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
132 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
133 else
134 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
135
136 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
Ben Skeggs07533ea2011-06-07 11:02:38 +1000137 if (dev_priv->card_type < NV_50)
138 rep->offset = nvbo->bo.offset;
139 else
140 rep->offset = nvbo->vma.offset;
Ben Skeggsd550c412011-02-16 08:41:56 +1000141 rep->map_handle = nvbo->bo.addr_space_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000142 rep->tile_mode = nvbo->tile_mode;
143 rep->tile_flags = nvbo->tile_flags;
144 return 0;
145}
146
Ben Skeggs6ee73862009-12-11 19:24:15 +1000147int
148nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
149 struct drm_file *file_priv)
150{
151 struct drm_nouveau_private *dev_priv = dev->dev_private;
152 struct drm_nouveau_gem_new *req = data;
153 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000154 int ret = 0;
155
Ben Skeggs6ee73862009-12-11 19:24:15 +1000156 if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL))
157 dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping;
158
Ben Skeggs60d2a882010-12-06 15:28:54 +1000159 if (!dev_priv->engine.vram.flags_valid(dev, req->info.tile_flags)) {
160 NV_ERROR(dev, "bad page flags: 0x%08x\n", req->info.tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000161 return -EINVAL;
Ben Skeggs60d2a882010-12-06 15:28:54 +1000162 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000163
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000164 ret = nouveau_gem_new(dev, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000165 req->info.domain, req->info.tile_mode,
166 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000167 if (ret)
168 return ret;
169
170 ret = nouveau_gem_info(nvbo->gem, &req->info);
171 if (ret)
172 goto out;
173
174 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle);
Dave Airlie29d08b32010-09-27 16:17:17 +1000175 /* drop reference from allocate - handle holds it now */
176 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000177out:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000178 return ret;
179}
180
181static int
182nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
183 uint32_t write_domains, uint32_t valid_domains)
184{
185 struct nouveau_bo *nvbo = gem->driver_private;
186 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000187 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100188 (write_domains ? write_domains : read_domains);
189 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000190
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100191 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000192 return -EINVAL;
193
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100194 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
195 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000196
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100197 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
198 valid_flags |= TTM_PL_FLAG_TT;
199
200 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
201 bo->mem.mem_type == TTM_PL_VRAM)
202 pref_flags |= TTM_PL_FLAG_VRAM;
203
204 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
205 bo->mem.mem_type == TTM_PL_TT)
206 pref_flags |= TTM_PL_FLAG_TT;
207
208 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
209 pref_flags |= TTM_PL_FLAG_VRAM;
210
211 else
212 pref_flags |= TTM_PL_FLAG_TT;
213
214 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
215
Ben Skeggs6ee73862009-12-11 19:24:15 +1000216 return 0;
217}
218
219struct validate_op {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220 struct list_head vram_list;
221 struct list_head gart_list;
222 struct list_head both_list;
223};
224
225static void
226validate_fini_list(struct list_head *list, struct nouveau_fence *fence)
227{
228 struct list_head *entry, *tmp;
229 struct nouveau_bo *nvbo;
230
231 list_for_each_safe(entry, tmp, list) {
232 nvbo = list_entry(entry, struct nouveau_bo, entry);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000233
Francisco Jerez332b2422010-10-20 23:35:40 +0200234 nouveau_bo_fence(nvbo, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235
Ben Skeggsa1606a92010-02-12 10:27:35 +1000236 if (unlikely(nvbo->validate_mapped)) {
237 ttm_bo_kunmap(&nvbo->kmap);
238 nvbo->validate_mapped = false;
239 }
240
Ben Skeggs6ee73862009-12-11 19:24:15 +1000241 list_del(&nvbo->entry);
242 nvbo->reserved_by = NULL;
243 ttm_bo_unreserve(&nvbo->bo);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200244 drm_gem_object_unreference_unlocked(nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000245 }
246}
247
248static void
Luca Barbieri234896a2010-01-06 04:02:45 +0100249validate_fini(struct validate_op *op, struct nouveau_fence* fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000250{
Luca Barbieri234896a2010-01-06 04:02:45 +0100251 validate_fini_list(&op->vram_list, fence);
252 validate_fini_list(&op->gart_list, fence);
253 validate_fini_list(&op->both_list, fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254}
255
256static int
257validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
258 struct drm_nouveau_gem_pushbuf_bo *pbbo,
259 int nr_buffers, struct validate_op *op)
260{
261 struct drm_device *dev = chan->dev;
262 struct drm_nouveau_private *dev_priv = dev->dev_private;
263 uint32_t sequence;
264 int trycnt = 0;
265 int ret, i;
266
267 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence);
268retry:
269 if (++trycnt > 100000) {
270 NV_ERROR(dev, "%s failed and gave up.\n", __func__);
271 return -EINVAL;
272 }
273
274 for (i = 0; i < nr_buffers; i++) {
275 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
276 struct drm_gem_object *gem;
277 struct nouveau_bo *nvbo;
278
279 gem = drm_gem_object_lookup(dev, file_priv, b->handle);
280 if (!gem) {
281 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle);
282 validate_fini(op, NULL);
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100283 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000284 }
285 nvbo = gem->driver_private;
286
287 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
288 NV_ERROR(dev, "multiple instances of buffer %d on "
289 "validation list\n", b->handle);
290 validate_fini(op, NULL);
291 return -EINVAL;
292 }
293
Ben Skeggs938c40e2010-10-12 09:54:54 +1000294 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000295 if (ret) {
296 validate_fini(op, NULL);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000297 if (unlikely(ret == -EAGAIN))
298 ret = ttm_bo_wait_unreserved(&nvbo->bo, true);
Francisco Jerez374c3af2010-08-29 12:21:16 +0200299 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs938c40e2010-10-12 09:54:54 +1000300 if (unlikely(ret)) {
301 if (ret != -ERESTARTSYS)
302 NV_ERROR(dev, "fail reserve\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000303 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000304 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000305 goto retry;
306 }
307
Ben Skeggsa1606a92010-02-12 10:27:35 +1000308 b->user_priv = (uint64_t)(unsigned long)nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000309 nvbo->reserved_by = file_priv;
310 nvbo->pbbo_index = i;
311 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
312 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
313 list_add_tail(&nvbo->entry, &op->both_list);
314 else
315 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
316 list_add_tail(&nvbo->entry, &op->vram_list);
317 else
318 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
319 list_add_tail(&nvbo->entry, &op->gart_list);
320 else {
321 NV_ERROR(dev, "invalid valid domains: 0x%08x\n",
322 b->valid_domains);
Ben Skeggs02088432010-01-21 15:03:23 +1000323 list_add_tail(&nvbo->entry, &op->both_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000324 validate_fini(op, NULL);
325 return -EINVAL;
326 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000327 }
328
329 return 0;
330}
331
332static int
333validate_list(struct nouveau_channel *chan, struct list_head *list,
334 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr)
335{
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000336 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000337 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
338 (void __force __user *)(uintptr_t)user_pbbo_ptr;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000339 struct drm_device *dev = chan->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000340 struct nouveau_bo *nvbo;
341 int ret, relocs = 0;
342
343 list_for_each_entry(nvbo, list, entry) {
344 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000345
Francisco Jerez27307232010-09-21 18:57:11 +0200346 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
Ben Skeggs415e6182010-07-23 09:06:52 +1000347 if (unlikely(ret)) {
348 NV_ERROR(dev, "fail pre-validate sync\n");
349 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000350 }
351
352 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains,
353 b->write_domains,
354 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000355 if (unlikely(ret)) {
356 NV_ERROR(dev, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000357 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000358 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000359
Ben Skeggs415e6182010-07-23 09:06:52 +1000360 nvbo->channel = (b->read_domains & (1 << 31)) ? NULL : chan;
Ben Skeggs7a45d762010-11-22 08:50:27 +1000361 ret = nouveau_bo_validate(nvbo, true, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000362 nvbo->channel = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000363 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000364 if (ret != -ERESTARTSYS)
365 NV_ERROR(dev, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000366 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000367 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000368
Francisco Jerez27307232010-09-21 18:57:11 +0200369 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan);
Ben Skeggs415e6182010-07-23 09:06:52 +1000370 if (unlikely(ret)) {
371 NV_ERROR(dev, "fail post-validate sync\n");
372 return ret;
373 }
374
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000375 if (dev_priv->card_type < NV_50) {
376 if (nvbo->bo.offset == b->presumed.offset &&
377 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
378 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
379 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
380 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
381 continue;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000382
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000383 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
384 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
385 else
386 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
387 b->presumed.offset = nvbo->bo.offset;
388 b->presumed.valid = 0;
389 relocs++;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000390
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000391 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed,
392 &b->presumed, sizeof(b->presumed)))
393 return -EFAULT;
394 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000395 }
396
397 return relocs;
398}
399
400static int
401nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
402 struct drm_file *file_priv,
403 struct drm_nouveau_gem_pushbuf_bo *pbbo,
404 uint64_t user_buffers, int nr_buffers,
405 struct validate_op *op, int *apply_relocs)
406{
Ben Skeggsa1606a92010-02-12 10:27:35 +1000407 struct drm_device *dev = chan->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000408 int ret, relocs = 0;
409
410 INIT_LIST_HEAD(&op->vram_list);
411 INIT_LIST_HEAD(&op->gart_list);
412 INIT_LIST_HEAD(&op->both_list);
413
Ben Skeggs6ee73862009-12-11 19:24:15 +1000414 if (nr_buffers == 0)
415 return 0;
416
417 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000418 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000419 if (ret != -ERESTARTSYS)
420 NV_ERROR(dev, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000421 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000422 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000423
424 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers);
425 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000426 if (ret != -ERESTARTSYS)
427 NV_ERROR(dev, "validate vram_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000428 validate_fini(op, NULL);
429 return ret;
430 }
431 relocs += ret;
432
433 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers);
434 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000435 if (ret != -ERESTARTSYS)
436 NV_ERROR(dev, "validate gart_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000437 validate_fini(op, NULL);
438 return ret;
439 }
440 relocs += ret;
441
442 ret = validate_list(chan, &op->both_list, pbbo, user_buffers);
443 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000444 if (ret != -ERESTARTSYS)
445 NV_ERROR(dev, "validate both_list\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000446 validate_fini(op, NULL);
447 return ret;
448 }
449 relocs += ret;
450
451 *apply_relocs = relocs;
452 return 0;
453}
454
455static inline void *
456u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
457{
458 void *mem;
459 void __user *userptr = (void __force __user *)(uintptr_t)user;
460
461 mem = kmalloc(nmemb * size, GFP_KERNEL);
462 if (!mem)
463 return ERR_PTR(-ENOMEM);
464
465 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) {
466 kfree(mem);
467 return ERR_PTR(-EFAULT);
468 }
469
470 return mem;
471}
472
473static int
Ben Skeggsa1606a92010-02-12 10:27:35 +1000474nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev,
475 struct drm_nouveau_gem_pushbuf *req,
476 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000477{
478 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100479 int ret = 0;
480 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000481
Ben Skeggsa1606a92010-02-12 10:27:35 +1000482 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000483 if (IS_ERR(reloc))
484 return PTR_ERR(reloc);
485
Ben Skeggsa1606a92010-02-12 10:27:35 +1000486 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000487 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
488 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000489 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000490 uint32_t data;
491
Ben Skeggsa1606a92010-02-12 10:27:35 +1000492 if (unlikely(r->bo_index > req->nr_buffers)) {
493 NV_ERROR(dev, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000494 ret = -EINVAL;
495 break;
496 }
497
498 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000499 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000500 continue;
501
Ben Skeggsa1606a92010-02-12 10:27:35 +1000502 if (unlikely(r->reloc_bo_index > req->nr_buffers)) {
503 NV_ERROR(dev, "reloc container bo index invalid\n");
504 ret = -EINVAL;
505 break;
506 }
507 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
508
509 if (unlikely(r->reloc_bo_offset + 4 >
510 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
511 NV_ERROR(dev, "reloc outside of bo\n");
512 ret = -EINVAL;
513 break;
514 }
515
516 if (!nvbo->kmap.virtual) {
517 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
518 &nvbo->kmap);
519 if (ret) {
520 NV_ERROR(dev, "failed kmap for reloc\n");
521 break;
522 }
523 nvbo->validate_mapped = true;
524 }
525
Ben Skeggs6ee73862009-12-11 19:24:15 +1000526 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000527 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000528 else
529 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000530 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000531 else
532 data = r->data;
533
534 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000535 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000536 data |= r->tor;
537 else
538 data |= r->vor;
539 }
540
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000541 spin_lock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000542 ret = ttm_bo_wait(&nvbo->bo, false, false, false);
Thomas Hellstrom702adba2010-11-17 12:28:29 +0000543 spin_unlock(&nvbo->bo.bdev->fence_lock);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000544 if (ret) {
545 NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret);
546 break;
547 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000548
549 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000550 }
551
552 kfree(reloc);
553 return ret;
554}
555
556int
557nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
558 struct drm_file *file_priv)
559{
Ben Skeggsa1606a92010-02-12 10:27:35 +1000560 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000561 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000562 struct drm_nouveau_gem_pushbuf_push *push;
563 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000564 struct nouveau_channel *chan;
565 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200566 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000567 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000568
Ben Skeggse8a863c2011-06-01 19:18:48 +1000569 chan = nouveau_channel_get(file_priv, req->channel);
Ben Skeggscff5c132010-10-06 16:16:59 +1000570 if (IS_ERR(chan))
571 return PTR_ERR(chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000572
Ben Skeggsa1606a92010-02-12 10:27:35 +1000573 req->vram_available = dev_priv->fb_aper_free;
574 req->gart_available = dev_priv->gart_info.aper_free;
575 if (unlikely(req->nr_push == 0))
576 goto out_next;
577
578 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
579 NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n",
580 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggscff5c132010-10-06 16:16:59 +1000581 nouveau_channel_put(&chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000582 return -EINVAL;
583 }
584
Ben Skeggsa1606a92010-02-12 10:27:35 +1000585 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
586 NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n",
587 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggscff5c132010-10-06 16:16:59 +1000588 nouveau_channel_put(&chan);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000589 return -EINVAL;
590 }
591
592 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
593 NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n",
594 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggscff5c132010-10-06 16:16:59 +1000595 nouveau_channel_put(&chan);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000596 return -EINVAL;
597 }
598
599 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggscff5c132010-10-06 16:16:59 +1000600 if (IS_ERR(push)) {
601 nouveau_channel_put(&chan);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000602 return PTR_ERR(push);
Ben Skeggscff5c132010-10-06 16:16:59 +1000603 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000604
605 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
606 if (IS_ERR(bo)) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000607 kfree(push);
Ben Skeggscff5c132010-10-06 16:16:59 +1000608 nouveau_channel_put(&chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000609 return PTR_ERR(bo);
610 }
611
Ben Skeggs415e6182010-07-23 09:06:52 +1000612 /* Mark push buffers as being used on PFIFO, the validation code
613 * will then make sure that if the pushbuf bo moves, that they
614 * happen on the kernel channel, which will in turn cause a sync
615 * to happen before we try and submit the push buffer.
616 */
617 for (i = 0; i < req->nr_push; i++) {
618 if (push[i].bo_index >= req->nr_buffers) {
619 NV_ERROR(dev, "push %d buffer not in list\n", i);
620 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100621 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000622 }
623
624 bo[push[i].bo_index].read_domains |= (1 << 31);
625 }
626
Ben Skeggs6ee73862009-12-11 19:24:15 +1000627 /* Validate buffer list */
628 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
629 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000630 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000631 if (ret != -ERESTARTSYS)
632 NV_ERROR(dev, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100633 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000634 }
635
Ben Skeggs6ee73862009-12-11 19:24:15 +1000636 /* Apply any relocations that are required */
637 if (do_reloc) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000638 ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000639 if (ret) {
640 NV_ERROR(dev, "reloc apply: %d\n", ret);
641 goto out;
642 }
643 }
644
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000645 if (chan->dma.ib_max) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000646 ret = nouveau_dma_wait(chan, req->nr_push + 1, 6);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000647 if (ret) {
648 NV_INFO(dev, "nv50cal_space: %d\n", ret);
649 goto out;
650 }
651
Ben Skeggsa1606a92010-02-12 10:27:35 +1000652 for (i = 0; i < req->nr_push; i++) {
653 struct nouveau_bo *nvbo = (void *)(unsigned long)
654 bo[push[i].bo_index].user_priv;
655
656 nv50_dma_push(chan, nvbo, push[i].offset,
657 push[i].length);
658 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000659 } else
Francisco Jerezee508b82010-08-25 12:54:53 +0200660 if (dev_priv->chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000661 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000662 if (ret) {
663 NV_ERROR(dev, "cal_space: %d\n", ret);
664 goto out;
665 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000666
667 for (i = 0; i < req->nr_push; i++) {
668 struct nouveau_bo *nvbo = (void *)(unsigned long)
669 bo[push[i].bo_index].user_priv;
670 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
671
672 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
673 push[i].offset) | 2);
674 OUT_RING(chan, 0);
675 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000676 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000677 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000678 if (ret) {
679 NV_ERROR(dev, "jmp_space: %d\n", ret);
680 goto out;
681 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000682
Ben Skeggsa1606a92010-02-12 10:27:35 +1000683 for (i = 0; i < req->nr_push; i++) {
684 struct nouveau_bo *nvbo = (void *)(unsigned long)
685 bo[push[i].bo_index].user_priv;
686 struct drm_mm_node *mem = nvbo->bo.mem.mm_node;
687 uint32_t cmd;
688
689 cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2);
690 cmd |= 0x20000000;
691 if (unlikely(cmd != req->suffix0)) {
692 if (!nvbo->kmap.virtual) {
693 ret = ttm_bo_kmap(&nvbo->bo, 0,
694 nvbo->bo.mem.
695 num_pages,
696 &nvbo->kmap);
697 if (ret) {
698 WIND_RING(chan);
699 goto out;
700 }
701 nvbo->validate_mapped = true;
702 }
703
704 nouveau_bo_wr32(nvbo, (push[i].offset +
705 push[i].length - 8) / 4, cmd);
706 }
707
708 OUT_RING(chan, ((mem->start << PAGE_SHIFT) +
709 push[i].offset) | 0x20000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000710 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000711 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
712 OUT_RING(chan, 0);
713 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000714 }
715
Luca Barbieri234896a2010-01-06 04:02:45 +0100716 ret = nouveau_fence_new(chan, &fence, true);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000717 if (ret) {
718 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret);
719 WIND_RING(chan);
720 goto out;
721 }
722
723out:
Luca Barbieri234896a2010-01-06 04:02:45 +0100724 validate_fini(&op, fence);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200725 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100726
727out_prevalid:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000728 kfree(bo);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000729 kfree(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000730
731out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000732 if (chan->dma.ib_max) {
733 req->suffix0 = 0x00000000;
734 req->suffix1 = 0x00000000;
735 } else
Francisco Jerezee508b82010-08-25 12:54:53 +0200736 if (dev_priv->chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000737 req->suffix0 = 0x00020000;
738 req->suffix1 = 0x00000000;
739 } else {
740 req->suffix0 = 0x20000000 |
741 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2));
742 req->suffix1 = 0x00000000;
743 }
744
Ben Skeggscff5c132010-10-06 16:16:59 +1000745 nouveau_channel_put(&chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000746 return ret;
747}
748
Ben Skeggs6ee73862009-12-11 19:24:15 +1000749static inline uint32_t
750domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
751{
752 uint32_t flags = 0;
753
754 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
755 flags |= TTM_PL_FLAG_VRAM;
756 if (domain & NOUVEAU_GEM_DOMAIN_GART)
757 flags |= TTM_PL_FLAG_TT;
758
759 return flags;
760}
761
762int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000763nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
764 struct drm_file *file_priv)
765{
766 struct drm_nouveau_gem_cpu_prep *req = data;
767 struct drm_gem_object *gem;
768 struct nouveau_bo *nvbo;
769 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
770 int ret = -EINVAL;
771
Ben Skeggs6ee73862009-12-11 19:24:15 +1000772 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
773 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100774 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000775 nvbo = nouveau_gem_object(gem);
776
Ben Skeggs21e86c12010-10-11 11:48:45 +1000777 spin_lock(&nvbo->bo.bdev->fence_lock);
778 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait);
779 spin_unlock(&nvbo->bo.bdev->fence_lock);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000780 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000781 return ret;
782}
783
784int
785nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
786 struct drm_file *file_priv)
787{
Ben Skeggs21e86c12010-10-11 11:48:45 +1000788 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000789}
790
791int
792nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
793 struct drm_file *file_priv)
794{
795 struct drm_nouveau_gem_info *req = data;
796 struct drm_gem_object *gem;
797 int ret;
798
Ben Skeggs6ee73862009-12-11 19:24:15 +1000799 gem = drm_gem_object_lookup(dev, file_priv, req->handle);
800 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100801 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000802
803 ret = nouveau_gem_info(gem, req);
Luca Barbieribc9025b2010-02-09 05:49:12 +0000804 drm_gem_object_unreference_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000805 return ret;
806}
807