amdgpu_vm.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/amdgpu_drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_trace.h"
  32. /*
  33. * GPUVM
  34. * GPUVM is similar to the legacy gart on older asics, however
  35. * rather than there being a single global gart table
  36. * for the entire GPU, there are multiple VM page tables active
  37. * at any given time. The VM page tables can contain a mix
  38. * vram pages and system memory pages and system memory pages
  39. * can be mapped as snooped (cached system pages) or unsnooped
  40. * (uncached system pages).
  41. * Each VM has an ID associated with it and there is a page table
  42. * associated with each VMID. When execting a command buffer,
  43. * the kernel tells the the ring what VMID to use for that command
  44. * buffer. VMIDs are allocated dynamically as commands are submitted.
  45. * The userspace drivers maintain their own address space and the kernel
  46. * sets up their pages tables accordingly when they submit their
  47. * command buffers and a VMID is assigned.
  48. * Cayman/Trinity support up to 8 active VMs at any given time;
  49. * SI supports 16.
  50. */
  51. /**
  52. * amdgpu_vm_num_pde - return the number of page directory entries
  53. *
  54. * @adev: amdgpu_device pointer
  55. *
  56. * Calculate the number of page directory entries (cayman+).
  57. */
  58. static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
  59. {
  60. return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
  61. }
  62. /**
  63. * amdgpu_vm_directory_size - returns the size of the page directory in bytes
  64. *
  65. * @adev: amdgpu_device pointer
  66. *
  67. * Calculate the size of the page directory in bytes (cayman+).
  68. */
  69. static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
  70. {
  71. return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
  72. }
  73. /**
  74. * amdgpu_vm_get_bos - add the vm BOs to a validation list
  75. *
  76. * @vm: vm providing the BOs
  77. * @head: head of validation list
  78. *
  79. * Add the page directory to the list of BOs to
  80. * validate for command submission (cayman+).
  81. */
  82. struct amdgpu_bo_list_entry *amdgpu_vm_get_bos(struct amdgpu_device *adev,
  83. struct amdgpu_vm *vm,
  84. struct list_head *head)
  85. {
  86. struct amdgpu_bo_list_entry *list;
  87. unsigned i, idx;
  88. list = drm_malloc_ab(vm->max_pde_used + 2,
  89. sizeof(struct amdgpu_bo_list_entry));
  90. if (!list) {
  91. return NULL;
  92. }
  93. /* add the vm page table to the list */
  94. list[0].robj = vm->page_directory;
  95. list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
  96. list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
  97. list[0].priority = 0;
  98. list[0].tv.bo = &vm->page_directory->tbo;
  99. list[0].tv.shared = true;
  100. list_add(&list[0].tv.head, head);
  101. for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
  102. if (!vm->page_tables[i].bo)
  103. continue;
  104. list[idx].robj = vm->page_tables[i].bo;
  105. list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
  106. list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
  107. list[idx].priority = 0;
  108. list[idx].tv.bo = &list[idx].robj->tbo;
  109. list[idx].tv.shared = true;
  110. list_add(&list[idx++].tv.head, head);
  111. }
  112. return list;
  113. }
  114. /**
  115. * amdgpu_vm_grab_id - allocate the next free VMID
  116. *
  117. * @vm: vm to allocate id for
  118. * @ring: ring we want to submit job to
  119. * @sync: sync object where we add dependencies
  120. *
  121. * Allocate an id for the vm, adding fences to the sync obj as necessary.
  122. *
  123. * Global mutex must be locked!
  124. */
  125. int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  126. struct amdgpu_sync *sync)
  127. {
  128. struct fence *best[AMDGPU_MAX_RINGS] = {};
  129. struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
  130. struct amdgpu_device *adev = ring->adev;
  131. unsigned choices[2] = {};
  132. unsigned i;
  133. /* check if the id is still valid */
  134. if (vm_id->id) {
  135. unsigned id = vm_id->id;
  136. long owner;
  137. owner = atomic_long_read(&adev->vm_manager.ids[id].owner);
  138. if (owner == (long)vm) {
  139. trace_amdgpu_vm_grab_id(vm_id->id, ring->idx);
  140. return 0;
  141. }
  142. }
  143. /* we definately need to flush */
  144. vm_id->pd_gpu_addr = ~0ll;
  145. /* skip over VMID 0, since it is the system VM */
  146. for (i = 1; i < adev->vm_manager.nvm; ++i) {
  147. struct fence *fence = adev->vm_manager.ids[i].active;
  148. struct amdgpu_ring *fring;
  149. if (fence == NULL) {
  150. /* found a free one */
  151. vm_id->id = i;
  152. trace_amdgpu_vm_grab_id(i, ring->idx);
  153. return 0;
  154. }
  155. fring = amdgpu_ring_from_fence(fence);
  156. if (best[fring->idx] == NULL ||
  157. fence_is_later(best[fring->idx], fence)) {
  158. best[fring->idx] = fence;
  159. choices[fring == ring ? 0 : 1] = i;
  160. }
  161. }
  162. for (i = 0; i < 2; ++i) {
  163. if (choices[i]) {
  164. struct fence *fence;
  165. fence = adev->vm_manager.ids[choices[i]].active;
  166. vm_id->id = choices[i];
  167. trace_amdgpu_vm_grab_id(choices[i], ring->idx);
  168. return amdgpu_sync_fence(ring->adev, sync, fence);
  169. }
  170. }
  171. /* should never happen */
  172. BUG();
  173. return -EINVAL;
  174. }
  175. /**
  176. * amdgpu_vm_flush - hardware flush the vm
  177. *
  178. * @ring: ring to use for flush
  179. * @vm: vm we want to flush
  180. * @updates: last vm update that we waited for
  181. *
  182. * Flush the vm (cayman+).
  183. *
  184. * Global and local mutex must be locked!
  185. */
  186. void amdgpu_vm_flush(struct amdgpu_ring *ring,
  187. struct amdgpu_vm *vm,
  188. struct fence *updates)
  189. {
  190. uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  191. struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
  192. struct fence *flushed_updates = vm_id->flushed_updates;
  193. bool is_later;
  194. if (!flushed_updates)
  195. is_later = true;
  196. else if (!updates)
  197. is_later = false;
  198. else
  199. is_later = fence_is_later(updates, flushed_updates);
  200. if (pd_addr != vm_id->pd_gpu_addr || is_later) {
  201. trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
  202. if (is_later) {
  203. vm_id->flushed_updates = fence_get(updates);
  204. fence_put(flushed_updates);
  205. }
  206. vm_id->pd_gpu_addr = pd_addr;
  207. amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
  208. }
  209. }
  210. /**
  211. * amdgpu_vm_fence - remember fence for vm
  212. *
  213. * @adev: amdgpu_device pointer
  214. * @vm: vm we want to fence
  215. * @fence: fence to remember
  216. *
  217. * Fence the vm (cayman+).
  218. * Set the fence used to protect page table and id.
  219. *
  220. * Global and local mutex must be locked!
  221. */
  222. void amdgpu_vm_fence(struct amdgpu_device *adev,
  223. struct amdgpu_vm *vm,
  224. struct fence *fence)
  225. {
  226. struct amdgpu_ring *ring = amdgpu_ring_from_fence(fence);
  227. unsigned vm_id = vm->ids[ring->idx].id;
  228. fence_put(adev->vm_manager.ids[vm_id].active);
  229. adev->vm_manager.ids[vm_id].active = fence_get(fence);
  230. atomic_long_set(&adev->vm_manager.ids[vm_id].owner, (long)vm);
  231. }
  232. /**
  233. * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
  234. *
  235. * @vm: requested vm
  236. * @bo: requested buffer object
  237. *
  238. * Find @bo inside the requested vm (cayman+).
  239. * Search inside the @bos vm list for the requested vm
  240. * Returns the found bo_va or NULL if none is found
  241. *
  242. * Object has to be reserved!
  243. */
  244. struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
  245. struct amdgpu_bo *bo)
  246. {
  247. struct amdgpu_bo_va *bo_va;
  248. list_for_each_entry(bo_va, &bo->va, bo_list) {
  249. if (bo_va->vm == vm) {
  250. return bo_va;
  251. }
  252. }
  253. return NULL;
  254. }
  255. /**
  256. * amdgpu_vm_update_pages - helper to call the right asic function
  257. *
  258. * @adev: amdgpu_device pointer
  259. * @ib: indirect buffer to fill with commands
  260. * @pe: addr of the page entry
  261. * @addr: dst addr to write into pe
  262. * @count: number of page entries to update
  263. * @incr: increase next addr by incr bytes
  264. * @flags: hw access flags
  265. * @gtt_flags: GTT hw access flags
  266. *
  267. * Traces the parameters and calls the right asic functions
  268. * to setup the page table using the DMA.
  269. */
  270. static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
  271. struct amdgpu_ib *ib,
  272. uint64_t pe, uint64_t addr,
  273. unsigned count, uint32_t incr,
  274. uint32_t flags, uint32_t gtt_flags)
  275. {
  276. trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
  277. if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
  278. uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
  279. amdgpu_vm_copy_pte(adev, ib, pe, src, count);
  280. } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
  281. amdgpu_vm_write_pte(adev, ib, pe, addr,
  282. count, incr, flags);
  283. } else {
  284. amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
  285. count, incr, flags);
  286. }
  287. }
  288. int amdgpu_vm_free_job(struct amdgpu_job *job)
  289. {
  290. int i;
  291. for (i = 0; i < job->num_ibs; i++)
  292. amdgpu_ib_free(job->adev, &job->ibs[i]);
  293. kfree(job->ibs);
  294. return 0;
  295. }
  296. /**
  297. * amdgpu_vm_clear_bo - initially clear the page dir/table
  298. *
  299. * @adev: amdgpu_device pointer
  300. * @bo: bo to clear
  301. *
  302. * need to reserve bo first before calling it.
  303. */
  304. static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
  305. struct amdgpu_bo *bo)
  306. {
  307. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  308. struct fence *fence = NULL;
  309. struct amdgpu_ib *ib;
  310. unsigned entries;
  311. uint64_t addr;
  312. int r;
  313. r = reservation_object_reserve_shared(bo->tbo.resv);
  314. if (r)
  315. return r;
  316. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  317. if (r)
  318. goto error;
  319. addr = amdgpu_bo_gpu_offset(bo);
  320. entries = amdgpu_bo_size(bo) / 8;
  321. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  322. if (!ib)
  323. goto error;
  324. r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
  325. if (r)
  326. goto error_free;
  327. ib->length_dw = 0;
  328. amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0);
  329. amdgpu_vm_pad_ib(adev, ib);
  330. WARN_ON(ib->length_dw > 64);
  331. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  332. &amdgpu_vm_free_job,
  333. AMDGPU_FENCE_OWNER_VM,
  334. &fence);
  335. if (!r)
  336. amdgpu_bo_fence(bo, fence, true);
  337. fence_put(fence);
  338. if (amdgpu_enable_scheduler)
  339. return 0;
  340. error_free:
  341. amdgpu_ib_free(adev, ib);
  342. kfree(ib);
  343. error:
  344. return r;
  345. }
  346. /**
  347. * amdgpu_vm_map_gart - get the physical address of a gart page
  348. *
  349. * @adev: amdgpu_device pointer
  350. * @addr: the unmapped addr
  351. *
  352. * Look up the physical address of the page that the pte resolves
  353. * to (cayman+).
  354. * Returns the physical address of the page.
  355. */
  356. uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
  357. {
  358. uint64_t result;
  359. /* page table offset */
  360. result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
  361. /* in case cpu page size != gpu page size*/
  362. result |= addr & (~PAGE_MASK);
  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. /* update too big for an IB */
  396. if (ndw > 0xfffff)
  397. return -ENOMEM;
  398. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  399. if (!ib)
  400. return -ENOMEM;
  401. r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
  402. if (r) {
  403. kfree(ib);
  404. return r;
  405. }
  406. ib->length_dw = 0;
  407. /* walk over the address space and update the page directory */
  408. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  409. struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
  410. uint64_t pde, pt;
  411. if (bo == NULL)
  412. continue;
  413. pt = amdgpu_bo_gpu_offset(bo);
  414. if (vm->page_tables[pt_idx].addr == pt)
  415. continue;
  416. vm->page_tables[pt_idx].addr = pt;
  417. pde = pd_addr + pt_idx * 8;
  418. if (((last_pde + 8 * count) != pde) ||
  419. ((last_pt + incr * count) != pt)) {
  420. if (count) {
  421. amdgpu_vm_update_pages(adev, ib, last_pde,
  422. last_pt, count, incr,
  423. AMDGPU_PTE_VALID, 0);
  424. }
  425. count = 1;
  426. last_pde = pde;
  427. last_pt = pt;
  428. } else {
  429. ++count;
  430. }
  431. }
  432. if (count)
  433. amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count,
  434. incr, AMDGPU_PTE_VALID, 0);
  435. if (ib->length_dw != 0) {
  436. amdgpu_vm_pad_ib(adev, ib);
  437. amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
  438. WARN_ON(ib->length_dw > ndw);
  439. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  440. &amdgpu_vm_free_job,
  441. AMDGPU_FENCE_OWNER_VM,
  442. &fence);
  443. if (r)
  444. goto error_free;
  445. amdgpu_bo_fence(pd, fence, true);
  446. fence_put(vm->page_directory_fence);
  447. vm->page_directory_fence = fence_get(fence);
  448. fence_put(fence);
  449. }
  450. if (!amdgpu_enable_scheduler || ib->length_dw == 0) {
  451. amdgpu_ib_free(adev, ib);
  452. kfree(ib);
  453. }
  454. return 0;
  455. error_free:
  456. amdgpu_ib_free(adev, ib);
  457. kfree(ib);
  458. return r;
  459. }
  460. /**
  461. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  462. *
  463. * @adev: amdgpu_device pointer
  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. * @gtt_flags: GTT hw mapping flags
  470. *
  471. * Global and local mutex must be locked!
  472. */
  473. static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
  474. struct amdgpu_ib *ib,
  475. uint64_t pe_start, uint64_t pe_end,
  476. uint64_t addr, uint32_t flags,
  477. uint32_t gtt_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 ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
  505. (frag_start >= frag_end)) {
  506. count = (pe_end - pe_start) / 8;
  507. amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
  508. AMDGPU_GPU_PAGE_SIZE, flags, gtt_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, ib, pe_start, addr, count,
  515. AMDGPU_GPU_PAGE_SIZE, flags, gtt_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, ib, frag_start, addr, count,
  521. AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
  522. gtt_flags);
  523. /* handle the 4K area at the end */
  524. if (frag_end != pe_end) {
  525. addr += AMDGPU_GPU_PAGE_SIZE * count;
  526. count = (pe_end - frag_end) / 8;
  527. amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
  528. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  529. }
  530. }
  531. /**
  532. * amdgpu_vm_update_ptes - make sure that page tables are valid
  533. *
  534. * @adev: amdgpu_device pointer
  535. * @vm: requested vm
  536. * @start: start of GPU address range
  537. * @end: end of GPU address range
  538. * @dst: destination address to map to
  539. * @flags: mapping flags
  540. *
  541. * Update the page tables in the range @start - @end (cayman+).
  542. *
  543. * Global and local mutex must be locked!
  544. */
  545. static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
  546. struct amdgpu_vm *vm,
  547. struct amdgpu_ib *ib,
  548. uint64_t start, uint64_t end,
  549. uint64_t dst, uint32_t flags,
  550. uint32_t gtt_flags)
  551. {
  552. uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  553. uint64_t last_pte = ~0, last_dst = ~0;
  554. void *owner = AMDGPU_FENCE_OWNER_VM;
  555. unsigned count = 0;
  556. uint64_t addr;
  557. /* sync to everything on unmapping */
  558. if (!(flags & AMDGPU_PTE_VALID))
  559. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  560. /* walk over the address space and update the page tables */
  561. for (addr = start; addr < end; ) {
  562. uint64_t pt_idx = addr >> amdgpu_vm_block_size;
  563. struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo;
  564. unsigned nptes;
  565. uint64_t pte;
  566. int r;
  567. amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv, owner);
  568. r = reservation_object_reserve_shared(pt->tbo.resv);
  569. if (r)
  570. return r;
  571. if ((addr & ~mask) == (end & ~mask))
  572. nptes = end - addr;
  573. else
  574. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  575. pte = amdgpu_bo_gpu_offset(pt);
  576. pte += (addr & mask) * 8;
  577. if ((last_pte + 8 * count) != pte) {
  578. if (count) {
  579. amdgpu_vm_frag_ptes(adev, ib, last_pte,
  580. last_pte + 8 * count,
  581. last_dst, flags,
  582. gtt_flags);
  583. }
  584. count = nptes;
  585. last_pte = pte;
  586. last_dst = dst;
  587. } else {
  588. count += nptes;
  589. }
  590. addr += nptes;
  591. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  592. }
  593. if (count) {
  594. amdgpu_vm_frag_ptes(adev, ib, last_pte,
  595. last_pte + 8 * count,
  596. last_dst, flags, gtt_flags);
  597. }
  598. return 0;
  599. }
  600. /**
  601. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  602. *
  603. * @adev: amdgpu_device pointer
  604. * @vm: requested vm
  605. * @mapping: mapped range and flags to use for the update
  606. * @addr: addr to set the area to
  607. * @gtt_flags: flags as they are used for GTT
  608. * @fence: optional resulting fence
  609. *
  610. * Fill in the page table entries for @mapping.
  611. * Returns 0 for success, -EINVAL for failure.
  612. *
  613. * Object have to be reserved and mutex must be locked!
  614. */
  615. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  616. struct amdgpu_vm *vm,
  617. struct amdgpu_bo_va_mapping *mapping,
  618. uint64_t addr, uint32_t gtt_flags,
  619. struct fence **fence)
  620. {
  621. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  622. unsigned nptes, ncmds, ndw;
  623. uint32_t flags = gtt_flags;
  624. struct amdgpu_ib *ib;
  625. struct fence *f = NULL;
  626. int r;
  627. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  628. * but in case of something, we filter the flags in first place
  629. */
  630. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  631. flags &= ~AMDGPU_PTE_READABLE;
  632. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  633. flags &= ~AMDGPU_PTE_WRITEABLE;
  634. trace_amdgpu_vm_bo_update(mapping);
  635. nptes = mapping->it.last - mapping->it.start + 1;
  636. /*
  637. * reserve space for one command every (1 << BLOCK_SIZE)
  638. * entries or 2k dwords (whatever is smaller)
  639. */
  640. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  641. /* padding, etc. */
  642. ndw = 64;
  643. if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
  644. /* only copy commands needed */
  645. ndw += ncmds * 7;
  646. } else if (flags & AMDGPU_PTE_SYSTEM) {
  647. /* header for write data commands */
  648. ndw += ncmds * 4;
  649. /* body of write data command */
  650. ndw += nptes * 2;
  651. } else {
  652. /* set page commands needed */
  653. ndw += ncmds * 10;
  654. /* two extra commands for begin/end of fragment */
  655. ndw += 2 * 10;
  656. }
  657. /* update too big for an IB */
  658. if (ndw > 0xfffff)
  659. return -ENOMEM;
  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, vm, ib, mapping->it.start,
  670. mapping->it.last + 1, addr + mapping->offset,
  671. flags, gtt_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. if (!amdgpu_enable_scheduler) {
  692. amdgpu_ib_free(adev, ib);
  693. kfree(ib);
  694. }
  695. return 0;
  696. error_free:
  697. amdgpu_ib_free(adev, ib);
  698. kfree(ib);
  699. return r;
  700. }
  701. /**
  702. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  703. *
  704. * @adev: amdgpu_device pointer
  705. * @bo_va: requested BO and VM object
  706. * @mem: ttm mem
  707. *
  708. * Fill in the page table entries for @bo_va.
  709. * Returns 0 for success, -EINVAL for failure.
  710. *
  711. * Object have to be reserved and mutex must be locked!
  712. */
  713. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  714. struct amdgpu_bo_va *bo_va,
  715. struct ttm_mem_reg *mem)
  716. {
  717. struct amdgpu_vm *vm = bo_va->vm;
  718. struct amdgpu_bo_va_mapping *mapping;
  719. uint32_t flags;
  720. uint64_t addr;
  721. int r;
  722. if (mem) {
  723. addr = (u64)mem->start << PAGE_SHIFT;
  724. if (mem->mem_type != TTM_PL_TT)
  725. addr += adev->vm_manager.vram_base_offset;
  726. } else {
  727. addr = 0;
  728. }
  729. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  730. spin_lock(&vm->status_lock);
  731. if (!list_empty(&bo_va->vm_status))
  732. list_splice_init(&bo_va->valids, &bo_va->invalids);
  733. spin_unlock(&vm->status_lock);
  734. list_for_each_entry(mapping, &bo_va->invalids, list) {
  735. r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
  736. flags, &bo_va->last_pt_update);
  737. if (r)
  738. return r;
  739. }
  740. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  741. list_for_each_entry(mapping, &bo_va->valids, list)
  742. trace_amdgpu_vm_bo_mapping(mapping);
  743. list_for_each_entry(mapping, &bo_va->invalids, list)
  744. trace_amdgpu_vm_bo_mapping(mapping);
  745. }
  746. spin_lock(&vm->status_lock);
  747. list_splice_init(&bo_va->invalids, &bo_va->valids);
  748. list_del_init(&bo_va->vm_status);
  749. if (!mem)
  750. list_add(&bo_va->vm_status, &vm->cleared);
  751. spin_unlock(&vm->status_lock);
  752. return 0;
  753. }
  754. /**
  755. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  756. *
  757. * @adev: amdgpu_device pointer
  758. * @vm: requested vm
  759. *
  760. * Make sure all freed BOs are cleared in the PT.
  761. * Returns 0 for success.
  762. *
  763. * PTs have to be reserved and mutex must be locked!
  764. */
  765. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  766. struct amdgpu_vm *vm)
  767. {
  768. struct amdgpu_bo_va_mapping *mapping;
  769. int r;
  770. spin_lock(&vm->freed_lock);
  771. while (!list_empty(&vm->freed)) {
  772. mapping = list_first_entry(&vm->freed,
  773. struct amdgpu_bo_va_mapping, list);
  774. list_del(&mapping->list);
  775. spin_unlock(&vm->freed_lock);
  776. r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
  777. kfree(mapping);
  778. if (r)
  779. return r;
  780. spin_lock(&vm->freed_lock);
  781. }
  782. spin_unlock(&vm->freed_lock);
  783. return 0;
  784. }
  785. /**
  786. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  787. *
  788. * @adev: amdgpu_device pointer
  789. * @vm: requested vm
  790. *
  791. * Make sure all invalidated BOs are cleared in the PT.
  792. * Returns 0 for success.
  793. *
  794. * PTs have to be reserved and mutex must be locked!
  795. */
  796. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  797. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  798. {
  799. struct amdgpu_bo_va *bo_va = NULL;
  800. int r = 0;
  801. spin_lock(&vm->status_lock);
  802. while (!list_empty(&vm->invalidated)) {
  803. bo_va = list_first_entry(&vm->invalidated,
  804. struct amdgpu_bo_va, vm_status);
  805. spin_unlock(&vm->status_lock);
  806. mutex_lock(&bo_va->mutex);
  807. r = amdgpu_vm_bo_update(adev, bo_va, NULL);
  808. mutex_unlock(&bo_va->mutex);
  809. if (r)
  810. return r;
  811. spin_lock(&vm->status_lock);
  812. }
  813. spin_unlock(&vm->status_lock);
  814. if (bo_va)
  815. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  816. return r;
  817. }
  818. /**
  819. * amdgpu_vm_bo_add - add a bo to a specific vm
  820. *
  821. * @adev: amdgpu_device pointer
  822. * @vm: requested vm
  823. * @bo: amdgpu buffer object
  824. *
  825. * Add @bo into the requested vm (cayman+).
  826. * Add @bo to the list of bos associated with the vm
  827. * Returns newly added bo_va or NULL for failure
  828. *
  829. * Object has to be reserved!
  830. */
  831. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  832. struct amdgpu_vm *vm,
  833. struct amdgpu_bo *bo)
  834. {
  835. struct amdgpu_bo_va *bo_va;
  836. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  837. if (bo_va == NULL) {
  838. return NULL;
  839. }
  840. bo_va->vm = vm;
  841. bo_va->bo = bo;
  842. bo_va->ref_count = 1;
  843. INIT_LIST_HEAD(&bo_va->bo_list);
  844. INIT_LIST_HEAD(&bo_va->valids);
  845. INIT_LIST_HEAD(&bo_va->invalids);
  846. INIT_LIST_HEAD(&bo_va->vm_status);
  847. mutex_init(&bo_va->mutex);
  848. list_add_tail(&bo_va->bo_list, &bo->va);
  849. return bo_va;
  850. }
  851. /**
  852. * amdgpu_vm_bo_map - map bo inside a vm
  853. *
  854. * @adev: amdgpu_device pointer
  855. * @bo_va: bo_va to store the address
  856. * @saddr: where to map the BO
  857. * @offset: requested offset in the BO
  858. * @flags: attributes of pages (read/write/valid/etc.)
  859. *
  860. * Add a mapping of the BO at the specefied addr into the VM.
  861. * Returns 0 for success, error for failure.
  862. *
  863. * Object has to be reserved and unreserved outside!
  864. */
  865. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  866. struct amdgpu_bo_va *bo_va,
  867. uint64_t saddr, uint64_t offset,
  868. uint64_t size, uint32_t flags)
  869. {
  870. struct amdgpu_bo_va_mapping *mapping;
  871. struct amdgpu_vm *vm = bo_va->vm;
  872. struct interval_tree_node *it;
  873. unsigned last_pfn, pt_idx;
  874. uint64_t eaddr;
  875. int r;
  876. /* validate the parameters */
  877. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  878. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  879. return -EINVAL;
  880. /* make sure object fit at this offset */
  881. eaddr = saddr + size - 1;
  882. if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
  883. return -EINVAL;
  884. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  885. if (last_pfn >= adev->vm_manager.max_pfn) {
  886. dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
  887. last_pfn, adev->vm_manager.max_pfn);
  888. return -EINVAL;
  889. }
  890. saddr /= AMDGPU_GPU_PAGE_SIZE;
  891. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  892. spin_lock(&vm->it_lock);
  893. it = interval_tree_iter_first(&vm->va, saddr, eaddr);
  894. spin_unlock(&vm->it_lock);
  895. if (it) {
  896. struct amdgpu_bo_va_mapping *tmp;
  897. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  898. /* bo and tmp overlap, invalid addr */
  899. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  900. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  901. tmp->it.start, tmp->it.last + 1);
  902. r = -EINVAL;
  903. goto error;
  904. }
  905. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  906. if (!mapping) {
  907. r = -ENOMEM;
  908. goto error;
  909. }
  910. INIT_LIST_HEAD(&mapping->list);
  911. mapping->it.start = saddr;
  912. mapping->it.last = eaddr;
  913. mapping->offset = offset;
  914. mapping->flags = flags;
  915. mutex_lock(&bo_va->mutex);
  916. list_add(&mapping->list, &bo_va->invalids);
  917. mutex_unlock(&bo_va->mutex);
  918. spin_lock(&vm->it_lock);
  919. interval_tree_insert(&mapping->it, &vm->va);
  920. spin_unlock(&vm->it_lock);
  921. trace_amdgpu_vm_bo_map(bo_va, mapping);
  922. /* Make sure the page tables are allocated */
  923. saddr >>= amdgpu_vm_block_size;
  924. eaddr >>= amdgpu_vm_block_size;
  925. BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
  926. if (eaddr > vm->max_pde_used)
  927. vm->max_pde_used = eaddr;
  928. /* walk over the address space and allocate the page tables */
  929. for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
  930. struct reservation_object *resv = vm->page_directory->tbo.resv;
  931. struct amdgpu_bo *pt;
  932. if (vm->page_tables[pt_idx].bo)
  933. continue;
  934. r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
  935. AMDGPU_GPU_PAGE_SIZE, true,
  936. AMDGPU_GEM_DOMAIN_VRAM,
  937. AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
  938. NULL, resv, &pt);
  939. if (r)
  940. goto error_free;
  941. /* Keep a reference to the page table to avoid freeing
  942. * them up in the wrong order.
  943. */
  944. pt->parent = amdgpu_bo_ref(vm->page_directory);
  945. r = amdgpu_vm_clear_bo(adev, pt);
  946. if (r) {
  947. amdgpu_bo_unref(&pt);
  948. goto error_free;
  949. }
  950. vm->page_tables[pt_idx].addr = 0;
  951. vm->page_tables[pt_idx].bo = pt;
  952. }
  953. return 0;
  954. error_free:
  955. list_del(&mapping->list);
  956. spin_lock(&vm->it_lock);
  957. interval_tree_remove(&mapping->it, &vm->va);
  958. spin_unlock(&vm->it_lock);
  959. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  960. kfree(mapping);
  961. error:
  962. return r;
  963. }
  964. /**
  965. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  966. *
  967. * @adev: amdgpu_device pointer
  968. * @bo_va: bo_va to remove the address from
  969. * @saddr: where to the BO is mapped
  970. *
  971. * Remove a mapping of the BO at the specefied addr from the VM.
  972. * Returns 0 for success, error for failure.
  973. *
  974. * Object has to be reserved and unreserved outside!
  975. */
  976. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  977. struct amdgpu_bo_va *bo_va,
  978. uint64_t saddr)
  979. {
  980. struct amdgpu_bo_va_mapping *mapping;
  981. struct amdgpu_vm *vm = bo_va->vm;
  982. bool valid = true;
  983. saddr /= AMDGPU_GPU_PAGE_SIZE;
  984. mutex_lock(&bo_va->mutex);
  985. list_for_each_entry(mapping, &bo_va->valids, list) {
  986. if (mapping->it.start == saddr)
  987. break;
  988. }
  989. if (&mapping->list == &bo_va->valids) {
  990. valid = false;
  991. list_for_each_entry(mapping, &bo_va->invalids, list) {
  992. if (mapping->it.start == saddr)
  993. break;
  994. }
  995. if (&mapping->list == &bo_va->invalids) {
  996. mutex_unlock(&bo_va->mutex);
  997. return -ENOENT;
  998. }
  999. }
  1000. mutex_unlock(&bo_va->mutex);
  1001. list_del(&mapping->list);
  1002. spin_lock(&vm->it_lock);
  1003. interval_tree_remove(&mapping->it, &vm->va);
  1004. spin_unlock(&vm->it_lock);
  1005. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1006. if (valid) {
  1007. spin_lock(&vm->freed_lock);
  1008. list_add(&mapping->list, &vm->freed);
  1009. spin_unlock(&vm->freed_lock);
  1010. } else {
  1011. kfree(mapping);
  1012. }
  1013. return 0;
  1014. }
  1015. /**
  1016. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1017. *
  1018. * @adev: amdgpu_device pointer
  1019. * @bo_va: requested bo_va
  1020. *
  1021. * Remove @bo_va->bo from the requested vm (cayman+).
  1022. *
  1023. * Object have to be reserved!
  1024. */
  1025. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1026. struct amdgpu_bo_va *bo_va)
  1027. {
  1028. struct amdgpu_bo_va_mapping *mapping, *next;
  1029. struct amdgpu_vm *vm = bo_va->vm;
  1030. list_del(&bo_va->bo_list);
  1031. spin_lock(&vm->status_lock);
  1032. list_del(&bo_va->vm_status);
  1033. spin_unlock(&vm->status_lock);
  1034. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1035. list_del(&mapping->list);
  1036. spin_lock(&vm->it_lock);
  1037. interval_tree_remove(&mapping->it, &vm->va);
  1038. spin_unlock(&vm->it_lock);
  1039. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1040. spin_lock(&vm->freed_lock);
  1041. list_add(&mapping->list, &vm->freed);
  1042. spin_unlock(&vm->freed_lock);
  1043. }
  1044. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1045. list_del(&mapping->list);
  1046. spin_lock(&vm->it_lock);
  1047. interval_tree_remove(&mapping->it, &vm->va);
  1048. spin_unlock(&vm->it_lock);
  1049. kfree(mapping);
  1050. }
  1051. fence_put(bo_va->last_pt_update);
  1052. mutex_destroy(&bo_va->mutex);
  1053. kfree(bo_va);
  1054. }
  1055. /**
  1056. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1057. *
  1058. * @adev: amdgpu_device pointer
  1059. * @vm: requested vm
  1060. * @bo: amdgpu buffer object
  1061. *
  1062. * Mark @bo as invalid (cayman+).
  1063. */
  1064. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1065. struct amdgpu_bo *bo)
  1066. {
  1067. struct amdgpu_bo_va *bo_va;
  1068. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1069. spin_lock(&bo_va->vm->status_lock);
  1070. if (list_empty(&bo_va->vm_status))
  1071. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1072. spin_unlock(&bo_va->vm->status_lock);
  1073. }
  1074. }
  1075. /**
  1076. * amdgpu_vm_init - initialize a vm instance
  1077. *
  1078. * @adev: amdgpu_device pointer
  1079. * @vm: requested vm
  1080. *
  1081. * Init @vm fields (cayman+).
  1082. */
  1083. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1084. {
  1085. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1086. AMDGPU_VM_PTE_COUNT * 8);
  1087. unsigned pd_size, pd_entries;
  1088. int i, r;
  1089. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1090. vm->ids[i].id = 0;
  1091. vm->ids[i].flushed_updates = NULL;
  1092. }
  1093. vm->va = RB_ROOT;
  1094. spin_lock_init(&vm->status_lock);
  1095. INIT_LIST_HEAD(&vm->invalidated);
  1096. INIT_LIST_HEAD(&vm->cleared);
  1097. INIT_LIST_HEAD(&vm->freed);
  1098. spin_lock_init(&vm->it_lock);
  1099. spin_lock_init(&vm->freed_lock);
  1100. pd_size = amdgpu_vm_directory_size(adev);
  1101. pd_entries = amdgpu_vm_num_pdes(adev);
  1102. /* allocate page table array */
  1103. vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
  1104. if (vm->page_tables == NULL) {
  1105. DRM_ERROR("Cannot allocate memory for page table array\n");
  1106. return -ENOMEM;
  1107. }
  1108. vm->page_directory_fence = NULL;
  1109. r = amdgpu_bo_create(adev, pd_size, align, true,
  1110. AMDGPU_GEM_DOMAIN_VRAM,
  1111. AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
  1112. NULL, NULL, &vm->page_directory);
  1113. if (r)
  1114. return r;
  1115. r = amdgpu_bo_reserve(vm->page_directory, false);
  1116. if (r) {
  1117. amdgpu_bo_unref(&vm->page_directory);
  1118. vm->page_directory = NULL;
  1119. return r;
  1120. }
  1121. r = amdgpu_vm_clear_bo(adev, vm->page_directory);
  1122. amdgpu_bo_unreserve(vm->page_directory);
  1123. if (r) {
  1124. amdgpu_bo_unref(&vm->page_directory);
  1125. vm->page_directory = NULL;
  1126. return r;
  1127. }
  1128. return 0;
  1129. }
  1130. /**
  1131. * amdgpu_vm_fini - tear down a vm instance
  1132. *
  1133. * @adev: amdgpu_device pointer
  1134. * @vm: requested vm
  1135. *
  1136. * Tear down @vm (cayman+).
  1137. * Unbind the VM and remove all bos from the vm bo list
  1138. */
  1139. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1140. {
  1141. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1142. int i;
  1143. if (!RB_EMPTY_ROOT(&vm->va)) {
  1144. dev_err(adev->dev, "still active bo inside vm\n");
  1145. }
  1146. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1147. list_del(&mapping->list);
  1148. interval_tree_remove(&mapping->it, &vm->va);
  1149. kfree(mapping);
  1150. }
  1151. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1152. list_del(&mapping->list);
  1153. kfree(mapping);
  1154. }
  1155. for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
  1156. amdgpu_bo_unref(&vm->page_tables[i].bo);
  1157. drm_free_large(vm->page_tables);
  1158. amdgpu_bo_unref(&vm->page_directory);
  1159. fence_put(vm->page_directory_fence);
  1160. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1161. unsigned id = vm->ids[i].id;
  1162. atomic_long_cmpxchg(&adev->vm_manager.ids[id].owner,
  1163. (long)vm, 0);
  1164. fence_put(vm->ids[i].flushed_updates);
  1165. }
  1166. }
  1167. /**
  1168. * amdgpu_vm_manager_fini - cleanup VM manager
  1169. *
  1170. * @adev: amdgpu_device pointer
  1171. *
  1172. * Cleanup the VM manager and free resources.
  1173. */
  1174. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  1175. {
  1176. unsigned i;
  1177. for (i = 0; i < AMDGPU_NUM_VM; ++i)
  1178. fence_put(adev->vm_manager.ids[i].active);
  1179. }