amdgpu_vm.c 34 KB

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