amdgpu_vm.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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/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. /* Special value that no flush is necessary */
  53. #define AMDGPU_VM_NO_FLUSH (~0ll)
  54. /* Local structure. Encapsulate some VM table update parameters to reduce
  55. * the number of function parameters
  56. */
  57. struct amdgpu_vm_update_params {
  58. /* address where to copy page table entries from */
  59. uint64_t src;
  60. /* DMA addresses to use for mapping */
  61. dma_addr_t *pages_addr;
  62. /* indirect buffer to fill with commands */
  63. struct amdgpu_ib *ib;
  64. };
  65. /**
  66. * amdgpu_vm_num_pde - return the number of page directory entries
  67. *
  68. * @adev: amdgpu_device pointer
  69. *
  70. * Calculate the number of page directory entries.
  71. */
  72. static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
  73. {
  74. return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
  75. }
  76. /**
  77. * amdgpu_vm_directory_size - returns the size of the page directory in bytes
  78. *
  79. * @adev: amdgpu_device pointer
  80. *
  81. * Calculate the size of the page directory in bytes.
  82. */
  83. static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
  84. {
  85. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
  86. }
  87. /**
  88. * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
  89. *
  90. * @vm: vm providing the BOs
  91. * @validated: head of validation list
  92. * @entry: entry to add
  93. *
  94. * Add the page directory to the list of BOs to
  95. * validate for command submission.
  96. */
  97. void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
  98. struct list_head *validated,
  99. struct amdgpu_bo_list_entry *entry)
  100. {
  101. entry->robj = vm->page_directory;
  102. entry->priority = 0;
  103. entry->tv.bo = &vm->page_directory->tbo;
  104. entry->tv.shared = true;
  105. entry->user_pages = NULL;
  106. list_add(&entry->tv.head, validated);
  107. }
  108. /**
  109. * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
  110. *
  111. * @adev: amdgpu device pointer
  112. * @vm: vm providing the BOs
  113. * @duplicates: head of duplicates list
  114. *
  115. * Add the page directory to the BO duplicates list
  116. * for command submission.
  117. */
  118. void amdgpu_vm_get_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  119. struct list_head *duplicates)
  120. {
  121. uint64_t num_evictions;
  122. unsigned i;
  123. /* We only need to validate the page tables
  124. * if they aren't already valid.
  125. */
  126. num_evictions = atomic64_read(&adev->num_evictions);
  127. if (num_evictions == vm->last_eviction_counter)
  128. return;
  129. /* add the vm page table to the list */
  130. for (i = 0; i <= vm->max_pde_used; ++i) {
  131. struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
  132. if (!entry->robj)
  133. continue;
  134. list_add(&entry->tv.head, duplicates);
  135. }
  136. }
  137. /**
  138. * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
  139. *
  140. * @adev: amdgpu device instance
  141. * @vm: vm providing the BOs
  142. *
  143. * Move the PT BOs to the tail of the LRU.
  144. */
  145. void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
  146. struct amdgpu_vm *vm)
  147. {
  148. struct ttm_bo_global *glob = adev->mman.bdev.glob;
  149. unsigned i;
  150. spin_lock(&glob->lru_lock);
  151. for (i = 0; i <= vm->max_pde_used; ++i) {
  152. struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
  153. if (!entry->robj)
  154. continue;
  155. ttm_bo_move_to_lru_tail(&entry->robj->tbo);
  156. }
  157. spin_unlock(&glob->lru_lock);
  158. }
  159. /**
  160. * amdgpu_vm_grab_id - allocate the next free VMID
  161. *
  162. * @vm: vm to allocate id for
  163. * @ring: ring we want to submit job to
  164. * @sync: sync object where we add dependencies
  165. * @fence: fence protecting ID from reuse
  166. *
  167. * Allocate an id for the vm, adding fences to the sync obj as necessary.
  168. */
  169. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  170. struct amdgpu_sync *sync, struct fence *fence,
  171. unsigned *vm_id, uint64_t *vm_pd_addr)
  172. {
  173. struct amdgpu_device *adev = ring->adev;
  174. struct fence *updates = sync->last_vm_update;
  175. struct amdgpu_vm_id *id, *idle;
  176. struct fence **fences;
  177. unsigned i;
  178. int r = 0;
  179. fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
  180. GFP_KERNEL);
  181. if (!fences)
  182. return -ENOMEM;
  183. mutex_lock(&adev->vm_manager.lock);
  184. /* Check if we have an idle VMID */
  185. i = 0;
  186. list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
  187. fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
  188. if (!fences[i])
  189. break;
  190. ++i;
  191. }
  192. /* If we can't find a idle VMID to use, wait till one becomes available */
  193. if (&idle->list == &adev->vm_manager.ids_lru) {
  194. u64 fence_context = adev->vm_manager.fence_context + ring->idx;
  195. unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
  196. struct fence_array *array;
  197. unsigned j;
  198. for (j = 0; j < i; ++j)
  199. fence_get(fences[j]);
  200. array = fence_array_create(i, fences, fence_context,
  201. seqno, true);
  202. if (!array) {
  203. for (j = 0; j < i; ++j)
  204. fence_put(fences[j]);
  205. kfree(fences);
  206. r = -ENOMEM;
  207. goto error;
  208. }
  209. r = amdgpu_sync_fence(ring->adev, sync, &array->base);
  210. fence_put(&array->base);
  211. if (r)
  212. goto error;
  213. mutex_unlock(&adev->vm_manager.lock);
  214. return 0;
  215. }
  216. kfree(fences);
  217. /* Check if we can use a VMID already assigned to this VM */
  218. i = ring->idx;
  219. do {
  220. struct fence *flushed;
  221. bool same_ring = ring->idx == i;
  222. id = vm->ids[i++];
  223. if (i == AMDGPU_MAX_RINGS)
  224. i = 0;
  225. /* Check all the prerequisites to using this VMID */
  226. if (!id)
  227. continue;
  228. if (id->current_gpu_reset_count != atomic_read(&adev->gpu_reset_counter))
  229. continue;
  230. if (atomic64_read(&id->owner) != vm->client_id)
  231. continue;
  232. if (*vm_pd_addr != id->pd_gpu_addr)
  233. continue;
  234. if (!same_ring &&
  235. (!id->last_flush || !fence_is_signaled(id->last_flush)))
  236. continue;
  237. flushed = id->flushed_updates;
  238. if (updates &&
  239. (!flushed || fence_is_later(updates, flushed)))
  240. continue;
  241. /* Good we can use this VMID. Remember this submission as
  242. * user of the VMID.
  243. */
  244. r = amdgpu_sync_fence(ring->adev, &id->active, fence);
  245. if (r)
  246. goto error;
  247. id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
  248. list_move_tail(&id->list, &adev->vm_manager.ids_lru);
  249. vm->ids[ring->idx] = id;
  250. *vm_id = id - adev->vm_manager.ids;
  251. *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
  252. trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
  253. mutex_unlock(&adev->vm_manager.lock);
  254. return 0;
  255. } while (i != ring->idx);
  256. /* Still no ID to use? Then use the idle one found earlier */
  257. id = idle;
  258. /* Remember this submission as user of the VMID */
  259. r = amdgpu_sync_fence(ring->adev, &id->active, fence);
  260. if (r)
  261. goto error;
  262. fence_put(id->first);
  263. id->first = fence_get(fence);
  264. fence_put(id->last_flush);
  265. id->last_flush = NULL;
  266. fence_put(id->flushed_updates);
  267. id->flushed_updates = fence_get(updates);
  268. id->pd_gpu_addr = *vm_pd_addr;
  269. list_move_tail(&id->list, &adev->vm_manager.ids_lru);
  270. atomic64_set(&id->owner, vm->client_id);
  271. vm->ids[ring->idx] = id;
  272. *vm_id = id - adev->vm_manager.ids;
  273. trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
  274. error:
  275. mutex_unlock(&adev->vm_manager.lock);
  276. return r;
  277. }
  278. static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
  279. {
  280. struct amdgpu_device *adev = ring->adev;
  281. const struct amdgpu_ip_block_version *ip_block;
  282. if (ring->type != AMDGPU_RING_TYPE_COMPUTE)
  283. /* only compute rings */
  284. return false;
  285. ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
  286. if (!ip_block)
  287. return false;
  288. if (ip_block->major <= 7) {
  289. /* gfx7 has no workaround */
  290. return true;
  291. } else if (ip_block->major == 8) {
  292. if (adev->gfx.mec_fw_version >= 673)
  293. /* gfx8 is fixed in MEC firmware 673 */
  294. return false;
  295. else
  296. return true;
  297. }
  298. return false;
  299. }
  300. /**
  301. * amdgpu_vm_flush - hardware flush the vm
  302. *
  303. * @ring: ring to use for flush
  304. * @vm_id: vmid number to use
  305. * @pd_addr: address of the page directory
  306. *
  307. * Emit a VM flush when it is necessary.
  308. */
  309. int amdgpu_vm_flush(struct amdgpu_ring *ring,
  310. unsigned vm_id, uint64_t pd_addr,
  311. uint32_t gds_base, uint32_t gds_size,
  312. uint32_t gws_base, uint32_t gws_size,
  313. uint32_t oa_base, uint32_t oa_size)
  314. {
  315. struct amdgpu_device *adev = ring->adev;
  316. struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
  317. bool gds_switch_needed = ring->funcs->emit_gds_switch && (
  318. id->gds_base != gds_base ||
  319. id->gds_size != gds_size ||
  320. id->gws_base != gws_base ||
  321. id->gws_size != gws_size ||
  322. id->oa_base != oa_base ||
  323. id->oa_size != oa_size);
  324. int r;
  325. if (ring->funcs->emit_pipeline_sync && (
  326. pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed ||
  327. amdgpu_vm_ring_has_compute_vm_bug(ring)))
  328. amdgpu_ring_emit_pipeline_sync(ring);
  329. if (ring->funcs->emit_vm_flush &&
  330. pd_addr != AMDGPU_VM_NO_FLUSH) {
  331. struct fence *fence;
  332. trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id);
  333. amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr);
  334. r = amdgpu_fence_emit(ring, &fence);
  335. if (r)
  336. return r;
  337. mutex_lock(&adev->vm_manager.lock);
  338. fence_put(id->last_flush);
  339. id->last_flush = fence;
  340. mutex_unlock(&adev->vm_manager.lock);
  341. }
  342. if (gds_switch_needed) {
  343. id->gds_base = gds_base;
  344. id->gds_size = gds_size;
  345. id->gws_base = gws_base;
  346. id->gws_size = gws_size;
  347. id->oa_base = oa_base;
  348. id->oa_size = oa_size;
  349. amdgpu_ring_emit_gds_switch(ring, vm_id,
  350. gds_base, gds_size,
  351. gws_base, gws_size,
  352. oa_base, oa_size);
  353. }
  354. return 0;
  355. }
  356. /**
  357. * amdgpu_vm_reset_id - reset VMID to zero
  358. *
  359. * @adev: amdgpu device structure
  360. * @vm_id: vmid number to use
  361. *
  362. * Reset saved GDW, GWS and OA to force switch on next flush.
  363. */
  364. void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
  365. {
  366. struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
  367. id->gds_base = 0;
  368. id->gds_size = 0;
  369. id->gws_base = 0;
  370. id->gws_size = 0;
  371. id->oa_base = 0;
  372. id->oa_size = 0;
  373. }
  374. /**
  375. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  376. *
  377. * @vm: requested vm
  378. * @bo: requested buffer object
  379. *
  380. * Find @bo inside the requested vm.
  381. * Search inside the @bos vm list for the requested vm
  382. * Returns the found bo_va or NULL if none is found
  383. *
  384. * Object has to be reserved!
  385. */
  386. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  387. struct amdgpu_bo *bo)
  388. {
  389. struct amdgpu_bo_va *bo_va;
  390. list_for_each_entry(bo_va, &bo->va, bo_list) {
  391. if (bo_va->vm == vm) {
  392. return bo_va;
  393. }
  394. }
  395. return NULL;
  396. }
  397. /**
  398. * amdgpu_vm_update_pages - helper to call the right asic function
  399. *
  400. * @adev: amdgpu_device pointer
  401. * @vm_update_params: see amdgpu_vm_update_params definition
  402. * @pe: addr of the page entry
  403. * @addr: dst addr to write into pe
  404. * @count: number of page entries to update
  405. * @incr: increase next addr by incr bytes
  406. * @flags: hw access flags
  407. *
  408. * Traces the parameters and calls the right asic functions
  409. * to setup the page table using the DMA.
  410. */
  411. static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
  412. struct amdgpu_vm_update_params
  413. *vm_update_params,
  414. uint64_t pe, uint64_t addr,
  415. unsigned count, uint32_t incr,
  416. uint32_t flags)
  417. {
  418. trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
  419. if (vm_update_params->src) {
  420. amdgpu_vm_copy_pte(adev, vm_update_params->ib,
  421. pe, (vm_update_params->src + (addr >> 12) * 8), count);
  422. } else if (vm_update_params->pages_addr) {
  423. amdgpu_vm_write_pte(adev, vm_update_params->ib,
  424. vm_update_params->pages_addr,
  425. pe, addr, count, incr, flags);
  426. } else if (count < 3) {
  427. amdgpu_vm_write_pte(adev, vm_update_params->ib, NULL, pe, addr,
  428. count, incr, flags);
  429. } else {
  430. amdgpu_vm_set_pte_pde(adev, vm_update_params->ib, pe, addr,
  431. count, incr, flags);
  432. }
  433. }
  434. /**
  435. * amdgpu_vm_clear_bo - initially clear the page dir/table
  436. *
  437. * @adev: amdgpu_device pointer
  438. * @bo: bo to clear
  439. *
  440. * need to reserve bo first before calling it.
  441. */
  442. static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
  443. struct amdgpu_vm *vm,
  444. struct amdgpu_bo *bo)
  445. {
  446. struct amdgpu_ring *ring;
  447. struct fence *fence = NULL;
  448. struct amdgpu_job *job;
  449. struct amdgpu_vm_update_params vm_update_params;
  450. unsigned entries;
  451. uint64_t addr;
  452. int r;
  453. memset(&vm_update_params, 0, sizeof(vm_update_params));
  454. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  455. r = reservation_object_reserve_shared(bo->tbo.resv);
  456. if (r)
  457. return r;
  458. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  459. if (r)
  460. goto error;
  461. addr = amdgpu_bo_gpu_offset(bo);
  462. entries = amdgpu_bo_size(bo) / 8;
  463. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  464. if (r)
  465. goto error;
  466. vm_update_params.ib = &job->ibs[0];
  467. amdgpu_vm_update_pages(adev, &vm_update_params, addr, 0, entries,
  468. 0, 0);
  469. amdgpu_ring_pad_ib(ring, &job->ibs[0]);
  470. WARN_ON(job->ibs[0].length_dw > 64);
  471. r = amdgpu_job_submit(job, ring, &vm->entity,
  472. AMDGPU_FENCE_OWNER_VM, &fence);
  473. if (r)
  474. goto error_free;
  475. amdgpu_bo_fence(bo, fence, true);
  476. fence_put(fence);
  477. return 0;
  478. error_free:
  479. amdgpu_job_free(job);
  480. error:
  481. return r;
  482. }
  483. /**
  484. * amdgpu_vm_map_gart - Resolve gart mapping of addr
  485. *
  486. * @pages_addr: optional DMA address to use for lookup
  487. * @addr: the unmapped addr
  488. *
  489. * Look up the physical address of the page that the pte resolves
  490. * to and return the pointer for the page table entry.
  491. */
  492. uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
  493. {
  494. uint64_t result;
  495. if (pages_addr) {
  496. /* page table offset */
  497. result = pages_addr[addr >> PAGE_SHIFT];
  498. /* in case cpu page size != gpu page size*/
  499. result |= addr & (~PAGE_MASK);
  500. } else {
  501. /* No mapping required */
  502. result = addr;
  503. }
  504. result &= 0xFFFFFFFFFFFFF000ULL;
  505. return result;
  506. }
  507. /**
  508. * amdgpu_vm_update_pdes - make sure that page directory is valid
  509. *
  510. * @adev: amdgpu_device pointer
  511. * @vm: requested vm
  512. * @start: start of GPU address range
  513. * @end: end of GPU address range
  514. *
  515. * Allocates new page tables if necessary
  516. * and updates the page directory.
  517. * Returns 0 for success, error for failure.
  518. */
  519. int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
  520. struct amdgpu_vm *vm)
  521. {
  522. struct amdgpu_ring *ring;
  523. struct amdgpu_bo *pd = vm->page_directory;
  524. uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
  525. uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
  526. uint64_t last_pde = ~0, last_pt = ~0;
  527. unsigned count = 0, pt_idx, ndw;
  528. struct amdgpu_job *job;
  529. struct amdgpu_vm_update_params vm_update_params;
  530. struct fence *fence = NULL;
  531. int r;
  532. memset(&vm_update_params, 0, sizeof(vm_update_params));
  533. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  534. /* padding, etc. */
  535. ndw = 64;
  536. /* assume the worst case */
  537. ndw += vm->max_pde_used * 6;
  538. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  539. if (r)
  540. return r;
  541. vm_update_params.ib = &job->ibs[0];
  542. /* walk over the address space and update the page directory */
  543. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  544. struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
  545. uint64_t pde, pt;
  546. if (bo == NULL)
  547. continue;
  548. pt = amdgpu_bo_gpu_offset(bo);
  549. if (vm->page_tables[pt_idx].addr == pt)
  550. continue;
  551. vm->page_tables[pt_idx].addr = pt;
  552. pde = pd_addr + pt_idx * 8;
  553. if (((last_pde + 8 * count) != pde) ||
  554. ((last_pt + incr * count) != pt)) {
  555. if (count) {
  556. amdgpu_vm_update_pages(adev, &vm_update_params,
  557. last_pde, last_pt,
  558. count, incr,
  559. AMDGPU_PTE_VALID);
  560. }
  561. count = 1;
  562. last_pde = pde;
  563. last_pt = pt;
  564. } else {
  565. ++count;
  566. }
  567. }
  568. if (count)
  569. amdgpu_vm_update_pages(adev, &vm_update_params,
  570. last_pde, last_pt,
  571. count, incr, AMDGPU_PTE_VALID);
  572. if (vm_update_params.ib->length_dw != 0) {
  573. amdgpu_ring_pad_ib(ring, vm_update_params.ib);
  574. amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
  575. AMDGPU_FENCE_OWNER_VM);
  576. WARN_ON(vm_update_params.ib->length_dw > ndw);
  577. r = amdgpu_job_submit(job, ring, &vm->entity,
  578. AMDGPU_FENCE_OWNER_VM, &fence);
  579. if (r)
  580. goto error_free;
  581. amdgpu_bo_fence(pd, fence, true);
  582. fence_put(vm->page_directory_fence);
  583. vm->page_directory_fence = fence_get(fence);
  584. fence_put(fence);
  585. } else {
  586. amdgpu_job_free(job);
  587. }
  588. return 0;
  589. error_free:
  590. amdgpu_job_free(job);
  591. return r;
  592. }
  593. /**
  594. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  595. *
  596. * @adev: amdgpu_device pointer
  597. * @vm_update_params: see amdgpu_vm_update_params definition
  598. * @pe_start: first PTE to handle
  599. * @pe_end: last PTE to handle
  600. * @addr: addr those PTEs should point to
  601. * @flags: hw mapping flags
  602. */
  603. static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
  604. struct amdgpu_vm_update_params
  605. *vm_update_params,
  606. uint64_t pe_start, uint64_t pe_end,
  607. uint64_t addr, uint32_t flags)
  608. {
  609. /**
  610. * The MC L1 TLB supports variable sized pages, based on a fragment
  611. * field in the PTE. When this field is set to a non-zero value, page
  612. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  613. * flags are considered valid for all PTEs within the fragment range
  614. * and corresponding mappings are assumed to be physically contiguous.
  615. *
  616. * The L1 TLB can store a single PTE for the whole fragment,
  617. * significantly increasing the space available for translation
  618. * caching. This leads to large improvements in throughput when the
  619. * TLB is under pressure.
  620. *
  621. * The L2 TLB distributes small and large fragments into two
  622. * asymmetric partitions. The large fragment cache is significantly
  623. * larger. Thus, we try to use large fragments wherever possible.
  624. * Userspace can support this by aligning virtual base address and
  625. * allocation size to the fragment size.
  626. */
  627. /* SI and newer are optimized for 64KB */
  628. uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
  629. uint64_t frag_align = 0x80;
  630. uint64_t frag_start = ALIGN(pe_start, frag_align);
  631. uint64_t frag_end = pe_end & ~(frag_align - 1);
  632. unsigned count;
  633. /* Abort early if there isn't anything to do */
  634. if (pe_start == pe_end)
  635. return;
  636. /* system pages are non continuously */
  637. if (vm_update_params->src || vm_update_params->pages_addr ||
  638. !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
  639. count = (pe_end - pe_start) / 8;
  640. amdgpu_vm_update_pages(adev, vm_update_params, pe_start,
  641. addr, count, AMDGPU_GPU_PAGE_SIZE,
  642. flags);
  643. return;
  644. }
  645. /* handle the 4K area at the beginning */
  646. if (pe_start != frag_start) {
  647. count = (frag_start - pe_start) / 8;
  648. amdgpu_vm_update_pages(adev, vm_update_params, pe_start, addr,
  649. count, AMDGPU_GPU_PAGE_SIZE, flags);
  650. addr += AMDGPU_GPU_PAGE_SIZE * count;
  651. }
  652. /* handle the area in the middle */
  653. count = (frag_end - frag_start) / 8;
  654. amdgpu_vm_update_pages(adev, vm_update_params, frag_start, addr, count,
  655. AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
  656. /* handle the 4K area at the end */
  657. if (frag_end != pe_end) {
  658. addr += AMDGPU_GPU_PAGE_SIZE * count;
  659. count = (pe_end - frag_end) / 8;
  660. amdgpu_vm_update_pages(adev, vm_update_params, frag_end, addr,
  661. count, AMDGPU_GPU_PAGE_SIZE, flags);
  662. }
  663. }
  664. /**
  665. * amdgpu_vm_update_ptes - make sure that page tables are valid
  666. *
  667. * @adev: amdgpu_device pointer
  668. * @vm_update_params: see amdgpu_vm_update_params definition
  669. * @vm: requested vm
  670. * @start: start of GPU address range
  671. * @end: end of GPU address range
  672. * @dst: destination address to map to, the next dst inside the function
  673. * @flags: mapping flags
  674. *
  675. * Update the page tables in the range @start - @end.
  676. */
  677. static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
  678. struct amdgpu_vm_update_params
  679. *vm_update_params,
  680. struct amdgpu_vm *vm,
  681. uint64_t start, uint64_t end,
  682. uint64_t dst, uint32_t flags)
  683. {
  684. const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  685. uint64_t cur_pe_start, cur_pe_end, cur_dst;
  686. uint64_t addr; /* next GPU address to be updated */
  687. uint64_t pt_idx;
  688. struct amdgpu_bo *pt;
  689. unsigned nptes; /* next number of ptes to be updated */
  690. uint64_t next_pe_start;
  691. /* initialize the variables */
  692. addr = start;
  693. pt_idx = addr >> amdgpu_vm_block_size;
  694. pt = vm->page_tables[pt_idx].entry.robj;
  695. if ((addr & ~mask) == (end & ~mask))
  696. nptes = end - addr;
  697. else
  698. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  699. cur_pe_start = amdgpu_bo_gpu_offset(pt);
  700. cur_pe_start += (addr & mask) * 8;
  701. cur_pe_end = cur_pe_start + 8 * nptes;
  702. cur_dst = dst;
  703. /* for next ptb*/
  704. addr += nptes;
  705. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  706. /* walk over the address space and update the page tables */
  707. while (addr < end) {
  708. pt_idx = addr >> amdgpu_vm_block_size;
  709. pt = vm->page_tables[pt_idx].entry.robj;
  710. if ((addr & ~mask) == (end & ~mask))
  711. nptes = end - addr;
  712. else
  713. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  714. next_pe_start = amdgpu_bo_gpu_offset(pt);
  715. next_pe_start += (addr & mask) * 8;
  716. if (cur_pe_end == next_pe_start) {
  717. /* The next ptb is consecutive to current ptb.
  718. * Don't call amdgpu_vm_frag_ptes now.
  719. * Will update two ptbs together in future.
  720. */
  721. cur_pe_end += 8 * nptes;
  722. } else {
  723. amdgpu_vm_frag_ptes(adev, vm_update_params,
  724. cur_pe_start, cur_pe_end,
  725. cur_dst, flags);
  726. cur_pe_start = next_pe_start;
  727. cur_pe_end = next_pe_start + 8 * nptes;
  728. cur_dst = dst;
  729. }
  730. /* for next ptb*/
  731. addr += nptes;
  732. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  733. }
  734. amdgpu_vm_frag_ptes(adev, vm_update_params, cur_pe_start,
  735. cur_pe_end, cur_dst, flags);
  736. }
  737. /**
  738. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  739. *
  740. * @adev: amdgpu_device pointer
  741. * @exclusive: fence we need to sync to
  742. * @src: address where to copy page table entries from
  743. * @pages_addr: DMA addresses to use for mapping
  744. * @vm: requested vm
  745. * @start: start of mapped range
  746. * @last: last mapped entry
  747. * @flags: flags for the entries
  748. * @addr: addr to set the area to
  749. * @fence: optional resulting fence
  750. *
  751. * Fill in the page table entries between @start and @last.
  752. * Returns 0 for success, -EINVAL for failure.
  753. */
  754. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  755. struct fence *exclusive,
  756. uint64_t src,
  757. dma_addr_t *pages_addr,
  758. struct amdgpu_vm *vm,
  759. uint64_t start, uint64_t last,
  760. uint32_t flags, uint64_t addr,
  761. struct fence **fence)
  762. {
  763. struct amdgpu_ring *ring;
  764. void *owner = AMDGPU_FENCE_OWNER_VM;
  765. unsigned nptes, ncmds, ndw;
  766. struct amdgpu_job *job;
  767. struct amdgpu_vm_update_params vm_update_params;
  768. struct fence *f = NULL;
  769. int r;
  770. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  771. memset(&vm_update_params, 0, sizeof(vm_update_params));
  772. vm_update_params.src = src;
  773. vm_update_params.pages_addr = pages_addr;
  774. /* sync to everything on unmapping */
  775. if (!(flags & AMDGPU_PTE_VALID))
  776. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  777. nptes = last - start + 1;
  778. /*
  779. * reserve space for one command every (1 << BLOCK_SIZE)
  780. * entries or 2k dwords (whatever is smaller)
  781. */
  782. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  783. /* padding, etc. */
  784. ndw = 64;
  785. if (vm_update_params.src) {
  786. /* only copy commands needed */
  787. ndw += ncmds * 7;
  788. } else if (vm_update_params.pages_addr) {
  789. /* header for write data commands */
  790. ndw += ncmds * 4;
  791. /* body of write data command */
  792. ndw += nptes * 2;
  793. } else {
  794. /* set page commands needed */
  795. ndw += ncmds * 10;
  796. /* two extra commands for begin/end of fragment */
  797. ndw += 2 * 10;
  798. }
  799. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  800. if (r)
  801. return r;
  802. vm_update_params.ib = &job->ibs[0];
  803. r = amdgpu_sync_fence(adev, &job->sync, exclusive);
  804. if (r)
  805. goto error_free;
  806. r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
  807. owner);
  808. if (r)
  809. goto error_free;
  810. r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
  811. if (r)
  812. goto error_free;
  813. amdgpu_vm_update_ptes(adev, &vm_update_params, vm, start,
  814. last + 1, addr, flags);
  815. amdgpu_ring_pad_ib(ring, vm_update_params.ib);
  816. WARN_ON(vm_update_params.ib->length_dw > ndw);
  817. r = amdgpu_job_submit(job, ring, &vm->entity,
  818. AMDGPU_FENCE_OWNER_VM, &f);
  819. if (r)
  820. goto error_free;
  821. amdgpu_bo_fence(vm->page_directory, f, true);
  822. if (fence) {
  823. fence_put(*fence);
  824. *fence = fence_get(f);
  825. }
  826. fence_put(f);
  827. return 0;
  828. error_free:
  829. amdgpu_job_free(job);
  830. return r;
  831. }
  832. /**
  833. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  834. *
  835. * @adev: amdgpu_device pointer
  836. * @exclusive: fence we need to sync to
  837. * @gtt_flags: flags as they are used for GTT
  838. * @pages_addr: DMA addresses to use for mapping
  839. * @vm: requested vm
  840. * @mapping: mapped range and flags to use for the update
  841. * @addr: addr to set the area to
  842. * @flags: HW flags for the mapping
  843. * @fence: optional resulting fence
  844. *
  845. * Split the mapping into smaller chunks so that each update fits
  846. * into a SDMA IB.
  847. * Returns 0 for success, -EINVAL for failure.
  848. */
  849. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  850. struct fence *exclusive,
  851. uint32_t gtt_flags,
  852. dma_addr_t *pages_addr,
  853. struct amdgpu_vm *vm,
  854. struct amdgpu_bo_va_mapping *mapping,
  855. uint32_t flags, uint64_t addr,
  856. struct fence **fence)
  857. {
  858. const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
  859. uint64_t src = 0, start = mapping->it.start;
  860. int r;
  861. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  862. * but in case of something, we filter the flags in first place
  863. */
  864. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  865. flags &= ~AMDGPU_PTE_READABLE;
  866. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  867. flags &= ~AMDGPU_PTE_WRITEABLE;
  868. trace_amdgpu_vm_bo_update(mapping);
  869. if (pages_addr) {
  870. if (flags == gtt_flags)
  871. src = adev->gart.table_addr + (addr >> 12) * 8;
  872. addr = 0;
  873. }
  874. addr += mapping->offset;
  875. if (!pages_addr || src)
  876. return amdgpu_vm_bo_update_mapping(adev, exclusive,
  877. src, pages_addr, vm,
  878. start, mapping->it.last,
  879. flags, addr, fence);
  880. while (start != mapping->it.last + 1) {
  881. uint64_t last;
  882. last = min((uint64_t)mapping->it.last, start + max_size - 1);
  883. r = amdgpu_vm_bo_update_mapping(adev, exclusive,
  884. src, pages_addr, vm,
  885. start, last, flags, addr,
  886. fence);
  887. if (r)
  888. return r;
  889. start = last + 1;
  890. addr += max_size * AMDGPU_GPU_PAGE_SIZE;
  891. }
  892. return 0;
  893. }
  894. /**
  895. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  896. *
  897. * @adev: amdgpu_device pointer
  898. * @bo_va: requested BO and VM object
  899. * @mem: ttm mem
  900. *
  901. * Fill in the page table entries for @bo_va.
  902. * Returns 0 for success, -EINVAL for failure.
  903. *
  904. * Object have to be reserved and mutex must be locked!
  905. */
  906. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  907. struct amdgpu_bo_va *bo_va,
  908. struct ttm_mem_reg *mem)
  909. {
  910. struct amdgpu_vm *vm = bo_va->vm;
  911. struct amdgpu_bo_va_mapping *mapping;
  912. dma_addr_t *pages_addr = NULL;
  913. uint32_t gtt_flags, flags;
  914. struct fence *exclusive;
  915. uint64_t addr;
  916. int r;
  917. if (mem) {
  918. struct ttm_dma_tt *ttm;
  919. addr = (u64)mem->start << PAGE_SHIFT;
  920. switch (mem->mem_type) {
  921. case TTM_PL_TT:
  922. ttm = container_of(bo_va->bo->tbo.ttm, struct
  923. ttm_dma_tt, ttm);
  924. pages_addr = ttm->dma_address;
  925. break;
  926. case TTM_PL_VRAM:
  927. addr += adev->vm_manager.vram_base_offset;
  928. break;
  929. default:
  930. break;
  931. }
  932. exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
  933. } else {
  934. addr = 0;
  935. exclusive = NULL;
  936. }
  937. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  938. gtt_flags = (adev == bo_va->bo->adev) ? flags : 0;
  939. spin_lock(&vm->status_lock);
  940. if (!list_empty(&bo_va->vm_status))
  941. list_splice_init(&bo_va->valids, &bo_va->invalids);
  942. spin_unlock(&vm->status_lock);
  943. list_for_each_entry(mapping, &bo_va->invalids, list) {
  944. r = amdgpu_vm_bo_split_mapping(adev, exclusive,
  945. gtt_flags, pages_addr, vm,
  946. mapping, flags, addr,
  947. &bo_va->last_pt_update);
  948. if (r)
  949. return r;
  950. }
  951. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  952. list_for_each_entry(mapping, &bo_va->valids, list)
  953. trace_amdgpu_vm_bo_mapping(mapping);
  954. list_for_each_entry(mapping, &bo_va->invalids, list)
  955. trace_amdgpu_vm_bo_mapping(mapping);
  956. }
  957. spin_lock(&vm->status_lock);
  958. list_splice_init(&bo_va->invalids, &bo_va->valids);
  959. list_del_init(&bo_va->vm_status);
  960. if (!mem)
  961. list_add(&bo_va->vm_status, &vm->cleared);
  962. spin_unlock(&vm->status_lock);
  963. return 0;
  964. }
  965. /**
  966. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  967. *
  968. * @adev: amdgpu_device pointer
  969. * @vm: requested vm
  970. *
  971. * Make sure all freed BOs are cleared in the PT.
  972. * Returns 0 for success.
  973. *
  974. * PTs have to be reserved and mutex must be locked!
  975. */
  976. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  977. struct amdgpu_vm *vm)
  978. {
  979. struct amdgpu_bo_va_mapping *mapping;
  980. int r;
  981. while (!list_empty(&vm->freed)) {
  982. mapping = list_first_entry(&vm->freed,
  983. struct amdgpu_bo_va_mapping, list);
  984. list_del(&mapping->list);
  985. r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
  986. 0, 0, NULL);
  987. kfree(mapping);
  988. if (r)
  989. return r;
  990. }
  991. return 0;
  992. }
  993. /**
  994. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  995. *
  996. * @adev: amdgpu_device pointer
  997. * @vm: requested vm
  998. *
  999. * Make sure all invalidated BOs are cleared in the PT.
  1000. * Returns 0 for success.
  1001. *
  1002. * PTs have to be reserved and mutex must be locked!
  1003. */
  1004. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  1005. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  1006. {
  1007. struct amdgpu_bo_va *bo_va = NULL;
  1008. int r = 0;
  1009. spin_lock(&vm->status_lock);
  1010. while (!list_empty(&vm->invalidated)) {
  1011. bo_va = list_first_entry(&vm->invalidated,
  1012. struct amdgpu_bo_va, vm_status);
  1013. spin_unlock(&vm->status_lock);
  1014. r = amdgpu_vm_bo_update(adev, bo_va, NULL);
  1015. if (r)
  1016. return r;
  1017. spin_lock(&vm->status_lock);
  1018. }
  1019. spin_unlock(&vm->status_lock);
  1020. if (bo_va)
  1021. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  1022. return r;
  1023. }
  1024. /**
  1025. * amdgpu_vm_bo_add - add a bo to a specific vm
  1026. *
  1027. * @adev: amdgpu_device pointer
  1028. * @vm: requested vm
  1029. * @bo: amdgpu buffer object
  1030. *
  1031. * Add @bo into the requested vm.
  1032. * Add @bo to the list of bos associated with the vm
  1033. * Returns newly added bo_va or NULL for failure
  1034. *
  1035. * Object has to be reserved!
  1036. */
  1037. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1038. struct amdgpu_vm *vm,
  1039. struct amdgpu_bo *bo)
  1040. {
  1041. struct amdgpu_bo_va *bo_va;
  1042. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1043. if (bo_va == NULL) {
  1044. return NULL;
  1045. }
  1046. bo_va->vm = vm;
  1047. bo_va->bo = bo;
  1048. bo_va->ref_count = 1;
  1049. INIT_LIST_HEAD(&bo_va->bo_list);
  1050. INIT_LIST_HEAD(&bo_va->valids);
  1051. INIT_LIST_HEAD(&bo_va->invalids);
  1052. INIT_LIST_HEAD(&bo_va->vm_status);
  1053. list_add_tail(&bo_va->bo_list, &bo->va);
  1054. return bo_va;
  1055. }
  1056. /**
  1057. * amdgpu_vm_bo_map - map bo inside a vm
  1058. *
  1059. * @adev: amdgpu_device pointer
  1060. * @bo_va: bo_va to store the address
  1061. * @saddr: where to map the BO
  1062. * @offset: requested offset in the BO
  1063. * @flags: attributes of pages (read/write/valid/etc.)
  1064. *
  1065. * Add a mapping of the BO at the specefied addr into the VM.
  1066. * Returns 0 for success, error for failure.
  1067. *
  1068. * Object has to be reserved and unreserved outside!
  1069. */
  1070. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1071. struct amdgpu_bo_va *bo_va,
  1072. uint64_t saddr, uint64_t offset,
  1073. uint64_t size, uint32_t flags)
  1074. {
  1075. struct amdgpu_bo_va_mapping *mapping;
  1076. struct amdgpu_vm *vm = bo_va->vm;
  1077. struct interval_tree_node *it;
  1078. unsigned last_pfn, pt_idx;
  1079. uint64_t eaddr;
  1080. int r;
  1081. /* validate the parameters */
  1082. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1083. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1084. return -EINVAL;
  1085. /* make sure object fit at this offset */
  1086. eaddr = saddr + size - 1;
  1087. if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
  1088. return -EINVAL;
  1089. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  1090. if (last_pfn >= adev->vm_manager.max_pfn) {
  1091. dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
  1092. last_pfn, adev->vm_manager.max_pfn);
  1093. return -EINVAL;
  1094. }
  1095. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1096. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1097. it = interval_tree_iter_first(&vm->va, saddr, eaddr);
  1098. if (it) {
  1099. struct amdgpu_bo_va_mapping *tmp;
  1100. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  1101. /* bo and tmp overlap, invalid addr */
  1102. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1103. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  1104. tmp->it.start, tmp->it.last + 1);
  1105. r = -EINVAL;
  1106. goto error;
  1107. }
  1108. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1109. if (!mapping) {
  1110. r = -ENOMEM;
  1111. goto error;
  1112. }
  1113. INIT_LIST_HEAD(&mapping->list);
  1114. mapping->it.start = saddr;
  1115. mapping->it.last = eaddr;
  1116. mapping->offset = offset;
  1117. mapping->flags = flags;
  1118. list_add(&mapping->list, &bo_va->invalids);
  1119. interval_tree_insert(&mapping->it, &vm->va);
  1120. /* Make sure the page tables are allocated */
  1121. saddr >>= amdgpu_vm_block_size;
  1122. eaddr >>= amdgpu_vm_block_size;
  1123. BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
  1124. if (eaddr > vm->max_pde_used)
  1125. vm->max_pde_used = eaddr;
  1126. /* walk over the address space and allocate the page tables */
  1127. for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
  1128. struct reservation_object *resv = vm->page_directory->tbo.resv;
  1129. struct amdgpu_bo_list_entry *entry;
  1130. struct amdgpu_bo *pt;
  1131. entry = &vm->page_tables[pt_idx].entry;
  1132. if (entry->robj)
  1133. continue;
  1134. r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
  1135. AMDGPU_GPU_PAGE_SIZE, true,
  1136. AMDGPU_GEM_DOMAIN_VRAM,
  1137. AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
  1138. NULL, resv, &pt);
  1139. if (r)
  1140. goto error_free;
  1141. /* Keep a reference to the page table to avoid freeing
  1142. * them up in the wrong order.
  1143. */
  1144. pt->parent = amdgpu_bo_ref(vm->page_directory);
  1145. r = amdgpu_vm_clear_bo(adev, vm, pt);
  1146. if (r) {
  1147. amdgpu_bo_unref(&pt);
  1148. goto error_free;
  1149. }
  1150. entry->robj = pt;
  1151. entry->priority = 0;
  1152. entry->tv.bo = &entry->robj->tbo;
  1153. entry->tv.shared = true;
  1154. entry->user_pages = NULL;
  1155. vm->page_tables[pt_idx].addr = 0;
  1156. }
  1157. return 0;
  1158. error_free:
  1159. list_del(&mapping->list);
  1160. interval_tree_remove(&mapping->it, &vm->va);
  1161. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1162. kfree(mapping);
  1163. error:
  1164. return r;
  1165. }
  1166. /**
  1167. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1168. *
  1169. * @adev: amdgpu_device pointer
  1170. * @bo_va: bo_va to remove the address from
  1171. * @saddr: where to the BO is mapped
  1172. *
  1173. * Remove a mapping of the BO at the specefied addr from the VM.
  1174. * Returns 0 for success, error for failure.
  1175. *
  1176. * Object has to be reserved and unreserved outside!
  1177. */
  1178. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1179. struct amdgpu_bo_va *bo_va,
  1180. uint64_t saddr)
  1181. {
  1182. struct amdgpu_bo_va_mapping *mapping;
  1183. struct amdgpu_vm *vm = bo_va->vm;
  1184. bool valid = true;
  1185. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1186. list_for_each_entry(mapping, &bo_va->valids, list) {
  1187. if (mapping->it.start == saddr)
  1188. break;
  1189. }
  1190. if (&mapping->list == &bo_va->valids) {
  1191. valid = false;
  1192. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1193. if (mapping->it.start == saddr)
  1194. break;
  1195. }
  1196. if (&mapping->list == &bo_va->invalids)
  1197. return -ENOENT;
  1198. }
  1199. list_del(&mapping->list);
  1200. interval_tree_remove(&mapping->it, &vm->va);
  1201. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1202. if (valid)
  1203. list_add(&mapping->list, &vm->freed);
  1204. else
  1205. kfree(mapping);
  1206. return 0;
  1207. }
  1208. /**
  1209. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1210. *
  1211. * @adev: amdgpu_device pointer
  1212. * @bo_va: requested bo_va
  1213. *
  1214. * Remove @bo_va->bo from the requested vm.
  1215. *
  1216. * Object have to be reserved!
  1217. */
  1218. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1219. struct amdgpu_bo_va *bo_va)
  1220. {
  1221. struct amdgpu_bo_va_mapping *mapping, *next;
  1222. struct amdgpu_vm *vm = bo_va->vm;
  1223. list_del(&bo_va->bo_list);
  1224. spin_lock(&vm->status_lock);
  1225. list_del(&bo_va->vm_status);
  1226. spin_unlock(&vm->status_lock);
  1227. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1228. list_del(&mapping->list);
  1229. interval_tree_remove(&mapping->it, &vm->va);
  1230. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1231. list_add(&mapping->list, &vm->freed);
  1232. }
  1233. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1234. list_del(&mapping->list);
  1235. interval_tree_remove(&mapping->it, &vm->va);
  1236. kfree(mapping);
  1237. }
  1238. fence_put(bo_va->last_pt_update);
  1239. kfree(bo_va);
  1240. }
  1241. /**
  1242. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1243. *
  1244. * @adev: amdgpu_device pointer
  1245. * @vm: requested vm
  1246. * @bo: amdgpu buffer object
  1247. *
  1248. * Mark @bo as invalid.
  1249. */
  1250. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1251. struct amdgpu_bo *bo)
  1252. {
  1253. struct amdgpu_bo_va *bo_va;
  1254. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1255. spin_lock(&bo_va->vm->status_lock);
  1256. if (list_empty(&bo_va->vm_status))
  1257. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1258. spin_unlock(&bo_va->vm->status_lock);
  1259. }
  1260. }
  1261. /**
  1262. * amdgpu_vm_init - initialize a vm instance
  1263. *
  1264. * @adev: amdgpu_device pointer
  1265. * @vm: requested vm
  1266. *
  1267. * Init @vm fields.
  1268. */
  1269. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1270. {
  1271. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1272. AMDGPU_VM_PTE_COUNT * 8);
  1273. unsigned pd_size, pd_entries;
  1274. unsigned ring_instance;
  1275. struct amdgpu_ring *ring;
  1276. struct amd_sched_rq *rq;
  1277. int i, r;
  1278. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1279. vm->ids[i] = NULL;
  1280. vm->va = RB_ROOT;
  1281. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  1282. spin_lock_init(&vm->status_lock);
  1283. INIT_LIST_HEAD(&vm->invalidated);
  1284. INIT_LIST_HEAD(&vm->cleared);
  1285. INIT_LIST_HEAD(&vm->freed);
  1286. pd_size = amdgpu_vm_directory_size(adev);
  1287. pd_entries = amdgpu_vm_num_pdes(adev);
  1288. /* allocate page table array */
  1289. vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
  1290. if (vm->page_tables == NULL) {
  1291. DRM_ERROR("Cannot allocate memory for page table array\n");
  1292. return -ENOMEM;
  1293. }
  1294. /* create scheduler entity for page table updates */
  1295. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  1296. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  1297. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  1298. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
  1299. r = amd_sched_entity_init(&ring->sched, &vm->entity,
  1300. rq, amdgpu_sched_jobs);
  1301. if (r)
  1302. return r;
  1303. vm->page_directory_fence = NULL;
  1304. r = amdgpu_bo_create(adev, pd_size, align, true,
  1305. AMDGPU_GEM_DOMAIN_VRAM,
  1306. AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
  1307. NULL, NULL, &vm->page_directory);
  1308. if (r)
  1309. goto error_free_sched_entity;
  1310. r = amdgpu_bo_reserve(vm->page_directory, false);
  1311. if (r)
  1312. goto error_free_page_directory;
  1313. r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
  1314. amdgpu_bo_unreserve(vm->page_directory);
  1315. if (r)
  1316. goto error_free_page_directory;
  1317. vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
  1318. return 0;
  1319. error_free_page_directory:
  1320. amdgpu_bo_unref(&vm->page_directory);
  1321. vm->page_directory = NULL;
  1322. error_free_sched_entity:
  1323. amd_sched_entity_fini(&ring->sched, &vm->entity);
  1324. return r;
  1325. }
  1326. /**
  1327. * amdgpu_vm_fini - tear down a vm instance
  1328. *
  1329. * @adev: amdgpu_device pointer
  1330. * @vm: requested vm
  1331. *
  1332. * Tear down @vm.
  1333. * Unbind the VM and remove all bos from the vm bo list
  1334. */
  1335. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1336. {
  1337. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1338. int i;
  1339. amd_sched_entity_fini(vm->entity.sched, &vm->entity);
  1340. if (!RB_EMPTY_ROOT(&vm->va)) {
  1341. dev_err(adev->dev, "still active bo inside vm\n");
  1342. }
  1343. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1344. list_del(&mapping->list);
  1345. interval_tree_remove(&mapping->it, &vm->va);
  1346. kfree(mapping);
  1347. }
  1348. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1349. list_del(&mapping->list);
  1350. kfree(mapping);
  1351. }
  1352. for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
  1353. amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
  1354. drm_free_large(vm->page_tables);
  1355. amdgpu_bo_unref(&vm->page_directory);
  1356. fence_put(vm->page_directory_fence);
  1357. }
  1358. /**
  1359. * amdgpu_vm_manager_init - init the VM manager
  1360. *
  1361. * @adev: amdgpu_device pointer
  1362. *
  1363. * Initialize the VM manager structures
  1364. */
  1365. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  1366. {
  1367. unsigned i;
  1368. INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
  1369. /* skip over VMID 0, since it is the system VM */
  1370. for (i = 1; i < adev->vm_manager.num_ids; ++i) {
  1371. amdgpu_vm_reset_id(adev, i);
  1372. amdgpu_sync_create(&adev->vm_manager.ids[i].active);
  1373. list_add_tail(&adev->vm_manager.ids[i].list,
  1374. &adev->vm_manager.ids_lru);
  1375. }
  1376. adev->vm_manager.fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
  1377. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1378. adev->vm_manager.seqno[i] = 0;
  1379. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  1380. atomic64_set(&adev->vm_manager.client_counter, 0);
  1381. }
  1382. /**
  1383. * amdgpu_vm_manager_fini - cleanup VM manager
  1384. *
  1385. * @adev: amdgpu_device pointer
  1386. *
  1387. * Cleanup the VM manager and free resources.
  1388. */
  1389. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  1390. {
  1391. unsigned i;
  1392. for (i = 0; i < AMDGPU_NUM_VM; ++i) {
  1393. struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
  1394. fence_put(adev->vm_manager.ids[i].first);
  1395. amdgpu_sync_free(&adev->vm_manager.ids[i].active);
  1396. fence_put(id->flushed_updates);
  1397. }
  1398. }