amdgpu_amdkfd_gpuvm.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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. int byte_align;
  834. u32 alloc_domain;
  835. u64 alloc_flags;
  836. uint32_t mapping_flags;
  837. int ret;
  838. /*
  839. * Check on which domain to allocate BO
  840. */
  841. if (flags & ALLOC_MEM_FLAGS_VRAM) {
  842. alloc_domain = AMDGPU_GEM_DOMAIN_VRAM;
  843. alloc_flags = AMDGPU_GEM_CREATE_VRAM_CLEARED;
  844. alloc_flags |= (flags & ALLOC_MEM_FLAGS_PUBLIC) ?
  845. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED :
  846. AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
  847. } else if (flags & ALLOC_MEM_FLAGS_GTT) {
  848. alloc_domain = AMDGPU_GEM_DOMAIN_GTT;
  849. alloc_flags = 0;
  850. } else {
  851. return -EINVAL;
  852. }
  853. *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL);
  854. if (!*mem)
  855. return -ENOMEM;
  856. INIT_LIST_HEAD(&(*mem)->bo_va_list);
  857. mutex_init(&(*mem)->lock);
  858. (*mem)->aql_queue = !!(flags & ALLOC_MEM_FLAGS_AQL_QUEUE_MEM);
  859. /* Workaround for AQL queue wraparound bug. Map the same
  860. * memory twice. That means we only actually allocate half
  861. * the memory.
  862. */
  863. if ((*mem)->aql_queue)
  864. size = size >> 1;
  865. /* Workaround for TLB bug on older VI chips */
  866. byte_align = (adev->family == AMDGPU_FAMILY_VI &&
  867. adev->asic_type != CHIP_FIJI &&
  868. adev->asic_type != CHIP_POLARIS10 &&
  869. adev->asic_type != CHIP_POLARIS11) ?
  870. VI_BO_SIZE_ALIGN : 1;
  871. mapping_flags = AMDGPU_VM_PAGE_READABLE;
  872. if (flags & ALLOC_MEM_FLAGS_WRITABLE)
  873. mapping_flags |= AMDGPU_VM_PAGE_WRITEABLE;
  874. if (flags & ALLOC_MEM_FLAGS_EXECUTABLE)
  875. mapping_flags |= AMDGPU_VM_PAGE_EXECUTABLE;
  876. if (flags & ALLOC_MEM_FLAGS_COHERENT)
  877. mapping_flags |= AMDGPU_VM_MTYPE_UC;
  878. else
  879. mapping_flags |= AMDGPU_VM_MTYPE_NC;
  880. (*mem)->mapping_flags = mapping_flags;
  881. amdgpu_sync_create(&(*mem)->sync);
  882. ret = amdgpu_amdkfd_reserve_system_mem_limit(adev, size, alloc_domain);
  883. if (ret) {
  884. pr_debug("Insufficient system memory\n");
  885. goto err_reserve_system_mem;
  886. }
  887. pr_debug("\tcreate BO VA 0x%llx size 0x%llx domain %s\n",
  888. va, size, domain_string(alloc_domain));
  889. ret = amdgpu_bo_create(adev, size, byte_align,
  890. alloc_domain, alloc_flags, ttm_bo_type_device, NULL, &bo);
  891. if (ret) {
  892. pr_debug("Failed to create BO on domain %s. ret %d\n",
  893. domain_string(alloc_domain), ret);
  894. goto err_bo_create;
  895. }
  896. bo->kfd_bo = *mem;
  897. (*mem)->bo = bo;
  898. (*mem)->va = va;
  899. (*mem)->domain = alloc_domain;
  900. (*mem)->mapped_to_gpu_memory = 0;
  901. (*mem)->process_info = avm->process_info;
  902. add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info);
  903. if (offset)
  904. *offset = amdgpu_bo_mmap_offset(bo);
  905. return 0;
  906. err_bo_create:
  907. unreserve_system_mem_limit(adev, size, alloc_domain);
  908. err_reserve_system_mem:
  909. mutex_destroy(&(*mem)->lock);
  910. kfree(*mem);
  911. return ret;
  912. }
  913. int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  914. struct kgd_dev *kgd, struct kgd_mem *mem)
  915. {
  916. struct amdkfd_process_info *process_info = mem->process_info;
  917. unsigned long bo_size = mem->bo->tbo.mem.size;
  918. struct kfd_bo_va_list *entry, *tmp;
  919. struct bo_vm_reservation_context ctx;
  920. struct ttm_validate_buffer *bo_list_entry;
  921. int ret;
  922. mutex_lock(&mem->lock);
  923. if (mem->mapped_to_gpu_memory > 0) {
  924. pr_debug("BO VA 0x%llx size 0x%lx is still mapped.\n",
  925. mem->va, bo_size);
  926. mutex_unlock(&mem->lock);
  927. return -EBUSY;
  928. }
  929. mutex_unlock(&mem->lock);
  930. /* lock is not needed after this, since mem is unused and will
  931. * be freed anyway
  932. */
  933. /* Make sure restore workers don't access the BO any more */
  934. bo_list_entry = &mem->validate_list;
  935. mutex_lock(&process_info->lock);
  936. list_del(&bo_list_entry->head);
  937. mutex_unlock(&process_info->lock);
  938. ret = reserve_bo_and_cond_vms(mem, NULL, BO_VM_ALL, &ctx);
  939. if (unlikely(ret))
  940. return ret;
  941. /* The eviction fence should be removed by the last unmap.
  942. * TODO: Log an error condition if the bo still has the eviction fence
  943. * attached
  944. */
  945. amdgpu_amdkfd_remove_eviction_fence(mem->bo,
  946. process_info->eviction_fence,
  947. NULL, NULL);
  948. pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
  949. mem->va + bo_size * (1 + mem->aql_queue));
  950. /* Remove from VM internal data structures */
  951. list_for_each_entry_safe(entry, tmp, &mem->bo_va_list, bo_list)
  952. remove_bo_from_vm((struct amdgpu_device *)entry->kgd_dev,
  953. entry, bo_size);
  954. ret = unreserve_bo_and_vms(&ctx, false, false);
  955. /* Free the sync object */
  956. amdgpu_sync_free(&mem->sync);
  957. /* Free the BO*/
  958. amdgpu_bo_unref(&mem->bo);
  959. mutex_destroy(&mem->lock);
  960. kfree(mem);
  961. return ret;
  962. }
  963. int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
  964. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm)
  965. {
  966. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  967. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  968. int ret;
  969. struct amdgpu_bo *bo;
  970. uint32_t domain;
  971. struct kfd_bo_va_list *entry;
  972. struct bo_vm_reservation_context ctx;
  973. struct kfd_bo_va_list *bo_va_entry = NULL;
  974. struct kfd_bo_va_list *bo_va_entry_aql = NULL;
  975. unsigned long bo_size;
  976. /* Make sure restore is not running concurrently.
  977. */
  978. mutex_lock(&mem->process_info->lock);
  979. mutex_lock(&mem->lock);
  980. bo = mem->bo;
  981. if (!bo) {
  982. pr_err("Invalid BO when mapping memory to GPU\n");
  983. ret = -EINVAL;
  984. goto out;
  985. }
  986. domain = mem->domain;
  987. bo_size = bo->tbo.mem.size;
  988. pr_debug("Map VA 0x%llx - 0x%llx to vm %p domain %s\n",
  989. mem->va,
  990. mem->va + bo_size * (1 + mem->aql_queue),
  991. vm, domain_string(domain));
  992. ret = reserve_bo_and_vm(mem, vm, &ctx);
  993. if (unlikely(ret))
  994. goto out;
  995. if (check_if_add_bo_to_vm(avm, mem)) {
  996. ret = add_bo_to_vm(adev, mem, avm, false,
  997. &bo_va_entry);
  998. if (ret)
  999. goto add_bo_to_vm_failed;
  1000. if (mem->aql_queue) {
  1001. ret = add_bo_to_vm(adev, mem, avm,
  1002. true, &bo_va_entry_aql);
  1003. if (ret)
  1004. goto add_bo_to_vm_failed_aql;
  1005. }
  1006. } else {
  1007. ret = vm_validate_pt_pd_bos(avm);
  1008. if (unlikely(ret))
  1009. goto add_bo_to_vm_failed;
  1010. }
  1011. if (mem->mapped_to_gpu_memory == 0) {
  1012. /* Validate BO only once. The eviction fence gets added to BO
  1013. * the first time it is mapped. Validate will wait for all
  1014. * background evictions to complete.
  1015. */
  1016. ret = amdgpu_amdkfd_bo_validate(bo, domain, true);
  1017. if (ret) {
  1018. pr_debug("Validate failed\n");
  1019. goto map_bo_to_gpuvm_failed;
  1020. }
  1021. }
  1022. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  1023. if (entry->bo_va->base.vm == vm && !entry->is_mapped) {
  1024. pr_debug("\t map VA 0x%llx - 0x%llx in entry %p\n",
  1025. entry->va, entry->va + bo_size,
  1026. entry);
  1027. ret = map_bo_to_gpuvm(adev, entry, ctx.sync);
  1028. if (ret) {
  1029. pr_err("Failed to map radeon bo to gpuvm\n");
  1030. goto map_bo_to_gpuvm_failed;
  1031. }
  1032. ret = vm_update_pds(vm, ctx.sync);
  1033. if (ret) {
  1034. pr_err("Failed to update page directories\n");
  1035. goto map_bo_to_gpuvm_failed;
  1036. }
  1037. entry->is_mapped = true;
  1038. mem->mapped_to_gpu_memory++;
  1039. pr_debug("\t INC mapping count %d\n",
  1040. mem->mapped_to_gpu_memory);
  1041. }
  1042. }
  1043. if (!amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) && !bo->pin_count)
  1044. amdgpu_bo_fence(bo,
  1045. &avm->process_info->eviction_fence->base,
  1046. true);
  1047. ret = unreserve_bo_and_vms(&ctx, false, false);
  1048. goto out;
  1049. map_bo_to_gpuvm_failed:
  1050. if (bo_va_entry_aql)
  1051. remove_bo_from_vm(adev, bo_va_entry_aql, bo_size);
  1052. add_bo_to_vm_failed_aql:
  1053. if (bo_va_entry)
  1054. remove_bo_from_vm(adev, bo_va_entry, bo_size);
  1055. add_bo_to_vm_failed:
  1056. unreserve_bo_and_vms(&ctx, false, false);
  1057. out:
  1058. mutex_unlock(&mem->process_info->lock);
  1059. mutex_unlock(&mem->lock);
  1060. return ret;
  1061. }
  1062. int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  1063. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm)
  1064. {
  1065. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  1066. struct amdkfd_process_info *process_info =
  1067. ((struct amdgpu_vm *)vm)->process_info;
  1068. unsigned long bo_size = mem->bo->tbo.mem.size;
  1069. struct kfd_bo_va_list *entry;
  1070. struct bo_vm_reservation_context ctx;
  1071. int ret;
  1072. mutex_lock(&mem->lock);
  1073. ret = reserve_bo_and_cond_vms(mem, vm, BO_VM_MAPPED, &ctx);
  1074. if (unlikely(ret))
  1075. goto out;
  1076. /* If no VMs were reserved, it means the BO wasn't actually mapped */
  1077. if (ctx.n_vms == 0) {
  1078. ret = -EINVAL;
  1079. goto unreserve_out;
  1080. }
  1081. ret = vm_validate_pt_pd_bos((struct amdgpu_vm *)vm);
  1082. if (unlikely(ret))
  1083. goto unreserve_out;
  1084. pr_debug("Unmap VA 0x%llx - 0x%llx from vm %p\n",
  1085. mem->va,
  1086. mem->va + bo_size * (1 + mem->aql_queue),
  1087. vm);
  1088. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  1089. if (entry->bo_va->base.vm == vm && entry->is_mapped) {
  1090. pr_debug("\t unmap VA 0x%llx - 0x%llx from entry %p\n",
  1091. entry->va,
  1092. entry->va + bo_size,
  1093. entry);
  1094. ret = unmap_bo_from_gpuvm(adev, entry, ctx.sync);
  1095. if (ret == 0) {
  1096. entry->is_mapped = false;
  1097. } else {
  1098. pr_err("failed to unmap VA 0x%llx\n",
  1099. mem->va);
  1100. goto unreserve_out;
  1101. }
  1102. mem->mapped_to_gpu_memory--;
  1103. pr_debug("\t DEC mapping count %d\n",
  1104. mem->mapped_to_gpu_memory);
  1105. }
  1106. }
  1107. /* If BO is unmapped from all VMs, unfence it. It can be evicted if
  1108. * required.
  1109. */
  1110. if (mem->mapped_to_gpu_memory == 0 &&
  1111. !amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) && !mem->bo->pin_count)
  1112. amdgpu_amdkfd_remove_eviction_fence(mem->bo,
  1113. process_info->eviction_fence,
  1114. NULL, NULL);
  1115. unreserve_out:
  1116. unreserve_bo_and_vms(&ctx, false, false);
  1117. out:
  1118. mutex_unlock(&mem->lock);
  1119. return ret;
  1120. }
  1121. int amdgpu_amdkfd_gpuvm_sync_memory(
  1122. struct kgd_dev *kgd, struct kgd_mem *mem, bool intr)
  1123. {
  1124. struct amdgpu_sync sync;
  1125. int ret;
  1126. amdgpu_sync_create(&sync);
  1127. mutex_lock(&mem->lock);
  1128. amdgpu_sync_clone(&mem->sync, &sync);
  1129. mutex_unlock(&mem->lock);
  1130. ret = amdgpu_sync_wait(&sync, intr);
  1131. amdgpu_sync_free(&sync);
  1132. return ret;
  1133. }
  1134. int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
  1135. struct kgd_mem *mem, void **kptr, uint64_t *size)
  1136. {
  1137. int ret;
  1138. struct amdgpu_bo *bo = mem->bo;
  1139. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
  1140. pr_err("userptr can't be mapped to kernel\n");
  1141. return -EINVAL;
  1142. }
  1143. /* delete kgd_mem from kfd_bo_list to avoid re-validating
  1144. * this BO in BO's restoring after eviction.
  1145. */
  1146. mutex_lock(&mem->process_info->lock);
  1147. ret = amdgpu_bo_reserve(bo, true);
  1148. if (ret) {
  1149. pr_err("Failed to reserve bo. ret %d\n", ret);
  1150. goto bo_reserve_failed;
  1151. }
  1152. ret = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT, NULL);
  1153. if (ret) {
  1154. pr_err("Failed to pin bo. ret %d\n", ret);
  1155. goto pin_failed;
  1156. }
  1157. ret = amdgpu_bo_kmap(bo, kptr);
  1158. if (ret) {
  1159. pr_err("Failed to map bo to kernel. ret %d\n", ret);
  1160. goto kmap_failed;
  1161. }
  1162. amdgpu_amdkfd_remove_eviction_fence(
  1163. bo, mem->process_info->eviction_fence, NULL, NULL);
  1164. list_del_init(&mem->validate_list.head);
  1165. if (size)
  1166. *size = amdgpu_bo_size(bo);
  1167. amdgpu_bo_unreserve(bo);
  1168. mutex_unlock(&mem->process_info->lock);
  1169. return 0;
  1170. kmap_failed:
  1171. amdgpu_bo_unpin(bo);
  1172. pin_failed:
  1173. amdgpu_bo_unreserve(bo);
  1174. bo_reserve_failed:
  1175. mutex_unlock(&mem->process_info->lock);
  1176. return ret;
  1177. }
  1178. /** amdgpu_amdkfd_gpuvm_restore_process_bos - Restore all BOs for the given
  1179. * KFD process identified by process_info
  1180. *
  1181. * @process_info: amdkfd_process_info of the KFD process
  1182. *
  1183. * After memory eviction, restore thread calls this function. The function
  1184. * should be called when the Process is still valid. BO restore involves -
  1185. *
  1186. * 1. Release old eviction fence and create new one
  1187. * 2. Get two copies of PD BO list from all the VMs. Keep one copy as pd_list.
  1188. * 3 Use the second PD list and kfd_bo_list to create a list (ctx.list) of
  1189. * BOs that need to be reserved.
  1190. * 4. Reserve all the BOs
  1191. * 5. Validate of PD and PT BOs.
  1192. * 6. Validate all KFD BOs using kfd_bo_list and Map them and add new fence
  1193. * 7. Add fence to all PD and PT BOs.
  1194. * 8. Unreserve all BOs
  1195. */
  1196. int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence **ef)
  1197. {
  1198. struct amdgpu_bo_list_entry *pd_bo_list;
  1199. struct amdkfd_process_info *process_info = info;
  1200. struct amdgpu_vm *peer_vm;
  1201. struct kgd_mem *mem;
  1202. struct bo_vm_reservation_context ctx;
  1203. struct amdgpu_amdkfd_fence *new_fence;
  1204. int ret = 0, i;
  1205. struct list_head duplicate_save;
  1206. struct amdgpu_sync sync_obj;
  1207. INIT_LIST_HEAD(&duplicate_save);
  1208. INIT_LIST_HEAD(&ctx.list);
  1209. INIT_LIST_HEAD(&ctx.duplicates);
  1210. pd_bo_list = kcalloc(process_info->n_vms,
  1211. sizeof(struct amdgpu_bo_list_entry),
  1212. GFP_KERNEL);
  1213. if (!pd_bo_list)
  1214. return -ENOMEM;
  1215. i = 0;
  1216. mutex_lock(&process_info->lock);
  1217. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1218. vm_list_node)
  1219. amdgpu_vm_get_pd_bo(peer_vm, &ctx.list, &pd_bo_list[i++]);
  1220. /* Reserve all BOs and page tables/directory. Add all BOs from
  1221. * kfd_bo_list to ctx.list
  1222. */
  1223. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1224. validate_list.head) {
  1225. list_add_tail(&mem->resv_list.head, &ctx.list);
  1226. mem->resv_list.bo = mem->validate_list.bo;
  1227. mem->resv_list.shared = mem->validate_list.shared;
  1228. }
  1229. ret = ttm_eu_reserve_buffers(&ctx.ticket, &ctx.list,
  1230. false, &duplicate_save);
  1231. if (ret) {
  1232. pr_debug("Memory eviction: TTM Reserve Failed. Try again\n");
  1233. goto ttm_reserve_fail;
  1234. }
  1235. amdgpu_sync_create(&sync_obj);
  1236. /* Validate PDs and PTs */
  1237. ret = process_validate_vms(process_info);
  1238. if (ret)
  1239. goto validate_map_fail;
  1240. /* Wait for PD/PTs validate to finish */
  1241. /* FIXME: I think this isn't needed */
  1242. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1243. vm_list_node) {
  1244. struct amdgpu_bo *bo = peer_vm->root.base.bo;
  1245. ttm_bo_wait(&bo->tbo, false, false);
  1246. }
  1247. /* Validate BOs and map them to GPUVM (update VM page tables). */
  1248. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1249. validate_list.head) {
  1250. struct amdgpu_bo *bo = mem->bo;
  1251. uint32_t domain = mem->domain;
  1252. struct kfd_bo_va_list *bo_va_entry;
  1253. ret = amdgpu_amdkfd_bo_validate(bo, domain, false);
  1254. if (ret) {
  1255. pr_debug("Memory eviction: Validate BOs failed. Try again\n");
  1256. goto validate_map_fail;
  1257. }
  1258. list_for_each_entry(bo_va_entry, &mem->bo_va_list,
  1259. bo_list) {
  1260. ret = update_gpuvm_pte((struct amdgpu_device *)
  1261. bo_va_entry->kgd_dev,
  1262. bo_va_entry,
  1263. &sync_obj);
  1264. if (ret) {
  1265. pr_debug("Memory eviction: update PTE failed. Try again\n");
  1266. goto validate_map_fail;
  1267. }
  1268. }
  1269. }
  1270. /* Update page directories */
  1271. ret = process_update_pds(process_info, &sync_obj);
  1272. if (ret) {
  1273. pr_debug("Memory eviction: update PDs failed. Try again\n");
  1274. goto validate_map_fail;
  1275. }
  1276. amdgpu_sync_wait(&sync_obj, false);
  1277. /* Release old eviction fence and create new one, because fence only
  1278. * goes from unsignaled to signaled, fence cannot be reused.
  1279. * Use context and mm from the old fence.
  1280. */
  1281. new_fence = amdgpu_amdkfd_fence_create(
  1282. process_info->eviction_fence->base.context,
  1283. process_info->eviction_fence->mm);
  1284. if (!new_fence) {
  1285. pr_err("Failed to create eviction fence\n");
  1286. ret = -ENOMEM;
  1287. goto validate_map_fail;
  1288. }
  1289. dma_fence_put(&process_info->eviction_fence->base);
  1290. process_info->eviction_fence = new_fence;
  1291. *ef = dma_fence_get(&new_fence->base);
  1292. /* Wait for validate to finish and attach new eviction fence */
  1293. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1294. validate_list.head)
  1295. ttm_bo_wait(&mem->bo->tbo, false, false);
  1296. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1297. validate_list.head)
  1298. amdgpu_bo_fence(mem->bo,
  1299. &process_info->eviction_fence->base, true);
  1300. /* Attach eviction fence to PD / PT BOs */
  1301. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1302. vm_list_node) {
  1303. struct amdgpu_bo *bo = peer_vm->root.base.bo;
  1304. amdgpu_bo_fence(bo, &process_info->eviction_fence->base, true);
  1305. }
  1306. validate_map_fail:
  1307. ttm_eu_backoff_reservation(&ctx.ticket, &ctx.list);
  1308. amdgpu_sync_free(&sync_obj);
  1309. ttm_reserve_fail:
  1310. mutex_unlock(&process_info->lock);
  1311. kfree(pd_bo_list);
  1312. return ret;
  1313. }