Commit | Line | Data |
---|---|---|
6ee73861 BS |
1 | /* |
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 | ||
35 | int | |
36 | nouveau_gem_object_new(struct drm_gem_object *gem) | |
37 | { | |
38 | return 0; | |
39 | } | |
40 | ||
41 | void | |
42 | nouveau_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 | ||
6ee73861 BS |
51 | if (unlikely(nvbo->pin_refcnt)) { |
52 | nvbo->pin_refcnt = 1; | |
53 | nouveau_bo_unpin(nvbo); | |
54 | } | |
55 | ||
56 | ttm_bo_unref(&bo); | |
fd632aa3 DV |
57 | |
58 | drm_gem_object_release(gem); | |
59 | kfree(gem); | |
6ee73861 BS |
60 | } |
61 | ||
639212d0 BS |
62 | int |
63 | nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv) | |
64 | { | |
65 | struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv); | |
2fd3db6f BS |
66 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
67 | struct nouveau_vma *vma; | |
68 | int ret; | |
639212d0 BS |
69 | |
70 | if (!fpriv->vm) | |
71 | return 0; | |
72 | ||
2fd3db6f BS |
73 | 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 | ||
94 | out: | |
95 | ttm_bo_unreserve(&nvbo->bo); | |
96 | return ret; | |
639212d0 BS |
97 | } |
98 | ||
99 | void | |
100 | nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv) | |
101 | { | |
102 | struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv); | |
2fd3db6f BS |
103 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
104 | struct nouveau_vma *vma; | |
105 | int ret; | |
639212d0 BS |
106 | |
107 | if (!fpriv->vm) | |
108 | return; | |
2fd3db6f BS |
109 | |
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); | |
639212d0 BS |
120 | } |
121 | ||
6ee73861 | 122 | int |
f6d4e621 BS |
123 | nouveau_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) | |
6ee73861 | 126 | { |
db5c8e29 | 127 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
6ee73861 | 128 | struct nouveau_bo *nvbo; |
6ba9a683 | 129 | u32 flags = 0; |
6ee73861 BS |
130 | int ret; |
131 | ||
6ba9a683 BS |
132 | 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 | ||
7375c95b | 139 | ret = nouveau_bo_new(dev, size, align, flags, tile_mode, |
d550c41e | 140 | tile_flags, pnvbo); |
6ee73861 BS |
141 | if (ret) |
142 | return ret; | |
143 | nvbo = *pnvbo; | |
144 | ||
db5c8e29 BS |
145 | /* 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 | ||
6ee73861 BS |
154 | 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 | ||
5df23979 | 160 | nvbo->bo.persistent_swap_storage = nvbo->gem->filp; |
6ee73861 BS |
161 | nvbo->gem->driver_private = nvbo; |
162 | return 0; | |
163 | } | |
164 | ||
165 | static int | |
e758a311 BS |
166 | nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem, |
167 | struct drm_nouveau_gem_info *rep) | |
6ee73861 | 168 | { |
e758a311 | 169 | struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv); |
6ee73861 | 170 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
e758a311 | 171 | struct nouveau_vma *vma; |
6ee73861 BS |
172 | |
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 | ||
e758a311 BS |
178 | 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 | ||
6ee73861 | 187 | rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT; |
d550c41e | 188 | rep->map_handle = nvbo->bo.addr_space_offset; |
6ee73861 BS |
189 | rep->tile_mode = nvbo->tile_mode; |
190 | rep->tile_flags = nvbo->tile_flags; | |
191 | return 0; | |
192 | } | |
193 | ||
6ee73861 BS |
194 | int |
195 | nouveau_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; | |
6ee73861 BS |
201 | int ret = 0; |
202 | ||
6ee73861 BS |
203 | if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL)) |
204 | dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping; | |
205 | ||
60d2a88a BS |
206 | 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); | |
6ee73861 | 208 | return -EINVAL; |
60d2a88a | 209 | } |
6ee73861 | 210 | |
f6d4e621 | 211 | ret = nouveau_gem_new(dev, req->info.size, req->align, |
6ba9a683 BS |
212 | req->info.domain, req->info.tile_mode, |
213 | req->info.tile_flags, &nvbo); | |
6ee73861 BS |
214 | if (ret) |
215 | return ret; | |
216 | ||
6ee73861 | 217 | ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle); |
e758a311 BS |
218 | 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 | ||
29d08b3e DA |
224 | /* drop reference from allocate - handle holds it now */ |
225 | drm_gem_object_unreference_unlocked(nvbo->gem); | |
6ee73861 BS |
226 | return ret; |
227 | } | |
228 | ||
229 | static int | |
230 | nouveau_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; | |
db5c8e29 | 235 | uint32_t domains = valid_domains & nvbo->valid_domains & |
78ad0f7b FJ |
236 | (write_domains ? write_domains : read_domains); |
237 | uint32_t pref_flags = 0, valid_flags = 0; | |
6ee73861 | 238 | |
78ad0f7b | 239 | if (!domains) |
6ee73861 BS |
240 | return -EINVAL; |
241 | ||
78ad0f7b FJ |
242 | if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) |
243 | valid_flags |= TTM_PL_FLAG_VRAM; | |
244 | ||
245 | 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); | |
6ee73861 | 263 | |
6ee73861 BS |
264 | return 0; |
265 | } | |
266 | ||
267 | struct validate_op { | |
6ee73861 BS |
268 | struct list_head vram_list; |
269 | struct list_head gart_list; | |
270 | struct list_head both_list; | |
271 | }; | |
272 | ||
273 | static void | |
274 | validate_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); | |
332b242f FJ |
281 | |
282 | nouveau_bo_fence(nvbo, fence); | |
6ee73861 | 283 | |
a1606a95 BS |
284 | if (unlikely(nvbo->validate_mapped)) { |
285 | ttm_bo_kunmap(&nvbo->kmap); | |
286 | nvbo->validate_mapped = false; | |
287 | } | |
288 | ||
6ee73861 BS |
289 | list_del(&nvbo->entry); |
290 | nvbo->reserved_by = NULL; | |
291 | ttm_bo_unreserve(&nvbo->bo); | |
374c3af8 | 292 | drm_gem_object_unreference_unlocked(nvbo->gem); |
6ee73861 BS |
293 | } |
294 | } | |
295 | ||
296 | static void | |
234896a7 | 297 | validate_fini(struct validate_op *op, struct nouveau_fence* fence) |
6ee73861 | 298 | { |
234896a7 LB |
299 | validate_fini_list(&op->vram_list, fence); |
300 | validate_fini_list(&op->gart_list, fence); | |
301 | validate_fini_list(&op->both_list, fence); | |
6ee73861 BS |
302 | } |
303 | ||
304 | static int | |
305 | validate_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); | |
316 | retry: | |
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); | |
bf79cb91 | 331 | return -ENOENT; |
6ee73861 BS |
332 | } |
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 | ||
938c40ed | 342 | ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence); |
6ee73861 BS |
343 | if (ret) { |
344 | validate_fini(op, NULL); | |
938c40ed BS |
345 | if (unlikely(ret == -EAGAIN)) |
346 | ret = ttm_bo_wait_unreserved(&nvbo->bo, true); | |
374c3af8 | 347 | drm_gem_object_unreference_unlocked(gem); |
938c40ed BS |
348 | if (unlikely(ret)) { |
349 | if (ret != -ERESTARTSYS) | |
350 | NV_ERROR(dev, "fail reserve\n"); | |
6ee73861 | 351 | return ret; |
a1606a95 | 352 | } |
6ee73861 BS |
353 | goto retry; |
354 | } | |
355 | ||
a1606a95 | 356 | b->user_priv = (uint64_t)(unsigned long)nvbo; |
6ee73861 BS |
357 | 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); | |
0208843d | 371 | list_add_tail(&nvbo->entry, &op->both_list); |
6ee73861 BS |
372 | validate_fini(op, NULL); |
373 | return -EINVAL; | |
374 | } | |
6ee73861 BS |
375 | } |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
380 | static int | |
381 | validate_list(struct nouveau_channel *chan, struct list_head *list, | |
382 | struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr) | |
383 | { | |
a3fcd0a9 | 384 | struct drm_nouveau_private *dev_priv = chan->dev->dev_private; |
6ee73861 BS |
385 | struct drm_nouveau_gem_pushbuf_bo __user *upbbo = |
386 | (void __force __user *)(uintptr_t)user_pbbo_ptr; | |
a1606a95 | 387 | struct drm_device *dev = chan->dev; |
6ee73861 BS |
388 | 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]; | |
6ee73861 | 393 | |
2730723b | 394 | ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan); |
415e6186 BS |
395 | if (unlikely(ret)) { |
396 | NV_ERROR(dev, "fail pre-validate sync\n"); | |
397 | return ret; | |
6ee73861 BS |
398 | } |
399 | ||
400 | ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains, | |
401 | b->write_domains, | |
402 | b->valid_domains); | |
a1606a95 BS |
403 | if (unlikely(ret)) { |
404 | NV_ERROR(dev, "fail set_domain\n"); | |
6ee73861 | 405 | return ret; |
a1606a95 | 406 | } |
6ee73861 | 407 | |
415e6186 | 408 | nvbo->channel = (b->read_domains & (1 << 31)) ? NULL : chan; |
7a45d764 | 409 | ret = nouveau_bo_validate(nvbo, true, false, false); |
6ee73861 | 410 | nvbo->channel = NULL; |
a1606a95 | 411 | if (unlikely(ret)) { |
938c40ed BS |
412 | if (ret != -ERESTARTSYS) |
413 | NV_ERROR(dev, "fail ttm_validate\n"); | |
6ee73861 | 414 | return ret; |
a1606a95 | 415 | } |
6ee73861 | 416 | |
2730723b | 417 | ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan); |
415e6186 BS |
418 | if (unlikely(ret)) { |
419 | NV_ERROR(dev, "fail post-validate sync\n"); | |
420 | return ret; | |
421 | } | |
422 | ||
a3fcd0a9 BS |
423 | 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; | |
430 | ||
431 | 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++; | |
438 | ||
439 | if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed, | |
440 | &b->presumed, sizeof(b->presumed))) | |
441 | return -EFAULT; | |
442 | } | |
6ee73861 BS |
443 | } |
444 | ||
445 | return relocs; | |
446 | } | |
447 | ||
448 | static int | |
449 | nouveau_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 | { | |
a1606a95 | 455 | struct drm_device *dev = chan->dev; |
6ee73861 BS |
456 | 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 | ||
6ee73861 BS |
462 | if (nr_buffers == 0) |
463 | return 0; | |
464 | ||
465 | ret = validate_init(chan, file_priv, pbbo, nr_buffers, op); | |
a1606a95 | 466 | if (unlikely(ret)) { |
938c40ed BS |
467 | if (ret != -ERESTARTSYS) |
468 | NV_ERROR(dev, "validate_init\n"); | |
6ee73861 | 469 | return ret; |
a1606a95 | 470 | } |
6ee73861 BS |
471 | |
472 | ret = validate_list(chan, &op->vram_list, pbbo, user_buffers); | |
473 | if (unlikely(ret < 0)) { | |
938c40ed BS |
474 | if (ret != -ERESTARTSYS) |
475 | NV_ERROR(dev, "validate vram_list\n"); | |
6ee73861 BS |
476 | 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)) { | |
938c40ed BS |
483 | if (ret != -ERESTARTSYS) |
484 | NV_ERROR(dev, "validate gart_list\n"); | |
6ee73861 BS |
485 | 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)) { | |
938c40ed BS |
492 | if (ret != -ERESTARTSYS) |
493 | NV_ERROR(dev, "validate both_list\n"); | |
6ee73861 BS |
494 | validate_fini(op, NULL); |
495 | return ret; | |
496 | } | |
497 | relocs += ret; | |
498 | ||
499 | *apply_relocs = relocs; | |
500 | return 0; | |
501 | } | |
502 | ||
503 | static inline void * | |
504 | u_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 | ||
521 | static int | |
a1606a95 BS |
522 | nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev, |
523 | struct drm_nouveau_gem_pushbuf *req, | |
524 | struct drm_nouveau_gem_pushbuf_bo *bo) | |
6ee73861 BS |
525 | { |
526 | struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; | |
12f735b7 LB |
527 | int ret = 0; |
528 | unsigned i; | |
6ee73861 | 529 | |
a1606a95 | 530 | reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); |
6ee73861 BS |
531 | if (IS_ERR(reloc)) |
532 | return PTR_ERR(reloc); | |
533 | ||
a1606a95 | 534 | for (i = 0; i < req->nr_relocs; i++) { |
6ee73861 BS |
535 | struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i]; |
536 | struct drm_nouveau_gem_pushbuf_bo *b; | |
a1606a95 | 537 | struct nouveau_bo *nvbo; |
6ee73861 BS |
538 | uint32_t data; |
539 | ||
a1606a95 BS |
540 | if (unlikely(r->bo_index > req->nr_buffers)) { |
541 | NV_ERROR(dev, "reloc bo index invalid\n"); | |
6ee73861 BS |
542 | ret = -EINVAL; |
543 | break; | |
544 | } | |
545 | ||
546 | b = &bo[r->bo_index]; | |
a1606a95 | 547 | if (b->presumed.valid) |
6ee73861 BS |
548 | continue; |
549 | ||
a1606a95 BS |
550 | 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 | ||
6ee73861 | 574 | if (r->flags & NOUVEAU_GEM_RELOC_LOW) |
a1606a95 | 575 | data = b->presumed.offset + r->data; |
6ee73861 BS |
576 | else |
577 | if (r->flags & NOUVEAU_GEM_RELOC_HIGH) | |
a1606a95 | 578 | data = (b->presumed.offset + r->data) >> 32; |
6ee73861 BS |
579 | else |
580 | data = r->data; | |
581 | ||
582 | if (r->flags & NOUVEAU_GEM_RELOC_OR) { | |
a1606a95 | 583 | if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART) |
6ee73861 BS |
584 | data |= r->tor; |
585 | else | |
586 | data |= r->vor; | |
587 | } | |
588 | ||
702adba2 | 589 | spin_lock(&nvbo->bo.bdev->fence_lock); |
a1606a95 | 590 | ret = ttm_bo_wait(&nvbo->bo, false, false, false); |
702adba2 | 591 | spin_unlock(&nvbo->bo.bdev->fence_lock); |
a1606a95 BS |
592 | if (ret) { |
593 | NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret); | |
594 | break; | |
595 | } | |
a1606a95 BS |
596 | |
597 | nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data); | |
6ee73861 BS |
598 | } |
599 | ||
600 | kfree(reloc); | |
601 | return ret; | |
602 | } | |
603 | ||
604 | int | |
605 | nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, | |
606 | struct drm_file *file_priv) | |
607 | { | |
a1606a95 | 608 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
6ee73861 | 609 | struct drm_nouveau_gem_pushbuf *req = data; |
a1606a95 BS |
610 | struct drm_nouveau_gem_pushbuf_push *push; |
611 | struct drm_nouveau_gem_pushbuf_bo *bo; | |
6ee73861 BS |
612 | struct nouveau_channel *chan; |
613 | struct validate_op op; | |
6e86e041 | 614 | struct nouveau_fence *fence = NULL; |
a1606a95 | 615 | int i, j, ret = 0, do_reloc = 0; |
6ee73861 | 616 | |
e8a863c1 | 617 | chan = nouveau_channel_get(file_priv, req->channel); |
cff5c133 BS |
618 | if (IS_ERR(chan)) |
619 | return PTR_ERR(chan); | |
6ee73861 | 620 | |
a1606a95 BS |
621 | 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; | |
6ee73861 | 625 | |
a1606a95 BS |
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); | |
cff5c133 | 629 | nouveau_channel_put(&chan); |
a1606a95 | 630 | return -EINVAL; |
6ee73861 BS |
631 | } |
632 | ||
a1606a95 BS |
633 | 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); | |
cff5c133 | 636 | nouveau_channel_put(&chan); |
a1606a95 | 637 | return -EINVAL; |
6ee73861 BS |
638 | } |
639 | ||
a1606a95 BS |
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); | |
cff5c133 | 643 | nouveau_channel_put(&chan); |
6ee73861 BS |
644 | return -EINVAL; |
645 | } | |
646 | ||
a1606a95 | 647 | push = u_memcpya(req->push, req->nr_push, sizeof(*push)); |
cff5c133 BS |
648 | if (IS_ERR(push)) { |
649 | nouveau_channel_put(&chan); | |
a1606a95 | 650 | return PTR_ERR(push); |
cff5c133 | 651 | } |
a1606a95 | 652 | |
6ee73861 | 653 | bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo)); |
a1606a95 BS |
654 | if (IS_ERR(bo)) { |
655 | kfree(push); | |
cff5c133 | 656 | nouveau_channel_put(&chan); |
6ee73861 | 657 | return PTR_ERR(bo); |
a1606a95 | 658 | } |
6ee73861 | 659 | |
415e6186 BS |
660 | /* 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; | |
7fa0cba2 | 669 | goto out_prevalid; |
415e6186 BS |
670 | } |
671 | ||
672 | bo[push[i].bo_index].read_domains |= (1 << 31); | |
673 | } | |
674 | ||
6ee73861 BS |
675 | /* Validate buffer list */ |
676 | ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers, | |
677 | req->nr_buffers, &op, &do_reloc); | |
678 | if (ret) { | |
938c40ed BS |
679 | if (ret != -ERESTARTSYS) |
680 | NV_ERROR(dev, "validate: %d\n", ret); | |
7fa0cba2 | 681 | goto out_prevalid; |
6ee73861 BS |
682 | } |
683 | ||
6ee73861 BS |
684 | /* Apply any relocations that are required */ |
685 | if (do_reloc) { | |
a1606a95 | 686 | ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo); |
6ee73861 | 687 | if (ret) { |
6ee73861 | 688 | NV_ERROR(dev, "reloc apply: %d\n", ret); |
6ee73861 BS |
689 | goto out; |
690 | } | |
6ee73861 | 691 | } |
6ee73861 | 692 | |
9a391ad8 | 693 | if (chan->dma.ib_max) { |
a1606a95 | 694 | ret = nouveau_dma_wait(chan, req->nr_push + 1, 6); |
6ee73861 | 695 | if (ret) { |
9a391ad8 | 696 | NV_INFO(dev, "nv50cal_space: %d\n", ret); |
6ee73861 BS |
697 | goto out; |
698 | } | |
6ee73861 | 699 | |
a1606a95 BS |
700 | 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 | } | |
9a391ad8 | 707 | } else |
ee508b82 | 708 | if (dev_priv->chipset >= 0x25) { |
a1606a95 | 709 | ret = RING_SPACE(chan, req->nr_push * 2); |
6ee73861 BS |
710 | if (ret) { |
711 | NV_ERROR(dev, "cal_space: %d\n", ret); | |
712 | goto out; | |
713 | } | |
a1606a95 BS |
714 | |
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 | } | |
6ee73861 | 724 | } else { |
a1606a95 | 725 | ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS)); |
6ee73861 BS |
726 | if (ret) { |
727 | NV_ERROR(dev, "jmp_space: %d\n", ret); | |
728 | goto out; | |
729 | } | |
6ee73861 | 730 | |
a1606a95 BS |
731 | 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); | |
6ee73861 | 758 | OUT_RING(chan, 0); |
a1606a95 BS |
759 | for (j = 0; j < NOUVEAU_DMA_SKIPS; j++) |
760 | OUT_RING(chan, 0); | |
761 | } | |
6ee73861 BS |
762 | } |
763 | ||
234896a7 | 764 | ret = nouveau_fence_new(chan, &fence, true); |
6ee73861 BS |
765 | if (ret) { |
766 | NV_ERROR(dev, "error fencing pushbuf: %d\n", ret); | |
767 | WIND_RING(chan); | |
768 | goto out; | |
769 | } | |
770 | ||
771 | out: | |
234896a7 | 772 | validate_fini(&op, fence); |
382d62e5 | 773 | nouveau_fence_unref(&fence); |
7fa0cba2 MS |
774 | |
775 | out_prevalid: | |
6ee73861 | 776 | kfree(bo); |
a1606a95 | 777 | kfree(push); |
6ee73861 BS |
778 | |
779 | out_next: | |
9a391ad8 BS |
780 | if (chan->dma.ib_max) { |
781 | req->suffix0 = 0x00000000; | |
782 | req->suffix1 = 0x00000000; | |
783 | } else | |
ee508b82 | 784 | if (dev_priv->chipset >= 0x25) { |
6ee73861 BS |
785 | 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 | ||
cff5c133 | 793 | nouveau_channel_put(&chan); |
6ee73861 BS |
794 | return ret; |
795 | } | |
796 | ||
6ee73861 BS |
797 | static inline uint32_t |
798 | domain_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 | ||
6ee73861 BS |
810 | int |
811 | nouveau_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 | ||
6ee73861 BS |
820 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
821 | if (!gem) | |
bf79cb91 | 822 | return -ENOENT; |
6ee73861 BS |
823 | nvbo = nouveau_gem_object(gem); |
824 | ||
21e86c1c BS |
825 | 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); | |
bc9025bd | 828 | drm_gem_object_unreference_unlocked(gem); |
6ee73861 BS |
829 | return ret; |
830 | } | |
831 | ||
832 | int | |
833 | nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data, | |
834 | struct drm_file *file_priv) | |
835 | { | |
21e86c1c | 836 | return 0; |
6ee73861 BS |
837 | } |
838 | ||
839 | int | |
840 | nouveau_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 | ||
6ee73861 BS |
847 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
848 | if (!gem) | |
bf79cb91 | 849 | return -ENOENT; |
6ee73861 | 850 | |
e758a311 | 851 | ret = nouveau_gem_info(file_priv, gem, req); |
bc9025bd | 852 | drm_gem_object_unreference_unlocked(gem); |
6ee73861 BS |
853 | return ret; |
854 | } | |
855 |