amdgpu_amdkfd_gpuvm.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. /*
  2. * Copyright 2014-2018 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. #define pr_fmt(fmt) "kfd2kgd: " fmt
  23. #include <linux/list.h>
  24. #include <drm/drmP.h>
  25. #include "amdgpu_object.h"
  26. #include "amdgpu_vm.h"
  27. #include "amdgpu_amdkfd.h"
  28. /* Special VM and GART address alignment needed for VI pre-Fiji due to
  29. * a HW bug.
  30. */
  31. #define VI_BO_SIZE_ALIGN (0x8000)
  32. /* Impose limit on how much memory KFD can use */
  33. static struct {
  34. uint64_t max_system_mem_limit;
  35. int64_t system_mem_used;
  36. spinlock_t mem_limit_lock;
  37. } kfd_mem_limit;
  38. /* Struct used for amdgpu_amdkfd_bo_validate */
  39. struct amdgpu_vm_parser {
  40. uint32_t domain;
  41. bool wait;
  42. };
  43. static const char * const domain_bit_to_string[] = {
  44. "CPU",
  45. "GTT",
  46. "VRAM",
  47. "GDS",
  48. "GWS",
  49. "OA"
  50. };
  51. #define domain_string(domain) domain_bit_to_string[ffs(domain)-1]
  52. static inline struct amdgpu_device *get_amdgpu_device(struct kgd_dev *kgd)
  53. {
  54. return (struct amdgpu_device *)kgd;
  55. }
  56. static bool check_if_add_bo_to_vm(struct amdgpu_vm *avm,
  57. struct kgd_mem *mem)
  58. {
  59. struct kfd_bo_va_list *entry;
  60. list_for_each_entry(entry, &mem->bo_va_list, bo_list)
  61. if (entry->bo_va->base.vm == avm)
  62. return false;
  63. return true;
  64. }
  65. /* Set memory usage limits. Current, limits are
  66. * System (kernel) memory - 3/8th System RAM
  67. */
  68. void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
  69. {
  70. struct sysinfo si;
  71. uint64_t mem;
  72. si_meminfo(&si);
  73. mem = si.totalram - si.totalhigh;
  74. mem *= si.mem_unit;
  75. spin_lock_init(&kfd_mem_limit.mem_limit_lock);
  76. kfd_mem_limit.max_system_mem_limit = (mem >> 1) - (mem >> 3);
  77. pr_debug("Kernel memory limit %lluM\n",
  78. (kfd_mem_limit.max_system_mem_limit >> 20));
  79. }
  80. static int amdgpu_amdkfd_reserve_system_mem_limit(struct amdgpu_device *adev,
  81. uint64_t size, u32 domain)
  82. {
  83. size_t acc_size;
  84. int ret = 0;
  85. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  86. sizeof(struct amdgpu_bo));
  87. spin_lock(&kfd_mem_limit.mem_limit_lock);
  88. if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  89. if (kfd_mem_limit.system_mem_used + (acc_size + size) >
  90. kfd_mem_limit.max_system_mem_limit) {
  91. ret = -ENOMEM;
  92. goto err_no_mem;
  93. }
  94. kfd_mem_limit.system_mem_used += (acc_size + size);
  95. }
  96. err_no_mem:
  97. spin_unlock(&kfd_mem_limit.mem_limit_lock);
  98. return ret;
  99. }
  100. static void unreserve_system_mem_limit(struct amdgpu_device *adev,
  101. uint64_t size, u32 domain)
  102. {
  103. size_t acc_size;
  104. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  105. sizeof(struct amdgpu_bo));
  106. spin_lock(&kfd_mem_limit.mem_limit_lock);
  107. if (domain == AMDGPU_GEM_DOMAIN_GTT)
  108. kfd_mem_limit.system_mem_used -= (acc_size + size);
  109. WARN_ONCE(kfd_mem_limit.system_mem_used < 0,
  110. "kfd system memory accounting unbalanced");
  111. spin_unlock(&kfd_mem_limit.mem_limit_lock);
  112. }
  113. void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo)
  114. {
  115. spin_lock(&kfd_mem_limit.mem_limit_lock);
  116. if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GTT) {
  117. kfd_mem_limit.system_mem_used -=
  118. (bo->tbo.acc_size + amdgpu_bo_size(bo));
  119. }
  120. WARN_ONCE(kfd_mem_limit.system_mem_used < 0,
  121. "kfd system memory accounting unbalanced");
  122. spin_unlock(&kfd_mem_limit.mem_limit_lock);
  123. }
  124. /* amdgpu_amdkfd_remove_eviction_fence - Removes eviction fence(s) from BO's
  125. * reservation object.
  126. *
  127. * @bo: [IN] Remove eviction fence(s) from this BO
  128. * @ef: [IN] If ef is specified, then this eviction fence is removed if it
  129. * is present in the shared list.
  130. * @ef_list: [OUT] Returns list of eviction fences. These fences are removed
  131. * from BO's reservation object shared list.
  132. * @ef_count: [OUT] Number of fences in ef_list.
  133. *
  134. * NOTE: If called with ef_list, then amdgpu_amdkfd_add_eviction_fence must be
  135. * called to restore the eviction fences and to avoid memory leak. This is
  136. * useful for shared BOs.
  137. * NOTE: Must be called with BO reserved i.e. bo->tbo.resv->lock held.
  138. */
  139. static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo,
  140. struct amdgpu_amdkfd_fence *ef,
  141. struct amdgpu_amdkfd_fence ***ef_list,
  142. unsigned int *ef_count)
  143. {
  144. struct reservation_object_list *fobj;
  145. struct reservation_object *resv;
  146. unsigned int i = 0, j = 0, k = 0, shared_count;
  147. unsigned int count = 0;
  148. struct amdgpu_amdkfd_fence **fence_list;
  149. if (!ef && !ef_list)
  150. return -EINVAL;
  151. if (ef_list) {
  152. *ef_list = NULL;
  153. *ef_count = 0;
  154. }
  155. resv = bo->tbo.resv;
  156. fobj = reservation_object_get_list(resv);
  157. if (!fobj)
  158. return 0;
  159. preempt_disable();
  160. write_seqcount_begin(&resv->seq);
  161. /* Go through all the shared fences in the resevation object. If
  162. * ef is specified and it exists in the list, remove it and reduce the
  163. * count. If ef is not specified, then get the count of eviction fences
  164. * present.
  165. */
  166. shared_count = fobj->shared_count;
  167. for (i = 0; i < shared_count; ++i) {
  168. struct dma_fence *f;
  169. f = rcu_dereference_protected(fobj->shared[i],
  170. reservation_object_held(resv));
  171. if (ef) {
  172. if (f->context == ef->base.context) {
  173. dma_fence_put(f);
  174. fobj->shared_count--;
  175. } else {
  176. RCU_INIT_POINTER(fobj->shared[j++], f);
  177. }
  178. } else if (to_amdgpu_amdkfd_fence(f))
  179. count++;
  180. }
  181. write_seqcount_end(&resv->seq);
  182. preempt_enable();
  183. if (ef || !count)
  184. return 0;
  185. /* Alloc memory for count number of eviction fence pointers. Fill the
  186. * ef_list array and ef_count
  187. */
  188. fence_list = kcalloc(count, sizeof(struct amdgpu_amdkfd_fence *),
  189. GFP_KERNEL);
  190. if (!fence_list)
  191. return -ENOMEM;
  192. preempt_disable();
  193. write_seqcount_begin(&resv->seq);
  194. j = 0;
  195. for (i = 0; i < shared_count; ++i) {
  196. struct dma_fence *f;
  197. struct amdgpu_amdkfd_fence *efence;
  198. f = rcu_dereference_protected(fobj->shared[i],
  199. reservation_object_held(resv));
  200. efence = to_amdgpu_amdkfd_fence(f);
  201. if (efence) {
  202. fence_list[k++] = efence;
  203. fobj->shared_count--;
  204. } else {
  205. RCU_INIT_POINTER(fobj->shared[j++], f);
  206. }
  207. }
  208. write_seqcount_end(&resv->seq);
  209. preempt_enable();
  210. *ef_list = fence_list;
  211. *ef_count = k;
  212. return 0;
  213. }
  214. /* amdgpu_amdkfd_add_eviction_fence - Adds eviction fence(s) back into BO's
  215. * reservation object.
  216. *
  217. * @bo: [IN] Add eviction fences to this BO
  218. * @ef_list: [IN] List of eviction fences to be added
  219. * @ef_count: [IN] Number of fences in ef_list.
  220. *
  221. * NOTE: Must call amdgpu_amdkfd_remove_eviction_fence before calling this
  222. * function.
  223. */
  224. static void amdgpu_amdkfd_add_eviction_fence(struct amdgpu_bo *bo,
  225. struct amdgpu_amdkfd_fence **ef_list,
  226. unsigned int ef_count)
  227. {
  228. int i;
  229. if (!ef_list || !ef_count)
  230. return;
  231. for (i = 0; i < ef_count; i++) {
  232. amdgpu_bo_fence(bo, &ef_list[i]->base, true);
  233. /* Re-adding the fence takes an additional reference. Drop that
  234. * reference.
  235. */
  236. dma_fence_put(&ef_list[i]->base);
  237. }
  238. kfree(ef_list);
  239. }
  240. static int amdgpu_amdkfd_bo_validate(struct amdgpu_bo *bo, uint32_t domain,
  241. bool wait)
  242. {
  243. struct ttm_operation_ctx ctx = { false, false };
  244. int ret;
  245. if (WARN(amdgpu_ttm_tt_get_usermm(bo->tbo.ttm),
  246. "Called with userptr BO"))
  247. return -EINVAL;
  248. amdgpu_ttm_placement_from_domain(bo, domain);
  249. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  250. if (ret)
  251. goto validate_fail;
  252. if (wait) {
  253. struct amdgpu_amdkfd_fence **ef_list;
  254. unsigned int ef_count;
  255. ret = amdgpu_amdkfd_remove_eviction_fence(bo, NULL, &ef_list,
  256. &ef_count);
  257. if (ret)
  258. goto validate_fail;
  259. ttm_bo_wait(&bo->tbo, false, false);
  260. amdgpu_amdkfd_add_eviction_fence(bo, ef_list, ef_count);
  261. }
  262. validate_fail:
  263. return ret;
  264. }
  265. static int amdgpu_amdkfd_validate(void *param, struct amdgpu_bo *bo)
  266. {
  267. struct amdgpu_vm_parser *p = param;
  268. return amdgpu_amdkfd_bo_validate(bo, p->domain, p->wait);
  269. }
  270. /* vm_validate_pt_pd_bos - Validate page table and directory BOs
  271. *
  272. * Page directories are not updated here because huge page handling
  273. * during page table updates can invalidate page directory entries
  274. * again. Page directories are only updated after updating page
  275. * tables.
  276. */
  277. static int vm_validate_pt_pd_bos(struct amdkfd_vm *vm)
  278. {
  279. struct amdgpu_bo *pd = vm->base.root.base.bo;
  280. struct amdgpu_device *adev = amdgpu_ttm_adev(pd->tbo.bdev);
  281. struct amdgpu_vm_parser param;
  282. uint64_t addr, flags = AMDGPU_PTE_VALID;
  283. int ret;
  284. param.domain = AMDGPU_GEM_DOMAIN_VRAM;
  285. param.wait = false;
  286. ret = amdgpu_vm_validate_pt_bos(adev, &vm->base, amdgpu_amdkfd_validate,
  287. &param);
  288. if (ret) {
  289. pr_err("amdgpu: failed to validate PT BOs\n");
  290. return ret;
  291. }
  292. ret = amdgpu_amdkfd_validate(&param, pd);
  293. if (ret) {
  294. pr_err("amdgpu: failed to validate PD\n");
  295. return ret;
  296. }
  297. addr = amdgpu_bo_gpu_offset(vm->base.root.base.bo);
  298. amdgpu_gmc_get_vm_pde(adev, -1, &addr, &flags);
  299. vm->pd_phys_addr = addr;
  300. if (vm->base.use_cpu_for_update) {
  301. ret = amdgpu_bo_kmap(pd, NULL);
  302. if (ret) {
  303. pr_err("amdgpu: failed to kmap PD, ret=%d\n", ret);
  304. return ret;
  305. }
  306. }
  307. return 0;
  308. }
  309. static int sync_vm_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
  310. struct dma_fence *f)
  311. {
  312. int ret = amdgpu_sync_fence(adev, sync, f, false);
  313. /* Sync objects can't handle multiple GPUs (contexts) updating
  314. * sync->last_vm_update. Fortunately we don't need it for
  315. * KFD's purposes, so we can just drop that fence.
  316. */
  317. if (sync->last_vm_update) {
  318. dma_fence_put(sync->last_vm_update);
  319. sync->last_vm_update = NULL;
  320. }
  321. return ret;
  322. }
  323. static int vm_update_pds(struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  324. {
  325. struct amdgpu_bo *pd = vm->root.base.bo;
  326. struct amdgpu_device *adev = amdgpu_ttm_adev(pd->tbo.bdev);
  327. int ret;
  328. ret = amdgpu_vm_update_directories(adev, vm);
  329. if (ret)
  330. return ret;
  331. return sync_vm_fence(adev, sync, vm->last_update);
  332. }
  333. /* add_bo_to_vm - Add a BO to a VM
  334. *
  335. * Everything that needs to bo done only once when a BO is first added
  336. * to a VM. It can later be mapped and unmapped many times without
  337. * repeating these steps.
  338. *
  339. * 1. Allocate and initialize BO VA entry data structure
  340. * 2. Add BO to the VM
  341. * 3. Determine ASIC-specific PTE flags
  342. * 4. Alloc page tables and directories if needed
  343. * 4a. Validate new page tables and directories
  344. */
  345. static int add_bo_to_vm(struct amdgpu_device *adev, struct kgd_mem *mem,
  346. struct amdgpu_vm *avm, bool is_aql,
  347. struct kfd_bo_va_list **p_bo_va_entry)
  348. {
  349. int ret;
  350. struct kfd_bo_va_list *bo_va_entry;
  351. struct amdkfd_vm *kvm = container_of(avm,
  352. struct amdkfd_vm, base);
  353. struct amdgpu_bo *pd = avm->root.base.bo;
  354. struct amdgpu_bo *bo = mem->bo;
  355. uint64_t va = mem->va;
  356. struct list_head *list_bo_va = &mem->bo_va_list;
  357. unsigned long bo_size = bo->tbo.mem.size;
  358. if (!va) {
  359. pr_err("Invalid VA when adding BO to VM\n");
  360. return -EINVAL;
  361. }
  362. if (is_aql)
  363. va += bo_size;
  364. bo_va_entry = kzalloc(sizeof(*bo_va_entry), GFP_KERNEL);
  365. if (!bo_va_entry)
  366. return -ENOMEM;
  367. pr_debug("\t add VA 0x%llx - 0x%llx to vm %p\n", va,
  368. va + bo_size, avm);
  369. /* Add BO to VM internal data structures*/
  370. bo_va_entry->bo_va = amdgpu_vm_bo_add(adev, avm, bo);
  371. if (!bo_va_entry->bo_va) {
  372. ret = -EINVAL;
  373. pr_err("Failed to add BO object to VM. ret == %d\n",
  374. ret);
  375. goto err_vmadd;
  376. }
  377. bo_va_entry->va = va;
  378. bo_va_entry->pte_flags = amdgpu_gmc_get_pte_flags(adev,
  379. mem->mapping_flags);
  380. bo_va_entry->kgd_dev = (void *)adev;
  381. list_add(&bo_va_entry->bo_list, list_bo_va);
  382. if (p_bo_va_entry)
  383. *p_bo_va_entry = bo_va_entry;
  384. /* Allocate new page tables if needed and validate
  385. * them. Clearing of new page tables and validate need to wait
  386. * on move fences. We don't want that to trigger the eviction
  387. * fence, so remove it temporarily.
  388. */
  389. amdgpu_amdkfd_remove_eviction_fence(pd,
  390. kvm->process_info->eviction_fence,
  391. NULL, NULL);
  392. ret = amdgpu_vm_alloc_pts(adev, avm, va, amdgpu_bo_size(bo));
  393. if (ret) {
  394. pr_err("Failed to allocate pts, err=%d\n", ret);
  395. goto err_alloc_pts;
  396. }
  397. ret = vm_validate_pt_pd_bos(kvm);
  398. if (ret) {
  399. pr_err("validate_pt_pd_bos() failed\n");
  400. goto err_alloc_pts;
  401. }
  402. /* Add the eviction fence back */
  403. amdgpu_bo_fence(pd, &kvm->process_info->eviction_fence->base, true);
  404. return 0;
  405. err_alloc_pts:
  406. amdgpu_bo_fence(pd, &kvm->process_info->eviction_fence->base, true);
  407. amdgpu_vm_bo_rmv(adev, bo_va_entry->bo_va);
  408. list_del(&bo_va_entry->bo_list);
  409. err_vmadd:
  410. kfree(bo_va_entry);
  411. return ret;
  412. }
  413. static void remove_bo_from_vm(struct amdgpu_device *adev,
  414. struct kfd_bo_va_list *entry, unsigned long size)
  415. {
  416. pr_debug("\t remove VA 0x%llx - 0x%llx in entry %p\n",
  417. entry->va,
  418. entry->va + size, entry);
  419. amdgpu_vm_bo_rmv(adev, entry->bo_va);
  420. list_del(&entry->bo_list);
  421. kfree(entry);
  422. }
  423. static void add_kgd_mem_to_kfd_bo_list(struct kgd_mem *mem,
  424. struct amdkfd_process_info *process_info)
  425. {
  426. struct ttm_validate_buffer *entry = &mem->validate_list;
  427. struct amdgpu_bo *bo = mem->bo;
  428. INIT_LIST_HEAD(&entry->head);
  429. entry->shared = true;
  430. entry->bo = &bo->tbo;
  431. mutex_lock(&process_info->lock);
  432. list_add_tail(&entry->head, &process_info->kfd_bo_list);
  433. mutex_unlock(&process_info->lock);
  434. }
  435. /* Reserving a BO and its page table BOs must happen atomically to
  436. * avoid deadlocks. Some operations update multiple VMs at once. Track
  437. * all the reservation info in a context structure. Optionally a sync
  438. * object can track VM updates.
  439. */
  440. struct bo_vm_reservation_context {
  441. struct amdgpu_bo_list_entry kfd_bo; /* BO list entry for the KFD BO */
  442. unsigned int n_vms; /* Number of VMs reserved */
  443. struct amdgpu_bo_list_entry *vm_pd; /* Array of VM BO list entries */
  444. struct ww_acquire_ctx ticket; /* Reservation ticket */
  445. struct list_head list, duplicates; /* BO lists */
  446. struct amdgpu_sync *sync; /* Pointer to sync object */
  447. bool reserved; /* Whether BOs are reserved */
  448. };
  449. enum bo_vm_match {
  450. BO_VM_NOT_MAPPED = 0, /* Match VMs where a BO is not mapped */
  451. BO_VM_MAPPED, /* Match VMs where a BO is mapped */
  452. BO_VM_ALL, /* Match all VMs a BO was added to */
  453. };
  454. /**
  455. * reserve_bo_and_vm - reserve a BO and a VM unconditionally.
  456. * @mem: KFD BO structure.
  457. * @vm: the VM to reserve.
  458. * @ctx: the struct that will be used in unreserve_bo_and_vms().
  459. */
  460. static int reserve_bo_and_vm(struct kgd_mem *mem,
  461. struct amdgpu_vm *vm,
  462. struct bo_vm_reservation_context *ctx)
  463. {
  464. struct amdgpu_bo *bo = mem->bo;
  465. int ret;
  466. WARN_ON(!vm);
  467. ctx->reserved = false;
  468. ctx->n_vms = 1;
  469. ctx->sync = &mem->sync;
  470. INIT_LIST_HEAD(&ctx->list);
  471. INIT_LIST_HEAD(&ctx->duplicates);
  472. ctx->vm_pd = kcalloc(ctx->n_vms, sizeof(*ctx->vm_pd), GFP_KERNEL);
  473. if (!ctx->vm_pd)
  474. return -ENOMEM;
  475. ctx->kfd_bo.robj = bo;
  476. ctx->kfd_bo.priority = 0;
  477. ctx->kfd_bo.tv.bo = &bo->tbo;
  478. ctx->kfd_bo.tv.shared = true;
  479. ctx->kfd_bo.user_pages = NULL;
  480. list_add(&ctx->kfd_bo.tv.head, &ctx->list);
  481. amdgpu_vm_get_pd_bo(vm, &ctx->list, &ctx->vm_pd[0]);
  482. ret = ttm_eu_reserve_buffers(&ctx->ticket, &ctx->list,
  483. false, &ctx->duplicates);
  484. if (!ret)
  485. ctx->reserved = true;
  486. else {
  487. pr_err("Failed to reserve buffers in ttm\n");
  488. kfree(ctx->vm_pd);
  489. ctx->vm_pd = NULL;
  490. }
  491. return ret;
  492. }
  493. /**
  494. * reserve_bo_and_cond_vms - reserve a BO and some VMs conditionally
  495. * @mem: KFD BO structure.
  496. * @vm: the VM to reserve. If NULL, then all VMs associated with the BO
  497. * is used. Otherwise, a single VM associated with the BO.
  498. * @map_type: the mapping status that will be used to filter the VMs.
  499. * @ctx: the struct that will be used in unreserve_bo_and_vms().
  500. *
  501. * Returns 0 for success, negative for failure.
  502. */
  503. static int reserve_bo_and_cond_vms(struct kgd_mem *mem,
  504. struct amdgpu_vm *vm, enum bo_vm_match map_type,
  505. struct bo_vm_reservation_context *ctx)
  506. {
  507. struct amdgpu_bo *bo = mem->bo;
  508. struct kfd_bo_va_list *entry;
  509. unsigned int i;
  510. int ret;
  511. ctx->reserved = false;
  512. ctx->n_vms = 0;
  513. ctx->vm_pd = NULL;
  514. ctx->sync = &mem->sync;
  515. INIT_LIST_HEAD(&ctx->list);
  516. INIT_LIST_HEAD(&ctx->duplicates);
  517. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  518. if ((vm && vm != entry->bo_va->base.vm) ||
  519. (entry->is_mapped != map_type
  520. && map_type != BO_VM_ALL))
  521. continue;
  522. ctx->n_vms++;
  523. }
  524. if (ctx->n_vms != 0) {
  525. ctx->vm_pd = kcalloc(ctx->n_vms, sizeof(*ctx->vm_pd),
  526. GFP_KERNEL);
  527. if (!ctx->vm_pd)
  528. return -ENOMEM;
  529. }
  530. ctx->kfd_bo.robj = bo;
  531. ctx->kfd_bo.priority = 0;
  532. ctx->kfd_bo.tv.bo = &bo->tbo;
  533. ctx->kfd_bo.tv.shared = true;
  534. ctx->kfd_bo.user_pages = NULL;
  535. list_add(&ctx->kfd_bo.tv.head, &ctx->list);
  536. i = 0;
  537. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  538. if ((vm && vm != entry->bo_va->base.vm) ||
  539. (entry->is_mapped != map_type
  540. && map_type != BO_VM_ALL))
  541. continue;
  542. amdgpu_vm_get_pd_bo(entry->bo_va->base.vm, &ctx->list,
  543. &ctx->vm_pd[i]);
  544. i++;
  545. }
  546. ret = ttm_eu_reserve_buffers(&ctx->ticket, &ctx->list,
  547. false, &ctx->duplicates);
  548. if (!ret)
  549. ctx->reserved = true;
  550. else
  551. pr_err("Failed to reserve buffers in ttm.\n");
  552. if (ret) {
  553. kfree(ctx->vm_pd);
  554. ctx->vm_pd = NULL;
  555. }
  556. return ret;
  557. }
  558. /**
  559. * unreserve_bo_and_vms - Unreserve BO and VMs from a reservation context
  560. * @ctx: Reservation context to unreserve
  561. * @wait: Optionally wait for a sync object representing pending VM updates
  562. * @intr: Whether the wait is interruptible
  563. *
  564. * Also frees any resources allocated in
  565. * reserve_bo_and_(cond_)vm(s). Returns the status from
  566. * amdgpu_sync_wait.
  567. */
  568. static int unreserve_bo_and_vms(struct bo_vm_reservation_context *ctx,
  569. bool wait, bool intr)
  570. {
  571. int ret = 0;
  572. if (wait)
  573. ret = amdgpu_sync_wait(ctx->sync, intr);
  574. if (ctx->reserved)
  575. ttm_eu_backoff_reservation(&ctx->ticket, &ctx->list);
  576. kfree(ctx->vm_pd);
  577. ctx->sync = NULL;
  578. ctx->reserved = false;
  579. ctx->vm_pd = NULL;
  580. return ret;
  581. }
  582. static int unmap_bo_from_gpuvm(struct amdgpu_device *adev,
  583. struct kfd_bo_va_list *entry,
  584. struct amdgpu_sync *sync)
  585. {
  586. struct amdgpu_bo_va *bo_va = entry->bo_va;
  587. struct amdgpu_vm *vm = bo_va->base.vm;
  588. struct amdkfd_vm *kvm = container_of(vm, struct amdkfd_vm, base);
  589. struct amdgpu_bo *pd = vm->root.base.bo;
  590. /* Remove eviction fence from PD (and thereby from PTs too as
  591. * they share the resv. object). Otherwise during PT update
  592. * job (see amdgpu_vm_bo_update_mapping), eviction fence would
  593. * get added to job->sync object and job execution would
  594. * trigger the eviction fence.
  595. */
  596. amdgpu_amdkfd_remove_eviction_fence(pd,
  597. kvm->process_info->eviction_fence,
  598. NULL, NULL);
  599. amdgpu_vm_bo_unmap(adev, bo_va, entry->va);
  600. amdgpu_vm_clear_freed(adev, vm, &bo_va->last_pt_update);
  601. /* Add the eviction fence back */
  602. amdgpu_bo_fence(pd, &kvm->process_info->eviction_fence->base, true);
  603. sync_vm_fence(adev, sync, bo_va->last_pt_update);
  604. return 0;
  605. }
  606. static int update_gpuvm_pte(struct amdgpu_device *adev,
  607. struct kfd_bo_va_list *entry,
  608. struct amdgpu_sync *sync)
  609. {
  610. int ret;
  611. struct amdgpu_vm *vm;
  612. struct amdgpu_bo_va *bo_va;
  613. struct amdgpu_bo *bo;
  614. bo_va = entry->bo_va;
  615. vm = bo_va->base.vm;
  616. bo = bo_va->base.bo;
  617. /* Update the page tables */
  618. ret = amdgpu_vm_bo_update(adev, bo_va, false);
  619. if (ret) {
  620. pr_err("amdgpu_vm_bo_update failed\n");
  621. return ret;
  622. }
  623. return sync_vm_fence(adev, sync, bo_va->last_pt_update);
  624. }
  625. static int map_bo_to_gpuvm(struct amdgpu_device *adev,
  626. struct kfd_bo_va_list *entry, struct amdgpu_sync *sync)
  627. {
  628. int ret;
  629. /* Set virtual address for the allocation */
  630. ret = amdgpu_vm_bo_map(adev, entry->bo_va, entry->va, 0,
  631. amdgpu_bo_size(entry->bo_va->base.bo),
  632. entry->pte_flags);
  633. if (ret) {
  634. pr_err("Failed to map VA 0x%llx in vm. ret %d\n",
  635. entry->va, ret);
  636. return ret;
  637. }
  638. ret = update_gpuvm_pte(adev, entry, sync);
  639. if (ret) {
  640. pr_err("update_gpuvm_pte() failed\n");
  641. goto update_gpuvm_pte_failed;
  642. }
  643. return 0;
  644. update_gpuvm_pte_failed:
  645. unmap_bo_from_gpuvm(adev, entry, sync);
  646. return ret;
  647. }
  648. static int process_validate_vms(struct amdkfd_process_info *process_info)
  649. {
  650. struct amdkfd_vm *peer_vm;
  651. int ret;
  652. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  653. vm_list_node) {
  654. ret = vm_validate_pt_pd_bos(peer_vm);
  655. if (ret)
  656. return ret;
  657. }
  658. return 0;
  659. }
  660. static int process_update_pds(struct amdkfd_process_info *process_info,
  661. struct amdgpu_sync *sync)
  662. {
  663. struct amdkfd_vm *peer_vm;
  664. int ret;
  665. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  666. vm_list_node) {
  667. ret = vm_update_pds(&peer_vm->base, sync);
  668. if (ret)
  669. return ret;
  670. }
  671. return 0;
  672. }
  673. int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, void **vm,
  674. void **process_info,
  675. struct dma_fence **ef)
  676. {
  677. int ret;
  678. struct amdkfd_vm *new_vm;
  679. struct amdkfd_process_info *info;
  680. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  681. new_vm = kzalloc(sizeof(*new_vm), GFP_KERNEL);
  682. if (!new_vm)
  683. return -ENOMEM;
  684. /* Initialize the VM context, allocate the page directory and zero it */
  685. ret = amdgpu_vm_init(adev, &new_vm->base, AMDGPU_VM_CONTEXT_COMPUTE, 0);
  686. if (ret) {
  687. pr_err("Failed init vm ret %d\n", ret);
  688. goto vm_init_fail;
  689. }
  690. new_vm->adev = adev;
  691. if (!*process_info) {
  692. info = kzalloc(sizeof(*info), GFP_KERNEL);
  693. if (!info) {
  694. ret = -ENOMEM;
  695. goto alloc_process_info_fail;
  696. }
  697. mutex_init(&info->lock);
  698. INIT_LIST_HEAD(&info->vm_list_head);
  699. INIT_LIST_HEAD(&info->kfd_bo_list);
  700. info->eviction_fence =
  701. amdgpu_amdkfd_fence_create(dma_fence_context_alloc(1),
  702. current->mm);
  703. if (!info->eviction_fence) {
  704. pr_err("Failed to create eviction fence\n");
  705. goto create_evict_fence_fail;
  706. }
  707. *process_info = info;
  708. *ef = dma_fence_get(&info->eviction_fence->base);
  709. }
  710. new_vm->process_info = *process_info;
  711. mutex_lock(&new_vm->process_info->lock);
  712. list_add_tail(&new_vm->vm_list_node,
  713. &(new_vm->process_info->vm_list_head));
  714. new_vm->process_info->n_vms++;
  715. mutex_unlock(&new_vm->process_info->lock);
  716. *vm = (void *) new_vm;
  717. pr_debug("Created process vm %p\n", *vm);
  718. return ret;
  719. create_evict_fence_fail:
  720. mutex_destroy(&info->lock);
  721. kfree(info);
  722. alloc_process_info_fail:
  723. amdgpu_vm_fini(adev, &new_vm->base);
  724. vm_init_fail:
  725. kfree(new_vm);
  726. return ret;
  727. }
  728. void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm)
  729. {
  730. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  731. struct amdkfd_vm *kfd_vm = (struct amdkfd_vm *) vm;
  732. struct amdgpu_vm *avm = &kfd_vm->base;
  733. struct amdgpu_bo *pd;
  734. struct amdkfd_process_info *process_info;
  735. if (WARN_ON(!kgd || !vm))
  736. return;
  737. pr_debug("Destroying process vm %p\n", vm);
  738. /* Release eviction fence from PD */
  739. pd = avm->root.base.bo;
  740. amdgpu_bo_reserve(pd, false);
  741. amdgpu_bo_fence(pd, NULL, false);
  742. amdgpu_bo_unreserve(pd);
  743. process_info = kfd_vm->process_info;
  744. mutex_lock(&process_info->lock);
  745. process_info->n_vms--;
  746. list_del(&kfd_vm->vm_list_node);
  747. mutex_unlock(&process_info->lock);
  748. /* Release per-process resources */
  749. if (!process_info->n_vms) {
  750. WARN_ON(!list_empty(&process_info->kfd_bo_list));
  751. dma_fence_put(&process_info->eviction_fence->base);
  752. mutex_destroy(&process_info->lock);
  753. kfree(process_info);
  754. }
  755. /* Release the VM context */
  756. amdgpu_vm_fini(adev, avm);
  757. kfree(vm);
  758. }
  759. uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm)
  760. {
  761. struct amdkfd_vm *avm = (struct amdkfd_vm *)vm;
  762. return avm->pd_phys_addr >> AMDGPU_GPU_PAGE_SHIFT;
  763. }
  764. int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
  765. struct kgd_dev *kgd, uint64_t va, uint64_t size,
  766. void *vm, struct kgd_mem **mem,
  767. uint64_t *offset, uint32_t flags)
  768. {
  769. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  770. struct amdkfd_vm *kfd_vm = (struct amdkfd_vm *)vm;
  771. struct amdgpu_bo *bo;
  772. int byte_align;
  773. u32 alloc_domain;
  774. u64 alloc_flags;
  775. uint32_t mapping_flags;
  776. int ret;
  777. /*
  778. * Check on which domain to allocate BO
  779. */
  780. if (flags & ALLOC_MEM_FLAGS_VRAM) {
  781. alloc_domain = AMDGPU_GEM_DOMAIN_VRAM;
  782. alloc_flags = AMDGPU_GEM_CREATE_VRAM_CLEARED;
  783. alloc_flags |= (flags & ALLOC_MEM_FLAGS_PUBLIC) ?
  784. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED :
  785. AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
  786. } else if (flags & ALLOC_MEM_FLAGS_GTT) {
  787. alloc_domain = AMDGPU_GEM_DOMAIN_GTT;
  788. alloc_flags = 0;
  789. } else {
  790. return -EINVAL;
  791. }
  792. *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL);
  793. if (!*mem)
  794. return -ENOMEM;
  795. INIT_LIST_HEAD(&(*mem)->bo_va_list);
  796. mutex_init(&(*mem)->lock);
  797. (*mem)->aql_queue = !!(flags & ALLOC_MEM_FLAGS_AQL_QUEUE_MEM);
  798. /* Workaround for AQL queue wraparound bug. Map the same
  799. * memory twice. That means we only actually allocate half
  800. * the memory.
  801. */
  802. if ((*mem)->aql_queue)
  803. size = size >> 1;
  804. /* Workaround for TLB bug on older VI chips */
  805. byte_align = (adev->family == AMDGPU_FAMILY_VI &&
  806. adev->asic_type != CHIP_FIJI &&
  807. adev->asic_type != CHIP_POLARIS10 &&
  808. adev->asic_type != CHIP_POLARIS11) ?
  809. VI_BO_SIZE_ALIGN : 1;
  810. mapping_flags = AMDGPU_VM_PAGE_READABLE;
  811. if (flags & ALLOC_MEM_FLAGS_WRITABLE)
  812. mapping_flags |= AMDGPU_VM_PAGE_WRITEABLE;
  813. if (flags & ALLOC_MEM_FLAGS_EXECUTABLE)
  814. mapping_flags |= AMDGPU_VM_PAGE_EXECUTABLE;
  815. if (flags & ALLOC_MEM_FLAGS_COHERENT)
  816. mapping_flags |= AMDGPU_VM_MTYPE_UC;
  817. else
  818. mapping_flags |= AMDGPU_VM_MTYPE_NC;
  819. (*mem)->mapping_flags = mapping_flags;
  820. amdgpu_sync_create(&(*mem)->sync);
  821. ret = amdgpu_amdkfd_reserve_system_mem_limit(adev, size, alloc_domain);
  822. if (ret) {
  823. pr_debug("Insufficient system memory\n");
  824. goto err_reserve_system_mem;
  825. }
  826. pr_debug("\tcreate BO VA 0x%llx size 0x%llx domain %s\n",
  827. va, size, domain_string(alloc_domain));
  828. ret = amdgpu_bo_create(adev, size, byte_align,
  829. alloc_domain, alloc_flags, ttm_bo_type_device, NULL, &bo);
  830. if (ret) {
  831. pr_debug("Failed to create BO on domain %s. ret %d\n",
  832. domain_string(alloc_domain), ret);
  833. goto err_bo_create;
  834. }
  835. bo->kfd_bo = *mem;
  836. (*mem)->bo = bo;
  837. (*mem)->va = va;
  838. (*mem)->domain = alloc_domain;
  839. (*mem)->mapped_to_gpu_memory = 0;
  840. (*mem)->process_info = kfd_vm->process_info;
  841. add_kgd_mem_to_kfd_bo_list(*mem, kfd_vm->process_info);
  842. if (offset)
  843. *offset = amdgpu_bo_mmap_offset(bo);
  844. return 0;
  845. err_bo_create:
  846. unreserve_system_mem_limit(adev, size, alloc_domain);
  847. err_reserve_system_mem:
  848. mutex_destroy(&(*mem)->lock);
  849. kfree(*mem);
  850. return ret;
  851. }
  852. int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  853. struct kgd_dev *kgd, struct kgd_mem *mem)
  854. {
  855. struct amdkfd_process_info *process_info = mem->process_info;
  856. unsigned long bo_size = mem->bo->tbo.mem.size;
  857. struct kfd_bo_va_list *entry, *tmp;
  858. struct bo_vm_reservation_context ctx;
  859. struct ttm_validate_buffer *bo_list_entry;
  860. int ret;
  861. mutex_lock(&mem->lock);
  862. if (mem->mapped_to_gpu_memory > 0) {
  863. pr_debug("BO VA 0x%llx size 0x%lx is still mapped.\n",
  864. mem->va, bo_size);
  865. mutex_unlock(&mem->lock);
  866. return -EBUSY;
  867. }
  868. mutex_unlock(&mem->lock);
  869. /* lock is not needed after this, since mem is unused and will
  870. * be freed anyway
  871. */
  872. /* Make sure restore workers don't access the BO any more */
  873. bo_list_entry = &mem->validate_list;
  874. mutex_lock(&process_info->lock);
  875. list_del(&bo_list_entry->head);
  876. mutex_unlock(&process_info->lock);
  877. ret = reserve_bo_and_cond_vms(mem, NULL, BO_VM_ALL, &ctx);
  878. if (unlikely(ret))
  879. return ret;
  880. /* The eviction fence should be removed by the last unmap.
  881. * TODO: Log an error condition if the bo still has the eviction fence
  882. * attached
  883. */
  884. amdgpu_amdkfd_remove_eviction_fence(mem->bo,
  885. process_info->eviction_fence,
  886. NULL, NULL);
  887. pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
  888. mem->va + bo_size * (1 + mem->aql_queue));
  889. /* Remove from VM internal data structures */
  890. list_for_each_entry_safe(entry, tmp, &mem->bo_va_list, bo_list)
  891. remove_bo_from_vm((struct amdgpu_device *)entry->kgd_dev,
  892. entry, bo_size);
  893. ret = unreserve_bo_and_vms(&ctx, false, false);
  894. /* Free the sync object */
  895. amdgpu_sync_free(&mem->sync);
  896. /* Free the BO*/
  897. amdgpu_bo_unref(&mem->bo);
  898. mutex_destroy(&mem->lock);
  899. kfree(mem);
  900. return ret;
  901. }
  902. int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
  903. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm)
  904. {
  905. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  906. struct amdkfd_vm *kfd_vm = (struct amdkfd_vm *)vm;
  907. int ret;
  908. struct amdgpu_bo *bo;
  909. uint32_t domain;
  910. struct kfd_bo_va_list *entry;
  911. struct bo_vm_reservation_context ctx;
  912. struct kfd_bo_va_list *bo_va_entry = NULL;
  913. struct kfd_bo_va_list *bo_va_entry_aql = NULL;
  914. unsigned long bo_size;
  915. /* Make sure restore is not running concurrently.
  916. */
  917. mutex_lock(&mem->process_info->lock);
  918. mutex_lock(&mem->lock);
  919. bo = mem->bo;
  920. if (!bo) {
  921. pr_err("Invalid BO when mapping memory to GPU\n");
  922. ret = -EINVAL;
  923. goto out;
  924. }
  925. domain = mem->domain;
  926. bo_size = bo->tbo.mem.size;
  927. pr_debug("Map VA 0x%llx - 0x%llx to vm %p domain %s\n",
  928. mem->va,
  929. mem->va + bo_size * (1 + mem->aql_queue),
  930. vm, domain_string(domain));
  931. ret = reserve_bo_and_vm(mem, vm, &ctx);
  932. if (unlikely(ret))
  933. goto out;
  934. if (check_if_add_bo_to_vm((struct amdgpu_vm *)vm, mem)) {
  935. ret = add_bo_to_vm(adev, mem, (struct amdgpu_vm *)vm, false,
  936. &bo_va_entry);
  937. if (ret)
  938. goto add_bo_to_vm_failed;
  939. if (mem->aql_queue) {
  940. ret = add_bo_to_vm(adev, mem, (struct amdgpu_vm *)vm,
  941. true, &bo_va_entry_aql);
  942. if (ret)
  943. goto add_bo_to_vm_failed_aql;
  944. }
  945. } else {
  946. ret = vm_validate_pt_pd_bos((struct amdkfd_vm *)vm);
  947. if (unlikely(ret))
  948. goto add_bo_to_vm_failed;
  949. }
  950. if (mem->mapped_to_gpu_memory == 0) {
  951. /* Validate BO only once. The eviction fence gets added to BO
  952. * the first time it is mapped. Validate will wait for all
  953. * background evictions to complete.
  954. */
  955. ret = amdgpu_amdkfd_bo_validate(bo, domain, true);
  956. if (ret) {
  957. pr_debug("Validate failed\n");
  958. goto map_bo_to_gpuvm_failed;
  959. }
  960. }
  961. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  962. if (entry->bo_va->base.vm == vm && !entry->is_mapped) {
  963. pr_debug("\t map VA 0x%llx - 0x%llx in entry %p\n",
  964. entry->va, entry->va + bo_size,
  965. entry);
  966. ret = map_bo_to_gpuvm(adev, entry, ctx.sync);
  967. if (ret) {
  968. pr_err("Failed to map radeon bo to gpuvm\n");
  969. goto map_bo_to_gpuvm_failed;
  970. }
  971. ret = vm_update_pds(vm, ctx.sync);
  972. if (ret) {
  973. pr_err("Failed to update page directories\n");
  974. goto map_bo_to_gpuvm_failed;
  975. }
  976. entry->is_mapped = true;
  977. mem->mapped_to_gpu_memory++;
  978. pr_debug("\t INC mapping count %d\n",
  979. mem->mapped_to_gpu_memory);
  980. }
  981. }
  982. if (!amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) && !bo->pin_count)
  983. amdgpu_bo_fence(bo,
  984. &kfd_vm->process_info->eviction_fence->base,
  985. true);
  986. ret = unreserve_bo_and_vms(&ctx, false, false);
  987. goto out;
  988. map_bo_to_gpuvm_failed:
  989. if (bo_va_entry_aql)
  990. remove_bo_from_vm(adev, bo_va_entry_aql, bo_size);
  991. add_bo_to_vm_failed_aql:
  992. if (bo_va_entry)
  993. remove_bo_from_vm(adev, bo_va_entry, bo_size);
  994. add_bo_to_vm_failed:
  995. unreserve_bo_and_vms(&ctx, false, false);
  996. out:
  997. mutex_unlock(&mem->process_info->lock);
  998. mutex_unlock(&mem->lock);
  999. return ret;
  1000. }
  1001. int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  1002. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm)
  1003. {
  1004. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  1005. struct amdkfd_process_info *process_info =
  1006. ((struct amdkfd_vm *)vm)->process_info;
  1007. unsigned long bo_size = mem->bo->tbo.mem.size;
  1008. struct kfd_bo_va_list *entry;
  1009. struct bo_vm_reservation_context ctx;
  1010. int ret;
  1011. mutex_lock(&mem->lock);
  1012. ret = reserve_bo_and_cond_vms(mem, vm, BO_VM_MAPPED, &ctx);
  1013. if (unlikely(ret))
  1014. goto out;
  1015. /* If no VMs were reserved, it means the BO wasn't actually mapped */
  1016. if (ctx.n_vms == 0) {
  1017. ret = -EINVAL;
  1018. goto unreserve_out;
  1019. }
  1020. ret = vm_validate_pt_pd_bos((struct amdkfd_vm *)vm);
  1021. if (unlikely(ret))
  1022. goto unreserve_out;
  1023. pr_debug("Unmap VA 0x%llx - 0x%llx from vm %p\n",
  1024. mem->va,
  1025. mem->va + bo_size * (1 + mem->aql_queue),
  1026. vm);
  1027. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  1028. if (entry->bo_va->base.vm == vm && entry->is_mapped) {
  1029. pr_debug("\t unmap VA 0x%llx - 0x%llx from entry %p\n",
  1030. entry->va,
  1031. entry->va + bo_size,
  1032. entry);
  1033. ret = unmap_bo_from_gpuvm(adev, entry, ctx.sync);
  1034. if (ret == 0) {
  1035. entry->is_mapped = false;
  1036. } else {
  1037. pr_err("failed to unmap VA 0x%llx\n",
  1038. mem->va);
  1039. goto unreserve_out;
  1040. }
  1041. mem->mapped_to_gpu_memory--;
  1042. pr_debug("\t DEC mapping count %d\n",
  1043. mem->mapped_to_gpu_memory);
  1044. }
  1045. }
  1046. /* If BO is unmapped from all VMs, unfence it. It can be evicted if
  1047. * required.
  1048. */
  1049. if (mem->mapped_to_gpu_memory == 0 &&
  1050. !amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) && !mem->bo->pin_count)
  1051. amdgpu_amdkfd_remove_eviction_fence(mem->bo,
  1052. process_info->eviction_fence,
  1053. NULL, NULL);
  1054. unreserve_out:
  1055. unreserve_bo_and_vms(&ctx, false, false);
  1056. out:
  1057. mutex_unlock(&mem->lock);
  1058. return ret;
  1059. }
  1060. int amdgpu_amdkfd_gpuvm_sync_memory(
  1061. struct kgd_dev *kgd, struct kgd_mem *mem, bool intr)
  1062. {
  1063. struct amdgpu_sync sync;
  1064. int ret;
  1065. amdgpu_sync_create(&sync);
  1066. mutex_lock(&mem->lock);
  1067. amdgpu_sync_clone(&mem->sync, &sync);
  1068. mutex_unlock(&mem->lock);
  1069. ret = amdgpu_sync_wait(&sync, intr);
  1070. amdgpu_sync_free(&sync);
  1071. return ret;
  1072. }
  1073. int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
  1074. struct kgd_mem *mem, void **kptr, uint64_t *size)
  1075. {
  1076. int ret;
  1077. struct amdgpu_bo *bo = mem->bo;
  1078. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
  1079. pr_err("userptr can't be mapped to kernel\n");
  1080. return -EINVAL;
  1081. }
  1082. /* delete kgd_mem from kfd_bo_list to avoid re-validating
  1083. * this BO in BO's restoring after eviction.
  1084. */
  1085. mutex_lock(&mem->process_info->lock);
  1086. ret = amdgpu_bo_reserve(bo, true);
  1087. if (ret) {
  1088. pr_err("Failed to reserve bo. ret %d\n", ret);
  1089. goto bo_reserve_failed;
  1090. }
  1091. ret = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT, NULL);
  1092. if (ret) {
  1093. pr_err("Failed to pin bo. ret %d\n", ret);
  1094. goto pin_failed;
  1095. }
  1096. ret = amdgpu_bo_kmap(bo, kptr);
  1097. if (ret) {
  1098. pr_err("Failed to map bo to kernel. ret %d\n", ret);
  1099. goto kmap_failed;
  1100. }
  1101. amdgpu_amdkfd_remove_eviction_fence(
  1102. bo, mem->process_info->eviction_fence, NULL, NULL);
  1103. list_del_init(&mem->validate_list.head);
  1104. if (size)
  1105. *size = amdgpu_bo_size(bo);
  1106. amdgpu_bo_unreserve(bo);
  1107. mutex_unlock(&mem->process_info->lock);
  1108. return 0;
  1109. kmap_failed:
  1110. amdgpu_bo_unpin(bo);
  1111. pin_failed:
  1112. amdgpu_bo_unreserve(bo);
  1113. bo_reserve_failed:
  1114. mutex_unlock(&mem->process_info->lock);
  1115. return ret;
  1116. }
  1117. /** amdgpu_amdkfd_gpuvm_restore_process_bos - Restore all BOs for the given
  1118. * KFD process identified by process_info
  1119. *
  1120. * @process_info: amdkfd_process_info of the KFD process
  1121. *
  1122. * After memory eviction, restore thread calls this function. The function
  1123. * should be called when the Process is still valid. BO restore involves -
  1124. *
  1125. * 1. Release old eviction fence and create new one
  1126. * 2. Get two copies of PD BO list from all the VMs. Keep one copy as pd_list.
  1127. * 3 Use the second PD list and kfd_bo_list to create a list (ctx.list) of
  1128. * BOs that need to be reserved.
  1129. * 4. Reserve all the BOs
  1130. * 5. Validate of PD and PT BOs.
  1131. * 6. Validate all KFD BOs using kfd_bo_list and Map them and add new fence
  1132. * 7. Add fence to all PD and PT BOs.
  1133. * 8. Unreserve all BOs
  1134. */
  1135. int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence **ef)
  1136. {
  1137. struct amdgpu_bo_list_entry *pd_bo_list;
  1138. struct amdkfd_process_info *process_info = info;
  1139. struct amdkfd_vm *peer_vm;
  1140. struct kgd_mem *mem;
  1141. struct bo_vm_reservation_context ctx;
  1142. struct amdgpu_amdkfd_fence *new_fence;
  1143. int ret = 0, i;
  1144. struct list_head duplicate_save;
  1145. struct amdgpu_sync sync_obj;
  1146. INIT_LIST_HEAD(&duplicate_save);
  1147. INIT_LIST_HEAD(&ctx.list);
  1148. INIT_LIST_HEAD(&ctx.duplicates);
  1149. pd_bo_list = kcalloc(process_info->n_vms,
  1150. sizeof(struct amdgpu_bo_list_entry),
  1151. GFP_KERNEL);
  1152. if (!pd_bo_list)
  1153. return -ENOMEM;
  1154. i = 0;
  1155. mutex_lock(&process_info->lock);
  1156. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1157. vm_list_node)
  1158. amdgpu_vm_get_pd_bo(&peer_vm->base, &ctx.list,
  1159. &pd_bo_list[i++]);
  1160. /* Reserve all BOs and page tables/directory. Add all BOs from
  1161. * kfd_bo_list to ctx.list
  1162. */
  1163. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1164. validate_list.head) {
  1165. list_add_tail(&mem->resv_list.head, &ctx.list);
  1166. mem->resv_list.bo = mem->validate_list.bo;
  1167. mem->resv_list.shared = mem->validate_list.shared;
  1168. }
  1169. ret = ttm_eu_reserve_buffers(&ctx.ticket, &ctx.list,
  1170. false, &duplicate_save);
  1171. if (ret) {
  1172. pr_debug("Memory eviction: TTM Reserve Failed. Try again\n");
  1173. goto ttm_reserve_fail;
  1174. }
  1175. amdgpu_sync_create(&sync_obj);
  1176. /* Validate PDs and PTs */
  1177. ret = process_validate_vms(process_info);
  1178. if (ret)
  1179. goto validate_map_fail;
  1180. /* Wait for PD/PTs validate to finish */
  1181. /* FIXME: I think this isn't needed */
  1182. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1183. vm_list_node) {
  1184. struct amdgpu_bo *bo = peer_vm->base.root.base.bo;
  1185. ttm_bo_wait(&bo->tbo, false, false);
  1186. }
  1187. /* Validate BOs and map them to GPUVM (update VM page tables). */
  1188. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1189. validate_list.head) {
  1190. struct amdgpu_bo *bo = mem->bo;
  1191. uint32_t domain = mem->domain;
  1192. struct kfd_bo_va_list *bo_va_entry;
  1193. ret = amdgpu_amdkfd_bo_validate(bo, domain, false);
  1194. if (ret) {
  1195. pr_debug("Memory eviction: Validate BOs failed. Try again\n");
  1196. goto validate_map_fail;
  1197. }
  1198. list_for_each_entry(bo_va_entry, &mem->bo_va_list,
  1199. bo_list) {
  1200. ret = update_gpuvm_pte((struct amdgpu_device *)
  1201. bo_va_entry->kgd_dev,
  1202. bo_va_entry,
  1203. &sync_obj);
  1204. if (ret) {
  1205. pr_debug("Memory eviction: update PTE failed. Try again\n");
  1206. goto validate_map_fail;
  1207. }
  1208. }
  1209. }
  1210. /* Update page directories */
  1211. ret = process_update_pds(process_info, &sync_obj);
  1212. if (ret) {
  1213. pr_debug("Memory eviction: update PDs failed. Try again\n");
  1214. goto validate_map_fail;
  1215. }
  1216. amdgpu_sync_wait(&sync_obj, false);
  1217. /* Release old eviction fence and create new one, because fence only
  1218. * goes from unsignaled to signaled, fence cannot be reused.
  1219. * Use context and mm from the old fence.
  1220. */
  1221. new_fence = amdgpu_amdkfd_fence_create(
  1222. process_info->eviction_fence->base.context,
  1223. process_info->eviction_fence->mm);
  1224. if (!new_fence) {
  1225. pr_err("Failed to create eviction fence\n");
  1226. ret = -ENOMEM;
  1227. goto validate_map_fail;
  1228. }
  1229. dma_fence_put(&process_info->eviction_fence->base);
  1230. process_info->eviction_fence = new_fence;
  1231. *ef = dma_fence_get(&new_fence->base);
  1232. /* Wait for validate to finish and attach new eviction fence */
  1233. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1234. validate_list.head)
  1235. ttm_bo_wait(&mem->bo->tbo, false, false);
  1236. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1237. validate_list.head)
  1238. amdgpu_bo_fence(mem->bo,
  1239. &process_info->eviction_fence->base, true);
  1240. /* Attach eviction fence to PD / PT BOs */
  1241. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1242. vm_list_node) {
  1243. struct amdgpu_bo *bo = peer_vm->base.root.base.bo;
  1244. amdgpu_bo_fence(bo, &process_info->eviction_fence->base, true);
  1245. }
  1246. validate_map_fail:
  1247. ttm_eu_backoff_reservation(&ctx.ticket, &ctx.list);
  1248. amdgpu_sync_free(&sync_obj);
  1249. ttm_reserve_fail:
  1250. mutex_unlock(&process_info->lock);
  1251. kfree(pd_bo_list);
  1252. return ret;
  1253. }