blob: a8fa33a08de301fde244f1584e8a9edeeea40775 [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,
Ben Goz0da75582015-01-01 17:10:01 +020044};
45
46static const struct kfd_device_info carrizo_device_info = {
47 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030048 .max_pasid_bits = 16,
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030049 /* max num of queues for CZ.TODO should be a dynamic value */
50 .max_no_of_hqd = 24,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030051 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030052 .event_interrupt_class = &event_interrupt_class_cik,
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030053 .num_of_watch_points = 4,
Felix Kuehling373d7082017-11-14 16:41:19 -050054 .mqd_size_aligned = MQD_SIZE_ALIGNED,
55 .supports_cwsr = true,
Oded Gabbay4a488a72014-07-16 21:08:55 +030056};
57
58struct kfd_deviceid {
59 unsigned short did;
60 const struct kfd_device_info *device_info;
61};
62
63/* Please keep this sorted by increasing device id. */
64static const struct kfd_deviceid supported_devices[] = {
65 { 0x1304, &kaveri_device_info }, /* Kaveri */
66 { 0x1305, &kaveri_device_info }, /* Kaveri */
67 { 0x1306, &kaveri_device_info }, /* Kaveri */
68 { 0x1307, &kaveri_device_info }, /* Kaveri */
69 { 0x1309, &kaveri_device_info }, /* Kaveri */
70 { 0x130A, &kaveri_device_info }, /* Kaveri */
71 { 0x130B, &kaveri_device_info }, /* Kaveri */
72 { 0x130C, &kaveri_device_info }, /* Kaveri */
73 { 0x130D, &kaveri_device_info }, /* Kaveri */
74 { 0x130E, &kaveri_device_info }, /* Kaveri */
75 { 0x130F, &kaveri_device_info }, /* Kaveri */
76 { 0x1310, &kaveri_device_info }, /* Kaveri */
77 { 0x1311, &kaveri_device_info }, /* Kaveri */
78 { 0x1312, &kaveri_device_info }, /* Kaveri */
79 { 0x1313, &kaveri_device_info }, /* Kaveri */
80 { 0x1315, &kaveri_device_info }, /* Kaveri */
81 { 0x1316, &kaveri_device_info }, /* Kaveri */
82 { 0x1317, &kaveri_device_info }, /* Kaveri */
83 { 0x1318, &kaveri_device_info }, /* Kaveri */
84 { 0x131B, &kaveri_device_info }, /* Kaveri */
85 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz123576d2015-01-12 14:37:24 +020086 { 0x131D, &kaveri_device_info }, /* Kaveri */
87 { 0x9870, &carrizo_device_info }, /* Carrizo */
88 { 0x9874, &carrizo_device_info }, /* Carrizo */
89 { 0x9875, &carrizo_device_info }, /* Carrizo */
90 { 0x9876, &carrizo_device_info }, /* Carrizo */
91 { 0x9877, &carrizo_device_info } /* Carrizo */
Oded Gabbay4a488a72014-07-16 21:08:55 +030092};
93
Oded Gabbay6e810902014-10-27 14:36:07 +020094static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
95 unsigned int chunk_size);
96static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
97
Yong Zhaob8935a72017-09-20 18:10:13 -040098static int kfd_resume(struct kfd_dev *kfd);
99
Oded Gabbay4a488a72014-07-16 21:08:55 +0300100static const struct kfd_device_info *lookup_device_info(unsigned short did)
101{
102 size_t i;
103
104 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
105 if (supported_devices[i].did == did) {
Felix Kuehling32fa8212017-08-15 23:00:12 -0400106 WARN_ON(!supported_devices[i].device_info);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300107 return supported_devices[i].device_info;
108 }
109 }
110
Yong Zhao4ebc7182017-08-15 23:00:13 -0400111 dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
112 did);
113
Oded Gabbay4a488a72014-07-16 21:08:55 +0300114 return NULL;
115}
116
Xihan Zhangcea405b2015-03-17 19:32:53 +0800117struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
118 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300119{
120 struct kfd_dev *kfd;
121
122 const struct kfd_device_info *device_info =
123 lookup_device_info(pdev->device);
124
Yong Zhao4ebc7182017-08-15 23:00:13 -0400125 if (!device_info) {
126 dev_err(kfd_device, "kgd2kfd_probe failed\n");
Oded Gabbay4a488a72014-07-16 21:08:55 +0300127 return NULL;
Yong Zhao4ebc7182017-08-15 23:00:13 -0400128 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300129
130 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
131 if (!kfd)
132 return NULL;
133
134 kfd->kgd = kgd;
135 kfd->device_info = device_info;
136 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300137 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800138 kfd->kfd2kgd = f2g;
139
140 mutex_init(&kfd->doorbell_mutex);
141 memset(&kfd->doorbell_available_index, 0,
142 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300143
144 return kfd;
145}
146
Oded Gabbayb17f0682014-07-17 00:06:27 +0300147static bool device_iommu_pasid_init(struct kfd_dev *kfd)
148{
149 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
150 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
151 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
152
153 struct amd_iommu_device_info iommu_info;
154 unsigned int pasid_limit;
155 int err;
156
157 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
158 if (err < 0) {
159 dev_err(kfd_device,
160 "error getting iommu info. is the iommu enabled?\n");
161 return false;
162 }
163
164 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
Kent Russell79775b62017-08-15 23:00:05 -0400165 dev_err(kfd_device, "error required iommu flags ats %i, pri %i, pasid %i\n",
Oded Gabbayb17f0682014-07-17 00:06:27 +0300166 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
167 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
Kent Russell8eabaf52017-08-15 23:00:04 -0400168 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP)
169 != 0);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300170 return false;
171 }
172
173 pasid_limit = min_t(unsigned int,
Kent Russell8eabaf52017-08-15 23:00:04 -0400174 (unsigned int)(1 << kfd->device_info->max_pasid_bits),
Oded Gabbayb17f0682014-07-17 00:06:27 +0300175 iommu_info.max_pasids);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300176
Oded Gabbayb17f0682014-07-17 00:06:27 +0300177 if (!kfd_set_pasid_limit(pasid_limit)) {
178 dev_err(kfd_device, "error setting pasid limit\n");
Oded Gabbayb17f0682014-07-17 00:06:27 +0300179 return false;
180 }
181
182 return true;
183}
184
185static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
186{
187 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
188
189 if (dev)
Yong Zhao733fa1f2017-09-20 18:10:14 -0400190 kfd_process_iommu_unbind_callback(dev, pasid);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300191}
192
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300193/*
194 * This function called by IOMMU driver on PPR failure
195 */
196static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
197 unsigned long address, u16 flags)
198{
199 struct kfd_dev *dev;
200
201 dev_warn(kfd_device,
202 "Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
203 PCI_BUS_NUM(pdev->devfn),
204 PCI_SLOT(pdev->devfn),
205 PCI_FUNC(pdev->devfn),
206 pasid,
207 address,
208 flags);
209
210 dev = kfd_device_by_pci_dev(pdev);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400211 if (!WARN_ON(!dev))
212 kfd_signal_iommu_event(dev, pasid, address,
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300213 flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
214
215 return AMD_IOMMU_INV_PRI_RSP_INVALID;
216}
217
Felix Kuehling373d7082017-11-14 16:41:19 -0500218static void kfd_cwsr_init(struct kfd_dev *kfd)
219{
220 if (cwsr_enable && kfd->device_info->supports_cwsr) {
221 BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
222
223 kfd->cwsr_isa = cwsr_trap_gfx8_hex;
224 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
225 kfd->cwsr_enabled = true;
226 }
227}
228
Oded Gabbay4a488a72014-07-16 21:08:55 +0300229bool kgd2kfd_device_init(struct kfd_dev *kfd,
230 const struct kgd2kfd_shared_resources *gpu_resources)
231{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300232 unsigned int size;
233
Oded Gabbay4a488a72014-07-16 21:08:55 +0300234 kfd->shared_resources = *gpu_resources;
235
Yong Zhao44008d72017-09-20 18:10:18 -0400236 kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
237 kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
238 kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
239 - kfd->vm_info.first_vmid_kfd + 1;
240
Felix Kuehlinga99c6d42017-11-27 18:29:45 -0500241 /* Verify module parameters regarding mapped process number*/
242 if ((hws_max_conc_proc < 0)
243 || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
244 dev_err(kfd_device,
245 "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
246 hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
247 kfd->vm_info.vmid_num_kfd);
248 kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
249 } else
250 kfd->max_proc_per_quantum = hws_max_conc_proc;
251
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300252 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200253 size = max_num_of_queues_per_device *
254 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300255
Oded Gabbaye18e7942014-10-26 10:12:22 +0200256 /*
257 * calculate max size of runlist packet.
258 * There can be only 2 packets at once
259 */
Felix Kuehling507968d2017-08-15 23:00:15 -0400260 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
261 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
262 + sizeof(struct pm4_mes_runlist)) * 2;
Oded Gabbaye18e7942014-10-26 10:12:22 +0200263
264 /* Add size of HIQ & DIQ */
265 size += KFD_KERNEL_QUEUE_SIZE * 2;
266
267 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300268 size += 512 * 1024;
269
Xihan Zhangcea405b2015-03-17 19:32:53 +0800270 if (kfd->kfd2kgd->init_gtt_mem_allocation(
271 kfd->kgd, size, &kfd->gtt_mem,
272 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
Kent Russell79775b62017-08-15 23:00:05 -0400273 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300274 goto out;
275 }
276
Kent Russell79775b62017-08-15 23:00:05 -0400277 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
Oded Gabbaye18e7942014-10-26 10:12:22 +0200278
Oded Gabbay73a1da02014-10-26 09:53:37 +0200279 /* Initialize GTT sa with 512 byte chunk size */
280 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
Kent Russell79775b62017-08-15 23:00:05 -0400281 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
Oded Gabbay73a1da02014-10-26 09:53:37 +0200282 goto kfd_gtt_sa_init_error;
283 }
284
Felix Kuehling735df2b2017-08-15 23:00:10 -0400285 if (kfd_doorbell_init(kfd)) {
286 dev_err(kfd_device,
287 "Error initializing doorbell aperture\n");
288 goto kfd_doorbell_error;
289 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300290
Kent Russell4eacc26b2017-08-15 23:00:06 -0400291 if (kfd_topology_add_device(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400292 dev_err(kfd_device, "Error adding device to topology\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300293 goto kfd_topology_add_device_error;
294 }
295
Andrew Lewycky2249d552014-07-17 01:37:30 +0300296 if (kfd_interrupt_init(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400297 dev_err(kfd_device, "Error initializing interrupts\n");
Andrew Lewycky2249d552014-07-17 01:37:30 +0300298 goto kfd_interrupt_error;
299 }
300
Ben Goz64c7f8c2014-07-17 01:27:00 +0300301 kfd->dqm = device_queue_manager_init(kfd);
302 if (!kfd->dqm) {
Kent Russell79775b62017-08-15 23:00:05 -0400303 dev_err(kfd_device, "Error initializing queue manager\n");
Ben Goz64c7f8c2014-07-17 01:27:00 +0300304 goto device_queue_manager_error;
305 }
306
Yong Zhaob8935a72017-09-20 18:10:13 -0400307 if (!device_iommu_pasid_init(kfd)) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300308 dev_err(kfd_device,
Yong Zhaob8935a72017-09-20 18:10:13 -0400309 "Error initializing iommuv2 for device %x:%x\n",
Ben Goz64c7f8c2014-07-17 01:27:00 +0300310 kfd->pdev->vendor, kfd->pdev->device);
Yong Zhaob8935a72017-09-20 18:10:13 -0400311 goto device_iommu_pasid_error;
Ben Goz64c7f8c2014-07-17 01:27:00 +0300312 }
313
Felix Kuehling373d7082017-11-14 16:41:19 -0500314 kfd_cwsr_init(kfd);
315
Yong Zhaob8935a72017-09-20 18:10:13 -0400316 if (kfd_resume(kfd))
317 goto kfd_resume_error;
318
Yair Shacharfbeb6612015-05-20 13:48:26 +0300319 kfd->dbgmgr = NULL;
320
Oded Gabbay4a488a72014-07-16 21:08:55 +0300321 kfd->init_complete = true;
Kent Russell79775b62017-08-15 23:00:05 -0400322 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
Oded Gabbay4a488a72014-07-16 21:08:55 +0300323 kfd->pdev->device);
324
Kent Russell79775b62017-08-15 23:00:05 -0400325 pr_debug("Starting kfd with the following scheduling policy %d\n",
Ben Goz64c7f8c2014-07-17 01:27:00 +0300326 sched_policy);
327
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300328 goto out;
329
Yong Zhaob8935a72017-09-20 18:10:13 -0400330kfd_resume_error:
331device_iommu_pasid_error:
Ben Goz64c7f8c2014-07-17 01:27:00 +0300332 device_queue_manager_uninit(kfd->dqm);
333device_queue_manager_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300334 kfd_interrupt_exit(kfd);
335kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300336 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300337kfd_topology_add_device_error:
Felix Kuehling735df2b2017-08-15 23:00:10 -0400338 kfd_doorbell_fini(kfd);
339kfd_doorbell_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200340 kfd_gtt_sa_fini(kfd);
341kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800342 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300343 dev_err(kfd_device,
Kent Russell79775b62017-08-15 23:00:05 -0400344 "device %x:%x NOT added due to errors\n",
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300345 kfd->pdev->vendor, kfd->pdev->device);
346out:
347 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300348}
349
350void kgd2kfd_device_exit(struct kfd_dev *kfd)
351{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300352 if (kfd->init_complete) {
Yong Zhaob8935a72017-09-20 18:10:13 -0400353 kgd2kfd_suspend(kfd);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300354 device_queue_manager_uninit(kfd->dqm);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300355 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300356 kfd_topology_remove_device(kfd);
Felix Kuehling735df2b2017-08-15 23:00:10 -0400357 kfd_doorbell_fini(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200358 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800359 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300360 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300361
Oded Gabbay4a488a72014-07-16 21:08:55 +0300362 kfree(kfd);
363}
364
365void kgd2kfd_suspend(struct kfd_dev *kfd)
366{
Yong Zhao733fa1f2017-09-20 18:10:14 -0400367 if (!kfd->init_complete)
368 return;
369
370 kfd->dqm->ops.stop(kfd->dqm);
371
372 kfd_unbind_processes_from_device(kfd);
373
374 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
375 amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
376 amd_iommu_free_device(kfd->pdev);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300377}
378
379int kgd2kfd_resume(struct kfd_dev *kfd)
380{
Yong Zhaob8935a72017-09-20 18:10:13 -0400381 if (!kfd->init_complete)
382 return 0;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300383
Yong Zhaob8935a72017-09-20 18:10:13 -0400384 return kfd_resume(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300385
Yong Zhaob8935a72017-09-20 18:10:13 -0400386}
Yong Zhao4ebc7182017-08-15 23:00:13 -0400387
Yong Zhaob8935a72017-09-20 18:10:13 -0400388static int kfd_resume(struct kfd_dev *kfd)
389{
390 int err = 0;
391 unsigned int pasid_limit = kfd_get_pasid_limit();
392
393 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
394 if (err)
395 return -ENXIO;
396 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
397 iommu_pasid_shutdown_callback);
398 amd_iommu_set_invalid_ppr_cb(kfd->pdev,
399 iommu_invalid_ppr_cb);
400
Yong Zhao733fa1f2017-09-20 18:10:14 -0400401 err = kfd_bind_processes_to_device(kfd);
402 if (err)
403 goto processes_bind_error;
404
Yong Zhaob8935a72017-09-20 18:10:13 -0400405 err = kfd->dqm->ops.start(kfd->dqm);
406 if (err) {
407 dev_err(kfd_device,
408 "Error starting queue manager for device %x:%x\n",
409 kfd->pdev->vendor, kfd->pdev->device);
410 goto dqm_start_error;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300411 }
412
Yong Zhaob8935a72017-09-20 18:10:13 -0400413 return err;
414
415dqm_start_error:
Yong Zhao733fa1f2017-09-20 18:10:14 -0400416processes_bind_error:
Yong Zhaob8935a72017-09-20 18:10:13 -0400417 amd_iommu_free_device(kfd->pdev);
418
419 return err;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300420}
421
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300422/* This is called directly from KGD at ISR. */
423void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300424{
Andrew Lewycky2249d552014-07-17 01:37:30 +0300425 if (!kfd->init_complete)
426 return;
427
428 spin_lock(&kfd->interrupt_lock);
429
430 if (kfd->interrupts_active
431 && interrupt_is_wanted(kfd, ih_ring_entry)
432 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
Andres Rodriguez48e876a2017-10-27 19:35:34 -0400433 queue_work(kfd->ih_wq, &kfd->interrupt_work);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300434
435 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300436}
Oded Gabbay6e810902014-10-27 14:36:07 +0200437
438static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
439 unsigned int chunk_size)
440{
Felix Kuehling8625ff92017-08-15 23:00:11 -0400441 unsigned int num_of_longs;
Oded Gabbay6e810902014-10-27 14:36:07 +0200442
Felix Kuehling32fa8212017-08-15 23:00:12 -0400443 if (WARN_ON(buf_size < chunk_size))
444 return -EINVAL;
445 if (WARN_ON(buf_size == 0))
446 return -EINVAL;
447 if (WARN_ON(chunk_size == 0))
448 return -EINVAL;
Oded Gabbay6e810902014-10-27 14:36:07 +0200449
450 kfd->gtt_sa_chunk_size = chunk_size;
451 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
452
Felix Kuehling8625ff92017-08-15 23:00:11 -0400453 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
454 BITS_PER_LONG;
Oded Gabbay6e810902014-10-27 14:36:07 +0200455
Felix Kuehling8625ff92017-08-15 23:00:11 -0400456 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
Oded Gabbay6e810902014-10-27 14:36:07 +0200457
458 if (!kfd->gtt_sa_bitmap)
459 return -ENOMEM;
460
Kent Russell79775b62017-08-15 23:00:05 -0400461 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200462 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
463
464 mutex_init(&kfd->gtt_sa_lock);
465
466 return 0;
467
468}
469
470static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
471{
472 mutex_destroy(&kfd->gtt_sa_lock);
473 kfree(kfd->gtt_sa_bitmap);
474}
475
476static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
477 unsigned int bit_num,
478 unsigned int chunk_size)
479{
480 return start_addr + bit_num * chunk_size;
481}
482
483static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
484 unsigned int bit_num,
485 unsigned int chunk_size)
486{
487 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
488}
489
490int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
491 struct kfd_mem_obj **mem_obj)
492{
493 unsigned int found, start_search, cur_size;
494
Oded Gabbay6e810902014-10-27 14:36:07 +0200495 if (size == 0)
496 return -EINVAL;
497
498 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
499 return -ENOMEM;
500
501 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
502 if ((*mem_obj) == NULL)
503 return -ENOMEM;
504
Kent Russell79775b62017-08-15 23:00:05 -0400505 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
Oded Gabbay6e810902014-10-27 14:36:07 +0200506
507 start_search = 0;
508
509 mutex_lock(&kfd->gtt_sa_lock);
510
511kfd_gtt_restart_search:
512 /* Find the first chunk that is free */
513 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
514 kfd->gtt_sa_num_of_chunks,
515 start_search);
516
Kent Russell79775b62017-08-15 23:00:05 -0400517 pr_debug("Found = %d\n", found);
Oded Gabbay6e810902014-10-27 14:36:07 +0200518
519 /* If there wasn't any free chunk, bail out */
520 if (found == kfd->gtt_sa_num_of_chunks)
521 goto kfd_gtt_no_free_chunk;
522
523 /* Update fields of mem_obj */
524 (*mem_obj)->range_start = found;
525 (*mem_obj)->range_end = found;
526 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
527 kfd->gtt_start_gpu_addr,
528 found,
529 kfd->gtt_sa_chunk_size);
530 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
531 kfd->gtt_start_cpu_ptr,
532 found,
533 kfd->gtt_sa_chunk_size);
534
Kent Russell79775b62017-08-15 23:00:05 -0400535 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200536 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
537
538 /* If we need only one chunk, mark it as allocated and get out */
539 if (size <= kfd->gtt_sa_chunk_size) {
Kent Russell79775b62017-08-15 23:00:05 -0400540 pr_debug("Single bit\n");
Oded Gabbay6e810902014-10-27 14:36:07 +0200541 set_bit(found, kfd->gtt_sa_bitmap);
542 goto kfd_gtt_out;
543 }
544
545 /* Otherwise, try to see if we have enough contiguous chunks */
546 cur_size = size - kfd->gtt_sa_chunk_size;
547 do {
548 (*mem_obj)->range_end =
549 find_next_zero_bit(kfd->gtt_sa_bitmap,
550 kfd->gtt_sa_num_of_chunks, ++found);
551 /*
552 * If next free chunk is not contiguous than we need to
553 * restart our search from the last free chunk we found (which
554 * wasn't contiguous to the previous ones
555 */
556 if ((*mem_obj)->range_end != found) {
557 start_search = found;
558 goto kfd_gtt_restart_search;
559 }
560
561 /*
562 * If we reached end of buffer, bail out with error
563 */
564 if (found == kfd->gtt_sa_num_of_chunks)
565 goto kfd_gtt_no_free_chunk;
566
567 /* Check if we don't need another chunk */
568 if (cur_size <= kfd->gtt_sa_chunk_size)
569 cur_size = 0;
570 else
571 cur_size -= kfd->gtt_sa_chunk_size;
572
573 } while (cur_size > 0);
574
Kent Russell79775b62017-08-15 23:00:05 -0400575 pr_debug("range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200576 (*mem_obj)->range_start, (*mem_obj)->range_end);
577
578 /* Mark the chunks as allocated */
579 for (found = (*mem_obj)->range_start;
580 found <= (*mem_obj)->range_end;
581 found++)
582 set_bit(found, kfd->gtt_sa_bitmap);
583
584kfd_gtt_out:
585 mutex_unlock(&kfd->gtt_sa_lock);
586 return 0;
587
588kfd_gtt_no_free_chunk:
Kent Russell79775b62017-08-15 23:00:05 -0400589 pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
Oded Gabbay6e810902014-10-27 14:36:07 +0200590 mutex_unlock(&kfd->gtt_sa_lock);
591 kfree(mem_obj);
592 return -ENOMEM;
593}
594
595int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
596{
597 unsigned int bit;
598
Oded Gabbay9216ed22015-01-12 22:34:21 +0200599 /* Act like kfree when trying to free a NULL object */
600 if (!mem_obj)
601 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200602
Kent Russell79775b62017-08-15 23:00:05 -0400603 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200604 mem_obj, mem_obj->range_start, mem_obj->range_end);
605
606 mutex_lock(&kfd->gtt_sa_lock);
607
608 /* Mark the chunks as free */
609 for (bit = mem_obj->range_start;
610 bit <= mem_obj->range_end;
611 bit++)
612 clear_bit(bit, kfd->gtt_sa_bitmap);
613
614 mutex_unlock(&kfd->gtt_sa_lock);
615
616 kfree(mem_obj);
617 return 0;
618}