amdgpu_vm.c 37 KB

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