amdgpu_vm.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505
  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. bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
  578. struct amdgpu_job *job)
  579. {
  580. struct amdgpu_device *adev = ring->adev;
  581. unsigned vmhub = ring->funcs->vmhub;
  582. struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  583. struct amdgpu_vm_id *id;
  584. bool gds_switch_needed;
  585. bool vm_flush_needed = job->vm_needs_flush ||
  586. amdgpu_ring_has_compute_vm_bug(ring);
  587. if (job->vm_id == 0)
  588. return false;
  589. id = &id_mgr->ids[job->vm_id];
  590. gds_switch_needed = ring->funcs->emit_gds_switch && (
  591. id->gds_base != job->gds_base ||
  592. id->gds_size != job->gds_size ||
  593. id->gws_base != job->gws_base ||
  594. id->gws_size != job->gws_size ||
  595. id->oa_base != job->oa_base ||
  596. id->oa_size != job->oa_size);
  597. if (amdgpu_vm_had_gpu_reset(adev, id))
  598. return true;
  599. return vm_flush_needed || gds_switch_needed;
  600. }
  601. /**
  602. * amdgpu_vm_flush - hardware flush the vm
  603. *
  604. * @ring: ring to use for flush
  605. * @vm_id: vmid number to use
  606. * @pd_addr: address of the page directory
  607. *
  608. * Emit a VM flush when it is necessary.
  609. */
  610. int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
  611. {
  612. struct amdgpu_device *adev = ring->adev;
  613. unsigned vmhub = ring->funcs->vmhub;
  614. struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  615. struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
  616. bool 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. bool vm_flush_needed = job->vm_needs_flush;
  624. unsigned patch_offset = 0;
  625. int r;
  626. if (amdgpu_vm_had_gpu_reset(adev, id)) {
  627. gds_switch_needed = true;
  628. vm_flush_needed = true;
  629. }
  630. if (!vm_flush_needed && !gds_switch_needed)
  631. return 0;
  632. if (ring->funcs->init_cond_exec)
  633. patch_offset = amdgpu_ring_init_cond_exec(ring);
  634. if (ring->funcs->emit_vm_flush && vm_flush_needed) {
  635. struct dma_fence *fence;
  636. trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
  637. amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
  638. r = amdgpu_fence_emit(ring, &fence);
  639. if (r)
  640. return r;
  641. mutex_lock(&id_mgr->lock);
  642. dma_fence_put(id->last_flush);
  643. id->last_flush = fence;
  644. id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
  645. mutex_unlock(&id_mgr->lock);
  646. }
  647. if (ring->funcs->emit_gds_switch && gds_switch_needed) {
  648. id->gds_base = job->gds_base;
  649. id->gds_size = job->gds_size;
  650. id->gws_base = job->gws_base;
  651. id->gws_size = job->gws_size;
  652. id->oa_base = job->oa_base;
  653. id->oa_size = job->oa_size;
  654. amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
  655. job->gds_size, job->gws_base,
  656. job->gws_size, job->oa_base,
  657. job->oa_size);
  658. }
  659. if (ring->funcs->patch_cond_exec)
  660. amdgpu_ring_patch_cond_exec(ring, patch_offset);
  661. /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
  662. if (ring->funcs->emit_switch_buffer) {
  663. amdgpu_ring_emit_switch_buffer(ring);
  664. amdgpu_ring_emit_switch_buffer(ring);
  665. }
  666. return 0;
  667. }
  668. /**
  669. * amdgpu_vm_reset_id - reset VMID to zero
  670. *
  671. * @adev: amdgpu device structure
  672. * @vm_id: vmid number to use
  673. *
  674. * Reset saved GDW, GWS and OA to force switch on next flush.
  675. */
  676. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
  677. unsigned vmid)
  678. {
  679. struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  680. struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
  681. atomic64_set(&id->owner, 0);
  682. id->gds_base = 0;
  683. id->gds_size = 0;
  684. id->gws_base = 0;
  685. id->gws_size = 0;
  686. id->oa_base = 0;
  687. id->oa_size = 0;
  688. }
  689. /**
  690. * amdgpu_vm_reset_all_id - reset VMID to zero
  691. *
  692. * @adev: amdgpu device structure
  693. *
  694. * Reset VMID to force flush on next use
  695. */
  696. void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
  697. {
  698. unsigned i, j;
  699. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  700. struct amdgpu_vm_id_manager *id_mgr =
  701. &adev->vm_manager.id_mgr[i];
  702. for (j = 1; j < id_mgr->num_ids; ++j)
  703. amdgpu_vm_reset_id(adev, i, j);
  704. }
  705. }
  706. /**
  707. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  708. *
  709. * @vm: requested vm
  710. * @bo: requested buffer object
  711. *
  712. * Find @bo inside the requested vm.
  713. * Search inside the @bos vm list for the requested vm
  714. * Returns the found bo_va or NULL if none is found
  715. *
  716. * Object has to be reserved!
  717. */
  718. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  719. struct amdgpu_bo *bo)
  720. {
  721. struct amdgpu_bo_va *bo_va;
  722. list_for_each_entry(bo_va, &bo->va, bo_list) {
  723. if (bo_va->vm == vm) {
  724. return bo_va;
  725. }
  726. }
  727. return NULL;
  728. }
  729. /**
  730. * amdgpu_vm_do_set_ptes - helper to call the right asic function
  731. *
  732. * @params: see amdgpu_pte_update_params definition
  733. * @pe: addr of the page entry
  734. * @addr: dst addr to write into pe
  735. * @count: number of page entries to update
  736. * @incr: increase next addr by incr bytes
  737. * @flags: hw access flags
  738. *
  739. * Traces the parameters and calls the right asic functions
  740. * to setup the page table using the DMA.
  741. */
  742. static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
  743. uint64_t pe, uint64_t addr,
  744. unsigned count, uint32_t incr,
  745. uint64_t flags)
  746. {
  747. trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
  748. if (count < 3) {
  749. amdgpu_vm_write_pte(params->adev, params->ib, pe,
  750. addr | flags, count, incr);
  751. } else {
  752. amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
  753. count, incr, flags);
  754. }
  755. }
  756. /**
  757. * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
  758. *
  759. * @params: see amdgpu_pte_update_params definition
  760. * @pe: addr of the page entry
  761. * @addr: dst addr to write into pe
  762. * @count: number of page entries to update
  763. * @incr: increase next addr by incr bytes
  764. * @flags: hw access flags
  765. *
  766. * Traces the parameters and calls the DMA function to copy the PTEs.
  767. */
  768. static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
  769. uint64_t pe, uint64_t addr,
  770. unsigned count, uint32_t incr,
  771. uint64_t flags)
  772. {
  773. uint64_t src = (params->src + (addr >> 12) * 8);
  774. trace_amdgpu_vm_copy_ptes(pe, src, count);
  775. amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
  776. }
  777. /**
  778. * amdgpu_vm_map_gart - Resolve gart mapping of addr
  779. *
  780. * @pages_addr: optional DMA address to use for lookup
  781. * @addr: the unmapped addr
  782. *
  783. * Look up the physical address of the page that the pte resolves
  784. * to and return the pointer for the page table entry.
  785. */
  786. static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
  787. {
  788. uint64_t result;
  789. /* page table offset */
  790. result = pages_addr[addr >> PAGE_SHIFT];
  791. /* in case cpu page size != gpu page size*/
  792. result |= addr & (~PAGE_MASK);
  793. result &= 0xFFFFFFFFFFFFF000ULL;
  794. return result;
  795. }
  796. /*
  797. * amdgpu_vm_update_level - update a single level in the hierarchy
  798. *
  799. * @adev: amdgpu_device pointer
  800. * @vm: requested vm
  801. * @parent: parent directory
  802. *
  803. * Makes sure all entries in @parent are up to date.
  804. * Returns 0 for success, error for failure.
  805. */
  806. static int amdgpu_vm_update_level(struct amdgpu_device *adev,
  807. struct amdgpu_vm *vm,
  808. struct amdgpu_vm_pt *parent,
  809. unsigned level)
  810. {
  811. struct amdgpu_bo *shadow;
  812. struct amdgpu_ring *ring;
  813. uint64_t pd_addr, shadow_addr;
  814. uint32_t incr = amdgpu_vm_bo_size(adev, level + 1);
  815. uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
  816. unsigned count = 0, pt_idx, ndw;
  817. struct amdgpu_job *job;
  818. struct amdgpu_pte_update_params params;
  819. struct dma_fence *fence = NULL;
  820. int r;
  821. if (!parent->entries)
  822. return 0;
  823. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  824. /* padding, etc. */
  825. ndw = 64;
  826. /* assume the worst case */
  827. ndw += parent->last_entry_used * 6;
  828. pd_addr = amdgpu_bo_gpu_offset(parent->bo);
  829. shadow = parent->bo->shadow;
  830. if (shadow) {
  831. r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
  832. if (r)
  833. return r;
  834. shadow_addr = amdgpu_bo_gpu_offset(shadow);
  835. ndw *= 2;
  836. } else {
  837. shadow_addr = 0;
  838. }
  839. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  840. if (r)
  841. return r;
  842. memset(&params, 0, sizeof(params));
  843. params.adev = adev;
  844. params.ib = &job->ibs[0];
  845. /* walk over the address space and update the directory */
  846. for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
  847. struct amdgpu_bo *bo = parent->entries[pt_idx].bo;
  848. uint64_t pde, pt;
  849. if (bo == NULL)
  850. continue;
  851. if (bo->shadow) {
  852. struct amdgpu_bo *pt_shadow = bo->shadow;
  853. r = amdgpu_ttm_bind(&pt_shadow->tbo,
  854. &pt_shadow->tbo.mem);
  855. if (r)
  856. return r;
  857. }
  858. pt = amdgpu_bo_gpu_offset(bo);
  859. if (parent->entries[pt_idx].addr == pt)
  860. continue;
  861. parent->entries[pt_idx].addr = pt;
  862. pde = pd_addr + pt_idx * 8;
  863. if (((last_pde + 8 * count) != pde) ||
  864. ((last_pt + incr * count) != pt) ||
  865. (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
  866. if (count) {
  867. uint64_t entry;
  868. entry = amdgpu_gart_get_vm_pde(adev, last_pt);
  869. if (shadow)
  870. amdgpu_vm_do_set_ptes(&params,
  871. last_shadow,
  872. entry, count,
  873. incr,
  874. AMDGPU_PTE_VALID);
  875. amdgpu_vm_do_set_ptes(&params, last_pde,
  876. entry, count, incr,
  877. AMDGPU_PTE_VALID);
  878. }
  879. count = 1;
  880. last_pde = pde;
  881. last_shadow = shadow_addr + pt_idx * 8;
  882. last_pt = pt;
  883. } else {
  884. ++count;
  885. }
  886. }
  887. if (count) {
  888. uint64_t entry;
  889. entry = amdgpu_gart_get_vm_pde(adev, last_pt);
  890. if (vm->root.bo->shadow)
  891. amdgpu_vm_do_set_ptes(&params, last_shadow, entry,
  892. count, incr, AMDGPU_PTE_VALID);
  893. amdgpu_vm_do_set_ptes(&params, last_pde, entry,
  894. count, incr, AMDGPU_PTE_VALID);
  895. }
  896. if (params.ib->length_dw == 0) {
  897. amdgpu_job_free(job);
  898. } else {
  899. amdgpu_ring_pad_ib(ring, params.ib);
  900. amdgpu_sync_resv(adev, &job->sync, parent->bo->tbo.resv,
  901. AMDGPU_FENCE_OWNER_VM);
  902. if (shadow)
  903. amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
  904. AMDGPU_FENCE_OWNER_VM);
  905. WARN_ON(params.ib->length_dw > ndw);
  906. r = amdgpu_job_submit(job, ring, &vm->entity,
  907. AMDGPU_FENCE_OWNER_VM, &fence);
  908. if (r)
  909. goto error_free;
  910. amdgpu_bo_fence(parent->bo, fence, true);
  911. dma_fence_put(vm->last_dir_update);
  912. vm->last_dir_update = dma_fence_get(fence);
  913. dma_fence_put(fence);
  914. }
  915. /*
  916. * Recurse into the subdirectories. This recursion is harmless because
  917. * we only have a maximum of 5 layers.
  918. */
  919. for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
  920. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  921. if (!entry->bo)
  922. continue;
  923. r = amdgpu_vm_update_level(adev, vm, entry, level + 1);
  924. if (r)
  925. return r;
  926. }
  927. return 0;
  928. error_free:
  929. amdgpu_job_free(job);
  930. return r;
  931. }
  932. /*
  933. * amdgpu_vm_invalidate_level - mark all PD levels as invalid
  934. *
  935. * @parent: parent PD
  936. *
  937. * Mark all PD level as invalid after an error.
  938. */
  939. static void amdgpu_vm_invalidate_level(struct amdgpu_vm_pt *parent)
  940. {
  941. unsigned pt_idx;
  942. /*
  943. * Recurse into the subdirectories. This recursion is harmless because
  944. * we only have a maximum of 5 layers.
  945. */
  946. for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
  947. struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
  948. if (!entry->bo)
  949. continue;
  950. entry->addr = ~0ULL;
  951. amdgpu_vm_invalidate_level(entry);
  952. }
  953. }
  954. /*
  955. * amdgpu_vm_update_directories - make sure that all directories are valid
  956. *
  957. * @adev: amdgpu_device pointer
  958. * @vm: requested vm
  959. *
  960. * Makes sure all directories are up to date.
  961. * Returns 0 for success, error for failure.
  962. */
  963. int amdgpu_vm_update_directories(struct amdgpu_device *adev,
  964. struct amdgpu_vm *vm)
  965. {
  966. int r;
  967. r = amdgpu_vm_update_level(adev, vm, &vm->root, 0);
  968. if (r)
  969. amdgpu_vm_invalidate_level(&vm->root);
  970. return r;
  971. }
  972. /**
  973. * amdgpu_vm_find_pt - find the page table for an address
  974. *
  975. * @p: see amdgpu_pte_update_params definition
  976. * @addr: virtual address in question
  977. *
  978. * Find the page table BO for a virtual address, return NULL when none found.
  979. */
  980. static struct amdgpu_bo *amdgpu_vm_get_pt(struct amdgpu_pte_update_params *p,
  981. uint64_t addr)
  982. {
  983. struct amdgpu_vm_pt *entry = &p->vm->root;
  984. unsigned idx, level = p->adev->vm_manager.num_level;
  985. while (entry->entries) {
  986. idx = addr >> (p->adev->vm_manager.block_size * level--);
  987. idx %= amdgpu_bo_size(entry->bo) / 8;
  988. entry = &entry->entries[idx];
  989. }
  990. if (level)
  991. return NULL;
  992. return entry->bo;
  993. }
  994. /**
  995. * amdgpu_vm_update_ptes - make sure that page tables are valid
  996. *
  997. * @params: see amdgpu_pte_update_params definition
  998. * @vm: requested vm
  999. * @start: start of GPU address range
  1000. * @end: end of GPU address range
  1001. * @dst: destination address to map to, the next dst inside the function
  1002. * @flags: mapping flags
  1003. *
  1004. * Update the page tables in the range @start - @end.
  1005. * Returns 0 for success, -EINVAL for failure.
  1006. */
  1007. static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  1008. uint64_t start, uint64_t end,
  1009. uint64_t dst, uint64_t flags)
  1010. {
  1011. struct amdgpu_device *adev = params->adev;
  1012. const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
  1013. uint64_t cur_pe_start, cur_nptes, cur_dst;
  1014. uint64_t addr; /* next GPU address to be updated */
  1015. struct amdgpu_bo *pt;
  1016. unsigned nptes; /* next number of ptes to be updated */
  1017. uint64_t next_pe_start;
  1018. /* initialize the variables */
  1019. addr = start;
  1020. pt = amdgpu_vm_get_pt(params, addr);
  1021. if (!pt) {
  1022. pr_err("PT not found, aborting update_ptes\n");
  1023. return -EINVAL;
  1024. }
  1025. if (params->shadow) {
  1026. if (!pt->shadow)
  1027. return 0;
  1028. pt = pt->shadow;
  1029. }
  1030. if ((addr & ~mask) == (end & ~mask))
  1031. nptes = end - addr;
  1032. else
  1033. nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
  1034. cur_pe_start = amdgpu_bo_gpu_offset(pt);
  1035. cur_pe_start += (addr & mask) * 8;
  1036. cur_nptes = nptes;
  1037. cur_dst = dst;
  1038. /* for next ptb*/
  1039. addr += nptes;
  1040. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  1041. /* walk over the address space and update the page tables */
  1042. while (addr < end) {
  1043. pt = amdgpu_vm_get_pt(params, addr);
  1044. if (!pt) {
  1045. pr_err("PT not found, aborting update_ptes\n");
  1046. return -EINVAL;
  1047. }
  1048. if (params->shadow) {
  1049. if (!pt->shadow)
  1050. return 0;
  1051. pt = pt->shadow;
  1052. }
  1053. if ((addr & ~mask) == (end & ~mask))
  1054. nptes = end - addr;
  1055. else
  1056. nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
  1057. next_pe_start = amdgpu_bo_gpu_offset(pt);
  1058. next_pe_start += (addr & mask) * 8;
  1059. if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
  1060. ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
  1061. /* The next ptb is consecutive to current ptb.
  1062. * Don't call the update function now.
  1063. * Will update two ptbs together in future.
  1064. */
  1065. cur_nptes += nptes;
  1066. } else {
  1067. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  1068. AMDGPU_GPU_PAGE_SIZE, flags);
  1069. cur_pe_start = next_pe_start;
  1070. cur_nptes = nptes;
  1071. cur_dst = dst;
  1072. }
  1073. /* for next ptb*/
  1074. addr += nptes;
  1075. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  1076. }
  1077. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  1078. AMDGPU_GPU_PAGE_SIZE, flags);
  1079. return 0;
  1080. }
  1081. /*
  1082. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  1083. *
  1084. * @params: see amdgpu_pte_update_params definition
  1085. * @vm: requested vm
  1086. * @start: first PTE to handle
  1087. * @end: last PTE to handle
  1088. * @dst: addr those PTEs should point to
  1089. * @flags: hw mapping flags
  1090. * Returns 0 for success, -EINVAL for failure.
  1091. */
  1092. static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  1093. uint64_t start, uint64_t end,
  1094. uint64_t dst, uint64_t flags)
  1095. {
  1096. int r;
  1097. /**
  1098. * The MC L1 TLB supports variable sized pages, based on a fragment
  1099. * field in the PTE. When this field is set to a non-zero value, page
  1100. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  1101. * flags are considered valid for all PTEs within the fragment range
  1102. * and corresponding mappings are assumed to be physically contiguous.
  1103. *
  1104. * The L1 TLB can store a single PTE for the whole fragment,
  1105. * significantly increasing the space available for translation
  1106. * caching. This leads to large improvements in throughput when the
  1107. * TLB is under pressure.
  1108. *
  1109. * The L2 TLB distributes small and large fragments into two
  1110. * asymmetric partitions. The large fragment cache is significantly
  1111. * larger. Thus, we try to use large fragments wherever possible.
  1112. * Userspace can support this by aligning virtual base address and
  1113. * allocation size to the fragment size.
  1114. */
  1115. /* SI and newer are optimized for 64KB */
  1116. uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
  1117. uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
  1118. uint64_t frag_start = ALIGN(start, frag_align);
  1119. uint64_t frag_end = end & ~(frag_align - 1);
  1120. /* system pages are non continuously */
  1121. if (params->src || !(flags & AMDGPU_PTE_VALID) ||
  1122. (frag_start >= frag_end))
  1123. return amdgpu_vm_update_ptes(params, start, end, dst, flags);
  1124. /* handle the 4K area at the beginning */
  1125. if (start != frag_start) {
  1126. r = amdgpu_vm_update_ptes(params, start, frag_start,
  1127. dst, flags);
  1128. if (r)
  1129. return r;
  1130. dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
  1131. }
  1132. /* handle the area in the middle */
  1133. r = amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
  1134. flags | frag_flags);
  1135. if (r)
  1136. return r;
  1137. /* handle the 4K area at the end */
  1138. if (frag_end != end) {
  1139. dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
  1140. r = amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
  1141. }
  1142. return r;
  1143. }
  1144. /**
  1145. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  1146. *
  1147. * @adev: amdgpu_device pointer
  1148. * @exclusive: fence we need to sync to
  1149. * @src: address where to copy page table entries from
  1150. * @pages_addr: DMA addresses to use for mapping
  1151. * @vm: requested vm
  1152. * @start: start of mapped range
  1153. * @last: last mapped entry
  1154. * @flags: flags for the entries
  1155. * @addr: addr to set the area to
  1156. * @fence: optional resulting fence
  1157. *
  1158. * Fill in the page table entries between @start and @last.
  1159. * Returns 0 for success, -EINVAL for failure.
  1160. */
  1161. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  1162. struct dma_fence *exclusive,
  1163. uint64_t src,
  1164. dma_addr_t *pages_addr,
  1165. struct amdgpu_vm *vm,
  1166. uint64_t start, uint64_t last,
  1167. uint64_t flags, uint64_t addr,
  1168. struct dma_fence **fence)
  1169. {
  1170. struct amdgpu_ring *ring;
  1171. void *owner = AMDGPU_FENCE_OWNER_VM;
  1172. unsigned nptes, ncmds, ndw;
  1173. struct amdgpu_job *job;
  1174. struct amdgpu_pte_update_params params;
  1175. struct dma_fence *f = NULL;
  1176. int r;
  1177. memset(&params, 0, sizeof(params));
  1178. params.adev = adev;
  1179. params.vm = vm;
  1180. params.src = src;
  1181. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  1182. /* sync to everything on unmapping */
  1183. if (!(flags & AMDGPU_PTE_VALID))
  1184. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  1185. nptes = last - start + 1;
  1186. /*
  1187. * reserve space for one command every (1 << BLOCK_SIZE)
  1188. * entries or 2k dwords (whatever is smaller)
  1189. */
  1190. ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1;
  1191. /* padding, etc. */
  1192. ndw = 64;
  1193. if (src) {
  1194. /* only copy commands needed */
  1195. ndw += ncmds * 7;
  1196. params.func = amdgpu_vm_do_copy_ptes;
  1197. } else if (pages_addr) {
  1198. /* copy commands needed */
  1199. ndw += ncmds * 7;
  1200. /* and also PTEs */
  1201. ndw += nptes * 2;
  1202. params.func = amdgpu_vm_do_copy_ptes;
  1203. } else {
  1204. /* set page commands needed */
  1205. ndw += ncmds * 10;
  1206. /* two extra commands for begin/end of fragment */
  1207. ndw += 2 * 10;
  1208. params.func = amdgpu_vm_do_set_ptes;
  1209. }
  1210. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  1211. if (r)
  1212. return r;
  1213. params.ib = &job->ibs[0];
  1214. if (!src && pages_addr) {
  1215. uint64_t *pte;
  1216. unsigned i;
  1217. /* Put the PTEs at the end of the IB. */
  1218. i = ndw - nptes * 2;
  1219. pte= (uint64_t *)&(job->ibs->ptr[i]);
  1220. params.src = job->ibs->gpu_addr + i * 4;
  1221. for (i = 0; i < nptes; ++i) {
  1222. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  1223. AMDGPU_GPU_PAGE_SIZE);
  1224. pte[i] |= flags;
  1225. }
  1226. addr = 0;
  1227. }
  1228. r = amdgpu_sync_fence(adev, &job->sync, exclusive);
  1229. if (r)
  1230. goto error_free;
  1231. r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
  1232. owner);
  1233. if (r)
  1234. goto error_free;
  1235. r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
  1236. if (r)
  1237. goto error_free;
  1238. params.shadow = true;
  1239. r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1240. if (r)
  1241. goto error_free;
  1242. params.shadow = false;
  1243. r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1244. if (r)
  1245. goto error_free;
  1246. amdgpu_ring_pad_ib(ring, params.ib);
  1247. WARN_ON(params.ib->length_dw > ndw);
  1248. r = amdgpu_job_submit(job, ring, &vm->entity,
  1249. AMDGPU_FENCE_OWNER_VM, &f);
  1250. if (r)
  1251. goto error_free;
  1252. amdgpu_bo_fence(vm->root.bo, f, true);
  1253. dma_fence_put(*fence);
  1254. *fence = f;
  1255. return 0;
  1256. error_free:
  1257. amdgpu_job_free(job);
  1258. return r;
  1259. }
  1260. /**
  1261. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  1262. *
  1263. * @adev: amdgpu_device pointer
  1264. * @exclusive: fence we need to sync to
  1265. * @gtt_flags: flags as they are used for GTT
  1266. * @pages_addr: DMA addresses to use for mapping
  1267. * @vm: requested vm
  1268. * @mapping: mapped range and flags to use for the update
  1269. * @flags: HW flags for the mapping
  1270. * @nodes: array of drm_mm_nodes with the MC addresses
  1271. * @fence: optional resulting fence
  1272. *
  1273. * Split the mapping into smaller chunks so that each update fits
  1274. * into a SDMA IB.
  1275. * Returns 0 for success, -EINVAL for failure.
  1276. */
  1277. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  1278. struct dma_fence *exclusive,
  1279. uint64_t gtt_flags,
  1280. dma_addr_t *pages_addr,
  1281. struct amdgpu_vm *vm,
  1282. struct amdgpu_bo_va_mapping *mapping,
  1283. uint64_t flags,
  1284. struct drm_mm_node *nodes,
  1285. struct dma_fence **fence)
  1286. {
  1287. uint64_t pfn, src = 0, start = mapping->start;
  1288. int r;
  1289. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  1290. * but in case of something, we filter the flags in first place
  1291. */
  1292. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  1293. flags &= ~AMDGPU_PTE_READABLE;
  1294. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  1295. flags &= ~AMDGPU_PTE_WRITEABLE;
  1296. flags &= ~AMDGPU_PTE_EXECUTABLE;
  1297. flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
  1298. flags &= ~AMDGPU_PTE_MTYPE_MASK;
  1299. flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
  1300. if ((mapping->flags & AMDGPU_PTE_PRT) &&
  1301. (adev->asic_type >= CHIP_VEGA10)) {
  1302. flags |= AMDGPU_PTE_PRT;
  1303. flags &= ~AMDGPU_PTE_VALID;
  1304. }
  1305. trace_amdgpu_vm_bo_update(mapping);
  1306. pfn = mapping->offset >> PAGE_SHIFT;
  1307. if (nodes) {
  1308. while (pfn >= nodes->size) {
  1309. pfn -= nodes->size;
  1310. ++nodes;
  1311. }
  1312. }
  1313. do {
  1314. uint64_t max_entries;
  1315. uint64_t addr, last;
  1316. if (nodes) {
  1317. addr = nodes->start << PAGE_SHIFT;
  1318. max_entries = (nodes->size - pfn) *
  1319. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  1320. } else {
  1321. addr = 0;
  1322. max_entries = S64_MAX;
  1323. }
  1324. if (pages_addr) {
  1325. if (flags == gtt_flags)
  1326. src = adev->gart.table_addr +
  1327. (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
  1328. else
  1329. max_entries = min(max_entries, 16ull * 1024ull);
  1330. addr = 0;
  1331. } else if (flags & AMDGPU_PTE_VALID) {
  1332. addr += adev->vm_manager.vram_base_offset;
  1333. }
  1334. addr += pfn << PAGE_SHIFT;
  1335. last = min((uint64_t)mapping->last, start + max_entries - 1);
  1336. r = amdgpu_vm_bo_update_mapping(adev, exclusive,
  1337. src, pages_addr, vm,
  1338. start, last, flags, addr,
  1339. fence);
  1340. if (r)
  1341. return r;
  1342. pfn += last - start + 1;
  1343. if (nodes && nodes->size == pfn) {
  1344. pfn = 0;
  1345. ++nodes;
  1346. }
  1347. start = last + 1;
  1348. } while (unlikely(start != mapping->last + 1));
  1349. return 0;
  1350. }
  1351. /**
  1352. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  1353. *
  1354. * @adev: amdgpu_device pointer
  1355. * @bo_va: requested BO and VM object
  1356. * @clear: if true clear the entries
  1357. *
  1358. * Fill in the page table entries for @bo_va.
  1359. * Returns 0 for success, -EINVAL for failure.
  1360. */
  1361. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  1362. struct amdgpu_bo_va *bo_va,
  1363. bool clear)
  1364. {
  1365. struct amdgpu_vm *vm = bo_va->vm;
  1366. struct amdgpu_bo_va_mapping *mapping;
  1367. dma_addr_t *pages_addr = NULL;
  1368. uint64_t gtt_flags, flags;
  1369. struct ttm_mem_reg *mem;
  1370. struct drm_mm_node *nodes;
  1371. struct dma_fence *exclusive;
  1372. int r;
  1373. if (clear || !bo_va->bo) {
  1374. mem = NULL;
  1375. nodes = NULL;
  1376. exclusive = NULL;
  1377. } else {
  1378. struct ttm_dma_tt *ttm;
  1379. mem = &bo_va->bo->tbo.mem;
  1380. nodes = mem->mm_node;
  1381. if (mem->mem_type == TTM_PL_TT) {
  1382. ttm = container_of(bo_va->bo->tbo.ttm, struct
  1383. ttm_dma_tt, ttm);
  1384. pages_addr = ttm->dma_address;
  1385. }
  1386. exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
  1387. }
  1388. if (bo_va->bo) {
  1389. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  1390. gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
  1391. adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
  1392. flags : 0;
  1393. } else {
  1394. flags = 0x0;
  1395. gtt_flags = ~0x0;
  1396. }
  1397. spin_lock(&vm->status_lock);
  1398. if (!list_empty(&bo_va->vm_status))
  1399. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1400. spin_unlock(&vm->status_lock);
  1401. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1402. r = amdgpu_vm_bo_split_mapping(adev, exclusive,
  1403. gtt_flags, pages_addr, vm,
  1404. mapping, flags, nodes,
  1405. &bo_va->last_pt_update);
  1406. if (r)
  1407. return r;
  1408. }
  1409. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1410. list_for_each_entry(mapping, &bo_va->valids, list)
  1411. trace_amdgpu_vm_bo_mapping(mapping);
  1412. list_for_each_entry(mapping, &bo_va->invalids, list)
  1413. trace_amdgpu_vm_bo_mapping(mapping);
  1414. }
  1415. spin_lock(&vm->status_lock);
  1416. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1417. list_del_init(&bo_va->vm_status);
  1418. if (clear)
  1419. list_add(&bo_va->vm_status, &vm->cleared);
  1420. spin_unlock(&vm->status_lock);
  1421. return 0;
  1422. }
  1423. /**
  1424. * amdgpu_vm_update_prt_state - update the global PRT state
  1425. */
  1426. static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
  1427. {
  1428. unsigned long flags;
  1429. bool enable;
  1430. spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
  1431. enable = !!atomic_read(&adev->vm_manager.num_prt_users);
  1432. adev->gart.gart_funcs->set_prt(adev, enable);
  1433. spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
  1434. }
  1435. /**
  1436. * amdgpu_vm_prt_get - add a PRT user
  1437. */
  1438. static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
  1439. {
  1440. if (!adev->gart.gart_funcs->set_prt)
  1441. return;
  1442. if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
  1443. amdgpu_vm_update_prt_state(adev);
  1444. }
  1445. /**
  1446. * amdgpu_vm_prt_put - drop a PRT user
  1447. */
  1448. static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
  1449. {
  1450. if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
  1451. amdgpu_vm_update_prt_state(adev);
  1452. }
  1453. /**
  1454. * amdgpu_vm_prt_cb - callback for updating the PRT status
  1455. */
  1456. static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
  1457. {
  1458. struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
  1459. amdgpu_vm_prt_put(cb->adev);
  1460. kfree(cb);
  1461. }
  1462. /**
  1463. * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
  1464. */
  1465. static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
  1466. struct dma_fence *fence)
  1467. {
  1468. struct amdgpu_prt_cb *cb;
  1469. if (!adev->gart.gart_funcs->set_prt)
  1470. return;
  1471. cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
  1472. if (!cb) {
  1473. /* Last resort when we are OOM */
  1474. if (fence)
  1475. dma_fence_wait(fence, false);
  1476. amdgpu_vm_prt_put(adev);
  1477. } else {
  1478. cb->adev = adev;
  1479. if (!fence || dma_fence_add_callback(fence, &cb->cb,
  1480. amdgpu_vm_prt_cb))
  1481. amdgpu_vm_prt_cb(fence, &cb->cb);
  1482. }
  1483. }
  1484. /**
  1485. * amdgpu_vm_free_mapping - free a mapping
  1486. *
  1487. * @adev: amdgpu_device pointer
  1488. * @vm: requested vm
  1489. * @mapping: mapping to be freed
  1490. * @fence: fence of the unmap operation
  1491. *
  1492. * Free a mapping and make sure we decrease the PRT usage count if applicable.
  1493. */
  1494. static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
  1495. struct amdgpu_vm *vm,
  1496. struct amdgpu_bo_va_mapping *mapping,
  1497. struct dma_fence *fence)
  1498. {
  1499. if (mapping->flags & AMDGPU_PTE_PRT)
  1500. amdgpu_vm_add_prt_cb(adev, fence);
  1501. kfree(mapping);
  1502. }
  1503. /**
  1504. * amdgpu_vm_prt_fini - finish all prt mappings
  1505. *
  1506. * @adev: amdgpu_device pointer
  1507. * @vm: requested vm
  1508. *
  1509. * Register a cleanup callback to disable PRT support after VM dies.
  1510. */
  1511. static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1512. {
  1513. struct reservation_object *resv = vm->root.bo->tbo.resv;
  1514. struct dma_fence *excl, **shared;
  1515. unsigned i, shared_count;
  1516. int r;
  1517. r = reservation_object_get_fences_rcu(resv, &excl,
  1518. &shared_count, &shared);
  1519. if (r) {
  1520. /* Not enough memory to grab the fence list, as last resort
  1521. * block for all the fences to complete.
  1522. */
  1523. reservation_object_wait_timeout_rcu(resv, true, false,
  1524. MAX_SCHEDULE_TIMEOUT);
  1525. return;
  1526. }
  1527. /* Add a callback for each fence in the reservation object */
  1528. amdgpu_vm_prt_get(adev);
  1529. amdgpu_vm_add_prt_cb(adev, excl);
  1530. for (i = 0; i < shared_count; ++i) {
  1531. amdgpu_vm_prt_get(adev);
  1532. amdgpu_vm_add_prt_cb(adev, shared[i]);
  1533. }
  1534. kfree(shared);
  1535. }
  1536. /**
  1537. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1538. *
  1539. * @adev: amdgpu_device pointer
  1540. * @vm: requested vm
  1541. * @fence: optional resulting fence (unchanged if no work needed to be done
  1542. * or if an error occurred)
  1543. *
  1544. * Make sure all freed BOs are cleared in the PT.
  1545. * Returns 0 for success.
  1546. *
  1547. * PTs have to be reserved and mutex must be locked!
  1548. */
  1549. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1550. struct amdgpu_vm *vm,
  1551. struct dma_fence **fence)
  1552. {
  1553. struct amdgpu_bo_va_mapping *mapping;
  1554. struct dma_fence *f = NULL;
  1555. int r;
  1556. while (!list_empty(&vm->freed)) {
  1557. mapping = list_first_entry(&vm->freed,
  1558. struct amdgpu_bo_va_mapping, list);
  1559. list_del(&mapping->list);
  1560. r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, NULL, vm,
  1561. mapping->start, mapping->last,
  1562. 0, 0, &f);
  1563. amdgpu_vm_free_mapping(adev, vm, mapping, f);
  1564. if (r) {
  1565. dma_fence_put(f);
  1566. return r;
  1567. }
  1568. }
  1569. if (fence && f) {
  1570. dma_fence_put(*fence);
  1571. *fence = f;
  1572. } else {
  1573. dma_fence_put(f);
  1574. }
  1575. return 0;
  1576. }
  1577. /**
  1578. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  1579. *
  1580. * @adev: amdgpu_device pointer
  1581. * @vm: requested vm
  1582. *
  1583. * Make sure all invalidated BOs are cleared in the PT.
  1584. * Returns 0 for success.
  1585. *
  1586. * PTs have to be reserved and mutex must be locked!
  1587. */
  1588. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  1589. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  1590. {
  1591. struct amdgpu_bo_va *bo_va = NULL;
  1592. int r = 0;
  1593. spin_lock(&vm->status_lock);
  1594. while (!list_empty(&vm->invalidated)) {
  1595. bo_va = list_first_entry(&vm->invalidated,
  1596. struct amdgpu_bo_va, vm_status);
  1597. spin_unlock(&vm->status_lock);
  1598. r = amdgpu_vm_bo_update(adev, bo_va, true);
  1599. if (r)
  1600. return r;
  1601. spin_lock(&vm->status_lock);
  1602. }
  1603. spin_unlock(&vm->status_lock);
  1604. if (bo_va)
  1605. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  1606. return r;
  1607. }
  1608. /**
  1609. * amdgpu_vm_bo_add - add a bo to a specific vm
  1610. *
  1611. * @adev: amdgpu_device pointer
  1612. * @vm: requested vm
  1613. * @bo: amdgpu buffer object
  1614. *
  1615. * Add @bo into the requested vm.
  1616. * Add @bo to the list of bos associated with the vm
  1617. * Returns newly added bo_va or NULL for failure
  1618. *
  1619. * Object has to be reserved!
  1620. */
  1621. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1622. struct amdgpu_vm *vm,
  1623. struct amdgpu_bo *bo)
  1624. {
  1625. struct amdgpu_bo_va *bo_va;
  1626. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1627. if (bo_va == NULL) {
  1628. return NULL;
  1629. }
  1630. bo_va->vm = vm;
  1631. bo_va->bo = bo;
  1632. bo_va->ref_count = 1;
  1633. INIT_LIST_HEAD(&bo_va->bo_list);
  1634. INIT_LIST_HEAD(&bo_va->valids);
  1635. INIT_LIST_HEAD(&bo_va->invalids);
  1636. INIT_LIST_HEAD(&bo_va->vm_status);
  1637. if (bo)
  1638. list_add_tail(&bo_va->bo_list, &bo->va);
  1639. return bo_va;
  1640. }
  1641. /**
  1642. * amdgpu_vm_bo_map - map bo inside a vm
  1643. *
  1644. * @adev: amdgpu_device pointer
  1645. * @bo_va: bo_va to store the address
  1646. * @saddr: where to map the BO
  1647. * @offset: requested offset in the BO
  1648. * @flags: attributes of pages (read/write/valid/etc.)
  1649. *
  1650. * Add a mapping of the BO at the specefied addr into the VM.
  1651. * Returns 0 for success, error for failure.
  1652. *
  1653. * Object has to be reserved and unreserved outside!
  1654. */
  1655. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1656. struct amdgpu_bo_va *bo_va,
  1657. uint64_t saddr, uint64_t offset,
  1658. uint64_t size, uint64_t flags)
  1659. {
  1660. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1661. struct amdgpu_vm *vm = bo_va->vm;
  1662. uint64_t eaddr;
  1663. /* validate the parameters */
  1664. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1665. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1666. return -EINVAL;
  1667. /* make sure object fit at this offset */
  1668. eaddr = saddr + size - 1;
  1669. if (saddr >= eaddr ||
  1670. (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
  1671. return -EINVAL;
  1672. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1673. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1674. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1675. if (tmp) {
  1676. /* bo and tmp overlap, invalid addr */
  1677. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1678. "0x%010Lx-0x%010Lx\n", bo_va->bo, saddr, eaddr,
  1679. tmp->start, tmp->last + 1);
  1680. return -EINVAL;
  1681. }
  1682. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1683. if (!mapping)
  1684. return -ENOMEM;
  1685. INIT_LIST_HEAD(&mapping->list);
  1686. mapping->start = saddr;
  1687. mapping->last = eaddr;
  1688. mapping->offset = offset;
  1689. mapping->flags = flags;
  1690. list_add(&mapping->list, &bo_va->invalids);
  1691. amdgpu_vm_it_insert(mapping, &vm->va);
  1692. if (flags & AMDGPU_PTE_PRT)
  1693. amdgpu_vm_prt_get(adev);
  1694. return 0;
  1695. }
  1696. /**
  1697. * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
  1698. *
  1699. * @adev: amdgpu_device pointer
  1700. * @bo_va: bo_va to store the address
  1701. * @saddr: where to map the BO
  1702. * @offset: requested offset in the BO
  1703. * @flags: attributes of pages (read/write/valid/etc.)
  1704. *
  1705. * Add a mapping of the BO at the specefied addr into the VM. Replace existing
  1706. * mappings as we do so.
  1707. * Returns 0 for success, error for failure.
  1708. *
  1709. * Object has to be reserved and unreserved outside!
  1710. */
  1711. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  1712. struct amdgpu_bo_va *bo_va,
  1713. uint64_t saddr, uint64_t offset,
  1714. uint64_t size, uint64_t flags)
  1715. {
  1716. struct amdgpu_bo_va_mapping *mapping;
  1717. struct amdgpu_vm *vm = bo_va->vm;
  1718. uint64_t eaddr;
  1719. int r;
  1720. /* validate the parameters */
  1721. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1722. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1723. return -EINVAL;
  1724. /* make sure object fit at this offset */
  1725. eaddr = saddr + size - 1;
  1726. if (saddr >= eaddr ||
  1727. (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
  1728. return -EINVAL;
  1729. /* Allocate all the needed memory */
  1730. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1731. if (!mapping)
  1732. return -ENOMEM;
  1733. r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
  1734. if (r) {
  1735. kfree(mapping);
  1736. return r;
  1737. }
  1738. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1739. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1740. mapping->start = saddr;
  1741. mapping->last = eaddr;
  1742. mapping->offset = offset;
  1743. mapping->flags = flags;
  1744. list_add(&mapping->list, &bo_va->invalids);
  1745. amdgpu_vm_it_insert(mapping, &vm->va);
  1746. if (flags & AMDGPU_PTE_PRT)
  1747. amdgpu_vm_prt_get(adev);
  1748. return 0;
  1749. }
  1750. /**
  1751. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1752. *
  1753. * @adev: amdgpu_device pointer
  1754. * @bo_va: bo_va to remove the address from
  1755. * @saddr: where to the BO is mapped
  1756. *
  1757. * Remove a mapping of the BO at the specefied addr from the VM.
  1758. * Returns 0 for success, error for failure.
  1759. *
  1760. * Object has to be reserved and unreserved outside!
  1761. */
  1762. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1763. struct amdgpu_bo_va *bo_va,
  1764. uint64_t saddr)
  1765. {
  1766. struct amdgpu_bo_va_mapping *mapping;
  1767. struct amdgpu_vm *vm = bo_va->vm;
  1768. bool valid = true;
  1769. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1770. list_for_each_entry(mapping, &bo_va->valids, list) {
  1771. if (mapping->start == saddr)
  1772. break;
  1773. }
  1774. if (&mapping->list == &bo_va->valids) {
  1775. valid = false;
  1776. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1777. if (mapping->start == saddr)
  1778. break;
  1779. }
  1780. if (&mapping->list == &bo_va->invalids)
  1781. return -ENOENT;
  1782. }
  1783. list_del(&mapping->list);
  1784. amdgpu_vm_it_remove(mapping, &vm->va);
  1785. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1786. if (valid)
  1787. list_add(&mapping->list, &vm->freed);
  1788. else
  1789. amdgpu_vm_free_mapping(adev, vm, mapping,
  1790. bo_va->last_pt_update);
  1791. return 0;
  1792. }
  1793. /**
  1794. * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
  1795. *
  1796. * @adev: amdgpu_device pointer
  1797. * @vm: VM structure to use
  1798. * @saddr: start of the range
  1799. * @size: size of the range
  1800. *
  1801. * Remove all mappings in a range, split them as appropriate.
  1802. * Returns 0 for success, error for failure.
  1803. */
  1804. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  1805. struct amdgpu_vm *vm,
  1806. uint64_t saddr, uint64_t size)
  1807. {
  1808. struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
  1809. LIST_HEAD(removed);
  1810. uint64_t eaddr;
  1811. eaddr = saddr + size - 1;
  1812. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1813. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1814. /* Allocate all the needed memory */
  1815. before = kzalloc(sizeof(*before), GFP_KERNEL);
  1816. if (!before)
  1817. return -ENOMEM;
  1818. INIT_LIST_HEAD(&before->list);
  1819. after = kzalloc(sizeof(*after), GFP_KERNEL);
  1820. if (!after) {
  1821. kfree(before);
  1822. return -ENOMEM;
  1823. }
  1824. INIT_LIST_HEAD(&after->list);
  1825. /* Now gather all removed mappings */
  1826. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1827. while (tmp) {
  1828. /* Remember mapping split at the start */
  1829. if (tmp->start < saddr) {
  1830. before->start = tmp->start;
  1831. before->last = saddr - 1;
  1832. before->offset = tmp->offset;
  1833. before->flags = tmp->flags;
  1834. list_add(&before->list, &tmp->list);
  1835. }
  1836. /* Remember mapping split at the end */
  1837. if (tmp->last > eaddr) {
  1838. after->start = eaddr + 1;
  1839. after->last = tmp->last;
  1840. after->offset = tmp->offset;
  1841. after->offset += after->start - tmp->start;
  1842. after->flags = tmp->flags;
  1843. list_add(&after->list, &tmp->list);
  1844. }
  1845. list_del(&tmp->list);
  1846. list_add(&tmp->list, &removed);
  1847. tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
  1848. }
  1849. /* And free them up */
  1850. list_for_each_entry_safe(tmp, next, &removed, list) {
  1851. amdgpu_vm_it_remove(tmp, &vm->va);
  1852. list_del(&tmp->list);
  1853. if (tmp->start < saddr)
  1854. tmp->start = saddr;
  1855. if (tmp->last > eaddr)
  1856. tmp->last = eaddr;
  1857. list_add(&tmp->list, &vm->freed);
  1858. trace_amdgpu_vm_bo_unmap(NULL, tmp);
  1859. }
  1860. /* Insert partial mapping before the range */
  1861. if (!list_empty(&before->list)) {
  1862. amdgpu_vm_it_insert(before, &vm->va);
  1863. if (before->flags & AMDGPU_PTE_PRT)
  1864. amdgpu_vm_prt_get(adev);
  1865. } else {
  1866. kfree(before);
  1867. }
  1868. /* Insert partial mapping after the range */
  1869. if (!list_empty(&after->list)) {
  1870. amdgpu_vm_it_insert(after, &vm->va);
  1871. if (after->flags & AMDGPU_PTE_PRT)
  1872. amdgpu_vm_prt_get(adev);
  1873. } else {
  1874. kfree(after);
  1875. }
  1876. return 0;
  1877. }
  1878. /**
  1879. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1880. *
  1881. * @adev: amdgpu_device pointer
  1882. * @bo_va: requested bo_va
  1883. *
  1884. * Remove @bo_va->bo from the requested vm.
  1885. *
  1886. * Object have to be reserved!
  1887. */
  1888. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1889. struct amdgpu_bo_va *bo_va)
  1890. {
  1891. struct amdgpu_bo_va_mapping *mapping, *next;
  1892. struct amdgpu_vm *vm = bo_va->vm;
  1893. list_del(&bo_va->bo_list);
  1894. spin_lock(&vm->status_lock);
  1895. list_del(&bo_va->vm_status);
  1896. spin_unlock(&vm->status_lock);
  1897. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1898. list_del(&mapping->list);
  1899. amdgpu_vm_it_remove(mapping, &vm->va);
  1900. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1901. list_add(&mapping->list, &vm->freed);
  1902. }
  1903. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1904. list_del(&mapping->list);
  1905. amdgpu_vm_it_remove(mapping, &vm->va);
  1906. amdgpu_vm_free_mapping(adev, vm, mapping,
  1907. bo_va->last_pt_update);
  1908. }
  1909. dma_fence_put(bo_va->last_pt_update);
  1910. kfree(bo_va);
  1911. }
  1912. /**
  1913. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1914. *
  1915. * @adev: amdgpu_device pointer
  1916. * @vm: requested vm
  1917. * @bo: amdgpu buffer object
  1918. *
  1919. * Mark @bo as invalid.
  1920. */
  1921. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1922. struct amdgpu_bo *bo)
  1923. {
  1924. struct amdgpu_bo_va *bo_va;
  1925. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1926. spin_lock(&bo_va->vm->status_lock);
  1927. if (list_empty(&bo_va->vm_status))
  1928. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1929. spin_unlock(&bo_va->vm->status_lock);
  1930. }
  1931. }
  1932. static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
  1933. {
  1934. /* Total bits covered by PD + PTs */
  1935. unsigned bits = ilog2(vm_size) + 18;
  1936. /* Make sure the PD is 4K in size up to 8GB address space.
  1937. Above that split equal between PD and PTs */
  1938. if (vm_size <= 8)
  1939. return (bits - 9);
  1940. else
  1941. return ((bits + 3) / 2);
  1942. }
  1943. /**
  1944. * amdgpu_vm_adjust_size - adjust vm size and block size
  1945. *
  1946. * @adev: amdgpu_device pointer
  1947. * @vm_size: the default vm size if it's set auto
  1948. */
  1949. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size)
  1950. {
  1951. /* adjust vm size firstly */
  1952. if (amdgpu_vm_size == -1)
  1953. adev->vm_manager.vm_size = vm_size;
  1954. else
  1955. adev->vm_manager.vm_size = amdgpu_vm_size;
  1956. /* block size depends on vm size */
  1957. if (amdgpu_vm_block_size == -1)
  1958. adev->vm_manager.block_size =
  1959. amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
  1960. else
  1961. adev->vm_manager.block_size = amdgpu_vm_block_size;
  1962. DRM_INFO("vm size is %llu GB, block size is %u-bit\n",
  1963. adev->vm_manager.vm_size, adev->vm_manager.block_size);
  1964. }
  1965. /**
  1966. * amdgpu_vm_init - initialize a vm instance
  1967. *
  1968. * @adev: amdgpu_device pointer
  1969. * @vm: requested vm
  1970. *
  1971. * Init @vm fields.
  1972. */
  1973. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1974. {
  1975. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1976. AMDGPU_VM_PTE_COUNT(adev) * 8);
  1977. unsigned ring_instance;
  1978. struct amdgpu_ring *ring;
  1979. struct amd_sched_rq *rq;
  1980. int r, i;
  1981. vm->va = RB_ROOT;
  1982. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  1983. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  1984. vm->reserved_vmid[i] = NULL;
  1985. spin_lock_init(&vm->status_lock);
  1986. INIT_LIST_HEAD(&vm->invalidated);
  1987. INIT_LIST_HEAD(&vm->cleared);
  1988. INIT_LIST_HEAD(&vm->freed);
  1989. /* create scheduler entity for page table updates */
  1990. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  1991. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  1992. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  1993. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
  1994. r = amd_sched_entity_init(&ring->sched, &vm->entity,
  1995. rq, amdgpu_sched_jobs);
  1996. if (r)
  1997. return r;
  1998. vm->last_dir_update = NULL;
  1999. r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
  2000. AMDGPU_GEM_DOMAIN_VRAM,
  2001. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  2002. AMDGPU_GEM_CREATE_SHADOW |
  2003. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  2004. AMDGPU_GEM_CREATE_VRAM_CLEARED,
  2005. NULL, NULL, &vm->root.bo);
  2006. if (r)
  2007. goto error_free_sched_entity;
  2008. r = amdgpu_bo_reserve(vm->root.bo, false);
  2009. if (r)
  2010. goto error_free_root;
  2011. vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
  2012. amdgpu_bo_unreserve(vm->root.bo);
  2013. return 0;
  2014. error_free_root:
  2015. amdgpu_bo_unref(&vm->root.bo->shadow);
  2016. amdgpu_bo_unref(&vm->root.bo);
  2017. vm->root.bo = NULL;
  2018. error_free_sched_entity:
  2019. amd_sched_entity_fini(&ring->sched, &vm->entity);
  2020. return r;
  2021. }
  2022. /**
  2023. * amdgpu_vm_free_levels - free PD/PT levels
  2024. *
  2025. * @level: PD/PT starting level to free
  2026. *
  2027. * Free the page directory or page table level and all sub levels.
  2028. */
  2029. static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
  2030. {
  2031. unsigned i;
  2032. if (level->bo) {
  2033. amdgpu_bo_unref(&level->bo->shadow);
  2034. amdgpu_bo_unref(&level->bo);
  2035. }
  2036. if (level->entries)
  2037. for (i = 0; i <= level->last_entry_used; i++)
  2038. amdgpu_vm_free_levels(&level->entries[i]);
  2039. drm_free_large(level->entries);
  2040. }
  2041. /**
  2042. * amdgpu_vm_fini - tear down a vm instance
  2043. *
  2044. * @adev: amdgpu_device pointer
  2045. * @vm: requested vm
  2046. *
  2047. * Tear down @vm.
  2048. * Unbind the VM and remove all bos from the vm bo list
  2049. */
  2050. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  2051. {
  2052. struct amdgpu_bo_va_mapping *mapping, *tmp;
  2053. bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
  2054. int i;
  2055. amd_sched_entity_fini(vm->entity.sched, &vm->entity);
  2056. if (!RB_EMPTY_ROOT(&vm->va)) {
  2057. dev_err(adev->dev, "still active bo inside vm\n");
  2058. }
  2059. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
  2060. list_del(&mapping->list);
  2061. amdgpu_vm_it_remove(mapping, &vm->va);
  2062. kfree(mapping);
  2063. }
  2064. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  2065. if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
  2066. amdgpu_vm_prt_fini(adev, vm);
  2067. prt_fini_needed = false;
  2068. }
  2069. list_del(&mapping->list);
  2070. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  2071. }
  2072. amdgpu_vm_free_levels(&vm->root);
  2073. dma_fence_put(vm->last_dir_update);
  2074. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2075. amdgpu_vm_free_reserved_vmid(adev, vm, i);
  2076. }
  2077. /**
  2078. * amdgpu_vm_manager_init - init the VM manager
  2079. *
  2080. * @adev: amdgpu_device pointer
  2081. *
  2082. * Initialize the VM manager structures
  2083. */
  2084. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  2085. {
  2086. unsigned i, j;
  2087. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  2088. struct amdgpu_vm_id_manager *id_mgr =
  2089. &adev->vm_manager.id_mgr[i];
  2090. mutex_init(&id_mgr->lock);
  2091. INIT_LIST_HEAD(&id_mgr->ids_lru);
  2092. atomic_set(&id_mgr->reserved_vmid_num, 0);
  2093. /* skip over VMID 0, since it is the system VM */
  2094. for (j = 1; j < id_mgr->num_ids; ++j) {
  2095. amdgpu_vm_reset_id(adev, i, j);
  2096. amdgpu_sync_create(&id_mgr->ids[i].active);
  2097. list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
  2098. }
  2099. }
  2100. adev->vm_manager.fence_context =
  2101. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2102. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  2103. adev->vm_manager.seqno[i] = 0;
  2104. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  2105. atomic64_set(&adev->vm_manager.client_counter, 0);
  2106. spin_lock_init(&adev->vm_manager.prt_lock);
  2107. atomic_set(&adev->vm_manager.num_prt_users, 0);
  2108. }
  2109. /**
  2110. * amdgpu_vm_manager_fini - cleanup VM manager
  2111. *
  2112. * @adev: amdgpu_device pointer
  2113. *
  2114. * Cleanup the VM manager and free resources.
  2115. */
  2116. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  2117. {
  2118. unsigned i, j;
  2119. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  2120. struct amdgpu_vm_id_manager *id_mgr =
  2121. &adev->vm_manager.id_mgr[i];
  2122. mutex_destroy(&id_mgr->lock);
  2123. for (j = 0; j < AMDGPU_NUM_VM; ++j) {
  2124. struct amdgpu_vm_id *id = &id_mgr->ids[j];
  2125. amdgpu_sync_free(&id->active);
  2126. dma_fence_put(id->flushed_updates);
  2127. dma_fence_put(id->last_flush);
  2128. }
  2129. }
  2130. }
  2131. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  2132. {
  2133. union drm_amdgpu_vm *args = data;
  2134. struct amdgpu_device *adev = dev->dev_private;
  2135. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  2136. int r;
  2137. switch (args->in.op) {
  2138. case AMDGPU_VM_OP_RESERVE_VMID:
  2139. /* current, we only have requirement to reserve vmid from gfxhub */
  2140. r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
  2141. AMDGPU_GFXHUB);
  2142. if (r)
  2143. return r;
  2144. break;
  2145. case AMDGPU_VM_OP_UNRESERVE_VMID:
  2146. amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2147. break;
  2148. default:
  2149. return -EINVAL;
  2150. }
  2151. return 0;
  2152. }