amdgpu_amdkfd_gpuvm.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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 amdgpu_vm *vm)
  278. {
  279. struct amdgpu_bo *pd = vm->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, 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->root.base.bo);
  298. amdgpu_gmc_get_vm_pde(adev, -1, &addr, &flags);
  299. vm->pd_phys_addr = addr;
  300. if (vm->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 *vm, 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 amdgpu_bo *pd = vm->root.base.bo;
  352. struct amdgpu_bo *bo = mem->bo;
  353. uint64_t va = mem->va;
  354. struct list_head *list_bo_va = &mem->bo_va_list;
  355. unsigned long bo_size = bo->tbo.mem.size;
  356. if (!va) {
  357. pr_err("Invalid VA when adding BO to VM\n");
  358. return -EINVAL;
  359. }
  360. if (is_aql)
  361. va += bo_size;
  362. bo_va_entry = kzalloc(sizeof(*bo_va_entry), GFP_KERNEL);
  363. if (!bo_va_entry)
  364. return -ENOMEM;
  365. pr_debug("\t add VA 0x%llx - 0x%llx to vm %p\n", va,
  366. va + bo_size, vm);
  367. /* Add BO to VM internal data structures*/
  368. bo_va_entry->bo_va = amdgpu_vm_bo_add(adev, vm, bo);
  369. if (!bo_va_entry->bo_va) {
  370. ret = -EINVAL;
  371. pr_err("Failed to add BO object to VM. ret == %d\n",
  372. ret);
  373. goto err_vmadd;
  374. }
  375. bo_va_entry->va = va;
  376. bo_va_entry->pte_flags = amdgpu_gmc_get_pte_flags(adev,
  377. mem->mapping_flags);
  378. bo_va_entry->kgd_dev = (void *)adev;
  379. list_add(&bo_va_entry->bo_list, list_bo_va);
  380. if (p_bo_va_entry)
  381. *p_bo_va_entry = bo_va_entry;
  382. /* Allocate new page tables if needed and validate
  383. * them. Clearing of new page tables and validate need to wait
  384. * on move fences. We don't want that to trigger the eviction
  385. * fence, so remove it temporarily.
  386. */
  387. amdgpu_amdkfd_remove_eviction_fence(pd,
  388. vm->process_info->eviction_fence,
  389. NULL, NULL);
  390. ret = amdgpu_vm_alloc_pts(adev, vm, va, amdgpu_bo_size(bo));
  391. if (ret) {
  392. pr_err("Failed to allocate pts, err=%d\n", ret);
  393. goto err_alloc_pts;
  394. }
  395. ret = vm_validate_pt_pd_bos(vm);
  396. if (ret) {
  397. pr_err("validate_pt_pd_bos() failed\n");
  398. goto err_alloc_pts;
  399. }
  400. /* Add the eviction fence back */
  401. amdgpu_bo_fence(pd, &vm->process_info->eviction_fence->base, true);
  402. return 0;
  403. err_alloc_pts:
  404. amdgpu_bo_fence(pd, &vm->process_info->eviction_fence->base, true);
  405. amdgpu_vm_bo_rmv(adev, bo_va_entry->bo_va);
  406. list_del(&bo_va_entry->bo_list);
  407. err_vmadd:
  408. kfree(bo_va_entry);
  409. return ret;
  410. }
  411. static void remove_bo_from_vm(struct amdgpu_device *adev,
  412. struct kfd_bo_va_list *entry, unsigned long size)
  413. {
  414. pr_debug("\t remove VA 0x%llx - 0x%llx in entry %p\n",
  415. entry->va,
  416. entry->va + size, entry);
  417. amdgpu_vm_bo_rmv(adev, entry->bo_va);
  418. list_del(&entry->bo_list);
  419. kfree(entry);
  420. }
  421. static void add_kgd_mem_to_kfd_bo_list(struct kgd_mem *mem,
  422. struct amdkfd_process_info *process_info)
  423. {
  424. struct ttm_validate_buffer *entry = &mem->validate_list;
  425. struct amdgpu_bo *bo = mem->bo;
  426. INIT_LIST_HEAD(&entry->head);
  427. entry->shared = true;
  428. entry->bo = &bo->tbo;
  429. mutex_lock(&process_info->lock);
  430. list_add_tail(&entry->head, &process_info->kfd_bo_list);
  431. mutex_unlock(&process_info->lock);
  432. }
  433. /* Reserving a BO and its page table BOs must happen atomically to
  434. * avoid deadlocks. Some operations update multiple VMs at once. Track
  435. * all the reservation info in a context structure. Optionally a sync
  436. * object can track VM updates.
  437. */
  438. struct bo_vm_reservation_context {
  439. struct amdgpu_bo_list_entry kfd_bo; /* BO list entry for the KFD BO */
  440. unsigned int n_vms; /* Number of VMs reserved */
  441. struct amdgpu_bo_list_entry *vm_pd; /* Array of VM BO list entries */
  442. struct ww_acquire_ctx ticket; /* Reservation ticket */
  443. struct list_head list, duplicates; /* BO lists */
  444. struct amdgpu_sync *sync; /* Pointer to sync object */
  445. bool reserved; /* Whether BOs are reserved */
  446. };
  447. enum bo_vm_match {
  448. BO_VM_NOT_MAPPED = 0, /* Match VMs where a BO is not mapped */
  449. BO_VM_MAPPED, /* Match VMs where a BO is mapped */
  450. BO_VM_ALL, /* Match all VMs a BO was added to */
  451. };
  452. /**
  453. * reserve_bo_and_vm - reserve a BO and a VM unconditionally.
  454. * @mem: KFD BO structure.
  455. * @vm: the VM to reserve.
  456. * @ctx: the struct that will be used in unreserve_bo_and_vms().
  457. */
  458. static int reserve_bo_and_vm(struct kgd_mem *mem,
  459. struct amdgpu_vm *vm,
  460. struct bo_vm_reservation_context *ctx)
  461. {
  462. struct amdgpu_bo *bo = mem->bo;
  463. int ret;
  464. WARN_ON(!vm);
  465. ctx->reserved = false;
  466. ctx->n_vms = 1;
  467. ctx->sync = &mem->sync;
  468. INIT_LIST_HEAD(&ctx->list);
  469. INIT_LIST_HEAD(&ctx->duplicates);
  470. ctx->vm_pd = kcalloc(ctx->n_vms, sizeof(*ctx->vm_pd), GFP_KERNEL);
  471. if (!ctx->vm_pd)
  472. return -ENOMEM;
  473. ctx->kfd_bo.robj = bo;
  474. ctx->kfd_bo.priority = 0;
  475. ctx->kfd_bo.tv.bo = &bo->tbo;
  476. ctx->kfd_bo.tv.shared = true;
  477. ctx->kfd_bo.user_pages = NULL;
  478. list_add(&ctx->kfd_bo.tv.head, &ctx->list);
  479. amdgpu_vm_get_pd_bo(vm, &ctx->list, &ctx->vm_pd[0]);
  480. ret = ttm_eu_reserve_buffers(&ctx->ticket, &ctx->list,
  481. false, &ctx->duplicates);
  482. if (!ret)
  483. ctx->reserved = true;
  484. else {
  485. pr_err("Failed to reserve buffers in ttm\n");
  486. kfree(ctx->vm_pd);
  487. ctx->vm_pd = NULL;
  488. }
  489. return ret;
  490. }
  491. /**
  492. * reserve_bo_and_cond_vms - reserve a BO and some VMs conditionally
  493. * @mem: KFD BO structure.
  494. * @vm: the VM to reserve. If NULL, then all VMs associated with the BO
  495. * is used. Otherwise, a single VM associated with the BO.
  496. * @map_type: the mapping status that will be used to filter the VMs.
  497. * @ctx: the struct that will be used in unreserve_bo_and_vms().
  498. *
  499. * Returns 0 for success, negative for failure.
  500. */
  501. static int reserve_bo_and_cond_vms(struct kgd_mem *mem,
  502. struct amdgpu_vm *vm, enum bo_vm_match map_type,
  503. struct bo_vm_reservation_context *ctx)
  504. {
  505. struct amdgpu_bo *bo = mem->bo;
  506. struct kfd_bo_va_list *entry;
  507. unsigned int i;
  508. int ret;
  509. ctx->reserved = false;
  510. ctx->n_vms = 0;
  511. ctx->vm_pd = NULL;
  512. ctx->sync = &mem->sync;
  513. INIT_LIST_HEAD(&ctx->list);
  514. INIT_LIST_HEAD(&ctx->duplicates);
  515. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  516. if ((vm && vm != entry->bo_va->base.vm) ||
  517. (entry->is_mapped != map_type
  518. && map_type != BO_VM_ALL))
  519. continue;
  520. ctx->n_vms++;
  521. }
  522. if (ctx->n_vms != 0) {
  523. ctx->vm_pd = kcalloc(ctx->n_vms, sizeof(*ctx->vm_pd),
  524. GFP_KERNEL);
  525. if (!ctx->vm_pd)
  526. return -ENOMEM;
  527. }
  528. ctx->kfd_bo.robj = bo;
  529. ctx->kfd_bo.priority = 0;
  530. ctx->kfd_bo.tv.bo = &bo->tbo;
  531. ctx->kfd_bo.tv.shared = true;
  532. ctx->kfd_bo.user_pages = NULL;
  533. list_add(&ctx->kfd_bo.tv.head, &ctx->list);
  534. i = 0;
  535. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  536. if ((vm && vm != entry->bo_va->base.vm) ||
  537. (entry->is_mapped != map_type
  538. && map_type != BO_VM_ALL))
  539. continue;
  540. amdgpu_vm_get_pd_bo(entry->bo_va->base.vm, &ctx->list,
  541. &ctx->vm_pd[i]);
  542. i++;
  543. }
  544. ret = ttm_eu_reserve_buffers(&ctx->ticket, &ctx->list,
  545. false, &ctx->duplicates);
  546. if (!ret)
  547. ctx->reserved = true;
  548. else
  549. pr_err("Failed to reserve buffers in ttm.\n");
  550. if (ret) {
  551. kfree(ctx->vm_pd);
  552. ctx->vm_pd = NULL;
  553. }
  554. return ret;
  555. }
  556. /**
  557. * unreserve_bo_and_vms - Unreserve BO and VMs from a reservation context
  558. * @ctx: Reservation context to unreserve
  559. * @wait: Optionally wait for a sync object representing pending VM updates
  560. * @intr: Whether the wait is interruptible
  561. *
  562. * Also frees any resources allocated in
  563. * reserve_bo_and_(cond_)vm(s). Returns the status from
  564. * amdgpu_sync_wait.
  565. */
  566. static int unreserve_bo_and_vms(struct bo_vm_reservation_context *ctx,
  567. bool wait, bool intr)
  568. {
  569. int ret = 0;
  570. if (wait)
  571. ret = amdgpu_sync_wait(ctx->sync, intr);
  572. if (ctx->reserved)
  573. ttm_eu_backoff_reservation(&ctx->ticket, &ctx->list);
  574. kfree(ctx->vm_pd);
  575. ctx->sync = NULL;
  576. ctx->reserved = false;
  577. ctx->vm_pd = NULL;
  578. return ret;
  579. }
  580. static int unmap_bo_from_gpuvm(struct amdgpu_device *adev,
  581. struct kfd_bo_va_list *entry,
  582. struct amdgpu_sync *sync)
  583. {
  584. struct amdgpu_bo_va *bo_va = entry->bo_va;
  585. struct amdgpu_vm *vm = bo_va->base.vm;
  586. struct amdgpu_bo *pd = vm->root.base.bo;
  587. /* Remove eviction fence from PD (and thereby from PTs too as
  588. * they share the resv. object). Otherwise during PT update
  589. * job (see amdgpu_vm_bo_update_mapping), eviction fence would
  590. * get added to job->sync object and job execution would
  591. * trigger the eviction fence.
  592. */
  593. amdgpu_amdkfd_remove_eviction_fence(pd,
  594. vm->process_info->eviction_fence,
  595. NULL, NULL);
  596. amdgpu_vm_bo_unmap(adev, bo_va, entry->va);
  597. amdgpu_vm_clear_freed(adev, vm, &bo_va->last_pt_update);
  598. /* Add the eviction fence back */
  599. amdgpu_bo_fence(pd, &vm->process_info->eviction_fence->base, true);
  600. sync_vm_fence(adev, sync, bo_va->last_pt_update);
  601. return 0;
  602. }
  603. static int update_gpuvm_pte(struct amdgpu_device *adev,
  604. struct kfd_bo_va_list *entry,
  605. struct amdgpu_sync *sync)
  606. {
  607. int ret;
  608. struct amdgpu_vm *vm;
  609. struct amdgpu_bo_va *bo_va;
  610. struct amdgpu_bo *bo;
  611. bo_va = entry->bo_va;
  612. vm = bo_va->base.vm;
  613. bo = bo_va->base.bo;
  614. /* Update the page tables */
  615. ret = amdgpu_vm_bo_update(adev, bo_va, false);
  616. if (ret) {
  617. pr_err("amdgpu_vm_bo_update failed\n");
  618. return ret;
  619. }
  620. return sync_vm_fence(adev, sync, bo_va->last_pt_update);
  621. }
  622. static int map_bo_to_gpuvm(struct amdgpu_device *adev,
  623. struct kfd_bo_va_list *entry, struct amdgpu_sync *sync)
  624. {
  625. int ret;
  626. /* Set virtual address for the allocation */
  627. ret = amdgpu_vm_bo_map(adev, entry->bo_va, entry->va, 0,
  628. amdgpu_bo_size(entry->bo_va->base.bo),
  629. entry->pte_flags);
  630. if (ret) {
  631. pr_err("Failed to map VA 0x%llx in vm. ret %d\n",
  632. entry->va, ret);
  633. return ret;
  634. }
  635. ret = update_gpuvm_pte(adev, entry, sync);
  636. if (ret) {
  637. pr_err("update_gpuvm_pte() failed\n");
  638. goto update_gpuvm_pte_failed;
  639. }
  640. return 0;
  641. update_gpuvm_pte_failed:
  642. unmap_bo_from_gpuvm(adev, entry, sync);
  643. return ret;
  644. }
  645. static int process_validate_vms(struct amdkfd_process_info *process_info)
  646. {
  647. struct amdgpu_vm *peer_vm;
  648. int ret;
  649. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  650. vm_list_node) {
  651. ret = vm_validate_pt_pd_bos(peer_vm);
  652. if (ret)
  653. return ret;
  654. }
  655. return 0;
  656. }
  657. static int process_update_pds(struct amdkfd_process_info *process_info,
  658. struct amdgpu_sync *sync)
  659. {
  660. struct amdgpu_vm *peer_vm;
  661. int ret;
  662. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  663. vm_list_node) {
  664. ret = vm_update_pds(peer_vm, sync);
  665. if (ret)
  666. return ret;
  667. }
  668. return 0;
  669. }
  670. static int init_kfd_vm(struct amdgpu_vm *vm, void **process_info,
  671. struct dma_fence **ef)
  672. {
  673. struct amdkfd_process_info *info = NULL;
  674. int ret;
  675. if (!*process_info) {
  676. info = kzalloc(sizeof(*info), GFP_KERNEL);
  677. if (!info)
  678. return -ENOMEM;
  679. mutex_init(&info->lock);
  680. INIT_LIST_HEAD(&info->vm_list_head);
  681. INIT_LIST_HEAD(&info->kfd_bo_list);
  682. info->eviction_fence =
  683. amdgpu_amdkfd_fence_create(dma_fence_context_alloc(1),
  684. current->mm);
  685. if (!info->eviction_fence) {
  686. pr_err("Failed to create eviction fence\n");
  687. ret = -ENOMEM;
  688. goto create_evict_fence_fail;
  689. }
  690. *process_info = info;
  691. *ef = dma_fence_get(&info->eviction_fence->base);
  692. }
  693. vm->process_info = *process_info;
  694. /* Validate page directory and attach eviction fence */
  695. ret = amdgpu_bo_reserve(vm->root.base.bo, true);
  696. if (ret)
  697. goto reserve_pd_fail;
  698. ret = vm_validate_pt_pd_bos(vm);
  699. if (ret) {
  700. pr_err("validate_pt_pd_bos() failed\n");
  701. goto validate_pd_fail;
  702. }
  703. ret = ttm_bo_wait(&vm->root.base.bo->tbo, false, false);
  704. if (ret)
  705. goto wait_pd_fail;
  706. amdgpu_bo_fence(vm->root.base.bo,
  707. &vm->process_info->eviction_fence->base, true);
  708. amdgpu_bo_unreserve(vm->root.base.bo);
  709. /* Update process info */
  710. mutex_lock(&vm->process_info->lock);
  711. list_add_tail(&vm->vm_list_node,
  712. &(vm->process_info->vm_list_head));
  713. vm->process_info->n_vms++;
  714. mutex_unlock(&vm->process_info->lock);
  715. return 0;
  716. wait_pd_fail:
  717. validate_pd_fail:
  718. amdgpu_bo_unreserve(vm->root.base.bo);
  719. reserve_pd_fail:
  720. vm->process_info = NULL;
  721. if (info) {
  722. /* Two fence references: one in info and one in *ef */
  723. dma_fence_put(&info->eviction_fence->base);
  724. dma_fence_put(*ef);
  725. *ef = NULL;
  726. *process_info = NULL;
  727. create_evict_fence_fail:
  728. mutex_destroy(&info->lock);
  729. kfree(info);
  730. }
  731. return ret;
  732. }
  733. int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, void **vm,
  734. void **process_info,
  735. struct dma_fence **ef)
  736. {
  737. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  738. struct amdgpu_vm *new_vm;
  739. int ret;
  740. new_vm = kzalloc(sizeof(*new_vm), GFP_KERNEL);
  741. if (!new_vm)
  742. return -ENOMEM;
  743. /* Initialize AMDGPU part of the VM */
  744. ret = amdgpu_vm_init(adev, new_vm, AMDGPU_VM_CONTEXT_COMPUTE, 0);
  745. if (ret) {
  746. pr_err("Failed init vm ret %d\n", ret);
  747. goto amdgpu_vm_init_fail;
  748. }
  749. /* Initialize KFD part of the VM and process info */
  750. ret = init_kfd_vm(new_vm, process_info, ef);
  751. if (ret)
  752. goto init_kfd_vm_fail;
  753. *vm = (void *) new_vm;
  754. return 0;
  755. init_kfd_vm_fail:
  756. amdgpu_vm_fini(adev, new_vm);
  757. amdgpu_vm_init_fail:
  758. kfree(new_vm);
  759. return ret;
  760. }
  761. int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd,
  762. struct file *filp,
  763. void **vm, void **process_info,
  764. struct dma_fence **ef)
  765. {
  766. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  767. struct drm_file *drm_priv = filp->private_data;
  768. struct amdgpu_fpriv *drv_priv = drm_priv->driver_priv;
  769. struct amdgpu_vm *avm = &drv_priv->vm;
  770. int ret;
  771. /* Already a compute VM? */
  772. if (avm->process_info)
  773. return -EINVAL;
  774. /* Convert VM into a compute VM */
  775. ret = amdgpu_vm_make_compute(adev, avm);
  776. if (ret)
  777. return ret;
  778. /* Initialize KFD part of the VM and process info */
  779. ret = init_kfd_vm(avm, process_info, ef);
  780. if (ret)
  781. return ret;
  782. *vm = (void *)avm;
  783. return 0;
  784. }
  785. void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
  786. struct amdgpu_vm *vm)
  787. {
  788. struct amdkfd_process_info *process_info = vm->process_info;
  789. struct amdgpu_bo *pd = vm->root.base.bo;
  790. if (!process_info)
  791. return;
  792. /* Release eviction fence from PD */
  793. amdgpu_bo_reserve(pd, false);
  794. amdgpu_bo_fence(pd, NULL, false);
  795. amdgpu_bo_unreserve(pd);
  796. /* Update process info */
  797. mutex_lock(&process_info->lock);
  798. process_info->n_vms--;
  799. list_del(&vm->vm_list_node);
  800. mutex_unlock(&process_info->lock);
  801. /* Release per-process resources when last compute VM is destroyed */
  802. if (!process_info->n_vms) {
  803. WARN_ON(!list_empty(&process_info->kfd_bo_list));
  804. dma_fence_put(&process_info->eviction_fence->base);
  805. mutex_destroy(&process_info->lock);
  806. kfree(process_info);
  807. }
  808. }
  809. void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm)
  810. {
  811. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  812. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  813. if (WARN_ON(!kgd || !vm))
  814. return;
  815. pr_debug("Destroying process vm %p\n", vm);
  816. /* Release the VM context */
  817. amdgpu_vm_fini(adev, avm);
  818. kfree(vm);
  819. }
  820. uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm)
  821. {
  822. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  823. return avm->pd_phys_addr >> AMDGPU_GPU_PAGE_SHIFT;
  824. }
  825. int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
  826. struct kgd_dev *kgd, uint64_t va, uint64_t size,
  827. void *vm, struct kgd_mem **mem,
  828. uint64_t *offset, uint32_t flags)
  829. {
  830. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  831. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  832. struct amdgpu_bo *bo;
  833. struct amdgpu_bo_param bp;
  834. int byte_align;
  835. u32 alloc_domain;
  836. u64 alloc_flags;
  837. uint32_t mapping_flags;
  838. int ret;
  839. /*
  840. * Check on which domain to allocate BO
  841. */
  842. if (flags & ALLOC_MEM_FLAGS_VRAM) {
  843. alloc_domain = AMDGPU_GEM_DOMAIN_VRAM;
  844. alloc_flags = AMDGPU_GEM_CREATE_VRAM_CLEARED;
  845. alloc_flags |= (flags & ALLOC_MEM_FLAGS_PUBLIC) ?
  846. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED :
  847. AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
  848. } else if (flags & ALLOC_MEM_FLAGS_GTT) {
  849. alloc_domain = AMDGPU_GEM_DOMAIN_GTT;
  850. alloc_flags = 0;
  851. } else {
  852. return -EINVAL;
  853. }
  854. *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL);
  855. if (!*mem)
  856. return -ENOMEM;
  857. INIT_LIST_HEAD(&(*mem)->bo_va_list);
  858. mutex_init(&(*mem)->lock);
  859. (*mem)->aql_queue = !!(flags & ALLOC_MEM_FLAGS_AQL_QUEUE_MEM);
  860. /* Workaround for AQL queue wraparound bug. Map the same
  861. * memory twice. That means we only actually allocate half
  862. * the memory.
  863. */
  864. if ((*mem)->aql_queue)
  865. size = size >> 1;
  866. /* Workaround for TLB bug on older VI chips */
  867. byte_align = (adev->family == AMDGPU_FAMILY_VI &&
  868. adev->asic_type != CHIP_FIJI &&
  869. adev->asic_type != CHIP_POLARIS10 &&
  870. adev->asic_type != CHIP_POLARIS11) ?
  871. VI_BO_SIZE_ALIGN : 1;
  872. mapping_flags = AMDGPU_VM_PAGE_READABLE;
  873. if (flags & ALLOC_MEM_FLAGS_WRITABLE)
  874. mapping_flags |= AMDGPU_VM_PAGE_WRITEABLE;
  875. if (flags & ALLOC_MEM_FLAGS_EXECUTABLE)
  876. mapping_flags |= AMDGPU_VM_PAGE_EXECUTABLE;
  877. if (flags & ALLOC_MEM_FLAGS_COHERENT)
  878. mapping_flags |= AMDGPU_VM_MTYPE_UC;
  879. else
  880. mapping_flags |= AMDGPU_VM_MTYPE_NC;
  881. (*mem)->mapping_flags = mapping_flags;
  882. amdgpu_sync_create(&(*mem)->sync);
  883. ret = amdgpu_amdkfd_reserve_system_mem_limit(adev, size, alloc_domain);
  884. if (ret) {
  885. pr_debug("Insufficient system memory\n");
  886. goto err_reserve_system_mem;
  887. }
  888. pr_debug("\tcreate BO VA 0x%llx size 0x%llx domain %s\n",
  889. va, size, domain_string(alloc_domain));
  890. memset(&bp, 0, sizeof(bp));
  891. bp.size = size;
  892. bp.byte_align = byte_align;
  893. bp.domain = alloc_domain;
  894. bp.flags = alloc_flags;
  895. bp.type = ttm_bo_type_device;
  896. bp.resv = NULL;
  897. ret = amdgpu_bo_create(adev, &bp, &bo);
  898. if (ret) {
  899. pr_debug("Failed to create BO on domain %s. ret %d\n",
  900. domain_string(alloc_domain), ret);
  901. goto err_bo_create;
  902. }
  903. bo->kfd_bo = *mem;
  904. (*mem)->bo = bo;
  905. (*mem)->va = va;
  906. (*mem)->domain = alloc_domain;
  907. (*mem)->mapped_to_gpu_memory = 0;
  908. (*mem)->process_info = avm->process_info;
  909. add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info);
  910. if (offset)
  911. *offset = amdgpu_bo_mmap_offset(bo);
  912. return 0;
  913. err_bo_create:
  914. unreserve_system_mem_limit(adev, size, alloc_domain);
  915. err_reserve_system_mem:
  916. mutex_destroy(&(*mem)->lock);
  917. kfree(*mem);
  918. return ret;
  919. }
  920. int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  921. struct kgd_dev *kgd, struct kgd_mem *mem)
  922. {
  923. struct amdkfd_process_info *process_info = mem->process_info;
  924. unsigned long bo_size = mem->bo->tbo.mem.size;
  925. struct kfd_bo_va_list *entry, *tmp;
  926. struct bo_vm_reservation_context ctx;
  927. struct ttm_validate_buffer *bo_list_entry;
  928. int ret;
  929. mutex_lock(&mem->lock);
  930. if (mem->mapped_to_gpu_memory > 0) {
  931. pr_debug("BO VA 0x%llx size 0x%lx is still mapped.\n",
  932. mem->va, bo_size);
  933. mutex_unlock(&mem->lock);
  934. return -EBUSY;
  935. }
  936. mutex_unlock(&mem->lock);
  937. /* lock is not needed after this, since mem is unused and will
  938. * be freed anyway
  939. */
  940. /* Make sure restore workers don't access the BO any more */
  941. bo_list_entry = &mem->validate_list;
  942. mutex_lock(&process_info->lock);
  943. list_del(&bo_list_entry->head);
  944. mutex_unlock(&process_info->lock);
  945. ret = reserve_bo_and_cond_vms(mem, NULL, BO_VM_ALL, &ctx);
  946. if (unlikely(ret))
  947. return ret;
  948. /* The eviction fence should be removed by the last unmap.
  949. * TODO: Log an error condition if the bo still has the eviction fence
  950. * attached
  951. */
  952. amdgpu_amdkfd_remove_eviction_fence(mem->bo,
  953. process_info->eviction_fence,
  954. NULL, NULL);
  955. pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
  956. mem->va + bo_size * (1 + mem->aql_queue));
  957. /* Remove from VM internal data structures */
  958. list_for_each_entry_safe(entry, tmp, &mem->bo_va_list, bo_list)
  959. remove_bo_from_vm((struct amdgpu_device *)entry->kgd_dev,
  960. entry, bo_size);
  961. ret = unreserve_bo_and_vms(&ctx, false, false);
  962. /* Free the sync object */
  963. amdgpu_sync_free(&mem->sync);
  964. /* Free the BO*/
  965. amdgpu_bo_unref(&mem->bo);
  966. mutex_destroy(&mem->lock);
  967. kfree(mem);
  968. return ret;
  969. }
  970. int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
  971. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm)
  972. {
  973. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  974. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  975. int ret;
  976. struct amdgpu_bo *bo;
  977. uint32_t domain;
  978. struct kfd_bo_va_list *entry;
  979. struct bo_vm_reservation_context ctx;
  980. struct kfd_bo_va_list *bo_va_entry = NULL;
  981. struct kfd_bo_va_list *bo_va_entry_aql = NULL;
  982. unsigned long bo_size;
  983. /* Make sure restore is not running concurrently.
  984. */
  985. mutex_lock(&mem->process_info->lock);
  986. mutex_lock(&mem->lock);
  987. bo = mem->bo;
  988. if (!bo) {
  989. pr_err("Invalid BO when mapping memory to GPU\n");
  990. ret = -EINVAL;
  991. goto out;
  992. }
  993. domain = mem->domain;
  994. bo_size = bo->tbo.mem.size;
  995. pr_debug("Map VA 0x%llx - 0x%llx to vm %p domain %s\n",
  996. mem->va,
  997. mem->va + bo_size * (1 + mem->aql_queue),
  998. vm, domain_string(domain));
  999. ret = reserve_bo_and_vm(mem, vm, &ctx);
  1000. if (unlikely(ret))
  1001. goto out;
  1002. if (check_if_add_bo_to_vm(avm, mem)) {
  1003. ret = add_bo_to_vm(adev, mem, avm, false,
  1004. &bo_va_entry);
  1005. if (ret)
  1006. goto add_bo_to_vm_failed;
  1007. if (mem->aql_queue) {
  1008. ret = add_bo_to_vm(adev, mem, avm,
  1009. true, &bo_va_entry_aql);
  1010. if (ret)
  1011. goto add_bo_to_vm_failed_aql;
  1012. }
  1013. } else {
  1014. ret = vm_validate_pt_pd_bos(avm);
  1015. if (unlikely(ret))
  1016. goto add_bo_to_vm_failed;
  1017. }
  1018. if (mem->mapped_to_gpu_memory == 0) {
  1019. /* Validate BO only once. The eviction fence gets added to BO
  1020. * the first time it is mapped. Validate will wait for all
  1021. * background evictions to complete.
  1022. */
  1023. ret = amdgpu_amdkfd_bo_validate(bo, domain, true);
  1024. if (ret) {
  1025. pr_debug("Validate failed\n");
  1026. goto map_bo_to_gpuvm_failed;
  1027. }
  1028. }
  1029. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  1030. if (entry->bo_va->base.vm == vm && !entry->is_mapped) {
  1031. pr_debug("\t map VA 0x%llx - 0x%llx in entry %p\n",
  1032. entry->va, entry->va + bo_size,
  1033. entry);
  1034. ret = map_bo_to_gpuvm(adev, entry, ctx.sync);
  1035. if (ret) {
  1036. pr_err("Failed to map radeon bo to gpuvm\n");
  1037. goto map_bo_to_gpuvm_failed;
  1038. }
  1039. ret = vm_update_pds(vm, ctx.sync);
  1040. if (ret) {
  1041. pr_err("Failed to update page directories\n");
  1042. goto map_bo_to_gpuvm_failed;
  1043. }
  1044. entry->is_mapped = true;
  1045. mem->mapped_to_gpu_memory++;
  1046. pr_debug("\t INC mapping count %d\n",
  1047. mem->mapped_to_gpu_memory);
  1048. }
  1049. }
  1050. if (!amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) && !bo->pin_count)
  1051. amdgpu_bo_fence(bo,
  1052. &avm->process_info->eviction_fence->base,
  1053. true);
  1054. ret = unreserve_bo_and_vms(&ctx, false, false);
  1055. goto out;
  1056. map_bo_to_gpuvm_failed:
  1057. if (bo_va_entry_aql)
  1058. remove_bo_from_vm(adev, bo_va_entry_aql, bo_size);
  1059. add_bo_to_vm_failed_aql:
  1060. if (bo_va_entry)
  1061. remove_bo_from_vm(adev, bo_va_entry, bo_size);
  1062. add_bo_to_vm_failed:
  1063. unreserve_bo_and_vms(&ctx, false, false);
  1064. out:
  1065. mutex_unlock(&mem->process_info->lock);
  1066. mutex_unlock(&mem->lock);
  1067. return ret;
  1068. }
  1069. int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  1070. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm)
  1071. {
  1072. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  1073. struct amdkfd_process_info *process_info =
  1074. ((struct amdgpu_vm *)vm)->process_info;
  1075. unsigned long bo_size = mem->bo->tbo.mem.size;
  1076. struct kfd_bo_va_list *entry;
  1077. struct bo_vm_reservation_context ctx;
  1078. int ret;
  1079. mutex_lock(&mem->lock);
  1080. ret = reserve_bo_and_cond_vms(mem, vm, BO_VM_MAPPED, &ctx);
  1081. if (unlikely(ret))
  1082. goto out;
  1083. /* If no VMs were reserved, it means the BO wasn't actually mapped */
  1084. if (ctx.n_vms == 0) {
  1085. ret = -EINVAL;
  1086. goto unreserve_out;
  1087. }
  1088. ret = vm_validate_pt_pd_bos((struct amdgpu_vm *)vm);
  1089. if (unlikely(ret))
  1090. goto unreserve_out;
  1091. pr_debug("Unmap VA 0x%llx - 0x%llx from vm %p\n",
  1092. mem->va,
  1093. mem->va + bo_size * (1 + mem->aql_queue),
  1094. vm);
  1095. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  1096. if (entry->bo_va->base.vm == vm && entry->is_mapped) {
  1097. pr_debug("\t unmap VA 0x%llx - 0x%llx from entry %p\n",
  1098. entry->va,
  1099. entry->va + bo_size,
  1100. entry);
  1101. ret = unmap_bo_from_gpuvm(adev, entry, ctx.sync);
  1102. if (ret == 0) {
  1103. entry->is_mapped = false;
  1104. } else {
  1105. pr_err("failed to unmap VA 0x%llx\n",
  1106. mem->va);
  1107. goto unreserve_out;
  1108. }
  1109. mem->mapped_to_gpu_memory--;
  1110. pr_debug("\t DEC mapping count %d\n",
  1111. mem->mapped_to_gpu_memory);
  1112. }
  1113. }
  1114. /* If BO is unmapped from all VMs, unfence it. It can be evicted if
  1115. * required.
  1116. */
  1117. if (mem->mapped_to_gpu_memory == 0 &&
  1118. !amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) && !mem->bo->pin_count)
  1119. amdgpu_amdkfd_remove_eviction_fence(mem->bo,
  1120. process_info->eviction_fence,
  1121. NULL, NULL);
  1122. unreserve_out:
  1123. unreserve_bo_and_vms(&ctx, false, false);
  1124. out:
  1125. mutex_unlock(&mem->lock);
  1126. return ret;
  1127. }
  1128. int amdgpu_amdkfd_gpuvm_sync_memory(
  1129. struct kgd_dev *kgd, struct kgd_mem *mem, bool intr)
  1130. {
  1131. struct amdgpu_sync sync;
  1132. int ret;
  1133. amdgpu_sync_create(&sync);
  1134. mutex_lock(&mem->lock);
  1135. amdgpu_sync_clone(&mem->sync, &sync);
  1136. mutex_unlock(&mem->lock);
  1137. ret = amdgpu_sync_wait(&sync, intr);
  1138. amdgpu_sync_free(&sync);
  1139. return ret;
  1140. }
  1141. int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
  1142. struct kgd_mem *mem, void **kptr, uint64_t *size)
  1143. {
  1144. int ret;
  1145. struct amdgpu_bo *bo = mem->bo;
  1146. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
  1147. pr_err("userptr can't be mapped to kernel\n");
  1148. return -EINVAL;
  1149. }
  1150. /* delete kgd_mem from kfd_bo_list to avoid re-validating
  1151. * this BO in BO's restoring after eviction.
  1152. */
  1153. mutex_lock(&mem->process_info->lock);
  1154. ret = amdgpu_bo_reserve(bo, true);
  1155. if (ret) {
  1156. pr_err("Failed to reserve bo. ret %d\n", ret);
  1157. goto bo_reserve_failed;
  1158. }
  1159. ret = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT, NULL);
  1160. if (ret) {
  1161. pr_err("Failed to pin bo. ret %d\n", ret);
  1162. goto pin_failed;
  1163. }
  1164. ret = amdgpu_bo_kmap(bo, kptr);
  1165. if (ret) {
  1166. pr_err("Failed to map bo to kernel. ret %d\n", ret);
  1167. goto kmap_failed;
  1168. }
  1169. amdgpu_amdkfd_remove_eviction_fence(
  1170. bo, mem->process_info->eviction_fence, NULL, NULL);
  1171. list_del_init(&mem->validate_list.head);
  1172. if (size)
  1173. *size = amdgpu_bo_size(bo);
  1174. amdgpu_bo_unreserve(bo);
  1175. mutex_unlock(&mem->process_info->lock);
  1176. return 0;
  1177. kmap_failed:
  1178. amdgpu_bo_unpin(bo);
  1179. pin_failed:
  1180. amdgpu_bo_unreserve(bo);
  1181. bo_reserve_failed:
  1182. mutex_unlock(&mem->process_info->lock);
  1183. return ret;
  1184. }
  1185. /** amdgpu_amdkfd_gpuvm_restore_process_bos - Restore all BOs for the given
  1186. * KFD process identified by process_info
  1187. *
  1188. * @process_info: amdkfd_process_info of the KFD process
  1189. *
  1190. * After memory eviction, restore thread calls this function. The function
  1191. * should be called when the Process is still valid. BO restore involves -
  1192. *
  1193. * 1. Release old eviction fence and create new one
  1194. * 2. Get two copies of PD BO list from all the VMs. Keep one copy as pd_list.
  1195. * 3 Use the second PD list and kfd_bo_list to create a list (ctx.list) of
  1196. * BOs that need to be reserved.
  1197. * 4. Reserve all the BOs
  1198. * 5. Validate of PD and PT BOs.
  1199. * 6. Validate all KFD BOs using kfd_bo_list and Map them and add new fence
  1200. * 7. Add fence to all PD and PT BOs.
  1201. * 8. Unreserve all BOs
  1202. */
  1203. int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence **ef)
  1204. {
  1205. struct amdgpu_bo_list_entry *pd_bo_list;
  1206. struct amdkfd_process_info *process_info = info;
  1207. struct amdgpu_vm *peer_vm;
  1208. struct kgd_mem *mem;
  1209. struct bo_vm_reservation_context ctx;
  1210. struct amdgpu_amdkfd_fence *new_fence;
  1211. int ret = 0, i;
  1212. struct list_head duplicate_save;
  1213. struct amdgpu_sync sync_obj;
  1214. INIT_LIST_HEAD(&duplicate_save);
  1215. INIT_LIST_HEAD(&ctx.list);
  1216. INIT_LIST_HEAD(&ctx.duplicates);
  1217. pd_bo_list = kcalloc(process_info->n_vms,
  1218. sizeof(struct amdgpu_bo_list_entry),
  1219. GFP_KERNEL);
  1220. if (!pd_bo_list)
  1221. return -ENOMEM;
  1222. i = 0;
  1223. mutex_lock(&process_info->lock);
  1224. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1225. vm_list_node)
  1226. amdgpu_vm_get_pd_bo(peer_vm, &ctx.list, &pd_bo_list[i++]);
  1227. /* Reserve all BOs and page tables/directory. Add all BOs from
  1228. * kfd_bo_list to ctx.list
  1229. */
  1230. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1231. validate_list.head) {
  1232. list_add_tail(&mem->resv_list.head, &ctx.list);
  1233. mem->resv_list.bo = mem->validate_list.bo;
  1234. mem->resv_list.shared = mem->validate_list.shared;
  1235. }
  1236. ret = ttm_eu_reserve_buffers(&ctx.ticket, &ctx.list,
  1237. false, &duplicate_save);
  1238. if (ret) {
  1239. pr_debug("Memory eviction: TTM Reserve Failed. Try again\n");
  1240. goto ttm_reserve_fail;
  1241. }
  1242. amdgpu_sync_create(&sync_obj);
  1243. /* Validate PDs and PTs */
  1244. ret = process_validate_vms(process_info);
  1245. if (ret)
  1246. goto validate_map_fail;
  1247. /* Wait for PD/PTs validate to finish */
  1248. /* FIXME: I think this isn't needed */
  1249. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1250. vm_list_node) {
  1251. struct amdgpu_bo *bo = peer_vm->root.base.bo;
  1252. ttm_bo_wait(&bo->tbo, false, false);
  1253. }
  1254. /* Validate BOs and map them to GPUVM (update VM page tables). */
  1255. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1256. validate_list.head) {
  1257. struct amdgpu_bo *bo = mem->bo;
  1258. uint32_t domain = mem->domain;
  1259. struct kfd_bo_va_list *bo_va_entry;
  1260. ret = amdgpu_amdkfd_bo_validate(bo, domain, false);
  1261. if (ret) {
  1262. pr_debug("Memory eviction: Validate BOs failed. Try again\n");
  1263. goto validate_map_fail;
  1264. }
  1265. list_for_each_entry(bo_va_entry, &mem->bo_va_list,
  1266. bo_list) {
  1267. ret = update_gpuvm_pte((struct amdgpu_device *)
  1268. bo_va_entry->kgd_dev,
  1269. bo_va_entry,
  1270. &sync_obj);
  1271. if (ret) {
  1272. pr_debug("Memory eviction: update PTE failed. Try again\n");
  1273. goto validate_map_fail;
  1274. }
  1275. }
  1276. }
  1277. /* Update page directories */
  1278. ret = process_update_pds(process_info, &sync_obj);
  1279. if (ret) {
  1280. pr_debug("Memory eviction: update PDs failed. Try again\n");
  1281. goto validate_map_fail;
  1282. }
  1283. amdgpu_sync_wait(&sync_obj, false);
  1284. /* Release old eviction fence and create new one, because fence only
  1285. * goes from unsignaled to signaled, fence cannot be reused.
  1286. * Use context and mm from the old fence.
  1287. */
  1288. new_fence = amdgpu_amdkfd_fence_create(
  1289. process_info->eviction_fence->base.context,
  1290. process_info->eviction_fence->mm);
  1291. if (!new_fence) {
  1292. pr_err("Failed to create eviction fence\n");
  1293. ret = -ENOMEM;
  1294. goto validate_map_fail;
  1295. }
  1296. dma_fence_put(&process_info->eviction_fence->base);
  1297. process_info->eviction_fence = new_fence;
  1298. *ef = dma_fence_get(&new_fence->base);
  1299. /* Wait for validate to finish and attach new eviction fence */
  1300. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1301. validate_list.head)
  1302. ttm_bo_wait(&mem->bo->tbo, false, false);
  1303. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1304. validate_list.head)
  1305. amdgpu_bo_fence(mem->bo,
  1306. &process_info->eviction_fence->base, true);
  1307. /* Attach eviction fence to PD / PT BOs */
  1308. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1309. vm_list_node) {
  1310. struct amdgpu_bo *bo = peer_vm->root.base.bo;
  1311. amdgpu_bo_fence(bo, &process_info->eviction_fence->base, true);
  1312. }
  1313. validate_map_fail:
  1314. ttm_eu_backoff_reservation(&ctx.ticket, &ctx.list);
  1315. amdgpu_sync_free(&sync_obj);
  1316. ttm_reserve_fail:
  1317. mutex_unlock(&process_info->lock);
  1318. kfree(pd_bo_list);
  1319. return ret;
  1320. }