1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include "msm_ringbuffer.h"
8 #include "msm_gpu.h"
9 
10 struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
11 		void *memptrs, uint64_t memptrs_iova)
12 {
13 	struct msm_ringbuffer *ring;
14 	char name[32];
15 	int ret;
16 
17 	/* We assume everwhere that MSM_GPU_RINGBUFFER_SZ is a power of 2 */
18 	BUILD_BUG_ON(!is_power_of_2(MSM_GPU_RINGBUFFER_SZ));
19 
20 	ring = kzalloc(sizeof(*ring), GFP_KERNEL);
21 	if (!ring) {
22 		ret = -ENOMEM;
23 		goto fail;
24 	}
25 
26 	ring->gpu = gpu;
27 	ring->id = id;
28 
29 	ring->start = msm_gem_kernel_new(gpu->dev, MSM_GPU_RINGBUFFER_SZ,
30 		MSM_BO_WC, gpu->aspace, &ring->bo, &ring->iova);
31 
32 	if (IS_ERR(ring->start)) {
33 		ret = PTR_ERR(ring->start);
34 		ring->start = 0;
35 		goto fail;
36 	}
37 
38 	msm_gem_object_set_name(ring->bo, "ring%d", id);
39 
40 	ring->end   = ring->start + (MSM_GPU_RINGBUFFER_SZ >> 2);
41 	ring->next  = ring->start;
42 	ring->cur   = ring->start;
43 
44 	ring->memptrs = memptrs;
45 	ring->memptrs_iova = memptrs_iova;
46 
47 	INIT_LIST_HEAD(&ring->submits);
48 	spin_lock_init(&ring->lock);
49 
50 	snprintf(name, sizeof(name), "gpu-ring-%d", ring->id);
51 
52 	ring->fctx = msm_fence_context_alloc(gpu->dev, name);
53 
54 	return ring;
55 
56 fail:
57 	msm_ringbuffer_destroy(ring);
58 	return ERR_PTR(ret);
59 }
60 
61 void msm_ringbuffer_destroy(struct msm_ringbuffer *ring)
62 {
63 	if (IS_ERR_OR_NULL(ring))
64 		return;
65 
66 	msm_fence_context_free(ring->fctx);
67 
68 	msm_gem_kernel_put(ring->bo, ring->gpu->aspace, false);
69 
70 	kfree(ring);
71 }
72