amdgpu_vm.c 35 KB

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