amdgpu_vm.c 41 KB

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