Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 1 | /* exynos_drm_dmabuf.c |
| 2 | * |
| 3 | * Copyright (c) 2012 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 | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 28 | #include "exynos_drm_drv.h" |
| 29 | #include "exynos_drm_gem.h" |
| 30 | |
| 31 | #include <linux/dma-buf.h> |
| 32 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 33 | static struct sg_table *exynos_get_sgt(struct drm_device *drm_dev, |
| 34 | struct exynos_drm_gem_buf *buf) |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 35 | { |
| 36 | struct sg_table *sgt = NULL; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 37 | int ret; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 38 | |
| 39 | sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); |
| 40 | if (!sgt) |
| 41 | goto out; |
| 42 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 43 | ret = sg_alloc_table(sgt, buf->sgt->nents, GFP_KERNEL); |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 44 | if (ret) |
| 45 | goto err_free_sgt; |
| 46 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 47 | ret = dma_get_sgtable(drm_dev->dev, sgt, buf->kvaddr, |
| 48 | buf->dma_addr, buf->size); |
| 49 | if (ret < 0) { |
| 50 | DRM_ERROR("failed to get sgtable.\n"); |
| 51 | goto err_free_table; |
| 52 | } |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 53 | |
| 54 | return sgt; |
| 55 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 56 | err_free_table: |
| 57 | sg_free_table(sgt); |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 58 | err_free_sgt: |
| 59 | kfree(sgt); |
| 60 | sgt = NULL; |
| 61 | out: |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | static struct sg_table * |
| 66 | exynos_gem_map_dma_buf(struct dma_buf_attachment *attach, |
| 67 | enum dma_data_direction dir) |
| 68 | { |
| 69 | struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv; |
| 70 | struct drm_device *dev = gem_obj->base.dev; |
| 71 | struct exynos_drm_gem_buf *buf; |
| 72 | struct sg_table *sgt = NULL; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 73 | int nents; |
| 74 | |
| 75 | DRM_DEBUG_PRIME("%s\n", __FILE__); |
| 76 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 77 | buf = gem_obj->buffer; |
| 78 | if (!buf) { |
| 79 | DRM_ERROR("buffer is null.\n"); |
| 80 | return sgt; |
| 81 | } |
| 82 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 83 | mutex_lock(&dev->struct_mutex); |
| 84 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 85 | sgt = exynos_get_sgt(dev, buf); |
| 86 | if (!sgt) |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 87 | goto err_unlock; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 88 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 89 | nents = dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 90 | if (!nents) { |
| 91 | DRM_ERROR("failed to map sgl with iommu.\n"); |
| 92 | sgt = NULL; |
| 93 | goto err_unlock; |
| 94 | } |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 95 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 96 | DRM_DEBUG_PRIME("buffer size = 0x%lx page_size = 0x%lx\n", |
| 97 | buf->size, buf->page_size); |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 98 | |
| 99 | err_unlock: |
| 100 | mutex_unlock(&dev->struct_mutex); |
| 101 | return sgt; |
| 102 | } |
| 103 | |
| 104 | static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach, |
| 105 | struct sg_table *sgt, |
| 106 | enum dma_data_direction dir) |
| 107 | { |
| 108 | dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir); |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 109 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 110 | sg_free_table(sgt); |
| 111 | kfree(sgt); |
| 112 | sgt = NULL; |
| 113 | } |
| 114 | |
| 115 | static void exynos_dmabuf_release(struct dma_buf *dmabuf) |
| 116 | { |
| 117 | struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv; |
| 118 | |
| 119 | DRM_DEBUG_PRIME("%s\n", __FILE__); |
| 120 | |
| 121 | /* |
| 122 | * exynos_dmabuf_release() call means that file object's |
| 123 | * f_count is 0 and it calls drm_gem_object_handle_unreference() |
| 124 | * to drop the references that these values had been increased |
| 125 | * at drm_prime_handle_to_fd() |
| 126 | */ |
| 127 | if (exynos_gem_obj->base.export_dma_buf == dmabuf) { |
| 128 | exynos_gem_obj->base.export_dma_buf = NULL; |
| 129 | |
| 130 | /* |
| 131 | * drop this gem object refcount to release allocated buffer |
| 132 | * and resources. |
| 133 | */ |
| 134 | drm_gem_object_unreference_unlocked(&exynos_gem_obj->base); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, |
| 139 | unsigned long page_num) |
| 140 | { |
| 141 | /* TODO */ |
| 142 | |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, |
| 147 | unsigned long page_num, |
| 148 | void *addr) |
| 149 | { |
| 150 | /* TODO */ |
| 151 | } |
| 152 | |
| 153 | static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf, |
| 154 | unsigned long page_num) |
| 155 | { |
| 156 | /* TODO */ |
| 157 | |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf, |
| 162 | unsigned long page_num, void *addr) |
| 163 | { |
| 164 | /* TODO */ |
| 165 | } |
| 166 | |
Tomasz Stanislawski | b716d46 | 2012-09-05 19:31:56 +0900 | [diff] [blame] | 167 | static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf, |
| 168 | struct vm_area_struct *vma) |
| 169 | { |
| 170 | return -ENOTTY; |
| 171 | } |
| 172 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 173 | static struct dma_buf_ops exynos_dmabuf_ops = { |
| 174 | .map_dma_buf = exynos_gem_map_dma_buf, |
| 175 | .unmap_dma_buf = exynos_gem_unmap_dma_buf, |
| 176 | .kmap = exynos_gem_dmabuf_kmap, |
| 177 | .kmap_atomic = exynos_gem_dmabuf_kmap_atomic, |
| 178 | .kunmap = exynos_gem_dmabuf_kunmap, |
| 179 | .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic, |
Tomasz Stanislawski | b716d46 | 2012-09-05 19:31:56 +0900 | [diff] [blame] | 180 | .mmap = exynos_gem_dmabuf_mmap, |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 181 | .release = exynos_dmabuf_release, |
| 182 | }; |
| 183 | |
| 184 | struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev, |
| 185 | struct drm_gem_object *obj, int flags) |
| 186 | { |
| 187 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
| 188 | |
| 189 | return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops, |
| 190 | exynos_gem_obj->base.size, 0600); |
| 191 | } |
| 192 | |
| 193 | struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev, |
| 194 | struct dma_buf *dma_buf) |
| 195 | { |
| 196 | struct dma_buf_attachment *attach; |
| 197 | struct sg_table *sgt; |
| 198 | struct scatterlist *sgl; |
| 199 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 200 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | 47fcdce | 2012-06-07 16:15:07 +0900 | [diff] [blame] | 201 | int ret; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 202 | |
| 203 | DRM_DEBUG_PRIME("%s\n", __FILE__); |
| 204 | |
| 205 | /* is this one of own objects? */ |
| 206 | if (dma_buf->ops == &exynos_dmabuf_ops) { |
| 207 | struct drm_gem_object *obj; |
| 208 | |
| 209 | exynos_gem_obj = dma_buf->priv; |
| 210 | obj = &exynos_gem_obj->base; |
| 211 | |
| 212 | /* is it from our device? */ |
| 213 | if (obj->dev == drm_dev) { |
| 214 | drm_gem_object_reference(obj); |
| 215 | return obj; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | attach = dma_buf_attach(dma_buf, drm_dev->dev); |
| 220 | if (IS_ERR(attach)) |
| 221 | return ERR_PTR(-EINVAL); |
| 222 | |
| 223 | |
| 224 | sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); |
Subash Patel | 0dd3b72 | 2012-06-25 11:22:57 -0700 | [diff] [blame] | 225 | if (IS_ERR_OR_NULL(sgt)) { |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 226 | ret = PTR_ERR(sgt); |
| 227 | goto err_buf_detach; |
| 228 | } |
| 229 | |
| 230 | buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); |
| 231 | if (!buffer) { |
| 232 | DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n"); |
| 233 | ret = -ENOMEM; |
| 234 | goto err_unmap_attach; |
| 235 | } |
| 236 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 237 | exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size); |
| 238 | if (!exynos_gem_obj) { |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 239 | ret = -ENOMEM; |
| 240 | goto err_free_buffer; |
| 241 | } |
| 242 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 243 | sgl = sgt->sgl; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 244 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 245 | buffer->size = dma_buf->size; |
| 246 | buffer->dma_addr = sg_dma_address(sgl); |
Inki Dae | 47fcdce | 2012-06-07 16:15:07 +0900 | [diff] [blame] | 247 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 248 | if (sgt->nents == 1) { |
Inki Dae | 47fcdce | 2012-06-07 16:15:07 +0900 | [diff] [blame] | 249 | /* always physically continuous memory if sgt->nents is 1. */ |
| 250 | exynos_gem_obj->flags |= EXYNOS_BO_CONTIG; |
| 251 | } else { |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame^] | 252 | /* |
| 253 | * this case could be CONTIG or NONCONTIG type but for now |
| 254 | * sets NONCONTIG. |
| 255 | * TODO. we have to find a way that exporter can notify |
| 256 | * the type of its own buffer to importer. |
| 257 | */ |
Inki Dae | 47fcdce | 2012-06-07 16:15:07 +0900 | [diff] [blame] | 258 | exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | exynos_gem_obj->buffer = buffer; |
| 262 | buffer->sgt = sgt; |
| 263 | exynos_gem_obj->base.import_attach = attach; |
| 264 | |
| 265 | DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr, |
| 266 | buffer->size); |
| 267 | |
| 268 | return &exynos_gem_obj->base; |
| 269 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 270 | err_free_buffer: |
| 271 | kfree(buffer); |
| 272 | buffer = NULL; |
| 273 | err_unmap_attach: |
| 274 | dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); |
| 275 | err_buf_detach: |
| 276 | dma_buf_detach(dma_buf, attach); |
| 277 | return ERR_PTR(ret); |
| 278 | } |
| 279 | |
| 280 | MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>"); |
| 281 | MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module"); |
| 282 | MODULE_LICENSE("GPL"); |