amdgpu_vm.c 71 KB

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