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); | |
66 | ||
67 | if (!fpriv->vm) | |
68 | return 0; | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | void | |
74 | nouveau_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 | ||
6ee73861 | 82 | int |
f6d4e621 BS |
83 | nouveau_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) | |
6ee73861 | 86 | { |
db5c8e29 | 87 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
6ee73861 | 88 | struct nouveau_bo *nvbo; |
6ba9a683 | 89 | u32 flags = 0; |
6ee73861 BS |
90 | int ret; |
91 | ||
6ba9a683 BS |
92 | 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 | ||
f6d4e621 | 99 | ret = nouveau_bo_new(dev, NULL, size, align, flags, tile_mode, |
d550c41e | 100 | tile_flags, pnvbo); |
6ee73861 BS |
101 | if (ret) |
102 | return ret; | |
103 | nvbo = *pnvbo; | |
104 | ||
db5c8e29 BS |
105 | /* 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 | ||
6ee73861 BS |
114 | 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 | ||
5df23979 | 120 | nvbo->bo.persistent_swap_storage = nvbo->gem->filp; |
6ee73861 BS |
121 | nvbo->gem->driver_private = nvbo; |
122 | return 0; | |
123 | } | |
124 | ||
125 | static int | |
126 | nouveau_gem_info(struct drm_gem_object *gem, struct drm_nouveau_gem_info *rep) | |
127 | { | |
07533ea5 | 128 | struct drm_nouveau_private *dev_priv = gem->dev->dev_private; |
6ee73861 BS |
129 | 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; | |
07533ea5 BS |
137 | if (dev_priv->card_type < NV_50) |
138 | rep->offset = nvbo->bo.offset; | |
139 | else | |
140 | rep->offset = nvbo->vma.offset; | |
d550c41e | 141 | rep->map_handle = nvbo->bo.addr_space_offset; |
6ee73861 BS |
142 | rep->tile_mode = nvbo->tile_mode; |
143 | rep->tile_flags = nvbo->tile_flags; | |
144 | return 0; | |
145 | } | |
146 | ||
6ee73861 BS |
147 | int |
148 | nouveau_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; | |
6ee73861 BS |
154 | int ret = 0; |
155 | ||
6ee73861 BS |
156 | if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL)) |
157 | dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping; | |
158 | ||
60d2a88a BS |
159 | 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); | |
6ee73861 | 161 | return -EINVAL; |
60d2a88a | 162 | } |
6ee73861 | 163 | |
f6d4e621 | 164 | ret = nouveau_gem_new(dev, req->info.size, req->align, |
6ba9a683 BS |
165 | req->info.domain, req->info.tile_mode, |
166 | req->info.tile_flags, &nvbo); | |
6ee73861 BS |
167 | 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); | |
29d08b3e DA |
175 | /* drop reference from allocate - handle holds it now */ |
176 | drm_gem_object_unreference_unlocked(nvbo->gem); | |
6ee73861 | 177 | out: |
6ee73861 BS |
178 | return ret; |
179 | } | |
180 | ||
181 | static int | |
182 | nouveau_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; | |
db5c8e29 | 187 | uint32_t domains = valid_domains & nvbo->valid_domains & |
78ad0f7b FJ |
188 | (write_domains ? write_domains : read_domains); |
189 | uint32_t pref_flags = 0, valid_flags = 0; | |
6ee73861 | 190 | |
78ad0f7b | 191 | if (!domains) |
6ee73861 BS |
192 | return -EINVAL; |
193 | ||
78ad0f7b FJ |
194 | if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) |
195 | valid_flags |= TTM_PL_FLAG_VRAM; | |
196 | ||
197 | 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); | |
6ee73861 | 215 | |
6ee73861 BS |
216 | return 0; |
217 | } | |
218 | ||
219 | struct validate_op { | |
6ee73861 BS |
220 | struct list_head vram_list; |
221 | struct list_head gart_list; | |
222 | struct list_head both_list; | |
223 | }; | |
224 | ||
225 | static void | |
226 | validate_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); | |
332b242f FJ |
233 | |
234 | nouveau_bo_fence(nvbo, fence); | |
6ee73861 | 235 | |
a1606a95 BS |
236 | if (unlikely(nvbo->validate_mapped)) { |
237 | ttm_bo_kunmap(&nvbo->kmap); | |
238 | nvbo->validate_mapped = false; | |
239 | } | |
240 | ||
6ee73861 BS |
241 | list_del(&nvbo->entry); |
242 | nvbo->reserved_by = NULL; | |
243 | ttm_bo_unreserve(&nvbo->bo); | |
374c3af8 | 244 | drm_gem_object_unreference_unlocked(nvbo->gem); |
6ee73861 BS |
245 | } |
246 | } | |
247 | ||
248 | static void | |
234896a7 | 249 | validate_fini(struct validate_op *op, struct nouveau_fence* fence) |
6ee73861 | 250 | { |
234896a7 LB |
251 | validate_fini_list(&op->vram_list, fence); |
252 | validate_fini_list(&op->gart_list, fence); | |
253 | validate_fini_list(&op->both_list, fence); | |
6ee73861 BS |
254 | } |
255 | ||
256 | static int | |
257 | validate_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); | |
268 | retry: | |
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); | |
bf79cb91 | 283 | return -ENOENT; |
6ee73861 BS |
284 | } |
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 | ||
938c40ed | 294 | ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence); |
6ee73861 BS |
295 | if (ret) { |
296 | validate_fini(op, NULL); | |
938c40ed BS |
297 | if (unlikely(ret == -EAGAIN)) |
298 | ret = ttm_bo_wait_unreserved(&nvbo->bo, true); | |
374c3af8 | 299 | drm_gem_object_unreference_unlocked(gem); |
938c40ed BS |
300 | if (unlikely(ret)) { |
301 | if (ret != -ERESTARTSYS) | |
302 | NV_ERROR(dev, "fail reserve\n"); | |
6ee73861 | 303 | return ret; |
a1606a95 | 304 | } |
6ee73861 BS |
305 | goto retry; |
306 | } | |
307 | ||
a1606a95 | 308 | b->user_priv = (uint64_t)(unsigned long)nvbo; |
6ee73861 BS |
309 | 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); | |
0208843d | 323 | list_add_tail(&nvbo->entry, &op->both_list); |
6ee73861 BS |
324 | validate_fini(op, NULL); |
325 | return -EINVAL; | |
326 | } | |
6ee73861 BS |
327 | } |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
332 | static int | |
333 | validate_list(struct nouveau_channel *chan, struct list_head *list, | |
334 | struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr) | |
335 | { | |
a3fcd0a9 | 336 | struct drm_nouveau_private *dev_priv = chan->dev->dev_private; |
6ee73861 BS |
337 | struct drm_nouveau_gem_pushbuf_bo __user *upbbo = |
338 | (void __force __user *)(uintptr_t)user_pbbo_ptr; | |
a1606a95 | 339 | struct drm_device *dev = chan->dev; |
6ee73861 BS |
340 | 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]; | |
6ee73861 | 345 | |
2730723b | 346 | ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan); |
415e6186 BS |
347 | if (unlikely(ret)) { |
348 | NV_ERROR(dev, "fail pre-validate sync\n"); | |
349 | return ret; | |
6ee73861 BS |
350 | } |
351 | ||
352 | ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains, | |
353 | b->write_domains, | |
354 | b->valid_domains); | |
a1606a95 BS |
355 | if (unlikely(ret)) { |
356 | NV_ERROR(dev, "fail set_domain\n"); | |
6ee73861 | 357 | return ret; |
a1606a95 | 358 | } |
6ee73861 | 359 | |
415e6186 | 360 | nvbo->channel = (b->read_domains & (1 << 31)) ? NULL : chan; |
7a45d764 | 361 | ret = nouveau_bo_validate(nvbo, true, false, false); |
6ee73861 | 362 | nvbo->channel = NULL; |
a1606a95 | 363 | if (unlikely(ret)) { |
938c40ed BS |
364 | if (ret != -ERESTARTSYS) |
365 | NV_ERROR(dev, "fail ttm_validate\n"); | |
6ee73861 | 366 | return ret; |
a1606a95 | 367 | } |
6ee73861 | 368 | |
2730723b | 369 | ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan); |
415e6186 BS |
370 | if (unlikely(ret)) { |
371 | NV_ERROR(dev, "fail post-validate sync\n"); | |
372 | return ret; | |
373 | } | |
374 | ||
a3fcd0a9 BS |
375 | 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; | |
382 | ||
383 | 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++; | |
390 | ||
391 | if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed, | |
392 | &b->presumed, sizeof(b->presumed))) | |
393 | return -EFAULT; | |
394 | } | |
6ee73861 BS |
395 | } |
396 | ||
397 | return relocs; | |
398 | } | |
399 | ||
400 | static int | |
401 | nouveau_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 | { | |
a1606a95 | 407 | struct drm_device *dev = chan->dev; |
6ee73861 BS |
408 | 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 | ||
6ee73861 BS |
414 | if (nr_buffers == 0) |
415 | return 0; | |
416 | ||
417 | ret = validate_init(chan, file_priv, pbbo, nr_buffers, op); | |
a1606a95 | 418 | if (unlikely(ret)) { |
938c40ed BS |
419 | if (ret != -ERESTARTSYS) |
420 | NV_ERROR(dev, "validate_init\n"); | |
6ee73861 | 421 | return ret; |
a1606a95 | 422 | } |
6ee73861 BS |
423 | |
424 | ret = validate_list(chan, &op->vram_list, pbbo, user_buffers); | |
425 | if (unlikely(ret < 0)) { | |
938c40ed BS |
426 | if (ret != -ERESTARTSYS) |
427 | NV_ERROR(dev, "validate vram_list\n"); | |
6ee73861 BS |
428 | 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)) { | |
938c40ed BS |
435 | if (ret != -ERESTARTSYS) |
436 | NV_ERROR(dev, "validate gart_list\n"); | |
6ee73861 BS |
437 | 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)) { | |
938c40ed BS |
444 | if (ret != -ERESTARTSYS) |
445 | NV_ERROR(dev, "validate both_list\n"); | |
6ee73861 BS |
446 | validate_fini(op, NULL); |
447 | return ret; | |
448 | } | |
449 | relocs += ret; | |
450 | ||
451 | *apply_relocs = relocs; | |
452 | return 0; | |
453 | } | |
454 | ||
455 | static inline void * | |
456 | u_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 | ||
473 | static int | |
a1606a95 BS |
474 | nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev, |
475 | struct drm_nouveau_gem_pushbuf *req, | |
476 | struct drm_nouveau_gem_pushbuf_bo *bo) | |
6ee73861 BS |
477 | { |
478 | struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; | |
12f735b7 LB |
479 | int ret = 0; |
480 | unsigned i; | |
6ee73861 | 481 | |
a1606a95 | 482 | reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); |
6ee73861 BS |
483 | if (IS_ERR(reloc)) |
484 | return PTR_ERR(reloc); | |
485 | ||
a1606a95 | 486 | for (i = 0; i < req->nr_relocs; i++) { |
6ee73861 BS |
487 | struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i]; |
488 | struct drm_nouveau_gem_pushbuf_bo *b; | |
a1606a95 | 489 | struct nouveau_bo *nvbo; |
6ee73861 BS |
490 | uint32_t data; |
491 | ||
a1606a95 BS |
492 | if (unlikely(r->bo_index > req->nr_buffers)) { |
493 | NV_ERROR(dev, "reloc bo index invalid\n"); | |
6ee73861 BS |
494 | ret = -EINVAL; |
495 | break; | |
496 | } | |
497 | ||
498 | b = &bo[r->bo_index]; | |
a1606a95 | 499 | if (b->presumed.valid) |
6ee73861 BS |
500 | continue; |
501 | ||
a1606a95 BS |
502 | 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 | ||
6ee73861 | 526 | if (r->flags & NOUVEAU_GEM_RELOC_LOW) |
a1606a95 | 527 | data = b->presumed.offset + r->data; |
6ee73861 BS |
528 | else |
529 | if (r->flags & NOUVEAU_GEM_RELOC_HIGH) | |
a1606a95 | 530 | data = (b->presumed.offset + r->data) >> 32; |
6ee73861 BS |
531 | else |
532 | data = r->data; | |
533 | ||
534 | if (r->flags & NOUVEAU_GEM_RELOC_OR) { | |
a1606a95 | 535 | if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART) |
6ee73861 BS |
536 | data |= r->tor; |
537 | else | |
538 | data |= r->vor; | |
539 | } | |
540 | ||
702adba2 | 541 | spin_lock(&nvbo->bo.bdev->fence_lock); |
a1606a95 | 542 | ret = ttm_bo_wait(&nvbo->bo, false, false, false); |
702adba2 | 543 | spin_unlock(&nvbo->bo.bdev->fence_lock); |
a1606a95 BS |
544 | if (ret) { |
545 | NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret); | |
546 | break; | |
547 | } | |
a1606a95 BS |
548 | |
549 | nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data); | |
6ee73861 BS |
550 | } |
551 | ||
552 | kfree(reloc); | |
553 | return ret; | |
554 | } | |
555 | ||
556 | int | |
557 | nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, | |
558 | struct drm_file *file_priv) | |
559 | { | |
a1606a95 | 560 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
6ee73861 | 561 | struct drm_nouveau_gem_pushbuf *req = data; |
a1606a95 BS |
562 | struct drm_nouveau_gem_pushbuf_push *push; |
563 | struct drm_nouveau_gem_pushbuf_bo *bo; | |
6ee73861 BS |
564 | struct nouveau_channel *chan; |
565 | struct validate_op op; | |
6e86e041 | 566 | struct nouveau_fence *fence = NULL; |
a1606a95 | 567 | int i, j, ret = 0, do_reloc = 0; |
6ee73861 | 568 | |
e8a863c1 | 569 | chan = nouveau_channel_get(file_priv, req->channel); |
cff5c133 BS |
570 | if (IS_ERR(chan)) |
571 | return PTR_ERR(chan); | |
6ee73861 | 572 | |
a1606a95 BS |
573 | 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; | |
6ee73861 | 577 | |
a1606a95 BS |
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); | |
cff5c133 | 581 | nouveau_channel_put(&chan); |
a1606a95 | 582 | return -EINVAL; |
6ee73861 BS |
583 | } |
584 | ||
a1606a95 BS |
585 | 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); | |
cff5c133 | 588 | nouveau_channel_put(&chan); |
a1606a95 | 589 | return -EINVAL; |
6ee73861 BS |
590 | } |
591 | ||
a1606a95 BS |
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); | |
cff5c133 | 595 | nouveau_channel_put(&chan); |
6ee73861 BS |
596 | return -EINVAL; |
597 | } | |
598 | ||
a1606a95 | 599 | push = u_memcpya(req->push, req->nr_push, sizeof(*push)); |
cff5c133 BS |
600 | if (IS_ERR(push)) { |
601 | nouveau_channel_put(&chan); | |
a1606a95 | 602 | return PTR_ERR(push); |
cff5c133 | 603 | } |
a1606a95 | 604 | |
6ee73861 | 605 | bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo)); |
a1606a95 BS |
606 | if (IS_ERR(bo)) { |
607 | kfree(push); | |
cff5c133 | 608 | nouveau_channel_put(&chan); |
6ee73861 | 609 | return PTR_ERR(bo); |
a1606a95 | 610 | } |
6ee73861 | 611 | |
415e6186 BS |
612 | /* 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; | |
7fa0cba2 | 621 | goto out_prevalid; |
415e6186 BS |
622 | } |
623 | ||
624 | bo[push[i].bo_index].read_domains |= (1 << 31); | |
625 | } | |
626 | ||
6ee73861 BS |
627 | /* Validate buffer list */ |
628 | ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers, | |
629 | req->nr_buffers, &op, &do_reloc); | |
630 | if (ret) { | |
938c40ed BS |
631 | if (ret != -ERESTARTSYS) |
632 | NV_ERROR(dev, "validate: %d\n", ret); | |
7fa0cba2 | 633 | goto out_prevalid; |
6ee73861 BS |
634 | } |
635 | ||
6ee73861 BS |
636 | /* Apply any relocations that are required */ |
637 | if (do_reloc) { | |
a1606a95 | 638 | ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo); |
6ee73861 | 639 | if (ret) { |
6ee73861 | 640 | NV_ERROR(dev, "reloc apply: %d\n", ret); |
6ee73861 BS |
641 | goto out; |
642 | } | |
6ee73861 | 643 | } |
6ee73861 | 644 | |
9a391ad8 | 645 | if (chan->dma.ib_max) { |
a1606a95 | 646 | ret = nouveau_dma_wait(chan, req->nr_push + 1, 6); |
6ee73861 | 647 | if (ret) { |
9a391ad8 | 648 | NV_INFO(dev, "nv50cal_space: %d\n", ret); |
6ee73861 BS |
649 | goto out; |
650 | } | |
6ee73861 | 651 | |
a1606a95 BS |
652 | 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 | } | |
9a391ad8 | 659 | } else |
ee508b82 | 660 | if (dev_priv->chipset >= 0x25) { |
a1606a95 | 661 | ret = RING_SPACE(chan, req->nr_push * 2); |
6ee73861 BS |
662 | if (ret) { |
663 | NV_ERROR(dev, "cal_space: %d\n", ret); | |
664 | goto out; | |
665 | } | |
a1606a95 BS |
666 | |
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 | } | |
6ee73861 | 676 | } else { |
a1606a95 | 677 | ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS)); |
6ee73861 BS |
678 | if (ret) { |
679 | NV_ERROR(dev, "jmp_space: %d\n", ret); | |
680 | goto out; | |
681 | } | |
6ee73861 | 682 | |
a1606a95 BS |
683 | 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); | |
6ee73861 | 710 | OUT_RING(chan, 0); |
a1606a95 BS |
711 | for (j = 0; j < NOUVEAU_DMA_SKIPS; j++) |
712 | OUT_RING(chan, 0); | |
713 | } | |
6ee73861 BS |
714 | } |
715 | ||
234896a7 | 716 | ret = nouveau_fence_new(chan, &fence, true); |
6ee73861 BS |
717 | if (ret) { |
718 | NV_ERROR(dev, "error fencing pushbuf: %d\n", ret); | |
719 | WIND_RING(chan); | |
720 | goto out; | |
721 | } | |
722 | ||
723 | out: | |
234896a7 | 724 | validate_fini(&op, fence); |
382d62e5 | 725 | nouveau_fence_unref(&fence); |
7fa0cba2 MS |
726 | |
727 | out_prevalid: | |
6ee73861 | 728 | kfree(bo); |
a1606a95 | 729 | kfree(push); |
6ee73861 BS |
730 | |
731 | out_next: | |
9a391ad8 BS |
732 | if (chan->dma.ib_max) { |
733 | req->suffix0 = 0x00000000; | |
734 | req->suffix1 = 0x00000000; | |
735 | } else | |
ee508b82 | 736 | if (dev_priv->chipset >= 0x25) { |
6ee73861 BS |
737 | 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 | ||
cff5c133 | 745 | nouveau_channel_put(&chan); |
6ee73861 BS |
746 | return ret; |
747 | } | |
748 | ||
6ee73861 BS |
749 | static inline uint32_t |
750 | domain_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 | ||
6ee73861 BS |
762 | int |
763 | nouveau_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 | ||
6ee73861 BS |
772 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
773 | if (!gem) | |
bf79cb91 | 774 | return -ENOENT; |
6ee73861 BS |
775 | nvbo = nouveau_gem_object(gem); |
776 | ||
21e86c1c BS |
777 | 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); | |
bc9025bd | 780 | drm_gem_object_unreference_unlocked(gem); |
6ee73861 BS |
781 | return ret; |
782 | } | |
783 | ||
784 | int | |
785 | nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data, | |
786 | struct drm_file *file_priv) | |
787 | { | |
21e86c1c | 788 | return 0; |
6ee73861 BS |
789 | } |
790 | ||
791 | int | |
792 | nouveau_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 | ||
6ee73861 BS |
799 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
800 | if (!gem) | |
bf79cb91 | 801 | return -ENOENT; |
6ee73861 BS |
802 | |
803 | ret = nouveau_gem_info(gem, req); | |
bc9025bd | 804 | drm_gem_object_unreference_unlocked(gem); |
6ee73861 BS |
805 | return ret; |
806 | } | |
807 |