blob: 83d6f410890e9310509d5e8fe5ea5a863ddf1bcd [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
23#include <linux/amd-iommu.h>
24#include <linux/bsearch.h>
25#include <linux/pci.h>
26#include <linux/slab.h>
27#include "kfd_priv.h"
Ben Goz64c7f8c2014-07-17 01:27:00 +030028#include "kfd_device_queue_manager.h"
Felix Kuehling507968d2017-08-15 23:00:15 -040029#include "kfd_pm4_headers_vi.h"
Felix Kuehling373d7082017-11-14 16:41:19 -050030#include "cwsr_trap_handler_gfx8.asm"
Oded Gabbay4a488a72014-07-16 21:08:55 +030031
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030032#define MQD_SIZE_ALIGNED 768
33
Oded Gabbay4a488a72014-07-16 21:08:55 +030034static const struct kfd_device_info kaveri_device_info = {
Ben Goz0da75582015-01-01 17:10:01 +020035 .asic_family = CHIP_KAVERI,
36 .max_pasid_bits = 16,
Yair Shachar992839a2015-05-20 13:43:04 +030037 /* max num of queues for KV.TODO should be a dynamic value */
38 .max_no_of_hqd = 24,
Ben Goz0da75582015-01-01 17:10:01 +020039 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030040 .event_interrupt_class = &event_interrupt_class_cik,
Yair Shacharfbeb6612015-05-20 13:48:26 +030041 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050042 .mqd_size_aligned = MQD_SIZE_ALIGNED,
43 .supports_cwsr = false,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050044 .needs_pci_atomics = false,
Ben Goz0da75582015-01-01 17:10:01 +020045};
46
47static const struct kfd_device_info carrizo_device_info = {
48 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030049 .max_pasid_bits = 16,
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030050 /* max num of queues for CZ.TODO should be a dynamic value */
51 .max_no_of_hqd = 24,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030052 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030053 .event_interrupt_class = &event_interrupt_class_cik,
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030054 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050055 .mqd_size_aligned = MQD_SIZE_ALIGNED,
56 .supports_cwsr = true,
Felix Kuehling3ee2d002018-01-04 17:17:41 -050057 .needs_pci_atomics = false,
Oded Gabbay4a488a72014-07-16 21:08:55 +030058};
59
Felix Kuehlinga3084e62018-01-04 17:17:47 -050060static const struct kfd_device_info hawaii_device_info = {
61 .asic_family = CHIP_HAWAII,
62 .max_pasid_bits = 16,
63 /* max num of queues for KV.TODO should be a dynamic value */
64 .max_no_of_hqd = 24,
65 .ih_ring_entry_size = 4 * sizeof(uint32_t),
66 .event_interrupt_class = &event_interrupt_class_cik,
67 .num_of_watch_points = 4,
68 .mqd_size_aligned = MQD_SIZE_ALIGNED,
69 .supports_cwsr = false,
70 .needs_pci_atomics = false,
71};
72
73static const struct kfd_device_info tonga_device_info = {
74 .asic_family = CHIP_TONGA,
75 .max_pasid_bits = 16,
76 .max_no_of_hqd = 24,
77 .ih_ring_entry_size = 4 * sizeof(uint32_t),
78 .event_interrupt_class = &event_interrupt_class_cik,
79 .num_of_watch_points = 4,
80 .mqd_size_aligned = MQD_SIZE_ALIGNED,
81 .supports_cwsr = false,
82 .needs_pci_atomics = true,
83};
84
85static const struct kfd_device_info tonga_vf_device_info = {
86 .asic_family = CHIP_TONGA,
87 .max_pasid_bits = 16,
88 .max_no_of_hqd = 24,
89 .ih_ring_entry_size = 4 * sizeof(uint32_t),
90 .event_interrupt_class = &event_interrupt_class_cik,
91 .num_of_watch_points = 4,
92 .mqd_size_aligned = MQD_SIZE_ALIGNED,
93 .supports_cwsr = false,
94 .needs_pci_atomics = false,
95};
96
97static const struct kfd_device_info fiji_device_info = {
98 .asic_family = CHIP_FIJI,
99 .max_pasid_bits = 16,
100 .max_no_of_hqd = 24,
101 .ih_ring_entry_size = 4 * sizeof(uint32_t),
102 .event_interrupt_class = &event_interrupt_class_cik,
103 .num_of_watch_points = 4,
104 .mqd_size_aligned = MQD_SIZE_ALIGNED,
105 .supports_cwsr = true,
106 .needs_pci_atomics = true,
107};
108
109static const struct kfd_device_info fiji_vf_device_info = {
110 .asic_family = CHIP_FIJI,
111 .max_pasid_bits = 16,
112 .max_no_of_hqd = 24,
113 .ih_ring_entry_size = 4 * sizeof(uint32_t),
114 .event_interrupt_class = &event_interrupt_class_cik,
115 .num_of_watch_points = 4,
116 .mqd_size_aligned = MQD_SIZE_ALIGNED,
117 .supports_cwsr = true,
118 .needs_pci_atomics = false,
119};
120
121
122static const struct kfd_device_info polaris10_device_info = {
123 .asic_family = CHIP_POLARIS10,
124 .max_pasid_bits = 16,
125 .max_no_of_hqd = 24,
126 .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,
131 .needs_pci_atomics = true,
132};
133
134static const struct kfd_device_info polaris10_vf_device_info = {
135 .asic_family = CHIP_POLARIS10,
136 .max_pasid_bits = 16,
137 .max_no_of_hqd = 24,
138 .ih_ring_entry_size = 4 * sizeof(uint32_t),
139 .event_interrupt_class = &event_interrupt_class_cik,
140 .num_of_watch_points = 4,
141 .mqd_size_aligned = MQD_SIZE_ALIGNED,
142 .supports_cwsr = true,
143 .needs_pci_atomics = false,
144};
145
146static const struct kfd_device_info polaris11_device_info = {
147 .asic_family = CHIP_POLARIS11,
148 .max_pasid_bits = 16,
149 .max_no_of_hqd = 24,
150 .ih_ring_entry_size = 4 * sizeof(uint32_t),
151 .event_interrupt_class = &event_interrupt_class_cik,
152 .num_of_watch_points = 4,
153 .mqd_size_aligned = MQD_SIZE_ALIGNED,
154 .supports_cwsr = true,
155 .needs_pci_atomics = true,
156};
157
158
Oded Gabbay4a488a72014-07-16 21:08:55 +0300159struct kfd_deviceid {
160 unsigned short did;
161 const struct kfd_device_info *device_info;
162};
163
Oded Gabbay4a488a72014-07-16 21:08:55 +0300164static const struct kfd_deviceid supported_devices[] = {
165 { 0x1304, &kaveri_device_info }, /* Kaveri */
166 { 0x1305, &kaveri_device_info }, /* Kaveri */
167 { 0x1306, &kaveri_device_info }, /* Kaveri */
168 { 0x1307, &kaveri_device_info }, /* Kaveri */
169 { 0x1309, &kaveri_device_info }, /* Kaveri */
170 { 0x130A, &kaveri_device_info }, /* Kaveri */
171 { 0x130B, &kaveri_device_info }, /* Kaveri */
172 { 0x130C, &kaveri_device_info }, /* Kaveri */
173 { 0x130D, &kaveri_device_info }, /* Kaveri */
174 { 0x130E, &kaveri_device_info }, /* Kaveri */
175 { 0x130F, &kaveri_device_info }, /* Kaveri */
176 { 0x1310, &kaveri_device_info }, /* Kaveri */
177 { 0x1311, &kaveri_device_info }, /* Kaveri */
178 { 0x1312, &kaveri_device_info }, /* Kaveri */
179 { 0x1313, &kaveri_device_info }, /* Kaveri */
180 { 0x1315, &kaveri_device_info }, /* Kaveri */
181 { 0x1316, &kaveri_device_info }, /* Kaveri */
182 { 0x1317, &kaveri_device_info }, /* Kaveri */
183 { 0x1318, &kaveri_device_info }, /* Kaveri */
184 { 0x131B, &kaveri_device_info }, /* Kaveri */
185 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz123576d2015-01-12 14:37:24 +0200186 { 0x131D, &kaveri_device_info }, /* Kaveri */
187 { 0x9870, &carrizo_device_info }, /* Carrizo */
188 { 0x9874, &carrizo_device_info }, /* Carrizo */
189 { 0x9875, &carrizo_device_info }, /* Carrizo */
190 { 0x9876, &carrizo_device_info }, /* Carrizo */
Felix Kuehlinga3084e62018-01-04 17:17:47 -0500191 { 0x9877, &carrizo_device_info }, /* Carrizo */
192 { 0x67A0, &hawaii_device_info }, /* Hawaii */
193 { 0x67A1, &hawaii_device_info }, /* Hawaii */
194 { 0x67A2, &hawaii_device_info }, /* Hawaii */
195 { 0x67A8, &hawaii_device_info }, /* Hawaii */
196 { 0x67A9, &hawaii_device_info }, /* Hawaii */
197 { 0x67AA, &hawaii_device_info }, /* Hawaii */
198 { 0x67B0, &hawaii_device_info }, /* Hawaii */
199 { 0x67B1, &hawaii_device_info }, /* Hawaii */
200 { 0x67B8, &hawaii_device_info }, /* Hawaii */
201 { 0x67B9, &hawaii_device_info }, /* Hawaii */
202 { 0x67BA, &hawaii_device_info }, /* Hawaii */
203 { 0x67BE, &hawaii_device_info }, /* Hawaii */
204 { 0x6920, &tonga_device_info }, /* Tonga */
205 { 0x6921, &tonga_device_info }, /* Tonga */
206 { 0x6928, &tonga_device_info }, /* Tonga */
207 { 0x6929, &tonga_device_info }, /* Tonga */
208 { 0x692B, &tonga_device_info }, /* Tonga */
209 { 0x692F, &tonga_vf_device_info }, /* Tonga vf */
210 { 0x6938, &tonga_device_info }, /* Tonga */
211 { 0x6939, &tonga_device_info }, /* Tonga */
212 { 0x7300, &fiji_device_info }, /* Fiji */
213 { 0x730F, &fiji_vf_device_info }, /* Fiji vf*/
214 { 0x67C0, &polaris10_device_info }, /* Polaris10 */
215 { 0x67C1, &polaris10_device_info }, /* Polaris10 */
216 { 0x67C2, &polaris10_device_info }, /* Polaris10 */
217 { 0x67C4, &polaris10_device_info }, /* Polaris10 */
218 { 0x67C7, &polaris10_device_info }, /* Polaris10 */
219 { 0x67C8, &polaris10_device_info }, /* Polaris10 */
220 { 0x67C9, &polaris10_device_info }, /* Polaris10 */
221 { 0x67CA, &polaris10_device_info }, /* Polaris10 */
222 { 0x67CC, &polaris10_device_info }, /* Polaris10 */
223 { 0x67CF, &polaris10_device_info }, /* Polaris10 */
224 { 0x67D0, &polaris10_vf_device_info }, /* Polaris10 vf*/
225 { 0x67DF, &polaris10_device_info }, /* Polaris10 */
226 { 0x67E0, &polaris11_device_info }, /* Polaris11 */
227 { 0x67E1, &polaris11_device_info }, /* Polaris11 */
228 { 0x67E3, &polaris11_device_info }, /* Polaris11 */
229 { 0x67E7, &polaris11_device_info }, /* Polaris11 */
230 { 0x67E8, &polaris11_device_info }, /* Polaris11 */
231 { 0x67E9, &polaris11_device_info }, /* Polaris11 */
232 { 0x67EB, &polaris11_device_info }, /* Polaris11 */
233 { 0x67EF, &polaris11_device_info }, /* Polaris11 */
234 { 0x67FF, &polaris11_device_info }, /* Polaris11 */
Oded Gabbay4a488a72014-07-16 21:08:55 +0300235};
236
Oded Gabbay6e810902014-10-27 14:36:07 +0200237static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
238 unsigned int chunk_size);
239static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
240
Yong Zhaob8935a72017-09-20 18:10:13 -0400241static int kfd_resume(struct kfd_dev *kfd);
242
Oded Gabbay4a488a72014-07-16 21:08:55 +0300243static const struct kfd_device_info *lookup_device_info(unsigned short did)
244{
245 size_t i;
246
247 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
248 if (supported_devices[i].did == did) {
Felix Kuehling32fa8212017-08-15 23:00:12 -0400249 WARN_ON(!supported_devices[i].device_info);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300250 return supported_devices[i].device_info;
251 }
252 }
253
Yong Zhao4ebc7182017-08-15 23:00:13 -0400254 dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
255 did);
256
Oded Gabbay4a488a72014-07-16 21:08:55 +0300257 return NULL;
258}
259
Xihan Zhangcea405b2015-03-17 19:32:53 +0800260struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
261 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300262{
263 struct kfd_dev *kfd;
264
265 const struct kfd_device_info *device_info =
266 lookup_device_info(pdev->device);
267
Yong Zhao4ebc7182017-08-15 23:00:13 -0400268 if (!device_info) {
269 dev_err(kfd_device, "kgd2kfd_probe failed\n");
Oded Gabbay4a488a72014-07-16 21:08:55 +0300270 return NULL;
Yong Zhao4ebc7182017-08-15 23:00:13 -0400271 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300272
Felix Kuehling3ee2d002018-01-04 17:17:41 -0500273 if (device_info->needs_pci_atomics) {
274 /* Allow BIF to recode atomics to PCIe 3.0
275 * AtomicOps. 32 and 64-bit requests are possible and
276 * must be supported.
277 */
278 if (pci_enable_atomic_ops_to_root(pdev,
279 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
280 PCI_EXP_DEVCAP2_ATOMIC_COMP64) < 0) {
281 dev_info(kfd_device,
282 "skipped device %x:%x, PCI rejects atomics",
283 pdev->vendor, pdev->device);
284 return NULL;
285 }
286 }
287
Oded Gabbay4a488a72014-07-16 21:08:55 +0300288 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
289 if (!kfd)
290 return NULL;
291
292 kfd->kgd = kgd;
293 kfd->device_info = device_info;
294 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300295 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800296 kfd->kfd2kgd = f2g;
297
298 mutex_init(&kfd->doorbell_mutex);
299 memset(&kfd->doorbell_available_index, 0,
300 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300301
302 return kfd;
303}
304
Oded Gabbayb17f0682014-07-17 00:06:27 +0300305static bool device_iommu_pasid_init(struct kfd_dev *kfd)
306{
307 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
308 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
309 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
310
311 struct amd_iommu_device_info iommu_info;
312 unsigned int pasid_limit;
313 int err;
314
315 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
316 if (err < 0) {
317 dev_err(kfd_device,
318 "error getting iommu info. is the iommu enabled?\n");
319 return false;
320 }
321
322 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
Kent Russell79775b62017-08-15 23:00:05 -0400323 dev_err(kfd_device, "error required iommu flags ats %i, pri %i, pasid %i\n",
Oded Gabbayb17f0682014-07-17 00:06:27 +0300324 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
325 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
Kent Russell8eabaf52017-08-15 23:00:04 -0400326 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP)
327 != 0);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300328 return false;
329 }
330
331 pasid_limit = min_t(unsigned int,
Kent Russell8eabaf52017-08-15 23:00:04 -0400332 (unsigned int)(1 << kfd->device_info->max_pasid_bits),
Oded Gabbayb17f0682014-07-17 00:06:27 +0300333 iommu_info.max_pasids);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300334
Oded Gabbayb17f0682014-07-17 00:06:27 +0300335 if (!kfd_set_pasid_limit(pasid_limit)) {
336 dev_err(kfd_device, "error setting pasid limit\n");
Oded Gabbayb17f0682014-07-17 00:06:27 +0300337 return false;
338 }
339
340 return true;
341}
342
343static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
344{
345 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
346
347 if (dev)
Yong Zhao733fa1f2017-09-20 18:10:14 -0400348 kfd_process_iommu_unbind_callback(dev, pasid);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300349}
350
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300351/*
352 * This function called by IOMMU driver on PPR failure
353 */
354static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
355 unsigned long address, u16 flags)
356{
357 struct kfd_dev *dev;
358
359 dev_warn(kfd_device,
360 "Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
361 PCI_BUS_NUM(pdev->devfn),
362 PCI_SLOT(pdev->devfn),
363 PCI_FUNC(pdev->devfn),
364 pasid,
365 address,
366 flags);
367
368 dev = kfd_device_by_pci_dev(pdev);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400369 if (!WARN_ON(!dev))
370 kfd_signal_iommu_event(dev, pasid, address,
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300371 flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
372
373 return AMD_IOMMU_INV_PRI_RSP_INVALID;
374}
375
Felix Kuehling373d7082017-11-14 16:41:19 -0500376static void kfd_cwsr_init(struct kfd_dev *kfd)
377{
378 if (cwsr_enable && kfd->device_info->supports_cwsr) {
379 BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
380
381 kfd->cwsr_isa = cwsr_trap_gfx8_hex;
382 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
383 kfd->cwsr_enabled = true;
384 }
385}
386
Oded Gabbay4a488a72014-07-16 21:08:55 +0300387bool kgd2kfd_device_init(struct kfd_dev *kfd,
388 const struct kgd2kfd_shared_resources *gpu_resources)
389{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300390 unsigned int size;
391
Oded Gabbay4a488a72014-07-16 21:08:55 +0300392 kfd->shared_resources = *gpu_resources;
393
Yong Zhao44008d72017-09-20 18:10:18 -0400394 kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
395 kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
396 kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
397 - kfd->vm_info.first_vmid_kfd + 1;
398
Felix Kuehlinga99c6d42017-11-27 18:29:45 -0500399 /* Verify module parameters regarding mapped process number*/
400 if ((hws_max_conc_proc < 0)
401 || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
402 dev_err(kfd_device,
403 "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
404 hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
405 kfd->vm_info.vmid_num_kfd);
406 kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
407 } else
408 kfd->max_proc_per_quantum = hws_max_conc_proc;
409
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300410 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200411 size = max_num_of_queues_per_device *
412 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300413
Oded Gabbaye18e7942014-10-26 10:12:22 +0200414 /*
415 * calculate max size of runlist packet.
416 * There can be only 2 packets at once
417 */
Felix Kuehling507968d2017-08-15 23:00:15 -0400418 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
419 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
420 + sizeof(struct pm4_mes_runlist)) * 2;
Oded Gabbaye18e7942014-10-26 10:12:22 +0200421
422 /* Add size of HIQ & DIQ */
423 size += KFD_KERNEL_QUEUE_SIZE * 2;
424
425 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300426 size += 512 * 1024;
427
Xihan Zhangcea405b2015-03-17 19:32:53 +0800428 if (kfd->kfd2kgd->init_gtt_mem_allocation(
429 kfd->kgd, size, &kfd->gtt_mem,
430 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
Kent Russell79775b62017-08-15 23:00:05 -0400431 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300432 goto out;
433 }
434
Kent Russell79775b62017-08-15 23:00:05 -0400435 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
Oded Gabbaye18e7942014-10-26 10:12:22 +0200436
Oded Gabbay73a1da02014-10-26 09:53:37 +0200437 /* Initialize GTT sa with 512 byte chunk size */
438 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
Kent Russell79775b62017-08-15 23:00:05 -0400439 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
Oded Gabbay73a1da02014-10-26 09:53:37 +0200440 goto kfd_gtt_sa_init_error;
441 }
442
Felix Kuehling735df2b2017-08-15 23:00:10 -0400443 if (kfd_doorbell_init(kfd)) {
444 dev_err(kfd_device,
445 "Error initializing doorbell aperture\n");
446 goto kfd_doorbell_error;
447 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300448
Kent Russell4eacc26b2017-08-15 23:00:06 -0400449 if (kfd_topology_add_device(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400450 dev_err(kfd_device, "Error adding device to topology\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300451 goto kfd_topology_add_device_error;
452 }
453
Andrew Lewycky2249d552014-07-17 01:37:30 +0300454 if (kfd_interrupt_init(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400455 dev_err(kfd_device, "Error initializing interrupts\n");
Andrew Lewycky2249d552014-07-17 01:37:30 +0300456 goto kfd_interrupt_error;
457 }
458
Ben Goz64c7f8c2014-07-17 01:27:00 +0300459 kfd->dqm = device_queue_manager_init(kfd);
460 if (!kfd->dqm) {
Kent Russell79775b62017-08-15 23:00:05 -0400461 dev_err(kfd_device, "Error initializing queue manager\n");
Ben Goz64c7f8c2014-07-17 01:27:00 +0300462 goto device_queue_manager_error;
463 }
464
Yong Zhaob8935a72017-09-20 18:10:13 -0400465 if (!device_iommu_pasid_init(kfd)) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300466 dev_err(kfd_device,
Yong Zhaob8935a72017-09-20 18:10:13 -0400467 "Error initializing iommuv2 for device %x:%x\n",
Ben Goz64c7f8c2014-07-17 01:27:00 +0300468 kfd->pdev->vendor, kfd->pdev->device);
Yong Zhaob8935a72017-09-20 18:10:13 -0400469 goto device_iommu_pasid_error;
Ben Goz64c7f8c2014-07-17 01:27:00 +0300470 }
471
Felix Kuehling373d7082017-11-14 16:41:19 -0500472 kfd_cwsr_init(kfd);
473
Yong Zhaob8935a72017-09-20 18:10:13 -0400474 if (kfd_resume(kfd))
475 goto kfd_resume_error;
476
Yair Shacharfbeb6612015-05-20 13:48:26 +0300477 kfd->dbgmgr = NULL;
478
Oded Gabbay4a488a72014-07-16 21:08:55 +0300479 kfd->init_complete = true;
Kent Russell79775b62017-08-15 23:00:05 -0400480 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
Oded Gabbay4a488a72014-07-16 21:08:55 +0300481 kfd->pdev->device);
482
Kent Russell79775b62017-08-15 23:00:05 -0400483 pr_debug("Starting kfd with the following scheduling policy %d\n",
Felix Kuehlingd146c5a2018-01-04 17:17:43 -0500484 kfd->dqm->sched_policy);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300485
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300486 goto out;
487
Yong Zhaob8935a72017-09-20 18:10:13 -0400488kfd_resume_error:
489device_iommu_pasid_error:
Ben Goz64c7f8c2014-07-17 01:27:00 +0300490 device_queue_manager_uninit(kfd->dqm);
491device_queue_manager_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300492 kfd_interrupt_exit(kfd);
493kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300494 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300495kfd_topology_add_device_error:
Felix Kuehling735df2b2017-08-15 23:00:10 -0400496 kfd_doorbell_fini(kfd);
497kfd_doorbell_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200498 kfd_gtt_sa_fini(kfd);
499kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800500 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300501 dev_err(kfd_device,
Kent Russell79775b62017-08-15 23:00:05 -0400502 "device %x:%x NOT added due to errors\n",
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300503 kfd->pdev->vendor, kfd->pdev->device);
504out:
505 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300506}
507
508void kgd2kfd_device_exit(struct kfd_dev *kfd)
509{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300510 if (kfd->init_complete) {
Yong Zhaob8935a72017-09-20 18:10:13 -0400511 kgd2kfd_suspend(kfd);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300512 device_queue_manager_uninit(kfd->dqm);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300513 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300514 kfd_topology_remove_device(kfd);
Felix Kuehling735df2b2017-08-15 23:00:10 -0400515 kfd_doorbell_fini(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200516 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800517 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300518 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300519
Oded Gabbay4a488a72014-07-16 21:08:55 +0300520 kfree(kfd);
521}
522
523void kgd2kfd_suspend(struct kfd_dev *kfd)
524{
Yong Zhao733fa1f2017-09-20 18:10:14 -0400525 if (!kfd->init_complete)
526 return;
527
528 kfd->dqm->ops.stop(kfd->dqm);
529
530 kfd_unbind_processes_from_device(kfd);
531
532 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
533 amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
534 amd_iommu_free_device(kfd->pdev);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300535}
536
537int kgd2kfd_resume(struct kfd_dev *kfd)
538{
Yong Zhaob8935a72017-09-20 18:10:13 -0400539 if (!kfd->init_complete)
540 return 0;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300541
Yong Zhaob8935a72017-09-20 18:10:13 -0400542 return kfd_resume(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300543
Yong Zhaob8935a72017-09-20 18:10:13 -0400544}
Yong Zhao4ebc7182017-08-15 23:00:13 -0400545
Yong Zhaob8935a72017-09-20 18:10:13 -0400546static int kfd_resume(struct kfd_dev *kfd)
547{
548 int err = 0;
549 unsigned int pasid_limit = kfd_get_pasid_limit();
550
551 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
552 if (err)
553 return -ENXIO;
554 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
555 iommu_pasid_shutdown_callback);
556 amd_iommu_set_invalid_ppr_cb(kfd->pdev,
557 iommu_invalid_ppr_cb);
558
Yong Zhao733fa1f2017-09-20 18:10:14 -0400559 err = kfd_bind_processes_to_device(kfd);
560 if (err)
561 goto processes_bind_error;
562
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:
Yong Zhao733fa1f2017-09-20 18:10:14 -0400574processes_bind_error:
Yong Zhaob8935a72017-09-20 18:10:13 -0400575 amd_iommu_free_device(kfd->pdev);
576
577 return err;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300578}
579
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300580/* This is called directly from KGD at ISR. */
581void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300582{
Andrew Lewycky2249d552014-07-17 01:37:30 +0300583 if (!kfd->init_complete)
584 return;
585
586 spin_lock(&kfd->interrupt_lock);
587
588 if (kfd->interrupts_active
589 && interrupt_is_wanted(kfd, ih_ring_entry)
590 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
Andres Rodriguez48e876a2017-10-27 19:35:34 -0400591 queue_work(kfd->ih_wq, &kfd->interrupt_work);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300592
593 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300594}
Oded Gabbay6e810902014-10-27 14:36:07 +0200595
596static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
597 unsigned int chunk_size)
598{
Felix Kuehling8625ff92017-08-15 23:00:11 -0400599 unsigned int num_of_longs;
Oded Gabbay6e810902014-10-27 14:36:07 +0200600
Felix Kuehling32fa8212017-08-15 23:00:12 -0400601 if (WARN_ON(buf_size < chunk_size))
602 return -EINVAL;
603 if (WARN_ON(buf_size == 0))
604 return -EINVAL;
605 if (WARN_ON(chunk_size == 0))
606 return -EINVAL;
Oded Gabbay6e810902014-10-27 14:36:07 +0200607
608 kfd->gtt_sa_chunk_size = chunk_size;
609 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
610
Felix Kuehling8625ff92017-08-15 23:00:11 -0400611 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
612 BITS_PER_LONG;
Oded Gabbay6e810902014-10-27 14:36:07 +0200613
Felix Kuehling8625ff92017-08-15 23:00:11 -0400614 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
Oded Gabbay6e810902014-10-27 14:36:07 +0200615
616 if (!kfd->gtt_sa_bitmap)
617 return -ENOMEM;
618
Kent Russell79775b62017-08-15 23:00:05 -0400619 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200620 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
621
622 mutex_init(&kfd->gtt_sa_lock);
623
624 return 0;
625
626}
627
628static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
629{
630 mutex_destroy(&kfd->gtt_sa_lock);
631 kfree(kfd->gtt_sa_bitmap);
632}
633
634static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
635 unsigned int bit_num,
636 unsigned int chunk_size)
637{
638 return start_addr + bit_num * chunk_size;
639}
640
641static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
642 unsigned int bit_num,
643 unsigned int chunk_size)
644{
645 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
646}
647
648int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
649 struct kfd_mem_obj **mem_obj)
650{
651 unsigned int found, start_search, cur_size;
652
Oded Gabbay6e810902014-10-27 14:36:07 +0200653 if (size == 0)
654 return -EINVAL;
655
656 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
657 return -ENOMEM;
658
659 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
660 if ((*mem_obj) == NULL)
661 return -ENOMEM;
662
Kent Russell79775b62017-08-15 23:00:05 -0400663 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
Oded Gabbay6e810902014-10-27 14:36:07 +0200664
665 start_search = 0;
666
667 mutex_lock(&kfd->gtt_sa_lock);
668
669kfd_gtt_restart_search:
670 /* Find the first chunk that is free */
671 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
672 kfd->gtt_sa_num_of_chunks,
673 start_search);
674
Kent Russell79775b62017-08-15 23:00:05 -0400675 pr_debug("Found = %d\n", found);
Oded Gabbay6e810902014-10-27 14:36:07 +0200676
677 /* If there wasn't any free chunk, bail out */
678 if (found == kfd->gtt_sa_num_of_chunks)
679 goto kfd_gtt_no_free_chunk;
680
681 /* Update fields of mem_obj */
682 (*mem_obj)->range_start = found;
683 (*mem_obj)->range_end = found;
684 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
685 kfd->gtt_start_gpu_addr,
686 found,
687 kfd->gtt_sa_chunk_size);
688 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
689 kfd->gtt_start_cpu_ptr,
690 found,
691 kfd->gtt_sa_chunk_size);
692
Kent Russell79775b62017-08-15 23:00:05 -0400693 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200694 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
695
696 /* If we need only one chunk, mark it as allocated and get out */
697 if (size <= kfd->gtt_sa_chunk_size) {
Kent Russell79775b62017-08-15 23:00:05 -0400698 pr_debug("Single bit\n");
Oded Gabbay6e810902014-10-27 14:36:07 +0200699 set_bit(found, kfd->gtt_sa_bitmap);
700 goto kfd_gtt_out;
701 }
702
703 /* Otherwise, try to see if we have enough contiguous chunks */
704 cur_size = size - kfd->gtt_sa_chunk_size;
705 do {
706 (*mem_obj)->range_end =
707 find_next_zero_bit(kfd->gtt_sa_bitmap,
708 kfd->gtt_sa_num_of_chunks, ++found);
709 /*
710 * If next free chunk is not contiguous than we need to
711 * restart our search from the last free chunk we found (which
712 * wasn't contiguous to the previous ones
713 */
714 if ((*mem_obj)->range_end != found) {
715 start_search = found;
716 goto kfd_gtt_restart_search;
717 }
718
719 /*
720 * If we reached end of buffer, bail out with error
721 */
722 if (found == kfd->gtt_sa_num_of_chunks)
723 goto kfd_gtt_no_free_chunk;
724
725 /* Check if we don't need another chunk */
726 if (cur_size <= kfd->gtt_sa_chunk_size)
727 cur_size = 0;
728 else
729 cur_size -= kfd->gtt_sa_chunk_size;
730
731 } while (cur_size > 0);
732
Kent Russell79775b62017-08-15 23:00:05 -0400733 pr_debug("range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200734 (*mem_obj)->range_start, (*mem_obj)->range_end);
735
736 /* Mark the chunks as allocated */
737 for (found = (*mem_obj)->range_start;
738 found <= (*mem_obj)->range_end;
739 found++)
740 set_bit(found, kfd->gtt_sa_bitmap);
741
742kfd_gtt_out:
743 mutex_unlock(&kfd->gtt_sa_lock);
744 return 0;
745
746kfd_gtt_no_free_chunk:
Kent Russell79775b62017-08-15 23:00:05 -0400747 pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
Oded Gabbay6e810902014-10-27 14:36:07 +0200748 mutex_unlock(&kfd->gtt_sa_lock);
749 kfree(mem_obj);
750 return -ENOMEM;
751}
752
753int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
754{
755 unsigned int bit;
756
Oded Gabbay9216ed22015-01-12 22:34:21 +0200757 /* Act like kfree when trying to free a NULL object */
758 if (!mem_obj)
759 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200760
Kent Russell79775b62017-08-15 23:00:05 -0400761 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200762 mem_obj, mem_obj->range_start, mem_obj->range_end);
763
764 mutex_lock(&kfd->gtt_sa_lock);
765
766 /* Mark the chunks as free */
767 for (bit = mem_obj->range_start;
768 bit <= mem_obj->range_end;
769 bit++)
770 clear_bit(bit, kfd->gtt_sa_bitmap);
771
772 mutex_unlock(&kfd->gtt_sa_lock);
773
774 kfree(mem_obj);
775 return 0;
776}