GNU Linux-libre 4.4.283-gnu1
[releases.git] / drivers / gpu / drm / msm / msm_ringbuffer.c
1 /*
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
21 struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int size)
22 {
23         struct msm_ringbuffer *ring;
24         int ret;
25
26         if (WARN_ON(!is_power_of_2(size)))
27                 return ERR_PTR(-EINVAL);
28
29         ring = kzalloc(sizeof(*ring), GFP_KERNEL);
30         if (!ring) {
31                 ret = -ENOMEM;
32                 goto fail;
33         }
34
35         ring->gpu = gpu;
36         ring->bo = msm_gem_new(gpu->dev, size, MSM_BO_WC);
37         if (IS_ERR(ring->bo)) {
38                 ret = PTR_ERR(ring->bo);
39                 ring->bo = NULL;
40                 goto fail;
41         }
42
43         ring->start = msm_gem_vaddr_locked(ring->bo);
44         ring->end   = ring->start + (size / 4);
45         ring->cur   = ring->start;
46
47         ring->size = size;
48
49         return ring;
50
51 fail:
52         if (ring)
53                 msm_ringbuffer_destroy(ring);
54         return ERR_PTR(ret);
55 }
56
57 void msm_ringbuffer_destroy(struct msm_ringbuffer *ring)
58 {
59         if (ring->bo)
60                 drm_gem_object_unreference_unlocked(ring->bo);
61         kfree(ring);
62 }