blob: 4c0316957a57bb761611db739707069b4a2f2398 [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),
Andrew Lewyckyf3a39812015-05-10 12:15:46 +030037 .event_interrupt_class = &event_interrupt_class_cik,
Ben Goz0da75582015-01-01 17:10:01 +020038 .mqd_size_aligned = MQD_SIZE_ALIGNED
39};
40
41static const struct kfd_device_info carrizo_device_info = {
42 .asic_family = CHIP_CARRIZO,
Oded Gabbay4a488a72014-07-16 21:08:55 +030043 .max_pasid_bits = 16,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030044 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030045 .num_of_watch_points = 4,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030046 .mqd_size_aligned = MQD_SIZE_ALIGNED
Oded Gabbay4a488a72014-07-16 21:08:55 +030047};
48
49struct kfd_deviceid {
50 unsigned short did;
51 const struct kfd_device_info *device_info;
52};
53
54/* Please keep this sorted by increasing device id. */
55static const struct kfd_deviceid supported_devices[] = {
56 { 0x1304, &kaveri_device_info }, /* Kaveri */
57 { 0x1305, &kaveri_device_info }, /* Kaveri */
58 { 0x1306, &kaveri_device_info }, /* Kaveri */
59 { 0x1307, &kaveri_device_info }, /* Kaveri */
60 { 0x1309, &kaveri_device_info }, /* Kaveri */
61 { 0x130A, &kaveri_device_info }, /* Kaveri */
62 { 0x130B, &kaveri_device_info }, /* Kaveri */
63 { 0x130C, &kaveri_device_info }, /* Kaveri */
64 { 0x130D, &kaveri_device_info }, /* Kaveri */
65 { 0x130E, &kaveri_device_info }, /* Kaveri */
66 { 0x130F, &kaveri_device_info }, /* Kaveri */
67 { 0x1310, &kaveri_device_info }, /* Kaveri */
68 { 0x1311, &kaveri_device_info }, /* Kaveri */
69 { 0x1312, &kaveri_device_info }, /* Kaveri */
70 { 0x1313, &kaveri_device_info }, /* Kaveri */
71 { 0x1315, &kaveri_device_info }, /* Kaveri */
72 { 0x1316, &kaveri_device_info }, /* Kaveri */
73 { 0x1317, &kaveri_device_info }, /* Kaveri */
74 { 0x1318, &kaveri_device_info }, /* Kaveri */
75 { 0x131B, &kaveri_device_info }, /* Kaveri */
76 { 0x131C, &kaveri_device_info }, /* Kaveri */
Ben Goz0da75582015-01-01 17:10:01 +020077 { 0x131D, &kaveri_device_info } /* Kaveri */
Oded Gabbay4a488a72014-07-16 21:08:55 +030078};
79
Oded Gabbay6e810902014-10-27 14:36:07 +020080static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
81 unsigned int chunk_size);
82static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
83
Oded Gabbay4a488a72014-07-16 21:08:55 +030084static const struct kfd_device_info *lookup_device_info(unsigned short did)
85{
86 size_t i;
87
88 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
89 if (supported_devices[i].did == did) {
90 BUG_ON(supported_devices[i].device_info == NULL);
91 return supported_devices[i].device_info;
92 }
93 }
94
95 return NULL;
96}
97
Xihan Zhangcea405b2015-03-17 19:32:53 +080098struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
99 struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300100{
101 struct kfd_dev *kfd;
102
103 const struct kfd_device_info *device_info =
104 lookup_device_info(pdev->device);
105
106 if (!device_info)
107 return NULL;
108
109 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
110 if (!kfd)
111 return NULL;
112
113 kfd->kgd = kgd;
114 kfd->device_info = device_info;
115 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300116 kfd->init_complete = false;
Xihan Zhangcea405b2015-03-17 19:32:53 +0800117 kfd->kfd2kgd = f2g;
118
119 mutex_init(&kfd->doorbell_mutex);
120 memset(&kfd->doorbell_available_index, 0,
121 sizeof(kfd->doorbell_available_index));
Oded Gabbay4a488a72014-07-16 21:08:55 +0300122
123 return kfd;
124}
125
Oded Gabbayb17f0682014-07-17 00:06:27 +0300126static bool device_iommu_pasid_init(struct kfd_dev *kfd)
127{
128 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
129 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
130 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
131
132 struct amd_iommu_device_info iommu_info;
133 unsigned int pasid_limit;
134 int err;
135
136 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
137 if (err < 0) {
138 dev_err(kfd_device,
139 "error getting iommu info. is the iommu enabled?\n");
140 return false;
141 }
142
143 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
144 dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n",
145 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
146 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
147 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0);
148 return false;
149 }
150
151 pasid_limit = min_t(unsigned int,
152 (unsigned int)1 << kfd->device_info->max_pasid_bits,
153 iommu_info.max_pasids);
154 /*
155 * last pasid is used for kernel queues doorbells
156 * in the future the last pasid might be used for a kernel thread.
157 */
158 pasid_limit = min_t(unsigned int,
159 pasid_limit,
160 kfd->doorbell_process_limit - 1);
161
162 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
163 if (err < 0) {
164 dev_err(kfd_device, "error initializing iommu device\n");
165 return false;
166 }
167
168 if (!kfd_set_pasid_limit(pasid_limit)) {
169 dev_err(kfd_device, "error setting pasid limit\n");
170 amd_iommu_free_device(kfd->pdev);
171 return false;
172 }
173
174 return true;
175}
176
177static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
178{
179 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
180
181 if (dev)
182 kfd_unbind_process_from_device(dev, pasid);
183}
184
Oded Gabbay4a488a72014-07-16 21:08:55 +0300185bool kgd2kfd_device_init(struct kfd_dev *kfd,
186 const struct kgd2kfd_shared_resources *gpu_resources)
187{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300188 unsigned int size;
189
Oded Gabbay4a488a72014-07-16 21:08:55 +0300190 kfd->shared_resources = *gpu_resources;
191
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300192 /* calculate max size of mqds needed for queues */
Oded Gabbayb8cbab02015-01-18 13:18:01 +0200193 size = max_num_of_queues_per_device *
194 kfd->device_info->mqd_size_aligned;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300195
Oded Gabbaye18e7942014-10-26 10:12:22 +0200196 /*
197 * calculate max size of runlist packet.
198 * There can be only 2 packets at once
199 */
Dave Airlieb3869b12015-01-29 11:45:31 +1000200 size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_map_process) +
201 max_num_of_queues_per_device *
Oded Gabbaye18e7942014-10-26 10:12:22 +0200202 sizeof(struct pm4_map_queues) + sizeof(struct pm4_runlist)) * 2;
203
204 /* Add size of HIQ & DIQ */
205 size += KFD_KERNEL_QUEUE_SIZE * 2;
206
207 /* add another 512KB for all other allocations on gart (HPD, fences) */
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300208 size += 512 * 1024;
209
Xihan Zhangcea405b2015-03-17 19:32:53 +0800210 if (kfd->kfd2kgd->init_gtt_mem_allocation(
211 kfd->kgd, size, &kfd->gtt_mem,
212 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300213 dev_err(kfd_device,
Oded Gabbaye18e7942014-10-26 10:12:22 +0200214 "Could not allocate %d bytes for device (%x:%x)\n",
215 size, kfd->pdev->vendor, kfd->pdev->device);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300216 goto out;
217 }
218
Oded Gabbaye18e7942014-10-26 10:12:22 +0200219 dev_info(kfd_device,
220 "Allocated %d bytes on gart for device(%x:%x)\n",
221 size, kfd->pdev->vendor, kfd->pdev->device);
222
Oded Gabbay73a1da02014-10-26 09:53:37 +0200223 /* Initialize GTT sa with 512 byte chunk size */
224 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
225 dev_err(kfd_device,
226 "Error initializing gtt sub-allocator\n");
227 goto kfd_gtt_sa_init_error;
228 }
229
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300230 kfd_doorbell_init(kfd);
231
232 if (kfd_topology_add_device(kfd) != 0) {
233 dev_err(kfd_device,
234 "Error adding device (%x:%x) to topology\n",
235 kfd->pdev->vendor, kfd->pdev->device);
236 goto kfd_topology_add_device_error;
237 }
238
Andrew Lewycky2249d552014-07-17 01:37:30 +0300239 if (kfd_interrupt_init(kfd)) {
240 dev_err(kfd_device,
241 "Error initializing interrupts for device (%x:%x)\n",
242 kfd->pdev->vendor, kfd->pdev->device);
243 goto kfd_interrupt_error;
244 }
245
Oded Gabbayb17f0682014-07-17 00:06:27 +0300246 if (!device_iommu_pasid_init(kfd)) {
247 dev_err(kfd_device,
248 "Error initializing iommuv2 for device (%x:%x)\n",
249 kfd->pdev->vendor, kfd->pdev->device);
250 goto device_iommu_pasid_error;
251 }
252 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
253 iommu_pasid_shutdown_callback);
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300254
Ben Goz64c7f8c2014-07-17 01:27:00 +0300255 kfd->dqm = device_queue_manager_init(kfd);
256 if (!kfd->dqm) {
257 dev_err(kfd_device,
258 "Error initializing queue manager for device (%x:%x)\n",
259 kfd->pdev->vendor, kfd->pdev->device);
260 goto device_queue_manager_error;
261 }
262
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200263 if (kfd->dqm->ops.start(kfd->dqm) != 0) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300264 dev_err(kfd_device,
265 "Error starting queuen manager for device (%x:%x)\n",
266 kfd->pdev->vendor, kfd->pdev->device);
267 goto dqm_start_error;
268 }
269
Oded Gabbay4a488a72014-07-16 21:08:55 +0300270 kfd->init_complete = true;
271 dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor,
272 kfd->pdev->device);
273
Ben Goz64c7f8c2014-07-17 01:27:00 +0300274 pr_debug("kfd: Starting kfd with the following scheduling policy %d\n",
275 sched_policy);
276
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300277 goto out;
278
Ben Goz64c7f8c2014-07-17 01:27:00 +0300279dqm_start_error:
280 device_queue_manager_uninit(kfd->dqm);
281device_queue_manager_error:
282 amd_iommu_free_device(kfd->pdev);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300283device_iommu_pasid_error:
Andrew Lewycky2249d552014-07-17 01:37:30 +0300284 kfd_interrupt_exit(kfd);
285kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300286 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300287kfd_topology_add_device_error:
Oded Gabbay73a1da02014-10-26 09:53:37 +0200288 kfd_gtt_sa_fini(kfd);
289kfd_gtt_sa_init_error:
Xihan Zhangcea405b2015-03-17 19:32:53 +0800290 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300291 dev_err(kfd_device,
292 "device (%x:%x) NOT added due to errors\n",
293 kfd->pdev->vendor, kfd->pdev->device);
294out:
295 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300296}
297
298void kgd2kfd_device_exit(struct kfd_dev *kfd)
299{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300300 if (kfd->init_complete) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300301 device_queue_manager_uninit(kfd->dqm);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300302 amd_iommu_free_device(kfd->pdev);
Andrew Lewycky2249d552014-07-17 01:37:30 +0300303 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300304 kfd_topology_remove_device(kfd);
Oded Gabbay73a1da02014-10-26 09:53:37 +0200305 kfd_gtt_sa_fini(kfd);
Xihan Zhangcea405b2015-03-17 19:32:53 +0800306 kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300307 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300308
Oded Gabbay4a488a72014-07-16 21:08:55 +0300309 kfree(kfd);
310}
311
312void kgd2kfd_suspend(struct kfd_dev *kfd)
313{
314 BUG_ON(kfd == NULL);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300315
Ben Goz64c7f8c2014-07-17 01:27:00 +0300316 if (kfd->init_complete) {
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200317 kfd->dqm->ops.stop(kfd->dqm);
Oded Gabbayabc9d3e2014-11-09 22:36:22 +0200318 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300319 amd_iommu_free_device(kfd->pdev);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300320 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300321}
322
323int kgd2kfd_resume(struct kfd_dev *kfd)
324{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300325 unsigned int pasid_limit;
326 int err;
327
Oded Gabbay4a488a72014-07-16 21:08:55 +0300328 BUG_ON(kfd == NULL);
329
Oded Gabbayb17f0682014-07-17 00:06:27 +0300330 pasid_limit = kfd_get_pasid_limit();
331
332 if (kfd->init_complete) {
333 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
334 if (err < 0)
335 return -ENXIO;
336 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
337 iommu_pasid_shutdown_callback);
Oded Gabbay45c9a5e2015-01-12 14:26:10 +0200338 kfd->dqm->ops.start(kfd->dqm);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300339 }
340
Oded Gabbay4a488a72014-07-16 21:08:55 +0300341 return 0;
342}
343
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300344/* This is called directly from KGD at ISR. */
345void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300346{
Andrew Lewycky2249d552014-07-17 01:37:30 +0300347 if (!kfd->init_complete)
348 return;
349
350 spin_lock(&kfd->interrupt_lock);
351
352 if (kfd->interrupts_active
353 && interrupt_is_wanted(kfd, ih_ring_entry)
354 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
355 schedule_work(&kfd->interrupt_work);
356
357 spin_unlock(&kfd->interrupt_lock);
Oded Gabbay4a488a72014-07-16 21:08:55 +0300358}
Oded Gabbay6e810902014-10-27 14:36:07 +0200359
360static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
361 unsigned int chunk_size)
362{
363 unsigned int num_of_bits;
364
365 BUG_ON(!kfd);
366 BUG_ON(!kfd->gtt_mem);
367 BUG_ON(buf_size < chunk_size);
368 BUG_ON(buf_size == 0);
369 BUG_ON(chunk_size == 0);
370
371 kfd->gtt_sa_chunk_size = chunk_size;
372 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
373
374 num_of_bits = kfd->gtt_sa_num_of_chunks / BITS_PER_BYTE;
375 BUG_ON(num_of_bits == 0);
376
377 kfd->gtt_sa_bitmap = kzalloc(num_of_bits, GFP_KERNEL);
378
379 if (!kfd->gtt_sa_bitmap)
380 return -ENOMEM;
381
382 pr_debug("kfd: gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
383 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
384
385 mutex_init(&kfd->gtt_sa_lock);
386
387 return 0;
388
389}
390
391static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
392{
393 mutex_destroy(&kfd->gtt_sa_lock);
394 kfree(kfd->gtt_sa_bitmap);
395}
396
397static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
398 unsigned int bit_num,
399 unsigned int chunk_size)
400{
401 return start_addr + bit_num * chunk_size;
402}
403
404static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
405 unsigned int bit_num,
406 unsigned int chunk_size)
407{
408 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
409}
410
411int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
412 struct kfd_mem_obj **mem_obj)
413{
414 unsigned int found, start_search, cur_size;
415
416 BUG_ON(!kfd);
417
418 if (size == 0)
419 return -EINVAL;
420
421 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
422 return -ENOMEM;
423
424 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
425 if ((*mem_obj) == NULL)
426 return -ENOMEM;
427
428 pr_debug("kfd: allocated mem_obj = %p for size = %d\n", *mem_obj, size);
429
430 start_search = 0;
431
432 mutex_lock(&kfd->gtt_sa_lock);
433
434kfd_gtt_restart_search:
435 /* Find the first chunk that is free */
436 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
437 kfd->gtt_sa_num_of_chunks,
438 start_search);
439
440 pr_debug("kfd: found = %d\n", found);
441
442 /* If there wasn't any free chunk, bail out */
443 if (found == kfd->gtt_sa_num_of_chunks)
444 goto kfd_gtt_no_free_chunk;
445
446 /* Update fields of mem_obj */
447 (*mem_obj)->range_start = found;
448 (*mem_obj)->range_end = found;
449 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
450 kfd->gtt_start_gpu_addr,
451 found,
452 kfd->gtt_sa_chunk_size);
453 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
454 kfd->gtt_start_cpu_ptr,
455 found,
456 kfd->gtt_sa_chunk_size);
457
458 pr_debug("kfd: gpu_addr = %p, cpu_addr = %p\n",
459 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
460
461 /* If we need only one chunk, mark it as allocated and get out */
462 if (size <= kfd->gtt_sa_chunk_size) {
463 pr_debug("kfd: single bit\n");
464 set_bit(found, kfd->gtt_sa_bitmap);
465 goto kfd_gtt_out;
466 }
467
468 /* Otherwise, try to see if we have enough contiguous chunks */
469 cur_size = size - kfd->gtt_sa_chunk_size;
470 do {
471 (*mem_obj)->range_end =
472 find_next_zero_bit(kfd->gtt_sa_bitmap,
473 kfd->gtt_sa_num_of_chunks, ++found);
474 /*
475 * If next free chunk is not contiguous than we need to
476 * restart our search from the last free chunk we found (which
477 * wasn't contiguous to the previous ones
478 */
479 if ((*mem_obj)->range_end != found) {
480 start_search = found;
481 goto kfd_gtt_restart_search;
482 }
483
484 /*
485 * If we reached end of buffer, bail out with error
486 */
487 if (found == kfd->gtt_sa_num_of_chunks)
488 goto kfd_gtt_no_free_chunk;
489
490 /* Check if we don't need another chunk */
491 if (cur_size <= kfd->gtt_sa_chunk_size)
492 cur_size = 0;
493 else
494 cur_size -= kfd->gtt_sa_chunk_size;
495
496 } while (cur_size > 0);
497
498 pr_debug("kfd: range_start = %d, range_end = %d\n",
499 (*mem_obj)->range_start, (*mem_obj)->range_end);
500
501 /* Mark the chunks as allocated */
502 for (found = (*mem_obj)->range_start;
503 found <= (*mem_obj)->range_end;
504 found++)
505 set_bit(found, kfd->gtt_sa_bitmap);
506
507kfd_gtt_out:
508 mutex_unlock(&kfd->gtt_sa_lock);
509 return 0;
510
511kfd_gtt_no_free_chunk:
512 pr_debug("kfd: allocation failed with mem_obj = %p\n", mem_obj);
513 mutex_unlock(&kfd->gtt_sa_lock);
514 kfree(mem_obj);
515 return -ENOMEM;
516}
517
518int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
519{
520 unsigned int bit;
521
522 BUG_ON(!kfd);
Oded Gabbay9216ed22015-01-12 22:34:21 +0200523
524 /* Act like kfree when trying to free a NULL object */
525 if (!mem_obj)
526 return 0;
Oded Gabbay6e810902014-10-27 14:36:07 +0200527
528 pr_debug("kfd: free mem_obj = %p, range_start = %d, range_end = %d\n",
529 mem_obj, mem_obj->range_start, mem_obj->range_end);
530
531 mutex_lock(&kfd->gtt_sa_lock);
532
533 /* Mark the chunks as free */
534 for (bit = mem_obj->range_start;
535 bit <= mem_obj->range_end;
536 bit++)
537 clear_bit(bit, kfd->gtt_sa_bitmap);
538
539 mutex_unlock(&kfd->gtt_sa_lock);
540
541 kfree(mem_obj);
542 return 0;
543}