amdgpu_vm.c 36 KB

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