kfd_device.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  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. #include <linux/amd-iommu.h>
  23. #include <linux/bsearch.h>
  24. #include <linux/pci.h>
  25. #include <linux/slab.h>
  26. #include "kfd_priv.h"
  27. #include "kfd_device_queue_manager.h"
  28. #include "kfd_pm4_headers_vi.h"
  29. #define MQD_SIZE_ALIGNED 768
  30. static const struct kfd_device_info kaveri_device_info = {
  31. .asic_family = CHIP_KAVERI,
  32. .max_pasid_bits = 16,
  33. /* max num of queues for KV.TODO should be a dynamic value */
  34. .max_no_of_hqd = 24,
  35. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  36. .event_interrupt_class = &event_interrupt_class_cik,
  37. .num_of_watch_points = 4,
  38. .mqd_size_aligned = MQD_SIZE_ALIGNED
  39. };
  40. static const struct kfd_device_info carrizo_device_info = {
  41. .asic_family = CHIP_CARRIZO,
  42. .max_pasid_bits = 16,
  43. /* max num of queues for CZ.TODO should be a dynamic value */
  44. .max_no_of_hqd = 24,
  45. .ih_ring_entry_size = 4 * sizeof(uint32_t),
  46. .event_interrupt_class = &event_interrupt_class_cik,
  47. .num_of_watch_points = 4,
  48. .mqd_size_aligned = MQD_SIZE_ALIGNED
  49. };
  50. struct kfd_deviceid {
  51. unsigned short did;
  52. const struct kfd_device_info *device_info;
  53. };
  54. /* Please keep this sorted by increasing device id. */
  55. static 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 */
  77. { 0x131D, &kaveri_device_info }, /* Kaveri */
  78. { 0x9870, &carrizo_device_info }, /* Carrizo */
  79. { 0x9874, &carrizo_device_info }, /* Carrizo */
  80. { 0x9875, &carrizo_device_info }, /* Carrizo */
  81. { 0x9876, &carrizo_device_info }, /* Carrizo */
  82. { 0x9877, &carrizo_device_info } /* Carrizo */
  83. };
  84. static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
  85. unsigned int chunk_size);
  86. static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
  87. static int kfd_resume(struct kfd_dev *kfd);
  88. static const struct kfd_device_info *lookup_device_info(unsigned short did)
  89. {
  90. size_t i;
  91. for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
  92. if (supported_devices[i].did == did) {
  93. WARN_ON(!supported_devices[i].device_info);
  94. return supported_devices[i].device_info;
  95. }
  96. }
  97. dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
  98. did);
  99. return NULL;
  100. }
  101. struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
  102. struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
  103. {
  104. struct kfd_dev *kfd;
  105. const struct kfd_device_info *device_info =
  106. lookup_device_info(pdev->device);
  107. if (!device_info) {
  108. dev_err(kfd_device, "kgd2kfd_probe failed\n");
  109. return NULL;
  110. }
  111. kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
  112. if (!kfd)
  113. return NULL;
  114. kfd->kgd = kgd;
  115. kfd->device_info = device_info;
  116. kfd->pdev = pdev;
  117. kfd->init_complete = false;
  118. kfd->kfd2kgd = f2g;
  119. mutex_init(&kfd->doorbell_mutex);
  120. memset(&kfd->doorbell_available_index, 0,
  121. sizeof(kfd->doorbell_available_index));
  122. return kfd;
  123. }
  124. static bool device_iommu_pasid_init(struct kfd_dev *kfd)
  125. {
  126. const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
  127. AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
  128. AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
  129. struct amd_iommu_device_info iommu_info;
  130. unsigned int pasid_limit;
  131. int err;
  132. err = amd_iommu_device_info(kfd->pdev, &iommu_info);
  133. if (err < 0) {
  134. dev_err(kfd_device,
  135. "error getting iommu info. is the iommu enabled?\n");
  136. return false;
  137. }
  138. if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
  139. dev_err(kfd_device, "error required iommu flags ats %i, pri %i, pasid %i\n",
  140. (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
  141. (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
  142. (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP)
  143. != 0);
  144. return false;
  145. }
  146. pasid_limit = min_t(unsigned int,
  147. (unsigned int)(1 << kfd->device_info->max_pasid_bits),
  148. iommu_info.max_pasids);
  149. if (!kfd_set_pasid_limit(pasid_limit)) {
  150. dev_err(kfd_device, "error setting pasid limit\n");
  151. return false;
  152. }
  153. return true;
  154. }
  155. static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
  156. {
  157. struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
  158. if (dev)
  159. kfd_process_iommu_unbind_callback(dev, pasid);
  160. }
  161. /*
  162. * This function called by IOMMU driver on PPR failure
  163. */
  164. static int iommu_invalid_ppr_cb(struct pci_dev *pdev, int pasid,
  165. unsigned long address, u16 flags)
  166. {
  167. struct kfd_dev *dev;
  168. dev_warn(kfd_device,
  169. "Invalid PPR device %x:%x.%x pasid %d address 0x%lX flags 0x%X",
  170. PCI_BUS_NUM(pdev->devfn),
  171. PCI_SLOT(pdev->devfn),
  172. PCI_FUNC(pdev->devfn),
  173. pasid,
  174. address,
  175. flags);
  176. dev = kfd_device_by_pci_dev(pdev);
  177. if (!WARN_ON(!dev))
  178. kfd_signal_iommu_event(dev, pasid, address,
  179. flags & PPR_FAULT_WRITE, flags & PPR_FAULT_EXEC);
  180. return AMD_IOMMU_INV_PRI_RSP_INVALID;
  181. }
  182. bool kgd2kfd_device_init(struct kfd_dev *kfd,
  183. const struct kgd2kfd_shared_resources *gpu_resources)
  184. {
  185. unsigned int size;
  186. kfd->shared_resources = *gpu_resources;
  187. kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
  188. kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
  189. kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
  190. - kfd->vm_info.first_vmid_kfd + 1;
  191. /* calculate max size of mqds needed for queues */
  192. size = max_num_of_queues_per_device *
  193. kfd->device_info->mqd_size_aligned;
  194. /*
  195. * calculate max size of runlist packet.
  196. * There can be only 2 packets at once
  197. */
  198. size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
  199. max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
  200. + sizeof(struct pm4_mes_runlist)) * 2;
  201. /* Add size of HIQ & DIQ */
  202. size += KFD_KERNEL_QUEUE_SIZE * 2;
  203. /* add another 512KB for all other allocations on gart (HPD, fences) */
  204. size += 512 * 1024;
  205. if (kfd->kfd2kgd->init_gtt_mem_allocation(
  206. kfd->kgd, size, &kfd->gtt_mem,
  207. &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
  208. dev_err(kfd_device, "Could not allocate %d bytes\n", size);
  209. goto out;
  210. }
  211. dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
  212. /* Initialize GTT sa with 512 byte chunk size */
  213. if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
  214. dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
  215. goto kfd_gtt_sa_init_error;
  216. }
  217. if (kfd_doorbell_init(kfd)) {
  218. dev_err(kfd_device,
  219. "Error initializing doorbell aperture\n");
  220. goto kfd_doorbell_error;
  221. }
  222. if (kfd_topology_add_device(kfd)) {
  223. dev_err(kfd_device, "Error adding device to topology\n");
  224. goto kfd_topology_add_device_error;
  225. }
  226. if (kfd_interrupt_init(kfd)) {
  227. dev_err(kfd_device, "Error initializing interrupts\n");
  228. goto kfd_interrupt_error;
  229. }
  230. kfd->dqm = device_queue_manager_init(kfd);
  231. if (!kfd->dqm) {
  232. dev_err(kfd_device, "Error initializing queue manager\n");
  233. goto device_queue_manager_error;
  234. }
  235. if (!device_iommu_pasid_init(kfd)) {
  236. dev_err(kfd_device,
  237. "Error initializing iommuv2 for device %x:%x\n",
  238. kfd->pdev->vendor, kfd->pdev->device);
  239. goto device_iommu_pasid_error;
  240. }
  241. if (kfd_resume(kfd))
  242. goto kfd_resume_error;
  243. kfd->dbgmgr = NULL;
  244. kfd->init_complete = true;
  245. dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
  246. kfd->pdev->device);
  247. pr_debug("Starting kfd with the following scheduling policy %d\n",
  248. sched_policy);
  249. goto out;
  250. kfd_resume_error:
  251. device_iommu_pasid_error:
  252. device_queue_manager_uninit(kfd->dqm);
  253. device_queue_manager_error:
  254. kfd_interrupt_exit(kfd);
  255. kfd_interrupt_error:
  256. kfd_topology_remove_device(kfd);
  257. kfd_topology_add_device_error:
  258. kfd_doorbell_fini(kfd);
  259. kfd_doorbell_error:
  260. kfd_gtt_sa_fini(kfd);
  261. kfd_gtt_sa_init_error:
  262. kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
  263. dev_err(kfd_device,
  264. "device %x:%x NOT added due to errors\n",
  265. kfd->pdev->vendor, kfd->pdev->device);
  266. out:
  267. return kfd->init_complete;
  268. }
  269. void kgd2kfd_device_exit(struct kfd_dev *kfd)
  270. {
  271. if (kfd->init_complete) {
  272. kgd2kfd_suspend(kfd);
  273. device_queue_manager_uninit(kfd->dqm);
  274. kfd_interrupt_exit(kfd);
  275. kfd_topology_remove_device(kfd);
  276. kfd_doorbell_fini(kfd);
  277. kfd_gtt_sa_fini(kfd);
  278. kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
  279. }
  280. kfree(kfd);
  281. }
  282. void kgd2kfd_suspend(struct kfd_dev *kfd)
  283. {
  284. if (!kfd->init_complete)
  285. return;
  286. kfd->dqm->ops.stop(kfd->dqm);
  287. kfd_unbind_processes_from_device(kfd);
  288. amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
  289. amd_iommu_set_invalid_ppr_cb(kfd->pdev, NULL);
  290. amd_iommu_free_device(kfd->pdev);
  291. }
  292. int kgd2kfd_resume(struct kfd_dev *kfd)
  293. {
  294. if (!kfd->init_complete)
  295. return 0;
  296. return kfd_resume(kfd);
  297. }
  298. static int kfd_resume(struct kfd_dev *kfd)
  299. {
  300. int err = 0;
  301. unsigned int pasid_limit = kfd_get_pasid_limit();
  302. err = amd_iommu_init_device(kfd->pdev, pasid_limit);
  303. if (err)
  304. return -ENXIO;
  305. amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
  306. iommu_pasid_shutdown_callback);
  307. amd_iommu_set_invalid_ppr_cb(kfd->pdev,
  308. iommu_invalid_ppr_cb);
  309. err = kfd_bind_processes_to_device(kfd);
  310. if (err)
  311. goto processes_bind_error;
  312. err = kfd->dqm->ops.start(kfd->dqm);
  313. if (err) {
  314. dev_err(kfd_device,
  315. "Error starting queue manager for device %x:%x\n",
  316. kfd->pdev->vendor, kfd->pdev->device);
  317. goto dqm_start_error;
  318. }
  319. return err;
  320. dqm_start_error:
  321. processes_bind_error:
  322. amd_iommu_free_device(kfd->pdev);
  323. return err;
  324. }
  325. /* This is called directly from KGD at ISR. */
  326. void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
  327. {
  328. if (!kfd->init_complete)
  329. return;
  330. spin_lock(&kfd->interrupt_lock);
  331. if (kfd->interrupts_active
  332. && interrupt_is_wanted(kfd, ih_ring_entry)
  333. && enqueue_ih_ring_entry(kfd, ih_ring_entry))
  334. queue_work(kfd->ih_wq, &kfd->interrupt_work);
  335. spin_unlock(&kfd->interrupt_lock);
  336. }
  337. static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
  338. unsigned int chunk_size)
  339. {
  340. unsigned int num_of_longs;
  341. if (WARN_ON(buf_size < chunk_size))
  342. return -EINVAL;
  343. if (WARN_ON(buf_size == 0))
  344. return -EINVAL;
  345. if (WARN_ON(chunk_size == 0))
  346. return -EINVAL;
  347. kfd->gtt_sa_chunk_size = chunk_size;
  348. kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
  349. num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
  350. BITS_PER_LONG;
  351. kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
  352. if (!kfd->gtt_sa_bitmap)
  353. return -ENOMEM;
  354. pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
  355. kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
  356. mutex_init(&kfd->gtt_sa_lock);
  357. return 0;
  358. }
  359. static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
  360. {
  361. mutex_destroy(&kfd->gtt_sa_lock);
  362. kfree(kfd->gtt_sa_bitmap);
  363. }
  364. static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
  365. unsigned int bit_num,
  366. unsigned int chunk_size)
  367. {
  368. return start_addr + bit_num * chunk_size;
  369. }
  370. static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
  371. unsigned int bit_num,
  372. unsigned int chunk_size)
  373. {
  374. return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
  375. }
  376. int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
  377. struct kfd_mem_obj **mem_obj)
  378. {
  379. unsigned int found, start_search, cur_size;
  380. if (size == 0)
  381. return -EINVAL;
  382. if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
  383. return -ENOMEM;
  384. *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
  385. if ((*mem_obj) == NULL)
  386. return -ENOMEM;
  387. pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
  388. start_search = 0;
  389. mutex_lock(&kfd->gtt_sa_lock);
  390. kfd_gtt_restart_search:
  391. /* Find the first chunk that is free */
  392. found = find_next_zero_bit(kfd->gtt_sa_bitmap,
  393. kfd->gtt_sa_num_of_chunks,
  394. start_search);
  395. pr_debug("Found = %d\n", found);
  396. /* If there wasn't any free chunk, bail out */
  397. if (found == kfd->gtt_sa_num_of_chunks)
  398. goto kfd_gtt_no_free_chunk;
  399. /* Update fields of mem_obj */
  400. (*mem_obj)->range_start = found;
  401. (*mem_obj)->range_end = found;
  402. (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
  403. kfd->gtt_start_gpu_addr,
  404. found,
  405. kfd->gtt_sa_chunk_size);
  406. (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
  407. kfd->gtt_start_cpu_ptr,
  408. found,
  409. kfd->gtt_sa_chunk_size);
  410. pr_debug("gpu_addr = %p, cpu_addr = %p\n",
  411. (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
  412. /* If we need only one chunk, mark it as allocated and get out */
  413. if (size <= kfd->gtt_sa_chunk_size) {
  414. pr_debug("Single bit\n");
  415. set_bit(found, kfd->gtt_sa_bitmap);
  416. goto kfd_gtt_out;
  417. }
  418. /* Otherwise, try to see if we have enough contiguous chunks */
  419. cur_size = size - kfd->gtt_sa_chunk_size;
  420. do {
  421. (*mem_obj)->range_end =
  422. find_next_zero_bit(kfd->gtt_sa_bitmap,
  423. kfd->gtt_sa_num_of_chunks, ++found);
  424. /*
  425. * If next free chunk is not contiguous than we need to
  426. * restart our search from the last free chunk we found (which
  427. * wasn't contiguous to the previous ones
  428. */
  429. if ((*mem_obj)->range_end != found) {
  430. start_search = found;
  431. goto kfd_gtt_restart_search;
  432. }
  433. /*
  434. * If we reached end of buffer, bail out with error
  435. */
  436. if (found == kfd->gtt_sa_num_of_chunks)
  437. goto kfd_gtt_no_free_chunk;
  438. /* Check if we don't need another chunk */
  439. if (cur_size <= kfd->gtt_sa_chunk_size)
  440. cur_size = 0;
  441. else
  442. cur_size -= kfd->gtt_sa_chunk_size;
  443. } while (cur_size > 0);
  444. pr_debug("range_start = %d, range_end = %d\n",
  445. (*mem_obj)->range_start, (*mem_obj)->range_end);
  446. /* Mark the chunks as allocated */
  447. for (found = (*mem_obj)->range_start;
  448. found <= (*mem_obj)->range_end;
  449. found++)
  450. set_bit(found, kfd->gtt_sa_bitmap);
  451. kfd_gtt_out:
  452. mutex_unlock(&kfd->gtt_sa_lock);
  453. return 0;
  454. kfd_gtt_no_free_chunk:
  455. pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
  456. mutex_unlock(&kfd->gtt_sa_lock);
  457. kfree(mem_obj);
  458. return -ENOMEM;
  459. }
  460. int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
  461. {
  462. unsigned int bit;
  463. /* Act like kfree when trying to free a NULL object */
  464. if (!mem_obj)
  465. return 0;
  466. pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
  467. mem_obj, mem_obj->range_start, mem_obj->range_end);
  468. mutex_lock(&kfd->gtt_sa_lock);
  469. /* Mark the chunks as free */
  470. for (bit = mem_obj->range_start;
  471. bit <= mem_obj->range_end;
  472. bit++)
  473. clear_bit(bit, kfd->gtt_sa_bitmap);
  474. mutex_unlock(&kfd->gtt_sa_lock);
  475. kfree(mem_obj);
  476. return 0;
  477. }