blob: 4f7c0a79bbf533c94481ae8dea03ca19a5740a0d [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"
Yong Zhao0db54b22018-05-01 17:56:06 -040029#include "cwsr_trap_handler.h"
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050030#include "kfd_iommu.h"
Oded Gabbay4a488a72014-07-16 21:08:55 +030031
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030032#define MQD_SIZE_ALIGNED 768
Shaoyun Liue42051d2018-07-11 22:32:56 -040033
34/*
35 * kfd_locked is used to lock the kfd driver during suspend or reset
36 * once locked, kfd driver will stop any further GPU execution.
37 * create process (open) will return -EAGAIN.
38 */
39static atomic_t kfd_locked = ATOMIC_INIT(0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030040
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050041#ifdef KFD_SUPPORT_IOMMU_V2
Oded Gabbay4a488a72014-07-16 21:08:55 +030042static const struct kfd_device_info kaveri_device_info = {
Ben Goz0da75582015-01-01 17:10:01 +020043 .asic_family = CHIP_KAVERI,
44 .max_pasid_bits = 16,
Yair Shachar992839a2015-05-20 13:43:04 +030045 /* max num of queues for KV.TODO should be a dynamic value */
46 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040047 .doorbell_size = 4,
Ben Goz0da75582015-01-01 17:10:01 +020048 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030049 .event_interrupt_class = &event_interrupt_class_cik,
Yair Shacharfbeb6612015-05-20 13:48:26 +030050 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050051 .mqd_size_aligned = MQD_SIZE_ALIGNED,
52 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050053 .needs_iommu_device = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050054 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -040055 .num_sdma_engines = 2,
Ben Goz0da75582015-01-01 17:10:01 +020056};
57
58static const struct kfd_device_info carrizo_device_info = {
59 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030060 .max_pasid_bits = 16,
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030061 /* max num of queues for CZ.TODO should be a dynamic value */
62 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040063 .doorbell_size = 4,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030064 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030065 .event_interrupt_class = &event_interrupt_class_cik,
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030066 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050067 .mqd_size_aligned = MQD_SIZE_ALIGNED,
68 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050069 .needs_iommu_device = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050070 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -040071 .num_sdma_engines = 2,
Oded Gabbay4a488a72014-07-16 21:08:55 +030072};
Yong Zhao4d663df2018-07-13 16:17:48 -040073
74static const struct kfd_device_info raven_device_info = {
75 .asic_family = CHIP_RAVEN,
76 .max_pasid_bits = 16,
77 .max_no_of_hqd = 24,
78 .doorbell_size = 8,
79 .ih_ring_entry_size = 8 * sizeof(uint32_t),
80 .event_interrupt_class = &event_interrupt_class_v9,
81 .num_of_watch_points = 4,
82 .mqd_size_aligned = MQD_SIZE_ALIGNED,
83 .supports_cwsr = true,
84 .needs_iommu_device = true,
85 .needs_pci_atomics = true,
86 .num_sdma_engines = 1,
87};
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050088#endif
Oded Gabbay4a488a72014-07-16 21:08:55 +030089
Felix Kuehlinga3084e62018-01-04 17:17:47 -050090static const struct kfd_device_info hawaii_device_info = {
91 .asic_family = CHIP_HAWAII,
92 .max_pasid_bits = 16,
93 /* max num of queues for KV.TODO should be a dynamic value */
94 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040095 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050096 .ih_ring_entry_size = 4 * sizeof(uint32_t),
97 .event_interrupt_class = &event_interrupt_class_cik,
98 .num_of_watch_points = 4,
99 .mqd_size_aligned = MQD_SIZE_ALIGNED,
100 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500101 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500102 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -0400103 .num_sdma_engines = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500104};
105
106static const struct kfd_device_info tonga_device_info = {
107 .asic_family = CHIP_TONGA,
108 .max_pasid_bits = 16,
109 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400110 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500111 .ih_ring_entry_size = 4 * sizeof(uint32_t),
112 .event_interrupt_class = &event_interrupt_class_cik,
113 .num_of_watch_points = 4,
114 .mqd_size_aligned = MQD_SIZE_ALIGNED,
115 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500116 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500117 .needs_pci_atomics = true,
Yong Zhao98bb9222018-07-13 16:17:44 -0400118 .num_sdma_engines = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500119};
120
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500121static const struct kfd_device_info fiji_device_info = {
122 .asic_family = CHIP_FIJI,
123 .max_pasid_bits = 16,
124 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400125 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500126 .ih_ring_entry_size = 4 * sizeof(uint32_t),
127 .event_interrupt_class = &event_interrupt_class_cik,
128 .num_of_watch_points = 4,
129 .mqd_size_aligned = MQD_SIZE_ALIGNED,
130 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500131 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500132 .needs_pci_atomics = true,
Yong Zhao98bb9222018-07-13 16:17:44 -0400133 .num_sdma_engines = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500134};
135
136static const struct kfd_device_info fiji_vf_device_info = {
137 .asic_family = CHIP_FIJI,
138 .max_pasid_bits = 16,
139 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400140 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500141 .ih_ring_entry_size = 4 * sizeof(uint32_t),
142 .event_interrupt_class = &event_interrupt_class_cik,
143 .num_of_watch_points = 4,
144 .mqd_size_aligned = MQD_SIZE_ALIGNED,
145 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500146 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500147 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -0400148 .num_sdma_engines = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500149};
150
151
152static const struct kfd_device_info polaris10_device_info = {
153 .asic_family = CHIP_POLARIS10,
154 .max_pasid_bits = 16,
155 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400156 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500157 .ih_ring_entry_size = 4 * sizeof(uint32_t),
158 .event_interrupt_class = &event_interrupt_class_cik,
159 .num_of_watch_points = 4,
160 .mqd_size_aligned = MQD_SIZE_ALIGNED,
161 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500162 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500163 .needs_pci_atomics = true,
Yong Zhao98bb9222018-07-13 16:17:44 -0400164 .num_sdma_engines = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500165};
166
167static const struct kfd_device_info polaris10_vf_device_info = {
168 .asic_family = CHIP_POLARIS10,
169 .max_pasid_bits = 16,
170 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400171 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500172 .ih_ring_entry_size = 4 * sizeof(uint32_t),
173 .event_interrupt_class = &event_interrupt_class_cik,
174 .num_of_watch_points = 4,
175 .mqd_size_aligned = MQD_SIZE_ALIGNED,
176 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500177 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500178 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -0400179 .num_sdma_engines = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500180};
181
182static const struct kfd_device_info polaris11_device_info = {
183 .asic_family = CHIP_POLARIS11,
184 .max_pasid_bits = 16,
185 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400186 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500187 .ih_ring_entry_size = 4 * sizeof(uint32_t),
188 .event_interrupt_class = &event_interrupt_class_cik,
189 .num_of_watch_points = 4,
190 .mqd_size_aligned = MQD_SIZE_ALIGNED,
191 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500192 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500193 .needs_pci_atomics = true,
Yong Zhao98bb9222018-07-13 16:17:44 -0400194 .num_sdma_engines = 2,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500195};
196
Felix Kuehling389056e2018-04-10 17:33:18 -0400197static const struct kfd_device_info vega10_device_info = {
198 .asic_family = CHIP_VEGA10,
199 .max_pasid_bits = 16,
200 .max_no_of_hqd = 24,
201 .doorbell_size = 8,
202 .ih_ring_entry_size = 8 * sizeof(uint32_t),
203 .event_interrupt_class = &event_interrupt_class_v9,
204 .num_of_watch_points = 4,
205 .mqd_size_aligned = MQD_SIZE_ALIGNED,
206 .supports_cwsr = true,
207 .needs_iommu_device = false,
208 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -0400209 .num_sdma_engines = 2,
Felix Kuehling389056e2018-04-10 17:33:18 -0400210};
211
212static const struct kfd_device_info vega10_vf_device_info = {
213 .asic_family = CHIP_VEGA10,
214 .max_pasid_bits = 16,
215 .max_no_of_hqd = 24,
216 .doorbell_size = 8,
217 .ih_ring_entry_size = 8 * sizeof(uint32_t),
218 .event_interrupt_class = &event_interrupt_class_v9,
219 .num_of_watch_points = 4,
220 .mqd_size_aligned = MQD_SIZE_ALIGNED,
221 .supports_cwsr = true,
222 .needs_iommu_device = false,
223 .needs_pci_atomics = false,
Yong Zhao98bb9222018-07-13 16:17:44 -0400224 .num_sdma_engines = 2,
Felix Kuehling389056e2018-04-10 17:33:18 -0400225};
226
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500227
Oded Gabbay4a488a72014-07-16 21:08:55 +0300228struct kfd_deviceid {
229 unsigned short did;
230 const struct kfd_device_info *device_info;
231};
232
Oded Gabbay4a488a72014-07-16 21:08:55 +0300233static const struct kfd_deviceid supported_devices[] = {
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500234#ifdef KFD_SUPPORT_IOMMU_V2
Oded Gabbay4a488a72014-07-16 21:08:55 +0300235 { 0x1304, &kaveri_device_info }, /* Kaveri */
236 { 0x1305, &kaveri_device_info }, /* Kaveri */
237 { 0x1306, &kaveri_device_info }, /* Kaveri */
238 { 0x1307, &kaveri_device_info }, /* Kaveri */
239 { 0x1309, &kaveri_device_info }, /* Kaveri */
240 { 0x130A, &kaveri_device_info }, /* Kaveri */
241 { 0x130B, &kaveri_device_info }, /* Kaveri */
242 { 0x130C, &kaveri_device_info }, /* Kaveri */
243 { 0x130D, &kaveri_device_info }, /* Kaveri */
244 { 0x130E, &kaveri_device_info }, /* Kaveri */
245 { 0x130F, &kaveri_device_info }, /* Kaveri */
246 { 0x1310, &kaveri_device_info }, /* Kaveri */
247 { 0x1311, &kaveri_device_info }, /* Kaveri */
248 { 0x1312, &kaveri_device_info }, /* Kaveri */
249 { 0x1313, &kaveri_device_info }, /* Kaveri */
250 { 0x1315, &kaveri_device_info }, /* Kaveri */
251 { 0x1316, &kaveri_device_info }, /* Kaveri */
252 { 0x1317, &kaveri_device_info }, /* Kaveri */
253 { 0x1318, &kaveri_device_info }, /* Kaveri */
254 { 0x131B, &kaveri_device_info }, /* Kaveri */
255 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz123576d2015-01-12 14:37:24 +0200256 { 0x131D, &kaveri_device_info }, /* Kaveri */
257 { 0x9870, &carrizo_device_info }, /* Carrizo */
258 { 0x9874, &carrizo_device_info }, /* Carrizo */
259 { 0x9875, &carrizo_device_info }, /* Carrizo */
260 { 0x9876, &carrizo_device_info }, /* Carrizo */
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500261 { 0x9877, &carrizo_device_info }, /* Carrizo */
Yong Zhao4d663df2018-07-13 16:17:48 -0400262 { 0x15DD, &raven_device_info }, /* Raven */
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500263#endif
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500264 { 0x67A0, &hawaii_device_info }, /* Hawaii */
265 { 0x67A1, &hawaii_device_info }, /* Hawaii */
266 { 0x67A2, &hawaii_device_info }, /* Hawaii */
267 { 0x67A8, &hawaii_device_info }, /* Hawaii */
268 { 0x67A9, &hawaii_device_info }, /* Hawaii */
269 { 0x67AA, &hawaii_device_info }, /* Hawaii */
270 { 0x67B0, &hawaii_device_info }, /* Hawaii */
271 { 0x67B1, &hawaii_device_info }, /* Hawaii */
272 { 0x67B8, &hawaii_device_info }, /* Hawaii */
273 { 0x67B9, &hawaii_device_info }, /* Hawaii */
274 { 0x67BA, &hawaii_device_info }, /* Hawaii */
275 { 0x67BE, &hawaii_device_info }, /* Hawaii */
276 { 0x6920, &tonga_device_info }, /* Tonga */
277 { 0x6921, &tonga_device_info }, /* Tonga */
278 { 0x6928, &tonga_device_info }, /* Tonga */
279 { 0x6929, &tonga_device_info }, /* Tonga */
280 { 0x692B, &tonga_device_info }, /* Tonga */
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500281 { 0x6938, &tonga_device_info }, /* Tonga */
282 { 0x6939, &tonga_device_info }, /* Tonga */
283 { 0x7300, &fiji_device_info }, /* Fiji */
284 { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/
285 { 0x67C0, &polaris10_device_info }, /* Polaris10 */
286 { 0x67C1, &polaris10_device_info }, /* Polaris10 */
287 { 0x67C2, &polaris10_device_info }, /* Polaris10 */
288 { 0x67C4, &polaris10_device_info }, /* Polaris10 */
289 { 0x67C7, &polaris10_device_info }, /* Polaris10 */
290 { 0x67C8, &polaris10_device_info }, /* Polaris10 */
291 { 0x67C9, &polaris10_device_info }, /* Polaris10 */
292 { 0x67CA, &polaris10_device_info }, /* Polaris10 */
293 { 0x67CC, &polaris10_device_info }, /* Polaris10 */
294 { 0x67CF, &polaris10_device_info }, /* Polaris10 */
295 { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/
296 { 0x67DF, &polaris10_device_info }, /* Polaris10 */
297 { 0x67E0, &polaris11_device_info }, /* Polaris11 */
298 { 0x67E1, &polaris11_device_info }, /* Polaris11 */
299 { 0x67E3, &polaris11_device_info }, /* Polaris11 */
300 { 0x67E7, &polaris11_device_info }, /* Polaris11 */
301 { 0x67E8, &polaris11_device_info }, /* Polaris11 */
302 { 0x67E9, &polaris11_device_info }, /* Polaris11 */
303 { 0x67EB, &polaris11_device_info }, /* Polaris11 */
304 { 0x67EF, &polaris11_device_info }, /* Polaris11 */
305 { 0x67FF, &polaris11_device_info }, /* Polaris11 */
Felix Kuehling389056e2018-04-10 17:33:18 -0400306 { 0x6860, &vega10_device_info }, /* Vega10 */
307 { 0x6861, &vega10_device_info }, /* Vega10 */
308 { 0x6862, &vega10_device_info }, /* Vega10 */
309 { 0x6863, &vega10_device_info }, /* Vega10 */
310 { 0x6864, &vega10_device_info }, /* Vega10 */
311 { 0x6867, &vega10_device_info }, /* Vega10 */
312 { 0x6868, &vega10_device_info }, /* Vega10 */
313 { 0x686C, &vega10_vf_device_info }, /* Vega10 vf*/
314 { 0x687F, &vega10_device_info }, /* Vega10 */
Oded Gabbay4a488a72014-07-16 21:08:55 +0300315};
316
Oded Gabbay6e810902014-10-27 14:36:07 +0200317static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
318 unsigned int chunk_size);
319static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
320
Yong Zhaob8935a72017-09-20 18:10:13 -0400321static int kfd_resume(struct kfd_dev *kfd);
322
Oded Gabbay4a488a72014-07-16 21:08:55 +0300323static const struct kfd_device_info *lookup_device_info(unsigned short did)
324{
325 size_t i;
326
327 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
328 if (supported_devices[i].did == did) {
Felix Kuehling32fa8212017-08-15 23:00:12 -0400329 WARN_ON(!supported_devices[i].device_info);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300330 return supported_devices[i].device_info;
331 }
332 }
333
Yong Zhao4ebc7182017-08-15 23:00:13 -0400334 dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
335 did);
336
Oded Gabbay4a488a72014-07-16 21:08:55 +0300337 return NULL;
338}
339
Xihan Zhangcea405b2015-03-17 19:32:53 +0800340struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
341 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300342{
343 struct kfd_dev *kfd;
welu6106dce2018-04-10 17:33:17 -0400344 int ret;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300345 const struct kfd_device_info *device_info =
346 lookup_device_info(pdev->device);
347
Yong Zhao4ebc7182017-08-15 23:00:13 -0400348 if (!device_info) {
349 dev_err(kfd_device, "kgd2kfd_probe failed\n");
Oded Gabbay4a488a72014-07-16 21:08:55 +0300350 return NULL;
Yong Zhao4ebc7182017-08-15 23:00:13 -0400351 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300352
Eric Huangd35f00d2018-06-04 15:22:24 -0400353 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
354 if (!kfd)
355 return NULL;
356
welu6106dce2018-04-10 17:33:17 -0400357 /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
358 * 32 and 64-bit requests are possible and must be
359 * supported.
360 */
361 ret = pci_enable_atomic_ops_to_root(pdev,
362 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
363 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
364 if (device_info->needs_pci_atomics && ret < 0) {
365 dev_info(kfd_device,
366 "skipped device %x:%x, PCI rejects atomics\n",
367 pdev->vendor, pdev->device);
Eric Huangd35f00d2018-06-04 15:22:24 -0400368 kfree(kfd);
welu6106dce2018-04-10 17:33:17 -0400369 return NULL;
Eric Huangd35f00d2018-06-04 15:22:24 -0400370 } else if (!ret)
371 kfd->pci_atomic_requested = true;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300372
373 kfd->kgd = kgd;
374 kfd->device_info = device_info;
375 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300376 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800377 kfd->kfd2kgd = f2g;
378
379 mutex_init(&kfd->doorbell_mutex);
380 memset(&kfd->doorbell_available_index, 0,
381 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300382
383 return kfd;
384}
385
Felix Kuehling373d7082017-11-14 16:41:19 -0500386static void kfd_cwsr_init(struct kfd_dev *kfd)
387{
388 if (cwsr_enable && kfd->device_info->supports_cwsr) {
Felix Kuehling3e76c232018-04-10 17:33:16 -0400389 if (kfd->device_info->asic_family < CHIP_VEGA10) {
390 BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
391 kfd->cwsr_isa = cwsr_trap_gfx8_hex;
392 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
393 } else {
394 BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
395 kfd->cwsr_isa = cwsr_trap_gfx9_hex;
396 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
397 }
Felix Kuehling373d7082017-11-14 16:41:19 -0500398
Felix Kuehling373d7082017-11-14 16:41:19 -0500399 kfd->cwsr_enabled = true;
400 }
401}
402
Oded Gabbay4a488a72014-07-16 21:08:55 +0300403bool kgd2kfd_device_init(struct kfd_dev *kfd,
404 const struct kgd2kfd_shared_resources *gpu_resources)
405{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300406 unsigned int size;
407
Felix Kuehling5ade6c92018-08-20 20:15:00 -0400408 kfd->mec_fw_version = kfd->kfd2kgd->get_fw_version(kfd->kgd,
409 KGD_ENGINE_MEC1);
410 kfd->sdma_fw_version = kfd->kfd2kgd->get_fw_version(kfd->kgd,
411 KGD_ENGINE_SDMA1);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300412 kfd->shared_resources = *gpu_resources;
413
Yong Zhao44008d72017-09-20 18:10:18 -0400414 kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
415 kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
416 kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
417 - kfd->vm_info.first_vmid_kfd + 1;
418
Felix Kuehlinga99c6d42017-11-27 18:29:45 -0500419 /* Verify module parameters regarding mapped process number*/
420 if ((hws_max_conc_proc < 0)
421 || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
422 dev_err(kfd_device,
423 "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
424 hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
425 kfd->vm_info.vmid_num_kfd);
426 kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
427 } else
428 kfd->max_proc_per_quantum = hws_max_conc_proc;
429
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300430 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200431 size = max_num_of_queues_per_device *
432 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300433
Oded Gabbaye18e7942014-10-26 10:12:22 +0200434 /*
435 * calculate max size of runlist packet.
436 * There can be only 2 packets at once
437 */
Felix Kuehling507968d2017-08-15 23:00:15 -0400438 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
439 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
440 + sizeof(struct pm4_mes_runlist)) * 2;
Oded Gabbaye18e7942014-10-26 10:12:22 +0200441
442 /* Add size of HIQ & DIQ */
443 size += KFD_KERNEL_QUEUE_SIZE * 2;
444
445 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300446 size += 512 * 1024;
447
Xihan Zhangcea405b2015-03-17 19:32:53 +0800448 if (kfd->kfd2kgd->init_gtt_mem_allocation(
449 kfd->kgd, size, &kfd->gtt_mem,
Yong Zhao15426db2018-09-12 21:42:19 -0400450 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
451 false)) {
Kent Russell79775b62017-08-15 23:00:05 -0400452 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300453 goto out;
454 }
455
Kent Russell79775b62017-08-15 23:00:05 -0400456 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
Oded Gabbaye18e7942014-10-26 10:12:22 +0200457
Oded Gabbay73a1da02014-10-26 09:53:37 +0200458 /* Initialize GTT sa with 512 byte chunk size */
459 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
Kent Russell79775b62017-08-15 23:00:05 -0400460 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
Oded Gabbay73a1da02014-10-26 09:53:37 +0200461 goto kfd_gtt_sa_init_error;
462 }
463
Felix Kuehling735df2b2017-08-15 23:00:10 -0400464 if (kfd_doorbell_init(kfd)) {
465 dev_err(kfd_device,
466 "Error initializing doorbell aperture\n");
467 goto kfd_doorbell_error;
468 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300469
Shaoyun Liu0c1690e2018-07-06 11:32:42 -0400470 if (kfd->kfd2kgd->get_hive_id)
471 kfd->hive_id = kfd->kfd2kgd->get_hive_id(kfd->kgd);
472
Kent Russell4eacc26b2017-08-15 23:00:06 -0400473 if (kfd_topology_add_device(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400474 dev_err(kfd_device, "Error adding device to topology\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300475 goto kfd_topology_add_device_error;
476 }
477
Andrew Lewycky2249d552014-07-17 01:37:30 +0300478 if (kfd_interrupt_init(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400479 dev_err(kfd_device, "Error initializing interrupts\n");
Andrew Lewycky2249d552014-07-17 01:37:30 +0300480 goto kfd_interrupt_error;
481 }
482
Ben Goz64c7f8c2014-07-17 01:27:00 +0300483 kfd->dqm = device_queue_manager_init(kfd);
484 if (!kfd->dqm) {
Kent Russell79775b62017-08-15 23:00:05 -0400485 dev_err(kfd_device, "Error initializing queue manager\n");
Ben Goz64c7f8c2014-07-17 01:27:00 +0300486 goto device_queue_manager_error;
487 }
488
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500489 if (kfd_iommu_device_init(kfd)) {
490 dev_err(kfd_device, "Error initializing iommuv2\n");
491 goto device_iommu_error;
Ben Goz64c7f8c2014-07-17 01:27:00 +0300492 }
493
Felix Kuehling373d7082017-11-14 16:41:19 -0500494 kfd_cwsr_init(kfd);
495
Yong Zhaob8935a72017-09-20 18:10:13 -0400496 if (kfd_resume(kfd))
497 goto kfd_resume_error;
498
Yair Shacharfbeb6612015-05-20 13:48:26 +0300499 kfd->dbgmgr = NULL;
500
Oded Gabbay4a488a72014-07-16 21:08:55 +0300501 kfd->init_complete = true;
Kent Russell79775b62017-08-15 23:00:05 -0400502 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
Oded Gabbay4a488a72014-07-16 21:08:55 +0300503 kfd->pdev->device);
504
Kent Russell79775b62017-08-15 23:00:05 -0400505 pr_debug("Starting kfd with the following scheduling policy %d\n",
Felix Kuehlingd146c5a2018-01-04 17:17:43 -0500506 kfd->dqm->sched_policy);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300507
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300508 goto out;
509
Yong Zhaob8935a72017-09-20 18:10:13 -0400510kfd_resume_error:
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500511device_iommu_error:
Ben Goz64c7f8c2014-07-17 01:27:00 +0300512 device_queue_manager_uninit(kfd->dqm);
513device_queue_manager_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300514 kfd_interrupt_exit(kfd);
515kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300516 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300517kfd_topology_add_device_error:
Felix Kuehling735df2b2017-08-15 23:00:10 -0400518 kfd_doorbell_fini(kfd);
519kfd_doorbell_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200520 kfd_gtt_sa_fini(kfd);
521kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800522 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300523 dev_err(kfd_device,
Kent Russell79775b62017-08-15 23:00:05 -0400524 "device %x:%x NOT added due to errors\n",
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300525 kfd->pdev->vendor, kfd->pdev->device);
526out:
527 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300528}
529
530void kgd2kfd_device_exit(struct kfd_dev *kfd)
531{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300532 if (kfd->init_complete) {
Yong Zhaob8935a72017-09-20 18:10:13 -0400533 kgd2kfd_suspend(kfd);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300534 device_queue_manager_uninit(kfd->dqm);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300535 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300536 kfd_topology_remove_device(kfd);
Felix Kuehling735df2b2017-08-15 23:00:10 -0400537 kfd_doorbell_fini(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200538 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800539 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300540 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300541
Oded Gabbay4a488a72014-07-16 21:08:55 +0300542 kfree(kfd);
543}
544
Shaoyun Liue3b7a962018-07-11 22:32:54 -0400545int kgd2kfd_pre_reset(struct kfd_dev *kfd)
546{
Shaoyun Liue42051d2018-07-11 22:32:56 -0400547 if (!kfd->init_complete)
548 return 0;
549 kgd2kfd_suspend(kfd);
550
551 /* hold dqm->lock to prevent further execution*/
552 dqm_lock(kfd->dqm);
553
554 kfd_signal_reset_event(kfd);
Shaoyun Liue3b7a962018-07-11 22:32:54 -0400555 return 0;
556}
557
Shaoyun Liue42051d2018-07-11 22:32:56 -0400558/*
559 * Fix me. KFD won't be able to resume existing process for now.
560 * We will keep all existing process in a evicted state and
561 * wait the process to be terminated.
562 */
563
Shaoyun Liue3b7a962018-07-11 22:32:54 -0400564int kgd2kfd_post_reset(struct kfd_dev *kfd)
565{
Shaoyun Liue42051d2018-07-11 22:32:56 -0400566 int ret, count;
567
568 if (!kfd->init_complete)
569 return 0;
570
571 dqm_unlock(kfd->dqm);
572
573 ret = kfd_resume(kfd);
574 if (ret)
575 return ret;
576 count = atomic_dec_return(&kfd_locked);
577 WARN_ONCE(count != 0, "KFD reset ref. error");
Shaoyun Liue3b7a962018-07-11 22:32:54 -0400578 return 0;
579}
580
Shaoyun Liue42051d2018-07-11 22:32:56 -0400581bool kfd_is_locked(void)
582{
583 return (atomic_read(&kfd_locked) > 0);
584}
585
Oded Gabbay4a488a72014-07-16 21:08:55 +0300586void kgd2kfd_suspend(struct kfd_dev *kfd)
587{
Yong Zhao733fa1f2017-09-20 18:10:14 -0400588 if (!kfd->init_complete)
589 return;
590
Felix Kuehling26103432018-02-06 20:32:45 -0500591 /* For first KFD device suspend all the KFD processes */
Shaoyun Liue42051d2018-07-11 22:32:56 -0400592 if (atomic_inc_return(&kfd_locked) == 1)
Felix Kuehling26103432018-02-06 20:32:45 -0500593 kfd_suspend_all_processes();
594
Yong Zhao733fa1f2017-09-20 18:10:14 -0400595 kfd->dqm->ops.stop(kfd->dqm);
596
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500597 kfd_iommu_suspend(kfd);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300598}
599
600int kgd2kfd_resume(struct kfd_dev *kfd)
601{
Felix Kuehling26103432018-02-06 20:32:45 -0500602 int ret, count;
603
Yong Zhaob8935a72017-09-20 18:10:13 -0400604 if (!kfd->init_complete)
605 return 0;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300606
Felix Kuehling26103432018-02-06 20:32:45 -0500607 ret = kfd_resume(kfd);
608 if (ret)
609 return ret;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300610
Shaoyun Liue42051d2018-07-11 22:32:56 -0400611 count = atomic_dec_return(&kfd_locked);
Felix Kuehling26103432018-02-06 20:32:45 -0500612 WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
613 if (count == 0)
614 ret = kfd_resume_all_processes();
615
616 return ret;
Yong Zhaob8935a72017-09-20 18:10:13 -0400617}
Yong Zhao4ebc7182017-08-15 23:00:13 -0400618
Yong Zhaob8935a72017-09-20 18:10:13 -0400619static int kfd_resume(struct kfd_dev *kfd)
620{
621 int err = 0;
Yong Zhaob8935a72017-09-20 18:10:13 -0400622
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500623 err = kfd_iommu_resume(kfd);
624 if (err) {
625 dev_err(kfd_device,
626 "Failed to resume IOMMU for device %x:%x\n",
627 kfd->pdev->vendor, kfd->pdev->device);
628 return err;
629 }
Yong Zhao733fa1f2017-09-20 18:10:14 -0400630
Yong Zhaob8935a72017-09-20 18:10:13 -0400631 err = kfd->dqm->ops.start(kfd->dqm);
632 if (err) {
633 dev_err(kfd_device,
634 "Error starting queue manager for device %x:%x\n",
635 kfd->pdev->vendor, kfd->pdev->device);
636 goto dqm_start_error;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300637 }
638
Yong Zhaob8935a72017-09-20 18:10:13 -0400639 return err;
640
641dqm_start_error:
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500642 kfd_iommu_suspend(kfd);
Yong Zhaob8935a72017-09-20 18:10:13 -0400643 return err;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300644}
645
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300646/* This is called directly from KGD at ISR. */
647void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300648{
Lan Xiao58e69882018-07-11 22:32:51 -0400649 uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
650 bool is_patched = false;
651
Andrew Lewycky2249d552014-07-17 01:37:30 +0300652 if (!kfd->init_complete)
653 return;
654
Lan Xiao58e69882018-07-11 22:32:51 -0400655 if (kfd->device_info->ih_ring_entry_size > sizeof(patched_ihre)) {
656 dev_err_once(kfd_device, "Ring entry too small\n");
657 return;
658 }
659
Andrew Lewycky2249d552014-07-17 01:37:30 +0300660 spin_lock(&kfd->interrupt_lock);
661
662 if (kfd->interrupts_active
Lan Xiao58e69882018-07-11 22:32:51 -0400663 && interrupt_is_wanted(kfd, ih_ring_entry,
664 patched_ihre, &is_patched)
665 && enqueue_ih_ring_entry(kfd,
666 is_patched ? patched_ihre : ih_ring_entry))
Andres Rodriguez48e876a2017-10-27 19:35:34 -0400667 queue_work(kfd->ih_wq, &kfd->interrupt_work);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300668
669 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300670}
Oded Gabbay6e810902014-10-27 14:36:07 +0200671
Felix Kuehling6b95e792018-03-23 15:32:32 -0400672int kgd2kfd_quiesce_mm(struct mm_struct *mm)
673{
674 struct kfd_process *p;
675 int r;
676
677 /* Because we are called from arbitrary context (workqueue) as opposed
678 * to process context, kfd_process could attempt to exit while we are
679 * running so the lookup function increments the process ref count.
680 */
681 p = kfd_lookup_process_by_mm(mm);
682 if (!p)
683 return -ESRCH;
684
685 r = kfd_process_evict_queues(p);
686
687 kfd_unref_process(p);
688 return r;
689}
690
691int kgd2kfd_resume_mm(struct mm_struct *mm)
692{
693 struct kfd_process *p;
694 int r;
695
696 /* Because we are called from arbitrary context (workqueue) as opposed
697 * to process context, kfd_process could attempt to exit while we are
698 * running so the lookup function increments the process ref count.
699 */
700 p = kfd_lookup_process_by_mm(mm);
701 if (!p)
702 return -ESRCH;
703
704 r = kfd_process_restore_queues(p);
705
706 kfd_unref_process(p);
707 return r;
708}
709
Felix Kuehling26103432018-02-06 20:32:45 -0500710/** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
711 * prepare for safe eviction of KFD BOs that belong to the specified
712 * process.
713 *
714 * @mm: mm_struct that identifies the specified KFD process
715 * @fence: eviction fence attached to KFD process BOs
716 *
717 */
718int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
719 struct dma_fence *fence)
720{
721 struct kfd_process *p;
722 unsigned long active_time;
723 unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
724
725 if (!fence)
726 return -EINVAL;
727
728 if (dma_fence_is_signaled(fence))
729 return 0;
730
731 p = kfd_lookup_process_by_mm(mm);
732 if (!p)
733 return -ENODEV;
734
735 if (fence->seqno == p->last_eviction_seqno)
736 goto out;
737
738 p->last_eviction_seqno = fence->seqno;
739
740 /* Avoid KFD process starvation. Wait for at least
741 * PROCESS_ACTIVE_TIME_MS before evicting the process again
742 */
743 active_time = get_jiffies_64() - p->last_restore_timestamp;
744 if (delay_jiffies > active_time)
745 delay_jiffies -= active_time;
746 else
747 delay_jiffies = 0;
748
749 /* During process initialization eviction_work.dwork is initialized
750 * to kfd_evict_bo_worker
751 */
752 schedule_delayed_work(&p->eviction_work, delay_jiffies);
753out:
754 kfd_unref_process(p);
755 return 0;
756}
757
Oded Gabbay6e810902014-10-27 14:36:07 +0200758static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
759 unsigned int chunk_size)
760{
Felix Kuehling8625ff92017-08-15 23:00:11 -0400761 unsigned int num_of_longs;
Oded Gabbay6e810902014-10-27 14:36:07 +0200762
Felix Kuehling32fa8212017-08-15 23:00:12 -0400763 if (WARN_ON(buf_size < chunk_size))
764 return -EINVAL;
765 if (WARN_ON(buf_size == 0))
766 return -EINVAL;
767 if (WARN_ON(chunk_size == 0))
768 return -EINVAL;
Oded Gabbay6e810902014-10-27 14:36:07 +0200769
770 kfd->gtt_sa_chunk_size = chunk_size;
771 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
772
Felix Kuehling8625ff92017-08-15 23:00:11 -0400773 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
774 BITS_PER_LONG;
Oded Gabbay6e810902014-10-27 14:36:07 +0200775
Felix Kuehling8625ff92017-08-15 23:00:11 -0400776 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
Oded Gabbay6e810902014-10-27 14:36:07 +0200777
778 if (!kfd->gtt_sa_bitmap)
779 return -ENOMEM;
780
Kent Russell79775b62017-08-15 23:00:05 -0400781 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200782 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
783
784 mutex_init(&kfd->gtt_sa_lock);
785
786 return 0;
787
788}
789
790static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
791{
792 mutex_destroy(&kfd->gtt_sa_lock);
793 kfree(kfd->gtt_sa_bitmap);
794}
795
796static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
797 unsigned int bit_num,
798 unsigned int chunk_size)
799{
800 return start_addr + bit_num * chunk_size;
801}
802
803static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
804 unsigned int bit_num,
805 unsigned int chunk_size)
806{
807 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
808}
809
810int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
811 struct kfd_mem_obj **mem_obj)
812{
813 unsigned int found, start_search, cur_size;
814
Oded Gabbay6e810902014-10-27 14:36:07 +0200815 if (size == 0)
816 return -EINVAL;
817
818 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
819 return -ENOMEM;
820
Felix Kuehling1cd106e2018-07-11 22:32:45 -0400821 *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
822 if (!(*mem_obj))
Oded Gabbay6e810902014-10-27 14:36:07 +0200823 return -ENOMEM;
824
Kent Russell79775b62017-08-15 23:00:05 -0400825 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
Oded Gabbay6e810902014-10-27 14:36:07 +0200826
827 start_search = 0;
828
829 mutex_lock(&kfd->gtt_sa_lock);
830
831kfd_gtt_restart_search:
832 /* Find the first chunk that is free */
833 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
834 kfd->gtt_sa_num_of_chunks,
835 start_search);
836
Kent Russell79775b62017-08-15 23:00:05 -0400837 pr_debug("Found = %d\n", found);
Oded Gabbay6e810902014-10-27 14:36:07 +0200838
839 /* If there wasn't any free chunk, bail out */
840 if (found == kfd->gtt_sa_num_of_chunks)
841 goto kfd_gtt_no_free_chunk;
842
843 /* Update fields of mem_obj */
844 (*mem_obj)->range_start = found;
845 (*mem_obj)->range_end = found;
846 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
847 kfd->gtt_start_gpu_addr,
848 found,
849 kfd->gtt_sa_chunk_size);
850 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
851 kfd->gtt_start_cpu_ptr,
852 found,
853 kfd->gtt_sa_chunk_size);
854
Kent Russell79775b62017-08-15 23:00:05 -0400855 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200856 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
857
858 /* If we need only one chunk, mark it as allocated and get out */
859 if (size <= kfd->gtt_sa_chunk_size) {
Kent Russell79775b62017-08-15 23:00:05 -0400860 pr_debug("Single bit\n");
Oded Gabbay6e810902014-10-27 14:36:07 +0200861 set_bit(found, kfd->gtt_sa_bitmap);
862 goto kfd_gtt_out;
863 }
864
865 /* Otherwise, try to see if we have enough contiguous chunks */
866 cur_size = size - kfd->gtt_sa_chunk_size;
867 do {
868 (*mem_obj)->range_end =
869 find_next_zero_bit(kfd->gtt_sa_bitmap,
870 kfd->gtt_sa_num_of_chunks, ++found);
871 /*
872 * If next free chunk is not contiguous than we need to
873 * restart our search from the last free chunk we found (which
874 * wasn't contiguous to the previous ones
875 */
876 if ((*mem_obj)->range_end != found) {
877 start_search = found;
878 goto kfd_gtt_restart_search;
879 }
880
881 /*
882 * If we reached end of buffer, bail out with error
883 */
884 if (found == kfd->gtt_sa_num_of_chunks)
885 goto kfd_gtt_no_free_chunk;
886
887 /* Check if we don't need another chunk */
888 if (cur_size <= kfd->gtt_sa_chunk_size)
889 cur_size = 0;
890 else
891 cur_size -= kfd->gtt_sa_chunk_size;
892
893 } while (cur_size > 0);
894
Kent Russell79775b62017-08-15 23:00:05 -0400895 pr_debug("range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200896 (*mem_obj)->range_start, (*mem_obj)->range_end);
897
898 /* Mark the chunks as allocated */
899 for (found = (*mem_obj)->range_start;
900 found <= (*mem_obj)->range_end;
901 found++)
902 set_bit(found, kfd->gtt_sa_bitmap);
903
904kfd_gtt_out:
905 mutex_unlock(&kfd->gtt_sa_lock);
906 return 0;
907
908kfd_gtt_no_free_chunk:
Kent Russell79775b62017-08-15 23:00:05 -0400909 pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
Oded Gabbay6e810902014-10-27 14:36:07 +0200910 mutex_unlock(&kfd->gtt_sa_lock);
911 kfree(mem_obj);
912 return -ENOMEM;
913}
914
915int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
916{
917 unsigned int bit;
918
Oded Gabbay9216ed22015-01-12 22:34:21 +0200919 /* Act like kfree when trying to free a NULL object */
920 if (!mem_obj)
921 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200922
Kent Russell79775b62017-08-15 23:00:05 -0400923 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200924 mem_obj, mem_obj->range_start, mem_obj->range_end);
925
926 mutex_lock(&kfd->gtt_sa_lock);
927
928 /* Mark the chunks as free */
929 for (bit = mem_obj->range_start;
930 bit <= mem_obj->range_end;
931 bit++)
932 clear_bit(bit, kfd->gtt_sa_bitmap);
933
934 mutex_unlock(&kfd->gtt_sa_lock);
935
936 kfree(mem_obj);
937 return 0;
938}
Shaoyun Liua29ec472018-07-11 22:33:04 -0400939
940#if defined(CONFIG_DEBUG_FS)
941
942/* This function will send a package to HIQ to hang the HWS
943 * which will trigger a GPU reset and bring the HWS back to normal state
944 */
945int kfd_debugfs_hang_hws(struct kfd_dev *dev)
946{
947 int r = 0;
948
949 if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
950 pr_err("HWS is not enabled");
951 return -EINVAL;
952 }
953
954 r = pm_debugfs_hang_hws(&dev->dqm->packets);
955 if (!r)
956 r = dqm_debugfs_execute_queues(dev->dqm);
957
958 return r;
959}
960
961#endif