amdgpu_vm.c 63 KB

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