amdgpu_vm.c 56 KB

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