amdgpu_vm.c 64 KB

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