1 /* $NetBSD: drm_vma_manager.h,v 1.7 2021/12/19 11:57:27 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 #if BITS_PER_LONG == 64
44 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
45 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 256)
46 #else
47 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
48 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
49 #endif
50
51 struct drm_file;
52
53 struct drm_vma_offset_manager {
54 krwlock_t vom_lock;
55 struct rb_tree vom_nodes;
56 struct vmem *vom_vmem;
57 };
58
59 struct drm_vma_offset_node {
60 krwlock_t von_lock;
61 vmem_addr_t von_startpage;
62 vmem_size_t von_npages;
63 struct rb_tree von_files;
64 struct rb_node von_rb_node;
65
66 /* Linux API names */
67 bool readonly;
68 };
69
70 static inline unsigned long
drm_vma_node_start(const struct drm_vma_offset_node * node)71 drm_vma_node_start(const struct drm_vma_offset_node *node)
72 {
73 return node->von_startpage;
74 }
75
76 static inline unsigned long
drm_vma_node_size(const struct drm_vma_offset_node * node)77 drm_vma_node_size(const struct drm_vma_offset_node *node)
78 {
79 return node->von_npages;
80 }
81
82 static inline bool
drm_vma_node_has_offset(const struct drm_vma_offset_node * node)83 drm_vma_node_has_offset(const struct drm_vma_offset_node *node)
84 {
85 return (node->von_npages != 0);
86 }
87
88 static inline uint64_t
drm_vma_node_offset_addr(const struct drm_vma_offset_node * node)89 drm_vma_node_offset_addr(const struct drm_vma_offset_node *node)
90 {
91 return (uint64_t)node->von_startpage << PAGE_SHIFT;
92 }
93
94 struct drm_vma_offset_file {
95 struct drm_file *vof_file;
96 struct rb_node vof_rb_node;
97 };
98
99 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *,
100 unsigned long, unsigned long);
101 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *);
102
103 /*
104 * This is called drm_vma_node_reset upstream. Name is changed so you
105 * have to find calls and match them with drm_vma_node_destroy.
106 */
107 void drm_vma_node_init(struct drm_vma_offset_node *);
108
109 /* This does not exist upstream. */
110 void drm_vma_node_destroy(struct drm_vma_offset_node *);
111
112 int drm_vma_offset_add(struct drm_vma_offset_manager *,
113 struct drm_vma_offset_node *, unsigned long);
114 void drm_vma_offset_remove(struct drm_vma_offset_manager *,
115 struct drm_vma_offset_node *);
116
117 void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *);
118 void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *);
119 struct drm_vma_offset_node *
120 drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *,
121 unsigned long, unsigned long);
122 struct drm_vma_offset_node *
123 drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *,
124 unsigned long, unsigned long);
125 struct drm_vma_offset_node *
126 drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *,
127 unsigned long, unsigned long);
128
129 int drm_vma_node_allow(struct drm_vma_offset_node *, struct drm_file *);
130 void drm_vma_node_revoke(struct drm_vma_offset_node *, struct drm_file *);
131 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *,
132 struct drm_file *);
133 int drm_vma_node_verify_access(struct drm_vma_offset_node *,
134 struct drm_file *);
135
136 #endif /* _DRM_DRM_VMA_MANAGER_H_ */
137