amdgpu_vm.c 71 KB

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