amdgpu_vm.c 32 KB

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