amdgpu_vm.c 36 KB

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