xref: /OK3568_Linux_fs/kernel/include/drm/drm_vma_manager.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun #ifndef __DRM_VMA_MANAGER_H__
2*4882a593Smuzhiyun #define __DRM_VMA_MANAGER_H__
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun /*
5*4882a593Smuzhiyun  * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
8*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
9*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
10*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
12*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be included in
15*4882a593Smuzhiyun  * all copies or substantial portions of the Software.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20*4882a593Smuzhiyun  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21*4882a593Smuzhiyun  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22*4882a593Smuzhiyun  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23*4882a593Smuzhiyun  * OTHER DEALINGS IN THE SOFTWARE.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <drm/drm_mm.h>
27*4882a593Smuzhiyun #include <linux/mm.h>
28*4882a593Smuzhiyun #include <linux/rbtree.h>
29*4882a593Smuzhiyun #include <linux/spinlock.h>
30*4882a593Smuzhiyun #include <linux/types.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* We make up offsets for buffer objects so we can recognize them at
33*4882a593Smuzhiyun  * mmap time. pgoff in mmap is an unsigned long, so we need to make sure
34*4882a593Smuzhiyun  * that the faked up offset will fit
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun #if BITS_PER_LONG == 64
37*4882a593Smuzhiyun #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
38*4882a593Smuzhiyun #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 256)
39*4882a593Smuzhiyun #else
40*4882a593Smuzhiyun #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
41*4882a593Smuzhiyun #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun struct drm_file;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun struct drm_vma_offset_file {
47*4882a593Smuzhiyun 	struct rb_node vm_rb;
48*4882a593Smuzhiyun 	struct drm_file *vm_tag;
49*4882a593Smuzhiyun 	unsigned long vm_count;
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun struct drm_vma_offset_node {
53*4882a593Smuzhiyun 	rwlock_t vm_lock;
54*4882a593Smuzhiyun 	struct drm_mm_node vm_node;
55*4882a593Smuzhiyun 	struct rb_root vm_files;
56*4882a593Smuzhiyun 	bool readonly:1;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun struct drm_vma_offset_manager {
60*4882a593Smuzhiyun 	rwlock_t vm_lock;
61*4882a593Smuzhiyun 	struct drm_mm vm_addr_space_mm;
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
65*4882a593Smuzhiyun 				 unsigned long page_offset, unsigned long size);
66*4882a593Smuzhiyun void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
69*4882a593Smuzhiyun 							   unsigned long start,
70*4882a593Smuzhiyun 							   unsigned long pages);
71*4882a593Smuzhiyun int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
72*4882a593Smuzhiyun 		       struct drm_vma_offset_node *node, unsigned long pages);
73*4882a593Smuzhiyun void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
74*4882a593Smuzhiyun 			   struct drm_vma_offset_node *node);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun int drm_vma_node_allow(struct drm_vma_offset_node *node, struct drm_file *tag);
77*4882a593Smuzhiyun void drm_vma_node_revoke(struct drm_vma_offset_node *node,
78*4882a593Smuzhiyun 			 struct drm_file *tag);
79*4882a593Smuzhiyun bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
80*4882a593Smuzhiyun 			     struct drm_file *tag);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /**
83*4882a593Smuzhiyun  * drm_vma_offset_exact_lookup_locked() - Look up node by exact address
84*4882a593Smuzhiyun  * @mgr: Manager object
85*4882a593Smuzhiyun  * @start: Start address (page-based, not byte-based)
86*4882a593Smuzhiyun  * @pages: Size of object (page-based)
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * Same as drm_vma_offset_lookup_locked() but does not allow any offset into the node.
89*4882a593Smuzhiyun  * It only returns the exact object with the given start address.
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * RETURNS:
92*4882a593Smuzhiyun  * Node at exact start address @start.
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun static inline struct drm_vma_offset_node *
drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager * mgr,unsigned long start,unsigned long pages)95*4882a593Smuzhiyun drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *mgr,
96*4882a593Smuzhiyun 				   unsigned long start,
97*4882a593Smuzhiyun 				   unsigned long pages)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct drm_vma_offset_node *node;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	node = drm_vma_offset_lookup_locked(mgr, start, pages);
102*4882a593Smuzhiyun 	return (node && node->vm_node.start == start) ? node : NULL;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /**
106*4882a593Smuzhiyun  * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
107*4882a593Smuzhiyun  * @mgr: Manager object
108*4882a593Smuzhiyun  *
109*4882a593Smuzhiyun  * Lock VMA manager for extended lookups. Only locked VMA function calls
110*4882a593Smuzhiyun  * are allowed while holding this lock. All other contexts are blocked from VMA
111*4882a593Smuzhiyun  * until the lock is released via drm_vma_offset_unlock_lookup().
112*4882a593Smuzhiyun  *
113*4882a593Smuzhiyun  * Use this if you need to take a reference to the objects returned by
114*4882a593Smuzhiyun  * drm_vma_offset_lookup_locked() before releasing this lock again.
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * This lock must not be used for anything else than extended lookups. You must
117*4882a593Smuzhiyun  * not call any other VMA helpers while holding this lock.
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * Note: You're in atomic-context while holding this lock!
120*4882a593Smuzhiyun  */
drm_vma_offset_lock_lookup(struct drm_vma_offset_manager * mgr)121*4882a593Smuzhiyun static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	read_lock(&mgr->vm_lock);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun  * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
128*4882a593Smuzhiyun  * @mgr: Manager object
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
131*4882a593Smuzhiyun  */
drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager * mgr)132*4882a593Smuzhiyun static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	read_unlock(&mgr->vm_lock);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun  * drm_vma_node_reset() - Initialize or reset node object
139*4882a593Smuzhiyun  * @node: Node to initialize or reset
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  * Reset a node to its initial state. This must be called before using it with
142*4882a593Smuzhiyun  * any VMA offset manager.
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * This must not be called on an already allocated node, or you will leak
145*4882a593Smuzhiyun  * memory.
146*4882a593Smuzhiyun  */
drm_vma_node_reset(struct drm_vma_offset_node * node)147*4882a593Smuzhiyun static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	memset(node, 0, sizeof(*node));
150*4882a593Smuzhiyun 	node->vm_files = RB_ROOT;
151*4882a593Smuzhiyun 	rwlock_init(&node->vm_lock);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /**
155*4882a593Smuzhiyun  * drm_vma_node_start() - Return start address for page-based addressing
156*4882a593Smuzhiyun  * @node: Node to inspect
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  * Return the start address of the given node. This can be used as offset into
159*4882a593Smuzhiyun  * the linear VM space that is provided by the VMA offset manager. Note that
160*4882a593Smuzhiyun  * this can only be used for page-based addressing. If you need a proper offset
161*4882a593Smuzhiyun  * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
162*4882a593Smuzhiyun  * drm_vma_node_offset_addr() helper instead.
163*4882a593Smuzhiyun  *
164*4882a593Smuzhiyun  * RETURNS:
165*4882a593Smuzhiyun  * Start address of @node for page-based addressing. 0 if the node does not
166*4882a593Smuzhiyun  * have an offset allocated.
167*4882a593Smuzhiyun  */
drm_vma_node_start(const struct drm_vma_offset_node * node)168*4882a593Smuzhiyun static inline unsigned long drm_vma_node_start(const struct drm_vma_offset_node *node)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	return node->vm_node.start;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  * drm_vma_node_size() - Return size (page-based)
175*4882a593Smuzhiyun  * @node: Node to inspect
176*4882a593Smuzhiyun  *
177*4882a593Smuzhiyun  * Return the size as number of pages for the given node. This is the same size
178*4882a593Smuzhiyun  * that was passed to drm_vma_offset_add(). If no offset is allocated for the
179*4882a593Smuzhiyun  * node, this is 0.
180*4882a593Smuzhiyun  *
181*4882a593Smuzhiyun  * RETURNS:
182*4882a593Smuzhiyun  * Size of @node as number of pages. 0 if the node does not have an offset
183*4882a593Smuzhiyun  * allocated.
184*4882a593Smuzhiyun  */
drm_vma_node_size(struct drm_vma_offset_node * node)185*4882a593Smuzhiyun static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	return node->vm_node.size;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun  * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
192*4882a593Smuzhiyun  * @node: Linked offset node
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * Same as drm_vma_node_start() but returns the address as a valid offset that
195*4882a593Smuzhiyun  * can be used for user-space mappings during mmap().
196*4882a593Smuzhiyun  * This must not be called on unlinked nodes.
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * RETURNS:
199*4882a593Smuzhiyun  * Offset of @node for byte-based addressing. 0 if the node does not have an
200*4882a593Smuzhiyun  * object allocated.
201*4882a593Smuzhiyun  */
drm_vma_node_offset_addr(struct drm_vma_offset_node * node)202*4882a593Smuzhiyun static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	return ((__u64)node->vm_node.start) << PAGE_SHIFT;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /**
208*4882a593Smuzhiyun  * drm_vma_node_unmap() - Unmap offset node
209*4882a593Smuzhiyun  * @node: Offset node
210*4882a593Smuzhiyun  * @file_mapping: Address space to unmap @node from
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * Unmap all userspace mappings for a given offset node. The mappings must be
213*4882a593Smuzhiyun  * associated with the @file_mapping address-space. If no offset exists
214*4882a593Smuzhiyun  * nothing is done.
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
217*4882a593Smuzhiyun  * is not called on this node concurrently.
218*4882a593Smuzhiyun  */
drm_vma_node_unmap(struct drm_vma_offset_node * node,struct address_space * file_mapping)219*4882a593Smuzhiyun static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
220*4882a593Smuzhiyun 				      struct address_space *file_mapping)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	if (drm_mm_node_allocated(&node->vm_node))
223*4882a593Smuzhiyun 		unmap_mapping_range(file_mapping,
224*4882a593Smuzhiyun 				    drm_vma_node_offset_addr(node),
225*4882a593Smuzhiyun 				    drm_vma_node_size(node) << PAGE_SHIFT, 1);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun /**
229*4882a593Smuzhiyun  * drm_vma_node_verify_access() - Access verification helper for TTM
230*4882a593Smuzhiyun  * @node: Offset node
231*4882a593Smuzhiyun  * @tag: Tag of file to check
232*4882a593Smuzhiyun  *
233*4882a593Smuzhiyun  * This checks whether @tag is granted access to @node. It is the same as
234*4882a593Smuzhiyun  * drm_vma_node_is_allowed() but suitable as drop-in helper for TTM
235*4882a593Smuzhiyun  * verify_access() callbacks.
236*4882a593Smuzhiyun  *
237*4882a593Smuzhiyun  * RETURNS:
238*4882a593Smuzhiyun  * 0 if access is granted, -EACCES otherwise.
239*4882a593Smuzhiyun  */
drm_vma_node_verify_access(struct drm_vma_offset_node * node,struct drm_file * tag)240*4882a593Smuzhiyun static inline int drm_vma_node_verify_access(struct drm_vma_offset_node *node,
241*4882a593Smuzhiyun 					     struct drm_file *tag)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	return drm_vma_node_is_allowed(node, tag) ? 0 : -EACCES;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun #endif /* __DRM_VMA_MANAGER_H__ */
247