blob: 13c30a0996842c8c994d0fc189c332dfef9917e9 [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,
36 .ih_ring_entry_size = 4 * sizeof(uint32_t),
37 .mqd_size_aligned = MQD_SIZE_ALIGNED
38};
39
40static const struct kfd_device_info carrizo_device_info = {
41 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030042 .max_pasid_bits = 16,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030043 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030044 .num_of_watch_points = 4,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030045 .mqd_size_aligned = MQD_SIZE_ALIGNED
Oded Gabbay4a488a72014-07-16 21:08:55 +030046};
47
48struct kfd_deviceid {
49 unsigned short did;
50 const struct kfd_device_info *device_info;
51};
52
53/* Please keep this sorted by increasing device id. */
54static const struct kfd_deviceid supported_devices[] = {
55 { 0x1304, &kaveri_device_info }, /* Kaveri */
56 { 0x1305, &kaveri_device_info }, /* Kaveri */
57 { 0x1306, &kaveri_device_info }, /* Kaveri */
58 { 0x1307, &kaveri_device_info }, /* Kaveri */
59 { 0x1309, &kaveri_device_info }, /* Kaveri */
60 { 0x130A, &kaveri_device_info }, /* Kaveri */
61 { 0x130B, &kaveri_device_info }, /* Kaveri */
62 { 0x130C, &kaveri_device_info }, /* Kaveri */
63 { 0x130D, &kaveri_device_info }, /* Kaveri */
64 { 0x130E, &kaveri_device_info }, /* Kaveri */
65 { 0x130F, &kaveri_device_info }, /* Kaveri */
66 { 0x1310, &kaveri_device_info }, /* Kaveri */
67 { 0x1311, &kaveri_device_info }, /* Kaveri */
68 { 0x1312, &kaveri_device_info }, /* Kaveri */
69 { 0x1313, &kaveri_device_info }, /* Kaveri */
70 { 0x1315, &kaveri_device_info }, /* Kaveri */
71 { 0x1316, &kaveri_device_info }, /* Kaveri */
72 { 0x1317, &kaveri_device_info }, /* Kaveri */
73 { 0x1318, &kaveri_device_info }, /* Kaveri */
74 { 0x131B, &kaveri_device_info }, /* Kaveri */
75 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz0da75582015-01-01 17:10:01 +020076 { 0x131D, &kaveri_device_info } /* Kaveri */
Oded Gabbay4a488a72014-07-16 21:08:55 +030077};
78
Oded Gabbay6e810902014-10-27 14:36:07 +020079static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
80 unsigned int chunk_size);
81static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
82
Oded Gabbay4a488a72014-07-16 21:08:55 +030083static const struct kfd_device_info *lookup_device_info(unsigned short did)
84{
85 size_t i;
86
87 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
88 if (supported_devices[i].did == did) {
89 BUG_ON(supported_devices[i].device_info == NULL);
90 return supported_devices[i].device_info;
91 }
92 }
93
94 return NULL;
95}
96
Xihan Zhangcea405b2015-03-17 19:32:53 +080097struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
98 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +030099{
100 struct kfd_dev *kfd;
101
102 const struct kfd_device_info *device_info =
103 lookup_device_info(pdev->device);
104
105 if (!device_info)
106 return NULL;
107
108 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
109 if (!kfd)
110 return NULL;
111
112 kfd->kgd = kgd;
113 kfd->device_info = device_info;
114 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300115 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800116 kfd->kfd2kgd = f2g;
117
118 mutex_init(&kfd->doorbell_mutex);
119 memset(&kfd->doorbell_available_index, 0,
120 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300121
122 return kfd;
123}
124
Oded Gabbayb17f0682014-07-17 00:06:27 +0300125static bool device_iommu_pasid_init(struct kfd_dev *kfd)
126{
127 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
128 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
129 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
130
131 struct amd_iommu_device_info iommu_info;
132 unsigned int pasid_limit;
133 int err;
134
135 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
136 if (err < 0) {
137 dev_err(kfd_device,
138 "error getting iommu info. is the iommu enabled?\n");
139 return false;
140 }
141
142 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
143 dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n",
144 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
145 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
146 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0);
147 return false;
148 }
149
150 pasid_limit = min_t(unsigned int,
151 (unsigned int)1 << kfd->device_info->max_pasid_bits,
152 iommu_info.max_pasids);
153 /*
154 * last pasid is used for kernel queues doorbells
155 * in the future the last pasid might be used for a kernel thread.
156 */
157 pasid_limit = min_t(unsigned int,
158 pasid_limit,
159 kfd->doorbell_process_limit - 1);
160
161 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
162 if (err < 0) {
163 dev_err(kfd_device, "error initializing iommu device\n");
164 return false;
165 }
166
167 if (!kfd_set_pasid_limit(pasid_limit)) {
168 dev_err(kfd_device, "error setting pasid limit\n");
169 amd_iommu_free_device(kfd->pdev);
170 return false;
171 }
172
173 return true;
174}
175
176static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
177{
178 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
179
180 if (dev)
181 kfd_unbind_process_from_device(dev, pasid);
182}
183
Oded Gabbay4a488a72014-07-16 21:08:55 +0300184bool kgd2kfd_device_init(struct kfd_dev *kfd,
185 const struct kgd2kfd_shared_resources *gpu_resources)
186{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300187 unsigned int size;
188
Oded Gabbay4a488a72014-07-16 21:08:55 +0300189 kfd->shared_resources = *gpu_resources;
190
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300191 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200192 size = max_num_of_queues_per_device *
193 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300194
Oded Gabbaye18e7942014-10-26 10:12:22 +0200195 /*
196 * calculate max size of runlist packet.
197 * There can be only 2 packets at once
198 */
Dave Airlieb3869b12015-01-29 11:45:31 +1000199 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_map_process) +
200 max_num_of_queues_per_device *
Oded Gabbaye18e7942014-10-26 10:12:22 +0200201 sizeof(struct pm4_map_queues) + sizeof(struct pm4_runlist)) * 2;
202
203 /* Add size of HIQ & DIQ */
204 size += KFD_KERNEL_QUEUE_SIZE * 2;
205
206 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300207 size += 512 * 1024;
208
Xihan Zhangcea405b2015-03-17 19:32:53 +0800209 if (kfd->kfd2kgd->init_gtt_mem_allocation(
210 kfd->kgd, size, &kfd->gtt_mem,
211 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300212 dev_err(kfd_device,
Oded Gabbaye18e7942014-10-26 10:12:22 +0200213 "Could not allocate %d bytes for device (%x:%x)\n",
214 size, kfd->pdev->vendor, kfd->pdev->device);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300215 goto out;
216 }
217
Oded Gabbaye18e7942014-10-26 10:12:22 +0200218 dev_info(kfd_device,
219 "Allocated %d bytes on gart for device(%x:%x)\n",
220 size, kfd->pdev->vendor, kfd->pdev->device);
221
Oded Gabbay73a1da02014-10-26 09:53:37 +0200222 /* Initialize GTT sa with 512 byte chunk size */
223 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
224 dev_err(kfd_device,
225 "Error initializing gtt sub-allocator\n");
226 goto kfd_gtt_sa_init_error;
227 }
228
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300229 kfd_doorbell_init(kfd);
230
231 if (kfd_topology_add_device(kfd) != 0) {
232 dev_err(kfd_device,
233 "Error adding device (%x:%x) to topology\n",
234 kfd->pdev->vendor, kfd->pdev->device);
235 goto kfd_topology_add_device_error;
236 }
237
Andrew Lewycky2249d552014-07-17 01:37:30 +0300238 if (kfd_interrupt_init(kfd)) {
239 dev_err(kfd_device,
240 "Error initializing interrupts for device (%x:%x)\n",
241 kfd->pdev->vendor, kfd->pdev->device);
242 goto kfd_interrupt_error;
243 }
244
Oded Gabbayb17f0682014-07-17 00:06:27 +0300245 if (!device_iommu_pasid_init(kfd)) {
246 dev_err(kfd_device,
247 "Error initializing iommuv2 for device (%x:%x)\n",
248 kfd->pdev->vendor, kfd->pdev->device);
249 goto device_iommu_pasid_error;
250 }
251 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
252 iommu_pasid_shutdown_callback);
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300253
Ben Goz64c7f8c2014-07-17 01:27:00 +0300254 kfd->dqm = device_queue_manager_init(kfd);
255 if (!kfd->dqm) {
256 dev_err(kfd_device,
257 "Error initializing queue manager for device (%x:%x)\n",
258 kfd->pdev->vendor, kfd->pdev->device);
259 goto device_queue_manager_error;
260 }
261
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200262 if (kfd->dqm->ops.start(kfd->dqm) != 0) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300263 dev_err(kfd_device,
264 "Error starting queuen manager for device (%x:%x)\n",
265 kfd->pdev->vendor, kfd->pdev->device);
266 goto dqm_start_error;
267 }
268
Oded Gabbay4a488a72014-07-16 21:08:55 +0300269 kfd->init_complete = true;
270 dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor,
271 kfd->pdev->device);
272
Ben Goz64c7f8c2014-07-17 01:27:00 +0300273 pr_debug("kfd: Starting kfd with the following scheduling policy %d\n",
274 sched_policy);
275
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300276 goto out;
277
Ben Goz64c7f8c2014-07-17 01:27:00 +0300278dqm_start_error:
279 device_queue_manager_uninit(kfd->dqm);
280device_queue_manager_error:
281 amd_iommu_free_device(kfd->pdev);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300282device_iommu_pasid_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300283 kfd_interrupt_exit(kfd);
284kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300285 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300286kfd_topology_add_device_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200287 kfd_gtt_sa_fini(kfd);
288kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800289 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300290 dev_err(kfd_device,
291 "device (%x:%x) NOT added due to errors\n",
292 kfd->pdev->vendor, kfd->pdev->device);
293out:
294 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300295}
296
297void kgd2kfd_device_exit(struct kfd_dev *kfd)
298{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300299 if (kfd->init_complete) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300300 device_queue_manager_uninit(kfd->dqm);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300301 amd_iommu_free_device(kfd->pdev);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300302 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300303 kfd_topology_remove_device(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200304 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800305 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300306 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300307
Oded Gabbay4a488a72014-07-16 21:08:55 +0300308 kfree(kfd);
309}
310
311void kgd2kfd_suspend(struct kfd_dev *kfd)
312{
313 BUG_ON(kfd == NULL);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300314
Ben Goz64c7f8c2014-07-17 01:27:00 +0300315 if (kfd->init_complete) {
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200316 kfd->dqm->ops.stop(kfd->dqm);
Oded Gabbayabc9d3e2014-11-09 22:36:22 +0200317 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300318 amd_iommu_free_device(kfd->pdev);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300319 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300320}
321
322int kgd2kfd_resume(struct kfd_dev *kfd)
323{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300324 unsigned int pasid_limit;
325 int err;
326
Oded Gabbay4a488a72014-07-16 21:08:55 +0300327 BUG_ON(kfd == NULL);
328
Oded Gabbayb17f0682014-07-17 00:06:27 +0300329 pasid_limit = kfd_get_pasid_limit();
330
331 if (kfd->init_complete) {
332 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
333 if (err < 0)
334 return -ENXIO;
335 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
336 iommu_pasid_shutdown_callback);
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200337 kfd->dqm->ops.start(kfd->dqm);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300338 }
339
Oded Gabbay4a488a72014-07-16 21:08:55 +0300340 return 0;
341}
342
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300343/* This is called directly from KGD at ISR. */
344void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300345{
Andrew Lewycky2249d552014-07-17 01:37:30 +0300346 if (!kfd->init_complete)
347 return;
348
349 spin_lock(&kfd->interrupt_lock);
350
351 if (kfd->interrupts_active
352 && interrupt_is_wanted(kfd, ih_ring_entry)
353 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
354 schedule_work(&kfd->interrupt_work);
355
356 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300357}
Oded Gabbay6e810902014-10-27 14:36:07 +0200358
359static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
360 unsigned int chunk_size)
361{
362 unsigned int num_of_bits;
363
364 BUG_ON(!kfd);
365 BUG_ON(!kfd->gtt_mem);
366 BUG_ON(buf_size < chunk_size);
367 BUG_ON(buf_size == 0);
368 BUG_ON(chunk_size == 0);
369
370 kfd->gtt_sa_chunk_size = chunk_size;
371 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
372
373 num_of_bits = kfd->gtt_sa_num_of_chunks / BITS_PER_BYTE;
374 BUG_ON(num_of_bits == 0);
375
376 kfd->gtt_sa_bitmap = kzalloc(num_of_bits, GFP_KERNEL);
377
378 if (!kfd->gtt_sa_bitmap)
379 return -ENOMEM;
380
381 pr_debug("kfd: gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
382 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
383
384 mutex_init(&kfd->gtt_sa_lock);
385
386 return 0;
387
388}
389
390static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
391{
392 mutex_destroy(&kfd->gtt_sa_lock);
393 kfree(kfd->gtt_sa_bitmap);
394}
395
396static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
397 unsigned int bit_num,
398 unsigned int chunk_size)
399{
400 return start_addr + bit_num * chunk_size;
401}
402
403static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
404 unsigned int bit_num,
405 unsigned int chunk_size)
406{
407 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
408}
409
410int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
411 struct kfd_mem_obj **mem_obj)
412{
413 unsigned int found, start_search, cur_size;
414
415 BUG_ON(!kfd);
416
417 if (size == 0)
418 return -EINVAL;
419
420 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
421 return -ENOMEM;
422
423 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
424 if ((*mem_obj) == NULL)
425 return -ENOMEM;
426
427 pr_debug("kfd: allocated mem_obj = %p for size = %d\n", *mem_obj, size);
428
429 start_search = 0;
430
431 mutex_lock(&kfd->gtt_sa_lock);
432
433kfd_gtt_restart_search:
434 /* Find the first chunk that is free */
435 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
436 kfd->gtt_sa_num_of_chunks,
437 start_search);
438
439 pr_debug("kfd: found = %d\n", found);
440
441 /* If there wasn't any free chunk, bail out */
442 if (found == kfd->gtt_sa_num_of_chunks)
443 goto kfd_gtt_no_free_chunk;
444
445 /* Update fields of mem_obj */
446 (*mem_obj)->range_start = found;
447 (*mem_obj)->range_end = found;
448 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
449 kfd->gtt_start_gpu_addr,
450 found,
451 kfd->gtt_sa_chunk_size);
452 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
453 kfd->gtt_start_cpu_ptr,
454 found,
455 kfd->gtt_sa_chunk_size);
456
457 pr_debug("kfd: gpu_addr = %p, cpu_addr = %p\n",
458 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
459
460 /* If we need only one chunk, mark it as allocated and get out */
461 if (size <= kfd->gtt_sa_chunk_size) {
462 pr_debug("kfd: single bit\n");
463 set_bit(found, kfd->gtt_sa_bitmap);
464 goto kfd_gtt_out;
465 }
466
467 /* Otherwise, try to see if we have enough contiguous chunks */
468 cur_size = size - kfd->gtt_sa_chunk_size;
469 do {
470 (*mem_obj)->range_end =
471 find_next_zero_bit(kfd->gtt_sa_bitmap,
472 kfd->gtt_sa_num_of_chunks, ++found);
473 /*
474 * If next free chunk is not contiguous than we need to
475 * restart our search from the last free chunk we found (which
476 * wasn't contiguous to the previous ones
477 */
478 if ((*mem_obj)->range_end != found) {
479 start_search = found;
480 goto kfd_gtt_restart_search;
481 }
482
483 /*
484 * If we reached end of buffer, bail out with error
485 */
486 if (found == kfd->gtt_sa_num_of_chunks)
487 goto kfd_gtt_no_free_chunk;
488
489 /* Check if we don't need another chunk */
490 if (cur_size <= kfd->gtt_sa_chunk_size)
491 cur_size = 0;
492 else
493 cur_size -= kfd->gtt_sa_chunk_size;
494
495 } while (cur_size > 0);
496
497 pr_debug("kfd: range_start = %d, range_end = %d\n",
498 (*mem_obj)->range_start, (*mem_obj)->range_end);
499
500 /* Mark the chunks as allocated */
501 for (found = (*mem_obj)->range_start;
502 found <= (*mem_obj)->range_end;
503 found++)
504 set_bit(found, kfd->gtt_sa_bitmap);
505
506kfd_gtt_out:
507 mutex_unlock(&kfd->gtt_sa_lock);
508 return 0;
509
510kfd_gtt_no_free_chunk:
511 pr_debug("kfd: allocation failed with mem_obj = %p\n", mem_obj);
512 mutex_unlock(&kfd->gtt_sa_lock);
513 kfree(mem_obj);
514 return -ENOMEM;
515}
516
517int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
518{
519 unsigned int bit;
520
521 BUG_ON(!kfd);
Oded Gabbay9216ed22015-01-12 22:34:21 +0200522
523 /* Act like kfree when trying to free a NULL object */
524 if (!mem_obj)
525 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200526
527 pr_debug("kfd: free mem_obj = %p, range_start = %d, range_end = %d\n",
528 mem_obj, mem_obj->range_start, mem_obj->range_end);
529
530 mutex_lock(&kfd->gtt_sa_lock);
531
532 /* Mark the chunks as free */
533 for (bit = mem_obj->range_start;
534 bit <= mem_obj->range_end;
535 bit++)
536 clear_bit(bit, kfd->gtt_sa_bitmap);
537
538 mutex_unlock(&kfd->gtt_sa_lock);
539
540 kfree(mem_obj);
541 return 0;
542}