Linux Audio

Check our new training course

Loading...
v6.2
  1/**************************************************************************
  2 *
  3 * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
  4 * Copyright 2016 Intel Corporation
  5 * All Rights Reserved.
  6 *
  7 * Permission is hereby granted, free of charge, to any person obtaining a
  8 * copy of this software and associated documentation files (the
  9 * "Software"), to deal in the Software without restriction, including
 10 * without limitation the rights to use, copy, modify, merge, publish,
 11 * distribute, sub license, and/or sell copies of the Software, and to
 12 * permit persons to whom the Software is furnished to do so, subject to
 13 * the following conditions:
 14 *
 15 * The above copyright notice and this permission notice (including the
 16 * next paragraph) shall be included in all copies or substantial portions
 17 * of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 26 *
 27 *
 28 **************************************************************************/
 29/*
 30 * Authors:
 31 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
 32 */
 33
 34#ifndef _DRM_MM_H_
 35#define _DRM_MM_H_
 36
 37/*
 38 * Generic range manager structs
 39 */
 40#include <linux/bug.h>
 41#include <linux/rbtree.h>
 42#include <linux/limits.h>
 43#include <linux/mm_types.h>
 44#include <linux/list.h>
 45#include <linux/spinlock.h>
 
 
 
 46#ifdef CONFIG_DRM_DEBUG_MM
 47#include <linux/stackdepot.h>
 48#endif
 49#include <linux/types.h>
 50
 51#include <drm/drm_print.h>
 52
 53#ifdef CONFIG_DRM_DEBUG_MM
 54#define DRM_MM_BUG_ON(expr) BUG_ON(expr)
 55#else
 56#define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
 57#endif
 58
 59/**
 60 * enum drm_mm_insert_mode - control search and allocation behaviour
 61 *
 62 * The &struct drm_mm range manager supports finding a suitable modes using
 63 * a number of search trees. These trees are oranised by size, by address and
 64 * in most recent eviction order. This allows the user to find either the
 65 * smallest hole to reuse, the lowest or highest address to reuse, or simply
 66 * reuse the most recent eviction that fits. When allocating the &drm_mm_node
 67 * from within the hole, the &drm_mm_insert_mode also dictate whether to
 68 * allocate the lowest matching address or the highest.
 69 */
 70enum drm_mm_insert_mode {
 71	/**
 72	 * @DRM_MM_INSERT_BEST:
 73	 *
 74	 * Search for the smallest hole (within the search range) that fits
 75	 * the desired node.
 76	 *
 77	 * Allocates the node from the bottom of the found hole.
 78	 */
 79	DRM_MM_INSERT_BEST = 0,
 80
 81	/**
 82	 * @DRM_MM_INSERT_LOW:
 83	 *
 84	 * Search for the lowest hole (address closest to 0, within the search
 85	 * range) that fits the desired node.
 86	 *
 87	 * Allocates the node from the bottom of the found hole.
 88	 */
 89	DRM_MM_INSERT_LOW,
 90
 91	/**
 92	 * @DRM_MM_INSERT_HIGH:
 93	 *
 94	 * Search for the highest hole (address closest to U64_MAX, within the
 95	 * search range) that fits the desired node.
 96	 *
 97	 * Allocates the node from the *top* of the found hole. The specified
 98	 * alignment for the node is applied to the base of the node
 99	 * (&drm_mm_node.start).
100	 */
101	DRM_MM_INSERT_HIGH,
102
103	/**
104	 * @DRM_MM_INSERT_EVICT:
105	 *
106	 * Search for the most recently evicted hole (within the search range)
107	 * that fits the desired node. This is appropriate for use immediately
108	 * after performing an eviction scan (see drm_mm_scan_init()) and
109	 * removing the selected nodes to form a hole.
110	 *
111	 * Allocates the node from the bottom of the found hole.
112	 */
113	DRM_MM_INSERT_EVICT,
114
115	/**
116	 * @DRM_MM_INSERT_ONCE:
117	 *
118	 * Only check the first hole for suitablity and report -ENOSPC
119	 * immediately otherwise, rather than check every hole until a
120	 * suitable one is found. Can only be used in conjunction with another
121	 * search method such as DRM_MM_INSERT_HIGH or DRM_MM_INSERT_LOW.
122	 */
123	DRM_MM_INSERT_ONCE = BIT(31),
124
125	/**
126	 * @DRM_MM_INSERT_HIGHEST:
127	 *
128	 * Only check the highest hole (the hole with the largest address) and
129	 * insert the node at the top of the hole or report -ENOSPC if
130	 * unsuitable.
131	 *
132	 * Does not search all holes.
133	 */
134	DRM_MM_INSERT_HIGHEST = DRM_MM_INSERT_HIGH | DRM_MM_INSERT_ONCE,
135
136	/**
137	 * @DRM_MM_INSERT_LOWEST:
138	 *
139	 * Only check the lowest hole (the hole with the smallest address) and
140	 * insert the node at the bottom of the hole or report -ENOSPC if
141	 * unsuitable.
142	 *
143	 * Does not search all holes.
144	 */
145	DRM_MM_INSERT_LOWEST  = DRM_MM_INSERT_LOW | DRM_MM_INSERT_ONCE,
146};
147
148/**
149 * struct drm_mm_node - allocated block in the DRM allocator
150 *
151 * This represents an allocated block in a &drm_mm allocator. Except for
152 * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is
153 * entirely opaque and should only be accessed through the provided funcions.
154 * Since allocation of these nodes is entirely handled by the driver they can be
155 * embedded.
156 */
157struct drm_mm_node {
158	/** @color: Opaque driver-private tag. */
159	unsigned long color;
160	/** @start: Start address of the allocated block. */
161	u64 start;
162	/** @size: Size of the allocated block. */
163	u64 size;
164	/* private: */
165	struct drm_mm *mm;
166	struct list_head node_list;
167	struct list_head hole_stack;
168	struct rb_node rb;
169	struct rb_node rb_hole_size;
170	struct rb_node rb_hole_addr;
 
 
 
 
 
 
 
171	u64 __subtree_last;
172	u64 hole_size;
173	u64 subtree_max_hole;
174	unsigned long flags;
175#define DRM_MM_NODE_ALLOCATED_BIT	0
176#define DRM_MM_NODE_SCANNED_BIT		1
177#ifdef CONFIG_DRM_DEBUG_MM
178	depot_stack_handle_t stack;
179#endif
180};
181
182/**
183 * struct drm_mm - DRM allocator
184 *
185 * DRM range allocator with a few special functions and features geared towards
186 * managing GPU memory. Except for the @color_adjust callback the structure is
187 * entirely opaque and should only be accessed through the provided functions
188 * and macros. This structure can be embedded into larger driver structures.
189 */
190struct drm_mm {
191	/**
192	 * @color_adjust:
193	 *
194	 * Optional driver callback to further apply restrictions on a hole. The
195	 * node argument points at the node containing the hole from which the
196	 * block would be allocated (see drm_mm_hole_follows() and friends). The
197	 * other arguments are the size of the block to be allocated. The driver
198	 * can adjust the start and end as needed to e.g. insert guard pages.
199	 */
200	void (*color_adjust)(const struct drm_mm_node *node,
201			     unsigned long color,
202			     u64 *start, u64 *end);
203
204	/* private: */
205	/* List of all memory nodes that immediately precede a free hole. */
206	struct list_head hole_stack;
207	/* head_node.node_list is the list of all memory nodes, ordered
208	 * according to the (increasing) start address of the memory node. */
209	struct drm_mm_node head_node;
210	/* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
211	struct rb_root_cached interval_tree;
212	struct rb_root_cached holes_size;
213	struct rb_root holes_addr;
214
215	unsigned long scan_active;
216};
217
218/**
219 * struct drm_mm_scan - DRM allocator eviction roaster data
220 *
221 * This structure tracks data needed for the eviction roaster set up using
222 * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and
223 * drm_mm_scan_remove_block(). The structure is entirely opaque and should only
224 * be accessed through the provided functions and macros. It is meant to be
225 * allocated temporarily by the driver on the stack.
226 */
227struct drm_mm_scan {
228	/* private: */
229	struct drm_mm *mm;
230
231	u64 size;
232	u64 alignment;
233	u64 remainder_mask;
234
235	u64 range_start;
236	u64 range_end;
237
238	u64 hit_start;
239	u64 hit_end;
240
241	unsigned long color;
242	enum drm_mm_insert_mode mode;
243};
244
245/**
246 * drm_mm_node_allocated - checks whether a node is allocated
247 * @node: drm_mm_node to check
248 *
249 * Drivers are required to clear a node prior to using it with the
250 * drm_mm range manager.
251 *
252 * Drivers should use this helper for proper encapsulation of drm_mm
253 * internals.
254 *
255 * Returns:
256 * True if the @node is allocated.
257 */
258static inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
259{
260	return test_bit(DRM_MM_NODE_ALLOCATED_BIT, &node->flags);
261}
262
263/**
264 * drm_mm_initialized - checks whether an allocator is initialized
265 * @mm: drm_mm to check
266 *
267 * Drivers should clear the struct drm_mm prior to initialisation if they
268 * want to use this function.
269 *
270 * Drivers should use this helper for proper encapsulation of drm_mm
271 * internals.
272 *
273 * Returns:
274 * True if the @mm is initialized.
275 */
276static inline bool drm_mm_initialized(const struct drm_mm *mm)
277{
278	return READ_ONCE(mm->hole_stack.next);
279}
280
281/**
282 * drm_mm_hole_follows - checks whether a hole follows this node
283 * @node: drm_mm_node to check
284 *
285 * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
286 * If you wish to know whether a hole follows this particular node,
287 * query this function. See also drm_mm_hole_node_start() and
288 * drm_mm_hole_node_end().
289 *
290 * Returns:
291 * True if a hole follows the @node.
292 */
293static inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
294{
295	return node->hole_size;
296}
297
298static inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
299{
300	return hole_node->start + hole_node->size;
301}
302
303/**
304 * drm_mm_hole_node_start - computes the start of the hole following @node
305 * @hole_node: drm_mm_node which implicitly tracks the following hole
306 *
307 * This is useful for driver-specific debug dumpers. Otherwise drivers should
308 * not inspect holes themselves. Drivers must check first whether a hole indeed
309 * follows by looking at drm_mm_hole_follows()
310 *
311 * Returns:
312 * Start of the subsequent hole.
313 */
314static inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
315{
316	DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
317	return __drm_mm_hole_node_start(hole_node);
318}
319
320static inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
321{
322	return list_next_entry(hole_node, node_list)->start;
323}
324
325/**
326 * drm_mm_hole_node_end - computes the end of the hole following @node
327 * @hole_node: drm_mm_node which implicitly tracks the following hole
328 *
329 * This is useful for driver-specific debug dumpers. Otherwise drivers should
330 * not inspect holes themselves. Drivers must check first whether a hole indeed
331 * follows by looking at drm_mm_hole_follows().
332 *
333 * Returns:
334 * End of the subsequent hole.
335 */
336static inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
337{
338	return __drm_mm_hole_node_end(hole_node);
339}
340
341/**
342 * drm_mm_nodes - list of nodes under the drm_mm range manager
343 * @mm: the struct drm_mm range manager
344 *
345 * As the drm_mm range manager hides its node_list deep with its
346 * structure, extracting it looks painful and repetitive. This is
347 * not expected to be used outside of the drm_mm_for_each_node()
348 * macros and similar internal functions.
349 *
350 * Returns:
351 * The node list, may be empty.
352 */
353#define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
354
355/**
356 * drm_mm_for_each_node - iterator to walk over all allocated nodes
357 * @entry: &struct drm_mm_node to assign to in each iteration step
358 * @mm: &drm_mm allocator to walk
359 *
360 * This iterator walks over all nodes in the range allocator. It is implemented
361 * with list_for_each(), so not save against removal of elements.
362 */
363#define drm_mm_for_each_node(entry, mm) \
364	list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
365
366/**
367 * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
368 * @entry: &struct drm_mm_node to assign to in each iteration step
369 * @next: &struct drm_mm_node to store the next step
370 * @mm: &drm_mm allocator to walk
371 *
372 * This iterator walks over all nodes in the range allocator. It is implemented
373 * with list_for_each_safe(), so save against removal of elements.
374 */
375#define drm_mm_for_each_node_safe(entry, next, mm) \
376	list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
 
 
 
 
 
 
 
 
 
377
378/**
379 * drm_mm_for_each_hole - iterator to walk over all holes
380 * @pos: &drm_mm_node used internally to track progress
381 * @mm: &drm_mm allocator to walk
382 * @hole_start: ulong variable to assign the hole start to on each iteration
383 * @hole_end: ulong variable to assign the hole end to on each iteration
384 *
385 * This iterator walks over all holes in the range allocator. It is implemented
386 * with list_for_each(), so not save against removal of elements. @entry is used
387 * internally and will not reflect a real drm_mm_node for the very first hole.
388 * Hence users of this iterator may not access it.
389 *
390 * Implementation Note:
391 * We need to inline list_for_each_entry in order to be able to set hole_start
392 * and hole_end on each iteration while keeping the macro sane.
 
 
 
393 */
394#define drm_mm_for_each_hole(pos, mm, hole_start, hole_end) \
395	for (pos = list_first_entry(&(mm)->hole_stack, \
396				    typeof(*pos), hole_stack); \
397	     &pos->hole_stack != &(mm)->hole_stack ? \
398	     hole_start = drm_mm_hole_node_start(pos), \
399	     hole_end = hole_start + pos->hole_size, \
400	     1 : 0; \
401	     pos = list_next_entry(pos, hole_stack))
402
403/*
404 * Basic range manager support (drm_mm.c)
405 */
406int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
407int drm_mm_insert_node_in_range(struct drm_mm *mm,
408				struct drm_mm_node *node,
409				u64 size,
410				u64 alignment,
411				unsigned long color,
412				u64 start,
413				u64 end,
414				enum drm_mm_insert_mode mode);
415
 
 
 
 
 
 
 
416/**
417 * drm_mm_insert_node_generic - search for space and insert @node
418 * @mm: drm_mm to allocate from
419 * @node: preallocate node to insert
420 * @size: size of the allocation
421 * @alignment: alignment of the allocation
422 * @color: opaque tag value to use for this node
423 * @mode: fine-tune the allocation search and placement
424 *
425 * This is a simplified version of drm_mm_insert_node_in_range() with no
426 * range restrictions applied.
427 *
428 * The preallocated node must be cleared to 0.
429 *
430 * Returns:
431 * 0 on success, -ENOSPC if there's no suitable hole.
432 */
433static inline int
434drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
435			   u64 size, u64 alignment,
436			   unsigned long color,
437			   enum drm_mm_insert_mode mode)
438{
439	return drm_mm_insert_node_in_range(mm, node,
440					   size, alignment, color,
441					   0, U64_MAX, mode);
442}
443
 
 
 
 
 
 
 
 
444/**
445 * drm_mm_insert_node - search for space and insert @node
446 * @mm: drm_mm to allocate from
447 * @node: preallocate node to insert
448 * @size: size of the allocation
 
 
 
 
449 *
450 * This is a simplified version of drm_mm_insert_node_generic() with @color set
451 * to 0.
452 *
453 * The preallocated node must be cleared to 0.
454 *
455 * Returns:
456 * 0 on success, -ENOSPC if there's no suitable hole.
457 */
458static inline int drm_mm_insert_node(struct drm_mm *mm,
459				     struct drm_mm_node *node,
460				     u64 size)
461{
462	return drm_mm_insert_node_generic(mm, node, size, 0, 0, 0);
 
 
 
 
 
 
463}
464
465void drm_mm_remove_node(struct drm_mm_node *node);
466void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
467void drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
 
 
468void drm_mm_takedown(struct drm_mm *mm);
469
470/**
471 * drm_mm_clean - checks whether an allocator is clean
472 * @mm: drm_mm allocator to check
473 *
474 * Returns:
475 * True if the allocator is completely free, false if there's still a node
476 * allocated in it.
477 */
478static inline bool drm_mm_clean(const struct drm_mm *mm)
479{
480	return list_empty(drm_mm_nodes(mm));
481}
482
483struct drm_mm_node *
484__drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
485
486/**
487 * drm_mm_for_each_node_in_range - iterator to walk over a range of
488 * allocated nodes
489 * @node__: drm_mm_node structure to assign to in each iteration step
490 * @mm__: drm_mm allocator to walk
491 * @start__: starting offset, the first node will overlap this
492 * @end__: ending offset, the last node will start before this (but may overlap)
493 *
494 * This iterator walks over all nodes in the range allocator that lie
495 * between @start and @end. It is implemented similarly to list_for_each(),
496 * but using the internal interval tree to accelerate the search for the
497 * starting node, and so not safe against removal of elements. It assumes
498 * that @end is within (or is the upper limit of) the drm_mm allocator.
499 * If [@start, @end] are beyond the range of the drm_mm, the iterator may walk
500 * over the special _unallocated_ &drm_mm.head_node, and may even continue
501 * indefinitely.
502 */
503#define drm_mm_for_each_node_in_range(node__, mm__, start__, end__)	\
504	for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
505	     node__->start < (end__);					\
506	     node__ = list_next_entry(node__, node_list))
507
508void drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
509				 struct drm_mm *mm,
510				 u64 size, u64 alignment, unsigned long color,
511				 u64 start, u64 end,
512				 enum drm_mm_insert_mode mode);
513
514/**
515 * drm_mm_scan_init - initialize lru scanning
516 * @scan: scan state
517 * @mm: drm_mm to scan
518 * @size: size of the allocation
519 * @alignment: alignment of the allocation
520 * @color: opaque tag value to use for the allocation
521 * @mode: fine-tune the allocation search and placement
522 *
523 * This is a simplified version of drm_mm_scan_init_with_range() with no range
524 * restrictions applied.
525 *
526 * This simply sets up the scanning routines with the parameters for the desired
527 * hole.
528 *
529 * Warning:
530 * As long as the scan list is non-empty, no other operations than
531 * adding/removing nodes to/from the scan list are allowed.
532 */
533static inline void drm_mm_scan_init(struct drm_mm_scan *scan,
534				    struct drm_mm *mm,
535				    u64 size,
536				    u64 alignment,
537				    unsigned long color,
538				    enum drm_mm_insert_mode mode)
539{
540	drm_mm_scan_init_with_range(scan, mm,
541				    size, alignment, color,
542				    0, U64_MAX, mode);
543}
544
545bool drm_mm_scan_add_block(struct drm_mm_scan *scan,
546			   struct drm_mm_node *node);
547bool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
548			      struct drm_mm_node *node);
549struct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
550
551void drm_mm_print(const struct drm_mm *mm, struct drm_printer *p);
552
553#endif
v4.10.11
  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/rbtree.h>
 41#include <linux/kernel.h>
 
 42#include <linux/list.h>
 43#include <linux/spinlock.h>
 44#ifdef CONFIG_DEBUG_FS
 45#include <linux/seq_file.h>
 46#endif
 47#ifdef CONFIG_DRM_DEBUG_MM
 48#include <linux/stackdepot.h>
 49#endif
 
 
 
 50
 51enum drm_mm_search_flags {
 52	DRM_MM_SEARCH_DEFAULT =		0,
 53	DRM_MM_SEARCH_BEST =		1 << 0,
 54	DRM_MM_SEARCH_BELOW =		1 << 1,
 55};
 56
 57enum drm_mm_allocator_flags {
 58	DRM_MM_CREATE_DEFAULT =		0,
 59	DRM_MM_CREATE_TOP =		1 << 0,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 60};
 61
 62#define DRM_MM_BOTTOMUP DRM_MM_SEARCH_DEFAULT, DRM_MM_CREATE_DEFAULT
 63#define DRM_MM_TOPDOWN DRM_MM_SEARCH_BELOW, DRM_MM_CREATE_TOP
 64
 
 
 
 
 
 
 65struct drm_mm_node {
 
 
 
 
 
 
 
 
 66	struct list_head node_list;
 67	struct list_head hole_stack;
 68	struct rb_node rb;
 69	unsigned hole_follows : 1;
 70	unsigned scanned_block : 1;
 71	unsigned scanned_prev_free : 1;
 72	unsigned scanned_next_free : 1;
 73	unsigned scanned_preceeds_hole : 1;
 74	unsigned allocated : 1;
 75	unsigned long color;
 76	u64 start;
 77	u64 size;
 78	u64 __subtree_last;
 79	struct drm_mm *mm;
 
 
 
 
 80#ifdef CONFIG_DRM_DEBUG_MM
 81	depot_stack_handle_t stack;
 82#endif
 83};
 84
 
 
 
 
 
 
 
 
 85struct drm_mm {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 86	/* List of all memory nodes that immediately precede a free hole. */
 87	struct list_head hole_stack;
 88	/* head_node.node_list is the list of all memory nodes, ordered
 89	 * according to the (increasing) start address of the memory node. */
 90	struct drm_mm_node head_node;
 91	/* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
 92	struct rb_root interval_tree;
 
 
 93
 94	unsigned int scan_check_range : 1;
 95	unsigned scan_alignment;
 96	unsigned long scan_color;
 97	u64 scan_size;
 98	u64 scan_hit_start;
 99	u64 scan_hit_end;
100	unsigned scanned_blocks;
101	u64 scan_start;
102	u64 scan_end;
103	struct drm_mm_node *prev_scanned_node;
 
 
 
 
 
104
105	void (*color_adjust)(struct drm_mm_node *node, unsigned long color,
106			     u64 *start, u64 *end);
 
 
 
 
 
 
 
 
 
 
107};
108
109/**
110 * drm_mm_node_allocated - checks whether a node is allocated
111 * @node: drm_mm_node to check
112 *
113 * Drivers should use this helpers for proper encapusulation of drm_mm
 
 
 
114 * internals.
115 *
116 * Returns:
117 * True if the @node is allocated.
118 */
119static inline bool drm_mm_node_allocated(struct drm_mm_node *node)
120{
121	return node->allocated;
122}
123
124/**
125 * drm_mm_initialized - checks whether an allocator is initialized
126 * @mm: drm_mm to check
127 *
128 * Drivers should use this helpers for proper encapusulation of drm_mm
 
 
 
129 * internals.
130 *
131 * Returns:
132 * True if the @mm is initialized.
133 */
134static inline bool drm_mm_initialized(struct drm_mm *mm)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135{
136	return mm->hole_stack.next;
137}
138
139static inline u64 __drm_mm_hole_node_start(struct drm_mm_node *hole_node)
140{
141	return hole_node->start + hole_node->size;
142}
143
144/**
145 * drm_mm_hole_node_start - computes the start of the hole following @node
146 * @hole_node: drm_mm_node which implicitly tracks the following hole
147 *
148 * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
149 * inspect holes themselves. Drivers must check first whether a hole indeed
150 * follows by looking at node->hole_follows.
151 *
152 * Returns:
153 * Start of the subsequent hole.
154 */
155static inline u64 drm_mm_hole_node_start(struct drm_mm_node *hole_node)
156{
157	BUG_ON(!hole_node->hole_follows);
158	return __drm_mm_hole_node_start(hole_node);
159}
160
161static inline u64 __drm_mm_hole_node_end(struct drm_mm_node *hole_node)
162{
163	return list_next_entry(hole_node, node_list)->start;
164}
165
166/**
167 * drm_mm_hole_node_end - computes the end of the hole following @node
168 * @hole_node: drm_mm_node which implicitly tracks the following hole
169 *
170 * This is useful for driver-sepific debug dumpers. Otherwise drivers should not
171 * inspect holes themselves. Drivers must check first whether a hole indeed
172 * follows by looking at node->hole_follows.
173 *
174 * Returns:
175 * End of the subsequent hole.
176 */
177static inline u64 drm_mm_hole_node_end(struct drm_mm_node *hole_node)
178{
179	return __drm_mm_hole_node_end(hole_node);
180}
181
182/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183 * drm_mm_for_each_node - iterator to walk over all allocated nodes
184 * @entry: drm_mm_node structure to assign to in each iteration step
185 * @mm: drm_mm allocator to walk
 
 
 
 
 
 
 
 
 
 
 
 
186 *
187 * This iterator walks over all nodes in the range allocator. It is implemented
188 * with list_for_each, so not save against removal of elements.
189 */
190#define drm_mm_for_each_node(entry, mm) list_for_each_entry(entry, \
191						&(mm)->head_node.node_list, \
192						node_list)
193
194#define __drm_mm_for_each_hole(entry, mm, hole_start, hole_end, backwards) \
195	for (entry = list_entry((backwards) ? (mm)->hole_stack.prev : (mm)->hole_stack.next, struct drm_mm_node, hole_stack); \
196	     &entry->hole_stack != &(mm)->hole_stack ? \
197	     hole_start = drm_mm_hole_node_start(entry), \
198	     hole_end = drm_mm_hole_node_end(entry), \
199	     1 : 0; \
200	     entry = list_entry((backwards) ? entry->hole_stack.prev : entry->hole_stack.next, struct drm_mm_node, hole_stack))
201
202/**
203 * drm_mm_for_each_hole - iterator to walk over all holes
204 * @entry: drm_mm_node used internally to track progress
205 * @mm: drm_mm allocator to walk
206 * @hole_start: ulong variable to assign the hole start to on each iteration
207 * @hole_end: ulong variable to assign the hole end to on each iteration
208 *
209 * This iterator walks over all holes in the range allocator. It is implemented
210 * with list_for_each, so not save against removal of elements. @entry is used
211 * internally and will not reflect a real drm_mm_node for the very first hole.
212 * Hence users of this iterator may not access it.
213 *
214 * Implementation Note:
215 * We need to inline list_for_each_entry in order to be able to set hole_start
216 * and hole_end on each iteration while keeping the macro sane.
217 *
218 * The __drm_mm_for_each_hole version is similar, but with added support for
219 * going backwards.
220 */
221#define drm_mm_for_each_hole(entry, mm, hole_start, hole_end) \
222	__drm_mm_for_each_hole(entry, mm, hole_start, hole_end, 0)
 
 
 
 
 
 
223
224/*
225 * Basic range manager support (drm_mm.c)
226 */
227int drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
 
 
 
 
 
 
 
 
228
229int drm_mm_insert_node_generic(struct drm_mm *mm,
230			       struct drm_mm_node *node,
231			       u64 size,
232			       unsigned alignment,
233			       unsigned long color,
234			       enum drm_mm_search_flags sflags,
235			       enum drm_mm_allocator_flags aflags);
236/**
237 * drm_mm_insert_node - search for space and insert @node
238 * @mm: drm_mm to allocate from
239 * @node: preallocate node to insert
240 * @size: size of the allocation
241 * @alignment: alignment of the allocation
242 * @flags: flags to fine-tune the allocation
 
243 *
244 * This is a simplified version of drm_mm_insert_node_generic() with @color set
245 * to 0.
246 *
247 * The preallocated node must be cleared to 0.
248 *
249 * Returns:
250 * 0 on success, -ENOSPC if there's no suitable hole.
251 */
252static inline int drm_mm_insert_node(struct drm_mm *mm,
253				     struct drm_mm_node *node,
254				     u64 size,
255				     unsigned alignment,
256				     enum drm_mm_search_flags flags)
257{
258	return drm_mm_insert_node_generic(mm, node, size, alignment, 0, flags,
259					  DRM_MM_CREATE_DEFAULT);
260}
261
262int drm_mm_insert_node_in_range_generic(struct drm_mm *mm,
263					struct drm_mm_node *node,
264					u64 size,
265					unsigned alignment,
266					unsigned long color,
267					u64 start,
268					u64 end,
269					enum drm_mm_search_flags sflags,
270					enum drm_mm_allocator_flags aflags);
271/**
272 * drm_mm_insert_node_in_range - ranged search for space and insert @node
273 * @mm: drm_mm to allocate from
274 * @node: preallocate node to insert
275 * @size: size of the allocation
276 * @alignment: alignment of the allocation
277 * @start: start of the allowed range for this node
278 * @end: end of the allowed range for this node
279 * @flags: flags to fine-tune the allocation
280 *
281 * This is a simplified version of drm_mm_insert_node_in_range_generic() with
282 * @color set to 0.
283 *
284 * The preallocated node must be cleared to 0.
285 *
286 * Returns:
287 * 0 on success, -ENOSPC if there's no suitable hole.
288 */
289static inline int drm_mm_insert_node_in_range(struct drm_mm *mm,
290					      struct drm_mm_node *node,
291					      u64 size,
292					      unsigned alignment,
293					      u64 start,
294					      u64 end,
295					      enum drm_mm_search_flags flags)
296{
297	return drm_mm_insert_node_in_range_generic(mm, node, size, alignment,
298						   0, start, end, flags,
299						   DRM_MM_CREATE_DEFAULT);
300}
301
302void drm_mm_remove_node(struct drm_mm_node *node);
303void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
304void drm_mm_init(struct drm_mm *mm,
305		 u64 start,
306		 u64 size);
307void drm_mm_takedown(struct drm_mm *mm);
308bool drm_mm_clean(struct drm_mm *mm);
 
 
 
 
 
 
 
 
 
 
 
 
309
310struct drm_mm_node *
311__drm_mm_interval_first(struct drm_mm *mm, u64 start, u64 last);
312
313/**
314 * drm_mm_for_each_node_in_range - iterator to walk over a range of
315 * allocated nodes
316 * @node__: drm_mm_node structure to assign to in each iteration step
317 * @mm__: drm_mm allocator to walk
318 * @start__: starting offset, the first node will overlap this
319 * @end__: ending offset, the last node will start before this (but may overlap)
320 *
321 * This iterator walks over all nodes in the range allocator that lie
322 * between @start and @end. It is implemented similarly to list_for_each(),
323 * but using the internal interval tree to accelerate the search for the
324 * starting node, and so not safe against removal of elements. It assumes
325 * that @end is within (or is the upper limit of) the drm_mm allocator.
 
 
 
326 */
327#define drm_mm_for_each_node_in_range(node__, mm__, start__, end__)	\
328	for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
329	     node__ && node__->start < (end__);				\
330	     node__ = list_next_entry(node__, node_list))
331
332void drm_mm_init_scan(struct drm_mm *mm,
333		      u64 size,
334		      unsigned alignment,
335		      unsigned long color);
336void drm_mm_init_scan_with_range(struct drm_mm *mm,
337				 u64 size,
338				 unsigned alignment,
339				 unsigned long color,
340				 u64 start,
341				 u64 end);
342bool drm_mm_scan_add_block(struct drm_mm_node *node);
343bool drm_mm_scan_remove_block(struct drm_mm_node *node);
344
345void drm_mm_debug_table(struct drm_mm *mm, const char *prefix);
346#ifdef CONFIG_DEBUG_FS
347int drm_mm_dump_table(struct seq_file *m, struct drm_mm *mm);
348#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
350#endif