amdgpu_vm.c 67 KB

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