amdgpu_vm.c 34 KB

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