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