amdgpu_vm.c 56 KB

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