Home | History | Annotate | Line # | Download | only in amdkfd
kfd_device.c revision 1.1
      1 /*	$NetBSD: kfd_device.c,v 1.1 2018/08/27 01:34:46 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2014 Advanced Micro Devices, Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 __KERNEL_RCSID(0, "$NetBSD: kfd_device.c,v 1.1 2018/08/27 01:34:46 riastradh Exp $");
     27 
     28 #include <linux/amd-iommu.h>
     29 #include <linux/bsearch.h>
     30 #include <linux/pci.h>
     31 #include <linux/slab.h>
     32 #include "kfd_priv.h"
     33 #include "kfd_device_queue_manager.h"
     34 #include "kfd_pm4_headers.h"
     35 
     36 #define MQD_SIZE_ALIGNED 768
     37 
     38 static const struct kfd_device_info kaveri_device_info = {
     39 	.asic_family = CHIP_KAVERI,
     40 	.max_pasid_bits = 16,
     41 	/* max num of queues for KV.TODO should be a dynamic value */
     42 	.max_no_of_hqd	= 24,
     43 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
     44 	.event_interrupt_class = &event_interrupt_class_cik,
     45 	.num_of_watch_points = 4,
     46 	.mqd_size_aligned = MQD_SIZE_ALIGNED
     47 };
     48 
     49 static const struct kfd_device_info carrizo_device_info = {
     50 	.asic_family = CHIP_CARRIZO,
     51 	.max_pasid_bits = 16,
     52 	/* max num of queues for CZ.TODO should be a dynamic value */
     53 	.max_no_of_hqd	= 24,
     54 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
     55 	.event_interrupt_class = &event_interrupt_class_cik,
     56 	.num_of_watch_points = 4,
     57 	.mqd_size_aligned = MQD_SIZE_ALIGNED
     58 };
     59 
     60 struct kfd_deviceid {
     61 	unsigned short did;
     62 	const struct kfd_device_info *device_info;
     63 };
     64 
     65 /* Please keep this sorted by increasing device id. */
     66 static const struct kfd_deviceid supported_devices[] = {
     67 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
     68 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
     69 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
     70 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
     71 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
     72 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
     73 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
     74 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
     75 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
     76 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
     77 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
     78 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
     79 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
     80 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
     81 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
     82 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
     83 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
     84 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
     85 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
     86 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
     87 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
     88 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
     89 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
     90 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
     91 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
     92 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
     93 	{ 0x9877, &carrizo_device_info }	/* Carrizo */
     94 };
     95 
     96 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
     97 				unsigned int chunk_size);
     98 static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
     99 
    100 static 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) {
    106 			BUG_ON(supported_devices[i].device_info == NULL);
    107 			return supported_devices[i].device_info;
    108 		}
    109 	}
    110 
    111 	return NULL;
    112 }
    113 
    114 struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
    115 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
    116 {
    117 	struct kfd_dev *kfd;
    118 
    119 	const struct kfd_device_info *device_info =
    120 					lookup_device_info(pdev->device);
    121 
    122 	if (!device_info)
    123 		return NULL;
    124 
    125 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
    126 	if (!kfd)
    127 		return NULL;
    128 
    129 	kfd->kgd = kgd;
    130 	kfd->device_info = device_info;
    131 	kfd->pdev = pdev;
    132 	kfd->init_complete = false;
    133 	kfd->kfd2kgd = f2g;
    134 
    135 	mutex_init(&kfd->doorbell_mutex);
    136 	memset(&kfd->doorbell_available_index, 0,
    137 		sizeof(kfd->doorbell_available_index));
    138 
    139 	return kfd;
    140 }
    141 
    142 static bool device_iommu_pasid_init(struct kfd_dev *kfd)
    143 {
    144 	const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
    145 					AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
    146 					AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
    147 
    148 	struct amd_iommu_device_info iommu_info;
    149 	unsigned int pasid_limit;
    150 	int err;
    151 
    152 	err = amd_iommu_device_info(kfd->pdev, &iommu_info);
    153 	if (err < 0) {
    154 		dev_err(kfd_device,
    155 			"error getting iommu info. is the iommu enabled?\n");
    156 		return false;
    157 	}
    158 
    159 	if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
    160 		dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n",
    161 		       (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
    162 		       (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
    163 		       (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0);
    164 		return false;
    165 	}
    166 
    167 	pasid_limit = min_t(unsigned int,
    168 			(unsigned int)1 << kfd->device_info->max_pasid_bits,
    169 			iommu_info.max_pasids);
    170 	/*
    171 	 * last pasid is used for kernel queues doorbells
    172 	 * in the future the last pasid might be used for a kernel thread.
    173 	 */
    174 	pasid_limit = min_t(unsigned int,
    175 				pasid_limit,
    176 				kfd->doorbell_process_limit - 1);
    177 
    178 	err = amd_iommu_init_device(kfd->pdev, pasid_limit);
    179 	if (err < 0) {
    180 		dev_err(kfd_device, "error initializing iommu device\n");
    181 		return false;
    182 	}
    183 
    184 	if (!kfd_set_pasid_limit(pasid_limit)) {
    185 		dev_err(kfd_device, "error setting pasid limit\n");
    186 		amd_iommu_free_device(kfd->pdev);
    187 		return false;
    188 	}
    189 
    190 	return true;
    191 }
    192 
    193 static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
    194 {
    195 	struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
    196 
    197 	if (dev)
    198 		kfd_unbind_process_from_device(dev, pasid);
    199 }
    200 
    201 /*
    202  * This function called by IOMMU driver on PPR failure
    203  */
    204 static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
    205 		unsigned long address, u16 flags)
    206 {
    207 	struct kfd_dev *dev;
    208 
    209 	dev_warn(kfd_device,
    210 			"Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
    211 			PCI_BUS_NUM(pdev->devfn),
    212 			PCI_SLOT(pdev->devfn),
    213 			PCI_FUNC(pdev->devfn),
    214 			pasid,
    215 			address,
    216 			flags);
    217 
    218 	dev = kfd_device_by_pci_dev(pdev);
    219 	BUG_ON(dev == NULL);
    220 
    221 	kfd_signal_iommu_event(dev, pasid, address,
    222 			flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
    223 
    224 	return AMD_IOMMU_INV_PRI_RSP_INVALID;
    225 }
    226 
    227 bool kgd2kfd_device_init(struct kfd_dev *kfd,
    228 			 const struct kgd2kfd_shared_resources *gpu_resources)
    229 {
    230 	unsigned int size;
    231 
    232 	kfd->shared_resources = *gpu_resources;
    233 
    234 	/* calculate max size of mqds needed for queues */
    235 	size = max_num_of_queues_per_device *
    236 			kfd->device_info->mqd_size_aligned;
    237 
    238 	/*
    239 	 * calculate max size of runlist packet.
    240 	 * There can be only 2 packets at once
    241 	 */
    242 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_map_process) +
    243 		max_num_of_queues_per_device *
    244 		sizeof(struct pm4_map_queues) + sizeof(struct pm4_runlist)) * 2;
    245 
    246 	/* Add size of HIQ & DIQ */
    247 	size += KFD_KERNEL_QUEUE_SIZE * 2;
    248 
    249 	/* add another 512KB for all other allocations on gart (HPD, fences) */
    250 	size += 512 * 1024;
    251 
    252 	if (kfd->kfd2kgd->init_gtt_mem_allocation(
    253 			kfd->kgd, size, &kfd->gtt_mem,
    254 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
    255 		dev_err(kfd_device,
    256 			"Could not allocate %d bytes for device (%x:%x)\n",
    257 			size, kfd->pdev->vendor, kfd->pdev->device);
    258 		goto out;
    259 	}
    260 
    261 	dev_info(kfd_device,
    262 		"Allocated %d bytes on gart for device(%x:%x)\n",
    263 		size, kfd->pdev->vendor, kfd->pdev->device);
    264 
    265 	/* Initialize GTT sa with 512 byte chunk size */
    266 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
    267 		dev_err(kfd_device,
    268 			"Error initializing gtt sub-allocator\n");
    269 		goto kfd_gtt_sa_init_error;
    270 	}
    271 
    272 	kfd_doorbell_init(kfd);
    273 
    274 	if (kfd_topology_add_device(kfd) != 0) {
    275 		dev_err(kfd_device,
    276 			"Error adding device (%x:%x) to topology\n",
    277 			kfd->pdev->vendor, kfd->pdev->device);
    278 		goto kfd_topology_add_device_error;
    279 	}
    280 
    281 	if (kfd_interrupt_init(kfd)) {
    282 		dev_err(kfd_device,
    283 			"Error initializing interrupts for device (%x:%x)\n",
    284 			kfd->pdev->vendor, kfd->pdev->device);
    285 		goto kfd_interrupt_error;
    286 	}
    287 
    288 	if (!device_iommu_pasid_init(kfd)) {
    289 		dev_err(kfd_device,
    290 			"Error initializing iommuv2 for device (%x:%x)\n",
    291 			kfd->pdev->vendor, kfd->pdev->device);
    292 		goto device_iommu_pasid_error;
    293 	}
    294 	amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
    295 						iommu_pasid_shutdown_callback);
    296 	amd_iommu_set_invalid_ppr_cb(kfd->pdev, iommu_invalid_ppr_cb);
    297 
    298 	kfd->dqm = device_queue_manager_init(kfd);
    299 	if (!kfd->dqm) {
    300 		dev_err(kfd_device,
    301 			"Error initializing queue manager for device (%x:%x)\n",
    302 			kfd->pdev->vendor, kfd->pdev->device);
    303 		goto device_queue_manager_error;
    304 	}
    305 
    306 	if (kfd->dqm->ops.start(kfd->dqm) != 0) {
    307 		dev_err(kfd_device,
    308 			"Error starting queuen manager for device (%x:%x)\n",
    309 			kfd->pdev->vendor, kfd->pdev->device);
    310 		goto dqm_start_error;
    311 	}
    312 
    313 	kfd->dbgmgr = NULL;
    314 
    315 	kfd->init_complete = true;
    316 	dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor,
    317 		 kfd->pdev->device);
    318 
    319 	pr_debug("kfd: Starting kfd with the following scheduling policy %d\n",
    320 		sched_policy);
    321 
    322 	goto out;
    323 
    324 dqm_start_error:
    325 	device_queue_manager_uninit(kfd->dqm);
    326 device_queue_manager_error:
    327 	amd_iommu_free_device(kfd->pdev);
    328 device_iommu_pasid_error:
    329 	kfd_interrupt_exit(kfd);
    330 kfd_interrupt_error:
    331 	kfd_topology_remove_device(kfd);
    332 kfd_topology_add_device_error:
    333 	kfd_gtt_sa_fini(kfd);
    334 kfd_gtt_sa_init_error:
    335 	kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
    336 	dev_err(kfd_device,
    337 		"device (%x:%x) NOT added due to errors\n",
    338 		kfd->pdev->vendor, kfd->pdev->device);
    339 out:
    340 	return kfd->init_complete;
    341 }
    342 
    343 void kgd2kfd_device_exit(struct kfd_dev *kfd)
    344 {
    345 	if (kfd->init_complete) {
    346 		device_queue_manager_uninit(kfd->dqm);
    347 		amd_iommu_free_device(kfd->pdev);
    348 		kfd_interrupt_exit(kfd);
    349 		kfd_topology_remove_device(kfd);
    350 		kfd_gtt_sa_fini(kfd);
    351 		kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
    352 	}
    353 
    354 	kfree(kfd);
    355 }
    356 
    357 void kgd2kfd_suspend(struct kfd_dev *kfd)
    358 {
    359 	BUG_ON(kfd == NULL);
    360 
    361 	if (kfd->init_complete) {
    362 		kfd->dqm->ops.stop(kfd->dqm);
    363 		amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
    364 		amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
    365 		amd_iommu_free_device(kfd->pdev);
    366 	}
    367 }
    368 
    369 int kgd2kfd_resume(struct kfd_dev *kfd)
    370 {
    371 	unsigned int pasid_limit;
    372 	int err;
    373 
    374 	BUG_ON(kfd == NULL);
    375 
    376 	pasid_limit = kfd_get_pasid_limit();
    377 
    378 	if (kfd->init_complete) {
    379 		err = amd_iommu_init_device(kfd->pdev, pasid_limit);
    380 		if (err < 0)
    381 			return -ENXIO;
    382 		amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
    383 						iommu_pasid_shutdown_callback);
    384 		amd_iommu_set_invalid_ppr_cb(kfd->pdev, iommu_invalid_ppr_cb);
    385 		kfd->dqm->ops.start(kfd->dqm);
    386 	}
    387 
    388 	return 0;
    389 }
    390 
    391 /* This is called directly from KGD at ISR. */
    392 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
    393 {
    394 	if (!kfd->init_complete)
    395 		return;
    396 
    397 	spin_lock(&kfd->interrupt_lock);
    398 
    399 	if (kfd->interrupts_active
    400 	    && interrupt_is_wanted(kfd, ih_ring_entry)
    401 	    && enqueue_ih_ring_entry(kfd, ih_ring_entry))
    402 		schedule_work(&kfd->interrupt_work);
    403 
    404 	spin_unlock(&kfd->interrupt_lock);
    405 }
    406 
    407 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
    408 				unsigned int chunk_size)
    409 {
    410 	unsigned int num_of_bits;
    411 
    412 	BUG_ON(!kfd);
    413 	BUG_ON(!kfd->gtt_mem);
    414 	BUG_ON(buf_size < chunk_size);
    415 	BUG_ON(buf_size == 0);
    416 	BUG_ON(chunk_size == 0);
    417 
    418 	kfd->gtt_sa_chunk_size = chunk_size;
    419 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
    420 
    421 	num_of_bits = kfd->gtt_sa_num_of_chunks / BITS_PER_BYTE;
    422 	BUG_ON(num_of_bits == 0);
    423 
    424 	kfd->gtt_sa_bitmap = kzalloc(num_of_bits, GFP_KERNEL);
    425 
    426 	if (!kfd->gtt_sa_bitmap)
    427 		return -ENOMEM;
    428 
    429 	pr_debug("kfd: gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
    430 			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 
    438 static 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 
    444 static 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 
    451 static 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 
    458 int 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 
    463 	BUG_ON(!kfd);
    464 
    465 	if (size == 0)
    466 		return -EINVAL;
    467 
    468 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
    469 		return -ENOMEM;
    470 
    471 	*mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
    472 	if ((*mem_obj) == NULL)
    473 		return -ENOMEM;
    474 
    475 	pr_debug("kfd: allocated mem_obj = %p for size = %d\n", *mem_obj, size);
    476 
    477 	start_search = 0;
    478 
    479 	mutex_lock(&kfd->gtt_sa_lock);
    480 
    481 kfd_gtt_restart_search:
    482 	/* Find the first chunk that is free */
    483 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
    484 					kfd->gtt_sa_num_of_chunks,
    485 					start_search);
    486 
    487 	pr_debug("kfd: found = %d\n", found);
    488 
    489 	/* If there wasn't any free chunk, bail out */
    490 	if (found == kfd->gtt_sa_num_of_chunks)
    491 		goto kfd_gtt_no_free_chunk;
    492 
    493 	/* Update fields of mem_obj */
    494 	(*mem_obj)->range_start = found;
    495 	(*mem_obj)->range_end = found;
    496 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
    497 					kfd->gtt_start_gpu_addr,
    498 					found,
    499 					kfd->gtt_sa_chunk_size);
    500 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
    501 					kfd->gtt_start_cpu_ptr,
    502 					found,
    503 					kfd->gtt_sa_chunk_size);
    504 
    505 	pr_debug("kfd: gpu_addr = %p, cpu_addr = %p\n",
    506 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
    507 
    508 	/* If we need only one chunk, mark it as allocated and get out */
    509 	if (size <= kfd->gtt_sa_chunk_size) {
    510 		pr_debug("kfd: single bit\n");
    511 		set_bit(found, kfd->gtt_sa_bitmap);
    512 		goto kfd_gtt_out;
    513 	}
    514 
    515 	/* Otherwise, try to see if we have enough contiguous chunks */
    516 	cur_size = size - kfd->gtt_sa_chunk_size;
    517 	do {
    518 		(*mem_obj)->range_end =
    519 			find_next_zero_bit(kfd->gtt_sa_bitmap,
    520 					kfd->gtt_sa_num_of_chunks, ++found);
    521 		/*
    522 		 * If next free chunk is not contiguous than we need to
    523 		 * restart our search from the last free chunk we found (which
    524 		 * wasn't contiguous to the previous ones
    525 		 */
    526 		if ((*mem_obj)->range_end != found) {
    527 			start_search = found;
    528 			goto kfd_gtt_restart_search;
    529 		}
    530 
    531 		/*
    532 		 * If we reached end of buffer, bail out with error
    533 		 */
    534 		if (found == kfd->gtt_sa_num_of_chunks)
    535 			goto kfd_gtt_no_free_chunk;
    536 
    537 		/* Check if we don't need another chunk */
    538 		if (cur_size <= kfd->gtt_sa_chunk_size)
    539 			cur_size = 0;
    540 		else
    541 			cur_size -= kfd->gtt_sa_chunk_size;
    542 
    543 	} while (cur_size > 0);
    544 
    545 	pr_debug("kfd: range_start = %d, range_end = %d\n",
    546 		(*mem_obj)->range_start, (*mem_obj)->range_end);
    547 
    548 	/* Mark the chunks as allocated */
    549 	for (found = (*mem_obj)->range_start;
    550 		found <= (*mem_obj)->range_end;
    551 		found++)
    552 		set_bit(found, kfd->gtt_sa_bitmap);
    553 
    554 kfd_gtt_out:
    555 	mutex_unlock(&kfd->gtt_sa_lock);
    556 	return 0;
    557 
    558 kfd_gtt_no_free_chunk:
    559 	pr_debug("kfd: allocation failed with mem_obj = %p\n", mem_obj);
    560 	mutex_unlock(&kfd->gtt_sa_lock);
    561 	kfree(mem_obj);
    562 	return -ENOMEM;
    563 }
    564 
    565 int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
    566 {
    567 	unsigned int bit;
    568 
    569 	BUG_ON(!kfd);
    570 
    571 	/* Act like kfree when trying to free a NULL object */
    572 	if (!mem_obj)
    573 		return 0;
    574 
    575 	pr_debug("kfd: free mem_obj = %p, range_start = %d, range_end = %d\n",
    576 			mem_obj, mem_obj->range_start, mem_obj->range_end);
    577 
    578 	mutex_lock(&kfd->gtt_sa_lock);
    579 
    580 	/* Mark the chunks as free */
    581 	for (bit = mem_obj->range_start;
    582 		bit <= mem_obj->range_end;
    583 		bit++)
    584 		clear_bit(bit, kfd->gtt_sa_bitmap);
    585 
    586 	mutex_unlock(&kfd->gtt_sa_lock);
    587 
    588 	kfree(mem_obj);
    589 	return 0;
    590 }
    591