amdgpu_vm.c 77 KB

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