1 /* 2 * include/linux/amlogic/meson_uvm_core.h 3 * 4 * Copyright (C) 2017 Amlogic, Inc. All rights reserved. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 * more details. 15 * 16 */ 17 18 #ifndef __MESON_UVM_H 19 #define __MESON_UVM_H 20 21 #include <linux/device.h> 22 #include <linux/dma-direction.h> 23 #include <linux/miscdevice.h> 24 #include <linux/kref.h> 25 #include <linux/mm_types.h> 26 #include <linux/mutex.h> 27 #include <linux/rbtree.h> 28 #include <linux/sched.h> 29 #include <linux/ioctl.h> 30 #include <linux/types.h> 31 32 #include <linux/amlogic/media/vfm/vframe.h> 33 34 #define MAX_BUFF_NUMS 4 35 #define UVM_FAKE_SIZE 4096 36 37 enum uvm_alloc_flag { 38 UVM_IMM_ALLOC, 39 UVM_DELAY_ALLOC, 40 UVM_FAKE_ALLOC, 41 UVM_SECURE_ALLOC, 42 }; 43 44 /** 45 * struct uvm_handle - video dmabuffer wrap vframe 46 * 47 * @ref: reference count 48 * @lock: protects the uvm_handle fields 49 * @uvm_alloc: buffer alloc info 50 * @dma_buf: dmabuf structure 51 * @alloc: non-alloc or allocated 52 * @size: dmabuf size 53 * @align: dmabuf align size 54 * @flags: alloc flag and usage flag 55 * @vf: builtin vframe 56 * @vfp: pointer the origin vframe 57 * @mod_attached: list of attached module 58 * @n_mod_attached: num of attached module 59 * @mod_attached_mask: mask of attached module 60 */ 61 struct uvm_handle { 62 struct kref ref; 63 struct mutex lock; 64 65 struct uvm_alloc *ua; 66 struct dma_buf *dmabuf; 67 bool alloc; 68 size_t size; 69 size_t align; 70 unsigned long flags; 71 72 struct vframe_s vf; 73 struct vframe_s *vfp; 74 75 struct list_head mod_attached; 76 size_t n_mod_attached; 77 unsigned long mod_attached_mask; 78 }; 79 80 /** 81 * struct uvm_buf_obj - uvm buffer object 82 * This structure define the uvm buffer object. uvm do not allocate buffer. 83 * device that allocate buffer, wrap this structure. 84 * 85 * @arg: uvm dmabuf exporter device priv structure 86 * @dev: uvm dmabuf exporter device 87 */ 88 struct uvm_buf_obj { 89 void *arg; 90 struct device *dev; 91 struct dma_buf *dmabuf; 92 }; 93 94 /** 95 * struct uvm_alloc - uvm buffer alloc struct 96 * 97 * @sgt: dmabuf sg_table 98 * @pages: dmabuf page array 99 * @vaddr: dmabuf kernel address 100 * @size: dmabuf real size 101 * @obj: uvm buffer object 102 * @free: free uvm_buf_obj related 103 */ 104 struct uvm_alloc { 105 struct sg_table *sgt; 106 struct page **pages; 107 void *vaddr; 108 size_t size; 109 uint64_t flags; 110 struct uvm_buf_obj *obj; 111 void (*free)(struct uvm_buf_obj *obj); 112 }; 113 114 struct uvm_alloc_info { 115 size_t size; 116 uint64_t flags; 117 struct sg_table *sgt; 118 struct uvm_buf_obj *obj; 119 void (*free)(struct uvm_buf_obj *obj); 120 }; 121 122 enum uvm_hook_mod_type { 123 VF_SRC_DECODER, 124 VF_SRC_VDIN, 125 VF_PROCESS_V4LVIDEO, 126 VF_PROCESS_DI, 127 VF_PROCESS_VIDEOCOMPOSER, 128 }; 129 130 /** 131 * struct uvm_hook_mod - uvm hook module 132 * 133 * @ref: reference count 134 * @type: module type 135 * @arg: module private data 136 * @free: module free function 137 * @acquire_fence: module acquire fence 138 * @head: module link node 139 */ 140 struct uvm_hook_mod { 141 struct kref ref; 142 enum uvm_hook_mod_type type; 143 void *arg; 144 void (*free)(void *arg); 145 struct sync_fence *acquire_fence; 146 struct list_head list; 147 }; 148 149 struct uvm_hook_mod_info { 150 enum uvm_hook_mod_type type; 151 void *arg; 152 void (*free)(void *arg); 153 struct sync_fence *acquire_fence; 154 }; 155 156 struct dma_buf *uvm_alloc_dmabuf(size_t len, size_t align, ulong flags); 157 158 bool dmabuf_is_uvm(struct dma_buf *dmabuf); 159 160 int dmabuf_bind_uvm_alloc(struct dma_buf *dmabuf, struct uvm_alloc_info *info); 161 162 /** 163 * uvm vframe interface 164 */ 165 int dmabuf_set_vframe(struct dma_buf *dmabuf, struct vframe_s *vf); 166 167 struct vframe_s *dmabuf_get_vframe(struct dma_buf *dmabuf); 168 169 int dmabuf_put_vframe(struct dma_buf *dmabuf); 170 171 /** 172 * uvm hook module interface 173 */ 174 int uvm_attach_hook_mod(struct dma_buf *dmabuf, 175 struct uvm_hook_mod_info *info); 176 int uvm_detach_hook_mod(struct dma_buf *dmabuf, 177 int type); 178 179 struct uvm_hook_mod *uvm_get_hook_mod(struct dma_buf *dmabuf, 180 int type); 181 182 int uvm_put_hook_mod(struct dma_buf *dmabuf, int type); 183 184 #endif 185