amdgpu_vm.c 54 KB

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