blob: c1d9e2772cbc02b180ec6517836e51219e2668cb [file] [log] [blame]
Oded Gabbay4a488a72014-07-16 21:08:55 +03001/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
Oded Gabbay4a488a72014-07-16 21:08:55 +030023#include <linux/bsearch.h>
24#include <linux/pci.h>
25#include <linux/slab.h>
26#include "kfd_priv.h"
Ben Goz64c7f8c2014-07-17 01:27:00 +030027#include "kfd_device_queue_manager.h"
Felix Kuehling507968d2017-08-15 23:00:15 -040028#include "kfd_pm4_headers_vi.h"
Felix Kuehling373d7082017-11-14 16:41:19 -050029#include "cwsr_trap_handler_gfx8.asm"
Felix Kuehling3e76c232018-04-10 17:33:16 -040030#include "cwsr_trap_handler_gfx9.asm"
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050031#include "kfd_iommu.h"
Oded Gabbay4a488a72014-07-16 21:08:55 +030032
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030033#define MQD_SIZE_ALIGNED 768
Felix Kuehling26103432018-02-06 20:32:45 -050034static atomic_t kfd_device_suspended = ATOMIC_INIT(0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030035
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050036#ifdef KFD_SUPPORT_IOMMU_V2
Oded Gabbay4a488a72014-07-16 21:08:55 +030037static const struct kfd_device_info kaveri_device_info = {
Ben Goz0da75582015-01-01 17:10:01 +020038 .asic_family = CHIP_KAVERI,
39 .max_pasid_bits = 16,
Yair Shachar992839a2015-05-20 13:43:04 +030040 /* max num of queues for KV.TODO should be a dynamic value */
41 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040042 .doorbell_size = 4,
Ben Goz0da75582015-01-01 17:10:01 +020043 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030044 .event_interrupt_class = &event_interrupt_class_cik,
Yair Shacharfbeb6612015-05-20 13:48:26 +030045 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050046 .mqd_size_aligned = MQD_SIZE_ALIGNED,
47 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050048 .needs_iommu_device = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050049 .needs_pci_atomics = false,
Ben Goz0da75582015-01-01 17:10:01 +020050};
51
52static const struct kfd_device_info carrizo_device_info = {
53 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030054 .max_pasid_bits = 16,
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030055 /* max num of queues for CZ.TODO should be a dynamic value */
56 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040057 .doorbell_size = 4,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030058 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030059 .event_interrupt_class = &event_interrupt_class_cik,
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030060 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050061 .mqd_size_aligned = MQD_SIZE_ALIGNED,
62 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050063 .needs_iommu_device = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050064 .needs_pci_atomics = false,
Oded Gabbay4a488a72014-07-16 21:08:55 +030065};
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050066#endif
Oded Gabbay4a488a72014-07-16 21:08:55 +030067
Felix Kuehlinga3084e62018-01-04 17:17:47 -050068static const struct kfd_device_info hawaii_device_info = {
69 .asic_family = CHIP_HAWAII,
70 .max_pasid_bits = 16,
71 /* max num of queues for KV.TODO should be a dynamic value */
72 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040073 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050074 .ih_ring_entry_size = 4 * sizeof(uint32_t),
75 .event_interrupt_class = &event_interrupt_class_cik,
76 .num_of_watch_points = 4,
77 .mqd_size_aligned = MQD_SIZE_ALIGNED,
78 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050079 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050080 .needs_pci_atomics = false,
81};
82
83static const struct kfd_device_info tonga_device_info = {
84 .asic_family = CHIP_TONGA,
85 .max_pasid_bits = 16,
86 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040087 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050088 .ih_ring_entry_size = 4 * sizeof(uint32_t),
89 .event_interrupt_class = &event_interrupt_class_cik,
90 .num_of_watch_points = 4,
91 .mqd_size_aligned = MQD_SIZE_ALIGNED,
92 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050093 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050094 .needs_pci_atomics = true,
95};
96
97static const struct kfd_device_info tonga_vf_device_info = {
98 .asic_family = CHIP_TONGA,
99 .max_pasid_bits = 16,
100 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400101 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500102 .ih_ring_entry_size = 4 * sizeof(uint32_t),
103 .event_interrupt_class = &event_interrupt_class_cik,
104 .num_of_watch_points = 4,
105 .mqd_size_aligned = MQD_SIZE_ALIGNED,
106 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500107 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500108 .needs_pci_atomics = false,
109};
110
111static const struct kfd_device_info fiji_device_info = {
112 .asic_family = CHIP_FIJI,
113 .max_pasid_bits = 16,
114 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400115 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500116 .ih_ring_entry_size = 4 * sizeof(uint32_t),
117 .event_interrupt_class = &event_interrupt_class_cik,
118 .num_of_watch_points = 4,
119 .mqd_size_aligned = MQD_SIZE_ALIGNED,
120 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500121 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500122 .needs_pci_atomics = true,
123};
124
125static const struct kfd_device_info fiji_vf_device_info = {
126 .asic_family = CHIP_FIJI,
127 .max_pasid_bits = 16,
128 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400129 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500130 .ih_ring_entry_size = 4 * sizeof(uint32_t),
131 .event_interrupt_class = &event_interrupt_class_cik,
132 .num_of_watch_points = 4,
133 .mqd_size_aligned = MQD_SIZE_ALIGNED,
134 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500135 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500136 .needs_pci_atomics = false,
137};
138
139
140static const struct kfd_device_info polaris10_device_info = {
141 .asic_family = CHIP_POLARIS10,
142 .max_pasid_bits = 16,
143 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400144 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500145 .ih_ring_entry_size = 4 * sizeof(uint32_t),
146 .event_interrupt_class = &event_interrupt_class_cik,
147 .num_of_watch_points = 4,
148 .mqd_size_aligned = MQD_SIZE_ALIGNED,
149 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500150 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500151 .needs_pci_atomics = true,
152};
153
154static const struct kfd_device_info polaris10_vf_device_info = {
155 .asic_family = CHIP_POLARIS10,
156 .max_pasid_bits = 16,
157 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400158 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500159 .ih_ring_entry_size = 4 * sizeof(uint32_t),
160 .event_interrupt_class = &event_interrupt_class_cik,
161 .num_of_watch_points = 4,
162 .mqd_size_aligned = MQD_SIZE_ALIGNED,
163 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500164 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500165 .needs_pci_atomics = false,
166};
167
168static const struct kfd_device_info polaris11_device_info = {
169 .asic_family = CHIP_POLARIS11,
170 .max_pasid_bits = 16,
171 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400172 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500173 .ih_ring_entry_size = 4 * sizeof(uint32_t),
174 .event_interrupt_class = &event_interrupt_class_cik,
175 .num_of_watch_points = 4,
176 .mqd_size_aligned = MQD_SIZE_ALIGNED,
177 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500178 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500179 .needs_pci_atomics = true,
180};
181
Felix Kuehling389056e2018-04-10 17:33:18 -0400182static const struct kfd_device_info vega10_device_info = {
183 .asic_family = CHIP_VEGA10,
184 .max_pasid_bits = 16,
185 .max_no_of_hqd = 24,
186 .doorbell_size = 8,
187 .ih_ring_entry_size = 8 * sizeof(uint32_t),
188 .event_interrupt_class = &event_interrupt_class_v9,
189 .num_of_watch_points = 4,
190 .mqd_size_aligned = MQD_SIZE_ALIGNED,
191 .supports_cwsr = true,
192 .needs_iommu_device = false,
193 .needs_pci_atomics = false,
194};
195
196static const struct kfd_device_info vega10_vf_device_info = {
197 .asic_family = CHIP_VEGA10,
198 .max_pasid_bits = 16,
199 .max_no_of_hqd = 24,
200 .doorbell_size = 8,
201 .ih_ring_entry_size = 8 * sizeof(uint32_t),
202 .event_interrupt_class = &event_interrupt_class_v9,
203 .num_of_watch_points = 4,
204 .mqd_size_aligned = MQD_SIZE_ALIGNED,
205 .supports_cwsr = true,
206 .needs_iommu_device = false,
207 .needs_pci_atomics = false,
208};
209
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500210
Oded Gabbay4a488a72014-07-16 21:08:55 +0300211struct kfd_deviceid {
212 unsigned short did;
213 const struct kfd_device_info *device_info;
214};
215
Oded Gabbay4a488a72014-07-16 21:08:55 +0300216static const struct kfd_deviceid supported_devices[] = {
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500217#ifdef KFD_SUPPORT_IOMMU_V2
Oded Gabbay4a488a72014-07-16 21:08:55 +0300218 { 0x1304, &kaveri_device_info }, /* Kaveri */
219 { 0x1305, &kaveri_device_info }, /* Kaveri */
220 { 0x1306, &kaveri_device_info }, /* Kaveri */
221 { 0x1307, &kaveri_device_info }, /* Kaveri */
222 { 0x1309, &kaveri_device_info }, /* Kaveri */
223 { 0x130A, &kaveri_device_info }, /* Kaveri */
224 { 0x130B, &kaveri_device_info }, /* Kaveri */
225 { 0x130C, &kaveri_device_info }, /* Kaveri */
226 { 0x130D, &kaveri_device_info }, /* Kaveri */
227 { 0x130E, &kaveri_device_info }, /* Kaveri */
228 { 0x130F, &kaveri_device_info }, /* Kaveri */
229 { 0x1310, &kaveri_device_info }, /* Kaveri */
230 { 0x1311, &kaveri_device_info }, /* Kaveri */
231 { 0x1312, &kaveri_device_info }, /* Kaveri */
232 { 0x1313, &kaveri_device_info }, /* Kaveri */
233 { 0x1315, &kaveri_device_info }, /* Kaveri */
234 { 0x1316, &kaveri_device_info }, /* Kaveri */
235 { 0x1317, &kaveri_device_info }, /* Kaveri */
236 { 0x1318, &kaveri_device_info }, /* Kaveri */
237 { 0x131B, &kaveri_device_info }, /* Kaveri */
238 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz123576d2015-01-12 14:37:24 +0200239 { 0x131D, &kaveri_device_info }, /* Kaveri */
240 { 0x9870, &carrizo_device_info }, /* Carrizo */
241 { 0x9874, &carrizo_device_info }, /* Carrizo */
242 { 0x9875, &carrizo_device_info }, /* Carrizo */
243 { 0x9876, &carrizo_device_info }, /* Carrizo */
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500244 { 0x9877, &carrizo_device_info }, /* Carrizo */
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500245#endif
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500246 { 0x67A0, &hawaii_device_info }, /* Hawaii */
247 { 0x67A1, &hawaii_device_info }, /* Hawaii */
248 { 0x67A2, &hawaii_device_info }, /* Hawaii */
249 { 0x67A8, &hawaii_device_info }, /* Hawaii */
250 { 0x67A9, &hawaii_device_info }, /* Hawaii */
251 { 0x67AA, &hawaii_device_info }, /* Hawaii */
252 { 0x67B0, &hawaii_device_info }, /* Hawaii */
253 { 0x67B1, &hawaii_device_info }, /* Hawaii */
254 { 0x67B8, &hawaii_device_info }, /* Hawaii */
255 { 0x67B9, &hawaii_device_info }, /* Hawaii */
256 { 0x67BA, &hawaii_device_info }, /* Hawaii */
257 { 0x67BE, &hawaii_device_info }, /* Hawaii */
258 { 0x6920, &tonga_device_info }, /* Tonga */
259 { 0x6921, &tonga_device_info }, /* Tonga */
260 { 0x6928, &tonga_device_info }, /* Tonga */
261 { 0x6929, &tonga_device_info }, /* Tonga */
262 { 0x692B, &tonga_device_info }, /* Tonga */
263 { 0x692F, &tonga_vf_device_info }, /* Tonga vf */
264 { 0x6938, &tonga_device_info }, /* Tonga */
265 { 0x6939, &tonga_device_info }, /* Tonga */
266 { 0x7300, &fiji_device_info }, /* Fiji */
267 { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/
268 { 0x67C0, &polaris10_device_info }, /* Polaris10 */
269 { 0x67C1, &polaris10_device_info }, /* Polaris10 */
270 { 0x67C2, &polaris10_device_info }, /* Polaris10 */
271 { 0x67C4, &polaris10_device_info }, /* Polaris10 */
272 { 0x67C7, &polaris10_device_info }, /* Polaris10 */
273 { 0x67C8, &polaris10_device_info }, /* Polaris10 */
274 { 0x67C9, &polaris10_device_info }, /* Polaris10 */
275 { 0x67CA, &polaris10_device_info }, /* Polaris10 */
276 { 0x67CC, &polaris10_device_info }, /* Polaris10 */
277 { 0x67CF, &polaris10_device_info }, /* Polaris10 */
278 { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/
279 { 0x67DF, &polaris10_device_info }, /* Polaris10 */
280 { 0x67E0, &polaris11_device_info }, /* Polaris11 */
281 { 0x67E1, &polaris11_device_info }, /* Polaris11 */
282 { 0x67E3, &polaris11_device_info }, /* Polaris11 */
283 { 0x67E7, &polaris11_device_info }, /* Polaris11 */
284 { 0x67E8, &polaris11_device_info }, /* Polaris11 */
285 { 0x67E9, &polaris11_device_info }, /* Polaris11 */
286 { 0x67EB, &polaris11_device_info }, /* Polaris11 */
287 { 0x67EF, &polaris11_device_info }, /* Polaris11 */
288 { 0x67FF, &polaris11_device_info }, /* Polaris11 */
Felix Kuehling389056e2018-04-10 17:33:18 -0400289 { 0x6860, &vega10_device_info }, /* Vega10 */
290 { 0x6861, &vega10_device_info }, /* Vega10 */
291 { 0x6862, &vega10_device_info }, /* Vega10 */
292 { 0x6863, &vega10_device_info }, /* Vega10 */
293 { 0x6864, &vega10_device_info }, /* Vega10 */
294 { 0x6867, &vega10_device_info }, /* Vega10 */
295 { 0x6868, &vega10_device_info }, /* Vega10 */
296 { 0x686C, &vega10_vf_device_info }, /* Vega10 vf*/
297 { 0x687F, &vega10_device_info }, /* Vega10 */
Oded Gabbay4a488a72014-07-16 21:08:55 +0300298};
299
Oded Gabbay6e810902014-10-27 14:36:07 +0200300static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
301 unsigned int chunk_size);
302static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
303
Yong Zhaob8935a72017-09-20 18:10:13 -0400304static int kfd_resume(struct kfd_dev *kfd);
305
Oded Gabbay4a488a72014-07-16 21:08:55 +0300306static const struct kfd_device_info *lookup_device_info(unsigned short did)
307{
308 size_t i;
309
310 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
311 if (supported_devices[i].did == did) {
Felix Kuehling32fa8212017-08-15 23:00:12 -0400312 WARN_ON(!supported_devices[i].device_info);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300313 return supported_devices[i].device_info;
314 }
315 }
316
Yong Zhao4ebc7182017-08-15 23:00:13 -0400317 dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
318 did);
319
Oded Gabbay4a488a72014-07-16 21:08:55 +0300320 return NULL;
321}
322
Xihan Zhangcea405b2015-03-17 19:32:53 +0800323struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
324 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300325{
326 struct kfd_dev *kfd;
welu6106dce2018-04-10 17:33:17 -0400327 int ret;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300328 const struct kfd_device_info *device_info =
329 lookup_device_info(pdev->device);
330
Yong Zhao4ebc7182017-08-15 23:00:13 -0400331 if (!device_info) {
332 dev_err(kfd_device, "kgd2kfd_probe failed\n");
Oded Gabbay4a488a72014-07-16 21:08:55 +0300333 return NULL;
Yong Zhao4ebc7182017-08-15 23:00:13 -0400334 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300335
welu6106dce2018-04-10 17:33:17 -0400336 /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
337 * 32 and 64-bit requests are possible and must be
338 * supported.
339 */
340 ret = pci_enable_atomic_ops_to_root(pdev,
341 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
342 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
343 if (device_info->needs_pci_atomics && ret < 0) {
344 dev_info(kfd_device,
345 "skipped device %x:%x, PCI rejects atomics\n",
346 pdev->vendor, pdev->device);
347 return NULL;
Felix Kuehling3ee2d002018-01-04 17:17:41 -0500348 }
349
Oded Gabbay4a488a72014-07-16 21:08:55 +0300350 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
351 if (!kfd)
352 return NULL;
353
354 kfd->kgd = kgd;
355 kfd->device_info = device_info;
356 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300357 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800358 kfd->kfd2kgd = f2g;
359
360 mutex_init(&kfd->doorbell_mutex);
361 memset(&kfd->doorbell_available_index, 0,
362 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300363
364 return kfd;
365}
366
Felix Kuehling373d7082017-11-14 16:41:19 -0500367static void kfd_cwsr_init(struct kfd_dev *kfd)
368{
369 if (cwsr_enable && kfd->device_info->supports_cwsr) {
Felix Kuehling3e76c232018-04-10 17:33:16 -0400370 if (kfd->device_info->asic_family < CHIP_VEGA10) {
371 BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
372 kfd->cwsr_isa = cwsr_trap_gfx8_hex;
373 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
374 } else {
375 BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
376 kfd->cwsr_isa = cwsr_trap_gfx9_hex;
377 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
378 }
Felix Kuehling373d7082017-11-14 16:41:19 -0500379
Felix Kuehling373d7082017-11-14 16:41:19 -0500380 kfd->cwsr_enabled = true;
381 }
382}
383
Oded Gabbay4a488a72014-07-16 21:08:55 +0300384bool kgd2kfd_device_init(struct kfd_dev *kfd,
385 const struct kgd2kfd_shared_resources *gpu_resources)
386{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300387 unsigned int size;
388
Oded Gabbay4a488a72014-07-16 21:08:55 +0300389 kfd->shared_resources = *gpu_resources;
390
Yong Zhao44008d72017-09-20 18:10:18 -0400391 kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
392 kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
393 kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
394 - kfd->vm_info.first_vmid_kfd + 1;
395
Felix Kuehlinga99c6d42017-11-27 18:29:45 -0500396 /* Verify module parameters regarding mapped process number*/
397 if ((hws_max_conc_proc < 0)
398 || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
399 dev_err(kfd_device,
400 "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
401 hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
402 kfd->vm_info.vmid_num_kfd);
403 kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
404 } else
405 kfd->max_proc_per_quantum = hws_max_conc_proc;
406
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300407 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200408 size = max_num_of_queues_per_device *
409 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300410
Oded Gabbaye18e7942014-10-26 10:12:22 +0200411 /*
412 * calculate max size of runlist packet.
413 * There can be only 2 packets at once
414 */
Felix Kuehling507968d2017-08-15 23:00:15 -0400415 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
416 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
417 + sizeof(struct pm4_mes_runlist)) * 2;
Oded Gabbaye18e7942014-10-26 10:12:22 +0200418
419 /* Add size of HIQ & DIQ */
420 size += KFD_KERNEL_QUEUE_SIZE * 2;
421
422 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300423 size += 512 * 1024;
424
Xihan Zhangcea405b2015-03-17 19:32:53 +0800425 if (kfd->kfd2kgd->init_gtt_mem_allocation(
426 kfd->kgd, size, &kfd->gtt_mem,
427 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
Kent Russell79775b62017-08-15 23:00:05 -0400428 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300429 goto out;
430 }
431
Kent Russell79775b62017-08-15 23:00:05 -0400432 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
Oded Gabbaye18e7942014-10-26 10:12:22 +0200433
Oded Gabbay73a1da02014-10-26 09:53:37 +0200434 /* Initialize GTT sa with 512 byte chunk size */
435 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
Kent Russell79775b62017-08-15 23:00:05 -0400436 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
Oded Gabbay73a1da02014-10-26 09:53:37 +0200437 goto kfd_gtt_sa_init_error;
438 }
439
Felix Kuehling735df2b2017-08-15 23:00:10 -0400440 if (kfd_doorbell_init(kfd)) {
441 dev_err(kfd_device,
442 "Error initializing doorbell aperture\n");
443 goto kfd_doorbell_error;
444 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300445
Kent Russell4eacc26b2017-08-15 23:00:06 -0400446 if (kfd_topology_add_device(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400447 dev_err(kfd_device, "Error adding device to topology\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300448 goto kfd_topology_add_device_error;
449 }
450
Andrew Lewycky2249d552014-07-17 01:37:30 +0300451 if (kfd_interrupt_init(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400452 dev_err(kfd_device, "Error initializing interrupts\n");
Andrew Lewycky2249d552014-07-17 01:37:30 +0300453 goto kfd_interrupt_error;
454 }
455
Ben Goz64c7f8c2014-07-17 01:27:00 +0300456 kfd->dqm = device_queue_manager_init(kfd);
457 if (!kfd->dqm) {
Kent Russell79775b62017-08-15 23:00:05 -0400458 dev_err(kfd_device, "Error initializing queue manager\n");
Ben Goz64c7f8c2014-07-17 01:27:00 +0300459 goto device_queue_manager_error;
460 }
461
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500462 if (kfd_iommu_device_init(kfd)) {
463 dev_err(kfd_device, "Error initializing iommuv2\n");
464 goto device_iommu_error;
Ben Goz64c7f8c2014-07-17 01:27:00 +0300465 }
466
Felix Kuehling373d7082017-11-14 16:41:19 -0500467 kfd_cwsr_init(kfd);
468
Yong Zhaob8935a72017-09-20 18:10:13 -0400469 if (kfd_resume(kfd))
470 goto kfd_resume_error;
471
Yair Shacharfbeb6612015-05-20 13:48:26 +0300472 kfd->dbgmgr = NULL;
473
Oded Gabbay4a488a72014-07-16 21:08:55 +0300474 kfd->init_complete = true;
Kent Russell79775b62017-08-15 23:00:05 -0400475 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
Oded Gabbay4a488a72014-07-16 21:08:55 +0300476 kfd->pdev->device);
477
Kent Russell79775b62017-08-15 23:00:05 -0400478 pr_debug("Starting kfd with the following scheduling policy %d\n",
Felix Kuehlingd146c5a2018-01-04 17:17:43 -0500479 kfd->dqm->sched_policy);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300480
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300481 goto out;
482
Yong Zhaob8935a72017-09-20 18:10:13 -0400483kfd_resume_error:
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500484device_iommu_error:
Ben Goz64c7f8c2014-07-17 01:27:00 +0300485 device_queue_manager_uninit(kfd->dqm);
486device_queue_manager_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300487 kfd_interrupt_exit(kfd);
488kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300489 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300490kfd_topology_add_device_error:
Felix Kuehling735df2b2017-08-15 23:00:10 -0400491 kfd_doorbell_fini(kfd);
492kfd_doorbell_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200493 kfd_gtt_sa_fini(kfd);
494kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800495 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300496 dev_err(kfd_device,
Kent Russell79775b62017-08-15 23:00:05 -0400497 "device %x:%x NOT added due to errors\n",
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300498 kfd->pdev->vendor, kfd->pdev->device);
499out:
500 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300501}
502
503void kgd2kfd_device_exit(struct kfd_dev *kfd)
504{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300505 if (kfd->init_complete) {
Yong Zhaob8935a72017-09-20 18:10:13 -0400506 kgd2kfd_suspend(kfd);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300507 device_queue_manager_uninit(kfd->dqm);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300508 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300509 kfd_topology_remove_device(kfd);
Felix Kuehling735df2b2017-08-15 23:00:10 -0400510 kfd_doorbell_fini(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200511 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800512 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300513 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300514
Oded Gabbay4a488a72014-07-16 21:08:55 +0300515 kfree(kfd);
516}
517
518void kgd2kfd_suspend(struct kfd_dev *kfd)
519{
Yong Zhao733fa1f2017-09-20 18:10:14 -0400520 if (!kfd->init_complete)
521 return;
522
Felix Kuehling26103432018-02-06 20:32:45 -0500523 /* For first KFD device suspend all the KFD processes */
524 if (atomic_inc_return(&kfd_device_suspended) == 1)
525 kfd_suspend_all_processes();
526
Yong Zhao733fa1f2017-09-20 18:10:14 -0400527 kfd->dqm->ops.stop(kfd->dqm);
528
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500529 kfd_iommu_suspend(kfd);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300530}
531
532int kgd2kfd_resume(struct kfd_dev *kfd)
533{
Felix Kuehling26103432018-02-06 20:32:45 -0500534 int ret, count;
535
Yong Zhaob8935a72017-09-20 18:10:13 -0400536 if (!kfd->init_complete)
537 return 0;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300538
Felix Kuehling26103432018-02-06 20:32:45 -0500539 ret = kfd_resume(kfd);
540 if (ret)
541 return ret;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300542
Felix Kuehling26103432018-02-06 20:32:45 -0500543 count = atomic_dec_return(&kfd_device_suspended);
544 WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
545 if (count == 0)
546 ret = kfd_resume_all_processes();
547
548 return ret;
Yong Zhaob8935a72017-09-20 18:10:13 -0400549}
Yong Zhao4ebc7182017-08-15 23:00:13 -0400550
Yong Zhaob8935a72017-09-20 18:10:13 -0400551static int kfd_resume(struct kfd_dev *kfd)
552{
553 int err = 0;
Yong Zhaob8935a72017-09-20 18:10:13 -0400554
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500555 err = kfd_iommu_resume(kfd);
556 if (err) {
557 dev_err(kfd_device,
558 "Failed to resume IOMMU for device %x:%x\n",
559 kfd->pdev->vendor, kfd->pdev->device);
560 return err;
561 }
Yong Zhao733fa1f2017-09-20 18:10:14 -0400562
Yong Zhaob8935a72017-09-20 18:10:13 -0400563 err = kfd->dqm->ops.start(kfd->dqm);
564 if (err) {
565 dev_err(kfd_device,
566 "Error starting queue manager for device %x:%x\n",
567 kfd->pdev->vendor, kfd->pdev->device);
568 goto dqm_start_error;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300569 }
570
Yong Zhaob8935a72017-09-20 18:10:13 -0400571 return err;
572
573dqm_start_error:
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500574 kfd_iommu_suspend(kfd);
Yong Zhaob8935a72017-09-20 18:10:13 -0400575 return err;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300576}
577
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300578/* This is called directly from KGD at ISR. */
579void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300580{
Andrew Lewycky2249d552014-07-17 01:37:30 +0300581 if (!kfd->init_complete)
582 return;
583
584 spin_lock(&kfd->interrupt_lock);
585
586 if (kfd->interrupts_active
587 && interrupt_is_wanted(kfd, ih_ring_entry)
588 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
Andres Rodriguez48e876a2017-10-27 19:35:34 -0400589 queue_work(kfd->ih_wq, &kfd->interrupt_work);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300590
591 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300592}
Oded Gabbay6e810902014-10-27 14:36:07 +0200593
Felix Kuehling6b95e792018-03-23 15:32:32 -0400594int kgd2kfd_quiesce_mm(struct mm_struct *mm)
595{
596 struct kfd_process *p;
597 int r;
598
599 /* Because we are called from arbitrary context (workqueue) as opposed
600 * to process context, kfd_process could attempt to exit while we are
601 * running so the lookup function increments the process ref count.
602 */
603 p = kfd_lookup_process_by_mm(mm);
604 if (!p)
605 return -ESRCH;
606
607 r = kfd_process_evict_queues(p);
608
609 kfd_unref_process(p);
610 return r;
611}
612
613int kgd2kfd_resume_mm(struct mm_struct *mm)
614{
615 struct kfd_process *p;
616 int r;
617
618 /* Because we are called from arbitrary context (workqueue) as opposed
619 * to process context, kfd_process could attempt to exit while we are
620 * running so the lookup function increments the process ref count.
621 */
622 p = kfd_lookup_process_by_mm(mm);
623 if (!p)
624 return -ESRCH;
625
626 r = kfd_process_restore_queues(p);
627
628 kfd_unref_process(p);
629 return r;
630}
631
Felix Kuehling26103432018-02-06 20:32:45 -0500632/** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
633 * prepare for safe eviction of KFD BOs that belong to the specified
634 * process.
635 *
636 * @mm: mm_struct that identifies the specified KFD process
637 * @fence: eviction fence attached to KFD process BOs
638 *
639 */
640int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
641 struct dma_fence *fence)
642{
643 struct kfd_process *p;
644 unsigned long active_time;
645 unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
646
647 if (!fence)
648 return -EINVAL;
649
650 if (dma_fence_is_signaled(fence))
651 return 0;
652
653 p = kfd_lookup_process_by_mm(mm);
654 if (!p)
655 return -ENODEV;
656
657 if (fence->seqno == p->last_eviction_seqno)
658 goto out;
659
660 p->last_eviction_seqno = fence->seqno;
661
662 /* Avoid KFD process starvation. Wait for at least
663 * PROCESS_ACTIVE_TIME_MS before evicting the process again
664 */
665 active_time = get_jiffies_64() - p->last_restore_timestamp;
666 if (delay_jiffies > active_time)
667 delay_jiffies -= active_time;
668 else
669 delay_jiffies = 0;
670
671 /* During process initialization eviction_work.dwork is initialized
672 * to kfd_evict_bo_worker
673 */
674 schedule_delayed_work(&p->eviction_work, delay_jiffies);
675out:
676 kfd_unref_process(p);
677 return 0;
678}
679
Oded Gabbay6e810902014-10-27 14:36:07 +0200680static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
681 unsigned int chunk_size)
682{
Felix Kuehling8625ff92017-08-15 23:00:11 -0400683 unsigned int num_of_longs;
Oded Gabbay6e810902014-10-27 14:36:07 +0200684
Felix Kuehling32fa8212017-08-15 23:00:12 -0400685 if (WARN_ON(buf_size < chunk_size))
686 return -EINVAL;
687 if (WARN_ON(buf_size == 0))
688 return -EINVAL;
689 if (WARN_ON(chunk_size == 0))
690 return -EINVAL;
Oded Gabbay6e810902014-10-27 14:36:07 +0200691
692 kfd->gtt_sa_chunk_size = chunk_size;
693 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
694
Felix Kuehling8625ff92017-08-15 23:00:11 -0400695 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
696 BITS_PER_LONG;
Oded Gabbay6e810902014-10-27 14:36:07 +0200697
Felix Kuehling8625ff92017-08-15 23:00:11 -0400698 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
Oded Gabbay6e810902014-10-27 14:36:07 +0200699
700 if (!kfd->gtt_sa_bitmap)
701 return -ENOMEM;
702
Kent Russell79775b62017-08-15 23:00:05 -0400703 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200704 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
705
706 mutex_init(&kfd->gtt_sa_lock);
707
708 return 0;
709
710}
711
712static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
713{
714 mutex_destroy(&kfd->gtt_sa_lock);
715 kfree(kfd->gtt_sa_bitmap);
716}
717
718static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
719 unsigned int bit_num,
720 unsigned int chunk_size)
721{
722 return start_addr + bit_num * chunk_size;
723}
724
725static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
726 unsigned int bit_num,
727 unsigned int chunk_size)
728{
729 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
730}
731
732int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
733 struct kfd_mem_obj **mem_obj)
734{
735 unsigned int found, start_search, cur_size;
736
Oded Gabbay6e810902014-10-27 14:36:07 +0200737 if (size == 0)
738 return -EINVAL;
739
740 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
741 return -ENOMEM;
742
Felix Kuehlingb91d43d2018-04-10 17:33:08 -0400743 *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
Oded Gabbay6e810902014-10-27 14:36:07 +0200744 if ((*mem_obj) == NULL)
745 return -ENOMEM;
746
Kent Russell79775b62017-08-15 23:00:05 -0400747 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
Oded Gabbay6e810902014-10-27 14:36:07 +0200748
749 start_search = 0;
750
751 mutex_lock(&kfd->gtt_sa_lock);
752
753kfd_gtt_restart_search:
754 /* Find the first chunk that is free */
755 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
756 kfd->gtt_sa_num_of_chunks,
757 start_search);
758
Kent Russell79775b62017-08-15 23:00:05 -0400759 pr_debug("Found = %d\n", found);
Oded Gabbay6e810902014-10-27 14:36:07 +0200760
761 /* If there wasn't any free chunk, bail out */
762 if (found == kfd->gtt_sa_num_of_chunks)
763 goto kfd_gtt_no_free_chunk;
764
765 /* Update fields of mem_obj */
766 (*mem_obj)->range_start = found;
767 (*mem_obj)->range_end = found;
768 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
769 kfd->gtt_start_gpu_addr,
770 found,
771 kfd->gtt_sa_chunk_size);
772 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
773 kfd->gtt_start_cpu_ptr,
774 found,
775 kfd->gtt_sa_chunk_size);
776
Kent Russell79775b62017-08-15 23:00:05 -0400777 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200778 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
779
780 /* If we need only one chunk, mark it as allocated and get out */
781 if (size <= kfd->gtt_sa_chunk_size) {
Kent Russell79775b62017-08-15 23:00:05 -0400782 pr_debug("Single bit\n");
Oded Gabbay6e810902014-10-27 14:36:07 +0200783 set_bit(found, kfd->gtt_sa_bitmap);
784 goto kfd_gtt_out;
785 }
786
787 /* Otherwise, try to see if we have enough contiguous chunks */
788 cur_size = size - kfd->gtt_sa_chunk_size;
789 do {
790 (*mem_obj)->range_end =
791 find_next_zero_bit(kfd->gtt_sa_bitmap,
792 kfd->gtt_sa_num_of_chunks, ++found);
793 /*
794 * If next free chunk is not contiguous than we need to
795 * restart our search from the last free chunk we found (which
796 * wasn't contiguous to the previous ones
797 */
798 if ((*mem_obj)->range_end != found) {
799 start_search = found;
800 goto kfd_gtt_restart_search;
801 }
802
803 /*
804 * If we reached end of buffer, bail out with error
805 */
806 if (found == kfd->gtt_sa_num_of_chunks)
807 goto kfd_gtt_no_free_chunk;
808
809 /* Check if we don't need another chunk */
810 if (cur_size <= kfd->gtt_sa_chunk_size)
811 cur_size = 0;
812 else
813 cur_size -= kfd->gtt_sa_chunk_size;
814
815 } while (cur_size > 0);
816
Kent Russell79775b62017-08-15 23:00:05 -0400817 pr_debug("range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200818 (*mem_obj)->range_start, (*mem_obj)->range_end);
819
820 /* Mark the chunks as allocated */
821 for (found = (*mem_obj)->range_start;
822 found <= (*mem_obj)->range_end;
823 found++)
824 set_bit(found, kfd->gtt_sa_bitmap);
825
826kfd_gtt_out:
827 mutex_unlock(&kfd->gtt_sa_lock);
828 return 0;
829
830kfd_gtt_no_free_chunk:
Kent Russell79775b62017-08-15 23:00:05 -0400831 pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
Oded Gabbay6e810902014-10-27 14:36:07 +0200832 mutex_unlock(&kfd->gtt_sa_lock);
833 kfree(mem_obj);
834 return -ENOMEM;
835}
836
837int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
838{
839 unsigned int bit;
840
Oded Gabbay9216ed22015-01-12 22:34:21 +0200841 /* Act like kfree when trying to free a NULL object */
842 if (!mem_obj)
843 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200844
Kent Russell79775b62017-08-15 23:00:05 -0400845 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200846 mem_obj, mem_obj->range_start, mem_obj->range_end);
847
848 mutex_lock(&kfd->gtt_sa_lock);
849
850 /* Mark the chunks as free */
851 for (bit = mem_obj->range_start;
852 bit <= mem_obj->range_end;
853 bit++)
854 clear_bit(bit, kfd->gtt_sa_bitmap);
855
856 mutex_unlock(&kfd->gtt_sa_lock);
857
858 kfree(mem_obj);
859 return 0;
860}