panthor_heap.c 15 KB

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