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