amdgpu_vm.c 70 KB

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