amdgpu_vm.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  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_ptes(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. uint64_t src = (params->src + (addr >> 12) * 8);
  452. trace_amdgpu_vm_copy_ptes(pe, src, count);
  453. amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, 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. /*
  527. * amdgpu_vm_update_pdes - make sure that page directory is valid
  528. *
  529. * @adev: amdgpu_device pointer
  530. * @vm: requested vm
  531. * @start: start of GPU address range
  532. * @end: end of GPU address range
  533. *
  534. * Allocates new page tables if necessary
  535. * and updates the page directory.
  536. * Returns 0 for success, error for failure.
  537. */
  538. int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
  539. struct amdgpu_vm *vm)
  540. {
  541. struct amdgpu_bo *shadow;
  542. struct amdgpu_ring *ring;
  543. uint64_t pd_addr, shadow_addr;
  544. uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
  545. uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
  546. unsigned count = 0, pt_idx, ndw;
  547. struct amdgpu_job *job;
  548. struct amdgpu_pte_update_params params;
  549. struct fence *fence = NULL;
  550. int r;
  551. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  552. shadow = vm->page_directory->shadow;
  553. /* padding, etc. */
  554. ndw = 64;
  555. /* assume the worst case */
  556. ndw += vm->max_pde_used * 6;
  557. pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
  558. if (shadow) {
  559. r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
  560. if (r)
  561. return r;
  562. shadow_addr = amdgpu_bo_gpu_offset(shadow);
  563. ndw *= 2;
  564. } else {
  565. shadow_addr = 0;
  566. }
  567. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  568. if (r)
  569. return r;
  570. memset(&params, 0, sizeof(params));
  571. params.adev = adev;
  572. params.ib = &job->ibs[0];
  573. /* walk over the address space and update the page directory */
  574. for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
  575. struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
  576. uint64_t pde, pt;
  577. if (bo == NULL)
  578. continue;
  579. if (bo->shadow) {
  580. struct amdgpu_bo *pt_shadow = bo->shadow;
  581. r = amdgpu_ttm_bind(&pt_shadow->tbo,
  582. &pt_shadow->tbo.mem);
  583. if (r)
  584. return r;
  585. }
  586. pt = amdgpu_bo_gpu_offset(bo);
  587. if (vm->page_tables[pt_idx].addr == pt)
  588. continue;
  589. vm->page_tables[pt_idx].addr = pt;
  590. pde = pd_addr + pt_idx * 8;
  591. if (((last_pde + 8 * count) != pde) ||
  592. ((last_pt + incr * count) != pt) ||
  593. (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
  594. if (count) {
  595. if (shadow)
  596. amdgpu_vm_do_set_ptes(&params,
  597. last_shadow,
  598. last_pt, count,
  599. incr,
  600. AMDGPU_PTE_VALID);
  601. amdgpu_vm_do_set_ptes(&params, last_pde,
  602. last_pt, count, incr,
  603. AMDGPU_PTE_VALID);
  604. }
  605. count = 1;
  606. last_pde = pde;
  607. last_shadow = shadow_addr + pt_idx * 8;
  608. last_pt = pt;
  609. } else {
  610. ++count;
  611. }
  612. }
  613. if (count) {
  614. if (vm->page_directory->shadow)
  615. amdgpu_vm_do_set_ptes(&params, last_shadow, last_pt,
  616. count, incr, AMDGPU_PTE_VALID);
  617. amdgpu_vm_do_set_ptes(&params, last_pde, last_pt,
  618. count, incr, AMDGPU_PTE_VALID);
  619. }
  620. if (params.ib->length_dw == 0) {
  621. amdgpu_job_free(job);
  622. return 0;
  623. }
  624. amdgpu_ring_pad_ib(ring, params.ib);
  625. amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
  626. AMDGPU_FENCE_OWNER_VM);
  627. if (shadow)
  628. amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
  629. AMDGPU_FENCE_OWNER_VM);
  630. WARN_ON(params.ib->length_dw > ndw);
  631. r = amdgpu_job_submit(job, ring, &vm->entity,
  632. AMDGPU_FENCE_OWNER_VM, &fence);
  633. if (r)
  634. goto error_free;
  635. amdgpu_bo_fence(vm->page_directory, fence, true);
  636. fence_put(vm->page_directory_fence);
  637. vm->page_directory_fence = fence_get(fence);
  638. fence_put(fence);
  639. return 0;
  640. error_free:
  641. amdgpu_job_free(job);
  642. return r;
  643. }
  644. /**
  645. * amdgpu_vm_update_ptes - make sure that page tables are valid
  646. *
  647. * @params: see amdgpu_pte_update_params definition
  648. * @vm: requested vm
  649. * @start: start of GPU address range
  650. * @end: end of GPU address range
  651. * @dst: destination address to map to, the next dst inside the function
  652. * @flags: mapping flags
  653. *
  654. * Update the page tables in the range @start - @end.
  655. */
  656. static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
  657. struct amdgpu_vm *vm,
  658. uint64_t start, uint64_t end,
  659. uint64_t dst, uint32_t flags)
  660. {
  661. const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
  662. uint64_t cur_pe_start, cur_nptes, cur_dst;
  663. uint64_t addr; /* next GPU address to be updated */
  664. uint64_t pt_idx;
  665. struct amdgpu_bo *pt;
  666. unsigned nptes; /* next number of ptes to be updated */
  667. uint64_t next_pe_start;
  668. /* initialize the variables */
  669. addr = start;
  670. pt_idx = addr >> amdgpu_vm_block_size;
  671. pt = vm->page_tables[pt_idx].entry.robj;
  672. if (params->shadow) {
  673. if (!pt->shadow)
  674. return;
  675. pt = vm->page_tables[pt_idx].entry.robj->shadow;
  676. }
  677. if ((addr & ~mask) == (end & ~mask))
  678. nptes = end - addr;
  679. else
  680. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  681. cur_pe_start = amdgpu_bo_gpu_offset(pt);
  682. cur_pe_start += (addr & mask) * 8;
  683. cur_nptes = nptes;
  684. cur_dst = dst;
  685. /* for next ptb*/
  686. addr += nptes;
  687. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  688. /* walk over the address space and update the page tables */
  689. while (addr < end) {
  690. pt_idx = addr >> amdgpu_vm_block_size;
  691. pt = vm->page_tables[pt_idx].entry.robj;
  692. if (params->shadow) {
  693. if (!pt->shadow)
  694. return;
  695. pt = vm->page_tables[pt_idx].entry.robj->shadow;
  696. }
  697. if ((addr & ~mask) == (end & ~mask))
  698. nptes = end - addr;
  699. else
  700. nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
  701. next_pe_start = amdgpu_bo_gpu_offset(pt);
  702. next_pe_start += (addr & mask) * 8;
  703. if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
  704. ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
  705. /* The next ptb is consecutive to current ptb.
  706. * Don't call the update function now.
  707. * Will update two ptbs together in future.
  708. */
  709. cur_nptes += nptes;
  710. } else {
  711. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  712. AMDGPU_GPU_PAGE_SIZE, flags);
  713. cur_pe_start = next_pe_start;
  714. cur_nptes = nptes;
  715. cur_dst = dst;
  716. }
  717. /* for next ptb*/
  718. addr += nptes;
  719. dst += nptes * AMDGPU_GPU_PAGE_SIZE;
  720. }
  721. params->func(params, cur_pe_start, cur_dst, cur_nptes,
  722. AMDGPU_GPU_PAGE_SIZE, flags);
  723. }
  724. /*
  725. * amdgpu_vm_frag_ptes - add fragment information to PTEs
  726. *
  727. * @params: see amdgpu_pte_update_params definition
  728. * @vm: requested vm
  729. * @start: first PTE to handle
  730. * @end: last PTE to handle
  731. * @dst: addr those PTEs should point to
  732. * @flags: hw mapping flags
  733. */
  734. static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
  735. struct amdgpu_vm *vm,
  736. uint64_t start, uint64_t end,
  737. uint64_t dst, uint32_t flags)
  738. {
  739. /**
  740. * The MC L1 TLB supports variable sized pages, based on a fragment
  741. * field in the PTE. When this field is set to a non-zero value, page
  742. * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
  743. * flags are considered valid for all PTEs within the fragment range
  744. * and corresponding mappings are assumed to be physically contiguous.
  745. *
  746. * The L1 TLB can store a single PTE for the whole fragment,
  747. * significantly increasing the space available for translation
  748. * caching. This leads to large improvements in throughput when the
  749. * TLB is under pressure.
  750. *
  751. * The L2 TLB distributes small and large fragments into two
  752. * asymmetric partitions. The large fragment cache is significantly
  753. * larger. Thus, we try to use large fragments wherever possible.
  754. * Userspace can support this by aligning virtual base address and
  755. * allocation size to the fragment size.
  756. */
  757. /* SI and newer are optimized for 64KB */
  758. uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
  759. uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
  760. uint64_t frag_start = ALIGN(start, frag_align);
  761. uint64_t frag_end = end & ~(frag_align - 1);
  762. /* system pages are non continuously */
  763. if (params->src || !(flags & AMDGPU_PTE_VALID) ||
  764. (frag_start >= frag_end)) {
  765. amdgpu_vm_update_ptes(params, vm, start, end, dst, flags);
  766. return;
  767. }
  768. /* handle the 4K area at the beginning */
  769. if (start != frag_start) {
  770. amdgpu_vm_update_ptes(params, vm, start, frag_start,
  771. dst, flags);
  772. dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
  773. }
  774. /* handle the area in the middle */
  775. amdgpu_vm_update_ptes(params, vm, frag_start, frag_end, dst,
  776. flags | frag_flags);
  777. /* handle the 4K area at the end */
  778. if (frag_end != end) {
  779. dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
  780. amdgpu_vm_update_ptes(params, vm, frag_end, end, dst, flags);
  781. }
  782. }
  783. /**
  784. * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
  785. *
  786. * @adev: amdgpu_device pointer
  787. * @exclusive: fence we need to sync to
  788. * @src: address where to copy page table entries from
  789. * @pages_addr: DMA addresses to use for mapping
  790. * @vm: requested vm
  791. * @start: start of mapped range
  792. * @last: last mapped entry
  793. * @flags: flags for the entries
  794. * @addr: addr to set the area to
  795. * @fence: optional resulting fence
  796. *
  797. * Fill in the page table entries between @start and @last.
  798. * Returns 0 for success, -EINVAL for failure.
  799. */
  800. static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  801. struct fence *exclusive,
  802. uint64_t src,
  803. dma_addr_t *pages_addr,
  804. struct amdgpu_vm *vm,
  805. uint64_t start, uint64_t last,
  806. uint32_t flags, uint64_t addr,
  807. struct fence **fence)
  808. {
  809. struct amdgpu_ring *ring;
  810. void *owner = AMDGPU_FENCE_OWNER_VM;
  811. unsigned nptes, ncmds, ndw;
  812. struct amdgpu_job *job;
  813. struct amdgpu_pte_update_params params;
  814. struct fence *f = NULL;
  815. int r;
  816. memset(&params, 0, sizeof(params));
  817. params.adev = adev;
  818. params.src = src;
  819. ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
  820. memset(&params, 0, sizeof(params));
  821. params.adev = adev;
  822. params.src = src;
  823. /* sync to everything on unmapping */
  824. if (!(flags & AMDGPU_PTE_VALID))
  825. owner = AMDGPU_FENCE_OWNER_UNDEFINED;
  826. nptes = last - start + 1;
  827. /*
  828. * reserve space for one command every (1 << BLOCK_SIZE)
  829. * entries or 2k dwords (whatever is smaller)
  830. */
  831. ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
  832. /* padding, etc. */
  833. ndw = 64;
  834. if (src) {
  835. /* only copy commands needed */
  836. ndw += ncmds * 7;
  837. params.func = amdgpu_vm_do_copy_ptes;
  838. } else if (pages_addr) {
  839. /* copy commands needed */
  840. ndw += ncmds * 7;
  841. /* and also PTEs */
  842. ndw += nptes * 2;
  843. params.func = amdgpu_vm_do_copy_ptes;
  844. } else {
  845. /* set page commands needed */
  846. ndw += ncmds * 10;
  847. /* two extra commands for begin/end of fragment */
  848. ndw += 2 * 10;
  849. params.func = amdgpu_vm_do_set_ptes;
  850. }
  851. r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
  852. if (r)
  853. return r;
  854. params.ib = &job->ibs[0];
  855. if (!src && pages_addr) {
  856. uint64_t *pte;
  857. unsigned i;
  858. /* Put the PTEs at the end of the IB. */
  859. i = ndw - nptes * 2;
  860. pte= (uint64_t *)&(job->ibs->ptr[i]);
  861. params.src = job->ibs->gpu_addr + i * 4;
  862. for (i = 0; i < nptes; ++i) {
  863. pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
  864. AMDGPU_GPU_PAGE_SIZE);
  865. pte[i] |= flags;
  866. }
  867. addr = 0;
  868. }
  869. r = amdgpu_sync_fence(adev, &job->sync, exclusive);
  870. if (r)
  871. goto error_free;
  872. r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
  873. owner);
  874. if (r)
  875. goto error_free;
  876. r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
  877. if (r)
  878. goto error_free;
  879. params.shadow = true;
  880. amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
  881. params.shadow = false;
  882. amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
  883. amdgpu_ring_pad_ib(ring, params.ib);
  884. WARN_ON(params.ib->length_dw > ndw);
  885. r = amdgpu_job_submit(job, ring, &vm->entity,
  886. AMDGPU_FENCE_OWNER_VM, &f);
  887. if (r)
  888. goto error_free;
  889. amdgpu_bo_fence(vm->page_directory, f, true);
  890. if (fence) {
  891. fence_put(*fence);
  892. *fence = fence_get(f);
  893. }
  894. fence_put(f);
  895. return 0;
  896. error_free:
  897. amdgpu_job_free(job);
  898. return r;
  899. }
  900. /**
  901. * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
  902. *
  903. * @adev: amdgpu_device pointer
  904. * @exclusive: fence we need to sync to
  905. * @gtt_flags: flags as they are used for GTT
  906. * @pages_addr: DMA addresses to use for mapping
  907. * @vm: requested vm
  908. * @mapping: mapped range and flags to use for the update
  909. * @flags: HW flags for the mapping
  910. * @nodes: array of drm_mm_nodes with the MC addresses
  911. * @fence: optional resulting fence
  912. *
  913. * Split the mapping into smaller chunks so that each update fits
  914. * into a SDMA IB.
  915. * Returns 0 for success, -EINVAL for failure.
  916. */
  917. static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
  918. struct fence *exclusive,
  919. uint32_t gtt_flags,
  920. dma_addr_t *pages_addr,
  921. struct amdgpu_vm *vm,
  922. struct amdgpu_bo_va_mapping *mapping,
  923. uint32_t flags,
  924. struct drm_mm_node *nodes,
  925. struct fence **fence)
  926. {
  927. uint64_t pfn, src = 0, start = mapping->it.start;
  928. int r;
  929. /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
  930. * but in case of something, we filter the flags in first place
  931. */
  932. if (!(mapping->flags & AMDGPU_PTE_READABLE))
  933. flags &= ~AMDGPU_PTE_READABLE;
  934. if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
  935. flags &= ~AMDGPU_PTE_WRITEABLE;
  936. trace_amdgpu_vm_bo_update(mapping);
  937. pfn = mapping->offset >> PAGE_SHIFT;
  938. if (nodes) {
  939. while (pfn >= nodes->size) {
  940. pfn -= nodes->size;
  941. ++nodes;
  942. }
  943. }
  944. do {
  945. uint64_t max_entries;
  946. uint64_t addr, last;
  947. if (nodes) {
  948. addr = nodes->start << PAGE_SHIFT;
  949. max_entries = (nodes->size - pfn) *
  950. (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
  951. } else {
  952. addr = 0;
  953. max_entries = S64_MAX;
  954. }
  955. if (pages_addr) {
  956. if (flags == gtt_flags)
  957. src = adev->gart.table_addr +
  958. (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
  959. else
  960. max_entries = min(max_entries, 16ull * 1024ull);
  961. addr = 0;
  962. } else if (flags & AMDGPU_PTE_VALID) {
  963. addr += adev->vm_manager.vram_base_offset;
  964. }
  965. addr += pfn << PAGE_SHIFT;
  966. last = min((uint64_t)mapping->it.last, start + max_entries - 1);
  967. r = amdgpu_vm_bo_update_mapping(adev, exclusive,
  968. src, pages_addr, vm,
  969. start, last, flags, addr,
  970. fence);
  971. if (r)
  972. return r;
  973. pfn += last - start + 1;
  974. if (nodes && nodes->size == pfn) {
  975. pfn = 0;
  976. ++nodes;
  977. }
  978. start = last + 1;
  979. } while (unlikely(start != mapping->it.last + 1));
  980. return 0;
  981. }
  982. /**
  983. * amdgpu_vm_bo_update - update all BO mappings in the vm page table
  984. *
  985. * @adev: amdgpu_device pointer
  986. * @bo_va: requested BO and VM object
  987. * @clear: if true clear the entries
  988. *
  989. * Fill in the page table entries for @bo_va.
  990. * Returns 0 for success, -EINVAL for failure.
  991. */
  992. int amdgpu_vm_bo_update(struct amdgpu_device *adev,
  993. struct amdgpu_bo_va *bo_va,
  994. bool clear)
  995. {
  996. struct amdgpu_vm *vm = bo_va->vm;
  997. struct amdgpu_bo_va_mapping *mapping;
  998. dma_addr_t *pages_addr = NULL;
  999. uint32_t gtt_flags, flags;
  1000. struct ttm_mem_reg *mem;
  1001. struct drm_mm_node *nodes;
  1002. struct fence *exclusive;
  1003. int r;
  1004. if (clear) {
  1005. mem = NULL;
  1006. nodes = NULL;
  1007. exclusive = NULL;
  1008. } else {
  1009. struct ttm_dma_tt *ttm;
  1010. mem = &bo_va->bo->tbo.mem;
  1011. nodes = mem->mm_node;
  1012. if (mem->mem_type == TTM_PL_TT) {
  1013. ttm = container_of(bo_va->bo->tbo.ttm, struct
  1014. ttm_dma_tt, ttm);
  1015. pages_addr = ttm->dma_address;
  1016. }
  1017. exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
  1018. }
  1019. flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
  1020. gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
  1021. adev == bo_va->bo->adev) ? flags : 0;
  1022. spin_lock(&vm->status_lock);
  1023. if (!list_empty(&bo_va->vm_status))
  1024. list_splice_init(&bo_va->valids, &bo_va->invalids);
  1025. spin_unlock(&vm->status_lock);
  1026. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1027. r = amdgpu_vm_bo_split_mapping(adev, exclusive,
  1028. gtt_flags, pages_addr, vm,
  1029. mapping, flags, nodes,
  1030. &bo_va->last_pt_update);
  1031. if (r)
  1032. return r;
  1033. }
  1034. if (trace_amdgpu_vm_bo_mapping_enabled()) {
  1035. list_for_each_entry(mapping, &bo_va->valids, list)
  1036. trace_amdgpu_vm_bo_mapping(mapping);
  1037. list_for_each_entry(mapping, &bo_va->invalids, list)
  1038. trace_amdgpu_vm_bo_mapping(mapping);
  1039. }
  1040. spin_lock(&vm->status_lock);
  1041. list_splice_init(&bo_va->invalids, &bo_va->valids);
  1042. list_del_init(&bo_va->vm_status);
  1043. if (clear)
  1044. list_add(&bo_va->vm_status, &vm->cleared);
  1045. spin_unlock(&vm->status_lock);
  1046. return 0;
  1047. }
  1048. /**
  1049. * amdgpu_vm_clear_freed - clear freed BOs in the PT
  1050. *
  1051. * @adev: amdgpu_device pointer
  1052. * @vm: requested vm
  1053. *
  1054. * Make sure all freed BOs are cleared in the PT.
  1055. * Returns 0 for success.
  1056. *
  1057. * PTs have to be reserved and mutex must be locked!
  1058. */
  1059. int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
  1060. struct amdgpu_vm *vm)
  1061. {
  1062. struct amdgpu_bo_va_mapping *mapping;
  1063. int r;
  1064. while (!list_empty(&vm->freed)) {
  1065. mapping = list_first_entry(&vm->freed,
  1066. struct amdgpu_bo_va_mapping, list);
  1067. list_del(&mapping->list);
  1068. r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
  1069. 0, 0, NULL);
  1070. kfree(mapping);
  1071. if (r)
  1072. return r;
  1073. }
  1074. return 0;
  1075. }
  1076. /**
  1077. * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
  1078. *
  1079. * @adev: amdgpu_device pointer
  1080. * @vm: requested vm
  1081. *
  1082. * Make sure all invalidated BOs are cleared in the PT.
  1083. * Returns 0 for success.
  1084. *
  1085. * PTs have to be reserved and mutex must be locked!
  1086. */
  1087. int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
  1088. struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  1089. {
  1090. struct amdgpu_bo_va *bo_va = NULL;
  1091. int r = 0;
  1092. spin_lock(&vm->status_lock);
  1093. while (!list_empty(&vm->invalidated)) {
  1094. bo_va = list_first_entry(&vm->invalidated,
  1095. struct amdgpu_bo_va, vm_status);
  1096. spin_unlock(&vm->status_lock);
  1097. r = amdgpu_vm_bo_update(adev, bo_va, true);
  1098. if (r)
  1099. return r;
  1100. spin_lock(&vm->status_lock);
  1101. }
  1102. spin_unlock(&vm->status_lock);
  1103. if (bo_va)
  1104. r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
  1105. return r;
  1106. }
  1107. /**
  1108. * amdgpu_vm_bo_add - add a bo to a specific vm
  1109. *
  1110. * @adev: amdgpu_device pointer
  1111. * @vm: requested vm
  1112. * @bo: amdgpu buffer object
  1113. *
  1114. * Add @bo into the requested vm.
  1115. * Add @bo to the list of bos associated with the vm
  1116. * Returns newly added bo_va or NULL for failure
  1117. *
  1118. * Object has to be reserved!
  1119. */
  1120. struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
  1121. struct amdgpu_vm *vm,
  1122. struct amdgpu_bo *bo)
  1123. {
  1124. struct amdgpu_bo_va *bo_va;
  1125. bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
  1126. if (bo_va == NULL) {
  1127. return NULL;
  1128. }
  1129. bo_va->vm = vm;
  1130. bo_va->bo = bo;
  1131. bo_va->ref_count = 1;
  1132. INIT_LIST_HEAD(&bo_va->bo_list);
  1133. INIT_LIST_HEAD(&bo_va->valids);
  1134. INIT_LIST_HEAD(&bo_va->invalids);
  1135. INIT_LIST_HEAD(&bo_va->vm_status);
  1136. list_add_tail(&bo_va->bo_list, &bo->va);
  1137. return bo_va;
  1138. }
  1139. /**
  1140. * amdgpu_vm_bo_map - map bo inside a vm
  1141. *
  1142. * @adev: amdgpu_device pointer
  1143. * @bo_va: bo_va to store the address
  1144. * @saddr: where to map the BO
  1145. * @offset: requested offset in the BO
  1146. * @flags: attributes of pages (read/write/valid/etc.)
  1147. *
  1148. * Add a mapping of the BO at the specefied addr into the VM.
  1149. * Returns 0 for success, error for failure.
  1150. *
  1151. * Object has to be reserved and unreserved outside!
  1152. */
  1153. int amdgpu_vm_bo_map(struct amdgpu_device *adev,
  1154. struct amdgpu_bo_va *bo_va,
  1155. uint64_t saddr, uint64_t offset,
  1156. uint64_t size, uint32_t flags)
  1157. {
  1158. struct amdgpu_bo_va_mapping *mapping;
  1159. struct amdgpu_vm *vm = bo_va->vm;
  1160. struct interval_tree_node *it;
  1161. unsigned last_pfn, pt_idx;
  1162. uint64_t eaddr;
  1163. int r;
  1164. /* validate the parameters */
  1165. if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
  1166. size == 0 || size & AMDGPU_GPU_PAGE_MASK)
  1167. return -EINVAL;
  1168. /* make sure object fit at this offset */
  1169. eaddr = saddr + size - 1;
  1170. if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
  1171. return -EINVAL;
  1172. last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
  1173. if (last_pfn >= adev->vm_manager.max_pfn) {
  1174. dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
  1175. last_pfn, adev->vm_manager.max_pfn);
  1176. return -EINVAL;
  1177. }
  1178. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1179. eaddr /= AMDGPU_GPU_PAGE_SIZE;
  1180. it = interval_tree_iter_first(&vm->va, saddr, eaddr);
  1181. if (it) {
  1182. struct amdgpu_bo_va_mapping *tmp;
  1183. tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
  1184. /* bo and tmp overlap, invalid addr */
  1185. dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
  1186. "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
  1187. tmp->it.start, tmp->it.last + 1);
  1188. r = -EINVAL;
  1189. goto error;
  1190. }
  1191. mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
  1192. if (!mapping) {
  1193. r = -ENOMEM;
  1194. goto error;
  1195. }
  1196. INIT_LIST_HEAD(&mapping->list);
  1197. mapping->it.start = saddr;
  1198. mapping->it.last = eaddr;
  1199. mapping->offset = offset;
  1200. mapping->flags = flags;
  1201. list_add(&mapping->list, &bo_va->invalids);
  1202. interval_tree_insert(&mapping->it, &vm->va);
  1203. /* Make sure the page tables are allocated */
  1204. saddr >>= amdgpu_vm_block_size;
  1205. eaddr >>= amdgpu_vm_block_size;
  1206. BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
  1207. if (eaddr > vm->max_pde_used)
  1208. vm->max_pde_used = eaddr;
  1209. /* walk over the address space and allocate the page tables */
  1210. for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
  1211. struct reservation_object *resv = vm->page_directory->tbo.resv;
  1212. struct amdgpu_bo_list_entry *entry;
  1213. struct amdgpu_bo *pt;
  1214. entry = &vm->page_tables[pt_idx].entry;
  1215. if (entry->robj)
  1216. continue;
  1217. r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
  1218. AMDGPU_GPU_PAGE_SIZE, true,
  1219. AMDGPU_GEM_DOMAIN_VRAM,
  1220. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  1221. AMDGPU_GEM_CREATE_SHADOW |
  1222. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  1223. NULL, resv, &pt);
  1224. if (r)
  1225. goto error_free;
  1226. /* Keep a reference to the page table to avoid freeing
  1227. * them up in the wrong order.
  1228. */
  1229. pt->parent = amdgpu_bo_ref(vm->page_directory);
  1230. r = amdgpu_vm_clear_bo(adev, vm, pt);
  1231. if (r) {
  1232. amdgpu_bo_unref(&pt->shadow);
  1233. amdgpu_bo_unref(&pt);
  1234. goto error_free;
  1235. }
  1236. if (pt->shadow) {
  1237. r = amdgpu_vm_clear_bo(adev, vm, pt->shadow);
  1238. if (r) {
  1239. amdgpu_bo_unref(&pt->shadow);
  1240. amdgpu_bo_unref(&pt);
  1241. goto error_free;
  1242. }
  1243. }
  1244. entry->robj = pt;
  1245. entry->priority = 0;
  1246. entry->tv.bo = &entry->robj->tbo;
  1247. entry->tv.shared = true;
  1248. entry->user_pages = NULL;
  1249. vm->page_tables[pt_idx].addr = 0;
  1250. }
  1251. return 0;
  1252. error_free:
  1253. list_del(&mapping->list);
  1254. interval_tree_remove(&mapping->it, &vm->va);
  1255. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1256. kfree(mapping);
  1257. error:
  1258. return r;
  1259. }
  1260. /**
  1261. * amdgpu_vm_bo_unmap - remove bo mapping from vm
  1262. *
  1263. * @adev: amdgpu_device pointer
  1264. * @bo_va: bo_va to remove the address from
  1265. * @saddr: where to the BO is mapped
  1266. *
  1267. * Remove a mapping of the BO at the specefied addr from the VM.
  1268. * Returns 0 for success, error for failure.
  1269. *
  1270. * Object has to be reserved and unreserved outside!
  1271. */
  1272. int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
  1273. struct amdgpu_bo_va *bo_va,
  1274. uint64_t saddr)
  1275. {
  1276. struct amdgpu_bo_va_mapping *mapping;
  1277. struct amdgpu_vm *vm = bo_va->vm;
  1278. bool valid = true;
  1279. saddr /= AMDGPU_GPU_PAGE_SIZE;
  1280. list_for_each_entry(mapping, &bo_va->valids, list) {
  1281. if (mapping->it.start == saddr)
  1282. break;
  1283. }
  1284. if (&mapping->list == &bo_va->valids) {
  1285. valid = false;
  1286. list_for_each_entry(mapping, &bo_va->invalids, list) {
  1287. if (mapping->it.start == saddr)
  1288. break;
  1289. }
  1290. if (&mapping->list == &bo_va->invalids)
  1291. return -ENOENT;
  1292. }
  1293. list_del(&mapping->list);
  1294. interval_tree_remove(&mapping->it, &vm->va);
  1295. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1296. if (valid)
  1297. list_add(&mapping->list, &vm->freed);
  1298. else
  1299. kfree(mapping);
  1300. return 0;
  1301. }
  1302. /**
  1303. * amdgpu_vm_bo_rmv - remove a bo to a specific vm
  1304. *
  1305. * @adev: amdgpu_device pointer
  1306. * @bo_va: requested bo_va
  1307. *
  1308. * Remove @bo_va->bo from the requested vm.
  1309. *
  1310. * Object have to be reserved!
  1311. */
  1312. void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
  1313. struct amdgpu_bo_va *bo_va)
  1314. {
  1315. struct amdgpu_bo_va_mapping *mapping, *next;
  1316. struct amdgpu_vm *vm = bo_va->vm;
  1317. list_del(&bo_va->bo_list);
  1318. spin_lock(&vm->status_lock);
  1319. list_del(&bo_va->vm_status);
  1320. spin_unlock(&vm->status_lock);
  1321. list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
  1322. list_del(&mapping->list);
  1323. interval_tree_remove(&mapping->it, &vm->va);
  1324. trace_amdgpu_vm_bo_unmap(bo_va, mapping);
  1325. list_add(&mapping->list, &vm->freed);
  1326. }
  1327. list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
  1328. list_del(&mapping->list);
  1329. interval_tree_remove(&mapping->it, &vm->va);
  1330. kfree(mapping);
  1331. }
  1332. fence_put(bo_va->last_pt_update);
  1333. kfree(bo_va);
  1334. }
  1335. /**
  1336. * amdgpu_vm_bo_invalidate - mark the bo as invalid
  1337. *
  1338. * @adev: amdgpu_device pointer
  1339. * @vm: requested vm
  1340. * @bo: amdgpu buffer object
  1341. *
  1342. * Mark @bo as invalid.
  1343. */
  1344. void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
  1345. struct amdgpu_bo *bo)
  1346. {
  1347. struct amdgpu_bo_va *bo_va;
  1348. list_for_each_entry(bo_va, &bo->va, bo_list) {
  1349. spin_lock(&bo_va->vm->status_lock);
  1350. if (list_empty(&bo_va->vm_status))
  1351. list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
  1352. spin_unlock(&bo_va->vm->status_lock);
  1353. }
  1354. }
  1355. /**
  1356. * amdgpu_vm_init - initialize a vm instance
  1357. *
  1358. * @adev: amdgpu_device pointer
  1359. * @vm: requested vm
  1360. *
  1361. * Init @vm fields.
  1362. */
  1363. int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1364. {
  1365. const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
  1366. AMDGPU_VM_PTE_COUNT * 8);
  1367. unsigned pd_size, pd_entries;
  1368. unsigned ring_instance;
  1369. struct amdgpu_ring *ring;
  1370. struct amd_sched_rq *rq;
  1371. int i, r;
  1372. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1373. vm->ids[i] = NULL;
  1374. vm->va = RB_ROOT;
  1375. vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
  1376. spin_lock_init(&vm->status_lock);
  1377. INIT_LIST_HEAD(&vm->invalidated);
  1378. INIT_LIST_HEAD(&vm->cleared);
  1379. INIT_LIST_HEAD(&vm->freed);
  1380. pd_size = amdgpu_vm_directory_size(adev);
  1381. pd_entries = amdgpu_vm_num_pdes(adev);
  1382. /* allocate page table array */
  1383. vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
  1384. if (vm->page_tables == NULL) {
  1385. DRM_ERROR("Cannot allocate memory for page table array\n");
  1386. return -ENOMEM;
  1387. }
  1388. /* create scheduler entity for page table updates */
  1389. ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
  1390. ring_instance %= adev->vm_manager.vm_pte_num_rings;
  1391. ring = adev->vm_manager.vm_pte_rings[ring_instance];
  1392. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
  1393. r = amd_sched_entity_init(&ring->sched, &vm->entity,
  1394. rq, amdgpu_sched_jobs);
  1395. if (r)
  1396. goto err;
  1397. vm->page_directory_fence = NULL;
  1398. r = amdgpu_bo_create(adev, pd_size, align, true,
  1399. AMDGPU_GEM_DOMAIN_VRAM,
  1400. AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
  1401. AMDGPU_GEM_CREATE_SHADOW |
  1402. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  1403. NULL, NULL, &vm->page_directory);
  1404. if (r)
  1405. goto error_free_sched_entity;
  1406. r = amdgpu_bo_reserve(vm->page_directory, false);
  1407. if (r)
  1408. goto error_free_page_directory;
  1409. r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
  1410. if (r)
  1411. goto error_unreserve;
  1412. if (vm->page_directory->shadow) {
  1413. r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory->shadow);
  1414. if (r)
  1415. goto error_unreserve;
  1416. }
  1417. vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
  1418. amdgpu_bo_unreserve(vm->page_directory);
  1419. return 0;
  1420. error_unreserve:
  1421. amdgpu_bo_unreserve(vm->page_directory);
  1422. error_free_page_directory:
  1423. amdgpu_bo_unref(&vm->page_directory->shadow);
  1424. amdgpu_bo_unref(&vm->page_directory);
  1425. vm->page_directory = NULL;
  1426. error_free_sched_entity:
  1427. amd_sched_entity_fini(&ring->sched, &vm->entity);
  1428. err:
  1429. drm_free_large(vm->page_tables);
  1430. return r;
  1431. }
  1432. /**
  1433. * amdgpu_vm_fini - tear down a vm instance
  1434. *
  1435. * @adev: amdgpu_device pointer
  1436. * @vm: requested vm
  1437. *
  1438. * Tear down @vm.
  1439. * Unbind the VM and remove all bos from the vm bo list
  1440. */
  1441. void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
  1442. {
  1443. struct amdgpu_bo_va_mapping *mapping, *tmp;
  1444. int i;
  1445. amd_sched_entity_fini(vm->entity.sched, &vm->entity);
  1446. if (!RB_EMPTY_ROOT(&vm->va)) {
  1447. dev_err(adev->dev, "still active bo inside vm\n");
  1448. }
  1449. rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
  1450. list_del(&mapping->list);
  1451. interval_tree_remove(&mapping->it, &vm->va);
  1452. kfree(mapping);
  1453. }
  1454. list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
  1455. list_del(&mapping->list);
  1456. kfree(mapping);
  1457. }
  1458. for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) {
  1459. struct amdgpu_bo *pt = vm->page_tables[i].entry.robj;
  1460. if (!pt)
  1461. continue;
  1462. amdgpu_bo_unref(&pt->shadow);
  1463. amdgpu_bo_unref(&pt);
  1464. }
  1465. drm_free_large(vm->page_tables);
  1466. amdgpu_bo_unref(&vm->page_directory->shadow);
  1467. amdgpu_bo_unref(&vm->page_directory);
  1468. fence_put(vm->page_directory_fence);
  1469. }
  1470. /**
  1471. * amdgpu_vm_manager_init - init the VM manager
  1472. *
  1473. * @adev: amdgpu_device pointer
  1474. *
  1475. * Initialize the VM manager structures
  1476. */
  1477. void amdgpu_vm_manager_init(struct amdgpu_device *adev)
  1478. {
  1479. unsigned i;
  1480. INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
  1481. /* skip over VMID 0, since it is the system VM */
  1482. for (i = 1; i < adev->vm_manager.num_ids; ++i) {
  1483. amdgpu_vm_reset_id(adev, i);
  1484. amdgpu_sync_create(&adev->vm_manager.ids[i].active);
  1485. list_add_tail(&adev->vm_manager.ids[i].list,
  1486. &adev->vm_manager.ids_lru);
  1487. }
  1488. adev->vm_manager.fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
  1489. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  1490. adev->vm_manager.seqno[i] = 0;
  1491. atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
  1492. atomic64_set(&adev->vm_manager.client_counter, 0);
  1493. }
  1494. /**
  1495. * amdgpu_vm_manager_fini - cleanup VM manager
  1496. *
  1497. * @adev: amdgpu_device pointer
  1498. *
  1499. * Cleanup the VM manager and free resources.
  1500. */
  1501. void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
  1502. {
  1503. unsigned i;
  1504. for (i = 0; i < AMDGPU_NUM_VM; ++i) {
  1505. struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
  1506. fence_put(adev->vm_manager.ids[i].first);
  1507. amdgpu_sync_free(&adev->vm_manager.ids[i].active);
  1508. fence_put(id->flushed_updates);
  1509. }
  1510. }