amdgpu_vm.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953
  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. unsigned pt_idx, ndw = 0;
  947. struct amdgpu_job *job;
  948. struct amdgpu_pte_update_params params;
  949. struct dma_fence *fence = NULL;
  950. uint32_t incr;
  951. int r;
  952. if (!parent->entries)
  953. return 0;
  954. memset(&params, 0, sizeof(params));
  955. params.adev = adev;
  956. shadow = parent->base.bo->shadow;
  957. if (vm->use_cpu_for_update) {
  958. pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
  959. r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
  960. if (unlikely(r))
  961. return r;
  962. params.func = amdgpu_vm_cpu_set_ptes;
  963. } else {
  964. ring = container_of(vm->entity.sched, struct amdgpu_ring,
  965. sched);
  966. /* padding, etc. */
  967. ndw = 64;
  968. /* assume the worst case */
  969. ndw += parent->last_entry_used * 6;
  970. pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
  971. if (shadow) {
  972. shadow_addr = amdgpu_bo_gpu_offset(shadow);
  973. ndw *= 2;
  974. } else {
  975. shadow_addr = 0;
  976. }
  977. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  978. if (r)
  979. return r;
  980. params.ib = &job->ibs[0];
  981. params.func = amdgpu_vm_do_set_ptes;
  982. }
  983. /* walk over the address space and update the directory */
  984. for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
  985. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  986. struct amdgpu_bo *bo = entry->base.bo;
  987. uint64_t pde, pt;
  988. if (bo == NULL)
  989. continue;
  990. spin_lock(&vm->status_lock);
  991. list_del_init(&entry->base.vm_status);
  992. spin_unlock(&vm->status_lock);
  993. pt = amdgpu_bo_gpu_offset(bo);
  994. pt = amdgpu_gart_get_vm_pde(adev, pt);
  995. /* Don't update huge pages here */
  996. if ((parent->entries[pt_idx].addr & AMDGPU_PDE_PTE) ||
  997. parent->entries[pt_idx].addr == (pt | AMDGPU_PTE_VALID))
  998. continue;
  999. parent->entries[pt_idx].addr = pt | AMDGPU_PTE_VALID;
  1000. incr = amdgpu_bo_size(bo);
  1001. if (shadow) {
  1002. pde = shadow_addr + pt_idx * 8;
  1003. params.func(&params, pde, pt, 1, incr,
  1004. AMDGPU_PTE_VALID);
  1005. }
  1006. pde = pd_addr + pt_idx * 8;
  1007. params.func(&params, pde, pt, 1, incr, AMDGPU_PTE_VALID);
  1008. }
  1009. if (!vm->use_cpu_for_update) {
  1010. if (params.ib->length_dw == 0) {
  1011. amdgpu_job_free(job);
  1012. } else {
  1013. amdgpu_ring_pad_ib(ring, params.ib);
  1014. amdgpu_sync_resv(adev, &job->sync,
  1015. parent->base.bo->tbo.resv,
  1016. AMDGPU_FENCE_OWNER_VM, false);
  1017. if (shadow)
  1018. amdgpu_sync_resv(adev, &job->sync,
  1019. shadow->tbo.resv,
  1020. AMDGPU_FENCE_OWNER_VM, false);
  1021. WARN_ON(params.ib->length_dw > ndw);
  1022. r = amdgpu_job_submit(job, ring, &vm->entity,
  1023. AMDGPU_FENCE_OWNER_VM, &fence);
  1024. if (r)
  1025. goto error_free;
  1026. amdgpu_bo_fence(parent->base.bo, fence, true);
  1027. dma_fence_put(vm->last_update);
  1028. vm->last_update = fence;
  1029. }
  1030. }
  1031. return 0;
  1032. error_free:
  1033. amdgpu_job_free(job);
  1034. return r;
  1035. }
  1036. /*
  1037. * amdgpu_vm_invalidate_level - mark all PD levels as invalid
  1038. *
  1039. * @parent: parent PD
  1040. *
  1041. * Mark all PD level as invalid after an error.
  1042. */
  1043. static void amdgpu_vm_invalidate_level(struct amdgpu_vm *vm,
  1044. struct amdgpu_vm_pt *parent)
  1045. {
  1046. unsigned pt_idx;
  1047. /*
  1048. * Recurse into the subdirectories. This recursion is harmless because
  1049. * we only have a maximum of 5 layers.
  1050. */
  1051. for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
  1052. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  1053. if (!entry->base.bo)
  1054. continue;
  1055. entry->addr = ~0ULL;
  1056. spin_lock(&vm->status_lock);
  1057. if (list_empty(&entry->base.vm_status))
  1058. list_add(&entry->base.vm_status, &vm->relocated);
  1059. spin_unlock(&vm->status_lock);
  1060. amdgpu_vm_invalidate_level(vm, entry);
  1061. }
  1062. }
  1063. /*
  1064. * amdgpu_vm_update_directories - make sure that all directories are valid
  1065. *
  1066. * @adev: amdgpu_device pointer
  1067. * @vm: requested vm
  1068. *
  1069. * Makes sure all directories are up to date.
  1070. * Returns 0 for success, error for failure.
  1071. */
  1072. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  1073. struct amdgpu_vm *vm)
  1074. {
  1075. int r = 0;
  1076. spin_lock(&vm->status_lock);
  1077. while (!list_empty(&vm->relocated)) {
  1078. struct amdgpu_vm_bo_base *bo_base;
  1079. struct amdgpu_bo *bo;
  1080. bo_base = list_first_entry(&vm->relocated,
  1081. struct amdgpu_vm_bo_base,
  1082. vm_status);
  1083. spin_unlock(&vm->status_lock);
  1084. bo = bo_base->bo->parent;
  1085. if (bo) {
  1086. struct amdgpu_vm_bo_base *parent;
  1087. struct amdgpu_vm_pt *pt;
  1088. parent = list_first_entry(&bo->va,
  1089. struct amdgpu_vm_bo_base,
  1090. bo_list);
  1091. pt = container_of(parent, struct amdgpu_vm_pt, base);
  1092. r = amdgpu_vm_update_level(adev, vm, pt);
  1093. if (r) {
  1094. amdgpu_vm_invalidate_level(vm, &vm->root);
  1095. return r;
  1096. }
  1097. spin_lock(&vm->status_lock);
  1098. } else {
  1099. spin_lock(&vm->status_lock);
  1100. list_del_init(&bo_base->vm_status);
  1101. }
  1102. }
  1103. spin_unlock(&vm->status_lock);
  1104. if (vm->use_cpu_for_update) {
  1105. /* Flush HDP */
  1106. mb();
  1107. amdgpu_gart_flush_gpu_tlb(adev, 0);
  1108. }
  1109. return r;
  1110. }
  1111. /**
  1112. * amdgpu_vm_find_entry - find the entry for an address
  1113. *
  1114. * @p: see amdgpu_pte_update_params definition
  1115. * @addr: virtual address in question
  1116. * @entry: resulting entry or NULL
  1117. * @parent: parent entry
  1118. *
  1119. * Find the vm_pt entry and it's parent for the given address.
  1120. */
  1121. void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
  1122. struct amdgpu_vm_pt **entry,
  1123. struct amdgpu_vm_pt **parent)
  1124. {
  1125. unsigned level = 0;
  1126. *parent = NULL;
  1127. *entry = &p->vm->root;
  1128. while ((*entry)->entries) {
  1129. unsigned idx = addr >> amdgpu_vm_level_shift(p->adev, level++);
  1130. idx %= amdgpu_bo_size((*entry)->base.bo) / 8;
  1131. *parent = *entry;
  1132. *entry = &(*entry)->entries[idx];
  1133. }
  1134. if (level != p->adev->vm_manager.num_level)
  1135. *entry = NULL;
  1136. }
  1137. /**
  1138. * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
  1139. *
  1140. * @p: see amdgpu_pte_update_params definition
  1141. * @entry: vm_pt entry to check
  1142. * @parent: parent entry
  1143. * @nptes: number of PTEs updated with this operation
  1144. * @dst: destination address where the PTEs should point to
  1145. * @flags: access flags fro the PTEs
  1146. *
  1147. * Check if we can update the PD with a huge page.
  1148. */
  1149. static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
  1150. struct amdgpu_vm_pt *entry,
  1151. struct amdgpu_vm_pt *parent,
  1152. unsigned nptes, uint64_t dst,
  1153. uint64_t flags)
  1154. {
  1155. bool use_cpu_update = (p->func == amdgpu_vm_cpu_set_ptes);
  1156. uint64_t pd_addr, pde;
  1157. /* In the case of a mixed PT the PDE must point to it*/
  1158. if (p->adev->asic_type < CHIP_VEGA10 ||
  1159. nptes != AMDGPU_VM_PTE_COUNT(p->adev) ||
  1160. p->src ||
  1161. !(flags & AMDGPU_PTE_VALID)) {
  1162. dst = amdgpu_bo_gpu_offset(entry->base.bo);
  1163. dst = amdgpu_gart_get_vm_pde(p->adev, dst);
  1164. flags = AMDGPU_PTE_VALID;
  1165. } else {
  1166. /* Set the huge page flag to stop scanning at this PDE */
  1167. flags |= AMDGPU_PDE_PTE;
  1168. }
  1169. if (entry->addr == (dst | flags))
  1170. return;
  1171. entry->addr = (dst | flags);
  1172. if (use_cpu_update) {
  1173. /* In case a huge page is replaced with a system
  1174. * memory mapping, p->pages_addr != NULL and
  1175. * amdgpu_vm_cpu_set_ptes would try to translate dst
  1176. * through amdgpu_vm_map_gart. But dst is already a
  1177. * GPU address (of the page table). Disable
  1178. * amdgpu_vm_map_gart temporarily.
  1179. */
  1180. dma_addr_t *tmp;
  1181. tmp = p->pages_addr;
  1182. p->pages_addr = NULL;
  1183. pd_addr = (unsigned long)amdgpu_bo_kptr(parent->base.bo);
  1184. pde = pd_addr + (entry - parent->entries) * 8;
  1185. amdgpu_vm_cpu_set_ptes(p, pde, dst, 1, 0, flags);
  1186. p->pages_addr = tmp;
  1187. } else {
  1188. if (parent->base.bo->shadow) {
  1189. pd_addr = amdgpu_bo_gpu_offset(parent->base.bo->shadow);
  1190. pde = pd_addr + (entry - parent->entries) * 8;
  1191. amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
  1192. }
  1193. pd_addr = amdgpu_bo_gpu_offset(parent->base.bo);
  1194. pde = pd_addr + (entry - parent->entries) * 8;
  1195. amdgpu_vm_do_set_ptes(p, pde, dst, 1, 0, flags);
  1196. }
  1197. }
  1198. /**
  1199. * amdgpu_vm_update_ptes - make sure that page tables are valid
  1200. *
  1201. * @params: see amdgpu_pte_update_params definition
  1202. * @vm: requested vm
  1203. * @start: start of GPU address range
  1204. * @end: end of GPU address range
  1205. * @dst: destination address to map to, the next dst inside the function
  1206. * @flags: mapping flags
  1207. *
  1208. * Update the page tables in the range @start - @end.
  1209. * Returns 0 for success, -EINVAL for failure.
  1210. */
  1211. static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  1212. uint64_t start, uint64_t end,
  1213. uint64_t dst, uint64_t flags)
  1214. {
  1215. struct amdgpu_device *adev = params->adev;
  1216. const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
  1217. uint64_t addr, pe_start;
  1218. struct amdgpu_bo *pt;
  1219. unsigned nptes;
  1220. bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
  1221. /* walk over the address space and update the page tables */
  1222. for (addr = start; addr < end; addr += nptes,
  1223. dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
  1224. struct amdgpu_vm_pt *entry, *parent;
  1225. amdgpu_vm_get_entry(params, addr, &entry, &parent);
  1226. if (!entry)
  1227. return -ENOENT;
  1228. if ((addr & ~mask) == (end & ~mask))
  1229. nptes = end - addr;
  1230. else
  1231. nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
  1232. amdgpu_vm_handle_huge_pages(params, entry, parent,
  1233. nptes, dst, flags);
  1234. /* We don't need to update PTEs for huge pages */
  1235. if (entry->addr & AMDGPU_PDE_PTE)
  1236. continue;
  1237. pt = entry->base.bo;
  1238. if (use_cpu_update) {
  1239. pe_start = (unsigned long)amdgpu_bo_kptr(pt);
  1240. } else {
  1241. if (pt->shadow) {
  1242. pe_start = amdgpu_bo_gpu_offset(pt->shadow);
  1243. pe_start += (addr & mask) * 8;
  1244. params->func(params, pe_start, dst, nptes,
  1245. AMDGPU_GPU_PAGE_SIZE, flags);
  1246. }
  1247. pe_start = amdgpu_bo_gpu_offset(pt);
  1248. }
  1249. pe_start += (addr & mask) * 8;
  1250. params->func(params, pe_start, dst, nptes,
  1251. AMDGPU_GPU_PAGE_SIZE, flags);
  1252. }
  1253. return 0;
  1254. }
  1255. /*
  1256. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  1257. *
  1258. * @params: see amdgpu_pte_update_params definition
  1259. * @vm: requested vm
  1260. * @start: first PTE to handle
  1261. * @end: last PTE to handle
  1262. * @dst: addr those PTEs should point to
  1263. * @flags: hw mapping flags
  1264. * Returns 0 for success, -EINVAL for failure.
  1265. */
  1266. static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  1267. uint64_t start, uint64_t end,
  1268. uint64_t dst, uint64_t flags)
  1269. {
  1270. /**
  1271. * The MC L1 TLB supports variable sized pages, based on a fragment
  1272. * field in the PTE. When this field is set to a non-zero value, page
  1273. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  1274. * flags are considered valid for all PTEs within the fragment range
  1275. * and corresponding mappings are assumed to be physically contiguous.
  1276. *
  1277. * The L1 TLB can store a single PTE for the whole fragment,
  1278. * significantly increasing the space available for translation
  1279. * caching. This leads to large improvements in throughput when the
  1280. * TLB is under pressure.
  1281. *
  1282. * The L2 TLB distributes small and large fragments into two
  1283. * asymmetric partitions. The large fragment cache is significantly
  1284. * larger. Thus, we try to use large fragments wherever possible.
  1285. * Userspace can support this by aligning virtual base address and
  1286. * allocation size to the fragment size.
  1287. */
  1288. unsigned max_frag = params->adev->vm_manager.fragment_size;
  1289. int r;
  1290. /* system pages are non continuously */
  1291. if (params->src || !(flags & AMDGPU_PTE_VALID))
  1292. return amdgpu_vm_update_ptes(params, start, end, dst, flags);
  1293. while (start != end) {
  1294. uint64_t frag_flags, frag_end;
  1295. unsigned frag;
  1296. /* This intentionally wraps around if no bit is set */
  1297. frag = min((unsigned)ffs(start) - 1,
  1298. (unsigned)fls64(end - start) - 1);
  1299. if (frag >= max_frag) {
  1300. frag_flags = AMDGPU_PTE_FRAG(max_frag);
  1301. frag_end = end & ~((1ULL << max_frag) - 1);
  1302. } else {
  1303. frag_flags = AMDGPU_PTE_FRAG(frag);
  1304. frag_end = start + (1 << frag);
  1305. }
  1306. r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
  1307. flags | frag_flags);
  1308. if (r)
  1309. return r;
  1310. dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
  1311. start = frag_end;
  1312. }
  1313. return 0;
  1314. }
  1315. /**
  1316. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  1317. *
  1318. * @adev: amdgpu_device pointer
  1319. * @exclusive: fence we need to sync to
  1320. * @pages_addr: DMA addresses to use for mapping
  1321. * @vm: requested vm
  1322. * @start: start of mapped range
  1323. * @last: last mapped entry
  1324. * @flags: flags for the entries
  1325. * @addr: addr to set the area to
  1326. * @fence: optional resulting fence
  1327. *
  1328. * Fill in the page table entries between @start and @last.
  1329. * Returns 0 for success, -EINVAL for failure.
  1330. */
  1331. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  1332. struct dma_fence *exclusive,
  1333. dma_addr_t *pages_addr,
  1334. struct amdgpu_vm *vm,
  1335. uint64_t start, uint64_t last,
  1336. uint64_t flags, uint64_t addr,
  1337. struct dma_fence **fence)
  1338. {
  1339. struct amdgpu_ring *ring;
  1340. void *owner = AMDGPU_FENCE_OWNER_VM;
  1341. unsigned nptes, ncmds, ndw;
  1342. struct amdgpu_job *job;
  1343. struct amdgpu_pte_update_params params;
  1344. struct dma_fence *f = NULL;
  1345. int r;
  1346. memset(&params, 0, sizeof(params));
  1347. params.adev = adev;
  1348. params.vm = vm;
  1349. /* sync to everything on unmapping */
  1350. if (!(flags & AMDGPU_PTE_VALID))
  1351. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  1352. if (vm->use_cpu_for_update) {
  1353. /* params.src is used as flag to indicate system Memory */
  1354. if (pages_addr)
  1355. params.src = ~0;
  1356. /* Wait for PT BOs to be free. PTs share the same resv. object
  1357. * as the root PD BO
  1358. */
  1359. r = amdgpu_vm_wait_pd(adev, vm, owner);
  1360. if (unlikely(r))
  1361. return r;
  1362. params.func = amdgpu_vm_cpu_set_ptes;
  1363. params.pages_addr = pages_addr;
  1364. return amdgpu_vm_frag_ptes(&params, start, last + 1,
  1365. addr, flags);
  1366. }
  1367. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  1368. nptes = last - start + 1;
  1369. /*
  1370. * reserve space for two commands every (1 << BLOCK_SIZE)
  1371. * entries or 2k dwords (whatever is smaller)
  1372. *
  1373. * The second command is for the shadow pagetables.
  1374. */
  1375. ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
  1376. /* padding, etc. */
  1377. ndw = 64;
  1378. /* one PDE write for each huge page */
  1379. ndw += ((nptes >> adev->vm_manager.block_size) + 1) * 6;
  1380. if (pages_addr) {
  1381. /* copy commands needed */
  1382. ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
  1383. /* and also PTEs */
  1384. ndw += nptes * 2;
  1385. params.func = amdgpu_vm_do_copy_ptes;
  1386. } else {
  1387. /* set page commands needed */
  1388. ndw += ncmds * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
  1389. /* extra commands for begin/end fragments */
  1390. ndw += 2 * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw
  1391. * adev->vm_manager.fragment_size;
  1392. params.func = amdgpu_vm_do_set_ptes;
  1393. }
  1394. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  1395. if (r)
  1396. return r;
  1397. params.ib = &job->ibs[0];
  1398. if (pages_addr) {
  1399. uint64_t *pte;
  1400. unsigned i;
  1401. /* Put the PTEs at the end of the IB. */
  1402. i = ndw - nptes * 2;
  1403. pte= (uint64_t *)&(job->ibs->ptr[i]);
  1404. params.src = job->ibs->gpu_addr + i * 4;
  1405. for (i = 0; i < nptes; ++i) {
  1406. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  1407. AMDGPU_GPU_PAGE_SIZE);
  1408. pte[i] |= flags;
  1409. }
  1410. addr = 0;
  1411. }
  1412. r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
  1413. if (r)
  1414. goto error_free;
  1415. r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
  1416. owner, false);
  1417. if (r)
  1418. goto error_free;
  1419. r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
  1420. if (r)
  1421. goto error_free;
  1422. r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1423. if (r)
  1424. goto error_free;
  1425. amdgpu_ring_pad_ib(ring, params.ib);
  1426. WARN_ON(params.ib->length_dw > ndw);
  1427. r = amdgpu_job_submit(job, ring, &vm->entity,
  1428. AMDGPU_FENCE_OWNER_VM, &f);
  1429. if (r)
  1430. goto error_free;
  1431. amdgpu_bo_fence(vm->root.base.bo, f, true);
  1432. dma_fence_put(*fence);
  1433. *fence = f;
  1434. return 0;
  1435. error_free:
  1436. amdgpu_job_free(job);
  1437. amdgpu_vm_invalidate_level(vm, &vm->root);
  1438. return r;
  1439. }
  1440. /**
  1441. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  1442. *
  1443. * @adev: amdgpu_device pointer
  1444. * @exclusive: fence we need to sync to
  1445. * @pages_addr: DMA addresses to use for mapping
  1446. * @vm: requested vm
  1447. * @mapping: mapped range and flags to use for the update
  1448. * @flags: HW flags for the mapping
  1449. * @nodes: array of drm_mm_nodes with the MC addresses
  1450. * @fence: optional resulting fence
  1451. *
  1452. * Split the mapping into smaller chunks so that each update fits
  1453. * into a SDMA IB.
  1454. * Returns 0 for success, -EINVAL for failure.
  1455. */
  1456. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  1457. struct dma_fence *exclusive,
  1458. dma_addr_t *pages_addr,
  1459. struct amdgpu_vm *vm,
  1460. struct amdgpu_bo_va_mapping *mapping,
  1461. uint64_t flags,
  1462. struct drm_mm_node *nodes,
  1463. struct dma_fence **fence)
  1464. {
  1465. unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
  1466. uint64_t pfn, start = mapping->start;
  1467. int r;
  1468. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  1469. * but in case of something, we filter the flags in first place
  1470. */
  1471. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  1472. flags &= ~AMDGPU_PTE_READABLE;
  1473. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  1474. flags &= ~AMDGPU_PTE_WRITEABLE;
  1475. flags &= ~AMDGPU_PTE_EXECUTABLE;
  1476. flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
  1477. flags &= ~AMDGPU_PTE_MTYPE_MASK;
  1478. flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
  1479. if ((mapping->flags & AMDGPU_PTE_PRT) &&
  1480. (adev->asic_type >= CHIP_VEGA10)) {
  1481. flags |= AMDGPU_PTE_PRT;
  1482. flags &= ~AMDGPU_PTE_VALID;
  1483. }
  1484. trace_amdgpu_vm_bo_update(mapping);
  1485. pfn = mapping->offset >> PAGE_SHIFT;
  1486. if (nodes) {
  1487. while (pfn >= nodes->size) {
  1488. pfn -= nodes->size;
  1489. ++nodes;
  1490. }
  1491. }
  1492. do {
  1493. dma_addr_t *dma_addr = NULL;
  1494. uint64_t max_entries;
  1495. uint64_t addr, last;
  1496. if (nodes) {
  1497. addr = nodes->start << PAGE_SHIFT;
  1498. max_entries = (nodes->size - pfn) *
  1499. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  1500. } else {
  1501. addr = 0;
  1502. max_entries = S64_MAX;
  1503. }
  1504. if (pages_addr) {
  1505. uint64_t count;
  1506. max_entries = min(max_entries, 16ull * 1024ull);
  1507. for (count = 1; count < max_entries; ++count) {
  1508. uint64_t idx = pfn + count;
  1509. if (pages_addr[idx] !=
  1510. (pages_addr[idx - 1] + PAGE_SIZE))
  1511. break;
  1512. }
  1513. if (count < min_linear_pages) {
  1514. addr = pfn << PAGE_SHIFT;
  1515. dma_addr = pages_addr;
  1516. } else {
  1517. addr = pages_addr[pfn];
  1518. max_entries = count;
  1519. }
  1520. } else if (flags & AMDGPU_PTE_VALID) {
  1521. addr += adev->vm_manager.vram_base_offset;
  1522. addr += pfn << PAGE_SHIFT;
  1523. }
  1524. last = min((uint64_t)mapping->last, start + max_entries - 1);
  1525. r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
  1526. start, last, flags, addr,
  1527. fence);
  1528. if (r)
  1529. return r;
  1530. pfn += last - start + 1;
  1531. if (nodes && nodes->size == pfn) {
  1532. pfn = 0;
  1533. ++nodes;
  1534. }
  1535. start = last + 1;
  1536. } while (unlikely(start != mapping->last + 1));
  1537. return 0;
  1538. }
  1539. /**
  1540. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  1541. *
  1542. * @adev: amdgpu_device pointer
  1543. * @bo_va: requested BO and VM object
  1544. * @clear: if true clear the entries
  1545. *
  1546. * Fill in the page table entries for @bo_va.
  1547. * Returns 0 for success, -EINVAL for failure.
  1548. */
  1549. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  1550. struct amdgpu_bo_va *bo_va,
  1551. bool clear)
  1552. {
  1553. struct amdgpu_bo *bo = bo_va->base.bo;
  1554. struct amdgpu_vm *vm = bo_va->base.vm;
  1555. struct amdgpu_bo_va_mapping *mapping;
  1556. dma_addr_t *pages_addr = NULL;
  1557. struct ttm_mem_reg *mem;
  1558. struct drm_mm_node *nodes;
  1559. struct dma_fence *exclusive, **last_update;
  1560. uint64_t flags;
  1561. int r;
  1562. if (clear || !bo_va->base.bo) {
  1563. mem = NULL;
  1564. nodes = NULL;
  1565. exclusive = NULL;
  1566. } else {
  1567. struct ttm_dma_tt *ttm;
  1568. mem = &bo_va->base.bo->tbo.mem;
  1569. nodes = mem->mm_node;
  1570. if (mem->mem_type == TTM_PL_TT) {
  1571. ttm = container_of(bo_va->base.bo->tbo.ttm,
  1572. struct ttm_dma_tt, ttm);
  1573. pages_addr = ttm->dma_address;
  1574. }
  1575. exclusive = reservation_object_get_excl(bo->tbo.resv);
  1576. }
  1577. if (bo)
  1578. flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
  1579. else
  1580. flags = 0x0;
  1581. if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
  1582. last_update = &vm->last_update;
  1583. else
  1584. last_update = &bo_va->last_pt_update;
  1585. if (!clear && bo_va->base.moved) {
  1586. bo_va->base.moved = false;
  1587. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1588. } else if (bo_va->cleared != clear) {
  1589. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1590. }
  1591. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1592. r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
  1593. mapping, flags, nodes,
  1594. last_update);
  1595. if (r)
  1596. return r;
  1597. }
  1598. if (vm->use_cpu_for_update) {
  1599. /* Flush HDP */
  1600. mb();
  1601. amdgpu_gart_flush_gpu_tlb(adev, 0);
  1602. }
  1603. spin_lock(&vm->status_lock);
  1604. list_del_init(&bo_va->base.vm_status);
  1605. spin_unlock(&vm->status_lock);
  1606. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1607. bo_va->cleared = clear;
  1608. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1609. list_for_each_entry(mapping, &bo_va->valids, list)
  1610. trace_amdgpu_vm_bo_mapping(mapping);
  1611. }
  1612. return 0;
  1613. }
  1614. /**
  1615. * amdgpu_vm_update_prt_state - update the global PRT state
  1616. */
  1617. static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
  1618. {
  1619. unsigned long flags;
  1620. bool enable;
  1621. spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
  1622. enable = !!atomic_read(&adev->vm_manager.num_prt_users);
  1623. adev->gart.gart_funcs->set_prt(adev, enable);
  1624. spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
  1625. }
  1626. /**
  1627. * amdgpu_vm_prt_get - add a PRT user
  1628. */
  1629. static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
  1630. {
  1631. if (!adev->gart.gart_funcs->set_prt)
  1632. return;
  1633. if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
  1634. amdgpu_vm_update_prt_state(adev);
  1635. }
  1636. /**
  1637. * amdgpu_vm_prt_put - drop a PRT user
  1638. */
  1639. static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
  1640. {
  1641. if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
  1642. amdgpu_vm_update_prt_state(adev);
  1643. }
  1644. /**
  1645. * amdgpu_vm_prt_cb - callback for updating the PRT status
  1646. */
  1647. static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
  1648. {
  1649. struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
  1650. amdgpu_vm_prt_put(cb->adev);
  1651. kfree(cb);
  1652. }
  1653. /**
  1654. * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
  1655. */
  1656. static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
  1657. struct dma_fence *fence)
  1658. {
  1659. struct amdgpu_prt_cb *cb;
  1660. if (!adev->gart.gart_funcs->set_prt)
  1661. return;
  1662. cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
  1663. if (!cb) {
  1664. /* Last resort when we are OOM */
  1665. if (fence)
  1666. dma_fence_wait(fence, false);
  1667. amdgpu_vm_prt_put(adev);
  1668. } else {
  1669. cb->adev = adev;
  1670. if (!fence || dma_fence_add_callback(fence, &cb->cb,
  1671. amdgpu_vm_prt_cb))
  1672. amdgpu_vm_prt_cb(fence, &cb->cb);
  1673. }
  1674. }
  1675. /**
  1676. * amdgpu_vm_free_mapping - free a mapping
  1677. *
  1678. * @adev: amdgpu_device pointer
  1679. * @vm: requested vm
  1680. * @mapping: mapping to be freed
  1681. * @fence: fence of the unmap operation
  1682. *
  1683. * Free a mapping and make sure we decrease the PRT usage count if applicable.
  1684. */
  1685. static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
  1686. struct amdgpu_vm *vm,
  1687. struct amdgpu_bo_va_mapping *mapping,
  1688. struct dma_fence *fence)
  1689. {
  1690. if (mapping->flags & AMDGPU_PTE_PRT)
  1691. amdgpu_vm_add_prt_cb(adev, fence);
  1692. kfree(mapping);
  1693. }
  1694. /**
  1695. * amdgpu_vm_prt_fini - finish all prt mappings
  1696. *
  1697. * @adev: amdgpu_device pointer
  1698. * @vm: requested vm
  1699. *
  1700. * Register a cleanup callback to disable PRT support after VM dies.
  1701. */
  1702. static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1703. {
  1704. struct reservation_object *resv = vm->root.base.bo->tbo.resv;
  1705. struct dma_fence *excl, **shared;
  1706. unsigned i, shared_count;
  1707. int r;
  1708. r = reservation_object_get_fences_rcu(resv, &excl,
  1709. &shared_count, &shared);
  1710. if (r) {
  1711. /* Not enough memory to grab the fence list, as last resort
  1712. * block for all the fences to complete.
  1713. */
  1714. reservation_object_wait_timeout_rcu(resv, true, false,
  1715. MAX_SCHEDULE_TIMEOUT);
  1716. return;
  1717. }
  1718. /* Add a callback for each fence in the reservation object */
  1719. amdgpu_vm_prt_get(adev);
  1720. amdgpu_vm_add_prt_cb(adev, excl);
  1721. for (i = 0; i < shared_count; ++i) {
  1722. amdgpu_vm_prt_get(adev);
  1723. amdgpu_vm_add_prt_cb(adev, shared[i]);
  1724. }
  1725. kfree(shared);
  1726. }
  1727. /**
  1728. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1729. *
  1730. * @adev: amdgpu_device pointer
  1731. * @vm: requested vm
  1732. * @fence: optional resulting fence (unchanged if no work needed to be done
  1733. * or if an error occurred)
  1734. *
  1735. * Make sure all freed BOs are cleared in the PT.
  1736. * Returns 0 for success.
  1737. *
  1738. * PTs have to be reserved and mutex must be locked!
  1739. */
  1740. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1741. struct amdgpu_vm *vm,
  1742. struct dma_fence **fence)
  1743. {
  1744. struct amdgpu_bo_va_mapping *mapping;
  1745. struct dma_fence *f = NULL;
  1746. int r;
  1747. uint64_t init_pte_value = 0;
  1748. while (!list_empty(&vm->freed)) {
  1749. mapping = list_first_entry(&vm->freed,
  1750. struct amdgpu_bo_va_mapping, list);
  1751. list_del(&mapping->list);
  1752. if (vm->pte_support_ats)
  1753. init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
  1754. r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
  1755. mapping->start, mapping->last,
  1756. init_pte_value, 0, &f);
  1757. amdgpu_vm_free_mapping(adev, vm, mapping, f);
  1758. if (r) {
  1759. dma_fence_put(f);
  1760. return r;
  1761. }
  1762. }
  1763. if (fence && f) {
  1764. dma_fence_put(*fence);
  1765. *fence = f;
  1766. } else {
  1767. dma_fence_put(f);
  1768. }
  1769. return 0;
  1770. }
  1771. /**
  1772. * amdgpu_vm_handle_moved - handle moved BOs in the PT
  1773. *
  1774. * @adev: amdgpu_device pointer
  1775. * @vm: requested vm
  1776. * @sync: sync object to add fences to
  1777. *
  1778. * Make sure all BOs which are moved are updated in the PTs.
  1779. * Returns 0 for success.
  1780. *
  1781. * PTs have to be reserved!
  1782. */
  1783. int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
  1784. struct amdgpu_vm *vm)
  1785. {
  1786. bool clear;
  1787. int r = 0;
  1788. spin_lock(&vm->status_lock);
  1789. while (!list_empty(&vm->moved)) {
  1790. struct amdgpu_bo_va *bo_va;
  1791. bo_va = list_first_entry(&vm->moved,
  1792. struct amdgpu_bo_va, base.vm_status);
  1793. spin_unlock(&vm->status_lock);
  1794. /* Per VM BOs never need to bo cleared in the page tables */
  1795. clear = bo_va->base.bo->tbo.resv != vm->root.base.bo->tbo.resv;
  1796. r = amdgpu_vm_bo_update(adev, bo_va, clear);
  1797. if (r)
  1798. return r;
  1799. spin_lock(&vm->status_lock);
  1800. }
  1801. spin_unlock(&vm->status_lock);
  1802. return r;
  1803. }
  1804. /**
  1805. * amdgpu_vm_bo_add - add a bo to a specific vm
  1806. *
  1807. * @adev: amdgpu_device pointer
  1808. * @vm: requested vm
  1809. * @bo: amdgpu buffer object
  1810. *
  1811. * Add @bo into the requested vm.
  1812. * Add @bo to the list of bos associated with the vm
  1813. * Returns newly added bo_va or NULL for failure
  1814. *
  1815. * Object has to be reserved!
  1816. */
  1817. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1818. struct amdgpu_vm *vm,
  1819. struct amdgpu_bo *bo)
  1820. {
  1821. struct amdgpu_bo_va *bo_va;
  1822. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1823. if (bo_va == NULL) {
  1824. return NULL;
  1825. }
  1826. bo_va->base.vm = vm;
  1827. bo_va->base.bo = bo;
  1828. INIT_LIST_HEAD(&bo_va->base.bo_list);
  1829. INIT_LIST_HEAD(&bo_va->base.vm_status);
  1830. bo_va->ref_count = 1;
  1831. INIT_LIST_HEAD(&bo_va->valids);
  1832. INIT_LIST_HEAD(&bo_va->invalids);
  1833. if (bo)
  1834. list_add_tail(&bo_va->base.bo_list, &bo->va);
  1835. return bo_va;
  1836. }
  1837. /**
  1838. * amdgpu_vm_bo_insert_mapping - insert a new mapping
  1839. *
  1840. * @adev: amdgpu_device pointer
  1841. * @bo_va: bo_va to store the address
  1842. * @mapping: the mapping to insert
  1843. *
  1844. * Insert a new mapping into all structures.
  1845. */
  1846. static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
  1847. struct amdgpu_bo_va *bo_va,
  1848. struct amdgpu_bo_va_mapping *mapping)
  1849. {
  1850. struct amdgpu_vm *vm = bo_va->base.vm;
  1851. struct amdgpu_bo *bo = bo_va->base.bo;
  1852. mapping->bo_va = bo_va;
  1853. list_add(&mapping->list, &bo_va->invalids);
  1854. amdgpu_vm_it_insert(mapping, &vm->va);
  1855. if (mapping->flags & AMDGPU_PTE_PRT)
  1856. amdgpu_vm_prt_get(adev);
  1857. if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
  1858. spin_lock(&vm->status_lock);
  1859. if (list_empty(&bo_va->base.vm_status))
  1860. list_add(&bo_va->base.vm_status, &vm->moved);
  1861. spin_unlock(&vm->status_lock);
  1862. }
  1863. trace_amdgpu_vm_bo_map(bo_va, mapping);
  1864. }
  1865. /**
  1866. * amdgpu_vm_bo_map - map bo inside a vm
  1867. *
  1868. * @adev: amdgpu_device pointer
  1869. * @bo_va: bo_va to store the address
  1870. * @saddr: where to map the BO
  1871. * @offset: requested offset in the BO
  1872. * @flags: attributes of pages (read/write/valid/etc.)
  1873. *
  1874. * Add a mapping of the BO at the specefied addr into the VM.
  1875. * Returns 0 for success, error for failure.
  1876. *
  1877. * Object has to be reserved and unreserved outside!
  1878. */
  1879. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1880. struct amdgpu_bo_va *bo_va,
  1881. uint64_t saddr, uint64_t offset,
  1882. uint64_t size, uint64_t flags)
  1883. {
  1884. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1885. struct amdgpu_bo *bo = bo_va->base.bo;
  1886. struct amdgpu_vm *vm = bo_va->base.vm;
  1887. uint64_t eaddr;
  1888. /* validate the parameters */
  1889. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1890. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1891. return -EINVAL;
  1892. /* make sure object fit at this offset */
  1893. eaddr = saddr + size - 1;
  1894. if (saddr >= eaddr ||
  1895. (bo && offset + size > amdgpu_bo_size(bo)))
  1896. return -EINVAL;
  1897. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1898. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1899. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1900. if (tmp) {
  1901. /* bo and tmp overlap, invalid addr */
  1902. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1903. "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
  1904. tmp->start, tmp->last + 1);
  1905. return -EINVAL;
  1906. }
  1907. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1908. if (!mapping)
  1909. return -ENOMEM;
  1910. mapping->start = saddr;
  1911. mapping->last = eaddr;
  1912. mapping->offset = offset;
  1913. mapping->flags = flags;
  1914. amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  1915. return 0;
  1916. }
  1917. /**
  1918. * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
  1919. *
  1920. * @adev: amdgpu_device pointer
  1921. * @bo_va: bo_va to store the address
  1922. * @saddr: where to map the BO
  1923. * @offset: requested offset in the BO
  1924. * @flags: attributes of pages (read/write/valid/etc.)
  1925. *
  1926. * Add a mapping of the BO at the specefied addr into the VM. Replace existing
  1927. * mappings as we do so.
  1928. * Returns 0 for success, error for failure.
  1929. *
  1930. * Object has to be reserved and unreserved outside!
  1931. */
  1932. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  1933. struct amdgpu_bo_va *bo_va,
  1934. uint64_t saddr, uint64_t offset,
  1935. uint64_t size, uint64_t flags)
  1936. {
  1937. struct amdgpu_bo_va_mapping *mapping;
  1938. struct amdgpu_bo *bo = bo_va->base.bo;
  1939. uint64_t eaddr;
  1940. int r;
  1941. /* validate the parameters */
  1942. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1943. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1944. return -EINVAL;
  1945. /* make sure object fit at this offset */
  1946. eaddr = saddr + size - 1;
  1947. if (saddr >= eaddr ||
  1948. (bo && offset + size > amdgpu_bo_size(bo)))
  1949. return -EINVAL;
  1950. /* Allocate all the needed memory */
  1951. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1952. if (!mapping)
  1953. return -ENOMEM;
  1954. r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
  1955. if (r) {
  1956. kfree(mapping);
  1957. return r;
  1958. }
  1959. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1960. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1961. mapping->start = saddr;
  1962. mapping->last = eaddr;
  1963. mapping->offset = offset;
  1964. mapping->flags = flags;
  1965. amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
  1966. return 0;
  1967. }
  1968. /**
  1969. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1970. *
  1971. * @adev: amdgpu_device pointer
  1972. * @bo_va: bo_va to remove the address from
  1973. * @saddr: where to the BO is mapped
  1974. *
  1975. * Remove a mapping of the BO at the specefied addr from the VM.
  1976. * Returns 0 for success, error for failure.
  1977. *
  1978. * Object has to be reserved and unreserved outside!
  1979. */
  1980. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1981. struct amdgpu_bo_va *bo_va,
  1982. uint64_t saddr)
  1983. {
  1984. struct amdgpu_bo_va_mapping *mapping;
  1985. struct amdgpu_vm *vm = bo_va->base.vm;
  1986. bool valid = true;
  1987. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1988. list_for_each_entry(mapping, &bo_va->valids, list) {
  1989. if (mapping->start == saddr)
  1990. break;
  1991. }
  1992. if (&mapping->list == &bo_va->valids) {
  1993. valid = false;
  1994. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1995. if (mapping->start == saddr)
  1996. break;
  1997. }
  1998. if (&mapping->list == &bo_va->invalids)
  1999. return -ENOENT;
  2000. }
  2001. list_del(&mapping->list);
  2002. amdgpu_vm_it_remove(mapping, &vm->va);
  2003. mapping->bo_va = NULL;
  2004. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  2005. if (valid)
  2006. list_add(&mapping->list, &vm->freed);
  2007. else
  2008. amdgpu_vm_free_mapping(adev, vm, mapping,
  2009. bo_va->last_pt_update);
  2010. return 0;
  2011. }
  2012. /**
  2013. * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
  2014. *
  2015. * @adev: amdgpu_device pointer
  2016. * @vm: VM structure to use
  2017. * @saddr: start of the range
  2018. * @size: size of the range
  2019. *
  2020. * Remove all mappings in a range, split them as appropriate.
  2021. * Returns 0 for success, error for failure.
  2022. */
  2023. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  2024. struct amdgpu_vm *vm,
  2025. uint64_t saddr, uint64_t size)
  2026. {
  2027. struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
  2028. LIST_HEAD(removed);
  2029. uint64_t eaddr;
  2030. eaddr = saddr + size - 1;
  2031. saddr /= AMDGPU_GPU_PAGE_SIZE;
  2032. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  2033. /* Allocate all the needed memory */
  2034. before = kzalloc(sizeof(*before), GFP_KERNEL);
  2035. if (!before)
  2036. return -ENOMEM;
  2037. INIT_LIST_HEAD(&before->list);
  2038. after = kzalloc(sizeof(*after), GFP_KERNEL);
  2039. if (!after) {
  2040. kfree(before);
  2041. return -ENOMEM;
  2042. }
  2043. INIT_LIST_HEAD(&after->list);
  2044. /* Now gather all removed mappings */
  2045. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  2046. while (tmp) {
  2047. /* Remember mapping split at the start */
  2048. if (tmp->start < saddr) {
  2049. before->start = tmp->start;
  2050. before->last = saddr - 1;
  2051. before->offset = tmp->offset;
  2052. before->flags = tmp->flags;
  2053. list_add(&before->list, &tmp->list);
  2054. }
  2055. /* Remember mapping split at the end */
  2056. if (tmp->last > eaddr) {
  2057. after->start = eaddr + 1;
  2058. after->last = tmp->last;
  2059. after->offset = tmp->offset;
  2060. after->offset += after->start - tmp->start;
  2061. after->flags = tmp->flags;
  2062. list_add(&after->list, &tmp->list);
  2063. }
  2064. list_del(&tmp->list);
  2065. list_add(&tmp->list, &removed);
  2066. tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
  2067. }
  2068. /* And free them up */
  2069. list_for_each_entry_safe(tmp, next, &removed, list) {
  2070. amdgpu_vm_it_remove(tmp, &vm->va);
  2071. list_del(&tmp->list);
  2072. if (tmp->start < saddr)
  2073. tmp->start = saddr;
  2074. if (tmp->last > eaddr)
  2075. tmp->last = eaddr;
  2076. tmp->bo_va = NULL;
  2077. list_add(&tmp->list, &vm->freed);
  2078. trace_amdgpu_vm_bo_unmap(NULL, tmp);
  2079. }
  2080. /* Insert partial mapping before the range */
  2081. if (!list_empty(&before->list)) {
  2082. amdgpu_vm_it_insert(before, &vm->va);
  2083. if (before->flags & AMDGPU_PTE_PRT)
  2084. amdgpu_vm_prt_get(adev);
  2085. } else {
  2086. kfree(before);
  2087. }
  2088. /* Insert partial mapping after the range */
  2089. if (!list_empty(&after->list)) {
  2090. amdgpu_vm_it_insert(after, &vm->va);
  2091. if (after->flags & AMDGPU_PTE_PRT)
  2092. amdgpu_vm_prt_get(adev);
  2093. } else {
  2094. kfree(after);
  2095. }
  2096. return 0;
  2097. }
  2098. /**
  2099. * amdgpu_vm_bo_lookup_mapping - find mapping by address
  2100. *
  2101. * @vm: the requested VM
  2102. *
  2103. * Find a mapping by it's address.
  2104. */
  2105. struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
  2106. uint64_t addr)
  2107. {
  2108. return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
  2109. }
  2110. /**
  2111. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  2112. *
  2113. * @adev: amdgpu_device pointer
  2114. * @bo_va: requested bo_va
  2115. *
  2116. * Remove @bo_va->bo from the requested vm.
  2117. *
  2118. * Object have to be reserved!
  2119. */
  2120. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  2121. struct amdgpu_bo_va *bo_va)
  2122. {
  2123. struct amdgpu_bo_va_mapping *mapping, *next;
  2124. struct amdgpu_vm *vm = bo_va->base.vm;
  2125. list_del(&bo_va->base.bo_list);
  2126. spin_lock(&vm->status_lock);
  2127. list_del(&bo_va->base.vm_status);
  2128. spin_unlock(&vm->status_lock);
  2129. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  2130. list_del(&mapping->list);
  2131. amdgpu_vm_it_remove(mapping, &vm->va);
  2132. mapping->bo_va = NULL;
  2133. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  2134. list_add(&mapping->list, &vm->freed);
  2135. }
  2136. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  2137. list_del(&mapping->list);
  2138. amdgpu_vm_it_remove(mapping, &vm->va);
  2139. amdgpu_vm_free_mapping(adev, vm, mapping,
  2140. bo_va->last_pt_update);
  2141. }
  2142. dma_fence_put(bo_va->last_pt_update);
  2143. kfree(bo_va);
  2144. }
  2145. /**
  2146. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  2147. *
  2148. * @adev: amdgpu_device pointer
  2149. * @vm: requested vm
  2150. * @bo: amdgpu buffer object
  2151. *
  2152. * Mark @bo as invalid.
  2153. */
  2154. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  2155. struct amdgpu_bo *bo, bool evicted)
  2156. {
  2157. struct amdgpu_vm_bo_base *bo_base;
  2158. list_for_each_entry(bo_base, &bo->va, bo_list) {
  2159. struct amdgpu_vm *vm = bo_base->vm;
  2160. bo_base->moved = true;
  2161. if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
  2162. spin_lock(&bo_base->vm->status_lock);
  2163. if (bo->tbo.type == ttm_bo_type_kernel)
  2164. list_move(&bo_base->vm_status, &vm->evicted);
  2165. else
  2166. list_move_tail(&bo_base->vm_status,
  2167. &vm->evicted);
  2168. spin_unlock(&bo_base->vm->status_lock);
  2169. continue;
  2170. }
  2171. if (bo->tbo.type == ttm_bo_type_kernel) {
  2172. spin_lock(&bo_base->vm->status_lock);
  2173. if (list_empty(&bo_base->vm_status))
  2174. list_add(&bo_base->vm_status, &vm->relocated);
  2175. spin_unlock(&bo_base->vm->status_lock);
  2176. continue;
  2177. }
  2178. spin_lock(&bo_base->vm->status_lock);
  2179. if (list_empty(&bo_base->vm_status))
  2180. list_add(&bo_base->vm_status, &vm->moved);
  2181. spin_unlock(&bo_base->vm->status_lock);
  2182. }
  2183. }
  2184. static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
  2185. {
  2186. /* Total bits covered by PD + PTs */
  2187. unsigned bits = ilog2(vm_size) + 18;
  2188. /* Make sure the PD is 4K in size up to 8GB address space.
  2189. Above that split equal between PD and PTs */
  2190. if (vm_size <= 8)
  2191. return (bits - 9);
  2192. else
  2193. return ((bits + 3) / 2);
  2194. }
  2195. /**
  2196. * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
  2197. *
  2198. * @adev: amdgpu_device pointer
  2199. * @vm_size: the default vm size if it's set auto
  2200. */
  2201. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
  2202. uint32_t fragment_size_default, unsigned max_level,
  2203. unsigned max_bits)
  2204. {
  2205. uint64_t tmp;
  2206. /* adjust vm size first */
  2207. if (amdgpu_vm_size != -1) {
  2208. unsigned max_size = 1 << (max_bits - 30);
  2209. vm_size = amdgpu_vm_size;
  2210. if (vm_size > max_size) {
  2211. dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
  2212. amdgpu_vm_size, max_size);
  2213. vm_size = max_size;
  2214. }
  2215. }
  2216. adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
  2217. tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
  2218. if (amdgpu_vm_block_size != -1)
  2219. tmp >>= amdgpu_vm_block_size - 9;
  2220. tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
  2221. adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
  2222. /* block size depends on vm size and hw setup*/
  2223. if (amdgpu_vm_block_size != -1)
  2224. adev->vm_manager.block_size =
  2225. min((unsigned)amdgpu_vm_block_size, max_bits
  2226. - AMDGPU_GPU_PAGE_SHIFT
  2227. - 9 * adev->vm_manager.num_level);
  2228. else if (adev->vm_manager.num_level > 1)
  2229. adev->vm_manager.block_size = 9;
  2230. else
  2231. adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
  2232. if (amdgpu_vm_fragment_size == -1)
  2233. adev->vm_manager.fragment_size = fragment_size_default;
  2234. else
  2235. adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
  2236. DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
  2237. vm_size, adev->vm_manager.num_level + 1,
  2238. adev->vm_manager.block_size,
  2239. adev->vm_manager.fragment_size);
  2240. }
  2241. /**
  2242. * amdgpu_vm_init - initialize a vm instance
  2243. *
  2244. * @adev: amdgpu_device pointer
  2245. * @vm: requested vm
  2246. * @vm_context: Indicates if it GFX or Compute context
  2247. *
  2248. * Init @vm fields.
  2249. */
  2250. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  2251. int vm_context, unsigned int pasid)
  2252. {
  2253. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  2254. AMDGPU_VM_PTE_COUNT(adev) * 8);
  2255. unsigned ring_instance;
  2256. struct amdgpu_ring *ring;
  2257. struct drm_sched_rq *rq;
  2258. int r, i;
  2259. u64 flags;
  2260. uint64_t init_pde_value = 0;
  2261. vm->va = RB_ROOT_CACHED;
  2262. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  2263. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2264. vm->reserved_vmid[i] = NULL;
  2265. spin_lock_init(&vm->status_lock);
  2266. INIT_LIST_HEAD(&vm->evicted);
  2267. INIT_LIST_HEAD(&vm->relocated);
  2268. INIT_LIST_HEAD(&vm->moved);
  2269. INIT_LIST_HEAD(&vm->freed);
  2270. /* create scheduler entity for page table updates */
  2271. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  2272. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  2273. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  2274. rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
  2275. r = drm_sched_entity_init(&ring->sched, &vm->entity,
  2276. rq, amdgpu_sched_jobs, NULL);
  2277. if (r)
  2278. return r;
  2279. vm->pte_support_ats = false;
  2280. if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
  2281. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2282. AMDGPU_VM_USE_CPU_FOR_COMPUTE);
  2283. if (adev->asic_type == CHIP_RAVEN) {
  2284. vm->pte_support_ats = true;
  2285. init_pde_value = AMDGPU_PTE_DEFAULT_ATC
  2286. | AMDGPU_PDE_PTE;
  2287. }
  2288. } else
  2289. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2290. AMDGPU_VM_USE_CPU_FOR_GFX);
  2291. DRM_DEBUG_DRIVER("VM update mode is %s\n",
  2292. vm->use_cpu_for_update ? "CPU" : "SDMA");
  2293. WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
  2294. "CPU update of VM recommended only for large BAR system\n");
  2295. vm->last_update = NULL;
  2296. flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  2297. AMDGPU_GEM_CREATE_VRAM_CLEARED;
  2298. if (vm->use_cpu_for_update)
  2299. flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  2300. else
  2301. flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  2302. AMDGPU_GEM_CREATE_SHADOW);
  2303. r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
  2304. AMDGPU_GEM_DOMAIN_VRAM,
  2305. flags,
  2306. NULL, NULL, init_pde_value, &vm->root.base.bo);
  2307. if (r)
  2308. goto error_free_sched_entity;
  2309. vm->root.base.vm = vm;
  2310. list_add_tail(&vm->root.base.bo_list, &vm->root.base.bo->va);
  2311. INIT_LIST_HEAD(&vm->root.base.vm_status);
  2312. if (vm->use_cpu_for_update) {
  2313. r = amdgpu_bo_reserve(vm->root.base.bo, false);
  2314. if (r)
  2315. goto error_free_root;
  2316. r = amdgpu_bo_kmap(vm->root.base.bo, NULL);
  2317. amdgpu_bo_unreserve(vm->root.base.bo);
  2318. if (r)
  2319. goto error_free_root;
  2320. }
  2321. if (pasid) {
  2322. unsigned long flags;
  2323. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2324. r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
  2325. GFP_ATOMIC);
  2326. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2327. if (r < 0)
  2328. goto error_free_root;
  2329. vm->pasid = pasid;
  2330. }
  2331. INIT_KFIFO(vm->faults);
  2332. vm->fault_credit = 16;
  2333. return 0;
  2334. error_free_root:
  2335. amdgpu_bo_unref(&vm->root.base.bo->shadow);
  2336. amdgpu_bo_unref(&vm->root.base.bo);
  2337. vm->root.base.bo = NULL;
  2338. error_free_sched_entity:
  2339. drm_sched_entity_fini(&ring->sched, &vm->entity);
  2340. return r;
  2341. }
  2342. /**
  2343. * amdgpu_vm_free_levels - free PD/PT levels
  2344. *
  2345. * @level: PD/PT starting level to free
  2346. *
  2347. * Free the page directory or page table level and all sub levels.
  2348. */
  2349. static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
  2350. {
  2351. unsigned i;
  2352. if (level->base.bo) {
  2353. list_del(&level->base.bo_list);
  2354. list_del(&level->base.vm_status);
  2355. amdgpu_bo_unref(&level->base.bo->shadow);
  2356. amdgpu_bo_unref(&level->base.bo);
  2357. }
  2358. if (level->entries)
  2359. for (i = 0; i <= level->last_entry_used; i++)
  2360. amdgpu_vm_free_levels(&level->entries[i]);
  2361. kvfree(level->entries);
  2362. }
  2363. /**
  2364. * amdgpu_vm_fini - tear down a vm instance
  2365. *
  2366. * @adev: amdgpu_device pointer
  2367. * @vm: requested vm
  2368. *
  2369. * Tear down @vm.
  2370. * Unbind the VM and remove all bos from the vm bo list
  2371. */
  2372. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  2373. {
  2374. struct amdgpu_bo_va_mapping *mapping, *tmp;
  2375. bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
  2376. struct amdgpu_bo *root;
  2377. u64 fault;
  2378. int i, r;
  2379. /* Clear pending page faults from IH when the VM is destroyed */
  2380. while (kfifo_get(&vm->faults, &fault))
  2381. amdgpu_ih_clear_fault(adev, fault);
  2382. if (vm->pasid) {
  2383. unsigned long flags;
  2384. spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
  2385. idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
  2386. spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
  2387. }
  2388. drm_sched_entity_fini(vm->entity.sched, &vm->entity);
  2389. if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
  2390. dev_err(adev->dev, "still active bo inside vm\n");
  2391. }
  2392. rbtree_postorder_for_each_entry_safe(mapping, tmp,
  2393. &vm->va.rb_root, rb) {
  2394. list_del(&mapping->list);
  2395. amdgpu_vm_it_remove(mapping, &vm->va);
  2396. kfree(mapping);
  2397. }
  2398. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  2399. if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
  2400. amdgpu_vm_prt_fini(adev, vm);
  2401. prt_fini_needed = false;
  2402. }
  2403. list_del(&mapping->list);
  2404. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  2405. }
  2406. root = amdgpu_bo_ref(vm->root.base.bo);
  2407. r = amdgpu_bo_reserve(root, true);
  2408. if (r) {
  2409. dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
  2410. } else {
  2411. amdgpu_vm_free_levels(&vm->root);
  2412. amdgpu_bo_unreserve(root);
  2413. }
  2414. amdgpu_bo_unref(&root);
  2415. dma_fence_put(vm->last_update);
  2416. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2417. amdgpu_vm_free_reserved_vmid(adev, vm, i);
  2418. }
  2419. /**
  2420. * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
  2421. *
  2422. * @adev: amdgpu_device pointer
  2423. * @pasid: PASID do identify the VM
  2424. *
  2425. * This function is expected to be called in interrupt context. Returns
  2426. * true if there was fault credit, false otherwise
  2427. */
  2428. bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
  2429. unsigned int pasid)
  2430. {
  2431. struct amdgpu_vm *vm;
  2432. spin_lock(&adev->vm_manager.pasid_lock);
  2433. vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
  2434. spin_unlock(&adev->vm_manager.pasid_lock);
  2435. if (!vm)
  2436. /* VM not found, can't track fault credit */
  2437. return true;
  2438. /* No lock needed. only accessed by IRQ handler */
  2439. if (!vm->fault_credit)
  2440. /* Too many faults in this VM */
  2441. return false;
  2442. vm->fault_credit--;
  2443. return true;
  2444. }
  2445. /**
  2446. * amdgpu_vm_manager_init - init the VM manager
  2447. *
  2448. * @adev: amdgpu_device pointer
  2449. *
  2450. * Initialize the VM manager structures
  2451. */
  2452. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  2453. {
  2454. unsigned i, j;
  2455. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  2456. struct amdgpu_vm_id_manager *id_mgr =
  2457. &adev->vm_manager.id_mgr[i];
  2458. mutex_init(&id_mgr->lock);
  2459. INIT_LIST_HEAD(&id_mgr->ids_lru);
  2460. atomic_set(&id_mgr->reserved_vmid_num, 0);
  2461. /* skip over VMID 0, since it is the system VM */
  2462. for (j = 1; j < id_mgr->num_ids; ++j) {
  2463. amdgpu_vm_reset_id(adev, i, j);
  2464. amdgpu_sync_create(&id_mgr->ids[i].active);
  2465. list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
  2466. }
  2467. }
  2468. adev->vm_manager.fence_context =
  2469. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2470. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  2471. adev->vm_manager.seqno[i] = 0;
  2472. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  2473. atomic64_set(&adev->vm_manager.client_counter, 0);
  2474. spin_lock_init(&adev->vm_manager.prt_lock);
  2475. atomic_set(&adev->vm_manager.num_prt_users, 0);
  2476. /* If not overridden by the user, by default, only in large BAR systems
  2477. * Compute VM tables will be updated by CPU
  2478. */
  2479. #ifdef CONFIG_X86_64
  2480. if (amdgpu_vm_update_mode == -1) {
  2481. if (amdgpu_vm_is_large_bar(adev))
  2482. adev->vm_manager.vm_update_mode =
  2483. AMDGPU_VM_USE_CPU_FOR_COMPUTE;
  2484. else
  2485. adev->vm_manager.vm_update_mode = 0;
  2486. } else
  2487. adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
  2488. #else
  2489. adev->vm_manager.vm_update_mode = 0;
  2490. #endif
  2491. idr_init(&adev->vm_manager.pasid_idr);
  2492. spin_lock_init(&adev->vm_manager.pasid_lock);
  2493. }
  2494. /**
  2495. * amdgpu_vm_manager_fini - cleanup VM manager
  2496. *
  2497. * @adev: amdgpu_device pointer
  2498. *
  2499. * Cleanup the VM manager and free resources.
  2500. */
  2501. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  2502. {
  2503. unsigned i, j;
  2504. WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
  2505. idr_destroy(&adev->vm_manager.pasid_idr);
  2506. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  2507. struct amdgpu_vm_id_manager *id_mgr =
  2508. &adev->vm_manager.id_mgr[i];
  2509. mutex_destroy(&id_mgr->lock);
  2510. for (j = 0; j < AMDGPU_NUM_VM; ++j) {
  2511. struct amdgpu_vm_id *id = &id_mgr->ids[j];
  2512. amdgpu_sync_free(&id->active);
  2513. dma_fence_put(id->flushed_updates);
  2514. dma_fence_put(id->last_flush);
  2515. }
  2516. }
  2517. }
  2518. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  2519. {
  2520. union drm_amdgpu_vm *args = data;
  2521. struct amdgpu_device *adev = dev->dev_private;
  2522. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  2523. int r;
  2524. switch (args->in.op) {
  2525. case AMDGPU_VM_OP_RESERVE_VMID:
  2526. /* current, we only have requirement to reserve vmid from gfxhub */
  2527. r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
  2528. AMDGPU_GFXHUB);
  2529. if (r)
  2530. return r;
  2531. break;
  2532. case AMDGPU_VM_OP_UNRESERVE_VMID:
  2533. amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2534. break;
  2535. default:
  2536. return -EINVAL;
  2537. }
  2538. return 0;
  2539. }