amdgpu_ids.c 16 KB

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