amdgpu_vm.c 44 KB

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