amdgpu_vm.c 38 KB

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