amdgpu_vm.c 38 KB

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