amdgpu_vm.c 58 KB

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