diff options
Diffstat (limited to 'drivers/gpu/drm/amd/amdgpu/amdgpu_object.c')
| -rw-r--r-- | drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 522 | 
1 files changed, 288 insertions, 234 deletions
| diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c index ac043baac05d..b7a2070d90af 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c @@ -52,34 +52,11 @@   *   */ -/** - * amdgpu_bo_subtract_pin_size - Remove BO from pin_size accounting - * - * @bo: &amdgpu_bo buffer object - * - * This function is called when a BO stops being pinned, and updates the - * &amdgpu_device pin_size values accordingly. - */ -static void amdgpu_bo_subtract_pin_size(struct amdgpu_bo *bo) -{ -	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); - -	if (bo->tbo.mem.mem_type == TTM_PL_VRAM) { -		atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); -		atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), -			     &adev->visible_pin_size); -	} else if (bo->tbo.mem.mem_type == TTM_PL_TT) { -		atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size); -	} -} -  static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)  {  	struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);  	struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo); - -	if (bo->pin_count > 0) -		amdgpu_bo_subtract_pin_size(bo); +	struct amdgpu_bo_user *ubo;  	amdgpu_bo_kunmap(bo); @@ -94,8 +71,12 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)  	}  	amdgpu_bo_unref(&bo->parent); -	kfree(bo->metadata); -	kfree(bo); +	if (bo->tbo.type != ttm_bo_type_kernel) { +		ubo = to_amdgpu_bo_user(bo); +		kfree(ubo->metadata); +	} + +	kvfree(bo);  }  /** @@ -137,7 +118,7 @@ void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)  		places[c].fpfn = 0;  		places[c].lpfn = 0;  		places[c].mem_type = TTM_PL_VRAM; -		places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED; +		places[c].flags = 0;  		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)  			places[c].lpfn = visible_pfn; @@ -152,13 +133,10 @@ void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)  	if (domain & AMDGPU_GEM_DOMAIN_GTT) {  		places[c].fpfn = 0;  		places[c].lpfn = 0; -		places[c].mem_type = TTM_PL_TT; +		places[c].mem_type = +			abo->flags & AMDGPU_GEM_CREATE_PREEMPTIBLE ? +			AMDGPU_PL_PREEMPT : TTM_PL_TT;  		places[c].flags = 0; -		if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) -			places[c].flags |= TTM_PL_FLAG_WC | -				TTM_PL_FLAG_UNCACHED; -		else -			places[c].flags |= TTM_PL_FLAG_CACHED;  		c++;  	} @@ -167,11 +145,6 @@ void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)  		places[c].lpfn = 0;  		places[c].mem_type = TTM_PL_SYSTEM;  		places[c].flags = 0; -		if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) -			places[c].flags |= TTM_PL_FLAG_WC | -				TTM_PL_FLAG_UNCACHED; -		else -			places[c].flags |= TTM_PL_FLAG_CACHED;  		c++;  	} @@ -179,7 +152,7 @@ void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)  		places[c].fpfn = 0;  		places[c].lpfn = 0;  		places[c].mem_type = AMDGPU_PL_GDS; -		places[c].flags = TTM_PL_FLAG_UNCACHED; +		places[c].flags = 0;  		c++;  	} @@ -187,7 +160,7 @@ void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)  		places[c].fpfn = 0;  		places[c].lpfn = 0;  		places[c].mem_type = AMDGPU_PL_GWS; -		places[c].flags = TTM_PL_FLAG_UNCACHED; +		places[c].flags = 0;  		c++;  	} @@ -195,7 +168,7 @@ void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)  		places[c].fpfn = 0;  		places[c].lpfn = 0;  		places[c].mem_type = AMDGPU_PL_OA; -		places[c].flags = TTM_PL_FLAG_UNCACHED; +		places[c].flags = 0;  		c++;  	} @@ -203,7 +176,7 @@ void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)  		places[c].fpfn = 0;  		places[c].lpfn = 0;  		places[c].mem_type = TTM_PL_SYSTEM; -		places[c].flags = TTM_PL_MASK_CACHING; +		places[c].flags = 0;  		c++;  	} @@ -258,6 +231,7 @@ int amdgpu_bo_create_reserved(struct amdgpu_device *adev,  	bp.flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;  	bp.type = ttm_bo_type_kernel;  	bp.resv = NULL; +	bp.bo_ptr_size = sizeof(struct amdgpu_bo);  	if (!*bo_ptr) {  		r = amdgpu_bo_create(adev, &bp, bo_ptr); @@ -390,14 +364,14 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,  	if (cpu_addr)  		amdgpu_bo_kunmap(*bo_ptr); -	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.mem); +	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);  	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {  		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;  		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;  	}  	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement, -			     &(*bo_ptr)->tbo.mem, &ctx); +			     &(*bo_ptr)->tbo.resource, &ctx);  	if (r)  		goto error; @@ -519,20 +493,31 @@ bool amdgpu_bo_support_uswc(u64 bo_flags)  #endif  } -static int amdgpu_bo_do_create(struct amdgpu_device *adev, +/** + * amdgpu_bo_create - create an &amdgpu_bo buffer object + * @adev: amdgpu device object + * @bp: parameters to be used for the buffer object + * @bo_ptr: pointer to the buffer object pointer + * + * Creates an &amdgpu_bo buffer object. + * + * Returns: + * 0 for success or a negative error code on failure. + */ +int amdgpu_bo_create(struct amdgpu_device *adev,  			       struct amdgpu_bo_param *bp,  			       struct amdgpu_bo **bo_ptr)  {  	struct ttm_operation_ctx ctx = {  		.interruptible = (bp->type != ttm_bo_type_kernel),  		.no_wait_gpu = bp->no_wait_gpu, -		.resv = bp->resv, -		.flags = bp->type != ttm_bo_type_kernel ? -			TTM_OPT_FLAG_ALLOW_RES_EVICT : 0 +		/* We opt to avoid OOM on system pages allocations */ +		.gfp_retry_mayfail = true, +		.allow_res_evict = bp->type != ttm_bo_type_kernel, +		.resv = bp->resv  	};  	struct amdgpu_bo *bo;  	unsigned long page_align, size = bp->size; -	size_t acc_size;  	int r;  	/* Note that GDS/GWS/OA allocates 1 page per byte/resource. */ @@ -553,12 +538,10 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,  	if (!amdgpu_bo_validate_size(adev, size, bp->domain))  		return -ENOMEM; -	*bo_ptr = NULL; - -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size, -				       sizeof(struct amdgpu_bo)); +	BUG_ON(bp->bo_ptr_size < sizeof(struct amdgpu_bo)); -	bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL); +	*bo_ptr = NULL; +	bo = kvzalloc(bp->bo_ptr_size, GFP_KERNEL);  	if (bo == NULL)  		return -ENOMEM;  	drm_gem_private_object_init(adev_to_drm(adev), &bo->tbo.base, size); @@ -586,21 +569,21 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,  		bo->tbo.priority = 1;  	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type, -				 &bo->placement, page_align, &ctx, acc_size, -				 NULL, bp->resv, &amdgpu_bo_destroy); +				 &bo->placement, page_align, &ctx,  NULL, +				 bp->resv, &amdgpu_bo_destroy);  	if (unlikely(r != 0))  		return r;  	if (!amdgpu_gmc_vram_full_visible(&adev->gmc) && -	    bo->tbo.mem.mem_type == TTM_PL_VRAM && -	    bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT) +	    bo->tbo.resource->mem_type == TTM_PL_VRAM && +	    bo->tbo.resource->start < adev->gmc.visible_vram_size >> PAGE_SHIFT)  		amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,  					     ctx.bytes_moved);  	else  		amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);  	if (bp->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED && -	    bo->tbo.mem.mem_type == TTM_PL_VRAM) { +	    bo->tbo.resource->mem_type == TTM_PL_VRAM) {  		struct dma_fence *fence;  		r = amdgpu_fill_buffer(bo, 0, bo->tbo.base.resv, &fence); @@ -631,75 +614,62 @@ fail_unreserve:  	return r;  } -static int amdgpu_bo_create_shadow(struct amdgpu_device *adev, -				   unsigned long size, -				   struct amdgpu_bo *bo) +/** + * amdgpu_bo_create_user - create an &amdgpu_bo_user buffer object + * @adev: amdgpu device object + * @bp: parameters to be used for the buffer object + * @ubo_ptr: pointer to the buffer object pointer + * + * Create a BO to be used by user application; + * + * Returns: + * 0 for success or a negative error code on failure. + */ + +int amdgpu_bo_create_user(struct amdgpu_device *adev, +			  struct amdgpu_bo_param *bp, +			  struct amdgpu_bo_user **ubo_ptr)  { -	struct amdgpu_bo_param bp; +	struct amdgpu_bo *bo_ptr;  	int r; -	if (bo->shadow) -		return 0; - -	memset(&bp, 0, sizeof(bp)); -	bp.size = size; -	bp.domain = AMDGPU_GEM_DOMAIN_GTT; -	bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC | -		AMDGPU_GEM_CREATE_SHADOW; -	bp.type = ttm_bo_type_kernel; -	bp.resv = bo->tbo.base.resv; - -	r = amdgpu_bo_do_create(adev, &bp, &bo->shadow); -	if (!r) { -		bo->shadow->parent = amdgpu_bo_ref(bo); -		mutex_lock(&adev->shadow_list_lock); -		list_add_tail(&bo->shadow->shadow_list, &adev->shadow_list); -		mutex_unlock(&adev->shadow_list_lock); -	} +	bp->bo_ptr_size = sizeof(struct amdgpu_bo_user); +	r = amdgpu_bo_create(adev, bp, &bo_ptr); +	if (r) +		return r; +	*ubo_ptr = to_amdgpu_bo_user(bo_ptr);  	return r;  }  /** - * amdgpu_bo_create - create an &amdgpu_bo buffer object + * amdgpu_bo_create_vm - create an &amdgpu_bo_vm buffer object   * @adev: amdgpu device object   * @bp: parameters to be used for the buffer object - * @bo_ptr: pointer to the buffer object pointer + * @vmbo_ptr: pointer to the buffer object pointer   * - * Creates an &amdgpu_bo buffer object; and if requested, also creates a - * shadow object. - * Shadow object is used to backup the original buffer object, and is always - * in GTT. + * Create a BO to be for GPUVM.   *   * Returns:   * 0 for success or a negative error code on failure.   */ -int amdgpu_bo_create(struct amdgpu_device *adev, -		     struct amdgpu_bo_param *bp, -		     struct amdgpu_bo **bo_ptr) + +int amdgpu_bo_create_vm(struct amdgpu_device *adev, +			struct amdgpu_bo_param *bp, +			struct amdgpu_bo_vm **vmbo_ptr)  { -	u64 flags = bp->flags; +	struct amdgpu_bo *bo_ptr;  	int r; -	bp->flags = bp->flags & ~AMDGPU_GEM_CREATE_SHADOW; -	r = amdgpu_bo_do_create(adev, bp, bo_ptr); +	/* bo_ptr_size will be determined by the caller and it depends on +	 * num of amdgpu_vm_pt entries. +	 */ +	BUG_ON(bp->bo_ptr_size < sizeof(struct amdgpu_bo_vm)); +	r = amdgpu_bo_create(adev, bp, &bo_ptr);  	if (r)  		return r; -	if ((flags & AMDGPU_GEM_CREATE_SHADOW) && !(adev->flags & AMD_IS_APU)) { -		if (!bp->resv) -			WARN_ON(dma_resv_lock((*bo_ptr)->tbo.base.resv, -							NULL)); - -		r = amdgpu_bo_create_shadow(adev, bp->size, *bo_ptr); - -		if (!bp->resv) -			dma_resv_unlock((*bo_ptr)->tbo.base.resv); - -		if (r) -			amdgpu_bo_unref(bo_ptr); -	} - +	*vmbo_ptr = to_amdgpu_bo_vm(bo_ptr);  	return r;  } @@ -721,7 +691,7 @@ int amdgpu_bo_validate(struct amdgpu_bo *bo)  	uint32_t domain;  	int r; -	if (bo->pin_count) +	if (bo->tbo.pin_count)  		return 0;  	domain = bo->preferred_domains; @@ -738,6 +708,22 @@ retry:  }  /** + * amdgpu_bo_add_to_shadow_list - add a BO to the shadow list + * + * @bo: BO that will be inserted into the shadow list + * + * Insert a BO to the shadow list. + */ +void amdgpu_bo_add_to_shadow_list(struct amdgpu_bo *bo) +{ +	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); + +	mutex_lock(&adev->shadow_list_lock); +	list_add_tail(&bo->shadow_list, &adev->shadow_list); +	mutex_unlock(&adev->shadow_list_lock); +} + +/**   * amdgpu_bo_restore_shadow - restore an &amdgpu_bo shadow   *   * @shadow: &amdgpu_bo shadow to be restored @@ -791,12 +777,12 @@ int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)  		return 0;  	} -	r = dma_resv_wait_timeout_rcu(bo->tbo.base.resv, false, false, -						MAX_SCHEDULE_TIMEOUT); +	r = dma_resv_wait_timeout(bo->tbo.base.resv, false, false, +				  MAX_SCHEDULE_TIMEOUT);  	if (r < 0)  		return r; -	r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap); +	r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.resource->num_pages, &bo->kmap);  	if (r)  		return r; @@ -906,7 +892,7 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,  		return -EINVAL;  	/* A shared bo cannot be migrated to VRAM */ -	if (bo->prime_shared_count) { +	if (bo->prime_shared_count || bo->tbo.base.import_attach) {  		if (domain & AMDGPU_GEM_DOMAIN_GTT)  			domain = AMDGPU_GEM_DOMAIN_GTT;  		else @@ -918,13 +904,19 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,  	 */  	domain = amdgpu_bo_get_preferred_pin_domain(adev, domain); -	if (bo->pin_count) { -		uint32_t mem_type = bo->tbo.mem.mem_type; +	if (bo->tbo.pin_count) { +		uint32_t mem_type = bo->tbo.resource->mem_type; +		uint32_t mem_flags = bo->tbo.resource->placement;  		if (!(domain & amdgpu_mem_type_to_domain(mem_type)))  			return -EINVAL; -		bo->pin_count++; +		if ((mem_type == TTM_PL_VRAM) && +		    (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) && +		    !(mem_flags & TTM_PL_FLAG_CONTIGUOUS)) +			return -EINVAL; + +		ttm_bo_pin(&bo->tbo);  		if (max_offset != 0) {  			u64 domain_start = amdgpu_ttm_domain_start(adev, @@ -939,7 +931,6 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,  	if (bo->tbo.base.import_attach)  		dma_buf_pin(bo->tbo.base.import_attach); -	bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;  	/* force to pin into visible video ram */  	if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))  		bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; @@ -955,7 +946,6 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,  		if (!bo->placements[i].lpfn ||  		    (lpfn && lpfn < bo->placements[i].lpfn))  			bo->placements[i].lpfn = lpfn; -		bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;  	}  	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); @@ -964,9 +954,9 @@ int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,  		goto error;  	} -	bo->pin_count = 1; +	ttm_bo_pin(&bo->tbo); -	domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); +	domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);  	if (domain == AMDGPU_GEM_DOMAIN_VRAM) {  		atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size);  		atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo), @@ -993,6 +983,7 @@ error:   */  int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)  { +	bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;  	return amdgpu_bo_pin_restricted(bo, domain, 0, 0);  } @@ -1006,34 +997,24 @@ int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)   * Returns:   * 0 for success or a negative error code on failure.   */ -int amdgpu_bo_unpin(struct amdgpu_bo *bo) +void amdgpu_bo_unpin(struct amdgpu_bo *bo)  {  	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); -	struct ttm_operation_ctx ctx = { false, false }; -	int r, i; -	if (WARN_ON_ONCE(!bo->pin_count)) { -		dev_warn(adev->dev, "%p unpin not necessary\n", bo); -		return 0; -	} -	bo->pin_count--; -	if (bo->pin_count) -		return 0; - -	amdgpu_bo_subtract_pin_size(bo); +	ttm_bo_unpin(&bo->tbo); +	if (bo->tbo.pin_count) +		return;  	if (bo->tbo.base.import_attach)  		dma_buf_unpin(bo->tbo.base.import_attach); -	for (i = 0; i < bo->placement.num_placement; i++) { -		bo->placements[i].lpfn = 0; -		bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT; +	if (bo->tbo.resource->mem_type == TTM_PL_VRAM) { +		atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size); +		atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo), +			     &adev->visible_pin_size); +	} else if (bo->tbo.resource->mem_type == TTM_PL_TT) { +		atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size);  	} -	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); -	if (unlikely(r)) -		dev_err(adev->dev, "%p validate failed for unpin\n", bo); - -	return r;  }  /** @@ -1048,14 +1029,15 @@ int amdgpu_bo_unpin(struct amdgpu_bo *bo)   */  int amdgpu_bo_evict_vram(struct amdgpu_device *adev)  { -	/* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */ -#ifndef CONFIG_HIBERNATION -	if (adev->flags & AMD_IS_APU) { -		/* Useless to evict on IGP chips */ +	struct ttm_resource_manager *man; + +	if (adev->in_s3 && (adev->flags & AMD_IS_APU)) { +		/* No need to evict vram on APUs for suspend to ram */  		return 0;  	} -#endif -	return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM); + +	man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM); +	return ttm_resource_manager_evict_all(&adev->mman.bdev, man);  }  static const char *amdgpu_vram_names[] = { @@ -1069,6 +1051,7 @@ static const char *amdgpu_vram_names[] = {  	"DDR3",  	"DDR4",  	"GDDR6", +	"DDR5"  };  /** @@ -1082,13 +1065,17 @@ static const char *amdgpu_vram_names[] = {   */  int amdgpu_bo_init(struct amdgpu_device *adev)  { -	/* reserve PAT memory space to WC for VRAM */ -	arch_io_reserve_memtype_wc(adev->gmc.aper_base, -				   adev->gmc.aper_size); +	/* On A+A platform, VRAM can be mapped as WB */ +	if (!adev->gmc.xgmi.connected_to_cpu) { +		/* reserve PAT memory space to WC for VRAM */ +		arch_io_reserve_memtype_wc(adev->gmc.aper_base, +				adev->gmc.aper_size); + +		/* Add an MTRR for the VRAM */ +		adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base, +				adev->gmc.aper_size); +	} -	/* Add an MTRR for the VRAM */ -	adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base, -					      adev->gmc.aper_size);  	DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",  		 adev->gmc.mc_vram_size >> 20,  		 (unsigned long long)adev->gmc.aper_size >> 20); @@ -1098,23 +1085,6 @@ int amdgpu_bo_init(struct amdgpu_device *adev)  }  /** - * amdgpu_bo_late_init - late init - * @adev: amdgpu device object - * - * Calls amdgpu_ttm_late_init() to free resources used earlier during - * initialization. - * - * Returns: - * 0 for success or a negative error code on failure. - */ -int amdgpu_bo_late_init(struct amdgpu_device *adev) -{ -	amdgpu_ttm_late_init(adev); - -	return 0; -} - -/**   * amdgpu_bo_fini - tear down memory manager   * @adev: amdgpu device object   * @@ -1123,27 +1093,6 @@ int amdgpu_bo_late_init(struct amdgpu_device *adev)  void amdgpu_bo_fini(struct amdgpu_device *adev)  {  	amdgpu_ttm_fini(adev); -	arch_phys_wc_del(adev->gmc.vram_mtrr); -	arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size); -} - -/** - * amdgpu_bo_fbdev_mmap - mmap fbdev memory - * @bo: &amdgpu_bo buffer object - * @vma: vma as input from the fbdev mmap method - * - * Calls ttm_fbdev_mmap() to mmap fbdev memory if it is backed by a bo. - * - * Returns: - * 0 for success or a negative error code on failure. - */ -int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo, -			     struct vm_area_struct *vma) -{ -	if (vma->vm_pgoff != 0) -		return -EACCES; - -	return ttm_bo_mmap_obj(vma, &bo->tbo);  }  /** @@ -1160,12 +1109,15 @@ int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,  int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)  {  	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); +	struct amdgpu_bo_user *ubo; +	BUG_ON(bo->tbo.type == ttm_bo_type_kernel);  	if (adev->family <= AMDGPU_FAMILY_CZ &&  	    AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)  		return -EINVAL; -	bo->tiling_flags = tiling_flags; +	ubo = to_amdgpu_bo_user(bo); +	ubo->tiling_flags = tiling_flags;  	return 0;  } @@ -1179,10 +1131,14 @@ int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)   */  void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)  { +	struct amdgpu_bo_user *ubo; + +	BUG_ON(bo->tbo.type == ttm_bo_type_kernel);  	dma_resv_assert_held(bo->tbo.base.resv); +	ubo = to_amdgpu_bo_user(bo);  	if (tiling_flags) -		*tiling_flags = bo->tiling_flags; +		*tiling_flags = ubo->tiling_flags;  }  /** @@ -1201,13 +1157,16 @@ void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)  int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,  			    uint32_t metadata_size, uint64_t flags)  { +	struct amdgpu_bo_user *ubo;  	void *buffer; +	BUG_ON(bo->tbo.type == ttm_bo_type_kernel); +	ubo = to_amdgpu_bo_user(bo);  	if (!metadata_size) { -		if (bo->metadata_size) { -			kfree(bo->metadata); -			bo->metadata = NULL; -			bo->metadata_size = 0; +		if (ubo->metadata_size) { +			kfree(ubo->metadata); +			ubo->metadata = NULL; +			ubo->metadata_size = 0;  		}  		return 0;  	} @@ -1219,10 +1178,10 @@ int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,  	if (buffer == NULL)  		return -ENOMEM; -	kfree(bo->metadata); -	bo->metadata_flags = flags; -	bo->metadata = buffer; -	bo->metadata_size = metadata_size; +	kfree(ubo->metadata); +	ubo->metadata_flags = flags; +	ubo->metadata = buffer; +	ubo->metadata_size = metadata_size;  	return 0;  } @@ -1246,21 +1205,26 @@ int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,  			   size_t buffer_size, uint32_t *metadata_size,  			   uint64_t *flags)  { +	struct amdgpu_bo_user *ubo; +  	if (!buffer && !metadata_size)  		return -EINVAL; +	BUG_ON(bo->tbo.type == ttm_bo_type_kernel); +	ubo = to_amdgpu_bo_user(bo); +	if (metadata_size) +		*metadata_size = ubo->metadata_size; +  	if (buffer) { -		if (buffer_size < bo->metadata_size) +		if (buffer_size < ubo->metadata_size)  			return -EINVAL; -		if (bo->metadata_size) -			memcpy(buffer, bo->metadata, bo->metadata_size); +		if (ubo->metadata_size) +			memcpy(buffer, ubo->metadata, ubo->metadata_size);  	} -	if (metadata_size) -		*metadata_size = bo->metadata_size;  	if (flags) -		*flags = bo->metadata_flags; +		*flags = ubo->metadata_flags;  	return 0;  } @@ -1281,7 +1245,7 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,  {  	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);  	struct amdgpu_bo *abo; -	struct ttm_resource *old_mem = &bo->mem; +	struct ttm_resource *old_mem = bo->resource;  	if (!amdgpu_bo_is_amdgpu_bo(bo))  		return; @@ -1292,7 +1256,7 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,  	amdgpu_bo_kunmap(abo);  	if (abo->tbo.base.dma_buf && !abo->tbo.base.import_attach && -	    bo->mem.mem_type != TTM_PL_SYSTEM) +	    bo->resource->mem_type != TTM_PL_SYSTEM)  		dma_buf_move_notify(abo->tbo.base.dma_buf);  	/* remember the eviction */ @@ -1307,6 +1271,26 @@ void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,  	trace_amdgpu_bo_move(abo, new_mem->mem_type, old_mem->mem_type);  } +void amdgpu_bo_get_memory(struct amdgpu_bo *bo, uint64_t *vram_mem, +				uint64_t *gtt_mem, uint64_t *cpu_mem) +{ +	unsigned int domain; + +	domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type); +	switch (domain) { +	case AMDGPU_GEM_DOMAIN_VRAM: +		*vram_mem += amdgpu_bo_size(bo); +		break; +	case AMDGPU_GEM_DOMAIN_GTT: +		*gtt_mem += amdgpu_bo_size(bo); +		break; +	case AMDGPU_GEM_DOMAIN_CPU: +	default: +		*cpu_mem += amdgpu_bo_size(bo); +		break; +	} +} +  /**   * amdgpu_bo_release_notify - notification about a BO being released   * @bo: pointer to a buffer object @@ -1334,7 +1318,7 @@ void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)  	if (bo->base.resv == &bo->base._resv)  		amdgpu_amdkfd_remove_fence_on_pt_pd_bos(abo); -	if (bo->mem.mem_type != TTM_PL_VRAM || !bo->mem.mm_node || +	if (bo->resource->mem_type != TTM_PL_VRAM ||  	    !(abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE))  		return; @@ -1360,33 +1344,27 @@ void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)   * Returns:   * 0 for success or a negative error code on failure.   */ -int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo) +vm_fault_t amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)  {  	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);  	struct ttm_operation_ctx ctx = { false, false }; -	struct amdgpu_bo *abo; -	unsigned long offset, size; +	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo); +	unsigned long offset;  	int r; -	if (!amdgpu_bo_is_amdgpu_bo(bo)) -		return 0; - -	abo = ttm_to_amdgpu_bo(bo); -  	/* Remember that this BO was accessed by the CPU */  	abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; -	if (bo->mem.mem_type != TTM_PL_VRAM) +	if (bo->resource->mem_type != TTM_PL_VRAM)  		return 0; -	size = bo->mem.num_pages << PAGE_SHIFT; -	offset = bo->mem.start << PAGE_SHIFT; -	if ((offset + size) <= adev->gmc.visible_vram_size) +	offset = bo->resource->start << PAGE_SHIFT; +	if ((offset + bo->base.size) <= adev->gmc.visible_vram_size)  		return 0;  	/* Can't move a pinned BO to visible VRAM */ -	if (abo->pin_count > 0) -		return -EINVAL; +	if (abo->tbo.pin_count > 0) +		return VM_FAULT_SIGBUS;  	/* hurrah the memory is not visible ! */  	atomic64_inc(&adev->num_vram_cpu_page_faults); @@ -1398,15 +1376,18 @@ int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)  	abo->placement.busy_placement = &abo->placements[1];  	r = ttm_bo_validate(bo, &abo->placement, &ctx); -	if (unlikely(r != 0)) -		return r; +	if (unlikely(r == -EBUSY || r == -ERESTARTSYS)) +		return VM_FAULT_NOPAGE; +	else if (unlikely(r)) +		return VM_FAULT_SIGBUS; -	offset = bo->mem.start << PAGE_SHIFT; +	offset = bo->resource->start << PAGE_SHIFT;  	/* this should never happen */ -	if (bo->mem.mem_type == TTM_PL_VRAM && -	    (offset + size) > adev->gmc.visible_vram_size) -		return -EINVAL; +	if (bo->resource->mem_type == TTM_PL_VRAM && +	    (offset + bo->base.size) > adev->gmc.visible_vram_size) +		return VM_FAULT_SIGBUS; +	ttm_bo_move_to_lru_tail_unlocked(bo);  	return 0;  } @@ -1487,11 +1468,11 @@ int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr)   */  u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)  { -	WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM); +	WARN_ON_ONCE(bo->tbo.resource->mem_type == TTM_PL_SYSTEM);  	WARN_ON_ONCE(!dma_resv_is_locked(bo->tbo.base.resv) && -		     !bo->pin_count && bo->tbo.type != ttm_bo_type_kernel); -	WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET); -	WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM && +		     !bo->tbo.pin_count && bo->tbo.type != ttm_bo_type_kernel); +	WARN_ON_ONCE(bo->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET); +	WARN_ON_ONCE(bo->tbo.resource->mem_type == TTM_PL_VRAM &&  		     !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));  	return amdgpu_bo_gpu_offset_no_check(bo); @@ -1509,8 +1490,8 @@ u64 amdgpu_bo_gpu_offset_no_check(struct amdgpu_bo *bo)  	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);  	uint64_t offset; -	offset = (bo->tbo.mem.start << PAGE_SHIFT) + -		 amdgpu_ttm_domain_start(adev, bo->tbo.mem.mem_type); +	offset = (bo->tbo.resource->start << PAGE_SHIFT) + +		 amdgpu_ttm_domain_start(adev, bo->tbo.resource->mem_type);  	return amdgpu_gmc_sign_extend(offset);  } @@ -1533,3 +1514,76 @@ uint32_t amdgpu_bo_get_preferred_pin_domain(struct amdgpu_device *adev,  	}  	return domain;  } + +#if defined(CONFIG_DEBUG_FS) +#define amdgpu_bo_print_flag(m, bo, flag)		        \ +	do {							\ +		if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) {	\ +			seq_printf((m), " " #flag);		\ +		}						\ +	} while (0) + +/** + * amdgpu_bo_print_info - print BO info in debugfs file + * + * @id: Index or Id of the BO + * @bo: Requested BO for printing info + * @m: debugfs file + * + * Print BO information in debugfs file + * + * Returns: + * Size of the BO in bytes. + */ +u64 amdgpu_bo_print_info(int id, struct amdgpu_bo *bo, struct seq_file *m) +{ +	struct dma_buf_attachment *attachment; +	struct dma_buf *dma_buf; +	unsigned int domain; +	const char *placement; +	unsigned int pin_count; +	u64 size; + +	domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type); +	switch (domain) { +	case AMDGPU_GEM_DOMAIN_VRAM: +		placement = "VRAM"; +		break; +	case AMDGPU_GEM_DOMAIN_GTT: +		placement = " GTT"; +		break; +	case AMDGPU_GEM_DOMAIN_CPU: +	default: +		placement = " CPU"; +		break; +	} + +	size = amdgpu_bo_size(bo); +	seq_printf(m, "\t\t0x%08x: %12lld byte %s", +			id, size, placement); + +	pin_count = READ_ONCE(bo->tbo.pin_count); +	if (pin_count) +		seq_printf(m, " pin count %d", pin_count); + +	dma_buf = READ_ONCE(bo->tbo.base.dma_buf); +	attachment = READ_ONCE(bo->tbo.base.import_attach); + +	if (attachment) +		seq_printf(m, " imported from %p", dma_buf); +	else if (dma_buf) +		seq_printf(m, " exported as %p", dma_buf); + +	amdgpu_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED); +	amdgpu_bo_print_flag(m, bo, NO_CPU_ACCESS); +	amdgpu_bo_print_flag(m, bo, CPU_GTT_USWC); +	amdgpu_bo_print_flag(m, bo, VRAM_CLEARED); +	amdgpu_bo_print_flag(m, bo, VRAM_CONTIGUOUS); +	amdgpu_bo_print_flag(m, bo, VM_ALWAYS_VALID); +	amdgpu_bo_print_flag(m, bo, EXPLICIT_SYNC); + +	seq_puts(m, "\n"); + +	return size; +} +#endif |