amdgpu_vm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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) || (count < 3)) {
  270. amdgpu_vm_write_pte(adev, ib, pe, addr,
  271. count, incr, flags);
  272. } else {
  273. amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
  274. count, incr, flags);
  275. }
  276. }
  277. int amdgpu_vm_free_job(struct amdgpu_job *job)
  278. {
  279. int i;
  280. for (i = 0; i < job->num_ibs; i++)
  281. amdgpu_ib_free(job->adev, &job->ibs[i]);
  282. kfree(job->ibs);
  283. return 0;
  284. }
  285. /**
  286. * amdgpu_vm_clear_bo - initially clear the page dir/table
  287. *
  288. * @adev: amdgpu_device pointer
  289. * @bo: bo to clear
  290. *
  291. * need to reserve bo first before calling it.
  292. */
  293. static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
  294. struct amdgpu_bo *bo)
  295. {
  296. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  297. struct fence *fence = NULL;
  298. struct amdgpu_ib *ib;
  299. unsigned entries;
  300. uint64_t addr;
  301. int r;
  302. r = reservation_object_reserve_shared(bo->tbo.resv);
  303. if (r)
  304. return r;
  305. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  306. if (r)
  307. goto error;
  308. addr = amdgpu_bo_gpu_offset(bo);
  309. entries = amdgpu_bo_size(bo) / 8;
  310. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  311. if (!ib)
  312. goto error;
  313. r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
  314. if (r)
  315. goto error_free;
  316. ib->length_dw = 0;
  317. amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0);
  318. amdgpu_vm_pad_ib(adev, ib);
  319. WARN_ON(ib->length_dw > 64);
  320. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  321. &amdgpu_vm_free_job,
  322. AMDGPU_FENCE_OWNER_VM,
  323. &fence);
  324. if (!r)
  325. amdgpu_bo_fence(bo, fence, true);
  326. fence_put(fence);
  327. return 0;
  328. error_free:
  329. amdgpu_ib_free(adev, ib);
  330. kfree(ib);
  331. error:
  332. return r;
  333. }
  334. /**
  335. * amdgpu_vm_map_gart - get the physical address of a gart page
  336. *
  337. * @adev: amdgpu_device pointer
  338. * @addr: the unmapped addr
  339. *
  340. * Look up the physical address of the page that the pte resolves
  341. * to (cayman+).
  342. * Returns the physical address of the page.
  343. */
  344. uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
  345. {
  346. uint64_t result;
  347. /* page table offset */
  348. result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
  349. /* in case cpu page size != gpu page size*/
  350. result |= addr & (~PAGE_MASK);
  351. return result;
  352. }
  353. /**
  354. * amdgpu_vm_update_pdes - make sure that page directory is valid
  355. *
  356. * @adev: amdgpu_device pointer
  357. * @vm: requested vm
  358. * @start: start of GPU address range
  359. * @end: end of GPU address range
  360. *
  361. * Allocates new page tables if necessary
  362. * and updates the page directory (cayman+).
  363. * Returns 0 for success, error for failure.
  364. *
  365. * Global and local mutex must be locked!
  366. */
  367. int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
  368. struct amdgpu_vm *vm)
  369. {
  370. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  371. struct amdgpu_bo *pd = vm->page_directory;
  372. uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
  373. uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
  374. uint64_t last_pde = ~0, last_pt = ~0;
  375. unsigned count = 0, pt_idx, ndw;
  376. struct amdgpu_ib *ib;
  377. struct fence *fence = NULL;
  378. int r;
  379. /* padding, etc. */
  380. ndw = 64;
  381. /* assume the worst case */
  382. ndw += vm->max_pde_used * 6;
  383. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  384. if (!ib)
  385. return -ENOMEM;
  386. r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
  387. if (r) {
  388. kfree(ib);
  389. return r;
  390. }
  391. ib->length_dw = 0;
  392. /* walk over the address space and update the page directory */
  393. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  394. struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
  395. uint64_t pde, pt;
  396. if (bo == NULL)
  397. continue;
  398. pt = amdgpu_bo_gpu_offset(bo);
  399. if (vm->page_tables[pt_idx].addr == pt)
  400. continue;
  401. vm->page_tables[pt_idx].addr = pt;
  402. pde = pd_addr + pt_idx * 8;
  403. if (((last_pde + 8 * count) != pde) ||
  404. ((last_pt + incr * count) != pt)) {
  405. if (count) {
  406. amdgpu_vm_update_pages(adev, ib, last_pde,
  407. last_pt, count, incr,
  408. AMDGPU_PTE_VALID, 0);
  409. }
  410. count = 1;
  411. last_pde = pde;
  412. last_pt = pt;
  413. } else {
  414. ++count;
  415. }
  416. }
  417. if (count)
  418. amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count,
  419. incr, AMDGPU_PTE_VALID, 0);
  420. if (ib->length_dw != 0) {
  421. amdgpu_vm_pad_ib(adev, ib);
  422. amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
  423. WARN_ON(ib->length_dw > ndw);
  424. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  425. &amdgpu_vm_free_job,
  426. AMDGPU_FENCE_OWNER_VM,
  427. &fence);
  428. if (r)
  429. goto error_free;
  430. amdgpu_bo_fence(pd, fence, true);
  431. fence_put(vm->page_directory_fence);
  432. vm->page_directory_fence = fence_get(fence);
  433. fence_put(fence);
  434. }
  435. if (ib->length_dw == 0) {
  436. amdgpu_ib_free(adev, ib);
  437. kfree(ib);
  438. }
  439. return 0;
  440. error_free:
  441. amdgpu_ib_free(adev, ib);
  442. kfree(ib);
  443. return r;
  444. }
  445. /**
  446. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  447. *
  448. * @adev: amdgpu_device pointer
  449. * @ib: IB for the update
  450. * @pe_start: first PTE to handle
  451. * @pe_end: last PTE to handle
  452. * @addr: addr those PTEs should point to
  453. * @flags: hw mapping flags
  454. * @gtt_flags: GTT hw mapping flags
  455. *
  456. * Global and local mutex must be locked!
  457. */
  458. static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
  459. struct amdgpu_ib *ib,
  460. uint64_t pe_start, uint64_t pe_end,
  461. uint64_t addr, uint32_t flags,
  462. uint32_t gtt_flags)
  463. {
  464. /**
  465. * The MC L1 TLB supports variable sized pages, based on a fragment
  466. * field in the PTE. When this field is set to a non-zero value, page
  467. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  468. * flags are considered valid for all PTEs within the fragment range
  469. * and corresponding mappings are assumed to be physically contiguous.
  470. *
  471. * The L1 TLB can store a single PTE for the whole fragment,
  472. * significantly increasing the space available for translation
  473. * caching. This leads to large improvements in throughput when the
  474. * TLB is under pressure.
  475. *
  476. * The L2 TLB distributes small and large fragments into two
  477. * asymmetric partitions. The large fragment cache is significantly
  478. * larger. Thus, we try to use large fragments wherever possible.
  479. * Userspace can support this by aligning virtual base address and
  480. * allocation size to the fragment size.
  481. */
  482. /* SI and newer are optimized for 64KB */
  483. uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
  484. uint64_t frag_align = 0x80;
  485. uint64_t frag_start = ALIGN(pe_start, frag_align);
  486. uint64_t frag_end = pe_end & ~(frag_align - 1);
  487. unsigned count;
  488. /* system pages are non continuously */
  489. if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
  490. (frag_start >= frag_end)) {
  491. count = (pe_end - pe_start) / 8;
  492. amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
  493. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  494. return;
  495. }
  496. /* handle the 4K area at the beginning */
  497. if (pe_start != frag_start) {
  498. count = (frag_start - pe_start) / 8;
  499. amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
  500. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  501. addr += AMDGPU_GPU_PAGE_SIZE * count;
  502. }
  503. /* handle the area in the middle */
  504. count = (frag_end - frag_start) / 8;
  505. amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
  506. AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
  507. gtt_flags);
  508. /* handle the 4K area at the end */
  509. if (frag_end != pe_end) {
  510. addr += AMDGPU_GPU_PAGE_SIZE * count;
  511. count = (pe_end - frag_end) / 8;
  512. amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
  513. AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
  514. }
  515. }
  516. /**
  517. * amdgpu_vm_update_ptes - make sure that page tables are valid
  518. *
  519. * @adev: amdgpu_device pointer
  520. * @vm: requested vm
  521. * @start: start of GPU address range
  522. * @end: end of GPU address range
  523. * @dst: destination address to map to
  524. * @flags: mapping flags
  525. *
  526. * Update the page tables in the range @start - @end (cayman+).
  527. *
  528. * Global and local mutex must be locked!
  529. */
  530. static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
  531. struct amdgpu_vm *vm,
  532. struct amdgpu_ib *ib,
  533. uint64_t start, uint64_t end,
  534. uint64_t dst, uint32_t flags,
  535. uint32_t gtt_flags)
  536. {
  537. uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  538. uint64_t last_pte = ~0, last_dst = ~0;
  539. void *owner = AMDGPU_FENCE_OWNER_VM;
  540. unsigned count = 0;
  541. uint64_t addr;
  542. /* sync to everything on unmapping */
  543. if (!(flags & AMDGPU_PTE_VALID))
  544. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  545. /* walk over the address space and update the page tables */
  546. for (addr = start; addr < end; ) {
  547. uint64_t pt_idx = addr >> amdgpu_vm_block_size;
  548. struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
  549. unsigned nptes;
  550. uint64_t pte;
  551. int r;
  552. amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv, owner);
  553. r = reservation_object_reserve_shared(pt->tbo.resv);
  554. if (r)
  555. return r;
  556. if ((addr & ~mask) == (end & ~mask))
  557. nptes = end - addr;
  558. else
  559. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  560. pte = amdgpu_bo_gpu_offset(pt);
  561. pte += (addr & mask) * 8;
  562. if ((last_pte + 8 * count) != pte) {
  563. if (count) {
  564. amdgpu_vm_frag_ptes(adev, ib, last_pte,
  565. last_pte + 8 * count,
  566. last_dst, flags,
  567. gtt_flags);
  568. }
  569. count = nptes;
  570. last_pte = pte;
  571. last_dst = dst;
  572. } else {
  573. count += nptes;
  574. }
  575. addr += nptes;
  576. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  577. }
  578. if (count) {
  579. amdgpu_vm_frag_ptes(adev, ib, last_pte,
  580. last_pte + 8 * count,
  581. last_dst, flags, gtt_flags);
  582. }
  583. return 0;
  584. }
  585. /**
  586. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  587. *
  588. * @adev: amdgpu_device pointer
  589. * @vm: requested vm
  590. * @mapping: mapped range and flags to use for the update
  591. * @addr: addr to set the area to
  592. * @gtt_flags: flags as they are used for GTT
  593. * @fence: optional resulting fence
  594. *
  595. * Fill in the page table entries for @mapping.
  596. * Returns 0 for success, -EINVAL for failure.
  597. *
  598. * Object have to be reserved and mutex must be locked!
  599. */
  600. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  601. struct amdgpu_vm *vm,
  602. struct amdgpu_bo_va_mapping *mapping,
  603. uint64_t addr, uint32_t gtt_flags,
  604. struct fence **fence)
  605. {
  606. struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
  607. unsigned nptes, ncmds, ndw;
  608. uint32_t flags = gtt_flags;
  609. struct amdgpu_ib *ib;
  610. struct fence *f = NULL;
  611. int r;
  612. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  613. * but in case of something, we filter the flags in first place
  614. */
  615. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  616. flags &= ~AMDGPU_PTE_READABLE;
  617. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  618. flags &= ~AMDGPU_PTE_WRITEABLE;
  619. trace_amdgpu_vm_bo_update(mapping);
  620. nptes = mapping->it.last - mapping->it.start + 1;
  621. /*
  622. * reserve space for one command every (1 << BLOCK_SIZE)
  623. * entries or 2k dwords (whatever is smaller)
  624. */
  625. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  626. /* padding, etc. */
  627. ndw = 64;
  628. if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
  629. /* only copy commands needed */
  630. ndw += ncmds * 7;
  631. } else if (flags & AMDGPU_PTE_SYSTEM) {
  632. /* header for write data commands */
  633. ndw += ncmds * 4;
  634. /* body of write data command */
  635. ndw += nptes * 2;
  636. } else {
  637. /* set page commands needed */
  638. ndw += ncmds * 10;
  639. /* two extra commands for begin/end of fragment */
  640. ndw += 2 * 10;
  641. }
  642. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  643. if (!ib)
  644. return -ENOMEM;
  645. r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
  646. if (r) {
  647. kfree(ib);
  648. return r;
  649. }
  650. ib->length_dw = 0;
  651. r = amdgpu_vm_update_ptes(adev, vm, ib, mapping->it.start,
  652. mapping->it.last + 1, addr + mapping->offset,
  653. flags, gtt_flags);
  654. if (r) {
  655. amdgpu_ib_free(adev, ib);
  656. kfree(ib);
  657. return r;
  658. }
  659. amdgpu_vm_pad_ib(adev, ib);
  660. WARN_ON(ib->length_dw > ndw);
  661. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  662. &amdgpu_vm_free_job,
  663. AMDGPU_FENCE_OWNER_VM,
  664. &f);
  665. if (r)
  666. goto error_free;
  667. amdgpu_bo_fence(vm->page_directory, f, true);
  668. if (fence) {
  669. fence_put(*fence);
  670. *fence = fence_get(f);
  671. }
  672. fence_put(f);
  673. return 0;
  674. error_free:
  675. amdgpu_ib_free(adev, ib);
  676. kfree(ib);
  677. return r;
  678. }
  679. /**
  680. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  681. *
  682. * @adev: amdgpu_device pointer
  683. * @bo_va: requested BO and VM object
  684. * @mem: ttm mem
  685. *
  686. * Fill in the page table entries for @bo_va.
  687. * Returns 0 for success, -EINVAL for failure.
  688. *
  689. * Object have to be reserved and mutex must be locked!
  690. */
  691. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  692. struct amdgpu_bo_va *bo_va,
  693. struct ttm_mem_reg *mem)
  694. {
  695. struct amdgpu_vm *vm = bo_va->vm;
  696. struct amdgpu_bo_va_mapping *mapping;
  697. uint32_t flags;
  698. uint64_t addr;
  699. int r;
  700. if (mem) {
  701. addr = (u64)mem->start << PAGE_SHIFT;
  702. if (mem->mem_type != TTM_PL_TT)
  703. addr += adev->vm_manager.vram_base_offset;
  704. } else {
  705. addr = 0;
  706. }
  707. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  708. spin_lock(&vm->status_lock);
  709. if (!list_empty(&bo_va->vm_status))
  710. list_splice_init(&bo_va->valids, &bo_va->invalids);
  711. spin_unlock(&vm->status_lock);
  712. list_for_each_entry(mapping, &bo_va->invalids, list) {
  713. r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
  714. flags, &bo_va->last_pt_update);
  715. if (r)
  716. return r;
  717. }
  718. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  719. list_for_each_entry(mapping, &bo_va->valids, list)
  720. trace_amdgpu_vm_bo_mapping(mapping);
  721. list_for_each_entry(mapping, &bo_va->invalids, list)
  722. trace_amdgpu_vm_bo_mapping(mapping);
  723. }
  724. spin_lock(&vm->status_lock);
  725. list_splice_init(&bo_va->invalids, &bo_va->valids);
  726. list_del_init(&bo_va->vm_status);
  727. if (!mem)
  728. list_add(&bo_va->vm_status, &vm->cleared);
  729. spin_unlock(&vm->status_lock);
  730. return 0;
  731. }
  732. /**
  733. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  734. *
  735. * @adev: amdgpu_device pointer
  736. * @vm: requested vm
  737. *
  738. * Make sure all freed BOs are cleared in the PT.
  739. * Returns 0 for success.
  740. *
  741. * PTs have to be reserved and mutex must be locked!
  742. */
  743. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  744. struct amdgpu_vm *vm)
  745. {
  746. struct amdgpu_bo_va_mapping *mapping;
  747. int r;
  748. spin_lock(&vm->freed_lock);
  749. while (!list_empty(&vm->freed)) {
  750. mapping = list_first_entry(&vm->freed,
  751. struct amdgpu_bo_va_mapping, list);
  752. list_del(&mapping->list);
  753. spin_unlock(&vm->freed_lock);
  754. r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
  755. kfree(mapping);
  756. if (r)
  757. return r;
  758. spin_lock(&vm->freed_lock);
  759. }
  760. spin_unlock(&vm->freed_lock);
  761. return 0;
  762. }
  763. /**
  764. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  765. *
  766. * @adev: amdgpu_device pointer
  767. * @vm: requested vm
  768. *
  769. * Make sure all invalidated BOs are cleared in the PT.
  770. * Returns 0 for success.
  771. *
  772. * PTs have to be reserved and mutex must be locked!
  773. */
  774. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  775. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  776. {
  777. struct amdgpu_bo_va *bo_va = NULL;
  778. int r = 0;
  779. spin_lock(&vm->status_lock);
  780. while (!list_empty(&vm->invalidated)) {
  781. bo_va = list_first_entry(&vm->invalidated,
  782. struct amdgpu_bo_va, vm_status);
  783. spin_unlock(&vm->status_lock);
  784. mutex_lock(&bo_va->mutex);
  785. r = amdgpu_vm_bo_update(adev, bo_va, NULL);
  786. mutex_unlock(&bo_va->mutex);
  787. if (r)
  788. return r;
  789. spin_lock(&vm->status_lock);
  790. }
  791. spin_unlock(&vm->status_lock);
  792. if (bo_va)
  793. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  794. return r;
  795. }
  796. /**
  797. * amdgpu_vm_bo_add - add a bo to a specific vm
  798. *
  799. * @adev: amdgpu_device pointer
  800. * @vm: requested vm
  801. * @bo: amdgpu buffer object
  802. *
  803. * Add @bo into the requested vm (cayman+).
  804. * Add @bo to the list of bos associated with the vm
  805. * Returns newly added bo_va or NULL for failure
  806. *
  807. * Object has to be reserved!
  808. */
  809. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  810. struct amdgpu_vm *vm,
  811. struct amdgpu_bo *bo)
  812. {
  813. struct amdgpu_bo_va *bo_va;
  814. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  815. if (bo_va == NULL) {
  816. return NULL;
  817. }
  818. bo_va->vm = vm;
  819. bo_va->bo = bo;
  820. bo_va->ref_count = 1;
  821. INIT_LIST_HEAD(&bo_va->bo_list);
  822. INIT_LIST_HEAD(&bo_va->valids);
  823. INIT_LIST_HEAD(&bo_va->invalids);
  824. INIT_LIST_HEAD(&bo_va->vm_status);
  825. mutex_init(&bo_va->mutex);
  826. list_add_tail(&bo_va->bo_list, &bo->va);
  827. return bo_va;
  828. }
  829. /**
  830. * amdgpu_vm_bo_map - map bo inside a vm
  831. *
  832. * @adev: amdgpu_device pointer
  833. * @bo_va: bo_va to store the address
  834. * @saddr: where to map the BO
  835. * @offset: requested offset in the BO
  836. * @flags: attributes of pages (read/write/valid/etc.)
  837. *
  838. * Add a mapping of the BO at the specefied addr into the VM.
  839. * Returns 0 for success, error for failure.
  840. *
  841. * Object has to be reserved and unreserved outside!
  842. */
  843. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  844. struct amdgpu_bo_va *bo_va,
  845. uint64_t saddr, uint64_t offset,
  846. uint64_t size, uint32_t flags)
  847. {
  848. struct amdgpu_bo_va_mapping *mapping;
  849. struct amdgpu_vm *vm = bo_va->vm;
  850. struct interval_tree_node *it;
  851. unsigned last_pfn, pt_idx;
  852. uint64_t eaddr;
  853. int r;
  854. /* validate the parameters */
  855. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  856. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  857. return -EINVAL;
  858. /* make sure object fit at this offset */
  859. eaddr = saddr + size - 1;
  860. if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
  861. return -EINVAL;
  862. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  863. if (last_pfn >= adev->vm_manager.max_pfn) {
  864. dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
  865. last_pfn, adev->vm_manager.max_pfn);
  866. return -EINVAL;
  867. }
  868. saddr /= AMDGPU_GPU_PAGE_SIZE;
  869. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  870. spin_lock(&vm->it_lock);
  871. it = interval_tree_iter_first(&vm->va, saddr, eaddr);
  872. spin_unlock(&vm->it_lock);
  873. if (it) {
  874. struct amdgpu_bo_va_mapping *tmp;
  875. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  876. /* bo and tmp overlap, invalid addr */
  877. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  878. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  879. tmp->it.start, tmp->it.last + 1);
  880. r = -EINVAL;
  881. goto error;
  882. }
  883. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  884. if (!mapping) {
  885. r = -ENOMEM;
  886. goto error;
  887. }
  888. INIT_LIST_HEAD(&mapping->list);
  889. mapping->it.start = saddr;
  890. mapping->it.last = eaddr;
  891. mapping->offset = offset;
  892. mapping->flags = flags;
  893. mutex_lock(&bo_va->mutex);
  894. list_add(&mapping->list, &bo_va->invalids);
  895. mutex_unlock(&bo_va->mutex);
  896. spin_lock(&vm->it_lock);
  897. interval_tree_insert(&mapping->it, &vm->va);
  898. spin_unlock(&vm->it_lock);
  899. trace_amdgpu_vm_bo_map(bo_va, mapping);
  900. /* Make sure the page tables are allocated */
  901. saddr >>= amdgpu_vm_block_size;
  902. eaddr >>= amdgpu_vm_block_size;
  903. BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
  904. if (eaddr > vm->max_pde_used)
  905. vm->max_pde_used = eaddr;
  906. /* walk over the address space and allocate the page tables */
  907. for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
  908. struct reservation_object *resv = vm->page_directory->tbo.resv;
  909. struct amdgpu_bo_list_entry *entry;
  910. struct amdgpu_bo *pt;
  911. entry = &vm->page_tables[pt_idx].entry;
  912. if (entry->robj)
  913. continue;
  914. r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
  915. AMDGPU_GPU_PAGE_SIZE, true,
  916. AMDGPU_GEM_DOMAIN_VRAM,
  917. AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
  918. NULL, resv, &pt);
  919. if (r)
  920. goto error_free;
  921. /* Keep a reference to the page table to avoid freeing
  922. * them up in the wrong order.
  923. */
  924. pt->parent = amdgpu_bo_ref(vm->page_directory);
  925. r = amdgpu_vm_clear_bo(adev, pt);
  926. if (r) {
  927. amdgpu_bo_unref(&pt);
  928. goto error_free;
  929. }
  930. entry->robj = pt;
  931. entry->priority = 0;
  932. entry->tv.bo = &entry->robj->tbo;
  933. entry->tv.shared = true;
  934. vm->page_tables[pt_idx].addr = 0;
  935. }
  936. return 0;
  937. error_free:
  938. list_del(&mapping->list);
  939. spin_lock(&vm->it_lock);
  940. interval_tree_remove(&mapping->it, &vm->va);
  941. spin_unlock(&vm->it_lock);
  942. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  943. kfree(mapping);
  944. error:
  945. return r;
  946. }
  947. /**
  948. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  949. *
  950. * @adev: amdgpu_device pointer
  951. * @bo_va: bo_va to remove the address from
  952. * @saddr: where to the BO is mapped
  953. *
  954. * Remove a mapping of the BO at the specefied addr from the VM.
  955. * Returns 0 for success, error for failure.
  956. *
  957. * Object has to be reserved and unreserved outside!
  958. */
  959. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  960. struct amdgpu_bo_va *bo_va,
  961. uint64_t saddr)
  962. {
  963. struct amdgpu_bo_va_mapping *mapping;
  964. struct amdgpu_vm *vm = bo_va->vm;
  965. bool valid = true;
  966. saddr /= AMDGPU_GPU_PAGE_SIZE;
  967. mutex_lock(&bo_va->mutex);
  968. list_for_each_entry(mapping, &bo_va->valids, list) {
  969. if (mapping->it.start == saddr)
  970. break;
  971. }
  972. if (&mapping->list == &bo_va->valids) {
  973. valid = false;
  974. list_for_each_entry(mapping, &bo_va->invalids, list) {
  975. if (mapping->it.start == saddr)
  976. break;
  977. }
  978. if (&mapping->list == &bo_va->invalids) {
  979. mutex_unlock(&bo_va->mutex);
  980. return -ENOENT;
  981. }
  982. }
  983. mutex_unlock(&bo_va->mutex);
  984. list_del(&mapping->list);
  985. spin_lock(&vm->it_lock);
  986. interval_tree_remove(&mapping->it, &vm->va);
  987. spin_unlock(&vm->it_lock);
  988. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  989. if (valid) {
  990. spin_lock(&vm->freed_lock);
  991. list_add(&mapping->list, &vm->freed);
  992. spin_unlock(&vm->freed_lock);
  993. } else {
  994. kfree(mapping);
  995. }
  996. return 0;
  997. }
  998. /**
  999. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1000. *
  1001. * @adev: amdgpu_device pointer
  1002. * @bo_va: requested bo_va
  1003. *
  1004. * Remove @bo_va->bo from the requested vm (cayman+).
  1005. *
  1006. * Object have to be reserved!
  1007. */
  1008. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1009. struct amdgpu_bo_va *bo_va)
  1010. {
  1011. struct amdgpu_bo_va_mapping *mapping, *next;
  1012. struct amdgpu_vm *vm = bo_va->vm;
  1013. list_del(&bo_va->bo_list);
  1014. spin_lock(&vm->status_lock);
  1015. list_del(&bo_va->vm_status);
  1016. spin_unlock(&vm->status_lock);
  1017. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1018. list_del(&mapping->list);
  1019. spin_lock(&vm->it_lock);
  1020. interval_tree_remove(&mapping->it, &vm->va);
  1021. spin_unlock(&vm->it_lock);
  1022. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1023. spin_lock(&vm->freed_lock);
  1024. list_add(&mapping->list, &vm->freed);
  1025. spin_unlock(&vm->freed_lock);
  1026. }
  1027. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1028. list_del(&mapping->list);
  1029. spin_lock(&vm->it_lock);
  1030. interval_tree_remove(&mapping->it, &vm->va);
  1031. spin_unlock(&vm->it_lock);
  1032. kfree(mapping);
  1033. }
  1034. fence_put(bo_va->last_pt_update);
  1035. mutex_destroy(&bo_va->mutex);
  1036. kfree(bo_va);
  1037. }
  1038. /**
  1039. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1040. *
  1041. * @adev: amdgpu_device pointer
  1042. * @vm: requested vm
  1043. * @bo: amdgpu buffer object
  1044. *
  1045. * Mark @bo as invalid (cayman+).
  1046. */
  1047. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1048. struct amdgpu_bo *bo)
  1049. {
  1050. struct amdgpu_bo_va *bo_va;
  1051. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1052. spin_lock(&bo_va->vm->status_lock);
  1053. if (list_empty(&bo_va->vm_status))
  1054. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1055. spin_unlock(&bo_va->vm->status_lock);
  1056. }
  1057. }
  1058. /**
  1059. * amdgpu_vm_init - initialize a vm instance
  1060. *
  1061. * @adev: amdgpu_device pointer
  1062. * @vm: requested vm
  1063. *
  1064. * Init @vm fields (cayman+).
  1065. */
  1066. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1067. {
  1068. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1069. AMDGPU_VM_PTE_COUNT * 8);
  1070. unsigned pd_size, pd_entries;
  1071. int i, r;
  1072. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1073. vm->ids[i].id = 0;
  1074. vm->ids[i].flushed_updates = NULL;
  1075. }
  1076. vm->va = RB_ROOT;
  1077. spin_lock_init(&vm->status_lock);
  1078. INIT_LIST_HEAD(&vm->invalidated);
  1079. INIT_LIST_HEAD(&vm->cleared);
  1080. INIT_LIST_HEAD(&vm->freed);
  1081. spin_lock_init(&vm->it_lock);
  1082. spin_lock_init(&vm->freed_lock);
  1083. pd_size = amdgpu_vm_directory_size(adev);
  1084. pd_entries = amdgpu_vm_num_pdes(adev);
  1085. /* allocate page table array */
  1086. vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
  1087. if (vm->page_tables == NULL) {
  1088. DRM_ERROR("Cannot allocate memory for page table array\n");
  1089. return -ENOMEM;
  1090. }
  1091. vm->page_directory_fence = NULL;
  1092. r = amdgpu_bo_create(adev, pd_size, align, true,
  1093. AMDGPU_GEM_DOMAIN_VRAM,
  1094. AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
  1095. NULL, NULL, &vm->page_directory);
  1096. if (r)
  1097. return r;
  1098. r = amdgpu_bo_reserve(vm->page_directory, false);
  1099. if (r) {
  1100. amdgpu_bo_unref(&vm->page_directory);
  1101. vm->page_directory = NULL;
  1102. return r;
  1103. }
  1104. r = amdgpu_vm_clear_bo(adev, vm->page_directory);
  1105. amdgpu_bo_unreserve(vm->page_directory);
  1106. if (r) {
  1107. amdgpu_bo_unref(&vm->page_directory);
  1108. vm->page_directory = NULL;
  1109. return r;
  1110. }
  1111. return 0;
  1112. }
  1113. /**
  1114. * amdgpu_vm_fini - tear down a vm instance
  1115. *
  1116. * @adev: amdgpu_device pointer
  1117. * @vm: requested vm
  1118. *
  1119. * Tear down @vm (cayman+).
  1120. * Unbind the VM and remove all bos from the vm bo list
  1121. */
  1122. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1123. {
  1124. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1125. int i;
  1126. if (!RB_EMPTY_ROOT(&vm->va)) {
  1127. dev_err(adev->dev, "still active bo inside vm\n");
  1128. }
  1129. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1130. list_del(&mapping->list);
  1131. interval_tree_remove(&mapping->it, &vm->va);
  1132. kfree(mapping);
  1133. }
  1134. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1135. list_del(&mapping->list);
  1136. kfree(mapping);
  1137. }
  1138. for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
  1139. amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
  1140. drm_free_large(vm->page_tables);
  1141. amdgpu_bo_unref(&vm->page_directory);
  1142. fence_put(vm->page_directory_fence);
  1143. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  1144. unsigned id = vm->ids[i].id;
  1145. atomic_long_cmpxchg(&adev->vm_manager.ids[id].owner,
  1146. (long)vm, 0);
  1147. fence_put(vm->ids[i].flushed_updates);
  1148. }
  1149. }
  1150. /**
  1151. * amdgpu_vm_manager_init - init the VM manager
  1152. *
  1153. * @adev: amdgpu_device pointer
  1154. *
  1155. * Initialize the VM manager structures
  1156. */
  1157. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  1158. {
  1159. unsigned i;
  1160. INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
  1161. /* skip over VMID 0, since it is the system VM */
  1162. for (i = 1; i < adev->vm_manager.num_ids; ++i)
  1163. list_add_tail(&adev->vm_manager.ids[i].list,
  1164. &adev->vm_manager.ids_lru);
  1165. }
  1166. /**
  1167. * amdgpu_vm_manager_fini - cleanup VM manager
  1168. *
  1169. * @adev: amdgpu_device pointer
  1170. *
  1171. * Cleanup the VM manager and free resources.
  1172. */
  1173. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  1174. {
  1175. unsigned i;
  1176. for (i = 0; i < AMDGPU_NUM_VM; ++i)
  1177. fence_put(adev->vm_manager.ids[i].active);
  1178. }