drm_mm.h revision 1.1.1.3 1 /* $NetBSD: drm_mm.h,v 1.1.1.3 2018/08/27 01:35:00 riastradh Exp $ */
2
3 /**************************************************************************
4 *
5 * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 *
29 **************************************************************************/
30 /*
31 * Authors:
32 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
33 */
34
35 #ifndef _DRM_MM_H_
36 #define _DRM_MM_H_
37
38 /*
39 * Generic range manager structs
40 */
41 #include <linux/bug.h>
42 #include <linux/kernel.h>
43 #include <linux/list.h>
44 #include <linux/spinlock.h>
45 #ifdef CONFIG_DEBUG_FS
46 #include <linux/seq_file.h>
47 #endif
48
49 enum drm_mm_search_flags {
50 DRM_MM_SEARCH_DEFAULT = 0,
51 DRM_MM_SEARCH_BEST = 1 << 0,
52 DRM_MM_SEARCH_BELOW = 1 << 1,
53 };
54
55 enum drm_mm_allocator_flags {
56 DRM_MM_CREATE_DEFAULT = 0,
57 DRM_MM_CREATE_TOP = 1 << 0,
58 };
59
60 #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT
61 #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP
62
63 struct drm_mm_node {
64 struct list_head node_list;
65 struct list_head hole_stack;
66 unsigned hole_follows : 1;
67 unsigned scanned_block : 1;
68 unsigned scanned_prev_free : 1;
69 unsigned scanned_next_free : 1;
70 unsigned scanned_preceeds_hole : 1;
71 unsigned allocated : 1;
72 unsigned long color;
73 u64 start;
74 u64 size;
75 struct drm_mm *mm;
76 };
77
78 struct drm_mm {
79 /* List of all memory nodes that immediately precede a free hole. */
80 struct list_head hole_stack;
81 /* head_node.node_list is the list of all memory nodes, ordered
82 * according to the (increasing) start address of the memory node. */
83 struct drm_mm_node head_node;
84 unsigned int scan_check_range : 1;
85 unsigned scan_alignment;
86 unsigned long scan_color;
87 u64 scan_size;
88 u64 scan_hit_start;
89 u64 scan_hit_end;
90 unsigned scanned_blocks;
91 u64 scan_start;
92 u64 scan_end;
93 struct drm_mm_node *prev_scanned_node;
94
95 void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
96 u64 *start, u64 *end);
97 };
98
99 /**
100 * drm_mm_node_allocated - checks whether a node is allocated
101 * @node: drm_mm_node to check
102 *
103 * Drivers should use this helpers for proper encapusulation of drm_mm
104 * internals.
105 *
106 * Returns:
107 * True if the @node is allocated.
108 */
109 static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
110 {
111 return node->allocated;
112 }
113
114 /**
115 * drm_mm_initialized - checks whether an allocator is initialized
116 * @mm: drm_mm to check
117 *
118 * Drivers should use this helpers for proper encapusulation of drm_mm
119 * internals.
120 *
121 * Returns:
122 * True if the @mm is initialized.
123 */
124 static inline bool drm_mm_initialized(struct drm_mm *mm)
125 {
126 return mm->hole_stack.next;
127 }
128
129 static inline u64 __drm_mm_hole_node_start(struct drm_mm_node *hole_node)
130 {
131 return hole_node->start + hole_node->size;
132 }
133
134 /**
135 * drm_mm_hole_node_start - computes the start of the hole following @node
136 * @hole_node: drm_mm_node which implicitly tracks the following hole
137 *
138 * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
139 * inspect holes themselves. Drivers must check first whether a hole indeed
140 * follows by looking at node->hole_follows.
141 *
142 * Returns:
143 * Start of the subsequent hole.
144 */
145 static inline u64 drm_mm_hole_node_start(struct drm_mm_node *hole_node)
146 {
147 BUG_ON(!hole_node->hole_follows);
148 return __drm_mm_hole_node_start(hole_node);
149 }
150
151 static inline u64 __drm_mm_hole_node_end(struct drm_mm_node *hole_node)
152 {
153 return list_entry(hole_node->node_list.next,
154 struct drm_mm_node, node_list)->start;
155 }
156
157 /**
158 * drm_mm_hole_node_end - computes the end of the hole following @node
159 * @hole_node: drm_mm_node which implicitly tracks the following hole
160 *
161 * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
162 * inspect holes themselves. Drivers must check first whether a hole indeed
163 * follows by looking at node->hole_follows.
164 *
165 * Returns:
166 * End of the subsequent hole.
167 */
168 static inline u64 drm_mm_hole_node_end(struct drm_mm_node *hole_node)
169 {
170 return __drm_mm_hole_node_end(hole_node);
171 }
172
173 /**
174 * drm_mm_for_each_node - iterator to walk over all allocated nodes
175 * @entry: drm_mm_node structure to assign to in each iteration step
176 * @mm: drm_mm allocator to walk
177 *
178 * This iterator walks over all nodes in the range allocator. It is implemented
179 * with list_for_each, so not save against removal of elements.
180 */
181 #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
182 &(mm)->head_node.node_list, \
183 node_list)
184
185 /**
186 * drm_mm_for_each_hole - iterator to walk over all holes
187 * @entry: drm_mm_node used internally to track progress
188 * @mm: drm_mm allocator to walk
189 * @hole_start: ulong variable to assign the hole start to on each iteration
190 * @hole_end: ulong variable to assign the hole end to on each iteration
191 *
192 * This iterator walks over all holes in the range allocator. It is implemented
193 * with list_for_each, so not save against removal of elements. @entry is used
194 * internally and will not reflect a real drm_mm_node for the very first hole.
195 * Hence users of this iterator may not access it.
196 *
197 * Implementation Note:
198 * We need to inline list_for_each_entry in order to be able to set hole_start
199 * and hole_end on each iteration while keeping the macro sane.
200 *
201 * The __drm_mm_for_each_hole version is similar, but with added support for
202 * going backwards.
203 */
204 #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
205 for (entry = list_entry((mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
206 &entry->hole_stack != &(mm)->hole_stack ? \
207 hole_start = drm_mm_hole_node_start(entry), \
208 hole_end = drm_mm_hole_node_end(entry), \
209 1 : 0; \
210 entry = list_entry(entry->hole_stack.next, struct drm_mm_node, hole_stack))
211
212 #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
213 for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
214 &entry->hole_stack != &(mm)->hole_stack ? \
215 hole_start = drm_mm_hole_node_start(entry), \
216 hole_end = drm_mm_hole_node_end(entry), \
217 1 : 0; \
218 entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
219
220 /*
221 * Basic range manager support (drm_mm.c)
222 */
223 int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
224
225 int drm_mm_insert_node_generic(struct drm_mm *mm,
226 struct drm_mm_node *node,
227 u64 size,
228 unsigned alignment,
229 unsigned long color,
230 enum drm_mm_search_flags sflags,
231 enum drm_mm_allocator_flags aflags);
232 /**
233 * drm_mm_insert_node - search for space and insert @node
234 * @mm: drm_mm to allocate from
235 * @node: preallocate node to insert
236 * @size: size of the allocation
237 * @alignment: alignment of the allocation
238 * @flags: flags to fine-tune the allocation
239 *
240 * This is a simplified version of drm_mm_insert_node_generic() with @color set
241 * to 0.
242 *
243 * The preallocated node must be cleared to 0.
244 *
245 * Returns:
246 * 0 on success, -ENOSPC if there's no suitable hole.
247 */
248 static inline int drm_mm_insert_node(struct drm_mm *mm,
249 struct drm_mm_node *node,
250 u64 size,
251 unsigned alignment,
252 enum drm_mm_search_flags flags)
253 {
254 return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags,
255 DRM_MM_CREATE_DEFAULT);
256 }
257
258 int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
259 struct drm_mm_node *node,
260 u64 size,
261 unsigned alignment,
262 unsigned long color,
263 u64 start,
264 u64 end,
265 enum drm_mm_search_flags sflags,
266 enum drm_mm_allocator_flags aflags);
267 /**
268 * drm_mm_insert_node_in_range - ranged search for space and insert @node
269 * @mm: drm_mm to allocate from
270 * @node: preallocate node to insert
271 * @size: size of the allocation
272 * @alignment: alignment of the allocation
273 * @start: start of the allowed range for this node
274 * @end: end of the allowed range for this node
275 * @flags: flags to fine-tune the allocation
276 *
277 * This is a simplified version of drm_mm_insert_node_in_range_generic() with
278 * @color set to 0.
279 *
280 * The preallocated node must be cleared to 0.
281 *
282 * Returns:
283 * 0 on success, -ENOSPC if there's no suitable hole.
284 */
285 static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
286 struct drm_mm_node *node,
287 u64 size,
288 unsigned alignment,
289 u64 start,
290 u64 end,
291 enum drm_mm_search_flags flags)
292 {
293 return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
294 0, start, end, flags,
295 DRM_MM_CREATE_DEFAULT);
296 }
297
298 void drm_mm_remove_node(struct drm_mm_node *node);
299 void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
300 void drm_mm_init(struct drm_mm *mm,
301 u64 start,
302 u64 size);
303 void drm_mm_takedown(struct drm_mm *mm);
304 bool drm_mm_clean(struct drm_mm *mm);
305
306 void drm_mm_init_scan(struct drm_mm *mm,
307 u64 size,
308 unsigned alignment,
309 unsigned long color);
310 void drm_mm_init_scan_with_range(struct drm_mm *mm,
311 u64 size,
312 unsigned alignment,
313 unsigned long color,
314 u64 start,
315 u64 end);
316 bool drm_mm_scan_add_block(struct drm_mm_node *node);
317 bool drm_mm_scan_remove_block(struct drm_mm_node *node);
318
319 void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
320 #ifdef CONFIG_DEBUG_FS
321 int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
322 #endif
323
324 #endif
325