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