xref: /freebsd/sys/dev/drm2/drm_mm.h (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * Authors:
34  * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
35  */
36 
37 #ifndef _DRM_MM_H_
38 #define _DRM_MM_H_
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 start;
52 	unsigned long size;
53 	struct drm_mm *mm;
54 	void *private;
55 };
56 
57 struct drm_mm {
58 	struct list_head hole_stack;
59 	struct drm_mm_node head_node;
60 	struct list_head unused_nodes;
61 	int num_unused;
62 	struct mtx unused_lock;
63 	unsigned int scan_check_range : 1;
64 	unsigned scan_alignment;
65 	unsigned long scan_size;
66 	unsigned long scan_hit_start;
67 	unsigned scan_hit_size;
68 	unsigned scanned_blocks;
69 	unsigned long scan_start;
70 	unsigned long scan_end;
71 	struct drm_mm_node *prev_scanned_node;
72 };
73 
74 static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
75 {
76 	return node->allocated;
77 }
78 
79 static inline bool drm_mm_initialized(struct drm_mm *mm)
80 {
81 	return (mm->hole_stack.next != NULL);
82 }
83 #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
84 						&(mm)->head_node.node_list, \
85 						node_list)
86 #define drm_mm_for_each_scanned_node_reverse(entry, n, mm) \
87 	for (entry = (mm)->prev_scanned_node, \
88 		next = entry ? list_entry(entry->node_list.next, \
89 			struct drm_mm_node, node_list) : NULL; \
90 	     entry != NULL; entry = next, \
91 		next = entry ? list_entry(entry->node_list.next, \
92 			struct drm_mm_node, node_list) : NULL)
93 
94 /*
95  * Basic range manager support (drm_mm.c)
96  */
97 extern struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
98 						    unsigned long size,
99 						    unsigned alignment,
100 						    int atomic);
101 extern struct drm_mm_node *drm_mm_get_block_range_generic(
102 						struct drm_mm_node *node,
103 						unsigned long size,
104 						unsigned alignment,
105 						unsigned long start,
106 						unsigned long end,
107 						int atomic);
108 static inline struct drm_mm_node *drm_mm_get_block(struct drm_mm_node *parent,
109 						   unsigned long size,
110 						   unsigned alignment)
111 {
112 	return drm_mm_get_block_generic(parent, size, alignment, 0);
113 }
114 static inline struct drm_mm_node *drm_mm_get_block_atomic(struct drm_mm_node *parent,
115 							  unsigned long size,
116 							  unsigned alignment)
117 {
118 	return drm_mm_get_block_generic(parent, size, alignment, 1);
119 }
120 static inline struct drm_mm_node *drm_mm_get_block_range(
121 						struct drm_mm_node *parent,
122 						unsigned long size,
123 						unsigned alignment,
124 						unsigned long start,
125 						unsigned long end)
126 {
127 	return drm_mm_get_block_range_generic(parent, size, alignment,
128 						start, end, 0);
129 }
130 static inline struct drm_mm_node *drm_mm_get_block_atomic_range(
131 						struct drm_mm_node *parent,
132 						unsigned long size,
133 						unsigned alignment,
134 						unsigned long start,
135 						unsigned long end)
136 {
137 	return drm_mm_get_block_range_generic(parent, size, alignment,
138 						start, end, 1);
139 }
140 extern int drm_mm_insert_node(struct drm_mm *mm, struct drm_mm_node *node,
141 			      unsigned long size, unsigned alignment);
142 extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
143 				       struct drm_mm_node *node,
144 				       unsigned long size, unsigned alignment,
145 				       unsigned long start, unsigned long end);
146 extern void drm_mm_put_block(struct drm_mm_node *cur);
147 extern void drm_mm_remove_node(struct drm_mm_node *node);
148 extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
149 extern struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
150 					      unsigned long size,
151 					      unsigned alignment,
152 					      int best_match);
153 extern struct drm_mm_node *drm_mm_search_free_in_range(
154 						const struct drm_mm *mm,
155 						unsigned long size,
156 						unsigned alignment,
157 						unsigned long start,
158 						unsigned long end,
159 						int best_match);
160 extern int drm_mm_init(struct drm_mm *mm, unsigned long start,
161 		       unsigned long size);
162 extern void drm_mm_takedown(struct drm_mm *mm);
163 extern int drm_mm_clean(struct drm_mm *mm);
164 extern unsigned long drm_mm_tail_space(struct drm_mm *mm);
165 extern int drm_mm_remove_space_from_tail(struct drm_mm *mm,
166 					 unsigned long size);
167 extern int drm_mm_add_space_to_tail(struct drm_mm *mm,
168 				    unsigned long size, int atomic);
169 extern int drm_mm_pre_get(struct drm_mm *mm);
170 
171 static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
172 {
173 	return block->mm;
174 }
175 
176 void drm_mm_init_scan(struct drm_mm *mm, unsigned long size,
177 		      unsigned alignment);
178 void drm_mm_init_scan_with_range(struct drm_mm *mm, unsigned long size,
179 				 unsigned alignment,
180 				 unsigned long start,
181 				 unsigned long end);
182 int drm_mm_scan_add_block(struct drm_mm_node *node);
183 int drm_mm_scan_remove_block(struct drm_mm_node *node);
184 
185 void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
186 
187 #endif
188