amdgpu_vm.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637
  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 = vm->use_cpu_for_update;
  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 = vm->use_cpu_for_update;
  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 = vm->use_cpu_for_update;
  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, bool need_pipe_sync)
  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 && !need_pipe_sync)
  675. return 0;
  676. if (ring->funcs->init_cond_exec)
  677. patch_offset = amdgpu_ring_init_cond_exec(ring);
  678. if (need_pipe_sync)
  679. amdgpu_ring_emit_pipeline_sync(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 ((addr & ~mask) == (end & ~mask))
  1129. nptes = end - addr;
  1130. else
  1131. nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
  1132. if (use_cpu_update) {
  1133. r = amdgpu_bo_kmap(pt, (void *)&pe_start);
  1134. if (r)
  1135. return r;
  1136. WARN_ONCE(pt->shadow,
  1137. "CPU VM update doesn't support shadow pages");
  1138. } else {
  1139. if (pt->shadow) {
  1140. pe_start = amdgpu_bo_gpu_offset(pt->shadow);
  1141. pe_start += (addr & mask) * 8;
  1142. params->func(params, pe_start, dst, nptes,
  1143. AMDGPU_GPU_PAGE_SIZE, flags);
  1144. }
  1145. pe_start = amdgpu_bo_gpu_offset(pt);
  1146. }
  1147. pe_start += (addr & mask) * 8;
  1148. params->func(params, pe_start, dst, nptes,
  1149. AMDGPU_GPU_PAGE_SIZE, flags);
  1150. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  1151. if (use_cpu_update)
  1152. amdgpu_bo_kunmap(pt);
  1153. }
  1154. return 0;
  1155. }
  1156. /*
  1157. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  1158. *
  1159. * @params: see amdgpu_pte_update_params definition
  1160. * @vm: requested vm
  1161. * @start: first PTE to handle
  1162. * @end: last PTE to handle
  1163. * @dst: addr those PTEs should point to
  1164. * @flags: hw mapping flags
  1165. * Returns 0 for success, -EINVAL for failure.
  1166. */
  1167. static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  1168. uint64_t start, uint64_t end,
  1169. uint64_t dst, uint64_t flags)
  1170. {
  1171. int r;
  1172. /**
  1173. * The MC L1 TLB supports variable sized pages, based on a fragment
  1174. * field in the PTE. When this field is set to a non-zero value, page
  1175. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  1176. * flags are considered valid for all PTEs within the fragment range
  1177. * and corresponding mappings are assumed to be physically contiguous.
  1178. *
  1179. * The L1 TLB can store a single PTE for the whole fragment,
  1180. * significantly increasing the space available for translation
  1181. * caching. This leads to large improvements in throughput when the
  1182. * TLB is under pressure.
  1183. *
  1184. * The L2 TLB distributes small and large fragments into two
  1185. * asymmetric partitions. The large fragment cache is significantly
  1186. * larger. Thus, we try to use large fragments wherever possible.
  1187. * Userspace can support this by aligning virtual base address and
  1188. * allocation size to the fragment size.
  1189. */
  1190. /* SI and newer are optimized for 64KB */
  1191. uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
  1192. uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
  1193. uint64_t frag_start = ALIGN(start, frag_align);
  1194. uint64_t frag_end = end & ~(frag_align - 1);
  1195. /* system pages are non continuously */
  1196. if (params->src || !(flags & AMDGPU_PTE_VALID) ||
  1197. (frag_start >= frag_end))
  1198. return amdgpu_vm_update_ptes(params, start, end, dst, flags);
  1199. /* handle the 4K area at the beginning */
  1200. if (start != frag_start) {
  1201. r = amdgpu_vm_update_ptes(params, start, frag_start,
  1202. dst, flags);
  1203. if (r)
  1204. return r;
  1205. dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
  1206. }
  1207. /* handle the area in the middle */
  1208. r = amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
  1209. flags | frag_flags);
  1210. if (r)
  1211. return r;
  1212. /* handle the 4K area at the end */
  1213. if (frag_end != end) {
  1214. dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
  1215. r = amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
  1216. }
  1217. return r;
  1218. }
  1219. /**
  1220. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  1221. *
  1222. * @adev: amdgpu_device pointer
  1223. * @exclusive: fence we need to sync to
  1224. * @src: address where to copy page table entries from
  1225. * @pages_addr: DMA addresses to use for mapping
  1226. * @vm: requested vm
  1227. * @start: start of mapped range
  1228. * @last: last mapped entry
  1229. * @flags: flags for the entries
  1230. * @addr: addr to set the area to
  1231. * @fence: optional resulting fence
  1232. *
  1233. * Fill in the page table entries between @start and @last.
  1234. * Returns 0 for success, -EINVAL for failure.
  1235. */
  1236. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  1237. struct dma_fence *exclusive,
  1238. uint64_t src,
  1239. dma_addr_t *pages_addr,
  1240. struct amdgpu_vm *vm,
  1241. uint64_t start, uint64_t last,
  1242. uint64_t flags, uint64_t addr,
  1243. struct dma_fence **fence)
  1244. {
  1245. struct amdgpu_ring *ring;
  1246. void *owner = AMDGPU_FENCE_OWNER_VM;
  1247. unsigned nptes, ncmds, ndw;
  1248. struct amdgpu_job *job;
  1249. struct amdgpu_pte_update_params params;
  1250. struct dma_fence *f = NULL;
  1251. int r;
  1252. memset(&params, 0, sizeof(params));
  1253. params.adev = adev;
  1254. params.vm = vm;
  1255. params.src = src;
  1256. if (vm->use_cpu_for_update) {
  1257. /* params.src is used as flag to indicate system Memory */
  1258. if (pages_addr)
  1259. params.src = ~0;
  1260. /* Wait for PT BOs to be free. PTs share the same resv. object
  1261. * as the root PD BO
  1262. */
  1263. r = amdgpu_vm_bo_wait(adev, vm->root.bo);
  1264. if (unlikely(r))
  1265. return r;
  1266. params.func = amdgpu_vm_cpu_set_ptes;
  1267. params.pages_addr = pages_addr;
  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. r = amdgpu_vm_frag_ptes(&params, start, last + 1, addr, flags);
  1329. if (r)
  1330. goto error_free;
  1331. amdgpu_ring_pad_ib(ring, params.ib);
  1332. WARN_ON(params.ib->length_dw > ndw);
  1333. r = amdgpu_job_submit(job, ring, &vm->entity,
  1334. AMDGPU_FENCE_OWNER_VM, &f);
  1335. if (r)
  1336. goto error_free;
  1337. amdgpu_bo_fence(vm->root.bo, f, true);
  1338. dma_fence_put(*fence);
  1339. *fence = f;
  1340. return 0;
  1341. error_free:
  1342. amdgpu_job_free(job);
  1343. return r;
  1344. }
  1345. /**
  1346. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  1347. *
  1348. * @adev: amdgpu_device pointer
  1349. * @exclusive: fence we need to sync to
  1350. * @gtt_flags: flags as they are used for GTT
  1351. * @pages_addr: DMA addresses to use for mapping
  1352. * @vm: requested vm
  1353. * @mapping: mapped range and flags to use for the update
  1354. * @flags: HW flags for the mapping
  1355. * @nodes: array of drm_mm_nodes with the MC addresses
  1356. * @fence: optional resulting fence
  1357. *
  1358. * Split the mapping into smaller chunks so that each update fits
  1359. * into a SDMA IB.
  1360. * Returns 0 for success, -EINVAL for failure.
  1361. */
  1362. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  1363. struct dma_fence *exclusive,
  1364. uint64_t gtt_flags,
  1365. dma_addr_t *pages_addr,
  1366. struct amdgpu_vm *vm,
  1367. struct amdgpu_bo_va_mapping *mapping,
  1368. uint64_t flags,
  1369. struct drm_mm_node *nodes,
  1370. struct dma_fence **fence)
  1371. {
  1372. uint64_t pfn, src = 0, start = mapping->start;
  1373. int r;
  1374. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  1375. * but in case of something, we filter the flags in first place
  1376. */
  1377. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  1378. flags &= ~AMDGPU_PTE_READABLE;
  1379. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  1380. flags &= ~AMDGPU_PTE_WRITEABLE;
  1381. flags &= ~AMDGPU_PTE_EXECUTABLE;
  1382. flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
  1383. flags &= ~AMDGPU_PTE_MTYPE_MASK;
  1384. flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
  1385. if ((mapping->flags & AMDGPU_PTE_PRT) &&
  1386. (adev->asic_type >= CHIP_VEGA10)) {
  1387. flags |= AMDGPU_PTE_PRT;
  1388. flags &= ~AMDGPU_PTE_VALID;
  1389. }
  1390. trace_amdgpu_vm_bo_update(mapping);
  1391. pfn = mapping->offset >> PAGE_SHIFT;
  1392. if (nodes) {
  1393. while (pfn >= nodes->size) {
  1394. pfn -= nodes->size;
  1395. ++nodes;
  1396. }
  1397. }
  1398. do {
  1399. uint64_t max_entries;
  1400. uint64_t addr, last;
  1401. if (nodes) {
  1402. addr = nodes->start << PAGE_SHIFT;
  1403. max_entries = (nodes->size - pfn) *
  1404. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  1405. } else {
  1406. addr = 0;
  1407. max_entries = S64_MAX;
  1408. }
  1409. if (pages_addr) {
  1410. if (flags == gtt_flags)
  1411. src = adev->gart.table_addr +
  1412. (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
  1413. else
  1414. max_entries = min(max_entries, 16ull * 1024ull);
  1415. addr = 0;
  1416. } else if (flags & AMDGPU_PTE_VALID) {
  1417. addr += adev->vm_manager.vram_base_offset;
  1418. }
  1419. addr += pfn << PAGE_SHIFT;
  1420. last = min((uint64_t)mapping->last, start + max_entries - 1);
  1421. r = amdgpu_vm_bo_update_mapping(adev, exclusive,
  1422. src, pages_addr, vm,
  1423. start, last, flags, addr,
  1424. fence);
  1425. if (r)
  1426. return r;
  1427. pfn += last - start + 1;
  1428. if (nodes && nodes->size == pfn) {
  1429. pfn = 0;
  1430. ++nodes;
  1431. }
  1432. start = last + 1;
  1433. } while (unlikely(start != mapping->last + 1));
  1434. return 0;
  1435. }
  1436. /**
  1437. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  1438. *
  1439. * @adev: amdgpu_device pointer
  1440. * @bo_va: requested BO and VM object
  1441. * @clear: if true clear the entries
  1442. *
  1443. * Fill in the page table entries for @bo_va.
  1444. * Returns 0 for success, -EINVAL for failure.
  1445. */
  1446. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  1447. struct amdgpu_bo_va *bo_va,
  1448. bool clear)
  1449. {
  1450. struct amdgpu_vm *vm = bo_va->vm;
  1451. struct amdgpu_bo_va_mapping *mapping;
  1452. dma_addr_t *pages_addr = NULL;
  1453. uint64_t gtt_flags, flags;
  1454. struct ttm_mem_reg *mem;
  1455. struct drm_mm_node *nodes;
  1456. struct dma_fence *exclusive;
  1457. int r;
  1458. if (clear || !bo_va->bo) {
  1459. mem = NULL;
  1460. nodes = NULL;
  1461. exclusive = NULL;
  1462. } else {
  1463. struct ttm_dma_tt *ttm;
  1464. mem = &bo_va->bo->tbo.mem;
  1465. nodes = mem->mm_node;
  1466. if (mem->mem_type == TTM_PL_TT) {
  1467. ttm = container_of(bo_va->bo->tbo.ttm, struct
  1468. ttm_dma_tt, ttm);
  1469. pages_addr = ttm->dma_address;
  1470. }
  1471. exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
  1472. }
  1473. if (bo_va->bo) {
  1474. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  1475. gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
  1476. adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
  1477. flags : 0;
  1478. } else {
  1479. flags = 0x0;
  1480. gtt_flags = ~0x0;
  1481. }
  1482. spin_lock(&vm->status_lock);
  1483. if (!list_empty(&bo_va->vm_status))
  1484. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1485. spin_unlock(&vm->status_lock);
  1486. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1487. r = amdgpu_vm_bo_split_mapping(adev, exclusive,
  1488. gtt_flags, pages_addr, vm,
  1489. mapping, flags, nodes,
  1490. &bo_va->last_pt_update);
  1491. if (r)
  1492. return r;
  1493. }
  1494. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1495. list_for_each_entry(mapping, &bo_va->valids, list)
  1496. trace_amdgpu_vm_bo_mapping(mapping);
  1497. list_for_each_entry(mapping, &bo_va->invalids, list)
  1498. trace_amdgpu_vm_bo_mapping(mapping);
  1499. }
  1500. spin_lock(&vm->status_lock);
  1501. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1502. list_del_init(&bo_va->vm_status);
  1503. if (clear)
  1504. list_add(&bo_va->vm_status, &vm->cleared);
  1505. spin_unlock(&vm->status_lock);
  1506. return 0;
  1507. }
  1508. /**
  1509. * amdgpu_vm_update_prt_state - update the global PRT state
  1510. */
  1511. static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
  1512. {
  1513. unsigned long flags;
  1514. bool enable;
  1515. spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
  1516. enable = !!atomic_read(&adev->vm_manager.num_prt_users);
  1517. adev->gart.gart_funcs->set_prt(adev, enable);
  1518. spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
  1519. }
  1520. /**
  1521. * amdgpu_vm_prt_get - add a PRT user
  1522. */
  1523. static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
  1524. {
  1525. if (!adev->gart.gart_funcs->set_prt)
  1526. return;
  1527. if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
  1528. amdgpu_vm_update_prt_state(adev);
  1529. }
  1530. /**
  1531. * amdgpu_vm_prt_put - drop a PRT user
  1532. */
  1533. static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
  1534. {
  1535. if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
  1536. amdgpu_vm_update_prt_state(adev);
  1537. }
  1538. /**
  1539. * amdgpu_vm_prt_cb - callback for updating the PRT status
  1540. */
  1541. static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
  1542. {
  1543. struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
  1544. amdgpu_vm_prt_put(cb->adev);
  1545. kfree(cb);
  1546. }
  1547. /**
  1548. * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
  1549. */
  1550. static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
  1551. struct dma_fence *fence)
  1552. {
  1553. struct amdgpu_prt_cb *cb;
  1554. if (!adev->gart.gart_funcs->set_prt)
  1555. return;
  1556. cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
  1557. if (!cb) {
  1558. /* Last resort when we are OOM */
  1559. if (fence)
  1560. dma_fence_wait(fence, false);
  1561. amdgpu_vm_prt_put(adev);
  1562. } else {
  1563. cb->adev = adev;
  1564. if (!fence || dma_fence_add_callback(fence, &cb->cb,
  1565. amdgpu_vm_prt_cb))
  1566. amdgpu_vm_prt_cb(fence, &cb->cb);
  1567. }
  1568. }
  1569. /**
  1570. * amdgpu_vm_free_mapping - free a mapping
  1571. *
  1572. * @adev: amdgpu_device pointer
  1573. * @vm: requested vm
  1574. * @mapping: mapping to be freed
  1575. * @fence: fence of the unmap operation
  1576. *
  1577. * Free a mapping and make sure we decrease the PRT usage count if applicable.
  1578. */
  1579. static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
  1580. struct amdgpu_vm *vm,
  1581. struct amdgpu_bo_va_mapping *mapping,
  1582. struct dma_fence *fence)
  1583. {
  1584. if (mapping->flags & AMDGPU_PTE_PRT)
  1585. amdgpu_vm_add_prt_cb(adev, fence);
  1586. kfree(mapping);
  1587. }
  1588. /**
  1589. * amdgpu_vm_prt_fini - finish all prt mappings
  1590. *
  1591. * @adev: amdgpu_device pointer
  1592. * @vm: requested vm
  1593. *
  1594. * Register a cleanup callback to disable PRT support after VM dies.
  1595. */
  1596. static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1597. {
  1598. struct reservation_object *resv = vm->root.bo->tbo.resv;
  1599. struct dma_fence *excl, **shared;
  1600. unsigned i, shared_count;
  1601. int r;
  1602. r = reservation_object_get_fences_rcu(resv, &excl,
  1603. &shared_count, &shared);
  1604. if (r) {
  1605. /* Not enough memory to grab the fence list, as last resort
  1606. * block for all the fences to complete.
  1607. */
  1608. reservation_object_wait_timeout_rcu(resv, true, false,
  1609. MAX_SCHEDULE_TIMEOUT);
  1610. return;
  1611. }
  1612. /* Add a callback for each fence in the reservation object */
  1613. amdgpu_vm_prt_get(adev);
  1614. amdgpu_vm_add_prt_cb(adev, excl);
  1615. for (i = 0; i < shared_count; ++i) {
  1616. amdgpu_vm_prt_get(adev);
  1617. amdgpu_vm_add_prt_cb(adev, shared[i]);
  1618. }
  1619. kfree(shared);
  1620. }
  1621. /**
  1622. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1623. *
  1624. * @adev: amdgpu_device pointer
  1625. * @vm: requested vm
  1626. * @fence: optional resulting fence (unchanged if no work needed to be done
  1627. * or if an error occurred)
  1628. *
  1629. * Make sure all freed BOs are cleared in the PT.
  1630. * Returns 0 for success.
  1631. *
  1632. * PTs have to be reserved and mutex must be locked!
  1633. */
  1634. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1635. struct amdgpu_vm *vm,
  1636. struct dma_fence **fence)
  1637. {
  1638. struct amdgpu_bo_va_mapping *mapping;
  1639. struct dma_fence *f = NULL;
  1640. int r;
  1641. while (!list_empty(&vm->freed)) {
  1642. mapping = list_first_entry(&vm->freed,
  1643. struct amdgpu_bo_va_mapping, list);
  1644. list_del(&mapping->list);
  1645. r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, NULL, vm,
  1646. mapping->start, mapping->last,
  1647. 0, 0, &f);
  1648. amdgpu_vm_free_mapping(adev, vm, mapping, f);
  1649. if (r) {
  1650. dma_fence_put(f);
  1651. return r;
  1652. }
  1653. }
  1654. if (fence && f) {
  1655. dma_fence_put(*fence);
  1656. *fence = f;
  1657. } else {
  1658. dma_fence_put(f);
  1659. }
  1660. return 0;
  1661. }
  1662. /**
  1663. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  1664. *
  1665. * @adev: amdgpu_device pointer
  1666. * @vm: requested vm
  1667. *
  1668. * Make sure all invalidated BOs are cleared in the PT.
  1669. * Returns 0 for success.
  1670. *
  1671. * PTs have to be reserved and mutex must be locked!
  1672. */
  1673. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  1674. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  1675. {
  1676. struct amdgpu_bo_va *bo_va = NULL;
  1677. int r = 0;
  1678. spin_lock(&vm->status_lock);
  1679. while (!list_empty(&vm->invalidated)) {
  1680. bo_va = list_first_entry(&vm->invalidated,
  1681. struct amdgpu_bo_va, vm_status);
  1682. spin_unlock(&vm->status_lock);
  1683. r = amdgpu_vm_bo_update(adev, bo_va, true);
  1684. if (r)
  1685. return r;
  1686. spin_lock(&vm->status_lock);
  1687. }
  1688. spin_unlock(&vm->status_lock);
  1689. if (bo_va)
  1690. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  1691. return r;
  1692. }
  1693. /**
  1694. * amdgpu_vm_bo_add - add a bo to a specific vm
  1695. *
  1696. * @adev: amdgpu_device pointer
  1697. * @vm: requested vm
  1698. * @bo: amdgpu buffer object
  1699. *
  1700. * Add @bo into the requested vm.
  1701. * Add @bo to the list of bos associated with the vm
  1702. * Returns newly added bo_va or NULL for failure
  1703. *
  1704. * Object has to be reserved!
  1705. */
  1706. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1707. struct amdgpu_vm *vm,
  1708. struct amdgpu_bo *bo)
  1709. {
  1710. struct amdgpu_bo_va *bo_va;
  1711. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1712. if (bo_va == NULL) {
  1713. return NULL;
  1714. }
  1715. bo_va->vm = vm;
  1716. bo_va->bo = bo;
  1717. bo_va->ref_count = 1;
  1718. INIT_LIST_HEAD(&bo_va->bo_list);
  1719. INIT_LIST_HEAD(&bo_va->valids);
  1720. INIT_LIST_HEAD(&bo_va->invalids);
  1721. INIT_LIST_HEAD(&bo_va->vm_status);
  1722. if (bo)
  1723. list_add_tail(&bo_va->bo_list, &bo->va);
  1724. return bo_va;
  1725. }
  1726. /**
  1727. * amdgpu_vm_bo_map - map bo inside a vm
  1728. *
  1729. * @adev: amdgpu_device pointer
  1730. * @bo_va: bo_va to store the address
  1731. * @saddr: where to map the BO
  1732. * @offset: requested offset in the BO
  1733. * @flags: attributes of pages (read/write/valid/etc.)
  1734. *
  1735. * Add a mapping of the BO at the specefied addr into the VM.
  1736. * Returns 0 for success, error for failure.
  1737. *
  1738. * Object has to be reserved and unreserved outside!
  1739. */
  1740. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1741. struct amdgpu_bo_va *bo_va,
  1742. uint64_t saddr, uint64_t offset,
  1743. uint64_t size, uint64_t flags)
  1744. {
  1745. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1746. struct amdgpu_vm *vm = bo_va->vm;
  1747. uint64_t eaddr;
  1748. /* validate the parameters */
  1749. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1750. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1751. return -EINVAL;
  1752. /* make sure object fit at this offset */
  1753. eaddr = saddr + size - 1;
  1754. if (saddr >= eaddr ||
  1755. (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
  1756. return -EINVAL;
  1757. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1758. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1759. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1760. if (tmp) {
  1761. /* bo and tmp overlap, invalid addr */
  1762. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1763. "0x%010Lx-0x%010Lx\n", bo_va->bo, saddr, eaddr,
  1764. tmp->start, tmp->last + 1);
  1765. return -EINVAL;
  1766. }
  1767. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1768. if (!mapping)
  1769. return -ENOMEM;
  1770. INIT_LIST_HEAD(&mapping->list);
  1771. mapping->start = saddr;
  1772. mapping->last = eaddr;
  1773. mapping->offset = offset;
  1774. mapping->flags = flags;
  1775. list_add(&mapping->list, &bo_va->invalids);
  1776. amdgpu_vm_it_insert(mapping, &vm->va);
  1777. if (flags & AMDGPU_PTE_PRT)
  1778. amdgpu_vm_prt_get(adev);
  1779. return 0;
  1780. }
  1781. /**
  1782. * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
  1783. *
  1784. * @adev: amdgpu_device pointer
  1785. * @bo_va: bo_va to store the address
  1786. * @saddr: where to map the BO
  1787. * @offset: requested offset in the BO
  1788. * @flags: attributes of pages (read/write/valid/etc.)
  1789. *
  1790. * Add a mapping of the BO at the specefied addr into the VM. Replace existing
  1791. * mappings as we do so.
  1792. * Returns 0 for success, error for failure.
  1793. *
  1794. * Object has to be reserved and unreserved outside!
  1795. */
  1796. int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
  1797. struct amdgpu_bo_va *bo_va,
  1798. uint64_t saddr, uint64_t offset,
  1799. uint64_t size, uint64_t flags)
  1800. {
  1801. struct amdgpu_bo_va_mapping *mapping;
  1802. struct amdgpu_vm *vm = bo_va->vm;
  1803. uint64_t eaddr;
  1804. int r;
  1805. /* validate the parameters */
  1806. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1807. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1808. return -EINVAL;
  1809. /* make sure object fit at this offset */
  1810. eaddr = saddr + size - 1;
  1811. if (saddr >= eaddr ||
  1812. (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
  1813. return -EINVAL;
  1814. /* Allocate all the needed memory */
  1815. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1816. if (!mapping)
  1817. return -ENOMEM;
  1818. r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
  1819. if (r) {
  1820. kfree(mapping);
  1821. return r;
  1822. }
  1823. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1824. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1825. mapping->start = saddr;
  1826. mapping->last = eaddr;
  1827. mapping->offset = offset;
  1828. mapping->flags = flags;
  1829. list_add(&mapping->list, &bo_va->invalids);
  1830. amdgpu_vm_it_insert(mapping, &vm->va);
  1831. if (flags & AMDGPU_PTE_PRT)
  1832. amdgpu_vm_prt_get(adev);
  1833. return 0;
  1834. }
  1835. /**
  1836. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1837. *
  1838. * @adev: amdgpu_device pointer
  1839. * @bo_va: bo_va to remove the address from
  1840. * @saddr: where to the BO is mapped
  1841. *
  1842. * Remove a mapping of the BO at the specefied addr from the VM.
  1843. * Returns 0 for success, error for failure.
  1844. *
  1845. * Object has to be reserved and unreserved outside!
  1846. */
  1847. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1848. struct amdgpu_bo_va *bo_va,
  1849. uint64_t saddr)
  1850. {
  1851. struct amdgpu_bo_va_mapping *mapping;
  1852. struct amdgpu_vm *vm = bo_va->vm;
  1853. bool valid = true;
  1854. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1855. list_for_each_entry(mapping, &bo_va->valids, list) {
  1856. if (mapping->start == saddr)
  1857. break;
  1858. }
  1859. if (&mapping->list == &bo_va->valids) {
  1860. valid = false;
  1861. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1862. if (mapping->start == saddr)
  1863. break;
  1864. }
  1865. if (&mapping->list == &bo_va->invalids)
  1866. return -ENOENT;
  1867. }
  1868. list_del(&mapping->list);
  1869. amdgpu_vm_it_remove(mapping, &vm->va);
  1870. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1871. if (valid)
  1872. list_add(&mapping->list, &vm->freed);
  1873. else
  1874. amdgpu_vm_free_mapping(adev, vm, mapping,
  1875. bo_va->last_pt_update);
  1876. return 0;
  1877. }
  1878. /**
  1879. * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
  1880. *
  1881. * @adev: amdgpu_device pointer
  1882. * @vm: VM structure to use
  1883. * @saddr: start of the range
  1884. * @size: size of the range
  1885. *
  1886. * Remove all mappings in a range, split them as appropriate.
  1887. * Returns 0 for success, error for failure.
  1888. */
  1889. int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
  1890. struct amdgpu_vm *vm,
  1891. uint64_t saddr, uint64_t size)
  1892. {
  1893. struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
  1894. LIST_HEAD(removed);
  1895. uint64_t eaddr;
  1896. eaddr = saddr + size - 1;
  1897. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1898. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1899. /* Allocate all the needed memory */
  1900. before = kzalloc(sizeof(*before), GFP_KERNEL);
  1901. if (!before)
  1902. return -ENOMEM;
  1903. INIT_LIST_HEAD(&before->list);
  1904. after = kzalloc(sizeof(*after), GFP_KERNEL);
  1905. if (!after) {
  1906. kfree(before);
  1907. return -ENOMEM;
  1908. }
  1909. INIT_LIST_HEAD(&after->list);
  1910. /* Now gather all removed mappings */
  1911. tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
  1912. while (tmp) {
  1913. /* Remember mapping split at the start */
  1914. if (tmp->start < saddr) {
  1915. before->start = tmp->start;
  1916. before->last = saddr - 1;
  1917. before->offset = tmp->offset;
  1918. before->flags = tmp->flags;
  1919. list_add(&before->list, &tmp->list);
  1920. }
  1921. /* Remember mapping split at the end */
  1922. if (tmp->last > eaddr) {
  1923. after->start = eaddr + 1;
  1924. after->last = tmp->last;
  1925. after->offset = tmp->offset;
  1926. after->offset += after->start - tmp->start;
  1927. after->flags = tmp->flags;
  1928. list_add(&after->list, &tmp->list);
  1929. }
  1930. list_del(&tmp->list);
  1931. list_add(&tmp->list, &removed);
  1932. tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
  1933. }
  1934. /* And free them up */
  1935. list_for_each_entry_safe(tmp, next, &removed, list) {
  1936. amdgpu_vm_it_remove(tmp, &vm->va);
  1937. list_del(&tmp->list);
  1938. if (tmp->start < saddr)
  1939. tmp->start = saddr;
  1940. if (tmp->last > eaddr)
  1941. tmp->last = eaddr;
  1942. list_add(&tmp->list, &vm->freed);
  1943. trace_amdgpu_vm_bo_unmap(NULL, tmp);
  1944. }
  1945. /* Insert partial mapping before the range */
  1946. if (!list_empty(&before->list)) {
  1947. amdgpu_vm_it_insert(before, &vm->va);
  1948. if (before->flags & AMDGPU_PTE_PRT)
  1949. amdgpu_vm_prt_get(adev);
  1950. } else {
  1951. kfree(before);
  1952. }
  1953. /* Insert partial mapping after the range */
  1954. if (!list_empty(&after->list)) {
  1955. amdgpu_vm_it_insert(after, &vm->va);
  1956. if (after->flags & AMDGPU_PTE_PRT)
  1957. amdgpu_vm_prt_get(adev);
  1958. } else {
  1959. kfree(after);
  1960. }
  1961. return 0;
  1962. }
  1963. /**
  1964. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1965. *
  1966. * @adev: amdgpu_device pointer
  1967. * @bo_va: requested bo_va
  1968. *
  1969. * Remove @bo_va->bo from the requested vm.
  1970. *
  1971. * Object have to be reserved!
  1972. */
  1973. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1974. struct amdgpu_bo_va *bo_va)
  1975. {
  1976. struct amdgpu_bo_va_mapping *mapping, *next;
  1977. struct amdgpu_vm *vm = bo_va->vm;
  1978. list_del(&bo_va->bo_list);
  1979. spin_lock(&vm->status_lock);
  1980. list_del(&bo_va->vm_status);
  1981. spin_unlock(&vm->status_lock);
  1982. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1983. list_del(&mapping->list);
  1984. amdgpu_vm_it_remove(mapping, &vm->va);
  1985. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1986. list_add(&mapping->list, &vm->freed);
  1987. }
  1988. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1989. list_del(&mapping->list);
  1990. amdgpu_vm_it_remove(mapping, &vm->va);
  1991. amdgpu_vm_free_mapping(adev, vm, mapping,
  1992. bo_va->last_pt_update);
  1993. }
  1994. dma_fence_put(bo_va->last_pt_update);
  1995. kfree(bo_va);
  1996. }
  1997. /**
  1998. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1999. *
  2000. * @adev: amdgpu_device pointer
  2001. * @vm: requested vm
  2002. * @bo: amdgpu buffer object
  2003. *
  2004. * Mark @bo as invalid.
  2005. */
  2006. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  2007. struct amdgpu_bo *bo)
  2008. {
  2009. struct amdgpu_bo_va *bo_va;
  2010. list_for_each_entry(bo_va, &bo->va, bo_list) {
  2011. spin_lock(&bo_va->vm->status_lock);
  2012. if (list_empty(&bo_va->vm_status))
  2013. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  2014. spin_unlock(&bo_va->vm->status_lock);
  2015. }
  2016. }
  2017. static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
  2018. {
  2019. /* Total bits covered by PD + PTs */
  2020. unsigned bits = ilog2(vm_size) + 18;
  2021. /* Make sure the PD is 4K in size up to 8GB address space.
  2022. Above that split equal between PD and PTs */
  2023. if (vm_size <= 8)
  2024. return (bits - 9);
  2025. else
  2026. return ((bits + 3) / 2);
  2027. }
  2028. /**
  2029. * amdgpu_vm_adjust_size - adjust vm size and block size
  2030. *
  2031. * @adev: amdgpu_device pointer
  2032. * @vm_size: the default vm size if it's set auto
  2033. */
  2034. void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size)
  2035. {
  2036. /* adjust vm size firstly */
  2037. if (amdgpu_vm_size == -1)
  2038. adev->vm_manager.vm_size = vm_size;
  2039. else
  2040. adev->vm_manager.vm_size = amdgpu_vm_size;
  2041. /* block size depends on vm size */
  2042. if (amdgpu_vm_block_size == -1)
  2043. adev->vm_manager.block_size =
  2044. amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
  2045. else
  2046. adev->vm_manager.block_size = amdgpu_vm_block_size;
  2047. DRM_INFO("vm size is %llu GB, block size is %u-bit\n",
  2048. adev->vm_manager.vm_size, adev->vm_manager.block_size);
  2049. }
  2050. /**
  2051. * amdgpu_vm_init - initialize a vm instance
  2052. *
  2053. * @adev: amdgpu_device pointer
  2054. * @vm: requested vm
  2055. * @vm_context: Indicates if it GFX or Compute context
  2056. *
  2057. * Init @vm fields.
  2058. */
  2059. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  2060. int vm_context)
  2061. {
  2062. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  2063. AMDGPU_VM_PTE_COUNT(adev) * 8);
  2064. unsigned ring_instance;
  2065. struct amdgpu_ring *ring;
  2066. struct amd_sched_rq *rq;
  2067. int r, i;
  2068. u64 flags;
  2069. vm->va = RB_ROOT;
  2070. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  2071. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2072. vm->reserved_vmid[i] = NULL;
  2073. spin_lock_init(&vm->status_lock);
  2074. INIT_LIST_HEAD(&vm->invalidated);
  2075. INIT_LIST_HEAD(&vm->cleared);
  2076. INIT_LIST_HEAD(&vm->freed);
  2077. /* create scheduler entity for page table updates */
  2078. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  2079. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  2080. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  2081. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
  2082. r = amd_sched_entity_init(&ring->sched, &vm->entity,
  2083. rq, amdgpu_sched_jobs);
  2084. if (r)
  2085. return r;
  2086. if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE)
  2087. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2088. AMDGPU_VM_USE_CPU_FOR_COMPUTE);
  2089. else
  2090. vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
  2091. AMDGPU_VM_USE_CPU_FOR_GFX);
  2092. DRM_DEBUG_DRIVER("VM update mode is %s\n",
  2093. vm->use_cpu_for_update ? "CPU" : "SDMA");
  2094. WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
  2095. "CPU update of VM recommended only for large BAR system\n");
  2096. vm->last_dir_update = NULL;
  2097. flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  2098. AMDGPU_GEM_CREATE_VRAM_CLEARED;
  2099. if (vm->use_cpu_for_update)
  2100. flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
  2101. else
  2102. flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  2103. AMDGPU_GEM_CREATE_SHADOW);
  2104. r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
  2105. AMDGPU_GEM_DOMAIN_VRAM,
  2106. flags,
  2107. NULL, NULL, &vm->root.bo);
  2108. if (r)
  2109. goto error_free_sched_entity;
  2110. r = amdgpu_bo_reserve(vm->root.bo, false);
  2111. if (r)
  2112. goto error_free_root;
  2113. vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
  2114. amdgpu_bo_unreserve(vm->root.bo);
  2115. return 0;
  2116. error_free_root:
  2117. amdgpu_bo_unref(&vm->root.bo->shadow);
  2118. amdgpu_bo_unref(&vm->root.bo);
  2119. vm->root.bo = NULL;
  2120. error_free_sched_entity:
  2121. amd_sched_entity_fini(&ring->sched, &vm->entity);
  2122. return r;
  2123. }
  2124. /**
  2125. * amdgpu_vm_free_levels - free PD/PT levels
  2126. *
  2127. * @level: PD/PT starting level to free
  2128. *
  2129. * Free the page directory or page table level and all sub levels.
  2130. */
  2131. static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
  2132. {
  2133. unsigned i;
  2134. if (level->bo) {
  2135. amdgpu_bo_unref(&level->bo->shadow);
  2136. amdgpu_bo_unref(&level->bo);
  2137. }
  2138. if (level->entries)
  2139. for (i = 0; i <= level->last_entry_used; i++)
  2140. amdgpu_vm_free_levels(&level->entries[i]);
  2141. kvfree(level->entries);
  2142. }
  2143. /**
  2144. * amdgpu_vm_fini - tear down a vm instance
  2145. *
  2146. * @adev: amdgpu_device pointer
  2147. * @vm: requested vm
  2148. *
  2149. * Tear down @vm.
  2150. * Unbind the VM and remove all bos from the vm bo list
  2151. */
  2152. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  2153. {
  2154. struct amdgpu_bo_va_mapping *mapping, *tmp;
  2155. bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
  2156. int i;
  2157. amd_sched_entity_fini(vm->entity.sched, &vm->entity);
  2158. if (!RB_EMPTY_ROOT(&vm->va)) {
  2159. dev_err(adev->dev, "still active bo inside vm\n");
  2160. }
  2161. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
  2162. list_del(&mapping->list);
  2163. amdgpu_vm_it_remove(mapping, &vm->va);
  2164. kfree(mapping);
  2165. }
  2166. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  2167. if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
  2168. amdgpu_vm_prt_fini(adev, vm);
  2169. prt_fini_needed = false;
  2170. }
  2171. list_del(&mapping->list);
  2172. amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
  2173. }
  2174. amdgpu_vm_free_levels(&vm->root);
  2175. dma_fence_put(vm->last_dir_update);
  2176. for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
  2177. amdgpu_vm_free_reserved_vmid(adev, vm, i);
  2178. }
  2179. /**
  2180. * amdgpu_vm_manager_init - init the VM manager
  2181. *
  2182. * @adev: amdgpu_device pointer
  2183. *
  2184. * Initialize the VM manager structures
  2185. */
  2186. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  2187. {
  2188. unsigned i, j;
  2189. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  2190. struct amdgpu_vm_id_manager *id_mgr =
  2191. &adev->vm_manager.id_mgr[i];
  2192. mutex_init(&id_mgr->lock);
  2193. INIT_LIST_HEAD(&id_mgr->ids_lru);
  2194. atomic_set(&id_mgr->reserved_vmid_num, 0);
  2195. /* skip over VMID 0, since it is the system VM */
  2196. for (j = 1; j < id_mgr->num_ids; ++j) {
  2197. amdgpu_vm_reset_id(adev, i, j);
  2198. amdgpu_sync_create(&id_mgr->ids[i].active);
  2199. list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
  2200. }
  2201. }
  2202. adev->vm_manager.fence_context =
  2203. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  2204. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  2205. adev->vm_manager.seqno[i] = 0;
  2206. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  2207. atomic64_set(&adev->vm_manager.client_counter, 0);
  2208. spin_lock_init(&adev->vm_manager.prt_lock);
  2209. atomic_set(&adev->vm_manager.num_prt_users, 0);
  2210. /* If not overridden by the user, by default, only in large BAR systems
  2211. * Compute VM tables will be updated by CPU
  2212. */
  2213. #ifdef CONFIG_X86_64
  2214. if (amdgpu_vm_update_mode == -1) {
  2215. if (amdgpu_vm_is_large_bar(adev))
  2216. adev->vm_manager.vm_update_mode =
  2217. AMDGPU_VM_USE_CPU_FOR_COMPUTE;
  2218. else
  2219. adev->vm_manager.vm_update_mode = 0;
  2220. } else
  2221. adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
  2222. #else
  2223. adev->vm_manager.vm_update_mode = 0;
  2224. #endif
  2225. }
  2226. /**
  2227. * amdgpu_vm_manager_fini - cleanup VM manager
  2228. *
  2229. * @adev: amdgpu_device pointer
  2230. *
  2231. * Cleanup the VM manager and free resources.
  2232. */
  2233. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  2234. {
  2235. unsigned i, j;
  2236. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  2237. struct amdgpu_vm_id_manager *id_mgr =
  2238. &adev->vm_manager.id_mgr[i];
  2239. mutex_destroy(&id_mgr->lock);
  2240. for (j = 0; j < AMDGPU_NUM_VM; ++j) {
  2241. struct amdgpu_vm_id *id = &id_mgr->ids[j];
  2242. amdgpu_sync_free(&id->active);
  2243. dma_fence_put(id->flushed_updates);
  2244. dma_fence_put(id->last_flush);
  2245. }
  2246. }
  2247. }
  2248. int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  2249. {
  2250. union drm_amdgpu_vm *args = data;
  2251. struct amdgpu_device *adev = dev->dev_private;
  2252. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  2253. int r;
  2254. switch (args->in.op) {
  2255. case AMDGPU_VM_OP_RESERVE_VMID:
  2256. /* current, we only have requirement to reserve vmid from gfxhub */
  2257. r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
  2258. AMDGPU_GFXHUB);
  2259. if (r)
  2260. return r;
  2261. break;
  2262. case AMDGPU_VM_OP_UNRESERVE_VMID:
  2263. amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);
  2264. break;
  2265. default:
  2266. return -EINVAL;
  2267. }
  2268. return 0;
  2269. }