amdgpu_vm.c 42 KB

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