xref: /openbmc/linux/include/drm/drm_mm.h (revision 86e81f0e)
1249d6048SJerome Glisse /**************************************************************************
2249d6048SJerome Glisse  *
3249d6048SJerome Glisse  * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
4249d6048SJerome Glisse  * All Rights Reserved.
5249d6048SJerome Glisse  *
6249d6048SJerome Glisse  * Permission is hereby granted, free of charge, to any person obtaining a
7249d6048SJerome Glisse  * copy of this software and associated documentation files (the
8249d6048SJerome Glisse  * "Software"), to deal in the Software without restriction, including
9249d6048SJerome Glisse  * without limitation the rights to use, copy, modify, merge, publish,
10249d6048SJerome Glisse  * distribute, sub license, and/or sell copies of the Software, and to
11249d6048SJerome Glisse  * permit persons to whom the Software is furnished to do so, subject to
12249d6048SJerome Glisse  * the following conditions:
13249d6048SJerome Glisse  *
14249d6048SJerome Glisse  * The above copyright notice and this permission notice (including the
15249d6048SJerome Glisse  * next paragraph) shall be included in all copies or substantial portions
16249d6048SJerome Glisse  * of the Software.
17249d6048SJerome Glisse  *
18249d6048SJerome Glisse  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19249d6048SJerome Glisse  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20249d6048SJerome Glisse  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21249d6048SJerome Glisse  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22249d6048SJerome Glisse  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23249d6048SJerome Glisse  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24249d6048SJerome Glisse  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25249d6048SJerome Glisse  *
26249d6048SJerome Glisse  *
27249d6048SJerome Glisse  **************************************************************************/
28249d6048SJerome Glisse /*
29249d6048SJerome Glisse  * Authors:
30249d6048SJerome Glisse  * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
31249d6048SJerome Glisse  */
32249d6048SJerome Glisse 
33249d6048SJerome Glisse #ifndef _DRM_MM_H_
34249d6048SJerome Glisse #define _DRM_MM_H_
35249d6048SJerome Glisse 
36249d6048SJerome Glisse /*
37249d6048SJerome Glisse  * Generic range manager structs
38249d6048SJerome Glisse  */
3986e81f0eSDavid Herrmann #include <linux/bug.h>
4086e81f0eSDavid Herrmann #include <linux/kernel.h>
41249d6048SJerome Glisse #include <linux/list.h>
4286e81f0eSDavid Herrmann #include <linux/spinlock.h>
43f1938cd6SDave Airlie #ifdef CONFIG_DEBUG_FS
44f1938cd6SDave Airlie #include <linux/seq_file.h>
45f1938cd6SDave Airlie #endif
46249d6048SJerome Glisse 
47249d6048SJerome Glisse struct drm_mm_node {
48d1024ce9SDaniel Vetter 	struct list_head node_list;
49ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
50ea7b1dd4SDaniel Vetter 	unsigned hole_follows : 1;
51709ea971SDaniel Vetter 	unsigned scanned_block : 1;
52709ea971SDaniel Vetter 	unsigned scanned_prev_free : 1;
53709ea971SDaniel Vetter 	unsigned scanned_next_free : 1;
54ea7b1dd4SDaniel Vetter 	unsigned scanned_preceeds_hole : 1;
55b0b7af18SDaniel Vetter 	unsigned allocated : 1;
566b9d89b4SChris Wilson 	unsigned long color;
57249d6048SJerome Glisse 	unsigned long start;
58249d6048SJerome Glisse 	unsigned long size;
59249d6048SJerome Glisse 	struct drm_mm *mm;
60249d6048SJerome Glisse };
61249d6048SJerome Glisse 
62249d6048SJerome Glisse struct drm_mm {
6325985edcSLucas De Marchi 	/* List of all memory nodes that immediately precede a free hole. */
64ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
65ea7b1dd4SDaniel Vetter 	/* head_node.node_list is the list of all memory nodes, ordered
66ea7b1dd4SDaniel Vetter 	 * according to the (increasing) start address of the memory node. */
67ea7b1dd4SDaniel Vetter 	struct drm_mm_node head_node;
68249d6048SJerome Glisse 	struct list_head unused_nodes;
69249d6048SJerome Glisse 	int num_unused;
70249d6048SJerome Glisse 	spinlock_t unused_lock;
71d935cc61SDaniel Vetter 	unsigned int scan_check_range : 1;
72709ea971SDaniel Vetter 	unsigned scan_alignment;
736b9d89b4SChris Wilson 	unsigned long scan_color;
74709ea971SDaniel Vetter 	unsigned long scan_size;
75709ea971SDaniel Vetter 	unsigned long scan_hit_start;
76901593f2SChris Wilson 	unsigned long scan_hit_end;
77709ea971SDaniel Vetter 	unsigned scanned_blocks;
78d935cc61SDaniel Vetter 	unsigned long scan_start;
79d935cc61SDaniel Vetter 	unsigned long scan_end;
80ae0cec28SDaniel Vetter 	struct drm_mm_node *prev_scanned_node;
816b9d89b4SChris Wilson 
826b9d89b4SChris Wilson 	void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
836b9d89b4SChris Wilson 			     unsigned long *start, unsigned long *end);
84249d6048SJerome Glisse };
85249d6048SJerome Glisse 
86b0b7af18SDaniel Vetter static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
87b0b7af18SDaniel Vetter {
88b0b7af18SDaniel Vetter 	return node->allocated;
89b0b7af18SDaniel Vetter }
90b0b7af18SDaniel Vetter 
9131a5b8ceSDaniel Vetter static inline bool drm_mm_initialized(struct drm_mm *mm)
9231a5b8ceSDaniel Vetter {
93ea7b1dd4SDaniel Vetter 	return mm->hole_stack.next;
9431a5b8ceSDaniel Vetter }
959e8944abSChris Wilson 
969e8944abSChris Wilson static inline unsigned long __drm_mm_hole_node_start(struct drm_mm_node *hole_node)
979e8944abSChris Wilson {
989e8944abSChris Wilson 	return hole_node->start + hole_node->size;
999e8944abSChris Wilson }
1009e8944abSChris Wilson 
1019e8944abSChris Wilson static inline unsigned long drm_mm_hole_node_start(struct drm_mm_node *hole_node)
1029e8944abSChris Wilson {
1039e8944abSChris Wilson 	BUG_ON(!hole_node->hole_follows);
1049e8944abSChris Wilson 	return __drm_mm_hole_node_start(hole_node);
1059e8944abSChris Wilson }
1069e8944abSChris Wilson 
1079e8944abSChris Wilson static inline unsigned long __drm_mm_hole_node_end(struct drm_mm_node *hole_node)
1089e8944abSChris Wilson {
1099e8944abSChris Wilson 	return list_entry(hole_node->node_list.next,
1109e8944abSChris Wilson 			  struct drm_mm_node, node_list)->start;
1119e8944abSChris Wilson }
1129e8944abSChris Wilson 
1139e8944abSChris Wilson static inline unsigned long drm_mm_hole_node_end(struct drm_mm_node *hole_node)
1149e8944abSChris Wilson {
1159e8944abSChris Wilson 	return __drm_mm_hole_node_end(hole_node);
1169e8944abSChris Wilson }
1179e8944abSChris Wilson 
118ea7b1dd4SDaniel Vetter #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
119ea7b1dd4SDaniel Vetter 						&(mm)->head_node.node_list, \
1202bbd4492SDaniel Vetter 						node_list)
121ae0cec28SDaniel Vetter #define drm_mm_for_each_scanned_node_reverse(entry, n, mm) \
122ae0cec28SDaniel Vetter 	for (entry = (mm)->prev_scanned_node, \
123ae0cec28SDaniel Vetter 		next = entry ? list_entry(entry->node_list.next, \
124ae0cec28SDaniel Vetter 			struct drm_mm_node, node_list) : NULL; \
125ae0cec28SDaniel Vetter 	     entry != NULL; entry = next, \
126ae0cec28SDaniel Vetter 		next = entry ? list_entry(entry->node_list.next, \
127ae0cec28SDaniel Vetter 			struct drm_mm_node, node_list) : NULL) \
1289e8944abSChris Wilson 
1299e8944abSChris Wilson /* Note that we need to unroll list_for_each_entry in order to inline
1309e8944abSChris Wilson  * setting hole_start and hole_end on each iteration and keep the
1319e8944abSChris Wilson  * macro sane.
1329e8944abSChris Wilson  */
1339e8944abSChris Wilson #define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
1349e8944abSChris Wilson 	for (entry = list_entry((mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
1359e8944abSChris Wilson 	     &entry->hole_stack != &(mm)->hole_stack ? \
1369e8944abSChris Wilson 	     hole_start = drm_mm_hole_node_start(entry), \
1379e8944abSChris Wilson 	     hole_end = drm_mm_hole_node_end(entry), \
1389e8944abSChris Wilson 	     1 : 0; \
1399e8944abSChris Wilson 	     entry = list_entry(entry->hole_stack.next, struct drm_mm_node, hole_stack))
1409e8944abSChris Wilson 
141249d6048SJerome Glisse /*
142249d6048SJerome Glisse  * Basic range manager support (drm_mm.c)
143249d6048SJerome Glisse  */
144338710e7SBen Widawsky extern int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
14589579f77SThomas Hellstrom extern struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
146249d6048SJerome Glisse 						    unsigned long size,
14789579f77SThomas Hellstrom 						    unsigned alignment,
1486b9d89b4SChris Wilson 						    unsigned long color,
14989579f77SThomas Hellstrom 						    int atomic);
150a2e68e92SJerome Glisse extern struct drm_mm_node *drm_mm_get_block_range_generic(
151a2e68e92SJerome Glisse 						struct drm_mm_node *node,
152a2e68e92SJerome Glisse 						unsigned long size,
153a2e68e92SJerome Glisse 						unsigned alignment,
1546b9d89b4SChris Wilson 						unsigned long color,
155a2e68e92SJerome Glisse 						unsigned long start,
156a2e68e92SJerome Glisse 						unsigned long end,
157a2e68e92SJerome Glisse 						int atomic);
158b3a070ccSBen Widawsky 
15989579f77SThomas Hellstrom static inline struct drm_mm_node *drm_mm_get_block(struct drm_mm_node *parent,
160249d6048SJerome Glisse 						   unsigned long size,
16189579f77SThomas Hellstrom 						   unsigned alignment)
16289579f77SThomas Hellstrom {
1636b9d89b4SChris Wilson 	return drm_mm_get_block_generic(parent, size, alignment, 0, 0);
16489579f77SThomas Hellstrom }
16589579f77SThomas Hellstrom static inline struct drm_mm_node *drm_mm_get_block_atomic(struct drm_mm_node *parent,
16689579f77SThomas Hellstrom 							  unsigned long size,
16789579f77SThomas Hellstrom 							  unsigned alignment)
16889579f77SThomas Hellstrom {
1696b9d89b4SChris Wilson 	return drm_mm_get_block_generic(parent, size, alignment, 0, 1);
17089579f77SThomas Hellstrom }
171a2e68e92SJerome Glisse static inline struct drm_mm_node *drm_mm_get_block_range(
172a2e68e92SJerome Glisse 						struct drm_mm_node *parent,
173a2e68e92SJerome Glisse 						unsigned long size,
174a2e68e92SJerome Glisse 						unsigned alignment,
175a2e68e92SJerome Glisse 						unsigned long start,
176a2e68e92SJerome Glisse 						unsigned long end)
177a2e68e92SJerome Glisse {
1786b9d89b4SChris Wilson 	return drm_mm_get_block_range_generic(parent, size, alignment, 0,
1796b9d89b4SChris Wilson 					      start, end, 0);
1806b9d89b4SChris Wilson }
181a2e68e92SJerome Glisse static inline struct drm_mm_node *drm_mm_get_block_atomic_range(
182a2e68e92SJerome Glisse 						struct drm_mm_node *parent,
183a2e68e92SJerome Glisse 						unsigned long size,
184a2e68e92SJerome Glisse 						unsigned alignment,
185a2e68e92SJerome Glisse 						unsigned long start,
186a2e68e92SJerome Glisse 						unsigned long end)
187a2e68e92SJerome Glisse {
1886b9d89b4SChris Wilson 	return drm_mm_get_block_range_generic(parent, size, alignment, 0,
189a2e68e92SJerome Glisse 						start, end, 1);
190a2e68e92SJerome Glisse }
191b8103450SChris Wilson 
192b8103450SChris Wilson extern int drm_mm_insert_node(struct drm_mm *mm,
193b8103450SChris Wilson 			      struct drm_mm_node *node,
194b8103450SChris Wilson 			      unsigned long size,
195b8103450SChris Wilson 			      unsigned alignment);
196b0b7af18SDaniel Vetter extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
197b0b7af18SDaniel Vetter 				       struct drm_mm_node *node,
198b8103450SChris Wilson 				       unsigned long size,
199b8103450SChris Wilson 				       unsigned alignment,
200b8103450SChris Wilson 				       unsigned long start,
201b8103450SChris Wilson 				       unsigned long end);
202b8103450SChris Wilson extern int drm_mm_insert_node_generic(struct drm_mm *mm,
203b8103450SChris Wilson 				      struct drm_mm_node *node,
204b8103450SChris Wilson 				      unsigned long size,
205b8103450SChris Wilson 				      unsigned alignment,
206b8103450SChris Wilson 				      unsigned long color);
207b8103450SChris Wilson extern int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
208b8103450SChris Wilson 				       struct drm_mm_node *node,
209b8103450SChris Wilson 				       unsigned long size,
210b8103450SChris Wilson 				       unsigned alignment,
211b8103450SChris Wilson 				       unsigned long color,
212b8103450SChris Wilson 				       unsigned long start,
213b8103450SChris Wilson 				       unsigned long end);
214249d6048SJerome Glisse extern void drm_mm_put_block(struct drm_mm_node *cur);
215b0b7af18SDaniel Vetter extern void drm_mm_remove_node(struct drm_mm_node *node);
216b0b7af18SDaniel Vetter extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
2176b9d89b4SChris Wilson extern struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
218249d6048SJerome Glisse 						      unsigned long size,
219249d6048SJerome Glisse 						      unsigned alignment,
2206b9d89b4SChris Wilson 						      unsigned long color,
2216b9d89b4SChris Wilson 						      bool best_match);
2226b9d89b4SChris Wilson extern struct drm_mm_node *drm_mm_search_free_in_range_generic(
2236b9d89b4SChris Wilson 						const struct drm_mm *mm,
2246b9d89b4SChris Wilson 						unsigned long size,
2256b9d89b4SChris Wilson 						unsigned alignment,
2266b9d89b4SChris Wilson 						unsigned long color,
2276b9d89b4SChris Wilson 						unsigned long start,
2286b9d89b4SChris Wilson 						unsigned long end,
2296b9d89b4SChris Wilson 						bool best_match);
2306b9d89b4SChris Wilson static inline struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
2316b9d89b4SChris Wilson 						     unsigned long size,
2326b9d89b4SChris Wilson 						     unsigned alignment,
2336b9d89b4SChris Wilson 						     bool best_match)
2346b9d89b4SChris Wilson {
2356b9d89b4SChris Wilson 	return drm_mm_search_free_generic(mm,size, alignment, 0, best_match);
2366b9d89b4SChris Wilson }
2376b9d89b4SChris Wilson static inline  struct drm_mm_node *drm_mm_search_free_in_range(
238a2e68e92SJerome Glisse 						const struct drm_mm *mm,
239a2e68e92SJerome Glisse 						unsigned long size,
240a2e68e92SJerome Glisse 						unsigned alignment,
241a2e68e92SJerome Glisse 						unsigned long start,
242a2e68e92SJerome Glisse 						unsigned long end,
2436b9d89b4SChris Wilson 						bool best_match)
2446b9d89b4SChris Wilson {
2456b9d89b4SChris Wilson 	return drm_mm_search_free_in_range_generic(mm, size, alignment, 0,
2466b9d89b4SChris Wilson 						   start, end, best_match);
2476b9d89b4SChris Wilson }
24869163ea8SDaniel Vetter 
24977ef8bbcSDavid Herrmann extern void drm_mm_init(struct drm_mm *mm,
2506b9d89b4SChris Wilson 			unsigned long start,
251249d6048SJerome Glisse 			unsigned long size);
252249d6048SJerome Glisse extern void drm_mm_takedown(struct drm_mm *mm);
253249d6048SJerome Glisse extern int drm_mm_clean(struct drm_mm *mm);
254249d6048SJerome Glisse extern int drm_mm_pre_get(struct drm_mm *mm);
255249d6048SJerome Glisse 
256249d6048SJerome Glisse static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
257249d6048SJerome Glisse {
258249d6048SJerome Glisse 	return block->mm;
259249d6048SJerome Glisse }
260249d6048SJerome Glisse 
2616b9d89b4SChris Wilson void drm_mm_init_scan(struct drm_mm *mm,
2626b9d89b4SChris Wilson 		      unsigned long size,
263d935cc61SDaniel Vetter 		      unsigned alignment,
2646b9d89b4SChris Wilson 		      unsigned long color);
2656b9d89b4SChris Wilson void drm_mm_init_scan_with_range(struct drm_mm *mm,
2666b9d89b4SChris Wilson 				 unsigned long size,
2676b9d89b4SChris Wilson 				 unsigned alignment,
2686b9d89b4SChris Wilson 				 unsigned long color,
269d935cc61SDaniel Vetter 				 unsigned long start,
270d935cc61SDaniel Vetter 				 unsigned long end);
271709ea971SDaniel Vetter int drm_mm_scan_add_block(struct drm_mm_node *node);
272709ea971SDaniel Vetter int drm_mm_scan_remove_block(struct drm_mm_node *node);
273709ea971SDaniel Vetter 
27499d7e48eSJerome Glisse extern void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
275fa8a1238SDave Airlie #ifdef CONFIG_DEBUG_FS
276fa8a1238SDave Airlie int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
277fa8a1238SDave Airlie #endif
278fa8a1238SDave Airlie 
279249d6048SJerome Glisse #endif
280