amdgpu_vm.c 45 KB

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