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 | ||
51 | if (unlikely(nvbo->cpu_filp)) | |
52 | ttm_bo_synccpu_write_release(bo); | |
53 | ||
54 | if (unlikely(nvbo->pin_refcnt)) { | |
55 | nvbo->pin_refcnt = 1; | |
56 | nouveau_bo_unpin(nvbo); | |
57 | } | |
58 | ||
59 | ttm_bo_unref(&bo); | |
60 | } | |
61 | ||
62 | int | |
63 | nouveau_gem_new(struct drm_device *dev, struct nouveau_channel *chan, | |
64 | int size, int align, uint32_t flags, uint32_t tile_mode, | |
65 | uint32_t tile_flags, bool no_vm, bool mappable, | |
66 | struct nouveau_bo **pnvbo) | |
67 | { | |
68 | struct nouveau_bo *nvbo; | |
69 | int ret; | |
70 | ||
71 | ret = nouveau_bo_new(dev, chan, size, align, flags, tile_mode, | |
72 | tile_flags, no_vm, mappable, pnvbo); | |
73 | if (ret) | |
74 | return ret; | |
75 | nvbo = *pnvbo; | |
76 | ||
77 | nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size); | |
78 | if (!nvbo->gem) { | |
79 | nouveau_bo_ref(NULL, pnvbo); | |
80 | return -ENOMEM; | |
81 | } | |
82 | ||
83 | nvbo->bo.persistant_swap_storage = nvbo->gem->filp; | |
84 | nvbo->gem->driver_private = nvbo; | |
85 | return 0; | |
86 | } | |
87 | ||
88 | static int | |
89 | nouveau_gem_info(struct drm_gem_object *gem, struct drm_nouveau_gem_info *rep) | |
90 | { | |
91 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); | |
92 | ||
93 | if (nvbo->bo.mem.mem_type == TTM_PL_TT) | |
94 | rep->domain = NOUVEAU_GEM_DOMAIN_GART; | |
95 | else | |
96 | rep->domain = NOUVEAU_GEM_DOMAIN_VRAM; | |
97 | ||
98 | rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT; | |
99 | rep->offset = nvbo->bo.offset; | |
100 | rep->map_handle = nvbo->mappable ? nvbo->bo.addr_space_offset : 0; | |
101 | rep->tile_mode = nvbo->tile_mode; | |
102 | rep->tile_flags = nvbo->tile_flags; | |
103 | return 0; | |
104 | } | |
105 | ||
106 | static bool | |
107 | nouveau_gem_tile_flags_valid(struct drm_device *dev, uint32_t tile_flags) { | |
108 | switch (tile_flags) { | |
109 | case 0x0000: | |
110 | case 0x1800: | |
111 | case 0x2800: | |
112 | case 0x4800: | |
113 | case 0x7000: | |
114 | case 0x7400: | |
115 | case 0x7a00: | |
116 | case 0xe000: | |
117 | break; | |
118 | default: | |
119 | NV_ERROR(dev, "bad page flags: 0x%08x\n", tile_flags); | |
120 | return false; | |
121 | } | |
122 | ||
123 | return true; | |
124 | } | |
125 | ||
126 | int | |
127 | nouveau_gem_ioctl_new(struct drm_device *dev, void *data, | |
128 | struct drm_file *file_priv) | |
129 | { | |
130 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
131 | struct drm_nouveau_gem_new *req = data; | |
132 | struct nouveau_bo *nvbo = NULL; | |
133 | struct nouveau_channel *chan = NULL; | |
134 | uint32_t flags = 0; | |
135 | int ret = 0; | |
136 | ||
137 | NOUVEAU_CHECK_INITIALISED_WITH_RETURN; | |
138 | ||
139 | if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL)) | |
140 | dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping; | |
141 | ||
142 | if (req->channel_hint) { | |
143 | NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel_hint, | |
144 | file_priv, chan); | |
145 | } | |
146 | ||
147 | if (req->info.domain & NOUVEAU_GEM_DOMAIN_VRAM) | |
148 | flags |= TTM_PL_FLAG_VRAM; | |
149 | if (req->info.domain & NOUVEAU_GEM_DOMAIN_GART) | |
150 | flags |= TTM_PL_FLAG_TT; | |
151 | if (!flags || req->info.domain & NOUVEAU_GEM_DOMAIN_CPU) | |
152 | flags |= TTM_PL_FLAG_SYSTEM; | |
153 | ||
154 | if (!nouveau_gem_tile_flags_valid(dev, req->info.tile_flags)) | |
155 | return -EINVAL; | |
156 | ||
157 | ret = nouveau_gem_new(dev, chan, req->info.size, req->align, flags, | |
158 | req->info.tile_mode, req->info.tile_flags, false, | |
159 | (req->info.domain & NOUVEAU_GEM_DOMAIN_MAPPABLE), | |
160 | &nvbo); | |
161 | if (ret) | |
162 | return ret; | |
163 | ||
164 | ret = nouveau_gem_info(nvbo->gem, &req->info); | |
165 | if (ret) | |
166 | goto out; | |
167 | ||
168 | ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle); | |
169 | out: | |
bc9025bd | 170 | drm_gem_object_handle_unreference_unlocked(nvbo->gem); |
6ee73861 BS |
171 | |
172 | if (ret) | |
bc9025bd | 173 | drm_gem_object_unreference_unlocked(nvbo->gem); |
6ee73861 BS |
174 | return ret; |
175 | } | |
176 | ||
177 | static int | |
178 | nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains, | |
179 | uint32_t write_domains, uint32_t valid_domains) | |
180 | { | |
181 | struct nouveau_bo *nvbo = gem->driver_private; | |
182 | struct ttm_buffer_object *bo = &nvbo->bo; | |
78ad0f7b FJ |
183 | uint32_t domains = valid_domains & |
184 | (write_domains ? write_domains : read_domains); | |
185 | uint32_t pref_flags = 0, valid_flags = 0; | |
6ee73861 | 186 | |
78ad0f7b | 187 | if (!domains) |
6ee73861 BS |
188 | return -EINVAL; |
189 | ||
78ad0f7b FJ |
190 | if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) |
191 | valid_flags |= TTM_PL_FLAG_VRAM; | |
192 | ||
193 | if (valid_domains & NOUVEAU_GEM_DOMAIN_GART) | |
194 | valid_flags |= TTM_PL_FLAG_TT; | |
195 | ||
196 | if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) && | |
197 | bo->mem.mem_type == TTM_PL_VRAM) | |
198 | pref_flags |= TTM_PL_FLAG_VRAM; | |
199 | ||
200 | else if ((domains & NOUVEAU_GEM_DOMAIN_GART) && | |
201 | bo->mem.mem_type == TTM_PL_TT) | |
202 | pref_flags |= TTM_PL_FLAG_TT; | |
203 | ||
204 | else if (domains & NOUVEAU_GEM_DOMAIN_VRAM) | |
205 | pref_flags |= TTM_PL_FLAG_VRAM; | |
206 | ||
207 | else | |
208 | pref_flags |= TTM_PL_FLAG_TT; | |
209 | ||
210 | nouveau_bo_placement_set(nvbo, pref_flags, valid_flags); | |
6ee73861 | 211 | |
6ee73861 BS |
212 | return 0; |
213 | } | |
214 | ||
215 | struct validate_op { | |
6ee73861 BS |
216 | struct list_head vram_list; |
217 | struct list_head gart_list; | |
218 | struct list_head both_list; | |
219 | }; | |
220 | ||
221 | static void | |
222 | validate_fini_list(struct list_head *list, struct nouveau_fence *fence) | |
223 | { | |
224 | struct list_head *entry, *tmp; | |
225 | struct nouveau_bo *nvbo; | |
226 | ||
227 | list_for_each_safe(entry, tmp, list) { | |
228 | nvbo = list_entry(entry, struct nouveau_bo, entry); | |
229 | if (likely(fence)) { | |
230 | struct nouveau_fence *prev_fence; | |
231 | ||
232 | spin_lock(&nvbo->bo.lock); | |
233 | prev_fence = nvbo->bo.sync_obj; | |
234 | nvbo->bo.sync_obj = nouveau_fence_ref(fence); | |
235 | spin_unlock(&nvbo->bo.lock); | |
236 | nouveau_fence_unref((void *)&prev_fence); | |
237 | } | |
238 | ||
a1606a95 BS |
239 | if (unlikely(nvbo->validate_mapped)) { |
240 | ttm_bo_kunmap(&nvbo->kmap); | |
241 | nvbo->validate_mapped = false; | |
242 | } | |
243 | ||
6ee73861 BS |
244 | list_del(&nvbo->entry); |
245 | nvbo->reserved_by = NULL; | |
246 | ttm_bo_unreserve(&nvbo->bo); | |
247 | drm_gem_object_unreference(nvbo->gem); | |
248 | } | |
249 | } | |
250 | ||
251 | static void | |
234896a7 | 252 | validate_fini(struct validate_op *op, struct nouveau_fence* fence) |
6ee73861 | 253 | { |
234896a7 LB |
254 | validate_fini_list(&op->vram_list, fence); |
255 | validate_fini_list(&op->gart_list, fence); | |
256 | validate_fini_list(&op->both_list, fence); | |
6ee73861 BS |
257 | } |
258 | ||
259 | static int | |
260 | validate_init(struct nouveau_channel *chan, struct drm_file *file_priv, | |
261 | struct drm_nouveau_gem_pushbuf_bo *pbbo, | |
262 | int nr_buffers, struct validate_op *op) | |
263 | { | |
264 | struct drm_device *dev = chan->dev; | |
265 | struct drm_nouveau_private *dev_priv = dev->dev_private; | |
266 | uint32_t sequence; | |
267 | int trycnt = 0; | |
268 | int ret, i; | |
269 | ||
270 | sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence); | |
271 | retry: | |
272 | if (++trycnt > 100000) { | |
273 | NV_ERROR(dev, "%s failed and gave up.\n", __func__); | |
274 | return -EINVAL; | |
275 | } | |
276 | ||
277 | for (i = 0; i < nr_buffers; i++) { | |
278 | struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i]; | |
279 | struct drm_gem_object *gem; | |
280 | struct nouveau_bo *nvbo; | |
281 | ||
282 | gem = drm_gem_object_lookup(dev, file_priv, b->handle); | |
283 | if (!gem) { | |
284 | NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle); | |
285 | validate_fini(op, NULL); | |
286 | return -EINVAL; | |
287 | } | |
288 | nvbo = gem->driver_private; | |
289 | ||
290 | if (nvbo->reserved_by && nvbo->reserved_by == file_priv) { | |
291 | NV_ERROR(dev, "multiple instances of buffer %d on " | |
292 | "validation list\n", b->handle); | |
293 | validate_fini(op, NULL); | |
294 | return -EINVAL; | |
295 | } | |
296 | ||
297 | ret = ttm_bo_reserve(&nvbo->bo, false, false, true, sequence); | |
298 | if (ret) { | |
299 | validate_fini(op, NULL); | |
300 | if (ret == -EAGAIN) | |
301 | ret = ttm_bo_wait_unreserved(&nvbo->bo, false); | |
302 | drm_gem_object_unreference(gem); | |
a1606a95 BS |
303 | if (ret) { |
304 | NV_ERROR(dev, "fail reserve\n"); | |
6ee73861 | 305 | return ret; |
a1606a95 | 306 | } |
6ee73861 BS |
307 | goto retry; |
308 | } | |
309 | ||
a1606a95 | 310 | b->user_priv = (uint64_t)(unsigned long)nvbo; |
6ee73861 BS |
311 | nvbo->reserved_by = file_priv; |
312 | nvbo->pbbo_index = i; | |
313 | if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) && | |
314 | (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)) | |
315 | list_add_tail(&nvbo->entry, &op->both_list); | |
316 | else | |
317 | if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) | |
318 | list_add_tail(&nvbo->entry, &op->vram_list); | |
319 | else | |
320 | if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART) | |
321 | list_add_tail(&nvbo->entry, &op->gart_list); | |
322 | else { | |
323 | NV_ERROR(dev, "invalid valid domains: 0x%08x\n", | |
324 | b->valid_domains); | |
0208843d | 325 | list_add_tail(&nvbo->entry, &op->both_list); |
6ee73861 BS |
326 | validate_fini(op, NULL); |
327 | return -EINVAL; | |
328 | } | |
329 | ||
330 | if (unlikely(atomic_read(&nvbo->bo.cpu_writers) > 0)) { | |
331 | validate_fini(op, NULL); | |
332 | ||
333 | if (nvbo->cpu_filp == file_priv) { | |
334 | NV_ERROR(dev, "bo %p mapped by process trying " | |
335 | "to validate it!\n", nvbo); | |
336 | return -EINVAL; | |
337 | } | |
338 | ||
339 | ret = ttm_bo_wait_cpu(&nvbo->bo, false); | |
a1606a95 BS |
340 | if (ret) { |
341 | NV_ERROR(dev, "fail wait_cpu\n"); | |
6ee73861 | 342 | return ret; |
a1606a95 | 343 | } |
6ee73861 BS |
344 | goto retry; |
345 | } | |
346 | } | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | static int | |
352 | validate_list(struct nouveau_channel *chan, struct list_head *list, | |
353 | struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr) | |
354 | { | |
355 | struct drm_nouveau_gem_pushbuf_bo __user *upbbo = | |
356 | (void __force __user *)(uintptr_t)user_pbbo_ptr; | |
a1606a95 | 357 | struct drm_device *dev = chan->dev; |
6ee73861 BS |
358 | struct nouveau_bo *nvbo; |
359 | int ret, relocs = 0; | |
360 | ||
361 | list_for_each_entry(nvbo, list, entry) { | |
362 | struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index]; | |
363 | struct nouveau_fence *prev_fence = nvbo->bo.sync_obj; | |
364 | ||
365 | if (prev_fence && nouveau_fence_channel(prev_fence) != chan) { | |
366 | spin_lock(&nvbo->bo.lock); | |
367 | ret = ttm_bo_wait(&nvbo->bo, false, false, false); | |
368 | spin_unlock(&nvbo->bo.lock); | |
a1606a95 BS |
369 | if (unlikely(ret)) { |
370 | NV_ERROR(dev, "fail wait other chan\n"); | |
6ee73861 | 371 | return ret; |
a1606a95 | 372 | } |
6ee73861 BS |
373 | } |
374 | ||
375 | ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains, | |
376 | b->write_domains, | |
377 | b->valid_domains); | |
a1606a95 BS |
378 | if (unlikely(ret)) { |
379 | NV_ERROR(dev, "fail set_domain\n"); | |
6ee73861 | 380 | return ret; |
a1606a95 | 381 | } |
6ee73861 BS |
382 | |
383 | nvbo->channel = chan; | |
384 | ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, | |
385 | false, false); | |
386 | nvbo->channel = NULL; | |
a1606a95 BS |
387 | if (unlikely(ret)) { |
388 | NV_ERROR(dev, "fail ttm_validate\n"); | |
6ee73861 | 389 | return ret; |
a1606a95 | 390 | } |
6ee73861 | 391 | |
a1606a95 | 392 | if (nvbo->bo.offset == b->presumed.offset && |
6ee73861 | 393 | ((nvbo->bo.mem.mem_type == TTM_PL_VRAM && |
a1606a95 | 394 | b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) || |
6ee73861 | 395 | (nvbo->bo.mem.mem_type == TTM_PL_TT && |
a1606a95 | 396 | b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART))) |
6ee73861 BS |
397 | continue; |
398 | ||
399 | if (nvbo->bo.mem.mem_type == TTM_PL_TT) | |
a1606a95 | 400 | b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART; |
6ee73861 | 401 | else |
a1606a95 BS |
402 | b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM; |
403 | b->presumed.offset = nvbo->bo.offset; | |
404 | b->presumed.valid = 0; | |
6ee73861 BS |
405 | relocs++; |
406 | ||
a1606a95 BS |
407 | if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed, |
408 | &b->presumed, sizeof(b->presumed))) | |
6ee73861 BS |
409 | return -EFAULT; |
410 | } | |
411 | ||
412 | return relocs; | |
413 | } | |
414 | ||
415 | static int | |
416 | nouveau_gem_pushbuf_validate(struct nouveau_channel *chan, | |
417 | struct drm_file *file_priv, | |
418 | struct drm_nouveau_gem_pushbuf_bo *pbbo, | |
419 | uint64_t user_buffers, int nr_buffers, | |
420 | struct validate_op *op, int *apply_relocs) | |
421 | { | |
a1606a95 | 422 | struct drm_device *dev = chan->dev; |
6ee73861 BS |
423 | int ret, relocs = 0; |
424 | ||
425 | INIT_LIST_HEAD(&op->vram_list); | |
426 | INIT_LIST_HEAD(&op->gart_list); | |
427 | INIT_LIST_HEAD(&op->both_list); | |
428 | ||
6ee73861 BS |
429 | if (nr_buffers == 0) |
430 | return 0; | |
431 | ||
432 | ret = validate_init(chan, file_priv, pbbo, nr_buffers, op); | |
a1606a95 BS |
433 | if (unlikely(ret)) { |
434 | NV_ERROR(dev, "validate_init\n"); | |
6ee73861 | 435 | return ret; |
a1606a95 | 436 | } |
6ee73861 BS |
437 | |
438 | ret = validate_list(chan, &op->vram_list, pbbo, user_buffers); | |
439 | if (unlikely(ret < 0)) { | |
a1606a95 | 440 | NV_ERROR(dev, "validate vram_list\n"); |
6ee73861 BS |
441 | validate_fini(op, NULL); |
442 | return ret; | |
443 | } | |
444 | relocs += ret; | |
445 | ||
446 | ret = validate_list(chan, &op->gart_list, pbbo, user_buffers); | |
447 | if (unlikely(ret < 0)) { | |
a1606a95 | 448 | NV_ERROR(dev, "validate gart_list\n"); |
6ee73861 BS |
449 | validate_fini(op, NULL); |
450 | return ret; | |
451 | } | |
452 | relocs += ret; | |
453 | ||
454 | ret = validate_list(chan, &op->both_list, pbbo, user_buffers); | |
455 | if (unlikely(ret < 0)) { | |
a1606a95 | 456 | NV_ERROR(dev, "validate both_list\n"); |
6ee73861 BS |
457 | validate_fini(op, NULL); |
458 | return ret; | |
459 | } | |
460 | relocs += ret; | |
461 | ||
462 | *apply_relocs = relocs; | |
463 | return 0; | |
464 | } | |
465 | ||
466 | static inline void * | |
467 | u_memcpya(uint64_t user, unsigned nmemb, unsigned size) | |
468 | { | |
469 | void *mem; | |
470 | void __user *userptr = (void __force __user *)(uintptr_t)user; | |
471 | ||
472 | mem = kmalloc(nmemb * size, GFP_KERNEL); | |
473 | if (!mem) | |
474 | return ERR_PTR(-ENOMEM); | |
475 | ||
476 | if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) { | |
477 | kfree(mem); | |
478 | return ERR_PTR(-EFAULT); | |
479 | } | |
480 | ||
481 | return mem; | |
482 | } | |
483 | ||
484 | static int | |
a1606a95 BS |
485 | nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev, |
486 | struct drm_nouveau_gem_pushbuf *req, | |
487 | struct drm_nouveau_gem_pushbuf_bo *bo) | |
6ee73861 BS |
488 | { |
489 | struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; | |
12f735b7 LB |
490 | int ret = 0; |
491 | unsigned i; | |
6ee73861 | 492 | |
a1606a95 | 493 | reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); |
6ee73861 BS |
494 | if (IS_ERR(reloc)) |
495 | return PTR_ERR(reloc); | |
496 | ||
a1606a95 | 497 | for (i = 0; i < req->nr_relocs; i++) { |
6ee73861 BS |
498 | struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i]; |
499 | struct drm_nouveau_gem_pushbuf_bo *b; | |
a1606a95 | 500 | struct nouveau_bo *nvbo; |
6ee73861 BS |
501 | uint32_t data; |
502 | ||
a1606a95 BS |
503 | if (unlikely(r->bo_index > req->nr_buffers)) { |
504 | NV_ERROR(dev, "reloc bo index invalid\n"); | |
6ee73861 BS |
505 | ret = -EINVAL; |
506 | break; | |
507 | } | |
508 | ||
509 | b = &bo[r->bo_index]; | |
a1606a95 | 510 | if (b->presumed.valid) |
6ee73861 BS |
511 | continue; |
512 | ||
a1606a95 BS |
513 | if (unlikely(r->reloc_bo_index > req->nr_buffers)) { |
514 | NV_ERROR(dev, "reloc container bo index invalid\n"); | |
515 | ret = -EINVAL; | |
516 | break; | |
517 | } | |
518 | nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv; | |
519 | ||
520 | if (unlikely(r->reloc_bo_offset + 4 > | |
521 | nvbo->bo.mem.num_pages << PAGE_SHIFT)) { | |
522 | NV_ERROR(dev, "reloc outside of bo\n"); | |
523 | ret = -EINVAL; | |
524 | break; | |
525 | } | |
526 | ||
527 | if (!nvbo->kmap.virtual) { | |
528 | ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, | |
529 | &nvbo->kmap); | |
530 | if (ret) { | |
531 | NV_ERROR(dev, "failed kmap for reloc\n"); | |
532 | break; | |
533 | } | |
534 | nvbo->validate_mapped = true; | |
535 | } | |
536 | ||
6ee73861 | 537 | if (r->flags & NOUVEAU_GEM_RELOC_LOW) |
a1606a95 | 538 | data = b->presumed.offset + r->data; |
6ee73861 BS |
539 | else |
540 | if (r->flags & NOUVEAU_GEM_RELOC_HIGH) | |
a1606a95 | 541 | data = (b->presumed.offset + r->data) >> 32; |
6ee73861 BS |
542 | else |
543 | data = r->data; | |
544 | ||
545 | if (r->flags & NOUVEAU_GEM_RELOC_OR) { | |
a1606a95 | 546 | if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART) |
6ee73861 BS |
547 | data |= r->tor; |
548 | else | |
549 | data |= r->vor; | |
550 | } | |
551 | ||
a1606a95 BS |
552 | spin_lock(&nvbo->bo.lock); |
553 | ret = ttm_bo_wait(&nvbo->bo, false, false, false); | |
e32b2c88 | 554 | spin_unlock(&nvbo->bo.lock); |
a1606a95 BS |
555 | if (ret) { |
556 | NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret); | |
557 | break; | |
558 | } | |
a1606a95 BS |
559 | |
560 | nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data); | |
6ee73861 BS |
561 | } |
562 | ||
563 | kfree(reloc); | |
564 | return ret; | |
565 | } | |
566 | ||
567 | int | |
568 | nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, | |
569 | struct drm_file *file_priv) | |
570 | { | |
a1606a95 | 571 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
6ee73861 | 572 | struct drm_nouveau_gem_pushbuf *req = data; |
a1606a95 BS |
573 | struct drm_nouveau_gem_pushbuf_push *push; |
574 | struct drm_nouveau_gem_pushbuf_bo *bo; | |
6ee73861 BS |
575 | struct nouveau_channel *chan; |
576 | struct validate_op op; | |
a1606a95 BS |
577 | struct nouveau_fence *fence = 0; |
578 | int i, j, ret = 0, do_reloc = 0; | |
6ee73861 BS |
579 | |
580 | NOUVEAU_CHECK_INITIALISED_WITH_RETURN; | |
581 | NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req->channel, file_priv, chan); | |
582 | ||
a1606a95 BS |
583 | req->vram_available = dev_priv->fb_aper_free; |
584 | req->gart_available = dev_priv->gart_info.aper_free; | |
585 | if (unlikely(req->nr_push == 0)) | |
586 | goto out_next; | |
6ee73861 | 587 | |
a1606a95 BS |
588 | if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) { |
589 | NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n", | |
590 | req->nr_push, NOUVEAU_GEM_MAX_PUSH); | |
591 | return -EINVAL; | |
6ee73861 BS |
592 | } |
593 | ||
a1606a95 BS |
594 | if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) { |
595 | NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n", | |
596 | req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS); | |
597 | return -EINVAL; | |
6ee73861 BS |
598 | } |
599 | ||
a1606a95 BS |
600 | if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) { |
601 | NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n", | |
602 | req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS); | |
6ee73861 BS |
603 | return -EINVAL; |
604 | } | |
605 | ||
a1606a95 BS |
606 | push = u_memcpya(req->push, req->nr_push, sizeof(*push)); |
607 | if (IS_ERR(push)) | |
608 | return PTR_ERR(push); | |
609 | ||
6ee73861 | 610 | bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo)); |
a1606a95 BS |
611 | if (IS_ERR(bo)) { |
612 | kfree(push); | |
6ee73861 | 613 | return PTR_ERR(bo); |
a1606a95 | 614 | } |
6ee73861 BS |
615 | |
616 | mutex_lock(&dev->struct_mutex); | |
617 | ||
618 | /* Validate buffer list */ | |
619 | ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers, | |
620 | req->nr_buffers, &op, &do_reloc); | |
621 | if (ret) { | |
622 | NV_ERROR(dev, "validate: %d\n", ret); | |
623 | goto out; | |
624 | } | |
625 | ||
6ee73861 BS |
626 | /* Apply any relocations that are required */ |
627 | if (do_reloc) { | |
a1606a95 | 628 | ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo); |
6ee73861 | 629 | if (ret) { |
6ee73861 | 630 | NV_ERROR(dev, "reloc apply: %d\n", ret); |
6ee73861 BS |
631 | goto out; |
632 | } | |
6ee73861 | 633 | } |
6ee73861 | 634 | |
9a391ad8 | 635 | if (chan->dma.ib_max) { |
a1606a95 | 636 | ret = nouveau_dma_wait(chan, req->nr_push + 1, 6); |
6ee73861 | 637 | if (ret) { |
9a391ad8 | 638 | NV_INFO(dev, "nv50cal_space: %d\n", ret); |
6ee73861 BS |
639 | goto out; |
640 | } | |
6ee73861 | 641 | |
a1606a95 BS |
642 | for (i = 0; i < req->nr_push; i++) { |
643 | struct nouveau_bo *nvbo = (void *)(unsigned long) | |
644 | bo[push[i].bo_index].user_priv; | |
645 | ||
646 | nv50_dma_push(chan, nvbo, push[i].offset, | |
647 | push[i].length); | |
648 | } | |
9a391ad8 | 649 | } else |
2ccb04ec | 650 | if (dev_priv->card_type >= NV_20) { |
a1606a95 | 651 | ret = RING_SPACE(chan, req->nr_push * 2); |
6ee73861 BS |
652 | if (ret) { |
653 | NV_ERROR(dev, "cal_space: %d\n", ret); | |
654 | goto out; | |
655 | } | |
a1606a95 BS |
656 | |
657 | for (i = 0; i < req->nr_push; i++) { | |
658 | struct nouveau_bo *nvbo = (void *)(unsigned long) | |
659 | bo[push[i].bo_index].user_priv; | |
660 | struct drm_mm_node *mem = nvbo->bo.mem.mm_node; | |
661 | ||
662 | OUT_RING(chan, ((mem->start << PAGE_SHIFT) + | |
663 | push[i].offset) | 2); | |
664 | OUT_RING(chan, 0); | |
665 | } | |
6ee73861 | 666 | } else { |
a1606a95 | 667 | ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS)); |
6ee73861 BS |
668 | if (ret) { |
669 | NV_ERROR(dev, "jmp_space: %d\n", ret); | |
670 | goto out; | |
671 | } | |
6ee73861 | 672 | |
a1606a95 BS |
673 | for (i = 0; i < req->nr_push; i++) { |
674 | struct nouveau_bo *nvbo = (void *)(unsigned long) | |
675 | bo[push[i].bo_index].user_priv; | |
676 | struct drm_mm_node *mem = nvbo->bo.mem.mm_node; | |
677 | uint32_t cmd; | |
678 | ||
679 | cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2); | |
680 | cmd |= 0x20000000; | |
681 | if (unlikely(cmd != req->suffix0)) { | |
682 | if (!nvbo->kmap.virtual) { | |
683 | ret = ttm_bo_kmap(&nvbo->bo, 0, | |
684 | nvbo->bo.mem. | |
685 | num_pages, | |
686 | &nvbo->kmap); | |
687 | if (ret) { | |
688 | WIND_RING(chan); | |
689 | goto out; | |
690 | } | |
691 | nvbo->validate_mapped = true; | |
692 | } | |
693 | ||
694 | nouveau_bo_wr32(nvbo, (push[i].offset + | |
695 | push[i].length - 8) / 4, cmd); | |
696 | } | |
697 | ||
698 | OUT_RING(chan, ((mem->start << PAGE_SHIFT) + | |
699 | push[i].offset) | 0x20000000); | |
6ee73861 | 700 | OUT_RING(chan, 0); |
a1606a95 BS |
701 | for (j = 0; j < NOUVEAU_DMA_SKIPS; j++) |
702 | OUT_RING(chan, 0); | |
703 | } | |
6ee73861 BS |
704 | } |
705 | ||
234896a7 | 706 | ret = nouveau_fence_new(chan, &fence, true); |
6ee73861 BS |
707 | if (ret) { |
708 | NV_ERROR(dev, "error fencing pushbuf: %d\n", ret); | |
709 | WIND_RING(chan); | |
710 | goto out; | |
711 | } | |
712 | ||
713 | out: | |
234896a7 LB |
714 | validate_fini(&op, fence); |
715 | nouveau_fence_unref((void**)&fence); | |
6ee73861 BS |
716 | mutex_unlock(&dev->struct_mutex); |
717 | kfree(bo); | |
a1606a95 | 718 | kfree(push); |
6ee73861 BS |
719 | |
720 | out_next: | |
9a391ad8 BS |
721 | if (chan->dma.ib_max) { |
722 | req->suffix0 = 0x00000000; | |
723 | req->suffix1 = 0x00000000; | |
724 | } else | |
2ccb04ec | 725 | if (dev_priv->card_type >= NV_20) { |
6ee73861 BS |
726 | req->suffix0 = 0x00020000; |
727 | req->suffix1 = 0x00000000; | |
728 | } else { | |
729 | req->suffix0 = 0x20000000 | | |
730 | (chan->pushbuf_base + ((chan->dma.cur + 2) << 2)); | |
731 | req->suffix1 = 0x00000000; | |
732 | } | |
733 | ||
734 | return ret; | |
735 | } | |
736 | ||
6ee73861 BS |
737 | static inline uint32_t |
738 | domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain) | |
739 | { | |
740 | uint32_t flags = 0; | |
741 | ||
742 | if (domain & NOUVEAU_GEM_DOMAIN_VRAM) | |
743 | flags |= TTM_PL_FLAG_VRAM; | |
744 | if (domain & NOUVEAU_GEM_DOMAIN_GART) | |
745 | flags |= TTM_PL_FLAG_TT; | |
746 | ||
747 | return flags; | |
748 | } | |
749 | ||
6ee73861 BS |
750 | int |
751 | nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, | |
752 | struct drm_file *file_priv) | |
753 | { | |
754 | struct drm_nouveau_gem_cpu_prep *req = data; | |
755 | struct drm_gem_object *gem; | |
756 | struct nouveau_bo *nvbo; | |
757 | bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT); | |
758 | int ret = -EINVAL; | |
759 | ||
760 | NOUVEAU_CHECK_INITIALISED_WITH_RETURN; | |
761 | ||
762 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); | |
763 | if (!gem) | |
764 | return ret; | |
765 | nvbo = nouveau_gem_object(gem); | |
766 | ||
767 | if (nvbo->cpu_filp) { | |
768 | if (nvbo->cpu_filp == file_priv) | |
769 | goto out; | |
770 | ||
771 | ret = ttm_bo_wait_cpu(&nvbo->bo, no_wait); | |
6ee73861 BS |
772 | if (ret) |
773 | goto out; | |
774 | } | |
775 | ||
776 | if (req->flags & NOUVEAU_GEM_CPU_PREP_NOBLOCK) { | |
f0fbe3eb | 777 | spin_lock(&nvbo->bo.lock); |
6ee73861 | 778 | ret = ttm_bo_wait(&nvbo->bo, false, false, no_wait); |
f0fbe3eb | 779 | spin_unlock(&nvbo->bo.lock); |
6ee73861 BS |
780 | } else { |
781 | ret = ttm_bo_synccpu_write_grab(&nvbo->bo, no_wait); | |
6ee73861 BS |
782 | if (ret == 0) |
783 | nvbo->cpu_filp = file_priv; | |
784 | } | |
785 | ||
786 | out: | |
bc9025bd | 787 | drm_gem_object_unreference_unlocked(gem); |
6ee73861 BS |
788 | return ret; |
789 | } | |
790 | ||
791 | int | |
792 | nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data, | |
793 | struct drm_file *file_priv) | |
794 | { | |
795 | struct drm_nouveau_gem_cpu_prep *req = data; | |
796 | struct drm_gem_object *gem; | |
797 | struct nouveau_bo *nvbo; | |
798 | int ret = -EINVAL; | |
799 | ||
800 | NOUVEAU_CHECK_INITIALISED_WITH_RETURN; | |
801 | ||
802 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); | |
803 | if (!gem) | |
804 | return ret; | |
805 | nvbo = nouveau_gem_object(gem); | |
806 | ||
807 | if (nvbo->cpu_filp != file_priv) | |
808 | goto out; | |
809 | nvbo->cpu_filp = NULL; | |
810 | ||
811 | ttm_bo_synccpu_write_release(&nvbo->bo); | |
812 | ret = 0; | |
813 | ||
814 | out: | |
bc9025bd | 815 | drm_gem_object_unreference_unlocked(gem); |
6ee73861 BS |
816 | return ret; |
817 | } | |
818 | ||
819 | int | |
820 | nouveau_gem_ioctl_info(struct drm_device *dev, void *data, | |
821 | struct drm_file *file_priv) | |
822 | { | |
823 | struct drm_nouveau_gem_info *req = data; | |
824 | struct drm_gem_object *gem; | |
825 | int ret; | |
826 | ||
827 | NOUVEAU_CHECK_INITIALISED_WITH_RETURN; | |
828 | ||
829 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); | |
830 | if (!gem) | |
831 | return -EINVAL; | |
832 | ||
833 | ret = nouveau_gem_info(gem, req); | |
bc9025bd | 834 | drm_gem_object_unreference_unlocked(gem); |
6ee73861 BS |
835 | return ret; |
836 | } | |
837 |