kfd_device.c 17 KB

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