Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 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 | |
Jordan Crouse | 88b333b | 2016-12-20 08:54:29 -0700 | [diff] [blame] | 26 | if (WARN_ON(!is_power_of_2(size))) |
| 27 | return ERR_PTR(-EINVAL); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 28 | |
| 29 | ring = kzalloc(sizeof(*ring), GFP_KERNEL); |
| 30 | if (!ring) { |
| 31 | ret = -ENOMEM; |
| 32 | goto fail; |
| 33 | } |
| 34 | |
| 35 | ring->gpu = gpu; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 36 | |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 37 | /* Pass NULL for the iova pointer - we will map it later */ |
| 38 | ring->start = msm_gem_kernel_new(gpu->dev, size, MSM_BO_WC, |
| 39 | gpu->aspace, &ring->bo, NULL); |
| 40 | |
Rob Clark | 69a834c | 2016-05-24 18:29:38 -0400 | [diff] [blame] | 41 | if (IS_ERR(ring->start)) { |
| 42 | ret = PTR_ERR(ring->start); |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 43 | ring->start = 0; |
Rob Clark | 69a834c | 2016-05-24 18:29:38 -0400 | [diff] [blame] | 44 | goto fail; |
| 45 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 46 | ring->end = ring->start + (size / 4); |
| 47 | ring->cur = ring->start; |
| 48 | |
| 49 | ring->size = size; |
| 50 | |
| 51 | return ring; |
| 52 | |
| 53 | fail: |
| 54 | if (ring) |
| 55 | msm_ringbuffer_destroy(ring); |
| 56 | return ERR_PTR(ret); |
| 57 | } |
| 58 | |
| 59 | void msm_ringbuffer_destroy(struct msm_ringbuffer *ring) |
| 60 | { |
Rob Clark | 18f2304 | 2016-05-26 16:24:35 -0400 | [diff] [blame] | 61 | if (ring->bo) { |
| 62 | msm_gem_put_vaddr(ring->bo); |
Rob Clark | 774449e | 2015-05-15 09:19:36 -0400 | [diff] [blame] | 63 | drm_gem_object_unreference_unlocked(ring->bo); |
Rob Clark | 18f2304 | 2016-05-26 16:24:35 -0400 | [diff] [blame] | 64 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 65 | kfree(ring); |
| 66 | } |