OSDN Git Service

Merge tag 'drm-misc-next-2022-01-27' of git://anongit.freedesktop.org/drm/drm-misc...
[uclinux-h8/linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_preempt_mgr.c
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 static inline struct amdgpu_preempt_mgr *
29 to_preempt_mgr(struct ttm_resource_manager *man)
30 {
31         return container_of(man, struct amdgpu_preempt_mgr, manager);
32 }
33
34 /**
35  * DOC: mem_info_preempt_used
36  *
37  * The amdgpu driver provides a sysfs API for reporting current total amount of
38  * used preemptible memory.
39  * The file mem_info_preempt_used is used for this, and returns the current
40  * used size of the preemptible block, in bytes
41  */
42 static ssize_t mem_info_preempt_used_show(struct device *dev,
43                                           struct device_attribute *attr,
44                                           char *buf)
45 {
46         struct drm_device *ddev = dev_get_drvdata(dev);
47         struct amdgpu_device *adev = drm_to_adev(ddev);
48         struct ttm_resource_manager *man;
49
50         man = ttm_manager_type(&adev->mman.bdev, AMDGPU_PL_PREEMPT);
51         return sysfs_emit(buf, "%llu\n", amdgpu_preempt_mgr_usage(man));
52 }
53
54 static DEVICE_ATTR_RO(mem_info_preempt_used);
55
56 /**
57  * amdgpu_preempt_mgr_new - allocate a new node
58  *
59  * @man: TTM memory type manager
60  * @tbo: TTM BO we need this range for
61  * @place: placement flags and restrictions
62  * @res: TTM memory object
63  *
64  * Dummy, just count the space used without allocating resources or any limit.
65  */
66 static int amdgpu_preempt_mgr_new(struct ttm_resource_manager *man,
67                                   struct ttm_buffer_object *tbo,
68                                   const struct ttm_place *place,
69                                   struct ttm_resource **res)
70 {
71         struct amdgpu_preempt_mgr *mgr = to_preempt_mgr(man);
72
73         *res = kzalloc(sizeof(**res), GFP_KERNEL);
74         if (!*res)
75                 return -ENOMEM;
76
77         ttm_resource_init(tbo, place, *res);
78         (*res)->start = AMDGPU_BO_INVALID_OFFSET;
79
80         atomic64_add((*res)->num_pages, &mgr->used);
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  */
92 static void amdgpu_preempt_mgr_del(struct ttm_resource_manager *man,
93                                    struct ttm_resource *res)
94 {
95         struct amdgpu_preempt_mgr *mgr = to_preempt_mgr(man);
96
97         atomic64_sub(res->num_pages, &mgr->used);
98         ttm_resource_fini(man, res);
99         kfree(res);
100 }
101
102 /**
103  * amdgpu_preempt_mgr_usage - return usage of PREEMPT domain
104  *
105  * @man: TTM memory type manager
106  *
107  * Return how many bytes are used in the GTT domain
108  */
109 uint64_t amdgpu_preempt_mgr_usage(struct ttm_resource_manager *man)
110 {
111         struct amdgpu_preempt_mgr *mgr = to_preempt_mgr(man);
112         s64 result = atomic64_read(&mgr->used);
113
114         return (result > 0 ? result : 0) * PAGE_SIZE;
115 }
116
117 /**
118  * amdgpu_preempt_mgr_debug - dump VRAM table
119  *
120  * @man: TTM memory type manager
121  * @printer: DRM printer to use
122  *
123  * Dump the table content using printk.
124  */
125 static void amdgpu_preempt_mgr_debug(struct ttm_resource_manager *man,
126                                      struct drm_printer *printer)
127 {
128         struct amdgpu_preempt_mgr *mgr = to_preempt_mgr(man);
129
130         drm_printf(printer, "man size:%llu pages, preempt used:%lld pages\n",
131                    man->size, (u64)atomic64_read(&mgr->used));
132 }
133
134 static const struct ttm_resource_manager_func amdgpu_preempt_mgr_func = {
135         .alloc = amdgpu_preempt_mgr_new,
136         .free = amdgpu_preempt_mgr_del,
137         .debug = amdgpu_preempt_mgr_debug
138 };
139
140 /**
141  * amdgpu_preempt_mgr_init - init PREEMPT manager and DRM MM
142  *
143  * @adev: amdgpu_device pointer
144  *
145  * Allocate and initialize the GTT manager.
146  */
147 int amdgpu_preempt_mgr_init(struct amdgpu_device *adev)
148 {
149         struct amdgpu_preempt_mgr *mgr = &adev->mman.preempt_mgr;
150         struct ttm_resource_manager *man = &mgr->manager;
151         int ret;
152
153         man->use_tt = true;
154         man->func = &amdgpu_preempt_mgr_func;
155
156         ttm_resource_manager_init(man, &adev->mman.bdev, (1 << 30));
157
158         atomic64_set(&mgr->used, 0);
159
160         ret = device_create_file(adev->dev, &dev_attr_mem_info_preempt_used);
161         if (ret) {
162                 DRM_ERROR("Failed to create device file mem_info_preempt_used\n");
163                 return ret;
164         }
165
166         ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT,
167                                &mgr->manager);
168         ttm_resource_manager_set_used(man, true);
169         return 0;
170 }
171
172 /**
173  * amdgpu_preempt_mgr_fini - free and destroy GTT manager
174  *
175  * @adev: amdgpu_device pointer
176  *
177  * Destroy and free the GTT manager, returns -EBUSY if ranges are still
178  * allocated inside it.
179  */
180 void amdgpu_preempt_mgr_fini(struct amdgpu_device *adev)
181 {
182         struct amdgpu_preempt_mgr *mgr = &adev->mman.preempt_mgr;
183         struct ttm_resource_manager *man = &mgr->manager;
184         int ret;
185
186         ttm_resource_manager_set_used(man, false);
187
188         ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man);
189         if (ret)
190                 return;
191
192         device_remove_file(adev->dev, &dev_attr_mem_info_preempt_used);
193
194         ttm_resource_manager_cleanup(man);
195         ttm_set_driver_manager(&adev->mman.bdev, AMDGPU_PL_PREEMPT, NULL);
196 }