amdgpu_vm.c 39 KB

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