kfd_process.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. if (!process)
  206. return ERR_PTR(-EINVAL);
  207. return process;
  208. }
  209. static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
  210. {
  211. struct kfd_process *process;
  212. hash_for_each_possible_rcu(kfd_processes_table, process,
  213. kfd_processes, (uintptr_t)mm)
  214. if (process->mm == mm)
  215. return process;
  216. return NULL;
  217. }
  218. static struct kfd_process *find_process(const struct task_struct *thread)
  219. {
  220. struct kfd_process *p;
  221. int idx;
  222. idx = srcu_read_lock(&kfd_processes_srcu);
  223. p = find_process_by_mm(thread->mm);
  224. srcu_read_unlock(&kfd_processes_srcu, idx);
  225. return p;
  226. }
  227. void kfd_unref_process(struct kfd_process *p)
  228. {
  229. kref_put(&p->ref, kfd_process_ref_release);
  230. }
  231. static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
  232. {
  233. struct kfd_process *p = pdd->process;
  234. void *mem;
  235. int id;
  236. /*
  237. * Remove all handles from idr and release appropriate
  238. * local memory object
  239. */
  240. idr_for_each_entry(&pdd->alloc_idr, mem, id) {
  241. struct kfd_process_device *peer_pdd;
  242. list_for_each_entry(peer_pdd, &p->per_device_data,
  243. per_device_list) {
  244. if (!peer_pdd->vm)
  245. continue;
  246. peer_pdd->dev->kfd2kgd->unmap_memory_to_gpu(
  247. peer_pdd->dev->kgd, mem, peer_pdd->vm);
  248. }
  249. pdd->dev->kfd2kgd->free_memory_of_gpu(pdd->dev->kgd, mem);
  250. kfd_process_device_remove_obj_handle(pdd, id);
  251. }
  252. }
  253. static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
  254. {
  255. struct kfd_process_device *pdd;
  256. list_for_each_entry(pdd, &p->per_device_data, per_device_list)
  257. kfd_process_device_free_bos(pdd);
  258. }
  259. static void kfd_process_destroy_pdds(struct kfd_process *p)
  260. {
  261. struct kfd_process_device *pdd, *temp;
  262. list_for_each_entry_safe(pdd, temp, &p->per_device_data,
  263. per_device_list) {
  264. pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
  265. pdd->dev->id, p->pasid);
  266. if (pdd->drm_file)
  267. fput(pdd->drm_file);
  268. else if (pdd->vm)
  269. pdd->dev->kfd2kgd->destroy_process_vm(
  270. pdd->dev->kgd, pdd->vm);
  271. list_del(&pdd->per_device_list);
  272. if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base)
  273. free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
  274. get_order(KFD_CWSR_TBA_TMA_SIZE));
  275. kfree(pdd->qpd.doorbell_bitmap);
  276. idr_destroy(&pdd->alloc_idr);
  277. kfree(pdd);
  278. }
  279. }
  280. /* No process locking is needed in this function, because the process
  281. * is not findable any more. We must assume that no other thread is
  282. * using it any more, otherwise we couldn't safely free the process
  283. * structure in the end.
  284. */
  285. static void kfd_process_wq_release(struct work_struct *work)
  286. {
  287. struct kfd_process *p = container_of(work, struct kfd_process,
  288. release_work);
  289. kfd_iommu_unbind_process(p);
  290. kfd_process_free_outstanding_kfd_bos(p);
  291. kfd_process_destroy_pdds(p);
  292. dma_fence_put(p->ef);
  293. kfd_event_free_process(p);
  294. kfd_pasid_free(p->pasid);
  295. kfd_free_process_doorbells(p);
  296. mutex_destroy(&p->mutex);
  297. put_task_struct(p->lead_thread);
  298. kfree(p);
  299. }
  300. static void kfd_process_ref_release(struct kref *ref)
  301. {
  302. struct kfd_process *p = container_of(ref, struct kfd_process, ref);
  303. INIT_WORK(&p->release_work, kfd_process_wq_release);
  304. queue_work(kfd_process_wq, &p->release_work);
  305. }
  306. static void kfd_process_destroy_delayed(struct rcu_head *rcu)
  307. {
  308. struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
  309. kfd_unref_process(p);
  310. }
  311. static void kfd_process_notifier_release(struct mmu_notifier *mn,
  312. struct mm_struct *mm)
  313. {
  314. struct kfd_process *p;
  315. struct kfd_process_device *pdd = NULL;
  316. /*
  317. * The kfd_process structure can not be free because the
  318. * mmu_notifier srcu is read locked
  319. */
  320. p = container_of(mn, struct kfd_process, mmu_notifier);
  321. if (WARN_ON(p->mm != mm))
  322. return;
  323. mutex_lock(&kfd_processes_mutex);
  324. hash_del_rcu(&p->kfd_processes);
  325. mutex_unlock(&kfd_processes_mutex);
  326. synchronize_srcu(&kfd_processes_srcu);
  327. cancel_delayed_work_sync(&p->eviction_work);
  328. cancel_delayed_work_sync(&p->restore_work);
  329. mutex_lock(&p->mutex);
  330. /* Iterate over all process device data structures and if the
  331. * pdd is in debug mode, we should first force unregistration,
  332. * then we will be able to destroy the queues
  333. */
  334. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  335. struct kfd_dev *dev = pdd->dev;
  336. mutex_lock(kfd_get_dbgmgr_mutex());
  337. if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
  338. if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
  339. kfd_dbgmgr_destroy(dev->dbgmgr);
  340. dev->dbgmgr = NULL;
  341. }
  342. }
  343. mutex_unlock(kfd_get_dbgmgr_mutex());
  344. }
  345. kfd_process_dequeue_from_all_devices(p);
  346. pqm_uninit(&p->pqm);
  347. /* Indicate to other users that MM is no longer valid */
  348. p->mm = NULL;
  349. mutex_unlock(&p->mutex);
  350. mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
  351. mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
  352. }
  353. static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
  354. .release = kfd_process_notifier_release,
  355. };
  356. static int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
  357. {
  358. unsigned long offset;
  359. struct kfd_process_device *pdd;
  360. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  361. struct kfd_dev *dev = pdd->dev;
  362. struct qcm_process_device *qpd = &pdd->qpd;
  363. if (!dev->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base)
  364. continue;
  365. offset = (KFD_MMAP_TYPE_RESERVED_MEM | KFD_MMAP_GPU_ID(dev->id))
  366. << PAGE_SHIFT;
  367. qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
  368. KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
  369. MAP_SHARED, offset);
  370. if (IS_ERR_VALUE(qpd->tba_addr)) {
  371. int err = qpd->tba_addr;
  372. pr_err("Failure to set tba address. error %d.\n", err);
  373. qpd->tba_addr = 0;
  374. qpd->cwsr_kaddr = NULL;
  375. return err;
  376. }
  377. memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
  378. qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
  379. pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
  380. qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
  381. }
  382. return 0;
  383. }
  384. static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd)
  385. {
  386. struct kfd_dev *dev = pdd->dev;
  387. struct qcm_process_device *qpd = &pdd->qpd;
  388. uint32_t flags = ALLOC_MEM_FLAGS_GTT |
  389. ALLOC_MEM_FLAGS_NO_SUBSTITUTE | ALLOC_MEM_FLAGS_EXECUTABLE;
  390. void *kaddr;
  391. int ret;
  392. if (!dev->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base)
  393. return 0;
  394. /* cwsr_base is only set for dGPU */
  395. ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
  396. KFD_CWSR_TBA_TMA_SIZE, flags, &kaddr);
  397. if (ret)
  398. return ret;
  399. qpd->cwsr_kaddr = kaddr;
  400. qpd->tba_addr = qpd->cwsr_base;
  401. memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
  402. qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
  403. pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
  404. qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
  405. return 0;
  406. }
  407. static struct kfd_process *create_process(const struct task_struct *thread,
  408. struct file *filep)
  409. {
  410. struct kfd_process *process;
  411. int err = -ENOMEM;
  412. process = kzalloc(sizeof(*process), GFP_KERNEL);
  413. if (!process)
  414. goto err_alloc_process;
  415. process->pasid = kfd_pasid_alloc();
  416. if (process->pasid == 0)
  417. goto err_alloc_pasid;
  418. if (kfd_alloc_process_doorbells(process) < 0)
  419. goto err_alloc_doorbells;
  420. kref_init(&process->ref);
  421. mutex_init(&process->mutex);
  422. process->mm = thread->mm;
  423. /* register notifier */
  424. process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
  425. err = mmu_notifier_register(&process->mmu_notifier, process->mm);
  426. if (err)
  427. goto err_mmu_notifier;
  428. hash_add_rcu(kfd_processes_table, &process->kfd_processes,
  429. (uintptr_t)process->mm);
  430. process->lead_thread = thread->group_leader;
  431. get_task_struct(process->lead_thread);
  432. INIT_LIST_HEAD(&process->per_device_data);
  433. kfd_event_init_process(process);
  434. err = pqm_init(&process->pqm, process);
  435. if (err != 0)
  436. goto err_process_pqm_init;
  437. /* init process apertures*/
  438. process->is_32bit_user_mode = in_compat_syscall();
  439. err = kfd_init_apertures(process);
  440. if (err != 0)
  441. goto err_init_apertures;
  442. INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
  443. INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
  444. process->last_restore_timestamp = get_jiffies_64();
  445. err = kfd_process_init_cwsr_apu(process, filep);
  446. if (err)
  447. goto err_init_cwsr;
  448. return process;
  449. err_init_cwsr:
  450. kfd_process_free_outstanding_kfd_bos(process);
  451. kfd_process_destroy_pdds(process);
  452. err_init_apertures:
  453. pqm_uninit(&process->pqm);
  454. err_process_pqm_init:
  455. hash_del_rcu(&process->kfd_processes);
  456. synchronize_rcu();
  457. mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
  458. err_mmu_notifier:
  459. mutex_destroy(&process->mutex);
  460. kfd_free_process_doorbells(process);
  461. err_alloc_doorbells:
  462. kfd_pasid_free(process->pasid);
  463. err_alloc_pasid:
  464. kfree(process);
  465. err_alloc_process:
  466. return ERR_PTR(err);
  467. }
  468. static int init_doorbell_bitmap(struct qcm_process_device *qpd,
  469. struct kfd_dev *dev)
  470. {
  471. unsigned int i;
  472. if (!KFD_IS_SOC15(dev->device_info->asic_family))
  473. return 0;
  474. qpd->doorbell_bitmap =
  475. kzalloc(DIV_ROUND_UP(KFD_MAX_NUM_OF_QUEUES_PER_PROCESS,
  476. BITS_PER_BYTE), GFP_KERNEL);
  477. if (!qpd->doorbell_bitmap)
  478. return -ENOMEM;
  479. /* Mask out any reserved doorbells */
  480. for (i = 0; i < KFD_MAX_NUM_OF_QUEUES_PER_PROCESS; i++)
  481. if ((dev->shared_resources.reserved_doorbell_mask & i) ==
  482. dev->shared_resources.reserved_doorbell_val) {
  483. set_bit(i, qpd->doorbell_bitmap);
  484. pr_debug("reserved doorbell 0x%03x\n", i);
  485. }
  486. return 0;
  487. }
  488. struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
  489. struct kfd_process *p)
  490. {
  491. struct kfd_process_device *pdd = NULL;
  492. list_for_each_entry(pdd, &p->per_device_data, per_device_list)
  493. if (pdd->dev == dev)
  494. return pdd;
  495. return NULL;
  496. }
  497. struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
  498. struct kfd_process *p)
  499. {
  500. struct kfd_process_device *pdd = NULL;
  501. pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
  502. if (!pdd)
  503. return NULL;
  504. if (init_doorbell_bitmap(&pdd->qpd, dev)) {
  505. pr_err("Failed to init doorbell for process\n");
  506. kfree(pdd);
  507. return NULL;
  508. }
  509. pdd->dev = dev;
  510. INIT_LIST_HEAD(&pdd->qpd.queues_list);
  511. INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
  512. pdd->qpd.dqm = dev->dqm;
  513. pdd->qpd.pqm = &p->pqm;
  514. pdd->qpd.evicted = 0;
  515. pdd->process = p;
  516. pdd->bound = PDD_UNBOUND;
  517. pdd->already_dequeued = false;
  518. list_add(&pdd->per_device_list, &p->per_device_data);
  519. /* Init idr used for memory handle translation */
  520. idr_init(&pdd->alloc_idr);
  521. return pdd;
  522. }
  523. /**
  524. * kfd_process_device_init_vm - Initialize a VM for a process-device
  525. *
  526. * @pdd: The process-device
  527. * @drm_file: Optional pointer to a DRM file descriptor
  528. *
  529. * If @drm_file is specified, it will be used to acquire the VM from
  530. * that file descriptor. If successful, the @pdd takes ownership of
  531. * the file descriptor.
  532. *
  533. * If @drm_file is NULL, a new VM is created.
  534. *
  535. * Returns 0 on success, -errno on failure.
  536. */
  537. int kfd_process_device_init_vm(struct kfd_process_device *pdd,
  538. struct file *drm_file)
  539. {
  540. struct kfd_process *p;
  541. struct kfd_dev *dev;
  542. int ret;
  543. if (pdd->vm)
  544. return drm_file ? -EBUSY : 0;
  545. p = pdd->process;
  546. dev = pdd->dev;
  547. if (drm_file)
  548. ret = dev->kfd2kgd->acquire_process_vm(
  549. dev->kgd, drm_file,
  550. &pdd->vm, &p->kgd_process_info, &p->ef);
  551. else
  552. ret = dev->kfd2kgd->create_process_vm(
  553. dev->kgd, &pdd->vm, &p->kgd_process_info, &p->ef);
  554. if (ret) {
  555. pr_err("Failed to create process VM object\n");
  556. return ret;
  557. }
  558. ret = kfd_process_device_reserve_ib_mem(pdd);
  559. if (ret)
  560. goto err_reserve_ib_mem;
  561. ret = kfd_process_device_init_cwsr_dgpu(pdd);
  562. if (ret)
  563. goto err_init_cwsr;
  564. pdd->drm_file = drm_file;
  565. return 0;
  566. err_init_cwsr:
  567. err_reserve_ib_mem:
  568. kfd_process_device_free_bos(pdd);
  569. if (!drm_file)
  570. dev->kfd2kgd->destroy_process_vm(dev->kgd, pdd->vm);
  571. pdd->vm = NULL;
  572. return ret;
  573. }
  574. /*
  575. * Direct the IOMMU to bind the process (specifically the pasid->mm)
  576. * to the device.
  577. * Unbinding occurs when the process dies or the device is removed.
  578. *
  579. * Assumes that the process lock is held.
  580. */
  581. struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
  582. struct kfd_process *p)
  583. {
  584. struct kfd_process_device *pdd;
  585. int err;
  586. pdd = kfd_get_process_device_data(dev, p);
  587. if (!pdd) {
  588. pr_err("Process device data doesn't exist\n");
  589. return ERR_PTR(-ENOMEM);
  590. }
  591. err = kfd_iommu_bind_process_to_device(pdd);
  592. if (err)
  593. return ERR_PTR(err);
  594. err = kfd_process_device_init_vm(pdd, NULL);
  595. if (err)
  596. return ERR_PTR(err);
  597. return pdd;
  598. }
  599. struct kfd_process_device *kfd_get_first_process_device_data(
  600. struct kfd_process *p)
  601. {
  602. return list_first_entry(&p->per_device_data,
  603. struct kfd_process_device,
  604. per_device_list);
  605. }
  606. struct kfd_process_device *kfd_get_next_process_device_data(
  607. struct kfd_process *p,
  608. struct kfd_process_device *pdd)
  609. {
  610. if (list_is_last(&pdd->per_device_list, &p->per_device_data))
  611. return NULL;
  612. return list_next_entry(pdd, per_device_list);
  613. }
  614. bool kfd_has_process_device_data(struct kfd_process *p)
  615. {
  616. return !(list_empty(&p->per_device_data));
  617. }
  618. /* Create specific handle mapped to mem from process local memory idr
  619. * Assumes that the process lock is held.
  620. */
  621. int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
  622. void *mem)
  623. {
  624. return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
  625. }
  626. /* Translate specific handle from process local memory idr
  627. * Assumes that the process lock is held.
  628. */
  629. void *kfd_process_device_translate_handle(struct kfd_process_device *pdd,
  630. int handle)
  631. {
  632. if (handle < 0)
  633. return NULL;
  634. return idr_find(&pdd->alloc_idr, handle);
  635. }
  636. /* Remove specific handle from process local memory idr
  637. * Assumes that the process lock is held.
  638. */
  639. void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
  640. int handle)
  641. {
  642. if (handle >= 0)
  643. idr_remove(&pdd->alloc_idr, handle);
  644. }
  645. /* This increments the process->ref counter. */
  646. struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
  647. {
  648. struct kfd_process *p, *ret_p = NULL;
  649. unsigned int temp;
  650. int idx = srcu_read_lock(&kfd_processes_srcu);
  651. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  652. if (p->pasid == pasid) {
  653. kref_get(&p->ref);
  654. ret_p = p;
  655. break;
  656. }
  657. }
  658. srcu_read_unlock(&kfd_processes_srcu, idx);
  659. return ret_p;
  660. }
  661. /* This increments the process->ref counter. */
  662. struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
  663. {
  664. struct kfd_process *p;
  665. int idx = srcu_read_lock(&kfd_processes_srcu);
  666. p = find_process_by_mm(mm);
  667. if (p)
  668. kref_get(&p->ref);
  669. srcu_read_unlock(&kfd_processes_srcu, idx);
  670. return p;
  671. }
  672. /* process_evict_queues - Evict all user queues of a process
  673. *
  674. * Eviction is reference-counted per process-device. This means multiple
  675. * evictions from different sources can be nested safely.
  676. */
  677. int kfd_process_evict_queues(struct kfd_process *p)
  678. {
  679. struct kfd_process_device *pdd;
  680. int r = 0;
  681. unsigned int n_evicted = 0;
  682. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  683. r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
  684. &pdd->qpd);
  685. if (r) {
  686. pr_err("Failed to evict process queues\n");
  687. goto fail;
  688. }
  689. n_evicted++;
  690. }
  691. return r;
  692. fail:
  693. /* To keep state consistent, roll back partial eviction by
  694. * restoring queues
  695. */
  696. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  697. if (n_evicted == 0)
  698. break;
  699. if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
  700. &pdd->qpd))
  701. pr_err("Failed to restore queues\n");
  702. n_evicted--;
  703. }
  704. return r;
  705. }
  706. /* process_restore_queues - Restore all user queues of a process */
  707. int kfd_process_restore_queues(struct kfd_process *p)
  708. {
  709. struct kfd_process_device *pdd;
  710. int r, ret = 0;
  711. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  712. r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
  713. &pdd->qpd);
  714. if (r) {
  715. pr_err("Failed to restore process queues\n");
  716. if (!ret)
  717. ret = r;
  718. }
  719. }
  720. return ret;
  721. }
  722. static void evict_process_worker(struct work_struct *work)
  723. {
  724. int ret;
  725. struct kfd_process *p;
  726. struct delayed_work *dwork;
  727. dwork = to_delayed_work(work);
  728. /* Process termination destroys this worker thread. So during the
  729. * lifetime of this thread, kfd_process p will be valid
  730. */
  731. p = container_of(dwork, struct kfd_process, eviction_work);
  732. WARN_ONCE(p->last_eviction_seqno != p->ef->seqno,
  733. "Eviction fence mismatch\n");
  734. /* Narrow window of overlap between restore and evict work
  735. * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos
  736. * unreserves KFD BOs, it is possible to evicted again. But
  737. * restore has few more steps of finish. So lets wait for any
  738. * previous restore work to complete
  739. */
  740. flush_delayed_work(&p->restore_work);
  741. pr_debug("Started evicting pasid %d\n", p->pasid);
  742. ret = kfd_process_evict_queues(p);
  743. if (!ret) {
  744. dma_fence_signal(p->ef);
  745. dma_fence_put(p->ef);
  746. p->ef = NULL;
  747. queue_delayed_work(kfd_restore_wq, &p->restore_work,
  748. msecs_to_jiffies(PROCESS_RESTORE_TIME_MS));
  749. pr_debug("Finished evicting pasid %d\n", p->pasid);
  750. } else
  751. pr_err("Failed to evict queues of pasid %d\n", p->pasid);
  752. }
  753. static void restore_process_worker(struct work_struct *work)
  754. {
  755. struct delayed_work *dwork;
  756. struct kfd_process *p;
  757. struct kfd_process_device *pdd;
  758. int ret = 0;
  759. dwork = to_delayed_work(work);
  760. /* Process termination destroys this worker thread. So during the
  761. * lifetime of this thread, kfd_process p will be valid
  762. */
  763. p = container_of(dwork, struct kfd_process, restore_work);
  764. /* Call restore_process_bos on the first KGD device. This function
  765. * takes care of restoring the whole process including other devices.
  766. * Restore can fail if enough memory is not available. If so,
  767. * reschedule again.
  768. */
  769. pdd = list_first_entry(&p->per_device_data,
  770. struct kfd_process_device,
  771. per_device_list);
  772. pr_debug("Started restoring pasid %d\n", p->pasid);
  773. /* Setting last_restore_timestamp before successful restoration.
  774. * Otherwise this would have to be set by KGD (restore_process_bos)
  775. * before KFD BOs are unreserved. If not, the process can be evicted
  776. * again before the timestamp is set.
  777. * If restore fails, the timestamp will be set again in the next
  778. * attempt. This would mean that the minimum GPU quanta would be
  779. * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
  780. * functions)
  781. */
  782. p->last_restore_timestamp = get_jiffies_64();
  783. ret = pdd->dev->kfd2kgd->restore_process_bos(p->kgd_process_info,
  784. &p->ef);
  785. if (ret) {
  786. pr_debug("Failed to restore BOs of pasid %d, retry after %d ms\n",
  787. p->pasid, PROCESS_BACK_OFF_TIME_MS);
  788. ret = queue_delayed_work(kfd_restore_wq, &p->restore_work,
  789. msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS));
  790. WARN(!ret, "reschedule restore work failed\n");
  791. return;
  792. }
  793. ret = kfd_process_restore_queues(p);
  794. if (!ret)
  795. pr_debug("Finished restoring pasid %d\n", p->pasid);
  796. else
  797. pr_err("Failed to restore queues of pasid %d\n", p->pasid);
  798. }
  799. void kfd_suspend_all_processes(void)
  800. {
  801. struct kfd_process *p;
  802. unsigned int temp;
  803. int idx = srcu_read_lock(&kfd_processes_srcu);
  804. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  805. cancel_delayed_work_sync(&p->eviction_work);
  806. cancel_delayed_work_sync(&p->restore_work);
  807. if (kfd_process_evict_queues(p))
  808. pr_err("Failed to suspend process %d\n", p->pasid);
  809. dma_fence_signal(p->ef);
  810. dma_fence_put(p->ef);
  811. p->ef = NULL;
  812. }
  813. srcu_read_unlock(&kfd_processes_srcu, idx);
  814. }
  815. int kfd_resume_all_processes(void)
  816. {
  817. struct kfd_process *p;
  818. unsigned int temp;
  819. int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
  820. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  821. if (!queue_delayed_work(kfd_restore_wq, &p->restore_work, 0)) {
  822. pr_err("Restore process %d failed during resume\n",
  823. p->pasid);
  824. ret = -EFAULT;
  825. }
  826. }
  827. srcu_read_unlock(&kfd_processes_srcu, idx);
  828. return ret;
  829. }
  830. int kfd_reserved_mem_mmap(struct kfd_dev *dev, struct kfd_process *process,
  831. struct vm_area_struct *vma)
  832. {
  833. struct kfd_process_device *pdd;
  834. struct qcm_process_device *qpd;
  835. if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
  836. pr_err("Incorrect CWSR mapping size.\n");
  837. return -EINVAL;
  838. }
  839. pdd = kfd_get_process_device_data(dev, process);
  840. if (!pdd)
  841. return -EINVAL;
  842. qpd = &pdd->qpd;
  843. qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  844. get_order(KFD_CWSR_TBA_TMA_SIZE));
  845. if (!qpd->cwsr_kaddr) {
  846. pr_err("Error allocating per process CWSR buffer.\n");
  847. return -ENOMEM;
  848. }
  849. vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
  850. | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
  851. /* Mapping pages to user process */
  852. return remap_pfn_range(vma, vma->vm_start,
  853. PFN_DOWN(__pa(qpd->cwsr_kaddr)),
  854. KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
  855. }
  856. void kfd_flush_tlb(struct kfd_process_device *pdd)
  857. {
  858. struct kfd_dev *dev = pdd->dev;
  859. const struct kfd2kgd_calls *f2g = dev->kfd2kgd;
  860. if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
  861. /* Nothing to flush until a VMID is assigned, which
  862. * only happens when the first queue is created.
  863. */
  864. if (pdd->qpd.vmid)
  865. f2g->invalidate_tlbs_vmid(dev->kgd, pdd->qpd.vmid);
  866. } else {
  867. f2g->invalidate_tlbs(dev->kgd, pdd->process->pasid);
  868. }
  869. }
  870. #if defined(CONFIG_DEBUG_FS)
  871. int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
  872. {
  873. struct kfd_process *p;
  874. unsigned int temp;
  875. int r = 0;
  876. int idx = srcu_read_lock(&kfd_processes_srcu);
  877. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  878. seq_printf(m, "Process %d PASID %d:\n",
  879. p->lead_thread->tgid, p->pasid);
  880. mutex_lock(&p->mutex);
  881. r = pqm_debugfs_mqds(m, &p->pqm);
  882. mutex_unlock(&p->mutex);
  883. if (r)
  884. break;
  885. }
  886. srcu_read_unlock(&kfd_processes_srcu, idx);
  887. return r;
  888. }
  889. #endif