amdgpu_vm.c 33 KB

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