kfd_process.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. struct mm_struct;
  33. #include "kfd_priv.h"
  34. #include "kfd_device_queue_manager.h"
  35. #include "kfd_dbgmgr.h"
  36. #include "kfd_iommu.h"
  37. /*
  38. * List of struct kfd_process (field kfd_process).
  39. * Unique/indexed by mm_struct*
  40. */
  41. DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
  42. static DEFINE_MUTEX(kfd_processes_mutex);
  43. DEFINE_SRCU(kfd_processes_srcu);
  44. static struct workqueue_struct *kfd_process_wq;
  45. static struct kfd_process *find_process(const struct task_struct *thread);
  46. static void kfd_process_ref_release(struct kref *ref);
  47. static struct kfd_process *create_process(const struct task_struct *thread,
  48. struct file *filep);
  49. static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep);
  50. static void evict_process_worker(struct work_struct *work);
  51. static void restore_process_worker(struct work_struct *work);
  52. void kfd_process_create_wq(void)
  53. {
  54. if (!kfd_process_wq)
  55. kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
  56. }
  57. void kfd_process_destroy_wq(void)
  58. {
  59. if (kfd_process_wq) {
  60. destroy_workqueue(kfd_process_wq);
  61. kfd_process_wq = NULL;
  62. }
  63. }
  64. struct kfd_process *kfd_create_process(struct file *filep)
  65. {
  66. struct kfd_process *process;
  67. struct task_struct *thread = current;
  68. if (!thread->mm)
  69. return ERR_PTR(-EINVAL);
  70. /* Only the pthreads threading model is supported. */
  71. if (thread->group_leader->mm != thread->mm)
  72. return ERR_PTR(-EINVAL);
  73. /*
  74. * take kfd processes mutex before starting of process creation
  75. * so there won't be a case where two threads of the same process
  76. * create two kfd_process structures
  77. */
  78. mutex_lock(&kfd_processes_mutex);
  79. /* A prior open of /dev/kfd could have already created the process. */
  80. process = find_process(thread);
  81. if (process)
  82. pr_debug("Process already found\n");
  83. else
  84. process = create_process(thread, filep);
  85. mutex_unlock(&kfd_processes_mutex);
  86. return process;
  87. }
  88. struct kfd_process *kfd_get_process(const struct task_struct *thread)
  89. {
  90. struct kfd_process *process;
  91. if (!thread->mm)
  92. return ERR_PTR(-EINVAL);
  93. /* Only the pthreads threading model is supported. */
  94. if (thread->group_leader->mm != thread->mm)
  95. return ERR_PTR(-EINVAL);
  96. process = find_process(thread);
  97. return process;
  98. }
  99. static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
  100. {
  101. struct kfd_process *process;
  102. hash_for_each_possible_rcu(kfd_processes_table, process,
  103. kfd_processes, (uintptr_t)mm)
  104. if (process->mm == mm)
  105. return process;
  106. return NULL;
  107. }
  108. static struct kfd_process *find_process(const struct task_struct *thread)
  109. {
  110. struct kfd_process *p;
  111. int idx;
  112. idx = srcu_read_lock(&kfd_processes_srcu);
  113. p = find_process_by_mm(thread->mm);
  114. srcu_read_unlock(&kfd_processes_srcu, idx);
  115. return p;
  116. }
  117. void kfd_unref_process(struct kfd_process *p)
  118. {
  119. kref_put(&p->ref, kfd_process_ref_release);
  120. }
  121. static void kfd_process_destroy_pdds(struct kfd_process *p)
  122. {
  123. struct kfd_process_device *pdd, *temp;
  124. list_for_each_entry_safe(pdd, temp, &p->per_device_data,
  125. per_device_list) {
  126. pr_debug("Releasing pdd (topology id %d) for process (pasid %d)\n",
  127. pdd->dev->id, p->pasid);
  128. if (pdd->vm)
  129. pdd->dev->kfd2kgd->destroy_process_vm(
  130. pdd->dev->kgd, pdd->vm);
  131. list_del(&pdd->per_device_list);
  132. if (pdd->qpd.cwsr_kaddr)
  133. free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
  134. get_order(KFD_CWSR_TBA_TMA_SIZE));
  135. kfree(pdd);
  136. }
  137. }
  138. /* No process locking is needed in this function, because the process
  139. * is not findable any more. We must assume that no other thread is
  140. * using it any more, otherwise we couldn't safely free the process
  141. * structure in the end.
  142. */
  143. static void kfd_process_wq_release(struct work_struct *work)
  144. {
  145. struct kfd_process *p = container_of(work, struct kfd_process,
  146. release_work);
  147. kfd_iommu_unbind_process(p);
  148. kfd_process_destroy_pdds(p);
  149. dma_fence_put(p->ef);
  150. kfd_event_free_process(p);
  151. kfd_pasid_free(p->pasid);
  152. kfd_free_process_doorbells(p);
  153. mutex_destroy(&p->mutex);
  154. put_task_struct(p->lead_thread);
  155. kfree(p);
  156. }
  157. static void kfd_process_ref_release(struct kref *ref)
  158. {
  159. struct kfd_process *p = container_of(ref, struct kfd_process, ref);
  160. INIT_WORK(&p->release_work, kfd_process_wq_release);
  161. queue_work(kfd_process_wq, &p->release_work);
  162. }
  163. static void kfd_process_destroy_delayed(struct rcu_head *rcu)
  164. {
  165. struct kfd_process *p = container_of(rcu, struct kfd_process, rcu);
  166. kfd_unref_process(p);
  167. }
  168. static void kfd_process_notifier_release(struct mmu_notifier *mn,
  169. struct mm_struct *mm)
  170. {
  171. struct kfd_process *p;
  172. struct kfd_process_device *pdd = NULL;
  173. /*
  174. * The kfd_process structure can not be free because the
  175. * mmu_notifier srcu is read locked
  176. */
  177. p = container_of(mn, struct kfd_process, mmu_notifier);
  178. if (WARN_ON(p->mm != mm))
  179. return;
  180. mutex_lock(&kfd_processes_mutex);
  181. hash_del_rcu(&p->kfd_processes);
  182. mutex_unlock(&kfd_processes_mutex);
  183. synchronize_srcu(&kfd_processes_srcu);
  184. cancel_delayed_work_sync(&p->eviction_work);
  185. cancel_delayed_work_sync(&p->restore_work);
  186. mutex_lock(&p->mutex);
  187. /* Iterate over all process device data structures and if the
  188. * pdd is in debug mode, we should first force unregistration,
  189. * then we will be able to destroy the queues
  190. */
  191. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  192. struct kfd_dev *dev = pdd->dev;
  193. mutex_lock(kfd_get_dbgmgr_mutex());
  194. if (dev && dev->dbgmgr && dev->dbgmgr->pasid == p->pasid) {
  195. if (!kfd_dbgmgr_unregister(dev->dbgmgr, p)) {
  196. kfd_dbgmgr_destroy(dev->dbgmgr);
  197. dev->dbgmgr = NULL;
  198. }
  199. }
  200. mutex_unlock(kfd_get_dbgmgr_mutex());
  201. }
  202. kfd_process_dequeue_from_all_devices(p);
  203. pqm_uninit(&p->pqm);
  204. /* Indicate to other users that MM is no longer valid */
  205. p->mm = NULL;
  206. mutex_unlock(&p->mutex);
  207. mmu_notifier_unregister_no_release(&p->mmu_notifier, mm);
  208. mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
  209. }
  210. static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
  211. .release = kfd_process_notifier_release,
  212. };
  213. static int kfd_process_init_cwsr(struct kfd_process *p, struct file *filep)
  214. {
  215. unsigned long offset;
  216. struct kfd_process_device *pdd = NULL;
  217. struct kfd_dev *dev = NULL;
  218. struct qcm_process_device *qpd = NULL;
  219. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  220. dev = pdd->dev;
  221. qpd = &pdd->qpd;
  222. if (!dev->cwsr_enabled || qpd->cwsr_kaddr)
  223. continue;
  224. offset = (dev->id | KFD_MMAP_RESERVED_MEM_MASK) << PAGE_SHIFT;
  225. qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
  226. KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
  227. MAP_SHARED, offset);
  228. if (IS_ERR_VALUE(qpd->tba_addr)) {
  229. int err = qpd->tba_addr;
  230. pr_err("Failure to set tba address. error %d.\n", err);
  231. qpd->tba_addr = 0;
  232. qpd->cwsr_kaddr = NULL;
  233. return err;
  234. }
  235. memcpy(qpd->cwsr_kaddr, dev->cwsr_isa, dev->cwsr_isa_size);
  236. qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
  237. pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
  238. qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
  239. }
  240. return 0;
  241. }
  242. static struct kfd_process *create_process(const struct task_struct *thread,
  243. struct file *filep)
  244. {
  245. struct kfd_process *process;
  246. int err = -ENOMEM;
  247. process = kzalloc(sizeof(*process), GFP_KERNEL);
  248. if (!process)
  249. goto err_alloc_process;
  250. process->pasid = kfd_pasid_alloc();
  251. if (process->pasid == 0)
  252. goto err_alloc_pasid;
  253. if (kfd_alloc_process_doorbells(process) < 0)
  254. goto err_alloc_doorbells;
  255. kref_init(&process->ref);
  256. mutex_init(&process->mutex);
  257. process->mm = thread->mm;
  258. /* register notifier */
  259. process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
  260. err = mmu_notifier_register(&process->mmu_notifier, process->mm);
  261. if (err)
  262. goto err_mmu_notifier;
  263. hash_add_rcu(kfd_processes_table, &process->kfd_processes,
  264. (uintptr_t)process->mm);
  265. process->lead_thread = thread->group_leader;
  266. get_task_struct(process->lead_thread);
  267. INIT_LIST_HEAD(&process->per_device_data);
  268. kfd_event_init_process(process);
  269. err = pqm_init(&process->pqm, process);
  270. if (err != 0)
  271. goto err_process_pqm_init;
  272. /* init process apertures*/
  273. process->is_32bit_user_mode = in_compat_syscall();
  274. err = kfd_init_apertures(process);
  275. if (err != 0)
  276. goto err_init_apertures;
  277. INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
  278. INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
  279. process->last_restore_timestamp = get_jiffies_64();
  280. err = kfd_process_init_cwsr(process, filep);
  281. if (err)
  282. goto err_init_cwsr;
  283. return process;
  284. err_init_cwsr:
  285. kfd_process_destroy_pdds(process);
  286. err_init_apertures:
  287. pqm_uninit(&process->pqm);
  288. err_process_pqm_init:
  289. hash_del_rcu(&process->kfd_processes);
  290. synchronize_rcu();
  291. mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
  292. err_mmu_notifier:
  293. mutex_destroy(&process->mutex);
  294. kfd_free_process_doorbells(process);
  295. err_alloc_doorbells:
  296. kfd_pasid_free(process->pasid);
  297. err_alloc_pasid:
  298. kfree(process);
  299. err_alloc_process:
  300. return ERR_PTR(err);
  301. }
  302. struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
  303. struct kfd_process *p)
  304. {
  305. struct kfd_process_device *pdd = NULL;
  306. list_for_each_entry(pdd, &p->per_device_data, per_device_list)
  307. if (pdd->dev == dev)
  308. return pdd;
  309. return NULL;
  310. }
  311. struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
  312. struct kfd_process *p)
  313. {
  314. struct kfd_process_device *pdd = NULL;
  315. pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
  316. if (!pdd)
  317. return NULL;
  318. pdd->dev = dev;
  319. INIT_LIST_HEAD(&pdd->qpd.queues_list);
  320. INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
  321. pdd->qpd.dqm = dev->dqm;
  322. pdd->qpd.pqm = &p->pqm;
  323. pdd->qpd.evicted = 0;
  324. pdd->process = p;
  325. pdd->bound = PDD_UNBOUND;
  326. pdd->already_dequeued = false;
  327. list_add(&pdd->per_device_list, &p->per_device_data);
  328. /* Create the GPUVM context for this specific device */
  329. if (dev->kfd2kgd->create_process_vm(dev->kgd, &pdd->vm,
  330. &p->kgd_process_info, &p->ef)) {
  331. pr_err("Failed to create process VM object\n");
  332. goto err_create_pdd;
  333. }
  334. return pdd;
  335. err_create_pdd:
  336. list_del(&pdd->per_device_list);
  337. kfree(pdd);
  338. return NULL;
  339. }
  340. /*
  341. * Direct the IOMMU to bind the process (specifically the pasid->mm)
  342. * to the device.
  343. * Unbinding occurs when the process dies or the device is removed.
  344. *
  345. * Assumes that the process lock is held.
  346. */
  347. struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
  348. struct kfd_process *p)
  349. {
  350. struct kfd_process_device *pdd;
  351. int err;
  352. pdd = kfd_get_process_device_data(dev, p);
  353. if (!pdd) {
  354. pr_err("Process device data doesn't exist\n");
  355. return ERR_PTR(-ENOMEM);
  356. }
  357. err = kfd_iommu_bind_process_to_device(pdd);
  358. if (err)
  359. return ERR_PTR(err);
  360. return pdd;
  361. }
  362. struct kfd_process_device *kfd_get_first_process_device_data(
  363. struct kfd_process *p)
  364. {
  365. return list_first_entry(&p->per_device_data,
  366. struct kfd_process_device,
  367. per_device_list);
  368. }
  369. struct kfd_process_device *kfd_get_next_process_device_data(
  370. struct kfd_process *p,
  371. struct kfd_process_device *pdd)
  372. {
  373. if (list_is_last(&pdd->per_device_list, &p->per_device_data))
  374. return NULL;
  375. return list_next_entry(pdd, per_device_list);
  376. }
  377. bool kfd_has_process_device_data(struct kfd_process *p)
  378. {
  379. return !(list_empty(&p->per_device_data));
  380. }
  381. /* This increments the process->ref counter. */
  382. struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
  383. {
  384. struct kfd_process *p, *ret_p = NULL;
  385. unsigned int temp;
  386. int idx = srcu_read_lock(&kfd_processes_srcu);
  387. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  388. if (p->pasid == pasid) {
  389. kref_get(&p->ref);
  390. ret_p = p;
  391. break;
  392. }
  393. }
  394. srcu_read_unlock(&kfd_processes_srcu, idx);
  395. return ret_p;
  396. }
  397. /* This increments the process->ref counter. */
  398. struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
  399. {
  400. struct kfd_process *p;
  401. int idx = srcu_read_lock(&kfd_processes_srcu);
  402. p = find_process_by_mm(mm);
  403. if (p)
  404. kref_get(&p->ref);
  405. srcu_read_unlock(&kfd_processes_srcu, idx);
  406. return p;
  407. }
  408. /* process_evict_queues - Evict all user queues of a process
  409. *
  410. * Eviction is reference-counted per process-device. This means multiple
  411. * evictions from different sources can be nested safely.
  412. */
  413. static int process_evict_queues(struct kfd_process *p)
  414. {
  415. struct kfd_process_device *pdd;
  416. int r = 0;
  417. unsigned int n_evicted = 0;
  418. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  419. r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
  420. &pdd->qpd);
  421. if (r) {
  422. pr_err("Failed to evict process queues\n");
  423. goto fail;
  424. }
  425. n_evicted++;
  426. }
  427. return r;
  428. fail:
  429. /* To keep state consistent, roll back partial eviction by
  430. * restoring queues
  431. */
  432. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  433. if (n_evicted == 0)
  434. break;
  435. if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
  436. &pdd->qpd))
  437. pr_err("Failed to restore queues\n");
  438. n_evicted--;
  439. }
  440. return r;
  441. }
  442. /* process_restore_queues - Restore all user queues of a process */
  443. static int process_restore_queues(struct kfd_process *p)
  444. {
  445. struct kfd_process_device *pdd;
  446. int r, ret = 0;
  447. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  448. r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
  449. &pdd->qpd);
  450. if (r) {
  451. pr_err("Failed to restore process queues\n");
  452. if (!ret)
  453. ret = r;
  454. }
  455. }
  456. return ret;
  457. }
  458. static void evict_process_worker(struct work_struct *work)
  459. {
  460. int ret;
  461. struct kfd_process *p;
  462. struct delayed_work *dwork;
  463. dwork = to_delayed_work(work);
  464. /* Process termination destroys this worker thread. So during the
  465. * lifetime of this thread, kfd_process p will be valid
  466. */
  467. p = container_of(dwork, struct kfd_process, eviction_work);
  468. WARN_ONCE(p->last_eviction_seqno != p->ef->seqno,
  469. "Eviction fence mismatch\n");
  470. /* Narrow window of overlap between restore and evict work
  471. * item is possible. Once amdgpu_amdkfd_gpuvm_restore_process_bos
  472. * unreserves KFD BOs, it is possible to evicted again. But
  473. * restore has few more steps of finish. So lets wait for any
  474. * previous restore work to complete
  475. */
  476. flush_delayed_work(&p->restore_work);
  477. pr_debug("Started evicting pasid %d\n", p->pasid);
  478. ret = process_evict_queues(p);
  479. if (!ret) {
  480. dma_fence_signal(p->ef);
  481. dma_fence_put(p->ef);
  482. p->ef = NULL;
  483. schedule_delayed_work(&p->restore_work,
  484. msecs_to_jiffies(PROCESS_RESTORE_TIME_MS));
  485. pr_debug("Finished evicting pasid %d\n", p->pasid);
  486. } else
  487. pr_err("Failed to evict queues of pasid %d\n", p->pasid);
  488. }
  489. static void restore_process_worker(struct work_struct *work)
  490. {
  491. struct delayed_work *dwork;
  492. struct kfd_process *p;
  493. struct kfd_process_device *pdd;
  494. int ret = 0;
  495. dwork = to_delayed_work(work);
  496. /* Process termination destroys this worker thread. So during the
  497. * lifetime of this thread, kfd_process p will be valid
  498. */
  499. p = container_of(dwork, struct kfd_process, restore_work);
  500. /* Call restore_process_bos on the first KGD device. This function
  501. * takes care of restoring the whole process including other devices.
  502. * Restore can fail if enough memory is not available. If so,
  503. * reschedule again.
  504. */
  505. pdd = list_first_entry(&p->per_device_data,
  506. struct kfd_process_device,
  507. per_device_list);
  508. pr_debug("Started restoring pasid %d\n", p->pasid);
  509. /* Setting last_restore_timestamp before successful restoration.
  510. * Otherwise this would have to be set by KGD (restore_process_bos)
  511. * before KFD BOs are unreserved. If not, the process can be evicted
  512. * again before the timestamp is set.
  513. * If restore fails, the timestamp will be set again in the next
  514. * attempt. This would mean that the minimum GPU quanta would be
  515. * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
  516. * functions)
  517. */
  518. p->last_restore_timestamp = get_jiffies_64();
  519. ret = pdd->dev->kfd2kgd->restore_process_bos(p->kgd_process_info,
  520. &p->ef);
  521. if (ret) {
  522. pr_debug("Failed to restore BOs of pasid %d, retry after %d ms\n",
  523. p->pasid, PROCESS_BACK_OFF_TIME_MS);
  524. ret = schedule_delayed_work(&p->restore_work,
  525. msecs_to_jiffies(PROCESS_BACK_OFF_TIME_MS));
  526. WARN(!ret, "reschedule restore work failed\n");
  527. return;
  528. }
  529. ret = process_restore_queues(p);
  530. if (!ret)
  531. pr_debug("Finished restoring pasid %d\n", p->pasid);
  532. else
  533. pr_err("Failed to restore queues of pasid %d\n", p->pasid);
  534. }
  535. void kfd_suspend_all_processes(void)
  536. {
  537. struct kfd_process *p;
  538. unsigned int temp;
  539. int idx = srcu_read_lock(&kfd_processes_srcu);
  540. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  541. cancel_delayed_work_sync(&p->eviction_work);
  542. cancel_delayed_work_sync(&p->restore_work);
  543. if (process_evict_queues(p))
  544. pr_err("Failed to suspend process %d\n", p->pasid);
  545. dma_fence_signal(p->ef);
  546. dma_fence_put(p->ef);
  547. p->ef = NULL;
  548. }
  549. srcu_read_unlock(&kfd_processes_srcu, idx);
  550. }
  551. int kfd_resume_all_processes(void)
  552. {
  553. struct kfd_process *p;
  554. unsigned int temp;
  555. int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
  556. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  557. if (!schedule_delayed_work(&p->restore_work, 0)) {
  558. pr_err("Restore process %d failed during resume\n",
  559. p->pasid);
  560. ret = -EFAULT;
  561. }
  562. }
  563. srcu_read_unlock(&kfd_processes_srcu, idx);
  564. return ret;
  565. }
  566. int kfd_reserved_mem_mmap(struct kfd_process *process,
  567. struct vm_area_struct *vma)
  568. {
  569. struct kfd_dev *dev = kfd_device_by_id(vma->vm_pgoff);
  570. struct kfd_process_device *pdd;
  571. struct qcm_process_device *qpd;
  572. if (!dev)
  573. return -EINVAL;
  574. if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
  575. pr_err("Incorrect CWSR mapping size.\n");
  576. return -EINVAL;
  577. }
  578. pdd = kfd_get_process_device_data(dev, process);
  579. if (!pdd)
  580. return -EINVAL;
  581. qpd = &pdd->qpd;
  582. qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  583. get_order(KFD_CWSR_TBA_TMA_SIZE));
  584. if (!qpd->cwsr_kaddr) {
  585. pr_err("Error allocating per process CWSR buffer.\n");
  586. return -ENOMEM;
  587. }
  588. vma->vm_flags |= VM_IO | VM_DONTCOPY | VM_DONTEXPAND
  589. | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP;
  590. /* Mapping pages to user process */
  591. return remap_pfn_range(vma, vma->vm_start,
  592. PFN_DOWN(__pa(qpd->cwsr_kaddr)),
  593. KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
  594. }
  595. void kfd_flush_tlb(struct kfd_process_device *pdd)
  596. {
  597. struct kfd_dev *dev = pdd->dev;
  598. const struct kfd2kgd_calls *f2g = dev->kfd2kgd;
  599. if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
  600. /* Nothing to flush until a VMID is assigned, which
  601. * only happens when the first queue is created.
  602. */
  603. if (pdd->qpd.vmid)
  604. f2g->invalidate_tlbs_vmid(dev->kgd, pdd->qpd.vmid);
  605. } else {
  606. f2g->invalidate_tlbs(dev->kgd, pdd->process->pasid);
  607. }
  608. }
  609. #if defined(CONFIG_DEBUG_FS)
  610. int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
  611. {
  612. struct kfd_process *p;
  613. unsigned int temp;
  614. int r = 0;
  615. int idx = srcu_read_lock(&kfd_processes_srcu);
  616. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  617. seq_printf(m, "Process %d PASID %d:\n",
  618. p->lead_thread->tgid, p->pasid);
  619. mutex_lock(&p->mutex);
  620. r = pqm_debugfs_mqds(m, &p->pqm);
  621. mutex_unlock(&p->mutex);
  622. if (r)
  623. break;
  624. }
  625. srcu_read_unlock(&kfd_processes_srcu, idx);
  626. return r;
  627. }
  628. #endif