xref: /openbmc/linux/include/drm/drm_mm.h (revision b8103450)
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  */
39249d6048SJerome Glisse #include <linux/list.h>
40f1938cd6SDave Airlie #ifdef CONFIG_DEBUG_FS
41f1938cd6SDave Airlie #include <linux/seq_file.h>
42f1938cd6SDave Airlie #endif
43249d6048SJerome Glisse 
44249d6048SJerome Glisse struct drm_mm_node {
45d1024ce9SDaniel Vetter 	struct list_head node_list;
46ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
47ea7b1dd4SDaniel Vetter 	unsigned hole_follows : 1;
48709ea971SDaniel Vetter 	unsigned scanned_block : 1;
49709ea971SDaniel Vetter 	unsigned scanned_prev_free : 1;
50709ea971SDaniel Vetter 	unsigned scanned_next_free : 1;
51ea7b1dd4SDaniel Vetter 	unsigned scanned_preceeds_hole : 1;
52b0b7af18SDaniel Vetter 	unsigned allocated : 1;
536b9d89b4SChris Wilson 	unsigned long color;
54249d6048SJerome Glisse 	unsigned long start;
55249d6048SJerome Glisse 	unsigned long size;
56249d6048SJerome Glisse 	struct drm_mm *mm;
57249d6048SJerome Glisse };
58249d6048SJerome Glisse 
59249d6048SJerome Glisse struct drm_mm {
6025985edcSLucas De Marchi 	/* List of all memory nodes that immediately precede a free hole. */
61ea7b1dd4SDaniel Vetter 	struct list_head hole_stack;
62ea7b1dd4SDaniel Vetter 	/* head_node.node_list is the list of all memory nodes, ordered
63ea7b1dd4SDaniel Vetter 	 * according to the (increasing) start address of the memory node. */
64ea7b1dd4SDaniel Vetter 	struct drm_mm_node head_node;
65249d6048SJerome Glisse 	struct list_head unused_nodes;
66249d6048SJerome Glisse 	int num_unused;
67249d6048SJerome Glisse 	spinlock_t unused_lock;
68d935cc61SDaniel Vetter 	unsigned int scan_check_range : 1;
69709ea971SDaniel Vetter 	unsigned scan_alignment;
706b9d89b4SChris Wilson 	unsigned long scan_color;
71709ea971SDaniel Vetter 	unsigned long scan_size;
72709ea971SDaniel Vetter 	unsigned long scan_hit_start;
73709ea971SDaniel Vetter 	unsigned scan_hit_size;
74709ea971SDaniel Vetter 	unsigned scanned_blocks;
75d935cc61SDaniel Vetter 	unsigned long scan_start;
76d935cc61SDaniel Vetter 	unsigned long scan_end;
77ae0cec28SDaniel Vetter 	struct drm_mm_node *prev_scanned_node;
786b9d89b4SChris Wilson 
796b9d89b4SChris Wilson 	void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
806b9d89b4SChris Wilson 			     unsigned long *start, unsigned long *end);
81249d6048SJerome Glisse };
82249d6048SJerome Glisse 
83b0b7af18SDaniel Vetter static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
84b0b7af18SDaniel Vetter {
85b0b7af18SDaniel Vetter 	return node->allocated;
86b0b7af18SDaniel Vetter }
87b0b7af18SDaniel Vetter 
8831a5b8ceSDaniel Vetter static inline bool drm_mm_initialized(struct drm_mm *mm)
8931a5b8ceSDaniel Vetter {
90ea7b1dd4SDaniel Vetter 	return mm->hole_stack.next;
9131a5b8ceSDaniel Vetter }
92ea7b1dd4SDaniel Vetter #define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
93ea7b1dd4SDaniel Vetter 						&(mm)->head_node.node_list, \
942bbd4492SDaniel Vetter 						node_list)
95ae0cec28SDaniel Vetter #define drm_mm_for_each_scanned_node_reverse(entry, n, mm) \
96ae0cec28SDaniel Vetter 	for (entry = (mm)->prev_scanned_node, \
97ae0cec28SDaniel Vetter 		next = entry ? list_entry(entry->node_list.next, \
98ae0cec28SDaniel Vetter 			struct drm_mm_node, node_list) : NULL; \
99ae0cec28SDaniel Vetter 	     entry != NULL; entry = next, \
100ae0cec28SDaniel Vetter 		next = entry ? list_entry(entry->node_list.next, \
101ae0cec28SDaniel Vetter 			struct drm_mm_node, node_list) : NULL) \
102249d6048SJerome Glisse /*
103249d6048SJerome Glisse  * Basic range manager support (drm_mm.c)
104249d6048SJerome Glisse  */
10589579f77SThomas Hellstrom extern struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *node,
106249d6048SJerome Glisse 						    unsigned long size,
10789579f77SThomas Hellstrom 						    unsigned alignment,
1086b9d89b4SChris Wilson 						    unsigned long color,
10989579f77SThomas Hellstrom 						    int atomic);
110a2e68e92SJerome Glisse extern struct drm_mm_node *drm_mm_get_block_range_generic(
111a2e68e92SJerome Glisse 						struct drm_mm_node *node,
112a2e68e92SJerome Glisse 						unsigned long size,
113a2e68e92SJerome Glisse 						unsigned alignment,
1146b9d89b4SChris Wilson 						unsigned long color,
115a2e68e92SJerome Glisse 						unsigned long start,
116a2e68e92SJerome Glisse 						unsigned long end,
117a2e68e92SJerome Glisse 						int atomic);
11889579f77SThomas Hellstrom static inline struct drm_mm_node *drm_mm_get_block(struct drm_mm_node *parent,
119249d6048SJerome Glisse 						   unsigned long size,
12089579f77SThomas Hellstrom 						   unsigned alignment)
12189579f77SThomas Hellstrom {
1226b9d89b4SChris Wilson 	return drm_mm_get_block_generic(parent, size, alignment, 0, 0);
12389579f77SThomas Hellstrom }
12489579f77SThomas Hellstrom static inline struct drm_mm_node *drm_mm_get_block_atomic(struct drm_mm_node *parent,
12589579f77SThomas Hellstrom 							  unsigned long size,
12689579f77SThomas Hellstrom 							  unsigned alignment)
12789579f77SThomas Hellstrom {
1286b9d89b4SChris Wilson 	return drm_mm_get_block_generic(parent, size, alignment, 0, 1);
12989579f77SThomas Hellstrom }
130a2e68e92SJerome Glisse static inline struct drm_mm_node *drm_mm_get_block_range(
131a2e68e92SJerome Glisse 						struct drm_mm_node *parent,
132a2e68e92SJerome Glisse 						unsigned long size,
133a2e68e92SJerome Glisse 						unsigned alignment,
134a2e68e92SJerome Glisse 						unsigned long start,
135a2e68e92SJerome Glisse 						unsigned long end)
136a2e68e92SJerome Glisse {
1376b9d89b4SChris Wilson 	return drm_mm_get_block_range_generic(parent, size, alignment, 0,
1386b9d89b4SChris Wilson 					      start, end, 0);
1396b9d89b4SChris Wilson }
1406b9d89b4SChris Wilson static inline struct drm_mm_node *drm_mm_get_color_block_range(
1416b9d89b4SChris Wilson 						struct drm_mm_node *parent,
1426b9d89b4SChris Wilson 						unsigned long size,
1436b9d89b4SChris Wilson 						unsigned alignment,
1446b9d89b4SChris Wilson 						unsigned long color,
1456b9d89b4SChris Wilson 						unsigned long start,
1466b9d89b4SChris Wilson 						unsigned long end)
1476b9d89b4SChris Wilson {
1486b9d89b4SChris Wilson 	return drm_mm_get_block_range_generic(parent, size, alignment, color,
149a2e68e92SJerome Glisse 					      start, end, 0);
150a2e68e92SJerome Glisse }
151a2e68e92SJerome Glisse static inline struct drm_mm_node *drm_mm_get_block_atomic_range(
152a2e68e92SJerome Glisse 						struct drm_mm_node *parent,
153a2e68e92SJerome Glisse 						unsigned long size,
154a2e68e92SJerome Glisse 						unsigned alignment,
155a2e68e92SJerome Glisse 						unsigned long start,
156a2e68e92SJerome Glisse 						unsigned long end)
157a2e68e92SJerome Glisse {
1586b9d89b4SChris Wilson 	return drm_mm_get_block_range_generic(parent, size, alignment, 0,
159a2e68e92SJerome Glisse 						start, end, 1);
160a2e68e92SJerome Glisse }
161b8103450SChris Wilson 
162b8103450SChris Wilson extern int drm_mm_insert_node(struct drm_mm *mm,
163b8103450SChris Wilson 			      struct drm_mm_node *node,
164b8103450SChris Wilson 			      unsigned long size,
165b8103450SChris Wilson 			      unsigned alignment);
166b0b7af18SDaniel Vetter extern int drm_mm_insert_node_in_range(struct drm_mm *mm,
167b0b7af18SDaniel Vetter 				       struct drm_mm_node *node,
168b8103450SChris Wilson 				       unsigned long size,
169b8103450SChris Wilson 				       unsigned alignment,
170b8103450SChris Wilson 				       unsigned long start,
171b8103450SChris Wilson 				       unsigned long end);
172b8103450SChris Wilson extern int drm_mm_insert_node_generic(struct drm_mm *mm,
173b8103450SChris Wilson 				      struct drm_mm_node *node,
174b8103450SChris Wilson 				      unsigned long size,
175b8103450SChris Wilson 				      unsigned alignment,
176b8103450SChris Wilson 				      unsigned long color);
177b8103450SChris Wilson extern int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
178b8103450SChris Wilson 				       struct drm_mm_node *node,
179b8103450SChris Wilson 				       unsigned long size,
180b8103450SChris Wilson 				       unsigned alignment,
181b8103450SChris Wilson 				       unsigned long color,
182b8103450SChris Wilson 				       unsigned long start,
183b8103450SChris Wilson 				       unsigned long end);
184249d6048SJerome Glisse extern void drm_mm_put_block(struct drm_mm_node *cur);
185b0b7af18SDaniel Vetter extern void drm_mm_remove_node(struct drm_mm_node *node);
186b0b7af18SDaniel Vetter extern void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
1876b9d89b4SChris Wilson extern struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
188249d6048SJerome Glisse 						      unsigned long size,
189249d6048SJerome Glisse 						      unsigned alignment,
1906b9d89b4SChris Wilson 						      unsigned long color,
1916b9d89b4SChris Wilson 						      bool best_match);
1926b9d89b4SChris Wilson extern struct drm_mm_node *drm_mm_search_free_in_range_generic(
1936b9d89b4SChris Wilson 						const struct drm_mm *mm,
1946b9d89b4SChris Wilson 						unsigned long size,
1956b9d89b4SChris Wilson 						unsigned alignment,
1966b9d89b4SChris Wilson 						unsigned long color,
1976b9d89b4SChris Wilson 						unsigned long start,
1986b9d89b4SChris Wilson 						unsigned long end,
1996b9d89b4SChris Wilson 						bool best_match);
2006b9d89b4SChris Wilson static inline struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm,
2016b9d89b4SChris Wilson 						     unsigned long size,
2026b9d89b4SChris Wilson 						     unsigned alignment,
2036b9d89b4SChris Wilson 						     bool best_match)
2046b9d89b4SChris Wilson {
2056b9d89b4SChris Wilson 	return drm_mm_search_free_generic(mm,size, alignment, 0, best_match);
2066b9d89b4SChris Wilson }
2076b9d89b4SChris Wilson static inline  struct drm_mm_node *drm_mm_search_free_in_range(
208a2e68e92SJerome Glisse 						const struct drm_mm *mm,
209a2e68e92SJerome Glisse 						unsigned long size,
210a2e68e92SJerome Glisse 						unsigned alignment,
211a2e68e92SJerome Glisse 						unsigned long start,
212a2e68e92SJerome Glisse 						unsigned long end,
2136b9d89b4SChris Wilson 						bool best_match)
2146b9d89b4SChris Wilson {
2156b9d89b4SChris Wilson 	return drm_mm_search_free_in_range_generic(mm, size, alignment, 0,
2166b9d89b4SChris Wilson 						   start, end, best_match);
2176b9d89b4SChris Wilson }
2186b9d89b4SChris Wilson static inline struct drm_mm_node *drm_mm_search_free_color(const struct drm_mm *mm,
2196b9d89b4SChris Wilson 							   unsigned long size,
2206b9d89b4SChris Wilson 							   unsigned alignment,
2216b9d89b4SChris Wilson 							   unsigned long color,
2226b9d89b4SChris Wilson 							   bool best_match)
2236b9d89b4SChris Wilson {
2246b9d89b4SChris Wilson 	return drm_mm_search_free_generic(mm,size, alignment, color, best_match);
2256b9d89b4SChris Wilson }
2266b9d89b4SChris Wilson static inline  struct drm_mm_node *drm_mm_search_free_in_range_color(
2276b9d89b4SChris Wilson 						const struct drm_mm *mm,
2286b9d89b4SChris Wilson 						unsigned long size,
2296b9d89b4SChris Wilson 						unsigned alignment,
2306b9d89b4SChris Wilson 						unsigned long color,
2316b9d89b4SChris Wilson 						unsigned long start,
2326b9d89b4SChris Wilson 						unsigned long end,
2336b9d89b4SChris Wilson 						bool best_match)
2346b9d89b4SChris Wilson {
2356b9d89b4SChris Wilson 	return drm_mm_search_free_in_range_generic(mm, size, alignment, color,
2366b9d89b4SChris Wilson 						   start, end, best_match);
2376b9d89b4SChris Wilson }
2386b9d89b4SChris Wilson extern int drm_mm_init(struct drm_mm *mm,
2396b9d89b4SChris Wilson 		       unsigned long start,
240249d6048SJerome Glisse 		       unsigned long size);
241249d6048SJerome Glisse extern void drm_mm_takedown(struct drm_mm *mm);
242249d6048SJerome Glisse extern int drm_mm_clean(struct drm_mm *mm);
243249d6048SJerome Glisse extern int drm_mm_pre_get(struct drm_mm *mm);
244249d6048SJerome Glisse 
245249d6048SJerome Glisse static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
246249d6048SJerome Glisse {
247249d6048SJerome Glisse 	return block->mm;
248249d6048SJerome Glisse }
249249d6048SJerome Glisse 
2506b9d89b4SChris Wilson void drm_mm_init_scan(struct drm_mm *mm,
2516b9d89b4SChris Wilson 		      unsigned long size,
252d935cc61SDaniel Vetter 		      unsigned alignment,
2536b9d89b4SChris Wilson 		      unsigned long color);
2546b9d89b4SChris Wilson void drm_mm_init_scan_with_range(struct drm_mm *mm,
2556b9d89b4SChris Wilson 				 unsigned long size,
2566b9d89b4SChris Wilson 				 unsigned alignment,
2576b9d89b4SChris Wilson 				 unsigned long color,
258d935cc61SDaniel Vetter 				 unsigned long start,
259d935cc61SDaniel Vetter 				 unsigned long end);
260709ea971SDaniel Vetter int drm_mm_scan_add_block(struct drm_mm_node *node);
261709ea971SDaniel Vetter int drm_mm_scan_remove_block(struct drm_mm_node *node);
262709ea971SDaniel Vetter 
26399d7e48eSJerome Glisse extern void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
264fa8a1238SDave Airlie #ifdef CONFIG_DEBUG_FS
265fa8a1238SDave Airlie int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
266fa8a1238SDave Airlie #endif
267fa8a1238SDave Airlie 
268249d6048SJerome Glisse #endif
269