projects/linux·blob master · refs

153 lines · 4.4 KiB

1// SPDX-License-Identifier: GPL-2.0 OR MIT
2/*
3 * Copyright 2016-2021 Advanced Micro Devices, Inc.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors: Christian König, Felix Kuehling
24 */
25
26#include "amdgpu.h"
27
28/**
29 * DOC: mem_info_preempt_used
30 *
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
35 */
36static ssize_t mem_info_preempt_used_show(struct device *dev,
37 struct device_attribute *attr,
38 char *buf)
39{
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;
43
44 return sysfs_emit(buf, "%llu\n", ttm_resource_manager_usage(man));
45}
46
47static DEVICE_ATTR_RO(mem_info_preempt_used);
48
49/**
50 * amdgpu_preempt_mgr_sysfs_fini - remove PREEMPT manager sysfs attributes
51 *
52 * @adev: amdgpu_device pointer
53 */
54void amdgpu_preempt_mgr_sysfs_fini(struct amdgpu_device *adev)
55{
56 if (adev->dev->kobj.sd)
57 device_remove_file(adev->dev, &dev_attr_mem_info_preempt_used);
58}
59
60/**
61 * amdgpu_preempt_mgr_new - allocate a new node
62 *
63 * @man: TTM memory type manager
64 * @tbo: TTM BO we need this range for
65 * @place: placement flags and restrictions
66 * @res: TTM memory object
67 *
68 * Dummy, just count the space used without allocating resources or any limit.
69 */
70static int amdgpu_preempt_mgr_new(struct ttm_resource_manager *man,
71 struct ttm_buffer_object *tbo,
72 const struct ttm_place *place,
73 struct ttm_resource **res)
74{
75 *res = kzalloc_obj(**res);
76 if (!*res)
77 return -ENOMEM;
78
79 ttm_resource_init(tbo, place, *res);
80 (*res)->start = AMDGPU_BO_INVALID_OFFSET;
81 return 0;
82}
83
84/**
85 * amdgpu_preempt_mgr_del - free ranges
86 *
87 * @man: TTM memory type manager
88 * @res: TTM memory object
89 *
90 * Free the allocated GTT again.
91 */
92static void amdgpu_preempt_mgr_del(struct ttm_resource_manager *man,
93 struct ttm_resource *res)
94{
95 ttm_resource_fini(man, res);
96 kfree(res);
97}
98
99static const struct ttm_resource_manager_func amdgpu_preempt_mgr_func = {
100 .alloc = amdgpu_preempt_mgr_new,
101 .free = amdgpu_preempt_mgr_del,
102};
103
104/**
105 * amdgpu_preempt_mgr_init - init PREEMPT manager and DRM MM
106 *
107 * @adev: amdgpu_device pointer
108 *
109 * Allocate and initialize the GTT manager.
110 */
111int amdgpu_preempt_mgr_init(struct amdgpu_device *adev)
112{
113 struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
114 int ret;
115
116 man->use_tt = true;
117 man->func = &amdgpu_preempt_mgr_func;
118
119 ttm_resource_manager_init(man, &adev->mman.bdev, (1 << 30));
120
121 ret = device_create_file(adev->dev, &dev_attr_mem_info_preempt_used);
122 if (ret) {
123 DRM_ERROR("Failed to create device file mem_info_preempt_used\n");
124 return ret;
125 }
126
127 ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, man);
128 ttm_resource_manager_set_used(man, true);
129 return 0;
130}
131
132/**
133 * amdgpu_preempt_mgr_fini - free and destroy GTT manager
134 *
135 * @adev: amdgpu_device pointer
136 *
137 * Destroy and free the GTT manager, returns -EBUSY if ranges are still
138 * allocated inside it.
139 */
140void amdgpu_preempt_mgr_fini(struct amdgpu_device *adev)
141{
142 struct ttm_resource_manager *man = &adev->mman.preempt_mgr;
143 int ret;
144
145 ttm_resource_manager_set_used(man, false);
146
147 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
148 if (ret)
149 return;
150
151 ttm_resource_manager_cleanup(man);
152 ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, NULL);
153}