Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_gem.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Author: Inki Dae <inki.dae@samsung.com> |
| 5 | * |
Inki Dae | d81aecb | 2012-12-18 02:30:17 +0900 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 10 | */ |
| 11 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 12 | #include <drm/drmP.h> |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 13 | #include <drm/drm_vma_manager.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 14 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 15 | #include <linux/shmem_fs.h> |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 16 | #include <linux/dma-buf.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 17 | #include <drm/exynos_drm.h> |
| 18 | |
| 19 | #include "exynos_drm_drv.h" |
| 20 | #include "exynos_drm_gem.h" |
Vikas Sajjan | 3fec453 | 2013-08-23 12:05:06 +0530 | [diff] [blame] | 21 | #include "exynos_drm_iommu.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 22 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 23 | static int exynos_drm_alloc_buf(struct exynos_drm_gem_obj *obj) |
| 24 | { |
| 25 | struct drm_device *dev = obj->base.dev; |
| 26 | enum dma_attr attr; |
| 27 | unsigned int nr_pages; |
| 28 | |
| 29 | if (obj->dma_addr) { |
| 30 | DRM_DEBUG_KMS("already allocated.\n"); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | init_dma_attrs(&obj->dma_attrs); |
| 35 | |
| 36 | /* |
| 37 | * if EXYNOS_BO_CONTIG, fully physically contiguous memory |
| 38 | * region will be allocated else physically contiguous |
| 39 | * as possible. |
| 40 | */ |
| 41 | if (!(obj->flags & EXYNOS_BO_NONCONTIG)) |
| 42 | dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, &obj->dma_attrs); |
| 43 | |
| 44 | /* |
| 45 | * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping |
| 46 | * else cachable mapping. |
| 47 | */ |
| 48 | if (obj->flags & EXYNOS_BO_WC || !(obj->flags & EXYNOS_BO_CACHABLE)) |
| 49 | attr = DMA_ATTR_WRITE_COMBINE; |
| 50 | else |
| 51 | attr = DMA_ATTR_NON_CONSISTENT; |
| 52 | |
| 53 | dma_set_attr(attr, &obj->dma_attrs); |
| 54 | dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &obj->dma_attrs); |
| 55 | |
| 56 | nr_pages = obj->size >> PAGE_SHIFT; |
| 57 | |
| 58 | if (!is_drm_iommu_supported(dev)) { |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 59 | obj->pages = drm_calloc_large(nr_pages, sizeof(struct page *)); |
| 60 | if (!obj->pages) { |
| 61 | DRM_ERROR("failed to allocate pages.\n"); |
| 62 | return -ENOMEM; |
| 63 | } |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame^] | 64 | } |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 65 | |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame^] | 66 | obj->cookie = dma_alloc_attrs(dev->dev, obj->size, &obj->dma_addr, |
| 67 | GFP_KERNEL, &obj->dma_attrs); |
| 68 | if (!obj->cookie) { |
| 69 | DRM_ERROR("failed to allocate buffer.\n"); |
| 70 | if (obj->pages) |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 71 | drm_free_large(obj->pages); |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame^] | 72 | return -ENOMEM; |
| 73 | } |
| 74 | |
| 75 | if (obj->pages) { |
| 76 | dma_addr_t start_addr; |
| 77 | unsigned int i = 0; |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 78 | |
| 79 | start_addr = obj->dma_addr; |
| 80 | while (i < nr_pages) { |
Joonyoung Shim | 5e0fb1f | 2015-09-16 14:16:45 +0900 | [diff] [blame] | 81 | obj->pages[i] = pfn_to_page(dma_to_pfn(dev->dev, |
| 82 | start_addr)); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 83 | start_addr += PAGE_SIZE; |
| 84 | i++; |
| 85 | } |
| 86 | } else { |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame^] | 87 | obj->pages = obj->cookie; |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n", |
| 91 | (unsigned long)obj->dma_addr, |
| 92 | obj->size); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static void exynos_drm_free_buf(struct exynos_drm_gem_obj *obj) |
| 98 | { |
| 99 | struct drm_device *dev = obj->base.dev; |
| 100 | |
| 101 | if (!obj->dma_addr) { |
| 102 | DRM_DEBUG_KMS("dma_addr is invalid.\n"); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n", |
| 107 | (unsigned long)obj->dma_addr, obj->size); |
| 108 | |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame^] | 109 | dma_free_attrs(dev->dev, obj->size, obj->cookie, |
| 110 | (dma_addr_t)obj->dma_addr, &obj->dma_attrs); |
| 111 | |
| 112 | if (!is_drm_iommu_supported(dev)) |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 113 | drm_free_large(obj->pages); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 116 | static int exynos_drm_gem_handle_create(struct drm_gem_object *obj, |
| 117 | struct drm_file *file_priv, |
| 118 | unsigned int *handle) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 119 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 120 | int ret; |
| 121 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 122 | /* |
| 123 | * allocate a id of idr table where the obj is registered |
| 124 | * and handle has the id what user can see. |
| 125 | */ |
| 126 | ret = drm_gem_handle_create(file_priv, obj, handle); |
| 127 | if (ret) |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 128 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 129 | |
| 130 | DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle); |
| 131 | |
| 132 | /* drop reference from allocate - handle holds it now. */ |
| 133 | drm_gem_object_unreference_unlocked(obj); |
| 134 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 135 | return 0; |
| 136 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 137 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 138 | void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj) |
| 139 | { |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 140 | struct drm_gem_object *obj = &exynos_gem_obj->base; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 141 | |
Daniel Vetter | a8e11d1 | 2013-08-15 00:02:37 +0200 | [diff] [blame] | 142 | DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 143 | |
Inki Dae | c374e73 | 2012-06-12 16:52:54 +0900 | [diff] [blame] | 144 | /* |
| 145 | * do not release memory region from exporter. |
| 146 | * |
| 147 | * the region will be released by exporter |
| 148 | * once dmabuf's refcount becomes 0. |
| 149 | */ |
| 150 | if (obj->import_attach) |
Joonyoung Shim | 7c93537 | 2015-09-16 14:14:54 +0900 | [diff] [blame] | 151 | drm_prime_gem_destroy(obj, exynos_gem_obj->sgt); |
| 152 | else |
| 153 | exynos_drm_free_buf(exynos_gem_obj); |
Inki Dae | c374e73 | 2012-06-12 16:52:54 +0900 | [diff] [blame] | 154 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 155 | drm_gem_free_mmap_offset(obj); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 156 | |
| 157 | /* release file pointer to gem object. */ |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 158 | drm_gem_object_release(obj); |
| 159 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 160 | kfree(exynos_gem_obj); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 161 | } |
| 162 | |
Inki Dae | a4f19aa | 2013-03-11 21:15:59 +0900 | [diff] [blame] | 163 | unsigned long exynos_drm_gem_get_size(struct drm_device *dev, |
| 164 | unsigned int gem_handle, |
| 165 | struct drm_file *file_priv) |
| 166 | { |
| 167 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 168 | struct drm_gem_object *obj; |
| 169 | |
| 170 | obj = drm_gem_object_lookup(dev, file_priv, gem_handle); |
| 171 | if (!obj) { |
| 172 | DRM_ERROR("failed to lookup gem object.\n"); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 177 | |
| 178 | drm_gem_object_unreference_unlocked(obj); |
| 179 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 180 | return exynos_gem_obj->size; |
Inki Dae | a4f19aa | 2013-03-11 21:15:59 +0900 | [diff] [blame] | 181 | } |
| 182 | |
Joonyoung Shim | b319dc6 | 2015-09-16 14:29:33 +0900 | [diff] [blame] | 183 | static struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev, |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 184 | unsigned long size) |
| 185 | { |
| 186 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 187 | struct drm_gem_object *obj; |
| 188 | int ret; |
| 189 | |
| 190 | exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL); |
Sachin Kamat | 38bb525 | 2013-08-19 19:04:55 +0900 | [diff] [blame] | 191 | if (!exynos_gem_obj) |
Joonyoung Shim | 5f3f426 | 2015-07-28 17:53:22 +0900 | [diff] [blame] | 192 | return ERR_PTR(-ENOMEM); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 193 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 194 | exynos_gem_obj->size = size; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 195 | obj = &exynos_gem_obj->base; |
| 196 | |
| 197 | ret = drm_gem_object_init(dev, obj, size); |
| 198 | if (ret < 0) { |
| 199 | DRM_ERROR("failed to initialize gem object\n"); |
| 200 | kfree(exynos_gem_obj); |
Joonyoung Shim | 5f3f426 | 2015-07-28 17:53:22 +0900 | [diff] [blame] | 201 | return ERR_PTR(ret); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp); |
| 205 | |
| 206 | return exynos_gem_obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 207 | } |
| 208 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 209 | struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev, |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 210 | unsigned int flags, |
| 211 | unsigned long size) |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 212 | { |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 213 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 214 | int ret; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 215 | |
Joonyoung Shim | c4130bc | 2015-08-16 14:15:06 +0900 | [diff] [blame] | 216 | if (flags & ~(EXYNOS_BO_MASK)) { |
| 217 | DRM_ERROR("invalid flags.\n"); |
| 218 | return ERR_PTR(-EINVAL); |
| 219 | } |
| 220 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 221 | if (!size) { |
| 222 | DRM_ERROR("invalid size.\n"); |
| 223 | return ERR_PTR(-EINVAL); |
| 224 | } |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 225 | |
Joonyoung Shim | eb57da8 | 2015-07-28 17:53:27 +0900 | [diff] [blame] | 226 | size = roundup(size, PAGE_SIZE); |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 227 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 228 | exynos_gem_obj = exynos_drm_gem_init(dev, size); |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 229 | if (IS_ERR(exynos_gem_obj)) |
| 230 | return exynos_gem_obj; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 231 | |
| 232 | /* set memory type and cache attribute from user side. */ |
| 233 | exynos_gem_obj->flags = flags; |
| 234 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 235 | ret = exynos_drm_alloc_buf(exynos_gem_obj); |
| 236 | if (ret < 0) { |
| 237 | drm_gem_object_release(&exynos_gem_obj->base); |
| 238 | kfree(exynos_gem_obj); |
| 239 | return ERR_PTR(ret); |
| 240 | } |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 241 | |
| 242 | return exynos_gem_obj; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 243 | } |
| 244 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 245 | int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 246 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 247 | { |
| 248 | struct drm_exynos_gem_create *args = data; |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 249 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 250 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 251 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 252 | exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 253 | if (IS_ERR(exynos_gem_obj)) |
| 254 | return PTR_ERR(exynos_gem_obj); |
| 255 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 256 | ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv, |
| 257 | &args->handle); |
| 258 | if (ret) { |
| 259 | exynos_drm_gem_destroy(exynos_gem_obj); |
| 260 | return ret; |
| 261 | } |
| 262 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 266 | dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev, |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 267 | unsigned int gem_handle, |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 268 | struct drm_file *filp) |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 269 | { |
| 270 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 271 | struct drm_gem_object *obj; |
| 272 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 273 | obj = drm_gem_object_lookup(dev, filp, gem_handle); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 274 | if (!obj) { |
| 275 | DRM_ERROR("failed to lookup gem object.\n"); |
| 276 | return ERR_PTR(-EINVAL); |
| 277 | } |
| 278 | |
| 279 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 280 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 281 | return &exynos_gem_obj->dma_addr; |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void exynos_drm_gem_put_dma_addr(struct drm_device *dev, |
| 285 | unsigned int gem_handle, |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 286 | struct drm_file *filp) |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 287 | { |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 288 | struct drm_gem_object *obj; |
| 289 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 290 | obj = drm_gem_object_lookup(dev, filp, gem_handle); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 291 | if (!obj) { |
| 292 | DRM_ERROR("failed to lookup gem object.\n"); |
| 293 | return; |
| 294 | } |
| 295 | |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 296 | drm_gem_object_unreference_unlocked(obj); |
| 297 | |
| 298 | /* |
| 299 | * decrease obj->refcount one more time because we has already |
| 300 | * increased it at exynos_drm_gem_get_dma_addr(). |
| 301 | */ |
| 302 | drm_gem_object_unreference_unlocked(obj); |
| 303 | } |
| 304 | |
Inki Dae | 832316c | 2014-09-18 14:19:01 +0900 | [diff] [blame] | 305 | int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem_obj *exynos_gem_obj, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 306 | struct vm_area_struct *vma) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 307 | { |
Inki Dae | 832316c | 2014-09-18 14:19:01 +0900 | [diff] [blame] | 308 | struct drm_device *drm_dev = exynos_gem_obj->base.dev; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 309 | unsigned long vm_size; |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 310 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 311 | |
Inki Dae | 832316c | 2014-09-18 14:19:01 +0900 | [diff] [blame] | 312 | vma->vm_flags &= ~VM_PFNMAP; |
| 313 | vma->vm_pgoff = 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 314 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 315 | vm_size = vma->vm_end - vma->vm_start; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 316 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 317 | /* check if user-requested size is valid. */ |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 318 | if (vm_size > exynos_gem_obj->size) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 319 | return -EINVAL; |
| 320 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 321 | ret = dma_mmap_attrs(drm_dev->dev, vma, exynos_gem_obj->pages, |
| 322 | exynos_gem_obj->dma_addr, exynos_gem_obj->size, |
| 323 | &exynos_gem_obj->dma_attrs); |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 324 | if (ret < 0) { |
| 325 | DRM_ERROR("failed to mmap.\n"); |
| 326 | return ret; |
| 327 | } |
| 328 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Inki Dae | 40cd7e0 | 2012-05-04 15:51:17 +0900 | [diff] [blame] | 332 | int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data, |
| 333 | struct drm_file *file_priv) |
| 334 | { struct exynos_drm_gem_obj *exynos_gem_obj; |
| 335 | struct drm_exynos_gem_info *args = data; |
| 336 | struct drm_gem_object *obj; |
| 337 | |
| 338 | mutex_lock(&dev->struct_mutex); |
| 339 | |
| 340 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 341 | if (!obj) { |
| 342 | DRM_ERROR("failed to lookup gem object.\n"); |
| 343 | mutex_unlock(&dev->struct_mutex); |
| 344 | return -EINVAL; |
| 345 | } |
| 346 | |
| 347 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 348 | |
| 349 | args->flags = exynos_gem_obj->flags; |
| 350 | args->size = exynos_gem_obj->size; |
| 351 | |
| 352 | drm_gem_object_unreference(obj); |
| 353 | mutex_unlock(&dev->struct_mutex); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 358 | int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev, |
| 359 | struct sg_table *sgt, |
| 360 | enum dma_data_direction dir) |
| 361 | { |
| 362 | int nents; |
| 363 | |
| 364 | mutex_lock(&drm_dev->struct_mutex); |
| 365 | |
| 366 | nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir); |
| 367 | if (!nents) { |
| 368 | DRM_ERROR("failed to map sgl with dma.\n"); |
| 369 | mutex_unlock(&drm_dev->struct_mutex); |
| 370 | return nents; |
| 371 | } |
| 372 | |
| 373 | mutex_unlock(&drm_dev->struct_mutex); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev, |
| 378 | struct sg_table *sgt, |
| 379 | enum dma_data_direction dir) |
| 380 | { |
| 381 | dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir); |
| 382 | } |
| 383 | |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 384 | void exynos_drm_gem_free_object(struct drm_gem_object *obj) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 385 | { |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 386 | exynos_drm_gem_destroy(to_exynos_gem_obj(obj)); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | int exynos_drm_gem_dumb_create(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 390 | struct drm_device *dev, |
| 391 | struct drm_mode_create_dumb *args) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 392 | { |
| 393 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame^] | 394 | unsigned int flags; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 395 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 396 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 397 | /* |
Masanari Iida | c6b78bc | 2013-10-24 16:02:57 +0900 | [diff] [blame] | 398 | * allocate memory to be used for framebuffer. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 399 | * - this callback would be called by user application |
| 400 | * with DRM_IOCTL_MODE_CREATE_DUMB command. |
| 401 | */ |
| 402 | |
Cooper Yuan | 3fd6b69 | 2012-06-29 11:49:45 +0900 | [diff] [blame] | 403 | args->pitch = args->width * ((args->bpp + 7) / 8); |
Inki Dae | 7da5907 | 2012-08-17 15:24:03 +0900 | [diff] [blame] | 404 | args->size = args->pitch * args->height; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 405 | |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame^] | 406 | if (is_drm_iommu_supported(dev)) |
| 407 | flags = EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC; |
| 408 | else |
| 409 | flags = EXYNOS_BO_CONTIG | EXYNOS_BO_WC; |
Vikas Sajjan | 3fec453 | 2013-08-23 12:05:06 +0530 | [diff] [blame] | 410 | |
Joonyoung Shim | 333e8e5 | 2015-09-16 14:29:34 +0900 | [diff] [blame^] | 411 | exynos_gem_obj = exynos_drm_gem_create(dev, flags, args->size); |
Rahul Sharma | 122beea | 2014-05-07 17:21:29 +0530 | [diff] [blame] | 412 | if (IS_ERR(exynos_gem_obj)) { |
| 413 | dev_warn(dev->dev, "FB allocation failed.\n"); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 414 | return PTR_ERR(exynos_gem_obj); |
Rahul Sharma | 122beea | 2014-05-07 17:21:29 +0530 | [diff] [blame] | 415 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 416 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 417 | ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv, |
| 418 | &args->handle); |
| 419 | if (ret) { |
| 420 | exynos_drm_gem_destroy(exynos_gem_obj); |
| 421 | return ret; |
| 422 | } |
| 423 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 428 | struct drm_device *dev, uint32_t handle, |
| 429 | uint64_t *offset) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 430 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 431 | struct drm_gem_object *obj; |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 432 | int ret = 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 433 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 434 | mutex_lock(&dev->struct_mutex); |
| 435 | |
| 436 | /* |
| 437 | * get offset of memory allocated for drm framebuffer. |
| 438 | * - this callback would be called by user application |
| 439 | * with DRM_IOCTL_MODE_MAP_DUMB command. |
| 440 | */ |
| 441 | |
| 442 | obj = drm_gem_object_lookup(dev, file_priv, handle); |
| 443 | if (!obj) { |
| 444 | DRM_ERROR("failed to lookup gem object.\n"); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 445 | ret = -EINVAL; |
| 446 | goto unlock; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 447 | } |
| 448 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 449 | ret = drm_gem_create_mmap_offset(obj); |
| 450 | if (ret) |
| 451 | goto out; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 452 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 453 | *offset = drm_vma_node_offset_addr(&obj->vma_node); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 454 | DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset); |
| 455 | |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 456 | out: |
| 457 | drm_gem_object_unreference(obj); |
| 458 | unlock: |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 459 | mutex_unlock(&dev->struct_mutex); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 460 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 464 | { |
| 465 | struct drm_gem_object *obj = vma->vm_private_data; |
Joonyoung Shim | 0e9a2ee | 2015-07-28 17:53:19 +0900 | [diff] [blame] | 466 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
Joonyoung Shim | 0e9a2ee | 2015-07-28 17:53:19 +0900 | [diff] [blame] | 467 | unsigned long pfn; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 468 | pgoff_t page_offset; |
| 469 | int ret; |
| 470 | |
| 471 | page_offset = ((unsigned long)vmf->virtual_address - |
| 472 | vma->vm_start) >> PAGE_SHIFT; |
| 473 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 474 | if (page_offset >= (exynos_gem_obj->size >> PAGE_SHIFT)) { |
Joonyoung Shim | 0e9a2ee | 2015-07-28 17:53:19 +0900 | [diff] [blame] | 475 | DRM_ERROR("invalid page offset\n"); |
| 476 | ret = -EINVAL; |
| 477 | goto out; |
| 478 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 479 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 480 | pfn = page_to_pfn(exynos_gem_obj->pages[page_offset]); |
Joonyoung Shim | 0e9a2ee | 2015-07-28 17:53:19 +0900 | [diff] [blame] | 481 | ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn); |
| 482 | |
| 483 | out: |
Joonyoung Shim | 23597e2 | 2015-07-28 17:53:17 +0900 | [diff] [blame] | 484 | switch (ret) { |
| 485 | case 0: |
| 486 | case -ERESTARTSYS: |
| 487 | case -EINTR: |
| 488 | return VM_FAULT_NOPAGE; |
| 489 | case -ENOMEM: |
| 490 | return VM_FAULT_OOM; |
| 491 | default: |
| 492 | return VM_FAULT_SIGBUS; |
| 493 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 497 | { |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 498 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 499 | struct drm_gem_object *obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 500 | int ret; |
| 501 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 502 | /* set vm_area_struct. */ |
| 503 | ret = drm_gem_mmap(filp, vma); |
| 504 | if (ret < 0) { |
| 505 | DRM_ERROR("failed to mmap.\n"); |
| 506 | return ret; |
| 507 | } |
| 508 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 509 | obj = vma->vm_private_data; |
| 510 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 511 | |
Joonyoung Shim | 211b887 | 2015-08-16 14:16:49 +0900 | [diff] [blame] | 512 | DRM_DEBUG_KMS("flags = 0x%x\n", exynos_gem_obj->flags); |
| 513 | |
| 514 | /* non-cachable as default. */ |
| 515 | if (exynos_gem_obj->flags & EXYNOS_BO_CACHABLE) |
| 516 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 517 | else if (exynos_gem_obj->flags & EXYNOS_BO_WC) |
| 518 | vma->vm_page_prot = |
| 519 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 520 | else |
| 521 | vma->vm_page_prot = |
| 522 | pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 523 | |
Inki Dae | 832316c | 2014-09-18 14:19:01 +0900 | [diff] [blame] | 524 | ret = exynos_drm_gem_mmap_buffer(exynos_gem_obj, vma); |
| 525 | if (ret) |
| 526 | goto err_close_vm; |
| 527 | |
| 528 | return ret; |
| 529 | |
| 530 | err_close_vm: |
| 531 | drm_gem_vm_close(vma); |
| 532 | drm_gem_free_mmap_offset(obj); |
| 533 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 534 | return ret; |
| 535 | } |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 536 | |
| 537 | /* low-level interface prime helpers */ |
| 538 | struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj) |
| 539 | { |
| 540 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 541 | int npages; |
| 542 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 543 | npages = exynos_gem_obj->size >> PAGE_SHIFT; |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 544 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 545 | return drm_prime_pages_to_sg(exynos_gem_obj->pages, npages); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | struct drm_gem_object * |
| 549 | exynos_drm_gem_prime_import_sg_table(struct drm_device *dev, |
| 550 | struct dma_buf_attachment *attach, |
| 551 | struct sg_table *sgt) |
| 552 | { |
| 553 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 554 | int npages; |
| 555 | int ret; |
| 556 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 557 | exynos_gem_obj = exynos_drm_gem_init(dev, attach->dmabuf->size); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 558 | if (IS_ERR(exynos_gem_obj)) { |
| 559 | ret = PTR_ERR(exynos_gem_obj); |
Inki Dae | 50002d4 | 2015-08-31 01:11:53 +0900 | [diff] [blame] | 560 | return ERR_PTR(ret); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 561 | } |
| 562 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 563 | exynos_gem_obj->dma_addr = sg_dma_address(sgt->sgl); |
| 564 | |
| 565 | npages = exynos_gem_obj->size >> PAGE_SHIFT; |
| 566 | exynos_gem_obj->pages = drm_malloc_ab(npages, sizeof(struct page *)); |
| 567 | if (!exynos_gem_obj->pages) { |
| 568 | ret = -ENOMEM; |
| 569 | goto err; |
| 570 | } |
| 571 | |
| 572 | ret = drm_prime_sg_to_page_addr_arrays(sgt, exynos_gem_obj->pages, NULL, |
| 573 | npages); |
| 574 | if (ret < 0) |
| 575 | goto err_free_large; |
| 576 | |
Joonyoung Shim | 7c93537 | 2015-09-16 14:14:54 +0900 | [diff] [blame] | 577 | exynos_gem_obj->sgt = sgt; |
| 578 | |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 579 | if (sgt->nents == 1) { |
| 580 | /* always physically continuous memory if sgt->nents is 1. */ |
| 581 | exynos_gem_obj->flags |= EXYNOS_BO_CONTIG; |
| 582 | } else { |
| 583 | /* |
| 584 | * this case could be CONTIG or NONCONTIG type but for now |
| 585 | * sets NONCONTIG. |
| 586 | * TODO. we have to find a way that exporter can notify |
| 587 | * the type of its own buffer to importer. |
| 588 | */ |
| 589 | exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG; |
| 590 | } |
| 591 | |
| 592 | return &exynos_gem_obj->base; |
| 593 | |
| 594 | err_free_large: |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 595 | drm_free_large(exynos_gem_obj->pages); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 596 | err: |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 597 | drm_gem_object_release(&exynos_gem_obj->base); |
| 598 | kfree(exynos_gem_obj); |
Joonyoung Shim | 01ed50d | 2015-08-16 14:33:08 +0900 | [diff] [blame] | 599 | return ERR_PTR(ret); |
| 600 | } |
| 601 | |
| 602 | void *exynos_drm_gem_prime_vmap(struct drm_gem_object *obj) |
| 603 | { |
| 604 | return NULL; |
| 605 | } |
| 606 | |
| 607 | void exynos_drm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) |
| 608 | { |
| 609 | /* Nothing to do */ |
| 610 | } |