amdgpu_vm.c 66 KB

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