amdgpu_vm.c 45 KB

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