xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_preempt_mgr.c (revision 1bb76ff151c0aba8e3312a604e4cd2e5195cf4b7)
15ca02815Sjsg // SPDX-License-Identifier: GPL-2.0 OR MIT
25ca02815Sjsg /*
35ca02815Sjsg  * Copyright 2016-2021 Advanced Micro Devices, Inc.
45ca02815Sjsg  *
55ca02815Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
65ca02815Sjsg  * copy of this software and associated documentation files (the "Software"),
75ca02815Sjsg  * to deal in the Software without restriction, including without limitation
85ca02815Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
95ca02815Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
105ca02815Sjsg  * Software is furnished to do so, subject to the following conditions:
115ca02815Sjsg  *
125ca02815Sjsg  * The above copyright notice and this permission notice shall be included in
135ca02815Sjsg  * all copies or substantial portions of the Software.
145ca02815Sjsg  *
155ca02815Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
165ca02815Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
175ca02815Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
185ca02815Sjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
195ca02815Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
205ca02815Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
215ca02815Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
225ca02815Sjsg  *
235ca02815Sjsg  * Authors: Christian König, Felix Kuehling
245ca02815Sjsg  */
255ca02815Sjsg 
265ca02815Sjsg #include "amdgpu.h"
275ca02815Sjsg 
285ca02815Sjsg /**
295ca02815Sjsg  * DOC: mem_info_preempt_used
305ca02815Sjsg  *
315ca02815Sjsg  * The amdgpu driver provides a sysfs API for reporting current total amount of
325ca02815Sjsg  * used preemptible memory.
335ca02815Sjsg  * The file mem_info_preempt_used is used for this, and returns the current
345ca02815Sjsg  * used size of the preemptible block, in bytes
355ca02815Sjsg  */
mem_info_preempt_used_show(struct device * dev,struct device_attribute * attr,char * buf)365ca02815Sjsg static ssize_t mem_info_preempt_used_show(struct device *dev,
375ca02815Sjsg 					  struct device_attribute *attr,
385ca02815Sjsg 					  char *buf)
395ca02815Sjsg {
405ca02815Sjsg 	struct drm_device *ddev = dev_get_drvdata(dev);
415ca02815Sjsg 	struct amdgpu_device *adev = drm_to_adev(ddev);
42*1bb76ff1Sjsg 	struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
435ca02815Sjsg 
44*1bb76ff1Sjsg 	return sysfs_emit(buf, "%llu\n", ttm_resource_manager_usage(man));
455ca02815Sjsg }
465ca02815Sjsg 
475ca02815Sjsg static DEVICE_ATTR_RO(mem_info_preempt_used);
485ca02815Sjsg 
495ca02815Sjsg /**
505ca02815Sjsg  * amdgpu_preempt_mgr_new - allocate a new node
515ca02815Sjsg  *
525ca02815Sjsg  * @man: TTM memory type manager
535ca02815Sjsg  * @tbo: TTM BO we need this range for
545ca02815Sjsg  * @place: placement flags and restrictions
55*1bb76ff1Sjsg  * @res: TTM memory object
565ca02815Sjsg  *
575ca02815Sjsg  * Dummy, just count the space used without allocating resources or any limit.
585ca02815Sjsg  */
amdgpu_preempt_mgr_new(struct ttm_resource_manager * man,struct ttm_buffer_object * tbo,const struct ttm_place * place,struct ttm_resource ** res)595ca02815Sjsg static int amdgpu_preempt_mgr_new(struct ttm_resource_manager *man,
605ca02815Sjsg 				  struct ttm_buffer_object *tbo,
615ca02815Sjsg 				  const struct ttm_place *place,
625ca02815Sjsg 				  struct ttm_resource **res)
635ca02815Sjsg {
645ca02815Sjsg 	*res = kzalloc(sizeof(**res), GFP_KERNEL);
655ca02815Sjsg 	if (!*res)
665ca02815Sjsg 		return -ENOMEM;
675ca02815Sjsg 
685ca02815Sjsg 	ttm_resource_init(tbo, place, *res);
695ca02815Sjsg 	(*res)->start = AMDGPU_BO_INVALID_OFFSET;
705ca02815Sjsg 	return 0;
715ca02815Sjsg }
725ca02815Sjsg 
735ca02815Sjsg /**
745ca02815Sjsg  * amdgpu_preempt_mgr_del - free ranges
755ca02815Sjsg  *
765ca02815Sjsg  * @man: TTM memory type manager
77*1bb76ff1Sjsg  * @res: TTM memory object
785ca02815Sjsg  *
795ca02815Sjsg  * Free the allocated GTT again.
805ca02815Sjsg  */
amdgpu_preempt_mgr_del(struct ttm_resource_manager * man,struct ttm_resource * res)815ca02815Sjsg static void amdgpu_preempt_mgr_del(struct ttm_resource_manager *man,
825ca02815Sjsg 				   struct ttm_resource *res)
835ca02815Sjsg {
84*1bb76ff1Sjsg 	ttm_resource_fini(man, res);
855ca02815Sjsg 	kfree(res);
865ca02815Sjsg }
875ca02815Sjsg 
885ca02815Sjsg static const struct ttm_resource_manager_func amdgpu_preempt_mgr_func = {
895ca02815Sjsg 	.alloc = amdgpu_preempt_mgr_new,
905ca02815Sjsg 	.free = amdgpu_preempt_mgr_del,
915ca02815Sjsg };
925ca02815Sjsg 
935ca02815Sjsg /**
945ca02815Sjsg  * amdgpu_preempt_mgr_init - init PREEMPT manager and DRM MM
955ca02815Sjsg  *
965ca02815Sjsg  * @adev: amdgpu_device pointer
975ca02815Sjsg  *
985ca02815Sjsg  * Allocate and initialize the GTT manager.
995ca02815Sjsg  */
amdgpu_preempt_mgr_init(struct amdgpu_device * adev)1005ca02815Sjsg int amdgpu_preempt_mgr_init(struct amdgpu_device *adev)
1015ca02815Sjsg {
102*1bb76ff1Sjsg 	struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
1035ca02815Sjsg 	int ret;
1045ca02815Sjsg 
1055ca02815Sjsg 	man->use_tt = true;
1065ca02815Sjsg 	man->func = &amdgpu_preempt_mgr_func;
1075ca02815Sjsg 
108*1bb76ff1Sjsg 	ttm_resource_manager_init(man, &adev->mman.bdev, (1 << 30));
1095ca02815Sjsg 
1105ca02815Sjsg 	ret = device_create_file(adev->dev, &dev_attr_mem_info_preempt_used);
1115ca02815Sjsg 	if (ret) {
1125ca02815Sjsg 		DRM_ERROR("Failed to create device file mem_info_preempt_used\n");
1135ca02815Sjsg 		return ret;
1145ca02815Sjsg 	}
1155ca02815Sjsg 
116*1bb76ff1Sjsg 	ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, man);
1175ca02815Sjsg 	ttm_resource_manager_set_used(man, true);
1185ca02815Sjsg 	return 0;
1195ca02815Sjsg }
1205ca02815Sjsg 
1215ca02815Sjsg /**
1225ca02815Sjsg  * amdgpu_preempt_mgr_fini - free and destroy GTT manager
1235ca02815Sjsg  *
1245ca02815Sjsg  * @adev: amdgpu_device pointer
1255ca02815Sjsg  *
1265ca02815Sjsg  * Destroy and free the GTT manager, returns -EBUSY if ranges are still
1275ca02815Sjsg  * allocated inside it.
1285ca02815Sjsg  */
amdgpu_preempt_mgr_fini(struct amdgpu_device * adev)1295ca02815Sjsg void amdgpu_preempt_mgr_fini(struct amdgpu_device *adev)
1305ca02815Sjsg {
131*1bb76ff1Sjsg 	struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
1325ca02815Sjsg 	int ret;
1335ca02815Sjsg 
1345ca02815Sjsg 	ttm_resource_manager_set_used(man, false);
1355ca02815Sjsg 
1365ca02815Sjsg 	ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
1375ca02815Sjsg 	if (ret)
1385ca02815Sjsg 		return;
1395ca02815Sjsg 
1405ca02815Sjsg 	device_remove_file(adev->dev, &dev_attr_mem_info_preempt_used);
1415ca02815Sjsg 
1425ca02815Sjsg 	ttm_resource_manager_cleanup(man);
1435ca02815Sjsg 	ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, NULL);
1445ca02815Sjsg }
145