blob: 1f5032534944c6f228270cbac3fad0c1ae598bac [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"
Oded Gabbaye18e7942014-10-26 10:12:22 +020029#include "kfd_pm4_headers.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
Oded Gabbay4a488a72014-07-16 21:08:55 +030095static const struct kfd_device_info *lookup_device_info(unsigned short did)
96{
97 size_t i;
98
99 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
100 if (supported_devices[i].did == did) {
101 BUG_ON(supported_devices[i].device_info == NULL);
102 return supported_devices[i].device_info;
103 }
104 }
105
106 return NULL;
107}
108
Xihan Zhangcea405b2015-03-17 19:32:53 +0800109struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
110 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300111{
112 struct kfd_dev *kfd;
113
114 const struct kfd_device_info *device_info =
115 lookup_device_info(pdev->device);
116
117 if (!device_info)
118 return NULL;
119
120 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
121 if (!kfd)
122 return NULL;
123
124 kfd->kgd = kgd;
125 kfd->device_info = device_info;
126 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300127 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800128 kfd->kfd2kgd = f2g;
129
130 mutex_init(&kfd->doorbell_mutex);
131 memset(&kfd->doorbell_available_index, 0,
132 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300133
134 return kfd;
135}
136
Oded Gabbayb17f0682014-07-17 00:06:27 +0300137static bool device_iommu_pasid_init(struct kfd_dev *kfd)
138{
139 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
140 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
141 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
142
143 struct amd_iommu_device_info iommu_info;
144 unsigned int pasid_limit;
145 int err;
146
147 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
148 if (err < 0) {
149 dev_err(kfd_device,
150 "error getting iommu info. is the iommu enabled?\n");
151 return false;
152 }
153
154 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
155 dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n",
156 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
157 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
Kent Russell8eabaf52017-08-15 23:00:04 -0400158 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP)
159 != 0);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300160 return false;
161 }
162
163 pasid_limit = min_t(unsigned int,
Kent Russell8eabaf52017-08-15 23:00:04 -0400164 (unsigned int)(1 << kfd->device_info->max_pasid_bits),
Oded Gabbayb17f0682014-07-17 00:06:27 +0300165 iommu_info.max_pasids);
166 /*
167 * last pasid is used for kernel queues doorbells
168 * in the future the last pasid might be used for a kernel thread.
169 */
170 pasid_limit = min_t(unsigned int,
171 pasid_limit,
172 kfd->doorbell_process_limit - 1);
173
174 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
175 if (err < 0) {
176 dev_err(kfd_device, "error initializing iommu device\n");
177 return false;
178 }
179
180 if (!kfd_set_pasid_limit(pasid_limit)) {
181 dev_err(kfd_device, "error setting pasid limit\n");
182 amd_iommu_free_device(kfd->pdev);
183 return false;
184 }
185
186 return true;
187}
188
189static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
190{
191 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
192
193 if (dev)
194 kfd_unbind_process_from_device(dev, pasid);
195}
196
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300197/*
198 * This function called by IOMMU driver on PPR failure
199 */
200static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
201 unsigned long address, u16 flags)
202{
203 struct kfd_dev *dev;
204
205 dev_warn(kfd_device,
206 "Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
207 PCI_BUS_NUM(pdev->devfn),
208 PCI_SLOT(pdev->devfn),
209 PCI_FUNC(pdev->devfn),
210 pasid,
211 address,
212 flags);
213
214 dev = kfd_device_by_pci_dev(pdev);
215 BUG_ON(dev == NULL);
216
217 kfd_signal_iommu_event(dev, pasid, address,
218 flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
219
220 return AMD_IOMMU_INV_PRI_RSP_INVALID;
221}
222
Oded Gabbay4a488a72014-07-16 21:08:55 +0300223bool kgd2kfd_device_init(struct kfd_dev *kfd,
224 const struct kgd2kfd_shared_resources *gpu_resources)
225{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300226 unsigned int size;
227
Oded Gabbay4a488a72014-07-16 21:08:55 +0300228 kfd->shared_resources = *gpu_resources;
229
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300230 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200231 size = max_num_of_queues_per_device *
232 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300233
Oded Gabbaye18e7942014-10-26 10:12:22 +0200234 /*
235 * calculate max size of runlist packet.
236 * There can be only 2 packets at once
237 */
Dave Airlieb3869b12015-01-29 11:45:31 +1000238 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_map_process) +
239 max_num_of_queues_per_device *
Oded Gabbaye18e7942014-10-26 10:12:22 +0200240 sizeof(struct pm4_map_queues) + sizeof(struct pm4_runlist)) * 2;
241
242 /* Add size of HIQ & DIQ */
243 size += KFD_KERNEL_QUEUE_SIZE * 2;
244
245 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300246 size += 512 * 1024;
247
Xihan Zhangcea405b2015-03-17 19:32:53 +0800248 if (kfd->kfd2kgd->init_gtt_mem_allocation(
249 kfd->kgd, size, &kfd->gtt_mem,
250 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300251 dev_err(kfd_device,
Oded Gabbaye18e7942014-10-26 10:12:22 +0200252 "Could not allocate %d bytes for device (%x:%x)\n",
253 size, kfd->pdev->vendor, kfd->pdev->device);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300254 goto out;
255 }
256
Oded Gabbaye18e7942014-10-26 10:12:22 +0200257 dev_info(kfd_device,
258 "Allocated %d bytes on gart for device(%x:%x)\n",
259 size, kfd->pdev->vendor, kfd->pdev->device);
260
Oded Gabbay73a1da02014-10-26 09:53:37 +0200261 /* Initialize GTT sa with 512 byte chunk size */
262 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
263 dev_err(kfd_device,
264 "Error initializing gtt sub-allocator\n");
265 goto kfd_gtt_sa_init_error;
266 }
267
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300268 kfd_doorbell_init(kfd);
269
270 if (kfd_topology_add_device(kfd) != 0) {
271 dev_err(kfd_device,
272 "Error adding device (%x:%x) to topology\n",
273 kfd->pdev->vendor, kfd->pdev->device);
274 goto kfd_topology_add_device_error;
275 }
276
Andrew Lewycky2249d552014-07-17 01:37:30 +0300277 if (kfd_interrupt_init(kfd)) {
278 dev_err(kfd_device,
279 "Error initializing interrupts for device (%x:%x)\n",
280 kfd->pdev->vendor, kfd->pdev->device);
281 goto kfd_interrupt_error;
282 }
283
Oded Gabbayb17f0682014-07-17 00:06:27 +0300284 if (!device_iommu_pasid_init(kfd)) {
285 dev_err(kfd_device,
286 "Error initializing iommuv2 for device (%x:%x)\n",
287 kfd->pdev->vendor, kfd->pdev->device);
288 goto device_iommu_pasid_error;
289 }
290 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
291 iommu_pasid_shutdown_callback);
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300292 amd_iommu_set_invalid_ppr_cb(kfd->pdev, iommu_invalid_ppr_cb);
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300293
Ben Goz64c7f8c2014-07-17 01:27:00 +0300294 kfd->dqm = device_queue_manager_init(kfd);
295 if (!kfd->dqm) {
296 dev_err(kfd_device,
297 "Error initializing queue manager for device (%x:%x)\n",
298 kfd->pdev->vendor, kfd->pdev->device);
299 goto device_queue_manager_error;
300 }
301
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200302 if (kfd->dqm->ops.start(kfd->dqm) != 0) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300303 dev_err(kfd_device,
304 "Error starting queuen manager for device (%x:%x)\n",
305 kfd->pdev->vendor, kfd->pdev->device);
306 goto dqm_start_error;
307 }
308
Yair Shacharfbeb6612015-05-20 13:48:26 +0300309 kfd->dbgmgr = NULL;
310
Oded Gabbay4a488a72014-07-16 21:08:55 +0300311 kfd->init_complete = true;
312 dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor,
313 kfd->pdev->device);
314
Ben Goz64c7f8c2014-07-17 01:27:00 +0300315 pr_debug("kfd: Starting kfd with the following scheduling policy %d\n",
316 sched_policy);
317
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300318 goto out;
319
Ben Goz64c7f8c2014-07-17 01:27:00 +0300320dqm_start_error:
321 device_queue_manager_uninit(kfd->dqm);
322device_queue_manager_error:
323 amd_iommu_free_device(kfd->pdev);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300324device_iommu_pasid_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300325 kfd_interrupt_exit(kfd);
326kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300327 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300328kfd_topology_add_device_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200329 kfd_gtt_sa_fini(kfd);
330kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800331 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300332 dev_err(kfd_device,
333 "device (%x:%x) NOT added due to errors\n",
334 kfd->pdev->vendor, kfd->pdev->device);
335out:
336 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300337}
338
339void kgd2kfd_device_exit(struct kfd_dev *kfd)
340{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300341 if (kfd->init_complete) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300342 device_queue_manager_uninit(kfd->dqm);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300343 amd_iommu_free_device(kfd->pdev);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300344 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300345 kfd_topology_remove_device(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200346 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800347 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300348 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300349
Oded Gabbay4a488a72014-07-16 21:08:55 +0300350 kfree(kfd);
351}
352
353void kgd2kfd_suspend(struct kfd_dev *kfd)
354{
355 BUG_ON(kfd == NULL);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300356
Ben Goz64c7f8c2014-07-17 01:27:00 +0300357 if (kfd->init_complete) {
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200358 kfd->dqm->ops.stop(kfd->dqm);
Oded Gabbayabc9d3e2014-11-09 22:36:22 +0200359 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300360 amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300361 amd_iommu_free_device(kfd->pdev);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300362 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300363}
364
365int kgd2kfd_resume(struct kfd_dev *kfd)
366{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300367 unsigned int pasid_limit;
368 int err;
369
Oded Gabbay4a488a72014-07-16 21:08:55 +0300370 BUG_ON(kfd == NULL);
371
Oded Gabbayb17f0682014-07-17 00:06:27 +0300372 pasid_limit = kfd_get_pasid_limit();
373
374 if (kfd->init_complete) {
375 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
376 if (err < 0)
377 return -ENXIO;
378 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
379 iommu_pasid_shutdown_callback);
Alexey Skidanov59d3e8b2015-04-14 18:05:49 +0300380 amd_iommu_set_invalid_ppr_cb(kfd->pdev, iommu_invalid_ppr_cb);
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200381 kfd->dqm->ops.start(kfd->dqm);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300382 }
383
Oded Gabbay4a488a72014-07-16 21:08:55 +0300384 return 0;
385}
386
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300387/* This is called directly from KGD at ISR. */
388void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300389{
Andrew Lewycky2249d552014-07-17 01:37:30 +0300390 if (!kfd->init_complete)
391 return;
392
393 spin_lock(&kfd->interrupt_lock);
394
395 if (kfd->interrupts_active
396 && interrupt_is_wanted(kfd, ih_ring_entry)
397 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
398 schedule_work(&kfd->interrupt_work);
399
400 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300401}
Oded Gabbay6e810902014-10-27 14:36:07 +0200402
403static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
404 unsigned int chunk_size)
405{
406 unsigned int num_of_bits;
407
408 BUG_ON(!kfd);
409 BUG_ON(!kfd->gtt_mem);
410 BUG_ON(buf_size < chunk_size);
411 BUG_ON(buf_size == 0);
412 BUG_ON(chunk_size == 0);
413
414 kfd->gtt_sa_chunk_size = chunk_size;
415 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
416
417 num_of_bits = kfd->gtt_sa_num_of_chunks / BITS_PER_BYTE;
418 BUG_ON(num_of_bits == 0);
419
420 kfd->gtt_sa_bitmap = kzalloc(num_of_bits, GFP_KERNEL);
421
422 if (!kfd->gtt_sa_bitmap)
423 return -ENOMEM;
424
425 pr_debug("kfd: gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
426 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
427
428 mutex_init(&kfd->gtt_sa_lock);
429
430 return 0;
431
432}
433
434static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
435{
436 mutex_destroy(&kfd->gtt_sa_lock);
437 kfree(kfd->gtt_sa_bitmap);
438}
439
440static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
441 unsigned int bit_num,
442 unsigned int chunk_size)
443{
444 return start_addr + bit_num * chunk_size;
445}
446
447static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
448 unsigned int bit_num,
449 unsigned int chunk_size)
450{
451 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
452}
453
454int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
455 struct kfd_mem_obj **mem_obj)
456{
457 unsigned int found, start_search, cur_size;
458
459 BUG_ON(!kfd);
460
461 if (size == 0)
462 return -EINVAL;
463
464 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
465 return -ENOMEM;
466
467 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
468 if ((*mem_obj) == NULL)
469 return -ENOMEM;
470
471 pr_debug("kfd: allocated mem_obj = %p for size = %d\n", *mem_obj, size);
472
473 start_search = 0;
474
475 mutex_lock(&kfd->gtt_sa_lock);
476
477kfd_gtt_restart_search:
478 /* Find the first chunk that is free */
479 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
480 kfd->gtt_sa_num_of_chunks,
481 start_search);
482
483 pr_debug("kfd: found = %d\n", found);
484
485 /* If there wasn't any free chunk, bail out */
486 if (found == kfd->gtt_sa_num_of_chunks)
487 goto kfd_gtt_no_free_chunk;
488
489 /* Update fields of mem_obj */
490 (*mem_obj)->range_start = found;
491 (*mem_obj)->range_end = found;
492 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
493 kfd->gtt_start_gpu_addr,
494 found,
495 kfd->gtt_sa_chunk_size);
496 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
497 kfd->gtt_start_cpu_ptr,
498 found,
499 kfd->gtt_sa_chunk_size);
500
501 pr_debug("kfd: gpu_addr = %p, cpu_addr = %p\n",
502 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
503
504 /* If we need only one chunk, mark it as allocated and get out */
505 if (size <= kfd->gtt_sa_chunk_size) {
506 pr_debug("kfd: single bit\n");
507 set_bit(found, kfd->gtt_sa_bitmap);
508 goto kfd_gtt_out;
509 }
510
511 /* Otherwise, try to see if we have enough contiguous chunks */
512 cur_size = size - kfd->gtt_sa_chunk_size;
513 do {
514 (*mem_obj)->range_end =
515 find_next_zero_bit(kfd->gtt_sa_bitmap,
516 kfd->gtt_sa_num_of_chunks, ++found);
517 /*
518 * If next free chunk is not contiguous than we need to
519 * restart our search from the last free chunk we found (which
520 * wasn't contiguous to the previous ones
521 */
522 if ((*mem_obj)->range_end != found) {
523 start_search = found;
524 goto kfd_gtt_restart_search;
525 }
526
527 /*
528 * If we reached end of buffer, bail out with error
529 */
530 if (found == kfd->gtt_sa_num_of_chunks)
531 goto kfd_gtt_no_free_chunk;
532
533 /* Check if we don't need another chunk */
534 if (cur_size <= kfd->gtt_sa_chunk_size)
535 cur_size = 0;
536 else
537 cur_size -= kfd->gtt_sa_chunk_size;
538
539 } while (cur_size > 0);
540
541 pr_debug("kfd: range_start = %d, range_end = %d\n",
542 (*mem_obj)->range_start, (*mem_obj)->range_end);
543
544 /* Mark the chunks as allocated */
545 for (found = (*mem_obj)->range_start;
546 found <= (*mem_obj)->range_end;
547 found++)
548 set_bit(found, kfd->gtt_sa_bitmap);
549
550kfd_gtt_out:
551 mutex_unlock(&kfd->gtt_sa_lock);
552 return 0;
553
554kfd_gtt_no_free_chunk:
555 pr_debug("kfd: allocation failed with mem_obj = %p\n", mem_obj);
556 mutex_unlock(&kfd->gtt_sa_lock);
557 kfree(mem_obj);
558 return -ENOMEM;
559}
560
561int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
562{
563 unsigned int bit;
564
565 BUG_ON(!kfd);
Oded Gabbay9216ed22015-01-12 22:34:21 +0200566
567 /* 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
571 pr_debug("kfd: free mem_obj = %p, range_start = %d, range_end = %d\n",
572 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}