1 /*
2 * Copyright 2009 Jerome Glisse.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * 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, sub license, 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26 /*
27 * Authors:
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
30 * Dave Airlie
31 */
32 #include <linux/list.h>
33 #include <linux/slab.h>
34 #include <drm/drmP.h>
35 #include <drm/radeon_drm.h>
36 #include <drm/drm_cache.h>
37 #include "radeon.h"
38 #include "radeon_trace.h"
39
40
41 int radeon_ttm_init(struct radeon_device *rdev);
42 void radeon_ttm_fini(struct radeon_device *rdev);
43 static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
44
45 /*
46 * To exclude mutual BO access we rely on bo_reserve exclusion, as all
47 * function are calling it.
48 */
49
radeon_update_memory_usage(struct radeon_bo * bo,unsigned mem_type,int sign)50 static void radeon_update_memory_usage(struct radeon_bo *bo,
51 unsigned mem_type, int sign)
52 {
53 struct radeon_device *rdev = bo->rdev;
54 u64 size = (u64)bo->tbo.num_pages << PAGE_SHIFT;
55
56 switch (mem_type) {
57 case TTM_PL_TT:
58 if (sign > 0)
59 atomic64_add(size, &rdev->gtt_usage);
60 else
61 atomic64_sub(size, &rdev->gtt_usage);
62 break;
63 case TTM_PL_VRAM:
64 if (sign > 0)
65 atomic64_add(size, &rdev->vram_usage);
66 else
67 atomic64_sub(size, &rdev->vram_usage);
68 break;
69 }
70 }
71
radeon_ttm_bo_destroy(struct ttm_buffer_object * tbo)72 static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
73 {
74 struct radeon_bo *bo;
75
76 bo = container_of(tbo, struct radeon_bo, tbo);
77
78 radeon_update_memory_usage(bo, bo->tbo.mem.mem_type, -1);
79 radeon_mn_unregister(bo);
80
81 mutex_lock(&bo->rdev->gem.mutex);
82 list_del_init(&bo->list);
83 mutex_unlock(&bo->rdev->gem.mutex);
84 radeon_bo_clear_surface_reg(bo);
85 WARN_ON(!list_empty(&bo->va));
86 drm_gem_object_release(&bo->gem_base);
87 kfree(bo);
88 }
89
radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object * bo)90 bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo)
91 {
92 if (bo->destroy == &radeon_ttm_bo_destroy)
93 return true;
94 return false;
95 }
96
radeon_ttm_placement_from_domain(struct radeon_bo * rbo,u32 domain)97 void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
98 {
99 u32 c = 0, i;
100
101 rbo->placement.placement = rbo->placements;
102 rbo->placement.busy_placement = rbo->placements;
103 if (domain & RADEON_GEM_DOMAIN_VRAM)
104 rbo->placements[c++].flags = TTM_PL_FLAG_WC |
105 TTM_PL_FLAG_UNCACHED |
106 TTM_PL_FLAG_VRAM;
107
108 if (domain & RADEON_GEM_DOMAIN_GTT) {
109 if (rbo->flags & RADEON_GEM_GTT_UC) {
110 rbo->placements[c++].flags = TTM_PL_FLAG_UNCACHED |
111 TTM_PL_FLAG_TT;
112
113 } else if ((rbo->flags & RADEON_GEM_GTT_WC) ||
114 (rbo->rdev->flags & RADEON_IS_AGP)) {
115 rbo->placements[c++].flags = TTM_PL_FLAG_WC |
116 TTM_PL_FLAG_UNCACHED |
117 TTM_PL_FLAG_TT;
118 } else {
119 rbo->placements[c++].flags = TTM_PL_FLAG_CACHED |
120 TTM_PL_FLAG_TT;
121 }
122 }
123
124 if (domain & RADEON_GEM_DOMAIN_CPU) {
125 if (rbo->flags & RADEON_GEM_GTT_UC) {
126 rbo->placements[c++].flags = TTM_PL_FLAG_UNCACHED |
127 TTM_PL_FLAG_SYSTEM;
128
129 } else if ((rbo->flags & RADEON_GEM_GTT_WC) ||
130 rbo->rdev->flags & RADEON_IS_AGP) {
131 rbo->placements[c++].flags = TTM_PL_FLAG_WC |
132 TTM_PL_FLAG_UNCACHED |
133 TTM_PL_FLAG_SYSTEM;
134 } else {
135 rbo->placements[c++].flags = TTM_PL_FLAG_CACHED |
136 TTM_PL_FLAG_SYSTEM;
137 }
138 }
139 if (!c)
140 rbo->placements[c++].flags = TTM_PL_MASK_CACHING |
141 TTM_PL_FLAG_SYSTEM;
142
143 rbo->placement.num_placement = c;
144 rbo->placement.num_busy_placement = c;
145
146 for (i = 0; i < c; ++i) {
147 rbo->placements[i].fpfn = 0;
148 if ((rbo->flags & RADEON_GEM_CPU_ACCESS) &&
149 (rbo->placements[i].flags & TTM_PL_FLAG_VRAM))
150 rbo->placements[i].lpfn =
151 rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
152 else
153 rbo->placements[i].lpfn = 0;
154 }
155 }
156
radeon_bo_create(struct radeon_device * rdev,unsigned long size,int byte_align,bool kernel,u32 domain,u32 flags,struct sg_table * sg,struct reservation_object * resv,struct radeon_bo ** bo_ptr)157 int radeon_bo_create(struct radeon_device *rdev,
158 unsigned long size, int byte_align, bool kernel,
159 u32 domain, u32 flags, struct sg_table *sg,
160 struct reservation_object *resv,
161 struct radeon_bo **bo_ptr)
162 {
163 struct radeon_bo *bo;
164 enum ttm_bo_type type;
165 unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
166 size_t acc_size;
167 int r;
168
169 size = ALIGN(size, PAGE_SIZE);
170
171 if (kernel) {
172 type = ttm_bo_type_kernel;
173 } else if (sg) {
174 type = ttm_bo_type_sg;
175 } else {
176 type = ttm_bo_type_device;
177 }
178 *bo_ptr = NULL;
179
180 acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
181 sizeof(struct radeon_bo));
182
183 bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
184 if (bo == NULL)
185 return -ENOMEM;
186 r = drm_gem_object_init(rdev->ddev, &bo->gem_base, size);
187 if (unlikely(r)) {
188 kfree(bo);
189 return r;
190 }
191 bo->rdev = rdev;
192 bo->surface_reg = -1;
193 INIT_LIST_HEAD(&bo->list);
194 INIT_LIST_HEAD(&bo->va);
195 bo->initial_domain = domain & (RADEON_GEM_DOMAIN_VRAM |
196 RADEON_GEM_DOMAIN_GTT |
197 RADEON_GEM_DOMAIN_CPU);
198
199 bo->flags = flags;
200 /* PCI GART is always snooped */
201 if (!(rdev->flags & RADEON_IS_PCIE))
202 bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
203
204 #ifdef CONFIG_X86_32
205 /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
206 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
207 */
208 bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
209 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
210 /* Don't try to enable write-combining when it can't work, or things
211 * may be slow
212 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
213 */
214
215 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
216 thanks to write-combining
217
218 DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
219 "better performance thanks to write-combining\n");
220 bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
221 #else
222 /* For architectures that don't support WC memory,
223 * mask out the WC flag from the BO
224 */
225 if (!drm_arch_can_wc_memory())
226 bo->flags &= ~RADEON_GEM_GTT_WC;
227 #endif
228
229 radeon_ttm_placement_from_domain(bo, domain);
230 /* Kernel allocation are uninterruptible */
231 down_read(&rdev->pm.mclk_lock);
232 r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
233 &bo->placement, page_align, !kernel, NULL,
234 acc_size, sg, resv, &radeon_ttm_bo_destroy);
235 up_read(&rdev->pm.mclk_lock);
236 if (unlikely(r != 0)) {
237 return r;
238 }
239 *bo_ptr = bo;
240
241 trace_radeon_bo_create(bo);
242
243 return 0;
244 }
245
radeon_bo_kmap(struct radeon_bo * bo,void ** ptr)246 int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
247 {
248 bool is_iomem;
249 int r;
250
251 if (bo->kptr) {
252 if (ptr) {
253 *ptr = bo->kptr;
254 }
255 return 0;
256 }
257 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
258 if (r) {
259 return r;
260 }
261 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
262 if (ptr) {
263 *ptr = bo->kptr;
264 }
265 radeon_bo_check_tiling(bo, 0, 0);
266 return 0;
267 }
268
radeon_bo_kunmap(struct radeon_bo * bo)269 void radeon_bo_kunmap(struct radeon_bo *bo)
270 {
271 if (bo->kptr == NULL)
272 return;
273 bo->kptr = NULL;
274 radeon_bo_check_tiling(bo, 0, 0);
275 ttm_bo_kunmap(&bo->kmap);
276 }
277
radeon_bo_ref(struct radeon_bo * bo)278 struct radeon_bo *radeon_bo_ref(struct radeon_bo *bo)
279 {
280 if (bo == NULL)
281 return NULL;
282
283 ttm_bo_reference(&bo->tbo);
284 return bo;
285 }
286
radeon_bo_unref(struct radeon_bo ** bo)287 void radeon_bo_unref(struct radeon_bo **bo)
288 {
289 struct ttm_buffer_object *tbo;
290 struct radeon_device *rdev;
291
292 if ((*bo) == NULL)
293 return;
294 rdev = (*bo)->rdev;
295 tbo = &((*bo)->tbo);
296 ttm_bo_unref(&tbo);
297 if (tbo == NULL)
298 *bo = NULL;
299 }
300
radeon_bo_pin_restricted(struct radeon_bo * bo,u32 domain,u64 max_offset,u64 * gpu_addr)301 int radeon_bo_pin_restricted(struct radeon_bo *bo, u32 domain, u64 max_offset,
302 u64 *gpu_addr)
303 {
304 int r, i;
305
306 if (radeon_ttm_tt_has_userptr(bo->tbo.ttm))
307 return -EPERM;
308
309 if (bo->pin_count) {
310 bo->pin_count++;
311 if (gpu_addr)
312 *gpu_addr = radeon_bo_gpu_offset(bo);
313
314 if (max_offset != 0) {
315 u64 domain_start;
316
317 if (domain == RADEON_GEM_DOMAIN_VRAM)
318 domain_start = bo->rdev->mc.vram_start;
319 else
320 domain_start = bo->rdev->mc.gtt_start;
321 WARN_ON_ONCE(max_offset <
322 (radeon_bo_gpu_offset(bo) - domain_start));
323 }
324
325 return 0;
326 }
327 radeon_ttm_placement_from_domain(bo, domain);
328 for (i = 0; i < bo->placement.num_placement; i++) {
329 /* force to pin into visible video ram */
330 if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
331 !(bo->flags & RADEON_GEM_NO_CPU_ACCESS) &&
332 (!max_offset || max_offset > bo->rdev->mc.visible_vram_size))
333 bo->placements[i].lpfn =
334 bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
335 else
336 bo->placements[i].lpfn = max_offset >> PAGE_SHIFT;
337
338 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
339 }
340
341 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
342 if (likely(r == 0)) {
343 bo->pin_count = 1;
344 if (gpu_addr != NULL)
345 *gpu_addr = radeon_bo_gpu_offset(bo);
346 if (domain == RADEON_GEM_DOMAIN_VRAM)
347 bo->rdev->vram_pin_size += radeon_bo_size(bo);
348 else
349 bo->rdev->gart_pin_size += radeon_bo_size(bo);
350 } else {
351 dev_err(bo->rdev->dev, "%p pin failed\n", bo);
352 }
353 return r;
354 }
355
radeon_bo_pin(struct radeon_bo * bo,u32 domain,u64 * gpu_addr)356 int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
357 {
358 return radeon_bo_pin_restricted(bo, domain, 0, gpu_addr);
359 }
360
radeon_bo_unpin(struct radeon_bo * bo)361 int radeon_bo_unpin(struct radeon_bo *bo)
362 {
363 int r, i;
364
365 if (!bo->pin_count) {
366 dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
367 return 0;
368 }
369 bo->pin_count--;
370 if (bo->pin_count)
371 return 0;
372 for (i = 0; i < bo->placement.num_placement; i++) {
373 bo->placements[i].lpfn = 0;
374 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
375 }
376 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
377 if (likely(r == 0)) {
378 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
379 bo->rdev->vram_pin_size -= radeon_bo_size(bo);
380 else
381 bo->rdev->gart_pin_size -= radeon_bo_size(bo);
382 } else {
383 dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
384 }
385 return r;
386 }
387
radeon_bo_evict_vram(struct radeon_device * rdev)388 int radeon_bo_evict_vram(struct radeon_device *rdev)
389 {
390 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
391 if (0 && (rdev->flags & RADEON_IS_IGP)) {
392 if (rdev->mc.igp_sideport_enabled == false)
393 /* Useless to evict on IGP chips */
394 return 0;
395 }
396 return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
397 }
398
radeon_bo_force_delete(struct radeon_device * rdev)399 void radeon_bo_force_delete(struct radeon_device *rdev)
400 {
401 struct radeon_bo *bo, *n;
402
403 if (list_empty(&rdev->gem.objects)) {
404 return;
405 }
406 dev_err(rdev->dev, "Userspace still has active objects !\n");
407 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
408 mutex_lock(&rdev->ddev->struct_mutex);
409 dev_err(rdev->dev, "%p %p %lu %lu force free\n",
410 &bo->gem_base, bo, (unsigned long)bo->gem_base.size,
411 *((unsigned long *)&bo->gem_base.refcount));
412 mutex_lock(&bo->rdev->gem.mutex);
413 list_del_init(&bo->list);
414 mutex_unlock(&bo->rdev->gem.mutex);
415 /* this should unref the ttm bo */
416 drm_gem_object_unreference(&bo->gem_base);
417 mutex_unlock(&rdev->ddev->struct_mutex);
418 }
419 }
420
radeon_bo_init(struct radeon_device * rdev)421 int radeon_bo_init(struct radeon_device *rdev)
422 {
423 /* Add an MTRR for the VRAM */
424 if (!rdev->fastfb_working) {
425 rdev->mc.vram_mtrr = arch_phys_wc_add(rdev->mc.aper_base,
426 rdev->mc.aper_size);
427 }
428 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
429 rdev->mc.mc_vram_size >> 20,
430 (unsigned long long)rdev->mc.aper_size >> 20);
431 DRM_INFO("RAM width %dbits %cDR\n",
432 rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
433 return radeon_ttm_init(rdev);
434 }
435
radeon_bo_fini(struct radeon_device * rdev)436 void radeon_bo_fini(struct radeon_device *rdev)
437 {
438 radeon_ttm_fini(rdev);
439 arch_phys_wc_del(rdev->mc.vram_mtrr);
440 }
441
442 /* Returns how many bytes TTM can move per IB.
443 */
radeon_bo_get_threshold_for_moves(struct radeon_device * rdev)444 static u64 radeon_bo_get_threshold_for_moves(struct radeon_device *rdev)
445 {
446 u64 real_vram_size = rdev->mc.real_vram_size;
447 u64 vram_usage = atomic64_read(&rdev->vram_usage);
448
449 /* This function is based on the current VRAM usage.
450 *
451 * - If all of VRAM is free, allow relocating the number of bytes that
452 * is equal to 1/4 of the size of VRAM for this IB.
453
454 * - If more than one half of VRAM is occupied, only allow relocating
455 * 1 MB of data for this IB.
456 *
457 * - From 0 to one half of used VRAM, the threshold decreases
458 * linearly.
459 * __________________
460 * 1/4 of -|\ |
461 * VRAM | \ |
462 * | \ |
463 * | \ |
464 * | \ |
465 * | \ |
466 * | \ |
467 * | \________|1 MB
468 * |----------------|
469 * VRAM 0 % 100 %
470 * used used
471 *
472 * Note: It's a threshold, not a limit. The threshold must be crossed
473 * for buffer relocations to stop, so any buffer of an arbitrary size
474 * can be moved as long as the threshold isn't crossed before
475 * the relocation takes place. We don't want to disable buffer
476 * relocations completely.
477 *
478 * The idea is that buffers should be placed in VRAM at creation time
479 * and TTM should only do a minimum number of relocations during
480 * command submission. In practice, you need to submit at least
481 * a dozen IBs to move all buffers to VRAM if they are in GTT.
482 *
483 * Also, things can get pretty crazy under memory pressure and actual
484 * VRAM usage can change a lot, so playing safe even at 50% does
485 * consistently increase performance.
486 */
487
488 u64 half_vram = real_vram_size >> 1;
489 u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
490 u64 bytes_moved_threshold = half_free_vram >> 1;
491 return max(bytes_moved_threshold, 1024*1024ull);
492 }
493
radeon_bo_list_validate(struct radeon_device * rdev,struct ww_acquire_ctx * ticket,struct list_head * head,int ring)494 int radeon_bo_list_validate(struct radeon_device *rdev,
495 struct ww_acquire_ctx *ticket,
496 struct list_head *head, int ring)
497 {
498 struct radeon_cs_reloc *lobj;
499 struct radeon_bo *bo;
500 int r;
501 u64 bytes_moved = 0, initial_bytes_moved;
502 u64 bytes_moved_threshold = radeon_bo_get_threshold_for_moves(rdev);
503
504 r = ttm_eu_reserve_buffers(ticket, head, true);
505 if (unlikely(r != 0)) {
506 return r;
507 }
508
509 list_for_each_entry(lobj, head, tv.head) {
510 bo = lobj->robj;
511 if (!bo->pin_count) {
512 u32 domain = lobj->prefered_domains;
513 u32 allowed = lobj->allowed_domains;
514 u32 current_domain =
515 radeon_mem_type_to_domain(bo->tbo.mem.mem_type);
516
517 /* Check if this buffer will be moved and don't move it
518 * if we have moved too many buffers for this IB already.
519 *
520 * Note that this allows moving at least one buffer of
521 * any size, because it doesn't take the current "bo"
522 * into account. We don't want to disallow buffer moves
523 * completely.
524 */
525 if ((allowed & current_domain) != 0 &&
526 (domain & current_domain) == 0 && /* will be moved */
527 bytes_moved > bytes_moved_threshold) {
528 /* don't move it */
529 domain = current_domain;
530 }
531
532 retry:
533 radeon_ttm_placement_from_domain(bo, domain);
534 if (ring == R600_RING_TYPE_UVD_INDEX)
535 radeon_uvd_force_into_uvd_segment(bo, allowed);
536
537 initial_bytes_moved = atomic64_read(&rdev->num_bytes_moved);
538 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
539 bytes_moved += atomic64_read(&rdev->num_bytes_moved) -
540 initial_bytes_moved;
541
542 if (unlikely(r)) {
543 if (r != -ERESTARTSYS &&
544 domain != lobj->allowed_domains) {
545 domain = lobj->allowed_domains;
546 goto retry;
547 }
548 ttm_eu_backoff_reservation(ticket, head);
549 return r;
550 }
551 }
552 lobj->gpu_offset = radeon_bo_gpu_offset(bo);
553 lobj->tiling_flags = bo->tiling_flags;
554 }
555 return 0;
556 }
557
radeon_bo_fbdev_mmap(struct radeon_bo * bo,struct vm_area_struct * vma)558 int radeon_bo_fbdev_mmap(struct radeon_bo *bo,
559 struct vm_area_struct *vma)
560 {
561 return ttm_fbdev_mmap(vma, &bo->tbo);
562 }
563
radeon_bo_get_surface_reg(struct radeon_bo * bo)564 int radeon_bo_get_surface_reg(struct radeon_bo *bo)
565 {
566 struct radeon_device *rdev = bo->rdev;
567 struct radeon_surface_reg *reg;
568 struct radeon_bo *old_object;
569 int steal;
570 int i;
571
572 lockdep_assert_held(&bo->tbo.resv->lock.base);
573
574 if (!bo->tiling_flags)
575 return 0;
576
577 if (bo->surface_reg >= 0) {
578 reg = &rdev->surface_regs[bo->surface_reg];
579 i = bo->surface_reg;
580 goto out;
581 }
582
583 steal = -1;
584 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
585
586 reg = &rdev->surface_regs[i];
587 if (!reg->bo)
588 break;
589
590 old_object = reg->bo;
591 if (old_object->pin_count == 0)
592 steal = i;
593 }
594
595 /* if we are all out */
596 if (i == RADEON_GEM_MAX_SURFACES) {
597 if (steal == -1)
598 return -ENOMEM;
599 /* find someone with a surface reg and nuke their BO */
600 reg = &rdev->surface_regs[steal];
601 old_object = reg->bo;
602 /* blow away the mapping */
603 DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
604 ttm_bo_unmap_virtual(&old_object->tbo);
605 old_object->surface_reg = -1;
606 i = steal;
607 }
608
609 bo->surface_reg = i;
610 reg->bo = bo;
611
612 out:
613 radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
614 bo->tbo.mem.start << PAGE_SHIFT,
615 bo->tbo.num_pages << PAGE_SHIFT);
616 return 0;
617 }
618
radeon_bo_clear_surface_reg(struct radeon_bo * bo)619 static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
620 {
621 struct radeon_device *rdev = bo->rdev;
622 struct radeon_surface_reg *reg;
623
624 if (bo->surface_reg == -1)
625 return;
626
627 reg = &rdev->surface_regs[bo->surface_reg];
628 radeon_clear_surface_reg(rdev, bo->surface_reg);
629
630 reg->bo = NULL;
631 bo->surface_reg = -1;
632 }
633
radeon_bo_set_tiling_flags(struct radeon_bo * bo,uint32_t tiling_flags,uint32_t pitch)634 int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
635 uint32_t tiling_flags, uint32_t pitch)
636 {
637 struct radeon_device *rdev = bo->rdev;
638 int r;
639
640 if (rdev->family >= CHIP_CEDAR) {
641 unsigned bankw, bankh, mtaspect, tilesplit, stilesplit;
642
643 bankw = (tiling_flags >> RADEON_TILING_EG_BANKW_SHIFT) & RADEON_TILING_EG_BANKW_MASK;
644 bankh = (tiling_flags >> RADEON_TILING_EG_BANKH_SHIFT) & RADEON_TILING_EG_BANKH_MASK;
645 mtaspect = (tiling_flags >> RADEON_TILING_EG_MACRO_TILE_ASPECT_SHIFT) & RADEON_TILING_EG_MACRO_TILE_ASPECT_MASK;
646 tilesplit = (tiling_flags >> RADEON_TILING_EG_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_TILE_SPLIT_MASK;
647 stilesplit = (tiling_flags >> RADEON_TILING_EG_STENCIL_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_STENCIL_TILE_SPLIT_MASK;
648 switch (bankw) {
649 case 0:
650 case 1:
651 case 2:
652 case 4:
653 case 8:
654 break;
655 default:
656 return -EINVAL;
657 }
658 switch (bankh) {
659 case 0:
660 case 1:
661 case 2:
662 case 4:
663 case 8:
664 break;
665 default:
666 return -EINVAL;
667 }
668 switch (mtaspect) {
669 case 0:
670 case 1:
671 case 2:
672 case 4:
673 case 8:
674 break;
675 default:
676 return -EINVAL;
677 }
678 if (tilesplit > 6) {
679 return -EINVAL;
680 }
681 if (stilesplit > 6) {
682 return -EINVAL;
683 }
684 }
685 r = radeon_bo_reserve(bo, false);
686 if (unlikely(r != 0))
687 return r;
688 bo->tiling_flags = tiling_flags;
689 bo->pitch = pitch;
690 radeon_bo_unreserve(bo);
691 return 0;
692 }
693
radeon_bo_get_tiling_flags(struct radeon_bo * bo,uint32_t * tiling_flags,uint32_t * pitch)694 void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
695 uint32_t *tiling_flags,
696 uint32_t *pitch)
697 {
698 lockdep_assert_held(&bo->tbo.resv->lock.base);
699
700 if (tiling_flags)
701 *tiling_flags = bo->tiling_flags;
702 if (pitch)
703 *pitch = bo->pitch;
704 }
705
radeon_bo_check_tiling(struct radeon_bo * bo,bool has_moved,bool force_drop)706 int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
707 bool force_drop)
708 {
709 if (!force_drop)
710 lockdep_assert_held(&bo->tbo.resv->lock.base);
711
712 if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
713 return 0;
714
715 if (force_drop) {
716 radeon_bo_clear_surface_reg(bo);
717 return 0;
718 }
719
720 if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
721 if (!has_moved)
722 return 0;
723
724 if (bo->surface_reg >= 0)
725 radeon_bo_clear_surface_reg(bo);
726 return 0;
727 }
728
729 if ((bo->surface_reg >= 0) && !has_moved)
730 return 0;
731
732 return radeon_bo_get_surface_reg(bo);
733 }
734
radeon_bo_move_notify(struct ttm_buffer_object * bo,struct ttm_mem_reg * new_mem)735 void radeon_bo_move_notify(struct ttm_buffer_object *bo,
736 struct ttm_mem_reg *new_mem)
737 {
738 struct radeon_bo *rbo;
739
740 if (!radeon_ttm_bo_is_radeon_bo(bo))
741 return;
742
743 rbo = container_of(bo, struct radeon_bo, tbo);
744 radeon_bo_check_tiling(rbo, 0, 1);
745 radeon_vm_bo_invalidate(rbo->rdev, rbo);
746
747 /* update statistics */
748 if (!new_mem)
749 return;
750
751 radeon_update_memory_usage(rbo, bo->mem.mem_type, -1);
752 radeon_update_memory_usage(rbo, new_mem->mem_type, 1);
753 }
754
radeon_bo_fault_reserve_notify(struct ttm_buffer_object * bo)755 int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
756 {
757 struct radeon_device *rdev;
758 struct radeon_bo *rbo;
759 unsigned long offset, size;
760 int r;
761
762 if (!radeon_ttm_bo_is_radeon_bo(bo))
763 return 0;
764 rbo = container_of(bo, struct radeon_bo, tbo);
765 radeon_bo_check_tiling(rbo, 0, 0);
766 rdev = rbo->rdev;
767 if (bo->mem.mem_type != TTM_PL_VRAM)
768 return 0;
769
770 size = bo->mem.num_pages << PAGE_SHIFT;
771 offset = bo->mem.start << PAGE_SHIFT;
772 if ((offset + size) <= rdev->mc.visible_vram_size)
773 return 0;
774
775 /* hurrah the memory is not visible ! */
776 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
777 rbo->placements[0].lpfn = rdev->mc.visible_vram_size >> PAGE_SHIFT;
778 r = ttm_bo_validate(bo, &rbo->placement, false, false);
779 if (unlikely(r == -ENOMEM)) {
780 radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
781 return ttm_bo_validate(bo, &rbo->placement, false, false);
782 } else if (unlikely(r != 0)) {
783 return r;
784 }
785
786 offset = bo->mem.start << PAGE_SHIFT;
787 /* this should never happen */
788 if ((offset + size) > rdev->mc.visible_vram_size)
789 return -EINVAL;
790
791 return 0;
792 }
793
radeon_bo_wait(struct radeon_bo * bo,u32 * mem_type,bool no_wait)794 int radeon_bo_wait(struct radeon_bo *bo, u32 *mem_type, bool no_wait)
795 {
796 int r;
797
798 r = ttm_bo_reserve(&bo->tbo, true, no_wait, false, NULL);
799 if (unlikely(r != 0))
800 return r;
801 if (mem_type)
802 *mem_type = bo->tbo.mem.mem_type;
803
804 r = ttm_bo_wait(&bo->tbo, true, true, no_wait);
805 ttm_bo_unreserve(&bo->tbo);
806 return r;
807 }
808
809 /**
810 * radeon_bo_fence - add fence to buffer object
811 *
812 * @bo: buffer object in question
813 * @fence: fence to add
814 * @shared: true if fence should be added shared
815 *
816 */
radeon_bo_fence(struct radeon_bo * bo,struct radeon_fence * fence,bool shared)817 void radeon_bo_fence(struct radeon_bo *bo, struct radeon_fence *fence,
818 bool shared)
819 {
820 struct reservation_object *resv = bo->tbo.resv;
821
822 if (shared)
823 reservation_object_add_shared_fence(resv, &fence->base);
824 else
825 reservation_object_add_excl_fence(resv, &fence->base);
826 }
827