amdgpu_vm.c 40 KB

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