1 /* $NetBSD: drm_vma_manager.h,v 1.1 2014/07/16 20:56:25 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 struct drm_vma_offset_manager {
42 krwlock_t vom_lock;
43 struct rb_tree vom_nodes;
44 struct vmem *vom_vmem;
45 };
46
47 struct drm_vma_offset_node {
48 krwlock_t von_lock;
49 vmem_addr_t von_startpage;
50 vmem_size_t von_npages;
51 struct rb_tree von_files;
52 struct rb_node von_rb_node;
53 };
54
55 static inline unsigned long
drm_vma_node_start(struct drm_vma_offset_node * node)56 drm_vma_node_start(struct drm_vma_offset_node *node)
57 {
58 return node->von_startpage;
59 }
60
61 static inline unsigned long
drm_vma_node_size(struct drm_vma_offset_node * node)62 drm_vma_node_size(struct drm_vma_offset_node *node)
63 {
64 return node->von_npages;
65 }
66
67 static inline bool
drm_vma_node_has_offset(struct drm_vma_offset_node * node)68 drm_vma_node_has_offset(struct drm_vma_offset_node *node)
69 {
70 return (node->von_npages != 0);
71 }
72
73 static inline uint64_t
drm_vma_node_offset_addr(struct drm_vma_offset_node * node)74 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
75 {
76 return (uint64_t)node->von_startpage << PAGE_SHIFT;
77 }
78
79 struct drm_vma_offset_file {
80 struct file *vof_file;
81 struct rb_node vof_rb_node;
82 };
83
84 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *,
85 unsigned long, unsigned long);
86 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *);
87
88 /*
89 * This is called drm_vma_node_reset upstream. Name is changed so you
90 * have to find calls and match them with drm_vma_node_destroy.
91 */
92 void drm_vma_node_init(struct drm_vma_offset_node *);
93
94 /* This does not exist upstream. */
95 void drm_vma_node_destroy(struct drm_vma_offset_node *);
96
97 int drm_vma_offset_add(struct drm_vma_offset_manager *,
98 struct drm_vma_offset_node *, unsigned long);
99 void drm_vma_offset_remove(struct drm_vma_offset_manager *,
100 struct drm_vma_offset_node *);
101
102 void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *);
103 void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *);
104 struct drm_vma_offset_node *
105 drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *,
106 unsigned long, unsigned long);
107 struct drm_vma_offset_node *
108 drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *,
109 unsigned long, unsigned long);
110
111 int drm_vma_node_allow(struct drm_vma_offset_node *, struct file *);
112 void drm_vma_node_revoke(struct drm_vma_offset_node *, struct file *);
113 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *, struct file *);
114 int drm_vma_node_verify_access(struct drm_vma_offset_node *,
115 struct file *);
116
117 #endif /* _DRM_DRM_VMA_MANAGER_H_ */
118