amdgpu_vm.c 42 KB

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