amdgpu_vm.c 43 KB

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