drm_vma_manager.h revision 1.5 1 /* $NetBSD: drm_vma_manager.h,v 1.5 2021/12/19 09:45:42 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _DRM_DRM_VMA_MANAGER_H_
33 #define _DRM_DRM_VMA_MANAGER_H_
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/rbtree.h>
38 #include <sys/rwlock.h>
39 #include <sys/vmem.h>
40
41 #include <linux/mm.h>
42
43 struct drm_file;
44
45 struct drm_vma_offset_manager {
46 krwlock_t vom_lock;
47 struct rb_tree vom_nodes;
48 struct vmem *vom_vmem;
49 };
50
51 struct drm_vma_offset_node {
52 krwlock_t von_lock;
53 vmem_addr_t von_startpage;
54 vmem_size_t von_npages;
55 struct rb_tree von_files;
56 struct rb_node von_rb_node;
57
58 /* Linux API names */
59 bool readonly;
60 };
61
62 static inline unsigned long
63 drm_vma_node_start(const struct drm_vma_offset_node *node)
64 {
65 return node->von_startpage;
66 }
67
68 static inline unsigned long
69 drm_vma_node_size(const struct drm_vma_offset_node *node)
70 {
71 return node->von_npages;
72 }
73
74 static inline bool
75 drm_vma_node_has_offset(const struct drm_vma_offset_node *node)
76 {
77 return (node->von_npages != 0);
78 }
79
80 static inline uint64_t
81 drm_vma_node_offset_addr(const struct drm_vma_offset_node *node)
82 {
83 return (uint64_t)node->von_startpage << PAGE_SHIFT;
84 }
85
86 struct drm_vma_offset_file {
87 struct drm_file *vof_file;
88 struct rb_node vof_rb_node;
89 };
90
91 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *,
92 unsigned long, unsigned long);
93 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *);
94
95 /*
96 * This is called drm_vma_node_reset upstream. Name is changed so you
97 * have to find calls and match them with drm_vma_node_destroy.
98 */
99 void drm_vma_node_init(struct drm_vma_offset_node *);
100
101 /* This does not exist upstream. */
102 void drm_vma_node_destroy(struct drm_vma_offset_node *);
103
104 int drm_vma_offset_add(struct drm_vma_offset_manager *,
105 struct drm_vma_offset_node *, unsigned long);
106 void drm_vma_offset_remove(struct drm_vma_offset_manager *,
107 struct drm_vma_offset_node *);
108
109 void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *);
110 void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *);
111 struct drm_vma_offset_node *
112 drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *,
113 unsigned long, unsigned long);
114 struct drm_vma_offset_node *
115 drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *,
116 unsigned long, unsigned long);
117
118 int drm_vma_node_allow(struct drm_vma_offset_node *, struct drm_file *);
119 void drm_vma_node_revoke(struct drm_vma_offset_node *, struct drm_file *);
120 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *,
121 struct drm_file *);
122 int drm_vma_node_verify_access(struct drm_vma_offset_node *,
123 struct drm_file *);
124
125 #endif /* _DRM_DRM_VMA_MANAGER_H_ */
126