amdgpu_vm.c 63 KB

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