amdgpu_vm.c 42 KB

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