amdgpu_vm.c 42 KB

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