drm_mm.h revision 1.4.28.1 1 /* $NetBSD: drm_mm.h,v 1.4.28.1 2018/09/06 06:56:35 pgoyette 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 #include <linux/spinlock.h>
49
50 enum drm_mm_search_flags {
51 DRM_MM_SEARCH_DEFAULT = 0,
52 DRM_MM_SEARCH_BEST = 1 << 0,
53 DRM_MM_SEARCH_BELOW = 1 << 1,
54 };
55
56 enum drm_mm_allocator_flags {
57 DRM_MM_CREATE_DEFAULT = 0,
58 DRM_MM_CREATE_TOP = 1 << 0,
59 };
60
61 #define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT
62 #define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP
63
64 struct drm_mm_node {
65 struct list_head node_list;
66 struct list_head hole_stack;
67 unsigned hole_follows : 1;
68 unsigned scanned_block : 1;
69 unsigned scanned_prev_free : 1;
70 unsigned scanned_next_free : 1;
71 unsigned scanned_preceeds_hole : 1;
72 unsigned allocated : 1;
73 unsigned long color;
74 u64 start;
75 u64 size;
76 struct drm_mm *mm;
77 };
78
79 struct drm_mm {
80 /* List of all memory nodes that immediately precede a free hole. */
81 struct list_head hole_stack;
82 /* head_node.node_list is the list of all memory nodes, ordered
83 * according to the (increasing) start address of the memory node. */
84 struct drm_mm_node head_node;
85 unsigned int scan_check_range : 1;
86 unsigned scan_alignment;
87 unsigned long scan_color;
88 u64 scan_size;
89 u64 scan_hit_start;
90 u64 scan_hit_end;
91 unsigned scanned_blocks;
92 u64 scan_start;
93 u64 scan_end;
94 struct drm_mm_node *prev_scanned_node;
95
96 void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
97 u64 *start, u64 *end);
98 };
99
100 /**
101 * drm_mm_node_allocated - checks whether a node is allocated
102 * @node: drm_mm_node to check
103 *
104 * Drivers should use this helpers for proper encapusulation of drm_mm
105 * internals.
106 *
107 * Returns:
108 * True if the @node is allocated.
109 */
110 static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
111 {
112 return node->allocated;
113 }
114
115 /**
116 * drm_mm_initialized - checks whether an allocator is initialized
117 * @mm: drm_mm to check
118 *
119 * Drivers should use this helpers for proper encapusulation of drm_mm
120 * internals.
121 *
122 * Returns:
123 * True if the @mm is initialized.
124 */
125 static inline bool drm_mm_initialized(struct drm_mm *mm)
126 {
127 return mm->hole_stack.next;
128 }
129
130 static inline u64 __drm_mm_hole_node_start(struct drm_mm_node *hole_node)
131 {
132 return hole_node->start + hole_node->size;
133 }
134
135 /**
136 * drm_mm_hole_node_start - computes the start of the hole following @node
137 * @hole_node: drm_mm_node which implicitly tracks the following hole
138 *
139 * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
140 * inspect holes themselves. Drivers must check first whether a hole indeed
141 * follows by looking at node->hole_follows.
142 *
143 * Returns:
144 * Start of the subsequent hole.
145 */
146 static inline u64 drm_mm_hole_node_start(struct drm_mm_node *hole_node)
147 {
148 BUG_ON(!hole_node->hole_follows);
149 return __drm_mm_hole_node_start(hole_node);
150 }
151
152 static inline u64 __drm_mm_hole_node_end(struct drm_mm_node *hole_node)
153 {
154 return list_entry(hole_node->node_list.next,
155 struct drm_mm_node, node_list)->start;
156 }
157
158 /**
159 * drm_mm_hole_node_end - computes the end of the hole following @node
160 * @hole_node: drm_mm_node which implicitly tracks the following hole
161 *
162 * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
163 * inspect holes themselves. Drivers must check first whether a hole indeed
164 * follows by looking at node->hole_follows.
165 *
166 * Returns:
167 * End of the subsequent hole.
168 */
169 static inline u64 drm_mm_hole_node_end(struct drm_mm_node *hole_node)
170 {
171 return __drm_mm_hole_node_end(hole_node);
172 }
173
174 /**
175 * drm_mm_for_each_node - iterator to walk over all allocated nodes
176 * @entry: drm_mm_node structure to assign to in each iteration step
177 * @mm: drm_mm allocator to walk
178 *
179 * This iterator walks over all nodes in the range allocator. It is implemented
180 * with list_for_each, so not save against removal of elements.
181 */
182 #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
183 &(mm)->head_node.node_list, \
184 node_list)
185
186 /**
187 * drm_mm_for_each_hole - iterator to walk over all holes
188 * @entry: drm_mm_node used internally to track progress
189 * @mm: drm_mm allocator to walk
190 * @hole_start: ulong variable to assign the hole start to on each iteration
191 * @hole_end: ulong variable to assign the hole end to on each iteration
192 *
193 * This iterator walks over all holes in the range allocator. It is implemented
194 * with list_for_each, so not save against removal of elements. @entry is used
195 * internally and will not reflect a real drm_mm_node for the very first hole.
196 * Hence users of this iterator may not access it.
197 *
198 * Implementation Note:
199 * We need to inline list_for_each_entry in order to be able to set hole_start
200 * and hole_end on each iteration while keeping the macro sane.
201 *
202 * The __drm_mm_for_each_hole version is similar, but with added support for
203 * going backwards.
204 */
205 #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
206 for (entry = list_entry((mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
207 &entry->hole_stack != &(mm)->hole_stack ? \
208 hole_start = drm_mm_hole_node_start(entry), \
209 hole_end = drm_mm_hole_node_end(entry), \
210 1 : 0; \
211 entry = list_entry(entry->hole_stack.next, struct drm_mm_node, hole_stack))
212
213 #define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
214 for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
215 &entry->hole_stack != &(mm)->hole_stack ? \
216 hole_start = drm_mm_hole_node_start(entry), \
217 hole_end = drm_mm_hole_node_end(entry), \
218 1 : 0; \
219 entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
220
221 /*
222 * Basic range manager support (drm_mm.c)
223 */
224 int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
225
226 int drm_mm_insert_node_generic(struct drm_mm *mm,
227 struct drm_mm_node *node,
228 u64 size,
229 unsigned alignment,
230 unsigned long color,
231 enum drm_mm_search_flags sflags,
232 enum drm_mm_allocator_flags aflags);
233 /**
234 * drm_mm_insert_node - search for space and insert @node
235 * @mm: drm_mm to allocate from
236 * @node: preallocate node to insert
237 * @size: size of the allocation
238 * @alignment: alignment of the allocation
239 * @flags: flags to fine-tune the allocation
240 *
241 * This is a simplified version of drm_mm_insert_node_generic() with @color set
242 * to 0.
243 *
244 * The preallocated node must be cleared to 0.
245 *
246 * Returns:
247 * 0 on success, -ENOSPC if there's no suitable hole.
248 */
249 static inline int drm_mm_insert_node(struct drm_mm *mm,
250 struct drm_mm_node *node,
251 u64 size,
252 unsigned alignment,
253 enum drm_mm_search_flags flags)
254 {
255 return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags,
256 DRM_MM_CREATE_DEFAULT);
257 }
258
259 int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
260 struct drm_mm_node *node,
261 u64 size,
262 unsigned alignment,
263 unsigned long color,
264 u64 start,
265 u64 end,
266 enum drm_mm_search_flags sflags,
267 enum drm_mm_allocator_flags aflags);
268 /**
269 * drm_mm_insert_node_in_range - ranged search for space and insert @node
270 * @mm: drm_mm to allocate from
271 * @node: preallocate node to insert
272 * @size: size of the allocation
273 * @alignment: alignment of the allocation
274 * @start: start of the allowed range for this node
275 * @end: end of the allowed range for this node
276 * @flags: flags to fine-tune the allocation
277 *
278 * This is a simplified version of drm_mm_insert_node_in_range_generic() with
279 * @color set to 0.
280 *
281 * The preallocated node must be cleared to 0.
282 *
283 * Returns:
284 * 0 on success, -ENOSPC if there's no suitable hole.
285 */
286 static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
287 struct drm_mm_node *node,
288 u64 size,
289 unsigned alignment,
290 u64 start,
291 u64 end,
292 enum drm_mm_search_flags flags)
293 {
294 return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
295 0, start, end, flags,
296 DRM_MM_CREATE_DEFAULT);
297 }
298
299 void drm_mm_remove_node(struct drm_mm_node *node);
300 void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
301 void drm_mm_init(struct drm_mm *mm,
302 u64 start,
303 u64 size);
304 void drm_mm_takedown(struct drm_mm *mm);
305 bool drm_mm_clean(struct drm_mm *mm);
306
307 void drm_mm_init_scan(struct drm_mm *mm,
308 u64 size,
309 unsigned alignment,
310 unsigned long color);
311 void drm_mm_init_scan_with_range(struct drm_mm *mm,
312 u64 size,
313 unsigned alignment,
314 unsigned long color,
315 u64 start,
316 u64 end);
317 bool drm_mm_scan_add_block(struct drm_mm_node *node);
318 bool drm_mm_scan_remove_block(struct drm_mm_node *node);
319
320 void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
321 #ifdef CONFIG_DEBUG_FS
322 int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
323 #endif
324
325 #endif
326