amdgpu_vm.c 34 KB

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