xref: /netbsd-src/sys/external/bsd/drm2/dist/include/drm/drm_mm.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
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 #ifndef _DRM_MM_H_
34 #define _DRM_MM_H_
35 
36 /*
37  * Generic range manager structs
38  */
39 #include <linux/list.h>
40 #ifdef CONFIG_DEBUG_FS
41 #include <linux/seq_file.h>
42 #endif
43 #include <linux/spinlock.h>
44 
45 struct drm_mm_node {
46 	struct list_head node_list;
47 	struct list_head hole_stack;
48 	unsigned hole_follows : 1;
49 	unsigned scanned_block : 1;
50 	unsigned scanned_prev_free : 1;
51 	unsigned scanned_next_free : 1;
52 	unsigned scanned_preceeds_hole : 1;
53 	unsigned allocated : 1;
54 	unsigned long color;
55 	unsigned long start;
56 	unsigned long size;
57 	struct drm_mm *mm;
58 };
59 
60 struct drm_mm {
61 	/* List of all memory nodes that immediately precede a free hole. */
62 	struct list_head hole_stack;
63 	/* head_node.node_list is the list of all memory nodes, ordered
64 	 * according to the (increasing) start address of the memory node. */
65 	struct drm_mm_node head_node;
66 	struct list_head unused_nodes;
67 	int num_unused;
68 	spinlock_t unused_lock;
69 	unsigned int scan_check_range : 1;
70 	unsigned scan_alignment;
71 	unsigned long scan_color;
72 	unsigned long scan_size;
73 	unsigned long scan_hit_start;
74 	unsigned long scan_hit_end;
75 	unsigned scanned_blocks;
76 	unsigned long scan_start;
77 	unsigned long scan_end;
78 	struct drm_mm_node *prev_scanned_node;
79 
80 	void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
81 			     unsigned long *start, unsigned long *end);
82 };
83 
84 static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
85 {
86 	return node->allocated;
87 }
88 
89 static inline bool drm_mm_initialized(struct drm_mm *mm)
90 {
91 	return mm->hole_stack.next;
92 }
93 #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
94 						&(mm)->head_node.node_list, \
95 						node_list)
96 #define drm_mm_for_each_scanned_node_reverse(entry, n, mm) \
97 	for (entry = (mm)->prev_scanned_node, \
98 		next = entry ? list_entry(entry->node_list.next, \
99 			struct drm_mm_node, node_list) : NULL; \
100 	     entry != NULL; entry = next, \
101 		next = entry ? list_entry(entry->node_list.next, \
102 			struct drm_mm_node, node_list) : NULL) \
103 /*
104  * Basic range manager support (drm_mm.c)
105  */
106 extern struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
107 						    unsigned long size,
108 						    unsigned alignment,
109 						    unsigned long color,
110 						    int atomic);
111 extern struct drm_mm_node *drm_mm_get_block_range_generic(
112 						struct drm_mm_node *node,
113 						unsigned long size,
114 						unsigned alignment,
115 						unsigned long color,
116 						unsigned long start,
117 						unsigned long end,
118 						int atomic);
119 static inline struct drm_mm_node *drm_mm_get_block(struct drm_mm_node *parent,
120 						   unsigned long size,
121 						   unsigned alignment)
122 {
123 	return drm_mm_get_block_generic(parent, size, alignment, 0, 0);
124 }
125 static inline struct drm_mm_node *drm_mm_get_block_atomic(struct drm_mm_node *parent,
126 							  unsigned long size,
127 							  unsigned alignment)
128 {
129 	return drm_mm_get_block_generic(parent, size, alignment, 0, 1);
130 }
131 static inline struct drm_mm_node *drm_mm_get_block_range(
132 						struct drm_mm_node *parent,
133 						unsigned long size,
134 						unsigned alignment,
135 						unsigned long start,
136 						unsigned long end)
137 {
138 	return drm_mm_get_block_range_generic(parent, size, alignment, 0,
139 					      start, end, 0);
140 }
141 static inline struct drm_mm_node *drm_mm_get_color_block_range(
142 						struct drm_mm_node *parent,
143 						unsigned long size,
144 						unsigned alignment,
145 						unsigned long color,
146 						unsigned long start,
147 						unsigned long end)
148 {
149 	return drm_mm_get_block_range_generic(parent, size, alignment, color,
150 					      start, end, 0);
151 }
152 static inline struct drm_mm_node *drm_mm_get_block_atomic_range(
153 						struct drm_mm_node *parent,
154 						unsigned long size,
155 						unsigned alignment,
156 						unsigned long start,
157 						unsigned long end)
158 {
159 	return drm_mm_get_block_range_generic(parent, size, alignment, 0,
160 						start, end, 1);
161 }
162 
163 extern int drm_mm_insert_node(struct drm_mm *mm,
164 			      struct drm_mm_node *node,
165 			      unsigned long size,
166 			      unsigned alignment);
167 extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
168 				       struct drm_mm_node *node,
169 				       unsigned long size,
170 				       unsigned alignment,
171 				       unsigned long start,
172 				       unsigned long end);
173 extern int drm_mm_insert_node_generic(struct drm_mm *mm,
174 				      struct drm_mm_node *node,
175 				      unsigned long size,
176 				      unsigned alignment,
177 				      unsigned long color);
178 extern int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
179 				       struct drm_mm_node *node,
180 				       unsigned long size,
181 				       unsigned alignment,
182 				       unsigned long color,
183 				       unsigned long start,
184 				       unsigned long end);
185 extern void drm_mm_put_block(struct drm_mm_node *cur);
186 extern void drm_mm_remove_node(struct drm_mm_node *node);
187 extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
188 extern struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
189 						      unsigned long size,
190 						      unsigned alignment,
191 						      unsigned long color,
192 						      bool best_match);
193 extern struct drm_mm_node *drm_mm_search_free_in_range_generic(
194 						const struct drm_mm *mm,
195 						unsigned long size,
196 						unsigned alignment,
197 						unsigned long color,
198 						unsigned long start,
199 						unsigned long end,
200 						bool best_match);
201 static inline struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
202 						     unsigned long size,
203 						     unsigned alignment,
204 						     bool best_match)
205 {
206 	return drm_mm_search_free_generic(mm,size, alignment, 0, best_match);
207 }
208 static inline  struct drm_mm_node *drm_mm_search_free_in_range(
209 						const struct drm_mm *mm,
210 						unsigned long size,
211 						unsigned alignment,
212 						unsigned long start,
213 						unsigned long end,
214 						bool best_match)
215 {
216 	return drm_mm_search_free_in_range_generic(mm, size, alignment, 0,
217 						   start, end, best_match);
218 }
219 static inline struct drm_mm_node *drm_mm_search_free_color(const struct drm_mm *mm,
220 							   unsigned long size,
221 							   unsigned alignment,
222 							   unsigned long color,
223 							   bool best_match)
224 {
225 	return drm_mm_search_free_generic(mm,size, alignment, color, best_match);
226 }
227 static inline  struct drm_mm_node *drm_mm_search_free_in_range_color(
228 						const struct drm_mm *mm,
229 						unsigned long size,
230 						unsigned alignment,
231 						unsigned long color,
232 						unsigned long start,
233 						unsigned long end,
234 						bool best_match)
235 {
236 	return drm_mm_search_free_in_range_generic(mm, size, alignment, color,
237 						   start, end, best_match);
238 }
239 extern int drm_mm_init(struct drm_mm *mm,
240 		       unsigned long start,
241 		       unsigned long size);
242 extern void drm_mm_takedown(struct drm_mm *mm);
243 extern int drm_mm_clean(struct drm_mm *mm);
244 extern int drm_mm_pre_get(struct drm_mm *mm);
245 
246 static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
247 {
248 	return block->mm;
249 }
250 
251 void drm_mm_init_scan(struct drm_mm *mm,
252 		      unsigned long size,
253 		      unsigned alignment,
254 		      unsigned long color);
255 void drm_mm_init_scan_with_range(struct drm_mm *mm,
256 				 unsigned long size,
257 				 unsigned alignment,
258 				 unsigned long color,
259 				 unsigned long start,
260 				 unsigned long end);
261 int drm_mm_scan_add_block(struct drm_mm_node *node);
262 int drm_mm_scan_remove_block(struct drm_mm_node *node);
263 
264 extern void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
265 #ifdef CONFIG_DEBUG_FS
266 int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
267 #endif
268 
269 #endif
270