amdgpu_ids.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright 2017 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include "amdgpu_ids.h"
  24. #include <linux/idr.h>
  25. #include <linux/dma-fence-array.h>
  26. #include <drm/drmP.h>
  27. #include "amdgpu.h"
  28. #include "amdgpu_trace.h"
  29. /*
  30. * PASID manager
  31. *
  32. * PASIDs are global address space identifiers that can be shared
  33. * between the GPU, an IOMMU and the driver. VMs on different devices
  34. * may use the same PASID if they share the same address
  35. * space. Therefore PASIDs are allocated using a global IDA. VMs are
  36. * looked up from the PASID per amdgpu_device.
  37. */
  38. static DEFINE_IDA(amdgpu_pasid_ida);
  39. /**
  40. * amdgpu_pasid_alloc - Allocate a PASID
  41. * @bits: Maximum width of the PASID in bits, must be at least 1
  42. *
  43. * Allocates a PASID of the given width while keeping smaller PASIDs
  44. * available if possible.
  45. *
  46. * Returns a positive integer on success. Returns %-EINVAL if bits==0.
  47. * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
  48. * memory allocation failure.
  49. */
  50. int amdgpu_pasid_alloc(unsigned int bits)
  51. {
  52. int pasid = -EINVAL;
  53. for (bits = min(bits, 31U); bits > 0; bits--) {
  54. pasid = ida_simple_get(&amdgpu_pasid_ida,
  55. 1U << (bits - 1), 1U << bits,
  56. GFP_KERNEL);
  57. if (pasid != -ENOSPC)
  58. break;
  59. }
  60. return pasid;
  61. }
  62. /**
  63. * amdgpu_pasid_free - Free a PASID
  64. * @pasid: PASID to free
  65. */
  66. void amdgpu_pasid_free(unsigned int pasid)
  67. {
  68. ida_simple_remove(&amdgpu_pasid_ida, pasid);
  69. }
  70. /*
  71. * VMID manager
  72. *
  73. * VMIDs are a per VMHUB identifier for page tables handling.
  74. */
  75. /**
  76. * amdgpu_vmid_had_gpu_reset - check if reset occured since last use
  77. *
  78. * @adev: amdgpu_device pointer
  79. * @id: VMID structure
  80. *
  81. * Check if GPU reset occured since last use of the VMID.
  82. */
  83. bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
  84. struct amdgpu_vmid *id)
  85. {
  86. return id->current_gpu_reset_count !=
  87. atomic_read(&adev->gpu_reset_counter);
  88. }
  89. /* idr_mgr->lock must be held */
  90. static int amdgpu_vmid_grab_reserved_locked(struct amdgpu_vm *vm,
  91. struct amdgpu_ring *ring,
  92. struct amdgpu_sync *sync,
  93. struct dma_fence *fence,
  94. struct amdgpu_job *job)
  95. {
  96. struct amdgpu_device *adev = ring->adev;
  97. unsigned vmhub = ring->funcs->vmhub;
  98. uint64_t fence_context = adev->fence_context + ring->idx;
  99. struct amdgpu_vmid *id = vm->reserved_vmid[vmhub];
  100. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  101. struct dma_fence *updates = sync->last_vm_update;
  102. int r = 0;
  103. struct dma_fence *flushed, *tmp;
  104. bool needs_flush = vm->use_cpu_for_update;
  105. flushed = id->flushed_updates;
  106. if ((amdgpu_vmid_had_gpu_reset(adev, id)) ||
  107. (atomic64_read(&id->owner) != vm->entity.fence_context) ||
  108. (job->vm_pd_addr != id->pd_gpu_addr) ||
  109. (updates && (!flushed || updates->context != flushed->context ||
  110. dma_fence_is_later(updates, flushed))) ||
  111. (!id->last_flush || (id->last_flush->context != fence_context &&
  112. !dma_fence_is_signaled(id->last_flush)))) {
  113. needs_flush = true;
  114. /* to prevent one context starved by another context */
  115. id->pd_gpu_addr = 0;
  116. tmp = amdgpu_sync_peek_fence(&id->active, ring);
  117. if (tmp) {
  118. r = amdgpu_sync_fence(adev, sync, tmp, false);
  119. return r;
  120. }
  121. }
  122. /* Good we can use this VMID. Remember this submission as
  123. * user of the VMID.
  124. */
  125. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  126. if (r)
  127. goto out;
  128. if (updates && (!flushed || updates->context != flushed->context ||
  129. dma_fence_is_later(updates, flushed))) {
  130. dma_fence_put(id->flushed_updates);
  131. id->flushed_updates = dma_fence_get(updates);
  132. }
  133. id->pd_gpu_addr = job->vm_pd_addr;
  134. atomic64_set(&id->owner, vm->entity.fence_context);
  135. job->vm_needs_flush = needs_flush;
  136. if (needs_flush) {
  137. dma_fence_put(id->last_flush);
  138. id->last_flush = NULL;
  139. }
  140. job->vmid = id - id_mgr->ids;
  141. trace_amdgpu_vm_grab_id(vm, ring, job);
  142. out:
  143. return r;
  144. }
  145. /**
  146. * amdgpu_vm_grab_id - allocate the next free VMID
  147. *
  148. * @vm: vm to allocate id for
  149. * @ring: ring we want to submit job to
  150. * @sync: sync object where we add dependencies
  151. * @fence: fence protecting ID from reuse
  152. *
  153. * Allocate an id for the vm, adding fences to the sync obj as necessary.
  154. */
  155. int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  156. struct amdgpu_sync *sync, struct dma_fence *fence,
  157. struct amdgpu_job *job)
  158. {
  159. struct amdgpu_device *adev = ring->adev;
  160. unsigned vmhub = ring->funcs->vmhub;
  161. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  162. uint64_t fence_context = adev->fence_context + ring->idx;
  163. struct dma_fence *updates = sync->last_vm_update;
  164. struct amdgpu_vmid *id, *idle;
  165. struct dma_fence **fences;
  166. unsigned i;
  167. int r = 0;
  168. mutex_lock(&id_mgr->lock);
  169. if (vm->reserved_vmid[vmhub]) {
  170. r = amdgpu_vmid_grab_reserved_locked(vm, ring, sync, fence, job);
  171. mutex_unlock(&id_mgr->lock);
  172. return r;
  173. }
  174. fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
  175. if (!fences) {
  176. mutex_unlock(&id_mgr->lock);
  177. return -ENOMEM;
  178. }
  179. /* Check if we have an idle VMID */
  180. i = 0;
  181. list_for_each_entry(idle, &id_mgr->ids_lru, list) {
  182. fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
  183. if (!fences[i])
  184. break;
  185. ++i;
  186. }
  187. /* If we can't find a idle VMID to use, wait till one becomes available */
  188. if (&idle->list == &id_mgr->ids_lru) {
  189. u64 fence_context = adev->vm_manager.fence_context + ring->idx;
  190. unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
  191. struct dma_fence_array *array;
  192. unsigned j;
  193. for (j = 0; j < i; ++j)
  194. dma_fence_get(fences[j]);
  195. array = dma_fence_array_create(i, fences, fence_context,
  196. seqno, true);
  197. if (!array) {
  198. for (j = 0; j < i; ++j)
  199. dma_fence_put(fences[j]);
  200. kfree(fences);
  201. r = -ENOMEM;
  202. goto error;
  203. }
  204. r = amdgpu_sync_fence(ring->adev, sync, &array->base, false);
  205. dma_fence_put(&array->base);
  206. if (r)
  207. goto error;
  208. mutex_unlock(&id_mgr->lock);
  209. return 0;
  210. }
  211. kfree(fences);
  212. job->vm_needs_flush = vm->use_cpu_for_update;
  213. /* Check if we can use a VMID already assigned to this VM */
  214. list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
  215. struct dma_fence *flushed;
  216. bool needs_flush = vm->use_cpu_for_update;
  217. /* Check all the prerequisites to using this VMID */
  218. if (amdgpu_vmid_had_gpu_reset(adev, id))
  219. continue;
  220. if (atomic64_read(&id->owner) != vm->entity.fence_context)
  221. continue;
  222. if (job->vm_pd_addr != id->pd_gpu_addr)
  223. continue;
  224. if (!id->last_flush ||
  225. (id->last_flush->context != fence_context &&
  226. !dma_fence_is_signaled(id->last_flush)))
  227. needs_flush = true;
  228. flushed = id->flushed_updates;
  229. if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
  230. needs_flush = true;
  231. /* Concurrent flushes are only possible starting with Vega10 */
  232. if (adev->asic_type < CHIP_VEGA10 && needs_flush)
  233. continue;
  234. /* Good we can use this VMID. Remember this submission as
  235. * user of the VMID.
  236. */
  237. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  238. if (r)
  239. goto error;
  240. if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
  241. dma_fence_put(id->flushed_updates);
  242. id->flushed_updates = dma_fence_get(updates);
  243. }
  244. if (needs_flush)
  245. goto needs_flush;
  246. else
  247. goto no_flush_needed;
  248. };
  249. /* Still no ID to use? Then use the idle one found earlier */
  250. id = idle;
  251. /* Remember this submission as user of the VMID */
  252. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  253. if (r)
  254. goto error;
  255. id->pd_gpu_addr = job->vm_pd_addr;
  256. dma_fence_put(id->flushed_updates);
  257. id->flushed_updates = dma_fence_get(updates);
  258. atomic64_set(&id->owner, vm->entity.fence_context);
  259. needs_flush:
  260. job->vm_needs_flush = true;
  261. dma_fence_put(id->last_flush);
  262. id->last_flush = NULL;
  263. no_flush_needed:
  264. list_move_tail(&id->list, &id_mgr->ids_lru);
  265. job->vmid = id - id_mgr->ids;
  266. trace_amdgpu_vm_grab_id(vm, ring, job);
  267. error:
  268. mutex_unlock(&id_mgr->lock);
  269. return r;
  270. }
  271. int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev,
  272. struct amdgpu_vm *vm,
  273. unsigned vmhub)
  274. {
  275. struct amdgpu_vmid_mgr *id_mgr;
  276. struct amdgpu_vmid *idle;
  277. int r = 0;
  278. id_mgr = &adev->vm_manager.id_mgr[vmhub];
  279. mutex_lock(&id_mgr->lock);
  280. if (vm->reserved_vmid[vmhub])
  281. goto unlock;
  282. if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
  283. AMDGPU_VM_MAX_RESERVED_VMID) {
  284. DRM_ERROR("Over limitation of reserved vmid\n");
  285. atomic_dec(&id_mgr->reserved_vmid_num);
  286. r = -EINVAL;
  287. goto unlock;
  288. }
  289. /* Select the first entry VMID */
  290. idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vmid, list);
  291. list_del_init(&idle->list);
  292. vm->reserved_vmid[vmhub] = idle;
  293. mutex_unlock(&id_mgr->lock);
  294. return 0;
  295. unlock:
  296. mutex_unlock(&id_mgr->lock);
  297. return r;
  298. }
  299. void amdgpu_vmid_free_reserved(struct amdgpu_device *adev,
  300. struct amdgpu_vm *vm,
  301. unsigned vmhub)
  302. {
  303. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  304. mutex_lock(&id_mgr->lock);
  305. if (vm->reserved_vmid[vmhub]) {
  306. list_add(&vm->reserved_vmid[vmhub]->list,
  307. &id_mgr->ids_lru);
  308. vm->reserved_vmid[vmhub] = NULL;
  309. atomic_dec(&id_mgr->reserved_vmid_num);
  310. }
  311. mutex_unlock(&id_mgr->lock);
  312. }
  313. /**
  314. * amdgpu_vmid_reset - reset VMID to zero
  315. *
  316. * @adev: amdgpu device structure
  317. * @vmid: vmid number to use
  318. *
  319. * Reset saved GDW, GWS and OA to force switch on next flush.
  320. */
  321. void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub,
  322. unsigned vmid)
  323. {
  324. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  325. struct amdgpu_vmid *id = &id_mgr->ids[vmid];
  326. atomic64_set(&id->owner, 0);
  327. id->gds_base = 0;
  328. id->gds_size = 0;
  329. id->gws_base = 0;
  330. id->gws_size = 0;
  331. id->oa_base = 0;
  332. id->oa_size = 0;
  333. }
  334. /**
  335. * amdgpu_vmid_reset_all - reset VMID to zero
  336. *
  337. * @adev: amdgpu device structure
  338. *
  339. * Reset VMID to force flush on next use
  340. */
  341. void amdgpu_vmid_reset_all(struct amdgpu_device *adev)
  342. {
  343. unsigned i, j;
  344. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  345. struct amdgpu_vmid_mgr *id_mgr =
  346. &adev->vm_manager.id_mgr[i];
  347. for (j = 1; j < id_mgr->num_ids; ++j)
  348. amdgpu_vmid_reset(adev, i, j);
  349. }
  350. }
  351. /**
  352. * amdgpu_vmid_mgr_init - init the VMID manager
  353. *
  354. * @adev: amdgpu_device pointer
  355. *
  356. * Initialize the VM manager structures
  357. */
  358. void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
  359. {
  360. unsigned i, j;
  361. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  362. struct amdgpu_vmid_mgr *id_mgr =
  363. &adev->vm_manager.id_mgr[i];
  364. mutex_init(&id_mgr->lock);
  365. INIT_LIST_HEAD(&id_mgr->ids_lru);
  366. atomic_set(&id_mgr->reserved_vmid_num, 0);
  367. /* skip over VMID 0, since it is the system VM */
  368. for (j = 1; j < id_mgr->num_ids; ++j) {
  369. amdgpu_vmid_reset(adev, i, j);
  370. amdgpu_sync_create(&id_mgr->ids[i].active);
  371. list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
  372. }
  373. }
  374. adev->vm_manager.fence_context =
  375. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  376. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  377. adev->vm_manager.seqno[i] = 0;
  378. }
  379. /**
  380. * amdgpu_vmid_mgr_fini - cleanup VM manager
  381. *
  382. * @adev: amdgpu_device pointer
  383. *
  384. * Cleanup the VM manager and free resources.
  385. */
  386. void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
  387. {
  388. unsigned i, j;
  389. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  390. struct amdgpu_vmid_mgr *id_mgr =
  391. &adev->vm_manager.id_mgr[i];
  392. mutex_destroy(&id_mgr->lock);
  393. for (j = 0; j < AMDGPU_NUM_VMID; ++j) {
  394. struct amdgpu_vmid *id = &id_mgr->ids[j];
  395. amdgpu_sync_free(&id->active);
  396. dma_fence_put(id->flushed_updates);
  397. dma_fence_put(id->last_flush);
  398. }
  399. }
  400. }