amdgpu_vm.c 67 KB

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