amdgpu_vm.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/dma-fence-array.h>
  29. #include <linux/interval_tree_generic.h>
  30. #include <linux/idr.h>
  31. #include <drm/drmP.h>
  32. #include <drm/amdgpu_drm.h>
  33. #include "amdgpu.h"
  34. #include "amdgpu_trace.h"
  35. /*
  36. * GPUVM
  37. * GPUVM is similar to the legacy gart on older asics, however
  38. * rather than there being a single global gart table
  39. * for the entire GPU, there are multiple VM page tables active
  40. * at any given time. The VM page tables can contain a mix
  41. * vram pages and system memory pages and system memory pages
  42. * can be mapped as snooped (cached system pages) or unsnooped
  43. * (uncached system pages).
  44. * Each VM has an ID associated with it and there is a page table
  45. * associated with each VMID. When execting a command buffer,
  46. * the kernel tells the the ring what VMID to use for that command
  47. * buffer. VMIDs are allocated dynamically as commands are submitted.
  48. * The userspace drivers maintain their own address space and the kernel
  49. * sets up their pages tables accordingly when they submit their
  50. * command buffers and a VMID is assigned.
  51. * Cayman/Trinity support up to 8 active VMs at any given time;
  52. * SI supports 16.
  53. */
  54. #define START(node) ((node)->start)
  55. #define LAST(node) ((node)->last)
  56. INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
  57. START, LAST, static, amdgpu_vm_it)
  58. #undef START
  59. #undef LAST
  60. /* Local structure. Encapsulate some VM table update parameters to reduce
  61. * the number of function parameters
  62. */
  63. struct amdgpu_pte_update_params {
  64. /* amdgpu device we do this update for */
  65. struct amdgpu_device *adev;
  66. /* optional amdgpu_vm we do this update for */
  67. struct amdgpu_vm *vm;
  68. /* address where to copy page table entries from */
  69. uint64_t src;
  70. /* indirect buffer to fill with commands */
  71. struct amdgpu_ib *ib;
  72. /* Function which actually does the update */
  73. void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
  74. uint64_t addr, unsigned count, uint32_t incr,
  75. uint64_t flags);
  76. /* The next two are used during VM update by CPU
  77. * DMA addresses to use for mapping
  78. * Kernel pointer of PD/PT BO that needs to be updated
  79. */
  80. dma_addr_t *pages_addr;
  81. void *kptr;
  82. };
  83. /* Helper to disable partial resident texture feature from a fence callback */
  84. struct amdgpu_prt_cb {
  85. struct amdgpu_device *adev;
  86. struct dma_fence_cb cb;
  87. };
  88. /**
  89. * amdgpu_vm_level_shift - return the addr shift for each level
  90. *
  91. * @adev: amdgpu_device pointer
  92. *
  93. * Returns the number of bits the pfn needs to be right shifted for a level.
  94. */
  95. static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
  96. unsigned level)
  97. {
  98. unsigned shift = 0xff;
  99. switch (level) {
  100. case AMDGPU_VM_PDB2:
  101. case AMDGPU_VM_PDB1:
  102. case AMDGPU_VM_PDB0:
  103. shift = 9 * (AMDGPU_VM_PDB0 - level) +
  104. adev->vm_manager.block_size;
  105. break;
  106. case AMDGPU_VM_PTB:
  107. shift = 0;
  108. break;
  109. default:
  110. dev_err(adev->dev, "the level%d isn't supported.\n", level);
  111. }
  112. return shift;
  113. }
  114. /**
  115. * amdgpu_vm_num_entries - return the number of entries in a PD/PT
  116. *
  117. * @adev: amdgpu_device pointer
  118. *
  119. * Calculate the number of entries in a page directory or page table.
  120. */
  121. static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
  122. unsigned level)
  123. {
  124. unsigned shift = amdgpu_vm_level_shift(adev,
  125. adev->vm_manager.root_level);
  126. if (level == adev->vm_manager.root_level)
  127. /* For the root directory */
  128. return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
  129. else if (level != AMDGPU_VM_PTB)
  130. /* Everything in between */
  131. return 512;
  132. else
  133. /* For the page tables on the leaves */
  134. return AMDGPU_VM_PTE_COUNT(adev);
  135. }
  136. /**
  137. * amdgpu_vm_bo_size - returns the size of the BOs in bytes
  138. *
  139. * @adev: amdgpu_device pointer
  140. *
  141. * Calculate the size of the BO for a page directory or page table in bytes.
  142. */
  143. static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
  144. {
  145. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
  146. }
  147. /**
  148. * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
  149. *
  150. * @vm: vm providing the BOs
  151. * @validated: head of validation list
  152. * @entry: entry to add
  153. *
  154. * Add the page directory to the list of BOs to
  155. * validate for command submission.
  156. */
  157. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  158. struct list_head *validated,
  159. struct amdgpu_bo_list_entry *entry)
  160. {
  161. entry->robj = vm->root.base.bo;
  162. entry->priority = 0;
  163. entry->tv.bo = &entry->robj->tbo;
  164. entry->tv.shared = true;
  165. entry->user_pages = NULL;
  166. list_add(&entry->tv.head, validated);
  167. }
  168. /**
  169. * amdgpu_vm_validate_pt_bos - validate the page table BOs
  170. *
  171. * @adev: amdgpu device pointer
  172. * @vm: vm providing the BOs
  173. * @validate: callback to do the validation
  174. * @param: parameter for the validation callback
  175. *
  176. * Validate the page table BOs on command submission if neccessary.
  177. */
  178. int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  179. int (*validate)(void *p, struct amdgpu_bo *bo),
  180. void *param)
  181. {
  182. struct ttm_bo_global *glob = adev->mman.bdev.glob;
  183. int r;
  184. spin_lock(&vm->status_lock);
  185. while (!list_empty(&vm->evicted)) {
  186. struct amdgpu_vm_bo_base *bo_base;
  187. struct amdgpu_bo *bo;
  188. bo_base = list_first_entry(&vm->evicted,
  189. struct amdgpu_vm_bo_base,
  190. vm_status);
  191. spin_unlock(&vm->status_lock);
  192. bo = bo_base->bo;
  193. BUG_ON(!bo);
  194. if (bo->parent) {
  195. r = validate(param, bo);
  196. if (r)
  197. return r;
  198. spin_lock(&glob->lru_lock);
  199. ttm_bo_move_to_lru_tail(&bo->tbo);
  200. if (bo->shadow)
  201. ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
  202. spin_unlock(&glob->lru_lock);
  203. }
  204. if (bo->tbo.type == ttm_bo_type_kernel &&
  205. vm->use_cpu_for_update) {
  206. r = amdgpu_bo_kmap(bo, NULL);
  207. if (r)
  208. return r;
  209. }
  210. spin_lock(&vm->status_lock);
  211. if (bo->tbo.type != ttm_bo_type_kernel)
  212. list_move(&bo_base->vm_status, &vm->moved);
  213. else
  214. list_move(&bo_base->vm_status, &vm->relocated);
  215. }
  216. spin_unlock(&vm->status_lock);
  217. return 0;
  218. }
  219. /**
  220. * amdgpu_vm_ready - check VM is ready for updates
  221. *
  222. * @vm: VM to check
  223. *
  224. * Check if all VM PDs/PTs are ready for updates
  225. */
  226. bool amdgpu_vm_ready(struct amdgpu_vm *vm)
  227. {
  228. bool ready;
  229. spin_lock(&vm->status_lock);
  230. ready = list_empty(&vm->evicted);
  231. spin_unlock(&vm->status_lock);
  232. return ready;
  233. }
  234. /**
  235. * amdgpu_vm_alloc_levels - allocate the PD/PT levels
  236. *
  237. * @adev: amdgpu_device pointer
  238. * @vm: requested vm
  239. * @saddr: start of the address range
  240. * @eaddr: end of the address range
  241. *
  242. * Make sure the page directories and page tables are allocated
  243. */
  244. static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
  245. struct amdgpu_vm *vm,
  246. struct amdgpu_vm_pt *parent,
  247. uint64_t saddr, uint64_t eaddr,
  248. unsigned level)
  249. {
  250. unsigned shift = amdgpu_vm_level_shift(adev, level);
  251. unsigned pt_idx, from, to;
  252. int r;
  253. u64 flags;
  254. uint64_t init_value = 0;
  255. if (!parent->entries) {
  256. unsigned num_entries = amdgpu_vm_num_entries(adev, level);
  257. parent->entries = kvmalloc_array(num_entries,
  258. sizeof(struct amdgpu_vm_pt),
  259. GFP_KERNEL | __GFP_ZERO);
  260. if (!parent->entries)
  261. return -ENOMEM;
  262. memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
  263. }
  264. from = saddr >> shift;
  265. to = eaddr >> shift;
  266. if (from >= amdgpu_vm_num_entries(adev, level) ||
  267. to >= amdgpu_vm_num_entries(adev, level))
  268. return -EINVAL;
  269. ++level;
  270. saddr = saddr & ((1 << shift) - 1);
  271. eaddr = eaddr & ((1 << shift) - 1);
  272. flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  273. AMDGPU_GEM_CREATE_VRAM_CLEARED;
  274. if (vm->use_cpu_for_update)
  275. flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  276. else
  277. flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  278. AMDGPU_GEM_CREATE_SHADOW);
  279. if (vm->pte_support_ats) {
  280. init_value = AMDGPU_PTE_DEFAULT_ATC;
  281. if (level != AMDGPU_VM_PTB)
  282. init_value |= AMDGPU_PDE_PTE;
  283. }
  284. /* walk over the address space and allocate the page tables */
  285. for (pt_idx = from; pt_idx <= to; ++pt_idx) {
  286. struct reservation_object *resv = vm->root.base.bo->tbo.resv;
  287. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  288. struct amdgpu_bo *pt;
  289. if (!entry->base.bo) {
  290. r = amdgpu_bo_create(adev,
  291. amdgpu_vm_bo_size(adev, level),
  292. AMDGPU_GPU_PAGE_SIZE, true,
  293. AMDGPU_GEM_DOMAIN_VRAM,
  294. flags,
  295. NULL, resv, init_value, &pt);
  296. if (r)
  297. return r;
  298. if (vm->use_cpu_for_update) {
  299. r = amdgpu_bo_kmap(pt, NULL);
  300. if (r) {
  301. amdgpu_bo_unref(&pt);
  302. return r;
  303. }
  304. }
  305. /* Keep a reference to the root directory to avoid
  306. * freeing them up in the wrong order.
  307. */
  308. pt->parent = amdgpu_bo_ref(parent->base.bo);
  309. entry->base.vm = vm;
  310. entry->base.bo = pt;
  311. list_add_tail(&entry->base.bo_list, &pt->va);
  312. spin_lock(&vm->status_lock);
  313. list_add(&entry->base.vm_status, &vm->relocated);
  314. spin_unlock(&vm->status_lock);
  315. }
  316. if (level < AMDGPU_VM_PTB) {
  317. uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
  318. uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
  319. ((1 << shift) - 1);
  320. r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
  321. sub_eaddr, level);
  322. if (r)
  323. return r;
  324. }
  325. }
  326. return 0;
  327. }
  328. /**
  329. * amdgpu_vm_alloc_pts - Allocate page tables.
  330. *
  331. * @adev: amdgpu_device pointer
  332. * @vm: VM to allocate page tables for
  333. * @saddr: Start address which needs to be allocated
  334. * @size: Size from start address we need.
  335. *
  336. * Make sure the page tables are allocated.
  337. */
  338. int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
  339. struct amdgpu_vm *vm,
  340. uint64_t saddr, uint64_t size)
  341. {
  342. uint64_t last_pfn;
  343. uint64_t eaddr;
  344. /* validate the parameters */
  345. if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
  346. return -EINVAL;
  347. eaddr = saddr + size - 1;
  348. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  349. if (last_pfn >= adev->vm_manager.max_pfn) {
  350. dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
  351. last_pfn, adev->vm_manager.max_pfn);
  352. return -EINVAL;
  353. }
  354. saddr /= AMDGPU_GPU_PAGE_SIZE;
  355. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  356. return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
  357. adev->vm_manager.root_level);
  358. }
  359. /**
  360. * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
  361. *
  362. * @adev: amdgpu_device pointer
  363. */
  364. void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
  365. {
  366. const struct amdgpu_ip_block *ip_block;
  367. bool has_compute_vm_bug;
  368. struct amdgpu_ring *ring;
  369. int i;
  370. has_compute_vm_bug = false;
  371. ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
  372. if (ip_block) {
  373. /* Compute has a VM bug for GFX version < 7.
  374. Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
  375. if (ip_block->version->major <= 7)
  376. has_compute_vm_bug = true;
  377. else if (ip_block->version->major == 8)
  378. if (adev->gfx.mec_fw_version < 673)
  379. has_compute_vm_bug = true;
  380. }
  381. for (i = 0; i < adev->num_rings; i++) {
  382. ring = adev->rings[i];
  383. if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
  384. /* only compute rings */
  385. ring->has_compute_vm_bug = has_compute_vm_bug;
  386. else
  387. ring->has_compute_vm_bug = false;
  388. }
  389. }
  390. bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
  391. struct amdgpu_job *job)
  392. {
  393. struct amdgpu_device *adev = ring->adev;
  394. unsigned vmhub = ring->funcs->vmhub;
  395. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  396. struct amdgpu_vmid *id;
  397. bool gds_switch_needed;
  398. bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
  399. if (job->vmid == 0)
  400. return false;
  401. id = &id_mgr->ids[job->vmid];
  402. gds_switch_needed = ring->funcs->emit_gds_switch && (
  403. id->gds_base != job->gds_base ||
  404. id->gds_size != job->gds_size ||
  405. id->gws_base != job->gws_base ||
  406. id->gws_size != job->gws_size ||
  407. id->oa_base != job->oa_base ||
  408. id->oa_size != job->oa_size);
  409. if (amdgpu_vmid_had_gpu_reset(adev, id))
  410. return true;
  411. return vm_flush_needed || gds_switch_needed;
  412. }
  413. static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
  414. {
  415. return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
  416. }
  417. /**
  418. * amdgpu_vm_flush - hardware flush the vm
  419. *
  420. * @ring: ring to use for flush
  421. * @vmid: vmid number to use
  422. * @pd_addr: address of the page directory
  423. *
  424. * Emit a VM flush when it is necessary.
  425. */
  426. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
  427. {
  428. struct amdgpu_device *adev = ring->adev;
  429. unsigned vmhub = ring->funcs->vmhub;
  430. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  431. struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
  432. bool gds_switch_needed = ring->funcs->emit_gds_switch && (
  433. id->gds_base != job->gds_base ||
  434. id->gds_size != job->gds_size ||
  435. id->gws_base != job->gws_base ||
  436. id->gws_size != job->gws_size ||
  437. id->oa_base != job->oa_base ||
  438. id->oa_size != job->oa_size);
  439. bool vm_flush_needed = job->vm_needs_flush;
  440. unsigned patch_offset = 0;
  441. int r;
  442. if (amdgpu_vmid_had_gpu_reset(adev, id)) {
  443. gds_switch_needed = true;
  444. vm_flush_needed = true;
  445. }
  446. if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
  447. return 0;
  448. if (ring->funcs->init_cond_exec)
  449. patch_offset = amdgpu_ring_init_cond_exec(ring);
  450. if (need_pipe_sync)
  451. amdgpu_ring_emit_pipeline_sync(ring);
  452. if (ring->funcs->emit_vm_flush && vm_flush_needed) {
  453. struct dma_fence *fence;
  454. trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
  455. amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
  456. r = amdgpu_fence_emit(ring, &fence);
  457. if (r)
  458. return r;
  459. mutex_lock(&id_mgr->lock);
  460. dma_fence_put(id->last_flush);
  461. id->last_flush = fence;
  462. id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
  463. mutex_unlock(&id_mgr->lock);
  464. }
  465. if (ring->funcs->emit_gds_switch && gds_switch_needed) {
  466. id->gds_base = job->gds_base;
  467. id->gds_size = job->gds_size;
  468. id->gws_base = job->gws_base;
  469. id->gws_size = job->gws_size;
  470. id->oa_base = job->oa_base;
  471. id->oa_size = job->oa_size;
  472. amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
  473. job->gds_size, job->gws_base,
  474. job->gws_size, job->oa_base,
  475. job->oa_size);
  476. }
  477. if (ring->funcs->patch_cond_exec)
  478. amdgpu_ring_patch_cond_exec(ring, patch_offset);
  479. /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
  480. if (ring->funcs->emit_switch_buffer) {
  481. amdgpu_ring_emit_switch_buffer(ring);
  482. amdgpu_ring_emit_switch_buffer(ring);
  483. }
  484. return 0;
  485. }
  486. /**
  487. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  488. *
  489. * @vm: requested vm
  490. * @bo: requested buffer object
  491. *
  492. * Find @bo inside the requested vm.
  493. * Search inside the @bos vm list for the requested vm
  494. * Returns the found bo_va or NULL if none is found
  495. *
  496. * Object has to be reserved!
  497. */
  498. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  499. struct amdgpu_bo *bo)
  500. {
  501. struct amdgpu_bo_va *bo_va;
  502. list_for_each_entry(bo_va, &bo->va, base.bo_list) {
  503. if (bo_va->base.vm == vm) {
  504. return bo_va;
  505. }
  506. }
  507. return NULL;
  508. }
  509. /**
  510. * amdgpu_vm_do_set_ptes - helper to call the right asic function
  511. *
  512. * @params: see amdgpu_pte_update_params definition
  513. * @pe: addr of the page entry
  514. * @addr: dst addr to write into pe
  515. * @count: number of page entries to update
  516. * @incr: increase next addr by incr bytes
  517. * @flags: hw access flags
  518. *
  519. * Traces the parameters and calls the right asic functions
  520. * to setup the page table using the DMA.
  521. */
  522. static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
  523. uint64_t pe, uint64_t addr,
  524. unsigned count, uint32_t incr,
  525. uint64_t flags)
  526. {
  527. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  528. if (count < 3) {
  529. amdgpu_vm_write_pte(params->adev, params->ib, pe,
  530. addr | flags, count, incr);
  531. } else {
  532. amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
  533. count, incr, flags);
  534. }
  535. }
  536. /**
  537. * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
  538. *
  539. * @params: see amdgpu_pte_update_params definition
  540. * @pe: addr of the page entry
  541. * @addr: dst addr to write into pe
  542. * @count: number of page entries to update
  543. * @incr: increase next addr by incr bytes
  544. * @flags: hw access flags
  545. *
  546. * Traces the parameters and calls the DMA function to copy the PTEs.
  547. */
  548. static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
  549. uint64_t pe, uint64_t addr,
  550. unsigned count, uint32_t incr,
  551. uint64_t flags)
  552. {
  553. uint64_t src = (params->src + (addr >> 12) * 8);
  554. trace_amdgpu_vm_copy_ptes(pe, src, count);
  555. amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
  556. }
  557. /**
  558. * amdgpu_vm_map_gart - Resolve gart mapping of addr
  559. *
  560. * @pages_addr: optional DMA address to use for lookup
  561. * @addr: the unmapped addr
  562. *
  563. * Look up the physical address of the page that the pte resolves
  564. * to and return the pointer for the page table entry.
  565. */
  566. static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
  567. {
  568. uint64_t result;
  569. /* page table offset */
  570. result = pages_addr[addr >> PAGE_SHIFT];
  571. /* in case cpu page size != gpu page size*/
  572. result |= addr & (~PAGE_MASK);
  573. result &= 0xFFFFFFFFFFFFF000ULL;
  574. return result;
  575. }
  576. /**
  577. * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
  578. *
  579. * @params: see amdgpu_pte_update_params definition
  580. * @pe: kmap addr of the page entry
  581. * @addr: dst addr to write into pe
  582. * @count: number of page entries to update
  583. * @incr: increase next addr by incr bytes
  584. * @flags: hw access flags
  585. *
  586. * Write count number of PT/PD entries directly.
  587. */
  588. static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
  589. uint64_t pe, uint64_t addr,
  590. unsigned count, uint32_t incr,
  591. uint64_t flags)
  592. {
  593. unsigned int i;
  594. uint64_t value;
  595. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  596. for (i = 0; i < count; i++) {
  597. value = params->pages_addr ?
  598. amdgpu_vm_map_gart(params->pages_addr, addr) :
  599. addr;
  600. amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
  601. i, value, flags);
  602. addr += incr;
  603. }
  604. }
  605. static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  606. void *owner)
  607. {
  608. struct amdgpu_sync sync;
  609. int r;
  610. amdgpu_sync_create(&sync);
  611. amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
  612. r = amdgpu_sync_wait(&sync, true);
  613. amdgpu_sync_free(&sync);
  614. return r;
  615. }
  616. /*
  617. * amdgpu_vm_update_pde - update a single level in the hierarchy
  618. *
  619. * @param: parameters for the update
  620. * @vm: requested vm
  621. * @parent: parent directory
  622. * @entry: entry to update
  623. *
  624. * Makes sure the requested entry in parent is up to date.
  625. */
  626. static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
  627. struct amdgpu_vm *vm,
  628. struct amdgpu_vm_pt *parent,
  629. struct amdgpu_vm_pt *entry)
  630. {
  631. struct amdgpu_bo *bo = entry->base.bo, *shadow = NULL, *pbo;
  632. uint64_t pd_addr, shadow_addr = 0;
  633. uint64_t pde, pt, flags;
  634. unsigned level;
  635. /* Don't update huge pages here */
  636. if (entry->huge)
  637. return;
  638. if (vm->use_cpu_for_update) {
  639. pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
  640. } else {
  641. pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
  642. shadow = parent->base.bo->shadow;
  643. if (shadow)
  644. shadow_addr = amdgpu_bo_gpu_offset(shadow);
  645. }
  646. for (level = 0, pbo = parent->base.bo->parent; pbo; ++level)
  647. pbo = pbo->parent;
  648. level += params->adev->vm_manager.root_level;
  649. pt = amdgpu_bo_gpu_offset(bo);
  650. flags = AMDGPU_PTE_VALID;
  651. amdgpu_gart_get_vm_pde(params->adev, level, &pt, &flags);
  652. if (shadow) {
  653. pde = shadow_addr + (entry - parent->entries) * 8;
  654. params->func(params, pde, pt, 1, 0, flags);
  655. }
  656. pde = pd_addr + (entry - parent->entries) * 8;
  657. params->func(params, pde, pt, 1, 0, flags);
  658. }
  659. /*
  660. * amdgpu_vm_invalidate_level - mark all PD levels as invalid
  661. *
  662. * @parent: parent PD
  663. *
  664. * Mark all PD level as invalid after an error.
  665. */
  666. static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
  667. struct amdgpu_vm *vm,
  668. struct amdgpu_vm_pt *parent,
  669. unsigned level)
  670. {
  671. unsigned pt_idx, num_entries;
  672. /*
  673. * Recurse into the subdirectories. This recursion is harmless because
  674. * we only have a maximum of 5 layers.
  675. */
  676. num_entries = amdgpu_vm_num_entries(adev, level);
  677. for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
  678. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  679. if (!entry->base.bo)
  680. continue;
  681. spin_lock(&vm->status_lock);
  682. if (list_empty(&entry->base.vm_status))
  683. list_add(&entry->base.vm_status, &vm->relocated);
  684. spin_unlock(&vm->status_lock);
  685. amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
  686. }
  687. }
  688. /*
  689. * amdgpu_vm_update_directories - make sure that all directories are valid
  690. *
  691. * @adev: amdgpu_device pointer
  692. * @vm: requested vm
  693. *
  694. * Makes sure all directories are up to date.
  695. * Returns 0 for success, error for failure.
  696. */
  697. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  698. struct amdgpu_vm *vm)
  699. {
  700. struct amdgpu_pte_update_params params;
  701. struct amdgpu_job *job;
  702. unsigned ndw = 0;
  703. int r = 0;
  704. if (list_empty(&vm->relocated))
  705. return 0;
  706. restart:
  707. memset(&params, 0, sizeof(params));
  708. params.adev = adev;
  709. if (vm->use_cpu_for_update) {
  710. r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
  711. if (unlikely(r))
  712. return r;
  713. params.func = amdgpu_vm_cpu_set_ptes;
  714. } else {
  715. ndw = 512 * 8;
  716. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  717. if (r)
  718. return r;
  719. params.ib = &job->ibs[0];
  720. params.func = amdgpu_vm_do_set_ptes;
  721. }
  722. spin_lock(&vm->status_lock);
  723. while (!list_empty(&vm->relocated)) {
  724. struct amdgpu_vm_bo_base *bo_base, *parent;
  725. struct amdgpu_vm_pt *pt, *entry;
  726. struct amdgpu_bo *bo;
  727. bo_base = list_first_entry(&vm->relocated,
  728. struct amdgpu_vm_bo_base,
  729. vm_status);
  730. list_del_init(&bo_base->vm_status);
  731. spin_unlock(&vm->status_lock);
  732. bo = bo_base->bo->parent;
  733. if (!bo) {
  734. spin_lock(&vm->status_lock);
  735. continue;
  736. }
  737. parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
  738. bo_list);
  739. pt = container_of(parent, struct amdgpu_vm_pt, base);
  740. entry = container_of(bo_base, struct amdgpu_vm_pt, base);
  741. amdgpu_vm_update_pde(&params, vm, pt, entry);
  742. spin_lock(&vm->status_lock);
  743. if (!vm->use_cpu_for_update &&
  744. (ndw - params.ib->length_dw) < 32)
  745. break;
  746. }
  747. spin_unlock(&vm->status_lock);
  748. if (vm->use_cpu_for_update) {
  749. /* Flush HDP */
  750. mb();
  751. amdgpu_gart_flush_gpu_tlb(adev, 0);
  752. } else if (params.ib->length_dw == 0) {
  753. amdgpu_job_free(job);
  754. } else {
  755. struct amdgpu_bo *root = vm->root.base.bo;
  756. struct amdgpu_ring *ring;
  757. struct dma_fence *fence;
  758. ring = container_of(vm->entity.sched, struct amdgpu_ring,
  759. sched);
  760. amdgpu_ring_pad_ib(ring, params.ib);
  761. amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
  762. AMDGPU_FENCE_OWNER_VM, false);
  763. if (root->shadow)
  764. amdgpu_sync_resv(adev, &job->sync,
  765. root->shadow->tbo.resv,
  766. AMDGPU_FENCE_OWNER_VM, false);
  767. WARN_ON(params.ib->length_dw > ndw);
  768. r = amdgpu_job_submit(job, ring, &vm->entity,
  769. AMDGPU_FENCE_OWNER_VM, &fence);
  770. if (r)
  771. goto error;
  772. amdgpu_bo_fence(root, fence, true);
  773. dma_fence_put(vm->last_update);
  774. vm->last_update = fence;
  775. }
  776. if (!list_empty(&vm->relocated))
  777. goto restart;
  778. return 0;
  779. error:
  780. amdgpu_vm_invalidate_level(adev, vm, &vm->root,
  781. adev->vm_manager.root_level);
  782. amdgpu_job_free(job);
  783. return r;
  784. }
  785. /**
  786. * amdgpu_vm_find_entry - find the entry for an address
  787. *
  788. * @p: see amdgpu_pte_update_params definition
  789. * @addr: virtual address in question
  790. * @entry: resulting entry or NULL
  791. * @parent: parent entry
  792. *
  793. * Find the vm_pt entry and it's parent for the given address.
  794. */
  795. void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
  796. struct amdgpu_vm_pt **entry,
  797. struct amdgpu_vm_pt **parent)
  798. {
  799. unsigned level = p->adev->vm_manager.root_level;
  800. *parent = NULL;
  801. *entry = &p->vm->root;
  802. while ((*entry)->entries) {
  803. unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
  804. *parent = *entry;
  805. *entry = &(*entry)->entries[addr >> shift];
  806. addr &= (1ULL << shift) - 1;
  807. }
  808. if (level != AMDGPU_VM_PTB)
  809. *entry = NULL;
  810. }
  811. /**
  812. * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
  813. *
  814. * @p: see amdgpu_pte_update_params definition
  815. * @entry: vm_pt entry to check
  816. * @parent: parent entry
  817. * @nptes: number of PTEs updated with this operation
  818. * @dst: destination address where the PTEs should point to
  819. * @flags: access flags fro the PTEs
  820. *
  821. * Check if we can update the PD with a huge page.
  822. */
  823. static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
  824. struct amdgpu_vm_pt *entry,
  825. struct amdgpu_vm_pt *parent,
  826. unsigned nptes, uint64_t dst,
  827. uint64_t flags)
  828. {
  829. bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
  830. uint64_t pd_addr, pde;
  831. /* In the case of a mixed PT the PDE must point to it*/
  832. if (p->adev->asic_type < CHIP_VEGA10 ||
  833. nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
  834. p->src ||
  835. !(flags & AMDGPU_PTE_VALID)) {
  836. dst = amdgpu_bo_gpu_offset(entry->base.bo);
  837. flags = AMDGPU_PTE_VALID;
  838. } else {
  839. /* Set the huge page flag to stop scanning at this PDE */
  840. flags |= AMDGPU_PDE_PTE;
  841. }
  842. if (!entry->huge && !(flags & AMDGPU_PDE_PTE))
  843. return;
  844. entry->huge = !!(flags & AMDGPU_PDE_PTE);
  845. amdgpu_gart_get_vm_pde(p->adev, AMDGPU_VM_PDB0,
  846. &dst, &flags);
  847. if (use_cpu_update) {
  848. /* In case a huge page is replaced with a system
  849. * memory mapping, p->pages_addr != NULL and
  850. * amdgpu_vm_cpu_set_ptes would try to translate dst
  851. * through amdgpu_vm_map_gart. But dst is already a
  852. * GPU address (of the page table). Disable
  853. * amdgpu_vm_map_gart temporarily.
  854. */
  855. dma_addr_t *tmp;
  856. tmp = p->pages_addr;
  857. p->pages_addr = NULL;
  858. pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
  859. pde = pd_addr + (entry - parent->entries) * 8;
  860. amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
  861. p->pages_addr = tmp;
  862. } else {
  863. if (parent->base.bo->shadow) {
  864. pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
  865. pde = pd_addr + (entry - parent->entries) * 8;
  866. amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
  867. }
  868. pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
  869. pde = pd_addr + (entry - parent->entries) * 8;
  870. amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
  871. }
  872. }
  873. /**
  874. * amdgpu_vm_update_ptes - make sure that page tables are valid
  875. *
  876. * @params: see amdgpu_pte_update_params definition
  877. * @vm: requested vm
  878. * @start: start of GPU address range
  879. * @end: end of GPU address range
  880. * @dst: destination address to map to, the next dst inside the function
  881. * @flags: mapping flags
  882. *
  883. * Update the page tables in the range @start - @end.
  884. * Returns 0 for success, -EINVAL for failure.
  885. */
  886. static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  887. uint64_t start, uint64_t end,
  888. uint64_t dst, uint64_t flags)
  889. {
  890. struct amdgpu_device *adev = params->adev;
  891. const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
  892. uint64_t addr, pe_start;
  893. struct amdgpu_bo *pt;
  894. unsigned nptes;
  895. bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
  896. /* walk over the address space and update the page tables */
  897. for (addr = start; addr < end; addr += nptes,
  898. dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
  899. struct amdgpu_vm_pt *entry, *parent;
  900. amdgpu_vm_get_entry(params, addr, &entry, &parent);
  901. if (!entry)
  902. return -ENOENT;
  903. if ((addr & ~mask) == (end & ~mask))
  904. nptes = end - addr;
  905. else
  906. nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
  907. amdgpu_vm_handle_huge_pages(params, entry, parent,
  908. nptes, dst, flags);
  909. /* We don't need to update PTEs for huge pages */
  910. if (entry->huge)
  911. continue;
  912. pt = entry->base.bo;
  913. if (use_cpu_update) {
  914. pe_start = (unsigned long)amdgpu_bo_kptr(pt);
  915. } else {
  916. if (pt->shadow) {
  917. pe_start = amdgpu_bo_gpu_offset(pt->shadow);
  918. pe_start += (addr & mask) * 8;
  919. params->func(params, pe_start, dst, nptes,
  920. AMDGPU_GPU_PAGE_SIZE, flags);
  921. }
  922. pe_start = amdgpu_bo_gpu_offset(pt);
  923. }
  924. pe_start += (addr & mask) * 8;
  925. params->func(params, pe_start, dst, nptes,
  926. AMDGPU_GPU_PAGE_SIZE, flags);
  927. }
  928. return 0;
  929. }
  930. /*
  931. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  932. *
  933. * @params: see amdgpu_pte_update_params definition
  934. * @vm: requested vm
  935. * @start: first PTE to handle
  936. * @end: last PTE to handle
  937. * @dst: addr those PTEs should point to
  938. * @flags: hw mapping flags
  939. * Returns 0 for success, -EINVAL for failure.
  940. */
  941. static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  942. uint64_t start, uint64_t end,
  943. uint64_t dst, uint64_t flags)
  944. {
  945. /**
  946. * The MC L1 TLB supports variable sized pages, based on a fragment
  947. * field in the PTE. When this field is set to a non-zero value, page
  948. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  949. * flags are considered valid for all PTEs within the fragment range
  950. * and corresponding mappings are assumed to be physically contiguous.
  951. *
  952. * The L1 TLB can store a single PTE for the whole fragment,
  953. * significantly increasing the space available for translation
  954. * caching. This leads to large improvements in throughput when the
  955. * TLB is under pressure.
  956. *
  957. * The L2 TLB distributes small and large fragments into two
  958. * asymmetric partitions. The large fragment cache is significantly
  959. * larger. Thus, we try to use large fragments wherever possible.
  960. * Userspace can support this by aligning virtual base address and
  961. * allocation size to the fragment size.
  962. */
  963. unsigned max_frag = params->adev->vm_manager.fragment_size;
  964. int r;
  965. /* system pages are non continuously */
  966. if (params->src || !(flags & AMDGPU_PTE_VALID))
  967. return amdgpu_vm_update_ptes(params, start, end, dst, flags);
  968. while (start != end) {
  969. uint64_t frag_flags, frag_end;
  970. unsigned frag;
  971. /* This intentionally wraps around if no bit is set */
  972. frag = min((unsigned)ffs(start) - 1,
  973. (unsigned)fls64(end - start) - 1);
  974. if (frag >= max_frag) {
  975. frag_flags = AMDGPU_PTE_FRAG(max_frag);
  976. frag_end = end & ~((1ULL << max_frag) - 1);
  977. } else {
  978. frag_flags = AMDGPU_PTE_FRAG(frag);
  979. frag_end = start + (1 << frag);
  980. }
  981. r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
  982. flags | frag_flags);
  983. if (r)
  984. return r;
  985. dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
  986. start = frag_end;
  987. }
  988. return 0;
  989. }
  990. /**
  991. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  992. *
  993. * @adev: amdgpu_device pointer
  994. * @exclusive: fence we need to sync to
  995. * @pages_addr: DMA addresses to use for mapping
  996. * @vm: requested vm
  997. * @start: start of mapped range
  998. * @last: last mapped entry
  999. * @flags: flags for the entries
  1000. * @addr: addr to set the area to
  1001. * @fence: optional resulting fence
  1002. *
  1003. * Fill in the page table entries between @start and @last.
  1004. * Returns 0 for success, -EINVAL for failure.
  1005. */
  1006. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  1007. struct dma_fence *exclusive,
  1008. dma_addr_t *pages_addr,
  1009. struct amdgpu_vm *vm,
  1010. uint64_t start, uint64_t last,
  1011. uint64_t flags, uint64_t addr,
  1012. struct dma_fence **fence)
  1013. {
  1014. struct amdgpu_ring *ring;
  1015. void *owner = AMDGPU_FENCE_OWNER_VM;
  1016. unsigned nptes, ncmds, ndw;
  1017. struct amdgpu_job *job;
  1018. struct amdgpu_pte_update_params params;
  1019. struct dma_fence *f = NULL;
  1020. int r;
  1021. memset(&params, 0, sizeof(params));
  1022. params.adev = adev;
  1023. params.vm = vm;
  1024. /* sync to everything on unmapping */
  1025. if (!(flags & AMDGPU_PTE_VALID))
  1026. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  1027. if (vm->use_cpu_for_update) {
  1028. /* params.src is used as flag to indicate system Memory */
  1029. if (pages_addr)
  1030. params.src = ~0;
  1031. /* Wait for PT BOs to be free. PTs share the same resv. object
  1032. * as the root PD BO
  1033. */
  1034. r = amdgpu_vm_wait_pd(adev, vm, owner);
  1035. if (unlikely(r))
  1036. return r;
  1037. params.func = amdgpu_vm_cpu_set_ptes;
  1038. params.pages_addr = pages_addr;
  1039. return amdgpu_vm_frag_ptes(&params, start, last + 1,
  1040. addr, flags);
  1041. }
  1042. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  1043. nptes = last - start + 1;
  1044. /*
  1045. * reserve space for two commands every (1 << BLOCK_SIZE)
  1046. * entries or 2k dwords (whatever is smaller)
  1047. *
  1048. * The second command is for the shadow pagetables.
  1049. */
  1050. ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
  1051. /* padding, etc. */
  1052. ndw = 64;
  1053. /* one PDE write for each huge page */
  1054. ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
  1055. if (pages_addr) {
  1056. /* copy commands needed */
  1057. ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
  1058. /* and also PTEs */
  1059. ndw += nptes * 2;
  1060. params.func = amdgpu_vm_do_copy_ptes;
  1061. } else {
  1062. /* set page commands needed */
  1063. ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
  1064. /* extra commands for begin/end fragments */
  1065. ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
  1066. * adev->vm_manager.fragment_size;
  1067. params.func = amdgpu_vm_do_set_ptes;
  1068. }
  1069. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  1070. if (r)
  1071. return r;
  1072. params.ib = &job->ibs[0];
  1073. if (pages_addr) {
  1074. uint64_t *pte;
  1075. unsigned i;
  1076. /* Put the PTEs at the end of the IB. */
  1077. i = ndw - nptes * 2;
  1078. pte= (uint64_t *)&(job->ibs->ptr[i]);
  1079. params.src = job->ibs->gpu_addr + i * 4;
  1080. for (i = 0; i < nptes; ++i) {
  1081. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  1082. AMDGPU_GPU_PAGE_SIZE);
  1083. pte[i] |= flags;
  1084. }
  1085. addr = 0;
  1086. }
  1087. r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
  1088. if (r)
  1089. goto error_free;
  1090. r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
  1091. owner, false);
  1092. if (r)
  1093. goto error_free;
  1094. r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
  1095. if (r)
  1096. goto error_free;
  1097. r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1098. if (r)
  1099. goto error_free;
  1100. amdgpu_ring_pad_ib(ring, params.ib);
  1101. WARN_ON(params.ib->length_dw > ndw);
  1102. r = amdgpu_job_submit(job, ring, &vm->entity,
  1103. AMDGPU_FENCE_OWNER_VM, &f);
  1104. if (r)
  1105. goto error_free;
  1106. amdgpu_bo_fence(vm->root.base.bo, f, true);
  1107. dma_fence_put(*fence);
  1108. *fence = f;
  1109. return 0;
  1110. error_free:
  1111. amdgpu_job_free(job);
  1112. amdgpu_vm_invalidate_level(adev, vm, &vm->root,
  1113. adev->vm_manager.root_level);
  1114. return r;
  1115. }
  1116. /**
  1117. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  1118. *
  1119. * @adev: amdgpu_device pointer
  1120. * @exclusive: fence we need to sync to
  1121. * @pages_addr: DMA addresses to use for mapping
  1122. * @vm: requested vm
  1123. * @mapping: mapped range and flags to use for the update
  1124. * @flags: HW flags for the mapping
  1125. * @nodes: array of drm_mm_nodes with the MC addresses
  1126. * @fence: optional resulting fence
  1127. *
  1128. * Split the mapping into smaller chunks so that each update fits
  1129. * into a SDMA IB.
  1130. * Returns 0 for success, -EINVAL for failure.
  1131. */
  1132. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  1133. struct dma_fence *exclusive,
  1134. dma_addr_t *pages_addr,
  1135. struct amdgpu_vm *vm,
  1136. struct amdgpu_bo_va_mapping *mapping,
  1137. uint64_t flags,
  1138. struct drm_mm_node *nodes,
  1139. struct dma_fence **fence)
  1140. {
  1141. unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
  1142. uint64_t pfn, start = mapping->start;
  1143. int r;
  1144. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  1145. * but in case of something, we filter the flags in first place
  1146. */
  1147. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  1148. flags &= ~AMDGPU_PTE_READABLE;
  1149. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  1150. flags &= ~AMDGPU_PTE_WRITEABLE;
  1151. flags &= ~AMDGPU_PTE_EXECUTABLE;
  1152. flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
  1153. flags &= ~AMDGPU_PTE_MTYPE_MASK;
  1154. flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
  1155. if ((mapping->flags & AMDGPU_PTE_PRT) &&
  1156. (adev->asic_type >= CHIP_VEGA10)) {
  1157. flags |= AMDGPU_PTE_PRT;
  1158. flags &= ~AMDGPU_PTE_VALID;
  1159. }
  1160. trace_amdgpu_vm_bo_update(mapping);
  1161. pfn = mapping->offset >> PAGE_SHIFT;
  1162. if (nodes) {
  1163. while (pfn >= nodes->size) {
  1164. pfn -= nodes->size;
  1165. ++nodes;
  1166. }
  1167. }
  1168. do {
  1169. dma_addr_t *dma_addr = NULL;
  1170. uint64_t max_entries;
  1171. uint64_t addr, last;
  1172. if (nodes) {
  1173. addr = nodes->start << PAGE_SHIFT;
  1174. max_entries = (nodes->size - pfn) *
  1175. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  1176. } else {
  1177. addr = 0;
  1178. max_entries = S64_MAX;
  1179. }
  1180. if (pages_addr) {
  1181. uint64_t count;
  1182. max_entries = min(max_entries, 16ull * 1024ull);
  1183. for (count = 1; count < max_entries; ++count) {
  1184. uint64_t idx = pfn + count;
  1185. if (pages_addr[idx] !=
  1186. (pages_addr[idx - 1] + PAGE_SIZE))
  1187. break;
  1188. }
  1189. if (count < min_linear_pages) {
  1190. addr = pfn << PAGE_SHIFT;
  1191. dma_addr = pages_addr;
  1192. } else {
  1193. addr = pages_addr[pfn];
  1194. max_entries = count;
  1195. }
  1196. } else if (flags & AMDGPU_PTE_VALID) {
  1197. addr += adev->vm_manager.vram_base_offset;
  1198. addr += pfn << PAGE_SHIFT;
  1199. }
  1200. last = min((uint64_t)mapping->last, start + max_entries - 1);
  1201. r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
  1202. start, last, flags, addr,
  1203. fence);
  1204. if (r)
  1205. return r;
  1206. pfn += last - start + 1;
  1207. if (nodes && nodes->size == pfn) {
  1208. pfn = 0;
  1209. ++nodes;
  1210. }
  1211. start = last + 1;
  1212. } while (unlikely(start != mapping->last + 1));
  1213. return 0;
  1214. }
  1215. /**
  1216. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  1217. *
  1218. * @adev: amdgpu_device pointer
  1219. * @bo_va: requested BO and VM object
  1220. * @clear: if true clear the entries
  1221. *
  1222. * Fill in the page table entries for @bo_va.
  1223. * Returns 0 for success, -EINVAL for failure.
  1224. */
  1225. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  1226. struct amdgpu_bo_va *bo_va,
  1227. bool clear)
  1228. {
  1229. struct amdgpu_bo *bo = bo_va->base.bo;
  1230. struct amdgpu_vm *vm = bo_va->base.vm;
  1231. struct amdgpu_bo_va_mapping *mapping;
  1232. dma_addr_t *pages_addr = NULL;
  1233. struct ttm_mem_reg *mem;
  1234. struct drm_mm_node *nodes;
  1235. struct dma_fence *exclusive, **last_update;
  1236. uint64_t flags;
  1237. int r;
  1238. if (clear || !bo_va->base.bo) {
  1239. mem = NULL;
  1240. nodes = NULL;
  1241. exclusive = NULL;
  1242. } else {
  1243. struct ttm_dma_tt *ttm;
  1244. mem = &bo_va->base.bo->tbo.mem;
  1245. nodes = mem->mm_node;
  1246. if (mem->mem_type == TTM_PL_TT) {
  1247. ttm = container_of(bo_va->base.bo->tbo.ttm,
  1248. struct ttm_dma_tt, ttm);
  1249. pages_addr = ttm->dma_address;
  1250. }
  1251. exclusive = reservation_object_get_excl(bo->tbo.resv);
  1252. }
  1253. if (bo)
  1254. flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
  1255. else
  1256. flags = 0x0;
  1257. if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
  1258. last_update = &vm->last_update;
  1259. else
  1260. last_update = &bo_va->last_pt_update;
  1261. if (!clear && bo_va->base.moved) {
  1262. bo_va->base.moved = false;
  1263. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1264. } else if (bo_va->cleared != clear) {
  1265. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1266. }
  1267. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1268. r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
  1269. mapping, flags, nodes,
  1270. last_update);
  1271. if (r)
  1272. return r;
  1273. }
  1274. if (vm->use_cpu_for_update) {
  1275. /* Flush HDP */
  1276. mb();
  1277. amdgpu_gart_flush_gpu_tlb(adev, 0);
  1278. }
  1279. spin_lock(&vm->status_lock);
  1280. list_del_init(&bo_va->base.vm_status);
  1281. spin_unlock(&vm->status_lock);
  1282. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1283. bo_va->cleared = clear;
  1284. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1285. list_for_each_entry(mapping, &bo_va->valids, list)
  1286. trace_amdgpu_vm_bo_mapping(mapping);
  1287. }
  1288. return 0;
  1289. }
  1290. /**
  1291. * amdgpu_vm_update_prt_state - update the global PRT state
  1292. */
  1293. static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
  1294. {
  1295. unsigned long flags;
  1296. bool enable;
  1297. spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
  1298. enable = !!atomic_read(&adev->vm_manager.num_prt_users);
  1299. adev->gart.gart_funcs->set_prt(adev, enable);
  1300. spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
  1301. }
  1302. /**
  1303. * amdgpu_vm_prt_get - add a PRT user
  1304. */
  1305. static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
  1306. {
  1307. if (!adev->gart.gart_funcs->set_prt)
  1308. return;
  1309. if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
  1310. amdgpu_vm_update_prt_state(adev);
  1311. }
  1312. /**
  1313. * amdgpu_vm_prt_put - drop a PRT user
  1314. */
  1315. static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
  1316. {
  1317. if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
  1318. amdgpu_vm_update_prt_state(adev);
  1319. }
  1320. /**
  1321. * amdgpu_vm_prt_cb - callback for updating the PRT status
  1322. */
  1323. static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
  1324. {
  1325. struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
  1326. amdgpu_vm_prt_put(cb->adev);
  1327. kfree(cb);
  1328. }
  1329. /**
  1330. * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
  1331. */
  1332. static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
  1333. struct dma_fence *fence)
  1334. {
  1335. struct amdgpu_prt_cb *cb;
  1336. if (!adev->gart.gart_funcs->set_prt)
  1337. return;
  1338. cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
  1339. if (!cb) {
  1340. /* Last resort when we are OOM */
  1341. if (fence)
  1342. dma_fence_wait(fence, false);
  1343. amdgpu_vm_prt_put(adev);
  1344. } else {
  1345. cb->adev = adev;
  1346. if (!fence || dma_fence_add_callback(fence, &cb->cb,
  1347. amdgpu_vm_prt_cb))
  1348. amdgpu_vm_prt_cb(fence, &cb->cb);
  1349. }
  1350. }
  1351. /**
  1352. * amdgpu_vm_free_mapping - free a mapping
  1353. *
  1354. * @adev: amdgpu_device pointer
  1355. * @vm: requested vm
  1356. * @mapping: mapping to be freed
  1357. * @fence: fence of the unmap operation
  1358. *
  1359. * Free a mapping and make sure we decrease the PRT usage count if applicable.
  1360. */
  1361. static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
  1362. struct amdgpu_vm *vm,
  1363. struct amdgpu_bo_va_mapping *mapping,
  1364. struct dma_fence *fence)
  1365. {
  1366. if (mapping->flags & AMDGPU_PTE_PRT)
  1367. amdgpu_vm_add_prt_cb(adev, fence);
  1368. kfree(mapping);
  1369. }
  1370. /**
  1371. * amdgpu_vm_prt_fini - finish all prt mappings
  1372. *
  1373. * @adev: amdgpu_device pointer
  1374. * @vm: requested vm
  1375. *
  1376. * Register a cleanup callback to disable PRT support after VM dies.
  1377. */
  1378. static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1379. {
  1380. struct reservation_object *resv = vm->root.base.bo->tbo.resv;
  1381. struct dma_fence *excl, **shared;
  1382. unsigned i, shared_count;
  1383. int r;
  1384. r = reservation_object_get_fences_rcu(resv, &excl,
  1385. &shared_count, &shared);
  1386. if (r) {
  1387. /* Not enough memory to grab the fence list, as last resort
  1388. * block for all the fences to complete.
  1389. */
  1390. reservation_object_wait_timeout_rcu(resv, true, false,
  1391. MAX_SCHEDULE_TIMEOUT);
  1392. return;
  1393. }
  1394. /* Add a callback for each fence in the reservation object */
  1395. amdgpu_vm_prt_get(adev);
  1396. amdgpu_vm_add_prt_cb(adev, excl);
  1397. for (i = 0; i < shared_count; ++i) {
  1398. amdgpu_vm_prt_get(adev);
  1399. amdgpu_vm_add_prt_cb(adev, shared[i]);
  1400. }
  1401. kfree(shared);
  1402. }
  1403. /**
  1404. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1405. *
  1406. * @adev: amdgpu_device pointer
  1407. * @vm: requested vm
  1408. * @fence: optional resulting fence (unchanged if no work needed to be done
  1409. * or if an error occurred)
  1410. *
  1411. * Make sure all freed BOs are cleared in the PT.
  1412. * Returns 0 for success.
  1413. *
  1414. * PTs have to be reserved and mutex must be locked!
  1415. */
  1416. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1417. struct amdgpu_vm *vm,
  1418. struct dma_fence **fence)
  1419. {
  1420. struct amdgpu_bo_va_mapping *mapping;
  1421. struct dma_fence *f = NULL;
  1422. int r;
  1423. uint64_t init_pte_value = 0;
  1424. while (!list_empty(&vm->freed)) {
  1425. mapping = list_first_entry(&vm->freed,
  1426. struct amdgpu_bo_va_mapping, list);
  1427. list_del(&mapping->list);
  1428. if (vm->pte_support_ats)
  1429. init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
  1430. r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
  1431. mapping->start, mapping->last,
  1432. init_pte_value, 0, &f);
  1433. amdgpu_vm_free_mapping(adev, vm, mapping, f);
  1434. if (r) {
  1435. dma_fence_put(f);
  1436. return r;
  1437. }
  1438. }
  1439. if (fence && f) {
  1440. dma_fence_put(*fence);
  1441. *fence = f;
  1442. } else {
  1443. dma_fence_put(f);
  1444. }
  1445. return 0;
  1446. }
  1447. /**
  1448. * amdgpu_vm_handle_moved - handle moved BOs in the PT
  1449. *
  1450. * @adev: amdgpu_device pointer
  1451. * @vm: requested vm
  1452. * @sync: sync object to add fences to
  1453. *
  1454. * Make sure all BOs which are moved are updated in the PTs.
  1455. * Returns 0 for success.
  1456. *
  1457. * PTs have to be reserved!
  1458. */
  1459. int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
  1460. struct amdgpu_vm *vm)
  1461. {
  1462. bool clear;
  1463. int r = 0;
  1464. spin_lock(&vm->status_lock);
  1465. while (!list_empty(&vm->moved)) {
  1466. struct amdgpu_bo_va *bo_va;
  1467. bo_va = list_first_entry(&vm->moved,
  1468. struct amdgpu_bo_va, base.vm_status);
  1469. spin_unlock(&vm->status_lock);
  1470. /* Per VM BOs never need to bo cleared in the page tables */
  1471. clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
  1472. r = amdgpu_vm_bo_update(adev, bo_va, clear);
  1473. if (r)
  1474. return r;
  1475. spin_lock(&vm->status_lock);
  1476. }
  1477. spin_unlock(&vm->status_lock);
  1478. return r;
  1479. }
  1480. /**
  1481. * amdgpu_vm_bo_add - add a bo to a specific vm
  1482. *
  1483. * @adev: amdgpu_device pointer
  1484. * @vm: requested vm
  1485. * @bo: amdgpu buffer object
  1486. *
  1487. * Add @bo into the requested vm.
  1488. * Add @bo to the list of bos associated with the vm
  1489. * Returns newly added bo_va or NULL for failure
  1490. *
  1491. * Object has to be reserved!
  1492. */
  1493. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1494. struct amdgpu_vm *vm,
  1495. struct amdgpu_bo *bo)
  1496. {
  1497. struct amdgpu_bo_va *bo_va;
  1498. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1499. if (bo_va == NULL) {
  1500. return NULL;
  1501. }
  1502. bo_va->base.vm = vm;
  1503. bo_va->base.bo = bo;
  1504. INIT_LIST_HEAD(&bo_va->base.bo_list);
  1505. INIT_LIST_HEAD(&bo_va->base.vm_status);
  1506. bo_va->ref_count = 1;
  1507. INIT_LIST_HEAD(&bo_va->valids);
  1508. INIT_LIST_HEAD(&bo_va->invalids);
  1509. if (bo)
  1510. list_add_tail(&bo_va->base.bo_list, &bo->va);
  1511. return bo_va;
  1512. }
  1513. /**
  1514. * amdgpu_vm_bo_insert_mapping - insert a new mapping
  1515. *
  1516. * @adev: amdgpu_device pointer
  1517. * @bo_va: bo_va to store the address
  1518. * @mapping: the mapping to insert
  1519. *
  1520. * Insert a new mapping into all structures.
  1521. */
  1522. static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
  1523. struct amdgpu_bo_va *bo_va,
  1524. struct amdgpu_bo_va_mapping *mapping)
  1525. {
  1526. struct amdgpu_vm *vm = bo_va->base.vm;
  1527. struct amdgpu_bo *bo = bo_va->base.bo;
  1528. mapping->bo_va = bo_va;
  1529. list_add(&mapping->list, &bo_va->invalids);
  1530. amdgpu_vm_it_insert(mapping, &vm->va);
  1531. if (mapping->flags & AMDGPU_PTE_PRT)
  1532. amdgpu_vm_prt_get(adev);
  1533. if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
  1534. spin_lock(&vm->status_lock);
  1535. if (list_empty(&bo_va->base.vm_status))
  1536. list_add(&bo_va->base.vm_status, &vm->moved);
  1537. spin_unlock(&vm->status_lock);
  1538. }
  1539. trace_amdgpu_vm_bo_map(bo_va, mapping);
  1540. }
  1541. /**
  1542. * amdgpu_vm_bo_map - map bo inside a vm
  1543. *
  1544. * @adev: amdgpu_device pointer
  1545. * @bo_va: bo_va to store the address
  1546. * @saddr: where to map the BO
  1547. * @offset: requested offset in the BO
  1548. * @flags: attributes of pages (read/write/valid/etc.)
  1549. *
  1550. * Add a mapping of the BO at the specefied addr into the VM.
  1551. * Returns 0 for success, error for failure.
  1552. *
  1553. * Object has to be reserved and unreserved outside!
  1554. */
  1555. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1556. struct amdgpu_bo_va *bo_va,
  1557. uint64_t saddr, uint64_t offset,
  1558. uint64_t size, uint64_t flags)
  1559. {
  1560. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1561. struct amdgpu_bo *bo = bo_va->base.bo;
  1562. struct amdgpu_vm *vm = bo_va->base.vm;
  1563. uint64_t eaddr;
  1564. /* validate the parameters */
  1565. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1566. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1567. return -EINVAL;
  1568. /* make sure object fit at this offset */
  1569. eaddr = saddr + size - 1;
  1570. if (saddr >= eaddr ||
  1571. (bo && offset + size > amdgpu_bo_size(bo)))
  1572. return -EINVAL;
  1573. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1574. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1575. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1576. if (tmp) {
  1577. /* bo and tmp overlap, invalid addr */
  1578. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1579. "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
  1580. tmp->start, tmp->last + 1);
  1581. return -EINVAL;
  1582. }
  1583. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1584. if (!mapping)
  1585. return -ENOMEM;
  1586. mapping->start = saddr;
  1587. mapping->last = eaddr;
  1588. mapping->offset = offset;
  1589. mapping->flags = flags;
  1590. amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  1591. return 0;
  1592. }
  1593. /**
  1594. * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
  1595. *
  1596. * @adev: amdgpu_device pointer
  1597. * @bo_va: bo_va to store the address
  1598. * @saddr: where to map the BO
  1599. * @offset: requested offset in the BO
  1600. * @flags: attributes of pages (read/write/valid/etc.)
  1601. *
  1602. * Add a mapping of the BO at the specefied addr into the VM. Replace existing
  1603. * mappings as we do so.
  1604. * Returns 0 for success, error for failure.
  1605. *
  1606. * Object has to be reserved and unreserved outside!
  1607. */
  1608. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  1609. struct amdgpu_bo_va *bo_va,
  1610. uint64_t saddr, uint64_t offset,
  1611. uint64_t size, uint64_t flags)
  1612. {
  1613. struct amdgpu_bo_va_mapping *mapping;
  1614. struct amdgpu_bo *bo = bo_va->base.bo;
  1615. uint64_t eaddr;
  1616. int r;
  1617. /* validate the parameters */
  1618. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1619. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1620. return -EINVAL;
  1621. /* make sure object fit at this offset */
  1622. eaddr = saddr + size - 1;
  1623. if (saddr >= eaddr ||
  1624. (bo && offset + size > amdgpu_bo_size(bo)))
  1625. return -EINVAL;
  1626. /* Allocate all the needed memory */
  1627. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1628. if (!mapping)
  1629. return -ENOMEM;
  1630. r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
  1631. if (r) {
  1632. kfree(mapping);
  1633. return r;
  1634. }
  1635. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1636. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1637. mapping->start = saddr;
  1638. mapping->last = eaddr;
  1639. mapping->offset = offset;
  1640. mapping->flags = flags;
  1641. amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  1642. return 0;
  1643. }
  1644. /**
  1645. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1646. *
  1647. * @adev: amdgpu_device pointer
  1648. * @bo_va: bo_va to remove the address from
  1649. * @saddr: where to the BO is mapped
  1650. *
  1651. * Remove a mapping of the BO at the specefied addr from the VM.
  1652. * Returns 0 for success, error for failure.
  1653. *
  1654. * Object has to be reserved and unreserved outside!
  1655. */
  1656. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1657. struct amdgpu_bo_va *bo_va,
  1658. uint64_t saddr)
  1659. {
  1660. struct amdgpu_bo_va_mapping *mapping;
  1661. struct amdgpu_vm *vm = bo_va->base.vm;
  1662. bool valid = true;
  1663. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1664. list_for_each_entry(mapping, &bo_va->valids, list) {
  1665. if (mapping->start == saddr)
  1666. break;
  1667. }
  1668. if (&mapping->list == &bo_va->valids) {
  1669. valid = false;
  1670. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1671. if (mapping->start == saddr)
  1672. break;
  1673. }
  1674. if (&mapping->list == &bo_va->invalids)
  1675. return -ENOENT;
  1676. }
  1677. list_del(&mapping->list);
  1678. amdgpu_vm_it_remove(mapping, &vm->va);
  1679. mapping->bo_va = NULL;
  1680. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1681. if (valid)
  1682. list_add(&mapping->list, &vm->freed);
  1683. else
  1684. amdgpu_vm_free_mapping(adev, vm, mapping,
  1685. bo_va->last_pt_update);
  1686. return 0;
  1687. }
  1688. /**
  1689. * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
  1690. *
  1691. * @adev: amdgpu_device pointer
  1692. * @vm: VM structure to use
  1693. * @saddr: start of the range
  1694. * @size: size of the range
  1695. *
  1696. * Remove all mappings in a range, split them as appropriate.
  1697. * Returns 0 for success, error for failure.
  1698. */
  1699. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  1700. struct amdgpu_vm *vm,
  1701. uint64_t saddr, uint64_t size)
  1702. {
  1703. struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
  1704. LIST_HEAD(removed);
  1705. uint64_t eaddr;
  1706. eaddr = saddr + size - 1;
  1707. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1708. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1709. /* Allocate all the needed memory */
  1710. before = kzalloc(sizeof(*before), GFP_KERNEL);
  1711. if (!before)
  1712. return -ENOMEM;
  1713. INIT_LIST_HEAD(&before->list);
  1714. after = kzalloc(sizeof(*after), GFP_KERNEL);
  1715. if (!after) {
  1716. kfree(before);
  1717. return -ENOMEM;
  1718. }
  1719. INIT_LIST_HEAD(&after->list);
  1720. /* Now gather all removed mappings */
  1721. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1722. while (tmp) {
  1723. /* Remember mapping split at the start */
  1724. if (tmp->start < saddr) {
  1725. before->start = tmp->start;
  1726. before->last = saddr - 1;
  1727. before->offset = tmp->offset;
  1728. before->flags = tmp->flags;
  1729. list_add(&before->list, &tmp->list);
  1730. }
  1731. /* Remember mapping split at the end */
  1732. if (tmp->last > eaddr) {
  1733. after->start = eaddr + 1;
  1734. after->last = tmp->last;
  1735. after->offset = tmp->offset;
  1736. after->offset += after->start - tmp->start;
  1737. after->flags = tmp->flags;
  1738. list_add(&after->list, &tmp->list);
  1739. }
  1740. list_del(&tmp->list);
  1741. list_add(&tmp->list, &removed);
  1742. tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
  1743. }
  1744. /* And free them up */
  1745. list_for_each_entry_safe(tmp, next, &removed, list) {
  1746. amdgpu_vm_it_remove(tmp, &vm->va);
  1747. list_del(&tmp->list);
  1748. if (tmp->start < saddr)
  1749. tmp->start = saddr;
  1750. if (tmp->last > eaddr)
  1751. tmp->last = eaddr;
  1752. tmp->bo_va = NULL;
  1753. list_add(&tmp->list, &vm->freed);
  1754. trace_amdgpu_vm_bo_unmap(NULL, tmp);
  1755. }
  1756. /* Insert partial mapping before the range */
  1757. if (!list_empty(&before->list)) {
  1758. amdgpu_vm_it_insert(before, &vm->va);
  1759. if (before->flags & AMDGPU_PTE_PRT)
  1760. amdgpu_vm_prt_get(adev);
  1761. } else {
  1762. kfree(before);
  1763. }
  1764. /* Insert partial mapping after the range */
  1765. if (!list_empty(&after->list)) {
  1766. amdgpu_vm_it_insert(after, &vm->va);
  1767. if (after->flags & AMDGPU_PTE_PRT)
  1768. amdgpu_vm_prt_get(adev);
  1769. } else {
  1770. kfree(after);
  1771. }
  1772. return 0;
  1773. }
  1774. /**
  1775. * amdgpu_vm_bo_lookup_mapping - find mapping by address
  1776. *
  1777. * @vm: the requested VM
  1778. *
  1779. * Find a mapping by it's address.
  1780. */
  1781. struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
  1782. uint64_t addr)
  1783. {
  1784. return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
  1785. }
  1786. /**
  1787. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1788. *
  1789. * @adev: amdgpu_device pointer
  1790. * @bo_va: requested bo_va
  1791. *
  1792. * Remove @bo_va->bo from the requested vm.
  1793. *
  1794. * Object have to be reserved!
  1795. */
  1796. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1797. struct amdgpu_bo_va *bo_va)
  1798. {
  1799. struct amdgpu_bo_va_mapping *mapping, *next;
  1800. struct amdgpu_vm *vm = bo_va->base.vm;
  1801. list_del(&bo_va->base.bo_list);
  1802. spin_lock(&vm->status_lock);
  1803. list_del(&bo_va->base.vm_status);
  1804. spin_unlock(&vm->status_lock);
  1805. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1806. list_del(&mapping->list);
  1807. amdgpu_vm_it_remove(mapping, &vm->va);
  1808. mapping->bo_va = NULL;
  1809. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1810. list_add(&mapping->list, &vm->freed);
  1811. }
  1812. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1813. list_del(&mapping->list);
  1814. amdgpu_vm_it_remove(mapping, &vm->va);
  1815. amdgpu_vm_free_mapping(adev, vm, mapping,
  1816. bo_va->last_pt_update);
  1817. }
  1818. dma_fence_put(bo_va->last_pt_update);
  1819. kfree(bo_va);
  1820. }
  1821. /**
  1822. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1823. *
  1824. * @adev: amdgpu_device pointer
  1825. * @vm: requested vm
  1826. * @bo: amdgpu buffer object
  1827. *
  1828. * Mark @bo as invalid.
  1829. */
  1830. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1831. struct amdgpu_bo *bo, bool evicted)
  1832. {
  1833. struct amdgpu_vm_bo_base *bo_base;
  1834. list_for_each_entry(bo_base, &bo->va, bo_list) {
  1835. struct amdgpu_vm *vm = bo_base->vm;
  1836. bo_base->moved = true;
  1837. if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
  1838. spin_lock(&bo_base->vm->status_lock);
  1839. if (bo->tbo.type == ttm_bo_type_kernel)
  1840. list_move(&bo_base->vm_status, &vm->evicted);
  1841. else
  1842. list_move_tail(&bo_base->vm_status,
  1843. &vm->evicted);
  1844. spin_unlock(&bo_base->vm->status_lock);
  1845. continue;
  1846. }
  1847. if (bo->tbo.type == ttm_bo_type_kernel) {
  1848. spin_lock(&bo_base->vm->status_lock);
  1849. if (list_empty(&bo_base->vm_status))
  1850. list_add(&bo_base->vm_status, &vm->relocated);
  1851. spin_unlock(&bo_base->vm->status_lock);
  1852. continue;
  1853. }
  1854. spin_lock(&bo_base->vm->status_lock);
  1855. if (list_empty(&bo_base->vm_status))
  1856. list_add(&bo_base->vm_status, &vm->moved);
  1857. spin_unlock(&bo_base->vm->status_lock);
  1858. }
  1859. }
  1860. static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
  1861. {
  1862. /* Total bits covered by PD + PTs */
  1863. unsigned bits = ilog2(vm_size) + 18;
  1864. /* Make sure the PD is 4K in size up to 8GB address space.
  1865. Above that split equal between PD and PTs */
  1866. if (vm_size <= 8)
  1867. return (bits - 9);
  1868. else
  1869. return ((bits + 3) / 2);
  1870. }
  1871. /**
  1872. * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
  1873. *
  1874. * @adev: amdgpu_device pointer
  1875. * @vm_size: the default vm size if it's set auto
  1876. */
  1877. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
  1878. uint32_t fragment_size_default, unsigned max_level,
  1879. unsigned max_bits)
  1880. {
  1881. uint64_t tmp;
  1882. /* adjust vm size first */
  1883. if (amdgpu_vm_size != -1) {
  1884. unsigned max_size = 1 << (max_bits - 30);
  1885. vm_size = amdgpu_vm_size;
  1886. if (vm_size > max_size) {
  1887. dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
  1888. amdgpu_vm_size, max_size);
  1889. vm_size = max_size;
  1890. }
  1891. }
  1892. adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
  1893. tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
  1894. if (amdgpu_vm_block_size != -1)
  1895. tmp >>= amdgpu_vm_block_size - 9;
  1896. tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
  1897. adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
  1898. switch (adev->vm_manager.num_level) {
  1899. case 3:
  1900. adev->vm_manager.root_level = AMDGPU_VM_PDB2;
  1901. break;
  1902. case 2:
  1903. adev->vm_manager.root_level = AMDGPU_VM_PDB1;
  1904. break;
  1905. case 1:
  1906. adev->vm_manager.root_level = AMDGPU_VM_PDB0;
  1907. break;
  1908. default:
  1909. dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
  1910. }
  1911. /* block size depends on vm size and hw setup*/
  1912. if (amdgpu_vm_block_size != -1)
  1913. adev->vm_manager.block_size =
  1914. min((unsigned)amdgpu_vm_block_size, max_bits
  1915. - AMDGPU_GPU_PAGE_SHIFT
  1916. - 9 * adev->vm_manager.num_level);
  1917. else if (adev->vm_manager.num_level > 1)
  1918. adev->vm_manager.block_size = 9;
  1919. else
  1920. adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
  1921. if (amdgpu_vm_fragment_size == -1)
  1922. adev->vm_manager.fragment_size = fragment_size_default;
  1923. else
  1924. adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
  1925. DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
  1926. vm_size, adev->vm_manager.num_level + 1,
  1927. adev->vm_manager.block_size,
  1928. adev->vm_manager.fragment_size);
  1929. }
  1930. /**
  1931. * amdgpu_vm_init - initialize a vm instance
  1932. *
  1933. * @adev: amdgpu_device pointer
  1934. * @vm: requested vm
  1935. * @vm_context: Indicates if it GFX or Compute context
  1936. *
  1937. * Init @vm fields.
  1938. */
  1939. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  1940. int vm_context, unsigned int pasid)
  1941. {
  1942. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1943. AMDGPU_VM_PTE_COUNT(adev) * 8);
  1944. unsigned ring_instance;
  1945. struct amdgpu_ring *ring;
  1946. struct drm_sched_rq *rq;
  1947. int r, i;
  1948. u64 flags;
  1949. uint64_t init_pde_value = 0;
  1950. vm->va = RB_ROOT_CACHED;
  1951. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  1952. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  1953. vm->reserved_vmid[i] = NULL;
  1954. spin_lock_init(&vm->status_lock);
  1955. INIT_LIST_HEAD(&vm->evicted);
  1956. INIT_LIST_HEAD(&vm->relocated);
  1957. INIT_LIST_HEAD(&vm->moved);
  1958. INIT_LIST_HEAD(&vm->freed);
  1959. /* create scheduler entity for page table updates */
  1960. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  1961. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  1962. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  1963. rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
  1964. r = drm_sched_entity_init(&ring->sched, &vm->entity,
  1965. rq, amdgpu_sched_jobs, NULL);
  1966. if (r)
  1967. return r;
  1968. vm->pte_support_ats = false;
  1969. if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
  1970. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  1971. AMDGPU_VM_USE_CPU_FOR_COMPUTE);
  1972. if (adev->asic_type == CHIP_RAVEN) {
  1973. vm->pte_support_ats = true;
  1974. init_pde_value = AMDGPU_PTE_DEFAULT_ATC
  1975. | AMDGPU_PDE_PTE;
  1976. }
  1977. } else
  1978. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  1979. AMDGPU_VM_USE_CPU_FOR_GFX);
  1980. DRM_DEBUG_DRIVER("VM update mode is %s\n",
  1981. vm->use_cpu_for_update ? "CPU" : "SDMA");
  1982. WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
  1983. "CPU update of VM recommended only for large BAR system\n");
  1984. vm->last_update = NULL;
  1985. flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  1986. AMDGPU_GEM_CREATE_VRAM_CLEARED;
  1987. if (vm->use_cpu_for_update)
  1988. flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  1989. else
  1990. flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  1991. AMDGPU_GEM_CREATE_SHADOW);
  1992. r = amdgpu_bo_create(adev,
  1993. amdgpu_vm_bo_size(adev, adev->vm_manager.root_level),
  1994. align, true,
  1995. AMDGPU_GEM_DOMAIN_VRAM,
  1996. flags,
  1997. NULL, NULL, init_pde_value, &vm->root.base.bo);
  1998. if (r)
  1999. goto error_free_sched_entity;
  2000. vm->root.base.vm = vm;
  2001. list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
  2002. INIT_LIST_HEAD(&vm->root.base.vm_status);
  2003. if (vm->use_cpu_for_update) {
  2004. r = amdgpu_bo_reserve(vm->root.base.bo, false);
  2005. if (r)
  2006. goto error_free_root;
  2007. r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
  2008. amdgpu_bo_unreserve(vm->root.base.bo);
  2009. if (r)
  2010. goto error_free_root;
  2011. }
  2012. if (pasid) {
  2013. unsigned long flags;
  2014. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2015. r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
  2016. GFP_ATOMIC);
  2017. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2018. if (r < 0)
  2019. goto error_free_root;
  2020. vm->pasid = pasid;
  2021. }
  2022. INIT_KFIFO(vm->faults);
  2023. vm->fault_credit = 16;
  2024. return 0;
  2025. error_free_root:
  2026. amdgpu_bo_unref(&vm->root.base.bo->shadow);
  2027. amdgpu_bo_unref(&vm->root.base.bo);
  2028. vm->root.base.bo = NULL;
  2029. error_free_sched_entity:
  2030. drm_sched_entity_fini(&ring->sched, &vm->entity);
  2031. return r;
  2032. }
  2033. /**
  2034. * amdgpu_vm_free_levels - free PD/PT levels
  2035. *
  2036. * @adev: amdgpu device structure
  2037. * @parent: PD/PT starting level to free
  2038. * @level: level of parent structure
  2039. *
  2040. * Free the page directory or page table level and all sub levels.
  2041. */
  2042. static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
  2043. struct amdgpu_vm_pt *parent,
  2044. unsigned level)
  2045. {
  2046. unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
  2047. if (parent->base.bo) {
  2048. list_del(&parent->base.bo_list);
  2049. list_del(&parent->base.vm_status);
  2050. amdgpu_bo_unref(&parent->base.bo->shadow);
  2051. amdgpu_bo_unref(&parent->base.bo);
  2052. }
  2053. if (parent->entries)
  2054. for (i = 0; i < num_entries; i++)
  2055. amdgpu_vm_free_levels(adev, &parent->entries[i],
  2056. level + 1);
  2057. kvfree(parent->entries);
  2058. }
  2059. /**
  2060. * amdgpu_vm_fini - tear down a vm instance
  2061. *
  2062. * @adev: amdgpu_device pointer
  2063. * @vm: requested vm
  2064. *
  2065. * Tear down @vm.
  2066. * Unbind the VM and remove all bos from the vm bo list
  2067. */
  2068. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  2069. {
  2070. struct amdgpu_bo_va_mapping *mapping, *tmp;
  2071. bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
  2072. struct amdgpu_bo *root;
  2073. u64 fault;
  2074. int i, r;
  2075. /* Clear pending page faults from IH when the VM is destroyed */
  2076. while (kfifo_get(&vm->faults, &fault))
  2077. amdgpu_ih_clear_fault(adev, fault);
  2078. if (vm->pasid) {
  2079. unsigned long flags;
  2080. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2081. idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
  2082. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2083. }
  2084. drm_sched_entity_fini(vm->entity.sched, &vm->entity);
  2085. if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
  2086. dev_err(adev->dev, "still active bo inside vm\n");
  2087. }
  2088. rbtree_postorder_for_each_entry_safe(mapping, tmp,
  2089. &vm->va.rb_root, rb) {
  2090. list_del(&mapping->list);
  2091. amdgpu_vm_it_remove(mapping, &vm->va);
  2092. kfree(mapping);
  2093. }
  2094. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  2095. if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
  2096. amdgpu_vm_prt_fini(adev, vm);
  2097. prt_fini_needed = false;
  2098. }
  2099. list_del(&mapping->list);
  2100. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  2101. }
  2102. root = amdgpu_bo_ref(vm->root.base.bo);
  2103. r = amdgpu_bo_reserve(root, true);
  2104. if (r) {
  2105. dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
  2106. } else {
  2107. amdgpu_vm_free_levels(adev, &vm->root,
  2108. adev->vm_manager.root_level);
  2109. amdgpu_bo_unreserve(root);
  2110. }
  2111. amdgpu_bo_unref(&root);
  2112. dma_fence_put(vm->last_update);
  2113. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2114. amdgpu_vmid_free_reserved(adev, vm, i);
  2115. }
  2116. /**
  2117. * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
  2118. *
  2119. * @adev: amdgpu_device pointer
  2120. * @pasid: PASID do identify the VM
  2121. *
  2122. * This function is expected to be called in interrupt context. Returns
  2123. * true if there was fault credit, false otherwise
  2124. */
  2125. bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
  2126. unsigned int pasid)
  2127. {
  2128. struct amdgpu_vm *vm;
  2129. spin_lock(&adev->vm_manager.pasid_lock);
  2130. vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
  2131. spin_unlock(&adev->vm_manager.pasid_lock);
  2132. if (!vm)
  2133. /* VM not found, can't track fault credit */
  2134. return true;
  2135. /* No lock needed. only accessed by IRQ handler */
  2136. if (!vm->fault_credit)
  2137. /* Too many faults in this VM */
  2138. return false;
  2139. vm->fault_credit--;
  2140. return true;
  2141. }
  2142. /**
  2143. * amdgpu_vm_manager_init - init the VM manager
  2144. *
  2145. * @adev: amdgpu_device pointer
  2146. *
  2147. * Initialize the VM manager structures
  2148. */
  2149. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  2150. {
  2151. unsigned i;
  2152. amdgpu_vmid_mgr_init(adev);
  2153. adev->vm_manager.fence_context =
  2154. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2155. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  2156. adev->vm_manager.seqno[i] = 0;
  2157. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  2158. atomic64_set(&adev->vm_manager.client_counter, 0);
  2159. spin_lock_init(&adev->vm_manager.prt_lock);
  2160. atomic_set(&adev->vm_manager.num_prt_users, 0);
  2161. /* If not overridden by the user, by default, only in large BAR systems
  2162. * Compute VM tables will be updated by CPU
  2163. */
  2164. #ifdef CONFIG_X86_64
  2165. if (amdgpu_vm_update_mode == -1) {
  2166. if (amdgpu_vm_is_large_bar(adev))
  2167. adev->vm_manager.vm_update_mode =
  2168. AMDGPU_VM_USE_CPU_FOR_COMPUTE;
  2169. else
  2170. adev->vm_manager.vm_update_mode = 0;
  2171. } else
  2172. adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
  2173. #else
  2174. adev->vm_manager.vm_update_mode = 0;
  2175. #endif
  2176. idr_init(&adev->vm_manager.pasid_idr);
  2177. spin_lock_init(&adev->vm_manager.pasid_lock);
  2178. }
  2179. /**
  2180. * amdgpu_vm_manager_fini - cleanup VM manager
  2181. *
  2182. * @adev: amdgpu_device pointer
  2183. *
  2184. * Cleanup the VM manager and free resources.
  2185. */
  2186. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  2187. {
  2188. WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
  2189. idr_destroy(&adev->vm_manager.pasid_idr);
  2190. amdgpu_vmid_mgr_fini(adev);
  2191. }
  2192. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  2193. {
  2194. union drm_amdgpu_vm *args = data;
  2195. struct amdgpu_device *adev = dev->dev_private;
  2196. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  2197. int r;
  2198. switch (args->in.op) {
  2199. case AMDGPU_VM_OP_RESERVE_VMID:
  2200. /* current, we only have requirement to reserve vmid from gfxhub */
  2201. r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2202. if (r)
  2203. return r;
  2204. break;
  2205. case AMDGPU_VM_OP_UNRESERVE_VMID:
  2206. amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2207. break;
  2208. default:
  2209. return -EINVAL;
  2210. }
  2211. return 0;
  2212. }