amdgpu_vm.c 33 KB

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