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 | |
| 29 | #include <drm/exynos_drm.h> |
| 30 | |
| 31 | #include "exynos_drm_drv.h" |
| 32 | #include "exynos_drm_gem.h" |
| 33 | #include "exynos_drm_buf.h" |
| 34 | |
| 35 | static unsigned int convert_to_vm_err_msg(int msg) |
| 36 | { |
| 37 | unsigned int out_msg; |
| 38 | |
| 39 | switch (msg) { |
| 40 | case 0: |
| 41 | case -ERESTARTSYS: |
| 42 | case -EINTR: |
| 43 | out_msg = VM_FAULT_NOPAGE; |
| 44 | break; |
| 45 | |
| 46 | case -ENOMEM: |
| 47 | out_msg = VM_FAULT_OOM; |
| 48 | break; |
| 49 | |
| 50 | default: |
| 51 | out_msg = VM_FAULT_SIGBUS; |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | return out_msg; |
| 56 | } |
| 57 | |
| 58 | static unsigned int get_gem_mmap_offset(struct drm_gem_object *obj) |
| 59 | { |
| 60 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 61 | |
| 62 | return (unsigned int)obj->map_list.hash.key << PAGE_SHIFT; |
| 63 | } |
| 64 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 65 | static struct exynos_drm_gem_obj |
| 66 | *exynos_drm_gem_init(struct drm_device *drm_dev, |
| 67 | struct drm_file *file_priv, unsigned int *handle, |
| 68 | unsigned int size) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 69 | { |
| 70 | struct exynos_drm_gem_obj *exynos_gem_obj; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 71 | struct drm_gem_object *obj; |
| 72 | int ret; |
| 73 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 74 | exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL); |
| 75 | if (!exynos_gem_obj) { |
| 76 | DRM_ERROR("failed to allocate exynos gem object.\n"); |
| 77 | return ERR_PTR(-ENOMEM); |
| 78 | } |
| 79 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 80 | obj = &exynos_gem_obj->base; |
| 81 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 82 | ret = drm_gem_object_init(drm_dev, obj, size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 83 | if (ret < 0) { |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 84 | DRM_ERROR("failed to initialize gem object.\n"); |
| 85 | ret = -EINVAL; |
| 86 | goto err_object_init; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp); |
| 90 | |
| 91 | ret = drm_gem_create_mmap_offset(obj); |
| 92 | if (ret < 0) { |
| 93 | DRM_ERROR("failed to allocate mmap offset.\n"); |
| 94 | goto err_create_mmap_offset; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * allocate a id of idr table where the obj is registered |
| 99 | * and handle has the id what user can see. |
| 100 | */ |
| 101 | ret = drm_gem_handle_create(file_priv, obj, handle); |
| 102 | if (ret) |
| 103 | goto err_handle_create; |
| 104 | |
| 105 | DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle); |
| 106 | |
| 107 | /* drop reference from allocate - handle holds it now. */ |
| 108 | drm_gem_object_unreference_unlocked(obj); |
| 109 | |
| 110 | return exynos_gem_obj; |
| 111 | |
| 112 | err_handle_create: |
| 113 | drm_gem_free_mmap_offset(obj); |
| 114 | |
| 115 | err_create_mmap_offset: |
| 116 | drm_gem_object_release(obj); |
| 117 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 118 | err_object_init: |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 119 | kfree(exynos_gem_obj); |
| 120 | |
| 121 | return ERR_PTR(ret); |
| 122 | } |
| 123 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 124 | struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev, |
| 125 | struct drm_file *file_priv, |
| 126 | unsigned int *handle, unsigned long size) |
| 127 | { |
| 128 | |
| 129 | struct exynos_drm_gem_obj *exynos_gem_obj = NULL; |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 130 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 131 | int ret; |
| 132 | |
| 133 | size = roundup(size, PAGE_SIZE); |
| 134 | |
| 135 | DRM_DEBUG_KMS("%s: size = 0x%lx\n", __FILE__, size); |
| 136 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 137 | buffer = exynos_drm_buf_create(dev, size); |
| 138 | if (!buffer) |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 139 | return ERR_PTR(-ENOMEM); |
| 140 | |
| 141 | exynos_gem_obj = exynos_drm_gem_init(dev, file_priv, handle, size); |
| 142 | if (IS_ERR(exynos_gem_obj)) { |
| 143 | ret = PTR_ERR(exynos_gem_obj); |
| 144 | goto err_gem_init; |
| 145 | } |
| 146 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 147 | exynos_gem_obj->buffer = buffer; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 148 | |
| 149 | return exynos_gem_obj; |
| 150 | |
| 151 | err_gem_init: |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 152 | exynos_drm_buf_destroy(dev, exynos_gem_obj->buffer); |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 153 | |
| 154 | return ERR_PTR(ret); |
| 155 | } |
| 156 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 157 | int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data, |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 158 | struct drm_file *file_priv) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 159 | { |
| 160 | struct drm_exynos_gem_create *args = data; |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 161 | struct exynos_drm_gem_obj *exynos_gem_obj = NULL; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 162 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 163 | DRM_DEBUG_KMS("%s\n", __FILE__); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 164 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 165 | exynos_gem_obj = exynos_drm_gem_create(dev, file_priv, |
| 166 | &args->handle, args->size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 167 | if (IS_ERR(exynos_gem_obj)) |
| 168 | return PTR_ERR(exynos_gem_obj); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data, |
| 174 | struct drm_file *file_priv) |
| 175 | { |
| 176 | struct drm_exynos_gem_map_off *args = data; |
| 177 | |
| 178 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 179 | |
| 180 | DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n", |
| 181 | args->handle, (unsigned long)args->offset); |
| 182 | |
| 183 | if (!(dev->driver->driver_features & DRIVER_GEM)) { |
| 184 | DRM_ERROR("does not support GEM.\n"); |
| 185 | return -ENODEV; |
| 186 | } |
| 187 | |
| 188 | return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle, |
| 189 | &args->offset); |
| 190 | } |
| 191 | |
| 192 | static int exynos_drm_gem_mmap_buffer(struct file *filp, |
| 193 | struct vm_area_struct *vma) |
| 194 | { |
| 195 | struct drm_gem_object *obj = filp->private_data; |
| 196 | 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^] | 197 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 198 | unsigned long pfn, vm_size; |
| 199 | |
| 200 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 201 | |
| 202 | vma->vm_flags |= (VM_IO | VM_RESERVED); |
| 203 | |
| 204 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 205 | vma->vm_file = filp; |
| 206 | |
| 207 | vm_size = vma->vm_end - vma->vm_start; |
| 208 | /* |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 209 | * a buffer contains information to physically continuous memory |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 210 | * allocated by user request or at framebuffer creation. |
| 211 | */ |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 212 | buffer = exynos_gem_obj->buffer; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 213 | |
| 214 | /* check if user-requested size is valid. */ |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 215 | if (vm_size > buffer->size) |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 216 | return -EINVAL; |
| 217 | |
| 218 | /* |
| 219 | * get page frame number to physical memory to be mapped |
| 220 | * to user space. |
| 221 | */ |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 222 | pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >> PAGE_SHIFT; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 223 | |
| 224 | DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn); |
| 225 | |
| 226 | if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size, |
| 227 | vma->vm_page_prot)) { |
| 228 | DRM_ERROR("failed to remap pfn range.\n"); |
| 229 | return -EAGAIN; |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static const struct file_operations exynos_drm_gem_fops = { |
| 236 | .mmap = exynos_drm_gem_mmap_buffer, |
| 237 | }; |
| 238 | |
| 239 | int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 240 | struct drm_file *file_priv) |
| 241 | { |
| 242 | struct drm_exynos_gem_mmap *args = data; |
| 243 | struct drm_gem_object *obj; |
| 244 | unsigned int addr; |
| 245 | |
| 246 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 247 | |
| 248 | if (!(dev->driver->driver_features & DRIVER_GEM)) { |
| 249 | DRM_ERROR("does not support GEM.\n"); |
| 250 | return -ENODEV; |
| 251 | } |
| 252 | |
| 253 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 254 | if (!obj) { |
| 255 | DRM_ERROR("failed to lookup gem object.\n"); |
| 256 | return -EINVAL; |
| 257 | } |
| 258 | |
| 259 | obj->filp->f_op = &exynos_drm_gem_fops; |
| 260 | obj->filp->private_data = obj; |
| 261 | |
| 262 | down_write(¤t->mm->mmap_sem); |
| 263 | addr = do_mmap(obj->filp, 0, args->size, |
| 264 | PROT_READ | PROT_WRITE, MAP_SHARED, 0); |
| 265 | up_write(¤t->mm->mmap_sem); |
| 266 | |
| 267 | drm_gem_object_unreference_unlocked(obj); |
| 268 | |
| 269 | if (IS_ERR((void *)addr)) |
| 270 | return PTR_ERR((void *)addr); |
| 271 | |
| 272 | args->mapped = addr; |
| 273 | |
| 274 | DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | int exynos_drm_gem_init_object(struct drm_gem_object *obj) |
| 280 | { |
| 281 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | void exynos_drm_gem_free_object(struct drm_gem_object *gem_obj) |
| 287 | { |
| 288 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 289 | |
| 290 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 291 | |
| 292 | DRM_DEBUG_KMS("handle count = %d\n", |
| 293 | atomic_read(&gem_obj->handle_count)); |
| 294 | |
| 295 | if (gem_obj->map_list.map) |
| 296 | drm_gem_free_mmap_offset(gem_obj); |
| 297 | |
| 298 | /* release file pointer to gem object. */ |
| 299 | drm_gem_object_release(gem_obj); |
| 300 | |
| 301 | exynos_gem_obj = to_exynos_gem_obj(gem_obj); |
| 302 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 303 | exynos_drm_buf_destroy(gem_obj->dev, exynos_gem_obj->buffer); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 304 | |
| 305 | kfree(exynos_gem_obj); |
| 306 | } |
| 307 | |
| 308 | int exynos_drm_gem_dumb_create(struct drm_file *file_priv, |
| 309 | struct drm_device *dev, struct drm_mode_create_dumb *args) |
| 310 | { |
| 311 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 312 | |
| 313 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 314 | |
| 315 | /* |
| 316 | * alocate memory to be used for framebuffer. |
| 317 | * - this callback would be called by user application |
| 318 | * with DRM_IOCTL_MODE_CREATE_DUMB command. |
| 319 | */ |
| 320 | |
| 321 | args->pitch = args->width * args->bpp >> 3; |
| 322 | args->size = args->pitch * args->height; |
| 323 | |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 324 | exynos_gem_obj = exynos_drm_gem_create(dev, file_priv, &args->handle, |
| 325 | args->size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 326 | if (IS_ERR(exynos_gem_obj)) |
| 327 | return PTR_ERR(exynos_gem_obj); |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv, |
| 333 | struct drm_device *dev, uint32_t handle, uint64_t *offset) |
| 334 | { |
| 335 | struct exynos_drm_gem_obj *exynos_gem_obj; |
| 336 | struct drm_gem_object *obj; |
| 337 | |
| 338 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 339 | |
| 340 | mutex_lock(&dev->struct_mutex); |
| 341 | |
| 342 | /* |
| 343 | * get offset of memory allocated for drm framebuffer. |
| 344 | * - this callback would be called by user application |
| 345 | * with DRM_IOCTL_MODE_MAP_DUMB command. |
| 346 | */ |
| 347 | |
| 348 | obj = drm_gem_object_lookup(dev, file_priv, handle); |
| 349 | if (!obj) { |
| 350 | DRM_ERROR("failed to lookup gem object.\n"); |
| 351 | mutex_unlock(&dev->struct_mutex); |
| 352 | return -EINVAL; |
| 353 | } |
| 354 | |
| 355 | exynos_gem_obj = to_exynos_gem_obj(obj); |
| 356 | |
| 357 | *offset = get_gem_mmap_offset(&exynos_gem_obj->base); |
| 358 | |
| 359 | drm_gem_object_unreference(obj); |
| 360 | |
| 361 | DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset); |
| 362 | |
| 363 | mutex_unlock(&dev->struct_mutex); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 369 | { |
| 370 | struct drm_gem_object *obj = vma->vm_private_data; |
| 371 | struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj); |
| 372 | struct drm_device *dev = obj->dev; |
| 373 | unsigned long pfn; |
| 374 | pgoff_t page_offset; |
| 375 | int ret; |
| 376 | |
| 377 | page_offset = ((unsigned long)vmf->virtual_address - |
| 378 | vma->vm_start) >> PAGE_SHIFT; |
| 379 | |
| 380 | mutex_lock(&dev->struct_mutex); |
| 381 | |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame^] | 382 | pfn = (((unsigned long)exynos_gem_obj->buffer->dma_addr) >> |
| 383 | PAGE_SHIFT) + page_offset; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 384 | |
| 385 | ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn); |
| 386 | |
| 387 | mutex_unlock(&dev->struct_mutex); |
| 388 | |
| 389 | return convert_to_vm_err_msg(ret); |
| 390 | } |
| 391 | |
| 392 | int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 393 | { |
| 394 | int ret; |
| 395 | |
| 396 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 397 | |
| 398 | /* set vm_area_struct. */ |
| 399 | ret = drm_gem_mmap(filp, vma); |
| 400 | if (ret < 0) { |
| 401 | DRM_ERROR("failed to mmap.\n"); |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | vma->vm_flags &= ~VM_PFNMAP; |
| 406 | vma->vm_flags |= VM_MIXEDMAP; |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | |
| 412 | int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv, |
| 413 | struct drm_device *dev, unsigned int handle) |
| 414 | { |
| 415 | int ret; |
| 416 | |
| 417 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 418 | |
| 419 | /* |
| 420 | * obj->refcount and obj->handle_count are decreased and |
| 421 | * if both them are 0 then exynos_drm_gem_free_object() |
| 422 | * would be called by callback to release resources. |
| 423 | */ |
| 424 | ret = drm_gem_handle_delete(file_priv, handle); |
| 425 | if (ret < 0) { |
| 426 | DRM_ERROR("failed to delete drm_gem_handle.\n"); |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>"); |
| 434 | MODULE_DESCRIPTION("Samsung SoC DRM GEM Module"); |
| 435 | MODULE_LICENSE("GPL"); |