blob: f326cf6a32e64473461e0b7859101f1a404c0318 [file] [log] [blame]
Rob Clark7198e6b2013-07-19 12:59:32 -04001/*
2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "msm_ringbuffer.h"
19#include "msm_gpu.h"
20
21struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int size)
22{
23 struct msm_ringbuffer *ring;
24 int ret;
25
26 size = ALIGN(size, 4); /* size should be dword aligned */
27
28 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
29 if (!ring) {
30 ret = -ENOMEM;
31 goto fail;
32 }
33
34 ring->gpu = gpu;
35 ring->bo = msm_gem_new(gpu->dev, size, MSM_BO_WC);
36 if (IS_ERR(ring->bo)) {
37 ret = PTR_ERR(ring->bo);
38 ring->bo = NULL;
39 goto fail;
40 }
41
Rob Clark18f23042016-05-26 16:24:35 -040042 ring->start = msm_gem_get_vaddr_locked(ring->bo);
Rob Clark69a834c2016-05-24 18:29:38 -040043 if (IS_ERR(ring->start)) {
44 ret = PTR_ERR(ring->start);
45 goto fail;
46 }
Rob Clark7198e6b2013-07-19 12:59:32 -040047 ring->end = ring->start + (size / 4);
48 ring->cur = ring->start;
49
50 ring->size = size;
51
52 return ring;
53
54fail:
55 if (ring)
56 msm_ringbuffer_destroy(ring);
57 return ERR_PTR(ret);
58}
59
60void msm_ringbuffer_destroy(struct msm_ringbuffer *ring)
61{
Rob Clark18f23042016-05-26 16:24:35 -040062 if (ring->bo) {
63 msm_gem_put_vaddr(ring->bo);
Rob Clark774449e2015-05-15 09:19:36 -040064 drm_gem_object_unreference_unlocked(ring->bo);
Rob Clark18f23042016-05-26 16:24:35 -040065 }
Rob Clark7198e6b2013-07-19 12:59:32 -040066 kfree(ring);
67}