panthor_heap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. // SPDX-License-Identifier: GPL-2.0 or MIT
  2. /* Copyright 2023 Collabora ltd. */
  3. #include <linux/iosys-map.h>
  4. #include <linux/rwsem.h>
  5. #include <drm/drm_print.h>
  6. #include <drm/panthor_drm.h>
  7. #include "panthor_device.h"
  8. #include "panthor_gem.h"
  9. #include "panthor_heap.h"
  10. #include "panthor_mmu.h"
  11. #include "panthor_regs.h"
  12. /*
  13. * The GPU heap context is an opaque structure used by the GPU to track the
  14. * heap allocations. The driver should only touch it to initialize it (zero all
  15. * fields). Because the CPU and GPU can both access this structure it is
  16. * required to be GPU cache line aligned.
  17. */
  18. #define HEAP_CONTEXT_SIZE 32
  19. /**
  20. * struct panthor_heap_chunk_header - Heap chunk header
  21. */
  22. struct panthor_heap_chunk_header {
  23. /**
  24. * @next: Next heap chunk in the list.
  25. *
  26. * This is a GPU VA.
  27. */
  28. u64 next;
  29. /** @unknown: MBZ. */
  30. u32 unknown[14];
  31. };
  32. /**
  33. * struct panthor_heap_chunk - Structure used to keep track of allocated heap chunks.
  34. */
  35. struct panthor_heap_chunk {
  36. /** @node: Used to insert the heap chunk in panthor_heap::chunks. */
  37. struct list_head node;
  38. /** @bo: Buffer object backing the heap chunk. */
  39. struct panthor_kernel_bo *bo;
  40. };
  41. /**
  42. * struct panthor_heap - Structure used to manage tiler heap contexts.
  43. */
  44. struct panthor_heap {
  45. /** @chunks: List containing all heap chunks allocated so far. */
  46. struct list_head chunks;
  47. /** @lock: Lock protecting insertion in the chunks list. */
  48. struct mutex lock;
  49. /** @chunk_size: Size of each chunk. */
  50. u32 chunk_size;
  51. /** @max_chunks: Maximum number of chunks. */
  52. u32 max_chunks;
  53. /**
  54. * @target_in_flight: Number of in-flight render passes after which
  55. * we'd let the FW wait for fragment job to finish instead of allocating new chunks.
  56. */
  57. u32 target_in_flight;
  58. /** @chunk_count: Number of heap chunks currently allocated. */
  59. u32 chunk_count;
  60. };
  61. #define MAX_HEAPS_PER_POOL 128
  62. /**
  63. * struct panthor_heap_pool - Pool of heap contexts
  64. *
  65. * The pool is attached to a panthor_file and can't be shared across processes.
  66. */
  67. struct panthor_heap_pool {
  68. /** @refcount: Reference count. */
  69. struct kref refcount;
  70. /** @ptdev: Device. */
  71. struct panthor_device *ptdev;
  72. /** @vm: VM this pool is bound to. */
  73. struct panthor_vm *vm;
  74. /** @lock: Lock protecting access to @xa. */
  75. struct rw_semaphore lock;
  76. /** @xa: Array storing panthor_heap objects. */
  77. struct xarray xa;
  78. /** @gpu_contexts: Buffer object containing the GPU heap contexts. */
  79. struct panthor_kernel_bo *gpu_contexts;
  80. /** @size: Size of all chunks across all heaps in the pool. */
  81. atomic_t size;
  82. };
  83. static int panthor_heap_ctx_stride(struct panthor_device *ptdev)
  84. {
  85. u32 l2_features = ptdev->gpu_info.l2_features;
  86. u32 gpu_cache_line_size = GPU_L2_FEATURES_LINE_SIZE(l2_features);
  87. return ALIGN(HEAP_CONTEXT_SIZE, gpu_cache_line_size);
  88. }
  89. static int panthor_get_heap_ctx_offset(struct panthor_heap_pool *pool, int id)
  90. {
  91. return panthor_heap_ctx_stride(pool->ptdev) * id;
  92. }
  93. static void *panthor_get_heap_ctx(struct panthor_heap_pool *pool, int id)
  94. {
  95. return pool->gpu_contexts->kmap +
  96. panthor_get_heap_ctx_offset(pool, id);
  97. }
  98. static void panthor_free_heap_chunk(struct panthor_heap_pool *pool,
  99. struct panthor_heap *heap,
  100. struct panthor_heap_chunk *chunk)
  101. {
  102. mutex_lock(&heap->lock);
  103. list_del(&chunk->node);
  104. heap->chunk_count--;
  105. mutex_unlock(&heap->lock);
  106. atomic_sub(heap->chunk_size, &pool->size);
  107. panthor_kernel_bo_destroy(chunk->bo);
  108. kfree(chunk);
  109. }
  110. static int panthor_alloc_heap_chunk(struct panthor_heap_pool *pool,
  111. struct panthor_heap *heap,
  112. bool initial_chunk)
  113. {
  114. struct panthor_heap_chunk *chunk;
  115. struct panthor_heap_chunk_header *hdr;
  116. int ret;
  117. chunk = kmalloc_obj(*chunk);
  118. if (!chunk)
  119. return -ENOMEM;
  120. chunk->bo = panthor_kernel_bo_create(pool->ptdev, pool->vm, heap->chunk_size,
  121. DRM_PANTHOR_BO_NO_MMAP,
  122. DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC,
  123. PANTHOR_VM_KERNEL_AUTO_VA,
  124. "Tiler heap chunk");
  125. if (IS_ERR(chunk->bo)) {
  126. ret = PTR_ERR(chunk->bo);
  127. goto err_free_chunk;
  128. }
  129. ret = panthor_kernel_bo_vmap(chunk->bo);
  130. if (ret)
  131. goto err_destroy_bo;
  132. hdr = chunk->bo->kmap;
  133. memset(hdr, 0, sizeof(*hdr));
  134. if (initial_chunk && !list_empty(&heap->chunks)) {
  135. struct panthor_heap_chunk *prev_chunk;
  136. u64 prev_gpuva;
  137. prev_chunk = list_first_entry(&heap->chunks,
  138. struct panthor_heap_chunk,
  139. node);
  140. prev_gpuva = panthor_kernel_bo_gpuva(prev_chunk->bo);
  141. hdr->next = (prev_gpuva & GENMASK_ULL(63, 12)) |
  142. (heap->chunk_size >> 12);
  143. }
  144. panthor_kernel_bo_vunmap(chunk->bo);
  145. mutex_lock(&heap->lock);
  146. list_add(&chunk->node, &heap->chunks);
  147. heap->chunk_count++;
  148. mutex_unlock(&heap->lock);
  149. atomic_add(heap->chunk_size, &pool->size);
  150. return 0;
  151. err_destroy_bo:
  152. panthor_kernel_bo_destroy(chunk->bo);
  153. err_free_chunk:
  154. kfree(chunk);
  155. return ret;
  156. }
  157. static void panthor_free_heap_chunks(struct panthor_heap_pool *pool,
  158. struct panthor_heap *heap)
  159. {
  160. struct panthor_heap_chunk *chunk, *tmp;
  161. list_for_each_entry_safe(chunk, tmp, &heap->chunks, node)
  162. panthor_free_heap_chunk(pool, heap, chunk);
  163. }
  164. static int panthor_alloc_heap_chunks(struct panthor_heap_pool *pool,
  165. struct panthor_heap *heap,
  166. u32 chunk_count)
  167. {
  168. int ret;
  169. u32 i;
  170. for (i = 0; i < chunk_count; i++) {
  171. ret = panthor_alloc_heap_chunk(pool, heap, true);
  172. if (ret)
  173. return ret;
  174. }
  175. return 0;
  176. }
  177. static int
  178. panthor_heap_destroy_locked(struct panthor_heap_pool *pool, u32 handle)
  179. {
  180. struct panthor_heap *heap;
  181. heap = xa_erase(&pool->xa, handle);
  182. if (!heap)
  183. return -EINVAL;
  184. panthor_free_heap_chunks(pool, heap);
  185. mutex_destroy(&heap->lock);
  186. kfree(heap);
  187. return 0;
  188. }
  189. /**
  190. * panthor_heap_destroy() - Destroy a heap context
  191. * @pool: Pool this context belongs to.
  192. * @handle: Handle returned by panthor_heap_create().
  193. */
  194. int panthor_heap_destroy(struct panthor_heap_pool *pool, u32 handle)
  195. {
  196. int ret;
  197. down_write(&pool->lock);
  198. ret = panthor_heap_destroy_locked(pool, handle);
  199. up_write(&pool->lock);
  200. return ret;
  201. }
  202. /**
  203. * panthor_heap_create() - Create a heap context
  204. * @pool: Pool to instantiate the heap context from.
  205. * @initial_chunk_count: Number of chunk allocated at initialization time.
  206. * Must be at least 1.
  207. * @chunk_size: The size of each chunk. Must be page-aligned and lie in the
  208. * [128k:8M] range.
  209. * @max_chunks: Maximum number of chunks that can be allocated.
  210. * @target_in_flight: Maximum number of in-flight render passes.
  211. * @heap_ctx_gpu_va: Pointer holding the GPU address of the allocated heap
  212. * context.
  213. * @first_chunk_gpu_va: Pointer holding the GPU address of the first chunk
  214. * assigned to the heap context.
  215. *
  216. * Return: a positive handle on success, a negative error otherwise.
  217. */
  218. int panthor_heap_create(struct panthor_heap_pool *pool,
  219. u32 initial_chunk_count,
  220. u32 chunk_size,
  221. u32 max_chunks,
  222. u32 target_in_flight,
  223. u64 *heap_ctx_gpu_va,
  224. u64 *first_chunk_gpu_va)
  225. {
  226. struct panthor_heap *heap;
  227. struct panthor_heap_chunk *first_chunk;
  228. struct panthor_vm *vm;
  229. int ret = 0;
  230. u32 id;
  231. if (initial_chunk_count == 0)
  232. return -EINVAL;
  233. if (initial_chunk_count > max_chunks)
  234. return -EINVAL;
  235. if (!IS_ALIGNED(chunk_size, PAGE_SIZE) ||
  236. chunk_size < SZ_128K || chunk_size > SZ_8M)
  237. return -EINVAL;
  238. down_read(&pool->lock);
  239. vm = panthor_vm_get(pool->vm);
  240. up_read(&pool->lock);
  241. /* The pool has been destroyed, we can't create a new heap. */
  242. if (!vm)
  243. return -EINVAL;
  244. heap = kzalloc_obj(*heap);
  245. if (!heap) {
  246. ret = -ENOMEM;
  247. goto err_put_vm;
  248. }
  249. mutex_init(&heap->lock);
  250. INIT_LIST_HEAD(&heap->chunks);
  251. heap->chunk_size = chunk_size;
  252. heap->max_chunks = max_chunks;
  253. heap->target_in_flight = target_in_flight;
  254. ret = panthor_alloc_heap_chunks(pool, heap, initial_chunk_count);
  255. if (ret)
  256. goto err_free_heap;
  257. first_chunk = list_first_entry(&heap->chunks,
  258. struct panthor_heap_chunk,
  259. node);
  260. *first_chunk_gpu_va = panthor_kernel_bo_gpuva(first_chunk->bo);
  261. down_write(&pool->lock);
  262. /* The pool has been destroyed, we can't create a new heap. */
  263. if (!pool->vm) {
  264. ret = -EINVAL;
  265. } else {
  266. ret = xa_alloc(&pool->xa, &id, heap,
  267. XA_LIMIT(0, MAX_HEAPS_PER_POOL - 1), GFP_KERNEL);
  268. if (!ret) {
  269. void *gpu_ctx = panthor_get_heap_ctx(pool, id);
  270. memset(gpu_ctx, 0, panthor_heap_ctx_stride(pool->ptdev));
  271. *heap_ctx_gpu_va = panthor_kernel_bo_gpuva(pool->gpu_contexts) +
  272. panthor_get_heap_ctx_offset(pool, id);
  273. }
  274. }
  275. up_write(&pool->lock);
  276. if (ret)
  277. goto err_free_heap;
  278. panthor_vm_put(vm);
  279. return id;
  280. err_free_heap:
  281. panthor_free_heap_chunks(pool, heap);
  282. mutex_destroy(&heap->lock);
  283. kfree(heap);
  284. err_put_vm:
  285. panthor_vm_put(vm);
  286. return ret;
  287. }
  288. /**
  289. * panthor_heap_return_chunk() - Return an unused heap chunk
  290. * @pool: The pool this heap belongs to.
  291. * @heap_gpu_va: The GPU address of the heap context.
  292. * @chunk_gpu_va: The chunk VA to return.
  293. *
  294. * This function is used when a chunk allocated with panthor_heap_grow()
  295. * couldn't be linked to the heap context through the FW interface because
  296. * the group requesting the allocation was scheduled out in the meantime.
  297. */
  298. int panthor_heap_return_chunk(struct panthor_heap_pool *pool,
  299. u64 heap_gpu_va,
  300. u64 chunk_gpu_va)
  301. {
  302. u64 offset = heap_gpu_va - panthor_kernel_bo_gpuva(pool->gpu_contexts);
  303. u32 heap_id = (u32)offset / panthor_heap_ctx_stride(pool->ptdev);
  304. struct panthor_heap_chunk *chunk, *tmp, *removed = NULL;
  305. struct panthor_heap *heap;
  306. int ret;
  307. if (offset > U32_MAX || heap_id >= MAX_HEAPS_PER_POOL)
  308. return -EINVAL;
  309. down_read(&pool->lock);
  310. heap = xa_load(&pool->xa, heap_id);
  311. if (!heap) {
  312. ret = -EINVAL;
  313. goto out_unlock;
  314. }
  315. chunk_gpu_va &= GENMASK_ULL(63, 12);
  316. mutex_lock(&heap->lock);
  317. list_for_each_entry_safe(chunk, tmp, &heap->chunks, node) {
  318. if (panthor_kernel_bo_gpuva(chunk->bo) == chunk_gpu_va) {
  319. removed = chunk;
  320. list_del(&chunk->node);
  321. heap->chunk_count--;
  322. atomic_sub(heap->chunk_size, &pool->size);
  323. break;
  324. }
  325. }
  326. mutex_unlock(&heap->lock);
  327. if (removed) {
  328. panthor_kernel_bo_destroy(chunk->bo);
  329. kfree(chunk);
  330. ret = 0;
  331. } else {
  332. ret = -EINVAL;
  333. }
  334. out_unlock:
  335. up_read(&pool->lock);
  336. return ret;
  337. }
  338. /**
  339. * panthor_heap_grow() - Make a heap context grow.
  340. * @pool: The pool this heap belongs to.
  341. * @heap_gpu_va: The GPU address of the heap context.
  342. * @renderpasses_in_flight: Number of render passes currently in-flight.
  343. * @pending_frag_count: Number of fragment jobs waiting for execution/completion.
  344. * @new_chunk_gpu_va: Pointer used to return the chunk VA.
  345. *
  346. * Return:
  347. * - 0 if a new heap was allocated
  348. * - -ENOMEM if the tiler context reached the maximum number of chunks
  349. * or if too many render passes are in-flight
  350. * or if the allocation failed
  351. * - -EINVAL if any of the arguments passed to panthor_heap_grow() is invalid
  352. */
  353. int panthor_heap_grow(struct panthor_heap_pool *pool,
  354. u64 heap_gpu_va,
  355. u32 renderpasses_in_flight,
  356. u32 pending_frag_count,
  357. u64 *new_chunk_gpu_va)
  358. {
  359. u64 offset = heap_gpu_va - panthor_kernel_bo_gpuva(pool->gpu_contexts);
  360. u32 heap_id = (u32)offset / panthor_heap_ctx_stride(pool->ptdev);
  361. struct panthor_heap_chunk *chunk;
  362. struct panthor_heap *heap;
  363. int ret;
  364. if (offset > U32_MAX || heap_id >= MAX_HEAPS_PER_POOL)
  365. return -EINVAL;
  366. down_read(&pool->lock);
  367. heap = xa_load(&pool->xa, heap_id);
  368. if (!heap) {
  369. ret = -EINVAL;
  370. goto out_unlock;
  371. }
  372. /* If we reached the target in-flight render passes, or if we
  373. * reached the maximum number of chunks, let the FW figure another way to
  374. * find some memory (wait for render passes to finish, or call the exception
  375. * handler provided by the userspace driver, if any).
  376. */
  377. if (renderpasses_in_flight > heap->target_in_flight ||
  378. heap->chunk_count >= heap->max_chunks) {
  379. ret = -ENOMEM;
  380. goto out_unlock;
  381. }
  382. /* FIXME: panthor_alloc_heap_chunk() triggers a kernel BO creation,
  383. * which goes through the blocking allocation path. Ultimately, we
  384. * want a non-blocking allocation, so we can immediately report to the
  385. * FW when the system is running out of memory. In that case, the FW
  386. * can call a user-provided exception handler, which might try to free
  387. * some tiler memory by issuing an intermediate fragment job. If the
  388. * exception handler can't do anything, it will flag the queue as
  389. * faulty so the job that triggered this tiler chunk allocation and all
  390. * further jobs in this queue fail immediately instead of having to
  391. * wait for the job timeout.
  392. */
  393. ret = panthor_alloc_heap_chunk(pool, heap, false);
  394. if (ret)
  395. goto out_unlock;
  396. chunk = list_first_entry(&heap->chunks,
  397. struct panthor_heap_chunk,
  398. node);
  399. *new_chunk_gpu_va = (panthor_kernel_bo_gpuva(chunk->bo) & GENMASK_ULL(63, 12)) |
  400. (heap->chunk_size >> 12);
  401. ret = 0;
  402. out_unlock:
  403. up_read(&pool->lock);
  404. return ret;
  405. }
  406. static void panthor_heap_pool_release(struct kref *refcount)
  407. {
  408. struct panthor_heap_pool *pool =
  409. container_of(refcount, struct panthor_heap_pool, refcount);
  410. xa_destroy(&pool->xa);
  411. kfree(pool);
  412. }
  413. /**
  414. * panthor_heap_pool_put() - Release a heap pool reference
  415. * @pool: Pool to release the reference on. Can be NULL.
  416. */
  417. void panthor_heap_pool_put(struct panthor_heap_pool *pool)
  418. {
  419. if (pool)
  420. kref_put(&pool->refcount, panthor_heap_pool_release);
  421. }
  422. /**
  423. * panthor_heap_pool_get() - Get a heap pool reference
  424. * @pool: Pool to get the reference on. Can be NULL.
  425. *
  426. * Return: @pool.
  427. */
  428. struct panthor_heap_pool *
  429. panthor_heap_pool_get(struct panthor_heap_pool *pool)
  430. {
  431. if (pool)
  432. kref_get(&pool->refcount);
  433. return pool;
  434. }
  435. /**
  436. * panthor_heap_pool_create() - Create a heap pool
  437. * @ptdev: Device.
  438. * @vm: The VM this heap pool will be attached to.
  439. *
  440. * Heap pools might contain up to 128 heap contexts, and are per-VM.
  441. *
  442. * Return: A valid pointer on success, a negative error code otherwise.
  443. */
  444. struct panthor_heap_pool *
  445. panthor_heap_pool_create(struct panthor_device *ptdev, struct panthor_vm *vm)
  446. {
  447. size_t bosize = ALIGN(MAX_HEAPS_PER_POOL *
  448. panthor_heap_ctx_stride(ptdev),
  449. 4096);
  450. struct panthor_heap_pool *pool;
  451. int ret = 0;
  452. pool = kzalloc_obj(*pool);
  453. if (!pool)
  454. return ERR_PTR(-ENOMEM);
  455. /* We want a weak ref here: the heap pool belongs to the VM, so we're
  456. * sure that, as long as the heap pool exists, the VM exists too.
  457. */
  458. pool->vm = vm;
  459. pool->ptdev = ptdev;
  460. init_rwsem(&pool->lock);
  461. xa_init_flags(&pool->xa, XA_FLAGS_ALLOC);
  462. kref_init(&pool->refcount);
  463. pool->gpu_contexts = panthor_kernel_bo_create(ptdev, vm, bosize,
  464. DRM_PANTHOR_BO_NO_MMAP,
  465. DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC,
  466. PANTHOR_VM_KERNEL_AUTO_VA,
  467. "Heap pool");
  468. if (IS_ERR(pool->gpu_contexts)) {
  469. ret = PTR_ERR(pool->gpu_contexts);
  470. goto err_destroy_pool;
  471. }
  472. ret = panthor_kernel_bo_vmap(pool->gpu_contexts);
  473. if (ret)
  474. goto err_destroy_pool;
  475. atomic_add(pool->gpu_contexts->obj->size, &pool->size);
  476. return pool;
  477. err_destroy_pool:
  478. panthor_heap_pool_destroy(pool);
  479. return ERR_PTR(ret);
  480. }
  481. /**
  482. * panthor_heap_pool_destroy() - Destroy a heap pool.
  483. * @pool: Pool to destroy.
  484. *
  485. * This function destroys all heap contexts and their resources. Thus
  486. * preventing any use of the heap context or the chunk attached to them
  487. * after that point.
  488. *
  489. * If the GPU still has access to some heap contexts, a fault should be
  490. * triggered, which should flag the command stream groups using these
  491. * context as faulty.
  492. *
  493. * The heap pool object is only released when all references to this pool
  494. * are released.
  495. */
  496. void panthor_heap_pool_destroy(struct panthor_heap_pool *pool)
  497. {
  498. struct panthor_heap *heap;
  499. unsigned long i;
  500. if (!pool)
  501. return;
  502. down_write(&pool->lock);
  503. xa_for_each(&pool->xa, i, heap)
  504. drm_WARN_ON(&pool->ptdev->base, panthor_heap_destroy_locked(pool, i));
  505. if (!IS_ERR_OR_NULL(pool->gpu_contexts)) {
  506. atomic_sub(pool->gpu_contexts->obj->size, &pool->size);
  507. panthor_kernel_bo_destroy(pool->gpu_contexts);
  508. }
  509. /* Reflects the fact the pool has been destroyed. */
  510. pool->vm = NULL;
  511. up_write(&pool->lock);
  512. panthor_heap_pool_put(pool);
  513. }
  514. /**
  515. * panthor_heap_pool_size() - Get a heap pool's total size
  516. * @pool: Pool whose total chunks size to return
  517. *
  518. * Returns the aggregated size of all chunks for all heaps in the pool
  519. *
  520. */
  521. size_t panthor_heap_pool_size(struct panthor_heap_pool *pool)
  522. {
  523. if (!pool)
  524. return 0;
  525. return atomic_read(&pool->size);
  526. }