amdgpu_vm.c 34 KB

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