amdgpu_vm.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  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_ptes(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. uint64_t src = (params->src + (addr >> 12) * 8);
  452. trace_amdgpu_vm_copy_ptes(pe, src, count);
  453. amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, 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. r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
  482. if (r)
  483. goto error;
  484. addr = amdgpu_bo_gpu_offset(bo);
  485. entries = amdgpu_bo_size(bo) / 8;
  486. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  487. if (r)
  488. goto error;
  489. memset(&params, 0, sizeof(params));
  490. params.adev = adev;
  491. params.ib = &job->ibs[0];
  492. amdgpu_vm_do_set_ptes(&params, addr, 0, entries, 0, 0);
  493. amdgpu_ring_pad_ib(ring, &job->ibs[0]);
  494. WARN_ON(job->ibs[0].length_dw > 64);
  495. r = amdgpu_job_submit(job, ring, &vm->entity,
  496. AMDGPU_FENCE_OWNER_VM, &fence);
  497. if (r)
  498. goto error_free;
  499. amdgpu_bo_fence(bo, fence, true);
  500. fence_put(fence);
  501. return 0;
  502. error_free:
  503. amdgpu_job_free(job);
  504. error:
  505. return r;
  506. }
  507. /**
  508. * amdgpu_vm_map_gart - Resolve gart mapping of addr
  509. *
  510. * @pages_addr: optional DMA address to use for lookup
  511. * @addr: the unmapped addr
  512. *
  513. * Look up the physical address of the page that the pte resolves
  514. * to and return the pointer for the page table entry.
  515. */
  516. static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
  517. {
  518. uint64_t result;
  519. /* page table offset */
  520. result = pages_addr[addr >> PAGE_SHIFT];
  521. /* in case cpu page size != gpu page size*/
  522. result |= addr & (~PAGE_MASK);
  523. result &= 0xFFFFFFFFFFFFF000ULL;
  524. return result;
  525. }
  526. static int amdgpu_vm_update_pd_or_shadow(struct amdgpu_device *adev,
  527. struct amdgpu_vm *vm,
  528. bool shadow)
  529. {
  530. struct amdgpu_ring *ring;
  531. struct amdgpu_bo *pd = shadow ? vm->page_directory->shadow :
  532. vm->page_directory;
  533. uint64_t pd_addr;
  534. uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
  535. uint64_t last_pde = ~0, last_pt = ~0;
  536. unsigned count = 0, pt_idx, ndw;
  537. struct amdgpu_job *job;
  538. struct amdgpu_pte_update_params params;
  539. struct fence *fence = NULL;
  540. int r;
  541. if (!pd)
  542. return 0;
  543. r = amdgpu_ttm_bind(&pd->tbo, &pd->tbo.mem);
  544. if (r)
  545. return r;
  546. pd_addr = amdgpu_bo_gpu_offset(pd);
  547. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  548. /* padding, etc. */
  549. ndw = 64;
  550. /* assume the worst case */
  551. ndw += vm->max_pde_used * 6;
  552. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  553. if (r)
  554. return r;
  555. memset(&params, 0, sizeof(params));
  556. params.adev = adev;
  557. params.ib = &job->ibs[0];
  558. /* walk over the address space and update the page directory */
  559. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  560. struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
  561. uint64_t pde, pt;
  562. if (bo == NULL)
  563. continue;
  564. if (bo->shadow) {
  565. struct amdgpu_bo *shadow = bo->shadow;
  566. r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
  567. if (r)
  568. return r;
  569. }
  570. pt = amdgpu_bo_gpu_offset(bo);
  571. if (!shadow) {
  572. if (vm->page_tables[pt_idx].addr == pt)
  573. continue;
  574. vm->page_tables[pt_idx].addr = pt;
  575. } else {
  576. if (vm->page_tables[pt_idx].shadow_addr == pt)
  577. continue;
  578. vm->page_tables[pt_idx].shadow_addr = pt;
  579. }
  580. pde = pd_addr + pt_idx * 8;
  581. if (((last_pde + 8 * count) != pde) ||
  582. ((last_pt + incr * count) != pt) ||
  583. (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
  584. if (count) {
  585. amdgpu_vm_do_set_ptes(&params, last_pde,
  586. last_pt, count, incr,
  587. AMDGPU_PTE_VALID);
  588. }
  589. count = 1;
  590. last_pde = pde;
  591. last_pt = pt;
  592. } else {
  593. ++count;
  594. }
  595. }
  596. if (count)
  597. amdgpu_vm_do_set_ptes(&params, last_pde, last_pt,
  598. count, incr, AMDGPU_PTE_VALID);
  599. if (params.ib->length_dw != 0) {
  600. amdgpu_ring_pad_ib(ring, params.ib);
  601. amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
  602. AMDGPU_FENCE_OWNER_VM);
  603. WARN_ON(params.ib->length_dw > ndw);
  604. r = amdgpu_job_submit(job, ring, &vm->entity,
  605. AMDGPU_FENCE_OWNER_VM, &fence);
  606. if (r)
  607. goto error_free;
  608. amdgpu_bo_fence(pd, fence, true);
  609. fence_put(vm->page_directory_fence);
  610. vm->page_directory_fence = fence_get(fence);
  611. fence_put(fence);
  612. } else {
  613. amdgpu_job_free(job);
  614. }
  615. return 0;
  616. error_free:
  617. amdgpu_job_free(job);
  618. return r;
  619. }
  620. /*
  621. * amdgpu_vm_update_pdes - make sure that page directory is valid
  622. *
  623. * @adev: amdgpu_device pointer
  624. * @vm: requested vm
  625. * @start: start of GPU address range
  626. * @end: end of GPU address range
  627. *
  628. * Allocates new page tables if necessary
  629. * and updates the page directory.
  630. * Returns 0 for success, error for failure.
  631. */
  632. int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
  633. struct amdgpu_vm *vm)
  634. {
  635. int r;
  636. r = amdgpu_vm_update_pd_or_shadow(adev, vm, true);
  637. if (r)
  638. return r;
  639. return amdgpu_vm_update_pd_or_shadow(adev, vm, false);
  640. }
  641. /**
  642. * amdgpu_vm_update_ptes - make sure that page tables are valid
  643. *
  644. * @params: see amdgpu_pte_update_params definition
  645. * @vm: requested vm
  646. * @start: start of GPU address range
  647. * @end: end of GPU address range
  648. * @dst: destination address to map to, the next dst inside the function
  649. * @flags: mapping flags
  650. *
  651. * Update the page tables in the range @start - @end.
  652. */
  653. static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  654. struct amdgpu_vm *vm,
  655. uint64_t start, uint64_t end,
  656. uint64_t dst, uint32_t flags)
  657. {
  658. const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  659. uint64_t cur_pe_start, cur_nptes, cur_dst;
  660. uint64_t addr; /* next GPU address to be updated */
  661. uint64_t pt_idx;
  662. struct amdgpu_bo *pt;
  663. unsigned nptes; /* next number of ptes to be updated */
  664. uint64_t next_pe_start;
  665. /* initialize the variables */
  666. addr = start;
  667. pt_idx = addr >> amdgpu_vm_block_size;
  668. pt = vm->page_tables[pt_idx].entry.robj;
  669. if (params->shadow) {
  670. if (!pt->shadow)
  671. return;
  672. pt = vm->page_tables[pt_idx].entry.robj->shadow;
  673. }
  674. if ((addr & ~mask) == (end & ~mask))
  675. nptes = end - addr;
  676. else
  677. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  678. cur_pe_start = amdgpu_bo_gpu_offset(pt);
  679. cur_pe_start += (addr & mask) * 8;
  680. cur_nptes = nptes;
  681. cur_dst = dst;
  682. /* for next ptb*/
  683. addr += nptes;
  684. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  685. /* walk over the address space and update the page tables */
  686. while (addr < end) {
  687. pt_idx = addr >> amdgpu_vm_block_size;
  688. pt = vm->page_tables[pt_idx].entry.robj;
  689. if (params->shadow) {
  690. if (!pt->shadow)
  691. return;
  692. pt = vm->page_tables[pt_idx].entry.robj->shadow;
  693. }
  694. if ((addr & ~mask) == (end & ~mask))
  695. nptes = end - addr;
  696. else
  697. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  698. next_pe_start = amdgpu_bo_gpu_offset(pt);
  699. next_pe_start += (addr & mask) * 8;
  700. if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
  701. ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
  702. /* The next ptb is consecutive to current ptb.
  703. * Don't call the update function now.
  704. * Will update two ptbs together in future.
  705. */
  706. cur_nptes += nptes;
  707. } else {
  708. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  709. AMDGPU_GPU_PAGE_SIZE, flags);
  710. cur_pe_start = next_pe_start;
  711. cur_nptes = nptes;
  712. cur_dst = dst;
  713. }
  714. /* for next ptb*/
  715. addr += nptes;
  716. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  717. }
  718. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  719. AMDGPU_GPU_PAGE_SIZE, flags);
  720. }
  721. /*
  722. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  723. *
  724. * @params: see amdgpu_pte_update_params definition
  725. * @vm: requested vm
  726. * @start: first PTE to handle
  727. * @end: last PTE to handle
  728. * @dst: addr those PTEs should point to
  729. * @flags: hw mapping flags
  730. */
  731. static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  732. struct amdgpu_vm *vm,
  733. uint64_t start, uint64_t end,
  734. uint64_t dst, uint32_t flags)
  735. {
  736. /**
  737. * The MC L1 TLB supports variable sized pages, based on a fragment
  738. * field in the PTE. When this field is set to a non-zero value, page
  739. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  740. * flags are considered valid for all PTEs within the fragment range
  741. * and corresponding mappings are assumed to be physically contiguous.
  742. *
  743. * The L1 TLB can store a single PTE for the whole fragment,
  744. * significantly increasing the space available for translation
  745. * caching. This leads to large improvements in throughput when the
  746. * TLB is under pressure.
  747. *
  748. * The L2 TLB distributes small and large fragments into two
  749. * asymmetric partitions. The large fragment cache is significantly
  750. * larger. Thus, we try to use large fragments wherever possible.
  751. * Userspace can support this by aligning virtual base address and
  752. * allocation size to the fragment size.
  753. */
  754. /* SI and newer are optimized for 64KB */
  755. uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
  756. uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
  757. uint64_t frag_start = ALIGN(start, frag_align);
  758. uint64_t frag_end = end & ~(frag_align - 1);
  759. /* system pages are non continuously */
  760. if (params->src || !(flags & AMDGPU_PTE_VALID) ||
  761. (frag_start >= frag_end)) {
  762. amdgpu_vm_update_ptes(params, vm, start, end, dst, flags);
  763. return;
  764. }
  765. /* handle the 4K area at the beginning */
  766. if (start != frag_start) {
  767. amdgpu_vm_update_ptes(params, vm, start, frag_start,
  768. dst, flags);
  769. dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
  770. }
  771. /* handle the area in the middle */
  772. amdgpu_vm_update_ptes(params, vm, frag_start, frag_end, dst,
  773. flags | frag_flags);
  774. /* handle the 4K area at the end */
  775. if (frag_end != end) {
  776. dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
  777. amdgpu_vm_update_ptes(params, vm, frag_end, end, dst, flags);
  778. }
  779. }
  780. /**
  781. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  782. *
  783. * @adev: amdgpu_device pointer
  784. * @exclusive: fence we need to sync to
  785. * @src: address where to copy page table entries from
  786. * @pages_addr: DMA addresses to use for mapping
  787. * @vm: requested vm
  788. * @start: start of mapped range
  789. * @last: last mapped entry
  790. * @flags: flags for the entries
  791. * @addr: addr to set the area to
  792. * @fence: optional resulting fence
  793. *
  794. * Fill in the page table entries between @start and @last.
  795. * Returns 0 for success, -EINVAL for failure.
  796. */
  797. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  798. struct fence *exclusive,
  799. uint64_t src,
  800. dma_addr_t *pages_addr,
  801. struct amdgpu_vm *vm,
  802. uint64_t start, uint64_t last,
  803. uint32_t flags, uint64_t addr,
  804. struct fence **fence)
  805. {
  806. struct amdgpu_ring *ring;
  807. void *owner = AMDGPU_FENCE_OWNER_VM;
  808. unsigned nptes, ncmds, ndw;
  809. struct amdgpu_job *job;
  810. struct amdgpu_pte_update_params params;
  811. struct fence *f = NULL;
  812. int r;
  813. memset(&params, 0, sizeof(params));
  814. params.adev = adev;
  815. params.src = src;
  816. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  817. memset(&params, 0, sizeof(params));
  818. params.adev = adev;
  819. params.src = src;
  820. /* sync to everything on unmapping */
  821. if (!(flags & AMDGPU_PTE_VALID))
  822. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  823. nptes = last - start + 1;
  824. /*
  825. * reserve space for one command every (1 << BLOCK_SIZE)
  826. * entries or 2k dwords (whatever is smaller)
  827. */
  828. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  829. /* padding, etc. */
  830. ndw = 64;
  831. if (src) {
  832. /* only copy commands needed */
  833. ndw += ncmds * 7;
  834. params.func = amdgpu_vm_do_copy_ptes;
  835. } else if (pages_addr) {
  836. /* copy commands needed */
  837. ndw += ncmds * 7;
  838. /* and also PTEs */
  839. ndw += nptes * 2;
  840. params.func = amdgpu_vm_do_copy_ptes;
  841. } else {
  842. /* set page commands needed */
  843. ndw += ncmds * 10;
  844. /* two extra commands for begin/end of fragment */
  845. ndw += 2 * 10;
  846. params.func = amdgpu_vm_do_set_ptes;
  847. }
  848. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  849. if (r)
  850. return r;
  851. params.ib = &job->ibs[0];
  852. if (!src && pages_addr) {
  853. uint64_t *pte;
  854. unsigned i;
  855. /* Put the PTEs at the end of the IB. */
  856. i = ndw - nptes * 2;
  857. pte= (uint64_t *)&(job->ibs->ptr[i]);
  858. params.src = job->ibs->gpu_addr + i * 4;
  859. for (i = 0; i < nptes; ++i) {
  860. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  861. AMDGPU_GPU_PAGE_SIZE);
  862. pte[i] |= flags;
  863. }
  864. addr = 0;
  865. }
  866. r = amdgpu_sync_fence(adev, &job->sync, exclusive);
  867. if (r)
  868. goto error_free;
  869. r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
  870. owner);
  871. if (r)
  872. goto error_free;
  873. r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
  874. if (r)
  875. goto error_free;
  876. params.shadow = true;
  877. amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
  878. params.shadow = false;
  879. amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
  880. amdgpu_ring_pad_ib(ring, params.ib);
  881. WARN_ON(params.ib->length_dw > ndw);
  882. r = amdgpu_job_submit(job, ring, &vm->entity,
  883. AMDGPU_FENCE_OWNER_VM, &f);
  884. if (r)
  885. goto error_free;
  886. amdgpu_bo_fence(vm->page_directory, f, true);
  887. if (fence) {
  888. fence_put(*fence);
  889. *fence = fence_get(f);
  890. }
  891. fence_put(f);
  892. return 0;
  893. error_free:
  894. amdgpu_job_free(job);
  895. return r;
  896. }
  897. /**
  898. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  899. *
  900. * @adev: amdgpu_device pointer
  901. * @exclusive: fence we need to sync to
  902. * @gtt_flags: flags as they are used for GTT
  903. * @pages_addr: DMA addresses to use for mapping
  904. * @vm: requested vm
  905. * @mapping: mapped range and flags to use for the update
  906. * @addr: addr to set the area to
  907. * @flags: HW flags for the mapping
  908. * @fence: optional resulting fence
  909. *
  910. * Split the mapping into smaller chunks so that each update fits
  911. * into a SDMA IB.
  912. * Returns 0 for success, -EINVAL for failure.
  913. */
  914. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  915. struct fence *exclusive,
  916. uint32_t gtt_flags,
  917. dma_addr_t *pages_addr,
  918. struct amdgpu_vm *vm,
  919. struct amdgpu_bo_va_mapping *mapping,
  920. uint32_t flags, uint64_t addr,
  921. struct fence **fence)
  922. {
  923. const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
  924. uint64_t src = 0, start = mapping->it.start;
  925. int r;
  926. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  927. * but in case of something, we filter the flags in first place
  928. */
  929. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  930. flags &= ~AMDGPU_PTE_READABLE;
  931. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  932. flags &= ~AMDGPU_PTE_WRITEABLE;
  933. trace_amdgpu_vm_bo_update(mapping);
  934. if (pages_addr) {
  935. if (flags == gtt_flags)
  936. src = adev->gart.table_addr + (addr >> 12) * 8;
  937. addr = 0;
  938. }
  939. addr += mapping->offset;
  940. if (!pages_addr || src)
  941. return amdgpu_vm_bo_update_mapping(adev, exclusive,
  942. src, pages_addr, vm,
  943. start, mapping->it.last,
  944. flags, addr, fence);
  945. while (start != mapping->it.last + 1) {
  946. uint64_t last;
  947. last = min((uint64_t)mapping->it.last, start + max_size - 1);
  948. r = amdgpu_vm_bo_update_mapping(adev, exclusive,
  949. src, pages_addr, vm,
  950. start, last, flags, addr,
  951. fence);
  952. if (r)
  953. return r;
  954. start = last + 1;
  955. addr += max_size * AMDGPU_GPU_PAGE_SIZE;
  956. }
  957. return 0;
  958. }
  959. /**
  960. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  961. *
  962. * @adev: amdgpu_device pointer
  963. * @bo_va: requested BO and VM object
  964. * @clear: if true clear the entries
  965. *
  966. * Fill in the page table entries for @bo_va.
  967. * Returns 0 for success, -EINVAL for failure.
  968. */
  969. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  970. struct amdgpu_bo_va *bo_va,
  971. bool clear)
  972. {
  973. struct amdgpu_vm *vm = bo_va->vm;
  974. struct amdgpu_bo_va_mapping *mapping;
  975. dma_addr_t *pages_addr = NULL;
  976. uint32_t gtt_flags, flags;
  977. struct ttm_mem_reg *mem;
  978. struct fence *exclusive;
  979. uint64_t addr;
  980. int r;
  981. if (clear) {
  982. mem = NULL;
  983. addr = 0;
  984. exclusive = NULL;
  985. } else {
  986. struct ttm_dma_tt *ttm;
  987. mem = &bo_va->bo->tbo.mem;
  988. addr = (u64)mem->start << PAGE_SHIFT;
  989. switch (mem->mem_type) {
  990. case TTM_PL_TT:
  991. ttm = container_of(bo_va->bo->tbo.ttm, struct
  992. ttm_dma_tt, ttm);
  993. pages_addr = ttm->dma_address;
  994. break;
  995. case TTM_PL_VRAM:
  996. addr += adev->vm_manager.vram_base_offset;
  997. break;
  998. default:
  999. break;
  1000. }
  1001. exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
  1002. }
  1003. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  1004. gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
  1005. adev == bo_va->bo->adev) ? flags : 0;
  1006. spin_lock(&vm->status_lock);
  1007. if (!list_empty(&bo_va->vm_status))
  1008. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1009. spin_unlock(&vm->status_lock);
  1010. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1011. r = amdgpu_vm_bo_split_mapping(adev, exclusive,
  1012. gtt_flags, pages_addr, vm,
  1013. mapping, flags, addr,
  1014. &bo_va->last_pt_update);
  1015. if (r)
  1016. return r;
  1017. }
  1018. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1019. list_for_each_entry(mapping, &bo_va->valids, list)
  1020. trace_amdgpu_vm_bo_mapping(mapping);
  1021. list_for_each_entry(mapping, &bo_va->invalids, list)
  1022. trace_amdgpu_vm_bo_mapping(mapping);
  1023. }
  1024. spin_lock(&vm->status_lock);
  1025. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1026. list_del_init(&bo_va->vm_status);
  1027. if (clear)
  1028. list_add(&bo_va->vm_status, &vm->cleared);
  1029. spin_unlock(&vm->status_lock);
  1030. return 0;
  1031. }
  1032. /**
  1033. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1034. *
  1035. * @adev: amdgpu_device pointer
  1036. * @vm: requested vm
  1037. *
  1038. * Make sure all freed BOs are cleared in the PT.
  1039. * Returns 0 for success.
  1040. *
  1041. * PTs have to be reserved and mutex must be locked!
  1042. */
  1043. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1044. struct amdgpu_vm *vm)
  1045. {
  1046. struct amdgpu_bo_va_mapping *mapping;
  1047. int r;
  1048. while (!list_empty(&vm->freed)) {
  1049. mapping = list_first_entry(&vm->freed,
  1050. struct amdgpu_bo_va_mapping, list);
  1051. list_del(&mapping->list);
  1052. r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
  1053. 0, 0, NULL);
  1054. kfree(mapping);
  1055. if (r)
  1056. return r;
  1057. }
  1058. return 0;
  1059. }
  1060. /**
  1061. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  1062. *
  1063. * @adev: amdgpu_device pointer
  1064. * @vm: requested vm
  1065. *
  1066. * Make sure all invalidated BOs are cleared in the PT.
  1067. * Returns 0 for success.
  1068. *
  1069. * PTs have to be reserved and mutex must be locked!
  1070. */
  1071. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  1072. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  1073. {
  1074. struct amdgpu_bo_va *bo_va = NULL;
  1075. int r = 0;
  1076. spin_lock(&vm->status_lock);
  1077. while (!list_empty(&vm->invalidated)) {
  1078. bo_va = list_first_entry(&vm->invalidated,
  1079. struct amdgpu_bo_va, vm_status);
  1080. spin_unlock(&vm->status_lock);
  1081. r = amdgpu_vm_bo_update(adev, bo_va, true);
  1082. if (r)
  1083. return r;
  1084. spin_lock(&vm->status_lock);
  1085. }
  1086. spin_unlock(&vm->status_lock);
  1087. if (bo_va)
  1088. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  1089. return r;
  1090. }
  1091. /**
  1092. * amdgpu_vm_bo_add - add a bo to a specific vm
  1093. *
  1094. * @adev: amdgpu_device pointer
  1095. * @vm: requested vm
  1096. * @bo: amdgpu buffer object
  1097. *
  1098. * Add @bo into the requested vm.
  1099. * Add @bo to the list of bos associated with the vm
  1100. * Returns newly added bo_va or NULL for failure
  1101. *
  1102. * Object has to be reserved!
  1103. */
  1104. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1105. struct amdgpu_vm *vm,
  1106. struct amdgpu_bo *bo)
  1107. {
  1108. struct amdgpu_bo_va *bo_va;
  1109. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1110. if (bo_va == NULL) {
  1111. return NULL;
  1112. }
  1113. bo_va->vm = vm;
  1114. bo_va->bo = bo;
  1115. bo_va->ref_count = 1;
  1116. INIT_LIST_HEAD(&bo_va->bo_list);
  1117. INIT_LIST_HEAD(&bo_va->valids);
  1118. INIT_LIST_HEAD(&bo_va->invalids);
  1119. INIT_LIST_HEAD(&bo_va->vm_status);
  1120. list_add_tail(&bo_va->bo_list, &bo->va);
  1121. return bo_va;
  1122. }
  1123. /**
  1124. * amdgpu_vm_bo_map - map bo inside a vm
  1125. *
  1126. * @adev: amdgpu_device pointer
  1127. * @bo_va: bo_va to store the address
  1128. * @saddr: where to map the BO
  1129. * @offset: requested offset in the BO
  1130. * @flags: attributes of pages (read/write/valid/etc.)
  1131. *
  1132. * Add a mapping of the BO at the specefied addr into the VM.
  1133. * Returns 0 for success, error for failure.
  1134. *
  1135. * Object has to be reserved and unreserved outside!
  1136. */
  1137. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1138. struct amdgpu_bo_va *bo_va,
  1139. uint64_t saddr, uint64_t offset,
  1140. uint64_t size, uint32_t flags)
  1141. {
  1142. struct amdgpu_bo_va_mapping *mapping;
  1143. struct amdgpu_vm *vm = bo_va->vm;
  1144. struct interval_tree_node *it;
  1145. unsigned last_pfn, pt_idx;
  1146. uint64_t eaddr;
  1147. int r;
  1148. /* validate the parameters */
  1149. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1150. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1151. return -EINVAL;
  1152. /* make sure object fit at this offset */
  1153. eaddr = saddr + size - 1;
  1154. if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
  1155. return -EINVAL;
  1156. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  1157. if (last_pfn >= adev->vm_manager.max_pfn) {
  1158. dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
  1159. last_pfn, adev->vm_manager.max_pfn);
  1160. return -EINVAL;
  1161. }
  1162. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1163. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1164. it = interval_tree_iter_first(&vm->va, saddr, eaddr);
  1165. if (it) {
  1166. struct amdgpu_bo_va_mapping *tmp;
  1167. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  1168. /* bo and tmp overlap, invalid addr */
  1169. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1170. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  1171. tmp->it.start, tmp->it.last + 1);
  1172. r = -EINVAL;
  1173. goto error;
  1174. }
  1175. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1176. if (!mapping) {
  1177. r = -ENOMEM;
  1178. goto error;
  1179. }
  1180. INIT_LIST_HEAD(&mapping->list);
  1181. mapping->it.start = saddr;
  1182. mapping->it.last = eaddr;
  1183. mapping->offset = offset;
  1184. mapping->flags = flags;
  1185. list_add(&mapping->list, &bo_va->invalids);
  1186. interval_tree_insert(&mapping->it, &vm->va);
  1187. /* Make sure the page tables are allocated */
  1188. saddr >>= amdgpu_vm_block_size;
  1189. eaddr >>= amdgpu_vm_block_size;
  1190. BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
  1191. if (eaddr > vm->max_pde_used)
  1192. vm->max_pde_used = eaddr;
  1193. /* walk over the address space and allocate the page tables */
  1194. for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
  1195. struct reservation_object *resv = vm->page_directory->tbo.resv;
  1196. struct amdgpu_bo_list_entry *entry;
  1197. struct amdgpu_bo *pt;
  1198. entry = &vm->page_tables[pt_idx].entry;
  1199. if (entry->robj)
  1200. continue;
  1201. r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
  1202. AMDGPU_GPU_PAGE_SIZE, true,
  1203. AMDGPU_GEM_DOMAIN_VRAM,
  1204. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  1205. AMDGPU_GEM_CREATE_SHADOW,
  1206. NULL, resv, &pt);
  1207. if (r)
  1208. goto error_free;
  1209. /* Keep a reference to the page table to avoid freeing
  1210. * them up in the wrong order.
  1211. */
  1212. pt->parent = amdgpu_bo_ref(vm->page_directory);
  1213. r = amdgpu_vm_clear_bo(adev, vm, pt);
  1214. if (r) {
  1215. amdgpu_bo_unref(&pt->shadow);
  1216. amdgpu_bo_unref(&pt);
  1217. goto error_free;
  1218. }
  1219. if (pt->shadow) {
  1220. r = amdgpu_vm_clear_bo(adev, vm, pt->shadow);
  1221. if (r) {
  1222. amdgpu_bo_unref(&pt->shadow);
  1223. amdgpu_bo_unref(&pt);
  1224. goto error_free;
  1225. }
  1226. }
  1227. entry->robj = pt;
  1228. entry->priority = 0;
  1229. entry->tv.bo = &entry->robj->tbo;
  1230. entry->tv.shared = true;
  1231. entry->user_pages = NULL;
  1232. vm->page_tables[pt_idx].addr = 0;
  1233. }
  1234. return 0;
  1235. error_free:
  1236. list_del(&mapping->list);
  1237. interval_tree_remove(&mapping->it, &vm->va);
  1238. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1239. kfree(mapping);
  1240. error:
  1241. return r;
  1242. }
  1243. /**
  1244. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1245. *
  1246. * @adev: amdgpu_device pointer
  1247. * @bo_va: bo_va to remove the address from
  1248. * @saddr: where to the BO is mapped
  1249. *
  1250. * Remove a mapping of the BO at the specefied addr from the VM.
  1251. * Returns 0 for success, error for failure.
  1252. *
  1253. * Object has to be reserved and unreserved outside!
  1254. */
  1255. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1256. struct amdgpu_bo_va *bo_va,
  1257. uint64_t saddr)
  1258. {
  1259. struct amdgpu_bo_va_mapping *mapping;
  1260. struct amdgpu_vm *vm = bo_va->vm;
  1261. bool valid = true;
  1262. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1263. list_for_each_entry(mapping, &bo_va->valids, list) {
  1264. if (mapping->it.start == saddr)
  1265. break;
  1266. }
  1267. if (&mapping->list == &bo_va->valids) {
  1268. valid = false;
  1269. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1270. if (mapping->it.start == saddr)
  1271. break;
  1272. }
  1273. if (&mapping->list == &bo_va->invalids)
  1274. return -ENOENT;
  1275. }
  1276. list_del(&mapping->list);
  1277. interval_tree_remove(&mapping->it, &vm->va);
  1278. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1279. if (valid)
  1280. list_add(&mapping->list, &vm->freed);
  1281. else
  1282. kfree(mapping);
  1283. return 0;
  1284. }
  1285. /**
  1286. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1287. *
  1288. * @adev: amdgpu_device pointer
  1289. * @bo_va: requested bo_va
  1290. *
  1291. * Remove @bo_va->bo from the requested vm.
  1292. *
  1293. * Object have to be reserved!
  1294. */
  1295. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1296. struct amdgpu_bo_va *bo_va)
  1297. {
  1298. struct amdgpu_bo_va_mapping *mapping, *next;
  1299. struct amdgpu_vm *vm = bo_va->vm;
  1300. list_del(&bo_va->bo_list);
  1301. spin_lock(&vm->status_lock);
  1302. list_del(&bo_va->vm_status);
  1303. spin_unlock(&vm->status_lock);
  1304. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1305. list_del(&mapping->list);
  1306. interval_tree_remove(&mapping->it, &vm->va);
  1307. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1308. list_add(&mapping->list, &vm->freed);
  1309. }
  1310. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1311. list_del(&mapping->list);
  1312. interval_tree_remove(&mapping->it, &vm->va);
  1313. kfree(mapping);
  1314. }
  1315. fence_put(bo_va->last_pt_update);
  1316. kfree(bo_va);
  1317. }
  1318. /**
  1319. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1320. *
  1321. * @adev: amdgpu_device pointer
  1322. * @vm: requested vm
  1323. * @bo: amdgpu buffer object
  1324. *
  1325. * Mark @bo as invalid.
  1326. */
  1327. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1328. struct amdgpu_bo *bo)
  1329. {
  1330. struct amdgpu_bo_va *bo_va;
  1331. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1332. spin_lock(&bo_va->vm->status_lock);
  1333. if (list_empty(&bo_va->vm_status))
  1334. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1335. spin_unlock(&bo_va->vm->status_lock);
  1336. }
  1337. }
  1338. /**
  1339. * amdgpu_vm_init - initialize a vm instance
  1340. *
  1341. * @adev: amdgpu_device pointer
  1342. * @vm: requested vm
  1343. *
  1344. * Init @vm fields.
  1345. */
  1346. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1347. {
  1348. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1349. AMDGPU_VM_PTE_COUNT * 8);
  1350. unsigned pd_size, pd_entries;
  1351. unsigned ring_instance;
  1352. struct amdgpu_ring *ring;
  1353. struct amd_sched_rq *rq;
  1354. int i, r;
  1355. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1356. vm->ids[i] = NULL;
  1357. vm->va = RB_ROOT;
  1358. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  1359. spin_lock_init(&vm->status_lock);
  1360. INIT_LIST_HEAD(&vm->invalidated);
  1361. INIT_LIST_HEAD(&vm->cleared);
  1362. INIT_LIST_HEAD(&vm->freed);
  1363. pd_size = amdgpu_vm_directory_size(adev);
  1364. pd_entries = amdgpu_vm_num_pdes(adev);
  1365. /* allocate page table array */
  1366. vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
  1367. if (vm->page_tables == NULL) {
  1368. DRM_ERROR("Cannot allocate memory for page table array\n");
  1369. return -ENOMEM;
  1370. }
  1371. /* create scheduler entity for page table updates */
  1372. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  1373. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  1374. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  1375. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
  1376. r = amd_sched_entity_init(&ring->sched, &vm->entity,
  1377. rq, amdgpu_sched_jobs);
  1378. if (r)
  1379. goto err;
  1380. vm->page_directory_fence = NULL;
  1381. r = amdgpu_bo_create(adev, pd_size, align, true,
  1382. AMDGPU_GEM_DOMAIN_VRAM,
  1383. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  1384. AMDGPU_GEM_CREATE_SHADOW,
  1385. NULL, NULL, &vm->page_directory);
  1386. if (r)
  1387. goto error_free_sched_entity;
  1388. r = amdgpu_bo_reserve(vm->page_directory, false);
  1389. if (r)
  1390. goto error_free_page_directory;
  1391. r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
  1392. if (r)
  1393. goto error_unreserve;
  1394. if (vm->page_directory->shadow) {
  1395. r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory->shadow);
  1396. if (r)
  1397. goto error_unreserve;
  1398. }
  1399. vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
  1400. amdgpu_bo_unreserve(vm->page_directory);
  1401. return 0;
  1402. error_unreserve:
  1403. amdgpu_bo_unreserve(vm->page_directory);
  1404. error_free_page_directory:
  1405. amdgpu_bo_unref(&vm->page_directory->shadow);
  1406. amdgpu_bo_unref(&vm->page_directory);
  1407. vm->page_directory = NULL;
  1408. error_free_sched_entity:
  1409. amd_sched_entity_fini(&ring->sched, &vm->entity);
  1410. err:
  1411. drm_free_large(vm->page_tables);
  1412. return r;
  1413. }
  1414. /**
  1415. * amdgpu_vm_fini - tear down a vm instance
  1416. *
  1417. * @adev: amdgpu_device pointer
  1418. * @vm: requested vm
  1419. *
  1420. * Tear down @vm.
  1421. * Unbind the VM and remove all bos from the vm bo list
  1422. */
  1423. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1424. {
  1425. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1426. int i;
  1427. amd_sched_entity_fini(vm->entity.sched, &vm->entity);
  1428. if (!RB_EMPTY_ROOT(&vm->va)) {
  1429. dev_err(adev->dev, "still active bo inside vm\n");
  1430. }
  1431. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1432. list_del(&mapping->list);
  1433. interval_tree_remove(&mapping->it, &vm->va);
  1434. kfree(mapping);
  1435. }
  1436. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1437. list_del(&mapping->list);
  1438. kfree(mapping);
  1439. }
  1440. for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) {
  1441. struct amdgpu_bo *pt = vm->page_tables[i].entry.robj;
  1442. if (!pt)
  1443. continue;
  1444. amdgpu_bo_unref(&pt->shadow);
  1445. amdgpu_bo_unref(&pt);
  1446. }
  1447. drm_free_large(vm->page_tables);
  1448. amdgpu_bo_unref(&vm->page_directory->shadow);
  1449. amdgpu_bo_unref(&vm->page_directory);
  1450. fence_put(vm->page_directory_fence);
  1451. }
  1452. /**
  1453. * amdgpu_vm_manager_init - init the VM manager
  1454. *
  1455. * @adev: amdgpu_device pointer
  1456. *
  1457. * Initialize the VM manager structures
  1458. */
  1459. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  1460. {
  1461. unsigned i;
  1462. INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
  1463. /* skip over VMID 0, since it is the system VM */
  1464. for (i = 1; i < adev->vm_manager.num_ids; ++i) {
  1465. amdgpu_vm_reset_id(adev, i);
  1466. amdgpu_sync_create(&adev->vm_manager.ids[i].active);
  1467. list_add_tail(&adev->vm_manager.ids[i].list,
  1468. &adev->vm_manager.ids_lru);
  1469. }
  1470. adev->vm_manager.fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
  1471. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1472. adev->vm_manager.seqno[i] = 0;
  1473. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  1474. atomic64_set(&adev->vm_manager.client_counter, 0);
  1475. }
  1476. /**
  1477. * amdgpu_vm_manager_fini - cleanup VM manager
  1478. *
  1479. * @adev: amdgpu_device pointer
  1480. *
  1481. * Cleanup the VM manager and free resources.
  1482. */
  1483. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  1484. {
  1485. unsigned i;
  1486. for (i = 0; i < AMDGPU_NUM_VM; ++i) {
  1487. struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
  1488. fence_put(adev->vm_manager.ids[i].first);
  1489. amdgpu_sync_free(&adev->vm_manager.ids[i].active);
  1490. fence_put(id->flushed_updates);
  1491. }
  1492. }