Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_buf.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Author: Inki Dae <inki.dae@samsung.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 26 | #include <drm/drmP.h> |
| 27 | #include <drm/exynos_drm.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 28 | |
| 29 | #include "exynos_drm_drv.h" |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 30 | #include "exynos_drm_gem.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 31 | #include "exynos_drm_buf.h" |
| 32 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 33 | static int lowlevel_buffer_allocate(struct drm_device *dev, |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 34 | unsigned int flags, struct exynos_drm_gem_buf *buf) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 35 | { |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 36 | int ret = 0; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 37 | enum dma_attr attr = DMA_ATTR_FORCE_CONTIGUOUS; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 38 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 39 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 40 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 41 | if (buf->dma_addr) { |
| 42 | DRM_DEBUG_KMS("already allocated.\n"); |
| 43 | return 0; |
| 44 | } |
| 45 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 46 | init_dma_attrs(&buf->dma_attrs); |
| 47 | |
| 48 | if (flags & EXYNOS_BO_NONCONTIG) |
| 49 | attr = DMA_ATTR_WRITE_COMBINE; |
| 50 | |
| 51 | dma_set_attr(attr, &buf->dma_attrs); |
| 52 | |
| 53 | buf->kvaddr = dma_alloc_attrs(dev->dev, buf->size, |
| 54 | &buf->dma_addr, GFP_KERNEL, &buf->dma_attrs); |
| 55 | if (!buf->kvaddr) { |
| 56 | DRM_ERROR("failed to allocate buffer.\n"); |
| 57 | return -ENOMEM; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 61 | if (!buf->sgt) { |
| 62 | DRM_ERROR("failed to allocate sg table.\n"); |
Inki Dae | 61db75d | 2012-04-03 21:49:15 +0900 | [diff] [blame] | 63 | ret = -ENOMEM; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 64 | goto err_free_attrs; |
Inki Dae | 61db75d | 2012-04-03 21:49:15 +0900 | [diff] [blame] | 65 | } |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 66 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 67 | ret = dma_get_sgtable(dev->dev, buf->sgt, buf->kvaddr, buf->dma_addr, |
| 68 | buf->size); |
| 69 | if (ret < 0) { |
| 70 | DRM_ERROR("failed to get sgtable.\n"); |
| 71 | goto err_free_sgt; |
| 72 | } |
| 73 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 74 | DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n", |
| 75 | (unsigned long)buf->kvaddr, |
| 76 | (unsigned long)buf->dma_addr, |
| 77 | buf->size); |
| 78 | |
| 79 | return ret; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 80 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 81 | err_free_sgt: |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 82 | kfree(buf->sgt); |
| 83 | buf->sgt = NULL; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 84 | err_free_attrs: |
| 85 | dma_free_attrs(dev->dev, buf->size, buf->kvaddr, |
| 86 | (dma_addr_t)buf->dma_addr, &buf->dma_attrs); |
| 87 | buf->dma_addr = (dma_addr_t)NULL; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 88 | |
| 89 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void lowlevel_buffer_deallocate(struct drm_device *dev, |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 93 | unsigned int flags, struct exynos_drm_gem_buf *buf) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 94 | { |
| 95 | DRM_DEBUG_KMS("%s.\n", __FILE__); |
| 96 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 97 | if (!buf->dma_addr) { |
| 98 | DRM_DEBUG_KMS("dma_addr is invalid.\n"); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | DRM_DEBUG_KMS("vaddr(0x%lx), dma_addr(0x%lx), size(0x%lx)\n", |
| 103 | (unsigned long)buf->kvaddr, |
| 104 | (unsigned long)buf->dma_addr, |
| 105 | buf->size); |
| 106 | |
| 107 | sg_free_table(buf->sgt); |
| 108 | |
| 109 | kfree(buf->sgt); |
| 110 | buf->sgt = NULL; |
| 111 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 112 | dma_free_attrs(dev->dev, buf->size, buf->kvaddr, |
| 113 | (dma_addr_t)buf->dma_addr, &buf->dma_attrs); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 114 | buf->dma_addr = (dma_addr_t)NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 117 | struct exynos_drm_gem_buf *exynos_drm_init_buf(struct drm_device *dev, |
| 118 | unsigned int size) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 119 | { |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 120 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 121 | |
| 122 | DRM_DEBUG_KMS("%s.\n", __FILE__); |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 123 | DRM_DEBUG_KMS("desired size = 0x%x\n", size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 124 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 125 | buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); |
| 126 | if (!buffer) { |
| 127 | DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n"); |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 128 | return NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 129 | } |
| 130 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 131 | buffer->size = size; |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 132 | return buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 135 | void exynos_drm_fini_buf(struct drm_device *dev, |
| 136 | struct exynos_drm_gem_buf *buffer) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 137 | { |
| 138 | DRM_DEBUG_KMS("%s.\n", __FILE__); |
| 139 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 140 | if (!buffer) { |
| 141 | DRM_DEBUG_KMS("buffer is null.\n"); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 145 | kfree(buffer); |
| 146 | buffer = NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 149 | int exynos_drm_alloc_buf(struct drm_device *dev, |
| 150 | struct exynos_drm_gem_buf *buf, unsigned int flags) |
| 151 | { |
| 152 | |
| 153 | /* |
| 154 | * allocate memory region and set the memory information |
| 155 | * to vaddr and dma_addr of a buffer object. |
| 156 | */ |
| 157 | if (lowlevel_buffer_allocate(dev, flags, buf) < 0) |
| 158 | return -ENOMEM; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | void exynos_drm_free_buf(struct drm_device *dev, |
| 164 | unsigned int flags, struct exynos_drm_gem_buf *buffer) |
| 165 | { |
| 166 | |
| 167 | lowlevel_buffer_deallocate(dev, flags, buffer); |
| 168 | } |