amdgpu_vm.c 43 KB

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