amdgpu_vm.c 35 KB

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