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 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "drmP.h" |
| 27 | #include "drm.h" |
| 28 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 29 | #include <linux/shmem_fs.h> |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 30 | #include <drm/exynos_drm.h> |
| 31 | |
| 32 | #include "exynos_drm_drv.h" |
| 33 | #include "exynos_drm_gem.h" |
| 34 | #include "exynos_drm_buf.h" |
| 35 | |
| 36 | static unsigned int convert_to_vm_err_msg(int msg) |
| 37 | { |
| 38 | unsigned int out_msg; |
| 39 | |
| 40 | switch (msg) { |
| 41 | case 0: |
| 42 | case -ERESTARTSYS: |
| 43 | case -EINTR: |
| 44 | out_msg = VM_FAULT_NOPAGE; |
| 45 | break; |
| 46 | |
| 47 | case -ENOMEM: |
| 48 | out_msg = VM_FAULT_OOM; |
| 49 | break; |
| 50 | |
| 51 | default: |
| 52 | out_msg = VM_FAULT_SIGBUS; |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | return out_msg; |
| 57 | } |
| 58 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 59 | static int check_gem_flags(unsigned int flags) |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 60 | { |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 61 | if (flags & ~(EXYNOS_BO_MASK)) { |
| 62 | DRM_ERROR("invalid flags.\n"); |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 69 | static void update_vm_cache_attr(struct exynos_drm_gem_obj *obj, |
| 70 | struct vm_area_struct *vma) |
| 71 | { |
| 72 | DRM_DEBUG_KMS("flags = 0x%x\n", obj->flags); |
| 73 | |
| 74 | /* non-cachable as default. */ |
| 75 | if (obj->flags & EXYNOS_BO_CACHABLE) |
| 76 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 77 | else if (obj->flags & EXYNOS_BO_WC) |
| 78 | vma->vm_page_prot = |
| 79 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 80 | else |
| 81 | vma->vm_page_prot = |
| 82 | pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 83 | } |
| 84 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 85 | static unsigned long roundup_gem_size(unsigned long size, unsigned int flags) |
| 86 | { |
| 87 | if (!IS_NONCONTIG_BUFFER(flags)) { |
| 88 | if (size >= SZ_1M) |
| 89 | return roundup(size, SECTION_SIZE); |
| 90 | else if (size >= SZ_64K) |
| 91 | return roundup(size, SZ_64K); |
| 92 | else |
| 93 | goto out; |
| 94 | } |
| 95 | out: |
| 96 | return roundup(size, PAGE_SIZE); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 97 | } |
| 98 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 99 | struct page **exynos_gem_get_pages(struct drm_gem_object *obj, |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 100 | gfp_t gfpmask) |
| 101 | { |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 102 | struct page *p, **pages; |
| 103 | int i, npages; |
| 104 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 105 | npages = obj->size >> PAGE_SHIFT; |
| 106 | |
| 107 | pages = drm_malloc_ab(npages, sizeof(struct page *)); |
| 108 | if (pages == NULL) |
| 109 | return ERR_PTR(-ENOMEM); |
| 110 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 111 | for (i = 0; i < npages; i++) { |
Inki Dae | c62bc75 | 2012-06-07 15:59:48 +0900 | [diff] [blame] | 112 | p = alloc_page(gfpmask); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 113 | if (IS_ERR(p)) |
| 114 | goto fail; |
| 115 | pages[i] = p; |
| 116 | } |
| 117 | |
| 118 | return pages; |
| 119 | |
| 120 | fail: |
Inki Dae | cb364e3 | 2012-07-09 15:42:16 +0900 | [diff] [blame] | 121 | while (--i) |
Inki Dae | c62bc75 | 2012-06-07 15:59:48 +0900 | [diff] [blame] | 122 | __free_page(pages[i]); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 123 | |
| 124 | drm_free_large(pages); |
| 125 | return ERR_PTR(PTR_ERR(p)); |
| 126 | } |
| 127 | |
| 128 | static void exynos_gem_put_pages(struct drm_gem_object *obj, |
Inki Dae | d73c1c9 | 2012-07-09 14:35:38 +0900 | [diff] [blame] | 129 | struct page **pages) |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 130 | { |
Inki Dae | d73c1c9 | 2012-07-09 14:35:38 +0900 | [diff] [blame] | 131 | int npages; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 132 | |
| 133 | npages = obj->size >> PAGE_SHIFT; |
| 134 | |
Inki Dae | d73c1c9 | 2012-07-09 14:35:38 +0900 | [diff] [blame] | 135 | while (--npages >= 0) |
| 136 | __free_page(pages[npages]); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 137 | |
| 138 | drm_free_large(pages); |
| 139 | } |
| 140 | |
| 141 | static int exynos_drm_gem_map_pages(struct drm_gem_object *obj, |
| 142 | struct vm_area_struct *vma, |
| 143 | unsigned long f_vaddr, |
| 144 | pgoff_t page_offset) |
| 145 | { |
| 146 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
| 147 | struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer; |
| 148 | unsigned long pfn; |
| 149 | |
| 150 | if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) { |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 151 | if (!buf->pages) |
| 152 | return -EINTR; |
| 153 | |
Inki Dae | f6ead8d | 2012-04-23 19:41:14 +0900 | [diff] [blame] | 154 | pfn = page_to_pfn(buf->pages[page_offset++]); |
| 155 | } else |
| 156 | pfn = (buf->dma_addr >> PAGE_SHIFT) + page_offset; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 157 | |
| 158 | return vm_insert_mixed(vma, f_vaddr, pfn); |
| 159 | } |
| 160 | |
| 161 | static int exynos_drm_gem_get_pages(struct drm_gem_object *obj) |
| 162 | { |
| 163 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
| 164 | struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer; |
| 165 | struct scatterlist *sgl; |
| 166 | struct page **pages; |
| 167 | unsigned int npages, i = 0; |
| 168 | int ret; |
| 169 | |
| 170 | if (buf->pages) { |
| 171 | DRM_DEBUG_KMS("already allocated.\n"); |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
Inki Dae | c62bc75 | 2012-06-07 15:59:48 +0900 | [diff] [blame] | 175 | pages = exynos_gem_get_pages(obj, GFP_HIGHUSER_MOVABLE); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 176 | if (IS_ERR(pages)) { |
| 177 | DRM_ERROR("failed to get pages.\n"); |
| 178 | return PTR_ERR(pages); |
| 179 | } |
| 180 | |
| 181 | npages = obj->size >> PAGE_SHIFT; |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 182 | buf->page_size = PAGE_SIZE; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 183 | |
| 184 | buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 185 | if (!buf->sgt) { |
| 186 | DRM_ERROR("failed to allocate sg table.\n"); |
| 187 | ret = -ENOMEM; |
| 188 | goto err; |
| 189 | } |
| 190 | |
| 191 | ret = sg_alloc_table(buf->sgt, npages, GFP_KERNEL); |
| 192 | if (ret < 0) { |
| 193 | DRM_ERROR("failed to initialize sg table.\n"); |
| 194 | ret = -EFAULT; |
| 195 | goto err1; |
| 196 | } |
| 197 | |
| 198 | sgl = buf->sgt->sgl; |
| 199 | |
| 200 | /* set all pages to sg list. */ |
| 201 | while (i < npages) { |
| 202 | sg_set_page(sgl, pages[i], PAGE_SIZE, 0); |
| 203 | sg_dma_address(sgl) = page_to_phys(pages[i]); |
| 204 | i++; |
| 205 | sgl = sg_next(sgl); |
| 206 | } |
| 207 | |
| 208 | /* add some codes for UNCACHED type here. TODO */ |
| 209 | |
| 210 | buf->pages = pages; |
| 211 | return ret; |
| 212 | err1: |
| 213 | kfree(buf->sgt); |
| 214 | buf->sgt = NULL; |
| 215 | err: |
Inki Dae | d73c1c9 | 2012-07-09 14:35:38 +0900 | [diff] [blame] | 216 | exynos_gem_put_pages(obj, pages); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 217 | return ret; |
| 218 | |
| 219 | } |
| 220 | |
| 221 | static void exynos_drm_gem_put_pages(struct drm_gem_object *obj) |
| 222 | { |
| 223 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
| 224 | struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer; |
| 225 | |
| 226 | /* |
| 227 | * if buffer typs is EXYNOS_BO_NONCONTIG then release all pages |
| 228 | * allocated at gem fault handler. |
| 229 | */ |
| 230 | sg_free_table(buf->sgt); |
| 231 | kfree(buf->sgt); |
| 232 | buf->sgt = NULL; |
| 233 | |
Inki Dae | d73c1c9 | 2012-07-09 14:35:38 +0900 | [diff] [blame] | 234 | exynos_gem_put_pages(obj, buf->pages); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 235 | buf->pages = NULL; |
| 236 | |
| 237 | /* add some codes for UNCACHED type here. TODO */ |
| 238 | } |
| 239 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 240 | static int exynos_drm_gem_handle_create(struct drm_gem_object *obj, |
| 241 | struct drm_file *file_priv, |
| 242 | unsigned int *handle) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 243 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 244 | int ret; |
| 245 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 246 | /* |
| 247 | * allocate a id of idr table where the obj is registered |
| 248 | * and handle has the id what user can see. |
| 249 | */ |
| 250 | ret = drm_gem_handle_create(file_priv, obj, handle); |
| 251 | if (ret) |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 252 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 253 | |
| 254 | DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle); |
| 255 | |
| 256 | /* drop reference from allocate - handle holds it now. */ |
| 257 | drm_gem_object_unreference_unlocked(obj); |
| 258 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 259 | return 0; |
| 260 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 261 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 262 | void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj) |
| 263 | { |
| 264 | struct drm_gem_object *obj; |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 265 | struct exynos_drm_gem_buf *buf; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 266 | |
| 267 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 268 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 269 | obj = &exynos_gem_obj->base; |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 270 | buf = exynos_gem_obj->buffer; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 271 | |
| 272 | DRM_DEBUG_KMS("handle count = %d\n", atomic_read(&obj->handle_count)); |
| 273 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 274 | if (!buf->pages) |
| 275 | return; |
| 276 | |
Inki Dae | c374e73 | 2012-06-12 16:52:54 +0900 | [diff] [blame] | 277 | /* |
| 278 | * do not release memory region from exporter. |
| 279 | * |
| 280 | * the region will be released by exporter |
| 281 | * once dmabuf's refcount becomes 0. |
| 282 | */ |
| 283 | if (obj->import_attach) |
| 284 | goto out; |
| 285 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 286 | if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 287 | exynos_drm_gem_put_pages(obj); |
| 288 | else |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 289 | exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags, buf); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 290 | |
Inki Dae | c374e73 | 2012-06-12 16:52:54 +0900 | [diff] [blame] | 291 | out: |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 292 | exynos_drm_fini_buf(obj->dev, buf); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 293 | exynos_gem_obj->buffer = NULL; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 294 | |
| 295 | if (obj->map_list.map) |
| 296 | drm_gem_free_mmap_offset(obj); |
| 297 | |
| 298 | /* release file pointer to gem object. */ |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 299 | drm_gem_object_release(obj); |
| 300 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 301 | kfree(exynos_gem_obj); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 302 | exynos_gem_obj = NULL; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 303 | } |
| 304 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 305 | 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] | 306 | unsigned long size) |
| 307 | { |
| 308 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 309 | struct drm_gem_object *obj; |
| 310 | int ret; |
| 311 | |
| 312 | exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL); |
| 313 | if (!exynos_gem_obj) { |
| 314 | DRM_ERROR("failed to allocate exynos gem object\n"); |
| 315 | return NULL; |
| 316 | } |
| 317 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 318 | exynos_gem_obj->size = size; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 319 | obj = &exynos_gem_obj->base; |
| 320 | |
| 321 | ret = drm_gem_object_init(dev, obj, size); |
| 322 | if (ret < 0) { |
| 323 | DRM_ERROR("failed to initialize gem object\n"); |
| 324 | kfree(exynos_gem_obj); |
| 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp); |
| 329 | |
| 330 | return exynos_gem_obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 331 | } |
| 332 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 333 | 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] | 334 | unsigned int flags, |
| 335 | unsigned long size) |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 336 | { |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 337 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 338 | struct exynos_drm_gem_buf *buf; |
| 339 | int ret; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 340 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 341 | if (!size) { |
| 342 | DRM_ERROR("invalid size.\n"); |
| 343 | return ERR_PTR(-EINVAL); |
| 344 | } |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 345 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 346 | size = roundup_gem_size(size, flags); |
| 347 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 348 | |
| 349 | ret = check_gem_flags(flags); |
| 350 | if (ret) |
| 351 | return ERR_PTR(ret); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 352 | |
| 353 | buf = exynos_drm_init_buf(dev, size); |
| 354 | if (!buf) |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 355 | return ERR_PTR(-ENOMEM); |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 356 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 357 | exynos_gem_obj = exynos_drm_gem_init(dev, size); |
| 358 | if (!exynos_gem_obj) { |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 359 | ret = -ENOMEM; |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 360 | goto err_fini_buf; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 361 | } |
| 362 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 363 | exynos_gem_obj->buffer = buf; |
| 364 | |
| 365 | /* set memory type and cache attribute from user side. */ |
| 366 | exynos_gem_obj->flags = flags; |
| 367 | |
| 368 | /* |
| 369 | * allocate all pages as desired size if user wants to allocate |
| 370 | * physically non-continuous memory. |
| 371 | */ |
| 372 | if (flags & EXYNOS_BO_NONCONTIG) { |
| 373 | ret = exynos_drm_gem_get_pages(&exynos_gem_obj->base); |
| 374 | if (ret < 0) { |
| 375 | drm_gem_object_release(&exynos_gem_obj->base); |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 376 | goto err_fini_buf; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 377 | } |
| 378 | } else { |
| 379 | ret = exynos_drm_alloc_buf(dev, buf, flags); |
| 380 | if (ret < 0) { |
| 381 | drm_gem_object_release(&exynos_gem_obj->base); |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 382 | goto err_fini_buf; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 383 | } |
| 384 | } |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 385 | |
| 386 | return exynos_gem_obj; |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 387 | |
| 388 | err_fini_buf: |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 389 | exynos_drm_fini_buf(dev, buf); |
| 390 | return ERR_PTR(ret); |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 391 | } |
| 392 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 393 | int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 394 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 395 | { |
| 396 | struct drm_exynos_gem_create *args = data; |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 397 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 398 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 399 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 400 | DRM_DEBUG_KMS("%s\n", __FILE__); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 401 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 402 | exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 403 | if (IS_ERR(exynos_gem_obj)) |
| 404 | return PTR_ERR(exynos_gem_obj); |
| 405 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 406 | ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv, |
| 407 | &args->handle); |
| 408 | if (ret) { |
| 409 | exynos_drm_gem_destroy(exynos_gem_obj); |
| 410 | return ret; |
| 411 | } |
| 412 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 416 | void *exynos_drm_gem_get_dma_addr(struct drm_device *dev, |
| 417 | unsigned int gem_handle, |
| 418 | struct drm_file *file_priv) |
| 419 | { |
| 420 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 421 | struct drm_gem_object *obj; |
| 422 | |
| 423 | obj = drm_gem_object_lookup(dev, file_priv, gem_handle); |
| 424 | if (!obj) { |
| 425 | DRM_ERROR("failed to lookup gem object.\n"); |
| 426 | return ERR_PTR(-EINVAL); |
| 427 | } |
| 428 | |
| 429 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 430 | |
| 431 | if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) { |
| 432 | DRM_DEBUG_KMS("not support NONCONTIG type.\n"); |
| 433 | drm_gem_object_unreference_unlocked(obj); |
| 434 | |
| 435 | /* TODO */ |
| 436 | return ERR_PTR(-EINVAL); |
| 437 | } |
| 438 | |
| 439 | return &exynos_gem_obj->buffer->dma_addr; |
| 440 | } |
| 441 | |
| 442 | void exynos_drm_gem_put_dma_addr(struct drm_device *dev, |
| 443 | unsigned int gem_handle, |
| 444 | struct drm_file *file_priv) |
| 445 | { |
| 446 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 447 | struct drm_gem_object *obj; |
| 448 | |
| 449 | obj = drm_gem_object_lookup(dev, file_priv, gem_handle); |
| 450 | if (!obj) { |
| 451 | DRM_ERROR("failed to lookup gem object.\n"); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 456 | |
| 457 | if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) { |
| 458 | DRM_DEBUG_KMS("not support NONCONTIG type.\n"); |
| 459 | drm_gem_object_unreference_unlocked(obj); |
| 460 | |
| 461 | /* TODO */ |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | drm_gem_object_unreference_unlocked(obj); |
| 466 | |
| 467 | /* |
| 468 | * decrease obj->refcount one more time because we has already |
| 469 | * increased it at exynos_drm_gem_get_dma_addr(). |
| 470 | */ |
| 471 | drm_gem_object_unreference_unlocked(obj); |
| 472 | } |
| 473 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 474 | 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] | 475 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 476 | { |
| 477 | struct drm_exynos_gem_map_off *args = data; |
| 478 | |
| 479 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 480 | |
| 481 | DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n", |
| 482 | args->handle, (unsigned long)args->offset); |
| 483 | |
| 484 | if (!(dev->driver->driver_features & DRIVER_GEM)) { |
| 485 | DRM_ERROR("does not support GEM.\n"); |
| 486 | return -ENODEV; |
| 487 | } |
| 488 | |
| 489 | return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle, |
| 490 | &args->offset); |
| 491 | } |
| 492 | |
| 493 | static int exynos_drm_gem_mmap_buffer(struct file *filp, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 494 | struct vm_area_struct *vma) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 495 | { |
| 496 | struct drm_gem_object *obj = filp->private_data; |
| 497 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 498 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 499 | unsigned long pfn, vm_size, usize, uaddr = vma->vm_start; |
| 500 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 501 | |
| 502 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 503 | |
| 504 | vma->vm_flags |= (VM_IO | VM_RESERVED); |
| 505 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 506 | update_vm_cache_attr(exynos_gem_obj, vma); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 507 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 508 | vm_size = usize = vma->vm_end - vma->vm_start; |
| 509 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 510 | /* |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 511 | * a buffer contains information to physically continuous memory |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 512 | * allocated by user request or at framebuffer creation. |
| 513 | */ |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 514 | buffer = exynos_gem_obj->buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 515 | |
| 516 | /* check if user-requested size is valid. */ |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 517 | if (vm_size > buffer->size) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 518 | return -EINVAL; |
| 519 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 520 | if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) { |
| 521 | int i = 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 522 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 523 | if (!buffer->pages) |
| 524 | return -EINVAL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 525 | |
Inki Dae | 818c4ea | 2012-04-23 19:47:18 +0900 | [diff] [blame] | 526 | vma->vm_flags |= VM_MIXEDMAP; |
| 527 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 528 | do { |
| 529 | ret = vm_insert_page(vma, uaddr, buffer->pages[i++]); |
| 530 | if (ret) { |
| 531 | DRM_ERROR("failed to remap user space.\n"); |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | uaddr += PAGE_SIZE; |
| 536 | usize -= PAGE_SIZE; |
| 537 | } while (usize > 0); |
| 538 | } else { |
| 539 | /* |
| 540 | * get page frame number to physical memory to be mapped |
| 541 | * to user space. |
| 542 | */ |
| 543 | pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >> |
| 544 | PAGE_SHIFT; |
| 545 | |
| 546 | DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn); |
| 547 | |
| 548 | if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size, |
| 549 | vma->vm_page_prot)) { |
| 550 | DRM_ERROR("failed to remap pfn range.\n"); |
| 551 | return -EAGAIN; |
| 552 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static const struct file_operations exynos_drm_gem_fops = { |
| 559 | .mmap = exynos_drm_gem_mmap_buffer, |
| 560 | }; |
| 561 | |
| 562 | int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 563 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 564 | { |
| 565 | struct drm_exynos_gem_mmap *args = data; |
| 566 | struct drm_gem_object *obj; |
| 567 | unsigned int addr; |
| 568 | |
| 569 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 570 | |
| 571 | if (!(dev->driver->driver_features & DRIVER_GEM)) { |
| 572 | DRM_ERROR("does not support GEM.\n"); |
| 573 | return -ENODEV; |
| 574 | } |
| 575 | |
| 576 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 577 | if (!obj) { |
| 578 | DRM_ERROR("failed to lookup gem object.\n"); |
| 579 | return -EINVAL; |
| 580 | } |
| 581 | |
| 582 | obj->filp->f_op = &exynos_drm_gem_fops; |
| 583 | obj->filp->private_data = obj; |
| 584 | |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 585 | addr = vm_mmap(obj->filp, 0, args->size, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 586 | PROT_READ | PROT_WRITE, MAP_SHARED, 0); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 587 | |
| 588 | drm_gem_object_unreference_unlocked(obj); |
| 589 | |
| 590 | if (IS_ERR((void *)addr)) |
| 591 | return PTR_ERR((void *)addr); |
| 592 | |
| 593 | args->mapped = addr; |
| 594 | |
| 595 | DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped); |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
Inki Dae | 40cd7e0 | 2012-05-04 15:51:17 +0900 | [diff] [blame] | 600 | int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data, |
| 601 | struct drm_file *file_priv) |
| 602 | { struct exynos_drm_gem_obj *exynos_gem_obj; |
| 603 | struct drm_exynos_gem_info *args = data; |
| 604 | struct drm_gem_object *obj; |
| 605 | |
| 606 | mutex_lock(&dev->struct_mutex); |
| 607 | |
| 608 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 609 | if (!obj) { |
| 610 | DRM_ERROR("failed to lookup gem object.\n"); |
| 611 | mutex_unlock(&dev->struct_mutex); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
| 615 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 616 | |
| 617 | args->flags = exynos_gem_obj->flags; |
| 618 | args->size = exynos_gem_obj->size; |
| 619 | |
| 620 | drm_gem_object_unreference(obj); |
| 621 | mutex_unlock(&dev->struct_mutex); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 626 | int exynos_drm_gem_init_object(struct drm_gem_object *obj) |
| 627 | { |
| 628 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 633 | void exynos_drm_gem_free_object(struct drm_gem_object *obj) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 634 | { |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 635 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 636 | struct exynos_drm_gem_buf *buf; |
| 637 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 638 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 639 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 640 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 641 | buf = exynos_gem_obj->buffer; |
| 642 | |
| 643 | if (obj->import_attach) |
| 644 | drm_prime_gem_destroy(obj, buf->sgt); |
| 645 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 646 | exynos_drm_gem_destroy(to_exynos_gem_obj(obj)); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | int exynos_drm_gem_dumb_create(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 650 | struct drm_device *dev, |
| 651 | struct drm_mode_create_dumb *args) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 652 | { |
| 653 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 654 | int ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 655 | |
| 656 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 657 | |
| 658 | /* |
| 659 | * alocate memory to be used for framebuffer. |
| 660 | * - this callback would be called by user application |
| 661 | * with DRM_IOCTL_MODE_CREATE_DUMB command. |
| 662 | */ |
| 663 | |
Cooper Yuan | 3fd6b69 | 2012-06-29 11:49:45 +0900 | [diff] [blame] | 664 | args->pitch = args->width * ((args->bpp + 7) / 8); |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 665 | args->size = PAGE_ALIGN(args->pitch * args->height); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 666 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 667 | exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 668 | if (IS_ERR(exynos_gem_obj)) |
| 669 | return PTR_ERR(exynos_gem_obj); |
| 670 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 671 | ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv, |
| 672 | &args->handle); |
| 673 | if (ret) { |
| 674 | exynos_drm_gem_destroy(exynos_gem_obj); |
| 675 | return ret; |
| 676 | } |
| 677 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 682 | struct drm_device *dev, uint32_t handle, |
| 683 | uint64_t *offset) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 684 | { |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 685 | struct drm_gem_object *obj; |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 686 | int ret = 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 687 | |
| 688 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 689 | |
| 690 | mutex_lock(&dev->struct_mutex); |
| 691 | |
| 692 | /* |
| 693 | * get offset of memory allocated for drm framebuffer. |
| 694 | * - this callback would be called by user application |
| 695 | * with DRM_IOCTL_MODE_MAP_DUMB command. |
| 696 | */ |
| 697 | |
| 698 | obj = drm_gem_object_lookup(dev, file_priv, handle); |
| 699 | if (!obj) { |
| 700 | DRM_ERROR("failed to lookup gem object.\n"); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 701 | ret = -EINVAL; |
| 702 | goto unlock; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 703 | } |
| 704 | |
Laurent Pinchart | 6037baf | 2012-05-16 17:08:55 +0200 | [diff] [blame] | 705 | if (!obj->map_list.map) { |
| 706 | ret = drm_gem_create_mmap_offset(obj); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 707 | if (ret) |
| 708 | goto out; |
| 709 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 710 | |
Laurent Pinchart | 6037baf | 2012-05-16 17:08:55 +0200 | [diff] [blame] | 711 | *offset = (u64)obj->map_list.hash.key << PAGE_SHIFT; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 712 | DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset); |
| 713 | |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 714 | out: |
| 715 | drm_gem_object_unreference(obj); |
| 716 | unlock: |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 717 | mutex_unlock(&dev->struct_mutex); |
Joonyoung Shim | 2d91cf1 | 2011-12-13 14:32:24 +0900 | [diff] [blame] | 718 | return ret; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 719 | } |
| 720 | |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 721 | int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv, |
| 722 | struct drm_device *dev, |
| 723 | unsigned int handle) |
| 724 | { |
| 725 | int ret; |
| 726 | |
| 727 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 728 | |
| 729 | /* |
| 730 | * obj->refcount and obj->handle_count are decreased and |
| 731 | * if both them are 0 then exynos_drm_gem_free_object() |
| 732 | * would be called by callback to release resources. |
| 733 | */ |
| 734 | ret = drm_gem_handle_delete(file_priv, handle); |
| 735 | if (ret < 0) { |
| 736 | DRM_ERROR("failed to delete drm_gem_handle.\n"); |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 743 | int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 744 | { |
| 745 | struct drm_gem_object *obj = vma->vm_private_data; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 746 | struct drm_device *dev = obj->dev; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 747 | unsigned long f_vaddr; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 748 | pgoff_t page_offset; |
| 749 | int ret; |
| 750 | |
| 751 | page_offset = ((unsigned long)vmf->virtual_address - |
| 752 | vma->vm_start) >> PAGE_SHIFT; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 753 | f_vaddr = (unsigned long)vmf->virtual_address; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 754 | |
| 755 | mutex_lock(&dev->struct_mutex); |
| 756 | |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 757 | ret = exynos_drm_gem_map_pages(obj, vma, f_vaddr, page_offset); |
| 758 | if (ret < 0) |
| 759 | DRM_ERROR("failed to map pages.\n"); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 760 | |
| 761 | mutex_unlock(&dev->struct_mutex); |
| 762 | |
| 763 | return convert_to_vm_err_msg(ret); |
| 764 | } |
| 765 | |
| 766 | int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 767 | { |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 768 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 769 | struct drm_gem_object *obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 770 | int ret; |
| 771 | |
| 772 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 773 | |
| 774 | /* set vm_area_struct. */ |
| 775 | ret = drm_gem_mmap(filp, vma); |
| 776 | if (ret < 0) { |
| 777 | DRM_ERROR("failed to mmap.\n"); |
| 778 | return ret; |
| 779 | } |
| 780 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 781 | obj = vma->vm_private_data; |
| 782 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 783 | |
| 784 | ret = check_gem_flags(exynos_gem_obj->flags); |
| 785 | if (ret) { |
| 786 | drm_gem_vm_close(vma); |
| 787 | drm_gem_free_mmap_offset(obj); |
| 788 | return ret; |
| 789 | } |
| 790 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 791 | vma->vm_flags &= ~VM_PFNMAP; |
| 792 | vma->vm_flags |= VM_MIXEDMAP; |
| 793 | |
Inki Dae | c01d73fa | 2012-04-23 19:26:34 +0900 | [diff] [blame] | 794 | update_vm_cache_attr(exynos_gem_obj, vma); |
| 795 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 796 | return ret; |
| 797 | } |