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