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