amdgpu_vm.c 67 KB

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