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