amdgpu_vm.c 42 KB

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