kfd_process.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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/mutex.h>
  23. #include <linux/log2.h>
  24. #include <linux/sched.h>
  25. #include <linux/sched/mm.h>
  26. #include <linux/sched/task.h>
  27. #include <linux/slab.h>
  28. #include <linux/amd-iommu.h>
  29. #include <linux/notifier.h>
  30. #include <linux/compat.h>
  31. #include <linux/mman.h>
  32. #include <linux/file.h>
  33. struct mm_struct;
  34. #include "kfd_priv.h"
  35. #include "kfd_device_queue_manager.h"
  36. #include "kfd_dbgmgr.h"
  37. #include "kfd_iommu.h"
  38. /*
  39. * List of struct kfd_process (field kfd_process).
  40. * Unique/indexed by mm_struct*
  41. */
  42. DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
  43. static DEFINE_MUTEX(kfd_processes_mutex);
  44. DEFINE_SRCU(kfd_processes_srcu);
  45. /* For process termination handling */
  46. static struct workqueue_struct *kfd_process_wq;
  47. /* Ordered, single-threaded workqueue for restoring evicted
  48. * processes. Restoring multiple processes concurrently under memory
  49. * pressure can lead to processes blocking each other from validating
  50. * their BOs and result in a live-lock situation where processes
  51. * remain evicted indefinitely.
  52. */
  53. static struct workqueue_struct *kfd_restore_wq;
  54. static struct kfd_process *find_process(const struct task_struct *thread);
  55. static void kfd_process_ref_release(struct kref *ref);
  56. static struct kfd_process *create_process(const struct task_struct *thread,
  57. struct file *filep);
  58. static void evict_process_worker(struct work_struct *work);
  59. static void restore_process_worker(struct work_struct *work);
  60. int kfd_process_create_wq(void)
  61. {
  62. if (!kfd_process_wq)
  63. kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
  64. if (!kfd_restore_wq)
  65. kfd_restore_wq = alloc_ordered_workqueue("kfd_restore_wq", 0);
  66. if (!kfd_process_wq || !kfd_restore_wq) {
  67. kfd_process_destroy_wq();
  68. return -ENOMEM;
  69. }
  70. return 0;
  71. }
  72. void kfd_process_destroy_wq(void)
  73. {
  74. if (kfd_process_wq) {
  75. destroy_workqueue(kfd_process_wq);
  76. kfd_process_wq = NULL;
  77. }
  78. if (kfd_restore_wq) {
  79. destroy_workqueue(kfd_restore_wq);
  80. kfd_restore_wq = NULL;
  81. }
  82. }
  83. static void kfd_process_free_gpuvm(struct kgd_mem *mem,
  84. struct kfd_process_device *pdd)
  85. {
  86. struct kfd_dev *dev = pdd->dev;
  87. dev->kfd2kgd->unmap_memory_to_gpu(dev->kgd, mem, pdd->vm);
  88. dev->kfd2kgd->free_memory_of_gpu(dev->kgd, mem);
  89. }
  90. /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process
  91. * This function should be only called right after the process
  92. * is created and when kfd_processes_mutex is still being held
  93. * to avoid concurrency. Because of that exclusiveness, we do
  94. * not need to take p->mutex.
  95. */
  96. static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd,
  97. uint64_t gpu_va, uint32_t size,
  98. uint32_t flags, void **kptr)
  99. {
  100. struct kfd_dev *kdev = pdd->dev;
  101. struct kgd_mem *mem = NULL;
  102. int handle;
  103. int err;
  104. err = kdev->kfd2kgd->alloc_memory_of_gpu(kdev->kgd, gpu_va, size,
  105. pdd->vm, &mem, NULL, flags);
  106. if (err)
  107. goto err_alloc_mem;
  108. err = kdev->kfd2kgd->map_memory_to_gpu(kdev->kgd, mem, pdd->vm);
  109. if (err)
  110. goto err_map_mem;
  111. err = kdev->kfd2kgd->sync_memory(kdev->kgd, mem, true);
  112. if (err) {
  113. pr_debug("Sync memory failed, wait interrupted by user signal\n");
  114. goto sync_memory_failed;
  115. }
  116. /* Create an obj handle so kfd_process_device_remove_obj_handle
  117. * will take care of the bo removal when the process finishes.
  118. * We do not need to take p->mutex, because the process is just
  119. * created and the ioctls have not had the chance to run.
  120. */
  121. handle = kfd_process_device_create_obj_handle(pdd, mem);
  122. if (handle < 0) {
  123. err = handle;
  124. goto free_gpuvm;
  125. }
  126. if (kptr) {
  127. err = kdev->kfd2kgd->map_gtt_bo_to_kernel(kdev->kgd,
  128. (struct kgd_mem *)mem, kptr, NULL);
  129. if (err) {
  130. pr_debug("Map GTT BO to kernel failed\n");
  131. goto free_obj_handle;
  132. }
  133. }
  134. return err;
  135. free_obj_handle:
  136. kfd_process_device_remove_obj_handle(pdd, handle);
  137. free_gpuvm:
  138. sync_memory_failed:
  139. kfd_process_free_gpuvm(mem, pdd);
  140. return err;
  141. err_map_mem:
  142. kdev->kfd2kgd->free_memory_of_gpu(kdev->kgd, mem);
  143. err_alloc_mem:
  144. *kptr = NULL;
  145. return err;
  146. }
  147. /* kfd_process_device_reserve_ib_mem - Reserve memory inside the
  148. * process for IB usage The memory reserved is for KFD to submit
  149. * IB to AMDGPU from kernel. If the memory is reserved
  150. * successfully, ib_kaddr will have the CPU/kernel
  151. * address. Check ib_kaddr before accessing the memory.
  152. */
  153. static int kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd)
  154. {
  155. struct qcm_process_device *qpd = &pdd->qpd;
  156. uint32_t flags = ALLOC_MEM_FLAGS_GTT |
  157. ALLOC_MEM_FLAGS_NO_SUBSTITUTE |
  158. ALLOC_MEM_FLAGS_WRITABLE |
  159. ALLOC_MEM_FLAGS_EXECUTABLE;
  160. void *kaddr;
  161. int ret;
  162. if (qpd->ib_kaddr || !qpd->ib_base)
  163. return 0;
  164. /* ib_base is only set for dGPU */
  165. ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
  166. &kaddr);
  167. if (ret)
  168. return ret;
  169. qpd->ib_kaddr = kaddr;
  170. return 0;
  171. }
  172. struct kfd_process *kfd_create_process(struct file *filep)
  173. {
  174. struct kfd_process *process;
  175. struct task_struct *thread = current;
  176. if (!thread->mm)
  177. return ERR_PTR(-EINVAL);
  178. /* Only the pthreads threading model is supported. */
  179. if (thread->group_leader->mm != thread->mm)
  180. return ERR_PTR(-EINVAL);
  181. /*
  182. * take kfd processes mutex before starting of process creation
  183. * so there won't be a case where two threads of the same process
  184. * create two kfd_process structures
  185. */
  186. mutex_lock(&kfd_processes_mutex);
  187. /* A prior open of /dev/kfd could have already created the process. */
  188. process = find_process(thread);
  189. if (process)
  190. pr_debug("Process already found\n");
  191. else
  192. process = create_process(thread, filep);
  193. mutex_unlock(&kfd_processes_mutex);
  194. return process;
  195. }
  196. struct kfd_process *kfd_get_process(const struct task_struct *thread)
  197. {
  198. struct kfd_process *process;
  199. if (!thread->mm)
  200. return ERR_PTR(-EINVAL);
  201. /* Only the pthreads threading model is supported. */
  202. if (thread->group_leader->mm != thread->mm)
  203. return ERR_PTR(-EINVAL);
  204. process = find_process(thread);
  205. return process;
  206. }
  207. static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
  208. {
  209. struct kfd_process *process;
  210. hash_for_each_possible_rcu(kfd_processes_table, process,
  211. kfd_processes, (uintptr_t)mm)
  212. if (process->mm == mm)
  213. return process;
  214. return NULL;
  215. }
  216. static struct kfd_process *find_process(const struct task_struct *thread)
  217. {
  218. struct kfd_process *p;
  219. int idx;
  220. idx = srcu_read_lock(&kfd_processes_srcu);
  221. p = find_process_by_mm(thread->mm);
  222. srcu_read_unlock(&kfd_processes_srcu, idx);
  223. return p;
  224. }
  225. void kfd_unref_process(struct kfd_process *p)
  226. {
  227. kref_put(&p->ref, kfd_process_ref_release);
  228. }
  229. static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
  230. {
  231. struct kfd_process *p = pdd->process;
  232. void *mem;
  233. int id;
  234. /*
  235. * Remove all handles from idr and release appropriate
  236. * local memory object
  237. */
  238. idr_for_each_entry(&pdd->alloc_idr, mem, id) {
  239. struct kfd_process_device *peer_pdd;
  240. list_for_each_entry(peer_pdd, &p->per_device_data,
  241. per_device_list) {
  242. if (!peer_pdd->vm)
  243. continue;
  244. peer_pdd->dev->kfd2kgd->unmap_memory_to_gpu(
  245. peer_pdd->dev->kgd, mem, peer_pdd->vm);
  246. }
  247. pdd->dev->kfd2kgd->free_memory_of_gpu(pdd->dev->kgd, mem);
  248. kfd_process_device_remove_obj_handle(pdd, id);
  249. }
  250. }
  251. static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
  252. {
  253. struct kfd_process_device *pdd;
  254. list_for_each_entry(pdd, &p->per_device_data, per_device_list)
  255. kfd_process_device_free_bos(pdd);
  256. }
  257. static void kfd_process_destroy_pdds(struct kfd_process *p)
  258. {
  259. struct kfd_process_device *pdd, *temp;
  260. list_for_each_entry_safe(pdd, temp, &p->per_device_data,
  261. per_device_list) {
  262. pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
  263. pdd->dev->id, p->pasid);
  264. if (pdd->drm_file)
  265. fput(pdd->drm_file);
  266. else if (pdd->vm)
  267. pdd->dev->kfd2kgd->destroy_process_vm(
  268. pdd->dev->kgd, pdd->vm);
  269. list_del(&pdd->per_device_list);
  270. if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base)
  271. free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
  272. get_order(KFD_CWSR_TBA_TMA_SIZE));
  273. kfree(pdd->qpd.doorbell_bitmap);
  274. idr_destroy(&pdd->alloc_idr);
  275. kfree(pdd);
  276. }
  277. }
  278. /* No process locking is needed in this function, because the process
  279. * is not findable any more. We must assume that no other thread is
  280. * using it any more, otherwise we couldn't safely free the process
  281. * structure in the end.
  282. */
  283. static void kfd_process_wq_release(struct work_struct *work)
  284. {
  285. struct kfd_process *p = container_of(work, struct kfd_process,
  286. release_work);
  287. kfd_iommu_unbind_process(p);
  288. kfd_process_free_outstanding_kfd_bos(p);
  289. kfd_process_destroy_pdds(p);
  290. dma_fence_put(p->ef);
  291. kfd_event_free_process(p);
  292. kfd_pasid_free(p->pasid);
  293. kfd_free_process_doorbells(p);
  294. mutex_destroy(&p->mutex);
  295. put_task_struct(p->lead_thread);
  296. kfree(p);
  297. }
  298. static void kfd_process_ref_release(struct kref *ref)
  299. {
  300. struct kfd_process *p = container_of(ref, struct kfd_process, ref);
  301. INIT_WORK(&p->release_work, kfd_process_wq_release);
  302. queue_work(kfd_process_wq, &p->release_work);
  303. }
  304. static void kfd_process_destroy_delayed(struct rcu_head *rcu)
  305. {
  306. struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
  307. kfd_unref_process(p);
  308. }
  309. static void kfd_process_notifier_release(struct mmu_notifier *mn,
  310. struct mm_struct *mm)
  311. {
  312. struct kfd_process *p;
  313. struct kfd_process_device *pdd = NULL;
  314. /*
  315. * The kfd_process structure can not be free because the
  316. * mmu_notifier srcu is read locked
  317. */
  318. p = container_of(mn, struct kfd_process, mmu_notifier);
  319. if (WARN_ON(p->mm != mm))
  320. return;
  321. mutex_lock(&kfd_processes_mutex);
  322. hash_del_rcu(&p->kfd_processes);
  323. mutex_unlock(&kfd_processes_mutex);
  324. synchronize_srcu(&kfd_processes_srcu);
  325. cancel_delayed_work_sync(&p->eviction_work);
  326. cancel_delayed_work_sync(&p->restore_work);
  327. mutex_lock(&p->mutex);
  328. /* Iterate over all process device data structures and if the
  329. * pdd is in debug mode, we should first force unregistration,
  330. * then we will be able to destroy the queues
  331. */
  332. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  333. struct kfd_dev *dev = pdd->dev;
  334. mutex_lock(kfd_get_dbgmgr_mutex());
  335. if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
  336. if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
  337. kfd_dbgmgr_destroy(dev->dbgmgr);
  338. dev->dbgmgr = NULL;
  339. }
  340. }
  341. mutex_unlock(kfd_get_dbgmgr_mutex());
  342. }
  343. kfd_process_dequeue_from_all_devices(p);
  344. pqm_uninit(&p->pqm);
  345. /* Indicate to other users that MM is no longer valid */
  346. p->mm = NULL;
  347. mutex_unlock(&p->mutex);
  348. mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
  349. mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
  350. }
  351. static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
  352. .release = kfd_process_notifier_release,
  353. };
  354. static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
  355. {
  356. unsigned long offset;
  357. struct kfd_process_device *pdd;
  358. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  359. struct kfd_dev *dev = pdd->dev;
  360. struct qcm_process_device *qpd = &pdd->qpd;
  361. if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base)
  362. continue;
  363. offset = (KFD_MMAP_TYPE_RESERVED_MEM | KFD_MMAP_GPU_ID(dev->id))
  364. << PAGE_SHIFT;
  365. qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
  366. KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
  367. MAP_SHARED, offset);
  368. if (IS_ERR_VALUE(qpd->tba_addr)) {
  369. int err = qpd->tba_addr;
  370. pr_err("Failure to set tba address. error %d.\n", err);
  371. qpd->tba_addr = 0;
  372. qpd->cwsr_kaddr = NULL;
  373. return err;
  374. }
  375. memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
  376. qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
  377. pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
  378. qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
  379. }
  380. return 0;
  381. }
  382. static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd)
  383. {
  384. struct kfd_dev *dev = pdd->dev;
  385. struct qcm_process_device *qpd = &pdd->qpd;
  386. uint32_t flags = ALLOC_MEM_FLAGS_GTT |
  387. ALLOC_MEM_FLAGS_NO_SUBSTITUTE | ALLOC_MEM_FLAGS_EXECUTABLE;
  388. void *kaddr;
  389. int ret;
  390. if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base)
  391. return 0;
  392. /* cwsr_base is only set for dGPU */
  393. ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
  394. KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);
  395. if (ret)
  396. return ret;
  397. qpd->cwsr_kaddr = kaddr;
  398. qpd->tba_addr = qpd->cwsr_base;
  399. memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
  400. qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
  401. pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
  402. qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
  403. return 0;
  404. }
  405. static struct kfd_process *create_process(const struct task_struct *thread,
  406. struct file *filep)
  407. {
  408. struct kfd_process *process;
  409. int err = -ENOMEM;
  410. process = kzalloc(sizeof(*process), GFP_KERNEL);
  411. if (!process)
  412. goto err_alloc_process;
  413. process->pasid = kfd_pasid_alloc();
  414. if (process->pasid == 0)
  415. goto err_alloc_pasid;
  416. if (kfd_alloc_process_doorbells(process) < 0)
  417. goto err_alloc_doorbells;
  418. kref_init(&process->ref);
  419. mutex_init(&process->mutex);
  420. process->mm = thread->mm;
  421. /* register notifier */
  422. process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
  423. err = mmu_notifier_register(&process->mmu_notifier, process->mm);
  424. if (err)
  425. goto err_mmu_notifier;
  426. hash_add_rcu(kfd_processes_table, &process->kfd_processes,
  427. (uintptr_t)process->mm);
  428. process->lead_thread = thread->group_leader;
  429. get_task_struct(process->lead_thread);
  430. INIT_LIST_HEAD(&process->per_device_data);
  431. kfd_event_init_process(process);
  432. err = pqm_init(&process->pqm, process);
  433. if (err != 0)
  434. goto err_process_pqm_init;
  435. /* init process apertures*/
  436. process->is_32bit_user_mode = in_compat_syscall();
  437. err = kfd_init_apertures(process);
  438. if (err != 0)
  439. goto err_init_apertures;
  440. INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
  441. INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
  442. process->last_restore_timestamp = get_jiffies_64();
  443. err = kfd_process_init_cwsr_apu(process, filep);
  444. if (err)
  445. goto err_init_cwsr;
  446. return process;
  447. err_init_cwsr:
  448. kfd_process_free_outstanding_kfd_bos(process);
  449. kfd_process_destroy_pdds(process);
  450. err_init_apertures:
  451. pqm_uninit(&process->pqm);
  452. err_process_pqm_init:
  453. hash_del_rcu(&process->kfd_processes);
  454. synchronize_rcu();
  455. mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
  456. err_mmu_notifier:
  457. mutex_destroy(&process->mutex);
  458. kfd_free_process_doorbells(process);
  459. err_alloc_doorbells:
  460. kfd_pasid_free(process->pasid);
  461. err_alloc_pasid:
  462. kfree(process);
  463. err_alloc_process:
  464. return ERR_PTR(err);
  465. }
  466. static int init_doorbell_bitmap(struct qcm_process_device *qpd,
  467. struct kfd_dev *dev)
  468. {
  469. unsigned int i;
  470. if (!KFD_IS_SOC15(dev->device_info->asic_family))
  471. return 0;
  472. qpd->doorbell_bitmap =
  473. kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
  474. BITS_PER_BYTE), GFP_KERNEL);
  475. if (!qpd->doorbell_bitmap)
  476. return -ENOMEM;
  477. /* Mask out any reserved doorbells */
  478. for (i = 0; i < KFD_MAX_NUM_OF_QUEUES_PER_PROCESS; i++)
  479. if ((dev->shared_resources.reserved_doorbell_mask & i) ==
  480. dev->shared_resources.reserved_doorbell_val) {
  481. set_bit(i, qpd->doorbell_bitmap);
  482. pr_debug("reserved doorbell 0x%03x\n", i);
  483. }
  484. return 0;
  485. }
  486. struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
  487. struct kfd_process *p)
  488. {
  489. struct kfd_process_device *pdd = NULL;
  490. list_for_each_entry(pdd, &p->per_device_data, per_device_list)
  491. if (pdd->dev == dev)
  492. return pdd;
  493. return NULL;
  494. }
  495. struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
  496. struct kfd_process *p)
  497. {
  498. struct kfd_process_device *pdd = NULL;
  499. pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
  500. if (!pdd)
  501. return NULL;
  502. if (init_doorbell_bitmap(&pdd->qpd, dev)) {
  503. pr_err("Failed to init doorbell for process\n");
  504. kfree(pdd);
  505. return NULL;
  506. }
  507. pdd->dev = dev;
  508. INIT_LIST_HEAD(&pdd->qpd.queues_list);
  509. INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
  510. pdd->qpd.dqm = dev->dqm;
  511. pdd->qpd.pqm = &p->pqm;
  512. pdd->qpd.evicted = 0;
  513. pdd->process = p;
  514. pdd->bound = PDD_UNBOUND;
  515. pdd->already_dequeued = false;
  516. list_add(&pdd->per_device_list, &p->per_device_data);
  517. /* Init idr used for memory handle translation */
  518. idr_init(&pdd->alloc_idr);
  519. return pdd;
  520. }
  521. /**
  522. * kfd_process_device_init_vm - Initialize a VM for a process-device
  523. *
  524. * @pdd: The process-device
  525. * @drm_file: Optional pointer to a DRM file descriptor
  526. *
  527. * If @drm_file is specified, it will be used to acquire the VM from
  528. * that file descriptor. If successful, the @pdd takes ownership of
  529. * the file descriptor.
  530. *
  531. * If @drm_file is NULL, a new VM is created.
  532. *
  533. * Returns 0 on success, -errno on failure.
  534. */
  535. int kfd_process_device_init_vm(struct kfd_process_device *pdd,
  536. struct file *drm_file)
  537. {
  538. struct kfd_process *p;
  539. struct kfd_dev *dev;
  540. int ret;
  541. if (pdd->vm)
  542. return drm_file ? -EBUSY : 0;
  543. p = pdd->process;
  544. dev = pdd->dev;
  545. if (drm_file)
  546. ret = dev->kfd2kgd->acquire_process_vm(
  547. dev->kgd, drm_file,
  548. &pdd->vm, &p->kgd_process_info, &p->ef);
  549. else
  550. ret = dev->kfd2kgd->create_process_vm(
  551. dev->kgd, &pdd->vm, &p->kgd_process_info, &p->ef);
  552. if (ret) {
  553. pr_err("Failed to create process VM object\n");
  554. return ret;
  555. }
  556. ret = kfd_process_device_reserve_ib_mem(pdd);
  557. if (ret)
  558. goto err_reserve_ib_mem;
  559. ret = kfd_process_device_init_cwsr_dgpu(pdd);
  560. if (ret)
  561. goto err_init_cwsr;
  562. pdd->drm_file = drm_file;
  563. return 0;
  564. err_init_cwsr:
  565. err_reserve_ib_mem:
  566. kfd_process_device_free_bos(pdd);
  567. if (!drm_file)
  568. dev->kfd2kgd->destroy_process_vm(dev->kgd, pdd->vm);
  569. pdd->vm = NULL;
  570. return ret;
  571. }
  572. /*
  573. * Direct the IOMMU to bind the process (specifically the pasid->mm)
  574. * to the device.
  575. * Unbinding occurs when the process dies or the device is removed.
  576. *
  577. * Assumes that the process lock is held.
  578. */
  579. struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
  580. struct kfd_process *p)
  581. {
  582. struct kfd_process_device *pdd;
  583. int err;
  584. pdd = kfd_get_process_device_data(dev, p);
  585. if (!pdd) {
  586. pr_err("Process device data doesn't exist\n");
  587. return ERR_PTR(-ENOMEM);
  588. }
  589. err = kfd_iommu_bind_process_to_device(pdd);
  590. if (err)
  591. return ERR_PTR(err);
  592. err = kfd_process_device_init_vm(pdd, NULL);
  593. if (err)
  594. return ERR_PTR(err);
  595. return pdd;
  596. }
  597. struct kfd_process_device *kfd_get_first_process_device_data(
  598. struct kfd_process *p)
  599. {
  600. return list_first_entry(&p->per_device_data,
  601. struct kfd_process_device,
  602. per_device_list);
  603. }
  604. struct kfd_process_device *kfd_get_next_process_device_data(
  605. struct kfd_process *p,
  606. struct kfd_process_device *pdd)
  607. {
  608. if (list_is_last(&pdd->per_device_list, &p->per_device_data))
  609. return NULL;
  610. return list_next_entry(pdd, per_device_list);
  611. }
  612. bool kfd_has_process_device_data(struct kfd_process *p)
  613. {
  614. return !(list_empty(&p->per_device_data));
  615. }
  616. /* Create specific handle mapped to mem from process local memory idr
  617. * Assumes that the process lock is held.
  618. */
  619. int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
  620. void *mem)
  621. {
  622. return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
  623. }
  624. /* Translate specific handle from process local memory idr
  625. * Assumes that the process lock is held.
  626. */
  627. void *kfd_process_device_translate_handle(struct kfd_process_device *pdd,
  628. int handle)
  629. {
  630. if (handle < 0)
  631. return NULL;
  632. return idr_find(&pdd->alloc_idr, handle);
  633. }
  634. /* Remove specific handle from process local memory idr
  635. * Assumes that the process lock is held.
  636. */
  637. void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
  638. int handle)
  639. {
  640. if (handle >= 0)
  641. idr_remove(&pdd->alloc_idr, handle);
  642. }
  643. /* This increments the process->ref counter. */
  644. struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
  645. {
  646. struct kfd_process *p, *ret_p = NULL;
  647. unsigned int temp;
  648. int idx = srcu_read_lock(&kfd_processes_srcu);
  649. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  650. if (p->pasid == pasid) {
  651. kref_get(&p->ref);
  652. ret_p = p;
  653. break;
  654. }
  655. }
  656. srcu_read_unlock(&kfd_processes_srcu, idx);
  657. return ret_p;
  658. }
  659. /* This increments the process->ref counter. */
  660. struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
  661. {
  662. struct kfd_process *p;
  663. int idx = srcu_read_lock(&kfd_processes_srcu);
  664. p = find_process_by_mm(mm);
  665. if (p)
  666. kref_get(&p->ref);
  667. srcu_read_unlock(&kfd_processes_srcu, idx);
  668. return p;
  669. }
  670. /* process_evict_queues - Evict all user queues of a process
  671. *
  672. * Eviction is reference-counted per process-device. This means multiple
  673. * evictions from different sources can be nested safely.
  674. */
  675. int kfd_process_evict_queues(struct kfd_process *p)
  676. {
  677. struct kfd_process_device *pdd;
  678. int r = 0;
  679. unsigned int n_evicted = 0;
  680. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  681. r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
  682. &pdd->qpd);
  683. if (r) {
  684. pr_err("Failed to evict process queues\n");
  685. goto fail;
  686. }
  687. n_evicted++;
  688. }
  689. return r;
  690. fail:
  691. /* To keep state consistent, roll back partial eviction by
  692. * restoring queues
  693. */
  694. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  695. if (n_evicted == 0)
  696. break;
  697. if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
  698. &pdd->qpd))
  699. pr_err("Failed to restore queues\n");
  700. n_evicted--;
  701. }
  702. return r;
  703. }
  704. /* process_restore_queues - Restore all user queues of a process */
  705. int kfd_process_restore_queues(struct kfd_process *p)
  706. {
  707. struct kfd_process_device *pdd;
  708. int r, ret = 0;
  709. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  710. r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
  711. &pdd->qpd);
  712. if (r) {
  713. pr_err("Failed to restore process queues\n");
  714. if (!ret)
  715. ret = r;
  716. }
  717. }
  718. return ret;
  719. }
  720. static void evict_process_worker(struct work_struct *work)
  721. {
  722. int ret;
  723. struct kfd_process *p;
  724. struct delayed_work *dwork;
  725. dwork = to_delayed_work(work);
  726. /* Process termination destroys this worker thread. So during the
  727. * lifetime of this thread, kfd_process p will be valid
  728. */
  729. p = container_of(dwork, struct kfd_process, eviction_work);
  730. WARN_ONCE(p->last_eviction_seqno != p->ef->seqno,
  731. "Eviction fence mismatch\n");
  732. /* Narrow window of overlap between restore and evict work
  733. * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos
  734. * unreserves KFD BOs, it is possible to evicted again. But
  735. * restore has few more steps of finish. So lets wait for any
  736. * previous restore work to complete
  737. */
  738. flush_delayed_work(&p->restore_work);
  739. pr_debug("Started evicting pasid %d\n", p->pasid);
  740. ret = kfd_process_evict_queues(p);
  741. if (!ret) {
  742. dma_fence_signal(p->ef);
  743. dma_fence_put(p->ef);
  744. p->ef = NULL;
  745. queue_delayed_work(kfd_restore_wq, &p->restore_work,
  746. msecs_to_jiffies(PROCESS_RESTORE_TIME_MS));
  747. pr_debug("Finished evicting pasid %d\n", p->pasid);
  748. } else
  749. pr_err("Failed to evict queues of pasid %d\n", p->pasid);
  750. }
  751. static void restore_process_worker(struct work_struct *work)
  752. {
  753. struct delayed_work *dwork;
  754. struct kfd_process *p;
  755. struct kfd_process_device *pdd;
  756. int ret = 0;
  757. dwork = to_delayed_work(work);
  758. /* Process termination destroys this worker thread. So during the
  759. * lifetime of this thread, kfd_process p will be valid
  760. */
  761. p = container_of(dwork, struct kfd_process, restore_work);
  762. /* Call restore_process_bos on the first KGD device. This function
  763. * takes care of restoring the whole process including other devices.
  764. * Restore can fail if enough memory is not available. If so,
  765. * reschedule again.
  766. */
  767. pdd = list_first_entry(&p->per_device_data,
  768. struct kfd_process_device,
  769. per_device_list);
  770. pr_debug("Started restoring pasid %d\n", p->pasid);
  771. /* Setting last_restore_timestamp before successful restoration.
  772. * Otherwise this would have to be set by KGD (restore_process_bos)
  773. * before KFD BOs are unreserved. If not, the process can be evicted
  774. * again before the timestamp is set.
  775. * If restore fails, the timestamp will be set again in the next
  776. * attempt. This would mean that the minimum GPU quanta would be
  777. * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
  778. * functions)
  779. */
  780. p->last_restore_timestamp = get_jiffies_64();
  781. ret = pdd->dev->kfd2kgd->restore_process_bos(p->kgd_process_info,
  782. &p->ef);
  783. if (ret) {
  784. pr_debug("Failed to restore BOs of pasid %d, retry after %d ms\n",
  785. p->pasid, PROCESS_BACK_OFF_TIME_MS);
  786. ret = queue_delayed_work(kfd_restore_wq, &p->restore_work,
  787. msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS));
  788. WARN(!ret, "reschedule restore work failed\n");
  789. return;
  790. }
  791. ret = kfd_process_restore_queues(p);
  792. if (!ret)
  793. pr_debug("Finished restoring pasid %d\n", p->pasid);
  794. else
  795. pr_err("Failed to restore queues of pasid %d\n", p->pasid);
  796. }
  797. void kfd_suspend_all_processes(void)
  798. {
  799. struct kfd_process *p;
  800. unsigned int temp;
  801. int idx = srcu_read_lock(&kfd_processes_srcu);
  802. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  803. cancel_delayed_work_sync(&p->eviction_work);
  804. cancel_delayed_work_sync(&p->restore_work);
  805. if (kfd_process_evict_queues(p))
  806. pr_err("Failed to suspend process %d\n", p->pasid);
  807. dma_fence_signal(p->ef);
  808. dma_fence_put(p->ef);
  809. p->ef = NULL;
  810. }
  811. srcu_read_unlock(&kfd_processes_srcu, idx);
  812. }
  813. int kfd_resume_all_processes(void)
  814. {
  815. struct kfd_process *p;
  816. unsigned int temp;
  817. int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
  818. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  819. if (!queue_delayed_work(kfd_restore_wq, &p->restore_work, 0)) {
  820. pr_err("Restore process %d failed during resume\n",
  821. p->pasid);
  822. ret = -EFAULT;
  823. }
  824. }
  825. srcu_read_unlock(&kfd_processes_srcu, idx);
  826. return ret;
  827. }
  828. int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process,
  829. struct vm_area_struct *vma)
  830. {
  831. struct kfd_process_device *pdd;
  832. struct qcm_process_device *qpd;
  833. if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
  834. pr_err("Incorrect CWSR mapping size.\n");
  835. return -EINVAL;
  836. }
  837. pdd = kfd_get_process_device_data(dev, process);
  838. if (!pdd)
  839. return -EINVAL;
  840. qpd = &pdd->qpd;
  841. qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  842. get_order(KFD_CWSR_TBA_TMA_SIZE));
  843. if (!qpd->cwsr_kaddr) {
  844. pr_err("Error allocating per process CWSR buffer.\n");
  845. return -ENOMEM;
  846. }
  847. vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
  848. | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
  849. /* Mapping pages to user process */
  850. return remap_pfn_range(vma, vma->vm_start,
  851. PFN_DOWN(__pa(qpd->cwsr_kaddr)),
  852. KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
  853. }
  854. void kfd_flush_tlb(struct kfd_process_device *pdd)
  855. {
  856. struct kfd_dev *dev = pdd->dev;
  857. const struct kfd2kgd_calls *f2g = dev->kfd2kgd;
  858. if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
  859. /* Nothing to flush until a VMID is assigned, which
  860. * only happens when the first queue is created.
  861. */
  862. if (pdd->qpd.vmid)
  863. f2g->invalidate_tlbs_vmid(dev->kgd, pdd->qpd.vmid);
  864. } else {
  865. f2g->invalidate_tlbs(dev->kgd, pdd->process->pasid);
  866. }
  867. }
  868. #if defined(CONFIG_DEBUG_FS)
  869. int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
  870. {
  871. struct kfd_process *p;
  872. unsigned int temp;
  873. int r = 0;
  874. int idx = srcu_read_lock(&kfd_processes_srcu);
  875. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  876. seq_printf(m, "Process %d PASID %d:\n",
  877. p->lead_thread->tgid, p->pasid);
  878. mutex_lock(&p->mutex);
  879. r = pqm_debugfs_mqds(m, &p->pqm);
  880. mutex_unlock(&p->mutex);
  881. if (r)
  882. break;
  883. }
  884. srcu_read_unlock(&kfd_processes_srcu, idx);
  885. return r;
  886. }
  887. #endif