kfd_device.c 16 KB

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