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