amdgpu_ids.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. /* Helper to free pasid from a fence callback */
  40. struct amdgpu_pasid_cb {
  41. struct dma_fence_cb cb;
  42. unsigned int pasid;
  43. };
  44. /**
  45. * amdgpu_pasid_alloc - Allocate a PASID
  46. * @bits: Maximum width of the PASID in bits, must be at least 1
  47. *
  48. * Allocates a PASID of the given width while keeping smaller PASIDs
  49. * available if possible.
  50. *
  51. * Returns a positive integer on success. Returns %-EINVAL if bits==0.
  52. * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
  53. * memory allocation failure.
  54. */
  55. int amdgpu_pasid_alloc(unsigned int bits)
  56. {
  57. int pasid = -EINVAL;
  58. for (bits = min(bits, 31U); bits > 0; bits--) {
  59. pasid = ida_simple_get(&amdgpu_pasid_ida,
  60. 1U << (bits - 1), 1U << bits,
  61. GFP_KERNEL);
  62. if (pasid != -ENOSPC)
  63. break;
  64. }
  65. if (pasid >= 0)
  66. trace_amdgpu_pasid_allocated(pasid);
  67. return pasid;
  68. }
  69. /**
  70. * amdgpu_pasid_free - Free a PASID
  71. * @pasid: PASID to free
  72. */
  73. void amdgpu_pasid_free(unsigned int pasid)
  74. {
  75. trace_amdgpu_pasid_freed(pasid);
  76. ida_simple_remove(&amdgpu_pasid_ida, pasid);
  77. }
  78. static void amdgpu_pasid_free_cb(struct dma_fence *fence,
  79. struct dma_fence_cb *_cb)
  80. {
  81. struct amdgpu_pasid_cb *cb =
  82. container_of(_cb, struct amdgpu_pasid_cb, cb);
  83. amdgpu_pasid_free(cb->pasid);
  84. dma_fence_put(fence);
  85. kfree(cb);
  86. }
  87. /**
  88. * amdgpu_pasid_free_delayed - free pasid when fences signal
  89. *
  90. * @resv: reservation object with the fences to wait for
  91. * @pasid: pasid to free
  92. *
  93. * Free the pasid only after all the fences in resv are signaled.
  94. */
  95. void amdgpu_pasid_free_delayed(struct reservation_object *resv,
  96. unsigned int pasid)
  97. {
  98. struct dma_fence *fence, **fences;
  99. struct amdgpu_pasid_cb *cb;
  100. unsigned count;
  101. int r;
  102. r = reservation_object_get_fences_rcu(resv, NULL, &count, &fences);
  103. if (r)
  104. goto fallback;
  105. if (count == 0) {
  106. amdgpu_pasid_free(pasid);
  107. return;
  108. }
  109. if (count == 1) {
  110. fence = fences[0];
  111. kfree(fences);
  112. } else {
  113. uint64_t context = dma_fence_context_alloc(1);
  114. struct dma_fence_array *array;
  115. array = dma_fence_array_create(count, fences, context,
  116. 1, false);
  117. if (!array) {
  118. kfree(fences);
  119. goto fallback;
  120. }
  121. fence = &array->base;
  122. }
  123. cb = kmalloc(sizeof(*cb), GFP_KERNEL);
  124. if (!cb) {
  125. /* Last resort when we are OOM */
  126. dma_fence_wait(fence, false);
  127. dma_fence_put(fence);
  128. amdgpu_pasid_free(pasid);
  129. } else {
  130. cb->pasid = pasid;
  131. if (dma_fence_add_callback(fence, &cb->cb,
  132. amdgpu_pasid_free_cb))
  133. amdgpu_pasid_free_cb(fence, &cb->cb);
  134. }
  135. return;
  136. fallback:
  137. /* Not enough memory for the delayed delete, as last resort
  138. * block for all the fences to complete.
  139. */
  140. reservation_object_wait_timeout_rcu(resv, true, false,
  141. MAX_SCHEDULE_TIMEOUT);
  142. amdgpu_pasid_free(pasid);
  143. }
  144. /*
  145. * VMID manager
  146. *
  147. * VMIDs are a per VMHUB identifier for page tables handling.
  148. */
  149. /**
  150. * amdgpu_vmid_had_gpu_reset - check if reset occured since last use
  151. *
  152. * @adev: amdgpu_device pointer
  153. * @id: VMID structure
  154. *
  155. * Check if GPU reset occured since last use of the VMID.
  156. */
  157. bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
  158. struct amdgpu_vmid *id)
  159. {
  160. return id->current_gpu_reset_count !=
  161. atomic_read(&adev->gpu_reset_counter);
  162. }
  163. /* idr_mgr->lock must be held */
  164. static int amdgpu_vmid_grab_reserved_locked(struct amdgpu_vm *vm,
  165. struct amdgpu_ring *ring,
  166. struct amdgpu_sync *sync,
  167. struct dma_fence *fence,
  168. struct amdgpu_job *job)
  169. {
  170. struct amdgpu_device *adev = ring->adev;
  171. unsigned vmhub = ring->funcs->vmhub;
  172. uint64_t fence_context = adev->fence_context + ring->idx;
  173. struct amdgpu_vmid *id = vm->reserved_vmid[vmhub];
  174. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  175. struct dma_fence *updates = sync->last_vm_update;
  176. int r = 0;
  177. struct dma_fence *flushed, *tmp;
  178. bool needs_flush = vm->use_cpu_for_update;
  179. flushed = id->flushed_updates;
  180. if ((amdgpu_vmid_had_gpu_reset(adev, id)) ||
  181. (atomic64_read(&id->owner) != vm->entity.fence_context) ||
  182. (job->vm_pd_addr != id->pd_gpu_addr) ||
  183. (updates && (!flushed || updates->context != flushed->context ||
  184. dma_fence_is_later(updates, flushed))) ||
  185. (!id->last_flush || (id->last_flush->context != fence_context &&
  186. !dma_fence_is_signaled(id->last_flush)))) {
  187. needs_flush = true;
  188. /* to prevent one context starved by another context */
  189. id->pd_gpu_addr = 0;
  190. tmp = amdgpu_sync_peek_fence(&id->active, ring);
  191. if (tmp) {
  192. r = amdgpu_sync_fence(adev, sync, tmp, false);
  193. return r;
  194. }
  195. }
  196. /* Good we can use this VMID. Remember this submission as
  197. * user of the VMID.
  198. */
  199. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  200. if (r)
  201. goto out;
  202. if (updates && (!flushed || updates->context != flushed->context ||
  203. dma_fence_is_later(updates, flushed))) {
  204. dma_fence_put(id->flushed_updates);
  205. id->flushed_updates = dma_fence_get(updates);
  206. }
  207. id->pd_gpu_addr = job->vm_pd_addr;
  208. atomic64_set(&id->owner, vm->entity.fence_context);
  209. job->vm_needs_flush = needs_flush;
  210. if (needs_flush) {
  211. dma_fence_put(id->last_flush);
  212. id->last_flush = NULL;
  213. }
  214. job->vmid = id - id_mgr->ids;
  215. trace_amdgpu_vm_grab_id(vm, ring, job);
  216. out:
  217. return r;
  218. }
  219. /**
  220. * amdgpu_vm_grab_id - allocate the next free VMID
  221. *
  222. * @vm: vm to allocate id for
  223. * @ring: ring we want to submit job to
  224. * @sync: sync object where we add dependencies
  225. * @fence: fence protecting ID from reuse
  226. *
  227. * Allocate an id for the vm, adding fences to the sync obj as necessary.
  228. */
  229. int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  230. struct amdgpu_sync *sync, struct dma_fence *fence,
  231. struct amdgpu_job *job)
  232. {
  233. struct amdgpu_device *adev = ring->adev;
  234. unsigned vmhub = ring->funcs->vmhub;
  235. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  236. uint64_t fence_context = adev->fence_context + ring->idx;
  237. struct dma_fence *updates = sync->last_vm_update;
  238. struct amdgpu_vmid *id, *idle;
  239. struct dma_fence **fences;
  240. unsigned i;
  241. int r = 0;
  242. mutex_lock(&id_mgr->lock);
  243. if (vm->reserved_vmid[vmhub]) {
  244. r = amdgpu_vmid_grab_reserved_locked(vm, ring, sync, fence, job);
  245. mutex_unlock(&id_mgr->lock);
  246. return r;
  247. }
  248. fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
  249. if (!fences) {
  250. mutex_unlock(&id_mgr->lock);
  251. return -ENOMEM;
  252. }
  253. /* Check if we have an idle VMID */
  254. i = 0;
  255. list_for_each_entry(idle, &id_mgr->ids_lru, list) {
  256. fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
  257. if (!fences[i])
  258. break;
  259. ++i;
  260. }
  261. /* If we can't find a idle VMID to use, wait till one becomes available */
  262. if (&idle->list == &id_mgr->ids_lru) {
  263. u64 fence_context = adev->vm_manager.fence_context + ring->idx;
  264. unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
  265. struct dma_fence_array *array;
  266. unsigned j;
  267. for (j = 0; j < i; ++j)
  268. dma_fence_get(fences[j]);
  269. array = dma_fence_array_create(i, fences, fence_context,
  270. seqno, true);
  271. if (!array) {
  272. for (j = 0; j < i; ++j)
  273. dma_fence_put(fences[j]);
  274. kfree(fences);
  275. r = -ENOMEM;
  276. goto error;
  277. }
  278. r = amdgpu_sync_fence(ring->adev, sync, &array->base, false);
  279. dma_fence_put(&array->base);
  280. if (r)
  281. goto error;
  282. mutex_unlock(&id_mgr->lock);
  283. return 0;
  284. }
  285. kfree(fences);
  286. job->vm_needs_flush = vm->use_cpu_for_update;
  287. /* Check if we can use a VMID already assigned to this VM */
  288. list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
  289. struct dma_fence *flushed;
  290. bool needs_flush = vm->use_cpu_for_update;
  291. /* Check all the prerequisites to using this VMID */
  292. if (amdgpu_vmid_had_gpu_reset(adev, id))
  293. continue;
  294. if (atomic64_read(&id->owner) != vm->entity.fence_context)
  295. continue;
  296. if (job->vm_pd_addr != id->pd_gpu_addr)
  297. continue;
  298. if (!id->last_flush ||
  299. (id->last_flush->context != fence_context &&
  300. !dma_fence_is_signaled(id->last_flush)))
  301. needs_flush = true;
  302. flushed = id->flushed_updates;
  303. if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
  304. needs_flush = true;
  305. /* Concurrent flushes are only possible starting with Vega10 */
  306. if (adev->asic_type < CHIP_VEGA10 && needs_flush)
  307. continue;
  308. /* Good we can use this VMID. Remember this submission as
  309. * user of the VMID.
  310. */
  311. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  312. if (r)
  313. goto error;
  314. if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
  315. dma_fence_put(id->flushed_updates);
  316. id->flushed_updates = dma_fence_get(updates);
  317. }
  318. if (needs_flush)
  319. goto needs_flush;
  320. else
  321. goto no_flush_needed;
  322. }
  323. /* Still no ID to use? Then use the idle one found earlier */
  324. id = idle;
  325. /* Remember this submission as user of the VMID */
  326. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  327. if (r)
  328. goto error;
  329. id->pd_gpu_addr = job->vm_pd_addr;
  330. dma_fence_put(id->flushed_updates);
  331. id->flushed_updates = dma_fence_get(updates);
  332. atomic64_set(&id->owner, vm->entity.fence_context);
  333. needs_flush:
  334. job->vm_needs_flush = true;
  335. dma_fence_put(id->last_flush);
  336. id->last_flush = NULL;
  337. no_flush_needed:
  338. list_move_tail(&id->list, &id_mgr->ids_lru);
  339. job->vmid = id - id_mgr->ids;
  340. trace_amdgpu_vm_grab_id(vm, ring, job);
  341. error:
  342. mutex_unlock(&id_mgr->lock);
  343. return r;
  344. }
  345. int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev,
  346. struct amdgpu_vm *vm,
  347. unsigned vmhub)
  348. {
  349. struct amdgpu_vmid_mgr *id_mgr;
  350. struct amdgpu_vmid *idle;
  351. int r = 0;
  352. id_mgr = &adev->vm_manager.id_mgr[vmhub];
  353. mutex_lock(&id_mgr->lock);
  354. if (vm->reserved_vmid[vmhub])
  355. goto unlock;
  356. if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
  357. AMDGPU_VM_MAX_RESERVED_VMID) {
  358. DRM_ERROR("Over limitation of reserved vmid\n");
  359. atomic_dec(&id_mgr->reserved_vmid_num);
  360. r = -EINVAL;
  361. goto unlock;
  362. }
  363. /* Select the first entry VMID */
  364. idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vmid, list);
  365. list_del_init(&idle->list);
  366. vm->reserved_vmid[vmhub] = idle;
  367. mutex_unlock(&id_mgr->lock);
  368. return 0;
  369. unlock:
  370. mutex_unlock(&id_mgr->lock);
  371. return r;
  372. }
  373. void amdgpu_vmid_free_reserved(struct amdgpu_device *adev,
  374. struct amdgpu_vm *vm,
  375. unsigned vmhub)
  376. {
  377. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  378. mutex_lock(&id_mgr->lock);
  379. if (vm->reserved_vmid[vmhub]) {
  380. list_add(&vm->reserved_vmid[vmhub]->list,
  381. &id_mgr->ids_lru);
  382. vm->reserved_vmid[vmhub] = NULL;
  383. atomic_dec(&id_mgr->reserved_vmid_num);
  384. }
  385. mutex_unlock(&id_mgr->lock);
  386. }
  387. /**
  388. * amdgpu_vmid_reset - reset VMID to zero
  389. *
  390. * @adev: amdgpu device structure
  391. * @vmid: vmid number to use
  392. *
  393. * Reset saved GDW, GWS and OA to force switch on next flush.
  394. */
  395. void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub,
  396. unsigned vmid)
  397. {
  398. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  399. struct amdgpu_vmid *id = &id_mgr->ids[vmid];
  400. atomic64_set(&id->owner, 0);
  401. id->gds_base = 0;
  402. id->gds_size = 0;
  403. id->gws_base = 0;
  404. id->gws_size = 0;
  405. id->oa_base = 0;
  406. id->oa_size = 0;
  407. }
  408. /**
  409. * amdgpu_vmid_reset_all - reset VMID to zero
  410. *
  411. * @adev: amdgpu device structure
  412. *
  413. * Reset VMID to force flush on next use
  414. */
  415. void amdgpu_vmid_reset_all(struct amdgpu_device *adev)
  416. {
  417. unsigned i, j;
  418. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  419. struct amdgpu_vmid_mgr *id_mgr =
  420. &adev->vm_manager.id_mgr[i];
  421. for (j = 1; j < id_mgr->num_ids; ++j)
  422. amdgpu_vmid_reset(adev, i, j);
  423. }
  424. }
  425. /**
  426. * amdgpu_vmid_mgr_init - init the VMID manager
  427. *
  428. * @adev: amdgpu_device pointer
  429. *
  430. * Initialize the VM manager structures
  431. */
  432. void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
  433. {
  434. unsigned i, j;
  435. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  436. struct amdgpu_vmid_mgr *id_mgr =
  437. &adev->vm_manager.id_mgr[i];
  438. mutex_init(&id_mgr->lock);
  439. INIT_LIST_HEAD(&id_mgr->ids_lru);
  440. atomic_set(&id_mgr->reserved_vmid_num, 0);
  441. /* skip over VMID 0, since it is the system VM */
  442. for (j = 1; j < id_mgr->num_ids; ++j) {
  443. amdgpu_vmid_reset(adev, i, j);
  444. amdgpu_sync_create(&id_mgr->ids[i].active);
  445. list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
  446. }
  447. }
  448. adev->vm_manager.fence_context =
  449. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  450. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  451. adev->vm_manager.seqno[i] = 0;
  452. }
  453. /**
  454. * amdgpu_vmid_mgr_fini - cleanup VM manager
  455. *
  456. * @adev: amdgpu_device pointer
  457. *
  458. * Cleanup the VM manager and free resources.
  459. */
  460. void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
  461. {
  462. unsigned i, j;
  463. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  464. struct amdgpu_vmid_mgr *id_mgr =
  465. &adev->vm_manager.id_mgr[i];
  466. mutex_destroy(&id_mgr->lock);
  467. for (j = 0; j < AMDGPU_NUM_VMID; ++j) {
  468. struct amdgpu_vmid *id = &id_mgr->ids[j];
  469. amdgpu_sync_free(&id->active);
  470. dma_fence_put(id->flushed_updates);
  471. dma_fence_put(id->last_flush);
  472. }
  473. }
  474. }