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