amdgpu_vm.c 68 KB

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