amdgpu_ids.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. /**
  164. * amdgpu_vm_grab_idle - grab idle VMID
  165. *
  166. * @vm: vm to allocate id for
  167. * @ring: ring we want to submit job to
  168. * @sync: sync object where we add dependencies
  169. * @idle: resulting idle VMID
  170. *
  171. * Try to find an idle VMID, if none is idle add a fence to wait to the sync
  172. * object. Returns -ENOMEM when we are out of memory.
  173. */
  174. static int amdgpu_vmid_grab_idle(struct amdgpu_vm *vm,
  175. struct amdgpu_ring *ring,
  176. struct amdgpu_sync *sync,
  177. struct amdgpu_vmid **idle)
  178. {
  179. struct amdgpu_device *adev = ring->adev;
  180. unsigned vmhub = ring->funcs->vmhub;
  181. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  182. struct dma_fence **fences;
  183. unsigned i;
  184. int r;
  185. fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
  186. if (!fences)
  187. return -ENOMEM;
  188. /* Check if we have an idle VMID */
  189. i = 0;
  190. list_for_each_entry((*idle), &id_mgr->ids_lru, list) {
  191. fences[i] = amdgpu_sync_peek_fence(&(*idle)->active, ring);
  192. if (!fences[i])
  193. break;
  194. ++i;
  195. }
  196. /* If we can't find a idle VMID to use, wait till one becomes available */
  197. if (&(*idle)->list == &id_mgr->ids_lru) {
  198. u64 fence_context = adev->vm_manager.fence_context + ring->idx;
  199. unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
  200. struct dma_fence_array *array;
  201. unsigned j;
  202. *idle = NULL;
  203. for (j = 0; j < i; ++j)
  204. dma_fence_get(fences[j]);
  205. array = dma_fence_array_create(i, fences, fence_context,
  206. seqno, true);
  207. if (!array) {
  208. for (j = 0; j < i; ++j)
  209. dma_fence_put(fences[j]);
  210. kfree(fences);
  211. return -ENOMEM;
  212. }
  213. r = amdgpu_sync_fence(adev, sync, &array->base, false);
  214. dma_fence_put(&array->base);
  215. return r;
  216. }
  217. kfree(fences);
  218. return 0;
  219. }
  220. /* idr_mgr->lock must be held */
  221. static int amdgpu_vmid_grab_reserved_locked(struct amdgpu_vm *vm,
  222. struct amdgpu_ring *ring,
  223. struct amdgpu_sync *sync,
  224. struct dma_fence *fence,
  225. struct amdgpu_job *job)
  226. {
  227. struct amdgpu_device *adev = ring->adev;
  228. unsigned vmhub = ring->funcs->vmhub;
  229. uint64_t fence_context = adev->fence_context + ring->idx;
  230. struct amdgpu_vmid *id = vm->reserved_vmid[vmhub];
  231. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  232. struct dma_fence *updates = sync->last_vm_update;
  233. int r = 0;
  234. struct dma_fence *flushed, *tmp;
  235. bool needs_flush = vm->use_cpu_for_update;
  236. flushed = id->flushed_updates;
  237. if ((amdgpu_vmid_had_gpu_reset(adev, id)) ||
  238. (atomic64_read(&id->owner) != vm->entity.fence_context) ||
  239. (job->vm_pd_addr != id->pd_gpu_addr) ||
  240. (updates && (!flushed || updates->context != flushed->context ||
  241. dma_fence_is_later(updates, flushed))) ||
  242. (!id->last_flush || (id->last_flush->context != fence_context &&
  243. !dma_fence_is_signaled(id->last_flush)))) {
  244. needs_flush = true;
  245. /* to prevent one context starved by another context */
  246. id->pd_gpu_addr = 0;
  247. tmp = amdgpu_sync_peek_fence(&id->active, ring);
  248. if (tmp) {
  249. r = amdgpu_sync_fence(adev, sync, tmp, false);
  250. return r;
  251. }
  252. }
  253. /* Good we can use this VMID. Remember this submission as
  254. * user of the VMID.
  255. */
  256. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  257. if (r)
  258. goto out;
  259. if (updates && (!flushed || updates->context != flushed->context ||
  260. dma_fence_is_later(updates, flushed))) {
  261. dma_fence_put(id->flushed_updates);
  262. id->flushed_updates = dma_fence_get(updates);
  263. }
  264. id->pd_gpu_addr = job->vm_pd_addr;
  265. atomic64_set(&id->owner, vm->entity.fence_context);
  266. job->vm_needs_flush = needs_flush;
  267. if (needs_flush) {
  268. dma_fence_put(id->last_flush);
  269. id->last_flush = NULL;
  270. }
  271. job->vmid = id - id_mgr->ids;
  272. job->pasid = vm->pasid;
  273. trace_amdgpu_vm_grab_id(vm, ring, job);
  274. out:
  275. return r;
  276. }
  277. /**
  278. * amdgpu_vm_grab_id - allocate the next free VMID
  279. *
  280. * @vm: vm to allocate id for
  281. * @ring: ring we want to submit job to
  282. * @sync: sync object where we add dependencies
  283. * @fence: fence protecting ID from reuse
  284. *
  285. * Allocate an id for the vm, adding fences to the sync obj as necessary.
  286. */
  287. int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
  288. struct amdgpu_sync *sync, struct dma_fence *fence,
  289. struct amdgpu_job *job)
  290. {
  291. struct amdgpu_device *adev = ring->adev;
  292. unsigned vmhub = ring->funcs->vmhub;
  293. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  294. uint64_t fence_context = adev->fence_context + ring->idx;
  295. struct dma_fence *updates = sync->last_vm_update;
  296. struct amdgpu_vmid *id, *idle;
  297. int r = 0;
  298. mutex_lock(&id_mgr->lock);
  299. r = amdgpu_vmid_grab_idle(vm, ring, sync, &idle);
  300. if (r || !idle)
  301. goto error;
  302. if (vm->reserved_vmid[vmhub]) {
  303. r = amdgpu_vmid_grab_reserved_locked(vm, ring, sync,
  304. fence, job);
  305. mutex_unlock(&id_mgr->lock);
  306. return r;
  307. }
  308. job->vm_needs_flush = vm->use_cpu_for_update;
  309. /* Check if we can use a VMID already assigned to this VM */
  310. list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
  311. struct dma_fence *flushed;
  312. bool needs_flush = vm->use_cpu_for_update;
  313. /* Check all the prerequisites to using this VMID */
  314. if (amdgpu_vmid_had_gpu_reset(adev, id))
  315. continue;
  316. if (atomic64_read(&id->owner) != vm->entity.fence_context)
  317. continue;
  318. if (job->vm_pd_addr != id->pd_gpu_addr)
  319. continue;
  320. if (!id->last_flush ||
  321. (id->last_flush->context != fence_context &&
  322. !dma_fence_is_signaled(id->last_flush)))
  323. needs_flush = true;
  324. flushed = id->flushed_updates;
  325. if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
  326. needs_flush = true;
  327. /* Concurrent flushes are only possible starting with Vega10 */
  328. if (adev->asic_type < CHIP_VEGA10 && needs_flush)
  329. continue;
  330. /* Good we can use this VMID. Remember this submission as
  331. * user of the VMID.
  332. */
  333. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  334. if (r)
  335. goto error;
  336. if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
  337. dma_fence_put(id->flushed_updates);
  338. id->flushed_updates = dma_fence_get(updates);
  339. }
  340. if (needs_flush)
  341. goto needs_flush;
  342. else
  343. goto no_flush_needed;
  344. }
  345. /* Still no ID to use? Then use the idle one found earlier */
  346. id = idle;
  347. /* Remember this submission as user of the VMID */
  348. r = amdgpu_sync_fence(ring->adev, &id->active, fence, false);
  349. if (r)
  350. goto error;
  351. id->pd_gpu_addr = job->vm_pd_addr;
  352. dma_fence_put(id->flushed_updates);
  353. id->flushed_updates = dma_fence_get(updates);
  354. atomic64_set(&id->owner, vm->entity.fence_context);
  355. needs_flush:
  356. job->vm_needs_flush = true;
  357. dma_fence_put(id->last_flush);
  358. id->last_flush = NULL;
  359. no_flush_needed:
  360. list_move_tail(&id->list, &id_mgr->ids_lru);
  361. job->vmid = id - id_mgr->ids;
  362. job->pasid = vm->pasid;
  363. trace_amdgpu_vm_grab_id(vm, ring, job);
  364. error:
  365. mutex_unlock(&id_mgr->lock);
  366. return r;
  367. }
  368. int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev,
  369. struct amdgpu_vm *vm,
  370. unsigned vmhub)
  371. {
  372. struct amdgpu_vmid_mgr *id_mgr;
  373. struct amdgpu_vmid *idle;
  374. int r = 0;
  375. id_mgr = &adev->vm_manager.id_mgr[vmhub];
  376. mutex_lock(&id_mgr->lock);
  377. if (vm->reserved_vmid[vmhub])
  378. goto unlock;
  379. if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
  380. AMDGPU_VM_MAX_RESERVED_VMID) {
  381. DRM_ERROR("Over limitation of reserved vmid\n");
  382. atomic_dec(&id_mgr->reserved_vmid_num);
  383. r = -EINVAL;
  384. goto unlock;
  385. }
  386. /* Select the first entry VMID */
  387. idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vmid, list);
  388. list_del_init(&idle->list);
  389. vm->reserved_vmid[vmhub] = idle;
  390. mutex_unlock(&id_mgr->lock);
  391. return 0;
  392. unlock:
  393. mutex_unlock(&id_mgr->lock);
  394. return r;
  395. }
  396. void amdgpu_vmid_free_reserved(struct amdgpu_device *adev,
  397. struct amdgpu_vm *vm,
  398. unsigned vmhub)
  399. {
  400. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  401. mutex_lock(&id_mgr->lock);
  402. if (vm->reserved_vmid[vmhub]) {
  403. list_add(&vm->reserved_vmid[vmhub]->list,
  404. &id_mgr->ids_lru);
  405. vm->reserved_vmid[vmhub] = NULL;
  406. atomic_dec(&id_mgr->reserved_vmid_num);
  407. }
  408. mutex_unlock(&id_mgr->lock);
  409. }
  410. /**
  411. * amdgpu_vmid_reset - reset VMID to zero
  412. *
  413. * @adev: amdgpu device structure
  414. * @vmid: vmid number to use
  415. *
  416. * Reset saved GDW, GWS and OA to force switch on next flush.
  417. */
  418. void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub,
  419. unsigned vmid)
  420. {
  421. struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
  422. struct amdgpu_vmid *id = &id_mgr->ids[vmid];
  423. atomic64_set(&id->owner, 0);
  424. id->gds_base = 0;
  425. id->gds_size = 0;
  426. id->gws_base = 0;
  427. id->gws_size = 0;
  428. id->oa_base = 0;
  429. id->oa_size = 0;
  430. }
  431. /**
  432. * amdgpu_vmid_reset_all - reset VMID to zero
  433. *
  434. * @adev: amdgpu device structure
  435. *
  436. * Reset VMID to force flush on next use
  437. */
  438. void amdgpu_vmid_reset_all(struct amdgpu_device *adev)
  439. {
  440. unsigned i, j;
  441. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  442. struct amdgpu_vmid_mgr *id_mgr =
  443. &adev->vm_manager.id_mgr[i];
  444. for (j = 1; j < id_mgr->num_ids; ++j)
  445. amdgpu_vmid_reset(adev, i, j);
  446. }
  447. }
  448. /**
  449. * amdgpu_vmid_mgr_init - init the VMID manager
  450. *
  451. * @adev: amdgpu_device pointer
  452. *
  453. * Initialize the VM manager structures
  454. */
  455. void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
  456. {
  457. unsigned i, j;
  458. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  459. struct amdgpu_vmid_mgr *id_mgr =
  460. &adev->vm_manager.id_mgr[i];
  461. mutex_init(&id_mgr->lock);
  462. INIT_LIST_HEAD(&id_mgr->ids_lru);
  463. atomic_set(&id_mgr->reserved_vmid_num, 0);
  464. /* skip over VMID 0, since it is the system VM */
  465. for (j = 1; j < id_mgr->num_ids; ++j) {
  466. amdgpu_vmid_reset(adev, i, j);
  467. amdgpu_sync_create(&id_mgr->ids[i].active);
  468. list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
  469. }
  470. }
  471. adev->vm_manager.fence_context =
  472. dma_fence_context_alloc(AMDGPU_MAX_RINGS);
  473. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  474. adev->vm_manager.seqno[i] = 0;
  475. }
  476. /**
  477. * amdgpu_vmid_mgr_fini - cleanup VM manager
  478. *
  479. * @adev: amdgpu_device pointer
  480. *
  481. * Cleanup the VM manager and free resources.
  482. */
  483. void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
  484. {
  485. unsigned i, j;
  486. for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
  487. struct amdgpu_vmid_mgr *id_mgr =
  488. &adev->vm_manager.id_mgr[i];
  489. mutex_destroy(&id_mgr->lock);
  490. for (j = 0; j < AMDGPU_NUM_VMID; ++j) {
  491. struct amdgpu_vmid *id = &id_mgr->ids[j];
  492. amdgpu_sync_free(&id->active);
  493. dma_fence_put(id->flushed_updates);
  494. dma_fence_put(id->last_flush);
  495. }
  496. }
  497. }