amdgpu_vm.c 66 KB

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