blob: f563acbc1ad70fa083c4ab27f07270d38485ee58 [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
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050023#if defined(CONFIG_AMD_IOMMU_V2_MODULE) || defined(CONFIG_AMD_IOMMU_V2)
Oded Gabbay4a488a72014-07-16 21:08:55 +030024#include <linux/amd-iommu.h>
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050025#endif
Oded Gabbay4a488a72014-07-16 21:08:55 +030026#include <linux/bsearch.h>
27#include <linux/pci.h>
28#include <linux/slab.h>
29#include "kfd_priv.h"
Ben Goz64c7f8c2014-07-17 01:27:00 +030030#include "kfd_device_queue_manager.h"
Felix Kuehling507968d2017-08-15 23:00:15 -040031#include "kfd_pm4_headers_vi.h"
Felix Kuehling373d7082017-11-14 16:41:19 -050032#include "cwsr_trap_handler_gfx8.asm"
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050033#include "kfd_iommu.h"
Oded Gabbay4a488a72014-07-16 21:08:55 +030034
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030035#define MQD_SIZE_ALIGNED 768
Felix Kuehling26103432018-02-06 20:32:45 -050036static atomic_t kfd_device_suspended = ATOMIC_INIT(0);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030037
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050038#ifdef KFD_SUPPORT_IOMMU_V2
Oded Gabbay4a488a72014-07-16 21:08:55 +030039static const struct kfd_device_info kaveri_device_info = {
Ben Goz0da75582015-01-01 17:10:01 +020040 .asic_family = CHIP_KAVERI,
41 .max_pasid_bits = 16,
Yair Shachar992839a2015-05-20 13:43:04 +030042 /* max num of queues for KV.TODO should be a dynamic value */
43 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040044 .doorbell_size = 4,
Ben Goz0da75582015-01-01 17:10:01 +020045 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030046 .event_interrupt_class = &event_interrupt_class_cik,
Yair Shacharfbeb6612015-05-20 13:48:26 +030047 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050048 .mqd_size_aligned = MQD_SIZE_ALIGNED,
49 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050050 .needs_iommu_device = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050051 .needs_pci_atomics = false,
Ben Goz0da75582015-01-01 17:10:01 +020052};
53
54static const struct kfd_device_info carrizo_device_info = {
55 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030056 .max_pasid_bits = 16,
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030057 /* max num of queues for CZ.TODO should be a dynamic value */
58 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040059 .doorbell_size = 4,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030060 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030061 .event_interrupt_class = &event_interrupt_class_cik,
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030062 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050063 .mqd_size_aligned = MQD_SIZE_ALIGNED,
64 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050065 .needs_iommu_device = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050066 .needs_pci_atomics = false,
Oded Gabbay4a488a72014-07-16 21:08:55 +030067};
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050068#endif
Oded Gabbay4a488a72014-07-16 21:08:55 +030069
Felix Kuehlinga3084e62018-01-04 17:17:47 -050070static const struct kfd_device_info hawaii_device_info = {
71 .asic_family = CHIP_HAWAII,
72 .max_pasid_bits = 16,
73 /* max num of queues for KV.TODO should be a dynamic value */
74 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040075 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050076 .ih_ring_entry_size = 4 * sizeof(uint32_t),
77 .event_interrupt_class = &event_interrupt_class_cik,
78 .num_of_watch_points = 4,
79 .mqd_size_aligned = MQD_SIZE_ALIGNED,
80 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050081 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050082 .needs_pci_atomics = false,
83};
84
85static const struct kfd_device_info tonga_device_info = {
86 .asic_family = CHIP_TONGA,
87 .max_pasid_bits = 16,
88 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -040089 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050090 .ih_ring_entry_size = 4 * sizeof(uint32_t),
91 .event_interrupt_class = &event_interrupt_class_cik,
92 .num_of_watch_points = 4,
93 .mqd_size_aligned = MQD_SIZE_ALIGNED,
94 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -050095 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -050096 .needs_pci_atomics = true,
97};
98
99static const struct kfd_device_info tonga_vf_device_info = {
100 .asic_family = CHIP_TONGA,
101 .max_pasid_bits = 16,
102 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400103 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500104 .ih_ring_entry_size = 4 * sizeof(uint32_t),
105 .event_interrupt_class = &event_interrupt_class_cik,
106 .num_of_watch_points = 4,
107 .mqd_size_aligned = MQD_SIZE_ALIGNED,
108 .supports_cwsr = false,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500109 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500110 .needs_pci_atomics = false,
111};
112
113static const struct kfd_device_info fiji_device_info = {
114 .asic_family = CHIP_FIJI,
115 .max_pasid_bits = 16,
116 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400117 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500118 .ih_ring_entry_size = 4 * sizeof(uint32_t),
119 .event_interrupt_class = &event_interrupt_class_cik,
120 .num_of_watch_points = 4,
121 .mqd_size_aligned = MQD_SIZE_ALIGNED,
122 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500123 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500124 .needs_pci_atomics = true,
125};
126
127static const struct kfd_device_info fiji_vf_device_info = {
128 .asic_family = CHIP_FIJI,
129 .max_pasid_bits = 16,
130 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400131 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500132 .ih_ring_entry_size = 4 * sizeof(uint32_t),
133 .event_interrupt_class = &event_interrupt_class_cik,
134 .num_of_watch_points = 4,
135 .mqd_size_aligned = MQD_SIZE_ALIGNED,
136 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500137 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500138 .needs_pci_atomics = false,
139};
140
141
142static const struct kfd_device_info polaris10_device_info = {
143 .asic_family = CHIP_POLARIS10,
144 .max_pasid_bits = 16,
145 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400146 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500147 .ih_ring_entry_size = 4 * sizeof(uint32_t),
148 .event_interrupt_class = &event_interrupt_class_cik,
149 .num_of_watch_points = 4,
150 .mqd_size_aligned = MQD_SIZE_ALIGNED,
151 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500152 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500153 .needs_pci_atomics = true,
154};
155
156static const struct kfd_device_info polaris10_vf_device_info = {
157 .asic_family = CHIP_POLARIS10,
158 .max_pasid_bits = 16,
159 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400160 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500161 .ih_ring_entry_size = 4 * sizeof(uint32_t),
162 .event_interrupt_class = &event_interrupt_class_cik,
163 .num_of_watch_points = 4,
164 .mqd_size_aligned = MQD_SIZE_ALIGNED,
165 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500166 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500167 .needs_pci_atomics = false,
168};
169
170static const struct kfd_device_info polaris11_device_info = {
171 .asic_family = CHIP_POLARIS11,
172 .max_pasid_bits = 16,
173 .max_no_of_hqd = 24,
Felix Kuehlingada2b292018-04-10 17:33:03 -0400174 .doorbell_size = 4,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500175 .ih_ring_entry_size = 4 * sizeof(uint32_t),
176 .event_interrupt_class = &event_interrupt_class_cik,
177 .num_of_watch_points = 4,
178 .mqd_size_aligned = MQD_SIZE_ALIGNED,
179 .supports_cwsr = true,
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500180 .needs_iommu_device = false,
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500181 .needs_pci_atomics = true,
182};
183
184
Oded Gabbay4a488a72014-07-16 21:08:55 +0300185struct kfd_deviceid {
186 unsigned short did;
187 const struct kfd_device_info *device_info;
188};
189
Oded Gabbay4a488a72014-07-16 21:08:55 +0300190static const struct kfd_deviceid supported_devices[] = {
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500191#ifdef KFD_SUPPORT_IOMMU_V2
Oded Gabbay4a488a72014-07-16 21:08:55 +0300192 { 0x1304, &kaveri_device_info }, /* Kaveri */
193 { 0x1305, &kaveri_device_info }, /* Kaveri */
194 { 0x1306, &kaveri_device_info }, /* Kaveri */
195 { 0x1307, &kaveri_device_info }, /* Kaveri */
196 { 0x1309, &kaveri_device_info }, /* Kaveri */
197 { 0x130A, &kaveri_device_info }, /* Kaveri */
198 { 0x130B, &kaveri_device_info }, /* Kaveri */
199 { 0x130C, &kaveri_device_info }, /* Kaveri */
200 { 0x130D, &kaveri_device_info }, /* Kaveri */
201 { 0x130E, &kaveri_device_info }, /* Kaveri */
202 { 0x130F, &kaveri_device_info }, /* Kaveri */
203 { 0x1310, &kaveri_device_info }, /* Kaveri */
204 { 0x1311, &kaveri_device_info }, /* Kaveri */
205 { 0x1312, &kaveri_device_info }, /* Kaveri */
206 { 0x1313, &kaveri_device_info }, /* Kaveri */
207 { 0x1315, &kaveri_device_info }, /* Kaveri */
208 { 0x1316, &kaveri_device_info }, /* Kaveri */
209 { 0x1317, &kaveri_device_info }, /* Kaveri */
210 { 0x1318, &kaveri_device_info }, /* Kaveri */
211 { 0x131B, &kaveri_device_info }, /* Kaveri */
212 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz123576d2015-01-12 14:37:24 +0200213 { 0x131D, &kaveri_device_info }, /* Kaveri */
214 { 0x9870, &carrizo_device_info }, /* Carrizo */
215 { 0x9874, &carrizo_device_info }, /* Carrizo */
216 { 0x9875, &carrizo_device_info }, /* Carrizo */
217 { 0x9876, &carrizo_device_info }, /* Carrizo */
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500218 { 0x9877, &carrizo_device_info }, /* Carrizo */
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500219#endif
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500220 { 0x67A0, &hawaii_device_info }, /* Hawaii */
221 { 0x67A1, &hawaii_device_info }, /* Hawaii */
222 { 0x67A2, &hawaii_device_info }, /* Hawaii */
223 { 0x67A8, &hawaii_device_info }, /* Hawaii */
224 { 0x67A9, &hawaii_device_info }, /* Hawaii */
225 { 0x67AA, &hawaii_device_info }, /* Hawaii */
226 { 0x67B0, &hawaii_device_info }, /* Hawaii */
227 { 0x67B1, &hawaii_device_info }, /* Hawaii */
228 { 0x67B8, &hawaii_device_info }, /* Hawaii */
229 { 0x67B9, &hawaii_device_info }, /* Hawaii */
230 { 0x67BA, &hawaii_device_info }, /* Hawaii */
231 { 0x67BE, &hawaii_device_info }, /* Hawaii */
232 { 0x6920, &tonga_device_info }, /* Tonga */
233 { 0x6921, &tonga_device_info }, /* Tonga */
234 { 0x6928, &tonga_device_info }, /* Tonga */
235 { 0x6929, &tonga_device_info }, /* Tonga */
236 { 0x692B, &tonga_device_info }, /* Tonga */
237 { 0x692F, &tonga_vf_device_info }, /* Tonga vf */
238 { 0x6938, &tonga_device_info }, /* Tonga */
239 { 0x6939, &tonga_device_info }, /* Tonga */
240 { 0x7300, &fiji_device_info }, /* Fiji */
241 { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/
242 { 0x67C0, &polaris10_device_info }, /* Polaris10 */
243 { 0x67C1, &polaris10_device_info }, /* Polaris10 */
244 { 0x67C2, &polaris10_device_info }, /* Polaris10 */
245 { 0x67C4, &polaris10_device_info }, /* Polaris10 */
246 { 0x67C7, &polaris10_device_info }, /* Polaris10 */
247 { 0x67C8, &polaris10_device_info }, /* Polaris10 */
248 { 0x67C9, &polaris10_device_info }, /* Polaris10 */
249 { 0x67CA, &polaris10_device_info }, /* Polaris10 */
250 { 0x67CC, &polaris10_device_info }, /* Polaris10 */
251 { 0x67CF, &polaris10_device_info }, /* Polaris10 */
252 { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/
253 { 0x67DF, &polaris10_device_info }, /* Polaris10 */
254 { 0x67E0, &polaris11_device_info }, /* Polaris11 */
255 { 0x67E1, &polaris11_device_info }, /* Polaris11 */
256 { 0x67E3, &polaris11_device_info }, /* Polaris11 */
257 { 0x67E7, &polaris11_device_info }, /* Polaris11 */
258 { 0x67E8, &polaris11_device_info }, /* Polaris11 */
259 { 0x67E9, &polaris11_device_info }, /* Polaris11 */
260 { 0x67EB, &polaris11_device_info }, /* Polaris11 */
261 { 0x67EF, &polaris11_device_info }, /* Polaris11 */
262 { 0x67FF, &polaris11_device_info }, /* Polaris11 */
Oded Gabbay4a488a72014-07-16 21:08:55 +0300263};
264
Oded Gabbay6e810902014-10-27 14:36:07 +0200265static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
266 unsigned int chunk_size);
267static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
268
Yong Zhaob8935a72017-09-20 18:10:13 -0400269static int kfd_resume(struct kfd_dev *kfd);
270
Oded Gabbay4a488a72014-07-16 21:08:55 +0300271static const struct kfd_device_info *lookup_device_info(unsigned short did)
272{
273 size_t i;
274
275 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
276 if (supported_devices[i].did == did) {
Felix Kuehling32fa8212017-08-15 23:00:12 -0400277 WARN_ON(!supported_devices[i].device_info);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300278 return supported_devices[i].device_info;
279 }
280 }
281
Yong Zhao4ebc7182017-08-15 23:00:13 -0400282 dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
283 did);
284
Oded Gabbay4a488a72014-07-16 21:08:55 +0300285 return NULL;
286}
287
Xihan Zhangcea405b2015-03-17 19:32:53 +0800288struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
289 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300290{
291 struct kfd_dev *kfd;
292
293 const struct kfd_device_info *device_info =
294 lookup_device_info(pdev->device);
295
Yong Zhao4ebc7182017-08-15 23:00:13 -0400296 if (!device_info) {
297 dev_err(kfd_device, "kgd2kfd_probe failed\n");
Oded Gabbay4a488a72014-07-16 21:08:55 +0300298 return NULL;
Yong Zhao4ebc7182017-08-15 23:00:13 -0400299 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300300
Felix Kuehling3ee2d002018-01-04 17:17:41 -0500301 if (device_info->needs_pci_atomics) {
302 /* Allow BIF to recode atomics to PCIe 3.0
303 * AtomicOps. 32 and 64-bit requests are possible and
304 * must be supported.
305 */
306 if (pci_enable_atomic_ops_to_root(pdev,
307 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
308 PCI_EXP_DEVCAP2_ATOMIC_COMP64) < 0) {
309 dev_info(kfd_device,
310 "skipped device %x:%x, PCI rejects atomics",
311 pdev->vendor, pdev->device);
312 return NULL;
313 }
314 }
315
Oded Gabbay4a488a72014-07-16 21:08:55 +0300316 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
317 if (!kfd)
318 return NULL;
319
320 kfd->kgd = kgd;
321 kfd->device_info = device_info;
322 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300323 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800324 kfd->kfd2kgd = f2g;
325
326 mutex_init(&kfd->doorbell_mutex);
327 memset(&kfd->doorbell_available_index, 0,
328 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300329
330 return kfd;
331}
332
Felix Kuehling373d7082017-11-14 16:41:19 -0500333static void kfd_cwsr_init(struct kfd_dev *kfd)
334{
335 if (cwsr_enable && kfd->device_info->supports_cwsr) {
336 BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
337
338 kfd->cwsr_isa = cwsr_trap_gfx8_hex;
339 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
340 kfd->cwsr_enabled = true;
341 }
342}
343
Oded Gabbay4a488a72014-07-16 21:08:55 +0300344bool kgd2kfd_device_init(struct kfd_dev *kfd,
345 const struct kgd2kfd_shared_resources *gpu_resources)
346{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300347 unsigned int size;
348
Oded Gabbay4a488a72014-07-16 21:08:55 +0300349 kfd->shared_resources = *gpu_resources;
350
Yong Zhao44008d72017-09-20 18:10:18 -0400351 kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
352 kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
353 kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
354 - kfd->vm_info.first_vmid_kfd + 1;
355
Felix Kuehlinga99c6d42017-11-27 18:29:45 -0500356 /* Verify module parameters regarding mapped process number*/
357 if ((hws_max_conc_proc < 0)
358 || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
359 dev_err(kfd_device,
360 "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
361 hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
362 kfd->vm_info.vmid_num_kfd);
363 kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
364 } else
365 kfd->max_proc_per_quantum = hws_max_conc_proc;
366
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300367 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200368 size = max_num_of_queues_per_device *
369 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300370
Oded Gabbaye18e7942014-10-26 10:12:22 +0200371 /*
372 * calculate max size of runlist packet.
373 * There can be only 2 packets at once
374 */
Felix Kuehling507968d2017-08-15 23:00:15 -0400375 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
376 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
377 + sizeof(struct pm4_mes_runlist)) * 2;
Oded Gabbaye18e7942014-10-26 10:12:22 +0200378
379 /* Add size of HIQ & DIQ */
380 size += KFD_KERNEL_QUEUE_SIZE * 2;
381
382 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300383 size += 512 * 1024;
384
Xihan Zhangcea405b2015-03-17 19:32:53 +0800385 if (kfd->kfd2kgd->init_gtt_mem_allocation(
386 kfd->kgd, size, &kfd->gtt_mem,
387 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
Kent Russell79775b62017-08-15 23:00:05 -0400388 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300389 goto out;
390 }
391
Kent Russell79775b62017-08-15 23:00:05 -0400392 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
Oded Gabbaye18e7942014-10-26 10:12:22 +0200393
Oded Gabbay73a1da02014-10-26 09:53:37 +0200394 /* Initialize GTT sa with 512 byte chunk size */
395 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
Kent Russell79775b62017-08-15 23:00:05 -0400396 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
Oded Gabbay73a1da02014-10-26 09:53:37 +0200397 goto kfd_gtt_sa_init_error;
398 }
399
Felix Kuehling735df2b2017-08-15 23:00:10 -0400400 if (kfd_doorbell_init(kfd)) {
401 dev_err(kfd_device,
402 "Error initializing doorbell aperture\n");
403 goto kfd_doorbell_error;
404 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300405
Kent Russell4eacc26b2017-08-15 23:00:06 -0400406 if (kfd_topology_add_device(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400407 dev_err(kfd_device, "Error adding device to topology\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300408 goto kfd_topology_add_device_error;
409 }
410
Andrew Lewycky2249d552014-07-17 01:37:30 +0300411 if (kfd_interrupt_init(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400412 dev_err(kfd_device, "Error initializing interrupts\n");
Andrew Lewycky2249d552014-07-17 01:37:30 +0300413 goto kfd_interrupt_error;
414 }
415
Ben Goz64c7f8c2014-07-17 01:27:00 +0300416 kfd->dqm = device_queue_manager_init(kfd);
417 if (!kfd->dqm) {
Kent Russell79775b62017-08-15 23:00:05 -0400418 dev_err(kfd_device, "Error initializing queue manager\n");
Ben Goz64c7f8c2014-07-17 01:27:00 +0300419 goto device_queue_manager_error;
420 }
421
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500422 if (kfd_iommu_device_init(kfd)) {
423 dev_err(kfd_device, "Error initializing iommuv2\n");
424 goto device_iommu_error;
Ben Goz64c7f8c2014-07-17 01:27:00 +0300425 }
426
Felix Kuehling373d7082017-11-14 16:41:19 -0500427 kfd_cwsr_init(kfd);
428
Yong Zhaob8935a72017-09-20 18:10:13 -0400429 if (kfd_resume(kfd))
430 goto kfd_resume_error;
431
Yair Shacharfbeb6612015-05-20 13:48:26 +0300432 kfd->dbgmgr = NULL;
433
Oded Gabbay4a488a72014-07-16 21:08:55 +0300434 kfd->init_complete = true;
Kent Russell79775b62017-08-15 23:00:05 -0400435 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
Oded Gabbay4a488a72014-07-16 21:08:55 +0300436 kfd->pdev->device);
437
Kent Russell79775b62017-08-15 23:00:05 -0400438 pr_debug("Starting kfd with the following scheduling policy %d\n",
Felix Kuehlingd146c5a2018-01-04 17:17:43 -0500439 kfd->dqm->sched_policy);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300440
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300441 goto out;
442
Yong Zhaob8935a72017-09-20 18:10:13 -0400443kfd_resume_error:
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500444device_iommu_error:
Ben Goz64c7f8c2014-07-17 01:27:00 +0300445 device_queue_manager_uninit(kfd->dqm);
446device_queue_manager_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300447 kfd_interrupt_exit(kfd);
448kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300449 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300450kfd_topology_add_device_error:
Felix Kuehling735df2b2017-08-15 23:00:10 -0400451 kfd_doorbell_fini(kfd);
452kfd_doorbell_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200453 kfd_gtt_sa_fini(kfd);
454kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800455 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300456 dev_err(kfd_device,
Kent Russell79775b62017-08-15 23:00:05 -0400457 "device %x:%x NOT added due to errors\n",
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300458 kfd->pdev->vendor, kfd->pdev->device);
459out:
460 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300461}
462
463void kgd2kfd_device_exit(struct kfd_dev *kfd)
464{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300465 if (kfd->init_complete) {
Yong Zhaob8935a72017-09-20 18:10:13 -0400466 kgd2kfd_suspend(kfd);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300467 device_queue_manager_uninit(kfd->dqm);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300468 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300469 kfd_topology_remove_device(kfd);
Felix Kuehling735df2b2017-08-15 23:00:10 -0400470 kfd_doorbell_fini(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200471 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800472 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300473 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300474
Oded Gabbay4a488a72014-07-16 21:08:55 +0300475 kfree(kfd);
476}
477
478void kgd2kfd_suspend(struct kfd_dev *kfd)
479{
Yong Zhao733fa1f2017-09-20 18:10:14 -0400480 if (!kfd->init_complete)
481 return;
482
Felix Kuehling26103432018-02-06 20:32:45 -0500483 /* For first KFD device suspend all the KFD processes */
484 if (atomic_inc_return(&kfd_device_suspended) == 1)
485 kfd_suspend_all_processes();
486
Yong Zhao733fa1f2017-09-20 18:10:14 -0400487 kfd->dqm->ops.stop(kfd->dqm);
488
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500489 kfd_iommu_suspend(kfd);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300490}
491
492int kgd2kfd_resume(struct kfd_dev *kfd)
493{
Felix Kuehling26103432018-02-06 20:32:45 -0500494 int ret, count;
495
Yong Zhaob8935a72017-09-20 18:10:13 -0400496 if (!kfd->init_complete)
497 return 0;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300498
Felix Kuehling26103432018-02-06 20:32:45 -0500499 ret = kfd_resume(kfd);
500 if (ret)
501 return ret;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300502
Felix Kuehling26103432018-02-06 20:32:45 -0500503 count = atomic_dec_return(&kfd_device_suspended);
504 WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
505 if (count == 0)
506 ret = kfd_resume_all_processes();
507
508 return ret;
Yong Zhaob8935a72017-09-20 18:10:13 -0400509}
Yong Zhao4ebc7182017-08-15 23:00:13 -0400510
Yong Zhaob8935a72017-09-20 18:10:13 -0400511static int kfd_resume(struct kfd_dev *kfd)
512{
513 int err = 0;
Yong Zhaob8935a72017-09-20 18:10:13 -0400514
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500515 err = kfd_iommu_resume(kfd);
516 if (err) {
517 dev_err(kfd_device,
518 "Failed to resume IOMMU for device %x:%x\n",
519 kfd->pdev->vendor, kfd->pdev->device);
520 return err;
521 }
Yong Zhao733fa1f2017-09-20 18:10:14 -0400522
Yong Zhaob8935a72017-09-20 18:10:13 -0400523 err = kfd->dqm->ops.start(kfd->dqm);
524 if (err) {
525 dev_err(kfd_device,
526 "Error starting queue manager for device %x:%x\n",
527 kfd->pdev->vendor, kfd->pdev->device);
528 goto dqm_start_error;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300529 }
530
Yong Zhaob8935a72017-09-20 18:10:13 -0400531 return err;
532
533dqm_start_error:
Felix Kuehling64d1c3a2017-12-08 19:22:12 -0500534 kfd_iommu_suspend(kfd);
Yong Zhaob8935a72017-09-20 18:10:13 -0400535 return err;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300536}
537
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300538/* This is called directly from KGD at ISR. */
539void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300540{
Andrew Lewycky2249d552014-07-17 01:37:30 +0300541 if (!kfd->init_complete)
542 return;
543
544 spin_lock(&kfd->interrupt_lock);
545
546 if (kfd->interrupts_active
547 && interrupt_is_wanted(kfd, ih_ring_entry)
548 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
Andres Rodriguez48e876a2017-10-27 19:35:34 -0400549 queue_work(kfd->ih_wq, &kfd->interrupt_work);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300550
551 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300552}
Oded Gabbay6e810902014-10-27 14:36:07 +0200553
Felix Kuehling6b95e792018-03-23 15:32:32 -0400554int kgd2kfd_quiesce_mm(struct mm_struct *mm)
555{
556 struct kfd_process *p;
557 int r;
558
559 /* Because we are called from arbitrary context (workqueue) as opposed
560 * to process context, kfd_process could attempt to exit while we are
561 * running so the lookup function increments the process ref count.
562 */
563 p = kfd_lookup_process_by_mm(mm);
564 if (!p)
565 return -ESRCH;
566
567 r = kfd_process_evict_queues(p);
568
569 kfd_unref_process(p);
570 return r;
571}
572
573int kgd2kfd_resume_mm(struct mm_struct *mm)
574{
575 struct kfd_process *p;
576 int r;
577
578 /* Because we are called from arbitrary context (workqueue) as opposed
579 * to process context, kfd_process could attempt to exit while we are
580 * running so the lookup function increments the process ref count.
581 */
582 p = kfd_lookup_process_by_mm(mm);
583 if (!p)
584 return -ESRCH;
585
586 r = kfd_process_restore_queues(p);
587
588 kfd_unref_process(p);
589 return r;
590}
591
Felix Kuehling26103432018-02-06 20:32:45 -0500592/** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
593 * prepare for safe eviction of KFD BOs that belong to the specified
594 * process.
595 *
596 * @mm: mm_struct that identifies the specified KFD process
597 * @fence: eviction fence attached to KFD process BOs
598 *
599 */
600int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
601 struct dma_fence *fence)
602{
603 struct kfd_process *p;
604 unsigned long active_time;
605 unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
606
607 if (!fence)
608 return -EINVAL;
609
610 if (dma_fence_is_signaled(fence))
611 return 0;
612
613 p = kfd_lookup_process_by_mm(mm);
614 if (!p)
615 return -ENODEV;
616
617 if (fence->seqno == p->last_eviction_seqno)
618 goto out;
619
620 p->last_eviction_seqno = fence->seqno;
621
622 /* Avoid KFD process starvation. Wait for at least
623 * PROCESS_ACTIVE_TIME_MS before evicting the process again
624 */
625 active_time = get_jiffies_64() - p->last_restore_timestamp;
626 if (delay_jiffies > active_time)
627 delay_jiffies -= active_time;
628 else
629 delay_jiffies = 0;
630
631 /* During process initialization eviction_work.dwork is initialized
632 * to kfd_evict_bo_worker
633 */
634 schedule_delayed_work(&p->eviction_work, delay_jiffies);
635out:
636 kfd_unref_process(p);
637 return 0;
638}
639
Oded Gabbay6e810902014-10-27 14:36:07 +0200640static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
641 unsigned int chunk_size)
642{
Felix Kuehling8625ff92017-08-15 23:00:11 -0400643 unsigned int num_of_longs;
Oded Gabbay6e810902014-10-27 14:36:07 +0200644
Felix Kuehling32fa8212017-08-15 23:00:12 -0400645 if (WARN_ON(buf_size < chunk_size))
646 return -EINVAL;
647 if (WARN_ON(buf_size == 0))
648 return -EINVAL;
649 if (WARN_ON(chunk_size == 0))
650 return -EINVAL;
Oded Gabbay6e810902014-10-27 14:36:07 +0200651
652 kfd->gtt_sa_chunk_size = chunk_size;
653 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
654
Felix Kuehling8625ff92017-08-15 23:00:11 -0400655 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
656 BITS_PER_LONG;
Oded Gabbay6e810902014-10-27 14:36:07 +0200657
Felix Kuehling8625ff92017-08-15 23:00:11 -0400658 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
Oded Gabbay6e810902014-10-27 14:36:07 +0200659
660 if (!kfd->gtt_sa_bitmap)
661 return -ENOMEM;
662
Kent Russell79775b62017-08-15 23:00:05 -0400663 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200664 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
665
666 mutex_init(&kfd->gtt_sa_lock);
667
668 return 0;
669
670}
671
672static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
673{
674 mutex_destroy(&kfd->gtt_sa_lock);
675 kfree(kfd->gtt_sa_bitmap);
676}
677
678static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
679 unsigned int bit_num,
680 unsigned int chunk_size)
681{
682 return start_addr + bit_num * chunk_size;
683}
684
685static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
686 unsigned int bit_num,
687 unsigned int chunk_size)
688{
689 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
690}
691
692int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
693 struct kfd_mem_obj **mem_obj)
694{
695 unsigned int found, start_search, cur_size;
696
Oded Gabbay6e810902014-10-27 14:36:07 +0200697 if (size == 0)
698 return -EINVAL;
699
700 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
701 return -ENOMEM;
702
Felix Kuehlingd1853f42018-03-23 15:32:31 -0400703 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
Oded Gabbay6e810902014-10-27 14:36:07 +0200704 if ((*mem_obj) == NULL)
705 return -ENOMEM;
706
Kent Russell79775b62017-08-15 23:00:05 -0400707 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
Oded Gabbay6e810902014-10-27 14:36:07 +0200708
709 start_search = 0;
710
711 mutex_lock(&kfd->gtt_sa_lock);
712
713kfd_gtt_restart_search:
714 /* Find the first chunk that is free */
715 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
716 kfd->gtt_sa_num_of_chunks,
717 start_search);
718
Kent Russell79775b62017-08-15 23:00:05 -0400719 pr_debug("Found = %d\n", found);
Oded Gabbay6e810902014-10-27 14:36:07 +0200720
721 /* If there wasn't any free chunk, bail out */
722 if (found == kfd->gtt_sa_num_of_chunks)
723 goto kfd_gtt_no_free_chunk;
724
725 /* Update fields of mem_obj */
726 (*mem_obj)->range_start = found;
727 (*mem_obj)->range_end = found;
728 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
729 kfd->gtt_start_gpu_addr,
730 found,
731 kfd->gtt_sa_chunk_size);
732 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
733 kfd->gtt_start_cpu_ptr,
734 found,
735 kfd->gtt_sa_chunk_size);
736
Kent Russell79775b62017-08-15 23:00:05 -0400737 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200738 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
739
740 /* If we need only one chunk, mark it as allocated and get out */
741 if (size <= kfd->gtt_sa_chunk_size) {
Kent Russell79775b62017-08-15 23:00:05 -0400742 pr_debug("Single bit\n");
Oded Gabbay6e810902014-10-27 14:36:07 +0200743 set_bit(found, kfd->gtt_sa_bitmap);
744 goto kfd_gtt_out;
745 }
746
747 /* Otherwise, try to see if we have enough contiguous chunks */
748 cur_size = size - kfd->gtt_sa_chunk_size;
749 do {
750 (*mem_obj)->range_end =
751 find_next_zero_bit(kfd->gtt_sa_bitmap,
752 kfd->gtt_sa_num_of_chunks, ++found);
753 /*
754 * If next free chunk is not contiguous than we need to
755 * restart our search from the last free chunk we found (which
756 * wasn't contiguous to the previous ones
757 */
758 if ((*mem_obj)->range_end != found) {
759 start_search = found;
760 goto kfd_gtt_restart_search;
761 }
762
763 /*
764 * If we reached end of buffer, bail out with error
765 */
766 if (found == kfd->gtt_sa_num_of_chunks)
767 goto kfd_gtt_no_free_chunk;
768
769 /* Check if we don't need another chunk */
770 if (cur_size <= kfd->gtt_sa_chunk_size)
771 cur_size = 0;
772 else
773 cur_size -= kfd->gtt_sa_chunk_size;
774
775 } while (cur_size > 0);
776
Kent Russell79775b62017-08-15 23:00:05 -0400777 pr_debug("range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200778 (*mem_obj)->range_start, (*mem_obj)->range_end);
779
780 /* Mark the chunks as allocated */
781 for (found = (*mem_obj)->range_start;
782 found <= (*mem_obj)->range_end;
783 found++)
784 set_bit(found, kfd->gtt_sa_bitmap);
785
786kfd_gtt_out:
787 mutex_unlock(&kfd->gtt_sa_lock);
788 return 0;
789
790kfd_gtt_no_free_chunk:
Kent Russell79775b62017-08-15 23:00:05 -0400791 pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
Oded Gabbay6e810902014-10-27 14:36:07 +0200792 mutex_unlock(&kfd->gtt_sa_lock);
793 kfree(mem_obj);
794 return -ENOMEM;
795}
796
797int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
798{
799 unsigned int bit;
800
Oded Gabbay9216ed22015-01-12 22:34:21 +0200801 /* Act like kfree when trying to free a NULL object */
802 if (!mem_obj)
803 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200804
Kent Russell79775b62017-08-15 23:00:05 -0400805 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200806 mem_obj, mem_obj->range_start, mem_obj->range_end);
807
808 mutex_lock(&kfd->gtt_sa_lock);
809
810 /* Mark the chunks as free */
811 for (bit = mem_obj->range_start;
812 bit <= mem_obj->range_end;
813 bit++)
814 clear_bit(bit, kfd->gtt_sa_bitmap);
815
816 mutex_unlock(&kfd->gtt_sa_lock);
817
818 kfree(mem_obj);
819 return 0;
820}