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