amdgpu_vm.c 38 KB

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