amdgpu_vm.c 36 KB

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