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