amdgpu_vm.c 44 KB

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