amdgpu_vm.c 41 KB

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