amdgpu_vm.c 43 KB

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