amdgpu_vm.c 71 KB

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