amdgpu_vm.c 42 KB

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