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