xref: /freebsd/sys/dev/drm2/drm_mm.h (revision 90aac0d83bc9645f51ef0c2aeae6f9c0540bb031)
1 /**************************************************************************
2  *
3  * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  *
27  **************************************************************************/
28 /*
29  * Authors:
30  * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
31  */
32 
33 #include <sys/cdefs.h>
34 #ifndef _DRM_MM_H_
35 #define _DRM_MM_H_
36 
37 /*
38  * Generic range manager structs
39  */
40 #include <dev/drm2/drm_linux_list.h>
41 
42 struct drm_mm_node {
43 	struct list_head node_list;
44 	struct list_head hole_stack;
45 	unsigned hole_follows : 1;
46 	unsigned scanned_block : 1;
47 	unsigned scanned_prev_free : 1;
48 	unsigned scanned_next_free : 1;
49 	unsigned scanned_preceeds_hole : 1;
50 	unsigned allocated : 1;
51 	unsigned long color;
52 	unsigned long start;
53 	unsigned long size;
54 	struct drm_mm *mm;
55 };
56 
57 struct drm_mm {
58 	/* List of all memory nodes that immediately precede a free hole. */
59 	struct list_head hole_stack;
60 	/* head_node.node_list is the list of all memory nodes, ordered
61 	 * according to the (increasing) start address of the memory node. */
62 	struct drm_mm_node head_node;
63 	struct list_head unused_nodes;
64 	int num_unused;
65 	struct mtx unused_lock;
66 	unsigned int scan_check_range : 1;
67 	unsigned scan_alignment;
68 	unsigned long scan_color;
69 	unsigned long scan_size;
70 	unsigned long scan_hit_start;
71 	unsigned long scan_hit_end;
72 	unsigned scanned_blocks;
73 	unsigned long scan_start;
74 	unsigned long scan_end;
75 	struct drm_mm_node *prev_scanned_node;
76 
77 	void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
78 			     unsigned long *start, unsigned long *end);
79 };
80 
81 static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
82 {
83 	return node->allocated;
84 }
85 
86 static inline bool drm_mm_initialized(struct drm_mm *mm)
87 {
88 	return mm->hole_stack.next;
89 }
90 #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
91 						&(mm)->head_node.node_list, \
92 						node_list)
93 #define drm_mm_for_each_scanned_node_reverse(entry, n, mm) \
94 	for (entry = (mm)->prev_scanned_node, \
95 		next = entry ? list_entry(entry->node_list.next, \
96 			struct drm_mm_node, node_list) : NULL; \
97 	     entry != NULL; entry = next, \
98 		next = entry ? list_entry(entry->node_list.next, \
99 			struct drm_mm_node, node_list) : NULL) \
100 /*
101  * Basic range manager support (drm_mm.c)
102  */
103 extern struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
104 						    unsigned long size,
105 						    unsigned alignment,
106 						    unsigned long color,
107 						    int atomic);
108 extern struct drm_mm_node *drm_mm_get_block_range_generic(
109 						struct drm_mm_node *node,
110 						unsigned long size,
111 						unsigned alignment,
112 						unsigned long color,
113 						unsigned long start,
114 						unsigned long end,
115 						int atomic);
116 static inline struct drm_mm_node *drm_mm_get_block(struct drm_mm_node *parent,
117 						   unsigned long size,
118 						   unsigned alignment)
119 {
120 	return drm_mm_get_block_generic(parent, size, alignment, 0, 0);
121 }
122 static inline struct drm_mm_node *drm_mm_get_block_atomic(struct drm_mm_node *parent,
123 							  unsigned long size,
124 							  unsigned alignment)
125 {
126 	return drm_mm_get_block_generic(parent, size, alignment, 0, 1);
127 }
128 static inline struct drm_mm_node *drm_mm_get_block_range(
129 						struct drm_mm_node *parent,
130 						unsigned long size,
131 						unsigned alignment,
132 						unsigned long start,
133 						unsigned long end)
134 {
135 	return drm_mm_get_block_range_generic(parent, size, alignment, 0,
136 					      start, end, 0);
137 }
138 static inline struct drm_mm_node *drm_mm_get_color_block_range(
139 						struct drm_mm_node *parent,
140 						unsigned long size,
141 						unsigned alignment,
142 						unsigned long color,
143 						unsigned long start,
144 						unsigned long end)
145 {
146 	return drm_mm_get_block_range_generic(parent, size, alignment, color,
147 					      start, end, 0);
148 }
149 static inline struct drm_mm_node *drm_mm_get_block_atomic_range(
150 						struct drm_mm_node *parent,
151 						unsigned long size,
152 						unsigned alignment,
153 						unsigned long start,
154 						unsigned long end)
155 {
156 	return drm_mm_get_block_range_generic(parent, size, alignment, 0,
157 						start, end, 1);
158 }
159 
160 extern int drm_mm_insert_node(struct drm_mm *mm,
161 			      struct drm_mm_node *node,
162 			      unsigned long size,
163 			      unsigned alignment);
164 extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
165 				       struct drm_mm_node *node,
166 				       unsigned long size,
167 				       unsigned alignment,
168 				       unsigned long start,
169 				       unsigned long end);
170 extern int drm_mm_insert_node_generic(struct drm_mm *mm,
171 				      struct drm_mm_node *node,
172 				      unsigned long size,
173 				      unsigned alignment,
174 				      unsigned long color);
175 extern int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
176 				       struct drm_mm_node *node,
177 				       unsigned long size,
178 				       unsigned alignment,
179 				       unsigned long color,
180 				       unsigned long start,
181 				       unsigned long end);
182 extern void drm_mm_put_block(struct drm_mm_node *cur);
183 extern void drm_mm_remove_node(struct drm_mm_node *node);
184 extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
185 extern struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
186 						      unsigned long size,
187 						      unsigned alignment,
188 						      unsigned long color,
189 						      bool best_match);
190 extern struct drm_mm_node *drm_mm_search_free_in_range_generic(
191 						const struct drm_mm *mm,
192 						unsigned long size,
193 						unsigned alignment,
194 						unsigned long color,
195 						unsigned long start,
196 						unsigned long end,
197 						bool best_match);
198 static inline struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
199 						     unsigned long size,
200 						     unsigned alignment,
201 						     bool best_match)
202 {
203 	return drm_mm_search_free_generic(mm,size, alignment, 0, best_match);
204 }
205 static inline  struct drm_mm_node *drm_mm_search_free_in_range(
206 						const struct drm_mm *mm,
207 						unsigned long size,
208 						unsigned alignment,
209 						unsigned long start,
210 						unsigned long end,
211 						bool best_match)
212 {
213 	return drm_mm_search_free_in_range_generic(mm, size, alignment, 0,
214 						   start, end, best_match);
215 }
216 static inline struct drm_mm_node *drm_mm_search_free_color(const struct drm_mm *mm,
217 							   unsigned long size,
218 							   unsigned alignment,
219 							   unsigned long color,
220 							   bool best_match)
221 {
222 	return drm_mm_search_free_generic(mm,size, alignment, color, best_match);
223 }
224 static inline  struct drm_mm_node *drm_mm_search_free_in_range_color(
225 						const struct drm_mm *mm,
226 						unsigned long size,
227 						unsigned alignment,
228 						unsigned long color,
229 						unsigned long start,
230 						unsigned long end,
231 						bool best_match)
232 {
233 	return drm_mm_search_free_in_range_generic(mm, size, alignment, color,
234 						   start, end, best_match);
235 }
236 extern int drm_mm_init(struct drm_mm *mm,
237 		       unsigned long start,
238 		       unsigned long size);
239 extern void drm_mm_takedown(struct drm_mm *mm);
240 extern int drm_mm_clean(struct drm_mm *mm);
241 extern int drm_mm_pre_get(struct drm_mm *mm);
242 
243 static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
244 {
245 	return block->mm;
246 }
247 
248 void drm_mm_init_scan(struct drm_mm *mm,
249 		      unsigned long size,
250 		      unsigned alignment,
251 		      unsigned long color);
252 void drm_mm_init_scan_with_range(struct drm_mm *mm,
253 				 unsigned long size,
254 				 unsigned alignment,
255 				 unsigned long color,
256 				 unsigned long start,
257 				 unsigned long end);
258 int drm_mm_scan_add_block(struct drm_mm_node *node);
259 int drm_mm_scan_remove_block(struct drm_mm_node *node);
260 
261 extern void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
262 
263 #endif
264