blob: 80c90f3619b77445c2db6e1dba8f9b992fbd612e [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"
Oded Gabbay4a488a72014-07-16 21:08:55 +030030
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030031#define MQD_SIZE_ALIGNED 768
32
Oded Gabbay4a488a72014-07-16 21:08:55 +030033static const struct kfd_device_info kaveri_device_info = {
Ben Goz0da75582015-01-01 17:10:01 +020034 .asic_family = CHIP_KAVERI,
35 .max_pasid_bits = 16,
Yair Shachar992839a2015-05-20 13:43:04 +030036 /* max num of queues for KV.TODO should be a dynamic value */
37 .max_no_of_hqd = 24,
Ben Goz0da75582015-01-01 17:10:01 +020038 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030039 .event_interrupt_class = &event_interrupt_class_cik,
Yair Shacharfbeb6612015-05-20 13:48:26 +030040 .num_of_watch_points = 4,
Ben Goz0da75582015-01-01 17:10:01 +020041 .mqd_size_aligned = MQD_SIZE_ALIGNED
42};
43
44static const struct kfd_device_info carrizo_device_info = {
45 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030046 .max_pasid_bits = 16,
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030047 /* max num of queues for CZ.TODO should be a dynamic value */
48 .max_no_of_hqd = 24,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030049 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Oded Gabbayeaccd6e2015-06-06 21:45:43 +030050 .event_interrupt_class = &event_interrupt_class_cik,
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030051 .num_of_watch_points = 4,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030052 .mqd_size_aligned = MQD_SIZE_ALIGNED
Oded Gabbay4a488a72014-07-16 21:08:55 +030053};
54
55struct kfd_deviceid {
56 unsigned short did;
57 const struct kfd_device_info *device_info;
58};
59
60/* Please keep this sorted by increasing device id. */
61static const struct kfd_deviceid supported_devices[] = {
62 { 0x1304, &kaveri_device_info }, /* Kaveri */
63 { 0x1305, &kaveri_device_info }, /* Kaveri */
64 { 0x1306, &kaveri_device_info }, /* Kaveri */
65 { 0x1307, &kaveri_device_info }, /* Kaveri */
66 { 0x1309, &kaveri_device_info }, /* Kaveri */
67 { 0x130A, &kaveri_device_info }, /* Kaveri */
68 { 0x130B, &kaveri_device_info }, /* Kaveri */
69 { 0x130C, &kaveri_device_info }, /* Kaveri */
70 { 0x130D, &kaveri_device_info }, /* Kaveri */
71 { 0x130E, &kaveri_device_info }, /* Kaveri */
72 { 0x130F, &kaveri_device_info }, /* Kaveri */
73 { 0x1310, &kaveri_device_info }, /* Kaveri */
74 { 0x1311, &kaveri_device_info }, /* Kaveri */
75 { 0x1312, &kaveri_device_info }, /* Kaveri */
76 { 0x1313, &kaveri_device_info }, /* Kaveri */
77 { 0x1315, &kaveri_device_info }, /* Kaveri */
78 { 0x1316, &kaveri_device_info }, /* Kaveri */
79 { 0x1317, &kaveri_device_info }, /* Kaveri */
80 { 0x1318, &kaveri_device_info }, /* Kaveri */
81 { 0x131B, &kaveri_device_info }, /* Kaveri */
82 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz123576d2015-01-12 14:37:24 +020083 { 0x131D, &kaveri_device_info }, /* Kaveri */
84 { 0x9870, &carrizo_device_info }, /* Carrizo */
85 { 0x9874, &carrizo_device_info }, /* Carrizo */
86 { 0x9875, &carrizo_device_info }, /* Carrizo */
87 { 0x9876, &carrizo_device_info }, /* Carrizo */
88 { 0x9877, &carrizo_device_info } /* Carrizo */
Oded Gabbay4a488a72014-07-16 21:08:55 +030089};
90
Oded Gabbay6e810902014-10-27 14:36:07 +020091static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
92 unsigned int chunk_size);
93static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
94
Yong Zhaob8935a72017-09-20 18:10:13 -040095static int kfd_resume(struct kfd_dev *kfd);
96
Oded Gabbay4a488a72014-07-16 21:08:55 +030097static const struct kfd_device_info *lookup_device_info(unsigned short did)
98{
99 size_t i;
100
101 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
102 if (supported_devices[i].did == did) {
Felix Kuehling32fa8212017-08-15 23:00:12 -0400103 WARN_ON(!supported_devices[i].device_info);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300104 return supported_devices[i].device_info;
105 }
106 }
107
Yong Zhao4ebc7182017-08-15 23:00:13 -0400108 dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
109 did);
110
Oded Gabbay4a488a72014-07-16 21:08:55 +0300111 return NULL;
112}
113
Xihan Zhangcea405b2015-03-17 19:32:53 +0800114struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
115 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300116{
117 struct kfd_dev *kfd;
118
119 const struct kfd_device_info *device_info =
120 lookup_device_info(pdev->device);
121
Yong Zhao4ebc7182017-08-15 23:00:13 -0400122 if (!device_info) {
123 dev_err(kfd_device, "kgd2kfd_probe failed\n");
Oded Gabbay4a488a72014-07-16 21:08:55 +0300124 return NULL;
Yong Zhao4ebc7182017-08-15 23:00:13 -0400125 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300126
127 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
128 if (!kfd)
129 return NULL;
130
131 kfd->kgd = kgd;
132 kfd->device_info = device_info;
133 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300134 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800135 kfd->kfd2kgd = f2g;
136
137 mutex_init(&kfd->doorbell_mutex);
138 memset(&kfd->doorbell_available_index, 0,
139 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300140
141 return kfd;
142}
143
Oded Gabbayb17f0682014-07-17 00:06:27 +0300144static bool device_iommu_pasid_init(struct kfd_dev *kfd)
145{
146 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
147 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
148 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
149
150 struct amd_iommu_device_info iommu_info;
151 unsigned int pasid_limit;
152 int err;
153
154 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
155 if (err < 0) {
156 dev_err(kfd_device,
157 "error getting iommu info. is the iommu enabled?\n");
158 return false;
159 }
160
161 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
Kent Russell79775b62017-08-15 23:00:05 -0400162 dev_err(kfd_device, "error required iommu flags ats %i, pri %i, pasid %i\n",
Oded Gabbayb17f0682014-07-17 00:06:27 +0300163 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
164 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
Kent Russell8eabaf52017-08-15 23:00:04 -0400165 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP)
166 != 0);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300167 return false;
168 }
169
170 pasid_limit = min_t(unsigned int,
Kent Russell8eabaf52017-08-15 23:00:04 -0400171 (unsigned int)(1 << kfd->device_info->max_pasid_bits),
Oded Gabbayb17f0682014-07-17 00:06:27 +0300172 iommu_info.max_pasids);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300173
Oded Gabbayb17f0682014-07-17 00:06:27 +0300174 if (!kfd_set_pasid_limit(pasid_limit)) {
175 dev_err(kfd_device, "error setting pasid limit\n");
Oded Gabbayb17f0682014-07-17 00:06:27 +0300176 return false;
177 }
178
179 return true;
180}
181
182static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
183{
184 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
185
186 if (dev)
Yong Zhao733fa1f2017-09-20 18:10:14 -0400187 kfd_process_iommu_unbind_callback(dev, pasid);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300188}
189
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300190/*
191 * This function called by IOMMU driver on PPR failure
192 */
193static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
194 unsigned long address, u16 flags)
195{
196 struct kfd_dev *dev;
197
198 dev_warn(kfd_device,
199 "Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
200 PCI_BUS_NUM(pdev->devfn),
201 PCI_SLOT(pdev->devfn),
202 PCI_FUNC(pdev->devfn),
203 pasid,
204 address,
205 flags);
206
207 dev = kfd_device_by_pci_dev(pdev);
Felix Kuehling32fa8212017-08-15 23:00:12 -0400208 if (!WARN_ON(!dev))
209 kfd_signal_iommu_event(dev, pasid, address,
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300210 flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
211
212 return AMD_IOMMU_INV_PRI_RSP_INVALID;
213}
214
Oded Gabbay4a488a72014-07-16 21:08:55 +0300215bool kgd2kfd_device_init(struct kfd_dev *kfd,
216 const struct kgd2kfd_shared_resources *gpu_resources)
217{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300218 unsigned int size;
219
Oded Gabbay4a488a72014-07-16 21:08:55 +0300220 kfd->shared_resources = *gpu_resources;
221
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300222 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200223 size = max_num_of_queues_per_device *
224 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300225
Oded Gabbaye18e7942014-10-26 10:12:22 +0200226 /*
227 * calculate max size of runlist packet.
228 * There can be only 2 packets at once
229 */
Felix Kuehling507968d2017-08-15 23:00:15 -0400230 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
231 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
232 + sizeof(struct pm4_mes_runlist)) * 2;
Oded Gabbaye18e7942014-10-26 10:12:22 +0200233
234 /* Add size of HIQ & DIQ */
235 size += KFD_KERNEL_QUEUE_SIZE * 2;
236
237 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300238 size += 512 * 1024;
239
Xihan Zhangcea405b2015-03-17 19:32:53 +0800240 if (kfd->kfd2kgd->init_gtt_mem_allocation(
241 kfd->kgd, size, &kfd->gtt_mem,
242 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
Kent Russell79775b62017-08-15 23:00:05 -0400243 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300244 goto out;
245 }
246
Kent Russell79775b62017-08-15 23:00:05 -0400247 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
Oded Gabbaye18e7942014-10-26 10:12:22 +0200248
Oded Gabbay73a1da02014-10-26 09:53:37 +0200249 /* Initialize GTT sa with 512 byte chunk size */
250 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
Kent Russell79775b62017-08-15 23:00:05 -0400251 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
Oded Gabbay73a1da02014-10-26 09:53:37 +0200252 goto kfd_gtt_sa_init_error;
253 }
254
Felix Kuehling735df2b2017-08-15 23:00:10 -0400255 if (kfd_doorbell_init(kfd)) {
256 dev_err(kfd_device,
257 "Error initializing doorbell aperture\n");
258 goto kfd_doorbell_error;
259 }
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300260
Kent Russell4eacc26b2017-08-15 23:00:06 -0400261 if (kfd_topology_add_device(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400262 dev_err(kfd_device, "Error adding device to topology\n");
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300263 goto kfd_topology_add_device_error;
264 }
265
Andrew Lewycky2249d552014-07-17 01:37:30 +0300266 if (kfd_interrupt_init(kfd)) {
Kent Russell79775b62017-08-15 23:00:05 -0400267 dev_err(kfd_device, "Error initializing interrupts\n");
Andrew Lewycky2249d552014-07-17 01:37:30 +0300268 goto kfd_interrupt_error;
269 }
270
Ben Goz64c7f8c2014-07-17 01:27:00 +0300271 kfd->dqm = device_queue_manager_init(kfd);
272 if (!kfd->dqm) {
Kent Russell79775b62017-08-15 23:00:05 -0400273 dev_err(kfd_device, "Error initializing queue manager\n");
Ben Goz64c7f8c2014-07-17 01:27:00 +0300274 goto device_queue_manager_error;
275 }
276
Yong Zhaob8935a72017-09-20 18:10:13 -0400277 if (!device_iommu_pasid_init(kfd)) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300278 dev_err(kfd_device,
Yong Zhaob8935a72017-09-20 18:10:13 -0400279 "Error initializing iommuv2 for device %x:%x\n",
Ben Goz64c7f8c2014-07-17 01:27:00 +0300280 kfd->pdev->vendor, kfd->pdev->device);
Yong Zhaob8935a72017-09-20 18:10:13 -0400281 goto device_iommu_pasid_error;
Ben Goz64c7f8c2014-07-17 01:27:00 +0300282 }
283
Yong Zhaob8935a72017-09-20 18:10:13 -0400284 if (kfd_resume(kfd))
285 goto kfd_resume_error;
286
Yair Shacharfbeb6612015-05-20 13:48:26 +0300287 kfd->dbgmgr = NULL;
288
Oded Gabbay4a488a72014-07-16 21:08:55 +0300289 kfd->init_complete = true;
Kent Russell79775b62017-08-15 23:00:05 -0400290 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
Oded Gabbay4a488a72014-07-16 21:08:55 +0300291 kfd->pdev->device);
292
Kent Russell79775b62017-08-15 23:00:05 -0400293 pr_debug("Starting kfd with the following scheduling policy %d\n",
Ben Goz64c7f8c2014-07-17 01:27:00 +0300294 sched_policy);
295
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300296 goto out;
297
Yong Zhaob8935a72017-09-20 18:10:13 -0400298kfd_resume_error:
299device_iommu_pasid_error:
Ben Goz64c7f8c2014-07-17 01:27:00 +0300300 device_queue_manager_uninit(kfd->dqm);
301device_queue_manager_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300302 kfd_interrupt_exit(kfd);
303kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300304 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300305kfd_topology_add_device_error:
Felix Kuehling735df2b2017-08-15 23:00:10 -0400306 kfd_doorbell_fini(kfd);
307kfd_doorbell_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200308 kfd_gtt_sa_fini(kfd);
309kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800310 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300311 dev_err(kfd_device,
Kent Russell79775b62017-08-15 23:00:05 -0400312 "device %x:%x NOT added due to errors\n",
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300313 kfd->pdev->vendor, kfd->pdev->device);
314out:
315 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300316}
317
318void kgd2kfd_device_exit(struct kfd_dev *kfd)
319{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300320 if (kfd->init_complete) {
Yong Zhaob8935a72017-09-20 18:10:13 -0400321 kgd2kfd_suspend(kfd);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300322 device_queue_manager_uninit(kfd->dqm);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300323 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300324 kfd_topology_remove_device(kfd);
Felix Kuehling735df2b2017-08-15 23:00:10 -0400325 kfd_doorbell_fini(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200326 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800327 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300328 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300329
Oded Gabbay4a488a72014-07-16 21:08:55 +0300330 kfree(kfd);
331}
332
333void kgd2kfd_suspend(struct kfd_dev *kfd)
334{
Yong Zhao733fa1f2017-09-20 18:10:14 -0400335 if (!kfd->init_complete)
336 return;
337
338 kfd->dqm->ops.stop(kfd->dqm);
339
340 kfd_unbind_processes_from_device(kfd);
341
342 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
343 amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
344 amd_iommu_free_device(kfd->pdev);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300345}
346
347int kgd2kfd_resume(struct kfd_dev *kfd)
348{
Yong Zhaob8935a72017-09-20 18:10:13 -0400349 if (!kfd->init_complete)
350 return 0;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300351
Yong Zhaob8935a72017-09-20 18:10:13 -0400352 return kfd_resume(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300353
Yong Zhaob8935a72017-09-20 18:10:13 -0400354}
Yong Zhao4ebc7182017-08-15 23:00:13 -0400355
Yong Zhaob8935a72017-09-20 18:10:13 -0400356static int kfd_resume(struct kfd_dev *kfd)
357{
358 int err = 0;
359 unsigned int pasid_limit = kfd_get_pasid_limit();
360
361 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
362 if (err)
363 return -ENXIO;
364 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
365 iommu_pasid_shutdown_callback);
366 amd_iommu_set_invalid_ppr_cb(kfd->pdev,
367 iommu_invalid_ppr_cb);
368
Yong Zhao733fa1f2017-09-20 18:10:14 -0400369 err = kfd_bind_processes_to_device(kfd);
370 if (err)
371 goto processes_bind_error;
372
Yong Zhaob8935a72017-09-20 18:10:13 -0400373 err = kfd->dqm->ops.start(kfd->dqm);
374 if (err) {
375 dev_err(kfd_device,
376 "Error starting queue manager for device %x:%x\n",
377 kfd->pdev->vendor, kfd->pdev->device);
378 goto dqm_start_error;
Oded Gabbayb17f0682014-07-17 00:06:27 +0300379 }
380
Yong Zhaob8935a72017-09-20 18:10:13 -0400381 return err;
382
383dqm_start_error:
Yong Zhao733fa1f2017-09-20 18:10:14 -0400384processes_bind_error:
Yong Zhaob8935a72017-09-20 18:10:13 -0400385 amd_iommu_free_device(kfd->pdev);
386
387 return err;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300388}
389
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300390/* This is called directly from KGD at ISR. */
391void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300392{
Andrew Lewycky2249d552014-07-17 01:37:30 +0300393 if (!kfd->init_complete)
394 return;
395
396 spin_lock(&kfd->interrupt_lock);
397
398 if (kfd->interrupts_active
399 && interrupt_is_wanted(kfd, ih_ring_entry)
400 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
401 schedule_work(&kfd->interrupt_work);
402
403 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300404}
Oded Gabbay6e810902014-10-27 14:36:07 +0200405
406static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
407 unsigned int chunk_size)
408{
Felix Kuehling8625ff92017-08-15 23:00:11 -0400409 unsigned int num_of_longs;
Oded Gabbay6e810902014-10-27 14:36:07 +0200410
Felix Kuehling32fa8212017-08-15 23:00:12 -0400411 if (WARN_ON(buf_size < chunk_size))
412 return -EINVAL;
413 if (WARN_ON(buf_size == 0))
414 return -EINVAL;
415 if (WARN_ON(chunk_size == 0))
416 return -EINVAL;
Oded Gabbay6e810902014-10-27 14:36:07 +0200417
418 kfd->gtt_sa_chunk_size = chunk_size;
419 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
420
Felix Kuehling8625ff92017-08-15 23:00:11 -0400421 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
422 BITS_PER_LONG;
Oded Gabbay6e810902014-10-27 14:36:07 +0200423
Felix Kuehling8625ff92017-08-15 23:00:11 -0400424 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
Oded Gabbay6e810902014-10-27 14:36:07 +0200425
426 if (!kfd->gtt_sa_bitmap)
427 return -ENOMEM;
428
Kent Russell79775b62017-08-15 23:00:05 -0400429 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200430 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
431
432 mutex_init(&kfd->gtt_sa_lock);
433
434 return 0;
435
436}
437
438static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
439{
440 mutex_destroy(&kfd->gtt_sa_lock);
441 kfree(kfd->gtt_sa_bitmap);
442}
443
444static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
445 unsigned int bit_num,
446 unsigned int chunk_size)
447{
448 return start_addr + bit_num * chunk_size;
449}
450
451static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
452 unsigned int bit_num,
453 unsigned int chunk_size)
454{
455 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
456}
457
458int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
459 struct kfd_mem_obj **mem_obj)
460{
461 unsigned int found, start_search, cur_size;
462
Oded Gabbay6e810902014-10-27 14:36:07 +0200463 if (size == 0)
464 return -EINVAL;
465
466 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
467 return -ENOMEM;
468
469 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
470 if ((*mem_obj) == NULL)
471 return -ENOMEM;
472
Kent Russell79775b62017-08-15 23:00:05 -0400473 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
Oded Gabbay6e810902014-10-27 14:36:07 +0200474
475 start_search = 0;
476
477 mutex_lock(&kfd->gtt_sa_lock);
478
479kfd_gtt_restart_search:
480 /* Find the first chunk that is free */
481 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
482 kfd->gtt_sa_num_of_chunks,
483 start_search);
484
Kent Russell79775b62017-08-15 23:00:05 -0400485 pr_debug("Found = %d\n", found);
Oded Gabbay6e810902014-10-27 14:36:07 +0200486
487 /* If there wasn't any free chunk, bail out */
488 if (found == kfd->gtt_sa_num_of_chunks)
489 goto kfd_gtt_no_free_chunk;
490
491 /* Update fields of mem_obj */
492 (*mem_obj)->range_start = found;
493 (*mem_obj)->range_end = found;
494 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
495 kfd->gtt_start_gpu_addr,
496 found,
497 kfd->gtt_sa_chunk_size);
498 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
499 kfd->gtt_start_cpu_ptr,
500 found,
501 kfd->gtt_sa_chunk_size);
502
Kent Russell79775b62017-08-15 23:00:05 -0400503 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200504 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
505
506 /* If we need only one chunk, mark it as allocated and get out */
507 if (size <= kfd->gtt_sa_chunk_size) {
Kent Russell79775b62017-08-15 23:00:05 -0400508 pr_debug("Single bit\n");
Oded Gabbay6e810902014-10-27 14:36:07 +0200509 set_bit(found, kfd->gtt_sa_bitmap);
510 goto kfd_gtt_out;
511 }
512
513 /* Otherwise, try to see if we have enough contiguous chunks */
514 cur_size = size - kfd->gtt_sa_chunk_size;
515 do {
516 (*mem_obj)->range_end =
517 find_next_zero_bit(kfd->gtt_sa_bitmap,
518 kfd->gtt_sa_num_of_chunks, ++found);
519 /*
520 * If next free chunk is not contiguous than we need to
521 * restart our search from the last free chunk we found (which
522 * wasn't contiguous to the previous ones
523 */
524 if ((*mem_obj)->range_end != found) {
525 start_search = found;
526 goto kfd_gtt_restart_search;
527 }
528
529 /*
530 * If we reached end of buffer, bail out with error
531 */
532 if (found == kfd->gtt_sa_num_of_chunks)
533 goto kfd_gtt_no_free_chunk;
534
535 /* Check if we don't need another chunk */
536 if (cur_size <= kfd->gtt_sa_chunk_size)
537 cur_size = 0;
538 else
539 cur_size -= kfd->gtt_sa_chunk_size;
540
541 } while (cur_size > 0);
542
Kent Russell79775b62017-08-15 23:00:05 -0400543 pr_debug("range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200544 (*mem_obj)->range_start, (*mem_obj)->range_end);
545
546 /* Mark the chunks as allocated */
547 for (found = (*mem_obj)->range_start;
548 found <= (*mem_obj)->range_end;
549 found++)
550 set_bit(found, kfd->gtt_sa_bitmap);
551
552kfd_gtt_out:
553 mutex_unlock(&kfd->gtt_sa_lock);
554 return 0;
555
556kfd_gtt_no_free_chunk:
Kent Russell79775b62017-08-15 23:00:05 -0400557 pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
Oded Gabbay6e810902014-10-27 14:36:07 +0200558 mutex_unlock(&kfd->gtt_sa_lock);
559 kfree(mem_obj);
560 return -ENOMEM;
561}
562
563int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
564{
565 unsigned int bit;
566
Oded Gabbay9216ed22015-01-12 22:34:21 +0200567 /* Act like kfree when trying to free a NULL object */
568 if (!mem_obj)
569 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200570
Kent Russell79775b62017-08-15 23:00:05 -0400571 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
Oded Gabbay6e810902014-10-27 14:36:07 +0200572 mem_obj, mem_obj->range_start, mem_obj->range_end);
573
574 mutex_lock(&kfd->gtt_sa_lock);
575
576 /* Mark the chunks as free */
577 for (bit = mem_obj->range_start;
578 bit <= mem_obj->range_end;
579 bit++)
580 clear_bit(bit, kfd->gtt_sa_bitmap);
581
582 mutex_unlock(&kfd->gtt_sa_lock);
583
584 kfree(mem_obj);
585 return 0;
586}