1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2016-2021 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: Christian König, Felix Kuehling
29 * DOC: mem_info_preempt_used
31 * The amdgpu driver provides a sysfs API for reporting current total amount of
32 * used preemptible memory.
33 * The file mem_info_preempt_used is used for this, and returns the current
34 * used size of the preemptible block, in bytes
36 static ssize_t mem_info_preempt_used_show(struct device *dev,
37 struct device_attribute *attr,
40 struct drm_device *ddev = dev_get_drvdata(dev);
41 struct amdgpu_device *adev = drm_to_adev(ddev);
42 struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
44 return sysfs_emit(buf, "%llu\n", ttm_resource_manager_usage(man));
47 static DEVICE_ATTR_RO(mem_info_preempt_used);
50 * amdgpu_preempt_mgr_new - allocate a new node
52 * @man: TTM memory type manager
53 * @tbo: TTM BO we need this range for
54 * @place: placement flags and restrictions
55 * @res: TTM memory object
57 * Dummy, just count the space used without allocating resources or any limit.
59 static int amdgpu_preempt_mgr_new(struct ttm_resource_manager *man,
60 struct ttm_buffer_object *tbo,
61 const struct ttm_place *place,
62 struct ttm_resource **res)
64 *res = kzalloc(sizeof(**res), GFP_KERNEL);
68 ttm_resource_init(tbo, place, *res);
69 (*res)->start = AMDGPU_BO_INVALID_OFFSET;
74 * amdgpu_preempt_mgr_del - free ranges
76 * @man: TTM memory type manager
77 * @res: TTM memory object
79 * Free the allocated GTT again.
81 static void amdgpu_preempt_mgr_del(struct ttm_resource_manager *man,
82 struct ttm_resource *res)
84 ttm_resource_fini(man, res);
88 static const struct ttm_resource_manager_func amdgpu_preempt_mgr_func = {
89 .alloc = amdgpu_preempt_mgr_new,
90 .free = amdgpu_preempt_mgr_del,
94 * amdgpu_preempt_mgr_init - init PREEMPT manager and DRM MM
96 * @adev: amdgpu_device pointer
98 * Allocate and initialize the GTT manager.
100 int amdgpu_preempt_mgr_init(struct amdgpu_device *adev)
102 struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
106 man->func = &amdgpu_preempt_mgr_func;
108 ttm_resource_manager_init(man, &adev->mman.bdev, (1 << 30));
110 ret = device_create_file(adev->dev, &dev_attr_mem_info_preempt_used);
112 DRM_ERROR("Failed to create device file mem_info_preempt_used\n");
116 ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, man);
117 ttm_resource_manager_set_used(man, true);
122 * amdgpu_preempt_mgr_fini - free and destroy GTT manager
124 * @adev: amdgpu_device pointer
126 * Destroy and free the GTT manager, returns -EBUSY if ranges are still
127 * allocated inside it.
129 void amdgpu_preempt_mgr_fini(struct amdgpu_device *adev)
131 struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
134 ttm_resource_manager_set_used(man, false);
136 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
140 if (adev->dev->kobj.sd)
141 device_remove_file(adev->dev, &dev_attr_mem_info_preempt_used);
143 ttm_resource_manager_cleanup(man);
144 ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, NULL);