amdgpu_vm.c 36 KB

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