Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_gem.h |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Authoer: 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 | |
| 12 | #ifndef _EXYNOS_DRM_GEM_H_ |
| 13 | #define _EXYNOS_DRM_GEM_H_ |
| 14 | |
| 15 | #define to_exynos_gem_obj(x) container_of(x,\ |
| 16 | struct exynos_drm_gem_obj, base) |
| 17 | |
Inki Dae | dcf9af8 | 2012-04-03 21:27:58 +0900 | [diff] [blame] | 18 | #define IS_NONCONTIG_BUFFER(f) (f & EXYNOS_BO_NONCONTIG) |
| 19 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 20 | /* |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 21 | * exynos drm gem buffer structure. |
| 22 | * |
| 23 | * @kvaddr: kernel virtual address to allocated memory region. |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 24 | * *userptr: user space address. |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 25 | * @dma_addr: bus address(accessed by dma) to allocated memory region. |
| 26 | * - this address could be physical address without IOMMU and |
| 27 | * device address with IOMMU. |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 28 | * @write: whether pages will be written to by the caller. |
Inki Dae | 4744ad2 | 2012-12-07 17:51:27 +0900 | [diff] [blame] | 29 | * @pages: Array of backing pages. |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 30 | * @sgt: sg table to transfer page data. |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 31 | * @size: size of allocated memory region. |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 32 | * @pfnmap: indicate whether memory region from userptr is mmaped with |
| 33 | * VM_PFNMAP or not. |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 34 | */ |
| 35 | struct exynos_drm_gem_buf { |
| 36 | void __iomem *kvaddr; |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 37 | unsigned long userptr; |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 38 | dma_addr_t dma_addr; |
Inki Dae | 0519f9a | 2012-10-20 07:53:42 -0700 | [diff] [blame] | 39 | struct dma_attrs dma_attrs; |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 40 | unsigned int write; |
Inki Dae | 4744ad2 | 2012-12-07 17:51:27 +0900 | [diff] [blame] | 41 | struct page **pages; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 42 | struct sg_table *sgt; |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 43 | unsigned long size; |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 44 | bool pfnmap; |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 48 | * exynos drm buffer structure. |
| 49 | * |
| 50 | * @base: a gem object. |
| 51 | * - a new handle to this gem object would be created |
| 52 | * by drm_gem_handle_create(). |
Inki Dae | 2c87112 | 2011-11-12 15:23:32 +0900 | [diff] [blame] | 53 | * @buffer: a pointer to exynos_drm_gem_buffer object. |
| 54 | * - contain the information to memory region allocated |
| 55 | * by user request or at framebuffer creation. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 56 | * continuous memory region allocated by user request |
| 57 | * or at framebuffer creation. |
Inki Dae | 3c52b88 | 2012-06-29 16:28:17 +0900 | [diff] [blame] | 58 | * @size: size requested from user, in bytes and this size is aligned |
| 59 | * in page unit. |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 60 | * @vma: a pointer to vm_area. |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 61 | * @flags: indicate memory type to allocated buffer and cache attruibute. |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 62 | * |
| 63 | * P.S. this object would be transfered to user as kms_bo.handle so |
| 64 | * user can access the buffer through kms_bo.handle. |
| 65 | */ |
| 66 | struct exynos_drm_gem_obj { |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 67 | struct drm_gem_object base; |
| 68 | struct exynos_drm_gem_buf *buffer; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 69 | unsigned long size; |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 70 | struct vm_area_struct *vma; |
Inki Dae | 2b35892 | 2012-03-16 18:47:05 +0900 | [diff] [blame] | 71 | unsigned int flags; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 72 | }; |
| 73 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 74 | struct page **exynos_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask); |
| 75 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 76 | /* destroy a buffer with gem object */ |
| 77 | void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj); |
| 78 | |
Inki Dae | b2df26c | 2012-04-23 21:01:28 +0900 | [diff] [blame] | 79 | /* create a private gem object and initialize it. */ |
| 80 | struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev, |
| 81 | unsigned long size); |
| 82 | |
Joonyoung Shim | 2364839 | 2011-12-13 14:39:13 +0900 | [diff] [blame] | 83 | /* create a new buffer with gem object */ |
Inki Dae | f088d5a | 2011-11-12 14:51:23 +0900 | [diff] [blame] | 84 | 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] | 85 | unsigned int flags, |
| 86 | unsigned long size); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * request gem object creation and buffer allocation as the size |
| 90 | * that it is calculated with framebuffer information such as width, |
| 91 | * height and bpp. |
| 92 | */ |
| 93 | int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 94 | struct drm_file *file_priv); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 95 | |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 96 | /* |
| 97 | * get dma address from gem handle and this function could be used for |
| 98 | * other drivers such as 2d/3d acceleration drivers. |
| 99 | * with this function call, gem object reference count would be increased. |
| 100 | */ |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 101 | 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] | 102 | unsigned int gem_handle, |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 103 | struct drm_file *filp); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * put dma address from gem handle and this function could be used for |
| 107 | * other drivers such as 2d/3d acceleration drivers. |
| 108 | * with this function call, gem object reference count would be decreased. |
| 109 | */ |
| 110 | void exynos_drm_gem_put_dma_addr(struct drm_device *dev, |
| 111 | unsigned int gem_handle, |
Inki Dae | d87342c | 2012-11-03 21:53:24 -0700 | [diff] [blame] | 112 | struct drm_file *filp); |
Inki Dae | f0b1bda | 2012-03-16 18:47:06 +0900 | [diff] [blame] | 113 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 114 | /* get buffer offset to map to user space. */ |
| 115 | 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] | 116 | struct drm_file *file_priv); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 117 | |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 118 | /* |
| 119 | * mmap the physically continuous memory that a gem object contains |
| 120 | * to user space. |
| 121 | */ |
| 122 | int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 123 | struct drm_file *file_priv); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 124 | |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 125 | /* map user space allocated by malloc to pages. */ |
| 126 | int exynos_drm_gem_userptr_ioctl(struct drm_device *dev, void *data, |
| 127 | struct drm_file *file_priv); |
| 128 | |
Inki Dae | 40cd7e0 | 2012-05-04 15:51:17 +0900 | [diff] [blame] | 129 | /* get buffer information to memory region allocated by gem. */ |
| 130 | int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data, |
| 131 | struct drm_file *file_priv); |
| 132 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 133 | /* initialize gem object. */ |
| 134 | int exynos_drm_gem_init_object(struct drm_gem_object *obj); |
| 135 | |
| 136 | /* free gem object. */ |
| 137 | void exynos_drm_gem_free_object(struct drm_gem_object *gem_obj); |
| 138 | |
| 139 | /* create memory region for drm framebuffer. */ |
| 140 | int exynos_drm_gem_dumb_create(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 141 | struct drm_device *dev, |
| 142 | struct drm_mode_create_dumb *args); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 143 | |
| 144 | /* map memory region for drm framebuffer to user space. */ |
| 145 | int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 146 | struct drm_device *dev, uint32_t handle, |
| 147 | uint64_t *offset); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * destroy memory region allocated. |
| 151 | * - a gem handle and physical memory region pointed by a gem object |
| 152 | * would be released by drm_gem_handle_delete(). |
| 153 | */ |
| 154 | int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv, |
Joonyoung Shim | ee5e770 | 2011-12-13 14:20:23 +0900 | [diff] [blame] | 155 | struct drm_device *dev, |
| 156 | unsigned int handle); |
| 157 | |
| 158 | /* page fault handler and mmap fault address(virtual) to physical memory. */ |
| 159 | int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf); |
| 160 | |
| 161 | /* set vm_flags and we can change the vm attribute to other one at here. */ |
| 162 | int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 163 | |
Inki Dae | 2a3098f | 2012-11-04 05:48:52 -0800 | [diff] [blame] | 164 | static inline int vma_is_io(struct vm_area_struct *vma) |
| 165 | { |
| 166 | return !!(vma->vm_flags & (VM_IO | VM_PFNMAP)); |
| 167 | } |
| 168 | |
| 169 | /* get a copy of a virtual memory region. */ |
| 170 | struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma); |
| 171 | |
| 172 | /* release a userspace virtual memory area. */ |
| 173 | void exynos_gem_put_vma(struct vm_area_struct *vma); |
| 174 | |
| 175 | /* get pages from user space. */ |
| 176 | int exynos_gem_get_pages_from_userptr(unsigned long start, |
| 177 | unsigned int npages, |
| 178 | struct page **pages, |
| 179 | struct vm_area_struct *vma); |
| 180 | |
| 181 | /* drop the reference to pages. */ |
| 182 | void exynos_gem_put_pages_to_userptr(struct page **pages, |
| 183 | unsigned int npages, |
| 184 | struct vm_area_struct *vma); |
| 185 | |
| 186 | /* map sgt with dma region. */ |
| 187 | int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev, |
| 188 | struct sg_table *sgt, |
| 189 | enum dma_data_direction dir); |
| 190 | |
| 191 | /* unmap sgt from dma region. */ |
| 192 | void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev, |
| 193 | struct sg_table *sgt, |
| 194 | enum dma_data_direction dir); |
| 195 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 196 | #endif |