1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Support for Medifield PNW Camera Imaging ISP subsystem.
4  *
5  * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
6  *
7  * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version
11  * 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  *
19  */
20 
21 #ifndef	__HMM_BO_H__
22 #define	__HMM_BO_H__
23 
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/list.h>
27 #include <linux/spinlock.h>
28 #include <linux/mutex.h>
29 #include "mmu/isp_mmu.h"
30 #include "hmm/hmm_common.h"
31 #include "ia_css_types.h"
32 
33 #define	check_bodev_null_return(bdev, exp)	\
34 		check_null_return(bdev, exp, \
35 			"NULL hmm_bo_device.\n")
36 
37 #define	check_bodev_null_return_void(bdev)	\
38 		check_null_return_void(bdev, \
39 			"NULL hmm_bo_device.\n")
40 
41 #define	check_bo_status_yes_goto(bo, _status, label) \
42 	var_not_equal_goto((bo->status & (_status)), (_status), \
43 			label, \
44 			"HMM buffer status not contain %s.\n", \
45 			#_status)
46 
47 #define	check_bo_status_no_goto(bo, _status, label) \
48 	var_equal_goto((bo->status & (_status)), (_status), \
49 			label, \
50 			"HMM buffer status contains %s.\n", \
51 			#_status)
52 
53 #define rbtree_node_to_hmm_bo(root_node)	\
54 	container_of((root_node), struct hmm_buffer_object, node)
55 
56 #define	list_to_hmm_bo(list_ptr)	\
57 	list_entry((list_ptr), struct hmm_buffer_object, list)
58 
59 #define	kref_to_hmm_bo(kref_ptr)	\
60 	list_entry((kref_ptr), struct hmm_buffer_object, kref)
61 
62 #define	check_bo_null_return(bo, exp)	\
63 	check_null_return(bo, exp, "NULL hmm buffer object.\n")
64 
65 #define	check_bo_null_return_void(bo)	\
66 	check_null_return_void(bo, "NULL hmm buffer object.\n")
67 
68 #define	HMM_MAX_ORDER		3
69 #define	HMM_MIN_ORDER		0
70 
71 #define	ISP_VM_START	0x0
72 #define	ISP_VM_SIZE	(0x7FFFFFFF)	/* 2G address space */
73 #define	ISP_PTR_NULL	NULL
74 
75 #define	HMM_BO_DEVICE_INITED	0x1
76 
77 enum hmm_bo_type {
78 	HMM_BO_PRIVATE,
79 	HMM_BO_USER,
80 	HMM_BO_LAST,
81 };
82 
83 #define	HMM_BO_MASK		0x1
84 #define	HMM_BO_FREE		0x0
85 #define	HMM_BO_ALLOCED	0x1
86 #define	HMM_BO_PAGE_ALLOCED	0x2
87 #define	HMM_BO_BINDED		0x4
88 #define	HMM_BO_MMAPED		0x8
89 #define	HMM_BO_VMAPED		0x10
90 #define	HMM_BO_VMAPED_CACHED	0x20
91 #define	HMM_BO_ACTIVE		0x1000
92 #define	HMM_BO_MEM_TYPE_USER     0x1
93 #define	HMM_BO_MEM_TYPE_PFN      0x2
94 
95 struct hmm_bo_device {
96 	struct isp_mmu		mmu;
97 
98 	/* start/pgnr/size is used to record the virtual memory of this bo */
99 	unsigned int start;
100 	unsigned int pgnr;
101 	unsigned int size;
102 
103 	/* list lock is used to protect the entire_bo_list */
104 	spinlock_t	list_lock;
105 	int flag;
106 
107 	/* linked list for entire buffer object */
108 	struct list_head entire_bo_list;
109 	/* rbtree for maintain entire allocated vm */
110 	struct rb_root allocated_rbtree;
111 	/* rbtree for maintain entire free vm */
112 	struct rb_root free_rbtree;
113 	struct mutex rbtree_mutex;
114 	struct kmem_cache *bo_cache;
115 };
116 
117 struct hmm_buffer_object {
118 	struct hmm_bo_device	*bdev;
119 	struct list_head	list;
120 	struct kref	kref;
121 
122 	struct page **pages;
123 
124 	/* mutex protecting this BO */
125 	struct mutex		mutex;
126 	enum hmm_bo_type	type;
127 	int		mmap_count;
128 	int		status;
129 	int		mem_type;
130 	void		*vmap_addr; /* kernel virtual address by vmap */
131 
132 	struct rb_node	node;
133 	unsigned int	start;
134 	unsigned int	end;
135 	unsigned int	pgnr;
136 	/*
137 	 * When insert a bo which has the same pgnr with an existed
138 	 * bo node in the free_rbtree, using "prev & next" pointer
139 	 * to maintain a bo linked list instead of insert this bo
140 	 * into free_rbtree directly, it will make sure each node
141 	 * in free_rbtree has different pgnr.
142 	 * "prev & next" default is NULL.
143 	 */
144 	struct hmm_buffer_object	*prev;
145 	struct hmm_buffer_object	*next;
146 };
147 
148 struct hmm_buffer_object *hmm_bo_alloc(struct hmm_bo_device *bdev,
149 				       unsigned int pgnr);
150 
151 void hmm_bo_release(struct hmm_buffer_object *bo);
152 
153 int hmm_bo_device_init(struct hmm_bo_device *bdev,
154 		       struct isp_mmu_client *mmu_driver,
155 		       unsigned int vaddr_start, unsigned int size);
156 
157 /*
158  * clean up all hmm_bo_device related things.
159  */
160 void hmm_bo_device_exit(struct hmm_bo_device *bdev);
161 
162 /*
163  * whether the bo device is inited or not.
164  */
165 int hmm_bo_device_inited(struct hmm_bo_device *bdev);
166 
167 /*
168  * increse buffer object reference.
169  */
170 void hmm_bo_ref(struct hmm_buffer_object *bo);
171 
172 /*
173  * decrese buffer object reference. if reference reaches 0,
174  * release function of the buffer object will be called.
175  *
176  * this call is also used to release hmm_buffer_object or its
177  * upper level object with it embedded in. you need to call
178  * this function when it is no longer used.
179  *
180  * Note:
181  *
182  * user dont need to care about internal resource release of
183  * the buffer object in the release callback, it will be
184  * handled internally.
185  *
186  * this call will only release internal resource of the buffer
187  * object but will not free the buffer object itself, as the
188  * buffer object can be both pre-allocated statically or
189  * dynamically allocated. so user need to deal with the release
190  * of the buffer object itself manually. below example shows
191  * the normal case of using the buffer object.
192  *
193  *	struct hmm_buffer_object *bo = hmm_bo_create(bdev, pgnr);
194  *	......
195  *	hmm_bo_unref(bo);
196  *
197  * or:
198  *
199  *	struct hmm_buffer_object bo;
200  *
201  *	hmm_bo_init(bdev, &bo, pgnr, NULL);
202  *	...
203  *	hmm_bo_unref(&bo);
204  */
205 void hmm_bo_unref(struct hmm_buffer_object *bo);
206 
207 int hmm_bo_allocated(struct hmm_buffer_object *bo);
208 
209 /*
210  * Allocate/Free physical pages for the bo. Type indicates if the
211  * pages will be allocated by using video driver (for share buffer)
212  * or by ISP driver itself.
213  */
214 int hmm_bo_alloc_pages(struct hmm_buffer_object *bo,
215 		       enum hmm_bo_type type,
216 		       const void __user *userptr);
217 void hmm_bo_free_pages(struct hmm_buffer_object *bo);
218 int hmm_bo_page_allocated(struct hmm_buffer_object *bo);
219 
220 /*
221  * bind/unbind the physical pages to a virtual address space.
222  */
223 int hmm_bo_bind(struct hmm_buffer_object *bo);
224 void hmm_bo_unbind(struct hmm_buffer_object *bo);
225 int hmm_bo_binded(struct hmm_buffer_object *bo);
226 
227 /*
228  * vmap buffer object's pages to contiguous kernel virtual address.
229  * if the buffer has been vmaped, return the virtual address directly.
230  */
231 void *hmm_bo_vmap(struct hmm_buffer_object *bo, bool cached);
232 
233 /*
234  * flush the cache for the vmapped buffer object's pages,
235  * if the buffer has not been vmapped, return directly.
236  */
237 void hmm_bo_flush_vmap(struct hmm_buffer_object *bo);
238 
239 /*
240  * vunmap buffer object's kernel virtual address.
241  */
242 void hmm_bo_vunmap(struct hmm_buffer_object *bo);
243 
244 /*
245  * mmap the bo's physical pages to specific vma.
246  *
247  * vma's address space size must be the same as bo's size,
248  * otherwise it will return -EINVAL.
249  *
250  * vma->vm_flags will be set to (VM_RESERVED | VM_IO).
251  */
252 int hmm_bo_mmap(struct vm_area_struct *vma,
253 		struct hmm_buffer_object *bo);
254 
255 /*
256  * find the buffer object by its virtual address vaddr.
257  * return NULL if no such buffer object found.
258  */
259 struct hmm_buffer_object *hmm_bo_device_search_start(
260     struct hmm_bo_device *bdev, ia_css_ptr vaddr);
261 
262 /*
263  * find the buffer object by its virtual address.
264  * it does not need to be the start address of one bo,
265  * it can be an address within the range of one bo.
266  * return NULL if no such buffer object found.
267  */
268 struct hmm_buffer_object *hmm_bo_device_search_in_range(
269     struct hmm_bo_device *bdev, ia_css_ptr vaddr);
270 
271 /*
272  * find the buffer object with kernel virtual address vaddr.
273  * return NULL if no such buffer object found.
274  */
275 struct hmm_buffer_object *hmm_bo_device_search_vmap_start(
276     struct hmm_bo_device *bdev, const void *vaddr);
277 
278 #endif
279