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" |
| 21 | |
| 22 | static unsigned int convert_to_vm_err_msg(int msg) |
| 23 | { |
| 24 | unsigned int out_msg; |
| 25 | |
| 26 | switch (msg) { |
| 27 | case 0: |
| 28 | case -ERESTARTSYS: |
| 29 | case -EINTR: |
| 30 | out_msg = VM_FAULT_NOPAGE; |
| 31 | break; |
| 32 | |
| 33 | case -ENOMEM: |
| 34 | out_msg = VM_FAULT_OOM; |
| 35 | break; |
| 36 | |
| 37 | default: |
| 38 | out_msg = VM_FAULT_SIGBUS; |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | return out_msg; |
| 43 | } |
| 44 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 45 | static int check_gem_flags(unsigned int flags) |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 46 | { |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 47 | if (flags & ~(EXYNOS_BO_MASK)) { |
| 48 | DRM_ERROR("invalid flags.\n"); |
| 49 | return -EINVAL; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 55 | static void update_vm_cache_attr(struct exynos_drm_gem_obj *obj, |
| 56 | struct vm_area_struct *vma) |
| 57 | { |
| 58 | DRM_DEBUG_KMS("flags = 0x%x\n", obj->flags); |
| 59 | |
| 60 | /* non-cachable as default. */ |
| 61 | if (obj->flags & EXYNOS_BO_CACHABLE) |
| 62 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 63 | else if (obj->flags & EXYNOS_BO_WC) |
| 64 | vma->vm_page_prot = |
| 65 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 66 | else |
| 67 | vma->vm_page_prot = |
| 68 | pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 69 | } |
| 70 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 71 | static unsigned long roundup_gem_size(unsigned long size, unsigned int flags) |
| 72 | { |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 73 | /* TODO */ |
| 74 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 75 | return roundup(size, PAGE_SIZE); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 76 | } |
| 77 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 78 | static int exynos_drm_gem_map_buf(struct drm_gem_object *obj, |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 79 | struct vm_area_struct *vma, |
| 80 | unsigned long f_vaddr, |
| 81 | pgoff_t page_offset) |
| 82 | { |
| 83 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
| 84 | struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer; |
Rahul Sharma | 4ddc404 | 2012-11-05 21:04:29 +0530 | [diff] [blame] | 85 | struct scatterlist *sgl; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 86 | unsigned long pfn; |
Rahul Sharma | 4ddc404 | 2012-11-05 21:04:29 +0530 | [diff] [blame] | 87 | int i; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 88 | |
Prathyush K | 465ed66 | 2012-11-20 19:32:56 +0900 | [diff] [blame] | 89 | if (!buf->sgt) |
| 90 | return -EINTR; |
Rahul Sharma | 4ddc404 | 2012-11-05 21:04:29 +0530 | [diff] [blame] | 91 | |
Prathyush K | 465ed66 | 2012-11-20 19:32:56 +0900 | [diff] [blame] | 92 | if (page_offset >= (buf->size >> PAGE_SHIFT)) { |
| 93 | DRM_ERROR("invalid page offset\n"); |
| 94 | return -EINVAL; |
Rahul Sharma | 4ddc404 | 2012-11-05 21:04:29 +0530 | [diff] [blame] | 95 | } |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 96 | |
Prathyush K | 465ed66 | 2012-11-20 19:32:56 +0900 | [diff] [blame] | 97 | sgl = buf->sgt->sgl; |
| 98 | for_each_sg(buf->sgt->sgl, sgl, buf->sgt->nents, i) { |
| 99 | if (page_offset < (sgl->length >> PAGE_SHIFT)) |
| 100 | break; |
| 101 | page_offset -= (sgl->length >> PAGE_SHIFT); |
| 102 | } |
| 103 | |
| 104 | pfn = __phys_to_pfn(sg_phys(sgl)) + page_offset; |
| 105 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 106 | return vm_insert_mixed(vma, f_vaddr, pfn); |
| 107 | } |
| 108 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 109 | static int exynos_drm_gem_handle_create(struct drm_gem_object *obj, |
| 110 | struct drm_file *file_priv, |
| 111 | unsigned int *handle) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 112 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 113 | int ret; |
| 114 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 115 | /* |
| 116 | * allocate a id of idr table where the obj is registered |
| 117 | * and handle has the id what user can see. |
| 118 | */ |
| 119 | ret = drm_gem_handle_create(file_priv, obj, handle); |
| 120 | if (ret) |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 121 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 122 | |
| 123 | DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle); |
| 124 | |
| 125 | /* drop reference from allocate - handle holds it now. */ |
| 126 | drm_gem_object_unreference_unlocked(obj); |
| 127 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 128 | return 0; |
| 129 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 130 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 131 | void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj) |
| 132 | { |
| 133 | struct drm_gem_object *obj; |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 134 | struct exynos_drm_gem_buf *buf; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 135 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 136 | obj = &exynos_gem_obj->base; |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 137 | buf = exynos_gem_obj->buffer; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 138 | |
| 139 | DRM_DEBUG_KMS("handle count = %d\n", atomic_read(&obj->handle_count)); |
| 140 | |
Inki Dae | c374e73 | 2012-06-12 16:52:54 +0900 | [diff] [blame] | 141 | /* |
| 142 | * do not release memory region from exporter. |
| 143 | * |
| 144 | * the region will be released by exporter |
| 145 | * once dmabuf's refcount becomes 0. |
| 146 | */ |
| 147 | if (obj->import_attach) |
| 148 | goto out; |
| 149 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 150 | exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags, buf); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 151 | |
Inki Dae | c374e73 | 2012-06-12 16:52:54 +0900 | [diff] [blame] | 152 | out: |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 153 | exynos_drm_fini_buf(obj->dev, buf); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 154 | exynos_gem_obj->buffer = NULL; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 155 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 156 | drm_gem_free_mmap_offset(obj); |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 157 | |
| 158 | /* release file pointer to gem object. */ |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 159 | drm_gem_object_release(obj); |
| 160 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 161 | kfree(exynos_gem_obj); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 162 | exynos_gem_obj = NULL; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 163 | } |
| 164 | |
Inki Dae | a4f19aa | 2013-03-11 21:15:59 +0900 | [diff] [blame] | 165 | unsigned long exynos_drm_gem_get_size(struct drm_device *dev, |
| 166 | unsigned int gem_handle, |
| 167 | struct drm_file *file_priv) |
| 168 | { |
| 169 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 170 | struct drm_gem_object *obj; |
| 171 | |
| 172 | obj = drm_gem_object_lookup(dev, file_priv, gem_handle); |
| 173 | if (!obj) { |
| 174 | DRM_ERROR("failed to lookup gem object.\n"); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 179 | |
| 180 | drm_gem_object_unreference_unlocked(obj); |
| 181 | |
| 182 | return exynos_gem_obj->buffer->size; |
| 183 | } |
| 184 | |
| 185 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 186 | 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] | 187 | unsigned long size) |
| 188 | { |
| 189 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 190 | struct drm_gem_object *obj; |
| 191 | int ret; |
| 192 | |
| 193 | exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL); |
| 194 | if (!exynos_gem_obj) { |
| 195 | DRM_ERROR("failed to allocate exynos gem object\n"); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 199 | exynos_gem_obj->size = size; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 200 | obj = &exynos_gem_obj->base; |
| 201 | |
| 202 | ret = drm_gem_object_init(dev, obj, size); |
| 203 | if (ret < 0) { |
| 204 | DRM_ERROR("failed to initialize gem object\n"); |
| 205 | kfree(exynos_gem_obj); |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp); |
| 210 | |
| 211 | return exynos_gem_obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 212 | } |
| 213 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 214 | 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] | 215 | unsigned int flags, |
| 216 | unsigned long size) |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 217 | { |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 218 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 219 | struct exynos_drm_gem_buf *buf; |
| 220 | int ret; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 221 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 222 | if (!size) { |
| 223 | DRM_ERROR("invalid size.\n"); |
| 224 | return ERR_PTR(-EINVAL); |
| 225 | } |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 226 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 227 | size = roundup_gem_size(size, flags); |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 228 | |
| 229 | ret = check_gem_flags(flags); |
| 230 | if (ret) |
| 231 | return ERR_PTR(ret); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 232 | |
| 233 | buf = exynos_drm_init_buf(dev, size); |
| 234 | if (!buf) |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 235 | return ERR_PTR(-ENOMEM); |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 236 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 237 | exynos_gem_obj = exynos_drm_gem_init(dev, size); |
| 238 | if (!exynos_gem_obj) { |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 239 | ret = -ENOMEM; |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 240 | goto err_fini_buf; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 241 | } |
| 242 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 243 | exynos_gem_obj->buffer = buf; |
| 244 | |
| 245 | /* set memory type and cache attribute from user side. */ |
| 246 | exynos_gem_obj->flags = flags; |
| 247 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 248 | ret = exynos_drm_alloc_buf(dev, buf, flags); |
YoungJun Cho | c58c159 | 2013-07-01 11:17:12 +0900 | [diff] [blame] | 249 | if (ret < 0) |
| 250 | goto err_gem_fini; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 251 | |
| 252 | return exynos_gem_obj; |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 253 | |
YoungJun Cho | c58c159 | 2013-07-01 11:17:12 +0900 | [diff] [blame] | 254 | err_gem_fini: |
| 255 | drm_gem_object_release(&exynos_gem_obj->base); |
| 256 | kfree(exynos_gem_obj); |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 257 | err_fini_buf: |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 258 | exynos_drm_fini_buf(dev, buf); |
| 259 | return ERR_PTR(ret); |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 260 | } |
| 261 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 262 | int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 263 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 264 | { |
| 265 | struct drm_exynos_gem_create *args = data; |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 266 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 267 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 268 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 269 | exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 270 | if (IS_ERR(exynos_gem_obj)) |
| 271 | return PTR_ERR(exynos_gem_obj); |
| 272 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 273 | ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv, |
| 274 | &args->handle); |
| 275 | if (ret) { |
| 276 | exynos_drm_gem_destroy(exynos_gem_obj); |
| 277 | return ret; |
| 278 | } |
| 279 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 283 | 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] | 284 | unsigned int gem_handle, |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 285 | struct drm_file *filp) |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 286 | { |
| 287 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 288 | struct drm_gem_object *obj; |
| 289 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 290 | obj = drm_gem_object_lookup(dev, filp, gem_handle); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 291 | if (!obj) { |
| 292 | DRM_ERROR("failed to lookup gem object.\n"); |
| 293 | return ERR_PTR(-EINVAL); |
| 294 | } |
| 295 | |
| 296 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 297 | |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 298 | return &exynos_gem_obj->buffer->dma_addr; |
| 299 | } |
| 300 | |
| 301 | void exynos_drm_gem_put_dma_addr(struct drm_device *dev, |
| 302 | unsigned int gem_handle, |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 303 | struct drm_file *filp) |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 304 | { |
| 305 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 306 | struct drm_gem_object *obj; |
| 307 | |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 308 | obj = drm_gem_object_lookup(dev, filp, gem_handle); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 309 | if (!obj) { |
| 310 | DRM_ERROR("failed to lookup gem object.\n"); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 315 | |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 316 | drm_gem_object_unreference_unlocked(obj); |
| 317 | |
| 318 | /* |
| 319 | * decrease obj->refcount one more time because we has already |
| 320 | * increased it at exynos_drm_gem_get_dma_addr(). |
| 321 | */ |
| 322 | drm_gem_object_unreference_unlocked(obj); |
| 323 | } |
| 324 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 325 | int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 326 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 327 | { |
| 328 | struct drm_exynos_gem_map_off *args = data; |
| 329 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 330 | DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n", |
| 331 | args->handle, (unsigned long)args->offset); |
| 332 | |
| 333 | if (!(dev->driver->driver_features & DRIVER_GEM)) { |
| 334 | DRM_ERROR("does not support GEM.\n"); |
| 335 | return -ENODEV; |
| 336 | } |
| 337 | |
| 338 | return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle, |
| 339 | &args->offset); |
| 340 | } |
| 341 | |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 342 | static struct drm_file *exynos_drm_find_drm_file(struct drm_device *drm_dev, |
| 343 | struct file *filp) |
| 344 | { |
| 345 | struct drm_file *file_priv; |
| 346 | |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 347 | /* find current process's drm_file from filelist. */ |
YoungJun Cho | 5af9f11 | 2013-02-07 16:17:54 +0900 | [diff] [blame] | 348 | list_for_each_entry(file_priv, &drm_dev->filelist, lhead) |
| 349 | if (file_priv->filp == filp) |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 350 | return file_priv; |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 351 | |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 352 | WARN_ON(1); |
| 353 | |
| 354 | return ERR_PTR(-EFAULT); |
| 355 | } |
| 356 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 357 | static int exynos_drm_gem_mmap_buffer(struct file *filp, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 358 | struct vm_area_struct *vma) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 359 | { |
| 360 | struct drm_gem_object *obj = filp->private_data; |
| 361 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 362 | struct drm_device *drm_dev = obj->dev; |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 363 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 364 | struct drm_file *file_priv; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 365 | unsigned long vm_size; |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 366 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 367 | |
Konstantin Khlebnikov | 314e51b | 2012-10-08 16:29:02 -0700 | [diff] [blame] | 368 | vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP; |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 369 | vma->vm_private_data = obj; |
| 370 | vma->vm_ops = drm_dev->driver->gem_vm_ops; |
| 371 | |
| 372 | /* restore it to driver's fops. */ |
| 373 | filp->f_op = fops_get(drm_dev->driver->fops); |
| 374 | |
| 375 | file_priv = exynos_drm_find_drm_file(drm_dev, filp); |
| 376 | if (IS_ERR(file_priv)) |
| 377 | return PTR_ERR(file_priv); |
| 378 | |
| 379 | /* restore it to drm_file. */ |
| 380 | filp->private_data = file_priv; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 381 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 382 | update_vm_cache_attr(exynos_gem_obj, vma); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 383 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 384 | vm_size = vma->vm_end - vma->vm_start; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 385 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 386 | /* |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 387 | * a buffer contains information to physically continuous memory |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 388 | * allocated by user request or at framebuffer creation. |
| 389 | */ |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 390 | buffer = exynos_gem_obj->buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 391 | |
| 392 | /* check if user-requested size is valid. */ |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 393 | if (vm_size > buffer->size) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 394 | return -EINVAL; |
| 395 | |
Inki Dae | 4744ad2 | 2012-12-07 17:51:27 +0900 | [diff] [blame] | 396 | ret = dma_mmap_attrs(drm_dev->dev, vma, buffer->pages, |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 397 | buffer->dma_addr, buffer->size, |
| 398 | &buffer->dma_attrs); |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 399 | if (ret < 0) { |
| 400 | DRM_ERROR("failed to mmap.\n"); |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * take a reference to this mapping of the object. And this reference |
| 406 | * is unreferenced by the corresponding vm_close call. |
| 407 | */ |
| 408 | drm_gem_object_reference(obj); |
| 409 | |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 410 | drm_vm_open_locked(drm_dev, vma); |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 411 | |
| 412 | return 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static const struct file_operations exynos_drm_gem_fops = { |
| 416 | .mmap = exynos_drm_gem_mmap_buffer, |
| 417 | }; |
| 418 | |
| 419 | int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 420 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 421 | { |
| 422 | struct drm_exynos_gem_mmap *args = data; |
| 423 | struct drm_gem_object *obj; |
Inki Dae | 23f340e0 | 2013-06-14 17:54:27 +0900 | [diff] [blame] | 424 | unsigned long addr; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 425 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 426 | if (!(dev->driver->driver_features & DRIVER_GEM)) { |
| 427 | DRM_ERROR("does not support GEM.\n"); |
| 428 | return -ENODEV; |
| 429 | } |
| 430 | |
| 431 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 432 | if (!obj) { |
| 433 | DRM_ERROR("failed to lookup gem object.\n"); |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 437 | /* |
YoungJun Cho | 5af9f11 | 2013-02-07 16:17:54 +0900 | [diff] [blame] | 438 | * We have to use gem object and its fops for specific mmaper, |
| 439 | * but vm_mmap() can deliver only filp. So we have to change |
| 440 | * filp->f_op and filp->private_data temporarily, then restore |
| 441 | * again. So it is important to keep lock until restoration the |
| 442 | * settings to prevent others from misuse of filp->f_op or |
| 443 | * filp->private_data. |
| 444 | */ |
| 445 | mutex_lock(&dev->struct_mutex); |
| 446 | |
| 447 | /* |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 448 | * Set specific mmper's fops. And it will be restored by |
| 449 | * exynos_drm_gem_mmap_buffer to dev->driver->fops. |
| 450 | * This is used to call specific mapper temporarily. |
| 451 | */ |
| 452 | file_priv->filp->f_op = &exynos_drm_gem_fops; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 453 | |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 454 | /* |
| 455 | * Set gem object to private_data so that specific mmaper |
| 456 | * can get the gem object. And it will be restored by |
| 457 | * exynos_drm_gem_mmap_buffer to drm_file. |
| 458 | */ |
| 459 | file_priv->filp->private_data = obj; |
| 460 | |
| 461 | addr = vm_mmap(file_priv->filp, 0, args->size, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 462 | PROT_READ | PROT_WRITE, MAP_SHARED, 0); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 463 | |
YoungJun Cho | 5af9f11 | 2013-02-07 16:17:54 +0900 | [diff] [blame] | 464 | drm_gem_object_unreference(obj); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 465 | |
Inki Dae | 23f340e0 | 2013-06-14 17:54:27 +0900 | [diff] [blame] | 466 | if (IS_ERR_VALUE(addr)) { |
YoungJun Cho | 5af9f11 | 2013-02-07 16:17:54 +0900 | [diff] [blame] | 467 | /* check filp->f_op, filp->private_data are restored */ |
| 468 | if (file_priv->filp->f_op == &exynos_drm_gem_fops) { |
| 469 | file_priv->filp->f_op = fops_get(dev->driver->fops); |
| 470 | file_priv->filp->private_data = file_priv; |
| 471 | } |
| 472 | mutex_unlock(&dev->struct_mutex); |
Inki Dae | 23f340e0 | 2013-06-14 17:54:27 +0900 | [diff] [blame] | 473 | return (int)addr; |
Inki Dae | 5b07c66 | 2012-11-08 21:52:54 +0900 | [diff] [blame] | 474 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 475 | |
YoungJun Cho | 5af9f11 | 2013-02-07 16:17:54 +0900 | [diff] [blame] | 476 | mutex_unlock(&dev->struct_mutex); |
| 477 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 478 | args->mapped = addr; |
| 479 | |
| 480 | DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped); |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
Inki Dae | 40cd7e0 | 2012-05-04 15:51:17 +0900 | [diff] [blame] | 485 | int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data, |
| 486 | struct drm_file *file_priv) |
| 487 | { struct exynos_drm_gem_obj *exynos_gem_obj; |
| 488 | struct drm_exynos_gem_info *args = data; |
| 489 | struct drm_gem_object *obj; |
| 490 | |
| 491 | mutex_lock(&dev->struct_mutex); |
| 492 | |
| 493 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 494 | if (!obj) { |
| 495 | DRM_ERROR("failed to lookup gem object.\n"); |
| 496 | mutex_unlock(&dev->struct_mutex); |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | |
| 500 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 501 | |
| 502 | args->flags = exynos_gem_obj->flags; |
| 503 | args->size = exynos_gem_obj->size; |
| 504 | |
| 505 | drm_gem_object_unreference(obj); |
| 506 | mutex_unlock(&dev->struct_mutex); |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 511 | struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma) |
| 512 | { |
| 513 | struct vm_area_struct *vma_copy; |
| 514 | |
| 515 | vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL); |
| 516 | if (!vma_copy) |
| 517 | return NULL; |
| 518 | |
| 519 | if (vma->vm_ops && vma->vm_ops->open) |
| 520 | vma->vm_ops->open(vma); |
| 521 | |
| 522 | if (vma->vm_file) |
| 523 | get_file(vma->vm_file); |
| 524 | |
| 525 | memcpy(vma_copy, vma, sizeof(*vma)); |
| 526 | |
| 527 | vma_copy->vm_mm = NULL; |
| 528 | vma_copy->vm_next = NULL; |
| 529 | vma_copy->vm_prev = NULL; |
| 530 | |
| 531 | return vma_copy; |
| 532 | } |
| 533 | |
| 534 | void exynos_gem_put_vma(struct vm_area_struct *vma) |
| 535 | { |
| 536 | if (!vma) |
| 537 | return; |
| 538 | |
| 539 | if (vma->vm_ops && vma->vm_ops->close) |
| 540 | vma->vm_ops->close(vma); |
| 541 | |
| 542 | if (vma->vm_file) |
| 543 | fput(vma->vm_file); |
| 544 | |
| 545 | kfree(vma); |
| 546 | } |
| 547 | |
| 548 | int exynos_gem_get_pages_from_userptr(unsigned long start, |
| 549 | unsigned int npages, |
| 550 | struct page **pages, |
| 551 | struct vm_area_struct *vma) |
| 552 | { |
| 553 | int get_npages; |
| 554 | |
| 555 | /* the memory region mmaped with VM_PFNMAP. */ |
| 556 | if (vma_is_io(vma)) { |
| 557 | unsigned int i; |
| 558 | |
| 559 | for (i = 0; i < npages; ++i, start += PAGE_SIZE) { |
| 560 | unsigned long pfn; |
| 561 | int ret = follow_pfn(vma, start, &pfn); |
| 562 | if (ret) |
| 563 | return ret; |
| 564 | |
| 565 | pages[i] = pfn_to_page(pfn); |
| 566 | } |
| 567 | |
| 568 | if (i != npages) { |
| 569 | DRM_ERROR("failed to get user_pages.\n"); |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | get_npages = get_user_pages(current, current->mm, start, |
| 577 | npages, 1, 1, pages, NULL); |
| 578 | get_npages = max(get_npages, 0); |
| 579 | if (get_npages != npages) { |
| 580 | DRM_ERROR("failed to get user_pages.\n"); |
| 581 | while (get_npages) |
| 582 | put_page(pages[--get_npages]); |
| 583 | return -EFAULT; |
| 584 | } |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | void exynos_gem_put_pages_to_userptr(struct page **pages, |
| 590 | unsigned int npages, |
| 591 | struct vm_area_struct *vma) |
| 592 | { |
| 593 | if (!vma_is_io(vma)) { |
| 594 | unsigned int i; |
| 595 | |
| 596 | for (i = 0; i < npages; i++) { |
| 597 | set_page_dirty_lock(pages[i]); |
| 598 | |
| 599 | /* |
| 600 | * undo the reference we took when populating |
| 601 | * the table. |
| 602 | */ |
| 603 | put_page(pages[i]); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev, |
| 609 | struct sg_table *sgt, |
| 610 | enum dma_data_direction dir) |
| 611 | { |
| 612 | int nents; |
| 613 | |
| 614 | mutex_lock(&drm_dev->struct_mutex); |
| 615 | |
| 616 | nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir); |
| 617 | if (!nents) { |
| 618 | DRM_ERROR("failed to map sgl with dma.\n"); |
| 619 | mutex_unlock(&drm_dev->struct_mutex); |
| 620 | return nents; |
| 621 | } |
| 622 | |
| 623 | mutex_unlock(&drm_dev->struct_mutex); |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev, |
| 628 | struct sg_table *sgt, |
| 629 | enum dma_data_direction dir) |
| 630 | { |
| 631 | dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir); |
| 632 | } |
| 633 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 634 | int exynos_drm_gem_init_object(struct drm_gem_object *obj) |
| 635 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 639 | void exynos_drm_gem_free_object(struct drm_gem_object *obj) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 640 | { |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 641 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 642 | struct exynos_drm_gem_buf *buf; |
| 643 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 644 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 645 | buf = exynos_gem_obj->buffer; |
| 646 | |
| 647 | if (obj->import_attach) |
| 648 | drm_prime_gem_destroy(obj, buf->sgt); |
| 649 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 650 | exynos_drm_gem_destroy(to_exynos_gem_obj(obj)); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | int exynos_drm_gem_dumb_create(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 654 | struct drm_device *dev, |
| 655 | struct drm_mode_create_dumb *args) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 656 | { |
| 657 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 658 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 659 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 660 | /* |
| 661 | * alocate memory to be used for framebuffer. |
| 662 | * - this callback would be called by user application |
| 663 | * with DRM_IOCTL_MODE_CREATE_DUMB command. |
| 664 | */ |
| 665 | |
Cooper Yuan | 3fd6b69 | 2012-06-29 11:49:45 +0900 | [diff] [blame] | 666 | args->pitch = args->width * ((args->bpp + 7) / 8); |
Inki Dae | 7da5907 | 2012-08-17 15:24:03 +0900 | [diff] [blame] | 667 | args->size = args->pitch * args->height; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 668 | |
Inki Dae | 69961d8 | 2013-04-18 14:04:17 +0900 | [diff] [blame] | 669 | exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG | |
| 670 | EXYNOS_BO_WC, args->size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 671 | if (IS_ERR(exynos_gem_obj)) |
| 672 | return PTR_ERR(exynos_gem_obj); |
| 673 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 674 | ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv, |
| 675 | &args->handle); |
| 676 | if (ret) { |
| 677 | exynos_drm_gem_destroy(exynos_gem_obj); |
| 678 | return ret; |
| 679 | } |
| 680 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 685 | struct drm_device *dev, uint32_t handle, |
| 686 | uint64_t *offset) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 687 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 688 | struct drm_gem_object *obj; |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 689 | int ret = 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 690 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 691 | mutex_lock(&dev->struct_mutex); |
| 692 | |
| 693 | /* |
| 694 | * get offset of memory allocated for drm framebuffer. |
| 695 | * - this callback would be called by user application |
| 696 | * with DRM_IOCTL_MODE_MAP_DUMB command. |
| 697 | */ |
| 698 | |
| 699 | obj = drm_gem_object_lookup(dev, file_priv, handle); |
| 700 | if (!obj) { |
| 701 | DRM_ERROR("failed to lookup gem object.\n"); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 702 | ret = -EINVAL; |
| 703 | goto unlock; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 704 | } |
| 705 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 706 | ret = drm_gem_create_mmap_offset(obj); |
| 707 | if (ret) |
| 708 | goto out; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 709 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 710 | *offset = drm_vma_node_offset_addr(&obj->vma_node); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 711 | DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset); |
| 712 | |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 713 | out: |
| 714 | drm_gem_object_unreference(obj); |
| 715 | unlock: |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 716 | mutex_unlock(&dev->struct_mutex); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 717 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 718 | } |
| 719 | |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 720 | int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv, |
| 721 | struct drm_device *dev, |
| 722 | unsigned int handle) |
| 723 | { |
| 724 | int ret; |
| 725 | |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 726 | /* |
| 727 | * obj->refcount and obj->handle_count are decreased and |
| 728 | * if both them are 0 then exynos_drm_gem_free_object() |
| 729 | * would be called by callback to release resources. |
| 730 | */ |
| 731 | ret = drm_gem_handle_delete(file_priv, handle); |
| 732 | if (ret < 0) { |
| 733 | DRM_ERROR("failed to delete drm_gem_handle.\n"); |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 740 | int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 741 | { |
| 742 | struct drm_gem_object *obj = vma->vm_private_data; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 743 | struct drm_device *dev = obj->dev; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 744 | unsigned long f_vaddr; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 745 | pgoff_t page_offset; |
| 746 | int ret; |
| 747 | |
| 748 | page_offset = ((unsigned long)vmf->virtual_address - |
| 749 | vma->vm_start) >> PAGE_SHIFT; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 750 | f_vaddr = (unsigned long)vmf->virtual_address; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 751 | |
| 752 | mutex_lock(&dev->struct_mutex); |
| 753 | |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 754 | ret = exynos_drm_gem_map_buf(obj, vma, f_vaddr, page_offset); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 755 | if (ret < 0) |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 756 | DRM_ERROR("failed to map a buffer with user.\n"); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 757 | |
| 758 | mutex_unlock(&dev->struct_mutex); |
| 759 | |
| 760 | return convert_to_vm_err_msg(ret); |
| 761 | } |
| 762 | |
| 763 | int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 764 | { |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 765 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 766 | struct drm_gem_object *obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 767 | int ret; |
| 768 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 769 | /* set vm_area_struct. */ |
| 770 | ret = drm_gem_mmap(filp, vma); |
| 771 | if (ret < 0) { |
| 772 | DRM_ERROR("failed to mmap.\n"); |
| 773 | return ret; |
| 774 | } |
| 775 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 776 | obj = vma->vm_private_data; |
| 777 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 778 | |
| 779 | ret = check_gem_flags(exynos_gem_obj->flags); |
| 780 | if (ret) { |
| 781 | drm_gem_vm_close(vma); |
| 782 | drm_gem_free_mmap_offset(obj); |
| 783 | return ret; |
| 784 | } |
| 785 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 786 | vma->vm_flags &= ~VM_PFNMAP; |
| 787 | vma->vm_flags |= VM_MIXEDMAP; |
| 788 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 789 | update_vm_cache_attr(exynos_gem_obj, vma); |
| 790 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 791 | return ret; |
| 792 | } |